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