canonicalize_index.carbon 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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/array/canonicalize_index.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/array/canonicalize_index.carbon
  10. fn Add(a: i32, b: i32) -> i32 = "int.sadd";
  11. fn ConvertToU32(a: i32) -> u32 = "int.convert_checked";
  12. var a: [i32; Add(1, 2)] = (1, 2, 3);
  13. let b: [i32; 3]* = &a;
  14. let c: [i32; ConvertToU32(3)]* = &a;
  15. // CHECK:STDOUT: --- canonicalize_index.carbon
  16. // CHECK:STDOUT:
  17. // CHECK:STDOUT: constants {
  18. // CHECK:STDOUT: %Int32.type: type = fn_type @Int32 [template]
  19. // CHECK:STDOUT: %Int32: %Int32.type = struct_value () [template]
  20. // CHECK:STDOUT: %Add.type: type = fn_type @Add [template]
  21. // CHECK:STDOUT: %Add: %Add.type = struct_value () [template]
  22. // CHECK:STDOUT: %.1: Core.IntLiteral = int_value 32 [template]
  23. // CHECK:STDOUT: %UInt.type: type = fn_type @UInt [template]
  24. // CHECK:STDOUT: %UInt: %UInt.type = struct_value () [template]
  25. // CHECK:STDOUT: %.2: type = int_type unsigned, %.1 [template]
  26. // CHECK:STDOUT: %ConvertToU32.type: type = fn_type @ConvertToU32 [template]
  27. // CHECK:STDOUT: %ConvertToU32: %ConvertToU32.type = struct_value () [template]
  28. // CHECK:STDOUT: %.3: Core.IntLiteral = int_value 1 [template]
  29. // CHECK:STDOUT: %.4: Core.IntLiteral = int_value 2 [template]
  30. // CHECK:STDOUT: %Convert.type.2: type = fn_type @Convert.1, @ImplicitAs(i32) [template]
  31. // CHECK:STDOUT: %Convert.type.5: type = fn_type @Convert.1, @ImplicitAs(Core.IntLiteral) [template]
  32. // CHECK:STDOUT: %Convert.type.15: type = fn_type @Convert.11 [template]
  33. // CHECK:STDOUT: %Convert.15: %Convert.type.15 = struct_value () [template]
  34. // CHECK:STDOUT: %.28: <witness> = interface_witness (%Convert.15) [template]
  35. // CHECK:STDOUT: %.29: <bound method> = bound_method %.3, %Convert.15 [template]
  36. // CHECK:STDOUT: %.30: i32 = int_value 1 [template]
  37. // CHECK:STDOUT: %.31: <bound method> = bound_method %.4, %Convert.15 [template]
  38. // CHECK:STDOUT: %.32: i32 = int_value 2 [template]
  39. // CHECK:STDOUT: %.33: i32 = int_value 3 [template]
  40. // CHECK:STDOUT: %Convert.type.16: type = fn_type @Convert.12 [template]
  41. // CHECK:STDOUT: %Convert.16: %Convert.type.16 = struct_value () [template]
  42. // CHECK:STDOUT: %.34: <witness> = interface_witness (%Convert.16) [template]
  43. // CHECK:STDOUT: %.35: <bound method> = bound_method %.33, %Convert.16 [template]
  44. // CHECK:STDOUT: %.36: Core.IntLiteral = int_value 3 [template]
  45. // CHECK:STDOUT: %.37: type = array_type %.36, i32 [template]
  46. // CHECK:STDOUT: %.38: type = ptr_type %.37 [template]
  47. // CHECK:STDOUT: %tuple.type: type = tuple_type (Core.IntLiteral, Core.IntLiteral, Core.IntLiteral) [template]
  48. // CHECK:STDOUT: %.39: i32 = int_value 0 [template]
  49. // CHECK:STDOUT: %.40: <bound method> = bound_method %.36, %Convert.15 [template]
  50. // CHECK:STDOUT: %array: %.37 = tuple_value (%.30, %.32, %.33) [template]
  51. // CHECK:STDOUT: %.41: %.2 = int_value 3 [template]
  52. // CHECK:STDOUT: %Convert.type.17: type = fn_type @Convert.5, @impl.6(%.1) [template]
  53. // CHECK:STDOUT: %Convert.17: %Convert.type.17 = struct_value () [template]
  54. // CHECK:STDOUT: %.43: <witness> = interface_witness (%Convert.17) [template]
  55. // CHECK:STDOUT: %.44: <bound method> = bound_method %.41, %Convert.17 [template]
  56. // CHECK:STDOUT: %.45: <specific function> = specific_function %.44, @Convert.5(%.1) [template]
  57. // CHECK:STDOUT: }
  58. // CHECK:STDOUT:
  59. // CHECK:STDOUT: imports {
  60. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  61. // CHECK:STDOUT: .Int32 = %import_ref.1
  62. // CHECK:STDOUT: .UInt = %import_ref.2
  63. // CHECK:STDOUT: .ImplicitAs = %import_ref.3
  64. // CHECK:STDOUT: import Core//prelude
  65. // CHECK:STDOUT: import Core//prelude/...
  66. // CHECK:STDOUT: }
  67. // CHECK:STDOUT: }
  68. // CHECK:STDOUT:
  69. // CHECK:STDOUT: file {
  70. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  71. // CHECK:STDOUT: .Core = imports.%Core
  72. // CHECK:STDOUT: .Add = %Add.decl
  73. // CHECK:STDOUT: .ConvertToU32 = %ConvertToU32.decl
  74. // CHECK:STDOUT: .a = %a
  75. // CHECK:STDOUT: .b = @__global_init.%b
  76. // CHECK:STDOUT: .c = @__global_init.%c
  77. // CHECK:STDOUT: }
  78. // CHECK:STDOUT: %Core.import = import Core
  79. // CHECK:STDOUT: %Add.decl: %Add.type = fn_decl @Add [template = constants.%Add] {
  80. // CHECK:STDOUT: %a.patt: i32 = binding_pattern a
  81. // CHECK:STDOUT: %a.param_patt: i32 = value_param_pattern %a.patt, runtime_param0
  82. // CHECK:STDOUT: %b.patt: i32 = binding_pattern b
  83. // CHECK:STDOUT: %b.param_patt: i32 = value_param_pattern %b.patt, runtime_param1
  84. // CHECK:STDOUT: %return.patt: i32 = return_slot_pattern
  85. // CHECK:STDOUT: %return.param_patt: i32 = out_param_pattern %return.patt, runtime_param2
  86. // CHECK:STDOUT: } {
  87. // CHECK:STDOUT: %int.make_type_32.loc11_11: init type = call constants.%Int32() [template = i32]
  88. // CHECK:STDOUT: %.loc11_11.1: type = value_of_initializer %int.make_type_32.loc11_11 [template = i32]
  89. // CHECK:STDOUT: %.loc11_11.2: type = converted %int.make_type_32.loc11_11, %.loc11_11.1 [template = i32]
  90. // CHECK:STDOUT: %int.make_type_32.loc11_19: init type = call constants.%Int32() [template = i32]
  91. // CHECK:STDOUT: %.loc11_19.1: type = value_of_initializer %int.make_type_32.loc11_19 [template = i32]
  92. // CHECK:STDOUT: %.loc11_19.2: type = converted %int.make_type_32.loc11_19, %.loc11_19.1 [template = i32]
  93. // CHECK:STDOUT: %int.make_type_32.loc11_27: init type = call constants.%Int32() [template = i32]
  94. // CHECK:STDOUT: %.loc11_27.1: type = value_of_initializer %int.make_type_32.loc11_27 [template = i32]
  95. // CHECK:STDOUT: %.loc11_27.2: type = converted %int.make_type_32.loc11_27, %.loc11_27.1 [template = i32]
  96. // CHECK:STDOUT: %a.param: i32 = value_param runtime_param0
  97. // CHECK:STDOUT: %a: i32 = bind_name a, %a.param
  98. // CHECK:STDOUT: %b.param: i32 = value_param runtime_param1
  99. // CHECK:STDOUT: %b: i32 = bind_name b, %b.param
  100. // CHECK:STDOUT: %return.param: ref i32 = out_param runtime_param2
  101. // CHECK:STDOUT: %return: ref i32 = return_slot %return.param
  102. // CHECK:STDOUT: }
  103. // CHECK:STDOUT: %ConvertToU32.decl: %ConvertToU32.type = fn_decl @ConvertToU32 [template = constants.%ConvertToU32] {
  104. // CHECK:STDOUT: %a.patt: i32 = binding_pattern a
  105. // CHECK:STDOUT: %a.param_patt: i32 = value_param_pattern %a.patt, runtime_param0
  106. // CHECK:STDOUT: %return.patt: %.2 = return_slot_pattern
  107. // CHECK:STDOUT: %return.param_patt: %.2 = out_param_pattern %return.patt, runtime_param1
  108. // CHECK:STDOUT: } {
  109. // CHECK:STDOUT: %int.make_type_32: init type = call constants.%Int32() [template = i32]
  110. // CHECK:STDOUT: %.loc12_20.1: type = value_of_initializer %int.make_type_32 [template = i32]
  111. // CHECK:STDOUT: %.loc12_20.2: type = converted %int.make_type_32, %.loc12_20.1 [template = i32]
  112. // CHECK:STDOUT: %.loc12_28.1: Core.IntLiteral = int_value 32 [template = constants.%.1]
  113. // CHECK:STDOUT: %int.make_type_unsigned: init type = call constants.%UInt(%.loc12_28.1) [template = constants.%.2]
  114. // CHECK:STDOUT: %.loc12_28.2: type = value_of_initializer %int.make_type_unsigned [template = constants.%.2]
  115. // CHECK:STDOUT: %.loc12_28.3: type = converted %int.make_type_unsigned, %.loc12_28.2 [template = constants.%.2]
  116. // CHECK:STDOUT: %a.param: i32 = value_param runtime_param0
  117. // CHECK:STDOUT: %a: i32 = bind_name a, %a.param
  118. // CHECK:STDOUT: %return.param: ref %.2 = out_param runtime_param1
  119. // CHECK:STDOUT: %return: ref %.2 = return_slot %return.param
  120. // CHECK:STDOUT: }
  121. // CHECK:STDOUT: %int.make_type_32.loc14: init type = call constants.%Int32() [template = i32]
  122. // CHECK:STDOUT: %Add.ref: %Add.type = name_ref Add, %Add.decl [template = constants.%Add]
  123. // CHECK:STDOUT: %.loc14_18.1: Core.IntLiteral = int_value 1 [template = constants.%.3]
  124. // CHECK:STDOUT: %.loc14_21.1: Core.IntLiteral = int_value 2 [template = constants.%.4]
  125. // CHECK:STDOUT: %.loc14_18.2: %Convert.type.2 = interface_witness_access constants.%.28, element0 [template = constants.%Convert.15]
  126. // CHECK:STDOUT: %.loc14_18.3: <bound method> = bound_method %.loc14_18.1, %.loc14_18.2 [template = constants.%.29]
  127. // CHECK:STDOUT: %int.convert_checked.loc14_18: init i32 = call %.loc14_18.3(%.loc14_18.1) [template = constants.%.30]
  128. // CHECK:STDOUT: %.loc14_18.4: i32 = value_of_initializer %int.convert_checked.loc14_18 [template = constants.%.30]
  129. // CHECK:STDOUT: %.loc14_18.5: i32 = converted %.loc14_18.1, %.loc14_18.4 [template = constants.%.30]
  130. // CHECK:STDOUT: %.loc14_21.2: %Convert.type.2 = interface_witness_access constants.%.28, element0 [template = constants.%Convert.15]
  131. // CHECK:STDOUT: %.loc14_21.3: <bound method> = bound_method %.loc14_21.1, %.loc14_21.2 [template = constants.%.31]
  132. // CHECK:STDOUT: %int.convert_checked.loc14_21: init i32 = call %.loc14_21.3(%.loc14_21.1) [template = constants.%.32]
  133. // CHECK:STDOUT: %.loc14_21.4: i32 = value_of_initializer %int.convert_checked.loc14_21 [template = constants.%.32]
  134. // CHECK:STDOUT: %.loc14_21.5: i32 = converted %.loc14_21.1, %.loc14_21.4 [template = constants.%.32]
  135. // CHECK:STDOUT: %int.sadd: init i32 = call %Add.ref(%.loc14_18.5, %.loc14_21.5) [template = constants.%.33]
  136. // CHECK:STDOUT: %.loc14_9.1: type = value_of_initializer %int.make_type_32.loc14 [template = i32]
  137. // CHECK:STDOUT: %.loc14_9.2: type = converted %int.make_type_32.loc14, %.loc14_9.1 [template = i32]
  138. // CHECK:STDOUT: %.loc14_17.1: %Convert.type.5 = interface_witness_access constants.%.34, element0 [template = constants.%Convert.16]
  139. // CHECK:STDOUT: %.loc14_17.2: <bound method> = bound_method %int.sadd, %.loc14_17.1 [template = constants.%.35]
  140. // CHECK:STDOUT: %.loc14_17.3: i32 = value_of_initializer %int.sadd [template = constants.%.33]
  141. // CHECK:STDOUT: %.loc14_17.4: i32 = converted %int.sadd, %.loc14_17.3 [template = constants.%.33]
  142. // CHECK:STDOUT: %int.convert_checked.loc14_17: init Core.IntLiteral = call %.loc14_17.2(%.loc14_17.4) [template = constants.%.36]
  143. // CHECK:STDOUT: %.loc14_17.5: Core.IntLiteral = value_of_initializer %int.convert_checked.loc14_17 [template = constants.%.36]
  144. // CHECK:STDOUT: %.loc14_17.6: Core.IntLiteral = converted %int.sadd, %.loc14_17.5 [template = constants.%.36]
  145. // CHECK:STDOUT: %.loc14_23: type = array_type %.loc14_17.6, i32 [template = constants.%.37]
  146. // CHECK:STDOUT: %a.var: ref %.37 = var a
  147. // CHECK:STDOUT: %a: ref %.37 = bind_name a, %a.var
  148. // CHECK:STDOUT: %int.make_type_32.loc15: init type = call constants.%Int32() [template = i32]
  149. // CHECK:STDOUT: %.loc15_14: Core.IntLiteral = int_value 3 [template = constants.%.36]
  150. // CHECK:STDOUT: %.loc15_9.1: type = value_of_initializer %int.make_type_32.loc15 [template = i32]
  151. // CHECK:STDOUT: %.loc15_9.2: type = converted %int.make_type_32.loc15, %.loc15_9.1 [template = i32]
  152. // CHECK:STDOUT: %.loc15_15: type = array_type %.loc15_14, i32 [template = constants.%.37]
  153. // CHECK:STDOUT: %.loc15_16: type = ptr_type %.37 [template = constants.%.38]
  154. // CHECK:STDOUT: %int.make_type_32.loc16: init type = call constants.%Int32() [template = i32]
  155. // CHECK:STDOUT: %ConvertToU32.ref: %ConvertToU32.type = name_ref ConvertToU32, %ConvertToU32.decl [template = constants.%ConvertToU32]
  156. // CHECK:STDOUT: %.loc16_27.1: Core.IntLiteral = int_value 3 [template = constants.%.36]
  157. // CHECK:STDOUT: %.loc16_27.2: %Convert.type.2 = interface_witness_access constants.%.28, element0 [template = constants.%Convert.15]
  158. // CHECK:STDOUT: %.loc16_27.3: <bound method> = bound_method %.loc16_27.1, %.loc16_27.2 [template = constants.%.40]
  159. // CHECK:STDOUT: %int.convert_checked.loc16_27: init i32 = call %.loc16_27.3(%.loc16_27.1) [template = constants.%.33]
  160. // CHECK:STDOUT: %.loc16_27.4: i32 = value_of_initializer %int.convert_checked.loc16_27 [template = constants.%.33]
  161. // CHECK:STDOUT: %.loc16_27.5: i32 = converted %.loc16_27.1, %.loc16_27.4 [template = constants.%.33]
  162. // CHECK:STDOUT: %int.convert_checked.loc16_26.1: init %.2 = call %ConvertToU32.ref(%.loc16_27.5) [template = constants.%.41]
  163. // CHECK:STDOUT: %.loc16_9.1: type = value_of_initializer %int.make_type_32.loc16 [template = i32]
  164. // CHECK:STDOUT: %.loc16_9.2: type = converted %int.make_type_32.loc16, %.loc16_9.1 [template = i32]
  165. // CHECK:STDOUT: %.loc16_26.1: %Convert.type.5 = interface_witness_access constants.%.43, element0 [template = constants.%Convert.17]
  166. // CHECK:STDOUT: %.loc16_26.2: <bound method> = bound_method %int.convert_checked.loc16_26.1, %.loc16_26.1 [template = constants.%.44]
  167. // CHECK:STDOUT: %.loc16_26.3: <specific function> = specific_function %.loc16_26.2, @Convert.5(constants.%.1) [template = constants.%.45]
  168. // CHECK:STDOUT: %.loc16_26.4: %.2 = value_of_initializer %int.convert_checked.loc16_26.1 [template = constants.%.41]
  169. // CHECK:STDOUT: %.loc16_26.5: %.2 = converted %int.convert_checked.loc16_26.1, %.loc16_26.4 [template = constants.%.41]
  170. // CHECK:STDOUT: %int.convert_checked.loc16_26.2: init Core.IntLiteral = call %.loc16_26.3(%.loc16_26.5) [template = constants.%.36]
  171. // CHECK:STDOUT: %.loc16_26.6: Core.IntLiteral = value_of_initializer %int.convert_checked.loc16_26.2 [template = constants.%.36]
  172. // CHECK:STDOUT: %.loc16_26.7: Core.IntLiteral = converted %int.convert_checked.loc16_26.1, %.loc16_26.6 [template = constants.%.36]
  173. // CHECK:STDOUT: %.loc16_29: type = array_type %.loc16_26.7, i32 [template = constants.%.37]
  174. // CHECK:STDOUT: %.loc16_30: type = ptr_type %.37 [template = constants.%.38]
  175. // CHECK:STDOUT: }
  176. // CHECK:STDOUT:
  177. // CHECK:STDOUT: fn @Add(%a.param_patt: i32, %b.param_patt: i32) -> i32 = "int.sadd";
  178. // CHECK:STDOUT:
  179. // CHECK:STDOUT: fn @ConvertToU32(%a.param_patt: i32) -> %.2 = "int.convert_checked";
  180. // CHECK:STDOUT:
  181. // CHECK:STDOUT: fn @__global_init() {
  182. // CHECK:STDOUT: !entry:
  183. // CHECK:STDOUT: %.loc14_28: Core.IntLiteral = int_value 1 [template = constants.%.3]
  184. // CHECK:STDOUT: %.loc14_31: Core.IntLiteral = int_value 2 [template = constants.%.4]
  185. // CHECK:STDOUT: %.loc14_34: Core.IntLiteral = int_value 3 [template = constants.%.36]
  186. // CHECK:STDOUT: %.loc14_35.1: %tuple.type = tuple_literal (%.loc14_28, %.loc14_31, %.loc14_34)
  187. // CHECK:STDOUT: %.loc14_35.2: %Convert.type.2 = interface_witness_access constants.%.28, element0 [template = constants.%Convert.15]
  188. // CHECK:STDOUT: %.loc14_35.3: <bound method> = bound_method %.loc14_28, %.loc14_35.2 [template = constants.%.29]
  189. // CHECK:STDOUT: %int.convert_checked.loc14_35.1: init i32 = call %.loc14_35.3(%.loc14_28) [template = constants.%.30]
  190. // CHECK:STDOUT: %.loc14_35.4: init i32 = converted %.loc14_28, %int.convert_checked.loc14_35.1 [template = constants.%.30]
  191. // CHECK:STDOUT: %.loc14_35.5: i32 = int_value 0 [template = constants.%.39]
  192. // CHECK:STDOUT: %.loc14_35.6: ref i32 = array_index file.%a.var, %.loc14_35.5
  193. // CHECK:STDOUT: %.loc14_35.7: init i32 = initialize_from %.loc14_35.4 to %.loc14_35.6 [template = constants.%.30]
  194. // CHECK:STDOUT: %.loc14_35.8: %Convert.type.2 = interface_witness_access constants.%.28, element0 [template = constants.%Convert.15]
  195. // CHECK:STDOUT: %.loc14_35.9: <bound method> = bound_method %.loc14_31, %.loc14_35.8 [template = constants.%.31]
  196. // CHECK:STDOUT: %int.convert_checked.loc14_35.2: init i32 = call %.loc14_35.9(%.loc14_31) [template = constants.%.32]
  197. // CHECK:STDOUT: %.loc14_35.10: init i32 = converted %.loc14_31, %int.convert_checked.loc14_35.2 [template = constants.%.32]
  198. // CHECK:STDOUT: %.loc14_35.11: i32 = int_value 1 [template = constants.%.30]
  199. // CHECK:STDOUT: %.loc14_35.12: ref i32 = array_index file.%a.var, %.loc14_35.11
  200. // CHECK:STDOUT: %.loc14_35.13: init i32 = initialize_from %.loc14_35.10 to %.loc14_35.12 [template = constants.%.32]
  201. // CHECK:STDOUT: %.loc14_35.14: %Convert.type.2 = interface_witness_access constants.%.28, element0 [template = constants.%Convert.15]
  202. // CHECK:STDOUT: %.loc14_35.15: <bound method> = bound_method %.loc14_34, %.loc14_35.14 [template = constants.%.40]
  203. // CHECK:STDOUT: %int.convert_checked.loc14_35.3: init i32 = call %.loc14_35.15(%.loc14_34) [template = constants.%.33]
  204. // CHECK:STDOUT: %.loc14_35.16: init i32 = converted %.loc14_34, %int.convert_checked.loc14_35.3 [template = constants.%.33]
  205. // CHECK:STDOUT: %.loc14_35.17: i32 = int_value 2 [template = constants.%.32]
  206. // CHECK:STDOUT: %.loc14_35.18: ref i32 = array_index file.%a.var, %.loc14_35.17
  207. // CHECK:STDOUT: %.loc14_35.19: init i32 = initialize_from %.loc14_35.16 to %.loc14_35.18 [template = constants.%.33]
  208. // CHECK:STDOUT: %.loc14_35.20: init %.37 = array_init (%.loc14_35.7, %.loc14_35.13, %.loc14_35.19) to file.%a.var [template = constants.%array]
  209. // CHECK:STDOUT: %.loc14_36: init %.37 = converted %.loc14_35.1, %.loc14_35.20 [template = constants.%array]
  210. // CHECK:STDOUT: assign file.%a.var, %.loc14_36
  211. // CHECK:STDOUT: %a.ref.loc15: ref %.37 = name_ref a, file.%a
  212. // CHECK:STDOUT: %.loc15: %.38 = addr_of %a.ref.loc15
  213. // CHECK:STDOUT: %b: %.38 = bind_name b, %.loc15
  214. // CHECK:STDOUT: %a.ref.loc16: ref %.37 = name_ref a, file.%a
  215. // CHECK:STDOUT: %.loc16: %.38 = addr_of %a.ref.loc16
  216. // CHECK:STDOUT: %c: %.38 = bind_name c, %.loc16
  217. // CHECK:STDOUT: return
  218. // CHECK:STDOUT: }
  219. // CHECK:STDOUT: