40 #include <unordered_map>
68 using internal::WireFormat;
69 using internal::WireFormatLite;
74 void PrintFieldComment(
const Formatter&
format,
const T*
field) {
79 options.elide_group_body =
true;
80 options.elide_oneof_body =
true;
82 format(
"// $1$\n", def.substr(0, def.find_first_of(
'\n')));
86 const std::vector<int>& has_bit_indices,
87 io::Printer* printer,
int* cached_has_bit_index) {
88 if (!
field->options().weak()) {
89 int has_bit_index = has_bit_indices[
field->index()];
90 if (*cached_has_bit_index != (has_bit_index / 32)) {
91 *cached_has_bit_index = (has_bit_index / 32);
92 format(
"cached_has_bits = _has_bits_[$1$];\n", *cached_has_bit_index);
96 format(
"if (cached_has_bits & 0x$1$u) {\n",
mask);
103 struct FieldOrderingByNumber {
106 return a->number() <
b->number();
118 std::sort(
fields.begin(),
fields.end(), FieldOrderingByNumber());
123 struct ExtensionRangeSorter {
124 bool operator()(
const Descriptor::ExtensionRange*
left,
125 const Descriptor::ExtensionRange* right)
const {
126 return left->start < right->start;
131 if (
field->is_repeated() ||
field->is_extension())
return false;
132 switch (
field->cpp_type()) {
166 bool EmitFieldNonDefaultCondition(io::Printer* printer,
169 Formatter
format(printer);
174 if (!
field->is_repeated() && !
field->containing_oneof()) {
176 format(
"if ($prefix$$name$().size() > 0) {\n");
179 format(
"if ($prefix$has_$name$()) {\n");
183 format(
"if (!($prefix$$name$() <= 0 && $prefix$$name$() >= 0)) {\n");
185 format(
"if ($prefix$$name$() != 0) {\n");
189 }
else if (
field->containing_oneof()) {
190 format(
"if (has_$name$()) {\n");
210 std::map<std::string, std::string>* variables) {
212 std::map<std::string, std::string>& vars = *variables;
216 switch (
val->cpp_type()) {
226 vars[
"key_wire_type"] =
228 vars[
"val_wire_type"] =
235 vars[
"default_enum_value"] =
"0";
258 static std::set<std::string> exclusions{
262 return exclusions.find(
name) == exclusions.end() ||
268 static std::set<std::string> exclusions{
272 return exclusions.find(
name) == exclusions.end() ||
278 static std::set<std::string> exclusions{
282 return exclusions.find(
name) == exclusions.end() ||
288 if (!
options.table_driven_parsing) {
299 const double table_sparseness = 0.5;
300 int max_field_number = 0;
302 if (max_field_number < field->
number()) {
303 max_field_number =
field->number();
318 if (max_field_number >= (2 << 14)) {
325 if (max_field_number * table_sparseness >
descriptor->field_count()) {
339 std::map<std::string, std::string>* variables) {
342 (*variables)[
"unknown_fields_type"] =
"::" + proto_ns +
"::UnknownFieldSet";
344 (*variables)[
"unknown_fields_type"] =
347 (*variables)[
"have_unknown_fields"] =
348 "_internal_metadata_.have_unknown_fields()";
349 (*variables)[
"unknown_fields"] =
"_internal_metadata_.unknown_fields()";
350 (*variables)[
"mutable_unknown_fields"] =
351 "_internal_metadata_.mutable_unknown_fields()";
355 if (!
field->is_map()) {
366 if (IsCrossFileMapField(
field)) {
373 bool IsRequired(
const std::vector<const FieldDescriptor*>&
v) {
374 return v.front()->is_required();
380 class MatchRepeatedAndHasByte {
382 MatchRepeatedAndHasByte(
const std::vector<int>* has_bit_indices,
383 bool has_field_presence)
392 return a->is_repeated() ==
b->is_repeated() &&
405 class MatchRepeatedAndHasByteAndRequired :
public MatchRepeatedAndHasByte {
407 MatchRepeatedAndHasByteAndRequired(
const std::vector<int>* has_bit_indices,
408 bool has_field_presence)
409 : MatchRepeatedAndHasByte(has_bit_indices, has_field_presence) {}
412 return MatchRepeatedAndHasByte::operator()(
a,
b) &&
413 a->is_required() ==
b->is_required();
419 class MatchRepeatedAndHasByteAndZeroInits :
public MatchRepeatedAndHasByte {
421 MatchRepeatedAndHasByteAndZeroInits(
const std::vector<int>* has_bit_indices,
422 bool has_field_presence)
423 : MatchRepeatedAndHasByte(has_bit_indices, has_field_presence) {}
426 return MatchRepeatedAndHasByte::operator()(
a,
b) &&
432 template <
typename Predicate>
433 std::vector<std::vector<const FieldDescriptor*>> CollectFields(
434 const std::vector<const FieldDescriptor*>&
fields,
435 const Predicate& equivalent) {
436 std::vector<std::vector<const FieldDescriptor*>> chunks;
442 std::vector<const FieldDescriptor*> chunk;
444 if (!equivalent(last_field,
field) && !chunk.empty()) {
445 chunks.push_back(chunk);
448 chunk.push_back(
field);
451 if (!chunk.empty()) {
452 chunks.push_back(chunk);
460 uint32 GenChunkMask(
const std::vector<const FieldDescriptor*>&
fields,
461 const std::vector<int>& has_bit_indices) {
463 int first_index_offset = has_bit_indices[
fields.front()->index()] / 32;
469 chunk_mask |=
static_cast<uint32>(1) << (
index % 32);
487 class ColdChunkSkipper {
491 const std::vector<std::vector<const FieldDescriptor*>>& chunks,
492 const std::vector<int>& has_bit_indices,
const double cold_threshold,
493 bool has_field_presence)
505 void OnStartChunk(
int chunk,
int cached_has_bit_index,
507 bool OnEndChunk(
int chunk, io::Printer* printer);
510 bool IsColdChunk(
int chunk);
512 int HasbitWord(
int chunk,
int offset) {
516 const std::vector<std::vector<const FieldDescriptor*>>&
chunks_;
526 const double kColdRatio = 0.005;
528 bool ColdChunkSkipper::IsColdChunk(
int chunk) {
535 void ColdChunkSkipper::OnStartChunk(
int chunk,
int cached_has_bit_index,
537 io::Printer* printer) {
544 }
else if (!IsColdChunk(chunk)) {
562 format(
"if (PROTOBUF_PREDICT_FALSE(");
563 int first_word = HasbitWord(chunk, 0);
566 int this_word = HasbitWord(chunk, 0);
568 for (; chunk <
limit_chunk_ && HasbitWord(chunk, 0) == this_word; chunk++) {
573 mask |= 1 << (hasbit_index % 32);
577 if (this_word != first_word) {
581 if (this_word == cached_has_bit_index) {
582 format(
"(cached_has_bits & 0x$mask$u) != 0");
584 format(
"($1$_has_bits_[$2$] & 0x$mask$u) != 0", from, this_word);
591 bool ColdChunkSkipper::OnEndChunk(
int chunk, io::Printer* printer) {
607 const std::map<std::string, std::string>& vars,
int index_in_file_messages,
610 index_in_file_messages_(index_in_file_messages),
614 max_has_bit_index_(0),
616 scc_analyzer_(scc_analyzer),
635 }
else if (!
field->containing_oneof()) {
647 if (
field->is_repeated()) {
670 if (sizeof_has_bits == 0) {
678 return sizeof_has_bits;
682 std::vector<std::unique_ptr<EnumGenerator>>* enum_generators,
683 std::vector<std::unique_ptr<ExtensionGenerator>>* extension_generators) {
685 enum_generators->emplace_back(
690 extension_generators->emplace_back(
705 std::vector<const FieldDescriptor*> ordered_fields;
711 if (
field->containing_oneof() ==
NULL && !
field->options().weak()) {
714 ordered_fields.push_back(
field);
717 if (!ordered_fields.empty()) {
719 for (
auto field : ordered_fields) {
722 std::map<std::string, std::string> vars;
729 for (
auto field : ordered_fields) {
734 std::map<std::string, std::string> vars;
738 if (
field->is_repeated()) {
739 format(
"$deprecated_attr$int ${1$$name$_size$}$() const;\n",
field);
740 }
else if (HasHasMethod(
field)) {
741 format(
"$deprecated_attr$bool ${1$has_$name$$}$() const;\n",
field);
742 }
else if (HasPrivateHasMethod(
field)) {
745 "bool ${1$has_$name$$}$() const;\n"
750 format(
"$deprecated_attr$void ${1$clear_$name$$}$();\n",
field);
761 format(
"$GOOGLE_PROTOBUF$_EXTENSION_ACCESSORS($classname$)\n");
768 format(
"GOOGLE_PROTOBUF_EXTENSION_MESSAGE_SET_ACCESSORS($classname$)\n");
774 format.Set(
"oneof_name", oneof->name());
777 "void ${1$clear_$oneof_name$$}$();\n"
778 "$camel_oneof_name$Case $oneof_name$_case() const;\n",
785 if (
field->options().weak()) {
787 "inline bool $classname$::has_$name$() const {\n"
788 " return _weak_field_map_.Has($number$);\n"
798 format.Set(
"has_array_index", has_bit_index / 32);
802 "inline bool $classname$::has_$name$() const {\n"
803 " return (_has_bits_[$has_array_index$] & 0x$has_mask$u) != 0;\n"
810 "inline bool $classname$::has_$name$() const {\n"
811 " return !$name$_.IsCleared();\n"
815 "inline bool $classname$::has_$name$() const {\n"
816 " return this != internal_default_instance() "
817 "&& $name$_ != nullptr;\n"
827 format.Set(
"oneof_name", oneof->name());
828 format.Set(
"oneof_index", oneof->index());
831 "inline bool $classname$::has_$oneof_name$() const {\n"
832 " return $oneof_name$_case() != $cap_oneof_name$_NOT_SET;\n"
834 "inline void $classname$::clear_has_$oneof_name$() {\n"
835 " _oneof_case_[$oneof_index$] = $cap_oneof_name$_NOT_SET;\n"
849 "inline bool $classname$::has_$name$() const {\n"
850 " return $oneof_name$_case() == k$field_name$;\n"
852 "inline void $classname$::set_has_$name$() {\n"
853 " _oneof_case_[$oneof_index$] = k$field_name$;\n"
863 format(
"void $classname$::clear_$name$() {\n");
867 if (
field->containing_oneof()) {
870 format(
"if (has_$name$()) {\n");
873 format(
"clear_has_$oneof_name$();\n");
879 if (!
field->is_repeated() && !
field->options().weak()) {
883 format.Set(
"has_array_index", has_bit_index / 32);
886 format(
"_has_bits_[$has_array_index$] &= ~0x$has_mask$u;\n");
897 format(
"// $classname$\n\n");
902 std::map<std::string, std::string> vars;
909 if (
field->is_repeated()) {
911 "inline int $classname$::$name$_size() const {\n"
912 " return $name$_.size();\n"
914 }
else if (
field->containing_oneof()) {
916 format.Set(
"oneof_name",
field->containing_oneof()->name());
925 if (!IsCrossFileMaybeMap(
field)) {
945 std::map<std::string, std::string> vars;
951 "class $classname$ : public "
952 "::$proto_ns$::internal::MapEntry$lite$<$classname$, \n"
953 " $key_cpp$, $val_cpp$,\n"
954 " ::$proto_ns$::internal::WireFormatLite::$key_wire_type$,\n"
955 " ::$proto_ns$::internal::WireFormatLite::$val_wire_type$,\n"
956 " $default_enum_value$ > {\n"
958 " typedef ::$proto_ns$::internal::MapEntry$lite$<$classname$, \n"
959 " $key_cpp$, $val_cpp$,\n"
960 " ::$proto_ns$::internal::WireFormatLite::$key_wire_type$,\n"
961 " ::$proto_ns$::internal::WireFormatLite::$val_wire_type$,\n"
962 " $default_enum_value$ > SuperType;\n"
964 " $classname$(::$proto_ns$::Arena* arena);\n"
965 " void MergeFrom(const $classname$& other);\n"
966 " static const $classname$* internal_default_instance() { return "
967 "reinterpret_cast<const "
968 "$classname$*>(&_$classname$_default_instance_); }\n");
972 if (suffix ==
"UTF8") {
974 " static bool ValidateKey(std::string* s) {\n"
975 " return ::$proto_ns$::internal::WireFormatLite::"
976 "VerifyUtf8String(s->data(), s->size(), "
977 "::$proto_ns$::internal::WireFormatLite::PARSE, \"$1$\");\n"
983 " static bool ValidateKey(std::string* s) {\n"
985 " ::$proto_ns$::internal::WireFormatLite::VerifyUtf8String(\n"
986 " s->data(), s->size(), ::$proto_ns$::internal::"
987 "WireFormatLite::PARSE, \"$1$\");\n"
994 format(
" static bool ValidateKey(void*) { return true; }\n");
998 if (suffix ==
"UTF8") {
1000 " static bool ValidateValue(std::string* s) {\n"
1001 " return ::$proto_ns$::internal::WireFormatLite::"
1002 "VerifyUtf8String(s->data(), s->size(), "
1003 "::$proto_ns$::internal::WireFormatLite::PARSE, \"$1$\");\n"
1009 " static bool ValidateValue(std::string* s) {\n"
1011 " ::$proto_ns$::internal::WireFormatLite::VerifyUtf8String(\n"
1012 " s->data(), s->size(), ::$proto_ns$::internal::"
1013 "WireFormatLite::PARSE, \"$1$\");\n"
1020 format(
" static bool ValidateValue(void*) { return true; }\n");
1024 " void MergeFrom(const ::$proto_ns$::Message& other) final;\n"
1025 " ::$proto_ns$::Metadata GetMetadata() const final;\n"
1027 " static ::$proto_ns$::Metadata GetMetadataStatic() {\n"
1028 " ::$proto_ns$::internal::AssignDescriptors(&::$desc_table$);\n"
1029 " return ::$desc_table$.file_level_metadata[$1$];\n"
1042 "class $dllexport_decl $${1$$classname$$}$$ class_final$ :\n"
1043 " public $superclass$ /* @@protoc_insertion_point("
1044 "class_definition:$full_name$) */ {\n",
1051 "virtual ~$classname$();\n"
1053 "$classname$(const $classname$& from);\n"
1054 "$classname$($classname$&& from) noexcept\n"
1055 " : $classname$() {\n"
1056 " *this = ::std::move(from);\n"
1059 "inline $classname$& operator=(const $classname$& from) {\n"
1060 " CopyFrom(from);\n"
1063 "inline $classname$& operator=($classname$&& from) noexcept {\n"
1064 " if (GetArenaNoVirtual() == from.GetArenaNoVirtual()) {\n"
1065 " if (this != &from) InternalSwap(&from);\n"
1067 " CopyFrom(from);\n"
1076 "const void* InternalGetTable() const;\n"
1081 std::map<std::string, std::string> vars;
1086 "inline const $unknown_fields_type$& unknown_fields() const {\n"
1087 " return $unknown_fields$;\n"
1089 "inline $unknown_fields_type$* mutable_unknown_fields() {\n"
1090 " return $mutable_unknown_fields$;\n"
1102 "inline ::$proto_ns$::Arena* GetArena() const final {\n"
1103 " return GetArenaNoVirtual();\n"
1105 "inline void* GetMaybeArenaPointer() const final {\n"
1106 " return MaybeArenaPtr();\n"
1114 "static const ::$proto_ns$::Descriptor* descriptor() {\n"
1115 " return GetDescriptor();\n"
1129 "static const ::$proto_ns$::Descriptor* GetDescriptor() {\n"
1130 " return GetMetadataStatic().descriptor;\n"
1132 "static const ::$proto_ns$::Reflection* GetReflection() {\n"
1133 " return GetMetadataStatic().reflection;\n"
1138 "static const $classname$& default_instance();\n"
1149 format(
"k$1$ = $2$,\n", oneof_enum_case_field_name,
1161 "static void InitAsDefaultInstance(); // FOR INTERNAL USE ONLY\n"
1162 "static inline const $classname$* internal_default_instance() {\n"
1163 " return reinterpret_cast<const $classname$*>(\n"
1164 " &_$classname$_default_instance_);\n"
1166 "static constexpr int kIndexInFileMessages =\n"
1173 "// implements Any -----------------------------------------------\n"
1177 "void PackFrom(const ::$proto_ns$::Message& message);\n"
1178 "void PackFrom(const ::$proto_ns$::Message& message,\n"
1179 " const std::string& type_url_prefix);\n"
1180 "bool UnpackTo(::$proto_ns$::Message* message) const;\n"
1181 "static bool GetAnyFieldDescriptors(\n"
1182 " const ::$proto_ns$::Message& message,\n"
1183 " const ::$proto_ns$::FieldDescriptor** type_url_field,\n"
1184 " const ::$proto_ns$::FieldDescriptor** value_field);\n");
1187 "template <typename T>\n"
1188 "void PackFrom(const T& message) {\n"
1189 " _any_metadata_.PackFrom(message);\n"
1191 "template <typename T>\n"
1192 "void PackFrom(const T& message,\n"
1193 " const std::string& type_url_prefix) {\n"
1194 " _any_metadata_.PackFrom(message, type_url_prefix);"
1196 "template <typename T>\n"
1197 "bool UnpackTo(T* message) const {\n"
1198 " return _any_metadata_.UnpackTo(message);\n"
1202 "template<typename T> bool Is() const {\n"
1203 " return _any_metadata_.Is<T>();\n"
1205 "static bool ParseAnyTypeUrl(const string& type_url,\n"
1206 " std::string* full_type_name);\n");
1213 "friend void swap($classname$& a, $classname$& b) {\n"
1219 "inline void Swap($classname$* other) {\n"
1220 " if (other == this) return;\n"
1221 " if (GetArenaNoVirtual() == other->GetArenaNoVirtual()) {\n"
1222 " InternalSwap(other);\n"
1224 " ::PROTOBUF_NAMESPACE_ID::internal::GenericSwap(this, other);\n"
1227 "void UnsafeArenaSwap($classname$* other) {\n"
1228 " if (other == this) return;\n"
1229 " $DCHK$(GetArenaNoVirtual() == other->GetArenaNoVirtual());\n"
1230 " InternalSwap(other);\n"
1234 "inline void Swap($classname$* other) {\n"
1235 " if (other == this) return;\n"
1236 " InternalSwap(other);\n"
1242 "// implements Message ----------------------------------------------\n"
1244 "inline $classname$* New() const$ new_final$ {\n"
1245 " return CreateMaybeMessage<$classname$>(nullptr);\n"
1248 "$classname$* New(::$proto_ns$::Arena* arena) const$ new_final$ {\n"
1249 " return CreateMaybeMessage<$classname$>(arena);\n"
1261 "void CopyFrom(const ::$proto_ns$::Message& from) final;\n"
1262 "void MergeFrom(const ::$proto_ns$::Message& from) final;\n");
1265 "void CheckTypeAndMergeFrom(const ::$proto_ns$::MessageLite& from)\n"
1269 format.Set(
"clear_final",
1272 "is_initialized_final",
1276 "void CopyFrom(const $classname$& from);\n"
1277 "void MergeFrom(const $classname$& from);\n"
1278 "PROTOBUF_ATTRIBUTE_REINITIALIZES void Clear()$ clear_final$;\n"
1279 "bool IsInitialized() const$ is_initialized_final$;\n"
1281 "size_t ByteSizeLong() const final;\n"
1282 "#if $GOOGLE_PROTOBUF$_ENABLE_EXPERIMENTAL_PARSER\n"
1283 "const char* _InternalParse(const char* ptr, "
1284 "::$proto_ns$::internal::ParseContext* ctx) final;\n"
1286 "bool MergePartialFromCodedStream(\n"
1287 " ::$proto_ns$::io::CodedInputStream* input) final;\n"
1288 "#endif // $GOOGLE_PROTOBUF$_ENABLE_EXPERIMENTAL_PARSER\n");
1293 "void SerializeWithCachedSizes(\n"
1294 " ::$proto_ns$::io::CodedOutputStream* output) const final;\n");
1299 format(
"void DiscardUnknownFields()$ full_final$;\n");
1303 "$uint8$* InternalSerializeWithCachedSizesToArray(\n"
1304 " $uint8$* target) const final;\n");
1309 "int GetCachedSize() const final { return _cached_size_.Get(); }"
1311 "inline void SharedCtor();\n"
1312 "inline void SharedDtor();\n"
1313 "void SetCachedSize(int size) const$ full_final$;\n"
1314 "void InternalSwap($classname$* other);\n");
1318 "friend class ::$proto_ns$::internal::AnyMetadata;\n"
1319 "static $1$ FullMessageName() {\n"
1320 " return \"$full_name$\";\n"
1331 "explicit $classname$(::$proto_ns$::Arena* arena);\n"
1333 "static void ArenaDtor(void* object);\n"
1334 "inline void RegisterArenaDtor(::$proto_ns$::Arena* arena);\n");
1340 "inline ::$proto_ns$::Arena* GetArenaNoVirtual() const {\n"
1341 " return _internal_metadata_.arena();\n"
1343 "inline void* MaybeArenaPtr() const {\n"
1344 " return _internal_metadata_.raw_arena_ptr();\n"
1349 "inline ::$proto_ns$::Arena* GetArenaNoVirtual() const {\n"
1350 " return nullptr;\n"
1352 "inline void* MaybeArenaPtr() const {\n"
1353 " return nullptr;\n"
1363 "::$proto_ns$::Metadata GetMetadata() const final;\n"
1365 "static ::$proto_ns$::Metadata GetMetadataStatic() {\n"
1366 " ::$proto_ns$::internal::AssignDescriptors(&::$desc_table$);\n"
1367 " return ::$desc_table$.file_level_metadata[kIndexInFileMessages];\n"
1374 "std::string GetTypeName() const final;\n"
1379 "// nested types ----------------------------------------------------\n"
1388 format(
"typedef ${1$$nested_full_name$$}$ ${1$$nested_name$$}$;\n",
1405 "// accessors -------------------------------------------------------\n"
1417 format(
"// @@protoc_insertion_point(class_scope:$full_name$)\n");
1424 format(
"class _Internal;\n");
1429 if (!
field->is_repeated() && !
field->options().weak() &&
1430 field->containing_oneof()) {
1439 "inline bool has_$1$() const;\n"
1440 "inline void clear_has_$1$();\n\n",
1448 "// helper for ByteSizeLong()\n"
1449 "size_t RequiredFieldsByteSizeFallback() const;\n\n");
1455 bool need_to_emit_cached_size =
true;
1457 "mutable ::$proto_ns$::internal::CachedSize _cached_size_;\n";
1461 sizeof_has_bits == 0
1463 :
StrCat(
"::$proto_ns$::internal::HasBits<",
1464 sizeof_has_bits / 4,
"> _has_bits_;\n");
1476 "::$proto_ns$::internal::ExtensionSet _extensions_;\n"
1482 "::$proto_ns$::internal::InternalMetadataWithArena "
1483 "_internal_metadata_;\n");
1486 "::$proto_ns$::internal::InternalMetadataWithArenaLite "
1487 "_internal_metadata_;\n");
1492 "template <typename T> friend class "
1493 "::$proto_ns$::Arena::InternalHelper;\n"
1494 "typedef void InternalArenaConstructable_;\n"
1495 "typedef void DestructorSkippable_;\n");
1503 format(has_bits_decl.c_str());
1504 format(cached_size_decl.c_str());
1505 need_to_emit_cached_size =
false;
1521 "union $1$Union {\n"
1531 format(
"} $1$_;\n", oneof->name());
1539 if (need_to_emit_cached_size) {
1540 format(cached_size_decl.c_str());
1541 need_to_emit_cached_size =
false;
1547 "$uint32$ _oneof_case_[$1$];\n"
1553 format(
"::$proto_ns$::internal::WeakFieldMap _weak_field_map_;\n");
1557 format(
"::$proto_ns$::internal::AnyMetadata _any_metadata_;\n");
1562 format(
"friend struct ::$tablename$;\n");
1577 format.Set(
"oneof_name", oneof->name());
1578 format.Set(
"oneof_index", oneof->index());
1580 "inline $classname$::$camel_oneof_name$Case $classname$::"
1581 "${1$$oneof_name$_case$}$() const {\n"
1582 " return $classname$::$camel_oneof_name$Case("
1583 "_oneof_case_[$oneof_index$]);\n"
1605 if (
field->options().weak()) {
1613 size_t aux_offset) {
1617 format(
"{ nullptr, nullptr, 0, -1, -1, -1, -1, nullptr, false },\n");
1621 int max_field_number = 0;
1623 if (max_field_number < field->
number()) {
1624 max_field_number =
field->number();
1632 "$tablename$::entries + $1$,\n"
1633 "$tablename$::aux + $2$,\n"
1635 offset, aux_offset, max_field_number);
1641 format(
"PROTOBUF_FIELD_OFFSET($classtype$, _has_bits_),\n");
1645 format(
"PROTOBUF_FIELD_OFFSET($classtype$, _oneof_case_),\n");
1647 format(
"-1, // no _oneof_case_\n");
1651 format(
"PROTOBUF_FIELD_OFFSET($classtype$, _extensions_),\n");
1653 format(
"-1, // no _extensions_\n");
1659 "PROTOBUF_FIELD_OFFSET($classtype$, _internal_metadata_),\n"
1660 "&$package_ns$::_$classname$_default_instance_,\n");
1681 format(
"{ $1$, $2$, sizeof($classtype$)},\n",
offset, has_offset);
1704 if (
field->containing_oneof()) {
1708 if (
field->is_packed()) {
1711 }
else if (
field->is_repeated()) {
1715 field->containing_oneof() ==
NULL && !is_a_map) {
1725 std::vector<const Descriptor*> flatten =
1727 return std::find(flatten.begin(), flatten.end(),
descriptor) -
1741 for (
int i = 0;
i < 2;
i++) {
1748 std::map<std::string, std::string> vars;
1751 vars[
"tag"] =
StrCat(tag);
1754 vars[
"ptr"] =
"nullptr";
1759 "::serialization_table + " +
1760 StrCat(FindMessageIndexInFile(
field->message_type()));
1765 "{PROTOBUF_FIELD_OFFSET("
1766 "::$proto_ns$::internal::MapEntryHelper<$classtype$::"
1767 "SuperType>, $field_name$_), $tag$,"
1768 "PROTOBUF_FIELD_OFFSET("
1769 "::$proto_ns$::internal::MapEntryHelper<$classtype$::"
1770 "SuperType>, _has_bits_) * 8 + $hasbit$, $type$, "
1776 "{PROTOBUF_FIELD_OFFSET($classtype$, _cached_size_),"
1777 " 0, 0, 0, nullptr},\n");
1778 std::vector<const Descriptor::ExtensionRange*> sorted_extensions;
1783 std::sort(sorted_extensions.begin(), sorted_extensions.end(),
1784 ExtensionRangeSorter());
1785 for (
int i = 0, extension_idx = 0; ;
i++) {
1786 for (; extension_idx < sorted_extensions.size() &&
1787 (
i == sorted.size() ||
1788 sorted_extensions[extension_idx]->start < sorted[
i]->number());
1790 const Descriptor::ExtensionRange*
range =
1791 sorted_extensions[extension_idx];
1793 "{PROTOBUF_FIELD_OFFSET($classtype$, _extensions_), "
1794 "$1$, $2$, ::$proto_ns$::internal::FieldMetadata::kSpecial, "
1795 "reinterpret_cast<const "
1796 "void*>(::$proto_ns$::internal::ExtensionSerializer)},\n",
1799 if (
i == sorted.size())
break;
1804 if (
field->is_packed()) {
1810 if (
field->containing_oneof()) {
1811 classfieldname =
field->containing_oneof()->name();
1813 format.Set(
"field_name", classfieldname);
1818 "{PROTOBUF_FIELD_OFFSET($classtype$, $field_name$_), $1$, $2$, "
1819 "::$proto_ns$::internal::FieldMetadata::kSpecial, "
1820 "reinterpret_cast<const void*>(static_cast< "
1821 "::$proto_ns$::internal::SpecialSerializer>("
1822 "::$proto_ns$::internal::MapFieldSerializer< "
1823 "::$proto_ns$::internal::MapEntryToMapField<"
1824 "$3$>::MapFieldType, "
1825 "$tablename$::serialization_table>))},\n",
1826 tag, FindMessageIndexInFile(
field->message_type()),
1829 }
else if (!
field->message_type()->options().message_set_wire_format()) {
1834 "::serialization_table + " +
1835 StrCat(FindMessageIndexInFile(
field->message_type()));
1844 ptr =
"reinterpret_cast<const void*>(::" +
variables_[
"proto_ns"] +
1845 "::internal::LazyFieldSerializer";
1846 if (
field->containing_oneof()) {
1850 ptr +=
"NoPresence";
1855 if (
field->options().weak()) {
1858 "{PROTOBUF_FIELD_OFFSET("
1859 "$classtype$, _weak_field_map_), $1$, $1$, "
1860 "::$proto_ns$::internal::FieldMetadata::kSpecial, "
1861 "reinterpret_cast<const "
1862 "void*>(::$proto_ns$::internal::WeakFieldSerializer)},\n",
1864 }
else if (
field->containing_oneof()) {
1865 format.Set(
"oneofoffset",
1866 sizeof(
uint32) *
field->containing_oneof()->index());
1868 "{PROTOBUF_FIELD_OFFSET($classtype$, $field_name$_), $1$,"
1869 " PROTOBUF_FIELD_OFFSET($classtype$, _oneof_case_) + "
1870 "$oneofoffset$, $2$, $3$},\n",
1876 "{PROTOBUF_FIELD_OFFSET($classtype$, $field_name$_), "
1877 "$1$, PROTOBUF_FIELD_OFFSET($classtype$, _has_bits_) * 8 + "
1878 "$hasbitsoffset$, $2$, $3$},\n",
1882 "{PROTOBUF_FIELD_OFFSET($classtype$, $field_name$_), "
1883 "$1$, ~0u, $2$, $3$},\n",
1887 int num_field_metadata = 1 + sorted.size() + sorted_extensions.size();
1888 num_field_metadata++;
1890 ?
"UnknownFieldSetSerializer"
1891 :
"UnknownFieldSerializerLite";
1893 "{PROTOBUF_FIELD_OFFSET($classtype$, _internal_metadata_), 0, ~0u, "
1894 "::$proto_ns$::internal::FieldMetadata::kSpecial, reinterpret_cast<const "
1895 "void*>(::$proto_ns$::internal::$1$)},\n",
1897 return num_field_metadata;
1925 if (
field->containing_oneof() ||
field->options().weak()) {
1929 "_" +
classname_ +
"_default_instance_._instance.get_mutable()->";
1935 "$package_ns$::$name$_ = reinterpret_cast<const "
1936 "::$proto_ns$::Message*>(&$1$);\n"
1937 "if ($package_ns$::$name$_ == nullptr) {\n"
1938 " $package_ns$::$name$_ = "
1939 "::$proto_ns$::Empty::internal_default_instance();\n"
1946 "$package_ns$::$name$_ = const_cast< $1$*>(\n"
1947 " $1$::internal_default_instance());\n",
1949 }
else if (
field->containing_oneof() &&
1960 "$classname$::$classname$() {}\n"
1961 "$classname$::$classname$(::$proto_ns$::Arena* arena)\n"
1962 " : SuperType(arena) {}\n"
1963 "void $classname$::MergeFrom(const $classname$& other) {\n"
1964 " MergeFromInternal(other);\n"
1968 "::$proto_ns$::Metadata $classname$::GetMetadata() const {\n"
1969 " return GetMetadataStatic();\n"
1972 "void $classname$::MergeFrom(\n"
1973 " const ::$proto_ns$::Message& other) {\n"
1974 " ::$proto_ns$::Message::MergeFrom(other);\n"
1983 format(
"void $classname$::InitAsDefaultInstance() {\n");
1992 "void $classname$::PackFrom(const ::$proto_ns$::Message& message) {\n"
1993 " _any_metadata_.PackFrom(message);\n"
1996 "void $classname$::PackFrom(const ::$proto_ns$::Message& message,\n"
1997 " const std::string& type_url_prefix) {\n"
1998 " _any_metadata_.PackFrom(message, type_url_prefix);\n"
2001 "bool $classname$::UnpackTo(::$proto_ns$::Message* message) const {\n"
2002 " return _any_metadata_.UnpackTo(message);\n"
2004 "bool $classname$::GetAnyFieldDescriptors(\n"
2005 " const ::$proto_ns$::Message& message,\n"
2006 " const ::$proto_ns$::FieldDescriptor** type_url_field,\n"
2007 " const ::$proto_ns$::FieldDescriptor** value_field) {\n"
2008 " return ::$proto_ns$::internal::GetAnyFieldDescriptors(\n"
2009 " message, type_url_field, value_field);\n"
2013 "bool $classname$::ParseAnyTypeUrl(const string& type_url,\n"
2014 " std::string* full_type_name) {\n"
2015 " return ::$proto_ns$::internal::ParseAnyTypeUrl(type_url,\n"
2016 " full_type_name);\n"
2022 "class $classname$::_Internal {\n"
2027 "using HasBits = decltype(std::declval<$classname$>()._has_bits_);\n");
2032 !
field->options().weak() && !
field->containing_oneof()) {
2036 "static void set_has_$1$(HasBits* has_bits) {\n"
2037 " (*has_bits)[$2$] |= $3$u;\n"
2039 FieldName(
field), has_bit_index / 32, (1u << (has_bit_index % 32)));
2051 if (IsCrossFileMaybeMap(
field)) {
2053 std::map<std::string, std::string> vars;
2055 if (
field->containing_oneof()) {
2104 "const void* $classname$::InternalGetTable() const {\n"
2105 " return ::$tablename$::serialization_table + $1$;\n"
2112 "::$proto_ns$::Metadata $classname$::GetMetadata() const {\n"
2113 " return GetMetadataStatic();\n"
2118 "std::string $classname$::GetTypeName() const {\n"
2119 " return \"$full_name$\";\n"
2135 format(
"{0, 0, 0, ::$proto_ns$::internal::kInvalidMask, 0, 0},\n");
2136 int last_field_number = 1;
2138 std::vector<const FieldDescriptor*> ordered_fields =
2141 for (
auto field : ordered_fields) {
2145 for (; last_field_number <
field->number(); last_field_number++) {
2147 "{ 0, 0, ::$proto_ns$::internal::kInvalidMask,\n"
2148 " ::$proto_ns$::internal::kInvalidMask, 0, 0 },\n");
2150 last_field_number++;
2152 unsigned char normal_wiretype, packed_wiretype, processing_type;
2155 if (
field->is_packable()) {
2161 processing_type =
static_cast<unsigned>(
field->type());
2195 processing_type |=
static_cast<unsigned>(
2197 processing_type |=
static_cast<unsigned>(
2200 if (
field->is_map()) {
2204 const unsigned char tag_size =
2207 std::map<std::string, std::string> vars;
2208 if (
field->containing_oneof() !=
NULL) {
2209 vars[
"name"] =
field->containing_oneof()->name();
2210 vars[
"presence"] =
StrCat(
field->containing_oneof()->index());
2215 vars[
"nwtype"] =
StrCat(normal_wiretype);
2216 vars[
"pwtype"] =
StrCat(packed_wiretype);
2217 vars[
"ptype"] =
StrCat(processing_type);
2218 vars[
"tag_size"] =
StrCat(tag_size);
2224 " PROTOBUF_FIELD_OFFSET($classtype$, $name$_),\n"
2225 " static_cast<$uint32$>($presence$),\n"
2226 " $nwtype$, $pwtype$, $ptype$, $tag_size$\n"
2230 return last_field_number;
2240 std::vector<const FieldDescriptor*> ordered_fields =
2243 format(
"::$proto_ns$::internal::AuxillaryParseTableField(),\n");
2244 int last_field_number = 1;
2245 for (
auto field : ordered_fields) {
2249 for (; last_field_number <
field->number(); last_field_number++) {
2250 format(
"::$proto_ns$::internal::AuxillaryParseTableField(),\n");
2253 std::map<std::string, std::string> vars;
2257 switch (
field->cpp_type()) {
2261 "{::$proto_ns$::internal::AuxillaryParseTableField::enum_aux{"
2265 "{::$proto_ns$::internal::AuxillaryParseTableField::enum_aux{"
2269 last_field_number++;
2272 if (
field->is_map()) {
2274 "{::$proto_ns$::internal::AuxillaryParseTableField::map_"
2275 "aux{&::$proto_ns$::internal::ParseMap<$1$>}},\n",
2277 last_field_number++;
2285 "{::$proto_ns$::internal::AuxillaryParseTableField::message_aux{\n"
2286 " &$default_instance$}},\n");
2287 last_field_number++;
2294 default_val =
field->default_value_string().empty()
2296 "::internal::fixed_address_empty_string"
2308 "{::$proto_ns$::internal::AuxillaryParseTableField::string_aux{\n"
2312 default_val,
field->full_name());
2313 last_field_number++;
2321 return last_field_number;
2329 format(
"PROTOBUF_FIELD_OFFSET($classtype$, _has_bits_),\n");
2331 format(
"~0u, // no _has_bits_\n");
2333 format(
"PROTOBUF_FIELD_OFFSET($classtype$, _internal_metadata_),\n");
2335 format(
"PROTOBUF_FIELD_OFFSET($classtype$, _extensions_),\n");
2337 format(
"~0u, // no _extensions_\n");
2340 format(
"PROTOBUF_FIELD_OFFSET($classtype$, _oneof_case_[0]),\n");
2342 format(
"~0u, // no _oneof_case_\n");
2345 format(
"PROTOBUF_FIELD_OFFSET($classtype$, _weak_field_map_),\n");
2347 format(
"~0u, // no _weak_field_map_\n");
2349 const int kNumGenericOffsets = 5;
2352 size_t entries = offsets;
2354 if (
field->containing_oneof() ||
field->options().weak()) {
2355 format(
"offsetof($classtype$DefaultTypeInternal, $1$_)",
2370 format(
"PROTOBUF_FIELD_OFFSET($classtype$, $1$_),\n", oneof->name());
2387 return std::make_pair(entries, offsets);
2393 format(
"void $classname$::SharedCtor() {\n");
2396 format(
" ::$proto_ns$::internal::InitSCC(&$scc_info$.base);\n");
2405 format(
"clear_has_$1$();\n", oneof->name());
2415 format(
"void $classname$::SharedDtor() {\n");
2418 format(
"$DCHK$(GetArenaNoVirtual() == nullptr);\n");
2429 "if (has_$1$()) {\n"
2436 format(
"_weak_field_map_.ClearAll();\n");
2449 format(
"void $classname$::ArenaDtor(void* object) {\n");
2457 "$classname$* _this = reinterpret_cast< $classname$* >(object);\n"
2461 bool need_registration =
false;
2465 need_registration =
true;
2476 need_registration =
true;
2483 format(
"_this->_weak_field_map_.ClearAll();\n");
2484 need_registration =
true;
2490 if (need_registration) {
2492 "inline void $classname$::RegisterArenaDtor(::$proto_ns$::Arena* "
2494 " if (arena != nullptr) {\n"
2495 " arena->OwnCustomDestructor(this, &$classname$::ArenaDtor);\n"
2500 "void $classname$::RegisterArenaDtor(::$proto_ns$::Arena*) {\n"
2506 std::vector<bool> processed,
2507 bool copy_constructor)
const {
2513 typedef std::unordered_map<const FieldDescriptor*, size_t> RunMap;
2517 if ((copy_constructor && IsPOD(
field)) ||
2518 (!copy_constructor && CanConstructByZeroing(
field,
options_))) {
2519 if (last_start ==
NULL) {
2530 if (copy_constructor) {
2532 "::memcpy(&$first$_, &from.$first$_,\n"
2533 " static_cast<size_t>(reinterpret_cast<char*>(&$last$_) -\n"
2534 " reinterpret_cast<char*>(&$first$_)) + sizeof($last$_));\n";
2537 "::memset(&$first$_, 0, static_cast<size_t>(\n"
2538 " reinterpret_cast<char*>(&$last$_) -\n"
2539 " reinterpret_cast<char*>(&$first$_)) + sizeof($last$_));\n";
2548 RunMap::const_iterator
it = runs.find(
field);
2552 if (
it != runs.end() &&
it->second > 1) {
2554 const size_t run_length =
it->second;
2559 format.Set(
"first", first_field_name);
2560 format.Set(
"last", last_field_name);
2562 format(pod_template.c_str());
2564 i += run_length - 1;
2567 if (copy_constructor) {
2581 std::string initializer_with_arena = superclass +
"()";
2584 initializer_with_arena +=
",\n _extensions_(arena)";
2587 initializer_with_arena +=
",\n _internal_metadata_(arena)";
2591 bool has_arena_constructor =
field->is_repeated();
2592 if (
field->containing_oneof() ==
NULL &&
2594 has_arena_constructor =
true;
2596 if (has_arena_constructor) {
2597 initializer_with_arena +=
2603 initializer_with_arena +=
",\n _any_metadata_(&type_url_, &value_)";
2606 initializer_with_arena +=
", _weak_field_map_(arena)";
2610 superclass +
"(), _internal_metadata_(nullptr)";
2612 initializer_null +=
", _any_metadata_(&type_url_, &value_)";
2615 initializer_null +=
", _weak_field_map_(nullptr)";
2619 "$classname$::$classname$()\n"
2622 " // @@protoc_insertion_point(constructor:$full_name$)\n"
2628 "$classname$::$classname$(::$proto_ns$::Arena* arena)\n"
2631 " RegisterArenaDtor(arena);\n"
2632 " // @@protoc_insertion_point(arena_constructor:$full_name$)\n"
2634 initializer_with_arena);
2644 "$classname$::$classname$(const $classname$& from)\n"
2645 " : $classname$() {\n"
2646 " MergeFrom(from);\n"
2650 "$classname$::$classname$(const $classname$& from)\n"
2651 " : $superclass$()");
2655 format(
",\n_internal_metadata_(nullptr)");
2659 format(
",\n_has_bits_(from._has_bits_)");
2666 if (!(
field->is_repeated() && !(
field->is_map())) &&
2671 processed[
i] =
true;
2676 format(
",\n_any_metadata_(&type_url_, &value_)");
2679 format(
",\n_weak_field_map_(from._weak_field_map_)");
2686 format(
"_internal_metadata_.MergeFrom(from._internal_metadata_);\n");
2689 format(
"_extensions_.MergeFrom(from._extensions_);\n");
2697 "clear_has_$1$();\n"
2698 "switch (from.$1$_case()) {\n",
2710 "case $1$_NOT_SET: {\n"
2720 " // @@protoc_insertion_point(copy_constructor:$full_name$)\n"
2730 "$classname$::~$classname$() {\n"
2731 " // @@protoc_insertion_point(destructor:$full_name$)\n"
2746 "void $classname$::SetCachedSize(int size) const {\n"
2747 " _cached_size_.Set(size);\n"
2751 "const $classname$& $classname$::default_instance() {\n"
2753 "::$proto_ns$::internal::InitSCC(&::$scc_info$.base)"
2755 " return *internal_default_instance();\n"
2763 "PROTOBUF_NOINLINE "
2764 "$classtype$* Arena::CreateMaybeMessage< $classtype$ >(Arena* arena) {\n"
2765 " return Arena::$1$Internal< $classtype$ >(arena);\n"
2773 const int kMaxUnconditionalPrimitiveBytesClear = 4;
2776 "void $classname$::Clear() {\n"
2777 "// @@protoc_insertion_point(message_clear_start:$full_name$)\n");
2783 "$uint32$ cached_has_bits = 0;\n"
2784 "// Prevent compiler warnings about cached_has_bits being unused\n"
2785 "(void) cached_has_bits;\n\n");
2787 int cached_has_bit_index = -1;
2791 format(
"_extensions_.Clear();\n");
2794 int unconditional_budget = kMaxUnconditionalPrimitiveBytesClear;
2805 std::vector<std::vector<const FieldDescriptor*>> chunks_frag = CollectFields(
2807 MatchRepeatedAndHasByteAndZeroInits(
2812 std::vector<std::vector<const FieldDescriptor*>> chunks;
2815 chunks = chunks_frag;
2818 for (
int i = 0;
i < chunks_frag.size();
i++) {
2819 chunks.push_back(chunks_frag[
i]);
2822 (
i + 1) < chunks_frag.size() ? chunks_frag[
i + 1].front() :
nullptr;
2824 (chunks_frag[
i].size() == 1 || unconditional_budget < 0) &&
2825 next_field !=
nullptr &&
2830 chunks.back().insert(chunks.back().end(), chunks_frag[
i + 1].begin(),
2831 chunks_frag[
i + 1].end());
2839 for (
int chunk_index = 0; chunk_index < chunks.size(); chunk_index++) {
2840 std::vector<const FieldDescriptor*>& chunk = chunks[chunk_index];
2845 if (chunk.front()->is_repeated()) {
2846 for (
int i = 0;
i < chunk.size();
i++) {
2855 cold_skipper.OnStartChunk(chunk_index, cached_has_bit_index,
"", printer);
2862 int last_chunk_start = 0;
2863 int memset_run_start = -1;
2864 int memset_run_end = -1;
2866 for (
int i = 0;
i < chunk.size();
i++) {
2869 if (memset_run_start == -1) {
2870 memset_run_start =
i;
2876 const bool have_outer_if =
2878 (memset_run_end != chunk.size() - 1 || unconditional_budget < 0);
2880 if (have_outer_if) {
2882 const int count = popcnt(last_chunk_mask);
2890 if (cached_has_bit_index != last_chunk / 4) {
2891 cached_has_bit_index = last_chunk / 4;
2892 format(
"cached_has_bits = _has_bits_[$1$];\n", cached_has_bit_index);
2894 format(
"if (cached_has_bits & 0x$1$u) {\n",
2899 if (memset_run_start != -1) {
2900 if (memset_run_start == memset_run_end) {
2910 "::memset(&$1$_, 0, static_cast<size_t>(\n"
2911 " reinterpret_cast<char*>(&$2$_) -\n"
2912 " reinterpret_cast<char*>(&$1$_)) + sizeof($2$_));\n",
2913 first_field_name, last_field_name);
2917 last_chunk_start = memset_run_end + 1;
2921 for (
int j = last_chunk_start; j < chunk.size(); j++) {
2929 bool should_check_bit =
2933 bool have_enclosing_if =
false;
2934 if (should_check_bit &&
2938 &cached_has_bit_index);
2939 have_enclosing_if =
true;
2944 if (have_enclosing_if) {
2950 if (have_outer_if) {
2955 if (cold_skipper.OnEndChunk(chunk_index, printer)) {
2957 cached_has_bit_index = -1;
2963 format(
"clear_$1$();\n", oneof->name());
2967 format(
"_weak_field_map_.ClearAll();\n");
2972 format(
"_has_bits_.Clear();\n");
2975 format(
"_internal_metadata_.Clear();\n");
2986 format.Set(
"oneofname", oneof->name());
2989 "void $classname$::clear_$oneofname$() {\n"
2990 "// @@protoc_insertion_point(one_of_clear_start:$full_name$)\n");
2992 format(
"switch ($oneofname$_case()) {\n");
2999 format(
"// No need to clear\n");
3008 "case $1$_NOT_SET: {\n"
3015 "_oneof_case_[$1$] = $2$_NOT_SET;\n",
3027 format(
"void $classname$::InternalSwap($classname$* other) {\n");
3029 format(
"using std::swap;\n");
3033 format(
"_extensions_.Swap(&other->_extensions_);\n");
3036 format(
"_internal_metadata_.Swap(&other->_internal_metadata_);\n");
3040 format(
"swap(_has_bits_[$1$], other->_has_bits_[$1$]);\n",
i);
3052 format(
"swap($1$_, other->$1$_);\n", oneof->name());
3056 format(
"swap(_oneof_case_[$1$], other->_oneof_case_[$1$]);\n",
i);
3060 format(
"_weak_field_map_.UnsafeArenaSwap(&other->_weak_field_map_);\n");
3063 format(
"GetReflection()->Swap(this, other);");
3076 "void $classname$::MergeFrom(const ::$proto_ns$::Message& from) {\n"
3077 "// @@protoc_insertion_point(generalized_merge_from_start:"
3079 " $DCHK$_NE(&from, this);\n");
3087 "const $classname$* source =\n"
3088 " ::$proto_ns$::DynamicCastToGenerated<$classname$>(\n"
3090 "if (source == nullptr) {\n"
3091 "// @@protoc_insertion_point(generalized_merge_from_cast_fail:"
3093 " ::$proto_ns$::internal::ReflectionOps::Merge(from, this);\n"
3095 "// @@protoc_insertion_point(generalized_merge_from_cast_success:"
3097 " MergeFrom(*source);\n"
3105 "void $classname$::CheckTypeAndMergeFrom(\n"
3106 " const ::$proto_ns$::MessageLite& from) {\n"
3107 " MergeFrom(*::$proto_ns$::internal::DownCast<const $classname$*>(\n"
3115 "void $classname$::MergeFrom(const $classname$& from) {\n"
3116 "// @@protoc_insertion_point(class_specific_merge_from_start:"
3118 " $DCHK$_NE(&from, this);\n");
3122 format(
"_extensions_.MergeFrom(from._extensions_);\n");
3126 "_internal_metadata_.MergeFrom(from._internal_metadata_);\n"
3127 "$uint32$ cached_has_bits = 0;\n"
3128 "(void) cached_has_bits;\n\n");
3131 std::vector<std::vector<const FieldDescriptor*>> chunks = CollectFields(
3140 int cached_has_bit_index = -1;
3142 for (
int chunk_index = 0; chunk_index < chunks.size(); chunk_index++) {
3143 const std::vector<const FieldDescriptor*>& chunk = chunks[chunk_index];
3148 if (chunk.front()->is_repeated()) {
3149 for (
int i = 0;
i < chunk.size();
i++) {
3159 cold_skipper.OnStartChunk(chunk_index, cached_has_bit_index,
"from.",
3165 const bool have_outer_if = chunk.size() > 1;
3166 if (have_outer_if) {
3168 const int count = popcnt(last_chunk_mask);
3176 if (cached_has_bit_index != last_chunk / 4) {
3177 cached_has_bit_index = last_chunk / 4;
3178 format(
"cached_has_bits = from._has_bits_[$1$];\n",
3179 cached_has_bit_index);
3181 format(
"if (cached_has_bits & 0x$1$u) {\n",
3187 bool deferred_has_bit_changes =
false;
3188 for (
const auto field : chunk) {
3193 if (!
field->options().weak() &&
3194 cached_has_bit_index == has_bit_index / 32) {
3198 format(
"if (cached_has_bits & 0x$1$u) {\n",
mask);
3204 if (have_outer_if && IsPOD(
field)) {
3210 deferred_has_bit_changes =
true;
3220 if (have_outer_if) {
3221 if (deferred_has_bit_changes) {
3224 format(
"_has_bits_[$1$] |= cached_has_bits;\n", cached_has_bit_index);
3231 if (cold_skipper.OnEndChunk(chunk_index, printer)) {
3233 cached_has_bit_index = -1;
3242 bool have_enclosing_if =
3243 EmitFieldNonDefaultCondition(printer,
"from.",
field);
3247 if (have_enclosing_if) {
3256 format(
"switch (from.$1$_case()) {\n", oneof->name());
3267 "case $1$_NOT_SET: {\n"
3275 format(
"_weak_field_map_.MergeFrom(from._weak_field_map_);\n");
3288 "void $classname$::CopyFrom(const ::$proto_ns$::Message& from) {\n"
3289 "// @@protoc_insertion_point(generalized_copy_from_start:"
3293 format(
"if (&from == this) return;\n");
3301 "size_t from_size = from.ByteSizeLong();\n"
3305 "$CHK$_EQ(from_size, from.ByteSizeLong())\n"
3306 " << \"Source of CopyFrom changed when clearing target. Either \"\n"
3307 " << \"source is a nested message in target (not allowed), or \"\n"
3308 " << \"another thread is modifying the source.\";\n"
3313 format(
"MergeFrom(from);\n");
3321 "void $classname$::CopyFrom(const $classname$& from) {\n"
3322 "// @@protoc_insertion_point(class_specific_copy_from_start:"
3326 format(
"if (&from == this) return;\n");
3333 "size_t from_size = from.ByteSizeLong();\n"
3337 "$CHK$_EQ(from_size, from.ByteSizeLong())\n"
3338 " << \"Source of CopyFrom changed when clearing target. Either \"\n"
3339 " << \"source is a nested message in target (not allowed), or \"\n"
3340 " << \"another thread is modifying the source.\";\n"
3345 format(
"MergeFrom(from);\n");
3352 std::map<std::string, std::string> vars =
variables_;
3358 "#if $GOOGLE_PROTOBUF$_ENABLE_EXPERIMENTAL_PARSER\n"
3359 "const char* $classname$::_InternalParse(const char* ptr,\n"
3360 " ::$proto_ns$::internal::ParseContext* ctx) {\n"
3361 " return _extensions_.ParseMessageSet(ptr, \n"
3362 " internal_default_instance(), &_internal_metadata_, ctx);\n"
3365 "bool $classname$::MergePartialFromCodedStream(\n"
3366 " ::$proto_ns$::io::CodedInputStream* input) {\n"
3367 " return _extensions_.ParseMessageSet(input,\n"
3368 " internal_default_instance(), $mutable_unknown_fields$);\n"
3370 "#endif // $GOOGLE_PROTOBUF$_ENABLE_EXPERIMENTAL_PARSER\n");
3373 format(
"#if $GOOGLE_PROTOBUF$_ENABLE_EXPERIMENTAL_PARSER\n");
3376 format(
"#else // $GOOGLE_PROTOBUF$_ENABLE_EXPERIMENTAL_PARSER\n");
3377 std::vector<const FieldDescriptor*> ordered_fields =
3381 "bool $classname$::MergePartialFromCodedStream(\n"
3382 " ::$proto_ns$::io::CodedInputStream* input) {\n");
3391 "return ::$proto_ns$::internal::MergePartialFromCodedStream$1$(\n"
3392 " this, ::$tablename$::schema[\n"
3393 " $classname$::kIndexInFileMessages], input);\n",
3399 format(
"#endif // $GOOGLE_PROTOBUF$_ENABLE_EXPERIMENTAL_PARSER\n");
3404 for (
auto field : ordered_fields) {
3407 format(
" ::$proto_ns$::Arena* arena = GetArenaNoVirtual();\n");
3414 "#define DO_(EXPRESSION) if "
3415 "(!PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure\n"
3416 " $uint32$ tag;\n");
3420 " ::$proto_ns$::internal::LiteUnknownFieldSetter "
3421 "unknown_fields_setter(\n"
3422 " &_internal_metadata_);\n"
3423 " ::$proto_ns$::io::StringOutputStream unknown_fields_output(\n"
3424 " unknown_fields_setter.buffer());\n"
3425 " ::$proto_ns$::io::CodedOutputStream unknown_fields_stream(\n"
3426 " &unknown_fields_output, false);\n");
3429 format(
" // @@protoc_insertion_point(parse_start:$full_name$)\n");
3440 ordered_fields.empty() ? 0 : ordered_fields.back()->number() * 8 + 5;
3441 const int kCutoff0 = 127;
3442 const int kCutoff1 = (127 << 7) + 127;
3447 bool capture_last_tag =
false;
3453 capture_last_tag =
true;
3462 capture_last_tag =
true;
3472 capture_last_tag =
true;
3478 "::std::pair<$uint32$, bool> p = "
3479 "input->ReadTagWithCutoffNoLastTag($1$u);\n"
3481 "if (!p.second) goto handle_unusual;\n",
3482 maxtag <= kCutoff0 ? kCutoff0 : (maxtag <= kCutoff1 ? kCutoff1 : maxtag));
3501 format(
"uint32 weak_offset;\n");
3504 "switch (::$proto_ns$::internal::WireFormatLite::"
3505 "GetTagFieldNumber(tag)) {\n");
3509 for (
auto field : ordered_fields) {
3514 " weak_offset = offsetof($classname$DefaultTypeInternal, $2$_);\n"
3515 " goto handle_weak_field_map;\n",
3527 format(
"if (static_cast< $uint8$>(tag) == ($1$ & 0xFF)) {\n",
3531 if (
field->is_packed()) {
3539 if (
field->is_packed()) {
3544 format(
"} else if (static_cast< $uint8$>(tag) == ($1$ & 0xFF)) {\n",
3550 }
else if (
field->is_packable() && !
field->is_packed()) {
3555 format(
"} else if (static_cast< $uint8$>(tag) == ($1$ & 0xFF)) {\n",
3564 " goto handle_unusual;\n"
3573 format(
"handle_weak_field_map: {\n");
3577 "if ((tag & 0x7) != 2) goto handle_unusual;\n"
3578 "DO_(_weak_field_map_.ReadMessage(input, tag >> 3,\n"
3579 " &_$classname$_default_instance_, weak_offset));\n");
3589 format(
"handle_unusual:\n");
3592 if (capture_last_tag) {
3595 " ::$proto_ns$::internal::WireFormatLite::GetTagWireType(tag) ==\n"
3596 " ::$proto_ns$::internal::WireFormatLite::WIRETYPE_END_GROUP) {\n"
3597 " input->SetLastTag(tag);\n"
3620 format(
"($1$u <= tag)", start_tag);
3622 format(
"($1$u <= tag && tag < $2$u)", start_tag, end_tag);
3628 " DO_(_extensions_.ParseField(tag, input,\n"
3629 " internal_default_instance(),\n"
3630 " $mutable_unknown_fields$));\n");
3633 " DO_(_extensions_.ParseField(tag, input,\n"
3634 " internal_default_instance(),\n"
3635 " &unknown_fields_stream));\n");
3645 "DO_(::$proto_ns$::internal::WireFormat::SkipField(\n"
3646 " input, tag, $mutable_unknown_fields$));\n");
3649 "DO_(::$proto_ns$::internal::WireFormatLite::SkipField(\n"
3650 " input, tag, &unknown_fields_stream));\n");
3666 " // @@protoc_insertion_point(parse_success:$full_name$)\n"
3669 " // @@protoc_insertion_point(parse_failure:$full_name$)\n"
3673 format(
"#endif // $GOOGLE_PROTOBUF$_ENABLE_EXPERIMENTAL_PARSER\n");
3681 if (
fields.size() == 1) {
3687 format(
"switch ($1$_case()) {\n", oneof->
name());
3711 int cached_has_bits_index) {
3713 if (!
field->options().weak()) {
3718 bool have_enclosing_if =
false;
3719 if (
field->options().weak()) {
3723 if (cached_has_bits_index == has_bit_index / 32) {
3727 format(
"if (cached_has_bits & 0x$1$u) {\n",
mask);
3733 have_enclosing_if =
true;
3735 have_enclosing_if = EmitFieldNonDefaultCondition(printer,
"this->",
field);
3745 if (have_enclosing_if) {
3755 std::map<std::string, std::string> vars;
3759 format(
"// Extension range [$start$, $end$)\n");
3762 "target = _extensions_.InternalSerializeWithCachedSizesToArray(\n"
3763 " $start$, $end$, target);\n\n");
3766 "_extensions_.SerializeWithCachedSizes($start$, $end$, output);\n"
3776 "void $classname$::SerializeWithCachedSizes(\n"
3777 " ::$proto_ns$::io::CodedOutputStream* output) const {\n"
3778 " _extensions_.SerializeMessageSetWithCachedSizes(output);\n");
3779 std::map<std::string, std::string> vars;
3784 "::$proto_ns$::internal::SerializeUnknownMessageSetItems(\n"
3785 " $unknown_fields$, output);\n");
3792 "void $classname$::SerializeWithCachedSizes(\n"
3793 " ::$proto_ns$::io::CodedOutputStream* output) const {\n");
3796 format(
"// @@protoc_insertion_point(serialize_start:$full_name$)\n");
3800 format(
"// @@protoc_insertion_point(serialize_end:$full_name$)\n");
3812 "$uint8$* $classname$::InternalSerializeWithCachedSizesToArray(\n"
3813 " $uint8$* target) const {\n"
3814 " target = _extensions_."
3815 "InternalSerializeMessageSetWithCachedSizesToArray(target);\n");
3817 std::map<std::string, std::string> vars;
3821 " target = ::$proto_ns$::internal::WireFormat::\n"
3822 " SerializeUnknownMessageSetItemsToArray(\n"
3823 " $unknown_fields$, target);\n");
3831 "$uint8$* $classname$::InternalSerializeWithCachedSizesToArray(\n"
3832 " $uint8$* target) const {\n");
3835 format(
"// @@protoc_insertion_point(serialize_to_array_start:$full_name$)\n");
3839 format(
"// @@protoc_insertion_point(serialize_to_array_end:$full_name$)\n");
3855 class LazySerializerEmitter {
3861 to_array_(to_array),
3863 cached_has_bit_index_(-1) {}
3865 ~LazySerializerEmitter() { Flush(); }
3870 if (eager_ || MustFlush(
field)) {
3873 if (
field->containing_oneof() ==
NULL) {
3876 if (!
field->options().weak() && !
field->is_repeated() && !eager_) {
3880 int has_bit_index = mg_->has_bit_indices_[
field->index()];
3881 if (cached_has_bit_index_ != has_bit_index / 32) {
3883 int new_index = has_bit_index / 32;
3885 format_(
"cached_has_bits = _has_bits_[$1$];\n", new_index);
3887 cached_has_bit_index_ = new_index;
3891 mg_->GenerateSerializeOneField(format_.printer(),
field, to_array_,
3892 cached_has_bit_index_);
3894 v_.push_back(
field);
3900 mg_->GenerateSerializeOneofFields(format_.printer(), v_, to_array_);
3909 return !v_.empty() &&
3910 v_[0]->containing_oneof() !=
field->containing_oneof();
3915 const bool to_array_;
3917 std::vector<const FieldDescriptor*> v_;
3922 int cached_has_bit_index_;
3925 std::vector<const FieldDescriptor*> ordered_fields =
3928 std::vector<const Descriptor::ExtensionRange*> sorted_extensions;
3933 std::sort(sorted_extensions.begin(), sorted_extensions.end(),
3934 ExtensionRangeSorter());
3937 "::$proto_ns$::internal::WeakFieldMap::FieldWriter field_writer("
3938 "_weak_field_map_);\n");
3942 "$uint32$ cached_has_bits = 0;\n"
3943 "(void) cached_has_bits;\n\n");
3947 LazySerializerEmitter e(
this, printer, to_array);
3951 i < ordered_fields.size() || j < sorted_extensions.size();) {
3952 if ((j == sorted_extensions.size()) ||
3953 (i < descriptor_->field_count() &&
3954 ordered_fields[
i]->
number() < sorted_extensions[j]->
start)) {
3956 if (
field->options().weak()) {
3957 last_weak_field =
field;
3960 if (last_weak_field !=
nullptr) {
3961 e.Emit(last_weak_field);
3962 last_weak_field =
nullptr;
3967 if (last_weak_field !=
nullptr) {
3968 e.Emit(last_weak_field);
3969 last_weak_field =
nullptr;
3976 if (last_weak_field !=
nullptr) {
3977 e.Emit(last_weak_field);
3981 std::map<std::string, std::string> vars;
3985 format(
"if ($have_unknown_fields$) {\n");
3990 "::$proto_ns$::internal::WireFormat::SerializeUnknownFieldsToArray(\n"
3991 " $unknown_fields$, target);\n");
3994 "::$proto_ns$::internal::WireFormat::SerializeUnknownFields(\n"
3995 " $unknown_fields$, output);\n");
4002 "output->WriteRaw($unknown_fields$.data(),\n"
4003 " static_cast<int>($unknown_fields$.size()));\n");
4009 std::vector<uint32> masks(array_size, 0);
4012 if (!
field->is_required()) {
4017 masks[has_bit_index / 32] |=
static_cast<uint32>(1) << (has_bit_index % 32);
4027 const std::vector<uint32>& masks) {
4028 std::vector<std::string> parts;
4029 for (
int i = 0;
i < masks.size();
i++) {
4030 if (masks[
i] == 0)
continue;
4034 StrCat(
"((_has_bits_[",
i,
"] & ",
m,
") ^ ",
m,
")"));
4042 return result +
" == 0";
4050 std::map<std::string, std::string> vars;
4054 "size_t $classname$::ByteSizeLong() const {\n"
4055 "// @@protoc_insertion_point(message_set_byte_size_start:$full_name$)\n"
4056 " size_t total_size = _extensions_.MessageSetByteSize();\n"
4057 " if ($have_unknown_fields$) {\n"
4058 " total_size += ::$proto_ns$::internal::\n"
4059 " ComputeUnknownMessageSetItemsSize($unknown_fields$);\n"
4061 " int cached_size = "
4062 "::$proto_ns$::internal::ToCachedSize(total_size);\n"
4063 " SetCachedSize(cached_size);\n"
4064 " return total_size;\n"
4073 "size_t $classname$::RequiredFieldsByteSizeFallback() const {\n"
4074 "// @@protoc_insertion_point(required_fields_byte_size_fallback_start:"
4077 format(
"size_t total_size = 0;\n");
4079 if (
field->is_required()) {
4082 "if (has_$1$()) {\n",
4093 "return total_size;\n");
4099 "size_t $classname$::ByteSizeLong() const {\n"
4100 "// @@protoc_insertion_point(message_byte_size_start:$full_name$)\n");
4103 "size_t total_size = 0;\n"
4108 "total_size += _extensions_.ByteSize();\n"
4112 std::map<std::string, std::string> vars;
4117 "if ($have_unknown_fields$) {\n"
4119 " ::$proto_ns$::internal::WireFormat::ComputeUnknownFieldsSize(\n"
4120 " $unknown_fields$);\n"
4124 "total_size += $unknown_fields$.size();\n"
4134 format(
"if ($1$) { // All required fields are present.\n",
4140 if (!
field->is_required())
continue;
4148 " total_size += RequiredFieldsByteSizeFallback();\n"
4153 if (!
field->is_required())
continue;
4163 std::vector<std::vector<const FieldDescriptor*>> chunks = CollectFields(
4165 MatchRepeatedAndHasByteAndRequired(
4169 chunks.erase(std::remove_if(chunks.begin(), chunks.end(), IsRequired),
4176 "$uint32$ cached_has_bits = 0;\n"
4177 "// Prevent compiler warnings about cached_has_bits being unused\n"
4178 "(void) cached_has_bits;\n\n");
4180 int cached_has_bit_index = -1;
4182 for (
int chunk_index = 0; chunk_index < chunks.size(); chunk_index++) {
4183 const std::vector<const FieldDescriptor*>& chunk = chunks[chunk_index];
4187 if (chunk.front()->is_repeated()) {
4188 for (
int i = 0;
i < chunk.size();
i++) {
4199 cold_skipper.OnStartChunk(chunk_index, cached_has_bit_index,
"", printer);
4215 const bool have_outer_if =
4218 if (have_outer_if) {
4220 const int count = popcnt(last_chunk_mask);
4228 if (cached_has_bit_index != last_chunk / 4) {
4229 cached_has_bit_index = last_chunk / 4;
4230 format(
"cached_has_bits = _has_bits_[$1$];\n", cached_has_bit_index);
4232 format(
"if (cached_has_bits & 0x$1$u) {\n",
4238 for (
int j = 0; j < chunk.size(); j++) {
4244 bool have_enclosing_if =
false;
4247 &cached_has_bit_index);
4248 have_enclosing_if =
true;
4253 EmitFieldNonDefaultCondition(printer,
"this->",
field);
4258 if (have_enclosing_if) {
4266 if (have_outer_if) {
4271 if (cold_skipper.OnEndChunk(chunk_index, printer)) {
4273 cached_has_bit_index = -1;
4280 format(
"switch ($1$_case()) {\n", oneof->name());
4292 "case $1$_NOT_SET: {\n"
4302 format(
"total_size += _weak_field_map_.ByteSizeLong();\n");
4314 "int cached_size = ::$proto_ns$::internal::ToCachedSize(total_size);\n"
4315 "SetCachedSize(cached_size);\n"
4316 "return total_size;\n");
4324 format(
"bool $classname$::IsInitialized() const {\n");
4329 "if (!_extensions_.IsInitialized()) {\n"
4339 for (
int i = 0;
i < masks.size();
i++) {
4348 format(
"if ((_has_bits_[$1$] & 0x$2$) != 0x$2$) return false;\n",
4360 if (
field->is_repeated()) {
4363 "if (!::$proto_ns$::internal::AllAreInitializedWeak(this->$1$_))"
4368 "if (!::$proto_ns$::internal::AllAreInitialized(this->$1$()))"
4372 }
else if (
field->options().weak()) {
4377 "if (has_$1$()) {\n"
4378 " if (!this->$1$_->IsInitialized()) return false;\n"
4386 format(
"if (!_weak_field_map_.IsInitialized()) return false;\n");
4391 bool has_required_fields =
false;
4396 has_required_fields =
true;
4401 if (!has_required_fields) {
4405 format(
"switch ($1$_case()) {\n", oneof->name());
4415 if (
field->options().weak()) {
4419 "if (has_$1$()) {\n"
4420 " if (!this->$1$().IsInitialized()) return false;\n"
4431 "case $1$_NOT_SET: {\n"