method.carbon 13 KB

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