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