added iOS source code
[wl-app.git] / iOS / Pods / Protobuf / objectivec / GPBDescriptor_PackagePrivate.h
1 // 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.
39 typedef NS_OPTIONS(uint16_t, GPBFieldFlags) {
40   GPBFieldNone            = 0,
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,
47
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,
53
54   // 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,
72 };
73
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
76 // field/extension.
77
78 // Describes a single field in a protobuf as it is represented as an ivar.
79 typedef struct GPBMessageFieldDescription {
80   // Name of ivar.
81   const char *name;
82   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;
88   } dataTypeSpecific;
89   // 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;
102 } GPBMessageFieldDescription;
103
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;
109
110   GPBMessageFieldDescription core;
111 } GPBMessageFieldDescriptionWithDefault;
112
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,
120 };
121
122 // An extension
123 typedef struct GPBExtensionDescription {
124   GPBGenericValue defaultValue;
125   const char *singletonName;
126   const char *extendedClass;
127   const char *messageOrGroupClassName;
128   GPBEnumDescriptorFunc enumDescriptorFunc;
129   int32_t fieldNumber;
130   GPBDataType dataType;
131   GPBExtensionOptions options;
132 } GPBExtensionDescription;
133
134 typedef NS_OPTIONS(uint32_t, GPBDescriptorInitializationFlags) {
135   GPBDescriptorInitializationFlag_None              = 0,
136   GPBDescriptorInitializationFlag_FieldsWithDefault = 1 << 0,
137   GPBDescriptorInitializationFlag_WireFormat        = 1 << 1,
138 };
139
140 @interface GPBDescriptor () {
141  @package
142   NSArray *fields_;
143   NSArray *oneofs_;
144   uint32_t storageSize_;
145 }
146
147 // fieldDescriptions have to be long lived, they are held as raw pointers.
148 + (instancetype)
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;
156
157 - (instancetype)initWithClass:(Class)messageClass
158                          file:(GPBFileDescriptor *)file
159                        fields:(NSArray *)fields
160                   storageSize:(uint32_t)storage
161                    wireFormat:(BOOL)wireFormat;
162
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;
173
174 @end
175
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;
182 @end
183
184 @interface GPBOneofDescriptor () {
185  @package
186   const char *name_;
187   NSArray *fields_;
188   SEL caseSel_;
189 }
190 // name must be long lived.
191 - (instancetype)initWithName:(const char *)name fields:(NSArray *)fields;
192 @end
193
194 @interface GPBFieldDescriptor () {
195  @package
196   GPBMessageFieldDescription *description_;
197   GPB_UNSAFE_UNRETAINED GPBOneofDescriptor *containingOneof_;
198
199   SEL getSel_;
200   SEL setSel_;
201   SEL hasOrCountSel_;  // *Count for map<>/repeated fields, has* otherwise.
202   SEL setHasSel_;
203 }
204
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;
210 @end
211
212 @interface GPBEnumDescriptor ()
213 // valueNames, values and extraTextFormatInfo have to be long lived, they are
214 // held as raw pointers.
215 + (instancetype)
216     allocDescriptorForName:(NSString *)name
217                 valueNames:(const char *)valueNames
218                     values:(const int32_t *)values
219                      count:(uint32_t)valueCount
220               enumVerifier:(GPBEnumValidationFunc)enumVerifier;
221 + (instancetype)
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;
228
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;
234 @end
235
236 @interface GPBExtensionDescriptor () {
237  @package
238   GPBExtensionDescription *description_;
239 }
240 @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;
247
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;
252 @end
253
254 CF_EXTERN_C_BEGIN
255
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"
261
262 GPB_INLINE BOOL GPBFieldIsMapOrArray(GPBFieldDescriptor *field) {
263   return (field->description_->flags &
264           (GPBFieldRepeated | GPBFieldMapKeyMask)) != 0;
265 }
266
267 GPB_INLINE GPBDataType GPBGetFieldDataType(GPBFieldDescriptor *field) {
268   return field->description_->dataType;
269 }
270
271 GPB_INLINE int32_t GPBFieldHasIndex(GPBFieldDescriptor *field) {
272   return field->description_->hasIndex;
273 }
274
275 GPB_INLINE uint32_t GPBFieldNumber(GPBFieldDescriptor *field) {
276   return field->description_->number;
277 }
278
279 #pragma clang diagnostic pop
280
281 uint32_t GPBFieldTag(GPBFieldDescriptor *self);
282
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);
288
289 GPB_INLINE BOOL GPBHasPreservingUnknownEnumSemantics(GPBFileSyntax syntax) {
290   return syntax == GPBFileSyntaxProto3;
291 }
292
293 GPB_INLINE BOOL GPBExtensionIsRepeated(GPBExtensionDescription *description) {
294   return (description->options & GPBExtensionRepeated) != 0;
295 }
296
297 GPB_INLINE BOOL GPBExtensionIsPacked(GPBExtensionDescription *description) {
298   return (description->options & GPBExtensionPacked) != 0;
299 }
300
301 GPB_INLINE BOOL GPBExtensionIsWireFormat(GPBExtensionDescription *description) {
302   return (description->options & GPBExtensionSetWireFormat) != 0;
303 }
304
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)
309   #else
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
317
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);
324
325 CF_EXTERN_C_END