PageRenderTime 41ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 1ms

/src/google/protobuf/descriptor.cc

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