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 (at) google.com (Kenton Varda) 32 // Based on original Protocol Buffers design by 33 // Sanjay Ghemawat, Jeff Dean, and others. 34 35 #include <google/protobuf/stubs/hash.h> 36 #include <map> 37 #include <set> 38 #include <vector> 39 #include <algorithm> 40 #include <limits> 41 42 #include <google/protobuf/descriptor.h> 43 #include <google/protobuf/descriptor_database.h> 44 #include <google/protobuf/descriptor.pb.h> 45 #include <google/protobuf/text_format.h> 46 #include <google/protobuf/unknown_field_set.h> 47 #include <google/protobuf/wire_format.h> 48 #include <google/protobuf/io/coded_stream.h> 49 #include <google/protobuf/io/zero_copy_stream_impl.h> 50 #include <google/protobuf/stubs/common.h> 51 #include <google/protobuf/stubs/once.h> 52 #include <google/protobuf/stubs/strutil.h> 53 #include <google/protobuf/stubs/substitute.h> 54 #include <google/protobuf/stubs/map-util.h> 55 #include <google/protobuf/stubs/stl_util-inl.h> 56 57 #undef PACKAGE // autoheader #defines this. :( 58 59 namespace google { 60 namespace protobuf { 61 62 const FieldDescriptor::CppType 63 FieldDescriptor::kTypeToCppTypeMap[MAX_TYPE + 1] = { 64 static_cast<CppType>(0), // 0 is reserved for errors 65 66 CPPTYPE_DOUBLE, // TYPE_DOUBLE 67 CPPTYPE_FLOAT, // TYPE_FLOAT 68 CPPTYPE_INT64, // TYPE_INT64 69 CPPTYPE_UINT64, // TYPE_UINT64 70 CPPTYPE_INT32, // TYPE_INT32 71 CPPTYPE_UINT64, // TYPE_FIXED64 72 CPPTYPE_UINT32, // TYPE_FIXED32 73 CPPTYPE_BOOL, // TYPE_BOOL 74 CPPTYPE_STRING, // TYPE_STRING 75 CPPTYPE_MESSAGE, // TYPE_GROUP 76 CPPTYPE_MESSAGE, // TYPE_MESSAGE 77 CPPTYPE_STRING, // TYPE_BYTES 78 CPPTYPE_UINT32, // TYPE_UINT32 79 CPPTYPE_ENUM, // TYPE_ENUM 80 CPPTYPE_INT32, // TYPE_SFIXED32 81 CPPTYPE_INT64, // TYPE_SFIXED64 82 CPPTYPE_INT32, // TYPE_SINT32 83 CPPTYPE_INT64, // TYPE_SINT64 84 }; 85 86 const char * const FieldDescriptor::kTypeToName[MAX_TYPE + 1] = { 87 "ERROR", // 0 is reserved for errors 88 89 "double", // TYPE_DOUBLE 90 "float", // TYPE_FLOAT 91 "int64", // TYPE_INT64 92 "uint64", // TYPE_UINT64 93 "int32", // TYPE_INT32 94 "fixed64", // TYPE_FIXED64 95 "fixed32", // TYPE_FIXED32 96 "bool", // TYPE_BOOL 97 "string", // TYPE_STRING 98 "group", // TYPE_GROUP 99 "message", // TYPE_MESSAGE 100 "bytes", // TYPE_BYTES 101 "uint32", // TYPE_UINT32 102 "enum", // TYPE_ENUM 103 "sfixed32", // TYPE_SFIXED32 104 "sfixed64", // TYPE_SFIXED64 105 "sint32", // TYPE_SINT32 106 "sint64", // TYPE_SINT64 107 }; 108 109 const char * const FieldDescriptor::kLabelToName[MAX_LABEL + 1] = { 110 "ERROR", // 0 is reserved for errors 111 112 "optional", // LABEL_OPTIONAL 113 "required", // LABEL_REQUIRED 114 "repeated", // LABEL_REPEATED 115 }; 116 117 #ifndef _MSC_VER // MSVC doesn't need these and won't even accept them. 118 const int FieldDescriptor::kMaxNumber; 119 const int FieldDescriptor::kFirstReservedNumber; 120 const int FieldDescriptor::kLastReservedNumber; 121 #endif 122 123 namespace { 124 125 const string kEmptyString; 126 127 string ToCamelCase(const string& input) { 128 bool capitalize_next = false; 129 string result; 130 result.reserve(input.size()); 131 132 for (int i = 0; i < input.size(); i++) { 133 if (input[i] == '_') { 134 capitalize_next = true; 135 } else if (capitalize_next) { 136 // Note: I distrust ctype.h due to locales. 137 if ('a' <= input[i] && input[i] <= 'z') { 138 result.push_back(input[i] - 'a' + 'A'); 139 } else { 140 result.push_back(input[i]); 141 } 142 capitalize_next = false; 143 } else { 144 result.push_back(input[i]); 145 } 146 } 147 148 // Lower-case the first letter. 149 if (!result.empty() && 'A' <= result[0] && result[0] <= 'Z') { 150 result[0] = result[0] - 'A' + 'a'; 151 } 152 153 return result; 154 } 155 156 // A DescriptorPool contains a bunch of hash_maps to implement the 157 // various Find*By*() methods. Since hashtable lookups are O(1), it's 158 // most efficient to construct a fixed set of large hash_maps used by 159 // all objects in the pool rather than construct one or more small 160 // hash_maps for each object. 161 // 162 // The keys to these hash_maps are (parent, name) or (parent, number) 163 // pairs. Unfortunately STL doesn't provide hash functions for pair<>, 164 // so we must invent our own. 165 // 166 // TODO(kenton): Use StringPiece rather than const char* in keys? It would 167 // be a lot cleaner but we'd just have to convert it back to const char* 168 // for the open source release. 169 170 typedef pair<const void*, const char*> PointerStringPair; 171 172 struct PointerStringPairEqual { 173 inline bool operator()(const PointerStringPair& a, 174 const PointerStringPair& b) const { 175 return a.first == b.first && strcmp(a.second, b.second) == 0; 176 } 177 }; 178 179 template<typename PairType> 180 struct PointerIntegerPairHash { 181 size_t operator()(const PairType& p) const { 182 // FIXME(kenton): What is the best way to compute this hash? I have 183 // no idea! This seems a bit better than an XOR. 184 return reinterpret_cast<intptr_t>(p.first) * ((1 << 16) - 1) + p.second; 185 } 186 187 // Used only by MSVC and platforms where hash_map is not available. 188 static const size_t bucket_size = 4; 189 static const size_t min_buckets = 8; 190 inline bool operator()(const PairType& a, const PairType& b) const { 191 return a.first < b.first || 192 (a.first == b.first && a.second < b.second); 193 } 194 }; 195 196 typedef pair<const Descriptor*, int> DescriptorIntPair; 197 typedef pair<const EnumDescriptor*, int> EnumIntPair; 198 199 struct PointerStringPairHash { 200 size_t operator()(const PointerStringPair& p) const { 201 // FIXME(kenton): What is the best way to compute this hash? I have 202 // no idea! This seems a bit better than an XOR. 203 hash<const char*> cstring_hash; 204 return reinterpret_cast<intptr_t>(p.first) * ((1 << 16) - 1) + 205 cstring_hash(p.second); 206 } 207 208 // Used only by MSVC and platforms where hash_map is not available. 209 static const size_t bucket_size = 4; 210 static const size_t min_buckets = 8; 211 inline bool operator()(const PointerStringPair& a, 212 const PointerStringPair& b) const { 213 if (a.first < b.first) return true; 214 if (a.first > b.first) return false; 215 return strcmp(a.second, b.second) < 0; 216 } 217 }; 218 219 220 struct Symbol { 221 enum Type { 222 NULL_SYMBOL, MESSAGE, FIELD, ENUM, ENUM_VALUE, SERVICE, METHOD, PACKAGE 223 }; 224 Type type; 225 union { 226 const Descriptor* descriptor; 227 const FieldDescriptor* field_descriptor; 228 const EnumDescriptor* enum_descriptor; 229 const EnumValueDescriptor* enum_value_descriptor; 230 const ServiceDescriptor* service_descriptor; 231 const MethodDescriptor* method_descriptor; 232 const FileDescriptor* package_file_descriptor; 233 }; 234 235 inline Symbol() : type(NULL_SYMBOL) { descriptor = NULL; } 236 inline bool IsNull() const { return type == NULL_SYMBOL; } 237 inline bool IsType() const { 238 return type == MESSAGE || type == ENUM; 239 } 240 inline bool IsAggregate() const { 241 return type == MESSAGE || type == PACKAGE 242 || type == ENUM || type == SERVICE; 243 } 244 245 #define CONSTRUCTOR(TYPE, TYPE_CONSTANT, FIELD) \ 246 inline explicit Symbol(const TYPE* value) { \ 247 type = TYPE_CONSTANT; \ 248 this->FIELD = value; \ 249 } 250 251 CONSTRUCTOR(Descriptor , MESSAGE , descriptor ) 252 CONSTRUCTOR(FieldDescriptor , FIELD , field_descriptor ) 253 CONSTRUCTOR(EnumDescriptor , ENUM , enum_descriptor ) 254 CONSTRUCTOR(EnumValueDescriptor, ENUM_VALUE, enum_value_descriptor ) 255 CONSTRUCTOR(ServiceDescriptor , SERVICE , service_descriptor ) 256 CONSTRUCTOR(MethodDescriptor , METHOD , method_descriptor ) 257 CONSTRUCTOR(FileDescriptor , PACKAGE , package_file_descriptor) 258 #undef CONSTRUCTOR 259 260 const FileDescriptor* GetFile() const { 261 switch (type) { 262 case NULL_SYMBOL: return NULL; 263 case MESSAGE : return descriptor ->file(); 264 case FIELD : return field_descriptor ->file(); 265 case ENUM : return enum_descriptor ->file(); 266 case ENUM_VALUE : return enum_value_descriptor->type()->file(); 267 case SERVICE : return service_descriptor ->file(); 268 case METHOD : return method_descriptor ->service()->file(); 269 case PACKAGE : return package_file_descriptor; 270 } 271 return NULL; 272 } 273 }; 274 275 const Symbol kNullSymbol; 276 277 typedef hash_map<const char*, Symbol, 278 hash<const char*>, streq> 279 SymbolsByNameMap; 280 typedef hash_map<PointerStringPair, Symbol, 281 PointerStringPairHash, PointerStringPairEqual> 282 SymbolsByParentMap; 283 typedef hash_map<const char*, const FileDescriptor*, 284 hash<const char*>, streq> 285 FilesByNameMap; 286 typedef hash_map<PointerStringPair, const FieldDescriptor*, 287 PointerStringPairHash, PointerStringPairEqual> 288 FieldsByNameMap; 289 typedef hash_map<DescriptorIntPair, const FieldDescriptor*, 290 PointerIntegerPairHash<DescriptorIntPair> > 291 FieldsByNumberMap; 292 typedef hash_map<EnumIntPair, const EnumValueDescriptor*, 293 PointerIntegerPairHash<EnumIntPair> > 294 EnumValuesByNumberMap; 295 // This is a map rather than a hash_map, since we use it to iterate 296 // through all the extensions that extend a given Descriptor, and an 297 // ordered data structure that implements lower_bound is convenient 298 // for that. 299 typedef map<DescriptorIntPair, const FieldDescriptor*> 300 ExtensionsGroupedByDescriptorMap; 301 302 } // anonymous namespace 303 304 // =================================================================== 305 // DescriptorPool::Tables 306 307 class DescriptorPool::Tables { 308 public: 309 Tables(); 310 ~Tables(); 311 312 // Checkpoint the state of the tables. Future calls to Rollback() will 313 // return the Tables to this state. This is used when building files, since 314 // some kinds of validation errors cannot be detected until the file's 315 // descriptors have already been added to the tables. BuildFile() calls 316 // Checkpoint() before it starts building and Rollback() if it encounters 317 // an error. 318 void Checkpoint(); 319 320 // Roll back the Tables to the state of the last Checkpoint(), removing 321 // everything that was added after that point. 322 void Rollback(); 323 324 // The stack of files which are currently being built. Used to detect 325 // cyclic dependencies when loading files from a DescriptorDatabase. Not 326 // used when fallback_database_ == NULL. 327 vector<string> pending_files_; 328 329 // A set of files which we have tried to load from the fallback database 330 // and encountered errors. We will not attempt to load them again. 331 // Not used when fallback_database_ == NULL. 332 hash_set<string> known_bad_files_; 333 334 // The set of descriptors for which we've already loaded the full 335 // set of extensions numbers from fallback_database_. 336 hash_set<const Descriptor*> extensions_loaded_from_db_; 337 338 // ----------------------------------------------------------------- 339 // Finding items. 340 341 // Find symbols. This returns a null Symbol (symbol.IsNull() is true) 342 // if not found. 343 inline Symbol FindSymbol(const string& key) const; 344 345 // This implements the body of DescriptorPool::Find*ByName(). It should 346 // really be a private method of DescriptorPool, but that would require 347 // declaring Symbol in descriptor.h, which would drag all kinds of other 348 // stuff into the header. Yay C++. 349 Symbol FindByNameHelper( 350 const DescriptorPool* pool, const string& name) const; 351 352 // These return NULL if not found. 353 inline const FileDescriptor* FindFile(const string& key) const; 354 inline const FieldDescriptor* FindExtension(const Descriptor* extendee, 355 int number); 356 inline void FindAllExtensions(const Descriptor* extendee, 357 vector<const FieldDescriptor*>* out) const; 358 359 // ----------------------------------------------------------------- 360 // Adding items. 361 362 // These add items to the corresponding tables. They return false if 363 // the key already exists in the table. For AddSymbol(), the string passed 364 // in must be one that was constructed using AllocateString(), as it will 365 // be used as a key in the symbols_by_name_ map without copying. 366 bool AddSymbol(const string& full_name, Symbol symbol); 367 bool AddFile(const FileDescriptor* file); 368 bool AddExtension(const FieldDescriptor* field); 369 370 // ----------------------------------------------------------------- 371 // Allocating memory. 372 373 // Allocate an object which will be reclaimed when the pool is 374 // destroyed. Note that the object's destructor will never be called, 375 // so its fields must be plain old data (primitive data types and 376 // pointers). All of the descriptor types are such objects. 377 template<typename Type> Type* Allocate(); 378 379 // Allocate an array of objects which will be reclaimed when the 380 // pool in destroyed. Again, destructors are never called. 381 template<typename Type> Type* AllocateArray(int count); 382 383 // Allocate a string which will be destroyed when the pool is destroyed. 384 // The string is initialized to the given value for convenience. 385 string* AllocateString(const string& value); 386 387 // Allocate a protocol message object. Some older versions of GCC have 388 // trouble understanding explicit template instantiations in some cases, so 389 // in those cases we have to pass a dummy pointer of the right type as the 390 // parameter instead of specifying the type explicitly. 391 template<typename Type> Type* AllocateMessage(Type* dummy = NULL); 392 393 // Allocate a FileDescriptorTables object. 394 FileDescriptorTables* AllocateFileTables(); 395 396 private: 397 vector<string*> strings_; // All strings in the pool. 398 vector<Message*> messages_; // All messages in the pool. 399 vector<FileDescriptorTables*> file_tables_; // All file tables in the pool. 400 vector<void*> allocations_; // All other memory allocated in the pool. 401 402 SymbolsByNameMap symbols_by_name_; 403 FilesByNameMap files_by_name_; 404 ExtensionsGroupedByDescriptorMap extensions_; 405 406 int strings_before_checkpoint_; 407 int messages_before_checkpoint_; 408 int file_tables_before_checkpoint_; 409 int allocations_before_checkpoint_; 410 vector<const char* > symbols_after_checkpoint_; 411 vector<const char* > files_after_checkpoint_; 412 vector<DescriptorIntPair> extensions_after_checkpoint_; 413 414 // Allocate some bytes which will be reclaimed when the pool is 415 // destroyed. 416 void* AllocateBytes(int size); 417 }; 418 419 // Contains tables specific to a particular file. These tables are not 420 // modified once the file has been constructed, so they need not be 421 // protected by a mutex. This makes operations that depend only on the 422 // contents of a single file -- e.g. Descriptor::FindFieldByName() -- 423 // lock-free. 424 // 425 // For historical reasons, the definitions of the methods of 426 // FileDescriptorTables and DescriptorPool::Tables are interleaved below. 427 // These used to be a single class. 428 class FileDescriptorTables { 429 public: 430 FileDescriptorTables(); 431 ~FileDescriptorTables(); 432 433 // Empty table, used with placeholder files. 434 static const FileDescriptorTables kEmpty; 435 436 // ----------------------------------------------------------------- 437 // Finding items. 438 439 // Find symbols. These return a null Symbol (symbol.IsNull() is true) 440 // if not found. 441 inline Symbol FindNestedSymbol(const void* parent, 442 const string& name) const; 443 inline Symbol FindNestedSymbolOfType(const void* parent, 444 const string& name, 445 const Symbol::Type type) const; 446 447 // These return NULL if not found. 448 inline const FieldDescriptor* FindFieldByNumber( 449 const Descriptor* parent, int number) const; 450 inline const FieldDescriptor* FindFieldByLowercaseName( 451 const void* parent, const string& lowercase_name) const; 452 inline const FieldDescriptor* FindFieldByCamelcaseName( 453 const void* parent, const string& camelcase_name) const; 454 inline const EnumValueDescriptor* FindEnumValueByNumber( 455 const EnumDescriptor* parent, int number) const; 456 457 // ----------------------------------------------------------------- 458 // Adding items. 459 460 // These add items to the corresponding tables. They return false if 461 // the key already exists in the table. For AddAliasUnderParent(), the 462 // string passed in must be one that was constructed using AllocateString(), 463 // as it will be used as a key in the symbols_by_parent_ map without copying. 464 bool AddAliasUnderParent(const void* parent, const string& name, 465 Symbol symbol); 466 bool AddFieldByNumber(const FieldDescriptor* field); 467 bool AddEnumValueByNumber(const EnumValueDescriptor* value); 468 469 // Adds the field to the lowercase_name and camelcase_name maps. Never 470 // fails because we allow duplicates; the first field by the name wins. 471 void AddFieldByStylizedNames(const FieldDescriptor* field); 472 473 private: 474 SymbolsByParentMap symbols_by_parent_; 475 FieldsByNameMap fields_by_lowercase_name_; 476 FieldsByNameMap fields_by_camelcase_name_; 477 FieldsByNumberMap fields_by_number_; // Not including extensions. 478 EnumValuesByNumberMap enum_values_by_number_; 479 }; 480 481 DescriptorPool::Tables::Tables() 482 : strings_before_checkpoint_(0), 483 messages_before_checkpoint_(0), 484 allocations_before_checkpoint_(0) {} 485 486 DescriptorPool::Tables::~Tables() { 487 // Note that the deletion order is important, since the destructors of some 488 // messages may refer to objects in allocations_. 489 STLDeleteElements(&messages_); 490 for (int i = 0; i < allocations_.size(); i++) { 491 operator delete(allocations_[i]); 492 } 493 STLDeleteElements(&strings_); 494 STLDeleteElements(&file_tables_); 495 } 496 497 FileDescriptorTables::FileDescriptorTables() {} 498 FileDescriptorTables::~FileDescriptorTables() {} 499 500 const FileDescriptorTables FileDescriptorTables::kEmpty; 501 502 void DescriptorPool::Tables::Checkpoint() { 503 strings_before_checkpoint_ = strings_.size(); 504 messages_before_checkpoint_ = messages_.size(); 505 file_tables_before_checkpoint_ = file_tables_.size(); 506 allocations_before_checkpoint_ = allocations_.size(); 507 508 symbols_after_checkpoint_.clear(); 509 files_after_checkpoint_.clear(); 510 extensions_after_checkpoint_.clear(); 511 } 512 513 void DescriptorPool::Tables::Rollback() { 514 for (int i = 0; i < symbols_after_checkpoint_.size(); i++) { 515 symbols_by_name_.erase(symbols_after_checkpoint_[i]); 516 } 517 for (int i = 0; i < files_after_checkpoint_.size(); i++) { 518 files_by_name_.erase(files_after_checkpoint_[i]); 519 } 520 for (int i = 0; i < extensions_after_checkpoint_.size(); i++) { 521 extensions_.erase(extensions_after_checkpoint_[i]); 522 } 523 524 symbols_after_checkpoint_.clear(); 525 files_after_checkpoint_.clear(); 526 extensions_after_checkpoint_.clear(); 527 528 STLDeleteContainerPointers( 529 strings_.begin() + strings_before_checkpoint_, strings_.end()); 530 STLDeleteContainerPointers( 531 messages_.begin() + messages_before_checkpoint_, messages_.end()); 532 STLDeleteContainerPointers( 533 file_tables_.begin() + file_tables_before_checkpoint_, file_tables_.end()); 534 for (int i = allocations_before_checkpoint_; i < allocations_.size(); i++) { 535 operator delete(allocations_[i]); 536 } 537 538 strings_.resize(strings_before_checkpoint_); 539 messages_.resize(messages_before_checkpoint_); 540 file_tables_.resize(file_tables_before_checkpoint_); 541 allocations_.resize(allocations_before_checkpoint_); 542 } 543 544 // ------------------------------------------------------------------- 545 546 inline Symbol DescriptorPool::Tables::FindSymbol(const string& key) const { 547 const Symbol* result = FindOrNull(symbols_by_name_, key.c_str()); 548 if (result == NULL) { 549 return kNullSymbol; 550 } else { 551 return *result; 552 } 553 } 554 555 inline Symbol FileDescriptorTables::FindNestedSymbol( 556 const void* parent, const string& name) const { 557 const Symbol* result = 558 FindOrNull(symbols_by_parent_, PointerStringPair(parent, name.c_str())); 559 if (result == NULL) { 560 return kNullSymbol; 561 } else { 562 return *result; 563 } 564 } 565 566 inline Symbol FileDescriptorTables::FindNestedSymbolOfType( 567 const void* parent, const string& name, const Symbol::Type type) const { 568 Symbol result = FindNestedSymbol(parent, name); 569 if (result.type != type) return kNullSymbol; 570 return result; 571 } 572 573 Symbol DescriptorPool::Tables::FindByNameHelper( 574 const DescriptorPool* pool, const string& name) const { 575 MutexLockMaybe lock(pool->mutex_); 576 Symbol result = FindSymbol(name); 577 578 if (result.IsNull() && pool->underlay_ != NULL) { 579 // Symbol not found; check the underlay. 580 result = 581 pool->underlay_->tables_->FindByNameHelper(pool->underlay_, name); 582 } 583 584 if (result.IsNull()) { 585 // Symbol still not found, so check fallback database. 586 if (pool->TryFindSymbolInFallbackDatabase(name)) { 587 result = FindSymbol(name); 588 } 589 } 590 591 return result; 592 } 593 594 inline const FileDescriptor* DescriptorPool::Tables::FindFile( 595 const string& key) const { 596 return FindPtrOrNull(files_by_name_, key.c_str()); 597 } 598 599 inline const FieldDescriptor* FileDescriptorTables::FindFieldByNumber( 600 const Descriptor* parent, int number) const { 601 return FindPtrOrNull(fields_by_number_, make_pair(parent, number)); 602 } 603 604 inline const FieldDescriptor* FileDescriptorTables::FindFieldByLowercaseName( 605 const void* parent, const string& lowercase_name) const { 606 return FindPtrOrNull(fields_by_lowercase_name_, 607 PointerStringPair(parent, lowercase_name.c_str())); 608 } 609 610 inline const FieldDescriptor* FileDescriptorTables::FindFieldByCamelcaseName( 611 const void* parent, const string& camelcase_name) const { 612 return FindPtrOrNull(fields_by_camelcase_name_, 613 PointerStringPair(parent, camelcase_name.c_str())); 614 } 615 616 inline const EnumValueDescriptor* FileDescriptorTables::FindEnumValueByNumber( 617 const EnumDescriptor* parent, int number) const { 618 return FindPtrOrNull(enum_values_by_number_, make_pair(parent, number)); 619 } 620 621 inline const FieldDescriptor* DescriptorPool::Tables::FindExtension( 622 const Descriptor* extendee, int number) { 623 return FindPtrOrNull(extensions_, make_pair(extendee, number)); 624 } 625 626 inline void DescriptorPool::Tables::FindAllExtensions( 627 const Descriptor* extendee, vector<const FieldDescriptor*>* out) const { 628 ExtensionsGroupedByDescriptorMap::const_iterator it = 629 extensions_.lower_bound(make_pair(extendee, 0)); 630 for (; it != extensions_.end() && it->first.first == extendee; ++it) { 631 out->push_back(it->second); 632 } 633 } 634 635 // ------------------------------------------------------------------- 636 637 bool DescriptorPool::Tables::AddSymbol( 638 const string& full_name, Symbol symbol) { 639 if (InsertIfNotPresent(&symbols_by_name_, full_name.c_str(), symbol)) { 640 symbols_after_checkpoint_.push_back(full_name.c_str()); 641 return true; 642 } else { 643 return false; 644 } 645 } 646 647 bool FileDescriptorTables::AddAliasUnderParent( 648 const void* parent, const string& name, Symbol symbol) { 649 PointerStringPair by_parent_key(parent, name.c_str()); 650 return InsertIfNotPresent(&symbols_by_parent_, by_parent_key, symbol); 651 } 652 653 bool DescriptorPool::Tables::AddFile(const FileDescriptor* file) { 654 if (InsertIfNotPresent(&files_by_name_, file->name().c_str(), file)) { 655 files_after_checkpoint_.push_back(file->name().c_str()); 656 return true; 657 } else { 658 return false; 659 } 660 } 661 662 void FileDescriptorTables::AddFieldByStylizedNames( 663 const FieldDescriptor* field) { 664 const void* parent; 665 if (field->is_extension()) { 666 if (field->extension_scope() == NULL) { 667 parent = field->file(); 668 } else { 669 parent = field->extension_scope(); 670 } 671 } else { 672 parent = field->containing_type(); 673 } 674 675 PointerStringPair lowercase_key(parent, field->lowercase_name().c_str()); 676 InsertIfNotPresent(&fields_by_lowercase_name_, lowercase_key, field); 677 678 PointerStringPair camelcase_key(parent, field->camelcase_name().c_str()); 679 InsertIfNotPresent(&fields_by_camelcase_name_, camelcase_key, field); 680 } 681 682 bool FileDescriptorTables::AddFieldByNumber(const FieldDescriptor* field) { 683 DescriptorIntPair key(field->containing_type(), field->number()); 684 return InsertIfNotPresent(&fields_by_number_, key, field); 685 } 686 687 bool FileDescriptorTables::AddEnumValueByNumber( 688 const EnumValueDescriptor* value) { 689 EnumIntPair key(value->type(), value->number()); 690 return InsertIfNotPresent(&enum_values_by_number_, key, value); 691 } 692 693 bool DescriptorPool::Tables::AddExtension(const FieldDescriptor* field) { 694 DescriptorIntPair key(field->containing_type(), field->number()); 695 if (InsertIfNotPresent(&extensions_, key, field)) { 696 extensions_after_checkpoint_.push_back(key); 697 return true; 698 } else { 699 return false; 700 } 701 } 702 703 // ------------------------------------------------------------------- 704 705 template<typename Type> 706 Type* DescriptorPool::Tables::Allocate() { 707 return reinterpret_cast<Type*>(AllocateBytes(sizeof(Type))); 708 } 709 710 template<typename Type> 711 Type* DescriptorPool::Tables::AllocateArray(int count) { 712 return reinterpret_cast<Type*>(AllocateBytes(sizeof(Type) * count)); 713 } 714 715 string* DescriptorPool::Tables::AllocateString(const string& value) { 716 string* result = new string(value); 717 strings_.push_back(result); 718 return result; 719 } 720 721 template<typename Type> 722 Type* DescriptorPool::Tables::AllocateMessage(Type* dummy) { 723 Type* result = new Type; 724 messages_.push_back(result); 725 return result; 726 } 727 728 FileDescriptorTables* DescriptorPool::Tables::AllocateFileTables() { 729 FileDescriptorTables* result = new FileDescriptorTables; 730 file_tables_.push_back(result); 731 return result; 732 } 733 734 void* DescriptorPool::Tables::AllocateBytes(int size) { 735 // TODO(kenton): Would it be worthwhile to implement this in some more 736 // sophisticated way? Probably not for the open source release, but for 737 // internal use we could easily plug in one of our existing memory pool 738 // allocators... 739 if (size == 0) return NULL; 740 741 void* result = operator new(size); 742 allocations_.push_back(result); 743 return result; 744 } 745 746 // =================================================================== 747 // DescriptorPool 748 749 DescriptorPool::ErrorCollector::~ErrorCollector() {} 750 751 DescriptorPool::DescriptorPool() 752 : mutex_(NULL), 753 fallback_database_(NULL), 754 default_error_collector_(NULL), 755 underlay_(NULL), 756 tables_(new Tables), 757 enforce_dependencies_(true), 758 allow_unknown_(false) {} 759 760 DescriptorPool::DescriptorPool(DescriptorDatabase* fallback_database, 761 ErrorCollector* error_collector) 762 : mutex_(new Mutex), 763 fallback_database_(fallback_database), 764 default_error_collector_(error_collector), 765 underlay_(NULL), 766 tables_(new Tables), 767 enforce_dependencies_(true), 768 allow_unknown_(false) { 769 } 770 771 DescriptorPool::DescriptorPool(const DescriptorPool* underlay) 772 : mutex_(NULL), 773 fallback_database_(NULL), 774 default_error_collector_(NULL), 775 underlay_(underlay), 776 tables_(new Tables), 777 enforce_dependencies_(true), 778 allow_unknown_(false) {} 779 780 DescriptorPool::~DescriptorPool() { 781 if (mutex_ != NULL) delete mutex_; 782 } 783 784 // DescriptorPool::BuildFile() defined later. 785 // DescriptorPool::BuildFileCollectingErrors() defined later. 786 787 void DescriptorPool::InternalDontEnforceDependencies() { 788 enforce_dependencies_ = false; 789 } 790 791 bool DescriptorPool::InternalIsFileLoaded(const string& filename) const { 792 MutexLockMaybe lock(mutex_); 793 return tables_->FindFile(filename) != NULL; 794 } 795 796 // generated_pool ==================================================== 797 798 namespace { 799 800 801 EncodedDescriptorDatabase* generated_database_ = NULL; 802 DescriptorPool* generated_pool_ = NULL; 803 GOOGLE_PROTOBUF_DECLARE_ONCE(generated_pool_init_); 804 805 void DeleteGeneratedPool() { 806 delete generated_database_; 807 generated_database_ = NULL; 808 delete generated_pool_; 809 generated_pool_ = NULL; 810 } 811 812 void InitGeneratedPool() { 813 generated_database_ = new EncodedDescriptorDatabase; 814 generated_pool_ = new DescriptorPool(generated_database_); 815 816 internal::OnShutdown(&DeleteGeneratedPool); 817 } 818 819 inline void InitGeneratedPoolOnce() { 820 ::google::protobuf::GoogleOnceInit(&generated_pool_init_, &InitGeneratedPool); 821 } 822 823 } // anonymous namespace 824 825 const DescriptorPool* DescriptorPool::generated_pool() { 826 InitGeneratedPoolOnce(); 827 return generated_pool_; 828 } 829 830 DescriptorPool* DescriptorPool::internal_generated_pool() { 831 InitGeneratedPoolOnce(); 832 return generated_pool_; 833 } 834 835 void DescriptorPool::InternalAddGeneratedFile( 836 const void* encoded_file_descriptor, int size) { 837 // So, this function is called in the process of initializing the 838 // descriptors for generated proto classes. Each generated .pb.cc file 839 // has an internal procedure called AddDescriptors() which is called at 840 // process startup, and that function calls this one in order to register 841 // the raw bytes of the FileDescriptorProto representing the file. 842 // 843 // We do not actually construct the descriptor objects right away. We just 844 // hang on to the bytes until they are actually needed. We actually construct 845 // the descriptor the first time one of the following things happens: 846 // * Someone calls a method like descriptor(), GetDescriptor(), or 847 // GetReflection() on the generated types, which requires returning the 848 // descriptor or an object based on it. 849 // * Someone looks up the descriptor in DescriptorPool::generated_pool(). 850 // 851 // Once one of these happens, the DescriptorPool actually parses the 852 // FileDescriptorProto and generates a FileDescriptor (and all its children) 853 // based on it. 854 // 855 // Note that FileDescriptorProto is itself a generated protocol message. 856 // Therefore, when we parse one, we have to be very careful to avoid using 857 // any descriptor-based operations, since this might cause infinite recursion 858 // or deadlock. 859 InitGeneratedPoolOnce(); 860 GOOGLE_CHECK(generated_database_->Add(encoded_file_descriptor, size)); 861 } 862 863 864 // Find*By* methods ================================================== 865 866 // TODO(kenton): There's a lot of repeated code here, but I'm not sure if 867 // there's any good way to factor it out. Think about this some time when 868 // there's nothing more important to do (read: never). 869 870 const FileDescriptor* DescriptorPool::FindFileByName(const string& name) const { 871 MutexLockMaybe lock(mutex_); 872 const FileDescriptor* result = tables_->FindFile(name); 873 if (result != NULL) return result; 874 if (underlay_ != NULL) { 875 const FileDescriptor* result = underlay_->FindFileByName(name); 876 if (result != NULL) return result; 877 } 878 if (TryFindFileInFallbackDatabase(name)) { 879 const FileDescriptor* result = tables_->FindFile(name); 880 if (result != NULL) return result; 881 } 882 return NULL; 883 } 884 885 const FileDescriptor* DescriptorPool::FindFileContainingSymbol( 886 const string& symbol_name) const { 887 MutexLockMaybe lock(mutex_); 888 Symbol result = tables_->FindSymbol(symbol_name); 889 if (!result.IsNull()) return result.GetFile(); 890 if (underlay_ != NULL) { 891 const FileDescriptor* result = 892 underlay_->FindFileContainingSymbol(symbol_name); 893 if (result != NULL) return result; 894 } 895 if (TryFindSymbolInFallbackDatabase(symbol_name)) { 896 Symbol result = tables_->FindSymbol(symbol_name); 897 if (!result.IsNull()) return result.GetFile(); 898 } 899 return NULL; 900 } 901 902 const Descriptor* DescriptorPool::FindMessageTypeByName( 903 const string& name) const { 904 Symbol result = tables_->FindByNameHelper(this, name); 905 return (result.type == Symbol::MESSAGE) ? result.descriptor : NULL; 906 } 907 908 const FieldDescriptor* DescriptorPool::FindFieldByName( 909 const string& name) const { 910 Symbol result = tables_->FindByNameHelper(this, name); 911 if (result.type == Symbol::FIELD && 912 !result.field_descriptor->is_extension()) { 913 return result.field_descriptor; 914 } else { 915 return NULL; 916 } 917 } 918 919 const FieldDescriptor* DescriptorPool::FindExtensionByName( 920 const string& name) const { 921 Symbol result = tables_->FindByNameHelper(this, name); 922 if (result.type == Symbol::FIELD && 923 result.field_descriptor->is_extension()) { 924 return result.field_descriptor; 925 } else { 926 return NULL; 927 } 928 } 929 930 const EnumDescriptor* DescriptorPool::FindEnumTypeByName( 931 const string& name) const { 932 Symbol result = tables_->FindByNameHelper(this, name); 933 return (result.type == Symbol::ENUM) ? result.enum_descriptor : NULL; 934 } 935 936 const EnumValueDescriptor* DescriptorPool::FindEnumValueByName( 937 const string& name) const { 938 Symbol result = tables_->FindByNameHelper(this, name); 939 return (result.type == Symbol::ENUM_VALUE) ? 940 result.enum_value_descriptor : NULL; 941 } 942 943 const ServiceDescriptor* DescriptorPool::FindServiceByName( 944 const string& name) const { 945 Symbol result = tables_->FindByNameHelper(this, name); 946 return (result.type == Symbol::SERVICE) ? result.service_descriptor : NULL; 947 } 948 949 const MethodDescriptor* DescriptorPool::FindMethodByName( 950 const string& name) const { 951 Symbol result = tables_->FindByNameHelper(this, name); 952 return (result.type == Symbol::METHOD) ? result.method_descriptor : NULL; 953 } 954 955 const FieldDescriptor* DescriptorPool::FindExtensionByNumber( 956 const Descriptor* extendee, int number) const { 957 MutexLockMaybe lock(mutex_); 958 const FieldDescriptor* result = tables_->FindExtension(extendee, number); 959 if (result != NULL) { 960 return result; 961 } 962 if (underlay_ != NULL) { 963 const FieldDescriptor* result = 964 underlay_->FindExtensionByNumber(extendee, number); 965 if (result != NULL) return result; 966 } 967 if (TryFindExtensionInFallbackDatabase(extendee, number)) { 968 const FieldDescriptor* result = tables_->FindExtension(extendee, number); 969 if (result != NULL) { 970 return result; 971 } 972 } 973 return NULL; 974 } 975 976 void DescriptorPool::FindAllExtensions( 977 const Descriptor* extendee, vector<const FieldDescriptor*>* out) const { 978 MutexLockMaybe lock(mutex_); 979 980 // Initialize tables_->extensions_ from the fallback database first 981 // (but do this only once per descriptor). 982 if (fallback_database_ != NULL && 983 tables_->extensions_loaded_from_db_.count(extendee) == 0) { 984 vector<int> numbers; 985 if (fallback_database_->FindAllExtensionNumbers(extendee->full_name(), 986 &numbers)) { 987 for (int i = 0; i < numbers.size(); ++i) { 988 int number = numbers[i]; 989 if (tables_->FindExtension(extendee, number) == NULL) { 990 TryFindExtensionInFallbackDatabase(extendee, number); 991 } 992 } 993 tables_->extensions_loaded_from_db_.insert(extendee); 994 } 995 } 996 997 tables_->FindAllExtensions(extendee, out); 998 if (underlay_ != NULL) { 999 underlay_->FindAllExtensions(extendee, out); 1000 } 1001 } 1002 1003 // ------------------------------------------------------------------- 1004 1005 const FieldDescriptor* 1006 Descriptor::FindFieldByNumber(int key) const { 1007 const FieldDescriptor* result = 1008 file()->tables_->FindFieldByNumber(this, key); 1009 if (result == NULL || result->is_extension()) { 1010 return NULL; 1011 } else { 1012 return result; 1013 } 1014 } 1015 1016 const FieldDescriptor* 1017 Descriptor::FindFieldByLowercaseName(const string& key) const { 1018 const FieldDescriptor* result = 1019 file()->tables_->FindFieldByLowercaseName(this, key); 1020 if (result == NULL || result->is_extension()) { 1021 return NULL; 1022 } else { 1023 return result; 1024 } 1025 } 1026 1027 const FieldDescriptor* 1028 Descriptor::FindFieldByCamelcaseName(const string& key) const { 1029 const FieldDescriptor* result = 1030 file()->tables_->FindFieldByCamelcaseName(this, key); 1031 if (result == NULL || result->is_extension()) { 1032 return NULL; 1033 } else { 1034 return result; 1035 } 1036 } 1037 1038 const FieldDescriptor* 1039 Descriptor::FindFieldByName(const string& key) const { 1040 Symbol result = 1041 file()->tables_->FindNestedSymbolOfType(this, key, Symbol::FIELD); 1042 if (!result.IsNull() && !result.field_descriptor->is_extension()) { 1043 return result.field_descriptor; 1044 } else { 1045 return NULL; 1046 } 1047 } 1048 1049 const FieldDescriptor* 1050 Descriptor::FindExtensionByName(const string& key) const { 1051 Symbol result = 1052 file()->tables_->FindNestedSymbolOfType(this, key, Symbol::FIELD); 1053 if (!result.IsNull() && result.field_descriptor->is_extension()) { 1054 return result.field_descriptor; 1055 } else { 1056 return NULL; 1057 } 1058 } 1059 1060 const FieldDescriptor* 1061 Descriptor::FindExtensionByLowercaseName(const string& key) const { 1062 const FieldDescriptor* result = 1063 file()->tables_->FindFieldByLowercaseName(this, key); 1064 if (result == NULL || !result->is_extension()) { 1065 return NULL; 1066 } else { 1067 return result; 1068 } 1069 } 1070 1071 const FieldDescriptor* 1072 Descriptor::FindExtensionByCamelcaseName(const string& key) const { 1073 const FieldDescriptor* result = 1074 file()->tables_->FindFieldByCamelcaseName(this, key); 1075 if (result == NULL || !result->is_extension()) { 1076 return NULL; 1077 } else { 1078 return result; 1079 } 1080 } 1081 1082 const Descriptor* 1083 Descriptor::FindNestedTypeByName(const string& key) const { 1084 Symbol result = 1085 file()->tables_->FindNestedSymbolOfType(this, key, Symbol::MESSAGE); 1086 if (!result.IsNull()) { 1087 return result.descriptor; 1088 } else { 1089 return NULL; 1090 } 1091 } 1092 1093 const EnumDescriptor* 1094 Descriptor::FindEnumTypeByName(const string& key) const { 1095 Symbol result = 1096 file()->tables_->FindNestedSymbolOfType(this, key, Symbol::ENUM); 1097 if (!result.IsNull()) { 1098 return result.enum_descriptor; 1099 } else { 1100 return NULL; 1101 } 1102 } 1103 1104 const EnumValueDescriptor* 1105 Descriptor::FindEnumValueByName(const string& key) const { 1106 Symbol result = 1107 file()->tables_->FindNestedSymbolOfType(this, key, Symbol::ENUM_VALUE); 1108 if (!result.IsNull()) { 1109 return result.enum_value_descriptor; 1110 } else { 1111 return NULL; 1112 } 1113 } 1114 1115 const EnumValueDescriptor* 1116 EnumDescriptor::FindValueByName(const string& key) const { 1117 Symbol result = 1118 file()->tables_->FindNestedSymbolOfType(this, key, Symbol::ENUM_VALUE); 1119 if (!result.IsNull()) { 1120 return result.enum_value_descriptor; 1121 } else { 1122 return NULL; 1123 } 1124 } 1125 1126 const EnumValueDescriptor* 1127 EnumDescriptor::FindValueByNumber(int key) const { 1128 return file()->tables_->FindEnumValueByNumber(this, key); 1129 } 1130 1131 const MethodDescriptor* 1132 ServiceDescriptor::FindMethodByName(const string& key) const { 1133 Symbol result = 1134 file()->tables_->FindNestedSymbolOfType(this, key, Symbol::METHOD); 1135 if (!result.IsNull()) { 1136 return result.method_descriptor; 1137 } else { 1138 return NULL; 1139 } 1140 } 1141 1142 const Descriptor* 1143 FileDescriptor::FindMessageTypeByName(const string& key) const { 1144 Symbol result = tables_->FindNestedSymbolOfType(this, key, Symbol::MESSAGE); 1145 if (!result.IsNull()) { 1146 return result.descriptor; 1147 } else { 1148 return NULL; 1149 } 1150 } 1151 1152 const EnumDescriptor* 1153 FileDescriptor::FindEnumTypeByName(const string& key) const { 1154 Symbol result = tables_->FindNestedSymbolOfType(this, key, Symbol::ENUM); 1155 if (!result.IsNull()) { 1156 return result.enum_descriptor; 1157 } else { 1158 return NULL; 1159 } 1160 } 1161 1162 const EnumValueDescriptor* 1163 FileDescriptor::FindEnumValueByName(const string& key) const { 1164 Symbol result = 1165 tables_->FindNestedSymbolOfType(this, key, Symbol::ENUM_VALUE); 1166 if (!result.IsNull()) { 1167 return result.enum_value_descriptor; 1168 } else { 1169 return NULL; 1170 } 1171 } 1172 1173 const ServiceDescriptor* 1174 FileDescriptor::FindServiceByName(const string& key) const { 1175 Symbol result = tables_->FindNestedSymbolOfType(this, key, Symbol::SERVICE); 1176 if (!result.IsNull()) { 1177 return result.service_descriptor; 1178 } else { 1179 return NULL; 1180 } 1181 } 1182 1183 const FieldDescriptor* 1184 FileDescriptor::FindExtensionByName(const string& key) const { 1185 Symbol result = tables_->FindNestedSymbolOfType(this, key, Symbol::FIELD); 1186 if (!result.IsNull() && result.field_descriptor->is_extension()) { 1187 return result.field_descriptor; 1188 } else { 1189 return NULL; 1190 } 1191 } 1192 1193 const FieldDescriptor* 1194 FileDescriptor::FindExtensionByLowercaseName(const string& key) const { 1195 const FieldDescriptor* result = tables_->FindFieldByLowercaseName(this, key); 1196 if (result == NULL || !result->is_extension()) { 1197 return NULL; 1198 } else { 1199 return result; 1200 } 1201 } 1202 1203 const FieldDescriptor* 1204 FileDescriptor::FindExtensionByCamelcaseName(const string& key) const { 1205 const FieldDescriptor* result = tables_->FindFieldByCamelcaseName(this, key); 1206 if (result == NULL || !result->is_extension()) { 1207 return NULL; 1208 } else { 1209 return result; 1210 } 1211 } 1212 1213 bool Descriptor::IsExtensionNumber(int number) const { 1214 // Linear search should be fine because we don't expect a message to have 1215 // more than a couple extension ranges. 1216 for (int i = 0; i < extension_range_count(); i++) { 1217 if (number >= extension_range(i)->start && 1218 number < extension_range(i)->end) { 1219 return true; 1220 } 1221 } 1222 return false; 1223 } 1224 1225 // ------------------------------------------------------------------- 1226 1227 bool DescriptorPool::TryFindFileInFallbackDatabase(const string& name) const { 1228 if (fallback_database_ == NULL) return false; 1229 1230 if (tables_->known_bad_files_.count(name) > 0) return false; 1231 1232 FileDescriptorProto file_proto; 1233 if (!fallback_database_->FindFileByName(name, &file_proto) || 1234 BuildFileFromDatabase(file_proto) == NULL) { 1235 tables_->known_bad_files_.insert(name); 1236 return false; 1237 } 1238 1239 return true; 1240 } 1241 1242 bool DescriptorPool::TryFindSymbolInFallbackDatabase(const string& name) const { 1243 if (fallback_database_ == NULL) return false; 1244 1245 FileDescriptorProto file_proto; 1246 if (!fallback_database_->FindFileContainingSymbol(name, &file_proto)) { 1247 return false; 1248 } 1249 1250 if (tables_->FindFile(file_proto.name()) != NULL) { 1251 // We've already loaded this file, and it apparently doesn't contain the 1252 // symbol we're looking for. Some DescriptorDatabases return false 1253 // positives. 1254 return false; 1255 } 1256 1257 if (BuildFileFromDatabase(file_proto) == NULL) { 1258 return false; 1259 } 1260 1261 return true; 1262 } 1263 1264 bool DescriptorPool::TryFindExtensionInFallbackDatabase( 1265 const Descriptor* containing_type, int field_number) const { 1266 if (fallback_database_ == NULL) return false; 1267 1268 FileDescriptorProto file_proto; 1269 if (!fallback_database_->FindFileContainingExtension( 1270 containing_type->full_name(), field_number, &file_proto)) { 1271 return false; 1272 } 1273 1274 if (tables_->FindFile(file_proto.name()) != NULL) { 1275 // We've already loaded this file, and it apparently doesn't contain the 1276 // extension we're looking for. Some DescriptorDatabases return false 1277 // positives. 1278 return false; 1279 } 1280 1281 if (BuildFileFromDatabase(file_proto) == NULL) { 1282 return false; 1283 } 1284 1285 return true; 1286 } 1287 1288 // =================================================================== 1289 1290 string FieldDescriptor::DefaultValueAsString(bool quote_string_type) const { 1291 GOOGLE_CHECK(has_default_value()) << "No default value"; 1292 switch (cpp_type()) { 1293 case CPPTYPE_INT32: 1294 return SimpleItoa(default_value_int32()); 1295 break; 1296 case CPPTYPE_INT64: 1297 return SimpleItoa(default_value_int64()); 1298 break; 1299 case CPPTYPE_UINT32: 1300 return SimpleItoa(default_value_uint32()); 1301 break; 1302 case CPPTYPE_UINT64: 1303 return SimpleItoa(default_value_uint64()); 1304 break; 1305 case CPPTYPE_FLOAT: 1306 return SimpleFtoa(default_value_float()); 1307 break; 1308 case CPPTYPE_DOUBLE: 1309 return SimpleDtoa(default_value_double()); 1310 break; 1311 case CPPTYPE_BOOL: 1312 return default_value_bool() ? "true" : "false"; 1313 break; 1314 case CPPTYPE_STRING: 1315 if (quote_string_type) { 1316 return "\"" + CEscape(default_value_string()) + "\""; 1317 } else { 1318 if (type() == TYPE_BYTES) { 1319 return CEscape(default_value_string()); 1320 } else { 1321 return default_value_string(); 1322 } 1323 } 1324 break; 1325 case CPPTYPE_ENUM: 1326 return default_value_enum()->name(); 1327 break; 1328 case CPPTYPE_MESSAGE: 1329 GOOGLE_LOG(DFATAL) << "Messages can't have default values!"; 1330 break; 1331 } 1332 GOOGLE_LOG(FATAL) << "Can't get here: failed to get default value as string"; 1333 return ""; 1334 } 1335 1336 // CopyTo methods ==================================================== 1337 1338 void FileDescriptor::CopyTo(FileDescriptorProto* proto) const { 1339 proto->set_name(name()); 1340 if (!package().empty()) proto->set_package(package()); 1341 1342 for (int i = 0; i < dependency_count(); i++) { 1343 proto->add_dependency(dependency(i)->name()); 1344 } 1345 1346 for (int i = 0; i < message_type_count(); i++) { 1347 message_type(i)->CopyTo(proto->add_message_type()); 1348 } 1349 for (int i = 0; i < enum_type_count(); i++) { 1350 enum_type(i)->CopyTo(proto->add_enum_type()); 1351 } 1352 for (int i = 0; i < service_count(); i++) { 1353 service(i)->CopyTo(proto->add_service()); 1354 } 1355 for (int i = 0; i < extension_count(); i++) { 1356 extension(i)->CopyTo(proto->add_extension()); 1357 } 1358 1359 if (&options() != &FileOptions::default_instance()) { 1360 proto->mutable_options()->CopyFrom(options()); 1361 } 1362 } 1363 1364 void Descriptor::CopyTo(DescriptorProto* proto) const { 1365 proto->set_name(name()); 1366 1367 for (int i = 0; i < field_count(); i++) { 1368 field(i)->CopyTo(proto->add_field()); 1369 } 1370 for (int i = 0; i < nested_type_count(); i++) { 1371 nested_type(i)->CopyTo(proto->add_nested_type()); 1372 } 1373 for (int i = 0; i < enum_type_count(); i++) { 1374 enum_type(i)->CopyTo(proto->add_enum_type()); 1375 } 1376 for (int i = 0; i < extension_range_count(); i++) { 1377 DescriptorProto::ExtensionRange* range = proto->add_extension_range(); 1378 range->set_start(extension_range(i)->start); 1379 range->set_end(extension_range(i)->end); 1380 } 1381 for (int i = 0; i < extension_count(); i++) { 1382 extension(i)->CopyTo(proto->add_extension()); 1383 } 1384 1385 if (&options() != &MessageOptions::default_instance()) { 1386 proto->mutable_options()->CopyFrom(options()); 1387 } 1388 } 1389 1390 void FieldDescriptor::CopyTo(FieldDescriptorProto* proto) const { 1391 proto->set_name(name()); 1392 proto->set_number(number()); 1393 1394 // Some compilers do not allow static_cast directly between two enum types, 1395 // so we must cast to int first. 1396 proto->set_label(static_cast<FieldDescriptorProto::Label>( 1397 implicit_cast<int>(label()))); 1398 proto->set_type(static_cast<FieldDescriptorProto::Type>( 1399 implicit_cast<int>(type()))); 1400 1401 if (is_extension()) { 1402 if (!containing_type()->is_unqualified_placeholder_) { 1403 proto->set_extendee("."); 1404 } 1405 proto->mutable_extendee()->append(containing_type()->full_name()); 1406 } 1407 1408 if (cpp_type() == CPPTYPE_MESSAGE) { 1409 if (message_type()->is_placeholder_) { 1410 // We don't actually know if the type is a message type. It could be 1411 // an enum. 1412 proto->clear_type(); 1413 } 1414 1415 if (!message_type()->is_unqualified_placeholder_) { 1416 proto->set_type_name("."); 1417 } 1418 proto->mutable_type_name()->append(message_type()->full_name()); 1419 } else if (cpp_type() == CPPTYPE_ENUM) { 1420 if (!enum_type()->is_unqualified_placeholder_) { 1421 proto->set_type_name("."); 1422 } 1423 proto->mutable_type_name()->append(enum_type()->full_name()); 1424 } 1425 1426 if (has_default_value()) { 1427 proto->set_default_value(DefaultValueAsString(false)); 1428 } 1429 1430 if (&options() != &FieldOptions::default_instance()) { 1431 proto->mutable_options()->CopyFrom(options()); 1432 } 1433 } 1434 1435 void EnumDescriptor::CopyTo(EnumDescriptorProto* proto) const { 1436 proto->set_name(name()); 1437 1438 for (int i = 0; i < value_count(); i++) { 1439 value(i)->CopyTo(proto->add_value()); 1440 } 1441 1442 if (&options() != &EnumOptions::default_instance()) { 1443 proto->mutable_options()->CopyFrom(options()); 1444 } 1445 } 1446 1447 void EnumValueDescriptor::CopyTo(EnumValueDescriptorProto* proto) const { 1448 proto->set_name(name()); 1449 proto->set_number(number()); 1450 1451 if (&options() != &EnumValueOptions::default_instance()) { 1452 proto->mutable_options()->CopyFrom(options()); 1453 } 1454 } 1455 1456 void ServiceDescriptor::CopyTo(ServiceDescriptorProto* proto) const { 1457 proto->set_name(name()); 1458 1459 for (int i = 0; i < method_count(); i++) { 1460 method(i)->CopyTo(proto->add_method()); 1461 } 1462 1463 if (&options() != &ServiceOptions::default_instance()) { 1464 proto->mutable_options()->CopyFrom(options()); 1465 } 1466 } 1467 1468 void MethodDescriptor::CopyTo(MethodDescriptorProto* proto) const { 1469 proto->set_name(name()); 1470 1471 if (!input_type()->is_unqualified_placeholder_) { 1472 proto->set_input_type("."); 1473 } 1474 proto->mutable_input_type()->append(input_type()->full_name()); 1475 1476 if (!output_type()->is_unqualified_placeholder_) { 1477 proto->set_output_type("."); 1478 } 1479 proto->mutable_output_type()->append(output_type()->full_name()); 1480 1481 if (&options() != &MethodOptions::default_instance()) { 1482 proto->mutable_options()->CopyFrom(options()); 1483 } 1484 } 1485 1486 // DebugString methods =============================================== 1487 1488 namespace { 1489 1490 // Used by each of the option formatters. 1491 bool RetrieveOptions(const Message &options, vector<string> *option_entries) { 1492 option_entries->clear(); 1493 const Reflection* reflection = options.GetReflection(); 1494 vector<const FieldDescriptor*> fields; 1495 reflection->ListFields(options, &fields); 1496 for (int i = 0; i < fields.size(); i++) { 1497 // Doesn't make sense to have message type fields here 1498 if (fields[i]->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { 1499 continue; 1500 } 1501 int count = 1; 1502 bool repeated = false; 1503 if (fields[i]->is_repeated()) { 1504 count = reflection->FieldSize(options, fields[i]); 1505 repeated = true; 1506 } 1507 for (int j = 0; j < count; j++) { 1508 string fieldval; 1509 TextFormat::PrintFieldValueToString(options, fields[i], 1510 repeated ? count : -1, &fieldval); 1511 option_entries->push_back(fields[i]->name() + " = " + fieldval); 1512 } 1513 } 1514 return !option_entries->empty(); 1515 } 1516 1517 // Formats options that all appear together in brackets. Does not include 1518 // brackets. 1519 bool FormatBracketedOptions(const Message &options, string *output) { 1520 vector<string> all_options; 1521 if (RetrieveOptions(options, &all_options)) { 1522 output->append(JoinStrings(all_options, ", ")); 1523 } 1524 return !all_options.empty(); 1525 } 1526 1527 // Formats options one per line 1528 bool FormatLineOptions(int depth, const Message &options, string *output) { 1529 string prefix(depth * 2, ' '); 1530 vector<string> all_options; 1531 if (RetrieveOptions(options, &all_options)) { 1532 for (int i = 0; i < all_options.size(); i++) { 1533 strings::SubstituteAndAppend(output, "$0option $1;\n", 1534 prefix, all_options[i]); 1535 } 1536 } 1537 return !all_options.empty(); 1538 } 1539 1540 } // anonymous namespace 1541 1542 string FileDescriptor::DebugString() const { 1543 string contents = "syntax = \"proto2\";\n\n"; 1544 1545 for (int i = 0; i < dependency_count(); i++) { 1546 strings::SubstituteAndAppend(&contents, "import \"$0\";\n", 1547 dependency(i)->name()); 1548 } 1549 1550 if (!package().empty()) { 1551 strings::SubstituteAndAppend(&contents, "package $0;\n\n", package()); 1552 } 1553 1554 if (FormatLineOptions(0, options(), &contents)) { 1555 contents.append("\n"); // add some space if we had options 1556 } 1557 1558 for (int i = 0; i < enum_type_count(); i++) { 1559 enum_type(i)->DebugString(0, &contents); 1560 contents.append("\n"); 1561 } 1562 1563 // Find all the 'group' type extensions; we will not output their nested 1564 // definitions (those will be done with their group field descriptor). 1565 set<const Descriptor*> groups; 1566 for (int i = 0; i < extension_count(); i++) { 1567 if (extension(i)->type() == FieldDescriptor::TYPE_GROUP) { 1568 groups.insert(extension(i)->message_type()); 1569 } 1570 } 1571 1572 for (int i = 0; i < message_type_count(); i++) { 1573 if (groups.count(message_type(i)) == 0) { 1574 strings::SubstituteAndAppend(&contents, "message $0", 1575 message_type(i)->name()); 1576 message_type(i)->DebugString(0, &contents); 1577 contents.append("\n"); 1578 } 1579 } 1580 1581 for (int i = 0; i < service_count(); i++) { 1582 service(i)->DebugString(&contents); 1583 contents.append("\n"); 1584 } 1585 1586 const Descriptor* containing_type = NULL; 1587 for (int i = 0; i < extension_count(); i++) { 1588 if (extension(i)->containing_type() != containing_type) { 1589 if (i > 0) contents.append("}\n\n"); 1590 containing_type = extension(i)->containing_type(); 1591 strings::SubstituteAndAppend(&contents, "extend .$0 {\n", 1592 containing_type->full_name()); 1593 } 1594 extension(i)->DebugString(1, &contents); 1595 } 1596 if (extension_count() > 0) contents.append("}\n\n"); 1597 1598 return contents; 1599 } 1600 1601 string Descriptor::DebugString() const { 1602 string contents; 1603 strings::SubstituteAndAppend(&contents, "message $0", name()); 1604 DebugString(0, &contents); 1605 return contents; 1606 } 1607 1608 void Descriptor::DebugString(int depth, string *contents) const { 1609 string prefix(depth * 2, ' '); 1610 ++depth; 1611 contents->append(" {\n"); 1612 1613 FormatLineOptions(depth, options(), contents); 1614 1615 // Find all the 'group' types for fields and extensions; we will not output 1616 // their nested definitions (those will be done with their group field 1617 // descriptor). 1618 set<const Descriptor*> groups; 1619 for (int i = 0; i < field_count(); i++) { 1620 if (field(i)->type() == FieldDescriptor::TYPE_GROUP) { 1621 groups.insert(field(i)->message_type()); 1622 } 1623 } 1624 for (int i = 0; i < extension_count(); i++) { 1625 if (extension(i)->type() == FieldDescriptor::TYPE_GROUP) { 1626 groups.insert(extension(i)->message_type()); 1627 } 1628 } 1629 1630 for (int i = 0; i < nested_type_count(); i++) { 1631 if (groups.count(nested_type(i)) == 0) { 1632 strings::SubstituteAndAppend(contents, "$0 message $1", 1633 prefix, nested_type(i)->name()); 1634 nested_type(i)->DebugString(depth, contents); 1635 } 1636 } 1637 for (int i = 0; i < enum_type_count(); i++) { 1638 enum_type(i)->DebugString(depth, contents); 1639 } 1640 for (int i = 0; i < field_count(); i++) { 1641 field(i)->DebugString(depth, contents); 1642 } 1643 1644 for (int i = 0; i < extension_range_count(); i++) { 1645 strings::SubstituteAndAppend(contents, "$0 extensions $1 to $2;\n", 1646 prefix, 1647 extension_range(i)->start, 1648 extension_range(i)->end - 1); 1649 } 1650 1651 // Group extensions by what they extend, so they can be printed out together. 1652 const Descriptor* containing_type = NULL; 1653 for (int i = 0; i < extension_count(); i++) { 1654 if (extension(i)->containing_type() != containing_type) { 1655 if (i > 0) strings::SubstituteAndAppend(contents, "$0 }\n", prefix); 1656 containing_type = extension(i)->containing_type(); 1657 strings::SubstituteAndAppend(contents, "$0 extend .$1 {\n", 1658 prefix, containing_type->full_name()); 1659 } 1660 extension(i)->DebugString(depth + 1, contents); 1661 } 1662 if (extension_count() > 0) 1663 strings::SubstituteAndAppend(contents, "$0 }\n", prefix); 1664 1665 strings::SubstituteAndAppend(contents, "$0}\n", prefix); 1666 } 1667 1668 string FieldDescriptor::DebugString() const { 1669 string contents; 1670 int depth = 0; 1671 if (is_extension()) { 1672 strings::SubstituteAndAppend(&contents, "extend .$0 {\n", 1673 containing_type()->full_name()); 1674 depth = 1; 1675 } 1676 DebugString(depth, &contents); 1677 if (is_extension()) { 1678 contents.append("}\n"); 1679 } 1680 return contents; 1681 } 1682 1683 void FieldDescriptor::DebugString(int depth, string *contents) const { 1684 string prefix(depth * 2, ' '); 1685 string field_type; 1686 switch (type()) { 1687 case TYPE_MESSAGE: 1688 field_type = "." + message_type()->full_name(); 1689 break; 1690 case TYPE_ENUM: 1691 field_type = "." + enum_type()->full_name(); 1692 break; 1693 default: 1694 field_type = kTypeToName[type()]; 1695 } 1696 1697 strings::SubstituteAndAppend(contents, "$0$1 $2 $3 = $4", 1698 prefix, 1699 kLabelToName[label()], 1700 field_type, 1701 type() == TYPE_GROUP ? message_type()->name() : 1702 name(), 1703 number()); 1704 1705 bool bracketed = false; 1706 if (has_default_value()) { 1707 bracketed = true; 1708 strings::SubstituteAndAppend(contents, " [default = $0", 1709 DefaultValueAsString(true)); 1710 } 1711 1712 string formatted_options; 1713 if (FormatBracketedOptions(options(), &formatted_options)) { 1714 contents->append(bracketed ? ", " : " ["); 1715 bracketed = true; 1716 contents->append(formatted_options); 1717 } 1718 1719 if (bracketed) { 1720 contents->append("]"); 1721 } 1722 1723 if (type() == TYPE_GROUP) { 1724 message_type()->DebugString(depth, contents); 1725 } else { 1726 contents->append(";\n"); 1727 } 1728 } 1729 1730 string EnumDescriptor::DebugString() const { 1731 string contents; 1732 DebugString(0, &contents); 1733 return contents; 1734 } 1735 1736 void EnumDescriptor::DebugString(int depth, string *contents) const { 1737 string prefix(depth * 2, ' '); 1738 ++depth; 1739 strings::SubstituteAndAppend(contents, "$0enum $1 {\n", 1740 prefix, name()); 1741 1742 FormatLineOptions(depth, options(), contents); 1743 1744 for (int i = 0; i < value_count(); i++) { 1745 value(i)->DebugString(depth, contents); 1746 } 1747 strings::SubstituteAndAppend(contents, "$0}\n", prefix); 1748 } 1749 1750 string EnumValueDescriptor::DebugString() const { 1751 string contents; 1752 DebugString(0, &contents); 1753 return contents; 1754 } 1755 1756 void EnumValueDescriptor::DebugString(int depth, string *contents) const { 1757 string prefix(depth * 2, ' '); 1758 strings::SubstituteAndAppend(contents, "$0$1 = $2", 1759 prefix, name(), number()); 1760 1761 string formatted_options; 1762 if (FormatBracketedOptions(options(), &formatted_options)) { 1763 strings::SubstituteAndAppend(contents, " [$0]", formatted_options); 1764 } 1765 contents->append(";\n"); 1766 } 1767 1768 string ServiceDescriptor::DebugString() const { 1769 string contents; 1770 DebugString(&contents); 1771 return contents; 1772 } 1773 1774 void ServiceDescriptor::DebugString(string *contents) const { 1775 strings::SubstituteAndAppend(contents, "service $0 {\n", name()); 1776 1777 FormatLineOptions(1, options(), contents); 1778 1779 for (int i = 0; i < method_count(); i++) { 1780 method(i)->DebugString(1, contents); 1781 } 1782 1783 contents->append("}\n"); 1784 } 1785 1786 string MethodDescriptor::DebugString() const { 1787 string contents; 1788 DebugString(0, &contents); 1789 return contents; 1790 } 1791 1792 void MethodDescriptor::DebugString(int depth, string *contents) const { 1793 string prefix(depth * 2, ' '); 1794 ++depth; 1795 strings::SubstituteAndAppend(contents, "$0rpc $1(.$2) returns (.$3)", 1796 prefix, name(), 1797 input_type()->full_name(), 1798 output_type()->full_name()); 1799 1800 string formatted_options; 1801 if (FormatLineOptions(depth, options(), &formatted_options)) { 1802 strings::SubstituteAndAppend(contents, " {\n$0$1}\n", 1803 formatted_options, prefix); 1804 } else { 1805 contents->append(";\n"); 1806 } 1807 } 1808 // =================================================================== 1809 1810 namespace { 1811 1812 // Represents an options message to interpret. Extension names in the option 1813 // name are respolved relative to name_scope. element_name and orig_opt are 1814 // used only for error reporting (since the parser records locations against 1815 // pointers in the original options, not the mutable copy). The Message must be 1816 // one of the Options messages in descriptor.proto. 1817 struct OptionsToInterpret { 1818 OptionsToInterpret(const string& ns, 1819 const string& el, 1820 const Message* orig_opt, 1821 Message* opt) 1822 : name_scope(ns), 1823 element_name(el), 1824 original_options(orig_opt), 1825 options(opt) { 1826 } 1827 string name_scope; 1828 string element_name; 1829 const Message* original_options; 1830 Message* options; 1831 }; 1832 1833 } // namespace 1834 1835 class DescriptorBuilder { 1836 public: 1837 DescriptorBuilder(const DescriptorPool* pool, 1838 DescriptorPool::Tables* tables, 1839 DescriptorPool::ErrorCollector* error_collector); 1840 ~DescriptorBuilder(); 1841 1842 const FileDescriptor* BuildFile(const FileDescriptorProto& proto); 1843 1844 private: 1845 friend class OptionInterpreter; 1846 1847 const DescriptorPool* pool_; 1848 DescriptorPool::Tables* tables_; // for convenience 1849 DescriptorPool::ErrorCollector* error_collector_; 1850 1851 // As we build descriptors we store copies of the options messages in 1852 // them. We put pointers to those copies in this vector, as we build, so we 1853 // can later (after cross-linking) interpret those options. 1854 vector<OptionsToInterpret> options_to_interpret_; 1855 1856 bool had_errors_; 1857 string filename_; 1858 FileDescriptor* file_; 1859 FileDescriptorTables* file_tables_; 1860 1861 // If LookupSymbol() finds a symbol that is in a file which is not a declared 1862 // dependency of this file, it will fail, but will set 1863 // possible_undeclared_dependency_ to point at that file. This is only used 1864 // by AddNotDefinedError() to report a more useful error message. 1865 // possible_undeclared_dependency_name_ is the name of the symbol that was 1866 // actually found in possible_undeclared_dependency_, which may be a parent 1867 // of the symbol actually looked for. 1868 const FileDescriptor* possible_undeclared_dependency_; 1869 string possible_undeclared_dependency_name_; 1870 1871 void AddError(const string& element_name, 1872 const Message& descriptor, 1873 DescriptorPool::ErrorCollector::ErrorLocation location, 1874 const string& error); 1875 1876 // Adds an error indicating that undefined_symbol was not defined. Must 1877 // only be called after LookupSymbol() fails. 1878 void AddNotDefinedError( 1879 const string& element_name, 1880 const Message& descriptor, 1881 DescriptorPool::ErrorCollector::ErrorLocation location, 1882 const string& undefined_symbol); 1883 1884 // Silly helper which determines if the given file is in the given package. 1885 // I.e., either file->package() == package_name or file->package() is a 1886 // nested package within package_name. 1887 bool IsInPackage(const FileDescriptor* file, const string& package_name); 1888 1889 // Like tables_->FindSymbol(), but additionally: 1890 // - Search the pool's underlay if not found in tables_. 1891 // - Insure that the resulting Symbol is from one of the file's declared 1892 // dependencies. 1893 Symbol FindSymbol(const string& name); 1894 1895 // Like FindSymbol() but does not require that the symbol is in one of the 1896 // file's declared dependencies. 1897 Symbol FindSymbolNotEnforcingDeps(const string& name); 1898 1899 // Like FindSymbol(), but looks up the name relative to some other symbol 1900 // name. This first searches siblings of relative_to, then siblings of its 1901 // parents, etc. For example, LookupSymbol("foo.bar", "baz.qux.corge") makes 1902 // the following calls, returning the first non-null result: 1903 // FindSymbol("baz.qux.foo.bar"), FindSymbol("baz.foo.bar"), 1904 // FindSymbol("foo.bar"). If AllowUnknownDependencies() has been called 1905 // on the DescriptorPool, this will generate a placeholder type if 1906 // the name is not found (unless the name itself is malformed). The 1907 // placeholder_type parameter indicates what kind of placeholder should be 1908 // constructed in this case. The resolve_mode parameter determines whether 1909 // any symbol is returned, or only symbols that are types. Note, however, 1910 // that LookupSymbol may still return a non-type symbol in LOOKUP_TYPES mode, 1911 // if it believes that's all it could refer to. The caller should always 1912 // check that it receives the type of symbol it was expecting. 1913 enum PlaceholderType { 1914 PLACEHOLDER_MESSAGE, 1915 PLACEHOLDER_ENUM, 1916 PLACEHOLDER_EXTENDABLE_MESSAGE 1917 }; 1918 enum ResolveMode { 1919 LOOKUP_ALL, LOOKUP_TYPES 1920 }; 1921 Symbol LookupSymbol(const string& name, const string& relative_to, 1922 PlaceholderType placeholder_type = PLACEHOLDER_MESSAGE, 1923 ResolveMode resolve_mode = LOOKUP_ALL); 1924 1925 // Like LookupSymbol() but will not return a placeholder even if 1926 // AllowUnknownDependencies() has been used. 1927 Symbol LookupSymbolNoPlaceholder(const string& name, 1928 const string& relative_to, 1929 ResolveMode resolve_mode = LOOKUP_ALL); 1930 1931 // Creates a placeholder type suitable for return from LookupSymbol(). May 1932 // return kNullSymbol if the name is not a valid type name. 1933 Symbol NewPlaceholder(const string& name, PlaceholderType placeholder_type); 1934 1935 // Creates a placeholder file. Never returns NULL. This is used when an 1936 // import is not found and AllowUnknownDependencies() is enabled. 1937 const FileDescriptor* NewPlaceholderFile(const string& name); 1938 1939 // Calls tables_->AddSymbol() and records an error if it fails. Returns 1940 // true if successful or false if failed, though most callers can ignore 1941 // the return value since an error has already been recorded. 1942 bool AddSymbol(const string& full_name, 1943 const void* parent, const string& name, 1944 const Message& proto, Symbol symbol); 1945 1946 // Like AddSymbol(), but succeeds if the symbol is already defined as long 1947 // as the existing definition is also a package (because it's OK to define 1948 // the same package in two different files). Also adds all parents of the 1949 // packgae to the symbol table (e.g. AddPackage("foo.bar", ...) will add 1950 // "foo.bar" and "foo" to the table). 1951 void AddPackage(const string& name, const Message& proto, 1952 const FileDescriptor* file); 1953 1954 // Checks that the symbol name contains only alphanumeric characters and 1955 // underscores. Records an error otherwise. 1956 void ValidateSymbolName(const string& name, const string& full_name, 1957 const Message& proto); 1958 1959 // Like ValidateSymbolName(), but the name is allowed to contain periods and 1960 // an error is indicated by returning false (not recording the error). 1961 bool ValidateQualifiedName(const string& name); 1962 1963 // Used by BUILD_ARRAY macro (below) to avoid having to have the type 1964 // specified as a macro parameter. 1965 template <typename Type> 1966 inline void AllocateArray(int size, Type** output) { 1967 *output = tables_->AllocateArray<Type>(size); 1968 } 1969 1970 // Allocates a copy of orig_options in tables_ and stores it in the 1971 // descriptor. Remembers its uninterpreted options, to be interpreted 1972 // later. DescriptorT must be one of the Descriptor messages from 1973 // descriptor.proto. 1974 template<class DescriptorT> void AllocateOptions( 1975 const typename DescriptorT::OptionsType& orig_options, 1976 DescriptorT* descriptor); 1977 // Specialization for FileOptions. 1978 void AllocateOptions(const FileOptions& orig_options, 1979 FileDescriptor* descriptor); 1980 1981 // Implementation for AllocateOptions(). Don't call this directly. 1982 template<class DescriptorT> void AllocateOptionsImpl( 1983 const string& name_scope, 1984 const string& element_name, 1985 const typename DescriptorT::OptionsType& orig_options, 1986 DescriptorT* descriptor); 1987 1988 // These methods all have the same signature for the sake of the BUILD_ARRAY 1989 // macro, below. 1990 void BuildMessage(const DescriptorProto& proto, 1991 const Descriptor* parent, 1992 Descriptor* result); 1993 void BuildFieldOrExtension(const FieldDescriptorProto& proto, 1994 const Descriptor* parent, 1995 FieldDescriptor* result, 1996 bool is_extension); 1997 void BuildField(const FieldDescriptorProto& proto, 1998 const Descriptor* parent, 1999 FieldDescriptor* result) { 2000 BuildFieldOrExtension(proto, parent, result, false); 2001 } 2002 void BuildExtension(const FieldDescriptorProto& proto, 2003 const Descriptor* parent, 2004 FieldDescriptor* result) { 2005 BuildFieldOrExtension(proto, parent, result, true); 2006 } 2007 void BuildExtensionRange(const DescriptorProto::ExtensionRange& proto, 2008 const Descriptor* parent, 2009 Descriptor::ExtensionRange* result); 2010 void BuildEnum(const EnumDescriptorProto& proto, 2011 const Descriptor* parent, 2012 EnumDescriptor* result); 2013 void BuildEnumValue(const EnumValueDescriptorProto& proto, 2014 const EnumDescriptor* parent, 2015 EnumValueDescriptor* result); 2016 void BuildService(const ServiceDescriptorProto& proto, 2017 const void* dummy, 2018 ServiceDescriptor* result); 2019 void BuildMethod(const MethodDescriptorProto& proto, 2020 const ServiceDescriptor* parent, 2021 MethodDescriptor* result); 2022 2023 // Must be run only after building. 2024 // 2025 // NOTE: Options will not be available during cross-linking, as they 2026 // have not yet been interpreted. Defer any handling of options to the 2027 // Validate*Options methods. 2028 void CrossLinkFile(FileDescriptor* file, const FileDescriptorProto& proto); 2029 void CrossLinkMessage(Descriptor* message, const DescriptorProto& proto); 2030 void CrossLinkField(FieldDescriptor* field, 2031 const FieldDescriptorProto& proto); 2032 void CrossLinkEnum(EnumDescriptor* enum_type, 2033 const EnumDescriptorProto& proto); 2034 void CrossLinkEnumValue(EnumValueDescriptor* enum_value, 2035 const EnumValueDescriptorProto& proto); 2036 void CrossLinkService(ServiceDescriptor* service, 2037 const ServiceDescriptorProto& proto); 2038 void CrossLinkMethod(MethodDescriptor* method, 2039 const MethodDescriptorProto& proto); 2040 2041 // Must be run only after cross-linking. 2042 void InterpretOptions(); 2043 2044 // A helper class for interpreting options. 2045 class OptionInterpreter { 2046 public: 2047 // Creates an interpreter that operates in the context of the pool of the 2048 // specified builder, which must not be NULL. We don't take ownership of the 2049 // builder. 2050 explicit OptionInterpreter(DescriptorBuilder* builder); 2051 2052 ~OptionInterpreter(); 2053 2054 // Interprets the uninterpreted options in the specified Options message. 2055 // On error, calls AddError() on the underlying builder and returns false. 2056 // Otherwise returns true. 2057 bool InterpretOptions(OptionsToInterpret* options_to_interpret); 2058 2059 private: 2060 // Interprets uninterpreted_option_ on the specified message, which 2061 // must be the mutable copy of the original options message to which 2062 // uninterpreted_option_ belongs. 2063 bool InterpretSingleOption(Message* options); 2064 2065 // Adds the uninterpreted_option to the given options message verbatim. 2066 // Used when AllowUnknownDependencies() is in effect and we can't find 2067 // the option's definition. 2068 void AddWithoutInterpreting(const UninterpretedOption& uninterpreted_option, 2069 Message* options); 2070 2071 // A recursive helper function that drills into the intermediate fields 2072 // in unknown_fields to check if field innermost_field is set on the 2073 // innermost message. Returns false and sets an error if so. 2074 bool ExamineIfOptionIsSet( 2075 vector<const FieldDescriptor*>::const_iterator intermediate_fields_iter, 2076 vector<const FieldDescriptor*>::const_iterator intermediate_fields_end, 2077 const FieldDescriptor* innermost_field, const string& debug_msg_name, 2078 const UnknownFieldSet& unknown_fields); 2079 2080 // Validates the value for the option field of the currently interpreted 2081 // option and then sets it on the unknown_field. 2082 bool SetOptionValue(const FieldDescriptor* option_field, 2083 UnknownFieldSet* unknown_fields); 2084 2085 // Convenience functions to set an int field the right way, depending on 2086 // its wire type (a single int CppType can represent multiple wire types). 2087 void SetInt32(int number, int32 value, FieldDescriptor::Type type, 2088 UnknownFieldSet* unknown_fields); 2089 void SetInt64(int number, int64 value, FieldDescriptor::Type type, 2090 UnknownFieldSet* unknown_fields); 2091 void SetUInt32(int number, uint32 value, FieldDescriptor::Type type, 2092 UnknownFieldSet* unknown_fields); 2093 void SetUInt64(int number, uint64 value, FieldDescriptor::Type type, 2094 UnknownFieldSet* unknown_fields); 2095 2096 // A helper function that adds an error at the specified location of the 2097 // option we're currently interpreting, and returns false. 2098 bool AddOptionError(DescriptorPool::ErrorCollector::ErrorLocation location, 2099 const string& msg) { 2100 builder_->AddError(options_to_interpret_->element_name, 2101 *uninterpreted_option_, location, msg); 2102 return false; 2103 } 2104 2105 // A helper function that adds an error at the location of the option name 2106 // and returns false. 2107 bool AddNameError(const string& msg) { 2108 return AddOptionError(DescriptorPool::ErrorCollector::OPTION_NAME, msg); 2109 } 2110 2111 // A helper function that adds an error at the location of the option name 2112 // and returns false. 2113 bool AddValueError(const string& msg) { 2114 return AddOptionError(DescriptorPool::ErrorCollector::OPTION_VALUE, msg); 2115 } 2116 2117 // We interpret against this builder's pool. Is never NULL. We don't own 2118 // this pointer. 2119 DescriptorBuilder* builder_; 2120 2121 // The options we're currently interpreting, or NULL if we're not in a call 2122 // to InterpretOptions. 2123 const OptionsToInterpret* options_to_interpret_; 2124 2125 // The option we're currently interpreting within options_to_interpret_, or 2126 // NULL if we're not in a call to InterpretOptions(). This points to a 2127 // submessage of the original option, not the mutable copy. Therefore we 2128 // can use it to find locations recorded by the parser. 2129 const UninterpretedOption* uninterpreted_option_; 2130 2131 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(OptionInterpreter); 2132 }; 2133 2134 // Work-around for broken compilers: According to the C++ standard, 2135 // OptionInterpreter should have access to the private members of any class 2136 // which has declared DescriptorBuilder as a friend. Unfortunately some old 2137 // versions of GCC and other compilers do not implement this correctly. So, 2138 // we have to have these intermediate methods to provide access. We also 2139 // redundantly declare OptionInterpreter a friend just to make things extra 2140 // clear for these bad compilers. 2141 friend class OptionInterpreter; 2142 static inline bool get_allow_unknown(const DescriptorPool* pool) { 2143 return pool->allow_unknown_; 2144 } 2145 static inline bool get_is_placeholder(const Descriptor* descriptor) { 2146 return descriptor->is_placeholder_; 2147 } 2148 2149 // Must be run only after options have been interpreted. 2150 // 2151 // NOTE: Validation code must only reference the options in the mutable 2152 // descriptors, which are the ones that have been interpreted. The const 2153 // proto references are passed in only so they can be provided to calls to 2154 // AddError(). Do not look at their options, which have not been interpreted. 2155 void ValidateFileOptions(FileDescriptor* file, 2156 const FileDescriptorProto& proto); 2157 void ValidateMessageOptions(Descriptor* message, 2158 const DescriptorProto& proto); 2159 void ValidateFieldOptions(FieldDescriptor* field, 2160 const FieldDescriptorProto& proto); 2161 void ValidateEnumOptions(EnumDescriptor* enm, 2162 const EnumDescriptorProto& proto); 2163 void ValidateEnumValueOptions(EnumValueDescriptor* enum_value, 2164 const EnumValueDescriptorProto& proto); 2165 void ValidateServiceOptions(ServiceDescriptor* service, 2166 const ServiceDescriptorProto& proto); 2167 void ValidateMethodOptions(MethodDescriptor* method, 2168 const MethodDescriptorProto& proto); 2169 2170 void ValidateMapKey(FieldDescriptor* field, 2171 const FieldDescriptorProto& proto); 2172 }; 2173 2174 const FileDescriptor* DescriptorPool::BuildFile( 2175 const FileDescriptorProto& proto) { 2176 GOOGLE_CHECK(fallback_database_ == NULL) 2177 << "Cannot call BuildFile on a DescriptorPool that uses a " 2178 "DescriptorDatabase. You must instead find a way to get your file " 2179 "into the underlying database."; 2180 GOOGLE_CHECK(mutex_ == NULL); // Implied by the above GOOGLE_CHECK. 2181 return DescriptorBuilder(this, tables_.get(), NULL).BuildFile(proto); 2182 } 2183 2184 const FileDescriptor* DescriptorPool::BuildFileCollectingErrors( 2185 const FileDescriptorProto& proto, 2186 ErrorCollector* error_collector) { 2187 GOOGLE_CHECK(fallback_database_ == NULL) 2188 << "Cannot call BuildFile on a DescriptorPool that uses a " 2189 "DescriptorDatabase. You must instead find a way to get your file " 2190 "into the underlying database."; 2191 GOOGLE_CHECK(mutex_ == NULL); // Implied by the above GOOGLE_CHECK. 2192 return DescriptorBuilder(this, tables_.get(), 2193 error_collector).BuildFile(proto); 2194 } 2195 2196 const FileDescriptor* DescriptorPool::BuildFileFromDatabase( 2197 const FileDescriptorProto& proto) const { 2198 mutex_->AssertHeld(); 2199 return DescriptorBuilder(this, tables_.get(), 2200 default_error_collector_).BuildFile(proto); 2201 } 2202 2203 DescriptorBuilder::DescriptorBuilder( 2204 const DescriptorPool* pool, 2205 DescriptorPool::Tables* tables, 2206 DescriptorPool::ErrorCollector* error_collector) 2207 : pool_(pool), 2208 tables_(tables), 2209 error_collector_(error_collector), 2210 had_errors_(false), 2211 possible_undeclared_dependency_(NULL) {} 2212 2213 DescriptorBuilder::~DescriptorBuilder() {} 2214 2215 void DescriptorBuilder::AddError( 2216 const string& element_name, 2217 const Message& descriptor, 2218 DescriptorPool::ErrorCollector::ErrorLocation location, 2219 const string& error) { 2220 if (error_collector_ == NULL) { 2221 if (!had_errors_) { 2222 GOOGLE_LOG(ERROR) << "Invalid proto descriptor for file \"" << filename_ 2223 << "\":"; 2224 } 2225 GOOGLE_LOG(ERROR) << " " << element_name << ": " << error; 2226 } else { 2227 error_collector_->AddError(filename_, element_name, 2228 &descriptor, location, error); 2229 } 2230 had_errors_ = true; 2231 } 2232 2233 void DescriptorBuilder::AddNotDefinedError( 2234 const string& element_name, 2235 const Message& descriptor, 2236 DescriptorPool::ErrorCollector::ErrorLocation location, 2237 const string& undefined_symbol) { 2238 if (possible_undeclared_dependency_ == NULL) { 2239 AddError(element_name, descriptor, location, 2240 "\"" + undefined_symbol + "\" is not defined."); 2241 } else { 2242 AddError(element_name, descriptor, location, 2243 "\"" + possible_undeclared_dependency_name_ + 2244 "\" seems to be defined in \"" + 2245 possible_undeclared_dependency_->name() + "\", which is not " 2246 "imported by \"" + filename_ + "\". To use it here, please " 2247 "add the necessary import."); 2248 } 2249 } 2250 2251 bool DescriptorBuilder::IsInPackage(const FileDescriptor* file, 2252 const string& package_name) { 2253 return HasPrefixString(file->package(), package_name) && 2254 (file->package().size() == package_name.size() || 2255 file->package()[package_name.size()] == '.'); 2256 } 2257 2258 Symbol DescriptorBuilder::FindSymbolNotEnforcingDeps(const string& name) { 2259 Symbol result; 2260 2261 // We need to search our pool and all its underlays. 2262 const DescriptorPool* pool = pool_; 2263 while (true) { 2264 // If we are looking at an underlay, we must lock its mutex_, since we are 2265 // accessing the underlay's tables_ dircetly. 2266 MutexLockMaybe lock((pool == pool_) ? NULL : pool->mutex_); 2267 2268 // Note that we don't have to check fallback_database_ here because the 2269 // symbol has to be in one of its file's direct dependencies, and we have 2270 // already loaded those by the time we get here. 2271 result = pool->tables_->FindSymbol(name); 2272 if (!result.IsNull()) break; 2273 if (pool->underlay_ == NULL) return kNullSymbol; 2274 pool = pool->underlay_; 2275 } 2276 2277 return result; 2278 } 2279 2280 Symbol DescriptorBuilder::FindSymbol(const string& name) { 2281 Symbol result = FindSymbolNotEnforcingDeps(name); 2282 2283 if (!pool_->enforce_dependencies_) { 2284 // Hack for CompilerUpgrader. 2285 return result; 2286 } 2287 2288 // Only find symbols which were defined in this file or one of its 2289 // dependencies. 2290 const FileDescriptor* file = result.GetFile(); 2291 if (file == file_) return result; 2292 for (int i = 0; i < file_->dependency_count(); i++) { 2293 if (file == file_->dependency(i)) return result; 2294 } 2295 2296 if (result.type == Symbol::PACKAGE) { 2297 // Arg, this is overcomplicated. The symbol is a package name. It could 2298 // be that the package was defined in multiple files. result.GetFile() 2299 // returns the first file we saw that used this package. We've determined 2300 // that that file is not a direct dependency of the file we are currently 2301 // building, but it could be that some other file which *is* a direct 2302 // dependency also defines the same package. We can't really rule out this 2303 // symbol unless none of the dependencies define it. 2304 if (IsInPackage(file_, name)) return result; 2305 for (int i = 0; i < file_->dependency_count(); i++) { 2306 // Note: A dependency may be NULL if it was not found or had errors. 2307 if (file_->dependency(i) != NULL && 2308 IsInPackage(file_->dependency(i), name)) { 2309 return result; 2310 } 2311 } 2312 } 2313 2314 possible_undeclared_dependency_ = file; 2315 possible_undeclared_dependency_name_ = name; 2316 return kNullSymbol; 2317 } 2318 2319 Symbol DescriptorBuilder::LookupSymbolNoPlaceholder( 2320 const string& name, const string& relative_to, ResolveMode resolve_mode) { 2321 possible_undeclared_dependency_ = NULL; 2322 2323 if (name.size() > 0 && name[0] == '.') { 2324 // Fully-qualified name. 2325 return FindSymbol(name.substr(1)); 2326 } 2327 2328 // If name is something like "Foo.Bar.baz", and symbols named "Foo" are 2329 // defined in multiple parent scopes, we only want to find "Bar.baz" in the 2330 // innermost one. E.g., the following should produce an error: 2331 // message Bar { message Baz {} } 2332 // message Foo { 2333 // message Bar { 2334 // } 2335 // optional Bar.Baz baz = 1; 2336 // } 2337 // So, we look for just "Foo" first, then look for "Bar.baz" within it if 2338 // found. 2339 int name_dot_pos = name.find_first_of('.'); 2340 string first_part_of_name; 2341 if (name_dot_pos == string::npos) { 2342 first_part_of_name = name; 2343 } else { 2344 first_part_of_name = name.substr(0, name_dot_pos); 2345 } 2346 2347 string scope_to_try(relative_to); 2348 2349 while (true) { 2350 // Chop off the last component of the scope. 2351 string::size_type dot_pos = scope_to_try.find_last_of('.'); 2352 if (dot_pos == string::npos) { 2353 return FindSymbol(name); 2354 } else { 2355 scope_to_try.erase(dot_pos); 2356 } 2357 2358 // Append ".first_part_of_name" and try to find. 2359 string::size_type old_size = scope_to_try.size(); 2360 scope_to_try.append(1, '.'); 2361 scope_to_try.append(first_part_of_name); 2362 Symbol result = FindSymbol(scope_to_try); 2363 if (!result.IsNull()) { 2364 if (first_part_of_name.size() < name.size()) { 2365 // name is a compound symbol, of which we only found the first part. 2366 // Now try to look up the rest of it. 2367 if (result.IsAggregate()) { 2368 scope_to_try.append(name, first_part_of_name.size(), 2369 name.size() - first_part_of_name.size()); 2370 return FindSymbol(scope_to_try); 2371 } else { 2372 // We found a symbol but it's not an aggregate. Continue the loop. 2373 } 2374 } else { 2375 if (resolve_mode == LOOKUP_TYPES && !result.IsType()) { 2376 // We found a symbol but it's not a type. Continue the loop. 2377 } else { 2378 return result; 2379 } 2380 } 2381 } 2382 2383 // Not found. Remove the name so we can try again. 2384 scope_to_try.erase(old_size); 2385 } 2386 } 2387 2388 Symbol DescriptorBuilder::LookupSymbol( 2389 const string& name, const string& relative_to, 2390 PlaceholderType placeholder_type, ResolveMode resolve_mode) { 2391 Symbol result = LookupSymbolNoPlaceholder( 2392 name, relative_to, resolve_mode); 2393 if (result.IsNull() && pool_->allow_unknown_) { 2394 // Not found, but AllowUnknownDependencies() is enabled. Return a 2395 // placeholder instead. 2396 result = NewPlaceholder(name, placeholder_type); 2397 } 2398 return result; 2399 } 2400 2401 Symbol DescriptorBuilder::NewPlaceholder(const string& name, 2402 PlaceholderType placeholder_type) { 2403 // Compute names. 2404 const string* placeholder_full_name; 2405 const string* placeholder_name; 2406 const string* placeholder_package; 2407 2408 if (!ValidateQualifiedName(name)) return kNullSymbol; 2409 if (name[0] == '.') { 2410 // Fully-qualified. 2411 placeholder_full_name = tables_->AllocateString(name.substr(1)); 2412 } else { 2413 placeholder_full_name = tables_->AllocateString(name); 2414 } 2415 2416 string::size_type dotpos = placeholder_full_name->find_last_of('.'); 2417 if (dotpos != string::npos) { 2418 placeholder_package = tables_->AllocateString( 2419 placeholder_full_name->substr(0, dotpos)); 2420 placeholder_name = tables_->AllocateString( 2421 placeholder_full_name->substr(dotpos + 1)); 2422 } else { 2423 placeholder_package = &kEmptyString; 2424 placeholder_name = placeholder_full_name; 2425 } 2426 2427 // Create the placeholders. 2428 FileDescriptor* placeholder_file = tables_->Allocate<FileDescriptor>(); 2429 memset(placeholder_file, 0, sizeof(*placeholder_file)); 2430 2431 placeholder_file->name_ = 2432 tables_->AllocateString(*placeholder_full_name + ".placeholder.proto"); 2433 placeholder_file->package_ = placeholder_package; 2434 placeholder_file->pool_ = pool_; 2435 placeholder_file->options_ = &FileOptions::default_instance(); 2436 placeholder_file->tables_ = &FileDescriptorTables::kEmpty; 2437 // All other fields are zero or NULL. 2438 2439 if (placeholder_type == PLACEHOLDER_ENUM) { 2440 placeholder_file->enum_type_count_ = 1; 2441 placeholder_file->enum_types_ = 2442 tables_->AllocateArray<EnumDescriptor>(1); 2443 2444 EnumDescriptor* placeholder_enum = &placeholder_file->enum_types_[0]; 2445 memset(placeholder_enum, 0, sizeof(*placeholder_enum)); 2446 2447 placeholder_enum->full_name_ = placeholder_full_name; 2448 placeholder_enum->name_ = placeholder_name; 2449 placeholder_enum->file_ = placeholder_file; 2450 placeholder_enum->options_ = &EnumOptions::default_instance(); 2451 placeholder_enum->is_placeholder_ = true; 2452 placeholder_enum->is_unqualified_placeholder_ = (name[0] != '.'); 2453 2454 // Enums must have at least one value. 2455 placeholder_enum->value_count_ = 1; 2456 placeholder_enum->values_ = tables_->AllocateArray<EnumValueDescriptor>(1); 2457 2458 EnumValueDescriptor* placeholder_value = &placeholder_enum->values_[0]; 2459 memset(placeholder_value, 0, sizeof(*placeholder_value)); 2460 2461 placeholder_value->name_ = tables_->AllocateString("PLACEHOLDER_VALUE"); 2462 // Note that enum value names are siblings of their type, not children. 2463 placeholder_value->full_name_ = 2464 placeholder_package->empty() ? placeholder_value->name_ : 2465 tables_->AllocateString(*placeholder_package + ".PLACEHOLDER_VALUE"); 2466 2467 placeholder_value->number_ = 0; 2468 placeholder_value->type_ = placeholder_enum; 2469 placeholder_value->options_ = &EnumValueOptions::default_instance(); 2470 2471 return Symbol(placeholder_enum); 2472 } else { 2473 placeholder_file->message_type_count_ = 1; 2474 placeholder_file->message_types_ = 2475 tables_->AllocateArray<Descriptor>(1); 2476 2477 Descriptor* placeholder_message = &placeholder_file->message_types_[0]; 2478 memset(placeholder_message, 0, sizeof(*placeholder_message)); 2479 2480 placeholder_message->full_name_ = placeholder_full_name; 2481 placeholder_message->name_ = placeholder_name; 2482 placeholder_message->file_ = placeholder_file; 2483 placeholder_message->options_ = &MessageOptions::default_instance(); 2484 placeholder_message->is_placeholder_ = true; 2485 placeholder_message->is_unqualified_placeholder_ = (name[0] != '.'); 2486 2487 if (placeholder_type == PLACEHOLDER_EXTENDABLE_MESSAGE) { 2488 placeholder_message->extension_range_count_ = 1; 2489 placeholder_message->extension_ranges_ = 2490 tables_->AllocateArray<Descriptor::ExtensionRange>(1); 2491 placeholder_message->extension_ranges_->start = 1; 2492 // kMaxNumber + 1 because ExtensionRange::end is exclusive. 2493 placeholder_message->extension_ranges_->end = 2494 FieldDescriptor::kMaxNumber + 1; 2495 } 2496 2497 return Symbol(placeholder_message); 2498 } 2499 } 2500 2501 const FileDescriptor* DescriptorBuilder::NewPlaceholderFile( 2502 const string& name) { 2503 FileDescriptor* placeholder = tables_->Allocate<FileDescriptor>(); 2504 memset(placeholder, 0, sizeof(*placeholder)); 2505 2506 placeholder->name_ = tables_->AllocateString(name); 2507 placeholder->package_ = &kEmptyString; 2508 placeholder->pool_ = pool_; 2509 placeholder->options_ = &FileOptions::default_instance(); 2510 placeholder->tables_ = &FileDescriptorTables::kEmpty; 2511 // All other fields are zero or NULL. 2512 2513 return placeholder; 2514 } 2515 2516 bool DescriptorBuilder::AddSymbol( 2517 const string& full_name, const void* parent, const string& name, 2518 const Message& proto, Symbol symbol) { 2519 // If the caller passed NULL for the parent, the symbol is at file scope. 2520 // Use its file as the parent instead. 2521 if (parent == NULL) parent = file_; 2522 2523 if (tables_->AddSymbol(full_name, symbol)) { 2524 if (!file_tables_->AddAliasUnderParent(parent, name, symbol)) { 2525 GOOGLE_LOG(DFATAL) << "\"" << full_name << "\" not previously defined in " 2526 "symbols_by_name_, but was defined in symbols_by_parent_; " 2527 "this shouldn't be possible."; 2528 return false; 2529 } 2530 return true; 2531 } else { 2532 const FileDescriptor* other_file = tables_->FindSymbol(full_name).GetFile(); 2533 if (other_file == file_) { 2534 string::size_type dot_pos = full_name.find_last_of('.'); 2535 if (dot_pos == string::npos) { 2536 AddError(full_name, proto, DescriptorPool::ErrorCollector::NAME, 2537 "\"" + full_name + "\" is already defined."); 2538 } else { 2539 AddError(full_name, proto, DescriptorPool::ErrorCollector::NAME, 2540 "\"" + full_name.substr(dot_pos + 1) + 2541 "\" is already defined in \"" + 2542 full_name.substr(0, dot_pos) + "\"."); 2543 } 2544 } else { 2545 // Symbol seems to have been defined in a different file. 2546 AddError(full_name, proto, DescriptorPool::ErrorCollector::NAME, 2547 "\"" + full_name + "\" is already defined in file \"" + 2548 other_file->name() + "\"."); 2549 } 2550 return false; 2551 } 2552 } 2553 2554 void DescriptorBuilder::AddPackage( 2555 const string& name, const Message& proto, const FileDescriptor* file) { 2556 if (tables_->AddSymbol(name, Symbol(file))) { 2557 // Success. Also add parent package, if any. 2558 string::size_type dot_pos = name.find_last_of('.'); 2559 if (dot_pos == string::npos) { 2560 // No parents. 2561 ValidateSymbolName(name, name, proto); 2562 } else { 2563 // Has parent. 2564 string* parent_name = tables_->AllocateString(name.substr(0, dot_pos)); 2565 AddPackage(*parent_name, proto, file); 2566 ValidateSymbolName(name.substr(dot_pos + 1), name, proto); 2567 } 2568 } else { 2569 Symbol existing_symbol = tables_->FindSymbol(name); 2570 // It's OK to redefine a package. 2571 if (existing_symbol.type != Symbol::PACKAGE) { 2572 // Symbol seems to have been defined in a different file. 2573 AddError(name, proto, DescriptorPool::ErrorCollector::NAME, 2574 "\"" + name + "\" is already defined (as something other than " 2575 "a package) in file \"" + existing_symbol.GetFile()->name() + 2576 "\"."); 2577 } 2578 } 2579 } 2580 2581 void DescriptorBuilder::ValidateSymbolName( 2582 const string& name, const string& full_name, const Message& proto) { 2583 if (name.empty()) { 2584 AddError(full_name, proto, DescriptorPool::ErrorCollector::NAME, 2585 "Missing name."); 2586 } else { 2587 for (int i = 0; i < name.size(); i++) { 2588 // I don't trust isalnum() due to locales. :( 2589 if ((name[i] < 'a' || 'z' < name[i]) && 2590 (name[i] < 'A' || 'Z' < name[i]) && 2591 (name[i] < '0' || '9' < name[i]) && 2592 (name[i] != '_')) { 2593 AddError(full_name, proto, DescriptorPool::ErrorCollector::NAME, 2594 "\"" + name + "\" is not a valid identifier."); 2595 } 2596 } 2597 } 2598 } 2599 2600 bool DescriptorBuilder::ValidateQualifiedName(const string& name) { 2601 bool last_was_period = false; 2602 2603 for (int i = 0; i < name.size(); i++) { 2604 // I don't trust isalnum() due to locales. :( 2605 if (('a' <= name[i] && name[i] <= 'z') || 2606 ('A' <= name[i] && name[i] <= 'Z') || 2607 ('0' <= name[i] && name[i] <= '9') || 2608 (name[i] == '_')) { 2609 last_was_period = false; 2610 } else if (name[i] == '.') { 2611 if (last_was_period) return false; 2612 last_was_period = true; 2613 } else { 2614 return false; 2615 } 2616 } 2617 2618 return !name.empty() && !last_was_period; 2619 } 2620 2621 // ------------------------------------------------------------------- 2622 2623 // This generic implementation is good for all descriptors except 2624 // FileDescriptor. 2625 template<class DescriptorT> void DescriptorBuilder::AllocateOptions( 2626 const typename DescriptorT::OptionsType& orig_options, 2627 DescriptorT* descriptor) { 2628 AllocateOptionsImpl(descriptor->full_name(), descriptor->full_name(), 2629 orig_options, descriptor); 2630 } 2631 2632 // We specialize for FileDescriptor. 2633 void DescriptorBuilder::AllocateOptions(const FileOptions& orig_options, 2634 FileDescriptor* descriptor) { 2635 // We add the dummy token so that LookupSymbol does the right thing. 2636 AllocateOptionsImpl(descriptor->package() + ".dummy", descriptor->name(), 2637 orig_options, descriptor); 2638 } 2639 2640 template<class DescriptorT> void DescriptorBuilder::AllocateOptionsImpl( 2641 const string& name_scope, 2642 const string& element_name, 2643 const typename DescriptorT::OptionsType& orig_options, 2644 DescriptorT* descriptor) { 2645 // We need to use a dummy pointer to work around a bug in older versions of 2646 // GCC. Otherwise, the following two lines could be replaced with: 2647 // typename DescriptorT::OptionsType* options = 2648 // tables_->AllocateMessage<typename DescriptorT::OptionsType>(); 2649 typename DescriptorT::OptionsType* const dummy = NULL; 2650 typename DescriptorT::OptionsType* options = tables_->AllocateMessage(dummy); 2651 options->CopyFrom(orig_options); 2652 descriptor->options_ = options; 2653 2654 // Don't add to options_to_interpret_ unless there were uninterpreted 2655 // options. This not only avoids unnecessary work, but prevents a 2656 // bootstrapping problem when building descriptors for descriptor.proto. 2657 // descriptor.proto does not contain any uninterpreted options, but 2658 // attempting to interpret options anyway will cause 2659 // OptionsType::GetDescriptor() to be called which may then deadlock since 2660 // we're still trying to build it. 2661 if (options->uninterpreted_option_size() > 0) { 2662 options_to_interpret_.push_back( 2663 OptionsToInterpret(name_scope, element_name, &orig_options, options)); 2664 } 2665 } 2666 2667 2668 // A common pattern: We want to convert a repeated field in the descriptor 2669 // to an array of values, calling some method to build each value. 2670 #define BUILD_ARRAY(INPUT, OUTPUT, NAME, METHOD, PARENT) \ 2671 OUTPUT->NAME##_count_ = INPUT.NAME##_size(); \ 2672 AllocateArray(INPUT.NAME##_size(), &OUTPUT->NAME##s_); \ 2673 for (int i = 0; i < INPUT.NAME##_size(); i++) { \ 2674 METHOD(INPUT.NAME(i), PARENT, OUTPUT->NAME##s_ + i); \ 2675 } 2676 2677 const FileDescriptor* DescriptorBuilder::BuildFile( 2678 const FileDescriptorProto& proto) { 2679 filename_ = proto.name(); 2680 2681 // Check if the file already exists and is identical to the one being built. 2682 // Note: This only works if the input is canonical -- that is, it 2683 // fully-qualifies all type names, has no UninterpretedOptions, etc. 2684 // This is fine, because this idempotency "feature" really only exists to 2685 // accomodate one hack in the proto1->proto2 migration layer. 2686 const FileDescriptor* existing_file = tables_->FindFile(filename_); 2687 if (existing_file != NULL) { 2688 // File already in pool. Compare the existing one to the input. 2689 FileDescriptorProto existing_proto; 2690 existing_file->CopyTo(&existing_proto); 2691 if (existing_proto.SerializeAsString() == proto.SerializeAsString()) { 2692 // They're identical. Return the existing descriptor. 2693 return existing_file; 2694 } 2695 2696 // Not a match. The error will be detected and handled later. 2697 } 2698 2699 // Check to see if this file is already on the pending files list. 2700 // TODO(kenton): Allow recursive imports? It may not work with some 2701 // (most?) programming languages. E.g., in C++, a forward declaration 2702 // of a type is not sufficient to allow it to be used even in a 2703 // generated header file due to inlining. This could perhaps be 2704 // worked around using tricks involving inserting #include statements 2705 // mid-file, but that's pretty ugly, and I'm pretty sure there are 2706 // some languages out there that do not allow recursive dependencies 2707 // at all. 2708 for (int i = 0; i < tables_->pending_files_.size(); i++) { 2709 if (tables_->pending_files_[i] == proto.name()) { 2710 string error_message("File recursively imports itself: "); 2711 for (; i < tables_->pending_files_.size(); i++) { 2712 error_message.append(tables_->pending_files_[i]); 2713 error_message.append(" -> "); 2714 } 2715 error_message.append(proto.name()); 2716 2717 AddError(proto.name(), proto, DescriptorPool::ErrorCollector::OTHER, 2718 error_message); 2719 return NULL; 2720 } 2721 } 2722 2723 // If we have a fallback_database_, attempt to load all dependencies now, 2724 // before checkpointing tables_. This avoids confusion with recursive 2725 // checkpoints. 2726 if (pool_->fallback_database_ != NULL) { 2727 tables_->pending_files_.push_back(proto.name()); 2728 for (int i = 0; i < proto.dependency_size(); i++) { 2729 if (tables_->FindFile(proto.dependency(i)) == NULL && 2730 (pool_->underlay_ == NULL || 2731 pool_->underlay_->FindFileByName(proto.dependency(i)) == NULL)) { 2732 // We don't care what this returns since we'll find out below anyway. 2733 pool_->TryFindFileInFallbackDatabase(proto.dependency(i)); 2734 } 2735 } 2736 tables_->pending_files_.pop_back(); 2737 } 2738 2739 // Checkpoint the tables so that we can roll back if something goes wrong. 2740 tables_->Checkpoint(); 2741 2742 FileDescriptor* result = tables_->Allocate<FileDescriptor>(); 2743 file_ = result; 2744 2745 file_tables_ = tables_->AllocateFileTables(); 2746 file_->tables_ = file_tables_; 2747 2748 if (!proto.has_name()) { 2749 AddError("", proto, DescriptorPool::ErrorCollector::OTHER, 2750 "Missing field: FileDescriptorProto.name."); 2751 } 2752 2753 result->name_ = tables_->AllocateString(proto.name()); 2754 if (proto.has_package()) { 2755 result->package_ = tables_->AllocateString(proto.package()); 2756 } else { 2757 // We cannot rely on proto.package() returning a valid string if 2758 // proto.has_package() is false, because we might be running at static 2759 // initialization time, in which case default values have not yet been 2760 // initialized. 2761 result->package_ = tables_->AllocateString(""); 2762 } 2763 result->pool_ = pool_; 2764 2765 // Add to tables. 2766 if (!tables_->AddFile(result)) { 2767 AddError(proto.name(), proto, DescriptorPool::ErrorCollector::OTHER, 2768 "A file with this name is already in the pool."); 2769 // Bail out early so that if this is actually the exact same file, we 2770 // don't end up reporting that every single symbol is already defined. 2771 tables_->Rollback(); 2772 return NULL; 2773 } 2774 if (!result->package().empty()) { 2775 AddPackage(result->package(), proto, result); 2776 } 2777 2778 // Make sure all dependencies are loaded. 2779 set<string> seen_dependencies; 2780 result->dependency_count_ = proto.dependency_size(); 2781 result->dependencies_ = 2782 tables_->AllocateArray<const FileDescriptor*>(proto.dependency_size()); 2783 for (int i = 0; i < proto.dependency_size(); i++) { 2784 if (!seen_dependencies.insert(proto.dependency(i)).second) { 2785 AddError(proto.name(), proto, 2786 DescriptorPool::ErrorCollector::OTHER, 2787 "Import \"" + proto.dependency(i) + "\" was listed twice."); 2788 } 2789 2790 const FileDescriptor* dependency = tables_->FindFile(proto.dependency(i)); 2791 if (dependency == NULL && pool_->underlay_ != NULL) { 2792 dependency = pool_->underlay_->FindFileByName(proto.dependency(i)); 2793 } 2794 2795 if (dependency == NULL) { 2796 if (pool_->allow_unknown_) { 2797 dependency = NewPlaceholderFile(proto.dependency(i)); 2798 } else { 2799 string message; 2800 if (pool_->fallback_database_ == NULL) { 2801 message = "Import \"" + proto.dependency(i) + 2802 "\" has not been loaded."; 2803 } else { 2804 message = "Import \"" + proto.dependency(i) + 2805 "\" was not found or had errors."; 2806 } 2807 AddError(proto.name(), proto, 2808 DescriptorPool::ErrorCollector::OTHER, 2809 message); 2810 } 2811 } 2812 2813 result->dependencies_[i] = dependency; 2814 } 2815 2816 // Convert children. 2817 BUILD_ARRAY(proto, result, message_type, BuildMessage , NULL); 2818 BUILD_ARRAY(proto, result, enum_type , BuildEnum , NULL); 2819 BUILD_ARRAY(proto, result, service , BuildService , NULL); 2820 BUILD_ARRAY(proto, result, extension , BuildExtension, NULL); 2821 2822 // Copy options. 2823 if (!proto.has_options()) { 2824 result->options_ = NULL; // Will set to default_instance later. 2825 } else { 2826 AllocateOptions(proto.options(), result); 2827 } 2828 2829 // Note that the following steps must occur in exactly the specified order. 2830 2831 // Cross-link. 2832 CrossLinkFile(result, proto); 2833 2834 // Interpret any remaining uninterpreted options gathered into 2835 // options_to_interpret_ during descriptor building. Cross-linking has made 2836 // extension options known, so all interpretations should now succeed. 2837 if (!had_errors_) { 2838 OptionInterpreter option_interpreter(this); 2839 for (vector<OptionsToInterpret>::iterator iter = 2840 options_to_interpret_.begin(); 2841 iter != options_to_interpret_.end(); ++iter) { 2842 option_interpreter.InterpretOptions(&(*iter)); 2843 } 2844 options_to_interpret_.clear(); 2845 } 2846 2847 // Validate options. 2848 if (!had_errors_) { 2849 ValidateFileOptions(result, proto); 2850 } 2851 2852 if (had_errors_) { 2853 tables_->Rollback(); 2854 return NULL; 2855 } else { 2856 tables_->Checkpoint(); 2857 return result; 2858 } 2859 } 2860 2861 void DescriptorBuilder::BuildMessage(const DescriptorProto& proto, 2862 const Descriptor* parent, 2863 Descriptor* result) { 2864 const string& scope = (parent == NULL) ? 2865 file_->package() : parent->full_name(); 2866 string* full_name = tables_->AllocateString(scope); 2867 if (!full_name->empty()) full_name->append(1, '.'); 2868 full_name->append(proto.name()); 2869 2870 ValidateSymbolName(proto.name(), *full_name, proto); 2871 2872 result->name_ = tables_->AllocateString(proto.name()); 2873 result->full_name_ = full_name; 2874 result->file_ = file_; 2875 result->containing_type_ = parent; 2876 result->is_placeholder_ = false; 2877 result->is_unqualified_placeholder_ = false; 2878 2879 BUILD_ARRAY(proto, result, field , BuildField , result); 2880 BUILD_ARRAY(proto, result, nested_type , BuildMessage , result); 2881 BUILD_ARRAY(proto, result, enum_type , BuildEnum , result); 2882 BUILD_ARRAY(proto, result, extension_range, BuildExtensionRange, result); 2883 BUILD_ARRAY(proto, result, extension , BuildExtension , result); 2884 2885 // Copy options. 2886 if (!proto.has_options()) { 2887 result->options_ = NULL; // Will set to default_instance later. 2888 } else { 2889 AllocateOptions(proto.options(), result); 2890 } 2891 2892 AddSymbol(result->full_name(), parent, result->name(), 2893 proto, Symbol(result)); 2894 2895 // Check that no fields have numbers in extension ranges. 2896 for (int i = 0; i < result->field_count(); i++) { 2897 const FieldDescriptor* field = result->field(i); 2898 for (int j = 0; j < result->extension_range_count(); j++) { 2899 const Descriptor::ExtensionRange* range = result->extension_range(j); 2900 if (range->start <= field->number() && field->number() < range->end) { 2901 AddError(field->full_name(), proto.extension_range(j), 2902 DescriptorPool::ErrorCollector::NUMBER, 2903 strings::Substitute( 2904 "Extension range $0 to $1 includes field \"$2\" ($3).", 2905 range->start, range->end - 1, 2906 field->name(), field->number())); 2907 } 2908 } 2909 } 2910 2911 // Check that extension ranges don't overlap. 2912 for (int i = 0; i < result->extension_range_count(); i++) { 2913 const Descriptor::ExtensionRange* range1 = result->extension_range(i); 2914 for (int j = i + 1; j < result->extension_range_count(); j++) { 2915 const Descriptor::ExtensionRange* range2 = result->extension_range(j); 2916 if (range1->end > range2->start && range2->end > range1->start) { 2917 AddError(result->full_name(), proto.extension_range(j), 2918 DescriptorPool::ErrorCollector::NUMBER, 2919 strings::Substitute("Extension range $0 to $1 overlaps with " 2920 "already-defined range $2 to $3.", 2921 range2->start, range2->end - 1, 2922 range1->start, range1->end - 1)); 2923 } 2924 } 2925 } 2926 } 2927 2928 void DescriptorBuilder::BuildFieldOrExtension(const FieldDescriptorProto& proto, 2929 const Descriptor* parent, 2930 FieldDescriptor* result, 2931 bool is_extension) { 2932 const string& scope = (parent == NULL) ? 2933 file_->package() : parent->full_name(); 2934 string* full_name = tables_->AllocateString(scope); 2935 if (!full_name->empty()) full_name->append(1, '.'); 2936 full_name->append(proto.name()); 2937 2938 ValidateSymbolName(proto.name(), *full_name, proto); 2939 2940 result->name_ = tables_->AllocateString(proto.name()); 2941 result->full_name_ = full_name; 2942 result->file_ = file_; 2943 result->number_ = proto.number(); 2944 result->is_extension_ = is_extension; 2945 2946 // If .proto files follow the style guide then the name should already be 2947 // lower-cased. If that's the case we can just reuse the string we already 2948 // allocated rather than allocate a new one. 2949 string lowercase_name(proto.name()); 2950 LowerString(&lowercase_name); 2951 if (lowercase_name == proto.name()) { 2952 result->lowercase_name_ = result->name_; 2953 } else { 2954 result->lowercase_name_ = tables_->AllocateString(lowercase_name); 2955 } 2956 2957 // Don't bother with the above optimization for camel-case names since 2958 // .proto files that follow the guide shouldn't be using names in this 2959 // format, so the optimization wouldn't help much. 2960 result->camelcase_name_ = tables_->AllocateString(ToCamelCase(proto.name())); 2961 2962 // Some compilers do not allow static_cast directly between two enum types, 2963 // so we must cast to int first. 2964 result->type_ = static_cast<FieldDescriptor::Type>( 2965 implicit_cast<int>(proto.type())); 2966 result->label_ = static_cast<FieldDescriptor::Label>( 2967 implicit_cast<int>(proto.label())); 2968 2969 // Some of these may be filled in when cross-linking. 2970 result->containing_type_ = NULL; 2971 result->extension_scope_ = NULL; 2972 result->experimental_map_key_ = NULL; 2973 result->message_type_ = NULL; 2974 result->enum_type_ = NULL; 2975 2976 result->has_default_value_ = proto.has_default_value(); 2977 if (proto.has_default_value() && result->is_repeated()) { 2978 AddError(result->full_name(), proto, 2979 DescriptorPool::ErrorCollector::DEFAULT_VALUE, 2980 "Repeated fields can't have default values."); 2981 } 2982 2983 if (proto.has_type()) { 2984 if (proto.has_default_value()) { 2985 char* end_pos = NULL; 2986 switch (result->cpp_type()) { 2987 case FieldDescriptor::CPPTYPE_INT32: 2988 result->default_value_int32_ = 2989 strtol(proto.default_value().c_str(), &end_pos, 0); 2990 break; 2991 case FieldDescriptor::CPPTYPE_INT64: 2992 result->default_value_int64_ = 2993 strto64(proto.default_value().c_str(), &end_pos, 0); 2994 break; 2995 case FieldDescriptor::CPPTYPE_UINT32: 2996 result->default_value_uint32_ = 2997 strtoul(proto.default_value().c_str(), &end_pos, 0); 2998 break; 2999 case FieldDescriptor::CPPTYPE_UINT64: 3000 result->default_value_uint64_ = 3001 strtou64(proto.default_value().c_str(), &end_pos, 0); 3002 break; 3003 case FieldDescriptor::CPPTYPE_FLOAT: 3004 if (proto.default_value() == "inf") { 3005 result->default_value_float_ = numeric_limits<float>::infinity(); 3006 } else if (proto.default_value() == "-inf") { 3007 result->default_value_float_ = -numeric_limits<float>::infinity(); 3008 } else if (proto.default_value() == "nan") { 3009 result->default_value_float_ = numeric_limits<float>::quiet_NaN(); 3010 } else { 3011 result->default_value_float_ = 3012 NoLocaleStrtod(proto.default_value().c_str(), &end_pos); 3013 } 3014 break; 3015 case FieldDescriptor::CPPTYPE_DOUBLE: 3016 if (proto.default_value() == "inf") { 3017 result->default_value_double_ = numeric_limits<double>::infinity(); 3018 } else if (proto.default_value() == "-inf") { 3019 result->default_value_double_ = -numeric_limits<double>::infinity(); 3020 } else if (proto.default_value() == "nan") { 3021 result->default_value_double_ = numeric_limits<double>::quiet_NaN(); 3022 } else { 3023 result->default_value_double_ = 3024 NoLocaleStrtod(proto.default_value().c_str(), &end_pos); 3025 } 3026 break; 3027 case FieldDescriptor::CPPTYPE_BOOL: 3028 if (proto.default_value() == "true") { 3029 result->default_value_bool_ = true; 3030 } else if (proto.default_value() == "false") { 3031 result->default_value_bool_ = false; 3032 } else { 3033 AddError(result->full_name(), proto, 3034 DescriptorPool::ErrorCollector::DEFAULT_VALUE, 3035 "Boolean default must be true or false."); 3036 } 3037 break; 3038 case FieldDescriptor::CPPTYPE_ENUM: 3039 // This will be filled in when cross-linking. 3040 result->default_value_enum_ = NULL; 3041 break; 3042 case FieldDescriptor::CPPTYPE_STRING: 3043 if (result->type() == FieldDescriptor::TYPE_BYTES) { 3044 result->default_value_string_ = tables_->AllocateString( 3045 UnescapeCEscapeString(proto.default_value())); 3046 } else { 3047 result->default_value_string_ = 3048 tables_->AllocateString(proto.default_value()); 3049 } 3050 break; 3051 case FieldDescriptor::CPPTYPE_MESSAGE: 3052 AddError(result->full_name(), proto, 3053 DescriptorPool::ErrorCollector::DEFAULT_VALUE, 3054 "Messages can't have default values."); 3055 result->has_default_value_ = false; 3056 break; 3057 } 3058 3059 if (end_pos != NULL) { 3060 // end_pos is only set non-NULL by the parsers for numeric types, above. 3061 // This checks that the default was non-empty and had no extra junk 3062 // after the end of the number. 3063 if (proto.default_value().empty() || *end_pos != '\0') { 3064 AddError(result->full_name(), proto, 3065 DescriptorPool::ErrorCollector::DEFAULT_VALUE, 3066 "Couldn't parse default value."); 3067 } 3068 } 3069 } else { 3070 // No explicit default value 3071 switch (result->cpp_type()) { 3072 case FieldDescriptor::CPPTYPE_INT32: 3073 result->default_value_int32_ = 0; 3074 break; 3075 case FieldDescriptor::CPPTYPE_INT64: 3076 result->default_value_int64_ = 0; 3077 break; 3078 case FieldDescriptor::CPPTYPE_UINT32: 3079 result->default_value_uint32_ = 0; 3080 break; 3081 case FieldDescriptor::CPPTYPE_UINT64: 3082 result->default_value_uint64_ = 0; 3083 break; 3084 case FieldDescriptor::CPPTYPE_FLOAT: 3085 result->default_value_float_ = 0.0f; 3086 break; 3087 case FieldDescriptor::CPPTYPE_DOUBLE: 3088 result->default_value_double_ = 0.0; 3089 break; 3090 case FieldDescriptor::CPPTYPE_BOOL: 3091 result->default_value_bool_ = false; 3092 break; 3093 case FieldDescriptor::CPPTYPE_ENUM: 3094 // This will be filled in when cross-linking. 3095 result->default_value_enum_ = NULL; 3096 break; 3097 case FieldDescriptor::CPPTYPE_STRING: 3098 result->default_value_string_ = &kEmptyString; 3099 break; 3100 case FieldDescriptor::CPPTYPE_MESSAGE: 3101 break; 3102 } 3103 } 3104 } 3105 3106 if (result->number() <= 0) { 3107 AddError(result->full_name(), proto, DescriptorPool::ErrorCollector::NUMBER, 3108 "Field numbers must be positive integers."); 3109 } else if (result->number() > FieldDescriptor::kMaxNumber) { 3110 AddError(result->full_name(), proto, DescriptorPool::ErrorCollector::NUMBER, 3111 strings::Substitute("Field numbers cannot be greater than $0.", 3112 FieldDescriptor::kMaxNumber)); 3113 } else if (result->number() >= FieldDescriptor::kFirstReservedNumber && 3114 result->number() <= FieldDescriptor::kLastReservedNumber) { 3115 AddError(result->full_name(), proto, DescriptorPool::ErrorCollector::NUMBER, 3116 strings::Substitute( 3117 "Field numbers $0 through $1 are reserved for the protocol " 3118 "buffer library implementation.", 3119 FieldDescriptor::kFirstReservedNumber, 3120 FieldDescriptor::kLastReservedNumber)); 3121 } 3122 3123 if (is_extension) { 3124 if (!proto.has_extendee()) { 3125 AddError(result->full_name(), proto, 3126 DescriptorPool::ErrorCollector::EXTENDEE, 3127 "FieldDescriptorProto.extendee not set for extension field."); 3128 } 3129 3130 result->extension_scope_ = parent; 3131 } else { 3132 if (proto.has_extendee()) { 3133 AddError(result->full_name(), proto, 3134 DescriptorPool::ErrorCollector::EXTENDEE, 3135 "FieldDescriptorProto.extendee set for non-extension field."); 3136 } 3137 3138 result->containing_type_ = parent; 3139 } 3140 3141 // Copy options. 3142 if (!proto.has_options()) { 3143 result->options_ = NULL; // Will set to default_instance later. 3144 } else { 3145 AllocateOptions(proto.options(), result); 3146 } 3147 3148 AddSymbol(result->full_name(), parent, result->name(), 3149 proto, Symbol(result)); 3150 } 3151 3152 void DescriptorBuilder::BuildExtensionRange( 3153 const DescriptorProto::ExtensionRange& proto, 3154 const Descriptor* parent, 3155 Descriptor::ExtensionRange* result) { 3156 result->start = proto.start(); 3157 result->end = proto.end(); 3158 if (result->start <= 0) { 3159 AddError(parent->full_name(), proto, 3160 DescriptorPool::ErrorCollector::NUMBER, 3161 "Extension numbers must be positive integers."); 3162 } 3163 3164 if (result->end > FieldDescriptor::kMaxNumber + 1) { 3165 AddError(parent->full_name(), proto, 3166 DescriptorPool::ErrorCollector::NUMBER, 3167 strings::Substitute("Extension numbers cannot be greater than $0.", 3168 FieldDescriptor::kMaxNumber)); 3169 } 3170 3171 if (result->start >= result->end) { 3172 AddError(parent->full_name(), proto, 3173 DescriptorPool::ErrorCollector::NUMBER, 3174 "Extension range end number must be greater than start number."); 3175 } 3176 } 3177 3178 void DescriptorBuilder::BuildEnum(const EnumDescriptorProto& proto, 3179 const Descriptor* parent, 3180 EnumDescriptor* result) { 3181 const string& scope = (parent == NULL) ? 3182 file_->package() : parent->full_name(); 3183 string* full_name = tables_->AllocateString(scope); 3184 if (!full_name->empty()) full_name->append(1, '.'); 3185 full_name->append(proto.name()); 3186 3187 ValidateSymbolName(proto.name(), *full_name, proto); 3188 3189 result->name_ = tables_->AllocateString(proto.name()); 3190 result->full_name_ = full_name; 3191 result->file_ = file_; 3192 result->containing_type_ = parent; 3193 result->is_placeholder_ = false; 3194 result->is_unqualified_placeholder_ = false; 3195 3196 if (proto.value_size() == 0) { 3197 // We cannot allow enums with no values because this would mean there 3198 // would be no valid default value for fields of this type. 3199 AddError(result->full_name(), proto, 3200 DescriptorPool::ErrorCollector::NAME, 3201 "Enums must contain at least one value."); 3202 } 3203 3204 BUILD_ARRAY(proto, result, value, BuildEnumValue, result); 3205 3206 // Copy options. 3207 if (!proto.has_options()) { 3208 result->options_ = NULL; // Will set to default_instance later. 3209 } else { 3210 AllocateOptions(proto.options(), result); 3211 } 3212 3213 AddSymbol(result->full_name(), parent, result->name(), 3214 proto, Symbol(result)); 3215 } 3216 3217 void DescriptorBuilder::BuildEnumValue(const EnumValueDescriptorProto& proto, 3218 const EnumDescriptor* parent, 3219 EnumValueDescriptor* result) { 3220 result->name_ = tables_->AllocateString(proto.name()); 3221 result->number_ = proto.number(); 3222 result->type_ = parent; 3223 3224 // Note: full_name for enum values is a sibling to the parent's name, not a 3225 // child of it. 3226 string* full_name = tables_->AllocateString(*parent->full_name_); 3227 full_name->resize(full_name->size() - parent->name_->size()); 3228 full_name->append(*result->name_); 3229 result->full_name_ = full_name; 3230 3231 ValidateSymbolName(proto.name(), *full_name, proto); 3232 3233 // Copy options. 3234 if (!proto.has_options()) { 3235 result->options_ = NULL; // Will set to default_instance later. 3236 } else { 3237 AllocateOptions(proto.options(), result); 3238 } 3239 3240 // Again, enum values are weird because we makes them appear as siblings 3241 // of the enum type instead of children of it. So, we use 3242 // parent->containing_type() as the value's parent. 3243 bool added_to_outer_scope = 3244 AddSymbol(result->full_name(), parent->containing_type(), result->name(), 3245 proto, Symbol(result)); 3246 3247 // However, we also want to be able to search for values within a single 3248 // enum type, so we add it as a child of the enum type itself, too. 3249 // Note: This could fail, but if it does, the error has already been 3250 // reported by the above AddSymbol() call, so we ignore the return code. 3251 bool added_to_inner_scope = 3252 file_tables_->AddAliasUnderParent(parent, result->name(), Symbol(result)); 3253 3254 if (added_to_inner_scope && !added_to_outer_scope) { 3255 // This value did not conflict with any values defined in the same enum, 3256 // but it did conflict with some other symbol defined in the enum type's 3257 // scope. Let's print an additional error to explain this. 3258 string outer_scope; 3259 if (parent->containing_type() == NULL) { 3260 outer_scope = file_->package(); 3261 } else { 3262 outer_scope = parent->containing_type()->full_name(); 3263 } 3264 3265 if (outer_scope.empty()) { 3266 outer_scope = "the global scope"; 3267 } else { 3268 outer_scope = "\"" + outer_scope + "\""; 3269 } 3270 3271 AddError(result->full_name(), proto, 3272 DescriptorPool::ErrorCollector::NAME, 3273 "Note that enum values use C++ scoping rules, meaning that " 3274 "enum values are siblings of their type, not children of it. " 3275 "Therefore, \"" + result->name() + "\" must be unique within " 3276 + outer_scope + ", not just within \"" + parent->name() + "\"."); 3277 } 3278 3279 // An enum is allowed to define two numbers that refer to the same value. 3280 // FindValueByNumber() should return the first such value, so we simply 3281 // ignore AddEnumValueByNumber()'s return code. 3282 file_tables_->AddEnumValueByNumber(result); 3283 } 3284 3285 void DescriptorBuilder::BuildService(const ServiceDescriptorProto& proto, 3286 const void* dummy, 3287 ServiceDescriptor* result) { 3288 string* full_name = tables_->AllocateString(file_->package()); 3289 if (!full_name->empty()) full_name->append(1, '.'); 3290 full_name->append(proto.name()); 3291 3292 ValidateSymbolName(proto.name(), *full_name, proto); 3293 3294 result->name_ = tables_->AllocateString(proto.name()); 3295 result->full_name_ = full_name; 3296 result->file_ = file_; 3297 3298 BUILD_ARRAY(proto, result, method, BuildMethod, result); 3299 3300 // Copy options. 3301 if (!proto.has_options()) { 3302 result->options_ = NULL; // Will set to default_instance later. 3303 } else { 3304 AllocateOptions(proto.options(), result); 3305 } 3306 3307 AddSymbol(result->full_name(), NULL, result->name(), 3308 proto, Symbol(result)); 3309 } 3310 3311 void DescriptorBuilder::BuildMethod(const MethodDescriptorProto& proto, 3312 const ServiceDescriptor* parent, 3313 MethodDescriptor* result) { 3314 result->name_ = tables_->AllocateString(proto.name()); 3315 result->service_ = parent; 3316 3317 string* full_name = tables_->AllocateString(parent->full_name()); 3318 full_name->append(1, '.'); 3319 full_name->append(*result->name_); 3320 result->full_name_ = full_name; 3321 3322 ValidateSymbolName(proto.name(), *full_name, proto); 3323 3324 // These will be filled in when cross-linking. 3325 result->input_type_ = NULL; 3326 result->output_type_ = NULL; 3327 3328 // Copy options. 3329 if (!proto.has_options()) { 3330 result->options_ = NULL; // Will set to default_instance later. 3331 } else { 3332 AllocateOptions(proto.options(), result); 3333 } 3334 3335 AddSymbol(result->full_name(), parent, result->name(), 3336 proto, Symbol(result)); 3337 } 3338 3339 #undef BUILD_ARRAY 3340 3341 // ------------------------------------------------------------------- 3342 3343 void DescriptorBuilder::CrossLinkFile( 3344 FileDescriptor* file, const FileDescriptorProto& proto) { 3345 if (file->options_ == NULL) { 3346 file->options_ = &FileOptions::default_instance(); 3347 } 3348 3349 for (int i = 0; i < file->message_type_count(); i++) { 3350 CrossLinkMessage(&file->message_types_[i], proto.message_type(i)); 3351 } 3352 3353 for (int i = 0; i < file->extension_count(); i++) { 3354 CrossLinkField(&file->extensions_[i], proto.extension(i)); 3355 } 3356 3357 for (int i = 0; i < file->enum_type_count(); i++) { 3358 CrossLinkEnum(&file->enum_types_[i], proto.enum_type(i)); 3359 } 3360 3361 for (int i = 0; i < file->service_count(); i++) { 3362 CrossLinkService(&file->services_[i], proto.service(i)); 3363 } 3364 } 3365 3366 void DescriptorBuilder::CrossLinkMessage( 3367 Descriptor* message, const DescriptorProto& proto) { 3368 if (message->options_ == NULL) { 3369 message->options_ = &MessageOptions::default_instance(); 3370 } 3371 3372 for (int i = 0; i < message->nested_type_count(); i++) { 3373 CrossLinkMessage(&message->nested_types_[i], proto.nested_type(i)); 3374 } 3375 3376 for (int i = 0; i < message->enum_type_count(); i++) { 3377 CrossLinkEnum(&message->enum_types_[i], proto.enum_type(i)); 3378 } 3379 3380 for (int i = 0; i < message->field_count(); i++) { 3381 CrossLinkField(&message->fields_[i], proto.field(i)); 3382 } 3383 3384 for (int i = 0; i < message->extension_count(); i++) { 3385 CrossLinkField(&message->extensions_[i], proto.extension(i)); 3386 } 3387 } 3388 3389 void DescriptorBuilder::CrossLinkField( 3390 FieldDescriptor* field, const FieldDescriptorProto& proto) { 3391 if (field->options_ == NULL) { 3392 field->options_ = &FieldOptions::default_instance(); 3393 } 3394 3395 if (proto.has_extendee()) { 3396 Symbol extendee = LookupSymbol(proto.extendee(), field->full_name(), 3397 PLACEHOLDER_EXTENDABLE_MESSAGE); 3398 if (extendee.IsNull()) { 3399 AddNotDefinedError(field->full_name(), proto, 3400 DescriptorPool::ErrorCollector::EXTENDEE, 3401 proto.extendee()); 3402 return; 3403 } else if (extendee.type != Symbol::MESSAGE) { 3404 AddError(field->full_name(), proto, 3405 DescriptorPool::ErrorCollector::EXTENDEE, 3406 "\"" + proto.extendee() + "\" is not a message type."); 3407 return; 3408 } 3409 field->containing_type_ = extendee.descriptor; 3410 3411 if (!field->containing_type()->IsExtensionNumber(field->number())) { 3412 AddError(field->full_name(), proto, 3413 DescriptorPool::ErrorCollector::NUMBER, 3414 strings::Substitute("\"$0\" does not declare $1 as an " 3415 "extension number.", 3416 field->containing_type()->full_name(), 3417 field->number())); 3418 } 3419 } 3420 3421 if (proto.has_type_name()) { 3422 // Assume we are expecting a message type unless the proto contains some 3423 // evidence that it expects an enum type. This only makes a difference if 3424 // we end up creating a placeholder. 3425 bool expecting_enum = (proto.type() == FieldDescriptorProto::TYPE_ENUM) || 3426 proto.has_default_value(); 3427 3428 Symbol type = 3429 LookupSymbol(proto.type_name(), field->full_name(), 3430 expecting_enum ? PLACEHOLDER_ENUM : PLACEHOLDER_MESSAGE, 3431 LOOKUP_TYPES); 3432 3433 if (type.IsNull()) { 3434 AddNotDefinedError(field->full_name(), proto, 3435 DescriptorPool::ErrorCollector::TYPE, 3436 proto.type_name()); 3437 return; 3438 } 3439 3440 if (!proto.has_type()) { 3441 // Choose field type based on symbol. 3442 if (type.type == Symbol::MESSAGE) { 3443 field->type_ = FieldDescriptor::TYPE_MESSAGE; 3444 } else if (type.type == Symbol::ENUM) { 3445 field->type_ = FieldDescriptor::TYPE_ENUM; 3446 } else { 3447 AddError(field->full_name(), proto, 3448 DescriptorPool::ErrorCollector::TYPE, 3449 "\"" + proto.type_name() + "\" is not a type."); 3450 return; 3451 } 3452 } 3453 3454 if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { 3455 if (type.type != Symbol::MESSAGE) { 3456 AddError(field->full_name(), proto, 3457 DescriptorPool::ErrorCollector::TYPE, 3458 "\"" + proto.type_name() + "\" is not a message type."); 3459 return; 3460 } 3461 field->message_type_ = type.descriptor; 3462 3463 if (field->has_default_value()) { 3464 AddError(field->full_name(), proto, 3465 DescriptorPool::ErrorCollector::DEFAULT_VALUE, 3466 "Messages can't have default values."); 3467 } 3468 } else if (field->cpp_type() == FieldDescriptor::CPPTYPE_ENUM) { 3469 if (type.type != Symbol::ENUM) { 3470 AddError(field->full_name(), proto, 3471 DescriptorPool::ErrorCollector::TYPE, 3472 "\"" + proto.type_name() + "\" is not an enum type."); 3473 return; 3474 } 3475 field->enum_type_ = type.enum_descriptor; 3476 3477 if (field->enum_type()->is_placeholder_) { 3478 // We can't look up default values for placeholder types. We'll have 3479 // to just drop them. 3480 field->has_default_value_ = false; 3481 } 3482 3483 if (field->has_default_value()) { 3484 // We can't just use field->enum_type()->FindValueByName() here 3485 // because that locks the pool's mutex, which we have already locked 3486 // at this point. 3487 Symbol default_value = 3488 LookupSymbolNoPlaceholder(proto.default_value(), 3489 field->enum_type()->full_name()); 3490 3491 if (default_value.type == Symbol::ENUM_VALUE && 3492 default_value.enum_value_descriptor->type() == field->enum_type()) { 3493 field->default_value_enum_ = default_value.enum_value_descriptor; 3494 } else { 3495 AddError(field->full_name(), proto, 3496 DescriptorPool::ErrorCollector::DEFAULT_VALUE, 3497 "Enum type \"" + field->enum_type()->full_name() + 3498 "\" has no value named \"" + proto.default_value() + "\"."); 3499 } 3500 } else if (field->enum_type()->value_count() > 0) { 3501 // All enums must have at least one value, or we would have reported 3502 // an error elsewhere. We use the first defined value as the default 3503 // if a default is not explicitly defined. 3504 field->default_value_enum_ = field->enum_type()->value(0); 3505 } 3506 } else { 3507 AddError(field->full_name(), proto, DescriptorPool::ErrorCollector::TYPE, 3508 "Field with primitive type has type_name."); 3509 } 3510 } else { 3511 if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE || 3512 field->cpp_type() == FieldDescriptor::CPPTYPE_ENUM) { 3513 AddError(field->full_name(), proto, DescriptorPool::ErrorCollector::TYPE, 3514 "Field with message or enum type missing type_name."); 3515 } 3516 } 3517 3518 // Add the field to the fields-by-number table. 3519 // Note: We have to do this *after* cross-linking because extensions do not 3520 // know their containing type until now. 3521 if (!file_tables_->AddFieldByNumber(field)) { 3522 const FieldDescriptor* conflicting_field = 3523 file_tables_->FindFieldByNumber(field->containing_type(), 3524 field->number()); 3525 if (field->is_extension()) { 3526 AddError(field->full_name(), proto, 3527 DescriptorPool::ErrorCollector::NUMBER, 3528 strings::Substitute("Extension number $0 has already been used " 3529 "in \"$1\" by extension \"$2\".", 3530 field->number(), 3531 field->containing_type()->full_name(), 3532 conflicting_field->full_name())); 3533 } else { 3534 AddError(field->full_name(), proto, 3535 DescriptorPool::ErrorCollector::NUMBER, 3536 strings::Substitute("Field number $0 has already been used in " 3537 "\"$1\" by field \"$2\".", 3538 field->number(), 3539 field->containing_type()->full_name(), 3540 conflicting_field->name())); 3541 } 3542 } 3543 3544 if (field->is_extension()) { 3545 // No need for error checking: if the extension number collided, 3546 // we've already been informed of it by the if() above. 3547 tables_->AddExtension(field); 3548 } 3549 3550 // Add the field to the lowercase-name and camelcase-name tables. 3551 file_tables_->AddFieldByStylizedNames(field); 3552 } 3553 3554 void DescriptorBuilder::CrossLinkEnum( 3555 EnumDescriptor* enum_type, const EnumDescriptorProto& proto) { 3556 if (enum_type->options_ == NULL) { 3557 enum_type->options_ = &EnumOptions::default_instance(); 3558 } 3559 3560 for (int i = 0; i < enum_type->value_count(); i++) { 3561 CrossLinkEnumValue(&enum_type->values_[i], proto.value(i)); 3562 } 3563 } 3564 3565 void DescriptorBuilder::CrossLinkEnumValue( 3566 EnumValueDescriptor* enum_value, const EnumValueDescriptorProto& proto) { 3567 if (enum_value->options_ == NULL) { 3568 enum_value->options_ = &EnumValueOptions::default_instance(); 3569 } 3570 } 3571 3572 void DescriptorBuilder::CrossLinkService( 3573 ServiceDescriptor* service, const ServiceDescriptorProto& proto) { 3574 if (service->options_ == NULL) { 3575 service->options_ = &ServiceOptions::default_instance(); 3576 } 3577 3578 for (int i = 0; i < service->method_count(); i++) { 3579 CrossLinkMethod(&service->methods_[i], proto.method(i)); 3580 } 3581 } 3582 3583 void DescriptorBuilder::CrossLinkMethod( 3584 MethodDescriptor* method, const MethodDescriptorProto& proto) { 3585 if (method->options_ == NULL) { 3586 method->options_ = &MethodOptions::default_instance(); 3587 } 3588 3589 Symbol input_type = LookupSymbol(proto.input_type(), method->full_name()); 3590 if (input_type.IsNull()) { 3591 AddNotDefinedError(method->full_name(), proto, 3592 DescriptorPool::ErrorCollector::INPUT_TYPE, 3593 proto.input_type()); 3594 } else if (input_type.type != Symbol::MESSAGE) { 3595 AddError(method->full_name(), proto, 3596 DescriptorPool::ErrorCollector::INPUT_TYPE, 3597 "\"" + proto.input_type() + "\" is not a message type."); 3598 } else { 3599 method->input_type_ = input_type.descriptor; 3600 } 3601 3602 Symbol output_type = LookupSymbol(proto.output_type(), method->full_name()); 3603 if (output_type.IsNull()) { 3604 AddNotDefinedError(method->full_name(), proto, 3605 DescriptorPool::ErrorCollector::OUTPUT_TYPE, 3606 proto.output_type()); 3607 } else if (output_type.type != Symbol::MESSAGE) { 3608 AddError(method->full_name(), proto, 3609 DescriptorPool::ErrorCollector::OUTPUT_TYPE, 3610 "\"" + proto.output_type() + "\" is not a message type."); 3611 } else { 3612 method->output_type_ = output_type.descriptor; 3613 } 3614 } 3615 3616 // ------------------------------------------------------------------- 3617 3618 #define VALIDATE_OPTIONS_FROM_ARRAY(descriptor, array_name, type) \ 3619 for (int i = 0; i < descriptor->array_name##_count(); ++i) { \ 3620 Validate##type##Options(descriptor->array_name##s_ + i, \ 3621 proto.array_name(i)); \ 3622 } 3623 3624 // Determine if the file uses optimize_for = LITE_RUNTIME, being careful to 3625 // avoid problems that exist at init time. 3626 static bool IsLite(const FileDescriptor* file) { 3627 // TODO(kenton): I don't even remember how many of these conditions are 3628 // actually possible. I'm just being super-safe. 3629 return file != NULL && 3630 &file->options() != NULL && 3631 &file->options() != &FileOptions::default_instance() && 3632 file->options().optimize_for() == FileOptions::LITE_RUNTIME; 3633 } 3634 3635 void DescriptorBuilder::ValidateFileOptions(FileDescriptor* file, 3636 const FileDescriptorProto& proto) { 3637 VALIDATE_OPTIONS_FROM_ARRAY(file, message_type, Message); 3638 VALIDATE_OPTIONS_FROM_ARRAY(file, enum_type, Enum); 3639 VALIDATE_OPTIONS_FROM_ARRAY(file, service, Service); 3640 VALIDATE_OPTIONS_FROM_ARRAY(file, extension, Field); 3641 3642 // Lite files can only be imported by other Lite files. 3643 if (!IsLite(file)) { 3644 for (int i = 0; i < file->dependency_count(); i++) { 3645 if (IsLite(file->dependency(i))) { 3646 AddError( 3647 file->name(), proto, 3648 DescriptorPool::ErrorCollector::OTHER, 3649 "Files that do not use optimize_for = LITE_RUNTIME cannot import " 3650 "files which do use this option. This file is not lite, but it " 3651 "imports \"" + file->dependency(i)->name() + "\" which is."); 3652 break; 3653 } 3654 } 3655 } 3656 } 3657 3658 void DescriptorBuilder::ValidateMessageOptions(Descriptor* message, 3659 const DescriptorProto& proto) { 3660 VALIDATE_OPTIONS_FROM_ARRAY(message, field, Field); 3661 VALIDATE_OPTIONS_FROM_ARRAY(message, nested_type, Message); 3662 VALIDATE_OPTIONS_FROM_ARRAY(message, enum_type, Enum); 3663 VALIDATE_OPTIONS_FROM_ARRAY(message, extension, Field); 3664 } 3665 3666 void DescriptorBuilder::ValidateFieldOptions(FieldDescriptor* field, 3667 const FieldDescriptorProto& proto) { 3668 if (field->options().has_experimental_map_key()) { 3669 ValidateMapKey(field, proto); 3670 } 3671 3672 // Only repeated primitive fields may be packed. 3673 if (field->options().packed() && !field->is_packable()) { 3674 AddError( 3675 field->full_name(), proto, 3676 DescriptorPool::ErrorCollector::TYPE, 3677 "[packed = true] can only be specified for repeated primitive fields."); 3678 } 3679 3680 // Note: Default instance may not yet be initialized here, so we have to 3681 // avoid reading from it. 3682 if (field->containing_type_ != NULL && 3683 &field->containing_type()->options() != 3684 &MessageOptions::default_instance() && 3685 field->containing_type()->options().message_set_wire_format()) { 3686 if (field->is_extension()) { 3687 if (!field->is_optional() || 3688 field->type() != FieldDescriptor::TYPE_MESSAGE) { 3689 AddError(field->full_name(), proto, 3690 DescriptorPool::ErrorCollector::TYPE, 3691 "Extensions of MessageSets must be optional messages."); 3692 } 3693 } else { 3694 AddError(field->full_name(), proto, 3695 DescriptorPool::ErrorCollector::NAME, 3696 "MessageSets cannot have fields, only extensions."); 3697 } 3698 } 3699 3700 // Lite extensions can only be of Lite types. 3701 if (IsLite(field->file()) && 3702 field->containing_type_ != NULL && 3703 !IsLite(field->containing_type()->file())) { 3704 AddError(field->full_name(), proto, 3705 DescriptorPool::ErrorCollector::EXTENDEE, 3706 "Extensions to non-lite types can only be declared in non-lite " 3707 "files. Note that you cannot extend a non-lite type to contain " 3708 "a lite type, but the reverse is allowed."); 3709 } 3710 } 3711 3712 void DescriptorBuilder::ValidateEnumOptions(EnumDescriptor* enm, 3713 const EnumDescriptorProto& proto) { 3714 VALIDATE_OPTIONS_FROM_ARRAY(enm, value, EnumValue); 3715 } 3716 3717 void DescriptorBuilder::ValidateEnumValueOptions( 3718 EnumValueDescriptor* enum_value, const EnumValueDescriptorProto& proto) { 3719 // Nothing to do so far. 3720 } 3721 void DescriptorBuilder::ValidateServiceOptions(ServiceDescriptor* service, 3722 const ServiceDescriptorProto& proto) { 3723 if (IsLite(service->file())) { 3724 AddError(service->full_name(), proto, 3725 DescriptorPool::ErrorCollector::NAME, 3726 "Files with optimize_for = LITE_RUNTIME cannot define services."); 3727 } 3728 3729 VALIDATE_OPTIONS_FROM_ARRAY(service, method, Method); 3730 } 3731 3732 void DescriptorBuilder::ValidateMethodOptions(MethodDescriptor* method, 3733 const MethodDescriptorProto& proto) { 3734 // Nothing to do so far. 3735 } 3736 3737 void DescriptorBuilder::ValidateMapKey(FieldDescriptor* field, 3738 const FieldDescriptorProto& proto) { 3739 if (!field->is_repeated()) { 3740 AddError(field->full_name(), proto, DescriptorPool::ErrorCollector::TYPE, 3741 "map type is only allowed for repeated fields."); 3742 return; 3743 } 3744 3745 if (field->cpp_type() != FieldDescriptor::CPPTYPE_MESSAGE) { 3746 AddError(field->full_name(), proto, DescriptorPool::ErrorCollector::TYPE, 3747 "map type is only allowed for fields with a message type."); 3748 return; 3749 } 3750 3751 const Descriptor* item_type = field->message_type(); 3752 if (item_type == NULL) { 3753 AddError(field->full_name(), proto, DescriptorPool::ErrorCollector::TYPE, 3754 "Could not find field type."); 3755 return; 3756 } 3757 3758 // Find the field in item_type named by "experimental_map_key" 3759 const string& key_name = field->options().experimental_map_key(); 3760 const Symbol key_symbol = LookupSymbol( 3761 key_name, 3762 // We append ".key_name" to the containing type's name since 3763 // LookupSymbol() searches for peers of the supplied name, not 3764 // children of the supplied name. 3765 item_type->full_name() + "." + key_name); 3766 3767 if (key_symbol.IsNull() || key_symbol.field_descriptor->is_extension()) { 3768 AddError(field->full_name(), proto, DescriptorPool::ErrorCollector::TYPE, 3769 "Could not find field named \"" + key_name + "\" in type \"" + 3770 item_type->full_name() + "\"."); 3771 return; 3772 } 3773 const FieldDescriptor* key_field = key_symbol.field_descriptor; 3774 3775 if (key_field->is_repeated()) { 3776 AddError(field->full_name(), proto, DescriptorPool::ErrorCollector::TYPE, 3777 "map_key must not name a repeated field."); 3778 return; 3779 } 3780 3781 if (key_field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { 3782 AddError(field->full_name(), proto, DescriptorPool::ErrorCollector::TYPE, 3783 "map key must name a scalar or string field."); 3784 return; 3785 } 3786 3787 field->experimental_map_key_ = key_field; 3788 } 3789 3790 #undef VALIDATE_OPTIONS_FROM_ARRAY 3791 3792 // ------------------------------------------------------------------- 3793 3794 DescriptorBuilder::OptionInterpreter::OptionInterpreter( 3795 DescriptorBuilder* builder) : builder_(builder) { 3796 GOOGLE_CHECK(builder_); 3797 } 3798 3799 DescriptorBuilder::OptionInterpreter::~OptionInterpreter() { 3800 } 3801 3802 bool DescriptorBuilder::OptionInterpreter::InterpretOptions( 3803 OptionsToInterpret* options_to_interpret) { 3804 // Note that these may be in different pools, so we can't use the same 3805 // descriptor and reflection objects on both. 3806 Message* options = options_to_interpret->options; 3807 const Message* original_options = options_to_interpret->original_options; 3808 3809 bool failed = false; 3810 options_to_interpret_ = options_to_interpret; 3811 3812 // Find the uninterpreted_option field in the mutable copy of the options 3813 // and clear them, since we're about to interpret them. 3814 const FieldDescriptor* uninterpreted_options_field = 3815 options->GetDescriptor()->FindFieldByName("uninterpreted_option"); 3816 GOOGLE_CHECK(uninterpreted_options_field != NULL) 3817 << "No field named \"uninterpreted_option\" in the Options proto."; 3818 options->GetReflection()->ClearField(options, uninterpreted_options_field); 3819 3820 // Find the uninterpreted_option field in the original options. 3821 const FieldDescriptor* original_uninterpreted_options_field = 3822 original_options->GetDescriptor()-> 3823 FindFieldByName("uninterpreted_option"); 3824 GOOGLE_CHECK(original_uninterpreted_options_field != NULL) 3825 << "No field named \"uninterpreted_option\" in the Options proto."; 3826 3827 const int num_uninterpreted_options = original_options->GetReflection()-> 3828 FieldSize(*original_options, original_uninterpreted_options_field); 3829 for (int i = 0; i < num_uninterpreted_options; ++i) { 3830 uninterpreted_option_ = down_cast<const UninterpretedOption*>( 3831 &original_options->GetReflection()->GetRepeatedMessage( 3832 *original_options, original_uninterpreted_options_field, i)); 3833 if (!InterpretSingleOption(options)) { 3834 // Error already added by InterpretSingleOption(). 3835 failed = true; 3836 break; 3837 } 3838 } 3839 // Reset these, so we don't have any dangling pointers. 3840 uninterpreted_option_ = NULL; 3841 options_to_interpret_ = NULL; 3842 3843 if (!failed) { 3844 // InterpretSingleOption() added the interpreted options in the 3845 // UnknownFieldSet, in case the option isn't yet known to us. Now we 3846 // serialize the options message and deserialize it back. That way, any 3847 // option fields that we do happen to know about will get moved from the 3848 // UnknownFieldSet into the real fields, and thus be available right away. 3849 // If they are not known, that's OK too. They will get reparsed into the 3850 // UnknownFieldSet and wait there until the message is parsed by something 3851 // that does know about the options. 3852 string buf; 3853 options->AppendToString(&buf); 3854 GOOGLE_CHECK(options->ParseFromString(buf)) 3855 << "Protocol message serialized itself in invalid fashion."; 3856 } 3857 3858 return !failed; 3859 } 3860 3861 bool DescriptorBuilder::OptionInterpreter::InterpretSingleOption( 3862 Message* options) { 3863 // First do some basic validation. 3864 if (uninterpreted_option_->name_size() == 0) { 3865 // This should never happen unless the parser has gone seriously awry or 3866 // someone has manually created the uninterpreted option badly. 3867 return AddNameError("Option must have a name."); 3868 } 3869 if (uninterpreted_option_->name(0).name_part() == "uninterpreted_option") { 3870 return AddNameError("Option must not use reserved name " 3871 "\"uninterpreted_option\"."); 3872 } 3873 3874 const Descriptor* options_descriptor = NULL; 3875 // Get the options message's descriptor from the builder's pool, so that we 3876 // get the version that knows about any extension options declared in the 3877 // file we're currently building. The descriptor should be there as long as 3878 // the file we're building imported "google/protobuf/descriptors.proto". 3879 3880 // Note that we use DescriptorBuilder::FindSymbol(), not 3881 // DescriptorPool::FindMessageTypeByName() because we're already holding the 3882 // pool's mutex, and the latter method locks it again. 3883 Symbol symbol = builder_->FindSymbolNotEnforcingDeps( 3884 options->GetDescriptor()->full_name()); 3885 if (!symbol.IsNull() && symbol.type == Symbol::MESSAGE) { 3886 options_descriptor = symbol.descriptor; 3887 } else { 3888 // The options message's descriptor was not in the builder's pool, so use 3889 // the standard version from the generated pool. We're not holding the 3890 // generated pool's mutex, so we can search it the straightforward way. 3891 options_descriptor = options->GetDescriptor(); 3892 } 3893 GOOGLE_CHECK(options_descriptor); 3894 3895 // We iterate over the name parts to drill into the submessages until we find 3896 // the leaf field for the option. As we drill down we remember the current 3897 // submessage's descriptor in |descriptor| and the next field in that 3898 // submessage in |field|. We also track the fields we're drilling down 3899 // through in |intermediate_fields|. As we go, we reconstruct the full option 3900 // name in |debug_msg_name|, for use in error messages. 3901 const Descriptor* descriptor = options_descriptor; 3902 const FieldDescriptor* field = NULL; 3903 vector<const FieldDescriptor*> intermediate_fields; 3904 string debug_msg_name = ""; 3905 3906 for (int i = 0; i < uninterpreted_option_->name_size(); ++i) { 3907 const string& name_part = uninterpreted_option_->name(i).name_part(); 3908 if (debug_msg_name.size() > 0) { 3909 debug_msg_name += "."; 3910 } 3911 if (uninterpreted_option_->name(i).is_extension()) { 3912 debug_msg_name += "(" + name_part + ")"; 3913 // Search for the extension's descriptor as an extension in the builder's 3914 // pool. Note that we use DescriptorBuilder::LookupSymbol(), not 3915 // DescriptorPool::FindExtensionByName(), for two reasons: 1) It allows 3916 // relative lookups, and 2) because we're already holding the pool's 3917 // mutex, and the latter method locks it again. 3918 Symbol symbol = builder_->LookupSymbol(name_part, 3919 options_to_interpret_->name_scope); 3920 if (!symbol.IsNull() && symbol.type == Symbol::FIELD) { 3921 field = symbol.field_descriptor; 3922 } 3923 // If we don't find the field then the field's descriptor was not in the 3924 // builder's pool, but there's no point in looking in the generated 3925 // pool. We require that you import the file that defines any extensions 3926 // you use, so they must be present in the builder's pool. 3927 } else { 3928 debug_msg_name += name_part; 3929 // Search for the field's descriptor as a regular field. 3930 field = descriptor->FindFieldByName(name_part); 3931 } 3932 3933 if (field == NULL) { 3934 if (get_allow_unknown(builder_->pool_)) { 3935 // We can't find the option, but AllowUnknownDependencies() is enabled, 3936 // so we will just leave it as uninterpreted. 3937 AddWithoutInterpreting(*uninterpreted_option_, options); 3938 return true; 3939 } else { 3940 return AddNameError("Option \"" + debug_msg_name + "\" unknown."); 3941 } 3942 } else if (field->containing_type() != descriptor) { 3943 if (get_is_placeholder(field->containing_type())) { 3944 // The field is an extension of a placeholder type, so we can't 3945 // reliably verify whether it is a valid extension to use here (e.g. 3946 // we don't know if it is an extension of the correct *Options message, 3947 // or if it has a valid field number, etc.). Just leave it as 3948 // uninterpreted instead. 3949 AddWithoutInterpreting(*uninterpreted_option_, options); 3950 return true; 3951 } else { 3952 // This can only happen if, due to some insane misconfiguration of the 3953 // pools, we find the options message in one pool but the field in 3954 // another. This would probably imply a hefty bug somewhere. 3955 return AddNameError("Option field \"" + debug_msg_name + 3956 "\" is not a field or extension of message \"" + 3957 descriptor->name() + "\"."); 3958 } 3959 } else if (field->is_repeated()) { 3960 return AddNameError("Option field \"" + debug_msg_name + 3961 "\" is repeated. Repeated options are not " 3962 "supported."); 3963 } else if (i < uninterpreted_option_->name_size() - 1) { 3964 if (field->cpp_type() != FieldDescriptor::CPPTYPE_MESSAGE) { 3965 return AddNameError("Option \"" + debug_msg_name + 3966 "\" is an atomic type, not a message."); 3967 } else { 3968 // Drill down into the submessage. 3969 intermediate_fields.push_back(field); 3970 descriptor = field->message_type(); 3971 } 3972 } else if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { 3973 return AddNameError("Option field \"" + debug_msg_name + 3974 "\" cannot be of message type."); 3975 } 3976 } 3977 3978 // We've found the leaf field. Now we use UnknownFieldSets to set its value 3979 // on the options message. We do so because the message may not yet know 3980 // about its extension fields, so we may not be able to set the fields 3981 // directly. But the UnknownFieldSets will serialize to the same wire-format 3982 // message, so reading that message back in once the extension fields are 3983 // known will populate them correctly. 3984 3985 // First see if the option is already set. 3986 if (!ExamineIfOptionIsSet( 3987 intermediate_fields.begin(), 3988 intermediate_fields.end(), 3989 field, debug_msg_name, 3990 options->GetReflection()->GetUnknownFields(*options))) { 3991 return false; // ExamineIfOptionIsSet() already added the error. 3992 } 3993 3994 3995 // First set the value on the UnknownFieldSet corresponding to the 3996 // innermost message. 3997 scoped_ptr<UnknownFieldSet> unknown_fields(new UnknownFieldSet()); 3998 if (!SetOptionValue(field, unknown_fields.get())) { 3999 return false; // SetOptionValue() already added the error. 4000 } 4001 4002 // Now wrap the UnknownFieldSet with UnknownFieldSets corresponding to all 4003 // the intermediate messages. 4004 for (vector<const FieldDescriptor*>::reverse_iterator iter = 4005 intermediate_fields.rbegin(); 4006 iter != intermediate_fields.rend(); ++iter) { 4007 scoped_ptr<UnknownFieldSet> parent_unknown_fields(new UnknownFieldSet()); 4008 switch ((*iter)->type()) { 4009 case FieldDescriptor::TYPE_MESSAGE: { 4010 io::StringOutputStream outstr( 4011 parent_unknown_fields->AddLengthDelimited((*iter)->number())); 4012 io::CodedOutputStream out(&outstr); 4013 internal::WireFormat::SerializeUnknownFields(*unknown_fields, &out); 4014 GOOGLE_CHECK(!out.HadError()) 4015 << "Unexpected failure while serializing option submessage " 4016 << debug_msg_name << "\"."; 4017 break; 4018 } 4019 4020 case FieldDescriptor::TYPE_GROUP: { 4021 parent_unknown_fields->AddGroup((*iter)->number()) 4022 ->MergeFrom(*unknown_fields); 4023 break; 4024 } 4025 4026 default: 4027 GOOGLE_LOG(FATAL) << "Invalid wire type for CPPTYPE_MESSAGE: " 4028 << (*iter)->type(); 4029 return false; 4030 } 4031 unknown_fields.reset(parent_unknown_fields.release()); 4032 } 4033 4034 // Now merge the UnknownFieldSet corresponding to the top-level message into 4035 // the options message. 4036 options->GetReflection()->MutableUnknownFields(options)->MergeFrom( 4037 *unknown_fields); 4038 4039 return true; 4040 } 4041 4042 void DescriptorBuilder::OptionInterpreter::AddWithoutInterpreting( 4043 const UninterpretedOption& uninterpreted_option, Message* options) { 4044 const FieldDescriptor* field = 4045 options->GetDescriptor()->FindFieldByName("uninterpreted_option"); 4046 GOOGLE_CHECK(field != NULL); 4047 4048 options->GetReflection()->AddMessage(options, field) 4049 ->CopyFrom(uninterpreted_option); 4050 } 4051 4052 bool DescriptorBuilder::OptionInterpreter::ExamineIfOptionIsSet( 4053 vector<const FieldDescriptor*>::const_iterator intermediate_fields_iter, 4054 vector<const FieldDescriptor*>::const_iterator intermediate_fields_end, 4055 const FieldDescriptor* innermost_field, const string& debug_msg_name, 4056 const UnknownFieldSet& unknown_fields) { 4057 // We do linear searches of the UnknownFieldSet and its sub-groups. This 4058 // should be fine since it's unlikely that any one options structure will 4059 // contain more than a handful of options. 4060 4061 if (intermediate_fields_iter == intermediate_fields_end) { 4062 // We're at the innermost submessage. 4063 for (int i = 0; i < unknown_fields.field_count(); i++) { 4064 if (unknown_fields.field(i).number() == innermost_field->number()) { 4065 return AddNameError("Option \"" + debug_msg_name + 4066 "\" was already set."); 4067 } 4068 } 4069 return true; 4070 } 4071 4072 for (int i = 0; i < unknown_fields.field_count(); i++) { 4073 if (unknown_fields.field(i).number() == 4074 (*intermediate_fields_iter)->number()) { 4075 const UnknownField* unknown_field = &unknown_fields.field(i); 4076 FieldDescriptor::Type type = (*intermediate_fields_iter)->type(); 4077 // Recurse into the next submessage. 4078 switch (type) { 4079 case FieldDescriptor::TYPE_MESSAGE: 4080 if (unknown_field->type() == UnknownField::TYPE_LENGTH_DELIMITED) { 4081 UnknownFieldSet intermediate_unknown_fields; 4082 if (intermediate_unknown_fields.ParseFromString( 4083 unknown_field->length_delimited()) && 4084 !ExamineIfOptionIsSet(intermediate_fields_iter + 1, 4085 intermediate_fields_end, 4086 innermost_field, debug_msg_name, 4087 intermediate_unknown_fields)) { 4088 return false; // Error already added. 4089 } 4090 } 4091 break; 4092 4093 case FieldDescriptor::TYPE_GROUP: 4094 if (unknown_field->type() == UnknownField::TYPE_GROUP) { 4095 if (!ExamineIfOptionIsSet(intermediate_fields_iter + 1, 4096 intermediate_fields_end, 4097 innermost_field, debug_msg_name, 4098 unknown_field->group())) { 4099 return false; // Error already added. 4100 } 4101 } 4102 break; 4103 4104 default: 4105 GOOGLE_LOG(FATAL) << "Invalid wire type for CPPTYPE_MESSAGE: " << type; 4106 return false; 4107 } 4108 } 4109 } 4110 return true; 4111 } 4112 4113 bool DescriptorBuilder::OptionInterpreter::SetOptionValue( 4114 const FieldDescriptor* option_field, 4115 UnknownFieldSet* unknown_fields) { 4116 // We switch on the CppType to validate. 4117 switch (option_field->cpp_type()) { 4118 4119 case FieldDescriptor::CPPTYPE_INT32: 4120 if (uninterpreted_option_->has_positive_int_value()) { 4121 if (uninterpreted_option_->positive_int_value() > 4122 static_cast<uint64>(kint32max)) { 4123 return AddValueError("Value out of range for int32 option \"" + 4124 option_field->full_name() + "\"."); 4125 } else { 4126 SetInt32(option_field->number(), 4127 uninterpreted_option_->positive_int_value(), 4128 option_field->type(), unknown_fields); 4129 } 4130 } else if (uninterpreted_option_->has_negative_int_value()) { 4131 if (uninterpreted_option_->negative_int_value() < 4132 static_cast<int64>(kint32min)) { 4133 return AddValueError("Value out of range for int32 option \"" + 4134 option_field->full_name() + "\"."); 4135 } else { 4136 SetInt32(option_field->number(), 4137 uninterpreted_option_->negative_int_value(), 4138 option_field->type(), unknown_fields); 4139 } 4140 } else { 4141 return AddValueError("Value must be integer for int32 option \"" + 4142 option_field->full_name() + "\"."); 4143 } 4144 break; 4145 4146 case FieldDescriptor::CPPTYPE_INT64: 4147 if (uninterpreted_option_->has_positive_int_value()) { 4148 if (uninterpreted_option_->positive_int_value() > 4149 static_cast<uint64>(kint64max)) { 4150 return AddValueError("Value out of range for int64 option \"" + 4151 option_field->full_name() + "\"."); 4152 } else { 4153 SetInt64(option_field->number(), 4154 uninterpreted_option_->positive_int_value(), 4155 option_field->type(), unknown_fields); 4156 } 4157 } else if (uninterpreted_option_->has_negative_int_value()) { 4158 SetInt64(option_field->number(), 4159 uninterpreted_option_->negative_int_value(), 4160 option_field->type(), unknown_fields); 4161 } else { 4162 return AddValueError("Value must be integer for int64 option \"" + 4163 option_field->full_name() + "\"."); 4164 } 4165 break; 4166 4167 case FieldDescriptor::CPPTYPE_UINT32: 4168 if (uninterpreted_option_->has_positive_int_value()) { 4169 if (uninterpreted_option_->positive_int_value() > kuint32max) { 4170 return AddValueError("Value out of range for uint32 option \"" + 4171 option_field->name() + "\"."); 4172 } else { 4173 SetUInt32(option_field->number(), 4174 uninterpreted_option_->positive_int_value(), 4175 option_field->type(), unknown_fields); 4176 } 4177 } else { 4178 return AddValueError("Value must be non-negative integer for uint32 " 4179 "option \"" + option_field->full_name() + "\"."); 4180 } 4181 break; 4182 4183 case FieldDescriptor::CPPTYPE_UINT64: 4184 if (uninterpreted_option_->has_positive_int_value()) { 4185 SetUInt64(option_field->number(), 4186 uninterpreted_option_->positive_int_value(), 4187 option_field->type(), unknown_fields); 4188 } else { 4189 return AddValueError("Value must be non-negative integer for uint64 " 4190 "option \"" + option_field->full_name() + "\"."); 4191 } 4192 break; 4193 4194 case FieldDescriptor::CPPTYPE_FLOAT: { 4195 float value; 4196 if (uninterpreted_option_->has_double_value()) { 4197 value = uninterpreted_option_->double_value(); 4198 } else if (uninterpreted_option_->has_positive_int_value()) { 4199 value = uninterpreted_option_->positive_int_value(); 4200 } else if (uninterpreted_option_->has_negative_int_value()) { 4201 value = uninterpreted_option_->negative_int_value(); 4202 } else { 4203 return AddValueError("Value must be number for float option \"" + 4204 option_field->full_name() + "\"."); 4205 } 4206 unknown_fields->AddFixed32(option_field->number(), 4207 google::protobuf::internal::WireFormatLite::EncodeFloat(value)); 4208 break; 4209 } 4210 4211 case FieldDescriptor::CPPTYPE_DOUBLE: { 4212 double value; 4213 if (uninterpreted_option_->has_double_value()) { 4214 value = uninterpreted_option_->double_value(); 4215 } else if (uninterpreted_option_->has_positive_int_value()) { 4216 value = uninterpreted_option_->positive_int_value(); 4217 } else if (uninterpreted_option_->has_negative_int_value()) { 4218 value = uninterpreted_option_->negative_int_value(); 4219 } else { 4220 return AddValueError("Value must be number for double option \"" + 4221 option_field->full_name() + "\"."); 4222 } 4223 unknown_fields->AddFixed64(option_field->number(), 4224 google::protobuf::internal::WireFormatLite::EncodeDouble(value)); 4225 break; 4226 } 4227 4228 case FieldDescriptor::CPPTYPE_BOOL: 4229 uint64 value; 4230 if (!uninterpreted_option_->has_identifier_value()) { 4231 return AddValueError("Value must be identifier for boolean option " 4232 "\"" + option_field->full_name() + "\"."); 4233 } 4234 if (uninterpreted_option_->identifier_value() == "true") { 4235 value = 1; 4236 } else if (uninterpreted_option_->identifier_value() == "false") { 4237 value = 0; 4238 } else { 4239 return AddValueError("Value must be \"true\" or \"false\" for boolean " 4240 "option \"" + option_field->full_name() + "\"."); 4241 } 4242 unknown_fields->AddVarint(option_field->number(), value); 4243 break; 4244 4245 case FieldDescriptor::CPPTYPE_ENUM: { 4246 if (!uninterpreted_option_->has_identifier_value()) { 4247 return AddValueError("Value must be identifier for enum-valued option " 4248 "\"" + option_field->full_name() + "\"."); 4249 } 4250 const EnumDescriptor* enum_type = option_field->enum_type(); 4251 const string& value_name = uninterpreted_option_->identifier_value(); 4252 const EnumValueDescriptor* enum_value = NULL; 4253 4254 if (enum_type->file()->pool() != DescriptorPool::generated_pool()) { 4255 // Note that the enum value's fully-qualified name is a sibling of the 4256 // enum's name, not a child of it. 4257 string fully_qualified_name = enum_type->full_name(); 4258 fully_qualified_name.resize(fully_qualified_name.size() - 4259 enum_type->name().size()); 4260 fully_qualified_name += value_name; 4261 4262 // Search for the enum value's descriptor in the builder's pool. Note 4263 // that we use DescriptorBuilder::FindSymbolNotEnforcingDeps(), not 4264 // DescriptorPool::FindEnumValueByName() because we're already holding 4265 // the pool's mutex, and the latter method locks it again. 4266 Symbol symbol = 4267 builder_->FindSymbolNotEnforcingDeps(fully_qualified_name); 4268 if (!symbol.IsNull() && symbol.type == Symbol::ENUM_VALUE) { 4269 if (symbol.enum_value_descriptor->type() != enum_type) { 4270 return AddValueError("Enum type \"" + enum_type->full_name() + 4271 "\" has no value named \"" + value_name + "\" for option \"" + 4272 option_field->full_name() + 4273 "\". This appears to be a value from a sibling type."); 4274 } else { 4275 enum_value = symbol.enum_value_descriptor; 4276 } 4277 } 4278 } else { 4279 // The enum type is in the generated pool, so we can search for the 4280 // value there. 4281 enum_value = enum_type->FindValueByName(value_name); 4282 } 4283 4284 if (enum_value == NULL) { 4285 return AddValueError("Enum type \"" + 4286 option_field->enum_type()->full_name() + 4287 "\" has no value named \"" + value_name + "\" for " 4288 "option \"" + option_field->full_name() + "\"."); 4289 } else { 4290 // Sign-extension is not a problem, since we cast directly from int32 to 4291 // uint64, without first going through uint32. 4292 unknown_fields->AddVarint(option_field->number(), 4293 static_cast<uint64>(static_cast<int64>(enum_value->number()))); 4294 } 4295 break; 4296 } 4297 4298 case FieldDescriptor::CPPTYPE_STRING: 4299 if (!uninterpreted_option_->has_string_value()) { 4300 return AddValueError("Value must be quoted string for string option " 4301 "\"" + option_field->full_name() + "\"."); 4302 } 4303 // The string has already been unquoted and unescaped by the parser. 4304 unknown_fields->AddLengthDelimited(option_field->number(), 4305 uninterpreted_option_->string_value()); 4306 break; 4307 4308 case FieldDescriptor::CPPTYPE_MESSAGE: 4309 // We don't currently support defining a message-typed option, so we 4310 // should never actually get here. 4311 return AddValueError("Option \"" + option_field->full_name() + 4312 "\" is a message. To set fields within it, use " 4313 "syntax like \"" + option_field->name() + 4314 ".foo = value\"."); 4315 break; 4316 } 4317 4318 return true; 4319 } 4320 4321 void DescriptorBuilder::OptionInterpreter::SetInt32(int number, int32 value, 4322 FieldDescriptor::Type type, UnknownFieldSet* unknown_fields) { 4323 switch (type) { 4324 case FieldDescriptor::TYPE_INT32: 4325 unknown_fields->AddVarint(number, 4326 static_cast<uint64>(static_cast<int64>(value))); 4327 break; 4328 4329 case FieldDescriptor::TYPE_SFIXED32: 4330 unknown_fields->AddFixed32(number, static_cast<uint32>(value)); 4331 break; 4332 4333 case FieldDescriptor::TYPE_SINT32: 4334 unknown_fields->AddVarint(number, 4335 google::protobuf::internal::WireFormatLite::ZigZagEncode32(value)); 4336 break; 4337 4338 default: 4339 GOOGLE_LOG(FATAL) << "Invalid wire type for CPPTYPE_INT32: " << type; 4340 break; 4341 } 4342 } 4343 4344 void DescriptorBuilder::OptionInterpreter::SetInt64(int number, int64 value, 4345 FieldDescriptor::Type type, UnknownFieldSet* unknown_fields) { 4346 switch (type) { 4347 case FieldDescriptor::TYPE_INT64: 4348 unknown_fields->AddVarint(number, static_cast<uint64>(value)); 4349 break; 4350 4351 case FieldDescriptor::TYPE_SFIXED64: 4352 unknown_fields->AddFixed64(number, static_cast<uint64>(value)); 4353 break; 4354 4355 case FieldDescriptor::TYPE_SINT64: 4356 unknown_fields->AddVarint(number, 4357 google::protobuf::internal::WireFormatLite::ZigZagEncode64(value)); 4358 break; 4359 4360 default: 4361 GOOGLE_LOG(FATAL) << "Invalid wire type for CPPTYPE_INT64: " << type; 4362 break; 4363 } 4364 } 4365 4366 void DescriptorBuilder::OptionInterpreter::SetUInt32(int number, uint32 value, 4367 FieldDescriptor::Type type, UnknownFieldSet* unknown_fields) { 4368 switch (type) { 4369 case FieldDescriptor::TYPE_UINT32: 4370 unknown_fields->AddVarint(number, static_cast<uint64>(value)); 4371 break; 4372 4373 case FieldDescriptor::TYPE_FIXED32: 4374 unknown_fields->AddFixed32(number, static_cast<uint32>(value)); 4375 break; 4376 4377 default: 4378 GOOGLE_LOG(FATAL) << "Invalid wire type for CPPTYPE_UINT32: " << type; 4379 break; 4380 } 4381 } 4382 4383 void DescriptorBuilder::OptionInterpreter::SetUInt64(int number, uint64 value, 4384 FieldDescriptor::Type type, UnknownFieldSet* unknown_fields) { 4385 switch (type) { 4386 case FieldDescriptor::TYPE_UINT64: 4387 unknown_fields->AddVarint(number, value); 4388 break; 4389 4390 case FieldDescriptor::TYPE_FIXED64: 4391 unknown_fields->AddFixed64(number, value); 4392 break; 4393 4394 default: 4395 GOOGLE_LOG(FATAL) << "Invalid wire type for CPPTYPE_UINT64: " << type; 4396 break; 4397 } 4398 } 4399 4400 } // namespace protobuf 4401 } // namespace google 4402