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 object_getIndexedIvars (id object)
70 return (void *)(((char *)object)
71 + object->class_pointer->instance_size);
76 object_getInstanceVariable (id object, const char *name, void **returnValue)
78 if (object == nil || name == NULL)
82 struct objc_ivar * variable = class_getInstanceVariable (object->class_pointer, name);
84 if (variable != NULL && returnValue != NULL)
86 char *location = (char *)object + variable->ivar_offset;
88 *returnValue = *((id *)location);
96 object_setInstanceVariable (id object, const char *name, void *newValue)
98 if (object == nil || name == NULL)
102 struct objc_ivar * variable = class_getInstanceVariable (object->class_pointer, name);
104 if (variable != NULL)
106 char *location = (char *)object + variable->ivar_offset;
108 *((id *)location) = (id)newValue;
115 id object_getIvar (id object, struct objc_ivar * variable)
117 if (object == nil || variable == NULL)
121 char *location = (char *)object + variable->ivar_offset;
123 return *((id *)location);
127 void object_setIvar (id object, struct objc_ivar * variable, id value)
129 if (object == nil || variable == NULL)
133 char *location = (char *)object + variable->ivar_offset;
135 *((id *)location) = value;
139 const char * ivar_getName (struct objc_ivar * variable)
141 return variable->ivar_name;
144 ptrdiff_t ivar_getOffset (struct objc_ivar * variable)
146 return (ptrdiff_t)(variable->ivar_offset);
149 const char * ivar_getTypeEncoding (struct objc_ivar * variable)
151 return variable->ivar_type;