method.carbon 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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/bitwise
  48. // CHECK:STDOUT: import Core//prelude/operators/comparison
  49. // CHECK:STDOUT: import Core//prelude/types/bool
  50. // CHECK:STDOUT: }
  51. // CHECK:STDOUT: %import_ref: %Int32.type = import_ref Core//prelude/types, inst+4, loaded [template = constants.%Int32]
  52. // CHECK:STDOUT: }
  53. // CHECK:STDOUT:
  54. // CHECK:STDOUT: file {
  55. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  56. // CHECK:STDOUT: .Core = imports.%Core
  57. // CHECK:STDOUT: .I = %I.decl
  58. // CHECK:STDOUT: .arr = %arr
  59. // CHECK:STDOUT: }
  60. // CHECK:STDOUT: %Core.import = import Core
  61. // CHECK:STDOUT: %I.decl: type = interface_decl @I [template = constants.%.1] {}
  62. // CHECK:STDOUT: impl_decl @impl {
  63. // CHECK:STDOUT: %int.make_type_32.loc15: init type = call constants.%Int32() [template = i32]
  64. // CHECK:STDOUT: %.loc15_6.1: type = value_of_initializer %int.make_type_32.loc15 [template = i32]
  65. // CHECK:STDOUT: %.loc15_6.2: type = converted %int.make_type_32.loc15, %.loc15_6.1 [template = i32]
  66. // CHECK:STDOUT: %I.ref.loc15: type = name_ref I, %I.decl [template = constants.%.1]
  67. // CHECK:STDOUT: }
  68. // CHECK:STDOUT: %int.make_type_32.loc19: init type = call constants.%Int32() [template = i32]
  69. // CHECK:STDOUT: %.loc19_16: i32 = int_literal 1 [template = constants.%.6]
  70. // CHECK:STDOUT: %I.ref.loc19: type = name_ref I, %I.decl [template = constants.%.1]
  71. // CHECK:STDOUT: %F.ref: %.3 = name_ref F, @I.%.loc12_40 [template = constants.%.4]
  72. // CHECK:STDOUT: %.1: %F.type.1 = interface_witness_access @impl.%.1, element0 [template = constants.%F.2]
  73. // CHECK:STDOUT: %.loc19_17: <bound method> = bound_method %.loc19_16, %.1 [template = constants.%.7]
  74. // CHECK:STDOUT: %.loc19_24: i32 = int_literal 2 [template = constants.%.8]
  75. // CHECK:STDOUT: %int.sadd: init i32 = call %.loc19_17(%.loc19_16, %.loc19_24) [template = constants.%.9]
  76. // CHECK:STDOUT: %.loc19_11.1: type = value_of_initializer %int.make_type_32.loc19 [template = i32]
  77. // CHECK:STDOUT: %.loc19_11.2: type = converted %int.make_type_32.loc19, %.loc19_11.1 [template = i32]
  78. // CHECK:STDOUT: %.loc19_26: type = array_type %int.sadd, i32 [template = constants.%.10]
  79. // CHECK:STDOUT: %arr.var: ref %.10 = var arr
  80. // CHECK:STDOUT: %arr: ref %.10 = bind_name arr, %arr.var
  81. // CHECK:STDOUT: }
  82. // CHECK:STDOUT:
  83. // CHECK:STDOUT: interface @I {
  84. // CHECK:STDOUT: %Self: %.1 = bind_symbolic_name Self 0 [symbolic = constants.%Self]
  85. // CHECK:STDOUT: %F.decl: %F.type.1 = fn_decl @F.1 [template = constants.%F.1] {
  86. // CHECK:STDOUT: %Self.ref.loc12_14: %.1 = name_ref Self, %Self [symbolic = @F.1.%Self (constants.%Self)]
  87. // CHECK:STDOUT: %.loc12_14.1: type = facet_type_access %Self.ref.loc12_14 [symbolic = @F.1.%Self (constants.%Self)]
  88. // CHECK:STDOUT: %.loc12_14.2: type = converted %Self.ref.loc12_14, %.loc12_14.1 [symbolic = @F.1.%Self (constants.%Self)]
  89. // CHECK:STDOUT: %self.loc12_8.1: @F.1.%Self (%Self) = param self
  90. // CHECK:STDOUT: %self.loc12_8.2: @F.1.%Self (%Self) = bind_name self, %self.loc12_8.1
  91. // CHECK:STDOUT: %Self.ref.loc12_27: %.1 = name_ref Self, %Self [symbolic = @F.1.%Self (constants.%Self)]
  92. // CHECK:STDOUT: %.loc12_27.1: type = facet_type_access %Self.ref.loc12_27 [symbolic = @F.1.%Self (constants.%Self)]
  93. // CHECK:STDOUT: %.loc12_27.2: type = converted %Self.ref.loc12_27, %.loc12_27.1 [symbolic = @F.1.%Self (constants.%Self)]
  94. // CHECK:STDOUT: %other.loc12_20.1: @F.1.%Self (%Self) = param other
  95. // CHECK:STDOUT: %other.loc12_20.2: @F.1.%Self (%Self) = bind_name other, %other.loc12_20.1
  96. // CHECK:STDOUT: %Self.ref.loc12_36: %.1 = name_ref Self, %Self [symbolic = @F.1.%Self (constants.%Self)]
  97. // CHECK:STDOUT: %.loc12_36.1: type = facet_type_access %Self.ref.loc12_36 [symbolic = @F.1.%Self (constants.%Self)]
  98. // CHECK:STDOUT: %.loc12_36.2: type = converted %Self.ref.loc12_36, %.loc12_36.1 [symbolic = @F.1.%Self (constants.%Self)]
  99. // CHECK:STDOUT: %return.var: ref @F.1.%Self (%Self) = var <return slot>
  100. // CHECK:STDOUT: }
  101. // CHECK:STDOUT: %.loc12_40: %.3 = assoc_entity element0, %F.decl [template = constants.%.4]
  102. // CHECK:STDOUT:
  103. // CHECK:STDOUT: !members:
  104. // CHECK:STDOUT: .Self = %Self
  105. // CHECK:STDOUT: .F = %.loc12_40
  106. // CHECK:STDOUT: witness = (%F.decl)
  107. // CHECK:STDOUT: }
  108. // CHECK:STDOUT:
  109. // CHECK:STDOUT: impl @impl: i32 as %.1 {
  110. // CHECK:STDOUT: %F.decl: %F.type.2 = fn_decl @F.2 [template = constants.%F.2] {
  111. // CHECK:STDOUT: %int.make_type_32.loc16_14: init type = call constants.%Int32() [template = i32]
  112. // CHECK:STDOUT: %.loc16_14.1: type = value_of_initializer %int.make_type_32.loc16_14 [template = i32]
  113. // CHECK:STDOUT: %.loc16_14.2: type = converted %int.make_type_32.loc16_14, %.loc16_14.1 [template = i32]
  114. // CHECK:STDOUT: %self.loc16_8.1: i32 = param self
  115. // CHECK:STDOUT: %self.loc16_8.2: i32 = bind_name self, %self.loc16_8.1
  116. // CHECK:STDOUT: %int.make_type_32.loc16_26: init type = call constants.%Int32() [template = i32]
  117. // CHECK:STDOUT: %.loc16_26.1: type = value_of_initializer %int.make_type_32.loc16_26 [template = i32]
  118. // CHECK:STDOUT: %.loc16_26.2: type = converted %int.make_type_32.loc16_26, %.loc16_26.1 [template = i32]
  119. // CHECK:STDOUT: %other.loc16_19.1: i32 = param other
  120. // CHECK:STDOUT: %other.loc16_19.2: i32 = bind_name other, %other.loc16_19.1
  121. // CHECK:STDOUT: %int.make_type_32.loc16_34: init type = call constants.%Int32() [template = i32]
  122. // CHECK:STDOUT: %.loc16_34.1: type = value_of_initializer %int.make_type_32.loc16_34 [template = i32]
  123. // CHECK:STDOUT: %.loc16_34.2: type = converted %int.make_type_32.loc16_34, %.loc16_34.1 [template = i32]
  124. // CHECK:STDOUT: %return.var: ref i32 = var <return slot>
  125. // CHECK:STDOUT: }
  126. // CHECK:STDOUT: %.1: <witness> = interface_witness (%F.decl) [template = constants.%.5]
  127. // CHECK:STDOUT:
  128. // CHECK:STDOUT: !members:
  129. // CHECK:STDOUT: .F = %F.decl
  130. // CHECK:STDOUT: witness = %.1
  131. // CHECK:STDOUT: }
  132. // CHECK:STDOUT:
  133. // CHECK:STDOUT: generic fn @F.1(@I.%Self: %.1) {
  134. // CHECK:STDOUT: %Self: %.1 = bind_symbolic_name Self 0 [symbolic = %Self (constants.%Self)]
  135. // CHECK:STDOUT:
  136. // CHECK:STDOUT: fn[@I.%self.loc12_8.2: @F.1.%Self (%Self)](@I.%other.loc12_20.2: @F.1.%Self (%Self)) -> @F.1.%Self (%Self);
  137. // CHECK:STDOUT: }
  138. // CHECK:STDOUT:
  139. // CHECK:STDOUT: fn @Int32() -> type = "int.make_type_32";
  140. // CHECK:STDOUT:
  141. // CHECK:STDOUT: fn @F.2[@impl.%self.loc16_8.2: i32](@impl.%other.loc16_19.2: i32) -> i32 = "int.sadd";
  142. // CHECK:STDOUT:
  143. // CHECK:STDOUT: specific @F.1(constants.%Self) {
  144. // CHECK:STDOUT: %Self => constants.%Self
  145. // CHECK:STDOUT: }
  146. // CHECK:STDOUT:
  147. // CHECK:STDOUT: specific @F.1(i32) {
  148. // CHECK:STDOUT: %Self => i32
  149. // CHECK:STDOUT: }
  150. // CHECK:STDOUT: