Skip to content

Commit ef475d8

Browse files
authored
Import C++ class A final as Carbon final class. (#5866)
Also import unions as final classes, and abstract classes as `abstract class`es.
1 parent 6b83414 commit ef475d8

File tree

4 files changed

+214
-578
lines changed

4 files changed

+214
-578
lines changed

toolchain/check/import_cpp.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,37 @@ static auto BuildClassDecl(Context& context,
497497
return {class_decl.class_id, context.types().GetAsTypeInstId(class_decl_id)};
498498
}
499499

500+
// Determines the Carbon inheritance kind to use for a C++ class definition.
501+
static auto GetInheritanceKind(clang::CXXRecordDecl* class_def)
502+
-> SemIR::Class::InheritanceKind {
503+
if (class_def->isUnion()) {
504+
// Treat all unions as final classes to match their C++ semantics. While we
505+
// could support this, the author of a C++ union has no way to mark their
506+
// type as `final` to prevent it, and so we assume the intent was to
507+
// disallow inheritance.
508+
return SemIR::Class::Final;
509+
}
510+
511+
if (class_def->hasAttr<clang::FinalAttr>()) {
512+
// The class is final in C++; don't allow Carbon types to derive from it.
513+
// Note that such a type might also be abstract in C++; we treat final as
514+
// taking precedence.
515+
//
516+
// We could also treat classes with a final destructor as being final, as
517+
// Clang does when determining whether a class is "effectively final", but
518+
// to keep our rules simpler we do not.
519+
return SemIR::Class::Final;
520+
}
521+
522+
if (class_def->isAbstract()) {
523+
// If the class has any abstract members, it's abstract.
524+
return SemIR::Class::Abstract;
525+
}
526+
527+
// Allow inheritance from any other C++ class type.
528+
return SemIR::Class::Base;
529+
}
530+
500531
// Checks that the specified finished class definition is valid and builds and
501532
// returns a corresponding complete type witness instruction.
502533
// TODO: Remove recursion into mapping field types.
@@ -685,6 +716,8 @@ static auto BuildClassDefinition(Context& context,
685716

686717
context.inst_block_stack().Push();
687718

719+
class_info.inheritance_kind = GetInheritanceKind(clang_def);
720+
688721
// Compute the class's object representation.
689722
auto object_repr_id = ImportClassObjectRepr(
690723
context, class_id, import_ir_inst_id, class_inst_id, clang_def);
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
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+
// INCLUDE-FILE: toolchain/testing/testdata/min_prelude/destroy.carbon
6+
// EXTRA-ARGS: --clang-arg=-Wno-abstract-final-class
7+
//
8+
// AUTOUPDATE
9+
// TIP: To test this file alone, run:
10+
// TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/interop/cpp/class/abstract.carbon
11+
// TIP: To dump output, run:
12+
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/interop/cpp/class/abstract.carbon
13+
14+
// --- abstract.h
15+
16+
struct A {
17+
virtual void f() = 0;
18+
};
19+
20+
// --- derive_base_from_abstract.carbon
21+
22+
library "[[@TEST_NAME]]";
23+
24+
import Cpp library "abstract.h";
25+
26+
base class B {
27+
extend base: Cpp.A;
28+
}
29+
30+
// --- todo_fail_derive_final_from_abstract.carbon
31+
32+
library "[[@TEST_NAME]]";
33+
34+
import Cpp library "abstract.h";
35+
36+
class C {
37+
// TODO: We should reject this because `f` is not implemented.
38+
extend base: Cpp.A;
39+
}
40+
41+
// --- fail_todo_impl_abstract_member.carbon
42+
43+
library "[[@TEST_NAME]]";
44+
45+
import Cpp library "abstract.h";
46+
47+
class C {
48+
extend base: Cpp.A;
49+
// CHECK:STDERR: fail_todo_impl_abstract_member.carbon:[[@LINE+4]]:3: error: impl without compatible virtual in base class [ImplWithoutVirtualInBase]
50+
// CHECK:STDERR: impl fn f[addr self: Self*]() {}
51+
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
52+
// CHECK:STDERR:
53+
impl fn f[addr self: Self*]() {}
54+
}
55+
56+
// --- abstract_final.h
57+
58+
// C++ allows a class to be both abstract and final.
59+
struct AbstractFinal final {
60+
virtual void f() = 0;
61+
};
62+
63+
// --- fail_derive_from_abstract_final.carbon
64+
65+
library "[[@TEST_NAME]]";
66+
67+
import Cpp library "abstract_final.h";
68+
69+
class C {
70+
// CHECK:STDERR: fail_derive_from_abstract_final.carbon:[[@LINE+4]]:16: error: deriving from final type `AbstractFinal`; base type must be an `abstract` or `base` class [BaseIsFinal]
71+
// CHECK:STDERR: extend base: Cpp.AbstractFinal;
72+
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~
73+
// CHECK:STDERR:
74+
extend base: Cpp.AbstractFinal;
75+
}
76+
77+
// --- use_abstract_final.carbon
78+
79+
library "[[@TEST_NAME]]";
80+
81+
import Cpp library "abstract_final.h";
82+
83+
// We model an abstract final class as being final, rather than abstract. This
84+
// prevents inheritance from it, and there's no actual way to initialize an
85+
// instance of the type, but that does not prevent defining a function that
86+
// returns the type by value. Make sure that doesn't crash.
87+
//@dump-sem-ir-begin
88+
fn F() -> Cpp.AbstractFinal {
89+
return F();
90+
}
91+
//@dump-sem-ir-end
92+
93+
// CHECK:STDOUT: --- use_abstract_final.carbon
94+
// CHECK:STDOUT:
95+
// CHECK:STDOUT: constants {
96+
// CHECK:STDOUT: %AbstractFinal: type = class_type @AbstractFinal [concrete]
97+
// CHECK:STDOUT: %pattern_type.2e0: type = pattern_type %AbstractFinal [concrete]
98+
// CHECK:STDOUT: %F.type: type = fn_type @F [concrete]
99+
// CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [concrete]
100+
// CHECK:STDOUT: %F: %F.type = struct_value () [concrete]
101+
// CHECK:STDOUT: %T.as.Destroy.impl.Op.type.4da: type = fn_type @T.as.Destroy.impl.Op, @T.as.Destroy.impl(%AbstractFinal) [concrete]
102+
// CHECK:STDOUT: %T.as.Destroy.impl.Op.0fa: %T.as.Destroy.impl.Op.type.4da = struct_value () [concrete]
103+
// CHECK:STDOUT: %ptr.d4c: type = ptr_type %AbstractFinal [concrete]
104+
// CHECK:STDOUT: }
105+
// CHECK:STDOUT:
106+
// CHECK:STDOUT: imports {
107+
// CHECK:STDOUT: %Cpp: <namespace> = namespace file.%Cpp.import_cpp, [concrete] {
108+
// CHECK:STDOUT: .AbstractFinal = %AbstractFinal.decl
109+
// CHECK:STDOUT: import Cpp//...
110+
// CHECK:STDOUT: }
111+
// CHECK:STDOUT: %AbstractFinal.decl: type = class_decl @AbstractFinal [concrete = constants.%AbstractFinal] {} {}
112+
// CHECK:STDOUT: }
113+
// CHECK:STDOUT:
114+
// CHECK:STDOUT: file {
115+
// CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [concrete = constants.%F] {
116+
// CHECK:STDOUT: %return.patt: %pattern_type.2e0 = return_slot_pattern [concrete]
117+
// CHECK:STDOUT: %return.param_patt: %pattern_type.2e0 = out_param_pattern %return.patt, call_param0 [concrete]
118+
// CHECK:STDOUT: } {
119+
// CHECK:STDOUT: %Cpp.ref: <namespace> = name_ref Cpp, imports.%Cpp [concrete = imports.%Cpp]
120+
// CHECK:STDOUT: %AbstractFinal.ref: type = name_ref AbstractFinal, imports.%AbstractFinal.decl [concrete = constants.%AbstractFinal]
121+
// CHECK:STDOUT: %return.param: ref %AbstractFinal = out_param call_param0
122+
// CHECK:STDOUT: %return: ref %AbstractFinal = return_slot %return.param
123+
// CHECK:STDOUT: }
124+
// CHECK:STDOUT: }
125+
// CHECK:STDOUT:
126+
// CHECK:STDOUT: fn @F() -> %return.param: %AbstractFinal {
127+
// CHECK:STDOUT: !entry:
128+
// CHECK:STDOUT: %F.ref: %F.type = name_ref F, file.%F.decl [concrete = constants.%F]
129+
// CHECK:STDOUT: %.loc11: ref %AbstractFinal = splice_block %return {}
130+
// CHECK:STDOUT: %F.call: init %AbstractFinal = call %F.ref() to %.loc11
131+
// CHECK:STDOUT: %T.as.Destroy.impl.Op.bound: <bound method> = bound_method %.loc11, constants.%T.as.Destroy.impl.Op.0fa
132+
// CHECK:STDOUT: <elided>
133+
// CHECK:STDOUT: %bound_method: <bound method> = bound_method %.loc11, %T.as.Destroy.impl.Op.specific_fn
134+
// CHECK:STDOUT: %addr: %ptr.d4c = addr_of %.loc11
135+
// CHECK:STDOUT: %T.as.Destroy.impl.Op.call: init %empty_tuple.type = call %bound_method(%addr)
136+
// CHECK:STDOUT: return %F.call to %return
137+
// CHECK:STDOUT: }
138+
// CHECK:STDOUT:

0 commit comments

Comments
 (0)