compound.carbon 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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: %Simple.type: type = facet_type <@Simple> [template]
  34. // CHECK:STDOUT: %Self.3c9: %Simple.type = bind_symbolic_name Self, 0 [symbolic]
  35. // CHECK:STDOUT: %F.type.e2e: type = fn_type @F.1 [template]
  36. // CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [template]
  37. // CHECK:STDOUT: %F.df8: %F.type.e2e = struct_value () [template]
  38. // CHECK:STDOUT: %Simple.assoc_type: type = assoc_entity_type %Simple.type [template]
  39. // CHECK:STDOUT: %assoc0.9ca: %Simple.assoc_type = assoc_entity element0, @Simple.%F.decl [template]
  40. // CHECK:STDOUT: %Self.as_type.716: type = facet_access_type %Self.3c9 [symbolic]
  41. // CHECK:STDOUT: %G.type.b60: type = fn_type @G.1 [template]
  42. // CHECK:STDOUT: %G.cb0: %G.type.b60 = struct_value () [template]
  43. // CHECK:STDOUT: %assoc1: %Simple.assoc_type = assoc_entity element1, @Simple.%G.decl [template]
  44. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [template]
  45. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [template]
  46. // CHECK:STDOUT: %impl_witness.5b2: <witness> = impl_witness (@impl.1.%F.decl, @impl.1.%G.decl) [template]
  47. // CHECK:STDOUT: %F.type.758: type = fn_type @F.2 [template]
  48. // CHECK:STDOUT: %F.df1: %F.type.758 = struct_value () [template]
  49. // CHECK:STDOUT: %G.type.c98: type = fn_type @G.2 [template]
  50. // CHECK:STDOUT: %G.e73: %G.type.c98 = struct_value () [template]
  51. // CHECK:STDOUT: %Simple.facet: %Simple.type = facet_value %i32, %impl_witness.5b2 [template]
  52. // CHECK:STDOUT: %NonInstanceCall.type: type = fn_type @NonInstanceCall [template]
  53. // CHECK:STDOUT: %NonInstanceCall: %NonInstanceCall.type = struct_value () [template]
  54. // CHECK:STDOUT: %.187: type = fn_type_with_self_type %F.type.e2e, %Simple.facet [template]
  55. // CHECK:STDOUT: %InstanceCall.type: type = fn_type @InstanceCall [template]
  56. // CHECK:STDOUT: %InstanceCall: %InstanceCall.type = struct_value () [template]
  57. // CHECK:STDOUT: %.b73: type = fn_type_with_self_type %G.type.b60, %Simple.facet [template]
  58. // CHECK:STDOUT: %ptr: type = ptr_type %i32 [template]
  59. // CHECK:STDOUT: %NonInstanceCallIndirect.type: type = fn_type @NonInstanceCallIndirect [template]
  60. // CHECK:STDOUT: %NonInstanceCallIndirect: %NonInstanceCallIndirect.type = struct_value () [template]
  61. // CHECK:STDOUT: %InstanceCallIndirect.type: type = fn_type @InstanceCallIndirect [template]
  62. // CHECK:STDOUT: %InstanceCallIndirect: %InstanceCallIndirect.type = struct_value () [template]
  63. // CHECK:STDOUT: }
  64. // CHECK:STDOUT:
  65. // CHECK:STDOUT: imports {
  66. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  67. // CHECK:STDOUT: .Int = %Core.Int
  68. // CHECK:STDOUT: import Core//prelude
  69. // CHECK:STDOUT: import Core//prelude/...
  70. // CHECK:STDOUT: }
  71. // CHECK:STDOUT: }
  72. // CHECK:STDOUT:
  73. // CHECK:STDOUT: file {
  74. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  75. // CHECK:STDOUT: .Core = imports.%Core
  76. // CHECK:STDOUT: .Simple = %Simple.decl
  77. // CHECK:STDOUT: .NonInstanceCall = %NonInstanceCall.decl
  78. // CHECK:STDOUT: .InstanceCall = %InstanceCall.decl
  79. // CHECK:STDOUT: .NonInstanceCallIndirect = %NonInstanceCallIndirect.decl
  80. // CHECK:STDOUT: .InstanceCallIndirect = %InstanceCallIndirect.decl
  81. // CHECK:STDOUT: }
  82. // CHECK:STDOUT: %Core.import = import Core
  83. // CHECK:STDOUT: %Simple.decl: type = interface_decl @Simple [template = constants.%Simple.type] {} {}
  84. // CHECK:STDOUT: impl_decl @impl.1 [template] {} {
  85. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  86. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  87. // CHECK:STDOUT: %Simple.ref: type = name_ref Simple, file.%Simple.decl [template = constants.%Simple.type]
  88. // CHECK:STDOUT: }
  89. // CHECK:STDOUT: %impl_witness: <witness> = impl_witness (@impl.1.%F.decl, @impl.1.%G.decl) [template = constants.%impl_witness.5b2]
  90. // CHECK:STDOUT: %NonInstanceCall.decl: %NonInstanceCall.type = fn_decl @NonInstanceCall [template = constants.%NonInstanceCall] {
  91. // CHECK:STDOUT: %n.patt: %i32 = binding_pattern n
  92. // CHECK:STDOUT: %n.param_patt: %i32 = value_param_pattern %n.patt, runtime_param0
  93. // CHECK:STDOUT: } {
  94. // CHECK:STDOUT: %n.param: %i32 = value_param runtime_param0
  95. // CHECK:STDOUT: %.loc21: type = splice_block %i32 [template = constants.%i32] {
  96. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  97. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  98. // CHECK:STDOUT: }
  99. // CHECK:STDOUT: %n: %i32 = bind_name n, %n.param
  100. // CHECK:STDOUT: }
  101. // CHECK:STDOUT: %InstanceCall.decl: %InstanceCall.type = fn_decl @InstanceCall [template = constants.%InstanceCall] {
  102. // CHECK:STDOUT: %n.patt: %i32 = binding_pattern n
  103. // CHECK:STDOUT: %n.param_patt: %i32 = value_param_pattern %n.patt, runtime_param0
  104. // CHECK:STDOUT: } {
  105. // CHECK:STDOUT: %n.param: %i32 = value_param runtime_param0
  106. // CHECK:STDOUT: %.loc25: type = splice_block %i32 [template = constants.%i32] {
  107. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  108. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  109. // CHECK:STDOUT: }
  110. // CHECK:STDOUT: %n: %i32 = bind_name n, %n.param
  111. // CHECK:STDOUT: }
  112. // CHECK:STDOUT: %NonInstanceCallIndirect.decl: %NonInstanceCallIndirect.type = fn_decl @NonInstanceCallIndirect [template = constants.%NonInstanceCallIndirect] {
  113. // CHECK:STDOUT: %p.patt: %ptr = binding_pattern p
  114. // CHECK:STDOUT: %p.param_patt: %ptr = value_param_pattern %p.patt, runtime_param0
  115. // CHECK:STDOUT: } {
  116. // CHECK:STDOUT: %p.param: %ptr = value_param runtime_param0
  117. // CHECK:STDOUT: %.loc29: type = splice_block %ptr [template = constants.%ptr] {
  118. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  119. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  120. // CHECK:STDOUT: %ptr: type = ptr_type %i32 [template = constants.%ptr]
  121. // CHECK:STDOUT: }
  122. // CHECK:STDOUT: %p: %ptr = bind_name p, %p.param
  123. // CHECK:STDOUT: }
  124. // CHECK:STDOUT: %InstanceCallIndirect.decl: %InstanceCallIndirect.type = fn_decl @InstanceCallIndirect [template = constants.%InstanceCallIndirect] {
  125. // CHECK:STDOUT: %p.patt: %ptr = binding_pattern p
  126. // CHECK:STDOUT: %p.param_patt: %ptr = value_param_pattern %p.patt, runtime_param0
  127. // CHECK:STDOUT: } {
  128. // CHECK:STDOUT: %p.param: %ptr = value_param runtime_param0
  129. // CHECK:STDOUT: %.loc33: type = splice_block %ptr [template = constants.%ptr] {
  130. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  131. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  132. // CHECK:STDOUT: %ptr: type = ptr_type %i32 [template = constants.%ptr]
  133. // CHECK:STDOUT: }
  134. // CHECK:STDOUT: %p: %ptr = bind_name p, %p.param
  135. // CHECK:STDOUT: }
  136. // CHECK:STDOUT: }
  137. // CHECK:STDOUT:
  138. // CHECK:STDOUT: interface @Simple {
  139. // CHECK:STDOUT: %Self: %Simple.type = bind_symbolic_name Self, 0 [symbolic = constants.%Self.3c9]
  140. // CHECK:STDOUT: %F.decl: %F.type.e2e = fn_decl @F.1 [template = constants.%F.df8] {} {}
  141. // CHECK:STDOUT: %assoc0: %Simple.assoc_type = assoc_entity element0, %F.decl [template = constants.%assoc0.9ca]
  142. // CHECK:STDOUT: %G.decl: %G.type.b60 = fn_decl @G.1 [template = constants.%G.cb0] {
  143. // CHECK:STDOUT: %self.patt: @G.1.%Self.as_type.loc13_14.1 (%Self.as_type.716) = binding_pattern self
  144. // CHECK:STDOUT: %self.param_patt: @G.1.%Self.as_type.loc13_14.1 (%Self.as_type.716) = value_param_pattern %self.patt, runtime_param0
  145. // CHECK:STDOUT: } {
  146. // CHECK:STDOUT: %self.param: @G.1.%Self.as_type.loc13_14.1 (%Self.as_type.716) = value_param runtime_param0
  147. // CHECK:STDOUT: %.loc13_14.1: type = splice_block %.loc13_14.2 [symbolic = %Self.as_type.loc13_14.1 (constants.%Self.as_type.716)] {
  148. // CHECK:STDOUT: %Self.ref: %Simple.type = name_ref Self, @Simple.%Self [symbolic = %Self (constants.%Self.3c9)]
  149. // CHECK:STDOUT: %Self.as_type.loc13_14.2: type = facet_access_type %Self.ref [symbolic = %Self.as_type.loc13_14.1 (constants.%Self.as_type.716)]
  150. // CHECK:STDOUT: %.loc13_14.2: type = converted %Self.ref, %Self.as_type.loc13_14.2 [symbolic = %Self.as_type.loc13_14.1 (constants.%Self.as_type.716)]
  151. // CHECK:STDOUT: }
  152. // CHECK:STDOUT: %self: @G.1.%Self.as_type.loc13_14.1 (%Self.as_type.716) = bind_name self, %self.param
  153. // CHECK:STDOUT: }
  154. // CHECK:STDOUT: %assoc1: %Simple.assoc_type = assoc_entity element1, %G.decl [template = constants.%assoc1]
  155. // CHECK:STDOUT:
  156. // CHECK:STDOUT: !members:
  157. // CHECK:STDOUT: .Self = %Self
  158. // CHECK:STDOUT: .F = %assoc0
  159. // CHECK:STDOUT: .G = %assoc1
  160. // CHECK:STDOUT: witness = (%F.decl, %G.decl)
  161. // CHECK:STDOUT: }
  162. // CHECK:STDOUT:
  163. // CHECK:STDOUT: impl @impl.1: %i32 as %Simple.ref {
  164. // CHECK:STDOUT: %F.decl: %F.type.758 = fn_decl @F.2 [template = constants.%F.df1] {} {}
  165. // CHECK:STDOUT: %G.decl: %G.type.c98 = fn_decl @G.2 [template = constants.%G.e73] {
  166. // CHECK:STDOUT: %self.patt: %i32 = binding_pattern self
  167. // CHECK:STDOUT: %self.param_patt: %i32 = value_param_pattern %self.patt, runtime_param0
  168. // CHECK:STDOUT: } {
  169. // CHECK:STDOUT: %self.param: %i32 = value_param runtime_param0
  170. // CHECK:STDOUT: %.loc18: type = splice_block %i32 [template = constants.%i32] {
  171. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  172. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  173. // CHECK:STDOUT: }
  174. // CHECK:STDOUT: %self: %i32 = bind_name self, %self.param
  175. // CHECK:STDOUT: }
  176. // CHECK:STDOUT:
  177. // CHECK:STDOUT: !members:
  178. // CHECK:STDOUT: .F = %F.decl
  179. // CHECK:STDOUT: .G = %G.decl
  180. // CHECK:STDOUT: witness = file.%impl_witness
  181. // CHECK:STDOUT: }
  182. // CHECK:STDOUT:
  183. // CHECK:STDOUT: generic fn @F.1(@Simple.%Self: %Simple.type) {
  184. // CHECK:STDOUT: fn();
  185. // CHECK:STDOUT: }
  186. // CHECK:STDOUT:
  187. // CHECK:STDOUT: generic fn @G.1(@Simple.%Self: %Simple.type) {
  188. // CHECK:STDOUT: %Self: %Simple.type = bind_symbolic_name Self, 0 [symbolic = %Self (constants.%Self.3c9)]
  189. // CHECK:STDOUT: %Self.as_type.loc13_14.1: type = facet_access_type %Self [symbolic = %Self.as_type.loc13_14.1 (constants.%Self.as_type.716)]
  190. // CHECK:STDOUT:
  191. // CHECK:STDOUT: fn[%self.param_patt: @G.1.%Self.as_type.loc13_14.1 (%Self.as_type.716)]();
  192. // CHECK:STDOUT: }
  193. // CHECK:STDOUT:
  194. // CHECK:STDOUT: fn @F.2();
  195. // CHECK:STDOUT:
  196. // CHECK:STDOUT: fn @G.2[%self.param_patt: %i32]();
  197. // CHECK:STDOUT:
  198. // CHECK:STDOUT: fn @NonInstanceCall(%n.param_patt: %i32) {
  199. // CHECK:STDOUT: !entry:
  200. // CHECK:STDOUT: %n.ref: %i32 = name_ref n, %n
  201. // CHECK:STDOUT: %Simple.ref: type = name_ref Simple, file.%Simple.decl [template = constants.%Simple.type]
  202. // CHECK:STDOUT: %F.ref: %Simple.assoc_type = name_ref F, @Simple.%assoc0 [template = constants.%assoc0.9ca]
  203. // CHECK:STDOUT: %impl.elem0: %.187 = impl_witness_access constants.%impl_witness.5b2, element0 [template = constants.%F.df1]
  204. // CHECK:STDOUT: %F.call: init %empty_tuple.type = call %impl.elem0()
  205. // CHECK:STDOUT: return
  206. // CHECK:STDOUT: }
  207. // CHECK:STDOUT:
  208. // CHECK:STDOUT: fn @InstanceCall(%n.param_patt: %i32) {
  209. // CHECK:STDOUT: !entry:
  210. // CHECK:STDOUT: %n.ref: %i32 = name_ref n, %n
  211. // CHECK:STDOUT: %Simple.ref: type = name_ref Simple, file.%Simple.decl [template = constants.%Simple.type]
  212. // CHECK:STDOUT: %G.ref: %Simple.assoc_type = name_ref G, @Simple.%assoc1 [template = constants.%assoc1]
  213. // CHECK:STDOUT: %impl.elem1: %.b73 = impl_witness_access constants.%impl_witness.5b2, element1 [template = constants.%G.e73]
  214. // CHECK:STDOUT: %bound_method: <bound method> = bound_method %n.ref, %impl.elem1
  215. // CHECK:STDOUT: %G.call: init %empty_tuple.type = call %bound_method(%n.ref)
  216. // CHECK:STDOUT: return
  217. // CHECK:STDOUT: }
  218. // CHECK:STDOUT:
  219. // CHECK:STDOUT: fn @NonInstanceCallIndirect(%p.param_patt: %ptr) {
  220. // CHECK:STDOUT: !entry:
  221. // CHECK:STDOUT: %p.ref: %ptr = name_ref p, %p
  222. // CHECK:STDOUT: %Simple.ref: type = name_ref Simple, file.%Simple.decl [template = constants.%Simple.type]
  223. // CHECK:STDOUT: %F.ref: %Simple.assoc_type = name_ref F, @Simple.%assoc0 [template = constants.%assoc0.9ca]
  224. // CHECK:STDOUT: %.loc30: ref %i32 = deref %p.ref
  225. // CHECK:STDOUT: %impl.elem0: %.187 = impl_witness_access constants.%impl_witness.5b2, element0 [template = constants.%F.df1]
  226. // CHECK:STDOUT: %F.call: init %empty_tuple.type = call %impl.elem0()
  227. // CHECK:STDOUT: return
  228. // CHECK:STDOUT: }
  229. // CHECK:STDOUT:
  230. // CHECK:STDOUT: fn @InstanceCallIndirect(%p.param_patt: %ptr) {
  231. // CHECK:STDOUT: !entry:
  232. // CHECK:STDOUT: %p.ref: %ptr = name_ref p, %p
  233. // CHECK:STDOUT: %Simple.ref: type = name_ref Simple, file.%Simple.decl [template = constants.%Simple.type]
  234. // CHECK:STDOUT: %G.ref: %Simple.assoc_type = name_ref G, @Simple.%assoc1 [template = constants.%assoc1]
  235. // CHECK:STDOUT: %.loc34_4.1: ref %i32 = deref %p.ref
  236. // CHECK:STDOUT: %impl.elem1: %.b73 = impl_witness_access constants.%impl_witness.5b2, element1 [template = constants.%G.e73]
  237. // CHECK:STDOUT: %bound_method: <bound method> = bound_method %.loc34_4.1, %impl.elem1
  238. // CHECK:STDOUT: %.loc34_4.2: %i32 = bind_value %.loc34_4.1
  239. // CHECK:STDOUT: %G.call: init %empty_tuple.type = call %bound_method(%.loc34_4.2)
  240. // CHECK:STDOUT: return
  241. // CHECK:STDOUT: }
  242. // CHECK:STDOUT:
  243. // CHECK:STDOUT: specific @F.1(constants.%Self.3c9) {}
  244. // CHECK:STDOUT:
  245. // CHECK:STDOUT: specific @G.1(constants.%Self.3c9) {
  246. // CHECK:STDOUT: %Self => constants.%Self.3c9
  247. // CHECK:STDOUT: %Self.as_type.loc13_14.1 => constants.%Self.as_type.716
  248. // CHECK:STDOUT: }
  249. // CHECK:STDOUT:
  250. // CHECK:STDOUT: specific @F.1(constants.%Simple.facet) {}
  251. // CHECK:STDOUT:
  252. // CHECK:STDOUT: specific @G.1(constants.%Simple.facet) {
  253. // CHECK:STDOUT: %Self => constants.%Simple.facet
  254. // CHECK:STDOUT: %Self.as_type.loc13_14.1 => constants.%i32
  255. // CHECK:STDOUT: }
  256. // CHECK:STDOUT: