aggregate.carbon 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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/eval/aggregate.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/eval/aggregate.carbon
  10. var tuple_copy: (i32, i32) = (1, 2) as (i32, i32);
  11. var struct_copy: {.a: i32, .b: i32, .c: i32} = {.c = 3, .b = 2, .a = 1} as {.b: i32, .a: i32, .c: i32};
  12. var tuple_index: [i32; 1] = (0,) as [i32; (5, 7, 1, 9)[2]];
  13. var struct_access: [i32; 1] = (0,) as [i32; {.a = 3, .b = 1}.b];
  14. // CHECK:STDOUT: --- aggregate.carbon
  15. // CHECK:STDOUT:
  16. // CHECK:STDOUT: constants {
  17. // CHECK:STDOUT: %Int32.type: type = fn_type @Int32 [template]
  18. // CHECK:STDOUT: %.1: type = tuple_type () [template]
  19. // CHECK:STDOUT: %Int32: %Int32.type = struct_value () [template]
  20. // CHECK:STDOUT: %.2: type = tuple_type (type, type) [template]
  21. // CHECK:STDOUT: %.3: type = tuple_type (i32, i32) [template]
  22. // CHECK:STDOUT: %.4: type = ptr_type %.3 [template]
  23. // CHECK:STDOUT: %.5: i32 = int_literal 1 [template]
  24. // CHECK:STDOUT: %.6: i32 = int_literal 2 [template]
  25. // CHECK:STDOUT: %tuple.1: %.3 = tuple_value (%.5, %.6) [template]
  26. // CHECK:STDOUT: %.7: type = struct_type {.a: i32, .b: i32, .c: i32} [template]
  27. // CHECK:STDOUT: %.8: type = ptr_type %.7 [template]
  28. // CHECK:STDOUT: %.9: i32 = int_literal 3 [template]
  29. // CHECK:STDOUT: %.10: type = struct_type {.c: i32, .b: i32, .a: i32} [template]
  30. // CHECK:STDOUT: %.11: type = struct_type {.b: i32, .a: i32, .c: i32} [template]
  31. // CHECK:STDOUT: %.12: type = ptr_type %.11 [template]
  32. // CHECK:STDOUT: %struct.1: %.11 = struct_value (%.6, %.5, %.9) [template]
  33. // CHECK:STDOUT: %struct.2: %.7 = struct_value (%.5, %.6, %.9) [template]
  34. // CHECK:STDOUT: %.13: type = array_type %.5, i32 [template]
  35. // CHECK:STDOUT: %.14: type = ptr_type %.13 [template]
  36. // CHECK:STDOUT: %.15: i32 = int_literal 0 [template]
  37. // CHECK:STDOUT: %.16: type = tuple_type (i32) [template]
  38. // CHECK:STDOUT: %.17: i32 = int_literal 5 [template]
  39. // CHECK:STDOUT: %.18: i32 = int_literal 7 [template]
  40. // CHECK:STDOUT: %.19: i32 = int_literal 9 [template]
  41. // CHECK:STDOUT: %.20: type = tuple_type (i32, i32, i32, i32) [template]
  42. // CHECK:STDOUT: %.21: type = ptr_type %.20 [template]
  43. // CHECK:STDOUT: %tuple.2: %.20 = tuple_value (%.17, %.18, %.5, %.19) [template]
  44. // CHECK:STDOUT: %array: %.13 = tuple_value (%.15) [template]
  45. // CHECK:STDOUT: %.22: type = struct_type {.a: i32, .b: i32} [template]
  46. // CHECK:STDOUT: %.23: type = ptr_type %.22 [template]
  47. // CHECK:STDOUT: %struct.3: %.22 = struct_value (%.9, %.5) [template]
  48. // CHECK:STDOUT: }
  49. // CHECK:STDOUT:
  50. // CHECK:STDOUT: file {
  51. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  52. // CHECK:STDOUT: .Core = %Core
  53. // CHECK:STDOUT: .tuple_copy = %tuple_copy
  54. // CHECK:STDOUT: .struct_copy = %struct_copy
  55. // CHECK:STDOUT: .tuple_index = %tuple_index
  56. // CHECK:STDOUT: .struct_access = %struct_access
  57. // CHECK:STDOUT: }
  58. // CHECK:STDOUT: %Core: <namespace> = namespace [template] {}
  59. // CHECK:STDOUT: %import_ref.1: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  60. // CHECK:STDOUT: %int.make_type_32.loc11_18: init type = call constants.%Int32() [template = i32]
  61. // CHECK:STDOUT: %import_ref.2: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  62. // CHECK:STDOUT: %int.make_type_32.loc11_23: init type = call constants.%Int32() [template = i32]
  63. // CHECK:STDOUT: %.loc11_26.1: %.2 = tuple_literal (%int.make_type_32.loc11_18, %int.make_type_32.loc11_23)
  64. // CHECK:STDOUT: %.loc11_26.2: type = value_of_initializer %int.make_type_32.loc11_18 [template = i32]
  65. // CHECK:STDOUT: %.loc11_26.3: type = converted %int.make_type_32.loc11_18, %.loc11_26.2 [template = i32]
  66. // CHECK:STDOUT: %.loc11_26.4: type = value_of_initializer %int.make_type_32.loc11_23 [template = i32]
  67. // CHECK:STDOUT: %.loc11_26.5: type = converted %int.make_type_32.loc11_23, %.loc11_26.4 [template = i32]
  68. // CHECK:STDOUT: %.loc11_26.6: type = converted %.loc11_26.1, constants.%.3 [template = constants.%.3]
  69. // CHECK:STDOUT: %tuple_copy.var: ref %.3 = var tuple_copy
  70. // CHECK:STDOUT: %tuple_copy: ref %.3 = bind_name tuple_copy, %tuple_copy.var
  71. // CHECK:STDOUT: %import_ref.3: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  72. // CHECK:STDOUT: %import_ref.4: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  73. // CHECK:STDOUT: %import_ref.5: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  74. // CHECK:STDOUT: %int.make_type_32.loc13_23: init type = call constants.%Int32() [template = i32]
  75. // CHECK:STDOUT: %.loc13_23.1: type = value_of_initializer %int.make_type_32.loc13_23 [template = i32]
  76. // CHECK:STDOUT: %.loc13_23.2: type = converted %int.make_type_32.loc13_23, %.loc13_23.1 [template = i32]
  77. // CHECK:STDOUT: %import_ref.6: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  78. // CHECK:STDOUT: %int.make_type_32.loc13_32: init type = call constants.%Int32() [template = i32]
  79. // CHECK:STDOUT: %.loc13_32.1: type = value_of_initializer %int.make_type_32.loc13_32 [template = i32]
  80. // CHECK:STDOUT: %.loc13_32.2: type = converted %int.make_type_32.loc13_32, %.loc13_32.1 [template = i32]
  81. // CHECK:STDOUT: %import_ref.7: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  82. // CHECK:STDOUT: %int.make_type_32.loc13_41: init type = call constants.%Int32() [template = i32]
  83. // CHECK:STDOUT: %.loc13_41.1: type = value_of_initializer %int.make_type_32.loc13_41 [template = i32]
  84. // CHECK:STDOUT: %.loc13_41.2: type = converted %int.make_type_32.loc13_41, %.loc13_41.1 [template = i32]
  85. // CHECK:STDOUT: %.loc13_44: type = struct_type {.a: i32, .b: i32, .c: i32} [template = constants.%.7]
  86. // CHECK:STDOUT: %struct_copy.var: ref %.7 = var struct_copy
  87. // CHECK:STDOUT: %struct_copy: ref %.7 = bind_name struct_copy, %struct_copy.var
  88. // CHECK:STDOUT: %import_ref.8: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  89. // CHECK:STDOUT: %import_ref.9: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  90. // CHECK:STDOUT: %import_ref.10: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  91. // CHECK:STDOUT: %import_ref.11: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  92. // CHECK:STDOUT: %int.make_type_32.loc15: init type = call constants.%Int32() [template = i32]
  93. // CHECK:STDOUT: %.loc15_24: i32 = int_literal 1 [template = constants.%.5]
  94. // CHECK:STDOUT: %.loc15_19.1: type = value_of_initializer %int.make_type_32.loc15 [template = i32]
  95. // CHECK:STDOUT: %.loc15_19.2: type = converted %int.make_type_32.loc15, %.loc15_19.1 [template = i32]
  96. // CHECK:STDOUT: %.loc15_25: type = array_type %.loc15_24, i32 [template = constants.%.13]
  97. // CHECK:STDOUT: %tuple_index.var: ref %.13 = var tuple_index
  98. // CHECK:STDOUT: %tuple_index: ref %.13 = bind_name tuple_index, %tuple_index.var
  99. // CHECK:STDOUT: %import_ref.12: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  100. // CHECK:STDOUT: %import_ref.13: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  101. // CHECK:STDOUT: %int.make_type_32.loc17: init type = call constants.%Int32() [template = i32]
  102. // CHECK:STDOUT: %.loc17_26: i32 = int_literal 1 [template = constants.%.5]
  103. // CHECK:STDOUT: %.loc17_21.1: type = value_of_initializer %int.make_type_32.loc17 [template = i32]
  104. // CHECK:STDOUT: %.loc17_21.2: type = converted %int.make_type_32.loc17, %.loc17_21.1 [template = i32]
  105. // CHECK:STDOUT: %.loc17_27: type = array_type %.loc17_26, i32 [template = constants.%.13]
  106. // CHECK:STDOUT: %struct_access.var: ref %.13 = var struct_access
  107. // CHECK:STDOUT: %struct_access: ref %.13 = bind_name struct_access, %struct_access.var
  108. // CHECK:STDOUT: %import_ref.14: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  109. // CHECK:STDOUT: }
  110. // CHECK:STDOUT:
  111. // CHECK:STDOUT: fn @Int32() -> type = "int.make_type_32";
  112. // CHECK:STDOUT:
  113. // CHECK:STDOUT: fn @__global_init() {
  114. // CHECK:STDOUT: !entry:
  115. // CHECK:STDOUT: %.loc11_31: i32 = int_literal 1 [template = constants.%.5]
  116. // CHECK:STDOUT: %.loc11_34: i32 = int_literal 2 [template = constants.%.6]
  117. // CHECK:STDOUT: %.loc11_35.1: %.3 = tuple_literal (%.loc11_31, %.loc11_34)
  118. // CHECK:STDOUT: %int.make_type_32.loc11_41: init type = call constants.%Int32() [template = i32]
  119. // CHECK:STDOUT: %int.make_type_32.loc11_46: init type = call constants.%Int32() [template = i32]
  120. // CHECK:STDOUT: %.loc11_49.1: %.2 = tuple_literal (%int.make_type_32.loc11_41, %int.make_type_32.loc11_46)
  121. // CHECK:STDOUT: %.loc11_49.2: type = value_of_initializer %int.make_type_32.loc11_41 [template = i32]
  122. // CHECK:STDOUT: %.loc11_49.3: type = converted %int.make_type_32.loc11_41, %.loc11_49.2 [template = i32]
  123. // CHECK:STDOUT: %.loc11_49.4: type = value_of_initializer %int.make_type_32.loc11_46 [template = i32]
  124. // CHECK:STDOUT: %.loc11_49.5: type = converted %int.make_type_32.loc11_46, %.loc11_49.4 [template = i32]
  125. // CHECK:STDOUT: %.loc11_49.6: type = converted %.loc11_49.1, constants.%.3 [template = constants.%.3]
  126. // CHECK:STDOUT: %.loc11_35.2: ref i32 = tuple_access file.%tuple_copy.var, element0
  127. // CHECK:STDOUT: %.loc11_35.3: init i32 = initialize_from %.loc11_31 to %.loc11_35.2 [template = constants.%.5]
  128. // CHECK:STDOUT: %.loc11_35.4: ref i32 = tuple_access file.%tuple_copy.var, element1
  129. // CHECK:STDOUT: %.loc11_35.5: init i32 = initialize_from %.loc11_34 to %.loc11_35.4 [template = constants.%.6]
  130. // CHECK:STDOUT: %.loc11_35.6: init %.3 = tuple_init (%.loc11_35.3, %.loc11_35.5) to file.%tuple_copy.var [template = constants.%tuple.1]
  131. // CHECK:STDOUT: %.loc11_50: init %.3 = converted %.loc11_35.1, %.loc11_35.6 [template = constants.%tuple.1]
  132. // CHECK:STDOUT: assign file.%tuple_copy.var, %.loc11_50
  133. // CHECK:STDOUT: %.loc13_54: i32 = int_literal 3 [template = constants.%.9]
  134. // CHECK:STDOUT: %.loc13_62: i32 = int_literal 2 [template = constants.%.6]
  135. // CHECK:STDOUT: %.loc13_70: i32 = int_literal 1 [template = constants.%.5]
  136. // CHECK:STDOUT: %.loc13_71: %.10 = struct_literal (%.loc13_54, %.loc13_62, %.loc13_70)
  137. // CHECK:STDOUT: %int.make_type_32.loc13_81: init type = call constants.%Int32() [template = i32]
  138. // CHECK:STDOUT: %.loc13_81.1: type = value_of_initializer %int.make_type_32.loc13_81 [template = i32]
  139. // CHECK:STDOUT: %.loc13_81.2: type = converted %int.make_type_32.loc13_81, %.loc13_81.1 [template = i32]
  140. // CHECK:STDOUT: %int.make_type_32.loc13_90: init type = call constants.%Int32() [template = i32]
  141. // CHECK:STDOUT: %.loc13_90.1: type = value_of_initializer %int.make_type_32.loc13_90 [template = i32]
  142. // CHECK:STDOUT: %.loc13_90.2: type = converted %int.make_type_32.loc13_90, %.loc13_90.1 [template = i32]
  143. // CHECK:STDOUT: %int.make_type_32.loc13_99: init type = call constants.%Int32() [template = i32]
  144. // CHECK:STDOUT: %.loc13_99.1: type = value_of_initializer %int.make_type_32.loc13_99 [template = i32]
  145. // CHECK:STDOUT: %.loc13_99.2: type = converted %int.make_type_32.loc13_99, %.loc13_99.1 [template = i32]
  146. // CHECK:STDOUT: %.loc13_102: type = struct_type {.b: i32, .a: i32, .c: i32} [template = constants.%.11]
  147. // CHECK:STDOUT: %struct.loc13: %.11 = struct_value (%.loc13_62, %.loc13_70, %.loc13_54) [template = constants.%struct.1]
  148. // CHECK:STDOUT: %.loc13_73.1: %.11 = converted %.loc13_71, %struct.loc13 [template = constants.%struct.1]
  149. // CHECK:STDOUT: %.loc13_73.2: i32 = struct_access %.loc13_73.1, element1 [template = constants.%.5]
  150. // CHECK:STDOUT: %.loc13_73.3: ref i32 = struct_access file.%struct_copy.var, element1
  151. // CHECK:STDOUT: %.loc13_73.4: init i32 = initialize_from %.loc13_73.2 to %.loc13_73.3 [template = constants.%.5]
  152. // CHECK:STDOUT: %.loc13_73.5: i32 = struct_access %.loc13_73.1, element0 [template = constants.%.6]
  153. // CHECK:STDOUT: %.loc13_73.6: ref i32 = struct_access file.%struct_copy.var, element0
  154. // CHECK:STDOUT: %.loc13_73.7: init i32 = initialize_from %.loc13_73.5 to %.loc13_73.6 [template = constants.%.6]
  155. // CHECK:STDOUT: %.loc13_73.8: i32 = struct_access %.loc13_73.1, element2 [template = constants.%.9]
  156. // CHECK:STDOUT: %.loc13_73.9: ref i32 = struct_access file.%struct_copy.var, element2
  157. // CHECK:STDOUT: %.loc13_73.10: init i32 = initialize_from %.loc13_73.8 to %.loc13_73.9 [template = constants.%.9]
  158. // CHECK:STDOUT: %.loc13_73.11: init %.7 = struct_init (%.loc13_73.4, %.loc13_73.7, %.loc13_73.10) to file.%struct_copy.var [template = constants.%struct.2]
  159. // CHECK:STDOUT: %.loc13_103: init %.7 = converted %.loc13_73.1, %.loc13_73.11 [template = constants.%struct.2]
  160. // CHECK:STDOUT: assign file.%struct_copy.var, %.loc13_103
  161. // CHECK:STDOUT: %.loc15_30: i32 = int_literal 0 [template = constants.%.15]
  162. // CHECK:STDOUT: %.loc15_32.1: %.16 = tuple_literal (%.loc15_30)
  163. // CHECK:STDOUT: %int.make_type_32.loc15: init type = call constants.%Int32() [template = i32]
  164. // CHECK:STDOUT: %.loc15_44: i32 = int_literal 5 [template = constants.%.17]
  165. // CHECK:STDOUT: %.loc15_47: i32 = int_literal 7 [template = constants.%.18]
  166. // CHECK:STDOUT: %.loc15_50: i32 = int_literal 1 [template = constants.%.5]
  167. // CHECK:STDOUT: %.loc15_53: i32 = int_literal 9 [template = constants.%.19]
  168. // CHECK:STDOUT: %.loc15_54.1: %.20 = tuple_literal (%.loc15_44, %.loc15_47, %.loc15_50, %.loc15_53)
  169. // CHECK:STDOUT: %.loc15_56: i32 = int_literal 2 [template = constants.%.6]
  170. // CHECK:STDOUT: %tuple: %.20 = tuple_value (%.loc15_44, %.loc15_47, %.loc15_50, %.loc15_53) [template = constants.%tuple.2]
  171. // CHECK:STDOUT: %.loc15_54.2: %.20 = converted %.loc15_54.1, %tuple [template = constants.%tuple.2]
  172. // CHECK:STDOUT: %.loc15_57: i32 = tuple_index %.loc15_54.2, %.loc15_56 [template = constants.%.5]
  173. // CHECK:STDOUT: %.loc15_38.1: type = value_of_initializer %int.make_type_32.loc15 [template = i32]
  174. // CHECK:STDOUT: %.loc15_38.2: type = converted %int.make_type_32.loc15, %.loc15_38.1 [template = i32]
  175. // CHECK:STDOUT: %.loc15_58: type = array_type %.loc15_57, i32 [template = constants.%.13]
  176. // CHECK:STDOUT: %.loc15_5: ref %.13 = splice_block file.%tuple_index.var {}
  177. // CHECK:STDOUT: %.loc15_32.2: i32 = int_literal 0 [template = constants.%.15]
  178. // CHECK:STDOUT: %.loc15_32.3: ref i32 = array_index %.loc15_5, %.loc15_32.2
  179. // CHECK:STDOUT: %.loc15_32.4: init i32 = initialize_from %.loc15_30 to %.loc15_32.3 [template = constants.%.15]
  180. // CHECK:STDOUT: %.loc15_32.5: init %.13 = array_init (%.loc15_32.4) to %.loc15_5 [template = constants.%array]
  181. // CHECK:STDOUT: %.loc15_34: init %.13 = converted %.loc15_32.1, %.loc15_32.5 [template = constants.%array]
  182. // CHECK:STDOUT: assign file.%tuple_index.var, %.loc15_34
  183. // CHECK:STDOUT: %.loc17_32: i32 = int_literal 0 [template = constants.%.15]
  184. // CHECK:STDOUT: %.loc17_34.1: %.16 = tuple_literal (%.loc17_32)
  185. // CHECK:STDOUT: %int.make_type_32.loc17: init type = call constants.%Int32() [template = i32]
  186. // CHECK:STDOUT: %.loc17_51: i32 = int_literal 3 [template = constants.%.9]
  187. // CHECK:STDOUT: %.loc17_59: i32 = int_literal 1 [template = constants.%.5]
  188. // CHECK:STDOUT: %.loc17_60.1: %.22 = struct_literal (%.loc17_51, %.loc17_59)
  189. // CHECK:STDOUT: %struct.loc17: %.22 = struct_value (%.loc17_51, %.loc17_59) [template = constants.%struct.3]
  190. // CHECK:STDOUT: %.loc17_60.2: %.22 = converted %.loc17_60.1, %struct.loc17 [template = constants.%struct.3]
  191. // CHECK:STDOUT: %.loc17_61: i32 = struct_access %.loc17_60.2, element1 [template = constants.%.5]
  192. // CHECK:STDOUT: %.loc17_40.1: type = value_of_initializer %int.make_type_32.loc17 [template = i32]
  193. // CHECK:STDOUT: %.loc17_40.2: type = converted %int.make_type_32.loc17, %.loc17_40.1 [template = i32]
  194. // CHECK:STDOUT: %.loc17_63: type = array_type %.loc17_61, i32 [template = constants.%.13]
  195. // CHECK:STDOUT: %.loc17_5: ref %.13 = splice_block file.%struct_access.var {}
  196. // CHECK:STDOUT: %.loc17_34.2: i32 = int_literal 0 [template = constants.%.15]
  197. // CHECK:STDOUT: %.loc17_34.3: ref i32 = array_index %.loc17_5, %.loc17_34.2
  198. // CHECK:STDOUT: %.loc17_34.4: init i32 = initialize_from %.loc17_32 to %.loc17_34.3 [template = constants.%.15]
  199. // CHECK:STDOUT: %.loc17_34.5: init %.13 = array_init (%.loc17_34.4) to %.loc17_5 [template = constants.%array]
  200. // CHECK:STDOUT: %.loc17_36: init %.13 = converted %.loc17_34.1, %.loc17_34.5 [template = constants.%array]
  201. // CHECK:STDOUT: assign file.%struct_access.var, %.loc17_36
  202. // CHECK:STDOUT: return
  203. // CHECK:STDOUT: }
  204. // CHECK:STDOUT: