assignment.carbon 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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/operators/builtin/assignment.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/operators/builtin/assignment.carbon
  10. fn Main() {
  11. var a: i32 = 12;
  12. a = 9;
  13. var b: (i32, i32) = (1, 2);
  14. b[0] = 3;
  15. b[1] = 4;
  16. var c: {.a: i32, .b: i32} = {.a = 1, .b = 2};
  17. c.a = 3;
  18. c.b = 4;
  19. var p: i32* = &a;
  20. *p = 5;
  21. *(if true then p else &a) = 10;
  22. }
  23. // CHECK:STDOUT: --- assignment.carbon
  24. // CHECK:STDOUT:
  25. // CHECK:STDOUT: constants {
  26. // CHECK:STDOUT: %Main.type: type = fn_type @Main [template]
  27. // CHECK:STDOUT: %.1: type = tuple_type () [template]
  28. // CHECK:STDOUT: %Main: %Main.type = struct_value () [template]
  29. // CHECK:STDOUT: %Int32.type: type = fn_type @Int32 [template]
  30. // CHECK:STDOUT: %Int32: %Int32.type = struct_value () [template]
  31. // CHECK:STDOUT: %.2: i32 = int_literal 12 [template]
  32. // CHECK:STDOUT: %.3: i32 = int_literal 9 [template]
  33. // CHECK:STDOUT: %.4: type = tuple_type (type, type) [template]
  34. // CHECK:STDOUT: %.5: type = tuple_type (i32, i32) [template]
  35. // CHECK:STDOUT: %.6: type = ptr_type %.5 [template]
  36. // CHECK:STDOUT: %.7: i32 = int_literal 1 [template]
  37. // CHECK:STDOUT: %.8: i32 = int_literal 2 [template]
  38. // CHECK:STDOUT: %tuple: %.5 = tuple_value (%.7, %.8) [template]
  39. // CHECK:STDOUT: %.9: i32 = int_literal 0 [template]
  40. // CHECK:STDOUT: %.10: i32 = int_literal 3 [template]
  41. // CHECK:STDOUT: %.11: i32 = int_literal 4 [template]
  42. // CHECK:STDOUT: %.12: type = struct_type {.a: i32, .b: i32} [template]
  43. // CHECK:STDOUT: %.13: type = ptr_type %.12 [template]
  44. // CHECK:STDOUT: %struct: %.12 = struct_value (%.7, %.8) [template]
  45. // CHECK:STDOUT: %.14: type = ptr_type i32 [template]
  46. // CHECK:STDOUT: %.15: i32 = int_literal 5 [template]
  47. // CHECK:STDOUT: %.16: bool = bool_literal true [template]
  48. // CHECK:STDOUT: %.17: i32 = int_literal 10 [template]
  49. // CHECK:STDOUT: }
  50. // CHECK:STDOUT:
  51. // CHECK:STDOUT: file {
  52. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  53. // CHECK:STDOUT: .Core = %Core
  54. // CHECK:STDOUT: .Main = %Main.decl
  55. // CHECK:STDOUT: }
  56. // CHECK:STDOUT: %Core: <namespace> = namespace [template] {}
  57. // CHECK:STDOUT: %Main.decl: %Main.type = fn_decl @Main [template = constants.%Main] {}
  58. // CHECK:STDOUT: %import_ref.1: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  59. // CHECK:STDOUT: %import_ref.2: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  60. // CHECK:STDOUT: %import_ref.3: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  61. // CHECK:STDOUT: %import_ref.4: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  62. // CHECK:STDOUT: %import_ref.5: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  63. // CHECK:STDOUT: %import_ref.6: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  64. // CHECK:STDOUT: }
  65. // CHECK:STDOUT:
  66. // CHECK:STDOUT: fn @Main() {
  67. // CHECK:STDOUT: !entry:
  68. // CHECK:STDOUT: %int.make_type_32.loc12: init type = call constants.%Int32() [template = i32]
  69. // CHECK:STDOUT: %.loc12_10.1: type = value_of_initializer %int.make_type_32.loc12 [template = i32]
  70. // CHECK:STDOUT: %.loc12_10.2: type = converted %int.make_type_32.loc12, %.loc12_10.1 [template = i32]
  71. // CHECK:STDOUT: %a.var: ref i32 = var a
  72. // CHECK:STDOUT: %a: ref i32 = bind_name a, %a.var
  73. // CHECK:STDOUT: %.loc12_16: i32 = int_literal 12 [template = constants.%.2]
  74. // CHECK:STDOUT: assign %a.var, %.loc12_16
  75. // CHECK:STDOUT: %a.ref.loc13: ref i32 = name_ref a, %a
  76. // CHECK:STDOUT: %.loc13: i32 = int_literal 9 [template = constants.%.3]
  77. // CHECK:STDOUT: assign %a.ref.loc13, %.loc13
  78. // CHECK:STDOUT: %int.make_type_32.loc15_11: init type = call constants.%Int32() [template = i32]
  79. // CHECK:STDOUT: %int.make_type_32.loc15_16: init type = call constants.%Int32() [template = i32]
  80. // CHECK:STDOUT: %.loc15_19.1: %.4 = tuple_literal (%int.make_type_32.loc15_11, %int.make_type_32.loc15_16)
  81. // CHECK:STDOUT: %.loc15_19.2: type = value_of_initializer %int.make_type_32.loc15_11 [template = i32]
  82. // CHECK:STDOUT: %.loc15_19.3: type = converted %int.make_type_32.loc15_11, %.loc15_19.2 [template = i32]
  83. // CHECK:STDOUT: %.loc15_19.4: type = value_of_initializer %int.make_type_32.loc15_16 [template = i32]
  84. // CHECK:STDOUT: %.loc15_19.5: type = converted %int.make_type_32.loc15_16, %.loc15_19.4 [template = i32]
  85. // CHECK:STDOUT: %.loc15_19.6: type = converted %.loc15_19.1, constants.%.5 [template = constants.%.5]
  86. // CHECK:STDOUT: %b.var: ref %.5 = var b
  87. // CHECK:STDOUT: %b: ref %.5 = bind_name b, %b.var
  88. // CHECK:STDOUT: %.loc15_24: i32 = int_literal 1 [template = constants.%.7]
  89. // CHECK:STDOUT: %.loc15_27: i32 = int_literal 2 [template = constants.%.8]
  90. // CHECK:STDOUT: %.loc15_28.1: %.5 = tuple_literal (%.loc15_24, %.loc15_27)
  91. // CHECK:STDOUT: %.loc15_28.2: ref i32 = tuple_access %b.var, element0
  92. // CHECK:STDOUT: %.loc15_28.3: init i32 = initialize_from %.loc15_24 to %.loc15_28.2 [template = constants.%.7]
  93. // CHECK:STDOUT: %.loc15_28.4: ref i32 = tuple_access %b.var, element1
  94. // CHECK:STDOUT: %.loc15_28.5: init i32 = initialize_from %.loc15_27 to %.loc15_28.4 [template = constants.%.8]
  95. // CHECK:STDOUT: %.loc15_28.6: init %.5 = tuple_init (%.loc15_28.3, %.loc15_28.5) to %b.var [template = constants.%tuple]
  96. // CHECK:STDOUT: %.loc15_29: init %.5 = converted %.loc15_28.1, %.loc15_28.6 [template = constants.%tuple]
  97. // CHECK:STDOUT: assign %b.var, %.loc15_29
  98. // CHECK:STDOUT: %b.ref.loc16: ref %.5 = name_ref b, %b
  99. // CHECK:STDOUT: %.loc16_5: i32 = int_literal 0 [template = constants.%.9]
  100. // CHECK:STDOUT: %.loc16_6: ref i32 = tuple_index %b.ref.loc16, %.loc16_5
  101. // CHECK:STDOUT: %.loc16_10: i32 = int_literal 3 [template = constants.%.10]
  102. // CHECK:STDOUT: assign %.loc16_6, %.loc16_10
  103. // CHECK:STDOUT: %b.ref.loc17: ref %.5 = name_ref b, %b
  104. // CHECK:STDOUT: %.loc17_5: i32 = int_literal 1 [template = constants.%.7]
  105. // CHECK:STDOUT: %.loc17_6: ref i32 = tuple_index %b.ref.loc17, %.loc17_5
  106. // CHECK:STDOUT: %.loc17_10: i32 = int_literal 4 [template = constants.%.11]
  107. // CHECK:STDOUT: assign %.loc17_6, %.loc17_10
  108. // CHECK:STDOUT: %int.make_type_32.loc19_15: init type = call constants.%Int32() [template = i32]
  109. // CHECK:STDOUT: %.loc19_15.1: type = value_of_initializer %int.make_type_32.loc19_15 [template = i32]
  110. // CHECK:STDOUT: %.loc19_15.2: type = converted %int.make_type_32.loc19_15, %.loc19_15.1 [template = i32]
  111. // CHECK:STDOUT: %int.make_type_32.loc19_24: init type = call constants.%Int32() [template = i32]
  112. // CHECK:STDOUT: %.loc19_24.1: type = value_of_initializer %int.make_type_32.loc19_24 [template = i32]
  113. // CHECK:STDOUT: %.loc19_24.2: type = converted %int.make_type_32.loc19_24, %.loc19_24.1 [template = i32]
  114. // CHECK:STDOUT: %.loc19_27: type = struct_type {.a: i32, .b: i32} [template = constants.%.12]
  115. // CHECK:STDOUT: %c.var: ref %.12 = var c
  116. // CHECK:STDOUT: %c: ref %.12 = bind_name c, %c.var
  117. // CHECK:STDOUT: %.loc19_37: i32 = int_literal 1 [template = constants.%.7]
  118. // CHECK:STDOUT: %.loc19_45: i32 = int_literal 2 [template = constants.%.8]
  119. // CHECK:STDOUT: %.loc19_46.1: %.12 = struct_literal (%.loc19_37, %.loc19_45)
  120. // CHECK:STDOUT: %.loc19_46.2: ref i32 = struct_access %c.var, element0
  121. // CHECK:STDOUT: %.loc19_46.3: init i32 = initialize_from %.loc19_37 to %.loc19_46.2 [template = constants.%.7]
  122. // CHECK:STDOUT: %.loc19_46.4: ref i32 = struct_access %c.var, element1
  123. // CHECK:STDOUT: %.loc19_46.5: init i32 = initialize_from %.loc19_45 to %.loc19_46.4 [template = constants.%.8]
  124. // CHECK:STDOUT: %.loc19_46.6: init %.12 = struct_init (%.loc19_46.3, %.loc19_46.5) to %c.var [template = constants.%struct]
  125. // CHECK:STDOUT: %.loc19_47: init %.12 = converted %.loc19_46.1, %.loc19_46.6 [template = constants.%struct]
  126. // CHECK:STDOUT: assign %c.var, %.loc19_47
  127. // CHECK:STDOUT: %c.ref.loc20: ref %.12 = name_ref c, %c
  128. // CHECK:STDOUT: %.loc20_4: ref i32 = struct_access %c.ref.loc20, element0
  129. // CHECK:STDOUT: %.loc20_9: i32 = int_literal 3 [template = constants.%.10]
  130. // CHECK:STDOUT: assign %.loc20_4, %.loc20_9
  131. // CHECK:STDOUT: %c.ref.loc21: ref %.12 = name_ref c, %c
  132. // CHECK:STDOUT: %.loc21_4: ref i32 = struct_access %c.ref.loc21, element1
  133. // CHECK:STDOUT: %.loc21_9: i32 = int_literal 4 [template = constants.%.11]
  134. // CHECK:STDOUT: assign %.loc21_4, %.loc21_9
  135. // CHECK:STDOUT: %int.make_type_32.loc23: init type = call constants.%Int32() [template = i32]
  136. // CHECK:STDOUT: %.loc23_13.1: type = value_of_initializer %int.make_type_32.loc23 [template = i32]
  137. // CHECK:STDOUT: %.loc23_13.2: type = converted %int.make_type_32.loc23, %.loc23_13.1 [template = i32]
  138. // CHECK:STDOUT: %.loc23_13.3: type = ptr_type i32 [template = constants.%.14]
  139. // CHECK:STDOUT: %p.var: ref %.14 = var p
  140. // CHECK:STDOUT: %p: ref %.14 = bind_name p, %p.var
  141. // CHECK:STDOUT: %a.ref.loc23: ref i32 = name_ref a, %a
  142. // CHECK:STDOUT: %.loc23_17: %.14 = addr_of %a.ref.loc23
  143. // CHECK:STDOUT: assign %p.var, %.loc23_17
  144. // CHECK:STDOUT: %p.ref.loc24: ref %.14 = name_ref p, %p
  145. // CHECK:STDOUT: %.loc24_4: %.14 = bind_value %p.ref.loc24
  146. // CHECK:STDOUT: %.loc24_3: ref i32 = deref %.loc24_4
  147. // CHECK:STDOUT: %.loc24_8: i32 = int_literal 5 [template = constants.%.15]
  148. // CHECK:STDOUT: assign %.loc24_3, %.loc24_8
  149. // CHECK:STDOUT: %.loc26_8: bool = bool_literal true [template = constants.%.16]
  150. // CHECK:STDOUT: if %.loc26_8 br !if.expr.then else br !if.expr.else
  151. // CHECK:STDOUT:
  152. // CHECK:STDOUT: !if.expr.then:
  153. // CHECK:STDOUT: %p.ref.loc26: ref %.14 = name_ref p, %p
  154. // CHECK:STDOUT: %.loc26_18: %.14 = bind_value %p.ref.loc26
  155. // CHECK:STDOUT: br !if.expr.result(%.loc26_18)
  156. // CHECK:STDOUT:
  157. // CHECK:STDOUT: !if.expr.else:
  158. // CHECK:STDOUT: %a.ref.loc26: ref i32 = name_ref a, %a
  159. // CHECK:STDOUT: %.loc26_25: %.14 = addr_of %a.ref.loc26
  160. // CHECK:STDOUT: br !if.expr.result(%.loc26_25)
  161. // CHECK:STDOUT:
  162. // CHECK:STDOUT: !if.expr.result:
  163. // CHECK:STDOUT: %.loc26_5: %.14 = block_arg !if.expr.result
  164. // CHECK:STDOUT: %.loc26_3: ref i32 = deref %.loc26_5
  165. // CHECK:STDOUT: %.loc26_31: i32 = int_literal 10 [template = constants.%.17]
  166. // CHECK:STDOUT: assign %.loc26_3, %.loc26_31
  167. // CHECK:STDOUT: return
  168. // CHECK:STDOUT: }
  169. // CHECK:STDOUT:
  170. // CHECK:STDOUT: fn @Int32() -> type = "int.make_type_32";
  171. // CHECK:STDOUT: