Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions cpp2rust/converter/converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ std::unordered_map<std::string, std::string> Converter::inner_structs_;
std::unordered_set<std::string> Converter::decl_ids_;
std::unordered_set<std::string> Converter::globals_;
std::vector<std::string> Converter::global_inits_;
std::unordered_set<std::string> Converter::abstract_structs_;
Converter::RecordIndex Converter::record_decls_;
std::map<std::string, Converter::DeferredBlock> Converter::virtual_methods_;

Expand Down Expand Up @@ -318,7 +317,7 @@ bool Converter::VisitPointerType(clang::PointerType *type) {
auto pointee_type = type->getPointeeType();
StrCat(pointee_type.isConstQualified() ? "*const" : "*mut");
if (pointee_type->isRecordType() &&
abstract_structs_.contains(GetID(pointee_type->getAsRecordDecl()))) {
IsAbstractUserClass(pointee_type->getAsRecordDecl())) {
StrCat(keyword::kDyn);
}
return Convert(pointee_type);
Expand Down Expand Up @@ -4629,7 +4628,6 @@ pub fn main() {{
}

void Converter::ConvertAbstractClass(clang::CXXRecordDecl *decl) {
ENSURE(abstract_structs_.insert(GetID(decl)).second);
auto trait_name = GetRecordName(decl);
auto access_specifier_as_string = AccessSpecifierAsString(decl->getAccess());
auto signature = std::format("{} {} trait {}", access_specifier_as_string,
Expand Down
1 change: 0 additions & 1 deletion cpp2rust/converter/converter.h
Original file line number Diff line number Diff line change
Expand Up @@ -952,7 +952,6 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
};

static std::unordered_set<std::string> decl_ids_;
static std::unordered_set<std::string> abstract_structs_;

class RecordIndex {
public:
Expand Down
7 changes: 7 additions & 0 deletions cpp2rust/converter/converter_lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,13 @@ bool IsUserDefinedDecl(const clang::Decl *decl) {
!src_mgr.isInSystemMacro(src_loc);
}

bool IsAbstractUserClass(const clang::RecordDecl *decl) {
auto cxx = llvm::dyn_cast<clang::CXXRecordDecl>(decl);
auto definition = cxx ? cxx->getDefinition() : nullptr;
return definition && IsUserDefinedDecl(definition) &&
definition->isAbstract();
}

bool RefersToUserDefinedDecl(const clang::Expr *expr) {
expr = expr->IgnoreParenImpCasts();
const clang::Decl *decl = nullptr;
Expand Down
2 changes: 2 additions & 0 deletions cpp2rust/converter/converter_lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ bool IsCodeUnitStringLiteral(const clang::StringLiteral *expr);

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

bool IsAbstractUserClass(const clang::RecordDecl *decl);

bool RefersToUserDefinedDecl(const clang::Expr *expr);

bool IsUnsignedArithOp(const clang::BinaryOperator *expr);
Expand Down
6 changes: 3 additions & 3 deletions cpp2rust/converter/models/converter_refcount.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ bool ConverterRefCount::VisitPointerType(clang::PointerType *type) {
PushConversionKind push2(*this, ConversionKind::FullRefCount,
pointee_is_array);
if (pointee_type->isRecordType() &&
abstract_structs_.contains(GetID(pointee_type->getAsRecordDecl()))) {
IsAbstractUserClass(pointee_type->getAsRecordDecl())) {
StrCat("PtrDyn<dyn");
} else {
StrCat("Ptr<");
Expand Down Expand Up @@ -1507,7 +1507,7 @@ bool ConverterRefCount::VisitImplicitCastExpr(clang::ImplicitCastExpr *expr) {
auto ptype = clang::dyn_cast<clang::PointerType>(expr->getType());
auto pointee_type = ptype->getPointeeType()->getAsCXXRecordDecl();

if (pointee_type && abstract_structs_.contains(GetID(pointee_type))) {
if (pointee_type && IsAbstractUserClass(pointee_type)) {
PushConversionKind push(*this, ConversionKind::Unboxed);
StrCat(std::format("{}.to_dyn::<{}>(|w| w)",
ToString(sub_expr->IgnoreCasts()),
Expand Down Expand Up @@ -3222,7 +3222,7 @@ void ConverterRefCount::ConvertArrow(clang::Expr *expr) {
auto pointee_type = expr->getType()->getPointeeType();
// Virtual methods, which take &mut self, are called through PtrDyn.
if (auto *record = pointee_type->getAsRecordDecl();
isLValue() && record && abstract_structs_.contains(GetID(record))) {
isLValue() && record && IsAbstractUserClass(record)) {
StrCat(std::format("(*{}.upgrade().deref_mut())", ToString(expr)));
SetValueFreshness(pointee_type);
return;
Expand Down
Loading