Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Devirtualize CheckTypeAndMergeFrom and use the existing function from #16547

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 16 additions & 29 deletions src/google/protobuf/compiler/cpp/message.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1718,10 +1718,14 @@ void MessageGenerator::GenerateClassDefinition(io::Printer* p) {
}
} else {
p->Emit(R"cc(
void CheckTypeAndMergeFrom(
const ::$proto_ns$::MessageLite& from) final;
void CopyFrom(const $classname$& from);
void MergeFrom(const $classname$& from);
void MergeFrom(const $classname$& from) { $classname$::MergeImpl(*this, from); }

private:
static void MergeImpl(::$proto_ns$::MessageLite& to_msg,
const ::$proto_ns$::MessageLite& from_msg);

public:
)cc");
}

Expand Down Expand Up @@ -3703,10 +3707,10 @@ void MessageGenerator::GenerateClassData(io::Printer* p) {
$table$,
$on_demand_register_arena_dtor$,
$is_initialized$,
&$classname$::MergeImpl,
PROTOBUF_FIELD_OFFSET($classname$, $cached_size$),
false,
},
&$classname$::MergeImpl,
&$classname$::kDescriptorMethods,
&$desc_table$,
$tracker_on_get_metadata$,
Expand All @@ -3732,6 +3736,7 @@ void MessageGenerator::GenerateClassData(io::Printer* p) {
&_table_.header,
$on_demand_register_arena_dtor$,
$is_initialized$,
&$classname$::MergeImpl,
PROTOBUF_FIELD_OFFSET($classname$, $cached_size$),
true,
},
Expand All @@ -3745,17 +3750,7 @@ void MessageGenerator::GenerateClassData(io::Printer* p) {
}

void MessageGenerator::GenerateMergeFrom(io::Printer* p) {
Formatter format(p);

if (!HasDescriptorMethods(descriptor_->file(), options_)) {
// Generate CheckTypeAndMergeFrom().
format(
"void $classname$::CheckTypeAndMergeFrom(\n"
" const ::$proto_ns$::MessageLite& from) {\n"
" MergeFrom(*::_pbi::DownCast<const $classname$*>(\n"
" &from));\n"
"}\n");
}
// DO NOT SUBMIT: REMOVE
}

bool MessageGenerator::RequiresArena(GeneratorFunction function) const {
Expand All @@ -3772,20 +3767,12 @@ void MessageGenerator::GenerateClassSpecificMergeImpl(io::Printer* p) {
// Generate the class-specific MergeFrom, which avoids the ABSL_CHECK and
// cast.
Formatter format(p);
if (!HasDescriptorMethods(descriptor_->file(), options_)) {
// For messages that don't inherit from Message, just implement MergeFrom
// directly.
format(
"void $classname$::MergeFrom(const $classname$& from) {\n"
" $classname$* const _this = this;\n");
} else {
format(
"void $classname$::MergeImpl(::$proto_ns$::MessageLite& to_msg, const "
"::$proto_ns$::MessageLite& from_msg) {\n"
"$WeakDescriptorSelfPin$"
" auto* const _this = static_cast<$classname$*>(&to_msg);\n"
" auto& from = static_cast<const $classname$&>(from_msg);\n");
}
format(
"void $classname$::MergeImpl(::$proto_ns$::MessageLite& to_msg, const "
"::$proto_ns$::MessageLite& from_msg) {\n"
"$WeakDescriptorSelfPin$"
" auto* const _this = static_cast<$classname$*>(&to_msg);\n"
" auto& from = static_cast<const $classname$&>(from_msg);\n");
format.Indent();
if (RequiresArena(GeneratorFunction::kMergeFrom)) {
p->Emit(R"cc(
Expand Down
2 changes: 1 addition & 1 deletion src/google/protobuf/compiler/java/java_features.pb.cc

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

8 changes: 4 additions & 4 deletions src/google/protobuf/compiler/plugin.pb.cc

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

2 changes: 1 addition & 1 deletion src/google/protobuf/cpp_features.pb.cc

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

Loading
Loading