blob: 7987d928700b4520148cf7783b7075d2b6dcadbf [file] [log] [blame]
Brian Silverman9c614bc2016-02-15 20:20:02 -05001// 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.
39typedef NS_OPTIONS(uint32_t, GPBFieldFlags) {
40 // These map to standard protobuf concepts.
41 GPBFieldRequired = 1 << 0,
42 GPBFieldRepeated = 1 << 1,
43 GPBFieldPacked = 1 << 2,
44 GPBFieldOptional = 1 << 3,
45 GPBFieldHasDefaultValue = 1 << 4,
46
47 // These are not standard protobuf concepts, they are specific to the
48 // Objective C runtime.
49
50 // These bits are used to mark the field as a map and what the key
51 // type is.
52 GPBFieldMapKeyMask = 0xF << 8,
53 GPBFieldMapKeyInt32 = 1 << 8,
54 GPBFieldMapKeyInt64 = 2 << 8,
55 GPBFieldMapKeyUInt32 = 3 << 8,
56 GPBFieldMapKeyUInt64 = 4 << 8,
57 GPBFieldMapKeySInt32 = 5 << 8,
58 GPBFieldMapKeySInt64 = 6 << 8,
59 GPBFieldMapKeyFixed32 = 7 << 8,
60 GPBFieldMapKeyFixed64 = 8 << 8,
61 GPBFieldMapKeySFixed32 = 9 << 8,
62 GPBFieldMapKeySFixed64 = 10 << 8,
63 GPBFieldMapKeyBool = 11 << 8,
64 GPBFieldMapKeyString = 12 << 8,
65
66 // Indicates the field needs custom handling for the TextFormat name, if not
67 // set, the name can be derived from the ObjC name.
68 GPBFieldTextFormatNameCustom = 1 << 16,
69 // Indicates the field has an enum descriptor.
70 GPBFieldHasEnumDescriptor = 1 << 17,
71};
72
73// Describes a single field in a protobuf as it is represented as an ivar.
74typedef struct GPBMessageFieldDescription {
75 // Name of ivar.
76 const char *name;
77 // The field number for the ivar.
78 uint32_t number;
79 // The index (in bits) into _has_storage_.
80 // > 0: the bit to use for a value being set.
81 // = 0: no storage used.
82 // < 0: in a oneOf, use a full int32 to record the field active.
83 int32_t hasIndex;
84 // Field flags. Use accessor functions below.
85 GPBFieldFlags flags;
86 // Data type of the ivar.
87 GPBDataType dataType;
88 // Offset of the variable into it's structure struct.
89 size_t offset;
90 // FieldOptions protobuf, serialized as string.
91 const char *fieldOptions;
92
93 GPBGenericValue defaultValue; // Default value for the ivar.
94 union {
95 const char *className; // Name for message class.
96 // For enums only: If EnumDescriptors are compiled in, it will be that,
97 // otherwise it will be the verifier.
98 GPBEnumDescriptorFunc enumDescFunc;
99 GPBEnumValidationFunc enumVerifier;
100 } dataTypeSpecific;
101} GPBMessageFieldDescription;
102
103// Describes a oneof.
104typedef struct GPBMessageOneofDescription {
105 // Name of this enum oneof.
106 const char *name;
107 // The index of this oneof in the has_storage.
108 int32_t index;
109} GPBMessageOneofDescription;
110
111// Describes an enum type defined in a .proto file.
112typedef struct GPBMessageEnumDescription {
113 GPBEnumDescriptorFunc enumDescriptorFunc;
114} GPBMessageEnumDescription;
115
116// Describes an individual enum constant of a particular type.
117typedef struct GPBMessageEnumValueDescription {
118 // Name of this enum constant.
119 const char *name;
120 // Numeric value of this enum constant.
121 int32_t number;
122} GPBMessageEnumValueDescription;
123
124// Describes attributes of the extension.
125typedef NS_OPTIONS(uint32_t, GPBExtensionOptions) {
126 // These map to standard protobuf concepts.
127 GPBExtensionRepeated = 1 << 0,
128 GPBExtensionPacked = 1 << 1,
129 GPBExtensionSetWireFormat = 1 << 2,
130};
131
132// An extension
133typedef struct GPBExtensionDescription {
134 const char *singletonName;
135 GPBDataType dataType;
136 const char *extendedClass;
137 int32_t fieldNumber;
138 GPBGenericValue defaultValue;
139 const char *messageOrGroupClassName;
140 GPBExtensionOptions options;
141 GPBEnumDescriptorFunc enumDescriptorFunc;
142} GPBExtensionDescription;
143
144@interface GPBDescriptor () {
145 @package
146 NSArray *fields_;
147 NSArray *oneofs_;
148 size_t storageSize_;
149}
150
151// fieldDescriptions, enumDescriptions, rangeDescriptions, and
152// extraTextFormatInfo have to be long lived, they are held as raw pointers.
153+ (instancetype)
154 allocDescriptorForClass:(Class)messageClass
155 rootClass:(Class)rootClass
156 file:(GPBFileDescriptor *)file
157 fields:(GPBMessageFieldDescription *)fieldDescriptions
158 fieldCount:(NSUInteger)fieldCount
159 oneofs:(GPBMessageOneofDescription *)oneofDescriptions
160 oneofCount:(NSUInteger)oneofCount
161 enums:(GPBMessageEnumDescription *)enumDescriptions
162 enumCount:(NSUInteger)enumCount
163 ranges:(const GPBExtensionRange *)ranges
164 rangeCount:(NSUInteger)rangeCount
165 storageSize:(size_t)storageSize
166 wireFormat:(BOOL)wireFormat;
167+ (instancetype)
168 allocDescriptorForClass:(Class)messageClass
169 rootClass:(Class)rootClass
170 file:(GPBFileDescriptor *)file
171 fields:(GPBMessageFieldDescription *)fieldDescriptions
172 fieldCount:(NSUInteger)fieldCount
173 oneofs:(GPBMessageOneofDescription *)oneofDescriptions
174 oneofCount:(NSUInteger)oneofCount
175 enums:(GPBMessageEnumDescription *)enumDescriptions
176 enumCount:(NSUInteger)enumCount
177 ranges:(const GPBExtensionRange *)ranges
178 rangeCount:(NSUInteger)rangeCount
179 storageSize:(size_t)storageSize
180 wireFormat:(BOOL)wireFormat
181 extraTextFormatInfo:(const char *)extraTextFormatInfo;
182
183- (instancetype)initWithClass:(Class)messageClass
184 file:(GPBFileDescriptor *)file
185 fields:(NSArray *)fields
186 oneofs:(NSArray *)oneofs
187 enums:(NSArray *)enums
188 extensionRanges:(const GPBExtensionRange *)ranges
189 extensionRangesCount:(NSUInteger)rangeCount
190 storageSize:(size_t)storage
191 wireFormat:(BOOL)wireFormat;
192
193@end
194
195@interface GPBFileDescriptor ()
196- (instancetype)initWithPackage:(NSString *)package
197 syntax:(GPBFileSyntax)syntax;
198@end
199
200@interface GPBOneofDescriptor () {
201 @package
202 GPBMessageOneofDescription *oneofDescription_;
203 NSArray *fields_;
204
205 SEL caseSel_;
206}
207- (instancetype)initWithOneofDescription:
208 (GPBMessageOneofDescription *)oneofDescription
209 fields:(NSArray *)fields;
210@end
211
212@interface GPBFieldDescriptor () {
213 @package
214 GPBMessageFieldDescription *description_;
215 GPB_UNSAFE_UNRETAINED GPBOneofDescriptor *containingOneof_;
216
217 SEL getSel_;
218 SEL setSel_;
219 SEL hasOrCountSel_; // *Count for map<>/repeated fields, has* otherwise.
220 SEL setHasSel_;
221}
222
223// Single initializer
224// description has to be long lived, it is held as a raw pointer.
225- (instancetype)initWithFieldDescription:
226 (GPBMessageFieldDescription *)description
227 rootClass:(Class)rootClass
228 syntax:(GPBFileSyntax)syntax;
229@end
230
231@interface GPBEnumDescriptor ()
232// valueDescriptions and extraTextFormatInfo have to be long lived, they are
233// held as raw pointers.
234+ (instancetype)
235 allocDescriptorForName:(NSString *)name
236 values:(GPBMessageEnumValueDescription *)valueDescriptions
237 valueCount:(NSUInteger)valueCount
238 enumVerifier:(GPBEnumValidationFunc)enumVerifier;
239+ (instancetype)
240 allocDescriptorForName:(NSString *)name
241 values:(GPBMessageEnumValueDescription *)valueDescriptions
242 valueCount:(NSUInteger)valueCount
243 enumVerifier:(GPBEnumValidationFunc)enumVerifier
244 extraTextFormatInfo:(const char *)extraTextFormatInfo;
245
246- (instancetype)initWithName:(NSString *)name
247 values:(GPBMessageEnumValueDescription *)valueDescriptions
248 valueCount:(NSUInteger)valueCount
249 enumVerifier:(GPBEnumValidationFunc)enumVerifier;
250@end
251
252@interface GPBExtensionDescriptor () {
253 @package
254 GPBExtensionDescription *description_;
255}
256@property(nonatomic, readonly) GPBWireFormat wireType;
257
258// For repeated extensions, alternateWireType is the wireType with the opposite
259// value for the packable property. i.e. - if the extension was marked packed
260// it would be the wire type for unpacked; if the extension was marked unpacked,
261// it would be the wire type for packed.
262@property(nonatomic, readonly) GPBWireFormat alternateWireType;
263
264// description has to be long lived, it is held as a raw pointer.
265- (instancetype)initWithExtensionDescription:
266 (GPBExtensionDescription *)description;
267- (NSComparisonResult)compareByFieldNumber:(GPBExtensionDescriptor *)other;
268@end
269
270CF_EXTERN_C_BEGIN
271
272GPB_INLINE BOOL GPBFieldIsMapOrArray(GPBFieldDescriptor *field) {
273 return (field->description_->flags &
274 (GPBFieldRepeated | GPBFieldMapKeyMask)) != 0;
275}
276
277GPB_INLINE GPBDataType GPBGetFieldDataType(GPBFieldDescriptor *field) {
278 return field->description_->dataType;
279}
280
281GPB_INLINE int32_t GPBFieldHasIndex(GPBFieldDescriptor *field) {
282 return field->description_->hasIndex;
283}
284
285GPB_INLINE uint32_t GPBFieldNumber(GPBFieldDescriptor *field) {
286 return field->description_->number;
287}
288
289uint32_t GPBFieldTag(GPBFieldDescriptor *self);
290
291// For repeated fields, alternateWireType is the wireType with the opposite
292// value for the packable property. i.e. - if the field was marked packed it
293// would be the wire type for unpacked; if the field was marked unpacked, it
294// would be the wire type for packed.
295uint32_t GPBFieldAlternateTag(GPBFieldDescriptor *self);
296
297GPB_INLINE BOOL GPBPreserveUnknownFields(GPBFileSyntax syntax) {
298 return syntax != GPBFileSyntaxProto3;
299}
300
301GPB_INLINE BOOL GPBHasPreservingUnknownEnumSemantics(GPBFileSyntax syntax) {
302 return syntax == GPBFileSyntaxProto3;
303}
304
305GPB_INLINE BOOL GPBExtensionIsRepeated(GPBExtensionDescription *description) {
306 return (description->options & GPBExtensionRepeated) != 0;
307}
308
309GPB_INLINE BOOL GPBExtensionIsPacked(GPBExtensionDescription *description) {
310 return (description->options & GPBExtensionPacked) != 0;
311}
312
313GPB_INLINE BOOL GPBExtensionIsWireFormat(GPBExtensionDescription *description) {
314 return (description->options & GPBExtensionSetWireFormat) != 0;
315}
316
317
318CF_EXTERN_C_END