Incremental migration of message.cc to Emit

PiperOrigin-RevId: 532314829
pull/12751/head
Matt Kulukundis 2 years ago committed by Copybara-Service
parent f746518b73
commit 7b23c9bddf
  1. 66
      src/google/protobuf/compiler/cpp/message.cc

@ -3640,36 +3640,52 @@ void MessageGenerator::GenerateSerializeOneField(io::Printer* p,
const FieldDescriptor* field,
int cached_has_bits_index) {
auto v = p->WithVars(FieldVars(field, options_));
Formatter format(p);
if (!field->options().weak()) {
// For weakfields, PrintFieldComment is called during iteration.
PrintFieldComment(format, field);
}
auto emit_body = [&] {
field_generators_.get(field).GenerateSerializeWithCachedSizesToArray(p);
};
bool have_enclosing_if = false;
if (field->options().weak()) {
} else if (HasHasbit(field)) {
// Attempt to use the state of cached_has_bits, if possible.
int has_bit_index = HasBitIndex(field);
auto v = p->WithVars(HasBitVars(field));
if (cached_has_bits_index == has_bit_index / 32) {
format("if (cached_has_bits & $has_mask$) {\n");
} else {
format("if (($has_bits$[$has_array_index$] & $has_mask$) != 0) {\n");
}
have_enclosing_if = true;
} else if (field->is_optional() && !HasHasbit(field)) {
have_enclosing_if = EmitFieldNonDefaultCondition(p, "this->", field);
emit_body();
p->Emit("\n");
return;
}
if (have_enclosing_if) format.Indent();
field_generators_.get(field).GenerateSerializeWithCachedSizesToArray(p);
if (have_enclosing_if) {
format.Outdent();
format("}\n");
PrintFieldComment(Formatter{p}, field);
if (HasHasbit(field)) {
p->Emit(
{
{"body", emit_body},
{"cond",
[&] {
int has_bit_index = HasBitIndex(field);
auto v = p->WithVars(HasBitVars(field));
// Attempt to use the state of cached_has_bits, if possible.
if (cached_has_bits_index == has_bit_index / 32) {
p->Emit("cached_has_bits & $has_mask$");
} else {
p->Emit("($has_bits$[$has_array_index$] & $has_mask$) != 0");
}
}},
},
R"cc(
if ($cond$) {
$body$;
}
)cc");
} else if (field->is_optional()) {
bool have_enclosing_if = EmitFieldNonDefaultCondition(p, "this->", field);
if (have_enclosing_if) p->Indent();
emit_body();
if (have_enclosing_if) {
p->Outdent();
p->Emit(R"cc(
}
)cc");
}
} else {
emit_body();
}
format("\n");
p->Emit("\n");
}
void MessageGenerator::GenerateSerializeOneExtensionRange(io::Printer* p,

Loading…
Cancel
Save