import_self.carbon 9.0 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/no_prelude/import_self.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/impl/no_prelude/import_self.carbon
  10. // --- a.carbon
  11. library "a";
  12. interface Add {
  13. fn Op[self: Self](other: Self) -> Self;
  14. }
  15. // --- b.carbon
  16. library "b";
  17. import library "a";
  18. impl () as Add {
  19. fn Op[self: Self](other: Self) -> Self { return (); }
  20. }
  21. fn F(x: (), y: ()) -> () {
  22. return x.(Add.Op)(y);
  23. }
  24. // CHECK:STDOUT: --- a.carbon
  25. // CHECK:STDOUT:
  26. // CHECK:STDOUT: constants {
  27. // CHECK:STDOUT: %.1: type = interface_type @Add [template]
  28. // CHECK:STDOUT: %Self: %.1 = bind_symbolic_name Self 0 [symbolic]
  29. // CHECK:STDOUT: %Op.type: type = fn_type @Op [template]
  30. // CHECK:STDOUT: %.2: type = tuple_type () [template]
  31. // CHECK:STDOUT: %Op: %Op.type = struct_value () [template]
  32. // CHECK:STDOUT: %.3: type = assoc_entity_type @Add, %Op.type [template]
  33. // CHECK:STDOUT: %.4: %.3 = assoc_entity element0, @Add.%Op.decl [template]
  34. // CHECK:STDOUT: }
  35. // CHECK:STDOUT:
  36. // CHECK:STDOUT: file {
  37. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  38. // CHECK:STDOUT: .Add = %Add.decl
  39. // CHECK:STDOUT: }
  40. // CHECK:STDOUT: %Add.decl: type = interface_decl @Add [template = constants.%.1] {}
  41. // CHECK:STDOUT: }
  42. // CHECK:STDOUT:
  43. // CHECK:STDOUT: interface @Add {
  44. // CHECK:STDOUT: %Self: %.1 = bind_symbolic_name Self 0 [symbolic = constants.%Self]
  45. // CHECK:STDOUT: %Op.decl: %Op.type = fn_decl @Op [template = constants.%Op] {
  46. // CHECK:STDOUT: %Self.ref.loc5_15: %.1 = name_ref Self, %Self [symbolic = constants.%Self]
  47. // CHECK:STDOUT: %.loc5_15.1: type = facet_type_access %Self.ref.loc5_15 [symbolic = constants.%Self]
  48. // CHECK:STDOUT: %.loc5_15.2: type = converted %Self.ref.loc5_15, %.loc5_15.1 [symbolic = constants.%Self]
  49. // CHECK:STDOUT: %self.loc5_9.1: %Self = param self
  50. // CHECK:STDOUT: %self.loc5_9.2: %Self = bind_name self, %self.loc5_9.1
  51. // CHECK:STDOUT: %Self.ref.loc5_28: %.1 = name_ref Self, %Self [symbolic = constants.%Self]
  52. // CHECK:STDOUT: %.loc5_28.1: type = facet_type_access %Self.ref.loc5_28 [symbolic = constants.%Self]
  53. // CHECK:STDOUT: %.loc5_28.2: type = converted %Self.ref.loc5_28, %.loc5_28.1 [symbolic = constants.%Self]
  54. // CHECK:STDOUT: %other.loc5_21.1: %Self = param other
  55. // CHECK:STDOUT: %other.loc5_21.2: %Self = bind_name other, %other.loc5_21.1
  56. // CHECK:STDOUT: %Self.ref.loc5_37: %.1 = name_ref Self, %Self [symbolic = constants.%Self]
  57. // CHECK:STDOUT: %.loc5_37.1: type = facet_type_access %Self.ref.loc5_37 [symbolic = constants.%Self]
  58. // CHECK:STDOUT: %.loc5_37.2: type = converted %Self.ref.loc5_37, %.loc5_37.1 [symbolic = constants.%Self]
  59. // CHECK:STDOUT: %return.var: ref %Self = var <return slot>
  60. // CHECK:STDOUT: }
  61. // CHECK:STDOUT: %.loc5_41: %.3 = assoc_entity element0, %Op.decl [template = constants.%.4]
  62. // CHECK:STDOUT:
  63. // CHECK:STDOUT: !members:
  64. // CHECK:STDOUT: .Self = %Self
  65. // CHECK:STDOUT: .Op = %.loc5_41
  66. // CHECK:STDOUT: witness = (%Op.decl)
  67. // CHECK:STDOUT: }
  68. // CHECK:STDOUT:
  69. // CHECK:STDOUT: fn @Op[@Add.%self.loc5_9.2: %Self](@Add.%other.loc5_21.2: %Self) -> %Self
  70. // CHECK:STDOUT: generic [@Add.%Self: %.1];
  71. // CHECK:STDOUT:
  72. // CHECK:STDOUT: --- b.carbon
  73. // CHECK:STDOUT:
  74. // CHECK:STDOUT: constants {
  75. // CHECK:STDOUT: %.1: type = tuple_type () [template]
  76. // CHECK:STDOUT: %.2: type = interface_type @Add [template]
  77. // CHECK:STDOUT: %Self: %.2 = bind_symbolic_name Self 0 [symbolic]
  78. // CHECK:STDOUT: %Op.type.1: type = fn_type @Op.1 [template]
  79. // CHECK:STDOUT: %Op.1: %Op.type.1 = struct_value () [template]
  80. // CHECK:STDOUT: %Op.type.2: type = fn_type @Op.2 [template]
  81. // CHECK:STDOUT: %Op.2: %Op.type.2 = struct_value () [template]
  82. // CHECK:STDOUT: %.3: <witness> = interface_witness (%Op.1) [template]
  83. // CHECK:STDOUT: %tuple: %.1 = tuple_value () [template]
  84. // CHECK:STDOUT: %F.type: type = fn_type @F [template]
  85. // CHECK:STDOUT: %F: %F.type = struct_value () [template]
  86. // CHECK:STDOUT: %.4: type = assoc_entity_type @Add, %Op.type.2 [template]
  87. // CHECK:STDOUT: %.5: %.4 = assoc_entity element0, file.%import_ref.5 [template]
  88. // CHECK:STDOUT: }
  89. // CHECK:STDOUT:
  90. // CHECK:STDOUT: file {
  91. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  92. // CHECK:STDOUT: .Add = %import_ref.1
  93. // CHECK:STDOUT: .F = %F.decl
  94. // CHECK:STDOUT: }
  95. // CHECK:STDOUT: %import_ref.1: type = import_ref ir1, inst+1, loaded [template = constants.%.2]
  96. // CHECK:STDOUT: %import_ref.2 = import_ref ir1, inst+3, unloaded
  97. // CHECK:STDOUT: %import_ref.3: %.4 = import_ref ir1, inst+24, loaded [template = constants.%.5]
  98. // CHECK:STDOUT: %import_ref.4: %Op.type.2 = import_ref ir1, inst+19, loaded [template = constants.%Op.2]
  99. // CHECK:STDOUT: impl_decl @impl {
  100. // CHECK:STDOUT: %.loc6_7.1: %.1 = tuple_literal ()
  101. // CHECK:STDOUT: %.loc6_7.2: type = converted %.loc6_7.1, constants.%.1 [template = constants.%.1]
  102. // CHECK:STDOUT: %Add.ref: type = name_ref Add, %import_ref.1 [template = constants.%.2]
  103. // CHECK:STDOUT: }
  104. // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [template = constants.%F] {
  105. // CHECK:STDOUT: %.loc10_10.1: %.1 = tuple_literal ()
  106. // CHECK:STDOUT: %.loc10_10.2: type = converted %.loc10_10.1, constants.%.1 [template = constants.%.1]
  107. // CHECK:STDOUT: %x.loc10_6.1: %.1 = param x
  108. // CHECK:STDOUT: @F.%x: %.1 = bind_name x, %x.loc10_6.1
  109. // CHECK:STDOUT: %.loc10_17.1: %.1 = tuple_literal ()
  110. // CHECK:STDOUT: %.loc10_17.2: type = converted %.loc10_17.1, constants.%.1 [template = constants.%.1]
  111. // CHECK:STDOUT: %y.loc10_13.1: %.1 = param y
  112. // CHECK:STDOUT: @F.%y: %.1 = bind_name y, %y.loc10_13.1
  113. // CHECK:STDOUT: %.loc10_24.1: %.1 = tuple_literal ()
  114. // CHECK:STDOUT: %.loc10_24.2: type = converted %.loc10_24.1, constants.%.1 [template = constants.%.1]
  115. // CHECK:STDOUT: @F.%return: ref %.1 = var <return slot>
  116. // CHECK:STDOUT: }
  117. // CHECK:STDOUT: %import_ref.5 = import_ref ir1, inst+19, unloaded
  118. // CHECK:STDOUT: }
  119. // CHECK:STDOUT:
  120. // CHECK:STDOUT: interface @Add {
  121. // CHECK:STDOUT: !members:
  122. // CHECK:STDOUT: .Self = file.%import_ref.2
  123. // CHECK:STDOUT: .Op = file.%import_ref.3
  124. // CHECK:STDOUT: witness = (file.%import_ref.4)
  125. // CHECK:STDOUT: }
  126. // CHECK:STDOUT:
  127. // CHECK:STDOUT: impl @impl: %.1 as %.2 {
  128. // CHECK:STDOUT: %Op.decl: %Op.type.1 = fn_decl @Op.1 [template = constants.%Op.1] {
  129. // CHECK:STDOUT: %Self.ref.loc7_15: type = name_ref Self, constants.%.1 [template = constants.%.1]
  130. // CHECK:STDOUT: %self.loc7_9.1: %.1 = param self
  131. // CHECK:STDOUT: %self.loc7_9.2: %.1 = bind_name self, %self.loc7_9.1
  132. // CHECK:STDOUT: %Self.ref.loc7_28: type = name_ref Self, constants.%.1 [template = constants.%.1]
  133. // CHECK:STDOUT: %other.loc7_21.1: %.1 = param other
  134. // CHECK:STDOUT: %other.loc7_21.2: %.1 = bind_name other, %other.loc7_21.1
  135. // CHECK:STDOUT: %Self.ref.loc7_37: type = name_ref Self, constants.%.1 [template = constants.%.1]
  136. // CHECK:STDOUT: %return.var: ref %.1 = var <return slot>
  137. // CHECK:STDOUT: }
  138. // CHECK:STDOUT: %.1: <witness> = interface_witness (%Op.decl) [template = constants.%.3]
  139. // CHECK:STDOUT:
  140. // CHECK:STDOUT: !members:
  141. // CHECK:STDOUT: .Op = %Op.decl
  142. // CHECK:STDOUT: witness = %.1
  143. // CHECK:STDOUT: }
  144. // CHECK:STDOUT:
  145. // CHECK:STDOUT: fn @Op.1[@impl.%self.loc7_9.2: %.1](@impl.%other.loc7_21.2: %.1) -> %.1 {
  146. // CHECK:STDOUT: !entry:
  147. // CHECK:STDOUT: %.loc7_52: %.1 = tuple_literal ()
  148. // CHECK:STDOUT: %tuple: %.1 = tuple_value () [template = constants.%tuple]
  149. // CHECK:STDOUT: %.loc7_53: %.1 = converted %.loc7_52, %tuple [template = constants.%tuple]
  150. // CHECK:STDOUT: return %.loc7_53
  151. // CHECK:STDOUT: }
  152. // CHECK:STDOUT:
  153. // CHECK:STDOUT: fn @Op.2[%self: %Self](%other: %Self) -> %Self;
  154. // CHECK:STDOUT:
  155. // CHECK:STDOUT: fn @F(%x: %.1, %y: %.1) -> %.1 {
  156. // CHECK:STDOUT: !entry:
  157. // CHECK:STDOUT: %x.ref: %.1 = name_ref x, %x
  158. // CHECK:STDOUT: %Add.ref: type = name_ref Add, file.%import_ref.1 [template = constants.%.2]
  159. // CHECK:STDOUT: %Op.ref: %.4 = name_ref Op, file.%import_ref.3 [template = constants.%.5]
  160. // CHECK:STDOUT: %.1: %Op.type.2 = interface_witness_access @impl.%.1, element0 [template = constants.%Op.1]
  161. // CHECK:STDOUT: %.loc11_11: <bound method> = bound_method %x.ref, %.1
  162. // CHECK:STDOUT: %y.ref: %.1 = name_ref y, %y
  163. // CHECK:STDOUT: %Op.call: init %.1 = call %.loc11_11(%x.ref, %y.ref)
  164. // CHECK:STDOUT: %.loc11_20.1: ref %.1 = temporary_storage
  165. // CHECK:STDOUT: %.loc11_20.2: ref %.1 = temporary %.loc11_20.1, %Op.call
  166. // CHECK:STDOUT: %tuple: %.1 = tuple_value () [template = constants.%tuple]
  167. // CHECK:STDOUT: %.loc11_23: %.1 = converted %Op.call, %tuple [template = constants.%tuple]
  168. // CHECK:STDOUT: return %.loc11_23
  169. // CHECK:STDOUT: }
  170. // CHECK:STDOUT: