OSDN Git Service

061fa211fa8f4c5707d8aaafbd8d1c7d4b9760ad
[pf3gnuchains/gcc-fork.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 void *
64 object_getIndexedIvars (id object)
65 {
66   if (object == nil)
67     return NULL;
68   else
69     {
70       return (void *)(((char *)object) 
71                       + object->class_pointer->instance_size);
72     }
73 }
74
75 struct objc_ivar *
76 object_getInstanceVariable (id object, const char *name, void **returnValue)
77 {
78   if (object == nil  ||  name == NULL)
79     return NULL;
80   else
81     {
82       struct objc_ivar * variable = class_getInstanceVariable (object->class_pointer, name);
83
84       if (variable != NULL  &&  returnValue != NULL)
85         {
86           char *location = (char *)object + variable->ivar_offset;
87          
88           *returnValue = *((id *)location);
89         }
90
91       return variable;
92     }
93 }
94
95 struct objc_ivar *
96 object_setInstanceVariable (id object, const char *name, void *newValue)
97 {
98   if (object == nil  ||  name == NULL)
99     return NULL;
100   else
101     {
102       struct objc_ivar * variable = class_getInstanceVariable (object->class_pointer, name);
103
104       if (variable != NULL)
105         {
106           char *location = (char *)object + variable->ivar_offset;
107           
108           *((id *)location) = (id)newValue;
109         }
110
111       return variable;
112     }
113 }
114
115 id object_getIvar (id object, struct objc_ivar * variable)
116 {
117   if (object == nil  ||  variable == NULL)
118     return nil;
119   else
120     {
121       char *location = (char *)object + variable->ivar_offset;
122
123       return *((id *)location);
124     }
125 }
126
127 void object_setIvar (id object, struct objc_ivar * variable, id value)
128 {
129   if (object == nil  ||  variable == NULL)
130     return;
131   else
132     {
133       char *location = (char *)object + variable->ivar_offset;
134
135       *((id *)location) = value;
136     }
137 }
138
139 const char * ivar_getName (struct objc_ivar * variable)
140 {
141   return variable->ivar_name;
142 }
143
144 ptrdiff_t ivar_getOffset (struct objc_ivar * variable)
145 {
146   return (ptrdiff_t)(variable->ivar_offset);
147 }
148
149 const char * ivar_getTypeEncoding (struct objc_ivar * variable)
150 {
151   return variable->ivar_type;
152 }