instance_method.carbon 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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/impl/lookup/instance_method.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/impl/lookup/instance_method.carbon
  10. class C;
  11. interface I {
  12. fn F[self: Self]() -> i32;
  13. }
  14. class C {
  15. extend impl as I {
  16. fn F[self: Self]() -> i32;
  17. }
  18. }
  19. fn F(c: C) -> i32 {
  20. return c.F();
  21. }
  22. // CHECK:STDOUT: --- instance_method.carbon
  23. // CHECK:STDOUT:
  24. // CHECK:STDOUT: constants {
  25. // CHECK:STDOUT: %C: type = class_type @C [template]
  26. // CHECK:STDOUT: %I.type: type = facet_type <@I> [template]
  27. // CHECK:STDOUT: %Self: %I.type = bind_symbolic_name Self, 0 [symbolic]
  28. // CHECK:STDOUT: %Self.as_type: type = facet_access_type %Self [symbolic]
  29. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [template]
  30. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [template]
  31. // CHECK:STDOUT: %F.type.cf0: type = fn_type @F.1 [template]
  32. // CHECK:STDOUT: %F.bc6: %F.type.cf0 = struct_value () [template]
  33. // CHECK:STDOUT: %I.assoc_type: type = assoc_entity_type %I.type [template]
  34. // CHECK:STDOUT: %assoc0: %I.assoc_type = assoc_entity element0, @I.%F.decl [template]
  35. // CHECK:STDOUT: %impl_witness: <witness> = impl_witness (@impl.%F.decl) [template]
  36. // CHECK:STDOUT: %F.type.f36: type = fn_type @F.2 [template]
  37. // CHECK:STDOUT: %F.4c3: %F.type.f36 = struct_value () [template]
  38. // CHECK:STDOUT: %I.facet: %I.type = facet_value %C, %impl_witness [template]
  39. // CHECK:STDOUT: %empty_struct_type: type = struct_type {} [template]
  40. // CHECK:STDOUT: %complete_type.357: <witness> = complete_type_witness %empty_struct_type [template]
  41. // CHECK:STDOUT: %F.type.b25: type = fn_type @F.3 [template]
  42. // CHECK:STDOUT: %F.c41: %F.type.b25 = struct_value () [template]
  43. // CHECK:STDOUT: %.f8b: type = fn_type_with_self_type %F.type.cf0, %I.facet [template]
  44. // CHECK:STDOUT: }
  45. // CHECK:STDOUT:
  46. // CHECK:STDOUT: imports {
  47. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  48. // CHECK:STDOUT: .Int = %Core.Int
  49. // CHECK:STDOUT: import Core//prelude
  50. // CHECK:STDOUT: import Core//prelude/...
  51. // CHECK:STDOUT: }
  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: .C = %C.decl.loc11
  58. // CHECK:STDOUT: .I = %I.decl
  59. // CHECK:STDOUT: .F = %F.decl
  60. // CHECK:STDOUT: }
  61. // CHECK:STDOUT: %Core.import = import Core
  62. // CHECK:STDOUT: %C.decl.loc11: type = class_decl @C [template = constants.%C] {} {}
  63. // CHECK:STDOUT: %I.decl: type = interface_decl @I [template = constants.%I.type] {} {}
  64. // CHECK:STDOUT: %C.decl.loc17: type = class_decl @C [template = constants.%C] {} {}
  65. // CHECK:STDOUT: %F.decl: %F.type.b25 = fn_decl @F.3 [template = constants.%F.c41] {
  66. // CHECK:STDOUT: %c.patt: %C = binding_pattern c
  67. // CHECK:STDOUT: %c.param_patt: %C = value_param_pattern %c.patt, runtime_param0
  68. // CHECK:STDOUT: %return.patt: %i32 = return_slot_pattern
  69. // CHECK:STDOUT: %return.param_patt: %i32 = out_param_pattern %return.patt, runtime_param1
  70. // CHECK:STDOUT: } {
  71. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  72. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  73. // CHECK:STDOUT: %c.param: %C = value_param runtime_param0
  74. // CHECK:STDOUT: %C.ref: type = name_ref C, file.%C.decl.loc11 [template = constants.%C]
  75. // CHECK:STDOUT: %c: %C = bind_name c, %c.param
  76. // CHECK:STDOUT: %return.param: ref %i32 = out_param runtime_param1
  77. // CHECK:STDOUT: %return: ref %i32 = return_slot %return.param
  78. // CHECK:STDOUT: }
  79. // CHECK:STDOUT: }
  80. // CHECK:STDOUT:
  81. // CHECK:STDOUT: interface @I {
  82. // CHECK:STDOUT: %Self: %I.type = bind_symbolic_name Self, 0 [symbolic = constants.%Self]
  83. // CHECK:STDOUT: %F.decl: %F.type.cf0 = fn_decl @F.1 [template = constants.%F.bc6] {
  84. // CHECK:STDOUT: %self.patt: @F.1.%Self.as_type.loc14_14.1 (%Self.as_type) = binding_pattern self
  85. // CHECK:STDOUT: %self.param_patt: @F.1.%Self.as_type.loc14_14.1 (%Self.as_type) = value_param_pattern %self.patt, runtime_param0
  86. // CHECK:STDOUT: %return.patt: %i32 = return_slot_pattern
  87. // CHECK:STDOUT: %return.param_patt: %i32 = out_param_pattern %return.patt, runtime_param1
  88. // CHECK:STDOUT: } {
  89. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  90. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  91. // CHECK:STDOUT: %self.param: @F.1.%Self.as_type.loc14_14.1 (%Self.as_type) = value_param runtime_param0
  92. // CHECK:STDOUT: %.loc14_14.1: type = splice_block %.loc14_14.2 [symbolic = %Self.as_type.loc14_14.1 (constants.%Self.as_type)] {
  93. // CHECK:STDOUT: %Self.ref: %I.type = name_ref Self, @I.%Self [symbolic = %Self (constants.%Self)]
  94. // CHECK:STDOUT: %Self.as_type.loc14_14.2: type = facet_access_type %Self.ref [symbolic = %Self.as_type.loc14_14.1 (constants.%Self.as_type)]
  95. // CHECK:STDOUT: %.loc14_14.2: type = converted %Self.ref, %Self.as_type.loc14_14.2 [symbolic = %Self.as_type.loc14_14.1 (constants.%Self.as_type)]
  96. // CHECK:STDOUT: }
  97. // CHECK:STDOUT: %self: @F.1.%Self.as_type.loc14_14.1 (%Self.as_type) = bind_name self, %self.param
  98. // CHECK:STDOUT: %return.param: ref %i32 = out_param runtime_param1
  99. // CHECK:STDOUT: %return: ref %i32 = return_slot %return.param
  100. // CHECK:STDOUT: }
  101. // CHECK:STDOUT: %assoc0: %I.assoc_type = assoc_entity element0, %F.decl [template = constants.%assoc0]
  102. // CHECK:STDOUT:
  103. // CHECK:STDOUT: !members:
  104. // CHECK:STDOUT: .Self = %Self
  105. // CHECK:STDOUT: .F = %assoc0
  106. // CHECK:STDOUT: witness = (%F.decl)
  107. // CHECK:STDOUT: }
  108. // CHECK:STDOUT:
  109. // CHECK:STDOUT: impl @impl: %Self.ref as %I.ref {
  110. // CHECK:STDOUT: %F.decl: %F.type.f36 = fn_decl @F.2 [template = constants.%F.4c3] {
  111. // CHECK:STDOUT: %self.patt: %C = binding_pattern self
  112. // CHECK:STDOUT: %self.param_patt: %C = value_param_pattern %self.patt, runtime_param0
  113. // CHECK:STDOUT: %return.patt: %i32 = return_slot_pattern
  114. // CHECK:STDOUT: %return.param_patt: %i32 = out_param_pattern %return.patt, runtime_param1
  115. // CHECK:STDOUT: } {
  116. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  117. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  118. // CHECK:STDOUT: %self.param: %C = value_param runtime_param0
  119. // CHECK:STDOUT: %Self.ref: type = name_ref Self, constants.%C [template = constants.%C]
  120. // CHECK:STDOUT: %self: %C = bind_name self, %self.param
  121. // CHECK:STDOUT: %return.param: ref %i32 = out_param runtime_param1
  122. // CHECK:STDOUT: %return: ref %i32 = return_slot %return.param
  123. // CHECK:STDOUT: }
  124. // CHECK:STDOUT:
  125. // CHECK:STDOUT: !members:
  126. // CHECK:STDOUT: .F = %F.decl
  127. // CHECK:STDOUT: witness = @C.%impl_witness
  128. // CHECK:STDOUT: }
  129. // CHECK:STDOUT:
  130. // CHECK:STDOUT: class @C {
  131. // CHECK:STDOUT: impl_decl @impl [template] {} {
  132. // CHECK:STDOUT: %Self.ref: type = name_ref Self, constants.%C [template = constants.%C]
  133. // CHECK:STDOUT: %I.ref: type = name_ref I, file.%I.decl [template = constants.%I.type]
  134. // CHECK:STDOUT: }
  135. // CHECK:STDOUT: %impl_witness: <witness> = impl_witness (@impl.%F.decl) [template = constants.%impl_witness]
  136. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %empty_struct_type [template = constants.%complete_type.357]
  137. // CHECK:STDOUT: complete_type_witness = %complete_type
  138. // CHECK:STDOUT:
  139. // CHECK:STDOUT: !members:
  140. // CHECK:STDOUT: .Self = constants.%C
  141. // CHECK:STDOUT: extend @impl.%I.ref
  142. // CHECK:STDOUT: }
  143. // CHECK:STDOUT:
  144. // CHECK:STDOUT: generic fn @F.1(@I.%Self: %I.type) {
  145. // CHECK:STDOUT: %Self: %I.type = bind_symbolic_name Self, 0 [symbolic = %Self (constants.%Self)]
  146. // CHECK:STDOUT: %Self.as_type.loc14_14.1: type = facet_access_type %Self [symbolic = %Self.as_type.loc14_14.1 (constants.%Self.as_type)]
  147. // CHECK:STDOUT:
  148. // CHECK:STDOUT: fn[%self.param_patt: @F.1.%Self.as_type.loc14_14.1 (%Self.as_type)]() -> %i32;
  149. // CHECK:STDOUT: }
  150. // CHECK:STDOUT:
  151. // CHECK:STDOUT: fn @F.2[%self.param_patt: %C]() -> %i32;
  152. // CHECK:STDOUT:
  153. // CHECK:STDOUT: fn @F.3(%c.param_patt: %C) -> %i32 {
  154. // CHECK:STDOUT: !entry:
  155. // CHECK:STDOUT: %c.ref: %C = name_ref c, %c
  156. // CHECK:STDOUT: %F.ref: %I.assoc_type = name_ref F, @I.%assoc0 [template = constants.%assoc0]
  157. // CHECK:STDOUT: %impl.elem0: %.f8b = impl_witness_access constants.%impl_witness, element0 [template = constants.%F.4c3]
  158. // CHECK:STDOUT: %bound_method: <bound method> = bound_method %c.ref, %impl.elem0
  159. // CHECK:STDOUT: %F.call: init %i32 = call %bound_method(%c.ref)
  160. // CHECK:STDOUT: %.loc24_15.1: %i32 = value_of_initializer %F.call
  161. // CHECK:STDOUT: %.loc24_15.2: %i32 = converted %F.call, %.loc24_15.1
  162. // CHECK:STDOUT: return %.loc24_15.2
  163. // CHECK:STDOUT: }
  164. // CHECK:STDOUT:
  165. // CHECK:STDOUT: specific @F.1(constants.%Self) {
  166. // CHECK:STDOUT: %Self => constants.%Self
  167. // CHECK:STDOUT: %Self.as_type.loc14_14.1 => constants.%Self.as_type
  168. // CHECK:STDOUT: }
  169. // CHECK:STDOUT:
  170. // CHECK:STDOUT: specific @F.1(constants.%I.facet) {
  171. // CHECK:STDOUT: %Self => constants.%I.facet
  172. // CHECK:STDOUT: %Self.as_type.loc14_14.1 => constants.%C
  173. // CHECK:STDOUT: }
  174. // CHECK:STDOUT: