OLD | NEW |
1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
2 // Copyright 2008 Google Inc. All rights reserved. | 2 // Copyright 2008 Google Inc. All rights reserved. |
3 // https://developers.google.com/protocol-buffers/ | 3 // https://developers.google.com/protocol-buffers/ |
4 // | 4 // |
5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
7 // met: | 7 // met: |
8 // | 8 // |
9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
(...skipping 18 matching lines...) Expand all Loading... |
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
30 | 30 |
31 // This header is private to the ProtobolBuffers library and must NOT be | 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 | 32 // included by any sources outside this library. The contents of this file are |
33 // subject to change at any time without notice. | 33 // subject to change at any time without notice. |
34 | 34 |
35 #import "GPBDescriptor.h" | 35 #import "GPBDescriptor.h" |
36 #import "GPBWireFormat.h" | 36 #import "GPBWireFormat.h" |
37 | 37 |
38 // Describes attributes of the field. | 38 // Describes attributes of the field. |
39 typedef NS_OPTIONS(uint32_t, GPBFieldFlags) { | 39 typedef NS_OPTIONS(uint16_t, GPBFieldFlags) { |
40 // These map to standard protobuf concepts. | 40 // These map to standard protobuf concepts. |
41 GPBFieldRequired = 1 << 0, | 41 GPBFieldRequired = 1 << 0, |
42 GPBFieldRepeated = 1 << 1, | 42 GPBFieldRepeated = 1 << 1, |
43 GPBFieldPacked = 1 << 2, | 43 GPBFieldPacked = 1 << 2, |
44 GPBFieldOptional = 1 << 3, | 44 GPBFieldOptional = 1 << 3, |
45 GPBFieldHasDefaultValue = 1 << 4, | 45 GPBFieldHasDefaultValue = 1 << 4, |
46 | 46 |
| 47 // Indicates the field needs custom handling for the TextFormat name, if not |
| 48 // set, the name can be derived from the ObjC name. |
| 49 GPBFieldTextFormatNameCustom = 1 << 6, |
| 50 // Indicates the field has an enum descriptor. |
| 51 GPBFieldHasEnumDescriptor = 1 << 7, |
| 52 |
47 // These are not standard protobuf concepts, they are specific to the | 53 // These are not standard protobuf concepts, they are specific to the |
48 // Objective C runtime. | 54 // Objective C runtime. |
49 | 55 |
50 // These bits are used to mark the field as a map and what the key | 56 // These bits are used to mark the field as a map and what the key |
51 // type is. | 57 // type is. |
52 GPBFieldMapKeyMask = 0xF << 8, | 58 GPBFieldMapKeyMask = 0xF << 8, |
53 GPBFieldMapKeyInt32 = 1 << 8, | 59 GPBFieldMapKeyInt32 = 1 << 8, |
54 GPBFieldMapKeyInt64 = 2 << 8, | 60 GPBFieldMapKeyInt64 = 2 << 8, |
55 GPBFieldMapKeyUInt32 = 3 << 8, | 61 GPBFieldMapKeyUInt32 = 3 << 8, |
56 GPBFieldMapKeyUInt64 = 4 << 8, | 62 GPBFieldMapKeyUInt64 = 4 << 8, |
57 GPBFieldMapKeySInt32 = 5 << 8, | 63 GPBFieldMapKeySInt32 = 5 << 8, |
58 GPBFieldMapKeySInt64 = 6 << 8, | 64 GPBFieldMapKeySInt64 = 6 << 8, |
59 GPBFieldMapKeyFixed32 = 7 << 8, | 65 GPBFieldMapKeyFixed32 = 7 << 8, |
60 GPBFieldMapKeyFixed64 = 8 << 8, | 66 GPBFieldMapKeyFixed64 = 8 << 8, |
61 GPBFieldMapKeySFixed32 = 9 << 8, | 67 GPBFieldMapKeySFixed32 = 9 << 8, |
62 GPBFieldMapKeySFixed64 = 10 << 8, | 68 GPBFieldMapKeySFixed64 = 10 << 8, |
63 GPBFieldMapKeyBool = 11 << 8, | 69 GPBFieldMapKeyBool = 11 << 8, |
64 GPBFieldMapKeyString = 12 << 8, | 70 GPBFieldMapKeyString = 12 << 8, |
| 71 }; |
65 | 72 |
66 // Indicates the field needs custom handling for the TextFormat name, if not | 73 // NOTE: The structures defined here have their members ordered to minimize |
67 // set, the name can be derived from the ObjC name. | 74 // their size. This directly impacts the size of apps since these exist per |
68 GPBFieldTextFormatNameCustom = 1 << 16, | 75 // field/extension. |
69 // Indicates the field has an enum descriptor. | |
70 GPBFieldHasEnumDescriptor = 1 << 17, | |
71 }; | |
72 | 76 |
73 // Describes a single field in a protobuf as it is represented as an ivar. | 77 // Describes a single field in a protobuf as it is represented as an ivar. |
74 typedef struct GPBMessageFieldDescription { | 78 typedef struct GPBMessageFieldDescription { |
75 // Name of ivar. | 79 // Name of ivar. |
76 const char *name; | 80 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 { | 81 union { |
95 const char *className; // Name for message class. | 82 const char *className; // Name for message class. |
96 // For enums only: If EnumDescriptors are compiled in, it will be that, | 83 // For enums only: If EnumDescriptors are compiled in, it will be that, |
97 // otherwise it will be the verifier. | 84 // otherwise it will be the verifier. |
98 GPBEnumDescriptorFunc enumDescFunc; | 85 GPBEnumDescriptorFunc enumDescFunc; |
99 GPBEnumValidationFunc enumVerifier; | 86 GPBEnumValidationFunc enumVerifier; |
100 } dataTypeSpecific; | 87 } dataTypeSpecific; |
| 88 // The field number for the ivar. |
| 89 uint32_t number; |
| 90 // The index (in bits) into _has_storage_. |
| 91 // >= 0: the bit to use for a value being set. |
| 92 // = GPBNoHasBit(INT32_MAX): no storage used. |
| 93 // < 0: in a oneOf, use a full int32 to record the field active. |
| 94 int32_t hasIndex; |
| 95 // Offset of the variable into it's structure struct. |
| 96 uint32_t offset; |
| 97 // Field flags. Use accessor functions below. |
| 98 GPBFieldFlags flags; |
| 99 // Data type of the ivar. |
| 100 GPBDataType dataType; |
101 } GPBMessageFieldDescription; | 101 } GPBMessageFieldDescription; |
102 | 102 |
103 // Describes a oneof. | 103 // Fields in messages defined in a 'proto2' syntax file can provide a default |
104 typedef struct GPBMessageOneofDescription { | 104 // value. This struct provides the default along with the field info. |
105 // Name of this enum oneof. | 105 typedef struct GPBMessageFieldDescriptionWithDefault { |
106 const char *name; | 106 // Default value for the ivar. |
107 // The index of this oneof in the has_storage. | 107 GPBGenericValue defaultValue; |
108 int32_t index; | |
109 } GPBMessageOneofDescription; | |
110 | 108 |
111 // Describes an enum type defined in a .proto file. | 109 GPBMessageFieldDescription core; |
112 typedef struct GPBMessageEnumDescription { | 110 } GPBMessageFieldDescriptionWithDefault; |
113 GPBEnumDescriptorFunc enumDescriptorFunc; | |
114 } GPBMessageEnumDescription; | |
115 | |
116 // Describes an individual enum constant of a particular type. | |
117 typedef 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 | 111 |
124 // Describes attributes of the extension. | 112 // Describes attributes of the extension. |
125 typedef NS_OPTIONS(uint32_t, GPBExtensionOptions) { | 113 typedef NS_OPTIONS(uint8_t, GPBExtensionOptions) { |
126 // These map to standard protobuf concepts. | 114 // These map to standard protobuf concepts. |
127 GPBExtensionRepeated = 1 << 0, | 115 GPBExtensionRepeated = 1 << 0, |
128 GPBExtensionPacked = 1 << 1, | 116 GPBExtensionPacked = 1 << 1, |
129 GPBExtensionSetWireFormat = 1 << 2, | 117 GPBExtensionSetWireFormat = 1 << 2, |
130 }; | 118 }; |
131 | 119 |
132 // An extension | 120 // An extension |
133 typedef struct GPBExtensionDescription { | 121 typedef struct GPBExtensionDescription { |
| 122 GPBGenericValue defaultValue; |
134 const char *singletonName; | 123 const char *singletonName; |
| 124 const char *extendedClass; |
| 125 const char *messageOrGroupClassName; |
| 126 GPBEnumDescriptorFunc enumDescriptorFunc; |
| 127 int32_t fieldNumber; |
135 GPBDataType dataType; | 128 GPBDataType dataType; |
136 const char *extendedClass; | |
137 int32_t fieldNumber; | |
138 GPBGenericValue defaultValue; | |
139 const char *messageOrGroupClassName; | |
140 GPBExtensionOptions options; | 129 GPBExtensionOptions options; |
141 GPBEnumDescriptorFunc enumDescriptorFunc; | |
142 } GPBExtensionDescription; | 130 } GPBExtensionDescription; |
143 | 131 |
| 132 typedef NS_OPTIONS(uint32_t, GPBDescriptorInitializationFlags) { |
| 133 GPBDescriptorInitializationFlag_FieldsWithDefault = 1 << 0, |
| 134 GPBDescriptorInitializationFlag_WireFormat = 1 << 1, |
| 135 }; |
| 136 |
144 @interface GPBDescriptor () { | 137 @interface GPBDescriptor () { |
145 @package | 138 @package |
146 NSArray *fields_; | 139 NSArray *fields_; |
147 NSArray *oneofs_; | 140 NSArray *oneofs_; |
148 size_t storageSize_; | 141 uint32_t storageSize_; |
149 } | 142 } |
150 | 143 |
151 // fieldDescriptions, enumDescriptions, rangeDescriptions, and | 144 // fieldDescriptions have to be long lived, they are held as raw pointers. |
152 // extraTextFormatInfo have to be long lived, they are held as raw pointers. | |
153 + (instancetype) | 145 + (instancetype) |
154 allocDescriptorForClass:(Class)messageClass | 146 allocDescriptorForClass:(Class)messageClass |
155 rootClass:(Class)rootClass | 147 rootClass:(Class)rootClass |
156 file:(GPBFileDescriptor *)file | 148 file:(GPBFileDescriptor *)file |
157 fields:(GPBMessageFieldDescription *)fieldDescriptions | 149 fields:(void *)fieldDescriptions |
158 fieldCount:(NSUInteger)fieldCount | 150 fieldCount:(uint32_t)fieldCount |
159 oneofs:(GPBMessageOneofDescription *)oneofDescriptions | 151 storageSize:(uint32_t)storageSize |
160 oneofCount:(NSUInteger)oneofCount | 152 flags:(GPBDescriptorInitializationFlags)flags; |
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 | 153 |
183 - (instancetype)initWithClass:(Class)messageClass | 154 - (instancetype)initWithClass:(Class)messageClass |
184 file:(GPBFileDescriptor *)file | 155 file:(GPBFileDescriptor *)file |
185 fields:(NSArray *)fields | 156 fields:(NSArray *)fields |
186 oneofs:(NSArray *)oneofs | 157 storageSize:(uint32_t)storage |
187 enums:(NSArray *)enums | |
188 extensionRanges:(const GPBExtensionRange *)ranges | |
189 extensionRangesCount:(NSUInteger)rangeCount | |
190 storageSize:(size_t)storage | |
191 wireFormat:(BOOL)wireFormat; | 158 wireFormat:(BOOL)wireFormat; |
192 | 159 |
| 160 // Called right after init to provide extra information to avoid init having |
| 161 // an explosion of args. These pointers are recorded, so they are expected |
| 162 // to live for the lifetime of the app. |
| 163 - (void)setupOneofs:(const char **)oneofNames |
| 164 count:(uint32_t)count |
| 165 firstHasIndex:(int32_t)firstHasIndex; |
| 166 - (void)setupExtraTextInfo:(const char *)extraTextFormatInfo; |
| 167 - (void)setupExtensionRanges:(const GPBExtensionRange *)ranges count:(int32_t)co
unt; |
| 168 |
193 @end | 169 @end |
194 | 170 |
195 @interface GPBFileDescriptor () | 171 @interface GPBFileDescriptor () |
196 - (instancetype)initWithPackage:(NSString *)package | 172 - (instancetype)initWithPackage:(NSString *)package |
197 syntax:(GPBFileSyntax)syntax; | 173 syntax:(GPBFileSyntax)syntax; |
198 @end | 174 @end |
199 | 175 |
200 @interface GPBOneofDescriptor () { | 176 @interface GPBOneofDescriptor () { |
201 @package | 177 @package |
202 GPBMessageOneofDescription *oneofDescription_; | 178 const char *name_; |
203 NSArray *fields_; | 179 NSArray *fields_; |
204 | |
205 SEL caseSel_; | 180 SEL caseSel_; |
206 } | 181 } |
207 - (instancetype)initWithOneofDescription: | 182 // name must be long lived. |
208 (GPBMessageOneofDescription *)oneofDescription | 183 - (instancetype)initWithName:(const char *)name fields:(NSArray *)fields; |
209 fields:(NSArray *)fields; | |
210 @end | 184 @end |
211 | 185 |
212 @interface GPBFieldDescriptor () { | 186 @interface GPBFieldDescriptor () { |
213 @package | 187 @package |
214 GPBMessageFieldDescription *description_; | 188 GPBMessageFieldDescription *description_; |
215 GPB_UNSAFE_UNRETAINED GPBOneofDescriptor *containingOneof_; | 189 GPB_UNSAFE_UNRETAINED GPBOneofDescriptor *containingOneof_; |
216 | 190 |
217 SEL getSel_; | 191 SEL getSel_; |
218 SEL setSel_; | 192 SEL setSel_; |
219 SEL hasOrCountSel_; // *Count for map<>/repeated fields, has* otherwise. | 193 SEL hasOrCountSel_; // *Count for map<>/repeated fields, has* otherwise. |
220 SEL setHasSel_; | 194 SEL setHasSel_; |
221 } | 195 } |
222 | 196 |
223 // Single initializer | 197 // Single initializer |
224 // description has to be long lived, it is held as a raw pointer. | 198 // description has to be long lived, it is held as a raw pointer. |
225 - (instancetype)initWithFieldDescription: | 199 - (instancetype)initWithFieldDescription:(void *)description |
226 (GPBMessageFieldDescription *)description | 200 includesDefault:(BOOL)includesDefault |
227 rootClass:(Class)rootClass | |
228 syntax:(GPBFileSyntax)syntax; | 201 syntax:(GPBFileSyntax)syntax; |
229 @end | 202 @end |
230 | 203 |
231 @interface GPBEnumDescriptor () | 204 @interface GPBEnumDescriptor () |
232 // valueDescriptions and extraTextFormatInfo have to be long lived, they are | 205 // valueNames, values and extraTextFormatInfo have to be long lived, they are |
233 // held as raw pointers. | 206 // held as raw pointers. |
234 + (instancetype) | 207 + (instancetype) |
235 allocDescriptorForName:(NSString *)name | 208 allocDescriptorForName:(NSString *)name |
236 values:(GPBMessageEnumValueDescription *)valueDescriptions | 209 valueNames:(const char *)valueNames |
237 valueCount:(NSUInteger)valueCount | 210 values:(const int32_t *)values |
| 211 count:(uint32_t)valueCount |
238 enumVerifier:(GPBEnumValidationFunc)enumVerifier; | 212 enumVerifier:(GPBEnumValidationFunc)enumVerifier; |
239 + (instancetype) | 213 + (instancetype) |
240 allocDescriptorForName:(NSString *)name | 214 allocDescriptorForName:(NSString *)name |
241 values:(GPBMessageEnumValueDescription *)valueDescriptions | 215 valueNames:(const char *)valueNames |
242 valueCount:(NSUInteger)valueCount | 216 values:(const int32_t *)values |
| 217 count:(uint32_t)valueCount |
243 enumVerifier:(GPBEnumValidationFunc)enumVerifier | 218 enumVerifier:(GPBEnumValidationFunc)enumVerifier |
244 extraTextFormatInfo:(const char *)extraTextFormatInfo; | 219 extraTextFormatInfo:(const char *)extraTextFormatInfo; |
245 | 220 |
246 - (instancetype)initWithName:(NSString *)name | 221 - (instancetype)initWithName:(NSString *)name |
247 values:(GPBMessageEnumValueDescription *)valueDescriptions | 222 valueNames:(const char *)valueNames |
248 valueCount:(NSUInteger)valueCount | 223 values:(const int32_t *)values |
| 224 count:(uint32_t)valueCount |
249 enumVerifier:(GPBEnumValidationFunc)enumVerifier; | 225 enumVerifier:(GPBEnumValidationFunc)enumVerifier; |
250 @end | 226 @end |
251 | 227 |
252 @interface GPBExtensionDescriptor () { | 228 @interface GPBExtensionDescriptor () { |
253 @package | 229 @package |
254 GPBExtensionDescription *description_; | 230 GPBExtensionDescription *description_; |
255 } | 231 } |
256 @property(nonatomic, readonly) GPBWireFormat wireType; | 232 @property(nonatomic, readonly) GPBWireFormat wireType; |
257 | 233 |
258 // For repeated extensions, alternateWireType is the wireType with the opposite | 234 // For repeated extensions, alternateWireType is the wireType with the opposite |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
307 } | 283 } |
308 | 284 |
309 GPB_INLINE BOOL GPBExtensionIsPacked(GPBExtensionDescription *description) { | 285 GPB_INLINE BOOL GPBExtensionIsPacked(GPBExtensionDescription *description) { |
310 return (description->options & GPBExtensionPacked) != 0; | 286 return (description->options & GPBExtensionPacked) != 0; |
311 } | 287 } |
312 | 288 |
313 GPB_INLINE BOOL GPBExtensionIsWireFormat(GPBExtensionDescription *description) { | 289 GPB_INLINE BOOL GPBExtensionIsWireFormat(GPBExtensionDescription *description) { |
314 return (description->options & GPBExtensionSetWireFormat) != 0; | 290 return (description->options & GPBExtensionSetWireFormat) != 0; |
315 } | 291 } |
316 | 292 |
| 293 // Helper for compile time assets. |
| 294 #ifndef _GPBCompileAssert |
| 295 #if __has_feature(c_static_assert) || __has_extension(c_static_assert) |
| 296 #define _GPBCompileAssert(test, msg) _Static_assert((test), #msg) |
| 297 #else |
| 298 // Pre-Xcode 7 support. |
| 299 #define _GPBCompileAssertSymbolInner(line, msg) _GPBCompileAssert ## line ##
__ ## msg |
| 300 #define _GPBCompileAssertSymbol(line, msg) _GPBCompileAssertSymbolInner(line
, msg) |
| 301 #define _GPBCompileAssert(test, msg) \ |
| 302 typedef char _GPBCompileAssertSymbol(__LINE__, msg) [ ((test) ? 1 : -1)
] |
| 303 #endif // __has_feature(c_static_assert) || __has_extension(c_static_assert) |
| 304 #endif // _GPBCompileAssert |
| 305 |
| 306 // Sanity check that there isn't padding between the field description |
| 307 // structures with and without a default. |
| 308 _GPBCompileAssert(sizeof(GPBMessageFieldDescriptionWithDefault) == |
| 309 (sizeof(GPBGenericValue) + |
| 310 sizeof(GPBMessageFieldDescription)), |
| 311 DescriptionsWithDefault_different_size_than_expected); |
317 | 312 |
318 CF_EXTERN_C_END | 313 CF_EXTERN_C_END |
OLD | NEW |