1// Protocol Buffers - Google's data interchange format 2// Copyright 2008 Google Inc. All rights reserved. 3// http://code.google.com/p/protobuf/ 4// 5// Redistribution and use in source and binary forms, with or without 6// modification, are permitted provided that the following conditions are 7// met: 8// 9// * Redistributions of source code must retain the above copyright 10// notice, this list of conditions and the following disclaimer. 11// * Redistributions in binary form must reproduce the above 12// copyright notice, this list of conditions and the following disclaimer 13// in the documentation and/or other materials provided with the 14// distribution. 15// * Neither the name of Google Inc. nor the names of its 16// contributors may be used to endorse or promote products derived from 17// this software without specific prior written permission. 18// 19// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 31// Author: kenton@google.com (Kenton Varda) 32// Based on original Protocol Buffers design by 33// Sanjay Ghemawat, Jeff Dean, and others. 34 35#include <algorithm> 36#include <google/protobuf/stubs/hash.h> 37#include <map> 38#include <utility> 39#include <vector> 40#include <google/protobuf/compiler/cpp/cpp_message.h> 41#include <google/protobuf/compiler/cpp/cpp_field.h> 42#include <google/protobuf/compiler/cpp/cpp_enum.h> 43#include <google/protobuf/compiler/cpp/cpp_extension.h> 44#include <google/protobuf/compiler/cpp/cpp_helpers.h> 45#include <google/protobuf/stubs/strutil.h> 46#include <google/protobuf/io/printer.h> 47#include <google/protobuf/io/coded_stream.h> 48#include <google/protobuf/wire_format.h> 49#include <google/protobuf/descriptor.pb.h> 50 51 52namespace google { 53namespace protobuf { 54namespace compiler { 55namespace cpp { 56 57using internal::WireFormat; 58using internal::WireFormatLite; 59 60namespace { 61 62void PrintFieldComment(io::Printer* printer, const FieldDescriptor* field) { 63 // Print the field's proto-syntax definition as a comment. We don't want to 64 // print group bodies so we cut off after the first line. 65 string def = field->DebugString(); 66 printer->Print("// $def$\n", 67 "def", def.substr(0, def.find_first_of('\n'))); 68} 69 70struct FieldOrderingByNumber { 71 inline bool operator()(const FieldDescriptor* a, 72 const FieldDescriptor* b) const { 73 return a->number() < b->number(); 74 } 75}; 76 77const char* kWireTypeNames[] = { 78 "VARINT", 79 "FIXED64", 80 "LENGTH_DELIMITED", 81 "START_GROUP", 82 "END_GROUP", 83 "FIXED32", 84}; 85 86// Sort the fields of the given Descriptor by number into a new[]'d array 87// and return it. 88const FieldDescriptor** SortFieldsByNumber(const Descriptor* descriptor) { 89 const FieldDescriptor** fields = 90 new const FieldDescriptor*[descriptor->field_count()]; 91 for (int i = 0; i < descriptor->field_count(); i++) { 92 fields[i] = descriptor->field(i); 93 } 94 sort(fields, fields + descriptor->field_count(), 95 FieldOrderingByNumber()); 96 return fields; 97} 98 99// Functor for sorting extension ranges by their "start" field number. 100struct ExtensionRangeSorter { 101 bool operator()(const Descriptor::ExtensionRange* left, 102 const Descriptor::ExtensionRange* right) const { 103 return left->start < right->start; 104 } 105}; 106 107// Returns true if the "required" restriction check should be ignored for the 108// given field. 109inline static bool ShouldIgnoreRequiredFieldCheck( 110 const FieldDescriptor* field) { 111 return false; 112} 113 114// Returns true if the message type has any required fields. If it doesn't, 115// we can optimize out calls to its IsInitialized() method. 116// 117// already_seen is used to avoid checking the same type multiple times 118// (and also to protect against recursion). 119static bool HasRequiredFields( 120 const Descriptor* type, 121 hash_set<const Descriptor*>* already_seen) { 122 if (already_seen->count(type) > 0) { 123 // Since the first occurrence of a required field causes the whole 124 // function to return true, we can assume that if the type is already 125 // in the cache it didn't have any required fields. 126 return false; 127 } 128 already_seen->insert(type); 129 130 // If the type has extensions, an extension with message type could contain 131 // required fields, so we have to be conservative and assume such an 132 // extension exists. 133 if (type->extension_range_count() > 0) return true; 134 135 for (int i = 0; i < type->field_count(); i++) { 136 const FieldDescriptor* field = type->field(i); 137 if (field->is_required()) { 138 return true; 139 } 140 if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE && 141 !ShouldIgnoreRequiredFieldCheck(field)) { 142 if (HasRequiredFields(field->message_type(), already_seen)) { 143 return true; 144 } 145 } 146 } 147 148 return false; 149} 150 151static bool HasRequiredFields(const Descriptor* type) { 152 hash_set<const Descriptor*> already_seen; 153 return HasRequiredFields(type, &already_seen); 154} 155 156// This returns an estimate of the compiler's alignment for the field. This 157// can't guarantee to be correct because the generated code could be compiled on 158// different systems with different alignment rules. The estimates below assume 159// 64-bit pointers. 160int EstimateAlignmentSize(const FieldDescriptor* field) { 161 if (field == NULL) return 0; 162 if (field->is_repeated()) return 8; 163 switch (field->cpp_type()) { 164 case FieldDescriptor::CPPTYPE_BOOL: 165 return 1; 166 167 case FieldDescriptor::CPPTYPE_INT32: 168 case FieldDescriptor::CPPTYPE_UINT32: 169 case FieldDescriptor::CPPTYPE_ENUM: 170 case FieldDescriptor::CPPTYPE_FLOAT: 171 return 4; 172 173 case FieldDescriptor::CPPTYPE_INT64: 174 case FieldDescriptor::CPPTYPE_UINT64: 175 case FieldDescriptor::CPPTYPE_DOUBLE: 176 case FieldDescriptor::CPPTYPE_STRING: 177 case FieldDescriptor::CPPTYPE_MESSAGE: 178 return 8; 179 } 180 GOOGLE_LOG(FATAL) << "Can't get here."; 181 return -1; // Make compiler happy. 182} 183 184// FieldGroup is just a helper for OptimizePadding below. It holds a vector of 185// fields that are grouped together because they have compatible alignment, and 186// a preferred location in the final field ordering. 187class FieldGroup { 188 public: 189 FieldGroup() 190 : preferred_location_(0) {} 191 192 // A group with a single field. 193 FieldGroup(float preferred_location, const FieldDescriptor* field) 194 : preferred_location_(preferred_location), 195 fields_(1, field) {} 196 197 // Append the fields in 'other' to this group. 198 void Append(const FieldGroup& other) { 199 if (other.fields_.empty()) { 200 return; 201 } 202 // Preferred location is the average among all the fields, so we weight by 203 // the number of fields on each FieldGroup object. 204 preferred_location_ = 205 (preferred_location_ * fields_.size() + 206 (other.preferred_location_ * other.fields_.size())) / 207 (fields_.size() + other.fields_.size()); 208 fields_.insert(fields_.end(), other.fields_.begin(), other.fields_.end()); 209 } 210 211 void SetPreferredLocation(float location) { preferred_location_ = location; } 212 const vector<const FieldDescriptor*>& fields() const { return fields_; } 213 214 // FieldGroup objects sort by their preferred location. 215 bool operator<(const FieldGroup& other) const { 216 return preferred_location_ < other.preferred_location_; 217 } 218 219 private: 220 // "preferred_location_" is an estimate of where this group should go in the 221 // final list of fields. We compute this by taking the average index of each 222 // field in this group in the original ordering of fields. This is very 223 // approximate, but should put this group close to where its member fields 224 // originally went. 225 float preferred_location_; 226 vector<const FieldDescriptor*> fields_; 227 // We rely on the default copy constructor and operator= so this type can be 228 // used in a vector. 229}; 230 231// Reorder 'fields' so that if the fields are output into a c++ class in the new 232// order, the alignment padding is minimized. We try to do this while keeping 233// each field as close as possible to its original position so that we don't 234// reduce cache locality much for function that access each field in order. 235void OptimizePadding(vector<const FieldDescriptor*>* fields) { 236 // First divide fields into those that align to 1 byte, 4 bytes or 8 bytes. 237 vector<FieldGroup> aligned_to_1, aligned_to_4, aligned_to_8; 238 for (int i = 0; i < fields->size(); ++i) { 239 switch (EstimateAlignmentSize((*fields)[i])) { 240 case 1: aligned_to_1.push_back(FieldGroup(i, (*fields)[i])); break; 241 case 4: aligned_to_4.push_back(FieldGroup(i, (*fields)[i])); break; 242 case 8: aligned_to_8.push_back(FieldGroup(i, (*fields)[i])); break; 243 default: 244 GOOGLE_LOG(FATAL) << "Unknown alignment size."; 245 } 246 } 247 248 // Now group fields aligned to 1 byte into sets of 4, and treat those like a 249 // single field aligned to 4 bytes. 250 for (int i = 0; i < aligned_to_1.size(); i += 4) { 251 FieldGroup field_group; 252 for (int j = i; j < aligned_to_1.size() && j < i + 4; ++j) { 253 field_group.Append(aligned_to_1[j]); 254 } 255 aligned_to_4.push_back(field_group); 256 } 257 // Sort by preferred location to keep fields as close to their original 258 // location as possible. 259 sort(aligned_to_4.begin(), aligned_to_4.end()); 260 261 // Now group fields aligned to 4 bytes (or the 4-field groups created above) 262 // into pairs, and treat those like a single field aligned to 8 bytes. 263 for (int i = 0; i < aligned_to_4.size(); i += 2) { 264 FieldGroup field_group; 265 for (int j = i; j < aligned_to_4.size() && j < i + 2; ++j) { 266 field_group.Append(aligned_to_4[j]); 267 } 268 if (i == aligned_to_4.size() - 1) { 269 // Move incomplete 4-byte block to the end. 270 field_group.SetPreferredLocation(fields->size() + 1); 271 } 272 aligned_to_8.push_back(field_group); 273 } 274 // Sort by preferred location to keep fields as close to their original 275 // location as possible. 276 sort(aligned_to_8.begin(), aligned_to_8.end()); 277 278 // Now pull out all the FieldDescriptors in order. 279 fields->clear(); 280 for (int i = 0; i < aligned_to_8.size(); ++i) { 281 fields->insert(fields->end(), 282 aligned_to_8[i].fields().begin(), 283 aligned_to_8[i].fields().end()); 284 } 285} 286 287} 288 289// =================================================================== 290 291MessageGenerator::MessageGenerator(const Descriptor* descriptor, 292 const Options& options) 293 : descriptor_(descriptor), 294 classname_(ClassName(descriptor, false)), 295 options_(options), 296 field_generators_(descriptor, options), 297 nested_generators_(new scoped_ptr<MessageGenerator>[ 298 descriptor->nested_type_count()]), 299 enum_generators_(new scoped_ptr<EnumGenerator>[ 300 descriptor->enum_type_count()]), 301 extension_generators_(new scoped_ptr<ExtensionGenerator>[ 302 descriptor->extension_count()]) { 303 304 for (int i = 0; i < descriptor->nested_type_count(); i++) { 305 nested_generators_[i].reset( 306 new MessageGenerator(descriptor->nested_type(i), options)); 307 } 308 309 for (int i = 0; i < descriptor->enum_type_count(); i++) { 310 enum_generators_[i].reset( 311 new EnumGenerator(descriptor->enum_type(i), options)); 312 } 313 314 for (int i = 0; i < descriptor->extension_count(); i++) { 315 extension_generators_[i].reset( 316 new ExtensionGenerator(descriptor->extension(i), options)); 317 } 318} 319 320MessageGenerator::~MessageGenerator() {} 321 322void MessageGenerator:: 323GenerateForwardDeclaration(io::Printer* printer) { 324 printer->Print("class $classname$;\n", 325 "classname", classname_); 326 327 for (int i = 0; i < descriptor_->nested_type_count(); i++) { 328 nested_generators_[i]->GenerateForwardDeclaration(printer); 329 } 330} 331 332void MessageGenerator:: 333GenerateEnumDefinitions(io::Printer* printer) { 334 for (int i = 0; i < descriptor_->nested_type_count(); i++) { 335 nested_generators_[i]->GenerateEnumDefinitions(printer); 336 } 337 338 for (int i = 0; i < descriptor_->enum_type_count(); i++) { 339 enum_generators_[i]->GenerateDefinition(printer); 340 } 341} 342 343void MessageGenerator:: 344GenerateGetEnumDescriptorSpecializations(io::Printer* printer) { 345 for (int i = 0; i < descriptor_->nested_type_count(); i++) { 346 nested_generators_[i]->GenerateGetEnumDescriptorSpecializations(printer); 347 } 348 for (int i = 0; i < descriptor_->enum_type_count(); i++) { 349 enum_generators_[i]->GenerateGetEnumDescriptorSpecializations(printer); 350 } 351} 352 353void MessageGenerator:: 354GenerateFieldAccessorDeclarations(io::Printer* printer) { 355 for (int i = 0; i < descriptor_->field_count(); i++) { 356 const FieldDescriptor* field = descriptor_->field(i); 357 358 PrintFieldComment(printer, field); 359 360 map<string, string> vars; 361 SetCommonFieldVariables(field, &vars, options_); 362 vars["constant_name"] = FieldConstantName(field); 363 364 if (field->is_repeated()) { 365 printer->Print(vars, "inline int $name$_size() const$deprecation$;\n"); 366 } else { 367 printer->Print(vars, "inline bool has_$name$() const$deprecation$;\n"); 368 } 369 370 printer->Print(vars, "inline void clear_$name$()$deprecation$;\n"); 371 printer->Print(vars, "static const int $constant_name$ = $number$;\n"); 372 373 // Generate type-specific accessor declarations. 374 field_generators_.get(field).GenerateAccessorDeclarations(printer); 375 376 printer->Print("\n"); 377 } 378 379 if (descriptor_->extension_range_count() > 0) { 380 // Generate accessors for extensions. We just call a macro located in 381 // extension_set.h since the accessors about 80 lines of static code. 382 printer->Print( 383 "GOOGLE_PROTOBUF_EXTENSION_ACCESSORS($classname$)\n", 384 "classname", classname_); 385 } 386} 387 388void MessageGenerator:: 389GenerateFieldAccessorDefinitions(io::Printer* printer) { 390 printer->Print("// $classname$\n\n", "classname", classname_); 391 392 for (int i = 0; i < descriptor_->field_count(); i++) { 393 const FieldDescriptor* field = descriptor_->field(i); 394 395 PrintFieldComment(printer, field); 396 397 map<string, string> vars; 398 SetCommonFieldVariables(field, &vars, options_); 399 400 // Generate has_$name$() or $name$_size(). 401 if (field->is_repeated()) { 402 printer->Print(vars, 403 "inline int $classname$::$name$_size() const {\n" 404 " return $name$_.size();\n" 405 "}\n"); 406 } else { 407 // Singular field. 408 char buffer[kFastToBufferSize]; 409 vars["has_array_index"] = SimpleItoa(field->index() / 32); 410 vars["has_mask"] = FastHex32ToBuffer(1u << (field->index() % 32), buffer); 411 printer->Print(vars, 412 "inline bool $classname$::has_$name$() const {\n" 413 " return (_has_bits_[$has_array_index$] & 0x$has_mask$u) != 0;\n" 414 "}\n" 415 "inline void $classname$::set_has_$name$() {\n" 416 " _has_bits_[$has_array_index$] |= 0x$has_mask$u;\n" 417 "}\n" 418 "inline void $classname$::clear_has_$name$() {\n" 419 " _has_bits_[$has_array_index$] &= ~0x$has_mask$u;\n" 420 "}\n" 421 ); 422 } 423 424 // Generate clear_$name$() 425 printer->Print(vars, 426 "inline void $classname$::clear_$name$() {\n"); 427 428 printer->Indent(); 429 field_generators_.get(field).GenerateClearingCode(printer); 430 printer->Outdent(); 431 432 if (!field->is_repeated()) { 433 printer->Print(vars, 434 " clear_has_$name$();\n"); 435 } 436 437 printer->Print("}\n"); 438 439 // Generate type-specific accessors. 440 field_generators_.get(field).GenerateInlineAccessorDefinitions(printer); 441 442 printer->Print("\n"); 443 } 444} 445 446void MessageGenerator:: 447GenerateClassDefinition(io::Printer* printer) { 448 for (int i = 0; i < descriptor_->nested_type_count(); i++) { 449 nested_generators_[i]->GenerateClassDefinition(printer); 450 printer->Print("\n"); 451 printer->Print(kThinSeparator); 452 printer->Print("\n"); 453 } 454 455 map<string, string> vars; 456 vars["classname"] = classname_; 457 vars["field_count"] = SimpleItoa(descriptor_->field_count()); 458 if (options_.dllexport_decl.empty()) { 459 vars["dllexport"] = ""; 460 } else { 461 vars["dllexport"] = options_.dllexport_decl + " "; 462 } 463 vars["superclass"] = SuperClassName(descriptor_); 464 465 printer->Print(vars, 466 "class $dllexport$$classname$ : public $superclass$ {\n" 467 " public:\n"); 468 printer->Indent(); 469 470 printer->Print(vars, 471 "$classname$();\n" 472 "virtual ~$classname$();\n" 473 "\n" 474 "$classname$(const $classname$& from);\n" 475 "\n" 476 "inline $classname$& operator=(const $classname$& from) {\n" 477 " CopyFrom(from);\n" 478 " return *this;\n" 479 "}\n" 480 "\n"); 481 482 if (HasUnknownFields(descriptor_->file())) { 483 printer->Print( 484 "inline const ::google::protobuf::UnknownFieldSet& unknown_fields() const {\n" 485 " return _unknown_fields_;\n" 486 "}\n" 487 "\n" 488 "inline ::google::protobuf::UnknownFieldSet* mutable_unknown_fields() {\n" 489 " return &_unknown_fields_;\n" 490 "}\n" 491 "\n"); 492 } 493 494 // Only generate this member if it's not disabled. 495 if (HasDescriptorMethods(descriptor_->file()) && 496 !descriptor_->options().no_standard_descriptor_accessor()) { 497 printer->Print(vars, 498 "static const ::google::protobuf::Descriptor* descriptor();\n"); 499 } 500 501 printer->Print(vars, 502 "static const $classname$& default_instance();\n" 503 "\n"); 504 505 if (!StaticInitializersForced(descriptor_->file())) { 506 printer->Print(vars, 507 "#ifdef GOOGLE_PROTOBUF_NO_STATIC_INITIALIZER\n" 508 "// Returns the internal default instance pointer. This function can\n" 509 "// return NULL thus should not be used by the user. This is intended\n" 510 "// for Protobuf internal code. Please use default_instance() declared\n" 511 "// above instead.\n" 512 "static inline const $classname$* internal_default_instance() {\n" 513 " return default_instance_;\n" 514 "}\n" 515 "#endif\n" 516 "\n"); 517 } 518 519 520 printer->Print(vars, 521 "void Swap($classname$* other);\n" 522 "\n" 523 "// implements Message ----------------------------------------------\n" 524 "\n" 525 "$classname$* New() const;\n"); 526 527 if (HasGeneratedMethods(descriptor_->file())) { 528 if (HasDescriptorMethods(descriptor_->file())) { 529 printer->Print(vars, 530 "void CopyFrom(const ::google::protobuf::Message& from);\n" 531 "void MergeFrom(const ::google::protobuf::Message& from);\n"); 532 } else { 533 printer->Print(vars, 534 "void CheckTypeAndMergeFrom(const ::google::protobuf::MessageLite& from);\n"); 535 } 536 537 printer->Print(vars, 538 "void CopyFrom(const $classname$& from);\n" 539 "void MergeFrom(const $classname$& from);\n" 540 "void Clear();\n" 541 "bool IsInitialized() const;\n" 542 "\n" 543 "int ByteSize() const;\n" 544 "bool MergePartialFromCodedStream(\n" 545 " ::google::protobuf::io::CodedInputStream* input);\n" 546 "void SerializeWithCachedSizes(\n" 547 " ::google::protobuf::io::CodedOutputStream* output) const;\n"); 548 if (HasFastArraySerialization(descriptor_->file())) { 549 printer->Print( 550 "::google::protobuf::uint8* SerializeWithCachedSizesToArray(::google::protobuf::uint8* output) const;\n"); 551 } 552 } 553 554 printer->Print(vars, 555 "int GetCachedSize() const { return _cached_size_; }\n" 556 "private:\n" 557 "void SharedCtor();\n" 558 "void SharedDtor();\n" 559 "void SetCachedSize(int size) const;\n" 560 "public:\n" 561 "\n"); 562 563 if (HasDescriptorMethods(descriptor_->file())) { 564 printer->Print( 565 "::google::protobuf::Metadata GetMetadata() const;\n" 566 "\n"); 567 } else { 568 printer->Print( 569 "::std::string GetTypeName() const;\n" 570 "\n"); 571 } 572 573 printer->Print( 574 "// nested types ----------------------------------------------------\n" 575 "\n"); 576 577 // Import all nested message classes into this class's scope with typedefs. 578 for (int i = 0; i < descriptor_->nested_type_count(); i++) { 579 const Descriptor* nested_type = descriptor_->nested_type(i); 580 printer->Print("typedef $nested_full_name$ $nested_name$;\n", 581 "nested_name", nested_type->name(), 582 "nested_full_name", ClassName(nested_type, false)); 583 } 584 585 if (descriptor_->nested_type_count() > 0) { 586 printer->Print("\n"); 587 } 588 589 // Import all nested enums and their values into this class's scope with 590 // typedefs and constants. 591 for (int i = 0; i < descriptor_->enum_type_count(); i++) { 592 enum_generators_[i]->GenerateSymbolImports(printer); 593 printer->Print("\n"); 594 } 595 596 printer->Print( 597 "// accessors -------------------------------------------------------\n" 598 "\n"); 599 600 // Generate accessor methods for all fields. 601 GenerateFieldAccessorDeclarations(printer); 602 603 // Declare extension identifiers. 604 for (int i = 0; i < descriptor_->extension_count(); i++) { 605 extension_generators_[i]->GenerateDeclaration(printer); 606 } 607 608 609 printer->Print( 610 "// @@protoc_insertion_point(class_scope:$full_name$)\n", 611 "full_name", descriptor_->full_name()); 612 613 // Generate private members. 614 printer->Outdent(); 615 printer->Print(" private:\n"); 616 printer->Indent(); 617 618 619 for (int i = 0; i < descriptor_->field_count(); i++) { 620 if (!descriptor_->field(i)->is_repeated()) { 621 printer->Print( 622 "inline void set_has_$name$();\n", 623 "name", FieldName(descriptor_->field(i))); 624 printer->Print( 625 "inline void clear_has_$name$();\n", 626 "name", FieldName(descriptor_->field(i))); 627 } 628 } 629 printer->Print("\n"); 630 631 // To minimize padding, data members are divided into three sections: 632 // (1) members assumed to align to 8 bytes 633 // (2) members corresponding to message fields, re-ordered to optimize 634 // alignment. 635 // (3) members assumed to align to 4 bytes. 636 637 // Members assumed to align to 8 bytes: 638 639 if (descriptor_->extension_range_count() > 0) { 640 printer->Print( 641 "::google::protobuf::internal::ExtensionSet _extensions_;\n" 642 "\n"); 643 } 644 645 if (HasUnknownFields(descriptor_->file())) { 646 printer->Print( 647 "::google::protobuf::UnknownFieldSet _unknown_fields_;\n" 648 "\n"); 649 } 650 651 // Field members: 652 653 vector<const FieldDescriptor*> fields; 654 for (int i = 0; i < descriptor_->field_count(); i++) { 655 fields.push_back(descriptor_->field(i)); 656 } 657 OptimizePadding(&fields); 658 for (int i = 0; i < fields.size(); ++i) { 659 field_generators_.get(fields[i]).GeneratePrivateMembers(printer); 660 } 661 662 // Members assumed to align to 4 bytes: 663 664 // TODO(kenton): Make _cached_size_ an atomic<int> when C++ supports it. 665 printer->Print( 666 "\n" 667 "mutable int _cached_size_;\n"); 668 669 // Generate _has_bits_. 670 if (descriptor_->field_count() > 0) { 671 printer->Print(vars, 672 "::google::protobuf::uint32 _has_bits_[($field_count$ + 31) / 32];\n" 673 "\n"); 674 } else { 675 // Zero-size arrays aren't technically allowed, and MSVC in particular 676 // doesn't like them. We still need to declare these arrays to make 677 // other code compile. Since this is an uncommon case, we'll just declare 678 // them with size 1 and waste some space. Oh well. 679 printer->Print( 680 "::google::protobuf::uint32 _has_bits_[1];\n" 681 "\n"); 682 } 683 684 // Declare AddDescriptors(), BuildDescriptors(), and ShutdownFile() as 685 // friends so that they can access private static variables like 686 // default_instance_ and reflection_. 687 PrintHandlingOptionalStaticInitializers( 688 descriptor_->file(), printer, 689 // With static initializers. 690 "friend void $dllexport_decl$ $adddescriptorsname$();\n", 691 // Without. 692 "friend void $dllexport_decl$ $adddescriptorsname$_impl();\n", 693 // Vars. 694 "dllexport_decl", options_.dllexport_decl, 695 "adddescriptorsname", 696 GlobalAddDescriptorsName(descriptor_->file()->name())); 697 698 printer->Print( 699 "friend void $assigndescriptorsname$();\n" 700 "friend void $shutdownfilename$();\n" 701 "\n", 702 "assigndescriptorsname", 703 GlobalAssignDescriptorsName(descriptor_->file()->name()), 704 "shutdownfilename", GlobalShutdownFileName(descriptor_->file()->name())); 705 706 printer->Print( 707 "void InitAsDefaultInstance();\n" 708 "static $classname$* default_instance_;\n", 709 "classname", classname_); 710 711 printer->Outdent(); 712 printer->Print(vars, "};"); 713} 714 715void MessageGenerator:: 716GenerateInlineMethods(io::Printer* printer) { 717 for (int i = 0; i < descriptor_->nested_type_count(); i++) { 718 nested_generators_[i]->GenerateInlineMethods(printer); 719 printer->Print(kThinSeparator); 720 printer->Print("\n"); 721 } 722 723 GenerateFieldAccessorDefinitions(printer); 724} 725 726void MessageGenerator:: 727GenerateDescriptorDeclarations(io::Printer* printer) { 728 printer->Print( 729 "const ::google::protobuf::Descriptor* $name$_descriptor_ = NULL;\n" 730 "const ::google::protobuf::internal::GeneratedMessageReflection*\n" 731 " $name$_reflection_ = NULL;\n", 732 "name", classname_); 733 734 for (int i = 0; i < descriptor_->nested_type_count(); i++) { 735 nested_generators_[i]->GenerateDescriptorDeclarations(printer); 736 } 737 738 for (int i = 0; i < descriptor_->enum_type_count(); i++) { 739 printer->Print( 740 "const ::google::protobuf::EnumDescriptor* $name$_descriptor_ = NULL;\n", 741 "name", ClassName(descriptor_->enum_type(i), false)); 742 } 743} 744 745void MessageGenerator:: 746GenerateDescriptorInitializer(io::Printer* printer, int index) { 747 // TODO(kenton): Passing the index to this method is redundant; just use 748 // descriptor_->index() instead. 749 map<string, string> vars; 750 vars["classname"] = classname_; 751 vars["index"] = SimpleItoa(index); 752 753 // Obtain the descriptor from the parent's descriptor. 754 if (descriptor_->containing_type() == NULL) { 755 printer->Print(vars, 756 "$classname$_descriptor_ = file->message_type($index$);\n"); 757 } else { 758 vars["parent"] = ClassName(descriptor_->containing_type(), false); 759 printer->Print(vars, 760 "$classname$_descriptor_ = " 761 "$parent$_descriptor_->nested_type($index$);\n"); 762 } 763 764 // Generate the offsets. 765 GenerateOffsets(printer); 766 767 // Construct the reflection object. 768 printer->Print(vars, 769 "$classname$_reflection_ =\n" 770 " new ::google::protobuf::internal::GeneratedMessageReflection(\n" 771 " $classname$_descriptor_,\n" 772 " $classname$::default_instance_,\n" 773 " $classname$_offsets_,\n" 774 " GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET($classname$, _has_bits_[0]),\n" 775 " GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(" 776 "$classname$, _unknown_fields_),\n"); 777 if (descriptor_->extension_range_count() > 0) { 778 printer->Print(vars, 779 " GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(" 780 "$classname$, _extensions_),\n"); 781 } else { 782 // No extensions. 783 printer->Print(vars, 784 " -1,\n"); 785 } 786 printer->Print( 787 " ::google::protobuf::DescriptorPool::generated_pool(),\n"); 788 printer->Print(vars, 789 " ::google::protobuf::MessageFactory::generated_factory(),\n"); 790 printer->Print(vars, 791 " sizeof($classname$));\n"); 792 793 // Handle nested types. 794 for (int i = 0; i < descriptor_->nested_type_count(); i++) { 795 nested_generators_[i]->GenerateDescriptorInitializer(printer, i); 796 } 797 798 for (int i = 0; i < descriptor_->enum_type_count(); i++) { 799 enum_generators_[i]->GenerateDescriptorInitializer(printer, i); 800 } 801} 802 803void MessageGenerator:: 804GenerateTypeRegistrations(io::Printer* printer) { 805 // Register this message type with the message factory. 806 printer->Print( 807 "::google::protobuf::MessageFactory::InternalRegisterGeneratedMessage(\n" 808 " $classname$_descriptor_, &$classname$::default_instance());\n", 809 "classname", classname_); 810 811 // Handle nested types. 812 for (int i = 0; i < descriptor_->nested_type_count(); i++) { 813 nested_generators_[i]->GenerateTypeRegistrations(printer); 814 } 815} 816 817void MessageGenerator:: 818GenerateDefaultInstanceAllocator(io::Printer* printer) { 819 // Construct the default instances of all fields, as they will be used 820 // when creating the default instance of the entire message. 821 for (int i = 0; i < descriptor_->field_count(); i++) { 822 field_generators_.get(descriptor_->field(i)) 823 .GenerateDefaultInstanceAllocator(printer); 824 } 825 826 // Construct the default instance. We can't call InitAsDefaultInstance() yet 827 // because we need to make sure all default instances that this one might 828 // depend on are constructed first. 829 printer->Print( 830 "$classname$::default_instance_ = new $classname$();\n", 831 "classname", classname_); 832 833 // Handle nested types. 834 for (int i = 0; i < descriptor_->nested_type_count(); i++) { 835 nested_generators_[i]->GenerateDefaultInstanceAllocator(printer); 836 } 837 838} 839 840void MessageGenerator:: 841GenerateDefaultInstanceInitializer(io::Printer* printer) { 842 printer->Print( 843 "$classname$::default_instance_->InitAsDefaultInstance();\n", 844 "classname", classname_); 845 846 // Register extensions. 847 for (int i = 0; i < descriptor_->extension_count(); i++) { 848 extension_generators_[i]->GenerateRegistration(printer); 849 } 850 851 // Handle nested types. 852 for (int i = 0; i < descriptor_->nested_type_count(); i++) { 853 nested_generators_[i]->GenerateDefaultInstanceInitializer(printer); 854 } 855} 856 857void MessageGenerator:: 858GenerateShutdownCode(io::Printer* printer) { 859 printer->Print( 860 "delete $classname$::default_instance_;\n", 861 "classname", classname_); 862 863 if (HasDescriptorMethods(descriptor_->file())) { 864 printer->Print( 865 "delete $classname$_reflection_;\n", 866 "classname", classname_); 867 } 868 869 // Handle default instances of fields. 870 for (int i = 0; i < descriptor_->field_count(); i++) { 871 field_generators_.get(descriptor_->field(i)) 872 .GenerateShutdownCode(printer); 873 } 874 875 // Handle nested types. 876 for (int i = 0; i < descriptor_->nested_type_count(); i++) { 877 nested_generators_[i]->GenerateShutdownCode(printer); 878 } 879} 880 881void MessageGenerator:: 882GenerateClassMethods(io::Printer* printer) { 883 for (int i = 0; i < descriptor_->enum_type_count(); i++) { 884 enum_generators_[i]->GenerateMethods(printer); 885 } 886 887 for (int i = 0; i < descriptor_->nested_type_count(); i++) { 888 nested_generators_[i]->GenerateClassMethods(printer); 889 printer->Print("\n"); 890 printer->Print(kThinSeparator); 891 printer->Print("\n"); 892 } 893 894 // Generate non-inline field definitions. 895 for (int i = 0; i < descriptor_->field_count(); i++) { 896 field_generators_.get(descriptor_->field(i)) 897 .GenerateNonInlineAccessorDefinitions(printer); 898 } 899 900 // Generate field number constants. 901 printer->Print("#ifndef _MSC_VER\n"); 902 for (int i = 0; i < descriptor_->field_count(); i++) { 903 const FieldDescriptor *field = descriptor_->field(i); 904 printer->Print( 905 "const int $classname$::$constant_name$;\n", 906 "classname", ClassName(FieldScope(field), false), 907 "constant_name", FieldConstantName(field)); 908 } 909 printer->Print( 910 "#endif // !_MSC_VER\n" 911 "\n"); 912 913 // Define extension identifiers. 914 for (int i = 0; i < descriptor_->extension_count(); i++) { 915 extension_generators_[i]->GenerateDefinition(printer); 916 } 917 918 GenerateStructors(printer); 919 printer->Print("\n"); 920 921 if (HasGeneratedMethods(descriptor_->file())) { 922 GenerateClear(printer); 923 printer->Print("\n"); 924 925 GenerateMergeFromCodedStream(printer); 926 printer->Print("\n"); 927 928 GenerateSerializeWithCachedSizes(printer); 929 printer->Print("\n"); 930 931 if (HasFastArraySerialization(descriptor_->file())) { 932 GenerateSerializeWithCachedSizesToArray(printer); 933 printer->Print("\n"); 934 } 935 936 GenerateByteSize(printer); 937 printer->Print("\n"); 938 939 GenerateMergeFrom(printer); 940 printer->Print("\n"); 941 942 GenerateCopyFrom(printer); 943 printer->Print("\n"); 944 945 GenerateIsInitialized(printer); 946 printer->Print("\n"); 947 } 948 949 GenerateSwap(printer); 950 printer->Print("\n"); 951 952 if (HasDescriptorMethods(descriptor_->file())) { 953 printer->Print( 954 "::google::protobuf::Metadata $classname$::GetMetadata() const {\n" 955 " protobuf_AssignDescriptorsOnce();\n" 956 " ::google::protobuf::Metadata metadata;\n" 957 " metadata.descriptor = $classname$_descriptor_;\n" 958 " metadata.reflection = $classname$_reflection_;\n" 959 " return metadata;\n" 960 "}\n" 961 "\n", 962 "classname", classname_); 963 } else { 964 printer->Print( 965 "::std::string $classname$::GetTypeName() const {\n" 966 " return \"$type_name$\";\n" 967 "}\n" 968 "\n", 969 "classname", classname_, 970 "type_name", descriptor_->full_name()); 971 } 972 973} 974 975void MessageGenerator:: 976GenerateOffsets(io::Printer* printer) { 977 printer->Print( 978 "static const int $classname$_offsets_[$field_count$] = {\n", 979 "classname", classname_, 980 "field_count", SimpleItoa(max(1, descriptor_->field_count()))); 981 printer->Indent(); 982 983 for (int i = 0; i < descriptor_->field_count(); i++) { 984 const FieldDescriptor* field = descriptor_->field(i); 985 printer->Print( 986 "GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET($classname$, $name$_),\n", 987 "classname", classname_, 988 "name", FieldName(field)); 989 } 990 991 printer->Outdent(); 992 printer->Print("};\n"); 993} 994 995void MessageGenerator:: 996GenerateSharedConstructorCode(io::Printer* printer) { 997 printer->Print( 998 "void $classname$::SharedCtor() {\n", 999 "classname", classname_); 1000 printer->Indent(); 1001 1002 printer->Print( 1003 "_cached_size_ = 0;\n"); 1004 1005 for (int i = 0; i < descriptor_->field_count(); i++) { 1006 field_generators_.get(descriptor_->field(i)) 1007 .GenerateConstructorCode(printer); 1008 } 1009 1010 printer->Print( 1011 "::memset(_has_bits_, 0, sizeof(_has_bits_));\n"); 1012 1013 printer->Outdent(); 1014 printer->Print("}\n\n"); 1015} 1016 1017void MessageGenerator:: 1018GenerateSharedDestructorCode(io::Printer* printer) { 1019 printer->Print( 1020 "void $classname$::SharedDtor() {\n", 1021 "classname", classname_); 1022 printer->Indent(); 1023 // Write the destructors for each field. 1024 for (int i = 0; i < descriptor_->field_count(); i++) { 1025 field_generators_.get(descriptor_->field(i)) 1026 .GenerateDestructorCode(printer); 1027 } 1028 1029 PrintHandlingOptionalStaticInitializers( 1030 descriptor_->file(), printer, 1031 // With static initializers. 1032 "if (this != default_instance_) {\n", 1033 // Without. 1034 "if (this != &default_instance()) {\n"); 1035 1036 // We need to delete all embedded messages. 1037 // TODO(kenton): If we make unset messages point at default instances 1038 // instead of NULL, then it would make sense to move this code into 1039 // MessageFieldGenerator::GenerateDestructorCode(). 1040 for (int i = 0; i < descriptor_->field_count(); i++) { 1041 const FieldDescriptor* field = descriptor_->field(i); 1042 1043 if (!field->is_repeated() && 1044 field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { 1045 printer->Print(" delete $name$_;\n", 1046 "name", FieldName(field)); 1047 } 1048 } 1049 1050 printer->Outdent(); 1051 printer->Print( 1052 " }\n" 1053 "}\n" 1054 "\n"); 1055} 1056 1057void MessageGenerator:: 1058GenerateStructors(io::Printer* printer) { 1059 string superclass = SuperClassName(descriptor_); 1060 1061 // Generate the default constructor. 1062 printer->Print( 1063 "$classname$::$classname$()\n" 1064 " : $superclass$() {\n" 1065 " SharedCtor();\n" 1066 "}\n", 1067 "classname", classname_, 1068 "superclass", superclass); 1069 1070 printer->Print( 1071 "\n" 1072 "void $classname$::InitAsDefaultInstance() {\n", 1073 "classname", classname_); 1074 1075 // The default instance needs all of its embedded message pointers 1076 // cross-linked to other default instances. We can't do this initialization 1077 // in the constructor because some other default instances may not have been 1078 // constructed yet at that time. 1079 // TODO(kenton): Maybe all message fields (even for non-default messages) 1080 // should be initialized to point at default instances rather than NULL? 1081 for (int i = 0; i < descriptor_->field_count(); i++) { 1082 const FieldDescriptor* field = descriptor_->field(i); 1083 1084 if (!field->is_repeated() && 1085 field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { 1086 PrintHandlingOptionalStaticInitializers( 1087 descriptor_->file(), printer, 1088 // With static initializers. 1089 " $name$_ = const_cast< $type$*>(&$type$::default_instance());\n", 1090 // Without. 1091 " $name$_ = const_cast< $type$*>(\n" 1092 " $type$::internal_default_instance());\n", 1093 // Vars. 1094 "name", FieldName(field), 1095 "type", FieldMessageTypeName(field)); 1096 } 1097 } 1098 printer->Print( 1099 "}\n" 1100 "\n"); 1101 1102 // Generate the copy constructor. 1103 printer->Print( 1104 "$classname$::$classname$(const $classname$& from)\n" 1105 " : $superclass$() {\n" 1106 " SharedCtor();\n" 1107 " MergeFrom(from);\n" 1108 "}\n" 1109 "\n", 1110 "classname", classname_, 1111 "superclass", superclass); 1112 1113 // Generate the shared constructor code. 1114 GenerateSharedConstructorCode(printer); 1115 1116 // Generate the destructor. 1117 printer->Print( 1118 "$classname$::~$classname$() {\n" 1119 " SharedDtor();\n" 1120 "}\n" 1121 "\n", 1122 "classname", classname_); 1123 1124 // Generate the shared destructor code. 1125 GenerateSharedDestructorCode(printer); 1126 1127 // Generate SetCachedSize. 1128 printer->Print( 1129 "void $classname$::SetCachedSize(int size) const {\n" 1130 " GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN();\n" 1131 " _cached_size_ = size;\n" 1132 " GOOGLE_SAFE_CONCURRENT_WRITES_END();\n" 1133 "}\n", 1134 "classname", classname_); 1135 1136 // Only generate this member if it's not disabled. 1137 if (HasDescriptorMethods(descriptor_->file()) && 1138 !descriptor_->options().no_standard_descriptor_accessor()) { 1139 printer->Print( 1140 "const ::google::protobuf::Descriptor* $classname$::descriptor() {\n" 1141 " protobuf_AssignDescriptorsOnce();\n" 1142 " return $classname$_descriptor_;\n" 1143 "}\n" 1144 "\n", 1145 "classname", classname_, 1146 "adddescriptorsname", 1147 GlobalAddDescriptorsName(descriptor_->file()->name())); 1148 } 1149 1150 printer->Print( 1151 "const $classname$& $classname$::default_instance() {\n", 1152 "classname", classname_); 1153 1154 PrintHandlingOptionalStaticInitializers( 1155 descriptor_->file(), printer, 1156 // With static initializers. 1157 " if (default_instance_ == NULL) $adddescriptorsname$();\n", 1158 // Without. 1159 " $adddescriptorsname$();\n", 1160 // Vars. 1161 "adddescriptorsname", 1162 GlobalAddDescriptorsName(descriptor_->file()->name())); 1163 1164 printer->Print( 1165 " return *default_instance_;\n" 1166 "}\n" 1167 "\n" 1168 "$classname$* $classname$::default_instance_ = NULL;\n" 1169 "\n" 1170 "$classname$* $classname$::New() const {\n" 1171 " return new $classname$;\n" 1172 "}\n", 1173 "classname", classname_, 1174 "adddescriptorsname", 1175 GlobalAddDescriptorsName(descriptor_->file()->name())); 1176} 1177 1178void MessageGenerator:: 1179GenerateClear(io::Printer* printer) { 1180 printer->Print("void $classname$::Clear() {\n", 1181 "classname", classname_); 1182 printer->Indent(); 1183 1184 int last_index = -1; 1185 1186 if (descriptor_->extension_range_count() > 0) { 1187 printer->Print("_extensions_.Clear();\n"); 1188 } 1189 1190 for (int i = 0; i < descriptor_->field_count(); i++) { 1191 const FieldDescriptor* field = descriptor_->field(i); 1192 1193 if (!field->is_repeated()) { 1194 // We can use the fact that _has_bits_ is a giant bitfield to our 1195 // advantage: We can check up to 32 bits at a time for equality to 1196 // zero, and skip the whole range if so. This can improve the speed 1197 // of Clear() for messages which contain a very large number of 1198 // optional fields of which only a few are used at a time. Here, 1199 // we've chosen to check 8 bits at a time rather than 32. 1200 if (i / 8 != last_index / 8 || last_index < 0) { 1201 if (last_index >= 0) { 1202 printer->Outdent(); 1203 printer->Print("}\n"); 1204 } 1205 printer->Print( 1206 "if (_has_bits_[$index$ / 32] & (0xffu << ($index$ % 32))) {\n", 1207 "index", SimpleItoa(field->index())); 1208 printer->Indent(); 1209 } 1210 last_index = i; 1211 1212 // It's faster to just overwrite primitive types, but we should 1213 // only clear strings and messages if they were set. 1214 // TODO(kenton): Let the CppFieldGenerator decide this somehow. 1215 bool should_check_bit = 1216 field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE || 1217 field->cpp_type() == FieldDescriptor::CPPTYPE_STRING; 1218 1219 if (should_check_bit) { 1220 printer->Print( 1221 "if (has_$name$()) {\n", 1222 "name", FieldName(field)); 1223 printer->Indent(); 1224 } 1225 1226 field_generators_.get(field).GenerateClearingCode(printer); 1227 1228 if (should_check_bit) { 1229 printer->Outdent(); 1230 printer->Print("}\n"); 1231 } 1232 } 1233 } 1234 1235 if (last_index >= 0) { 1236 printer->Outdent(); 1237 printer->Print("}\n"); 1238 } 1239 1240 // Repeated fields don't use _has_bits_ so we clear them in a separate 1241 // pass. 1242 for (int i = 0; i < descriptor_->field_count(); i++) { 1243 const FieldDescriptor* field = descriptor_->field(i); 1244 1245 if (field->is_repeated()) { 1246 field_generators_.get(field).GenerateClearingCode(printer); 1247 } 1248 } 1249 1250 printer->Print( 1251 "::memset(_has_bits_, 0, sizeof(_has_bits_));\n"); 1252 1253 if (HasUnknownFields(descriptor_->file())) { 1254 printer->Print( 1255 "mutable_unknown_fields()->Clear();\n"); 1256 } 1257 1258 printer->Outdent(); 1259 printer->Print("}\n"); 1260} 1261 1262void MessageGenerator:: 1263GenerateSwap(io::Printer* printer) { 1264 // Generate the Swap member function. 1265 printer->Print("void $classname$::Swap($classname$* other) {\n", 1266 "classname", classname_); 1267 printer->Indent(); 1268 printer->Print("if (other != this) {\n"); 1269 printer->Indent(); 1270 1271 if (HasGeneratedMethods(descriptor_->file())) { 1272 for (int i = 0; i < descriptor_->field_count(); i++) { 1273 const FieldDescriptor* field = descriptor_->field(i); 1274 field_generators_.get(field).GenerateSwappingCode(printer); 1275 } 1276 1277 for (int i = 0; i < (descriptor_->field_count() + 31) / 32; ++i) { 1278 printer->Print("std::swap(_has_bits_[$i$], other->_has_bits_[$i$]);\n", 1279 "i", SimpleItoa(i)); 1280 } 1281 1282 if (HasUnknownFields(descriptor_->file())) { 1283 printer->Print("_unknown_fields_.Swap(&other->_unknown_fields_);\n"); 1284 } 1285 printer->Print("std::swap(_cached_size_, other->_cached_size_);\n"); 1286 if (descriptor_->extension_range_count() > 0) { 1287 printer->Print("_extensions_.Swap(&other->_extensions_);\n"); 1288 } 1289 } else { 1290 printer->Print("GetReflection()->Swap(this, other);"); 1291 } 1292 1293 printer->Outdent(); 1294 printer->Print("}\n"); 1295 printer->Outdent(); 1296 printer->Print("}\n"); 1297} 1298 1299void MessageGenerator:: 1300GenerateMergeFrom(io::Printer* printer) { 1301 if (HasDescriptorMethods(descriptor_->file())) { 1302 // Generate the generalized MergeFrom (aka that which takes in the Message 1303 // base class as a parameter). 1304 printer->Print( 1305 "void $classname$::MergeFrom(const ::google::protobuf::Message& from) {\n" 1306 " GOOGLE_CHECK_NE(&from, this);\n", 1307 "classname", classname_); 1308 printer->Indent(); 1309 1310 // Cast the message to the proper type. If we find that the message is 1311 // *not* of the proper type, we can still call Merge via the reflection 1312 // system, as the GOOGLE_CHECK above ensured that we have the same descriptor 1313 // for each message. 1314 printer->Print( 1315 "const $classname$* source =\n" 1316 " ::google::protobuf::internal::dynamic_cast_if_available<const $classname$*>(\n" 1317 " &from);\n" 1318 "if (source == NULL) {\n" 1319 " ::google::protobuf::internal::ReflectionOps::Merge(from, this);\n" 1320 "} else {\n" 1321 " MergeFrom(*source);\n" 1322 "}\n", 1323 "classname", classname_); 1324 1325 printer->Outdent(); 1326 printer->Print("}\n\n"); 1327 } else { 1328 // Generate CheckTypeAndMergeFrom(). 1329 printer->Print( 1330 "void $classname$::CheckTypeAndMergeFrom(\n" 1331 " const ::google::protobuf::MessageLite& from) {\n" 1332 " MergeFrom(*::google::protobuf::down_cast<const $classname$*>(&from));\n" 1333 "}\n" 1334 "\n", 1335 "classname", classname_); 1336 } 1337 1338 // Generate the class-specific MergeFrom, which avoids the GOOGLE_CHECK and cast. 1339 printer->Print( 1340 "void $classname$::MergeFrom(const $classname$& from) {\n" 1341 " GOOGLE_CHECK_NE(&from, this);\n", 1342 "classname", classname_); 1343 printer->Indent(); 1344 1345 // Merge Repeated fields. These fields do not require a 1346 // check as we can simply iterate over them. 1347 for (int i = 0; i < descriptor_->field_count(); ++i) { 1348 const FieldDescriptor* field = descriptor_->field(i); 1349 1350 if (field->is_repeated()) { 1351 field_generators_.get(field).GenerateMergingCode(printer); 1352 } 1353 } 1354 1355 // Merge Optional and Required fields (after a _has_bit check). 1356 int last_index = -1; 1357 1358 for (int i = 0; i < descriptor_->field_count(); ++i) { 1359 const FieldDescriptor* field = descriptor_->field(i); 1360 1361 if (!field->is_repeated()) { 1362 // See above in GenerateClear for an explanation of this. 1363 if (i / 8 != last_index / 8 || last_index < 0) { 1364 if (last_index >= 0) { 1365 printer->Outdent(); 1366 printer->Print("}\n"); 1367 } 1368 printer->Print( 1369 "if (from._has_bits_[$index$ / 32] & (0xffu << ($index$ % 32))) {\n", 1370 "index", SimpleItoa(field->index())); 1371 printer->Indent(); 1372 } 1373 1374 last_index = i; 1375 1376 printer->Print( 1377 "if (from.has_$name$()) {\n", 1378 "name", FieldName(field)); 1379 printer->Indent(); 1380 1381 field_generators_.get(field).GenerateMergingCode(printer); 1382 1383 printer->Outdent(); 1384 printer->Print("}\n"); 1385 } 1386 } 1387 1388 if (last_index >= 0) { 1389 printer->Outdent(); 1390 printer->Print("}\n"); 1391 } 1392 1393 if (descriptor_->extension_range_count() > 0) { 1394 printer->Print("_extensions_.MergeFrom(from._extensions_);\n"); 1395 } 1396 1397 if (HasUnknownFields(descriptor_->file())) { 1398 printer->Print( 1399 "mutable_unknown_fields()->MergeFrom(from.unknown_fields());\n"); 1400 } 1401 1402 printer->Outdent(); 1403 printer->Print("}\n"); 1404} 1405 1406void MessageGenerator:: 1407GenerateCopyFrom(io::Printer* printer) { 1408 if (HasDescriptorMethods(descriptor_->file())) { 1409 // Generate the generalized CopyFrom (aka that which takes in the Message 1410 // base class as a parameter). 1411 printer->Print( 1412 "void $classname$::CopyFrom(const ::google::protobuf::Message& from) {\n", 1413 "classname", classname_); 1414 printer->Indent(); 1415 1416 printer->Print( 1417 "if (&from == this) return;\n" 1418 "Clear();\n" 1419 "MergeFrom(from);\n"); 1420 1421 printer->Outdent(); 1422 printer->Print("}\n\n"); 1423 } 1424 1425 // Generate the class-specific CopyFrom. 1426 printer->Print( 1427 "void $classname$::CopyFrom(const $classname$& from) {\n", 1428 "classname", classname_); 1429 printer->Indent(); 1430 1431 printer->Print( 1432 "if (&from == this) return;\n" 1433 "Clear();\n" 1434 "MergeFrom(from);\n"); 1435 1436 printer->Outdent(); 1437 printer->Print("}\n"); 1438} 1439 1440void MessageGenerator:: 1441GenerateMergeFromCodedStream(io::Printer* printer) { 1442 if (descriptor_->options().message_set_wire_format()) { 1443 // Special-case MessageSet. 1444 printer->Print( 1445 "bool $classname$::MergePartialFromCodedStream(\n" 1446 " ::google::protobuf::io::CodedInputStream* input) {\n", 1447 "classname", classname_); 1448 1449 PrintHandlingOptionalStaticInitializers( 1450 descriptor_->file(), printer, 1451 // With static initializers. 1452 " return _extensions_.ParseMessageSet(input, default_instance_,\n" 1453 " mutable_unknown_fields());\n", 1454 // Without. 1455 " return _extensions_.ParseMessageSet(input, &default_instance(),\n" 1456 " mutable_unknown_fields());\n", 1457 // Vars. 1458 "classname", classname_); 1459 1460 printer->Print( 1461 "}\n"); 1462 return; 1463 } 1464 1465 printer->Print( 1466 "bool $classname$::MergePartialFromCodedStream(\n" 1467 " ::google::protobuf::io::CodedInputStream* input) {\n" 1468 "#define DO_(EXPRESSION) if (!(EXPRESSION)) return false\n" 1469 " ::google::protobuf::uint32 tag;\n" 1470 " while ((tag = input->ReadTag()) != 0) {\n", 1471 "classname", classname_); 1472 1473 printer->Indent(); 1474 printer->Indent(); 1475 1476 if (descriptor_->field_count() > 0) { 1477 // We don't even want to print the switch() if we have no fields because 1478 // MSVC dislikes switch() statements that contain only a default value. 1479 1480 // Note: If we just switched on the tag rather than the field number, we 1481 // could avoid the need for the if() to check the wire type at the beginning 1482 // of each case. However, this is actually a bit slower in practice as it 1483 // creates a jump table that is 8x larger and sparser, and meanwhile the 1484 // if()s are highly predictable. 1485 printer->Print( 1486 "switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) {\n"); 1487 1488 printer->Indent(); 1489 1490 scoped_array<const FieldDescriptor*> ordered_fields( 1491 SortFieldsByNumber(descriptor_)); 1492 1493 for (int i = 0; i < descriptor_->field_count(); i++) { 1494 const FieldDescriptor* field = ordered_fields[i]; 1495 1496 PrintFieldComment(printer, field); 1497 1498 printer->Print( 1499 "case $number$: {\n", 1500 "number", SimpleItoa(field->number())); 1501 printer->Indent(); 1502 const FieldGenerator& field_generator = field_generators_.get(field); 1503 1504 // Emit code to parse the common, expected case. 1505 printer->Print( 1506 "if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) ==\n" 1507 " ::google::protobuf::internal::WireFormatLite::WIRETYPE_$wiretype$) {\n", 1508 "wiretype", kWireTypeNames[WireFormat::WireTypeForField(field)]); 1509 1510 if (i > 0 || (field->is_repeated() && !field->options().packed())) { 1511 printer->Print( 1512 " parse_$name$:\n", 1513 "name", field->name()); 1514 } 1515 1516 printer->Indent(); 1517 if (field->options().packed()) { 1518 field_generator.GenerateMergeFromCodedStreamWithPacking(printer); 1519 } else { 1520 field_generator.GenerateMergeFromCodedStream(printer); 1521 } 1522 printer->Outdent(); 1523 1524 // Emit code to parse unexpectedly packed or unpacked values. 1525 if (field->is_packable() && field->options().packed()) { 1526 printer->Print( 1527 "} else if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag)\n" 1528 " == ::google::protobuf::internal::WireFormatLite::\n" 1529 " WIRETYPE_$wiretype$) {\n", 1530 "wiretype", 1531 kWireTypeNames[WireFormat::WireTypeForFieldType(field->type())]); 1532 printer->Indent(); 1533 field_generator.GenerateMergeFromCodedStream(printer); 1534 printer->Outdent(); 1535 } else if (field->is_packable() && !field->options().packed()) { 1536 printer->Print( 1537 "} else if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag)\n" 1538 " == ::google::protobuf::internal::WireFormatLite::\n" 1539 " WIRETYPE_LENGTH_DELIMITED) {\n"); 1540 printer->Indent(); 1541 field_generator.GenerateMergeFromCodedStreamWithPacking(printer); 1542 printer->Outdent(); 1543 } 1544 1545 printer->Print( 1546 "} else {\n" 1547 " goto handle_uninterpreted;\n" 1548 "}\n"); 1549 1550 // switch() is slow since it can't be predicted well. Insert some if()s 1551 // here that attempt to predict the next tag. 1552 if (field->is_repeated() && !field->options().packed()) { 1553 // Expect repeats of this field. 1554 printer->Print( 1555 "if (input->ExpectTag($tag$)) goto parse_$name$;\n", 1556 "tag", SimpleItoa(WireFormat::MakeTag(field)), 1557 "name", field->name()); 1558 } 1559 1560 if (i + 1 < descriptor_->field_count()) { 1561 // Expect the next field in order. 1562 const FieldDescriptor* next_field = ordered_fields[i + 1]; 1563 printer->Print( 1564 "if (input->ExpectTag($next_tag$)) goto parse_$next_name$;\n", 1565 "next_tag", SimpleItoa(WireFormat::MakeTag(next_field)), 1566 "next_name", next_field->name()); 1567 } else { 1568 // Expect EOF. 1569 // TODO(kenton): Expect group end-tag? 1570 printer->Print( 1571 "if (input->ExpectAtEnd()) return true;\n"); 1572 } 1573 1574 printer->Print( 1575 "break;\n"); 1576 1577 printer->Outdent(); 1578 printer->Print("}\n\n"); 1579 } 1580 1581 printer->Print( 1582 "default: {\n" 1583 "handle_uninterpreted:\n"); 1584 printer->Indent(); 1585 } 1586 1587 // Is this an end-group tag? If so, this must be the end of the message. 1588 printer->Print( 1589 "if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) ==\n" 1590 " ::google::protobuf::internal::WireFormatLite::WIRETYPE_END_GROUP) {\n" 1591 " return true;\n" 1592 "}\n"); 1593 1594 // Handle extension ranges. 1595 if (descriptor_->extension_range_count() > 0) { 1596 printer->Print( 1597 "if ("); 1598 for (int i = 0; i < descriptor_->extension_range_count(); i++) { 1599 const Descriptor::ExtensionRange* range = 1600 descriptor_->extension_range(i); 1601 if (i > 0) printer->Print(" ||\n "); 1602 1603 uint32 start_tag = WireFormatLite::MakeTag( 1604 range->start, static_cast<WireFormatLite::WireType>(0)); 1605 uint32 end_tag = WireFormatLite::MakeTag( 1606 range->end, static_cast<WireFormatLite::WireType>(0)); 1607 1608 if (range->end > FieldDescriptor::kMaxNumber) { 1609 printer->Print( 1610 "($start$u <= tag)", 1611 "start", SimpleItoa(start_tag)); 1612 } else { 1613 printer->Print( 1614 "($start$u <= tag && tag < $end$u)", 1615 "start", SimpleItoa(start_tag), 1616 "end", SimpleItoa(end_tag)); 1617 } 1618 } 1619 printer->Print(") {\n"); 1620 if (HasUnknownFields(descriptor_->file())) { 1621 PrintHandlingOptionalStaticInitializers( 1622 descriptor_->file(), printer, 1623 // With static initializers. 1624 " DO_(_extensions_.ParseField(tag, input, default_instance_,\n" 1625 " mutable_unknown_fields()));\n", 1626 // Without. 1627 " DO_(_extensions_.ParseField(tag, input, &default_instance(),\n" 1628 " mutable_unknown_fields()));\n"); 1629 } else { 1630 PrintHandlingOptionalStaticInitializers( 1631 descriptor_->file(), printer, 1632 // With static initializers. 1633 " DO_(_extensions_.ParseField(tag, input, default_instance_, NULL));\n", 1634 // Without. 1635 " DO_(_extensions_.ParseField(tag, input, &default_instance(), NULL));\n"); 1636 } 1637 printer->Print( 1638 " continue;\n" 1639 "}\n"); 1640 } 1641 1642 // We really don't recognize this tag. Skip it. 1643 if (HasUnknownFields(descriptor_->file())) { 1644 printer->Print( 1645 "DO_(::google::protobuf::internal::WireFormatLite::SkipField(input, tag, mutable_unknown_fields()));\n"); 1646 } else { 1647 printer->Print( 1648 "DO_(::google::protobuf::internal::WireFormatLite::SkipField(input, tag, NULL));\n"); 1649 } 1650 1651 if (descriptor_->field_count() > 0) { 1652 printer->Print("break;\n"); 1653 printer->Outdent(); 1654 printer->Print("}\n"); // default: 1655 printer->Outdent(); 1656 printer->Print("}\n"); // switch 1657 } 1658 1659 printer->Outdent(); 1660 printer->Outdent(); 1661 printer->Print( 1662 " }\n" // while 1663 " return true;\n" 1664 "#undef DO_\n" 1665 "}\n"); 1666} 1667 1668void MessageGenerator::GenerateSerializeOneField( 1669 io::Printer* printer, const FieldDescriptor* field, bool to_array) { 1670 PrintFieldComment(printer, field); 1671 1672 if (!field->is_repeated()) { 1673 printer->Print( 1674 "if (has_$name$()) {\n", 1675 "name", FieldName(field)); 1676 printer->Indent(); 1677 } 1678 1679 if (to_array) { 1680 field_generators_.get(field).GenerateSerializeWithCachedSizesToArray( 1681 printer); 1682 } else { 1683 field_generators_.get(field).GenerateSerializeWithCachedSizes(printer); 1684 } 1685 1686 if (!field->is_repeated()) { 1687 printer->Outdent(); 1688 printer->Print("}\n"); 1689 } 1690 printer->Print("\n"); 1691} 1692 1693void MessageGenerator::GenerateSerializeOneExtensionRange( 1694 io::Printer* printer, const Descriptor::ExtensionRange* range, 1695 bool to_array) { 1696 map<string, string> vars; 1697 vars["start"] = SimpleItoa(range->start); 1698 vars["end"] = SimpleItoa(range->end); 1699 printer->Print(vars, 1700 "// Extension range [$start$, $end$)\n"); 1701 if (to_array) { 1702 printer->Print(vars, 1703 "target = _extensions_.SerializeWithCachedSizesToArray(\n" 1704 " $start$, $end$, target);\n\n"); 1705 } else { 1706 printer->Print(vars, 1707 "_extensions_.SerializeWithCachedSizes(\n" 1708 " $start$, $end$, output);\n\n"); 1709 } 1710} 1711 1712void MessageGenerator:: 1713GenerateSerializeWithCachedSizes(io::Printer* printer) { 1714 if (descriptor_->options().message_set_wire_format()) { 1715 // Special-case MessageSet. 1716 printer->Print( 1717 "void $classname$::SerializeWithCachedSizes(\n" 1718 " ::google::protobuf::io::CodedOutputStream* output) const {\n" 1719 " _extensions_.SerializeMessageSetWithCachedSizes(output);\n", 1720 "classname", classname_); 1721 if (HasUnknownFields(descriptor_->file())) { 1722 printer->Print( 1723 " ::google::protobuf::internal::WireFormatLite::SerializeUnknownMessageSetItems(\n" 1724 " unknown_fields(), output);\n"); 1725 } 1726 printer->Print( 1727 "}\n"); 1728 return; 1729 } 1730 1731 printer->Print( 1732 "void $classname$::SerializeWithCachedSizes(\n" 1733 " ::google::protobuf::io::CodedOutputStream* output) const {\n", 1734 "classname", classname_); 1735 printer->Indent(); 1736 1737 GenerateSerializeWithCachedSizesBody(printer, false); 1738 1739 printer->Outdent(); 1740 printer->Print( 1741 "}\n"); 1742} 1743 1744void MessageGenerator:: 1745GenerateSerializeWithCachedSizesToArray(io::Printer* printer) { 1746 if (descriptor_->options().message_set_wire_format()) { 1747 // Special-case MessageSet. 1748 printer->Print( 1749 "::google::protobuf::uint8* $classname$::SerializeWithCachedSizesToArray(\n" 1750 " ::google::protobuf::uint8* target) const {\n" 1751 " target =\n" 1752 " _extensions_.SerializeMessageSetWithCachedSizesToArray(target);\n", 1753 "classname", classname_); 1754 if (HasUnknownFields(descriptor_->file())) { 1755 printer->Print( 1756 " target = ::google::protobuf::internal::WireFormatLite::\n" 1757 " SerializeUnknownMessageSetItemsToArray(\n" 1758 " unknown_fields(), target);\n"); 1759 } 1760 printer->Print( 1761 " return target;\n" 1762 "}\n"); 1763 return; 1764 } 1765 1766 printer->Print( 1767 "::google::protobuf::uint8* $classname$::SerializeWithCachedSizesToArray(\n" 1768 " ::google::protobuf::uint8* target) const {\n", 1769 "classname", classname_); 1770 printer->Indent(); 1771 1772 GenerateSerializeWithCachedSizesBody(printer, true); 1773 1774 printer->Outdent(); 1775 printer->Print( 1776 " return target;\n" 1777 "}\n"); 1778} 1779 1780void MessageGenerator:: 1781GenerateSerializeWithCachedSizesBody(io::Printer* printer, bool to_array) { 1782 scoped_array<const FieldDescriptor*> ordered_fields( 1783 SortFieldsByNumber(descriptor_)); 1784 1785 vector<const Descriptor::ExtensionRange*> sorted_extensions; 1786 for (int i = 0; i < descriptor_->extension_range_count(); ++i) { 1787 sorted_extensions.push_back(descriptor_->extension_range(i)); 1788 } 1789 sort(sorted_extensions.begin(), sorted_extensions.end(), 1790 ExtensionRangeSorter()); 1791 1792 // Merge the fields and the extension ranges, both sorted by field number. 1793 int i, j; 1794 for (i = 0, j = 0; 1795 i < descriptor_->field_count() || j < sorted_extensions.size(); 1796 ) { 1797 if (i == descriptor_->field_count()) { 1798 GenerateSerializeOneExtensionRange(printer, 1799 sorted_extensions[j++], 1800 to_array); 1801 } else if (j == sorted_extensions.size()) { 1802 GenerateSerializeOneField(printer, ordered_fields[i++], to_array); 1803 } else if (ordered_fields[i]->number() < sorted_extensions[j]->start) { 1804 GenerateSerializeOneField(printer, ordered_fields[i++], to_array); 1805 } else { 1806 GenerateSerializeOneExtensionRange(printer, 1807 sorted_extensions[j++], 1808 to_array); 1809 } 1810 } 1811 1812 if (HasUnknownFields(descriptor_->file())) { 1813 printer->Print("if (!unknown_fields().empty()) {\n"); 1814 printer->Indent(); 1815 if (to_array) { 1816 printer->Print( 1817 "target = " 1818 "::google::protobuf::internal::WireFormatLite::SerializeUnknownFieldsToArray(\n" 1819 " unknown_fields(), target);\n"); 1820 } else { 1821 printer->Print( 1822 "::google::protobuf::internal::WireFormatLite::SerializeUnknownFields(\n" 1823 " unknown_fields(), output);\n"); 1824 } 1825 printer->Outdent(); 1826 1827 printer->Print( 1828 "}\n"); 1829 } 1830} 1831 1832void MessageGenerator:: 1833GenerateByteSize(io::Printer* printer) { 1834 if (descriptor_->options().message_set_wire_format()) { 1835 // Special-case MessageSet. 1836 printer->Print( 1837 "int $classname$::ByteSize() const {\n" 1838 " int total_size = _extensions_.MessageSetByteSize();\n", 1839 "classname", classname_); 1840 if (HasUnknownFields(descriptor_->file())) { 1841 printer->Print( 1842 " total_size += ::google::protobuf::internal::WireFormatLite::\n" 1843 " ComputeUnknownMessageSetItemsSize(unknown_fields());\n"); 1844 } 1845 printer->Print( 1846 " GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN();\n" 1847 " _cached_size_ = total_size;\n" 1848 " GOOGLE_SAFE_CONCURRENT_WRITES_END();\n" 1849 " return total_size;\n" 1850 "}\n"); 1851 return; 1852 } 1853 1854 printer->Print( 1855 "int $classname$::ByteSize() const {\n", 1856 "classname", classname_); 1857 printer->Indent(); 1858 printer->Print( 1859 "int total_size = 0;\n" 1860 "\n"); 1861 1862 int last_index = -1; 1863 1864 for (int i = 0; i < descriptor_->field_count(); i++) { 1865 const FieldDescriptor* field = descriptor_->field(i); 1866 1867 if (!field->is_repeated()) { 1868 // See above in GenerateClear for an explanation of this. 1869 // TODO(kenton): Share code? Unclear how to do so without 1870 // over-engineering. 1871 if ((i / 8) != (last_index / 8) || 1872 last_index < 0) { 1873 if (last_index >= 0) { 1874 printer->Outdent(); 1875 printer->Print("}\n"); 1876 } 1877 printer->Print( 1878 "if (_has_bits_[$index$ / 32] & (0xffu << ($index$ % 32))) {\n", 1879 "index", SimpleItoa(field->index())); 1880 printer->Indent(); 1881 } 1882 last_index = i; 1883 1884 PrintFieldComment(printer, field); 1885 1886 printer->Print( 1887 "if (has_$name$()) {\n", 1888 "name", FieldName(field)); 1889 printer->Indent(); 1890 1891 field_generators_.get(field).GenerateByteSize(printer); 1892 1893 printer->Outdent(); 1894 printer->Print( 1895 "}\n" 1896 "\n"); 1897 } 1898 } 1899 1900 if (last_index >= 0) { 1901 printer->Outdent(); 1902 printer->Print("}\n"); 1903 } 1904 1905 // Repeated fields don't use _has_bits_ so we count them in a separate 1906 // pass. 1907 for (int i = 0; i < descriptor_->field_count(); i++) { 1908 const FieldDescriptor* field = descriptor_->field(i); 1909 1910 if (field->is_repeated()) { 1911 PrintFieldComment(printer, field); 1912 field_generators_.get(field).GenerateByteSize(printer); 1913 printer->Print("\n"); 1914 } 1915 } 1916 1917 if (descriptor_->extension_range_count() > 0) { 1918 printer->Print( 1919 "total_size += _extensions_.ByteSize();\n" 1920 "\n"); 1921 } 1922 1923 if (HasUnknownFields(descriptor_->file())) { 1924 printer->Print("if (!unknown_fields().empty()) {\n"); 1925 printer->Indent(); 1926 printer->Print( 1927 "total_size +=\n" 1928 " ::google::protobuf::internal::WireFormatLite::ComputeUnknownFieldsSize(\n" 1929 " unknown_fields());\n"); 1930 printer->Outdent(); 1931 printer->Print("}\n"); 1932 } 1933 1934 // We update _cached_size_ even though this is a const method. In theory, 1935 // this is not thread-compatible, because concurrent writes have undefined 1936 // results. In practice, since any concurrent writes will be writing the 1937 // exact same value, it works on all common processors. In a future version 1938 // of C++, _cached_size_ should be made into an atomic<int>. 1939 printer->Print( 1940 "GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN();\n" 1941 "_cached_size_ = total_size;\n" 1942 "GOOGLE_SAFE_CONCURRENT_WRITES_END();\n" 1943 "return total_size;\n"); 1944 1945 printer->Outdent(); 1946 printer->Print("}\n"); 1947} 1948 1949void MessageGenerator:: 1950GenerateIsInitialized(io::Printer* printer) { 1951 printer->Print( 1952 "bool $classname$::IsInitialized() const {\n", 1953 "classname", classname_); 1954 printer->Indent(); 1955 1956 // Check that all required fields in this message are set. We can do this 1957 // most efficiently by checking 32 "has bits" at a time. 1958 int has_bits_array_size = (descriptor_->field_count() + 31) / 32; 1959 for (int i = 0; i < has_bits_array_size; i++) { 1960 uint32 mask = 0; 1961 for (int bit = 0; bit < 32; bit++) { 1962 int index = i * 32 + bit; 1963 if (index >= descriptor_->field_count()) break; 1964 const FieldDescriptor* field = descriptor_->field(index); 1965 1966 if (field->is_required()) { 1967 mask |= 1 << bit; 1968 } 1969 } 1970 1971 if (mask != 0) { 1972 char buffer[kFastToBufferSize]; 1973 printer->Print( 1974 "if ((_has_bits_[$i$] & 0x$mask$) != 0x$mask$) return false;\n", 1975 "i", SimpleItoa(i), 1976 "mask", FastHex32ToBuffer(mask, buffer)); 1977 } 1978 } 1979 1980 // Now check that all embedded messages are initialized. 1981 printer->Print("\n"); 1982 for (int i = 0; i < descriptor_->field_count(); i++) { 1983 const FieldDescriptor* field = descriptor_->field(i); 1984 if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE && 1985 !ShouldIgnoreRequiredFieldCheck(field) && 1986 HasRequiredFields(field->message_type())) { 1987 if (field->is_repeated()) { 1988 printer->Print( 1989 "for (int i = 0; i < $name$_size(); i++) {\n" 1990 " if (!this->$name$(i).IsInitialized()) return false;\n" 1991 "}\n", 1992 "name", FieldName(field)); 1993 } else { 1994 printer->Print( 1995 "if (has_$name$()) {\n" 1996 " if (!this->$name$().IsInitialized()) return false;\n" 1997 "}\n", 1998 "name", FieldName(field)); 1999 } 2000 } 2001 } 2002 2003 if (descriptor_->extension_range_count() > 0) { 2004 printer->Print( 2005 "\n" 2006 "if (!_extensions_.IsInitialized()) return false;"); 2007 } 2008 2009 printer->Outdent(); 2010 printer->Print( 2011 " return true;\n" 2012 "}\n"); 2013} 2014 2015 2016} // namespace cpp 2017} // namespace compiler 2018} // namespace protobuf 2019} // namespace google 2020