Skip to content

Commit

Permalink
Add message type traits that can be declared in duplicate so that .pr…
Browse files Browse the repository at this point in the history
…oto.h can

add the traits for their dependencies.

Move StrongPointerToType to the new traits and remove the old codegen for it.

We will extend these traits to contain more information.

PiperOrigin-RevId: 676575099
  • Loading branch information
protobuf-github-bot authored and copybara-github committed Sep 20, 2024
1 parent 6bf86f0 commit 79f5f4f
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 78 deletions.
19 changes: 17 additions & 2 deletions src/google/protobuf/compiler/cpp/file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1460,20 +1460,35 @@ class FileGenerator::ForwardDeclarations {
if (ShouldGenerateExternSpecializations(options)) {
for (const auto& c : classes_) {
if (!ShouldGenerateClass(c.second, options)) continue;
auto vars = p->WithVars(
{{"class", QualifiedClassName(c.second, options)},
{"default_name", QualifiedDefaultInstanceName(c.second, options,
/*split=*/false)}});
// To reduce total linker input size in large binaries we make these
// functions extern and define then in the pb.cc file. This avoids bloat
// in callers by having duplicate definitions of the template.
// However, it increases the size of the pb.cc translation units so it
// is a tradeoff.
p->Emit({{"class", QualifiedClassName(c.second, options)}}, R"cc(
p->Emit(R"cc(
extern template void* Arena::DefaultConstruct<$class$>(Arena*);
)cc");
if (!IsMapEntryMessage(c.second)) {
p->Emit({{"class", QualifiedClassName(c.second, options)}}, R"cc(
p->Emit(R"cc(
extern template void* Arena::CopyConstruct<$class$>(Arena*,
const void*);
)cc");
}
// We can't make a constexpr pointer to the global if we have DLL
// linkage so skip this.
// The fallback traits are slower, but correct.
if (options.dllexport_decl.empty()) {
p->Emit(R"cc(
template <>
internal::GeneratedMessageTraitsT<decltype($default_name$),
&$default_name$>
internal::MessageTraitsImpl::value<$class$>;
)cc");
}
}
}
}
Expand Down
14 changes: 0 additions & 14 deletions src/google/protobuf/compiler/cpp/message.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2041,18 +2041,6 @@ void MessageGenerator::GenerateClassDefinition(io::Printer* p) {
$pbi$::ParseContext* ctx);
)cc");
}},
{"raw_default_instance",
[&] {
// We can't make a constexpr pointer to the global if we have DLL
// linkage so skip this. The fallback in
// `MessageLite::GetStrongPointerForType` will do the right thing in
// those platforms.
if (!options_.dllexport_decl.empty()) return;
p->Emit(R"cc(
static constexpr const void* _raw_default_instance_ =
&_$classname$_default_instance_;
)cc");
}},
{"decl_impl", [&] { GenerateImplDefinition(p); }},
{"classdata_type",
HasDescriptorMethods(descriptor_->file(), options_)
Expand Down Expand Up @@ -2210,8 +2198,6 @@ void MessageGenerator::GenerateClassDefinition(io::Printer* p) {
$decl_data$;
$post_loop_handler$;
$raw_default_instance$;
friend class ::$proto_ns$::MessageLite;
friend class ::$proto_ns$::Arena;
template <typename T>
Expand Down
1 change: 0 additions & 1 deletion src/google/protobuf/compiler/java/java_features.pb.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions src/google/protobuf/compiler/plugin.pb.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion src/google/protobuf/cpp_features.pb.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 0 additions & 33 deletions src/google/protobuf/descriptor.pb.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 79f5f4f

Please sign in to comment.