Coverage Report

Created: 2024-07-27 06:53

/src/LPM/external.protobuf/include/google/protobuf/extension_set.h
Line
Count
Source (jump to first uncovered line)
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
// Author: kenton@google.com (Kenton Varda)
9
//  Based on original Protocol Buffers design by
10
//  Sanjay Ghemawat, Jeff Dean, and others.
11
//
12
// This header is logically internal, but is made public because it is used
13
// from protocol-compiler-generated code, which may reside in other components.
14
15
#ifndef GOOGLE_PROTOBUF_EXTENSION_SET_H__
16
#define GOOGLE_PROTOBUF_EXTENSION_SET_H__
17
18
#include <algorithm>
19
#include <atomic>
20
#include <cassert>
21
#include <cstddef>
22
#include <cstdint>
23
#include <initializer_list>
24
#include <string>
25
#include <type_traits>
26
#include <utility>
27
#include <vector>
28
29
#include "google/protobuf/stubs/common.h"
30
#include "absl/base/call_once.h"
31
#include "absl/container/btree_map.h"
32
#include "absl/log/absl_check.h"
33
#include "google/protobuf/internal_visibility.h"
34
#include "google/protobuf/port.h"
35
#include "google/protobuf/io/coded_stream.h"
36
#include "google/protobuf/message_lite.h"
37
#include "google/protobuf/parse_context.h"
38
#include "google/protobuf/repeated_field.h"
39
#include "google/protobuf/repeated_ptr_field.h"
40
#include "google/protobuf/wire_format_lite.h"
41
42
// clang-format off
43
#include "google/protobuf/port_def.inc"  // Must be last
44
// clang-format on
45
46
#ifdef SWIG
47
#error "You cannot SWIG proto headers"
48
#endif
49
50
51
namespace google {
52
namespace protobuf {
53
class Arena;
54
class Descriptor;       // descriptor.h
55
class FieldDescriptor;  // descriptor.h
56
class DescriptorPool;   // descriptor.h
57
class MessageLite;      // message_lite.h
58
class Message;          // message.h
59
class MessageFactory;   // message.h
60
class Reflection;       // message.h
61
class UnknownFieldSet;  // unknown_field_set.h
62
class FeatureSet;
63
namespace internal {
64
struct DescriptorTable;
65
class FieldSkipper;     // wire_format_lite.h
66
class ReflectionVisit;  // message_reflection_util.h
67
class WireFormat;
68
struct DynamicExtensionInfoHelper;
69
void InitializeLazyExtensionSet();
70
}  // namespace internal
71
}  // namespace protobuf
72
}  // namespace google
73
namespace pb {
74
class CppFeatures;
75
}  // namespace pb
76
77
namespace google {
78
namespace protobuf {
79
namespace internal {
80
81
class InternalMetadata;
82
83
// Used to store values of type WireFormatLite::FieldType without having to
84
// #include wire_format_lite.h.  Also, ensures that we use only one byte to
85
// store these values, which is important to keep the layout of
86
// ExtensionSet::Extension small.
87
typedef uint8_t FieldType;
88
89
// A function which, given an integer value, returns true if the number
90
// matches one of the defined values for the corresponding enum type.  This
91
// is used with RegisterEnumExtension, below.
92
typedef bool EnumValidityFunc(int number);
93
94
// Version of the above which takes an argument.  This is needed to deal with
95
// extensions that are not compiled in.
96
typedef bool EnumValidityFuncWithArg(const void* arg, int number);
97
98
enum class LazyAnnotation : int8_t {
99
  kUndefined = 0,
100
  kLazy = 1,
101
  kEager = 2,
102
};
103
104
// Information about a registered extension.
105
struct ExtensionInfo {
106
0
  constexpr ExtensionInfo() : enum_validity_check() {}
107
  constexpr ExtensionInfo(const MessageLite* extendee, int param_number,
108
                          FieldType type_param, bool isrepeated, bool ispacked)
109
      : message(extendee),
110
        number(param_number),
111
        type(type_param),
112
        is_repeated(isrepeated),
113
        is_packed(ispacked),
114
0
        enum_validity_check() {}
115
  constexpr ExtensionInfo(const MessageLite* extendee, int param_number,
116
                          FieldType type_param, bool isrepeated, bool ispacked,
117
                          LazyEagerVerifyFnType verify_func,
118
                          LazyAnnotation islazy = LazyAnnotation::kUndefined)
119
      : message(extendee),
120
        number(param_number),
121
        type(type_param),
122
        is_repeated(isrepeated),
123
        is_packed(ispacked),
124
        is_lazy(islazy),
125
        enum_validity_check(),
126
0
        lazy_eager_verify_func(verify_func) {}
127
128
  const MessageLite* message = nullptr;
129
  int number = 0;
130
131
  FieldType type = 0;
132
  bool is_repeated = false;
133
  bool is_packed = false;
134
  LazyAnnotation is_lazy = LazyAnnotation::kUndefined;
135
136
  struct EnumValidityCheck {
137
    EnumValidityFuncWithArg* func;
138
    const void* arg;
139
  };
140
141
  struct MessageInfo {
142
    const MessageLite* prototype;
143
    // The TcParse table used for this object.
144
    // Never null. (except in platforms that don't constant initialize default
145
    // instances)
146
    const internal::TcParseTableBase* tc_table;
147
  };
148
149
  union {
150
    EnumValidityCheck enum_validity_check;
151
    MessageInfo message_info;
152
  };
153
154
  // The descriptor for this extension, if one exists and is known.  May be
155
  // nullptr.  Must not be nullptr if the descriptor for the extension does not
156
  // live in the same pool as the descriptor for the containing type.
157
  const FieldDescriptor* descriptor = nullptr;
158
159
  // If this field is potentially lazy this function can be used as a cheap
160
  // verification of the raw bytes.
161
  // If nullptr then no verification is performed.
162
  LazyEagerVerifyFnType lazy_eager_verify_func = nullptr;
163
};
164
165
166
// An ExtensionFinder is an object which looks up extension definitions.  It
167
// must implement this method:
168
//
169
// bool Find(int number, ExtensionInfo* output);
170
171
// GeneratedExtensionFinder is an ExtensionFinder which finds extensions
172
// defined in .proto files which have been compiled into the binary.
173
class PROTOBUF_EXPORT GeneratedExtensionFinder {
174
 public:
175
  explicit GeneratedExtensionFinder(const MessageLite* extendee)
176
0
      : extendee_(extendee) {}
177
178
  // Returns true and fills in *output if found, otherwise returns false.
179
  bool Find(int number, ExtensionInfo* output);
180
181
 private:
182
  const MessageLite* extendee_;
183
};
184
185
// Note:  extension_set_heavy.cc defines DescriptorPoolExtensionFinder for
186
// finding extensions from a DescriptorPool.
187
188
// This is an internal helper class intended for use within the protocol buffer
189
// library and generated classes.  Clients should not use it directly.  Instead,
190
// use the generated accessors such as GetExtension() of the class being
191
// extended.
192
//
193
// This class manages extensions for a protocol message object.  The
194
// message's HasExtension(), GetExtension(), MutableExtension(), and
195
// ClearExtension() methods are just thin wrappers around the embedded
196
// ExtensionSet.  When parsing, if a tag number is encountered which is
197
// inside one of the message type's extension ranges, the tag is passed
198
// off to the ExtensionSet for parsing.  Etc.
199
class PROTOBUF_EXPORT ExtensionSet {
200
 public:
201
0
  constexpr ExtensionSet() : ExtensionSet(nullptr) {}
202
  ExtensionSet(const ExtensionSet& rhs) = delete;
203
204
  // Arena enabled constructors: for internal use only.
205
  ExtensionSet(internal::InternalVisibility, Arena* arena)
206
0
      : ExtensionSet(arena) {}
207
208
  // TODO: make constructor private, and migrate `ArenaInitialized`
209
  // to `InternalVisibility` overloaded constructor(s).
210
  explicit constexpr ExtensionSet(Arena* arena);
211
0
  ExtensionSet(ArenaInitialized, Arena* arena) : ExtensionSet(arena) {}
212
213
  ExtensionSet& operator=(const ExtensionSet&) = delete;
214
  ~ExtensionSet();
215
216
  // These are called at startup by protocol-compiler-generated code to
217
  // register known extensions.  The registrations are used by ParseField()
218
  // to look up extensions for parsed field numbers.  Note that dynamic parsing
219
  // does not use ParseField(); only protocol-compiler-generated parsing
220
  // methods do.
221
  static void RegisterExtension(const MessageLite* extendee, int number,
222
                                FieldType type, bool is_repeated,
223
                                bool is_packed);
224
  static void RegisterEnumExtension(const MessageLite* extendee, int number,
225
                                    FieldType type, bool is_repeated,
226
                                    bool is_packed, EnumValidityFunc* is_valid);
227
  static void RegisterMessageExtension(const MessageLite* extendee, int number,
228
                                       FieldType type, bool is_repeated,
229
                                       bool is_packed,
230
                                       const MessageLite* prototype,
231
                                       LazyEagerVerifyFnType verify_func,
232
                                       LazyAnnotation is_lazy);
233
234
  // In weak descriptor mode we register extensions in two phases.
235
  // This function determines if it is the right time to register a particular
236
  // extension.
237
  // During "preregistration" we only register extensions that have all their
238
  // types linked in.
239
  struct WeakPrototypeRef {
240
    const internal::DescriptorTable* table;
241
    int index;
242
  };
243
  static bool ShouldRegisterAtThisTime(
244
      std::initializer_list<WeakPrototypeRef> messages,
245
      bool is_preregistration);
246
247
  // =================================================================
248
249
  // Add all fields which are currently present to the given vector.  This
250
  // is useful to implement Reflection::ListFields(). Descriptors are appended
251
  // in increasing tag order.
252
  void AppendToList(const Descriptor* extendee, const DescriptorPool* pool,
253
                    std::vector<const FieldDescriptor*>* output) const;
254
255
  // =================================================================
256
  // Accessors
257
  //
258
  // Generated message classes include type-safe templated wrappers around
259
  // these methods.  Generally you should use those rather than call these
260
  // directly, unless you are doing low-level memory management.
261
  //
262
  // When calling any of these accessors, the extension number requested
263
  // MUST exist in the DescriptorPool provided to the constructor.  Otherwise,
264
  // the method will fail an assert.  Normally, though, you would not call
265
  // these directly; you would either call the generated accessors of your
266
  // message class (e.g. GetExtension()) or you would call the accessors
267
  // of the reflection interface.  In both cases, it is impossible to
268
  // trigger this assert failure:  the generated accessors only accept
269
  // linked-in extension types as parameters, while the Reflection interface
270
  // requires you to provide the FieldDescriptor describing the extension.
271
  //
272
  // When calling any of these accessors, a protocol-compiler-generated
273
  // implementation of the extension corresponding to the number MUST
274
  // be linked in, and the FieldDescriptor used to refer to it MUST be
275
  // the one generated by that linked-in code.  Otherwise, the method will
276
  // die on an assert failure.  The message objects returned by the message
277
  // accessors are guaranteed to be of the correct linked-in type.
278
  //
279
  // These methods pretty much match Reflection except that:
280
  // - They're not virtual.
281
  // - They identify fields by number rather than FieldDescriptors.
282
  // - They identify enum values using integers rather than descriptors.
283
  // - Strings provide Mutable() in addition to Set() accessors.
284
285
  bool Has(int number) const;
286
  int ExtensionSize(int number) const;  // Size of a repeated extension.
287
  int NumExtensions() const;            // The number of extensions
288
  FieldType ExtensionType(int number) const;
289
  void ClearExtension(int number);
290
291
  // singular fields -------------------------------------------------
292
293
  int32_t GetInt32(int number, int32_t default_value) const;
294
  int64_t GetInt64(int number, int64_t default_value) const;
295
  uint32_t GetUInt32(int number, uint32_t default_value) const;
296
  uint64_t GetUInt64(int number, uint64_t default_value) const;
297
  float GetFloat(int number, float default_value) const;
298
  double GetDouble(int number, double default_value) const;
299
  bool GetBool(int number, bool default_value) const;
300
  int GetEnum(int number, int default_value) const;
301
  const std::string& GetString(int number,
302
                               const std::string& default_value) const;
303
  const MessageLite& GetMessage(int number,
304
                                const MessageLite& default_value) const;
305
  const MessageLite& GetMessage(int number, const Descriptor* message_type,
306
                                MessageFactory* factory) const;
307
308
  // |descriptor| may be nullptr so long as it is known that the descriptor for
309
  // the extension lives in the same pool as the descriptor for the containing
310
  // type.
311
#define desc const FieldDescriptor* descriptor  // avoid line wrapping
312
  void SetInt32(int number, FieldType type, int32_t value, desc);
313
  void SetInt64(int number, FieldType type, int64_t value, desc);
314
  void SetUInt32(int number, FieldType type, uint32_t value, desc);
315
  void SetUInt64(int number, FieldType type, uint64_t value, desc);
316
  void SetFloat(int number, FieldType type, float value, desc);
317
  void SetDouble(int number, FieldType type, double value, desc);
318
  void SetBool(int number, FieldType type, bool value, desc);
319
  void SetEnum(int number, FieldType type, int value, desc);
320
  void SetString(int number, FieldType type, std::string value, desc);
321
  std::string* MutableString(int number, FieldType type, desc);
322
  MessageLite* MutableMessage(int number, FieldType type,
323
                              const MessageLite& prototype, desc);
324
  MessageLite* MutableMessage(const FieldDescriptor* descriptor,
325
                              MessageFactory* factory);
326
  // Adds the given message to the ExtensionSet, taking ownership of the
327
  // message object. Existing message with the same number will be deleted.
328
  // If "message" is nullptr, this is equivalent to "ClearExtension(number)".
329
  void SetAllocatedMessage(int number, FieldType type,
330
                           const FieldDescriptor* descriptor,
331
                           MessageLite* message);
332
  void UnsafeArenaSetAllocatedMessage(int number, FieldType type,
333
                                      const FieldDescriptor* descriptor,
334
                                      MessageLite* message);
335
  PROTOBUF_NODISCARD MessageLite* ReleaseMessage(int number,
336
                                                 const MessageLite& prototype);
337
  MessageLite* UnsafeArenaReleaseMessage(int number,
338
                                         const MessageLite& prototype);
339
340
  PROTOBUF_NODISCARD MessageLite* ReleaseMessage(
341
      const FieldDescriptor* descriptor, MessageFactory* factory);
342
  MessageLite* UnsafeArenaReleaseMessage(const FieldDescriptor* descriptor,
343
                                         MessageFactory* factory);
344
#undef desc
345
0
  Arena* GetArena() const { return arena_; }
346
347
  // repeated fields -------------------------------------------------
348
349
  // Fetches a RepeatedField extension by number; returns |default_value|
350
  // if no such extension exists. User should not touch this directly; it is
351
  // used by the GetRepeatedExtension() method.
352
  const void* GetRawRepeatedField(int number, const void* default_value) const;
353
  // Fetches a mutable version of a RepeatedField extension by number,
354
  // instantiating one if none exists. Similar to above, user should not use
355
  // this directly; it underlies MutableRepeatedExtension().
356
  void* MutableRawRepeatedField(int number, FieldType field_type, bool packed,
357
                                const FieldDescriptor* desc);
358
359
  // This is an overload of MutableRawRepeatedField to maintain compatibility
360
  // with old code using a previous API. This version of
361
  // MutableRawRepeatedField() will ABSL_CHECK-fail on a missing extension.
362
  // (E.g.: borg/clients/internal/proto1/proto2_reflection.cc.)
363
  void* MutableRawRepeatedField(int number);
364
365
  int32_t GetRepeatedInt32(int number, int index) const;
366
  int64_t GetRepeatedInt64(int number, int index) const;
367
  uint32_t GetRepeatedUInt32(int number, int index) const;
368
  uint64_t GetRepeatedUInt64(int number, int index) const;
369
  float GetRepeatedFloat(int number, int index) const;
370
  double GetRepeatedDouble(int number, int index) const;
371
  bool GetRepeatedBool(int number, int index) const;
372
  int GetRepeatedEnum(int number, int index) const;
373
  const std::string& GetRepeatedString(int number, int index) const;
374
  const MessageLite& GetRepeatedMessage(int number, int index) const;
375
376
  void SetRepeatedInt32(int number, int index, int32_t value);
377
  void SetRepeatedInt64(int number, int index, int64_t value);
378
  void SetRepeatedUInt32(int number, int index, uint32_t value);
379
  void SetRepeatedUInt64(int number, int index, uint64_t value);
380
  void SetRepeatedFloat(int number, int index, float value);
381
  void SetRepeatedDouble(int number, int index, double value);
382
  void SetRepeatedBool(int number, int index, bool value);
383
  void SetRepeatedEnum(int number, int index, int value);
384
  void SetRepeatedString(int number, int index, std::string value);
385
  std::string* MutableRepeatedString(int number, int index);
386
  MessageLite* MutableRepeatedMessage(int number, int index);
387
388
#define desc const FieldDescriptor* descriptor  // avoid line wrapping
389
  void AddInt32(int number, FieldType type, bool packed, int32_t value, desc);
390
  void AddInt64(int number, FieldType type, bool packed, int64_t value, desc);
391
  void AddUInt32(int number, FieldType type, bool packed, uint32_t value, desc);
392
  void AddUInt64(int number, FieldType type, bool packed, uint64_t value, desc);
393
  void AddFloat(int number, FieldType type, bool packed, float value, desc);
394
  void AddDouble(int number, FieldType type, bool packed, double value, desc);
395
  void AddBool(int number, FieldType type, bool packed, bool value, desc);
396
  void AddEnum(int number, FieldType type, bool packed, int value, desc);
397
  void AddString(int number, FieldType type, std::string value, desc);
398
  std::string* AddString(int number, FieldType type, desc);
399
  MessageLite* AddMessage(int number, FieldType type,
400
                          const MessageLite& prototype, desc);
401
  MessageLite* AddMessage(const FieldDescriptor* descriptor,
402
                          MessageFactory* factory);
403
  void AddAllocatedMessage(const FieldDescriptor* descriptor,
404
                           MessageLite* new_entry);
405
  void UnsafeArenaAddAllocatedMessage(const FieldDescriptor* descriptor,
406
                                      MessageLite* new_entry);
407
#undef desc
408
409
  void RemoveLast(int number);
410
  PROTOBUF_NODISCARD MessageLite* ReleaseLast(int number);
411
  MessageLite* UnsafeArenaReleaseLast(int number);
412
  void SwapElements(int number, int index1, int index2);
413
414
  // =================================================================
415
  // convenience methods for implementing methods of Message
416
  //
417
  // These could all be implemented in terms of the other methods of this
418
  // class, but providing them here helps keep the generated code size down.
419
420
  void Clear();
421
  void MergeFrom(const MessageLite* extendee, const ExtensionSet& other);
422
  void Swap(const MessageLite* extendee, ExtensionSet* other);
423
  void InternalSwap(ExtensionSet* other);
424
  void SwapExtension(const MessageLite* extendee, ExtensionSet* other,
425
                     int number);
426
  void UnsafeShallowSwapExtension(ExtensionSet* other, int number);
427
  bool IsInitialized(const MessageLite* extendee) const;
428
429
  // Lite parser
430
  const char* ParseField(uint64_t tag, const char* ptr,
431
                         const MessageLite* extendee,
432
                         internal::InternalMetadata* metadata,
433
                         internal::ParseContext* ctx);
434
  // Full parser
435
  const char* ParseField(uint64_t tag, const char* ptr, const Message* extendee,
436
                         internal::InternalMetadata* metadata,
437
                         internal::ParseContext* ctx);
438
  template <typename Msg>
439
  const char* ParseMessageSet(const char* ptr, const Msg* extendee,
440
                              InternalMetadata* metadata,
441
                              internal::ParseContext* ctx) {
442
    while (!ctx->Done(&ptr)) {
443
      uint32_t tag;
444
      ptr = ReadTag(ptr, &tag);
445
      GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
446
      if (tag == WireFormatLite::kMessageSetItemStartTag) {
447
        ptr = ctx->ParseGroupInlined(ptr, tag, [&](const char* ptr) {
448
          return ParseMessageSetItem(ptr, extendee, metadata, ctx);
449
        });
450
        GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
451
      } else {
452
        if (tag == 0 || (tag & 7) == 4) {
453
          ctx->SetLastTag(tag);
454
          return ptr;
455
        }
456
        ptr = ParseField(tag, ptr, extendee, metadata, ctx);
457
        GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
458
      }
459
    }
460
    return ptr;
461
  }
462
463
  // Write all extension fields with field numbers in the range
464
  //   [start_field_number, end_field_number)
465
  // to the output stream, using the cached sizes computed when ByteSize() was
466
  // last called.  Note that the range bounds are inclusive-exclusive.
467
  void SerializeWithCachedSizes(const MessageLite* extendee,
468
                                int start_field_number, int end_field_number,
469
0
                                io::CodedOutputStream* output) const {
470
0
    output->SetCur(_InternalSerialize(extendee, start_field_number,
471
0
                                      end_field_number, output->Cur(),
472
0
                                      output->EpsCopy()));
473
0
  }
474
475
  // Same as SerializeWithCachedSizes, but without any bounds checking.
476
  // The caller must ensure that target has sufficient capacity for the
477
  // serialized extensions.
478
  //
479
  // Returns a pointer past the last written byte.
480
481
  uint8_t* _InternalSerialize(const MessageLite* extendee,
482
                              int start_field_number, int end_field_number,
483
                              uint8_t* target,
484
0
                              io::EpsCopyOutputStream* stream) const {
485
0
    if (flat_size_ == 0) {
486
0
      assert(!is_large());
487
0
      return target;
488
0
    }
489
0
    return _InternalSerializeImpl(extendee, start_field_number,
490
0
                                  end_field_number, target, stream);
491
0
  }
492
493
  // Like above but serializes in MessageSet format.
494
  void SerializeMessageSetWithCachedSizes(const MessageLite* extendee,
495
0
                                          io::CodedOutputStream* output) const {
496
0
    output->SetCur(InternalSerializeMessageSetWithCachedSizesToArray(
497
0
        extendee, output->Cur(), output->EpsCopy()));
498
0
  }
499
  uint8_t* InternalSerializeMessageSetWithCachedSizesToArray(
500
      const MessageLite* extendee, uint8_t* target,
501
      io::EpsCopyOutputStream* stream) const;
502
503
  // For backward-compatibility, versions of two of the above methods that
504
  // serialize deterministically iff SetDefaultSerializationDeterministic()
505
  // has been called.
506
  uint8_t* SerializeWithCachedSizesToArray(int start_field_number,
507
                                           int end_field_number,
508
                                           uint8_t* target) const;
509
  uint8_t* SerializeMessageSetWithCachedSizesToArray(
510
      const MessageLite* extendee, uint8_t* target) const;
511
512
  // Returns the total serialized size of all the extensions.
513
  size_t ByteSize() const;
514
515
  // Like ByteSize() but uses MessageSet format.
516
  size_t MessageSetByteSize() const;
517
518
  // Returns (an estimate of) the total number of bytes used for storing the
519
  // extensions in memory, excluding sizeof(*this).  If the ExtensionSet is
520
  // for a lite message (and thus possibly contains lite messages), the results
521
  // are undefined (might work, might crash, might corrupt data, might not even
522
  // be linked in).  It's up to the protocol compiler to avoid calling this on
523
  // such ExtensionSets (easy enough since lite messages don't implement
524
  // SpaceUsed()).
525
  size_t SpaceUsedExcludingSelfLong() const;
526
527
  // This method just calls SpaceUsedExcludingSelfLong() but it can not be
528
  // inlined because the definition of SpaceUsedExcludingSelfLong() is not
529
  // included in lite runtime and when an inline method refers to it MSVC
530
  // will complain about unresolved symbols when building the lite runtime
531
  // as .dll.
532
  int SpaceUsedExcludingSelf() const;
533
534
 private:
535
  template <typename Type>
536
  friend class PrimitiveTypeTraits;
537
538
  template <typename Type>
539
  friend class RepeatedPrimitiveTypeTraits;
540
541
  template <typename Type, bool IsValid(int)>
542
  friend class EnumTypeTraits;
543
544
  template <typename Type, bool IsValid(int)>
545
  friend class RepeatedEnumTypeTraits;
546
547
  friend class google::protobuf::Reflection;
548
  friend class google::protobuf::internal::ReflectionVisit;
549
  friend struct google::protobuf::internal::DynamicExtensionInfoHelper;
550
  friend class google::protobuf::internal::WireFormat;
551
552
  friend void internal::InitializeLazyExtensionSet();
553
554
  const int32_t& GetRefInt32(int number, const int32_t& default_value) const;
555
  const int64_t& GetRefInt64(int number, const int64_t& default_value) const;
556
  const uint32_t& GetRefUInt32(int number, const uint32_t& default_value) const;
557
  const uint64_t& GetRefUInt64(int number, const uint64_t& default_value) const;
558
  const float& GetRefFloat(int number, const float& default_value) const;
559
  const double& GetRefDouble(int number, const double& default_value) const;
560
  const bool& GetRefBool(int number, const bool& default_value) const;
561
  const int& GetRefEnum(int number, const int& default_value) const;
562
  const int32_t& GetRefRepeatedInt32(int number, int index) const;
563
  const int64_t& GetRefRepeatedInt64(int number, int index) const;
564
  const uint32_t& GetRefRepeatedUInt32(int number, int index) const;
565
  const uint64_t& GetRefRepeatedUInt64(int number, int index) const;
566
  const float& GetRefRepeatedFloat(int number, int index) const;
567
  const double& GetRefRepeatedDouble(int number, int index) const;
568
  const bool& GetRefRepeatedBool(int number, int index) const;
569
  const int& GetRefRepeatedEnum(int number, int index) const;
570
571
  // Implementation of _InternalSerialize for non-empty map_.
572
  uint8_t* _InternalSerializeImpl(const MessageLite* extendee,
573
                                  int start_field_number, int end_field_number,
574
                                  uint8_t* target,
575
                                  io::EpsCopyOutputStream* stream) const;
576
  // Interface of a lazily parsed singular message extension.
577
  class PROTOBUF_EXPORT LazyMessageExtension {
578
   public:
579
    LazyMessageExtension() = default;
580
    LazyMessageExtension(const LazyMessageExtension&) = delete;
581
    LazyMessageExtension& operator=(const LazyMessageExtension&) = delete;
582
    virtual ~LazyMessageExtension() = default;
583
584
    virtual LazyMessageExtension* New(Arena* arena) const = 0;
585
    virtual const MessageLite& GetMessage(const MessageLite& prototype,
586
                                          Arena* arena) const = 0;
587
    virtual const MessageLite& GetMessageIgnoreUnparsed(
588
        const MessageLite& prototype, Arena* arena) const = 0;
589
    virtual MessageLite* MutableMessage(const MessageLite& prototype,
590
                                        Arena* arena) = 0;
591
    virtual void SetAllocatedMessage(MessageLite* message, Arena* arena) = 0;
592
    virtual void UnsafeArenaSetAllocatedMessage(MessageLite* message,
593
                                                Arena* arena) = 0;
594
    PROTOBUF_NODISCARD virtual MessageLite* ReleaseMessage(
595
        const MessageLite& prototype, Arena* arena) = 0;
596
    virtual MessageLite* UnsafeArenaReleaseMessage(const MessageLite& prototype,
597
                                                   Arena* arena) = 0;
598
599
    virtual bool IsInitialized(const MessageLite* prototype,
600
                               Arena* arena) const = 0;
601
    virtual bool IsEagerSerializeSafe(const MessageLite* prototype,
602
                                      Arena* arena) const = 0;
603
604
    [[deprecated("Please use ByteSizeLong() instead")]] virtual int ByteSize()
605
0
        const {
606
0
      return internal::ToIntSize(ByteSizeLong());
607
0
    }
608
    virtual size_t ByteSizeLong() const = 0;
609
    virtual size_t SpaceUsedLong() const = 0;
610
611
    virtual void MergeFrom(const MessageLite* prototype,
612
                           const LazyMessageExtension& other, Arena* arena,
613
                           Arena* other_arena) = 0;
614
    virtual void MergeFromMessage(const MessageLite& msg, Arena* arena) = 0;
615
    virtual void Clear() = 0;
616
617
    virtual const char* _InternalParse(const MessageLite& prototype,
618
                                       Arena* arena, const char* ptr,
619
                                       ParseContext* ctx) = 0;
620
    virtual uint8_t* WriteMessageToArray(
621
        const MessageLite* prototype, int number, uint8_t* target,
622
        io::EpsCopyOutputStream* stream) const = 0;
623
624
   private:
625
    virtual void UnusedKeyMethod();  // Dummy key method to avoid weak vtable.
626
  };
627
  // Give access to function defined below to see LazyMessageExtension.
628
  static LazyMessageExtension* MaybeCreateLazyExtensionImpl(Arena* arena);
629
0
  static LazyMessageExtension* MaybeCreateLazyExtension(Arena* arena) {
630
0
    auto* f = maybe_create_lazy_extension_.load(std::memory_order_relaxed);
631
0
    return f != nullptr ? f(arena) : nullptr;
632
0
  }
633
  static std::atomic<LazyMessageExtension* (*)(Arena* arena)>
634
      maybe_create_lazy_extension_;
635
  struct Extension {
636
    // The order of these fields packs Extension into 24 bytes when using 8
637
    // byte alignment. Consider this when adding or removing fields here.
638
    union {
639
      int32_t int32_t_value;
640
      int64_t int64_t_value;
641
      uint32_t uint32_t_value;
642
      uint64_t uint64_t_value;
643
      float float_value;
644
      double double_value;
645
      bool bool_value;
646
      int enum_value;
647
      std::string* string_value;
648
      MessageLite* message_value;
649
      LazyMessageExtension* lazymessage_value;
650
651
      RepeatedField<int32_t>* repeated_int32_t_value;
652
      RepeatedField<int64_t>* repeated_int64_t_value;
653
      RepeatedField<uint32_t>* repeated_uint32_t_value;
654
      RepeatedField<uint64_t>* repeated_uint64_t_value;
655
      RepeatedField<float>* repeated_float_value;
656
      RepeatedField<double>* repeated_double_value;
657
      RepeatedField<bool>* repeated_bool_value;
658
      RepeatedField<int>* repeated_enum_value;
659
      RepeatedPtrField<std::string>* repeated_string_value;
660
      RepeatedPtrField<MessageLite>* repeated_message_value;
661
    };
662
663
    FieldType type;
664
    bool is_repeated;
665
666
    // For singular types, indicates if the extension is "cleared".  This
667
    // happens when an extension is set and then later cleared by the caller.
668
    // We want to keep the Extension object around for reuse, so instead of
669
    // removing it from the map, we just set is_cleared = true.  This has no
670
    // meaning for repeated types; for those, the size of the RepeatedField
671
    // simply becomes zero when cleared.
672
    bool is_cleared : 4;
673
674
    // For singular message types, indicates whether lazy parsing is enabled
675
    // for this extension. This field is only valid when type == TYPE_MESSAGE
676
    // and !is_repeated because we only support lazy parsing for singular
677
    // message types currently. If is_lazy = true, the extension is stored in
678
    // lazymessage_value. Otherwise, the extension will be message_value.
679
    bool is_lazy : 4;
680
681
    // For repeated types, this indicates if the [packed=true] option is set.
682
    bool is_packed;
683
684
    // For packed fields, the size of the packed data is recorded here when
685
    // ByteSize() is called then used during serialization.
686
    // TODO:  Use atomic<int> when C++ supports it.
687
    mutable int cached_size;
688
689
    // The descriptor for this extension, if one exists and is known.  May be
690
    // nullptr.  Must not be nullptr if the descriptor for the extension does
691
    // not live in the same pool as the descriptor for the containing type.
692
    const FieldDescriptor* descriptor;
693
694
    // Some helper methods for operations on a single Extension.
695
    uint8_t* InternalSerializeFieldWithCachedSizesToArray(
696
        const MessageLite* extendee, const ExtensionSet* extension_set,
697
        int number, uint8_t* target, io::EpsCopyOutputStream* stream) const;
698
    uint8_t* InternalSerializeMessageSetItemWithCachedSizesToArray(
699
        const MessageLite* extendee, const ExtensionSet* extension_set,
700
        int number, uint8_t* target, io::EpsCopyOutputStream* stream) const;
701
    size_t ByteSize(int number) const;
702
    size_t MessageSetItemByteSize(int number) const;
703
    void Clear();
704
    int GetSize() const;
705
    void Free();
706
    size_t SpaceUsedExcludingSelfLong() const;
707
    bool IsInitialized(const ExtensionSet* ext_set, const MessageLite* extendee,
708
                       int number, Arena* arena) const;
709
  };
710
711
  // The Extension struct is small enough to be passed by value, so we use it
712
  // directly as the value type in mappings rather than use pointers.  We use
713
  // sorted maps rather than hash-maps because we expect most ExtensionSets will
714
  // only contain a small number of extension.  Also, we want AppendToList and
715
  // deterministic serialization to order fields by field number.
716
717
  struct KeyValue {
718
    int first;
719
    Extension second;
720
721
    struct FirstComparator {
722
0
      bool operator()(const KeyValue& lhs, const KeyValue& rhs) const {
723
0
        return lhs.first < rhs.first;
724
0
      }
725
0
      bool operator()(const KeyValue& lhs, int key) const {
726
0
        return lhs.first < key;
727
0
      }
728
0
      bool operator()(int key, const KeyValue& rhs) const {
729
0
        return key < rhs.first;
730
0
      }
731
    };
732
  };
733
734
  using LargeMap = absl::btree_map<int, Extension>;
735
736
  // Wrapper API that switches between flat-map and LargeMap.
737
738
  // Finds a key (if present) in the ExtensionSet.
739
  const Extension* FindOrNull(int key) const;
740
  Extension* FindOrNull(int key);
741
742
  // Helper-functions that only inspect the LargeMap.
743
  const Extension* FindOrNullInLargeMap(int key) const;
744
  Extension* FindOrNullInLargeMap(int key);
745
746
  // Inserts a new (key, Extension) into the ExtensionSet (and returns true), or
747
  // finds the already-existing Extension for that key (returns false).
748
  // The Extension* will point to the new-or-found Extension.
749
  std::pair<Extension*, bool> Insert(int key);
750
751
  // Grows the flat_capacity_.
752
  // If flat_capacity_ > kMaximumFlatCapacity, converts to LargeMap.
753
  void GrowCapacity(size_t minimum_new_capacity);
754
  static constexpr uint16_t kMaximumFlatCapacity = 256;
755
0
  bool is_large() const { return static_cast<int16_t>(flat_size_) < 0; }
756
757
  // Removes a key from the ExtensionSet.
758
  void Erase(int key);
759
760
0
  size_t Size() const {
761
0
    return PROTOBUF_PREDICT_FALSE(is_large()) ? map_.large->size() : flat_size_;
762
0
  }
763
764
  // Similar to std::for_each.
765
  // Each Iterator is decomposed into ->first and ->second fields, so
766
  // that the KeyValueFunctor can be agnostic vis-a-vis KeyValue-vs-std::pair.
767
  template <typename Iterator, typename KeyValueFunctor>
768
  static KeyValueFunctor ForEach(Iterator begin, Iterator end,
769
                                 KeyValueFunctor func) {
770
    for (Iterator it = begin; it != end; ++it) func(it->first, it->second);
771
    return std::move(func);
772
  }
773
774
  // Applies a functor to the <int, Extension&> pairs in sorted order.
775
  template <typename KeyValueFunctor>
776
  KeyValueFunctor ForEach(KeyValueFunctor func) {
777
    if (PROTOBUF_PREDICT_FALSE(is_large())) {
778
      return ForEach(map_.large->begin(), map_.large->end(), std::move(func));
779
    }
780
    return ForEach(flat_begin(), flat_end(), std::move(func));
781
  }
782
783
  // Applies a functor to the <int, const Extension&> pairs in sorted order.
784
  template <typename KeyValueFunctor>
785
  KeyValueFunctor ForEach(KeyValueFunctor func) const {
786
    if (PROTOBUF_PREDICT_FALSE(is_large())) {
787
      return ForEach(map_.large->begin(), map_.large->end(), std::move(func));
788
    }
789
    return ForEach(flat_begin(), flat_end(), std::move(func));
790
  }
791
792
  // Merges existing Extension from other_extension
793
  void InternalExtensionMergeFrom(const MessageLite* extendee, int number,
794
                                  const Extension& other_extension,
795
                                  Arena* other_arena);
796
797
0
  inline static bool is_packable(WireFormatLite::WireType type) {
798
0
    switch (type) {
799
0
      case WireFormatLite::WIRETYPE_VARINT:
800
0
      case WireFormatLite::WIRETYPE_FIXED64:
801
0
      case WireFormatLite::WIRETYPE_FIXED32:
802
0
        return true;
803
0
      case WireFormatLite::WIRETYPE_LENGTH_DELIMITED:
804
0
      case WireFormatLite::WIRETYPE_START_GROUP:
805
0
      case WireFormatLite::WIRETYPE_END_GROUP:
806
0
        return false;
807
0
808
0
        // Do not add a default statement. Let the compiler complain when
809
0
        // someone
810
0
        // adds a new wire type.
811
0
    }
812
0
    Unreachable();  // switch handles all possible enum values
813
0
    return false;
814
0
  }
815
816
  // Returns true and fills field_number and extension if extension is found.
817
  // Note to support packed repeated field compatibility, it also fills whether
818
  // the tag on wire is packed, which can be different from
819
  // extension->is_packed (whether packed=true is specified).
820
  template <typename ExtensionFinder>
821
  bool FindExtensionInfoFromTag(uint32_t tag, ExtensionFinder* extension_finder,
822
                                int* field_number, ExtensionInfo* extension,
823
                                bool* was_packed_on_wire) {
824
    *field_number = WireFormatLite::GetTagFieldNumber(tag);
825
    WireFormatLite::WireType wire_type = WireFormatLite::GetTagWireType(tag);
826
    return FindExtensionInfoFromFieldNumber(wire_type, *field_number,
827
                                            extension_finder, extension,
828
                                            was_packed_on_wire);
829
  }
830
831
  // Returns true and fills extension if extension is found.
832
  // Note to support packed repeated field compatibility, it also fills whether
833
  // the tag on wire is packed, which can be different from
834
  // extension->is_packed (whether packed=true is specified).
835
  template <typename ExtensionFinder>
836
  bool FindExtensionInfoFromFieldNumber(int wire_type, int field_number,
837
                                        ExtensionFinder* extension_finder,
838
                                        ExtensionInfo* extension,
839
0
                                        bool* was_packed_on_wire) const {
840
0
    if (!extension_finder->Find(field_number, extension)) {
841
0
      return false;
842
0
    }
843
0
844
0
    ABSL_DCHECK(extension->type > 0 &&
845
0
                extension->type <= WireFormatLite::MAX_FIELD_TYPE);
846
0
    auto real_type = static_cast<WireFormatLite::FieldType>(extension->type);
847
0
848
0
    WireFormatLite::WireType expected_wire_type =
849
0
        WireFormatLite::WireTypeForFieldType(real_type);
850
0
851
0
    // Check if this is a packed field.
852
0
    *was_packed_on_wire = false;
853
0
    if (extension->is_repeated &&
854
0
        wire_type == WireFormatLite::WIRETYPE_LENGTH_DELIMITED &&
855
0
        is_packable(expected_wire_type)) {
856
0
      *was_packed_on_wire = true;
857
0
      return true;
858
0
    }
859
0
    // Otherwise the wire type must match.
860
0
    return expected_wire_type == wire_type;
861
0
  }
862
863
  // Find the prototype for a LazyMessage from the extension registry. Returns
864
  // null if the extension is not found.
865
  const MessageLite* GetPrototypeForLazyMessage(const MessageLite* extendee,
866
                                                int number) const;
867
868
  // Returns true if extension is present and lazy.
869
  bool HasLazy(int number) const;
870
871
  // Gets the extension with the given number, creating it if it does not
872
  // already exist.  Returns true if the extension did not already exist.
873
  bool MaybeNewExtension(int number, const FieldDescriptor* descriptor,
874
                         Extension** result);
875
876
  // Gets the repeated extension for the given descriptor, creating it if
877
  // it does not exist.
878
  Extension* MaybeNewRepeatedExtension(const FieldDescriptor* descriptor);
879
880
  bool FindExtension(int wire_type, uint32_t field, const MessageLite* extendee,
881
                     const internal::ParseContext* /*ctx*/,
882
0
                     ExtensionInfo* extension, bool* was_packed_on_wire) {
883
0
    GeneratedExtensionFinder finder(extendee);
884
0
    return FindExtensionInfoFromFieldNumber(wire_type, field, &finder,
885
0
                                            extension, was_packed_on_wire);
886
0
  }
887
  inline bool FindExtension(int wire_type, uint32_t field,
888
                            const Message* extendee,
889
                            const internal::ParseContext* ctx,
890
                            ExtensionInfo* extension, bool* was_packed_on_wire);
891
  // Used for MessageSet only
892
  const char* ParseFieldMaybeLazily(uint64_t tag, const char* ptr,
893
                                    const MessageLite* extendee,
894
                                    internal::InternalMetadata* metadata,
895
0
                                    internal::ParseContext* ctx) {
896
0
    // Lite MessageSet doesn't implement lazy.
897
0
    return ParseField(tag, ptr, extendee, metadata, ctx);
898
0
  }
899
  const char* ParseFieldMaybeLazily(uint64_t tag, const char* ptr,
900
                                    const Message* extendee,
901
                                    internal::InternalMetadata* metadata,
902
                                    internal::ParseContext* ctx);
903
  const char* ParseMessageSetItem(const char* ptr, const MessageLite* extendee,
904
                                  internal::InternalMetadata* metadata,
905
                                  internal::ParseContext* ctx);
906
  const char* ParseMessageSetItem(const char* ptr, const Message* extendee,
907
                                  internal::InternalMetadata* metadata,
908
                                  internal::ParseContext* ctx);
909
910
  // Implemented in extension_set_inl.h to keep code out of the header file.
911
  template <typename T>
912
  const char* ParseFieldWithExtensionInfo(int number, bool was_packed_on_wire,
913
                                          const ExtensionInfo& info,
914
                                          internal::InternalMetadata* metadata,
915
                                          const char* ptr,
916
                                          internal::ParseContext* ctx);
917
  template <typename Msg, typename T>
918
  const char* ParseMessageSetItemTmpl(const char* ptr, const Msg* extendee,
919
                                      internal::InternalMetadata* metadata,
920
                                      internal::ParseContext* ctx);
921
922
  // Hack:  RepeatedPtrFieldBase declares ExtensionSet as a friend.  This
923
  //   friendship should automatically extend to ExtensionSet::Extension, but
924
  //   unfortunately some older compilers (e.g. GCC 3.4.4) do not implement this
925
  //   correctly.  So, we must provide helpers for calling methods of that
926
  //   class.
927
928
  // Defined in extension_set_heavy.cc.
929
  static inline size_t RepeatedMessage_SpaceUsedExcludingSelfLong(
930
      RepeatedPtrFieldBase* field);
931
932
0
  KeyValue* flat_begin() {
933
0
    assert(!is_large());
934
0
    return map_.flat;
935
0
  }
936
0
  const KeyValue* flat_begin() const {
937
0
    assert(!is_large());
938
0
    return map_.flat;
939
0
  }
940
0
  KeyValue* flat_end() {
941
0
    assert(!is_large());
942
0
    return map_.flat + flat_size_;
943
0
  }
944
0
  const KeyValue* flat_end() const {
945
0
    assert(!is_large());
946
0
    return map_.flat + flat_size_;
947
0
  }
948
949
  Arena* arena_;
950
951
  // Manual memory-management:
952
  // map_.flat is an allocated array of flat_capacity_ elements.
953
  // [map_.flat, map_.flat + flat_size_) is the currently-in-use prefix.
954
  uint16_t flat_capacity_;
955
  uint16_t flat_size_;  // negative int16_t(flat_size_) indicates is_large()
956
  union AllocatedData {
957
    KeyValue* flat;
958
959
    // If flat_capacity_ > kMaximumFlatCapacity, switch to LargeMap,
960
    // which guarantees O(n lg n) CPU but larger constant factors.
961
    LargeMap* large;
962
  } map_;
963
964
  static void DeleteFlatMap(const KeyValue* flat, uint16_t flat_capacity);
965
};
966
967
constexpr ExtensionSet::ExtensionSet(Arena* arena)
968
    : arena_(arena), flat_capacity_(0), flat_size_(0), map_{nullptr} {}
969
970
// These are just for convenience...
971
inline void ExtensionSet::SetString(int number, FieldType type,
972
                                    std::string value,
973
0
                                    const FieldDescriptor* descriptor) {
974
0
  MutableString(number, type, descriptor)->assign(std::move(value));
975
0
}
976
inline void ExtensionSet::SetRepeatedString(int number, int index,
977
0
                                            std::string value) {
978
0
  MutableRepeatedString(number, index)->assign(std::move(value));
979
0
}
980
inline void ExtensionSet::AddString(int number, FieldType type,
981
                                    std::string value,
982
0
                                    const FieldDescriptor* descriptor) {
983
0
  AddString(number, type, descriptor)->assign(std::move(value));
984
0
}
985
// ===================================================================
986
// Glue for generated extension accessors
987
988
// -------------------------------------------------------------------
989
// Template magic
990
991
// First we have a set of classes representing "type traits" for different
992
// field types.  A type traits class knows how to implement basic accessors
993
// for extensions of a particular type given an ExtensionSet.  The signature
994
// for a type traits class looks like this:
995
//
996
//   class TypeTraits {
997
//    public:
998
//     typedef ? ConstType;
999
//     typedef ? MutableType;
1000
//     // TypeTraits for singular fields and repeated fields will define the
1001
//     // symbol "Singular" or "Repeated" respectively. These two symbols will
1002
//     // be used in extension accessors to distinguish between singular
1003
//     // extensions and repeated extensions. If the TypeTraits for the passed
1004
//     // in extension doesn't have the expected symbol defined, it means the
1005
//     // user is passing a repeated extension to a singular accessor, or the
1006
//     // opposite. In that case the C++ compiler will generate an error
1007
//     // message "no matching member function" to inform the user.
1008
//     typedef ? Singular
1009
//     typedef ? Repeated
1010
//
1011
//     static inline ConstType Get(int number, const ExtensionSet& set);
1012
//     static inline void Set(int number, ConstType value, ExtensionSet* set);
1013
//     static inline MutableType Mutable(int number, ExtensionSet* set);
1014
//
1015
//     // Variants for repeated fields.
1016
//     static inline ConstType Get(int number, const ExtensionSet& set,
1017
//                                 int index);
1018
//     static inline void Set(int number, int index,
1019
//                            ConstType value, ExtensionSet* set);
1020
//     static inline MutableType Mutable(int number, int index,
1021
//                                       ExtensionSet* set);
1022
//     static inline void Add(int number, ConstType value, ExtensionSet* set);
1023
//     static inline MutableType Add(int number, ExtensionSet* set);
1024
//     This is used by the ExtensionIdentifier constructor to register
1025
//     the extension at dynamic initialization.
1026
//   };
1027
//
1028
// Not all of these methods make sense for all field types.  For example, the
1029
// "Mutable" methods only make sense for strings and messages, and the
1030
// repeated methods only make sense for repeated types.  So, each type
1031
// traits class implements only the set of methods from this signature that it
1032
// actually supports.  This will cause a compiler error if the user tries to
1033
// access an extension using a method that doesn't make sense for its type.
1034
// For example, if "foo" is an extension of type "optional int32", then if you
1035
// try to write code like:
1036
//   my_message.MutableExtension(foo)
1037
// you will get a compile error because PrimitiveTypeTraits<int32_t> does not
1038
// have a "Mutable()" method.
1039
1040
// -------------------------------------------------------------------
1041
// PrimitiveTypeTraits
1042
1043
// Since the ExtensionSet has different methods for each primitive type,
1044
// we must explicitly define the methods of the type traits class for each
1045
// known type.
1046
template <typename Type>
1047
class PrimitiveTypeTraits {
1048
 public:
1049
  typedef Type ConstType;
1050
  typedef Type MutableType;
1051
  using InitType = ConstType;
1052
  static const ConstType& FromInitType(const InitType& v) { return v; }
1053
  typedef PrimitiveTypeTraits<Type> Singular;
1054
  static constexpr bool kLifetimeBound = false;
1055
1056
  static inline ConstType Get(int number, const ExtensionSet& set,
1057
                              ConstType default_value);
1058
1059
  static inline const ConstType* GetPtr(int number, const ExtensionSet& set,
1060
                                        const ConstType& default_value);
1061
  static inline void Set(int number, FieldType field_type, ConstType value,
1062
                         ExtensionSet* set);
1063
};
1064
1065
template <typename Type>
1066
class RepeatedPrimitiveTypeTraits {
1067
 public:
1068
  typedef Type ConstType;
1069
  typedef Type MutableType;
1070
  using InitType = ConstType;
1071
  static const ConstType& FromInitType(const InitType& v) { return v; }
1072
  typedef RepeatedPrimitiveTypeTraits<Type> Repeated;
1073
  static constexpr bool kLifetimeBound = false;
1074
1075
  typedef RepeatedField<Type> RepeatedFieldType;
1076
1077
  static inline Type Get(int number, const ExtensionSet& set, int index);
1078
  static inline const Type* GetPtr(int number, const ExtensionSet& set,
1079
                                   int index);
1080
  static inline const RepeatedField<ConstType>* GetRepeatedPtr(
1081
      int number, const ExtensionSet& set);
1082
  static inline void Set(int number, int index, Type value, ExtensionSet* set);
1083
  static inline void Add(int number, FieldType field_type, bool is_packed,
1084
                         Type value, ExtensionSet* set);
1085
1086
  static inline const RepeatedField<ConstType>& GetRepeated(
1087
      int number, const ExtensionSet& set);
1088
  static inline RepeatedField<Type>* MutableRepeated(int number,
1089
                                                     FieldType field_type,
1090
                                                     bool is_packed,
1091
                                                     ExtensionSet* set);
1092
1093
  static const RepeatedFieldType* GetDefaultRepeatedField();
1094
};
1095
1096
class PROTOBUF_EXPORT RepeatedPrimitiveDefaults {
1097
 private:
1098
  template <typename Type>
1099
  friend class RepeatedPrimitiveTypeTraits;
1100
  static const RepeatedPrimitiveDefaults* default_instance();
1101
  RepeatedField<int32_t> default_repeated_field_int32_t_;
1102
  RepeatedField<int64_t> default_repeated_field_int64_t_;
1103
  RepeatedField<uint32_t> default_repeated_field_uint32_t_;
1104
  RepeatedField<uint64_t> default_repeated_field_uint64_t_;
1105
  RepeatedField<double> default_repeated_field_double_;
1106
  RepeatedField<float> default_repeated_field_float_;
1107
  RepeatedField<bool> default_repeated_field_bool_;
1108
};
1109
1110
#define PROTOBUF_DEFINE_PRIMITIVE_TYPE(TYPE, METHOD)                           \
1111
  template <>                                                                  \
1112
  inline TYPE PrimitiveTypeTraits<TYPE>::Get(                                  \
1113
0
      int number, const ExtensionSet& set, TYPE default_value) {               \
1114
0
    return set.Get##METHOD(number, default_value);                             \
1115
0
  }                                                                            \
Unexecuted instantiation: google::protobuf::internal::PrimitiveTypeTraits<int>::Get(int, google::protobuf::internal::ExtensionSet const&, int)
Unexecuted instantiation: google::protobuf::internal::PrimitiveTypeTraits<long>::Get(int, google::protobuf::internal::ExtensionSet const&, long)
Unexecuted instantiation: google::protobuf::internal::PrimitiveTypeTraits<unsigned int>::Get(int, google::protobuf::internal::ExtensionSet const&, unsigned int)
Unexecuted instantiation: google::protobuf::internal::PrimitiveTypeTraits<unsigned long>::Get(int, google::protobuf::internal::ExtensionSet const&, unsigned long)
Unexecuted instantiation: google::protobuf::internal::PrimitiveTypeTraits<float>::Get(int, google::protobuf::internal::ExtensionSet const&, float)
Unexecuted instantiation: google::protobuf::internal::PrimitiveTypeTraits<double>::Get(int, google::protobuf::internal::ExtensionSet const&, double)
Unexecuted instantiation: google::protobuf::internal::PrimitiveTypeTraits<bool>::Get(int, google::protobuf::internal::ExtensionSet const&, bool)
1116
  template <>                                                                  \
1117
  inline const TYPE* PrimitiveTypeTraits<TYPE>::GetPtr(                        \
1118
0
      int number, const ExtensionSet& set, const TYPE& default_value) {        \
1119
0
    return &set.GetRef##METHOD(number, default_value);                         \
1120
0
  }                                                                            \
Unexecuted instantiation: google::protobuf::internal::PrimitiveTypeTraits<int>::GetPtr(int, google::protobuf::internal::ExtensionSet const&, int const&)
Unexecuted instantiation: google::protobuf::internal::PrimitiveTypeTraits<long>::GetPtr(int, google::protobuf::internal::ExtensionSet const&, long const&)
Unexecuted instantiation: google::protobuf::internal::PrimitiveTypeTraits<unsigned int>::GetPtr(int, google::protobuf::internal::ExtensionSet const&, unsigned int const&)
Unexecuted instantiation: google::protobuf::internal::PrimitiveTypeTraits<unsigned long>::GetPtr(int, google::protobuf::internal::ExtensionSet const&, unsigned long const&)
Unexecuted instantiation: google::protobuf::internal::PrimitiveTypeTraits<float>::GetPtr(int, google::protobuf::internal::ExtensionSet const&, float const&)
Unexecuted instantiation: google::protobuf::internal::PrimitiveTypeTraits<double>::GetPtr(int, google::protobuf::internal::ExtensionSet const&, double const&)
Unexecuted instantiation: google::protobuf::internal::PrimitiveTypeTraits<bool>::GetPtr(int, google::protobuf::internal::ExtensionSet const&, bool const&)
1121
  template <>                                                                  \
1122
  inline void PrimitiveTypeTraits<TYPE>::Set(int number, FieldType field_type, \
1123
0
                                             TYPE value, ExtensionSet* set) {  \
1124
0
    set->Set##METHOD(number, field_type, value, nullptr);                      \
1125
0
  }                                                                            \
Unexecuted instantiation: google::protobuf::internal::PrimitiveTypeTraits<int>::Set(int, unsigned char, int, google::protobuf::internal::ExtensionSet*)
Unexecuted instantiation: google::protobuf::internal::PrimitiveTypeTraits<long>::Set(int, unsigned char, long, google::protobuf::internal::ExtensionSet*)
Unexecuted instantiation: google::protobuf::internal::PrimitiveTypeTraits<unsigned int>::Set(int, unsigned char, unsigned int, google::protobuf::internal::ExtensionSet*)
Unexecuted instantiation: google::protobuf::internal::PrimitiveTypeTraits<unsigned long>::Set(int, unsigned char, unsigned long, google::protobuf::internal::ExtensionSet*)
Unexecuted instantiation: google::protobuf::internal::PrimitiveTypeTraits<float>::Set(int, unsigned char, float, google::protobuf::internal::ExtensionSet*)
Unexecuted instantiation: google::protobuf::internal::PrimitiveTypeTraits<double>::Set(int, unsigned char, double, google::protobuf::internal::ExtensionSet*)
Unexecuted instantiation: google::protobuf::internal::PrimitiveTypeTraits<bool>::Set(int, unsigned char, bool, google::protobuf::internal::ExtensionSet*)
1126
                                                                               \
1127
  template <>                                                                  \
1128
  inline TYPE RepeatedPrimitiveTypeTraits<TYPE>::Get(                          \
1129
0
      int number, const ExtensionSet& set, int index) {                        \
1130
0
    return set.GetRepeated##METHOD(number, index);                             \
1131
0
  }                                                                            \
Unexecuted instantiation: google::protobuf::internal::RepeatedPrimitiveTypeTraits<int>::Get(int, google::protobuf::internal::ExtensionSet const&, int)
Unexecuted instantiation: google::protobuf::internal::RepeatedPrimitiveTypeTraits<long>::Get(int, google::protobuf::internal::ExtensionSet const&, int)
Unexecuted instantiation: google::protobuf::internal::RepeatedPrimitiveTypeTraits<unsigned int>::Get(int, google::protobuf::internal::ExtensionSet const&, int)
Unexecuted instantiation: google::protobuf::internal::RepeatedPrimitiveTypeTraits<unsigned long>::Get(int, google::protobuf::internal::ExtensionSet const&, int)
Unexecuted instantiation: google::protobuf::internal::RepeatedPrimitiveTypeTraits<float>::Get(int, google::protobuf::internal::ExtensionSet const&, int)
Unexecuted instantiation: google::protobuf::internal::RepeatedPrimitiveTypeTraits<double>::Get(int, google::protobuf::internal::ExtensionSet const&, int)
Unexecuted instantiation: google::protobuf::internal::RepeatedPrimitiveTypeTraits<bool>::Get(int, google::protobuf::internal::ExtensionSet const&, int)
1132
  template <>                                                                  \
1133
  inline const TYPE* RepeatedPrimitiveTypeTraits<TYPE>::GetPtr(                \
1134
0
      int number, const ExtensionSet& set, int index) {                        \
1135
0
    return &set.GetRefRepeated##METHOD(number, index);                         \
1136
0
  }                                                                            \
Unexecuted instantiation: google::protobuf::internal::RepeatedPrimitiveTypeTraits<int>::GetPtr(int, google::protobuf::internal::ExtensionSet const&, int)
Unexecuted instantiation: google::protobuf::internal::RepeatedPrimitiveTypeTraits<long>::GetPtr(int, google::protobuf::internal::ExtensionSet const&, int)
Unexecuted instantiation: google::protobuf::internal::RepeatedPrimitiveTypeTraits<unsigned int>::GetPtr(int, google::protobuf::internal::ExtensionSet const&, int)
Unexecuted instantiation: google::protobuf::internal::RepeatedPrimitiveTypeTraits<unsigned long>::GetPtr(int, google::protobuf::internal::ExtensionSet const&, int)
Unexecuted instantiation: google::protobuf::internal::RepeatedPrimitiveTypeTraits<float>::GetPtr(int, google::protobuf::internal::ExtensionSet const&, int)
Unexecuted instantiation: google::protobuf::internal::RepeatedPrimitiveTypeTraits<double>::GetPtr(int, google::protobuf::internal::ExtensionSet const&, int)
Unexecuted instantiation: google::protobuf::internal::RepeatedPrimitiveTypeTraits<bool>::GetPtr(int, google::protobuf::internal::ExtensionSet const&, int)
1137
  template <>                                                                  \
1138
  inline void RepeatedPrimitiveTypeTraits<TYPE>::Set(                          \
1139
0
      int number, int index, TYPE value, ExtensionSet* set) {                  \
1140
0
    set->SetRepeated##METHOD(number, index, value);                            \
1141
0
  }                                                                            \
Unexecuted instantiation: google::protobuf::internal::RepeatedPrimitiveTypeTraits<int>::Set(int, int, int, google::protobuf::internal::ExtensionSet*)
Unexecuted instantiation: google::protobuf::internal::RepeatedPrimitiveTypeTraits<long>::Set(int, int, long, google::protobuf::internal::ExtensionSet*)
Unexecuted instantiation: google::protobuf::internal::RepeatedPrimitiveTypeTraits<unsigned int>::Set(int, int, unsigned int, google::protobuf::internal::ExtensionSet*)
Unexecuted instantiation: google::protobuf::internal::RepeatedPrimitiveTypeTraits<unsigned long>::Set(int, int, unsigned long, google::protobuf::internal::ExtensionSet*)
Unexecuted instantiation: google::protobuf::internal::RepeatedPrimitiveTypeTraits<float>::Set(int, int, float, google::protobuf::internal::ExtensionSet*)
Unexecuted instantiation: google::protobuf::internal::RepeatedPrimitiveTypeTraits<double>::Set(int, int, double, google::protobuf::internal::ExtensionSet*)
Unexecuted instantiation: google::protobuf::internal::RepeatedPrimitiveTypeTraits<bool>::Set(int, int, bool, google::protobuf::internal::ExtensionSet*)
1142
  template <>                                                                  \
1143
  inline void RepeatedPrimitiveTypeTraits<TYPE>::Add(                          \
1144
      int number, FieldType field_type, bool is_packed, TYPE value,            \
1145
0
      ExtensionSet* set) {                                                     \
1146
0
    set->Add##METHOD(number, field_type, is_packed, value, nullptr);           \
1147
0
  }                                                                            \
Unexecuted instantiation: google::protobuf::internal::RepeatedPrimitiveTypeTraits<int>::Add(int, unsigned char, bool, int, google::protobuf::internal::ExtensionSet*)
Unexecuted instantiation: google::protobuf::internal::RepeatedPrimitiveTypeTraits<long>::Add(int, unsigned char, bool, long, google::protobuf::internal::ExtensionSet*)
Unexecuted instantiation: google::protobuf::internal::RepeatedPrimitiveTypeTraits<unsigned int>::Add(int, unsigned char, bool, unsigned int, google::protobuf::internal::ExtensionSet*)
Unexecuted instantiation: google::protobuf::internal::RepeatedPrimitiveTypeTraits<unsigned long>::Add(int, unsigned char, bool, unsigned long, google::protobuf::internal::ExtensionSet*)
Unexecuted instantiation: google::protobuf::internal::RepeatedPrimitiveTypeTraits<float>::Add(int, unsigned char, bool, float, google::protobuf::internal::ExtensionSet*)
Unexecuted instantiation: google::protobuf::internal::RepeatedPrimitiveTypeTraits<double>::Add(int, unsigned char, bool, double, google::protobuf::internal::ExtensionSet*)
Unexecuted instantiation: google::protobuf::internal::RepeatedPrimitiveTypeTraits<bool>::Add(int, unsigned char, bool, bool, google::protobuf::internal::ExtensionSet*)
1148
  template <>                                                                  \
1149
  inline const RepeatedField<TYPE>*                                            \
1150
0
  RepeatedPrimitiveTypeTraits<TYPE>::GetDefaultRepeatedField() {               \
1151
0
    return &RepeatedPrimitiveDefaults::default_instance()                      \
1152
0
                ->default_repeated_field_##TYPE##_;                            \
1153
0
  }                                                                            \
Unexecuted instantiation: google::protobuf::internal::RepeatedPrimitiveTypeTraits<int>::GetDefaultRepeatedField()
Unexecuted instantiation: google::protobuf::internal::RepeatedPrimitiveTypeTraits<long>::GetDefaultRepeatedField()
Unexecuted instantiation: google::protobuf::internal::RepeatedPrimitiveTypeTraits<unsigned int>::GetDefaultRepeatedField()
Unexecuted instantiation: google::protobuf::internal::RepeatedPrimitiveTypeTraits<unsigned long>::GetDefaultRepeatedField()
Unexecuted instantiation: google::protobuf::internal::RepeatedPrimitiveTypeTraits<float>::GetDefaultRepeatedField()
Unexecuted instantiation: google::protobuf::internal::RepeatedPrimitiveTypeTraits<double>::GetDefaultRepeatedField()
Unexecuted instantiation: google::protobuf::internal::RepeatedPrimitiveTypeTraits<bool>::GetDefaultRepeatedField()
1154
  template <>                                                                  \
1155
  inline const RepeatedField<TYPE>&                                            \
1156
  RepeatedPrimitiveTypeTraits<TYPE>::GetRepeated(int number,                   \
1157
0
                                                 const ExtensionSet& set) {    \
1158
0
    return *reinterpret_cast<const RepeatedField<TYPE>*>(                      \
1159
0
        set.GetRawRepeatedField(number, GetDefaultRepeatedField()));           \
1160
0
  }                                                                            \
Unexecuted instantiation: google::protobuf::internal::RepeatedPrimitiveTypeTraits<int>::GetRepeated(int, google::protobuf::internal::ExtensionSet const&)
Unexecuted instantiation: google::protobuf::internal::RepeatedPrimitiveTypeTraits<long>::GetRepeated(int, google::protobuf::internal::ExtensionSet const&)
Unexecuted instantiation: google::protobuf::internal::RepeatedPrimitiveTypeTraits<unsigned int>::GetRepeated(int, google::protobuf::internal::ExtensionSet const&)
Unexecuted instantiation: google::protobuf::internal::RepeatedPrimitiveTypeTraits<unsigned long>::GetRepeated(int, google::protobuf::internal::ExtensionSet const&)
Unexecuted instantiation: google::protobuf::internal::RepeatedPrimitiveTypeTraits<float>::GetRepeated(int, google::protobuf::internal::ExtensionSet const&)
Unexecuted instantiation: google::protobuf::internal::RepeatedPrimitiveTypeTraits<double>::GetRepeated(int, google::protobuf::internal::ExtensionSet const&)
Unexecuted instantiation: google::protobuf::internal::RepeatedPrimitiveTypeTraits<bool>::GetRepeated(int, google::protobuf::internal::ExtensionSet const&)
1161
  template <>                                                                  \
1162
  inline const RepeatedField<TYPE>*                                            \
1163
  RepeatedPrimitiveTypeTraits<TYPE>::GetRepeatedPtr(int number,                \
1164
0
                                                    const ExtensionSet& set) { \
1165
0
    return &GetRepeated(number, set);                                          \
1166
0
  }                                                                            \
Unexecuted instantiation: google::protobuf::internal::RepeatedPrimitiveTypeTraits<int>::GetRepeatedPtr(int, google::protobuf::internal::ExtensionSet const&)
Unexecuted instantiation: google::protobuf::internal::RepeatedPrimitiveTypeTraits<long>::GetRepeatedPtr(int, google::protobuf::internal::ExtensionSet const&)
Unexecuted instantiation: google::protobuf::internal::RepeatedPrimitiveTypeTraits<unsigned int>::GetRepeatedPtr(int, google::protobuf::internal::ExtensionSet const&)
Unexecuted instantiation: google::protobuf::internal::RepeatedPrimitiveTypeTraits<unsigned long>::GetRepeatedPtr(int, google::protobuf::internal::ExtensionSet const&)
Unexecuted instantiation: google::protobuf::internal::RepeatedPrimitiveTypeTraits<float>::GetRepeatedPtr(int, google::protobuf::internal::ExtensionSet const&)
Unexecuted instantiation: google::protobuf::internal::RepeatedPrimitiveTypeTraits<double>::GetRepeatedPtr(int, google::protobuf::internal::ExtensionSet const&)
Unexecuted instantiation: google::protobuf::internal::RepeatedPrimitiveTypeTraits<bool>::GetRepeatedPtr(int, google::protobuf::internal::ExtensionSet const&)
1167
  template <>                                                                  \
1168
  inline RepeatedField<TYPE>*                                                  \
1169
  RepeatedPrimitiveTypeTraits<TYPE>::MutableRepeated(                          \
1170
0
      int number, FieldType field_type, bool is_packed, ExtensionSet* set) {   \
1171
0
    return reinterpret_cast<RepeatedField<TYPE>*>(                             \
1172
0
        set->MutableRawRepeatedField(number, field_type, is_packed, nullptr)); \
1173
0
  }
Unexecuted instantiation: google::protobuf::internal::RepeatedPrimitiveTypeTraits<int>::MutableRepeated(int, unsigned char, bool, google::protobuf::internal::ExtensionSet*)
Unexecuted instantiation: google::protobuf::internal::RepeatedPrimitiveTypeTraits<long>::MutableRepeated(int, unsigned char, bool, google::protobuf::internal::ExtensionSet*)
Unexecuted instantiation: google::protobuf::internal::RepeatedPrimitiveTypeTraits<unsigned int>::MutableRepeated(int, unsigned char, bool, google::protobuf::internal::ExtensionSet*)
Unexecuted instantiation: google::protobuf::internal::RepeatedPrimitiveTypeTraits<unsigned long>::MutableRepeated(int, unsigned char, bool, google::protobuf::internal::ExtensionSet*)
Unexecuted instantiation: google::protobuf::internal::RepeatedPrimitiveTypeTraits<float>::MutableRepeated(int, unsigned char, bool, google::protobuf::internal::ExtensionSet*)
Unexecuted instantiation: google::protobuf::internal::RepeatedPrimitiveTypeTraits<double>::MutableRepeated(int, unsigned char, bool, google::protobuf::internal::ExtensionSet*)
Unexecuted instantiation: google::protobuf::internal::RepeatedPrimitiveTypeTraits<bool>::MutableRepeated(int, unsigned char, bool, google::protobuf::internal::ExtensionSet*)
1174
1175
PROTOBUF_DEFINE_PRIMITIVE_TYPE(int32_t, Int32)
1176
PROTOBUF_DEFINE_PRIMITIVE_TYPE(int64_t, Int64)
1177
PROTOBUF_DEFINE_PRIMITIVE_TYPE(uint32_t, UInt32)
1178
PROTOBUF_DEFINE_PRIMITIVE_TYPE(uint64_t, UInt64)
1179
PROTOBUF_DEFINE_PRIMITIVE_TYPE(float, Float)
1180
PROTOBUF_DEFINE_PRIMITIVE_TYPE(double, Double)
1181
PROTOBUF_DEFINE_PRIMITIVE_TYPE(bool, Bool)
1182
1183
#undef PROTOBUF_DEFINE_PRIMITIVE_TYPE
1184
1185
// -------------------------------------------------------------------
1186
// StringTypeTraits
1187
1188
// Strings support both Set() and Mutable().
1189
class PROTOBUF_EXPORT StringTypeTraits {
1190
 public:
1191
  typedef const std::string& ConstType;
1192
  typedef std::string* MutableType;
1193
  using InitType = ConstType;
1194
0
  static ConstType FromInitType(InitType v) { return v; }
1195
  typedef StringTypeTraits Singular;
1196
  static constexpr bool kLifetimeBound = true;
1197
1198
  static inline const std::string& Get(int number, const ExtensionSet& set,
1199
0
                                       ConstType default_value) {
1200
0
    return set.GetString(number, default_value);
1201
0
  }
1202
  static inline const std::string* GetPtr(int number, const ExtensionSet& set,
1203
0
                                          ConstType default_value) {
1204
0
    return &Get(number, set, default_value);
1205
0
  }
1206
  static inline void Set(int number, FieldType field_type,
1207
0
                         const std::string& value, ExtensionSet* set) {
1208
0
    set->SetString(number, field_type, value, nullptr);
1209
0
  }
1210
  static inline std::string* Mutable(int number, FieldType field_type,
1211
0
                                     ExtensionSet* set) {
1212
0
    return set->MutableString(number, field_type, nullptr);
1213
0
  }
1214
};
1215
1216
class PROTOBUF_EXPORT RepeatedStringTypeTraits {
1217
 public:
1218
  typedef const std::string& ConstType;
1219
  typedef std::string* MutableType;
1220
  using InitType = ConstType;
1221
0
  static ConstType FromInitType(InitType v) { return v; }
1222
  typedef RepeatedStringTypeTraits Repeated;
1223
  static constexpr bool kLifetimeBound = true;
1224
1225
  typedef RepeatedPtrField<std::string> RepeatedFieldType;
1226
1227
  static inline const std::string& Get(int number, const ExtensionSet& set,
1228
0
                                       int index) {
1229
0
    return set.GetRepeatedString(number, index);
1230
0
  }
1231
  static inline const std::string* GetPtr(int number, const ExtensionSet& set,
1232
0
                                          int index) {
1233
0
    return &Get(number, set, index);
1234
0
  }
1235
  static inline const RepeatedPtrField<std::string>* GetRepeatedPtr(
1236
0
      int number, const ExtensionSet& set) {
1237
0
    return &GetRepeated(number, set);
1238
0
  }
1239
  static inline void Set(int number, int index, const std::string& value,
1240
0
                         ExtensionSet* set) {
1241
0
    set->SetRepeatedString(number, index, value);
1242
0
  }
1243
0
  static inline std::string* Mutable(int number, int index, ExtensionSet* set) {
1244
0
    return set->MutableRepeatedString(number, index);
1245
0
  }
1246
  static inline void Add(int number, FieldType field_type, bool /*is_packed*/,
1247
0
                         const std::string& value, ExtensionSet* set) {
1248
0
    set->AddString(number, field_type, value, nullptr);
1249
0
  }
1250
  static inline std::string* Add(int number, FieldType field_type,
1251
0
                                 ExtensionSet* set) {
1252
0
    return set->AddString(number, field_type, nullptr);
1253
0
  }
1254
  static inline const RepeatedPtrField<std::string>& GetRepeated(
1255
0
      int number, const ExtensionSet& set) {
1256
0
    return *reinterpret_cast<const RepeatedPtrField<std::string>*>(
1257
0
        set.GetRawRepeatedField(number, GetDefaultRepeatedField()));
1258
0
  }
1259
1260
  static inline RepeatedPtrField<std::string>* MutableRepeated(
1261
0
      int number, FieldType field_type, bool is_packed, ExtensionSet* set) {
1262
0
    return reinterpret_cast<RepeatedPtrField<std::string>*>(
1263
0
        set->MutableRawRepeatedField(number, field_type, is_packed, nullptr));
1264
0
  }
1265
1266
  static const RepeatedFieldType* GetDefaultRepeatedField();
1267
1268
 private:
1269
  static void InitializeDefaultRepeatedFields();
1270
  static void DestroyDefaultRepeatedFields();
1271
};
1272
1273
// -------------------------------------------------------------------
1274
// EnumTypeTraits
1275
1276
// ExtensionSet represents enums using integers internally, so we have to
1277
// static_cast around.
1278
template <typename Type, bool IsValid(int)>
1279
class EnumTypeTraits {
1280
 public:
1281
  typedef Type ConstType;
1282
  typedef Type MutableType;
1283
  using InitType = ConstType;
1284
  static const ConstType& FromInitType(const InitType& v) { return v; }
1285
  typedef EnumTypeTraits<Type, IsValid> Singular;
1286
  static constexpr bool kLifetimeBound = false;
1287
1288
  static inline ConstType Get(int number, const ExtensionSet& set,
1289
                              ConstType default_value) {
1290
    return static_cast<Type>(set.GetEnum(number, default_value));
1291
  }
1292
  static inline const ConstType* GetPtr(int number, const ExtensionSet& set,
1293
                                        const ConstType& default_value) {
1294
    return reinterpret_cast<const Type*>(
1295
        &set.GetRefEnum(number, default_value));
1296
  }
1297
  static inline void Set(int number, FieldType field_type, ConstType value,
1298
                         ExtensionSet* set) {
1299
    ABSL_DCHECK(IsValid(value));
1300
    set->SetEnum(number, field_type, value, nullptr);
1301
  }
1302
};
1303
1304
template <typename Type, bool IsValid(int)>
1305
class RepeatedEnumTypeTraits {
1306
 public:
1307
  typedef Type ConstType;
1308
  typedef Type MutableType;
1309
  using InitType = ConstType;
1310
  static const ConstType& FromInitType(const InitType& v) { return v; }
1311
  typedef RepeatedEnumTypeTraits<Type, IsValid> Repeated;
1312
  static constexpr bool kLifetimeBound = false;
1313
1314
  typedef RepeatedField<Type> RepeatedFieldType;
1315
1316
  static inline ConstType Get(int number, const ExtensionSet& set, int index) {
1317
    return static_cast<Type>(set.GetRepeatedEnum(number, index));
1318
  }
1319
  static inline const ConstType* GetPtr(int number, const ExtensionSet& set,
1320
                                        int index) {
1321
    return reinterpret_cast<const Type*>(
1322
        &set.GetRefRepeatedEnum(number, index));
1323
  }
1324
  static inline void Set(int number, int index, ConstType value,
1325
                         ExtensionSet* set) {
1326
    ABSL_DCHECK(IsValid(value));
1327
    set->SetRepeatedEnum(number, index, value);
1328
  }
1329
  static inline void Add(int number, FieldType field_type, bool is_packed,
1330
                         ConstType value, ExtensionSet* set) {
1331
    ABSL_DCHECK(IsValid(value));
1332
    set->AddEnum(number, field_type, is_packed, value, nullptr);
1333
  }
1334
  static inline const RepeatedField<Type>& GetRepeated(
1335
      int number, const ExtensionSet& set) {
1336
    // Hack: the `Extension` struct stores a RepeatedField<int> for enums.
1337
    // RepeatedField<int> cannot implicitly convert to RepeatedField<EnumType>
1338
    // so we need to do some casting magic. See message.h for similar
1339
    // contortions for non-extension fields.
1340
    return *reinterpret_cast<const RepeatedField<Type>*>(
1341
        set.GetRawRepeatedField(number, GetDefaultRepeatedField()));
1342
  }
1343
  static inline const RepeatedField<Type>* GetRepeatedPtr(
1344
      int number, const ExtensionSet& set) {
1345
    return &GetRepeated(number, set);
1346
  }
1347
  static inline RepeatedField<Type>* MutableRepeated(int number,
1348
                                                     FieldType field_type,
1349
                                                     bool is_packed,
1350
                                                     ExtensionSet* set) {
1351
    return reinterpret_cast<RepeatedField<Type>*>(
1352
        set->MutableRawRepeatedField(number, field_type, is_packed, nullptr));
1353
  }
1354
1355
  static const RepeatedFieldType* GetDefaultRepeatedField() {
1356
    // Hack: as noted above, repeated enum fields are internally stored as a
1357
    // RepeatedField<int>. We need to be able to instantiate global static
1358
    // objects to return as default (empty) repeated fields on non-existent
1359
    // extensions. We would not be able to know a-priori all of the enum types
1360
    // (values of |Type|) to instantiate all of these, so we just re-use
1361
    // int32_t's default repeated field object.
1362
    return reinterpret_cast<const RepeatedField<Type>*>(
1363
        RepeatedPrimitiveTypeTraits<int32_t>::GetDefaultRepeatedField());
1364
  }
1365
};
1366
1367
// -------------------------------------------------------------------
1368
// MessageTypeTraits
1369
1370
// ExtensionSet guarantees that when manipulating extensions with message
1371
// types, the implementation used will be the compiled-in class representing
1372
// that type.  So, we can static_cast down to the exact type we expect.
1373
template <typename Type>
1374
class MessageTypeTraits {
1375
 public:
1376
  typedef const Type& ConstType;
1377
  typedef Type* MutableType;
1378
  using InitType = const void*;
1379
  static ConstType FromInitType(InitType v) {
1380
    return *static_cast<const Type*>(v);
1381
  }
1382
  typedef MessageTypeTraits<Type> Singular;
1383
  static constexpr bool kLifetimeBound = true;
1384
1385
  static inline ConstType Get(int number, const ExtensionSet& set,
1386
                              ConstType default_value) {
1387
    return static_cast<const Type&>(set.GetMessage(number, default_value));
1388
  }
1389
  static inline std::nullptr_t GetPtr(int /* number */,
1390
                                      const ExtensionSet& /* set */,
1391
                                      ConstType /* default_value */) {
1392
    // Cannot be implemented because of forward declared messages?
1393
    return nullptr;
1394
  }
1395
  static inline MutableType Mutable(int number, FieldType field_type,
1396
                                    ExtensionSet* set) {
1397
    return static_cast<Type*>(set->MutableMessage(
1398
        number, field_type, Type::default_instance(), nullptr));
1399
  }
1400
  static inline void SetAllocated(int number, FieldType field_type,
1401
                                  MutableType message, ExtensionSet* set) {
1402
    set->SetAllocatedMessage(number, field_type, nullptr, message);
1403
  }
1404
  static inline void UnsafeArenaSetAllocated(int number, FieldType field_type,
1405
                                             MutableType message,
1406
                                             ExtensionSet* set) {
1407
    set->UnsafeArenaSetAllocatedMessage(number, field_type, nullptr, message);
1408
  }
1409
  PROTOBUF_NODISCARD static inline MutableType Release(
1410
      int number, FieldType /* field_type */, ExtensionSet* set) {
1411
    return static_cast<Type*>(
1412
        set->ReleaseMessage(number, Type::default_instance()));
1413
  }
1414
  static inline MutableType UnsafeArenaRelease(int number,
1415
                                               FieldType /* field_type */,
1416
                                               ExtensionSet* set) {
1417
    return static_cast<Type*>(
1418
        set->UnsafeArenaReleaseMessage(number, Type::default_instance()));
1419
  }
1420
};
1421
1422
// Used by WireFormatVerify to extract the verify function from the registry.
1423
LazyEagerVerifyFnType FindExtensionLazyEagerVerifyFn(
1424
    const MessageLite* extendee, int number);
1425
1426
// forward declaration.
1427
class RepeatedMessageGenericTypeTraits;
1428
1429
template <typename Type>
1430
class RepeatedMessageTypeTraits {
1431
 public:
1432
  typedef const Type& ConstType;
1433
  typedef Type* MutableType;
1434
  using InitType = const void*;
1435
  static ConstType FromInitType(InitType v) {
1436
    return *static_cast<const Type*>(v);
1437
  }
1438
  typedef RepeatedMessageTypeTraits<Type> Repeated;
1439
  static constexpr bool kLifetimeBound = true;
1440
1441
  typedef RepeatedPtrField<Type> RepeatedFieldType;
1442
1443
  static inline ConstType Get(int number, const ExtensionSet& set, int index) {
1444
    return static_cast<const Type&>(set.GetRepeatedMessage(number, index));
1445
  }
1446
  static inline std::nullptr_t GetPtr(int /* number */,
1447
                                      const ExtensionSet& /* set */,
1448
                                      int /* index */) {
1449
    // Cannot be implemented because of forward declared messages?
1450
    return nullptr;
1451
  }
1452
  static inline std::nullptr_t GetRepeatedPtr(int /* number */,
1453
                                              const ExtensionSet& /* set */) {
1454
    // Cannot be implemented because of forward declared messages?
1455
    return nullptr;
1456
  }
1457
  static inline MutableType Mutable(int number, int index, ExtensionSet* set) {
1458
    return static_cast<Type*>(set->MutableRepeatedMessage(number, index));
1459
  }
1460
  static inline MutableType Add(int number, FieldType field_type,
1461
                                ExtensionSet* set) {
1462
    return static_cast<Type*>(
1463
        set->AddMessage(number, field_type, Type::default_instance(), nullptr));
1464
  }
1465
  static inline const RepeatedPtrField<Type>& GetRepeated(
1466
      int number, const ExtensionSet& set) {
1467
    // See notes above in RepeatedEnumTypeTraits::GetRepeated(): same
1468
    // casting hack applies here, because a RepeatedPtrField<MessageLite>
1469
    // cannot naturally become a RepeatedPtrType<Type> even though Type is
1470
    // presumably a message. google::protobuf::Message goes through similar contortions
1471
    // with a reinterpret_cast<>.
1472
    return *reinterpret_cast<const RepeatedPtrField<Type>*>(
1473
        set.GetRawRepeatedField(number, GetDefaultRepeatedField()));
1474
  }
1475
  static inline RepeatedPtrField<Type>* MutableRepeated(int number,
1476
                                                        FieldType field_type,
1477
                                                        bool is_packed,
1478
                                                        ExtensionSet* set) {
1479
    return reinterpret_cast<RepeatedPtrField<Type>*>(
1480
        set->MutableRawRepeatedField(number, field_type, is_packed, nullptr));
1481
  }
1482
1483
  static const RepeatedFieldType* GetDefaultRepeatedField();
1484
};
1485
1486
template <typename Type>
1487
inline const typename RepeatedMessageTypeTraits<Type>::RepeatedFieldType*
1488
RepeatedMessageTypeTraits<Type>::GetDefaultRepeatedField() {
1489
  static auto instance = OnShutdownDelete(new RepeatedFieldType);
1490
  return instance;
1491
}
1492
1493
// -------------------------------------------------------------------
1494
// ExtensionIdentifier
1495
1496
// This is the type of actual extension objects.  E.g. if you have:
1497
//   extend Foo {
1498
//     optional int32 bar = 1234;
1499
//   }
1500
// then "bar" will be defined in C++ as:
1501
//   ExtensionIdentifier<Foo, PrimitiveTypeTraits<int32_t>, 5, false> bar(1234);
1502
//
1503
// Note that we could, in theory, supply the field number as a template
1504
// parameter, and thus make an instance of ExtensionIdentifier have no
1505
// actual contents.  However, if we did that, then using an extension
1506
// identifier would not necessarily cause the compiler to output any sort
1507
// of reference to any symbol defined in the extension's .pb.o file.  Some
1508
// linkers will actually drop object files that are not explicitly referenced,
1509
// but that would be bad because it would cause this extension to not be
1510
// registered at static initialization, and therefore using it would crash.
1511
1512
template <typename ExtendeeType, typename TypeTraitsType, FieldType field_type,
1513
          bool is_packed>
1514
class ExtensionIdentifier {
1515
 public:
1516
  typedef TypeTraitsType TypeTraits;
1517
  typedef ExtendeeType Extendee;
1518
1519
  constexpr ExtensionIdentifier(int number,
1520
                                typename TypeTraits::InitType default_value)
1521
      : number_(number), default_value_(default_value) {}
1522
1523
  inline int number() const { return number_; }
1524
  typename TypeTraits::ConstType default_value() const {
1525
    return TypeTraits::FromInitType(default_value_);
1526
  }
1527
1528
  typename TypeTraits::ConstType const& default_value_ref() const {
1529
    return TypeTraits::FromInitType(default_value_);
1530
  }
1531
1532
 private:
1533
  const int number_;
1534
  typename TypeTraits::InitType default_value_;
1535
};
1536
1537
// -------------------------------------------------------------------
1538
// Generated accessors
1539
1540
1541
}  // namespace internal
1542
1543
// Call this function to ensure that this extensions's reflection is linked into
1544
// the binary:
1545
//
1546
//   google::protobuf::LinkExtensionReflection(Foo::my_extension);
1547
//
1548
// This will ensure that the following lookup will succeed:
1549
//
1550
//   DescriptorPool::generated_pool()->FindExtensionByName("Foo.my_extension");
1551
//
1552
// This is often relevant for parsing extensions in text mode.
1553
//
1554
// As a side-effect, it will also guarantee that anything else from the same
1555
// .proto file will also be available for lookup in the generated pool.
1556
//
1557
// This function does not actually register the extension, so it does not need
1558
// to be called before the lookup.  However it does need to occur in a function
1559
// that cannot be stripped from the binary (ie. it must be reachable from main).
1560
//
1561
// Best practice is to call this function as close as possible to where the
1562
// reflection is actually needed.  This function is very cheap to call, so you
1563
// should not need to worry about its runtime overhead except in tight loops (on
1564
// x86-64 it compiles into two "mov" instructions).
1565
template <typename ExtendeeType, typename TypeTraitsType,
1566
          internal::FieldType field_type, bool is_packed>
1567
void LinkExtensionReflection(
1568
    const google::protobuf::internal::ExtensionIdentifier<
1569
        ExtendeeType, TypeTraitsType, field_type, is_packed>& extension) {
1570
  internal::StrongReference(extension);
1571
}
1572
1573
// Returns the field descriptor for a generated extension identifier.  This is
1574
// useful when doing reflection over generated extensions.
1575
template <typename ExtendeeType, typename TypeTraitsType,
1576
          internal::FieldType field_type, bool is_packed,
1577
          typename PoolType = DescriptorPool>
1578
const FieldDescriptor* GetExtensionReflection(
1579
    const google::protobuf::internal::ExtensionIdentifier<
1580
        ExtendeeType, TypeTraitsType, field_type, is_packed>& extension) {
1581
  return PoolType::generated_pool()->FindExtensionByNumber(
1582
      google::protobuf::internal::ExtensionIdentifier<ExtendeeType, TypeTraitsType,
1583
                                            field_type,
1584
                                            is_packed>::Extendee::descriptor(),
1585
      extension.number());
1586
}
1587
1588
}  // namespace protobuf
1589
}  // namespace google
1590
1591
#include "google/protobuf/port_undef.inc"
1592
1593
#endif  // GOOGLE_PROTOBUF_EXTENSION_SET_H__