File indexing completed on 2026-10-01 08:56:09
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031 #ifndef GOOGLE_PROTOBUF_DESCRIPTOR_H__
0032 #define GOOGLE_PROTOBUF_DESCRIPTOR_H__
0033
0034 #include <atomic>
0035 #include <cstdint>
0036 #include <iterator>
0037 #include <memory>
0038 #include <string>
0039 #include <type_traits>
0040 #include <utility>
0041 #include <vector>
0042
0043 #include "absl/base/attributes.h"
0044 #include "absl/base/call_once.h"
0045 #include "absl/base/macros.h"
0046 #include "absl/base/optimization.h"
0047 #include "absl/container/flat_hash_map.h"
0048 #include "absl/functional/any_invocable.h"
0049 #include "absl/functional/function_ref.h"
0050 #include "absl/log/absl_check.h"
0051 #include "absl/log/absl_log.h"
0052 #include "absl/strings/str_format.h"
0053 #include "absl/strings/string_view.h"
0054 #include "absl/synchronization/mutex.h"
0055 #include "google/protobuf/descriptor_lite.h"
0056 #include "google/protobuf/extension_set.h"
0057 #include "google/protobuf/port.h"
0058
0059
0060 #include "google/protobuf/port_def.inc"
0061
0062 #ifdef SWIG
0063 #define PROTOBUF_EXPORT
0064 #define PROTOBUF_IGNORE_DEPRECATION_START
0065 #define PROTOBUF_IGNORE_DEPRECATION_STOP
0066 #endif
0067
0068
0069 namespace google {
0070 namespace protobuf {
0071
0072 class Descriptor;
0073 class FieldDescriptor;
0074 class OneofDescriptor;
0075 class EnumDescriptor;
0076 class EnumValueDescriptor;
0077 class ServiceDescriptor;
0078 class MethodDescriptor;
0079 class FileDescriptor;
0080 class DescriptorDatabase;
0081 class DescriptorPool;
0082
0083
0084 #ifndef SWIG
0085 enum Edition : int;
0086 enum SymbolVisibility : int;
0087 #else
0088 typedef int Edition;
0089 typedef int SymbolVisibility;
0090 #endif
0091 class DescriptorProto;
0092 class DescriptorProto_ExtensionRange;
0093 class FieldDescriptorProto;
0094 class OneofDescriptorProto;
0095 class EnumDescriptorProto;
0096 class EnumValueDescriptorProto;
0097 class ServiceDescriptorProto;
0098 class MethodDescriptorProto;
0099 class FileDescriptorProto;
0100 class MessageOptions;
0101 class FieldOptions;
0102 class OneofOptions;
0103 class EnumOptions;
0104 class EnumValueOptions;
0105 class ExtensionRangeOptions;
0106 class ServiceOptions;
0107 class MethodOptions;
0108 class FileOptions;
0109 class UninterpretedOption;
0110 class FeatureSet;
0111 class FeatureSetDefaults;
0112 class SourceCodeInfo;
0113
0114
0115 class MessageLite;
0116
0117
0118 class Message;
0119 class Reflection;
0120
0121
0122 class DescriptorBuilder;
0123 class FileDescriptorTables;
0124 class Symbol;
0125
0126
0127 class UnknownField;
0128
0129
0130 namespace compiler {
0131 class CodeGenerator;
0132 class CommandLineInterface;
0133 namespace cpp {
0134 class CppGenerator;
0135
0136 class Formatter;
0137 }
0138 namespace java {
0139 class MemoizeProjection;
0140 }
0141 }
0142
0143 namespace descriptor_unittest {
0144 class DescriptorPoolMemoizationTest;
0145 class DescriptorTest;
0146 class ValidationErrorTest;
0147 }
0148
0149
0150 namespace io {
0151 class Printer;
0152 }
0153
0154 namespace internal {
0155 class InternalFeatureHelper;
0156 }
0157
0158
0159 struct SourceLocation {
0160 int start_line;
0161 int end_line;
0162 int start_column;
0163 int end_column;
0164
0165
0166
0167 std::string leading_comments;
0168 std::string trailing_comments;
0169 std::vector<std::string> leading_detached_comments;
0170 };
0171
0172
0173
0174 struct DebugStringOptions {
0175
0176
0177
0178
0179 bool include_comments;
0180
0181 bool elide_group_body;
0182 bool elide_oneof_body;
0183
0184 DebugStringOptions()
0185 : include_comments(false),
0186 elide_group_body(false),
0187 elide_oneof_body(false) {
0188 }
0189 };
0190
0191
0192
0193
0194
0195 namespace internal {
0196
0197
0198
0199
0200
0201
0202
0203
0204 #if !defined(PROTOBUF_INTERNAL_CHECK_CLASS_SIZE)
0205 #define PROTOBUF_INTERNAL_CHECK_CLASS_SIZE(t, expected)
0206 #endif
0207
0208
0209
0210
0211
0212
0213
0214
0215
0216
0217
0218
0219
0220
0221
0222
0223
0224
0225
0226
0227
0228
0229
0230
0231
0232
0233 class DescriptorNames {
0234 public:
0235
0236 DescriptorNames() = default;
0237 explicit DescriptorNames(const char* payload) : payload_(payload) {}
0238
0239
0240
0241
0242 absl::string_view name() const { return get(get_size(0) + 1, get_size(0)); }
0243 absl::string_view full_name() const {
0244 return get(get_size(1) + 1, get_size(1));
0245 }
0246
0247
0248
0249 absl::string_view lowercase_name() const {
0250 return get(get_size(2), get_size(3));
0251 }
0252 absl::string_view camelcase_name() const {
0253 return get(get_size(4), get_size(5));
0254 }
0255 absl::string_view json_name() const { return get(get_size(6), get_size(7)); }
0256
0257 static constexpr size_t AllocationSizeForSimpleNames(size_t full_name_size) {
0258 return full_name_size + 1 + 2 * sizeof(uint16_t);
0259 }
0260
0261 private:
0262 size_t get_size(int index) const {
0263
0264
0265 uint16_t size;
0266 memcpy(&size, payload_ + index * sizeof(size), sizeof(size));
0267 return size;
0268 }
0269
0270 absl::string_view get(size_t offset, size_t size) const {
0271 return absl::string_view(payload_ - offset, size);
0272 }
0273
0274 const char* payload_;
0275 };
0276
0277 class FlatAllocator;
0278
0279 class PROTOBUF_EXPORT LazyDescriptor {
0280 public:
0281
0282
0283 void Init() {
0284 descriptor_ = nullptr;
0285 once_ = nullptr;
0286 }
0287
0288
0289
0290
0291
0292 void Set(const Descriptor* descriptor);
0293
0294
0295
0296
0297
0298
0299 void SetLazy(absl::string_view name, const FileDescriptor* file);
0300
0301
0302
0303
0304 inline const Descriptor* Get(const ServiceDescriptor* service) {
0305 Once(service);
0306 return descriptor_;
0307 }
0308
0309 private:
0310 void Once(const ServiceDescriptor* service);
0311
0312 const Descriptor* descriptor_;
0313
0314 absl::once_flag* once_;
0315 };
0316
0317 class PROTOBUF_EXPORT SymbolBase {
0318 private:
0319 friend class google::protobuf::Symbol;
0320 uint8_t symbol_type_;
0321 };
0322
0323
0324
0325
0326
0327 template <int N>
0328 class PROTOBUF_EXPORT SymbolBaseN : public SymbolBase {};
0329
0330 PROTOBUF_EXPORT absl::string_view ShortEditionName(Edition edition);
0331
0332 bool IsEnumFullySequential(const EnumDescriptor* enum_desc);
0333
0334 const std::string& DefaultValueStringAsString(const FieldDescriptor* field);
0335 const std::string& NameOfEnumAsString(const EnumValueDescriptor* descriptor);
0336
0337 }
0338
0339
0340 template <typename Sink>
0341 void AbslStringify(Sink& sink, Edition edition) {
0342 absl::Format(&sink, "%v", internal::ShortEditionName(edition));
0343 }
0344
0345
0346
0347
0348
0349
0350 class PROTOBUF_EXPORT Descriptor : private internal::SymbolBase {
0351 public:
0352 typedef DescriptorProto Proto;
0353 #ifndef SWIG
0354 Descriptor(const Descriptor&) = delete;
0355 Descriptor& operator=(const Descriptor&) = delete;
0356 #endif
0357
0358
0359 absl::string_view name() const;
0360
0361
0362
0363
0364
0365
0366 absl::string_view full_name() const;
0367
0368
0369
0370 int index() const;
0371
0372
0373 const FileDescriptor* file() const;
0374
0375
0376
0377 const Descriptor* containing_type() const;
0378
0379
0380
0381
0382
0383 const MessageOptions& options() const;
0384
0385
0386
0387
0388 void CopyTo(DescriptorProto* proto) const;
0389
0390
0391
0392
0393
0394 void CopyHeadingTo(DescriptorProto* proto) const;
0395
0396
0397
0398 std::string DebugString() const;
0399
0400
0401
0402 std::string DebugStringWithOptions(const DebugStringOptions& options) const;
0403
0404
0405 template <typename Sink>
0406 friend void AbslStringify(Sink& sink, const Descriptor& d) {
0407 absl::Format(&sink, "%s", d.DebugString());
0408 }
0409
0410
0411
0412
0413 bool is_placeholder() const;
0414
0415 enum WellKnownType {
0416 WELLKNOWNTYPE_UNSPECIFIED,
0417
0418
0419 WELLKNOWNTYPE_DOUBLEVALUE,
0420 WELLKNOWNTYPE_FLOATVALUE,
0421 WELLKNOWNTYPE_INT64VALUE,
0422 WELLKNOWNTYPE_UINT64VALUE,
0423 WELLKNOWNTYPE_INT32VALUE,
0424 WELLKNOWNTYPE_UINT32VALUE,
0425 WELLKNOWNTYPE_STRINGVALUE,
0426 WELLKNOWNTYPE_BYTESVALUE,
0427 WELLKNOWNTYPE_BOOLVALUE,
0428
0429
0430 WELLKNOWNTYPE_ANY,
0431 WELLKNOWNTYPE_FIELDMASK,
0432 WELLKNOWNTYPE_DURATION,
0433 WELLKNOWNTYPE_TIMESTAMP,
0434 WELLKNOWNTYPE_VALUE,
0435 WELLKNOWNTYPE_LISTVALUE,
0436 WELLKNOWNTYPE_STRUCT,
0437
0438
0439
0440 __WELLKNOWNTYPE__DO_NOT_USE__ADD_DEFAULT_INSTEAD__,
0441 };
0442
0443 WellKnownType well_known_type() const;
0444
0445
0446
0447
0448 int field_count() const;
0449
0450
0451
0452
0453 const FieldDescriptor* field(int index) const;
0454
0455
0456
0457 const FieldDescriptor* FindFieldByNumber(int number) const;
0458
0459 const FieldDescriptor* FindFieldByName(absl::string_view name) const;
0460
0461
0462
0463
0464 const FieldDescriptor* FindFieldByLowercaseName(
0465 absl::string_view lowercase_name) const;
0466
0467
0468
0469
0470
0471 const FieldDescriptor* FindFieldByCamelcaseName(
0472 absl::string_view camelcase_name) const;
0473
0474
0475 int oneof_decl_count() const;
0476
0477
0478
0479 int real_oneof_decl_count() const;
0480
0481
0482 const OneofDescriptor* oneof_decl(int index) const;
0483
0484
0485
0486 const OneofDescriptor* real_oneof_decl(int index) const;
0487
0488
0489 const OneofDescriptor* FindOneofByName(absl::string_view name) const;
0490
0491
0492
0493
0494 int nested_type_count() const;
0495
0496
0497 const Descriptor* nested_type(int index) const;
0498
0499
0500
0501 const Descriptor* FindNestedTypeByName(absl::string_view name) const;
0502
0503
0504
0505
0506 int enum_type_count() const;
0507
0508
0509 const EnumDescriptor* enum_type(int index) const;
0510
0511
0512
0513 const EnumDescriptor* FindEnumTypeByName(absl::string_view name) const;
0514
0515
0516
0517 const EnumValueDescriptor* FindEnumValueByName(absl::string_view name) const;
0518
0519
0520
0521
0522
0523 class PROTOBUF_EXPORT ExtensionRange {
0524 public:
0525 typedef DescriptorProto_ExtensionRange Proto;
0526
0527 typedef ExtensionRangeOptions OptionsType;
0528
0529
0530 void CopyTo(DescriptorProto_ExtensionRange* proto) const;
0531
0532
0533 int start_number() const { return start_; }
0534
0535
0536 int end_number() const { return end_; }
0537
0538
0539
0540 int index() const;
0541
0542
0543 const ExtensionRangeOptions& options() const { return *options_; }
0544
0545
0546 absl::string_view name() const { return containing_type_->name(); }
0547
0548
0549 absl::string_view full_name() const {
0550 return containing_type_->full_name();
0551 }
0552
0553
0554
0555 const FileDescriptor* file() const { return containing_type_->file(); }
0556
0557
0558
0559 const Descriptor* containing_type() const { return containing_type_; }
0560
0561 private:
0562 int start_;
0563 int end_;
0564 const ExtensionRangeOptions* options_;
0565
0566 private:
0567 const Descriptor* containing_type_;
0568 const FeatureSet* proto_features_;
0569 const FeatureSet* merged_features_;
0570
0571
0572
0573
0574
0575 const FeatureSet& features() const { return *merged_features_; }
0576 friend class internal::InternalFeatureHelper;
0577
0578
0579
0580 void GetLocationPath(std::vector<int>* output) const;
0581
0582 friend class Descriptor;
0583 friend class DescriptorPool;
0584 friend class DescriptorBuilder;
0585 };
0586
0587
0588 int extension_range_count() const;
0589
0590
0591
0592 const ExtensionRange* extension_range(int index) const;
0593
0594
0595 bool IsExtensionNumber(int number) const;
0596
0597
0598 const ExtensionRange* FindExtensionRangeContainingNumber(int number) const;
0599
0600
0601
0602
0603
0604
0605
0606
0607
0608
0609
0610
0611
0612
0613
0614
0615
0616
0617
0618
0619
0620
0621
0622
0623
0624 int extension_count() const;
0625
0626
0627 const FieldDescriptor* extension(int index) const;
0628
0629
0630
0631 const FieldDescriptor* FindExtensionByName(absl::string_view name) const;
0632
0633
0634
0635 const FieldDescriptor* FindExtensionByLowercaseName(
0636 absl::string_view name) const;
0637
0638
0639
0640 const FieldDescriptor* FindExtensionByCamelcaseName(
0641 absl::string_view name) const;
0642
0643
0644
0645
0646 struct ReservedRange {
0647 int start;
0648 int end;
0649 };
0650
0651
0652 int reserved_range_count() const;
0653
0654
0655
0656 const ReservedRange* reserved_range(int index) const;
0657
0658
0659 bool IsReservedNumber(int number) const;
0660
0661
0662 const ReservedRange* FindReservedRangeContainingNumber(int number) const;
0663
0664
0665 int reserved_name_count() const;
0666
0667
0668 absl::string_view reserved_name(int index) const;
0669
0670
0671 bool IsReservedName(absl::string_view name) const;
0672
0673
0674
0675
0676
0677
0678 bool GetSourceLocation(SourceLocation* out_location) const;
0679
0680
0681
0682
0683
0684 const FieldDescriptor* map_key() const;
0685
0686
0687
0688 const FieldDescriptor* map_value() const;
0689
0690 private:
0691 friend class Symbol;
0692 typedef MessageOptions OptionsType;
0693
0694
0695 friend class descriptor_unittest::DescriptorTest;
0696
0697
0698 friend class io::Printer;
0699 friend class compiler::cpp::Formatter;
0700
0701
0702
0703
0704
0705 const FeatureSet& features() const { return *merged_features_; }
0706 friend class internal::InternalFeatureHelper;
0707
0708
0709 void CopyJsonNameTo(DescriptorProto* proto) const;
0710
0711
0712
0713
0714
0715 void DebugString(int depth, std::string* contents,
0716 const DebugStringOptions& options,
0717 bool include_opening_clause) const;
0718
0719
0720
0721 void GetLocationPath(std::vector<int>* output) const;
0722
0723
0724 SymbolVisibility visibility_keyword() const;
0725
0726
0727 bool is_placeholder_ : 1;
0728
0729 bool is_unqualified_placeholder_ : 1;
0730
0731 uint8_t well_known_type_ : 5;
0732
0733 uint8_t visibility_ : 2;
0734
0735
0736
0737
0738
0739
0740
0741
0742 uint16_t sequential_field_limit_;
0743
0744 int field_count_;
0745
0746 internal::DescriptorNames all_names_;
0747 const FileDescriptor* file_;
0748 const Descriptor* containing_type_;
0749 const MessageOptions* options_;
0750 const FeatureSet* proto_features_;
0751 const FeatureSet* merged_features_;
0752
0753
0754 FieldDescriptor* fields_;
0755 OneofDescriptor* oneof_decls_;
0756 Descriptor* nested_types_;
0757 EnumDescriptor* enum_types_;
0758 ExtensionRange* extension_ranges_;
0759 FieldDescriptor* extensions_;
0760 ReservedRange* reserved_ranges_;
0761 const std::string** reserved_names_;
0762
0763 int oneof_decl_count_;
0764 int real_oneof_decl_count_;
0765 int nested_type_count_;
0766 int enum_type_count_;
0767 int extension_range_count_;
0768 int extension_count_;
0769 int reserved_range_count_;
0770 int reserved_name_count_;
0771
0772
0773
0774
0775
0776
0777 Descriptor();
0778 friend class DescriptorBuilder;
0779 friend class DescriptorPool;
0780 friend class EnumDescriptor;
0781 friend class FieldDescriptor;
0782 friend class FileDescriptorTables;
0783 friend class OneofDescriptor;
0784 friend class MethodDescriptor;
0785 friend class FileDescriptor;
0786 };
0787
0788 PROTOBUF_INTERNAL_CHECK_CLASS_SIZE(Descriptor, 160);
0789
0790
0791
0792
0793
0794
0795
0796
0797
0798
0799
0800 class PROTOBUF_EXPORT FieldDescriptor : private internal::SymbolBase,
0801 public internal::FieldDescriptorLite {
0802 public:
0803 typedef FieldDescriptorProto Proto;
0804
0805 #ifndef SWIG
0806 FieldDescriptor(const FieldDescriptor&) = delete;
0807 FieldDescriptor& operator=(const FieldDescriptor&) = delete;
0808 #endif
0809
0810
0811
0812
0813
0814
0815
0816
0817
0818
0819
0820
0821
0822
0823
0824
0825
0826
0827
0828
0829
0830
0831
0832
0833
0834
0835
0836
0837
0838
0839
0840
0841
0842
0843
0844
0845
0846
0847
0848
0849
0850
0851
0852
0853
0854
0855
0856
0857
0858
0859
0860
0861
0862
0863
0864
0865
0866
0867
0868
0869
0870
0871
0872
0873
0874
0875 static const int kMaxNumber = (1 << 29) - 1;
0876
0877
0878
0879 static const int kFirstReservedNumber = 19000;
0880
0881
0882 static const int kLastReservedNumber = 19999;
0883
0884
0885 absl::string_view name() const;
0886
0887 absl::string_view full_name() const;
0888
0889 absl::string_view json_name() const;
0890
0891 const FileDescriptor* file() const;
0892 bool is_extension() const;
0893 int number() const;
0894
0895
0896
0897
0898
0899
0900
0901 absl::string_view lowercase_name() const;
0902
0903
0904
0905
0906
0907
0908
0909
0910
0911
0912 absl::string_view camelcase_name() const;
0913
0914 Type type() const;
0915
0916 absl::string_view type_name() const;
0917 CppType cpp_type() const;
0918
0919 absl::string_view cpp_type_name() const;
0920
0921
0922
0923 ABSL_DEPRECATED("Use is_required() or is_repeated() instead.")
0924 Label label() const;
0925
0926 #ifndef SWIG
0927 CppStringType cpp_string_type() const;
0928 #endif
0929
0930
0931
0932 bool is_required() const;
0933 bool is_repeated() const;
0934
0935 ABSL_DEPRECATE_AND_INLINE()
0936 bool is_optional() const;
0937
0938 bool is_packable() const;
0939
0940 bool is_map() const;
0941
0942
0943
0944
0945
0946 bool is_packed() const;
0947
0948
0949
0950
0951
0952
0953
0954
0955 bool has_presence() const;
0956
0957
0958
0959 bool requires_utf8_validation() const;
0960
0961
0962
0963
0964
0965
0966
0967
0968
0969
0970
0971
0972
0973
0974
0975
0976
0977
0978 bool legacy_enum_field_treated_as_closed() const;
0979
0980
0981
0982 int index() const;
0983
0984
0985 bool has_default_value() const;
0986
0987
0988
0989 bool has_json_name() const;
0990
0991
0992
0993 int32_t default_value_int32_t() const;
0994 int32_t default_value_int32() const { return default_value_int32_t(); }
0995
0996
0997 int64_t default_value_int64_t() const;
0998 int64_t default_value_int64() const { return default_value_int64_t(); }
0999
1000
1001 uint32_t default_value_uint32_t() const;
1002 uint32_t default_value_uint32() const { return default_value_uint32_t(); }
1003
1004
1005 uint64_t default_value_uint64_t() const;
1006 uint64_t default_value_uint64() const { return default_value_uint64_t(); }
1007
1008
1009 float default_value_float() const;
1010
1011
1012 double default_value_double() const;
1013
1014
1015 bool default_value_bool() const;
1016
1017
1018
1019
1020 const EnumValueDescriptor* default_value_enum() const;
1021
1022
1023 absl::string_view default_value_string() const;
1024
1025
1026
1027 const Descriptor* containing_type() const;
1028
1029
1030
1031 const OneofDescriptor* containing_oneof() const;
1032
1033
1034
1035 const OneofDescriptor* real_containing_oneof() const;
1036
1037
1038 int index_in_oneof() const;
1039
1040
1041
1042
1043
1044
1045 const Descriptor* extension_scope() const;
1046
1047
1048
1049 const Descriptor* message_type() const;
1050
1051
1052 const EnumDescriptor* enum_type() const;
1053
1054
1055
1056
1057
1058
1059 const FieldOptions& options() const;
1060
1061
1062 void CopyTo(FieldDescriptorProto* proto) const;
1063
1064
1065 std::string DebugString() const;
1066
1067
1068 std::string DebugStringWithOptions(const DebugStringOptions& options) const;
1069
1070
1071 template <typename Sink>
1072 friend void AbslStringify(Sink& sink, const FieldDescriptor& d) {
1073 absl::Format(&sink, "%s", d.DebugString());
1074 }
1075
1076
1077 static CppType TypeToCppType(Type type);
1078
1079
1080 static absl::string_view TypeName(Type type);
1081
1082
1083 static absl::string_view CppTypeName(CppType cpp_type);
1084
1085
1086 static inline bool IsTypePackable(Type field_type);
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101 absl::string_view PrintableNameForExtension() const;
1102
1103
1104
1105
1106
1107
1108 bool GetSourceLocation(SourceLocation* out_location) const;
1109
1110 private:
1111 friend class Symbol;
1112 typedef FieldOptions OptionsType;
1113
1114
1115 friend class io::Printer;
1116 friend class compiler::cpp::Formatter;
1117 friend class Reflection;
1118 friend class FieldDescriptorLegacy;
1119 friend const std::string& internal::DefaultValueStringAsString(
1120 const FieldDescriptor* field);
1121
1122
1123
1124
1125
1126 friend class compiler::cpp::CppGenerator;
1127 int legacy_proto_ctype() const { return legacy_proto_ctype_; }
1128 bool has_legacy_proto_ctype() const;
1129
1130
1131
1132 ABSL_DEPRECATED("Use has_presence() instead.")
1133 bool has_optional_keyword() const;
1134
1135
1136
1137
1138
1139 const FeatureSet& features() const { return *merged_features_; }
1140 friend class internal::InternalFeatureHelper;
1141
1142
1143 void CopyJsonNameTo(FieldDescriptorProto* proto) const;
1144
1145
1146 void DebugString(int depth, std::string* contents,
1147 const DebugStringOptions& options) const;
1148
1149
1150
1151
1152 std::string DefaultValueAsString(bool quote_string_type) const;
1153
1154
1155 std::string FieldTypeNameDebugString() const;
1156
1157
1158
1159 void GetLocationPath(std::vector<int>* output) const;
1160
1161
1162 bool is_map_message_type() const;
1163
1164 CppStringType CalculateCppStringType() const;
1165
1166 bool has_default_value_ : 1;
1167 bool proto3_optional_ : 1;
1168
1169
1170 bool has_json_name_ : 1;
1171 bool is_extension_ : 1;
1172 bool is_oneof_ : 1;
1173 bool is_repeated_ : 1;
1174
1175
1176 uint8_t label_ : 2;
1177
1178
1179 uint8_t type_;
1180
1181
1182
1183 uint8_t cpp_string_type_ : 3;
1184
1185
1186
1187 bool in_real_oneof_ : 1;
1188
1189
1190
1191 bool is_map_ : 1;
1192
1193
1194
1195 uint8_t legacy_proto_ctype_ : 2;
1196
1197
1198
1199 int number_;
1200 internal::DescriptorNames all_names_;
1201 const FileDescriptor* file_;
1202
1203
1204
1205 absl::once_flag* type_once_;
1206 static void TypeOnceInit(const FieldDescriptor* to_init);
1207 void InternalTypeOnceInit() const;
1208 const Descriptor* containing_type_;
1209 union {
1210 const OneofDescriptor* containing_oneof;
1211 const Descriptor* extension_scope;
1212 } scope_;
1213 union {
1214 mutable const Descriptor* message_type;
1215 mutable const EnumDescriptor* enum_type;
1216 } type_descriptor_;
1217 const FieldOptions* options_;
1218 const FeatureSet* proto_features_;
1219 const FeatureSet* merged_features_;
1220
1221
1222
1223
1224 union {
1225 int32_t default_value_int32_t_;
1226 int64_t default_value_int64_t_;
1227 uint32_t default_value_uint32_t_;
1228 uint64_t default_value_uint64_t_;
1229 float default_value_float_;
1230 double default_value_double_;
1231 bool default_value_bool_;
1232
1233 mutable const EnumValueDescriptor* default_value_enum_;
1234 const std::string* default_value_string_;
1235 mutable std::atomic<const Message*> default_generated_instance_;
1236 };
1237
1238 static const CppType kTypeToCppTypeMap[MAX_TYPE + 1];
1239
1240 static const char* const kTypeToName[MAX_TYPE + 1];
1241
1242 static const char* const kCppTypeToName[MAX_CPPTYPE + 1];
1243
1244 static const char* const kLabelToName[MAX_LABEL + 1];
1245
1246
1247 FieldDescriptor();
1248 friend class DescriptorBuilder;
1249 friend class FileDescriptor;
1250 friend class Descriptor;
1251 friend class OneofDescriptor;
1252 };
1253
1254 PROTOBUF_INTERNAL_CHECK_CLASS_SIZE(FieldDescriptor, 88);
1255
1256
1257 class PROTOBUF_EXPORT OneofDescriptor : private internal::SymbolBase {
1258 public:
1259 typedef OneofDescriptorProto Proto;
1260
1261 #ifndef SWIG
1262 OneofDescriptor(const OneofDescriptor&) = delete;
1263 OneofDescriptor& operator=(const OneofDescriptor&) = delete;
1264 #endif
1265
1266
1267 absl::string_view name() const;
1268
1269 absl::string_view full_name() const;
1270
1271
1272 int index() const;
1273
1274
1275 const FileDescriptor* file() const;
1276
1277 const Descriptor* containing_type() const;
1278
1279
1280 int field_count() const;
1281
1282
1283 const FieldDescriptor* field(int index) const;
1284
1285 const OneofOptions& options() const;
1286
1287
1288 void CopyTo(OneofDescriptorProto* proto) const;
1289
1290
1291 std::string DebugString() const;
1292
1293
1294 std::string DebugStringWithOptions(const DebugStringOptions& options) const;
1295
1296
1297 template <typename Sink>
1298 friend void AbslStringify(Sink& sink, const OneofDescriptor& d) {
1299 absl::Format(&sink, "%s", d.DebugString());
1300 }
1301
1302
1303
1304
1305
1306
1307 bool GetSourceLocation(SourceLocation* out_location) const;
1308
1309 private:
1310 friend class Symbol;
1311 typedef OneofOptions OptionsType;
1312
1313
1314 friend class io::Printer;
1315 friend class compiler::cpp::Formatter;
1316 friend class OneofDescriptorLegacy;
1317
1318
1319
1320 bool is_synthetic() const;
1321
1322
1323
1324
1325
1326 const FeatureSet& features() const { return *merged_features_; }
1327 friend class internal::InternalFeatureHelper;
1328
1329
1330 void DebugString(int depth, std::string* contents,
1331 const DebugStringOptions& options) const;
1332
1333
1334
1335 void GetLocationPath(std::vector<int>* output) const;
1336
1337 int field_count_;
1338
1339 internal::DescriptorNames all_names_;
1340 const Descriptor* containing_type_;
1341 const OneofOptions* options_;
1342 const FeatureSet* proto_features_;
1343 const FeatureSet* merged_features_;
1344 const FieldDescriptor* fields_;
1345
1346
1347
1348
1349
1350
1351 OneofDescriptor();
1352 friend class DescriptorBuilder;
1353 friend class Descriptor;
1354 friend class FieldDescriptor;
1355 friend class Reflection;
1356 };
1357
1358 PROTOBUF_INTERNAL_CHECK_CLASS_SIZE(OneofDescriptor, 56);
1359
1360
1361
1362
1363 class PROTOBUF_EXPORT EnumDescriptor : private internal::SymbolBase {
1364 public:
1365 typedef EnumDescriptorProto Proto;
1366
1367 #ifndef SWIG
1368 EnumDescriptor(const EnumDescriptor&) = delete;
1369 EnumDescriptor& operator=(const EnumDescriptor&) = delete;
1370 #endif
1371
1372
1373 absl::string_view name() const;
1374
1375
1376 absl::string_view full_name() const;
1377
1378
1379 int index() const;
1380
1381
1382 const FileDescriptor* file() const;
1383
1384
1385
1386 int value_count() const;
1387
1388
1389
1390 const EnumValueDescriptor* value(int index) const;
1391
1392
1393 const EnumValueDescriptor* FindValueByName(absl::string_view name) const;
1394
1395
1396 const EnumValueDescriptor* FindValueByNumber(int number) const;
1397
1398
1399
1400 const Descriptor* containing_type() const;
1401
1402
1403
1404
1405
1406 const EnumOptions& options() const;
1407
1408
1409 void CopyTo(EnumDescriptorProto* proto) const;
1410
1411
1412 std::string DebugString() const;
1413
1414
1415 std::string DebugStringWithOptions(const DebugStringOptions& options) const;
1416
1417
1418 template <typename Sink>
1419 friend void AbslStringify(Sink& sink, const EnumDescriptor& d) {
1420 absl::Format(&sink, "%s", d.DebugString());
1421 }
1422
1423
1424
1425
1426 bool is_placeholder() const;
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445 bool is_closed() const;
1446
1447
1448
1449
1450 struct ReservedRange {
1451 int start;
1452 int end;
1453 };
1454
1455
1456 int reserved_range_count() const;
1457
1458
1459
1460 const EnumDescriptor::ReservedRange* reserved_range(int index) const;
1461
1462
1463 bool IsReservedNumber(int number) const;
1464
1465
1466 const EnumDescriptor::ReservedRange* FindReservedRangeContainingNumber(
1467 int number) const;
1468
1469
1470 int reserved_name_count() const;
1471
1472
1473 absl::string_view reserved_name(int index) const;
1474
1475
1476 bool IsReservedName(absl::string_view name) const;
1477
1478
1479
1480
1481
1482
1483 bool GetSourceLocation(SourceLocation* out_location) const;
1484
1485 private:
1486 friend class Symbol;
1487 friend bool internal::IsEnumFullySequential(const EnumDescriptor* enum_desc);
1488 typedef EnumOptions OptionsType;
1489
1490
1491 friend class io::Printer;
1492 friend class compiler::cpp::Formatter;
1493
1494
1495 friend class descriptor_unittest::DescriptorTest;
1496
1497
1498
1499
1500
1501 const FeatureSet& features() const { return *merged_features_; }
1502 friend class internal::InternalFeatureHelper;
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512 const EnumValueDescriptor* FindValueByNumberCreatingIfUnknown(
1513 int number) const;
1514
1515
1516 void DebugString(int depth, std::string* contents,
1517 const DebugStringOptions& options) const;
1518
1519
1520
1521 void GetLocationPath(std::vector<int>* output) const;
1522
1523
1524 SymbolVisibility visibility_keyword() const;
1525
1526
1527 bool is_placeholder_ : 1;
1528
1529 bool is_unqualified_placeholder_ : 1;
1530
1531
1532 uint8_t visibility_ : 2;
1533
1534
1535
1536
1537
1538
1539
1540
1541 int16_t sequential_value_limit_;
1542
1543 int value_count_;
1544
1545 internal::DescriptorNames all_names_;
1546 const FileDescriptor* file_;
1547 const Descriptor* containing_type_;
1548 const EnumOptions* options_;
1549 const FeatureSet* proto_features_;
1550 const FeatureSet* merged_features_;
1551 EnumValueDescriptor* values_;
1552
1553 int reserved_range_count_;
1554 int reserved_name_count_;
1555 EnumDescriptor::ReservedRange* reserved_ranges_;
1556 const std::string** reserved_names_;
1557
1558
1559
1560
1561
1562
1563 EnumDescriptor();
1564 friend class DescriptorBuilder;
1565 friend class Descriptor;
1566 friend class FieldDescriptor;
1567 friend class FileDescriptorTables;
1568 friend class EnumValueDescriptor;
1569 friend class FileDescriptor;
1570 friend class DescriptorPool;
1571 friend class Reflection;
1572 };
1573
1574 PROTOBUF_INTERNAL_CHECK_CLASS_SIZE(EnumDescriptor, 88);
1575
1576
1577
1578
1579
1580
1581 class PROTOBUF_EXPORT EnumValueDescriptor : private internal::SymbolBaseN<0>,
1582 private internal::SymbolBaseN<1> {
1583 public:
1584 typedef EnumValueDescriptorProto Proto;
1585
1586 #ifndef SWIG
1587 EnumValueDescriptor(const EnumValueDescriptor&) = delete;
1588 EnumValueDescriptor& operator=(const EnumValueDescriptor&) = delete;
1589 #endif
1590
1591 absl::string_view name() const;
1592 int index() const;
1593 int number() const;
1594
1595
1596
1597
1598
1599
1600 absl::string_view full_name() const;
1601
1602
1603 const FileDescriptor* file() const;
1604
1605 const EnumDescriptor* type() const;
1606
1607
1608
1609
1610
1611 const EnumValueOptions& options() const;
1612
1613
1614 void CopyTo(EnumValueDescriptorProto* proto) const;
1615
1616
1617 std::string DebugString() const;
1618
1619
1620 std::string DebugStringWithOptions(const DebugStringOptions& options) const;
1621
1622
1623 template <typename Sink>
1624 friend void AbslStringify(Sink& sink, const EnumValueDescriptor& d) {
1625 absl::Format(&sink, "%s", d.DebugString());
1626 }
1627
1628
1629
1630
1631
1632
1633 bool GetSourceLocation(SourceLocation* out_location) const;
1634
1635 private:
1636 friend class Symbol;
1637 typedef EnumValueOptions OptionsType;
1638
1639
1640 friend class io::Printer;
1641 friend class compiler::cpp::Formatter;
1642 friend const std::string& internal::NameOfEnumAsString(
1643 const EnumValueDescriptor* descriptor);
1644
1645
1646
1647
1648
1649 const FeatureSet& features() const { return *merged_features_; }
1650 friend class internal::InternalFeatureHelper;
1651
1652
1653 void DebugString(int depth, std::string* contents,
1654 const DebugStringOptions& options) const;
1655
1656
1657
1658 void GetLocationPath(std::vector<int>* output) const;
1659
1660 int number_;
1661
1662
1663
1664 const std::string* all_names_;
1665 const EnumDescriptor* type_;
1666 const EnumValueOptions* options_;
1667 const FeatureSet* proto_features_;
1668 const FeatureSet* merged_features_;
1669
1670
1671
1672
1673
1674 EnumValueDescriptor();
1675 friend class DescriptorBuilder;
1676 friend class EnumDescriptor;
1677 friend class DescriptorPool;
1678 friend class FileDescriptorTables;
1679 friend class Reflection;
1680 };
1681
1682 PROTOBUF_INTERNAL_CHECK_CLASS_SIZE(EnumValueDescriptor, 48);
1683
1684
1685
1686 class PROTOBUF_EXPORT ServiceDescriptor : private internal::SymbolBase {
1687 public:
1688 typedef ServiceDescriptorProto Proto;
1689
1690 #ifndef SWIG
1691 ServiceDescriptor(const ServiceDescriptor&) = delete;
1692 ServiceDescriptor& operator=(const ServiceDescriptor&) = delete;
1693 #endif
1694
1695
1696 absl::string_view name() const;
1697
1698 absl::string_view full_name() const;
1699
1700 int index() const;
1701
1702
1703 const FileDescriptor* file() const;
1704
1705
1706
1707
1708
1709 const ServiceOptions& options() const;
1710
1711
1712 int method_count() const;
1713
1714
1715 const MethodDescriptor* method(int index) const;
1716
1717
1718 const MethodDescriptor* FindMethodByName(absl::string_view name) const;
1719
1720
1721 void CopyTo(ServiceDescriptorProto* proto) const;
1722
1723
1724 std::string DebugString() const;
1725
1726
1727 std::string DebugStringWithOptions(const DebugStringOptions& options) const;
1728
1729
1730 template <typename Sink>
1731 friend void AbslStringify(Sink& sink, const ServiceDescriptor& d) {
1732 absl::Format(&sink, "%s", d.DebugString());
1733 }
1734
1735
1736
1737
1738
1739
1740 bool GetSourceLocation(SourceLocation* out_location) const;
1741
1742 private:
1743 friend class Symbol;
1744 typedef ServiceOptions OptionsType;
1745
1746
1747 friend class io::Printer;
1748 friend class compiler::cpp::Formatter;
1749
1750
1751
1752
1753
1754 const FeatureSet& features() const { return *merged_features_; }
1755 friend class internal::InternalFeatureHelper;
1756
1757
1758 void DebugString(std::string* contents,
1759 const DebugStringOptions& options) const;
1760
1761
1762
1763 void GetLocationPath(std::vector<int>* output) const;
1764
1765 internal::DescriptorNames all_names_;
1766 const FileDescriptor* file_;
1767 const ServiceOptions* options_;
1768 const FeatureSet* proto_features_;
1769 const FeatureSet* merged_features_;
1770 MethodDescriptor* methods_;
1771 int method_count_;
1772
1773
1774
1775
1776
1777 ServiceDescriptor();
1778 friend class DescriptorBuilder;
1779 friend class FileDescriptor;
1780 friend class MethodDescriptor;
1781 };
1782
1783 PROTOBUF_INTERNAL_CHECK_CLASS_SIZE(ServiceDescriptor, 64);
1784
1785
1786
1787
1788
1789 class PROTOBUF_EXPORT MethodDescriptor : private internal::SymbolBase {
1790 public:
1791 typedef MethodDescriptorProto Proto;
1792
1793 #ifndef SWIG
1794 MethodDescriptor(const MethodDescriptor&) = delete;
1795 MethodDescriptor& operator=(const MethodDescriptor&) = delete;
1796 #endif
1797
1798
1799 absl::string_view name() const;
1800
1801 absl::string_view full_name() const;
1802
1803 int index() const;
1804
1805
1806 const FileDescriptor* file() const;
1807
1808 const ServiceDescriptor* service() const;
1809
1810
1811 const Descriptor* input_type() const;
1812
1813 const Descriptor* output_type() const;
1814
1815
1816 bool client_streaming() const;
1817
1818 bool server_streaming() const;
1819
1820
1821
1822
1823
1824 const MethodOptions& options() const;
1825
1826
1827 void CopyTo(MethodDescriptorProto* proto) const;
1828
1829
1830 std::string DebugString() const;
1831
1832
1833 std::string DebugStringWithOptions(const DebugStringOptions& options) const;
1834
1835
1836 template <typename Sink>
1837 friend void AbslStringify(Sink& sink, const MethodDescriptor& d) {
1838 absl::Format(&sink, "%s", d.DebugString());
1839 }
1840
1841
1842
1843
1844
1845
1846 bool GetSourceLocation(SourceLocation* out_location) const;
1847
1848 private:
1849 friend class Symbol;
1850 typedef MethodOptions OptionsType;
1851
1852
1853 friend class io::Printer;
1854 friend class compiler::cpp::Formatter;
1855
1856
1857
1858
1859
1860 const FeatureSet& features() const { return *merged_features_; }
1861 friend class internal::InternalFeatureHelper;
1862
1863
1864 void DebugString(int depth, std::string* contents,
1865 const DebugStringOptions& options) const;
1866
1867
1868
1869 void GetLocationPath(std::vector<int>* output) const;
1870
1871 bool client_streaming_;
1872 bool server_streaming_;
1873 internal::DescriptorNames all_names_;
1874 const ServiceDescriptor* service_;
1875 mutable internal::LazyDescriptor input_type_;
1876 mutable internal::LazyDescriptor output_type_;
1877 const MethodOptions* options_;
1878 const FeatureSet* proto_features_;
1879 const FeatureSet* merged_features_;
1880
1881
1882
1883
1884
1885 MethodDescriptor();
1886 friend class DescriptorBuilder;
1887 friend class ServiceDescriptor;
1888 };
1889
1890 PROTOBUF_INTERNAL_CHECK_CLASS_SIZE(MethodDescriptor, 80);
1891
1892
1893
1894
1895 class PROTOBUF_EXPORT FileDescriptor : private internal::SymbolBase {
1896 public:
1897 typedef FileDescriptorProto Proto;
1898
1899 #ifndef SWIG
1900 FileDescriptor(const FileDescriptor&) = delete;
1901 FileDescriptor& operator=(const FileDescriptor&) = delete;
1902 #endif
1903
1904
1905
1906 absl::string_view name() const;
1907
1908
1909 absl::string_view package() const;
1910
1911
1912
1913 const DescriptorPool* pool() const;
1914
1915
1916 int dependency_count() const;
1917
1918
1919 const FileDescriptor* dependency(int index) const;
1920
1921
1922
1923 int public_dependency_count() const;
1924
1925
1926
1927 const FileDescriptor* public_dependency(int index) const;
1928
1929
1930
1931 int weak_dependency_count() const;
1932
1933
1934
1935 const FileDescriptor* weak_dependency(int index) const;
1936
1937
1938
1939 int option_dependency_count() const;
1940
1941
1942
1943
1944 absl::string_view option_dependency_name(int index) const;
1945
1946
1947
1948 int message_type_count() const;
1949
1950
1951 const Descriptor* message_type(int index) const;
1952
1953
1954
1955 int enum_type_count() const;
1956
1957
1958 const EnumDescriptor* enum_type(int index) const;
1959
1960
1961 int service_count() const;
1962
1963
1964 const ServiceDescriptor* service(int index) const;
1965
1966
1967
1968 int extension_count() const;
1969
1970
1971 const FieldDescriptor* extension(int index) const;
1972
1973
1974
1975
1976
1977 const FileOptions& options() const;
1978
1979
1980
1981 const Descriptor* FindMessageTypeByName(absl::string_view name) const;
1982
1983 const EnumDescriptor* FindEnumTypeByName(absl::string_view name) const;
1984
1985
1986 const EnumValueDescriptor* FindEnumValueByName(absl::string_view name) const;
1987
1988 const ServiceDescriptor* FindServiceByName(absl::string_view name) const;
1989
1990
1991 const FieldDescriptor* FindExtensionByName(absl::string_view name) const;
1992
1993
1994 const FieldDescriptor* FindExtensionByLowercaseName(
1995 absl::string_view name) const;
1996
1997
1998 const FieldDescriptor* FindExtensionByCamelcaseName(
1999 absl::string_view name) const;
2000
2001
2002
2003
2004
2005 void CopyTo(FileDescriptorProto* proto) const;
2006
2007
2008 void CopySourceCodeInfoTo(FileDescriptorProto* proto) const;
2009
2010
2011 void CopyJsonNameTo(FileDescriptorProto* proto) const;
2012
2013
2014 void CopyHeadingTo(FileDescriptorProto* proto) const;
2015
2016
2017 std::string DebugString() const;
2018
2019
2020 std::string DebugStringWithOptions(const DebugStringOptions& options) const;
2021
2022
2023 template <typename Sink>
2024 friend void AbslStringify(Sink& sink, const FileDescriptor& d) {
2025 absl::Format(&sink, "%s", d.DebugString());
2026 }
2027
2028
2029
2030
2031 bool is_placeholder() const;
2032
2033
2034
2035 bool GetSourceLocation(SourceLocation* out_location) const;
2036
2037
2038
2039
2040
2041
2042 bool GetSourceLocation(const std::vector<int>& path,
2043 SourceLocation* out_location) const;
2044
2045 private:
2046 friend class Symbol;
2047 friend class FileDescriptorLegacy;
2048 typedef FileOptions OptionsType;
2049
2050 bool is_placeholder_;
2051
2052
2053
2054 bool finished_building_;
2055
2056 int extension_count_;
2057
2058 const std::string* name_;
2059 const std::string* package_;
2060 const DescriptorPool* pool_;
2061 Edition edition_;
2062
2063
2064
2065 Edition edition() const;
2066
2067
2068
2069
2070
2071 const FeatureSet& features() const { return *merged_features_; }
2072 friend class internal::InternalFeatureHelper;
2073
2074
2075
2076
2077 absl::once_flag* dependencies_once_;
2078 static void DependenciesOnceInit(const FileDescriptor* to_init);
2079 void InternalDependenciesOnceInit() const;
2080
2081
2082 int dependency_count_;
2083 int public_dependency_count_;
2084 int weak_dependency_count_;
2085 int option_dependency_count_;
2086 int message_type_count_;
2087 int enum_type_count_;
2088 int service_count_;
2089
2090 mutable const FileDescriptor** dependencies_;
2091 int* public_dependencies_;
2092 int* weak_dependencies_;
2093 absl::string_view* option_dependencies_;
2094
2095 Descriptor* message_types_;
2096 EnumDescriptor* enum_types_;
2097 ServiceDescriptor* services_;
2098 FieldDescriptor* extensions_;
2099 const FileOptions* options_;
2100 const FeatureSet* proto_features_;
2101 const FeatureSet* merged_features_;
2102
2103 const FileDescriptorTables* tables_;
2104 const SourceCodeInfo* source_code_info_;
2105
2106
2107
2108
2109
2110 FileDescriptor();
2111 friend class DescriptorBuilder;
2112 friend class DescriptorPool;
2113 friend class Descriptor;
2114 friend class FieldDescriptor;
2115 friend class internal::LazyDescriptor;
2116 friend class OneofDescriptor;
2117 friend class EnumDescriptor;
2118 friend class EnumValueDescriptor;
2119 friend class MethodDescriptor;
2120 friend class ServiceDescriptor;
2121 };
2122
2123 PROTOBUF_INTERNAL_CHECK_CLASS_SIZE(FileDescriptor, 184);
2124
2125 #ifndef SWIG
2126 enum class ExtDeclEnforcementLevel : uint8_t {
2127
2128 kNoEnforcement = 0,
2129
2130
2131 kCustomExtensions = 1,
2132
2133 kAllExtensions = 2,
2134 };
2135 #endif
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163 class PROTOBUF_EXPORT DescriptorPool {
2164 public:
2165
2166 DescriptorPool();
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191 class ErrorCollector;
2192 explicit DescriptorPool(DescriptorDatabase* fallback_database,
2193 ErrorCollector* error_collector = nullptr);
2194
2195 #ifndef SWIG
2196 DescriptorPool(const DescriptorPool&) = delete;
2197 DescriptorPool& operator=(const DescriptorPool&) = delete;
2198 #endif
2199 ~DescriptorPool();
2200
2201
2202
2203
2204 static const DescriptorPool* generated_pool();
2205
2206
2207
2208
2209 const FileDescriptor* FindFileByName(absl::string_view name) const;
2210
2211
2212
2213
2214
2215 const FileDescriptor* FindFileContainingSymbol(
2216 absl::string_view symbol_name) const;
2217
2218
2219
2220
2221
2222
2223 const Descriptor* FindMessageTypeByName(absl::string_view name) const;
2224 const FieldDescriptor* FindFieldByName(absl::string_view name) const;
2225 const FieldDescriptor* FindExtensionByName(absl::string_view name) const;
2226 const OneofDescriptor* FindOneofByName(absl::string_view name) const;
2227 const EnumDescriptor* FindEnumTypeByName(absl::string_view name) const;
2228 const EnumValueDescriptor* FindEnumValueByName(absl::string_view name) const;
2229 const ServiceDescriptor* FindServiceByName(absl::string_view name) const;
2230 const MethodDescriptor* FindMethodByName(absl::string_view name) const;
2231
2232
2233
2234 const FieldDescriptor* FindExtensionByNumber(const Descriptor* extendee,
2235 int number) const;
2236
2237
2238
2239
2240
2241
2242 const FieldDescriptor* FindExtensionByPrintableName(
2243 const Descriptor* extendee, absl::string_view printable_name) const;
2244
2245
2246
2247
2248
2249
2250 void FindAllExtensions(const Descriptor* extendee,
2251 std::vector<const FieldDescriptor*>* out) const;
2252
2253
2254
2255
2256
2257
2258 class PROTOBUF_EXPORT ErrorCollector {
2259 public:
2260 inline ErrorCollector() {}
2261 #ifndef SWIG
2262 ErrorCollector(const ErrorCollector&) = delete;
2263 ErrorCollector& operator=(const ErrorCollector&) = delete;
2264 #endif
2265 virtual ~ErrorCollector();
2266
2267
2268
2269
2270 enum ErrorLocation {
2271 NAME,
2272 NUMBER,
2273 TYPE,
2274 EXTENDEE,
2275 DEFAULT_VALUE,
2276 INPUT_TYPE,
2277 OUTPUT_TYPE,
2278 OPTION_NAME,
2279 OPTION_VALUE,
2280 IMPORT,
2281 EDITIONS,
2282 OTHER
2283 };
2284 static absl::string_view ErrorLocationName(ErrorLocation location);
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294 virtual void RecordError(absl::string_view filename,
2295 absl::string_view element_name,
2296 const Message* descriptor, ErrorLocation location,
2297 absl::string_view message)
2298 = 0;
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308 virtual void RecordWarning([[maybe_unused]] absl::string_view filename,
2309 [[maybe_unused]] absl::string_view element_name,
2310 [[maybe_unused]] const Message* descriptor,
2311 [[maybe_unused]] ErrorLocation location,
2312 [[maybe_unused]] absl::string_view message) {
2313 }
2314
2315 };
2316
2317
2318
2319
2320
2321
2322 const FileDescriptor* BuildFile(const FileDescriptorProto& proto);
2323
2324
2325 const FileDescriptor* BuildFileCollectingErrors(
2326 const FileDescriptorProto& proto, ErrorCollector* error_collector);
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343 void AllowUnknownDependencies() { allow_unknown_ = true; }
2344
2345
2346
2347
2348
2349 void EnforceWeakDependencies(bool enforce) { enforce_weak_ = enforce; }
2350
2351
2352
2353
2354
2355
2356
2357
2358 void EnforceNamingStyle(bool enforce) { enforce_naming_style_ = enforce; }
2359
2360
2361
2362
2363 void EnforceSymbolVisibility(bool enforce) {
2364 enforce_symbol_visibility_ = enforce;
2365 }
2366
2367
2368
2369
2370 void EnforceOptionDependencies(bool enforce) { enforce_option_ = enforce; }
2371
2372
2373
2374
2375
2376
2377 absl::Status SetFeatureSetDefaults(FeatureSetDefaults spec);
2378
2379
2380
2381 template <typename TypeTraitsT, uint8_t field_type, bool is_packed>
2382 bool ResolvesFeaturesFor(
2383 const google::protobuf::internal::ExtensionIdentifier<
2384 FeatureSet, TypeTraitsT, field_type, is_packed>& extension) const {
2385 return ResolvesFeaturesForImpl(extension.number());
2386 }
2387
2388
2389
2390
2391
2392 void EnforceExtensionDeclarations(google::protobuf::ExtDeclEnforcementLevel enforce) {
2393 enforce_extension_declarations_ = enforce;
2394 }
2395
2396 bool ShouldEnforceDescriptorExtensionDeclarations() const {
2397 return enforce_extension_declarations_ ==
2398 ExtDeclEnforcementLevel::kAllExtensions;
2399 }
2400
2401 bool ShouldEnforceExtensionDeclaration(const FieldDescriptor& field) const;
2402
2403 #ifndef SWIG
2404
2405
2406
2407
2408 void SetRecursiveBuildDispatcher(
2409 absl::AnyInvocable<void(absl::FunctionRef<void()>) const> dispatcher) {
2410 if (dispatcher != nullptr) {
2411 dispatcher_ = std::make_unique<
2412 absl::AnyInvocable<void(absl::FunctionRef<void()>) const>>(
2413 std::move(dispatcher));
2414 } else {
2415 dispatcher_.reset(nullptr);
2416 }
2417 }
2418 #endif
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445 explicit DescriptorPool(const DescriptorPool* underlay);
2446
2447
2448
2449
2450 static void InternalAddGeneratedFile(const void* encoded_file_descriptor,
2451 int size);
2452
2453
2454 void DisallowEnforceUtf8() { disallow_enforce_utf8_ = true; }
2455
2456
2457 ABSL_DEPRECATED("Deprecated treatment of field name conflicts is enabled.")
2458 void UseDeprecatedLegacyJsonFieldConflicts() {
2459 deprecated_legacy_json_field_conflicts_ = true;
2460 }
2461
2462
2463
2464
2465
2466
2467 static DescriptorPool* internal_generated_pool();
2468
2469
2470
2471
2472 static DescriptorDatabase* internal_generated_database();
2473
2474
2475
2476
2477 void InternalDontEnforceDependencies();
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487 void InternalSetLazilyBuildDependencies() {
2488 lazily_build_dependencies_ = true;
2489
2490
2491 InternalDontEnforceDependencies();
2492 }
2493
2494
2495 void internal_set_underlay(const DescriptorPool* underlay) {
2496 underlay_ = underlay;
2497 }
2498
2499
2500
2501
2502 bool InternalIsFileLoaded(absl::string_view filename) const;
2503
2504
2505
2506
2507 void AddDirectInputFile(absl::string_view file_name,
2508 bool unused_import_is_error = false);
2509 void ClearDirectInputFiles();
2510
2511 #if !defined(PROTOBUF_FUTURE_RENAME_ADD_UNUSED_IMPORT) && !defined(SWIG)
2512 ABSL_DEPRECATED("Use AddDirectInputFile")
2513 void AddUnusedImportTrackFile(absl::string_view file_name,
2514 bool is_error = false) {
2515 AddDirectInputFile(file_name, is_error);
2516 }
2517 ABSL_DEPRECATED("Use AddDirectInputFile")
2518 void ClearUnusedImportTrackFiles() { ClearDirectInputFiles(); }
2519 #endif
2520
2521
2522 private:
2523 friend class Descriptor;
2524 friend class internal::LazyDescriptor;
2525 friend class FieldDescriptor;
2526 friend class EnumDescriptor;
2527 friend class ServiceDescriptor;
2528 friend class MethodDescriptor;
2529 friend class FileDescriptor;
2530 friend class DescriptorBuilder;
2531 friend class FileDescriptorTables;
2532 friend class google::protobuf::descriptor_unittest::DescriptorPoolMemoizationTest;
2533 friend class google::protobuf::descriptor_unittest::ValidationErrorTest;
2534 friend class ::google::protobuf::compiler::CommandLineInterface;
2535 friend class TextFormat;
2536 friend Reflection;
2537 friend class ::google::protobuf::compiler::java::MemoizeProjection;
2538
2539 struct MemoBase {
2540 virtual ~MemoBase() = default;
2541 };
2542 template <typename T>
2543 struct MemoData : MemoBase {
2544 T value;
2545 };
2546
2547 template <typename Desc>
2548 static const DescriptorPool* GetPool(const Desc* descriptor) {
2549 return descriptor->file()->pool();
2550 }
2551
2552 static const DescriptorPool* GetPool(const FileDescriptor* descriptor) {
2553 return descriptor->pool();
2554 }
2555
2556
2557
2558
2559 template <typename Desc, typename Func>
2560 static const auto& MemoizeProjection(const Desc* descriptor, Func func) {
2561 using ResultT = std::decay_t<decltype(func(descriptor))>;
2562 auto* pool = GetPool(descriptor);
2563 static_assert(std::is_empty_v<Func> ||
2564 std::is_function_v<std::remove_pointer_t<Func>>);
2565
2566 static bool type_key;
2567 auto key = std::pair<const void*, const void*>(descriptor, &type_key);
2568 {
2569 absl::ReaderMutexLock lock(&pool->field_memo_table_mutex_);
2570 auto it = pool->field_memo_table_->find(key);
2571 if (it != pool->field_memo_table_->end()) {
2572 return internal::DownCast<const MemoData<ResultT>&>(*it->second).value;
2573 }
2574 }
2575 auto result = std::make_unique<MemoData<ResultT>>();
2576 result->value = func(descriptor);
2577 {
2578 absl::MutexLock lock(&pool->field_memo_table_mutex_);
2579 auto insert_result =
2580 pool->field_memo_table_->insert({key, std::move(result)});
2581 auto it = insert_result.first;
2582 return internal::DownCast<const MemoData<ResultT>&>(*it->second).value;
2583 }
2584 }
2585
2586
2587
2588 bool IsSubSymbolOfBuiltType(absl::string_view name) const;
2589
2590
2591
2592
2593
2594
2595
2596
2597 class DeferredValidation;
2598 bool TryFindFileInFallbackDatabase(
2599 absl::string_view name, DeferredValidation& deferred_validation) const;
2600 bool TryFindSymbolInFallbackDatabase(
2601 absl::string_view name, DeferredValidation& deferred_validation) const;
2602 bool TryFindExtensionInFallbackDatabase(
2603 const Descriptor* containing_type, int field_number,
2604 DeferredValidation& deferred_validation) const;
2605
2606
2607
2608
2609 const FieldDescriptor* InternalFindExtensionByNumberNoLock(
2610 const Descriptor* extendee, int number) const;
2611
2612
2613
2614
2615 const FileDescriptor* BuildFileFromDatabase(
2616 const FileDescriptorProto& proto,
2617 DeferredValidation& deferred_validation) const;
2618
2619
2620
2621
2622
2623
2624 Symbol CrossLinkOnDemandHelper(absl::string_view name,
2625 bool expecting_enum) const;
2626
2627
2628 FileDescriptor* NewPlaceholderFile(absl::string_view name) const;
2629 FileDescriptor* NewPlaceholderFileWithMutexHeld(
2630 absl::string_view name, internal::FlatAllocator& alloc) const;
2631
2632 enum PlaceholderType {
2633 PLACEHOLDER_MESSAGE,
2634 PLACEHOLDER_ENUM,
2635 PLACEHOLDER_EXTENDABLE_MESSAGE
2636 };
2637
2638 Symbol NewPlaceholder(absl::string_view name,
2639 PlaceholderType placeholder_type) const;
2640 Symbol NewPlaceholderWithMutexHeld(absl::string_view name,
2641 PlaceholderType placeholder_type) const;
2642
2643 #ifndef SWIG
2644 mutable absl::Mutex field_memo_table_mutex_;
2645 mutable std::unique_ptr<absl::flat_hash_map<
2646 std::pair<const void*, const void*>, std::unique_ptr<MemoBase>>>
2647 field_memo_table_ ABSL_GUARDED_BY(field_memo_table_mutex_) =
2648 std::make_unique<
2649 absl::flat_hash_map<std::pair<const void*, const void*>,
2650 std::unique_ptr<MemoBase>>>();
2651 #endif
2652
2653
2654
2655 absl::Mutex* mutex_;
2656
2657
2658 DescriptorDatabase* fallback_database_;
2659 ErrorCollector* default_error_collector_;
2660 const DescriptorPool* underlay_;
2661
2662 #ifndef SWIG
2663
2664 std::unique_ptr<absl::AnyInvocable<void(absl::FunctionRef<void()>) const>>
2665 dispatcher_;
2666 #endif
2667
2668
2669
2670 class Tables;
2671 std::unique_ptr<Tables> tables_;
2672
2673 bool enforce_dependencies_;
2674 bool lazily_build_dependencies_;
2675 bool allow_unknown_;
2676 bool enforce_weak_;
2677 bool enforce_option_ = false;
2678 ExtDeclEnforcementLevel enforce_extension_declarations_;
2679 bool disallow_enforce_utf8_;
2680 bool deprecated_legacy_json_field_conflicts_;
2681 bool enforce_naming_style_;
2682 bool enforce_symbol_visibility_ = false;
2683 mutable bool build_started_ = false;
2684
2685
2686
2687 absl::flat_hash_map<std::string, bool> direct_input_files_;
2688
2689
2690
2691 std::unique_ptr<FeatureSetDefaults> feature_set_defaults_spec_;
2692
2693
2694 bool IsReadyForCheckingDescriptorExtDecl(
2695 absl::string_view message_name) const;
2696
2697
2698 bool ResolvesFeaturesForImpl(int extension_number) const;
2699
2700 const FeatureSetDefaults& GetFeatureSetDefaults() const;
2701 };
2702
2703
2704
2705
2706
2707 #define PROTOBUF_DEFINE_ACCESSOR(CLASS, FIELD, TYPE) \
2708 inline TYPE CLASS::FIELD() const { return FIELD##_; }
2709
2710
2711 #define PROTOBUF_DEFINE_STRING_ACCESSOR(CLASS, FIELD) \
2712 inline absl::string_view CLASS::FIELD() const { return *FIELD##_; }
2713
2714
2715 #define PROTOBUF_DEFINE_NAME_ACCESSOR(CLASS) \
2716 inline absl::string_view CLASS::name() const { return all_names_.name(); } \
2717 inline absl::string_view CLASS::full_name() const { \
2718 return all_names_.full_name(); \
2719 }
2720
2721
2722 #define PROTOBUF_DEFINE_ARRAY_ACCESSOR(CLASS, FIELD, TYPE) \
2723 inline TYPE CLASS::FIELD(int index) const { \
2724 ABSL_DCHECK_LE(0, index); \
2725 ABSL_DCHECK_LT(index, FIELD##_count()); \
2726 return FIELD##s_ + index; \
2727 }
2728
2729 #define PROTOBUF_DEFINE_OPTIONS_ACCESSOR(CLASS, TYPE) \
2730 inline const TYPE& CLASS::options() const { return *options_; }
2731
2732 PROTOBUF_DEFINE_NAME_ACCESSOR(Descriptor)
2733 PROTOBUF_DEFINE_ACCESSOR(Descriptor, file, const FileDescriptor*)
2734 PROTOBUF_DEFINE_ACCESSOR(Descriptor, containing_type, const Descriptor*)
2735
2736 PROTOBUF_DEFINE_ACCESSOR(Descriptor, field_count, int)
2737 PROTOBUF_DEFINE_ACCESSOR(Descriptor, oneof_decl_count, int)
2738 PROTOBUF_DEFINE_ACCESSOR(Descriptor, real_oneof_decl_count, int)
2739 PROTOBUF_DEFINE_ACCESSOR(Descriptor, nested_type_count, int)
2740 PROTOBUF_DEFINE_ACCESSOR(Descriptor, enum_type_count, int)
2741
2742 PROTOBUF_DEFINE_ARRAY_ACCESSOR(Descriptor, field, const FieldDescriptor*)
2743 PROTOBUF_DEFINE_ARRAY_ACCESSOR(Descriptor, oneof_decl, const OneofDescriptor*)
2744 PROTOBUF_DEFINE_ARRAY_ACCESSOR(Descriptor, nested_type, const Descriptor*)
2745 PROTOBUF_DEFINE_ARRAY_ACCESSOR(Descriptor, enum_type, const EnumDescriptor*)
2746 inline const OneofDescriptor* Descriptor::real_oneof_decl(int index) const {
2747 ABSL_DCHECK(index < real_oneof_decl_count());
2748 return oneof_decl(index);
2749 }
2750
2751 PROTOBUF_DEFINE_ACCESSOR(Descriptor, extension_range_count, int)
2752 PROTOBUF_DEFINE_ACCESSOR(Descriptor, extension_count, int)
2753 PROTOBUF_DEFINE_ARRAY_ACCESSOR(Descriptor, extension_range,
2754 const Descriptor::ExtensionRange*)
2755 PROTOBUF_DEFINE_ARRAY_ACCESSOR(Descriptor, extension, const FieldDescriptor*)
2756
2757 PROTOBUF_DEFINE_ACCESSOR(Descriptor, reserved_range_count, int)
2758 PROTOBUF_DEFINE_ARRAY_ACCESSOR(Descriptor, reserved_range,
2759 const Descriptor::ReservedRange*)
2760 PROTOBUF_DEFINE_ACCESSOR(Descriptor, reserved_name_count, int)
2761
2762 PROTOBUF_DEFINE_OPTIONS_ACCESSOR(Descriptor, MessageOptions)
2763 PROTOBUF_DEFINE_ACCESSOR(Descriptor, is_placeholder, bool)
2764
2765 PROTOBUF_DEFINE_NAME_ACCESSOR(FieldDescriptor)
2766 PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, file, const FileDescriptor*)
2767 PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, number, int)
2768 PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, is_extension, bool)
2769 PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, containing_type, const Descriptor*)
2770 PROTOBUF_DEFINE_OPTIONS_ACCESSOR(FieldDescriptor, FieldOptions)
2771 PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, has_default_value, bool)
2772 PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, has_json_name, bool)
2773 PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_int32_t, int32_t)
2774 PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_int64_t, int64_t)
2775 PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_uint32_t, uint32_t)
2776 PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_uint64_t, uint64_t)
2777 PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_float, float)
2778 PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_double, double)
2779 PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_bool, bool)
2780 PROTOBUF_DEFINE_STRING_ACCESSOR(FieldDescriptor, default_value_string)
2781
2782 PROTOBUF_DEFINE_NAME_ACCESSOR(OneofDescriptor)
2783 PROTOBUF_DEFINE_ACCESSOR(OneofDescriptor, containing_type, const Descriptor*)
2784 PROTOBUF_DEFINE_ACCESSOR(OneofDescriptor, field_count, int)
2785 PROTOBUF_DEFINE_ARRAY_ACCESSOR(OneofDescriptor, field, const FieldDescriptor*)
2786 PROTOBUF_DEFINE_OPTIONS_ACCESSOR(OneofDescriptor, OneofOptions)
2787
2788 PROTOBUF_DEFINE_NAME_ACCESSOR(EnumDescriptor)
2789 PROTOBUF_DEFINE_ACCESSOR(EnumDescriptor, file, const FileDescriptor*)
2790 PROTOBUF_DEFINE_ACCESSOR(EnumDescriptor, containing_type, const Descriptor*)
2791 PROTOBUF_DEFINE_ACCESSOR(EnumDescriptor, value_count, int)
2792 PROTOBUF_DEFINE_ARRAY_ACCESSOR(EnumDescriptor, value,
2793 const EnumValueDescriptor*)
2794 PROTOBUF_DEFINE_OPTIONS_ACCESSOR(EnumDescriptor, EnumOptions)
2795 PROTOBUF_DEFINE_ACCESSOR(EnumDescriptor, is_placeholder, bool)
2796 PROTOBUF_DEFINE_ACCESSOR(EnumDescriptor, reserved_range_count, int)
2797 PROTOBUF_DEFINE_ARRAY_ACCESSOR(EnumDescriptor, reserved_range,
2798 const EnumDescriptor::ReservedRange*)
2799 PROTOBUF_DEFINE_ACCESSOR(EnumDescriptor, reserved_name_count, int)
2800
2801 inline absl::string_view EnumValueDescriptor::name() const {
2802 return all_names_[0];
2803 }
2804 inline absl::string_view EnumValueDescriptor::full_name() const {
2805 return all_names_[1];
2806 }
2807 PROTOBUF_DEFINE_ACCESSOR(EnumValueDescriptor, number, int)
2808 PROTOBUF_DEFINE_ACCESSOR(EnumValueDescriptor, type, const EnumDescriptor*)
2809 PROTOBUF_DEFINE_OPTIONS_ACCESSOR(EnumValueDescriptor, EnumValueOptions)
2810
2811 PROTOBUF_DEFINE_NAME_ACCESSOR(ServiceDescriptor)
2812 PROTOBUF_DEFINE_ACCESSOR(ServiceDescriptor, file, const FileDescriptor*)
2813 PROTOBUF_DEFINE_ACCESSOR(ServiceDescriptor, method_count, int)
2814 PROTOBUF_DEFINE_ARRAY_ACCESSOR(ServiceDescriptor, method,
2815 const MethodDescriptor*)
2816 PROTOBUF_DEFINE_OPTIONS_ACCESSOR(ServiceDescriptor, ServiceOptions)
2817
2818 PROTOBUF_DEFINE_NAME_ACCESSOR(MethodDescriptor)
2819 PROTOBUF_DEFINE_ACCESSOR(MethodDescriptor, service, const ServiceDescriptor*)
2820 PROTOBUF_DEFINE_OPTIONS_ACCESSOR(MethodDescriptor, MethodOptions)
2821 PROTOBUF_DEFINE_ACCESSOR(MethodDescriptor, client_streaming, bool)
2822 PROTOBUF_DEFINE_ACCESSOR(MethodDescriptor, server_streaming, bool)
2823
2824 PROTOBUF_DEFINE_STRING_ACCESSOR(FileDescriptor, name)
2825 PROTOBUF_DEFINE_STRING_ACCESSOR(FileDescriptor, package)
2826 PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, pool, const DescriptorPool*)
2827 PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, dependency_count, int)
2828 PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, public_dependency_count, int)
2829 PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, weak_dependency_count, int)
2830 PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, option_dependency_count, int)
2831 PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, message_type_count, int)
2832 PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, enum_type_count, int)
2833 PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, service_count, int)
2834 PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, extension_count, int)
2835 PROTOBUF_DEFINE_OPTIONS_ACCESSOR(FileDescriptor, FileOptions)
2836 PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, is_placeholder, bool)
2837
2838 PROTOBUF_DEFINE_ARRAY_ACCESSOR(FileDescriptor, message_type, const Descriptor*)
2839 PROTOBUF_DEFINE_ARRAY_ACCESSOR(FileDescriptor, enum_type, const EnumDescriptor*)
2840 PROTOBUF_DEFINE_ARRAY_ACCESSOR(FileDescriptor, service,
2841 const ServiceDescriptor*)
2842 PROTOBUF_DEFINE_ARRAY_ACCESSOR(FileDescriptor, extension,
2843 const FieldDescriptor*)
2844
2845 #undef PROTOBUF_DEFINE_ACCESSOR
2846 #undef PROTOBUF_DEFINE_STRING_ACCESSOR
2847 #undef PROTOBUF_DEFINE_ARRAY_ACCESSOR
2848
2849
2850
2851 inline Descriptor::WellKnownType Descriptor::well_known_type() const {
2852 return static_cast<Descriptor::WellKnownType>(well_known_type_);
2853 }
2854
2855 inline bool Descriptor::IsExtensionNumber(int number) const {
2856 return FindExtensionRangeContainingNumber(number) != nullptr;
2857 }
2858
2859 inline bool Descriptor::IsReservedNumber(int number) const {
2860 return FindReservedRangeContainingNumber(number) != nullptr;
2861 }
2862
2863 inline bool Descriptor::IsReservedName(absl::string_view name) const {
2864 for (int i = 0; i < reserved_name_count(); i++) {
2865 if (name == static_cast<absl::string_view>(reserved_name(i))) {
2866 return true;
2867 }
2868 }
2869 return false;
2870 }
2871
2872
2873
2874 inline absl::string_view Descriptor::reserved_name(int index) const {
2875 return *reserved_names_[index];
2876 }
2877
2878 inline bool EnumDescriptor::IsReservedNumber(int number) const {
2879 return FindReservedRangeContainingNumber(number) != nullptr;
2880 }
2881
2882 inline bool EnumDescriptor::IsReservedName(absl::string_view name) const {
2883 for (int i = 0; i < reserved_name_count(); i++) {
2884 if (name == static_cast<absl::string_view>(reserved_name(i))) {
2885 return true;
2886 }
2887 }
2888 return false;
2889 }
2890
2891
2892
2893 inline absl::string_view EnumDescriptor::reserved_name(int index) const {
2894 return *reserved_names_[index];
2895 }
2896
2897 inline absl::string_view FieldDescriptor::lowercase_name() const {
2898 return all_names_.lowercase_name();
2899 }
2900
2901 inline absl::string_view FieldDescriptor::camelcase_name() const {
2902 return all_names_.camelcase_name();
2903 }
2904
2905 inline absl::string_view FieldDescriptor::json_name() const {
2906 return all_names_.json_name();
2907 }
2908
2909 inline const OneofDescriptor* FieldDescriptor::containing_oneof() const {
2910 if (is_oneof_) {
2911 auto* res = scope_.containing_oneof;
2912 PROTOBUF_ASSUME(res != nullptr);
2913 return res;
2914 }
2915 return nullptr;
2916 }
2917
2918 inline int FieldDescriptor::index_in_oneof() const {
2919 ABSL_DCHECK(is_oneof_);
2920 return static_cast<int>(this - scope_.containing_oneof->field(0));
2921 }
2922
2923 inline const Descriptor* FieldDescriptor::extension_scope() const {
2924 ABSL_CHECK(is_extension_);
2925 return scope_.extension_scope;
2926 }
2927
2928 inline FieldDescriptor::Label FieldDescriptor::label() const {
2929 if (is_required()) {
2930 return LABEL_REQUIRED;
2931 } else if (is_repeated()) {
2932 return LABEL_REPEATED;
2933 } else {
2934 return LABEL_OPTIONAL;
2935 }
2936 }
2937
2938 inline FieldDescriptor::Type FieldDescriptor::type() const {
2939 return static_cast<Type>(type_);
2940 }
2941
2942 inline FieldDescriptor::CppStringType FieldDescriptor::cpp_string_type() const {
2943 ABSL_DCHECK_EQ(cpp_string_type_,
2944 static_cast<uint8_t>(CalculateCppStringType()));
2945 return static_cast<FieldDescriptor::CppStringType>(cpp_string_type_);
2946 }
2947
2948 inline bool FieldDescriptor::is_optional() const {
2949 return !is_repeated() && !is_required();
2950 }
2951
2952 inline bool FieldDescriptor::is_repeated() const {
2953 ABSL_DCHECK_EQ(is_repeated_, static_cast<Label>(label_) == LABEL_REPEATED);
2954 return is_repeated_;
2955 }
2956
2957 inline bool FieldDescriptor::is_packable() const {
2958 return is_repeated() && IsTypePackable(type());
2959 }
2960
2961 inline bool FieldDescriptor::is_map() const {
2962 ABSL_DCHECK_EQ(is_map_, type() == TYPE_MESSAGE && is_map_message_type());
2963 return is_map_;
2964 }
2965
2966 inline const OneofDescriptor* FieldDescriptor::real_containing_oneof() const {
2967 if (in_real_oneof_) {
2968 auto* res = containing_oneof();
2969 PROTOBUF_ASSUME(res != nullptr);
2970 ABSL_DCHECK(!res->is_synthetic());
2971 return res;
2972 }
2973 return nullptr;
2974 }
2975
2976
2977
2978 inline int FieldDescriptor::index() const {
2979 if (!is_extension_) {
2980 return static_cast<int>(this - containing_type()->fields_);
2981 } else if (extension_scope() != nullptr) {
2982 return static_cast<int>(this - extension_scope()->extensions_);
2983 } else {
2984 return static_cast<int>(this - file_->extensions_);
2985 }
2986 }
2987
2988 inline int Descriptor::index() const {
2989 if (containing_type_ == nullptr) {
2990 return static_cast<int>(this - file_->message_types_);
2991 } else {
2992 return static_cast<int>(this - containing_type_->nested_types_);
2993 }
2994 }
2995
2996 inline int Descriptor::ExtensionRange::index() const {
2997 return static_cast<int>(this - containing_type_->extension_ranges_);
2998 }
2999
3000 inline const FileDescriptor* OneofDescriptor::file() const {
3001 return containing_type()->file();
3002 }
3003
3004 inline int OneofDescriptor::index() const {
3005 return static_cast<int>(this - containing_type_->oneof_decls_);
3006 }
3007
3008 inline bool OneofDescriptor::is_synthetic() const {
3009 return field_count() == 1 && field(0)->proto3_optional_;
3010 }
3011
3012 inline int EnumDescriptor::index() const {
3013 if (containing_type_ == nullptr) {
3014 return static_cast<int>(this - file_->enum_types_);
3015 } else {
3016 return static_cast<int>(this - containing_type_->enum_types_);
3017 }
3018 }
3019
3020 inline const FileDescriptor* EnumValueDescriptor::file() const {
3021 return type()->file();
3022 }
3023
3024 inline int EnumValueDescriptor::index() const {
3025 return static_cast<int>(this - type_->values_);
3026 }
3027
3028 inline int ServiceDescriptor::index() const {
3029 return static_cast<int>(this - file_->services_);
3030 }
3031
3032 inline const FileDescriptor* MethodDescriptor::file() const {
3033 return service()->file();
3034 }
3035
3036 inline int MethodDescriptor::index() const {
3037 return static_cast<int>(this - service_->methods_);
3038 }
3039
3040 inline absl::string_view FieldDescriptor::type_name() const {
3041 return kTypeToName[type()];
3042 }
3043
3044 inline FieldDescriptor::CppType FieldDescriptor::cpp_type() const {
3045 return kTypeToCppTypeMap[type()];
3046 }
3047
3048 inline absl::string_view FieldDescriptor::cpp_type_name() const {
3049 return kCppTypeToName[kTypeToCppTypeMap[type()]];
3050 }
3051
3052 inline FieldDescriptor::CppType FieldDescriptor::TypeToCppType(Type type) {
3053 return kTypeToCppTypeMap[type];
3054 }
3055
3056 inline absl::string_view FieldDescriptor::TypeName(Type type) {
3057 return kTypeToName[type];
3058 }
3059
3060 inline absl::string_view FieldDescriptor::CppTypeName(CppType cpp_type) {
3061 return kCppTypeToName[cpp_type];
3062 }
3063
3064 inline bool FieldDescriptor::IsTypePackable(Type field_type) {
3065 return (field_type != FieldDescriptor::TYPE_STRING &&
3066 field_type != FieldDescriptor::TYPE_GROUP &&
3067 field_type != FieldDescriptor::TYPE_MESSAGE &&
3068 field_type != FieldDescriptor::TYPE_BYTES);
3069 }
3070
3071 inline const FileDescriptor* FileDescriptor::public_dependency(
3072 int index) const {
3073 return dependency(public_dependencies_[index]);
3074 }
3075
3076 inline const FileDescriptor* FileDescriptor::weak_dependency(int index) const {
3077 return dependency(weak_dependencies_[index]);
3078 }
3079
3080
3081 inline SymbolVisibility Descriptor::visibility_keyword() const {
3082 return static_cast<SymbolVisibility>(visibility_);
3083 }
3084
3085 inline SymbolVisibility EnumDescriptor::visibility_keyword() const {
3086 return static_cast<SymbolVisibility>(visibility_);
3087 }
3088
3089 namespace internal {
3090
3091 inline const std::string& DefaultValueStringAsString(
3092 const FieldDescriptor* field) {
3093 return *field->default_value_string_;
3094 }
3095
3096 inline const std::string& NameOfEnumAsString(
3097 const EnumValueDescriptor* descriptor) {
3098 return descriptor->all_names_[0];
3099 }
3100
3101 inline bool IsEnumFullySequential(const EnumDescriptor* enum_desc) {
3102 return enum_desc->sequential_value_limit_ == enum_desc->value_count() - 1;
3103 }
3104
3105
3106
3107
3108 template <typename T>
3109 struct FieldRangeImpl;
3110
3111 template <typename T>
3112 FieldRangeImpl<T> FieldRange(const T* desc) {
3113 return {desc};
3114 }
3115
3116 template <typename T>
3117 struct FieldRangeImpl {
3118 struct Iterator {
3119 using iterator_category = std::forward_iterator_tag;
3120 using value_type = const FieldDescriptor*;
3121 using difference_type = int;
3122
3123 value_type operator*() { return descriptor->field(idx); }
3124
3125 friend bool operator==(const Iterator& a, const Iterator& b) {
3126 ABSL_DCHECK(a.descriptor == b.descriptor);
3127 return a.idx == b.idx;
3128 }
3129 friend bool operator!=(const Iterator& a, const Iterator& b) {
3130 return !(a == b);
3131 }
3132
3133 Iterator& operator++() {
3134 idx++;
3135 return *this;
3136 }
3137
3138 int idx;
3139 const T* descriptor;
3140 };
3141
3142 Iterator begin() const { return {0, descriptor}; }
3143 Iterator end() const { return {descriptor->field_count(), descriptor}; }
3144
3145 const T* descriptor;
3146 };
3147
3148
3149
3150
3151
3152
3153 bool ParseNoReflection(absl::string_view from, google::protobuf::MessageLite& to);
3154
3155
3156
3157
3158 namespace cpp {
3159
3160
3161
3162 constexpr int MaxMessageDeclarationNestingDepth() { return 32; }
3163
3164
3165
3166 PROTOBUF_EXPORT bool HasPreservingUnknownEnumSemantics(
3167 const FieldDescriptor* field);
3168
3169 #ifndef SWIG
3170 enum class HasbitMode : uint8_t {
3171
3172 kNoHasbit,
3173
3174
3175 kTrueHasbit,
3176
3177
3178
3179
3180 kHintHasbit,
3181 };
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193 PROTOBUF_EXPORT HasbitMode
3194 GetFieldHasbitModeWithoutProfile(const FieldDescriptor* field);
3195
3196
3197
3198
3199
3200
3201
3202 PROTOBUF_EXPORT bool HasHasbitWithoutProfile(const FieldDescriptor* field);
3203
3204 enum class Utf8CheckMode : uint8_t {
3205 kStrict = 0,
3206 kVerify = 1,
3207 kNone = 2,
3208 };
3209 PROTOBUF_EXPORT Utf8CheckMode GetUtf8CheckMode(const FieldDescriptor* field,
3210 bool is_lite);
3211
3212
3213
3214
3215
3216
3217 PROTOBUF_EXPORT bool IsGroupLike(const FieldDescriptor& field);
3218
3219
3220
3221
3222 PROTOBUF_EXPORT bool IsLazilyInitializedFile(absl::string_view filename);
3223
3224
3225
3226
3227 PROTOBUF_EXPORT inline bool& IsTrackingEnabledVar() {
3228 static PROTOBUF_THREAD_LOCAL bool is_tracking_enabled = true;
3229 return is_tracking_enabled;
3230 }
3231 PROTOBUF_EXPORT inline bool IsTrackingEnabled() {
3232 return ABSL_PREDICT_TRUE(IsTrackingEnabledVar());
3233 }
3234
3235 template <typename F>
3236 auto VisitDescriptorsInFileOrder(const Descriptor* desc,
3237 F& f) -> decltype(f(desc)) {
3238 for (int i = 0; i < desc->nested_type_count(); i++) {
3239 if (auto res = VisitDescriptorsInFileOrder(desc->nested_type(i), f)) {
3240 return res;
3241 }
3242 }
3243 if (auto res = f(desc)) return res;
3244 return {};
3245 }
3246
3247
3248
3249
3250
3251
3252 template <typename F>
3253 auto VisitDescriptorsInFileOrder(const FileDescriptor* file,
3254 F f) -> decltype(f(file->message_type(0))) {
3255 for (int i = 0; i < file->message_type_count(); i++) {
3256 if (auto res = VisitDescriptorsInFileOrder(file->message_type(i), f)) {
3257 return res;
3258 }
3259 }
3260 return {};
3261 }
3262 #endif
3263
3264
3265
3266
3267
3268
3269 PROTOBUF_EXPORT bool IsStringFieldWithPrivatizedAccessors(
3270 const FieldDescriptor& field);
3271
3272 }
3273 }
3274
3275 }
3276 }
3277
3278 #undef PROTOBUF_INTERNAL_CHECK_CLASS_SIZE
3279 #include "google/protobuf/port_undef.inc"
3280
3281 #endif