method.carbon 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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.(I.F)(2)];
  17. // CHECK:STDOUT: --- method.carbon
  18. // CHECK:STDOUT:
  19. // CHECK:STDOUT: constants {
  20. // CHECK:STDOUT: %.1: type = interface_type @I [template]
  21. // CHECK:STDOUT: %Self: %.1 = bind_symbolic_name Self, 0 [symbolic]
  22. // CHECK:STDOUT: %F.type.1: type = fn_type @F.1 [template]
  23. // CHECK:STDOUT: %.2: type = tuple_type () [template]
  24. // CHECK:STDOUT: %F.1: %F.type.1 = struct_value () [template]
  25. // CHECK:STDOUT: %.3: type = assoc_entity_type %.1, %F.type.1 [template]
  26. // CHECK:STDOUT: %.4: %.3 = assoc_entity element0, @I.%F.decl [template]
  27. // CHECK:STDOUT: %Int32.type: type = fn_type @Int32 [template]
  28. // CHECK:STDOUT: %Int32: %Int32.type = struct_value () [template]
  29. // CHECK:STDOUT: %F.type.2: type = fn_type @F.2 [template]
  30. // CHECK:STDOUT: %F.2: %F.type.2 = struct_value () [template]
  31. // CHECK:STDOUT: %.5: <witness> = interface_witness (%F.2) [template]
  32. // CHECK:STDOUT: %.6: i32 = int_literal 1 [template]
  33. // CHECK:STDOUT: %.7: <bound method> = bound_method %.6, %F.2 [template]
  34. // CHECK:STDOUT: %.8: i32 = int_literal 2 [template]
  35. // CHECK:STDOUT: %.9: i32 = int_literal 3 [template]
  36. // CHECK:STDOUT: %.10: type = array_type %.9, i32 [template]
  37. // CHECK:STDOUT: %.11: type = ptr_type %.10 [template]
  38. // CHECK:STDOUT: }
  39. // CHECK:STDOUT:
  40. // CHECK:STDOUT: imports {
  41. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  42. // CHECK:STDOUT: .Int32 = %import_ref
  43. // CHECK:STDOUT: import Core//prelude
  44. // CHECK:STDOUT: import Core//prelude/operators
  45. // CHECK:STDOUT: import Core//prelude/types
  46. // CHECK:STDOUT: import Core//prelude/operators/arithmetic
  47. // CHECK:STDOUT: import Core//prelude/operators/as
  48. // CHECK:STDOUT: import Core//prelude/operators/bitwise
  49. // CHECK:STDOUT: import Core//prelude/operators/comparison
  50. // CHECK:STDOUT: import Core//prelude/types/bool
  51. // CHECK:STDOUT: }
  52. // CHECK:STDOUT: %import_ref: %Int32.type = import_ref Core//prelude/types, inst+4, loaded [template = constants.%Int32]
  53. // CHECK:STDOUT: }
  54. // CHECK:STDOUT:
  55. // CHECK:STDOUT: file {
  56. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  57. // CHECK:STDOUT: .Core = imports.%Core
  58. // CHECK:STDOUT: .I = %I.decl
  59. // CHECK:STDOUT: .arr = %arr
  60. // CHECK:STDOUT: }
  61. // CHECK:STDOUT: %Core.import = import Core
  62. // CHECK:STDOUT: %I.decl: type = interface_decl @I [template = constants.%.1] {} {}
  63. // CHECK:STDOUT: impl_decl @impl [template] {} {
  64. // CHECK:STDOUT: %int.make_type_32: init type = call constants.%Int32() [template = i32]
  65. // CHECK:STDOUT: %.loc15_6.1: type = value_of_initializer %int.make_type_32 [template = i32]
  66. // CHECK:STDOUT: %.loc15_6.2: type = converted %int.make_type_32, %.loc15_6.1 [template = i32]
  67. // CHECK:STDOUT: %I.ref: type = name_ref I, file.%I.decl [template = constants.%.1]
  68. // CHECK:STDOUT: }
  69. // CHECK:STDOUT: %int.make_type_32: init type = call constants.%Int32() [template = i32]
  70. // CHECK:STDOUT: %.loc19_16: i32 = int_literal 1 [template = constants.%.6]
  71. // CHECK:STDOUT: %I.ref: type = name_ref I, %I.decl [template = constants.%.1]
  72. // CHECK:STDOUT: %F.ref: %.3 = name_ref F, @I.%.loc12 [template = constants.%.4]
  73. // CHECK:STDOUT: %.loc19_17.1: %F.type.1 = interface_witness_access constants.%.5, element0 [template = constants.%F.2]
  74. // CHECK:STDOUT: %.loc19_17.2: <bound method> = bound_method %.loc19_16, %.loc19_17.1 [template = constants.%.7]
  75. // CHECK:STDOUT: %.loc19_24: i32 = int_literal 2 [template = constants.%.8]
  76. // CHECK:STDOUT: %int.sadd: init i32 = call %.loc19_17.2(%.loc19_16, %.loc19_24) [template = constants.%.9]
  77. // CHECK:STDOUT: %.loc19_11.1: type = value_of_initializer %int.make_type_32 [template = i32]
  78. // CHECK:STDOUT: %.loc19_11.2: type = converted %int.make_type_32, %.loc19_11.1 [template = i32]
  79. // CHECK:STDOUT: %.loc19_26: type = array_type %int.sadd, i32 [template = constants.%.10]
  80. // CHECK:STDOUT: %arr.var: ref %.10 = var arr
  81. // CHECK:STDOUT: %arr: ref %.10 = bind_name arr, %arr.var
  82. // CHECK:STDOUT: }
  83. // CHECK:STDOUT:
  84. // CHECK:STDOUT: interface @I {
  85. // CHECK:STDOUT: %Self: %.1 = bind_symbolic_name Self, 0 [symbolic = constants.%Self]
  86. // CHECK:STDOUT: %F.decl: %F.type.1 = fn_decl @F.1 [template = constants.%F.1] {
  87. // CHECK:STDOUT: %self.patt: @F.1.%Self (%Self) = binding_pattern self
  88. // CHECK:STDOUT: %other.patt: @F.1.%Self (%Self) = binding_pattern other
  89. // CHECK:STDOUT: } {
  90. // CHECK:STDOUT: %Self.ref.loc12_14: %.1 = name_ref Self, @I.%Self [symbolic = %Self (constants.%Self)]
  91. // CHECK:STDOUT: %.loc12_14.1: type = facet_type_access %Self.ref.loc12_14 [symbolic = %Self (constants.%Self)]
  92. // CHECK:STDOUT: %.loc12_14.2: type = converted %Self.ref.loc12_14, %.loc12_14.1 [symbolic = %Self (constants.%Self)]
  93. // CHECK:STDOUT: %self.param: @F.1.%Self (%Self) = param self, runtime_param0
  94. // CHECK:STDOUT: %self: @F.1.%Self (%Self) = bind_name self, %self.param
  95. // CHECK:STDOUT: %Self.ref.loc12_27: %.1 = name_ref Self, @I.%Self [symbolic = %Self (constants.%Self)]
  96. // CHECK:STDOUT: %.loc12_27.1: type = facet_type_access %Self.ref.loc12_27 [symbolic = %Self (constants.%Self)]
  97. // CHECK:STDOUT: %.loc12_27.2: type = converted %Self.ref.loc12_27, %.loc12_27.1 [symbolic = %Self (constants.%Self)]
  98. // CHECK:STDOUT: %other.param: @F.1.%Self (%Self) = param other, runtime_param1
  99. // CHECK:STDOUT: %other: @F.1.%Self (%Self) = bind_name other, %other.param
  100. // CHECK:STDOUT: %Self.ref.loc12_36: %.1 = name_ref Self, @I.%Self [symbolic = %Self (constants.%Self)]
  101. // CHECK:STDOUT: %.loc12_36.1: type = facet_type_access %Self.ref.loc12_36 [symbolic = %Self (constants.%Self)]
  102. // CHECK:STDOUT: %.loc12_36.2: type = converted %Self.ref.loc12_36, %.loc12_36.1 [symbolic = %Self (constants.%Self)]
  103. // CHECK:STDOUT: %return: ref @F.1.%Self (%Self) = var <return slot>
  104. // CHECK:STDOUT: }
  105. // CHECK:STDOUT: %.loc12: %.3 = assoc_entity element0, %F.decl [template = constants.%.4]
  106. // CHECK:STDOUT:
  107. // CHECK:STDOUT: !members:
  108. // CHECK:STDOUT: .Self = %Self
  109. // CHECK:STDOUT: .F = %.loc12
  110. // CHECK:STDOUT: witness = (%F.decl)
  111. // CHECK:STDOUT: }
  112. // CHECK:STDOUT:
  113. // CHECK:STDOUT: impl @impl: %.loc15_6.2 as %I.ref {
  114. // CHECK:STDOUT: %F.decl: %F.type.2 = fn_decl @F.2 [template = constants.%F.2] {
  115. // CHECK:STDOUT: %self.patt: i32 = binding_pattern self
  116. // CHECK:STDOUT: %other.patt: i32 = binding_pattern other
  117. // CHECK:STDOUT: } {
  118. // CHECK:STDOUT: %int.make_type_32.loc16_14: init type = call constants.%Int32() [template = i32]
  119. // CHECK:STDOUT: %.loc16_14.1: type = value_of_initializer %int.make_type_32.loc16_14 [template = i32]
  120. // CHECK:STDOUT: %.loc16_14.2: type = converted %int.make_type_32.loc16_14, %.loc16_14.1 [template = i32]
  121. // CHECK:STDOUT: %self.param: i32 = param self, runtime_param0
  122. // CHECK:STDOUT: %self: i32 = bind_name self, %self.param
  123. // CHECK:STDOUT: %int.make_type_32.loc16_26: init type = call constants.%Int32() [template = i32]
  124. // CHECK:STDOUT: %.loc16_26.1: type = value_of_initializer %int.make_type_32.loc16_26 [template = i32]
  125. // CHECK:STDOUT: %.loc16_26.2: type = converted %int.make_type_32.loc16_26, %.loc16_26.1 [template = i32]
  126. // CHECK:STDOUT: %other.param: i32 = param other, runtime_param1
  127. // CHECK:STDOUT: %other: i32 = bind_name other, %other.param
  128. // CHECK:STDOUT: %int.make_type_32.loc16_34: init type = call constants.%Int32() [template = i32]
  129. // CHECK:STDOUT: %.loc16_34.1: type = value_of_initializer %int.make_type_32.loc16_34 [template = i32]
  130. // CHECK:STDOUT: %.loc16_34.2: type = converted %int.make_type_32.loc16_34, %.loc16_34.1 [template = i32]
  131. // CHECK:STDOUT: %return: ref i32 = var <return slot>
  132. // CHECK:STDOUT: }
  133. // CHECK:STDOUT: %.loc15_15: <witness> = interface_witness (%F.decl) [template = constants.%.5]
  134. // CHECK:STDOUT:
  135. // CHECK:STDOUT: !members:
  136. // CHECK:STDOUT: .F = %F.decl
  137. // CHECK:STDOUT: witness = %.loc15_15
  138. // CHECK:STDOUT: }
  139. // CHECK:STDOUT:
  140. // CHECK:STDOUT: generic fn @F.1(@I.%Self: %.1) {
  141. // CHECK:STDOUT: %Self: %.1 = bind_symbolic_name Self, 0 [symbolic = %Self (constants.%Self)]
  142. // CHECK:STDOUT:
  143. // CHECK:STDOUT: fn[%self: @F.1.%Self (%Self)](%other: @F.1.%Self (%Self)) -> @F.1.%Self (%Self);
  144. // CHECK:STDOUT: }
  145. // CHECK:STDOUT:
  146. // CHECK:STDOUT: fn @Int32() -> type = "int.make_type_32";
  147. // CHECK:STDOUT:
  148. // CHECK:STDOUT: fn @F.2[%self: i32](%other: i32) -> i32 = "int.sadd";
  149. // CHECK:STDOUT:
  150. // CHECK:STDOUT: specific @F.1(constants.%Self) {
  151. // CHECK:STDOUT: %Self => constants.%Self
  152. // CHECK:STDOUT: }
  153. // CHECK:STDOUT:
  154. // CHECK:STDOUT: specific @F.1(i32) {
  155. // CHECK:STDOUT: %Self => i32
  156. // CHECK:STDOUT: }
  157. // CHECK:STDOUT: