OSDN Git Service

In libobjc/:
[pf3gnuchains/gcc-fork.git] / libobjc / ivars.c
1 /* GNU Objective C Runtime ivar related functions.
2    Copyright (C) 2010 Free Software Foundation, Inc.
3    Contributed by Nicola Pero
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under the
8 terms of the GNU General Public License as published by the Free Software
9 Foundation; either version 3, or (at your option) any later version.
10
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
14 details.
15
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.
19
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/>.  */
24
25 #include "objc-private/common.h"
26 #include "objc/runtime.h"
27 #include "objc-private/module-abi-8.h" /* For runtime structures  */
28 #include "objc/thr.h"
29 #include "objc-private/runtime.h"      /* the kitchen sink */
30 #include <string.h>                    /* For strcmp */
31
32 struct objc_ivar *
33 class_getInstanceVariable (Class class_, const char *name)
34 {
35   if (class_ != Nil  &&  name != NULL)
36     {
37       objc_mutex_lock (__objc_runtime_mutex);
38       while (class_ != Nil)
39         {
40           struct objc_ivar_list *ivars = class_->ivars;
41           if (ivars != NULL)
42             {
43               int i;
44               
45               for (i = 0; i < ivars->ivar_count; i++)
46                 {
47                   struct objc_ivar *ivar = &(ivars->ivar_list[i]);
48                   
49                   if (!strcmp (ivar->ivar_name, name))
50                     {
51                       objc_mutex_unlock (__objc_runtime_mutex);
52                       return ivar;
53                     }
54                 }
55             }
56           class_ = class_->super_class;
57         }
58       objc_mutex_unlock (__objc_runtime_mutex);
59     }
60   return NULL;
61 }
62
63 struct objc_ivar *
64 class_getClassVariable (Class class_, const char *name)
65 {
66   if (class_ == Nil)
67     return NULL;
68
69   /* Logically, since a class is an instance of its meta-class, and
70      since its class methods are the instance methods of the
71      meta-class, class variables should be instance variables of the
72      meta-class.  That is different from the normal use of having
73      'static' variables in the class implementation file, because
74      every class would have its own variables.
75
76      Anyway, it is all speculative at this stage, but if we get class
77      variables in Objective-C, it is conceivable that this
78      implementation should work.  */
79   return class_getInstanceVariable (class_->class_pointer, name);
80 }
81
82 void *
83 object_getIndexedIvars (id object)
84 {
85   if (object == nil)
86     return NULL;
87   else
88     {
89       return (void *)(((char *)object) 
90                       + object->class_pointer->instance_size);
91     }
92 }
93
94 struct objc_ivar *
95 object_getInstanceVariable (id object, const char *name, void **returnValue)
96 {
97   if (object == nil  ||  name == NULL)
98     return NULL;
99   else
100     {
101       struct objc_ivar * variable = class_getInstanceVariable (object->class_pointer, name);
102
103       if (variable != NULL  &&  returnValue != NULL)
104         {
105           char *location = (char *)object + variable->ivar_offset;
106          
107           *returnValue = *((id *)location);
108         }
109
110       return variable;
111     }
112 }
113
114 struct objc_ivar *
115 object_setInstanceVariable (id object, const char *name, void *newValue)
116 {
117   if (object == nil  ||  name == NULL)
118     return NULL;
119   else
120     {
121       struct objc_ivar * variable = class_getInstanceVariable (object->class_pointer, name);
122
123       if (variable != NULL)
124         {
125           char *location = (char *)object + variable->ivar_offset;
126           
127           *((id *)location) = (id)newValue;
128         }
129
130       return variable;
131     }
132 }
133
134 id object_getIvar (id object, struct objc_ivar * variable)
135 {
136   if (object == nil  ||  variable == NULL)
137     return nil;
138   else
139     {
140       char *location = (char *)object + variable->ivar_offset;
141
142       return *((id *)location);
143     }
144 }
145
146 void object_setIvar (id object, struct objc_ivar * variable, id value)
147 {
148   if (object == nil  ||  variable == NULL)
149     return;
150   else
151     {
152       char *location = (char *)object + variable->ivar_offset;
153
154       *((id *)location) = value;
155     }
156 }
157
158 const char * ivar_getName (struct objc_ivar * variable)
159 {
160   if (variable == NULL)
161     return NULL;
162
163   return variable->ivar_name;
164 }
165
166 ptrdiff_t ivar_getOffset (struct objc_ivar * variable)
167 {
168   if (variable == NULL)
169     return 0;
170
171   return (ptrdiff_t)(variable->ivar_offset);
172 }
173
174 const char * ivar_getTypeEncoding (struct objc_ivar * variable)
175 {
176   if (variable == NULL)
177     return NULL;
178
179   return variable->ivar_type;
180 }
181
182 struct objc_ivar ** class_copyIvarList (Class class_, unsigned int *numberOfReturnedIvars)
183 {
184   unsigned int count = 0;
185   struct objc_ivar **returnValue = NULL;
186   struct objc_ivar_list* ivar_list;
187
188   if (class_ == Nil)
189     {
190       if (numberOfReturnedIvars)
191         *numberOfReturnedIvars = 0;
192       return NULL;
193     }
194
195   /* TODO: We do not need to lock the runtime mutex if the class has
196      been registered with the runtime, since the instance variable
197      list can not change after the class is registered.  The only case
198      where the lock may be useful if the class is still being created
199      using objc_allocateClassPair(), but has not been registered using
200      objc_registerClassPair() yet.  I'm not even sure that is
201      allowed.  */
202   objc_mutex_lock (__objc_runtime_mutex);
203
204   /* Count how many ivars we have.  */
205   ivar_list = class_->ivars;
206   count = ivar_list->ivar_count;
207
208   if (count != 0)
209     {
210       unsigned int i = 0;
211       
212       /* Allocate enough memory to hold them.  */
213       returnValue = (struct objc_ivar **)(malloc (sizeof (struct objc_ivar *) * (count + 1)));
214       
215       /* Copy the ivars.  */
216       for (i = 0; i < count; i++)
217         {
218           returnValue[i] = &(ivar_list->ivar_list[i]);
219         }
220       
221       returnValue[i] = NULL;
222     }
223   
224   objc_mutex_unlock (__objc_runtime_mutex);
225
226   if (numberOfReturnedIvars)
227     *numberOfReturnedIvars = count;
228
229   return returnValue;
230 }
231
232 const char *
233 property_getName (struct objc_property * property __attribute__ ((__unused__)))
234 {
235   if (property == NULL)
236     return NULL;
237
238   /* TODO: New ABI.  */
239   /* The current ABI does not have any information on properties.  */
240   return NULL;
241 }
242
243 const char *
244 property_getAttributes (struct objc_property * property __attribute__ ((__unused__)))
245 {
246   if (property == NULL)
247     return NULL;
248
249   /* TODO: New ABI.  */
250   /* The current ABI does not have any information on properties.  */
251   return NULL;
252 }
253
254 struct objc_property *
255 class_getProperty (Class class_ __attribute__ ((__unused__)),
256                    const char *propertyName __attribute__ ((__unused__)))
257 {
258   if (class_ == NULL  ||  propertyName == NULL)
259     return NULL;
260
261   /* TODO: New ABI.  */
262   /* The current ABI does not have any information on class properties.  */
263   return NULL;
264 }
265
266 struct objc_property ** 
267 class_copyPropertyList (Class class_ __attribute__ ((__unused__)), 
268                         unsigned int *numberOfReturnedProperties __attribute__ ((__unused__)))
269 {
270   if (class_ == Nil)
271     {
272       if (numberOfReturnedProperties)
273         *numberOfReturnedProperties = 0;
274       return NULL;
275     }
276
277   /* TODO: New ABI.  */
278   /* The current ABI does not have any information on class properties.  */
279   if (numberOfReturnedProperties)
280     *numberOfReturnedProperties = 0;
281
282   return NULL;
283 }
284
285 const char *
286 class_getIvarLayout (Class class_ __attribute__ ((__unused__)))
287 {
288   return NULL;
289 }
290
291 const char *
292 class_getWeakIvarLayout (Class class_ __attribute__ ((__unused__)))
293 {
294   return NULL;
295 }
296
297 void
298 class_setIvarLayout (Class class_ __attribute__ ((__unused__)),
299                      const char *layout __attribute__ ((__unused__)))
300 {
301   return;
302 }
303
304 void
305 class_setWeakIvarLayout (Class class_ __attribute__ ((__unused__)),
306                          const char *layout __attribute__ ((__unused__)))
307 {
308   return;
309 }