Files
carbon-lang/toolchain/sem_ir/vtable.h
T
124313269a Represent vtables as a top level SemIR construct (#5472)
The goal was/is to reduce the overhead for vtables in generics - the
previous representation/prior to this patch caused a new vtable to be
created in every specific which isn't generally what we want for Carbon
generics (the whole specific/generic thing is meant to avoid creating
specific versions for things that can be a generic form parameterized by
a specific instead of manifest as a unique entity per specific)

So this moves vtables to a top level object (like functions, classes,
etc). Each dynamic class will have a vtable in this list.

Classes have a `vtable_ptr` instruction in them that points to the
vtable.

The actual generic support hasn't been implemented in this patch, as
I've been struggling with just getting this part of the migration going
& wanted to get it flushed out before adding the additional
complications.

It's possible more laziness when doing cross-file importing would be
suitable - for instance if we only need to reference the vtable from
another file, but don't need to know its individual contents, it may be
beneficial for the functions in the vtable to be import_refs (or to add
another layer of indirection - so it can be a single import_ref
all-or-nothing for the functions in the vtable).

---------

Co-authored-by: Richard Smith <richard@metafoo.co.uk>
Co-authored-by: Carbon Infra Bot <carbon-external-infra@google.com>
2025-06-27 18:45:26 +00:00

34 lines
950 B
C++

// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#ifndef CARBON_TOOLCHAIN_SEM_IR_VTABLE_H_
#define CARBON_TOOLCHAIN_SEM_IR_VTABLE_H_
#include "toolchain/sem_ir/ids.h"
namespace Carbon::SemIR {
// Vtable-specific fields.
struct VtableFields {
// The class this is the vtable for.
ClassId class_id;
// A block containing all the virtual functions in this class and
// non-overriden functions in base classes, forming the complete vtable for
// the class.
InstBlockId virtual_functions_id;
};
struct Vtable : public VtableFields, public Printable<Vtable> {
auto Print(llvm::raw_ostream& out) const -> void {
out << "{class: " << class_id << ", {";
virtual_functions_id.Print(out);
out << "}}";
}
};
} // namespace Carbon::SemIR
#endif // CARBON_TOOLCHAIN_SEM_IR_VTABLE_H_