1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file or at
6 // https://developers.google.com/open-source/licenses/bsd
7
8 #import <Foundation/Foundation.h>
9
10 #import "GPBUtilities.h"
11
12 #import "GPBDescriptor.h"
13 #import "GPBDescriptor_PackagePrivate.h"
14
15 // Macros for stringifying library symbols. These are used in the generated
16 // GPB descriptor classes wherever a library symbol name is represented as a
17 // string.
18 #define GPBStringify(S) #S
19 #define GPBStringifySymbol(S) GPBStringify(S)
20
21 #define GPBNSStringify(S) @ #S
22 #define GPBNSStringifySymbol(S) GPBNSStringify(S)
23
24 // Macros for generating a Class from a class name. These are used in
25 // the generated GPB descriptor classes wherever an Objective C class
26 // reference is needed for a generated class.
27 #define GPBObjCClassSymbol(name) OBJC_CLASS_$_##name
28 #define GPBObjCClass(name) ((__bridge Class) & (GPBObjCClassSymbol(name)))
29 #define GPBObjCClassDeclaration(name) extern const GPBObjcClass_t GPBObjCClassSymbol(name)
30
31 // Constant to internally mark when there is no has bit.
32 #define GPBNoHasBit INT32_MAX
33
34 CF_EXTERN_C_BEGIN
35
36 // These two are used to inject a runtime check for version mismatch into the
37 // generated sources to make sure they are linked with a supporting runtime.
38 void GPBCheckRuntimeVersionSupport(int32_t objcRuntimeVersion);
GPB_DEBUG_CHECK_RUNTIME_VERSIONS(void)39 GPB_INLINE void GPB_DEBUG_CHECK_RUNTIME_VERSIONS(void) {
40 // NOTE: By being inline here, this captures the value from the library's
41 // headers at the time the generated code was compiled.
42 #if defined(DEBUG) && DEBUG
43 GPBCheckRuntimeVersionSupport(GOOGLE_PROTOBUF_OBJC_VERSION);
44 #endif
45 }
46
47 // Helper called within the library when the runtime detects something that
48 // indicates a older runtime is being used with newer generated code. Normally
49 // GPB_DEBUG_CHECK_RUNTIME_VERSIONS() gates this with a better message; this
50 // is just a final safety net to prevent otherwise hard to diagnose errors.
51 void GPBRuntimeMatchFailure(void);
52
53 // Legacy version of the checks, remove when GOOGLE_PROTOBUF_OBJC_GEN_VERSION
54 // goes away (see more info in GPBBootstrap.h).
55 void GPBCheckRuntimeVersionInternal(int32_t version)
56 __attribute__((deprecated("Please use a newer version of protoc to regenerate your sources. "
57 "Support for this version will go away in the future.")));
58 __attribute__((deprecated("Please use a newer version of protoc to regenerate your sources. "
59 "Support for this version will go away in the future."))) GPB_INLINE void
GPBDebugCheckRuntimeVersion(void)60 GPBDebugCheckRuntimeVersion(void) {
61 #if defined(DEBUG) && DEBUG
62 GPBCheckRuntimeVersionInternal(GOOGLE_PROTOBUF_OBJC_GEN_VERSION);
63 #endif
64 }
65
66 // Conversion functions for de/serializing floating point types.
67
GPBConvertDoubleToInt64(double v)68 GPB_INLINE int64_t GPBConvertDoubleToInt64(double v) {
69 GPBInternalCompileAssert(sizeof(double) == sizeof(int64_t), double_not_64_bits);
70 int64_t result;
71 memcpy(&result, &v, sizeof(result));
72 return result;
73 }
74
GPBConvertFloatToInt32(float v)75 GPB_INLINE int32_t GPBConvertFloatToInt32(float v) {
76 GPBInternalCompileAssert(sizeof(float) == sizeof(int32_t), float_not_32_bits);
77 int32_t result;
78 memcpy(&result, &v, sizeof(result));
79 return result;
80 }
81
GPBConvertInt64ToDouble(int64_t v)82 GPB_INLINE double GPBConvertInt64ToDouble(int64_t v) {
83 GPBInternalCompileAssert(sizeof(double) == sizeof(int64_t), double_not_64_bits);
84 double result;
85 memcpy(&result, &v, sizeof(result));
86 return result;
87 }
88
GPBConvertInt32ToFloat(int32_t v)89 GPB_INLINE float GPBConvertInt32ToFloat(int32_t v) {
90 GPBInternalCompileAssert(sizeof(float) == sizeof(int32_t), float_not_32_bits);
91 float result;
92 memcpy(&result, &v, sizeof(result));
93 return result;
94 }
95
GPBLogicalRightShift32(int32_t value,int32_t spaces)96 GPB_INLINE int32_t GPBLogicalRightShift32(int32_t value, int32_t spaces) {
97 return (int32_t)((uint32_t)(value) >> spaces);
98 }
99
GPBLogicalRightShift64(int64_t value,int32_t spaces)100 GPB_INLINE int64_t GPBLogicalRightShift64(int64_t value, int32_t spaces) {
101 return (int64_t)((uint64_t)(value) >> spaces);
102 }
103
104 // Decode a ZigZag-encoded 32-bit value. ZigZag encodes signed integers
105 // into values that can be efficiently encoded with varint. (Otherwise,
106 // negative values must be sign-extended to 64 bits to be varint encoded,
107 // thus always taking 10 bytes on the wire.)
GPBDecodeZigZag32(uint32_t n)108 GPB_INLINE int32_t GPBDecodeZigZag32(uint32_t n) {
109 return (int32_t)(GPBLogicalRightShift32((int32_t)n, 1) ^ -((int32_t)(n) & 1));
110 }
111
112 // Decode a ZigZag-encoded 64-bit value. ZigZag encodes signed integers
113 // into values that can be efficiently encoded with varint. (Otherwise,
114 // negative values must be sign-extended to 64 bits to be varint encoded,
115 // thus always taking 10 bytes on the wire.)
GPBDecodeZigZag64(uint64_t n)116 GPB_INLINE int64_t GPBDecodeZigZag64(uint64_t n) {
117 return (int64_t)(GPBLogicalRightShift64((int64_t)n, 1) ^ -((int64_t)(n) & 1));
118 }
119
120 // Encode a ZigZag-encoded 32-bit value. ZigZag encodes signed integers
121 // into values that can be efficiently encoded with varint. (Otherwise,
122 // negative values must be sign-extended to 64 bits to be varint encoded,
123 // thus always taking 10 bytes on the wire.)
GPBEncodeZigZag32(int32_t n)124 GPB_INLINE uint32_t GPBEncodeZigZag32(int32_t n) {
125 // Note: the right-shift must be arithmetic
126 return ((uint32_t)n << 1) ^ (uint32_t)(n >> 31);
127 }
128
129 // Encode a ZigZag-encoded 64-bit value. ZigZag encodes signed integers
130 // into values that can be efficiently encoded with varint. (Otherwise,
131 // negative values must be sign-extended to 64 bits to be varint encoded,
132 // thus always taking 10 bytes on the wire.)
GPBEncodeZigZag64(int64_t n)133 GPB_INLINE uint64_t GPBEncodeZigZag64(int64_t n) {
134 // Note: the right-shift must be arithmetic
135 return ((uint64_t)n << 1) ^ (uint64_t)(n >> 63);
136 }
137
138 #pragma clang diagnostic push
139 #pragma clang diagnostic ignored "-Wswitch-enum"
140 #pragma clang diagnostic ignored "-Wdirect-ivar-access"
141
GPBDataTypeIsObject(GPBDataType type)142 GPB_INLINE BOOL GPBDataTypeIsObject(GPBDataType type) {
143 switch (type) {
144 case GPBDataTypeBytes:
145 case GPBDataTypeString:
146 case GPBDataTypeMessage:
147 case GPBDataTypeGroup:
148 return YES;
149 default:
150 return NO;
151 }
152 }
153
GPBDataTypeIsMessage(GPBDataType type)154 GPB_INLINE BOOL GPBDataTypeIsMessage(GPBDataType type) {
155 switch (type) {
156 case GPBDataTypeMessage:
157 case GPBDataTypeGroup:
158 return YES;
159 default:
160 return NO;
161 }
162 }
163
GPBFieldDataTypeIsMessage(GPBFieldDescriptor * field)164 GPB_INLINE BOOL GPBFieldDataTypeIsMessage(GPBFieldDescriptor *field) {
165 return GPBDataTypeIsMessage(field->description_->dataType);
166 }
167
GPBFieldDataTypeIsObject(GPBFieldDescriptor * field)168 GPB_INLINE BOOL GPBFieldDataTypeIsObject(GPBFieldDescriptor *field) {
169 return GPBDataTypeIsObject(field->description_->dataType);
170 }
171
GPBExtensionIsMessage(GPBExtensionDescriptor * ext)172 GPB_INLINE BOOL GPBExtensionIsMessage(GPBExtensionDescriptor *ext) {
173 return GPBDataTypeIsMessage(ext->description_->dataType);
174 }
175
176 // The field is an array/map or it has an object value.
GPBFieldStoresObject(GPBFieldDescriptor * field)177 GPB_INLINE BOOL GPBFieldStoresObject(GPBFieldDescriptor *field) {
178 GPBMessageFieldDescription *desc = field->description_;
179 if ((desc->flags & (GPBFieldRepeated | GPBFieldMapKeyMask)) != 0) {
180 return YES;
181 }
182 return GPBDataTypeIsObject(desc->dataType);
183 }
184
185 BOOL GPBGetHasIvar(GPBMessage *self, int32_t index, uint32_t fieldNumber);
186 void GPBSetHasIvar(GPBMessage *self, int32_t idx, uint32_t fieldNumber, BOOL value);
187 uint32_t GPBGetHasOneof(GPBMessage *self, int32_t index);
188
GPBGetHasIvarField(GPBMessage * self,GPBFieldDescriptor * field)189 GPB_INLINE BOOL GPBGetHasIvarField(GPBMessage *self, GPBFieldDescriptor *field) {
190 GPBMessageFieldDescription *fieldDesc = field->description_;
191 return GPBGetHasIvar(self, fieldDesc->hasIndex, fieldDesc->number);
192 }
193
194 #pragma clang diagnostic pop
195
196 // Disable clang-format for the macros.
197 // clang-format off
198
199 //%PDDM-DEFINE GPB_IVAR_SET_DECL(NAME, TYPE)
200 //%void GPBSet##NAME##IvarWithFieldPrivate(GPBMessage *self,
201 //% NAME$S GPBFieldDescriptor *field,
202 //% NAME$S TYPE value);
203 //%PDDM-EXPAND GPB_IVAR_SET_DECL(Bool, BOOL)
204 // This block of code is generated, do not edit it directly.
205
206 void GPBSetBoolIvarWithFieldPrivate(GPBMessage *self,
207 GPBFieldDescriptor *field,
208 BOOL value);
209 //%PDDM-EXPAND GPB_IVAR_SET_DECL(Int32, int32_t)
210 // This block of code is generated, do not edit it directly.
211
212 void GPBSetInt32IvarWithFieldPrivate(GPBMessage *self,
213 GPBFieldDescriptor *field,
214 int32_t value);
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 GPBSetUInt32IvarWithFieldPrivate(GPBMessage *self,
219 GPBFieldDescriptor *field,
220 uint32_t value);
221 //%PDDM-EXPAND GPB_IVAR_SET_DECL(Int64, int64_t)
222 // This block of code is generated, do not edit it directly.
223
224 void GPBSetInt64IvarWithFieldPrivate(GPBMessage *self,
225 GPBFieldDescriptor *field,
226 int64_t value);
227 //%PDDM-EXPAND GPB_IVAR_SET_DECL(UInt64, uint64_t)
228 // This block of code is generated, do not edit it directly.
229
230 void GPBSetUInt64IvarWithFieldPrivate(GPBMessage *self,
231 GPBFieldDescriptor *field,
232 uint64_t value);
233 //%PDDM-EXPAND GPB_IVAR_SET_DECL(Float, float)
234 // This block of code is generated, do not edit it directly.
235
236 void GPBSetFloatIvarWithFieldPrivate(GPBMessage *self,
237 GPBFieldDescriptor *field,
238 float value);
239 //%PDDM-EXPAND GPB_IVAR_SET_DECL(Double, double)
240 // This block of code is generated, do not edit it directly.
241
242 void GPBSetDoubleIvarWithFieldPrivate(GPBMessage *self,
243 GPBFieldDescriptor *field,
244 double value);
245 //%PDDM-EXPAND-END (7 expansions)
246
247 // clang-format on
248
249 void GPBSetEnumIvarWithFieldPrivate(GPBMessage *self, GPBFieldDescriptor *field, int32_t value);
250
251 id GPBGetObjectIvarWithField(GPBMessage *self, GPBFieldDescriptor *field);
252
253 void GPBSetObjectIvarWithFieldPrivate(GPBMessage *self, GPBFieldDescriptor *field, id value);
254 void GPBSetRetainedObjectIvarWithFieldPrivate(GPBMessage *self, GPBFieldDescriptor *field,
255 id __attribute__((ns_consumed)) value);
256
257 // GPBGetObjectIvarWithField will automatically create the field (message) if
258 // it doesn't exist. GPBGetObjectIvarWithFieldNoAutocreate will return nil.
259 id GPBGetObjectIvarWithFieldNoAutocreate(GPBMessage *self, GPBFieldDescriptor *field);
260
261 // Clears and releases the autocreated message ivar, if it's autocreated. If
262 // it's not set as autocreated, this method does nothing.
263 void GPBClearAutocreatedMessageIvarWithField(GPBMessage *self, GPBFieldDescriptor *field);
264
265 // Returns an Objective C encoding for |selector|. |instanceSel| should be
266 // YES if it's an instance selector (as opposed to a class selector).
267 // |selector| must be a selector from MessageSignatureProtocol.
268 const char *GPBMessageEncodingForSelector(SEL selector, BOOL instanceSel);
269
270 // Helper for text format name encoding.
271 // decodeData is the data describing the special decodes.
272 // key and inputString are the input that needs decoding.
273 NSString *GPBDecodeTextFormatName(const uint8_t *decodeData, int32_t key, NSString *inputString);
274
275 // Shims from the older generated code into the runtime.
276 void GPBSetInt32IvarWithFieldInternal(GPBMessage *self, GPBFieldDescriptor *field, int32_t value,
277 GPBFileSyntax syntax);
278 void GPBMaybeClearOneof(GPBMessage *self, GPBOneofDescriptor *oneof, int32_t oneofHasIndex,
279 uint32_t fieldNumberNotToClear);
280
281 // A series of selectors that are used solely to get @encoding values
282 // for them by the dynamic protobuf runtime code. See
283 // GPBMessageEncodingForSelector for details. GPBRootObject conforms to
284 // the protocol so that it is encoded in the Objective C runtime.
285 @protocol GPBMessageSignatureProtocol
286 @optional
287
288 #define GPB_MESSAGE_SIGNATURE_ENTRY(TYPE, NAME) \
289 -(TYPE)get##NAME; \
290 -(void)set##NAME : (TYPE)value; \
291 -(TYPE)get##NAME##AtIndex : (NSUInteger)index;
292
293 GPB_MESSAGE_SIGNATURE_ENTRY(BOOL, Bool)
294 GPB_MESSAGE_SIGNATURE_ENTRY(uint32_t, Fixed32)
295 GPB_MESSAGE_SIGNATURE_ENTRY(int32_t, SFixed32)
296 GPB_MESSAGE_SIGNATURE_ENTRY(float, Float)
297 GPB_MESSAGE_SIGNATURE_ENTRY(uint64_t, Fixed64)
298 GPB_MESSAGE_SIGNATURE_ENTRY(int64_t, SFixed64)
299 GPB_MESSAGE_SIGNATURE_ENTRY(double, Double)
300 GPB_MESSAGE_SIGNATURE_ENTRY(int32_t, Int32)
301 GPB_MESSAGE_SIGNATURE_ENTRY(int64_t, Int64)
302 GPB_MESSAGE_SIGNATURE_ENTRY(int32_t, SInt32)
303 GPB_MESSAGE_SIGNATURE_ENTRY(int64_t, SInt64)
304 GPB_MESSAGE_SIGNATURE_ENTRY(uint32_t, UInt32)
305 GPB_MESSAGE_SIGNATURE_ENTRY(uint64_t, UInt64)
306 GPB_MESSAGE_SIGNATURE_ENTRY(NSData *, Bytes)
307 GPB_MESSAGE_SIGNATURE_ENTRY(NSString *, String)
308 GPB_MESSAGE_SIGNATURE_ENTRY(GPBMessage *, Message)
309 GPB_MESSAGE_SIGNATURE_ENTRY(GPBMessage *, Group)
310 GPB_MESSAGE_SIGNATURE_ENTRY(int32_t, Enum)
311
312 #undef GPB_MESSAGE_SIGNATURE_ENTRY
313
314 - (id)getArray;
315 - (NSUInteger)getArrayCount;
316 - (void)setArray:(NSArray *)array;
317 + (id)getClassValue;
318 @end
319
320 BOOL GPBClassHasSel(Class aClass, SEL sel);
321
322 CF_EXTERN_C_END
323