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