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.
32 @implementation Object
51 #import <malloc/malloc.h>
55 #import "objc-runtime.h"
59 extern void _objc_error(id, const char *, va_list);
64 _errShouldHaveImp[] = "should have implemented the '%s' method.",
65 _errShouldNotImp[] = "should NOT have implemented the '%s' method.",
66 _errLeftUndone[] = "method '%s' not implemented",
67 _errBadSel[] = "method %s given invalid selector %s",
68 _errDoesntRecognize[] = "does not recognize selector %c%s";
71 @implementation Object
86 return class_poseAs(self, aFactory);
91 id newObject = (*_alloc)((Class)self, 0);
92 Class metaClass = self->isa;
93 if (class_getVersion(metaClass) > 1)
94 return [newObject init];
101 return (*_zoneAlloc)((Class)self, 0, malloc_default_zone());
104 + allocFromZone:(void *) z
106 return (*_zoneAlloc)((Class)self, 0, z);
116 return class_getName(isa);
121 return class_getName((Class)self);
126 return (unsigned)(((uintptr_t)self) >> 2);
129 - (BOOL)isEqual:anObject
131 return anObject == self;
136 return (*_dealloc)(self);
161 void *z = malloc_zone_from_ptr(self);
162 return z ? z : malloc_default_zone();
167 return class_getSuperclass((Class)self);
172 return class_getSuperclass(isa);
177 return class_getVersion((Class)self);
180 + setVersion: (int) aVersion
182 class_setVersion((Class)self, aVersion);
186 - (BOOL)isKindOf:aClass
189 for (cls = isa; cls; cls = class_getSuperclass(cls))
190 if (cls == (Class)aClass)
195 - (BOOL)isMemberOf:aClass
197 return isa == (Class)aClass;
200 - (BOOL)isKindOfClassNamed:(const char *)aClassName
203 for (cls = isa; cls; cls = class_getSuperclass(cls))
204 if (strcmp(aClassName, class_getName(cls)) == 0)
209 - (BOOL)isMemberOfClassNamed:(const char *)aClassName
211 return strcmp(aClassName, class_getName(isa)) == 0;
214 + (BOOL)instancesRespondTo:(SEL)aSelector
216 return class_respondsToMethod((Class)self, aSelector);
219 - (BOOL)respondsTo:(SEL)aSelector
221 return class_respondsToMethod(isa, aSelector);
226 return [self copyFromZone: [self zone]];
229 - copyFromZone:(void *)z
231 return (*_zoneCopy)(self, 0, z);
234 - (IMP)methodFor:(SEL)aSelector
236 return class_lookupMethod(isa, aSelector);
239 + (IMP)instanceMethodFor:(SEL)aSelector
241 return class_lookupMethod(self, aSelector);
244 - perform:(SEL)aSelector
247 return objc_msgSend(self, aSelector);
249 return [self error:_errBadSel, sel_getName(_cmd), aSelector];
252 - perform:(SEL)aSelector with:anObject
255 return objc_msgSend(self, aSelector, anObject);
257 return [self error:_errBadSel, sel_getName(_cmd), aSelector];
260 - perform:(SEL)aSelector with:obj1 with:obj2
263 return objc_msgSend(self, aSelector, obj1, obj2);
265 return [self error:_errBadSel, sel_getName(_cmd), aSelector];
268 - subclassResponsibility:(SEL)aSelector
270 return [self error:_errShouldHaveImp, sel_getName(aSelector)];
273 - notImplemented:(SEL)aSelector
275 return [self error:_errLeftUndone, sel_getName(aSelector)];
278 - doesNotRecognize:(SEL)aMessage
280 return [self error:_errDoesntRecognize,
281 class_isMetaClass(isa) ? '+' : '-', sel_getName(aMessage)];
284 - error:(const char *)aCStr, ...
288 (*_error)(self, aCStr, ap);
289 _objc_error (self, aCStr, ap); /* In case (*_error)() returns. */
294 - (void) printForDebugger:(void *)stream
298 - write:(void *) stream
303 - read:(void *) stream
308 - forward: (SEL) sel : (marg_list) args
310 return [self doesNotRecognize: sel];
313 /* this method is not part of the published API */
315 - (unsigned)methodArgSize:(SEL)sel
317 Method method = class_getInstanceMethod((Class)isa, sel);
318 if (! method) return 0;
319 return method_getSizeOfArguments(method);
322 - performv: (SEL) sel : (marg_list) args
326 // Messages to nil object always return nil
327 if (! self) return nil;
329 // Calculate size of the marg_list from the method's
330 // signature. This looks for the method in self
331 // and its superclasses.
332 size = [self methodArgSize: sel];
334 // If neither self nor its superclasses implement
335 // it, forward the message because self might know
336 // someone who does. This is a "chained" forward...
337 if (! size) return [self forward: sel: args];
339 // Message self with the specified selector and arguments
340 return objc_msgSendv (self, sel, size, args);
343 /* Testing protocol conformance */
345 - (BOOL) conformsTo: (Protocol *)aProtocolObj
347 return [(id)isa conformsTo:aProtocolObj];
350 + (BOOL) conformsTo: (Protocol *)aProtocolObj
353 for (class = self; class; class = class_getSuperclass(class))
355 if (class_conformsToProtocol(class, aProtocolObj)) return YES;
361 /* Looking up information for a method */
363 - (struct objc_method_description *) descriptionForMethod:(SEL)aSelector
366 struct objc_method_description *m;
368 /* Look in the protocols first. */
369 for (cls = isa; cls; cls = cls->super_class)
371 if (cls->isa->version >= 3)
373 struct objc_protocol_list *protocols = cls->protocols;
379 for (i = 0; i < protocols->count; i++)
381 Protocol *p = protocols->list[i];
383 if (class_isMetaClass(cls))
384 m = [p descriptionForClassMethod:aSelector];
386 m = [p descriptionForInstanceMethod:aSelector];
393 if (cls->isa->version <= 4)
396 protocols = protocols->next;
401 /* Then try the class implementations. */
402 for (cls = isa; cls; cls = cls->super_class) {
405 struct objc_method_list *mlist;
406 while ( (mlist = class_nextMethodList( cls, &iterator )) ) {
407 for (i = 0; i < mlist->method_count; i++)
408 if (mlist->method_list[i].method_name == aSelector) {
409 struct objc_method_description *m;
410 m = (struct objc_method_description *)&mlist->method_list[i];
418 + (struct objc_method_description *) descriptionForInstanceMethod:(SEL)aSelector
422 /* Look in the protocols first. */
423 for (cls = self; cls; cls = cls->super_class)
425 if (cls->isa->version >= 3)
427 struct objc_protocol_list *protocols = cls->protocols;
433 for (i = 0; i < protocols->count; i++)
435 Protocol *p = protocols->list[i];
436 struct objc_method_description *m;
438 if ((m = [p descriptionForInstanceMethod:aSelector]))
442 if (cls->isa->version <= 4)
445 protocols = protocols->next;
450 /* Then try the class implementations. */
451 for (cls = self; cls; cls = cls->super_class) {
454 struct objc_method_list *mlist;
455 while ( (mlist = class_nextMethodList( cls, &iterator )) ) {
456 for (i = 0; i < mlist->method_count; i++)
457 if (mlist->method_list[i].method_name == aSelector) {
458 struct objc_method_description *m;
459 m = (struct objc_method_description *)&mlist->method_list[i];
468 /* Obsolete methods (for binary compatibility only). */
472 return [self superclass];
477 return [self superclass];
480 - (BOOL)isKindOfGivenName:(const char *)aClassName
482 return [self isKindOfClassNamed: aClassName];
485 - (BOOL)isMemberOfGivenName:(const char *)aClassName
487 return [self isMemberOfClassNamed: aClassName];
490 - (struct objc_method_description *) methodDescFor:(SEL)aSelector
492 return [self descriptionForMethod: aSelector];
495 + (struct objc_method_description *) instanceMethodDescFor:(SEL)aSelector
497 return [self descriptionForInstanceMethod: aSelector];
500 - findClass:(const char *)aClassName
502 return objc_lookUpClass(aClassName);
505 - shouldNotImplement:(SEL)aSelector
507 return [self error:_errShouldNotImp, sel_getName(aSelector)];