OSDN Git Service

Changed includes to doublequote style
[pf3gnuchains/gcc-fork.git] / gcc / objc / Object.m
1 /* The implementation of class Object for Objective-C.
2    Copyright (C) 1993 Free Software Foundation, Inc.
3
4 This file is part of GNU CC.
5
6 GNU CC 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 2, or (at your option) any
9 later version.
10
11 GNU CC is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
14 License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING.  If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
19
20 /* As a special exception, if you link this library with files compiled
21    with GCC to produce an executable, this does not cause the resulting
22    executable to be covered by the GNU General Public License.  This
23    exception does not however invalidate any other reasons why the
24    executable file might be covered by the GNU General Public License. */
25
26 #include "objc/Object.h"
27 #include "objc/Protocol.h"
28 #include "objc/objc-api.h"
29
30 extern int errno;
31
32 #define MAX_CLASS_NAME_LEN 256
33
34 @implementation Object
35
36 + initialize
37 {
38   return self;
39 }
40
41 - init
42 {
43   return self;
44 }
45
46 + new
47 {
48   return [[self alloc] init];
49 }
50
51 + alloc
52 {
53   return class_create_instance(self);
54 }
55
56 - free
57 {
58   return object_dispose(self);
59 }
60
61 - copy
62 {
63   return [[self shallowCopy] deepen];
64 }
65
66 - shallowCopy
67 {
68   return object_copy(self);
69 }
70
71 - deepen
72 {
73   return self;
74 }
75
76 - deepCopy
77 {
78   return [self copy];
79 }
80
81 - (Class_t)class
82 {
83   return object_get_class(self);
84 }
85
86 - (Class_t)superClass
87 {
88   return object_get_super_class(self);
89 }
90
91 - (MetaClass_t)metaClass
92 {
93   return object_get_meta_class(self);
94 }
95
96 - (const char *)name
97 {
98   return object_get_class_name(self);
99 }
100
101 - self
102 {
103   return self;
104 }
105
106 - (unsigned int)hash
107 {
108   return (unsigned int)self;
109 }
110
111 - (BOOL)isEqual:anObject
112 {
113   return self==anObject;
114 }
115
116 - (BOOL)isMetaClass
117 {
118   return NO;
119 }
120
121 - (BOOL)isClass
122 {
123   return object_is_class(self);
124 }
125
126 - (BOOL)isInstance
127 {
128   return object_is_instance(self);
129 }
130
131 - (BOOL)isKindOf:(Class_t)aClassObject
132 {
133   Class_t class;
134
135   for (class = self->isa; class!=Nil; class = class_get_super_class(class))
136     if (class==aClassObject)
137       return YES;
138   return NO;
139 }
140
141 - (BOOL)isMemberOf:(Class_t)aClassObject
142 {
143   return self->isa==aClassObject;
144 }
145
146 - (BOOL)isKindOfClassNamed:(const char *)aClassName
147 {
148   Class_t class;
149
150   if (aClassName!=NULL)
151     for (class = self->isa; class!=Nil; class = class_get_super_class(class))
152       if (!strcmp(class_get_class_name(class), aClassName))
153         return YES;
154   return NO;
155 }
156
157 - (BOOL)isMemberOfClassNamed:(const char *)aClassName
158 {
159   return ((aClassName!=NULL)
160           &&!strcmp(class_get_class_name(self->isa), aClassName));
161 }
162
163 + (BOOL)instancesRespondTo:(SEL)aSel
164 {
165   return class_get_instance_method(self, aSel)!=METHOD_NULL;
166 }
167
168 - (BOOL)respondsTo:(SEL)aSel
169 {
170   return ((object_is_instance(self)
171            ?class_get_instance_method(self->isa, aSel)
172            :class_get_class_method(self->isa, aSel))!=METHOD_NULL);
173 }
174
175 + (IMP)instanceMethodFor:(SEL)aSel
176 {
177   return method_get_imp(class_get_instance_method(self, aSel));
178 }
179
180 // Indicates if the receiving class or instance conforms to the given protocol
181 // not usually overridden by subclasses
182 - (BOOL) conformsTo: (Protocol*)aProtocol
183 {
184   int i;
185   struct objc_protocol_list* proto_list;
186
187   for (proto_list = isa->protocols;
188        proto_list; proto_list = proto_list->next)
189     {
190       for (i=0; i < proto_list->count; i++)
191       {
192         if ([proto_list->list[i] conformsTo: aProtocol])
193           return YES;
194       }
195     }
196
197   return NO;
198 }
199
200 - (IMP)methodFor:(SEL)aSel
201 {
202   return (method_get_imp(object_is_instance(self)
203                          ?class_get_instance_method(self->isa, aSel)
204                          :class_get_class_method(self->isa, aSel)));
205 }
206
207 + (struct objc_method_description *)descriptionForInstanceMethod:(SEL)aSel
208 {
209   return ((struct objc_method_description *)
210            class_get_instance_method(self, aSel));
211 }
212
213 - (struct objc_method_description *)descriptionForMethod:(SEL)aSel
214 {
215   return ((struct objc_method_description *)
216            (object_is_instance(self)
217             ?class_get_instance_method(self->isa, aSel)
218             :class_get_class_method(self->isa, aSel)));
219 }
220
221 - perform:(SEL)aSel
222 {
223   IMP msg = objc_msg_lookup(self, aSel);
224   if (!msg)
225     return [self error:"invalid selector passed to %s", sel_get_name(_cmd)];
226   return (*msg)(self, aSel);
227 }
228
229 - perform:(SEL)aSel with:anObject
230 {
231   IMP msg = objc_msg_lookup(self, aSel);
232   if (!msg)
233     return [self error:"invalid selector passed to %s", sel_get_name(_cmd)];
234   return (*msg)(self, aSel, anObject);
235 }
236
237 - perform:(SEL)aSel with:anObject1 with:anObject2
238 {
239   IMP msg = objc_msg_lookup(self, aSel);
240   if (!msg)
241     return [self error:"invalid selector passed to %s", sel_get_name(_cmd)];
242   return (*msg)(self, aSel, anObject1, anObject2);
243 }
244
245 - forward:(SEL)aSel :(arglist_t)argFrame
246 {
247   return [self doesNotRecognize: aSel];
248 }
249
250 - performv:(SEL)aSel :(arglist_t)argFrame
251 {
252   return objc_msg_sendv(self, aSel, method_get_argsize(0), argFrame);
253 }
254
255 + poseAs:(Class_t)aClassObject
256 {
257   return class_pose_as(self, aClassObject);
258 }
259
260 - (Class_t)transmuteClassTo:(Class_t)aClassObject
261 {
262   if (object_is_instance(self))
263     if (class_is_class(aClassObject))
264       if (class_get_instance_size(aClassObject)==class_get_instance_size(isa))
265         if ([self isKindOf:aClassObject])
266           {
267             Class_t old_isa = isa;
268             isa = aClassObject;
269             return old_isa;
270           }
271   return nil;
272 }
273
274 - subclassResponsibility:(SEL)aSel
275 {
276   return [self error:"subclass should override %s", sel_get_name(aSel)];
277 }
278
279 - notImplemented:(SEL)aSel
280 {
281   return [self error:"method %s not implemented", sel_get_name(aSel)];
282 }
283
284 - doesNotRecognize:(SEL)aSel
285 {
286   return [self error:"%s does not recognize %s",
287                      object_get_class_name(self), sel_get_name(aSel)];
288 }
289
290 - error:(const char *)aString, ...
291 {
292 #define FMT "error: %s (%s)\n%s\n"
293   char fmt[(strlen(FMT)+strlen(object_get_class_name(self))
294             +((aString!=NULL)?strlen(aString):0)+8)];
295   va_list ap;
296
297   sprintf(fmt, FMT, object_get_class_name(self),
298                     object_is_instance(self)?"instance":"class",
299                     (aString!=NULL)?aString:"");
300   va_start(ap, aString);
301   (*_objc_error)(self, fmt, ap);
302   va_end(ap);
303   return nil;
304 #undef FMT
305 }
306
307 + (int)version
308 {
309   return class_get_version(self);
310 }
311
312 + setVersion:(int)aVersion
313 {
314   class_set_version(self, aVersion);
315   return self;
316 }
317
318 // These are used to write or read the instance variables 
319 // declared in this particular part of the object.  Subclasses
320 // should extend these, by calling [super read/write: aStream]
321 // before doing their own archiving.  These methods are private, in
322 // the sense that they should only be called from subclasses.
323
324 - read: (TypedStream*)aStream
325 {
326   // [super read: aStream];  
327   return self;
328 }
329
330 - write: (TypedStream*)aStream
331 {
332   // [super write: aStream];
333   return self;
334 }
335
336 - awake: (TypedStream*)aStream
337 {
338   // [super awake: aStream];
339   return self;
340 }
341
342 @end