blob: f04ed8316b227126e0afd8de26302d8701a847f0 [file] [log] [blame]
Thomas Van Lenten30650d82015-05-01 08:57:16 -04001// 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
Dave MacLachlanaa1d7e72020-01-19 17:46:35 -080038// GPB descriptor classes wherever a library symbol name is represented as a
39// string.
Thomas Van Lenten30650d82015-05-01 08:57:16 -040040#define GPBStringify(S) #S
41#define GPBStringifySymbol(S) GPBStringify(S)
42
Thomas Van Lenten5d0b2172022-09-19 17:20:35 -040043#define GPBNSStringify(S) @ #S
Thomas Van Lenten30650d82015-05-01 08:57:16 -040044#define GPBNSStringifySymbol(S) GPBNSStringify(S)
45
Dave MacLachlanaa1d7e72020-01-19 17:46:35 -080046// Macros for generating a Class from a class name. These are used in
47// the generated GPB descriptor classes wherever an Objective C class
48// reference is needed for a generated class.
49#define GPBObjCClassSymbol(name) OBJC_CLASS_$_##name
Thomas Van Lenten5d0b2172022-09-19 17:20:35 -040050#define GPBObjCClass(name) ((__bridge Class) & (GPBObjCClassSymbol(name)))
51#define GPBObjCClassDeclaration(name) extern const GPBObjcClass_t GPBObjCClassSymbol(name)
Dave MacLachlanaa1d7e72020-01-19 17:46:35 -080052
Thomas Van Lenten30650d82015-05-01 08:57:16 -040053// Constant to internally mark when there is no has bit.
54#define GPBNoHasBit INT32_MAX
55
56CF_EXTERN_C_BEGIN
57
Thomas Van Lentend846b0b2015-06-08 16:24:57 -040058// These two are used to inject a runtime check for version mismatch into the
59// generated sources to make sure they are linked with a supporting runtime.
Thomas Van Lenten1aa65002016-09-15 13:27:17 -040060void GPBCheckRuntimeVersionSupport(int32_t objcRuntimeVersion);
61GPB_INLINE void GPB_DEBUG_CHECK_RUNTIME_VERSIONS() {
62 // NOTE: By being inline here, this captures the value from the library's
63 // headers at the time the generated code was compiled.
64#if defined(DEBUG) && DEBUG
65 GPBCheckRuntimeVersionSupport(GOOGLE_PROTOBUF_OBJC_VERSION);
66#endif
67}
68
69// Legacy version of the checks, remove when GOOGLE_PROTOBUF_OBJC_GEN_VERSION
70// goes away (see more info in GPBBootstrap.h).
Thomas Van Lentend846b0b2015-06-08 16:24:57 -040071void GPBCheckRuntimeVersionInternal(int32_t version);
72GPB_INLINE void GPBDebugCheckRuntimeVersion() {
Thomas Van Lentenc8a440d2016-05-25 13:46:00 -040073#if defined(DEBUG) && DEBUG
Thomas Van Lentend846b0b2015-06-08 16:24:57 -040074 GPBCheckRuntimeVersionInternal(GOOGLE_PROTOBUF_OBJC_GEN_VERSION);
75#endif
76}
77
Thomas Van Lenten30650d82015-05-01 08:57:16 -040078// Conversion functions for de/serializing floating point types.
79
80GPB_INLINE int64_t GPBConvertDoubleToInt64(double v) {
Thomas Van Lenten58464392019-09-20 10:52:34 -040081 GPBInternalCompileAssert(sizeof(double) == sizeof(int64_t), double_not_64_bits);
82 int64_t result;
83 memcpy(&result, &v, sizeof(result));
84 return result;
Thomas Van Lenten30650d82015-05-01 08:57:16 -040085}
86
87GPB_INLINE int32_t GPBConvertFloatToInt32(float v) {
Thomas Van Lenten58464392019-09-20 10:52:34 -040088 GPBInternalCompileAssert(sizeof(float) == sizeof(int32_t), float_not_32_bits);
89 int32_t result;
90 memcpy(&result, &v, sizeof(result));
91 return result;
Thomas Van Lenten30650d82015-05-01 08:57:16 -040092}
93
94GPB_INLINE double GPBConvertInt64ToDouble(int64_t v) {
Thomas Van Lenten58464392019-09-20 10:52:34 -040095 GPBInternalCompileAssert(sizeof(double) == sizeof(int64_t), double_not_64_bits);
96 double result;
97 memcpy(&result, &v, sizeof(result));
98 return result;
Thomas Van Lenten30650d82015-05-01 08:57:16 -040099}
100
101GPB_INLINE float GPBConvertInt32ToFloat(int32_t v) {
Thomas Van Lenten58464392019-09-20 10:52:34 -0400102 GPBInternalCompileAssert(sizeof(float) == sizeof(int32_t), float_not_32_bits);
103 float result;
104 memcpy(&result, &v, sizeof(result));
105 return result;
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400106}
107
108GPB_INLINE int32_t GPBLogicalRightShift32(int32_t value, int32_t spaces) {
109 return (int32_t)((uint32_t)(value) >> spaces);
110}
111
112GPB_INLINE int64_t GPBLogicalRightShift64(int64_t value, int32_t spaces) {
113 return (int64_t)((uint64_t)(value) >> spaces);
114}
115
116// Decode a ZigZag-encoded 32-bit value. ZigZag encodes signed integers
117// into values that can be efficiently encoded with varint. (Otherwise,
118// negative values must be sign-extended to 64 bits to be varint encoded,
119// thus always taking 10 bytes on the wire.)
120GPB_INLINE int32_t GPBDecodeZigZag32(uint32_t n) {
Thomas Van Lenten5d0b2172022-09-19 17:20:35 -0400121 return (int32_t)(GPBLogicalRightShift32((int32_t)n, 1) ^ -((int32_t)(n)&1));
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400122}
123
124// Decode a ZigZag-encoded 64-bit value. ZigZag encodes signed integers
125// into values that can be efficiently encoded with varint. (Otherwise,
126// negative values must be sign-extended to 64 bits to be varint encoded,
127// thus always taking 10 bytes on the wire.)
128GPB_INLINE int64_t GPBDecodeZigZag64(uint64_t n) {
Thomas Van Lenten5d0b2172022-09-19 17:20:35 -0400129 return (int64_t)(GPBLogicalRightShift64((int64_t)n, 1) ^ -((int64_t)(n)&1));
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400130}
131
132// Encode a ZigZag-encoded 32-bit value. ZigZag encodes signed integers
133// into values that can be efficiently encoded with varint. (Otherwise,
134// negative values must be sign-extended to 64 bits to be varint encoded,
135// thus always taking 10 bytes on the wire.)
136GPB_INLINE uint32_t GPBEncodeZigZag32(int32_t n) {
137 // Note: the right-shift must be arithmetic
Thomas Van Lenten953adb12018-01-31 11:59:57 -0500138 return ((uint32_t)n << 1) ^ (uint32_t)(n >> 31);
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400139}
140
141// Encode a ZigZag-encoded 64-bit value. ZigZag encodes signed integers
142// into values that can be efficiently encoded with varint. (Otherwise,
143// negative values must be sign-extended to 64 bits to be varint encoded,
144// thus always taking 10 bytes on the wire.)
145GPB_INLINE uint64_t GPBEncodeZigZag64(int64_t n) {
146 // Note: the right-shift must be arithmetic
Thomas Van Lenten953adb12018-01-31 11:59:57 -0500147 return ((uint64_t)n << 1) ^ (uint64_t)(n >> 63);
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400148}
149
Thomas Van Lentenc8a440d2016-05-25 13:46:00 -0400150#pragma clang diagnostic push
151#pragma clang diagnostic ignored "-Wswitch-enum"
152#pragma clang diagnostic ignored "-Wdirect-ivar-access"
153
Thomas Van Lentend846b0b2015-06-08 16:24:57 -0400154GPB_INLINE BOOL GPBDataTypeIsObject(GPBDataType type) {
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400155 switch (type) {
Thomas Van Lentend846b0b2015-06-08 16:24:57 -0400156 case GPBDataTypeBytes:
157 case GPBDataTypeString:
158 case GPBDataTypeMessage:
159 case GPBDataTypeGroup:
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400160 return YES;
161 default:
162 return NO;
163 }
164}
165
Thomas Van Lentend846b0b2015-06-08 16:24:57 -0400166GPB_INLINE BOOL GPBDataTypeIsMessage(GPBDataType type) {
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400167 switch (type) {
Thomas Van Lentend846b0b2015-06-08 16:24:57 -0400168 case GPBDataTypeMessage:
169 case GPBDataTypeGroup:
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400170 return YES;
171 default:
172 return NO;
173 }
174}
175
Thomas Van Lentend846b0b2015-06-08 16:24:57 -0400176GPB_INLINE BOOL GPBFieldDataTypeIsMessage(GPBFieldDescriptor *field) {
177 return GPBDataTypeIsMessage(field->description_->dataType);
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400178}
179
Thomas Van Lentend846b0b2015-06-08 16:24:57 -0400180GPB_INLINE BOOL GPBFieldDataTypeIsObject(GPBFieldDescriptor *field) {
181 return GPBDataTypeIsObject(field->description_->dataType);
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400182}
183
184GPB_INLINE BOOL GPBExtensionIsMessage(GPBExtensionDescriptor *ext) {
Thomas Van Lentend846b0b2015-06-08 16:24:57 -0400185 return GPBDataTypeIsMessage(ext->description_->dataType);
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400186}
187
188// The field is an array/map or it has an object value.
189GPB_INLINE BOOL GPBFieldStoresObject(GPBFieldDescriptor *field) {
190 GPBMessageFieldDescription *desc = field->description_;
191 if ((desc->flags & (GPBFieldRepeated | GPBFieldMapKeyMask)) != 0) {
192 return YES;
193 }
Thomas Van Lentend846b0b2015-06-08 16:24:57 -0400194 return GPBDataTypeIsObject(desc->dataType);
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400195}
196
197BOOL GPBGetHasIvar(GPBMessage *self, int32_t index, uint32_t fieldNumber);
Thomas Van Lenten5d0b2172022-09-19 17:20:35 -0400198void GPBSetHasIvar(GPBMessage *self, int32_t idx, uint32_t fieldNumber, BOOL value);
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400199uint32_t GPBGetHasOneof(GPBMessage *self, int32_t index);
200
Thomas Van Lenten5d0b2172022-09-19 17:20:35 -0400201GPB_INLINE BOOL GPBGetHasIvarField(GPBMessage *self, GPBFieldDescriptor *field) {
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400202 GPBMessageFieldDescription *fieldDesc = field->description_;
203 return GPBGetHasIvar(self, fieldDesc->hasIndex, fieldDesc->number);
204}
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400205
Thomas Van Lentenc8a440d2016-05-25 13:46:00 -0400206#pragma clang diagnostic pop
207
Thomas Van Lenten9e069b22022-09-19 13:34:01 -0400208// Disable clang-format for the macros.
209// clang-format off
210
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400211//%PDDM-DEFINE GPB_IVAR_SET_DECL(NAME, TYPE)
Thomas Van Lentene1e5b8a2020-04-10 15:55:32 -0400212//%void GPBSet##NAME##IvarWithFieldPrivate(GPBMessage *self,
213//% NAME$S GPBFieldDescriptor *field,
214//% NAME$S TYPE value);
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400215//%PDDM-EXPAND GPB_IVAR_SET_DECL(Bool, BOOL)
216// This block of code is generated, do not edit it directly.
217
Thomas Van Lentene1e5b8a2020-04-10 15:55:32 -0400218void GPBSetBoolIvarWithFieldPrivate(GPBMessage *self,
219 GPBFieldDescriptor *field,
220 BOOL value);
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400221//%PDDM-EXPAND GPB_IVAR_SET_DECL(Int32, int32_t)
222// This block of code is generated, do not edit it directly.
223
Thomas Van Lentene1e5b8a2020-04-10 15:55:32 -0400224void GPBSetInt32IvarWithFieldPrivate(GPBMessage *self,
225 GPBFieldDescriptor *field,
226 int32_t value);
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400227//%PDDM-EXPAND GPB_IVAR_SET_DECL(UInt32, uint32_t)
228// This block of code is generated, do not edit it directly.
229
Thomas Van Lentene1e5b8a2020-04-10 15:55:32 -0400230void GPBSetUInt32IvarWithFieldPrivate(GPBMessage *self,
231 GPBFieldDescriptor *field,
232 uint32_t value);
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400233//%PDDM-EXPAND GPB_IVAR_SET_DECL(Int64, int64_t)
234// This block of code is generated, do not edit it directly.
235
Thomas Van Lentene1e5b8a2020-04-10 15:55:32 -0400236void GPBSetInt64IvarWithFieldPrivate(GPBMessage *self,
237 GPBFieldDescriptor *field,
238 int64_t value);
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400239//%PDDM-EXPAND GPB_IVAR_SET_DECL(UInt64, uint64_t)
240// This block of code is generated, do not edit it directly.
241
Thomas Van Lentene1e5b8a2020-04-10 15:55:32 -0400242void GPBSetUInt64IvarWithFieldPrivate(GPBMessage *self,
243 GPBFieldDescriptor *field,
244 uint64_t value);
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400245//%PDDM-EXPAND GPB_IVAR_SET_DECL(Float, float)
246// This block of code is generated, do not edit it directly.
247
Thomas Van Lentene1e5b8a2020-04-10 15:55:32 -0400248void GPBSetFloatIvarWithFieldPrivate(GPBMessage *self,
249 GPBFieldDescriptor *field,
250 float value);
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400251//%PDDM-EXPAND GPB_IVAR_SET_DECL(Double, double)
252// This block of code is generated, do not edit it directly.
253
Thomas Van Lentene1e5b8a2020-04-10 15:55:32 -0400254void GPBSetDoubleIvarWithFieldPrivate(GPBMessage *self,
255 GPBFieldDescriptor *field,
256 double value);
Thomas Van Lentene1e5b8a2020-04-10 15:55:32 -0400257//%PDDM-EXPAND-END (7 expansions)
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400258
Thomas Van Lenten9e069b22022-09-19 13:34:01 -0400259// clang-format on
260
Thomas Van Lenten5d0b2172022-09-19 17:20:35 -0400261void GPBSetEnumIvarWithFieldPrivate(GPBMessage *self, GPBFieldDescriptor *field, int32_t value);
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400262
263id GPBGetObjectIvarWithField(GPBMessage *self, GPBFieldDescriptor *field);
264
Thomas Van Lenten5d0b2172022-09-19 17:20:35 -0400265void GPBSetObjectIvarWithFieldPrivate(GPBMessage *self, GPBFieldDescriptor *field, id value);
266void GPBSetRetainedObjectIvarWithFieldPrivate(GPBMessage *self, GPBFieldDescriptor *field,
267 id __attribute__((ns_consumed)) value);
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400268
269// GPBGetObjectIvarWithField will automatically create the field (message) if
270// it doesn't exist. GPBGetObjectIvarWithFieldNoAutocreate will return nil.
Thomas Van Lenten5d0b2172022-09-19 17:20:35 -0400271id GPBGetObjectIvarWithFieldNoAutocreate(GPBMessage *self, GPBFieldDescriptor *field);
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400272
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400273// Clears and releases the autocreated message ivar, if it's autocreated. If
274// it's not set as autocreated, this method does nothing.
Thomas Van Lenten5d0b2172022-09-19 17:20:35 -0400275void GPBClearAutocreatedMessageIvarWithField(GPBMessage *self, GPBFieldDescriptor *field);
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400276
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400277// Returns an Objective C encoding for |selector|. |instanceSel| should be
278// YES if it's an instance selector (as opposed to a class selector).
279// |selector| must be a selector from MessageSignatureProtocol.
280const char *GPBMessageEncodingForSelector(SEL selector, BOOL instanceSel);
281
282// Helper for text format name encoding.
Peter Newmane2cc2de2020-08-10 19:08:25 +0100283// decodeData is the data describing the special decodes.
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400284// key and inputString are the input that needs decoding.
Thomas Van Lenten5d0b2172022-09-19 17:20:35 -0400285NSString *GPBDecodeTextFormatName(const uint8_t *decodeData, int32_t key, NSString *inputString);
Thomas Van Lentene1e5b8a2020-04-10 15:55:32 -0400286
Thomas Van Lenten3c8e9592020-04-13 10:49:16 -0400287// Shims from the older generated code into the runtime.
Thomas Van Lenten5d0b2172022-09-19 17:20:35 -0400288void GPBSetInt32IvarWithFieldInternal(GPBMessage *self, GPBFieldDescriptor *field, int32_t value,
Thomas Van Lentene1e5b8a2020-04-10 15:55:32 -0400289 GPBFileSyntax syntax);
Thomas Van Lenten5d0b2172022-09-19 17:20:35 -0400290void GPBMaybeClearOneof(GPBMessage *self, GPBOneofDescriptor *oneof, int32_t oneofHasIndex,
291 uint32_t fieldNumberNotToClear);
Thomas Van Lentene1e5b8a2020-04-10 15:55:32 -0400292
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400293// A series of selectors that are used solely to get @encoding values
294// for them by the dynamic protobuf runtime code. See
Dave MacLachlan37a66722017-11-14 15:16:04 -0800295// GPBMessageEncodingForSelector for details. GPBRootObject conforms to
296// the protocol so that it is encoded in the Objective C runtime.
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400297@protocol GPBMessageSignatureProtocol
298@optional
299
300#define GPB_MESSAGE_SIGNATURE_ENTRY(TYPE, NAME) \
301 -(TYPE)get##NAME; \
302 -(void)set##NAME : (TYPE)value; \
303 -(TYPE)get##NAME##AtIndex : (NSUInteger)index;
304
305GPB_MESSAGE_SIGNATURE_ENTRY(BOOL, Bool)
306GPB_MESSAGE_SIGNATURE_ENTRY(uint32_t, Fixed32)
307GPB_MESSAGE_SIGNATURE_ENTRY(int32_t, SFixed32)
308GPB_MESSAGE_SIGNATURE_ENTRY(float, Float)
309GPB_MESSAGE_SIGNATURE_ENTRY(uint64_t, Fixed64)
310GPB_MESSAGE_SIGNATURE_ENTRY(int64_t, SFixed64)
311GPB_MESSAGE_SIGNATURE_ENTRY(double, Double)
312GPB_MESSAGE_SIGNATURE_ENTRY(int32_t, Int32)
313GPB_MESSAGE_SIGNATURE_ENTRY(int64_t, Int64)
314GPB_MESSAGE_SIGNATURE_ENTRY(int32_t, SInt32)
315GPB_MESSAGE_SIGNATURE_ENTRY(int64_t, SInt64)
316GPB_MESSAGE_SIGNATURE_ENTRY(uint32_t, UInt32)
317GPB_MESSAGE_SIGNATURE_ENTRY(uint64_t, UInt64)
Thomas Van Lentend846b0b2015-06-08 16:24:57 -0400318GPB_MESSAGE_SIGNATURE_ENTRY(NSData *, Bytes)
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400319GPB_MESSAGE_SIGNATURE_ENTRY(NSString *, String)
320GPB_MESSAGE_SIGNATURE_ENTRY(GPBMessage *, Message)
321GPB_MESSAGE_SIGNATURE_ENTRY(GPBMessage *, Group)
322GPB_MESSAGE_SIGNATURE_ENTRY(int32_t, Enum)
323
324#undef GPB_MESSAGE_SIGNATURE_ENTRY
325
326- (id)getArray;
Thomas Van Lentend846b0b2015-06-08 16:24:57 -0400327- (NSUInteger)getArrayCount;
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400328- (void)setArray:(NSArray *)array;
329+ (id)getClassValue;
330@end
331
Thomas Van Lenten2d1c5e22017-03-02 14:50:10 -0500332BOOL GPBClassHasSel(Class aClass, SEL sel);
333
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400334CF_EXTERN_C_END