import_base.carbon 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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_base.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/class/import_base.carbon
  10. // --- a.carbon
  11. library "[[@TEST_NAME]]";
  12. base class Base {
  13. fn F[self: Self]();
  14. fn Unused[self: Self]();
  15. var x: i32;
  16. var unused: i32;
  17. }
  18. class Child {
  19. extend base: Base;
  20. }
  21. // --- b.carbon
  22. library "[[@TEST_NAME]]";
  23. import library "a";
  24. fn Run() {
  25. var a: Child = {.base = {.x = 0, .unused = 1}};
  26. a.x = 2;
  27. a.F();
  28. }
  29. // CHECK:STDOUT: --- a.carbon
  30. // CHECK:STDOUT:
  31. // CHECK:STDOUT: constants {
  32. // CHECK:STDOUT: %Base: type = class_type @Base [template]
  33. // CHECK:STDOUT: %F.type: type = fn_type @F [template]
  34. // CHECK:STDOUT: %F: %F.type = struct_value () [template]
  35. // CHECK:STDOUT: %Unused.type: type = fn_type @Unused [template]
  36. // CHECK:STDOUT: %Unused: %Unused.type = struct_value () [template]
  37. // CHECK:STDOUT: %.1: Core.IntLiteral = int_value 32 [template]
  38. // CHECK:STDOUT: %Int.type: type = fn_type @Int [template]
  39. // CHECK:STDOUT: %Int: %Int.type = struct_value () [template]
  40. // CHECK:STDOUT: %i32: type = int_type signed, %.1 [template]
  41. // CHECK:STDOUT: %.2: type = unbound_element_type %Base, %i32 [template]
  42. // CHECK:STDOUT: %.3: type = struct_type {.x: %i32, .unused: %i32} [template]
  43. // CHECK:STDOUT: %.4: <witness> = complete_type_witness %.3 [template]
  44. // CHECK:STDOUT: %Child: type = class_type @Child [template]
  45. // CHECK:STDOUT: %.6: type = unbound_element_type %Child, %Base [template]
  46. // CHECK:STDOUT: %.7: type = struct_type {.base: %Base} [template]
  47. // CHECK:STDOUT: %.8: <witness> = complete_type_witness %.7 [template]
  48. // CHECK:STDOUT: }
  49. // CHECK:STDOUT:
  50. // CHECK:STDOUT: imports {
  51. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  52. // CHECK:STDOUT: .Int = %import_ref
  53. // CHECK:STDOUT: import Core//prelude
  54. // CHECK:STDOUT: import Core//prelude/...
  55. // CHECK:STDOUT: }
  56. // CHECK:STDOUT: }
  57. // CHECK:STDOUT:
  58. // CHECK:STDOUT: file {
  59. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  60. // CHECK:STDOUT: .Core = imports.%Core
  61. // CHECK:STDOUT: .Base = %Base.decl
  62. // CHECK:STDOUT: .Child = %Child.decl
  63. // CHECK:STDOUT: }
  64. // CHECK:STDOUT: %Core.import = import Core
  65. // CHECK:STDOUT: %Base.decl: type = class_decl @Base [template = constants.%Base] {} {}
  66. // CHECK:STDOUT: %Child.decl: type = class_decl @Child [template = constants.%Child] {} {}
  67. // CHECK:STDOUT: }
  68. // CHECK:STDOUT:
  69. // CHECK:STDOUT: class @Base {
  70. // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [template = constants.%F] {
  71. // CHECK:STDOUT: %self.patt: %Base = binding_pattern self
  72. // CHECK:STDOUT: %self.param_patt: %Base = value_param_pattern %self.patt, runtime_param0
  73. // CHECK:STDOUT: } {
  74. // CHECK:STDOUT: %Self.ref: type = name_ref Self, constants.%Base [template = constants.%Base]
  75. // CHECK:STDOUT: %self.param: %Base = value_param runtime_param0
  76. // CHECK:STDOUT: %self: %Base = bind_name self, %self.param
  77. // CHECK:STDOUT: }
  78. // CHECK:STDOUT: %Unused.decl: %Unused.type = fn_decl @Unused [template = constants.%Unused] {
  79. // CHECK:STDOUT: %self.patt: %Base = binding_pattern self
  80. // CHECK:STDOUT: %self.param_patt: %Base = value_param_pattern %self.patt, runtime_param0
  81. // CHECK:STDOUT: } {
  82. // CHECK:STDOUT: %Self.ref: type = name_ref Self, constants.%Base [template = constants.%Base]
  83. // CHECK:STDOUT: %self.param: %Base = value_param runtime_param0
  84. // CHECK:STDOUT: %self: %Base = bind_name self, %self.param
  85. // CHECK:STDOUT: }
  86. // CHECK:STDOUT: %.loc8_10.1: Core.IntLiteral = int_value 32 [template = constants.%.1]
  87. // CHECK:STDOUT: %int.make_type_signed.loc8: init type = call constants.%Int(%.loc8_10.1) [template = constants.%i32]
  88. // CHECK:STDOUT: %.loc8_10.2: type = value_of_initializer %int.make_type_signed.loc8 [template = constants.%i32]
  89. // CHECK:STDOUT: %.loc8_10.3: type = converted %int.make_type_signed.loc8, %.loc8_10.2 [template = constants.%i32]
  90. // CHECK:STDOUT: %.loc8_8: %.2 = field_decl x, element0 [template]
  91. // CHECK:STDOUT: %.loc9_15.1: Core.IntLiteral = int_value 32 [template = constants.%.1]
  92. // CHECK:STDOUT: %int.make_type_signed.loc9: init type = call constants.%Int(%.loc9_15.1) [template = constants.%i32]
  93. // CHECK:STDOUT: %.loc9_15.2: type = value_of_initializer %int.make_type_signed.loc9 [template = constants.%i32]
  94. // CHECK:STDOUT: %.loc9_15.3: type = converted %int.make_type_signed.loc9, %.loc9_15.2 [template = constants.%i32]
  95. // CHECK:STDOUT: %.loc9_13: %.2 = field_decl unused, element1 [template]
  96. // CHECK:STDOUT: %.loc10: <witness> = complete_type_witness %.3 [template = constants.%.4]
  97. // CHECK:STDOUT:
  98. // CHECK:STDOUT: !members:
  99. // CHECK:STDOUT: .Self = constants.%Base
  100. // CHECK:STDOUT: .F = %F.decl
  101. // CHECK:STDOUT: .Unused = %Unused.decl
  102. // CHECK:STDOUT: .x = %.loc8_8
  103. // CHECK:STDOUT: .unused = %.loc9_13
  104. // CHECK:STDOUT: }
  105. // CHECK:STDOUT:
  106. // CHECK:STDOUT: class @Child {
  107. // CHECK:STDOUT: %Base.ref: type = name_ref Base, file.%Base.decl [template = constants.%Base]
  108. // CHECK:STDOUT: %.loc13: %.6 = base_decl %Base, element0 [template]
  109. // CHECK:STDOUT: %.loc14: <witness> = complete_type_witness %.7 [template = constants.%.8]
  110. // CHECK:STDOUT:
  111. // CHECK:STDOUT: !members:
  112. // CHECK:STDOUT: .Self = constants.%Child
  113. // CHECK:STDOUT: .base = %.loc13
  114. // CHECK:STDOUT: extend %Base.ref
  115. // CHECK:STDOUT: }
  116. // CHECK:STDOUT:
  117. // CHECK:STDOUT: fn @F[%self.param_patt: %Base]();
  118. // CHECK:STDOUT:
  119. // CHECK:STDOUT: fn @Unused[%self.param_patt: %Base]();
  120. // CHECK:STDOUT:
  121. // CHECK:STDOUT: --- b.carbon
  122. // CHECK:STDOUT:
  123. // CHECK:STDOUT: constants {
  124. // CHECK:STDOUT: %Run.type: type = fn_type @Run [template]
  125. // CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [template]
  126. // CHECK:STDOUT: %Run: %Run.type = struct_value () [template]
  127. // CHECK:STDOUT: %Child: type = class_type @Child [template]
  128. // CHECK:STDOUT: %Base: type = class_type @Base [template]
  129. // CHECK:STDOUT: %.1: Core.IntLiteral = int_value 32 [template]
  130. // CHECK:STDOUT: %i32: type = int_type signed, %.1 [template]
  131. // CHECK:STDOUT: %.10: Core.IntLiteral = int_value 0 [template]
  132. // CHECK:STDOUT: %.11: Core.IntLiteral = int_value 1 [template]
  133. // CHECK:STDOUT: %.12: type = struct_type {.x: Core.IntLiteral, .unused: Core.IntLiteral} [template]
  134. // CHECK:STDOUT: %.13: type = struct_type {.base: %.12} [template]
  135. // CHECK:STDOUT: %Convert.type.2: type = fn_type @Convert.1, @ImplicitAs(%i32) [template]
  136. // CHECK:STDOUT: %Convert.type.14: type = fn_type @Convert.2, @impl.1(%.1) [template]
  137. // CHECK:STDOUT: %Convert.14: %Convert.type.14 = struct_value () [template]
  138. // CHECK:STDOUT: %.37: <witness> = interface_witness (%Convert.14) [template]
  139. // CHECK:STDOUT: %.38: <bound method> = bound_method %.10, %Convert.14 [template]
  140. // CHECK:STDOUT: %.39: <specific function> = specific_function %.38, @Convert.2(%.1) [template]
  141. // CHECK:STDOUT: %.40: %i32 = int_value 0 [template]
  142. // CHECK:STDOUT: %.41: <bound method> = bound_method %.11, %Convert.14 [template]
  143. // CHECK:STDOUT: %.42: <specific function> = specific_function %.41, @Convert.2(%.1) [template]
  144. // CHECK:STDOUT: %.43: %i32 = int_value 1 [template]
  145. // CHECK:STDOUT: %struct.1: %Base = struct_value (%.40, %.43) [template]
  146. // CHECK:STDOUT: %struct.2: %Child = struct_value (%struct.1) [template]
  147. // CHECK:STDOUT: %.44: type = unbound_element_type %Base, %i32 [template]
  148. // CHECK:STDOUT: %.45: Core.IntLiteral = int_value 2 [template]
  149. // CHECK:STDOUT: %.46: <bound method> = bound_method %.45, %Convert.14 [template]
  150. // CHECK:STDOUT: %.47: <specific function> = specific_function %.46, @Convert.2(%.1) [template]
  151. // CHECK:STDOUT: %.48: %i32 = int_value 2 [template]
  152. // CHECK:STDOUT: %F.type: type = fn_type @F [template]
  153. // CHECK:STDOUT: %F: %F.type = struct_value () [template]
  154. // CHECK:STDOUT: }
  155. // CHECK:STDOUT:
  156. // CHECK:STDOUT: imports {
  157. // CHECK:STDOUT: %import_ref.1 = import_ref Main//a, inst+3, unloaded
  158. // CHECK:STDOUT: %import_ref.2: type = import_ref Main//a, inst+47, loaded [template = constants.%Child]
  159. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  160. // CHECK:STDOUT: .ImplicitAs = %import_ref.11
  161. // CHECK:STDOUT: import Core//prelude
  162. // CHECK:STDOUT: import Core//prelude/...
  163. // CHECK:STDOUT: }
  164. // CHECK:STDOUT: %import_ref.3 = import_ref Main//a, inst+4, unloaded
  165. // CHECK:STDOUT: %import_ref.4: %F.type = import_ref Main//a, inst+10, loaded [template = constants.%F]
  166. // CHECK:STDOUT: %import_ref.5 = import_ref Main//a, inst+19, unloaded
  167. // CHECK:STDOUT: %import_ref.6: %.44 = import_ref Main//a, inst+38, loaded [template = %.1]
  168. // CHECK:STDOUT: %import_ref.7 = import_ref Main//a, inst+43, unloaded
  169. // CHECK:STDOUT: %import_ref.8 = import_ref Main//a, inst+48, unloaded
  170. // CHECK:STDOUT: %import_ref.9 = import_ref Main//a, inst+52, unloaded
  171. // CHECK:STDOUT: %import_ref.10: type = import_ref Main//a, inst+49, loaded [template = constants.%Base]
  172. // CHECK:STDOUT: }
  173. // CHECK:STDOUT:
  174. // CHECK:STDOUT: file {
  175. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  176. // CHECK:STDOUT: .Base = imports.%import_ref.1
  177. // CHECK:STDOUT: .Child = imports.%import_ref.2
  178. // CHECK:STDOUT: .Core = imports.%Core
  179. // CHECK:STDOUT: .Run = %Run.decl
  180. // CHECK:STDOUT: }
  181. // CHECK:STDOUT: %Core.import = import Core
  182. // CHECK:STDOUT: %default.import = import <invalid>
  183. // CHECK:STDOUT: %Run.decl: %Run.type = fn_decl @Run [template = constants.%Run] {} {}
  184. // CHECK:STDOUT: }
  185. // CHECK:STDOUT:
  186. // CHECK:STDOUT: class @Child {
  187. // CHECK:STDOUT: !members:
  188. // CHECK:STDOUT: .Self = imports.%import_ref.8
  189. // CHECK:STDOUT: .base = imports.%import_ref.9
  190. // CHECK:STDOUT: extend imports.%import_ref.10
  191. // CHECK:STDOUT: }
  192. // CHECK:STDOUT:
  193. // CHECK:STDOUT: class @Base {
  194. // CHECK:STDOUT: !members:
  195. // CHECK:STDOUT: .Self = imports.%import_ref.3
  196. // CHECK:STDOUT: .F = imports.%import_ref.4
  197. // CHECK:STDOUT: .Unused = imports.%import_ref.5
  198. // CHECK:STDOUT: .x = imports.%import_ref.6
  199. // CHECK:STDOUT: .unused = imports.%import_ref.7
  200. // CHECK:STDOUT: }
  201. // CHECK:STDOUT:
  202. // CHECK:STDOUT: fn @Run() {
  203. // CHECK:STDOUT: !entry:
  204. // CHECK:STDOUT: %Child.ref: type = name_ref Child, imports.%import_ref.2 [template = constants.%Child]
  205. // CHECK:STDOUT: %a.var: ref %Child = var a
  206. // CHECK:STDOUT: %a: ref %Child = bind_name a, %a.var
  207. // CHECK:STDOUT: %.loc7_33: Core.IntLiteral = int_value 0 [template = constants.%.10]
  208. // CHECK:STDOUT: %.loc7_46: Core.IntLiteral = int_value 1 [template = constants.%.11]
  209. // CHECK:STDOUT: %.loc7_47.1: %.12 = struct_literal (%.loc7_33, %.loc7_46)
  210. // CHECK:STDOUT: %.loc7_48.1: %.13 = struct_literal (%.loc7_47.1)
  211. // CHECK:STDOUT: %.loc7_47.2: %Convert.type.2 = interface_witness_access constants.%.37, element0 [template = constants.%Convert.14]
  212. // CHECK:STDOUT: %.loc7_47.3: <bound method> = bound_method %.loc7_33, %.loc7_47.2 [template = constants.%.38]
  213. // CHECK:STDOUT: %.loc7_47.4: <specific function> = specific_function %.loc7_47.3, @Convert.2(constants.%.1) [template = constants.%.39]
  214. // CHECK:STDOUT: %int.convert_checked.loc7_47.1: init %i32 = call %.loc7_47.4(%.loc7_33) [template = constants.%.40]
  215. // CHECK:STDOUT: %.loc7_47.5: init %i32 = converted %.loc7_33, %int.convert_checked.loc7_47.1 [template = constants.%.40]
  216. // CHECK:STDOUT: %.loc7_48.2: ref %Base = class_element_access %a.var, element0
  217. // CHECK:STDOUT: %.loc7_47.6: ref %i32 = class_element_access %.loc7_48.2, element0
  218. // CHECK:STDOUT: %.loc7_47.7: init %i32 = initialize_from %.loc7_47.5 to %.loc7_47.6 [template = constants.%.40]
  219. // CHECK:STDOUT: %.loc7_47.8: %Convert.type.2 = interface_witness_access constants.%.37, element0 [template = constants.%Convert.14]
  220. // CHECK:STDOUT: %.loc7_47.9: <bound method> = bound_method %.loc7_46, %.loc7_47.8 [template = constants.%.41]
  221. // CHECK:STDOUT: %.loc7_47.10: <specific function> = specific_function %.loc7_47.9, @Convert.2(constants.%.1) [template = constants.%.42]
  222. // CHECK:STDOUT: %int.convert_checked.loc7_47.2: init %i32 = call %.loc7_47.10(%.loc7_46) [template = constants.%.43]
  223. // CHECK:STDOUT: %.loc7_47.11: init %i32 = converted %.loc7_46, %int.convert_checked.loc7_47.2 [template = constants.%.43]
  224. // CHECK:STDOUT: %.loc7_47.12: ref %i32 = class_element_access %.loc7_48.2, element1
  225. // CHECK:STDOUT: %.loc7_47.13: init %i32 = initialize_from %.loc7_47.11 to %.loc7_47.12 [template = constants.%.43]
  226. // CHECK:STDOUT: %.loc7_47.14: init %Base = class_init (%.loc7_47.7, %.loc7_47.13), %.loc7_48.2 [template = constants.%struct.1]
  227. // CHECK:STDOUT: %.loc7_48.3: init %Base = converted %.loc7_47.1, %.loc7_47.14 [template = constants.%struct.1]
  228. // CHECK:STDOUT: %.loc7_48.4: init %Child = class_init (%.loc7_48.3), %a.var [template = constants.%struct.2]
  229. // CHECK:STDOUT: %.loc7_49: init %Child = converted %.loc7_48.1, %.loc7_48.4 [template = constants.%struct.2]
  230. // CHECK:STDOUT: assign %a.var, %.loc7_49
  231. // CHECK:STDOUT: %a.ref.loc8: ref %Child = name_ref a, %a
  232. // CHECK:STDOUT: %x.ref: %.44 = name_ref x, imports.%import_ref.6 [template = imports.%.1]
  233. // CHECK:STDOUT: %.loc8_4.1: ref %Base = class_element_access %a.ref.loc8, element0
  234. // CHECK:STDOUT: %.loc8_4.2: ref %Base = converted %a.ref.loc8, %.loc8_4.1
  235. // CHECK:STDOUT: %.loc8_4.3: ref %i32 = class_element_access %.loc8_4.2, element0
  236. // CHECK:STDOUT: %.loc8_9: Core.IntLiteral = int_value 2 [template = constants.%.45]
  237. // CHECK:STDOUT: %.loc8_7.1: %Convert.type.2 = interface_witness_access constants.%.37, element0 [template = constants.%Convert.14]
  238. // CHECK:STDOUT: %.loc8_7.2: <bound method> = bound_method %.loc8_9, %.loc8_7.1 [template = constants.%.46]
  239. // CHECK:STDOUT: %.loc8_7.3: <specific function> = specific_function %.loc8_7.2, @Convert.2(constants.%.1) [template = constants.%.47]
  240. // CHECK:STDOUT: %int.convert_checked.loc8: init %i32 = call %.loc8_7.3(%.loc8_9) [template = constants.%.48]
  241. // CHECK:STDOUT: %.loc8_7.4: init %i32 = converted %.loc8_9, %int.convert_checked.loc8 [template = constants.%.48]
  242. // CHECK:STDOUT: assign %.loc8_4.3, %.loc8_7.4
  243. // CHECK:STDOUT: %a.ref.loc9: ref %Child = name_ref a, %a
  244. // CHECK:STDOUT: %F.ref: %F.type = name_ref F, imports.%import_ref.4 [template = constants.%F]
  245. // CHECK:STDOUT: %.loc9_4: <bound method> = bound_method %a.ref.loc9, %F.ref
  246. // CHECK:STDOUT: %.loc9_3.1: ref %Base = class_element_access %a.ref.loc9, element0
  247. // CHECK:STDOUT: %.loc9_3.2: ref %Base = converted %a.ref.loc9, %.loc9_3.1
  248. // CHECK:STDOUT: %.loc9_3.3: %Base = bind_value %.loc9_3.2
  249. // CHECK:STDOUT: %F.call: init %empty_tuple.type = call %.loc9_4(%.loc9_3.3)
  250. // CHECK:STDOUT: return
  251. // CHECK:STDOUT: }
  252. // CHECK:STDOUT:
  253. // CHECK:STDOUT: fn @F[%self.param_patt: %Base]();
  254. // CHECK:STDOUT: