Skip to content

Commit 33e255c

Browse files
Rename default instances.
This CL prepares upcoming changes to collocate message-globals under a single wrapper (go/proto-msg-globals). The plan is to keep dual state guarded by PROTOBUF_MESSAGE_GLOBALS, which allows smaller CLs and potential A/B experiments. Exising code exposes a raw default instance to messages that contain the type to support constinit or constexpr. Using `T::default_instance()` would encapsulate #ifdef'ing but I didn't find a way to achieve that. (Also, constexpr doesn't allow reinterpret_cast.) This CL renames existing raw default instances so that both branches (#ifdef & #ifndef) can refer to the same name. ``` // Before: struct FooDefaultTypeInternal; extern FooDefaultTypeInternal _Foo_default_instance_; DoSomething(_Foo_default_instance_._instance); // After struct FooGlobalsTypeInternal; extern FooGlobalsTypeInternal _Foo_globals_; DoSomething(_Foo_globals_._default); ``` Note that this is meant to be name changes only. PiperOrigin-RevId: 864580684
1 parent 2aa094e commit 33e255c

File tree

15 files changed

+684
-719
lines changed

15 files changed

+684
-719
lines changed

src/google/protobuf/compiler/cpp/extension.cc

Lines changed: 38 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -159,52 +159,44 @@ void ExtensionGenerator::GenerateDeclaration(io::Printer* p) const {
159159

160160
void ExtensionGenerator::GenerateDefinition(io::Printer* p) {
161161
auto vars = p->WithVars(variables_);
162-
auto generate_default_string = [&] {
163-
if (descriptor_->cpp_type() == FieldDescriptor::CPPTYPE_STRING) {
164-
// We need to declare a global string which will contain the default
165-
// value. We cannot declare it at class scope because that would require
166-
// exposing it in the header which would be annoying for other reasons. So
167-
// we replace :: with _ in the name and declare it as a global.
168-
return absl::StrReplaceAll(variables_["scoped_name"], {{"::", "_"}}) +
169-
"_default";
170-
} else if (descriptor_->message_type()) {
171-
// We have to initialize the default instance for extensions at
172-
// registration time.
173-
return absl::StrCat("&", QualifiedDefaultInstanceName(
174-
descriptor_->message_type(), options_));
175-
} else {
176-
return DefaultValue(options_, descriptor_);
177-
}
178-
};
179-
180-
auto local_var = p->WithVars({
181-
{"default_str", generate_default_string()},
182-
{"default_val", DefaultValue(options_, descriptor_)},
183-
{"message_type", descriptor_->message_type() != nullptr
184-
? FieldMessageTypeName(descriptor_, options_)
185-
: ""},
186-
});
187-
p->Emit(
188-
{
189-
{"declare_default_str",
190-
[&] {
191-
if (descriptor_->cpp_type() != FieldDescriptor::CPPTYPE_STRING)
192-
return;
193-
194-
// If this is a class member, it needs to be declared in its class
195-
// scope.
196-
p->Emit(R"cc(
197-
const std::string $default_str$($default_val$);
198-
)cc");
199-
}},
200-
},
201-
R"cc(
202-
$declare_default_str$;
203-
PROTOBUF_CONSTINIT$ dllexport_decl$
204-
PROTOBUF_ATTRIBUTE_INIT_PRIORITY2 ::_pbi::ExtensionIdentifier<
205-
$extendee$, ::_pbi::$type_traits$, $field_type$, $packed$>
206-
$scoped_name$($constant_name$, $default_str$);
207-
)cc");
162+
if (descriptor_->cpp_type() == FieldDescriptor::CPPTYPE_STRING) {
163+
// We need to declare a global string which will contain the default
164+
// value. We cannot declare it at class scope because that would require
165+
// exposing it in the header which would be annoying for other reasons. So
166+
// we replace :: with _ in the name and declare it as a global.
167+
p->Emit({{"default_str",
168+
absl::StrReplaceAll(variables_["scoped_name"], {{"::", "_"}}) +
169+
"_default"},
170+
{"default_val", DefaultValue(options_, descriptor_)}},
171+
// If this is a class member, it needs to be declared in its class
172+
// scope.
173+
R"cc(
174+
const std::string $default_str$($default_val$);
175+
PROTOBUF_CONSTINIT$ dllexport_decl$
176+
PROTOBUF_ATTRIBUTE_INIT_PRIORITY2 ::_pbi::ExtensionIdentifier<
177+
$extendee$, ::_pbi::$type_traits$, $field_type$, $packed$>
178+
$scoped_name$($constant_name$, $default_str$);
179+
)cc");
180+
} else if (descriptor_->message_type()) {
181+
// We have to initialize the default instance for extensions at
182+
// registration time.
183+
p->Emit({{"globals_name", QualifiedMsgGlobalsInstanceName(
184+
descriptor_->message_type(), options_)}},
185+
R"cc(
186+
PROTOBUF_CONSTINIT$ dllexport_decl$
187+
PROTOBUF_ATTRIBUTE_INIT_PRIORITY2 ::_pbi::ExtensionIdentifier<
188+
$extendee$, ::_pbi::$type_traits$, $field_type$, $packed$>
189+
$scoped_name$($constant_name$, &$globals_name$);
190+
)cc");
191+
} else {
192+
p->Emit({{"default_val", DefaultValue(options_, descriptor_)}},
193+
R"cc(
194+
PROTOBUF_CONSTINIT$ dllexport_decl$
195+
PROTOBUF_ATTRIBUTE_INIT_PRIORITY2 ::_pbi::ExtensionIdentifier<
196+
$extendee$, ::_pbi::$type_traits$, $field_type$, $packed$>
197+
$scoped_name$($constant_name$, $default_val$);
198+
)cc");
199+
}
208200
}
209201

210202
bool ExtensionGenerator::WillGenerateRegistration(InitPriority priority) {

src/google/protobuf/compiler/cpp/field_generators/message_field.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ std::vector<Sub> Vars(const FieldDescriptor* field, const Options& opts,
4545
std::string field_name = FieldMemberName(field, split);
4646
std::string qualified_type = FieldMessageTypeName(field, opts);
4747
std::string default_ref =
48-
QualifiedDefaultInstanceName(field->message_type(), opts);
48+
QualifiedMsgGlobalsInstanceName(field->message_type(), opts);
4949
std::string base = absl::StrCat(
5050
"::", ProtobufNamespace(opts), "::",
5151
HasDescriptorMethods(field->file(), opts) ? "Message" : "MessageLite");

src/google/protobuf/compiler/cpp/file.cc

Lines changed: 34 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -578,10 +578,10 @@ void FileGenerator::GenerateSourceDefaultInstance(int idx, io::Printer* p) {
578578
// there just to improve performance and binary size in these builds.
579579
p->Emit(
580580
{
581-
{"type", DefaultInstanceType(generator->descriptor(), options_,
582-
/*split=*/true)},
583-
{"name", DefaultInstanceName(generator->descriptor(), options_,
584-
/*split=*/true)},
581+
{"type",
582+
SplitDefaultInstanceType(generator->descriptor(), options_)},
583+
{"name",
584+
SplitDefaultInstanceName(generator->descriptor(), options_)},
585585
{"default",
586586
[&] { generator->GenerateInitDefaultSplitInstance(p); }},
587587
{"class", absl::StrCat(ClassName(generator->descriptor()),
@@ -602,24 +602,24 @@ void FileGenerator::GenerateSourceDefaultInstance(int idx, io::Printer* p) {
602602

603603
generator->GenerateConstexprConstructor(p);
604604

605+
auto v = p->WithVars({
606+
{"type", MsgGlobalsInstanceType(generator->descriptor(), options_)},
607+
{"name", MsgGlobalsInstanceName(generator->descriptor(), options_)},
608+
{"class", ClassName(generator->descriptor())},
609+
});
605610
if (IsFileDescriptorProto(file_, options_)) {
606611
p->Emit(
607-
{
608-
{"type", DefaultInstanceType(generator->descriptor(), options_)},
609-
{"name", DefaultInstanceName(generator->descriptor(), options_)},
610-
{"class", ClassName(generator->descriptor())},
611-
},
612612
R"cc(
613613
struct $type$ {
614614
#if defined(PROTOBUF_CONSTINIT_DEFAULT_INSTANCES)
615-
constexpr $type$() : _instance(::_pbi::ConstantInitialized{}) {}
615+
constexpr $type$() : _default(::_pbi::ConstantInitialized{}) {}
616616
#else // defined(PROTOBUF_CONSTINIT_DEFAULT_INSTANCES)
617617
$type$() {}
618-
void Init() { ::new (&_instance) $class$(); };
618+
void Init() { ::new (&_default) $class$(); };
619619
#endif // defined(PROTOBUF_CONSTINIT_DEFAULT_INSTANCES)
620620
~$type$() {}
621621
union {
622-
$class$ _instance;
622+
$class$ _default;
623623
};
624624
};
625625
@@ -630,20 +630,17 @@ void FileGenerator::GenerateSourceDefaultInstance(int idx, io::Printer* p) {
630630
p->Emit(
631631
{
632632
{"index", generator->index_in_file_messages()},
633-
{"type", DefaultInstanceType(generator->descriptor(), options_)},
634-
{"name", DefaultInstanceName(generator->descriptor(), options_)},
635-
{"class", ClassName(generator->descriptor())},
636633
{"section", WeakDefaultInstanceSection(
637634
generator->descriptor(),
638635
generator->index_in_file_messages(), options_)},
639636
},
640637
R"cc(
641638
struct $type$ {
642-
constexpr $type$() : _instance(::_pbi::ConstantInitialized{}) {}
639+
constexpr $type$() : _default(::_pbi::ConstantInitialized{}) {}
643640
~$type$() {}
644-
//~ _instance must be the first member.
641+
//~ _default must be the first member.
645642
union {
646-
$class$ _instance;
643+
$class$ _default;
647644
};
648645
::_pbi::WeakDescriptorDefaultTail tail = {
649646
file_default_instances + $index$, sizeof($type$)};
@@ -655,17 +652,12 @@ void FileGenerator::GenerateSourceDefaultInstance(int idx, io::Printer* p) {
655652
)cc");
656653
} else {
657654
p->Emit(
658-
{
659-
{"type", DefaultInstanceType(generator->descriptor(), options_)},
660-
{"name", DefaultInstanceName(generator->descriptor(), options_)},
661-
{"class", ClassName(generator->descriptor())},
662-
},
663655
R"cc(
664656
struct $type$ {
665-
constexpr $type$() : _instance(::_pbi::ConstantInitialized{}) {}
657+
constexpr $type$() : _default(::_pbi::ConstantInitialized{}) {}
666658
~$type$() {}
667659
union {
668-
$class$ _instance;
660+
$class$ _default;
669661
};
670662
};
671663
@@ -677,8 +669,8 @@ void FileGenerator::GenerateSourceDefaultInstance(int idx, io::Printer* p) {
677669
if (options_.lite_implicit_weak_fields) {
678670
p->Emit(
679671
{
680-
{"ptr", DefaultInstancePtr(generator->descriptor(), options_)},
681-
{"name", DefaultInstanceName(generator->descriptor(), options_)},
672+
{"ptr", MsgGlobalsInstancePtr(generator->descriptor(), options_)},
673+
{"name", MsgGlobalsInstanceName(generator->descriptor(), options_)},
682674
},
683675
R"cc(
684676
PROTOBUF_CONSTINIT const void* $ptr$ = &$name$;
@@ -754,13 +746,13 @@ void FileGenerator::GenerateInternalForwardDeclarations(
754746
ns.ChangeTo(Namespace(instance, options_));
755747

756748
if (options_.lite_implicit_weak_fields) {
757-
p->Emit({{"ptr", DefaultInstancePtr(instance, options_)}}, R"cc(
749+
p->Emit({{"ptr", MsgGlobalsInstancePtr(instance, options_)}}, R"cc(
758750
PROTOBUF_CONSTINIT __attribute__((weak)) const void* $ptr$ =
759751
&::_pbi::implicit_weak_message_default_instance;
760752
)cc");
761753
} else {
762-
p->Emit({{"type", DefaultInstanceType(instance, options_)},
763-
{"name", DefaultInstanceName(instance, options_)}},
754+
p->Emit({{"type", MsgGlobalsInstanceType(instance, options_)},
755+
{"name", MsgGlobalsInstanceName(instance, options_)}},
764756
R"cc(
765757
extern __attribute__((weak)) $type$ $name$;
766758
)cc");
@@ -1216,7 +1208,7 @@ void FileGenerator::GenerateReflectionInitializationCode(io::Printer* p) {
12161208
{"class", ClassName(gen->descriptor())},
12171209
},
12181210
R"cc(
1219-
&$ns$::_$class$_default_instance_._instance,
1211+
&$ns$::_$class$_globals_._default,
12201212
)cc");
12211213
}
12221214
}}};
@@ -1401,7 +1393,7 @@ void FileGenerator::GenerateReflectionInitializationCode(io::Printer* p) {
14011393
[&](std::string* out, const auto& gen) {
14021394
absl::StrAppend(
14031395
out,
1404-
DefaultInstanceName(
1396+
MsgGlobalsInstanceName(
14051397
gen->descriptor(), options_),
14061398
".Init();");
14071399
})}},
@@ -1444,14 +1436,14 @@ class FileGenerator::ForwardDeclarations {
14441436
p->Emit(
14451437
{
14461438
Sub("class", c.first).AnnotatedAs(desc),
1447-
{"default_type", DefaultInstanceType(desc, options)},
1448-
{"default_name", DefaultInstanceName(desc, options)},
1439+
{"globals_type", MsgGlobalsInstanceType(desc, options)},
1440+
{"globals_name", MsgGlobalsInstanceName(desc, options)},
14491441
{"classdata_type", ClassDataType(desc, options)},
14501442
},
14511443
R"cc(
14521444
class $class$;
1453-
struct $default_type$;
1454-
$dllexport_decl $extern $default_type$ $default_name$;
1445+
struct $globals_type$;
1446+
$dllexport_decl $extern $globals_type$ $globals_name$;
14551447
$dllexport_decl $extern const $pbi$::$classdata_type$ $class$_class_data_;
14561448
)cc");
14571449
}
@@ -1460,10 +1452,8 @@ class FileGenerator::ForwardDeclarations {
14601452
const Descriptor* desc = s.second;
14611453
p->Emit(
14621454
{
1463-
{"default_type",
1464-
DefaultInstanceType(desc, options, /*split=*/true)},
1465-
{"default_name",
1466-
DefaultInstanceName(desc, options, /*split=*/true)},
1455+
{"default_type", SplitDefaultInstanceType(desc, options)},
1456+
{"default_name", SplitDefaultInstanceName(desc, options)},
14671457
},
14681458
R"cc(
14691459
struct $default_type$;
@@ -1484,10 +1474,10 @@ class FileGenerator::ForwardDeclarations {
14841474
if (ShouldGenerateExternSpecializations(options)) {
14851475
for (const auto& c : classes_) {
14861476
if (!ShouldGenerateClass(c.second, options)) continue;
1487-
auto vars = p->WithVars(
1488-
{{"class", QualifiedClassName(c.second, options)},
1489-
{"default_name", QualifiedDefaultInstanceName(c.second, options,
1490-
/*split=*/false)}});
1477+
auto vars =
1478+
p->WithVars({{"class", QualifiedClassName(c.second, options)},
1479+
{"default_name",
1480+
QualifiedMsgGlobalsInstanceName(c.second, options)}});
14911481
// To reduce total linker input size in large binaries we make these
14921482
// functions extern and define then in the pb.cc file. This avoids bloat
14931483
// in callers by having duplicate definitions of the template.

src/google/protobuf/compiler/cpp/helpers.cc

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -572,33 +572,48 @@ std::string Namespace(const EnumDescriptor* d, const Options& options) {
572572
return Namespace(d->file(), options);
573573
}
574574

575-
std::string DefaultInstanceType(const Descriptor* descriptor,
576-
const Options& /*options*/, bool split) {
577-
return ClassName(descriptor) + (split ? "__Impl_Split" : "") +
578-
"DefaultTypeInternal";
575+
std::string SplitDefaultInstanceType(const Descriptor* descriptor,
576+
const Options& /*options*/) {
577+
return absl::StrCat(ClassName(descriptor), "__Impl_SplitDefaultTypeInternal");
579578
}
580579

581-
std::string DefaultInstanceName(const Descriptor* descriptor,
582-
const Options& /*options*/, bool split) {
580+
std::string SplitDefaultInstanceName(const Descriptor* descriptor,
581+
const Options& /*options*/) {
583582
return absl::StrCat("_", ClassName(descriptor, false),
584-
(split ? "__Impl_Split" : ""), "_default_instance_");
583+
"__Impl_Split_default_instance_");
585584
}
586585

587-
std::string DefaultInstancePtr(const Descriptor* descriptor,
588-
const Options& options, bool split) {
589-
return absl::StrCat(DefaultInstanceName(descriptor, options, split), "ptr_");
586+
std::string MsgGlobalsInstanceType(const Descriptor* descriptor,
587+
const Options& /*options*/) {
588+
return absl::StrCat(ClassName(descriptor), "GlobalsTypeInternal");
590589
}
591590

592-
std::string QualifiedDefaultInstanceName(const Descriptor* descriptor,
593-
const Options& options, bool split) {
591+
std::string MsgGlobalsInstanceName(const Descriptor* descriptor,
592+
const Options& /*options*/) {
593+
return absl::StrCat("_", ClassName(descriptor, false), "_globals_");
594+
}
595+
596+
std::string MsgGlobalsInstancePtr(const Descriptor* descriptor,
597+
const Options& options) {
598+
return absl::StrCat(MsgGlobalsInstanceName(descriptor, options), "ptr_");
599+
}
600+
601+
std::string QualifiedSplitDefaultInstanceName(const Descriptor* descriptor,
602+
const Options& options) {
603+
return QualifiedFileLevelSymbol(descriptor->file(),
604+
SplitDefaultInstanceName(descriptor, options),
605+
options);
606+
}
607+
608+
std::string QualifiedMsgGlobalsInstanceName(const Descriptor* descriptor,
609+
const Options& options) {
594610
return QualifiedFileLevelSymbol(
595-
descriptor->file(), DefaultInstanceName(descriptor, options, split),
596-
options);
611+
descriptor->file(), MsgGlobalsInstanceName(descriptor, options), options);
597612
}
598613

599-
std::string QualifiedDefaultInstancePtr(const Descriptor* descriptor,
600-
const Options& options, bool split) {
601-
return absl::StrCat(QualifiedDefaultInstanceName(descriptor, options, split),
614+
std::string QualifiedMsgGlobalsInstancePtr(const Descriptor* descriptor,
615+
const Options& options) {
616+
return absl::StrCat(QualifiedMsgGlobalsInstanceName(descriptor, options),
602617
"ptr_");
603618
}
604619

@@ -1729,7 +1744,7 @@ bool UsingImplicitWeakDescriptor(const FileDescriptor* file,
17291744

17301745
std::string StrongReferenceToType(const Descriptor* desc,
17311746
const Options& options) {
1732-
const auto name = QualifiedDefaultInstanceName(desc, options);
1747+
const auto name = QualifiedMsgGlobalsInstanceName(desc, options);
17331748
return absl::StrFormat("::%s::internal::StrongPointer<decltype(%s)*, &%s>()",
17341749
ProtobufNamespace(options), name, name);
17351750
}

0 commit comments

Comments
 (0)