instance_method.carbon 8.5 KB

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