Initial import of v2.0.0beta
[protobuf.git] / src / google / protobuf / compiler / cpp / cpp_message.cc
blob002b0ad299b1c9095d7b6d21d94ef53379a9fa27
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc.
3 // http://code.google.com/p/protobuf/
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
17 // Author: kenton@google.com (Kenton Varda)
18 // Based on original Protocol Buffers design by
19 // Sanjay Ghemawat, Jeff Dean, and others.
21 #include <algorithm>
22 #include <google/protobuf/stubs/hash.h>
23 #include <google/protobuf/compiler/cpp/cpp_message.h>
24 #include <google/protobuf/compiler/cpp/cpp_enum.h>
25 #include <google/protobuf/compiler/cpp/cpp_extension.h>
26 #include <google/protobuf/compiler/cpp/cpp_helpers.h>
27 #include <google/protobuf/stubs/strutil.h>
28 #include <google/protobuf/io/printer.h>
29 #include <google/protobuf/io/coded_stream.h>
30 #include <google/protobuf/wire_format.h>
31 #include <google/protobuf/descriptor.pb.h>
33 namespace google {
34 namespace protobuf {
35 namespace compiler {
36 namespace cpp {
38 using internal::WireFormat;
40 namespace {
42 void PrintFieldComment(io::Printer* printer, const FieldDescriptor* field) {
43 // Print the field's proto-syntax definition as a comment. We don't want to
44 // print group bodies so we cut off after the first line.
45 string def = field->DebugString();
46 printer->Print("// $def$\n",
47 "def", def.substr(0, def.find_first_of('\n')));
50 struct FieldOrderingByNumber {
51 inline bool operator()(const FieldDescriptor* a,
52 const FieldDescriptor* b) const {
53 return a->number() < b->number();
57 const char* kWireTypeNames[] = {
58 "VARINT",
59 "FIXED64",
60 "LENGTH_DELIMITED",
61 "START_GROUP",
62 "END_GROUP",
63 "FIXED32",
66 // Sort the fields of the given Descriptor by number into a new[]'d array
67 // and return it.
68 const FieldDescriptor** SortFieldsByNumber(const Descriptor* descriptor) {
69 const FieldDescriptor** fields =
70 new const FieldDescriptor*[descriptor->field_count()];
71 for (int i = 0; i < descriptor->field_count(); i++) {
72 fields[i] = descriptor->field(i);
74 sort(fields, fields + descriptor->field_count(),
75 FieldOrderingByNumber());
76 return fields;
79 // Functor for sorting extension ranges by their "start" field number.
80 struct ExtensionRangeSorter {
81 bool operator()(const Descriptor::ExtensionRange* left,
82 const Descriptor::ExtensionRange* right) const {
83 return left->start < right->start;
87 // Returns true if the message type has any required fields. If it doesn't,
88 // we can optimize out calls to its IsInitialized() method.
90 // already_seen is used to avoid checking the same type multiple times
91 // (and also to protect against recursion).
92 static bool HasRequiredFields(
93 const Descriptor* type,
94 hash_set<const Descriptor*>* already_seen) {
95 if (already_seen->count(type) > 0) {
96 // Since the first occurrence of a required field causes the whole
97 // function to return true, we can assume that if the type is already
98 // in the cache it didn't have any required fields.
99 return false;
101 already_seen->insert(type);
103 // If the type has extensions, an extension with message type could contain
104 // required fields, so we have to be conservative and assume such an
105 // extension exists.
106 if (type->extension_range_count() > 0) return true;
108 for (int i = 0; i < type->field_count(); i++) {
109 const FieldDescriptor* field = type->field(i);
110 if (field->is_required()) {
111 return true;
113 if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) {
114 if (HasRequiredFields(field->message_type(), already_seen)) {
115 return true;
120 return false;
123 static bool HasRequiredFields(const Descriptor* type) {
124 hash_set<const Descriptor*> already_seen;
125 return HasRequiredFields(type, &already_seen);
130 // ===================================================================
132 MessageGenerator::MessageGenerator(const Descriptor* descriptor,
133 const string& dllexport_decl)
134 : descriptor_(descriptor),
135 classname_(ClassName(descriptor, false)),
136 dllexport_decl_(dllexport_decl),
137 field_generators_(descriptor),
138 nested_generators_(new scoped_ptr<MessageGenerator>[
139 descriptor->nested_type_count()]),
140 enum_generators_(new scoped_ptr<EnumGenerator>[
141 descriptor->enum_type_count()]),
142 extension_generators_(new scoped_ptr<ExtensionGenerator>[
143 descriptor->extension_count()]) {
145 for (int i = 0; i < descriptor->nested_type_count(); i++) {
146 nested_generators_[i].reset(
147 new MessageGenerator(descriptor->nested_type(i), dllexport_decl));
150 for (int i = 0; i < descriptor->enum_type_count(); i++) {
151 enum_generators_[i].reset(
152 new EnumGenerator(descriptor->enum_type(i), dllexport_decl));
155 for (int i = 0; i < descriptor->extension_count(); i++) {
156 extension_generators_[i].reset(
157 new ExtensionGenerator(descriptor->extension(i), dllexport_decl));
161 MessageGenerator::~MessageGenerator() {}
163 void MessageGenerator::
164 GenerateForwardDeclaration(io::Printer* printer) {
165 printer->Print("class $classname$;\n",
166 "classname", classname_);
168 for (int i = 0; i < descriptor_->nested_type_count(); i++) {
169 nested_generators_[i]->GenerateForwardDeclaration(printer);
173 void MessageGenerator::
174 GenerateEnumDefinitions(io::Printer* printer) {
175 for (int i = 0; i < descriptor_->nested_type_count(); i++) {
176 nested_generators_[i]->GenerateEnumDefinitions(printer);
179 for (int i = 0; i < descriptor_->enum_type_count(); i++) {
180 enum_generators_[i]->GenerateDefinition(printer);
184 void MessageGenerator::
185 GenerateFieldAccessorDeclarations(io::Printer* printer) {
186 for (int i = 0; i < descriptor_->field_count(); i++) {
187 const FieldDescriptor* field = descriptor_->field(i);
189 PrintFieldComment(printer, field);
191 map<string, string> vars;
192 vars["name"] = FieldName(field);
194 if (field->is_repeated()) {
195 printer->Print(vars, "inline int $name$_size() const;\n");
196 } else {
197 printer->Print(vars, "inline bool has_$name$() const;\n");
200 printer->Print(vars, "inline void clear_$name$();\n");
202 // Generate type-specific accessor declarations.
203 field_generators_.get(field).GenerateAccessorDeclarations(printer);
205 printer->Print("\n");
208 if (descriptor_->extension_range_count() > 0) {
209 // Generate accessors for extensions.
211 // Normally I'd generate prototypes here and generate the actual
212 // definitions of these methods in GenerateFieldAccessorDefinitions, but
213 // the prototypes for these silly methods are so absurdly complicated that
214 // it meant way too much repitition.
216 // We use "_proto_TypeTraits" as a type name below because "TypeTraits"
217 // causes problems if the class has a nested message or enum type with that
218 // name and "_TypeTraits" is technically reserved for the C++ library since
219 // it starts with an underscore followed by a capital letter.
220 printer->Print(
221 // Has, Size, Clear
222 "template <typename _proto_TypeTraits>\n"
223 "inline bool HasExtension(\n"
224 " const ::google::protobuf::internal::ExtensionIdentifier<\n"
225 " $classname$, _proto_TypeTraits>& id) const {\n"
226 " return _extensions_.Has(id.number());\n"
227 "}\n"
228 "\n"
229 "template <typename _proto_TypeTraits>\n"
230 "inline void ClearExtension(\n"
231 " const ::google::protobuf::internal::ExtensionIdentifier<\n"
232 " $classname$, _proto_TypeTraits>& id) {\n"
233 " _extensions_.ClearExtension(id.number());\n"
234 "}\n"
235 "\n"
236 "template <typename _proto_TypeTraits>\n"
237 "inline int ExtensionSize(\n"
238 " const ::google::protobuf::internal::ExtensionIdentifier<\n"
239 " $classname$, _proto_TypeTraits>& id) const {\n"
240 " return _extensions_.ExtensionSize(id.number());\n"
241 "}\n"
242 "\n"
244 // Singular accessors
245 "template <typename _proto_TypeTraits>\n"
246 "inline typename _proto_TypeTraits::ConstType GetExtension(\n"
247 " const ::google::protobuf::internal::ExtensionIdentifier<\n"
248 " $classname$, _proto_TypeTraits>& id) const {\n"
249 " return _proto_TypeTraits::Get(id.number(), _extensions_);\n"
250 "}\n"
251 "\n"
252 "template <typename _proto_TypeTraits>\n"
253 "inline typename _proto_TypeTraits::MutableType MutableExtension(\n"
254 " const ::google::protobuf::internal::ExtensionIdentifier<\n"
255 " $classname$, _proto_TypeTraits>& id) {\n"
256 " return _proto_TypeTraits::Mutable(id.number(), &_extensions_);\n"
257 "}\n"
258 "\n"
259 "template <typename _proto_TypeTraits>\n"
260 "inline void SetExtension(\n"
261 " const ::google::protobuf::internal::ExtensionIdentifier<\n"
262 " $classname$, _proto_TypeTraits>& id,\n"
263 " typename _proto_TypeTraits::ConstType value) {\n"
264 " _proto_TypeTraits::Set(id.number(), value, &_extensions_);\n"
265 "}\n"
266 "\n"
268 // Repeated accessors
269 "template <typename _proto_TypeTraits>\n"
270 "inline typename _proto_TypeTraits::ConstType GetExtension(\n"
271 " const ::google::protobuf::internal::ExtensionIdentifier<\n"
272 " $classname$, _proto_TypeTraits>& id,\n"
273 " int index) const {\n"
274 " return _proto_TypeTraits::Get(id.number(), _extensions_, index);\n"
275 "}\n"
276 "\n"
277 "template <typename _proto_TypeTraits>\n"
278 "inline typename _proto_TypeTraits::MutableType MutableExtension(\n"
279 " const ::google::protobuf::internal::ExtensionIdentifier<\n"
280 " $classname$, _proto_TypeTraits>& id,\n"
281 " int index) {\n"
282 " return _proto_TypeTraits::Mutable(id.number(),index,&_extensions_);\n"
283 "}\n"
284 "\n"
285 "template <typename _proto_TypeTraits>\n"
286 "inline void SetExtension(\n"
287 " const ::google::protobuf::internal::ExtensionIdentifier<\n"
288 " $classname$, _proto_TypeTraits>& id,\n"
289 " int index, typename _proto_TypeTraits::ConstType value) {\n"
290 " _proto_TypeTraits::Set(id.number(), index, value, &_extensions_);\n"
291 "}\n"
292 "\n"
293 "template <typename _proto_TypeTraits>\n"
294 "inline typename _proto_TypeTraits::MutableType AddExtension(\n"
295 " const ::google::protobuf::internal::ExtensionIdentifier<\n"
296 " $classname$, _proto_TypeTraits>& id) {\n"
297 " return _proto_TypeTraits::Add(id.number(), &_extensions_);\n"
298 "}\n"
299 "\n"
300 "template <typename _proto_TypeTraits>\n"
301 "inline void AddExtension(\n"
302 " const ::google::protobuf::internal::ExtensionIdentifier<\n"
303 " $classname$, _proto_TypeTraits>& id,\n"
304 " typename _proto_TypeTraits::ConstType value) {\n"
305 " _proto_TypeTraits::Add(id.number(), value, &_extensions_);\n"
306 "}\n",
307 "classname", classname_);
311 void MessageGenerator::
312 GenerateFieldAccessorDefinitions(io::Printer* printer) {
313 printer->Print("// $classname$\n\n", "classname", classname_);
315 for (int i = 0; i < descriptor_->field_count(); i++) {
316 const FieldDescriptor* field = descriptor_->field(i);
318 PrintFieldComment(printer, field);
320 map<string, string> vars;
321 vars["name"] = FieldName(field);
322 vars["index"] = SimpleItoa(field->index());
323 vars["classname"] = classname_;
325 // Generate has_$name$() or $name$_size().
326 if (field->is_repeated()) {
327 printer->Print(vars,
328 "inline int $classname$::$name$_size() const {\n"
329 " return $name$_.size();\n"
330 "}\n");
331 } else {
332 // Singular field.
333 printer->Print(vars,
334 "inline bool $classname$::has_$name$() const {\n"
335 " return _has_bit($index$);\n"
336 "}\n");
339 // Generate clear_$name$()
340 printer->Print(vars,
341 "inline void $classname$::clear_$name$() {\n");
343 printer->Indent();
344 field_generators_.get(field).GenerateClearingCode(printer);
345 printer->Outdent();
347 if (!field->is_repeated()) {
348 printer->Print(vars, " _clear_bit($index$);\n");
351 printer->Print("}\n");
353 // Generate type-specific accessors.
354 field_generators_.get(field).GenerateInlineAccessorDefinitions(printer);
356 printer->Print("\n");
360 void MessageGenerator::
361 GenerateClassDefinition(io::Printer* printer) {
362 for (int i = 0; i < descriptor_->nested_type_count(); i++) {
363 nested_generators_[i]->GenerateClassDefinition(printer);
364 printer->Print("\n");
365 printer->Print(kThinSeparator);
366 printer->Print("\n");
369 map<string, string> vars;
370 vars["classname"] = classname_;
371 vars["field_count"] = SimpleItoa(descriptor_->field_count());
372 if (dllexport_decl_.empty()) {
373 vars["dllexport"] = "";
374 } else {
375 vars["dllexport"] = dllexport_decl_ + " ";
378 printer->Print(vars,
379 "class $dllexport$$classname$ : public ::google::protobuf::Message {\n"
380 " public:\n");
381 printer->Indent();
383 printer->Print(vars,
384 "$classname$();\n"
385 "virtual ~$classname$();\n"
386 "\n"
387 "$classname$(const $classname$& from);\n"
388 "\n"
389 "inline $classname$& operator=(const $classname$& from) {\n"
390 " CopyFrom(from);\n"
391 " return *this;\n"
392 "}\n"
393 "\n"
394 "inline static const $classname$& default_instance() {\n"
395 " return default_instance_;\n"
396 "}\n"
397 "\n"
398 "inline const ::google::protobuf::UnknownFieldSet& unknown_fields() const {\n"
399 " return _reflection_.unknown_fields();\n"
400 "}\n"
401 "\n"
402 "inline ::google::protobuf::UnknownFieldSet* mutable_unknown_fields() {\n"
403 " return _reflection_.mutable_unknown_fields();\n"
404 "}\n"
405 "\n"
406 "static const ::google::protobuf::Descriptor* descriptor();\n"
407 "\n"
408 "// implements Message ----------------------------------------------\n"
409 "\n"
410 "$classname$* New() const;\n");
412 if (descriptor_->file()->options().optimize_for() == FileOptions::SPEED) {
413 printer->Print(vars,
414 "void CopyFrom(const ::google::protobuf::Message& from);\n"
415 "void MergeFrom(const ::google::protobuf::Message& from);\n"
416 "void CopyFrom(const $classname$& from);\n"
417 "void MergeFrom(const $classname$& from);\n"
418 "void Clear();\n"
419 "bool IsInitialized() const;\n"
420 "int ByteSize() const;\n"
421 "\n"
422 "bool MergePartialFromCodedStream(\n"
423 " ::google::protobuf::io::CodedInputStream* input);\n"
424 "bool SerializeWithCachedSizes(\n"
425 " ::google::protobuf::io::CodedOutputStream* output) const;\n");
428 printer->Print(vars,
429 "int GetCachedSize() const { return _cached_size_; }\n"
430 "private:\n"
431 "void SetCachedSize(int size) const { _cached_size_ = size; }\n"
432 "public:\n"
433 "\n"
434 "const ::google::protobuf::Descriptor* GetDescriptor() const;\n"
435 "const ::google::protobuf::Message::Reflection* GetReflection() const;\n"
436 "::google::protobuf::Message::Reflection* GetReflection();\n"
437 "\n"
438 "// nested types ----------------------------------------------------\n"
439 "\n");
441 // Import all nested message classes into this class's scope with typedefs.
442 for (int i = 0; i < descriptor_->nested_type_count(); i++) {
443 const Descriptor* nested_type = descriptor_->nested_type(i);
444 printer->Print("typedef $nested_full_name$ $nested_name$;\n",
445 "nested_name", nested_type->name(),
446 "nested_full_name", ClassName(nested_type, false));
449 if (descriptor_->nested_type_count() > 0) {
450 printer->Print("\n");
453 // Import all nested enums and their values into this class's scope with
454 // typedefs and constants.
455 for (int i = 0; i < descriptor_->enum_type_count(); i++) {
456 enum_generators_[i]->GenerateSymbolImports(printer);
457 printer->Print("\n");
460 printer->Print(
461 "// accessors -------------------------------------------------------\n"
462 "\n");
464 // Generate accessor methods for all fields.
465 GenerateFieldAccessorDeclarations(printer);
467 // Declare extension identifiers.
468 for (int i = 0; i < descriptor_->extension_count(); i++) {
469 extension_generators_[i]->GenerateDeclaration(printer);
472 // Generate private members for fields.
473 printer->Outdent();
474 printer->Print(" private:\n");
475 printer->Indent();
477 if (descriptor_->extension_range_count() > 0) {
478 printer->Print(
479 "::google::protobuf::internal::ExtensionSet _extensions_;\n");
482 // TODO(kenton): Make _cached_size_ an atomic<int> when C++ supports it.
483 printer->Print(
484 "::google::protobuf::internal::GeneratedMessageReflection _reflection_;\n"
485 "mutable int _cached_size_;\n"
486 "\n");
487 for (int i = 0; i < descriptor_->field_count(); i++) {
488 field_generators_.get(descriptor_->field(i))
489 .GeneratePrivateMembers(printer);
492 // Generate offsets and _has_bits_ boilerplate.
493 printer->Print(vars,
494 "\n"
495 "static const $classname$ default_instance_;\n");
497 if (descriptor_->field_count() > 0) {
498 printer->Print(vars,
499 "static const int _offsets_[$field_count$];\n"
500 "\n"
501 "::google::protobuf::uint32 _has_bits_[($field_count$ + 31) / 32];\n");
502 } else {
503 // Zero-size arrays aren't technically allowed, and MSVC in particular
504 // doesn't like them. We still need to declare these arrays to make
505 // other code compile. Since this is an uncommon case, we'll just declare
506 // them with size 1 and waste some space. Oh well.
507 printer->Print(
508 "static const int _offsets_[1];\n"
509 "\n"
510 "::google::protobuf::uint32 _has_bits_[1];\n");
513 printer->Print(
514 "\n"
515 "// WHY DOES & HAVE LOWER PRECEDENCE THAN != !?\n"
516 "inline bool _has_bit(int index) const {\n"
517 " return (_has_bits_[index / 32] & (1u << (index % 32))) != 0;\n"
518 "}\n"
519 "inline void _set_bit(int index) {\n"
520 " _has_bits_[index / 32] |= (1u << (index % 32));\n"
521 "}\n"
522 "inline void _clear_bit(int index) {\n"
523 " _has_bits_[index / 32] &= ~(1u << (index % 32));\n"
524 "}\n");
526 printer->Outdent();
527 printer->Print(vars, "};");
530 void MessageGenerator::
531 GenerateInlineMethods(io::Printer* printer) {
532 for (int i = 0; i < descriptor_->nested_type_count(); i++) {
533 nested_generators_[i]->GenerateInlineMethods(printer);
534 printer->Print(kThinSeparator);
535 printer->Print("\n");
538 GenerateFieldAccessorDefinitions(printer);
541 void MessageGenerator::
542 GenerateDescriptorDeclarations(io::Printer* printer) {
543 printer->Print("const ::google::protobuf::Descriptor* $name$_descriptor_ = NULL;\n",
544 "name", classname_);
546 for (int i = 0; i < descriptor_->nested_type_count(); i++) {
547 nested_generators_[i]->GenerateDescriptorDeclarations(printer);
550 for (int i = 0; i < descriptor_->enum_type_count(); i++) {
551 printer->Print(
552 "const ::google::protobuf::EnumDescriptor* $name$_descriptor_ = NULL;\n",
553 "name", ClassName(descriptor_->enum_type(i), false));
557 void MessageGenerator::
558 GenerateDescriptorInitializer(io::Printer* printer, int index) {
559 // TODO(kenton): Passing the index to this method is redundant; just use
560 // descriptor_->index() instead.
561 map<string, string> vars;
562 vars["classname"] = classname_;
563 vars["index"] = SimpleItoa(index);
565 if (descriptor_->containing_type() == NULL) {
566 printer->Print(vars,
567 "$classname$_descriptor_ = file->message_type($index$);\n");
568 } else {
569 vars["parent"] = ClassName(descriptor_->containing_type(), false);
570 printer->Print(vars,
571 "$classname$_descriptor_ = "
572 "$parent$_descriptor_->nested_type($index$);\n");
575 for (int i = 0; i < descriptor_->nested_type_count(); i++) {
576 nested_generators_[i]->GenerateDescriptorInitializer(printer, i);
579 for (int i = 0; i < descriptor_->enum_type_count(); i++) {
580 enum_generators_[i]->GenerateDescriptorInitializer(printer, i);
583 // Register this message type with the message factory.
584 printer->Print(vars,
585 "::google::protobuf::MessageFactory::InternalRegisterGeneratedMessage(\n"
586 " $classname$_descriptor_, &$classname$::default_instance());\n");
589 void MessageGenerator::
590 GenerateClassMethods(io::Printer* printer) {
591 for (int i = 0; i < descriptor_->enum_type_count(); i++) {
592 enum_generators_[i]->GenerateMethods(printer);
595 for (int i = 0; i < descriptor_->nested_type_count(); i++) {
596 nested_generators_[i]->GenerateClassMethods(printer);
597 printer->Print("\n");
598 printer->Print(kThinSeparator);
599 printer->Print("\n");
602 printer->Print(
603 "const $classname$ $classname$::default_instance_;\n"
604 "\n",
605 "classname", classname_);
607 // Generate non-inline field definitions.
608 for (int i = 0; i < descriptor_->field_count(); i++) {
609 field_generators_.get(descriptor_->field(i))
610 .GenerateNonInlineAccessorDefinitions(printer);
611 printer->Print("\n");
614 // Define extension identifiers.
615 for (int i = 0; i < descriptor_->extension_count(); i++) {
616 extension_generators_[i]->GenerateDefinition(printer);
619 GenerateOffsets(printer);
620 printer->Print("\n");
622 GenerateStructors(printer);
623 printer->Print("\n");
625 if (descriptor_->file()->options().optimize_for() == FileOptions::SPEED) {
626 GenerateClear(printer);
627 printer->Print("\n");
629 GenerateMergeFromCodedStream(printer);
630 printer->Print("\n");
632 GenerateSerializeWithCachedSizes(printer);
633 printer->Print("\n");
635 GenerateByteSize(printer);
636 printer->Print("\n");
638 GenerateMergeFrom(printer);
639 printer->Print("\n");
641 GenerateCopyFrom(printer);
642 printer->Print("\n");
644 GenerateIsInitialized(printer);
645 printer->Print("\n");
648 printer->Print(
649 "const ::google::protobuf::Descriptor* $classname$::GetDescriptor() const {\n"
650 " return descriptor();\n"
651 "}\n"
652 "\n"
653 "const ::google::protobuf::Message::Reflection*\n"
654 "$classname$::GetReflection() const {\n"
655 " return &_reflection_;\n"
656 "}\n"
657 "\n"
658 "::google::protobuf::Message::Reflection* $classname$::GetReflection() {\n"
659 " return &_reflection_;\n"
660 "}\n",
661 "classname", classname_);
664 void MessageGenerator::
665 GenerateOffsets(io::Printer* printer) {
666 printer->Print(
667 "const int $classname$::_offsets_[$field_count$] = {\n",
668 "classname", classname_,
669 "field_count", SimpleItoa(max(1, descriptor_->field_count())));
670 printer->Indent();
672 for (int i = 0; i < descriptor_->field_count(); i++) {
673 const FieldDescriptor* field = descriptor_->field(i);
674 printer->Print(
675 "GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET($classname$, $name$_),\n",
676 "classname", classname_,
677 "name", FieldName(field));
680 printer->Outdent();
681 printer->Print("};\n");
684 void MessageGenerator::
685 GenerateInitializerList(io::Printer* printer) {
686 printer->Indent();
687 printer->Indent();
689 bool has_extensions = descriptor_->extension_range_count() > 0;
690 if (has_extensions) {
691 printer->Print(
692 "_extensions_(descriptor(),\n"
693 " ::google::protobuf::DescriptorPool::generated_pool(),\n"
694 " ::google::protobuf::MessageFactory::generated_factory()),\n");
697 printer->Print(
698 "_reflection_(descriptor(),\n"
699 " this, &default_instance_,\n"
700 " _offsets_, _has_bits_, $extensions$),\n"
701 "_cached_size_(0)",
702 "extensions", has_extensions ? "&_extensions_" : "NULL");
704 // Write the initializers for each field.
705 for (int i = 0; i < descriptor_->field_count(); i++) {
706 field_generators_.get(descriptor_->field(i))
707 .GenerateInitializer(printer);
710 printer->Outdent();
711 printer->Outdent();
714 void MessageGenerator::
715 GenerateStructors(io::Printer* printer) {
716 // Generate the default constructor.
717 printer->Print(
718 "$classname$::$classname$()\n"
719 " : ",
720 "classname", classname_);
721 GenerateInitializerList(printer);
722 printer->Print(" {\n"
723 " ::memset(_has_bits_, 0, sizeof(_has_bits_));\n"
724 " if (this == &default_instance_) {\n");
726 // The default instance needs all of its embedded message pointers
727 // cross-linked to other default instances.
728 // TODO(kenton): Maybe all message fields (even for non-default messages)
729 // should be initialized to point at default instances rather than NULL?
730 for (int i = 0; i < descriptor_->field_count(); i++) {
731 const FieldDescriptor* field = descriptor_->field(i);
733 if (!field->is_repeated() &&
734 field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) {
735 printer->Print(
736 " $name$_ = const_cast< $type$*>(&$type$::default_instance());\n",
737 "name", FieldName(field),
738 "type", ClassName(field->message_type(), true));
741 printer->Print(
742 " }\n"
743 "}\n"
744 "\n");
746 // Generate the copy constructor.
747 printer->Print(
748 "$classname$::$classname$(const $classname$& from)\n"
749 " : ",
750 "classname", classname_);
751 GenerateInitializerList(printer);
752 printer->Print(" {\n"
753 " ::memset(_has_bits_, 0, sizeof(_has_bits_));\n"
754 " MergeFrom(from);\n"
755 "}\n"
756 "\n");
758 // Generate the destructor.
759 printer->Print(
760 "$classname$::~$classname$() {\n",
761 "classname", classname_);
763 printer->Indent();
765 // Write the destructors for each field.
766 for (int i = 0; i < descriptor_->field_count(); i++) {
767 field_generators_.get(descriptor_->field(i))
768 .GenerateDestructorCode(printer);
771 printer->Print(
772 "if (this != &default_instance_) {\n");
774 // We need to delete all embedded messages.
775 // TODO(kenton): If we make unset messages point at default instances
776 // instead of NULL, then it would make sense to move this code into
777 // MessageFieldGenerator::GenerateDestructorCode().
778 for (int i = 0; i < descriptor_->field_count(); i++) {
779 const FieldDescriptor* field = descriptor_->field(i);
781 if (!field->is_repeated() &&
782 field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) {
783 printer->Print(" delete $name$_;\n",
784 "name", FieldName(field));
788 printer->Outdent();
790 printer->Print(
791 " }\n"
792 "}\n"
793 "\n"
794 "const ::google::protobuf::Descriptor* $classname$::descriptor() {\n"
795 " if ($classname$_descriptor_ == NULL) $builddescriptorsname$();\n"
796 " return $classname$_descriptor_;\n"
797 "}\n"
798 "\n"
799 "$classname$* $classname$::New() const {\n"
800 " return new $classname$;\n"
801 "}\n",
802 "classname", classname_,
803 "builddescriptorsname",
804 GlobalBuildDescriptorsName(descriptor_->file()->name()));
807 void MessageGenerator::
808 GenerateClear(io::Printer* printer) {
809 printer->Print("void $classname$::Clear() {\n",
810 "classname", classname_);
811 printer->Indent();
813 int last_index = -1;
815 if (descriptor_->extension_range_count() > 0) {
816 printer->Print("_extensions_.Clear();\n");
819 for (int i = 0; i < descriptor_->field_count(); i++) {
820 const FieldDescriptor* field = descriptor_->field(i);
822 if (!field->is_repeated()) {
823 map<string, string> vars;
824 vars["index"] = SimpleItoa(field->index());
826 // We can use the fact that _has_bits_ is a giant bitfield to our
827 // advantage: We can check up to 32 bits at a time for equality to
828 // zero, and skip the whole range if so. This can improve the speed
829 // of Clear() for messages which contain a very large number of
830 // optional fields of which only a few are used at a time. Here,
831 // we've chosen to check 8 bits at a time rather than 32.
832 if (i / 8 != last_index / 8 || last_index < 0) {
833 if (last_index >= 0) {
834 printer->Outdent();
835 printer->Print("}\n");
837 printer->Print(vars,
838 "if (_has_bits_[$index$ / 32] & (0xffu << ($index$ % 32))) {\n");
839 printer->Indent();
841 last_index = i;
843 // It's faster to just overwrite primitive types, but we should
844 // only clear strings and messages if they were set.
845 // TODO(kenton): Let the CppFieldGenerator decide this somehow.
846 bool should_check_bit =
847 field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE ||
848 field->cpp_type() == FieldDescriptor::CPPTYPE_STRING;
850 if (should_check_bit) {
851 printer->Print(vars, "if (_has_bit($index$)) {\n");
852 printer->Indent();
855 field_generators_.get(field).GenerateClearingCode(printer);
857 if (should_check_bit) {
858 printer->Outdent();
859 printer->Print("}\n");
864 if (last_index >= 0) {
865 printer->Outdent();
866 printer->Print("}\n");
869 // Repeated fields don't use _has_bits_ so we clear them in a separate
870 // pass.
871 for (int i = 0; i < descriptor_->field_count(); i++) {
872 const FieldDescriptor* field = descriptor_->field(i);
874 if (field->is_repeated()) {
875 field_generators_.get(field).GenerateClearingCode(printer);
879 printer->Print(
880 "::memset(_has_bits_, 0, sizeof(_has_bits_));\n"
881 "mutable_unknown_fields()->Clear();\n");
883 printer->Outdent();
884 printer->Print("}\n");
887 void MessageGenerator::
888 GenerateMergeFrom(io::Printer* printer) {
889 // Generate the generalized MergeFrom (aka that which takes in the Message
890 // base class as a parameter).
891 printer->Print(
892 "void $classname$::MergeFrom(const ::google::protobuf::Message& from) {\n"
893 " GOOGLE_CHECK_NE(&from, this);\n",
894 "classname", classname_);
895 printer->Indent();
897 if (descriptor_->field_count() > 0) {
898 // Cast the message to the proper type. If we find that the message is
899 // *not* of the proper type, we can still call Merge via the reflection
900 // system, as the GOOGLE_CHECK above ensured that we have the same descriptor
901 // for each message.
902 printer->Print(
903 "const $classname$* source =\n"
904 " ::google::protobuf::internal::dynamic_cast_if_available<const $classname$*>(\n"
905 " &from);\n"
906 "if (source == NULL) {\n"
907 " ::google::protobuf::internal::ReflectionOps::Merge(\n"
908 " descriptor(), *from.GetReflection(), &_reflection_);\n"
909 "} else {\n"
910 " MergeFrom(*source);\n"
911 "}\n",
912 "classname", classname_);
915 printer->Outdent();
916 printer->Print("}\n\n");
918 // Generate the class-specific MergeFrom, which avoids the GOOGLE_CHECK and cast.
919 printer->Print(
920 "void $classname$::MergeFrom(const $classname$& from) {\n"
921 " GOOGLE_CHECK_NE(&from, this);\n",
922 "classname", classname_);
923 printer->Indent();
925 // Merge Repeated fields. These fields do not require a
926 // check as we can simply iterate over them.
927 for (int i = 0; i < descriptor_->field_count(); ++i) {
928 const FieldDescriptor* field = descriptor_->field(i);
930 if (field->is_repeated()) {
931 field_generators_.get(field).GenerateMergingCode(printer);
935 // Merge Optional and Required fields (after a _has_bit check).
936 int last_index = -1;
938 for (int i = 0; i < descriptor_->field_count(); ++i) {
939 const FieldDescriptor* field = descriptor_->field(i);
941 if (!field->is_repeated()) {
942 map<string, string> vars;
943 vars["index"] = SimpleItoa(field->index());
945 // See above in GenerateClear for an explanation of this.
946 if (i / 8 != last_index / 8 || last_index < 0) {
947 if (last_index >= 0) {
948 printer->Outdent();
949 printer->Print("}\n");
951 printer->Print(vars,
952 "if (from._has_bits_[$index$ / 32] & (0xffu << ($index$ % 32))) {\n");
953 printer->Indent();
956 last_index = i;
958 printer->Print(vars,
959 "if (from._has_bit($index$)) {\n");
960 printer->Indent();
962 field_generators_.get(field).GenerateMergingCode(printer);
964 printer->Outdent();
965 printer->Print("}\n");
969 if (last_index >= 0) {
970 printer->Outdent();
971 printer->Print("}\n");
974 if (descriptor_->extension_range_count() > 0) {
975 printer->Print("_extensions_.MergeFrom(from._extensions_);\n");
978 printer->Print(
979 "mutable_unknown_fields()->MergeFrom(from.unknown_fields());\n");
981 printer->Outdent();
982 printer->Print("}\n");
985 void MessageGenerator::
986 GenerateCopyFrom(io::Printer* printer) {
987 // Generate the generalized CopyFrom (aka that which takes in the Message
988 // base class as a parameter).
989 printer->Print(
990 "void $classname$::CopyFrom(const ::google::protobuf::Message& from) {\n",
991 "classname", classname_);
992 printer->Indent();
994 printer->Print(
995 "if (&from == this) return;\n"
996 "Clear();\n"
997 "MergeFrom(from);\n");
999 printer->Outdent();
1000 printer->Print("}\n\n");
1002 // Generate the class-specific CopyFrom.
1003 printer->Print(
1004 "void $classname$::CopyFrom(const $classname$& from) {\n",
1005 "classname", classname_);
1006 printer->Indent();
1008 printer->Print(
1009 "if (&from == this) return;\n"
1010 "Clear();\n"
1011 "MergeFrom(from);\n");
1013 printer->Outdent();
1014 printer->Print("}\n");
1017 void MessageGenerator::
1018 GenerateMergeFromCodedStream(io::Printer* printer) {
1019 if (descriptor_->options().message_set_wire_format()) {
1020 // For message_set_wire_format, we don't generate a parser, for two
1021 // reasons:
1022 // - WireFormat already needs to special-case this, and we'd like to
1023 // avoid having multiple implementations of MessageSet wire format
1024 // lying around the code base.
1025 // - All fields are extensions, and extension parsing falls back to
1026 // reflection anyway, so it wouldn't be any faster.
1027 printer->Print(
1028 "bool $classname$::MergePartialFromCodedStream(\n"
1029 " ::google::protobuf::io::CodedInputStream* input) {\n"
1030 " return ::google::protobuf::internal::WireFormat::ParseAndMergePartial(\n"
1031 " descriptor(), input, &_reflection_);\n"
1032 "}\n",
1033 "classname", classname_);
1034 return;
1037 printer->Print(
1038 "bool $classname$::MergePartialFromCodedStream(\n"
1039 " ::google::protobuf::io::CodedInputStream* input) {\n"
1040 "#define DO_(EXPRESSION) if (!(EXPRESSION)) return false\n"
1041 " ::google::protobuf::uint32 tag;\n"
1042 " while ((tag = input->ReadTag()) != 0) {\n",
1043 "classname", classname_);
1045 printer->Indent();
1046 printer->Indent();
1048 if (descriptor_->field_count() > 0) {
1049 // We don't even want to print the switch() if we have no fields because
1050 // MSVC dislikes switch() statements that contain only a default value.
1052 // Note: If we just switched on the tag rather than the field number, we
1053 // could avoid the need for the if() to check the wire type at the beginning
1054 // of each case. However, this is actually a bit slower in practice as it
1055 // creates a jump table that is 8x larger and sparser, and meanwhile the
1056 // if()s are highly predictable.
1057 printer->Print(
1058 "switch (::google::protobuf::internal::WireFormat::GetTagFieldNumber(tag)) {\n");
1060 printer->Indent();
1062 scoped_array<const FieldDescriptor*> ordered_fields(
1063 SortFieldsByNumber(descriptor_));
1065 for (int i = 0; i < descriptor_->field_count(); i++) {
1066 const FieldDescriptor* field = ordered_fields[i];
1068 PrintFieldComment(printer, field);
1070 printer->Print(
1071 "case $number$: {\n"
1072 " if (::google::protobuf::internal::WireFormat::GetTagWireType(tag) !=\n"
1073 " ::google::protobuf::internal::WireFormat::WIRETYPE_$wiretype$) {\n"
1074 " goto handle_uninterpreted;\n"
1075 " }\n",
1076 "number", SimpleItoa(field->number()),
1077 "wiretype", kWireTypeNames[
1078 WireFormat::WireTypeForFieldType(field->type())]);
1080 if (i > 0 || field->is_repeated()) {
1081 printer->Print(
1082 " parse_$name$:\n",
1083 "name", field->name());
1086 printer->Indent();
1088 field_generators_.get(field).GenerateMergeFromCodedStream(printer);
1090 // switch() is slow since it can't be predicted well. Insert some if()s
1091 // here that attempt to predict the next tag.
1092 if (field->is_repeated()) {
1093 // Expect repeats of this field.
1094 printer->Print(
1095 "if (input->ExpectTag($tag$)) goto parse_$name$;\n",
1096 "tag", SimpleItoa(WireFormat::MakeTag(field)),
1097 "name", field->name());
1100 if (i + 1 < descriptor_->field_count()) {
1101 // Expect the next field in order.
1102 const FieldDescriptor* next_field = ordered_fields[i + 1];
1103 printer->Print(
1104 "if (input->ExpectTag($next_tag$)) goto parse_$next_name$;\n",
1105 "next_tag", SimpleItoa(WireFormat::MakeTag(next_field)),
1106 "next_name", next_field->name());
1107 } else {
1108 // Expect EOF.
1109 // TODO(kenton): Expect group end-tag?
1110 printer->Print(
1111 "if (input->ExpectAtEnd()) return true;\n");
1114 printer->Print(
1115 "break;\n");
1117 printer->Outdent();
1118 printer->Print("}\n\n");
1121 printer->Print(
1122 "default: {\n"
1123 "handle_uninterpreted:\n");
1124 printer->Indent();
1127 // Is this an end-group tag? If so, this must be the end of the message.
1128 printer->Print(
1129 "if (::google::protobuf::internal::WireFormat::GetTagWireType(tag) ==\n"
1130 " ::google::protobuf::internal::WireFormat::WIRETYPE_END_GROUP) {\n"
1131 " return true;\n"
1132 "}\n");
1134 // Handle extension ranges.
1135 if (descriptor_->extension_range_count() > 0) {
1136 printer->Print(
1137 "if (");
1138 for (int i = 0; i < descriptor_->extension_range_count(); i++) {
1139 const Descriptor::ExtensionRange* range =
1140 descriptor_->extension_range(i);
1141 if (i > 0) printer->Print(" &&\n ");
1143 uint32 start_tag = WireFormat::MakeTag(
1144 range->start, static_cast<WireFormat::WireType>(0));
1145 uint32 end_tag = WireFormat::MakeTag(
1146 range->end, static_cast<WireFormat::WireType>(0));
1148 if (range->end > FieldDescriptor::kMaxNumber) {
1149 printer->Print(
1150 "($start$u <= tag)",
1151 "start", SimpleItoa(start_tag));
1152 } else {
1153 printer->Print(
1154 "($start$u <= tag && tag < $end$u)",
1155 "start", SimpleItoa(start_tag),
1156 "end", SimpleItoa(end_tag));
1159 printer->Print(") {\n"
1160 " DO_(_extensions_.ParseField(tag, input, &_reflection_));\n"
1161 " continue;\n"
1162 "}\n");
1165 // We really don't recognize this tag. Skip it.
1166 printer->Print(
1167 "DO_(::google::protobuf::internal::WireFormat::SkipField(\n"
1168 " input, tag, mutable_unknown_fields()));\n");
1170 if (descriptor_->field_count() > 0) {
1171 printer->Print("break;\n");
1172 printer->Outdent();
1173 printer->Print("}\n"); // default:
1174 printer->Outdent();
1175 printer->Print("}\n"); // switch
1178 printer->Outdent();
1179 printer->Outdent();
1180 printer->Print(
1181 " }\n" // while
1182 " return true;\n"
1183 "#undef DO_\n"
1184 "}\n");
1187 void MessageGenerator::GenerateSerializeOneField(
1188 io::Printer* printer, const FieldDescriptor* field) {
1189 PrintFieldComment(printer, field);
1191 if (field->is_repeated()) {
1192 printer->Print(
1193 "for (int i = 0; i < $name$_.size(); i++) {\n",
1194 "name", FieldName(field));
1195 } else {
1196 printer->Print(
1197 "if (_has_bit($index$)) {\n",
1198 "index", SimpleItoa(field->index()));
1201 printer->Indent();
1203 field_generators_.get(field).GenerateSerializeWithCachedSizes(printer);
1205 printer->Outdent();
1206 printer->Print("}\n\n");
1209 void MessageGenerator::GenerateSerializeOneExtensionRange(
1210 io::Printer* printer, const Descriptor::ExtensionRange* range) {
1211 map<string, string> vars;
1212 vars["start"] = SimpleItoa(range->start);
1213 vars["end"] = SimpleItoa(range->end);
1214 printer->Print(vars,
1215 "// Extension range [$start$, $end$)\n"
1216 "DO_(_extensions_.SerializeWithCachedSizes(\n"
1217 " $start$, $end$, &_reflection_, output));\n\n");
1220 void MessageGenerator::
1221 GenerateSerializeWithCachedSizes(io::Printer* printer) {
1222 printer->Print(
1223 "bool $classname$::SerializeWithCachedSizes(\n"
1224 " ::google::protobuf::io::CodedOutputStream* output) const {\n"
1225 "#define DO_(EXPRESSION) if (!(EXPRESSION)) return false\n",
1226 "classname", classname_);
1227 printer->Indent();
1229 scoped_array<const FieldDescriptor*> ordered_fields(
1230 SortFieldsByNumber(descriptor_));
1232 vector<const Descriptor::ExtensionRange*> sorted_extensions;
1233 for (int i = 0; i < descriptor_->extension_range_count(); ++i) {
1234 sorted_extensions.push_back(descriptor_->extension_range(i));
1236 sort(sorted_extensions.begin(), sorted_extensions.end(),
1237 ExtensionRangeSorter());
1239 // Merge the fields and the extension ranges, both sorted by field number.
1240 int i, j;
1241 for (i = 0, j = 0;
1242 i < descriptor_->field_count() || j < sorted_extensions.size();
1244 if (i == descriptor_->field_count()) {
1245 GenerateSerializeOneExtensionRange(printer, sorted_extensions[j++]);
1246 } else if (j == sorted_extensions.size()) {
1247 GenerateSerializeOneField(printer, ordered_fields[i++]);
1248 } else if (ordered_fields[i]->number() < sorted_extensions[j]->start) {
1249 GenerateSerializeOneField(printer, ordered_fields[i++]);
1250 } else {
1251 GenerateSerializeOneExtensionRange(printer, sorted_extensions[j++]);
1255 printer->Print("if (!unknown_fields().empty()) {\n");
1256 printer->Indent();
1257 if (descriptor_->options().message_set_wire_format()) {
1258 printer->Print(
1259 "DO_(::google::protobuf::internal::WireFormat::SerializeUnknownMessageSetItems(\n"
1260 " unknown_fields(), output));\n");
1261 } else {
1262 printer->Print(
1263 "DO_(::google::protobuf::internal::WireFormat::SerializeUnknownFields(\n"
1264 " unknown_fields(), output));\n");
1266 printer->Outdent();
1267 printer->Print(
1268 "}\n"
1269 "return true;\n");
1271 printer->Outdent();
1272 printer->Print(
1273 "#undef DO_\n"
1274 "}\n");
1277 void MessageGenerator::
1278 GenerateByteSize(io::Printer* printer) {
1279 printer->Print(
1280 "int $classname$::ByteSize() const {\n",
1281 "classname", classname_);
1282 printer->Indent();
1283 printer->Print(
1284 "int total_size = 0;\n"
1285 "\n");
1287 int last_index = -1;
1289 for (int i = 0; i < descriptor_->field_count(); i++) {
1290 const FieldDescriptor* field = descriptor_->field(i);
1292 if (!field->is_repeated()) {
1293 // See above in GenerateClear for an explanation of this.
1294 // TODO(kenton): Share code? Unclear how to do so without
1295 // over-engineering.
1296 if ((i / 8) != (last_index / 8) ||
1297 last_index < 0) {
1298 if (last_index >= 0) {
1299 printer->Outdent();
1300 printer->Print("}\n");
1302 printer->Print(
1303 "if (_has_bits_[$index$ / 32] & (0xffu << ($index$ % 32))) {\n",
1304 "index", SimpleItoa(field->index()));
1305 printer->Indent();
1307 last_index = i;
1309 PrintFieldComment(printer, field);
1311 printer->Print(
1312 "if (has_$name$()) {\n",
1313 "name", FieldName(field));
1314 printer->Indent();
1316 field_generators_.get(field).GenerateByteSize(printer);
1318 printer->Outdent();
1319 printer->Print(
1320 "}\n"
1321 "\n");
1325 if (last_index >= 0) {
1326 printer->Outdent();
1327 printer->Print("}\n");
1330 // Repeated fields don't use _has_bits_ so we count them in a separate
1331 // pass.
1332 for (int i = 0; i < descriptor_->field_count(); i++) {
1333 const FieldDescriptor* field = descriptor_->field(i);
1335 if (field->is_repeated()) {
1336 PrintFieldComment(printer, field);
1337 field_generators_.get(field).GenerateByteSize(printer);
1338 printer->Print("\n");
1342 if (descriptor_->extension_range_count() > 0) {
1343 printer->Print(
1344 "total_size += _extensions_.ByteSize(&_reflection_);\n"
1345 "\n");
1348 printer->Print("if (!unknown_fields().empty()) {\n");
1349 printer->Indent();
1350 if (descriptor_->options().message_set_wire_format()) {
1351 printer->Print(
1352 "total_size +=\n"
1353 " ::google::protobuf::internal::WireFormat::ComputeUnknownMessageSetItemsSize(\n"
1354 " unknown_fields());\n");
1355 } else {
1356 printer->Print(
1357 "total_size +=\n"
1358 " ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize(\n"
1359 " unknown_fields());\n");
1361 printer->Outdent();
1362 printer->Print("}\n");
1364 // We update _cached_size_ even though this is a const method. In theory,
1365 // this is not thread-compatible, because concurrent writes have undefined
1366 // results. In practice, since any concurrent writes will be writing the
1367 // exact same value, it works on all common processors. In a future version
1368 // of C++, _cached_size_ should be made into an atomic<int>.
1369 printer->Print(
1370 "_cached_size_ = total_size;\n"
1371 "return total_size;\n");
1373 printer->Outdent();
1374 printer->Print("}\n");
1377 void MessageGenerator::
1378 GenerateIsInitialized(io::Printer* printer) {
1379 printer->Print(
1380 "bool $classname$::IsInitialized() const {\n",
1381 "classname", classname_);
1382 printer->Indent();
1384 // Check that all required fields in this message are set. We can do this
1385 // most efficiently by checking 32 "has bits" at a time.
1386 int has_bits_array_size = (descriptor_->field_count() + 31) / 32;
1387 for (int i = 0; i < has_bits_array_size; i++) {
1388 uint32 mask = 0;
1389 for (int bit = 0; bit < 32; bit++) {
1390 int index = i * 32 + bit;
1391 if (index >= descriptor_->field_count()) break;
1392 const FieldDescriptor* field = descriptor_->field(index);
1394 if (field->is_required()) {
1395 mask |= 1 << bit;
1399 if (mask != 0) {
1400 char buffer[kFastToBufferSize];
1401 printer->Print(
1402 "if ((_has_bits_[$i$] & 0x$mask$) != 0x$mask$) return false;\n",
1403 "i", SimpleItoa(i),
1404 "mask", FastHex32ToBuffer(mask, buffer));
1408 // Now check that all embedded messages are initialized.
1409 printer->Print("\n");
1410 for (int i = 0; i < descriptor_->field_count(); i++) {
1411 const FieldDescriptor* field = descriptor_->field(i);
1412 if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE &&
1413 HasRequiredFields(field->message_type())) {
1414 if (field->is_repeated()) {
1415 printer->Print(
1416 "for (int i = 0; i < $name$_size(); i++) {\n"
1417 " if (!this->$name$(i).IsInitialized()) return false;\n"
1418 "}\n",
1419 "name", FieldName(field));
1420 } else {
1421 printer->Print(
1422 "if (has_$name$()) {\n"
1423 " if (!this->$name$().IsInitialized()) return false;\n"
1424 "}\n",
1425 "name", FieldName(field));
1430 if (descriptor_->extension_range_count() > 0) {
1431 printer->Print(
1432 "\n"
1433 "if (!_extensions_.IsInitialized()) return false;");
1436 printer->Outdent();
1437 printer->Print(
1438 " return true;\n"
1439 "}\n");
1442 } // namespace cpp
1443 } // namespace compiler
1444 } // namespace protobuf
1445 } // namespace google