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