virtual_modifiers.carbon 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. // --- fail_todo_init.carbon
  29. package Init;
  30. import Modifiers;
  31. fn F() {
  32. // TODO: The vptr shouldn't be counted for programmer-facing behavior.
  33. // CHECK:STDERR: fail_todo_init.carbon:[[@LINE+3]]:27: error: cannot initialize class with 1 field(s) from struct with 0 field(s).
  34. // CHECK:STDERR: var v: Modifiers.Base = {};
  35. // CHECK:STDERR: ^~
  36. var v: Modifiers.Base = {};
  37. }
  38. // CHECK:STDOUT: --- modifiers.carbon
  39. // CHECK:STDOUT:
  40. // CHECK:STDOUT: constants {
  41. // CHECK:STDOUT: %Base: type = class_type @Base [template]
  42. // CHECK:STDOUT: %H.type: type = fn_type @H [template]
  43. // CHECK:STDOUT: %.1: type = tuple_type () [template]
  44. // CHECK:STDOUT: %H: %H.type = struct_value () [template]
  45. // CHECK:STDOUT: %I.type: type = fn_type @I [template]
  46. // CHECK:STDOUT: %I: %I.type = struct_value () [template]
  47. // CHECK:STDOUT: %.2: type = ptr_type <vtable> [template]
  48. // CHECK:STDOUT: %.3: type = struct_type {.<vptr>: %.2} [template]
  49. // CHECK:STDOUT: %.4: <witness> = complete_type_witness %.3 [template]
  50. // CHECK:STDOUT: %Abstract: type = class_type @Abstract [template]
  51. // CHECK:STDOUT: %J.type: type = fn_type @J [template]
  52. // CHECK:STDOUT: %J: %J.type = struct_value () [template]
  53. // CHECK:STDOUT: %K.type: type = fn_type @K [template]
  54. // CHECK:STDOUT: %K: %K.type = struct_value () [template]
  55. // CHECK:STDOUT: %L.type: type = fn_type @L [template]
  56. // CHECK:STDOUT: %L: %L.type = struct_value () [template]
  57. // CHECK:STDOUT: }
  58. // CHECK:STDOUT:
  59. // CHECK:STDOUT: imports {
  60. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  61. // CHECK:STDOUT: import Core//prelude
  62. // CHECK:STDOUT: import Core//prelude/operators
  63. // CHECK:STDOUT: import Core//prelude/types
  64. // CHECK:STDOUT: import Core//prelude/operators/arithmetic
  65. // CHECK:STDOUT: import Core//prelude/operators/as
  66. // CHECK:STDOUT: import Core//prelude/operators/bitwise
  67. // CHECK:STDOUT: import Core//prelude/operators/comparison
  68. // CHECK:STDOUT: import Core//prelude/types/bool
  69. // CHECK:STDOUT: }
  70. // CHECK:STDOUT: }
  71. // CHECK:STDOUT:
  72. // CHECK:STDOUT: file {
  73. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  74. // CHECK:STDOUT: .Core = imports.%Core
  75. // CHECK:STDOUT: .Base = %Base.decl
  76. // CHECK:STDOUT: .Abstract = %Abstract.decl
  77. // CHECK:STDOUT: }
  78. // CHECK:STDOUT: %Core.import = import Core
  79. // CHECK:STDOUT: %Base.decl: type = class_decl @Base [template = constants.%Base] {} {}
  80. // CHECK:STDOUT: %Abstract.decl: type = class_decl @Abstract [template = constants.%Abstract] {} {}
  81. // CHECK:STDOUT: }
  82. // CHECK:STDOUT:
  83. // CHECK:STDOUT: class @Base {
  84. // CHECK:STDOUT: %H.decl: %H.type = fn_decl @H [template = constants.%H] {} {}
  85. // CHECK:STDOUT: %I.decl: %I.type = fn_decl @I [template = constants.%I] {} {}
  86. // CHECK:STDOUT: %.loc8: <witness> = complete_type_witness %.3 [template = constants.%.4]
  87. // CHECK:STDOUT:
  88. // CHECK:STDOUT: !members:
  89. // CHECK:STDOUT: .Self = constants.%Base
  90. // CHECK:STDOUT: .H = %H.decl
  91. // CHECK:STDOUT: .I = %I.decl
  92. // CHECK:STDOUT: }
  93. // CHECK:STDOUT:
  94. // CHECK:STDOUT: class @Abstract {
  95. // CHECK:STDOUT: %J.decl: %J.type = fn_decl @J [template = constants.%J] {} {}
  96. // CHECK:STDOUT: %K.decl: %K.type = fn_decl @K [template = constants.%K] {} {}
  97. // CHECK:STDOUT: %L.decl: %L.type = fn_decl @L [template = constants.%L] {} {}
  98. // CHECK:STDOUT: %.loc16: <witness> = complete_type_witness %.3 [template = constants.%.4]
  99. // CHECK:STDOUT:
  100. // CHECK:STDOUT: !members:
  101. // CHECK:STDOUT: .Self = constants.%Abstract
  102. // CHECK:STDOUT: .J = %J.decl
  103. // CHECK:STDOUT: .K = %K.decl
  104. // CHECK:STDOUT: .L = %L.decl
  105. // CHECK:STDOUT: }
  106. // CHECK:STDOUT:
  107. // CHECK:STDOUT: virtual fn @H();
  108. // CHECK:STDOUT:
  109. // CHECK:STDOUT: impl fn @I();
  110. // CHECK:STDOUT:
  111. // CHECK:STDOUT: abstract fn @J();
  112. // CHECK:STDOUT:
  113. // CHECK:STDOUT: virtual fn @K();
  114. // CHECK:STDOUT:
  115. // CHECK:STDOUT: impl fn @L();
  116. // CHECK:STDOUT:
  117. // CHECK:STDOUT: --- todo_fail_later_base.carbon
  118. // CHECK:STDOUT:
  119. // CHECK:STDOUT: constants {
  120. // CHECK:STDOUT: %Derived: type = class_type @Derived [template]
  121. // CHECK:STDOUT: %F.type: type = fn_type @F [template]
  122. // CHECK:STDOUT: %.1: type = tuple_type () [template]
  123. // CHECK:STDOUT: %F: %F.type = struct_value () [template]
  124. // CHECK:STDOUT: %Base: type = class_type @Base [template]
  125. // CHECK:STDOUT: %.2: type = ptr_type <vtable> [template]
  126. // CHECK:STDOUT: %.3: type = struct_type {.<vptr>: %.2} [template]
  127. // CHECK:STDOUT: %.4: <witness> = complete_type_witness %.3 [template]
  128. // CHECK:STDOUT: %.5: type = ptr_type %.3 [template]
  129. // CHECK:STDOUT: %.6: type = unbound_element_type %Derived, %Base [template]
  130. // CHECK:STDOUT: %.7: type = struct_type {.<vptr>: %.2, .base: %Base} [template]
  131. // CHECK:STDOUT: %.8: <witness> = complete_type_witness %.7 [template]
  132. // CHECK:STDOUT: }
  133. // CHECK:STDOUT:
  134. // CHECK:STDOUT: imports {
  135. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  136. // CHECK:STDOUT: import Core//prelude
  137. // CHECK:STDOUT: import Core//prelude/operators
  138. // CHECK:STDOUT: import Core//prelude/types
  139. // CHECK:STDOUT: import Core//prelude/operators/arithmetic
  140. // CHECK:STDOUT: import Core//prelude/operators/as
  141. // CHECK:STDOUT: import Core//prelude/operators/bitwise
  142. // CHECK:STDOUT: import Core//prelude/operators/comparison
  143. // CHECK:STDOUT: import Core//prelude/types/bool
  144. // CHECK:STDOUT: }
  145. // CHECK:STDOUT: %Modifiers: <namespace> = namespace file.%Modifiers.import, [template] {
  146. // CHECK:STDOUT: .Base = %import_ref.1
  147. // CHECK:STDOUT: import Modifiers//default
  148. // CHECK:STDOUT: }
  149. // CHECK:STDOUT: %import_ref.1: type = import_ref Modifiers//default, inst+3, loaded [template = constants.%Base]
  150. // CHECK:STDOUT: %import_ref.2 = import_ref Modifiers//default, inst+4, unloaded
  151. // CHECK:STDOUT: %import_ref.3 = import_ref Modifiers//default, inst+5, unloaded
  152. // CHECK:STDOUT: %import_ref.4 = import_ref Modifiers//default, inst+9, unloaded
  153. // CHECK:STDOUT: }
  154. // CHECK:STDOUT:
  155. // CHECK:STDOUT: file {
  156. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  157. // CHECK:STDOUT: .Core = imports.%Core
  158. // CHECK:STDOUT: .Modifiers = imports.%Modifiers
  159. // CHECK:STDOUT: .Derived = %Derived.decl
  160. // CHECK:STDOUT: }
  161. // CHECK:STDOUT: %Core.import = import Core
  162. // CHECK:STDOUT: %Modifiers.import = import Modifiers
  163. // CHECK:STDOUT: %Derived.decl: type = class_decl @Derived [template = constants.%Derived] {} {}
  164. // CHECK:STDOUT: }
  165. // CHECK:STDOUT:
  166. // CHECK:STDOUT: class @Derived {
  167. // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [template = constants.%F] {} {}
  168. // CHECK:STDOUT: %Modifiers.ref: <namespace> = name_ref Modifiers, imports.%Modifiers [template = imports.%Modifiers]
  169. // CHECK:STDOUT: %Base.ref: type = name_ref Base, imports.%import_ref.1 [template = constants.%Base]
  170. // CHECK:STDOUT: %.loc8: %.6 = base_decl %Base, element0 [template]
  171. // CHECK:STDOUT: %.loc9: <witness> = complete_type_witness %.7 [template = constants.%.8]
  172. // CHECK:STDOUT:
  173. // CHECK:STDOUT: !members:
  174. // CHECK:STDOUT: .Self = constants.%Derived
  175. // CHECK:STDOUT: .F = %F.decl
  176. // CHECK:STDOUT: .base = %.loc8
  177. // CHECK:STDOUT: extend name_scope4
  178. // CHECK:STDOUT: }
  179. // CHECK:STDOUT:
  180. // CHECK:STDOUT: class @Base {
  181. // CHECK:STDOUT: !members:
  182. // CHECK:STDOUT: .Self = imports.%import_ref.2
  183. // CHECK:STDOUT: .H = imports.%import_ref.3
  184. // CHECK:STDOUT: .I = imports.%import_ref.4
  185. // CHECK:STDOUT: }
  186. // CHECK:STDOUT:
  187. // CHECK:STDOUT: virtual fn @F();
  188. // CHECK:STDOUT:
  189. // CHECK:STDOUT: --- fail_todo_init.carbon
  190. // CHECK:STDOUT:
  191. // CHECK:STDOUT: constants {
  192. // CHECK:STDOUT: %F.type: type = fn_type @F [template]
  193. // CHECK:STDOUT: %.1: type = tuple_type () [template]
  194. // CHECK:STDOUT: %F: %F.type = struct_value () [template]
  195. // CHECK:STDOUT: %Base: type = class_type @Base [template]
  196. // CHECK:STDOUT: %.2: type = ptr_type <vtable> [template]
  197. // CHECK:STDOUT: %.3: type = struct_type {.<vptr>: %.2} [template]
  198. // CHECK:STDOUT: %.4: <witness> = complete_type_witness %.3 [template]
  199. // CHECK:STDOUT: %.5: type = ptr_type %.3 [template]
  200. // CHECK:STDOUT: %.6: type = struct_type {} [template]
  201. // CHECK:STDOUT: }
  202. // CHECK:STDOUT:
  203. // CHECK:STDOUT: imports {
  204. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  205. // CHECK:STDOUT: import Core//prelude
  206. // CHECK:STDOUT: import Core//prelude/operators
  207. // CHECK:STDOUT: import Core//prelude/types
  208. // CHECK:STDOUT: import Core//prelude/operators/arithmetic
  209. // CHECK:STDOUT: import Core//prelude/operators/as
  210. // CHECK:STDOUT: import Core//prelude/operators/bitwise
  211. // CHECK:STDOUT: import Core//prelude/operators/comparison
  212. // CHECK:STDOUT: import Core//prelude/types/bool
  213. // CHECK:STDOUT: }
  214. // CHECK:STDOUT: %Modifiers: <namespace> = namespace file.%Modifiers.import, [template] {
  215. // CHECK:STDOUT: .Base = %import_ref.1
  216. // CHECK:STDOUT: import Modifiers//default
  217. // CHECK:STDOUT: }
  218. // CHECK:STDOUT: %import_ref.1: type = import_ref Modifiers//default, inst+3, loaded [template = constants.%Base]
  219. // CHECK:STDOUT: %import_ref.2 = import_ref Modifiers//default, inst+4, unloaded
  220. // CHECK:STDOUT: %import_ref.3 = import_ref Modifiers//default, inst+5, unloaded
  221. // CHECK:STDOUT: %import_ref.4 = import_ref Modifiers//default, inst+9, unloaded
  222. // CHECK:STDOUT: }
  223. // CHECK:STDOUT:
  224. // CHECK:STDOUT: file {
  225. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  226. // CHECK:STDOUT: .Core = imports.%Core
  227. // CHECK:STDOUT: .Modifiers = imports.%Modifiers
  228. // CHECK:STDOUT: .F = %F.decl
  229. // CHECK:STDOUT: }
  230. // CHECK:STDOUT: %Core.import = import Core
  231. // CHECK:STDOUT: %Modifiers.import = import Modifiers
  232. // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [template = constants.%F] {} {}
  233. // CHECK:STDOUT: }
  234. // CHECK:STDOUT:
  235. // CHECK:STDOUT: class @Base {
  236. // CHECK:STDOUT: !members:
  237. // CHECK:STDOUT: .Self = imports.%import_ref.2
  238. // CHECK:STDOUT: .H = imports.%import_ref.3
  239. // CHECK:STDOUT: .I = imports.%import_ref.4
  240. // CHECK:STDOUT: }
  241. // CHECK:STDOUT:
  242. // CHECK:STDOUT: fn @F() {
  243. // CHECK:STDOUT: !entry:
  244. // CHECK:STDOUT: %Modifiers.ref: <namespace> = name_ref Modifiers, imports.%Modifiers [template = imports.%Modifiers]
  245. // CHECK:STDOUT: %Base.ref: type = name_ref Base, imports.%import_ref.1 [template = constants.%Base]
  246. // CHECK:STDOUT: %v.var: ref %Base = var v
  247. // CHECK:STDOUT: %v: ref %Base = bind_name v, %v.var
  248. // CHECK:STDOUT: %.loc11: %.6 = struct_literal ()
  249. // CHECK:STDOUT: assign %v.var, <error>
  250. // CHECK:STDOUT: return
  251. // CHECK:STDOUT: }
  252. // CHECK:STDOUT: