18c6e8af29db44d229a23d08c1812bc9ebf29ca8
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/objc.h"
27 #include "objc/objc-api.h"
28 #include "objc-private/runtime.h" /* the kitchen sink */
29 #include <string.h> /* For strcmp */
32 class_getInstanceVariable (Class class_
, const char *name
)
34 if (class_
!= Nil
&& name
!= NULL
)
36 objc_mutex_lock (__objc_runtime_mutex
);
39 struct objc_ivar_list
*ivars
= class_
->ivars
;
44 for (i
= 0; i
< ivars
->ivar_count
; i
++)
46 struct objc_ivar
*ivar
= &(ivars
->ivar_list
[i
]);
48 if (!strcmp (ivar
->ivar_name
, name
))
50 objc_mutex_unlock (__objc_runtime_mutex
);
55 class_
= class_
->super_class
;
57 objc_mutex_unlock (__objc_runtime_mutex
);
63 object_getIndexedIvars (id object
)
69 return (void *)(((char *)object
)
70 + object
->class_pointer
->instance_size
);
75 object_getInstanceVariable (id object
, const char *name
, void **returnValue
)
77 if (object
== nil
|| name
== NULL
)
81 struct objc_ivar
* variable
= class_getInstanceVariable (object
->class_pointer
, name
);
83 if (variable
!= NULL
&& returnValue
!= NULL
)
85 char *location
= (char *)object
+ variable
->ivar_offset
;
87 *returnValue
= *((id
*)location
);
95 object_setInstanceVariable (id object
, const char *name
, void *newValue
)
97 if (object
== nil
|| name
== NULL
)
101 struct objc_ivar
* variable
= class_getInstanceVariable (object
->class_pointer
, name
);
103 if (variable
!= NULL
)
105 char *location
= (char *)object
+ variable
->ivar_offset
;
107 *((id
*)location
) = (id
)newValue
;
114 id
object_getIvar (id object
, struct objc_ivar
* variable
)
116 if (object
== nil
|| variable
== NULL
)
120 char *location
= (char *)object
+ variable
->ivar_offset
;
122 return *((id
*)location
);
126 void object_setIvar (id object
, struct objc_ivar
* variable
, id value
)
128 if (object
== nil
|| variable
== NULL
)
132 char *location
= (char *)object
+ variable
->ivar_offset
;
134 *((id
*)location
) = value
;
138 const char * ivar_getName (struct objc_ivar
* variable
)
140 return variable
->ivar_name
;
143 ptrdiff_t ivar_getOffset (struct objc_ivar
* variable
)
145 return (ptrdiff_t)(variable
->ivar_offset
);
148 const char * ivar_getTypeEncoding (struct objc_ivar
* variable
)
150 return variable
->ivar_type
;