Merge from mainline (167278:168000).
[official-gcc/graphite-test-results.git] / libobjc / Object.m
blob0b7c49fdab292703714869e190b49f3c1ba09041
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
9 later version.
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/>.  */
25 #include "objc-private/common.h"
26 #include <stdarg.h>
27 #include <errno.h>
28 #include "objc/Object.h"
29 #include "objc/Protocol.h"
30 #include "objc/objc-api.h"
32 @implementation Object
34 - (Class)class
36   return object_get_class (self);
39 - (BOOL)isEqual: (id)anObject
41   return self == anObject;
44 @end
46 /* The following methods were deprecated in GCC 4.6.0 and will be
47    removed in the next GCC release.  */
48 @implementation Object (Deprecated)
50 + initialize
52   return self;
55 - init
57   return self;
60 + new
62   return [[self alloc] init];
65 + alloc
67   return class_create_instance(self);
70 - free
72   return object_dispose(self);
75 - copy
77   return [[self shallowCopy] deepen];
80 - shallowCopy
82   return object_copy(self);
85 - deepen
87   return self;
90 - deepCopy
92   return [self copy];
95 - (Class)superClass
97   return object_get_super_class(self);
100 - (MetaClass)metaClass
102   return object_get_meta_class(self);
105 - (const char *)name
107   return object_get_class_name(self);
110 - self
112   return self;
115 - (unsigned int)hash
117   return (size_t)self;
120 - (int)compare:(id)anotherObject;
122   if ([self isEqual:anotherObject])
123     return 0;
124   // Ordering objects by their address is pretty useless, 
125   // so subclasses should override this is some useful way.
126   else if ((id)self > anotherObject)
127     return 1;
128   else 
129     return -1;
132 - (BOOL)isMetaClass
134   return NO;
137 - (BOOL)isClass
139   return object_is_class(self);
142 - (BOOL)isInstance
144   return object_is_instance(self);
147 - (BOOL)isKindOf:(Class)aClassObject
149   Class class;
151   for (class = self->isa; class!=Nil; class = class_get_super_class(class))
152     if (class==aClassObject)
153       return YES;
154   return NO;
157 - (BOOL)isMemberOf:(Class)aClassObject
159   return self->isa==aClassObject;
162 - (BOOL)isKindOfClassNamed:(const char *)aClassName
164   Class class;
166   if (aClassName!=NULL)
167     for (class = self->isa; class!=Nil; class = class_get_super_class(class))
168       if (!strcmp(class_get_class_name(class), aClassName))
169         return YES;
170   return NO;
173 - (BOOL)isMemberOfClassNamed:(const char *)aClassName
175   return ((aClassName!=NULL)
176           &&!strcmp(class_get_class_name(self->isa), aClassName));
179 + (BOOL)instancesRespondTo:(SEL)aSel
181   return class_get_instance_method(self, aSel) != (Method_t)0;
184 - (BOOL)respondsTo:(SEL)aSel
186   return ((object_is_instance(self)
187            ?class_get_instance_method(self->isa, aSel)
188            :class_get_class_method(self->isa, aSel)) != (Method_t)0);
191 + (IMP)instanceMethodFor:(SEL)aSel
193   return method_get_imp(class_get_instance_method(self, aSel));
196 // Indicates if the receiving class or instance conforms to the given protocol
197 // not usually overridden by subclasses
199 // Modified 9/5/94 to always search the class object's protocol list, rather
200 // than the meta class.
202 + (BOOL) conformsTo: (Protocol*)aProtocol
204   size_t i;
205   struct objc_protocol_list* proto_list;
206   id parent;
208   for (proto_list = ((Class)self)->protocols;
209        proto_list; proto_list = proto_list->next)
210     {
211       for (i=0; i < proto_list->count; i++)
212       {
213         if ([proto_list->list[i] conformsTo: aProtocol])
214           return YES;
215       }
216     }
218   if ((parent = [self superClass]))
219     return [parent conformsTo: aProtocol];
220   else
221     return NO;
224 - (BOOL) conformsTo: (Protocol*)aProtocol
226   return [[self class] conformsTo:aProtocol];
229 - (IMP)methodFor:(SEL)aSel
231   return (method_get_imp(object_is_instance(self)
232                          ?class_get_instance_method(self->isa, aSel)
233                          :class_get_class_method(self->isa, aSel)));
236 + (struct objc_method_description *)descriptionForInstanceMethod:(SEL)aSel
238   return ((struct objc_method_description *)
239            class_get_instance_method(self, aSel));
242 - (struct objc_method_description *)descriptionForMethod:(SEL)aSel
244   return ((struct objc_method_description *)
245            (object_is_instance(self)
246             ?class_get_instance_method(self->isa, aSel)
247             :class_get_class_method(self->isa, aSel)));
250 - perform:(SEL)aSel
252   IMP msg = objc_msg_lookup(self, aSel);
253   if (!msg)
254     return [self error:"invalid selector passed to %s", sel_get_name(_cmd)];
255   return (*msg)(self, aSel);
258 - perform:(SEL)aSel with:anObject
260   IMP msg = objc_msg_lookup(self, aSel);
261   if (!msg)
262     return [self error:"invalid selector passed to %s", sel_get_name(_cmd)];
263   return (*msg)(self, aSel, anObject);
266 - perform:(SEL)aSel with:anObject1 with:anObject2
268   IMP msg = objc_msg_lookup(self, aSel);
269   if (!msg)
270     return [self error:"invalid selector passed to %s", sel_get_name(_cmd)];
271   return (*msg)(self, aSel, anObject1, anObject2);
274 - (retval_t)forward:(SEL)aSel :(arglist_t)argFrame
276   (void) argFrame; /* UNUSED */
277   return (retval_t)[self doesNotRecognize: aSel];
280 - (retval_t)performv:(SEL)aSel :(arglist_t)argFrame
282   return objc_msg_sendv(self, aSel, argFrame);
285 + poseAs:(Class)aClassObject
287   return class_pose_as(self, aClassObject);
290 - (Class)transmuteClassTo:(Class)aClassObject
292   if (object_is_instance(self))
293     if (class_is_class(aClassObject))
294       if (class_get_instance_size(aClassObject)==class_get_instance_size(isa))
295         if ([self isKindOf:aClassObject])
296           {
297             Class old_isa = isa;
298             isa = aClassObject;
299             return old_isa;
300           }
301   return nil;
304 - subclassResponsibility:(SEL)aSel
306   return [self error:"subclass should override %s", sel_get_name(aSel)];
309 - notImplemented:(SEL)aSel
311   return [self error:"method %s not implemented", sel_get_name(aSel)];
314 - shouldNotImplement:(SEL)aSel
316   return [self error:"%s should not implement %s", 
317                      object_get_class_name(self), sel_get_name(aSel)];
320 - doesNotRecognize:(SEL)aSel
322   return [self error:"%s does not recognize %s",
323                      object_get_class_name(self), sel_get_name(aSel)];
326 - error:(const char *)aString, ...
328 #define FMT "error: %s (%s)\n%s\n"
329   char fmt[(strlen((char*)FMT)+strlen((char*)object_get_class_name(self))
330             +((aString!=NULL)?strlen((char*)aString):0)+8)];
331   va_list ap;
333   sprintf(fmt, FMT, object_get_class_name(self),
334                     object_is_instance(self)?"instance":"class",
335                     (aString!=NULL)?aString:"");
336   va_start(ap, aString);
337   objc_verror(self, OBJC_ERR_UNKNOWN, fmt, ap);
338   va_end(ap);
339   return nil;
340 #undef FMT
343 + (int)version
345   return class_get_version(self);
348 + setVersion:(int)aVersion
350   class_set_version(self, aVersion);
351   return self;
354 + (int)streamVersion: (TypedStream*)aStream
356   if (aStream->mode == OBJC_READONLY)
357     return objc_get_stream_class_version (aStream, self);
358   else
359     return class_get_version (self);
362 // These are used to write or read the instance variables 
363 // declared in this particular part of the object.  Subclasses
364 // should extend these, by calling [super read/write: aStream]
365 // before doing their own archiving.  These methods are private, in
366 // the sense that they should only be called from subclasses.
368 - read: (TypedStream*)aStream
370   (void) aStream; /* UNUSED */
371   // [super read: aStream];  
372   return self;
375 - write: (TypedStream*)aStream
377   (void) aStream; /* UNUSED */
378   // [super write: aStream];
379   return self;
382 - awake
384   // [super awake];
385   return self;
388 @end