blob: db5d3b60cdc435161ab31becb7e46ced83bd5327 [file] [log] [blame]
Brian Silverman9c614bc2016-02-15 20:20:02 -05001// 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 "GPBMessage_PackagePrivate.h"
32
33#import <objc/runtime.h>
34#import <objc/message.h>
Austin Schuh40c16522018-10-28 20:27:54 -070035#import <stdatomic.h>
Brian Silverman9c614bc2016-02-15 20:20:02 -050036
37#import "GPBArray_PackagePrivate.h"
38#import "GPBCodedInputStream_PackagePrivate.h"
Austin Schuh40c16522018-10-28 20:27:54 -070039#import "GPBCodedOutputStream_PackagePrivate.h"
Brian Silverman9c614bc2016-02-15 20:20:02 -050040#import "GPBDescriptor_PackagePrivate.h"
41#import "GPBDictionary_PackagePrivate.h"
42#import "GPBExtensionInternals.h"
43#import "GPBExtensionRegistry.h"
44#import "GPBRootObject_PackagePrivate.h"
45#import "GPBUnknownFieldSet_PackagePrivate.h"
46#import "GPBUtilities_PackagePrivate.h"
47
Austin Schuh40c16522018-10-28 20:27:54 -070048// Direct access is use for speed, to avoid even internally declaring things
49// read/write, etc. The warning is enabled in the project to ensure code calling
50// protos can turn on -Wdirect-ivar-access without issues.
51#pragma clang diagnostic push
52#pragma clang diagnostic ignored "-Wdirect-ivar-access"
53
Brian Silverman9c614bc2016-02-15 20:20:02 -050054NSString *const GPBMessageErrorDomain =
55 GPBNSStringifySymbol(GPBMessageErrorDomain);
56
Austin Schuh40c16522018-10-28 20:27:54 -070057NSString *const GPBErrorReasonKey = @"Reason";
Brian Silverman9c614bc2016-02-15 20:20:02 -050058
59static NSString *const kGPBDataCoderKey = @"GPBData";
60
Brian Silverman9c614bc2016-02-15 20:20:02 -050061//
62// PLEASE REMEMBER:
63//
64// This is the base class for *all* messages generated, so any selector defined,
65// *public* or *private* could end up colliding with a proto message field. So
66// avoid using selectors that could match a property, use C functions to hide
67// them, etc.
68//
69
70@interface GPBMessage () {
71 @package
72 GPBUnknownFieldSet *unknownFields_;
73 NSMutableDictionary *extensionMap_;
74 NSMutableDictionary *autocreatedExtensionMap_;
75
76 // If the object was autocreated, we remember the creator so that if we get
77 // mutated, we can inform the creator to make our field visible.
78 GPBMessage *autocreator_;
79 GPBFieldDescriptor *autocreatorField_;
80 GPBExtensionDescriptor *autocreatorExtension_;
Austin Schuh40c16522018-10-28 20:27:54 -070081
82 // A lock to provide mutual exclusion from internal data that can be modified
83 // by *read* operations such as getters (autocreation of message fields and
84 // message extensions, not setting of values). Used to guarantee thread safety
85 // for concurrent reads on the message.
86 // NOTE: OSSpinLock may seem like a good fit here but Apple engineers have
87 // pointed out that they are vulnerable to live locking on iOS in cases of
88 // priority inversion:
89 // http://mjtsai.com/blog/2015/12/16/osspinlock-is-unsafe/
90 // https://lists.swift.org/pipermail/swift-dev/Week-of-Mon-20151214/000372.html
91 // Use of readOnlySemaphore_ must be prefaced by a call to
92 // GPBPrepareReadOnlySemaphore to ensure it has been created. This allows
93 // readOnlySemaphore_ to be only created when actually needed.
94 _Atomic(dispatch_semaphore_t) readOnlySemaphore_;
Brian Silverman9c614bc2016-02-15 20:20:02 -050095}
96@end
97
98static id CreateArrayForField(GPBFieldDescriptor *field,
99 GPBMessage *autocreator)
100 __attribute__((ns_returns_retained));
101static id GetOrCreateArrayIvarWithField(GPBMessage *self,
102 GPBFieldDescriptor *field,
103 GPBFileSyntax syntax);
104static id GetArrayIvarWithField(GPBMessage *self, GPBFieldDescriptor *field);
105static id CreateMapForField(GPBFieldDescriptor *field,
106 GPBMessage *autocreator)
107 __attribute__((ns_returns_retained));
108static id GetOrCreateMapIvarWithField(GPBMessage *self,
109 GPBFieldDescriptor *field,
110 GPBFileSyntax syntax);
111static id GetMapIvarWithField(GPBMessage *self, GPBFieldDescriptor *field);
112static NSMutableDictionary *CloneExtensionMap(NSDictionary *extensionMap,
113 NSZone *zone)
114 __attribute__((ns_returns_retained));
115
Austin Schuh40c16522018-10-28 20:27:54 -0700116#ifdef DEBUG
Brian Silverman9c614bc2016-02-15 20:20:02 -0500117static NSError *MessageError(NSInteger code, NSDictionary *userInfo) {
118 return [NSError errorWithDomain:GPBMessageErrorDomain
119 code:code
120 userInfo:userInfo];
121}
Austin Schuh40c16522018-10-28 20:27:54 -0700122#endif
Brian Silverman9c614bc2016-02-15 20:20:02 -0500123
Austin Schuh40c16522018-10-28 20:27:54 -0700124static NSError *ErrorFromException(NSException *exception) {
125 NSError *error = nil;
126
127 if ([exception.name isEqual:GPBCodedInputStreamException]) {
128 NSDictionary *exceptionInfo = exception.userInfo;
129 error = exceptionInfo[GPBCodedInputStreamUnderlyingErrorKey];
Brian Silverman9c614bc2016-02-15 20:20:02 -0500130 }
Brian Silverman9c614bc2016-02-15 20:20:02 -0500131
Austin Schuh40c16522018-10-28 20:27:54 -0700132 if (!error) {
133 NSString *reason = exception.reason;
134 NSDictionary *userInfo = nil;
135 if ([reason length]) {
136 userInfo = @{ GPBErrorReasonKey : reason };
137 }
138
139 error = [NSError errorWithDomain:GPBMessageErrorDomain
140 code:GPBMessageErrorCodeOther
141 userInfo:userInfo];
142 }
143 return error;
144}
Brian Silverman9c614bc2016-02-15 20:20:02 -0500145
146static void CheckExtension(GPBMessage *self,
147 GPBExtensionDescriptor *extension) {
Austin Schuh40c16522018-10-28 20:27:54 -0700148 if (![self isKindOfClass:extension.containingMessageClass]) {
Brian Silverman9c614bc2016-02-15 20:20:02 -0500149 [NSException
150 raise:NSInvalidArgumentException
151 format:@"Extension %@ used on wrong class (%@ instead of %@)",
152 extension.singletonName,
153 [self class], extension.containingMessageClass];
154 }
155}
156
157static NSMutableDictionary *CloneExtensionMap(NSDictionary *extensionMap,
158 NSZone *zone) {
159 if (extensionMap.count == 0) {
160 return nil;
161 }
162 NSMutableDictionary *result = [[NSMutableDictionary allocWithZone:zone]
163 initWithCapacity:extensionMap.count];
164
165 for (GPBExtensionDescriptor *extension in extensionMap) {
166 id value = [extensionMap objectForKey:extension];
167 BOOL isMessageExtension = GPBExtensionIsMessage(extension);
168
169 if (extension.repeated) {
170 if (isMessageExtension) {
171 NSMutableArray *list =
172 [[NSMutableArray alloc] initWithCapacity:[value count]];
173 for (GPBMessage *listValue in value) {
174 GPBMessage *copiedValue = [listValue copyWithZone:zone];
175 [list addObject:copiedValue];
176 [copiedValue release];
177 }
178 [result setObject:list forKey:extension];
179 [list release];
180 } else {
181 NSMutableArray *copiedValue = [value mutableCopyWithZone:zone];
182 [result setObject:copiedValue forKey:extension];
183 [copiedValue release];
184 }
185 } else {
186 if (isMessageExtension) {
187 GPBMessage *copiedValue = [value copyWithZone:zone];
188 [result setObject:copiedValue forKey:extension];
189 [copiedValue release];
190 } else {
191 [result setObject:value forKey:extension];
192 }
193 }
194 }
195
196 return result;
197}
198
199static id CreateArrayForField(GPBFieldDescriptor *field,
200 GPBMessage *autocreator) {
201 id result;
202 GPBDataType fieldDataType = GPBGetFieldDataType(field);
203 switch (fieldDataType) {
204 case GPBDataTypeBool:
205 result = [[GPBBoolArray alloc] init];
206 break;
207 case GPBDataTypeFixed32:
208 case GPBDataTypeUInt32:
209 result = [[GPBUInt32Array alloc] init];
210 break;
211 case GPBDataTypeInt32:
212 case GPBDataTypeSFixed32:
213 case GPBDataTypeSInt32:
214 result = [[GPBInt32Array alloc] init];
215 break;
216 case GPBDataTypeFixed64:
217 case GPBDataTypeUInt64:
218 result = [[GPBUInt64Array alloc] init];
219 break;
220 case GPBDataTypeInt64:
221 case GPBDataTypeSFixed64:
222 case GPBDataTypeSInt64:
223 result = [[GPBInt64Array alloc] init];
224 break;
225 case GPBDataTypeFloat:
226 result = [[GPBFloatArray alloc] init];
227 break;
228 case GPBDataTypeDouble:
229 result = [[GPBDoubleArray alloc] init];
230 break;
231
232 case GPBDataTypeEnum:
233 result = [[GPBEnumArray alloc]
234 initWithValidationFunction:field.enumDescriptor.enumVerifier];
235 break;
236
237 case GPBDataTypeBytes:
238 case GPBDataTypeGroup:
239 case GPBDataTypeMessage:
240 case GPBDataTypeString:
241 if (autocreator) {
242 result = [[GPBAutocreatedArray alloc] init];
243 } else {
244 result = [[NSMutableArray alloc] init];
245 }
246 break;
247 }
248
249 if (autocreator) {
250 if (GPBDataTypeIsObject(fieldDataType)) {
251 GPBAutocreatedArray *autoArray = result;
252 autoArray->_autocreator = autocreator;
253 } else {
254 GPBInt32Array *gpbArray = result;
255 gpbArray->_autocreator = autocreator;
256 }
257 }
258
259 return result;
260}
261
262static id CreateMapForField(GPBFieldDescriptor *field,
263 GPBMessage *autocreator) {
264 id result;
265 GPBDataType keyDataType = field.mapKeyDataType;
266 GPBDataType valueDataType = GPBGetFieldDataType(field);
267 switch (keyDataType) {
268 case GPBDataTypeBool:
269 switch (valueDataType) {
270 case GPBDataTypeBool:
271 result = [[GPBBoolBoolDictionary alloc] init];
272 break;
273 case GPBDataTypeFixed32:
274 case GPBDataTypeUInt32:
275 result = [[GPBBoolUInt32Dictionary alloc] init];
276 break;
277 case GPBDataTypeInt32:
278 case GPBDataTypeSFixed32:
279 case GPBDataTypeSInt32:
280 result = [[GPBBoolInt32Dictionary alloc] init];
281 break;
282 case GPBDataTypeFixed64:
283 case GPBDataTypeUInt64:
284 result = [[GPBBoolUInt64Dictionary alloc] init];
285 break;
286 case GPBDataTypeInt64:
287 case GPBDataTypeSFixed64:
288 case GPBDataTypeSInt64:
289 result = [[GPBBoolInt64Dictionary alloc] init];
290 break;
291 case GPBDataTypeFloat:
292 result = [[GPBBoolFloatDictionary alloc] init];
293 break;
294 case GPBDataTypeDouble:
295 result = [[GPBBoolDoubleDictionary alloc] init];
296 break;
297 case GPBDataTypeEnum:
298 result = [[GPBBoolEnumDictionary alloc]
299 initWithValidationFunction:field.enumDescriptor.enumVerifier];
300 break;
301 case GPBDataTypeBytes:
302 case GPBDataTypeMessage:
303 case GPBDataTypeString:
304 result = [[GPBBoolObjectDictionary alloc] init];
305 break;
306 case GPBDataTypeGroup:
307 NSCAssert(NO, @"shouldn't happen");
308 return nil;
309 }
310 break;
311 case GPBDataTypeFixed32:
312 case GPBDataTypeUInt32:
313 switch (valueDataType) {
314 case GPBDataTypeBool:
315 result = [[GPBUInt32BoolDictionary alloc] init];
316 break;
317 case GPBDataTypeFixed32:
318 case GPBDataTypeUInt32:
319 result = [[GPBUInt32UInt32Dictionary alloc] init];
320 break;
321 case GPBDataTypeInt32:
322 case GPBDataTypeSFixed32:
323 case GPBDataTypeSInt32:
324 result = [[GPBUInt32Int32Dictionary alloc] init];
325 break;
326 case GPBDataTypeFixed64:
327 case GPBDataTypeUInt64:
328 result = [[GPBUInt32UInt64Dictionary alloc] init];
329 break;
330 case GPBDataTypeInt64:
331 case GPBDataTypeSFixed64:
332 case GPBDataTypeSInt64:
333 result = [[GPBUInt32Int64Dictionary alloc] init];
334 break;
335 case GPBDataTypeFloat:
336 result = [[GPBUInt32FloatDictionary alloc] init];
337 break;
338 case GPBDataTypeDouble:
339 result = [[GPBUInt32DoubleDictionary alloc] init];
340 break;
341 case GPBDataTypeEnum:
342 result = [[GPBUInt32EnumDictionary alloc]
343 initWithValidationFunction:field.enumDescriptor.enumVerifier];
344 break;
345 case GPBDataTypeBytes:
346 case GPBDataTypeMessage:
347 case GPBDataTypeString:
348 result = [[GPBUInt32ObjectDictionary alloc] init];
349 break;
350 case GPBDataTypeGroup:
351 NSCAssert(NO, @"shouldn't happen");
352 return nil;
353 }
354 break;
355 case GPBDataTypeInt32:
356 case GPBDataTypeSFixed32:
357 case GPBDataTypeSInt32:
358 switch (valueDataType) {
359 case GPBDataTypeBool:
360 result = [[GPBInt32BoolDictionary alloc] init];
361 break;
362 case GPBDataTypeFixed32:
363 case GPBDataTypeUInt32:
364 result = [[GPBInt32UInt32Dictionary alloc] init];
365 break;
366 case GPBDataTypeInt32:
367 case GPBDataTypeSFixed32:
368 case GPBDataTypeSInt32:
369 result = [[GPBInt32Int32Dictionary alloc] init];
370 break;
371 case GPBDataTypeFixed64:
372 case GPBDataTypeUInt64:
373 result = [[GPBInt32UInt64Dictionary alloc] init];
374 break;
375 case GPBDataTypeInt64:
376 case GPBDataTypeSFixed64:
377 case GPBDataTypeSInt64:
378 result = [[GPBInt32Int64Dictionary alloc] init];
379 break;
380 case GPBDataTypeFloat:
381 result = [[GPBInt32FloatDictionary alloc] init];
382 break;
383 case GPBDataTypeDouble:
384 result = [[GPBInt32DoubleDictionary alloc] init];
385 break;
386 case GPBDataTypeEnum:
387 result = [[GPBInt32EnumDictionary alloc]
388 initWithValidationFunction:field.enumDescriptor.enumVerifier];
389 break;
390 case GPBDataTypeBytes:
391 case GPBDataTypeMessage:
392 case GPBDataTypeString:
393 result = [[GPBInt32ObjectDictionary alloc] init];
394 break;
395 case GPBDataTypeGroup:
396 NSCAssert(NO, @"shouldn't happen");
397 return nil;
398 }
399 break;
400 case GPBDataTypeFixed64:
401 case GPBDataTypeUInt64:
402 switch (valueDataType) {
403 case GPBDataTypeBool:
404 result = [[GPBUInt64BoolDictionary alloc] init];
405 break;
406 case GPBDataTypeFixed32:
407 case GPBDataTypeUInt32:
408 result = [[GPBUInt64UInt32Dictionary alloc] init];
409 break;
410 case GPBDataTypeInt32:
411 case GPBDataTypeSFixed32:
412 case GPBDataTypeSInt32:
413 result = [[GPBUInt64Int32Dictionary alloc] init];
414 break;
415 case GPBDataTypeFixed64:
416 case GPBDataTypeUInt64:
417 result = [[GPBUInt64UInt64Dictionary alloc] init];
418 break;
419 case GPBDataTypeInt64:
420 case GPBDataTypeSFixed64:
421 case GPBDataTypeSInt64:
422 result = [[GPBUInt64Int64Dictionary alloc] init];
423 break;
424 case GPBDataTypeFloat:
425 result = [[GPBUInt64FloatDictionary alloc] init];
426 break;
427 case GPBDataTypeDouble:
428 result = [[GPBUInt64DoubleDictionary alloc] init];
429 break;
430 case GPBDataTypeEnum:
431 result = [[GPBUInt64EnumDictionary alloc]
432 initWithValidationFunction:field.enumDescriptor.enumVerifier];
433 break;
434 case GPBDataTypeBytes:
435 case GPBDataTypeMessage:
436 case GPBDataTypeString:
437 result = [[GPBUInt64ObjectDictionary alloc] init];
438 break;
439 case GPBDataTypeGroup:
440 NSCAssert(NO, @"shouldn't happen");
441 return nil;
442 }
443 break;
444 case GPBDataTypeInt64:
445 case GPBDataTypeSFixed64:
446 case GPBDataTypeSInt64:
447 switch (valueDataType) {
448 case GPBDataTypeBool:
449 result = [[GPBInt64BoolDictionary alloc] init];
450 break;
451 case GPBDataTypeFixed32:
452 case GPBDataTypeUInt32:
453 result = [[GPBInt64UInt32Dictionary alloc] init];
454 break;
455 case GPBDataTypeInt32:
456 case GPBDataTypeSFixed32:
457 case GPBDataTypeSInt32:
458 result = [[GPBInt64Int32Dictionary alloc] init];
459 break;
460 case GPBDataTypeFixed64:
461 case GPBDataTypeUInt64:
462 result = [[GPBInt64UInt64Dictionary alloc] init];
463 break;
464 case GPBDataTypeInt64:
465 case GPBDataTypeSFixed64:
466 case GPBDataTypeSInt64:
467 result = [[GPBInt64Int64Dictionary alloc] init];
468 break;
469 case GPBDataTypeFloat:
470 result = [[GPBInt64FloatDictionary alloc] init];
471 break;
472 case GPBDataTypeDouble:
473 result = [[GPBInt64DoubleDictionary alloc] init];
474 break;
475 case GPBDataTypeEnum:
476 result = [[GPBInt64EnumDictionary alloc]
477 initWithValidationFunction:field.enumDescriptor.enumVerifier];
478 break;
479 case GPBDataTypeBytes:
480 case GPBDataTypeMessage:
481 case GPBDataTypeString:
482 result = [[GPBInt64ObjectDictionary alloc] init];
483 break;
484 case GPBDataTypeGroup:
485 NSCAssert(NO, @"shouldn't happen");
486 return nil;
487 }
488 break;
489 case GPBDataTypeString:
490 switch (valueDataType) {
491 case GPBDataTypeBool:
492 result = [[GPBStringBoolDictionary alloc] init];
493 break;
494 case GPBDataTypeFixed32:
495 case GPBDataTypeUInt32:
496 result = [[GPBStringUInt32Dictionary alloc] init];
497 break;
498 case GPBDataTypeInt32:
499 case GPBDataTypeSFixed32:
500 case GPBDataTypeSInt32:
501 result = [[GPBStringInt32Dictionary alloc] init];
502 break;
503 case GPBDataTypeFixed64:
504 case GPBDataTypeUInt64:
505 result = [[GPBStringUInt64Dictionary alloc] init];
506 break;
507 case GPBDataTypeInt64:
508 case GPBDataTypeSFixed64:
509 case GPBDataTypeSInt64:
510 result = [[GPBStringInt64Dictionary alloc] init];
511 break;
512 case GPBDataTypeFloat:
513 result = [[GPBStringFloatDictionary alloc] init];
514 break;
515 case GPBDataTypeDouble:
516 result = [[GPBStringDoubleDictionary alloc] init];
517 break;
518 case GPBDataTypeEnum:
519 result = [[GPBStringEnumDictionary alloc]
520 initWithValidationFunction:field.enumDescriptor.enumVerifier];
521 break;
522 case GPBDataTypeBytes:
523 case GPBDataTypeMessage:
524 case GPBDataTypeString:
525 if (autocreator) {
526 result = [[GPBAutocreatedDictionary alloc] init];
527 } else {
528 result = [[NSMutableDictionary alloc] init];
529 }
530 break;
531 case GPBDataTypeGroup:
532 NSCAssert(NO, @"shouldn't happen");
533 return nil;
534 }
535 break;
536
537 case GPBDataTypeFloat:
538 case GPBDataTypeDouble:
539 case GPBDataTypeEnum:
540 case GPBDataTypeBytes:
541 case GPBDataTypeGroup:
542 case GPBDataTypeMessage:
543 NSCAssert(NO, @"shouldn't happen");
544 return nil;
545 }
546
547 if (autocreator) {
548 if ((keyDataType == GPBDataTypeString) &&
549 GPBDataTypeIsObject(valueDataType)) {
550 GPBAutocreatedDictionary *autoDict = result;
551 autoDict->_autocreator = autocreator;
552 } else {
553 GPBInt32Int32Dictionary *gpbDict = result;
554 gpbDict->_autocreator = autocreator;
555 }
556 }
557
558 return result;
559}
560
561#if !defined(__clang_analyzer__)
562// These functions are blocked from the analyzer because the analyzer sees the
563// GPBSetRetainedObjectIvarWithFieldInternal() call as consuming the array/map,
564// so use of the array/map after the call returns is flagged as a use after
565// free.
566// But GPBSetRetainedObjectIvarWithFieldInternal() is "consuming" the retain
567// count be holding onto the object (it is transfering it), the object is
568// still valid after returning from the call. The other way to avoid this
569// would be to add a -retain/-autorelease, but that would force every
570// repeated/map field parsed into the autorelease pool which is both a memory
571// and performance hit.
572
573static id GetOrCreateArrayIvarWithField(GPBMessage *self,
574 GPBFieldDescriptor *field,
575 GPBFileSyntax syntax) {
576 id array = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
577 if (!array) {
578 // No lock needed, this is called from places expecting to mutate
579 // so no threading protection is needed.
580 array = CreateArrayForField(field, nil);
581 GPBSetRetainedObjectIvarWithFieldInternal(self, field, array, syntax);
582 }
583 return array;
584}
585
586// This is like GPBGetObjectIvarWithField(), but for arrays, it should
587// only be used to wire the method into the class.
588static id GetArrayIvarWithField(GPBMessage *self, GPBFieldDescriptor *field) {
589 id array = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
590 if (!array) {
591 // Check again after getting the lock.
Austin Schuh40c16522018-10-28 20:27:54 -0700592 GPBPrepareReadOnlySemaphore(self);
Brian Silverman9c614bc2016-02-15 20:20:02 -0500593 dispatch_semaphore_wait(self->readOnlySemaphore_, DISPATCH_TIME_FOREVER);
594 array = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
595 if (!array) {
596 array = CreateArrayForField(field, self);
597 GPBSetAutocreatedRetainedObjectIvarWithField(self, field, array);
598 }
599 dispatch_semaphore_signal(self->readOnlySemaphore_);
600 }
601 return array;
602}
603
604static id GetOrCreateMapIvarWithField(GPBMessage *self,
605 GPBFieldDescriptor *field,
606 GPBFileSyntax syntax) {
607 id dict = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
608 if (!dict) {
609 // No lock needed, this is called from places expecting to mutate
610 // so no threading protection is needed.
611 dict = CreateMapForField(field, nil);
612 GPBSetRetainedObjectIvarWithFieldInternal(self, field, dict, syntax);
613 }
614 return dict;
615}
616
617// This is like GPBGetObjectIvarWithField(), but for maps, it should
618// only be used to wire the method into the class.
619static id GetMapIvarWithField(GPBMessage *self, GPBFieldDescriptor *field) {
620 id dict = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
621 if (!dict) {
622 // Check again after getting the lock.
Austin Schuh40c16522018-10-28 20:27:54 -0700623 GPBPrepareReadOnlySemaphore(self);
Brian Silverman9c614bc2016-02-15 20:20:02 -0500624 dispatch_semaphore_wait(self->readOnlySemaphore_, DISPATCH_TIME_FOREVER);
625 dict = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
626 if (!dict) {
627 dict = CreateMapForField(field, self);
628 GPBSetAutocreatedRetainedObjectIvarWithField(self, field, dict);
629 }
630 dispatch_semaphore_signal(self->readOnlySemaphore_);
631 }
632 return dict;
633}
634
635#endif // !defined(__clang_analyzer__)
636
637GPBMessage *GPBCreateMessageWithAutocreator(Class msgClass,
638 GPBMessage *autocreator,
639 GPBFieldDescriptor *field) {
640 GPBMessage *message = [[msgClass alloc] init];
641 message->autocreator_ = autocreator;
642 message->autocreatorField_ = [field retain];
643 return message;
644}
645
646static GPBMessage *CreateMessageWithAutocreatorForExtension(
647 Class msgClass, GPBMessage *autocreator, GPBExtensionDescriptor *extension)
648 __attribute__((ns_returns_retained));
649
650static GPBMessage *CreateMessageWithAutocreatorForExtension(
651 Class msgClass, GPBMessage *autocreator,
652 GPBExtensionDescriptor *extension) {
653 GPBMessage *message = [[msgClass alloc] init];
654 message->autocreator_ = autocreator;
655 message->autocreatorExtension_ = [extension retain];
656 return message;
657}
658
659BOOL GPBWasMessageAutocreatedBy(GPBMessage *message, GPBMessage *parent) {
660 return (message->autocreator_ == parent);
661}
662
663void GPBBecomeVisibleToAutocreator(GPBMessage *self) {
664 // Message objects that are implicitly created by accessing a message field
665 // are initially not visible via the hasX selector. This method makes them
666 // visible.
667 if (self->autocreator_) {
668 // This will recursively make all parent messages visible until it reaches a
669 // super-creator that's visible.
670 if (self->autocreatorField_) {
671 GPBFileSyntax syntax = [self->autocreator_ descriptor].file.syntax;
672 GPBSetObjectIvarWithFieldInternal(self->autocreator_,
673 self->autocreatorField_, self, syntax);
674 } else {
675 [self->autocreator_ setExtension:self->autocreatorExtension_ value:self];
676 }
677 }
678}
679
680void GPBAutocreatedArrayModified(GPBMessage *self, id array) {
681 // When one of our autocreated arrays adds elements, make it visible.
682 GPBDescriptor *descriptor = [[self class] descriptor];
683 for (GPBFieldDescriptor *field in descriptor->fields_) {
684 if (field.fieldType == GPBFieldTypeRepeated) {
685 id curArray = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
686 if (curArray == array) {
687 if (GPBFieldDataTypeIsObject(field)) {
688 GPBAutocreatedArray *autoArray = array;
689 autoArray->_autocreator = nil;
690 } else {
691 GPBInt32Array *gpbArray = array;
692 gpbArray->_autocreator = nil;
693 }
694 GPBBecomeVisibleToAutocreator(self);
695 return;
696 }
697 }
698 }
699 NSCAssert(NO, @"Unknown autocreated %@ for %@.", [array class], self);
700}
701
702void GPBAutocreatedDictionaryModified(GPBMessage *self, id dictionary) {
703 // When one of our autocreated dicts adds elements, make it visible.
704 GPBDescriptor *descriptor = [[self class] descriptor];
705 for (GPBFieldDescriptor *field in descriptor->fields_) {
706 if (field.fieldType == GPBFieldTypeMap) {
707 id curDict = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
708 if (curDict == dictionary) {
709 if ((field.mapKeyDataType == GPBDataTypeString) &&
710 GPBFieldDataTypeIsObject(field)) {
711 GPBAutocreatedDictionary *autoDict = dictionary;
712 autoDict->_autocreator = nil;
713 } else {
714 GPBInt32Int32Dictionary *gpbDict = dictionary;
715 gpbDict->_autocreator = nil;
716 }
717 GPBBecomeVisibleToAutocreator(self);
718 return;
719 }
720 }
721 }
722 NSCAssert(NO, @"Unknown autocreated %@ for %@.", [dictionary class], self);
723}
724
725void GPBClearMessageAutocreator(GPBMessage *self) {
726 if ((self == nil) || !self->autocreator_) {
727 return;
728 }
729
Austin Schuh40c16522018-10-28 20:27:54 -0700730#if defined(DEBUG) && DEBUG && !defined(NS_BLOCK_ASSERTIONS)
Brian Silverman9c614bc2016-02-15 20:20:02 -0500731 // Either the autocreator must have its "has" flag set to YES, or it must be
732 // NO and not equal to ourselves.
733 BOOL autocreatorHas =
734 (self->autocreatorField_
735 ? GPBGetHasIvarField(self->autocreator_, self->autocreatorField_)
736 : [self->autocreator_ hasExtension:self->autocreatorExtension_]);
737 GPBMessage *autocreatorFieldValue =
738 (self->autocreatorField_
739 ? GPBGetObjectIvarWithFieldNoAutocreate(self->autocreator_,
740 self->autocreatorField_)
741 : [self->autocreator_->autocreatedExtensionMap_
742 objectForKey:self->autocreatorExtension_]);
743 NSCAssert(autocreatorHas || autocreatorFieldValue != self,
744 @"Cannot clear autocreator because it still refers to self, self: %@.",
745 self);
746
747#endif // DEBUG && !defined(NS_BLOCK_ASSERTIONS)
748
749 self->autocreator_ = nil;
750 [self->autocreatorField_ release];
751 self->autocreatorField_ = nil;
752 [self->autocreatorExtension_ release];
753 self->autocreatorExtension_ = nil;
754}
755
Austin Schuh40c16522018-10-28 20:27:54 -0700756// Call this before using the readOnlySemaphore_. This ensures it is created only once.
757void GPBPrepareReadOnlySemaphore(GPBMessage *self) {
758#pragma clang diagnostic push
759#pragma clang diagnostic ignored "-Wdirect-ivar-access"
760
761 // Create the semaphore on demand (rather than init) as developers might not cause them
762 // to be needed, and the heap usage can add up. The atomic swap is used to avoid needing
763 // another lock around creating it.
764 if (self->readOnlySemaphore_ == nil) {
765 dispatch_semaphore_t worker = dispatch_semaphore_create(1);
766 dispatch_semaphore_t expected = nil;
767 if (!atomic_compare_exchange_strong(&self->readOnlySemaphore_, &expected, worker)) {
768 dispatch_release(worker);
769 }
770#if defined(__clang_analyzer__)
771 // The Xcode 9.2 (and 9.3 beta) static analyzer thinks worker is leaked
772 // (doesn't seem to know about atomic_compare_exchange_strong); so just
773 // for the analyzer, let it think worker is also released in this case.
774 else { dispatch_release(worker); }
775#endif
776 }
777
778#pragma clang diagnostic pop
779}
780
Brian Silverman9c614bc2016-02-15 20:20:02 -0500781static GPBUnknownFieldSet *GetOrMakeUnknownFields(GPBMessage *self) {
782 if (!self->unknownFields_) {
783 self->unknownFields_ = [[GPBUnknownFieldSet alloc] init];
784 GPBBecomeVisibleToAutocreator(self);
785 }
786 return self->unknownFields_;
787}
788
789@implementation GPBMessage
790
791+ (void)initialize {
792 Class pbMessageClass = [GPBMessage class];
793 if ([self class] == pbMessageClass) {
794 // This is here to start up the "base" class descriptor.
795 [self descriptor];
796 // Message shares extension method resolving with GPBRootObject so insure
797 // it is started up at the same time.
798 (void)[GPBRootObject class];
799 } else if ([self superclass] == pbMessageClass) {
800 // This is here to start up all the "message" subclasses. Just needs to be
801 // done for the messages, not any of the subclasses.
802 // This must be done in initialize to enforce thread safety of start up of
803 // the protocol buffer library.
804 // Note: The generated code for -descriptor calls
805 // +[GPBDescriptor allocDescriptorForClass:...], passing the GPBRootObject
806 // subclass for the file. That call chain is what ensures that *Root class
807 // is started up to support extension resolution off the message class
808 // (+resolveClassMethod: below) in a thread safe manner.
809 [self descriptor];
810 }
811}
812
813+ (instancetype)allocWithZone:(NSZone *)zone {
814 // Override alloc to allocate our classes with the additional storage
815 // required for the instance variables.
816 GPBDescriptor *descriptor = [self descriptor];
817 return NSAllocateObject(self, descriptor->storageSize_, zone);
818}
819
820+ (instancetype)alloc {
821 return [self allocWithZone:nil];
822}
823
824+ (GPBDescriptor *)descriptor {
825 // This is thread safe because it is called from +initialize.
826 static GPBDescriptor *descriptor = NULL;
827 static GPBFileDescriptor *fileDescriptor = NULL;
828 if (!descriptor) {
829 // Use a dummy file that marks it as proto2 syntax so when used generically
830 // it supports unknowns/etc.
831 fileDescriptor =
832 [[GPBFileDescriptor alloc] initWithPackage:@"internal"
833 syntax:GPBFileSyntaxProto2];
834
835 descriptor = [GPBDescriptor allocDescriptorForClass:[GPBMessage class]
836 rootClass:Nil
837 file:fileDescriptor
838 fields:NULL
839 fieldCount:0
Brian Silverman9c614bc2016-02-15 20:20:02 -0500840 storageSize:0
Austin Schuh40c16522018-10-28 20:27:54 -0700841 flags:0];
Brian Silverman9c614bc2016-02-15 20:20:02 -0500842 }
843 return descriptor;
844}
845
846+ (instancetype)message {
847 return [[[self alloc] init] autorelease];
848}
849
850- (instancetype)init {
851 if ((self = [super init])) {
852 messageStorage_ = (GPBMessage_StoragePtr)(
853 ((uint8_t *)self) + class_getInstanceSize([self class]));
Brian Silverman9c614bc2016-02-15 20:20:02 -0500854 }
855
856 return self;
857}
858
859- (instancetype)initWithData:(NSData *)data error:(NSError **)errorPtr {
860 return [self initWithData:data extensionRegistry:nil error:errorPtr];
861}
862
863- (instancetype)initWithData:(NSData *)data
864 extensionRegistry:(GPBExtensionRegistry *)extensionRegistry
865 error:(NSError **)errorPtr {
866 if ((self = [self init])) {
867 @try {
868 [self mergeFromData:data extensionRegistry:extensionRegistry];
869 if (errorPtr) {
870 *errorPtr = nil;
871 }
872 }
873 @catch (NSException *exception) {
874 [self release];
875 self = nil;
876 if (errorPtr) {
Austin Schuh40c16522018-10-28 20:27:54 -0700877 *errorPtr = ErrorFromException(exception);
Brian Silverman9c614bc2016-02-15 20:20:02 -0500878 }
879 }
880#ifdef DEBUG
881 if (self && !self.initialized) {
882 [self release];
883 self = nil;
884 if (errorPtr) {
885 *errorPtr = MessageError(GPBMessageErrorCodeMissingRequiredField, nil);
886 }
887 }
888#endif
889 }
890 return self;
891}
892
893- (instancetype)initWithCodedInputStream:(GPBCodedInputStream *)input
894 extensionRegistry:
895 (GPBExtensionRegistry *)extensionRegistry
896 error:(NSError **)errorPtr {
897 if ((self = [self init])) {
898 @try {
899 [self mergeFromCodedInputStream:input extensionRegistry:extensionRegistry];
900 if (errorPtr) {
901 *errorPtr = nil;
902 }
903 }
904 @catch (NSException *exception) {
905 [self release];
906 self = nil;
907 if (errorPtr) {
Austin Schuh40c16522018-10-28 20:27:54 -0700908 *errorPtr = ErrorFromException(exception);
Brian Silverman9c614bc2016-02-15 20:20:02 -0500909 }
910 }
911#ifdef DEBUG
912 if (self && !self.initialized) {
913 [self release];
914 self = nil;
915 if (errorPtr) {
916 *errorPtr = MessageError(GPBMessageErrorCodeMissingRequiredField, nil);
917 }
918 }
919#endif
920 }
921 return self;
922}
923
924- (void)dealloc {
925 [self internalClear:NO];
926 NSCAssert(!autocreator_, @"Autocreator was not cleared before dealloc.");
Austin Schuh40c16522018-10-28 20:27:54 -0700927 if (readOnlySemaphore_) {
928 dispatch_release(readOnlySemaphore_);
929 }
Brian Silverman9c614bc2016-02-15 20:20:02 -0500930 [super dealloc];
931}
932
933- (void)copyFieldsInto:(GPBMessage *)message
934 zone:(NSZone *)zone
935 descriptor:(GPBDescriptor *)descriptor {
936 // Copy all the storage...
937 memcpy(message->messageStorage_, messageStorage_, descriptor->storageSize_);
938
939 GPBFileSyntax syntax = descriptor.file.syntax;
940
941 // Loop over the fields doing fixup...
942 for (GPBFieldDescriptor *field in descriptor->fields_) {
943 if (GPBFieldIsMapOrArray(field)) {
944 id value = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
945 if (value) {
946 // We need to copy the array/map, but the catch is for message fields,
947 // we also need to ensure all the messages as those need copying also.
948 id newValue;
949 if (GPBFieldDataTypeIsMessage(field)) {
950 if (field.fieldType == GPBFieldTypeRepeated) {
951 NSArray *existingArray = (NSArray *)value;
952 NSMutableArray *newArray =
953 [[NSMutableArray alloc] initWithCapacity:existingArray.count];
954 newValue = newArray;
955 for (GPBMessage *msg in existingArray) {
956 GPBMessage *copiedMsg = [msg copyWithZone:zone];
957 [newArray addObject:copiedMsg];
958 [copiedMsg release];
959 }
960 } else {
961 if (field.mapKeyDataType == GPBDataTypeString) {
962 // Map is an NSDictionary.
963 NSDictionary *existingDict = value;
964 NSMutableDictionary *newDict = [[NSMutableDictionary alloc]
965 initWithCapacity:existingDict.count];
966 newValue = newDict;
967 [existingDict enumerateKeysAndObjectsUsingBlock:^(NSString *key,
968 GPBMessage *msg,
969 BOOL *stop) {
970#pragma unused(stop)
971 GPBMessage *copiedMsg = [msg copyWithZone:zone];
972 [newDict setObject:copiedMsg forKey:key];
973 [copiedMsg release];
974 }];
975 } else {
976 // Is one of the GPB*ObjectDictionary classes. Type doesn't
977 // matter, just need one to invoke the selector.
978 GPBInt32ObjectDictionary *existingDict = value;
979 newValue = [existingDict deepCopyWithZone:zone];
980 }
981 }
982 } else {
983 // Not messages (but is a map/array)...
984 if (field.fieldType == GPBFieldTypeRepeated) {
985 if (GPBFieldDataTypeIsObject(field)) {
986 // NSArray
987 newValue = [value mutableCopyWithZone:zone];
988 } else {
989 // GPB*Array
990 newValue = [value copyWithZone:zone];
991 }
992 } else {
Austin Schuh40c16522018-10-28 20:27:54 -0700993 if ((field.mapKeyDataType == GPBDataTypeString) &&
994 GPBFieldDataTypeIsObject(field)) {
Brian Silverman9c614bc2016-02-15 20:20:02 -0500995 // NSDictionary
996 newValue = [value mutableCopyWithZone:zone];
997 } else {
998 // Is one of the GPB*Dictionary classes. Type doesn't matter,
999 // just need one to invoke the selector.
1000 GPBInt32Int32Dictionary *existingDict = value;
1001 newValue = [existingDict copyWithZone:zone];
1002 }
1003 }
1004 }
1005 // We retain here because the memcpy picked up the pointer value and
1006 // the next call to SetRetainedObject... will release the current value.
1007 [value retain];
1008 GPBSetRetainedObjectIvarWithFieldInternal(message, field, newValue,
1009 syntax);
1010 }
1011 } else if (GPBFieldDataTypeIsMessage(field)) {
1012 // For object types, if we have a value, copy it. If we don't,
1013 // zero it to remove the pointer to something that was autocreated
1014 // (and the ptr just got memcpyed).
1015 if (GPBGetHasIvarField(self, field)) {
1016 GPBMessage *value = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
1017 GPBMessage *newValue = [value copyWithZone:zone];
1018 // We retain here because the memcpy picked up the pointer value and
1019 // the next call to SetRetainedObject... will release the current value.
1020 [value retain];
1021 GPBSetRetainedObjectIvarWithFieldInternal(message, field, newValue,
1022 syntax);
1023 } else {
1024 uint8_t *storage = (uint8_t *)message->messageStorage_;
1025 id *typePtr = (id *)&storage[field->description_->offset];
1026 *typePtr = NULL;
1027 }
1028 } else if (GPBFieldDataTypeIsObject(field) &&
1029 GPBGetHasIvarField(self, field)) {
1030 // A set string/data value (message picked off above), copy it.
1031 id value = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
1032 id newValue = [value copyWithZone:zone];
1033 // We retain here because the memcpy picked up the pointer value and
1034 // the next call to SetRetainedObject... will release the current value.
1035 [value retain];
1036 GPBSetRetainedObjectIvarWithFieldInternal(message, field, newValue,
1037 syntax);
1038 } else {
1039 // memcpy took care of the rest of the primitive fields if they were set.
1040 }
1041 } // for (field in descriptor->fields_)
1042}
1043
1044- (id)copyWithZone:(NSZone *)zone {
1045 GPBDescriptor *descriptor = [self descriptor];
1046 GPBMessage *result = [[descriptor.messageClass allocWithZone:zone] init];
1047
1048 [self copyFieldsInto:result zone:zone descriptor:descriptor];
1049 // Make immutable copies of the extra bits.
1050 result->unknownFields_ = [unknownFields_ copyWithZone:zone];
1051 result->extensionMap_ = CloneExtensionMap(extensionMap_, zone);
1052 return result;
1053}
1054
1055- (void)clear {
1056 [self internalClear:YES];
1057}
1058
1059- (void)internalClear:(BOOL)zeroStorage {
1060 GPBDescriptor *descriptor = [self descriptor];
1061 for (GPBFieldDescriptor *field in descriptor->fields_) {
1062 if (GPBFieldIsMapOrArray(field)) {
1063 id arrayOrMap = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
1064 if (arrayOrMap) {
1065 if (field.fieldType == GPBFieldTypeRepeated) {
1066 if (GPBFieldDataTypeIsObject(field)) {
Austin Schuh40c16522018-10-28 20:27:54 -07001067 if ([arrayOrMap isKindOfClass:[GPBAutocreatedArray class]]) {
1068 GPBAutocreatedArray *autoArray = arrayOrMap;
1069 if (autoArray->_autocreator == self) {
1070 autoArray->_autocreator = nil;
1071 }
Brian Silverman9c614bc2016-02-15 20:20:02 -05001072 }
1073 } else {
1074 // Type doesn't matter, it is a GPB*Array.
1075 GPBInt32Array *gpbArray = arrayOrMap;
1076 if (gpbArray->_autocreator == self) {
1077 gpbArray->_autocreator = nil;
1078 }
1079 }
1080 } else {
1081 if ((field.mapKeyDataType == GPBDataTypeString) &&
1082 GPBFieldDataTypeIsObject(field)) {
Austin Schuh40c16522018-10-28 20:27:54 -07001083 if ([arrayOrMap isKindOfClass:[GPBAutocreatedDictionary class]]) {
1084 GPBAutocreatedDictionary *autoDict = arrayOrMap;
1085 if (autoDict->_autocreator == self) {
1086 autoDict->_autocreator = nil;
1087 }
Brian Silverman9c614bc2016-02-15 20:20:02 -05001088 }
1089 } else {
1090 // Type doesn't matter, it is a GPB*Dictionary.
1091 GPBInt32Int32Dictionary *gpbDict = arrayOrMap;
1092 if (gpbDict->_autocreator == self) {
1093 gpbDict->_autocreator = nil;
1094 }
1095 }
1096 }
1097 [arrayOrMap release];
1098 }
1099 } else if (GPBFieldDataTypeIsMessage(field)) {
1100 GPBClearAutocreatedMessageIvarWithField(self, field);
1101 GPBMessage *value = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
1102 [value release];
1103 } else if (GPBFieldDataTypeIsObject(field) &&
1104 GPBGetHasIvarField(self, field)) {
1105 id value = GPBGetObjectIvarWithField(self, field);
1106 [value release];
1107 }
1108 }
1109
1110 // GPBClearMessageAutocreator() expects that its caller has already been
1111 // removed from autocreatedExtensionMap_ so we set to nil first.
1112 NSArray *autocreatedValues = [autocreatedExtensionMap_ allValues];
1113 [autocreatedExtensionMap_ release];
1114 autocreatedExtensionMap_ = nil;
1115
1116 // Since we're clearing all of our extensions, make sure that we clear the
1117 // autocreator on any that we've created so they no longer refer to us.
1118 for (GPBMessage *value in autocreatedValues) {
1119 NSCAssert(GPBWasMessageAutocreatedBy(value, self),
1120 @"Autocreated extension does not refer back to self.");
1121 GPBClearMessageAutocreator(value);
1122 }
1123
1124 [extensionMap_ release];
1125 extensionMap_ = nil;
1126 [unknownFields_ release];
1127 unknownFields_ = nil;
1128
1129 // Note that clearing does not affect autocreator_. If we are being cleared
1130 // because of a dealloc, then autocreator_ should be nil anyway. If we are
1131 // being cleared because someone explicitly clears us, we don't want to
1132 // sever our relationship with our autocreator.
1133
1134 if (zeroStorage) {
1135 memset(messageStorage_, 0, descriptor->storageSize_);
1136 }
1137}
1138
1139- (BOOL)isInitialized {
1140 GPBDescriptor *descriptor = [self descriptor];
1141 for (GPBFieldDescriptor *field in descriptor->fields_) {
1142 if (field.isRequired) {
1143 if (!GPBGetHasIvarField(self, field)) {
1144 return NO;
1145 }
1146 }
1147 if (GPBFieldDataTypeIsMessage(field)) {
1148 GPBFieldType fieldType = field.fieldType;
1149 if (fieldType == GPBFieldTypeSingle) {
1150 if (field.isRequired) {
1151 GPBMessage *message = GPBGetMessageMessageField(self, field);
1152 if (!message.initialized) {
1153 return NO;
1154 }
1155 } else {
1156 NSAssert(field.isOptional,
1157 @"%@: Single message field %@ not required or optional?",
1158 [self class], field.name);
1159 if (GPBGetHasIvarField(self, field)) {
1160 GPBMessage *message = GPBGetMessageMessageField(self, field);
1161 if (!message.initialized) {
1162 return NO;
1163 }
1164 }
1165 }
1166 } else if (fieldType == GPBFieldTypeRepeated) {
1167 NSArray *array = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
1168 for (GPBMessage *message in array) {
1169 if (!message.initialized) {
1170 return NO;
1171 }
1172 }
1173 } else { // fieldType == GPBFieldTypeMap
1174 if (field.mapKeyDataType == GPBDataTypeString) {
1175 NSDictionary *map =
1176 GPBGetObjectIvarWithFieldNoAutocreate(self, field);
1177 if (map && !GPBDictionaryIsInitializedInternalHelper(map, field)) {
1178 return NO;
1179 }
1180 } else {
1181 // Real type is GPB*ObjectDictionary, exact type doesn't matter.
1182 GPBInt32ObjectDictionary *map =
1183 GPBGetObjectIvarWithFieldNoAutocreate(self, field);
1184 if (map && ![map isInitialized]) {
1185 return NO;
1186 }
1187 }
1188 }
1189 }
1190 }
1191
1192 __block BOOL result = YES;
1193 [extensionMap_
1194 enumerateKeysAndObjectsUsingBlock:^(GPBExtensionDescriptor *extension,
1195 id obj,
1196 BOOL *stop) {
1197 if (GPBExtensionIsMessage(extension)) {
1198 if (extension.isRepeated) {
1199 for (GPBMessage *msg in obj) {
1200 if (!msg.initialized) {
1201 result = NO;
1202 *stop = YES;
1203 break;
1204 }
1205 }
1206 } else {
1207 GPBMessage *asMsg = obj;
1208 if (!asMsg.initialized) {
1209 result = NO;
1210 *stop = YES;
1211 }
1212 }
1213 }
1214 }];
1215 return result;
1216}
1217
1218- (GPBDescriptor *)descriptor {
1219 return [[self class] descriptor];
1220}
1221
1222- (NSData *)data {
1223#ifdef DEBUG
1224 if (!self.initialized) {
1225 return nil;
1226 }
1227#endif
1228 NSMutableData *data = [NSMutableData dataWithLength:[self serializedSize]];
1229 GPBCodedOutputStream *stream =
1230 [[GPBCodedOutputStream alloc] initWithData:data];
1231 @try {
1232 [self writeToCodedOutputStream:stream];
1233 }
1234 @catch (NSException *exception) {
1235 // This really shouldn't happen. The only way writeToCodedOutputStream:
1236 // could throw is if something in the library has a bug and the
1237 // serializedSize was wrong.
1238#ifdef DEBUG
1239 NSLog(@"%@: Internal exception while building message data: %@",
1240 [self class], exception);
1241#endif
1242 data = nil;
1243 }
1244 [stream release];
1245 return data;
1246}
1247
1248- (NSData *)delimitedData {
1249 size_t serializedSize = [self serializedSize];
1250 size_t varintSize = GPBComputeRawVarint32SizeForInteger(serializedSize);
1251 NSMutableData *data =
1252 [NSMutableData dataWithLength:(serializedSize + varintSize)];
1253 GPBCodedOutputStream *stream =
1254 [[GPBCodedOutputStream alloc] initWithData:data];
1255 @try {
1256 [self writeDelimitedToCodedOutputStream:stream];
1257 }
1258 @catch (NSException *exception) {
1259 // This really shouldn't happen. The only way writeToCodedOutputStream:
1260 // could throw is if something in the library has a bug and the
1261 // serializedSize was wrong.
1262#ifdef DEBUG
1263 NSLog(@"%@: Internal exception while building message delimitedData: %@",
1264 [self class], exception);
1265#endif
1266 // If it happens, truncate.
1267 data.length = 0;
1268 }
1269 [stream release];
1270 return data;
1271}
1272
1273- (void)writeToOutputStream:(NSOutputStream *)output {
1274 GPBCodedOutputStream *stream =
1275 [[GPBCodedOutputStream alloc] initWithOutputStream:output];
1276 [self writeToCodedOutputStream:stream];
1277 [stream release];
1278}
1279
1280- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)output {
1281 GPBDescriptor *descriptor = [self descriptor];
1282 NSArray *fieldsArray = descriptor->fields_;
1283 NSUInteger fieldCount = fieldsArray.count;
1284 const GPBExtensionRange *extensionRanges = descriptor.extensionRanges;
1285 NSUInteger extensionRangesCount = descriptor.extensionRangesCount;
1286 for (NSUInteger i = 0, j = 0; i < fieldCount || j < extensionRangesCount;) {
1287 if (i == fieldCount) {
1288 [self writeExtensionsToCodedOutputStream:output
1289 range:extensionRanges[j++]];
1290 } else if (j == extensionRangesCount ||
1291 GPBFieldNumber(fieldsArray[i]) < extensionRanges[j].start) {
1292 [self writeField:fieldsArray[i++] toCodedOutputStream:output];
1293 } else {
1294 [self writeExtensionsToCodedOutputStream:output
1295 range:extensionRanges[j++]];
1296 }
1297 }
1298 if (descriptor.isWireFormat) {
1299 [unknownFields_ writeAsMessageSetTo:output];
1300 } else {
1301 [unknownFields_ writeToCodedOutputStream:output];
1302 }
1303}
1304
1305- (void)writeDelimitedToOutputStream:(NSOutputStream *)output {
1306 GPBCodedOutputStream *codedOutput =
1307 [[GPBCodedOutputStream alloc] initWithOutputStream:output];
1308 [self writeDelimitedToCodedOutputStream:codedOutput];
1309 [codedOutput release];
1310}
1311
1312- (void)writeDelimitedToCodedOutputStream:(GPBCodedOutputStream *)output {
1313 [output writeRawVarintSizeTAs32:[self serializedSize]];
1314 [self writeToCodedOutputStream:output];
1315}
1316
1317- (void)writeField:(GPBFieldDescriptor *)field
1318 toCodedOutputStream:(GPBCodedOutputStream *)output {
1319 GPBFieldType fieldType = field.fieldType;
1320 if (fieldType == GPBFieldTypeSingle) {
1321 BOOL has = GPBGetHasIvarField(self, field);
1322 if (!has) {
1323 return;
1324 }
1325 }
1326 uint32_t fieldNumber = GPBFieldNumber(field);
1327
1328//%PDDM-DEFINE FIELD_CASE(TYPE, REAL_TYPE)
1329//%FIELD_CASE_FULL(TYPE, REAL_TYPE, REAL_TYPE)
1330//%PDDM-DEFINE FIELD_CASE_FULL(TYPE, REAL_TYPE, ARRAY_TYPE)
1331//% case GPBDataType##TYPE:
1332//% if (fieldType == GPBFieldTypeRepeated) {
1333//% uint32_t tag = field.isPackable ? GPBFieldTag(field) : 0;
1334//% GPB##ARRAY_TYPE##Array *array =
1335//% GPBGetObjectIvarWithFieldNoAutocreate(self, field);
1336//% [output write##TYPE##Array:fieldNumber values:array tag:tag];
1337//% } else if (fieldType == GPBFieldTypeSingle) {
1338//% [output write##TYPE:fieldNumber
1339//% TYPE$S value:GPBGetMessage##REAL_TYPE##Field(self, field)];
1340//% } else { // fieldType == GPBFieldTypeMap
1341//% // Exact type here doesn't matter.
1342//% GPBInt32##ARRAY_TYPE##Dictionary *dict =
1343//% GPBGetObjectIvarWithFieldNoAutocreate(self, field);
1344//% [dict writeToCodedOutputStream:output asField:field];
1345//% }
1346//% break;
1347//%
1348//%PDDM-DEFINE FIELD_CASE2(TYPE)
1349//% case GPBDataType##TYPE:
1350//% if (fieldType == GPBFieldTypeRepeated) {
1351//% NSArray *array = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
1352//% [output write##TYPE##Array:fieldNumber values:array];
1353//% } else if (fieldType == GPBFieldTypeSingle) {
1354//% // GPBGetObjectIvarWithFieldNoAutocreate() avoids doing the has check
1355//% // again.
1356//% [output write##TYPE:fieldNumber
1357//% TYPE$S value:GPBGetObjectIvarWithFieldNoAutocreate(self, field)];
1358//% } else { // fieldType == GPBFieldTypeMap
1359//% // Exact type here doesn't matter.
1360//% id dict = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
1361//% GPBDataType mapKeyDataType = field.mapKeyDataType;
1362//% if (mapKeyDataType == GPBDataTypeString) {
1363//% GPBDictionaryWriteToStreamInternalHelper(output, dict, field);
1364//% } else {
1365//% [dict writeToCodedOutputStream:output asField:field];
1366//% }
1367//% }
1368//% break;
1369//%
1370
1371 switch (GPBGetFieldDataType(field)) {
1372
1373//%PDDM-EXPAND FIELD_CASE(Bool, Bool)
1374// This block of code is generated, do not edit it directly.
1375
1376 case GPBDataTypeBool:
1377 if (fieldType == GPBFieldTypeRepeated) {
1378 uint32_t tag = field.isPackable ? GPBFieldTag(field) : 0;
1379 GPBBoolArray *array =
1380 GPBGetObjectIvarWithFieldNoAutocreate(self, field);
1381 [output writeBoolArray:fieldNumber values:array tag:tag];
1382 } else if (fieldType == GPBFieldTypeSingle) {
1383 [output writeBool:fieldNumber
1384 value:GPBGetMessageBoolField(self, field)];
1385 } else { // fieldType == GPBFieldTypeMap
1386 // Exact type here doesn't matter.
1387 GPBInt32BoolDictionary *dict =
1388 GPBGetObjectIvarWithFieldNoAutocreate(self, field);
1389 [dict writeToCodedOutputStream:output asField:field];
1390 }
1391 break;
1392
1393//%PDDM-EXPAND FIELD_CASE(Fixed32, UInt32)
1394// This block of code is generated, do not edit it directly.
1395
1396 case GPBDataTypeFixed32:
1397 if (fieldType == GPBFieldTypeRepeated) {
1398 uint32_t tag = field.isPackable ? GPBFieldTag(field) : 0;
1399 GPBUInt32Array *array =
1400 GPBGetObjectIvarWithFieldNoAutocreate(self, field);
1401 [output writeFixed32Array:fieldNumber values:array tag:tag];
1402 } else if (fieldType == GPBFieldTypeSingle) {
1403 [output writeFixed32:fieldNumber
1404 value:GPBGetMessageUInt32Field(self, field)];
1405 } else { // fieldType == GPBFieldTypeMap
1406 // Exact type here doesn't matter.
1407 GPBInt32UInt32Dictionary *dict =
1408 GPBGetObjectIvarWithFieldNoAutocreate(self, field);
1409 [dict writeToCodedOutputStream:output asField:field];
1410 }
1411 break;
1412
1413//%PDDM-EXPAND FIELD_CASE(SFixed32, Int32)
1414// This block of code is generated, do not edit it directly.
1415
1416 case GPBDataTypeSFixed32:
1417 if (fieldType == GPBFieldTypeRepeated) {
1418 uint32_t tag = field.isPackable ? GPBFieldTag(field) : 0;
1419 GPBInt32Array *array =
1420 GPBGetObjectIvarWithFieldNoAutocreate(self, field);
1421 [output writeSFixed32Array:fieldNumber values:array tag:tag];
1422 } else if (fieldType == GPBFieldTypeSingle) {
1423 [output writeSFixed32:fieldNumber
1424 value:GPBGetMessageInt32Field(self, field)];
1425 } else { // fieldType == GPBFieldTypeMap
1426 // Exact type here doesn't matter.
1427 GPBInt32Int32Dictionary *dict =
1428 GPBGetObjectIvarWithFieldNoAutocreate(self, field);
1429 [dict writeToCodedOutputStream:output asField:field];
1430 }
1431 break;
1432
1433//%PDDM-EXPAND FIELD_CASE(Float, Float)
1434// This block of code is generated, do not edit it directly.
1435
1436 case GPBDataTypeFloat:
1437 if (fieldType == GPBFieldTypeRepeated) {
1438 uint32_t tag = field.isPackable ? GPBFieldTag(field) : 0;
1439 GPBFloatArray *array =
1440 GPBGetObjectIvarWithFieldNoAutocreate(self, field);
1441 [output writeFloatArray:fieldNumber values:array tag:tag];
1442 } else if (fieldType == GPBFieldTypeSingle) {
1443 [output writeFloat:fieldNumber
1444 value:GPBGetMessageFloatField(self, field)];
1445 } else { // fieldType == GPBFieldTypeMap
1446 // Exact type here doesn't matter.
1447 GPBInt32FloatDictionary *dict =
1448 GPBGetObjectIvarWithFieldNoAutocreate(self, field);
1449 [dict writeToCodedOutputStream:output asField:field];
1450 }
1451 break;
1452
1453//%PDDM-EXPAND FIELD_CASE(Fixed64, UInt64)
1454// This block of code is generated, do not edit it directly.
1455
1456 case GPBDataTypeFixed64:
1457 if (fieldType == GPBFieldTypeRepeated) {
1458 uint32_t tag = field.isPackable ? GPBFieldTag(field) : 0;
1459 GPBUInt64Array *array =
1460 GPBGetObjectIvarWithFieldNoAutocreate(self, field);
1461 [output writeFixed64Array:fieldNumber values:array tag:tag];
1462 } else if (fieldType == GPBFieldTypeSingle) {
1463 [output writeFixed64:fieldNumber
1464 value:GPBGetMessageUInt64Field(self, field)];
1465 } else { // fieldType == GPBFieldTypeMap
1466 // Exact type here doesn't matter.
1467 GPBInt32UInt64Dictionary *dict =
1468 GPBGetObjectIvarWithFieldNoAutocreate(self, field);
1469 [dict writeToCodedOutputStream:output asField:field];
1470 }
1471 break;
1472
1473//%PDDM-EXPAND FIELD_CASE(SFixed64, Int64)
1474// This block of code is generated, do not edit it directly.
1475
1476 case GPBDataTypeSFixed64:
1477 if (fieldType == GPBFieldTypeRepeated) {
1478 uint32_t tag = field.isPackable ? GPBFieldTag(field) : 0;
1479 GPBInt64Array *array =
1480 GPBGetObjectIvarWithFieldNoAutocreate(self, field);
1481 [output writeSFixed64Array:fieldNumber values:array tag:tag];
1482 } else if (fieldType == GPBFieldTypeSingle) {
1483 [output writeSFixed64:fieldNumber
1484 value:GPBGetMessageInt64Field(self, field)];
1485 } else { // fieldType == GPBFieldTypeMap
1486 // Exact type here doesn't matter.
1487 GPBInt32Int64Dictionary *dict =
1488 GPBGetObjectIvarWithFieldNoAutocreate(self, field);
1489 [dict writeToCodedOutputStream:output asField:field];
1490 }
1491 break;
1492
1493//%PDDM-EXPAND FIELD_CASE(Double, Double)
1494// This block of code is generated, do not edit it directly.
1495
1496 case GPBDataTypeDouble:
1497 if (fieldType == GPBFieldTypeRepeated) {
1498 uint32_t tag = field.isPackable ? GPBFieldTag(field) : 0;
1499 GPBDoubleArray *array =
1500 GPBGetObjectIvarWithFieldNoAutocreate(self, field);
1501 [output writeDoubleArray:fieldNumber values:array tag:tag];
1502 } else if (fieldType == GPBFieldTypeSingle) {
1503 [output writeDouble:fieldNumber
1504 value:GPBGetMessageDoubleField(self, field)];
1505 } else { // fieldType == GPBFieldTypeMap
1506 // Exact type here doesn't matter.
1507 GPBInt32DoubleDictionary *dict =
1508 GPBGetObjectIvarWithFieldNoAutocreate(self, field);
1509 [dict writeToCodedOutputStream:output asField:field];
1510 }
1511 break;
1512
1513//%PDDM-EXPAND FIELD_CASE(Int32, Int32)
1514// This block of code is generated, do not edit it directly.
1515
1516 case GPBDataTypeInt32:
1517 if (fieldType == GPBFieldTypeRepeated) {
1518 uint32_t tag = field.isPackable ? GPBFieldTag(field) : 0;
1519 GPBInt32Array *array =
1520 GPBGetObjectIvarWithFieldNoAutocreate(self, field);
1521 [output writeInt32Array:fieldNumber values:array tag:tag];
1522 } else if (fieldType == GPBFieldTypeSingle) {
1523 [output writeInt32:fieldNumber
1524 value:GPBGetMessageInt32Field(self, field)];
1525 } else { // fieldType == GPBFieldTypeMap
1526 // Exact type here doesn't matter.
1527 GPBInt32Int32Dictionary *dict =
1528 GPBGetObjectIvarWithFieldNoAutocreate(self, field);
1529 [dict writeToCodedOutputStream:output asField:field];
1530 }
1531 break;
1532
1533//%PDDM-EXPAND FIELD_CASE(Int64, Int64)
1534// This block of code is generated, do not edit it directly.
1535
1536 case GPBDataTypeInt64:
1537 if (fieldType == GPBFieldTypeRepeated) {
1538 uint32_t tag = field.isPackable ? GPBFieldTag(field) : 0;
1539 GPBInt64Array *array =
1540 GPBGetObjectIvarWithFieldNoAutocreate(self, field);
1541 [output writeInt64Array:fieldNumber values:array tag:tag];
1542 } else if (fieldType == GPBFieldTypeSingle) {
1543 [output writeInt64:fieldNumber
1544 value:GPBGetMessageInt64Field(self, field)];
1545 } else { // fieldType == GPBFieldTypeMap
1546 // Exact type here doesn't matter.
1547 GPBInt32Int64Dictionary *dict =
1548 GPBGetObjectIvarWithFieldNoAutocreate(self, field);
1549 [dict writeToCodedOutputStream:output asField:field];
1550 }
1551 break;
1552
1553//%PDDM-EXPAND FIELD_CASE(SInt32, Int32)
1554// This block of code is generated, do not edit it directly.
1555
1556 case GPBDataTypeSInt32:
1557 if (fieldType == GPBFieldTypeRepeated) {
1558 uint32_t tag = field.isPackable ? GPBFieldTag(field) : 0;
1559 GPBInt32Array *array =
1560 GPBGetObjectIvarWithFieldNoAutocreate(self, field);
1561 [output writeSInt32Array:fieldNumber values:array tag:tag];
1562 } else if (fieldType == GPBFieldTypeSingle) {
1563 [output writeSInt32:fieldNumber
1564 value:GPBGetMessageInt32Field(self, field)];
1565 } else { // fieldType == GPBFieldTypeMap
1566 // Exact type here doesn't matter.
1567 GPBInt32Int32Dictionary *dict =
1568 GPBGetObjectIvarWithFieldNoAutocreate(self, field);
1569 [dict writeToCodedOutputStream:output asField:field];
1570 }
1571 break;
1572
1573//%PDDM-EXPAND FIELD_CASE(SInt64, Int64)
1574// This block of code is generated, do not edit it directly.
1575
1576 case GPBDataTypeSInt64:
1577 if (fieldType == GPBFieldTypeRepeated) {
1578 uint32_t tag = field.isPackable ? GPBFieldTag(field) : 0;
1579 GPBInt64Array *array =
1580 GPBGetObjectIvarWithFieldNoAutocreate(self, field);
1581 [output writeSInt64Array:fieldNumber values:array tag:tag];
1582 } else if (fieldType == GPBFieldTypeSingle) {
1583 [output writeSInt64:fieldNumber
1584 value:GPBGetMessageInt64Field(self, field)];
1585 } else { // fieldType == GPBFieldTypeMap
1586 // Exact type here doesn't matter.
1587 GPBInt32Int64Dictionary *dict =
1588 GPBGetObjectIvarWithFieldNoAutocreate(self, field);
1589 [dict writeToCodedOutputStream:output asField:field];
1590 }
1591 break;
1592
1593//%PDDM-EXPAND FIELD_CASE(UInt32, UInt32)
1594// This block of code is generated, do not edit it directly.
1595
1596 case GPBDataTypeUInt32:
1597 if (fieldType == GPBFieldTypeRepeated) {
1598 uint32_t tag = field.isPackable ? GPBFieldTag(field) : 0;
1599 GPBUInt32Array *array =
1600 GPBGetObjectIvarWithFieldNoAutocreate(self, field);
1601 [output writeUInt32Array:fieldNumber values:array tag:tag];
1602 } else if (fieldType == GPBFieldTypeSingle) {
1603 [output writeUInt32:fieldNumber
1604 value:GPBGetMessageUInt32Field(self, field)];
1605 } else { // fieldType == GPBFieldTypeMap
1606 // Exact type here doesn't matter.
1607 GPBInt32UInt32Dictionary *dict =
1608 GPBGetObjectIvarWithFieldNoAutocreate(self, field);
1609 [dict writeToCodedOutputStream:output asField:field];
1610 }
1611 break;
1612
1613//%PDDM-EXPAND FIELD_CASE(UInt64, UInt64)
1614// This block of code is generated, do not edit it directly.
1615
1616 case GPBDataTypeUInt64:
1617 if (fieldType == GPBFieldTypeRepeated) {
1618 uint32_t tag = field.isPackable ? GPBFieldTag(field) : 0;
1619 GPBUInt64Array *array =
1620 GPBGetObjectIvarWithFieldNoAutocreate(self, field);
1621 [output writeUInt64Array:fieldNumber values:array tag:tag];
1622 } else if (fieldType == GPBFieldTypeSingle) {
1623 [output writeUInt64:fieldNumber
1624 value:GPBGetMessageUInt64Field(self, field)];
1625 } else { // fieldType == GPBFieldTypeMap
1626 // Exact type here doesn't matter.
1627 GPBInt32UInt64Dictionary *dict =
1628 GPBGetObjectIvarWithFieldNoAutocreate(self, field);
1629 [dict writeToCodedOutputStream:output asField:field];
1630 }
1631 break;
1632
1633//%PDDM-EXPAND FIELD_CASE_FULL(Enum, Int32, Enum)
1634// This block of code is generated, do not edit it directly.
1635
1636 case GPBDataTypeEnum:
1637 if (fieldType == GPBFieldTypeRepeated) {
1638 uint32_t tag = field.isPackable ? GPBFieldTag(field) : 0;
1639 GPBEnumArray *array =
1640 GPBGetObjectIvarWithFieldNoAutocreate(self, field);
1641 [output writeEnumArray:fieldNumber values:array tag:tag];
1642 } else if (fieldType == GPBFieldTypeSingle) {
1643 [output writeEnum:fieldNumber
1644 value:GPBGetMessageInt32Field(self, field)];
1645 } else { // fieldType == GPBFieldTypeMap
1646 // Exact type here doesn't matter.
1647 GPBInt32EnumDictionary *dict =
1648 GPBGetObjectIvarWithFieldNoAutocreate(self, field);
1649 [dict writeToCodedOutputStream:output asField:field];
1650 }
1651 break;
1652
1653//%PDDM-EXPAND FIELD_CASE2(Bytes)
1654// This block of code is generated, do not edit it directly.
1655
1656 case GPBDataTypeBytes:
1657 if (fieldType == GPBFieldTypeRepeated) {
1658 NSArray *array = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
1659 [output writeBytesArray:fieldNumber values:array];
1660 } else if (fieldType == GPBFieldTypeSingle) {
1661 // GPBGetObjectIvarWithFieldNoAutocreate() avoids doing the has check
1662 // again.
1663 [output writeBytes:fieldNumber
1664 value:GPBGetObjectIvarWithFieldNoAutocreate(self, field)];
1665 } else { // fieldType == GPBFieldTypeMap
1666 // Exact type here doesn't matter.
1667 id dict = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
1668 GPBDataType mapKeyDataType = field.mapKeyDataType;
1669 if (mapKeyDataType == GPBDataTypeString) {
1670 GPBDictionaryWriteToStreamInternalHelper(output, dict, field);
1671 } else {
1672 [dict writeToCodedOutputStream:output asField:field];
1673 }
1674 }
1675 break;
1676
1677//%PDDM-EXPAND FIELD_CASE2(String)
1678// This block of code is generated, do not edit it directly.
1679
1680 case GPBDataTypeString:
1681 if (fieldType == GPBFieldTypeRepeated) {
1682 NSArray *array = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
1683 [output writeStringArray:fieldNumber values:array];
1684 } else if (fieldType == GPBFieldTypeSingle) {
1685 // GPBGetObjectIvarWithFieldNoAutocreate() avoids doing the has check
1686 // again.
1687 [output writeString:fieldNumber
1688 value:GPBGetObjectIvarWithFieldNoAutocreate(self, field)];
1689 } else { // fieldType == GPBFieldTypeMap
1690 // Exact type here doesn't matter.
1691 id dict = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
1692 GPBDataType mapKeyDataType = field.mapKeyDataType;
1693 if (mapKeyDataType == GPBDataTypeString) {
1694 GPBDictionaryWriteToStreamInternalHelper(output, dict, field);
1695 } else {
1696 [dict writeToCodedOutputStream:output asField:field];
1697 }
1698 }
1699 break;
1700
1701//%PDDM-EXPAND FIELD_CASE2(Message)
1702// This block of code is generated, do not edit it directly.
1703
1704 case GPBDataTypeMessage:
1705 if (fieldType == GPBFieldTypeRepeated) {
1706 NSArray *array = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
1707 [output writeMessageArray:fieldNumber values:array];
1708 } else if (fieldType == GPBFieldTypeSingle) {
1709 // GPBGetObjectIvarWithFieldNoAutocreate() avoids doing the has check
1710 // again.
1711 [output writeMessage:fieldNumber
1712 value:GPBGetObjectIvarWithFieldNoAutocreate(self, field)];
1713 } else { // fieldType == GPBFieldTypeMap
1714 // Exact type here doesn't matter.
1715 id dict = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
1716 GPBDataType mapKeyDataType = field.mapKeyDataType;
1717 if (mapKeyDataType == GPBDataTypeString) {
1718 GPBDictionaryWriteToStreamInternalHelper(output, dict, field);
1719 } else {
1720 [dict writeToCodedOutputStream:output asField:field];
1721 }
1722 }
1723 break;
1724
1725//%PDDM-EXPAND FIELD_CASE2(Group)
1726// This block of code is generated, do not edit it directly.
1727
1728 case GPBDataTypeGroup:
1729 if (fieldType == GPBFieldTypeRepeated) {
1730 NSArray *array = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
1731 [output writeGroupArray:fieldNumber values:array];
1732 } else if (fieldType == GPBFieldTypeSingle) {
1733 // GPBGetObjectIvarWithFieldNoAutocreate() avoids doing the has check
1734 // again.
1735 [output writeGroup:fieldNumber
1736 value:GPBGetObjectIvarWithFieldNoAutocreate(self, field)];
1737 } else { // fieldType == GPBFieldTypeMap
1738 // Exact type here doesn't matter.
1739 id dict = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
1740 GPBDataType mapKeyDataType = field.mapKeyDataType;
1741 if (mapKeyDataType == GPBDataTypeString) {
1742 GPBDictionaryWriteToStreamInternalHelper(output, dict, field);
1743 } else {
1744 [dict writeToCodedOutputStream:output asField:field];
1745 }
1746 }
1747 break;
1748
1749//%PDDM-EXPAND-END (18 expansions)
1750 }
1751}
1752
1753#pragma mark - Extensions
1754
1755- (id)getExtension:(GPBExtensionDescriptor *)extension {
1756 CheckExtension(self, extension);
1757 id value = [extensionMap_ objectForKey:extension];
1758 if (value != nil) {
1759 return value;
1760 }
1761
1762 // No default for repeated.
1763 if (extension.isRepeated) {
1764 return nil;
1765 }
1766 // Non messages get their default.
1767 if (!GPBExtensionIsMessage(extension)) {
1768 return extension.defaultValue;
1769 }
1770
1771 // Check for an autocreated value.
Austin Schuh40c16522018-10-28 20:27:54 -07001772 GPBPrepareReadOnlySemaphore(self);
Brian Silverman9c614bc2016-02-15 20:20:02 -05001773 dispatch_semaphore_wait(readOnlySemaphore_, DISPATCH_TIME_FOREVER);
1774 value = [autocreatedExtensionMap_ objectForKey:extension];
1775 if (!value) {
1776 // Auto create the message extensions to match normal fields.
1777 value = CreateMessageWithAutocreatorForExtension(extension.msgClass, self,
1778 extension);
1779
1780 if (autocreatedExtensionMap_ == nil) {
1781 autocreatedExtensionMap_ = [[NSMutableDictionary alloc] init];
1782 }
1783
1784 // We can't simply call setExtension here because that would clear the new
1785 // value's autocreator.
1786 [autocreatedExtensionMap_ setObject:value forKey:extension];
1787 [value release];
1788 }
1789
1790 dispatch_semaphore_signal(readOnlySemaphore_);
1791 return value;
1792}
1793
1794- (id)getExistingExtension:(GPBExtensionDescriptor *)extension {
1795 // This is an internal method so we don't need to call CheckExtension().
1796 return [extensionMap_ objectForKey:extension];
1797}
1798
1799- (BOOL)hasExtension:(GPBExtensionDescriptor *)extension {
Austin Schuh40c16522018-10-28 20:27:54 -07001800#if defined(DEBUG) && DEBUG
Brian Silverman9c614bc2016-02-15 20:20:02 -05001801 CheckExtension(self, extension);
1802#endif // DEBUG
1803 return nil != [extensionMap_ objectForKey:extension];
1804}
1805
1806- (NSArray *)extensionsCurrentlySet {
1807 return [extensionMap_ allKeys];
1808}
1809
1810- (void)writeExtensionsToCodedOutputStream:(GPBCodedOutputStream *)output
1811 range:(GPBExtensionRange)range {
1812 NSArray *sortedExtensions = [[extensionMap_ allKeys]
1813 sortedArrayUsingSelector:@selector(compareByFieldNumber:)];
1814 uint32_t start = range.start;
1815 uint32_t end = range.end;
1816 for (GPBExtensionDescriptor *extension in sortedExtensions) {
1817 uint32_t fieldNumber = extension.fieldNumber;
1818 if (fieldNumber >= start && fieldNumber < end) {
1819 id value = [extensionMap_ objectForKey:extension];
1820 GPBWriteExtensionValueToOutputStream(extension, value, output);
1821 }
1822 }
1823}
1824
Brian Silverman9c614bc2016-02-15 20:20:02 -05001825- (void)setExtension:(GPBExtensionDescriptor *)extension value:(id)value {
1826 if (!value) {
1827 [self clearExtension:extension];
1828 return;
1829 }
1830
1831 CheckExtension(self, extension);
1832
1833 if (extension.repeated) {
1834 [NSException raise:NSInvalidArgumentException
1835 format:@"Must call addExtension() for repeated types."];
1836 }
1837
1838 if (extensionMap_ == nil) {
1839 extensionMap_ = [[NSMutableDictionary alloc] init];
1840 }
1841
1842 // This pointless cast is for CLANG_WARN_NULLABLE_TO_NONNULL_CONVERSION.
1843 // Without it, the compiler complains we're passing an id nullable when
1844 // setObject:forKey: requires a id nonnull for the value. The check for
1845 // !value at the start of the method ensures it isn't nil, but the check
1846 // isn't smart enough to realize that.
1847 [extensionMap_ setObject:(id)value forKey:extension];
1848
1849 GPBExtensionDescriptor *descriptor = extension;
1850
1851 if (GPBExtensionIsMessage(descriptor) && !descriptor.isRepeated) {
1852 GPBMessage *autocreatedValue =
1853 [[autocreatedExtensionMap_ objectForKey:extension] retain];
1854 // Must remove from the map before calling GPBClearMessageAutocreator() so
1855 // that GPBClearMessageAutocreator() knows its safe to clear.
1856 [autocreatedExtensionMap_ removeObjectForKey:extension];
1857 GPBClearMessageAutocreator(autocreatedValue);
1858 [autocreatedValue release];
1859 }
1860
1861 GPBBecomeVisibleToAutocreator(self);
1862}
1863
1864- (void)addExtension:(GPBExtensionDescriptor *)extension value:(id)value {
1865 CheckExtension(self, extension);
1866
1867 if (!extension.repeated) {
1868 [NSException raise:NSInvalidArgumentException
1869 format:@"Must call setExtension() for singular types."];
1870 }
1871
1872 if (extensionMap_ == nil) {
1873 extensionMap_ = [[NSMutableDictionary alloc] init];
1874 }
1875 NSMutableArray *list = [extensionMap_ objectForKey:extension];
1876 if (list == nil) {
1877 list = [NSMutableArray array];
1878 [extensionMap_ setObject:list forKey:extension];
1879 }
1880
1881 [list addObject:value];
1882 GPBBecomeVisibleToAutocreator(self);
1883}
1884
1885- (void)setExtension:(GPBExtensionDescriptor *)extension
1886 index:(NSUInteger)idx
1887 value:(id)value {
1888 CheckExtension(self, extension);
1889
1890 if (!extension.repeated) {
1891 [NSException raise:NSInvalidArgumentException
1892 format:@"Must call setExtension() for singular types."];
1893 }
1894
1895 if (extensionMap_ == nil) {
1896 extensionMap_ = [[NSMutableDictionary alloc] init];
1897 }
1898
1899 NSMutableArray *list = [extensionMap_ objectForKey:extension];
1900
1901 [list replaceObjectAtIndex:idx withObject:value];
1902 GPBBecomeVisibleToAutocreator(self);
1903}
1904
1905- (void)clearExtension:(GPBExtensionDescriptor *)extension {
1906 CheckExtension(self, extension);
1907
1908 // Only become visible if there was actually a value to clear.
1909 if ([extensionMap_ objectForKey:extension]) {
1910 [extensionMap_ removeObjectForKey:extension];
1911 GPBBecomeVisibleToAutocreator(self);
1912 }
1913}
1914
1915#pragma mark - mergeFrom
1916
1917- (void)mergeFromData:(NSData *)data
1918 extensionRegistry:(GPBExtensionRegistry *)extensionRegistry {
1919 GPBCodedInputStream *input = [[GPBCodedInputStream alloc] initWithData:data];
1920 [self mergeFromCodedInputStream:input extensionRegistry:extensionRegistry];
1921 [input checkLastTagWas:0];
1922 [input release];
1923}
1924
1925#pragma mark - mergeDelimitedFrom
1926
1927- (void)mergeDelimitedFromCodedInputStream:(GPBCodedInputStream *)input
1928 extensionRegistry:(GPBExtensionRegistry *)extensionRegistry {
1929 GPBCodedInputStreamState *state = &input->state_;
1930 if (GPBCodedInputStreamIsAtEnd(state)) {
1931 return;
1932 }
1933 NSData *data = GPBCodedInputStreamReadRetainedBytesNoCopy(state);
1934 if (data == nil) {
1935 return;
1936 }
1937 [self mergeFromData:data extensionRegistry:extensionRegistry];
1938 [data release];
1939}
1940
1941#pragma mark - Parse From Data Support
1942
1943+ (instancetype)parseFromData:(NSData *)data error:(NSError **)errorPtr {
1944 return [self parseFromData:data extensionRegistry:nil error:errorPtr];
1945}
1946
1947+ (instancetype)parseFromData:(NSData *)data
1948 extensionRegistry:(GPBExtensionRegistry *)extensionRegistry
1949 error:(NSError **)errorPtr {
1950 return [[[self alloc] initWithData:data
1951 extensionRegistry:extensionRegistry
1952 error:errorPtr] autorelease];
1953}
1954
1955+ (instancetype)parseFromCodedInputStream:(GPBCodedInputStream *)input
1956 extensionRegistry:(GPBExtensionRegistry *)extensionRegistry
1957 error:(NSError **)errorPtr {
1958 return
1959 [[[self alloc] initWithCodedInputStream:input
1960 extensionRegistry:extensionRegistry
1961 error:errorPtr] autorelease];
1962}
1963
1964#pragma mark - Parse Delimited From Data Support
1965
1966+ (instancetype)parseDelimitedFromCodedInputStream:(GPBCodedInputStream *)input
1967 extensionRegistry:
1968 (GPBExtensionRegistry *)extensionRegistry
1969 error:(NSError **)errorPtr {
1970 GPBMessage *message = [[[self alloc] init] autorelease];
1971 @try {
1972 [message mergeDelimitedFromCodedInputStream:input
1973 extensionRegistry:extensionRegistry];
1974 if (errorPtr) {
1975 *errorPtr = nil;
1976 }
1977 }
1978 @catch (NSException *exception) {
Brian Silverman9c614bc2016-02-15 20:20:02 -05001979 message = nil;
1980 if (errorPtr) {
Austin Schuh40c16522018-10-28 20:27:54 -07001981 *errorPtr = ErrorFromException(exception);
Brian Silverman9c614bc2016-02-15 20:20:02 -05001982 }
1983 }
1984#ifdef DEBUG
1985 if (message && !message.initialized) {
Brian Silverman9c614bc2016-02-15 20:20:02 -05001986 message = nil;
1987 if (errorPtr) {
1988 *errorPtr = MessageError(GPBMessageErrorCodeMissingRequiredField, nil);
1989 }
1990 }
1991#endif
1992 return message;
1993}
1994
1995#pragma mark - Unknown Field Support
1996
1997- (GPBUnknownFieldSet *)unknownFields {
1998 return unknownFields_;
1999}
2000
2001- (void)setUnknownFields:(GPBUnknownFieldSet *)unknownFields {
2002 if (unknownFields != unknownFields_) {
2003 [unknownFields_ release];
2004 unknownFields_ = [unknownFields copy];
2005 GPBBecomeVisibleToAutocreator(self);
2006 }
2007}
2008
2009- (void)parseMessageSet:(GPBCodedInputStream *)input
2010 extensionRegistry:(GPBExtensionRegistry *)extensionRegistry {
2011 uint32_t typeId = 0;
2012 NSData *rawBytes = nil;
2013 GPBExtensionDescriptor *extension = nil;
2014 GPBCodedInputStreamState *state = &input->state_;
2015 while (true) {
2016 uint32_t tag = GPBCodedInputStreamReadTag(state);
2017 if (tag == 0) {
2018 break;
2019 }
2020
2021 if (tag == GPBWireFormatMessageSetTypeIdTag) {
2022 typeId = GPBCodedInputStreamReadUInt32(state);
2023 if (typeId != 0) {
2024 extension = [extensionRegistry extensionForDescriptor:[self descriptor]
2025 fieldNumber:typeId];
2026 }
2027 } else if (tag == GPBWireFormatMessageSetMessageTag) {
2028 rawBytes =
2029 [GPBCodedInputStreamReadRetainedBytesNoCopy(state) autorelease];
2030 } else {
2031 if (![input skipField:tag]) {
2032 break;
2033 }
2034 }
2035 }
2036
2037 [input checkLastTagWas:GPBWireFormatMessageSetItemEndTag];
2038
2039 if (rawBytes != nil && typeId != 0) {
2040 if (extension != nil) {
2041 GPBCodedInputStream *newInput =
2042 [[GPBCodedInputStream alloc] initWithData:rawBytes];
2043 GPBExtensionMergeFromInputStream(extension,
2044 extension.packable,
2045 newInput,
2046 extensionRegistry,
2047 self);
2048 [newInput release];
2049 } else {
2050 GPBUnknownFieldSet *unknownFields = GetOrMakeUnknownFields(self);
Austin Schuh40c16522018-10-28 20:27:54 -07002051 // rawBytes was created via a NoCopy, so it can be reusing a
2052 // subrange of another NSData that might go out of scope as things
2053 // unwind, so a copy is needed to ensure what is saved in the
2054 // unknown fields stays valid.
2055 NSData *cloned = [NSData dataWithData:rawBytes];
2056 [unknownFields mergeMessageSetMessage:typeId data:cloned];
Brian Silverman9c614bc2016-02-15 20:20:02 -05002057 }
2058 }
2059}
2060
2061- (BOOL)parseUnknownField:(GPBCodedInputStream *)input
2062 extensionRegistry:(GPBExtensionRegistry *)extensionRegistry
2063 tag:(uint32_t)tag {
2064 GPBWireFormat wireType = GPBWireFormatGetTagWireType(tag);
2065 int32_t fieldNumber = GPBWireFormatGetTagFieldNumber(tag);
2066
2067 GPBDescriptor *descriptor = [self descriptor];
2068 GPBExtensionDescriptor *extension =
2069 [extensionRegistry extensionForDescriptor:descriptor
2070 fieldNumber:fieldNumber];
2071 if (extension == nil) {
2072 if (descriptor.wireFormat && GPBWireFormatMessageSetItemTag == tag) {
2073 [self parseMessageSet:input extensionRegistry:extensionRegistry];
2074 return YES;
2075 }
2076 } else {
2077 if (extension.wireType == wireType) {
2078 GPBExtensionMergeFromInputStream(extension,
2079 extension.packable,
2080 input,
2081 extensionRegistry,
2082 self);
2083 return YES;
2084 }
2085 // Primitive, repeated types can be packed on unpacked on the wire, and are
2086 // parsed either way.
2087 if ([extension isRepeated] &&
2088 !GPBDataTypeIsObject(extension->description_->dataType) &&
2089 (extension.alternateWireType == wireType)) {
2090 GPBExtensionMergeFromInputStream(extension,
2091 !extension.packable,
2092 input,
2093 extensionRegistry,
2094 self);
2095 return YES;
2096 }
2097 }
2098 if ([GPBUnknownFieldSet isFieldTag:tag]) {
2099 GPBUnknownFieldSet *unknownFields = GetOrMakeUnknownFields(self);
2100 return [unknownFields mergeFieldFrom:tag input:input];
2101 } else {
2102 return NO;
2103 }
2104}
2105
2106- (void)addUnknownMapEntry:(int32_t)fieldNum value:(NSData *)data {
2107 GPBUnknownFieldSet *unknownFields = GetOrMakeUnknownFields(self);
2108 [unknownFields addUnknownMapEntry:fieldNum value:data];
2109}
2110
2111#pragma mark - MergeFromCodedInputStream Support
2112
2113static void MergeSingleFieldFromCodedInputStream(
2114 GPBMessage *self, GPBFieldDescriptor *field, GPBFileSyntax syntax,
2115 GPBCodedInputStream *input, GPBExtensionRegistry *extensionRegistry) {
2116 GPBDataType fieldDataType = GPBGetFieldDataType(field);
2117 switch (fieldDataType) {
2118#define CASE_SINGLE_POD(NAME, TYPE, FUNC_TYPE) \
2119 case GPBDataType##NAME: { \
2120 TYPE val = GPBCodedInputStreamRead##NAME(&input->state_); \
2121 GPBSet##FUNC_TYPE##IvarWithFieldInternal(self, field, val, syntax); \
2122 break; \
2123 }
2124#define CASE_SINGLE_OBJECT(NAME) \
2125 case GPBDataType##NAME: { \
2126 id val = GPBCodedInputStreamReadRetained##NAME(&input->state_); \
2127 GPBSetRetainedObjectIvarWithFieldInternal(self, field, val, syntax); \
2128 break; \
2129 }
2130 CASE_SINGLE_POD(Bool, BOOL, Bool)
2131 CASE_SINGLE_POD(Fixed32, uint32_t, UInt32)
2132 CASE_SINGLE_POD(SFixed32, int32_t, Int32)
2133 CASE_SINGLE_POD(Float, float, Float)
2134 CASE_SINGLE_POD(Fixed64, uint64_t, UInt64)
2135 CASE_SINGLE_POD(SFixed64, int64_t, Int64)
2136 CASE_SINGLE_POD(Double, double, Double)
2137 CASE_SINGLE_POD(Int32, int32_t, Int32)
2138 CASE_SINGLE_POD(Int64, int64_t, Int64)
2139 CASE_SINGLE_POD(SInt32, int32_t, Int32)
2140 CASE_SINGLE_POD(SInt64, int64_t, Int64)
2141 CASE_SINGLE_POD(UInt32, uint32_t, UInt32)
2142 CASE_SINGLE_POD(UInt64, uint64_t, UInt64)
2143 CASE_SINGLE_OBJECT(Bytes)
2144 CASE_SINGLE_OBJECT(String)
2145#undef CASE_SINGLE_POD
2146#undef CASE_SINGLE_OBJECT
2147
2148 case GPBDataTypeMessage: {
2149 if (GPBGetHasIvarField(self, field)) {
2150 // GPBGetObjectIvarWithFieldNoAutocreate() avoids doing the has
2151 // check again.
2152 GPBMessage *message =
2153 GPBGetObjectIvarWithFieldNoAutocreate(self, field);
2154 [input readMessage:message extensionRegistry:extensionRegistry];
2155 } else {
2156 GPBMessage *message = [[field.msgClass alloc] init];
2157 [input readMessage:message extensionRegistry:extensionRegistry];
2158 GPBSetRetainedObjectIvarWithFieldInternal(self, field, message, syntax);
2159 }
2160 break;
2161 }
2162
2163 case GPBDataTypeGroup: {
2164 if (GPBGetHasIvarField(self, field)) {
2165 // GPBGetObjectIvarWithFieldNoAutocreate() avoids doing the has
2166 // check again.
2167 GPBMessage *message =
2168 GPBGetObjectIvarWithFieldNoAutocreate(self, field);
2169 [input readGroup:GPBFieldNumber(field)
2170 message:message
2171 extensionRegistry:extensionRegistry];
2172 } else {
2173 GPBMessage *message = [[field.msgClass alloc] init];
2174 [input readGroup:GPBFieldNumber(field)
2175 message:message
2176 extensionRegistry:extensionRegistry];
2177 GPBSetRetainedObjectIvarWithFieldInternal(self, field, message, syntax);
2178 }
2179 break;
2180 }
2181
2182 case GPBDataTypeEnum: {
2183 int32_t val = GPBCodedInputStreamReadEnum(&input->state_);
2184 if (GPBHasPreservingUnknownEnumSemantics(syntax) ||
2185 [field isValidEnumValue:val]) {
2186 GPBSetInt32IvarWithFieldInternal(self, field, val, syntax);
2187 } else {
2188 GPBUnknownFieldSet *unknownFields = GetOrMakeUnknownFields(self);
2189 [unknownFields mergeVarintField:GPBFieldNumber(field) value:val];
2190 }
2191 }
2192 } // switch
2193}
2194
2195static void MergeRepeatedPackedFieldFromCodedInputStream(
2196 GPBMessage *self, GPBFieldDescriptor *field, GPBFileSyntax syntax,
2197 GPBCodedInputStream *input) {
2198 GPBDataType fieldDataType = GPBGetFieldDataType(field);
2199 GPBCodedInputStreamState *state = &input->state_;
2200 id genericArray = GetOrCreateArrayIvarWithField(self, field, syntax);
2201 int32_t length = GPBCodedInputStreamReadInt32(state);
2202 size_t limit = GPBCodedInputStreamPushLimit(state, length);
2203 while (GPBCodedInputStreamBytesUntilLimit(state) > 0) {
2204 switch (fieldDataType) {
2205#define CASE_REPEATED_PACKED_POD(NAME, TYPE, ARRAY_TYPE) \
2206 case GPBDataType##NAME: { \
2207 TYPE val = GPBCodedInputStreamRead##NAME(state); \
2208 [(GPB##ARRAY_TYPE##Array *)genericArray addValue:val]; \
2209 break; \
2210 }
2211 CASE_REPEATED_PACKED_POD(Bool, BOOL, Bool)
2212 CASE_REPEATED_PACKED_POD(Fixed32, uint32_t, UInt32)
2213 CASE_REPEATED_PACKED_POD(SFixed32, int32_t, Int32)
2214 CASE_REPEATED_PACKED_POD(Float, float, Float)
2215 CASE_REPEATED_PACKED_POD(Fixed64, uint64_t, UInt64)
2216 CASE_REPEATED_PACKED_POD(SFixed64, int64_t, Int64)
2217 CASE_REPEATED_PACKED_POD(Double, double, Double)
2218 CASE_REPEATED_PACKED_POD(Int32, int32_t, Int32)
2219 CASE_REPEATED_PACKED_POD(Int64, int64_t, Int64)
2220 CASE_REPEATED_PACKED_POD(SInt32, int32_t, Int32)
2221 CASE_REPEATED_PACKED_POD(SInt64, int64_t, Int64)
2222 CASE_REPEATED_PACKED_POD(UInt32, uint32_t, UInt32)
2223 CASE_REPEATED_PACKED_POD(UInt64, uint64_t, UInt64)
2224#undef CASE_REPEATED_PACKED_POD
2225
2226 case GPBDataTypeBytes:
2227 case GPBDataTypeString:
2228 case GPBDataTypeMessage:
2229 case GPBDataTypeGroup:
2230 NSCAssert(NO, @"Non primitive types can't be packed");
2231 break;
2232
2233 case GPBDataTypeEnum: {
2234 int32_t val = GPBCodedInputStreamReadEnum(state);
2235 if (GPBHasPreservingUnknownEnumSemantics(syntax) ||
2236 [field isValidEnumValue:val]) {
2237 [(GPBEnumArray*)genericArray addRawValue:val];
2238 } else {
2239 GPBUnknownFieldSet *unknownFields = GetOrMakeUnknownFields(self);
2240 [unknownFields mergeVarintField:GPBFieldNumber(field) value:val];
2241 }
2242 break;
2243 }
2244 } // switch
2245 } // while(BytesUntilLimit() > 0)
2246 GPBCodedInputStreamPopLimit(state, limit);
2247}
2248
2249static void MergeRepeatedNotPackedFieldFromCodedInputStream(
2250 GPBMessage *self, GPBFieldDescriptor *field, GPBFileSyntax syntax,
2251 GPBCodedInputStream *input, GPBExtensionRegistry *extensionRegistry) {
2252 GPBCodedInputStreamState *state = &input->state_;
2253 id genericArray = GetOrCreateArrayIvarWithField(self, field, syntax);
2254 switch (GPBGetFieldDataType(field)) {
2255#define CASE_REPEATED_NOT_PACKED_POD(NAME, TYPE, ARRAY_TYPE) \
2256 case GPBDataType##NAME: { \
2257 TYPE val = GPBCodedInputStreamRead##NAME(state); \
2258 [(GPB##ARRAY_TYPE##Array *)genericArray addValue:val]; \
2259 break; \
2260 }
2261#define CASE_REPEATED_NOT_PACKED_OBJECT(NAME) \
2262 case GPBDataType##NAME: { \
2263 id val = GPBCodedInputStreamReadRetained##NAME(state); \
2264 [(NSMutableArray*)genericArray addObject:val]; \
2265 [val release]; \
2266 break; \
2267 }
2268 CASE_REPEATED_NOT_PACKED_POD(Bool, BOOL, Bool)
2269 CASE_REPEATED_NOT_PACKED_POD(Fixed32, uint32_t, UInt32)
2270 CASE_REPEATED_NOT_PACKED_POD(SFixed32, int32_t, Int32)
2271 CASE_REPEATED_NOT_PACKED_POD(Float, float, Float)
2272 CASE_REPEATED_NOT_PACKED_POD(Fixed64, uint64_t, UInt64)
2273 CASE_REPEATED_NOT_PACKED_POD(SFixed64, int64_t, Int64)
2274 CASE_REPEATED_NOT_PACKED_POD(Double, double, Double)
2275 CASE_REPEATED_NOT_PACKED_POD(Int32, int32_t, Int32)
2276 CASE_REPEATED_NOT_PACKED_POD(Int64, int64_t, Int64)
2277 CASE_REPEATED_NOT_PACKED_POD(SInt32, int32_t, Int32)
2278 CASE_REPEATED_NOT_PACKED_POD(SInt64, int64_t, Int64)
2279 CASE_REPEATED_NOT_PACKED_POD(UInt32, uint32_t, UInt32)
2280 CASE_REPEATED_NOT_PACKED_POD(UInt64, uint64_t, UInt64)
2281 CASE_REPEATED_NOT_PACKED_OBJECT(Bytes)
2282 CASE_REPEATED_NOT_PACKED_OBJECT(String)
2283#undef CASE_REPEATED_NOT_PACKED_POD
2284#undef CASE_NOT_PACKED_OBJECT
2285 case GPBDataTypeMessage: {
2286 GPBMessage *message = [[field.msgClass alloc] init];
2287 [input readMessage:message extensionRegistry:extensionRegistry];
2288 [(NSMutableArray*)genericArray addObject:message];
2289 [message release];
2290 break;
2291 }
2292 case GPBDataTypeGroup: {
2293 GPBMessage *message = [[field.msgClass alloc] init];
2294 [input readGroup:GPBFieldNumber(field)
2295 message:message
2296 extensionRegistry:extensionRegistry];
2297 [(NSMutableArray*)genericArray addObject:message];
2298 [message release];
2299 break;
2300 }
2301 case GPBDataTypeEnum: {
2302 int32_t val = GPBCodedInputStreamReadEnum(state);
2303 if (GPBHasPreservingUnknownEnumSemantics(syntax) ||
2304 [field isValidEnumValue:val]) {
2305 [(GPBEnumArray*)genericArray addRawValue:val];
2306 } else {
2307 GPBUnknownFieldSet *unknownFields = GetOrMakeUnknownFields(self);
2308 [unknownFields mergeVarintField:GPBFieldNumber(field) value:val];
2309 }
2310 break;
2311 }
2312 } // switch
2313}
2314
2315- (void)mergeFromCodedInputStream:(GPBCodedInputStream *)input
2316 extensionRegistry:(GPBExtensionRegistry *)extensionRegistry {
2317 GPBDescriptor *descriptor = [self descriptor];
2318 GPBFileSyntax syntax = descriptor.file.syntax;
2319 GPBCodedInputStreamState *state = &input->state_;
2320 uint32_t tag = 0;
2321 NSUInteger startingIndex = 0;
2322 NSArray *fields = descriptor->fields_;
2323 NSUInteger numFields = fields.count;
2324 while (YES) {
2325 BOOL merged = NO;
2326 tag = GPBCodedInputStreamReadTag(state);
Austin Schuh40c16522018-10-28 20:27:54 -07002327 if (tag == 0) {
2328 break; // Reached end.
2329 }
Brian Silverman9c614bc2016-02-15 20:20:02 -05002330 for (NSUInteger i = 0; i < numFields; ++i) {
2331 if (startingIndex >= numFields) startingIndex = 0;
2332 GPBFieldDescriptor *fieldDescriptor = fields[startingIndex];
2333 if (GPBFieldTag(fieldDescriptor) == tag) {
2334 GPBFieldType fieldType = fieldDescriptor.fieldType;
2335 if (fieldType == GPBFieldTypeSingle) {
2336 MergeSingleFieldFromCodedInputStream(self, fieldDescriptor, syntax,
2337 input, extensionRegistry);
2338 // Well formed protos will only have a single field once, advance
2339 // the starting index to the next field.
2340 startingIndex += 1;
2341 } else if (fieldType == GPBFieldTypeRepeated) {
2342 if (fieldDescriptor.isPackable) {
2343 MergeRepeatedPackedFieldFromCodedInputStream(
2344 self, fieldDescriptor, syntax, input);
2345 // Well formed protos will only have a repeated field that is
2346 // packed once, advance the starting index to the next field.
2347 startingIndex += 1;
2348 } else {
2349 MergeRepeatedNotPackedFieldFromCodedInputStream(
2350 self, fieldDescriptor, syntax, input, extensionRegistry);
2351 }
2352 } else { // fieldType == GPBFieldTypeMap
2353 // GPB*Dictionary or NSDictionary, exact type doesn't matter at this
2354 // point.
2355 id map = GetOrCreateMapIvarWithField(self, fieldDescriptor, syntax);
2356 [input readMapEntry:map
2357 extensionRegistry:extensionRegistry
2358 field:fieldDescriptor
2359 parentMessage:self];
2360 }
2361 merged = YES;
2362 break;
2363 } else {
2364 startingIndex += 1;
2365 }
2366 } // for(i < numFields)
2367
Austin Schuh40c16522018-10-28 20:27:54 -07002368 if (!merged && (tag != 0)) {
Brian Silverman9c614bc2016-02-15 20:20:02 -05002369 // Primitive, repeated types can be packed on unpacked on the wire, and
2370 // are parsed either way. The above loop covered tag in the preferred
2371 // for, so this need to check the alternate form.
2372 for (NSUInteger i = 0; i < numFields; ++i) {
2373 if (startingIndex >= numFields) startingIndex = 0;
2374 GPBFieldDescriptor *fieldDescriptor = fields[startingIndex];
2375 if ((fieldDescriptor.fieldType == GPBFieldTypeRepeated) &&
2376 !GPBFieldDataTypeIsObject(fieldDescriptor) &&
2377 (GPBFieldAlternateTag(fieldDescriptor) == tag)) {
2378 BOOL alternateIsPacked = !fieldDescriptor.isPackable;
2379 if (alternateIsPacked) {
2380 MergeRepeatedPackedFieldFromCodedInputStream(
2381 self, fieldDescriptor, syntax, input);
2382 // Well formed protos will only have a repeated field that is
2383 // packed once, advance the starting index to the next field.
2384 startingIndex += 1;
2385 } else {
2386 MergeRepeatedNotPackedFieldFromCodedInputStream(
2387 self, fieldDescriptor, syntax, input, extensionRegistry);
2388 }
2389 merged = YES;
2390 break;
2391 } else {
2392 startingIndex += 1;
2393 }
2394 }
2395 }
2396
2397 if (!merged) {
2398 if (tag == 0) {
2399 // zero signals EOF / limit reached
2400 return;
2401 } else {
Austin Schuh40c16522018-10-28 20:27:54 -07002402 if (![self parseUnknownField:input
2403 extensionRegistry:extensionRegistry
2404 tag:tag]) {
2405 // it's an endgroup tag
2406 return;
Brian Silverman9c614bc2016-02-15 20:20:02 -05002407 }
2408 }
2409 } // if(!merged)
2410
2411 } // while(YES)
2412}
2413
2414#pragma mark - MergeFrom Support
2415
2416- (void)mergeFrom:(GPBMessage *)other {
2417 Class selfClass = [self class];
2418 Class otherClass = [other class];
2419 if (!([selfClass isSubclassOfClass:otherClass] ||
2420 [otherClass isSubclassOfClass:selfClass])) {
2421 [NSException raise:NSInvalidArgumentException
2422 format:@"Classes must match %@ != %@", selfClass, otherClass];
2423 }
2424
2425 // We assume something will be done and become visible.
2426 GPBBecomeVisibleToAutocreator(self);
2427
2428 GPBDescriptor *descriptor = [[self class] descriptor];
2429 GPBFileSyntax syntax = descriptor.file.syntax;
2430
2431 for (GPBFieldDescriptor *field in descriptor->fields_) {
2432 GPBFieldType fieldType = field.fieldType;
2433 if (fieldType == GPBFieldTypeSingle) {
2434 int32_t hasIndex = GPBFieldHasIndex(field);
2435 uint32_t fieldNumber = GPBFieldNumber(field);
2436 if (!GPBGetHasIvar(other, hasIndex, fieldNumber)) {
2437 // Other doesn't have the field set, on to the next.
2438 continue;
2439 }
2440 GPBDataType fieldDataType = GPBGetFieldDataType(field);
2441 switch (fieldDataType) {
2442 case GPBDataTypeBool:
2443 GPBSetBoolIvarWithFieldInternal(
2444 self, field, GPBGetMessageBoolField(other, field), syntax);
2445 break;
2446 case GPBDataTypeSFixed32:
2447 case GPBDataTypeEnum:
2448 case GPBDataTypeInt32:
2449 case GPBDataTypeSInt32:
2450 GPBSetInt32IvarWithFieldInternal(
2451 self, field, GPBGetMessageInt32Field(other, field), syntax);
2452 break;
2453 case GPBDataTypeFixed32:
2454 case GPBDataTypeUInt32:
2455 GPBSetUInt32IvarWithFieldInternal(
2456 self, field, GPBGetMessageUInt32Field(other, field), syntax);
2457 break;
2458 case GPBDataTypeSFixed64:
2459 case GPBDataTypeInt64:
2460 case GPBDataTypeSInt64:
2461 GPBSetInt64IvarWithFieldInternal(
2462 self, field, GPBGetMessageInt64Field(other, field), syntax);
2463 break;
2464 case GPBDataTypeFixed64:
2465 case GPBDataTypeUInt64:
2466 GPBSetUInt64IvarWithFieldInternal(
2467 self, field, GPBGetMessageUInt64Field(other, field), syntax);
2468 break;
2469 case GPBDataTypeFloat:
2470 GPBSetFloatIvarWithFieldInternal(
2471 self, field, GPBGetMessageFloatField(other, field), syntax);
2472 break;
2473 case GPBDataTypeDouble:
2474 GPBSetDoubleIvarWithFieldInternal(
2475 self, field, GPBGetMessageDoubleField(other, field), syntax);
2476 break;
2477 case GPBDataTypeBytes:
2478 case GPBDataTypeString: {
2479 id otherVal = GPBGetObjectIvarWithFieldNoAutocreate(other, field);
2480 GPBSetObjectIvarWithFieldInternal(self, field, otherVal, syntax);
2481 break;
2482 }
2483 case GPBDataTypeMessage:
2484 case GPBDataTypeGroup: {
2485 id otherVal = GPBGetObjectIvarWithFieldNoAutocreate(other, field);
2486 if (GPBGetHasIvar(self, hasIndex, fieldNumber)) {
2487 GPBMessage *message =
2488 GPBGetObjectIvarWithFieldNoAutocreate(self, field);
2489 [message mergeFrom:otherVal];
2490 } else {
2491 GPBMessage *message = [otherVal copy];
2492 GPBSetRetainedObjectIvarWithFieldInternal(self, field, message,
2493 syntax);
2494 }
2495 break;
2496 }
2497 } // switch()
2498 } else if (fieldType == GPBFieldTypeRepeated) {
2499 // In the case of a list, they need to be appended, and there is no
2500 // _hasIvar to worry about setting.
2501 id otherArray =
2502 GPBGetObjectIvarWithFieldNoAutocreate(other, field);
2503 if (otherArray) {
2504 GPBDataType fieldDataType = field->description_->dataType;
2505 if (GPBDataTypeIsObject(fieldDataType)) {
2506 NSMutableArray *resultArray =
2507 GetOrCreateArrayIvarWithField(self, field, syntax);
2508 [resultArray addObjectsFromArray:otherArray];
2509 } else if (fieldDataType == GPBDataTypeEnum) {
2510 GPBEnumArray *resultArray =
2511 GetOrCreateArrayIvarWithField(self, field, syntax);
2512 [resultArray addRawValuesFromArray:otherArray];
2513 } else {
2514 // The array type doesn't matter, that all implment
2515 // -addValuesFromArray:.
2516 GPBInt32Array *resultArray =
2517 GetOrCreateArrayIvarWithField(self, field, syntax);
2518 [resultArray addValuesFromArray:otherArray];
2519 }
2520 }
2521 } else { // fieldType = GPBFieldTypeMap
2522 // In the case of a map, they need to be merged, and there is no
2523 // _hasIvar to worry about setting.
2524 id otherDict = GPBGetObjectIvarWithFieldNoAutocreate(other, field);
2525 if (otherDict) {
2526 GPBDataType keyDataType = field.mapKeyDataType;
2527 GPBDataType valueDataType = field->description_->dataType;
2528 if (GPBDataTypeIsObject(keyDataType) &&
2529 GPBDataTypeIsObject(valueDataType)) {
2530 NSMutableDictionary *resultDict =
2531 GetOrCreateMapIvarWithField(self, field, syntax);
2532 [resultDict addEntriesFromDictionary:otherDict];
2533 } else if (valueDataType == GPBDataTypeEnum) {
2534 // The exact type doesn't matter, just need to know it is a
2535 // GPB*EnumDictionary.
2536 GPBInt32EnumDictionary *resultDict =
2537 GetOrCreateMapIvarWithField(self, field, syntax);
2538 [resultDict addRawEntriesFromDictionary:otherDict];
2539 } else {
2540 // The exact type doesn't matter, they all implement
2541 // -addEntriesFromDictionary:.
2542 GPBInt32Int32Dictionary *resultDict =
2543 GetOrCreateMapIvarWithField(self, field, syntax);
2544 [resultDict addEntriesFromDictionary:otherDict];
2545 }
2546 }
2547 } // if (fieldType)..else if...else
2548 } // for(fields)
2549
2550 // Unknown fields.
2551 if (!unknownFields_) {
2552 [self setUnknownFields:other.unknownFields];
2553 } else {
2554 [unknownFields_ mergeUnknownFields:other.unknownFields];
2555 }
2556
2557 // Extensions
2558
2559 if (other->extensionMap_.count == 0) {
2560 return;
2561 }
2562
2563 if (extensionMap_ == nil) {
2564 extensionMap_ =
2565 CloneExtensionMap(other->extensionMap_, NSZoneFromPointer(self));
2566 } else {
2567 for (GPBExtensionDescriptor *extension in other->extensionMap_) {
2568 id otherValue = [other->extensionMap_ objectForKey:extension];
2569 id value = [extensionMap_ objectForKey:extension];
2570 BOOL isMessageExtension = GPBExtensionIsMessage(extension);
2571
2572 if (extension.repeated) {
2573 NSMutableArray *list = value;
2574 if (list == nil) {
2575 list = [[NSMutableArray alloc] init];
2576 [extensionMap_ setObject:list forKey:extension];
2577 [list release];
2578 }
2579 if (isMessageExtension) {
2580 for (GPBMessage *otherListValue in otherValue) {
2581 GPBMessage *copiedValue = [otherListValue copy];
2582 [list addObject:copiedValue];
2583 [copiedValue release];
2584 }
2585 } else {
2586 [list addObjectsFromArray:otherValue];
2587 }
2588 } else {
2589 if (isMessageExtension) {
2590 if (value) {
2591 [(GPBMessage *)value mergeFrom:(GPBMessage *)otherValue];
2592 } else {
2593 GPBMessage *copiedValue = [otherValue copy];
2594 [extensionMap_ setObject:copiedValue forKey:extension];
2595 [copiedValue release];
2596 }
2597 } else {
2598 [extensionMap_ setObject:otherValue forKey:extension];
2599 }
2600 }
2601
2602 if (isMessageExtension && !extension.isRepeated) {
2603 GPBMessage *autocreatedValue =
2604 [[autocreatedExtensionMap_ objectForKey:extension] retain];
2605 // Must remove from the map before calling GPBClearMessageAutocreator()
2606 // so that GPBClearMessageAutocreator() knows its safe to clear.
2607 [autocreatedExtensionMap_ removeObjectForKey:extension];
2608 GPBClearMessageAutocreator(autocreatedValue);
2609 [autocreatedValue release];
2610 }
2611 }
2612 }
2613}
2614
2615#pragma mark - isEqual: & hash Support
2616
Austin Schuh40c16522018-10-28 20:27:54 -07002617- (BOOL)isEqual:(id)other {
Brian Silverman9c614bc2016-02-15 20:20:02 -05002618 if (other == self) {
2619 return YES;
2620 }
Austin Schuh40c16522018-10-28 20:27:54 -07002621 if (![other isKindOfClass:[GPBMessage class]]) {
Brian Silverman9c614bc2016-02-15 20:20:02 -05002622 return NO;
2623 }
Austin Schuh40c16522018-10-28 20:27:54 -07002624 GPBMessage *otherMsg = other;
Brian Silverman9c614bc2016-02-15 20:20:02 -05002625 GPBDescriptor *descriptor = [[self class] descriptor];
Austin Schuh40c16522018-10-28 20:27:54 -07002626 if ([[otherMsg class] descriptor] != descriptor) {
2627 return NO;
2628 }
Brian Silverman9c614bc2016-02-15 20:20:02 -05002629 uint8_t *selfStorage = (uint8_t *)messageStorage_;
Austin Schuh40c16522018-10-28 20:27:54 -07002630 uint8_t *otherStorage = (uint8_t *)otherMsg->messageStorage_;
Brian Silverman9c614bc2016-02-15 20:20:02 -05002631
2632 for (GPBFieldDescriptor *field in descriptor->fields_) {
2633 if (GPBFieldIsMapOrArray(field)) {
2634 // In the case of a list or map, there is no _hasIvar to worry about.
2635 // NOTE: These are NSArray/GPB*Array or NSDictionary/GPB*Dictionary, but
2636 // the type doesn't really matter as the objects all support -count and
2637 // -isEqual:.
2638 NSArray *resultMapOrArray =
2639 GPBGetObjectIvarWithFieldNoAutocreate(self, field);
2640 NSArray *otherMapOrArray =
2641 GPBGetObjectIvarWithFieldNoAutocreate(other, field);
2642 // nil and empty are equal
2643 if (resultMapOrArray.count != 0 || otherMapOrArray.count != 0) {
2644 if (![resultMapOrArray isEqual:otherMapOrArray]) {
2645 return NO;
2646 }
2647 }
2648 } else { // Single field
2649 int32_t hasIndex = GPBFieldHasIndex(field);
2650 uint32_t fieldNum = GPBFieldNumber(field);
2651 BOOL selfHas = GPBGetHasIvar(self, hasIndex, fieldNum);
2652 BOOL otherHas = GPBGetHasIvar(other, hasIndex, fieldNum);
2653 if (selfHas != otherHas) {
2654 return NO; // Differing has values, not equal.
2655 }
2656 if (!selfHas) {
2657 // Same has values, was no, nothing else to check for this field.
2658 continue;
2659 }
2660 // Now compare the values.
2661 GPBDataType fieldDataType = GPBGetFieldDataType(field);
2662 size_t fieldOffset = field->description_->offset;
2663 switch (fieldDataType) {
2664 case GPBDataTypeBool: {
Austin Schuh40c16522018-10-28 20:27:54 -07002665 // Bools are stored in has_bits to avoid needing explicit space in
2666 // the storage structure.
2667 // (the field number passed to the HasIvar helper doesn't really
2668 // matter since the offset is never negative)
2669 BOOL selfValue = GPBGetHasIvar(self, (int32_t)(fieldOffset), 0);
2670 BOOL otherValue = GPBGetHasIvar(other, (int32_t)(fieldOffset), 0);
2671 if (selfValue != otherValue) {
Brian Silverman9c614bc2016-02-15 20:20:02 -05002672 return NO;
2673 }
2674 break;
2675 }
2676 case GPBDataTypeSFixed32:
2677 case GPBDataTypeInt32:
2678 case GPBDataTypeSInt32:
2679 case GPBDataTypeEnum:
2680 case GPBDataTypeFixed32:
2681 case GPBDataTypeUInt32:
2682 case GPBDataTypeFloat: {
Austin Schuh40c16522018-10-28 20:27:54 -07002683 GPBInternalCompileAssert(sizeof(float) == sizeof(uint32_t), float_not_32_bits);
Brian Silverman9c614bc2016-02-15 20:20:02 -05002684 // These are all 32bit, signed/unsigned doesn't matter for equality.
2685 uint32_t *selfValPtr = (uint32_t *)&selfStorage[fieldOffset];
2686 uint32_t *otherValPtr = (uint32_t *)&otherStorage[fieldOffset];
2687 if (*selfValPtr != *otherValPtr) {
2688 return NO;
2689 }
2690 break;
2691 }
2692 case GPBDataTypeSFixed64:
2693 case GPBDataTypeInt64:
2694 case GPBDataTypeSInt64:
2695 case GPBDataTypeFixed64:
2696 case GPBDataTypeUInt64:
2697 case GPBDataTypeDouble: {
Austin Schuh40c16522018-10-28 20:27:54 -07002698 GPBInternalCompileAssert(sizeof(double) == sizeof(uint64_t), double_not_64_bits);
Brian Silverman9c614bc2016-02-15 20:20:02 -05002699 // These are all 64bit, signed/unsigned doesn't matter for equality.
2700 uint64_t *selfValPtr = (uint64_t *)&selfStorage[fieldOffset];
2701 uint64_t *otherValPtr = (uint64_t *)&otherStorage[fieldOffset];
2702 if (*selfValPtr != *otherValPtr) {
2703 return NO;
2704 }
2705 break;
2706 }
2707 case GPBDataTypeBytes:
2708 case GPBDataTypeString:
2709 case GPBDataTypeMessage:
2710 case GPBDataTypeGroup: {
2711 // Type doesn't matter here, they all implement -isEqual:.
2712 id *selfValPtr = (id *)&selfStorage[fieldOffset];
2713 id *otherValPtr = (id *)&otherStorage[fieldOffset];
2714 if (![*selfValPtr isEqual:*otherValPtr]) {
2715 return NO;
2716 }
2717 break;
2718 }
2719 } // switch()
2720 } // if(mapOrArray)...else
2721 } // for(fields)
2722
2723 // nil and empty are equal
Austin Schuh40c16522018-10-28 20:27:54 -07002724 if (extensionMap_.count != 0 || otherMsg->extensionMap_.count != 0) {
2725 if (![extensionMap_ isEqual:otherMsg->extensionMap_]) {
Brian Silverman9c614bc2016-02-15 20:20:02 -05002726 return NO;
2727 }
2728 }
2729
2730 // nil and empty are equal
Austin Schuh40c16522018-10-28 20:27:54 -07002731 GPBUnknownFieldSet *otherUnknowns = otherMsg->unknownFields_;
Brian Silverman9c614bc2016-02-15 20:20:02 -05002732 if ([unknownFields_ countOfFields] != 0 ||
2733 [otherUnknowns countOfFields] != 0) {
2734 if (![unknownFields_ isEqual:otherUnknowns]) {
2735 return NO;
2736 }
2737 }
2738
2739 return YES;
2740}
2741
2742// It is very difficult to implement a generic hash for ProtoBuf messages that
2743// will perform well. If you need hashing on your ProtoBufs (eg you are using
2744// them as dictionary keys) you will probably want to implement a ProtoBuf
2745// message specific hash as a category on your protobuf class. Do not make it a
2746// category on GPBMessage as you will conflict with this hash, and will possibly
2747// override hash for all generated protobufs. A good implementation of hash will
2748// be really fast, so we would recommend only hashing protobufs that have an
2749// identifier field of some kind that you can easily hash. If you implement
2750// hash, we would strongly recommend overriding isEqual: in your category as
2751// well, as the default implementation of isEqual: is extremely slow, and may
2752// drastically affect performance in large sets.
2753- (NSUInteger)hash {
2754 GPBDescriptor *descriptor = [[self class] descriptor];
2755 const NSUInteger prime = 19;
2756 uint8_t *storage = (uint8_t *)messageStorage_;
2757
2758 // Start with the descriptor and then mix it with some instance info.
2759 // Hopefully that will give a spread based on classes and what fields are set.
2760 NSUInteger result = (NSUInteger)descriptor;
2761
2762 for (GPBFieldDescriptor *field in descriptor->fields_) {
2763 if (GPBFieldIsMapOrArray(field)) {
2764 // Exact type doesn't matter, just check if there are any elements.
2765 NSArray *mapOrArray = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
2766 NSUInteger count = mapOrArray.count;
2767 if (count) {
2768 // NSArray/NSDictionary use count, use the field number and the count.
2769 result = prime * result + GPBFieldNumber(field);
2770 result = prime * result + count;
2771 }
2772 } else if (GPBGetHasIvarField(self, field)) {
2773 // Just using the field number seemed simple/fast, but then a small
2774 // message class where all the same fields are always set (to different
2775 // things would end up all with the same hash, so pull in some data).
2776 GPBDataType fieldDataType = GPBGetFieldDataType(field);
2777 size_t fieldOffset = field->description_->offset;
2778 switch (fieldDataType) {
2779 case GPBDataTypeBool: {
Austin Schuh40c16522018-10-28 20:27:54 -07002780 // Bools are stored in has_bits to avoid needing explicit space in
2781 // the storage structure.
2782 // (the field number passed to the HasIvar helper doesn't really
2783 // matter since the offset is never negative)
2784 BOOL value = GPBGetHasIvar(self, (int32_t)(fieldOffset), 0);
2785 result = prime * result + value;
Brian Silverman9c614bc2016-02-15 20:20:02 -05002786 break;
2787 }
2788 case GPBDataTypeSFixed32:
2789 case GPBDataTypeInt32:
2790 case GPBDataTypeSInt32:
2791 case GPBDataTypeEnum:
2792 case GPBDataTypeFixed32:
2793 case GPBDataTypeUInt32:
2794 case GPBDataTypeFloat: {
Austin Schuh40c16522018-10-28 20:27:54 -07002795 GPBInternalCompileAssert(sizeof(float) == sizeof(uint32_t), float_not_32_bits);
Brian Silverman9c614bc2016-02-15 20:20:02 -05002796 // These are all 32bit, just mix it in.
2797 uint32_t *valPtr = (uint32_t *)&storage[fieldOffset];
2798 result = prime * result + *valPtr;
2799 break;
2800 }
2801 case GPBDataTypeSFixed64:
2802 case GPBDataTypeInt64:
2803 case GPBDataTypeSInt64:
2804 case GPBDataTypeFixed64:
2805 case GPBDataTypeUInt64:
2806 case GPBDataTypeDouble: {
Austin Schuh40c16522018-10-28 20:27:54 -07002807 GPBInternalCompileAssert(sizeof(double) == sizeof(uint64_t), double_not_64_bits);
Brian Silverman9c614bc2016-02-15 20:20:02 -05002808 // These are all 64bit, just mix what fits into an NSUInteger in.
2809 uint64_t *valPtr = (uint64_t *)&storage[fieldOffset];
2810 result = prime * result + (NSUInteger)(*valPtr);
2811 break;
2812 }
2813 case GPBDataTypeBytes:
2814 case GPBDataTypeString: {
2815 // Type doesn't matter here, they both implement -hash:.
2816 id *valPtr = (id *)&storage[fieldOffset];
2817 result = prime * result + [*valPtr hash];
2818 break;
2819 }
2820
2821 case GPBDataTypeMessage:
2822 case GPBDataTypeGroup: {
2823 GPBMessage **valPtr = (GPBMessage **)&storage[fieldOffset];
2824 // Could call -hash on the sub message, but that could recurse pretty
2825 // deep; follow the lead of NSArray/NSDictionary and don't really
2826 // recurse for hash, instead use the field number and the descriptor
2827 // of the sub message. Yes, this could suck for a bunch of messages
2828 // where they all only differ in the sub messages, but if you are
2829 // using a message with sub messages for something that needs -hash,
2830 // odds are you are also copying them as keys, and that deep copy
2831 // will also suck.
2832 result = prime * result + GPBFieldNumber(field);
2833 result = prime * result + (NSUInteger)[[*valPtr class] descriptor];
2834 break;
2835 }
2836 } // switch()
2837 }
2838 }
2839
2840 // Unknowns and extensions are not included.
2841
2842 return result;
2843}
2844
2845#pragma mark - Description Support
2846
2847- (NSString *)description {
2848 NSString *textFormat = GPBTextFormatForMessage(self, @" ");
2849 NSString *description = [NSString
2850 stringWithFormat:@"<%@ %p>: {\n%@}", [self class], self, textFormat];
2851 return description;
2852}
2853
Austin Schuh40c16522018-10-28 20:27:54 -07002854#if defined(DEBUG) && DEBUG
Brian Silverman9c614bc2016-02-15 20:20:02 -05002855
2856// Xcode 5.1 added support for custom quick look info.
2857// https://developer.apple.com/library/ios/documentation/IDEs/Conceptual/CustomClassDisplay_in_QuickLook/CH01-quick_look_for_custom_objects/CH01-quick_look_for_custom_objects.html#//apple_ref/doc/uid/TP40014001-CH2-SW1
2858- (id)debugQuickLookObject {
2859 return GPBTextFormatForMessage(self, nil);
2860}
2861
2862#endif // DEBUG
2863
2864#pragma mark - SerializedSize
2865
2866- (size_t)serializedSize {
2867 GPBDescriptor *descriptor = [[self class] descriptor];
2868 size_t result = 0;
2869
2870 // Has check is done explicitly, so GPBGetObjectIvarWithFieldNoAutocreate()
2871 // avoids doing the has check again.
2872
2873 // Fields.
2874 for (GPBFieldDescriptor *fieldDescriptor in descriptor->fields_) {
2875 GPBFieldType fieldType = fieldDescriptor.fieldType;
2876 GPBDataType fieldDataType = GPBGetFieldDataType(fieldDescriptor);
2877
2878 // Single Fields
2879 if (fieldType == GPBFieldTypeSingle) {
2880 BOOL selfHas = GPBGetHasIvarField(self, fieldDescriptor);
2881 if (!selfHas) {
2882 continue; // Nothing to do.
2883 }
2884
2885 uint32_t fieldNumber = GPBFieldNumber(fieldDescriptor);
2886
2887 switch (fieldDataType) {
2888#define CASE_SINGLE_POD(NAME, TYPE, FUNC_TYPE) \
2889 case GPBDataType##NAME: { \
2890 TYPE fieldVal = GPBGetMessage##FUNC_TYPE##Field(self, fieldDescriptor); \
2891 result += GPBCompute##NAME##Size(fieldNumber, fieldVal); \
2892 break; \
2893 }
2894#define CASE_SINGLE_OBJECT(NAME) \
2895 case GPBDataType##NAME: { \
2896 id fieldVal = GPBGetObjectIvarWithFieldNoAutocreate(self, fieldDescriptor); \
2897 result += GPBCompute##NAME##Size(fieldNumber, fieldVal); \
2898 break; \
2899 }
2900 CASE_SINGLE_POD(Bool, BOOL, Bool)
2901 CASE_SINGLE_POD(Fixed32, uint32_t, UInt32)
2902 CASE_SINGLE_POD(SFixed32, int32_t, Int32)
2903 CASE_SINGLE_POD(Float, float, Float)
2904 CASE_SINGLE_POD(Fixed64, uint64_t, UInt64)
2905 CASE_SINGLE_POD(SFixed64, int64_t, Int64)
2906 CASE_SINGLE_POD(Double, double, Double)
2907 CASE_SINGLE_POD(Int32, int32_t, Int32)
2908 CASE_SINGLE_POD(Int64, int64_t, Int64)
2909 CASE_SINGLE_POD(SInt32, int32_t, Int32)
2910 CASE_SINGLE_POD(SInt64, int64_t, Int64)
2911 CASE_SINGLE_POD(UInt32, uint32_t, UInt32)
2912 CASE_SINGLE_POD(UInt64, uint64_t, UInt64)
2913 CASE_SINGLE_OBJECT(Bytes)
2914 CASE_SINGLE_OBJECT(String)
2915 CASE_SINGLE_OBJECT(Message)
2916 CASE_SINGLE_OBJECT(Group)
2917 CASE_SINGLE_POD(Enum, int32_t, Int32)
2918#undef CASE_SINGLE_POD
2919#undef CASE_SINGLE_OBJECT
2920 }
2921
2922 // Repeated Fields
2923 } else if (fieldType == GPBFieldTypeRepeated) {
2924 id genericArray =
2925 GPBGetObjectIvarWithFieldNoAutocreate(self, fieldDescriptor);
2926 NSUInteger count = [genericArray count];
2927 if (count == 0) {
2928 continue; // Nothing to add.
2929 }
2930 __block size_t dataSize = 0;
2931
2932 switch (fieldDataType) {
2933#define CASE_REPEATED_POD(NAME, TYPE, ARRAY_TYPE) \
2934 CASE_REPEATED_POD_EXTRA(NAME, TYPE, ARRAY_TYPE, )
2935#define CASE_REPEATED_POD_EXTRA(NAME, TYPE, ARRAY_TYPE, ARRAY_ACCESSOR_NAME) \
2936 case GPBDataType##NAME: { \
2937 GPB##ARRAY_TYPE##Array *array = genericArray; \
2938 [array enumerate##ARRAY_ACCESSOR_NAME##ValuesWithBlock:^(TYPE value, NSUInteger idx, BOOL *stop) { \
2939 _Pragma("unused(idx, stop)"); \
2940 dataSize += GPBCompute##NAME##SizeNoTag(value); \
2941 }]; \
2942 break; \
2943 }
2944#define CASE_REPEATED_OBJECT(NAME) \
2945 case GPBDataType##NAME: { \
2946 for (id value in genericArray) { \
2947 dataSize += GPBCompute##NAME##SizeNoTag(value); \
2948 } \
2949 break; \
2950 }
2951 CASE_REPEATED_POD(Bool, BOOL, Bool)
2952 CASE_REPEATED_POD(Fixed32, uint32_t, UInt32)
2953 CASE_REPEATED_POD(SFixed32, int32_t, Int32)
2954 CASE_REPEATED_POD(Float, float, Float)
2955 CASE_REPEATED_POD(Fixed64, uint64_t, UInt64)
2956 CASE_REPEATED_POD(SFixed64, int64_t, Int64)
2957 CASE_REPEATED_POD(Double, double, Double)
2958 CASE_REPEATED_POD(Int32, int32_t, Int32)
2959 CASE_REPEATED_POD(Int64, int64_t, Int64)
2960 CASE_REPEATED_POD(SInt32, int32_t, Int32)
2961 CASE_REPEATED_POD(SInt64, int64_t, Int64)
2962 CASE_REPEATED_POD(UInt32, uint32_t, UInt32)
2963 CASE_REPEATED_POD(UInt64, uint64_t, UInt64)
2964 CASE_REPEATED_OBJECT(Bytes)
2965 CASE_REPEATED_OBJECT(String)
2966 CASE_REPEATED_OBJECT(Message)
2967 CASE_REPEATED_OBJECT(Group)
2968 CASE_REPEATED_POD_EXTRA(Enum, int32_t, Enum, Raw)
2969#undef CASE_REPEATED_POD
2970#undef CASE_REPEATED_POD_EXTRA
2971#undef CASE_REPEATED_OBJECT
2972 } // switch
2973 result += dataSize;
2974 size_t tagSize = GPBComputeTagSize(GPBFieldNumber(fieldDescriptor));
2975 if (fieldDataType == GPBDataTypeGroup) {
2976 // Groups have both a start and an end tag.
2977 tagSize *= 2;
2978 }
2979 if (fieldDescriptor.isPackable) {
2980 result += tagSize;
2981 result += GPBComputeSizeTSizeAsInt32NoTag(dataSize);
2982 } else {
2983 result += count * tagSize;
2984 }
2985
2986 // Map<> Fields
2987 } else { // fieldType == GPBFieldTypeMap
2988 if (GPBDataTypeIsObject(fieldDataType) &&
2989 (fieldDescriptor.mapKeyDataType == GPBDataTypeString)) {
2990 // If key type was string, then the map is an NSDictionary.
2991 NSDictionary *map =
2992 GPBGetObjectIvarWithFieldNoAutocreate(self, fieldDescriptor);
2993 if (map) {
2994 result += GPBDictionaryComputeSizeInternalHelper(map, fieldDescriptor);
2995 }
2996 } else {
2997 // Type will be GPB*GroupDictionary, exact type doesn't matter.
2998 GPBInt32Int32Dictionary *map =
2999 GPBGetObjectIvarWithFieldNoAutocreate(self, fieldDescriptor);
3000 result += [map computeSerializedSizeAsField:fieldDescriptor];
3001 }
3002 }
3003 } // for(fields)
3004
3005 // Add any unknown fields.
3006 if (descriptor.wireFormat) {
3007 result += [unknownFields_ serializedSizeAsMessageSet];
3008 } else {
3009 result += [unknownFields_ serializedSize];
3010 }
3011
3012 // Add any extensions.
3013 for (GPBExtensionDescriptor *extension in extensionMap_) {
3014 id value = [extensionMap_ objectForKey:extension];
3015 result += GPBComputeExtensionSerializedSizeIncludingTag(extension, value);
3016 }
3017
3018 return result;
3019}
3020
3021#pragma mark - Resolve Methods Support
3022
3023typedef struct ResolveIvarAccessorMethodResult {
3024 IMP impToAdd;
3025 SEL encodingSelector;
3026} ResolveIvarAccessorMethodResult;
3027
Austin Schuh40c16522018-10-28 20:27:54 -07003028// |field| can be __unsafe_unretained because they are created at startup
3029// and are essentially global. No need to pay for retain/release when
3030// they are captured in blocks.
3031static void ResolveIvarGet(__unsafe_unretained GPBFieldDescriptor *field,
Brian Silverman9c614bc2016-02-15 20:20:02 -05003032 ResolveIvarAccessorMethodResult *result) {
3033 GPBDataType fieldDataType = GPBGetFieldDataType(field);
3034 switch (fieldDataType) {
3035#define CASE_GET(NAME, TYPE, TRUE_NAME) \
3036 case GPBDataType##NAME: { \
3037 result->impToAdd = imp_implementationWithBlock(^(id obj) { \
3038 return GPBGetMessage##TRUE_NAME##Field(obj, field); \
3039 }); \
3040 result->encodingSelector = @selector(get##NAME); \
3041 break; \
3042 }
3043#define CASE_GET_OBJECT(NAME, TYPE, TRUE_NAME) \
3044 case GPBDataType##NAME: { \
3045 result->impToAdd = imp_implementationWithBlock(^(id obj) { \
3046 return GPBGetObjectIvarWithField(obj, field); \
3047 }); \
3048 result->encodingSelector = @selector(get##NAME); \
3049 break; \
3050 }
3051 CASE_GET(Bool, BOOL, Bool)
3052 CASE_GET(Fixed32, uint32_t, UInt32)
3053 CASE_GET(SFixed32, int32_t, Int32)
3054 CASE_GET(Float, float, Float)
3055 CASE_GET(Fixed64, uint64_t, UInt64)
3056 CASE_GET(SFixed64, int64_t, Int64)
3057 CASE_GET(Double, double, Double)
3058 CASE_GET(Int32, int32_t, Int32)
3059 CASE_GET(Int64, int64_t, Int64)
3060 CASE_GET(SInt32, int32_t, Int32)
3061 CASE_GET(SInt64, int64_t, Int64)
3062 CASE_GET(UInt32, uint32_t, UInt32)
3063 CASE_GET(UInt64, uint64_t, UInt64)
3064 CASE_GET_OBJECT(Bytes, id, Object)
3065 CASE_GET_OBJECT(String, id, Object)
3066 CASE_GET_OBJECT(Message, id, Object)
3067 CASE_GET_OBJECT(Group, id, Object)
3068 CASE_GET(Enum, int32_t, Enum)
3069#undef CASE_GET
3070 }
3071}
3072
Austin Schuh40c16522018-10-28 20:27:54 -07003073// See comment about __unsafe_unretained on ResolveIvarGet.
3074static void ResolveIvarSet(__unsafe_unretained GPBFieldDescriptor *field,
Brian Silverman9c614bc2016-02-15 20:20:02 -05003075 GPBFileSyntax syntax,
3076 ResolveIvarAccessorMethodResult *result) {
3077 GPBDataType fieldDataType = GPBGetFieldDataType(field);
3078 switch (fieldDataType) {
3079#define CASE_SET(NAME, TYPE, TRUE_NAME) \
3080 case GPBDataType##NAME: { \
3081 result->impToAdd = imp_implementationWithBlock(^(id obj, TYPE value) { \
3082 return GPBSet##TRUE_NAME##IvarWithFieldInternal(obj, field, value, syntax); \
3083 }); \
3084 result->encodingSelector = @selector(set##NAME:); \
3085 break; \
3086 }
3087 CASE_SET(Bool, BOOL, Bool)
3088 CASE_SET(Fixed32, uint32_t, UInt32)
3089 CASE_SET(SFixed32, int32_t, Int32)
3090 CASE_SET(Float, float, Float)
3091 CASE_SET(Fixed64, uint64_t, UInt64)
3092 CASE_SET(SFixed64, int64_t, Int64)
3093 CASE_SET(Double, double, Double)
3094 CASE_SET(Int32, int32_t, Int32)
3095 CASE_SET(Int64, int64_t, Int64)
3096 CASE_SET(SInt32, int32_t, Int32)
3097 CASE_SET(SInt64, int64_t, Int64)
3098 CASE_SET(UInt32, uint32_t, UInt32)
3099 CASE_SET(UInt64, uint64_t, UInt64)
3100 CASE_SET(Bytes, id, Object)
3101 CASE_SET(String, id, Object)
3102 CASE_SET(Message, id, Object)
3103 CASE_SET(Group, id, Object)
3104 CASE_SET(Enum, int32_t, Enum)
3105#undef CASE_SET
3106 }
3107}
3108
3109+ (BOOL)resolveInstanceMethod:(SEL)sel {
3110 const GPBDescriptor *descriptor = [self descriptor];
3111 if (!descriptor) {
Austin Schuh40c16522018-10-28 20:27:54 -07003112 return [super resolveInstanceMethod:sel];
Brian Silverman9c614bc2016-02-15 20:20:02 -05003113 }
3114
3115 // NOTE: hasOrCountSel_/setHasSel_ will be NULL if the field for the given
3116 // message should not have has support (done in GPBDescriptor.m), so there is
3117 // no need for checks here to see if has*/setHas* are allowed.
Brian Silverman9c614bc2016-02-15 20:20:02 -05003118 ResolveIvarAccessorMethodResult result = {NULL, NULL};
Austin Schuh40c16522018-10-28 20:27:54 -07003119
3120 // See comment about __unsafe_unretained on ResolveIvarGet.
3121 for (__unsafe_unretained GPBFieldDescriptor *field in descriptor->fields_) {
Brian Silverman9c614bc2016-02-15 20:20:02 -05003122 BOOL isMapOrArray = GPBFieldIsMapOrArray(field);
3123 if (!isMapOrArray) {
3124 // Single fields.
3125 if (sel == field->getSel_) {
3126 ResolveIvarGet(field, &result);
3127 break;
3128 } else if (sel == field->setSel_) {
3129 ResolveIvarSet(field, descriptor.file.syntax, &result);
3130 break;
3131 } else if (sel == field->hasOrCountSel_) {
3132 int32_t index = GPBFieldHasIndex(field);
3133 uint32_t fieldNum = GPBFieldNumber(field);
3134 result.impToAdd = imp_implementationWithBlock(^(id obj) {
3135 return GPBGetHasIvar(obj, index, fieldNum);
3136 });
3137 result.encodingSelector = @selector(getBool);
3138 break;
3139 } else if (sel == field->setHasSel_) {
3140 result.impToAdd = imp_implementationWithBlock(^(id obj, BOOL value) {
3141 if (value) {
3142 [NSException raise:NSInvalidArgumentException
3143 format:@"%@: %@ can only be set to NO (to clear field).",
3144 [obj class],
3145 NSStringFromSelector(field->setHasSel_)];
3146 }
3147 GPBClearMessageField(obj, field);
3148 });
3149 result.encodingSelector = @selector(setBool:);
3150 break;
3151 } else {
3152 GPBOneofDescriptor *oneof = field->containingOneof_;
3153 if (oneof && (sel == oneof->caseSel_)) {
Austin Schuh40c16522018-10-28 20:27:54 -07003154 int32_t index = GPBFieldHasIndex(field);
Brian Silverman9c614bc2016-02-15 20:20:02 -05003155 result.impToAdd = imp_implementationWithBlock(^(id obj) {
3156 return GPBGetHasOneof(obj, index);
3157 });
3158 result.encodingSelector = @selector(getEnum);
3159 break;
3160 }
3161 }
3162 } else {
3163 // map<>/repeated fields.
3164 if (sel == field->getSel_) {
3165 if (field.fieldType == GPBFieldTypeRepeated) {
3166 result.impToAdd = imp_implementationWithBlock(^(id obj) {
3167 return GetArrayIvarWithField(obj, field);
3168 });
3169 } else {
3170 result.impToAdd = imp_implementationWithBlock(^(id obj) {
3171 return GetMapIvarWithField(obj, field);
3172 });
3173 }
3174 result.encodingSelector = @selector(getArray);
3175 break;
3176 } else if (sel == field->setSel_) {
3177 // Local for syntax so the block can directly capture it and not the
3178 // full lookup.
3179 const GPBFileSyntax syntax = descriptor.file.syntax;
3180 result.impToAdd = imp_implementationWithBlock(^(id obj, id value) {
3181 return GPBSetObjectIvarWithFieldInternal(obj, field, value, syntax);
3182 });
3183 result.encodingSelector = @selector(setArray:);
3184 break;
3185 } else if (sel == field->hasOrCountSel_) {
3186 result.impToAdd = imp_implementationWithBlock(^(id obj) {
3187 // Type doesn't matter, all *Array and *Dictionary types support
3188 // -count.
3189 NSArray *arrayOrMap =
3190 GPBGetObjectIvarWithFieldNoAutocreate(obj, field);
3191 return [arrayOrMap count];
3192 });
3193 result.encodingSelector = @selector(getArrayCount);
3194 break;
3195 }
3196 }
3197 }
3198 if (result.impToAdd) {
3199 const char *encoding =
3200 GPBMessageEncodingForSelector(result.encodingSelector, YES);
Austin Schuh40c16522018-10-28 20:27:54 -07003201 Class msgClass = descriptor.messageClass;
3202 BOOL methodAdded = class_addMethod(msgClass, sel, result.impToAdd, encoding);
3203 // class_addMethod() is documented as also failing if the method was already
3204 // added; so we check if the method is already there and return success so
3205 // the method dispatch will still happen. Why would it already be added?
3206 // Two threads could cause the same method to be bound at the same time,
3207 // but only one will actually bind it; the other still needs to return true
3208 // so things will dispatch.
3209 if (!methodAdded) {
3210 methodAdded = GPBClassHasSel(msgClass, sel);
3211 }
Brian Silverman9c614bc2016-02-15 20:20:02 -05003212 return methodAdded;
3213 }
3214 return [super resolveInstanceMethod:sel];
3215}
3216
3217+ (BOOL)resolveClassMethod:(SEL)sel {
3218 // Extensions scoped to a Message and looked up via class methods.
Austin Schuh40c16522018-10-28 20:27:54 -07003219 if (GPBResolveExtensionClassMethod([self descriptor].messageClass, sel)) {
Brian Silverman9c614bc2016-02-15 20:20:02 -05003220 return YES;
3221 }
3222 return [super resolveClassMethod:sel];
3223}
3224
3225#pragma mark - NSCoding Support
3226
3227+ (BOOL)supportsSecureCoding {
3228 return YES;
3229}
3230
3231- (instancetype)initWithCoder:(NSCoder *)aDecoder {
3232 self = [self init];
3233 if (self) {
3234 NSData *data =
3235 [aDecoder decodeObjectOfClass:[NSData class] forKey:kGPBDataCoderKey];
3236 if (data.length) {
3237 [self mergeFromData:data extensionRegistry:nil];
3238 }
3239 }
3240 return self;
3241}
3242
3243- (void)encodeWithCoder:(NSCoder *)aCoder {
3244 NSData *data = [self data];
3245 if (data.length) {
3246 [aCoder encodeObject:data forKey:kGPBDataCoderKey];
3247 }
3248}
3249
3250#pragma mark - KVC Support
3251
3252+ (BOOL)accessInstanceVariablesDirectly {
3253 // Make sure KVC doesn't use instance variables.
3254 return NO;
3255}
3256
3257@end
Austin Schuh40c16522018-10-28 20:27:54 -07003258
3259#pragma mark - Messages from GPBUtilities.h but defined here for access to helpers.
3260
3261// Only exists for public api, no core code should use this.
3262id GPBGetMessageRepeatedField(GPBMessage *self, GPBFieldDescriptor *field) {
3263#if defined(DEBUG) && DEBUG
3264 if (field.fieldType != GPBFieldTypeRepeated) {
3265 [NSException raise:NSInvalidArgumentException
3266 format:@"%@.%@ is not a repeated field.",
3267 [self class], field.name];
3268 }
3269#endif
3270 GPBDescriptor *descriptor = [[self class] descriptor];
3271 GPBFileSyntax syntax = descriptor.file.syntax;
3272 return GetOrCreateArrayIvarWithField(self, field, syntax);
3273}
3274
3275// Only exists for public api, no core code should use this.
3276id GPBGetMessageMapField(GPBMessage *self, GPBFieldDescriptor *field) {
3277#if defined(DEBUG) && DEBUG
3278 if (field.fieldType != GPBFieldTypeMap) {
3279 [NSException raise:NSInvalidArgumentException
3280 format:@"%@.%@ is not a map<> field.",
3281 [self class], field.name];
3282 }
3283#endif
3284 GPBDescriptor *descriptor = [[self class] descriptor];
3285 GPBFileSyntax syntax = descriptor.file.syntax;
3286 return GetOrCreateMapIvarWithField(self, field, syntax);
3287}
3288
3289id GPBGetObjectIvarWithField(GPBMessage *self, GPBFieldDescriptor *field) {
3290 NSCAssert(!GPBFieldIsMapOrArray(field), @"Shouldn't get here");
3291 if (GPBGetHasIvarField(self, field)) {
3292 uint8_t *storage = (uint8_t *)self->messageStorage_;
3293 id *typePtr = (id *)&storage[field->description_->offset];
3294 return *typePtr;
3295 }
3296 // Not set...
3297
3298 // Non messages (string/data), get their default.
3299 if (!GPBFieldDataTypeIsMessage(field)) {
3300 return field.defaultValue.valueMessage;
3301 }
3302
3303 GPBPrepareReadOnlySemaphore(self);
3304 dispatch_semaphore_wait(self->readOnlySemaphore_, DISPATCH_TIME_FOREVER);
3305 GPBMessage *result = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
3306 if (!result) {
3307 // For non repeated messages, create the object, set it and return it.
3308 // This object will not initially be visible via GPBGetHasIvar, so
3309 // we save its creator so it can become visible if it's mutated later.
3310 result = GPBCreateMessageWithAutocreator(field.msgClass, self, field);
3311 GPBSetAutocreatedRetainedObjectIvarWithField(self, field, result);
3312 }
3313 dispatch_semaphore_signal(self->readOnlySemaphore_);
3314 return result;
3315}
3316
3317#pragma clang diagnostic pop