import_base.carbon 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 "a";
  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 "b";
  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: %.1: type = tuple_type () [template]
  35. // CHECK:STDOUT: %F: %F.type = struct_value () [template]
  36. // CHECK:STDOUT: %Unused.type: type = fn_type @Unused [template]
  37. // CHECK:STDOUT: %Unused: %Unused.type = struct_value () [template]
  38. // CHECK:STDOUT: %Int32.type: type = fn_type @Int32 [template]
  39. // CHECK:STDOUT: %Int32: %Int32.type = struct_value () [template]
  40. // CHECK:STDOUT: %.2: type = unbound_element_type %Base, i32 [template]
  41. // CHECK:STDOUT: %.3: type = struct_type {.x: i32, .unused: i32} [template]
  42. // CHECK:STDOUT: %Child: type = class_type @Child [template]
  43. // CHECK:STDOUT: %.4: type = ptr_type %.3 [template]
  44. // CHECK:STDOUT: %.5: type = unbound_element_type %Child, %Base [template]
  45. // CHECK:STDOUT: %.6: type = struct_type {.base: %Base} [template]
  46. // CHECK:STDOUT: }
  47. // CHECK:STDOUT:
  48. // CHECK:STDOUT: file {
  49. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  50. // CHECK:STDOUT: .Core = %Core
  51. // CHECK:STDOUT: .Base = %Base.decl
  52. // CHECK:STDOUT: .Child = %Child.decl
  53. // CHECK:STDOUT: }
  54. // CHECK:STDOUT: %Core: <namespace> = namespace [template] {}
  55. // CHECK:STDOUT: %Base.decl: type = class_decl @Base [template = constants.%Base] {}
  56. // CHECK:STDOUT: %import_ref.1: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  57. // CHECK:STDOUT: %import_ref.2: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  58. // CHECK:STDOUT: %Child.decl: type = class_decl @Child [template = constants.%Child] {}
  59. // CHECK:STDOUT: }
  60. // CHECK:STDOUT:
  61. // CHECK:STDOUT: class @Base {
  62. // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [template = constants.%F] {
  63. // CHECK:STDOUT: %Self.ref.loc5: type = name_ref Self, constants.%Base [template = constants.%Base]
  64. // CHECK:STDOUT: %self.loc5_8.1: %Base = param self
  65. // CHECK:STDOUT: %self.loc5_8.2: %Base = bind_name self, %self.loc5_8.1
  66. // CHECK:STDOUT: }
  67. // CHECK:STDOUT: %Unused.decl: %Unused.type = fn_decl @Unused [template = constants.%Unused] {
  68. // CHECK:STDOUT: %Self.ref.loc6: type = name_ref Self, constants.%Base [template = constants.%Base]
  69. // CHECK:STDOUT: %self.loc6_13.1: %Base = param self
  70. // CHECK:STDOUT: %self.loc6_13.2: %Base = bind_name self, %self.loc6_13.1
  71. // CHECK:STDOUT: }
  72. // CHECK:STDOUT: %int.make_type_32.loc8: init type = call constants.%Int32() [template = i32]
  73. // CHECK:STDOUT: %.loc8_10.1: type = value_of_initializer %int.make_type_32.loc8 [template = i32]
  74. // CHECK:STDOUT: %.loc8_10.2: type = converted %int.make_type_32.loc8, %.loc8_10.1 [template = i32]
  75. // CHECK:STDOUT: %.loc8_8: %.2 = field_decl x, element0 [template]
  76. // CHECK:STDOUT: %int.make_type_32.loc9: init type = call constants.%Int32() [template = i32]
  77. // CHECK:STDOUT: %.loc9_15.1: type = value_of_initializer %int.make_type_32.loc9 [template = i32]
  78. // CHECK:STDOUT: %.loc9_15.2: type = converted %int.make_type_32.loc9, %.loc9_15.1 [template = i32]
  79. // CHECK:STDOUT: %.loc9_13: %.2 = field_decl unused, element1 [template]
  80. // CHECK:STDOUT:
  81. // CHECK:STDOUT: !members:
  82. // CHECK:STDOUT: .Self = constants.%Base
  83. // CHECK:STDOUT: .F = %F.decl
  84. // CHECK:STDOUT: .Unused = %Unused.decl
  85. // CHECK:STDOUT: .x = %.loc8_8
  86. // CHECK:STDOUT: .unused = %.loc9_13
  87. // CHECK:STDOUT: }
  88. // CHECK:STDOUT:
  89. // CHECK:STDOUT: class @Child {
  90. // CHECK:STDOUT: %Base.ref: type = name_ref Base, file.%Base.decl [template = constants.%Base]
  91. // CHECK:STDOUT: %.loc13: %.5 = base_decl %Base, element0 [template]
  92. // CHECK:STDOUT:
  93. // CHECK:STDOUT: !members:
  94. // CHECK:STDOUT: .Self = constants.%Child
  95. // CHECK:STDOUT: .base = %.loc13
  96. // CHECK:STDOUT: extend name_scope2
  97. // CHECK:STDOUT: }
  98. // CHECK:STDOUT:
  99. // CHECK:STDOUT: fn @F[@Base.%self.loc5_8.2: %Base]();
  100. // CHECK:STDOUT:
  101. // CHECK:STDOUT: fn @Unused[@Base.%self.loc6_13.2: %Base]();
  102. // CHECK:STDOUT:
  103. // CHECK:STDOUT: fn @Int32() -> type = "int.make_type_32";
  104. // CHECK:STDOUT:
  105. // CHECK:STDOUT: --- b.carbon
  106. // CHECK:STDOUT:
  107. // CHECK:STDOUT: constants {
  108. // CHECK:STDOUT: %Run.type: type = fn_type @Run [template]
  109. // CHECK:STDOUT: %.1: type = tuple_type () [template]
  110. // CHECK:STDOUT: %Run: %Run.type = struct_value () [template]
  111. // CHECK:STDOUT: %Child: type = class_type @Child [template]
  112. // CHECK:STDOUT: %Base: type = class_type @Base [template]
  113. // CHECK:STDOUT: %.2: type = struct_type {.x: i32, .unused: i32} [template]
  114. // CHECK:STDOUT: %.3: type = unbound_element_type %Child, %Base [template]
  115. // CHECK:STDOUT: %.4: type = struct_type {.base: %Base} [template]
  116. // CHECK:STDOUT: %.5: type = ptr_type %.2 [template]
  117. // CHECK:STDOUT: %.6: type = struct_type {.base: %.5} [template]
  118. // CHECK:STDOUT: %.7: type = ptr_type %.4 [template]
  119. // CHECK:STDOUT: %.8: i32 = int_literal 0 [template]
  120. // CHECK:STDOUT: %.9: i32 = int_literal 1 [template]
  121. // CHECK:STDOUT: %.10: type = struct_type {.base: %.2} [template]
  122. // CHECK:STDOUT: %struct.1: %Base = struct_value (%.8, %.9) [template]
  123. // CHECK:STDOUT: %struct.2: %Child = struct_value (%struct.1) [template]
  124. // CHECK:STDOUT: %.11: type = unbound_element_type %Base, i32 [template]
  125. // CHECK:STDOUT: %.12: i32 = int_literal 2 [template]
  126. // CHECK:STDOUT: %F.type: type = fn_type @F [template]
  127. // CHECK:STDOUT: %F: %F.type = struct_value () [template]
  128. // CHECK:STDOUT: }
  129. // CHECK:STDOUT:
  130. // CHECK:STDOUT: file {
  131. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  132. // CHECK:STDOUT: .Base = %import_ref.1
  133. // CHECK:STDOUT: .Child = %import_ref.2
  134. // CHECK:STDOUT: .Core = %Core
  135. // CHECK:STDOUT: .Run = %Run.decl
  136. // CHECK:STDOUT: }
  137. // CHECK:STDOUT: %import_ref.1 = import_ref ir1, inst+2, unloaded
  138. // CHECK:STDOUT: %import_ref.2: type = import_ref ir1, inst+37, loaded [template = constants.%Child]
  139. // CHECK:STDOUT: %Core: <namespace> = namespace [template] {}
  140. // CHECK:STDOUT: %Run.decl: %Run.type = fn_decl @Run [template = constants.%Run] {}
  141. // CHECK:STDOUT: %import_ref.3 = import_ref ir1, inst+3, unloaded
  142. // CHECK:STDOUT: %import_ref.4: %F.type = import_ref ir1, inst+7, loaded [template = constants.%F]
  143. // CHECK:STDOUT: %import_ref.5 = import_ref ir1, inst+14, unloaded
  144. // CHECK:STDOUT: %import_ref.6: %.11 = import_ref ir1, inst+26, loaded [template = imports.%.1]
  145. // CHECK:STDOUT: %import_ref.7 = import_ref ir1, inst+33, unloaded
  146. // CHECK:STDOUT: %import_ref.8 = import_ref ir1, inst+38, unloaded
  147. // CHECK:STDOUT: %import_ref.9 = import_ref ir1, inst+42, unloaded
  148. // CHECK:STDOUT: }
  149. // CHECK:STDOUT:
  150. // CHECK:STDOUT: class @Child {
  151. // CHECK:STDOUT: !members:
  152. // CHECK:STDOUT: .Self = file.%import_ref.8
  153. // CHECK:STDOUT: .base = file.%import_ref.9
  154. // CHECK:STDOUT: extend name_scope2
  155. // CHECK:STDOUT: }
  156. // CHECK:STDOUT:
  157. // CHECK:STDOUT: class @Base {
  158. // CHECK:STDOUT: !members:
  159. // CHECK:STDOUT: .Self = file.%import_ref.3
  160. // CHECK:STDOUT: .F = file.%import_ref.4
  161. // CHECK:STDOUT: .Unused = file.%import_ref.5
  162. // CHECK:STDOUT: .x = file.%import_ref.6
  163. // CHECK:STDOUT: .unused = file.%import_ref.7
  164. // CHECK:STDOUT: }
  165. // CHECK:STDOUT:
  166. // CHECK:STDOUT: fn @Run() {
  167. // CHECK:STDOUT: !entry:
  168. // CHECK:STDOUT: %Child.ref: type = name_ref Child, file.%import_ref.2 [template = constants.%Child]
  169. // CHECK:STDOUT: %a.var: ref %Child = var a
  170. // CHECK:STDOUT: %a: ref %Child = bind_name a, %a.var
  171. // CHECK:STDOUT: %.loc7_33: i32 = int_literal 0 [template = constants.%.8]
  172. // CHECK:STDOUT: %.loc7_46: i32 = int_literal 1 [template = constants.%.9]
  173. // CHECK:STDOUT: %.loc7_47.1: %.2 = struct_literal (%.loc7_33, %.loc7_46)
  174. // CHECK:STDOUT: %.loc7_48.1: %.10 = struct_literal (%.loc7_47.1)
  175. // CHECK:STDOUT: %.loc7_48.2: ref %Base = class_element_access %a.var, element0
  176. // CHECK:STDOUT: %.loc7_47.2: ref i32 = class_element_access %.loc7_48.2, element0
  177. // CHECK:STDOUT: %.loc7_47.3: init i32 = initialize_from %.loc7_33 to %.loc7_47.2 [template = constants.%.8]
  178. // CHECK:STDOUT: %.loc7_47.4: ref i32 = class_element_access %.loc7_48.2, element1
  179. // CHECK:STDOUT: %.loc7_47.5: init i32 = initialize_from %.loc7_46 to %.loc7_47.4 [template = constants.%.9]
  180. // CHECK:STDOUT: %.loc7_47.6: init %Base = class_init (%.loc7_47.3, %.loc7_47.5), %.loc7_48.2 [template = constants.%struct.1]
  181. // CHECK:STDOUT: %.loc7_48.3: init %Base = converted %.loc7_47.1, %.loc7_47.6 [template = constants.%struct.1]
  182. // CHECK:STDOUT: %.loc7_48.4: init %Child = class_init (%.loc7_48.3), %a.var [template = constants.%struct.2]
  183. // CHECK:STDOUT: %.loc7_49: init %Child = converted %.loc7_48.1, %.loc7_48.4 [template = constants.%struct.2]
  184. // CHECK:STDOUT: assign %a.var, %.loc7_49
  185. // CHECK:STDOUT: %a.ref.loc8: ref %Child = name_ref a, %a
  186. // CHECK:STDOUT: %x.ref: %.11 = name_ref x, file.%import_ref.6 [template = imports.%.1]
  187. // CHECK:STDOUT: %.loc8_4.1: ref %Base = class_element_access %a.ref.loc8, element0
  188. // CHECK:STDOUT: %.loc8_4.2: ref %Base = converted %a.ref.loc8, %.loc8_4.1
  189. // CHECK:STDOUT: %.loc8_4.3: ref i32 = class_element_access %.loc8_4.2, element0
  190. // CHECK:STDOUT: %.loc8_9: i32 = int_literal 2 [template = constants.%.12]
  191. // CHECK:STDOUT: assign %.loc8_4.3, %.loc8_9
  192. // CHECK:STDOUT: %a.ref.loc9: ref %Child = name_ref a, %a
  193. // CHECK:STDOUT: %F.ref: %F.type = name_ref F, file.%import_ref.4 [template = constants.%F]
  194. // CHECK:STDOUT: %.loc9_4: <bound method> = bound_method %a.ref.loc9, %F.ref
  195. // CHECK:STDOUT: %.loc9_6.1: ref %Base = class_element_access %a.ref.loc9, element0
  196. // CHECK:STDOUT: %.loc9_6.2: ref %Base = converted %a.ref.loc9, %.loc9_6.1
  197. // CHECK:STDOUT: %.loc9_6.3: %Base = bind_value %.loc9_6.2
  198. // CHECK:STDOUT: %F.call: init %.1 = call %.loc9_4(%.loc9_6.3)
  199. // CHECK:STDOUT: return
  200. // CHECK:STDOUT: }
  201. // CHECK:STDOUT:
  202. // CHECK:STDOUT: fn @F[%self: %Base]();
  203. // CHECK:STDOUT: