import.carbon 15 KB

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