18c6e8af29db44d229a23d08c1812bc9ebf29ca8
[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/objc.h"
27 #include "objc/objc-api.h"
28 #include "objc-private/runtime.h" /* the kitchen sink */
29 #include <string.h> /* For strcmp */
30
31 struct objc_ivar *
32 class_getInstanceVariable (Class class_, const char *name)
33 {
34 if (class_ != Nil && name != NULL)
35 {
36 objc_mutex_lock (__objc_runtime_mutex);
37 while (class_ != Nil)
38 {
39 struct objc_ivar_list *ivars = class_->ivars;
40 if (ivars != NULL)
41 {
42 int i;
43
44 for (i = 0; i < ivars->ivar_count; i++)
45 {
46 struct objc_ivar *ivar = &(ivars->ivar_list[i]);
47
48 if (!strcmp (ivar->ivar_name, name))
49 {
50 objc_mutex_unlock (__objc_runtime_mutex);
51 return ivar;
52 }
53 }
54 }
55 class_ = class_->super_class;
56 }
57 objc_mutex_unlock (__objc_runtime_mutex);
58 }
59 return NULL;
60 }
61
62 void *
63 object_getIndexedIvars (id object)
64 {
65 if (object == nil)
66 return NULL;
67 else
68 {
69 return (void *)(((char *)object)
70 + object->class_pointer->instance_size);
71 }
72 }
73
74 struct objc_ivar *
75 object_getInstanceVariable (id object, const char *name, void **returnValue)
76 {
77 if (object == nil || name == NULL)
78 return NULL;
79 else
80 {
81 struct objc_ivar * variable = class_getInstanceVariable (object->class_pointer, name);
82
83 if (variable != NULL && returnValue != NULL)
84 {
85 char *location = (char *)object + variable->ivar_offset;
86
87 *returnValue = *((id *)location);
88 }
89
90 return variable;
91 }
92 }
93
94 struct objc_ivar *
95 object_setInstanceVariable (id object, const char *name, void *newValue)
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)
104 {
105 char *location = (char *)object + variable->ivar_offset;
106
107 *((id *)location) = (id)newValue;
108 }
109
110 return variable;
111 }
112 }
113
114 id object_getIvar (id object, struct objc_ivar * variable)
115 {
116 if (object == nil || variable == NULL)
117 return nil;
118 else
119 {
120 char *location = (char *)object + variable->ivar_offset;
121
122 return *((id *)location);
123 }
124 }
125
126 void object_setIvar (id object, struct objc_ivar * variable, id value)
127 {
128 if (object == nil || variable == NULL)
129 return;
130 else
131 {
132 char *location = (char *)object + variable->ivar_offset;
133
134 *((id *)location) = value;
135 }
136 }
137
138 const char * ivar_getName (struct objc_ivar * variable)
139 {
140 return variable->ivar_name;
141 }
142
143 ptrdiff_t ivar_getOffset (struct objc_ivar * variable)
144 {
145 return (ptrdiff_t)(variable->ivar_offset);
146 }
147
148 const char * ivar_getTypeEncoding (struct objc_ivar * variable)
149 {
150 return variable->ivar_type;
151 }