Skip to content

Commit cc5f8a2

Browse files
authored
Add Swappable trait (#689)
Depends on #688 Add the swappable trait and delete the cpp_swap macro. `swap_by_move` is a new libcc2rs function with the necessary traits: ```rs pub fn swap_by_move<T>(this: &Ptr<T>, other: Ptr<T>) where T: MoveConstructible + MoveAssignable + Destructible + ZeroInitialization ``` The standard explicitely requires MoveConstructible and MoveAssignable, and implicitly requires Destructible. ZeroInitialization is an artefact of refcount which needs storage to build objects into it. ZeroInitialization has no side-effects so it's ok to have the trait attached. --- I also had to change Destructible to be implemented by records with empty destruction, otherwise records with empty Destructible could not call swap_by_move.
1 parent de0e4d7 commit cc5f8a2

630 files changed

Lines changed: 16977 additions & 6106 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎cpp2rust/converter/converter.cpp‎

Lines changed: 151 additions & 99 deletions
Large diffs are not rendered by default.

‎cpp2rust/converter/converter.h‎

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <map>
1414
#include <memory>
1515
#include <optional>
16+
#include <set>
1617
#include <string>
1718
#include <type_traits>
1819
#include <unordered_map>
@@ -108,6 +109,7 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
108109
static void EmitGlobalInits(Model model, std::string &out);
109110

110111
static void EmitVirtualMethods(std::string &out);
112+
static void EmitUndefinedRecordTraits(Model model, std::string &out);
111113

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

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

359361
virtual const char *GetConversionTraitName() const;
360362

361-
virtual std::string ConvertPtrRead(clang::QualType type,
362-
const std::string &src);
363+
virtual const char *GetCopyOrMoveTraitName(const RecordTrait &trait) const;
363364

364-
virtual std::string ConvertPtrWrite(const std::string &dst,
365-
const std::string &value);
366-
367-
virtual std::string ConvertPointeeMethodCall(const char *method,
368-
const std::string &dst,
369-
const std::string &src);
370-
371-
virtual const char *GetMoveCtorTraitName() const;
365+
void AddCopyAndMoveDerives(const clang::RecordDecl *decl,
366+
std::vector<const char *> &attrs);
372367

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

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

890+
void ConvertCopyOrMoveTraitImpls(const clang::CXXRecordDecl *decl,
891+
bool (*predicate)(clang::CXXMethodDecl *));
892+
893+
void ConvertCopyOrMoveTraitImpl(clang::CXXMethodDecl *method,
894+
const RecordTrait &trait);
895+
895896
void ConvertCXXMethodDecls(const clang::CXXRecordDecl *decl,
896897
const std::string_view signature,
897898
bool (*predicate)(clang::CXXMethodDecl *));
@@ -1271,6 +1272,11 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
12711272
std::string body;
12721273
};
12731274
static std::map<std::string, DeferredBlock> virtual_methods_;
1275+
static std::map<std::string, std::map<std::string, const RecordTrait *>>
1276+
declared_record_traits_;
1277+
static std::set<std::pair<std::string, std::string>> converted_record_traits_;
1278+
1279+
void DeclareRecordTraits(const clang::CXXRecordDecl *decl);
12741280

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

‎cpp2rust/converter/converter_lib.cpp‎

Lines changed: 95 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,58 @@ bool CanUseCopyOrMoveName(const clang::CXXMethodDecl *decl,
602602
return is_unique_member && !HasMethodNamed(record, name);
603603
}
604604

