Skip to content

Commit e7c656c

Browse files
committed
Make GetStructAttributes receive a RecordDecl
1 parent deda3e2 commit e7c656c

4 files changed

Lines changed: 20 additions & 18 deletions

File tree

cpp2rust/converter/converter.cpp

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -519,9 +519,11 @@ bool IsPointerType(clang::QualType qual_type) {
519519
->getCanonicalTypeInternal()));
520520
}
521521

522-
bool Converter::RecordDerivesDefault(const clang::CXXRecordDecl *decl) {
523-
if (GetUserDefinedDefaultConstructor(decl)) {
524-
return false;
522+
bool Converter::RecordDerivesDefault(const clang::RecordDecl *decl) {
523+
if (auto cxx_decl = clang::dyn_cast<clang::CXXRecordDecl>(decl)) {
524+
if (GetUserDefinedDefaultConstructor(cxx_decl)) {
525+
return false;
526+
}
525527
}
526528

527529
for (auto f : decl->fields()) {
@@ -546,7 +548,7 @@ bool Converter::RecordDerivesDefault(const clang::CXXRecordDecl *decl) {
546548
return true;
547549
}
548550

549-
static bool recordDerivesCopy(const clang::CXXRecordDecl *decl) {
551+
static bool recordDerivesCopy(const clang::RecordDecl *decl) {
550552
for (auto f : decl->fields()) {
551553
// Records that contain std::vector, std::array, std::string or anything
552554
// that is translated to Vec<>, do not derive Copy
@@ -569,8 +571,8 @@ static bool recordDerivesCopy(const clang::CXXRecordDecl *decl) {
569571
}
570572
}
571573

572-
// Look recursively into fields that are CXXRecordDecl
573-
if (auto field_record = f->getType()->getAsCXXRecordDecl()) {
574+
// Look recursively into fields that are RecordDecl
575+
if (auto field_record = f->getType()->getAsRecordDecl()) {
574576
if (!recordDerivesCopy(field_record)) {
575577
return false;
576578
}
@@ -629,12 +631,8 @@ void Converter::EmitRustStruct(clang::RecordDecl *decl) {
629631

630632
// Derived traits
631633
StrCat("#[derive(");
632-
if (auto *cxx = clang::dyn_cast<clang::CXXRecordDecl>(decl)) {
633-
for (auto *attr : GetStructAttributes(cxx)) {
634-
StrCat(attr, ",");
635-
}
636-
} else {
637-
StrCat("Clone, Default");
634+
for (auto *attr : GetStructAttributes(decl)) {
635+
StrCat(attr, ",");
638636
}
639637
StrCat(")]");
640638

@@ -2837,14 +2835,18 @@ std::string Converter::GetRecordName(const clang::NamedDecl *decl) const {
28372835
}
28382836

28392837
std::vector<const char *>
2840-
Converter::GetStructAttributes(const clang::CXXRecordDecl *decl) {
2838+
Converter::GetStructAttributes(const clang::RecordDecl *decl) {
28412839
std::vector<const char *> struct_attrs = {};
28422840

28432841
if (recordDerivesCopy(decl)) {
28442842
struct_attrs.emplace_back("Copy");
28452843
}
28462844

2847-
if (!decl->defaultedCopyConstructorIsDeleted()) {
2845+
if (auto cxx_decl = clang::dyn_cast<clang::CXXRecordDecl>(decl)) {
2846+
if (!cxx_decl->defaultedCopyConstructorIsDeleted()) {
2847+
struct_attrs.emplace_back("Clone");
2848+
}
2849+
} else /* RecordDecl */ {
28482850
struct_attrs.emplace_back("Clone");
28492851
}
28502852

cpp2rust/converter/converter.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
359359
virtual std::string GetRecordName(const clang::NamedDecl *decl) const;
360360

361361
virtual std::vector<const char *>
362-
GetStructAttributes(const clang::CXXRecordDecl *decl);
362+
GetStructAttributes(const clang::RecordDecl *decl);
363363

364364
virtual std::string GetUnsafeTypeAsString(clang::QualType qual_type) const;
365365

@@ -457,7 +457,7 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
457457

458458
virtual bool IsReferenceType(const clang::Expr *expr) const;
459459

460-
virtual bool RecordDerivesDefault(const clang::CXXRecordDecl *decl);
460+
virtual bool RecordDerivesDefault(const clang::RecordDecl *decl);
461461

462462
std::string *rs_code_;
463463
clang::ASTContext &ctx_;

cpp2rust/converter/models/converter_refcount.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1604,7 +1604,7 @@ ConverterRefCount::ConvertVarDefaultInit(clang::QualType qual_type) {
16041604
}
16051605

16061606
std::vector<const char *>
1607-
ConverterRefCount::GetStructAttributes(const clang::CXXRecordDecl *decl) {
1607+
ConverterRefCount::GetStructAttributes(const clang::RecordDecl *decl) {
16081608
std::vector<const char *> attrs = {};
16091609

16101610
if (RecordDerivesDefault(decl)) {

cpp2rust/converter/models/converter_refcount.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ class ConverterRefCount final : public Converter {
121121
std::string ConvertVarDefaultInit(clang::QualType qual_type) override;
122122

123123
std::vector<const char *>
124-
GetStructAttributes(const clang::CXXRecordDecl *decl) override;
124+
GetStructAttributes(const clang::RecordDecl *decl) override;
125125

126126
bool MayCauseBorrowMutError(const clang::Expr *lhs, const clang::Expr *rhs);
127127

0 commit comments

Comments
 (0)