Incremental migration of message.cc to Emit

PiperOrigin-RevId: 530408821
pull/12722/head
Matt Kulukundis 2 years ago committed by Copybara-Service
parent d10b04c239
commit cefad57e8f
  1. 85
      src/google/protobuf/compiler/cpp/message.cc

@ -524,9 +524,7 @@ absl::flat_hash_map<absl::string_view, std::string> HasbitVars(
int has_bit_index) { int has_bit_index) {
return { return {
{"has_array_index", absl::StrCat(has_bit_index / 32)}, {"has_array_index", absl::StrCat(has_bit_index / 32)},
{"has_mask", {"has_mask", absl::StrFormat("0x%08xu", 1u << (has_bit_index % 32))},
absl::StrCat(
"0x", absl::Hex(1u << (has_bit_index % 32), absl::kZeroPad8), "u")},
}; };
} }
@ -963,13 +961,14 @@ void MessageGenerator::GenerateFieldAccessorDeclarations(io::Printer* p) {
void MessageGenerator::GenerateSingularFieldHasBits( void MessageGenerator::GenerateSingularFieldHasBits(
const FieldDescriptor* field, io::Printer* p) { const FieldDescriptor* field, io::Printer* p) {
auto t = p->WithVars(MakeTrackerCalls(field, options_)); auto t = p->WithVars(MakeTrackerCalls(field, options_));
Formatter format(p);
if (field->options().weak()) { if (field->options().weak()) {
format( p->Emit(
"inline bool $classname$::has_$name$() const {\n" R"cc(
"$annotate_has$" inline bool $classname$::has_$name$() const {
" return $weak_field_map$.Has($number$);\n" $annotate_has$;
"}\n"); return $weak_field_map$.Has($number$);
}
)cc");
return; return;
} }
if (HasHasbit(field)) { if (HasHasbit(field)) {
@ -977,41 +976,49 @@ void MessageGenerator::GenerateSingularFieldHasBits(
ABSL_CHECK_NE(has_bit_index, kNoHasbit); ABSL_CHECK_NE(has_bit_index, kNoHasbit);
auto v = p->WithVars(HasbitVars(has_bit_index)); auto v = p->WithVars(HasbitVars(has_bit_index));
format( p->Emit(
"inline bool $classname$::has_$name$() const {\n" {Sub{"ASSUME",
"$annotate_has$" [&] {
" bool value = ($has_bits$[$has_array_index$] & $has_mask$) != 0;\n"); if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE &&
!IsLazy(field, options_, scc_analyzer_)) {
if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE && // We maintain the invariant that for a submessage x, has_x()
!IsLazy(field, options_, scc_analyzer_)) { // returning true implies that x_ is not null. By giving this
// We maintain the invariant that for a submessage x, has_x() returning // information to the compiler, we allow it to eliminate
// true implies that x_ is not null. By giving this information to the // unnecessary null checks later on.
// compiler, we allow it to eliminate unnecessary null checks later on. p->Emit(
format(" PROTOBUF_ASSUME(!value || $field$ != nullptr);\n"); R"cc(PROTOBUF_ASSUME(!value || $field$ != nullptr);)cc");
} }
}}
format( .WithSuffix(";")},
" return value;\n" R"cc(
"}\n"); inline bool $classname$::has_$name$() const {
$annotate_has$;
bool value = ($has_bits$[$has_array_index$] & $has_mask$) != 0;
$ASSUME$;
return value;
}
)cc");
} else if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { } else if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) {
// Message fields have a has_$name$() method. // Message fields have a has_$name$() method.
if (IsLazy(field, options_, scc_analyzer_)) { if (IsLazy(field, options_, scc_analyzer_)) {
format( p->Emit(R"cc(
"inline bool $classname$::_internal_has_$name$() const {\n" inline bool $classname$::_internal_has_$name$() const {
" return !$field$.IsCleared();\n" return !$field$.IsCleared();
"}\n"); }
)cc");
} else { } else {
format( p->Emit(R"cc(
"inline bool $classname$::_internal_has_$name$() const {\n" inline bool $classname$::_internal_has_$name$() const {
" return this != internal_default_instance() " return this != internal_default_instance() && $field$ != nullptr;
"&& $field$ != nullptr;\n" }
"}\n"); )cc");
} }
format( p->Emit(R"cc(
"inline bool $classname$::has_$name$() const {\n" inline bool $classname$::has_$name$() const {
"$annotate_has$" $annotate_has$;
" return _internal_has_$name$();\n" return _internal_has_$name$();
"}\n"); }
)cc");
} }
} }

Loading…
Cancel
Save