35
35
#include <algorithm>
36
36
#include <google/protobuf/stubs/hash.h>
37
39
#include <google/protobuf/compiler/cpp/cpp_message.h>
40
#include <google/protobuf/compiler/cpp/cpp_field.h>
38
41
#include <google/protobuf/compiler/cpp/cpp_enum.h>
39
42
#include <google/protobuf/compiler/cpp/cpp_extension.h>
40
43
#include <google/protobuf/compiler/cpp/cpp_helpers.h>
41
44
#include <google/protobuf/stubs/strutil.h>
42
45
#include <google/protobuf/io/printer.h>
43
46
#include <google/protobuf/io/coded_stream.h>
44
#include <google/protobuf/wire_format_inl.h>
47
#include <google/protobuf/wire_format.h>
45
48
#include <google/protobuf/descriptor.pb.h>
198
202
void MessageGenerator::
203
GenerateGetEnumDescriptorSpecializations(io::Printer* printer) {
204
for (int i = 0; i < descriptor_->nested_type_count(); i++) {
205
nested_generators_[i]->GenerateGetEnumDescriptorSpecializations(printer);
207
for (int i = 0; i < descriptor_->enum_type_count(); i++) {
208
enum_generators_[i]->GenerateGetEnumDescriptorSpecializations(printer);
212
void MessageGenerator::
199
213
GenerateFieldAccessorDeclarations(io::Printer* printer) {
200
214
for (int i = 0; i < descriptor_->field_count(); i++) {
201
215
const FieldDescriptor* field = descriptor_->field(i);
203
217
PrintFieldComment(printer, field);
205
219
map<string, string> vars;
206
vars["name"] = FieldName(field);
220
SetCommonFieldVariables(field, &vars);
207
221
vars["constant_name"] = FieldConstantName(field);
208
vars["number"] = SimpleItoa(field->number());
210
223
if (field->is_repeated()) {
211
printer->Print(vars, "inline int $name$_size() const;\n");
224
printer->Print(vars, "inline int $name$_size() const$deprecation$;\n");
213
printer->Print(vars, "inline bool has_$name$() const;\n");
226
printer->Print(vars, "inline bool has_$name$() const$deprecation$;\n");
216
printer->Print(vars, "inline void clear_$name$();\n");
229
printer->Print(vars, "inline void clear_$name$()$deprecation$;\n");
217
230
printer->Print(vars, "static const int $constant_name$ = $number$;\n");
219
232
// Generate type-specific accessor declarations.
241
254
PrintFieldComment(printer, field);
243
256
map<string, string> vars;
244
vars["name"] = FieldName(field);
245
vars["index"] = SimpleItoa(field->index());
246
vars["classname"] = classname_;
257
SetCommonFieldVariables(field, &vars);
248
259
// Generate has_$name$() or $name$_size().
249
260
if (field->is_repeated()) {
298
309
vars["dllexport"] = dllexport_decl_ + " ";
311
vars["superclass"] = HasDescriptorMethods(descriptor_->file()) ?
312
"Message" : "MessageLite";
301
314
printer->Print(vars,
302
"class $dllexport$$classname$ : public ::google::protobuf::Message {\n"
315
"class $dllexport$$classname$ : public ::google::protobuf::$superclass$ {\n"
304
317
printer->Indent();
313
326
" CopyFrom(from);\n"
314
327
" return *this;\n"
317
"inline const ::google::protobuf::UnknownFieldSet& unknown_fields() const {\n"
318
" return _unknown_fields_;\n"
321
"inline ::google::protobuf::UnknownFieldSet* mutable_unknown_fields() {\n"
322
" return &_unknown_fields_;\n"
325
"static const ::google::protobuf::Descriptor* descriptor();\n"
331
if (HasUnknownFields(descriptor_->file())) {
333
"inline const ::google::protobuf::UnknownFieldSet& unknown_fields() const {\n"
334
" return _unknown_fields_;\n"
337
"inline ::google::protobuf::UnknownFieldSet* mutable_unknown_fields() {\n"
338
" return &_unknown_fields_;\n"
343
// Only generate this member if it's not disabled.
344
if (HasDescriptorMethods(descriptor_->file()) &&
345
!descriptor_->options().no_standard_descriptor_accessor()) {
347
"static const ::google::protobuf::Descriptor* descriptor();\n");
326
351
"static const $classname$& default_instance();\n"
327
352
"void Swap($classname$* other);\n"
331
356
"$classname$* New() const;\n");
333
if (descriptor_->file()->options().optimize_for() == FileOptions::SPEED) {
358
if (HasGeneratedMethods(descriptor_->file())) {
359
if (HasDescriptorMethods(descriptor_->file())) {
361
"void CopyFrom(const ::google::protobuf::Message& from);\n"
362
"void MergeFrom(const ::google::protobuf::Message& from);\n");
365
"void CheckTypeAndMergeFrom(const ::google::protobuf::MessageLite& from);\n");
334
368
printer->Print(vars,
335
"void CopyFrom(const ::google::protobuf::Message& from);\n"
336
"void MergeFrom(const ::google::protobuf::Message& from);\n"
337
369
"void CopyFrom(const $classname$& from);\n"
338
370
"void MergeFrom(const $classname$& from);\n"
339
371
"void Clear();\n"
340
"bool IsInitialized() const;\n");
342
if (!descriptor_->options().message_set_wire_format()) {
343
// For message_set_wire_format, we don't generate parsing or
344
// serialization code even if optimize_for = SPEED, since MessageSet
345
// encoding is somewhat more complicated than normal extension encoding
346
// and we'd like to avoid having to implement it in multiple places.
347
// WireFormat's implementation is probably good enough.
350
"int ByteSize() const;\n"
351
"bool MergePartialFromCodedStream(\n"
352
" ::google::protobuf::io::CodedInputStream* input);\n"
353
"void SerializeWithCachedSizes(\n"
354
" ::google::protobuf::io::CodedOutputStream* output) const;\n"
372
"bool IsInitialized() const;\n"
374
"int ByteSize() const;\n"
375
"bool MergePartialFromCodedStream(\n"
376
" ::google::protobuf::io::CodedInputStream* input);\n"
377
"void SerializeWithCachedSizes(\n"
378
" ::google::protobuf::io::CodedOutputStream* output) const;\n");
379
if (HasFastArraySerialization(descriptor_->file())) {
355
381
"::google::protobuf::uint8* SerializeWithCachedSizesToArray(::google::protobuf::uint8* output) const;\n");
363
389
"void SharedDtor();\n"
364
390
"void SetCachedSize(int size) const { _cached_size_ = size; }\n"
367
"const ::google::protobuf::Descriptor* GetDescriptor() const;\n"
368
"const ::google::protobuf::Reflection* GetReflection() const;\n"
394
if (HasDescriptorMethods(descriptor_->file())) {
396
"::google::protobuf::Metadata GetMetadata() const;\n"
400
"::std::string GetTypeName() const;\n"
370
405
"// nested types ----------------------------------------------------\n"
411
446
"::google::protobuf::internal::ExtensionSet _extensions_;\n");
449
if (HasUnknownFields(descriptor_->file())) {
451
"::google::protobuf::UnknownFieldSet _unknown_fields_;\n");
414
454
// TODO(kenton): Make _cached_size_ an atomic<int> when C++ supports it.
416
"::google::protobuf::UnknownFieldSet _unknown_fields_;\n"
417
456
"mutable int _cached_size_;\n"
419
458
for (int i = 0; i < descriptor_->field_count(); i++) {
431
470
GlobalAddDescriptorsName(descriptor_->file()->name()));
433
472
"friend void $assigndescriptorsname$();\n"
434
"friend void $shutdownfilename$();\n",
473
"friend void $shutdownfilename$();\n"
435
475
"assigndescriptorsname",
436
476
GlobalAssignDescriptorsName(descriptor_->file()->name()),
437
477
"shutdownfilename", GlobalShutdownFileName(descriptor_->file()->name()));
605
645
void MessageGenerator::
606
646
GenerateShutdownCode(io::Printer* printer) {
608
"delete $classname$::default_instance_;\n"
609
"delete $classname$_reflection_;\n",
648
"delete $classname$::default_instance_;\n",
610
649
"classname", classname_);
651
if (HasDescriptorMethods(descriptor_->file())) {
653
"delete $classname$_reflection_;\n",
654
"classname", classname_);
612
657
// Handle nested types.
613
658
for (int i = 0; i < descriptor_->nested_type_count(); i++) {
614
659
nested_generators_[i]->GenerateShutdownCode(printer);
655
700
GenerateStructors(printer);
656
701
printer->Print("\n");
658
if (descriptor_->file()->options().optimize_for() == FileOptions::SPEED) {
703
if (HasGeneratedMethods(descriptor_->file())) {
659
704
GenerateClear(printer);
660
705
printer->Print("\n");
662
if (!descriptor_->options().message_set_wire_format()) {
663
// For message_set_wire_format, we don't generate parsing or
664
// serialization code even if optimize_for = SPEED, since MessageSet
665
// encoding is somewhat more complicated than normal extension encoding
666
// and we'd like to avoid having to implement it in multiple places.
667
// WireFormat's implementation is probably good enough.
668
GenerateMergeFromCodedStream(printer);
669
printer->Print("\n");
671
GenerateSerializeWithCachedSizes(printer);
672
printer->Print("\n");
707
GenerateMergeFromCodedStream(printer);
708
printer->Print("\n");
710
GenerateSerializeWithCachedSizes(printer);
711
printer->Print("\n");
713
if (HasFastArraySerialization(descriptor_->file())) {
674
714
GenerateSerializeWithCachedSizesToArray(printer);
675
715
printer->Print("\n");
677
GenerateByteSize(printer);
678
printer->Print("\n");
718
GenerateByteSize(printer);
719
printer->Print("\n");
681
721
GenerateMergeFrom(printer);
682
722
printer->Print("\n");
684
724
GenerateCopyFrom(printer);
685
725
printer->Print("\n");
687
GenerateSwap(printer);
688
printer->Print("\n");
690
727
GenerateIsInitialized(printer);
691
728
printer->Print("\n");
695
"const ::google::protobuf::Descriptor* $classname$::GetDescriptor() const {\n"
696
" return descriptor();\n"
699
"const ::google::protobuf::Reflection* $classname$::GetReflection() const {\n"
700
" protobuf_AssignDescriptorsOnce();\n"
701
" return $classname$_reflection_;\n"
703
"classname", classname_);
731
GenerateSwap(printer);
732
printer->Print("\n");
734
if (HasDescriptorMethods(descriptor_->file())) {
736
"::google::protobuf::Metadata $classname$::GetMetadata() const {\n"
737
" protobuf_AssignDescriptorsOnce();\n"
738
" ::google::protobuf::Metadata metadata;\n"
739
" metadata.descriptor = $classname$_descriptor_;\n"
740
" metadata.reflection = $classname$_reflection_;\n"
741
" return metadata;\n"
744
"classname", classname_);
747
"::std::string $classname$::GetTypeName() const {\n"
748
" return \"$type_name$\";\n"
751
"classname", classname_,
752
"type_name", descriptor_->full_name());
706
756
void MessageGenerator::
797
835
GenerateStructors(io::Printer* printer) {
798
836
// Generate the default constructor.
800
"$classname$::$classname$()\n"
802
"classname", classname_);
803
GenerateInitializerList(printer);
804
printer->Print(" {\n"
838
"$classname$::$classname$() {\n"
805
839
" SharedCtor();\n"
841
"classname", classname_);
810
"void $classname$::InitAsDefaultInstance() {",
845
"void $classname$::InitAsDefaultInstance() {\n",
811
846
"classname", classname_);
813
848
// The default instance needs all of its embedded message pointers
834
869
// Generate the copy constructor.
836
"$classname$::$classname$(const $classname$& from)\n"
838
"classname", classname_);
839
GenerateInitializerList(printer);
840
printer->Print(" {\n"
871
"$classname$::$classname$(const $classname$& from) {\n"
841
872
" SharedCtor();\n"
842
873
" MergeFrom(from);\n"
876
"classname", classname_);
846
878
// Generate the shared constructor code.
847
879
GenerateSharedConstructorCode(printer);
857
889
// Generate the shared destructor code.
858
890
GenerateSharedDestructorCode(printer);
892
// Only generate this member if it's not disabled.
893
if (HasDescriptorMethods(descriptor_->file()) &&
894
!descriptor_->options().no_standard_descriptor_accessor()) {
896
"const ::google::protobuf::Descriptor* $classname$::descriptor() {\n"
897
" protobuf_AssignDescriptorsOnce();\n"
898
" return $classname$_descriptor_;\n"
901
"classname", classname_,
902
"adddescriptorsname",
903
GlobalAddDescriptorsName(descriptor_->file()->name()));
861
"const ::google::protobuf::Descriptor* $classname$::descriptor() {\n"
862
" protobuf_AssignDescriptorsOnce();\n"
863
" return $classname$_descriptor_;\n"
866
907
"const $classname$& $classname$::default_instance() {\n"
867
908
" if (default_instance_ == NULL) $adddescriptorsname$();"
868
909
" return *default_instance_;\n"
954
"::memset(_has_bits_, 0, sizeof(_has_bits_));\n"
955
"mutable_unknown_fields()->Clear();\n");
995
"::memset(_has_bits_, 0, sizeof(_has_bits_));\n");
997
if (HasUnknownFields(descriptor_->file())) {
999
"mutable_unknown_fields()->Clear();\n");
957
1002
printer->Outdent();
958
1003
printer->Print("}\n");
967
1012
printer->Print("if (other != this) {\n");
968
1013
printer->Indent();
970
for (int i = 0; i < descriptor_->field_count(); i++) {
971
const FieldDescriptor* field = descriptor_->field(i);
972
field_generators_.get(field).GenerateSwappingCode(printer);
975
for (int i = 0; i < (descriptor_->field_count() + 31) / 32; ++i) {
976
printer->Print("std::swap(_has_bits_[$i$], other->_has_bits_[$i$]);\n",
980
printer->Print("_unknown_fields_.Swap(&other->_unknown_fields_);\n");
981
printer->Print("std::swap(_cached_size_, other->_cached_size_);\n");
982
if (descriptor_->extension_range_count() > 0) {
983
printer->Print("_extensions_.Swap(&other->_extensions_);\n");
1015
if (HasGeneratedMethods(descriptor_->file())) {
1016
for (int i = 0; i < descriptor_->field_count(); i++) {
1017
const FieldDescriptor* field = descriptor_->field(i);
1018
field_generators_.get(field).GenerateSwappingCode(printer);
1021
for (int i = 0; i < (descriptor_->field_count() + 31) / 32; ++i) {
1022
printer->Print("std::swap(_has_bits_[$i$], other->_has_bits_[$i$]);\n",
1023
"i", SimpleItoa(i));
1026
if (HasUnknownFields(descriptor_->file())) {
1027
printer->Print("_unknown_fields_.Swap(&other->_unknown_fields_);\n");
1029
printer->Print("std::swap(_cached_size_, other->_cached_size_);\n");
1030
if (descriptor_->extension_range_count() > 0) {
1031
printer->Print("_extensions_.Swap(&other->_extensions_);\n");
1034
printer->Print("GetReflection()->Swap(this, other);");
986
1037
printer->Outdent();
992
1043
void MessageGenerator::
993
1044
GenerateMergeFrom(io::Printer* printer) {
994
// Generate the generalized MergeFrom (aka that which takes in the Message
995
// base class as a parameter).
997
"void $classname$::MergeFrom(const ::google::protobuf::Message& from) {\n"
998
" GOOGLE_CHECK_NE(&from, this);\n",
999
"classname", classname_);
1002
// Cast the message to the proper type. If we find that the message is
1003
// *not* of the proper type, we can still call Merge via the reflection
1004
// system, as the GOOGLE_CHECK above ensured that we have the same descriptor
1005
// for each message.
1007
"const $classname$* source =\n"
1008
" ::google::protobuf::internal::dynamic_cast_if_available<const $classname$*>(\n"
1010
"if (source == NULL) {\n"
1011
" ::google::protobuf::internal::ReflectionOps::Merge(from, this);\n"
1013
" MergeFrom(*source);\n"
1015
"classname", classname_);
1018
printer->Print("}\n\n");
1045
if (HasDescriptorMethods(descriptor_->file())) {
1046
// Generate the generalized MergeFrom (aka that which takes in the Message
1047
// base class as a parameter).
1049
"void $classname$::MergeFrom(const ::google::protobuf::Message& from) {\n"
1050
" GOOGLE_CHECK_NE(&from, this);\n",
1051
"classname", classname_);
1054
// Cast the message to the proper type. If we find that the message is
1055
// *not* of the proper type, we can still call Merge via the reflection
1056
// system, as the GOOGLE_CHECK above ensured that we have the same descriptor
1057
// for each message.
1059
"const $classname$* source =\n"
1060
" ::google::protobuf::internal::dynamic_cast_if_available<const $classname$*>(\n"
1062
"if (source == NULL) {\n"
1063
" ::google::protobuf::internal::ReflectionOps::Merge(from, this);\n"
1065
" MergeFrom(*source);\n"
1067
"classname", classname_);
1070
printer->Print("}\n\n");
1072
// Generate CheckTypeAndMergeFrom().
1074
"void $classname$::CheckTypeAndMergeFrom(\n"
1075
" const ::google::protobuf::MessageLite& from) {\n"
1076
" MergeFrom(*::google::protobuf::down_cast<const $classname$*>(&from));\n"
1079
"classname", classname_);
1020
1082
// Generate the class-specific MergeFrom, which avoids the GOOGLE_CHECK and cast.
1021
1083
printer->Print(
1087
1151
void MessageGenerator::
1088
1152
GenerateCopyFrom(io::Printer* printer) {
1089
// Generate the generalized CopyFrom (aka that which takes in the Message
1090
// base class as a parameter).
1092
"void $classname$::CopyFrom(const ::google::protobuf::Message& from) {\n",
1093
"classname", classname_);
1097
"if (&from == this) return;\n"
1099
"MergeFrom(from);\n");
1102
printer->Print("}\n\n");
1153
if (HasDescriptorMethods(descriptor_->file())) {
1154
// Generate the generalized CopyFrom (aka that which takes in the Message
1155
// base class as a parameter).
1157
"void $classname$::CopyFrom(const ::google::protobuf::Message& from) {\n",
1158
"classname", classname_);
1162
"if (&from == this) return;\n"
1164
"MergeFrom(from);\n");
1167
printer->Print("}\n\n");
1104
1170
// Generate the class-specific CopyFrom.
1105
1171
printer->Print(
1119
1185
void MessageGenerator::
1120
1186
GenerateMergeFromCodedStream(io::Printer* printer) {
1187
if (descriptor_->options().message_set_wire_format()) {
1188
// Special-case MessageSet.
1190
"bool $classname$::MergePartialFromCodedStream(\n"
1191
" ::google::protobuf::io::CodedInputStream* input) {\n"
1192
" return _extensions_.ParseMessageSet(input, default_instance_,\n"
1193
" mutable_unknown_fields());\n"
1195
"classname", classname_);
1121
1199
printer->Print(
1122
1200
"bool $classname$::MergePartialFromCodedStream(\n"
1123
1201
" ::google::protobuf::io::CodedInputStream* input) {\n"
1154
1232
printer->Print(
1155
1233
"case $number$: {\n"
1156
" if (::google::protobuf::internal::WireFormat::GetTagWireType(tag) !=\n"
1157
" ::google::protobuf::internal::WireFormat::WIRETYPE_$wiretype$) {\n"
1234
" if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) !=\n"
1235
" ::google::protobuf::internal::WireFormatLite::WIRETYPE_$wiretype$) {\n"
1158
1236
" goto handle_uninterpreted;\n"
1160
1238
"number", SimpleItoa(field->number()),
1210
1288
// Is this an end-group tag? If so, this must be the end of the message.
1211
1289
printer->Print(
1212
"if (::google::protobuf::internal::WireFormat::GetTagWireType(tag) ==\n"
1213
" ::google::protobuf::internal::WireFormat::WIRETYPE_END_GROUP) {\n"
1290
"if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) ==\n"
1291
" ::google::protobuf::internal::WireFormatLite::WIRETYPE_END_GROUP) {\n"
1214
1292
" return true;\n"
1223
1301
descriptor_->extension_range(i);
1224
1302
if (i > 0) printer->Print(" ||\n ");
1226
uint32 start_tag = WireFormat::MakeTag(
1227
range->start, static_cast<WireFormat::WireType>(0));
1228
uint32 end_tag = WireFormat::MakeTag(
1229
range->end, static_cast<WireFormat::WireType>(0));
1304
uint32 start_tag = WireFormatLite::MakeTag(
1305
range->start, static_cast<WireFormatLite::WireType>(0));
1306
uint32 end_tag = WireFormatLite::MakeTag(
1307
range->end, static_cast<WireFormatLite::WireType>(0));
1231
1309
if (range->end > FieldDescriptor::kMaxNumber) {
1232
1310
printer->Print(
1239
1317
"end", SimpleItoa(end_tag));
1242
printer->Print(") {\n"
1243
" DO_(_extensions_.ParseField(tag, input, default_instance_,\n"
1244
" mutable_unknown_fields()));\n"
1320
printer->Print(") {\n");
1321
if (HasUnknownFields(descriptor_->file())) {
1323
" DO_(_extensions_.ParseField(tag, input, default_instance_,\n"
1324
" mutable_unknown_fields()));\n");
1327
" DO_(_extensions_.ParseField(tag, input, default_instance_));\n");
1249
1334
// We really don't recognize this tag. Skip it.
1251
"DO_(::google::protobuf::internal::WireFormat::SkipField(\n"
1252
" input, tag, mutable_unknown_fields()));\n");
1335
if (HasUnknownFields(descriptor_->file())) {
1337
"DO_(::google::protobuf::internal::WireFormat::SkipField(\n"
1338
" input, tag, mutable_unknown_fields()));\n");
1341
"DO_(::google::protobuf::internal::WireFormatLite::SkipField(input, tag));\n");
1254
1344
if (descriptor_->field_count() > 0) {
1255
1345
printer->Print("break;\n");
1315
1405
void MessageGenerator::
1316
1406
GenerateSerializeWithCachedSizes(io::Printer* printer) {
1407
if (descriptor_->options().message_set_wire_format()) {
1408
// Special-case MessageSet.
1410
"void $classname$::SerializeWithCachedSizes(\n"
1411
" ::google::protobuf::io::CodedOutputStream* output) const {\n"
1412
" _extensions_.SerializeMessageSetWithCachedSizes(output);\n",
1413
"classname", classname_);
1414
if (HasUnknownFields(descriptor_->file())) {
1416
" ::google::protobuf::internal::WireFormat::SerializeUnknownMessageSetItems(\n"
1417
" unknown_fields(), output);\n");
1317
1424
printer->Print(
1318
1425
"void $classname$::SerializeWithCachedSizes(\n"
1319
1426
" ::google::protobuf::io::CodedOutputStream* output) const {\n",
1320
1427
"classname", classname_);
1321
1428
printer->Indent();
1324
"::google::protobuf::uint8* raw_buffer = "
1325
"output->GetDirectBufferForNBytesAndAdvance(_cached_size_);\n"
1326
"if (raw_buffer != NULL) {\n"
1327
" $classname$::SerializeWithCachedSizesToArray(raw_buffer);\n"
1331
"classname", classname_);
1430
if (HasFastArraySerialization(descriptor_->file())) {
1432
"::google::protobuf::uint8* raw_buffer = "
1433
"output->GetDirectBufferForNBytesAndAdvance(_cached_size_);\n"
1434
"if (raw_buffer != NULL) {\n"
1435
" $classname$::SerializeWithCachedSizesToArray(raw_buffer);\n"
1439
"classname", classname_);
1332
1442
GenerateSerializeWithCachedSizesBody(printer, false);
1334
1444
printer->Outdent();
1339
1449
void MessageGenerator::
1340
1450
GenerateSerializeWithCachedSizesToArray(io::Printer* printer) {
1451
if (descriptor_->options().message_set_wire_format()) {
1452
// Special-case MessageSet.
1454
"::google::protobuf::uint8* $classname$::SerializeWithCachedSizesToArray(\n"
1455
" ::google::protobuf::uint8* target) const {\n"
1457
" _extensions_.SerializeMessageSetWithCachedSizesToArray(target);\n",
1458
"classname", classname_);
1459
if (HasUnknownFields(descriptor_->file())) {
1461
" target = ::google::protobuf::internal::WireFormat::\n"
1462
" SerializeUnknownMessageSetItemsToArray(\n"
1463
" unknown_fields(), target);\n");
1341
1471
printer->Print(
1342
1472
"::google::protobuf::uint8* $classname$::SerializeWithCachedSizesToArray(\n"
1343
1473
" ::google::protobuf::uint8* target) const {\n",
1387
printer->Print("if (!unknown_fields().empty()) {\n");
1392
"::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray(\n"
1393
" unknown_fields(), target);\n");
1396
"::google::protobuf::internal::WireFormat::SerializeUnknownFields(\n"
1397
" unknown_fields(), output);\n");
1517
if (HasUnknownFields(descriptor_->file())) {
1518
printer->Print("if (!unknown_fields().empty()) {\n");
1523
"::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray(\n"
1524
" unknown_fields(), target);\n");
1527
"::google::protobuf::internal::WireFormat::SerializeUnknownFields(\n"
1528
" unknown_fields(), output);\n");
1405
1537
void MessageGenerator::
1406
1538
GenerateByteSize(io::Printer* printer) {
1539
if (descriptor_->options().message_set_wire_format()) {
1540
// Special-case MessageSet.
1542
"int $classname$::ByteSize() const {\n"
1543
" int total_size = _extensions_.MessageSetByteSize();\n",
1544
"classname", classname_);
1545
if (HasUnknownFields(descriptor_->file())) {
1547
" total_size += ::google::protobuf::internal::WireFormat::\n"
1548
" ComputeUnknownMessageSetItemsSize(unknown_fields());\n");
1551
" _cached_size_ = total_size;\n"
1552
" return total_size;\n"
1407
1557
printer->Print(
1408
1558
"int $classname$::ByteSize() const {\n",
1409
1559
"classname", classname_);
1476
printer->Print("if (!unknown_fields().empty()) {\n");
1480
" ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize(\n"
1481
" unknown_fields());\n");
1483
printer->Print("}\n");
1626
if (HasUnknownFields(descriptor_->file())) {
1627
printer->Print("if (!unknown_fields().empty()) {\n");
1631
" ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize(\n"
1632
" unknown_fields());\n");
1634
printer->Print("}\n");
1485
1637
// We update _cached_size_ even though this is a const method. In theory,
1486
1638
// this is not thread-compatible, because concurrent writes have undefined