descriptor.h 97 KB

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