1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file or at
6 // https://developers.google.com/open-source/licenses/bsd
7
8 // This header is private to the ProtobolBuffers library and must NOT be
9 // included by any sources outside this library. The contents of this file are
10 // subject to change at any time without notice.
11
12 #import "GPBDescriptor.h"
13 #import "GPBWireFormat.h"
14
15 // Describes attributes of the field.
16 typedef NS_OPTIONS(uint16_t, GPBFieldFlags) {
17 GPBFieldNone = 0,
18 // These map to standard protobuf concepts.
19 GPBFieldRequired = 1 << 0,
20 GPBFieldRepeated = 1 << 1,
21 GPBFieldPacked = 1 << 2,
22 GPBFieldOptional = 1 << 3,
23 GPBFieldHasDefaultValue = 1 << 4,
24
25 // Indicate that the field should "clear" when set to zero value. This is the
26 // proto3 non optional behavior for singular data (ints, data, string, enum)
27 // fields.
28 GPBFieldClearHasIvarOnZero = 1 << 5,
29 // Indicates the field needs custom handling for the TextFormat name, if not
30 // set, the name can be derived from the ObjC name.
31 GPBFieldTextFormatNameCustom = 1 << 6,
32 // This flag has never had any meaning, it was set on all enum fields.
33 GPBFieldHasEnumDescriptor = 1 << 7,
34
35 // These are not standard protobuf concepts, they are specific to the
36 // Objective C runtime.
37
38 // These bits are used to mark the field as a map and what the key
39 // type is.
40 GPBFieldMapKeyMask = 0xF << 8,
41 GPBFieldMapKeyInt32 = 1 << 8,
42 GPBFieldMapKeyInt64 = 2 << 8,
43 GPBFieldMapKeyUInt32 = 3 << 8,
44 GPBFieldMapKeyUInt64 = 4 << 8,
45 GPBFieldMapKeySInt32 = 5 << 8,
46 GPBFieldMapKeySInt64 = 6 << 8,
47 GPBFieldMapKeyFixed32 = 7 << 8,
48 GPBFieldMapKeyFixed64 = 8 << 8,
49 GPBFieldMapKeySFixed32 = 9 << 8,
50 GPBFieldMapKeySFixed64 = 10 << 8,
51 GPBFieldMapKeyBool = 11 << 8,
52 GPBFieldMapKeyString = 12 << 8,
53
54 // If the enum for this field is "closed", meaning that it:
55 // - Has a fixed set of named values.
56 // - Encountering values not in this set causes them to be treated as unknown
57 // fields.
58 // - The first value (i.e., the default) may be nonzero.
59 // NOTE: This could be tracked just on the GPBEnumDescriptor, but to support
60 // previously generated code, there would be not data to get the behavior
61 // correct, so instead it is tracked on the field. If old source compatibility
62 // is removed, this could be removed and the GPBEnumDescription fetched from
63 // the GPBFieldDescriptor instead.
64 GPBFieldClosedEnum = 1 << 12,
65 };
66
67 // NOTE: The structures defined here have their members ordered to minimize
68 // their size. This directly impacts the size of apps since these exist per
69 // field/extension.
70
71 typedef struct GPBFileDescription {
72 // The proto package for the file.
73 const char *package;
74 // The objc_class_prefix option if present.
75 const char *prefix;
76 // The file's proto syntax.
77 GPBFileSyntax syntax;
78 } GPBFileDescription;
79
80 // Describes a single field in a protobuf as it is represented as an ivar.
81 typedef struct GPBMessageFieldDescription {
82 // Name of ivar.
83 // Note that we looked into using a SEL here instead (which really is just a C string)
84 // but there is not a way to initialize an SEL with a constant (@selector is not constant) so the
85 // additional code generated to initialize the value is actually bigger in size than just using a
86 // C identifier for large apps.
87 const char *name;
88 union {
89 // className is deprecated and will be removed in favor of clazz.
90 // kept around right now for backwards compatibility.
91 // clazz is used iff GPBDescriptorInitializationFlag_UsesClassRefs is set.
92 char *className; // Name of the class of the message.
93 Class clazz; // Class of the message.
94 // For enums only.
95 GPBEnumDescriptorFunc enumDescFunc;
96 } dataTypeSpecific;
97 // The field number for the ivar.
98 uint32_t number;
99 // The index (in bits) into _has_storage_.
100 // >= 0: the bit to use for a value being set.
101 // = GPBNoHasBit(INT32_MAX): no storage used.
102 // < 0: in a oneOf, use a full int32 to record the field active.
103 int32_t hasIndex;
104 // Offset of the variable into it's structure struct.
105 uint32_t offset;
106 // Field flags. Use accessor functions below.
107 GPBFieldFlags flags;
108 // Data type of the ivar.
109 GPBDataType dataType;
110 } GPBMessageFieldDescription;
111
112 // If a message uses fields where they provide default values that are non zero, then this
113 // struct is used to provide the values along with the field info.
114 typedef struct GPBMessageFieldDescriptionWithDefault {
115 // Default value for the ivar.
116 GPBGenericValue defaultValue;
117
118 GPBMessageFieldDescription core;
119 } GPBMessageFieldDescriptionWithDefault;
120
121 // Describes attributes of the extension.
122 typedef NS_OPTIONS(uint8_t, GPBExtensionOptions) {
123 GPBExtensionNone = 0,
124 // These map to standard protobuf concepts.
125 GPBExtensionRepeated = 1 << 0,
126 GPBExtensionPacked = 1 << 1,
127 GPBExtensionSetWireFormat = 1 << 2,
128 };
129
130 // An extension
131 typedef struct GPBExtensionDescription {
132 GPBGenericValue defaultValue;
133 const char *singletonName;
134 // Before 3.12, `extendedClass` was just a `const char *`. Thanks to nested
135 // initialization
136 // (https://en.cppreference.com/w/c/language/struct_initialization#Nested_initialization) old
137 // generated code with `.extendedClass = GPBStringifySymbol(Something)` still works; and the
138 // current generator can use `extendedClass.clazz`, to pass a Class reference.
139 union {
140 const char *name;
141 Class clazz;
142 } extendedClass;
143 // Before 3.12, this was `const char *messageOrGroupClassName`. In the
144 // initial 3.12 release, we moved the `union messageOrGroupClass`, and failed
145 // to realize that would break existing source code for extensions. So to
146 // keep existing source code working, we added an unnamed union (C11) to
147 // provide both the old field name and the new union. This keeps both older
148 // and newer code working.
149 // Background: https://github.com/protocolbuffers/protobuf/issues/7555
150 union {
151 const char *messageOrGroupClassName;
152 union {
153 const char *name;
154 Class clazz;
155 } messageOrGroupClass;
156 };
157 GPBEnumDescriptorFunc enumDescriptorFunc;
158 int32_t fieldNumber;
159 GPBDataType dataType;
160 GPBExtensionOptions options;
161 } GPBExtensionDescription;
162
163 typedef NS_OPTIONS(uint32_t, GPBDescriptorInitializationFlags) {
164 GPBDescriptorInitializationFlag_None = 0,
165 GPBDescriptorInitializationFlag_FieldsWithDefault = 1 << 0,
166 GPBDescriptorInitializationFlag_WireFormat = 1 << 1,
167
168 // This is used as a stopgap as we move from using class names to class
169 // references. The runtime needs to support both until we allow a
170 // breaking change in the runtime.
171 GPBDescriptorInitializationFlag_UsesClassRefs = 1 << 2,
172
173 // This flag is used to indicate that the generated sources already contain
174 // the `GPBFieldClearHasIvarOnZero` flag and it doesn't have to be computed
175 // at startup. This allows older generated code to still work with the
176 // current runtime library.
177 GPBDescriptorInitializationFlag_Proto3OptionalKnown = 1 << 3,
178
179 // This flag is used to indicate that the generated sources already contain
180 // the `GPBFieldCloseEnum` flag and it doesn't have to be computed at startup.
181 // This allows the older generated code to still work with the current runtime
182 // library.
183 GPBDescriptorInitializationFlag_ClosedEnumSupportKnown = 1 << 4,
184 };
185
GPBDescriptor()186 @interface GPBDescriptor () {
187 @package
188 NSArray *fields_;
189 NSArray *oneofs_;
190 uint32_t storageSize_;
191 }
192
193 // fieldDescriptions and fileDescription have to be long lived, they are held as raw pointers.
194 + (instancetype)allocDescriptorForClass:(Class)messageClass
195 messageName:(NSString *)messageName
196 fileDescription:(GPBFileDescription *)fileDescription
197 fields:(void *)fieldDescriptions
198 fieldCount:(uint32_t)fieldCount
199 storageSize:(uint32_t)storageSize
200 flags:(GPBDescriptorInitializationFlags)flags;
201
202 // Called right after init to provide extra information to avoid init having
203 // an explosion of args. These pointers are recorded, so they are expected
204 // to live for the lifetime of the app.
205 - (void)setupOneofs:(const char **)oneofNames
206 count:(uint32_t)count
207 firstHasIndex:(int32_t)firstHasIndex;
208 - (void)setupExtraTextInfo:(const char *)extraTextFormatInfo;
209 - (void)setupExtensionRanges:(const GPBExtensionRange *)ranges count:(int32_t)count;
210 - (void)setupContainingMessageClass:(Class)msgClass;
211
212 // Deprecated, these remain to support older versions of source generation.
213 + (instancetype)allocDescriptorForClass:(Class)messageClass
214 file:(GPBFileDescriptor *)file
215 fields:(void *)fieldDescriptions
216 fieldCount:(uint32_t)fieldCount
217 storageSize:(uint32_t)storageSize
218 flags:(GPBDescriptorInitializationFlags)flags
219 __attribute__((deprecated("Please use a newer version of protoc to regenerate your sources. "
220 "Support for this version will go away in the future.")));
221 + (instancetype)allocDescriptorForClass:(Class)messageClass
222 rootClass:(Class)rootClass
223 file:(GPBFileDescriptor *)file
224 fields:(void *)fieldDescriptions
225 fieldCount:(uint32_t)fieldCount
226 storageSize:(uint32_t)storageSize
227 flags:(GPBDescriptorInitializationFlags)flags
228 __attribute__((deprecated("Please use a newer version of protoc to regenerate your sources. "
229 "Support for this version will go away in the future.")));
230 - (void)setupContainingMessageClassName:(const char *)msgClassName
231 __attribute__((deprecated("Please use a newer version of protoc to regenerate your sources. "
232 "Support for this version will go away in the future.")));
233 - (void)setupMessageClassNameSuffix:(NSString *)suffix
234 __attribute__((deprecated("Please use a newer version of protoc to regenerate your sources. "
235 "Support for this version will go away in the future.")));
236
237 @end
238
239 @interface GPBFileDescriptor ()
240 - (instancetype)initWithPackage:(NSString *)package
241 objcPrefix:(NSString *)objcPrefix
242 syntax:(GPBFileSyntax)syntax;
243 - (instancetype)initWithPackage:(NSString *)package syntax:(GPBFileSyntax)syntax;
244 @end
245
GPBOneofDescriptor()246 @interface GPBOneofDescriptor () {
247 @package
248 const char *name_;
249 NSArray *fields_;
250 }
251 // name must be long lived.
252 - (instancetype)initWithName:(const char *)name fields:(NSArray *)fields;
253 @end
254
GPBFieldDescriptor()255 @interface GPBFieldDescriptor () {
256 @package
257 GPBMessageFieldDescription *description_;
258 GPB_UNSAFE_UNRETAINED GPBOneofDescriptor *containingOneof_;
259 }
260 @end
261
262 typedef NS_OPTIONS(uint32_t, GPBEnumDescriptorInitializationFlags) {
263 GPBEnumDescriptorInitializationFlag_None = 0,
264
265 // Available: 1 << 0
266
267 // Marks this enum as a closed enum.
268 GPBEnumDescriptorInitializationFlag_IsClosed = 1 << 1,
269 };
270
271 @interface GPBEnumDescriptor ()
272 // valueNames, values and extraTextFormatInfo have to be long lived, they are
273 // held as raw pointers.
274 + (instancetype)allocDescriptorForName:(NSString *)name
275 valueNames:(const char *)valueNames
276 values:(const int32_t *)values
277 count:(uint32_t)valueCount
278 enumVerifier:(GPBEnumValidationFunc)enumVerifier
279 flags:(GPBEnumDescriptorInitializationFlags)flags;
280 + (instancetype)allocDescriptorForName:(NSString *)name
281 valueNames:(const char *)valueNames
282 values:(const int32_t *)values
283 count:(uint32_t)valueCount
284 enumVerifier:(GPBEnumValidationFunc)enumVerifier
285 flags:(GPBEnumDescriptorInitializationFlags)flags
286 extraTextFormatInfo:(const char *)extraTextFormatInfo;
287
288 // Deprecated, these remain to support older versions of source generation.
289 + (instancetype)allocDescriptorForName:(NSString *)name
290 valueNames:(const char *)valueNames
291 values:(const int32_t *)values
292 count:(uint32_t)valueCount
293 enumVerifier:(GPBEnumValidationFunc)enumVerifier
294 __attribute__((deprecated("Please use a newer version of protoc to regenerate your sources. "
295 "Support for this version will go away in the future.")));
296 + (instancetype)allocDescriptorForName:(NSString *)name
297 valueNames:(const char *)valueNames
298 values:(const int32_t *)values
299 count:(uint32_t)valueCount
300 enumVerifier:(GPBEnumValidationFunc)enumVerifier
301 extraTextFormatInfo:(const char *)extraTextFormatInfo
302 __attribute__((deprecated("Please use a newer version of protoc to regenerate your sources. "
303 "Support for this version will go away in the future.")));
304 @end
305
GPBExtensionDescriptor()306 @interface GPBExtensionDescriptor () {
307 @package
308 GPBExtensionDescription *description_;
309 }
310 @property(nonatomic, readonly) GPBWireFormat wireType;
311
312 // For repeated extensions, alternateWireType is the wireType with the opposite
313 // value for the packable property. i.e. - if the extension was marked packed
314 // it would be the wire type for unpacked; if the extension was marked unpacked,
315 // it would be the wire type for packed.
316 @property(nonatomic, readonly) GPBWireFormat alternateWireType;
317
318 // description has to be long lived, it is held as a raw pointer.
319 - (instancetype)initWithExtensionDescription:(GPBExtensionDescription *)desc
320 usesClassRefs:(BOOL)usesClassRefs;
321 // Deprecated. Calls above with `usesClassRefs = NO`
322 - (instancetype)initWithExtensionDescription:(GPBExtensionDescription *)desc
323 __attribute__((deprecated("Please use a newer version of protoc to regenerate your sources. "
324 "Support for this version will go away in the future.")));
325
326 - (NSComparisonResult)compareByFieldNumber:(GPBExtensionDescriptor *)other;
327 @end
328
329 CF_EXTERN_C_BEGIN
330
331 // Direct access is use for speed, to avoid even internally declaring things
332 // read/write, etc. The warning is enabled in the project to ensure code calling
333 // protos can turn on -Wdirect-ivar-access without issues.
334 #pragma clang diagnostic push
335 #pragma clang diagnostic ignored "-Wdirect-ivar-access"
336
GPBFieldIsMapOrArray(GPBFieldDescriptor * field)337 GPB_INLINE BOOL GPBFieldIsMapOrArray(GPBFieldDescriptor *field) {
338 return (field->description_->flags & (GPBFieldRepeated | GPBFieldMapKeyMask)) != 0;
339 }
340
GPBGetFieldDataType(GPBFieldDescriptor * field)341 GPB_INLINE GPBDataType GPBGetFieldDataType(GPBFieldDescriptor *field) {
342 return field->description_->dataType;
343 }
344
GPBFieldHasIndex(GPBFieldDescriptor * field)345 GPB_INLINE int32_t GPBFieldHasIndex(GPBFieldDescriptor *field) {
346 return field->description_->hasIndex;
347 }
348
GPBFieldNumber(GPBFieldDescriptor * field)349 GPB_INLINE uint32_t GPBFieldNumber(GPBFieldDescriptor *field) {
350 return field->description_->number;
351 }
352
GPBFieldIsClosedEnum(GPBFieldDescriptor * field)353 GPB_INLINE BOOL GPBFieldIsClosedEnum(GPBFieldDescriptor *field) {
354 return (field->description_->flags & GPBFieldClosedEnum) != 0;
355 }
356
357 #pragma clang diagnostic pop
358
359 uint32_t GPBFieldTag(GPBFieldDescriptor *self);
360
361 // For repeated fields, alternateWireType is the wireType with the opposite
362 // value for the packable property. i.e. - if the field was marked packed it
363 // would be the wire type for unpacked; if the field was marked unpacked, it
364 // would be the wire type for packed.
365 uint32_t GPBFieldAlternateTag(GPBFieldDescriptor *self);
366
GPBExtensionIsRepeated(GPBExtensionDescription * description)367 GPB_INLINE BOOL GPBExtensionIsRepeated(GPBExtensionDescription *description) {
368 return (description->options & GPBExtensionRepeated) != 0;
369 }
370
GPBExtensionIsPacked(GPBExtensionDescription * description)371 GPB_INLINE BOOL GPBExtensionIsPacked(GPBExtensionDescription *description) {
372 return (description->options & GPBExtensionPacked) != 0;
373 }
374
GPBExtensionIsWireFormat(GPBExtensionDescription * description)375 GPB_INLINE BOOL GPBExtensionIsWireFormat(GPBExtensionDescription *description) {
376 return (description->options & GPBExtensionSetWireFormat) != 0;
377 }
378
379 // Helper for compile time assets.
380 #ifndef GPBInternalCompileAssert
381 #define GPBInternalCompileAssert(test, msg) _Static_assert((test), #msg)
382 #endif // GPBInternalCompileAssert
383
384 // Sanity check that there isn't padding between the field description
385 // structures with and without a default.
386 GPBInternalCompileAssert(sizeof(GPBMessageFieldDescriptionWithDefault) ==
387 (sizeof(GPBGenericValue) + sizeof(GPBMessageFieldDescription)),
388 DescriptionsWithDefault_different_size_than_expected);
389
390 CF_EXTERN_C_END
391