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
  •  
  •  
  •  
55 changes: 42 additions & 13 deletions cpp2rust/converter/converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,21 @@ void Converter::EmitUndefinedRecordTraits(Model model, std::string &out) {
}

void Converter::DeclareRecordTraits(const clang::CXXRecordDecl *decl) {
auto &traits = declared_record_traits_[GetRecordName(decl)];
for (auto *method : decl->methods()) {
if (auto *trait = GetRecordTrait(method);
trait && !IsDerivedCopyOrMove(method)) {
declared_record_traits_[GetRecordName(decl)].emplace(trait->name, trait);
traits.emplace(trait->name, trait);
}
}
if (traits.contains(kCopyConstructible.name) &&
!decl->hasUserDeclaredMoveConstructor()) {
traits.emplace(kMoveConstructible.name, &kMoveConstructible);
}
if (traits.contains(kCopyAssignable.name) &&
!decl->hasUserDeclaredMoveAssignment()) {
traits.emplace(kMoveAssignable.name, &kMoveAssignable);
}
}

std::string Converter::ForceGlobalInit(const clang::VarDecl *decl) {
Expand Down Expand Up @@ -5145,12 +5154,11 @@ std::string Converter::GetRecordName(const clang::NamedDecl *decl) const {
std::vector<const char *>
Converter::GetStructAttributes(const clang::RecordDecl *decl) {
if (decl->isUnion()) {
std::vector<const char *> union_attrs = {"Copy",
"Clone",
"FnPtrArg",
"VaArg",
"ZeroInitializationUnsafe",
"DestructibleUnsafe"};
std::vector<const char *> union_attrs = {
"Copy", "Clone", "FnPtrArg", "VaArg", "ZeroInitializationUnsafe"};
if (!RecordDerivesTriviallyCopyable(decl)) {
union_attrs.emplace_back("DestructibleUnsafe");
}
AddCopyAndMoveDerives(decl, union_attrs);
return union_attrs;
}
Expand All @@ -5175,7 +5183,8 @@ Converter::GetStructAttributes(const clang::RecordDecl *decl) {
struct_attrs.emplace_back("CppFromUnsafe");
}

if (!ImplementsRecordTrait(decl, kDestructible)) {
if (!ImplementsRecordTrait(decl, kDestructible) &&
!RecordDerivesTriviallyCopyable(decl)) {
struct_attrs.emplace_back("DestructibleUnsafe");
}

Expand Down Expand Up @@ -5703,42 +5712,62 @@ void Converter::AddCopyAndMoveDerives(const clang::RecordDecl *decl,
auto implements = [&](const RecordTrait *trait) {
return ImplementsRecordTrait(decl, *trait);
};
auto derive = [&](const RecordTrait &trait,
std::initializer_list<const RecordTrait *> provided) {
attrs.emplace_back(GetCopyOrMoveTraitName(trait));
for (const auto *provided_trait : provided) {
converted_record_traits_.emplace(GetRecordName(decl),
provided_trait->name);
}
};
if (!GetDerivedCopyOrMoves(decl).empty()) {
attrs.emplace_back(GetCopyOrMoveTraitName(kMemberwise));
}
const RecordTrait *copy[] = {&kCopyConstructible, &kCopyAssignable};
const RecordTrait *move[] = {&kMoveConstructible, &kMoveAssignable};
if (RecordDerivesTriviallyCopyable(decl)) {
derive(kTriviallyCopyable,
{&kCopyConstructible, &kCopyAssignable, &kMoveConstructible,
&kMoveAssignable, &kSwappable, &kDestructible});
return;
}
bool copies = decl->isUnion() || RecordImplementsClone(decl);
bool writes_move = llvm::any_of(move, implements);
if (copies && !writes_move && llvm::none_of(copy, implements)) {
attrs.emplace_back(GetCopyOrMoveTraitName(kCopyable));
derive(kCopyable, {&kCopyConstructible, &kCopyAssignable,
&kMoveConstructible, &kMoveAssignable, &kSwappable});
return;
}
if (copies) {
for (const auto *trait : copy) {
if (!implements(trait)) {
attrs.emplace_back(GetCopyOrMoveTraitName(*trait));
derive(*trait, {trait});
}
}
}
if (!writes_move) {
if (copies) {
attrs.emplace_back(GetCopyOrMoveTraitName(kMovable));
derive(kMovable, {&kMoveConstructible, &kMoveAssignable, &kSwappable});
}
return;
}
if (copies) {
for (const auto *trait : move) {
if (!implements(trait)) {
attrs.emplace_back(GetCopyOrMoveTraitName(*trait));
derive(*trait, {trait});
}
}
}
if (copies || llvm::all_of(move, implements)) {
attrs.emplace_back(GetCopyOrMoveTraitName(kSwappable));
derive(kSwappable, {&kSwappable});
}
}

bool Converter::RecordDerivesTriviallyCopyable(const clang::RecordDecl *decl) {
return (decl->isUnion() || RecordImplementsClone(decl)) &&
IsTriviallyCopyable(decl);
}

bool Converter::RecordImplementsClone(const clang::RecordDecl *decl) {
return HasDefaultedCopyConstructor(decl) ||
GetUserDefinedCopyConstructor(decl) != nullptr;
Expand Down
2 changes: 2 additions & 0 deletions cpp2rust/converter/converter.h
Original file line number Diff line number Diff line change
Expand Up @@ -958,6 +958,8 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {

virtual bool RecordImplementsClone(const clang::RecordDecl *decl);

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

virtual void AddDefaultTrait(const clang::RecordDecl *decl);

virtual void AddDefaultTraitForUnion(const clang::RecordDecl *decl);
Expand Down
28 changes: 21 additions & 7 deletions cpp2rust/converter/converter_lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -711,19 +711,25 @@ bool IsConvertibleCopyConstructor(const clang::CXXConstructorDecl *ctor) {
// Whether default-initializing a field of decl runs a translated constructor,
// which receives the field in place.
static bool HasFieldWithDefaultConstructor(const clang::CXXRecordDecl *decl) {
return llvm::any_of(decl->fields(), [](const clang::FieldDecl *field) {
for (const auto *field : decl->fields()) {
if (auto *init = field->getInClassInitializer()) {
return GetInPlaceConstruct(init) != nullptr;
if (GetInPlaceConstruct(init)) {
return true;
}
continue;
}
auto *record = field->getType()->getAsCXXRecordDecl();
if (!record || !record->hasDefinition() || !IsUserDefinedDecl(record) ||
record->isLambda()) {
return false;
continue;
}
return record->hasUserProvidedDefaultConstructor() ||
(IsConvertibleImplicitMemberParent(record) &&
HasFieldWithDefaultConstructor(record));
});
if (record->hasUserProvidedDefaultConstructor() ||
(IsConvertibleImplicitMemberParent(record) &&
HasFieldWithDefaultConstructor(record))) {
return true;
}
}
return false;
}

bool IsConvertibleDefaultConstructor(const clang::CXXConstructorDecl *ctor) {
Expand Down Expand Up @@ -974,6 +980,14 @@ bool HasDefaultedCopyConstructor(const clang::RecordDecl *decl) {
return HasCallableCopyConstructor(cxx);
}

bool IsTriviallyCopyable(const clang::RecordDecl *decl) {
auto *cxx = clang::dyn_cast<clang::CXXRecordDecl>(decl);
if (!cxx) {
return true;
}
return cxx->isTriviallyCopyable();
}

bool RecordHasOnlyReferenceFields(const clang::RecordDecl *decl) {
for (auto *field : decl->fields()) {
if (!field->getType()->isReferenceType()) {
Expand Down
4 changes: 4 additions & 0 deletions cpp2rust/converter/converter_lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ inline constexpr RecordTrait kMovable{"Movable", "MovableUnsafe", nullptr,
nullptr, nullptr};
inline constexpr RecordTrait kCopyable{"Copyable", "CopyableUnsafe", nullptr,
nullptr, nullptr};
inline constexpr RecordTrait kTriviallyCopyable{
"TriviallyCopyable", "TriviallyCopyableUnsafe", nullptr, nullptr, nullptr};
inline constexpr RecordTrait kMemberwise{"Memberwise", "MemberwiseUnsafe",
nullptr, nullptr, nullptr};

Expand Down Expand Up @@ -207,6 +209,8 @@ bool HasCallableCopyConstructor(const clang::RecordDecl *decl);

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

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

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

bool HasDefaultedCopyAssignment(const clang::RecordDecl *decl);
Expand Down
25 changes: 18 additions & 7 deletions cpp2rust/converter/models/converter_refcount.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -632,13 +632,21 @@ bool ConverterRefCount::VisitOffsetOfExpr(clang::OffsetOfExpr *expr) {
}

// The operands of comparison operators are pointers to the values (&Ptr).
std::string ConverterRefCount::GetComparisonArg(const clang::ParmVarDecl *param,
const clang::CXXRecordDecl *,
std::string_view value) {
std::string
ConverterRefCount::GetComparisonArg(const clang::ParmVarDecl *param,
const clang::CXXRecordDecl *decl,
std::string_view value) {
if (param->getType()->isReferenceType()) {
return std::format("{}.clone()", value);
}
return std::format("{}.read()", value);
if (IsTriviallyCopyable(decl)) {
return std::format("{}.read()", value);
}
return std::format("{{ {} {}::{}({}, {}.clone()); {} }}",
CreatePlacementStorage(GetRecordName(decl)),
GetCopyOrMoveTraitName(kCopyConstructible),
kCopyConstructible.method, GetPlacementReceiver(), value,
TakePlacementValue());
}

std::string ConverterRefCount::GetComparisonResultReceiver(
Expand Down Expand Up @@ -2342,8 +2350,10 @@ ConverterRefCount::GetStructAttributes(const clang::RecordDecl *decl) {
std::vector<const char *> attrs;

if (decl->isUnion()) {
std::vector<const char *> union_attrs = {"VaArg", "FnPtrArg",
"Destructible"};
std::vector<const char *> union_attrs = {"VaArg", "FnPtrArg"};
if (!RecordDerivesTriviallyCopyable(decl)) {
union_attrs.emplace_back("Destructible");
}
AddCopyAndMoveDerives(decl, union_attrs);
return union_attrs;
}
Expand Down Expand Up @@ -2371,7 +2381,8 @@ ConverterRefCount::GetStructAttributes(const clang::RecordDecl *decl) {
attrs.emplace_back("CppFrom");
}

if (!ImplementsRecordTrait(decl, kDestructible)) {
if (!ImplementsRecordTrait(decl, kDestructible) &&
!RecordDerivesTriviallyCopyable(decl)) {
attrs.emplace_back("Destructible");
}

Expand Down
Loading
Loading