instance_method.carbon 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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: %.1: Core.IntLiteral = int_value 32 [template]
  29. // CHECK:STDOUT: %Int.type: type = fn_type @Int [template]
  30. // CHECK:STDOUT: %Int: %Int.type = struct_value () [template]
  31. // CHECK:STDOUT: %i32: type = int_type signed, %.1 [template]
  32. // CHECK:STDOUT: %F.type.1: type = fn_type @F.1 [template]
  33. // CHECK:STDOUT: %F.1: %F.type.1 = struct_value () [template]
  34. // CHECK:STDOUT: %.2: type = assoc_entity_type %I.type, %F.type.1 [template]
  35. // CHECK:STDOUT: %.3: %.2 = assoc_entity element0, @I.%F.decl [template]
  36. // CHECK:STDOUT: %F.type.2: type = fn_type @F.2 [template]
  37. // CHECK:STDOUT: %F.2: %F.type.2 = struct_value () [template]
  38. // CHECK:STDOUT: %.4: <witness> = interface_witness (%F.2) [template]
  39. // CHECK:STDOUT: %.5: type = struct_type {} [template]
  40. // CHECK:STDOUT: %.6: <witness> = complete_type_witness %.5 [template]
  41. // CHECK:STDOUT: %F.type.3: type = fn_type @F.3 [template]
  42. // CHECK:STDOUT: %F.3: %F.type.3 = struct_value () [template]
  43. // CHECK:STDOUT: }
  44. // CHECK:STDOUT:
  45. // CHECK:STDOUT: imports {
  46. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  47. // CHECK:STDOUT: .Int = %import_ref
  48. // CHECK:STDOUT: import Core//prelude
  49. // CHECK:STDOUT: import Core//prelude/...
  50. // CHECK:STDOUT: }
  51. // CHECK:STDOUT: }
  52. // CHECK:STDOUT:
  53. // CHECK:STDOUT: file {
  54. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  55. // CHECK:STDOUT: .Core = imports.%Core
  56. // CHECK:STDOUT: .C = %C.decl.loc11
  57. // CHECK:STDOUT: .I = %I.decl
  58. // CHECK:STDOUT: .F = %F.decl
  59. // CHECK:STDOUT: }
  60. // CHECK:STDOUT: %Core.import = import Core
  61. // CHECK:STDOUT: %C.decl.loc11: type = class_decl @C [template = constants.%C] {} {}
  62. // CHECK:STDOUT: %I.decl: type = interface_decl @I [template = constants.%I.type] {} {}
  63. // CHECK:STDOUT: %C.decl.loc17: type = class_decl @C [template = constants.%C] {} {}
  64. // CHECK:STDOUT: %F.decl: %F.type.3 = fn_decl @F.3 [template = constants.%F.3] {
  65. // CHECK:STDOUT: %c.patt: %C = binding_pattern c
  66. // CHECK:STDOUT: %c.param_patt: %C = value_param_pattern %c.patt, runtime_param0
  67. // CHECK:STDOUT: %return.patt: %i32 = return_slot_pattern
  68. // CHECK:STDOUT: %return.param_patt: %i32 = out_param_pattern %return.patt, runtime_param1
  69. // CHECK:STDOUT: } {
  70. // CHECK:STDOUT: %C.ref: type = name_ref C, file.%C.decl.loc11 [template = constants.%C]
  71. // CHECK:STDOUT: %.loc23_15.1: Core.IntLiteral = int_value 32 [template = constants.%.1]
  72. // CHECK:STDOUT: %int.make_type_signed: init type = call constants.%Int(%.loc23_15.1) [template = constants.%i32]
  73. // CHECK:STDOUT: %.loc23_15.2: type = value_of_initializer %int.make_type_signed [template = constants.%i32]
  74. // CHECK:STDOUT: %.loc23_15.3: type = converted %int.make_type_signed, %.loc23_15.2 [template = constants.%i32]
  75. // CHECK:STDOUT: %c.param: %C = value_param runtime_param0
  76. // CHECK:STDOUT: %c: %C = bind_name c, %c.param
  77. // CHECK:STDOUT: %return.param: ref %i32 = out_param runtime_param1
  78. // CHECK:STDOUT: %return: ref %i32 = return_slot %return.param
  79. // CHECK:STDOUT: }
  80. // CHECK:STDOUT: }
  81. // CHECK:STDOUT:
  82. // CHECK:STDOUT: interface @I {
  83. // CHECK:STDOUT: %Self: %I.type = bind_symbolic_name Self, 0 [symbolic = constants.%Self]
  84. // CHECK:STDOUT: %F.decl: %F.type.1 = fn_decl @F.1 [template = constants.%F.1] {
  85. // CHECK:STDOUT: %self.patt: @F.1.%Self (%Self) = binding_pattern self
  86. // CHECK:STDOUT: %self.param_patt: @F.1.%Self (%Self) = value_param_pattern %self.patt, runtime_param0
  87. // CHECK:STDOUT: %return.patt: %i32 = return_slot_pattern
  88. // CHECK:STDOUT: %return.param_patt: %i32 = out_param_pattern %return.patt, runtime_param1
  89. // CHECK:STDOUT: } {
  90. // CHECK:STDOUT: %Self.ref: %I.type = name_ref Self, @I.%Self [symbolic = %Self (constants.%Self)]
  91. // CHECK:STDOUT: %.loc14_14.1: type = facet_type_access %Self.ref [symbolic = %Self (constants.%Self)]
  92. // CHECK:STDOUT: %.loc14_14.2: type = converted %Self.ref, %.loc14_14.1 [symbolic = %Self (constants.%Self)]
  93. // CHECK:STDOUT: %.loc14_25.1: Core.IntLiteral = int_value 32 [template = constants.%.1]
  94. // CHECK:STDOUT: %int.make_type_signed: init type = call constants.%Int(%.loc14_25.1) [template = constants.%i32]
  95. // CHECK:STDOUT: %.loc14_25.2: type = value_of_initializer %int.make_type_signed [template = constants.%i32]
  96. // CHECK:STDOUT: %.loc14_25.3: type = converted %int.make_type_signed, %.loc14_25.2 [template = constants.%i32]
  97. // CHECK:STDOUT: %self.param: @F.1.%Self (%Self) = value_param runtime_param0
  98. // CHECK:STDOUT: %self: @F.1.%Self (%Self) = bind_name self, %self.param
  99. // CHECK:STDOUT: %return.param: ref %i32 = out_param runtime_param1
  100. // CHECK:STDOUT: %return: ref %i32 = return_slot %return.param
  101. // CHECK:STDOUT: }
  102. // CHECK:STDOUT: %.loc14: %.2 = assoc_entity element0, %F.decl [template = constants.%.3]
  103. // CHECK:STDOUT:
  104. // CHECK:STDOUT: !members:
  105. // CHECK:STDOUT: .Self = %Self
  106. // CHECK:STDOUT: .F = %.loc14
  107. // CHECK:STDOUT: witness = (%F.decl)
  108. // CHECK:STDOUT: }
  109. // CHECK:STDOUT:
  110. // CHECK:STDOUT: impl @impl: %Self.ref as %I.ref {
  111. // CHECK:STDOUT: %F.decl: %F.type.2 = fn_decl @F.2 [template = constants.%F.2] {
  112. // CHECK:STDOUT: %self.patt: %C = binding_pattern self
  113. // CHECK:STDOUT: %self.param_patt: %C = value_param_pattern %self.patt, runtime_param0
  114. // CHECK:STDOUT: %return.patt: %i32 = return_slot_pattern
  115. // CHECK:STDOUT: %return.param_patt: %i32 = out_param_pattern %return.patt, runtime_param1
  116. // CHECK:STDOUT: } {
  117. // CHECK:STDOUT: %Self.ref: type = name_ref Self, constants.%C [template = constants.%C]
  118. // CHECK:STDOUT: %.loc19_27.1: Core.IntLiteral = int_value 32 [template = constants.%.1]
  119. // CHECK:STDOUT: %int.make_type_signed: init type = call constants.%Int(%.loc19_27.1) [template = constants.%i32]
  120. // CHECK:STDOUT: %.loc19_27.2: type = value_of_initializer %int.make_type_signed [template = constants.%i32]
  121. // CHECK:STDOUT: %.loc19_27.3: type = converted %int.make_type_signed, %.loc19_27.2 [template = constants.%i32]
  122. // CHECK:STDOUT: %self.param: %C = value_param runtime_param0
  123. // CHECK:STDOUT: %self: %C = bind_name self, %self.param
  124. // CHECK:STDOUT: %return.param: ref %i32 = out_param runtime_param1
  125. // CHECK:STDOUT: %return: ref %i32 = return_slot %return.param
  126. // CHECK:STDOUT: }
  127. // CHECK:STDOUT: %.loc18: <witness> = interface_witness (%F.decl) [template = constants.%.4]
  128. // CHECK:STDOUT:
  129. // CHECK:STDOUT: !members:
  130. // CHECK:STDOUT: .F = %F.decl
  131. // CHECK:STDOUT: witness = %.loc18
  132. // CHECK:STDOUT: }
  133. // CHECK:STDOUT:
  134. // CHECK:STDOUT: class @C {
  135. // CHECK:STDOUT: impl_decl @impl [template] {} {
  136. // CHECK:STDOUT: %Self.ref: type = name_ref Self, constants.%C [template = constants.%C]
  137. // CHECK:STDOUT: %I.ref: type = name_ref I, file.%I.decl [template = constants.%I.type]
  138. // CHECK:STDOUT: }
  139. // CHECK:STDOUT: %.loc21: <witness> = complete_type_witness %.5 [template = constants.%.6]
  140. // CHECK:STDOUT:
  141. // CHECK:STDOUT: !members:
  142. // CHECK:STDOUT: .Self = constants.%C
  143. // CHECK:STDOUT: extend @impl.%I.ref
  144. // CHECK:STDOUT: }
  145. // CHECK:STDOUT:
  146. // CHECK:STDOUT: generic fn @F.1(@I.%Self: %I.type) {
  147. // CHECK:STDOUT: %Self: %I.type = bind_symbolic_name Self, 0 [symbolic = %Self (constants.%Self)]
  148. // CHECK:STDOUT:
  149. // CHECK:STDOUT: fn[%self.param_patt: @F.1.%Self (%Self)]() -> %i32;
  150. // CHECK:STDOUT: }
  151. // CHECK:STDOUT:
  152. // CHECK:STDOUT: fn @F.2[%self.param_patt: %C]() -> %i32;
  153. // CHECK:STDOUT:
  154. // CHECK:STDOUT: fn @F.3(%c.param_patt: %C) -> %i32 {
  155. // CHECK:STDOUT: !entry:
  156. // CHECK:STDOUT: %c.ref: %C = name_ref c, %c
  157. // CHECK:STDOUT: %F.ref: %.2 = name_ref F, @I.%.loc14 [template = constants.%.3]
  158. // CHECK:STDOUT: %.loc24_11.1: %F.type.1 = interface_witness_access constants.%.4, element0 [template = constants.%F.2]
  159. // CHECK:STDOUT: %.loc24_11.2: <bound method> = bound_method %c.ref, %.loc24_11.1
  160. // CHECK:STDOUT: %F.call: init %i32 = call %.loc24_11.2(%c.ref)
  161. // CHECK:STDOUT: %.loc24_15.1: %i32 = value_of_initializer %F.call
  162. // CHECK:STDOUT: %.loc24_15.2: %i32 = converted %F.call, %.loc24_15.1
  163. // CHECK:STDOUT: return %.loc24_15.2
  164. // CHECK:STDOUT: }
  165. // CHECK:STDOUT:
  166. // CHECK:STDOUT: specific @F.1(constants.%Self) {
  167. // CHECK:STDOUT: %Self => constants.%Self
  168. // CHECK:STDOUT: }
  169. // CHECK:STDOUT:
  170. // CHECK:STDOUT: specific @F.1(constants.%C) {
  171. // CHECK:STDOUT: %Self => constants.%C
  172. // CHECK:STDOUT: }
  173. // CHECK:STDOUT: