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