1 /* The implementation of class Object for Objective-C.
2 Copyright (C) 1993, 1994, 1995, 1997, 2002, 2009 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 3, or (at your option) any
11 GCC is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
14 License for more details.
16 Under Section 7 of GPL version 3, you are granted additional
17 permissions described in the GCC Runtime Library Exception, version
18 3.1, as published by the Free Software Foundation.
20 You should have received a copy of the GNU General Public License and
21 a copy of the GCC Runtime Library Exception along with this program;
22 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 <http://www.gnu.org/licenses/>. */
27 #include "objc/Object.h"
28 #include "objc/Protocol.h"
29 #include "objc/objc-api.h"
31 #define MAX_CLASS_NAME_LEN 256
33 @implementation Object
47 return [[self alloc] init];
52 return class_create_instance(self);
57 return object_dispose(self);
62 return [[self shallowCopy] deepen];
67 return object_copy(self);
82 return object_get_class(self);
87 return object_get_super_class(self);
90 - (MetaClass)metaClass
92 return object_get_meta_class(self);
97 return object_get_class_name(self);
110 - (BOOL)isEqual:anObject
112 return self==anObject;
115 - (int)compare:(id)anotherObject;
117 if ([self isEqual:anotherObject])
119 // Ordering objects by their address is pretty useless,
120 // so subclasses should override this is some useful way.
121 else if ((id)self > anotherObject)
134 return object_is_class(self);
139 return object_is_instance(self);
142 - (BOOL)isKindOf:(Class)aClassObject
146 for (class = self->isa; class!=Nil; class = class_get_super_class(class))
147 if (class==aClassObject)
152 - (BOOL)isMemberOf:(Class)aClassObject
154 return self->isa==aClassObject;
157 - (BOOL)isKindOfClassNamed:(const char *)aClassName
161 if (aClassName!=NULL)
162 for (class = self->isa; class!=Nil; class = class_get_super_class(class))
163 if (!strcmp(class_get_class_name(class), aClassName))
168 - (BOOL)isMemberOfClassNamed:(const char *)aClassName
170 return ((aClassName!=NULL)
171 &&!strcmp(class_get_class_name(self->isa), aClassName));
174 + (BOOL)instancesRespondTo:(SEL)aSel
176 return class_get_instance_method(self, aSel)!=METHOD_NULL;
179 - (BOOL)respondsTo:(SEL)aSel
181 return ((object_is_instance(self)
182 ?class_get_instance_method(self->isa, aSel)
183 :class_get_class_method(self->isa, aSel))!=METHOD_NULL);
186 + (IMP)instanceMethodFor:(SEL)aSel
188 return method_get_imp(class_get_instance_method(self, aSel));
191 // Indicates if the receiving class or instance conforms to the given protocol
192 // not usually overridden by subclasses
194 // Modified 9/5/94 to always search the class object's protocol list, rather
195 // than the meta class.
197 + (BOOL) conformsTo: (Protocol*)aProtocol
200 struct objc_protocol_list* proto_list;
203 for (proto_list = ((Class)self)->protocols;
204 proto_list; proto_list = proto_list->next)
206 for (i=0; i < proto_list->count; i++)
208 if ([proto_list->list[i] conformsTo: aProtocol])
213 if ((parent = [self superClass]))
214 return [parent conformsTo: aProtocol];
219 - (BOOL) conformsTo: (Protocol*)aProtocol
221 return [[self class] conformsTo:aProtocol];
224 - (IMP)methodFor:(SEL)aSel
226 return (method_get_imp(object_is_instance(self)
227 ?class_get_instance_method(self->isa, aSel)
228 :class_get_class_method(self->isa, aSel)));
231 + (struct objc_method_description *)descriptionForInstanceMethod:(SEL)aSel
233 return ((struct objc_method_description *)
234 class_get_instance_method(self, aSel));
237 - (struct objc_method_description *)descriptionForMethod:(SEL)aSel
239 return ((struct objc_method_description *)
240 (object_is_instance(self)
241 ?class_get_instance_method(self->isa, aSel)
242 :class_get_class_method(self->isa, aSel)));
247 IMP msg = objc_msg_lookup(self, aSel);
249 return [self error:"invalid selector passed to %s", sel_get_name(_cmd)];
250 return (*msg)(self, aSel);
253 - perform:(SEL)aSel with:anObject
255 IMP msg = objc_msg_lookup(self, aSel);
257 return [self error:"invalid selector passed to %s", sel_get_name(_cmd)];
258 return (*msg)(self, aSel, anObject);
261 - perform:(SEL)aSel with:anObject1 with:anObject2
263 IMP msg = objc_msg_lookup(self, aSel);
265 return [self error:"invalid selector passed to %s", sel_get_name(_cmd)];
266 return (*msg)(self, aSel, anObject1, anObject2);
269 - (retval_t)forward:(SEL)aSel :(arglist_t)argFrame
271 (void) argFrame; /* UNUSED */
272 return (retval_t)[self doesNotRecognize: aSel];
275 - (retval_t)performv:(SEL)aSel :(arglist_t)argFrame
277 return objc_msg_sendv(self, aSel, argFrame);
280 + poseAs:(Class)aClassObject
282 return class_pose_as(self, aClassObject);
285 - (Class)transmuteClassTo:(Class)aClassObject
287 if (object_is_instance(self))
288 if (class_is_class(aClassObject))
289 if (class_get_instance_size(aClassObject)==class_get_instance_size(isa))
290 if ([self isKindOf:aClassObject])
299 - subclassResponsibility:(SEL)aSel
301 return [self error:"subclass should override %s", sel_get_name(aSel)];
304 - notImplemented:(SEL)aSel
306 return [self error:"method %s not implemented", sel_get_name(aSel)];
309 - shouldNotImplement:(SEL)aSel
311 return [self error:"%s should not implement %s",
312 object_get_class_name(self), sel_get_name(aSel)];
315 - doesNotRecognize:(SEL)aSel
317 return [self error:"%s does not recognize %s",
318 object_get_class_name(self), sel_get_name(aSel)];
321 - error:(const char *)aString, ...
323 #define FMT "error: %s (%s)\n%s\n"
324 char fmt[(strlen((char*)FMT)+strlen((char*)object_get_class_name(self))
325 +((aString!=NULL)?strlen((char*)aString):0)+8)];
328 sprintf(fmt, FMT, object_get_class_name(self),
329 object_is_instance(self)?"instance":"class",
330 (aString!=NULL)?aString:"");
331 va_start(ap, aString);
332 objc_verror(self, OBJC_ERR_UNKNOWN, fmt, ap);
340 return class_get_version(self);
343 + setVersion:(int)aVersion
345 class_set_version(self, aVersion);
349 + (int)streamVersion: (TypedStream*)aStream
351 if (aStream->mode == OBJC_READONLY)
352 return objc_get_stream_class_version (aStream, self);
354 return class_get_version (self);
357 // These are used to write or read the instance variables
358 // declared in this particular part of the object. Subclasses
359 // should extend these, by calling [super read/write: aStream]
360 // before doing their own archiving. These methods are private, in
361 // the sense that they should only be called from subclasses.
363 - read: (TypedStream*)aStream
365 (void) aStream; /* UNUSED */
366 // [super read: aStream];
370 - write: (TypedStream*)aStream
372 (void) aStream; /* UNUSED */
373 // [super write: aStream];