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 #import <Foundation/Foundation.h>
32
33 #import "GPBUtilities.h"
34
35 #import "GPBDescriptor_PackagePrivate.h"
36
37 // Macros for stringifying library symbols. These are used in the generated
38 // PB descriptor classes wherever a library symbol name is represented as a
39 // string. See README.google for more information.
40 #define GPBStringify(S) #S
41 #define GPBStringifySymbol(S) GPBStringify(S)
42
43 #define GPBNSStringify(S) @#S
44 #define GPBNSStringifySymbol(S) GPBNSStringify(S)
45
46 // Constant to internally mark when there is no has bit.
47 #define GPBNoHasBit INT32_MAX
48
49 CF_EXTERN_C_BEGIN
50
51 // These two are used to inject a runtime check for version mismatch into the
52 // generated sources to make sure they are linked with a supporting runtime.
53 void GPBCheckRuntimeVersionInternal(int32_t version);
GPBDebugCheckRuntimeVersion()54 GPB_INLINE void GPBDebugCheckRuntimeVersion() {
55 #if defined(DEBUG) && DEBUG
56 GPBCheckRuntimeVersionInternal(GOOGLE_PROTOBUF_OBJC_GEN_VERSION);
57 #endif
58 }
59
60 // Conversion functions for de/serializing floating point types.
61
GPBConvertDoubleToInt64(double v)62 GPB_INLINE int64_t GPBConvertDoubleToInt64(double v) {
63 union { double f; int64_t i; } u;
64 u.f = v;
65 return u.i;
66 }
67
GPBConvertFloatToInt32(float v)68 GPB_INLINE int32_t GPBConvertFloatToInt32(float v) {
69 union { float f; int32_t i; } u;
70 u.f = v;
71 return u.i;
72 }
73
GPBConvertInt64ToDouble(int64_t v)74 GPB_INLINE double GPBConvertInt64ToDouble(int64_t v) {
75 union { double f; int64_t i; } u;
76 u.i = v;
77 return u.f;
78 }
79
GPBConvertInt32ToFloat(int32_t v)80 GPB_INLINE float GPBConvertInt32ToFloat(int32_t v) {
81 union { float f; int32_t i; } u;
82 u.i = v;
83 return u.f;
84 }
85
GPBLogicalRightShift32(int32_t value,int32_t spaces)86 GPB_INLINE int32_t GPBLogicalRightShift32(int32_t value, int32_t spaces) {
87 return (int32_t)((uint32_t)(value) >> spaces);
88 }
89
GPBLogicalRightShift64(int64_t value,int32_t spaces)90 GPB_INLINE int64_t GPBLogicalRightShift64(int64_t value, int32_t spaces) {
91 return (int64_t)((uint64_t)(value) >> spaces);
92 }
93
94 // Decode a ZigZag-encoded 32-bit value. ZigZag encodes signed integers
95 // into values that can be efficiently encoded with varint. (Otherwise,
96 // negative values must be sign-extended to 64 bits to be varint encoded,
97 // thus always taking 10 bytes on the wire.)
GPBDecodeZigZag32(uint32_t n)98 GPB_INLINE int32_t GPBDecodeZigZag32(uint32_t n) {
99 return GPBLogicalRightShift32(n, 1) ^ -(n & 1);
100 }
101
102 // Decode a ZigZag-encoded 64-bit value. ZigZag encodes signed integers
103 // into values that can be efficiently encoded with varint. (Otherwise,
104 // negative values must be sign-extended to 64 bits to be varint encoded,
105 // thus always taking 10 bytes on the wire.)
GPBDecodeZigZag64(uint64_t n)106 GPB_INLINE int64_t GPBDecodeZigZag64(uint64_t n) {
107 return GPBLogicalRightShift64(n, 1) ^ -(n & 1);
108 }
109
110 // Encode a ZigZag-encoded 32-bit value. ZigZag encodes signed integers
111 // into values that can be efficiently encoded with varint. (Otherwise,
112 // negative values must be sign-extended to 64 bits to be varint encoded,
113 // thus always taking 10 bytes on the wire.)
GPBEncodeZigZag32(int32_t n)114 GPB_INLINE uint32_t GPBEncodeZigZag32(int32_t n) {
115 // Note: the right-shift must be arithmetic
116 return (n << 1) ^ (n >> 31);
117 }
118
119 // Encode a ZigZag-encoded 64-bit value. ZigZag encodes signed integers
120 // into values that can be efficiently encoded with varint. (Otherwise,
121 // negative values must be sign-extended to 64 bits to be varint encoded,
122 // thus always taking 10 bytes on the wire.)
GPBEncodeZigZag64(int64_t n)123 GPB_INLINE uint64_t GPBEncodeZigZag64(int64_t n) {
124 // Note: the right-shift must be arithmetic
125 return (n << 1) ^ (n >> 63);
126 }
127
128 #pragma clang diagnostic push
129 #pragma clang diagnostic ignored "-Wswitch-enum"
130 #pragma clang diagnostic ignored "-Wdirect-ivar-access"
131
GPBDataTypeIsObject(GPBDataType type)132 GPB_INLINE BOOL GPBDataTypeIsObject(GPBDataType type) {
133 switch (type) {
134 case GPBDataTypeBytes:
135 case GPBDataTypeString:
136 case GPBDataTypeMessage:
137 case GPBDataTypeGroup:
138 return YES;
139 default:
140 return NO;
141 }
142 }
143
GPBDataTypeIsMessage(GPBDataType type)144 GPB_INLINE BOOL GPBDataTypeIsMessage(GPBDataType type) {
145 switch (type) {
146 case GPBDataTypeMessage:
147 case GPBDataTypeGroup:
148 return YES;
149 default:
150 return NO;
151 }
152 }
153
GPBFieldDataTypeIsMessage(GPBFieldDescriptor * field)154 GPB_INLINE BOOL GPBFieldDataTypeIsMessage(GPBFieldDescriptor *field) {
155 return GPBDataTypeIsMessage(field->description_->dataType);
156 }
157
GPBFieldDataTypeIsObject(GPBFieldDescriptor * field)158 GPB_INLINE BOOL GPBFieldDataTypeIsObject(GPBFieldDescriptor *field) {
159 return GPBDataTypeIsObject(field->description_->dataType);
160 }
161
GPBExtensionIsMessage(GPBExtensionDescriptor * ext)162 GPB_INLINE BOOL GPBExtensionIsMessage(GPBExtensionDescriptor *ext) {
163 return GPBDataTypeIsMessage(ext->description_->dataType);
164 }
165
166 // The field is an array/map or it has an object value.
GPBFieldStoresObject(GPBFieldDescriptor * field)167 GPB_INLINE BOOL GPBFieldStoresObject(GPBFieldDescriptor *field) {
168 GPBMessageFieldDescription *desc = field->description_;
169 if ((desc->flags & (GPBFieldRepeated | GPBFieldMapKeyMask)) != 0) {
170 return YES;
171 }
172 return GPBDataTypeIsObject(desc->dataType);
173 }
174
175 BOOL GPBGetHasIvar(GPBMessage *self, int32_t index, uint32_t fieldNumber);
176 void GPBSetHasIvar(GPBMessage *self, int32_t idx, uint32_t fieldNumber,
177 BOOL value);
178 uint32_t GPBGetHasOneof(GPBMessage *self, int32_t index);
179
180 GPB_INLINE BOOL
GPBGetHasIvarField(GPBMessage * self,GPBFieldDescriptor * field)181 GPBGetHasIvarField(GPBMessage *self, GPBFieldDescriptor *field) {
182 GPBMessageFieldDescription *fieldDesc = field->description_;
183 return GPBGetHasIvar(self, fieldDesc->hasIndex, fieldDesc->number);
184 }
GPBSetHasIvarField(GPBMessage * self,GPBFieldDescriptor * field,BOOL value)185 GPB_INLINE void GPBSetHasIvarField(GPBMessage *self, GPBFieldDescriptor *field,
186 BOOL value) {
187 GPBMessageFieldDescription *fieldDesc = field->description_;
188 GPBSetHasIvar(self, fieldDesc->hasIndex, fieldDesc->number, value);
189 }
190
191 void GPBMaybeClearOneof(GPBMessage *self, GPBOneofDescriptor *oneof,
192 int32_t oneofHasIndex, uint32_t fieldNumberNotToClear);
193
194 #pragma clang diagnostic pop
195
196 //%PDDM-DEFINE GPB_IVAR_SET_DECL(NAME, TYPE)
197 //%void GPBSet##NAME##IvarWithFieldInternal(GPBMessage *self,
198 //% NAME$S GPBFieldDescriptor *field,
199 //% NAME$S TYPE value,
200 //% NAME$S GPBFileSyntax syntax);
201 //%PDDM-EXPAND GPB_IVAR_SET_DECL(Bool, BOOL)
202 // This block of code is generated, do not edit it directly.
203
204 void GPBSetBoolIvarWithFieldInternal(GPBMessage *self,
205 GPBFieldDescriptor *field,
206 BOOL value,
207 GPBFileSyntax syntax);
208 //%PDDM-EXPAND GPB_IVAR_SET_DECL(Int32, int32_t)
209 // This block of code is generated, do not edit it directly.
210
211 void GPBSetInt32IvarWithFieldInternal(GPBMessage *self,
212 GPBFieldDescriptor *field,
213 int32_t value,
214 GPBFileSyntax syntax);
215 //%PDDM-EXPAND GPB_IVAR_SET_DECL(UInt32, uint32_t)
216 // This block of code is generated, do not edit it directly.
217
218 void GPBSetUInt32IvarWithFieldInternal(GPBMessage *self,
219 GPBFieldDescriptor *field,
220 uint32_t value,
221 GPBFileSyntax syntax);
222 //%PDDM-EXPAND GPB_IVAR_SET_DECL(Int64, int64_t)
223 // This block of code is generated, do not edit it directly.
224
225 void GPBSetInt64IvarWithFieldInternal(GPBMessage *self,
226 GPBFieldDescriptor *field,
227 int64_t value,
228 GPBFileSyntax syntax);
229 //%PDDM-EXPAND GPB_IVAR_SET_DECL(UInt64, uint64_t)
230 // This block of code is generated, do not edit it directly.
231
232 void GPBSetUInt64IvarWithFieldInternal(GPBMessage *self,
233 GPBFieldDescriptor *field,
234 uint64_t value,
235 GPBFileSyntax syntax);
236 //%PDDM-EXPAND GPB_IVAR_SET_DECL(Float, float)
237 // This block of code is generated, do not edit it directly.
238
239 void GPBSetFloatIvarWithFieldInternal(GPBMessage *self,
240 GPBFieldDescriptor *field,
241 float value,
242 GPBFileSyntax syntax);
243 //%PDDM-EXPAND GPB_IVAR_SET_DECL(Double, double)
244 // This block of code is generated, do not edit it directly.
245
246 void GPBSetDoubleIvarWithFieldInternal(GPBMessage *self,
247 GPBFieldDescriptor *field,
248 double value,
249 GPBFileSyntax syntax);
250 //%PDDM-EXPAND GPB_IVAR_SET_DECL(Enum, int32_t)
251 // This block of code is generated, do not edit it directly.
252
253 void GPBSetEnumIvarWithFieldInternal(GPBMessage *self,
254 GPBFieldDescriptor *field,
255 int32_t value,
256 GPBFileSyntax syntax);
257 //%PDDM-EXPAND-END (8 expansions)
258
259 int32_t GPBGetEnumIvarWithFieldInternal(GPBMessage *self,
260 GPBFieldDescriptor *field,
261 GPBFileSyntax syntax);
262
263 id GPBGetObjectIvarWithField(GPBMessage *self, GPBFieldDescriptor *field);
264
265 void GPBSetObjectIvarWithFieldInternal(GPBMessage *self,
266 GPBFieldDescriptor *field, id value,
267 GPBFileSyntax syntax);
268 void GPBSetRetainedObjectIvarWithFieldInternal(GPBMessage *self,
269 GPBFieldDescriptor *field,
270 id __attribute__((ns_consumed))
271 value,
272 GPBFileSyntax syntax);
273
274 // GPBGetObjectIvarWithField will automatically create the field (message) if
275 // it doesn't exist. GPBGetObjectIvarWithFieldNoAutocreate will return nil.
276 id GPBGetObjectIvarWithFieldNoAutocreate(GPBMessage *self,
277 GPBFieldDescriptor *field);
278
279 void GPBSetAutocreatedRetainedObjectIvarWithField(
280 GPBMessage *self, GPBFieldDescriptor *field,
281 id __attribute__((ns_consumed)) value);
282
283 // Clears and releases the autocreated message ivar, if it's autocreated. If
284 // it's not set as autocreated, this method does nothing.
285 void GPBClearAutocreatedMessageIvarWithField(GPBMessage *self,
286 GPBFieldDescriptor *field);
287
288 // Returns an Objective C encoding for |selector|. |instanceSel| should be
289 // YES if it's an instance selector (as opposed to a class selector).
290 // |selector| must be a selector from MessageSignatureProtocol.
291 const char *GPBMessageEncodingForSelector(SEL selector, BOOL instanceSel);
292
293 // Helper for text format name encoding.
294 // decodeData is the data describing the sepecial decodes.
295 // key and inputString are the input that needs decoding.
296 NSString *GPBDecodeTextFormatName(const uint8_t *decodeData, int32_t key,
297 NSString *inputString);
298
299 // A series of selectors that are used solely to get @encoding values
300 // for them by the dynamic protobuf runtime code. See
301 // GPBMessageEncodingForSelector for details.
302 @protocol GPBMessageSignatureProtocol
303 @optional
304
305 #define GPB_MESSAGE_SIGNATURE_ENTRY(TYPE, NAME) \
306 -(TYPE)get##NAME; \
307 -(void)set##NAME : (TYPE)value; \
308 -(TYPE)get##NAME##AtIndex : (NSUInteger)index;
309
310 GPB_MESSAGE_SIGNATURE_ENTRY(BOOL, Bool)
311 GPB_MESSAGE_SIGNATURE_ENTRY(uint32_t, Fixed32)
312 GPB_MESSAGE_SIGNATURE_ENTRY(int32_t, SFixed32)
313 GPB_MESSAGE_SIGNATURE_ENTRY(float, Float)
314 GPB_MESSAGE_SIGNATURE_ENTRY(uint64_t, Fixed64)
315 GPB_MESSAGE_SIGNATURE_ENTRY(int64_t, SFixed64)
316 GPB_MESSAGE_SIGNATURE_ENTRY(double, Double)
317 GPB_MESSAGE_SIGNATURE_ENTRY(int32_t, Int32)
318 GPB_MESSAGE_SIGNATURE_ENTRY(int64_t, Int64)
319 GPB_MESSAGE_SIGNATURE_ENTRY(int32_t, SInt32)
320 GPB_MESSAGE_SIGNATURE_ENTRY(int64_t, SInt64)
321 GPB_MESSAGE_SIGNATURE_ENTRY(uint32_t, UInt32)
322 GPB_MESSAGE_SIGNATURE_ENTRY(uint64_t, UInt64)
323 GPB_MESSAGE_SIGNATURE_ENTRY(NSData *, Bytes)
324 GPB_MESSAGE_SIGNATURE_ENTRY(NSString *, String)
325 GPB_MESSAGE_SIGNATURE_ENTRY(GPBMessage *, Message)
326 GPB_MESSAGE_SIGNATURE_ENTRY(GPBMessage *, Group)
327 GPB_MESSAGE_SIGNATURE_ENTRY(int32_t, Enum)
328
329 #undef GPB_MESSAGE_SIGNATURE_ENTRY
330
331 - (id)getArray;
332 - (NSUInteger)getArrayCount;
333 - (void)setArray:(NSArray *)array;
334 + (id)getClassValue;
335 @end
336
337 CF_EXTERN_C_END
338