1 /* The implementation of class Object for Objective-C.
2 Copyright (C) 1993, 1994, 1995, 1997, 2002, 2009, 2010
3 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3, or (at your option) any
12 GCC is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
15 License for more details.
17 Under Section 7 of GPL version 3, you are granted additional
18 permissions described in the GCC Runtime Library Exception, version
19 3.1, as published by the Free Software Foundation.
21 You should have received a copy of the GNU General Public License and
22 a copy of the GCC Runtime Library Exception along with this program;
23 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 <http://www.gnu.org/licenses/>. */
26 #include "objc-private/common.h"
29 #include "objc/Object.h"
30 #include "objc/Protocol.h"
31 #include "objc/objc-api.h"
33 @implementation Object
37 return object_get_class (self);
40 - (BOOL)isEqual: (id)anObject
42 return self == anObject;
47 /* The following methods were deprecated in GCC 4.6.0 and will be
48 removed in the next GCC release. */
49 @implementation Object (Deprecated)
63 return [[self alloc] init];
68 return class_create_instance(self);
73 return object_dispose(self);
78 return [[self shallowCopy] deepen];
83 return object_copy(self);
98 return object_get_super_class(self);
101 - (MetaClass)metaClass
103 return object_get_meta_class(self);
108 return object_get_class_name(self);
121 - (int)compare:(id)anotherObject;
123 if ([self isEqual:anotherObject])
125 // Ordering objects by their address is pretty useless,
126 // so subclasses should override this is some useful way.
127 else if ((id)self > anotherObject)
140 return object_is_class(self);
145 return object_is_instance(self);
148 - (BOOL)isKindOf:(Class)aClassObject
152 for (class = self->isa; class!=Nil; class = class_get_super_class(class))
153 if (class==aClassObject)
158 - (BOOL)isMemberOf:(Class)aClassObject
160 return self->isa==aClassObject;
163 - (BOOL)isKindOfClassNamed:(const char *)aClassName
167 if (aClassName!=NULL)
168 for (class = self->isa; class!=Nil; class = class_get_super_class(class))
169 if (!strcmp(class_get_class_name(class), aClassName))
174 - (BOOL)isMemberOfClassNamed:(const char *)aClassName
176 return ((aClassName!=NULL)
177 &&!strcmp(class_get_class_name(self->isa), aClassName));
180 + (BOOL)instancesRespondTo:(SEL)aSel
182 return class_get_instance_method(self, aSel) != (Method_t)0;
185 - (BOOL)respondsTo:(SEL)aSel
187 return ((object_is_instance(self)
188 ?class_get_instance_method(self->isa, aSel)
189 :class_get_class_method(self->isa, aSel)) != (Method_t)0);
192 + (IMP)instanceMethodFor:(SEL)aSel
194 return method_get_imp(class_get_instance_method(self, aSel));
197 // Indicates if the receiving class or instance conforms to the given protocol
198 // not usually overridden by subclasses
200 // Modified 9/5/94 to always search the class object's protocol list, rather
201 // than the meta class.
203 + (BOOL) conformsTo: (Protocol*)aProtocol
206 struct objc_protocol_list* proto_list;
209 for (proto_list = ((Class)self)->protocols;
210 proto_list; proto_list = proto_list->next)
212 for (i=0; i < proto_list->count; i++)
214 if ([proto_list->list[i] conformsTo: aProtocol])
219 if ((parent = [self superClass]))
220 return [parent conformsTo: aProtocol];
225 - (BOOL) conformsTo: (Protocol*)aProtocol
227 return [[self class] conformsTo:aProtocol];
230 - (IMP)methodFor:(SEL)aSel
232 return (method_get_imp(object_is_instance(self)
233 ?class_get_instance_method(self->isa, aSel)
234 :class_get_class_method(self->isa, aSel)));
237 + (struct objc_method_description *)descriptionForInstanceMethod:(SEL)aSel
239 return ((struct objc_method_description *)
240 class_get_instance_method(self, aSel));
243 - (struct objc_method_description *)descriptionForMethod:(SEL)aSel
245 return ((struct objc_method_description *)
246 (object_is_instance(self)
247 ?class_get_instance_method(self->isa, aSel)
248 :class_get_class_method(self->isa, aSel)));
251 - (retval_t)forward:(SEL)aSel :(arglist_t)argFrame
253 (void) argFrame; /* UNUSED */
254 return (retval_t)[self doesNotRecognize: aSel];
257 - (retval_t)performv:(SEL)aSel :(arglist_t)argFrame
259 return objc_msg_sendv(self, aSel, argFrame);
262 + poseAs:(Class)aClassObject
264 return class_pose_as(self, aClassObject);
267 - (Class)transmuteClassTo:(Class)aClassObject
269 if (object_is_instance(self))
270 if (class_is_class(aClassObject))
271 if (class_get_instance_size(aClassObject)==class_get_instance_size(isa))
272 if ([self isKindOf:aClassObject])
283 return class_get_version(self);
286 + setVersion:(int)aVersion
288 class_set_version(self, aVersion);
292 + (int)streamVersion: (TypedStream*)aStream
294 if (aStream->mode == OBJC_READONLY)
295 return objc_get_stream_class_version (aStream, self);
297 return class_get_version (self);
300 // These are used to write or read the instance variables
301 // declared in this particular part of the object. Subclasses
302 // should extend these, by calling [super read/write: aStream]
303 // before doing their own archiving. These methods are private, in
304 // the sense that they should only be called from subclasses.
306 - read: (TypedStream*)aStream
308 (void) aStream; /* UNUSED */
309 // [super read: aStream];
313 - write: (TypedStream*)aStream
315 (void) aStream; /* UNUSED */
316 // [super write: aStream];