address_of_lvalue.carbon 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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: %.1: type = tuple_type () [template]
  24. // CHECK:STDOUT: %F: %F.type = struct_value () [template]
  25. // CHECK:STDOUT: %Int32.type: type = fn_type @Int32 [template]
  26. // CHECK:STDOUT: %Int32: %Int32.type = struct_value () [template]
  27. // CHECK:STDOUT: %.2: type = struct_type {.a: i32, .b: i32} [template]
  28. // CHECK:STDOUT: %.3: type = ptr_type %.2 [template]
  29. // CHECK:STDOUT: %.4: i32 = int_literal 1 [template]
  30. // CHECK:STDOUT: %.5: i32 = int_literal 2 [template]
  31. // CHECK:STDOUT: %struct: %.2 = struct_value (%.4, %.5) [template]
  32. // CHECK:STDOUT: %.6: type = ptr_type i32 [template]
  33. // CHECK:STDOUT: %.7: type = tuple_type (type, type) [template]
  34. // CHECK:STDOUT: %.8: type = tuple_type (i32, i32) [template]
  35. // CHECK:STDOUT: %.9: type = ptr_type %.8 [template]
  36. // CHECK:STDOUT: %tuple: %.8 = tuple_value (%.4, %.5) [template]
  37. // CHECK:STDOUT: %.10: i32 = int_literal 0 [template]
  38. // CHECK:STDOUT: }
  39. // CHECK:STDOUT:
  40. // CHECK:STDOUT: file {
  41. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  42. // CHECK:STDOUT: .Core = %Core
  43. // CHECK:STDOUT: .F = %F.decl
  44. // CHECK:STDOUT: }
  45. // CHECK:STDOUT: %Core: <namespace> = namespace [template] {}
  46. // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [template = constants.%F] {}
  47. // CHECK:STDOUT: %import_ref.1: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  48. // CHECK:STDOUT: %import_ref.2: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  49. // CHECK:STDOUT: %import_ref.3: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  50. // CHECK:STDOUT: %import_ref.4: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  51. // CHECK:STDOUT: %import_ref.5: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  52. // CHECK:STDOUT: %import_ref.6: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  53. // CHECK:STDOUT: %import_ref.7: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  54. // CHECK:STDOUT: %import_ref.8: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  55. // CHECK:STDOUT: %import_ref.9: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  56. // CHECK:STDOUT: %import_ref.10: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  57. // CHECK:STDOUT: }
  58. // CHECK:STDOUT:
  59. // CHECK:STDOUT: fn @F() {
  60. // CHECK:STDOUT: !entry:
  61. // CHECK:STDOUT: %int.make_type_32.loc12_15: init type = call constants.%Int32() [template = i32]
  62. // CHECK:STDOUT: %.loc12_15.1: type = value_of_initializer %int.make_type_32.loc12_15 [template = i32]
  63. // CHECK:STDOUT: %.loc12_15.2: type = converted %int.make_type_32.loc12_15, %.loc12_15.1 [template = i32]
  64. // CHECK:STDOUT: %int.make_type_32.loc12_24: init type = call constants.%Int32() [template = i32]
  65. // CHECK:STDOUT: %.loc12_24.1: type = value_of_initializer %int.make_type_32.loc12_24 [template = i32]
  66. // CHECK:STDOUT: %.loc12_24.2: type = converted %int.make_type_32.loc12_24, %.loc12_24.1 [template = i32]
  67. // CHECK:STDOUT: %.loc12_27: type = struct_type {.a: i32, .b: i32} [template = constants.%.2]
  68. // CHECK:STDOUT: %s.var: ref %.2 = var s
  69. // CHECK:STDOUT: %s: ref %.2 = bind_name s, %s.var
  70. // CHECK:STDOUT: %.loc12_37: i32 = int_literal 1 [template = constants.%.4]
  71. // CHECK:STDOUT: %.loc12_45: i32 = int_literal 2 [template = constants.%.5]
  72. // CHECK:STDOUT: %.loc12_46.1: %.2 = struct_literal (%.loc12_37, %.loc12_45)
  73. // CHECK:STDOUT: %.loc12_46.2: ref i32 = struct_access %s.var, element0
  74. // CHECK:STDOUT: %.loc12_46.3: init i32 = initialize_from %.loc12_37 to %.loc12_46.2 [template = constants.%.4]
  75. // CHECK:STDOUT: %.loc12_46.4: ref i32 = struct_access %s.var, element1
  76. // CHECK:STDOUT: %.loc12_46.5: init i32 = initialize_from %.loc12_45 to %.loc12_46.4 [template = constants.%.5]
  77. // CHECK:STDOUT: %.loc12_46.6: init %.2 = struct_init (%.loc12_46.3, %.loc12_46.5) to %s.var [template = constants.%struct]
  78. // CHECK:STDOUT: %.loc12_47: init %.2 = converted %.loc12_46.1, %.loc12_46.6 [template = constants.%struct]
  79. // CHECK:STDOUT: assign %s.var, %.loc12_47
  80. // CHECK:STDOUT: %int.make_type_32.loc14_15: init type = call constants.%Int32() [template = i32]
  81. // CHECK:STDOUT: %.loc14_15.1: type = value_of_initializer %int.make_type_32.loc14_15 [template = i32]
  82. // CHECK:STDOUT: %.loc14_15.2: type = converted %int.make_type_32.loc14_15, %.loc14_15.1 [template = i32]
  83. // CHECK:STDOUT: %int.make_type_32.loc14_24: init type = call constants.%Int32() [template = i32]
  84. // CHECK:STDOUT: %.loc14_24.1: type = value_of_initializer %int.make_type_32.loc14_24 [template = i32]
  85. // CHECK:STDOUT: %.loc14_24.2: type = converted %int.make_type_32.loc14_24, %.loc14_24.1 [template = i32]
  86. // CHECK:STDOUT: %.loc14_27: type = struct_type {.a: i32, .b: i32} [template = constants.%.2]
  87. // CHECK:STDOUT: %.loc14_28: type = ptr_type %.2 [template = constants.%.3]
  88. // CHECK:STDOUT: %p.var: ref %.3 = var p
  89. // CHECK:STDOUT: %p: ref %.3 = bind_name p, %p.var
  90. // CHECK:STDOUT: %s.ref.loc14: ref %.2 = name_ref s, %s
  91. // CHECK:STDOUT: %.loc14_32: %.3 = addr_of %s.ref.loc14
  92. // CHECK:STDOUT: assign %p.var, %.loc14_32
  93. // CHECK:STDOUT: %int.make_type_32.loc15: init type = call constants.%Int32() [template = i32]
  94. // CHECK:STDOUT: %.loc15_13.1: type = value_of_initializer %int.make_type_32.loc15 [template = i32]
  95. // CHECK:STDOUT: %.loc15_13.2: type = converted %int.make_type_32.loc15, %.loc15_13.1 [template = i32]
  96. // CHECK:STDOUT: %.loc15_13.3: type = ptr_type i32 [template = constants.%.6]
  97. // CHECK:STDOUT: %q.var: ref %.6 = var q
  98. // CHECK:STDOUT: %q: ref %.6 = bind_name q, %q.var
  99. // CHECK:STDOUT: %s.ref.loc15: ref %.2 = name_ref s, %s
  100. // CHECK:STDOUT: %.loc15_19: ref i32 = struct_access %s.ref.loc15, element0
  101. // CHECK:STDOUT: %.loc15_17: %.6 = addr_of %.loc15_19
  102. // CHECK:STDOUT: assign %q.var, %.loc15_17
  103. // CHECK:STDOUT: %int.make_type_32.loc16: init type = call constants.%Int32() [template = i32]
  104. // CHECK:STDOUT: %.loc16_13.1: type = value_of_initializer %int.make_type_32.loc16 [template = i32]
  105. // CHECK:STDOUT: %.loc16_13.2: type = converted %int.make_type_32.loc16, %.loc16_13.1 [template = i32]
  106. // CHECK:STDOUT: %.loc16_13.3: type = ptr_type i32 [template = constants.%.6]
  107. // CHECK:STDOUT: %r.var: ref %.6 = var r
  108. // CHECK:STDOUT: %r: ref %.6 = bind_name r, %r.var
  109. // CHECK:STDOUT: %s.ref.loc16: ref %.2 = name_ref s, %s
  110. // CHECK:STDOUT: %.loc16_19: ref i32 = struct_access %s.ref.loc16, element1
  111. // CHECK:STDOUT: %.loc16_17: %.6 = addr_of %.loc16_19
  112. // CHECK:STDOUT: assign %r.var, %.loc16_17
  113. // CHECK:STDOUT: %int.make_type_32.loc18_11: init type = call constants.%Int32() [template = i32]
  114. // CHECK:STDOUT: %int.make_type_32.loc18_16: init type = call constants.%Int32() [template = i32]
  115. // CHECK:STDOUT: %.loc18_19.1: %.7 = tuple_literal (%int.make_type_32.loc18_11, %int.make_type_32.loc18_16)
  116. // CHECK:STDOUT: %.loc18_19.2: type = value_of_initializer %int.make_type_32.loc18_11 [template = i32]
  117. // CHECK:STDOUT: %.loc18_19.3: type = converted %int.make_type_32.loc18_11, %.loc18_19.2 [template = i32]
  118. // CHECK:STDOUT: %.loc18_19.4: type = value_of_initializer %int.make_type_32.loc18_16 [template = i32]
  119. // CHECK:STDOUT: %.loc18_19.5: type = converted %int.make_type_32.loc18_16, %.loc18_19.4 [template = i32]
  120. // CHECK:STDOUT: %.loc18_19.6: type = converted %.loc18_19.1, constants.%.8 [template = constants.%.8]
  121. // CHECK:STDOUT: %t.var: ref %.8 = var t
  122. // CHECK:STDOUT: %t: ref %.8 = bind_name t, %t.var
  123. // CHECK:STDOUT: %.loc18_24: i32 = int_literal 1 [template = constants.%.4]
  124. // CHECK:STDOUT: %.loc18_27: i32 = int_literal 2 [template = constants.%.5]
  125. // CHECK:STDOUT: %.loc18_28.1: %.8 = tuple_literal (%.loc18_24, %.loc18_27)
  126. // CHECK:STDOUT: %.loc18_28.2: ref i32 = tuple_access %t.var, element0
  127. // CHECK:STDOUT: %.loc18_28.3: init i32 = initialize_from %.loc18_24 to %.loc18_28.2 [template = constants.%.4]
  128. // CHECK:STDOUT: %.loc18_28.4: ref i32 = tuple_access %t.var, element1
  129. // CHECK:STDOUT: %.loc18_28.5: init i32 = initialize_from %.loc18_27 to %.loc18_28.4 [template = constants.%.5]
  130. // CHECK:STDOUT: %.loc18_28.6: init %.8 = tuple_init (%.loc18_28.3, %.loc18_28.5) to %t.var [template = constants.%tuple]
  131. // CHECK:STDOUT: %.loc18_29: init %.8 = converted %.loc18_28.1, %.loc18_28.6 [template = constants.%tuple]
  132. // CHECK:STDOUT: assign %t.var, %.loc18_29
  133. // CHECK:STDOUT: %int.make_type_32.loc19: init type = call constants.%Int32() [template = i32]
  134. // CHECK:STDOUT: %.loc19_14.1: type = value_of_initializer %int.make_type_32.loc19 [template = i32]
  135. // CHECK:STDOUT: %.loc19_14.2: type = converted %int.make_type_32.loc19, %.loc19_14.1 [template = i32]
  136. // CHECK:STDOUT: %.loc19_14.3: type = ptr_type i32 [template = constants.%.6]
  137. // CHECK:STDOUT: %t0.var: ref %.6 = var t0
  138. // CHECK:STDOUT: %t0: ref %.6 = bind_name t0, %t0.var
  139. // CHECK:STDOUT: %t.ref.loc19: ref %.8 = name_ref t, %t
  140. // CHECK:STDOUT: %.loc19_21: i32 = int_literal 0 [template = constants.%.10]
  141. // CHECK:STDOUT: %.loc19_22: ref i32 = tuple_index %t.ref.loc19, %.loc19_21
  142. // CHECK:STDOUT: %.loc19_18: %.6 = addr_of %.loc19_22
  143. // CHECK:STDOUT: assign %t0.var, %.loc19_18
  144. // CHECK:STDOUT: %int.make_type_32.loc20: init type = call constants.%Int32() [template = i32]
  145. // CHECK:STDOUT: %.loc20_14.1: type = value_of_initializer %int.make_type_32.loc20 [template = i32]
  146. // CHECK:STDOUT: %.loc20_14.2: type = converted %int.make_type_32.loc20, %.loc20_14.1 [template = i32]
  147. // CHECK:STDOUT: %.loc20_14.3: type = ptr_type i32 [template = constants.%.6]
  148. // CHECK:STDOUT: %t1.var: ref %.6 = var t1
  149. // CHECK:STDOUT: %t1: ref %.6 = bind_name t1, %t1.var
  150. // CHECK:STDOUT: %t.ref.loc20: ref %.8 = name_ref t, %t
  151. // CHECK:STDOUT: %.loc20_21: i32 = int_literal 1 [template = constants.%.4]
  152. // CHECK:STDOUT: %.loc20_22: ref i32 = tuple_index %t.ref.loc20, %.loc20_21
  153. // CHECK:STDOUT: %.loc20_18: %.6 = addr_of %.loc20_22
  154. // CHECK:STDOUT: assign %t1.var, %.loc20_18
  155. // CHECK:STDOUT: return
  156. // CHECK:STDOUT: }
  157. // CHECK:STDOUT:
  158. // CHECK:STDOUT: fn @Int32() -> type = "int.make_type_32";
  159. // CHECK:STDOUT: