52b71af1124bb576476d835e301de91f4fbf4021
[gcc.git] / libobjc / ivars.c
1 /* GNU Objective C Runtime ivar related functions.
2 Copyright (C) 2010 Free Software Foundation, Inc.
3 Contributed by Nicola Pero
4
5 This file is part of GCC.
6
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.
10
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.
15
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.
19
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/>. */
24
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
32 struct objc_ivar *
33 class_getInstanceVariable (Class class_, const char *name)
34 {
35 if (class_ != Nil && name != NULL)
36 {
37 objc_mutex_lock (__objc_runtime_mutex);
38 while (class_ != Nil)
39 {
40 struct objc_ivar_list *ivars = class_->ivars;
41 if (ivars != NULL)
42 {
43 int i;
44
45 for (i = 0; i < ivars->ivar_count; i++)
46 {
47 struct objc_ivar *ivar = &(ivars->ivar_list[i]);
48
49 if (!strcmp (ivar->ivar_name, name))
50 {
51 objc_mutex_unlock (__objc_runtime_mutex);
52 return ivar;
53 }
54 }
55 }
56 class_ = class_->super_class;
57 }
58 objc_mutex_unlock (__objc_runtime_mutex);
59 }
60 return NULL;
61 }
62
63 struct objc_ivar *
64 class_getClassVariable (Class class_, const char *name)
65 {
66 if (class_ == Nil)
67 return NULL;
68
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.
75
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);
80 }
81
82 void *
83 object_getIndexedIvars (id object)
84 {
85 if (object == nil)
86 return NULL;
87 else
88 {
89 return (void *)(((char *)object)
90 + object->class_pointer->instance_size);
91 }
92 }
93
94 struct objc_ivar *
95 object_getInstanceVariable (id object, const char *name, void **returnValue)
96 {
97 if (object == nil || name == NULL)
98 return NULL;
99 else
100 {
101 struct objc_ivar * variable = class_getInstanceVariable (object->class_pointer, name);
102
103 if (variable != NULL && returnValue != NULL)
104 {
105 char *location = (char *)object + variable->ivar_offset;
106
107 *returnValue = *((id *)location);
108 }
109
110 return variable;
111 }
112 }
113
114 struct objc_ivar *
115 object_setInstanceVariable (id object, const char *name, void *newValue)
116 {
117 if (object == nil || name == NULL)
118 return NULL;
119 else
120 {
121 struct objc_ivar * variable = class_getInstanceVariable (object->class_pointer, name);
122
123 if (variable != NULL)
124 {
125 char *location = (char *)object + variable->ivar_offset;
126
127 *((id *)location) = (id)newValue;
128 }
129
130 return variable;
131 }
132 }
133
134 id object_getIvar (id object, struct objc_ivar * variable)
135 {
136 if (object == nil || variable == NULL)
137 return nil;
138 else
139 {
140 char *location = (char *)object + variable->ivar_offset;
141
142 return *((id *)location);
143 }
144 }
145
146 void object_setIvar (id object, struct objc_ivar * variable, id value)
147 {
148 if (object == nil || variable == NULL)
149 return;
150 else
151 {
152 char *location = (char *)object + variable->ivar_offset;
153
154 *((id *)location) = value;
155 }
156 }
157
158 const char * ivar_getName (struct objc_ivar * variable)
159 {
160 return variable->ivar_name;
161 }
162
163 ptrdiff_t ivar_getOffset (struct objc_ivar * variable)
164 {
165 return (ptrdiff_t)(variable->ivar_offset);
166 }
167
168 const char * ivar_getTypeEncoding (struct objc_ivar * variable)
169 {
170 return variable->ivar_type;
171 }