Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-31 10:12:23

0001 // Protocol Buffers - Google's data interchange format
0002 // Copyright 2008 Google Inc.  All rights reserved.
0003 //
0004 // Use of this source code is governed by a BSD-style
0005 // license that can be found in the LICENSE file or at
0006 // https://developers.google.com/open-source/licenses/bsd
0007 
0008 // Author: kenton@google.com (Kenton Varda)
0009 //  Based on original Protocol Buffers design by
0010 //  Sanjay Ghemawat, Jeff Dean, and others.
0011 //
0012 // Defines Message, the abstract interface implemented by non-lite
0013 // protocol message objects.  Although it's possible to implement this
0014 // interface manually, most users will use the protocol compiler to
0015 // generate implementations.
0016 //
0017 // Example usage:
0018 //
0019 // Say you have a message defined as:
0020 //
0021 //   message Foo {
0022 //     optional string text = 1;
0023 //     repeated int32 numbers = 2;
0024 //   }
0025 //
0026 // Then, if you used the protocol compiler to generate a class from the above
0027 // definition, you could use it like so:
0028 //
0029 //   std::string data;  // Will store a serialized version of the message.
0030 //
0031 //   {
0032 //     // Create a message and serialize it.
0033 //     Foo foo;
0034 //     foo.set_text("Hello World!");
0035 //     foo.add_numbers(1);
0036 //     foo.add_numbers(5);
0037 //     foo.add_numbers(42);
0038 //
0039 //     foo.SerializeToString(&data);
0040 //   }
0041 //
0042 //   {
0043 //     // Parse the serialized message and check that it contains the
0044 //     // correct data.
0045 //     Foo foo;
0046 //     foo.ParseFromString(data);
0047 //
0048 //     assert(foo.text() == "Hello World!");
0049 //     assert(foo.numbers_size() == 3);
0050 //     assert(foo.numbers(0) == 1);
0051 //     assert(foo.numbers(1) == 5);
0052 //     assert(foo.numbers(2) == 42);
0053 //   }
0054 //
0055 //   {
0056 //     // Same as the last block, but do it dynamically via the Message
0057 //     // reflection interface.
0058 //     Message* foo = new Foo;
0059 //     const Descriptor* descriptor = foo->GetDescriptor();
0060 //
0061 //     // Get the descriptors for the fields we're interested in and verify
0062 //     // their types.
0063 //     const FieldDescriptor* text_field = descriptor->FindFieldByName("text");
0064 //     assert(text_field != nullptr);
0065 //     assert(text_field->type() == FieldDescriptor::TYPE_STRING);
0066 //     assert(text_field->label() == FieldDescriptor::LABEL_OPTIONAL);
0067 //     const FieldDescriptor* numbers_field = descriptor->
0068 //                                            FindFieldByName("numbers");
0069 //     assert(numbers_field != nullptr);
0070 //     assert(numbers_field->type() == FieldDescriptor::TYPE_INT32);
0071 //     assert(numbers_field->label() == FieldDescriptor::LABEL_REPEATED);
0072 //
0073 //     // Parse the message.
0074 //     foo->ParseFromString(data);
0075 //
0076 //     // Use the reflection interface to examine the contents.
0077 //     const Reflection* reflection = foo->GetReflection();
0078 //     assert(reflection->GetString(*foo, text_field) == "Hello World!");
0079 //     assert(reflection->FieldSize(*foo, numbers_field) == 3);
0080 //     assert(reflection->GetRepeatedInt32(*foo, numbers_field, 0) == 1);
0081 //     assert(reflection->GetRepeatedInt32(*foo, numbers_field, 1) == 5);
0082 //     assert(reflection->GetRepeatedInt32(*foo, numbers_field, 2) == 42);
0083 //
0084 //     delete foo;
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 // Must be included last.
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 // Defined in this file.
0126 class Message;
0127 class Reflection;
0128 class MessageFactory;
0129 
0130 // Defined in other files.
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 }  // namespace field_layout
0156 
0157 }  // namespace internal
0158 class UnknownFieldSet;  // unknown_field_set.h
0159 namespace io {
0160 class EpsCopyOutputStream;   // coded_stream.h
0161 class ZeroCopyInputStream;   // zero_copy_stream.h
0162 class ZeroCopyOutputStream;  // zero_copy_stream.h
0163 class CodedInputStream;      // coded_stream.h
0164 class CodedOutputStream;     // coded_stream.h
0165 }  // namespace io
0166 namespace python {
0167 class MapReflectionFriend;  // scalar_map_container.h
0168 class MessageReflectionFriend;
0169 }  // namespace python
0170 namespace expr {
0171 class CelMapReflectionFriend;  // field_backed_map_impl.cc
0172 class SudoMapReflectionFriend;
0173 }
0174 
0175 namespace internal {
0176 class MapFieldPrinterHelper;  // text_format.cc
0177 PROTOBUF_EXPORT std::string StringifyMessage(
0178     const Message& message);  // text_format.cc
0179 }  // namespace internal
0180 PROTOBUF_EXPORT std::string ShortFormat(
0181     const Message& message);  // text_format.cc
0182 PROTOBUF_EXPORT std::string Utf8Format(
0183     const Message& message);  // text_format.cc
0184 namespace util {
0185 class MessageDifferencer;
0186 }
0187 
0188 
0189 namespace internal {
0190 class ReflectionAccessor;      // message.cc
0191 class ReflectionOps;           // reflection_ops.h
0192 class MapKeySorter;            // wire_format.cc
0193 class WireFormat;              // wire_format.h
0194 class MapFieldReflectionTest;  // map_test.cc
0195 }  // namespace internal
0196 
0197 template <typename T>
0198 class RepeatedField;  // repeated_field.h
0199 
0200 template <typename T>
0201 class RepeatedPtrField;  // repeated_field.h
0202 
0203 // A container to hold message metadata.
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 // Returns true if "message" is a descendant of "root".
0229 PROTOBUF_EXPORT bool IsDescendant(Message& root, const Message& message);
0230 
0231 inline void MaybePoisonAfterClear(Message* root);
0232 }  // namespace internal
0233 
0234 // Abstract interface for protocol messages.
0235 //
0236 // See also MessageLite, which contains most every-day operations.  Message
0237 // adds descriptors and reflection on top of that.
0238 //
0239 // The methods of this class that are virtual but not pure-virtual have
0240 // default implementations based on reflection.  Message classes which are
0241 // optimized for speed will want to override these with faster implementations,
0242 // but classes optimized for code size may be happy with keeping them.  See
0243 // the optimize_for option in descriptor.proto.
0244 //
0245 // Users must not derive from this class. Only the protocol compiler and
0246 // the internal library are allowed to create subclasses.
0247 class PROTOBUF_EXPORT Message : public MessageLite {
0248  public:
0249   Message(const Message&) = delete;
0250   Message& operator=(const Message&) = delete;
0251 
0252   // Basic Operations ------------------------------------------------
0253 
0254   // Construct a new instance of the same type.  Ownership is passed to the
0255   // caller.  (This is also defined in MessageLite, but is defined again here
0256   // for return-type covariance.)
0257   Message* New() const { return New(nullptr); }
0258 
0259   // Construct a new instance on the arena. Ownership is passed to the caller
0260   // if arena is a nullptr.
0261 #if defined(PROTOBUF_CUSTOM_VTABLE)
0262   Message* New(Arena* arena) const {
0263     return static_cast<Message*>(MessageLite::New(arena));
0264   }
0265 #else   // PROTOBUF_CUSTOM_VTABLE
0266   Message* New(Arena* arena) const override = 0;
0267 #endif  // PROTOBUF_CUSTOM_VTABLE
0268 
0269   // Make this message into a copy of the given message.  The given message
0270   // must have the same descriptor, but need not necessarily be the same class.
0271   // By default this is just implemented as "Clear(); MergeFrom(from);".
0272   void CopyFrom(const Message& from);
0273 
0274   // Merge the fields from the given message into this message.  Singular
0275   // fields will be overwritten, if specified in from, except for embedded
0276   // messages which will be merged.  Repeated fields will be concatenated.
0277   // The given message must be of the same type as this message (i.e. the
0278   // exact same class).
0279   void MergeFrom(const Message& from);
0280 
0281   // Verifies that IsInitialized() returns true.  ABSL_CHECK-fails otherwise,
0282   // with a nice error message.
0283   void CheckInitialized() const;
0284 
0285   // Slowly build a list of all required fields that are not set.
0286   // This is much, much slower than IsInitialized() as it is implemented
0287   // purely via reflection.  Generally, you should not call this unless you
0288   // have already determined that an error exists by calling IsInitialized().
0289   void FindInitializationErrors(std::vector<std::string>* errors) const;
0290 
0291   // Like FindInitializationErrors, but joins all the strings, delimited by
0292   // commas, and returns them.
0293   std::string InitializationErrorString() const;
0294 
0295   // Clears all unknown fields from this message and all embedded messages.
0296   // Normally, if unknown tag numbers are encountered when parsing a message,
0297   // the tag and value are stored in the message's UnknownFieldSet and
0298   // then written back out when the message is serialized.  This allows servers
0299   // which simply route messages to other servers to pass through messages
0300   // that have new field definitions which they don't yet know about.  However,
0301   // this behavior can have security implications.  To avoid it, call this
0302   // method after parsing.
0303   //
0304   // See Reflection::GetUnknownFields() for more on unknown fields.
0305   void DiscardUnknownFields();
0306 
0307   // Computes (an estimate of) the total number of bytes currently used for
0308   // storing the message in memory.
0309   //
0310   // SpaceUsed() is noticeably slower than ByteSize(), as it is implemented
0311   // using reflection (rather than the generated code implementation for
0312   // ByteSize()). Like ByteSize(), its CPU time is linear in the number of
0313   // fields defined for the proto.
0314   //
0315   // Note: The precise value of this method should never be depended on, and can
0316   // change substantially due to internal details.  In debug builds, this will
0317   // include a random fuzz factor to prevent these dependencies.
0318   size_t SpaceUsedLong() const;
0319 
0320   [[deprecated("Please use SpaceUsedLong() instead")]] int SpaceUsed() const {
0321     return internal::ToIntSize(SpaceUsedLong());
0322   }
0323 
0324   // Debugging & Testing----------------------------------------------
0325 
0326   // Generates a human-readable form of this message for debugging purposes.
0327   // Note that the format and content of a debug string is not guaranteed, may
0328   // change without notice, and should not be depended on. Code that does
0329   // anything except display a string to assist in debugging should use
0330   // TextFormat instead.
0331   std::string DebugString() const;
0332   // Like DebugString(), but with less whitespace.
0333   std::string ShortDebugString() const;
0334   // Like DebugString(), but do not escape UTF-8 byte sequences.
0335   std::string Utf8DebugString() const;
0336   // Convenience function useful in GDB.  Prints DebugString() to stdout.
0337   void PrintDebugString() const;
0338 
0339   // Implementation of the `AbslStringify` interface. This adds something
0340   // similar to either `ShortDebugString()` or `DebugString()` to the sink.
0341   // Do not rely on exact format.
0342   template <typename Sink>
0343   friend void AbslStringify(Sink& sink, const google::protobuf::Message& message) {
0344     sink.Append(internal::StringifyMessage(message));
0345   }
0346 
0347   // Reflection-based methods ----------------------------------------
0348   // These methods are pure-virtual in MessageLite, but Message provides
0349   // reflection-based default implementations.
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  // !PROTOBUF_CUSTOM_VTABLE
0357 
0358   // Introspection ---------------------------------------------------
0359 
0360 
0361   // Get a non-owning pointer to a Descriptor for this message's type.  This
0362   // describes what fields the message contains, the types of those fields, etc.
0363   // This object remains property of the Message.
0364   const Descriptor* GetDescriptor() const { return GetMetadata().descriptor; }
0365 
0366   // Get a non-owning pointer to the Reflection interface for this Message,
0367   // which can be used to read and modify the fields of the Message dynamically
0368   // (in other words, without knowing the message type at compile time).  This
0369   // object remains property of the Message.
0370   const Reflection* GetReflection() const { return GetMetadata().reflection; }
0371 
0372  protected:
0373 #if !defined(PROTOBUF_CUSTOM_VTABLE)
0374   constexpr Message() {}
0375 #endif  // PROTOBUF_CUSTOM_VTABLE
0376   using MessageLite::MessageLite;
0377 
0378   // Get a struct containing the metadata for the Message, which is used in turn
0379   // to implement GetDescriptor() and GetReflection() above.
0380   Metadata GetMetadata() const;
0381   static Metadata GetMetadataImpl(const ClassDataFull& data);
0382 
0383   // For CODE_SIZE types
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   // Reflection based version for reflection based types.
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 // Creates and returns an allocation for a split message.
0411 void* CreateSplitMessageGeneric(Arena* arena, const void* default_split,
0412                                 size_t size, const void* message,
0413                                 const void* default_message);
0414 
0415 // Forward-declare interfaces used to implement RepeatedFieldRef.
0416 // These are protobuf internals that users shouldn't care about.
0417 class RepeatedFieldAccessor;
0418 }  // namespace internal
0419 
0420 // This interface contains methods that can be used to dynamically access
0421 // and modify the fields of a protocol message.  Their semantics are
0422 // similar to the accessors the protocol compiler generates.
0423 //
0424 // To get the Reflection for a given Message, call Message::GetReflection().
0425 //
0426 // This interface is separate from Message only for efficiency reasons;
0427 // the vast majority of implementations of Message will share the same
0428 // implementation of Reflection (GeneratedMessageReflection,
0429 // defined in generated_message.h), and all Messages of a particular class
0430 // should share the same Reflection object (though you should not rely on
0431 // the latter fact).
0432 //
0433 // There are several ways that these methods can be used incorrectly.  For
0434 // example, any of the following conditions will lead to undefined
0435 // results (probably assertion failures):
0436 // - The FieldDescriptor is not a field of this message type.
0437 // - The method called is not appropriate for the field's type.  For
0438 //   each field type in FieldDescriptor::TYPE_*, there is only one
0439 //   Get*() method, one Set*() method, and one Add*() method that is
0440 //   valid for that type.  It should be obvious which (except maybe
0441 //   for TYPE_BYTES, which are represented using strings in C++).
0442 // - A Get*() or Set*() method for singular fields is called on a repeated
0443 //   field.
0444 // - GetRepeated*(), SetRepeated*(), or Add*() is called on a non-repeated
0445 //   field.
0446 // - The Message object passed to any method is not of the right type for
0447 //   this Reflection object (i.e. message.GetReflection() != reflection).
0448 //
0449 // You might wonder why there is not any abstract representation for a field
0450 // of arbitrary type.  E.g., why isn't there just a "GetField()" method that
0451 // returns "const Field&", where "Field" is some class with accessors like
0452 // "GetInt32Value()".  The problem is that someone would have to deal with
0453 // allocating these Field objects.  For generated message classes, having to
0454 // allocate space for an additional object to wrap every field would at least
0455 // double the message's memory footprint, probably worse.  Allocating the
0456 // objects on-demand, on the other hand, would be expensive and prone to
0457 // memory leaks.  So, instead we ended up with this flat interface.
0458 class PROTOBUF_EXPORT Reflection final {
0459  public:
0460   Reflection(const Reflection&) = delete;
0461   Reflection& operator=(const Reflection&) = delete;
0462   ~Reflection();
0463 
0464   // Get the UnknownFieldSet for the message.  This contains fields which
0465   // were seen when the Message was parsed but were not recognized according
0466   // to the Message's definition.
0467   const UnknownFieldSet& GetUnknownFields(const Message& message) const;
0468   // Get a mutable pointer to the UnknownFieldSet for the message.  This
0469   // contains fields which were seen when the Message was parsed but were not
0470   // recognized according to the Message's definition.
0471   UnknownFieldSet* MutableUnknownFields(Message* message) const;
0472 
0473   // Estimate the amount of memory used by the message object.
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   // Returns true if the given message is a default message instance.
0482   bool IsDefaultInstance(const Message& message) const {
0483     return schema_.IsDefaultInstance(message);
0484   }
0485 
0486   // Check if the given non-repeated field is set.
0487   bool HasField(const Message& message, const FieldDescriptor* field) const;
0488 
0489   // Get the number of elements of a repeated field.
0490   int FieldSize(const Message& message, const FieldDescriptor* field) const;
0491 
0492   // Clear the value of a field, so that HasField() returns false or
0493   // FieldSize() returns zero.
0494   void ClearField(Message* message, const FieldDescriptor* field) const;
0495 
0496   // Check if the oneof is set. Returns true if any field in oneof
0497   // is set, false otherwise.
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   // Returns the field descriptor if the oneof is set. nullptr otherwise.
0505   const FieldDescriptor* GetOneofFieldDescriptor(
0506       const Message& message, const OneofDescriptor* oneof_descriptor) const;
0507 
0508   // Removes the last element of a repeated field.
0509   // We don't provide a way to remove any element other than the last
0510   // because it invites inefficient use, such as O(n^2) filtering loops
0511   // that should have been O(n).  If you want to remove an element other
0512   // than the last, the best way to do it is to re-arrange the elements
0513   // (using Swap()) so that the one you want removed is at the end, then
0514   // call RemoveLast().
0515   void RemoveLast(Message* message, const FieldDescriptor* field) const;
0516   // Removes the last element of a repeated message field, and returns the
0517   // pointer to the caller.  Caller takes ownership of the returned pointer.
0518   PROTOBUF_NODISCARD Message* ReleaseLast(Message* message,
0519                                           const FieldDescriptor* field) const;
0520 
0521   // Similar to ReleaseLast() without internal safety and ownershp checks. This
0522   // method should only be used when the objects are on the same arena or paired
0523   // with a call to `UnsafeArenaAddAllocatedMessage`.
0524   Message* UnsafeArenaReleaseLast(Message* message,
0525                                   const FieldDescriptor* field) const;
0526 
0527   // Swap the complete contents of two messages.
0528   void Swap(Message* message1, Message* message2) const;
0529 
0530   // Swap fields listed in fields vector of two messages.
0531   void SwapFields(Message* message1, Message* message2,
0532                   const std::vector<const FieldDescriptor*>& fields) const;
0533 
0534   // Swap two elements of a repeated field.
0535   void SwapElements(Message* message, const FieldDescriptor* field, int index1,
0536                     int index2) const;
0537 
0538   // Swap without internal safety and ownership checks. This method should only
0539   // be used when the objects are on the same arena.
0540   void UnsafeArenaSwap(Message* lhs, Message* rhs) const;
0541 
0542   // SwapFields without internal safety and ownership checks. This method should
0543   // only be used when the objects are on the same arena.
0544   void UnsafeArenaSwapFields(
0545       Message* lhs, Message* rhs,
0546       const std::vector<const FieldDescriptor*>& fields) const;
0547 
0548   // List all fields of the message which are currently set, except for unknown
0549   // fields, but including extension known to the parser (i.e. compiled in).
0550   // Singular fields will only be listed if HasField(field) would return true
0551   // and repeated fields will only be listed if FieldSize(field) would return
0552   // non-zero.  Fields (both normal fields and extension fields) will be listed
0553   // ordered by field number.
0554   // Use Reflection::GetUnknownFields() or message.unknown_fields() to also get
0555   // access to fields/extensions unknown to the parser.
0556   void ListFields(const Message& message,
0557                   std::vector<const FieldDescriptor*>* output) const;
0558 
0559   // Singular field getters ------------------------------------------
0560   // These get the value of a non-repeated field.  They return the default
0561   // value for fields that aren't set.
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   // GetEnumValue() returns an enum field's value as an integer rather than
0578   // an EnumValueDescriptor*. If the integer value does not correspond to a
0579   // known value descriptor, a new value descriptor is created. (Such a value
0580   // will only be present when the new unknown-enum-value semantics are enabled
0581   // for a message.)
0582   int GetEnumValue(const Message& message, const FieldDescriptor* field) const;
0583 
0584   // See MutableMessage() for the meaning of the "factory" parameter.
0585   const Message& GetMessage(const Message& message,
0586                             const FieldDescriptor* field,
0587                             MessageFactory* factory = nullptr) const;
0588 
0589   // Get a string value without copying, if possible.
0590   //
0591   // GetString() necessarily returns a copy of the string.  This can be
0592   // inefficient when the std::string is already stored in a std::string object
0593   // in the underlying message.  GetStringReference() will return a reference to
0594   // the underlying std::string in this case.  Otherwise, it will copy the
0595   // string into *scratch and return that.
0596   //
0597   // Note:  It is perfectly reasonable and useful to write code like:
0598   //     str = reflection->GetStringReference(message, field, &str);
0599   //   This line would ensure that only one copy of the string is made
0600   //   regardless of the field's underlying representation.  When initializing
0601   //   a newly-constructed string, though, it's just as fast and more
0602   //   readable to use code like:
0603   //     std::string str = reflection->GetString(message, field);
0604   const std::string& GetStringReference(const Message& message,
0605                                         const FieldDescriptor* field,
0606                                         std::string* scratch) const;
0607 
0608   // Returns a Cord containing the value of the string field.  If the
0609   // underlying field is stored as a cord (e.g. it has the [ctype=CORD]
0610   // option), this involves no copies (just reference counting).  If the
0611   // underlying representation is not a Cord, a copy will have to be made.
0612   absl::Cord GetCord(const Message& message,
0613                      const FieldDescriptor* field) const;
0614 
0615   // Enables GetStringView() and GetRepeatedStringView() APIs to return
0616   // absl::string_view even though the underlying implementation doesn't have
0617   // contiguous bytes; e.g. absl::Cord.
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   // Returns a view into the contents of a string field. "scratch" is used to
0643   // flatten bytes if it is non-contiguous. The lifetime of absl::string_view is
0644   // either tied to "message" (contiguous) or "scratch" (otherwise).
0645   absl::string_view GetStringView(
0646       const Message& message, const FieldDescriptor* field,
0647       ScratchSpace& scratch ABSL_ATTRIBUTE_LIFETIME_BOUND) const;
0648 
0649 
0650   // Singular field mutators -----------------------------------------
0651   // These mutate the value of a non-repeated field.
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   // Set a string field to a Cord value.  If the underlying field is
0670   // represented using a Cord already, this involves no copies  (just
0671   // reference counting).  Otherwise, a copy must be made.
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   // Set an enum field's value with an integer rather than EnumValueDescriptor.
0677   // For proto3 this is just setting the enum field to the value specified, for
0678   // proto2 it's more complicated. If value is a known enum value the field is
0679   // set as usual. If the value is unknown then it is added to the unknown field
0680   // set. Note this matches the behavior of parsing unknown enum values.
0681   // If multiple calls with unknown values happen than they are all added to the
0682   // unknown field set in order of the calls.
0683   void SetEnumValue(Message* message, const FieldDescriptor* field,
0684                     int value) const;
0685 
0686   // Get a mutable pointer to a field with a message type.  If a MessageFactory
0687   // is provided, it will be used to construct instances of the sub-message;
0688   // otherwise, the default factory is used.  If the field is an extension that
0689   // does not live in the same pool as the containing message's descriptor (e.g.
0690   // it lives in an overlay pool), then a MessageFactory must be provided.
0691   // If you have no idea what that meant, then you probably don't need to worry
0692   // about it (don't provide a MessageFactory).  WARNING:  If the
0693   // FieldDescriptor is for a compiled-in extension, then
0694   // factory->GetPrototype(field->message_type()) MUST return an instance of
0695   // the compiled-in class for this type, NOT DynamicMessage.
0696   Message* MutableMessage(Message* message, const FieldDescriptor* field,
0697                           MessageFactory* factory = nullptr) const;
0698 
0699   // Replaces the message specified by 'field' with the already-allocated object
0700   // sub_message, passing ownership to the message.  If the field contained a
0701   // message, that message is deleted.  If sub_message is nullptr, the field is
0702   // cleared.
0703   void SetAllocatedMessage(Message* message, Message* sub_message,
0704                            const FieldDescriptor* field) const;
0705 
0706   // Similar to `SetAllocatedMessage`, but omits all internal safety and
0707   // ownership checks.  This method should only be used when the objects are on
0708   // the same arena or paired with a call to `UnsafeArenaReleaseMessage`.
0709   void UnsafeArenaSetAllocatedMessage(Message* message, Message* sub_message,
0710                                       const FieldDescriptor* field) const;
0711 
0712   // Releases the message specified by 'field' and returns the pointer,
0713   // ReleaseMessage() will return the message the message object if it exists.
0714   // Otherwise, it may or may not return nullptr.  In any case, if the return
0715   // value is non-null, the caller takes ownership of the pointer.
0716   // If the field existed (HasField() is true), then the returned pointer will
0717   // be the same as the pointer returned by MutableMessage().
0718   // This function has the same effect as ClearField().
0719   PROTOBUF_NODISCARD Message* ReleaseMessage(
0720       Message* message, const FieldDescriptor* field,
0721       MessageFactory* factory = nullptr) const;
0722 
0723   // Similar to `ReleaseMessage`, but omits all internal safety and ownership
0724   // checks.  This method should only be used when the objects are on the same
0725   // arena or paired with a call to `UnsafeArenaSetAllocatedMessage`.
0726   Message* UnsafeArenaReleaseMessage(Message* message,
0727                                      const FieldDescriptor* field,
0728                                      MessageFactory* factory = nullptr) const;
0729 
0730 
0731   // Repeated field getters ------------------------------------------
0732   // These get the value of one element of a repeated field.
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   // GetRepeatedEnumValue() returns an enum field's value as an integer rather
0754   // than an EnumValueDescriptor*. If the integer value does not correspond to a
0755   // known value descriptor, a new value descriptor is created. (Such a value
0756   // will only be present when the new unknown-enum-value semantics are enabled
0757   // for a message.)
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   // See GetStringReference(), above.
0765   const std::string& GetRepeatedStringReference(const Message& message,
0766                                                 const FieldDescriptor* field,
0767                                                 int index,
0768                                                 std::string* scratch) const;
0769 
0770   // See GetStringView(), above.
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   // Repeated field mutators -----------------------------------------
0777   // These mutate the value of one element of a repeated field.
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   // Set an enum field's value with an integer rather than EnumValueDescriptor.
0798   // For proto3 this is just setting the enum field to the value specified, for
0799   // proto2 it's more complicated. If value is a known enum value the field is
0800   // set as usual. If the value is unknown then it is added to the unknown field
0801   // set. Note this matches the behavior of parsing unknown enum values.
0802   // If multiple calls with unknown values happen than they are all added to the
0803   // unknown field set in order of the calls.
0804   void SetRepeatedEnumValue(Message* message, const FieldDescriptor* field,
0805                             int index, int value) const;
0806   // Get a mutable pointer to an element of a repeated field with a message
0807   // type.
0808   Message* MutableRepeatedMessage(Message* message,
0809                                   const FieldDescriptor* field,
0810                                   int index) const;
0811 
0812 
0813   // Repeated field adders -------------------------------------------
0814   // These add an element to a repeated field.
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   // Add an integer value to a repeated enum field rather than
0836   // EnumValueDescriptor. For proto3 this is just setting the enum field to the
0837   // value specified, for proto2 it's more complicated. If value is a known enum
0838   // value the field is set as usual. If the value is unknown then it is added
0839   // to the unknown field set. Note this matches the behavior of parsing unknown
0840   // enum values. If multiple calls with unknown values happen than they are all
0841   // added to the unknown field set in order of the calls.
0842   void AddEnumValue(Message* message, const FieldDescriptor* field,
0843                     int value) const;
0844   // See MutableMessage() for comments on the "factory" parameter.
0845   Message* AddMessage(Message* message, const FieldDescriptor* field,
0846                       MessageFactory* factory = nullptr) const;
0847 
0848   // Appends an already-allocated object 'new_entry' to the repeated field
0849   // specified by 'field' passing ownership to the message.
0850   void AddAllocatedMessage(Message* message, const FieldDescriptor* field,
0851                            Message* new_entry) const;
0852 
0853   // Similar to AddAllocatedMessage() without internal safety and ownership
0854   // checks. This method should only be used when the objects are on the same
0855   // arena or paired with a call to `UnsafeArenaReleaseLast`.
0856   void UnsafeArenaAddAllocatedMessage(Message* message,
0857                                       const FieldDescriptor* field,
0858                                       Message* new_entry) const;
0859 
0860 
0861   // Get a RepeatedFieldRef object that can be used to read the underlying
0862   // repeated field. The type parameter T must be set according to the
0863   // field's cpp type. The following table shows the mapping from cpp type
0864   // to acceptable T.
0865   //
0866   //   field->cpp_type()      T
0867   //   CPPTYPE_INT32        int32_t
0868   //   CPPTYPE_UINT32       uint32_t
0869   //   CPPTYPE_INT64        int64_t
0870   //   CPPTYPE_UINT64       uint64_t
0871   //   CPPTYPE_DOUBLE       double
0872   //   CPPTYPE_FLOAT        float
0873   //   CPPTYPE_BOOL         bool
0874   //   CPPTYPE_ENUM         generated enum type or int32_t
0875   //   CPPTYPE_STRING       std::string
0876   //   CPPTYPE_MESSAGE      generated message type or google::protobuf::Message
0877   //
0878   // A RepeatedFieldRef object can be copied and the resulted object will point
0879   // to the same repeated field in the same message. The object can be used as
0880   // long as the message is not destroyed.
0881   //
0882   // Note that to use this method users need to include the header file
0883   // "reflection.h" (which defines the RepeatedFieldRef class templates).
0884   template <typename T>
0885   RepeatedFieldRef<T> GetRepeatedFieldRef(const Message& message,
0886                                           const FieldDescriptor* field) const;
0887 
0888   // Like GetRepeatedFieldRef() but return an object that can also be used
0889   // manipulate the underlying repeated field.
0890   template <typename T>
0891   MutableRepeatedFieldRef<T> GetMutableRepeatedFieldRef(
0892       Message* message, const FieldDescriptor* field) const;
0893 
0894   // DEPRECATED. Please use Get(Mutable)RepeatedFieldRef() for repeated field
0895   // access. The following repeated field accessors will be removed in the
0896   // future.
0897   //
0898   // Repeated field accessors  -------------------------------------------------
0899   // The methods above, e.g. GetRepeatedInt32(msg, fd, index), provide singular
0900   // access to the data in a RepeatedField.  The methods below provide aggregate
0901   // access by exposing the RepeatedField object itself with the Message.
0902   // Applying these templates to inappropriate types will lead to an undefined
0903   // reference at link time (e.g. GetRepeatedField<***double>), or possibly a
0904   // template matching error at compile time (e.g. GetRepeatedPtrField<File>).
0905   //
0906   // Usage example: my_doubs = refl->GetRepeatedField<double>(msg, fd);
0907 
0908   // DEPRECATED. Please use GetRepeatedFieldRef().
0909   //
0910   // for T = Cord and all protobuf scalar types except enums.
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   // DEPRECATED. Please use GetMutableRepeatedFieldRef().
0919   //
0920   // for T = Cord and all protobuf scalar types except enums.
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   // DEPRECATED. Please use GetRepeatedFieldRef().
0929   //
0930   // for T = std::string, google::protobuf::internal::StringPieceField
0931   //         google::protobuf::Message & descendants.
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   // DEPRECATED. Please use GetMutableRepeatedFieldRef().
0940   //
0941   // for T = std::string, google::protobuf::internal::StringPieceField
0942   //         google::protobuf::Message & descendants.
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   // Extensions ----------------------------------------------------------------
0951 
0952   // Try to find an extension of this message type by fully-qualified field
0953   // name.  Returns nullptr if no extension is known for this name or number.
0954   const FieldDescriptor* FindKnownExtensionByName(absl::string_view name) const;
0955 
0956   // Try to find an extension of this message type by field number.
0957   // Returns nullptr if no extension is known for this name or number.
0958   const FieldDescriptor* FindKnownExtensionByNumber(int number) const;
0959 
0960   // Returns the MessageFactory associated with this message.  This can be
0961   // useful for determining if a message is a generated message or not, for
0962   // example:
0963   //   if (message->GetReflection()->GetMessageFactory() ==
0964   //       google::protobuf::MessageFactory::generated_factory()) {
0965   //     // This is a generated message.
0966   //   }
0967   // It can also be used to create more messages of this type, though
0968   // Message::New() is an easier way to accomplish this.
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   // Obtain a pointer to a Repeated Field Structure and do some type checking:
0986   //   on field->cpp_type(),
0987   //   on field->field_option().ctype() (if ctype >= 0)
0988   //   of field->message_type() (if message_type != nullptr).
0989   // We use 2 routine rather than 4 (const vs mutable) x (scalar vs pointer).
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   // The following methods are used to implement (Mutable)RepeatedFieldRef.
1000   // A Ref object will store a raw pointer to the repeated field data (obtained
1001   // from RepeatedFieldData()) and a pointer to a Accessor (obtained from
1002   // RepeatedFieldAccessor) which will be used to access the raw data.
1003 
1004   // Returns a raw pointer to the repeated field
1005   //
1006   // "cpp_type" and "message_type" are deduced from the type parameter T passed
1007   // to Get(Mutable)RepeatedFieldRef. If T is a generated message type,
1008   // "message_type" should be set to its descriptor. Otherwise "message_type"
1009   // should be set to nullptr. Implementations of this method should check
1010   // whether "cpp_type"/"message_type" is consistent with the actual type of the
1011   // field.
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   // The returned pointer should point to a singleton instance which implements
1021   // the RepeatedFieldAccessor interface.
1022   const internal::RepeatedFieldAccessor* RepeatedFieldAccessor(
1023       const FieldDescriptor* field) const;
1024 
1025   // Returns true if the message field is backed by a LazyField.
1026   //
1027   // A message field may be backed by a LazyField without the user annotation
1028   // ([lazy = true]). While the user-annotated LazyField is lazily verified on
1029   // first touch (i.e. failure on access rather than parsing if the LazyField is
1030   // not initialized), the inferred LazyField is eagerly verified to avoid lazy
1031   // parsing error at the cost of lower efficiency. When reflecting a message
1032   // field, use this API instead of checking field->options().lazy().
1033   bool IsLazyField(const FieldDescriptor* field) const {
1034     return IsLazilyVerifiedLazyField(field) ||
1035            IsEagerlyVerifiedLazyField(field);
1036   }
1037 
1038   // Returns true if the field is lazy extension. It is meant to allow python
1039   // reparse lazy field until b/157559327 is fixed.
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   // Walks the message tree from "root" and poisons (under ASAN) the memory to
1053   // force subsequent accesses to fail. Always calls Clear beforehand to clear
1054   // strings, etc.
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   // Last non weak field index. This is an optimization when most weak fields
1069   // are at the end of the containing message. If a message proto doesn't
1070   // contain weak fields, then this field equals descriptor_->field_count().
1071   int last_non_weak_field_index_;
1072 
1073   // The table-driven parser table.
1074   // This table is generated on demand for Message types that did not override
1075   // _InternalParse. It uses the reflection information to do so.
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   // Needed for implementing text format for map.
1119   friend class internal::MapFieldPrinterHelper;
1120 
1121   Reflection(const Descriptor* descriptor,
1122              const internal::ReflectionSchema& schema,
1123              const DescriptorPool* pool, MessageFactory* factory);
1124 
1125   // Special version for specialized implementations of string.  We can't
1126   // call MutableRawRepeatedField directly here because we don't have access to
1127   // FieldOptions::* which are defined in descriptor.pb.h.  Including that
1128   // file here is not possible because it would cause a circular include cycle.
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   // Returns true if key is in map. Returns false if key is not in map field.
1137   bool ContainsMapKey(const Message& message, const FieldDescriptor* field,
1138                       const MapKey& key) const;
1139 
1140   // If key is in map field: Saves the value pointer to val and returns
1141   // false. If key in not in map field: Insert the key into map, saves
1142   // value pointer to val and returns true. Users are able to modify the
1143   // map value by MapValueRef.
1144   bool InsertOrLookupMapValue(Message* message, const FieldDescriptor* field,
1145                               const MapKey& key, MapValueRef* val) const;
1146 
1147   // If key is in map field: Saves the value pointer to val and returns true.
1148   // Returns false if key is not in map field. Users are NOT able to modify
1149   // the value by MapValueConstRef.
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   // Delete and returns true if key is in the map field. Returns false
1156   // otherwise.
1157   bool DeleteMapValue(Message* message, const FieldDescriptor* field,
1158                       const MapKey& key) const;
1159 
1160   // Returns a MapIterator referring to the first element in the map field.
1161   // If the map field is empty, this function returns the same as
1162   // reflection::MapEnd. Mutation to the field may invalidate the iterator.
1163   MapIterator MapBegin(Message* message, const FieldDescriptor* field) const;
1164 
1165   // Returns a MapIterator referring to the theoretical element that would
1166   // follow the last element in the map field. It does not point to any
1167   // real element. Mutation to the field may invalidate the iterator.
1168   MapIterator MapEnd(Message* message, const FieldDescriptor* field) const;
1169 
1170   // Get the number of <key, value> pair of a map field. The result may be
1171   // different from FieldSize which can have duplicate keys.
1172   int MapSize(const Message& message, const FieldDescriptor* field) const;
1173 
1174   // Help method for MapIterator.
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& /* 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   // Returns the `_split_` pointer. Requires: IsSplit() == true.
1250   inline const void* GetSplitField(const Message* message) const;
1251   // Returns the address of the `_split_` pointer. Requires: IsSplit() == true.
1252   inline void** MutableSplitField(Message* message) const;
1253 
1254   // Allocate the split instance if needed.
1255   void PrepareSplitMessageForWrite(Message* message) const;
1256 
1257   // Shallow-swap fields listed in fields vector of two messages. It is the
1258   // caller's responsibility to make sure shallow swap is safe.
1259   void UnsafeShallowSwapFields(
1260       Message* message1, Message* message2,
1261       const std::vector<const FieldDescriptor*>& fields) const;
1262 
1263   // This function only swaps the field. Should swap corresponding has_bit
1264   // before or after using this function.
1265   void SwapField(Message* message1, Message* message2,
1266                  const FieldDescriptor* field) const;
1267 
1268   // Unsafe but shallow version of SwapField.
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   // Internal versions of EnumValue API perform no checking. Called after checks
1321   // by public methods.
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 // Abstract interface for a factory for message objects.
1352 //
1353 // The thread safety for this class is implementation dependent, see comments
1354 // around GetPrototype for details
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   // Given a Descriptor, gets or constructs the default (prototype) Message
1363   // of that type.  You can then call that message's New() method to construct
1364   // a mutable message of that type.
1365   //
1366   // Calling this method twice with the same Descriptor returns the same
1367   // object.  The returned object remains property of the factory.  Also, any
1368   // objects created by calling the prototype's New() method share some data
1369   // with the prototype, so these must be destroyed before the MessageFactory
1370   // is destroyed.
1371   //
1372   // The given descriptor must outlive the returned message, and hence must
1373   // outlive the MessageFactory.
1374   //
1375   // Some implementations do not support all types.  GetPrototype() will
1376   // return nullptr if the descriptor passed in is not supported.
1377   //
1378   // This method may or may not be thread-safe depending on the implementation.
1379   // Each implementation should document its own degree thread-safety.
1380   virtual const Message* GetPrototype(const Descriptor* type) = 0;
1381 
1382   // Gets a MessageFactory which supports all generated, compiled-in messages.
1383   // In other words, for any compiled-in type FooMessage, the following is true:
1384   //   MessageFactory::generated_factory()->GetPrototype(
1385   //     FooMessage::descriptor()) == FooMessage::default_instance()
1386   // This factory supports all types which are found in
1387   // DescriptorPool::generated_pool().  If given a descriptor from any other
1388   // pool, GetPrototype() will return nullptr.  (You can also check if a
1389   // descriptor is for a generated message by checking if
1390   // descriptor->file()->pool() == DescriptorPool::generated_pool().)
1391   //
1392   // This factory is 100% thread-safe; calling GetPrototype() does not modify
1393   // any shared data.
1394   //
1395   // This factory is a singleton.  The caller must not delete the object.
1396   static MessageFactory* generated_factory();
1397 
1398   // For internal use only:  Registers a .proto file at static initialization
1399   // time, to be placed in generated_factory.  The first time GetPrototype()
1400   // is called with a descriptor from this file, |register_messages| will be
1401   // called, with the file name as the parameter.  It must call
1402   // InternalRegisterGeneratedMessage() (below) to register each message type
1403   // in the file.  This strange mechanism is necessary because descriptors are
1404   // built lazily, so we can't register types by their descriptor until we
1405   // know that the descriptor exists.  |filename| must be a permanent string.
1406   static void InternalRegisterGeneratedFile(
1407       const google::protobuf::internal::DescriptorTable* table);
1408 
1409   // For internal use only:  Registers a message type.  Called only by the
1410   // functions which are registered with InternalRegisterGeneratedFile(),
1411   // above.
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 // Call this function to ensure that this message's reflection is linked into
1443 // the binary:
1444 //
1445 //   google::protobuf::LinkMessageReflection<pkg::FooMessage>();
1446 //
1447 // This will ensure that the following lookup will succeed:
1448 //
1449 //   DescriptorPool::generated_pool()->FindMessageTypeByName("pkg.FooMessage");
1450 //
1451 // As a side-effect, it will also guarantee that anything else from the same
1452 // .proto file will also be available for lookup in the generated pool.
1453 //
1454 // This function does not actually register the message, so it does not need
1455 // to be called before the lookup.  However it does need to occur in a function
1456 // that cannot be stripped from the binary (ie. it must be reachable from main).
1457 //
1458 // Best practice is to call this function as close as possible to where the
1459 // reflection is actually needed.  This function is very cheap to call, so you
1460 // should not need to worry about its runtime overhead except in the tightest
1461 // of loops (on x86-64 it compiles into two "mov" instructions).
1462 template <typename T>
1463 void LinkMessageReflection() {
1464   internal::StrongReferenceToType<T>();
1465 }
1466 
1467 // Specializations to handle cast to `Message`. We can check the `is_lite` bit
1468 // in the class data.
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 // Implementation details for {Get,Mutable}RawRepeatedPtrField.  We provide
1484 // specializations for <std::string>, <StringPieceField> and <Message> and
1485 // handle everything else with the default template which will match any type
1486 // having a method with signature "static const google::protobuf::Descriptor*
1487 // descriptor()". Such a type presumably is a descendant of google::protobuf::Message.
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 // In some cases, (Get|Mutable)Raw may be called with a type that is different
1563 // from the final type; e.g. char. As a defensive coding to this unfortunate
1564 // practices, we should only assume extra indirection (or a lack thereof) for
1565 // the well known, complex types.
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 }  // namespace internal
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   // Oneof fields are not split.
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 }  // namespace protobuf
1645 }  // namespace google
1646 
1647 #include "google/protobuf/port_undef.inc"
1648 
1649 #endif  // GOOGLE_PROTOBUF_MESSAGE_H__