canonicalize_index.carbon 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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: %int_32: Core.IntLiteral = int_value 32 [template]
  19. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [template]
  20. // CHECK:STDOUT: %Add.type.1: type = fn_type @Add.1 [template]
  21. // CHECK:STDOUT: %Add: %Add.type.1 = struct_value () [template]
  22. // CHECK:STDOUT: %u32: type = class_type @UInt, @UInt(%int_32) [template]
  23. // CHECK:STDOUT: %ConvertToU32.type: type = fn_type @ConvertToU32 [template]
  24. // CHECK:STDOUT: %ConvertToU32: %ConvertToU32.type = struct_value () [template]
  25. // CHECK:STDOUT: %int_1.1: Core.IntLiteral = int_value 1 [template]
  26. // CHECK:STDOUT: %int_2.1: Core.IntLiteral = int_value 2 [template]
  27. // CHECK:STDOUT: %Convert.type.2: type = fn_type @Convert.1, @ImplicitAs(%i32) [template]
  28. // CHECK:STDOUT: %Convert.type.10: type = fn_type @Convert.2, @impl.1(%int_32) [template]
  29. // CHECK:STDOUT: %Convert.10: %Convert.type.10 = struct_value () [template]
  30. // CHECK:STDOUT: %interface.19: <witness> = interface_witness (%Convert.10) [template]
  31. // CHECK:STDOUT: %Convert.bound.1: <bound method> = bound_method %int_1.1, %Convert.10 [template]
  32. // CHECK:STDOUT: %Convert.specific_fn.1: <specific function> = specific_function %Convert.bound.1, @Convert.2(%int_32) [template]
  33. // CHECK:STDOUT: %int_1.2: %i32 = int_value 1 [template]
  34. // CHECK:STDOUT: %Convert.bound.2: <bound method> = bound_method %int_2.1, %Convert.10 [template]
  35. // CHECK:STDOUT: %Convert.specific_fn.2: <specific function> = specific_function %Convert.bound.2, @Convert.2(%int_32) [template]
  36. // CHECK:STDOUT: %int_2.2: %i32 = int_value 2 [template]
  37. // CHECK:STDOUT: %int_3.1: %i32 = int_value 3 [template]
  38. // CHECK:STDOUT: %int_3.2: Core.IntLiteral = int_value 3 [template]
  39. // CHECK:STDOUT: %array_type: type = array_type %int_3.2, %i32 [template]
  40. // CHECK:STDOUT: %ptr: type = ptr_type %array_type [template]
  41. // CHECK:STDOUT: %tuple.type: type = tuple_type (Core.IntLiteral, Core.IntLiteral, Core.IntLiteral) [template]
  42. // CHECK:STDOUT: %int_0: Core.IntLiteral = int_value 0 [template]
  43. // CHECK:STDOUT: %Convert.bound.4: <bound method> = bound_method %int_3.2, %Convert.10 [template]
  44. // CHECK:STDOUT: %Convert.specific_fn.4: <specific function> = specific_function %Convert.bound.4, @Convert.2(%int_32) [template]
  45. // CHECK:STDOUT: %array: %array_type = tuple_value (%int_1.2, %int_2.2, %int_3.1) [template]
  46. // CHECK:STDOUT: }
  47. // CHECK:STDOUT:
  48. // CHECK:STDOUT: imports {
  49. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  50. // CHECK:STDOUT: .Int = %import_ref.1
  51. // CHECK:STDOUT: .UInt = %import_ref.5
  52. // CHECK:STDOUT: .ImplicitAs = %import_ref.9
  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: .Add = %Add.decl
  62. // CHECK:STDOUT: .ConvertToU32 = %ConvertToU32.decl
  63. // CHECK:STDOUT: .a = %a
  64. // CHECK:STDOUT: .b = @__global_init.%b
  65. // CHECK:STDOUT: .c = @__global_init.%c
  66. // CHECK:STDOUT: }
  67. // CHECK:STDOUT: %Core.import = import Core
  68. // CHECK:STDOUT: %Add.decl: %Add.type.1 = fn_decl @Add.1 [template = constants.%Add] {
  69. // CHECK:STDOUT: %a.patt: %i32 = binding_pattern a
  70. // CHECK:STDOUT: %a.param_patt: %i32 = value_param_pattern %a.patt, runtime_param0
  71. // CHECK:STDOUT: %b.patt: %i32 = binding_pattern b
  72. // CHECK:STDOUT: %b.param_patt: %i32 = value_param_pattern %b.patt, runtime_param1
  73. // CHECK:STDOUT: %return.patt: %i32 = return_slot_pattern
  74. // CHECK:STDOUT: %return.param_patt: %i32 = out_param_pattern %return.patt, runtime_param2
  75. // CHECK:STDOUT: } {
  76. // CHECK:STDOUT: %int_32.loc11_27: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  77. // CHECK:STDOUT: %i32.loc11_27: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  78. // CHECK:STDOUT: %a.param: %i32 = value_param runtime_param0
  79. // CHECK:STDOUT: %.loc11_11: type = splice_block %i32.loc11_11 [template = constants.%i32] {
  80. // CHECK:STDOUT: %int_32.loc11_11: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  81. // CHECK:STDOUT: %i32.loc11_11: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  82. // CHECK:STDOUT: }
  83. // CHECK:STDOUT: %a: %i32 = bind_name a, %a.param
  84. // CHECK:STDOUT: %b.param: %i32 = value_param runtime_param1
  85. // CHECK:STDOUT: %.loc11_19: type = splice_block %i32.loc11_19 [template = constants.%i32] {
  86. // CHECK:STDOUT: %int_32.loc11_19: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  87. // CHECK:STDOUT: %i32.loc11_19: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  88. // CHECK:STDOUT: }
  89. // CHECK:STDOUT: %b: %i32 = bind_name b, %b.param
  90. // CHECK:STDOUT: %return.param: ref %i32 = out_param runtime_param2
  91. // CHECK:STDOUT: %return: ref %i32 = return_slot %return.param
  92. // CHECK:STDOUT: }
  93. // CHECK:STDOUT: %ConvertToU32.decl: %ConvertToU32.type = fn_decl @ConvertToU32 [template = constants.%ConvertToU32] {
  94. // CHECK:STDOUT: %a.patt: %i32 = binding_pattern a
  95. // CHECK:STDOUT: %a.param_patt: %i32 = value_param_pattern %a.patt, runtime_param0
  96. // CHECK:STDOUT: %return.patt: %u32 = return_slot_pattern
  97. // CHECK:STDOUT: %return.param_patt: %u32 = out_param_pattern %return.patt, runtime_param1
  98. // CHECK:STDOUT: } {
  99. // CHECK:STDOUT: %int_32.loc12_28: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  100. // CHECK:STDOUT: %u32: type = class_type @UInt, @UInt(constants.%int_32) [template = constants.%u32]
  101. // CHECK:STDOUT: %a.param: %i32 = value_param runtime_param0
  102. // CHECK:STDOUT: %.loc12: type = splice_block %i32 [template = constants.%i32] {
  103. // CHECK:STDOUT: %int_32.loc12_20: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  104. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  105. // CHECK:STDOUT: }
  106. // CHECK:STDOUT: %a: %i32 = bind_name a, %a.param
  107. // CHECK:STDOUT: %return.param: ref %u32 = out_param runtime_param1
  108. // CHECK:STDOUT: %return: ref %u32 = return_slot %return.param
  109. // CHECK:STDOUT: }
  110. // CHECK:STDOUT: %a.var: ref %array_type = var a
  111. // CHECK:STDOUT: %a: ref %array_type = bind_name a, %a.var
  112. // CHECK:STDOUT: }
  113. // CHECK:STDOUT:
  114. // CHECK:STDOUT: fn @Add.1(%a.param_patt: %i32, %b.param_patt: %i32) -> %i32 = "int.sadd";
  115. // CHECK:STDOUT:
  116. // CHECK:STDOUT: fn @ConvertToU32(%a.param_patt: %i32) -> %u32 = "int.convert_checked";
  117. // CHECK:STDOUT:
  118. // CHECK:STDOUT: fn @__global_init() {
  119. // CHECK:STDOUT: !entry:
  120. // CHECK:STDOUT: %int_1.loc14_28: Core.IntLiteral = int_value 1 [template = constants.%int_1.1]
  121. // CHECK:STDOUT: %int_2.loc14_31: Core.IntLiteral = int_value 2 [template = constants.%int_2.1]
  122. // CHECK:STDOUT: %int_3: Core.IntLiteral = int_value 3 [template = constants.%int_3.2]
  123. // CHECK:STDOUT: %.loc14_35.1: %tuple.type = tuple_literal (%int_1.loc14_28, %int_2.loc14_31, %int_3)
  124. // CHECK:STDOUT: %impl.elem0.loc14_35.1: %Convert.type.2 = interface_witness_access constants.%interface.19, element0 [template = constants.%Convert.10]
  125. // CHECK:STDOUT: %Convert.bound.loc14_35.1: <bound method> = bound_method %int_1.loc14_28, %impl.elem0.loc14_35.1 [template = constants.%Convert.bound.1]
  126. // CHECK:STDOUT: %Convert.specific_fn.loc14_35.1: <specific function> = specific_function %Convert.bound.loc14_35.1, @Convert.2(constants.%int_32) [template = constants.%Convert.specific_fn.1]
  127. // CHECK:STDOUT: %int.convert_checked.loc14_35.1: init %i32 = call %Convert.specific_fn.loc14_35.1(%int_1.loc14_28) [template = constants.%int_1.2]
  128. // CHECK:STDOUT: %.loc14_35.2: init %i32 = converted %int_1.loc14_28, %int.convert_checked.loc14_35.1 [template = constants.%int_1.2]
  129. // CHECK:STDOUT: %int_0: Core.IntLiteral = int_value 0 [template = constants.%int_0]
  130. // CHECK:STDOUT: %.loc14_35.3: ref %i32 = array_index file.%a.var, %int_0
  131. // CHECK:STDOUT: %.loc14_35.4: init %i32 = initialize_from %.loc14_35.2 to %.loc14_35.3 [template = constants.%int_1.2]
  132. // CHECK:STDOUT: %impl.elem0.loc14_35.2: %Convert.type.2 = interface_witness_access constants.%interface.19, element0 [template = constants.%Convert.10]
  133. // CHECK:STDOUT: %Convert.bound.loc14_35.2: <bound method> = bound_method %int_2.loc14_31, %impl.elem0.loc14_35.2 [template = constants.%Convert.bound.2]
  134. // CHECK:STDOUT: %Convert.specific_fn.loc14_35.2: <specific function> = specific_function %Convert.bound.loc14_35.2, @Convert.2(constants.%int_32) [template = constants.%Convert.specific_fn.2]
  135. // CHECK:STDOUT: %int.convert_checked.loc14_35.2: init %i32 = call %Convert.specific_fn.loc14_35.2(%int_2.loc14_31) [template = constants.%int_2.2]
  136. // CHECK:STDOUT: %.loc14_35.5: init %i32 = converted %int_2.loc14_31, %int.convert_checked.loc14_35.2 [template = constants.%int_2.2]
  137. // CHECK:STDOUT: %int_1.loc14_35: Core.IntLiteral = int_value 1 [template = constants.%int_1.1]
  138. // CHECK:STDOUT: %.loc14_35.6: ref %i32 = array_index file.%a.var, %int_1.loc14_35
  139. // CHECK:STDOUT: %.loc14_35.7: init %i32 = initialize_from %.loc14_35.5 to %.loc14_35.6 [template = constants.%int_2.2]
  140. // CHECK:STDOUT: %impl.elem0.loc14_35.3: %Convert.type.2 = interface_witness_access constants.%interface.19, element0 [template = constants.%Convert.10]
  141. // CHECK:STDOUT: %Convert.bound.loc14_35.3: <bound method> = bound_method %int_3, %impl.elem0.loc14_35.3 [template = constants.%Convert.bound.4]
  142. // CHECK:STDOUT: %Convert.specific_fn.loc14_35.3: <specific function> = specific_function %Convert.bound.loc14_35.3, @Convert.2(constants.%int_32) [template = constants.%Convert.specific_fn.4]
  143. // CHECK:STDOUT: %int.convert_checked.loc14_35.3: init %i32 = call %Convert.specific_fn.loc14_35.3(%int_3) [template = constants.%int_3.1]
  144. // CHECK:STDOUT: %.loc14_35.8: init %i32 = converted %int_3, %int.convert_checked.loc14_35.3 [template = constants.%int_3.1]
  145. // CHECK:STDOUT: %int_2.loc14_35: Core.IntLiteral = int_value 2 [template = constants.%int_2.1]
  146. // CHECK:STDOUT: %.loc14_35.9: ref %i32 = array_index file.%a.var, %int_2.loc14_35
  147. // CHECK:STDOUT: %.loc14_35.10: init %i32 = initialize_from %.loc14_35.8 to %.loc14_35.9 [template = constants.%int_3.1]
  148. // CHECK:STDOUT: %.loc14_35.11: init %array_type = array_init (%.loc14_35.4, %.loc14_35.7, %.loc14_35.10) to file.%a.var [template = constants.%array]
  149. // CHECK:STDOUT: %.loc14_36: init %array_type = converted %.loc14_35.1, %.loc14_35.11 [template = constants.%array]
  150. // CHECK:STDOUT: assign file.%a.var, %.loc14_36
  151. // CHECK:STDOUT: %a.ref.loc15: ref %array_type = name_ref a, file.%a
  152. // CHECK:STDOUT: %addr.loc15: %ptr = addr_of %a.ref.loc15
  153. // CHECK:STDOUT: %b: %ptr = bind_name b, %addr.loc15
  154. // CHECK:STDOUT: %a.ref.loc16: ref %array_type = name_ref a, file.%a
  155. // CHECK:STDOUT: %addr.loc16: %ptr = addr_of %a.ref.loc16
  156. // CHECK:STDOUT: %c: %ptr = bind_name c, %addr.loc16
  157. // CHECK:STDOUT: return
  158. // CHECK:STDOUT: }
  159. // CHECK:STDOUT: