descriptor.h 87 KB

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