• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc.  All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file or at
6 // https://developers.google.com/open-source/licenses/bsd
7 
8 // Authors: wink@google.com (Wink Saville),
9 //          kenton@google.com (Kenton Varda)
10 //  Based on original Protocol Buffers design by
11 //  Sanjay Ghemawat, Jeff Dean, and others.
12 //
13 // Defines MessageLite, the abstract interface implemented by all (lite
14 // and non-lite) protocol message objects.
15 
16 #ifndef GOOGLE_PROTOBUF_MESSAGE_LITE_H__
17 #define GOOGLE_PROTOBUF_MESSAGE_LITE_H__
18 
19 #include <climits>
20 #include <cstddef>
21 #include <cstdint>
22 #include <cstring>
23 #include <iosfwd>
24 #include <new>
25 #include <string>
26 #include <type_traits>
27 
28 #include "absl/base/attributes.h"
29 #include "absl/base/casts.h"
30 #include "absl/log/absl_check.h"
31 #include "absl/numeric/bits.h"
32 #include "absl/strings/cord.h"
33 #include "absl/strings/string_view.h"
34 #include "google/protobuf/arena.h"
35 #include "google/protobuf/explicitly_constructed.h"
36 #include "google/protobuf/internal_visibility.h"
37 #include "google/protobuf/io/coded_stream.h"
38 #include "google/protobuf/metadata_lite.h"
39 #include "google/protobuf/port.h"
40 
41 
42 // clang-format off
43 #include "google/protobuf/port_def.inc"
44 // clang-format on
45 
46 #ifdef SWIG
47 #error "You cannot SWIG proto headers"
48 #endif
49 
50 namespace google {
51 namespace protobuf {
52 
53 template <typename T>
54 class RepeatedPtrField;
55 
56 class FastReflectionMessageMutator;
57 class FastReflectionStringSetter;
58 class Reflection;
59 class Descriptor;
60 class AssignDescriptorsHelper;
61 class MessageLite;
62 
63 namespace io {
64 
65 class CodedInputStream;
66 class CodedOutputStream;
67 class ZeroCopyInputStream;
68 class ZeroCopyOutputStream;
69 
70 }  // namespace io
71 
72 namespace compiler {
73 namespace cpp {
74 class MessageTableTester;
75 }  // namespace cpp
76 }  // namespace compiler
77 
78 namespace internal {
79 
80 class MessageCreator {
81  public:
82   using Func = void* (*)(const void*, void*, Arena*);
83 
84   // Use -1/0/1 to be able to use <0, ==0, >0
85   enum Tag : int8_t {
86     kFunc = -1,
87     kZeroInit = 0,
88     kMemcpy = 1,
89   };
90 
MessageCreator()91   constexpr MessageCreator()
92       : allocation_size_(), tag_(), alignment_(), arena_bits_(uintptr_t{}) {}
93 
94   static constexpr MessageCreator ZeroInit(uint32_t allocation_size,
95                                            uint8_t alignment,
96                                            uintptr_t arena_bits = 0) {
97     MessageCreator out;
98     out.allocation_size_ = allocation_size;
99     out.tag_ = kZeroInit;
100     out.alignment_ = alignment;
101     out.arena_bits_ = arena_bits;
102     return out;
103   }
104   static constexpr MessageCreator CopyInit(uint32_t allocation_size,
105                                            uint8_t alignment,
106                                            uintptr_t arena_bits = 0) {
107     MessageCreator out;
108     out.allocation_size_ = allocation_size;
109     out.tag_ = kMemcpy;
110     out.alignment_ = alignment;
111     out.arena_bits_ = arena_bits;
112     return out;
113   }
MessageCreator(Func func,uint32_t allocation_size,uint8_t alignment)114   constexpr MessageCreator(Func func, uint32_t allocation_size,
115                            uint8_t alignment)
116       : allocation_size_(allocation_size),
117         tag_(kFunc),
118         alignment_(alignment),
119         func_(func) {}
120 
121   // Template for testing.
122   template <bool test_call = false, typename MessageLite>
123   MessageLite* New(const MessageLite* prototype_for_func,
124                    const MessageLite* prototype_for_copy, Arena* arena) const;
125 
126   template <bool test_call = false, typename MessageLite>
127   MessageLite* PlacementNew(const MessageLite* prototype_for_func,
128                             const MessageLite* prototype_for_copy, void* mem,
129                             Arena* arena) const;
130 
tag()131   Tag tag() const { return tag_; }
132 
allocation_size()133   uint32_t allocation_size() const { return allocation_size_; }
134 
alignment()135   uint8_t alignment() const { return alignment_; }
136 
arena_bits()137   uintptr_t arena_bits() const {
138     ABSL_DCHECK_NE(+tag(), +kFunc);
139     return arena_bits_;
140   }
141 
142  private:
143   uint32_t allocation_size_;
144   Tag tag_;
145   uint8_t alignment_;
146   union {
147     Func func_;
148     uintptr_t arena_bits_;
149   };
150 };
151 
152 // Allow easy change to regular int on platforms where the atomic might have a
153 // perf impact.
154 //
155 // CachedSize is like std::atomic<int> but with some important changes:
156 //
157 // 1) CachedSize uses Get / Set rather than load / store.
158 // 2) CachedSize always uses relaxed ordering.
159 // 3) CachedSize is assignable and copy-constructible.
160 // 4) CachedSize has a constexpr default constructor, and a constexpr
161 //    constructor that takes an int argument.
162 // 5) If the compiler supports the __atomic_load_n / __atomic_store_n builtins,
163 //    then CachedSize is trivially copyable.
164 //
165 // Developed at https://godbolt.org/z/vYcx7zYs1 ; supports gcc, clang, MSVC.
166 class PROTOBUF_EXPORT CachedSize {
167  private:
168   using Scalar = int;
169 
170  public:
CachedSize()171   constexpr CachedSize() noexcept : atom_(Scalar{}) {}
172   // NOLINTNEXTLINE(google-explicit-constructor)
CachedSize(Scalar desired)173   constexpr CachedSize(Scalar desired) noexcept : atom_(desired) {}
174 
175 #if PROTOBUF_BUILTIN_ATOMIC
176   constexpr CachedSize(const CachedSize& other) = default;
177 
Get()178   Scalar Get() const noexcept {
179     return __atomic_load_n(&atom_, __ATOMIC_RELAXED);
180   }
181 
Set(Scalar desired)182   void Set(Scalar desired) const noexcept {
183     // Avoid writing the value when it is zero. This prevents writing to global
184     // default instances, which might be in readonly memory.
185     if (ABSL_PREDICT_FALSE(desired == 0)) {
186       if (Get() == 0) return;
187     }
188     __atomic_store_n(&atom_, desired, __ATOMIC_RELAXED);
189   }
190 
SetNonZero(Scalar desired)191   void SetNonZero(Scalar desired) const noexcept {
192     __atomic_store_n(&atom_, desired, __ATOMIC_RELAXED);
193   }
194 #else
CachedSize(const CachedSize & other)195   CachedSize(const CachedSize& other) noexcept : atom_(other.Get()) {}
196   CachedSize& operator=(const CachedSize& other) noexcept {
197     Set(other.Get());
198     return *this;
199   }
200 
Get()201   Scalar Get() const noexcept {  //
202     return atom_.load(std::memory_order_relaxed);
203   }
204 
Set(Scalar desired)205   void Set(Scalar desired) const noexcept {
206     // Avoid writing the value when it is zero. This prevents writing to global
207     // default instances, which might be in readonly memory.
208     if (ABSL_PREDICT_FALSE(desired == 0)) {
209       if (Get() == 0) return;
210     }
211     atom_.store(desired, std::memory_order_relaxed);
212   }
213 
SetNonZero(Scalar desired)214   void SetNonZero(Scalar desired) const noexcept {
215     atom_.store(desired, std::memory_order_relaxed);
216   }
217 #endif
218 
219  private:
220 #if PROTOBUF_BUILTIN_ATOMIC
221   mutable Scalar atom_;
222 #else
223   mutable std::atomic<Scalar> atom_;
224 #endif
225 };
226 
227 // TODO: Upgrade to `auto` parameters when we drop C++14 support.
228 template <typename T, const T* kDefault>
229 struct GeneratedMessageTraitsT {
default_instanceGeneratedMessageTraitsT230   static constexpr const void* default_instance() { return kDefault; }
StrongPointerGeneratedMessageTraitsT231   static constexpr auto StrongPointer() { return default_instance(); }
232 };
233 
234 template <typename T>
235 struct FallbackMessageTraits {
default_instanceFallbackMessageTraits236   static const void* default_instance() { return T::default_instance(); }
237   // We can't make a constexpr pointer to the default, so use a function pointer
238   // instead.
StrongPointerFallbackMessageTraits239   static constexpr auto StrongPointer() { return &T::default_instance; }
240 };
241 
242 // Traits for message T.
243 // We use a class scope variable template, which can be specialized with a
244 // different type in a non-defining declaration.
245 // We need non-defining declarations because we might have duplicates of the
246 // same trait specification on each dependent coming from different .proto.h
247 // files.
248 struct MessageTraitsImpl {
249   template <typename T>
250   static FallbackMessageTraits<T> value;
251 };
252 template <typename T>
253 using MessageTraits = decltype(MessageTraitsImpl::value<T>);
254 
255 // For MessageLite to friend.
256 auto GetClassData(const MessageLite& msg);
257 
258 class SwapFieldHelper;
259 
260 // See parse_context.h for explanation
261 class ParseContext;
262 
263 struct DescriptorTable;
264 class DescriptorPoolExtensionFinder;
265 class ExtensionSet;
266 class LazyField;
267 class RepeatedPtrFieldBase;
268 class TcParser;
269 struct TcParseTableBase;
270 class WireFormatLite;
271 class WeakFieldMap;
272 class RustMapHelper;
273 
274 // We compute sizes as size_t but cache them as int.  This function converts a
275 // computed size to a cached size.  Since we don't proceed with serialization
276 // if the total size was > INT_MAX, it is not important what this function
277 // returns for inputs > INT_MAX.  However this case should not error or
278 // ABSL_CHECK-fail, because the full size_t resolution is still returned from
279 // ByteSizeLong() and checked against INT_MAX; we can catch the overflow
280 // there.
ToCachedSize(size_t size)281 inline int ToCachedSize(size_t size) { return static_cast<int>(size); }
282 
283 // We mainly calculate sizes in terms of size_t, but some functions that
284 // compute sizes return "int".  These int sizes are expected to always be
285 // positive. This function is more efficient than casting an int to size_t
286 // directly on 64-bit platforms because it avoids making the compiler emit a
287 // sign extending instruction, which we don't want and don't want to pay for.
FromIntSize(int size)288 inline size_t FromIntSize(int size) {
289   // Convert to unsigned before widening so sign extension is not necessary.
290   return static_cast<unsigned int>(size);
291 }
292 
293 // For cases where a legacy function returns an integer size.  We ABSL_DCHECK()
294 // that the conversion will fit within an integer; if this is false then we
295 // are losing information.
ToIntSize(size_t size)296 inline int ToIntSize(size_t size) {
297   ABSL_DCHECK_LE(size, static_cast<size_t>(INT_MAX));
298   return static_cast<int>(size);
299 }
300 
301 #if defined(PROTOBUF_FUTURE_STRING_VIEW_RETURN_TYPE)
302 using GetTypeNameReturnType = absl::string_view;
303 #else
304 using GetTypeNameReturnType = std::string;
305 #endif
306 
307 // Default empty string object. Don't use this directly. Instead, call
308 // GetEmptyString() to get the reference. This empty string is aligned with a
309 // minimum alignment of 8 bytes to match the requirement of ArenaStringPtr.
310 PROTOBUF_EXPORT extern ExplicitlyConstructedArenaString
311     fixed_address_empty_string;
312 
313 
GetEmptyStringAlreadyInited()314 PROTOBUF_EXPORT constexpr const std::string& GetEmptyStringAlreadyInited() {
315   return fixed_address_empty_string.get();
316 }
317 
318 PROTOBUF_EXPORT size_t StringSpaceUsedExcludingSelfLong(const std::string& str);
319 
320 struct ClassDataFull;
321 
322 // Note: The order of arguments in the functions is chosen so that it has
323 // the same ABI as the member function that calls them. Eg the `this`
324 // pointer becomes the first argument in the free function.
325 //
326 // Future work:
327 // We could save more data by omitting any optional pointer that would
328 // otherwise be null. We can have some metadata in ClassData telling us if we
329 // have them and their offset.
330 
331 struct PROTOBUF_EXPORT ClassData {
332   const MessageLite* prototype;
333   const internal::TcParseTableBase* tc_table;
334   void (*on_demand_register_arena_dtor)(MessageLite& msg, Arena& arena);
335   bool (*is_initialized)(const MessageLite&);
336   void (*merge_to_from)(MessageLite& to, const MessageLite& from_msg);
337   internal::MessageCreator message_creator;
338 #if defined(PROTOBUF_CUSTOM_VTABLE)
339   void (*destroy_message)(MessageLite& msg);
340   void (MessageLite::*clear)();
341   size_t (*byte_size_long)(const MessageLite&);
342   uint8_t* (*serialize)(const MessageLite& msg, uint8_t* ptr,
343                         io::EpsCopyOutputStream* stream);
344 #endif  // PROTOBUF_CUSTOM_VTABLE
345 
346   // Offset of the CachedSize member.
347   uint32_t cached_size_offset;
348   // LITE objects (ie !descriptor_methods) collocate their name as a
349   // char[] just beyond the ClassData.
350   bool is_lite;
351   bool is_dynamic = false;
352 
353   // In normal mode we have the small constructor to avoid the cost in
354   // codegen.
355 #if !defined(PROTOBUF_CUSTOM_VTABLE)
ClassDataClassData356   constexpr ClassData(
357       const MessageLite* prototype, const internal::TcParseTableBase* tc_table,
358       void (*on_demand_register_arena_dtor)(MessageLite&, Arena&),
359       bool (*is_initialized)(const MessageLite&),
360       void (*merge_to_from)(MessageLite& to, const MessageLite& from_msg),
361       internal::MessageCreator message_creator, uint32_t cached_size_offset,
362       bool is_lite
363       )
364       : prototype(prototype),
365         tc_table(tc_table),
366         on_demand_register_arena_dtor(on_demand_register_arena_dtor),
367         is_initialized(is_initialized),
368         merge_to_from(merge_to_from),
369         message_creator(message_creator),
370         cached_size_offset(cached_size_offset),
371         is_lite(is_lite)
372   {
373   }
374 #endif  // !PROTOBUF_CUSTOM_VTABLE
375 
376   // But we always provide the full constructor even in normal mode to make
377   // helper code simpler.
ClassDataClassData378   constexpr ClassData(
379       const MessageLite* prototype, const internal::TcParseTableBase* tc_table,
380       void (*on_demand_register_arena_dtor)(MessageLite&, Arena&),
381       bool (*is_initialized)(const MessageLite&),
382       void (*merge_to_from)(MessageLite& to, const MessageLite& from_msg),
383       internal::MessageCreator message_creator,
384       void (*destroy_message)(MessageLite& msg),  //
385       void (MessageLite::*clear)(),
386       size_t (*byte_size_long)(const MessageLite&),
387       uint8_t* (*serialize)(const MessageLite& msg, uint8_t* ptr,
388                             io::EpsCopyOutputStream* stream),
389       uint32_t cached_size_offset, bool is_lite
390       )
391       : prototype(prototype),
392         tc_table(tc_table),
393         on_demand_register_arena_dtor(on_demand_register_arena_dtor),
394         is_initialized(is_initialized),
395         merge_to_from(merge_to_from),
396         message_creator(message_creator),
397 #if defined(PROTOBUF_CUSTOM_VTABLE)
398         destroy_message(destroy_message),
399         clear(clear),
400         byte_size_long(byte_size_long),
401         serialize(serialize),
402 #endif  // PROTOBUF_CUSTOM_VTABLE
403         cached_size_offset(cached_size_offset),
404         is_lite(is_lite)
405   {
406   }
407 
408   const ClassDataFull& full() const;
409 
NewClassData410   MessageLite* New(Arena* arena) const {
411     return message_creator.New(prototype, prototype, arena);
412   }
413 
PlacementNewClassData414   MessageLite* PlacementNew(void* mem, Arena* arena) const {
415     return message_creator.PlacementNew(prototype, prototype, mem, arena);
416   }
417 
allocation_sizeClassData418   uint32_t allocation_size() const { return message_creator.allocation_size(); }
419 
alignmentClassData420   uint8_t alignment() const { return message_creator.alignment(); }
421 };
422 
423 template <size_t N>
424 struct ClassDataLite {
425   ClassData header;
426   const char type_name[N];
427 
baseClassDataLite428   constexpr const ClassData* base() const { return &header; }
429 };
430 
431 // We use a secondary vtable for descriptor based methods. This way ClassData
432 // does not grow with the number of descriptor methods. This avoids extra
433 // costs in MessageLite.
434 struct PROTOBUF_EXPORT DescriptorMethods {
435   absl::string_view (*get_type_name)(const ClassData* data);
436   std::string (*initialization_error_string)(const MessageLite&);
437   const internal::TcParseTableBase* (*get_tc_table)(const MessageLite&);
438   size_t (*space_used_long)(const MessageLite&);
439   std::string (*debug_string)(const MessageLite&);
440 };
441 
442 struct PROTOBUF_EXPORT ClassDataFull : ClassData {
ClassDataFullClassDataFull443   constexpr ClassDataFull(ClassData base,
444                           const DescriptorMethods* descriptor_methods,
445                           const internal::DescriptorTable* descriptor_table,
446                           void (*get_metadata_tracker)())
447       : ClassData(base),
448         descriptor_methods(descriptor_methods),
449         descriptor_table(descriptor_table),
450         reflection(),
451         descriptor(),
452         get_metadata_tracker(get_metadata_tracker) {}
453 
baseClassDataFull454   constexpr const ClassData* base() const { return this; }
455 
456   const DescriptorMethods* descriptor_methods;
457 
458   // Codegen types will provide a DescriptorTable to do lazy
459   // registration/initialization of the reflection objects.
460   // Other types, like DynamicMessage, keep the table as null but eagerly
461   // populate `reflection`/`descriptor` fields.
462   const internal::DescriptorTable* descriptor_table;
463   // Accesses are protected by the once_flag in `descriptor_table`. When the
464   // table is null these are populated from the beginning and need to
465   // protection.
466   mutable const Reflection* reflection;
467   mutable const Descriptor* descriptor;
468 
469   // When an access tracker is installed, this function notifies the tracker
470   // that GetMetadata was called.
471   void (*get_metadata_tracker)();
472 };
473 
full()474 inline const ClassDataFull& ClassData::full() const {
475   ABSL_DCHECK(!is_lite);
476   return *static_cast<const ClassDataFull*>(this);
477 }
478 
479 }  // namespace internal
480 
481 // Interface to light weight protocol messages.
482 //
483 // This interface is implemented by all protocol message objects.  Non-lite
484 // messages additionally implement the Message interface, which is a
485 // subclass of MessageLite.  Use MessageLite instead when you only need
486 // the subset of features which it supports -- namely, nothing that uses
487 // descriptors or reflection.  You can instruct the protocol compiler
488 // to generate classes which implement only MessageLite, not the full
489 // Message interface, by adding the following line to the .proto file:
490 //
491 //   option optimize_for = LITE_RUNTIME;
492 //
493 // This is particularly useful on resource-constrained systems where
494 // the full protocol buffers runtime library is too big.
495 //
496 // Note that on non-constrained systems (e.g. servers) when you need
497 // to link in lots of protocol definitions, a better way to reduce
498 // total code footprint is to use optimize_for = CODE_SIZE.  This
499 // will make the generated code smaller while still supporting all the
500 // same features (at the expense of speed).  optimize_for = LITE_RUNTIME
501 // is best when you only have a small number of message types linked
502 // into your binary, in which case the size of the protocol buffers
503 // runtime itself is the biggest problem.
504 //
505 // Users must not derive from this class. Only the protocol compiler and
506 // the internal library are allowed to create subclasses.
507 class PROTOBUF_EXPORT MessageLite {
508  public:
509   MessageLite(const MessageLite&) = delete;
510   MessageLite& operator=(const MessageLite&) = delete;
511   PROTOBUF_VIRTUAL ~MessageLite() = default;
512 
513   // Basic Operations ------------------------------------------------
514 
515   // Get the name of this message type, e.g. "foo.bar.BazProto".
516   internal::GetTypeNameReturnType GetTypeName() const;
517 
518   // Construct a new instance of the same type.  Ownership is passed to the
519   // caller.
New()520   MessageLite* New() const { return New(nullptr); }
521 
522   // Construct a new instance on the arena. Ownership is passed to the caller
523   // if arena is a nullptr.
524   MessageLite* New(Arena* arena) const;
525 
526   // Returns the arena, if any, that directly owns this message and its internal
527   // memory (Arena::Own is different in that the arena doesn't directly own the
528   // internal memory). This method is used in proto's implementation for
529   // swapping, moving and setting allocated, for deciding whether the ownership
530   // of this message or its internal memory could be changed.
GetArena()531   Arena* GetArena() const { return _internal_metadata_.arena(); }
532 
533   // Clear all fields of the message and set them to their default values.
534   // Clear() assumes that any memory allocated to hold parts of the message
535   // will likely be needed again, so the memory used may not be freed.
536   // To ensure that all memory used by a Message is freed, you must delete it.
537 #if defined(PROTOBUF_CUSTOM_VTABLE)
Clear()538   void Clear() { (this->*_class_data_->clear)(); }
539 #else
540   virtual void Clear() = 0;
541 #endif  // PROTOBUF_CUSTOM_VTABLE
542 
543   // Quickly check if all required fields have values set.
544   bool IsInitialized() const;
545 
546   // This is not implemented for Lite messages -- it just returns "(cannot
547   // determine missing fields for lite message)".  However, it is implemented
548   // for full messages.  See message.h.
549   std::string InitializationErrorString() const;
550 
551   // If |other| is the exact same class as this, calls MergeFrom(). Otherwise,
552   // results are undefined (probably crash).
553   void CheckTypeAndMergeFrom(const MessageLite& other);
554 
555   // These methods return a human-readable summary of the message. Note that
556   // since the MessageLite interface does not support reflection, there is very
557   // little information that these methods can provide. They are shadowed by
558   // methods of the same name on the Message interface which provide much more
559   // information. The methods here are intended primarily to facilitate code
560   // reuse for logic that needs to interoperate with both full and lite protos.
561   //
562   // The format of the returned string is subject to change, so please do not
563   // assume it will remain stable over time.
564   std::string DebugString() const;
ShortDebugString()565   std::string ShortDebugString() const { return DebugString(); }
566   // MessageLite::DebugString is already Utf8 Safe. This is to add compatibility
567   // with Message.
Utf8DebugString()568   std::string Utf8DebugString() const { return DebugString(); }
569 
570   // Implementation of the `AbslStringify` interface. This adds `DebugString()`
571   // to the sink. Do not rely on exact format.
572   template <typename Sink>
AbslStringify(Sink & sink,const google::protobuf::MessageLite & msg)573   friend void AbslStringify(Sink& sink, const google::protobuf::MessageLite& msg) {
574     sink.Append(msg.DebugString());
575   }
576 
577   // Parsing ---------------------------------------------------------
578   // Methods for parsing in protocol buffer format.  Most of these are
579   // just simple wrappers around MergeFromCodedStream().  Clear() will be
580   // called before merging the input.
581 
582   // Fill the message with a protocol buffer parsed from the given input
583   // stream. Returns false on a read error or if the input is in the wrong
584   // format.  A successful return does not indicate the entire input is
585   // consumed, ensure you call ConsumedEntireMessage() to check that if
586   // applicable.
587   ABSL_ATTRIBUTE_REINITIALIZES bool ParseFromCodedStream(
588       io::CodedInputStream* input);
589   // Like ParseFromCodedStream(), but accepts messages that are missing
590   // required fields.
591   ABSL_ATTRIBUTE_REINITIALIZES bool ParsePartialFromCodedStream(
592       io::CodedInputStream* input);
593   // Read a protocol buffer from the given zero-copy input stream.  If
594   // successful, the entire input will be consumed.
595   ABSL_ATTRIBUTE_REINITIALIZES bool ParseFromZeroCopyStream(
596       io::ZeroCopyInputStream* input);
597   // Like ParseFromZeroCopyStream(), but accepts messages that are missing
598   // required fields.
599   ABSL_ATTRIBUTE_REINITIALIZES bool ParsePartialFromZeroCopyStream(
600       io::ZeroCopyInputStream* input);
601   // Parse a protocol buffer from a file descriptor.  If successful, the entire
602   // input will be consumed.
603   ABSL_ATTRIBUTE_REINITIALIZES bool ParseFromFileDescriptor(
604       int file_descriptor);
605   // Like ParseFromFileDescriptor(), but accepts messages that are missing
606   // required fields.
607   ABSL_ATTRIBUTE_REINITIALIZES bool ParsePartialFromFileDescriptor(
608       int file_descriptor);
609   // Parse a protocol buffer from a C++ istream.  If successful, the entire
610   // input will be consumed.
611   ABSL_ATTRIBUTE_REINITIALIZES bool ParseFromIstream(std::istream* input);
612   // Like ParseFromIstream(), but accepts messages that are missing
613   // required fields.
614   ABSL_ATTRIBUTE_REINITIALIZES bool ParsePartialFromIstream(
615       std::istream* input);
616   // Read a protocol buffer from the given zero-copy input stream, expecting
617   // the message to be exactly "size" bytes long.  If successful, exactly
618   // this many bytes will have been consumed from the input.
619   bool MergePartialFromBoundedZeroCopyStream(io::ZeroCopyInputStream* input,
620                                              int size);
621   // Like ParseFromBoundedZeroCopyStream(), but accepts messages that are
622   // missing required fields.
623   bool MergeFromBoundedZeroCopyStream(io::ZeroCopyInputStream* input, int size);
624   ABSL_ATTRIBUTE_REINITIALIZES bool ParseFromBoundedZeroCopyStream(
625       io::ZeroCopyInputStream* input, int size);
626   // Like ParseFromBoundedZeroCopyStream(), but accepts messages that are
627   // missing required fields.
628   ABSL_ATTRIBUTE_REINITIALIZES bool ParsePartialFromBoundedZeroCopyStream(
629       io::ZeroCopyInputStream* input, int size);
630   // Parses a protocol buffer contained in a string. Returns true on success.
631   // This function takes a string in the (non-human-readable) binary wire
632   // format, matching the encoding output by MessageLite::SerializeToString().
633   // If you'd like to convert a human-readable string into a protocol buffer
634   // object, see google::protobuf::TextFormat::ParseFromString().
635   ABSL_ATTRIBUTE_REINITIALIZES bool ParseFromString(absl::string_view data);
636   // Like ParseFromString(), but accepts messages that are missing
637   // required fields.
638   ABSL_ATTRIBUTE_REINITIALIZES bool ParsePartialFromString(
639       absl::string_view data);
640   // Parse a protocol buffer contained in an array of bytes.
641   ABSL_ATTRIBUTE_REINITIALIZES bool ParseFromArray(const void* data, int size);
642   // Like ParseFromArray(), but accepts messages that are missing
643   // required fields.
644   ABSL_ATTRIBUTE_REINITIALIZES bool ParsePartialFromArray(const void* data,
645                                                           int size);
646 
647 
648   // Reads a protocol buffer from the stream and merges it into this
649   // Message.  Singular fields read from the what is
650   // already in the Message and repeated fields are appended to those
651   // already present.
652   //
653   // It is the responsibility of the caller to call input->LastTagWas()
654   // (for groups) or input->ConsumedEntireMessage() (for non-groups) after
655   // this returns to verify that the message's end was delimited correctly.
656   //
657   // ParseFromCodedStream() is implemented as Clear() followed by
658   // MergeFromCodedStream().
659   bool MergeFromCodedStream(io::CodedInputStream* input);
660 
661   // Like MergeFromCodedStream(), but succeeds even if required fields are
662   // missing in the input.
663   //
664   // MergeFromCodedStream() is just implemented as MergePartialFromCodedStream()
665   // followed by IsInitialized().
666   bool MergePartialFromCodedStream(io::CodedInputStream* input);
667 
668   // Merge a protocol buffer contained in a string.
669   bool MergeFromString(absl::string_view data);
670 
671 
672   // Serialization ---------------------------------------------------
673   // Methods for serializing in protocol buffer format.  Most of these
674   // are just simple wrappers around ByteSize() and SerializeWithCachedSizes().
675 
676   // Write a protocol buffer of this message to the given output.  Returns
677   // false on a write error.  If the message is missing required fields,
678   // this may ABSL_CHECK-fail.
679   bool SerializeToCodedStream(io::CodedOutputStream* output) const;
680   // Like SerializeToCodedStream(), but allows missing required fields.
681   bool SerializePartialToCodedStream(io::CodedOutputStream* output) const;
682   // Write the message to the given zero-copy output stream.  All required
683   // fields must be set.
684   bool SerializeToZeroCopyStream(io::ZeroCopyOutputStream* output) const;
685   // Like SerializeToZeroCopyStream(), but allows missing required fields.
686   bool SerializePartialToZeroCopyStream(io::ZeroCopyOutputStream* output) const;
687   // Serialize the message and store it in the given string.  All required
688   // fields must be set.
689   bool SerializeToString(std::string* output) const;
690   // Like SerializeToString(), but allows missing required fields.
691   bool SerializePartialToString(std::string* output) const;
692   // Serialize the message and store it in the given byte array.  All required
693   // fields must be set.
694   bool SerializeToArray(void* data, int size) const;
695   // Like SerializeToArray(), but allows missing required fields.
696   bool SerializePartialToArray(void* data, int size) const;
697 
698   // Make a string encoding the message. Is equivalent to calling
699   // SerializeToString() on a string and using that.  Returns the empty
700   // string if SerializeToString() would have returned an error.
701   // Note: If you intend to generate many such strings, you may
702   // reduce heap fragmentation by instead re-using the same string
703   // object with calls to SerializeToString().
704   std::string SerializeAsString() const;
705   // Like SerializeAsString(), but allows missing required fields.
706   std::string SerializePartialAsString() const;
707 
708   // Serialize the message and write it to the given file descriptor.  All
709   // required fields must be set.
710   bool SerializeToFileDescriptor(int file_descriptor) const;
711   // Like SerializeToFileDescriptor(), but allows missing required fields.
712   bool SerializePartialToFileDescriptor(int file_descriptor) const;
713   // Serialize the message and write it to the given C++ ostream.  All
714   // required fields must be set.
715   bool SerializeToOstream(std::ostream* output) const;
716   // Like SerializeToOstream(), but allows missing required fields.
717   bool SerializePartialToOstream(std::ostream* output) const;
718 
719   // Like SerializeToString(), but appends to the data to the string's
720   // existing contents.  All required fields must be set.
721   bool AppendToString(std::string* output) const;
722   // Like AppendToString(), but allows missing required fields.
723   bool AppendPartialToString(std::string* output) const;
724 
725   // Reads a protocol buffer from a Cord and merges it into this message.
726   bool MergeFromCord(const absl::Cord& cord);
727   // Like MergeFromCord(), but accepts messages that are missing
728   // required fields.
729   bool MergePartialFromCord(const absl::Cord& cord);
730   // Parse a protocol buffer contained in a Cord.
731   ABSL_ATTRIBUTE_REINITIALIZES bool ParseFromCord(const absl::Cord& cord);
732   // Like ParseFromCord(), but accepts messages that are missing
733   // required fields.
734   ABSL_ATTRIBUTE_REINITIALIZES bool ParsePartialFromCord(
735       const absl::Cord& cord);
736 
737   // Serialize the message and store it in the given Cord.  All required
738   // fields must be set.
739   bool SerializeToCord(absl::Cord* output) const;
740   // Like SerializeToCord(), but allows missing required fields.
741   bool SerializePartialToCord(absl::Cord* output) const;
742 
743   // Make a Cord encoding the message. Is equivalent to calling
744   // SerializeToCord() on a Cord and using that.  Returns an empty
745   // Cord if SerializeToCord() would have returned an error.
746   absl::Cord SerializeAsCord() const;
747   // Like SerializeAsCord(), but allows missing required fields.
748   absl::Cord SerializePartialAsCord() const;
749 
750   // Like SerializeToCord(), but appends to the data to the Cord's existing
751   // contents.  All required fields must be set.
752   bool AppendToCord(absl::Cord* output) const;
753   // Like AppendToCord(), but allows missing required fields.
754   bool AppendPartialToCord(absl::Cord* output) const;
755 
756   // Computes the serialized size of the message.  This recursively calls
757   // ByteSizeLong() on all embedded messages.
758   //
759   // ByteSizeLong() is generally linear in the number of fields defined for the
760   // proto.
761 #if defined(PROTOBUF_CUSTOM_VTABLE)
ByteSizeLong()762   size_t ByteSizeLong() const { return _class_data_->byte_size_long(*this); }
763 #else
764   virtual size_t ByteSizeLong() const = 0;
765 #endif  // PROTOBUF_CUSTOM_VTABLE
766 
767   // Legacy ByteSize() API.
ByteSize()768   [[deprecated("Please use ByteSizeLong() instead")]] int ByteSize() const {
769     return internal::ToIntSize(ByteSizeLong());
770   }
771 
772   // Serializes the message without recomputing the size.  The message must not
773   // have changed since the last call to ByteSize(), and the value returned by
774   // ByteSize must be non-negative.  Otherwise the results are undefined.
SerializeWithCachedSizes(io::CodedOutputStream * output)775   void SerializeWithCachedSizes(io::CodedOutputStream* output) const {
776     output->SetCur(_InternalSerialize(output->Cur(), output->EpsCopy()));
777   }
778 
779   // Functions below here are not part of the public interface.  It isn't
780   // enforced, but they should be treated as private, and will be private
781   // at some future time.  Unfortunately the implementation of the "friend"
782   // keyword in GCC is broken at the moment, but we expect it will be fixed.
783 
784   // Like SerializeWithCachedSizes, but writes directly to *target, returning
785   // a pointer to the byte immediately after the last byte written.  "target"
786   // must point at a byte array of at least ByteSize() bytes.  Whether to use
787   // deterministic serialization, e.g., maps in sorted order, is determined by
788   // CodedOutputStream::IsDefaultSerializationDeterministic().
789   uint8_t* SerializeWithCachedSizesToArray(uint8_t* target) const;
790 
791   // Returns the result of the last call to ByteSize().  An embedded message's
792   // size is needed both to serialize it (only true for length-prefixed
793   // submessages) and to compute the outer message's size.  Caching
794   // the size avoids computing it multiple times.
795   // Note that the submessage size is unnecessary when using
796   // group encoding / delimited since we have SGROUP/EGROUP bounds.
797   //
798   // ByteSize() does not automatically use the cached size when available
799   // because this would require invalidating it every time the message was
800   // modified, which would be too hard and expensive.  (E.g. if a deeply-nested
801   // sub-message is changed, all of its parents' cached sizes would need to be
802   // invalidated, which is too much work for an otherwise inlined setter
803   // method.)
804 #if defined(PROTOBUF_CUSTOM_VTABLE)
GetCachedSize()805   int GetCachedSize() const { return AccessCachedSize().Get(); }
806 #else
807   int GetCachedSize() const;
808 #endif
809 
810   const char* _InternalParse(const char* ptr, internal::ParseContext* ctx);
811 
812   void OnDemandRegisterArenaDtor(Arena* arena);
813 
814  protected:
815   // Message implementations require access to internally visible API.
internal_visibility()816   static constexpr internal::InternalVisibility internal_visibility() {
817     return internal::InternalVisibility{};
818   }
819 
820   template <typename T>
DefaultConstruct(Arena * arena)821   PROTOBUF_ALWAYS_INLINE static T* DefaultConstruct(Arena* arena) {
822     return static_cast<T*>(Arena::DefaultConstruct<T>(arena));
823   }
824 
825   template <typename T>
NewImpl(const void *,void * mem,Arena * arena)826   static void* NewImpl(const void*, void* mem, Arena* arena) {
827     return ::new (mem) T(arena);
828   }
829   template <typename T>
GetNewImpl()830   static constexpr internal::MessageCreator GetNewImpl() {
831 #if defined(__cpp_if_constexpr)
832     if constexpr (internal::EnableCustomNewFor<T>()) {
833 #else
834     // Equally valid code, but might be more work for the compiler
835     if (internal::EnableCustomNewFor<T>()) {
836 #endif
837       return T::InternalNewImpl_();
838     } else {
839       return internal::MessageCreator(&T::PlacementNew_, sizeof(T), alignof(T));
840     }
841   }
842 
843 #if defined(PROTOBUF_CUSTOM_VTABLE)
844   template <typename T>
845   static constexpr auto GetClearImpl() {
846     return static_cast<void (MessageLite::*)()>(&T::Clear);
847   }
848 #else   // PROTOBUF_CUSTOM_VTABLE
849   // When custom vtables are off we avoid instantiating the functions because we
850   // will not use them anyway. Less work for the compiler.
851   template <typename T>
852   using GetClearImpl = std::nullptr_t;
853 #endif  // PROTOBUF_CUSTOM_VTABLE
854 
855   template <typename T>
856   PROTOBUF_ALWAYS_INLINE static T* CopyConstruct(Arena* arena, const T& from) {
857     return static_cast<T*>(Arena::CopyConstruct<T>(arena, &from));
858   }
859 
860   const internal::TcParseTableBase* GetTcParseTable() const {
861     auto* data = GetClassData();
862     ABSL_DCHECK(data != nullptr);
863 
864     auto* tc_table = data->tc_table;
865     if (ABSL_PREDICT_FALSE(tc_table == nullptr)) {
866       ABSL_DCHECK(!data->is_lite);
867       return data->full().descriptor_methods->get_tc_table(*this);
868     }
869     return tc_table;
870   }
871 
872 #if defined(PROTOBUF_CUSTOM_VTABLE)
873   explicit constexpr MessageLite(const internal::ClassData* data)
874       : _class_data_(data) {}
875   explicit MessageLite(Arena* arena, const internal::ClassData* data)
876       : _internal_metadata_(arena), _class_data_(data) {}
877 #else   // PROTOBUF_CUSTOM_VTABLE
878   constexpr MessageLite() {}
879   explicit MessageLite(Arena* arena) : _internal_metadata_(arena) {}
880   explicit constexpr MessageLite(const internal::ClassData*) {}
881   explicit MessageLite(Arena* arena, const internal::ClassData*)
882       : _internal_metadata_(arena) {}
883 #endif  // PROTOBUF_CUSTOM_VTABLE
884 
885   // GetClassData() returns a pointer to a ClassData struct which
886   // exists in global memory and is unique to each subclass.  This uniqueness
887   // property is used in order to quickly determine whether two messages are
888   // of the same type.
889   //
890   // This is a work in progress. There are still some types (eg MapEntry) that
891   // return a default table instead of a unique one.
892 #if defined(PROTOBUF_CUSTOM_VTABLE)
893   const internal::ClassData* GetClassData() const {
894     ::absl::PrefetchToLocalCache(_class_data_);
895     return _class_data_;
896   }
897 #else   // PROTOBUF_CUSTOM_VTABLE
898   virtual const internal::ClassData* GetClassData() const = 0;
899 #endif  // PROTOBUF_CUSTOM_VTABLE
900 
901   template <typename T>
902   static auto GetClassDataGenerated() {
903     static_assert(std::is_base_of<MessageLite, T>::value, "");
904     // We could speed this up if needed by avoiding the function call.
905     // In LTO this is likely inlined, so it might not matter.
906     static_assert(
907         std::is_same<const T&, decltype(T::default_instance())>::value, "");
908     return T::default_instance().T::GetClassData();
909   }
910 
911   internal::InternalMetadata _internal_metadata_;
912 #if defined(PROTOBUF_CUSTOM_VTABLE)
913   const internal::ClassData* _class_data_;
914 #endif  // PROTOBUF_CUSTOM_VTABLE
915 
916   // Return the cached size object as described by
917   // ClassData::cached_size_offset.
918   const internal::CachedSize& AccessCachedSize() const {
919     return *reinterpret_cast<const internal::CachedSize*>(
920         reinterpret_cast<const char*>(this) +
921         GetClassData()->cached_size_offset);
922   }
923 
924  public:
925   enum ParseFlags {
926     // Merge vs. Parse:
927     // Merge: overwrites scalar fields but appends to repeated fields in the
928     //        destination; other fields in the destination remain untouched.
929     // Parse: clears all fields in the destination before calling Merge.
930     kMerge = 0,
931     kParse = 1,
932     // Default behaviour vs. Partial:
933     // Default: a missing required field is deemed as parsing failure.
934     // Partial: parse or merge will not give an error if input is missing
935     //          required fields.
936     kMergePartial = 2,
937     kParsePartial = 3,
938     // Default behaviour vs. Aliasing:
939     // Default:  when merging, pointer is followed and expanded (deep-copy).
940     // Aliasing: when merging, the destination message is allowed to retain
941     //           pointers to the original structure (shallow-copy). This mostly
942     //           is intended for use with STRING_PIECE.
943     // NOTE: STRING_PIECE is not recommended for new usage. Prefer Cords.
944     kMergeWithAliasing = 4,
945     kParseWithAliasing = 5,
946     kMergePartialWithAliasing = 6,
947     kParsePartialWithAliasing = 7
948   };
949 
950   template <ParseFlags flags, typename T>
951   bool ParseFrom(const T& input);
952 
953   // Fast path when conditions match (ie. non-deterministic)
954   //  uint8_t* _InternalSerialize(uint8_t* ptr) const;
955 #if defined(PROTOBUF_CUSTOM_VTABLE)
956   uint8_t* _InternalSerialize(uint8_t* ptr,
957                               io::EpsCopyOutputStream* stream) const {
958     return _class_data_->serialize(*this, ptr, stream);
959   }
960 #else   // PROTOBUF_CUSTOM_VTABLE
961   virtual uint8_t* _InternalSerialize(
962       uint8_t* ptr, io::EpsCopyOutputStream* stream) const = 0;
963 #endif  // PROTOBUF_CUSTOM_VTABLE
964 
965   // Identical to IsInitialized() except that it logs an error message.
966   bool IsInitializedWithErrors() const {
967     if (IsInitialized()) return true;
968     LogInitializationErrorMessage();
969     return false;
970   }
971 
972 #if defined(PROTOBUF_CUSTOM_VTABLE)
973   void operator delete(MessageLite* msg, std::destroying_delete_t) {
974     msg->DeleteInstance();
975   }
976 #endif
977 
978  private:
979   friend class FastReflectionMessageMutator;
980   friend class AssignDescriptorsHelper;
981   friend class FastReflectionStringSetter;
982   friend class Message;
983   friend class Reflection;
984   friend class TypeId;
985   friend class compiler::cpp::MessageTableTester;
986   friend class internal::DescriptorPoolExtensionFinder;
987   friend class internal::ExtensionSet;
988   friend class internal::LazyField;
989   friend class internal::SwapFieldHelper;
990   friend class internal::TcParser;
991   friend struct internal::TcParseTableBase;
992   friend class internal::UntypedMapBase;
993   friend class internal::WeakFieldMap;
994   friend class internal::WireFormatLite;
995   friend class internal::RustMapHelper;
996   friend internal::MessageCreator;
997 
998   template <typename Type>
999   friend class Arena::InternalHelper;
1000 
1001   friend auto internal::GetClassData(const MessageLite& msg);
1002 
1003   void LogInitializationErrorMessage() const;
1004 
1005   bool MergeFromImpl(io::CodedInputStream* input, ParseFlags parse_flags);
1006 
1007   // Runs the destructor for this instance.
1008   void DestroyInstance();
1009   // Runs the destructor for this instance and deletes the memory via
1010   // `operator delete`
1011   void DeleteInstance();
1012 
1013   // For tests that need to inspect private _oneof_case_. It is the callers
1014   // responsibility to ensure T has the right member.
1015   template <typename T>
1016   static uint32_t GetOneofCaseOffsetForTesting() {
1017     return offsetof(T, _impl_._oneof_case_);
1018   }
1019 };
1020 
1021 // A `std::type_info` equivalent for protobuf message types.
1022 // This class is preferred over using `typeid` for a few reasons:
1023 //  - It works with RTTI disabled.
1024 //  - It works for `DynamicMessage` types.
1025 //  - It works in custom vtable mode.
1026 //
1027 // Usage:
1028 //  - Instead of `typeid(Type)` use `TypeId::Get<Type>()`
1029 //  - Instead of `typeid(expr)` use `TypeId::Get(expr)`
1030 //
1031 // Supports all relationals including <=>, and supports hashing via
1032 // `absl::Hash`.
1033 class TypeId {
1034  public:
Get(const MessageLite & msg)1035   static TypeId Get(const MessageLite& msg) {
1036     return TypeId(msg.GetClassData());
1037   }
1038 
1039   template <typename T>
Get()1040   static TypeId Get() {
1041     return TypeId(MessageLite::GetClassDataGenerated<T>());
1042   }
1043 
1044   // Name of the message type.
1045   // Equivalent to `.GetTypeName()` on the message.
1046   absl::string_view name() const;
1047 
1048   friend constexpr bool operator==(TypeId a, TypeId b) {
1049     return a.data_ == b.data_;
1050   }
1051   friend constexpr bool operator!=(TypeId a, TypeId b) { return !(a == b); }
1052   friend constexpr bool operator<(TypeId a, TypeId b) {
1053     return a.data_ < b.data_;
1054   }
1055   friend constexpr bool operator>(TypeId a, TypeId b) {
1056     return a.data_ > b.data_;
1057   }
1058   friend constexpr bool operator<=(TypeId a, TypeId b) {
1059     return a.data_ <= b.data_;
1060   }
1061   friend constexpr bool operator>=(TypeId a, TypeId b) {
1062     return a.data_ >= b.data_;
1063   }
1064 
1065 #if defined(__cpp_impl_three_way_comparison) && \
1066     __cpp_impl_three_way_comparison >= 201907L
1067   friend constexpr auto operator<=>(TypeId a, TypeId b) {
1068     return a.data_ <=> b.data_;
1069   }
1070 #endif
1071 
1072   template <typename H>
AbslHashValue(H state,TypeId id)1073   friend H AbslHashValue(H state, TypeId id) {
1074     return H::combine(std::move(state), id.data_);
1075   }
1076 
1077  private:
TypeId(const internal::ClassData * data)1078   constexpr explicit TypeId(const internal::ClassData* data) : data_(data) {}
1079 
1080   const internal::ClassData* data_;
1081 };
1082 
1083 namespace internal {
1084 
GetClassData(const MessageLite & msg)1085 inline auto GetClassData(const MessageLite& msg) { return msg.GetClassData(); }
1086 
1087 template <bool alias>
1088 bool MergeFromImpl(absl::string_view input, MessageLite* msg,
1089                    const internal::TcParseTableBase* tc_table,
1090                    MessageLite::ParseFlags parse_flags);
1091 extern template PROTOBUF_EXPORT_TEMPLATE_DECLARE bool MergeFromImpl<false>(
1092     absl::string_view input, MessageLite* msg,
1093     const internal::TcParseTableBase* tc_table,
1094     MessageLite::ParseFlags parse_flags);
1095 extern template PROTOBUF_EXPORT_TEMPLATE_DECLARE bool MergeFromImpl<true>(
1096     absl::string_view input, MessageLite* msg,
1097     const internal::TcParseTableBase* tc_table,
1098     MessageLite::ParseFlags parse_flags);
1099 
1100 template <bool alias>
1101 bool MergeFromImpl(io::ZeroCopyInputStream* input, MessageLite* msg,
1102                    const internal::TcParseTableBase* tc_table,
1103                    MessageLite::ParseFlags parse_flags);
1104 extern template PROTOBUF_EXPORT_TEMPLATE_DECLARE bool MergeFromImpl<false>(
1105     io::ZeroCopyInputStream* input, MessageLite* msg,
1106     const internal::TcParseTableBase* tc_table,
1107     MessageLite::ParseFlags parse_flags);
1108 extern template PROTOBUF_EXPORT_TEMPLATE_DECLARE bool MergeFromImpl<true>(
1109     io::ZeroCopyInputStream* input, MessageLite* msg,
1110     const internal::TcParseTableBase* tc_table,
1111     MessageLite::ParseFlags parse_flags);
1112 
1113 struct BoundedZCIS {
1114   io::ZeroCopyInputStream* zcis;
1115   int limit;
1116 };
1117 
1118 template <bool alias>
1119 bool MergeFromImpl(BoundedZCIS input, MessageLite* msg,
1120                    const internal::TcParseTableBase* tc_table,
1121                    MessageLite::ParseFlags parse_flags);
1122 extern template PROTOBUF_EXPORT_TEMPLATE_DECLARE bool MergeFromImpl<false>(
1123     BoundedZCIS input, MessageLite* msg,
1124     const internal::TcParseTableBase* tc_table,
1125     MessageLite::ParseFlags parse_flags);
1126 extern template PROTOBUF_EXPORT_TEMPLATE_DECLARE bool MergeFromImpl<true>(
1127     BoundedZCIS input, MessageLite* msg,
1128     const internal::TcParseTableBase* tc_table,
1129     MessageLite::ParseFlags parse_flags);
1130 
1131 template <typename T>
1132 struct SourceWrapper;
1133 
1134 template <bool alias, typename T>
MergeFromImpl(const SourceWrapper<T> & input,MessageLite * msg,const internal::TcParseTableBase * tc_table,MessageLite::ParseFlags parse_flags)1135 bool MergeFromImpl(const SourceWrapper<T>& input, MessageLite* msg,
1136                    const internal::TcParseTableBase* tc_table,
1137                    MessageLite::ParseFlags parse_flags) {
1138   return input.template MergeInto<alias>(msg, tc_table, parse_flags);
1139 }
1140 
1141 }  // namespace internal
1142 
1143 template <MessageLite::ParseFlags flags, typename T>
ParseFrom(const T & input)1144 bool MessageLite::ParseFrom(const T& input) {
1145   if (flags & kParse) Clear();
1146   constexpr bool alias = (flags & kMergeWithAliasing) != 0;
1147   const internal::TcParseTableBase* tc_table;
1148   PROTOBUF_ALWAYS_INLINE_CALL tc_table = GetTcParseTable();
1149   return internal::MergeFromImpl<alias>(input, this, tc_table, flags);
1150 }
1151 
1152 // ===================================================================
1153 // Shutdown support.
1154 
1155 
1156 // Shut down the entire protocol buffers library, deleting all static-duration
1157 // objects allocated by the library or by generated .pb.cc files.
1158 //
1159 // There are two reasons you might want to call this:
1160 // * You use a draconian definition of "memory leak" in which you expect
1161 //   every single malloc() to have a corresponding free(), even for objects
1162 //   which live until program exit.
1163 // * You are writing a dynamically-loaded library which needs to clean up
1164 //   after itself when the library is unloaded.
1165 //
1166 // It is safe to call this multiple times.  However, it is not safe to use
1167 // any other part of the protocol buffers library after
1168 // ShutdownProtobufLibrary() has been called. Furthermore this call is not
1169 // thread safe, user needs to synchronize multiple calls.
1170 PROTOBUF_EXPORT void ShutdownProtobufLibrary();
1171 
1172 namespace internal {
1173 
1174 // Register a function to be called when ShutdownProtocolBuffers() is called.
1175 PROTOBUF_EXPORT void OnShutdown(void (*func)());
1176 // Run an arbitrary function on an arg
1177 PROTOBUF_EXPORT void OnShutdownRun(void (*f)(const void*), const void* arg);
1178 
1179 template <typename T>
OnShutdownDelete(T * p)1180 T* OnShutdownDelete(T* p) {
1181   OnShutdownRun([](const void* pp) { delete static_cast<const T*>(pp); }, p);
1182   return p;
1183 }
1184 
AssertDownCast(const MessageLite & from,const MessageLite & to)1185 inline void AssertDownCast(const MessageLite& from, const MessageLite& to) {
1186   ABSL_DCHECK(TypeId::Get(from) == TypeId::Get(to))
1187       << "Cannot downcast " << from.GetTypeName() << " to " << to.GetTypeName();
1188 }
1189 
1190 template <bool test_call, typename MessageLite>
PlacementNew(const MessageLite * prototype_for_func,const MessageLite * prototype_for_copy,void * mem,Arena * arena)1191 PROTOBUF_ALWAYS_INLINE inline MessageLite* MessageCreator::PlacementNew(
1192     const MessageLite* prototype_for_func,
1193     const MessageLite* prototype_for_copy, void* mem, Arena* arena) const {
1194   ABSL_DCHECK_EQ(reinterpret_cast<uintptr_t>(mem) % alignment_, 0u);
1195   const Tag as_tag = tag();
1196   // When the feature is not enabled we skip the `as_tag` check since it is
1197   // unnecessary. Except for testing, where we want to test the copy logic even
1198   // when we can't use it for real messages.
1199   constexpr bool kMustBeFunc = !test_call && !internal::EnableCustomNew();
1200   static_assert(kFunc < 0 && !(kZeroInit < 0) && !(kMemcpy < 0),
1201                 "Only kFunc must be the only negative value");
1202   if (ABSL_PREDICT_FALSE(kMustBeFunc || as_tag < 0)) {
1203     PROTOBUF_DEBUG_COUNTER("MessageCreator.Func").Inc();
1204     return static_cast<MessageLite*>(func_(prototype_for_func, mem, arena));
1205   }
1206 
1207   char* dst = static_cast<char*>(mem);
1208   const size_t size = allocation_size_;
1209   const char* src = reinterpret_cast<const char*>(prototype_for_copy);
1210 
1211   // These are a bit more efficient than calling normal memset/memcpy because:
1212   //  - We know the minimum size is 16. We have a fallback for when it is not.
1213   //  - We can "underflow" the buffer because those are the MessageLite bytes
1214   //    we will set later.
1215 #ifndef PROTO2_OPENSOURCE
1216   // This manual handling shows a 1.85% improvement in the parsing
1217   // microbenchmark.
1218   // TODO: Verify this is still the case.
1219 #endif  // !PROTO2_OPENSOUCE
1220   if (as_tag == kZeroInit) {
1221     // Make sure the input is really all zeros.
1222     ABSL_DCHECK(std::all_of(src + sizeof(MessageLite), src + size,
1223                             [](auto c) { return c == 0; }));
1224 
1225     if (sizeof(MessageLite) != 16) {
1226       memset(dst, 0, size);
1227     } else if (size <= 32) {
1228       memset(dst + size - 16, 0, 16);
1229     } else if (size <= 64) {
1230       memset(dst + 16, 0, 16);
1231       memset(dst + size - 32, 0, 32);
1232     } else {
1233       for (size_t offset = 16; offset + 64 < size; offset += 64) {
1234         absl::PrefetchToLocalCacheForWrite(dst + offset + 64);
1235         memset(dst + offset, 0, 64);
1236       }
1237       memset(dst + size - 64, 0, 64);
1238     }
1239   } else {
1240     ABSL_DCHECK_EQ(+as_tag, +kMemcpy);
1241 
1242     if (sizeof(MessageLite) != 16) {
1243       memcpy(dst, src, size);
1244     } else if (size <= 32) {
1245       memcpy(dst + size - 16, src + size - 16, 16);
1246     } else if (size <= 64) {
1247       memcpy(dst + 16, src + 16, 16);
1248       memcpy(dst + size - 32, src + size - 32, 32);
1249     } else {
1250       for (size_t offset = 16; offset + 64 < size; offset += 64) {
1251         absl::PrefetchToLocalCache(src + offset + 64);
1252         absl::PrefetchToLocalCacheForWrite(dst + offset + 64);
1253         memcpy(dst + offset, src + offset, 64);
1254       }
1255       memcpy(dst + size - 64, src + size - 64, 64);
1256     }
1257   }
1258 
1259   if (arena_bits() != 0) {
1260     if (as_tag == kZeroInit) {
1261       PROTOBUF_DEBUG_COUNTER("MessageCreator.ZeroArena").Inc();
1262     } else {
1263       PROTOBUF_DEBUG_COUNTER("MessageCreator.McpyArena").Inc();
1264     }
1265   } else {
1266     if (as_tag == kZeroInit) {
1267       PROTOBUF_DEBUG_COUNTER("MessageCreator.Zero").Inc();
1268     } else {
1269       PROTOBUF_DEBUG_COUNTER("MessageCreator.Mcpy").Inc();
1270     }
1271   }
1272 
1273   if (internal::PerformDebugChecks() || arena != nullptr) {
1274     if (uintptr_t offsets = arena_bits()) {
1275       do {
1276         const size_t offset = absl::countr_zero(offsets) * sizeof(Arena*);
1277         ABSL_DCHECK_LE(offset + sizeof(Arena*), size);
1278         // Verify we are overwriting a null pointer. If we are not, there is a
1279         // bug somewhere.
1280         ABSL_DCHECK_EQ(*reinterpret_cast<Arena**>(dst + offset), nullptr);
1281         memcpy(dst + offset, &arena, sizeof(arena));
1282         offsets &= offsets - 1;
1283       } while (offsets != 0);
1284     }
1285   }
1286 
1287   // The second memcpy overwrites part of the first, but the compiler should
1288   // avoid the double-write. It's easier than trying to avoid the overlap.
1289   memcpy(dst, static_cast<const void*>(prototype_for_copy),
1290          sizeof(MessageLite));
1291   memcpy(dst + PROTOBUF_FIELD_OFFSET(MessageLite, _internal_metadata_), &arena,
1292          sizeof(arena));
1293   return Launder(reinterpret_cast<MessageLite*>(mem));
1294 }
1295 
1296 template <bool test_call, typename MessageLite>
New(const MessageLite * prototype_for_func,const MessageLite * prototype_for_copy,Arena * arena)1297 PROTOBUF_ALWAYS_INLINE inline MessageLite* MessageCreator::New(
1298     const MessageLite* prototype_for_func,
1299     const MessageLite* prototype_for_copy, Arena* arena) const {
1300   return PlacementNew<test_call>(prototype_for_func, prototype_for_copy,
1301                                  arena != nullptr
1302                                      ? arena->AllocateAligned(allocation_size_)
1303                                      : ::operator new(allocation_size_),
1304                                  arena);
1305 }
1306 
1307 }  // namespace internal
1308 
1309 std::string ShortFormat(const MessageLite& message_lite);
1310 std::string Utf8Format(const MessageLite& message_lite);
1311 
1312 // Cast functions for message pointer/references.
1313 // This is the supported API to cast from a Message/MessageLite to derived
1314 // types. These work even when RTTI is disabled on message types.
1315 //
1316 // The template parameter is simplified and the return type is inferred from the
1317 // input. Eg just `DynamicCastMessage<Foo>(x)` instead of
1318 // `DynamicCastMessage<const Foo*>(x)`.
1319 //
1320 // `DynamicCastMessage` is similar to `dynamic_cast`, returns `nullptr` when the
1321 // input is not an instance of `T`. The overloads that take a reference will
1322 // terminate on mismatch.
1323 //
1324 // `DownCastMessage` is a lightweight function for downcasting base
1325 // `MessageLite` pointer to derived type, where it only does type checking if
1326 // !NDEBUG. It should only be used when the caller is certain that the input
1327 // message is of instance `T`.
1328 template <typename T>
DynamicCastMessage(const MessageLite * from)1329 const T* DynamicCastMessage(const MessageLite* from) {
1330   static_assert(std::is_base_of<MessageLite, T>::value, "");
1331 
1332   // We might avoid the call to T::GetClassData() altogether if T were to
1333   // expose the class data pointer.
1334   if (from == nullptr || TypeId::Get<T>() != TypeId::Get(*from)) {
1335     return nullptr;
1336   }
1337 
1338   return static_cast<const T*>(from);
1339 }
1340 
1341 template <typename T>
DynamicCastMessage(MessageLite * from)1342 T* DynamicCastMessage(MessageLite* from) {
1343   return const_cast<T*>(
1344       DynamicCastMessage<T>(static_cast<const MessageLite*>(from)));
1345 }
1346 
1347 namespace internal {
1348 [[noreturn]] PROTOBUF_EXPORT void FailDynamicCast(const MessageLite& from,
1349                                                   const MessageLite& to);
1350 }  // namespace internal
1351 
1352 template <typename T>
DynamicCastMessage(const MessageLite & from)1353 const T& DynamicCastMessage(const MessageLite& from) {
1354   const T* destination_message = DynamicCastMessage<T>(&from);
1355   if (ABSL_PREDICT_FALSE(destination_message == nullptr)) {
1356     // Move the logging into an out-of-line function to reduce bloat in the
1357     // caller.
1358     internal::FailDynamicCast(from, T::default_instance());
1359   }
1360   return *destination_message;
1361 }
1362 
1363 template <typename T>
DynamicCastMessage(MessageLite & from)1364 T& DynamicCastMessage(MessageLite& from) {
1365   return const_cast<T&>(
1366       DynamicCastMessage<T>(static_cast<const MessageLite&>(from)));
1367 }
1368 
1369 template <typename T>
DownCastMessage(const MessageLite * from)1370 const T* DownCastMessage(const MessageLite* from) {
1371   internal::StrongReferenceToType<T>();
1372   ABSL_DCHECK(DynamicCastMessage<T>(from) == from)
1373       << "Cannot downcast " << from->GetTypeName() << " to "
1374       << T::default_instance().GetTypeName();
1375   return static_cast<const T*>(from);
1376 }
1377 
1378 template <typename T>
DownCastMessage(MessageLite * from)1379 T* DownCastMessage(MessageLite* from) {
1380   return const_cast<T*>(
1381       DownCastMessage<T>(static_cast<const MessageLite*>(from)));
1382 }
1383 
1384 template <typename T>
DownCastMessage(const MessageLite & from)1385 const T& DownCastMessage(const MessageLite& from) {
1386   return *DownCastMessage<T>(&from);
1387 }
1388 
1389 template <typename T>
DownCastMessage(MessageLite & from)1390 T& DownCastMessage(MessageLite& from) {
1391   return *DownCastMessage<T>(&from);
1392 }
1393 
1394 template <>
DynamicCastMessage(const MessageLite * from)1395 inline const MessageLite* DynamicCastMessage(const MessageLite* from) {
1396   return from;
1397 }
1398 template <>
DownCastMessage(const MessageLite * from)1399 inline const MessageLite* DownCastMessage(const MessageLite* from) {
1400   return from;
1401 }
1402 
1403 // Deprecated names for the cast functions.
1404 // Prefer the ones above.
1405 template <typename T>
PROTOBUF_DEPRECATE_AND_INLINE()1406 PROTOBUF_DEPRECATE_AND_INLINE()
1407 const T* DynamicCastToGenerated(const MessageLite* from) {
1408   return DynamicCastMessage<T>(from);
1409 }
1410 
1411 template <typename T>
PROTOBUF_DEPRECATE_AND_INLINE()1412 PROTOBUF_DEPRECATE_AND_INLINE()
1413 T* DynamicCastToGenerated(MessageLite* from) {
1414   return DynamicCastMessage<T>(from);
1415 }
1416 
1417 template <typename T>
PROTOBUF_DEPRECATE_AND_INLINE()1418 PROTOBUF_DEPRECATE_AND_INLINE()
1419 const T& DynamicCastToGenerated(const MessageLite& from) {
1420   return DynamicCastMessage<T>(from);
1421 }
1422 
1423 template <typename T>
PROTOBUF_DEPRECATE_AND_INLINE()1424 PROTOBUF_DEPRECATE_AND_INLINE()
1425 T& DynamicCastToGenerated(MessageLite& from) {
1426   return DynamicCastMessage<T>(from);
1427 }
1428 
1429 template <typename T>
PROTOBUF_DEPRECATE_AND_INLINE()1430 PROTOBUF_DEPRECATE_AND_INLINE()
1431 const T* DownCastToGenerated(const MessageLite* from) {
1432   return DownCastMessage<T>(from);
1433 }
1434 
1435 template <typename T>
PROTOBUF_DEPRECATE_AND_INLINE()1436 PROTOBUF_DEPRECATE_AND_INLINE()
1437 T* DownCastToGenerated(MessageLite* from) {
1438   return DownCastMessage<T>(from);
1439 }
1440 
1441 template <typename T>
PROTOBUF_DEPRECATE_AND_INLINE()1442 PROTOBUF_DEPRECATE_AND_INLINE()
1443 const T& DownCastToGenerated(const MessageLite& from) {
1444   return DownCastMessage<T>(from);
1445 }
1446 
1447 template <typename T>
PROTOBUF_DEPRECATE_AND_INLINE()1448 PROTOBUF_DEPRECATE_AND_INLINE()
1449 T& DownCastToGenerated(MessageLite& from) {
1450   return DownCastMessage<T>(from);
1451 }
1452 
1453 }  // namespace protobuf
1454 }  // namespace google
1455 
1456 #include "google/protobuf/port_undef.inc"
1457 
1458 #endif  // GOOGLE_PROTOBUF_MESSAGE_LITE_H__
1459