]> git.ipfire.org Git - thirdparty/gcc.git/blob - libobjc/ivars.c
encoding.c (_darwin_rs6000_special_round_type_align): New.
[thirdparty/gcc.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 #include <stdlib.h> /* For malloc. */
32
33 struct objc_ivar *
34 class_getInstanceVariable (Class class_, const char *name)
35 {
36 if (class_ != Nil && name != NULL && ! CLS_IS_IN_CONSTRUCTION (class_))
37 {
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 return ivar;
52 }
53 }
54 }
55 class_ = class_getSuperclass (class_);
56 }
57 }
58 return NULL;
59 }
60
61 struct objc_ivar *
62 class_getClassVariable (Class class_, const char *name)
63 {
64 if (class_ == Nil)
65 return NULL;
66
67 /* Logically, since a class is an instance of its meta-class, and
68 since its class methods are the instance methods of the
69 meta-class, class variables should be instance variables of the
70 meta-class. That is different from the normal use of having
71 'static' variables in the class implementation file, because
72 every class would have its own variables.
73
74 Anyway, it is all speculative at this stage, but if we get class
75 variables in Objective-C, it is conceivable that this
76 implementation should work. */
77 return class_getInstanceVariable (class_->class_pointer, name);
78 }
79
80 void *
81 object_getIndexedIvars (id object)
82 {
83 if (object == nil)
84 return NULL;
85 else
86 {
87 return (void *)(((char *)object)
88 + object->class_pointer->instance_size);
89 }
90 }
91
92 struct objc_ivar *
93 object_getInstanceVariable (id object, const char *name, void **returnValue)
94 {
95 if (object == nil || name == NULL)
96 return NULL;
97 else
98 {
99 struct objc_ivar * variable = class_getInstanceVariable (object->class_pointer, name);
100
101 if (variable != NULL && returnValue != NULL)
102 {
103 char *location = (char *)object + variable->ivar_offset;
104
105 *returnValue = *((id *)location);
106 }
107
108 return variable;
109 }
110 }
111
112 struct objc_ivar *
113 object_setInstanceVariable (id object, const char *name, void *newValue)
114 {
115 if (object == nil || name == NULL)
116 return NULL;
117 else
118 {
119 struct objc_ivar * variable = class_getInstanceVariable (object->class_pointer, name);
120
121 if (variable != NULL)
122 {
123 char *location = (char *)object + variable->ivar_offset;
124
125 *((id *)location) = (id)newValue;
126 }
127
128 return variable;
129 }
130 }
131
132 id object_getIvar (id object, struct objc_ivar * variable)
133 {
134 if (object == nil || variable == NULL)
135 return nil;
136 else
137 {
138 char *location = (char *)object + variable->ivar_offset;
139
140 return *((id *)location);
141 }
142 }
143
144 void object_setIvar (id object, struct objc_ivar * variable, id value)
145 {
146 if (object == nil || variable == NULL)
147 return;
148 else
149 {
150 char *location = (char *)object + variable->ivar_offset;
151
152 *((id *)location) = value;
153 }
154 }
155
156 const char * ivar_getName (struct objc_ivar * variable)
157 {
158 if (variable == NULL)
159 return NULL;
160
161 return variable->ivar_name;
162 }
163
164 ptrdiff_t ivar_getOffset (struct objc_ivar * variable)
165 {
166 if (variable == NULL)
167 return 0;
168
169 return (ptrdiff_t)(variable->ivar_offset);
170 }
171
172 const char * ivar_getTypeEncoding (struct objc_ivar * variable)
173 {
174 if (variable == NULL)
175 return NULL;
176
177 return variable->ivar_type;
178 }
179
180 struct objc_ivar ** class_copyIvarList (Class class_, unsigned int *numberOfReturnedIvars)
181 {
182 unsigned int count = 0;
183 struct objc_ivar **returnValue = NULL;
184 struct objc_ivar_list* ivar_list;
185
186 if (class_ == Nil || CLS_IS_IN_CONSTRUCTION (class_))
187 {
188 if (numberOfReturnedIvars)
189 *numberOfReturnedIvars = 0;
190 return NULL;
191 }
192
193 /* Count how many ivars we have. */
194 ivar_list = class_->ivars;
195 count = ivar_list->ivar_count;
196
197 if (count != 0)
198 {
199 unsigned int i = 0;
200
201 /* Allocate enough memory to hold them. */
202 returnValue = (struct objc_ivar **)(malloc (sizeof (struct objc_ivar *) * (count + 1)));
203
204 /* Copy the ivars. */
205 for (i = 0; i < count; i++)
206 {
207 returnValue[i] = &(ivar_list->ivar_list[i]);
208 }
209
210 returnValue[i] = NULL;
211 }
212
213 if (numberOfReturnedIvars)
214 *numberOfReturnedIvars = count;
215
216 return returnValue;
217 }
218
219 BOOL
220 class_addIvar (Class class_, const char * ivar_name, size_t size,
221 unsigned char alignment, const char *type)
222 {
223 struct objc_ivar_list *ivars;
224
225 if (class_ == Nil
226 || (! CLS_IS_IN_CONSTRUCTION (class_))
227 || ivar_name == NULL
228 || (strcmp (ivar_name, "") == 0)
229 || size == 0
230 || type == NULL)
231 return NO;
232
233 /* Check if the class has an instance variable with that name
234 already. */
235 ivars = class_->ivars;
236
237 if (ivars != NULL)
238 {
239 int i;
240
241 for (i = 0; i < ivars->ivar_count; i++)
242 {
243 struct objc_ivar *ivar = &(ivars->ivar_list[i]);
244
245 if (strcmp (ivar->ivar_name, ivar_name) == 0)
246 {
247 return NO;
248 }
249 }
250 }
251
252 /* Ok, no direct ivars. Check superclasses. */
253 if (class_getInstanceVariable (objc_getClass ((char *)(class_->super_class)),
254 ivar_name))
255 return NO;
256
257 /* Good. Create space for the new instance variable. */
258 if (ivars)
259 {
260 int ivar_count = ivars->ivar_count + 1;
261 int new_size = sizeof (struct objc_ivar_list)
262 + (ivar_count - 1) * sizeof (struct objc_ivar);
263
264 ivars = (struct objc_ivar_list*) objc_realloc (ivars, new_size);
265 ivars->ivar_count = ivar_count;
266 class_->ivars = ivars;
267 }
268 else
269 {
270 int new_size = sizeof (struct objc_ivar_list);
271
272 ivars = (struct objc_ivar_list*) objc_malloc (new_size);
273 ivars->ivar_count = 1;
274 class_->ivars = ivars;
275 }
276
277 /* Now ivars is set to a list of instance variables of the right
278 size. */
279 {
280 struct objc_ivar *ivar = &(ivars->ivar_list[ivars->ivar_count - 1]);
281 int misalignment;
282
283 ivar->ivar_name = objc_malloc (strlen (ivar_name) + 1);
284 strcpy ((char *)ivar->ivar_name, ivar_name);
285
286 ivar->ivar_type = objc_malloc (strlen (type) + 1);
287 strcpy ((char *)ivar->ivar_type, type);
288
289 /* The new instance variable is placed at the end of the existing
290 instance_size, at the first byte that is aligned with
291 alignment. */
292 misalignment = class_->instance_size % alignment;
293
294 if (misalignment == 0)
295 ivar->ivar_offset = class_->instance_size;
296 else
297 ivar->ivar_offset = class_->instance_size - misalignment + alignment;
298
299 class_->instance_size = ivar->ivar_offset + objc_sizeof_type (ivar->ivar_type);
300 }
301
302 return YES;
303 }
304
305
306 const char *
307 property_getName (struct objc_property * property __attribute__ ((__unused__)))
308 {
309 if (property == NULL)
310 return NULL;
311
312 /* TODO: New ABI. */
313 /* The current ABI does not have any information on properties. */
314 return NULL;
315 }
316
317 const char *
318 property_getAttributes (struct objc_property * property __attribute__ ((__unused__)))
319 {
320 if (property == NULL)
321 return NULL;
322
323 /* TODO: New ABI. */
324 /* The current ABI does not have any information on properties. */
325 return NULL;
326 }
327
328 struct objc_property *
329 class_getProperty (Class class_ __attribute__ ((__unused__)),
330 const char *propertyName __attribute__ ((__unused__)))
331 {
332 if (class_ == NULL || propertyName == NULL)
333 return NULL;
334
335 /* TODO: New ABI. */
336 /* The current ABI does not have any information on class properties. */
337 return NULL;
338 }
339
340 struct objc_property **
341 class_copyPropertyList (Class class_ __attribute__ ((__unused__)),
342 unsigned int *numberOfReturnedProperties __attribute__ ((__unused__)))
343 {
344 if (class_ == Nil)
345 {
346 if (numberOfReturnedProperties)
347 *numberOfReturnedProperties = 0;
348 return NULL;
349 }
350
351 /* TODO: New ABI. */
352 /* The current ABI does not have any information on class properties. */
353 if (numberOfReturnedProperties)
354 *numberOfReturnedProperties = 0;
355
356 return NULL;
357 }
358
359 const char *
360 class_getIvarLayout (Class class_ __attribute__ ((__unused__)))
361 {
362 return NULL;
363 }
364
365 const char *
366 class_getWeakIvarLayout (Class class_ __attribute__ ((__unused__)))
367 {
368 return NULL;
369 }
370
371 void
372 class_setIvarLayout (Class class_ __attribute__ ((__unused__)),
373 const char *layout __attribute__ ((__unused__)))
374 {
375 return;
376 }
377
378 void
379 class_setWeakIvarLayout (Class class_ __attribute__ ((__unused__)),
380 const char *layout __attribute__ ((__unused__)))
381 {
382 return;
383 }