GPBDescriptor_PackagePrivate.h
Go to the documentation of this file.
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 // This header is private to the ProtobolBuffers library and must NOT be
32 // included by any sources outside this library. The contents of this file are
33 // subject to change at any time without notice.
34 
35 #import "GPBDescriptor.h"
36 #import "GPBWireFormat.h"
37 
38 // Describes attributes of the field.
39 typedef NS_OPTIONS(uint16_t, GPBFieldFlags) {
40  GPBFieldNone = 0,
41  // These map to standard protobuf concepts.
42  GPBFieldRequired = 1 << 0,
43  GPBFieldRepeated = 1 << 1,
44  GPBFieldPacked = 1 << 2,
45  GPBFieldOptional = 1 << 3,
46  GPBFieldHasDefaultValue = 1 << 4,
47 
48  // Indicates the field needs custom handling for the TextFormat name, if not
49  // set, the name can be derived from the ObjC name.
50  GPBFieldTextFormatNameCustom = 1 << 6,
51  // Indicates the field has an enum descriptor.
52  GPBFieldHasEnumDescriptor = 1 << 7,
53 
54  // These are not standard protobuf concepts, they are specific to the
55  // Objective C runtime.
56 
57  // These bits are used to mark the field as a map and what the key
58  // type is.
59  GPBFieldMapKeyMask = 0xF << 8,
60  GPBFieldMapKeyInt32 = 1 << 8,
61  GPBFieldMapKeyInt64 = 2 << 8,
62  GPBFieldMapKeyUInt32 = 3 << 8,
63  GPBFieldMapKeyUInt64 = 4 << 8,
64  GPBFieldMapKeySInt32 = 5 << 8,
65  GPBFieldMapKeySInt64 = 6 << 8,
66  GPBFieldMapKeyFixed32 = 7 << 8,
67  GPBFieldMapKeyFixed64 = 8 << 8,
68  GPBFieldMapKeySFixed32 = 9 << 8,
69  GPBFieldMapKeySFixed64 = 10 << 8,
70  GPBFieldMapKeyBool = 11 << 8,
71  GPBFieldMapKeyString = 12 << 8,
72 };
73 
74 // NOTE: The structures defined here have their members ordered to minimize
75 // their size. This directly impacts the size of apps since these exist per
76 // field/extension.
77 
78 // Describes a single field in a protobuf as it is represented as an ivar.
80  // Name of ivar.
81  const char *name;
82  union {
83  const char *className; // Name for message class.
84  // For enums only: If EnumDescriptors are compiled in, it will be that,
85  // otherwise it will be the verifier.
89  // The field number for the ivar.
90  uint32_t number;
91  // The index (in bits) into _has_storage_.
92  // >= 0: the bit to use for a value being set.
93  // = GPBNoHasBit(INT32_MAX): no storage used.
94  // < 0: in a oneOf, use a full int32 to record the field active.
95  int32_t hasIndex;
96  // Offset of the variable into it's structure struct.
97  uint32_t offset;
98  // Field flags. Use accessor functions below.
99  GPBFieldFlags flags;
100  // Data type of the ivar.
101  GPBDataType dataType;
103 
104 // Fields in messages defined in a 'proto2' syntax file can provide a default
105 // value. This struct provides the default along with the field info.
107  // Default value for the ivar.
109 
112 
113 // Describes attributes of the extension.
114 typedef NS_OPTIONS(uint8_t, GPBExtensionOptions) {
115  GPBExtensionNone = 0,
116  // These map to standard protobuf concepts.
117  GPBExtensionRepeated = 1 << 0,
118  GPBExtensionPacked = 1 << 1,
119  GPBExtensionSetWireFormat = 1 << 2,
120 };
121 
122 // An extension
123 typedef struct GPBExtensionDescription {
125  const char *singletonName;
126  const char *extendedClass;
129  int32_t fieldNumber;
130  GPBDataType dataType;
131  GPBExtensionOptions options;
133 
134 typedef NS_OPTIONS(uint32_t, GPBDescriptorInitializationFlags) {
135  GPBDescriptorInitializationFlag_None = 0,
136  GPBDescriptorInitializationFlag_FieldsWithDefault = 1 << 0,
137  GPBDescriptorInitializationFlag_WireFormat = 1 << 1,
138 };
139 
140 @interface GPBDescriptor () {
141  @package
142  NSArray *fields_;
143  NSArray *oneofs_;
144  uint32_t storageSize_;
145 }
146 
147 // fieldDescriptions have to be long lived, they are held as raw pointers.
148 + (instancetype)
149  allocDescriptorForClass:(Class)messageClass
150  rootClass:(Class)rootClass
151  file:(GPBFileDescriptor *)file
152  fields:(void *)fieldDescriptions
153  fieldCount:(uint32_t)fieldCount
154  storageSize:(uint32_t)storageSize
155  flags:(GPBDescriptorInitializationFlags)flags;
156 
157 - (instancetype)initWithClass:(Class)messageClass
158  file:(GPBFileDescriptor *)file
159  fields:(NSArray *)fields
160  storageSize:(uint32_t)storage
161  wireFormat:(BOOL)wireFormat;
162 
163 // Called right after init to provide extra information to avoid init having
164 // an explosion of args. These pointers are recorded, so they are expected
165 // to live for the lifetime of the app.
166 - (void)setupOneofs:(const char **)oneofNames
167  count:(uint32_t)count
168  firstHasIndex:(int32_t)firstHasIndex;
169 - (void)setupExtraTextInfo:(const char *)extraTextFormatInfo;
170 - (void)setupExtensionRanges:(const GPBExtensionRange *)ranges count:(int32_t)count;
171 - (void)setupContainingMessageClassName:(const char *)msgClassName;
172 - (void)setupMessageClassNameSuffix:(NSString *)suffix;
173 
174 @end
175 
176 @interface GPBFileDescriptor ()
177 - (instancetype)initWithPackage:(NSString *)package
178  objcPrefix:(NSString *)objcPrefix
179  syntax:(GPBFileSyntax)syntax;
180 - (instancetype)initWithPackage:(NSString *)package
181  syntax:(GPBFileSyntax)syntax;
182 @end
183 
184 @interface GPBOneofDescriptor () {
185  @package
186  const char *name_;
187  NSArray *fields_;
188  SEL caseSel_;
189 }
190 // name must be long lived.
191 - (instancetype)initWithName:(const char *)name fields:(NSArray *)fields;
192 @end
193 
194 @interface GPBFieldDescriptor () {
195  @package
196  GPBMessageFieldDescription *description_;
198 
199  SEL getSel_;
200  SEL setSel_;
201  SEL hasOrCountSel_; // *Count for map<>/repeated fields, has* otherwise.
203 }
204 
205 // Single initializer
206 // description has to be long lived, it is held as a raw pointer.
207 - (instancetype)initWithFieldDescription:(void *)description
208  includesDefault:(BOOL)includesDefault
209  syntax:(GPBFileSyntax)syntax;
210 @end
211 
212 @interface GPBEnumDescriptor ()
213 // valueNames, values and extraTextFormatInfo have to be long lived, they are
214 // held as raw pointers.
215 + (instancetype)
216  allocDescriptorForName:(NSString *)name
217  valueNames:(const char *)valueNames
218  values:(const int32_t *)values
219  count:(uint32_t)valueCount
220  enumVerifier:(GPBEnumValidationFunc)enumVerifier;
221 + (instancetype)
222  allocDescriptorForName:(NSString *)name
223  valueNames:(const char *)valueNames
224  values:(const int32_t *)values
225  count:(uint32_t)valueCount
226  enumVerifier:(GPBEnumValidationFunc)enumVerifier
227  extraTextFormatInfo:(const char *)extraTextFormatInfo;
228 
229 - (instancetype)initWithName:(NSString *)name
230  valueNames:(const char *)valueNames
231  values:(const int32_t *)values
232  count:(uint32_t)valueCount
233  enumVerifier:(GPBEnumValidationFunc)enumVerifier;
234 @end
235 
236 @interface GPBExtensionDescriptor () {
237  @package
238  GPBExtensionDescription *description_;
239 }
240 @property(nonatomic, readonly) GPBWireFormat wireType;
241 
242 // For repeated extensions, alternateWireType is the wireType with the opposite
243 // value for the packable property. i.e. - if the extension was marked packed
244 // it would be the wire type for unpacked; if the extension was marked unpacked,
245 // it would be the wire type for packed.
246 @property(nonatomic, readonly) GPBWireFormat alternateWireType;
247 
248 // description has to be long lived, it is held as a raw pointer.
249 - (instancetype)initWithExtensionDescription:
250  (GPBExtensionDescription *)description;
251 - (NSComparisonResult)compareByFieldNumber:(GPBExtensionDescriptor *)other;
252 @end
253 
254 CF_EXTERN_C_BEGIN
255 
256 // Direct access is use for speed, to avoid even internally declaring things
257 // read/write, etc. The warning is enabled in the project to ensure code calling
258 // protos can turn on -Wdirect-ivar-access without issues.
259 #pragma clang diagnostic push
260 #pragma clang diagnostic ignored "-Wdirect-ivar-access"
261 
263  return (field->description_->flags &
264  (GPBFieldRepeated | GPBFieldMapKeyMask)) != 0;
265 }
266 
268  return field->description_->dataType;
269 }
270 
272  return field->description_->hasIndex;
273 }
274 
276  return field->description_->number;
277 }
278 
279 #pragma clang diagnostic pop
280 
281 uint32_t GPBFieldTag(GPBFieldDescriptor *self);
282 
283 // For repeated fields, alternateWireType is the wireType with the opposite
284 // value for the packable property. i.e. - if the field was marked packed it
285 // would be the wire type for unpacked; if the field was marked unpacked, it
286 // would be the wire type for packed.
288 
290  return syntax == GPBFileSyntaxProto3;
291 }
292 
294  return (description->options & GPBExtensionRepeated) != 0;
295 }
296 
298  return (description->options & GPBExtensionPacked) != 0;
299 }
300 
302  return (description->options & GPBExtensionSetWireFormat) != 0;
303 }
304 
305 // Helper for compile time assets.
306 #ifndef GPBInternalCompileAssert
307  #if __has_feature(c_static_assert) || __has_extension(c_static_assert)
308  #define GPBInternalCompileAssert(test, msg) _Static_assert((test), #msg)
309  #else
310  // Pre-Xcode 7 support.
311  #define GPBInternalCompileAssertSymbolInner(line, msg) GPBInternalCompileAssert ## line ## __ ## msg
312  #define GPBInternalCompileAssertSymbol(line, msg) GPBInternalCompileAssertSymbolInner(line, msg)
313  #define GPBInternalCompileAssert(test, msg) \
314  typedef char GPBInternalCompileAssertSymbol(__LINE__, msg) [ ((test) ? 1 : -1) ]
315  #endif // __has_feature(c_static_assert) || __has_extension(c_static_assert)
316 #endif // GPBInternalCompileAssert
317 
318 // Sanity check that there isn't padding between the field description
319 // structures with and without a default.
321  (sizeof(GPBGenericValue) +
323  DescriptionsWithDefault_different_size_than_expected);
324 
325 CF_EXTERN_C_END
GPBMessageFieldDescriptionWithDefault
Definition: GPBDescriptor_PackagePrivate.h:106
GPBFieldDescriptor::containingOneof_
GPB_UNSAFE_UNRETAINED GPBOneofDescriptor * containingOneof_
Definition: GPBDescriptor_PackagePrivate.h:197
GPBExtensionDescription::options
GPBExtensionOptions options
Definition: GPBDescriptor_PackagePrivate.h:131
GPBOneofDescriptor::caseSel_
SEL caseSel_
Definition: GPBDescriptor_PackagePrivate.h:188
GPBDescriptor::storageSize_
uint32_t storageSize_
Definition: GPBDescriptor_PackagePrivate.h:144
GPBFieldAlternateTag
uint32_t GPBFieldAlternateTag(GPBFieldDescriptor *self)
benchmarks.python.py_benchmark.const
const
Definition: py_benchmark.py:14
GPBExtensionDescription::singletonName
const char * singletonName
Definition: GPBDescriptor_PackagePrivate.h:125
GPBOneofDescriptor
Definition: GPBDescriptor.h:138
GPBFileDescriptor
Definition: GPBDescriptor.h:124
GPBDescriptor::oneofs_
NSArray * oneofs_
Definition: GPBDescriptor_PackagePrivate.h:143
GPBFieldDescriptor::setSel_
SEL setSel_
Definition: GPBDescriptor_PackagePrivate.h:200
GPBWireFormat.h
GPBFieldHasIndex
GPB_INLINE int32_t GPBFieldHasIndex(GPBFieldDescriptor *field)
Definition: GPBDescriptor_PackagePrivate.h:271
GPBOneofDescriptor::fields_
NSArray * fields_
Definition: GPBDescriptor_PackagePrivate.h:187
GPBExtensionIsRepeated
GPB_INLINE BOOL GPBExtensionIsRepeated(GPBExtensionDescription *description)
Definition: GPBDescriptor_PackagePrivate.h:293
GPBExtensionDescription::messageOrGroupClassName
const char * messageOrGroupClassName
Definition: GPBDescriptor_PackagePrivate.h:127
setup.description
description
Definition: compatibility_tests/v2.5.0/setup.py:61
GPBDescriptor
Definition: GPBDescriptor.h:65
GPBExtensionIsWireFormat
GPB_INLINE BOOL GPBExtensionIsWireFormat(GPBExtensionDescription *description)
Definition: GPBDescriptor_PackagePrivate.h:301
GPBMessageFieldDescription::number
uint32_t number
Definition: GPBDescriptor_PackagePrivate.h:90
GPBHasPreservingUnknownEnumSemantics
GPB_INLINE BOOL GPBHasPreservingUnknownEnumSemantics(GPBFileSyntax syntax)
Definition: GPBDescriptor_PackagePrivate.h:289
GPBExtensionDescription::extendedClass
const char * extendedClass
Definition: GPBDescriptor_PackagePrivate.h:126
GPBMessageFieldDescriptionWithDefault
struct GPBMessageFieldDescriptionWithDefault GPBMessageFieldDescriptionWithDefault
GPB_UNSAFE_UNRETAINED
#define GPB_UNSAFE_UNRETAINED
Definition: GPBBootstrap.h:90
GPBMessageFieldDescriptionWithDefault::core
GPBMessageFieldDescription core
Definition: GPBDescriptor_PackagePrivate.h:110
GPBFieldTag
uint32_t GPBFieldTag(GPBFieldDescriptor *self)
GPBWireFormat
GPBWireFormat
Definition: GPBWireFormat.h:37
NS_OPTIONS
typedef NS_OPTIONS(uint16_t, GPBFieldFlags)
Definition: GPBDescriptor_PackagePrivate.h:39
GPBEnumValidationFunc
BOOL(* GPBEnumValidationFunc)(int32_t)
Definition: GPBRuntimeTypes.h:42
GPBFieldDescriptor::setHasSel_
SEL setHasSel_
Definition: GPBDescriptor_PackagePrivate.h:202
GPBMessageFieldDescriptionWithDefault::defaultValue
GPBGenericValue defaultValue
Definition: GPBDescriptor_PackagePrivate.h:108
GPBFieldIsMapOrArray
CF_EXTERN_C_BEGIN GPB_INLINE BOOL GPBFieldIsMapOrArray(GPBFieldDescriptor *field)
Definition: GPBDescriptor_PackagePrivate.h:262
GPBExtensionDescription
struct GPBExtensionDescription GPBExtensionDescription
GPB_INLINE
#define GPB_INLINE
Definition: GPBBootstrap.h:78
GPBExtensionDescription
Definition: GPBDescriptor_PackagePrivate.h:123
GPBMessageFieldDescription::dataTypeSpecific
union GPBMessageFieldDescription::@80 dataTypeSpecific
GPBExtensionDescription::fieldNumber
int32_t fieldNumber
Definition: GPBDescriptor_PackagePrivate.h:129
GPBFieldDescriptor::hasOrCountSel_
SEL hasOrCountSel_
Definition: GPBDescriptor_PackagePrivate.h:201
GPBExtensionRange
Definition: GPBRuntimeTypes.h:139
field
const FieldDescriptor * field
Definition: parser_unittest.cc:2694
GPBMessageFieldDescription::hasIndex
int32_t hasIndex
Definition: GPBDescriptor_PackagePrivate.h:95
void
typedef void(APIENTRY *GLDEBUGPROCARB)(GLenum source
GPBExtensionIsPacked
GPB_INLINE BOOL GPBExtensionIsPacked(GPBExtensionDescription *description)
Definition: GPBDescriptor_PackagePrivate.h:297
GPBExtensionDescription::defaultValue
GPBGenericValue defaultValue
Definition: GPBDescriptor_PackagePrivate.h:124
GPBDescriptor.h
GPBFieldDescriptor
Definition: GPBDescriptor.h:167
GPBMessageFieldDescription
struct GPBMessageFieldDescription GPBMessageFieldDescription
GPBExtensionDescriptor
Definition: GPBDescriptor.h:296
GPBMessageFieldDescription
Definition: GPBDescriptor_PackagePrivate.h:79
GPBMessageFieldDescription::name
const char * name
Definition: GPBDescriptor_PackagePrivate.h:81
GPBMessageFieldDescription::flags
GPBFieldFlags flags
Definition: GPBDescriptor_PackagePrivate.h:99
GPBExtensionDescription::dataType
GPBDataType dataType
Definition: GPBDescriptor_PackagePrivate.h:130
GPBInternalCompileAssert
#define GPBInternalCompileAssert(test, msg)
Definition: GPBDescriptor_PackagePrivate.h:313
GPBMessageFieldDescription::className
const char * className
Definition: GPBDescriptor_PackagePrivate.h:83
GPBFieldNumber
GPB_INLINE uint32_t GPBFieldNumber(GPBFieldDescriptor *field)
Definition: GPBDescriptor_PackagePrivate.h:275
GPBExtensionDescription::enumDescriptorFunc
GPBEnumDescriptorFunc enumDescriptorFunc
Definition: GPBDescriptor_PackagePrivate.h:128
GPBEnumDescriptorFunc
GPBEnumDescriptor *(* GPBEnumDescriptorFunc)(void)
Definition: GPBRuntimeTypes.h:47
GPBMessageFieldDescription::enumVerifier
GPBEnumValidationFunc enumVerifier
Definition: GPBDescriptor_PackagePrivate.h:87
GPBMessageFieldDescription::offset
uint32_t offset
Definition: GPBDescriptor_PackagePrivate.h:97
GPBGenericValue
Definition: GPBRuntimeTypes.h:61
GPBEnumDescriptor
Definition: GPBDescriptor.h:216
GPBMessageFieldDescription::enumDescFunc
GPBEnumDescriptorFunc enumDescFunc
Definition: GPBDescriptor_PackagePrivate.h:86
GPBMessageFieldDescription::dataType
GPBDataType dataType
Definition: GPBDescriptor_PackagePrivate.h:101
GPBGetFieldDataType
GPB_INLINE GPBDataType GPBGetFieldDataType(GPBFieldDescriptor *field)
Definition: GPBDescriptor_PackagePrivate.h:267
GPBFieldDescriptor::getSel_
SEL getSel_
Definition: GPBDescriptor_PackagePrivate.h:199


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:52