Export Carbon classes as base / final / abstract. (#7191)

* For Carbon `base class C`, export as a regular C++ class.
* For Carbon `class C`, export with the C++ `final` keyword attribute.
* For Carbon `abstract C`, mark the destructor as pure virtual in cases
where no member function is abstract, or emit an error if the destructor
is not virtual.

To support the final point, mark the destructor of an exported class as
virtual if it overrides a virtual destructor from the base class.

In passing, fix a crash exporting fields if the class has an invalid
base type.
This commit is contained in:
Richard Smith
2026-05-13 17:48:55 +00:00
committed by GitHub
parent c33fb9fc48
commit 917856aff7
9 changed files with 594 additions and 83 deletions
+9
View File
@@ -616,6 +616,15 @@ auto CarbonExternalASTSource::CompleteType(clang::TagDecl* tag_decl) -> void {
class_decl->startDefinition();
CARBON_CHECK(class_decl->hasDefinition());
// If the Carbon class is final, mark the C++ class as also being `final`.
// Abstract classes are handled when generating the destructor declaration.
if (class_info.inheritance_kind == SemIR::Class::InheritanceKind::Final) {
// TODO: Find the location of the `final` modifier and use it here.
class_decl->addAttr(clang::FinalAttr::Create(
context_->ast_context(),
GetCppLocation(*context_, SemIR::LocId(class_info.definition_id))));
}
// If the Carbon class has a base class that we can map into C++, add that as
// a C++ base class.
auto base_type_id =