mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-04 12:11:04 +01:00
Add vtable pointers to class layout (#4407)
A small step to virtual functions - adding vtable pointers to the
layout, but not initializing or otherwise using them at this stage.
A few open design questions I'd love feedback on:
* Is this the right/good enough SemIR representation for now? This patch
adds a `is_dynamic` attribute to `SemIR::Class` and populates/flags it
based on the flag of the base class, or if any virtual function is
declared in the class (or, at least that's my intent). Some other
options include:
* Each `Class` could store a `ClassId` (or `TypeId`?) of the (possibly
indirect, possibly self) base class that is the first one that is
dynamic/has a vtable pointer
* Could make the property narrower, like `has vtable pointer` and have
it `true` only on the type that introduces the vtable - then derived
classes would have to walk their base classes to check if they're the
one that needs to define the vtable pointer or not
* Should the vtable be the first element in the type? If there's a
non-dynamic base type, we could have a layout that's `{<non-dynamic base
type>, vtable ptr, <derived members>}`? Derived types would still be
able to uniquely identify where their vtable pointer is just fine... -
and the vtable pointer is, in a sense, a member of that intermediate
type, so it does seem a bit strange to force it to the front - but I
guess it's probably more efficient in some ways?
Open to any other suggestions/advice/thoughts on the direction, etc.
---------
Co-authored-by: Richard Smith <richard@metafoo.co.uk>
This commit is contained in:
co-authored by
Richard Smith
parent
3c58fb7ec5
commit
dfed743de2
@@ -253,6 +253,15 @@ static auto BuildFunctionDecl(Context& context,
|
||||
.Case(KeywordModifierSet::Impl,
|
||||
SemIR::Function::VirtualModifier::Impl)
|
||||
.Default(SemIR::Function::VirtualModifier::None);
|
||||
if (virtual_modifier != SemIR::Function::VirtualModifier::None &&
|
||||
parent_scope_inst) {
|
||||
if (auto class_decl = parent_scope_inst->TryAs<SemIR::ClassDecl>()) {
|
||||
auto& class_info = context.classes().Get(class_decl->class_id);
|
||||
CARBON_CHECK(virtual_modifier != SemIR::Function::VirtualModifier::Impl ||
|
||||
class_info.is_dynamic);
|
||||
class_info.is_dynamic = true;
|
||||
}
|
||||
}
|
||||
if (introducer.modifier_set.HasAnyOf(KeywordModifierSet::Interface)) {
|
||||
// TODO: Once we are saving the modifiers for a function, add check that
|
||||
// the function may only be defined if it is marked `default` or `final`.
|
||||
|
||||
Reference in New Issue
Block a user