诸暨麻将添加redis
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

2189 righe
91 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. //
  34. // This file contains classes which describe a type of protocol message.
  35. // You can use a message's descriptor to learn at runtime what fields
  36. // it contains and what the types of those fields are. The Message
  37. // interface also allows you to dynamically access and modify individual
  38. // fields by passing the FieldDescriptor of the field you are interested
  39. // in.
  40. //
  41. // Most users will not care about descriptors, because they will write
  42. // code specific to certain protocol types and will simply use the classes
  43. // generated by the protocol compiler directly. Advanced users who want
  44. // to operate on arbitrary types (not known at compile time) may want to
  45. // read descriptors in order to learn about the contents of a message.
  46. // A very small number of users will want to construct their own
  47. // Descriptors, either because they are implementing Message manually or
  48. // because they are writing something like the protocol compiler.
  49. //
  50. // For an example of how you might use descriptors, see the code example
  51. // at the top of message.h.
  52. #ifndef GOOGLE_PROTOBUF_DESCRIPTOR_H__
  53. #define GOOGLE_PROTOBUF_DESCRIPTOR_H__
  54. #include <memory>
  55. #include <set>
  56. #include <string>
  57. #include <vector>
  58. #include <google/protobuf/stubs/common.h>
  59. #include <google/protobuf/stubs/mutex.h>
  60. #include <google/protobuf/stubs/once.h>
  61. #include <google/protobuf/port_def.inc>
  62. // TYPE_BOOL is defined in the MacOS's ConditionalMacros.h.
  63. #ifdef TYPE_BOOL
  64. #undef TYPE_BOOL
  65. #endif // TYPE_BOOL
  66. #ifdef SWIG
  67. #define PROTOBUF_EXPORT
  68. #endif
  69. namespace google {
  70. namespace protobuf {
  71. // Defined in this file.
  72. class Descriptor;
  73. class FieldDescriptor;
  74. class OneofDescriptor;
  75. class EnumDescriptor;
  76. class EnumValueDescriptor;
  77. class ServiceDescriptor;
  78. class MethodDescriptor;
  79. class FileDescriptor;
  80. class DescriptorDatabase;
  81. class DescriptorPool;
  82. // Defined in descriptor.proto
  83. class DescriptorProto;
  84. class DescriptorProto_ExtensionRange;
  85. class FieldDescriptorProto;
  86. class OneofDescriptorProto;
  87. class EnumDescriptorProto;
  88. class EnumValueDescriptorProto;
  89. class ServiceDescriptorProto;
  90. class MethodDescriptorProto;
  91. class FileDescriptorProto;
  92. class MessageOptions;
  93. class FieldOptions;
  94. class OneofOptions;
  95. class EnumOptions;
  96. class EnumValueOptions;
  97. class ExtensionRangeOptions;
  98. class ServiceOptions;
  99. class MethodOptions;
  100. class FileOptions;
  101. class UninterpretedOption;
  102. class SourceCodeInfo;
  103. // Defined in message.h
  104. class Message;
  105. class Reflection;
  106. // Defined in descriptor.cc
  107. class DescriptorBuilder;
  108. class FileDescriptorTables;
  109. struct Symbol;
  110. // Defined in unknown_field_set.h.
  111. class UnknownField;
  112. // Defined in command_line_interface.cc
  113. namespace compiler {
  114. class CommandLineInterface;
  115. namespace cpp {
  116. // Defined in helpers.h
  117. class Formatter;
  118. } // namespace cpp
  119. } // namespace compiler
  120. namespace descriptor_unittest {
  121. class DescriptorTest;
  122. } // namespace descriptor_unittest
  123. // Defined in printer.h
  124. namespace io {
  125. class Printer;
  126. } // namespace io
  127. // NB, all indices are zero-based.
  128. struct SourceLocation {
  129. int start_line;
  130. int end_line;
  131. int start_column;
  132. int end_column;
  133. // Doc comments found at the source location.
  134. // See the comments in SourceCodeInfo.Location (descriptor.proto) for details.
  135. std::string leading_comments;
  136. std::string trailing_comments;
  137. std::vector<std::string> leading_detached_comments;
  138. };
  139. // Options when generating machine-parsable output from a descriptor with
  140. // DebugString().
  141. struct DebugStringOptions {
  142. // include original user comments as recorded in SourceLocation entries. N.B.
  143. // that this must be |false| by default: several other pieces of code (for
  144. // example, the C++ code generation for fields in the proto compiler) rely on
  145. // DebugString() output being unobstructed by user comments.
  146. bool include_comments;
  147. // If true, elide the braced body in the debug string.
  148. bool elide_group_body;
  149. bool elide_oneof_body;
  150. DebugStringOptions()
  151. : include_comments(false),
  152. elide_group_body(false),
  153. elide_oneof_body(false) {
  154. }
  155. };
  156. // A class to handle the simplest cases of a lazily linked descriptor
  157. // for a message type that isn't built at the time of cross linking,
  158. // which is needed when a pool has lazily_build_dependencies_ set.
  159. // Must be instantiated as mutable in a descriptor.
  160. namespace internal {
  161. class PROTOBUF_EXPORT LazyDescriptor {
  162. public:
  163. // Init function to be called at init time of a descriptor containing
  164. // a LazyDescriptor.
  165. void Init() {
  166. descriptor_ = nullptr;
  167. name_ = nullptr;
  168. once_ = nullptr;
  169. file_ = nullptr;
  170. }
  171. // Sets the value of the descriptor if it is known during the descriptor
  172. // building process. Not thread safe, should only be called during the
  173. // descriptor build process. Should not be called after SetLazy has been
  174. // called.
  175. void Set(const Descriptor* descriptor);
  176. // Sets the information needed to lazily cross link the descriptor at a later
  177. // time, SetLazy is not thread safe, should be called only once at descriptor
  178. // build time if the symbol wasn't found and building of the file containing
  179. // that type is delayed because lazily_build_dependencies_ is set on the pool.
  180. // Should not be called after Set() has been called.
  181. void SetLazy(const std::string& name, const FileDescriptor* file);
  182. // Returns the current value of the descriptor, thread-safe. If SetLazy(...)
  183. // has been called, will do a one-time cross link of the type specified,
  184. // building the descriptor file that contains the type if necessary.
  185. inline const Descriptor* Get() {
  186. Once();
  187. return descriptor_;
  188. }
  189. private:
  190. static void OnceStatic(LazyDescriptor* lazy);
  191. void OnceInternal();
  192. void Once();
  193. const Descriptor* descriptor_;
  194. const std::string* name_;
  195. internal::once_flag* once_;
  196. const FileDescriptor* file_;
  197. };
  198. } // namespace internal
  199. // Describes a type of protocol message, or a particular group within a
  200. // message. To obtain the Descriptor for a given message object, call
  201. // Message::GetDescriptor(). Generated message classes also have a
  202. // static method called descriptor() which returns the type's descriptor.
  203. // Use DescriptorPool to construct your own descriptors.
  204. class PROTOBUF_EXPORT Descriptor {
  205. public:
  206. typedef DescriptorProto Proto;
  207. // The name of the message type, not including its scope.
  208. const std::string& name() const;
  209. // The fully-qualified name of the message type, scope delimited by
  210. // periods. For example, message type "Foo" which is declared in package
  211. // "bar" has full name "bar.Foo". If a type "Baz" is nested within
  212. // Foo, Baz's full_name is "bar.Foo.Baz". To get only the part that
  213. // comes after the last '.', use name().
  214. const std::string& full_name() const;
  215. // Index of this descriptor within the file or containing type's message
  216. // type array.
  217. int index() const;
  218. // The .proto file in which this message type was defined. Never nullptr.
  219. const FileDescriptor* file() const;
  220. // If this Descriptor describes a nested type, this returns the type
  221. // in which it is nested. Otherwise, returns nullptr.
  222. const Descriptor* containing_type() const;
  223. // Get options for this message type. These are specified in the .proto file
  224. // by placing lines like "option foo = 1234;" in the message definition.
  225. // Allowed options are defined by MessageOptions in descriptor.proto, and any
  226. // available extensions of that message.
  227. const MessageOptions& options() const;
  228. // Write the contents of this Descriptor into the given DescriptorProto.
  229. // The target DescriptorProto must be clear before calling this; if it
  230. // isn't, the result may be garbage.
  231. void CopyTo(DescriptorProto* proto) const;
  232. // Write the contents of this decriptor in a human-readable form. Output
  233. // will be suitable for re-parsing.
  234. std::string DebugString() const;
  235. // Similar to DebugString(), but additionally takes options (e.g.,
  236. // include original user comments in output).
  237. std::string DebugStringWithOptions(const DebugStringOptions& options) const;
  238. // Returns true if this is a placeholder for an unknown type. This will
  239. // only be the case if this descriptor comes from a DescriptorPool
  240. // with AllowUnknownDependencies() set.
  241. bool is_placeholder() const;
  242. // Field stuff -----------------------------------------------------
  243. // The number of fields in this message type.
  244. int field_count() const;
  245. // Gets a field by index, where 0 <= index < field_count().
  246. // These are returned in the order they were defined in the .proto file.
  247. const FieldDescriptor* field(int index) const;
  248. // Looks up a field by declared tag number. Returns nullptr if no such field
  249. // exists.
  250. const FieldDescriptor* FindFieldByNumber(int number) const;
  251. // Looks up a field by name. Returns nullptr if no such field exists.
  252. const FieldDescriptor* FindFieldByName(const std::string& name) const;
  253. // Looks up a field by lowercased name (as returned by lowercase_name()).
  254. // This lookup may be ambiguous if multiple field names differ only by case,
  255. // in which case the field returned is chosen arbitrarily from the matches.
  256. const FieldDescriptor* FindFieldByLowercaseName(
  257. const std::string& lowercase_name) const;
  258. // Looks up a field by camel-case name (as returned by camelcase_name()).
  259. // This lookup may be ambiguous if multiple field names differ in a way that
  260. // leads them to have identical camel-case names, in which case the field
  261. // returned is chosen arbitrarily from the matches.
  262. const FieldDescriptor* FindFieldByCamelcaseName(
  263. const std::string& camelcase_name) const;
  264. // The number of oneofs in this message type.
  265. int oneof_decl_count() const;
  266. // Get a oneof by index, where 0 <= index < oneof_decl_count().
  267. // These are returned in the order they were defined in the .proto file.
  268. const OneofDescriptor* oneof_decl(int index) const;
  269. // Looks up a oneof by name. Returns nullptr if no such oneof exists.
  270. const OneofDescriptor* FindOneofByName(const std::string& name) const;
  271. // Nested type stuff -----------------------------------------------
  272. // The number of nested types in this message type.
  273. int nested_type_count() const;
  274. // Gets a nested type by index, where 0 <= index < nested_type_count().
  275. // These are returned in the order they were defined in the .proto file.
  276. const Descriptor* nested_type(int index) const;
  277. // Looks up a nested type by name. Returns nullptr if no such nested type
  278. // exists.
  279. const Descriptor* FindNestedTypeByName(const std::string& name) const;
  280. // Enum stuff ------------------------------------------------------
  281. // The number of enum types in this message type.
  282. int enum_type_count() const;
  283. // Gets an enum type by index, where 0 <= index < enum_type_count().
  284. // These are returned in the order they were defined in the .proto file.
  285. const EnumDescriptor* enum_type(int index) const;
  286. // Looks up an enum type by name. Returns nullptr if no such enum type
  287. // exists.
  288. const EnumDescriptor* FindEnumTypeByName(const std::string& name) const;
  289. // Looks up an enum value by name, among all enum types in this message.
  290. // Returns nullptr if no such value exists.
  291. const EnumValueDescriptor* FindEnumValueByName(const std::string& name) const;
  292. // Extensions ------------------------------------------------------
  293. // A range of field numbers which are designated for third-party
  294. // extensions.
  295. struct ExtensionRange {
  296. typedef DescriptorProto_ExtensionRange Proto;
  297. typedef ExtensionRangeOptions OptionsType;
  298. // See Descriptor::CopyTo().
  299. void CopyTo(DescriptorProto_ExtensionRange* proto) const;
  300. int start; // inclusive
  301. int end; // exclusive
  302. const ExtensionRangeOptions* options_;
  303. };
  304. // The number of extension ranges in this message type.
  305. int extension_range_count() const;
  306. // Gets an extension range by index, where 0 <= index <
  307. // extension_range_count(). These are returned in the order they were defined
  308. // in the .proto file.
  309. const ExtensionRange* extension_range(int index) const;
  310. // Returns true if the number is in one of the extension ranges.
  311. bool IsExtensionNumber(int number) const;
  312. // Returns nullptr if no extension range contains the given number.
  313. const ExtensionRange* FindExtensionRangeContainingNumber(int number) const;
  314. // The number of extensions -- extending *other* messages -- that were
  315. // defined nested within this message type's scope.
  316. int extension_count() const;
  317. // Get an extension by index, where 0 <= index < extension_count().
  318. // These are returned in the order they were defined in the .proto file.
  319. const FieldDescriptor* extension(int index) const;
  320. // Looks up a named extension (which extends some *other* message type)
  321. // defined within this message type's scope.
  322. const FieldDescriptor* FindExtensionByName(const std::string& name) const;
  323. // Similar to FindFieldByLowercaseName(), but finds extensions defined within
  324. // this message type's scope.
  325. const FieldDescriptor* FindExtensionByLowercaseName(
  326. const std::string& name) const;
  327. // Similar to FindFieldByCamelcaseName(), but finds extensions defined within
  328. // this message type's scope.
  329. const FieldDescriptor* FindExtensionByCamelcaseName(
  330. const std::string& name) const;
  331. // Reserved fields -------------------------------------------------
  332. // A range of reserved field numbers.
  333. struct ReservedRange {
  334. int start; // inclusive
  335. int end; // exclusive
  336. };
  337. // The number of reserved ranges in this message type.
  338. int reserved_range_count() const;
  339. // Gets an reserved range by index, where 0 <= index <
  340. // reserved_range_count(). These are returned in the order they were defined
  341. // in the .proto file.
  342. const ReservedRange* reserved_range(int index) const;
  343. // Returns true if the number is in one of the reserved ranges.
  344. bool IsReservedNumber(int number) const;
  345. // Returns nullptr if no reserved range contains the given number.
  346. const ReservedRange* FindReservedRangeContainingNumber(int number) const;
  347. // The number of reserved field names in this message type.
  348. int reserved_name_count() const;
  349. // Gets a reserved name by index, where 0 <= index < reserved_name_count().
  350. const std::string& reserved_name(int index) const;
  351. // Returns true if the field name is reserved.
  352. bool IsReservedName(const std::string& name) const;
  353. // Source Location ---------------------------------------------------
  354. // Updates |*out_location| to the source location of the complete
  355. // extent of this message declaration. Returns false and leaves
  356. // |*out_location| unchanged iff location information was not available.
  357. bool GetSourceLocation(SourceLocation* out_location) const;
  358. private:
  359. typedef MessageOptions OptionsType;
  360. // Allows tests to test CopyTo(proto, true).
  361. friend class descriptor_unittest::DescriptorTest;
  362. // Allows access to GetLocationPath for annotations.
  363. friend class io::Printer;
  364. friend class compiler::cpp::Formatter;
  365. // Fill the json_name field of FieldDescriptorProto.
  366. void CopyJsonNameTo(DescriptorProto* proto) const;
  367. // Internal version of DebugString; controls the level of indenting for
  368. // correct depth. Takes |options| to control debug-string options, and
  369. // |include_opening_clause| to indicate whether the "message ... " part of the
  370. // clause has already been generated (this varies depending on context).
  371. void DebugString(int depth, std::string* contents,
  372. const DebugStringOptions& options,
  373. bool include_opening_clause) const;
  374. // Walks up the descriptor tree to generate the source location path
  375. // to this descriptor from the file root.
  376. void GetLocationPath(std::vector<int>* output) const;
  377. const std::string* name_;
  378. const std::string* full_name_;
  379. const FileDescriptor* file_;
  380. const Descriptor* containing_type_;
  381. const MessageOptions* options_;
  382. // These arrays are separated from their sizes to minimize padding on 64-bit.
  383. FieldDescriptor* fields_;
  384. OneofDescriptor* oneof_decls_;
  385. Descriptor* nested_types_;
  386. EnumDescriptor* enum_types_;
  387. ExtensionRange* extension_ranges_;
  388. FieldDescriptor* extensions_;
  389. ReservedRange* reserved_ranges_;
  390. const std::string** reserved_names_;
  391. int field_count_;
  392. int oneof_decl_count_;
  393. int nested_type_count_;
  394. int enum_type_count_;
  395. int extension_range_count_;
  396. int extension_count_;
  397. int reserved_range_count_;
  398. int reserved_name_count_;
  399. // True if this is a placeholder for an unknown type.
  400. bool is_placeholder_;
  401. // True if this is a placeholder and the type name wasn't fully-qualified.
  402. bool is_unqualified_placeholder_;
  403. // IMPORTANT: If you add a new field, make sure to search for all instances
  404. // of Allocate<Descriptor>() and AllocateArray<Descriptor>() in descriptor.cc
  405. // and update them to initialize the field.
  406. // Must be constructed using DescriptorPool.
  407. Descriptor() {}
  408. friend class DescriptorBuilder;
  409. friend class DescriptorPool;
  410. friend class EnumDescriptor;
  411. friend class FieldDescriptor;
  412. friend class OneofDescriptor;
  413. friend class MethodDescriptor;
  414. friend class FileDescriptor;
  415. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Descriptor);
  416. };
  417. // Describes a single field of a message. To get the descriptor for a given
  418. // field, first get the Descriptor for the message in which it is defined,
  419. // then call Descriptor::FindFieldByName(). To get a FieldDescriptor for
  420. // an extension, do one of the following:
  421. // - Get the Descriptor or FileDescriptor for its containing scope, then
  422. // call Descriptor::FindExtensionByName() or
  423. // FileDescriptor::FindExtensionByName().
  424. // - Given a DescriptorPool, call DescriptorPool::FindExtensionByNumber() or
  425. // DescriptorPool::FindExtensionByPrintableName().
  426. // Use DescriptorPool to construct your own descriptors.
  427. class PROTOBUF_EXPORT FieldDescriptor {
  428. public:
  429. typedef FieldDescriptorProto Proto;
  430. // Identifies a field type. 0 is reserved for errors. The order is weird
  431. // for historical reasons. Types 12 and up are new in proto2.
  432. enum Type {
  433. TYPE_DOUBLE = 1, // double, exactly eight bytes on the wire.
  434. TYPE_FLOAT = 2, // float, exactly four bytes on the wire.
  435. TYPE_INT64 = 3, // int64, varint on the wire. Negative numbers
  436. // take 10 bytes. Use TYPE_SINT64 if negative
  437. // values are likely.
  438. TYPE_UINT64 = 4, // uint64, varint on the wire.
  439. TYPE_INT32 = 5, // int32, varint on the wire. Negative numbers
  440. // take 10 bytes. Use TYPE_SINT32 if negative
  441. // values are likely.
  442. TYPE_FIXED64 = 6, // uint64, exactly eight bytes on the wire.
  443. TYPE_FIXED32 = 7, // uint32, exactly four bytes on the wire.
  444. TYPE_BOOL = 8, // bool, varint on the wire.
  445. TYPE_STRING = 9, // UTF-8 text.
  446. TYPE_GROUP = 10, // Tag-delimited message. Deprecated.
  447. TYPE_MESSAGE = 11, // Length-delimited message.
  448. TYPE_BYTES = 12, // Arbitrary byte array.
  449. TYPE_UINT32 = 13, // uint32, varint on the wire
  450. TYPE_ENUM = 14, // Enum, varint on the wire
  451. TYPE_SFIXED32 = 15, // int32, exactly four bytes on the wire
  452. TYPE_SFIXED64 = 16, // int64, exactly eight bytes on the wire
  453. TYPE_SINT32 = 17, // int32, ZigZag-encoded varint on the wire
  454. TYPE_SINT64 = 18, // int64, ZigZag-encoded varint on the wire
  455. MAX_TYPE = 18, // Constant useful for defining lookup tables
  456. // indexed by Type.
  457. };
  458. // Specifies the C++ data type used to represent the field. There is a
  459. // fixed mapping from Type to CppType where each Type maps to exactly one
  460. // CppType. 0 is reserved for errors.
  461. enum CppType {
  462. CPPTYPE_INT32 = 1, // TYPE_INT32, TYPE_SINT32, TYPE_SFIXED32
  463. CPPTYPE_INT64 = 2, // TYPE_INT64, TYPE_SINT64, TYPE_SFIXED64
  464. CPPTYPE_UINT32 = 3, // TYPE_UINT32, TYPE_FIXED32
  465. CPPTYPE_UINT64 = 4, // TYPE_UINT64, TYPE_FIXED64
  466. CPPTYPE_DOUBLE = 5, // TYPE_DOUBLE
  467. CPPTYPE_FLOAT = 6, // TYPE_FLOAT
  468. CPPTYPE_BOOL = 7, // TYPE_BOOL
  469. CPPTYPE_ENUM = 8, // TYPE_ENUM
  470. CPPTYPE_STRING = 9, // TYPE_STRING, TYPE_BYTES
  471. CPPTYPE_MESSAGE = 10, // TYPE_MESSAGE, TYPE_GROUP
  472. MAX_CPPTYPE = 10, // Constant useful for defining lookup tables
  473. // indexed by CppType.
  474. };
  475. // Identifies whether the field is optional, required, or repeated. 0 is
  476. // reserved for errors.
  477. enum Label {
  478. LABEL_OPTIONAL = 1, // optional
  479. LABEL_REQUIRED = 2, // required
  480. LABEL_REPEATED = 3, // repeated
  481. MAX_LABEL = 3, // Constant useful for defining lookup tables
  482. // indexed by Label.
  483. };
  484. // Valid field numbers are positive integers up to kMaxNumber.
  485. static const int kMaxNumber = (1 << 29) - 1;
  486. // First field number reserved for the protocol buffer library implementation.
  487. // Users may not declare fields that use reserved numbers.
  488. static const int kFirstReservedNumber = 19000;
  489. // Last field number reserved for the protocol buffer library implementation.
  490. // Users may not declare fields that use reserved numbers.
  491. static const int kLastReservedNumber = 19999;
  492. const std::string& name() const; // Name of this field within the message.
  493. const std::string& full_name() const; // Fully-qualified name of the field.
  494. const std::string& json_name() const; // JSON name of this field.
  495. const FileDescriptor* file() const; // File in which this field was defined.
  496. bool is_extension() const; // Is this an extension field?
  497. int number() const; // Declared tag number.
  498. // Same as name() except converted to lower-case. This (and especially the
  499. // FindFieldByLowercaseName() method) can be useful when parsing formats
  500. // which prefer to use lowercase naming style. (Although, technically
  501. // field names should be lowercased anyway according to the protobuf style
  502. // guide, so this only makes a difference when dealing with old .proto files
  503. // which do not follow the guide.)
  504. const std::string& lowercase_name() const;
  505. // Same as name() except converted to camel-case. In this conversion, any
  506. // time an underscore appears in the name, it is removed and the next
  507. // letter is capitalized. Furthermore, the first letter of the name is
  508. // lower-cased. Examples:
  509. // FooBar -> fooBar
  510. // foo_bar -> fooBar
  511. // fooBar -> fooBar
  512. // This (and especially the FindFieldByCamelcaseName() method) can be useful
  513. // when parsing formats which prefer to use camel-case naming style.
  514. const std::string& camelcase_name() const;
  515. Type type() const; // Declared type of this field.
  516. const char* type_name() const; // Name of the declared type.
  517. CppType cpp_type() const; // C++ type of this field.
  518. const char* cpp_type_name() const; // Name of the C++ type.
  519. Label label() const; // optional/required/repeated
  520. bool is_required() const; // shorthand for label() == LABEL_REQUIRED
  521. bool is_optional() const; // shorthand for label() == LABEL_OPTIONAL
  522. bool is_repeated() const; // shorthand for label() == LABEL_REPEATED
  523. bool is_packable() const; // shorthand for is_repeated() &&
  524. // IsTypePackable(type())
  525. bool is_packed() const; // shorthand for is_packable() &&
  526. // options().packed()
  527. bool is_map() const; // shorthand for type() == TYPE_MESSAGE &&
  528. // message_type()->options().map_entry()
  529. // Index of this field within the message's field array, or the file or
  530. // extension scope's extensions array.
  531. int index() const;
  532. // Does this field have an explicitly-declared default value?
  533. bool has_default_value() const;
  534. // Whether the user has specified the json_name field option in the .proto
  535. // file.
  536. bool has_json_name() const;
  537. // Get the field default value if cpp_type() == CPPTYPE_INT32. If no
  538. // explicit default was defined, the default is 0.
  539. int32 default_value_int32() const;
  540. // Get the field default value if cpp_type() == CPPTYPE_INT64. If no
  541. // explicit default was defined, the default is 0.
  542. int64 default_value_int64() const;
  543. // Get the field default value if cpp_type() == CPPTYPE_UINT32. If no
  544. // explicit default was defined, the default is 0.
  545. uint32 default_value_uint32() const;
  546. // Get the field default value if cpp_type() == CPPTYPE_UINT64. If no
  547. // explicit default was defined, the default is 0.
  548. uint64 default_value_uint64() const;
  549. // Get the field default value if cpp_type() == CPPTYPE_FLOAT. If no
  550. // explicit default was defined, the default is 0.0.
  551. float default_value_float() const;
  552. // Get the field default value if cpp_type() == CPPTYPE_DOUBLE. If no
  553. // explicit default was defined, the default is 0.0.
  554. double default_value_double() const;
  555. // Get the field default value if cpp_type() == CPPTYPE_BOOL. If no
  556. // explicit default was defined, the default is false.
  557. bool default_value_bool() const;
  558. // Get the field default value if cpp_type() == CPPTYPE_ENUM. If no
  559. // explicit default was defined, the default is the first value defined
  560. // in the enum type (all enum types are required to have at least one value).
  561. // This never returns nullptr.
  562. const EnumValueDescriptor* default_value_enum() const;
  563. // Get the field default value if cpp_type() == CPPTYPE_STRING. If no
  564. // explicit default was defined, the default is the empty string.
  565. const std::string& default_value_string() const;
  566. // The Descriptor for the message of which this is a field. For extensions,
  567. // this is the extended type. Never nullptr.
  568. const Descriptor* containing_type() const;
  569. // If the field is a member of a oneof, this is the one, otherwise this is
  570. // nullptr.
  571. const OneofDescriptor* containing_oneof() const;
  572. // If the field is a member of a oneof, returns the index in that oneof.
  573. int index_in_oneof() const;
  574. // An extension may be declared within the scope of another message. If this
  575. // field is an extension (is_extension() is true), then extension_scope()
  576. // returns that message, or nullptr if the extension was declared at global
  577. // scope. If this is not an extension, extension_scope() is undefined (may
  578. // assert-fail).
  579. const Descriptor* extension_scope() const;
  580. // If type is TYPE_MESSAGE or TYPE_GROUP, returns a descriptor for the
  581. // message or the group type. Otherwise, returns null.
  582. const Descriptor* message_type() const;
  583. // If type is TYPE_ENUM, returns a descriptor for the enum. Otherwise,
  584. // returns null.
  585. const EnumDescriptor* enum_type() const;
  586. // Get the FieldOptions for this field. This includes things listed in
  587. // square brackets after the field definition. E.g., the field:
  588. // optional string text = 1 [ctype=CORD];
  589. // has the "ctype" option set. Allowed options are defined by FieldOptions in
  590. // descriptor.proto, and any available extensions of that message.
  591. const FieldOptions& options() const;
  592. // See Descriptor::CopyTo().
  593. void CopyTo(FieldDescriptorProto* proto) const;
  594. // See Descriptor::DebugString().
  595. std::string DebugString() const;
  596. // See Descriptor::DebugStringWithOptions().
  597. std::string DebugStringWithOptions(const DebugStringOptions& options) const;
  598. // Helper method to get the CppType for a particular Type.
  599. static CppType TypeToCppType(Type type);
  600. // Helper method to get the name of a Type.
  601. static const char* TypeName(Type type);
  602. // Helper method to get the name of a CppType.
  603. static const char* CppTypeName(CppType cpp_type);
  604. // Return true iff [packed = true] is valid for fields of this type.
  605. static inline bool IsTypePackable(Type field_type);
  606. // Returns full_name() except if the field is a MessageSet extension,
  607. // in which case it returns the full_name() of the containing message type
  608. // for backwards compatibility with proto1.
  609. //
  610. // A MessageSet extension is defined as an optional message extension
  611. // whose containing type has the message_set_wire_format option set.
  612. // This should be true of extensions of google.protobuf.bridge.MessageSet;
  613. // by convention, such extensions are named "message_set_extension".
  614. //
  615. // The opposite operation (looking up an extension's FieldDescriptor given
  616. // its printable name) can be accomplished with
  617. // message->file()->pool()->FindExtensionByPrintableName(message, name)
  618. // where the extension extends "message".
  619. const std::string& PrintableNameForExtension() const;
  620. // Source Location ---------------------------------------------------
  621. // Updates |*out_location| to the source location of the complete
  622. // extent of this field declaration. Returns false and leaves
  623. // |*out_location| unchanged iff location information was not available.
  624. bool GetSourceLocation(SourceLocation* out_location) const;
  625. private:
  626. typedef FieldOptions OptionsType;
  627. // Allows access to GetLocationPath for annotations.
  628. friend class io::Printer;
  629. friend class compiler::cpp::Formatter;
  630. // Fill the json_name field of FieldDescriptorProto.
  631. void CopyJsonNameTo(FieldDescriptorProto* proto) const;
  632. // See Descriptor::DebugString().
  633. enum PrintLabelFlag { PRINT_LABEL, OMIT_LABEL };
  634. void DebugString(int depth, PrintLabelFlag print_label_flag,
  635. std::string* contents,
  636. const DebugStringOptions& options) const;
  637. // formats the default value appropriately and returns it as a string.
  638. // Must have a default value to call this. If quote_string_type is true, then
  639. // types of CPPTYPE_STRING whill be surrounded by quotes and CEscaped.
  640. std::string DefaultValueAsString(bool quote_string_type) const;
  641. // Helper function that returns the field type name for DebugString.
  642. std::string FieldTypeNameDebugString() const;
  643. // Walks up the descriptor tree to generate the source location path
  644. // to this descriptor from the file root.
  645. void GetLocationPath(std::vector<int>* output) const;
  646. // Returns true if this is a map message type.
  647. bool is_map_message_type() const;
  648. const std::string* name_;
  649. const std::string* full_name_;
  650. const std::string* lowercase_name_;
  651. const std::string* camelcase_name_;
  652. // If has_json_name_ is true, it's the value specified by the user.
  653. // Otherwise, it has the same value as camelcase_name_.
  654. const std::string* json_name_;
  655. const FileDescriptor* file_;
  656. internal::once_flag* type_once_;
  657. static void TypeOnceInit(const FieldDescriptor* to_init);
  658. void InternalTypeOnceInit() const;
  659. mutable Type type_;
  660. Label label_;
  661. bool has_default_value_;
  662. // Whether the user has specified the json_name field option in the .proto
  663. // file.
  664. bool has_json_name_;
  665. bool is_extension_;
  666. int number_;
  667. int index_in_oneof_;
  668. const Descriptor* containing_type_;
  669. const OneofDescriptor* containing_oneof_;
  670. const Descriptor* extension_scope_;
  671. mutable const Descriptor* message_type_;
  672. mutable const EnumDescriptor* enum_type_;
  673. const FieldOptions* options_;
  674. const std::string* type_name_;
  675. const std::string* default_value_enum_name_;
  676. // IMPORTANT: If you add a new field, make sure to search for all instances
  677. // of Allocate<FieldDescriptor>() and AllocateArray<FieldDescriptor>() in
  678. // descriptor.cc and update them to initialize the field.
  679. union {
  680. int32 default_value_int32_;
  681. int64 default_value_int64_;
  682. uint32 default_value_uint32_;
  683. uint64 default_value_uint64_;
  684. float default_value_float_;
  685. double default_value_double_;
  686. bool default_value_bool_;
  687. mutable const EnumValueDescriptor* default_value_enum_;
  688. const std::string* default_value_string_;
  689. };
  690. static const CppType kTypeToCppTypeMap[MAX_TYPE + 1];
  691. static const char* const kTypeToName[MAX_TYPE + 1];
  692. static const char* const kCppTypeToName[MAX_CPPTYPE + 1];
  693. static const char* const kLabelToName[MAX_LABEL + 1];
  694. // Must be constructed using DescriptorPool.
  695. FieldDescriptor() {}
  696. friend class DescriptorBuilder;
  697. friend class FileDescriptor;
  698. friend class Descriptor;
  699. friend class OneofDescriptor;
  700. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FieldDescriptor);
  701. };
  702. // Describes a oneof defined in a message type.
  703. class PROTOBUF_EXPORT OneofDescriptor {
  704. public:
  705. typedef OneofDescriptorProto Proto;
  706. const std::string& name() const; // Name of this oneof.
  707. const std::string& full_name() const; // Fully-qualified name of the oneof.
  708. // Index of this oneof within the message's oneof array.
  709. int index() const;
  710. // The .proto file in which this oneof was defined. Never nullptr.
  711. const FileDescriptor* file() const;
  712. // The Descriptor for the message containing this oneof.
  713. const Descriptor* containing_type() const;
  714. // The number of (non-extension) fields which are members of this oneof.
  715. int field_count() const;
  716. // Get a member of this oneof, in the order in which they were declared in the
  717. // .proto file. Does not include extensions.
  718. const FieldDescriptor* field(int index) const;
  719. const OneofOptions& options() const;
  720. // See Descriptor::CopyTo().
  721. void CopyTo(OneofDescriptorProto* proto) const;
  722. // See Descriptor::DebugString().
  723. std::string DebugString() const;
  724. // See Descriptor::DebugStringWithOptions().
  725. std::string DebugStringWithOptions(const DebugStringOptions& options) const;
  726. // Source Location ---------------------------------------------------
  727. // Updates |*out_location| to the source location of the complete
  728. // extent of this oneof declaration. Returns false and leaves
  729. // |*out_location| unchanged iff location information was not available.
  730. bool GetSourceLocation(SourceLocation* out_location) const;
  731. private:
  732. typedef OneofOptions OptionsType;
  733. // Allows access to GetLocationPath for annotations.
  734. friend class io::Printer;
  735. friend class compiler::cpp::Formatter;
  736. // See Descriptor::DebugString().
  737. void DebugString(int depth, std::string* contents,
  738. const DebugStringOptions& options) const;
  739. // Walks up the descriptor tree to generate the source location path
  740. // to this descriptor from the file root.
  741. void GetLocationPath(std::vector<int>* output) const;
  742. const std::string* name_;
  743. const std::string* full_name_;
  744. const Descriptor* containing_type_;
  745. int field_count_;
  746. const FieldDescriptor** fields_;
  747. const OneofOptions* options_;
  748. // IMPORTANT: If you add a new field, make sure to search for all instances
  749. // of Allocate<OneofDescriptor>() and AllocateArray<OneofDescriptor>()
  750. // in descriptor.cc and update them to initialize the field.
  751. // Must be constructed using DescriptorPool.
  752. OneofDescriptor() {}
  753. friend class DescriptorBuilder;
  754. friend class Descriptor;
  755. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(OneofDescriptor);
  756. };
  757. // Describes an enum type defined in a .proto file. To get the EnumDescriptor
  758. // for a generated enum type, call TypeName_descriptor(). Use DescriptorPool
  759. // to construct your own descriptors.
  760. class PROTOBUF_EXPORT EnumDescriptor {
  761. public:
  762. typedef EnumDescriptorProto Proto;
  763. // The name of this enum type in the containing scope.
  764. const std::string& name() const;
  765. // The fully-qualified name of the enum type, scope delimited by periods.
  766. const std::string& full_name() const;
  767. // Index of this enum within the file or containing message's enum array.
  768. int index() const;
  769. // The .proto file in which this enum type was defined. Never nullptr.
  770. const FileDescriptor* file() const;
  771. // The number of values for this EnumDescriptor. Guaranteed to be greater
  772. // than zero.
  773. int value_count() const;
  774. // Gets a value by index, where 0 <= index < value_count().
  775. // These are returned in the order they were defined in the .proto file.
  776. const EnumValueDescriptor* value(int index) const;
  777. // Looks up a value by name. Returns nullptr if no such value exists.
  778. const EnumValueDescriptor* FindValueByName(const std::string& name) const;
  779. // Looks up a value by number. Returns nullptr if no such value exists. If
  780. // multiple values have this number, the first one defined is returned.
  781. const EnumValueDescriptor* FindValueByNumber(int number) const;
  782. // If this enum type is nested in a message type, this is that message type.
  783. // Otherwise, nullptr.
  784. const Descriptor* containing_type() const;
  785. // Get options for this enum type. These are specified in the .proto file by
  786. // placing lines like "option foo = 1234;" in the enum definition. Allowed
  787. // options are defined by EnumOptions in descriptor.proto, and any available
  788. // extensions of that message.
  789. const EnumOptions& options() const;
  790. // See Descriptor::CopyTo().
  791. void CopyTo(EnumDescriptorProto* proto) const;
  792. // See Descriptor::DebugString().
  793. std::string DebugString() const;
  794. // See Descriptor::DebugStringWithOptions().
  795. std::string DebugStringWithOptions(const DebugStringOptions& options) const;
  796. // Returns true if this is a placeholder for an unknown enum. This will
  797. // only be the case if this descriptor comes from a DescriptorPool
  798. // with AllowUnknownDependencies() set.
  799. bool is_placeholder() const;
  800. // Reserved fields -------------------------------------------------
  801. // A range of reserved field numbers.
  802. struct ReservedRange {
  803. int start; // inclusive
  804. int end; // inclusive
  805. };
  806. // The number of reserved ranges in this message type.
  807. int reserved_range_count() const;
  808. // Gets an reserved range by index, where 0 <= index <
  809. // reserved_range_count(). These are returned in the order they were defined
  810. // in the .proto file.
  811. const EnumDescriptor::ReservedRange* reserved_range(int index) const;
  812. // Returns true if the number is in one of the reserved ranges.
  813. bool IsReservedNumber(int number) const;
  814. // Returns nullptr if no reserved range contains the given number.
  815. const EnumDescriptor::ReservedRange* FindReservedRangeContainingNumber(
  816. int number) const;
  817. // The number of reserved field names in this message type.
  818. int reserved_name_count() const;
  819. // Gets a reserved name by index, where 0 <= index < reserved_name_count().
  820. const std::string& reserved_name(int index) const;
  821. // Returns true if the field name is reserved.
  822. bool IsReservedName(const std::string& name) const;
  823. // Source Location ---------------------------------------------------
  824. // Updates |*out_location| to the source location of the complete
  825. // extent of this enum declaration. Returns false and leaves
  826. // |*out_location| unchanged iff location information was not available.
  827. bool GetSourceLocation(SourceLocation* out_location) const;
  828. private:
  829. typedef EnumOptions OptionsType;
  830. // Allows access to GetLocationPath for annotations.
  831. friend class io::Printer;
  832. friend class compiler::cpp::Formatter;
  833. // Looks up a value by number. If the value does not exist, dynamically
  834. // creates a new EnumValueDescriptor for that value, assuming that it was
  835. // unknown. If a new descriptor is created, this is done in a thread-safe way,
  836. // and future calls will return the same value descriptor pointer.
  837. //
  838. // This is private but is used by Reflection (which is friended below) to
  839. // return a valid EnumValueDescriptor from GetEnum() when this feature is
  840. // enabled.
  841. const EnumValueDescriptor* FindValueByNumberCreatingIfUnknown(
  842. int number) const;
  843. // See Descriptor::DebugString().
  844. void DebugString(int depth, std::string* contents,
  845. const DebugStringOptions& options) const;
  846. // Walks up the descriptor tree to generate the source location path
  847. // to this descriptor from the file root.
  848. void GetLocationPath(std::vector<int>* output) const;
  849. const std::string* name_;
  850. const std::string* full_name_;
  851. const FileDescriptor* file_;
  852. const Descriptor* containing_type_;
  853. const EnumOptions* options_;
  854. // True if this is a placeholder for an unknown type.
  855. bool is_placeholder_;
  856. // True if this is a placeholder and the type name wasn't fully-qualified.
  857. bool is_unqualified_placeholder_;
  858. int value_count_;
  859. EnumValueDescriptor* values_;
  860. int reserved_range_count_;
  861. int reserved_name_count_;
  862. EnumDescriptor::ReservedRange* reserved_ranges_;
  863. const std::string** reserved_names_;
  864. // IMPORTANT: If you add a new field, make sure to search for all instances
  865. // of Allocate<EnumDescriptor>() and AllocateArray<EnumDescriptor>() in
  866. // descriptor.cc and update them to initialize the field.
  867. // Must be constructed using DescriptorPool.
  868. EnumDescriptor() {}
  869. friend class DescriptorBuilder;
  870. friend class Descriptor;
  871. friend class FieldDescriptor;
  872. friend class EnumValueDescriptor;
  873. friend class FileDescriptor;
  874. friend class DescriptorPool;
  875. friend class Reflection;
  876. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(EnumDescriptor);
  877. };
  878. // Describes an individual enum constant of a particular type. To get the
  879. // EnumValueDescriptor for a given enum value, first get the EnumDescriptor
  880. // for its type, then use EnumDescriptor::FindValueByName() or
  881. // EnumDescriptor::FindValueByNumber(). Use DescriptorPool to construct
  882. // your own descriptors.
  883. class PROTOBUF_EXPORT EnumValueDescriptor {
  884. public:
  885. typedef EnumValueDescriptorProto Proto;
  886. const std::string& name() const; // Name of this enum constant.
  887. int index() const; // Index within the enums's Descriptor.
  888. int number() const; // Numeric value of this enum constant.
  889. // The full_name of an enum value is a sibling symbol of the enum type.
  890. // e.g. the full name of FieldDescriptorProto::TYPE_INT32 is actually
  891. // "google.protobuf.FieldDescriptorProto.TYPE_INT32", NOT
  892. // "google.protobuf.FieldDescriptorProto.Type.TYPE_INT32". This is to conform
  893. // with C++ scoping rules for enums.
  894. const std::string& full_name() const;
  895. // The .proto file in which this value was defined. Never nullptr.
  896. const FileDescriptor* file() const;
  897. // The type of this value. Never nullptr.
  898. const EnumDescriptor* type() const;
  899. // Get options for this enum value. These are specified in the .proto file by
  900. // adding text like "[foo = 1234]" after an enum value definition. Allowed
  901. // options are defined by EnumValueOptions in descriptor.proto, and any
  902. // available extensions of that message.
  903. const EnumValueOptions& options() const;
  904. // See Descriptor::CopyTo().
  905. void CopyTo(EnumValueDescriptorProto* proto) const;
  906. // See Descriptor::DebugString().
  907. std::string DebugString() const;
  908. // See Descriptor::DebugStringWithOptions().
  909. std::string DebugStringWithOptions(const DebugStringOptions& options) const;
  910. // Source Location ---------------------------------------------------
  911. // Updates |*out_location| to the source location of the complete
  912. // extent of this enum value declaration. Returns false and leaves
  913. // |*out_location| unchanged iff location information was not available.
  914. bool GetSourceLocation(SourceLocation* out_location) const;
  915. private:
  916. typedef EnumValueOptions OptionsType;
  917. // Allows access to GetLocationPath for annotations.
  918. friend class io::Printer;
  919. friend class compiler::cpp::Formatter;
  920. // See Descriptor::DebugString().
  921. void DebugString(int depth, std::string* contents,
  922. const DebugStringOptions& options) const;
  923. // Walks up the descriptor tree to generate the source location path
  924. // to this descriptor from the file root.
  925. void GetLocationPath(std::vector<int>* output) const;
  926. const std::string* name_;
  927. const std::string* full_name_;
  928. int number_;
  929. const EnumDescriptor* type_;
  930. const EnumValueOptions* options_;
  931. // IMPORTANT: If you add a new field, make sure to search for all instances
  932. // of Allocate<EnumValueDescriptor>() and AllocateArray<EnumValueDescriptor>()
  933. // in descriptor.cc and update them to initialize the field.
  934. // Must be constructed using DescriptorPool.
  935. EnumValueDescriptor() {}
  936. friend class DescriptorBuilder;
  937. friend class EnumDescriptor;
  938. friend class DescriptorPool;
  939. friend class FileDescriptorTables;
  940. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(EnumValueDescriptor);
  941. };
  942. // Describes an RPC service. Use DescriptorPool to construct your own
  943. // descriptors.
  944. class PROTOBUF_EXPORT ServiceDescriptor {
  945. public:
  946. typedef ServiceDescriptorProto Proto;
  947. // The name of the service, not including its containing scope.
  948. const std::string& name() const;
  949. // The fully-qualified name of the service, scope delimited by periods.
  950. const std::string& full_name() const;
  951. // Index of this service within the file's services array.
  952. int index() const;
  953. // The .proto file in which this service was defined. Never nullptr.
  954. const FileDescriptor* file() const;
  955. // Get options for this service type. These are specified in the .proto file
  956. // by placing lines like "option foo = 1234;" in the service definition.
  957. // Allowed options are defined by ServiceOptions in descriptor.proto, and any
  958. // available extensions of that message.
  959. const ServiceOptions& options() const;
  960. // The number of methods this service defines.
  961. int method_count() const;
  962. // Gets a MethodDescriptor by index, where 0 <= index < method_count().
  963. // These are returned in the order they were defined in the .proto file.
  964. const MethodDescriptor* method(int index) const;
  965. // Look up a MethodDescriptor by name.
  966. const MethodDescriptor* FindMethodByName(const std::string& name) const;
  967. // See Descriptor::CopyTo().
  968. void CopyTo(ServiceDescriptorProto* proto) const;
  969. // See Descriptor::DebugString().
  970. std::string DebugString() const;
  971. // See Descriptor::DebugStringWithOptions().
  972. std::string DebugStringWithOptions(const DebugStringOptions& options) const;
  973. // Source Location ---------------------------------------------------
  974. // Updates |*out_location| to the source location of the complete
  975. // extent of this service declaration. Returns false and leaves
  976. // |*out_location| unchanged iff location information was not available.
  977. bool GetSourceLocation(SourceLocation* out_location) const;
  978. private:
  979. typedef ServiceOptions OptionsType;
  980. // Allows access to GetLocationPath for annotations.
  981. friend class io::Printer;
  982. friend class compiler::cpp::Formatter;
  983. // See Descriptor::DebugString().
  984. void DebugString(std::string* contents,
  985. const DebugStringOptions& options) const;
  986. // Walks up the descriptor tree to generate the source location path
  987. // to this descriptor from the file root.
  988. void GetLocationPath(std::vector<int>* output) const;
  989. const std::string* name_;
  990. const std::string* full_name_;
  991. const FileDescriptor* file_;
  992. const ServiceOptions* options_;
  993. MethodDescriptor* methods_;
  994. int method_count_;
  995. // IMPORTANT: If you add a new field, make sure to search for all instances
  996. // of Allocate<ServiceDescriptor>() and AllocateArray<ServiceDescriptor>() in
  997. // descriptor.cc and update them to initialize the field.
  998. // Must be constructed using DescriptorPool.
  999. ServiceDescriptor() {}
  1000. friend class DescriptorBuilder;
  1001. friend class FileDescriptor;
  1002. friend class MethodDescriptor;
  1003. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ServiceDescriptor);
  1004. };
  1005. // Describes an individual service method. To obtain a MethodDescriptor given
  1006. // a service, first get its ServiceDescriptor, then call
  1007. // ServiceDescriptor::FindMethodByName(). Use DescriptorPool to construct your
  1008. // own descriptors.
  1009. class PROTOBUF_EXPORT MethodDescriptor {
  1010. public:
  1011. typedef MethodDescriptorProto Proto;
  1012. // Name of this method, not including containing scope.
  1013. const std::string& name() const;
  1014. // The fully-qualified name of the method, scope delimited by periods.
  1015. const std::string& full_name() const;
  1016. // Index within the service's Descriptor.
  1017. int index() const;
  1018. // The .proto file in which this method was defined. Never nullptr.
  1019. const FileDescriptor* file() const;
  1020. // Gets the service to which this method belongs. Never nullptr.
  1021. const ServiceDescriptor* service() const;
  1022. // Gets the type of protocol message which this method accepts as input.
  1023. const Descriptor* input_type() const;
  1024. // Gets the type of protocol message which this message produces as output.
  1025. const Descriptor* output_type() const;
  1026. // Gets whether the client streams multiple requests.
  1027. bool client_streaming() const;
  1028. // Gets whether the server streams multiple responses.
  1029. bool server_streaming() const;
  1030. // Get options for this method. These are specified in the .proto file by
  1031. // placing lines like "option foo = 1234;" in curly-braces after a method
  1032. // declaration. Allowed options are defined by MethodOptions in
  1033. // descriptor.proto, and any available extensions of that message.
  1034. const MethodOptions& options() const;
  1035. // See Descriptor::CopyTo().
  1036. void CopyTo(MethodDescriptorProto* proto) const;
  1037. // See Descriptor::DebugString().
  1038. std::string DebugString() const;
  1039. // See Descriptor::DebugStringWithOptions().
  1040. std::string DebugStringWithOptions(const DebugStringOptions& options) const;
  1041. // Source Location ---------------------------------------------------
  1042. // Updates |*out_location| to the source location of the complete
  1043. // extent of this method declaration. Returns false and leaves
  1044. // |*out_location| unchanged iff location information was not available.
  1045. bool GetSourceLocation(SourceLocation* out_location) const;
  1046. private:
  1047. typedef MethodOptions OptionsType;
  1048. // Allows access to GetLocationPath for annotations.
  1049. friend class io::Printer;
  1050. friend class compiler::cpp::Formatter;
  1051. // See Descriptor::DebugString().
  1052. void DebugString(int depth, std::string* contents,
  1053. const DebugStringOptions& options) const;
  1054. // Walks up the descriptor tree to generate the source location path
  1055. // to this descriptor from the file root.
  1056. void GetLocationPath(std::vector<int>* output) const;
  1057. const std::string* name_;
  1058. const std::string* full_name_;
  1059. const ServiceDescriptor* service_;
  1060. mutable internal::LazyDescriptor input_type_;
  1061. mutable internal::LazyDescriptor output_type_;
  1062. const MethodOptions* options_;
  1063. bool client_streaming_;
  1064. bool server_streaming_;
  1065. // IMPORTANT: If you add a new field, make sure to search for all instances
  1066. // of Allocate<MethodDescriptor>() and AllocateArray<MethodDescriptor>() in
  1067. // descriptor.cc and update them to initialize the field.
  1068. // Must be constructed using DescriptorPool.
  1069. MethodDescriptor() {}
  1070. friend class DescriptorBuilder;
  1071. friend class ServiceDescriptor;
  1072. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MethodDescriptor);
  1073. };
  1074. // Describes a whole .proto file. To get the FileDescriptor for a compiled-in
  1075. // file, get the descriptor for something defined in that file and call
  1076. // descriptor->file(). Use DescriptorPool to construct your own descriptors.
  1077. class PROTOBUF_EXPORT FileDescriptor {
  1078. public:
  1079. typedef FileDescriptorProto Proto;
  1080. // The filename, relative to the source tree.
  1081. // e.g. "foo/bar/baz.proto"
  1082. const std::string& name() const;
  1083. // The package, e.g. "google.protobuf.compiler".
  1084. const std::string& package() const;
  1085. // The DescriptorPool in which this FileDescriptor and all its contents were
  1086. // allocated. Never nullptr.
  1087. const DescriptorPool* pool() const;
  1088. // The number of files imported by this one.
  1089. int dependency_count() const;
  1090. // Gets an imported file by index, where 0 <= index < dependency_count().
  1091. // These are returned in the order they were defined in the .proto file.
  1092. const FileDescriptor* dependency(int index) const;
  1093. // The number of files public imported by this one.
  1094. // The public dependency list is a subset of the dependency list.
  1095. int public_dependency_count() const;
  1096. // Gets a public imported file by index, where 0 <= index <
  1097. // public_dependency_count().
  1098. // These are returned in the order they were defined in the .proto file.
  1099. const FileDescriptor* public_dependency(int index) const;
  1100. // The number of files that are imported for weak fields.
  1101. // The weak dependency list is a subset of the dependency list.
  1102. int weak_dependency_count() const;
  1103. // Gets a weak imported file by index, where 0 <= index <
  1104. // weak_dependency_count().
  1105. // These are returned in the order they were defined in the .proto file.
  1106. const FileDescriptor* weak_dependency(int index) const;
  1107. // Number of top-level message types defined in this file. (This does not
  1108. // include nested types.)
  1109. int message_type_count() const;
  1110. // Gets a top-level message type, where 0 <= index < message_type_count().
  1111. // These are returned in the order they were defined in the .proto file.
  1112. const Descriptor* message_type(int index) const;
  1113. // Number of top-level enum types defined in this file. (This does not
  1114. // include nested types.)
  1115. int enum_type_count() const;
  1116. // Gets a top-level enum type, where 0 <= index < enum_type_count().
  1117. // These are returned in the order they were defined in the .proto file.
  1118. const EnumDescriptor* enum_type(int index) const;
  1119. // Number of services defined in this file.
  1120. int service_count() const;
  1121. // Gets a service, where 0 <= index < service_count().
  1122. // These are returned in the order they were defined in the .proto file.
  1123. const ServiceDescriptor* service(int index) const;
  1124. // Number of extensions defined at file scope. (This does not include
  1125. // extensions nested within message types.)
  1126. int extension_count() const;
  1127. // Gets an extension's descriptor, where 0 <= index < extension_count().
  1128. // These are returned in the order they were defined in the .proto file.
  1129. const FieldDescriptor* extension(int index) const;
  1130. // Get options for this file. These are specified in the .proto file by
  1131. // placing lines like "option foo = 1234;" at the top level, outside of any
  1132. // other definitions. Allowed options are defined by FileOptions in
  1133. // descriptor.proto, and any available extensions of that message.
  1134. const FileOptions& options() const;
  1135. // Syntax of this file.
  1136. enum Syntax {
  1137. SYNTAX_UNKNOWN = 0,
  1138. SYNTAX_PROTO2 = 2,
  1139. SYNTAX_PROTO3 = 3,
  1140. };
  1141. Syntax syntax() const;
  1142. static const char* SyntaxName(Syntax syntax);
  1143. // Find a top-level message type by name. Returns nullptr if not found.
  1144. const Descriptor* FindMessageTypeByName(const std::string& name) const;
  1145. // Find a top-level enum type by name. Returns nullptr if not found.
  1146. const EnumDescriptor* FindEnumTypeByName(const std::string& name) const;
  1147. // Find an enum value defined in any top-level enum by name. Returns nullptr
  1148. // if not found.
  1149. const EnumValueDescriptor* FindEnumValueByName(const std::string& name) const;
  1150. // Find a service definition by name. Returns nullptr if not found.
  1151. const ServiceDescriptor* FindServiceByName(const std::string& name) const;
  1152. // Find a top-level extension definition by name. Returns nullptr if not
  1153. // found.
  1154. const FieldDescriptor* FindExtensionByName(const std::string& name) const;
  1155. // Similar to FindExtensionByName(), but searches by lowercased-name. See
  1156. // Descriptor::FindFieldByLowercaseName().
  1157. const FieldDescriptor* FindExtensionByLowercaseName(
  1158. const std::string& name) const;
  1159. // Similar to FindExtensionByName(), but searches by camelcased-name. See
  1160. // Descriptor::FindFieldByCamelcaseName().
  1161. const FieldDescriptor* FindExtensionByCamelcaseName(
  1162. const std::string& name) const;
  1163. // See Descriptor::CopyTo().
  1164. // Notes:
  1165. // - This method does NOT copy source code information since it is relatively
  1166. // large and rarely needed. See CopySourceCodeInfoTo() below.
  1167. void CopyTo(FileDescriptorProto* proto) const;
  1168. // Write the source code information of this FileDescriptor into the given
  1169. // FileDescriptorProto. See CopyTo() above.
  1170. void CopySourceCodeInfoTo(FileDescriptorProto* proto) const;
  1171. // Fill the json_name field of FieldDescriptorProto for all fields. Can only
  1172. // be called after CopyTo().
  1173. void CopyJsonNameTo(FileDescriptorProto* proto) const;
  1174. // See Descriptor::DebugString().
  1175. std::string DebugString() const;
  1176. // See Descriptor::DebugStringWithOptions().
  1177. std::string DebugStringWithOptions(const DebugStringOptions& options) const;
  1178. // Returns true if this is a placeholder for an unknown file. This will
  1179. // only be the case if this descriptor comes from a DescriptorPool
  1180. // with AllowUnknownDependencies() set.
  1181. bool is_placeholder() const;
  1182. // Updates |*out_location| to the source location of the complete extent of
  1183. // this file declaration (namely, the empty path).
  1184. bool GetSourceLocation(SourceLocation* out_location) const;
  1185. // Updates |*out_location| to the source location of the complete
  1186. // extent of the declaration or declaration-part denoted by |path|.
  1187. // Returns false and leaves |*out_location| unchanged iff location
  1188. // information was not available. (See SourceCodeInfo for
  1189. // description of path encoding.)
  1190. bool GetSourceLocation(const std::vector<int>& path,
  1191. SourceLocation* out_location) const;
  1192. private:
  1193. typedef FileOptions OptionsType;
  1194. const std::string* name_;
  1195. const std::string* package_;
  1196. const DescriptorPool* pool_;
  1197. internal::once_flag* dependencies_once_;
  1198. static void DependenciesOnceInit(const FileDescriptor* to_init);
  1199. void InternalDependenciesOnceInit() const;
  1200. // These are arranged to minimze padding on 64-bit.
  1201. int dependency_count_;
  1202. int public_dependency_count_;
  1203. int weak_dependency_count_;
  1204. int message_type_count_;
  1205. int enum_type_count_;
  1206. int service_count_;
  1207. int extension_count_;
  1208. Syntax syntax_;
  1209. bool is_placeholder_;
  1210. // Indicates the FileDescriptor is completed building. Used to verify
  1211. // that type accessor functions that can possibly build a dependent file
  1212. // aren't called during the process of building the file.
  1213. bool finished_building_;
  1214. mutable const FileDescriptor** dependencies_;
  1215. const std::string** dependencies_names_;
  1216. int* public_dependencies_;
  1217. int* weak_dependencies_;
  1218. Descriptor* message_types_;
  1219. EnumDescriptor* enum_types_;
  1220. ServiceDescriptor* services_;
  1221. FieldDescriptor* extensions_;
  1222. const FileOptions* options_;
  1223. const FileDescriptorTables* tables_;
  1224. const SourceCodeInfo* source_code_info_;
  1225. // IMPORTANT: If you add a new field, make sure to search for all instances
  1226. // of Allocate<FileDescriptor>() and AllocateArray<FileDescriptor>() in
  1227. // descriptor.cc and update them to initialize the field.
  1228. FileDescriptor() {}
  1229. friend class DescriptorBuilder;
  1230. friend class DescriptorPool;
  1231. friend class Descriptor;
  1232. friend class FieldDescriptor;
  1233. friend class internal::LazyDescriptor;
  1234. friend class OneofDescriptor;
  1235. friend class EnumDescriptor;
  1236. friend class EnumValueDescriptor;
  1237. friend class MethodDescriptor;
  1238. friend class ServiceDescriptor;
  1239. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FileDescriptor);
  1240. };
  1241. // ===================================================================
  1242. // Used to construct descriptors.
  1243. //
  1244. // Normally you won't want to build your own descriptors. Message classes
  1245. // constructed by the protocol compiler will provide them for you. However,
  1246. // if you are implementing Message on your own, or if you are writing a
  1247. // program which can operate on totally arbitrary types and needs to load
  1248. // them from some sort of database, you might need to.
  1249. //
  1250. // Since Descriptors are composed of a whole lot of cross-linked bits of
  1251. // data that would be a pain to put together manually, the
  1252. // DescriptorPool class is provided to make the process easier. It can
  1253. // take a FileDescriptorProto (defined in descriptor.proto), validate it,
  1254. // and convert it to a set of nicely cross-linked Descriptors.
  1255. //
  1256. // DescriptorPool also helps with memory management. Descriptors are
  1257. // composed of many objects containing static data and pointers to each
  1258. // other. In all likelihood, when it comes time to delete this data,
  1259. // you'll want to delete it all at once. In fact, it is not uncommon to
  1260. // have a whole pool of descriptors all cross-linked with each other which
  1261. // you wish to delete all at once. This class represents such a pool, and
  1262. // handles the memory management for you.
  1263. //
  1264. // You can also search for descriptors within a DescriptorPool by name, and
  1265. // extensions by number.
  1266. class PROTOBUF_EXPORT DescriptorPool {
  1267. public:
  1268. // Create a normal, empty DescriptorPool.
  1269. DescriptorPool();
  1270. // Constructs a DescriptorPool that, when it can't find something among the
  1271. // descriptors already in the pool, looks for it in the given
  1272. // DescriptorDatabase.
  1273. // Notes:
  1274. // - If a DescriptorPool is constructed this way, its BuildFile*() methods
  1275. // must not be called (they will assert-fail). The only way to populate
  1276. // the pool with descriptors is to call the Find*By*() methods.
  1277. // - The Find*By*() methods may block the calling thread if the
  1278. // DescriptorDatabase blocks. This in turn means that parsing messages
  1279. // may block if they need to look up extensions.
  1280. // - The Find*By*() methods will use mutexes for thread-safety, thus making
  1281. // them slower even when they don't have to fall back to the database.
  1282. // In fact, even the Find*By*() methods of descriptor objects owned by
  1283. // this pool will be slower, since they will have to obtain locks too.
  1284. // - An ErrorCollector may optionally be given to collect validation errors
  1285. // in files loaded from the database. If not given, errors will be printed
  1286. // to GOOGLE_LOG(ERROR). Remember that files are built on-demand, so this
  1287. // ErrorCollector may be called from any thread that calls one of the
  1288. // Find*By*() methods.
  1289. // - The DescriptorDatabase must not be mutated during the lifetime of
  1290. // the DescriptorPool. Even if the client takes care to avoid data races,
  1291. // changes to the content of the DescriptorDatabase may not be reflected
  1292. // in subsequent lookups in the DescriptorPool.
  1293. class ErrorCollector;
  1294. explicit DescriptorPool(DescriptorDatabase* fallback_database,
  1295. ErrorCollector* error_collector = nullptr);
  1296. ~DescriptorPool();
  1297. // Get a pointer to the generated pool. Generated protocol message classes
  1298. // which are compiled into the binary will allocate their descriptors in
  1299. // this pool. Do not add your own descriptors to this pool.
  1300. static const DescriptorPool* generated_pool();
  1301. // Find a FileDescriptor in the pool by file name. Returns nullptr if not
  1302. // found.
  1303. const FileDescriptor* FindFileByName(const std::string& name) const;
  1304. // Find the FileDescriptor in the pool which defines the given symbol.
  1305. // If any of the Find*ByName() methods below would succeed, then this is
  1306. // equivalent to calling that method and calling the result's file() method.
  1307. // Otherwise this returns nullptr.
  1308. const FileDescriptor* FindFileContainingSymbol(
  1309. const std::string& symbol_name) const;
  1310. // Looking up descriptors ------------------------------------------
  1311. // These find descriptors by fully-qualified name. These will find both
  1312. // top-level descriptors and nested descriptors. They return nullptr if not
  1313. // found.
  1314. const Descriptor* FindMessageTypeByName(const std::string& name) const;
  1315. const FieldDescriptor* FindFieldByName(const std::string& name) const;
  1316. const FieldDescriptor* FindExtensionByName(const std::string& name) const;
  1317. const OneofDescriptor* FindOneofByName(const std::string& name) const;
  1318. const EnumDescriptor* FindEnumTypeByName(const std::string& name) const;
  1319. const EnumValueDescriptor* FindEnumValueByName(const std::string& name) const;
  1320. const ServiceDescriptor* FindServiceByName(const std::string& name) const;
  1321. const MethodDescriptor* FindMethodByName(const std::string& name) const;
  1322. // Finds an extension of the given type by number. The extendee must be
  1323. // a member of this DescriptorPool or one of its underlays.
  1324. const FieldDescriptor* FindExtensionByNumber(const Descriptor* extendee,
  1325. int number) const;
  1326. // Finds an extension of the given type by its printable name.
  1327. // See comments above PrintableNameForExtension() for the definition of
  1328. // "printable name". The extendee must be a member of this DescriptorPool
  1329. // or one of its underlays. Returns nullptr if there is no known message
  1330. // extension with the given printable name.
  1331. const FieldDescriptor* FindExtensionByPrintableName(
  1332. const Descriptor* extendee, const std::string& printable_name) const;
  1333. // Finds extensions of extendee. The extensions will be appended to
  1334. // out in an undefined order. Only extensions defined directly in
  1335. // this DescriptorPool or one of its underlays are guaranteed to be
  1336. // found: extensions defined in the fallback database might not be found
  1337. // depending on the database implementation.
  1338. void FindAllExtensions(const Descriptor* extendee,
  1339. std::vector<const FieldDescriptor*>* out) const;
  1340. // Building descriptors --------------------------------------------
  1341. // When converting a FileDescriptorProto to a FileDescriptor, various
  1342. // errors might be detected in the input. The caller may handle these
  1343. // programmatically by implementing an ErrorCollector.
  1344. class PROTOBUF_EXPORT ErrorCollector {
  1345. public:
  1346. inline ErrorCollector() {}
  1347. virtual ~ErrorCollector();
  1348. // These constants specify what exact part of the construct is broken.
  1349. // This is useful e.g. for mapping the error back to an exact location
  1350. // in a .proto file.
  1351. enum ErrorLocation {
  1352. NAME, // the symbol name, or the package name for files
  1353. NUMBER, // field or extension range number
  1354. TYPE, // field type
  1355. EXTENDEE, // field extendee
  1356. DEFAULT_VALUE, // field default value
  1357. INPUT_TYPE, // method input type
  1358. OUTPUT_TYPE, // method output type
  1359. OPTION_NAME, // name in assignment
  1360. OPTION_VALUE, // value in option assignment
  1361. IMPORT, // import error
  1362. OTHER // some other problem
  1363. };
  1364. // Reports an error in the FileDescriptorProto. Use this function if the
  1365. // problem occurred should interrupt building the FileDescriptorProto.
  1366. virtual void AddError(
  1367. const std::string& filename, // File name in which the error occurred.
  1368. const std::string& element_name, // Full name of the erroneous element.
  1369. const Message* descriptor, // Descriptor of the erroneous element.
  1370. ErrorLocation location, // One of the location constants, above.
  1371. const std::string& message // Human-readable error message.
  1372. ) = 0;
  1373. // Reports a warning in the FileDescriptorProto. Use this function if the
  1374. // problem occurred should NOT interrupt building the FileDescriptorProto.
  1375. virtual void AddWarning(
  1376. const std::string& /*filename*/, // File name in which the error
  1377. // occurred.
  1378. const std::string& /*element_name*/, // Full name of the erroneous
  1379. // element.
  1380. const Message* /*descriptor*/, // Descriptor of the erroneous element.
  1381. ErrorLocation /*location*/, // One of the location constants, above.
  1382. const std::string& /*message*/ // Human-readable error message.
  1383. ) {}
  1384. private:
  1385. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ErrorCollector);
  1386. };
  1387. // Convert the FileDescriptorProto to real descriptors and place them in
  1388. // this DescriptorPool. All dependencies of the file must already be in
  1389. // the pool. Returns the resulting FileDescriptor, or nullptr if there were
  1390. // problems with the input (e.g. the message was invalid, or dependencies
  1391. // were missing). Details about the errors are written to GOOGLE_LOG(ERROR).
  1392. const FileDescriptor* BuildFile(const FileDescriptorProto& proto);
  1393. // Same as BuildFile() except errors are sent to the given ErrorCollector.
  1394. const FileDescriptor* BuildFileCollectingErrors(
  1395. const FileDescriptorProto& proto, ErrorCollector* error_collector);
  1396. // By default, it is an error if a FileDescriptorProto contains references
  1397. // to types or other files that are not found in the DescriptorPool (or its
  1398. // backing DescriptorDatabase, if any). If you call
  1399. // AllowUnknownDependencies(), however, then unknown types and files
  1400. // will be replaced by placeholder descriptors (which can be identified by
  1401. // the is_placeholder() method). This can allow you to
  1402. // perform some useful operations with a .proto file even if you do not
  1403. // have access to other .proto files on which it depends. However, some
  1404. // heuristics must be used to fill in the gaps in information, and these
  1405. // can lead to descriptors which are inaccurate. For example, the
  1406. // DescriptorPool may be forced to guess whether an unknown type is a message
  1407. // or an enum, as well as what package it resides in. Furthermore,
  1408. // placeholder types will not be discoverable via FindMessageTypeByName()
  1409. // and similar methods, which could confuse some descriptor-based algorithms.
  1410. // Generally, the results of this option should be handled with extreme care.
  1411. void AllowUnknownDependencies() { allow_unknown_ = true; }
  1412. // By default, weak imports are allowed to be missing, in which case we will
  1413. // use a placeholder for the dependency and convert the field to be an Empty
  1414. // message field. If you call EnforceWeakDependencies(true), however, the
  1415. // DescriptorPool will report a import not found error.
  1416. void EnforceWeakDependencies(bool enforce) { enforce_weak_ = enforce; }
  1417. // Internal stuff --------------------------------------------------
  1418. // These methods MUST NOT be called from outside the proto2 library.
  1419. // These methods may contain hidden pitfalls and may be removed in a
  1420. // future library version.
  1421. // Create a DescriptorPool which is overlaid on top of some other pool.
  1422. // If you search for a descriptor in the overlay and it is not found, the
  1423. // underlay will be searched as a backup. If the underlay has its own
  1424. // underlay, that will be searched next, and so on. This also means that
  1425. // files built in the overlay will be cross-linked with the underlay's
  1426. // descriptors if necessary. The underlay remains property of the caller;
  1427. // it must remain valid for the lifetime of the newly-constructed pool.
  1428. //
  1429. // Example: Say you want to parse a .proto file at runtime in order to use
  1430. // its type with a DynamicMessage. Say this .proto file has dependencies,
  1431. // but you know that all the dependencies will be things that are already
  1432. // compiled into the binary. For ease of use, you'd like to load the types
  1433. // right out of generated_pool() rather than have to parse redundant copies
  1434. // of all these .protos and runtime. But, you don't want to add the parsed
  1435. // types directly into generated_pool(): this is not allowed, and would be
  1436. // bad design anyway. So, instead, you could use generated_pool() as an
  1437. // underlay for a new DescriptorPool in which you add only the new file.
  1438. //
  1439. // WARNING: Use of underlays can lead to many subtle gotchas. Instead,
  1440. // try to formulate what you want to do in terms of DescriptorDatabases.
  1441. explicit DescriptorPool(const DescriptorPool* underlay);
  1442. // Called by generated classes at init time to add their descriptors to
  1443. // generated_pool. Do NOT call this in your own code! filename must be a
  1444. // permanent string (e.g. a string literal).
  1445. static void InternalAddGeneratedFile(const void* encoded_file_descriptor,
  1446. int size);
  1447. // Disallow [enforce_utf8 = false] in .proto files.
  1448. void DisallowEnforceUtf8() { disallow_enforce_utf8_ = true; }
  1449. // For internal use only: Gets a non-const pointer to the generated pool.
  1450. // This is called at static-initialization time only, so thread-safety is
  1451. // not a concern. If both an underlay and a fallback database are present,
  1452. // the underlay takes precedence.
  1453. static DescriptorPool* internal_generated_pool();
  1454. // For internal use only: Changes the behavior of BuildFile() such that it
  1455. // allows the file to make reference to message types declared in other files
  1456. // which it did not officially declare as dependencies.
  1457. void InternalDontEnforceDependencies();
  1458. // For internal use only: Enables lazy building of dependencies of a file.
  1459. // Delay the building of dependencies of a file descriptor until absolutely
  1460. // necessary, like when message_type() is called on a field that is defined
  1461. // in that dependency's file. This will cause functional issues if a proto
  1462. // or one of it's dependencies has errors. Should only be enabled for the
  1463. // generated_pool_ (because no descriptor build errors are guaranteed by
  1464. // the compilation generation process), testing, or if a lack of descriptor
  1465. // build errors can be guaranteed for a pool.
  1466. void InternalSetLazilyBuildDependencies() {
  1467. lazily_build_dependencies_ = true;
  1468. // This needs to be set when lazily building dependencies, as it breaks
  1469. // dependency checking.
  1470. InternalDontEnforceDependencies();
  1471. }
  1472. // For internal use only.
  1473. void internal_set_underlay(const DescriptorPool* underlay) {
  1474. underlay_ = underlay;
  1475. }
  1476. // For internal (unit test) use only: Returns true if a FileDescriptor has
  1477. // been constructed for the given file, false otherwise. Useful for testing
  1478. // lazy descriptor initialization behavior.
  1479. bool InternalIsFileLoaded(const std::string& filename) const;
  1480. // Add a file to unused_import_track_files_. DescriptorBuilder will log
  1481. // warnings for those files if there is any unused import.
  1482. void AddUnusedImportTrackFile(const std::string& file_name);
  1483. void ClearUnusedImportTrackFiles();
  1484. private:
  1485. friend class Descriptor;
  1486. friend class internal::LazyDescriptor;
  1487. friend class FieldDescriptor;
  1488. friend class EnumDescriptor;
  1489. friend class ServiceDescriptor;
  1490. friend class MethodDescriptor;
  1491. friend class FileDescriptor;
  1492. friend class StreamDescriptor;
  1493. friend class DescriptorBuilder;
  1494. friend class FileDescriptorTables;
  1495. // Return true if the given name is a sub-symbol of any non-package
  1496. // descriptor that already exists in the descriptor pool. (The full
  1497. // definition of such types is already known.)
  1498. bool IsSubSymbolOfBuiltType(const std::string& name) const;
  1499. // Tries to find something in the fallback database and link in the
  1500. // corresponding proto file. Returns true if successful, in which case
  1501. // the caller should search for the thing again. These are declared
  1502. // const because they are called by (semantically) const methods.
  1503. bool TryFindFileInFallbackDatabase(const std::string& name) const;
  1504. bool TryFindSymbolInFallbackDatabase(const std::string& name) const;
  1505. bool TryFindExtensionInFallbackDatabase(const Descriptor* containing_type,
  1506. int field_number) const;
  1507. // Like BuildFile() but called internally when the file has been loaded from
  1508. // fallback_database_. Declared const because it is called by (semantically)
  1509. // const methods.
  1510. const FileDescriptor* BuildFileFromDatabase(
  1511. const FileDescriptorProto& proto) const;
  1512. // Helper for when lazily_build_dependencies_ is set, can look up a symbol
  1513. // after the file's descriptor is built, and can build the file where that
  1514. // symbol is defined if necessary. Will create a placeholder if the type
  1515. // doesn't exist in the fallback database, or the file doesn't build
  1516. // successfully.
  1517. Symbol CrossLinkOnDemandHelper(const std::string& name,
  1518. bool expecting_enum) const;
  1519. // Create a placeholder FileDescriptor of the specified name
  1520. FileDescriptor* NewPlaceholderFile(const std::string& name) const;
  1521. FileDescriptor* NewPlaceholderFileWithMutexHeld(
  1522. const std::string& name) const;
  1523. enum PlaceholderType {
  1524. PLACEHOLDER_MESSAGE,
  1525. PLACEHOLDER_ENUM,
  1526. PLACEHOLDER_EXTENDABLE_MESSAGE
  1527. };
  1528. // Create a placeholder Descriptor of the specified name
  1529. Symbol NewPlaceholder(const std::string& name,
  1530. PlaceholderType placeholder_type) const;
  1531. Symbol NewPlaceholderWithMutexHeld(const std::string& name,
  1532. PlaceholderType placeholder_type) const;
  1533. // If fallback_database_ is nullptr, this is nullptr. Otherwise, this is a
  1534. // mutex which must be locked while accessing tables_.
  1535. internal::WrappedMutex* mutex_;
  1536. // See constructor.
  1537. DescriptorDatabase* fallback_database_;
  1538. ErrorCollector* default_error_collector_;
  1539. const DescriptorPool* underlay_;
  1540. // This class contains a lot of hash maps with complicated types that
  1541. // we'd like to keep out of the header.
  1542. class Tables;
  1543. std::unique_ptr<Tables> tables_;
  1544. bool enforce_dependencies_;
  1545. bool lazily_build_dependencies_;
  1546. bool allow_unknown_;
  1547. bool enforce_weak_;
  1548. bool disallow_enforce_utf8_;
  1549. std::set<std::string> unused_import_track_files_;
  1550. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(DescriptorPool);
  1551. };
  1552. // inline methods ====================================================
  1553. // These macros makes this repetitive code more readable.
  1554. #define PROTOBUF_DEFINE_ACCESSOR(CLASS, FIELD, TYPE) \
  1555. inline TYPE CLASS::FIELD() const { return FIELD##_; }
  1556. // Strings fields are stored as pointers but returned as const references.
  1557. #define PROTOBUF_DEFINE_STRING_ACCESSOR(CLASS, FIELD) \
  1558. inline const std::string& CLASS::FIELD() const { return *FIELD##_; }
  1559. // Arrays take an index parameter, obviously.
  1560. #define PROTOBUF_DEFINE_ARRAY_ACCESSOR(CLASS, FIELD, TYPE) \
  1561. inline TYPE CLASS::FIELD(int index) const { return FIELD##s_ + index; }
  1562. #define PROTOBUF_DEFINE_OPTIONS_ACCESSOR(CLASS, TYPE) \
  1563. inline const TYPE& CLASS::options() const { return *options_; }
  1564. PROTOBUF_DEFINE_STRING_ACCESSOR(Descriptor, name)
  1565. PROTOBUF_DEFINE_STRING_ACCESSOR(Descriptor, full_name)
  1566. PROTOBUF_DEFINE_ACCESSOR(Descriptor, file, const FileDescriptor*)
  1567. PROTOBUF_DEFINE_ACCESSOR(Descriptor, containing_type, const Descriptor*)
  1568. PROTOBUF_DEFINE_ACCESSOR(Descriptor, field_count, int)
  1569. PROTOBUF_DEFINE_ACCESSOR(Descriptor, oneof_decl_count, int)
  1570. PROTOBUF_DEFINE_ACCESSOR(Descriptor, nested_type_count, int)
  1571. PROTOBUF_DEFINE_ACCESSOR(Descriptor, enum_type_count, int)
  1572. PROTOBUF_DEFINE_ARRAY_ACCESSOR(Descriptor, field, const FieldDescriptor*)
  1573. PROTOBUF_DEFINE_ARRAY_ACCESSOR(Descriptor, oneof_decl, const OneofDescriptor*)
  1574. PROTOBUF_DEFINE_ARRAY_ACCESSOR(Descriptor, nested_type, const Descriptor*)
  1575. PROTOBUF_DEFINE_ARRAY_ACCESSOR(Descriptor, enum_type, const EnumDescriptor*)
  1576. PROTOBUF_DEFINE_ACCESSOR(Descriptor, extension_range_count, int)
  1577. PROTOBUF_DEFINE_ACCESSOR(Descriptor, extension_count, int)
  1578. PROTOBUF_DEFINE_ARRAY_ACCESSOR(Descriptor, extension_range,
  1579. const Descriptor::ExtensionRange*)
  1580. PROTOBUF_DEFINE_ARRAY_ACCESSOR(Descriptor, extension, const FieldDescriptor*)
  1581. PROTOBUF_DEFINE_ACCESSOR(Descriptor, reserved_range_count, int)
  1582. PROTOBUF_DEFINE_ARRAY_ACCESSOR(Descriptor, reserved_range,
  1583. const Descriptor::ReservedRange*)
  1584. PROTOBUF_DEFINE_ACCESSOR(Descriptor, reserved_name_count, int)
  1585. PROTOBUF_DEFINE_OPTIONS_ACCESSOR(Descriptor, MessageOptions)
  1586. PROTOBUF_DEFINE_ACCESSOR(Descriptor, is_placeholder, bool)
  1587. PROTOBUF_DEFINE_STRING_ACCESSOR(FieldDescriptor, name)
  1588. PROTOBUF_DEFINE_STRING_ACCESSOR(FieldDescriptor, full_name)
  1589. PROTOBUF_DEFINE_STRING_ACCESSOR(FieldDescriptor, json_name)
  1590. PROTOBUF_DEFINE_STRING_ACCESSOR(FieldDescriptor, lowercase_name)
  1591. PROTOBUF_DEFINE_STRING_ACCESSOR(FieldDescriptor, camelcase_name)
  1592. PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, file, const FileDescriptor*)
  1593. PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, number, int)
  1594. PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, is_extension, bool)
  1595. PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, label, FieldDescriptor::Label)
  1596. PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, containing_type, const Descriptor*)
  1597. PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, containing_oneof,
  1598. const OneofDescriptor*)
  1599. PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, index_in_oneof, int)
  1600. PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, extension_scope, const Descriptor*)
  1601. PROTOBUF_DEFINE_OPTIONS_ACCESSOR(FieldDescriptor, FieldOptions)
  1602. PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, has_default_value, bool)
  1603. PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, has_json_name, bool)
  1604. PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_int32, int32)
  1605. PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_int64, int64)
  1606. PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_uint32, uint32)
  1607. PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_uint64, uint64)
  1608. PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_float, float)
  1609. PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_double, double)
  1610. PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_bool, bool)
  1611. PROTOBUF_DEFINE_STRING_ACCESSOR(FieldDescriptor, default_value_string)
  1612. PROTOBUF_DEFINE_STRING_ACCESSOR(OneofDescriptor, name)
  1613. PROTOBUF_DEFINE_STRING_ACCESSOR(OneofDescriptor, full_name)
  1614. PROTOBUF_DEFINE_ACCESSOR(OneofDescriptor, containing_type, const Descriptor*)
  1615. PROTOBUF_DEFINE_ACCESSOR(OneofDescriptor, field_count, int)
  1616. PROTOBUF_DEFINE_OPTIONS_ACCESSOR(OneofDescriptor, OneofOptions)
  1617. PROTOBUF_DEFINE_STRING_ACCESSOR(EnumDescriptor, name)
  1618. PROTOBUF_DEFINE_STRING_ACCESSOR(EnumDescriptor, full_name)
  1619. PROTOBUF_DEFINE_ACCESSOR(EnumDescriptor, file, const FileDescriptor*)
  1620. PROTOBUF_DEFINE_ACCESSOR(EnumDescriptor, containing_type, const Descriptor*)
  1621. PROTOBUF_DEFINE_ACCESSOR(EnumDescriptor, value_count, int)
  1622. PROTOBUF_DEFINE_ARRAY_ACCESSOR(EnumDescriptor, value,
  1623. const EnumValueDescriptor*)
  1624. PROTOBUF_DEFINE_OPTIONS_ACCESSOR(EnumDescriptor, EnumOptions)
  1625. PROTOBUF_DEFINE_ACCESSOR(EnumDescriptor, is_placeholder, bool)
  1626. PROTOBUF_DEFINE_ACCESSOR(EnumDescriptor, reserved_range_count, int)
  1627. PROTOBUF_DEFINE_ARRAY_ACCESSOR(EnumDescriptor, reserved_range,
  1628. const EnumDescriptor::ReservedRange*)
  1629. PROTOBUF_DEFINE_ACCESSOR(EnumDescriptor, reserved_name_count, int)
  1630. PROTOBUF_DEFINE_STRING_ACCESSOR(EnumValueDescriptor, name)
  1631. PROTOBUF_DEFINE_STRING_ACCESSOR(EnumValueDescriptor, full_name)
  1632. PROTOBUF_DEFINE_ACCESSOR(EnumValueDescriptor, number, int)
  1633. PROTOBUF_DEFINE_ACCESSOR(EnumValueDescriptor, type, const EnumDescriptor*)
  1634. PROTOBUF_DEFINE_OPTIONS_ACCESSOR(EnumValueDescriptor, EnumValueOptions)
  1635. PROTOBUF_DEFINE_STRING_ACCESSOR(ServiceDescriptor, name)
  1636. PROTOBUF_DEFINE_STRING_ACCESSOR(ServiceDescriptor, full_name)
  1637. PROTOBUF_DEFINE_ACCESSOR(ServiceDescriptor, file, const FileDescriptor*)
  1638. PROTOBUF_DEFINE_ACCESSOR(ServiceDescriptor, method_count, int)
  1639. PROTOBUF_DEFINE_ARRAY_ACCESSOR(ServiceDescriptor, method,
  1640. const MethodDescriptor*)
  1641. PROTOBUF_DEFINE_OPTIONS_ACCESSOR(ServiceDescriptor, ServiceOptions)
  1642. PROTOBUF_DEFINE_STRING_ACCESSOR(MethodDescriptor, name)
  1643. PROTOBUF_DEFINE_STRING_ACCESSOR(MethodDescriptor, full_name)
  1644. PROTOBUF_DEFINE_ACCESSOR(MethodDescriptor, service, const ServiceDescriptor*)
  1645. PROTOBUF_DEFINE_OPTIONS_ACCESSOR(MethodDescriptor, MethodOptions)
  1646. PROTOBUF_DEFINE_ACCESSOR(MethodDescriptor, client_streaming, bool)
  1647. PROTOBUF_DEFINE_ACCESSOR(MethodDescriptor, server_streaming, bool)
  1648. PROTOBUF_DEFINE_STRING_ACCESSOR(FileDescriptor, name)
  1649. PROTOBUF_DEFINE_STRING_ACCESSOR(FileDescriptor, package)
  1650. PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, pool, const DescriptorPool*)
  1651. PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, dependency_count, int)
  1652. PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, public_dependency_count, int)
  1653. PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, weak_dependency_count, int)
  1654. PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, message_type_count, int)
  1655. PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, enum_type_count, int)
  1656. PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, service_count, int)
  1657. PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, extension_count, int)
  1658. PROTOBUF_DEFINE_OPTIONS_ACCESSOR(FileDescriptor, FileOptions)
  1659. PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, is_placeholder, bool)
  1660. PROTOBUF_DEFINE_ARRAY_ACCESSOR(FileDescriptor, message_type, const Descriptor*)
  1661. PROTOBUF_DEFINE_ARRAY_ACCESSOR(FileDescriptor, enum_type, const EnumDescriptor*)
  1662. PROTOBUF_DEFINE_ARRAY_ACCESSOR(FileDescriptor, service,
  1663. const ServiceDescriptor*)
  1664. PROTOBUF_DEFINE_ARRAY_ACCESSOR(FileDescriptor, extension,
  1665. const FieldDescriptor*)
  1666. #undef PROTOBUF_DEFINE_ACCESSOR
  1667. #undef PROTOBUF_DEFINE_STRING_ACCESSOR
  1668. #undef PROTOBUF_DEFINE_ARRAY_ACCESSOR
  1669. // A few accessors differ from the macros...
  1670. inline bool Descriptor::IsExtensionNumber(int number) const {
  1671. return FindExtensionRangeContainingNumber(number) != nullptr;
  1672. }
  1673. inline bool Descriptor::IsReservedNumber(int number) const {
  1674. return FindReservedRangeContainingNumber(number) != nullptr;
  1675. }
  1676. inline bool Descriptor::IsReservedName(const std::string& name) const {
  1677. for (int i = 0; i < reserved_name_count(); i++) {
  1678. if (name == reserved_name(i)) {
  1679. return true;
  1680. }
  1681. }
  1682. return false;
  1683. }
  1684. // Can't use PROTOBUF_DEFINE_ARRAY_ACCESSOR because reserved_names_ is actually
  1685. // an array of pointers rather than the usual array of objects.
  1686. inline const std::string& Descriptor::reserved_name(int index) const {
  1687. return *reserved_names_[index];
  1688. }
  1689. inline bool EnumDescriptor::IsReservedNumber(int number) const {
  1690. return FindReservedRangeContainingNumber(number) != nullptr;
  1691. }
  1692. inline bool EnumDescriptor::IsReservedName(const std::string& name) const {
  1693. for (int i = 0; i < reserved_name_count(); i++) {
  1694. if (name == reserved_name(i)) {
  1695. return true;
  1696. }
  1697. }
  1698. return false;
  1699. }
  1700. // Can't use PROTOBUF_DEFINE_ARRAY_ACCESSOR because reserved_names_ is actually
  1701. // an array of pointers rather than the usual array of objects.
  1702. inline const std::string& EnumDescriptor::reserved_name(int index) const {
  1703. return *reserved_names_[index];
  1704. }
  1705. inline FieldDescriptor::Type FieldDescriptor::type() const {
  1706. if (type_once_) {
  1707. internal::call_once(*type_once_, &FieldDescriptor::TypeOnceInit, this);
  1708. }
  1709. return type_;
  1710. }
  1711. inline bool FieldDescriptor::is_required() const {
  1712. return label() == LABEL_REQUIRED;
  1713. }
  1714. inline bool FieldDescriptor::is_optional() const {
  1715. return label() == LABEL_OPTIONAL;
  1716. }
  1717. inline bool FieldDescriptor::is_repeated() const {
  1718. return label() == LABEL_REPEATED;
  1719. }
  1720. inline bool FieldDescriptor::is_packable() const {
  1721. return is_repeated() && IsTypePackable(type());
  1722. }
  1723. inline bool FieldDescriptor::is_map() const {
  1724. return type() == TYPE_MESSAGE && is_map_message_type();
  1725. }
  1726. // To save space, index() is computed by looking at the descriptor's position
  1727. // in the parent's array of children.
  1728. inline int FieldDescriptor::index() const {
  1729. if (!is_extension_) {
  1730. return static_cast<int>(this - containing_type()->fields_);
  1731. } else if (extension_scope_ != nullptr) {
  1732. return static_cast<int>(this - extension_scope_->extensions_);
  1733. } else {
  1734. return static_cast<int>(this - file_->extensions_);
  1735. }
  1736. }
  1737. inline int Descriptor::index() const {
  1738. if (containing_type_ == nullptr) {
  1739. return static_cast<int>(this - file_->message_types_);
  1740. } else {
  1741. return static_cast<int>(this - containing_type_->nested_types_);
  1742. }
  1743. }
  1744. inline const FileDescriptor* OneofDescriptor::file() const {
  1745. return containing_type()->file();
  1746. }
  1747. inline int OneofDescriptor::index() const {
  1748. return static_cast<int>(this - containing_type_->oneof_decls_);
  1749. }
  1750. inline int EnumDescriptor::index() const {
  1751. if (containing_type_ == nullptr) {
  1752. return static_cast<int>(this - file_->enum_types_);
  1753. } else {
  1754. return static_cast<int>(this - containing_type_->enum_types_);
  1755. }
  1756. }
  1757. inline const FileDescriptor* EnumValueDescriptor::file() const {
  1758. return type()->file();
  1759. }
  1760. inline int EnumValueDescriptor::index() const {
  1761. return static_cast<int>(this - type_->values_);
  1762. }
  1763. inline int ServiceDescriptor::index() const {
  1764. return static_cast<int>(this - file_->services_);
  1765. }
  1766. inline const FileDescriptor* MethodDescriptor::file() const {
  1767. return service()->file();
  1768. }
  1769. inline int MethodDescriptor::index() const {
  1770. return static_cast<int>(this - service_->methods_);
  1771. }
  1772. inline const char* FieldDescriptor::type_name() const {
  1773. return kTypeToName[type()];
  1774. }
  1775. inline FieldDescriptor::CppType FieldDescriptor::cpp_type() const {
  1776. return kTypeToCppTypeMap[type()];
  1777. }
  1778. inline const char* FieldDescriptor::cpp_type_name() const {
  1779. return kCppTypeToName[kTypeToCppTypeMap[type()]];
  1780. }
  1781. inline FieldDescriptor::CppType FieldDescriptor::TypeToCppType(Type type) {
  1782. return kTypeToCppTypeMap[type];
  1783. }
  1784. inline const char* FieldDescriptor::TypeName(Type type) {
  1785. return kTypeToName[type];
  1786. }
  1787. inline const char* FieldDescriptor::CppTypeName(CppType cpp_type) {
  1788. return kCppTypeToName[cpp_type];
  1789. }
  1790. inline bool FieldDescriptor::IsTypePackable(Type field_type) {
  1791. return (field_type != FieldDescriptor::TYPE_STRING &&
  1792. field_type != FieldDescriptor::TYPE_GROUP &&
  1793. field_type != FieldDescriptor::TYPE_MESSAGE &&
  1794. field_type != FieldDescriptor::TYPE_BYTES);
  1795. }
  1796. inline const FileDescriptor* FileDescriptor::public_dependency(
  1797. int index) const {
  1798. return dependency(public_dependencies_[index]);
  1799. }
  1800. inline const FileDescriptor* FileDescriptor::weak_dependency(int index) const {
  1801. return dependency(weak_dependencies_[index]);
  1802. }
  1803. inline FileDescriptor::Syntax FileDescriptor::syntax() const { return syntax_; }
  1804. // Can't use PROTOBUF_DEFINE_ARRAY_ACCESSOR because fields_ is actually an array
  1805. // of pointers rather than the usual array of objects.
  1806. inline const FieldDescriptor* OneofDescriptor::field(int index) const {
  1807. return fields_[index];
  1808. }
  1809. } // namespace protobuf
  1810. } // namespace google
  1811. #include <google/protobuf/port_undef.inc>
  1812. #endif // GOOGLE_PROTOBUF_DESCRIPTOR_H__