605+
static bool IsTranslatedCopyAssignment(const clang::CXXMethodDecl *method);
606+
607+
static bool IsTranslatedMoveAssignment(const clang::CXXMethodDecl *method);
608+
609+
static bool IsTranslatedCopyOrMove(const clang::CXXMethodDecl *method) {
610+
if (auto *ctor = clang::dyn_cast<clang::CXXConstructorDecl>(method)) {
611+
return IsConvertibleCopyOrMoveConstructor(ctor);
612+
}
613+
return method->isCopyAssignmentOperator()
614+
? IsTranslatedCopyAssignment(method)
615+
: IsTranslatedMoveAssignment(method);
616+
}
617+
618+
static bool IsUserWrittenMember(const clang::CXXMethodDecl *method) {
619+
if (!method->isUserProvided() || !IsUserDefinedDecl(method)) {
620+
return false;
621+
}
622+
auto *definition = method->getDefinition();
623+
return !definition || !definition->isDefaulted();
624+
}
625+
626+
const RecordTrait *GetRecordTrait(const clang::CXXMethodDecl *method) {
627+
method = method->getCanonicalDecl();
628+
if (auto *dtor = clang::dyn_cast<clang::CXXDestructorDecl>(method)) {
629+
return (IsUserWrittenMember(dtor) || IsConvertedDestructor(dtor)) &&
630+
!dtor->getParent()->isAbstract()
631+
? &kDestructible
632+
: nullptr;
633+
}
634+
const char *name = GetCopyOrMoveName(method);
635+
if (!name || method->isDeleted() || method->isVirtual() ||
636+
!CanUseCopyOrMoveName(method, name) ||
637+
(!IsUserWrittenMember(method) && !IsTranslatedCopyOrMove(method))) {
638+
return nullptr;
639+
}
640+
auto *ctor = clang::dyn_cast<clang::CXXConstructorDecl>(method);
641+
if (!ctor && !method->getReturnType()->isLValueReferenceType()) {
642+
return nullptr;
643+
}
644+
auto param = method->getParamDecl(0)->getType();
645+
bool is_copy =
646+
ctor ? ctor->isCopyConstructor() : method->isCopyAssignmentOperator();
647+
if (!param->isReferenceType() ||
648+
param.getNonReferenceType().isConstQualified() != is_copy) {
649+
return nullptr;
650+
}
651+
if (ctor) {
652+
return is_copy ? &kCopyConstructible : &kMoveConstructible;
653+
}
654+
return is_copy ? &kCopyAssignable : &kMoveAssignable;
655+
}
656+
605657
bool IsUserDefinedCopyConstructor(const clang::CXXConstructorDecl *ctor) {
606658
return ctor->isCopyConstructor() && ctor->isUserProvided() &&
607659
IsUserDefinedDecl(ctor);
@@ -731,7 +783,7 @@ static bool HasFieldWithMoveAssign(const clang::RecordDecl *decl) {
731783
return !record->hasTrivialMoveAssignment();
732784
}
733785
return llvm::any_of(record->methods(), [](auto *method) {
734-
return IsConvertibleMoveAssignment(method);
786+
return IsTranslatedMoveAssignment(method);
735787
});
736788
});
737789
}
@@ -743,11 +795,22 @@ static bool NeedsDefaultedMoveAssignment(const clang::CXXRecordDecl *decl) {
743795

744796
bool IsConvertibleMoveAssignment(const clang::CXXMethodDecl *method) {
745797
auto *parent = method->getParent();
798+
if (!method->isMoveAssignmentOperator() || method->isUserProvided() ||
799+
method->isDeleted() || !method->hasBody() ||
800+
!IsConvertibleImplicitMemberParent(parent)) {
801+
return false;
802+
}
803+
return NeedsDefaultedMoveAssignment(parent);
804+
}
805+
806+
static bool IsTranslatedMoveAssignment(const clang::CXXMethodDecl *method) {
746807
if (!method->isMoveAssignmentOperator() || method->isDeleted() ||
747-
!method->hasBody() || !IsConvertibleImplicitMemberParent(parent)) {
808+
method->isVirtual()) {
748809
return false;
749810
}
750-
return method->isUserProvided() || NeedsDefaultedMoveAssignment(parent);
811+
return (method->isUserProvided() && IsUserDefinedDecl(method) &&
812+
method->hasBody()) ||
813+
IsConvertibleMoveAssignment(method);
751814
}
752815

753816
static bool IsTranslatedCopyAssignment(const clang::CXXMethodDecl *method) {
@@ -771,7 +834,7 @@ bool HasCopyAssignOperator(const clang::RecordDecl *decl) {
771834
bool HasMoveAssignOperator(const clang::RecordDecl *decl) {
772835
auto *cxx = clang::dyn_cast<clang::CXXRecordDecl>(decl);
773836
return cxx && llvm::any_of(cxx->methods(), [](auto *method) {
774-
return IsConvertibleMoveAssignment(method) && !method->isVirtual() &&
837+
return IsTranslatedMoveAssignment(method) &&
775838
CanUseCopyOrMoveName(method, "move_assign");
776839
});
777840
}
@@ -918,8 +981,8 @@ bool IsConvertibleCXXRecordDecl(const clang::CXXRecordDecl *decl) {
918981
}
919982

920983
bool IsConvertibleCXXMethodDecl(const clang::CXXMethodDecl *decl) {
921-
if (llvm::isa<clang::CXXDestructorDecl>(decl)) {
922-
return GetUserDefinedDestructor(decl->getParent()) != nullptr;
984+
if (auto *dtor = llvm::dyn_cast<clang::CXXDestructorDecl>(decl)) {
985+
return IsConvertedDestructor(dtor);
923986
}
924987
return !decl->isImplicit() || IsComparisonOperator(decl) ||
925988
IsConvertibleImplicitMember(decl);
@@ -1756,6 +1819,23 @@ GetUserDefinedDestructor(const clang::CXXRecordDecl *decl) {
17561819
return dtor;
17571820
}
17581821

1822+
bool IsConvertedDestructor(const clang::CXXDestructorDecl *dtor) {
1823+
auto *parent = dtor->getParent();
1824+
if (auto *user = GetUserDefinedDestructor(parent)) {
1825+
return user->getCanonicalDecl() == dtor->getCanonicalDecl();
1826+
}
1827+
return !dtor->isUserProvided() && !dtor->isDeleted() && dtor->hasBody() &&
1828+
IsUserDefinedDecl(parent) && HasFieldsNeedingDestruction(parent);
1829+
}
1830+
1831+
bool ImplementsRecordTrait(const clang::RecordDecl *decl,
1832+
const RecordTrait &trait) {
1833+
auto *cxx = clang::dyn_cast<clang::CXXRecordDecl>(decl);
1834+
return cxx && llvm::any_of(cxx->methods(), [&](auto *method) {
1835+
return GetRecordTrait(method) == &trait;
1836+
});
1837+
}
1838+
17591839
bool TypeNeedsDestruction(clang::QualType type) {
17601840
if (type->isArrayType()) {
17611841
type = clang::QualType(type->getBaseElementTypeUnsafe(), 0);
@@ -1862,6 +1942,13 @@ void DefineImplicitMembers(clang::Sema &sema, clang::CXXRecordDecl *decl) {
18621942
});
18631943
}
18641944
}
1945+
if (auto *dtor = decl->getDestructor();
1946+
dtor && !dtor->isUserProvided() &&
1947+
!dtor->doesThisDeclarationHaveABody() && !dtor->isDeleted() &&
1948+
HasFieldsNeedingDestruction(decl)) {
1949+
define(dtor,
1950+
[&] { sema.DefineImplicitDestructor(decl->getLocation(), dtor); });
1951+
}
18651952
for (auto *method : decl->methods()) {
18661953
if (method->isMoveAssignmentOperator() && !method->isUserProvided() &&
18671954
!method->doesThisDeclarationHaveABody() && !method->isDeleted() &&
@@ -1906,8 +1993,8 @@ bool IsEmittableMethod(clang::CXXMethodDecl *method) {
19061993
if (method->isDeleted()) {
19071994
return false;
19081995
}
1909-
if (clang::isa<clang::CXXDestructorDecl>(method)) {
1910-
return GetUserDefinedDestructor(method->getParent()) &&
1996+
if (auto *dtor = clang::dyn_cast<clang::CXXDestructorDecl>(method)) {
1997+
return IsConvertedDestructor(dtor) &&
19111998
method->isThisDeclarationADefinition();
19121999
}
19132000
// Virtual methods go into the base trait impl

‎cpp2rust/converter/converter_lib.h‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,44 @@ const char *GetCopyOrMoveName(const clang::CXXMethodDecl *method);
105105
bool CanUseCopyOrMoveName(const clang::CXXMethodDecl *decl,
106106
const std::string &name);
107107

108+
struct RecordTrait {
109+
const char *name;
110+
const char *unsafe_name;
111+
const char *method;
112+
const char *signature;
113+
const char *unsafe_signature;
114+
};
115+
116+
inline constexpr RecordTrait kCopyConstructible{
117+
"CopyConstructible", "CopyConstructibleUnsafe", "copy_constructor",
118+
"fn copy_constructor(_: &Ptr<Self>, _: Ptr<Self>)",
119+
"unsafe fn copy_constructor(&mut self, _: *const Self)"};
120+
inline constexpr RecordTrait kMoveConstructible{
121+
"MoveConstructible", "MoveConstructibleUnsafe", "move_constructor",
122+
"fn move_constructor(_: &Ptr<Self>, _: Ptr<Self>)",
123+
"unsafe fn move_constructor(&mut self, _: *mut Self)"};
124+
inline constexpr RecordTrait kCopyAssignable{
125+
"CopyAssignable", "CopyAssignableUnsafe", "copy_assign",
126+
"fn copy_assign(_: &Ptr<Self>, _: Ptr<Self>) -> Ptr<Self>",
127+
"unsafe fn copy_assign(&mut self, _: *const Self) -> *mut Self"};
128+
inline constexpr RecordTrait kMoveAssignable{
129+
"MoveAssignable", "MoveAssignableUnsafe", "move_assign",
130+
"fn move_assign(_: &Ptr<Self>, _: Ptr<Self>) -> Ptr<Self>",
131+
"unsafe fn move_assign(&mut self, _: *mut Self) -> *mut Self"};
132+
inline constexpr RecordTrait kSwappable{
133+
"Swappable", "SwappableUnsafe", "swap",
134+
"fn swap(_: &Ptr<Self>, _: Ptr<Self>)",
135+
"unsafe fn swap(&mut self, _: *mut Self)"};
136+
inline constexpr RecordTrait kDestructible{
137+
"Destructible", "DestructibleUnsafe", "destroy",
138+
"fn destroy(_: &Ptr<Self>)", "unsafe fn destroy(&mut self)"};
139+
inline constexpr RecordTrait kMovable{"Movable", "MovableUnsafe", nullptr,
140+
nullptr, nullptr};
141+
inline constexpr RecordTrait kCopyable{"Copyable", "CopyableUnsafe", nullptr,
142+
nullptr, nullptr};
143+
144+
const RecordTrait *GetRecordTrait(const clang::CXXMethodDecl *method);
145+
108146
bool IsUserDefinedCopyConstructor(const clang::CXXConstructorDecl *ctor);
109147

110148
bool IsConvertibleCopyOrMoveConstructor(const clang::CXXConstructorDecl *ctor);
@@ -288,6 +326,11 @@ bool IsSameTypeComparison(const clang::FunctionDecl *fn,
288326
clang::CXXDestructorDecl *
289327
GetUserDefinedDestructor(const clang::CXXRecordDecl *decl);
290328

329+
bool IsConvertedDestructor(const clang::CXXDestructorDecl *dtor);
330+
331+
bool ImplementsRecordTrait(const clang::RecordDecl *decl,
332+
const RecordTrait &trait);
333+
291334
bool TypeNeedsDestruction(clang::QualType type);
292335

293336
bool HasFieldsNeedingDestruction(const clang::CXXRecordDecl *decl);

0 commit comments

Comments
 (0)