field_access.carbon 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. //
  5. // AUTOUPDATE
  6. // TIP: To test this file alone, run:
  7. // TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/class/field_access.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/class/field_access.carbon
  10. class Class {
  11. var j: i32;
  12. var k: i32;
  13. }
  14. fn Run() {
  15. var c: Class;
  16. c.j = 1;
  17. c.k = 2;
  18. var cj: i32 = c.j;
  19. var ck: i32 = c.k;
  20. }
  21. // CHECK:STDOUT: --- field_access.carbon
  22. // CHECK:STDOUT:
  23. // CHECK:STDOUT: constants {
  24. // CHECK:STDOUT: %Class: type = class_type @Class [template]
  25. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [template]
  26. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [template]
  27. // CHECK:STDOUT: %Class.elem: type = unbound_element_type %Class, %i32 [template]
  28. // CHECK:STDOUT: %struct_type.j.k: type = struct_type {.j: %i32, .k: %i32} [template]
  29. // CHECK:STDOUT: %complete_type.f45: <witness> = complete_type_witness %struct_type.j.k [template]
  30. // CHECK:STDOUT: %Run.type: type = fn_type @Run [template]
  31. // CHECK:STDOUT: %Run: %Run.type = struct_value () [template]
  32. // CHECK:STDOUT: %int_1.5b8: Core.IntLiteral = int_value 1 [template]
  33. // CHECK:STDOUT: %Convert.type.cd1: type = fn_type @Convert.1, @ImplicitAs(%i32) [template]
  34. // CHECK:STDOUT: %impl_witness.5b0: <witness> = impl_witness (imports.%import_ref.723), @impl.1(%int_32) [template]
  35. // CHECK:STDOUT: %Convert.type.466: type = fn_type @Convert.2, @impl.1(%int_32) [template]
  36. // CHECK:STDOUT: %Convert.925: %Convert.type.466 = struct_value () [template]
  37. // CHECK:STDOUT: %Convert.bound.afd: <bound method> = bound_method %int_1.5b8, %Convert.925 [template]
  38. // CHECK:STDOUT: %Convert.specific_fn.b73: <specific function> = specific_function %Convert.bound.afd, @Convert.2(%int_32) [template]
  39. // CHECK:STDOUT: %int_1.c60: %i32 = int_value 1 [template]
  40. // CHECK:STDOUT: %int_2.ecc: Core.IntLiteral = int_value 2 [template]
  41. // CHECK:STDOUT: %Convert.bound.f0b: <bound method> = bound_method %int_2.ecc, %Convert.925 [template]
  42. // CHECK:STDOUT: %Convert.specific_fn.20e: <specific function> = specific_function %Convert.bound.f0b, @Convert.2(%int_32) [template]
  43. // CHECK:STDOUT: %int_2.166: %i32 = int_value 2 [template]
  44. // CHECK:STDOUT: }
  45. // CHECK:STDOUT:
  46. // CHECK:STDOUT: imports {
  47. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  48. // CHECK:STDOUT: .Int = %import_ref.187
  49. // CHECK:STDOUT: .ImplicitAs = %import_ref.a69
  50. // CHECK:STDOUT: import Core//prelude
  51. // CHECK:STDOUT: import Core//prelude/...
  52. // CHECK:STDOUT: }
  53. // CHECK:STDOUT: }
  54. // CHECK:STDOUT:
  55. // CHECK:STDOUT: file {
  56. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  57. // CHECK:STDOUT: .Core = imports.%Core
  58. // CHECK:STDOUT: .Class = %Class.decl
  59. // CHECK:STDOUT: .Run = %Run.decl
  60. // CHECK:STDOUT: }
  61. // CHECK:STDOUT: %Core.import = import Core
  62. // CHECK:STDOUT: %Class.decl: type = class_decl @Class [template = constants.%Class] {} {}
  63. // CHECK:STDOUT: %Run.decl: %Run.type = fn_decl @Run [template = constants.%Run] {} {}
  64. // CHECK:STDOUT: }
  65. // CHECK:STDOUT:
  66. // CHECK:STDOUT: class @Class {
  67. // CHECK:STDOUT: %.loc12: %Class.elem = field_decl j, element0 [template]
  68. // CHECK:STDOUT: %.loc13: %Class.elem = field_decl k, element1 [template]
  69. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %struct_type.j.k [template = constants.%complete_type.f45]
  70. // CHECK:STDOUT:
  71. // CHECK:STDOUT: !members:
  72. // CHECK:STDOUT: .Self = constants.%Class
  73. // CHECK:STDOUT: .j = %.loc12
  74. // CHECK:STDOUT: .k = %.loc13
  75. // CHECK:STDOUT: complete_type_witness = %complete_type
  76. // CHECK:STDOUT: }
  77. // CHECK:STDOUT:
  78. // CHECK:STDOUT: fn @Run() {
  79. // CHECK:STDOUT: !entry:
  80. // CHECK:STDOUT: %c.var: ref %Class = var c
  81. // CHECK:STDOUT: %c: ref %Class = bind_name c, %c.var
  82. // CHECK:STDOUT: %c.ref.loc18: ref %Class = name_ref c, %c
  83. // CHECK:STDOUT: %j.ref.loc18: %Class.elem = name_ref j, @Class.%.loc12 [template = @Class.%.loc12]
  84. // CHECK:STDOUT: %.loc18_4: ref %i32 = class_element_access %c.ref.loc18, element0
  85. // CHECK:STDOUT: %int_1: Core.IntLiteral = int_value 1 [template = constants.%int_1.5b8]
  86. // CHECK:STDOUT: %impl.elem0.loc18: %Convert.type.cd1 = impl_witness_access constants.%impl_witness.5b0, element0 [template = constants.%Convert.925]
  87. // CHECK:STDOUT: %Convert.bound.loc18: <bound method> = bound_method %int_1, %impl.elem0.loc18 [template = constants.%Convert.bound.afd]
  88. // CHECK:STDOUT: %Convert.specific_fn.loc18: <specific function> = specific_function %Convert.bound.loc18, @Convert.2(constants.%int_32) [template = constants.%Convert.specific_fn.b73]
  89. // CHECK:STDOUT: %int.convert_checked.loc18: init %i32 = call %Convert.specific_fn.loc18(%int_1) [template = constants.%int_1.c60]
  90. // CHECK:STDOUT: %.loc18_7: init %i32 = converted %int_1, %int.convert_checked.loc18 [template = constants.%int_1.c60]
  91. // CHECK:STDOUT: assign %.loc18_4, %.loc18_7
  92. // CHECK:STDOUT: %c.ref.loc19: ref %Class = name_ref c, %c
  93. // CHECK:STDOUT: %k.ref.loc19: %Class.elem = name_ref k, @Class.%.loc13 [template = @Class.%.loc13]
  94. // CHECK:STDOUT: %.loc19_4: ref %i32 = class_element_access %c.ref.loc19, element1
  95. // CHECK:STDOUT: %int_2: Core.IntLiteral = int_value 2 [template = constants.%int_2.ecc]
  96. // CHECK:STDOUT: %impl.elem0.loc19: %Convert.type.cd1 = impl_witness_access constants.%impl_witness.5b0, element0 [template = constants.%Convert.925]
  97. // CHECK:STDOUT: %Convert.bound.loc19: <bound method> = bound_method %int_2, %impl.elem0.loc19 [template = constants.%Convert.bound.f0b]
  98. // CHECK:STDOUT: %Convert.specific_fn.loc19: <specific function> = specific_function %Convert.bound.loc19, @Convert.2(constants.%int_32) [template = constants.%Convert.specific_fn.20e]
  99. // CHECK:STDOUT: %int.convert_checked.loc19: init %i32 = call %Convert.specific_fn.loc19(%int_2) [template = constants.%int_2.166]
  100. // CHECK:STDOUT: %.loc19_7: init %i32 = converted %int_2, %int.convert_checked.loc19 [template = constants.%int_2.166]
  101. // CHECK:STDOUT: assign %.loc19_4, %.loc19_7
  102. // CHECK:STDOUT: %cj.var: ref %i32 = var cj
  103. // CHECK:STDOUT: %cj: ref %i32 = bind_name cj, %cj.var
  104. // CHECK:STDOUT: %c.ref.loc20: ref %Class = name_ref c, %c
  105. // CHECK:STDOUT: %j.ref.loc20: %Class.elem = name_ref j, @Class.%.loc12 [template = @Class.%.loc12]
  106. // CHECK:STDOUT: %.loc20_18.1: ref %i32 = class_element_access %c.ref.loc20, element0
  107. // CHECK:STDOUT: %.loc20_18.2: %i32 = bind_value %.loc20_18.1
  108. // CHECK:STDOUT: assign %cj.var, %.loc20_18.2
  109. // CHECK:STDOUT: %ck.var: ref %i32 = var ck
  110. // CHECK:STDOUT: %ck: ref %i32 = bind_name ck, %ck.var
  111. // CHECK:STDOUT: %c.ref.loc21: ref %Class = name_ref c, %c
  112. // CHECK:STDOUT: %k.ref.loc21: %Class.elem = name_ref k, @Class.%.loc13 [template = @Class.%.loc13]
  113. // CHECK:STDOUT: %.loc21_18.1: ref %i32 = class_element_access %c.ref.loc21, element1
  114. // CHECK:STDOUT: %.loc21_18.2: %i32 = bind_value %.loc21_18.1
  115. // CHECK:STDOUT: assign %ck.var, %.loc21_18.2
  116. // CHECK:STDOUT: return
  117. // CHECK:STDOUT: }
  118. // CHECK:STDOUT: