function_param.carbon 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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/function_param.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/array/function_param.carbon
  10. fn F(arr: [i32; 3], i: i32) -> i32 {
  11. return arr[i];
  12. }
  13. fn G() -> i32 {
  14. return F((1, 2, 3), 1);
  15. }
  16. // CHECK:STDOUT: --- function_param.carbon
  17. // CHECK:STDOUT:
  18. // CHECK:STDOUT: constants {
  19. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [template]
  20. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [template]
  21. // CHECK:STDOUT: %int_3.1ba: Core.IntLiteral = int_value 3 [template]
  22. // CHECK:STDOUT: %array_type: type = array_type %int_3.1ba, %i32 [template]
  23. // CHECK:STDOUT: %F.type: type = fn_type @F [template]
  24. // CHECK:STDOUT: %F: %F.type = struct_value () [template]
  25. // CHECK:STDOUT: %G.type: type = fn_type @G [template]
  26. // CHECK:STDOUT: %G: %G.type = struct_value () [template]
  27. // CHECK:STDOUT: %int_1.5b8: Core.IntLiteral = int_value 1 [template]
  28. // CHECK:STDOUT: %int_2.ecc: Core.IntLiteral = int_value 2 [template]
  29. // CHECK:STDOUT: %tuple.type: type = tuple_type (Core.IntLiteral, Core.IntLiteral, Core.IntLiteral) [template]
  30. // CHECK:STDOUT: %int_0: Core.IntLiteral = int_value 0 [template]
  31. // CHECK:STDOUT: %Convert.type.1b6: type = fn_type @Convert.1, @ImplicitAs(%i32) [template]
  32. // CHECK:STDOUT: %impl_witness.d39: <witness> = impl_witness (imports.%Core.import_ref.a5b), @impl.1(%int_32) [template]
  33. // CHECK:STDOUT: %Convert.type.035: type = fn_type @Convert.2, @impl.1(%int_32) [template]
  34. // CHECK:STDOUT: %Convert.956: %Convert.type.035 = struct_value () [template]
  35. // CHECK:STDOUT: %Convert.bound.ab5: <bound method> = bound_method %int_1.5b8, %Convert.956 [template]
  36. // CHECK:STDOUT: %Convert.specific_fn.70c: <specific function> = specific_function %Convert.bound.ab5, @Convert.2(%int_32) [template]
  37. // CHECK:STDOUT: %int_1.5d2: %i32 = int_value 1 [template]
  38. // CHECK:STDOUT: %Convert.bound.ef9: <bound method> = bound_method %int_2.ecc, %Convert.956 [template]
  39. // CHECK:STDOUT: %Convert.specific_fn.787: <specific function> = specific_function %Convert.bound.ef9, @Convert.2(%int_32) [template]
  40. // CHECK:STDOUT: %int_2.ef8: %i32 = int_value 2 [template]
  41. // CHECK:STDOUT: %Convert.bound.b30: <bound method> = bound_method %int_3.1ba, %Convert.956 [template]
  42. // CHECK:STDOUT: %Convert.specific_fn.b42: <specific function> = specific_function %Convert.bound.b30, @Convert.2(%int_32) [template]
  43. // CHECK:STDOUT: %int_3.822: %i32 = int_value 3 [template]
  44. // CHECK:STDOUT: %array: %array_type = tuple_value (%int_1.5d2, %int_2.ef8, %int_3.822) [template]
  45. // CHECK:STDOUT: }
  46. // CHECK:STDOUT:
  47. // CHECK:STDOUT: imports {
  48. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  49. // CHECK:STDOUT: .Int = %Core.Int
  50. // CHECK:STDOUT: .ImplicitAs = %Core.ImplicitAs
  51. // CHECK:STDOUT: import Core//prelude
  52. // CHECK:STDOUT: import Core//prelude/...
  53. // CHECK:STDOUT: }
  54. // CHECK:STDOUT: }
  55. // CHECK:STDOUT:
  56. // CHECK:STDOUT: file {
  57. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  58. // CHECK:STDOUT: .Core = imports.%Core
  59. // CHECK:STDOUT: .F = %F.decl
  60. // CHECK:STDOUT: .G = %G.decl
  61. // CHECK:STDOUT: }
  62. // CHECK:STDOUT: %Core.import = import Core
  63. // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [template = constants.%F] {
  64. // CHECK:STDOUT: %arr.patt: %array_type = binding_pattern arr
  65. // CHECK:STDOUT: %arr.param_patt: %array_type = value_param_pattern %arr.patt, runtime_param0
  66. // CHECK:STDOUT: %i.patt: %i32 = binding_pattern i
  67. // CHECK:STDOUT: %i.param_patt: %i32 = value_param_pattern %i.patt, runtime_param1
  68. // CHECK:STDOUT: %return.patt: %i32 = return_slot_pattern
  69. // CHECK:STDOUT: %return.param_patt: %i32 = out_param_pattern %return.patt, runtime_param2
  70. // CHECK:STDOUT: } {
  71. // CHECK:STDOUT: %int_32.loc11_32: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  72. // CHECK:STDOUT: %i32.loc11_32: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  73. // CHECK:STDOUT: %arr.param: %array_type = value_param runtime_param0
  74. // CHECK:STDOUT: %.loc11_18: type = splice_block %array_type [template = constants.%array_type] {
  75. // CHECK:STDOUT: %int_32.loc11_12: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  76. // CHECK:STDOUT: %i32.loc11_12: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  77. // CHECK:STDOUT: %int_3: Core.IntLiteral = int_value 3 [template = constants.%int_3.1ba]
  78. // CHECK:STDOUT: %array_type: type = array_type %int_3, %i32 [template = constants.%array_type]
  79. // CHECK:STDOUT: }
  80. // CHECK:STDOUT: %arr: %array_type = bind_name arr, %arr.param
  81. // CHECK:STDOUT: %i.param: %i32 = value_param runtime_param1
  82. // CHECK:STDOUT: %.loc11_24: type = splice_block %i32.loc11_24 [template = constants.%i32] {
  83. // CHECK:STDOUT: %int_32.loc11_24: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  84. // CHECK:STDOUT: %i32.loc11_24: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  85. // CHECK:STDOUT: }
  86. // CHECK:STDOUT: %i: %i32 = bind_name i, %i.param
  87. // CHECK:STDOUT: %return.param: ref %i32 = out_param runtime_param2
  88. // CHECK:STDOUT: %return: ref %i32 = return_slot %return.param
  89. // CHECK:STDOUT: }
  90. // CHECK:STDOUT: %G.decl: %G.type = fn_decl @G [template = constants.%G] {
  91. // CHECK:STDOUT: %return.patt: %i32 = return_slot_pattern
  92. // CHECK:STDOUT: %return.param_patt: %i32 = out_param_pattern %return.patt, runtime_param0
  93. // CHECK:STDOUT: } {
  94. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  95. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  96. // CHECK:STDOUT: %return.param: ref %i32 = out_param runtime_param0
  97. // CHECK:STDOUT: %return: ref %i32 = return_slot %return.param
  98. // CHECK:STDOUT: }
  99. // CHECK:STDOUT: }
  100. // CHECK:STDOUT:
  101. // CHECK:STDOUT: fn @F(%arr.param_patt: %array_type, %i.param_patt: %i32) -> %i32 {
  102. // CHECK:STDOUT: !entry:
  103. // CHECK:STDOUT: %arr.ref: %array_type = name_ref arr, %arr
  104. // CHECK:STDOUT: %i.ref: %i32 = name_ref i, %i
  105. // CHECK:STDOUT: %int_32.loc12: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  106. // CHECK:STDOUT: %i32.loc12: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  107. // CHECK:STDOUT: %.loc12_15.1: ref %array_type = value_as_ref %arr.ref
  108. // CHECK:STDOUT: %.loc12_15.2: ref %i32 = array_index %.loc12_15.1, %i.ref
  109. // CHECK:STDOUT: %.loc12_15.3: %i32 = bind_value %.loc12_15.2
  110. // CHECK:STDOUT: return %.loc12_15.3
  111. // CHECK:STDOUT: }
  112. // CHECK:STDOUT:
  113. // CHECK:STDOUT: fn @G() -> %i32 {
  114. // CHECK:STDOUT: !entry:
  115. // CHECK:STDOUT: %F.ref: %F.type = name_ref F, file.%F.decl [template = constants.%F]
  116. // CHECK:STDOUT: %int_1.loc16_13: Core.IntLiteral = int_value 1 [template = constants.%int_1.5b8]
  117. // CHECK:STDOUT: %int_2.loc16_16: Core.IntLiteral = int_value 2 [template = constants.%int_2.ecc]
  118. // CHECK:STDOUT: %int_3: Core.IntLiteral = int_value 3 [template = constants.%int_3.1ba]
  119. // CHECK:STDOUT: %.loc16_20.1: %tuple.type = tuple_literal (%int_1.loc16_13, %int_2.loc16_16, %int_3)
  120. // CHECK:STDOUT: %int_1.loc16_23: Core.IntLiteral = int_value 1 [template = constants.%int_1.5b8]
  121. // CHECK:STDOUT: %impl.elem0.loc16_20.1: %Convert.type.1b6 = impl_witness_access constants.%impl_witness.d39, element0 [template = constants.%Convert.956]
  122. // CHECK:STDOUT: %Convert.bound.loc16_20.1: <bound method> = bound_method %int_1.loc16_13, %impl.elem0.loc16_20.1 [template = constants.%Convert.bound.ab5]
  123. // CHECK:STDOUT: %Convert.specific_fn.loc16_20.1: <specific function> = specific_function %Convert.bound.loc16_20.1, @Convert.2(constants.%int_32) [template = constants.%Convert.specific_fn.70c]
  124. // CHECK:STDOUT: %int.convert_checked.loc16_20.1: init %i32 = call %Convert.specific_fn.loc16_20.1(%int_1.loc16_13) [template = constants.%int_1.5d2]
  125. // CHECK:STDOUT: %.loc16_20.2: init %i32 = converted %int_1.loc16_13, %int.convert_checked.loc16_20.1 [template = constants.%int_1.5d2]
  126. // CHECK:STDOUT: %.loc16_20.3: ref %array_type = temporary_storage
  127. // CHECK:STDOUT: %int_0: Core.IntLiteral = int_value 0 [template = constants.%int_0]
  128. // CHECK:STDOUT: %.loc16_20.4: ref %i32 = array_index %.loc16_20.3, %int_0
  129. // CHECK:STDOUT: %.loc16_20.5: init %i32 = initialize_from %.loc16_20.2 to %.loc16_20.4 [template = constants.%int_1.5d2]
  130. // CHECK:STDOUT: %impl.elem0.loc16_20.2: %Convert.type.1b6 = impl_witness_access constants.%impl_witness.d39, element0 [template = constants.%Convert.956]
  131. // CHECK:STDOUT: %Convert.bound.loc16_20.2: <bound method> = bound_method %int_2.loc16_16, %impl.elem0.loc16_20.2 [template = constants.%Convert.bound.ef9]
  132. // CHECK:STDOUT: %Convert.specific_fn.loc16_20.2: <specific function> = specific_function %Convert.bound.loc16_20.2, @Convert.2(constants.%int_32) [template = constants.%Convert.specific_fn.787]
  133. // CHECK:STDOUT: %int.convert_checked.loc16_20.2: init %i32 = call %Convert.specific_fn.loc16_20.2(%int_2.loc16_16) [template = constants.%int_2.ef8]
  134. // CHECK:STDOUT: %.loc16_20.6: init %i32 = converted %int_2.loc16_16, %int.convert_checked.loc16_20.2 [template = constants.%int_2.ef8]
  135. // CHECK:STDOUT: %int_1.loc16_20: Core.IntLiteral = int_value 1 [template = constants.%int_1.5b8]
  136. // CHECK:STDOUT: %.loc16_20.7: ref %i32 = array_index %.loc16_20.3, %int_1.loc16_20
  137. // CHECK:STDOUT: %.loc16_20.8: init %i32 = initialize_from %.loc16_20.6 to %.loc16_20.7 [template = constants.%int_2.ef8]
  138. // CHECK:STDOUT: %impl.elem0.loc16_20.3: %Convert.type.1b6 = impl_witness_access constants.%impl_witness.d39, element0 [template = constants.%Convert.956]
  139. // CHECK:STDOUT: %Convert.bound.loc16_20.3: <bound method> = bound_method %int_3, %impl.elem0.loc16_20.3 [template = constants.%Convert.bound.b30]
  140. // CHECK:STDOUT: %Convert.specific_fn.loc16_20.3: <specific function> = specific_function %Convert.bound.loc16_20.3, @Convert.2(constants.%int_32) [template = constants.%Convert.specific_fn.b42]
  141. // CHECK:STDOUT: %int.convert_checked.loc16_20.3: init %i32 = call %Convert.specific_fn.loc16_20.3(%int_3) [template = constants.%int_3.822]
  142. // CHECK:STDOUT: %.loc16_20.9: init %i32 = converted %int_3, %int.convert_checked.loc16_20.3 [template = constants.%int_3.822]
  143. // CHECK:STDOUT: %int_2.loc16_20: Core.IntLiteral = int_value 2 [template = constants.%int_2.ecc]
  144. // CHECK:STDOUT: %.loc16_20.10: ref %i32 = array_index %.loc16_20.3, %int_2.loc16_20
  145. // CHECK:STDOUT: %.loc16_20.11: init %i32 = initialize_from %.loc16_20.9 to %.loc16_20.10 [template = constants.%int_3.822]
  146. // CHECK:STDOUT: %.loc16_20.12: init %array_type = array_init (%.loc16_20.5, %.loc16_20.8, %.loc16_20.11) to %.loc16_20.3 [template = constants.%array]
  147. // CHECK:STDOUT: %.loc16_20.13: init %array_type = converted %.loc16_20.1, %.loc16_20.12 [template = constants.%array]
  148. // CHECK:STDOUT: %.loc16_20.14: ref %array_type = temporary %.loc16_20.3, %.loc16_20.13
  149. // CHECK:STDOUT: %.loc16_20.15: %array_type = bind_value %.loc16_20.14
  150. // CHECK:STDOUT: %impl.elem0.loc16_23: %Convert.type.1b6 = impl_witness_access constants.%impl_witness.d39, element0 [template = constants.%Convert.956]
  151. // CHECK:STDOUT: %Convert.bound.loc16_23: <bound method> = bound_method %int_1.loc16_23, %impl.elem0.loc16_23 [template = constants.%Convert.bound.ab5]
  152. // CHECK:STDOUT: %Convert.specific_fn.loc16_23: <specific function> = specific_function %Convert.bound.loc16_23, @Convert.2(constants.%int_32) [template = constants.%Convert.specific_fn.70c]
  153. // CHECK:STDOUT: %int.convert_checked.loc16_23: init %i32 = call %Convert.specific_fn.loc16_23(%int_1.loc16_23) [template = constants.%int_1.5d2]
  154. // CHECK:STDOUT: %.loc16_23.1: %i32 = value_of_initializer %int.convert_checked.loc16_23 [template = constants.%int_1.5d2]
  155. // CHECK:STDOUT: %.loc16_23.2: %i32 = converted %int_1.loc16_23, %.loc16_23.1 [template = constants.%int_1.5d2]
  156. // CHECK:STDOUT: %F.call: init %i32 = call %F.ref(%.loc16_20.15, %.loc16_23.2)
  157. // CHECK:STDOUT: %.loc16_25.1: %i32 = value_of_initializer %F.call
  158. // CHECK:STDOUT: %.loc16_25.2: %i32 = converted %F.call, %.loc16_25.1
  159. // CHECK:STDOUT: return %.loc16_25.2
  160. // CHECK:STDOUT: }
  161. // CHECK:STDOUT: