blob: 452b3f8e7ecc747edb18974608e21b139c3dd833 [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 Lenten79a23c42016-03-17 10:04:21 -040048 // 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
Thomas Van Lenten30650d82015-05-01 08:57:16 -040054 // 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,
Thomas Van Lenten30650d82015-05-01 08:57:16 -040072};
73
Thomas Van Lenten79a23c42016-03-17 10:04:21 -040074// 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
Thomas Van Lenten30650d82015-05-01 08:57:16 -040078// Describes a single field in a protobuf as it is represented as an ivar.
79typedef struct GPBMessageFieldDescription {
80 // Name of ivar.
81 const char *name;
Thomas Van Lenten30650d82015-05-01 08:57:16 -040082 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.
86 GPBEnumDescriptorFunc enumDescFunc;
87 GPBEnumValidationFunc enumVerifier;
Thomas Van Lentend846b0b2015-06-08 16:24:57 -040088 } dataTypeSpecific;
Thomas Van Lenten79a23c42016-03-17 10:04:21 -040089 // 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;
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400102} GPBMessageFieldDescription;
103
Thomas Van Lenten79a23c42016-03-17 10:04:21 -0400104// 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.
106typedef struct GPBMessageFieldDescriptionWithDefault {
107 // Default value for the ivar.
108 GPBGenericValue defaultValue;
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400109
Thomas Van Lenten79a23c42016-03-17 10:04:21 -0400110 GPBMessageFieldDescription core;
111} GPBMessageFieldDescriptionWithDefault;
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400112
113// Describes attributes of the extension.
Thomas Van Lenten79a23c42016-03-17 10:04:21 -0400114typedef NS_OPTIONS(uint8_t, GPBExtensionOptions) {
Sergio Campamá14e74f62016-09-08 12:15:12 -0700115 GPBExtensionNone = 0,
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400116 // These map to standard protobuf concepts.
117 GPBExtensionRepeated = 1 << 0,
118 GPBExtensionPacked = 1 << 1,
119 GPBExtensionSetWireFormat = 1 << 2,
120};
121
122// An extension
123typedef struct GPBExtensionDescription {
Thomas Van Lentend846b0b2015-06-08 16:24:57 -0400124 GPBGenericValue defaultValue;
Thomas Van Lenten79a23c42016-03-17 10:04:21 -0400125 const char *singletonName;
126 const char *extendedClass;
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400127 const char *messageOrGroupClassName;
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400128 GPBEnumDescriptorFunc enumDescriptorFunc;
Thomas Van Lenten79a23c42016-03-17 10:04:21 -0400129 int32_t fieldNumber;
130 GPBDataType dataType;
131 GPBExtensionOptions options;
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400132} GPBExtensionDescription;
133
Thomas Van Lenten79a23c42016-03-17 10:04:21 -0400134typedef NS_OPTIONS(uint32_t, GPBDescriptorInitializationFlags) {
Sergio Campamá14e74f62016-09-08 12:15:12 -0700135 GPBDescriptorInitializationFlag_None = 0,
Thomas Van Lenten79a23c42016-03-17 10:04:21 -0400136 GPBDescriptorInitializationFlag_FieldsWithDefault = 1 << 0,
137 GPBDescriptorInitializationFlag_WireFormat = 1 << 1,
138};
139
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400140@interface GPBDescriptor () {
141 @package
142 NSArray *fields_;
143 NSArray *oneofs_;
Thomas Van Lenten79a23c42016-03-17 10:04:21 -0400144 uint32_t storageSize_;
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400145}
146
Thomas Van Lenten79a23c42016-03-17 10:04:21 -0400147// fieldDescriptions have to be long lived, they are held as raw pointers.
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400148+ (instancetype)
149 allocDescriptorForClass:(Class)messageClass
150 rootClass:(Class)rootClass
151 file:(GPBFileDescriptor *)file
Thomas Van Lenten79a23c42016-03-17 10:04:21 -0400152 fields:(void *)fieldDescriptions
153 fieldCount:(uint32_t)fieldCount
154 storageSize:(uint32_t)storageSize
155 flags:(GPBDescriptorInitializationFlags)flags;
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400156
157- (instancetype)initWithClass:(Class)messageClass
158 file:(GPBFileDescriptor *)file
159 fields:(NSArray *)fields
Thomas Van Lenten79a23c42016-03-17 10:04:21 -0400160 storageSize:(uint32_t)storage
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400161 wireFormat:(BOOL)wireFormat;
162
Thomas Van Lenten79a23c42016-03-17 10:04:21 -0400163// 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;
Thomas Van Lenten337ec302016-08-16 11:26:49 -0400171- (void)setupContainingMessageClassName:(const char *)msgClassName;
172- (void)setupMessageClassNameSuffix:(NSString *)suffix;
Thomas Van Lenten79a23c42016-03-17 10:04:21 -0400173
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400174@end
175
176@interface GPBFileDescriptor ()
177- (instancetype)initWithPackage:(NSString *)package
Thomas Van Lenten337ec302016-08-16 11:26:49 -0400178 objcPrefix:(NSString *)objcPrefix
179 syntax:(GPBFileSyntax)syntax;
180- (instancetype)initWithPackage:(NSString *)package
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400181 syntax:(GPBFileSyntax)syntax;
182@end
183
184@interface GPBOneofDescriptor () {
185 @package
Thomas Van Lenten79a23c42016-03-17 10:04:21 -0400186 const char *name_;
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400187 NSArray *fields_;
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400188 SEL caseSel_;
189}
Thomas Van Lenten79a23c42016-03-17 10:04:21 -0400190// name must be long lived.
191- (instancetype)initWithName:(const char *)name fields:(NSArray *)fields;
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400192@end
193
194@interface GPBFieldDescriptor () {
195 @package
196 GPBMessageFieldDescription *description_;
197 GPB_UNSAFE_UNRETAINED GPBOneofDescriptor *containingOneof_;
198
199 SEL getSel_;
200 SEL setSel_;
Thomas Van Lentend846b0b2015-06-08 16:24:57 -0400201 SEL hasOrCountSel_; // *Count for map<>/repeated fields, has* otherwise.
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400202 SEL setHasSel_;
203}
204
205// Single initializer
206// description has to be long lived, it is held as a raw pointer.
Thomas Van Lenten79a23c42016-03-17 10:04:21 -0400207- (instancetype)initWithFieldDescription:(void *)description
208 includesDefault:(BOOL)includesDefault
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400209 syntax:(GPBFileSyntax)syntax;
210@end
211
212@interface GPBEnumDescriptor ()
Thomas Van Lenten79a23c42016-03-17 10:04:21 -0400213// valueNames, values and extraTextFormatInfo have to be long lived, they are
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400214// held as raw pointers.
215+ (instancetype)
216 allocDescriptorForName:(NSString *)name
Thomas Van Lenten79a23c42016-03-17 10:04:21 -0400217 valueNames:(const char *)valueNames
218 values:(const int32_t *)values
219 count:(uint32_t)valueCount
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400220 enumVerifier:(GPBEnumValidationFunc)enumVerifier;
221+ (instancetype)
222 allocDescriptorForName:(NSString *)name
Thomas Van Lenten79a23c42016-03-17 10:04:21 -0400223 valueNames:(const char *)valueNames
224 values:(const int32_t *)values
225 count:(uint32_t)valueCount
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400226 enumVerifier:(GPBEnumValidationFunc)enumVerifier
227 extraTextFormatInfo:(const char *)extraTextFormatInfo;
228
229- (instancetype)initWithName:(NSString *)name
Thomas Van Lenten79a23c42016-03-17 10:04:21 -0400230 valueNames:(const char *)valueNames
231 values:(const int32_t *)values
232 count:(uint32_t)valueCount
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400233 enumVerifier:(GPBEnumValidationFunc)enumVerifier;
234@end
235
236@interface GPBExtensionDescriptor () {
237 @package
238 GPBExtensionDescription *description_;
239}
Thomas Van Lentend846b0b2015-06-08 16:24:57 -0400240@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;
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400247
248// description has to be long lived, it is held as a raw pointer.
249- (instancetype)initWithExtensionDescription:
Thomas Van Lentend846b0b2015-06-08 16:24:57 -0400250 (GPBExtensionDescription *)description;
251- (NSComparisonResult)compareByFieldNumber:(GPBExtensionDescriptor *)other;
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400252@end
253
254CF_EXTERN_C_BEGIN
255
Thomas Van Lentenc8a440d2016-05-25 13:46:00 -0400256// 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
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400262GPB_INLINE BOOL GPBFieldIsMapOrArray(GPBFieldDescriptor *field) {
263 return (field->description_->flags &
264 (GPBFieldRepeated | GPBFieldMapKeyMask)) != 0;
265}
266
Thomas Van Lentend846b0b2015-06-08 16:24:57 -0400267GPB_INLINE GPBDataType GPBGetFieldDataType(GPBFieldDescriptor *field) {
268 return field->description_->dataType;
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400269}
270
271GPB_INLINE int32_t GPBFieldHasIndex(GPBFieldDescriptor *field) {
272 return field->description_->hasIndex;
273}
274
275GPB_INLINE uint32_t GPBFieldNumber(GPBFieldDescriptor *field) {
276 return field->description_->number;
277}
278
Thomas Van Lentenc8a440d2016-05-25 13:46:00 -0400279#pragma clang diagnostic pop
280
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400281uint32_t GPBFieldTag(GPBFieldDescriptor *self);
282
Thomas Van Lentend846b0b2015-06-08 16:24:57 -0400283// 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.
287uint32_t GPBFieldAlternateTag(GPBFieldDescriptor *self);
288
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400289GPB_INLINE BOOL GPBHasPreservingUnknownEnumSemantics(GPBFileSyntax syntax) {
290 return syntax == GPBFileSyntaxProto3;
291}
292
Thomas Van Lentend846b0b2015-06-08 16:24:57 -0400293GPB_INLINE BOOL GPBExtensionIsRepeated(GPBExtensionDescription *description) {
294 return (description->options & GPBExtensionRepeated) != 0;
295}
296
297GPB_INLINE BOOL GPBExtensionIsPacked(GPBExtensionDescription *description) {
298 return (description->options & GPBExtensionPacked) != 0;
299}
300
301GPB_INLINE BOOL GPBExtensionIsWireFormat(GPBExtensionDescription *description) {
302 return (description->options & GPBExtensionSetWireFormat) != 0;
303}
304
Thomas Van Lenten79a23c42016-03-17 10:04:21 -0400305// Helper for compile time assets.
Thomas Van Lentenc8a440d2016-05-25 13:46:00 -0400306#ifndef GPBInternalCompileAssert
Thomas Van Lenten79a23c42016-03-17 10:04:21 -0400307 #if __has_feature(c_static_assert) || __has_extension(c_static_assert)
Thomas Van Lentenc8a440d2016-05-25 13:46:00 -0400308 #define GPBInternalCompileAssert(test, msg) _Static_assert((test), #msg)
Thomas Van Lenten79a23c42016-03-17 10:04:21 -0400309 #else
310 // Pre-Xcode 7 support.
Thomas Van Lentenc8a440d2016-05-25 13:46:00 -0400311 #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) ]
Thomas Van Lenten79a23c42016-03-17 10:04:21 -0400315 #endif // __has_feature(c_static_assert) || __has_extension(c_static_assert)
Thomas Van Lentenc8a440d2016-05-25 13:46:00 -0400316#endif // GPBInternalCompileAssert
Thomas Van Lenten79a23c42016-03-17 10:04:21 -0400317
318// Sanity check that there isn't padding between the field description
319// structures with and without a default.
Thomas Van Lentenc8a440d2016-05-25 13:46:00 -0400320GPBInternalCompileAssert(sizeof(GPBMessageFieldDescriptionWithDefault) ==
321 (sizeof(GPBGenericValue) +
322 sizeof(GPBMessageFieldDescription)),
323 DescriptionsWithDefault_different_size_than_expected);
Thomas Van Lentend846b0b2015-06-08 16:24:57 -0400324
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400325CF_EXTERN_C_END