Skip to content

Commit 86495dc

Browse files
committed
Use default literals instead of Default::default() in const initialization
1 parent 999aabf commit 86495dc

2 files changed

Lines changed: 43 additions & 7 deletions

File tree

cpp2rust/converter/converter.cpp

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,8 @@ void Converter::ConvertVarDecl(clang::VarDecl *decl) {
445445
return;
446446
}
447447
auto qual_type = decl->getType();
448+
PushConstInitializer static_init(*this, decl->isFileVarDecl() ||
449+
decl->isStaticLocal());
448450
if (decl->hasInit()) {
449451
StrCat(token::kAssign);
450452
ConvertVarInit(qual_type, decl->getInit());
@@ -3043,6 +3045,18 @@ std::string Converter::GetDefaultAsStringFallback(clang::QualType qual_type) {
30433045
return std::format("0.0_{}", ToString(qual_type));
30443046
}
30453047

3048+
if (auto record = qual_type->getAsRecordDecl();
3049+
record && in_const_initializer_) {
3050+
if (auto cxx = clang::dyn_cast<clang::CXXRecordDecl>(record)) {
3051+
assert(GetUserDefinedDefaultConstructor(cxx) == nullptr &&
3052+
"Default initializing globals using default constructor is not "
3053+
"supported");
3054+
}
3055+
Buffer buf(*this);
3056+
EmitDefaultStructLiteral(record);
3057+
return std::move(buf).str();
3058+
}
3059+
30463060
return std::format("<{}>::default()", ToString(qual_type));
30473061
}
30483062

@@ -3458,13 +3472,15 @@ void Converter::AddDefaultTrait(const clang::RecordDecl *decl) {
34583472
}
34593473
}
34603474

3461-
StrCat(struct_name);
3462-
{
3463-
PushBrace struct_brace(*this);
3464-
for (auto *field : decl->fields()) {
3465-
StrCat(GetNamedDeclAsString(field), token::kColon,
3466-
GetDefaultAsString(field->getType()), token::kComma);
3467-
}
3475+
EmitDefaultStructLiteral(decl);
3476+
}
3477+
3478+
void Converter::EmitDefaultStructLiteral(const clang::RecordDecl *decl) {
3479+
StrCat(GetRecordName(decl));
3480+
PushBrace brace(*this);
3481+
for (auto *field : decl->fields()) {
3482+
StrCat(GetNamedDeclAsString(field), token::kColon,
3483+
GetDefaultAsString(field->getType()), token::kComma);
34683484
}
34693485
}
34703486

cpp2rust/converter/converter.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,8 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
464464

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

467+
void EmitDefaultStructLiteral(const clang::RecordDecl *decl);
468+
467469
virtual void AddByteReprTrait(const clang::RecordDecl *decl);
468470

469471
virtual void
@@ -517,6 +519,7 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
517519
clang::ASTContext &ctx_;
518520
clang::FunctionDecl *curr_function_ = nullptr;
519521
bool in_function_formals_ = false;
522+
bool in_const_initializer_ = false;
520523
std::optional<bool> autoref_mut_;
521524

522525
struct PushExplicitAutoref {
@@ -528,6 +531,23 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
528531
}
529532
~PushExplicitAutoref() { c.autoref_mut_ = prev; }
530533
};
534+
535+
struct PushConstInitializer {
536+
Converter &c;
537+
bool prev;
538+
bool enabled;
539+
PushConstInitializer(Converter &c, bool enabled)
540+
: c(c), prev(c.in_const_initializer_), enabled(enabled) {
541+
if (enabled) {
542+
c.in_const_initializer_ = true;
543+
}
544+
}
545+
~PushConstInitializer() {
546+
if (enabled) {
547+
c.in_const_initializer_ = prev;
548+
}
549+
}
550+
};
531551
std::stack<clang::Expr *> curr_for_inc_;
532552
std::stack<clang::QualType> curr_init_type_;
533553

0 commit comments

Comments
 (0)