1 /* GNU Objective C Runtime ivar related functions.
2 Copyright (C) 2010-2020 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
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 */
29 #include "objc-private/runtime.h" /* the kitchen sink */
30 #include <string.h> /* For strcmp. */
31 #include <stdlib.h> /* For malloc. */
34 class_getInstanceVariable (Class class_
, const char *name
)
36 if (class_
!= Nil
&& name
!= NULL
&& ! CLS_IS_IN_CONSTRUCTION (class_
))
40 struct objc_ivar_list
*ivars
= class_
->ivars
;
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
))
53 class_
= class_getSuperclass (class_
);
60 class_getClassVariable (Class class_
, const char *name
)
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
);
79 object_getIndexedIvars (id object
)
84 return (void *)(((char *)object
)
85 + object
->class_pointer
->instance_size
);
89 object_getInstanceVariable (id object
, const char *name
, void **returnValue
)
91 if (object
== nil
|| name
== NULL
)
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
);
109 object_setInstanceVariable (id object
, const char *name
, void *newValue
)
111 if (object
== nil
|| name
== NULL
)
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
;
128 id
object_getIvar (id object
, struct objc_ivar
* variable
)
130 if (object
== nil
|| variable
== NULL
)
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
)
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
)
157 return variable
->ivar_name
;
160 ptrdiff_t ivar_getOffset (struct objc_ivar
* variable
)
162 if (variable
== NULL
)
165 return (ptrdiff_t)(variable
->ivar_offset
);
168 const char * ivar_getTypeEncoding (struct objc_ivar
* variable
)
170 if (variable
== 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;
189 /* Count how many ivars we have. */
190 ivar_list
= class_
->ivars
;
191 count
= ivar_list
->ivar_count
;
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
;
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
;
220 || (! CLS_IS_IN_CONSTRUCTION (class_
))
222 || (strcmp (ivar_name
, "") == 0)
227 /* Check if the class has an instance variable with that name
229 ivars
= class_
->ivars
;
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)
244 /* Ok, no direct ivars. Check superclasses. */
245 if (class_getInstanceVariable (objc_getClass ((char *)(class_
->super_class
)),
249 /* Good. Create space for the new instance variable. */
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
;
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
272 struct objc_ivar
*ivar
= &(ivars
->ivar_list
[ivars
->ivar_count
- 1]);
273 unsigned int alignment
= 1 << log_2_of_alignment
;
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
285 misalignment
= class_
->instance_size
% alignment
;
287 if (misalignment
== 0)
288 ivar
->ivar_offset
= class_
->instance_size
;
290 ivar
->ivar_offset
= class_
->instance_size
- misalignment
+ alignment
;
292 class_
->instance_size
= ivar
->ivar_offset
+ size
;
300 property_getName (struct objc_property
* property
__attribute__ ((__unused__
)))
302 if (property
== NULL
)
306 /* The current ABI does not have any information on properties. */
311 property_getAttributes (struct objc_property
* property
__attribute__ ((__unused__
)))
313 if (property
== NULL
)
317 /* The current ABI does not have any information on properties. */
321 struct objc_property
*
322 class_getProperty (Class class_
__attribute__ ((__unused__
)),
323 const char *propertyName
__attribute__ ((__unused__
)))
325 if (class_
== NULL
|| propertyName
== NULL
)
329 /* The current ABI does not have any information on class properties. */
333 struct objc_property
**
334 class_copyPropertyList (Class class_
__attribute__ ((__unused__
)),
335 unsigned int *numberOfReturnedProperties
__attribute__ ((__unused__
)))
339 if (numberOfReturnedProperties
)
340 *numberOfReturnedProperties
= 0;
345 /* The current ABI does not have any information on class properties. */
346 if (numberOfReturnedProperties
)
347 *numberOfReturnedProperties
= 0;
353 class_getIvarLayout (Class class_
__attribute__ ((__unused__
)))
359 class_getWeakIvarLayout (Class class_
__attribute__ ((__unused__
)))
365 class_setIvarLayout (Class class_
__attribute__ ((__unused__
)),
366 const char *layout
__attribute__ ((__unused__
)))
372 class_setWeakIvarLayout (Class class_
__attribute__ ((__unused__
)),
373 const char *layout
__attribute__ ((__unused__
)))