address_of_lvalue.carbon 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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: %int_32: Core.IntLiteral = int_value 32 [template]
  25. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [template]
  26. // CHECK:STDOUT: %struct_type.a.b.3de: type = struct_type {.a: %i32, .b: %i32} [template]
  27. // CHECK:STDOUT: %ptr.570: type = ptr_type %struct_type.a.b.3de [template]
  28. // CHECK:STDOUT: %int_1.5b8: Core.IntLiteral = int_value 1 [template]
  29. // CHECK:STDOUT: %int_2.ecc: Core.IntLiteral = int_value 2 [template]
  30. // CHECK:STDOUT: %struct_type.a.b.cfd: type = struct_type {.a: Core.IntLiteral, .b: Core.IntLiteral} [template]
  31. // CHECK:STDOUT: %Convert.type.cd1: type = fn_type @Convert.1, @ImplicitAs(%i32) [template]
  32. // CHECK:STDOUT: %impl_witness.5b0: <witness> = impl_witness (imports.%import_ref.723), @impl.1(%int_32) [template]
  33. // CHECK:STDOUT: %Convert.type.466: type = fn_type @Convert.2, @impl.1(%int_32) [template]
  34. // CHECK:STDOUT: %Convert.925: %Convert.type.466 = struct_value () [template]
  35. // CHECK:STDOUT: %Convert.bound.afd: <bound method> = bound_method %int_1.5b8, %Convert.925 [template]
  36. // CHECK:STDOUT: %Convert.specific_fn.b73: <specific function> = specific_function %Convert.bound.afd, @Convert.2(%int_32) [template]
  37. // CHECK:STDOUT: %int_1.c60: %i32 = int_value 1 [template]
  38. // CHECK:STDOUT: %Convert.bound.f0b: <bound method> = bound_method %int_2.ecc, %Convert.925 [template]
  39. // CHECK:STDOUT: %Convert.specific_fn.20e: <specific function> = specific_function %Convert.bound.f0b, @Convert.2(%int_32) [template]
  40. // CHECK:STDOUT: %int_2.166: %i32 = int_value 2 [template]
  41. // CHECK:STDOUT: %struct: %struct_type.a.b.3de = struct_value (%int_1.c60, %int_2.166) [template]
  42. // CHECK:STDOUT: %ptr.dc3: type = ptr_type %i32 [template]
  43. // CHECK:STDOUT: %tuple.type.471: type = tuple_type (%i32, %i32) [template]
  44. // CHECK:STDOUT: %tuple.type.f94: type = tuple_type (Core.IntLiteral, Core.IntLiteral) [template]
  45. // CHECK:STDOUT: %tuple: %tuple.type.471 = tuple_value (%int_1.c60, %int_2.166) [template]
  46. // CHECK:STDOUT: %int_0: Core.IntLiteral = int_value 0 [template]
  47. // CHECK:STDOUT: }
  48. // CHECK:STDOUT:
  49. // CHECK:STDOUT: imports {
  50. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  51. // CHECK:STDOUT: .Int = %import_ref.187
  52. // CHECK:STDOUT: .ImplicitAs = %import_ref.a69
  53. // CHECK:STDOUT: import Core//prelude
  54. // CHECK:STDOUT: import Core//prelude/...
  55. // CHECK:STDOUT: }
  56. // CHECK:STDOUT: }
  57. // CHECK:STDOUT:
  58. // CHECK:STDOUT: file {
  59. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  60. // CHECK:STDOUT: .Core = imports.%Core
  61. // CHECK:STDOUT: .F = %F.decl
  62. // CHECK:STDOUT: }
  63. // CHECK:STDOUT: %Core.import = import Core
  64. // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [template = constants.%F] {} {}
  65. // CHECK:STDOUT: }
  66. // CHECK:STDOUT:
  67. // CHECK:STDOUT: fn @F() {
  68. // CHECK:STDOUT: !entry:
  69. // CHECK:STDOUT: %s.var: ref %struct_type.a.b.3de = var s
  70. // CHECK:STDOUT: %s: ref %struct_type.a.b.3de = bind_name s, %s.var
  71. // CHECK:STDOUT: %int_1.loc12: Core.IntLiteral = int_value 1 [template = constants.%int_1.5b8]
  72. // CHECK:STDOUT: %int_2.loc12: Core.IntLiteral = int_value 2 [template = constants.%int_2.ecc]
  73. // CHECK:STDOUT: %.loc12_46.1: %struct_type.a.b.cfd = struct_literal (%int_1.loc12, %int_2.loc12)
  74. // CHECK:STDOUT: %impl.elem0.loc12_46.1: %Convert.type.cd1 = impl_witness_access constants.%impl_witness.5b0, element0 [template = constants.%Convert.925]
  75. // CHECK:STDOUT: %Convert.bound.loc12_46.1: <bound method> = bound_method %int_1.loc12, %impl.elem0.loc12_46.1 [template = constants.%Convert.bound.afd]
  76. // CHECK:STDOUT: %Convert.specific_fn.loc12_46.1: <specific function> = specific_function %Convert.bound.loc12_46.1, @Convert.2(constants.%int_32) [template = constants.%Convert.specific_fn.b73]
  77. // CHECK:STDOUT: %int.convert_checked.loc12_46.1: init %i32 = call %Convert.specific_fn.loc12_46.1(%int_1.loc12) [template = constants.%int_1.c60]
  78. // CHECK:STDOUT: %.loc12_46.2: init %i32 = converted %int_1.loc12, %int.convert_checked.loc12_46.1 [template = constants.%int_1.c60]
  79. // CHECK:STDOUT: %.loc12_46.3: ref %i32 = struct_access %s.var, element0
  80. // CHECK:STDOUT: %.loc12_46.4: init %i32 = initialize_from %.loc12_46.2 to %.loc12_46.3 [template = constants.%int_1.c60]
  81. // CHECK:STDOUT: %impl.elem0.loc12_46.2: %Convert.type.cd1 = impl_witness_access constants.%impl_witness.5b0, element0 [template = constants.%Convert.925]
  82. // CHECK:STDOUT: %Convert.bound.loc12_46.2: <bound method> = bound_method %int_2.loc12, %impl.elem0.loc12_46.2 [template = constants.%Convert.bound.f0b]
  83. // CHECK:STDOUT: %Convert.specific_fn.loc12_46.2: <specific function> = specific_function %Convert.bound.loc12_46.2, @Convert.2(constants.%int_32) [template = constants.%Convert.specific_fn.20e]
  84. // CHECK:STDOUT: %int.convert_checked.loc12_46.2: init %i32 = call %Convert.specific_fn.loc12_46.2(%int_2.loc12) [template = constants.%int_2.166]
  85. // CHECK:STDOUT: %.loc12_46.5: init %i32 = converted %int_2.loc12, %int.convert_checked.loc12_46.2 [template = constants.%int_2.166]
  86. // CHECK:STDOUT: %.loc12_46.6: ref %i32 = struct_access %s.var, element1
  87. // CHECK:STDOUT: %.loc12_46.7: init %i32 = initialize_from %.loc12_46.5 to %.loc12_46.6 [template = constants.%int_2.166]
  88. // CHECK:STDOUT: %.loc12_46.8: init %struct_type.a.b.3de = struct_init (%.loc12_46.4, %.loc12_46.7) to %s.var [template = constants.%struct]
  89. // CHECK:STDOUT: %.loc12_47: init %struct_type.a.b.3de = converted %.loc12_46.1, %.loc12_46.8 [template = constants.%struct]
  90. // CHECK:STDOUT: assign %s.var, %.loc12_47
  91. // CHECK:STDOUT: %p.var: ref %ptr.570 = var p
  92. // CHECK:STDOUT: %p: ref %ptr.570 = bind_name p, %p.var
  93. // CHECK:STDOUT: %s.ref.loc14: ref %struct_type.a.b.3de = name_ref s, %s
  94. // CHECK:STDOUT: %addr.loc14: %ptr.570 = addr_of %s.ref.loc14
  95. // CHECK:STDOUT: assign %p.var, %addr.loc14
  96. // CHECK:STDOUT: %q.var: ref %ptr.dc3 = var q
  97. // CHECK:STDOUT: %q: ref %ptr.dc3 = bind_name q, %q.var
  98. // CHECK:STDOUT: %s.ref.loc15: ref %struct_type.a.b.3de = name_ref s, %s
  99. // CHECK:STDOUT: %.loc15: ref %i32 = struct_access %s.ref.loc15, element0
  100. // CHECK:STDOUT: %addr.loc15: %ptr.dc3 = addr_of %.loc15
  101. // CHECK:STDOUT: assign %q.var, %addr.loc15
  102. // CHECK:STDOUT: %r.var: ref %ptr.dc3 = var r
  103. // CHECK:STDOUT: %r: ref %ptr.dc3 = bind_name r, %r.var
  104. // CHECK:STDOUT: %s.ref.loc16: ref %struct_type.a.b.3de = name_ref s, %s
  105. // CHECK:STDOUT: %.loc16: ref %i32 = struct_access %s.ref.loc16, element1
  106. // CHECK:STDOUT: %addr.loc16: %ptr.dc3 = addr_of %.loc16
  107. // CHECK:STDOUT: assign %r.var, %addr.loc16
  108. // CHECK:STDOUT: %t.var: ref %tuple.type.471 = var t
  109. // CHECK:STDOUT: %t: ref %tuple.type.471 = bind_name t, %t.var
  110. // CHECK:STDOUT: %int_1.loc18: Core.IntLiteral = int_value 1 [template = constants.%int_1.5b8]
  111. // CHECK:STDOUT: %int_2.loc18: Core.IntLiteral = int_value 2 [template = constants.%int_2.ecc]
  112. // CHECK:STDOUT: %.loc18_28.1: %tuple.type.f94 = tuple_literal (%int_1.loc18, %int_2.loc18)
  113. // CHECK:STDOUT: %impl.elem0.loc18_28.1: %Convert.type.cd1 = impl_witness_access constants.%impl_witness.5b0, element0 [template = constants.%Convert.925]
  114. // CHECK:STDOUT: %Convert.bound.loc18_28.1: <bound method> = bound_method %int_1.loc18, %impl.elem0.loc18_28.1 [template = constants.%Convert.bound.afd]
  115. // CHECK:STDOUT: %Convert.specific_fn.loc18_28.1: <specific function> = specific_function %Convert.bound.loc18_28.1, @Convert.2(constants.%int_32) [template = constants.%Convert.specific_fn.b73]
  116. // CHECK:STDOUT: %int.convert_checked.loc18_28.1: init %i32 = call %Convert.specific_fn.loc18_28.1(%int_1.loc18) [template = constants.%int_1.c60]
  117. // CHECK:STDOUT: %.loc18_28.2: init %i32 = converted %int_1.loc18, %int.convert_checked.loc18_28.1 [template = constants.%int_1.c60]
  118. // CHECK:STDOUT: %tuple.elem0.loc18: ref %i32 = tuple_access %t.var, element0
  119. // CHECK:STDOUT: %.loc18_28.3: init %i32 = initialize_from %.loc18_28.2 to %tuple.elem0.loc18 [template = constants.%int_1.c60]
  120. // CHECK:STDOUT: %impl.elem0.loc18_28.2: %Convert.type.cd1 = impl_witness_access constants.%impl_witness.5b0, element0 [template = constants.%Convert.925]
  121. // CHECK:STDOUT: %Convert.bound.loc18_28.2: <bound method> = bound_method %int_2.loc18, %impl.elem0.loc18_28.2 [template = constants.%Convert.bound.f0b]
  122. // CHECK:STDOUT: %Convert.specific_fn.loc18_28.2: <specific function> = specific_function %Convert.bound.loc18_28.2, @Convert.2(constants.%int_32) [template = constants.%Convert.specific_fn.20e]
  123. // CHECK:STDOUT: %int.convert_checked.loc18_28.2: init %i32 = call %Convert.specific_fn.loc18_28.2(%int_2.loc18) [template = constants.%int_2.166]
  124. // CHECK:STDOUT: %.loc18_28.4: init %i32 = converted %int_2.loc18, %int.convert_checked.loc18_28.2 [template = constants.%int_2.166]
  125. // CHECK:STDOUT: %tuple.elem1.loc18: ref %i32 = tuple_access %t.var, element1
  126. // CHECK:STDOUT: %.loc18_28.5: init %i32 = initialize_from %.loc18_28.4 to %tuple.elem1.loc18 [template = constants.%int_2.166]
  127. // CHECK:STDOUT: %.loc18_28.6: init %tuple.type.471 = tuple_init (%.loc18_28.3, %.loc18_28.5) to %t.var [template = constants.%tuple]
  128. // CHECK:STDOUT: %.loc18_29: init %tuple.type.471 = converted %.loc18_28.1, %.loc18_28.6 [template = constants.%tuple]
  129. // CHECK:STDOUT: assign %t.var, %.loc18_29
  130. // CHECK:STDOUT: %t0.var: ref %ptr.dc3 = var t0
  131. // CHECK:STDOUT: %t0: ref %ptr.dc3 = bind_name t0, %t0.var
  132. // CHECK:STDOUT: %t.ref.loc19: ref %tuple.type.471 = name_ref t, %t
  133. // CHECK:STDOUT: %int_0: Core.IntLiteral = int_value 0 [template = constants.%int_0]
  134. // CHECK:STDOUT: %tuple.elem0.loc19: ref %i32 = tuple_access %t.ref.loc19, element0
  135. // CHECK:STDOUT: %addr.loc19: %ptr.dc3 = addr_of %tuple.elem0.loc19
  136. // CHECK:STDOUT: assign %t0.var, %addr.loc19
  137. // CHECK:STDOUT: %t1.var: ref %ptr.dc3 = var t1
  138. // CHECK:STDOUT: %t1: ref %ptr.dc3 = bind_name t1, %t1.var
  139. // CHECK:STDOUT: %t.ref.loc20: ref %tuple.type.471 = name_ref t, %t
  140. // CHECK:STDOUT: %int_1.loc20: Core.IntLiteral = int_value 1 [template = constants.%int_1.5b8]
  141. // CHECK:STDOUT: %tuple.elem1.loc20: ref %i32 = tuple_access %t.ref.loc20, element1
  142. // CHECK:STDOUT: %addr.loc20: %ptr.dc3 = addr_of %tuple.elem1.loc20
  143. // CHECK:STDOUT: assign %t1.var, %addr.loc20
  144. // CHECK:STDOUT: return
  145. // CHECK:STDOUT: }
  146. // CHECK:STDOUT: