descriptor.h 94 KB

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