descriptor.h 96 KB

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