base.carbon 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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/base.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/class/base.carbon
  10. // --- base.carbon
  11. package Base;
  12. base class Base {
  13. var b: i32;
  14. }
  15. class Derived {
  16. extend base: Base;
  17. var d: i32;
  18. }
  19. fn Make() -> Derived {
  20. return {.base = {.b = 4}, .d = 7};
  21. }
  22. fn Access(d: Derived) -> (i32, i32) {
  23. return (d.d, d.base.b);
  24. }
  25. // --- fail_base_after_field.carbon
  26. package BaseAfterField;
  27. base class Base {
  28. }
  29. class Derived {
  30. var d: i32;
  31. // CHECK:STDERR: fail_base_after_field.carbon:[[@LINE+4]]:3: error: `base` declaration must appear before field declarations [BaseDeclAfterFieldDecl]
  32. // CHECK:STDERR: extend base: Base;
  33. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~
  34. // CHECK:STDERR:
  35. extend base: Base;
  36. }
  37. // CHECK:STDOUT: --- base.carbon
  38. // CHECK:STDOUT:
  39. // CHECK:STDOUT: constants {
  40. // CHECK:STDOUT: %Base: type = class_type @Base [template]
  41. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [template]
  42. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [template]
  43. // CHECK:STDOUT: %Base.elem: type = unbound_element_type %Base, %i32 [template]
  44. // CHECK:STDOUT: %struct_type.b.0a3: type = struct_type {.b: %i32} [template]
  45. // CHECK:STDOUT: %complete_type.ba8: <witness> = complete_type_witness %struct_type.b.0a3 [template]
  46. // CHECK:STDOUT: %Derived: type = class_type @Derived [template]
  47. // CHECK:STDOUT: %Derived.elem.69e: type = unbound_element_type %Derived, %Base [template]
  48. // CHECK:STDOUT: %Derived.elem.344: type = unbound_element_type %Derived, %i32 [template]
  49. // CHECK:STDOUT: %struct_type.base.d.f8f: type = struct_type {.base: %Base, .d: %i32} [template]
  50. // CHECK:STDOUT: %complete_type.da6: <witness> = complete_type_witness %struct_type.base.d.f8f [template]
  51. // CHECK:STDOUT: %Make.type: type = fn_type @Make [template]
  52. // CHECK:STDOUT: %Make: %Make.type = struct_value () [template]
  53. // CHECK:STDOUT: %int_4.0c1: Core.IntLiteral = int_value 4 [template]
  54. // CHECK:STDOUT: %struct_type.b.a15: type = struct_type {.b: Core.IntLiteral} [template]
  55. // CHECK:STDOUT: %int_7.29f: Core.IntLiteral = int_value 7 [template]
  56. // CHECK:STDOUT: %struct_type.base.d.a20: type = struct_type {.base: %struct_type.b.a15, .d: Core.IntLiteral} [template]
  57. // CHECK:STDOUT: %Convert.type.1b6: type = fn_type @Convert.1, @ImplicitAs(%i32) [template]
  58. // CHECK:STDOUT: %impl_witness.d39: <witness> = impl_witness (imports.%Core.import_ref.a5b), @impl.1(%int_32) [template]
  59. // CHECK:STDOUT: %Convert.type.035: type = fn_type @Convert.2, @impl.1(%int_32) [template]
  60. // CHECK:STDOUT: %Convert.956: %Convert.type.035 = struct_value () [template]
  61. // CHECK:STDOUT: %Convert.bound.ac3: <bound method> = bound_method %int_4.0c1, %Convert.956 [template]
  62. // CHECK:STDOUT: %Convert.specific_fn.450: <specific function> = specific_function %Convert.bound.ac3, @Convert.2(%int_32) [template]
  63. // CHECK:STDOUT: %int_4.940: %i32 = int_value 4 [template]
  64. // CHECK:STDOUT: %Base.val: %Base = struct_value (%int_4.940) [template]
  65. // CHECK:STDOUT: %Convert.bound.208: <bound method> = bound_method %int_7.29f, %Convert.956 [template]
  66. // CHECK:STDOUT: %Convert.specific_fn.c12: <specific function> = specific_function %Convert.bound.208, @Convert.2(%int_32) [template]
  67. // CHECK:STDOUT: %int_7.0b1: %i32 = int_value 7 [template]
  68. // CHECK:STDOUT: %Derived.val: %Derived = struct_value (%Base.val, %int_7.0b1) [template]
  69. // CHECK:STDOUT: %tuple.type.24b: type = tuple_type (type, type) [template]
  70. // CHECK:STDOUT: %tuple.type.d07: type = tuple_type (%i32, %i32) [template]
  71. // CHECK:STDOUT: %Access.type: type = fn_type @Access [template]
  72. // CHECK:STDOUT: %Access: %Access.type = struct_value () [template]
  73. // CHECK:STDOUT: }
  74. // CHECK:STDOUT:
  75. // CHECK:STDOUT: imports {
  76. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  77. // CHECK:STDOUT: .Int = %Core.Int
  78. // CHECK:STDOUT: .ImplicitAs = %Core.ImplicitAs
  79. // CHECK:STDOUT: import Core//prelude
  80. // CHECK:STDOUT: import Core//prelude/...
  81. // CHECK:STDOUT: }
  82. // CHECK:STDOUT: }
  83. // CHECK:STDOUT:
  84. // CHECK:STDOUT: file {
  85. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  86. // CHECK:STDOUT: .Core = imports.%Core
  87. // CHECK:STDOUT: .Base = %Base.decl
  88. // CHECK:STDOUT: .Derived = %Derived.decl
  89. // CHECK:STDOUT: .Make = %Make.decl
  90. // CHECK:STDOUT: .Access = %Access.decl
  91. // CHECK:STDOUT: }
  92. // CHECK:STDOUT: %Core.import = import Core
  93. // CHECK:STDOUT: %Base.decl: type = class_decl @Base [template = constants.%Base] {} {}
  94. // CHECK:STDOUT: %Derived.decl: type = class_decl @Derived [template = constants.%Derived] {} {}
  95. // CHECK:STDOUT: %Make.decl: %Make.type = fn_decl @Make [template = constants.%Make] {
  96. // CHECK:STDOUT: %return.patt: %Derived = return_slot_pattern
  97. // CHECK:STDOUT: %return.param_patt: %Derived = out_param_pattern %return.patt, runtime_param0
  98. // CHECK:STDOUT: } {
  99. // CHECK:STDOUT: %Derived.ref: type = name_ref Derived, file.%Derived.decl [template = constants.%Derived]
  100. // CHECK:STDOUT: %return.param: ref %Derived = out_param runtime_param0
  101. // CHECK:STDOUT: %return: ref %Derived = return_slot %return.param
  102. // CHECK:STDOUT: }
  103. // CHECK:STDOUT: %Access.decl: %Access.type = fn_decl @Access [template = constants.%Access] {
  104. // CHECK:STDOUT: %d.patt: %Derived = binding_pattern d
  105. // CHECK:STDOUT: %d.param_patt: %Derived = value_param_pattern %d.patt, runtime_param0
  106. // CHECK:STDOUT: %return.patt: %tuple.type.d07 = return_slot_pattern
  107. // CHECK:STDOUT: %return.param_patt: %tuple.type.d07 = out_param_pattern %return.patt, runtime_param1
  108. // CHECK:STDOUT: } {
  109. // CHECK:STDOUT: %int_32.loc17_27: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  110. // CHECK:STDOUT: %i32.loc17_27: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  111. // CHECK:STDOUT: %int_32.loc17_32: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  112. // CHECK:STDOUT: %i32.loc17_32: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  113. // CHECK:STDOUT: %.loc17_35.1: %tuple.type.24b = tuple_literal (%i32.loc17_27, %i32.loc17_32)
  114. // CHECK:STDOUT: %.loc17_35.2: type = converted %.loc17_35.1, constants.%tuple.type.d07 [template = constants.%tuple.type.d07]
  115. // CHECK:STDOUT: %d.param: %Derived = value_param runtime_param0
  116. // CHECK:STDOUT: %Derived.ref: type = name_ref Derived, file.%Derived.decl [template = constants.%Derived]
  117. // CHECK:STDOUT: %d: %Derived = bind_name d, %d.param
  118. // CHECK:STDOUT: %return.param: ref %tuple.type.d07 = out_param runtime_param1
  119. // CHECK:STDOUT: %return: ref %tuple.type.d07 = return_slot %return.param
  120. // CHECK:STDOUT: }
  121. // CHECK:STDOUT: }
  122. // CHECK:STDOUT:
  123. // CHECK:STDOUT: class @Base {
  124. // CHECK:STDOUT: %.loc4_8: %Base.elem = field_decl b, element0 [template]
  125. // CHECK:STDOUT: name_binding_decl {
  126. // CHECK:STDOUT: %.loc4_3: %Base.elem = var_pattern %.loc4_8
  127. // CHECK:STDOUT: }
  128. // CHECK:STDOUT: %.var: ref %Base.elem = var <invalid>
  129. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %struct_type.b.0a3 [template = constants.%complete_type.ba8]
  130. // CHECK:STDOUT: complete_type_witness = %complete_type
  131. // CHECK:STDOUT:
  132. // CHECK:STDOUT: !members:
  133. // CHECK:STDOUT: .Self = constants.%Base
  134. // CHECK:STDOUT: .b = %.loc4_8
  135. // CHECK:STDOUT: }
  136. // CHECK:STDOUT:
  137. // CHECK:STDOUT: class @Derived {
  138. // CHECK:STDOUT: %Base.ref: type = name_ref Base, file.%Base.decl [template = constants.%Base]
  139. // CHECK:STDOUT: %.loc8: %Derived.elem.69e = base_decl %Base.ref, element0 [template]
  140. // CHECK:STDOUT: %.loc10_8: %Derived.elem.344 = field_decl d, element1 [template]
  141. // CHECK:STDOUT: name_binding_decl {
  142. // CHECK:STDOUT: %.loc10_3: %Derived.elem.344 = var_pattern %.loc10_8
  143. // CHECK:STDOUT: }
  144. // CHECK:STDOUT: %.var: ref %Derived.elem.344 = var <invalid>
  145. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %struct_type.base.d.f8f [template = constants.%complete_type.da6]
  146. // CHECK:STDOUT: complete_type_witness = %complete_type
  147. // CHECK:STDOUT:
  148. // CHECK:STDOUT: !members:
  149. // CHECK:STDOUT: .Self = constants.%Derived
  150. // CHECK:STDOUT: .base = %.loc8
  151. // CHECK:STDOUT: .d = %.loc10_8
  152. // CHECK:STDOUT: extend %Base.ref
  153. // CHECK:STDOUT: }
  154. // CHECK:STDOUT:
  155. // CHECK:STDOUT: fn @Make() -> %return.param_patt: %Derived {
  156. // CHECK:STDOUT: !entry:
  157. // CHECK:STDOUT: %int_4: Core.IntLiteral = int_value 4 [template = constants.%int_4.0c1]
  158. // CHECK:STDOUT: %.loc14_26.1: %struct_type.b.a15 = struct_literal (%int_4)
  159. // CHECK:STDOUT: %int_7: Core.IntLiteral = int_value 7 [template = constants.%int_7.29f]
  160. // CHECK:STDOUT: %.loc14_35.1: %struct_type.base.d.a20 = struct_literal (%.loc14_26.1, %int_7)
  161. // CHECK:STDOUT: %impl.elem0.loc14_26: %Convert.type.1b6 = impl_witness_access constants.%impl_witness.d39, element0 [template = constants.%Convert.956]
  162. // CHECK:STDOUT: %Convert.bound.loc14_26: <bound method> = bound_method %int_4, %impl.elem0.loc14_26 [template = constants.%Convert.bound.ac3]
  163. // CHECK:STDOUT: %Convert.specific_fn.loc14_26: <specific function> = specific_function %Convert.bound.loc14_26, @Convert.2(constants.%int_32) [template = constants.%Convert.specific_fn.450]
  164. // CHECK:STDOUT: %int.convert_checked.loc14_26: init %i32 = call %Convert.specific_fn.loc14_26(%int_4) [template = constants.%int_4.940]
  165. // CHECK:STDOUT: %.loc14_26.2: init %i32 = converted %int_4, %int.convert_checked.loc14_26 [template = constants.%int_4.940]
  166. // CHECK:STDOUT: %.loc14_35.2: ref %Base = class_element_access %return, element0
  167. // CHECK:STDOUT: %.loc14_26.3: ref %i32 = class_element_access %.loc14_35.2, element0
  168. // CHECK:STDOUT: %.loc14_26.4: init %i32 = initialize_from %.loc14_26.2 to %.loc14_26.3 [template = constants.%int_4.940]
  169. // CHECK:STDOUT: %.loc14_26.5: init %Base = class_init (%.loc14_26.4), %.loc14_35.2 [template = constants.%Base.val]
  170. // CHECK:STDOUT: %.loc14_35.3: init %Base = converted %.loc14_26.1, %.loc14_26.5 [template = constants.%Base.val]
  171. // CHECK:STDOUT: %impl.elem0.loc14_35: %Convert.type.1b6 = impl_witness_access constants.%impl_witness.d39, element0 [template = constants.%Convert.956]
  172. // CHECK:STDOUT: %Convert.bound.loc14_35: <bound method> = bound_method %int_7, %impl.elem0.loc14_35 [template = constants.%Convert.bound.208]
  173. // CHECK:STDOUT: %Convert.specific_fn.loc14_35: <specific function> = specific_function %Convert.bound.loc14_35, @Convert.2(constants.%int_32) [template = constants.%Convert.specific_fn.c12]
  174. // CHECK:STDOUT: %int.convert_checked.loc14_35: init %i32 = call %Convert.specific_fn.loc14_35(%int_7) [template = constants.%int_7.0b1]
  175. // CHECK:STDOUT: %.loc14_35.4: init %i32 = converted %int_7, %int.convert_checked.loc14_35 [template = constants.%int_7.0b1]
  176. // CHECK:STDOUT: %.loc14_35.5: ref %i32 = class_element_access %return, element1
  177. // CHECK:STDOUT: %.loc14_35.6: init %i32 = initialize_from %.loc14_35.4 to %.loc14_35.5 [template = constants.%int_7.0b1]
  178. // CHECK:STDOUT: %.loc14_35.7: init %Derived = class_init (%.loc14_35.3, %.loc14_35.6), %return [template = constants.%Derived.val]
  179. // CHECK:STDOUT: %.loc14_36: init %Derived = converted %.loc14_35.1, %.loc14_35.7 [template = constants.%Derived.val]
  180. // CHECK:STDOUT: return %.loc14_36 to %return
  181. // CHECK:STDOUT: }
  182. // CHECK:STDOUT:
  183. // CHECK:STDOUT: fn @Access(%d.param_patt: %Derived) -> %return.param_patt: %tuple.type.d07 {
  184. // CHECK:STDOUT: !entry:
  185. // CHECK:STDOUT: %d.ref.loc18_11: %Derived = name_ref d, %d
  186. // CHECK:STDOUT: %d.ref.loc18_12: %Derived.elem.344 = name_ref d, @Derived.%.loc10_8 [template = @Derived.%.loc10_8]
  187. // CHECK:STDOUT: %.loc18_12.1: ref %i32 = class_element_access %d.ref.loc18_11, element1
  188. // CHECK:STDOUT: %.loc18_12.2: %i32 = bind_value %.loc18_12.1
  189. // CHECK:STDOUT: %d.ref.loc18_16: %Derived = name_ref d, %d
  190. // CHECK:STDOUT: %base.ref: %Derived.elem.69e = name_ref base, @Derived.%.loc8 [template = @Derived.%.loc8]
  191. // CHECK:STDOUT: %.loc18_17.1: ref %Base = class_element_access %d.ref.loc18_16, element0
  192. // CHECK:STDOUT: %.loc18_17.2: %Base = bind_value %.loc18_17.1
  193. // CHECK:STDOUT: %b.ref: %Base.elem = name_ref b, @Base.%.loc4_8 [template = @Base.%.loc4_8]
  194. // CHECK:STDOUT: %.loc18_22.1: ref %i32 = class_element_access %.loc18_17.2, element0
  195. // CHECK:STDOUT: %.loc18_22.2: %i32 = bind_value %.loc18_22.1
  196. // CHECK:STDOUT: %.loc18_24.1: %tuple.type.d07 = tuple_literal (%.loc18_12.2, %.loc18_22.2)
  197. // CHECK:STDOUT: %tuple.elem0: ref %i32 = tuple_access %return, element0
  198. // CHECK:STDOUT: %.loc18_24.2: init %i32 = initialize_from %.loc18_12.2 to %tuple.elem0
  199. // CHECK:STDOUT: %tuple.elem1: ref %i32 = tuple_access %return, element1
  200. // CHECK:STDOUT: %.loc18_24.3: init %i32 = initialize_from %.loc18_22.2 to %tuple.elem1
  201. // CHECK:STDOUT: %.loc18_24.4: init %tuple.type.d07 = tuple_init (%.loc18_24.2, %.loc18_24.3) to %return
  202. // CHECK:STDOUT: %.loc18_25: init %tuple.type.d07 = converted %.loc18_24.1, %.loc18_24.4
  203. // CHECK:STDOUT: return %.loc18_25 to %return
  204. // CHECK:STDOUT: }
  205. // CHECK:STDOUT:
  206. // CHECK:STDOUT: --- fail_base_after_field.carbon
  207. // CHECK:STDOUT:
  208. // CHECK:STDOUT: constants {
  209. // CHECK:STDOUT: %Base: type = class_type @Base [template]
  210. // CHECK:STDOUT: %empty_struct_type: type = struct_type {} [template]
  211. // CHECK:STDOUT: %complete_type.357: <witness> = complete_type_witness %empty_struct_type [template]
  212. // CHECK:STDOUT: %Derived: type = class_type @Derived [template]
  213. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [template]
  214. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [template]
  215. // CHECK:STDOUT: %Derived.elem: type = unbound_element_type %Derived, %i32 [template]
  216. // CHECK:STDOUT: %struct_type.d: type = struct_type {.d: %i32} [template]
  217. // CHECK:STDOUT: %complete_type.860: <witness> = complete_type_witness %struct_type.d [template]
  218. // CHECK:STDOUT: }
  219. // CHECK:STDOUT:
  220. // CHECK:STDOUT: imports {
  221. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  222. // CHECK:STDOUT: .Int = %Core.Int
  223. // CHECK:STDOUT: import Core//prelude
  224. // CHECK:STDOUT: import Core//prelude/...
  225. // CHECK:STDOUT: }
  226. // CHECK:STDOUT: }
  227. // CHECK:STDOUT:
  228. // CHECK:STDOUT: file {
  229. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  230. // CHECK:STDOUT: .Core = imports.%Core
  231. // CHECK:STDOUT: .Base = %Base.decl
  232. // CHECK:STDOUT: .Derived = %Derived.decl
  233. // CHECK:STDOUT: }
  234. // CHECK:STDOUT: %Core.import = import Core
  235. // CHECK:STDOUT: %Base.decl: type = class_decl @Base [template = constants.%Base] {} {}
  236. // CHECK:STDOUT: %Derived.decl: type = class_decl @Derived [template = constants.%Derived] {} {}
  237. // CHECK:STDOUT: }
  238. // CHECK:STDOUT:
  239. // CHECK:STDOUT: class @Base {
  240. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %empty_struct_type [template = constants.%complete_type.357]
  241. // CHECK:STDOUT: complete_type_witness = %complete_type
  242. // CHECK:STDOUT:
  243. // CHECK:STDOUT: !members:
  244. // CHECK:STDOUT: .Self = constants.%Base
  245. // CHECK:STDOUT: }
  246. // CHECK:STDOUT:
  247. // CHECK:STDOUT: class @Derived {
  248. // CHECK:STDOUT: %.loc7_8: %Derived.elem = field_decl d, element0 [template]
  249. // CHECK:STDOUT: name_binding_decl {
  250. // CHECK:STDOUT: %.loc7_3: %Derived.elem = var_pattern %.loc7_8
  251. // CHECK:STDOUT: }
  252. // CHECK:STDOUT: %.var: ref %Derived.elem = var <invalid>
  253. // CHECK:STDOUT: %Base.ref: type = name_ref Base, file.%Base.decl [template = constants.%Base]
  254. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %struct_type.d [template = constants.%complete_type.860]
  255. // CHECK:STDOUT: complete_type_witness = %complete_type
  256. // CHECK:STDOUT:
  257. // CHECK:STDOUT: !members:
  258. // CHECK:STDOUT: .Self = constants.%Derived
  259. // CHECK:STDOUT: .d = %.loc7_8
  260. // CHECK:STDOUT: }
  261. // CHECK:STDOUT: