Skip to content

Commit 6c4c340

Browse files
authored
Delete abstract_structs_ (#526)
We don't need extra state to translate abstract structs.
1 parent 318c416 commit 6c4c340

5 files changed

Lines changed: 13 additions & 7 deletions

File tree

‎cpp2rust/converter/converter.cpp‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ std::unordered_map<std::string, std::string> Converter::inner_structs_;
3030
std::unordered_set<std::string> Converter::decl_ids_;
3131
std::unordered_set<std::string> Converter::globals_;
3232
std::vector<std::string> Converter::global_inits_;
33-
std::unordered_set<std::string> Converter::abstract_structs_;
3433
Converter::RecordIndex Converter::record_decls_;
3534
std::map<std::string, Converter::DeferredBlock> Converter::virtual_methods_;
3635

@@ -318,7 +317,7 @@ bool Converter::VisitPointerType(clang::PointerType *type) {
318317
auto pointee_type = type->getPointeeType();
319318
StrCat(pointee_type.isConstQualified() ? "*const" : "*mut");
320319
if (pointee_type->isRecordType() &&
321-
abstract_structs_.contains(GetID(pointee_type->getAsRecordDecl()))) {
320+
IsAbstractUserClass(pointee_type->getAsRecordDecl())) {
322321
StrCat(keyword::kDyn);
323322
}
324323
return Convert(pointee_type);
@@ -4634,7 +4633,6 @@ pub fn main() {{
46344633
}
46354634

46364635
void Converter::ConvertAbstractClass(clang::CXXRecordDecl *decl) {
4637-
ENSURE(abstract_structs_.insert(GetID(decl)).second);
46384636
auto trait_name = GetRecordName(decl);
46394637
auto access_specifier_as_string = AccessSpecifierAsString(decl->getAccess());
46404638
auto signature = std::format("{} {} trait {}", access_specifier_as_string,

‎cpp2rust/converter/converter.h‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -952,7 +952,6 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
952952
};
953953

954954
static std::unordered_set<std::string> decl_ids_;
955-
static std::unordered_set<std::string> abstract_structs_;
956955

957956
class RecordIndex {
958957
public:

‎cpp2rust/converter/converter_lib.cpp‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,13 @@ bool IsUserDefinedDecl(const clang::Decl *decl) {
302302
!src_mgr.isInSystemMacro(src_loc);
303303
}
304304

305+
bool IsAbstractUserClass(const clang::RecordDecl *decl) {
306+
auto cxx = llvm::dyn_cast<clang::CXXRecordDecl>(decl);
307+
auto definition = cxx ? cxx->getDefinition() : nullptr;
308+
return definition && IsUserDefinedDecl(definition) &&
309+
definition->isAbstract();
310+
}
311+
305312
bool RefersToUserDefinedDecl(const clang::Expr *expr) {
306313
expr = expr->IgnoreParenImpCasts();
307314
const clang::Decl *decl = nullptr;

‎cpp2rust/converter/converter_lib.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ bool IsCodeUnitStringLiteral(const clang::StringLiteral *expr);
6666

6767
bool IsUserDefinedDecl(const clang::Decl *decl);
6868

69+
bool IsAbstractUserClass(const clang::RecordDecl *decl);
70+
6971
bool RefersToUserDefinedDecl(const clang::Expr *expr);
7072

7173
bool IsUnsignedArithOp(const clang::BinaryOperator *expr);

‎cpp2rust/converter/models/converter_refcount.cpp‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ bool ConverterRefCount::VisitPointerType(clang::PointerType *type) {
341341
PushConversionKind push2(*this, ConversionKind::FullRefCount,
342342
pointee_is_array);
343343
if (pointee_type->isRecordType() &&
344-
abstract_structs_.contains(GetID(pointee_type->getAsRecordDecl()))) {
344+
IsAbstractUserClass(pointee_type->getAsRecordDecl())) {
345345
StrCat("PtrDyn<dyn");
346346
} else {
347347
StrCat("Ptr<");
@@ -1499,7 +1499,7 @@ bool ConverterRefCount::VisitImplicitCastExpr(clang::ImplicitCastExpr *expr) {
14991499
auto ptype = clang::dyn_cast<clang::PointerType>(expr->getType());
15001500
auto pointee_type = ptype->getPointeeType()->getAsCXXRecordDecl();
15011501

1502-
if (pointee_type && abstract_structs_.contains(GetID(pointee_type))) {
1502+
if (pointee_type && IsAbstractUserClass(pointee_type)) {
15031503
PushConversionKind push(*this, ConversionKind::Unboxed);
15041504
StrCat(std::format("{}.to_dyn::<{}>(|w| w)",
15051505
ToString(sub_expr->IgnoreCasts()),
@@ -3214,7 +3214,7 @@ void ConverterRefCount::ConvertArrow(clang::Expr *expr) {
32143214
auto pointee_type = expr->getType()->getPointeeType();
32153215
// Virtual methods, which take &mut self, are called through PtrDyn.
32163216
if (auto *record = pointee_type->getAsRecordDecl();
3217-
isLValue() && record && abstract_structs_.contains(GetID(record))) {
3217+
isLValue() && record && IsAbstractUserClass(record)) {
32183218
StrCat(std::format("(*{}.upgrade().deref_mut())", ToString(expr)));
32193219
SetValueFreshness(pointee_type);
32203220
return;

0 commit comments

Comments
 (0)