compound.carbon 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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/compound.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/impl/compound.carbon
  10. interface Simple {
  11. fn F();
  12. fn G[self: Self]();
  13. }
  14. impl i32 as Simple {
  15. fn F();
  16. fn G[self: i32]();
  17. }
  18. fn NonInstanceCall(n: i32) {
  19. n.(Simple.F)();
  20. }
  21. fn InstanceCall(n: i32) {
  22. n.(Simple.G)();
  23. }
  24. fn NonInstanceCallIndirect(p: i32*) {
  25. p->(Simple.F)();
  26. }
  27. fn InstanceCallIndirect(p: i32*) {
  28. p->(Simple.G)();
  29. }
  30. // CHECK:STDOUT: --- compound.carbon
  31. // CHECK:STDOUT:
  32. // CHECK:STDOUT: constants {
  33. // CHECK:STDOUT: %.1: type = interface_type @Simple [template]
  34. // CHECK:STDOUT: %Self: %.1 = bind_symbolic_name Self 0 [symbolic]
  35. // CHECK:STDOUT: %F.type.1: type = fn_type @F.1 [template]
  36. // CHECK:STDOUT: %.2: type = tuple_type () [template]
  37. // CHECK:STDOUT: %F.1: %F.type.1 = struct_value () [template]
  38. // CHECK:STDOUT: %.3: type = assoc_entity_type @Simple, %F.type.1 [template]
  39. // CHECK:STDOUT: %.4: %.3 = assoc_entity element0, @Simple.%F.decl [template]
  40. // CHECK:STDOUT: %G.type.1: type = fn_type @G.1 [template]
  41. // CHECK:STDOUT: %G.1: %G.type.1 = struct_value () [template]
  42. // CHECK:STDOUT: %.5: type = assoc_entity_type @Simple, %G.type.1 [template]
  43. // CHECK:STDOUT: %.6: %.5 = assoc_entity element1, @Simple.%G.decl [template]
  44. // CHECK:STDOUT: %Int32.type: type = fn_type @Int32 [template]
  45. // CHECK:STDOUT: %Int32: %Int32.type = struct_value () [template]
  46. // CHECK:STDOUT: %F.type.2: type = fn_type @F.2 [template]
  47. // CHECK:STDOUT: %F.2: %F.type.2 = struct_value () [template]
  48. // CHECK:STDOUT: %G.type.2: type = fn_type @G.2 [template]
  49. // CHECK:STDOUT: %G.2: %G.type.2 = struct_value () [template]
  50. // CHECK:STDOUT: %.7: <witness> = interface_witness (%F.2, %G.2) [template]
  51. // CHECK:STDOUT: %NonInstanceCall.type: type = fn_type @NonInstanceCall [template]
  52. // CHECK:STDOUT: %NonInstanceCall: %NonInstanceCall.type = struct_value () [template]
  53. // CHECK:STDOUT: %InstanceCall.type: type = fn_type @InstanceCall [template]
  54. // CHECK:STDOUT: %InstanceCall: %InstanceCall.type = struct_value () [template]
  55. // CHECK:STDOUT: %.8: type = ptr_type i32 [template]
  56. // CHECK:STDOUT: %NonInstanceCallIndirect.type: type = fn_type @NonInstanceCallIndirect [template]
  57. // CHECK:STDOUT: %NonInstanceCallIndirect: %NonInstanceCallIndirect.type = struct_value () [template]
  58. // CHECK:STDOUT: %InstanceCallIndirect.type: type = fn_type @InstanceCallIndirect [template]
  59. // CHECK:STDOUT: %InstanceCallIndirect: %InstanceCallIndirect.type = struct_value () [template]
  60. // CHECK:STDOUT: }
  61. // CHECK:STDOUT:
  62. // CHECK:STDOUT: file {
  63. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  64. // CHECK:STDOUT: .Core = %Core
  65. // CHECK:STDOUT: .Simple = %Simple.decl
  66. // CHECK:STDOUT: .NonInstanceCall = %NonInstanceCall.decl
  67. // CHECK:STDOUT: .InstanceCall = %InstanceCall.decl
  68. // CHECK:STDOUT: .NonInstanceCallIndirect = %NonInstanceCallIndirect.decl
  69. // CHECK:STDOUT: .InstanceCallIndirect = %InstanceCallIndirect.decl
  70. // CHECK:STDOUT: }
  71. // CHECK:STDOUT: %Core: <namespace> = namespace [template] {}
  72. // CHECK:STDOUT: %Simple.decl: type = interface_decl @Simple [template = constants.%.1] {}
  73. // CHECK:STDOUT: %import_ref.1: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  74. // CHECK:STDOUT: impl_decl @impl {
  75. // CHECK:STDOUT: %int.make_type_32.loc16: init type = call constants.%Int32() [template = i32]
  76. // CHECK:STDOUT: %.loc16_6.1: type = value_of_initializer %int.make_type_32.loc16 [template = i32]
  77. // CHECK:STDOUT: %.loc16_6.2: type = converted %int.make_type_32.loc16, %.loc16_6.1 [template = i32]
  78. // CHECK:STDOUT: %Simple.ref: type = name_ref Simple, %Simple.decl [template = constants.%.1]
  79. // CHECK:STDOUT: }
  80. // CHECK:STDOUT: %import_ref.2: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  81. // CHECK:STDOUT: %import_ref.3: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  82. // CHECK:STDOUT: %NonInstanceCall.decl: %NonInstanceCall.type = fn_decl @NonInstanceCall [template = constants.%NonInstanceCall] {
  83. // CHECK:STDOUT: %int.make_type_32.loc21: init type = call constants.%Int32() [template = i32]
  84. // CHECK:STDOUT: %.loc21_23.1: type = value_of_initializer %int.make_type_32.loc21 [template = i32]
  85. // CHECK:STDOUT: %.loc21_23.2: type = converted %int.make_type_32.loc21, %.loc21_23.1 [template = i32]
  86. // CHECK:STDOUT: %n.loc21_20.1: i32 = param n
  87. // CHECK:STDOUT: @NonInstanceCall.%n: i32 = bind_name n, %n.loc21_20.1
  88. // CHECK:STDOUT: }
  89. // CHECK:STDOUT: %import_ref.4: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  90. // CHECK:STDOUT: %InstanceCall.decl: %InstanceCall.type = fn_decl @InstanceCall [template = constants.%InstanceCall] {
  91. // CHECK:STDOUT: %int.make_type_32.loc25: init type = call constants.%Int32() [template = i32]
  92. // CHECK:STDOUT: %.loc25_20.1: type = value_of_initializer %int.make_type_32.loc25 [template = i32]
  93. // CHECK:STDOUT: %.loc25_20.2: type = converted %int.make_type_32.loc25, %.loc25_20.1 [template = i32]
  94. // CHECK:STDOUT: %n.loc25_17.1: i32 = param n
  95. // CHECK:STDOUT: @InstanceCall.%n: i32 = bind_name n, %n.loc25_17.1
  96. // CHECK:STDOUT: }
  97. // CHECK:STDOUT: %import_ref.5: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  98. // CHECK:STDOUT: %NonInstanceCallIndirect.decl: %NonInstanceCallIndirect.type = fn_decl @NonInstanceCallIndirect [template = constants.%NonInstanceCallIndirect] {
  99. // CHECK:STDOUT: %int.make_type_32.loc29: init type = call constants.%Int32() [template = i32]
  100. // CHECK:STDOUT: %.loc29_34.1: type = value_of_initializer %int.make_type_32.loc29 [template = i32]
  101. // CHECK:STDOUT: %.loc29_34.2: type = converted %int.make_type_32.loc29, %.loc29_34.1 [template = i32]
  102. // CHECK:STDOUT: %.loc29_34.3: type = ptr_type i32 [template = constants.%.8]
  103. // CHECK:STDOUT: %p.loc29_28.1: %.8 = param p
  104. // CHECK:STDOUT: @NonInstanceCallIndirect.%p: %.8 = bind_name p, %p.loc29_28.1
  105. // CHECK:STDOUT: }
  106. // CHECK:STDOUT: %import_ref.6: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  107. // CHECK:STDOUT: %InstanceCallIndirect.decl: %InstanceCallIndirect.type = fn_decl @InstanceCallIndirect [template = constants.%InstanceCallIndirect] {
  108. // CHECK:STDOUT: %int.make_type_32.loc33: init type = call constants.%Int32() [template = i32]
  109. // CHECK:STDOUT: %.loc33_31.1: type = value_of_initializer %int.make_type_32.loc33 [template = i32]
  110. // CHECK:STDOUT: %.loc33_31.2: type = converted %int.make_type_32.loc33, %.loc33_31.1 [template = i32]
  111. // CHECK:STDOUT: %.loc33_31.3: type = ptr_type i32 [template = constants.%.8]
  112. // CHECK:STDOUT: %p.loc33_25.1: %.8 = param p
  113. // CHECK:STDOUT: @InstanceCallIndirect.%p: %.8 = bind_name p, %p.loc33_25.1
  114. // CHECK:STDOUT: }
  115. // CHECK:STDOUT: }
  116. // CHECK:STDOUT:
  117. // CHECK:STDOUT: interface @Simple {
  118. // CHECK:STDOUT: %Self: %.1 = bind_symbolic_name Self 0 [symbolic = constants.%Self]
  119. // CHECK:STDOUT: %F.decl: %F.type.1 = fn_decl @F.1 [template = constants.%F.1] {}
  120. // CHECK:STDOUT: %.loc12: %.3 = assoc_entity element0, %F.decl [template = constants.%.4]
  121. // CHECK:STDOUT: %G.decl: %G.type.1 = fn_decl @G.1 [template = constants.%G.1] {
  122. // CHECK:STDOUT: %Self.ref: %.1 = name_ref Self, %Self [symbolic = constants.%Self]
  123. // CHECK:STDOUT: %.loc13_14.1: type = facet_type_access %Self.ref [symbolic = constants.%Self]
  124. // CHECK:STDOUT: %.loc13_14.2: type = converted %Self.ref, %.loc13_14.1 [symbolic = constants.%Self]
  125. // CHECK:STDOUT: %self.loc13_8.1: %Self = param self
  126. // CHECK:STDOUT: %self.loc13_8.2: %Self = bind_name self, %self.loc13_8.1
  127. // CHECK:STDOUT: }
  128. // CHECK:STDOUT: %.loc13_21: %.5 = assoc_entity element1, %G.decl [template = constants.%.6]
  129. // CHECK:STDOUT:
  130. // CHECK:STDOUT: !members:
  131. // CHECK:STDOUT: .Self = %Self
  132. // CHECK:STDOUT: .F = %.loc12
  133. // CHECK:STDOUT: .G = %.loc13_21
  134. // CHECK:STDOUT: witness = (%F.decl, %G.decl)
  135. // CHECK:STDOUT: }
  136. // CHECK:STDOUT:
  137. // CHECK:STDOUT: impl @impl: i32 as %.1 {
  138. // CHECK:STDOUT: %F.decl: %F.type.2 = fn_decl @F.2 [template = constants.%F.2] {}
  139. // CHECK:STDOUT: %G.decl: %G.type.2 = fn_decl @G.2 [template = constants.%G.2] {
  140. // CHECK:STDOUT: %int.make_type_32: init type = call constants.%Int32() [template = i32]
  141. // CHECK:STDOUT: %.loc18_14.1: type = value_of_initializer %int.make_type_32 [template = i32]
  142. // CHECK:STDOUT: %.loc18_14.2: type = converted %int.make_type_32, %.loc18_14.1 [template = i32]
  143. // CHECK:STDOUT: %self.loc18_8.1: i32 = param self
  144. // CHECK:STDOUT: %self.loc18_8.2: i32 = bind_name self, %self.loc18_8.1
  145. // CHECK:STDOUT: }
  146. // CHECK:STDOUT: %.1: <witness> = interface_witness (%F.decl, %G.decl) [template = constants.%.7]
  147. // CHECK:STDOUT:
  148. // CHECK:STDOUT: !members:
  149. // CHECK:STDOUT: .F = %F.decl
  150. // CHECK:STDOUT: .G = %G.decl
  151. // CHECK:STDOUT: witness = %.1
  152. // CHECK:STDOUT: }
  153. // CHECK:STDOUT:
  154. // CHECK:STDOUT: fn @F.1()
  155. // CHECK:STDOUT: generic [@Simple.%Self: %.1];
  156. // CHECK:STDOUT:
  157. // CHECK:STDOUT: fn @G.1[@Simple.%self.loc13_8.2: %Self]()
  158. // CHECK:STDOUT: generic [@Simple.%Self: %.1];
  159. // CHECK:STDOUT:
  160. // CHECK:STDOUT: fn @Int32() -> type = "int.make_type_32";
  161. // CHECK:STDOUT:
  162. // CHECK:STDOUT: fn @F.2();
  163. // CHECK:STDOUT:
  164. // CHECK:STDOUT: fn @G.2[@impl.%self.loc18_8.2: i32]();
  165. // CHECK:STDOUT:
  166. // CHECK:STDOUT: fn @NonInstanceCall(%n: i32) {
  167. // CHECK:STDOUT: !entry:
  168. // CHECK:STDOUT: %n.ref: i32 = name_ref n, %n
  169. // CHECK:STDOUT: %Simple.ref: type = name_ref Simple, file.%Simple.decl [template = constants.%.1]
  170. // CHECK:STDOUT: %F.ref: %.3 = name_ref F, @Simple.%.loc12 [template = constants.%.4]
  171. // CHECK:STDOUT: %.1: %F.type.1 = interface_witness_access @impl.%.1, element0 [template = constants.%F.2]
  172. // CHECK:STDOUT: %F.call: init %.2 = call %.1()
  173. // CHECK:STDOUT: return
  174. // CHECK:STDOUT: }
  175. // CHECK:STDOUT:
  176. // CHECK:STDOUT: fn @InstanceCall(%n: i32) {
  177. // CHECK:STDOUT: !entry:
  178. // CHECK:STDOUT: %n.ref: i32 = name_ref n, %n
  179. // CHECK:STDOUT: %Simple.ref: type = name_ref Simple, file.%Simple.decl [template = constants.%.1]
  180. // CHECK:STDOUT: %G.ref: %.5 = name_ref G, @Simple.%.loc13_21 [template = constants.%.6]
  181. // CHECK:STDOUT: %.1: %G.type.1 = interface_witness_access @impl.%.1, element1 [template = constants.%G.2]
  182. // CHECK:STDOUT: %.loc26: <bound method> = bound_method %n.ref, %.1
  183. // CHECK:STDOUT: %G.call: init %.2 = call %.loc26(%n.ref)
  184. // CHECK:STDOUT: return
  185. // CHECK:STDOUT: }
  186. // CHECK:STDOUT:
  187. // CHECK:STDOUT: fn @NonInstanceCallIndirect(%p: %.8) {
  188. // CHECK:STDOUT: !entry:
  189. // CHECK:STDOUT: %p.ref: %.8 = name_ref p, %p
  190. // CHECK:STDOUT: %Simple.ref: type = name_ref Simple, file.%Simple.decl [template = constants.%.1]
  191. // CHECK:STDOUT: %F.ref: %.3 = name_ref F, @Simple.%.loc12 [template = constants.%.4]
  192. // CHECK:STDOUT: %.loc30: ref i32 = deref %p.ref
  193. // CHECK:STDOUT: %.1: %F.type.1 = interface_witness_access @impl.%.1, element0 [template = constants.%F.2]
  194. // CHECK:STDOUT: %F.call: init %.2 = call %.1()
  195. // CHECK:STDOUT: return
  196. // CHECK:STDOUT: }
  197. // CHECK:STDOUT:
  198. // CHECK:STDOUT: fn @InstanceCallIndirect(%p: %.8) {
  199. // CHECK:STDOUT: !entry:
  200. // CHECK:STDOUT: %p.ref: %.8 = name_ref p, %p
  201. // CHECK:STDOUT: %Simple.ref: type = name_ref Simple, file.%Simple.decl [template = constants.%.1]
  202. // CHECK:STDOUT: %G.ref: %.5 = name_ref G, @Simple.%.loc13_21 [template = constants.%.6]
  203. // CHECK:STDOUT: %.loc34_4.1: ref i32 = deref %p.ref
  204. // CHECK:STDOUT: %.1: %G.type.1 = interface_witness_access @impl.%.1, element1 [template = constants.%G.2]
  205. // CHECK:STDOUT: %.loc34_4.2: <bound method> = bound_method %.loc34_4.1, %.1
  206. // CHECK:STDOUT: %.loc34_4.3: i32 = bind_value %.loc34_4.1
  207. // CHECK:STDOUT: %G.call: init %.2 = call %.loc34_4.2(%.loc34_4.3)
  208. // CHECK:STDOUT: return
  209. // CHECK:STDOUT: }
  210. // CHECK:STDOUT: