Coverage Report

Created: 2023-06-07 07:09

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