import.carbon 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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/class/import.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/class/import.carbon
  10. // --- a.carbon
  11. library "a";
  12. class Empty {
  13. }
  14. class Field {
  15. var x: i32;
  16. }
  17. class ForwardDeclared;
  18. class ForwardDeclared {
  19. fn F[self: Self]();
  20. fn G[addr self: Self*]();
  21. }
  22. class Incomplete;
  23. // --- b.carbon
  24. library "b";
  25. import library "a";
  26. fn Run() {
  27. var a: Empty = {};
  28. var b: Field = {.x = 1};
  29. b.x = 2;
  30. var c: ForwardDeclared = {};
  31. c.F();
  32. c.G();
  33. var d: ForwardDeclared* = &c;
  34. var e: Incomplete*;
  35. }
  36. // CHECK:STDOUT: --- a.carbon
  37. // CHECK:STDOUT:
  38. // CHECK:STDOUT: constants {
  39. // CHECK:STDOUT: %Empty: type = class_type @Empty [template]
  40. // CHECK:STDOUT: %.1: type = struct_type {} [template]
  41. // CHECK:STDOUT: %Field: type = class_type @Field [template]
  42. // CHECK:STDOUT: %Int32.type: type = fn_type @Int32 [template]
  43. // CHECK:STDOUT: %.2: type = tuple_type () [template]
  44. // CHECK:STDOUT: %Int32: %Int32.type = struct_value () [template]
  45. // CHECK:STDOUT: %.3: type = unbound_element_type %Field, i32 [template]
  46. // CHECK:STDOUT: %.4: type = struct_type {.x: i32} [template]
  47. // CHECK:STDOUT: %ForwardDeclared: type = class_type @ForwardDeclared [template]
  48. // CHECK:STDOUT: %F.type: type = fn_type @F [template]
  49. // CHECK:STDOUT: %F: %F.type = struct_value () [template]
  50. // CHECK:STDOUT: %.5: type = ptr_type %ForwardDeclared [template]
  51. // CHECK:STDOUT: %G.type: type = fn_type @G [template]
  52. // CHECK:STDOUT: %G: %G.type = struct_value () [template]
  53. // CHECK:STDOUT: %Incomplete: type = class_type @Incomplete [template]
  54. // CHECK:STDOUT: }
  55. // CHECK:STDOUT:
  56. // CHECK:STDOUT: file {
  57. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  58. // CHECK:STDOUT: .Core = %Core
  59. // CHECK:STDOUT: .Empty = %Empty.decl
  60. // CHECK:STDOUT: .Field = %Field.decl
  61. // CHECK:STDOUT: .ForwardDeclared = %ForwardDeclared.decl.loc11
  62. // CHECK:STDOUT: .Incomplete = %Incomplete.decl
  63. // CHECK:STDOUT: }
  64. // CHECK:STDOUT: %Core: <namespace> = namespace [template] {}
  65. // CHECK:STDOUT: %Empty.decl: type = class_decl @Empty [template = constants.%Empty] {}
  66. // CHECK:STDOUT: %Field.decl: type = class_decl @Field [template = constants.%Field] {}
  67. // CHECK:STDOUT: %import_ref: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  68. // CHECK:STDOUT: %ForwardDeclared.decl.loc11: type = class_decl @ForwardDeclared [template = constants.%ForwardDeclared] {}
  69. // CHECK:STDOUT: %ForwardDeclared.decl.loc13: type = class_decl @ForwardDeclared [template = constants.%ForwardDeclared] {}
  70. // CHECK:STDOUT: %Incomplete.decl: type = class_decl @Incomplete [template = constants.%Incomplete] {}
  71. // CHECK:STDOUT: }
  72. // CHECK:STDOUT:
  73. // CHECK:STDOUT: class @Empty {
  74. // CHECK:STDOUT: !members:
  75. // CHECK:STDOUT: .Self = constants.%Empty
  76. // CHECK:STDOUT: }
  77. // CHECK:STDOUT:
  78. // CHECK:STDOUT: class @Field {
  79. // CHECK:STDOUT: %int.make_type_32: init type = call constants.%Int32() [template = i32]
  80. // CHECK:STDOUT: %.loc8_10.1: type = value_of_initializer %int.make_type_32 [template = i32]
  81. // CHECK:STDOUT: %.loc8_10.2: type = converted %int.make_type_32, %.loc8_10.1 [template = i32]
  82. // CHECK:STDOUT: %.loc8_8: %.3 = field_decl x, element0 [template]
  83. // CHECK:STDOUT:
  84. // CHECK:STDOUT: !members:
  85. // CHECK:STDOUT: .Self = constants.%Field
  86. // CHECK:STDOUT: .x = %.loc8_8
  87. // CHECK:STDOUT: }
  88. // CHECK:STDOUT:
  89. // CHECK:STDOUT: class @ForwardDeclared {
  90. // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [template = constants.%F] {
  91. // CHECK:STDOUT: %Self.ref.loc14: type = name_ref Self, constants.%ForwardDeclared [template = constants.%ForwardDeclared]
  92. // CHECK:STDOUT: %self.loc14_8.1: %ForwardDeclared = param self
  93. // CHECK:STDOUT: %self.loc14_8.2: %ForwardDeclared = bind_name self, %self.loc14_8.1
  94. // CHECK:STDOUT: }
  95. // CHECK:STDOUT: %G.decl: %G.type = fn_decl @G [template = constants.%G] {
  96. // CHECK:STDOUT: %Self.ref.loc15: type = name_ref Self, constants.%ForwardDeclared [template = constants.%ForwardDeclared]
  97. // CHECK:STDOUT: %.loc15_23: type = ptr_type %ForwardDeclared [template = constants.%.5]
  98. // CHECK:STDOUT: %self.loc15_13.1: %.5 = param self
  99. // CHECK:STDOUT: %self.loc15_13.3: %.5 = bind_name self, %self.loc15_13.1
  100. // CHECK:STDOUT: %.loc15_8: %.5 = addr_pattern %self.loc15_13.3
  101. // CHECK:STDOUT: }
  102. // CHECK:STDOUT:
  103. // CHECK:STDOUT: !members:
  104. // CHECK:STDOUT: .Self = constants.%ForwardDeclared
  105. // CHECK:STDOUT: .F = %F.decl
  106. // CHECK:STDOUT: .G = %G.decl
  107. // CHECK:STDOUT: }
  108. // CHECK:STDOUT:
  109. // CHECK:STDOUT: class @Incomplete;
  110. // CHECK:STDOUT:
  111. // CHECK:STDOUT: fn @Int32() -> type = "int.make_type_32";
  112. // CHECK:STDOUT:
  113. // CHECK:STDOUT: fn @F[@ForwardDeclared.%self.loc14_8.2: %ForwardDeclared]();
  114. // CHECK:STDOUT:
  115. // CHECK:STDOUT: fn @G[addr @ForwardDeclared.%self.loc15_13.3: %.5]();
  116. // CHECK:STDOUT:
  117. // CHECK:STDOUT: --- b.carbon
  118. // CHECK:STDOUT:
  119. // CHECK:STDOUT: constants {
  120. // CHECK:STDOUT: %Run.type: type = fn_type @Run [template]
  121. // CHECK:STDOUT: %.1: type = tuple_type () [template]
  122. // CHECK:STDOUT: %Run: %Run.type = struct_value () [template]
  123. // CHECK:STDOUT: %Empty: type = class_type @Empty [template]
  124. // CHECK:STDOUT: %.2: type = struct_type {} [template]
  125. // CHECK:STDOUT: %.3: type = ptr_type %.2 [template]
  126. // CHECK:STDOUT: %struct.1: %Empty = struct_value () [template]
  127. // CHECK:STDOUT: %Field: type = class_type @Field [template]
  128. // CHECK:STDOUT: %.4: type = struct_type {.x: i32} [template]
  129. // CHECK:STDOUT: %.5: type = ptr_type %.4 [template]
  130. // CHECK:STDOUT: %.6: i32 = int_literal 1 [template]
  131. // CHECK:STDOUT: %struct.2: %Field = struct_value (%.6) [template]
  132. // CHECK:STDOUT: %.7: type = unbound_element_type %Field, i32 [template]
  133. // CHECK:STDOUT: %.8: i32 = int_literal 2 [template]
  134. // CHECK:STDOUT: %ForwardDeclared.1: type = class_type @ForwardDeclared.1 [template]
  135. // CHECK:STDOUT: %struct.3: %ForwardDeclared.1 = struct_value () [template]
  136. // CHECK:STDOUT: %ForwardDeclared.2: type = class_type @ForwardDeclared.2 [template]
  137. // CHECK:STDOUT: %F.type: type = fn_type @F [template]
  138. // CHECK:STDOUT: %F: %F.type = struct_value () [template]
  139. // CHECK:STDOUT: %.9: type = ptr_type %ForwardDeclared.1 [template]
  140. // CHECK:STDOUT: %G.type: type = fn_type @G [template]
  141. // CHECK:STDOUT: %G: %G.type = struct_value () [template]
  142. // CHECK:STDOUT: %Incomplete: type = class_type @Incomplete [template]
  143. // CHECK:STDOUT: %.10: type = ptr_type %Incomplete [template]
  144. // CHECK:STDOUT: }
  145. // CHECK:STDOUT:
  146. // CHECK:STDOUT: file {
  147. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  148. // CHECK:STDOUT: .Empty = %import_ref.1
  149. // CHECK:STDOUT: .Field = %import_ref.2
  150. // CHECK:STDOUT: .ForwardDeclared = %import_ref.3
  151. // CHECK:STDOUT: .Incomplete = %import_ref.4
  152. // CHECK:STDOUT: .Core = %Core
  153. // CHECK:STDOUT: .Run = %Run.decl
  154. // CHECK:STDOUT: }
  155. // CHECK:STDOUT: %import_ref.1: type = import_ref ir1, inst+2, loaded [template = constants.%Empty]
  156. // CHECK:STDOUT: %import_ref.2: type = import_ref ir1, inst+5, loaded [template = constants.%Field]
  157. // CHECK:STDOUT: %import_ref.3: type = import_ref ir1, inst+21, loaded [template = constants.%ForwardDeclared.1]
  158. // CHECK:STDOUT: %import_ref.4: type = import_ref ir1, inst+39, loaded [template = constants.%Incomplete]
  159. // CHECK:STDOUT: %Core: <namespace> = namespace [template] {}
  160. // CHECK:STDOUT: %Run.decl: %Run.type = fn_decl @Run [template = constants.%Run] {}
  161. // CHECK:STDOUT: %import_ref.5 = import_ref ir1, inst+3, unloaded
  162. // CHECK:STDOUT: %import_ref.6 = import_ref ir1, inst+6, unloaded
  163. // CHECK:STDOUT: %import_ref.7: %.7 = import_ref ir1, inst+17, loaded [template = imports.%.1]
  164. // CHECK:STDOUT: %import_ref.8 = import_ref ir1, inst+22, unloaded
  165. // CHECK:STDOUT: %import_ref.9: %F.type = import_ref ir1, inst+27, loaded [template = constants.%F]
  166. // CHECK:STDOUT: %import_ref.10: %G.type = import_ref ir1, inst+36, loaded [template = constants.%G]
  167. // CHECK:STDOUT: %import_ref.11 = import_ref ir1, inst+22, unloaded
  168. // CHECK:STDOUT: %import_ref.12 = import_ref ir1, inst+27, unloaded
  169. // CHECK:STDOUT: %import_ref.13 = import_ref ir1, inst+36, unloaded
  170. // CHECK:STDOUT: }
  171. // CHECK:STDOUT:
  172. // CHECK:STDOUT: class @Empty {
  173. // CHECK:STDOUT: !members:
  174. // CHECK:STDOUT: .Self = file.%import_ref.5
  175. // CHECK:STDOUT: }
  176. // CHECK:STDOUT:
  177. // CHECK:STDOUT: class @Field {
  178. // CHECK:STDOUT: !members:
  179. // CHECK:STDOUT: .Self = file.%import_ref.6
  180. // CHECK:STDOUT: .x = file.%import_ref.7
  181. // CHECK:STDOUT: }
  182. // CHECK:STDOUT:
  183. // CHECK:STDOUT: class @ForwardDeclared.1 {
  184. // CHECK:STDOUT: !members:
  185. // CHECK:STDOUT: .Self = file.%import_ref.8
  186. // CHECK:STDOUT: .F = file.%import_ref.9
  187. // CHECK:STDOUT: .G = file.%import_ref.10
  188. // CHECK:STDOUT: }
  189. // CHECK:STDOUT:
  190. // CHECK:STDOUT: class @ForwardDeclared.2 {
  191. // CHECK:STDOUT: !members:
  192. // CHECK:STDOUT: .Self = file.%import_ref.11
  193. // CHECK:STDOUT: .F = file.%import_ref.12
  194. // CHECK:STDOUT: .G = file.%import_ref.13
  195. // CHECK:STDOUT: }
  196. // CHECK:STDOUT:
  197. // CHECK:STDOUT: class @Incomplete;
  198. // CHECK:STDOUT:
  199. // CHECK:STDOUT: fn @Run() {
  200. // CHECK:STDOUT: !entry:
  201. // CHECK:STDOUT: %Empty.ref: type = name_ref Empty, file.%import_ref.1 [template = constants.%Empty]
  202. // CHECK:STDOUT: %a.var: ref %Empty = var a
  203. // CHECK:STDOUT: %a: ref %Empty = bind_name a, %a.var
  204. // CHECK:STDOUT: %.loc7_19.1: %.2 = struct_literal ()
  205. // CHECK:STDOUT: %.loc7_19.2: init %Empty = class_init (), %a.var [template = constants.%struct.1]
  206. // CHECK:STDOUT: %.loc7_20: init %Empty = converted %.loc7_19.1, %.loc7_19.2 [template = constants.%struct.1]
  207. // CHECK:STDOUT: assign %a.var, %.loc7_20
  208. // CHECK:STDOUT: %Field.ref: type = name_ref Field, file.%import_ref.2 [template = constants.%Field]
  209. // CHECK:STDOUT: %b.var: ref %Field = var b
  210. // CHECK:STDOUT: %b: ref %Field = bind_name b, %b.var
  211. // CHECK:STDOUT: %.loc9_24: i32 = int_literal 1 [template = constants.%.6]
  212. // CHECK:STDOUT: %.loc9_25.1: %.4 = struct_literal (%.loc9_24)
  213. // CHECK:STDOUT: %.loc9_25.2: ref i32 = class_element_access %b.var, element0
  214. // CHECK:STDOUT: %.loc9_25.3: init i32 = initialize_from %.loc9_24 to %.loc9_25.2 [template = constants.%.6]
  215. // CHECK:STDOUT: %.loc9_25.4: init %Field = class_init (%.loc9_25.3), %b.var [template = constants.%struct.2]
  216. // CHECK:STDOUT: %.loc9_26: init %Field = converted %.loc9_25.1, %.loc9_25.4 [template = constants.%struct.2]
  217. // CHECK:STDOUT: assign %b.var, %.loc9_26
  218. // CHECK:STDOUT: %b.ref: ref %Field = name_ref b, %b
  219. // CHECK:STDOUT: %x.ref: %.7 = name_ref x, file.%import_ref.7 [template = imports.%.1]
  220. // CHECK:STDOUT: %.loc10_4: ref i32 = class_element_access %b.ref, element0
  221. // CHECK:STDOUT: %.loc10_9: i32 = int_literal 2 [template = constants.%.8]
  222. // CHECK:STDOUT: assign %.loc10_4, %.loc10_9
  223. // CHECK:STDOUT: %ForwardDeclared.ref.loc12: type = name_ref ForwardDeclared, file.%import_ref.3 [template = constants.%ForwardDeclared.1]
  224. // CHECK:STDOUT: %c.var: ref %ForwardDeclared.1 = var c
  225. // CHECK:STDOUT: %c: ref %ForwardDeclared.1 = bind_name c, %c.var
  226. // CHECK:STDOUT: %.loc12_29.1: %.2 = struct_literal ()
  227. // CHECK:STDOUT: %.loc12_29.2: init %ForwardDeclared.1 = class_init (), %c.var [template = constants.%struct.3]
  228. // CHECK:STDOUT: %.loc12_30: init %ForwardDeclared.1 = converted %.loc12_29.1, %.loc12_29.2 [template = constants.%struct.3]
  229. // CHECK:STDOUT: assign %c.var, %.loc12_30
  230. // CHECK:STDOUT: %c.ref.loc13: ref %ForwardDeclared.1 = name_ref c, %c
  231. // CHECK:STDOUT: %F.ref: %F.type = name_ref F, file.%import_ref.9 [template = constants.%F]
  232. // CHECK:STDOUT: %.loc13_4: <bound method> = bound_method %c.ref.loc13, %F.ref
  233. // CHECK:STDOUT: %.loc13_3: %ForwardDeclared.1 = bind_value %c.ref.loc13
  234. // CHECK:STDOUT: %F.call: init %.1 = call %.loc13_4(%.loc13_3)
  235. // CHECK:STDOUT: %c.ref.loc14: ref %ForwardDeclared.1 = name_ref c, %c
  236. // CHECK:STDOUT: %G.ref: %G.type = name_ref G, file.%import_ref.10 [template = constants.%G]
  237. // CHECK:STDOUT: %.loc14_4: <bound method> = bound_method %c.ref.loc14, %G.ref
  238. // CHECK:STDOUT: %.loc14_3: %.9 = addr_of %c.ref.loc14
  239. // CHECK:STDOUT: %G.call: init %.1 = call %.loc14_4(%.loc14_3)
  240. // CHECK:STDOUT: %ForwardDeclared.ref.loc16: type = name_ref ForwardDeclared, file.%import_ref.3 [template = constants.%ForwardDeclared.1]
  241. // CHECK:STDOUT: %.loc16_25: type = ptr_type %ForwardDeclared.1 [template = constants.%.9]
  242. // CHECK:STDOUT: %d.var: ref %.9 = var d
  243. // CHECK:STDOUT: %d: ref %.9 = bind_name d, %d.var
  244. // CHECK:STDOUT: %c.ref.loc16: ref %ForwardDeclared.1 = name_ref c, %c
  245. // CHECK:STDOUT: %.loc16_29: %.9 = addr_of %c.ref.loc16
  246. // CHECK:STDOUT: assign %d.var, %.loc16_29
  247. // CHECK:STDOUT: %Incomplete.ref: type = name_ref Incomplete, file.%import_ref.4 [template = constants.%Incomplete]
  248. // CHECK:STDOUT: %.loc18: type = ptr_type %Incomplete [template = constants.%.10]
  249. // CHECK:STDOUT: %e.var: ref %.10 = var e
  250. // CHECK:STDOUT: %e: ref %.10 = bind_name e, %e.var
  251. // CHECK:STDOUT: return
  252. // CHECK:STDOUT: }
  253. // CHECK:STDOUT:
  254. // CHECK:STDOUT: fn @F[%self: %ForwardDeclared.1]();
  255. // CHECK:STDOUT:
  256. // CHECK:STDOUT: fn @G[addr %self: %.9]();
  257. // CHECK:STDOUT: