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;
38 @implementation Object
52 return _objc_rootRetain(self);
57 _objc_rootRelease(self);
62 return _objc_rootAutorelease(self);
90 #include <malloc/malloc.h>
94 #include "objc-runtime.h"
99 _errShouldHaveImp[] = "should have implemented the '%s' method.",
100 _errShouldNotImp[] = "should NOT have implemented the '%s' method.",
101 _errLeftUndone[] = "method '%s' not implemented",
102 _errBadSel[] = "method %s given invalid selector %s",
103 _errDoesntRecognize[] = "does not recognize selector %c%s";
106 @implementation Object
119 + (id)poseAs: aFactory
121 return class_poseAs(self, aFactory);
126 id newObject = (*_alloc)((Class)self, 0);
127 Class metaClass = self->ISA();
128 if (class_getVersion(metaClass) > 1)
129 return [newObject init];
136 return (*_zoneAlloc)((Class)self, 0, malloc_default_zone());
139 + (id)allocFromZone:(void *) z
141 return (*_zoneAlloc)((Class)self, 0, z);
151 return class_getName(isa);
156 return class_getName((Class)self);
161 return (unsigned)(((uintptr_t)self) >> 2);
164 - (BOOL)isEqual:anObject
166 return anObject == self;
171 return (*_dealloc)(self);
197 void *z = malloc_zone_from_ptr(self);
198 return z ? z : malloc_default_zone();
203 return self->superclass;
208 return isa->superclass;
213 return class_getVersion((Class)self);
216 + (id)setVersion: (int) aVersion
218 class_setVersion((Class)self, aVersion);
222 - (BOOL)isKindOf:aClass
225 for (cls = isa; cls; cls = cls->superclass)
226 if (cls == (Class)aClass)
231 - (BOOL)isMemberOf:aClass
233 return isa == (Class)aClass;
236 - (BOOL)isKindOfClassNamed:(const char *)aClassName
239 for (cls = isa; cls; cls = cls->superclass)
240 if (strcmp(aClassName, class_getName(cls)) == 0)
245 - (BOOL)isMemberOfClassNamed:(const char *)aClassName
247 return strcmp(aClassName, class_getName(isa)) == 0;
250 + (BOOL)instancesRespondTo:(SEL)aSelector
252 return class_respondsToMethod((Class)self, aSelector);
255 - (BOOL)respondsTo:(SEL)aSelector
257 return class_respondsToMethod(isa, aSelector);
262 return [self copyFromZone: [self zone]];
265 - (id)copyFromZone:(void *)z
267 return (*_zoneCopy)(self, 0, z);
270 - (IMP)methodFor:(SEL)aSelector
272 return class_lookupMethod(isa, aSelector);
275 + (IMP)instanceMethodFor:(SEL)aSelector
277 return class_lookupMethod(self, aSelector);
280 - (id)perform:(SEL)aSelector
283 return ((id(*)(id, SEL))objc_msgSend)(self, aSelector);
285 return [self error:_errBadSel, sel_getName(_cmd), aSelector];
288 - (id)perform:(SEL)aSelector with:anObject
291 return ((id(*)(id, SEL, id))objc_msgSend)(self, aSelector, anObject);
293 return [self error:_errBadSel, sel_getName(_cmd), aSelector];
296 - (id)perform:(SEL)aSelector with:obj1 with:obj2
299 return ((id(*)(id, SEL, id, id))objc_msgSend)(self, aSelector, obj1, obj2);
301 return [self error:_errBadSel, sel_getName(_cmd), aSelector];
304 - (id)subclassResponsibility:(SEL)aSelector
306 return [self error:_errShouldHaveImp, sel_getName(aSelector)];
309 - (id)notImplemented:(SEL)aSelector
311 return [self error:_errLeftUndone, sel_getName(aSelector)];
314 - (id)doesNotRecognize:(SEL)aMessage
316 return [self error:_errDoesntRecognize,
317 class_isMetaClass(isa) ? '+' : '-', sel_getName(aMessage)];
320 - (id)error:(const char *)aCStr, ...
324 (*_error)(self, aCStr, ap);
325 _objc_error (self, aCStr, ap); /* In case (*_error)() returns. */
330 - (void) printForDebugger:(void *)stream
334 - (id)write:(void *) stream
339 - (id)read:(void *) stream
344 - (id)forward: (SEL) sel : (marg_list) args
346 return [self doesNotRecognize: sel];
349 /* this method is not part of the published API */
351 - (unsigned)methodArgSize:(SEL)sel
353 Method method = class_getInstanceMethod((Class)isa, sel);
354 if (! method) return 0;
355 return method_getSizeOfArguments(method);
358 - (id)performv: (SEL) sel : (marg_list) args
362 // Messages to nil object always return nil
363 if (! self) return nil;
365 // Calculate size of the marg_list from the method's
366 // signature. This looks for the method in self
367 // and its superclasses.
368 size = [self methodArgSize: sel];
370 // If neither self nor its superclasses implement
371 // it, forward the message because self might know
372 // someone who does. This is a "chained" forward...
373 if (! size) return [self forward: sel: args];
375 // Message self with the specified selector and arguments
376 return objc_msgSendv (self, sel, size, args);
379 /* Testing protocol conformance */
381 - (BOOL) conformsTo: (Protocol *)aProtocolObj
383 return [(id)isa conformsTo:aProtocolObj];
386 + (BOOL) conformsTo: (Protocol *)aProtocolObj
389 for (cls = self; cls; cls = cls->superclass)
391 if (class_conformsToProtocol(cls, aProtocolObj)) return YES;
397 /* Looking up information for a method */
399 - (struct objc_method_description *) descriptionForMethod:(SEL)aSelector
402 struct objc_method_description *m;
404 /* Look in the protocols first. */
405 for (cls = isa; cls; cls = cls->superclass)
407 if (cls->ISA()->version >= 3)
409 struct objc_protocol_list *protocols =
410 (struct objc_protocol_list *)cls->protocols;
416 for (i = 0; i < protocols->count; i++)
418 Protocol *p = protocols->list[i];
420 if (class_isMetaClass(cls))
421 m = [p descriptionForClassMethod:aSelector];
423 m = [p descriptionForInstanceMethod:aSelector];
430 if (cls->ISA()->version <= 4)
433 protocols = protocols->next;
438 /* Then try the class implementations. */
439 for (cls = isa; cls; cls = cls->superclass) {
442 struct objc_method_list *mlist;
443 while ( (mlist = class_nextMethodList( cls, &iterator )) ) {
444 for (i = 0; i < mlist->method_count; i++)
445 if (mlist->method_list[i].method_name == aSelector) {
446 m = (struct objc_method_description *)&mlist->method_list[i];
454 + (struct objc_method_description *) descriptionForInstanceMethod:(SEL)aSelector
458 /* Look in the protocols first. */
459 for (cls = self; cls; cls = cls->superclass)
461 if (cls->ISA()->version >= 3)
463 struct objc_protocol_list *protocols =
464 (struct objc_protocol_list *)cls->protocols;
470 for (i = 0; i < protocols->count; i++)
472 Protocol *p = protocols->list[i];
473 struct objc_method_description *m;
475 if ((m = [p descriptionForInstanceMethod:aSelector]))
479 if (cls->ISA()->version <= 4)
482 protocols = protocols->next;
487 /* Then try the class implementations. */
488 for (cls = self; cls; cls = cls->superclass) {
491 struct objc_method_list *mlist;
492 while ( (mlist = class_nextMethodList( cls, &iterator )) ) {
493 for (i = 0; i < mlist->method_count; i++)
494 if (mlist->method_list[i].method_name == aSelector) {
495 struct objc_method_description *m;
496 m = (struct objc_method_description *)&mlist->method_list[i];
505 /* Obsolete methods (for binary compatibility only). */
509 return [self superclass];
514 return [self superclass];
517 - (BOOL)isKindOfGivenName:(const char *)aClassName
519 return [self isKindOfClassNamed: aClassName];
522 - (BOOL)isMemberOfGivenName:(const char *)aClassName
524 return [self isMemberOfClassNamed: aClassName];
527 - (struct objc_method_description *) methodDescFor:(SEL)aSelector
529 return [self descriptionForMethod: aSelector];
532 + (struct objc_method_description *) instanceMethodDescFor:(SEL)aSelector
534 return [self descriptionForInstanceMethod: aSelector];
537 - (id)findClass:(const char *)aClassName
539 return objc_lookUpClass(aClassName);
542 - (id)shouldNotImplement:(SEL)aSelector
544 return [self error:_errShouldNotImp, sel_getName(aSelector)];