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

descriptor.h 101 kB

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