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