method.carbon 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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/function/builtin/method.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/function/builtin/method.carbon
  10. interface I {
  11. fn F[self: Self](other: Self) -> Self;
  12. }
  13. impl i32 as I {
  14. fn F[self: i32](other: i32) -> i32 = "int.sadd";
  15. }
  16. var arr: [i32; (1 as i32).(I.F)(2)];
  17. // CHECK:STDOUT: --- method.carbon
  18. // CHECK:STDOUT:
  19. // CHECK:STDOUT: constants {
  20. // CHECK:STDOUT: %I.type: type = facet_type <@I> [template]
  21. // CHECK:STDOUT: %Self.1: %I.type = bind_symbolic_name Self, 0 [symbolic]
  22. // CHECK:STDOUT: %Self.as_type.1: type = facet_access_type %Self.1 [symbolic]
  23. // CHECK:STDOUT: %F.type.1: type = fn_type @F.1 [template]
  24. // CHECK:STDOUT: %F.1: %F.type.1 = struct_value () [template]
  25. // CHECK:STDOUT: %F.assoc_type: type = assoc_entity_type %I.type, %F.type.1 [template]
  26. // CHECK:STDOUT: %assoc0.1: %F.assoc_type = assoc_entity element0, @I.%F.decl [template]
  27. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [template]
  28. // CHECK:STDOUT: %Int.type: type = fn_type @Int [template]
  29. // CHECK:STDOUT: %Int: %Int.type = struct_value () [template]
  30. // CHECK:STDOUT: %i32: type = int_type signed, %int_32 [template]
  31. // CHECK:STDOUT: %F.type.2: type = fn_type @F.2 [template]
  32. // CHECK:STDOUT: %F.2: %F.type.2 = struct_value () [template]
  33. // CHECK:STDOUT: %I.facet: %I.type = facet_value %i32, %i32 [template]
  34. // CHECK:STDOUT: %interface.1: <witness> = interface_witness (%F.2) [template]
  35. // CHECK:STDOUT: %int_1.1: Core.IntLiteral = int_value 1 [template]
  36. // CHECK:STDOUT: %Convert.type.2: type = fn_type @Convert.1, @As(%i32) [template]
  37. // CHECK:STDOUT: %Convert.type.7: type = fn_type @Convert.2, @ImplicitAs(Core.IntLiteral) [template]
  38. // CHECK:STDOUT: %Convert.type.14: type = fn_type @Convert.7, @impl.6(%int_32) [template]
  39. // CHECK:STDOUT: %Convert.14: %Convert.type.14 = struct_value () [template]
  40. // CHECK:STDOUT: %interface.10: <witness> = interface_witness (%Convert.14) [template]
  41. // CHECK:STDOUT: %Convert.bound.1: <bound method> = bound_method %int_1.1, %Convert.14 [template]
  42. // CHECK:STDOUT: %Convert.specific_fn.1: <specific function> = specific_function %Convert.bound.1, @Convert.7(%int_32) [template]
  43. // CHECK:STDOUT: %int_1.2: %i32 = int_value 1 [template]
  44. // CHECK:STDOUT: %F.bound: <bound method> = bound_method %int_1.2, %F.2 [template]
  45. // CHECK:STDOUT: %int_2.1: Core.IntLiteral = int_value 2 [template]
  46. // CHECK:STDOUT: %Convert.type.15: type = fn_type @Convert.2, @ImplicitAs(%i32) [template]
  47. // CHECK:STDOUT: %Convert.type.16: type = fn_type @Convert.3, @impl.2(%int_32) [template]
  48. // CHECK:STDOUT: %Convert.16: %Convert.type.16 = struct_value () [template]
  49. // CHECK:STDOUT: %interface.11: <witness> = interface_witness (%Convert.16) [template]
  50. // CHECK:STDOUT: %Convert.bound.2: <bound method> = bound_method %int_2.1, %Convert.16 [template]
  51. // CHECK:STDOUT: %Convert.specific_fn.2: <specific function> = specific_function %Convert.bound.2, @Convert.3(%int_32) [template]
  52. // CHECK:STDOUT: %int_2.2: %i32 = int_value 2 [template]
  53. // CHECK:STDOUT: %int_3.1: %i32 = int_value 3 [template]
  54. // CHECK:STDOUT: %Convert.type.17: type = fn_type @Convert.5, @impl.4(%int_32) [template]
  55. // CHECK:STDOUT: %Convert.17: %Convert.type.17 = struct_value () [template]
  56. // CHECK:STDOUT: %interface.12: <witness> = interface_witness (%Convert.17) [template]
  57. // CHECK:STDOUT: %Convert.bound.3: <bound method> = bound_method %int_3.1, %Convert.17 [template]
  58. // CHECK:STDOUT: %Convert.specific_fn.3: <specific function> = specific_function %Convert.bound.3, @Convert.5(%int_32) [template]
  59. // CHECK:STDOUT: %int_3.2: Core.IntLiteral = int_value 3 [template]
  60. // CHECK:STDOUT: %array_type: type = array_type %int_3.2, %i32 [template]
  61. // CHECK:STDOUT: }
  62. // CHECK:STDOUT:
  63. // CHECK:STDOUT: imports {
  64. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  65. // CHECK:STDOUT: .Int = %import_ref.1
  66. // CHECK:STDOUT: .As = %import_ref.2
  67. // CHECK:STDOUT: .ImplicitAs = %import_ref.38
  68. // CHECK:STDOUT: import Core//prelude
  69. // CHECK:STDOUT: import Core//prelude/...
  70. // CHECK:STDOUT: }
  71. // CHECK:STDOUT: }
  72. // CHECK:STDOUT:
  73. // CHECK:STDOUT: file {
  74. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  75. // CHECK:STDOUT: .Core = imports.%Core
  76. // CHECK:STDOUT: .I = %I.decl
  77. // CHECK:STDOUT: .arr = %arr
  78. // CHECK:STDOUT: }
  79. // CHECK:STDOUT: %Core.import = import Core
  80. // CHECK:STDOUT: %I.decl: type = interface_decl @I [template = constants.%I.type] {} {}
  81. // CHECK:STDOUT: impl_decl @impl.1 [template] {} {
  82. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  83. // CHECK:STDOUT: %int.make_type_signed: init type = call constants.%Int(%int_32) [template = constants.%i32]
  84. // CHECK:STDOUT: %.loc15_6.1: type = value_of_initializer %int.make_type_signed [template = constants.%i32]
  85. // CHECK:STDOUT: %.loc15_6.2: type = converted %int.make_type_signed, %.loc15_6.1 [template = constants.%i32]
  86. // CHECK:STDOUT: %I.ref: type = name_ref I, file.%I.decl [template = constants.%I.type]
  87. // CHECK:STDOUT: }
  88. // CHECK:STDOUT: %int_32.loc19_11: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  89. // CHECK:STDOUT: %int.make_type_signed.loc19_11: init type = call constants.%Int(%int_32.loc19_11) [template = constants.%i32]
  90. // CHECK:STDOUT: %int_1: Core.IntLiteral = int_value 1 [template = constants.%int_1.1]
  91. // CHECK:STDOUT: %int_32.loc19_22: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  92. // CHECK:STDOUT: %int.make_type_signed.loc19_22: init type = call constants.%Int(%int_32.loc19_22) [template = constants.%i32]
  93. // CHECK:STDOUT: %.loc19_22.1: type = value_of_initializer %int.make_type_signed.loc19_22 [template = constants.%i32]
  94. // CHECK:STDOUT: %.loc19_22.2: type = converted %int.make_type_signed.loc19_22, %.loc19_22.1 [template = constants.%i32]
  95. // CHECK:STDOUT: %impl.elem0.loc19_19: %Convert.type.2 = interface_witness_access constants.%interface.10, element0 [template = constants.%Convert.14]
  96. // CHECK:STDOUT: %Convert.bound.loc19_19: <bound method> = bound_method %int_1, %impl.elem0.loc19_19 [template = constants.%Convert.bound.1]
  97. // CHECK:STDOUT: %Convert.specific_fn.loc19_19: <specific function> = specific_function %Convert.bound.loc19_19, @Convert.7(constants.%int_32) [template = constants.%Convert.specific_fn.1]
  98. // CHECK:STDOUT: %int.convert_checked.loc19_19: init %i32 = call %Convert.specific_fn.loc19_19(%int_1) [template = constants.%int_1.2]
  99. // CHECK:STDOUT: %.loc19_19.1: %i32 = value_of_initializer %int.convert_checked.loc19_19 [template = constants.%int_1.2]
  100. // CHECK:STDOUT: %.loc19_19.2: %i32 = converted %int_1, %.loc19_19.1 [template = constants.%int_1.2]
  101. // CHECK:STDOUT: %I.ref: type = name_ref I, %I.decl [template = constants.%I.type]
  102. // CHECK:STDOUT: %F.ref: %F.assoc_type = name_ref F, @I.%assoc0 [template = constants.%assoc0.1]
  103. // CHECK:STDOUT: %impl.elem0.loc19_26: %F.type.1 = interface_witness_access constants.%interface.1, element0 [template = constants.%F.2]
  104. // CHECK:STDOUT: %F.bound: <bound method> = bound_method %.loc19_19.2, %impl.elem0.loc19_26 [template = constants.%F.bound]
  105. // CHECK:STDOUT: %int_2: Core.IntLiteral = int_value 2 [template = constants.%int_2.1]
  106. // CHECK:STDOUT: %impl.elem0.loc19_33: %Convert.type.15 = interface_witness_access constants.%interface.11, element0 [template = constants.%Convert.16]
  107. // CHECK:STDOUT: %Convert.bound.loc19_33: <bound method> = bound_method %int_2, %impl.elem0.loc19_33 [template = constants.%Convert.bound.2]
  108. // CHECK:STDOUT: %Convert.specific_fn.loc19_33: <specific function> = specific_function %Convert.bound.loc19_33, @Convert.3(constants.%int_32) [template = constants.%Convert.specific_fn.2]
  109. // CHECK:STDOUT: %int.convert_checked.loc19_33: init %i32 = call %Convert.specific_fn.loc19_33(%int_2) [template = constants.%int_2.2]
  110. // CHECK:STDOUT: %.loc19_33.1: %i32 = value_of_initializer %int.convert_checked.loc19_33 [template = constants.%int_2.2]
  111. // CHECK:STDOUT: %.loc19_33.2: %i32 = converted %int_2, %.loc19_33.1 [template = constants.%int_2.2]
  112. // CHECK:STDOUT: %int.sadd: init %i32 = call %F.bound(%.loc19_19.2, %.loc19_33.2) [template = constants.%int_3.1]
  113. // CHECK:STDOUT: %.loc19_11.1: type = value_of_initializer %int.make_type_signed.loc19_11 [template = constants.%i32]
  114. // CHECK:STDOUT: %.loc19_11.2: type = converted %int.make_type_signed.loc19_11, %.loc19_11.1 [template = constants.%i32]
  115. // CHECK:STDOUT: %impl.elem0.loc19_34: %Convert.type.7 = interface_witness_access constants.%interface.12, element0 [template = constants.%Convert.17]
  116. // CHECK:STDOUT: %Convert.bound.loc19_34: <bound method> = bound_method %int.sadd, %impl.elem0.loc19_34 [template = constants.%Convert.bound.3]
  117. // CHECK:STDOUT: %Convert.specific_fn.loc19_34: <specific function> = specific_function %Convert.bound.loc19_34, @Convert.5(constants.%int_32) [template = constants.%Convert.specific_fn.3]
  118. // CHECK:STDOUT: %.loc19_34.1: %i32 = value_of_initializer %int.sadd [template = constants.%int_3.1]
  119. // CHECK:STDOUT: %.loc19_34.2: %i32 = converted %int.sadd, %.loc19_34.1 [template = constants.%int_3.1]
  120. // CHECK:STDOUT: %int.convert_checked.loc19_34: init Core.IntLiteral = call %Convert.specific_fn.loc19_34(%.loc19_34.2) [template = constants.%int_3.2]
  121. // CHECK:STDOUT: %.loc19_34.3: Core.IntLiteral = value_of_initializer %int.convert_checked.loc19_34 [template = constants.%int_3.2]
  122. // CHECK:STDOUT: %.loc19_34.4: Core.IntLiteral = converted %int.sadd, %.loc19_34.3 [template = constants.%int_3.2]
  123. // CHECK:STDOUT: %array_type: type = array_type %.loc19_34.4, %i32 [template = constants.%array_type]
  124. // CHECK:STDOUT: %arr.var: ref %array_type = var arr
  125. // CHECK:STDOUT: %arr: ref %array_type = bind_name arr, %arr.var
  126. // CHECK:STDOUT: }
  127. // CHECK:STDOUT:
  128. // CHECK:STDOUT: interface @I {
  129. // CHECK:STDOUT: %Self: %I.type = bind_symbolic_name Self, 0 [symbolic = constants.%Self.1]
  130. // CHECK:STDOUT: %F.decl: %F.type.1 = fn_decl @F.1 [template = constants.%F.1] {
  131. // CHECK:STDOUT: %self.patt: @F.1.%Self.as_type.loc12_14.1 (%Self.as_type.1) = binding_pattern self
  132. // CHECK:STDOUT: %self.param_patt: @F.1.%Self.as_type.loc12_14.1 (%Self.as_type.1) = value_param_pattern %self.patt, runtime_param0
  133. // CHECK:STDOUT: %other.patt: @F.1.%Self.as_type.loc12_14.1 (%Self.as_type.1) = binding_pattern other
  134. // CHECK:STDOUT: %other.param_patt: @F.1.%Self.as_type.loc12_14.1 (%Self.as_type.1) = value_param_pattern %other.patt, runtime_param1
  135. // CHECK:STDOUT: %return.patt: @F.1.%Self.as_type.loc12_14.1 (%Self.as_type.1) = return_slot_pattern
  136. // CHECK:STDOUT: %return.param_patt: @F.1.%Self.as_type.loc12_14.1 (%Self.as_type.1) = out_param_pattern %return.patt, runtime_param2
  137. // CHECK:STDOUT: } {
  138. // CHECK:STDOUT: %Self.ref.loc12_14: %I.type = name_ref Self, @I.%Self [symbolic = %Self (constants.%Self.1)]
  139. // CHECK:STDOUT: %Self.as_type.loc12_14.2: type = facet_access_type %Self.ref.loc12_14 [symbolic = %Self.as_type.loc12_14.1 (constants.%Self.as_type.1)]
  140. // CHECK:STDOUT: %.loc12_14: type = converted %Self.ref.loc12_14, %Self.as_type.loc12_14.2 [symbolic = %Self.as_type.loc12_14.1 (constants.%Self.as_type.1)]
  141. // CHECK:STDOUT: %Self.ref.loc12_27: %I.type = name_ref Self, @I.%Self [symbolic = %Self (constants.%Self.1)]
  142. // CHECK:STDOUT: %Self.as_type.loc12_27: type = facet_access_type %Self.ref.loc12_27 [symbolic = %Self.as_type.loc12_14.1 (constants.%Self.as_type.1)]
  143. // CHECK:STDOUT: %.loc12_27: type = converted %Self.ref.loc12_27, %Self.as_type.loc12_27 [symbolic = %Self.as_type.loc12_14.1 (constants.%Self.as_type.1)]
  144. // CHECK:STDOUT: %Self.ref.loc12_36: %I.type = name_ref Self, @I.%Self [symbolic = %Self (constants.%Self.1)]
  145. // CHECK:STDOUT: %Self.as_type.loc12_36: type = facet_access_type %Self.ref.loc12_36 [symbolic = %Self.as_type.loc12_14.1 (constants.%Self.as_type.1)]
  146. // CHECK:STDOUT: %.loc12_36: type = converted %Self.ref.loc12_36, %Self.as_type.loc12_36 [symbolic = %Self.as_type.loc12_14.1 (constants.%Self.as_type.1)]
  147. // CHECK:STDOUT: %self.param: @F.1.%Self.as_type.loc12_14.1 (%Self.as_type.1) = value_param runtime_param0
  148. // CHECK:STDOUT: %self: @F.1.%Self.as_type.loc12_14.1 (%Self.as_type.1) = bind_name self, %self.param
  149. // CHECK:STDOUT: %other.param: @F.1.%Self.as_type.loc12_14.1 (%Self.as_type.1) = value_param runtime_param1
  150. // CHECK:STDOUT: %other: @F.1.%Self.as_type.loc12_14.1 (%Self.as_type.1) = bind_name other, %other.param
  151. // CHECK:STDOUT: %return.param: ref @F.1.%Self.as_type.loc12_14.1 (%Self.as_type.1) = out_param runtime_param2
  152. // CHECK:STDOUT: %return: ref @F.1.%Self.as_type.loc12_14.1 (%Self.as_type.1) = return_slot %return.param
  153. // CHECK:STDOUT: }
  154. // CHECK:STDOUT: %assoc0: %F.assoc_type = assoc_entity element0, %F.decl [template = constants.%assoc0.1]
  155. // CHECK:STDOUT:
  156. // CHECK:STDOUT: !members:
  157. // CHECK:STDOUT: .Self = %Self
  158. // CHECK:STDOUT: .F = %assoc0
  159. // CHECK:STDOUT: witness = (%F.decl)
  160. // CHECK:STDOUT: }
  161. // CHECK:STDOUT:
  162. // CHECK:STDOUT: impl @impl.1: %.loc15_6.2 as %I.ref {
  163. // CHECK:STDOUT: %F.decl: %F.type.2 = fn_decl @F.2 [template = constants.%F.2] {
  164. // CHECK:STDOUT: %self.patt: %i32 = binding_pattern self
  165. // CHECK:STDOUT: %self.param_patt: %i32 = value_param_pattern %self.patt, runtime_param0
  166. // CHECK:STDOUT: %other.patt: %i32 = binding_pattern other
  167. // CHECK:STDOUT: %other.param_patt: %i32 = value_param_pattern %other.patt, runtime_param1
  168. // CHECK:STDOUT: %return.patt: %i32 = return_slot_pattern
  169. // CHECK:STDOUT: %return.param_patt: %i32 = out_param_pattern %return.patt, runtime_param2
  170. // CHECK:STDOUT: } {
  171. // CHECK:STDOUT: %int_32.loc16_14: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  172. // CHECK:STDOUT: %int.make_type_signed.loc16_14: init type = call constants.%Int(%int_32.loc16_14) [template = constants.%i32]
  173. // CHECK:STDOUT: %.loc16_14.1: type = value_of_initializer %int.make_type_signed.loc16_14 [template = constants.%i32]
  174. // CHECK:STDOUT: %.loc16_14.2: type = converted %int.make_type_signed.loc16_14, %.loc16_14.1 [template = constants.%i32]
  175. // CHECK:STDOUT: %int_32.loc16_26: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  176. // CHECK:STDOUT: %int.make_type_signed.loc16_26: init type = call constants.%Int(%int_32.loc16_26) [template = constants.%i32]
  177. // CHECK:STDOUT: %.loc16_26.1: type = value_of_initializer %int.make_type_signed.loc16_26 [template = constants.%i32]
  178. // CHECK:STDOUT: %.loc16_26.2: type = converted %int.make_type_signed.loc16_26, %.loc16_26.1 [template = constants.%i32]
  179. // CHECK:STDOUT: %int_32.loc16_34: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  180. // CHECK:STDOUT: %int.make_type_signed.loc16_34: init type = call constants.%Int(%int_32.loc16_34) [template = constants.%i32]
  181. // CHECK:STDOUT: %.loc16_34.1: type = value_of_initializer %int.make_type_signed.loc16_34 [template = constants.%i32]
  182. // CHECK:STDOUT: %.loc16_34.2: type = converted %int.make_type_signed.loc16_34, %.loc16_34.1 [template = constants.%i32]
  183. // CHECK:STDOUT: %self.param: %i32 = value_param runtime_param0
  184. // CHECK:STDOUT: %self: %i32 = bind_name self, %self.param
  185. // CHECK:STDOUT: %other.param: %i32 = value_param runtime_param1
  186. // CHECK:STDOUT: %other: %i32 = bind_name other, %other.param
  187. // CHECK:STDOUT: %return.param: ref %i32 = out_param runtime_param2
  188. // CHECK:STDOUT: %return: ref %i32 = return_slot %return.param
  189. // CHECK:STDOUT: }
  190. // CHECK:STDOUT: %interface: <witness> = interface_witness (%F.decl) [template = constants.%interface.1]
  191. // CHECK:STDOUT:
  192. // CHECK:STDOUT: !members:
  193. // CHECK:STDOUT: .F = %F.decl
  194. // CHECK:STDOUT: witness = %interface
  195. // CHECK:STDOUT: }
  196. // CHECK:STDOUT:
  197. // CHECK:STDOUT: generic fn @F.1(@I.%Self: %I.type) {
  198. // CHECK:STDOUT: %Self: %I.type = bind_symbolic_name Self, 0 [symbolic = %Self (constants.%Self.1)]
  199. // CHECK:STDOUT: %Self.as_type.loc12_14.1: type = facet_access_type %Self [symbolic = %Self.as_type.loc12_14.1 (constants.%Self.as_type.1)]
  200. // CHECK:STDOUT:
  201. // CHECK:STDOUT: fn[%self.param_patt: @F.1.%Self.as_type.loc12_14.1 (%Self.as_type.1)](%other.param_patt: @F.1.%Self.as_type.loc12_14.1 (%Self.as_type.1)) -> @F.1.%Self.as_type.loc12_14.1 (%Self.as_type.1);
  202. // CHECK:STDOUT: }
  203. // CHECK:STDOUT:
  204. // CHECK:STDOUT: fn @F.2[%self.param_patt: %i32](%other.param_patt: %i32) -> %i32 = "int.sadd";
  205. // CHECK:STDOUT:
  206. // CHECK:STDOUT: specific @F.1(constants.%Self.1) {
  207. // CHECK:STDOUT: %Self => constants.%Self.1
  208. // CHECK:STDOUT: %Self.as_type.loc12_14.1 => constants.%Self.as_type.1
  209. // CHECK:STDOUT: }
  210. // CHECK:STDOUT:
  211. // CHECK:STDOUT: specific @F.1(constants.%I.facet) {
  212. // CHECK:STDOUT: %Self => constants.%I.facet
  213. // CHECK:STDOUT: %Self.as_type.loc12_14.1 => constants.%i32
  214. // CHECK:STDOUT: }
  215. // CHECK:STDOUT: