instance_method.carbon 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. // INCLUDE-FILE: toolchain/testing/testdata/min_prelude/int.carbon
  6. // TODO: Add ranges and switch to "--dump-sem-ir-ranges=only".
  7. // EXTRA-ARGS: --dump-sem-ir-ranges=if-present
  8. //
  9. // AUTOUPDATE
  10. // TIP: To test this file alone, run:
  11. // TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/impl/lookup/instance_method.carbon
  12. // TIP: To dump output, run:
  13. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/impl/lookup/instance_method.carbon
  14. class C;
  15. interface I {
  16. fn F[self: Self]() -> i32;
  17. }
  18. class C {
  19. extend impl as I {
  20. fn F[self: Self]() -> i32;
  21. }
  22. }
  23. fn F(c: C) -> i32 {
  24. return c.F();
  25. }
  26. // CHECK:STDOUT: --- instance_method.carbon
  27. // CHECK:STDOUT:
  28. // CHECK:STDOUT: constants {
  29. // CHECK:STDOUT: %C: type = class_type @C [concrete]
  30. // CHECK:STDOUT: %I.type: type = facet_type <@I> [concrete]
  31. // CHECK:STDOUT: %Self: %I.type = symbolic_binding Self, 0 [symbolic]
  32. // CHECK:STDOUT: %Self.binding.as_type: type = symbolic_binding_type Self, 0, %Self [symbolic]
  33. // CHECK:STDOUT: %pattern_type.fa0: type = pattern_type %Self.binding.as_type [symbolic]
  34. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete]
  35. // CHECK:STDOUT: %Int.type: type = generic_class_type @Int [concrete]
  36. // CHECK:STDOUT: %Int.generic: %Int.type = struct_value () [concrete]
  37. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [concrete]
  38. // CHECK:STDOUT: %.4d5: form = init_form %i32, call_param1 [concrete]
  39. // CHECK:STDOUT: %pattern_type.7ce: type = pattern_type %i32 [concrete]
  40. // CHECK:STDOUT: %I.F.type: type = fn_type @I.F [concrete]
  41. // CHECK:STDOUT: %I.F: %I.F.type = struct_value () [concrete]
  42. // CHECK:STDOUT: %I.assoc_type: type = assoc_entity_type @I [concrete]
  43. // CHECK:STDOUT: %assoc0: %I.assoc_type = assoc_entity element0, @I.%I.F.decl [concrete]
  44. // CHECK:STDOUT: %I.impl_witness: <witness> = impl_witness @C.as.I.impl.%I.impl_witness_table [concrete]
  45. // CHECK:STDOUT: %pattern_type.7c7: type = pattern_type %C [concrete]
  46. // CHECK:STDOUT: %C.as.I.impl.F.type: type = fn_type @C.as.I.impl.F [concrete]
  47. // CHECK:STDOUT: %C.as.I.impl.F: %C.as.I.impl.F.type = struct_value () [concrete]
  48. // CHECK:STDOUT: %I.facet: %I.type = facet_value %C, (%I.impl_witness) [concrete]
  49. // CHECK:STDOUT: %empty_struct_type: type = struct_type {} [concrete]
  50. // CHECK:STDOUT: %complete_type.357: <witness> = complete_type_witness %empty_struct_type [concrete]
  51. // CHECK:STDOUT: %F.type: type = fn_type @F [concrete]
  52. // CHECK:STDOUT: %F: %F.type = struct_value () [concrete]
  53. // CHECK:STDOUT: %.7a3: type = fn_type_with_self_type %I.F.type, %I.facet [concrete]
  54. // CHECK:STDOUT: }
  55. // CHECK:STDOUT:
  56. // CHECK:STDOUT: imports {
  57. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [concrete] {
  58. // CHECK:STDOUT: .Int = %Core.Int
  59. // CHECK:STDOUT: import Core//prelude
  60. // CHECK:STDOUT: import Core//prelude/...
  61. // CHECK:STDOUT: }
  62. // CHECK:STDOUT: %Core.Int: %Int.type = import_ref Core//prelude/parts/int, Int, loaded [concrete = constants.%Int.generic]
  63. // CHECK:STDOUT: }
  64. // CHECK:STDOUT:
  65. // CHECK:STDOUT: file {
  66. // CHECK:STDOUT: package: <namespace> = namespace [concrete] {
  67. // CHECK:STDOUT: .Core = imports.%Core
  68. // CHECK:STDOUT: .C = %C.decl.loc15
  69. // CHECK:STDOUT: .I = %I.decl
  70. // CHECK:STDOUT: .F = %F.decl
  71. // CHECK:STDOUT: }
  72. // CHECK:STDOUT: %Core.import = import Core
  73. // CHECK:STDOUT: %C.decl.loc15: type = class_decl @C [concrete = constants.%C] {} {}
  74. // CHECK:STDOUT: %I.decl: type = interface_decl @I [concrete = constants.%I.type] {} {}
  75. // CHECK:STDOUT: %C.decl.loc21: type = class_decl @C [concrete = constants.%C] {} {}
  76. // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [concrete = constants.%F] {
  77. // CHECK:STDOUT: %c.patt: %pattern_type.7c7 = value_binding_pattern c [concrete]
  78. // CHECK:STDOUT: %c.param_patt: %pattern_type.7c7 = value_param_pattern %c.patt, call_param0 [concrete]
  79. // CHECK:STDOUT: %return.patt: %pattern_type.7ce = return_slot_pattern [concrete]
  80. // CHECK:STDOUT: %return.param_patt: %pattern_type.7ce = out_param_pattern %return.patt, call_param1 [concrete]
  81. // CHECK:STDOUT: } {
  82. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  83. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  84. // CHECK:STDOUT: %.loc27: form = init_form %i32, call_param1 [concrete = constants.%.4d5]
  85. // CHECK:STDOUT: %c.param: %C = value_param call_param0
  86. // CHECK:STDOUT: %C.ref: type = name_ref C, file.%C.decl.loc15 [concrete = constants.%C]
  87. // CHECK:STDOUT: %c: %C = value_binding c, %c.param
  88. // CHECK:STDOUT: %return.param: ref %i32 = out_param call_param1
  89. // CHECK:STDOUT: %return: ref %i32 = return_slot %return.param
  90. // CHECK:STDOUT: }
  91. // CHECK:STDOUT: }
  92. // CHECK:STDOUT:
  93. // CHECK:STDOUT: interface @I {
  94. // CHECK:STDOUT: %Self: %I.type = symbolic_binding Self, 0 [symbolic = constants.%Self]
  95. // CHECK:STDOUT: %I.F.decl: %I.F.type = fn_decl @I.F [concrete = constants.%I.F] {
  96. // CHECK:STDOUT: %self.patt: @I.F.%pattern_type (%pattern_type.fa0) = value_binding_pattern self [concrete]
  97. // CHECK:STDOUT: %self.param_patt: @I.F.%pattern_type (%pattern_type.fa0) = value_param_pattern %self.patt, call_param0 [concrete]
  98. // CHECK:STDOUT: %return.patt: %pattern_type.7ce = return_slot_pattern [concrete]
  99. // CHECK:STDOUT: %return.param_patt: %pattern_type.7ce = out_param_pattern %return.patt, call_param1 [concrete]
  100. // CHECK:STDOUT: } {
  101. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  102. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  103. // CHECK:STDOUT: %.loc18_25: form = init_form %i32, call_param1 [concrete = constants.%.4d5]
  104. // CHECK:STDOUT: %self.param: @I.F.%Self.binding.as_type (%Self.binding.as_type) = value_param call_param0
  105. // CHECK:STDOUT: %.loc18_14.1: type = splice_block %.loc18_14.2 [symbolic = %Self.binding.as_type (constants.%Self.binding.as_type)] {
  106. // CHECK:STDOUT: %Self.ref: %I.type = name_ref Self, @I.%Self [symbolic = %Self (constants.%Self)]
  107. // CHECK:STDOUT: %Self.as_type: type = facet_access_type %Self.ref [symbolic = %Self.binding.as_type (constants.%Self.binding.as_type)]
  108. // CHECK:STDOUT: %.loc18_14.2: type = converted %Self.ref, %Self.as_type [symbolic = %Self.binding.as_type (constants.%Self.binding.as_type)]
  109. // CHECK:STDOUT: }
  110. // CHECK:STDOUT: %self: @I.F.%Self.binding.as_type (%Self.binding.as_type) = value_binding self, %self.param
  111. // CHECK:STDOUT: %return.param: ref %i32 = out_param call_param1
  112. // CHECK:STDOUT: %return: ref %i32 = return_slot %return.param
  113. // CHECK:STDOUT: }
  114. // CHECK:STDOUT: %assoc0: %I.assoc_type = assoc_entity element0, %I.F.decl [concrete = constants.%assoc0]
  115. // CHECK:STDOUT:
  116. // CHECK:STDOUT: !members:
  117. // CHECK:STDOUT: .Self = %Self
  118. // CHECK:STDOUT: .F = %assoc0
  119. // CHECK:STDOUT: witness = (%I.F.decl)
  120. // CHECK:STDOUT:
  121. // CHECK:STDOUT: !requires:
  122. // CHECK:STDOUT: }
  123. // CHECK:STDOUT:
  124. // CHECK:STDOUT: impl @C.as.I.impl: %Self.ref as %I.ref {
  125. // CHECK:STDOUT: %C.as.I.impl.F.decl: %C.as.I.impl.F.type = fn_decl @C.as.I.impl.F [concrete = constants.%C.as.I.impl.F] {
  126. // CHECK:STDOUT: %self.patt: %pattern_type.7c7 = value_binding_pattern self [concrete]
  127. // CHECK:STDOUT: %self.param_patt: %pattern_type.7c7 = value_param_pattern %self.patt, call_param0 [concrete]
  128. // CHECK:STDOUT: %return.patt: %pattern_type.7ce = return_slot_pattern [concrete]
  129. // CHECK:STDOUT: %return.param_patt: %pattern_type.7ce = out_param_pattern %return.patt, call_param1 [concrete]
  130. // CHECK:STDOUT: } {
  131. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  132. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  133. // CHECK:STDOUT: %.loc23: form = init_form %i32, call_param1 [concrete = constants.%.4d5]
  134. // CHECK:STDOUT: %self.param: %C = value_param call_param0
  135. // CHECK:STDOUT: %Self.ref: type = name_ref Self, constants.%C [concrete = constants.%C]
  136. // CHECK:STDOUT: %self: %C = value_binding self, %self.param
  137. // CHECK:STDOUT: %return.param: ref %i32 = out_param call_param1
  138. // CHECK:STDOUT: %return: ref %i32 = return_slot %return.param
  139. // CHECK:STDOUT: }
  140. // CHECK:STDOUT: %I.impl_witness_table = impl_witness_table (%C.as.I.impl.F.decl), @C.as.I.impl [concrete]
  141. // CHECK:STDOUT: %I.impl_witness: <witness> = impl_witness %I.impl_witness_table [concrete = constants.%I.impl_witness]
  142. // CHECK:STDOUT:
  143. // CHECK:STDOUT: !members:
  144. // CHECK:STDOUT: .F = %C.as.I.impl.F.decl
  145. // CHECK:STDOUT: witness = %I.impl_witness
  146. // CHECK:STDOUT: }
  147. // CHECK:STDOUT:
  148. // CHECK:STDOUT: class @C {
  149. // CHECK:STDOUT: impl_decl @C.as.I.impl [concrete] {} {
  150. // CHECK:STDOUT: %Self.ref: type = name_ref Self, constants.%C [concrete = constants.%C]
  151. // CHECK:STDOUT: %I.ref: type = name_ref I, file.%I.decl [concrete = constants.%I.type]
  152. // CHECK:STDOUT: }
  153. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness constants.%empty_struct_type [concrete = constants.%complete_type.357]
  154. // CHECK:STDOUT: complete_type_witness = %complete_type
  155. // CHECK:STDOUT:
  156. // CHECK:STDOUT: !members:
  157. // CHECK:STDOUT: .Self = constants.%C
  158. // CHECK:STDOUT: .I = <poisoned>
  159. // CHECK:STDOUT: .F = <poisoned>
  160. // CHECK:STDOUT: extend @C.as.I.impl.%I.ref
  161. // CHECK:STDOUT: }
  162. // CHECK:STDOUT:
  163. // CHECK:STDOUT: generic fn @I.F(@I.%Self: %I.type) {
  164. // CHECK:STDOUT: %Self: %I.type = symbolic_binding Self, 0 [symbolic = %Self (constants.%Self)]
  165. // CHECK:STDOUT: %Self.binding.as_type: type = symbolic_binding_type Self, 0, %Self [symbolic = %Self.binding.as_type (constants.%Self.binding.as_type)]
  166. // CHECK:STDOUT: %pattern_type: type = pattern_type %Self.binding.as_type [symbolic = %pattern_type (constants.%pattern_type.fa0)]
  167. // CHECK:STDOUT:
  168. // CHECK:STDOUT: fn(%self.param: @I.F.%Self.binding.as_type (%Self.binding.as_type)) -> %i32;
  169. // CHECK:STDOUT: }
  170. // CHECK:STDOUT:
  171. // CHECK:STDOUT: fn @C.as.I.impl.F(%self.param: %C) -> %i32;
  172. // CHECK:STDOUT:
  173. // CHECK:STDOUT: fn @F(%c.param: %C) -> %i32 {
  174. // CHECK:STDOUT: !entry:
  175. // CHECK:STDOUT: %c.ref: %C = name_ref c, %c
  176. // CHECK:STDOUT: %F.ref: %I.assoc_type = name_ref F, @I.%assoc0 [concrete = constants.%assoc0]
  177. // CHECK:STDOUT: %impl.elem0: %.7a3 = impl_witness_access constants.%I.impl_witness, element0 [concrete = constants.%C.as.I.impl.F]
  178. // CHECK:STDOUT: %bound_method: <bound method> = bound_method %c.ref, %impl.elem0
  179. // CHECK:STDOUT: %C.as.I.impl.F.call: init %i32 = call %bound_method(%c.ref)
  180. // CHECK:STDOUT: return %C.as.I.impl.F.call to %return.param
  181. // CHECK:STDOUT: }
  182. // CHECK:STDOUT:
  183. // CHECK:STDOUT: specific @I.F(constants.%Self) {
  184. // CHECK:STDOUT: %Self => constants.%Self
  185. // CHECK:STDOUT: %Self.binding.as_type => constants.%Self.binding.as_type
  186. // CHECK:STDOUT: %pattern_type => constants.%pattern_type.fa0
  187. // CHECK:STDOUT: }
  188. // CHECK:STDOUT:
  189. // CHECK:STDOUT: specific @I.F(constants.%I.facet) {
  190. // CHECK:STDOUT: %Self => constants.%I.facet
  191. // CHECK:STDOUT: %Self.binding.as_type => constants.%C
  192. // CHECK:STDOUT: %pattern_type => constants.%pattern_type.7c7
  193. // CHECK:STDOUT: }
  194. // CHECK:STDOUT: