1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
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
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.
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.
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.
35 #import "GPBDescriptor.h"
36 #import "GPBWireFormat.h"
38 // Describes attributes of the field.
39 typedef NS_OPTIONS(uint16_t, GPBFieldFlags) {
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,
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,
54 // These are not standard protobuf concepts, they are specific to the
55 // Objective C runtime.
57 // These bits are used to mark the field as a map and what the key
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,
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
78 // Describes a single field in a protobuf as it is represented as an ivar.
79 typedef struct GPBMessageFieldDescription {
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;
89 // The field number for the ivar.
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.
96 // Offset of the variable into it's structure struct.
98 // Field flags. Use accessor functions below.
100 // Data type of the ivar.
101 GPBDataType dataType;
102 } GPBMessageFieldDescription;
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.
106 typedef struct GPBMessageFieldDescriptionWithDefault {
107 // Default value for the ivar.
108 GPBGenericValue defaultValue;
110 GPBMessageFieldDescription core;
111 } GPBMessageFieldDescriptionWithDefault;
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,
123 typedef struct GPBExtensionDescription {
124 GPBGenericValue defaultValue;
125 const char *singletonName;
126 const char *extendedClass;
127 const char *messageOrGroupClassName;
128 GPBEnumDescriptorFunc enumDescriptorFunc;
130 GPBDataType dataType;
131 GPBExtensionOptions options;
132 } GPBExtensionDescription;
134 typedef NS_OPTIONS(uint32_t, GPBDescriptorInitializationFlags) {
135 GPBDescriptorInitializationFlag_None = 0,
136 GPBDescriptorInitializationFlag_FieldsWithDefault = 1 << 0,
137 GPBDescriptorInitializationFlag_WireFormat = 1 << 1,
140 @interface GPBDescriptor () {
144 uint32_t storageSize_;
147 // fieldDescriptions have to be long lived, they are held as raw pointers.
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;
157 - (instancetype)initWithClass:(Class)messageClass
158 file:(GPBFileDescriptor *)file
159 fields:(NSArray *)fields
160 storageSize:(uint32_t)storage
161 wireFormat:(BOOL)wireFormat;
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;
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;
184 @interface GPBOneofDescriptor () {
190 // name must be long lived.
191 - (instancetype)initWithName:(const char *)name fields:(NSArray *)fields;
194 @interface GPBFieldDescriptor () {
196 GPBMessageFieldDescription *description_;
197 GPB_UNSAFE_UNRETAINED GPBOneofDescriptor *containingOneof_;
201 SEL hasOrCountSel_; // *Count for map<>/repeated fields, has* otherwise.
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;
212 @interface GPBEnumDescriptor ()
213 // valueNames, values and extraTextFormatInfo have to be long lived, they are
214 // held as raw pointers.
216 allocDescriptorForName:(NSString *)name
217 valueNames:(const char *)valueNames
218 values:(const int32_t *)values
219 count:(uint32_t)valueCount
220 enumVerifier:(GPBEnumValidationFunc)enumVerifier;
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;
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;
236 @interface GPBExtensionDescriptor () {
238 GPBExtensionDescription *description_;
240 @property(nonatomic, readonly) GPBWireFormat wireType;
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;
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;
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"
262 GPB_INLINE BOOL GPBFieldIsMapOrArray(GPBFieldDescriptor *field) {
263 return (field->description_->flags &
264 (GPBFieldRepeated | GPBFieldMapKeyMask)) != 0;
267 GPB_INLINE GPBDataType GPBGetFieldDataType(GPBFieldDescriptor *field) {
268 return field->description_->dataType;
271 GPB_INLINE int32_t GPBFieldHasIndex(GPBFieldDescriptor *field) {
272 return field->description_->hasIndex;
275 GPB_INLINE uint32_t GPBFieldNumber(GPBFieldDescriptor *field) {
276 return field->description_->number;
279 #pragma clang diagnostic pop
281 uint32_t GPBFieldTag(GPBFieldDescriptor *self);
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.
287 uint32_t GPBFieldAlternateTag(GPBFieldDescriptor *self);
289 GPB_INLINE BOOL GPBHasPreservingUnknownEnumSemantics(GPBFileSyntax syntax) {
290 return syntax == GPBFileSyntaxProto3;
293 GPB_INLINE BOOL GPBExtensionIsRepeated(GPBExtensionDescription *description) {
294 return (description->options & GPBExtensionRepeated) != 0;
297 GPB_INLINE BOOL GPBExtensionIsPacked(GPBExtensionDescription *description) {
298 return (description->options & GPBExtensionPacked) != 0;
301 GPB_INLINE BOOL GPBExtensionIsWireFormat(GPBExtensionDescription *description) {
302 return (description->options & GPBExtensionSetWireFormat) != 0;
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)
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
318 // Sanity check that there isn't padding between the field description
319 // structures with and without a default.
320 GPBInternalCompileAssert(sizeof(GPBMessageFieldDescriptionWithDefault) ==
321 (sizeof(GPBGenericValue) +
322 sizeof(GPBMessageFieldDescription)),
323 DescriptionsWithDefault_different_size_than_expected);