address_of_lvalue.carbon 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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/pointer/address_of_lvalue.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/pointer/address_of_lvalue.carbon
  10. fn F() {
  11. var s: {.a: i32, .b: i32} = {.a = 1, .b = 2};
  12. var p: {.a: i32, .b: i32}* = &s;
  13. var q: i32* = &s.a;
  14. var r: i32* = &s.b;
  15. var t: (i32, i32) = (1, 2);
  16. var t0: i32* = &t.0;
  17. var t1: i32* = &t.1;
  18. }
  19. // CHECK:STDOUT: --- address_of_lvalue.carbon
  20. // CHECK:STDOUT:
  21. // CHECK:STDOUT: constants {
  22. // CHECK:STDOUT: %F.type: type = fn_type @F [template]
  23. // CHECK:STDOUT: %F: %F.type = struct_value () [template]
  24. // CHECK:STDOUT: %.1: Core.IntLiteral = int_value 32 [template]
  25. // CHECK:STDOUT: %Int.type: type = fn_type @Int [template]
  26. // CHECK:STDOUT: %Int: %Int.type = struct_value () [template]
  27. // CHECK:STDOUT: %i32: type = int_type signed, %.1 [template]
  28. // CHECK:STDOUT: %.2: type = struct_type {.a: %i32, .b: %i32} [template]
  29. // CHECK:STDOUT: %.3: type = ptr_type %.2 [template]
  30. // CHECK:STDOUT: %.4: Core.IntLiteral = int_value 1 [template]
  31. // CHECK:STDOUT: %.5: Core.IntLiteral = int_value 2 [template]
  32. // CHECK:STDOUT: %.6: type = struct_type {.a: Core.IntLiteral, .b: Core.IntLiteral} [template]
  33. // CHECK:STDOUT: %Convert.type.2: type = fn_type @Convert.1, @ImplicitAs(%i32) [template]
  34. // CHECK:STDOUT: %Convert.type.14: type = fn_type @Convert.2, @impl.1(%.1) [template]
  35. // CHECK:STDOUT: %Convert.14: %Convert.type.14 = struct_value () [template]
  36. // CHECK:STDOUT: %.26: <witness> = interface_witness (%Convert.14) [template]
  37. // CHECK:STDOUT: %.27: <bound method> = bound_method %.4, %Convert.14 [template]
  38. // CHECK:STDOUT: %.28: <specific function> = specific_function %.27, @Convert.2(%.1) [template]
  39. // CHECK:STDOUT: %.29: %i32 = int_value 1 [template]
  40. // CHECK:STDOUT: %.30: <bound method> = bound_method %.5, %Convert.14 [template]
  41. // CHECK:STDOUT: %.31: <specific function> = specific_function %.30, @Convert.2(%.1) [template]
  42. // CHECK:STDOUT: %.32: %i32 = int_value 2 [template]
  43. // CHECK:STDOUT: %struct: %.2 = struct_value (%.29, %.32) [template]
  44. // CHECK:STDOUT: %.33: type = ptr_type %i32 [template]
  45. // CHECK:STDOUT: %tuple.type.1: type = tuple_type (type, type) [template]
  46. // CHECK:STDOUT: %tuple.type.2: type = tuple_type (%i32, %i32) [template]
  47. // CHECK:STDOUT: %tuple.type.3: type = tuple_type (Core.IntLiteral, Core.IntLiteral) [template]
  48. // CHECK:STDOUT: %tuple: %tuple.type.2 = tuple_value (%.29, %.32) [template]
  49. // CHECK:STDOUT: %.35: Core.IntLiteral = int_value 0 [template]
  50. // CHECK:STDOUT: }
  51. // CHECK:STDOUT:
  52. // CHECK:STDOUT: imports {
  53. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  54. // CHECK:STDOUT: .Int = %import_ref.1
  55. // CHECK:STDOUT: .ImplicitAs = %import_ref.2
  56. // CHECK:STDOUT: import Core//prelude
  57. // CHECK:STDOUT: import Core//prelude/...
  58. // CHECK:STDOUT: }
  59. // CHECK:STDOUT: }
  60. // CHECK:STDOUT:
  61. // CHECK:STDOUT: file {
  62. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  63. // CHECK:STDOUT: .Core = imports.%Core
  64. // CHECK:STDOUT: .F = %F.decl
  65. // CHECK:STDOUT: }
  66. // CHECK:STDOUT: %Core.import = import Core
  67. // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [template = constants.%F] {} {}
  68. // CHECK:STDOUT: }
  69. // CHECK:STDOUT:
  70. // CHECK:STDOUT: fn @F() {
  71. // CHECK:STDOUT: !entry:
  72. // CHECK:STDOUT: %.loc12_15.1: Core.IntLiteral = int_value 32 [template = constants.%.1]
  73. // CHECK:STDOUT: %int.make_type_signed.loc12_15: init type = call constants.%Int(%.loc12_15.1) [template = constants.%i32]
  74. // CHECK:STDOUT: %.loc12_15.2: type = value_of_initializer %int.make_type_signed.loc12_15 [template = constants.%i32]
  75. // CHECK:STDOUT: %.loc12_15.3: type = converted %int.make_type_signed.loc12_15, %.loc12_15.2 [template = constants.%i32]
  76. // CHECK:STDOUT: %.loc12_24.1: Core.IntLiteral = int_value 32 [template = constants.%.1]
  77. // CHECK:STDOUT: %int.make_type_signed.loc12_24: init type = call constants.%Int(%.loc12_24.1) [template = constants.%i32]
  78. // CHECK:STDOUT: %.loc12_24.2: type = value_of_initializer %int.make_type_signed.loc12_24 [template = constants.%i32]
  79. // CHECK:STDOUT: %.loc12_24.3: type = converted %int.make_type_signed.loc12_24, %.loc12_24.2 [template = constants.%i32]
  80. // CHECK:STDOUT: %.loc12_27: type = struct_type {.a: %i32, .b: %i32} [template = constants.%.2]
  81. // CHECK:STDOUT: %s.var: ref %.2 = var s
  82. // CHECK:STDOUT: %s: ref %.2 = bind_name s, %s.var
  83. // CHECK:STDOUT: %.loc12_37: Core.IntLiteral = int_value 1 [template = constants.%.4]
  84. // CHECK:STDOUT: %.loc12_45: Core.IntLiteral = int_value 2 [template = constants.%.5]
  85. // CHECK:STDOUT: %.loc12_46.1: %.6 = struct_literal (%.loc12_37, %.loc12_45)
  86. // CHECK:STDOUT: %.loc12_46.2: %Convert.type.2 = interface_witness_access constants.%.26, element0 [template = constants.%Convert.14]
  87. // CHECK:STDOUT: %.loc12_46.3: <bound method> = bound_method %.loc12_37, %.loc12_46.2 [template = constants.%.27]
  88. // CHECK:STDOUT: %.loc12_46.4: <specific function> = specific_function %.loc12_46.3, @Convert.2(constants.%.1) [template = constants.%.28]
  89. // CHECK:STDOUT: %int.convert_checked.loc12_46.1: init %i32 = call %.loc12_46.4(%.loc12_37) [template = constants.%.29]
  90. // CHECK:STDOUT: %.loc12_46.5: init %i32 = converted %.loc12_37, %int.convert_checked.loc12_46.1 [template = constants.%.29]
  91. // CHECK:STDOUT: %.loc12_46.6: ref %i32 = struct_access %s.var, element0
  92. // CHECK:STDOUT: %.loc12_46.7: init %i32 = initialize_from %.loc12_46.5 to %.loc12_46.6 [template = constants.%.29]
  93. // CHECK:STDOUT: %.loc12_46.8: %Convert.type.2 = interface_witness_access constants.%.26, element0 [template = constants.%Convert.14]
  94. // CHECK:STDOUT: %.loc12_46.9: <bound method> = bound_method %.loc12_45, %.loc12_46.8 [template = constants.%.30]
  95. // CHECK:STDOUT: %.loc12_46.10: <specific function> = specific_function %.loc12_46.9, @Convert.2(constants.%.1) [template = constants.%.31]
  96. // CHECK:STDOUT: %int.convert_checked.loc12_46.2: init %i32 = call %.loc12_46.10(%.loc12_45) [template = constants.%.32]
  97. // CHECK:STDOUT: %.loc12_46.11: init %i32 = converted %.loc12_45, %int.convert_checked.loc12_46.2 [template = constants.%.32]
  98. // CHECK:STDOUT: %.loc12_46.12: ref %i32 = struct_access %s.var, element1
  99. // CHECK:STDOUT: %.loc12_46.13: init %i32 = initialize_from %.loc12_46.11 to %.loc12_46.12 [template = constants.%.32]
  100. // CHECK:STDOUT: %.loc12_46.14: init %.2 = struct_init (%.loc12_46.7, %.loc12_46.13) to %s.var [template = constants.%struct]
  101. // CHECK:STDOUT: %.loc12_47: init %.2 = converted %.loc12_46.1, %.loc12_46.14 [template = constants.%struct]
  102. // CHECK:STDOUT: assign %s.var, %.loc12_47
  103. // CHECK:STDOUT: %.loc14_15.1: Core.IntLiteral = int_value 32 [template = constants.%.1]
  104. // CHECK:STDOUT: %int.make_type_signed.loc14_15: init type = call constants.%Int(%.loc14_15.1) [template = constants.%i32]
  105. // CHECK:STDOUT: %.loc14_15.2: type = value_of_initializer %int.make_type_signed.loc14_15 [template = constants.%i32]
  106. // CHECK:STDOUT: %.loc14_15.3: type = converted %int.make_type_signed.loc14_15, %.loc14_15.2 [template = constants.%i32]
  107. // CHECK:STDOUT: %.loc14_24.1: Core.IntLiteral = int_value 32 [template = constants.%.1]
  108. // CHECK:STDOUT: %int.make_type_signed.loc14_24: init type = call constants.%Int(%.loc14_24.1) [template = constants.%i32]
  109. // CHECK:STDOUT: %.loc14_24.2: type = value_of_initializer %int.make_type_signed.loc14_24 [template = constants.%i32]
  110. // CHECK:STDOUT: %.loc14_24.3: type = converted %int.make_type_signed.loc14_24, %.loc14_24.2 [template = constants.%i32]
  111. // CHECK:STDOUT: %.loc14_27: type = struct_type {.a: %i32, .b: %i32} [template = constants.%.2]
  112. // CHECK:STDOUT: %.loc14_28: type = ptr_type %.2 [template = constants.%.3]
  113. // CHECK:STDOUT: %p.var: ref %.3 = var p
  114. // CHECK:STDOUT: %p: ref %.3 = bind_name p, %p.var
  115. // CHECK:STDOUT: %s.ref.loc14: ref %.2 = name_ref s, %s
  116. // CHECK:STDOUT: %.loc14_32: %.3 = addr_of %s.ref.loc14
  117. // CHECK:STDOUT: assign %p.var, %.loc14_32
  118. // CHECK:STDOUT: %.loc15_10: Core.IntLiteral = int_value 32 [template = constants.%.1]
  119. // CHECK:STDOUT: %int.make_type_signed.loc15: init type = call constants.%Int(%.loc15_10) [template = constants.%i32]
  120. // CHECK:STDOUT: %.loc15_13.1: type = value_of_initializer %int.make_type_signed.loc15 [template = constants.%i32]
  121. // CHECK:STDOUT: %.loc15_13.2: type = converted %int.make_type_signed.loc15, %.loc15_13.1 [template = constants.%i32]
  122. // CHECK:STDOUT: %.loc15_13.3: type = ptr_type %i32 [template = constants.%.33]
  123. // CHECK:STDOUT: %q.var: ref %.33 = var q
  124. // CHECK:STDOUT: %q: ref %.33 = bind_name q, %q.var
  125. // CHECK:STDOUT: %s.ref.loc15: ref %.2 = name_ref s, %s
  126. // CHECK:STDOUT: %.loc15_19: ref %i32 = struct_access %s.ref.loc15, element0
  127. // CHECK:STDOUT: %.loc15_17: %.33 = addr_of %.loc15_19
  128. // CHECK:STDOUT: assign %q.var, %.loc15_17
  129. // CHECK:STDOUT: %.loc16_10: Core.IntLiteral = int_value 32 [template = constants.%.1]
  130. // CHECK:STDOUT: %int.make_type_signed.loc16: init type = call constants.%Int(%.loc16_10) [template = constants.%i32]
  131. // CHECK:STDOUT: %.loc16_13.1: type = value_of_initializer %int.make_type_signed.loc16 [template = constants.%i32]
  132. // CHECK:STDOUT: %.loc16_13.2: type = converted %int.make_type_signed.loc16, %.loc16_13.1 [template = constants.%i32]
  133. // CHECK:STDOUT: %.loc16_13.3: type = ptr_type %i32 [template = constants.%.33]
  134. // CHECK:STDOUT: %r.var: ref %.33 = var r
  135. // CHECK:STDOUT: %r: ref %.33 = bind_name r, %r.var
  136. // CHECK:STDOUT: %s.ref.loc16: ref %.2 = name_ref s, %s
  137. // CHECK:STDOUT: %.loc16_19: ref %i32 = struct_access %s.ref.loc16, element1
  138. // CHECK:STDOUT: %.loc16_17: %.33 = addr_of %.loc16_19
  139. // CHECK:STDOUT: assign %r.var, %.loc16_17
  140. // CHECK:STDOUT: %.loc18_11: Core.IntLiteral = int_value 32 [template = constants.%.1]
  141. // CHECK:STDOUT: %int.make_type_signed.loc18_11: init type = call constants.%Int(%.loc18_11) [template = constants.%i32]
  142. // CHECK:STDOUT: %.loc18_16: Core.IntLiteral = int_value 32 [template = constants.%.1]
  143. // CHECK:STDOUT: %int.make_type_signed.loc18_16: init type = call constants.%Int(%.loc18_16) [template = constants.%i32]
  144. // CHECK:STDOUT: %.loc18_19.1: %tuple.type.1 = tuple_literal (%int.make_type_signed.loc18_11, %int.make_type_signed.loc18_16)
  145. // CHECK:STDOUT: %.loc18_19.2: type = value_of_initializer %int.make_type_signed.loc18_11 [template = constants.%i32]
  146. // CHECK:STDOUT: %.loc18_19.3: type = converted %int.make_type_signed.loc18_11, %.loc18_19.2 [template = constants.%i32]
  147. // CHECK:STDOUT: %.loc18_19.4: type = value_of_initializer %int.make_type_signed.loc18_16 [template = constants.%i32]
  148. // CHECK:STDOUT: %.loc18_19.5: type = converted %int.make_type_signed.loc18_16, %.loc18_19.4 [template = constants.%i32]
  149. // CHECK:STDOUT: %.loc18_19.6: type = converted %.loc18_19.1, constants.%tuple.type.2 [template = constants.%tuple.type.2]
  150. // CHECK:STDOUT: %t.var: ref %tuple.type.2 = var t
  151. // CHECK:STDOUT: %t: ref %tuple.type.2 = bind_name t, %t.var
  152. // CHECK:STDOUT: %.loc18_24: Core.IntLiteral = int_value 1 [template = constants.%.4]
  153. // CHECK:STDOUT: %.loc18_27: Core.IntLiteral = int_value 2 [template = constants.%.5]
  154. // CHECK:STDOUT: %.loc18_28.1: %tuple.type.3 = tuple_literal (%.loc18_24, %.loc18_27)
  155. // CHECK:STDOUT: %.loc18_28.2: %Convert.type.2 = interface_witness_access constants.%.26, element0 [template = constants.%Convert.14]
  156. // CHECK:STDOUT: %.loc18_28.3: <bound method> = bound_method %.loc18_24, %.loc18_28.2 [template = constants.%.27]
  157. // CHECK:STDOUT: %.loc18_28.4: <specific function> = specific_function %.loc18_28.3, @Convert.2(constants.%.1) [template = constants.%.28]
  158. // CHECK:STDOUT: %int.convert_checked.loc18_28.1: init %i32 = call %.loc18_28.4(%.loc18_24) [template = constants.%.29]
  159. // CHECK:STDOUT: %.loc18_28.5: init %i32 = converted %.loc18_24, %int.convert_checked.loc18_28.1 [template = constants.%.29]
  160. // CHECK:STDOUT: %.loc18_28.6: ref %i32 = tuple_access %t.var, element0
  161. // CHECK:STDOUT: %.loc18_28.7: init %i32 = initialize_from %.loc18_28.5 to %.loc18_28.6 [template = constants.%.29]
  162. // CHECK:STDOUT: %.loc18_28.8: %Convert.type.2 = interface_witness_access constants.%.26, element0 [template = constants.%Convert.14]
  163. // CHECK:STDOUT: %.loc18_28.9: <bound method> = bound_method %.loc18_27, %.loc18_28.8 [template = constants.%.30]
  164. // CHECK:STDOUT: %.loc18_28.10: <specific function> = specific_function %.loc18_28.9, @Convert.2(constants.%.1) [template = constants.%.31]
  165. // CHECK:STDOUT: %int.convert_checked.loc18_28.2: init %i32 = call %.loc18_28.10(%.loc18_27) [template = constants.%.32]
  166. // CHECK:STDOUT: %.loc18_28.11: init %i32 = converted %.loc18_27, %int.convert_checked.loc18_28.2 [template = constants.%.32]
  167. // CHECK:STDOUT: %.loc18_28.12: ref %i32 = tuple_access %t.var, element1
  168. // CHECK:STDOUT: %.loc18_28.13: init %i32 = initialize_from %.loc18_28.11 to %.loc18_28.12 [template = constants.%.32]
  169. // CHECK:STDOUT: %.loc18_28.14: init %tuple.type.2 = tuple_init (%.loc18_28.7, %.loc18_28.13) to %t.var [template = constants.%tuple]
  170. // CHECK:STDOUT: %.loc18_29: init %tuple.type.2 = converted %.loc18_28.1, %.loc18_28.14 [template = constants.%tuple]
  171. // CHECK:STDOUT: assign %t.var, %.loc18_29
  172. // CHECK:STDOUT: %.loc19_11: Core.IntLiteral = int_value 32 [template = constants.%.1]
  173. // CHECK:STDOUT: %int.make_type_signed.loc19: init type = call constants.%Int(%.loc19_11) [template = constants.%i32]
  174. // CHECK:STDOUT: %.loc19_14.1: type = value_of_initializer %int.make_type_signed.loc19 [template = constants.%i32]
  175. // CHECK:STDOUT: %.loc19_14.2: type = converted %int.make_type_signed.loc19, %.loc19_14.1 [template = constants.%i32]
  176. // CHECK:STDOUT: %.loc19_14.3: type = ptr_type %i32 [template = constants.%.33]
  177. // CHECK:STDOUT: %t0.var: ref %.33 = var t0
  178. // CHECK:STDOUT: %t0: ref %.33 = bind_name t0, %t0.var
  179. // CHECK:STDOUT: %t.ref.loc19: ref %tuple.type.2 = name_ref t, %t
  180. // CHECK:STDOUT: %.loc19_21: Core.IntLiteral = int_value 0 [template = constants.%.35]
  181. // CHECK:STDOUT: %.loc19_20: ref %i32 = tuple_access %t.ref.loc19, element0
  182. // CHECK:STDOUT: %.loc19_18: %.33 = addr_of %.loc19_20
  183. // CHECK:STDOUT: assign %t0.var, %.loc19_18
  184. // CHECK:STDOUT: %.loc20_11: Core.IntLiteral = int_value 32 [template = constants.%.1]
  185. // CHECK:STDOUT: %int.make_type_signed.loc20: init type = call constants.%Int(%.loc20_11) [template = constants.%i32]
  186. // CHECK:STDOUT: %.loc20_14.1: type = value_of_initializer %int.make_type_signed.loc20 [template = constants.%i32]
  187. // CHECK:STDOUT: %.loc20_14.2: type = converted %int.make_type_signed.loc20, %.loc20_14.1 [template = constants.%i32]
  188. // CHECK:STDOUT: %.loc20_14.3: type = ptr_type %i32 [template = constants.%.33]
  189. // CHECK:STDOUT: %t1.var: ref %.33 = var t1
  190. // CHECK:STDOUT: %t1: ref %.33 = bind_name t1, %t1.var
  191. // CHECK:STDOUT: %t.ref.loc20: ref %tuple.type.2 = name_ref t, %t
  192. // CHECK:STDOUT: %.loc20_21: Core.IntLiteral = int_value 1 [template = constants.%.4]
  193. // CHECK:STDOUT: %.loc20_20: ref %i32 = tuple_access %t.ref.loc20, element1
  194. // CHECK:STDOUT: %.loc20_18: %.33 = addr_of %.loc20_20
  195. // CHECK:STDOUT: assign %t1.var, %.loc20_18
  196. // CHECK:STDOUT: return
  197. // CHECK:STDOUT: }
  198. // CHECK:STDOUT: