You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

descriptor.h 121 kB

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