import.carbon 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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/interface/no_prelude/import.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/interface/no_prelude/import.carbon
  10. // --- a.carbon
  11. library "a";
  12. interface Empty {
  13. }
  14. interface Basic {
  15. let T:! type;
  16. fn F();
  17. }
  18. // TODO: Implement merging of interface on import.
  19. // interface ForwardDeclared;
  20. interface ForwardDeclared {
  21. let T:! type;
  22. fn F();
  23. }
  24. var f_ref: {.f: ForwardDeclared};
  25. // --- b.carbon
  26. library "b";
  27. import library "a";
  28. fn UseEmpty(e: Empty) {}
  29. fn UseBasic(e: Basic) {}
  30. fn UseForwardDeclared(f: ForwardDeclared) {}
  31. alias UseBasicT = Basic.T;
  32. alias UseBasicF = Basic.F;
  33. alias UseForwardDeclaredT = ForwardDeclared.T;
  34. alias UseForwardDeclaredF = ForwardDeclared.F;
  35. var f: ForwardDeclared* = &f_ref.f;
  36. // CHECK:STDOUT: --- a.carbon
  37. // CHECK:STDOUT:
  38. // CHECK:STDOUT: constants {
  39. // CHECK:STDOUT: %.1: type = interface_type @Empty [template]
  40. // CHECK:STDOUT: %Self.1: %.1 = bind_symbolic_name Self 0 [symbolic]
  41. // CHECK:STDOUT: %.2: type = interface_type @Basic [template]
  42. // CHECK:STDOUT: %Self.2: %.2 = bind_symbolic_name Self 0 [symbolic]
  43. // CHECK:STDOUT: %.3: type = assoc_entity_type @Basic, type [template]
  44. // CHECK:STDOUT: %.4: %.3 = assoc_entity element0, @Basic.%T [template]
  45. // CHECK:STDOUT: %F.type.1: type = fn_type @F.1 [template]
  46. // CHECK:STDOUT: %.5: type = tuple_type () [template]
  47. // CHECK:STDOUT: %F.1: %F.type.1 = struct_value () [template]
  48. // CHECK:STDOUT: %.6: type = assoc_entity_type @Basic, %F.type.1 [template]
  49. // CHECK:STDOUT: %.7: %.6 = assoc_entity element1, @Basic.%F.decl [template]
  50. // CHECK:STDOUT: %.8: type = interface_type @ForwardDeclared [template]
  51. // CHECK:STDOUT: %Self.3: %.8 = bind_symbolic_name Self 0 [symbolic]
  52. // CHECK:STDOUT: %.9: type = assoc_entity_type @ForwardDeclared, type [template]
  53. // CHECK:STDOUT: %.10: %.9 = assoc_entity element0, @ForwardDeclared.%T [template]
  54. // CHECK:STDOUT: %F.type.2: type = fn_type @F.2 [template]
  55. // CHECK:STDOUT: %F.2: %F.type.2 = struct_value () [template]
  56. // CHECK:STDOUT: %.11: type = assoc_entity_type @ForwardDeclared, %F.type.2 [template]
  57. // CHECK:STDOUT: %.12: %.11 = assoc_entity element1, @ForwardDeclared.%F.decl [template]
  58. // CHECK:STDOUT: %.13: type = struct_type {.f: %.8} [template]
  59. // CHECK:STDOUT: %.14: type = struct_type {.f: %.5} [template]
  60. // CHECK:STDOUT: }
  61. // CHECK:STDOUT:
  62. // CHECK:STDOUT: file {
  63. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  64. // CHECK:STDOUT: .Empty = %Empty.decl
  65. // CHECK:STDOUT: .Basic = %Basic.decl
  66. // CHECK:STDOUT: .ForwardDeclared = %ForwardDeclared.decl
  67. // CHECK:STDOUT: .f_ref = %f_ref
  68. // CHECK:STDOUT: }
  69. // CHECK:STDOUT: %Empty.decl: type = interface_decl @Empty [template = constants.%.1] {}
  70. // CHECK:STDOUT: %Basic.decl: type = interface_decl @Basic [template = constants.%.2] {}
  71. // CHECK:STDOUT: %ForwardDeclared.decl: type = interface_decl @ForwardDeclared [template = constants.%.8] {}
  72. // CHECK:STDOUT: %ForwardDeclared.ref: type = name_ref ForwardDeclared, %ForwardDeclared.decl [template = constants.%.8]
  73. // CHECK:STDOUT: %.loc20: type = struct_type {.f: %.8} [template = constants.%.13]
  74. // CHECK:STDOUT: %f_ref.var: ref %.13 = var f_ref
  75. // CHECK:STDOUT: %f_ref: ref %.13 = bind_name f_ref, %f_ref.var
  76. // CHECK:STDOUT: }
  77. // CHECK:STDOUT:
  78. // CHECK:STDOUT: interface @Empty {
  79. // CHECK:STDOUT: %Self: %.1 = bind_symbolic_name Self 0 [symbolic = constants.%Self.1]
  80. // CHECK:STDOUT:
  81. // CHECK:STDOUT: !members:
  82. // CHECK:STDOUT: .Self = %Self
  83. // CHECK:STDOUT: witness = ()
  84. // CHECK:STDOUT: }
  85. // CHECK:STDOUT:
  86. // CHECK:STDOUT: interface @Basic {
  87. // CHECK:STDOUT: %Self: %.2 = bind_symbolic_name Self 0 [symbolic = constants.%Self.2]
  88. // CHECK:STDOUT: %T: type = assoc_const_decl T [template]
  89. // CHECK:STDOUT: %.loc8: %.3 = assoc_entity element0, %T [template = constants.%.4]
  90. // CHECK:STDOUT: %F.decl: %F.type.1 = fn_decl @F.1 [template = constants.%F.1] {}
  91. // CHECK:STDOUT: %.loc9: %.6 = assoc_entity element1, %F.decl [template = constants.%.7]
  92. // CHECK:STDOUT:
  93. // CHECK:STDOUT: !members:
  94. // CHECK:STDOUT: .Self = %Self
  95. // CHECK:STDOUT: .T = %.loc8
  96. // CHECK:STDOUT: .F = %.loc9
  97. // CHECK:STDOUT: witness = (%T, %F.decl)
  98. // CHECK:STDOUT: }
  99. // CHECK:STDOUT:
  100. // CHECK:STDOUT: interface @ForwardDeclared {
  101. // CHECK:STDOUT: %Self: %.8 = bind_symbolic_name Self 0 [symbolic = constants.%Self.3]
  102. // CHECK:STDOUT: %T: type = assoc_const_decl T [template]
  103. // CHECK:STDOUT: %.loc16: %.9 = assoc_entity element0, %T [template = constants.%.10]
  104. // CHECK:STDOUT: %F.decl: %F.type.2 = fn_decl @F.2 [template = constants.%F.2] {}
  105. // CHECK:STDOUT: %.loc17: %.11 = assoc_entity element1, %F.decl [template = constants.%.12]
  106. // CHECK:STDOUT:
  107. // CHECK:STDOUT: !members:
  108. // CHECK:STDOUT: .Self = %Self
  109. // CHECK:STDOUT: .T = %.loc16
  110. // CHECK:STDOUT: .F = %.loc17
  111. // CHECK:STDOUT: witness = (%T, %F.decl)
  112. // CHECK:STDOUT: }
  113. // CHECK:STDOUT:
  114. // CHECK:STDOUT: fn @F.1()
  115. // CHECK:STDOUT: generic [@Basic.%Self: %.2];
  116. // CHECK:STDOUT:
  117. // CHECK:STDOUT: fn @F.2()
  118. // CHECK:STDOUT: generic [@ForwardDeclared.%Self: %.8];
  119. // CHECK:STDOUT:
  120. // CHECK:STDOUT: --- b.carbon
  121. // CHECK:STDOUT:
  122. // CHECK:STDOUT: constants {
  123. // CHECK:STDOUT: %.1: type = interface_type @Empty [template]
  124. // CHECK:STDOUT: %Self.1: %.1 = bind_symbolic_name Self 0 [symbolic]
  125. // CHECK:STDOUT: %UseEmpty.type: type = fn_type @UseEmpty [template]
  126. // CHECK:STDOUT: %.2: type = tuple_type () [template]
  127. // CHECK:STDOUT: %UseEmpty: %UseEmpty.type = struct_value () [template]
  128. // CHECK:STDOUT: %.3: type = interface_type @Basic [template]
  129. // CHECK:STDOUT: %Self.2: %.3 = bind_symbolic_name Self 0 [symbolic]
  130. // CHECK:STDOUT: %UseBasic.type: type = fn_type @UseBasic [template]
  131. // CHECK:STDOUT: %UseBasic: %UseBasic.type = struct_value () [template]
  132. // CHECK:STDOUT: %.4: type = interface_type @ForwardDeclared [template]
  133. // CHECK:STDOUT: %Self.3: %.4 = bind_symbolic_name Self 0 [symbolic]
  134. // CHECK:STDOUT: %UseForwardDeclared.type: type = fn_type @UseForwardDeclared [template]
  135. // CHECK:STDOUT: %UseForwardDeclared: %UseForwardDeclared.type = struct_value () [template]
  136. // CHECK:STDOUT: %.5: type = assoc_entity_type @Basic, type [template]
  137. // CHECK:STDOUT: %.6: %.5 = assoc_entity element0, file.%import_ref.16 [template]
  138. // CHECK:STDOUT: %F.type.1: type = fn_type @F.1 [template]
  139. // CHECK:STDOUT: %F.1: %F.type.1 = struct_value () [template]
  140. // CHECK:STDOUT: %.7: type = assoc_entity_type @Basic, %F.type.1 [template]
  141. // CHECK:STDOUT: %.8: %.7 = assoc_entity element1, file.%import_ref.17 [template]
  142. // CHECK:STDOUT: %.9: type = assoc_entity_type @ForwardDeclared, type [template]
  143. // CHECK:STDOUT: %.10: %.9 = assoc_entity element0, file.%import_ref.18 [template]
  144. // CHECK:STDOUT: %F.type.2: type = fn_type @F.2 [template]
  145. // CHECK:STDOUT: %F.2: %F.type.2 = struct_value () [template]
  146. // CHECK:STDOUT: %.11: type = assoc_entity_type @ForwardDeclared, %F.type.2 [template]
  147. // CHECK:STDOUT: %.12: %.11 = assoc_entity element1, file.%import_ref.19 [template]
  148. // CHECK:STDOUT: %.13: type = ptr_type %.4 [template]
  149. // CHECK:STDOUT: %.14: type = struct_type {.f: %.4} [template]
  150. // CHECK:STDOUT: %.15: type = struct_type {.f: %.2} [template]
  151. // CHECK:STDOUT: }
  152. // CHECK:STDOUT:
  153. // CHECK:STDOUT: file {
  154. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  155. // CHECK:STDOUT: .Empty = %import_ref.1
  156. // CHECK:STDOUT: .Basic = %import_ref.2
  157. // CHECK:STDOUT: .ForwardDeclared = %import_ref.3
  158. // CHECK:STDOUT: .f_ref = %import_ref.4
  159. // CHECK:STDOUT: .UseEmpty = %UseEmpty.decl
  160. // CHECK:STDOUT: .UseBasic = %UseBasic.decl
  161. // CHECK:STDOUT: .UseForwardDeclared = %UseForwardDeclared.decl
  162. // CHECK:STDOUT: .UseBasicT = %UseBasicT
  163. // CHECK:STDOUT: .UseBasicF = %UseBasicF
  164. // CHECK:STDOUT: .UseForwardDeclaredT = %UseForwardDeclaredT
  165. // CHECK:STDOUT: .UseForwardDeclaredF = %UseForwardDeclaredF
  166. // CHECK:STDOUT: .f = %f.loc16
  167. // CHECK:STDOUT: }
  168. // CHECK:STDOUT: %import_ref.1: type = import_ref ir1, inst+1, loaded [template = constants.%.1]
  169. // CHECK:STDOUT: %import_ref.2: type = import_ref ir1, inst+5, loaded [template = constants.%.3]
  170. // CHECK:STDOUT: %import_ref.3: type = import_ref ir1, inst+20, loaded [template = constants.%.4]
  171. // CHECK:STDOUT: %import_ref.4: ref %.14 = import_ref ir1, inst+42, loaded
  172. // CHECK:STDOUT: %import_ref.5 = import_ref ir1, inst+3, unloaded
  173. // CHECK:STDOUT: %UseEmpty.decl: %UseEmpty.type = fn_decl @UseEmpty [template = constants.%UseEmpty] {
  174. // CHECK:STDOUT: %Empty.ref: type = name_ref Empty, %import_ref.1 [template = constants.%.1]
  175. // CHECK:STDOUT: %e.loc6_13.1: %.1 = param e
  176. // CHECK:STDOUT: @UseEmpty.%e: %.1 = bind_name e, %e.loc6_13.1
  177. // CHECK:STDOUT: }
  178. // CHECK:STDOUT: %import_ref.6 = import_ref ir1, inst+7, unloaded
  179. // CHECK:STDOUT: %import_ref.7: %.5 = import_ref ir1, inst+11, loaded [template = constants.%.6]
  180. // CHECK:STDOUT: %import_ref.8: %.7 = import_ref ir1, inst+18, loaded [template = constants.%.8]
  181. // CHECK:STDOUT: %import_ref.9 = import_ref ir1, inst+9, unloaded
  182. // CHECK:STDOUT: %import_ref.10 = import_ref ir1, inst+13, unloaded
  183. // CHECK:STDOUT: %UseBasic.decl: %UseBasic.type = fn_decl @UseBasic [template = constants.%UseBasic] {
  184. // CHECK:STDOUT: %Basic.ref.loc7: type = name_ref Basic, %import_ref.2 [template = constants.%.3]
  185. // CHECK:STDOUT: %e.loc7_13.1: %.3 = param e
  186. // CHECK:STDOUT: @UseBasic.%e: %.3 = bind_name e, %e.loc7_13.1
  187. // CHECK:STDOUT: }
  188. // CHECK:STDOUT: %import_ref.11 = import_ref ir1, inst+22, unloaded
  189. // CHECK:STDOUT: %import_ref.12: %.9 = import_ref ir1, inst+26, loaded [template = constants.%.10]
  190. // CHECK:STDOUT: %import_ref.13: %.11 = import_ref ir1, inst+32, loaded [template = constants.%.12]
  191. // CHECK:STDOUT: %import_ref.14 = import_ref ir1, inst+24, unloaded
  192. // CHECK:STDOUT: %import_ref.15 = import_ref ir1, inst+28, unloaded
  193. // CHECK:STDOUT: %UseForwardDeclared.decl: %UseForwardDeclared.type = fn_decl @UseForwardDeclared [template = constants.%UseForwardDeclared] {
  194. // CHECK:STDOUT: %ForwardDeclared.ref.loc8: type = name_ref ForwardDeclared, %import_ref.3 [template = constants.%.4]
  195. // CHECK:STDOUT: %f.loc8_23.1: %.4 = param f
  196. // CHECK:STDOUT: @UseForwardDeclared.%f: %.4 = bind_name f, %f.loc8_23.1
  197. // CHECK:STDOUT: }
  198. // CHECK:STDOUT: %Basic.ref.loc10: type = name_ref Basic, %import_ref.2 [template = constants.%.3]
  199. // CHECK:STDOUT: %import_ref.16 = import_ref ir1, inst+9, unloaded
  200. // CHECK:STDOUT: %T.ref.loc10: %.5 = name_ref T, %import_ref.7 [template = constants.%.6]
  201. // CHECK:STDOUT: %UseBasicT: %.5 = bind_alias UseBasicT, %import_ref.7 [template = constants.%.6]
  202. // CHECK:STDOUT: %Basic.ref.loc11: type = name_ref Basic, %import_ref.2 [template = constants.%.3]
  203. // CHECK:STDOUT: %import_ref.17 = import_ref ir1, inst+13, unloaded
  204. // CHECK:STDOUT: %F.ref.loc11: %.7 = name_ref F, %import_ref.8 [template = constants.%.8]
  205. // CHECK:STDOUT: %UseBasicF: %.7 = bind_alias UseBasicF, %import_ref.8 [template = constants.%.8]
  206. // CHECK:STDOUT: %ForwardDeclared.ref.loc13: type = name_ref ForwardDeclared, %import_ref.3 [template = constants.%.4]
  207. // CHECK:STDOUT: %import_ref.18 = import_ref ir1, inst+24, unloaded
  208. // CHECK:STDOUT: %T.ref.loc13: %.9 = name_ref T, %import_ref.12 [template = constants.%.10]
  209. // CHECK:STDOUT: %UseForwardDeclaredT: %.9 = bind_alias UseForwardDeclaredT, %import_ref.12 [template = constants.%.10]
  210. // CHECK:STDOUT: %ForwardDeclared.ref.loc14: type = name_ref ForwardDeclared, %import_ref.3 [template = constants.%.4]
  211. // CHECK:STDOUT: %import_ref.19 = import_ref ir1, inst+28, unloaded
  212. // CHECK:STDOUT: %F.ref.loc14: %.11 = name_ref F, %import_ref.13 [template = constants.%.12]
  213. // CHECK:STDOUT: %UseForwardDeclaredF: %.11 = bind_alias UseForwardDeclaredF, %import_ref.13 [template = constants.%.12]
  214. // CHECK:STDOUT: %ForwardDeclared.ref.loc16: type = name_ref ForwardDeclared, %import_ref.3 [template = constants.%.4]
  215. // CHECK:STDOUT: %.loc16: type = ptr_type %.4 [template = constants.%.13]
  216. // CHECK:STDOUT: %f.var: ref %.13 = var f
  217. // CHECK:STDOUT: %f.loc16: ref %.13 = bind_name f, %f.var
  218. // CHECK:STDOUT: }
  219. // CHECK:STDOUT:
  220. // CHECK:STDOUT: interface @Empty {
  221. // CHECK:STDOUT: !members:
  222. // CHECK:STDOUT: .Self = file.%import_ref.5
  223. // CHECK:STDOUT: witness = ()
  224. // CHECK:STDOUT: }
  225. // CHECK:STDOUT:
  226. // CHECK:STDOUT: interface @Basic {
  227. // CHECK:STDOUT: !members:
  228. // CHECK:STDOUT: .Self = file.%import_ref.6
  229. // CHECK:STDOUT: .T = file.%import_ref.7
  230. // CHECK:STDOUT: .F = file.%import_ref.8
  231. // CHECK:STDOUT: witness = (file.%import_ref.9, file.%import_ref.10)
  232. // CHECK:STDOUT: }
  233. // CHECK:STDOUT:
  234. // CHECK:STDOUT: interface @ForwardDeclared {
  235. // CHECK:STDOUT: !members:
  236. // CHECK:STDOUT: .Self = file.%import_ref.11
  237. // CHECK:STDOUT: .T = file.%import_ref.12
  238. // CHECK:STDOUT: .F = file.%import_ref.13
  239. // CHECK:STDOUT: witness = (file.%import_ref.14, file.%import_ref.15)
  240. // CHECK:STDOUT: }
  241. // CHECK:STDOUT:
  242. // CHECK:STDOUT: fn @UseEmpty(%e: %.1) {
  243. // CHECK:STDOUT: !entry:
  244. // CHECK:STDOUT: return
  245. // CHECK:STDOUT: }
  246. // CHECK:STDOUT:
  247. // CHECK:STDOUT: fn @UseBasic(%e: %.3) {
  248. // CHECK:STDOUT: !entry:
  249. // CHECK:STDOUT: return
  250. // CHECK:STDOUT: }
  251. // CHECK:STDOUT:
  252. // CHECK:STDOUT: fn @UseForwardDeclared(%f: %.4) {
  253. // CHECK:STDOUT: !entry:
  254. // CHECK:STDOUT: return
  255. // CHECK:STDOUT: }
  256. // CHECK:STDOUT:
  257. // CHECK:STDOUT: fn @F.1();
  258. // CHECK:STDOUT:
  259. // CHECK:STDOUT: fn @F.2();
  260. // CHECK:STDOUT:
  261. // CHECK:STDOUT: fn @__global_init() {
  262. // CHECK:STDOUT: !entry:
  263. // CHECK:STDOUT: %f_ref.ref: ref %.14 = name_ref f_ref, file.%import_ref.4
  264. // CHECK:STDOUT: %.loc16_33: ref %.4 = struct_access %f_ref.ref, element0
  265. // CHECK:STDOUT: %.loc16_27: %.13 = addr_of %.loc16_33
  266. // CHECK:STDOUT: assign file.%f.var, %.loc16_27
  267. // CHECK:STDOUT: return
  268. // CHECK:STDOUT: }
  269. // CHECK:STDOUT: