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