virtual_modifiers.carbon 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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/virtual_modifiers.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/class/virtual_modifiers.carbon
  10. // --- modifiers.carbon
  11. package Modifiers;
  12. base class Base {
  13. virtual fn H();
  14. impl fn I();
  15. }
  16. abstract class Abstract {
  17. abstract fn J();
  18. virtual fn K();
  19. impl fn L();
  20. }
  21. // --- todo_fail_later_base.carbon
  22. package FailLaterBase;
  23. import Modifiers;
  24. base class Derived {
  25. virtual fn F();
  26. extend base: Modifiers.Base;
  27. }
  28. // --- init.carbon
  29. package Init;
  30. import Modifiers;
  31. fn F() {
  32. var v: Modifiers.Base = {};
  33. }
  34. // --- init_members.carbon
  35. package InitMembers;
  36. base class Base {
  37. virtual fn F();
  38. var m1: i32;
  39. var m2: i32;
  40. }
  41. fn F() {
  42. var v: Base = {.m2 = 3, .m1 = 5};
  43. }
  44. // CHECK:STDOUT: --- modifiers.carbon
  45. // CHECK:STDOUT:
  46. // CHECK:STDOUT: constants {
  47. // CHECK:STDOUT: %Base: type = class_type @Base [template]
  48. // CHECK:STDOUT: %H.type: type = fn_type @H [template]
  49. // CHECK:STDOUT: %H: %H.type = struct_value () [template]
  50. // CHECK:STDOUT: %I.type: type = fn_type @I [template]
  51. // CHECK:STDOUT: %I: %I.type = struct_value () [template]
  52. // CHECK:STDOUT: %.1: type = ptr_type <vtable> [template]
  53. // CHECK:STDOUT: %.2: type = struct_type {.<vptr>: %.1} [template]
  54. // CHECK:STDOUT: %.3: <witness> = complete_type_witness %.2 [template]
  55. // CHECK:STDOUT: %Abstract: type = class_type @Abstract [template]
  56. // CHECK:STDOUT: %J.type: type = fn_type @J [template]
  57. // CHECK:STDOUT: %J: %J.type = struct_value () [template]
  58. // CHECK:STDOUT: %K.type: type = fn_type @K [template]
  59. // CHECK:STDOUT: %K: %K.type = struct_value () [template]
  60. // CHECK:STDOUT: %L.type: type = fn_type @L [template]
  61. // CHECK:STDOUT: %L: %L.type = struct_value () [template]
  62. // CHECK:STDOUT: }
  63. // CHECK:STDOUT:
  64. // CHECK:STDOUT: imports {
  65. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  66. // CHECK:STDOUT: import Core//prelude
  67. // CHECK:STDOUT: import Core//prelude/...
  68. // CHECK:STDOUT: }
  69. // CHECK:STDOUT: }
  70. // CHECK:STDOUT:
  71. // CHECK:STDOUT: file {
  72. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  73. // CHECK:STDOUT: .Core = imports.%Core
  74. // CHECK:STDOUT: .Base = %Base.decl
  75. // CHECK:STDOUT: .Abstract = %Abstract.decl
  76. // CHECK:STDOUT: }
  77. // CHECK:STDOUT: %Core.import = import Core
  78. // CHECK:STDOUT: %Base.decl: type = class_decl @Base [template = constants.%Base] {} {}
  79. // CHECK:STDOUT: %Abstract.decl: type = class_decl @Abstract [template = constants.%Abstract] {} {}
  80. // CHECK:STDOUT: }
  81. // CHECK:STDOUT:
  82. // CHECK:STDOUT: class @Base {
  83. // CHECK:STDOUT: %H.decl: %H.type = fn_decl @H [template = constants.%H] {} {}
  84. // CHECK:STDOUT: %I.decl: %I.type = fn_decl @I [template = constants.%I] {} {}
  85. // CHECK:STDOUT: %.loc8: <witness> = complete_type_witness %.2 [template = constants.%.3]
  86. // CHECK:STDOUT:
  87. // CHECK:STDOUT: !members:
  88. // CHECK:STDOUT: .Self = constants.%Base
  89. // CHECK:STDOUT: .H = %H.decl
  90. // CHECK:STDOUT: .I = %I.decl
  91. // CHECK:STDOUT: }
  92. // CHECK:STDOUT:
  93. // CHECK:STDOUT: class @Abstract {
  94. // CHECK:STDOUT: %J.decl: %J.type = fn_decl @J [template = constants.%J] {} {}
  95. // CHECK:STDOUT: %K.decl: %K.type = fn_decl @K [template = constants.%K] {} {}
  96. // CHECK:STDOUT: %L.decl: %L.type = fn_decl @L [template = constants.%L] {} {}
  97. // CHECK:STDOUT: %.loc16: <witness> = complete_type_witness %.2 [template = constants.%.3]
  98. // CHECK:STDOUT:
  99. // CHECK:STDOUT: !members:
  100. // CHECK:STDOUT: .Self = constants.%Abstract
  101. // CHECK:STDOUT: .J = %J.decl
  102. // CHECK:STDOUT: .K = %K.decl
  103. // CHECK:STDOUT: .L = %L.decl
  104. // CHECK:STDOUT: }
  105. // CHECK:STDOUT:
  106. // CHECK:STDOUT: virtual fn @H();
  107. // CHECK:STDOUT:
  108. // CHECK:STDOUT: impl fn @I();
  109. // CHECK:STDOUT:
  110. // CHECK:STDOUT: abstract fn @J();
  111. // CHECK:STDOUT:
  112. // CHECK:STDOUT: virtual fn @K();
  113. // CHECK:STDOUT:
  114. // CHECK:STDOUT: impl fn @L();
  115. // CHECK:STDOUT:
  116. // CHECK:STDOUT: --- todo_fail_later_base.carbon
  117. // CHECK:STDOUT:
  118. // CHECK:STDOUT: constants {
  119. // CHECK:STDOUT: %Derived: type = class_type @Derived [template]
  120. // CHECK:STDOUT: %F.type: type = fn_type @F [template]
  121. // CHECK:STDOUT: %F: %F.type = struct_value () [template]
  122. // CHECK:STDOUT: %Base: type = class_type @Base [template]
  123. // CHECK:STDOUT: %.5: type = unbound_element_type %Derived, %Base [template]
  124. // CHECK:STDOUT: %.6: type = struct_type {.base: %Base} [template]
  125. // CHECK:STDOUT: %.7: <witness> = complete_type_witness %.6 [template]
  126. // CHECK:STDOUT: }
  127. // CHECK:STDOUT:
  128. // CHECK:STDOUT: imports {
  129. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  130. // CHECK:STDOUT: import Core//prelude
  131. // CHECK:STDOUT: import Core//prelude/...
  132. // CHECK:STDOUT: }
  133. // CHECK:STDOUT: %Modifiers: <namespace> = namespace file.%Modifiers.import, [template] {
  134. // CHECK:STDOUT: .Base = %import_ref.1
  135. // CHECK:STDOUT: import Modifiers//default
  136. // CHECK:STDOUT: }
  137. // CHECK:STDOUT: %import_ref.1: type = import_ref Modifiers//default, inst+3, loaded [template = constants.%Base]
  138. // CHECK:STDOUT: %import_ref.2 = import_ref Modifiers//default, inst+4, unloaded
  139. // CHECK:STDOUT: %import_ref.3 = import_ref Modifiers//default, inst+5, unloaded
  140. // CHECK:STDOUT: %import_ref.4 = import_ref Modifiers//default, inst+9, unloaded
  141. // CHECK:STDOUT: }
  142. // CHECK:STDOUT:
  143. // CHECK:STDOUT: file {
  144. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  145. // CHECK:STDOUT: .Core = imports.%Core
  146. // CHECK:STDOUT: .Modifiers = imports.%Modifiers
  147. // CHECK:STDOUT: .Derived = %Derived.decl
  148. // CHECK:STDOUT: }
  149. // CHECK:STDOUT: %Core.import = import Core
  150. // CHECK:STDOUT: %Modifiers.import = import Modifiers
  151. // CHECK:STDOUT: %Derived.decl: type = class_decl @Derived [template = constants.%Derived] {} {}
  152. // CHECK:STDOUT: }
  153. // CHECK:STDOUT:
  154. // CHECK:STDOUT: class @Derived {
  155. // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [template = constants.%F] {} {}
  156. // CHECK:STDOUT: %Modifiers.ref: <namespace> = name_ref Modifiers, imports.%Modifiers [template = imports.%Modifiers]
  157. // CHECK:STDOUT: %Base.ref: type = name_ref Base, imports.%import_ref.1 [template = constants.%Base]
  158. // CHECK:STDOUT: %.loc8: %.5 = base_decl %Base, element0 [template]
  159. // CHECK:STDOUT: %.loc9: <witness> = complete_type_witness %.6 [template = constants.%.7]
  160. // CHECK:STDOUT:
  161. // CHECK:STDOUT: !members:
  162. // CHECK:STDOUT: .Self = constants.%Derived
  163. // CHECK:STDOUT: .F = %F.decl
  164. // CHECK:STDOUT: .base = %.loc8
  165. // CHECK:STDOUT: extend %Base.ref
  166. // CHECK:STDOUT: }
  167. // CHECK:STDOUT:
  168. // CHECK:STDOUT: class @Base {
  169. // CHECK:STDOUT: !members:
  170. // CHECK:STDOUT: .Self = imports.%import_ref.2
  171. // CHECK:STDOUT: .H = imports.%import_ref.3
  172. // CHECK:STDOUT: .I = imports.%import_ref.4
  173. // CHECK:STDOUT: }
  174. // CHECK:STDOUT:
  175. // CHECK:STDOUT: virtual fn @F();
  176. // CHECK:STDOUT:
  177. // CHECK:STDOUT: --- init.carbon
  178. // CHECK:STDOUT:
  179. // CHECK:STDOUT: constants {
  180. // CHECK:STDOUT: %F.type: type = fn_type @F [template]
  181. // CHECK:STDOUT: %F: %F.type = struct_value () [template]
  182. // CHECK:STDOUT: %Base: type = class_type @Base [template]
  183. // CHECK:STDOUT: %.5: type = struct_type {} [template]
  184. // CHECK:STDOUT: }
  185. // CHECK:STDOUT:
  186. // CHECK:STDOUT: imports {
  187. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  188. // CHECK:STDOUT: import Core//prelude
  189. // CHECK:STDOUT: import Core//prelude/...
  190. // CHECK:STDOUT: }
  191. // CHECK:STDOUT: %Modifiers: <namespace> = namespace file.%Modifiers.import, [template] {
  192. // CHECK:STDOUT: .Base = %import_ref.1
  193. // CHECK:STDOUT: import Modifiers//default
  194. // CHECK:STDOUT: }
  195. // CHECK:STDOUT: %import_ref.1: type = import_ref Modifiers//default, inst+3, loaded [template = constants.%Base]
  196. // CHECK:STDOUT: %import_ref.2 = import_ref Modifiers//default, inst+4, unloaded
  197. // CHECK:STDOUT: %import_ref.3 = import_ref Modifiers//default, inst+5, unloaded
  198. // CHECK:STDOUT: %import_ref.4 = import_ref Modifiers//default, inst+9, unloaded
  199. // CHECK:STDOUT: }
  200. // CHECK:STDOUT:
  201. // CHECK:STDOUT: file {
  202. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  203. // CHECK:STDOUT: .Core = imports.%Core
  204. // CHECK:STDOUT: .Modifiers = imports.%Modifiers
  205. // CHECK:STDOUT: .F = %F.decl
  206. // CHECK:STDOUT: }
  207. // CHECK:STDOUT: %Core.import = import Core
  208. // CHECK:STDOUT: %Modifiers.import = import Modifiers
  209. // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [template = constants.%F] {} {}
  210. // CHECK:STDOUT: }
  211. // CHECK:STDOUT:
  212. // CHECK:STDOUT: class @Base {
  213. // CHECK:STDOUT: !members:
  214. // CHECK:STDOUT: .Self = imports.%import_ref.2
  215. // CHECK:STDOUT: .H = imports.%import_ref.3
  216. // CHECK:STDOUT: .I = imports.%import_ref.4
  217. // CHECK:STDOUT: }
  218. // CHECK:STDOUT:
  219. // CHECK:STDOUT: fn @F() {
  220. // CHECK:STDOUT: !entry:
  221. // CHECK:STDOUT: %Modifiers.ref: <namespace> = name_ref Modifiers, imports.%Modifiers [template = imports.%Modifiers]
  222. // CHECK:STDOUT: %Base.ref: type = name_ref Base, imports.%import_ref.1 [template = constants.%Base]
  223. // CHECK:STDOUT: %v.var: ref %Base = var v
  224. // CHECK:STDOUT: %v: ref %Base = bind_name v, %v.var
  225. // CHECK:STDOUT: %.loc7_28.1: %.5 = struct_literal ()
  226. // CHECK:STDOUT: %.loc7_28.2: init %Base = class_init (<error>), %v.var [template = <error>]
  227. // CHECK:STDOUT: %.loc7_29: init %Base = converted %.loc7_28.1, %.loc7_28.2 [template = <error>]
  228. // CHECK:STDOUT: assign %v.var, %.loc7_29
  229. // CHECK:STDOUT: return
  230. // CHECK:STDOUT: }
  231. // CHECK:STDOUT:
  232. // CHECK:STDOUT: --- init_members.carbon
  233. // CHECK:STDOUT:
  234. // CHECK:STDOUT: constants {
  235. // CHECK:STDOUT: %Base: type = class_type @Base [template]
  236. // CHECK:STDOUT: %F.type.1: type = fn_type @F.1 [template]
  237. // CHECK:STDOUT: %F.1: %F.type.1 = struct_value () [template]
  238. // CHECK:STDOUT: %.1: Core.IntLiteral = int_value 32 [template]
  239. // CHECK:STDOUT: %Int.type: type = fn_type @Int [template]
  240. // CHECK:STDOUT: %Int: %Int.type = struct_value () [template]
  241. // CHECK:STDOUT: %i32: type = int_type signed, %.1 [template]
  242. // CHECK:STDOUT: %.2: type = unbound_element_type %Base, %i32 [template]
  243. // CHECK:STDOUT: %.3: type = ptr_type <vtable> [template]
  244. // CHECK:STDOUT: %.4: type = struct_type {.<vptr>: %.3, .m1: %i32, .m2: %i32} [template]
  245. // CHECK:STDOUT: %.5: <witness> = complete_type_witness %.4 [template]
  246. // CHECK:STDOUT: %F.type.2: type = fn_type @F.2 [template]
  247. // CHECK:STDOUT: %F.2: %F.type.2 = struct_value () [template]
  248. // CHECK:STDOUT: %.7: Core.IntLiteral = int_value 3 [template]
  249. // CHECK:STDOUT: %.8: Core.IntLiteral = int_value 5 [template]
  250. // CHECK:STDOUT: %.9: type = struct_type {.m2: Core.IntLiteral, .m1: Core.IntLiteral} [template]
  251. // CHECK:STDOUT: %Convert.type.2: type = fn_type @Convert.1, @ImplicitAs(%i32) [template]
  252. // CHECK:STDOUT: %Convert.type.14: type = fn_type @Convert.2, @impl.1(%.1) [template]
  253. // CHECK:STDOUT: %Convert.14: %Convert.type.14 = struct_value () [template]
  254. // CHECK:STDOUT: %.33: <witness> = interface_witness (%Convert.14) [template]
  255. // CHECK:STDOUT: %.34: <bound method> = bound_method %.8, %Convert.14 [template]
  256. // CHECK:STDOUT: %.35: <specific function> = specific_function %.34, @Convert.2(%.1) [template]
  257. // CHECK:STDOUT: %.36: %i32 = int_value 5 [template]
  258. // CHECK:STDOUT: %.37: <bound method> = bound_method %.7, %Convert.14 [template]
  259. // CHECK:STDOUT: %.38: <specific function> = specific_function %.37, @Convert.2(%.1) [template]
  260. // CHECK:STDOUT: %.39: %i32 = int_value 3 [template]
  261. // CHECK:STDOUT: }
  262. // CHECK:STDOUT:
  263. // CHECK:STDOUT: imports {
  264. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  265. // CHECK:STDOUT: .Int = %import_ref.1
  266. // CHECK:STDOUT: .ImplicitAs = %import_ref.2
  267. // CHECK:STDOUT: import Core//prelude
  268. // CHECK:STDOUT: import Core//prelude/...
  269. // CHECK:STDOUT: }
  270. // CHECK:STDOUT: }
  271. // CHECK:STDOUT:
  272. // CHECK:STDOUT: file {
  273. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  274. // CHECK:STDOUT: .Core = imports.%Core
  275. // CHECK:STDOUT: .Base = %Base.decl
  276. // CHECK:STDOUT: .F = %F.decl
  277. // CHECK:STDOUT: }
  278. // CHECK:STDOUT: %Core.import = import Core
  279. // CHECK:STDOUT: %Base.decl: type = class_decl @Base [template = constants.%Base] {} {}
  280. // CHECK:STDOUT: %F.decl: %F.type.2 = fn_decl @F.2 [template = constants.%F.2] {} {}
  281. // CHECK:STDOUT: }
  282. // CHECK:STDOUT:
  283. // CHECK:STDOUT: class @Base {
  284. // CHECK:STDOUT: %F.decl: %F.type.1 = fn_decl @F.1 [template = constants.%F.1] {} {}
  285. // CHECK:STDOUT: %.loc6_11.1: Core.IntLiteral = int_value 32 [template = constants.%.1]
  286. // CHECK:STDOUT: %int.make_type_signed.loc6: init type = call constants.%Int(%.loc6_11.1) [template = constants.%i32]
  287. // CHECK:STDOUT: %.loc6_11.2: type = value_of_initializer %int.make_type_signed.loc6 [template = constants.%i32]
  288. // CHECK:STDOUT: %.loc6_11.3: type = converted %int.make_type_signed.loc6, %.loc6_11.2 [template = constants.%i32]
  289. // CHECK:STDOUT: %.loc6_9: %.2 = field_decl m1, element0 [template]
  290. // CHECK:STDOUT: %.loc7_11.1: Core.IntLiteral = int_value 32 [template = constants.%.1]
  291. // CHECK:STDOUT: %int.make_type_signed.loc7: init type = call constants.%Int(%.loc7_11.1) [template = constants.%i32]
  292. // CHECK:STDOUT: %.loc7_11.2: type = value_of_initializer %int.make_type_signed.loc7 [template = constants.%i32]
  293. // CHECK:STDOUT: %.loc7_11.3: type = converted %int.make_type_signed.loc7, %.loc7_11.2 [template = constants.%i32]
  294. // CHECK:STDOUT: %.loc7_9: %.2 = field_decl m2, element1 [template]
  295. // CHECK:STDOUT: %.loc8: <witness> = complete_type_witness %.4 [template = constants.%.5]
  296. // CHECK:STDOUT:
  297. // CHECK:STDOUT: !members:
  298. // CHECK:STDOUT: .Self = constants.%Base
  299. // CHECK:STDOUT: .F = %F.decl
  300. // CHECK:STDOUT: .m1 = %.loc6_9
  301. // CHECK:STDOUT: .m2 = %.loc7_9
  302. // CHECK:STDOUT: }
  303. // CHECK:STDOUT:
  304. // CHECK:STDOUT: virtual fn @F.1();
  305. // CHECK:STDOUT:
  306. // CHECK:STDOUT: fn @F.2() {
  307. // CHECK:STDOUT: !entry:
  308. // CHECK:STDOUT: %Base.ref: type = name_ref Base, file.%Base.decl [template = constants.%Base]
  309. // CHECK:STDOUT: %v.var: ref %Base = var v
  310. // CHECK:STDOUT: %v: ref %Base = bind_name v, %v.var
  311. // CHECK:STDOUT: %.loc11_24: Core.IntLiteral = int_value 3 [template = constants.%.7]
  312. // CHECK:STDOUT: %.loc11_33: Core.IntLiteral = int_value 5 [template = constants.%.8]
  313. // CHECK:STDOUT: %.loc11_34.1: %.9 = struct_literal (%.loc11_24, %.loc11_33)
  314. // CHECK:STDOUT: %.loc11_34.2: %Convert.type.2 = interface_witness_access constants.%.33, element0 [template = constants.%Convert.14]
  315. // CHECK:STDOUT: %.loc11_34.3: <bound method> = bound_method %.loc11_33, %.loc11_34.2 [template = constants.%.34]
  316. // CHECK:STDOUT: %.loc11_34.4: <specific function> = specific_function %.loc11_34.3, @Convert.2(constants.%.1) [template = constants.%.35]
  317. // CHECK:STDOUT: %int.convert_checked.loc11_34.1: init %i32 = call %.loc11_34.4(%.loc11_33) [template = constants.%.36]
  318. // CHECK:STDOUT: %.loc11_34.5: init %i32 = converted %.loc11_33, %int.convert_checked.loc11_34.1 [template = constants.%.36]
  319. // CHECK:STDOUT: %.loc11_34.6: ref %i32 = class_element_access %v.var, element2
  320. // CHECK:STDOUT: %.loc11_34.7: init %i32 = initialize_from %.loc11_34.5 to %.loc11_34.6 [template = constants.%.36]
  321. // CHECK:STDOUT: %.loc11_34.8: %Convert.type.2 = interface_witness_access constants.%.33, element0 [template = constants.%Convert.14]
  322. // CHECK:STDOUT: %.loc11_34.9: <bound method> = bound_method %.loc11_24, %.loc11_34.8 [template = constants.%.37]
  323. // CHECK:STDOUT: %.loc11_34.10: <specific function> = specific_function %.loc11_34.9, @Convert.2(constants.%.1) [template = constants.%.38]
  324. // CHECK:STDOUT: %int.convert_checked.loc11_34.2: init %i32 = call %.loc11_34.10(%.loc11_24) [template = constants.%.39]
  325. // CHECK:STDOUT: %.loc11_34.11: init %i32 = converted %.loc11_24, %int.convert_checked.loc11_34.2 [template = constants.%.39]
  326. // CHECK:STDOUT: %.loc11_34.12: ref %i32 = class_element_access %v.var, element1
  327. // CHECK:STDOUT: %.loc11_34.13: init %i32 = initialize_from %.loc11_34.11 to %.loc11_34.12 [template = constants.%.39]
  328. // CHECK:STDOUT: %.loc11_34.14: init %Base = class_init (<error>, %.loc11_34.7, %.loc11_34.13), %v.var [template = <error>]
  329. // CHECK:STDOUT: %.loc11_35: init %Base = converted %.loc11_34.1, %.loc11_34.14 [template = <error>]
  330. // CHECK:STDOUT: assign %v.var, %.loc11_35
  331. // CHECK:STDOUT: return
  332. // CHECK:STDOUT: }
  333. // CHECK:STDOUT: