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