forked from protocolbuffers/protobuf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
descriptor.h
2417 lines (1999 loc) · 97.4 KB
/
descriptor.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc. All rights reserved.
// https://developers.google.com/protocol-buffers/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// Author: kenton@google.com (Kenton Varda)
// Based on original Protocol Buffers design by
// Sanjay Ghemawat, Jeff Dean, and others.
//
// This file contains classes which describe a type of protocol message.
// You can use a message's descriptor to learn at runtime what fields
// it contains and what the types of those fields are. The Message
// interface also allows you to dynamically access and modify individual
// fields by passing the FieldDescriptor of the field you are interested
// in.
//
// Most users will not care about descriptors, because they will write
// code specific to certain protocol types and will simply use the classes
// generated by the protocol compiler directly. Advanced users who want
// to operate on arbitrary types (not known at compile time) may want to
// read descriptors in order to learn about the contents of a message.
// A very small number of users will want to construct their own
// Descriptors, either because they are implementing Message manually or
// because they are writing something like the protocol compiler.
//
// For an example of how you might use descriptors, see the code example
// at the top of message.h.
#ifndef GOOGLE_PROTOBUF_DESCRIPTOR_H__
#define GOOGLE_PROTOBUF_DESCRIPTOR_H__
#include <atomic>
#include <map>
#include <memory>
#include <set>
#include <string>
#include <vector>
#include <google/protobuf/stubs/common.h>
#include <google/protobuf/stubs/logging.h>
#include <google/protobuf/stubs/mutex.h>
#include <google/protobuf/stubs/once.h>
#include <google/protobuf/port.h>
#include <google/protobuf/port_def.inc>
// TYPE_BOOL is defined in the MacOS's ConditionalMacros.h.
#ifdef TYPE_BOOL
#undef TYPE_BOOL
#endif // TYPE_BOOL
#ifdef SWIG
#define PROTOBUF_EXPORT
#endif
namespace google {
namespace protobuf {
// Defined in this file.
class Descriptor;
class FieldDescriptor;
class OneofDescriptor;
class EnumDescriptor;
class EnumValueDescriptor;
class ServiceDescriptor;
class MethodDescriptor;
class FileDescriptor;
class DescriptorDatabase;
class DescriptorPool;
// Defined in descriptor.proto
class DescriptorProto;
class DescriptorProto_ExtensionRange;
class FieldDescriptorProto;
class OneofDescriptorProto;
class EnumDescriptorProto;
class EnumValueDescriptorProto;
class ServiceDescriptorProto;
class MethodDescriptorProto;
class FileDescriptorProto;
class MessageOptions;
class FieldOptions;
class OneofOptions;
class EnumOptions;
class EnumValueOptions;
class ExtensionRangeOptions;
class ServiceOptions;
class MethodOptions;
class FileOptions;
class UninterpretedOption;
class SourceCodeInfo;
// Defined in message.h
class Message;
class Reflection;
// Defined in descriptor.cc
class DescriptorBuilder;
class FileDescriptorTables;
class Symbol;
// Defined in unknown_field_set.h.
class UnknownField;
// Defined in command_line_interface.cc
namespace compiler {
class CommandLineInterface;
namespace cpp {
// Defined in helpers.h
class Formatter;
} // namespace cpp
} // namespace compiler
namespace descriptor_unittest {
class DescriptorTest;
} // namespace descriptor_unittest
// Defined in printer.h
namespace io {
class Printer;
} // namespace io
// NB, all indices are zero-based.
struct SourceLocation {
int start_line;
int end_line;
int start_column;
int end_column;
// Doc comments found at the source location.
// See the comments in SourceCodeInfo.Location (descriptor.proto) for details.
std::string leading_comments;
std::string trailing_comments;
std::vector<std::string> leading_detached_comments;
};
// Options when generating machine-parsable output from a descriptor with
// DebugString().
struct DebugStringOptions {
// include original user comments as recorded in SourceLocation entries. N.B.
// that this must be |false| by default: several other pieces of code (for
// example, the C++ code generation for fields in the proto compiler) rely on
// DebugString() output being unobstructed by user comments.
bool include_comments;
// If true, elide the braced body in the debug string.
bool elide_group_body;
bool elide_oneof_body;
DebugStringOptions()
: include_comments(false),
elide_group_body(false),
elide_oneof_body(false) {
}
};
// A class to handle the simplest cases of a lazily linked descriptor
// for a message type that isn't built at the time of cross linking,
// which is needed when a pool has lazily_build_dependencies_ set.
// Must be instantiated as mutable in a descriptor.
namespace internal {
class PROTOBUF_EXPORT LazyDescriptor {
public:
// Init function to be called at init time of a descriptor containing
// a LazyDescriptor.
void Init() {
descriptor_ = nullptr;
once_ = nullptr;
}
// Sets the value of the descriptor if it is known during the descriptor
// building process. Not thread safe, should only be called during the
// descriptor build process. Should not be called after SetLazy has been
// called.
void Set(const Descriptor* descriptor);
// Sets the information needed to lazily cross link the descriptor at a later
// time, SetLazy is not thread safe, should be called only once at descriptor
// build time if the symbol wasn't found and building of the file containing
// that type is delayed because lazily_build_dependencies_ is set on the pool.
// Should not be called after Set() has been called.
void SetLazy(StringPiece name, const FileDescriptor* file);
// Returns the current value of the descriptor, thread-safe. If SetLazy(...)
// has been called, will do a one-time cross link of the type specified,
// building the descriptor file that contains the type if necessary.
inline const Descriptor* Get(const ServiceDescriptor* service) {
Once(service);
return descriptor_;
}
private:
void Once(const ServiceDescriptor* service);
union {
const Descriptor* descriptor_;
const char* lazy_name_;
};
internal::once_flag* once_;
};
class PROTOBUF_EXPORT SymbolBase {
private:
friend class google::protobuf::Symbol;
uint8_t symbol_type_;
};
// Some types have more than one SymbolBase because they have multiple
// identities in the table. We can't have duplicate direct bases, so we use this
// intermediate base to do so.
// See BuildEnumValue for details.
template <int N>
class PROTOBUF_EXPORT SymbolBaseN : public SymbolBase {};
} // namespace internal
// Describes a type of protocol message, or a particular group within a
// message. To obtain the Descriptor for a given message object, call
// Message::GetDescriptor(). Generated message classes also have a
// static method called descriptor() which returns the type's descriptor.
// Use DescriptorPool to construct your own descriptors.
class PROTOBUF_EXPORT Descriptor : private internal::SymbolBase {
public:
typedef DescriptorProto Proto;
// The name of the message type, not including its scope.
const std::string& name() const;
// The fully-qualified name of the message type, scope delimited by
// periods. For example, message type "Foo" which is declared in package
// "bar" has full name "bar.Foo". If a type "Baz" is nested within
// Foo, Baz's full_name is "bar.Foo.Baz". To get only the part that
// comes after the last '.', use name().
const std::string& full_name() const;
// Index of this descriptor within the file or containing type's message
// type array.
int index() const;
// The .proto file in which this message type was defined. Never nullptr.
const FileDescriptor* file() const;
// If this Descriptor describes a nested type, this returns the type
// in which it is nested. Otherwise, returns nullptr.
const Descriptor* containing_type() const;
// Get options for this message type. These are specified in the .proto file
// by placing lines like "option foo = 1234;" in the message definition.
// Allowed options are defined by MessageOptions in descriptor.proto, and any
// available extensions of that message.
const MessageOptions& options() const;
// Write the contents of this Descriptor into the given DescriptorProto.
// The target DescriptorProto must be clear before calling this; if it
// isn't, the result may be garbage.
void CopyTo(DescriptorProto* proto) const;
// Write the contents of this descriptor in a human-readable form. Output
// will be suitable for re-parsing.
std::string DebugString() const;
// Similar to DebugString(), but additionally takes options (e.g.,
// include original user comments in output).
std::string DebugStringWithOptions(const DebugStringOptions& options) const;
// Returns true if this is a placeholder for an unknown type. This will
// only be the case if this descriptor comes from a DescriptorPool
// with AllowUnknownDependencies() set.
bool is_placeholder() const;
enum WellKnownType {
WELLKNOWNTYPE_UNSPECIFIED, // Not a well-known type.
// Wrapper types.
WELLKNOWNTYPE_DOUBLEVALUE, // google.protobuf.DoubleValue
WELLKNOWNTYPE_FLOATVALUE, // google.protobuf.FloatValue
WELLKNOWNTYPE_INT64VALUE, // google.protobuf.Int64Value
WELLKNOWNTYPE_UINT64VALUE, // google.protobuf.UInt64Value
WELLKNOWNTYPE_INT32VALUE, // google.protobuf.Int32Value
WELLKNOWNTYPE_UINT32VALUE, // google.protobuf.UInt32Value
WELLKNOWNTYPE_STRINGVALUE, // google.protobuf.StringValue
WELLKNOWNTYPE_BYTESVALUE, // google.protobuf.BytesValue
WELLKNOWNTYPE_BOOLVALUE, // google.protobuf.BoolValue
// Other well known types.
WELLKNOWNTYPE_ANY, // google.protobuf.Any
WELLKNOWNTYPE_FIELDMASK, // google.protobuf.FieldMask
WELLKNOWNTYPE_DURATION, // google.protobuf.Duration
WELLKNOWNTYPE_TIMESTAMP, // google.protobuf.Timestamp
WELLKNOWNTYPE_VALUE, // google.protobuf.Value
WELLKNOWNTYPE_LISTVALUE, // google.protobuf.ListValue
WELLKNOWNTYPE_STRUCT, // google.protobuf.Struct
// New well-known types may be added in the future.
// Please make sure any switch() statements have a 'default' case.
__WELLKNOWNTYPE__DO_NOT_USE__ADD_DEFAULT_INSTEAD__,
};
WellKnownType well_known_type() const;
// Field stuff -----------------------------------------------------
// The number of fields in this message type.
int field_count() const;
// Gets a field by index, where 0 <= index < field_count().
// These are returned in the order they were defined in the .proto file.
const FieldDescriptor* field(int index) const;
// Looks up a field by declared tag number. Returns nullptr if no such field
// exists.
const FieldDescriptor* FindFieldByNumber(int number) const;
// Looks up a field by name. Returns nullptr if no such field exists.
const FieldDescriptor* FindFieldByName(ConstStringParam name) const;
// Looks up a field by lowercased name (as returned by lowercase_name()).
// This lookup may be ambiguous if multiple field names differ only by case,
// in which case the field returned is chosen arbitrarily from the matches.
const FieldDescriptor* FindFieldByLowercaseName(
ConstStringParam lowercase_name) const;
// Looks up a field by camel-case name (as returned by camelcase_name()).
// This lookup may be ambiguous if multiple field names differ in a way that
// leads them to have identical camel-case names, in which case the field
// returned is chosen arbitrarily from the matches.
const FieldDescriptor* FindFieldByCamelcaseName(
ConstStringParam camelcase_name) const;
// The number of oneofs in this message type.
int oneof_decl_count() const;
// The number of oneofs in this message type, excluding synthetic oneofs.
// Real oneofs always come first, so iterating up to real_oneof_decl_cout()
// will yield all real oneofs.
int real_oneof_decl_count() const;
// Get a oneof by index, where 0 <= index < oneof_decl_count().
// These are returned in the order they were defined in the .proto file.
const OneofDescriptor* oneof_decl(int index) const;
// Looks up a oneof by name. Returns nullptr if no such oneof exists.
const OneofDescriptor* FindOneofByName(ConstStringParam name) const;
// Nested type stuff -----------------------------------------------
// The number of nested types in this message type.
int nested_type_count() const;
// Gets a nested type by index, where 0 <= index < nested_type_count().
// These are returned in the order they were defined in the .proto file.
const Descriptor* nested_type(int index) const;
// Looks up a nested type by name. Returns nullptr if no such nested type
// exists.
const Descriptor* FindNestedTypeByName(ConstStringParam name) const;
// Enum stuff ------------------------------------------------------
// The number of enum types in this message type.
int enum_type_count() const;
// Gets an enum type by index, where 0 <= index < enum_type_count().
// These are returned in the order they were defined in the .proto file.
const EnumDescriptor* enum_type(int index) const;
// Looks up an enum type by name. Returns nullptr if no such enum type
// exists.
const EnumDescriptor* FindEnumTypeByName(ConstStringParam name) const;
// Looks up an enum value by name, among all enum types in this message.
// Returns nullptr if no such value exists.
const EnumValueDescriptor* FindEnumValueByName(ConstStringParam name) const;
// Extensions ------------------------------------------------------
// A range of field numbers which are designated for third-party
// extensions.
struct ExtensionRange {
typedef DescriptorProto_ExtensionRange Proto;
typedef ExtensionRangeOptions OptionsType;
// See Descriptor::CopyTo().
void CopyTo(DescriptorProto_ExtensionRange* proto) const;
int start; // inclusive
int end; // exclusive
const ExtensionRangeOptions* options_;
};
// The number of extension ranges in this message type.
int extension_range_count() const;
// Gets an extension range by index, where 0 <= index <
// extension_range_count(). These are returned in the order they were defined
// in the .proto file.
const ExtensionRange* extension_range(int index) const;
// Returns true if the number is in one of the extension ranges.
bool IsExtensionNumber(int number) const;
// Returns nullptr if no extension range contains the given number.
const ExtensionRange* FindExtensionRangeContainingNumber(int number) const;
// The number of extensions defined nested within this message type's scope.
// See doc:
// https://developers.google.com/protocol-buffers/docs/proto#nested-extensions
//
// Note that the extensions may be extending *other* messages.
//
// For example:
// message M1 {
// extensions 1 to max;
// }
//
// message M2 {
// extend M1 {
// optional int32 foo = 1;
// }
// }
//
// In this case,
// DescriptorPool::generated_pool()
// ->FindMessageTypeByName("M2")
// ->extension(0)
// will return "foo", even though "foo" is an extension of M1.
// To find all known extensions of a given message, instead use
// DescriptorPool::FindAllExtensions.
int extension_count() const;
// Get an extension by index, where 0 <= index < extension_count().
// These are returned in the order they were defined in the .proto file.
const FieldDescriptor* extension(int index) const;
// Looks up a named extension (which extends some *other* message type)
// defined within this message type's scope.
const FieldDescriptor* FindExtensionByName(ConstStringParam name) const;
// Similar to FindFieldByLowercaseName(), but finds extensions defined within
// this message type's scope.
const FieldDescriptor* FindExtensionByLowercaseName(
ConstStringParam name) const;
// Similar to FindFieldByCamelcaseName(), but finds extensions defined within
// this message type's scope.
const FieldDescriptor* FindExtensionByCamelcaseName(
ConstStringParam name) const;
// Reserved fields -------------------------------------------------
// A range of reserved field numbers.
struct ReservedRange {
int start; // inclusive
int end; // exclusive
};
// The number of reserved ranges in this message type.
int reserved_range_count() const;
// Gets an reserved range by index, where 0 <= index <
// reserved_range_count(). These are returned in the order they were defined
// in the .proto file.
const ReservedRange* reserved_range(int index) const;
// Returns true if the number is in one of the reserved ranges.
bool IsReservedNumber(int number) const;
// Returns nullptr if no reserved range contains the given number.
const ReservedRange* FindReservedRangeContainingNumber(int number) const;
// The number of reserved field names in this message type.
int reserved_name_count() const;
// Gets a reserved name by index, where 0 <= index < reserved_name_count().
const std::string& reserved_name(int index) const;
// Returns true if the field name is reserved.
bool IsReservedName(ConstStringParam name) const;
// Source Location ---------------------------------------------------
// Updates |*out_location| to the source location of the complete
// extent of this message declaration. Returns false and leaves
// |*out_location| unchanged iff location information was not available.
bool GetSourceLocation(SourceLocation* out_location) const;
// Maps --------------------------------------------------------------
// Returns the FieldDescriptor for the "key" field. If this isn't a map entry
// field, returns nullptr.
const FieldDescriptor* map_key() const;
// Returns the FieldDescriptor for the "value" field. If this isn't a map
// entry field, returns nullptr.
const FieldDescriptor* map_value() const;
private:
friend class Symbol;
typedef MessageOptions OptionsType;
// Allows tests to test CopyTo(proto, true).
friend class descriptor_unittest::DescriptorTest;
// Allows access to GetLocationPath for annotations.
friend class io::Printer;
friend class compiler::cpp::Formatter;
// Fill the json_name field of FieldDescriptorProto.
void CopyJsonNameTo(DescriptorProto* proto) const;
// Internal version of DebugString; controls the level of indenting for
// correct depth. Takes |options| to control debug-string options, and
// |include_opening_clause| to indicate whether the "message ... " part of the
// clause has already been generated (this varies depending on context).
void DebugString(int depth, std::string* contents,
const DebugStringOptions& options,
bool include_opening_clause) const;
// Walks up the descriptor tree to generate the source location path
// to this descriptor from the file root.
void GetLocationPath(std::vector<int>* output) const;
// True if this is a placeholder for an unknown type.
bool is_placeholder_ : 1;
// True if this is a placeholder and the type name wasn't fully-qualified.
bool is_unqualified_placeholder_ : 1;
// Well known type. Stored like this to conserve space.
uint8_t well_known_type_ : 5;
// This points to the last field _number_ that is part of the sequence
// starting at 1, where
// `desc->field(i)->number() == i + 1`
// A value of `0` means no field matches. That is, there are no fields or the
// first field is not field `1`.
// Uses 16-bit to avoid extra padding. Unlikely to have more than 2^16
// sequentially numbered fields in a message.
uint16_t sequential_field_limit_;
int field_count_;
// all_names_ = [name, full_name]
const std::string* all_names_;
const FileDescriptor* file_;
const Descriptor* containing_type_;
const MessageOptions* options_;
// These arrays are separated from their sizes to minimize padding on 64-bit.
FieldDescriptor* fields_;
OneofDescriptor* oneof_decls_;
Descriptor* nested_types_;
EnumDescriptor* enum_types_;
ExtensionRange* extension_ranges_;
FieldDescriptor* extensions_;
ReservedRange* reserved_ranges_;
const std::string** reserved_names_;
int oneof_decl_count_;
int real_oneof_decl_count_;
int nested_type_count_;
int enum_type_count_;
int extension_range_count_;
int extension_count_;
int reserved_range_count_;
int reserved_name_count_;
// IMPORTANT: If you add a new field, make sure to search for all instances
// of Allocate<Descriptor>() and AllocateArray<Descriptor>() in descriptor.cc
// and update them to initialize the field.
// Must be constructed using DescriptorPool.
Descriptor() {}
friend class DescriptorBuilder;
friend class DescriptorPool;
friend class EnumDescriptor;
friend class FieldDescriptor;
friend class FileDescriptorTables;
friend class OneofDescriptor;
friend class MethodDescriptor;
friend class FileDescriptor;
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Descriptor);
};
// Describes a single field of a message. To get the descriptor for a given
// field, first get the Descriptor for the message in which it is defined,
// then call Descriptor::FindFieldByName(). To get a FieldDescriptor for
// an extension, do one of the following:
// - Get the Descriptor or FileDescriptor for its containing scope, then
// call Descriptor::FindExtensionByName() or
// FileDescriptor::FindExtensionByName().
// - Given a DescriptorPool, call DescriptorPool::FindExtensionByNumber() or
// DescriptorPool::FindExtensionByPrintableName().
// Use DescriptorPool to construct your own descriptors.
class PROTOBUF_EXPORT FieldDescriptor : private internal::SymbolBase {
public:
typedef FieldDescriptorProto Proto;
// Identifies a field type. 0 is reserved for errors. The order is weird
// for historical reasons. Types 12 and up are new in proto2.
enum Type {
TYPE_DOUBLE = 1, // double, exactly eight bytes on the wire.
TYPE_FLOAT = 2, // float, exactly four bytes on the wire.
TYPE_INT64 = 3, // int64, varint on the wire. Negative numbers
// take 10 bytes. Use TYPE_SINT64 if negative
// values are likely.
TYPE_UINT64 = 4, // uint64, varint on the wire.
TYPE_INT32 = 5, // int32, varint on the wire. Negative numbers
// take 10 bytes. Use TYPE_SINT32 if negative
// values are likely.
TYPE_FIXED64 = 6, // uint64, exactly eight bytes on the wire.
TYPE_FIXED32 = 7, // uint32, exactly four bytes on the wire.
TYPE_BOOL = 8, // bool, varint on the wire.
TYPE_STRING = 9, // UTF-8 text.
TYPE_GROUP = 10, // Tag-delimited message. Deprecated.
TYPE_MESSAGE = 11, // Length-delimited message.
TYPE_BYTES = 12, // Arbitrary byte array.
TYPE_UINT32 = 13, // uint32, varint on the wire
TYPE_ENUM = 14, // Enum, varint on the wire
TYPE_SFIXED32 = 15, // int32, exactly four bytes on the wire
TYPE_SFIXED64 = 16, // int64, exactly eight bytes on the wire
TYPE_SINT32 = 17, // int32, ZigZag-encoded varint on the wire
TYPE_SINT64 = 18, // int64, ZigZag-encoded varint on the wire
MAX_TYPE = 18, // Constant useful for defining lookup tables
// indexed by Type.
};
// Specifies the C++ data type used to represent the field. There is a
// fixed mapping from Type to CppType where each Type maps to exactly one
// CppType. 0 is reserved for errors.
enum CppType {
CPPTYPE_INT32 = 1, // TYPE_INT32, TYPE_SINT32, TYPE_SFIXED32
CPPTYPE_INT64 = 2, // TYPE_INT64, TYPE_SINT64, TYPE_SFIXED64
CPPTYPE_UINT32 = 3, // TYPE_UINT32, TYPE_FIXED32
CPPTYPE_UINT64 = 4, // TYPE_UINT64, TYPE_FIXED64
CPPTYPE_DOUBLE = 5, // TYPE_DOUBLE
CPPTYPE_FLOAT = 6, // TYPE_FLOAT
CPPTYPE_BOOL = 7, // TYPE_BOOL
CPPTYPE_ENUM = 8, // TYPE_ENUM
CPPTYPE_STRING = 9, // TYPE_STRING, TYPE_BYTES
CPPTYPE_MESSAGE = 10, // TYPE_MESSAGE, TYPE_GROUP
MAX_CPPTYPE = 10, // Constant useful for defining lookup tables
// indexed by CppType.
};
// Identifies whether the field is optional, required, or repeated. 0 is
// reserved for errors.
enum Label {
LABEL_OPTIONAL = 1, // optional
LABEL_REQUIRED = 2, // required
LABEL_REPEATED = 3, // repeated
MAX_LABEL = 3, // Constant useful for defining lookup tables
// indexed by Label.
};
// Valid field numbers are positive integers up to kMaxNumber.
static const int kMaxNumber = (1 << 29) - 1;
// First field number reserved for the protocol buffer library implementation.
// Users may not declare fields that use reserved numbers.
static const int kFirstReservedNumber = 19000;
// Last field number reserved for the protocol buffer library implementation.
// Users may not declare fields that use reserved numbers.
static const int kLastReservedNumber = 19999;
const std::string& name() const; // Name of this field within the message.
const std::string& full_name() const; // Fully-qualified name of the field.
const std::string& json_name() const; // JSON name of this field.
const FileDescriptor* file() const; // File in which this field was defined.
bool is_extension() const; // Is this an extension field?
int number() const; // Declared tag number.
// Same as name() except converted to lower-case. This (and especially the
// FindFieldByLowercaseName() method) can be useful when parsing formats
// which prefer to use lowercase naming style. (Although, technically
// field names should be lowercased anyway according to the protobuf style
// guide, so this only makes a difference when dealing with old .proto files
// which do not follow the guide.)
const std::string& lowercase_name() const;
// Same as name() except converted to camel-case. In this conversion, any
// time an underscore appears in the name, it is removed and the next
// letter is capitalized. Furthermore, the first letter of the name is
// lower-cased. Examples:
// FooBar -> fooBar
// foo_bar -> fooBar
// fooBar -> fooBar
// This (and especially the FindFieldByCamelcaseName() method) can be useful
// when parsing formats which prefer to use camel-case naming style.
const std::string& camelcase_name() const;
Type type() const; // Declared type of this field.
const char* type_name() const; // Name of the declared type.
CppType cpp_type() const; // C++ type of this field.
const char* cpp_type_name() const; // Name of the C++ type.
Label label() const; // optional/required/repeated
bool is_required() const; // shorthand for label() == LABEL_REQUIRED
bool is_optional() const; // shorthand for label() == LABEL_OPTIONAL
bool is_repeated() const; // shorthand for label() == LABEL_REPEATED
bool is_packable() const; // shorthand for is_repeated() &&
// IsTypePackable(type())
bool is_packed() const; // shorthand for is_packable() &&
// options().packed()
bool is_map() const; // shorthand for type() == TYPE_MESSAGE &&
// message_type()->options().map_entry()
// Returns true if this field was syntactically written with "optional" in the
// .proto file. Excludes singular proto3 fields that do not have a label.
bool has_optional_keyword() const;
// Returns true if this field tracks presence, ie. does the field
// distinguish between "unset" and "present with default value."
// This includes required, optional, and oneof fields. It excludes maps,
// repeated fields, and singular proto3 fields without "optional".
//
// For fields where has_presence() == true, the return value of
// Reflection::HasField() is semantically meaningful.
bool has_presence() const;
// Index of this field within the message's field array, or the file or
// extension scope's extensions array.
int index() const;
// Does this field have an explicitly-declared default value?
bool has_default_value() const;
// Whether the user has specified the json_name field option in the .proto
// file.
bool has_json_name() const;
// Get the field default value if cpp_type() == CPPTYPE_INT32. If no
// explicit default was defined, the default is 0.
int32_t default_value_int32_t() const;
int32_t default_value_int32() const { return default_value_int32_t(); }
// Get the field default value if cpp_type() == CPPTYPE_INT64. If no
// explicit default was defined, the default is 0.
int64_t default_value_int64_t() const;
int64_t default_value_int64() const { return default_value_int64_t(); }
// Get the field default value if cpp_type() == CPPTYPE_UINT32. If no
// explicit default was defined, the default is 0.
uint32_t default_value_uint32_t() const;
uint32_t default_value_uint32() const { return default_value_uint32_t(); }
// Get the field default value if cpp_type() == CPPTYPE_UINT64. If no
// explicit default was defined, the default is 0.
uint64_t default_value_uint64_t() const;
uint64_t default_value_uint64() const { return default_value_uint64_t(); }
// Get the field default value if cpp_type() == CPPTYPE_FLOAT. If no
// explicit default was defined, the default is 0.0.
float default_value_float() const;
// Get the field default value if cpp_type() == CPPTYPE_DOUBLE. If no
// explicit default was defined, the default is 0.0.
double default_value_double() const;
// Get the field default value if cpp_type() == CPPTYPE_BOOL. If no
// explicit default was defined, the default is false.
bool default_value_bool() const;
// Get the field default value if cpp_type() == CPPTYPE_ENUM. If no
// explicit default was defined, the default is the first value defined
// in the enum type (all enum types are required to have at least one value).
// This never returns nullptr.
const EnumValueDescriptor* default_value_enum() const;
// Get the field default value if cpp_type() == CPPTYPE_STRING. If no
// explicit default was defined, the default is the empty string.
const std::string& default_value_string() const;
// The Descriptor for the message of which this is a field. For extensions,
// this is the extended type. Never nullptr.
const Descriptor* containing_type() const;
// If the field is a member of a oneof, this is the one, otherwise this is
// nullptr.
const OneofDescriptor* containing_oneof() const;
// If the field is a member of a non-synthetic oneof, returns the descriptor
// for the oneof, otherwise returns nullptr.
const OneofDescriptor* real_containing_oneof() const;
// If the field is a member of a oneof, returns the index in that oneof.
int index_in_oneof() const;
// An extension may be declared within the scope of another message. If this
// field is an extension (is_extension() is true), then extension_scope()
// returns that message, or nullptr if the extension was declared at global
// scope. If this is not an extension, extension_scope() is undefined (may
// assert-fail).
const Descriptor* extension_scope() const;
// If type is TYPE_MESSAGE or TYPE_GROUP, returns a descriptor for the
// message or the group type. Otherwise, returns null.
const Descriptor* message_type() const;
// If type is TYPE_ENUM, returns a descriptor for the enum. Otherwise,
// returns null.
const EnumDescriptor* enum_type() const;
// Get the FieldOptions for this field. This includes things listed in
// square brackets after the field definition. E.g., the field:
// optional string text = 1 [ctype=CORD];
// has the "ctype" option set. Allowed options are defined by FieldOptions in
// descriptor.proto, and any available extensions of that message.
const FieldOptions& options() const;
// See Descriptor::CopyTo().
void CopyTo(FieldDescriptorProto* proto) const;
// See Descriptor::DebugString().
std::string DebugString() const;
// See Descriptor::DebugStringWithOptions().
std::string DebugStringWithOptions(const DebugStringOptions& options) const;
// Helper method to get the CppType for a particular Type.
static CppType TypeToCppType(Type type);
// Helper method to get the name of a Type.
static const char* TypeName(Type type);
// Helper method to get the name of a CppType.
static const char* CppTypeName(CppType cpp_type);
// Return true iff [packed = true] is valid for fields of this type.
static inline bool IsTypePackable(Type field_type);
// Returns full_name() except if the field is a MessageSet extension,
// in which case it returns the full_name() of the containing message type
// for backwards compatibility with proto1.
//
// A MessageSet extension is defined as an optional message extension
// whose containing type has the message_set_wire_format option set.
// This should be true of extensions of google.protobuf.bridge.MessageSet;
// by convention, such extensions are named "message_set_extension".
//
// The opposite operation (looking up an extension's FieldDescriptor given
// its printable name) can be accomplished with
// message->file()->pool()->FindExtensionByPrintableName(message, name)
// where the extension extends "message".
const std::string& PrintableNameForExtension() const;
// Source Location ---------------------------------------------------
// Updates |*out_location| to the source location of the complete
// extent of this field declaration. Returns false and leaves
// |*out_location| unchanged iff location information was not available.
bool GetSourceLocation(SourceLocation* out_location) const;
private:
friend class Symbol;
typedef FieldOptions OptionsType;
// Allows access to GetLocationPath for annotations.
friend class io::Printer;
friend class compiler::cpp::Formatter;
friend class Reflection;
// Fill the json_name field of FieldDescriptorProto.
void CopyJsonNameTo(FieldDescriptorProto* proto) const;
// See Descriptor::DebugString().
void DebugString(int depth, std::string* contents,
const DebugStringOptions& options) const;
// formats the default value appropriately and returns it as a string.
// Must have a default value to call this. If quote_string_type is true, then
// types of CPPTYPE_STRING will be surrounded by quotes and CEscaped.
std::string DefaultValueAsString(bool quote_string_type) const;
// Helper function that returns the field type name for DebugString.
std::string FieldTypeNameDebugString() const;
// Walks up the descriptor tree to generate the source location path
// to this descriptor from the file root.
void GetLocationPath(std::vector<int>* output) const;
// Returns true if this is a map message type.
bool is_map_message_type() const;
bool has_default_value_ : 1;
bool proto3_optional_ : 1;
// Whether the user has specified the json_name field option in the .proto
// file.
bool has_json_name_ : 1;
bool is_extension_ : 1;
bool is_oneof_ : 1;
// Actually a `Label` but stored as uint8_t to save space.
uint8_t label_ : 2;
// Actually a `Type`, but stored as uint8_t to save space.
mutable uint8_t type_;
// Logically:
// all_names_ = [name, full_name, lower, camel, json]
// However:
// duplicates will be omitted, so lower/camel/json might be in the same
// position.
// We store the true offset for each name here, and the bit width must be
// large enough to account for the worst case where all names are present.
uint8_t lowercase_name_index_ : 2;
uint8_t camelcase_name_index_ : 2;
uint8_t json_name_index_ : 3;
// Sadly, `number_` located here to reduce padding. Unrelated to all_names_
// and its indices above.
int number_;
const std::string* all_names_;
const FileDescriptor* file_;
internal::once_flag* type_once_;
static void TypeOnceInit(const FieldDescriptor* to_init);
void InternalTypeOnceInit() const;
const Descriptor* containing_type_;
union {
const OneofDescriptor* containing_oneof;
const Descriptor* extension_scope;
} scope_;
union {
mutable const Descriptor* message_type;
mutable const EnumDescriptor* enum_type;
const char* lazy_type_name;
} type_descriptor_;
const FieldOptions* options_;
// IMPORTANT: If you add a new field, make sure to search for all instances
// of Allocate<FieldDescriptor>() and AllocateArray<FieldDescriptor>() in
// descriptor.cc and update them to initialize the field.
union {
int32_t default_value_int32_t_;
int64_t default_value_int64_t_;
uint32_t default_value_uint32_t_;
uint64_t default_value_uint64_t_;
float default_value_float_;
double default_value_double_;
bool default_value_bool_;
mutable const EnumValueDescriptor* default_value_enum_;
const char* lazy_default_value_enum_name_;
const std::string* default_value_string_;
mutable std::atomic<const Message*> default_generated_instance_;
};
static const CppType kTypeToCppTypeMap[MAX_TYPE + 1];
static const char* const kTypeToName[MAX_TYPE + 1];
static const char* const kCppTypeToName[MAX_CPPTYPE + 1];
static const char* const kLabelToName[MAX_LABEL + 1];
// Must be constructed using DescriptorPool.
FieldDescriptor() {}
friend class DescriptorBuilder;
friend class FileDescriptor;
friend class Descriptor;
friend class OneofDescriptor;
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FieldDescriptor);
};
// Describes a oneof defined in a message type.
class PROTOBUF_EXPORT OneofDescriptor : private internal::SymbolBase {
public:
typedef OneofDescriptorProto Proto;
const std::string& name() const; // Name of this oneof.
const std::string& full_name() const; // Fully-qualified name of the oneof.
// Index of this oneof within the message's oneof array.
int index() const;
// Returns whether this oneof was inserted by the compiler to wrap a proto3
// optional field. If this returns true, code generators should *not* emit it.
bool is_synthetic() const;
// The .proto file in which this oneof was defined. Never nullptr.
const FileDescriptor* file() const;
// The Descriptor for the message containing this oneof.
const Descriptor* containing_type() const;
// The number of (non-extension) fields which are members of this oneof.
int field_count() const;
// Get a member of this oneof, in the order in which they were declared in the