Skip to content
  •  
  •  
  •  
100 changes: 68 additions & 32 deletions cpp2rust/converter/converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,11 @@ void Converter::EmitUndefinedRecordTraits(Model model, std::string &out) {
if (converted_record_traits_.contains({record, name})) {
continue;
}
const auto &spelling = is_unsafe ? trait->unsafe : trait->refcount;
out += std::format(
"impl {} for {} {{ {} {{ unreachable!(\"{}::{} is never defined\") "
"}} }}\n",
is_unsafe ? trait->unsafe_name : trait->name, record,
is_unsafe ? trait->unsafe_signature : trait->signature, record,
trait->method);
spelling.name, record, spelling.signature, record, trait->method);
}
}
}
Expand All @@ -88,16 +87,16 @@ void Converter::DeclareRecordTraits(const clang::CXXRecordDecl *decl) {
for (auto *method : decl->methods()) {
if (auto *trait = GetRecordTrait(method);
trait && !IsDerivedCopyOrMove(method)) {
traits.emplace(trait->name, trait);
traits.emplace(trait->refcount.name, trait);
}
}
if (traits.contains(kCopyConstructible.name) &&
if (traits.contains(kCopyConstructible.refcount.name) &&
!decl->hasUserDeclaredMoveConstructor()) {
traits.emplace(kMoveConstructible.name, &kMoveConstructible);
traits.emplace(kMoveConstructible.refcount.name, &kMoveConstructible);
}
if (traits.contains(kCopyAssignable.name) &&
if (traits.contains(kCopyAssignable.refcount.name) &&
!decl->hasUserDeclaredMoveAssignment()) {
traits.emplace(kMoveAssignable.name, &kMoveAssignable);
traits.emplace(kMoveAssignable.refcount.name, &kMoveAssignable);
}
}

Expand Down Expand Up @@ -1098,7 +1097,7 @@ void Converter::ConvertCopyOrMoveTraitImpl(clang::CXXMethodDecl *method,
return;
}
converted_record_traits_.emplace(GetRecordName(method->getParent()),
trait.name);
trait.refcount.name);
StrCat(keyword::kImpl, GetCopyOrMoveTraitName(trait), "for",
GetRecordName(method->getParent()));
PushBrace brace(*this);
Expand Down Expand Up @@ -2663,8 +2662,13 @@ std::string Converter::GetUFCSName(const clang::CXXMethodDecl *method) const {
return GetRecordName(method->getParent());
}

const RecordTraitSpelling &
Converter::GetRecordTraitSpelling(const RecordTrait &trait) const {
return trait.unsafe;
}

const char *Converter::GetCopyOrMoveTraitName(const RecordTrait &trait) const {
return trait.unsafe_name;
return GetRecordTraitSpelling(trait).name;
}

void Converter::ConvertUserOperatorCall(clang::CXXOperatorCallExpr *expr) {
Expand Down Expand Up @@ -5248,10 +5252,7 @@ Converter::GetStructAttributes(const clang::RecordDecl *decl) {
if (decl->isUnion()) {
std::vector<const char *> union_attrs = {
"Copy", "Clone", "FnPtrArg", "VaArg", "ZeroInitializationUnsafe"};
if (!RecordDerivesTriviallyCopyable(decl)) {
union_attrs.emplace_back("DestructibleUnsafe");
}
AddCopyAndMoveDerives(decl, union_attrs);
AddSpecialMemberDerives(decl, union_attrs);
return union_attrs;
}

Expand All @@ -5275,12 +5276,7 @@ Converter::GetStructAttributes(const clang::RecordDecl *decl) {
struct_attrs.emplace_back("CppFromUnsafe");
}

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

AddCopyAndMoveDerives(decl, struct_attrs);
AddSpecialMemberDerives(decl, struct_attrs);

if (RecordDerivesDefault(decl)) {
struct_attrs.emplace_back("Default");
Expand Down Expand Up @@ -5802,8 +5798,8 @@ void Converter::AddCloneTrait(const clang::RecordDecl *decl) {
TakePlacementValue()));
}

void Converter::AddCopyAndMoveDerives(const clang::RecordDecl *decl,
std::vector<const char *> &attrs) {
void Converter::AddSpecialMemberDerives(const clang::RecordDecl *decl,
std::vector<const char *> &attrs) {
auto implements = [&](const RecordTrait *trait) {
return ImplementsRecordTrait(decl, *trait);
};
Expand All @@ -5812,9 +5808,26 @@ void Converter::AddCopyAndMoveDerives(const clang::RecordDecl *decl,
attrs.emplace_back(GetCopyOrMoveTraitName(trait));
for (const auto *provided_trait : provided) {
converted_record_traits_.emplace(GetRecordName(decl),
provided_trait->name);
provided_trait->refcount.name);
}
};
auto *cxx = clang::dyn_cast<clang::CXXRecordDecl>(decl);
bool trivial_default = !cxx || cxx->hasTrivialDefaultConstructor();
if (trivial_default) {
attrs.emplace_back(GetCopyOrMoveTraitName(kTriviallyDefaultConstructible));
}
bool derives_default = !trivial_default &&
!implements(&kDefaultConstructible) &&
!GetConvertedDefaultConstructor(decl);
bool derives_destructible =
!implements(&kDestructible) && !RecordDerivesTriviallyCopyable(decl);
if (derives_default && derives_destructible) {
attrs.emplace_back(GetCopyOrMoveTraitName(kDefaultInitializable));
} else if (derives_default) {
attrs.emplace_back(GetCopyOrMoveTraitName(kDefaultConstructible));
} else if (derives_destructible) {
attrs.emplace_back(GetCopyOrMoveTraitName(kDestructible));
}
if (!GetDerivedCopyOrMoves(decl).empty()) {
attrs.emplace_back(GetCopyOrMoveTraitName(kMemberwise));
}
Expand Down Expand Up @@ -5879,6 +5892,11 @@ void Converter::AddDefaultTraitForUnion(const clang::RecordDecl *decl) {
}

void Converter::AddDefaultTrait(const clang::RecordDecl *decl) {
if (auto *default_ctor = GetConvertedDefaultConstructor(decl);
default_ctor && !GetRecordTrait(default_ctor)) {
AddDefaultConstructibleTrait(clang::cast<clang::CXXRecordDecl>(decl),
default_ctor);
}
if (decl->isUnion()) {
AddDefaultTraitForUnion(decl);
return;
Expand All @@ -5896,19 +5914,37 @@ void Converter::AddDefaultTrait(const clang::RecordDecl *decl) {
}
if (auto *default_ctor =
cxx ? GetUserDefinedDefaultConstructor(cxx) : nullptr) {
std::vector<clang::Expr *> args;
for (auto *param : default_ctor->parameters()) {
args.push_back(clang::CXXDefaultArgExpr::Create(
ctx_, param->getLocation(), param, nullptr, default_ctor));
}
Convert(MakeConstructExpr(ctx_, ctx_.getCanonicalTagType(decl),
default_ctor, args));
Convert(MakeDefaultConstructExpr(cxx, default_ctor));
} else {
EmitDefaultStructLiteral(decl);
}
});
}

clang::CXXConstructExpr *
Converter::MakeDefaultConstructExpr(const clang::CXXRecordDecl *decl,
clang::CXXConstructorDecl *ctor) {
std::vector<clang::Expr *> args;
for (auto *param : ctor->parameters()) {
args.push_back(clang::CXXDefaultArgExpr::Create(ctx_, param->getLocation(),
param, nullptr, ctor));
}
return MakeConstructExpr(ctx_, ctx_.getCanonicalTagType(decl), ctor, args);
}

void Converter::AddDefaultConstructibleTrait(const clang::CXXRecordDecl *decl,
clang::CXXConstructorDecl *ctor) {
const auto &spelling = GetRecordTraitSpelling(kDefaultConstructible);
StrCat(keyword::kImpl, spelling.name, "for", GetRecordName(decl));
PushBrace impl_brace(*this);
StrCat(spelling.signature);
PushBrace fn_brace(*this);
StrCat(keyword_unsafe_);
PushBrace unsafe_brace(*this);
auto *construct = MakeDefaultConstructExpr(decl, ctor);
ConstructInPlace(construct, construct, spelling.receiver);
}

void Converter::EmitDefaultImpl(const clang::RecordDecl *decl,
llvm::function_ref<void()> body) {
StrCat(std::format("impl Default for {}", GetRecordName(decl)));
Expand Down Expand Up @@ -6849,8 +6885,8 @@ std::string Converter::ConvertGenericCall(
assert(expr && "unbound generic outside an expression rule");
type = RuleRegistry::GetGenericType(ctx_, expr, call.generic);
}
if (call.method == "default") {
assert(call_args.empty() && "default takes no arguments");
if (call.method == "default_constructed") {
assert(call_args.empty() && "default_constructed takes no arguments");
return GetUnboxedDefaultAsString(type);
}
if (call.method == "cpp_from" || call.method == "cpp_from_move") {
Expand Down
16 changes: 13 additions & 3 deletions cpp2rust/converter/converter.h
Original file line number Diff line number Diff line change
Expand Up @@ -381,10 +381,13 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {

virtual const char *GetConversionTraitName() const;

virtual const char *GetCopyOrMoveTraitName(const RecordTrait &trait) const;
virtual const RecordTraitSpelling &
GetRecordTraitSpelling(const RecordTrait &trait) const;

void AddCopyAndMoveDerives(const clang::RecordDecl *decl,
std::vector<const char *> &attrs);
const char *GetCopyOrMoveTraitName(const RecordTrait &trait) const;

void AddSpecialMemberDerives(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 @@ -970,6 +973,13 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {

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

clang::CXXConstructExpr *
MakeDefaultConstructExpr(const clang::CXXRecordDecl *decl,
clang::CXXConstructorDecl *ctor);

void AddDefaultConstructibleTrait(const clang::CXXRecordDecl *decl,
clang::CXXConstructorDecl *ctor);

void EmitDefaultStructLiteral(const clang::RecordDecl *decl);

// Emits `impl Default for decl`, whose default() returns what body emits.
Expand Down
61 changes: 34 additions & 27 deletions cpp2rust/converter/converter_lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,9 @@ const char *GetCopyOrMoveName(const clang::CXXMethodDecl *method) {
if (ctor->isMoveConstructor()) {
return "move_from";
}
if (ctor->isDefaultConstructor() && ctor->getNumParams() == 0) {
return "default_constructor";
}
return nullptr;
}
if (method->isCopyAssignmentOperator()) {
Expand Down Expand Up @@ -606,8 +609,12 @@ static bool IsTranslatedCopyAssignment(const clang::CXXMethodDecl *method);

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

static bool IsTranslatedCopyOrMove(const clang::CXXMethodDecl *method) {
static bool IsTranslatedSpecialMember(const clang::CXXMethodDecl *method) {
if (auto *ctor = clang::dyn_cast<clang::CXXConstructorDecl>(method)) {
if (ctor->isDefaultConstructor()) {
return !ctor->getParent()->isUnion() &&
IsConvertibleDefaultConstructor(ctor);
}
return IsConvertibleCopyOrMoveConstructor(ctor);
}
return method->isCopyAssignmentOperator()
Expand All @@ -633,13 +640,16 @@ const RecordTrait *GetRecordTrait(const clang::CXXMethodDecl *method) {
const char *name = GetCopyOrMoveName(method);
if (!name || method->isDeleted() || method->isVirtual() ||
!CanUseCopyOrMoveName(method, name) ||
(!IsUserWrittenMember(method) && !IsTranslatedCopyOrMove(method))) {
(!IsUserWrittenMember(method) && !IsTranslatedSpecialMember(method))) {
return nullptr;
}
auto *ctor = clang::dyn_cast<clang::CXXConstructorDecl>(method);
if (!ctor && !method->getReturnType()->isLValueReferenceType()) {
return nullptr;
}
if (ctor && ctor->isDefaultConstructor()) {
return &kDefaultConstructible;
}
auto param = method->getParamDecl(0)->getType();
bool is_copy =
ctor ? ctor->isCopyConstructor() : method->isCopyAssignmentOperator();
Expand All @@ -654,6 +664,10 @@ const RecordTrait *GetRecordTrait(const clang::CXXMethodDecl *method) {
}

bool IsDerivedCopyOrMove(const clang::CXXMethodDecl *method) {
if (auto *ctor = clang::dyn_cast<clang::CXXConstructorDecl>(method);
ctor && ctor->isDefaultConstructor()) {
return false;
}
return !method->isUserProvided() && GetRecordTrait(method);
}

Expand Down Expand Up @@ -708,35 +722,11 @@ bool IsConvertibleCopyConstructor(const clang::CXXConstructorDecl *ctor) {
HasFieldWithCopyConstructor(ctor->getParent()));
}

// Whether default-initializing a field of decl runs a translated constructor,
// which receives the field in place.
static bool HasFieldWithDefaultConstructor(const clang::CXXRecordDecl *decl) {
for (const auto *field : decl->fields()) {
if (auto *init = field->getInClassInitializer()) {
if (GetPlacementInitializer(init)) {
return true;
}
continue;
}
auto *record = field->getType()->getAsCXXRecordDecl();
if (!record || !record->hasDefinition() || !IsUserDefinedDecl(record) ||
record->isLambda()) {
continue;
}
if (record->hasUserProvidedDefaultConstructor() ||
(IsConvertibleImplicitMemberParent(record) &&
HasFieldWithDefaultConstructor(record))) {
return true;
}
}
return false;
}

bool IsConvertibleDefaultConstructor(const clang::CXXConstructorDecl *ctor) {
return ctor->isDefaultConstructor() && !ctor->isUserProvided() &&
!ctor->isDeleted() &&
IsConvertibleImplicitMemberParent(ctor->getParent()) &&
HasFieldWithDefaultConstructor(ctor->getParent());
!ctor->isTrivial();
}

// Whether moving a field of decl isn't a copy: the field is a record with a
Expand Down Expand Up @@ -1248,6 +1238,23 @@ GetUserDefinedDefaultConstructor(const clang::CXXRecordDecl *decl) {
return nullptr;
}

clang::CXXConstructorDecl *
GetConvertedDefaultConstructor(const clang::RecordDecl *decl) {
auto *cxx = clang::dyn_cast<clang::CXXRecordDecl>(decl);
if (!cxx || cxx->isUnion()) {
return nullptr;
}
if (auto *ctor = GetUserDefinedDefaultConstructor(cxx)) {
return ctor;
}
for (auto *ctor : cxx->ctors()) {
if (IsConvertibleDefaultConstructor(ctor)) {
return ctor;
}
}
return nullptr;
}

bool HasUsableDefaultArg(const clang::ParmVarDecl *param) {
return param->hasDefaultArg() && !param->hasUninstantiatedDefaultArg();
}
Expand Down
Loading
Loading