File indexing completed on 2025-01-31 10:12:23
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
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087 #ifndef GOOGLE_PROTOBUF_MESSAGE_H__
0088 #define GOOGLE_PROTOBUF_MESSAGE_H__
0089
0090 #include <cstddef>
0091 #include <cstdint>
0092 #include <memory>
0093 #include <string>
0094 #include <type_traits>
0095 #include <vector>
0096
0097 #include "absl/base/attributes.h"
0098 #include "absl/base/call_once.h"
0099 #include "absl/base/macros.h"
0100 #include "absl/log/absl_check.h"
0101 #include "absl/memory/memory.h"
0102 #include "absl/strings/cord.h"
0103 #include "absl/strings/string_view.h"
0104 #include "absl/types/optional.h"
0105 #include "google/protobuf/arena.h"
0106 #include "google/protobuf/descriptor.h"
0107 #include "google/protobuf/generated_message_reflection.h"
0108 #include "google/protobuf/generated_message_tctable_decl.h"
0109 #include "google/protobuf/generated_message_util.h"
0110 #include "google/protobuf/map.h" // TODO: cleanup
0111 #include "google/protobuf/message_lite.h"
0112 #include "google/protobuf/port.h"
0113 #include "google/protobuf/reflection.h"
0114
0115
0116 #include "google/protobuf/port_def.inc"
0117
0118 #ifdef SWIG
0119 #error "You cannot SWIG proto headers"
0120 #endif
0121
0122 namespace google {
0123 namespace protobuf {
0124
0125
0126 class Message;
0127 class Reflection;
0128 class MessageFactory;
0129
0130
0131 class AssignDescriptorsHelper;
0132 class DynamicMessageFactory;
0133 class GeneratedMessageReflectionTestHelper;
0134 class MapKey;
0135 class MapValueConstRef;
0136 class MapValueRef;
0137 class MapIterator;
0138 class MapReflectionTester;
0139 class TextFormat;
0140
0141 namespace internal {
0142 struct FuzzPeer;
0143 struct DescriptorTable;
0144 template <bool is_oneof>
0145 struct DynamicFieldInfoHelper;
0146 class MapFieldBase;
0147 class MessageUtil;
0148 class ReflectionVisit;
0149 class SwapFieldHelper;
0150 class CachedSize;
0151 struct TailCallTableInfo;
0152
0153 namespace field_layout {
0154 enum TransformValidation : uint16_t;
0155 }
0156
0157 }
0158 class UnknownFieldSet;
0159 namespace io {
0160 class EpsCopyOutputStream;
0161 class ZeroCopyInputStream;
0162 class ZeroCopyOutputStream;
0163 class CodedInputStream;
0164 class CodedOutputStream;
0165 }
0166 namespace python {
0167 class MapReflectionFriend;
0168 class MessageReflectionFriend;
0169 }
0170 namespace expr {
0171 class CelMapReflectionFriend;
0172 class SudoMapReflectionFriend;
0173 }
0174
0175 namespace internal {
0176 class MapFieldPrinterHelper;
0177 PROTOBUF_EXPORT std::string StringifyMessage(
0178 const Message& message);
0179 }
0180 PROTOBUF_EXPORT std::string ShortFormat(
0181 const Message& message);
0182 PROTOBUF_EXPORT std::string Utf8Format(
0183 const Message& message);
0184 namespace util {
0185 class MessageDifferencer;
0186 }
0187
0188
0189 namespace internal {
0190 class ReflectionAccessor;
0191 class ReflectionOps;
0192 class MapKeySorter;
0193 class WireFormat;
0194 class MapFieldReflectionTest;
0195 }
0196
0197 template <typename T>
0198 class RepeatedField;
0199
0200 template <typename T>
0201 class RepeatedPtrField;
0202
0203
0204 struct Metadata {
0205 const Descriptor* descriptor;
0206 const Reflection* reflection;
0207 };
0208
0209 namespace internal {
0210 template <class To>
0211 inline To* GetPointerAtOffset(void* message, uint32_t offset) {
0212 return reinterpret_cast<To*>(reinterpret_cast<char*>(message) + offset);
0213 }
0214
0215 template <class To>
0216 const To* GetConstPointerAtOffset(const void* message, uint32_t offset) {
0217 return reinterpret_cast<const To*>(reinterpret_cast<const char*>(message) +
0218 offset);
0219 }
0220
0221 template <class To>
0222 const To& GetConstRefAtOffset(const Message& message, uint32_t offset) {
0223 return *GetConstPointerAtOffset<To>(&message, offset);
0224 }
0225
0226 bool CreateUnknownEnumValues(const FieldDescriptor* field);
0227
0228
0229 PROTOBUF_EXPORT bool IsDescendant(Message& root, const Message& message);
0230
0231 inline void MaybePoisonAfterClear(Message* root);
0232 }
0233
0234
0235
0236
0237
0238
0239
0240
0241
0242
0243
0244
0245
0246
0247 class PROTOBUF_EXPORT Message : public MessageLite {
0248 public:
0249 Message(const Message&) = delete;
0250 Message& operator=(const Message&) = delete;
0251
0252
0253
0254
0255
0256
0257 Message* New() const { return New(nullptr); }
0258
0259
0260
0261 #if defined(PROTOBUF_CUSTOM_VTABLE)
0262 Message* New(Arena* arena) const {
0263 return static_cast<Message*>(MessageLite::New(arena));
0264 }
0265 #else
0266 Message* New(Arena* arena) const override = 0;
0267 #endif
0268
0269
0270
0271
0272 void CopyFrom(const Message& from);
0273
0274
0275
0276
0277
0278
0279 void MergeFrom(const Message& from);
0280
0281
0282
0283 void CheckInitialized() const;
0284
0285
0286
0287
0288
0289 void FindInitializationErrors(std::vector<std::string>* errors) const;
0290
0291
0292
0293 std::string InitializationErrorString() const;
0294
0295
0296
0297
0298
0299
0300
0301
0302
0303
0304
0305 void DiscardUnknownFields();
0306
0307
0308
0309
0310
0311
0312
0313
0314
0315
0316
0317
0318 size_t SpaceUsedLong() const;
0319
0320 [[deprecated("Please use SpaceUsedLong() instead")]] int SpaceUsed() const {
0321 return internal::ToIntSize(SpaceUsedLong());
0322 }
0323
0324
0325
0326
0327
0328
0329
0330
0331 std::string DebugString() const;
0332
0333 std::string ShortDebugString() const;
0334
0335 std::string Utf8DebugString() const;
0336
0337 void PrintDebugString() const;
0338
0339
0340
0341
0342 template <typename Sink>
0343 friend void AbslStringify(Sink& sink, const google::protobuf::Message& message) {
0344 sink.Append(internal::StringifyMessage(message));
0345 }
0346
0347
0348
0349
0350 #if !defined(PROTOBUF_CUSTOM_VTABLE)
0351 void Clear() override;
0352
0353 size_t ByteSizeLong() const override;
0354 uint8_t* _InternalSerialize(uint8_t* target,
0355 io::EpsCopyOutputStream* stream) const override;
0356 #endif
0357
0358
0359
0360
0361
0362
0363
0364 const Descriptor* GetDescriptor() const { return GetMetadata().descriptor; }
0365
0366
0367
0368
0369
0370 const Reflection* GetReflection() const { return GetMetadata().reflection; }
0371
0372 protected:
0373 #if !defined(PROTOBUF_CUSTOM_VTABLE)
0374 constexpr Message() {}
0375 #endif
0376 using MessageLite::MessageLite;
0377
0378
0379
0380 Metadata GetMetadata() const;
0381 static Metadata GetMetadataImpl(const ClassDataFull& data);
0382
0383
0384 static bool IsInitializedImpl(const MessageLite&);
0385
0386 size_t ComputeUnknownFieldsSize(size_t total_size,
0387 internal::CachedSize* cached_size) const;
0388 size_t MaybeComputeUnknownFieldsSize(size_t total_size,
0389 internal::CachedSize* cached_size) const;
0390
0391
0392 static absl::string_view GetTypeNameImpl(const ClassData* data);
0393 static void MergeImpl(MessageLite& to, const MessageLite& from);
0394 static void ClearImpl(MessageLite& msg);
0395 static size_t ByteSizeLongImpl(const MessageLite& msg);
0396 static uint8_t* _InternalSerializeImpl(const MessageLite& msg,
0397 uint8_t* target,
0398 io::EpsCopyOutputStream* stream);
0399
0400 static const internal::TcParseTableBase* GetTcParseTableImpl(
0401 const MessageLite& msg);
0402
0403 static size_t SpaceUsedLongImpl(const MessageLite& msg_lite);
0404
0405 static const DescriptorMethods kDescriptorMethods;
0406
0407 };
0408
0409 namespace internal {
0410
0411 void* CreateSplitMessageGeneric(Arena* arena, const void* default_split,
0412 size_t size, const void* message,
0413 const void* default_message);
0414
0415
0416
0417 class RepeatedFieldAccessor;
0418 }
0419
0420
0421
0422
0423
0424
0425
0426
0427
0428
0429
0430
0431
0432
0433
0434
0435
0436
0437
0438
0439
0440
0441
0442
0443
0444
0445
0446
0447
0448
0449
0450
0451
0452
0453
0454
0455
0456
0457
0458 class PROTOBUF_EXPORT Reflection final {
0459 public:
0460 Reflection(const Reflection&) = delete;
0461 Reflection& operator=(const Reflection&) = delete;
0462 ~Reflection();
0463
0464
0465
0466
0467 const UnknownFieldSet& GetUnknownFields(const Message& message) const;
0468
0469
0470
0471 UnknownFieldSet* MutableUnknownFields(Message* message) const;
0472
0473
0474 size_t SpaceUsedLong(const Message& message) const;
0475
0476 [[deprecated("Please use SpaceUsedLong() instead")]] int SpaceUsed(
0477 const Message& message) const {
0478 return internal::ToIntSize(SpaceUsedLong(message));
0479 }
0480
0481
0482 bool IsDefaultInstance(const Message& message) const {
0483 return schema_.IsDefaultInstance(message);
0484 }
0485
0486
0487 bool HasField(const Message& message, const FieldDescriptor* field) const;
0488
0489
0490 int FieldSize(const Message& message, const FieldDescriptor* field) const;
0491
0492
0493
0494 void ClearField(Message* message, const FieldDescriptor* field) const;
0495
0496
0497
0498 bool HasOneof(const Message& message,
0499 const OneofDescriptor* oneof_descriptor) const;
0500
0501 void ClearOneof(Message* message,
0502 const OneofDescriptor* oneof_descriptor) const;
0503
0504
0505 const FieldDescriptor* GetOneofFieldDescriptor(
0506 const Message& message, const OneofDescriptor* oneof_descriptor) const;
0507
0508
0509
0510
0511
0512
0513
0514
0515 void RemoveLast(Message* message, const FieldDescriptor* field) const;
0516
0517
0518 PROTOBUF_NODISCARD Message* ReleaseLast(Message* message,
0519 const FieldDescriptor* field) const;
0520
0521
0522
0523
0524 Message* UnsafeArenaReleaseLast(Message* message,
0525 const FieldDescriptor* field) const;
0526
0527
0528 void Swap(Message* message1, Message* message2) const;
0529
0530
0531 void SwapFields(Message* message1, Message* message2,
0532 const std::vector<const FieldDescriptor*>& fields) const;
0533
0534
0535 void SwapElements(Message* message, const FieldDescriptor* field, int index1,
0536 int index2) const;
0537
0538
0539
0540 void UnsafeArenaSwap(Message* lhs, Message* rhs) const;
0541
0542
0543
0544 void UnsafeArenaSwapFields(
0545 Message* lhs, Message* rhs,
0546 const std::vector<const FieldDescriptor*>& fields) const;
0547
0548
0549
0550
0551
0552
0553
0554
0555
0556 void ListFields(const Message& message,
0557 std::vector<const FieldDescriptor*>* output) const;
0558
0559
0560
0561
0562
0563 int32_t GetInt32(const Message& message, const FieldDescriptor* field) const;
0564 int64_t GetInt64(const Message& message, const FieldDescriptor* field) const;
0565 uint32_t GetUInt32(const Message& message,
0566 const FieldDescriptor* field) const;
0567 uint64_t GetUInt64(const Message& message,
0568 const FieldDescriptor* field) const;
0569 float GetFloat(const Message& message, const FieldDescriptor* field) const;
0570 double GetDouble(const Message& message, const FieldDescriptor* field) const;
0571 bool GetBool(const Message& message, const FieldDescriptor* field) const;
0572 std::string GetString(const Message& message,
0573 const FieldDescriptor* field) const;
0574 const EnumValueDescriptor* GetEnum(const Message& message,
0575 const FieldDescriptor* field) const;
0576
0577
0578
0579
0580
0581
0582 int GetEnumValue(const Message& message, const FieldDescriptor* field) const;
0583
0584
0585 const Message& GetMessage(const Message& message,
0586 const FieldDescriptor* field,
0587 MessageFactory* factory = nullptr) const;
0588
0589
0590
0591
0592
0593
0594
0595
0596
0597
0598
0599
0600
0601
0602
0603
0604 const std::string& GetStringReference(const Message& message,
0605 const FieldDescriptor* field,
0606 std::string* scratch) const;
0607
0608
0609
0610
0611
0612 absl::Cord GetCord(const Message& message,
0613 const FieldDescriptor* field) const;
0614
0615
0616
0617
0618 class ScratchSpace {
0619 public:
0620 ScratchSpace() = default;
0621
0622 ScratchSpace(const ScratchSpace&) = delete;
0623 ScratchSpace& operator=(const ScratchSpace&) = delete;
0624
0625 private:
0626 friend class Reflection;
0627
0628 absl::string_view CopyFromCord(const absl::Cord& cord) {
0629 if (absl::optional<absl::string_view> flat = cord.TryFlat()) {
0630 return *flat;
0631 }
0632 if (!buffer_) {
0633 buffer_ = absl::make_unique<std::string>();
0634 }
0635 absl::CopyCordToString(cord, buffer_.get());
0636 return *buffer_;
0637 }
0638
0639 std::unique_ptr<std::string> buffer_;
0640 };
0641
0642
0643
0644
0645 absl::string_view GetStringView(
0646 const Message& message, const FieldDescriptor* field,
0647 ScratchSpace& scratch ABSL_ATTRIBUTE_LIFETIME_BOUND) const;
0648
0649
0650
0651
0652
0653 void SetInt32(Message* message, const FieldDescriptor* field,
0654 int32_t value) const;
0655 void SetInt64(Message* message, const FieldDescriptor* field,
0656 int64_t value) const;
0657 void SetUInt32(Message* message, const FieldDescriptor* field,
0658 uint32_t value) const;
0659 void SetUInt64(Message* message, const FieldDescriptor* field,
0660 uint64_t value) const;
0661 void SetFloat(Message* message, const FieldDescriptor* field,
0662 float value) const;
0663 void SetDouble(Message* message, const FieldDescriptor* field,
0664 double value) const;
0665 void SetBool(Message* message, const FieldDescriptor* field,
0666 bool value) const;
0667 void SetString(Message* message, const FieldDescriptor* field,
0668 std::string value) const;
0669
0670
0671
0672 void SetString(Message* message, const FieldDescriptor* field,
0673 const absl::Cord& value) const;
0674 void SetEnum(Message* message, const FieldDescriptor* field,
0675 const EnumValueDescriptor* value) const;
0676
0677
0678
0679
0680
0681
0682
0683 void SetEnumValue(Message* message, const FieldDescriptor* field,
0684 int value) const;
0685
0686
0687
0688
0689
0690
0691
0692
0693
0694
0695
0696 Message* MutableMessage(Message* message, const FieldDescriptor* field,
0697 MessageFactory* factory = nullptr) const;
0698
0699
0700
0701
0702
0703 void SetAllocatedMessage(Message* message, Message* sub_message,
0704 const FieldDescriptor* field) const;
0705
0706
0707
0708
0709 void UnsafeArenaSetAllocatedMessage(Message* message, Message* sub_message,
0710 const FieldDescriptor* field) const;
0711
0712
0713
0714
0715
0716
0717
0718
0719 PROTOBUF_NODISCARD Message* ReleaseMessage(
0720 Message* message, const FieldDescriptor* field,
0721 MessageFactory* factory = nullptr) const;
0722
0723
0724
0725
0726 Message* UnsafeArenaReleaseMessage(Message* message,
0727 const FieldDescriptor* field,
0728 MessageFactory* factory = nullptr) const;
0729
0730
0731
0732
0733
0734 int32_t GetRepeatedInt32(const Message& message, const FieldDescriptor* field,
0735 int index) const;
0736 int64_t GetRepeatedInt64(const Message& message, const FieldDescriptor* field,
0737 int index) const;
0738 uint32_t GetRepeatedUInt32(const Message& message,
0739 const FieldDescriptor* field, int index) const;
0740 uint64_t GetRepeatedUInt64(const Message& message,
0741 const FieldDescriptor* field, int index) const;
0742 float GetRepeatedFloat(const Message& message, const FieldDescriptor* field,
0743 int index) const;
0744 double GetRepeatedDouble(const Message& message, const FieldDescriptor* field,
0745 int index) const;
0746 bool GetRepeatedBool(const Message& message, const FieldDescriptor* field,
0747 int index) const;
0748 std::string GetRepeatedString(const Message& message,
0749 const FieldDescriptor* field, int index) const;
0750 const EnumValueDescriptor* GetRepeatedEnum(const Message& message,
0751 const FieldDescriptor* field,
0752 int index) const;
0753
0754
0755
0756
0757
0758 int GetRepeatedEnumValue(const Message& message, const FieldDescriptor* field,
0759 int index) const;
0760 const Message& GetRepeatedMessage(const Message& message,
0761 const FieldDescriptor* field,
0762 int index) const;
0763
0764
0765 const std::string& GetRepeatedStringReference(const Message& message,
0766 const FieldDescriptor* field,
0767 int index,
0768 std::string* scratch) const;
0769
0770
0771 absl::string_view GetRepeatedStringView(
0772 const Message& message, const FieldDescriptor* field, int index,
0773 ScratchSpace& scratch ABSL_ATTRIBUTE_LIFETIME_BOUND) const;
0774
0775
0776
0777
0778
0779 void SetRepeatedInt32(Message* message, const FieldDescriptor* field,
0780 int index, int32_t value) const;
0781 void SetRepeatedInt64(Message* message, const FieldDescriptor* field,
0782 int index, int64_t value) const;
0783 void SetRepeatedUInt32(Message* message, const FieldDescriptor* field,
0784 int index, uint32_t value) const;
0785 void SetRepeatedUInt64(Message* message, const FieldDescriptor* field,
0786 int index, uint64_t value) const;
0787 void SetRepeatedFloat(Message* message, const FieldDescriptor* field,
0788 int index, float value) const;
0789 void SetRepeatedDouble(Message* message, const FieldDescriptor* field,
0790 int index, double value) const;
0791 void SetRepeatedBool(Message* message, const FieldDescriptor* field,
0792 int index, bool value) const;
0793 void SetRepeatedString(Message* message, const FieldDescriptor* field,
0794 int index, std::string value) const;
0795 void SetRepeatedEnum(Message* message, const FieldDescriptor* field,
0796 int index, const EnumValueDescriptor* value) const;
0797
0798
0799
0800
0801
0802
0803
0804 void SetRepeatedEnumValue(Message* message, const FieldDescriptor* field,
0805 int index, int value) const;
0806
0807
0808 Message* MutableRepeatedMessage(Message* message,
0809 const FieldDescriptor* field,
0810 int index) const;
0811
0812
0813
0814
0815
0816 void AddInt32(Message* message, const FieldDescriptor* field,
0817 int32_t value) const;
0818 void AddInt64(Message* message, const FieldDescriptor* field,
0819 int64_t value) const;
0820 void AddUInt32(Message* message, const FieldDescriptor* field,
0821 uint32_t value) const;
0822 void AddUInt64(Message* message, const FieldDescriptor* field,
0823 uint64_t value) const;
0824 void AddFloat(Message* message, const FieldDescriptor* field,
0825 float value) const;
0826 void AddDouble(Message* message, const FieldDescriptor* field,
0827 double value) const;
0828 void AddBool(Message* message, const FieldDescriptor* field,
0829 bool value) const;
0830 void AddString(Message* message, const FieldDescriptor* field,
0831 std::string value) const;
0832 void AddEnum(Message* message, const FieldDescriptor* field,
0833 const EnumValueDescriptor* value) const;
0834
0835
0836
0837
0838
0839
0840
0841
0842 void AddEnumValue(Message* message, const FieldDescriptor* field,
0843 int value) const;
0844
0845 Message* AddMessage(Message* message, const FieldDescriptor* field,
0846 MessageFactory* factory = nullptr) const;
0847
0848
0849
0850 void AddAllocatedMessage(Message* message, const FieldDescriptor* field,
0851 Message* new_entry) const;
0852
0853
0854
0855
0856 void UnsafeArenaAddAllocatedMessage(Message* message,
0857 const FieldDescriptor* field,
0858 Message* new_entry) const;
0859
0860
0861
0862
0863
0864
0865
0866
0867
0868
0869
0870
0871
0872
0873
0874
0875
0876
0877
0878
0879
0880
0881
0882
0883
0884 template <typename T>
0885 RepeatedFieldRef<T> GetRepeatedFieldRef(const Message& message,
0886 const FieldDescriptor* field) const;
0887
0888
0889
0890 template <typename T>
0891 MutableRepeatedFieldRef<T> GetMutableRepeatedFieldRef(
0892 Message* message, const FieldDescriptor* field) const;
0893
0894
0895
0896
0897
0898
0899
0900
0901
0902
0903
0904
0905
0906
0907
0908
0909
0910
0911 template <typename T>
0912 [[deprecated(
0913 "Please use GetRepeatedFieldRef() instead")]] const RepeatedField<T>&
0914 GetRepeatedField(const Message& msg, const FieldDescriptor* d) const {
0915 return GetRepeatedFieldInternal<T>(msg, d);
0916 }
0917
0918
0919
0920
0921 template <typename T>
0922 [[deprecated(
0923 "Please use GetMutableRepeatedFieldRef() instead")]] RepeatedField<T>*
0924 MutableRepeatedField(Message* msg, const FieldDescriptor* d) const {
0925 return MutableRepeatedFieldInternal<T>(msg, d);
0926 }
0927
0928
0929
0930
0931
0932 template <typename T>
0933 [[deprecated(
0934 "Please use GetRepeatedFieldRef() instead")]] const RepeatedPtrField<T>&
0935 GetRepeatedPtrField(const Message& msg, const FieldDescriptor* d) const {
0936 return GetRepeatedPtrFieldInternal<T>(msg, d);
0937 }
0938
0939
0940
0941
0942
0943 template <typename T>
0944 [[deprecated(
0945 "Please use GetMutableRepeatedFieldRef() instead")]] RepeatedPtrField<T>*
0946 MutableRepeatedPtrField(Message* msg, const FieldDescriptor* d) const {
0947 return MutableRepeatedPtrFieldInternal<T>(msg, d);
0948 }
0949
0950
0951
0952
0953
0954 const FieldDescriptor* FindKnownExtensionByName(absl::string_view name) const;
0955
0956
0957
0958 const FieldDescriptor* FindKnownExtensionByNumber(int number) const;
0959
0960
0961
0962
0963
0964
0965
0966
0967
0968
0969 MessageFactory* GetMessageFactory() const;
0970
0971 private:
0972 template <typename T>
0973 const RepeatedField<T>& GetRepeatedFieldInternal(
0974 const Message& message, const FieldDescriptor* field) const;
0975 template <typename T>
0976 RepeatedField<T>* MutableRepeatedFieldInternal(
0977 Message* message, const FieldDescriptor* field) const;
0978 template <typename T>
0979 const RepeatedPtrField<T>& GetRepeatedPtrFieldInternal(
0980 const Message& message, const FieldDescriptor* field) const;
0981 template <typename T>
0982 RepeatedPtrField<T>* MutableRepeatedPtrFieldInternal(
0983 Message* message, const FieldDescriptor* field) const;
0984
0985
0986
0987
0988
0989
0990 void* MutableRawRepeatedField(Message* message, const FieldDescriptor* field,
0991 FieldDescriptor::CppType cpptype, int ctype,
0992 const Descriptor* desc) const;
0993
0994 const void* GetRawRepeatedField(const Message& message,
0995 const FieldDescriptor* field,
0996 FieldDescriptor::CppType cpptype, int ctype,
0997 const Descriptor* desc) const;
0998
0999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012 const void* RepeatedFieldData(const Message& message,
1013 const FieldDescriptor* field,
1014 FieldDescriptor::CppType cpp_type,
1015 const Descriptor* message_type) const;
1016 void* RepeatedFieldData(Message* message, const FieldDescriptor* field,
1017 FieldDescriptor::CppType cpp_type,
1018 const Descriptor* message_type) const;
1019
1020
1021
1022 const internal::RepeatedFieldAccessor* RepeatedFieldAccessor(
1023 const FieldDescriptor* field) const;
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033 bool IsLazyField(const FieldDescriptor* field) const {
1034 return IsLazilyVerifiedLazyField(field) ||
1035 IsEagerlyVerifiedLazyField(field);
1036 }
1037
1038
1039
1040 bool IsLazyExtension(const Message& message,
1041 const FieldDescriptor* field) const;
1042
1043 bool IsLazilyVerifiedLazyField(const FieldDescriptor* field) const;
1044 bool IsEagerlyVerifiedLazyField(const FieldDescriptor* field) const;
1045 internal::field_layout::TransformValidation GetLazyStyle(
1046 const FieldDescriptor* field) const;
1047
1048 bool IsSplit(const FieldDescriptor* field) const {
1049 return schema_.IsSplit(field);
1050 }
1051
1052
1053
1054
1055 void MaybePoisonAfterClear(Message& root) const;
1056
1057 friend class FastReflectionBase;
1058 friend class FastReflectionMessageMutator;
1059 friend class internal::ReflectionVisit;
1060 friend bool internal::IsDescendant(Message& root, const Message& message);
1061 friend void internal::MaybePoisonAfterClear(Message* root);
1062
1063 const Descriptor* const descriptor_;
1064 const internal::ReflectionSchema schema_;
1065 const DescriptorPool* const descriptor_pool_;
1066 MessageFactory* const message_factory_;
1067
1068
1069
1070
1071 int last_non_weak_field_index_;
1072
1073
1074
1075
1076 mutable absl::once_flag tcparse_table_once_;
1077 using TcParseTableBase = internal::TcParseTableBase;
1078 mutable const TcParseTableBase* tcparse_table_ = nullptr;
1079
1080 const TcParseTableBase* GetTcParseTable() const {
1081 absl::call_once(tcparse_table_once_,
1082 [&] { tcparse_table_ = CreateTcParseTable(); });
1083 return tcparse_table_;
1084 }
1085
1086 const TcParseTableBase* CreateTcParseTable() const;
1087 void PopulateTcParseFastEntries(
1088 const internal::TailCallTableInfo& table_info,
1089 TcParseTableBase::FastFieldEntry* fast_entries) const;
1090 void PopulateTcParseEntries(internal::TailCallTableInfo& table_info,
1091 TcParseTableBase::FieldEntry* entries) const;
1092 void PopulateTcParseFieldAux(const internal::TailCallTableInfo& table_info,
1093 TcParseTableBase::FieldAux* field_aux) const;
1094
1095 template <typename T, typename Enable>
1096 friend class RepeatedFieldRef;
1097 template <typename T, typename Enable>
1098 friend class MutableRepeatedFieldRef;
1099 friend class Message;
1100 friend class MessageLayoutInspector;
1101 friend class AssignDescriptorsHelper;
1102 friend class DynamicMessageFactory;
1103 friend class GeneratedMessageReflectionTestHelper;
1104 friend class python::MapReflectionFriend;
1105 friend class python::MessageReflectionFriend;
1106 friend class util::MessageDifferencer;
1107 #define GOOGLE_PROTOBUF_HAS_CEL_MAP_REFLECTION_FRIEND
1108 friend class expr::CelMapReflectionFriend;
1109 friend class internal::MapFieldReflectionTest;
1110 friend class internal::MapKeySorter;
1111 friend class internal::MessageUtil;
1112 friend class internal::WireFormat;
1113 friend class internal::ReflectionOps;
1114 friend class internal::SwapFieldHelper;
1115 template <bool is_oneof>
1116 friend struct internal::DynamicFieldInfoHelper;
1117 friend struct internal::FuzzPeer;
1118
1119 friend class internal::MapFieldPrinterHelper;
1120
1121 Reflection(const Descriptor* descriptor,
1122 const internal::ReflectionSchema& schema,
1123 const DescriptorPool* pool, MessageFactory* factory);
1124
1125
1126
1127
1128
1129 const void* GetRawRepeatedString(const Message& message,
1130 const FieldDescriptor* field,
1131 bool is_string) const;
1132 void* MutableRawRepeatedString(Message* message, const FieldDescriptor* field,
1133 bool is_string) const;
1134
1135 friend class MapReflectionTester;
1136
1137 bool ContainsMapKey(const Message& message, const FieldDescriptor* field,
1138 const MapKey& key) const;
1139
1140
1141
1142
1143
1144 bool InsertOrLookupMapValue(Message* message, const FieldDescriptor* field,
1145 const MapKey& key, MapValueRef* val) const;
1146
1147
1148
1149
1150 bool LookupMapValue(const Message& message, const FieldDescriptor* field,
1151 const MapKey& key, MapValueConstRef* val) const;
1152 bool LookupMapValue(const Message&, const FieldDescriptor*, const MapKey&,
1153 MapValueRef*) const = delete;
1154
1155
1156
1157 bool DeleteMapValue(Message* message, const FieldDescriptor* field,
1158 const MapKey& key) const;
1159
1160
1161
1162
1163 MapIterator MapBegin(Message* message, const FieldDescriptor* field) const;
1164
1165
1166
1167
1168 MapIterator MapEnd(Message* message, const FieldDescriptor* field) const;
1169
1170
1171
1172 int MapSize(const Message& message, const FieldDescriptor* field) const;
1173
1174
1175 friend class MapIterator;
1176 friend class WireFormatForMapFieldTest;
1177 internal::MapFieldBase* MutableMapData(Message* message,
1178 const FieldDescriptor* field) const;
1179
1180 const internal::MapFieldBase* GetMapData(const Message& message,
1181 const FieldDescriptor* field) const;
1182
1183 template <class T>
1184 const T& GetRawNonOneof(const Message& message,
1185 const FieldDescriptor* field) const;
1186 template <class T>
1187 const T& GetRawSplit(const Message& message,
1188 const FieldDescriptor* field) const;
1189 template <typename Type>
1190 const Type& GetRaw(const Message& message,
1191 const FieldDescriptor* field) const;
1192
1193 void* MutableRawNonOneofImpl(Message* message,
1194 const FieldDescriptor* field) const;
1195 void* MutableRawSplitImpl(Message* message,
1196 const FieldDescriptor* field) const;
1197 void* MutableRawImpl(Message* message, const FieldDescriptor* field) const;
1198
1199 template <typename Type>
1200 Type* MutableRawNonOneof(Message* message,
1201 const FieldDescriptor* field) const {
1202 return reinterpret_cast<Type*>(MutableRawNonOneofImpl(message, field));
1203 }
1204 template <typename Type>
1205 Type* MutableRaw(Message* message, const FieldDescriptor* field) const {
1206 return reinterpret_cast<Type*>(MutableRawImpl(message, field));
1207 }
1208
1209 template <typename Type>
1210 const Type& DefaultRaw(const FieldDescriptor* field) const;
1211
1212 const Message* GetDefaultMessageInstance(const FieldDescriptor* field) const;
1213
1214 const uint32_t* GetHasBits(const Message& message) const;
1215 inline uint32_t* MutableHasBits(Message* message) const;
1216 uint32_t GetOneofCase(const Message& message,
1217 const OneofDescriptor* oneof_descriptor) const;
1218 inline uint32_t* MutableOneofCase(
1219 Message* message, const OneofDescriptor* oneof_descriptor) const;
1220 inline bool HasExtensionSet(const Message& ) const {
1221 return schema_.HasExtensionSet();
1222 }
1223 const internal::ExtensionSet& GetExtensionSet(const Message& message) const;
1224 internal::ExtensionSet* MutableExtensionSet(Message* message) const;
1225
1226 const internal::InternalMetadata& GetInternalMetadata(
1227 const Message& message) const;
1228
1229 internal::InternalMetadata* MutableInternalMetadata(Message* message) const;
1230
1231 inline bool IsInlined(const FieldDescriptor* field) const {
1232 return schema_.IsFieldInlined(field);
1233 }
1234
1235 bool HasBit(const Message& message, const FieldDescriptor* field) const;
1236 void SetBit(Message* message, const FieldDescriptor* field) const;
1237 inline void ClearBit(Message* message, const FieldDescriptor* field) const;
1238 inline void SwapBit(Message* message1, Message* message2,
1239 const FieldDescriptor* field) const;
1240
1241 inline const uint32_t* GetInlinedStringDonatedArray(
1242 const Message& message) const;
1243 inline uint32_t* MutableInlinedStringDonatedArray(Message* message) const;
1244 inline bool IsInlinedStringDonated(const Message& message,
1245 const FieldDescriptor* field) const;
1246 inline void SwapInlinedStringDonated(Message* lhs, Message* rhs,
1247 const FieldDescriptor* field) const;
1248
1249
1250 inline const void* GetSplitField(const Message* message) const;
1251
1252 inline void** MutableSplitField(Message* message) const;
1253
1254
1255 void PrepareSplitMessageForWrite(Message* message) const;
1256
1257
1258
1259 void UnsafeShallowSwapFields(
1260 Message* message1, Message* message2,
1261 const std::vector<const FieldDescriptor*>& fields) const;
1262
1263
1264
1265 void SwapField(Message* message1, Message* message2,
1266 const FieldDescriptor* field) const;
1267
1268
1269 void UnsafeShallowSwapField(Message* message1, Message* message2,
1270 const FieldDescriptor* field) const;
1271
1272 template <bool unsafe_shallow_swap>
1273 void SwapFieldsImpl(Message* message1, Message* message2,
1274 const std::vector<const FieldDescriptor*>& fields) const;
1275
1276 template <bool unsafe_shallow_swap>
1277 void SwapOneofField(Message* lhs, Message* rhs,
1278 const OneofDescriptor* oneof_descriptor) const;
1279
1280 void InternalSwap(Message* lhs, Message* rhs) const;
1281
1282 inline bool HasOneofField(const Message& message,
1283 const FieldDescriptor* field) const;
1284 inline void SetOneofCase(Message* message,
1285 const FieldDescriptor* field) const;
1286 void ClearOneofField(Message* message, const FieldDescriptor* field) const;
1287
1288 template <typename Type>
1289 inline const Type& GetField(const Message& message,
1290 const FieldDescriptor* field) const;
1291 template <typename Type>
1292 inline void SetField(Message* message, const FieldDescriptor* field,
1293 const Type& value) const;
1294 template <typename Type>
1295 inline Type* MutableField(Message* message,
1296 const FieldDescriptor* field) const;
1297 template <typename Type>
1298 inline const Type& GetRepeatedField(const Message& message,
1299 const FieldDescriptor* field,
1300 int index) const;
1301 template <typename Type>
1302 inline const Type& GetRepeatedPtrField(const Message& message,
1303 const FieldDescriptor* field,
1304 int index) const;
1305 template <typename Type>
1306 inline void SetRepeatedField(Message* message, const FieldDescriptor* field,
1307 int index, Type value) const;
1308 template <typename Type>
1309 inline Type* MutableRepeatedField(Message* message,
1310 const FieldDescriptor* field,
1311 int index) const;
1312 template <typename Type>
1313 inline void AddField(Message* message, const FieldDescriptor* field,
1314 const Type& value) const;
1315 template <typename Type>
1316 inline Type* AddField(Message* message, const FieldDescriptor* field) const;
1317
1318 int GetExtensionNumberOrDie(const Descriptor* type) const;
1319
1320
1321
1322 void SetEnumValueInternal(Message* message, const FieldDescriptor* field,
1323 int value) const;
1324 void SetRepeatedEnumValueInternal(Message* message,
1325 const FieldDescriptor* field, int index,
1326 int value) const;
1327 void AddEnumValueInternal(Message* message, const FieldDescriptor* field,
1328 int value) const;
1329
1330 friend inline const char* ParseLenDelim(int field_number,
1331 const FieldDescriptor* field,
1332 Message* msg,
1333 const Reflection* reflection,
1334 const char* ptr,
1335 internal::ParseContext* ctx);
1336 friend inline const char* ParsePackedField(const FieldDescriptor* field,
1337 Message* msg,
1338 const Reflection* reflection,
1339 const char* ptr,
1340 internal::ParseContext* ctx);
1341 };
1342
1343 extern template void Reflection::SwapFieldsImpl<true>(
1344 Message* message1, Message* message2,
1345 const std::vector<const FieldDescriptor*>& fields) const;
1346
1347 extern template void Reflection::SwapFieldsImpl<false>(
1348 Message* message1, Message* message2,
1349 const std::vector<const FieldDescriptor*>& fields) const;
1350
1351
1352
1353
1354
1355 class PROTOBUF_EXPORT MessageFactory {
1356 public:
1357 inline MessageFactory() = default;
1358 MessageFactory(const MessageFactory&) = delete;
1359 MessageFactory& operator=(const MessageFactory&) = delete;
1360 virtual ~MessageFactory();
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380 virtual const Message* GetPrototype(const Descriptor* type) = 0;
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396 static MessageFactory* generated_factory();
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406 static void InternalRegisterGeneratedFile(
1407 const google::protobuf::internal::DescriptorTable* table);
1408
1409
1410
1411
1412 static void InternalRegisterGeneratedMessage(const Descriptor* descriptor,
1413 const Message* prototype);
1414
1415
1416 private:
1417 friend class DynamicMessageFactory;
1418 static const Message* TryGetGeneratedPrototype(const Descriptor* type);
1419 };
1420
1421 #define DECLARE_GET_REPEATED_FIELD(TYPE) \
1422 template <> \
1423 PROTOBUF_EXPORT const RepeatedField<TYPE>& \
1424 Reflection::GetRepeatedFieldInternal<TYPE>( \
1425 const Message& message, const FieldDescriptor* field) const; \
1426 \
1427 template <> \
1428 PROTOBUF_EXPORT RepeatedField<TYPE>* \
1429 Reflection::MutableRepeatedFieldInternal<TYPE>( \
1430 Message * message, const FieldDescriptor* field) const;
1431
1432 DECLARE_GET_REPEATED_FIELD(int32_t)
1433 DECLARE_GET_REPEATED_FIELD(int64_t)
1434 DECLARE_GET_REPEATED_FIELD(uint32_t)
1435 DECLARE_GET_REPEATED_FIELD(uint64_t)
1436 DECLARE_GET_REPEATED_FIELD(float)
1437 DECLARE_GET_REPEATED_FIELD(double)
1438 DECLARE_GET_REPEATED_FIELD(bool)
1439
1440 #undef DECLARE_GET_REPEATED_FIELD
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462 template <typename T>
1463 void LinkMessageReflection() {
1464 internal::StrongReferenceToType<T>();
1465 }
1466
1467
1468
1469 template <>
1470 inline const Message* DynamicCastMessage(const MessageLite* from) {
1471 return from == nullptr || internal::GetClassData(*from)->is_lite
1472 ? nullptr
1473 : static_cast<const Message*>(from);
1474 }
1475 template <>
1476 inline const Message* DownCastMessage(const MessageLite* from) {
1477 ABSL_DCHECK(DynamicCastMessage<Message>(from) == from)
1478 << "Cannot downcast " << from->GetTypeName() << " to Message";
1479 return static_cast<const Message*>(from);
1480 }
1481
1482
1483
1484
1485
1486
1487
1488
1489 template <>
1490 inline const RepeatedPtrField<std::string>&
1491 Reflection::GetRepeatedPtrFieldInternal<std::string>(
1492 const Message& message, const FieldDescriptor* field) const {
1493 return *static_cast<const RepeatedPtrField<std::string>*>(
1494 GetRawRepeatedString(message, field, true));
1495 }
1496
1497 template <>
1498 inline RepeatedPtrField<std::string>*
1499 Reflection::MutableRepeatedPtrFieldInternal<std::string>(
1500 Message* message, const FieldDescriptor* field) const {
1501 return static_cast<RepeatedPtrField<std::string>*>(
1502 MutableRawRepeatedString(message, field, true));
1503 }
1504
1505
1506
1507
1508 template <>
1509 inline const RepeatedPtrField<Message>& Reflection::GetRepeatedPtrFieldInternal(
1510 const Message& message, const FieldDescriptor* field) const {
1511 return *static_cast<const RepeatedPtrField<Message>*>(GetRawRepeatedField(
1512 message, field, FieldDescriptor::CPPTYPE_MESSAGE, -1, nullptr));
1513 }
1514
1515 template <>
1516 inline RepeatedPtrField<Message>* Reflection::MutableRepeatedPtrFieldInternal(
1517 Message* message, const FieldDescriptor* field) const {
1518 return static_cast<RepeatedPtrField<Message>*>(MutableRawRepeatedField(
1519 message, field, FieldDescriptor::CPPTYPE_MESSAGE, -1, nullptr));
1520 }
1521
1522 template <typename PB>
1523 inline const RepeatedPtrField<PB>& Reflection::GetRepeatedPtrFieldInternal(
1524 const Message& message, const FieldDescriptor* field) const {
1525 return *static_cast<const RepeatedPtrField<PB>*>(
1526 GetRawRepeatedField(message, field, FieldDescriptor::CPPTYPE_MESSAGE, -1,
1527 PB::default_instance().GetDescriptor()));
1528 }
1529
1530 template <typename PB>
1531 inline RepeatedPtrField<PB>* Reflection::MutableRepeatedPtrFieldInternal(
1532 Message* message, const FieldDescriptor* field) const {
1533 return static_cast<RepeatedPtrField<PB>*>(
1534 MutableRawRepeatedField(message, field, FieldDescriptor::CPPTYPE_MESSAGE,
1535 -1, PB::default_instance().GetDescriptor()));
1536 }
1537
1538 template <typename Type>
1539 const Type& Reflection::DefaultRaw(const FieldDescriptor* field) const {
1540 return *reinterpret_cast<const Type*>(schema_.GetFieldDefault(field));
1541 }
1542
1543 bool Reflection::HasOneofField(const Message& message,
1544 const FieldDescriptor* field) const {
1545 return (GetOneofCase(message, field->containing_oneof()) ==
1546 static_cast<uint32_t>(field->number()));
1547 }
1548
1549 const void* Reflection::GetSplitField(const Message* message) const {
1550 ABSL_DCHECK(schema_.IsSplit());
1551 return *internal::GetConstPointerAtOffset<void*>(message,
1552 schema_.SplitOffset());
1553 }
1554
1555 void** Reflection::MutableSplitField(Message* message) const {
1556 ABSL_DCHECK(schema_.IsSplit());
1557 return internal::GetPointerAtOffset<void*>(message, schema_.SplitOffset());
1558 }
1559
1560 namespace internal {
1561
1562
1563
1564
1565
1566 template <typename T>
1567 bool SplitFieldHasExtraIndirectionStatic(const FieldDescriptor* field) {
1568 if (std::is_base_of<RepeatedFieldBase, T>() ||
1569 std::is_base_of<RepeatedPtrFieldBase, T>()) {
1570 ABSL_DCHECK(SplitFieldHasExtraIndirection(field));
1571 return true;
1572 } else if (std::is_base_of<MessageLite, T>()) {
1573 ABSL_DCHECK(!SplitFieldHasExtraIndirection(field));
1574 return false;
1575 }
1576 return SplitFieldHasExtraIndirection(field);
1577 }
1578
1579 inline void MaybePoisonAfterClear(Message* root) {
1580 if (root == nullptr) return;
1581 #ifndef PROTOBUF_ASAN
1582 root->Clear();
1583 #else
1584 const Reflection* reflection = root->GetReflection();
1585 reflection->MaybePoisonAfterClear(*root);
1586 #endif
1587 }
1588
1589 }
1590
1591 template <typename Type>
1592 const Type& Reflection::GetRawSplit(const Message& message,
1593 const FieldDescriptor* field) const {
1594 ABSL_DCHECK(!schema_.InRealOneof(field)) << "Field = " << field->full_name();
1595
1596 const void* split = GetSplitField(&message);
1597 const uint32_t field_offset = schema_.GetFieldOffsetNonOneof(field);
1598 if (internal::SplitFieldHasExtraIndirectionStatic<Type>(field)) {
1599 return **internal::GetConstPointerAtOffset<Type*>(split, field_offset);
1600 }
1601 return *internal::GetConstPointerAtOffset<Type>(split, field_offset);
1602 }
1603
1604 template <class Type>
1605 const Type& Reflection::GetRawNonOneof(const Message& message,
1606 const FieldDescriptor* field) const {
1607 if (PROTOBUF_PREDICT_FALSE(schema_.IsSplit(field))) {
1608 return GetRawSplit<Type>(message, field);
1609 }
1610 const uint32_t field_offset = schema_.GetFieldOffsetNonOneof(field);
1611 return internal::GetConstRefAtOffset<Type>(message, field_offset);
1612 }
1613
1614 template <typename Type>
1615 const Type& Reflection::GetRaw(const Message& message,
1616 const FieldDescriptor* field) const {
1617 ABSL_DCHECK(!schema_.InRealOneof(field) || HasOneofField(message, field))
1618 << "Field = " << field->full_name();
1619
1620 if (PROTOBUF_PREDICT_TRUE(!schema_.InRealOneof(field))) {
1621 return GetRawNonOneof<Type>(message, field);
1622 }
1623
1624
1625 ABSL_DCHECK(!schema_.IsSplit(field));
1626
1627 const uint32_t field_offset = schema_.GetFieldOffset(field);
1628 return internal::GetConstRefAtOffset<Type>(message, field_offset);
1629 }
1630
1631 template <typename T>
1632 RepeatedFieldRef<T> Reflection::GetRepeatedFieldRef(
1633 const Message& message, const FieldDescriptor* field) const {
1634 return RepeatedFieldRef<T>(message, field);
1635 }
1636
1637 template <typename T>
1638 MutableRepeatedFieldRef<T> Reflection::GetMutableRepeatedFieldRef(
1639 Message* message, const FieldDescriptor* field) const {
1640 return MutableRepeatedFieldRef<T>(message, field);
1641 }
1642
1643
1644 }
1645 }
1646
1647 #include "google/protobuf/port_undef.inc"
1648
1649 #endif