@@ -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
0 commit comments