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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
250 changes: 151 additions & 99 deletions cpp2rust/converter/converter.cpp

Large diffs are not rendered by default.

26 changes: 16 additions & 10 deletions cpp2rust/converter/converter.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <map>
#include <memory>
#include <optional>
#include <set>
#include <string>
#include <type_traits>
#include <unordered_map>
Expand Down Expand Up @@ -108,6 +109,7 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
static void EmitGlobalInits(Model model, std::string &out);

static void EmitVirtualMethods(std::string &out);
static void EmitUndefinedRecordTraits(Model model, std::string &out);

virtual bool VisitBuiltinType(clang::BuiltinType *type);

Expand Down Expand Up @@ -358,17 +360,10 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {

virtual const char *GetConversionTraitName() const;

virtual std::string ConvertPtrRead(clang::QualType type,
const std::string &src);
virtual const char *GetCopyOrMoveTraitName(const RecordTrait &trait) const;

virtual std::string ConvertPtrWrite(const std::string &dst,
const std::string &value);

virtual std::string ConvertPointeeMethodCall(const char *method,
const std::string &dst,
const std::string &src);

virtual const char *GetMoveCtorTraitName() const;
void AddCopyAndMoveDerives(const clang::RecordDecl *decl,
std::vector<const char *> &attrs);

virtual std::string DerefPtrExpr(std::string_view ptr_expr,
clang::QualType pointee_type);
Expand Down Expand Up @@ -892,6 +887,12 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {

virtual void ConvertAbstractClass(clang::CXXRecordDecl *decl);

void ConvertCopyOrMoveTraitImpls(const clang::CXXRecordDecl *decl,
bool (*predicate)(clang::CXXMethodDecl *));

void ConvertCopyOrMoveTraitImpl(clang::CXXMethodDecl *method,
const RecordTrait &trait);

void ConvertCXXMethodDecls(const clang::CXXRecordDecl *decl,
const std::string_view signature,
bool (*predicate)(clang::CXXMethodDecl *));
Expand Down Expand Up @@ -1271,6 +1272,11 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
std::string body;
};
static std::map<std::string, DeferredBlock> virtual_methods_;
static std::map<std::string, std::map<std::string, const RecordTrait *>>
declared_record_traits_;
static std::set<std::pair<std::string, std::string>> converted_record_traits_;

void DeclareRecordTraits(const clang::CXXRecordDecl *decl);

static void EmitDeferredBlock(const DeferredBlock &block, std::string &out);

Expand Down
103 changes: 95 additions & 8 deletions cpp2rust/converter/converter_lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,58 @@ bool CanUseCopyOrMoveName(const clang::CXXMethodDecl *decl,
return is_unique_member && !HasMethodNamed(record, name);
}

static bool IsTranslatedCopyAssignment(const clang::CXXMethodDecl *method);

static bool IsTranslatedMoveAssignment(const clang::CXXMethodDecl *method);

static bool IsTranslatedCopyOrMove(const clang::CXXMethodDecl *method) {
if (auto *ctor = clang::dyn_cast<clang::CXXConstructorDecl>(method)) {
return IsConvertibleCopyOrMoveConstructor(ctor);
}
return method->isCopyAssignmentOperator()
? IsTranslatedCopyAssignment(method)
: IsTranslatedMoveAssignment(method);
}

static bool IsUserWrittenMember(const clang::CXXMethodDecl *method) {
if (!method->isUserProvided() || !IsUserDefinedDecl(method)) {
return false;
}
auto *definition = method->getDefinition();
return !definition || !definition->isDefaulted();
}

const RecordTrait *GetRecordTrait(const clang::CXXMethodDecl *method) {
method = method->getCanonicalDecl();
if (auto *dtor = clang::dyn_cast<clang::CXXDestructorDecl>(method)) {
return (IsUserWrittenMember(dtor) || IsConvertedDestructor(dtor)) &&
!dtor->getParent()->isAbstract()
? &kDestructible
: nullptr;
}
const char *name = GetCopyOrMoveName(method);
if (!name || method->isDeleted() || method->isVirtual() ||
!CanUseCopyOrMoveName(method, name) ||
(!IsUserWrittenMember(method) && !IsTranslatedCopyOrMove(method))) {
return nullptr;
}
auto *ctor = clang::dyn_cast<clang::CXXConstructorDecl>(method);
if (!ctor && !method->getReturnType()->isLValueReferenceType()) {
return nullptr;
}
auto param = method->getParamDecl(0)->getType();
bool is_copy =
ctor ? ctor->isCopyConstructor() : method->isCopyAssignmentOperator();
if (!param->isReferenceType() ||
param.getNonReferenceType().isConstQualified() != is_copy) {
return nullptr;
}
if (ctor) {
return is_copy ? &kCopyConstructible : &kMoveConstructible;
}
return is_copy ? &kCopyAssignable : &kMoveAssignable;
}

bool IsUserDefinedCopyConstructor(const clang::CXXConstructorDecl *ctor) {
return ctor->isCopyConstructor() && ctor->isUserProvided() &&
IsUserDefinedDecl(ctor);
Expand Down Expand Up @@ -731,7 +783,7 @@ static bool HasFieldWithMoveAssign(const clang::RecordDecl *decl) {
return !record->hasTrivialMoveAssignment();
}
return llvm::any_of(record->methods(), [](auto *method) {
return IsConvertibleMoveAssignment(method);
return IsTranslatedMoveAssignment(method);
});
});
}
Expand All @@ -743,11 +795,22 @@ static bool NeedsDefaultedMoveAssignment(const clang::CXXRecordDecl *decl) {

bool IsConvertibleMoveAssignment(const clang::CXXMethodDecl *method) {
auto *parent = method->getParent();
if (!method->isMoveAssignmentOperator() || method->isUserProvided() ||
method->isDeleted() || !method->hasBody() ||
!IsConvertibleImplicitMemberParent(parent)) {
return false;
}
return NeedsDefaultedMoveAssignment(parent);
}

static bool IsTranslatedMoveAssignment(const clang::CXXMethodDecl *method) {
if (!method->isMoveAssignmentOperator() || method->isDeleted() ||
!method->hasBody() || !IsConvertibleImplicitMemberParent(parent)) {
method->isVirtual()) {
return false;
}
return method->isUserProvided() || NeedsDefaultedMoveAssignment(parent);
return (method->isUserProvided() && IsUserDefinedDecl(method) &&
method->hasBody()) ||
IsConvertibleMoveAssignment(method);
}

static bool IsTranslatedCopyAssignment(const clang::CXXMethodDecl *method) {
Expand All @@ -771,7 +834,7 @@ bool HasCopyAssignOperator(const clang::RecordDecl *decl) {
bool HasMoveAssignOperator(const clang::RecordDecl *decl) {
auto *cxx = clang::dyn_cast<clang::CXXRecordDecl>(decl);
return cxx && llvm::any_of(cxx->methods(), [](auto *method) {
return IsConvertibleMoveAssignment(method) && !method->isVirtual() &&
return IsTranslatedMoveAssignment(method) &&
CanUseCopyOrMoveName(method, "move_assign");
});
}
Expand Down Expand Up @@ -918,8 +981,8 @@ bool IsConvertibleCXXRecordDecl(const clang::CXXRecordDecl *decl) {
}

bool IsConvertibleCXXMethodDecl(const clang::CXXMethodDecl *decl) {
if (llvm::isa<clang::CXXDestructorDecl>(decl)) {
return GetUserDefinedDestructor(decl->getParent()) != nullptr;
if (auto *dtor = llvm::dyn_cast<clang::CXXDestructorDecl>(decl)) {
return IsConvertedDestructor(dtor);
}
return !decl->isImplicit() || IsComparisonOperator(decl) ||
IsConvertibleImplicitMember(decl);
Expand Down Expand Up @@ -1756,6 +1819,23 @@ GetUserDefinedDestructor(const clang::CXXRecordDecl *decl) {
return dtor;
}

bool IsConvertedDestructor(const clang::CXXDestructorDecl *dtor) {
auto *parent = dtor->getParent();
if (auto *user = GetUserDefinedDestructor(parent)) {
return user->getCanonicalDecl() == dtor->getCanonicalDecl();
}
return !dtor->isUserProvided() && !dtor->isDeleted() && dtor->hasBody() &&
IsUserDefinedDecl(parent) && HasFieldsNeedingDestruction(parent);
}

bool ImplementsRecordTrait(const clang::RecordDecl *decl,
const RecordTrait &trait) {
auto *cxx = clang::dyn_cast<clang::CXXRecordDecl>(decl);
return cxx && llvm::any_of(cxx->methods(), [&](auto *method) {
return GetRecordTrait(method) == &trait;
});
}

bool TypeNeedsDestruction(clang::QualType type) {
if (type->isArrayType()) {
type = clang::QualType(type->getBaseElementTypeUnsafe(), 0);
Expand Down Expand Up @@ -1862,6 +1942,13 @@ void DefineImplicitMembers(clang::Sema &sema, clang::CXXRecordDecl *decl) {
});
}
}
if (auto *dtor = decl->getDestructor();
dtor && !dtor->isUserProvided() &&
!dtor->doesThisDeclarationHaveABody() && !dtor->isDeleted() &&
HasFieldsNeedingDestruction(decl)) {
define(dtor,
[&] { sema.DefineImplicitDestructor(decl->getLocation(), dtor); });
}
for (auto *method : decl->methods()) {
if (method->isMoveAssignmentOperator() && !method->isUserProvided() &&
!method->doesThisDeclarationHaveABody() && !method->isDeleted() &&
Expand Down Expand Up @@ -1906,8 +1993,8 @@ bool IsEmittableMethod(clang::CXXMethodDecl *method) {
if (method->isDeleted()) {
return false;
}
if (clang::isa<clang::CXXDestructorDecl>(method)) {
return GetUserDefinedDestructor(method->getParent()) &&
if (auto *dtor = clang::dyn_cast<clang::CXXDestructorDecl>(method)) {
return IsConvertedDestructor(dtor) &&
method->isThisDeclarationADefinition();
}
// Virtual methods go into the base trait impl
Expand Down
43 changes: 43 additions & 0 deletions cpp2rust/converter/converter_lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,44 @@ const char *GetCopyOrMoveName(const clang::CXXMethodDecl *method);
bool CanUseCopyOrMoveName(const clang::CXXMethodDecl *decl,
const std::string &name);

struct RecordTrait {
const char *name;
const char *unsafe_name;
const char *method;
const char *signature;
const char *unsafe_signature;
};

inline constexpr RecordTrait kCopyConstructible{
"CopyConstructible", "CopyConstructibleUnsafe", "copy_constructor",
"fn copy_constructor(_: &Ptr<Self>, _: Ptr<Self>)",
"unsafe fn copy_constructor(&mut self, _: *const Self)"};
inline constexpr RecordTrait kMoveConstructible{
"MoveConstructible", "MoveConstructibleUnsafe", "move_constructor",
"fn move_constructor(_: &Ptr<Self>, _: Ptr<Self>)",
"unsafe fn move_constructor(&mut self, _: *mut Self)"};
inline constexpr RecordTrait kCopyAssignable{
"CopyAssignable", "CopyAssignableUnsafe", "copy_assign",
"fn copy_assign(_: &Ptr<Self>, _: Ptr<Self>) -> Ptr<Self>",
"unsafe fn copy_assign(&mut self, _: *const Self) -> *mut Self"};
inline constexpr RecordTrait kMoveAssignable{
"MoveAssignable", "MoveAssignableUnsafe", "move_assign",
"fn move_assign(_: &Ptr<Self>, _: Ptr<Self>) -> Ptr<Self>",
"unsafe fn move_assign(&mut self, _: *mut Self) -> *mut Self"};
inline constexpr RecordTrait kSwappable{
"Swappable", "SwappableUnsafe", "swap",
"fn swap(_: &Ptr<Self>, _: Ptr<Self>)",
"unsafe fn swap(&mut self, _: *mut Self)"};
inline constexpr RecordTrait kDestructible{
"Destructible", "DestructibleUnsafe", "destroy",
"fn destroy(_: &Ptr<Self>)", "unsafe fn destroy(&mut self)"};
inline constexpr RecordTrait kMovable{"Movable", "MovableUnsafe", nullptr,
nullptr, nullptr};
inline constexpr RecordTrait kCopyable{"Copyable", "CopyableUnsafe", nullptr,
nullptr, nullptr};

const RecordTrait *GetRecordTrait(const clang::CXXMethodDecl *method);

bool IsUserDefinedCopyConstructor(const clang::CXXConstructorDecl *ctor);

bool IsConvertibleCopyOrMoveConstructor(const clang::CXXConstructorDecl *ctor);
Expand Down Expand Up @@ -288,6 +326,11 @@ bool IsSameTypeComparison(const clang::FunctionDecl *fn,
clang::CXXDestructorDecl *
GetUserDefinedDestructor(const clang::CXXRecordDecl *decl);

bool IsConvertedDestructor(const clang::CXXDestructorDecl *dtor);

bool ImplementsRecordTrait(const clang::RecordDecl *decl,
const RecordTrait &trait);

bool TypeNeedsDestruction(clang::QualType type);

bool HasFieldsNeedingDestruction(const clang::CXXRecordDecl *decl);
Expand Down
Loading
Loading