2 * Copyright (c) 1999-2007 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
25 Copyright 1988-1996 NeXT Software, Inc.
28 #include "objc-private.h"
33 typedef struct objc_class *Class;
34 typedef struct objc_object *id;
39 __IOS_UNAVAILABLE __TVOS_UNAVAILABLE
40 __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE
47 @implementation Object
61 return _objc_rootRetain(self);
66 _objc_rootRelease(self);
71 return _objc_rootAutorelease(self);
99 #include <malloc/malloc.h>
102 #include "Protocol.h"
103 #include "objc-runtime.h"
108 _errShouldHaveImp[] = "should have implemented the '%s' method.",
109 _errShouldNotImp[] = "should NOT have implemented the '%s' method.",
110 _errLeftUndone[] = "method '%s' not implemented",
111 _errBadSel[] = "method %s given invalid selector %s",
112 _errDoesntRecognize[] = "does not recognize selector %c%s";
115 @implementation Object
128 + (id)poseAs: aFactory
130 return class_poseAs(self, aFactory);
135 id newObject = (*_alloc)((Class)self, 0);
136 Class metaClass = self->ISA();
137 if (class_getVersion(metaClass) > 1)
138 return [newObject init];
145 return (*_zoneAlloc)((Class)self, 0, malloc_default_zone());
148 + (id)allocFromZone:(void *) z
150 return (*_zoneAlloc)((Class)self, 0, z);
160 return class_getName(isa);
165 return class_getName((Class)self);
170 return (unsigned)(((uintptr_t)self) >> 2);
173 - (BOOL)isEqual:anObject
175 return anObject == self;
180 return (*_dealloc)(self);
206 void *z = malloc_zone_from_ptr(self);
207 return z ? z : malloc_default_zone();
212 return self->superclass;
217 return isa->superclass;
222 return class_getVersion((Class)self);
225 + (id)setVersion: (int) aVersion
227 class_setVersion((Class)self, aVersion);
231 - (BOOL)isKindOf:aClass
234 for (cls = isa; cls; cls = cls->superclass)
235 if (cls == (Class)aClass)
240 - (BOOL)isMemberOf:aClass
242 return isa == (Class)aClass;
245 - (BOOL)isKindOfClassNamed:(const char *)aClassName
248 for (cls = isa; cls; cls = cls->superclass)
249 if (strcmp(aClassName, class_getName(cls)) == 0)
254 - (BOOL)isMemberOfClassNamed:(const char *)aClassName
256 return strcmp(aClassName, class_getName(isa)) == 0;
259 + (BOOL)instancesRespondTo:(SEL)aSelector
261 return class_respondsToMethod((Class)self, aSelector);
264 - (BOOL)respondsTo:(SEL)aSelector
266 return class_respondsToMethod(isa, aSelector);
271 return [self copyFromZone: [self zone]];
274 - (id)copyFromZone:(void *)z
276 return (*_zoneCopy)(self, 0, z);
279 - (IMP)methodFor:(SEL)aSelector
281 return class_lookupMethod(isa, aSelector);
284 + (IMP)instanceMethodFor:(SEL)aSelector
286 return class_lookupMethod(self, aSelector);
289 - (id)perform:(SEL)aSelector
292 return ((id(*)(id, SEL))objc_msgSend)(self, aSelector);
294 return [self error:_errBadSel, sel_getName(_cmd), aSelector];
297 - (id)perform:(SEL)aSelector with:anObject
300 return ((id(*)(id, SEL, id))objc_msgSend)(self, aSelector, anObject);
302 return [self error:_errBadSel, sel_getName(_cmd), aSelector];
305 - (id)perform:(SEL)aSelector with:obj1 with:obj2
308 return ((id(*)(id, SEL, id, id))objc_msgSend)(self, aSelector, obj1, obj2);
310 return [self error:_errBadSel, sel_getName(_cmd), aSelector];
313 - (id)subclassResponsibility:(SEL)aSelector
315 return [self error:_errShouldHaveImp, sel_getName(aSelector)];
318 - (id)notImplemented:(SEL)aSelector
320 return [self error:_errLeftUndone, sel_getName(aSelector)];
323 - (id)doesNotRecognize:(SEL)aMessage
325 return [self error:_errDoesntRecognize,
326 class_isMetaClass(isa) ? '+' : '-', sel_getName(aMessage)];
329 - (id)error:(const char *)aCStr, ...
333 (*_error)(self, aCStr, ap);
334 _objc_error (self, aCStr, ap); /* In case (*_error)() returns. */
339 - (void) printForDebugger:(void *)stream
343 - (id)write:(void *) stream
348 - (id)read:(void *) stream
353 - (id)forward: (SEL) sel : (marg_list) args
355 return [self doesNotRecognize: sel];
358 /* this method is not part of the published API */
360 - (unsigned)methodArgSize:(SEL)sel
362 Method method = class_getInstanceMethod((Class)isa, sel);
363 if (! method) return 0;
364 return method_getSizeOfArguments(method);
367 - (id)performv: (SEL) sel : (marg_list) args
371 // Messages to nil object always return nil
372 if (! self) return nil;
374 // Calculate size of the marg_list from the method's
375 // signature. This looks for the method in self
376 // and its superclasses.
377 size = [self methodArgSize: sel];
379 // If neither self nor its superclasses implement
380 // it, forward the message because self might know
381 // someone who does. This is a "chained" forward...
382 if (! size) return [self forward: sel: args];
384 // Message self with the specified selector and arguments
385 return objc_msgSendv (self, sel, size, args);
388 /* Testing protocol conformance */
390 - (BOOL) conformsTo: (Protocol *)aProtocolObj
392 return [(id)isa conformsTo:aProtocolObj];
395 + (BOOL) conformsTo: (Protocol *)aProtocolObj
398 for (cls = self; cls; cls = cls->superclass)
400 if (class_conformsToProtocol(cls, aProtocolObj)) return YES;
406 /* Looking up information for a method */
408 - (struct objc_method_description *) descriptionForMethod:(SEL)aSelector
411 struct objc_method_description *m;
413 /* Look in the protocols first. */
414 for (cls = isa; cls; cls = cls->superclass)
416 if (cls->ISA()->version >= 3)
418 struct objc_protocol_list *protocols =
419 (struct objc_protocol_list *)cls->protocols;
425 for (i = 0; i < protocols->count; i++)
427 Protocol *p = protocols->list[i];
429 if (class_isMetaClass(cls))
430 m = [p descriptionForClassMethod:aSelector];
432 m = [p descriptionForInstanceMethod:aSelector];
439 if (cls->ISA()->version <= 4)
442 protocols = protocols->next;
447 /* Then try the class implementations. */
448 for (cls = isa; cls; cls = cls->superclass) {
451 struct objc_method_list *mlist;
452 while ( (mlist = class_nextMethodList( cls, &iterator )) ) {
453 for (i = 0; i < mlist->method_count; i++)
454 if (mlist->method_list[i].method_name == aSelector) {
455 m = (struct objc_method_description *)&mlist->method_list[i];
463 + (struct objc_method_description *) descriptionForInstanceMethod:(SEL)aSelector
467 /* Look in the protocols first. */
468 for (cls = self; cls; cls = cls->superclass)
470 if (cls->ISA()->version >= 3)
472 struct objc_protocol_list *protocols =
473 (struct objc_protocol_list *)cls->protocols;
479 for (i = 0; i < protocols->count; i++)
481 Protocol *p = protocols->list[i];
482 struct objc_method_description *m;
484 if ((m = [p descriptionForInstanceMethod:aSelector]))
488 if (cls->ISA()->version <= 4)
491 protocols = protocols->next;
496 /* Then try the class implementations. */
497 for (cls = self; cls; cls = cls->superclass) {
500 struct objc_method_list *mlist;
501 while ( (mlist = class_nextMethodList( cls, &iterator )) ) {
502 for (i = 0; i < mlist->method_count; i++)
503 if (mlist->method_list[i].method_name == aSelector) {
504 struct objc_method_description *m;
505 m = (struct objc_method_description *)&mlist->method_list[i];
514 /* Obsolete methods (for binary compatibility only). */
518 return [self superclass];
523 return [self superclass];
526 - (BOOL)isKindOfGivenName:(const char *)aClassName
528 return [self isKindOfClassNamed: aClassName];
531 - (BOOL)isMemberOfGivenName:(const char *)aClassName
533 return [self isMemberOfClassNamed: aClassName];
536 - (struct objc_method_description *) methodDescFor:(SEL)aSelector
538 return [self descriptionForMethod: aSelector];
541 + (struct objc_method_description *) instanceMethodDescFor:(SEL)aSelector
543 return [self descriptionForInstanceMethod: aSelector];
546 - (id)findClass:(const char *)aClassName
548 return objc_lookUpClass(aClassName);
551 - (id)shouldNotImplement:(SEL)aSelector
553 return [self error:_errShouldNotImp, sel_getName(aSelector)];