1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
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
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.
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.
31 // Author: kenton@google.com (Kenton Varda)
32 // Based on original Protocol Buffers design by
33 // Sanjay Ghemawat, Jeff Dean, and others.
35 #include <google/protobuf/stubs/hash.h>
43 #include <google/protobuf/descriptor.h>
44 #include <google/protobuf/descriptor_database.h>
45 #include <google/protobuf/descriptor.pb.h>
46 #include <google/protobuf/dynamic_message.h>
47 #include <google/protobuf/generated_message_util.h>
48 #include <google/protobuf/text_format.h>
49 #include <google/protobuf/unknown_field_set.h>
50 #include <google/protobuf/wire_format.h>
51 #include <google/protobuf/io/strtod.h>
52 #include <google/protobuf/io/coded_stream.h>
53 #include <google/protobuf/io/tokenizer.h>
54 #include <google/protobuf/io/zero_copy_stream_impl.h>
55 #include <google/protobuf/stubs/common.h>
56 #include <google/protobuf/stubs/once.h>
57 #include <google/protobuf/stubs/strutil.h>
58 #include <google/protobuf/stubs/substitute.h>
59 #include <google/protobuf/stubs/map_util.h>
60 #include <google/protobuf/stubs/stl_util.h>
62 #undef PACKAGE // autoheader #defines this. :(
67 const FieldDescriptor::CppType
68 FieldDescriptor::kTypeToCppTypeMap[MAX_TYPE + 1] = {
69 static_cast<CppType>(0), // 0 is reserved for errors
71 CPPTYPE_DOUBLE, // TYPE_DOUBLE
72 CPPTYPE_FLOAT, // TYPE_FLOAT
73 CPPTYPE_INT64, // TYPE_INT64
74 CPPTYPE_UINT64, // TYPE_UINT64
75 CPPTYPE_INT32, // TYPE_INT32
76 CPPTYPE_UINT64, // TYPE_FIXED64
77 CPPTYPE_UINT32, // TYPE_FIXED32
78 CPPTYPE_BOOL, // TYPE_BOOL
79 CPPTYPE_STRING, // TYPE_STRING
80 CPPTYPE_MESSAGE, // TYPE_GROUP
81 CPPTYPE_MESSAGE, // TYPE_MESSAGE
82 CPPTYPE_STRING, // TYPE_BYTES
83 CPPTYPE_UINT32, // TYPE_UINT32
84 CPPTYPE_ENUM, // TYPE_ENUM
85 CPPTYPE_INT32, // TYPE_SFIXED32
86 CPPTYPE_INT64, // TYPE_SFIXED64
87 CPPTYPE_INT32, // TYPE_SINT32
88 CPPTYPE_INT64, // TYPE_SINT64
91 const char * const FieldDescriptor::kTypeToName[MAX_TYPE + 1] = {
92 "ERROR", // 0 is reserved for errors
94 "double", // TYPE_DOUBLE
95 "float", // TYPE_FLOAT
96 "int64", // TYPE_INT64
97 "uint64", // TYPE_UINT64
98 "int32", // TYPE_INT32
99 "fixed64", // TYPE_FIXED64
100 "fixed32", // TYPE_FIXED32
102 "string", // TYPE_STRING
103 "group", // TYPE_GROUP
104 "message", // TYPE_MESSAGE
105 "bytes", // TYPE_BYTES
106 "uint32", // TYPE_UINT32
108 "sfixed32", // TYPE_SFIXED32
109 "sfixed64", // TYPE_SFIXED64
110 "sint32", // TYPE_SINT32
111 "sint64", // TYPE_SINT64
114 const char * const FieldDescriptor::kCppTypeToName[MAX_CPPTYPE + 1] = {
115 "ERROR", // 0 is reserved for errors
117 "int32", // CPPTYPE_INT32
118 "int64", // CPPTYPE_INT64
119 "uint32", // CPPTYPE_UINT32
120 "uint64", // CPPTYPE_UINT64
121 "double", // CPPTYPE_DOUBLE
122 "float", // CPPTYPE_FLOAT
123 "bool", // CPPTYPE_BOOL
124 "enum", // CPPTYPE_ENUM
125 "string", // CPPTYPE_STRING
126 "message", // CPPTYPE_MESSAGE
129 const char * const FieldDescriptor::kLabelToName[MAX_LABEL + 1] = {
130 "ERROR", // 0 is reserved for errors
132 "optional", // LABEL_OPTIONAL
133 "required", // LABEL_REQUIRED
134 "repeated", // LABEL_REPEATED
137 static const char * const kNonLinkedWeakMessageReplacementName = "google.protobuf.Empty";
139 #ifndef _MSC_VER // MSVC doesn't need these and won't even accept them.
140 const int FieldDescriptor::kMaxNumber;
141 const int FieldDescriptor::kFirstReservedNumber;
142 const int FieldDescriptor::kLastReservedNumber;
147 string ToCamelCase(const string& input) {
148 bool capitalize_next = false;
150 result.reserve(input.size());
152 for (int i = 0; i < input.size(); i++) {
153 if (input[i] == '_') {
154 capitalize_next = true;
155 } else if (capitalize_next) {
156 // Note: I distrust ctype.h due to locales.
157 if ('a' <= input[i] && input[i] <= 'z') {
158 result.push_back(input[i] - 'a' + 'A');
160 result.push_back(input[i]);
162 capitalize_next = false;
164 result.push_back(input[i]);
168 // Lower-case the first letter.
169 if (!result.empty() && 'A' <= result[0] && result[0] <= 'Z') {
170 result[0] = result[0] - 'A' + 'a';
176 // A DescriptorPool contains a bunch of hash_maps to implement the
177 // various Find*By*() methods. Since hashtable lookups are O(1), it's
178 // most efficient to construct a fixed set of large hash_maps used by
179 // all objects in the pool rather than construct one or more small
180 // hash_maps for each object.
182 // The keys to these hash_maps are (parent, name) or (parent, number)
183 // pairs. Unfortunately STL doesn't provide hash functions for pair<>,
184 // so we must invent our own.
186 // TODO(kenton): Use StringPiece rather than const char* in keys? It would
187 // be a lot cleaner but we'd just have to convert it back to const char*
188 // for the open source release.
190 typedef pair<const void*, const char*> PointerStringPair;
192 struct PointerStringPairEqual {
193 inline bool operator()(const PointerStringPair& a,
194 const PointerStringPair& b) const {
195 return a.first == b.first && strcmp(a.second, b.second) == 0;
199 template<typename PairType>
200 struct PointerIntegerPairHash {
201 size_t operator()(const PairType& p) const {
202 // FIXME(kenton): What is the best way to compute this hash? I have
203 // no idea! This seems a bit better than an XOR.
204 return reinterpret_cast<intptr_t>(p.first) * ((1 << 16) - 1) + p.second;
208 // Used only by MSVC and platforms where hash_map is not available.
209 static const size_t bucket_size = 4;
210 static const size_t min_buckets = 8;
212 inline bool operator()(const PairType& a, const PairType& b) const {
213 return a.first < b.first ||
214 (a.first == b.first && a.second < b.second);
218 typedef pair<const Descriptor*, int> DescriptorIntPair;
219 typedef pair<const EnumDescriptor*, int> EnumIntPair;
221 struct PointerStringPairHash {
222 size_t operator()(const PointerStringPair& p) const {
223 // FIXME(kenton): What is the best way to compute this hash? I have
224 // no idea! This seems a bit better than an XOR.
225 hash<const char*> cstring_hash;
226 return reinterpret_cast<intptr_t>(p.first) * ((1 << 16) - 1) +
227 cstring_hash(p.second);
231 // Used only by MSVC and platforms where hash_map is not available.
232 static const size_t bucket_size = 4;
233 static const size_t min_buckets = 8;
235 inline bool operator()(const PointerStringPair& a,
236 const PointerStringPair& b) const {
237 if (a.first < b.first) return true;
238 if (a.first > b.first) return false;
239 return strcmp(a.second, b.second) < 0;
246 NULL_SYMBOL, MESSAGE, FIELD, ONEOF, ENUM, ENUM_VALUE, SERVICE, METHOD,
251 const Descriptor* descriptor;
252 const FieldDescriptor* field_descriptor;
253 const OneofDescriptor* oneof_descriptor;
254 const EnumDescriptor* enum_descriptor;
255 const EnumValueDescriptor* enum_value_descriptor;
256 const ServiceDescriptor* service_descriptor;
257 const MethodDescriptor* method_descriptor;
258 const FileDescriptor* package_file_descriptor;
261 inline Symbol() : type(NULL_SYMBOL) { descriptor = NULL; }
262 inline bool IsNull() const { return type == NULL_SYMBOL; }
263 inline bool IsType() const {
264 return type == MESSAGE || type == ENUM;
266 inline bool IsAggregate() const {
267 return type == MESSAGE || type == PACKAGE
268 || type == ENUM || type == SERVICE;
271 #define CONSTRUCTOR(TYPE, TYPE_CONSTANT, FIELD) \
272 inline explicit Symbol(const TYPE* value) { \
273 type = TYPE_CONSTANT; \
274 this->FIELD = value; \
277 CONSTRUCTOR(Descriptor , MESSAGE , descriptor )
278 CONSTRUCTOR(FieldDescriptor , FIELD , field_descriptor )
279 CONSTRUCTOR(OneofDescriptor , ONEOF , oneof_descriptor )
280 CONSTRUCTOR(EnumDescriptor , ENUM , enum_descriptor )
281 CONSTRUCTOR(EnumValueDescriptor, ENUM_VALUE, enum_value_descriptor )
282 CONSTRUCTOR(ServiceDescriptor , SERVICE , service_descriptor )
283 CONSTRUCTOR(MethodDescriptor , METHOD , method_descriptor )
284 CONSTRUCTOR(FileDescriptor , PACKAGE , package_file_descriptor)
287 const FileDescriptor* GetFile() const {
289 case NULL_SYMBOL: return NULL;
290 case MESSAGE : return descriptor ->file();
291 case FIELD : return field_descriptor ->file();
292 case ONEOF : return oneof_descriptor ->containing_type()->file();
293 case ENUM : return enum_descriptor ->file();
294 case ENUM_VALUE : return enum_value_descriptor->type()->file();
295 case SERVICE : return service_descriptor ->file();
296 case METHOD : return method_descriptor ->service()->file();
297 case PACKAGE : return package_file_descriptor;
303 const Symbol kNullSymbol;
305 typedef hash_map<const char*, Symbol,
306 hash<const char*>, streq>
308 typedef hash_map<PointerStringPair, Symbol,
309 PointerStringPairHash, PointerStringPairEqual>
311 typedef hash_map<const char*, const FileDescriptor*,
312 hash<const char*>, streq>
314 typedef hash_map<PointerStringPair, const FieldDescriptor*,
315 PointerStringPairHash, PointerStringPairEqual>
317 typedef hash_map<DescriptorIntPair, const FieldDescriptor*,
318 PointerIntegerPairHash<DescriptorIntPair> >
320 typedef hash_map<EnumIntPair, const EnumValueDescriptor*,
321 PointerIntegerPairHash<EnumIntPair> >
322 EnumValuesByNumberMap;
323 // This is a map rather than a hash_map, since we use it to iterate
324 // through all the extensions that extend a given Descriptor, and an
325 // ordered data structure that implements lower_bound is convenient
327 typedef map<DescriptorIntPair, const FieldDescriptor*>
328 ExtensionsGroupedByDescriptorMap;
329 typedef hash_map<string, const SourceCodeInfo_Location*> LocationsByPathMap;
330 } // anonymous namespace
332 // ===================================================================
333 // DescriptorPool::Tables
335 class DescriptorPool::Tables {
340 // Record the current state of the tables to the stack of checkpoints.
341 // Each call to AddCheckpoint() must be paired with exactly one call to either
342 // ClearLastCheckpoint() or RollbackToLastCheckpoint().
344 // This is used when building files, since some kinds of validation errors
345 // cannot be detected until the file's descriptors have already been added to
348 // This supports recursive checkpoints, since building a file may trigger
349 // recursive building of other files. Note that recursive checkpoints are not
350 // normally necessary; explicit dependencies are built prior to checkpointing.
351 // So although we recursively build transitive imports, there is at most one
352 // checkpoint in the stack during dependency building.
354 // Recursive checkpoints only arise during cross-linking of the descriptors.
355 // Symbol references must be resolved, via DescriptorBuilder::FindSymbol and
356 // friends. If the pending file references an unknown symbol
357 // (e.g., it is not defined in the pending file's explicit dependencies), and
358 // the pool is using a fallback database, and that database contains a file
359 // defining that symbol, and that file has not yet been built by the pool,
360 // the pool builds the file during cross-linking, leading to another
362 void AddCheckpoint();
364 // Mark the last checkpoint as having cleared successfully, removing it from
365 // the stack. If the stack is empty, all pending symbols will be committed.
367 // Note that this does not guarantee that the symbols added since the last
368 // checkpoint won't be rolled back: if a checkpoint gets rolled back,
369 // everything past that point gets rolled back, including symbols added after
370 // checkpoints that were pushed onto the stack after it and marked as cleared.
371 void ClearLastCheckpoint();
373 // Roll back the Tables to the state of the checkpoint at the top of the
374 // stack, removing everything that was added after that point.
375 void RollbackToLastCheckpoint();
377 // The stack of files which are currently being built. Used to detect
378 // cyclic dependencies when loading files from a DescriptorDatabase. Not
379 // used when fallback_database_ == NULL.
380 vector<string> pending_files_;
382 // A set of files which we have tried to load from the fallback database
383 // and encountered errors. We will not attempt to load them again during
384 // execution of the current public API call, but for compatibility with
385 // legacy clients, this is cleared at the beginning of each public API call.
386 // Not used when fallback_database_ == NULL.
387 hash_set<string> known_bad_files_;
389 // A set of symbols which we have tried to load from the fallback database
390 // and encountered errors. We will not attempt to load them again during
391 // execution of the current public API call, but for compatibility with
392 // legacy clients, this is cleared at the beginning of each public API call.
393 hash_set<string> known_bad_symbols_;
395 // The set of descriptors for which we've already loaded the full
396 // set of extensions numbers from fallback_database_.
397 hash_set<const Descriptor*> extensions_loaded_from_db_;
399 // -----------------------------------------------------------------
402 // Find symbols. This returns a null Symbol (symbol.IsNull() is true)
404 inline Symbol FindSymbol(const string& key) const;
406 // This implements the body of DescriptorPool::Find*ByName(). It should
407 // really be a private method of DescriptorPool, but that would require
408 // declaring Symbol in descriptor.h, which would drag all kinds of other
409 // stuff into the header. Yay C++.
410 Symbol FindByNameHelper(
411 const DescriptorPool* pool, const string& name);
413 // These return NULL if not found.
414 inline const FileDescriptor* FindFile(const string& key) const;
415 inline const FieldDescriptor* FindExtension(const Descriptor* extendee,
417 inline void FindAllExtensions(const Descriptor* extendee,
418 vector<const FieldDescriptor*>* out) const;
420 // -----------------------------------------------------------------
423 // These add items to the corresponding tables. They return false if
424 // the key already exists in the table. For AddSymbol(), the string passed
425 // in must be one that was constructed using AllocateString(), as it will
426 // be used as a key in the symbols_by_name_ map without copying.
427 bool AddSymbol(const string& full_name, Symbol symbol);
428 bool AddFile(const FileDescriptor* file);
429 bool AddExtension(const FieldDescriptor* field);
431 // -----------------------------------------------------------------
432 // Allocating memory.
434 // Allocate an object which will be reclaimed when the pool is
435 // destroyed. Note that the object's destructor will never be called,
436 // so its fields must be plain old data (primitive data types and
437 // pointers). All of the descriptor types are such objects.
438 template<typename Type> Type* Allocate();
440 // Allocate an array of objects which will be reclaimed when the
441 // pool in destroyed. Again, destructors are never called.
442 template<typename Type> Type* AllocateArray(int count);
444 // Allocate a string which will be destroyed when the pool is destroyed.
445 // The string is initialized to the given value for convenience.
446 string* AllocateString(const string& value);
448 // Allocate a protocol message object. Some older versions of GCC have
449 // trouble understanding explicit template instantiations in some cases, so
450 // in those cases we have to pass a dummy pointer of the right type as the
451 // parameter instead of specifying the type explicitly.
452 template<typename Type> Type* AllocateMessage(Type* dummy = NULL);
454 // Allocate a FileDescriptorTables object.
455 FileDescriptorTables* AllocateFileTables();
458 vector<string*> strings_; // All strings in the pool.
459 vector<Message*> messages_; // All messages in the pool.
460 vector<FileDescriptorTables*> file_tables_; // All file tables in the pool.
461 vector<void*> allocations_; // All other memory allocated in the pool.
463 SymbolsByNameMap symbols_by_name_;
464 FilesByNameMap files_by_name_;
465 ExtensionsGroupedByDescriptorMap extensions_;
468 explicit CheckPoint(const Tables* tables)
469 : strings_before_checkpoint(tables->strings_.size()),
470 messages_before_checkpoint(tables->messages_.size()),
471 file_tables_before_checkpoint(tables->file_tables_.size()),
472 allocations_before_checkpoint(tables->allocations_.size()),
473 pending_symbols_before_checkpoint(
474 tables->symbols_after_checkpoint_.size()),
475 pending_files_before_checkpoint(
476 tables->files_after_checkpoint_.size()),
477 pending_extensions_before_checkpoint(
478 tables->extensions_after_checkpoint_.size()) {
480 int strings_before_checkpoint;
481 int messages_before_checkpoint;
482 int file_tables_before_checkpoint;
483 int allocations_before_checkpoint;
484 int pending_symbols_before_checkpoint;
485 int pending_files_before_checkpoint;
486 int pending_extensions_before_checkpoint;
488 vector<CheckPoint> checkpoints_;
489 vector<const char* > symbols_after_checkpoint_;
490 vector<const char* > files_after_checkpoint_;
491 vector<DescriptorIntPair> extensions_after_checkpoint_;
493 // Allocate some bytes which will be reclaimed when the pool is
495 void* AllocateBytes(int size);
498 // Contains tables specific to a particular file. These tables are not
499 // modified once the file has been constructed, so they need not be
500 // protected by a mutex. This makes operations that depend only on the
501 // contents of a single file -- e.g. Descriptor::FindFieldByName() --
504 // For historical reasons, the definitions of the methods of
505 // FileDescriptorTables and DescriptorPool::Tables are interleaved below.
506 // These used to be a single class.
507 class FileDescriptorTables {
509 FileDescriptorTables();
510 ~FileDescriptorTables();
512 // Empty table, used with placeholder files.
513 static const FileDescriptorTables kEmpty;
515 // -----------------------------------------------------------------
518 // Find symbols. These return a null Symbol (symbol.IsNull() is true)
520 inline Symbol FindNestedSymbol(const void* parent,
521 const string& name) const;
522 inline Symbol FindNestedSymbolOfType(const void* parent,
524 const Symbol::Type type) const;
526 // These return NULL if not found.
527 inline const FieldDescriptor* FindFieldByNumber(
528 const Descriptor* parent, int number) const;
529 inline const FieldDescriptor* FindFieldByLowercaseName(
530 const void* parent, const string& lowercase_name) const;
531 inline const FieldDescriptor* FindFieldByCamelcaseName(
532 const void* parent, const string& camelcase_name) const;
533 inline const EnumValueDescriptor* FindEnumValueByNumber(
534 const EnumDescriptor* parent, int number) const;
536 // -----------------------------------------------------------------
539 // These add items to the corresponding tables. They return false if
540 // the key already exists in the table. For AddAliasUnderParent(), the
541 // string passed in must be one that was constructed using AllocateString(),
542 // as it will be used as a key in the symbols_by_parent_ map without copying.
543 bool AddAliasUnderParent(const void* parent, const string& name,
545 bool AddFieldByNumber(const FieldDescriptor* field);
546 bool AddEnumValueByNumber(const EnumValueDescriptor* value);
548 // Adds the field to the lowercase_name and camelcase_name maps. Never
549 // fails because we allow duplicates; the first field by the name wins.
550 void AddFieldByStylizedNames(const FieldDescriptor* field);
552 // Populates p->first->locations_by_path_ from p->second.
553 // Unusual signature dictated by GoogleOnceDynamic.
554 static void BuildLocationsByPath(
555 pair<const FileDescriptorTables*, const SourceCodeInfo*>* p);
557 // Returns the location denoted by the specified path through info,
558 // or NULL if not found.
559 // The value of info must be that of the corresponding FileDescriptor.
560 // (Conceptually a pure function, but stateful as an optimisation.)
561 const SourceCodeInfo_Location* GetSourceLocation(
562 const vector<int>& path, const SourceCodeInfo* info) const;
565 SymbolsByParentMap symbols_by_parent_;
566 FieldsByNameMap fields_by_lowercase_name_;
567 FieldsByNameMap fields_by_camelcase_name_;
568 FieldsByNumberMap fields_by_number_; // Not including extensions.
569 EnumValuesByNumberMap enum_values_by_number_;
571 // Populated on first request to save space, hence constness games.
572 mutable GoogleOnceDynamic locations_by_path_once_;
573 mutable LocationsByPathMap locations_by_path_;
576 DescriptorPool::Tables::Tables()
577 // Start some hash_map and hash_set objects with a small # of buckets
578 : known_bad_files_(3),
579 known_bad_symbols_(3),
580 extensions_loaded_from_db_(3),
585 DescriptorPool::Tables::~Tables() {
586 GOOGLE_DCHECK(checkpoints_.empty());
587 // Note that the deletion order is important, since the destructors of some
588 // messages may refer to objects in allocations_.
589 STLDeleteElements(&messages_);
590 for (int i = 0; i < allocations_.size(); i++) {
591 operator delete(allocations_[i]);
593 STLDeleteElements(&strings_);
594 STLDeleteElements(&file_tables_);
597 FileDescriptorTables::FileDescriptorTables()
598 // Initialize all the hash tables to start out with a small # of buckets
599 : symbols_by_parent_(3),
600 fields_by_lowercase_name_(3),
601 fields_by_camelcase_name_(3),
602 fields_by_number_(3),
603 enum_values_by_number_(3) {
606 FileDescriptorTables::~FileDescriptorTables() {}
608 const FileDescriptorTables FileDescriptorTables::kEmpty;
610 void DescriptorPool::Tables::AddCheckpoint() {
611 checkpoints_.push_back(CheckPoint(this));
614 void DescriptorPool::Tables::ClearLastCheckpoint() {
615 GOOGLE_DCHECK(!checkpoints_.empty());
616 checkpoints_.pop_back();
617 if (checkpoints_.empty()) {
618 // All checkpoints have been cleared: we can now commit all of the pending
620 symbols_after_checkpoint_.clear();
621 files_after_checkpoint_.clear();
622 extensions_after_checkpoint_.clear();
626 void DescriptorPool::Tables::RollbackToLastCheckpoint() {
627 GOOGLE_DCHECK(!checkpoints_.empty());
628 const CheckPoint& checkpoint = checkpoints_.back();
630 for (int i = checkpoint.pending_symbols_before_checkpoint;
631 i < symbols_after_checkpoint_.size();
633 symbols_by_name_.erase(symbols_after_checkpoint_[i]);
635 for (int i = checkpoint.pending_files_before_checkpoint;
636 i < files_after_checkpoint_.size();
638 files_by_name_.erase(files_after_checkpoint_[i]);
640 for (int i = checkpoint.pending_extensions_before_checkpoint;
641 i < extensions_after_checkpoint_.size();
643 extensions_.erase(extensions_after_checkpoint_[i]);
646 symbols_after_checkpoint_.resize(
647 checkpoint.pending_symbols_before_checkpoint);
648 files_after_checkpoint_.resize(checkpoint.pending_files_before_checkpoint);
649 extensions_after_checkpoint_.resize(
650 checkpoint.pending_extensions_before_checkpoint);
652 STLDeleteContainerPointers(
653 strings_.begin() + checkpoint.strings_before_checkpoint, strings_.end());
654 STLDeleteContainerPointers(
655 messages_.begin() + checkpoint.messages_before_checkpoint,
657 STLDeleteContainerPointers(
658 file_tables_.begin() + checkpoint.file_tables_before_checkpoint,
660 for (int i = checkpoint.allocations_before_checkpoint;
661 i < allocations_.size();
663 operator delete(allocations_[i]);
666 strings_.resize(checkpoint.strings_before_checkpoint);
667 messages_.resize(checkpoint.messages_before_checkpoint);
668 file_tables_.resize(checkpoint.file_tables_before_checkpoint);
669 allocations_.resize(checkpoint.allocations_before_checkpoint);
670 checkpoints_.pop_back();
673 // -------------------------------------------------------------------
675 inline Symbol DescriptorPool::Tables::FindSymbol(const string& key) const {
676 const Symbol* result = FindOrNull(symbols_by_name_, key.c_str());
677 if (result == NULL) {
684 inline Symbol FileDescriptorTables::FindNestedSymbol(
685 const void* parent, const string& name) const {
686 const Symbol* result =
687 FindOrNull(symbols_by_parent_, PointerStringPair(parent, name.c_str()));
688 if (result == NULL) {
695 inline Symbol FileDescriptorTables::FindNestedSymbolOfType(
696 const void* parent, const string& name, const Symbol::Type type) const {
697 Symbol result = FindNestedSymbol(parent, name);
698 if (result.type != type) return kNullSymbol;
702 Symbol DescriptorPool::Tables::FindByNameHelper(
703 const DescriptorPool* pool, const string& name) {
704 MutexLockMaybe lock(pool->mutex_);
705 known_bad_symbols_.clear();
706 known_bad_files_.clear();
707 Symbol result = FindSymbol(name);
709 if (result.IsNull() && pool->underlay_ != NULL) {
710 // Symbol not found; check the underlay.
712 pool->underlay_->tables_->FindByNameHelper(pool->underlay_, name);
715 if (result.IsNull()) {
716 // Symbol still not found, so check fallback database.
717 if (pool->TryFindSymbolInFallbackDatabase(name)) {
718 result = FindSymbol(name);
725 inline const FileDescriptor* DescriptorPool::Tables::FindFile(
726 const string& key) const {
727 return FindPtrOrNull(files_by_name_, key.c_str());
730 inline const FieldDescriptor* FileDescriptorTables::FindFieldByNumber(
731 const Descriptor* parent, int number) const {
732 return FindPtrOrNull(fields_by_number_, make_pair(parent, number));
735 inline const FieldDescriptor* FileDescriptorTables::FindFieldByLowercaseName(
736 const void* parent, const string& lowercase_name) const {
737 return FindPtrOrNull(fields_by_lowercase_name_,
738 PointerStringPair(parent, lowercase_name.c_str()));
741 inline const FieldDescriptor* FileDescriptorTables::FindFieldByCamelcaseName(
742 const void* parent, const string& camelcase_name) const {
743 return FindPtrOrNull(fields_by_camelcase_name_,
744 PointerStringPair(parent, camelcase_name.c_str()));
747 inline const EnumValueDescriptor* FileDescriptorTables::FindEnumValueByNumber(
748 const EnumDescriptor* parent, int number) const {
749 return FindPtrOrNull(enum_values_by_number_, make_pair(parent, number));
752 inline const FieldDescriptor* DescriptorPool::Tables::FindExtension(
753 const Descriptor* extendee, int number) {
754 return FindPtrOrNull(extensions_, make_pair(extendee, number));
757 inline void DescriptorPool::Tables::FindAllExtensions(
758 const Descriptor* extendee, vector<const FieldDescriptor*>* out) const {
759 ExtensionsGroupedByDescriptorMap::const_iterator it =
760 extensions_.lower_bound(make_pair(extendee, 0));
761 for (; it != extensions_.end() && it->first.first == extendee; ++it) {
762 out->push_back(it->second);
766 // -------------------------------------------------------------------
768 bool DescriptorPool::Tables::AddSymbol(
769 const string& full_name, Symbol symbol) {
770 if (InsertIfNotPresent(&symbols_by_name_, full_name.c_str(), symbol)) {
771 symbols_after_checkpoint_.push_back(full_name.c_str());
778 bool FileDescriptorTables::AddAliasUnderParent(
779 const void* parent, const string& name, Symbol symbol) {
780 PointerStringPair by_parent_key(parent, name.c_str());
781 return InsertIfNotPresent(&symbols_by_parent_, by_parent_key, symbol);
784 bool DescriptorPool::Tables::AddFile(const FileDescriptor* file) {
785 if (InsertIfNotPresent(&files_by_name_, file->name().c_str(), file)) {
786 files_after_checkpoint_.push_back(file->name().c_str());
793 void FileDescriptorTables::AddFieldByStylizedNames(
794 const FieldDescriptor* field) {
796 if (field->is_extension()) {
797 if (field->extension_scope() == NULL) {
798 parent = field->file();
800 parent = field->extension_scope();
803 parent = field->containing_type();
806 PointerStringPair lowercase_key(parent, field->lowercase_name().c_str());
807 InsertIfNotPresent(&fields_by_lowercase_name_, lowercase_key, field);
809 PointerStringPair camelcase_key(parent, field->camelcase_name().c_str());
810 InsertIfNotPresent(&fields_by_camelcase_name_, camelcase_key, field);
813 bool FileDescriptorTables::AddFieldByNumber(const FieldDescriptor* field) {
814 DescriptorIntPair key(field->containing_type(), field->number());
815 return InsertIfNotPresent(&fields_by_number_, key, field);
818 bool FileDescriptorTables::AddEnumValueByNumber(
819 const EnumValueDescriptor* value) {
820 EnumIntPair key(value->type(), value->number());
821 return InsertIfNotPresent(&enum_values_by_number_, key, value);
824 bool DescriptorPool::Tables::AddExtension(const FieldDescriptor* field) {
825 DescriptorIntPair key(field->containing_type(), field->number());
826 if (InsertIfNotPresent(&extensions_, key, field)) {
827 extensions_after_checkpoint_.push_back(key);
834 // -------------------------------------------------------------------
836 template<typename Type>
837 Type* DescriptorPool::Tables::Allocate() {
838 return reinterpret_cast<Type*>(AllocateBytes(sizeof(Type)));
841 template<typename Type>
842 Type* DescriptorPool::Tables::AllocateArray(int count) {
843 return reinterpret_cast<Type*>(AllocateBytes(sizeof(Type) * count));
846 string* DescriptorPool::Tables::AllocateString(const string& value) {
847 string* result = new string(value);
848 strings_.push_back(result);
852 template<typename Type>
853 Type* DescriptorPool::Tables::AllocateMessage(Type* /* dummy */) {
854 Type* result = new Type;
855 messages_.push_back(result);
859 FileDescriptorTables* DescriptorPool::Tables::AllocateFileTables() {
860 FileDescriptorTables* result = new FileDescriptorTables;
861 file_tables_.push_back(result);
865 void* DescriptorPool::Tables::AllocateBytes(int size) {
866 // TODO(kenton): Would it be worthwhile to implement this in some more
867 // sophisticated way? Probably not for the open source release, but for
868 // internal use we could easily plug in one of our existing memory pool
870 if (size == 0) return NULL;
872 void* result = operator new(size);
873 allocations_.push_back(result);
877 void FileDescriptorTables::BuildLocationsByPath(
878 pair<const FileDescriptorTables*, const SourceCodeInfo*>* p) {
879 for (int i = 0, len = p->second->location_size(); i < len; ++i) {
880 const SourceCodeInfo_Location* loc = &p->second->location().Get(i);
881 p->first->locations_by_path_[Join(loc->path(), ",")] = loc;
885 const SourceCodeInfo_Location* FileDescriptorTables::GetSourceLocation(
886 const vector<int>& path, const SourceCodeInfo* info) const {
887 pair<const FileDescriptorTables*, const SourceCodeInfo*> p(
888 make_pair(this, info));
889 locations_by_path_once_.Init(&FileDescriptorTables::BuildLocationsByPath, &p);
890 return FindPtrOrNull(locations_by_path_, Join(path, ","));
893 // ===================================================================
896 DescriptorPool::ErrorCollector::~ErrorCollector() {}
898 DescriptorPool::DescriptorPool()
900 fallback_database_(NULL),
901 default_error_collector_(NULL),
904 enforce_dependencies_(true),
905 allow_unknown_(false),
906 enforce_weak_(false) {}
908 DescriptorPool::DescriptorPool(DescriptorDatabase* fallback_database,
909 ErrorCollector* error_collector)
911 fallback_database_(fallback_database),
912 default_error_collector_(error_collector),
915 enforce_dependencies_(true),
916 allow_unknown_(false),
917 enforce_weak_(false) {
920 DescriptorPool::DescriptorPool(const DescriptorPool* underlay)
922 fallback_database_(NULL),
923 default_error_collector_(NULL),
926 enforce_dependencies_(true),
927 allow_unknown_(false),
928 enforce_weak_(false) {}
930 DescriptorPool::~DescriptorPool() {
931 if (mutex_ != NULL) delete mutex_;
934 // DescriptorPool::BuildFile() defined later.
935 // DescriptorPool::BuildFileCollectingErrors() defined later.
937 void DescriptorPool::InternalDontEnforceDependencies() {
938 enforce_dependencies_ = false;
941 void DescriptorPool::AddUnusedImportTrackFile(const string& file_name) {
942 unused_import_track_files_.insert(file_name);
945 void DescriptorPool::ClearUnusedImportTrackFiles() {
946 unused_import_track_files_.clear();
949 bool DescriptorPool::InternalIsFileLoaded(const string& filename) const {
950 MutexLockMaybe lock(mutex_);
951 return tables_->FindFile(filename) != NULL;
954 // generated_pool ====================================================
959 EncodedDescriptorDatabase* generated_database_ = NULL;
960 DescriptorPool* generated_pool_ = NULL;
961 GOOGLE_PROTOBUF_DECLARE_ONCE(generated_pool_init_);
963 void DeleteGeneratedPool() {
964 delete generated_database_;
965 generated_database_ = NULL;
966 delete generated_pool_;
967 generated_pool_ = NULL;
970 static void InitGeneratedPool() {
971 generated_database_ = new EncodedDescriptorDatabase;
972 generated_pool_ = new DescriptorPool(generated_database_);
974 internal::OnShutdown(&DeleteGeneratedPool);
977 inline void InitGeneratedPoolOnce() {
978 ::google::protobuf::GoogleOnceInit(&generated_pool_init_, &InitGeneratedPool);
981 } // anonymous namespace
983 const DescriptorPool* DescriptorPool::generated_pool() {
984 InitGeneratedPoolOnce();
985 return generated_pool_;
988 DescriptorPool* DescriptorPool::internal_generated_pool() {
989 InitGeneratedPoolOnce();
990 return generated_pool_;
993 void DescriptorPool::InternalAddGeneratedFile(
994 const void* encoded_file_descriptor, int size) {
995 // So, this function is called in the process of initializing the
996 // descriptors for generated proto classes. Each generated .pb.cc file
997 // has an internal procedure called AddDescriptors() which is called at
998 // process startup, and that function calls this one in order to register
999 // the raw bytes of the FileDescriptorProto representing the file.
1001 // We do not actually construct the descriptor objects right away. We just
1002 // hang on to the bytes until they are actually needed. We actually construct
1003 // the descriptor the first time one of the following things happens:
1004 // * Someone calls a method like descriptor(), GetDescriptor(), or
1005 // GetReflection() on the generated types, which requires returning the
1006 // descriptor or an object based on it.
1007 // * Someone looks up the descriptor in DescriptorPool::generated_pool().
1009 // Once one of these happens, the DescriptorPool actually parses the
1010 // FileDescriptorProto and generates a FileDescriptor (and all its children)
1013 // Note that FileDescriptorProto is itself a generated protocol message.
1014 // Therefore, when we parse one, we have to be very careful to avoid using
1015 // any descriptor-based operations, since this might cause infinite recursion
1017 InitGeneratedPoolOnce();
1018 GOOGLE_CHECK(generated_database_->Add(encoded_file_descriptor, size));
1022 // Find*By* methods ==================================================
1024 // TODO(kenton): There's a lot of repeated code here, but I'm not sure if
1025 // there's any good way to factor it out. Think about this some time when
1026 // there's nothing more important to do (read: never).
1028 const FileDescriptor* DescriptorPool::FindFileByName(const string& name) const {
1029 MutexLockMaybe lock(mutex_);
1030 tables_->known_bad_symbols_.clear();
1031 tables_->known_bad_files_.clear();
1032 const FileDescriptor* result = tables_->FindFile(name);
1033 if (result != NULL) return result;
1034 if (underlay_ != NULL) {
1035 result = underlay_->FindFileByName(name);
1036 if (result != NULL) return result;
1038 if (TryFindFileInFallbackDatabase(name)) {
1039 result = tables_->FindFile(name);
1040 if (result != NULL) return result;
1045 const FileDescriptor* DescriptorPool::FindFileContainingSymbol(
1046 const string& symbol_name) const {
1047 MutexLockMaybe lock(mutex_);
1048 tables_->known_bad_symbols_.clear();
1049 tables_->known_bad_files_.clear();
1050 Symbol result = tables_->FindSymbol(symbol_name);
1051 if (!result.IsNull()) return result.GetFile();
1052 if (underlay_ != NULL) {
1053 const FileDescriptor* file_result =
1054 underlay_->FindFileContainingSymbol(symbol_name);
1055 if (file_result != NULL) return file_result;
1057 if (TryFindSymbolInFallbackDatabase(symbol_name)) {
1058 result = tables_->FindSymbol(symbol_name);
1059 if (!result.IsNull()) return result.GetFile();
1064 const Descriptor* DescriptorPool::FindMessageTypeByName(
1065 const string& name) const {
1066 Symbol result = tables_->FindByNameHelper(this, name);
1067 return (result.type == Symbol::MESSAGE) ? result.descriptor : NULL;
1070 const FieldDescriptor* DescriptorPool::FindFieldByName(
1071 const string& name) const {
1072 Symbol result = tables_->FindByNameHelper(this, name);
1073 if (result.type == Symbol::FIELD &&
1074 !result.field_descriptor->is_extension()) {
1075 return result.field_descriptor;
1081 const FieldDescriptor* DescriptorPool::FindExtensionByName(
1082 const string& name) const {
1083 Symbol result = tables_->FindByNameHelper(this, name);
1084 if (result.type == Symbol::FIELD &&
1085 result.field_descriptor->is_extension()) {
1086 return result.field_descriptor;
1092 const OneofDescriptor* DescriptorPool::FindOneofByName(
1093 const string& name) const {
1094 Symbol result = tables_->FindByNameHelper(this, name);
1095 return (result.type == Symbol::ONEOF) ? result.oneof_descriptor : NULL;
1098 const EnumDescriptor* DescriptorPool::FindEnumTypeByName(
1099 const string& name) const {
1100 Symbol result = tables_->FindByNameHelper(this, name);
1101 return (result.type == Symbol::ENUM) ? result.enum_descriptor : NULL;
1104 const EnumValueDescriptor* DescriptorPool::FindEnumValueByName(
1105 const string& name) const {
1106 Symbol result = tables_->FindByNameHelper(this, name);
1107 return (result.type == Symbol::ENUM_VALUE) ?
1108 result.enum_value_descriptor : NULL;
1111 const ServiceDescriptor* DescriptorPool::FindServiceByName(
1112 const string& name) const {
1113 Symbol result = tables_->FindByNameHelper(this, name);
1114 return (result.type == Symbol::SERVICE) ? result.service_descriptor : NULL;
1117 const MethodDescriptor* DescriptorPool::FindMethodByName(
1118 const string& name) const {
1119 Symbol result = tables_->FindByNameHelper(this, name);
1120 return (result.type == Symbol::METHOD) ? result.method_descriptor : NULL;
1123 const FieldDescriptor* DescriptorPool::FindExtensionByNumber(
1124 const Descriptor* extendee, int number) const {
1125 MutexLockMaybe lock(mutex_);
1126 tables_->known_bad_symbols_.clear();
1127 tables_->known_bad_files_.clear();
1128 const FieldDescriptor* result = tables_->FindExtension(extendee, number);
1129 if (result != NULL) {
1132 if (underlay_ != NULL) {
1133 result = underlay_->FindExtensionByNumber(extendee, number);
1134 if (result != NULL) return result;
1136 if (TryFindExtensionInFallbackDatabase(extendee, number)) {
1137 result = tables_->FindExtension(extendee, number);
1138 if (result != NULL) {
1145 void DescriptorPool::FindAllExtensions(
1146 const Descriptor* extendee, vector<const FieldDescriptor*>* out) const {
1147 MutexLockMaybe lock(mutex_);
1148 tables_->known_bad_symbols_.clear();
1149 tables_->known_bad_files_.clear();
1151 // Initialize tables_->extensions_ from the fallback database first
1152 // (but do this only once per descriptor).
1153 if (fallback_database_ != NULL &&
1154 tables_->extensions_loaded_from_db_.count(extendee) == 0) {
1155 vector<int> numbers;
1156 if (fallback_database_->FindAllExtensionNumbers(extendee->full_name(),
1158 for (int i = 0; i < numbers.size(); ++i) {
1159 int number = numbers[i];
1160 if (tables_->FindExtension(extendee, number) == NULL) {
1161 TryFindExtensionInFallbackDatabase(extendee, number);
1164 tables_->extensions_loaded_from_db_.insert(extendee);
1168 tables_->FindAllExtensions(extendee, out);
1169 if (underlay_ != NULL) {
1170 underlay_->FindAllExtensions(extendee, out);
1175 // -------------------------------------------------------------------
1177 const FieldDescriptor*
1178 Descriptor::FindFieldByNumber(int key) const {
1179 const FieldDescriptor* result =
1180 file()->tables_->FindFieldByNumber(this, key);
1181 if (result == NULL || result->is_extension()) {
1188 const FieldDescriptor*
1189 Descriptor::FindFieldByLowercaseName(const string& key) const {
1190 const FieldDescriptor* result =
1191 file()->tables_->FindFieldByLowercaseName(this, key);
1192 if (result == NULL || result->is_extension()) {
1199 const FieldDescriptor*
1200 Descriptor::FindFieldByCamelcaseName(const string& key) const {
1201 const FieldDescriptor* result =
1202 file()->tables_->FindFieldByCamelcaseName(this, key);
1203 if (result == NULL || result->is_extension()) {
1210 const FieldDescriptor*
1211 Descriptor::FindFieldByName(const string& key) const {
1213 file()->tables_->FindNestedSymbolOfType(this, key, Symbol::FIELD);
1214 if (!result.IsNull() && !result.field_descriptor->is_extension()) {
1215 return result.field_descriptor;
1221 const OneofDescriptor*
1222 Descriptor::FindOneofByName(const string& key) const {
1224 file()->tables_->FindNestedSymbolOfType(this, key, Symbol::ONEOF);
1225 if (!result.IsNull()) {
1226 return result.oneof_descriptor;
1232 const FieldDescriptor*
1233 Descriptor::FindExtensionByName(const string& key) const {
1235 file()->tables_->FindNestedSymbolOfType(this, key, Symbol::FIELD);
1236 if (!result.IsNull() && result.field_descriptor->is_extension()) {
1237 return result.field_descriptor;
1243 const FieldDescriptor*
1244 Descriptor::FindExtensionByLowercaseName(const string& key) const {
1245 const FieldDescriptor* result =
1246 file()->tables_->FindFieldByLowercaseName(this, key);
1247 if (result == NULL || !result->is_extension()) {
1254 const FieldDescriptor*
1255 Descriptor::FindExtensionByCamelcaseName(const string& key) const {
1256 const FieldDescriptor* result =
1257 file()->tables_->FindFieldByCamelcaseName(this, key);
1258 if (result == NULL || !result->is_extension()) {
1266 Descriptor::FindNestedTypeByName(const string& key) const {
1268 file()->tables_->FindNestedSymbolOfType(this, key, Symbol::MESSAGE);
1269 if (!result.IsNull()) {
1270 return result.descriptor;
1276 const EnumDescriptor*
1277 Descriptor::FindEnumTypeByName(const string& key) const {
1279 file()->tables_->FindNestedSymbolOfType(this, key, Symbol::ENUM);
1280 if (!result.IsNull()) {
1281 return result.enum_descriptor;
1287 const EnumValueDescriptor*
1288 Descriptor::FindEnumValueByName(const string& key) const {
1290 file()->tables_->FindNestedSymbolOfType(this, key, Symbol::ENUM_VALUE);
1291 if (!result.IsNull()) {
1292 return result.enum_value_descriptor;
1298 const EnumValueDescriptor*
1299 EnumDescriptor::FindValueByName(const string& key) const {
1301 file()->tables_->FindNestedSymbolOfType(this, key, Symbol::ENUM_VALUE);
1302 if (!result.IsNull()) {
1303 return result.enum_value_descriptor;
1309 const EnumValueDescriptor*
1310 EnumDescriptor::FindValueByNumber(int key) const {
1311 return file()->tables_->FindEnumValueByNumber(this, key);
1314 const MethodDescriptor*
1315 ServiceDescriptor::FindMethodByName(const string& key) const {
1317 file()->tables_->FindNestedSymbolOfType(this, key, Symbol::METHOD);
1318 if (!result.IsNull()) {
1319 return result.method_descriptor;
1326 FileDescriptor::FindMessageTypeByName(const string& key) const {
1327 Symbol result = tables_->FindNestedSymbolOfType(this, key, Symbol::MESSAGE);
1328 if (!result.IsNull()) {
1329 return result.descriptor;
1335 const EnumDescriptor*
1336 FileDescriptor::FindEnumTypeByName(const string& key) const {
1337 Symbol result = tables_->FindNestedSymbolOfType(this, key, Symbol::ENUM);
1338 if (!result.IsNull()) {
1339 return result.enum_descriptor;
1345 const EnumValueDescriptor*
1346 FileDescriptor::FindEnumValueByName(const string& key) const {
1348 tables_->FindNestedSymbolOfType(this, key, Symbol::ENUM_VALUE);
1349 if (!result.IsNull()) {
1350 return result.enum_value_descriptor;
1356 const ServiceDescriptor*
1357 FileDescriptor::FindServiceByName(const string& key) const {
1358 Symbol result = tables_->FindNestedSymbolOfType(this, key, Symbol::SERVICE);
1359 if (!result.IsNull()) {
1360 return result.service_descriptor;
1366 const FieldDescriptor*
1367 FileDescriptor::FindExtensionByName(const string& key) const {
1368 Symbol result = tables_->FindNestedSymbolOfType(this, key, Symbol::FIELD);
1369 if (!result.IsNull() && result.field_descriptor->is_extension()) {
1370 return result.field_descriptor;
1376 const FieldDescriptor*
1377 FileDescriptor::FindExtensionByLowercaseName(const string& key) const {
1378 const FieldDescriptor* result = tables_->FindFieldByLowercaseName(this, key);
1379 if (result == NULL || !result->is_extension()) {
1386 const FieldDescriptor*
1387 FileDescriptor::FindExtensionByCamelcaseName(const string& key) const {
1388 const FieldDescriptor* result = tables_->FindFieldByCamelcaseName(this, key);
1389 if (result == NULL || !result->is_extension()) {
1396 const Descriptor::ExtensionRange*
1397 Descriptor::FindExtensionRangeContainingNumber(int number) const {
1398 // Linear search should be fine because we don't expect a message to have
1399 // more than a couple extension ranges.
1400 for (int i = 0; i < extension_range_count(); i++) {
1401 if (number >= extension_range(i)->start &&
1402 number < extension_range(i)->end) {
1403 return extension_range(i);
1409 // -------------------------------------------------------------------
1411 bool DescriptorPool::TryFindFileInFallbackDatabase(const string& name) const {
1412 if (fallback_database_ == NULL) return false;
1414 if (tables_->known_bad_files_.count(name) > 0) return false;
1416 FileDescriptorProto file_proto;
1417 if (!fallback_database_->FindFileByName(name, &file_proto) ||
1418 BuildFileFromDatabase(file_proto) == NULL) {
1419 tables_->known_bad_files_.insert(name);
1425 bool DescriptorPool::IsSubSymbolOfBuiltType(const string& name) const {
1426 string prefix = name;
1428 string::size_type dot_pos = prefix.find_last_of('.');
1429 if (dot_pos == string::npos) {
1432 prefix = prefix.substr(0, dot_pos);
1433 Symbol symbol = tables_->FindSymbol(prefix);
1434 // If the symbol type is anything other than PACKAGE, then its complete
1435 // definition is already known.
1436 if (!symbol.IsNull() && symbol.type != Symbol::PACKAGE) {
1440 if (underlay_ != NULL) {
1441 // Check to see if any prefix of this symbol exists in the underlay.
1442 return underlay_->IsSubSymbolOfBuiltType(name);
1447 bool DescriptorPool::TryFindSymbolInFallbackDatabase(const string& name) const {
1448 if (fallback_database_ == NULL) return false;
1450 if (tables_->known_bad_symbols_.count(name) > 0) return false;
1452 FileDescriptorProto file_proto;
1453 if (// We skip looking in the fallback database if the name is a sub-symbol
1454 // of any descriptor that already exists in the descriptor pool (except
1455 // for package descriptors). This is valid because all symbols except
1456 // for packages are defined in a single file, so if the symbol exists
1457 // then we should already have its definition.
1459 // The other reason to do this is to support "overriding" type
1460 // definitions by merging two databases that define the same type. (Yes,
1461 // people do this.) The main difficulty with making this work is that
1462 // FindFileContainingSymbol() is allowed to return both false positives
1463 // (e.g., SimpleDescriptorDatabase, UpgradedDescriptorDatabase) and false
1464 // negatives (e.g. ProtoFileParser, SourceTreeDescriptorDatabase).
1465 // When two such databases are merged, looking up a non-existent
1466 // sub-symbol of a type that already exists in the descriptor pool can
1467 // result in an attempt to load multiple definitions of the same type.
1468 // The check below avoids this.
1469 IsSubSymbolOfBuiltType(name)
1471 // Look up file containing this symbol in fallback database.
1472 || !fallback_database_->FindFileContainingSymbol(name, &file_proto)
1474 // Check if we've already built this file. If so, it apparently doesn't
1475 // contain the symbol we're looking for. Some DescriptorDatabases
1476 // return false positives.
1477 || tables_->FindFile(file_proto.name()) != NULL
1480 || BuildFileFromDatabase(file_proto) == NULL) {
1481 tables_->known_bad_symbols_.insert(name);
1488 bool DescriptorPool::TryFindExtensionInFallbackDatabase(
1489 const Descriptor* containing_type, int field_number) const {
1490 if (fallback_database_ == NULL) return false;
1492 FileDescriptorProto file_proto;
1493 if (!fallback_database_->FindFileContainingExtension(
1494 containing_type->full_name(), field_number, &file_proto)) {
1498 if (tables_->FindFile(file_proto.name()) != NULL) {
1499 // We've already loaded this file, and it apparently doesn't contain the
1500 // extension we're looking for. Some DescriptorDatabases return false
1505 if (BuildFileFromDatabase(file_proto) == NULL) {
1512 // ===================================================================
1514 string FieldDescriptor::DefaultValueAsString(bool quote_string_type) const {
1515 GOOGLE_CHECK(has_default_value()) << "No default value";
1516 switch (cpp_type()) {
1518 return SimpleItoa(default_value_int32());
1521 return SimpleItoa(default_value_int64());
1523 case CPPTYPE_UINT32:
1524 return SimpleItoa(default_value_uint32());
1526 case CPPTYPE_UINT64:
1527 return SimpleItoa(default_value_uint64());
1530 return SimpleFtoa(default_value_float());
1532 case CPPTYPE_DOUBLE:
1533 return SimpleDtoa(default_value_double());
1536 return default_value_bool() ? "true" : "false";
1538 case CPPTYPE_STRING:
1539 if (quote_string_type) {
1540 return "\"" + CEscape(default_value_string()) + "\"";
1542 if (type() == TYPE_BYTES) {
1543 return CEscape(default_value_string());
1545 return default_value_string();
1550 return default_value_enum()->name();
1552 case CPPTYPE_MESSAGE:
1553 GOOGLE_LOG(DFATAL) << "Messages can't have default values!";
1556 GOOGLE_LOG(FATAL) << "Can't get here: failed to get default value as string";
1560 // CopyTo methods ====================================================
1562 void FileDescriptor::CopyTo(FileDescriptorProto* proto) const {
1563 proto->set_name(name());
1564 if (!package().empty()) proto->set_package(package());
1566 for (int i = 0; i < dependency_count(); i++) {
1567 proto->add_dependency(dependency(i)->name());
1570 for (int i = 0; i < public_dependency_count(); i++) {
1571 proto->add_public_dependency(public_dependencies_[i]);
1574 for (int i = 0; i < weak_dependency_count(); i++) {
1575 proto->add_weak_dependency(weak_dependencies_[i]);
1578 for (int i = 0; i < message_type_count(); i++) {
1579 message_type(i)->CopyTo(proto->add_message_type());
1581 for (int i = 0; i < enum_type_count(); i++) {
1582 enum_type(i)->CopyTo(proto->add_enum_type());
1584 for (int i = 0; i < service_count(); i++) {
1585 service(i)->CopyTo(proto->add_service());
1587 for (int i = 0; i < extension_count(); i++) {
1588 extension(i)->CopyTo(proto->add_extension());
1591 if (&options() != &FileOptions::default_instance()) {
1592 proto->mutable_options()->CopyFrom(options());
1596 void FileDescriptor::CopySourceCodeInfoTo(FileDescriptorProto* proto) const {
1597 if (source_code_info_ != &SourceCodeInfo::default_instance()) {
1598 proto->mutable_source_code_info()->CopyFrom(*source_code_info_);
1602 void Descriptor::CopyTo(DescriptorProto* proto) const {
1603 proto->set_name(name());
1605 for (int i = 0; i < field_count(); i++) {
1606 field(i)->CopyTo(proto->add_field());
1608 for (int i = 0; i < oneof_decl_count(); i++) {
1609 oneof_decl(i)->CopyTo(proto->add_oneof_decl());
1611 for (int i = 0; i < nested_type_count(); i++) {
1612 nested_type(i)->CopyTo(proto->add_nested_type());
1614 for (int i = 0; i < enum_type_count(); i++) {
1615 enum_type(i)->CopyTo(proto->add_enum_type());
1617 for (int i = 0; i < extension_range_count(); i++) {
1618 DescriptorProto::ExtensionRange* range = proto->add_extension_range();
1619 range->set_start(extension_range(i)->start);
1620 range->set_end(extension_range(i)->end);
1622 for (int i = 0; i < extension_count(); i++) {
1623 extension(i)->CopyTo(proto->add_extension());
1626 if (&options() != &MessageOptions::default_instance()) {
1627 proto->mutable_options()->CopyFrom(options());
1631 void FieldDescriptor::CopyTo(FieldDescriptorProto* proto) const {
1632 proto->set_name(name());
1633 proto->set_number(number());
1635 // Some compilers do not allow static_cast directly between two enum types,
1636 // so we must cast to int first.
1637 proto->set_label(static_cast<FieldDescriptorProto::Label>(
1638 implicit_cast<int>(label())));
1639 proto->set_type(static_cast<FieldDescriptorProto::Type>(
1640 implicit_cast<int>(type())));
1642 if (is_extension()) {
1643 if (!containing_type()->is_unqualified_placeholder_) {
1644 proto->set_extendee(".");
1646 proto->mutable_extendee()->append(containing_type()->full_name());
1649 if (cpp_type() == CPPTYPE_MESSAGE) {
1650 if (message_type()->is_placeholder_) {
1651 // We don't actually know if the type is a message type. It could be
1653 proto->clear_type();
1656 if (!message_type()->is_unqualified_placeholder_) {
1657 proto->set_type_name(".");
1659 proto->mutable_type_name()->append(message_type()->full_name());
1660 } else if (cpp_type() == CPPTYPE_ENUM) {
1661 if (!enum_type()->is_unqualified_placeholder_) {
1662 proto->set_type_name(".");
1664 proto->mutable_type_name()->append(enum_type()->full_name());
1667 if (has_default_value()) {
1668 proto->set_default_value(DefaultValueAsString(false));
1671 if (containing_oneof() != NULL && !is_extension()) {
1672 proto->set_oneof_index(containing_oneof()->index());
1675 if (&options() != &FieldOptions::default_instance()) {
1676 proto->mutable_options()->CopyFrom(options());
1680 void OneofDescriptor::CopyTo(OneofDescriptorProto* proto) const {
1681 proto->set_name(name());
1684 void EnumDescriptor::CopyTo(EnumDescriptorProto* proto) const {
1685 proto->set_name(name());
1687 for (int i = 0; i < value_count(); i++) {
1688 value(i)->CopyTo(proto->add_value());
1691 if (&options() != &EnumOptions::default_instance()) {
1692 proto->mutable_options()->CopyFrom(options());
1696 void EnumValueDescriptor::CopyTo(EnumValueDescriptorProto* proto) const {
1697 proto->set_name(name());
1698 proto->set_number(number());
1700 if (&options() != &EnumValueOptions::default_instance()) {
1701 proto->mutable_options()->CopyFrom(options());
1705 void ServiceDescriptor::CopyTo(ServiceDescriptorProto* proto) const {
1706 proto->set_name(name());
1708 for (int i = 0; i < method_count(); i++) {
1709 method(i)->CopyTo(proto->add_method());
1712 if (&options() != &ServiceOptions::default_instance()) {
1713 proto->mutable_options()->CopyFrom(options());
1717 void MethodDescriptor::CopyTo(MethodDescriptorProto* proto) const {
1718 proto->set_name(name());
1720 if (!input_type()->is_unqualified_placeholder_) {
1721 proto->set_input_type(".");
1723 proto->mutable_input_type()->append(input_type()->full_name());
1725 if (!output_type()->is_unqualified_placeholder_) {
1726 proto->set_output_type(".");
1728 proto->mutable_output_type()->append(output_type()->full_name());
1730 if (&options() != &MethodOptions::default_instance()) {
1731 proto->mutable_options()->CopyFrom(options());
1735 // DebugString methods ===============================================
1739 // Used by each of the option formatters.
1740 bool RetrieveOptions(int depth,
1741 const Message &options,
1742 vector<string> *option_entries) {
1743 option_entries->clear();
1744 const Reflection* reflection = options.GetReflection();
1745 vector<const FieldDescriptor*> fields;
1746 reflection->ListFields(options, &fields);
1747 for (int i = 0; i < fields.size(); i++) {
1749 bool repeated = false;
1750 if (fields[i]->is_repeated()) {
1751 count = reflection->FieldSize(options, fields[i]);
1754 for (int j = 0; j < count; j++) {
1756 if (fields[i]->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) {
1758 TextFormat::Printer printer;
1759 printer.SetInitialIndentLevel(depth + 1);
1760 printer.PrintFieldValueToString(options, fields[i],
1761 repeated ? j : -1, &tmp);
1762 fieldval.append("{\n");
1763 fieldval.append(tmp);
1764 fieldval.append(depth * 2, ' ');
1765 fieldval.append("}");
1767 TextFormat::PrintFieldValueToString(options, fields[i],
1768 repeated ? j : -1, &fieldval);
1771 if (fields[i]->is_extension()) {
1772 name = "(." + fields[i]->full_name() + ")";
1774 name = fields[i]->name();
1776 option_entries->push_back(name + " = " + fieldval);
1779 return !option_entries->empty();
1782 // Formats options that all appear together in brackets. Does not include
1784 bool FormatBracketedOptions(int depth, const Message &options, string *output) {
1785 vector<string> all_options;
1786 if (RetrieveOptions(depth, options, &all_options)) {
1787 output->append(Join(all_options, ", "));
1789 return !all_options.empty();
1792 // Formats options one per line
1793 bool FormatLineOptions(int depth, const Message &options, string *output) {
1794 string prefix(depth * 2, ' ');
1795 vector<string> all_options;
1796 if (RetrieveOptions(depth, options, &all_options)) {
1797 for (int i = 0; i < all_options.size(); i++) {
1798 strings::SubstituteAndAppend(output, "$0option $1;\n",
1799 prefix, all_options[i]);
1802 return !all_options.empty();
1805 } // anonymous namespace
1807 string FileDescriptor::DebugString() const {
1808 string contents = "syntax = \"proto2\";\n\n";
1810 set<int> public_dependencies;
1811 set<int> weak_dependencies;
1812 public_dependencies.insert(public_dependencies_,
1813 public_dependencies_ + public_dependency_count_);
1814 weak_dependencies.insert(weak_dependencies_,
1815 weak_dependencies_ + weak_dependency_count_);
1817 for (int i = 0; i < dependency_count(); i++) {
1818 if (public_dependencies.count(i) > 0) {
1819 strings::SubstituteAndAppend(&contents, "import public \"$0\";\n",
1820 dependency(i)->name());
1821 } else if (weak_dependencies.count(i) > 0) {
1822 strings::SubstituteAndAppend(&contents, "import weak \"$0\";\n",
1823 dependency(i)->name());
1825 strings::SubstituteAndAppend(&contents, "import \"$0\";\n",
1826 dependency(i)->name());
1830 if (!package().empty()) {
1831 strings::SubstituteAndAppend(&contents, "package $0;\n\n", package());
1834 if (FormatLineOptions(0, options(), &contents)) {
1835 contents.append("\n"); // add some space if we had options
1838 for (int i = 0; i < enum_type_count(); i++) {
1839 enum_type(i)->DebugString(0, &contents);
1840 contents.append("\n");
1843 // Find all the 'group' type extensions; we will not output their nested
1844 // definitions (those will be done with their group field descriptor).
1845 set<const Descriptor*> groups;
1846 for (int i = 0; i < extension_count(); i++) {
1847 if (extension(i)->type() == FieldDescriptor::TYPE_GROUP) {
1848 groups.insert(extension(i)->message_type());
1852 for (int i = 0; i < message_type_count(); i++) {
1853 if (groups.count(message_type(i)) == 0) {
1854 strings::SubstituteAndAppend(&contents, "message $0",
1855 message_type(i)->name());
1856 message_type(i)->DebugString(0, &contents);
1857 contents.append("\n");
1861 for (int i = 0; i < service_count(); i++) {
1862 service(i)->DebugString(&contents);
1863 contents.append("\n");
1866 const Descriptor* containing_type = NULL;
1867 for (int i = 0; i < extension_count(); i++) {
1868 if (extension(i)->containing_type() != containing_type) {
1869 if (i > 0) contents.append("}\n\n");
1870 containing_type = extension(i)->containing_type();
1871 strings::SubstituteAndAppend(&contents, "extend .$0 {\n",
1872 containing_type->full_name());
1874 extension(i)->DebugString(1, FieldDescriptor::PRINT_LABEL, &contents);
1876 if (extension_count() > 0) contents.append("}\n\n");
1881 string Descriptor::DebugString() const {
1883 strings::SubstituteAndAppend(&contents, "message $0", name());
1884 DebugString(0, &contents);
1888 void Descriptor::DebugString(int depth, string *contents) const {
1889 string prefix(depth * 2, ' ');
1891 contents->append(" {\n");
1893 FormatLineOptions(depth, options(), contents);
1895 // Find all the 'group' types for fields and extensions; we will not output
1896 // their nested definitions (those will be done with their group field
1898 set<const Descriptor*> groups;
1899 for (int i = 0; i < field_count(); i++) {
1900 if (field(i)->type() == FieldDescriptor::TYPE_GROUP) {
1901 groups.insert(field(i)->message_type());
1904 for (int i = 0; i < extension_count(); i++) {
1905 if (extension(i)->type() == FieldDescriptor::TYPE_GROUP) {
1906 groups.insert(extension(i)->message_type());
1910 for (int i = 0; i < nested_type_count(); i++) {
1911 if (groups.count(nested_type(i)) == 0) {
1912 strings::SubstituteAndAppend(contents, "$0 message $1",
1913 prefix, nested_type(i)->name());
1914 nested_type(i)->DebugString(depth, contents);
1917 for (int i = 0; i < enum_type_count(); i++) {
1918 enum_type(i)->DebugString(depth, contents);
1920 for (int i = 0; i < field_count(); i++) {
1921 if (field(i)->containing_oneof() == NULL) {
1922 field(i)->DebugString(depth, FieldDescriptor::PRINT_LABEL, contents);
1923 } else if (field(i)->containing_oneof()->field(0) == field(i)) {
1924 // This is the first field in this oneof, so print the whole oneof.
1925 field(i)->containing_oneof()->DebugString(depth, contents);
1929 for (int i = 0; i < extension_range_count(); i++) {
1930 strings::SubstituteAndAppend(contents, "$0 extensions $1 to $2;\n",
1932 extension_range(i)->start,
1933 extension_range(i)->end - 1);
1936 // Group extensions by what they extend, so they can be printed out together.
1937 const Descriptor* containing_type = NULL;
1938 for (int i = 0; i < extension_count(); i++) {
1939 if (extension(i)->containing_type() != containing_type) {
1940 if (i > 0) strings::SubstituteAndAppend(contents, "$0 }\n", prefix);
1941 containing_type = extension(i)->containing_type();
1942 strings::SubstituteAndAppend(contents, "$0 extend .$1 {\n",
1943 prefix, containing_type->full_name());
1945 extension(i)->DebugString(
1946 depth + 1, FieldDescriptor::PRINT_LABEL, contents);
1948 if (extension_count() > 0)
1949 strings::SubstituteAndAppend(contents, "$0 }\n", prefix);
1951 strings::SubstituteAndAppend(contents, "$0}\n", prefix);
1954 string FieldDescriptor::DebugString() const {
1957 if (is_extension()) {
1958 strings::SubstituteAndAppend(&contents, "extend .$0 {\n",
1959 containing_type()->full_name());
1962 DebugString(depth, PRINT_LABEL, &contents);
1963 if (is_extension()) {
1964 contents.append("}\n");
1969 void FieldDescriptor::DebugString(int depth,
1970 PrintLabelFlag print_label_flag,
1971 string *contents) const {
1972 string prefix(depth * 2, ' ');
1976 field_type = "." + message_type()->full_name();
1979 field_type = "." + enum_type()->full_name();
1982 field_type = kTypeToName[type()];
1986 if (print_label_flag == PRINT_LABEL) {
1987 label = kLabelToName[this->label()];
1988 label.push_back(' ');
1991 strings::SubstituteAndAppend(contents, "$0$1$2 $3 = $4",
1995 type() == TYPE_GROUP ? message_type()->name() :
1999 bool bracketed = false;
2000 if (has_default_value()) {
2002 strings::SubstituteAndAppend(contents, " [default = $0",
2003 DefaultValueAsString(true));
2006 string formatted_options;
2007 if (FormatBracketedOptions(depth, options(), &formatted_options)) {
2008 contents->append(bracketed ? ", " : " [");
2010 contents->append(formatted_options);
2014 contents->append("]");
2017 if (type() == TYPE_GROUP) {
2018 message_type()->DebugString(depth, contents);
2020 contents->append(";\n");
2024 string OneofDescriptor::DebugString() const {
2026 DebugString(0, &contents);
2030 void OneofDescriptor::DebugString(int depth, string* contents) const {
2031 string prefix(depth * 2, ' ');
2033 strings::SubstituteAndAppend(
2034 contents, "$0 oneof $1 {\n", prefix, name());
2035 for (int i = 0; i < field_count(); i++) {
2036 field(i)->DebugString(depth, FieldDescriptor::OMIT_LABEL, contents);
2038 strings::SubstituteAndAppend(contents, "$0}\n", prefix);
2041 string EnumDescriptor::DebugString() const {
2043 DebugString(0, &contents);
2047 void EnumDescriptor::DebugString(int depth, string *contents) const {
2048 string prefix(depth * 2, ' ');
2050 strings::SubstituteAndAppend(contents, "$0enum $1 {\n",
2053 FormatLineOptions(depth, options(), contents);
2055 for (int i = 0; i < value_count(); i++) {
2056 value(i)->DebugString(depth, contents);
2058 strings::SubstituteAndAppend(contents, "$0}\n", prefix);
2061 string EnumValueDescriptor::DebugString() const {
2063 DebugString(0, &contents);
2067 void EnumValueDescriptor::DebugString(int depth, string *contents) const {
2068 string prefix(depth * 2, ' ');
2069 strings::SubstituteAndAppend(contents, "$0$1 = $2",
2070 prefix, name(), number());
2072 string formatted_options;
2073 if (FormatBracketedOptions(depth, options(), &formatted_options)) {
2074 strings::SubstituteAndAppend(contents, " [$0]", formatted_options);
2076 contents->append(";\n");
2079 string ServiceDescriptor::DebugString() const {
2081 DebugString(&contents);
2085 void ServiceDescriptor::DebugString(string *contents) const {
2086 strings::SubstituteAndAppend(contents, "service $0 {\n", name());
2088 FormatLineOptions(1, options(), contents);
2090 for (int i = 0; i < method_count(); i++) {
2091 method(i)->DebugString(1, contents);
2094 contents->append("}\n");
2097 string MethodDescriptor::DebugString() const {
2099 DebugString(0, &contents);
2103 void MethodDescriptor::DebugString(int depth, string *contents) const {
2104 string prefix(depth * 2, ' ');
2106 strings::SubstituteAndAppend(contents, "$0rpc $1(.$2) returns (.$3)",
2108 input_type()->full_name(),
2109 output_type()->full_name());
2111 string formatted_options;
2112 if (FormatLineOptions(depth, options(), &formatted_options)) {
2113 strings::SubstituteAndAppend(contents, " {\n$0$1}\n",
2114 formatted_options, prefix);
2116 contents->append(";\n");
2121 // Location methods ===============================================
2123 bool FileDescriptor::GetSourceLocation(const vector<int>& path,
2124 SourceLocation* out_location) const {
2125 GOOGLE_CHECK_NOTNULL(out_location);
2126 if (source_code_info_) {
2127 if (const SourceCodeInfo_Location* loc =
2128 tables_->GetSourceLocation(path, source_code_info_)) {
2129 const RepeatedField<int32>& span = loc->span();
2130 if (span.size() == 3 || span.size() == 4) {
2131 out_location->start_line = span.Get(0);
2132 out_location->start_column = span.Get(1);
2133 out_location->end_line = span.Get(span.size() == 3 ? 0 : 2);
2134 out_location->end_column = span.Get(span.size() - 1);
2136 out_location->leading_comments = loc->leading_comments();
2137 out_location->trailing_comments = loc->trailing_comments();
2145 bool FieldDescriptor::is_packed() const {
2146 return is_packable() && (options_ != NULL) && options_->packed();
2149 bool Descriptor::GetSourceLocation(SourceLocation* out_location) const {
2151 GetLocationPath(&path);
2152 return file()->GetSourceLocation(path, out_location);
2155 bool FieldDescriptor::GetSourceLocation(SourceLocation* out_location) const {
2157 GetLocationPath(&path);
2158 return file()->GetSourceLocation(path, out_location);
2161 bool OneofDescriptor::GetSourceLocation(SourceLocation* out_location) const {
2163 GetLocationPath(&path);
2164 return containing_type()->file()->GetSourceLocation(path, out_location);
2167 bool EnumDescriptor::GetSourceLocation(SourceLocation* out_location) const {
2169 GetLocationPath(&path);
2170 return file()->GetSourceLocation(path, out_location);
2173 bool MethodDescriptor::GetSourceLocation(SourceLocation* out_location) const {
2175 GetLocationPath(&path);
2176 return service()->file()->GetSourceLocation(path, out_location);
2179 bool ServiceDescriptor::GetSourceLocation(SourceLocation* out_location) const {
2181 GetLocationPath(&path);
2182 return file()->GetSourceLocation(path, out_location);
2185 bool EnumValueDescriptor::GetSourceLocation(
2186 SourceLocation* out_location) const {
2188 GetLocationPath(&path);
2189 return type()->file()->GetSourceLocation(path, out_location);
2192 void Descriptor::GetLocationPath(vector<int>* output) const {
2193 if (containing_type()) {
2194 containing_type()->GetLocationPath(output);
2195 output->push_back(DescriptorProto::kNestedTypeFieldNumber);
2196 output->push_back(index());
2198 output->push_back(FileDescriptorProto::kMessageTypeFieldNumber);
2199 output->push_back(index());
2203 void FieldDescriptor::GetLocationPath(vector<int>* output) const {
2204 if (is_extension()) {
2205 if (extension_scope() == NULL) {
2206 output->push_back(FileDescriptorProto::kExtensionFieldNumber);
2207 output->push_back(index());
2209 extension_scope()->GetLocationPath(output);
2210 output->push_back(DescriptorProto::kExtensionFieldNumber);
2211 output->push_back(index());
2214 containing_type()->GetLocationPath(output);
2215 output->push_back(DescriptorProto::kFieldFieldNumber);
2216 output->push_back(index());
2220 void OneofDescriptor::GetLocationPath(vector<int>* output) const {
2221 containing_type()->GetLocationPath(output);
2222 output->push_back(DescriptorProto::kOneofDeclFieldNumber);
2223 output->push_back(index());
2226 void EnumDescriptor::GetLocationPath(vector<int>* output) const {
2227 if (containing_type()) {
2228 containing_type()->GetLocationPath(output);
2229 output->push_back(DescriptorProto::kEnumTypeFieldNumber);
2230 output->push_back(index());
2232 output->push_back(FileDescriptorProto::kEnumTypeFieldNumber);
2233 output->push_back(index());
2237 void EnumValueDescriptor::GetLocationPath(vector<int>* output) const {
2238 type()->GetLocationPath(output);
2239 output->push_back(EnumDescriptorProto::kValueFieldNumber);
2240 output->push_back(index());
2243 void ServiceDescriptor::GetLocationPath(vector<int>* output) const {
2244 output->push_back(FileDescriptorProto::kServiceFieldNumber);
2245 output->push_back(index());
2248 void MethodDescriptor::GetLocationPath(vector<int>* output) const {
2249 service()->GetLocationPath(output);
2250 output->push_back(ServiceDescriptorProto::kMethodFieldNumber);
2251 output->push_back(index());
2254 // ===================================================================
2258 // Represents an options message to interpret. Extension names in the option
2259 // name are respolved relative to name_scope. element_name and orig_opt are
2260 // used only for error reporting (since the parser records locations against
2261 // pointers in the original options, not the mutable copy). The Message must be
2262 // one of the Options messages in descriptor.proto.
2263 struct OptionsToInterpret {
2264 OptionsToInterpret(const string& ns,
2266 const Message* orig_opt,
2270 original_options(orig_opt),
2274 string element_name;
2275 const Message* original_options;
2281 class DescriptorBuilder {
2283 DescriptorBuilder(const DescriptorPool* pool,
2284 DescriptorPool::Tables* tables,
2285 DescriptorPool::ErrorCollector* error_collector);
2286 ~DescriptorBuilder();
2288 const FileDescriptor* BuildFile(const FileDescriptorProto& proto);
2291 friend class OptionInterpreter;
2293 const DescriptorPool* pool_;
2294 DescriptorPool::Tables* tables_; // for convenience
2295 DescriptorPool::ErrorCollector* error_collector_;
2297 // As we build descriptors we store copies of the options messages in
2298 // them. We put pointers to those copies in this vector, as we build, so we
2299 // can later (after cross-linking) interpret those options.
2300 vector<OptionsToInterpret> options_to_interpret_;
2304 FileDescriptor* file_;
2305 FileDescriptorTables* file_tables_;
2306 set<const FileDescriptor*> dependencies_;
2308 // unused_dependency_ is used to record the unused imported files.
2309 // Note: public import is not considered.
2310 set<const FileDescriptor*> unused_dependency_;
2312 // If LookupSymbol() finds a symbol that is in a file which is not a declared
2313 // dependency of this file, it will fail, but will set
2314 // possible_undeclared_dependency_ to point at that file. This is only used
2315 // by AddNotDefinedError() to report a more useful error message.
2316 // possible_undeclared_dependency_name_ is the name of the symbol that was
2317 // actually found in possible_undeclared_dependency_, which may be a parent
2318 // of the symbol actually looked for.
2319 const FileDescriptor* possible_undeclared_dependency_;
2320 string possible_undeclared_dependency_name_;
2322 // If LookupSymbol() could resolve a symbol which is not defined,
2323 // record the resolved name. This is only used by AddNotDefinedError()
2324 // to report a more useful error message.
2325 string undefine_resolved_name_;
2327 void AddError(const string& element_name,
2328 const Message& descriptor,
2329 DescriptorPool::ErrorCollector::ErrorLocation location,
2330 const string& error);
2331 void AddError(const string& element_name,
2332 const Message& descriptor,
2333 DescriptorPool::ErrorCollector::ErrorLocation location,
2335 void AddRecursiveImportError(const FileDescriptorProto& proto, int from_here);
2336 void AddTwiceListedError(const FileDescriptorProto& proto, int index);
2337 void AddImportError(const FileDescriptorProto& proto, int index);
2339 // Adds an error indicating that undefined_symbol was not defined. Must
2340 // only be called after LookupSymbol() fails.
2341 void AddNotDefinedError(
2342 const string& element_name,
2343 const Message& descriptor,
2344 DescriptorPool::ErrorCollector::ErrorLocation location,
2345 const string& undefined_symbol);
2347 void AddWarning(const string& element_name, const Message& descriptor,
2348 DescriptorPool::ErrorCollector::ErrorLocation location,
2349 const string& error);
2351 // Silly helper which determines if the given file is in the given package.
2352 // I.e., either file->package() == package_name or file->package() is a
2353 // nested package within package_name.
2354 bool IsInPackage(const FileDescriptor* file, const string& package_name);
2356 // Helper function which finds all public dependencies of the given file, and
2357 // stores the them in the dependencies_ set in the builder.
2358 void RecordPublicDependencies(const FileDescriptor* file);
2360 // Like tables_->FindSymbol(), but additionally:
2361 // - Search the pool's underlay if not found in tables_.
2362 // - Insure that the resulting Symbol is from one of the file's declared
2364 Symbol FindSymbol(const string& name);
2366 // Like FindSymbol() but does not require that the symbol is in one of the
2367 // file's declared dependencies.
2368 Symbol FindSymbolNotEnforcingDeps(const string& name);
2370 // This implements the body of FindSymbolNotEnforcingDeps().
2371 Symbol FindSymbolNotEnforcingDepsHelper(const DescriptorPool* pool,
2372 const string& name);
2374 // Like FindSymbol(), but looks up the name relative to some other symbol
2375 // name. This first searches siblings of relative_to, then siblings of its
2376 // parents, etc. For example, LookupSymbol("foo.bar", "baz.qux.corge") makes
2377 // the following calls, returning the first non-null result:
2378 // FindSymbol("baz.qux.foo.bar"), FindSymbol("baz.foo.bar"),
2379 // FindSymbol("foo.bar"). If AllowUnknownDependencies() has been called
2380 // on the DescriptorPool, this will generate a placeholder type if
2381 // the name is not found (unless the name itself is malformed). The
2382 // placeholder_type parameter indicates what kind of placeholder should be
2383 // constructed in this case. The resolve_mode parameter determines whether
2384 // any symbol is returned, or only symbols that are types. Note, however,
2385 // that LookupSymbol may still return a non-type symbol in LOOKUP_TYPES mode,
2386 // if it believes that's all it could refer to. The caller should always
2387 // check that it receives the type of symbol it was expecting.
2388 enum PlaceholderType {
2389 PLACEHOLDER_MESSAGE,
2391 PLACEHOLDER_EXTENDABLE_MESSAGE
2394 LOOKUP_ALL, LOOKUP_TYPES
2396 Symbol LookupSymbol(const string& name, const string& relative_to,
2397 PlaceholderType placeholder_type = PLACEHOLDER_MESSAGE,
2398 ResolveMode resolve_mode = LOOKUP_ALL);
2400 // Like LookupSymbol() but will not return a placeholder even if
2401 // AllowUnknownDependencies() has been used.
2402 Symbol LookupSymbolNoPlaceholder(const string& name,
2403 const string& relative_to,
2404 ResolveMode resolve_mode = LOOKUP_ALL);
2406 // Creates a placeholder type suitable for return from LookupSymbol(). May
2407 // return kNullSymbol if the name is not a valid type name.
2408 Symbol NewPlaceholder(const string& name, PlaceholderType placeholder_type);
2410 // Creates a placeholder file. Never returns NULL. This is used when an
2411 // import is not found and AllowUnknownDependencies() is enabled.
2412 const FileDescriptor* NewPlaceholderFile(const string& name);
2414 // Calls tables_->AddSymbol() and records an error if it fails. Returns
2415 // true if successful or false if failed, though most callers can ignore
2416 // the return value since an error has already been recorded.
2417 bool AddSymbol(const string& full_name,
2418 const void* parent, const string& name,
2419 const Message& proto, Symbol symbol);
2421 // Like AddSymbol(), but succeeds if the symbol is already defined as long
2422 // as the existing definition is also a package (because it's OK to define
2423 // the same package in two different files). Also adds all parents of the
2424 // packgae to the symbol table (e.g. AddPackage("foo.bar", ...) will add
2425 // "foo.bar" and "foo" to the table).
2426 void AddPackage(const string& name, const Message& proto,
2427 const FileDescriptor* file);
2429 // Checks that the symbol name contains only alphanumeric characters and
2430 // underscores. Records an error otherwise.
2431 void ValidateSymbolName(const string& name, const string& full_name,
2432 const Message& proto);
2434 // Like ValidateSymbolName(), but the name is allowed to contain periods and
2435 // an error is indicated by returning false (not recording the error).
2436 bool ValidateQualifiedName(const string& name);
2438 // Used by BUILD_ARRAY macro (below) to avoid having to have the type
2439 // specified as a macro parameter.
2440 template <typename Type>
2441 inline void AllocateArray(int size, Type** output) {
2442 *output = tables_->AllocateArray<Type>(size);
2445 // Allocates a copy of orig_options in tables_ and stores it in the
2446 // descriptor. Remembers its uninterpreted options, to be interpreted
2447 // later. DescriptorT must be one of the Descriptor messages from
2448 // descriptor.proto.
2449 template<class DescriptorT> void AllocateOptions(
2450 const typename DescriptorT::OptionsType& orig_options,
2451 DescriptorT* descriptor);
2452 // Specialization for FileOptions.
2453 void AllocateOptions(const FileOptions& orig_options,
2454 FileDescriptor* descriptor);
2456 // Implementation for AllocateOptions(). Don't call this directly.
2457 template<class DescriptorT> void AllocateOptionsImpl(
2458 const string& name_scope,
2459 const string& element_name,
2460 const typename DescriptorT::OptionsType& orig_options,
2461 DescriptorT* descriptor);
2463 // These methods all have the same signature for the sake of the BUILD_ARRAY
2465 void BuildMessage(const DescriptorProto& proto,
2466 const Descriptor* parent,
2467 Descriptor* result);
2468 void BuildFieldOrExtension(const FieldDescriptorProto& proto,
2469 const Descriptor* parent,
2470 FieldDescriptor* result,
2472 void BuildField(const FieldDescriptorProto& proto,
2473 const Descriptor* parent,
2474 FieldDescriptor* result) {
2475 BuildFieldOrExtension(proto, parent, result, false);
2477 void BuildExtension(const FieldDescriptorProto& proto,
2478 const Descriptor* parent,
2479 FieldDescriptor* result) {
2480 BuildFieldOrExtension(proto, parent, result, true);
2482 void BuildExtensionRange(const DescriptorProto::ExtensionRange& proto,
2483 const Descriptor* parent,
2484 Descriptor::ExtensionRange* result);
2485 void BuildOneof(const OneofDescriptorProto& proto,
2487 OneofDescriptor* result);
2488 void BuildEnum(const EnumDescriptorProto& proto,
2489 const Descriptor* parent,
2490 EnumDescriptor* result);
2491 void BuildEnumValue(const EnumValueDescriptorProto& proto,
2492 const EnumDescriptor* parent,
2493 EnumValueDescriptor* result);
2494 void BuildService(const ServiceDescriptorProto& proto,
2496 ServiceDescriptor* result);
2497 void BuildMethod(const MethodDescriptorProto& proto,
2498 const ServiceDescriptor* parent,
2499 MethodDescriptor* result);
2501 void LogUnusedDependency(const FileDescriptor* result);
2503 // Must be run only after building.
2505 // NOTE: Options will not be available during cross-linking, as they
2506 // have not yet been interpreted. Defer any handling of options to the
2507 // Validate*Options methods.
2508 void CrossLinkFile(FileDescriptor* file, const FileDescriptorProto& proto);
2509 void CrossLinkMessage(Descriptor* message, const DescriptorProto& proto);
2510 void CrossLinkField(FieldDescriptor* field,
2511 const FieldDescriptorProto& proto);
2512 void CrossLinkEnum(EnumDescriptor* enum_type,
2513 const EnumDescriptorProto& proto);
2514 void CrossLinkEnumValue(EnumValueDescriptor* enum_value,
2515 const EnumValueDescriptorProto& proto);
2516 void CrossLinkService(ServiceDescriptor* service,
2517 const ServiceDescriptorProto& proto);
2518 void CrossLinkMethod(MethodDescriptor* method,
2519 const MethodDescriptorProto& proto);
2521 // Must be run only after cross-linking.
2522 void InterpretOptions();
2524 // A helper class for interpreting options.
2525 class OptionInterpreter {
2527 // Creates an interpreter that operates in the context of the pool of the
2528 // specified builder, which must not be NULL. We don't take ownership of the
2530 explicit OptionInterpreter(DescriptorBuilder* builder);
2532 ~OptionInterpreter();
2534 // Interprets the uninterpreted options in the specified Options message.
2535 // On error, calls AddError() on the underlying builder and returns false.
2536 // Otherwise returns true.
2537 bool InterpretOptions(OptionsToInterpret* options_to_interpret);
2539 class AggregateOptionFinder;
2542 // Interprets uninterpreted_option_ on the specified message, which
2543 // must be the mutable copy of the original options message to which
2544 // uninterpreted_option_ belongs.
2545 bool InterpretSingleOption(Message* options);
2547 // Adds the uninterpreted_option to the given options message verbatim.
2548 // Used when AllowUnknownDependencies() is in effect and we can't find
2549 // the option's definition.
2550 void AddWithoutInterpreting(const UninterpretedOption& uninterpreted_option,
2553 // A recursive helper function that drills into the intermediate fields
2554 // in unknown_fields to check if field innermost_field is set on the
2555 // innermost message. Returns false and sets an error if so.
2556 bool ExamineIfOptionIsSet(
2557 vector<const FieldDescriptor*>::const_iterator intermediate_fields_iter,
2558 vector<const FieldDescriptor*>::const_iterator intermediate_fields_end,
2559 const FieldDescriptor* innermost_field, const string& debug_msg_name,
2560 const UnknownFieldSet& unknown_fields);
2562 // Validates the value for the option field of the currently interpreted
2563 // option and then sets it on the unknown_field.
2564 bool SetOptionValue(const FieldDescriptor* option_field,
2565 UnknownFieldSet* unknown_fields);
2567 // Parses an aggregate value for a CPPTYPE_MESSAGE option and
2568 // saves it into *unknown_fields.
2569 bool SetAggregateOption(const FieldDescriptor* option_field,
2570 UnknownFieldSet* unknown_fields);
2572 // Convenience functions to set an int field the right way, depending on
2573 // its wire type (a single int CppType can represent multiple wire types).
2574 void SetInt32(int number, int32 value, FieldDescriptor::Type type,
2575 UnknownFieldSet* unknown_fields);
2576 void SetInt64(int number, int64 value, FieldDescriptor::Type type,
2577 UnknownFieldSet* unknown_fields);
2578 void SetUInt32(int number, uint32 value, FieldDescriptor::Type type,
2579 UnknownFieldSet* unknown_fields);
2580 void SetUInt64(int number, uint64 value, FieldDescriptor::Type type,
2581 UnknownFieldSet* unknown_fields);
2583 // A helper function that adds an error at the specified location of the
2584 // option we're currently interpreting, and returns false.
2585 bool AddOptionError(DescriptorPool::ErrorCollector::ErrorLocation location,
2586 const string& msg) {
2587 builder_->AddError(options_to_interpret_->element_name,
2588 *uninterpreted_option_, location, msg);
2592 // A helper function that adds an error at the location of the option name
2593 // and returns false.
2594 bool AddNameError(const string& msg) {
2595 return AddOptionError(DescriptorPool::ErrorCollector::OPTION_NAME, msg);
2598 // A helper function that adds an error at the location of the option name
2599 // and returns false.
2600 bool AddValueError(const string& msg) {
2601 return AddOptionError(DescriptorPool::ErrorCollector::OPTION_VALUE, msg);
2604 // We interpret against this builder's pool. Is never NULL. We don't own
2606 DescriptorBuilder* builder_;
2608 // The options we're currently interpreting, or NULL if we're not in a call
2609 // to InterpretOptions.
2610 const OptionsToInterpret* options_to_interpret_;
2612 // The option we're currently interpreting within options_to_interpret_, or
2613 // NULL if we're not in a call to InterpretOptions(). This points to a
2614 // submessage of the original option, not the mutable copy. Therefore we
2615 // can use it to find locations recorded by the parser.
2616 const UninterpretedOption* uninterpreted_option_;
2618 // Factory used to create the dynamic messages we need to parse
2619 // any aggregate option values we encounter.
2620 DynamicMessageFactory dynamic_factory_;
2622 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(OptionInterpreter);
2625 // Work-around for broken compilers: According to the C++ standard,
2626 // OptionInterpreter should have access to the private members of any class
2627 // which has declared DescriptorBuilder as a friend. Unfortunately some old
2628 // versions of GCC and other compilers do not implement this correctly. So,
2629 // we have to have these intermediate methods to provide access. We also
2630 // redundantly declare OptionInterpreter a friend just to make things extra
2631 // clear for these bad compilers.
2632 friend class OptionInterpreter;
2633 friend class OptionInterpreter::AggregateOptionFinder;
2635 static inline bool get_allow_unknown(const DescriptorPool* pool) {
2636 return pool->allow_unknown_;
2638 static inline bool get_enforce_weak(const DescriptorPool* pool) {
2639 return pool->enforce_weak_;
2641 static inline bool get_is_placeholder(const Descriptor* descriptor) {
2642 return descriptor->is_placeholder_;
2644 static inline void assert_mutex_held(const DescriptorPool* pool) {
2645 if (pool->mutex_ != NULL) {
2646 pool->mutex_->AssertHeld();
2650 // Must be run only after options have been interpreted.
2652 // NOTE: Validation code must only reference the options in the mutable
2653 // descriptors, which are the ones that have been interpreted. The const
2654 // proto references are passed in only so they can be provided to calls to
2655 // AddError(). Do not look at their options, which have not been interpreted.
2656 void ValidateFileOptions(FileDescriptor* file,
2657 const FileDescriptorProto& proto);
2658 void ValidateMessageOptions(Descriptor* message,
2659 const DescriptorProto& proto);
2660 void ValidateFieldOptions(FieldDescriptor* field,
2661 const FieldDescriptorProto& proto);
2662 void ValidateEnumOptions(EnumDescriptor* enm,
2663 const EnumDescriptorProto& proto);
2664 void ValidateEnumValueOptions(EnumValueDescriptor* enum_value,
2665 const EnumValueDescriptorProto& proto);
2666 void ValidateServiceOptions(ServiceDescriptor* service,
2667 const ServiceDescriptorProto& proto);
2668 void ValidateMethodOptions(MethodDescriptor* method,
2669 const MethodDescriptorProto& proto);
2671 void ValidateMapKey(FieldDescriptor* field,
2672 const FieldDescriptorProto& proto);
2676 const FileDescriptor* DescriptorPool::BuildFile(
2677 const FileDescriptorProto& proto) {
2678 GOOGLE_CHECK(fallback_database_ == NULL)
2679 << "Cannot call BuildFile on a DescriptorPool that uses a "
2680 "DescriptorDatabase. You must instead find a way to get your file "
2681 "into the underlying database.";
2682 GOOGLE_CHECK(mutex_ == NULL); // Implied by the above GOOGLE_CHECK.
2683 tables_->known_bad_symbols_.clear();
2684 tables_->known_bad_files_.clear();
2685 return DescriptorBuilder(this, tables_.get(), NULL).BuildFile(proto);
2688 const FileDescriptor* DescriptorPool::BuildFileCollectingErrors(
2689 const FileDescriptorProto& proto,
2690 ErrorCollector* error_collector) {
2691 GOOGLE_CHECK(fallback_database_ == NULL)
2692 << "Cannot call BuildFile on a DescriptorPool that uses a "
2693 "DescriptorDatabase. You must instead find a way to get your file "
2694 "into the underlying database.";
2695 GOOGLE_CHECK(mutex_ == NULL); // Implied by the above GOOGLE_CHECK.
2696 tables_->known_bad_symbols_.clear();
2697 tables_->known_bad_files_.clear();
2698 return DescriptorBuilder(this, tables_.get(),
2699 error_collector).BuildFile(proto);
2702 const FileDescriptor* DescriptorPool::BuildFileFromDatabase(
2703 const FileDescriptorProto& proto) const {
2704 mutex_->AssertHeld();
2705 if (tables_->known_bad_files_.count(proto.name()) > 0) {
2708 const FileDescriptor* result =
2709 DescriptorBuilder(this, tables_.get(),
2710 default_error_collector_).BuildFile(proto);
2711 if (result == NULL) {
2712 tables_->known_bad_files_.insert(proto.name());
2717 DescriptorBuilder::DescriptorBuilder(
2718 const DescriptorPool* pool,
2719 DescriptorPool::Tables* tables,
2720 DescriptorPool::ErrorCollector* error_collector)
2723 error_collector_(error_collector),
2725 possible_undeclared_dependency_(NULL),
2726 undefine_resolved_name_("") {}
2728 DescriptorBuilder::~DescriptorBuilder() {}
2730 void DescriptorBuilder::AddError(
2731 const string& element_name,
2732 const Message& descriptor,
2733 DescriptorPool::ErrorCollector::ErrorLocation location,
2734 const string& error) {
2735 if (error_collector_ == NULL) {
2737 GOOGLE_LOG(ERROR) << "Invalid proto descriptor for file \"" << filename_
2740 GOOGLE_LOG(ERROR) << " " << element_name << ": " << error;
2742 error_collector_->AddError(filename_, element_name,
2743 &descriptor, location, error);
2748 void DescriptorBuilder::AddError(
2749 const string& element_name,
2750 const Message& descriptor,
2751 DescriptorPool::ErrorCollector::ErrorLocation location,
2752 const char* error) {
2753 AddError(element_name, descriptor, location, string(error));
2756 void DescriptorBuilder::AddNotDefinedError(
2757 const string& element_name,
2758 const Message& descriptor,
2759 DescriptorPool::ErrorCollector::ErrorLocation location,
2760 const string& undefined_symbol) {
2761 if (possible_undeclared_dependency_ == NULL &&
2762 undefine_resolved_name_.empty()) {
2763 AddError(element_name, descriptor, location,
2764 "\"" + undefined_symbol + "\" is not defined.");
2766 if (possible_undeclared_dependency_ != NULL) {
2767 AddError(element_name, descriptor, location,
2768 "\"" + possible_undeclared_dependency_name_ +
2769 "\" seems to be defined in \"" +
2770 possible_undeclared_dependency_->name() + "\", which is not "
2771 "imported by \"" + filename_ + "\". To use it here, please "
2772 "add the necessary import.");
2774 if (!undefine_resolved_name_.empty()) {
2775 AddError(element_name, descriptor, location,
2776 "\"" + undefined_symbol + "\" is resolved to \"" +
2777 undefine_resolved_name_ + "\", which is not defined. "
2778 "The innermost scope is searched first in name resolution. "
2779 "Consider using a leading '.'(i.e., \"."
2780 + undefined_symbol +
2781 "\") to start from the outermost scope.");
2786 void DescriptorBuilder::AddWarning(
2787 const string& element_name, const Message& descriptor,
2788 DescriptorPool::ErrorCollector::ErrorLocation location,
2789 const string& error) {
2790 if (error_collector_ == NULL) {
2791 GOOGLE_LOG(WARNING) << filename_ << " " << element_name << ": " << error;
2793 error_collector_->AddWarning(filename_, element_name, &descriptor, location,
2798 bool DescriptorBuilder::IsInPackage(const FileDescriptor* file,
2799 const string& package_name) {
2800 return HasPrefixString(file->package(), package_name) &&
2801 (file->package().size() == package_name.size() ||
2802 file->package()[package_name.size()] == '.');
2805 void DescriptorBuilder::RecordPublicDependencies(const FileDescriptor* file) {
2806 if (file == NULL || !dependencies_.insert(file).second) return;
2807 for (int i = 0; file != NULL && i < file->public_dependency_count(); i++) {
2808 RecordPublicDependencies(file->public_dependency(i));
2812 Symbol DescriptorBuilder::FindSymbolNotEnforcingDepsHelper(
2813 const DescriptorPool* pool, const string& name) {
2814 // If we are looking at an underlay, we must lock its mutex_, since we are
2815 // accessing the underlay's tables_ directly.
2816 MutexLockMaybe lock((pool == pool_) ? NULL : pool->mutex_);
2818 Symbol result = pool->tables_->FindSymbol(name);
2819 if (result.IsNull() && pool->underlay_ != NULL) {
2820 // Symbol not found; check the underlay.
2821 result = FindSymbolNotEnforcingDepsHelper(pool->underlay_, name);
2824 if (result.IsNull()) {
2825 // In theory, we shouldn't need to check fallback_database_ because the
2826 // symbol should be in one of its file's direct dependencies, and we have
2827 // already loaded those by the time we get here. But we check anyway so
2828 // that we can generate better error message when dependencies are missing
2829 // (i.e., "missing dependency" rather than "type is not defined").
2830 if (pool->TryFindSymbolInFallbackDatabase(name)) {
2831 result = pool->tables_->FindSymbol(name);
2838 Symbol DescriptorBuilder::FindSymbolNotEnforcingDeps(const string& name) {
2839 return FindSymbolNotEnforcingDepsHelper(pool_, name);
2842 Symbol DescriptorBuilder::FindSymbol(const string& name) {
2843 Symbol result = FindSymbolNotEnforcingDeps(name);
2845 if (result.IsNull()) return result;
2847 if (!pool_->enforce_dependencies_) {
2848 // Hack for CompilerUpgrader.
2852 // Only find symbols which were defined in this file or one of its
2854 const FileDescriptor* file = result.GetFile();
2855 if (file == file_ || dependencies_.count(file) > 0) {
2856 unused_dependency_.erase(file);
2860 if (result.type == Symbol::PACKAGE) {
2861 // Arg, this is overcomplicated. The symbol is a package name. It could
2862 // be that the package was defined in multiple files. result.GetFile()
2863 // returns the first file we saw that used this package. We've determined
2864 // that that file is not a direct dependency of the file we are currently
2865 // building, but it could be that some other file which *is* a direct
2866 // dependency also defines the same package. We can't really rule out this
2867 // symbol unless none of the dependencies define it.
2868 if (IsInPackage(file_, name)) return result;
2869 for (set<const FileDescriptor*>::const_iterator it = dependencies_.begin();
2870 it != dependencies_.end(); ++it) {
2871 // Note: A dependency may be NULL if it was not found or had errors.
2872 if (*it != NULL && IsInPackage(*it, name)) return result;
2876 possible_undeclared_dependency_ = file;
2877 possible_undeclared_dependency_name_ = name;
2881 Symbol DescriptorBuilder::LookupSymbolNoPlaceholder(
2882 const string& name, const string& relative_to, ResolveMode resolve_mode) {
2883 possible_undeclared_dependency_ = NULL;
2884 undefine_resolved_name_.clear();
2886 if (name.size() > 0 && name[0] == '.') {
2887 // Fully-qualified name.
2888 return FindSymbol(name.substr(1));
2891 // If name is something like "Foo.Bar.baz", and symbols named "Foo" are
2892 // defined in multiple parent scopes, we only want to find "Bar.baz" in the
2893 // innermost one. E.g., the following should produce an error:
2894 // message Bar { message Baz {} }
2898 // optional Bar.Baz baz = 1;
2900 // So, we look for just "Foo" first, then look for "Bar.baz" within it if
2902 string::size_type name_dot_pos = name.find_first_of('.');
2903 string first_part_of_name;
2904 if (name_dot_pos == string::npos) {
2905 first_part_of_name = name;
2907 first_part_of_name = name.substr(0, name_dot_pos);
2910 string scope_to_try(relative_to);
2913 // Chop off the last component of the scope.
2914 string::size_type dot_pos = scope_to_try.find_last_of('.');
2915 if (dot_pos == string::npos) {
2916 return FindSymbol(name);
2918 scope_to_try.erase(dot_pos);
2921 // Append ".first_part_of_name" and try to find.
2922 string::size_type old_size = scope_to_try.size();
2923 scope_to_try.append(1, '.');
2924 scope_to_try.append(first_part_of_name);
2925 Symbol result = FindSymbol(scope_to_try);
2926 if (!result.IsNull()) {
2927 if (first_part_of_name.size() < name.size()) {
2928 // name is a compound symbol, of which we only found the first part.
2929 // Now try to look up the rest of it.
2930 if (result.IsAggregate()) {
2931 scope_to_try.append(name, first_part_of_name.size(),
2932 name.size() - first_part_of_name.size());
2933 result = FindSymbol(scope_to_try);
2934 if (result.IsNull()) {
2935 undefine_resolved_name_ = scope_to_try;
2939 // We found a symbol but it's not an aggregate. Continue the loop.
2942 if (resolve_mode == LOOKUP_TYPES && !result.IsType()) {
2943 // We found a symbol but it's not a type. Continue the loop.
2950 // Not found. Remove the name so we can try again.
2951 scope_to_try.erase(old_size);
2955 Symbol DescriptorBuilder::LookupSymbol(
2956 const string& name, const string& relative_to,
2957 PlaceholderType placeholder_type, ResolveMode resolve_mode) {
2958 Symbol result = LookupSymbolNoPlaceholder(
2959 name, relative_to, resolve_mode);
2960 if (result.IsNull() && pool_->allow_unknown_) {
2961 // Not found, but AllowUnknownDependencies() is enabled. Return a
2962 // placeholder instead.
2963 result = NewPlaceholder(name, placeholder_type);
2968 Symbol DescriptorBuilder::NewPlaceholder(const string& name,
2969 PlaceholderType placeholder_type) {
2971 const string* placeholder_full_name;
2972 const string* placeholder_name;
2973 const string* placeholder_package;
2975 if (!ValidateQualifiedName(name)) return kNullSymbol;
2976 if (name[0] == '.') {
2978 placeholder_full_name = tables_->AllocateString(name.substr(1));
2980 placeholder_full_name = tables_->AllocateString(name);
2983 string::size_type dotpos = placeholder_full_name->find_last_of('.');
2984 if (dotpos != string::npos) {
2985 placeholder_package = tables_->AllocateString(
2986 placeholder_full_name->substr(0, dotpos));
2987 placeholder_name = tables_->AllocateString(
2988 placeholder_full_name->substr(dotpos + 1));
2990 placeholder_package = &internal::GetEmptyString();
2991 placeholder_name = placeholder_full_name;
2994 // Create the placeholders.
2995 FileDescriptor* placeholder_file = tables_->Allocate<FileDescriptor>();
2996 memset(placeholder_file, 0, sizeof(*placeholder_file));
2998 placeholder_file->source_code_info_ = &SourceCodeInfo::default_instance();
3000 placeholder_file->name_ =
3001 tables_->AllocateString(*placeholder_full_name + ".placeholder.proto");
3002 placeholder_file->package_ = placeholder_package;
3003 placeholder_file->pool_ = pool_;
3004 placeholder_file->options_ = &FileOptions::default_instance();
3005 placeholder_file->tables_ = &FileDescriptorTables::kEmpty;
3006 placeholder_file->is_placeholder_ = true;
3007 // All other fields are zero or NULL.
3009 if (placeholder_type == PLACEHOLDER_ENUM) {
3010 placeholder_file->enum_type_count_ = 1;
3011 placeholder_file->enum_types_ =
3012 tables_->AllocateArray<EnumDescriptor>(1);
3014 EnumDescriptor* placeholder_enum = &placeholder_file->enum_types_[0];
3015 memset(placeholder_enum, 0, sizeof(*placeholder_enum));
3017 placeholder_enum->full_name_ = placeholder_full_name;
3018 placeholder_enum->name_ = placeholder_name;
3019 placeholder_enum->file_ = placeholder_file;
3020 placeholder_enum->options_ = &EnumOptions::default_instance();
3021 placeholder_enum->is_placeholder_ = true;
3022 placeholder_enum->is_unqualified_placeholder_ = (name[0] != '.');
3024 // Enums must have at least one value.
3025 placeholder_enum->value_count_ = 1;
3026 placeholder_enum->values_ = tables_->AllocateArray<EnumValueDescriptor>(1);
3028 EnumValueDescriptor* placeholder_value = &placeholder_enum->values_[0];
3029 memset(placeholder_value, 0, sizeof(*placeholder_value));
3031 placeholder_value->name_ = tables_->AllocateString("PLACEHOLDER_VALUE");
3032 // Note that enum value names are siblings of their type, not children.
3033 placeholder_value->full_name_ =
3034 placeholder_package->empty() ? placeholder_value->name_ :
3035 tables_->AllocateString(*placeholder_package + ".PLACEHOLDER_VALUE");
3037 placeholder_value->number_ = 0;
3038 placeholder_value->type_ = placeholder_enum;
3039 placeholder_value->options_ = &EnumValueOptions::default_instance();
3041 return Symbol(placeholder_enum);
3043 placeholder_file->message_type_count_ = 1;
3044 placeholder_file->message_types_ =
3045 tables_->AllocateArray<Descriptor>(1);
3047 Descriptor* placeholder_message = &placeholder_file->message_types_[0];
3048 memset(placeholder_message, 0, sizeof(*placeholder_message));
3050 placeholder_message->full_name_ = placeholder_full_name;
3051 placeholder_message->name_ = placeholder_name;
3052 placeholder_message->file_ = placeholder_file;
3053 placeholder_message->options_ = &MessageOptions::default_instance();
3054 placeholder_message->is_placeholder_ = true;
3055 placeholder_message->is_unqualified_placeholder_ = (name[0] != '.');
3057 if (placeholder_type == PLACEHOLDER_EXTENDABLE_MESSAGE) {
3058 placeholder_message->extension_range_count_ = 1;
3059 placeholder_message->extension_ranges_ =
3060 tables_->AllocateArray<Descriptor::ExtensionRange>(1);
3061 placeholder_message->extension_ranges_->start = 1;
3062 // kMaxNumber + 1 because ExtensionRange::end is exclusive.
3063 placeholder_message->extension_ranges_->end =
3064 FieldDescriptor::kMaxNumber + 1;
3067 return Symbol(placeholder_message);
3071 const FileDescriptor* DescriptorBuilder::NewPlaceholderFile(
3072 const string& name) {
3073 FileDescriptor* placeholder = tables_->Allocate<FileDescriptor>();
3074 memset(placeholder, 0, sizeof(*placeholder));
3076 placeholder->name_ = tables_->AllocateString(name);
3077 placeholder->package_ = &internal::GetEmptyString();
3078 placeholder->pool_ = pool_;
3079 placeholder->options_ = &FileOptions::default_instance();
3080 placeholder->tables_ = &FileDescriptorTables::kEmpty;
3081 placeholder->is_placeholder_ = true;
3082 // All other fields are zero or NULL.
3087 bool DescriptorBuilder::AddSymbol(
3088 const string& full_name, const void* parent, const string& name,
3089 const Message& proto, Symbol symbol) {
3090 // If the caller passed NULL for the parent, the symbol is at file scope.
3091 // Use its file as the parent instead.
3092 if (parent == NULL) parent = file_;
3094 if (tables_->AddSymbol(full_name, symbol)) {
3095 if (!file_tables_->AddAliasUnderParent(parent, name, symbol)) {
3096 GOOGLE_LOG(DFATAL) << "\"" << full_name << "\" not previously defined in "
3097 "symbols_by_name_, but was defined in symbols_by_parent_; "
3098 "this shouldn't be possible.";
3103 const FileDescriptor* other_file = tables_->FindSymbol(full_name).GetFile();
3104 if (other_file == file_) {
3105 string::size_type dot_pos = full_name.find_last_of('.');
3106 if (dot_pos == string::npos) {
3107 AddError(full_name, proto, DescriptorPool::ErrorCollector::NAME,
3108 "\"" + full_name + "\" is already defined.");
3110 AddError(full_name, proto, DescriptorPool::ErrorCollector::NAME,
3111 "\"" + full_name.substr(dot_pos + 1) +
3112 "\" is already defined in \"" +
3113 full_name.substr(0, dot_pos) + "\".");
3116 // Symbol seems to have been defined in a different file.
3117 AddError(full_name, proto, DescriptorPool::ErrorCollector::NAME,
3118 "\"" + full_name + "\" is already defined in file \"" +
3119 other_file->name() + "\".");
3125 void DescriptorBuilder::AddPackage(
3126 const string& name, const Message& proto, const FileDescriptor* file) {
3127 if (tables_->AddSymbol(name, Symbol(file))) {
3128 // Success. Also add parent package, if any.
3129 string::size_type dot_pos = name.find_last_of('.');
3130 if (dot_pos == string::npos) {
3132 ValidateSymbolName(name, name, proto);
3135 string* parent_name = tables_->AllocateString(name.substr(0, dot_pos));
3136 AddPackage(*parent_name, proto, file);
3137 ValidateSymbolName(name.substr(dot_pos + 1), name, proto);
3140 Symbol existing_symbol = tables_->FindSymbol(name);
3141 // It's OK to redefine a package.
3142 if (existing_symbol.type != Symbol::PACKAGE) {
3143 // Symbol seems to have been defined in a different file.
3144 AddError(name, proto, DescriptorPool::ErrorCollector::NAME,
3145 "\"" + name + "\" is already defined (as something other than "
3146 "a package) in file \"" + existing_symbol.GetFile()->name() +
3152 void DescriptorBuilder::ValidateSymbolName(
3153 const string& name, const string& full_name, const Message& proto) {
3155 AddError(full_name, proto, DescriptorPool::ErrorCollector::NAME,
3158 for (int i = 0; i < name.size(); i++) {
3159 // I don't trust isalnum() due to locales. :(
3160 if ((name[i] < 'a' || 'z' < name[i]) &&
3161 (name[i] < 'A' || 'Z' < name[i]) &&
3162 (name[i] < '0' || '9' < name[i]) &&
3164 AddError(full_name, proto, DescriptorPool::ErrorCollector::NAME,
3165 "\"" + name + "\" is not a valid identifier.");
3171 bool DescriptorBuilder::ValidateQualifiedName(const string& name) {
3172 bool last_was_period = false;
3174 for (int i = 0; i < name.size(); i++) {
3175 // I don't trust isalnum() due to locales. :(
3176 if (('a' <= name[i] && name[i] <= 'z') ||
3177 ('A' <= name[i] && name[i] <= 'Z') ||
3178 ('0' <= name[i] && name[i] <= '9') ||
3180 last_was_period = false;
3181 } else if (name[i] == '.') {
3182 if (last_was_period) return false;
3183 last_was_period = true;
3189 return !name.empty() && !last_was_period;
3192 // -------------------------------------------------------------------
3194 // This generic implementation is good for all descriptors except
3196 template<class DescriptorT> void DescriptorBuilder::AllocateOptions(
3197 const typename DescriptorT::OptionsType& orig_options,
3198 DescriptorT* descriptor) {
3199 AllocateOptionsImpl(descriptor->full_name(), descriptor->full_name(),
3200 orig_options, descriptor);
3203 // We specialize for FileDescriptor.
3204 void DescriptorBuilder::AllocateOptions(const FileOptions& orig_options,
3205 FileDescriptor* descriptor) {
3206 // We add the dummy token so that LookupSymbol does the right thing.
3207 AllocateOptionsImpl(descriptor->package() + ".dummy", descriptor->name(),
3208 orig_options, descriptor);
3211 template<class DescriptorT> void DescriptorBuilder::AllocateOptionsImpl(
3212 const string& name_scope,
3213 const string& element_name,
3214 const typename DescriptorT::OptionsType& orig_options,
3215 DescriptorT* descriptor) {
3216 // We need to use a dummy pointer to work around a bug in older versions of
3217 // GCC. Otherwise, the following two lines could be replaced with:
3218 // typename DescriptorT::OptionsType* options =
3219 // tables_->AllocateMessage<typename DescriptorT::OptionsType>();
3220 typename DescriptorT::OptionsType* const dummy = NULL;
3221 typename DescriptorT::OptionsType* options = tables_->AllocateMessage(dummy);
3222 // Avoid using MergeFrom()/CopyFrom() in this class to make it -fno-rtti
3223 // friendly. Without RTTI, MergeFrom() and CopyFrom() will fallback to the
3224 // reflection based method, which requires the Descriptor. However, we are in
3225 // the middle of building the descriptors, thus the deadlock.
3226 options->ParseFromString(orig_options.SerializeAsString());
3227 descriptor->options_ = options;
3229 // Don't add to options_to_interpret_ unless there were uninterpreted
3230 // options. This not only avoids unnecessary work, but prevents a
3231 // bootstrapping problem when building descriptors for descriptor.proto.
3232 // descriptor.proto does not contain any uninterpreted options, but
3233 // attempting to interpret options anyway will cause
3234 // OptionsType::GetDescriptor() to be called which may then deadlock since
3235 // we're still trying to build it.
3236 if (options->uninterpreted_option_size() > 0) {
3237 options_to_interpret_.push_back(
3238 OptionsToInterpret(name_scope, element_name, &orig_options, options));
3243 // A common pattern: We want to convert a repeated field in the descriptor
3244 // to an array of values, calling some method to build each value.
3245 #define BUILD_ARRAY(INPUT, OUTPUT, NAME, METHOD, PARENT) \
3246 OUTPUT->NAME##_count_ = INPUT.NAME##_size(); \
3247 AllocateArray(INPUT.NAME##_size(), &OUTPUT->NAME##s_); \
3248 for (int i = 0; i < INPUT.NAME##_size(); i++) { \
3249 METHOD(INPUT.NAME(i), PARENT, OUTPUT->NAME##s_ + i); \
3252 void DescriptorBuilder::AddRecursiveImportError(
3253 const FileDescriptorProto& proto, int from_here) {
3254 string error_message("File recursively imports itself: ");
3255 for (int i = from_here; i < tables_->pending_files_.size(); i++) {
3256 error_message.append(tables_->pending_files_[i]);
3257 error_message.append(" -> ");
3259 error_message.append(proto.name());
3261 AddError(proto.name(), proto, DescriptorPool::ErrorCollector::OTHER,
3265 void DescriptorBuilder::AddTwiceListedError(const FileDescriptorProto& proto,
3267 AddError(proto.name(), proto, DescriptorPool::ErrorCollector::OTHER,
3268 "Import \"" + proto.dependency(index) + "\" was listed twice.");
3271 void DescriptorBuilder::AddImportError(const FileDescriptorProto& proto,
3274 if (pool_->fallback_database_ == NULL) {
3275 message = "Import \"" + proto.dependency(index) +
3276 "\" has not been loaded.";
3278 message = "Import \"" + proto.dependency(index) +
3279 "\" was not found or had errors.";
3281 AddError(proto.name(), proto, DescriptorPool::ErrorCollector::OTHER, message);
3284 static bool ExistingFileMatchesProto(const FileDescriptor* existing_file,
3285 const FileDescriptorProto& proto) {
3286 FileDescriptorProto existing_proto;
3287 existing_file->CopyTo(&existing_proto);
3288 return existing_proto.SerializeAsString() == proto.SerializeAsString();
3291 const FileDescriptor* DescriptorBuilder::BuildFile(
3292 const FileDescriptorProto& proto) {
3293 filename_ = proto.name();
3295 // Check if the file already exists and is identical to the one being built.
3296 // Note: This only works if the input is canonical -- that is, it
3297 // fully-qualifies all type names, has no UninterpretedOptions, etc.
3298 // This is fine, because this idempotency "feature" really only exists to
3299 // accomodate one hack in the proto1->proto2 migration layer.
3300 const FileDescriptor* existing_file = tables_->FindFile(filename_);
3301 if (existing_file != NULL) {
3302 // File already in pool. Compare the existing one to the input.
3303 if (ExistingFileMatchesProto(existing_file, proto)) {
3304 // They're identical. Return the existing descriptor.
3305 return existing_file;
3308 // Not a match. The error will be detected and handled later.
3311 // Check to see if this file is already on the pending files list.
3312 // TODO(kenton): Allow recursive imports? It may not work with some
3313 // (most?) programming languages. E.g., in C++, a forward declaration
3314 // of a type is not sufficient to allow it to be used even in a
3315 // generated header file due to inlining. This could perhaps be
3316 // worked around using tricks involving inserting #include statements
3317 // mid-file, but that's pretty ugly, and I'm pretty sure there are
3318 // some languages out there that do not allow recursive dependencies
3320 for (int i = 0; i < tables_->pending_files_.size(); i++) {
3321 if (tables_->pending_files_[i] == proto.name()) {
3322 AddRecursiveImportError(proto, i);
3327 // If we have a fallback_database_, attempt to load all dependencies now,
3328 // before checkpointing tables_. This avoids confusion with recursive
3330 if (pool_->fallback_database_ != NULL) {
3331 tables_->pending_files_.push_back(proto.name());
3332 for (int i = 0; i < proto.dependency_size(); i++) {
3333 if (tables_->FindFile(proto.dependency(i)) == NULL &&
3334 (pool_->underlay_ == NULL ||
3335 pool_->underlay_->FindFileByName(proto.dependency(i)) == NULL)) {
3336 // We don't care what this returns since we'll find out below anyway.
3337 pool_->TryFindFileInFallbackDatabase(proto.dependency(i));
3340 tables_->pending_files_.pop_back();
3343 // Checkpoint the tables so that we can roll back if something goes wrong.
3344 tables_->AddCheckpoint();
3346 FileDescriptor* result = tables_->Allocate<FileDescriptor>();
3349 result->is_placeholder_ = false;
3350 if (proto.has_source_code_info()) {
3351 SourceCodeInfo *info = tables_->AllocateMessage<SourceCodeInfo>();
3352 info->CopyFrom(proto.source_code_info());
3353 result->source_code_info_ = info;
3355 result->source_code_info_ = &SourceCodeInfo::default_instance();
3358 file_tables_ = tables_->AllocateFileTables();
3359 file_->tables_ = file_tables_;
3361 if (!proto.has_name()) {
3362 AddError("", proto, DescriptorPool::ErrorCollector::OTHER,
3363 "Missing field: FileDescriptorProto.name.");
3366 result->name_ = tables_->AllocateString(proto.name());
3367 if (proto.has_package()) {
3368 result->package_ = tables_->AllocateString(proto.package());
3370 // We cannot rely on proto.package() returning a valid string if
3371 // proto.has_package() is false, because we might be running at static
3372 // initialization time, in which case default values have not yet been
3374 result->package_ = tables_->AllocateString("");
3376 result->pool_ = pool_;
3379 if (!tables_->AddFile(result)) {
3380 AddError(proto.name(), proto, DescriptorPool::ErrorCollector::OTHER,
3381 "A file with this name is already in the pool.");
3382 // Bail out early so that if this is actually the exact same file, we
3383 // don't end up reporting that every single symbol is already defined.
3384 tables_->RollbackToLastCheckpoint();
3387 if (!result->package().empty()) {
3388 AddPackage(result->package(), proto, result);
3391 // Make sure all dependencies are loaded.
3392 set<string> seen_dependencies;
3393 result->dependency_count_ = proto.dependency_size();
3394 result->dependencies_ =
3395 tables_->AllocateArray<const FileDescriptor*>(proto.dependency_size());
3396 unused_dependency_.clear();
3398 for (int i = 0; i < proto.weak_dependency_size(); ++i) {
3399 weak_deps.insert(proto.weak_dependency(i));
3401 for (int i = 0; i < proto.dependency_size(); i++) {
3402 if (!seen_dependencies.insert(proto.dependency(i)).second) {
3403 AddTwiceListedError(proto, i);
3406 const FileDescriptor* dependency = tables_->FindFile(proto.dependency(i));
3407 if (dependency == NULL && pool_->underlay_ != NULL) {
3408 dependency = pool_->underlay_->FindFileByName(proto.dependency(i));
3411 if (dependency == NULL) {
3412 if (pool_->allow_unknown_ ||
3413 (!pool_->enforce_weak_ && weak_deps.find(i) != weak_deps.end())) {
3414 dependency = NewPlaceholderFile(proto.dependency(i));
3416 AddImportError(proto, i);
3419 // Add to unused_dependency_ to track unused imported files.
3420 // Note: do not track unused imported files for public import.
3421 if (pool_->enforce_dependencies_ &&
3422 (pool_->unused_import_track_files_.find(proto.name()) !=
3423 pool_->unused_import_track_files_.end()) &&
3424 (dependency->public_dependency_count() == 0)) {
3425 unused_dependency_.insert(dependency);
3429 result->dependencies_[i] = dependency;
3432 // Check public dependencies.
3433 int public_dependency_count = 0;
3434 result->public_dependencies_ = tables_->AllocateArray<int>(
3435 proto.public_dependency_size());
3436 for (int i = 0; i < proto.public_dependency_size(); i++) {
3437 // Only put valid public dependency indexes.
3438 int index = proto.public_dependency(i);
3439 if (index >= 0 && index < proto.dependency_size()) {
3440 result->public_dependencies_[public_dependency_count++] = index;
3441 // Do not track unused imported files for public import.
3442 unused_dependency_.erase(result->dependency(index));
3444 AddError(proto.name(), proto,
3445 DescriptorPool::ErrorCollector::OTHER,
3446 "Invalid public dependency index.");
3449 result->public_dependency_count_ = public_dependency_count;
3451 // Build dependency set
3452 dependencies_.clear();
3453 for (int i = 0; i < result->dependency_count(); i++) {
3454 RecordPublicDependencies(result->dependency(i));
3457 // Check weak dependencies.
3458 int weak_dependency_count = 0;
3459 result->weak_dependencies_ = tables_->AllocateArray<int>(
3460 proto.weak_dependency_size());
3461 for (int i = 0; i < proto.weak_dependency_size(); i++) {
3462 int index = proto.weak_dependency(i);
3463 if (index >= 0 && index < proto.dependency_size()) {
3464 result->weak_dependencies_[weak_dependency_count++] = index;
3466 AddError(proto.name(), proto,
3467 DescriptorPool::ErrorCollector::OTHER,
3468 "Invalid weak dependency index.");
3471 result->weak_dependency_count_ = weak_dependency_count;
3473 // Convert children.
3474 BUILD_ARRAY(proto, result, message_type, BuildMessage , NULL);
3475 BUILD_ARRAY(proto, result, enum_type , BuildEnum , NULL);
3476 BUILD_ARRAY(proto, result, service , BuildService , NULL);
3477 BUILD_ARRAY(proto, result, extension , BuildExtension, NULL);
3480 if (!proto.has_options()) {
3481 result->options_ = NULL; // Will set to default_instance later.
3483 AllocateOptions(proto.options(), result);
3486 // Note that the following steps must occur in exactly the specified order.
3489 CrossLinkFile(result, proto);
3491 // Interpret any remaining uninterpreted options gathered into
3492 // options_to_interpret_ during descriptor building. Cross-linking has made
3493 // extension options known, so all interpretations should now succeed.
3495 OptionInterpreter option_interpreter(this);
3496 for (vector<OptionsToInterpret>::iterator iter =
3497 options_to_interpret_.begin();
3498 iter != options_to_interpret_.end(); ++iter) {
3499 option_interpreter.InterpretOptions(&(*iter));
3501 options_to_interpret_.clear();
3504 // Validate options.
3506 ValidateFileOptions(result, proto);
3510 if (!unused_dependency_.empty()) {
3511 LogUnusedDependency(result);
3515 tables_->RollbackToLastCheckpoint();
3518 tables_->ClearLastCheckpoint();
3523 void DescriptorBuilder::BuildMessage(const DescriptorProto& proto,
3524 const Descriptor* parent,
3525 Descriptor* result) {
3526 const string& scope = (parent == NULL) ?
3527 file_->package() : parent->full_name();
3528 string* full_name = tables_->AllocateString(scope);
3529 if (!full_name->empty()) full_name->append(1, '.');
3530 full_name->append(proto.name());
3532 ValidateSymbolName(proto.name(), *full_name, proto);
3534 result->name_ = tables_->AllocateString(proto.name());
3535 result->full_name_ = full_name;
3536 result->file_ = file_;
3537 result->containing_type_ = parent;
3538 result->is_placeholder_ = false;
3539 result->is_unqualified_placeholder_ = false;
3541 // Build oneofs first so that fields and extension ranges can refer to them.
3542 BUILD_ARRAY(proto, result, oneof_decl , BuildOneof , result);
3543 BUILD_ARRAY(proto, result, field , BuildField , result);
3544 BUILD_ARRAY(proto, result, nested_type , BuildMessage , result);
3545 BUILD_ARRAY(proto, result, enum_type , BuildEnum , result);
3546 BUILD_ARRAY(proto, result, extension_range, BuildExtensionRange, result);
3547 BUILD_ARRAY(proto, result, extension , BuildExtension , result);
3550 if (!proto.has_options()) {
3551 result->options_ = NULL; // Will set to default_instance later.
3553 AllocateOptions(proto.options(), result);
3556 AddSymbol(result->full_name(), parent, result->name(),
3557 proto, Symbol(result));
3559 // Check that no fields have numbers in extension ranges.
3560 for (int i = 0; i < result->field_count(); i++) {
3561 const FieldDescriptor* field = result->field(i);
3562 for (int j = 0; j < result->extension_range_count(); j++) {
3563 const Descriptor::ExtensionRange* range = result->extension_range(j);
3564 if (range->start <= field->number() && field->number() < range->end) {
3565 AddError(field->full_name(), proto.extension_range(j),
3566 DescriptorPool::ErrorCollector::NUMBER,
3567 strings::Substitute(
3568 "Extension range $0 to $1 includes field \"$2\" ($3).",
3569 range->start, range->end - 1,
3570 field->name(), field->number()));
3575 // Check that extension ranges don't overlap.
3576 for (int i = 0; i < result->extension_range_count(); i++) {
3577 const Descriptor::ExtensionRange* range1 = result->extension_range(i);
3578 for (int j = i + 1; j < result->extension_range_count(); j++) {
3579 const Descriptor::ExtensionRange* range2 = result->extension_range(j);
3580 if (range1->end > range2->start && range2->end > range1->start) {
3581 AddError(result->full_name(), proto.extension_range(j),
3582 DescriptorPool::ErrorCollector::NUMBER,
3583 strings::Substitute("Extension range $0 to $1 overlaps with "
3584 "already-defined range $2 to $3.",
3585 range2->start, range2->end - 1,
3586 range1->start, range1->end - 1));
3592 void DescriptorBuilder::BuildFieldOrExtension(const FieldDescriptorProto& proto,
3593 const Descriptor* parent,
3594 FieldDescriptor* result,
3595 bool is_extension) {
3596 const string& scope = (parent == NULL) ?
3597 file_->package() : parent->full_name();
3598 string* full_name = tables_->AllocateString(scope);
3599 if (!full_name->empty()) full_name->append(1, '.');
3600 full_name->append(proto.name());
3602 ValidateSymbolName(proto.name(), *full_name, proto);
3604 result->name_ = tables_->AllocateString(proto.name());
3605 result->full_name_ = full_name;
3606 result->file_ = file_;
3607 result->number_ = proto.number();
3608 result->is_extension_ = is_extension;
3610 // If .proto files follow the style guide then the name should already be
3611 // lower-cased. If that's the case we can just reuse the string we already
3612 // allocated rather than allocate a new one.
3613 string lowercase_name(proto.name());
3614 LowerString(&lowercase_name);
3615 if (lowercase_name == proto.name()) {
3616 result->lowercase_name_ = result->name_;
3618 result->lowercase_name_ = tables_->AllocateString(lowercase_name);
3621 // Don't bother with the above optimization for camel-case names since
3622 // .proto files that follow the guide shouldn't be using names in this
3623 // format, so the optimization wouldn't help much.
3624 result->camelcase_name_ = tables_->AllocateString(ToCamelCase(proto.name()));
3626 // Some compilers do not allow static_cast directly between two enum types,
3627 // so we must cast to int first.
3628 result->type_ = static_cast<FieldDescriptor::Type>(
3629 implicit_cast<int>(proto.type()));
3630 result->label_ = static_cast<FieldDescriptor::Label>(
3631 implicit_cast<int>(proto.label()));
3633 // An extension cannot have a required field (b/13365836).
3634 if (result->is_extension_ &&
3635 result->label_ == FieldDescriptor::LABEL_REQUIRED) {
3636 AddError(result->full_name(), proto,
3637 // Error location `TYPE`: we would really like to indicate
3638 // `LABEL`, but the `ErrorLocation` enum has no entry for this, and
3639 // we don't necessarily know about all implementations of the
3640 // `ErrorCollector` interface to extend them to handle the new
3641 // error location type properly.
3642 DescriptorPool::ErrorCollector::TYPE,
3643 "Message extensions cannot have required fields.");
3646 // Some of these may be filled in when cross-linking.
3647 result->containing_type_ = NULL;
3648 result->extension_scope_ = NULL;
3649 result->experimental_map_key_ = NULL;
3650 result->message_type_ = NULL;
3651 result->enum_type_ = NULL;
3653 result->has_default_value_ = proto.has_default_value();
3654 if (proto.has_default_value() && result->is_repeated()) {
3655 AddError(result->full_name(), proto,
3656 DescriptorPool::ErrorCollector::DEFAULT_VALUE,
3657 "Repeated fields can't have default values.");
3660 if (proto.has_type()) {
3661 if (proto.has_default_value()) {
3662 char* end_pos = NULL;
3663 switch (result->cpp_type()) {
3664 case FieldDescriptor::CPPTYPE_INT32:
3665 result->default_value_int32_ =
3666 strtol(proto.default_value().c_str(), &end_pos, 0);
3668 case FieldDescriptor::CPPTYPE_INT64:
3669 result->default_value_int64_ =
3670 strto64(proto.default_value().c_str(), &end_pos, 0);
3672 case FieldDescriptor::CPPTYPE_UINT32:
3673 result->default_value_uint32_ =
3674 strtoul(proto.default_value().c_str(), &end_pos, 0);
3676 case FieldDescriptor::CPPTYPE_UINT64:
3677 result->default_value_uint64_ =
3678 strtou64(proto.default_value().c_str(), &end_pos, 0);
3680 case FieldDescriptor::CPPTYPE_FLOAT:
3681 if (proto.default_value() == "inf") {
3682 result->default_value_float_ = numeric_limits<float>::infinity();
3683 } else if (proto.default_value() == "-inf") {
3684 result->default_value_float_ = -numeric_limits<float>::infinity();
3685 } else if (proto.default_value() == "nan") {
3686 result->default_value_float_ = numeric_limits<float>::quiet_NaN();
3688 result->default_value_float_ =
3689 io::NoLocaleStrtod(proto.default_value().c_str(), &end_pos);
3692 case FieldDescriptor::CPPTYPE_DOUBLE:
3693 if (proto.default_value() == "inf") {
3694 result->default_value_double_ = numeric_limits<double>::infinity();
3695 } else if (proto.default_value() == "-inf") {
3696 result->default_value_double_ = -numeric_limits<double>::infinity();
3697 } else if (proto.default_value() == "nan") {
3698 result->default_value_double_ = numeric_limits<double>::quiet_NaN();
3700 result->default_value_double_ =
3701 io::NoLocaleStrtod(proto.default_value().c_str(), &end_pos);
3704 case FieldDescriptor::CPPTYPE_BOOL:
3705 if (proto.default_value() == "true") {
3706 result->default_value_bool_ = true;
3707 } else if (proto.default_value() == "false") {
3708 result->default_value_bool_ = false;
3710 AddError(result->full_name(), proto,
3711 DescriptorPool::ErrorCollector::DEFAULT_VALUE,
3712 "Boolean default must be true or false.");
3715 case FieldDescriptor::CPPTYPE_ENUM:
3716 // This will be filled in when cross-linking.
3717 result->default_value_enum_ = NULL;
3719 case FieldDescriptor::CPPTYPE_STRING:
3720 if (result->type() == FieldDescriptor::TYPE_BYTES) {
3721 result->default_value_string_ = tables_->AllocateString(
3722 UnescapeCEscapeString(proto.default_value()));
3724 result->default_value_string_ =
3725 tables_->AllocateString(proto.default_value());
3728 case FieldDescriptor::CPPTYPE_MESSAGE:
3729 AddError(result->full_name(), proto,
3730 DescriptorPool::ErrorCollector::DEFAULT_VALUE,
3731 "Messages can't have default values.");
3732 result->has_default_value_ = false;
3736 if (end_pos != NULL) {
3737 // end_pos is only set non-NULL by the parsers for numeric types, above.
3738 // This checks that the default was non-empty and had no extra junk
3739 // after the end of the number.
3740 if (proto.default_value().empty() || *end_pos != '\0') {
3741 AddError(result->full_name(), proto,
3742 DescriptorPool::ErrorCollector::DEFAULT_VALUE,
3743 "Couldn't parse default value \"" + proto.default_value() +
3748 // No explicit default value
3749 switch (result->cpp_type()) {
3750 case FieldDescriptor::CPPTYPE_INT32:
3751 result->default_value_int32_ = 0;
3753 case FieldDescriptor::CPPTYPE_INT64:
3754 result->default_value_int64_ = 0;
3756 case FieldDescriptor::CPPTYPE_UINT32:
3757 result->default_value_uint32_ = 0;
3759 case FieldDescriptor::CPPTYPE_UINT64:
3760 result->default_value_uint64_ = 0;
3762 case FieldDescriptor::CPPTYPE_FLOAT:
3763 result->default_value_float_ = 0.0f;
3765 case FieldDescriptor::CPPTYPE_DOUBLE:
3766 result->default_value_double_ = 0.0;
3768 case FieldDescriptor::CPPTYPE_BOOL:
3769 result->default_value_bool_ = false;
3771 case FieldDescriptor::CPPTYPE_ENUM:
3772 // This will be filled in when cross-linking.
3773 result->default_value_enum_ = NULL;
3775 case FieldDescriptor::CPPTYPE_STRING:
3776 result->default_value_string_ = &internal::GetEmptyString();
3778 case FieldDescriptor::CPPTYPE_MESSAGE:
3784 if (result->number() <= 0) {
3785 AddError(result->full_name(), proto, DescriptorPool::ErrorCollector::NUMBER,
3786 "Field numbers must be positive integers.");
3787 } else if (!is_extension && result->number() > FieldDescriptor::kMaxNumber) {
3788 // Only validate that the number is within the valid field range if it is
3789 // not an extension. Since extension numbers are validated with the
3790 // extendee's valid set of extension numbers, and those are in turn
3791 // validated against the max allowed number, the check is unnecessary for
3792 // extension fields.
3793 // This avoids cross-linking issues that arise when attempting to check if
3794 // the extendee is a message_set_wire_format message, which has a higher max
3795 // on extension numbers.
3796 AddError(result->full_name(), proto, DescriptorPool::ErrorCollector::NUMBER,
3797 strings::Substitute("Field numbers cannot be greater than $0.",
3798 FieldDescriptor::kMaxNumber));
3799 } else if (result->number() >= FieldDescriptor::kFirstReservedNumber &&
3800 result->number() <= FieldDescriptor::kLastReservedNumber) {
3801 AddError(result->full_name(), proto, DescriptorPool::ErrorCollector::NUMBER,
3802 strings::Substitute(
3803 "Field numbers $0 through $1 are reserved for the protocol "
3804 "buffer library implementation.",
3805 FieldDescriptor::kFirstReservedNumber,
3806 FieldDescriptor::kLastReservedNumber));
3810 if (!proto.has_extendee()) {
3811 AddError(result->full_name(), proto,
3812 DescriptorPool::ErrorCollector::EXTENDEE,
3813 "FieldDescriptorProto.extendee not set for extension field.");
3816 result->extension_scope_ = parent;
3818 if (proto.has_oneof_index()) {
3819 AddError(result->full_name(), proto,
3820 DescriptorPool::ErrorCollector::OTHER,
3821 "FieldDescriptorProto.oneof_index should not be set for "
3825 // Fill in later (maybe).
3826 result->containing_oneof_ = NULL;
3828 if (proto.has_extendee()) {
3829 AddError(result->full_name(), proto,
3830 DescriptorPool::ErrorCollector::EXTENDEE,
3831 "FieldDescriptorProto.extendee set for non-extension field.");
3834 result->containing_type_ = parent;
3836 if (proto.has_oneof_index()) {
3837 if (proto.oneof_index() < 0 ||
3838 proto.oneof_index() >= parent->oneof_decl_count()) {
3839 AddError(result->full_name(), proto,
3840 DescriptorPool::ErrorCollector::OTHER,
3841 strings::Substitute("FieldDescriptorProto.oneof_index $0 is "
3842 "out of range for type \"$1\".",
3843 proto.oneof_index(),
3845 result->containing_oneof_ = NULL;
3847 result->containing_oneof_ = parent->oneof_decl(proto.oneof_index());
3850 result->containing_oneof_ = NULL;
3855 if (!proto.has_options()) {
3856 result->options_ = NULL; // Will set to default_instance later.
3858 AllocateOptions(proto.options(), result);
3861 AddSymbol(result->full_name(), parent, result->name(),
3862 proto, Symbol(result));
3865 void DescriptorBuilder::BuildExtensionRange(
3866 const DescriptorProto::ExtensionRange& proto,
3867 const Descriptor* parent,
3868 Descriptor::ExtensionRange* result) {
3869 result->start = proto.start();
3870 result->end = proto.end();
3871 if (result->start <= 0) {
3872 AddError(parent->full_name(), proto,
3873 DescriptorPool::ErrorCollector::NUMBER,
3874 "Extension numbers must be positive integers.");
3877 // Checking of the upper bound of the extension range is deferred until after
3878 // options interpreting. This allows messages with message_set_wire_format to
3879 // have extensions beyond FieldDescriptor::kMaxNumber, since the extension
3880 // numbers are actually used as int32s in the message_set_wire_format.
3882 if (result->start >= result->end) {
3883 AddError(parent->full_name(), proto,
3884 DescriptorPool::ErrorCollector::NUMBER,
3885 "Extension range end number must be greater than start number.");
3889 void DescriptorBuilder::BuildOneof(const OneofDescriptorProto& proto,
3891 OneofDescriptor* result) {
3892 string* full_name = tables_->AllocateString(parent->full_name());
3893 full_name->append(1, '.');
3894 full_name->append(proto.name());
3896 ValidateSymbolName(proto.name(), *full_name, proto);
3898 result->name_ = tables_->AllocateString(proto.name());
3899 result->full_name_ = full_name;
3901 result->containing_type_ = parent;
3903 // We need to fill these in later.
3904 result->field_count_ = 0;
3905 result->fields_ = NULL;
3907 AddSymbol(result->full_name(), parent, result->name(),
3908 proto, Symbol(result));
3911 void DescriptorBuilder::BuildEnum(const EnumDescriptorProto& proto,
3912 const Descriptor* parent,
3913 EnumDescriptor* result) {
3914 const string& scope = (parent == NULL) ?
3915 file_->package() : parent->full_name();
3916 string* full_name = tables_->AllocateString(scope);
3917 if (!full_name->empty()) full_name->append(1, '.');
3918 full_name->append(proto.name());
3920 ValidateSymbolName(proto.name(), *full_name, proto);
3922 result->name_ = tables_->AllocateString(proto.name());
3923 result->full_name_ = full_name;
3924 result->file_ = file_;
3925 result->containing_type_ = parent;
3926 result->is_placeholder_ = false;
3927 result->is_unqualified_placeholder_ = false;
3929 if (proto.value_size() == 0) {
3930 // We cannot allow enums with no values because this would mean there
3931 // would be no valid default value for fields of this type.
3932 AddError(result->full_name(), proto,
3933 DescriptorPool::ErrorCollector::NAME,
3934 "Enums must contain at least one value.");
3937 BUILD_ARRAY(proto, result, value, BuildEnumValue, result);
3940 if (!proto.has_options()) {
3941 result->options_ = NULL; // Will set to default_instance later.
3943 AllocateOptions(proto.options(), result);
3946 AddSymbol(result->full_name(), parent, result->name(),
3947 proto, Symbol(result));
3950 void DescriptorBuilder::BuildEnumValue(const EnumValueDescriptorProto& proto,
3951 const EnumDescriptor* parent,
3952 EnumValueDescriptor* result) {
3953 result->name_ = tables_->AllocateString(proto.name());
3954 result->number_ = proto.number();
3955 result->type_ = parent;
3957 // Note: full_name for enum values is a sibling to the parent's name, not a
3959 string* full_name = tables_->AllocateString(*parent->full_name_);
3960 full_name->resize(full_name->size() - parent->name_->size());
3961 full_name->append(*result->name_);
3962 result->full_name_ = full_name;
3964 ValidateSymbolName(proto.name(), *full_name, proto);
3967 if (!proto.has_options()) {
3968 result->options_ = NULL; // Will set to default_instance later.
3970 AllocateOptions(proto.options(), result);
3973 // Again, enum values are weird because we makes them appear as siblings
3974 // of the enum type instead of children of it. So, we use
3975 // parent->containing_type() as the value's parent.
3976 bool added_to_outer_scope =
3977 AddSymbol(result->full_name(), parent->containing_type(), result->name(),
3978 proto, Symbol(result));
3980 // However, we also want to be able to search for values within a single
3981 // enum type, so we add it as a child of the enum type itself, too.
3982 // Note: This could fail, but if it does, the error has already been
3983 // reported by the above AddSymbol() call, so we ignore the return code.
3984 bool added_to_inner_scope =
3985 file_tables_->AddAliasUnderParent(parent, result->name(), Symbol(result));
3987 if (added_to_inner_scope && !added_to_outer_scope) {
3988 // This value did not conflict with any values defined in the same enum,
3989 // but it did conflict with some other symbol defined in the enum type's
3990 // scope. Let's print an additional error to explain this.
3992 if (parent->containing_type() == NULL) {
3993 outer_scope = file_->package();
3995 outer_scope = parent->containing_type()->full_name();
3998 if (outer_scope.empty()) {
3999 outer_scope = "the global scope";
4001 outer_scope = "\"" + outer_scope + "\"";
4004 AddError(result->full_name(), proto,
4005 DescriptorPool::ErrorCollector::NAME,
4006 "Note that enum values use C++ scoping rules, meaning that "
4007 "enum values are siblings of their type, not children of it. "
4008 "Therefore, \"" + result->name() + "\" must be unique within "
4009 + outer_scope + ", not just within \"" + parent->name() + "\".");
4012 // An enum is allowed to define two numbers that refer to the same value.
4013 // FindValueByNumber() should return the first such value, so we simply
4014 // ignore AddEnumValueByNumber()'s return code.
4015 file_tables_->AddEnumValueByNumber(result);
4018 void DescriptorBuilder::BuildService(const ServiceDescriptorProto& proto,
4019 const void* /* dummy */,
4020 ServiceDescriptor* result) {
4021 string* full_name = tables_->AllocateString(file_->package());
4022 if (!full_name->empty()) full_name->append(1, '.');
4023 full_name->append(proto.name());
4025 ValidateSymbolName(proto.name(), *full_name, proto);
4027 result->name_ = tables_->AllocateString(proto.name());
4028 result->full_name_ = full_name;
4029 result->file_ = file_;
4031 BUILD_ARRAY(proto, result, method, BuildMethod, result);
4034 if (!proto.has_options()) {
4035 result->options_ = NULL; // Will set to default_instance later.
4037 AllocateOptions(proto.options(), result);
4040 AddSymbol(result->full_name(), NULL, result->name(),
4041 proto, Symbol(result));
4044 void DescriptorBuilder::BuildMethod(const MethodDescriptorProto& proto,
4045 const ServiceDescriptor* parent,
4046 MethodDescriptor* result) {
4047 result->name_ = tables_->AllocateString(proto.name());
4048 result->service_ = parent;
4050 string* full_name = tables_->AllocateString(parent->full_name());
4051 full_name->append(1, '.');
4052 full_name->append(*result->name_);
4053 result->full_name_ = full_name;
4055 ValidateSymbolName(proto.name(), *full_name, proto);
4057 // These will be filled in when cross-linking.
4058 result->input_type_ = NULL;
4059 result->output_type_ = NULL;
4062 if (!proto.has_options()) {
4063 result->options_ = NULL; // Will set to default_instance later.
4065 AllocateOptions(proto.options(), result);
4068 AddSymbol(result->full_name(), parent, result->name(),
4069 proto, Symbol(result));
4074 // -------------------------------------------------------------------
4076 void DescriptorBuilder::CrossLinkFile(
4077 FileDescriptor* file, const FileDescriptorProto& proto) {
4078 if (file->options_ == NULL) {
4079 file->options_ = &FileOptions::default_instance();
4082 for (int i = 0; i < file->message_type_count(); i++) {
4083 CrossLinkMessage(&file->message_types_[i], proto.message_type(i));
4086 for (int i = 0; i < file->extension_count(); i++) {
4087 CrossLinkField(&file->extensions_[i], proto.extension(i));
4090 for (int i = 0; i < file->enum_type_count(); i++) {
4091 CrossLinkEnum(&file->enum_types_[i], proto.enum_type(i));
4094 for (int i = 0; i < file->service_count(); i++) {
4095 CrossLinkService(&file->services_[i], proto.service(i));
4099 void DescriptorBuilder::CrossLinkMessage(
4100 Descriptor* message, const DescriptorProto& proto) {
4101 if (message->options_ == NULL) {
4102 message->options_ = &MessageOptions::default_instance();
4105 for (int i = 0; i < message->nested_type_count(); i++) {
4106 CrossLinkMessage(&message->nested_types_[i], proto.nested_type(i));
4109 for (int i = 0; i < message->enum_type_count(); i++) {
4110 CrossLinkEnum(&message->enum_types_[i], proto.enum_type(i));
4113 for (int i = 0; i < message->field_count(); i++) {
4114 CrossLinkField(&message->fields_[i], proto.field(i));
4117 for (int i = 0; i < message->extension_count(); i++) {
4118 CrossLinkField(&message->extensions_[i], proto.extension(i));
4121 // Set up field array for each oneof.
4123 // First count the number of fields per oneof.
4124 for (int i = 0; i < message->field_count(); i++) {
4125 const OneofDescriptor* oneof_decl = message->field(i)->containing_oneof();
4126 if (oneof_decl != NULL) {
4127 // Must go through oneof_decls_ array to get a non-const version of the
4129 ++message->oneof_decls_[oneof_decl->index()].field_count_;
4133 // Then allocate the arrays.
4134 for (int i = 0; i < message->oneof_decl_count(); i++) {
4135 OneofDescriptor* oneof_decl = &message->oneof_decls_[i];
4137 if (oneof_decl->field_count() == 0) {
4138 AddError(message->full_name() + "." + oneof_decl->name(),
4139 proto.oneof_decl(i),
4140 DescriptorPool::ErrorCollector::NAME,
4141 "Oneof must have at least one field.");
4144 oneof_decl->fields_ =
4145 tables_->AllocateArray<const FieldDescriptor*>(oneof_decl->field_count_);
4146 oneof_decl->field_count_ = 0;
4149 // Then fill them in.
4150 for (int i = 0; i < message->field_count(); i++) {
4151 const OneofDescriptor* oneof_decl = message->field(i)->containing_oneof();
4152 if (oneof_decl != NULL) {
4153 OneofDescriptor* mutable_oneof_decl =
4154 &message->oneof_decls_[oneof_decl->index()];
4155 message->fields_[i].index_in_oneof_ = mutable_oneof_decl->field_count_;
4156 mutable_oneof_decl->fields_[mutable_oneof_decl->field_count_++] =
4162 void DescriptorBuilder::CrossLinkField(
4163 FieldDescriptor* field, const FieldDescriptorProto& proto) {
4164 if (field->options_ == NULL) {
4165 field->options_ = &FieldOptions::default_instance();
4168 if (proto.has_extendee()) {
4169 Symbol extendee = LookupSymbol(proto.extendee(), field->full_name(),
4170 PLACEHOLDER_EXTENDABLE_MESSAGE);
4171 if (extendee.IsNull()) {
4172 AddNotDefinedError(field->full_name(), proto,
4173 DescriptorPool::ErrorCollector::EXTENDEE,
4176 } else if (extendee.type != Symbol::MESSAGE) {
4177 AddError(field->full_name(), proto,
4178 DescriptorPool::ErrorCollector::EXTENDEE,
4179 "\"" + proto.extendee() + "\" is not a message type.");
4182 field->containing_type_ = extendee.descriptor;
4184 const Descriptor::ExtensionRange* extension_range = field->containing_type()
4185 ->FindExtensionRangeContainingNumber(field->number());
4187 if (extension_range == NULL) {
4188 AddError(field->full_name(), proto,
4189 DescriptorPool::ErrorCollector::NUMBER,
4190 strings::Substitute("\"$0\" does not declare $1 as an "
4191 "extension number.",
4192 field->containing_type()->full_name(),
4197 if (field->containing_oneof() != NULL) {
4198 if (field->label() != FieldDescriptor::LABEL_OPTIONAL) {
4199 // Note that this error will never happen when parsing .proto files.
4200 // It can only happen if you manually construct a FileDescriptorProto
4201 // that is incorrect.
4202 AddError(field->full_name(), proto,
4203 DescriptorPool::ErrorCollector::NAME,
4204 "Fields of oneofs must themselves have label LABEL_OPTIONAL.");
4208 if (proto.has_type_name()) {
4209 // Assume we are expecting a message type unless the proto contains some
4210 // evidence that it expects an enum type. This only makes a difference if
4211 // we end up creating a placeholder.
4212 bool expecting_enum = (proto.type() == FieldDescriptorProto::TYPE_ENUM) ||
4213 proto.has_default_value();
4216 LookupSymbol(proto.type_name(), field->full_name(),
4217 expecting_enum ? PLACEHOLDER_ENUM : PLACEHOLDER_MESSAGE,
4220 // If the type is a weak type, we change the type to a google.protobuf.Empty field.
4221 if (type.IsNull() && !pool_->enforce_weak_ && proto.options().weak()) {
4222 type = FindSymbol(kNonLinkedWeakMessageReplacementName);
4225 if (type.IsNull()) {
4226 AddNotDefinedError(field->full_name(), proto,
4227 DescriptorPool::ErrorCollector::TYPE,
4232 if (!proto.has_type()) {
4233 // Choose field type based on symbol.
4234 if (type.type == Symbol::MESSAGE) {
4235 field->type_ = FieldDescriptor::TYPE_MESSAGE;
4236 } else if (type.type == Symbol::ENUM) {
4237 field->type_ = FieldDescriptor::TYPE_ENUM;
4239 AddError(field->full_name(), proto,
4240 DescriptorPool::ErrorCollector::TYPE,
4241 "\"" + proto.type_name() + "\" is not a type.");
4246 if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) {
4247 if (type.type != Symbol::MESSAGE) {
4248 AddError(field->full_name(), proto,
4249 DescriptorPool::ErrorCollector::TYPE,
4250 "\"" + proto.type_name() + "\" is not a message type.");
4253 field->message_type_ = type.descriptor;
4255 if (field->has_default_value()) {
4256 AddError(field->full_name(), proto,
4257 DescriptorPool::ErrorCollector::DEFAULT_VALUE,
4258 "Messages can't have default values.");
4260 } else if (field->cpp_type() == FieldDescriptor::CPPTYPE_ENUM) {
4261 if (type.type != Symbol::ENUM) {
4262 AddError(field->full_name(), proto,
4263 DescriptorPool::ErrorCollector::TYPE,
4264 "\"" + proto.type_name() + "\" is not an enum type.");
4267 field->enum_type_ = type.enum_descriptor;
4269 if (field->enum_type()->is_placeholder_) {
4270 // We can't look up default values for placeholder types. We'll have
4271 // to just drop them.
4272 field->has_default_value_ = false;
4275 if (field->has_default_value()) {
4276 // Ensure that the default value is an identifier. Parser cannot always
4277 // verify this because it does not have complete type information.
4278 // N.B. that this check yields better error messages but is not
4279 // necessary for correctness (an enum symbol must be a valid identifier
4280 // anyway), only for better errors.
4281 if (!io::Tokenizer::IsIdentifier(proto.default_value())) {
4282 AddError(field->full_name(), proto,
4283 DescriptorPool::ErrorCollector::DEFAULT_VALUE,
4284 "Default value for an enum field must be an identifier.");
4286 // We can't just use field->enum_type()->FindValueByName() here
4287 // because that locks the pool's mutex, which we have already locked
4289 Symbol default_value =
4290 LookupSymbolNoPlaceholder(proto.default_value(),
4291 field->enum_type()->full_name());
4293 if (default_value.type == Symbol::ENUM_VALUE &&
4294 default_value.enum_value_descriptor->type() ==
4295 field->enum_type()) {
4296 field->default_value_enum_ = default_value.enum_value_descriptor;
4298 AddError(field->full_name(), proto,
4299 DescriptorPool::ErrorCollector::DEFAULT_VALUE,
4300 "Enum type \"" + field->enum_type()->full_name() +
4301 "\" has no value named \"" + proto.default_value() +
4305 } else if (field->enum_type()->value_count() > 0) {
4306 // All enums must have at least one value, or we would have reported
4307 // an error elsewhere. We use the first defined value as the default
4308 // if a default is not explicitly defined.
4309 field->default_value_enum_ = field->enum_type()->value(0);
4312 AddError(field->full_name(), proto, DescriptorPool::ErrorCollector::TYPE,
4313 "Field with primitive type has type_name.");
4316 if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE ||
4317 field->cpp_type() == FieldDescriptor::CPPTYPE_ENUM) {
4318 AddError(field->full_name(), proto, DescriptorPool::ErrorCollector::TYPE,
4319 "Field with message or enum type missing type_name.");
4323 // Add the field to the fields-by-number table.
4324 // Note: We have to do this *after* cross-linking because extensions do not
4325 // know their containing type until now.
4326 if (!file_tables_->AddFieldByNumber(field)) {
4327 const FieldDescriptor* conflicting_field =
4328 file_tables_->FindFieldByNumber(field->containing_type(),
4330 if (field->is_extension()) {
4331 AddError(field->full_name(), proto,
4332 DescriptorPool::ErrorCollector::NUMBER,
4333 strings::Substitute("Extension number $0 has already been used "
4334 "in \"$1\" by extension \"$2\".",
4336 field->containing_type()->full_name(),
4337 conflicting_field->full_name()));
4339 AddError(field->full_name(), proto,
4340 DescriptorPool::ErrorCollector::NUMBER,
4341 strings::Substitute("Field number $0 has already been used in "
4342 "\"$1\" by field \"$2\".",
4344 field->containing_type()->full_name(),
4345 conflicting_field->name()));
4348 if (field->is_extension()) {
4349 if (!tables_->AddExtension(field)) {
4350 const FieldDescriptor* conflicting_field =
4351 tables_->FindExtension(field->containing_type(), field->number());
4352 string error_msg = strings::Substitute(
4353 "Extension number $0 has already been used in \"$1\" by extension "
4354 "\"$2\" defined in $3.",
4356 field->containing_type()->full_name(),
4357 conflicting_field->full_name(),
4358 conflicting_field->file()->name());
4359 // Conflicting extension numbers should be an error. However, before
4360 // turning this into an error we need to fix all existing broken
4362 // TODO(xiaofeng): Change this to an error.
4363 AddWarning(field->full_name(), proto,
4364 DescriptorPool::ErrorCollector::NUMBER, error_msg);
4369 // Add the field to the lowercase-name and camelcase-name tables.
4370 file_tables_->AddFieldByStylizedNames(field);
4373 void DescriptorBuilder::CrossLinkEnum(
4374 EnumDescriptor* enum_type, const EnumDescriptorProto& proto) {
4375 if (enum_type->options_ == NULL) {
4376 enum_type->options_ = &EnumOptions::default_instance();
4379 for (int i = 0; i < enum_type->value_count(); i++) {
4380 CrossLinkEnumValue(&enum_type->values_[i], proto.value(i));
4384 void DescriptorBuilder::CrossLinkEnumValue(
4385 EnumValueDescriptor* enum_value,
4386 const EnumValueDescriptorProto& /* proto */) {
4387 if (enum_value->options_ == NULL) {
4388 enum_value->options_ = &EnumValueOptions::default_instance();
4392 void DescriptorBuilder::CrossLinkService(
4393 ServiceDescriptor* service, const ServiceDescriptorProto& proto) {
4394 if (service->options_ == NULL) {
4395 service->options_ = &ServiceOptions::default_instance();
4398 for (int i = 0; i < service->method_count(); i++) {
4399 CrossLinkMethod(&service->methods_[i], proto.method(i));
4403 void DescriptorBuilder::CrossLinkMethod(
4404 MethodDescriptor* method, const MethodDescriptorProto& proto) {
4405 if (method->options_ == NULL) {
4406 method->options_ = &MethodOptions::default_instance();
4409 Symbol input_type = LookupSymbol(proto.input_type(), method->full_name());
4410 if (input_type.IsNull()) {
4411 AddNotDefinedError(method->full_name(), proto,
4412 DescriptorPool::ErrorCollector::INPUT_TYPE,
4413 proto.input_type());
4414 } else if (input_type.type != Symbol::MESSAGE) {
4415 AddError(method->full_name(), proto,
4416 DescriptorPool::ErrorCollector::INPUT_TYPE,
4417 "\"" + proto.input_type() + "\" is not a message type.");
4419 method->input_type_ = input_type.descriptor;
4422 Symbol output_type = LookupSymbol(proto.output_type(), method->full_name());
4423 if (output_type.IsNull()) {
4424 AddNotDefinedError(method->full_name(), proto,
4425 DescriptorPool::ErrorCollector::OUTPUT_TYPE,
4426 proto.output_type());
4427 } else if (output_type.type != Symbol::MESSAGE) {
4428 AddError(method->full_name(), proto,
4429 DescriptorPool::ErrorCollector::OUTPUT_TYPE,
4430 "\"" + proto.output_type() + "\" is not a message type.");
4432 method->output_type_ = output_type.descriptor;
4436 // -------------------------------------------------------------------
4438 #define VALIDATE_OPTIONS_FROM_ARRAY(descriptor, array_name, type) \
4439 for (int i = 0; i < descriptor->array_name##_count(); ++i) { \
4440 Validate##type##Options(descriptor->array_name##s_ + i, \
4441 proto.array_name(i)); \
4444 // Determine if the file uses optimize_for = LITE_RUNTIME, being careful to
4445 // avoid problems that exist at init time.
4446 static bool IsLite(const FileDescriptor* file) {
4447 // TODO(kenton): I don't even remember how many of these conditions are
4448 // actually possible. I'm just being super-safe.
4449 return file != NULL &&
4450 &file->options() != &FileOptions::default_instance() &&
4451 file->options().optimize_for() == FileOptions::LITE_RUNTIME;
4454 void DescriptorBuilder::ValidateFileOptions(FileDescriptor* file,
4455 const FileDescriptorProto& proto) {
4456 VALIDATE_OPTIONS_FROM_ARRAY(file, message_type, Message);
4457 VALIDATE_OPTIONS_FROM_ARRAY(file, enum_type, Enum);
4458 VALIDATE_OPTIONS_FROM_ARRAY(file, service, Service);
4459 VALIDATE_OPTIONS_FROM_ARRAY(file, extension, Field);
4461 // Lite files can only be imported by other Lite files.
4462 if (!IsLite(file)) {
4463 for (int i = 0; i < file->dependency_count(); i++) {
4464 if (IsLite(file->dependency(i))) {
4466 file->name(), proto,
4467 DescriptorPool::ErrorCollector::OTHER,
4468 "Files that do not use optimize_for = LITE_RUNTIME cannot import "
4469 "files which do use this option. This file is not lite, but it "
4470 "imports \"" + file->dependency(i)->name() + "\" which is.");
4478 void DescriptorBuilder::ValidateMessageOptions(Descriptor* message,
4479 const DescriptorProto& proto) {
4480 VALIDATE_OPTIONS_FROM_ARRAY(message, field, Field);
4481 VALIDATE_OPTIONS_FROM_ARRAY(message, nested_type, Message);
4482 VALIDATE_OPTIONS_FROM_ARRAY(message, enum_type, Enum);
4483 VALIDATE_OPTIONS_FROM_ARRAY(message, extension, Field);
4485 const int64 max_extension_range =
4486 static_cast<int64>(message->options().message_set_wire_format() ?
4488 FieldDescriptor::kMaxNumber);
4489 for (int i = 0; i < message->extension_range_count(); ++i) {
4490 if (message->extension_range(i)->end > max_extension_range + 1) {
4492 message->full_name(), proto.extension_range(i),
4493 DescriptorPool::ErrorCollector::NUMBER,
4494 strings::Substitute("Extension numbers cannot be greater than $0.",
4495 max_extension_range));
4500 void DescriptorBuilder::ValidateFieldOptions(FieldDescriptor* field,
4501 const FieldDescriptorProto& proto) {
4502 if (field->options().has_experimental_map_key()) {
4503 ValidateMapKey(field, proto);
4506 // Only message type fields may be lazy.
4507 if (field->options().lazy()) {
4508 if (field->type() != FieldDescriptor::TYPE_MESSAGE) {
4509 AddError(field->full_name(), proto,
4510 DescriptorPool::ErrorCollector::TYPE,
4511 "[lazy = true] can only be specified for submessage fields.");
4515 // Only repeated primitive fields may be packed.
4516 if (field->options().packed() && !field->is_packable()) {
4518 field->full_name(), proto,
4519 DescriptorPool::ErrorCollector::TYPE,
4520 "[packed = true] can only be specified for repeated primitive fields.");
4523 // Note: Default instance may not yet be initialized here, so we have to
4524 // avoid reading from it.
4525 if (field->containing_type_ != NULL &&
4526 &field->containing_type()->options() !=
4527 &MessageOptions::default_instance() &&
4528 field->containing_type()->options().message_set_wire_format()) {
4529 if (field->is_extension()) {
4530 if (!field->is_optional() ||
4531 field->type() != FieldDescriptor::TYPE_MESSAGE) {
4532 AddError(field->full_name(), proto,
4533 DescriptorPool::ErrorCollector::TYPE,
4534 "Extensions of MessageSets must be optional messages.");
4537 AddError(field->full_name(), proto,
4538 DescriptorPool::ErrorCollector::NAME,
4539 "MessageSets cannot have fields, only extensions.");
4543 // Lite extensions can only be of Lite types.
4544 if (IsLite(field->file()) &&
4545 field->containing_type_ != NULL &&
4546 !IsLite(field->containing_type()->file())) {
4547 AddError(field->full_name(), proto,
4548 DescriptorPool::ErrorCollector::EXTENDEE,
4549 "Extensions to non-lite types can only be declared in non-lite "
4550 "files. Note that you cannot extend a non-lite type to contain "
4551 "a lite type, but the reverse is allowed.");
4556 void DescriptorBuilder::ValidateEnumOptions(EnumDescriptor* enm,
4557 const EnumDescriptorProto& proto) {
4558 VALIDATE_OPTIONS_FROM_ARRAY(enm, value, EnumValue);
4559 if (!enm->options().has_allow_alias() || !enm->options().allow_alias()) {
4560 map<int, string> used_values;
4561 for (int i = 0; i < enm->value_count(); ++i) {
4562 const EnumValueDescriptor* enum_value = enm->value(i);
4563 if (used_values.find(enum_value->number()) != used_values.end()) {
4565 "\"" + enum_value->full_name() +
4566 "\" uses the same enum value as \"" +
4567 used_values[enum_value->number()] + "\". If this is intended, set "
4568 "'option allow_alias = true;' to the enum definition.";
4569 if (!enm->options().allow_alias()) {
4570 // Generate error if duplicated enum values are explicitly disallowed.
4571 AddError(enm->full_name(), proto,
4572 DescriptorPool::ErrorCollector::NUMBER,
4575 // Generate warning if duplicated values are found but the option
4577 GOOGLE_LOG(ERROR) << error;
4580 used_values[enum_value->number()] = enum_value->full_name();
4586 void DescriptorBuilder::ValidateEnumValueOptions(
4587 EnumValueDescriptor* /* enum_value */,
4588 const EnumValueDescriptorProto& /* proto */) {
4589 // Nothing to do so far.
4591 void DescriptorBuilder::ValidateServiceOptions(ServiceDescriptor* service,
4592 const ServiceDescriptorProto& proto) {
4593 if (IsLite(service->file()) &&
4594 (service->file()->options().cc_generic_services() ||
4595 service->file()->options().java_generic_services())) {
4596 AddError(service->full_name(), proto,
4597 DescriptorPool::ErrorCollector::NAME,
4598 "Files with optimize_for = LITE_RUNTIME cannot define services "
4599 "unless you set both options cc_generic_services and "
4600 "java_generic_sevices to false.");
4603 VALIDATE_OPTIONS_FROM_ARRAY(service, method, Method);
4606 void DescriptorBuilder::ValidateMethodOptions(MethodDescriptor* /* method */,
4607 const MethodDescriptorProto& /* proto */) {
4608 // Nothing to do so far.
4611 void DescriptorBuilder::ValidateMapKey(FieldDescriptor* field,
4612 const FieldDescriptorProto& proto) {
4613 if (!field->is_repeated()) {
4614 AddError(field->full_name(), proto, DescriptorPool::ErrorCollector::TYPE,
4615 "map type is only allowed for repeated fields.");
4619 if (field->cpp_type() != FieldDescriptor::CPPTYPE_MESSAGE) {
4620 AddError(field->full_name(), proto, DescriptorPool::ErrorCollector::TYPE,
4621 "map type is only allowed for fields with a message type.");
4625 const Descriptor* item_type = field->message_type();
4626 if (item_type == NULL) {
4627 AddError(field->full_name(), proto, DescriptorPool::ErrorCollector::TYPE,
4628 "Could not find field type.");
4632 // Find the field in item_type named by "experimental_map_key"
4633 const string& key_name = field->options().experimental_map_key();
4634 const Symbol key_symbol = LookupSymbol(
4636 // We append ".key_name" to the containing type's name since
4637 // LookupSymbol() searches for peers of the supplied name, not
4638 // children of the supplied name.
4639 item_type->full_name() + "." + key_name);
4641 if (key_symbol.IsNull() || key_symbol.field_descriptor->is_extension()) {
4642 AddError(field->full_name(), proto, DescriptorPool::ErrorCollector::TYPE,
4643 "Could not find field named \"" + key_name + "\" in type \"" +
4644 item_type->full_name() + "\".");
4647 const FieldDescriptor* key_field = key_symbol.field_descriptor;
4649 if (key_field->is_repeated()) {
4650 AddError(field->full_name(), proto, DescriptorPool::ErrorCollector::TYPE,
4651 "map_key must not name a repeated field.");
4655 if (key_field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) {
4656 AddError(field->full_name(), proto, DescriptorPool::ErrorCollector::TYPE,
4657 "map key must name a scalar or string field.");
4661 field->experimental_map_key_ = key_field;
4665 #undef VALIDATE_OPTIONS_FROM_ARRAY
4667 // -------------------------------------------------------------------
4669 DescriptorBuilder::OptionInterpreter::OptionInterpreter(
4670 DescriptorBuilder* builder) : builder_(builder) {
4671 GOOGLE_CHECK(builder_);
4674 DescriptorBuilder::OptionInterpreter::~OptionInterpreter() {
4677 bool DescriptorBuilder::OptionInterpreter::InterpretOptions(
4678 OptionsToInterpret* options_to_interpret) {
4679 // Note that these may be in different pools, so we can't use the same
4680 // descriptor and reflection objects on both.
4681 Message* options = options_to_interpret->options;
4682 const Message* original_options = options_to_interpret->original_options;
4684 bool failed = false;
4685 options_to_interpret_ = options_to_interpret;
4687 // Find the uninterpreted_option field in the mutable copy of the options
4688 // and clear them, since we're about to interpret them.
4689 const FieldDescriptor* uninterpreted_options_field =
4690 options->GetDescriptor()->FindFieldByName("uninterpreted_option");
4691 GOOGLE_CHECK(uninterpreted_options_field != NULL)
4692 << "No field named \"uninterpreted_option\" in the Options proto.";
4693 options->GetReflection()->ClearField(options, uninterpreted_options_field);
4695 // Find the uninterpreted_option field in the original options.
4696 const FieldDescriptor* original_uninterpreted_options_field =
4697 original_options->GetDescriptor()->
4698 FindFieldByName("uninterpreted_option");
4699 GOOGLE_CHECK(original_uninterpreted_options_field != NULL)
4700 << "No field named \"uninterpreted_option\" in the Options proto.";
4702 const int num_uninterpreted_options = original_options->GetReflection()->
4703 FieldSize(*original_options, original_uninterpreted_options_field);
4704 for (int i = 0; i < num_uninterpreted_options; ++i) {
4705 uninterpreted_option_ = down_cast<const UninterpretedOption*>(
4706 &original_options->GetReflection()->GetRepeatedMessage(
4707 *original_options, original_uninterpreted_options_field, i));
4708 if (!InterpretSingleOption(options)) {
4709 // Error already added by InterpretSingleOption().
4714 // Reset these, so we don't have any dangling pointers.
4715 uninterpreted_option_ = NULL;
4716 options_to_interpret_ = NULL;
4719 // InterpretSingleOption() added the interpreted options in the
4720 // UnknownFieldSet, in case the option isn't yet known to us. Now we
4721 // serialize the options message and deserialize it back. That way, any
4722 // option fields that we do happen to know about will get moved from the
4723 // UnknownFieldSet into the real fields, and thus be available right away.
4724 // If they are not known, that's OK too. They will get reparsed into the
4725 // UnknownFieldSet and wait there until the message is parsed by something
4726 // that does know about the options.
4728 options->AppendToString(&buf);
4729 GOOGLE_CHECK(options->ParseFromString(buf))
4730 << "Protocol message serialized itself in invalid fashion.";
4736 bool DescriptorBuilder::OptionInterpreter::InterpretSingleOption(
4738 // First do some basic validation.
4739 if (uninterpreted_option_->name_size() == 0) {
4740 // This should never happen unless the parser has gone seriously awry or
4741 // someone has manually created the uninterpreted option badly.
4742 return AddNameError("Option must have a name.");
4744 if (uninterpreted_option_->name(0).name_part() == "uninterpreted_option") {
4745 return AddNameError("Option must not use reserved name "
4746 "\"uninterpreted_option\".");
4749 const Descriptor* options_descriptor = NULL;
4750 // Get the options message's descriptor from the builder's pool, so that we
4751 // get the version that knows about any extension options declared in the
4752 // file we're currently building. The descriptor should be there as long as
4753 // the file we're building imported "google/protobuf/descriptors.proto".
4755 // Note that we use DescriptorBuilder::FindSymbolNotEnforcingDeps(), not
4756 // DescriptorPool::FindMessageTypeByName() because we're already holding the
4757 // pool's mutex, and the latter method locks it again. We don't use
4758 // FindSymbol() because files that use custom options only need to depend on
4759 // the file that defines the option, not descriptor.proto itself.
4760 Symbol symbol = builder_->FindSymbolNotEnforcingDeps(
4761 options->GetDescriptor()->full_name());
4762 if (!symbol.IsNull() && symbol.type == Symbol::MESSAGE) {
4763 options_descriptor = symbol.descriptor;
4765 // The options message's descriptor was not in the builder's pool, so use
4766 // the standard version from the generated pool. We're not holding the
4767 // generated pool's mutex, so we can search it the straightforward way.
4768 options_descriptor = options->GetDescriptor();
4770 GOOGLE_CHECK(options_descriptor);
4772 // We iterate over the name parts to drill into the submessages until we find
4773 // the leaf field for the option. As we drill down we remember the current
4774 // submessage's descriptor in |descriptor| and the next field in that
4775 // submessage in |field|. We also track the fields we're drilling down
4776 // through in |intermediate_fields|. As we go, we reconstruct the full option
4777 // name in |debug_msg_name|, for use in error messages.
4778 const Descriptor* descriptor = options_descriptor;
4779 const FieldDescriptor* field = NULL;
4780 vector<const FieldDescriptor*> intermediate_fields;
4781 string debug_msg_name = "";
4783 for (int i = 0; i < uninterpreted_option_->name_size(); ++i) {
4784 const string& name_part = uninterpreted_option_->name(i).name_part();
4785 if (debug_msg_name.size() > 0) {
4786 debug_msg_name += ".";
4788 if (uninterpreted_option_->name(i).is_extension()) {
4789 debug_msg_name += "(" + name_part + ")";
4790 // Search for the extension's descriptor as an extension in the builder's
4791 // pool. Note that we use DescriptorBuilder::LookupSymbol(), not
4792 // DescriptorPool::FindExtensionByName(), for two reasons: 1) It allows
4793 // relative lookups, and 2) because we're already holding the pool's
4794 // mutex, and the latter method locks it again.
4795 symbol = builder_->LookupSymbol(name_part,
4796 options_to_interpret_->name_scope);
4797 if (!symbol.IsNull() && symbol.type == Symbol::FIELD) {
4798 field = symbol.field_descriptor;
4800 // If we don't find the field then the field's descriptor was not in the
4801 // builder's pool, but there's no point in looking in the generated
4802 // pool. We require that you import the file that defines any extensions
4803 // you use, so they must be present in the builder's pool.
4805 debug_msg_name += name_part;
4806 // Search for the field's descriptor as a regular field.
4807 field = descriptor->FindFieldByName(name_part);
4810 if (field == NULL) {
4811 if (get_allow_unknown(builder_->pool_)) {
4812 // We can't find the option, but AllowUnknownDependencies() is enabled,
4813 // so we will just leave it as uninterpreted.
4814 AddWithoutInterpreting(*uninterpreted_option_, options);
4816 } else if (!(builder_->undefine_resolved_name_).empty()) {
4817 // Option is resolved to a name which is not defined.
4818 return AddNameError(
4819 "Option \"" + debug_msg_name + "\" is resolved to \"(" +
4820 builder_->undefine_resolved_name_ +
4821 ")\", which is not defined. The innermost scope is searched first "
4822 "in name resolution. Consider using a leading '.'(i.e., \"(." +
4823 debug_msg_name.substr(1) +
4824 "\") to start from the outermost scope.");
4826 return AddNameError("Option \"" + debug_msg_name + "\" unknown.");
4828 } else if (field->containing_type() != descriptor) {
4829 if (get_is_placeholder(field->containing_type())) {
4830 // The field is an extension of a placeholder type, so we can't
4831 // reliably verify whether it is a valid extension to use here (e.g.
4832 // we don't know if it is an extension of the correct *Options message,
4833 // or if it has a valid field number, etc.). Just leave it as
4834 // uninterpreted instead.
4835 AddWithoutInterpreting(*uninterpreted_option_, options);
4838 // This can only happen if, due to some insane misconfiguration of the
4839 // pools, we find the options message in one pool but the field in
4840 // another. This would probably imply a hefty bug somewhere.
4841 return AddNameError("Option field \"" + debug_msg_name +
4842 "\" is not a field or extension of message \"" +
4843 descriptor->name() + "\".");
4845 } else if (i < uninterpreted_option_->name_size() - 1) {
4846 if (field->cpp_type() != FieldDescriptor::CPPTYPE_MESSAGE) {
4847 return AddNameError("Option \"" + debug_msg_name +
4848 "\" is an atomic type, not a message.");
4849 } else if (field->is_repeated()) {
4850 return AddNameError("Option field \"" + debug_msg_name +
4851 "\" is a repeated message. Repeated message "
4852 "options must be initialized using an "
4853 "aggregate value.");
4855 // Drill down into the submessage.
4856 intermediate_fields.push_back(field);
4857 descriptor = field->message_type();
4862 // We've found the leaf field. Now we use UnknownFieldSets to set its value
4863 // on the options message. We do so because the message may not yet know
4864 // about its extension fields, so we may not be able to set the fields
4865 // directly. But the UnknownFieldSets will serialize to the same wire-format
4866 // message, so reading that message back in once the extension fields are
4867 // known will populate them correctly.
4869 // First see if the option is already set.
4870 if (!field->is_repeated() && !ExamineIfOptionIsSet(
4871 intermediate_fields.begin(),
4872 intermediate_fields.end(),
4873 field, debug_msg_name,
4874 options->GetReflection()->GetUnknownFields(*options))) {
4875 return false; // ExamineIfOptionIsSet() already added the error.
4879 // First set the value on the UnknownFieldSet corresponding to the
4880 // innermost message.
4881 scoped_ptr<UnknownFieldSet> unknown_fields(new UnknownFieldSet());
4882 if (!SetOptionValue(field, unknown_fields.get())) {
4883 return false; // SetOptionValue() already added the error.
4886 // Now wrap the UnknownFieldSet with UnknownFieldSets corresponding to all
4887 // the intermediate messages.
4888 for (vector<const FieldDescriptor*>::reverse_iterator iter =
4889 intermediate_fields.rbegin();
4890 iter != intermediate_fields.rend(); ++iter) {
4891 scoped_ptr<UnknownFieldSet> parent_unknown_fields(new UnknownFieldSet());
4892 switch ((*iter)->type()) {
4893 case FieldDescriptor::TYPE_MESSAGE: {
4894 io::StringOutputStream outstr(
4895 parent_unknown_fields->AddLengthDelimited((*iter)->number()));
4896 io::CodedOutputStream out(&outstr);
4897 internal::WireFormat::SerializeUnknownFields(*unknown_fields, &out);
4898 GOOGLE_CHECK(!out.HadError())
4899 << "Unexpected failure while serializing option submessage "
4900 << debug_msg_name << "\".";
4904 case FieldDescriptor::TYPE_GROUP: {
4905 parent_unknown_fields->AddGroup((*iter)->number())
4906 ->MergeFrom(*unknown_fields);
4911 GOOGLE_LOG(FATAL) << "Invalid wire type for CPPTYPE_MESSAGE: "
4915 unknown_fields.reset(parent_unknown_fields.release());
4918 // Now merge the UnknownFieldSet corresponding to the top-level message into
4919 // the options message.
4920 options->GetReflection()->MutableUnknownFields(options)->MergeFrom(
4926 void DescriptorBuilder::OptionInterpreter::AddWithoutInterpreting(
4927 const UninterpretedOption& uninterpreted_option, Message* options) {
4928 const FieldDescriptor* field =
4929 options->GetDescriptor()->FindFieldByName("uninterpreted_option");
4930 GOOGLE_CHECK(field != NULL);
4932 options->GetReflection()->AddMessage(options, field)
4933 ->CopyFrom(uninterpreted_option);
4936 bool DescriptorBuilder::OptionInterpreter::ExamineIfOptionIsSet(
4937 vector<const FieldDescriptor*>::const_iterator intermediate_fields_iter,
4938 vector<const FieldDescriptor*>::const_iterator intermediate_fields_end,
4939 const FieldDescriptor* innermost_field, const string& debug_msg_name,
4940 const UnknownFieldSet& unknown_fields) {
4941 // We do linear searches of the UnknownFieldSet and its sub-groups. This
4942 // should be fine since it's unlikely that any one options structure will
4943 // contain more than a handful of options.
4945 if (intermediate_fields_iter == intermediate_fields_end) {
4946 // We're at the innermost submessage.
4947 for (int i = 0; i < unknown_fields.field_count(); i++) {
4948 if (unknown_fields.field(i).number() == innermost_field->number()) {
4949 return AddNameError("Option \"" + debug_msg_name +
4950 "\" was already set.");
4956 for (int i = 0; i < unknown_fields.field_count(); i++) {
4957 if (unknown_fields.field(i).number() ==
4958 (*intermediate_fields_iter)->number()) {
4959 const UnknownField* unknown_field = &unknown_fields.field(i);
4960 FieldDescriptor::Type type = (*intermediate_fields_iter)->type();
4961 // Recurse into the next submessage.
4963 case FieldDescriptor::TYPE_MESSAGE:
4964 if (unknown_field->type() == UnknownField::TYPE_LENGTH_DELIMITED) {
4965 UnknownFieldSet intermediate_unknown_fields;
4966 if (intermediate_unknown_fields.ParseFromString(
4967 unknown_field->length_delimited()) &&
4968 !ExamineIfOptionIsSet(intermediate_fields_iter + 1,
4969 intermediate_fields_end,
4970 innermost_field, debug_msg_name,
4971 intermediate_unknown_fields)) {
4972 return false; // Error already added.
4977 case FieldDescriptor::TYPE_GROUP:
4978 if (unknown_field->type() == UnknownField::TYPE_GROUP) {
4979 if (!ExamineIfOptionIsSet(intermediate_fields_iter + 1,
4980 intermediate_fields_end,
4981 innermost_field, debug_msg_name,
4982 unknown_field->group())) {
4983 return false; // Error already added.
4989 GOOGLE_LOG(FATAL) << "Invalid wire type for CPPTYPE_MESSAGE: " << type;
4997 bool DescriptorBuilder::OptionInterpreter::SetOptionValue(
4998 const FieldDescriptor* option_field,
4999 UnknownFieldSet* unknown_fields) {
5000 // We switch on the CppType to validate.
5001 switch (option_field->cpp_type()) {
5003 case FieldDescriptor::CPPTYPE_INT32:
5004 if (uninterpreted_option_->has_positive_int_value()) {
5005 if (uninterpreted_option_->positive_int_value() >
5006 static_cast<uint64>(kint32max)) {
5007 return AddValueError("Value out of range for int32 option \"" +
5008 option_field->full_name() + "\".");
5010 SetInt32(option_field->number(),
5011 uninterpreted_option_->positive_int_value(),
5012 option_field->type(), unknown_fields);
5014 } else if (uninterpreted_option_->has_negative_int_value()) {
5015 if (uninterpreted_option_->negative_int_value() <
5016 static_cast<int64>(kint32min)) {
5017 return AddValueError("Value out of range for int32 option \"" +
5018 option_field->full_name() + "\".");
5020 SetInt32(option_field->number(),
5021 uninterpreted_option_->negative_int_value(),
5022 option_field->type(), unknown_fields);
5025 return AddValueError("Value must be integer for int32 option \"" +
5026 option_field->full_name() + "\".");
5030 case FieldDescriptor::CPPTYPE_INT64:
5031 if (uninterpreted_option_->has_positive_int_value()) {
5032 if (uninterpreted_option_->positive_int_value() >
5033 static_cast<uint64>(kint64max)) {
5034 return AddValueError("Value out of range for int64 option \"" +
5035 option_field->full_name() + "\".");
5037 SetInt64(option_field->number(),
5038 uninterpreted_option_->positive_int_value(),
5039 option_field->type(), unknown_fields);
5041 } else if (uninterpreted_option_->has_negative_int_value()) {
5042 SetInt64(option_field->number(),
5043 uninterpreted_option_->negative_int_value(),
5044 option_field->type(), unknown_fields);
5046 return AddValueError("Value must be integer for int64 option \"" +
5047 option_field->full_name() + "\".");
5051 case FieldDescriptor::CPPTYPE_UINT32:
5052 if (uninterpreted_option_->has_positive_int_value()) {
5053 if (uninterpreted_option_->positive_int_value() > kuint32max) {
5054 return AddValueError("Value out of range for uint32 option \"" +
5055 option_field->name() + "\".");
5057 SetUInt32(option_field->number(),
5058 uninterpreted_option_->positive_int_value(),
5059 option_field->type(), unknown_fields);
5062 return AddValueError("Value must be non-negative integer for uint32 "
5063 "option \"" + option_field->full_name() + "\".");
5067 case FieldDescriptor::CPPTYPE_UINT64:
5068 if (uninterpreted_option_->has_positive_int_value()) {
5069 SetUInt64(option_field->number(),
5070 uninterpreted_option_->positive_int_value(),
5071 option_field->type(), unknown_fields);
5073 return AddValueError("Value must be non-negative integer for uint64 "
5074 "option \"" + option_field->full_name() + "\".");
5078 case FieldDescriptor::CPPTYPE_FLOAT: {
5080 if (uninterpreted_option_->has_double_value()) {
5081 value = uninterpreted_option_->double_value();
5082 } else if (uninterpreted_option_->has_positive_int_value()) {
5083 value = uninterpreted_option_->positive_int_value();
5084 } else if (uninterpreted_option_->has_negative_int_value()) {
5085 value = uninterpreted_option_->negative_int_value();
5087 return AddValueError("Value must be number for float option \"" +
5088 option_field->full_name() + "\".");
5090 unknown_fields->AddFixed32(option_field->number(),
5091 google::protobuf::internal::WireFormatLite::EncodeFloat(value));
5095 case FieldDescriptor::CPPTYPE_DOUBLE: {
5097 if (uninterpreted_option_->has_double_value()) {
5098 value = uninterpreted_option_->double_value();
5099 } else if (uninterpreted_option_->has_positive_int_value()) {
5100 value = uninterpreted_option_->positive_int_value();
5101 } else if (uninterpreted_option_->has_negative_int_value()) {
5102 value = uninterpreted_option_->negative_int_value();
5104 return AddValueError("Value must be number for double option \"" +
5105 option_field->full_name() + "\".");
5107 unknown_fields->AddFixed64(option_field->number(),
5108 google::protobuf::internal::WireFormatLite::EncodeDouble(value));
5112 case FieldDescriptor::CPPTYPE_BOOL:
5114 if (!uninterpreted_option_->has_identifier_value()) {
5115 return AddValueError("Value must be identifier for boolean option "
5116 "\"" + option_field->full_name() + "\".");
5118 if (uninterpreted_option_->identifier_value() == "true") {
5120 } else if (uninterpreted_option_->identifier_value() == "false") {
5123 return AddValueError("Value must be \"true\" or \"false\" for boolean "
5124 "option \"" + option_field->full_name() + "\".");
5126 unknown_fields->AddVarint(option_field->number(), value);
5129 case FieldDescriptor::CPPTYPE_ENUM: {
5130 if (!uninterpreted_option_->has_identifier_value()) {
5131 return AddValueError("Value must be identifier for enum-valued option "
5132 "\"" + option_field->full_name() + "\".");
5134 const EnumDescriptor* enum_type = option_field->enum_type();
5135 const string& value_name = uninterpreted_option_->identifier_value();
5136 const EnumValueDescriptor* enum_value = NULL;
5138 if (enum_type->file()->pool() != DescriptorPool::generated_pool()) {
5139 // Note that the enum value's fully-qualified name is a sibling of the
5140 // enum's name, not a child of it.
5141 string fully_qualified_name = enum_type->full_name();
5142 fully_qualified_name.resize(fully_qualified_name.size() -
5143 enum_type->name().size());
5144 fully_qualified_name += value_name;
5146 // Search for the enum value's descriptor in the builder's pool. Note
5147 // that we use DescriptorBuilder::FindSymbolNotEnforcingDeps(), not
5148 // DescriptorPool::FindEnumValueByName() because we're already holding
5149 // the pool's mutex, and the latter method locks it again.
5151 builder_->FindSymbolNotEnforcingDeps(fully_qualified_name);
5152 if (!symbol.IsNull() && symbol.type == Symbol::ENUM_VALUE) {
5153 if (symbol.enum_value_descriptor->type() != enum_type) {
5154 return AddValueError("Enum type \"" + enum_type->full_name() +
5155 "\" has no value named \"" + value_name + "\" for option \"" +
5156 option_field->full_name() +
5157 "\". This appears to be a value from a sibling type.");
5159 enum_value = symbol.enum_value_descriptor;
5163 // The enum type is in the generated pool, so we can search for the
5165 enum_value = enum_type->FindValueByName(value_name);
5168 if (enum_value == NULL) {
5169 return AddValueError("Enum type \"" +
5170 option_field->enum_type()->full_name() +
5171 "\" has no value named \"" + value_name + "\" for "
5172 "option \"" + option_field->full_name() + "\".");
5174 // Sign-extension is not a problem, since we cast directly from int32 to
5175 // uint64, without first going through uint32.
5176 unknown_fields->AddVarint(option_field->number(),
5177 static_cast<uint64>(static_cast<int64>(enum_value->number())));
5182 case FieldDescriptor::CPPTYPE_STRING:
5183 if (!uninterpreted_option_->has_string_value()) {
5184 return AddValueError("Value must be quoted string for string option "
5185 "\"" + option_field->full_name() + "\".");
5187 // The string has already been unquoted and unescaped by the parser.
5188 unknown_fields->AddLengthDelimited(option_field->number(),
5189 uninterpreted_option_->string_value());
5192 case FieldDescriptor::CPPTYPE_MESSAGE:
5193 if (!SetAggregateOption(option_field, unknown_fields)) {
5202 class DescriptorBuilder::OptionInterpreter::AggregateOptionFinder
5203 : public TextFormat::Finder {
5205 DescriptorBuilder* builder_;
5207 virtual const FieldDescriptor* FindExtension(
5208 Message* message, const string& name) const {
5209 assert_mutex_held(builder_->pool_);
5210 const Descriptor* descriptor = message->GetDescriptor();
5211 Symbol result = builder_->LookupSymbolNoPlaceholder(
5212 name, descriptor->full_name());
5213 if (result.type == Symbol::FIELD &&
5214 result.field_descriptor->is_extension()) {
5215 return result.field_descriptor;
5216 } else if (result.type == Symbol::MESSAGE &&
5217 descriptor->options().message_set_wire_format()) {
5218 const Descriptor* foreign_type = result.descriptor;
5219 // The text format allows MessageSet items to be specified using
5220 // the type name, rather than the extension identifier. If the symbol
5221 // lookup returned a Message, and the enclosing Message has
5222 // message_set_wire_format = true, then return the message set
5223 // extension, if one exists.
5224 for (int i = 0; i < foreign_type->extension_count(); i++) {
5225 const FieldDescriptor* extension = foreign_type->extension(i);
5226 if (extension->containing_type() == descriptor &&
5227 extension->type() == FieldDescriptor::TYPE_MESSAGE &&
5228 extension->is_optional() &&
5229 extension->message_type() == foreign_type) {
5239 // A custom error collector to record any text-format parsing errors
5241 class AggregateErrorCollector : public io::ErrorCollector {
5245 virtual void AddError(int /* line */, int /* column */,
5246 const string& message) {
5247 if (!error_.empty()) {
5253 virtual void AddWarning(int /* line */, int /* column */,
5254 const string& /* message */) {
5260 // We construct a dynamic message of the type corresponding to
5261 // option_field, parse the supplied text-format string into this
5262 // message, and serialize the resulting message to produce the value.
5263 bool DescriptorBuilder::OptionInterpreter::SetAggregateOption(
5264 const FieldDescriptor* option_field,
5265 UnknownFieldSet* unknown_fields) {
5266 if (!uninterpreted_option_->has_aggregate_value()) {
5267 return AddValueError("Option \"" + option_field->full_name() +
5268 "\" is a message. To set the entire message, use "
5269 "syntax like \"" + option_field->name() +
5270 " = { <proto text format> }\". "
5271 "To set fields within it, use "
5272 "syntax like \"" + option_field->name() +
5276 const Descriptor* type = option_field->message_type();
5277 scoped_ptr<Message> dynamic(dynamic_factory_.GetPrototype(type)->New());
5278 GOOGLE_CHECK(dynamic.get() != NULL)
5279 << "Could not create an instance of " << option_field->DebugString();
5281 AggregateErrorCollector collector;
5282 AggregateOptionFinder finder;
5283 finder.builder_ = builder_;
5284 TextFormat::Parser parser;
5285 parser.RecordErrorsTo(&collector);
5286 parser.SetFinder(&finder);
5287 if (!parser.ParseFromString(uninterpreted_option_->aggregate_value(),
5289 AddValueError("Error while parsing option value for \"" +
5290 option_field->name() + "\": " + collector.error_);
5294 dynamic->SerializeToString(&serial); // Never fails
5295 if (option_field->type() == FieldDescriptor::TYPE_MESSAGE) {
5296 unknown_fields->AddLengthDelimited(option_field->number(), serial);
5298 GOOGLE_CHECK_EQ(option_field->type(), FieldDescriptor::TYPE_GROUP);
5299 UnknownFieldSet* group = unknown_fields->AddGroup(option_field->number());
5300 group->ParseFromString(serial);
5306 void DescriptorBuilder::OptionInterpreter::SetInt32(int number, int32 value,
5307 FieldDescriptor::Type type, UnknownFieldSet* unknown_fields) {
5309 case FieldDescriptor::TYPE_INT32:
5310 unknown_fields->AddVarint(number,
5311 static_cast<uint64>(static_cast<int64>(value)));
5314 case FieldDescriptor::TYPE_SFIXED32:
5315 unknown_fields->AddFixed32(number, static_cast<uint32>(value));
5318 case FieldDescriptor::TYPE_SINT32:
5319 unknown_fields->AddVarint(number,
5320 google::protobuf::internal::WireFormatLite::ZigZagEncode32(value));
5324 GOOGLE_LOG(FATAL) << "Invalid wire type for CPPTYPE_INT32: " << type;
5329 void DescriptorBuilder::OptionInterpreter::SetInt64(int number, int64 value,
5330 FieldDescriptor::Type type, UnknownFieldSet* unknown_fields) {
5332 case FieldDescriptor::TYPE_INT64:
5333 unknown_fields->AddVarint(number, static_cast<uint64>(value));
5336 case FieldDescriptor::TYPE_SFIXED64:
5337 unknown_fields->AddFixed64(number, static_cast<uint64>(value));
5340 case FieldDescriptor::TYPE_SINT64:
5341 unknown_fields->AddVarint(number,
5342 google::protobuf::internal::WireFormatLite::ZigZagEncode64(value));
5346 GOOGLE_LOG(FATAL) << "Invalid wire type for CPPTYPE_INT64: " << type;
5351 void DescriptorBuilder::OptionInterpreter::SetUInt32(int number, uint32 value,
5352 FieldDescriptor::Type type, UnknownFieldSet* unknown_fields) {
5354 case FieldDescriptor::TYPE_UINT32:
5355 unknown_fields->AddVarint(number, static_cast<uint64>(value));
5358 case FieldDescriptor::TYPE_FIXED32:
5359 unknown_fields->AddFixed32(number, static_cast<uint32>(value));
5363 GOOGLE_LOG(FATAL) << "Invalid wire type for CPPTYPE_UINT32: " << type;
5368 void DescriptorBuilder::OptionInterpreter::SetUInt64(int number, uint64 value,
5369 FieldDescriptor::Type type, UnknownFieldSet* unknown_fields) {
5371 case FieldDescriptor::TYPE_UINT64:
5372 unknown_fields->AddVarint(number, value);
5375 case FieldDescriptor::TYPE_FIXED64:
5376 unknown_fields->AddFixed64(number, value);
5380 GOOGLE_LOG(FATAL) << "Invalid wire type for CPPTYPE_UINT64: " << type;
5385 void DescriptorBuilder::LogUnusedDependency(const FileDescriptor* result) {
5387 if (!unused_dependency_.empty()) {
5388 std::set<string> annotation_extensions;
5389 annotation_extensions.insert("google.protobuf.MessageOptions");
5390 annotation_extensions.insert("google.protobuf.FileOptions");
5391 annotation_extensions.insert("google.protobuf.FieldOptions");
5392 annotation_extensions.insert("google.protobuf.EnumOptions");
5393 annotation_extensions.insert("google.protobuf.EnumValueOptions");
5394 annotation_extensions.insert("google.protobuf.ServiceOptions");
5395 annotation_extensions.insert("google.protobuf.MethodOptions");
5396 annotation_extensions.insert("google.protobuf.StreamOptions");
5397 for (set<const FileDescriptor*>::const_iterator
5398 it = unused_dependency_.begin();
5399 it != unused_dependency_.end(); ++it) {
5400 // Do not log warnings for proto files which extend annotations.
5402 for (i = 0 ; i < (*it)->extension_count(); ++i) {
5403 if (annotation_extensions.find(
5404 (*it)->extension(i)->containing_type()->full_name())
5405 != annotation_extensions.end()) {
5409 // Log warnings for unused imported files.
5410 if (i == (*it)->extension_count()) {
5411 GOOGLE_LOG(WARNING) << "Warning: Unused import: \"" << result->name()
5412 << "\" imports \"" << (*it)->name()
5413 << "\" which is not used.";
5419 } // namespace protobuf
5420 } // namespace google