instance_method.carbon 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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.89c: 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: %pattern_type.7ce: type = pattern_type %i32 [concrete]
  39. // CHECK:STDOUT: %I.F.type: type = fn_type @I.F [concrete]
  40. // CHECK:STDOUT: %I.F: %I.F.type = struct_value () [concrete]
  41. // CHECK:STDOUT: %I.assoc_type: type = assoc_entity_type @I [concrete]
  42. // CHECK:STDOUT: %assoc0: %I.assoc_type = assoc_entity element0, @I.%I.F.decl [concrete]
  43. // CHECK:STDOUT: %I.impl_witness: <witness> = impl_witness @C.%I.impl_witness_table [concrete]
  44. // CHECK:STDOUT: %pattern_type.c48: type = pattern_type %C [concrete]
  45. // CHECK:STDOUT: %C.as.I.impl.F.type: type = fn_type @C.as.I.impl.F [concrete]
  46. // CHECK:STDOUT: %C.as.I.impl.F: %C.as.I.impl.F.type = struct_value () [concrete]
  47. // CHECK:STDOUT: %I.facet: %I.type = facet_value %C, (%I.impl_witness) [concrete]
  48. // CHECK:STDOUT: %empty_struct_type: type = struct_type {} [concrete]
  49. // CHECK:STDOUT: %complete_type.357: <witness> = complete_type_witness %empty_struct_type [concrete]
  50. // CHECK:STDOUT: %F.type: type = fn_type @F [concrete]
  51. // CHECK:STDOUT: %F: %F.type = struct_value () [concrete]
  52. // CHECK:STDOUT: %.635: type = fn_type_with_self_type %I.F.type, %I.facet [concrete]
  53. // CHECK:STDOUT: }
  54. // CHECK:STDOUT:
  55. // CHECK:STDOUT: imports {
  56. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [concrete] {
  57. // CHECK:STDOUT: .Int = %Core.Int
  58. // CHECK:STDOUT: import Core//prelude
  59. // CHECK:STDOUT: import Core//prelude/...
  60. // CHECK:STDOUT: }
  61. // CHECK:STDOUT: %Core.Int: %Int.type = import_ref Core//prelude/parts/int, Int, loaded [concrete = constants.%Int.generic]
  62. // CHECK:STDOUT: }
  63. // CHECK:STDOUT:
  64. // CHECK:STDOUT: file {
  65. // CHECK:STDOUT: package: <namespace> = namespace [concrete] {
  66. // CHECK:STDOUT: .Core = imports.%Core
  67. // CHECK:STDOUT: .C = %C.decl.loc15
  68. // CHECK:STDOUT: .I = %I.decl
  69. // CHECK:STDOUT: .F = %F.decl
  70. // CHECK:STDOUT: }
  71. // CHECK:STDOUT: %Core.import = import Core
  72. // CHECK:STDOUT: %C.decl.loc15: type = class_decl @C [concrete = constants.%C] {} {}
  73. // CHECK:STDOUT: %I.decl: type = interface_decl @I [concrete = constants.%I.type] {} {}
  74. // CHECK:STDOUT: %C.decl.loc21: type = class_decl @C [concrete = constants.%C] {} {}
  75. // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [concrete = constants.%F] {
  76. // CHECK:STDOUT: %c.patt: %pattern_type.c48 = value_binding_pattern c [concrete]
  77. // CHECK:STDOUT: %c.param_patt: %pattern_type.c48 = value_param_pattern %c.patt, call_param0 [concrete]
  78. // CHECK:STDOUT: %return.patt: %pattern_type.7ce = return_slot_pattern [concrete]
  79. // CHECK:STDOUT: %return.param_patt: %pattern_type.7ce = out_param_pattern %return.patt, call_param1 [concrete]
  80. // CHECK:STDOUT: } {
  81. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  82. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  83. // CHECK:STDOUT: %c.param: %C = value_param call_param0
  84. // CHECK:STDOUT: %C.ref: type = name_ref C, file.%C.decl.loc15 [concrete = constants.%C]
  85. // CHECK:STDOUT: %c: %C = value_binding c, %c.param
  86. // CHECK:STDOUT: %return.param: ref %i32 = out_param call_param1
  87. // CHECK:STDOUT: %return: ref %i32 = return_slot %return.param
  88. // CHECK:STDOUT: }
  89. // CHECK:STDOUT: }
  90. // CHECK:STDOUT:
  91. // CHECK:STDOUT: interface @I {
  92. // CHECK:STDOUT: %Self: %I.type = symbolic_binding Self, 0 [symbolic = constants.%Self]
  93. // CHECK:STDOUT: %I.F.decl: %I.F.type = fn_decl @I.F [concrete = constants.%I.F] {
  94. // CHECK:STDOUT: %self.patt: @I.F.%pattern_type (%pattern_type.89c) = value_binding_pattern self [concrete]
  95. // CHECK:STDOUT: %self.param_patt: @I.F.%pattern_type (%pattern_type.89c) = value_param_pattern %self.patt, call_param0 [concrete]
  96. // CHECK:STDOUT: %return.patt: %pattern_type.7ce = return_slot_pattern [concrete]
  97. // CHECK:STDOUT: %return.param_patt: %pattern_type.7ce = out_param_pattern %return.patt, call_param1 [concrete]
  98. // CHECK:STDOUT: } {
  99. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  100. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  101. // CHECK:STDOUT: %self.param: @I.F.%Self.binding.as_type (%Self.binding.as_type) = value_param call_param0
  102. // CHECK:STDOUT: %.loc18_14.1: type = splice_block %.loc18_14.2 [symbolic = %Self.binding.as_type (constants.%Self.binding.as_type)] {
  103. // CHECK:STDOUT: %Self.ref: %I.type = name_ref Self, @I.%Self [symbolic = %Self (constants.%Self)]
  104. // CHECK:STDOUT: %Self.as_type: type = facet_access_type %Self.ref [symbolic = %Self.binding.as_type (constants.%Self.binding.as_type)]
  105. // CHECK:STDOUT: %.loc18_14.2: type = converted %Self.ref, %Self.as_type [symbolic = %Self.binding.as_type (constants.%Self.binding.as_type)]
  106. // CHECK:STDOUT: }
  107. // CHECK:STDOUT: %self: @I.F.%Self.binding.as_type (%Self.binding.as_type) = value_binding self, %self.param
  108. // CHECK:STDOUT: %return.param: ref %i32 = out_param call_param1
  109. // CHECK:STDOUT: %return: ref %i32 = return_slot %return.param
  110. // CHECK:STDOUT: }
  111. // CHECK:STDOUT: %assoc0: %I.assoc_type = assoc_entity element0, %I.F.decl [concrete = constants.%assoc0]
  112. // CHECK:STDOUT:
  113. // CHECK:STDOUT: !members:
  114. // CHECK:STDOUT: .Self = %Self
  115. // CHECK:STDOUT: .F = %assoc0
  116. // CHECK:STDOUT: witness = (%I.F.decl)
  117. // CHECK:STDOUT:
  118. // CHECK:STDOUT: !requires:
  119. // CHECK:STDOUT: }
  120. // CHECK:STDOUT:
  121. // CHECK:STDOUT: impl @C.as.I.impl: %Self.ref as %I.ref {
  122. // 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] {
  123. // CHECK:STDOUT: %self.patt: %pattern_type.c48 = value_binding_pattern self [concrete]
  124. // CHECK:STDOUT: %self.param_patt: %pattern_type.c48 = value_param_pattern %self.patt, call_param0 [concrete]
  125. // CHECK:STDOUT: %return.patt: %pattern_type.7ce = return_slot_pattern [concrete]
  126. // CHECK:STDOUT: %return.param_patt: %pattern_type.7ce = out_param_pattern %return.patt, call_param1 [concrete]
  127. // CHECK:STDOUT: } {
  128. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  129. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  130. // CHECK:STDOUT: %self.param: %C = value_param call_param0
  131. // CHECK:STDOUT: %Self.ref: type = name_ref Self, constants.%C [concrete = constants.%C]
  132. // CHECK:STDOUT: %self: %C = value_binding self, %self.param
  133. // CHECK:STDOUT: %return.param: ref %i32 = out_param call_param1
  134. // CHECK:STDOUT: %return: ref %i32 = return_slot %return.param
  135. // CHECK:STDOUT: }
  136. // CHECK:STDOUT:
  137. // CHECK:STDOUT: !members:
  138. // CHECK:STDOUT: .F = %C.as.I.impl.F.decl
  139. // CHECK:STDOUT: witness = @C.%I.impl_witness
  140. // CHECK:STDOUT: }
  141. // CHECK:STDOUT:
  142. // CHECK:STDOUT: class @C {
  143. // CHECK:STDOUT: impl_decl @C.as.I.impl [concrete] {} {
  144. // CHECK:STDOUT: %Self.ref: type = name_ref Self, constants.%C [concrete = constants.%C]
  145. // CHECK:STDOUT: %I.ref: type = name_ref I, file.%I.decl [concrete = constants.%I.type]
  146. // CHECK:STDOUT: }
  147. // CHECK:STDOUT: %I.impl_witness_table = impl_witness_table (@C.as.I.impl.%C.as.I.impl.F.decl), @C.as.I.impl [concrete]
  148. // CHECK:STDOUT: %I.impl_witness: <witness> = impl_witness %I.impl_witness_table [concrete = constants.%I.impl_witness]
  149. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness constants.%empty_struct_type [concrete = constants.%complete_type.357]
  150. // CHECK:STDOUT: complete_type_witness = %complete_type
  151. // CHECK:STDOUT:
  152. // CHECK:STDOUT: !members:
  153. // CHECK:STDOUT: .Self = constants.%C
  154. // CHECK:STDOUT: .I = <poisoned>
  155. // CHECK:STDOUT: .F = <poisoned>
  156. // CHECK:STDOUT: extend @C.as.I.impl.%I.ref
  157. // CHECK:STDOUT: }
  158. // CHECK:STDOUT:
  159. // CHECK:STDOUT: generic fn @I.F(@I.%Self: %I.type) {
  160. // CHECK:STDOUT: %Self: %I.type = symbolic_binding Self, 0 [symbolic = %Self (constants.%Self)]
  161. // CHECK:STDOUT: %Self.binding.as_type: type = symbolic_binding_type Self, 0, %Self [symbolic = %Self.binding.as_type (constants.%Self.binding.as_type)]
  162. // CHECK:STDOUT: %pattern_type: type = pattern_type %Self.binding.as_type [symbolic = %pattern_type (constants.%pattern_type.89c)]
  163. // CHECK:STDOUT:
  164. // CHECK:STDOUT: fn(%self.param: @I.F.%Self.binding.as_type (%Self.binding.as_type)) -> %i32;
  165. // CHECK:STDOUT: }
  166. // CHECK:STDOUT:
  167. // CHECK:STDOUT: fn @C.as.I.impl.F(%self.param: %C) -> %i32;
  168. // CHECK:STDOUT:
  169. // CHECK:STDOUT: fn @F(%c.param: %C) -> %i32 {
  170. // CHECK:STDOUT: !entry:
  171. // CHECK:STDOUT: %c.ref: %C = name_ref c, %c
  172. // CHECK:STDOUT: %F.ref: %I.assoc_type = name_ref F, @I.%assoc0 [concrete = constants.%assoc0]
  173. // CHECK:STDOUT: %impl.elem0: %.635 = impl_witness_access constants.%I.impl_witness, element0 [concrete = constants.%C.as.I.impl.F]
  174. // CHECK:STDOUT: %bound_method: <bound method> = bound_method %c.ref, %impl.elem0
  175. // CHECK:STDOUT: %C.as.I.impl.F.call: init %i32 = call %bound_method(%c.ref)
  176. // CHECK:STDOUT: return %C.as.I.impl.F.call to %return
  177. // CHECK:STDOUT: }
  178. // CHECK:STDOUT:
  179. // CHECK:STDOUT: specific @I.F(constants.%Self) {
  180. // CHECK:STDOUT: %Self => constants.%Self
  181. // CHECK:STDOUT: %Self.binding.as_type => constants.%Self.binding.as_type
  182. // CHECK:STDOUT: %pattern_type => constants.%pattern_type.89c
  183. // CHECK:STDOUT: }
  184. // CHECK:STDOUT:
  185. // CHECK:STDOUT: specific @I.F(constants.%I.facet) {
  186. // CHECK:STDOUT: %Self => constants.%I.facet
  187. // CHECK:STDOUT: %Self.binding.as_type => constants.%C
  188. // CHECK:STDOUT: %pattern_type => constants.%pattern_type.c48
  189. // CHECK:STDOUT: }
  190. // CHECK:STDOUT: