Coverage Report

Created: 2024-07-27 06:53

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