1 /* GNU Objective C Runtime ivar related functions.
2 Copyright (C) 2010 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 */
33 class_getInstanceVariable (Class class_
, const char *name
)
35 if (class_
!= Nil
&& name
!= NULL
)
37 objc_mutex_lock (__objc_runtime_mutex
);
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
))
51 objc_mutex_unlock (__objc_runtime_mutex
);
56 class_
= class_
->super_class
;
58 objc_mutex_unlock (__objc_runtime_mutex
);
64 class_getClassVariable (Class class_
, const char *name
)
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.
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
);
83 object_getIndexedIvars (id object
)
89 return (void *)(((char *)object
)
90 + object
->class_pointer
->instance_size
);
95 object_getInstanceVariable (id object
, const char *name
, void **returnValue
)
97 if (object
== nil
|| name
== NULL
)
101 struct objc_ivar
* variable
= class_getInstanceVariable (object
->class_pointer
, name
);
103 if (variable
!= NULL
&& returnValue
!= NULL
)
105 char *location
= (char *)object
+ variable
->ivar_offset
;
107 *returnValue
= *((id
*)location
);
115 object_setInstanceVariable (id object
, const char *name
, void *newValue
)
117 if (object
== nil
|| name
== NULL
)
121 struct objc_ivar
* variable
= class_getInstanceVariable (object
->class_pointer
, name
);
123 if (variable
!= NULL
)
125 char *location
= (char *)object
+ variable
->ivar_offset
;
127 *((id
*)location
) = (id
)newValue
;
134 id
object_getIvar (id object
, struct objc_ivar
* variable
)
136 if (object
== nil
|| variable
== NULL
)
140 char *location
= (char *)object
+ variable
->ivar_offset
;
142 return *((id
*)location
);
146 void object_setIvar (id object
, struct objc_ivar
* variable
, id value
)
148 if (object
== nil
|| variable
== NULL
)
152 char *location
= (char *)object
+ variable
->ivar_offset
;
154 *((id
*)location
) = value
;
158 const char * ivar_getName (struct objc_ivar
* variable
)
160 if (variable
== NULL
)
163 return variable
->ivar_name
;
166 ptrdiff_t ivar_getOffset (struct objc_ivar
* variable
)
168 if (variable
== NULL
)
171 return (ptrdiff_t)(variable
->ivar_offset
);
174 const char * ivar_getTypeEncoding (struct objc_ivar
* variable
)
176 if (variable
== NULL
)
179 return variable
->ivar_type
;
182 struct objc_ivar
** class_copyIvarList (Class class_
, unsigned int *numberOfReturnedIvars
)
184 unsigned int count
= 0;
185 struct objc_ivar
**returnValue
= NULL
;
186 struct objc_ivar_list
* ivar_list
;
190 if (numberOfReturnedIvars
)
191 *numberOfReturnedIvars
= 0;
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
202 objc_mutex_lock (__objc_runtime_mutex
);
204 /* Count how many ivars we have. */
205 ivar_list
= class_
->ivars
;
206 count
= ivar_list
->ivar_count
;
212 /* Allocate enough memory to hold them. */
213 returnValue
= (struct objc_ivar
**)(malloc (sizeof (struct objc_ivar
*) * (count
+ 1)));
215 /* Copy the ivars. */
216 for (i
= 0; i
< count
; i
++)
218 returnValue
[i
] = &(ivar_list
->ivar_list
[i
]);
221 returnValue
[i
] = NULL
;
224 objc_mutex_unlock (__objc_runtime_mutex
);
226 if (numberOfReturnedIvars
)
227 *numberOfReturnedIvars
= count
;
233 property_getName (struct objc_property
* property
__attribute__ ((__unused__
)))
235 if (property
== NULL
)
239 /* The current ABI does not have any information on properties. */
244 property_getAttributes (struct objc_property
* property
__attribute__ ((__unused__
)))
246 if (property
== NULL
)
250 /* The current ABI does not have any information on properties. */
254 struct objc_property
*
255 class_getProperty (Class class_
__attribute__ ((__unused__
)),
256 const char *propertyName
__attribute__ ((__unused__
)))
258 if (class_
== NULL
|| propertyName
== NULL
)
262 /* The current ABI does not have any information on class properties. */
266 struct objc_property
**
267 class_copyPropertyList (Class class_
__attribute__ ((__unused__
)),
268 unsigned int *numberOfReturnedProperties
__attribute__ ((__unused__
)))
272 if (numberOfReturnedProperties
)
273 *numberOfReturnedProperties
= 0;
278 /* The current ABI does not have any information on class properties. */
279 if (numberOfReturnedProperties
)
280 *numberOfReturnedProperties
= 0;