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