field_access_in_value.carbon 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. // INCLUDE-FILE: toolchain/testing/testdata/min_prelude/int.carbon
  6. // TODO: Add ranges and switch to "--dump-sem-ir-ranges=only".
  7. // EXTRA-ARGS: --dump-sem-ir-ranges=if-present
  8. //
  9. // AUTOUPDATE
  10. // TIP: To test this file alone, run:
  11. // TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/class/field_access_in_value.carbon
  12. // TIP: To dump output, run:
  13. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/class/field_access_in_value.carbon
  14. class Class {
  15. var j: i32;
  16. var k: i32;
  17. }
  18. fn Test() {
  19. var cv: Class;
  20. cv.j = 1;
  21. cv.k = 2;
  22. let c: Class = cv;
  23. var cj: i32 = c.j;
  24. var ck: i32 = c.k;
  25. }
  26. // CHECK:STDOUT: --- field_access_in_value.carbon
  27. // CHECK:STDOUT:
  28. // CHECK:STDOUT: constants {
  29. // CHECK:STDOUT: %Class: type = class_type @Class [concrete]
  30. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete]
  31. // CHECK:STDOUT: %Int.type: type = generic_class_type @Int [concrete]
  32. // CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [concrete]
  33. // CHECK:STDOUT: %Int.generic: %Int.type = struct_value () [concrete]
  34. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [concrete]
  35. // CHECK:STDOUT: %Class.elem: type = unbound_element_type %Class, %i32 [concrete]
  36. // CHECK:STDOUT: %struct_type.j.k: type = struct_type {.j: %i32, .k: %i32} [concrete]
  37. // CHECK:STDOUT: %complete_type.cf7: <witness> = complete_type_witness %struct_type.j.k [concrete]
  38. // CHECK:STDOUT: %Test.type: type = fn_type @Test [concrete]
  39. // CHECK:STDOUT: %Test: %Test.type = struct_value () [concrete]
  40. // CHECK:STDOUT: %pattern_type.761: type = pattern_type %Class [concrete]
  41. // CHECK:STDOUT: %int_1.5b8: Core.IntLiteral = int_value 1 [concrete]
  42. // CHECK:STDOUT: %ImplicitAs.type.cc7: type = generic_interface_type @ImplicitAs [concrete]
  43. // CHECK:STDOUT: %ImplicitAs.generic: %ImplicitAs.type.cc7 = struct_value () [concrete]
  44. // CHECK:STDOUT: %ImplicitAs.type.205: type = facet_type <@ImplicitAs, @ImplicitAs(%i32)> [concrete]
  45. // CHECK:STDOUT: %Convert.type.1b6: type = fn_type @Convert.2, @ImplicitAs(%i32) [concrete]
  46. // CHECK:STDOUT: %To: Core.IntLiteral = bind_symbolic_name To, 0 [symbolic]
  47. // CHECK:STDOUT: %Convert.type.0f9: type = fn_type @Convert.1, @ImplicitAs.impl.4f9(%To) [symbolic]
  48. // CHECK:STDOUT: %Convert.f06: %Convert.type.0f9 = struct_value () [symbolic]
  49. // CHECK:STDOUT: %ImplicitAs.impl_witness.c75: <witness> = impl_witness imports.%ImplicitAs.impl_witness_table.a2f, @ImplicitAs.impl.4f9(%int_32) [concrete]
  50. // CHECK:STDOUT: %Convert.type.035: type = fn_type @Convert.1, @ImplicitAs.impl.4f9(%int_32) [concrete]
  51. // CHECK:STDOUT: %Convert.956: %Convert.type.035 = struct_value () [concrete]
  52. // CHECK:STDOUT: %ImplicitAs.facet: %ImplicitAs.type.205 = facet_value Core.IntLiteral, (%ImplicitAs.impl_witness.c75) [concrete]
  53. // CHECK:STDOUT: %.9c3: type = fn_type_with_self_type %Convert.type.1b6, %ImplicitAs.facet [concrete]
  54. // CHECK:STDOUT: %Convert.bound.ab5: <bound method> = bound_method %int_1.5b8, %Convert.956 [concrete]
  55. // CHECK:STDOUT: %pattern_type.7ce: type = pattern_type %i32 [concrete]
  56. // CHECK:STDOUT: %Convert.specific_fn: <specific function> = specific_function %Convert.956, @Convert.1(%int_32) [concrete]
  57. // CHECK:STDOUT: %bound_method.9a1: <bound method> = bound_method %int_1.5b8, %Convert.specific_fn [concrete]
  58. // CHECK:STDOUT: %int_1.5d2: %i32 = int_value 1 [concrete]
  59. // CHECK:STDOUT: %int_2.ecc: Core.IntLiteral = int_value 2 [concrete]
  60. // CHECK:STDOUT: %Convert.bound.ef9: <bound method> = bound_method %int_2.ecc, %Convert.956 [concrete]
  61. // CHECK:STDOUT: %bound_method.b92: <bound method> = bound_method %int_2.ecc, %Convert.specific_fn [concrete]
  62. // CHECK:STDOUT: %int_2.ef8: %i32 = int_value 2 [concrete]
  63. // CHECK:STDOUT: %Destroy.type: type = facet_type <@Destroy> [concrete]
  64. // CHECK:STDOUT: %Op.type.a17: type = fn_type @Op.1, @Destroy.impl(%i32) [concrete]
  65. // CHECK:STDOUT: %Op.e6a: %Op.type.a17 = struct_value () [concrete]
  66. // CHECK:STDOUT: %ptr.235: type = ptr_type %i32 [concrete]
  67. // CHECK:STDOUT: %Op.specific_fn.014: <specific function> = specific_function %Op.e6a, @Op.1(%i32) [concrete]
  68. // CHECK:STDOUT: %Op.type.7de: type = fn_type @Op.1, @Destroy.impl(%Class) [concrete]
  69. // CHECK:STDOUT: %Op.d64: %Op.type.7de = struct_value () [concrete]
  70. // CHECK:STDOUT: %ptr.e71: type = ptr_type %Class [concrete]
  71. // CHECK:STDOUT: %Op.specific_fn.618: <specific function> = specific_function %Op.d64, @Op.1(%Class) [concrete]
  72. // CHECK:STDOUT: }
  73. // CHECK:STDOUT:
  74. // CHECK:STDOUT: imports {
  75. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [concrete] {
  76. // CHECK:STDOUT: .Int = %Core.Int
  77. // CHECK:STDOUT: .ImplicitAs = %Core.ImplicitAs
  78. // CHECK:STDOUT: .Destroy = %Core.Destroy
  79. // CHECK:STDOUT: import Core//prelude
  80. // CHECK:STDOUT: import Core//prelude/...
  81. // CHECK:STDOUT: }
  82. // CHECK:STDOUT: %Core.Int: %Int.type = import_ref Core//prelude/parts/int, Int, loaded [concrete = constants.%Int.generic]
  83. // CHECK:STDOUT: %Core.ImplicitAs: %ImplicitAs.type.cc7 = import_ref Core//prelude/parts/as, ImplicitAs, loaded [concrete = constants.%ImplicitAs.generic]
  84. // CHECK:STDOUT: %Core.import_ref.a5b: @ImplicitAs.impl.4f9.%Convert.type (%Convert.type.0f9) = import_ref Core//prelude/parts/int, loc16_39, loaded [symbolic = @ImplicitAs.impl.4f9.%Convert (constants.%Convert.f06)]
  85. // CHECK:STDOUT: %ImplicitAs.impl_witness_table.a2f = impl_witness_table (%Core.import_ref.a5b), @ImplicitAs.impl.4f9 [concrete]
  86. // CHECK:STDOUT: %Core.Destroy: type = import_ref Core//prelude/parts/destroy, Destroy, loaded [concrete = constants.%Destroy.type]
  87. // CHECK:STDOUT: }
  88. // CHECK:STDOUT:
  89. // CHECK:STDOUT: file {
  90. // CHECK:STDOUT: package: <namespace> = namespace [concrete] {
  91. // CHECK:STDOUT: .Core = imports.%Core
  92. // CHECK:STDOUT: .Class = %Class.decl
  93. // CHECK:STDOUT: .Test = %Test.decl
  94. // CHECK:STDOUT: }
  95. // CHECK:STDOUT: %Core.import = import Core
  96. // CHECK:STDOUT: %Class.decl: type = class_decl @Class [concrete = constants.%Class] {} {}
  97. // CHECK:STDOUT: %Test.decl: %Test.type = fn_decl @Test [concrete = constants.%Test] {} {}
  98. // CHECK:STDOUT: }
  99. // CHECK:STDOUT:
  100. // CHECK:STDOUT: class @Class {
  101. // CHECK:STDOUT: %int_32.loc16: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  102. // CHECK:STDOUT: %i32.loc16: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  103. // CHECK:STDOUT: %.loc16: %Class.elem = field_decl j, element0 [concrete]
  104. // CHECK:STDOUT: %int_32.loc17: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  105. // CHECK:STDOUT: %i32.loc17: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  106. // CHECK:STDOUT: %.loc17: %Class.elem = field_decl k, element1 [concrete]
  107. // CHECK:STDOUT: %struct_type.j.k: type = struct_type {.j: %i32, .k: %i32} [concrete = constants.%struct_type.j.k]
  108. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %struct_type.j.k [concrete = constants.%complete_type.cf7]
  109. // CHECK:STDOUT: complete_type_witness = %complete_type
  110. // CHECK:STDOUT:
  111. // CHECK:STDOUT: !members:
  112. // CHECK:STDOUT: .Self = constants.%Class
  113. // CHECK:STDOUT: .j = %.loc16
  114. // CHECK:STDOUT: .k = %.loc17
  115. // CHECK:STDOUT: }
  116. // CHECK:STDOUT:
  117. // CHECK:STDOUT: fn @Test() {
  118. // CHECK:STDOUT: !entry:
  119. // CHECK:STDOUT: name_binding_decl {
  120. // CHECK:STDOUT: %cv.patt: %pattern_type.761 = binding_pattern cv [concrete]
  121. // CHECK:STDOUT: %cv.var_patt: %pattern_type.761 = var_pattern %cv.patt [concrete]
  122. // CHECK:STDOUT: }
  123. // CHECK:STDOUT: %cv.var: ref %Class = var %cv.var_patt
  124. // CHECK:STDOUT: %Class.ref.loc21: type = name_ref Class, file.%Class.decl [concrete = constants.%Class]
  125. // CHECK:STDOUT: %cv: ref %Class = bind_name cv, %cv.var
  126. // CHECK:STDOUT: %cv.ref.loc22: ref %Class = name_ref cv, %cv
  127. // CHECK:STDOUT: %j.ref.loc22: %Class.elem = name_ref j, @Class.%.loc16 [concrete = @Class.%.loc16]
  128. // CHECK:STDOUT: %.loc22_5: ref %i32 = class_element_access %cv.ref.loc22, element0
  129. // CHECK:STDOUT: %int_1: Core.IntLiteral = int_value 1 [concrete = constants.%int_1.5b8]
  130. // CHECK:STDOUT: %impl.elem0.loc22: %.9c3 = impl_witness_access constants.%ImplicitAs.impl_witness.c75, element0 [concrete = constants.%Convert.956]
  131. // CHECK:STDOUT: %bound_method.loc22_8.1: <bound method> = bound_method %int_1, %impl.elem0.loc22 [concrete = constants.%Convert.bound.ab5]
  132. // CHECK:STDOUT: %specific_fn.loc22: <specific function> = specific_function %impl.elem0.loc22, @Convert.1(constants.%int_32) [concrete = constants.%Convert.specific_fn]
  133. // CHECK:STDOUT: %bound_method.loc22_8.2: <bound method> = bound_method %int_1, %specific_fn.loc22 [concrete = constants.%bound_method.9a1]
  134. // CHECK:STDOUT: %int.convert_checked.loc22: init %i32 = call %bound_method.loc22_8.2(%int_1) [concrete = constants.%int_1.5d2]
  135. // CHECK:STDOUT: %.loc22_8: init %i32 = converted %int_1, %int.convert_checked.loc22 [concrete = constants.%int_1.5d2]
  136. // CHECK:STDOUT: assign %.loc22_5, %.loc22_8
  137. // CHECK:STDOUT: %cv.ref.loc23: ref %Class = name_ref cv, %cv
  138. // CHECK:STDOUT: %k.ref.loc23: %Class.elem = name_ref k, @Class.%.loc17 [concrete = @Class.%.loc17]
  139. // CHECK:STDOUT: %.loc23_5: ref %i32 = class_element_access %cv.ref.loc23, element1
  140. // CHECK:STDOUT: %int_2: Core.IntLiteral = int_value 2 [concrete = constants.%int_2.ecc]
  141. // CHECK:STDOUT: %impl.elem0.loc23: %.9c3 = impl_witness_access constants.%ImplicitAs.impl_witness.c75, element0 [concrete = constants.%Convert.956]
  142. // CHECK:STDOUT: %bound_method.loc23_8.1: <bound method> = bound_method %int_2, %impl.elem0.loc23 [concrete = constants.%Convert.bound.ef9]
  143. // CHECK:STDOUT: %specific_fn.loc23: <specific function> = specific_function %impl.elem0.loc23, @Convert.1(constants.%int_32) [concrete = constants.%Convert.specific_fn]
  144. // CHECK:STDOUT: %bound_method.loc23_8.2: <bound method> = bound_method %int_2, %specific_fn.loc23 [concrete = constants.%bound_method.b92]
  145. // CHECK:STDOUT: %int.convert_checked.loc23: init %i32 = call %bound_method.loc23_8.2(%int_2) [concrete = constants.%int_2.ef8]
  146. // CHECK:STDOUT: %.loc23_8: init %i32 = converted %int_2, %int.convert_checked.loc23 [concrete = constants.%int_2.ef8]
  147. // CHECK:STDOUT: assign %.loc23_5, %.loc23_8
  148. // CHECK:STDOUT: name_binding_decl {
  149. // CHECK:STDOUT: %c.patt: %pattern_type.761 = binding_pattern c [concrete]
  150. // CHECK:STDOUT: }
  151. // CHECK:STDOUT: %cv.ref.loc24: ref %Class = name_ref cv, %cv
  152. // CHECK:STDOUT: %Class.ref.loc24: type = name_ref Class, file.%Class.decl [concrete = constants.%Class]
  153. // CHECK:STDOUT: %.loc24: %Class = bind_value %cv.ref.loc24
  154. // CHECK:STDOUT: %c: %Class = bind_name c, %.loc24
  155. // CHECK:STDOUT: name_binding_decl {
  156. // CHECK:STDOUT: %cj.patt: %pattern_type.7ce = binding_pattern cj [concrete]
  157. // CHECK:STDOUT: %cj.var_patt: %pattern_type.7ce = var_pattern %cj.patt [concrete]
  158. // CHECK:STDOUT: }
  159. // CHECK:STDOUT: %cj.var: ref %i32 = var %cj.var_patt
  160. // CHECK:STDOUT: %c.ref.loc25: %Class = name_ref c, %c
  161. // CHECK:STDOUT: %j.ref.loc25: %Class.elem = name_ref j, @Class.%.loc16 [concrete = @Class.%.loc16]
  162. // CHECK:STDOUT: %.loc25_18.1: ref %i32 = class_element_access %c.ref.loc25, element0
  163. // CHECK:STDOUT: %.loc25_18.2: %i32 = bind_value %.loc25_18.1
  164. // CHECK:STDOUT: assign %cj.var, %.loc25_18.2
  165. // CHECK:STDOUT: %.loc25_11: type = splice_block %i32.loc25 [concrete = constants.%i32] {
  166. // CHECK:STDOUT: %int_32.loc25: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  167. // CHECK:STDOUT: %i32.loc25: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  168. // CHECK:STDOUT: }
  169. // CHECK:STDOUT: %cj: ref %i32 = bind_name cj, %cj.var
  170. // CHECK:STDOUT: name_binding_decl {
  171. // CHECK:STDOUT: %ck.patt: %pattern_type.7ce = binding_pattern ck [concrete]
  172. // CHECK:STDOUT: %ck.var_patt: %pattern_type.7ce = var_pattern %ck.patt [concrete]
  173. // CHECK:STDOUT: }
  174. // CHECK:STDOUT: %ck.var: ref %i32 = var %ck.var_patt
  175. // CHECK:STDOUT: %c.ref.loc26: %Class = name_ref c, %c
  176. // CHECK:STDOUT: %k.ref.loc26: %Class.elem = name_ref k, @Class.%.loc17 [concrete = @Class.%.loc17]
  177. // CHECK:STDOUT: %.loc26_18.1: ref %i32 = class_element_access %c.ref.loc26, element1
  178. // CHECK:STDOUT: %.loc26_18.2: %i32 = bind_value %.loc26_18.1
  179. // CHECK:STDOUT: assign %ck.var, %.loc26_18.2
  180. // CHECK:STDOUT: %.loc26_11: type = splice_block %i32.loc26 [concrete = constants.%i32] {
  181. // CHECK:STDOUT: %int_32.loc26: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  182. // CHECK:STDOUT: %i32.loc26: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  183. // CHECK:STDOUT: }
  184. // CHECK:STDOUT: %ck: ref %i32 = bind_name ck, %ck.var
  185. // CHECK:STDOUT: %Op.bound.loc26: <bound method> = bound_method %ck.var, constants.%Op.e6a
  186. // CHECK:STDOUT: %Op.specific_fn.1: <specific function> = specific_function constants.%Op.e6a, @Op.1(constants.%i32) [concrete = constants.%Op.specific_fn.014]
  187. // CHECK:STDOUT: %bound_method.loc26: <bound method> = bound_method %ck.var, %Op.specific_fn.1
  188. // CHECK:STDOUT: %addr.loc26: %ptr.235 = addr_of %ck.var
  189. // CHECK:STDOUT: %no_op.loc26: init %empty_tuple.type = call %bound_method.loc26(%addr.loc26)
  190. // CHECK:STDOUT: %Op.bound.loc25: <bound method> = bound_method %cj.var, constants.%Op.e6a
  191. // CHECK:STDOUT: %Op.specific_fn.2: <specific function> = specific_function constants.%Op.e6a, @Op.1(constants.%i32) [concrete = constants.%Op.specific_fn.014]
  192. // CHECK:STDOUT: %bound_method.loc25: <bound method> = bound_method %cj.var, %Op.specific_fn.2
  193. // CHECK:STDOUT: %addr.loc25: %ptr.235 = addr_of %cj.var
  194. // CHECK:STDOUT: %no_op.loc25: init %empty_tuple.type = call %bound_method.loc25(%addr.loc25)
  195. // CHECK:STDOUT: %Op.bound.loc21: <bound method> = bound_method %cv.var, constants.%Op.d64
  196. // CHECK:STDOUT: %Op.specific_fn.3: <specific function> = specific_function constants.%Op.d64, @Op.1(constants.%Class) [concrete = constants.%Op.specific_fn.618]
  197. // CHECK:STDOUT: %bound_method.loc21: <bound method> = bound_method %cv.var, %Op.specific_fn.3
  198. // CHECK:STDOUT: %addr.loc21: %ptr.e71 = addr_of %cv.var
  199. // CHECK:STDOUT: %no_op.loc21: init %empty_tuple.type = call %bound_method.loc21(%addr.loc21)
  200. // CHECK:STDOUT: return
  201. // CHECK:STDOUT: }
  202. // CHECK:STDOUT: