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