1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
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
8 #import <Foundation/Foundation.h>
10 #import "GPBUtilities.h"
12 #import "GPBDescriptor.h"
13 #import "GPBDescriptor_PackagePrivate.h"
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
18 #define GPBStringify(S) #S
19 #define GPBStringifySymbol(S) GPBStringify(S)
21 #define GPBNSStringify(S) @ #S
22 #define GPBNSStringifySymbol(S) GPBNSStringify(S)
24 // A type to represent an Objective C class.
25 // This is actually an `objc_class` but the runtime headers will not allow us to
26 // reference `objc_class`, so we have defined our own.
27 typedef struct GPBObjcClass_t GPBObjcClass_t
;
29 // Macros for generating a Class from a class name. These are used in
30 // the generated GPB descriptor classes wherever an Objective C class
31 // reference is needed for a generated class.
32 #define GPBObjCClassSymbol(name) OBJC_CLASS_$_##name
33 #define GPBObjCClass(name) ((__bridge Class) & (GPBObjCClassSymbol(name)))
34 #define GPBObjCClassDeclaration(name) extern const GPBObjcClass_t GPBObjCClassSymbol(name)
36 // Constant to internally mark when there is no has bit.
37 #define GPBNoHasBit INT32_MAX
41 // These two are used to inject a runtime check for version mismatch into the
42 // generated sources to make sure they are linked with a supporting runtime.
43 void GPBCheckRuntimeVersionSupport(int32_t objcRuntimeVersion
);
44 GPB_INLINE
void GPB_DEBUG_CHECK_RUNTIME_VERSIONS(void) {
45 // NOTE: By being inline here, this captures the value from the library's
46 // headers at the time the generated code was compiled.
47 #if defined(DEBUG) && DEBUG
48 GPBCheckRuntimeVersionSupport(GOOGLE_PROTOBUF_OBJC_VERSION
);
52 // Helper called within the library when the runtime detects something that
53 // indicates a older runtime is being used with newer generated code. Normally
54 // GPB_DEBUG_CHECK_RUNTIME_VERSIONS() gates this with a better message; this
55 // is just a final safety net to prevent otherwise hard to diagnose errors.
56 void GPBRuntimeMatchFailure(void);
58 // Conversion functions for de/serializing floating point types.
60 GPB_INLINE
int64_t GPBConvertDoubleToInt64(double v
) {
61 GPBInternalCompileAssert(sizeof(double) == sizeof(int64_t), double_not_64_bits
);
63 memcpy(&result
, &v
, sizeof(result
));
67 GPB_INLINE
int32_t GPBConvertFloatToInt32(float v
) {
68 GPBInternalCompileAssert(sizeof(float) == sizeof(int32_t), float_not_32_bits
);
70 memcpy(&result
, &v
, sizeof(result
));
74 GPB_INLINE
double GPBConvertInt64ToDouble(int64_t v
) {
75 GPBInternalCompileAssert(sizeof(double) == sizeof(int64_t), double_not_64_bits
);
77 memcpy(&result
, &v
, sizeof(result
));
81 GPB_INLINE
float GPBConvertInt32ToFloat(int32_t v
) {
82 GPBInternalCompileAssert(sizeof(float) == sizeof(int32_t), float_not_32_bits
);
84 memcpy(&result
, &v
, sizeof(result
));
88 GPB_INLINE
int32_t GPBLogicalRightShift32(int32_t value
, int32_t spaces
) {
89 return (int32_t)((uint32_t)(value
) >> spaces
);
92 GPB_INLINE
int64_t GPBLogicalRightShift64(int64_t value
, int32_t spaces
) {
93 return (int64_t)((uint64_t)(value
) >> spaces
);
96 // Decode a ZigZag-encoded 32-bit value. ZigZag encodes signed integers
97 // into values that can be efficiently encoded with varint. (Otherwise,
98 // negative values must be sign-extended to 64 bits to be varint encoded,
99 // thus always taking 10 bytes on the wire.)
100 GPB_INLINE
int32_t GPBDecodeZigZag32(uint32_t n
) {
101 return (int32_t)(GPBLogicalRightShift32((int32_t)n
, 1) ^ -((int32_t)(n
) & 1));
104 // Decode a ZigZag-encoded 64-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.)
108 GPB_INLINE
int64_t GPBDecodeZigZag64(uint64_t n
) {
109 return (int64_t)(GPBLogicalRightShift64((int64_t)n
, 1) ^ -((int64_t)(n
) & 1));
112 // Encode a ZigZag-encoded 32-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.)
116 GPB_INLINE
uint32_t GPBEncodeZigZag32(int32_t n
) {
117 // Note: the right-shift must be arithmetic
118 return ((uint32_t)n
<< 1) ^ (uint32_t)(n
>> 31);
121 // Encode a ZigZag-encoded 64-bit value. ZigZag encodes signed integers
122 // into values that can be efficiently encoded with varint. (Otherwise,
123 // negative values must be sign-extended to 64 bits to be varint encoded,
124 // thus always taking 10 bytes on the wire.)
125 GPB_INLINE
uint64_t GPBEncodeZigZag64(int64_t n
) {
126 // Note: the right-shift must be arithmetic
127 return ((uint64_t)n
<< 1) ^ (uint64_t)(n
>> 63);
130 #pragma clang diagnostic push
131 #pragma clang diagnostic ignored "-Wswitch-enum"
132 #pragma clang diagnostic ignored "-Wdirect-ivar-access"
134 GPB_INLINE BOOL
GPBDataTypeIsObject(GPBDataType type
) {
136 case GPBDataTypeBytes
:
137 case GPBDataTypeString
:
138 case GPBDataTypeMessage
:
139 case GPBDataTypeGroup
:
146 GPB_INLINE BOOL
GPBDataTypeIsMessage(GPBDataType type
) {
148 case GPBDataTypeMessage
:
149 case GPBDataTypeGroup
:
156 GPB_INLINE BOOL
GPBFieldDataTypeIsMessage(GPBFieldDescriptor
*field
) {
157 return GPBDataTypeIsMessage(field
->description_
->dataType
);
160 GPB_INLINE BOOL
GPBFieldDataTypeIsObject(GPBFieldDescriptor
*field
) {
161 return GPBDataTypeIsObject(field
->description_
->dataType
);
164 GPB_INLINE BOOL
GPBExtensionIsMessage(GPBExtensionDescriptor
*ext
) {
165 return GPBDataTypeIsMessage(ext
->description_
->dataType
);
168 // The field is an array/map or it has an object value.
169 GPB_INLINE BOOL
GPBFieldStoresObject(GPBFieldDescriptor
*field
) {
170 GPBMessageFieldDescription
*desc
= field
->description_
;
171 if ((desc
->flags
& (GPBFieldRepeated
| GPBFieldMapKeyMask
)) != 0) {
174 return GPBDataTypeIsObject(desc
->dataType
);
177 BOOL
GPBGetHasIvar(GPBMessage
*self
, int32_t idx
, uint32_t fieldNumber
);
178 void GPBSetHasIvar(GPBMessage
*self
, int32_t idx
, uint32_t fieldNumber
, BOOL value
);
179 uint32_t GPBGetHasOneof(GPBMessage
*self
, int32_t idx
);
181 GPB_INLINE BOOL
GPBGetHasIvarField(GPBMessage
*self
, GPBFieldDescriptor
*field
) {
182 GPBMessageFieldDescription
*fieldDesc
= field
->description_
;
183 return GPBGetHasIvar(self
, fieldDesc
->hasIndex
, fieldDesc
->number
);
186 #pragma clang diagnostic pop
188 // Disable clang-format for the macros.
191 //%PDDM-DEFINE GPB_IVAR_SET_DECL(NAME, TYPE)
192 //%void GPBSet##NAME##IvarWithFieldPrivate(GPBMessage *self,
193 //% NAME$S GPBFieldDescriptor *field,
194 //% NAME$S TYPE value);
195 //%PDDM-EXPAND GPB_IVAR_SET_DECL(Bool, BOOL)
196 // This block of code is generated, do not edit it directly.
198 void GPBSetBoolIvarWithFieldPrivate(GPBMessage
*self
,
199 GPBFieldDescriptor
*field
,
201 //%PDDM-EXPAND GPB_IVAR_SET_DECL(Int32, int32_t)
202 // This block of code is generated, do not edit it directly.
204 void GPBSetInt32IvarWithFieldPrivate(GPBMessage
*self
,
205 GPBFieldDescriptor
*field
,
207 //%PDDM-EXPAND GPB_IVAR_SET_DECL(UInt32, uint32_t)
208 // This block of code is generated, do not edit it directly.
210 void GPBSetUInt32IvarWithFieldPrivate(GPBMessage
*self
,
211 GPBFieldDescriptor
*field
,
213 //%PDDM-EXPAND GPB_IVAR_SET_DECL(Int64, int64_t)
214 // This block of code is generated, do not edit it directly.
216 void GPBSetInt64IvarWithFieldPrivate(GPBMessage
*self
,
217 GPBFieldDescriptor
*field
,
219 //%PDDM-EXPAND GPB_IVAR_SET_DECL(UInt64, uint64_t)
220 // This block of code is generated, do not edit it directly.
222 void GPBSetUInt64IvarWithFieldPrivate(GPBMessage
*self
,
223 GPBFieldDescriptor
*field
,
225 //%PDDM-EXPAND GPB_IVAR_SET_DECL(Float, float)
226 // This block of code is generated, do not edit it directly.
228 void GPBSetFloatIvarWithFieldPrivate(GPBMessage
*self
,
229 GPBFieldDescriptor
*field
,
231 //%PDDM-EXPAND GPB_IVAR_SET_DECL(Double, double)
232 // This block of code is generated, do not edit it directly.
234 void GPBSetDoubleIvarWithFieldPrivate(GPBMessage
*self
,
235 GPBFieldDescriptor
*field
,
237 //%PDDM-EXPAND-END (7 expansions)
241 void GPBSetEnumIvarWithFieldPrivate(GPBMessage
*self
, GPBFieldDescriptor
*field
, int32_t value
);
243 id
GPBGetObjectIvarWithField(GPBMessage
*self
, GPBFieldDescriptor
*field
);
245 void GPBSetObjectIvarWithFieldPrivate(GPBMessage
*self
, GPBFieldDescriptor
*field
, id value
);
246 void GPBSetRetainedObjectIvarWithFieldPrivate(GPBMessage
*self
, GPBFieldDescriptor
*field
,
247 id
__attribute__((ns_consumed
)) value
);
249 // GPBGetObjectIvarWithField will automatically create the field (message) if
250 // it doesn't exist. GPBGetObjectIvarWithFieldNoAutocreate will return nil.
251 id
GPBGetObjectIvarWithFieldNoAutocreate(GPBMessage
*self
, GPBFieldDescriptor
*field
);
253 // Clears and releases the autocreated message ivar, if it's autocreated. If
254 // it's not set as autocreated, this method does nothing.
255 void GPBClearAutocreatedMessageIvarWithField(GPBMessage
*self
, GPBFieldDescriptor
*field
);
257 // Returns an Objective C encoding for |selector|. |instanceSel| should be
258 // YES if it's an instance selector (as opposed to a class selector).
259 // |selector| must be a selector from MessageSignatureProtocol.
260 const char *GPBMessageEncodingForSelector(SEL selector
, BOOL instanceSel
);
262 // Helper for text format name encoding.
263 // decodeData is the data describing the special decodes.
264 // key and inputString are the input that needs decoding.
265 NSString
*GPBDecodeTextFormatName(const uint8_t *decodeData
, int32_t key
, NSString
*inputString
);
267 // Shims from the older generated code into the runtime.
268 void GPBSetInt32IvarWithFieldInternal(GPBMessage
*self
, GPBFieldDescriptor
*field
, int32_t value
,
269 GPBFileSyntax syntax
);
270 void GPBMaybeClearOneof(GPBMessage
*self
, GPBOneofDescriptor
*oneof
, int32_t oneofHasIndex
,
271 uint32_t fieldNumberNotToClear
);
273 // A series of selectors that are used solely to get @encoding values
274 // for them by the dynamic protobuf runtime code. See
275 // GPBMessageEncodingForSelector for details. GPBRootObject conforms to
276 // the protocol so that it is encoded in the Objective C runtime.
277 @protocol GPBMessageSignatureProtocol
280 #define GPB_MESSAGE_SIGNATURE_ENTRY(TYPE, NAME) \
282 -(void)set##NAME : (TYPE)value; \
283 -(TYPE)get##NAME##AtIndex : (NSUInteger)index;
285 GPB_MESSAGE_SIGNATURE_ENTRY(BOOL
, Bool
)
286 GPB_MESSAGE_SIGNATURE_ENTRY(uint32_t, Fixed32
)
287 GPB_MESSAGE_SIGNATURE_ENTRY(int32_t, SFixed32
)
288 GPB_MESSAGE_SIGNATURE_ENTRY(float, Float
)
289 GPB_MESSAGE_SIGNATURE_ENTRY(uint64_t, Fixed64
)
290 GPB_MESSAGE_SIGNATURE_ENTRY(int64_t, SFixed64
)
291 GPB_MESSAGE_SIGNATURE_ENTRY(double, Double
)
292 GPB_MESSAGE_SIGNATURE_ENTRY(int32_t, Int32
)
293 GPB_MESSAGE_SIGNATURE_ENTRY(int64_t, Int64
)
294 GPB_MESSAGE_SIGNATURE_ENTRY(int32_t, SInt32
)
295 GPB_MESSAGE_SIGNATURE_ENTRY(int64_t, SInt64
)
296 GPB_MESSAGE_SIGNATURE_ENTRY(uint32_t, UInt32
)
297 GPB_MESSAGE_SIGNATURE_ENTRY(uint64_t, UInt64
)
298 GPB_MESSAGE_SIGNATURE_ENTRY(NSData
*, Bytes
)
299 GPB_MESSAGE_SIGNATURE_ENTRY(NSString
*, String
)
300 GPB_MESSAGE_SIGNATURE_ENTRY(GPBMessage
*, Message
)
301 GPB_MESSAGE_SIGNATURE_ENTRY(GPBMessage
*, Group
)
302 GPB_MESSAGE_SIGNATURE_ENTRY(int32_t, Enum
)
304 #undef GPB_MESSAGE_SIGNATURE_ENTRY
307 - (NSUInteger
)getArrayCount
;
308 - (void)setArray
:(NSArray
*)array
;
312 BOOL
GPBClassHasSel(Class aClass
, SEL sel
);