blob: 408f8d4e1014b9b1dcef326e7ad30240924b13e4 [file] [log] [blame]
Thomas Van Lenten30650d82015-05-01 08:57:16 -04001// 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"
Thomas Van Lentend846b0b2015-06-08 16:24:57 -040036#import "GPBWireFormat.h"
Thomas Van Lenten30650d82015-05-01 08:57:16 -040037
38// Describes attributes of the field.
Thomas Van Lenten79a23c42016-03-17 10:04:21 -040039typedef NS_OPTIONS(uint16_t, GPBFieldFlags) {
Sergio Campamá14e74f62016-09-08 12:15:12 -070040 GPBFieldNone = 0,
Thomas Van Lenten30650d82015-05-01 08:57:16 -040041 // 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
Thomas Van Lenten8d224b42020-04-08 11:34:37 -040048 // Indicate that the field should "clear" when set to zero value. This is the
49 // proto3 non optional behavior for singular data (ints, data, string, enum)
50 // fields.
51 GPBFieldClearHasIvarOnZero = 1 << 5,
Thomas Van Lenten79a23c42016-03-17 10:04:21 -040052 // Indicates the field needs custom handling for the TextFormat name, if not
53 // set, the name can be derived from the ObjC name.
54 GPBFieldTextFormatNameCustom = 1 << 6,
55 // Indicates the field has an enum descriptor.
56 GPBFieldHasEnumDescriptor = 1 << 7,
57
Thomas Van Lenten30650d82015-05-01 08:57:16 -040058 // These are not standard protobuf concepts, they are specific to the
59 // Objective C runtime.
60
61 // These bits are used to mark the field as a map and what the key
62 // type is.
63 GPBFieldMapKeyMask = 0xF << 8,
64 GPBFieldMapKeyInt32 = 1 << 8,
65 GPBFieldMapKeyInt64 = 2 << 8,
66 GPBFieldMapKeyUInt32 = 3 << 8,
67 GPBFieldMapKeyUInt64 = 4 << 8,
68 GPBFieldMapKeySInt32 = 5 << 8,
69 GPBFieldMapKeySInt64 = 6 << 8,
70 GPBFieldMapKeyFixed32 = 7 << 8,
71 GPBFieldMapKeyFixed64 = 8 << 8,
72 GPBFieldMapKeySFixed32 = 9 << 8,
73 GPBFieldMapKeySFixed64 = 10 << 8,
74 GPBFieldMapKeyBool = 11 << 8,
75 GPBFieldMapKeyString = 12 << 8,
Thomas Van Lenten30650d82015-05-01 08:57:16 -040076};
77
Thomas Van Lenten79a23c42016-03-17 10:04:21 -040078// NOTE: The structures defined here have their members ordered to minimize
79// their size. This directly impacts the size of apps since these exist per
80// field/extension.
81
Thomas Van Lenten30650d82015-05-01 08:57:16 -040082// Describes a single field in a protobuf as it is represented as an ivar.
83typedef struct GPBMessageFieldDescription {
84 // Name of ivar.
85 const char *name;
Thomas Van Lenten30650d82015-05-01 08:57:16 -040086 union {
Dave MacLachlan74956e12019-12-17 17:32:09 -080087 // className is deprecated and will be removed in favor of clazz.
88 // kept around right now for backwards compatibility.
89 // clazz is used iff GPBDescriptorInitializationFlag_UsesClassRefs is set.
90 char *className; // Name of the class of the message.
91 Class clazz; // Class of the message.
Thomas Van Lenten30650d82015-05-01 08:57:16 -040092 // For enums only: If EnumDescriptors are compiled in, it will be that,
93 // otherwise it will be the verifier.
94 GPBEnumDescriptorFunc enumDescFunc;
95 GPBEnumValidationFunc enumVerifier;
Thomas Van Lentend846b0b2015-06-08 16:24:57 -040096 } dataTypeSpecific;
Thomas Van Lenten79a23c42016-03-17 10:04:21 -040097 // 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;
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400110} GPBMessageFieldDescription;
111
Thomas Van Lenten79a23c42016-03-17 10:04:21 -0400112// Fields in messages defined in a 'proto2' syntax file can provide a default
113// value. This struct provides the default along with the field info.
114typedef struct GPBMessageFieldDescriptionWithDefault {
115 // Default value for the ivar.
116 GPBGenericValue defaultValue;
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400117
Thomas Van Lenten79a23c42016-03-17 10:04:21 -0400118 GPBMessageFieldDescription core;
119} GPBMessageFieldDescriptionWithDefault;
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400120
121// Describes attributes of the extension.
Thomas Van Lenten79a23c42016-03-17 10:04:21 -0400122typedef NS_OPTIONS(uint8_t, GPBExtensionOptions) {
Sergio Campamá14e74f62016-09-08 12:15:12 -0700123 GPBExtensionNone = 0,
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400124 // These map to standard protobuf concepts.
125 GPBExtensionRepeated = 1 << 0,
126 GPBExtensionPacked = 1 << 1,
127 GPBExtensionSetWireFormat = 1 << 2,
128};
129
130// An extension
131typedef struct GPBExtensionDescription {
Thomas Van Lentend846b0b2015-06-08 16:24:57 -0400132 GPBGenericValue defaultValue;
Thomas Van Lenten79a23c42016-03-17 10:04:21 -0400133 const char *singletonName;
Thomas Van Lenten2360bac2020-05-28 14:13:39 -0400134 // Before 3.12, `extendedClass` was just a `const char *`. Thanks to nested
135 // initialization (https://en.cppreference.com/w/c/language/struct_initialization#Nested_initialization)
136 // old generated code with `.extendedClass = GPBStringifySymbol(Something)`
137 // still works; and the current generator can use `extendedClass.clazz`, to
138 // pass a Class reference.
Dave MacLachlan74956e12019-12-17 17:32:09 -0800139 union {
140 const char *name;
141 Class clazz;
142 } extendedClass;
Thomas Van Lenten2360bac2020-05-28 14:13:39 -0400143 // 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
Dave MacLachlan74956e12019-12-17 17:32:09 -0800150 union {
Thomas Van Lenten2360bac2020-05-28 14:13:39 -0400151 const char *messageOrGroupClassName;
152 union {
153 const char *name;
154 Class clazz;
155 } messageOrGroupClass;
156 };
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400157 GPBEnumDescriptorFunc enumDescriptorFunc;
Thomas Van Lenten79a23c42016-03-17 10:04:21 -0400158 int32_t fieldNumber;
159 GPBDataType dataType;
160 GPBExtensionOptions options;
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400161} GPBExtensionDescription;
162
Thomas Van Lenten79a23c42016-03-17 10:04:21 -0400163typedef NS_OPTIONS(uint32_t, GPBDescriptorInitializationFlags) {
Sergio Campamá14e74f62016-09-08 12:15:12 -0700164 GPBDescriptorInitializationFlag_None = 0,
Thomas Van Lenten79a23c42016-03-17 10:04:21 -0400165 GPBDescriptorInitializationFlag_FieldsWithDefault = 1 << 0,
166 GPBDescriptorInitializationFlag_WireFormat = 1 << 1,
Dave MacLachlan74956e12019-12-17 17:32:09 -0800167
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.
Thomas Van Lenten8d224b42020-04-08 11:34:37 -0400171 GPBDescriptorInitializationFlag_UsesClassRefs = 1 << 2,
172
Thomas Van Lentendddeed22020-04-24 13:40:59 -0400173 // This flag is used to indicate that the generated sources already contain
Thomas Van Lenten8d224b42020-04-08 11:34:37 -0400174 // the `GPBFieldClearHasIvarOnZero` flag and it doesn't have to be computed
Thomas Van Lentendddeed22020-04-24 13:40:59 -0400175 // at startup. This allows older generated code to still work with the
Thomas Van Lenten8d224b42020-04-08 11:34:37 -0400176 // current runtime library.
177 GPBDescriptorInitializationFlag_Proto3OptionalKnown = 1 << 3,
Thomas Van Lenten79a23c42016-03-17 10:04:21 -0400178};
179
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400180@interface GPBDescriptor () {
181 @package
182 NSArray *fields_;
183 NSArray *oneofs_;
Thomas Van Lenten79a23c42016-03-17 10:04:21 -0400184 uint32_t storageSize_;
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400185}
186
Thomas Van Lenten79a23c42016-03-17 10:04:21 -0400187// fieldDescriptions have to be long lived, they are held as raw pointers.
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400188+ (instancetype)
189 allocDescriptorForClass:(Class)messageClass
190 rootClass:(Class)rootClass
191 file:(GPBFileDescriptor *)file
Thomas Van Lenten79a23c42016-03-17 10:04:21 -0400192 fields:(void *)fieldDescriptions
193 fieldCount:(uint32_t)fieldCount
194 storageSize:(uint32_t)storageSize
195 flags:(GPBDescriptorInitializationFlags)flags;
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400196
197- (instancetype)initWithClass:(Class)messageClass
198 file:(GPBFileDescriptor *)file
199 fields:(NSArray *)fields
Thomas Van Lenten79a23c42016-03-17 10:04:21 -0400200 storageSize:(uint32_t)storage
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400201 wireFormat:(BOOL)wireFormat;
202
Thomas Van Lenten79a23c42016-03-17 10:04:21 -0400203// Called right after init to provide extra information to avoid init having
204// an explosion of args. These pointers are recorded, so they are expected
205// to live for the lifetime of the app.
206- (void)setupOneofs:(const char **)oneofNames
207 count:(uint32_t)count
208 firstHasIndex:(int32_t)firstHasIndex;
209- (void)setupExtraTextInfo:(const char *)extraTextFormatInfo;
210- (void)setupExtensionRanges:(const GPBExtensionRange *)ranges count:(int32_t)count;
Dave MacLachlan74956e12019-12-17 17:32:09 -0800211- (void)setupContainingMessageClass:(Class)msgClass;
Thomas Van Lenten337ec302016-08-16 11:26:49 -0400212- (void)setupMessageClassNameSuffix:(NSString *)suffix;
Thomas Van Lenten79a23c42016-03-17 10:04:21 -0400213
Dave MacLachlan74956e12019-12-17 17:32:09 -0800214// Deprecated. Use setupContainingMessageClass instead.
215- (void)setupContainingMessageClassName:(const char *)msgClassName;
216
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400217@end
218
219@interface GPBFileDescriptor ()
220- (instancetype)initWithPackage:(NSString *)package
Thomas Van Lenten337ec302016-08-16 11:26:49 -0400221 objcPrefix:(NSString *)objcPrefix
222 syntax:(GPBFileSyntax)syntax;
223- (instancetype)initWithPackage:(NSString *)package
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400224 syntax:(GPBFileSyntax)syntax;
225@end
226
227@interface GPBOneofDescriptor () {
228 @package
Thomas Van Lenten79a23c42016-03-17 10:04:21 -0400229 const char *name_;
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400230 NSArray *fields_;
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400231 SEL caseSel_;
232}
Thomas Van Lenten79a23c42016-03-17 10:04:21 -0400233// name must be long lived.
234- (instancetype)initWithName:(const char *)name fields:(NSArray *)fields;
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400235@end
236
237@interface GPBFieldDescriptor () {
238 @package
239 GPBMessageFieldDescription *description_;
240 GPB_UNSAFE_UNRETAINED GPBOneofDescriptor *containingOneof_;
241
242 SEL getSel_;
243 SEL setSel_;
Thomas Van Lentend846b0b2015-06-08 16:24:57 -0400244 SEL hasOrCountSel_; // *Count for map<>/repeated fields, has* otherwise.
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400245 SEL setHasSel_;
246}
247
248// Single initializer
249// description has to be long lived, it is held as a raw pointer.
Thomas Van Lenten79a23c42016-03-17 10:04:21 -0400250- (instancetype)initWithFieldDescription:(void *)description
251 includesDefault:(BOOL)includesDefault
Dave MacLachlan74956e12019-12-17 17:32:09 -0800252 usesClassRefs:(BOOL)usesClassRefs
Thomas Van Lenten8d224b42020-04-08 11:34:37 -0400253 proto3OptionalKnown:(BOOL)proto3OptionalKnown
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400254 syntax:(GPBFileSyntax)syntax;
Dave MacLachlan74956e12019-12-17 17:32:09 -0800255
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400256@end
257
258@interface GPBEnumDescriptor ()
Thomas Van Lenten79a23c42016-03-17 10:04:21 -0400259// valueNames, values and extraTextFormatInfo have to be long lived, they are
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400260// held as raw pointers.
261+ (instancetype)
262 allocDescriptorForName:(NSString *)name
Thomas Van Lenten79a23c42016-03-17 10:04:21 -0400263 valueNames:(const char *)valueNames
264 values:(const int32_t *)values
265 count:(uint32_t)valueCount
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400266 enumVerifier:(GPBEnumValidationFunc)enumVerifier;
267+ (instancetype)
268 allocDescriptorForName:(NSString *)name
Thomas Van Lenten79a23c42016-03-17 10:04:21 -0400269 valueNames:(const char *)valueNames
270 values:(const int32_t *)values
271 count:(uint32_t)valueCount
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400272 enumVerifier:(GPBEnumValidationFunc)enumVerifier
273 extraTextFormatInfo:(const char *)extraTextFormatInfo;
274
275- (instancetype)initWithName:(NSString *)name
Thomas Van Lenten79a23c42016-03-17 10:04:21 -0400276 valueNames:(const char *)valueNames
277 values:(const int32_t *)values
278 count:(uint32_t)valueCount
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400279 enumVerifier:(GPBEnumValidationFunc)enumVerifier;
280@end
281
282@interface GPBExtensionDescriptor () {
283 @package
284 GPBExtensionDescription *description_;
285}
Thomas Van Lentend846b0b2015-06-08 16:24:57 -0400286@property(nonatomic, readonly) GPBWireFormat wireType;
287
288// For repeated extensions, alternateWireType is the wireType with the opposite
289// value for the packable property. i.e. - if the extension was marked packed
290// it would be the wire type for unpacked; if the extension was marked unpacked,
291// it would be the wire type for packed.
292@property(nonatomic, readonly) GPBWireFormat alternateWireType;
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400293
294// description has to be long lived, it is held as a raw pointer.
Dave MacLachlan74956e12019-12-17 17:32:09 -0800295- (instancetype)initWithExtensionDescription:(GPBExtensionDescription *)desc
296 usesClassRefs:(BOOL)usesClassRefs;
297// Deprecated. Calls above with `usesClassRefs = NO`
298- (instancetype)initWithExtensionDescription:(GPBExtensionDescription *)desc;
299
Thomas Van Lentend846b0b2015-06-08 16:24:57 -0400300- (NSComparisonResult)compareByFieldNumber:(GPBExtensionDescriptor *)other;
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400301@end
302
303CF_EXTERN_C_BEGIN
304
Thomas Van Lentenc8a440d2016-05-25 13:46:00 -0400305// Direct access is use for speed, to avoid even internally declaring things
306// read/write, etc. The warning is enabled in the project to ensure code calling
307// protos can turn on -Wdirect-ivar-access without issues.
308#pragma clang diagnostic push
309#pragma clang diagnostic ignored "-Wdirect-ivar-access"
310
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400311GPB_INLINE BOOL GPBFieldIsMapOrArray(GPBFieldDescriptor *field) {
312 return (field->description_->flags &
313 (GPBFieldRepeated | GPBFieldMapKeyMask)) != 0;
314}
315
Thomas Van Lentend846b0b2015-06-08 16:24:57 -0400316GPB_INLINE GPBDataType GPBGetFieldDataType(GPBFieldDescriptor *field) {
317 return field->description_->dataType;
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400318}
319
320GPB_INLINE int32_t GPBFieldHasIndex(GPBFieldDescriptor *field) {
321 return field->description_->hasIndex;
322}
323
324GPB_INLINE uint32_t GPBFieldNumber(GPBFieldDescriptor *field) {
325 return field->description_->number;
326}
327
Thomas Van Lentenc8a440d2016-05-25 13:46:00 -0400328#pragma clang diagnostic pop
329
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400330uint32_t GPBFieldTag(GPBFieldDescriptor *self);
331
Thomas Van Lentend846b0b2015-06-08 16:24:57 -0400332// For repeated fields, alternateWireType is the wireType with the opposite
333// value for the packable property. i.e. - if the field was marked packed it
334// would be the wire type for unpacked; if the field was marked unpacked, it
335// would be the wire type for packed.
336uint32_t GPBFieldAlternateTag(GPBFieldDescriptor *self);
337
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400338GPB_INLINE BOOL GPBHasPreservingUnknownEnumSemantics(GPBFileSyntax syntax) {
339 return syntax == GPBFileSyntaxProto3;
340}
341
Thomas Van Lentend846b0b2015-06-08 16:24:57 -0400342GPB_INLINE BOOL GPBExtensionIsRepeated(GPBExtensionDescription *description) {
343 return (description->options & GPBExtensionRepeated) != 0;
344}
345
346GPB_INLINE BOOL GPBExtensionIsPacked(GPBExtensionDescription *description) {
347 return (description->options & GPBExtensionPacked) != 0;
348}
349
350GPB_INLINE BOOL GPBExtensionIsWireFormat(GPBExtensionDescription *description) {
351 return (description->options & GPBExtensionSetWireFormat) != 0;
352}
353
Thomas Van Lenten79a23c42016-03-17 10:04:21 -0400354// Helper for compile time assets.
Thomas Van Lentenc8a440d2016-05-25 13:46:00 -0400355#ifndef GPBInternalCompileAssert
Thomas Van Lenten79a23c42016-03-17 10:04:21 -0400356 #if __has_feature(c_static_assert) || __has_extension(c_static_assert)
Thomas Van Lentenc8a440d2016-05-25 13:46:00 -0400357 #define GPBInternalCompileAssert(test, msg) _Static_assert((test), #msg)
Thomas Van Lenten79a23c42016-03-17 10:04:21 -0400358 #else
359 // Pre-Xcode 7 support.
Thomas Van Lentenc8a440d2016-05-25 13:46:00 -0400360 #define GPBInternalCompileAssertSymbolInner(line, msg) GPBInternalCompileAssert ## line ## __ ## msg
361 #define GPBInternalCompileAssertSymbol(line, msg) GPBInternalCompileAssertSymbolInner(line, msg)
362 #define GPBInternalCompileAssert(test, msg) \
363 typedef char GPBInternalCompileAssertSymbol(__LINE__, msg) [ ((test) ? 1 : -1) ]
Thomas Van Lenten79a23c42016-03-17 10:04:21 -0400364 #endif // __has_feature(c_static_assert) || __has_extension(c_static_assert)
Thomas Van Lentenc8a440d2016-05-25 13:46:00 -0400365#endif // GPBInternalCompileAssert
Thomas Van Lenten79a23c42016-03-17 10:04:21 -0400366
367// Sanity check that there isn't padding between the field description
368// structures with and without a default.
Thomas Van Lentenc8a440d2016-05-25 13:46:00 -0400369GPBInternalCompileAssert(sizeof(GPBMessageFieldDescriptionWithDefault) ==
370 (sizeof(GPBGenericValue) +
371 sizeof(GPBMessageFieldDescription)),
372 DescriptionsWithDefault_different_size_than_expected);
Thomas Van Lentend846b0b2015-06-08 16:24:57 -0400373
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400374CF_EXTERN_C_END