instance_method.carbon 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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: %.1: type = interface_type @I [template]
  27. // CHECK:STDOUT: %Self: %.1 = bind_symbolic_name Self 0 [symbolic]
  28. // CHECK:STDOUT: %Int32.type: type = fn_type @Int32 [template]
  29. // CHECK:STDOUT: %.2: 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: %.3: type = assoc_entity_type @I, %F.type.1 [template]
  34. // CHECK:STDOUT: %.4: %.3 = 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: %.5: <witness> = interface_witness (%F.2) [template]
  38. // CHECK:STDOUT: %.6: type = struct_type {} [template]
  39. // CHECK:STDOUT: %F.type.3: type = fn_type @F.3 [template]
  40. // CHECK:STDOUT: %F.3: %F.type.3 = struct_value () [template]
  41. // CHECK:STDOUT: %.7: type = ptr_type %.6 [template]
  42. // CHECK:STDOUT: }
  43. // CHECK:STDOUT:
  44. // CHECK:STDOUT: imports {
  45. // CHECK:STDOUT: %import_ref.1: %Int32.type = import_ref ir3, inst+4, loaded [template = constants.%Int32]
  46. // CHECK:STDOUT: %import_ref.2: %Int32.type = import_ref ir3, inst+4, loaded [template = constants.%Int32]
  47. // CHECK:STDOUT: %import_ref.3: %Int32.type = import_ref ir3, inst+4, loaded [template = constants.%Int32]
  48. // CHECK:STDOUT: }
  49. // CHECK:STDOUT:
  50. // CHECK:STDOUT: file {
  51. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  52. // CHECK:STDOUT: .Core = %Core
  53. // CHECK:STDOUT: .C = %C.decl.loc11
  54. // CHECK:STDOUT: .I = %I.decl
  55. // CHECK:STDOUT: .F = %F.decl
  56. // CHECK:STDOUT: }
  57. // CHECK:STDOUT: %Core.import = import Core
  58. // CHECK:STDOUT: %Core: <namespace> = namespace %Core.import, [template] {}
  59. // CHECK:STDOUT: %C.decl.loc11: type = class_decl @C [template = constants.%C] {}
  60. // CHECK:STDOUT: %I.decl: type = interface_decl @I [template = constants.%.1] {}
  61. // CHECK:STDOUT: %C.decl.loc17: type = class_decl @C [template = constants.%C] {}
  62. // CHECK:STDOUT: %F.decl: %F.type.3 = fn_decl @F.3 [template = constants.%F.3] {
  63. // CHECK:STDOUT: %C.ref: type = name_ref C, %C.decl.loc11 [template = constants.%C]
  64. // CHECK:STDOUT: %c.loc23_6.1: %C = param c
  65. // CHECK:STDOUT: @F.3.%c: %C = bind_name c, %c.loc23_6.1
  66. // CHECK:STDOUT: %int.make_type_32: init type = call constants.%Int32() [template = i32]
  67. // CHECK:STDOUT: %.loc23_15.1: type = value_of_initializer %int.make_type_32 [template = i32]
  68. // CHECK:STDOUT: %.loc23_15.2: type = converted %int.make_type_32, %.loc23_15.1 [template = i32]
  69. // CHECK:STDOUT: @F.3.%return: ref i32 = var <return slot>
  70. // CHECK:STDOUT: }
  71. // CHECK:STDOUT: }
  72. // CHECK:STDOUT:
  73. // CHECK:STDOUT: interface @I {
  74. // CHECK:STDOUT: %Self: %.1 = bind_symbolic_name Self 0 [symbolic = constants.%Self]
  75. // CHECK:STDOUT: %F.decl: %F.type.1 = fn_decl @F.1 [template = constants.%F.1] {
  76. // CHECK:STDOUT: %Self.ref: %.1 = name_ref Self, %Self [symbolic = %Self.ref (constants.%Self)]
  77. // CHECK:STDOUT: %.loc14_14.1: type = facet_type_access %Self.ref [symbolic = %Self.ref (constants.%Self)]
  78. // CHECK:STDOUT: %.loc14_14.2: type = converted %Self.ref, %.loc14_14.1 [symbolic = %Self.ref (constants.%Self)]
  79. // CHECK:STDOUT: %self.loc14_8.1: @I.%Self.ref (%Self) = param self
  80. // CHECK:STDOUT: %self.loc14_8.2: @I.%Self.ref (%Self) = bind_name self, %self.loc14_8.1
  81. // CHECK:STDOUT: %int.make_type_32: init type = call constants.%Int32() [template = i32]
  82. // CHECK:STDOUT: %.loc14_25.1: type = value_of_initializer %int.make_type_32 [template = i32]
  83. // CHECK:STDOUT: %.loc14_25.2: type = converted %int.make_type_32, %.loc14_25.1 [template = i32]
  84. // CHECK:STDOUT: %return.var: ref i32 = var <return slot>
  85. // CHECK:STDOUT: }
  86. // CHECK:STDOUT: %.loc14_28: %.3 = assoc_entity element0, %F.decl [template = constants.%.4]
  87. // CHECK:STDOUT:
  88. // CHECK:STDOUT: !members:
  89. // CHECK:STDOUT: .Self = %Self
  90. // CHECK:STDOUT: .F = %.loc14_28
  91. // CHECK:STDOUT: witness = (%F.decl)
  92. // CHECK:STDOUT: }
  93. // CHECK:STDOUT:
  94. // CHECK:STDOUT: impl @impl: %C as %.1 {
  95. // CHECK:STDOUT: %F.decl: %F.type.2 = fn_decl @F.2 [template = constants.%F.2] {
  96. // CHECK:STDOUT: %Self.ref: type = name_ref Self, constants.%C [template = constants.%C]
  97. // CHECK:STDOUT: %self.loc19_10.1: %C = param self
  98. // CHECK:STDOUT: %self.loc19_10.2: %C = bind_name self, %self.loc19_10.1
  99. // CHECK:STDOUT: %int.make_type_32: init type = call constants.%Int32() [template = i32]
  100. // CHECK:STDOUT: %.loc19_27.1: type = value_of_initializer %int.make_type_32 [template = i32]
  101. // CHECK:STDOUT: %.loc19_27.2: type = converted %int.make_type_32, %.loc19_27.1 [template = i32]
  102. // CHECK:STDOUT: %return.var: ref i32 = var <return slot>
  103. // CHECK:STDOUT: }
  104. // CHECK:STDOUT: %.1: <witness> = interface_witness (%F.decl) [template = constants.%.5]
  105. // CHECK:STDOUT:
  106. // CHECK:STDOUT: !members:
  107. // CHECK:STDOUT: .F = %F.decl
  108. // CHECK:STDOUT: witness = %.1
  109. // CHECK:STDOUT: }
  110. // CHECK:STDOUT:
  111. // CHECK:STDOUT: class @C {
  112. // CHECK:STDOUT: impl_decl @impl {
  113. // CHECK:STDOUT: %I.ref: type = name_ref I, file.%I.decl [template = constants.%.1]
  114. // CHECK:STDOUT: }
  115. // CHECK:STDOUT:
  116. // CHECK:STDOUT: !members:
  117. // CHECK:STDOUT: .Self = constants.%C
  118. // CHECK:STDOUT: extend name_scope2
  119. // CHECK:STDOUT: }
  120. // CHECK:STDOUT:
  121. // CHECK:STDOUT: fn @Int32() -> type = "int.make_type_32";
  122. // CHECK:STDOUT:
  123. // CHECK:STDOUT: fn @F.1[@I.%self.loc14_8.2: @I.%Self.ref (%Self)]() -> i32
  124. // CHECK:STDOUT: generic [@I.%Self: %.1];
  125. // CHECK:STDOUT:
  126. // CHECK:STDOUT: fn @F.2[@impl.%self.loc19_10.2: %C]() -> i32;
  127. // CHECK:STDOUT:
  128. // CHECK:STDOUT: fn @F.3(%c: %C) -> i32 {
  129. // CHECK:STDOUT: !entry:
  130. // CHECK:STDOUT: %c.ref: %C = name_ref c, %c
  131. // CHECK:STDOUT: %F.ref: %.3 = name_ref F, @I.%.loc14_28 [template = constants.%.4]
  132. // CHECK:STDOUT: %.1: %F.type.1 = interface_witness_access @impl.%.1, element0 [template = constants.%F.2]
  133. // CHECK:STDOUT: %.loc24_11: <bound method> = bound_method %c.ref, %.1
  134. // CHECK:STDOUT: %F.call: init i32 = call %.loc24_11(%c.ref)
  135. // CHECK:STDOUT: %.loc24_15.1: i32 = value_of_initializer %F.call
  136. // CHECK:STDOUT: %.loc24_15.2: i32 = converted %F.call, %.loc24_15.1
  137. // CHECK:STDOUT: return %.loc24_15.2
  138. // CHECK:STDOUT: }
  139. // CHECK:STDOUT:
  140. // CHECK:STDOUT: specific @I.%F.decl(constants.%Self) {
  141. // CHECK:STDOUT: declaration:
  142. // CHECK:STDOUT: @I.%Self.ref => constants.%Self
  143. // CHECK:STDOUT: }
  144. // CHECK:STDOUT: