Coverage Report

Created: 2023-06-07 07:09

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