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