2003-06-17 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mono / metadata / class.h
1 #ifndef _MONO_CLI_CLASS_H_
2 #define _MONO_CLI_CLASS_H_
3
4 #include <mono/metadata/metadata.h>
5 #include <mono/metadata/image.h>
6 #include <mono/metadata/loader.h>
7
8 #define MONO_CLASS_IS_ARRAY(c) ((c)->rank)
9
10 #define MONO_DEFAULT_SUPERTABLE_SIZE 6
11
12 extern gboolean mono_print_vtable;
13 typedef struct MonoVTable MonoVTable;
14
15 typedef struct {
16         MonoTypeEnum type;
17         gpointer value;
18 } MonoConstant;
19
20 typedef struct {
21         MonoType        *type;
22         int              offset;
23         const char      *name;
24         const char      *data;
25         MonoClass       *parent;
26         MonoConstant    *def_value;
27 } MonoClassField;
28
29 typedef struct {
30         MonoClassField *field;
31         guint32 offset;
32         MonoMarshalSpec *mspec;
33 } MonoMarshalField;
34
35 typedef struct {
36         guint32 native_size;
37         guint32 num_fields;
38         MonoMarshalField fields [MONO_ZERO_LEN_ARRAY];
39 } MonoMarshalType;
40
41 typedef struct {
42         const char *name;
43         MonoMethod *get;
44         MonoMethod *set;
45         guint32 attrs;
46 } MonoProperty;
47
48 typedef struct {
49         const char *name;
50         MonoMethod *add;
51         MonoMethod *remove;
52         MonoMethod *raise;
53         MonoMethod **other;
54         guint32 attrs;
55 } MonoEvent;
56
57 struct _MonoClass {
58         MonoImage *image;
59
60         /* The underlying type of the enum */
61         MonoType *enum_basetype;
62         /* element class for arrays and enum */
63         MonoClass *element_class; 
64         /* used for subtype checks */
65         MonoClass *cast_class; 
66         /* array dimension */
67         guint32    rank;          
68
69         guint inited          : 1;
70         /* We use init_pending to detect cyclic calls to mono_class_init */
71         guint init_pending    : 1;
72
73         /* A class contains static and non static data. Static data can be
74          * of the same type as the class itselfs, but it does not influence
75          * the instance size of the class. To avoid cyclic calls to 
76          * mono_class_init (from mono_class_instance_size ()) we first 
77          * initialise all non static fields. After that we set size_inited 
78          * to 1, because we know the instance size now. After that we 
79          * initialise all static fields.
80          */
81         guint size_inited     : 1;
82         guint valuetype       : 1; /* derives from System.ValueType */
83         guint enumtype        : 1; /* derives from System.Enum */
84         guint blittable       : 1; /* class is blittable */
85         guint unicode         : 1; /* class uses unicode char when marshalled */
86         guint wastypebuilder  : 1; /* class was created at runtime from a TypeBuilder */
87         /* next byte */
88         guint min_align       : 4;
89         guint packing_size    : 4;
90         /* next byte */
91         guint ghcimpl         : 1; /* class has its own GetHashCode impl */ 
92         guint has_finalize    : 1; /* class has its own Finalize impl */ 
93         guint marshalbyref    : 1; /* class is a MarshalByRefObject */
94         guint contextbound    : 1; /* class is a ContextBoundObject */
95         guint delegate        : 1; /* class is a Delegate */
96         guint gc_descr_inited : 1; /* gc_descr is initialized */
97         guint dummy           : 1; /* temporary hack */
98
99         MonoClass  *parent;
100         MonoClass  *nested_in;
101         GList      *nested_classes;
102         GList      *subclasses; /* list of all subclasses */
103
104         guint32    type_token;
105         const char *name;
106         const char *name_space;
107         
108         guint       interface_count;
109         guint       interface_id;        /* unique inderface id (for interfaces) */
110         guint       max_interface_id;
111         gint       *interface_offsets;   
112         MonoClass **interfaces;
113
114         /* for fast subtype checks */
115         guint       idepth;
116         MonoClass **supertypes;
117
118         /*
119          * Computed object instance size, total.
120          */
121         int        instance_size;
122         int        class_size;
123         int        vtable_size; /* number of slots */
124
125         /*
126          * From the TypeDef table
127          */
128         guint32    flags;
129         struct {
130                 guint32 first, last;
131                 int count;
132         } field, method, property, event;
133
134         /* loaded on demand */
135         MonoMarshalType *marshal_info;
136
137         /*
138          * Field information: Type and location from object base
139          */
140         MonoClassField *fields;
141
142         MonoProperty *properties;
143         
144         MonoEvent *events;
145
146         MonoMethod **methods;
147
148         /* used as the type of the this argument and when passing the arg by value */
149         MonoType this_arg;
150         MonoType byval_arg;
151
152         void *reflection_info;
153
154         void *gc_descr;
155         guint64 gc_bitmap;
156
157         MonoMethod *ptr_to_str;
158         MonoMethod *str_to_ptr;
159
160         MonoVTable *cached_vtable;
161         MonoMethod **vtable;    
162 };
163
164 struct MonoVTable {
165         MonoClass  *klass;
166     /*
167          * According to comments in gc_gcj.h, this should be the second word in
168          * the vtable.
169          */
170         void *gc_descr;         
171         MonoDomain *domain;  /* each object/vtable belongs to exactly one domain */
172         guint       max_interface_id;
173         gpointer   *interface_offsets;   
174         gpointer    data; /* to store static class data */
175         guint remote          : 1; /* class is remotely activated */
176         guint initialized     : 1; /* cctor has been run */
177         guint initializing    : 1; /* cctor is running */
178         /* do not add any fields after vtable, the structure is dynamically extended */
179         gpointer    vtable [MONO_ZERO_LEN_ARRAY];       
180 };
181
182 #define mono_class_has_parent(klass,parent) ((klass)->idepth >= (parent)->idepth) && ((klass)->supertypes [(parent)->idepth - 1] == (parent))
183
184 typedef struct {
185         gulong new_object_count;
186         gulong initialized_class_count;
187         gulong used_class_count;
188         gulong class_vtable_size;
189         gulong class_static_data_size;
190 } MonoStats;
191
192 extern MonoStats mono_stats;
193
194 typedef gpointer (*MonoTrampoline)       (MonoMethod *method);
195
196 typedef gpointer (*MonoLookupDynamicToken) (MonoImage *image, guint32 token);
197
198 MonoClass *
199 mono_class_get             (MonoImage *image, guint32 type_token);
200
201 void
202 mono_class_init            (MonoClass *klass);
203
204 void
205 mono_class_layout_fields   (MonoClass *klass);
206
207 void
208 mono_class_setup_vtable    (MonoClass *klass, MonoMethod **overrides, int onum);
209
210 MonoVTable *
211 mono_class_vtable          (MonoDomain *domain, MonoClass *klass);
212
213 MonoVTable *
214 mono_class_proxy_vtable    (MonoDomain *domain, MonoClass *klass);
215
216 void
217 mono_class_setup_mono_type (MonoClass *klass);
218
219 void
220 mono_class_setup_parent    (MonoClass *klass, MonoClass *parent);
221
222 void
223 mono_class_setup_supertypes (MonoClass *klass);
224
225 MonoClass *
226 mono_class_from_name       (MonoImage *image, const char* name_space, const char *name);
227
228 MonoClass *
229 mono_class_from_name_case  (MonoImage *image, const char* name_space, const char *name);
230
231 MonoClass * 
232 mono_class_from_typeref    (MonoImage *image, guint32 type_token);
233
234 MonoClassField*
235 mono_field_from_memberref  (MonoImage *image, guint32 token, MonoClass **retklass);
236
237 MonoClassField*
238 mono_field_from_token      (MonoImage *image, guint32 token, MonoClass **retklass);
239
240 MonoMethod**
241 mono_class_get_overrides   (MonoImage *image, guint32 type_token, gint32 *num_overrides);
242
243 MonoClass *
244 mono_array_class_get       (MonoType *element_type, guint32 rank);
245
246 MonoClass *
247 mono_ptr_class_get         (MonoType *type);
248
249 MonoClassField *
250 mono_class_get_field       (MonoClass *klass, guint32 field_token);
251
252 MonoClassField *
253 mono_class_get_field_from_name (MonoClass *klass, const char *name);
254
255 MonoProperty*
256 mono_class_get_property_from_name (MonoClass *klass, const char *name);
257
258 gint32
259 mono_array_element_size    (MonoClass *ac);
260
261 gint32
262 mono_class_instance_size   (MonoClass *klass);
263
264 gint32
265 mono_class_array_element_size (MonoClass *klass);
266
267 gint32
268 mono_class_data_size       (MonoClass *klass);
269
270 gint32
271 mono_class_value_size      (MonoClass *klass, guint32 *align);
272
273 gint32
274 mono_class_min_align       (MonoClass *klass);
275
276 MonoClass *
277 mono_class_from_mono_type  (MonoType *type);
278
279 gboolean
280 mono_class_is_subclass_of (MonoClass *klass, MonoClass *klassc, 
281                                                    gboolean check_interfaces);
282
283 gpointer
284 mono_ldtoken               (MonoImage *image, guint32 token, MonoClass **retclass);
285
286 void
287 mono_install_trampoline (MonoTrampoline func);
288
289 void
290 mono_install_remoting_trampoline (MonoTrampoline func);
291
292 gpointer
293 mono_lookup_dynamic_token (MonoImage *image, guint32 token);
294
295 void
296 mono_install_lookup_dynamic_token (MonoLookupDynamicToken func);
297
298 void    
299 mono_install_get_config_dir(void);
300
301 #endif /* _MONO_CLI_CLASS_H_ */