Fix change log
[official-gcc.git] / libobjc / ivars.c
blob0dbb45b676af0bafab5949b7cd57ea744d3a8d17
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
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 */
32 struct objc_ivar *
33 class_getInstanceVariable (Class class_, const char *name)
35 if (class_ != Nil && name != NULL)
37 objc_mutex_lock (__objc_runtime_mutex);
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))
51 objc_mutex_unlock (__objc_runtime_mutex);
52 return ivar;
56 class_ = class_->super_class;
58 objc_mutex_unlock (__objc_runtime_mutex);
60 return NULL;
63 struct objc_ivar *
64 class_getClassVariable (Class class_, const char *name)
66 if (class_ == Nil)
67 return NULL;
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);
82 void *
83 object_getIndexedIvars (id object)
85 if (object == nil)
86 return NULL;
87 else
89 return (void *)(((char *)object)
90 + object->class_pointer->instance_size);
94 struct objc_ivar *
95 object_getInstanceVariable (id object, const char *name, void **returnValue)
97 if (object == nil || name == NULL)
98 return NULL;
99 else
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);
110 return variable;
114 struct objc_ivar *
115 object_setInstanceVariable (id object, const char *name, void *newValue)
117 if (object == nil || name == NULL)
118 return NULL;
119 else
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;
130 return variable;
134 id object_getIvar (id object, struct objc_ivar * variable)
136 if (object == nil || variable == NULL)
137 return nil;
138 else
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)
149 return;
150 else
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)
161 return NULL;
163 return variable->ivar_name;
166 ptrdiff_t ivar_getOffset (struct objc_ivar * variable)
168 if (variable == NULL)
169 return 0;
171 return (ptrdiff_t)(variable->ivar_offset);
174 const char * ivar_getTypeEncoding (struct objc_ivar * variable)
176 if (variable == NULL)
177 return 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;
188 if (class_ == Nil)
190 if (numberOfReturnedIvars)
191 *numberOfReturnedIvars = 0;
192 return NULL;
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);
204 /* Count how many ivars we have. */
205 ivar_list = class_->ivars;
206 count = ivar_list->ivar_count;
208 if (count != 0)
210 unsigned int i = 0;
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;
229 return returnValue;