Daily bump.
[official-gcc.git] / libobjc / ivars.c
blob910a09828dbb3d1d1dea7deb11bd547540210cc4
1 /* GNU Objective C Runtime ivar related functions.
2 Copyright (C) 2010-2015 Free Software Foundation, Inc.
3 Contributed by Nicola Pero
5 This file is part of GCC.
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.
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.
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 "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. */
33 struct objc_ivar *
34 class_getInstanceVariable (Class class_, const char *name)
36 if (class_ != Nil && name != NULL && ! CLS_IS_IN_CONSTRUCTION (class_))
38 while (class_ != Nil)
40 struct objc_ivar_list *ivars = class_->ivars;
41 if (ivars != NULL)
43 int i;
45 for (i = 0; i < ivars->ivar_count; i++)
47 struct objc_ivar *ivar = &(ivars->ivar_list[i]);
49 if (!strcmp (ivar->ivar_name, name))
50 return ivar;
53 class_ = class_getSuperclass (class_);
56 return NULL;
59 struct objc_ivar *
60 class_getClassVariable (Class class_, const char *name)
62 if (class_ == Nil)
63 return NULL;
65 /* Logically, since a class is an instance of its meta-class, and
66 since its class methods are the instance methods of the
67 meta-class, class variables should be instance variables of the
68 meta-class. That is different from the normal use of having
69 'static' variables in the class implementation file, because
70 every class would have its own variables.
72 Anyway, it is all speculative at this stage, but if we get class
73 variables in Objective-C, it is conceivable that this
74 implementation should work. */
75 return class_getInstanceVariable (class_->class_pointer, name);
78 void *
79 object_getIndexedIvars (id object)
81 if (object == nil)
82 return NULL;
83 else
84 return (void *)(((char *)object)
85 + object->class_pointer->instance_size);
88 struct objc_ivar *
89 object_getInstanceVariable (id object, const char *name, void **returnValue)
91 if (object == nil || name == NULL)
92 return NULL;
93 else
95 struct objc_ivar * variable = class_getInstanceVariable (object->class_pointer, name);
97 if (variable != NULL && returnValue != NULL)
99 char *location = (char *)object + variable->ivar_offset;
101 *returnValue = *((id *)location);
104 return variable;
108 struct objc_ivar *
109 object_setInstanceVariable (id object, const char *name, void *newValue)
111 if (object == nil || name == NULL)
112 return NULL;
113 else
115 struct objc_ivar * variable = class_getInstanceVariable (object->class_pointer, name);
117 if (variable != NULL)
119 char *location = (char *)object + variable->ivar_offset;
121 *((id *)location) = (id)newValue;
124 return variable;
128 id object_getIvar (id object, struct objc_ivar * variable)
130 if (object == nil || variable == NULL)
131 return nil;
132 else
134 char *location = (char *)object + variable->ivar_offset;
136 return *((id *)location);
140 void object_setIvar (id object, struct objc_ivar * variable, id value)
142 if (object == nil || variable == NULL)
143 return;
144 else
146 char *location = (char *)object + variable->ivar_offset;
148 *((id *)location) = value;
152 const char * ivar_getName (struct objc_ivar * variable)
154 if (variable == NULL)
155 return NULL;
157 return variable->ivar_name;
160 ptrdiff_t ivar_getOffset (struct objc_ivar * variable)
162 if (variable == NULL)
163 return 0;
165 return (ptrdiff_t)(variable->ivar_offset);
168 const char * ivar_getTypeEncoding (struct objc_ivar * variable)
170 if (variable == NULL)
171 return NULL;
173 return variable->ivar_type;
176 struct objc_ivar ** class_copyIvarList (Class class_, unsigned int *numberOfReturnedIvars)
178 unsigned int count = 0;
179 struct objc_ivar **returnValue = NULL;
180 struct objc_ivar_list* ivar_list;
182 if (class_ == Nil || CLS_IS_IN_CONSTRUCTION (class_) || !class_->ivars)
184 if (numberOfReturnedIvars)
185 *numberOfReturnedIvars = 0;
186 return NULL;
189 /* Count how many ivars we have. */
190 ivar_list = class_->ivars;
191 count = ivar_list->ivar_count;
193 if (count != 0)
195 unsigned int i = 0;
197 /* Allocate enough memory to hold them. */
198 returnValue = (struct objc_ivar **)(malloc (sizeof (struct objc_ivar *) * (count + 1)));
200 /* Copy the ivars. */
201 for (i = 0; i < count; i++)
202 returnValue[i] = &(ivar_list->ivar_list[i]);
204 returnValue[i] = NULL;
207 if (numberOfReturnedIvars)
208 *numberOfReturnedIvars = count;
210 return returnValue;
213 BOOL
214 class_addIvar (Class class_, const char * ivar_name, size_t size,
215 unsigned char log_2_of_alignment, const char *type)
217 struct objc_ivar_list *ivars;
219 if (class_ == Nil
220 || (! CLS_IS_IN_CONSTRUCTION (class_))
221 || ivar_name == NULL
222 || (strcmp (ivar_name, "") == 0)
223 || size == 0
224 || type == NULL)
225 return NO;
227 /* Check if the class has an instance variable with that name
228 already. */
229 ivars = class_->ivars;
231 if (ivars != NULL)
233 int i;
235 for (i = 0; i < ivars->ivar_count; i++)
237 struct objc_ivar *ivar = &(ivars->ivar_list[i]);
239 if (strcmp (ivar->ivar_name, ivar_name) == 0)
240 return NO;
244 /* Ok, no direct ivars. Check superclasses. */
245 if (class_getInstanceVariable (objc_getClass ((char *)(class_->super_class)),
246 ivar_name))
247 return NO;
249 /* Good. Create space for the new instance variable. */
250 if (ivars)
252 int ivar_count = ivars->ivar_count + 1;
253 int new_size = sizeof (struct objc_ivar_list)
254 + (ivar_count - 1) * sizeof (struct objc_ivar);
256 ivars = (struct objc_ivar_list*) objc_realloc (ivars, new_size);
257 ivars->ivar_count = ivar_count;
258 class_->ivars = ivars;
260 else
262 int new_size = sizeof (struct objc_ivar_list);
264 ivars = (struct objc_ivar_list*) objc_malloc (new_size);
265 ivars->ivar_count = 1;
266 class_->ivars = ivars;
269 /* Now ivars is set to a list of instance variables of the right
270 size. */
272 struct objc_ivar *ivar = &(ivars->ivar_list[ivars->ivar_count - 1]);
273 unsigned int alignment = 1 << log_2_of_alignment;
274 int misalignment;
276 ivar->ivar_name = objc_malloc (strlen (ivar_name) + 1);
277 strcpy ((char *)ivar->ivar_name, ivar_name);
279 ivar->ivar_type = objc_malloc (strlen (type) + 1);
280 strcpy ((char *)ivar->ivar_type, type);
282 /* The new instance variable is placed at the end of the existing
283 instance_size, at the first byte that is aligned with
284 alignment. */
285 misalignment = class_->instance_size % alignment;
287 if (misalignment == 0)
288 ivar->ivar_offset = class_->instance_size;
289 else
290 ivar->ivar_offset = class_->instance_size - misalignment + alignment;
292 class_->instance_size = ivar->ivar_offset + size;
295 return YES;
299 const char *
300 property_getName (struct objc_property * property __attribute__ ((__unused__)))
302 if (property == NULL)
303 return NULL;
305 /* TODO: New ABI. */
306 /* The current ABI does not have any information on properties. */
307 return NULL;
310 const char *
311 property_getAttributes (struct objc_property * property __attribute__ ((__unused__)))
313 if (property == NULL)
314 return NULL;
316 /* TODO: New ABI. */
317 /* The current ABI does not have any information on properties. */
318 return NULL;
321 struct objc_property *
322 class_getProperty (Class class_ __attribute__ ((__unused__)),
323 const char *propertyName __attribute__ ((__unused__)))
325 if (class_ == NULL || propertyName == NULL)
326 return NULL;
328 /* TODO: New ABI. */
329 /* The current ABI does not have any information on class properties. */
330 return NULL;
333 struct objc_property **
334 class_copyPropertyList (Class class_ __attribute__ ((__unused__)),
335 unsigned int *numberOfReturnedProperties __attribute__ ((__unused__)))
337 if (class_ == Nil)
339 if (numberOfReturnedProperties)
340 *numberOfReturnedProperties = 0;
341 return NULL;
344 /* TODO: New ABI. */
345 /* The current ABI does not have any information on class properties. */
346 if (numberOfReturnedProperties)
347 *numberOfReturnedProperties = 0;
349 return NULL;
352 const char *
353 class_getIvarLayout (Class class_ __attribute__ ((__unused__)))
355 return NULL;
358 const char *
359 class_getWeakIvarLayout (Class class_ __attribute__ ((__unused__)))
361 return NULL;
364 void
365 class_setIvarLayout (Class class_ __attribute__ ((__unused__)),
366 const char *layout __attribute__ ((__unused__)))
368 return;
371 void
372 class_setWeakIvarLayout (Class class_ __attribute__ ((__unused__)),
373 const char *layout __attribute__ ((__unused__)))
375 return;