诸暨麻将添加redis
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

7301 lines
283 KiB

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