Tue Aug 3 17:54:17 CEST 2004 Paolo Molaro <lupus@ximian.com>
[mono.git] / mono / metadata / class-internals.h
1 #ifndef __MONO_METADATA_CLASS_INTERBALS_H__
2 #define __MONO_METADATA_CLASS_INTERBALS_H__
3
4 #include <mono/metadata/class.h>
5 #include <mono/io-layer/io-layer.h>
6
7 #define MONO_CLASS_IS_ARRAY(c) ((c)->rank)
8
9 #define MONO_DEFAULT_SUPERTABLE_SIZE 6
10
11 extern gboolean mono_print_vtable;
12
13 typedef void     (*MonoStackWalkImpl) (MonoStackWalk func, gpointer user_data);
14
15 typedef struct _MonoMethodNormal MonoMethodNormal;
16 typedef struct _MonoMethodWrapper MonoMethodWrapper;
17 typedef struct _MonoMethodInflated MonoMethodInflated;
18 typedef struct _MonoMethodPInvoke MonoMethodPInvoke;
19
20 typedef enum {
21         MONO_WRAPPER_NONE,
22         MONO_WRAPPER_DELEGATE_INVOKE,
23         MONO_WRAPPER_DELEGATE_BEGIN_INVOKE,
24         MONO_WRAPPER_DELEGATE_END_INVOKE,
25         MONO_WRAPPER_RUNTIME_INVOKE,
26         MONO_WRAPPER_NATIVE_TO_MANAGED,
27         MONO_WRAPPER_MANAGED_TO_NATIVE,
28         MONO_WRAPPER_REMOTING_INVOKE,
29         MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK,
30         MONO_WRAPPER_LDFLD,
31         MONO_WRAPPER_STFLD,
32         MONO_WRAPPER_SYNCHRONIZED,
33         MONO_WRAPPER_DYNAMIC_METHOD,
34         MONO_WRAPPER_ISINST,
35         MONO_WRAPPER_CASTCLASS,
36         MONO_WRAPPER_PROXY_ISINST,
37         MONO_WRAPPER_STELEMREF,
38         MONO_WRAPPER_UNKNOWN
39 } MonoWrapperType;
40
41 struct _MonoMethod {
42         guint16 flags;  /* method flags */
43         guint16 iflags; /* method implementation flags */
44         guint32 token;
45         MonoClass *klass;
46         MonoMethodSignature *signature;
47         gpointer addr;
48         gpointer info; /* runtime info */
49         gpointer remoting_tramp; 
50         gint slot;
51         /* name is useful mostly for debugging */
52         const char *name;
53         /* this is used by the inlining algorithm */
54         unsigned int inline_info:1;
55         unsigned int uses_this:1;
56         unsigned int wrapper_type:5;
57         unsigned int string_ctor:1;
58         unsigned int save_lmf:1;
59         gint16 inline_count;
60 };
61
62 struct _MonoMethodNormal {
63         MonoMethod method;
64         MonoMethodHeader *header;
65 };
66
67 struct _MonoMethodWrapper {
68         MonoMethodNormal method;
69         GList *data;
70 };
71
72 struct _MonoMethodInflated {
73         MonoMethodNormal nmethod;
74         MonoGenericContext *context;
75         MonoMethod *declaring;
76 };
77
78 struct _MonoMethodPInvoke {
79         MonoMethod method;
80         void  (*code) (void);
81         /* add marshal info */
82         guint16 piflags;  /* pinvoke flags */
83         guint16 implmap_idx;  /* index into IMPLMAP */
84 };
85
86 typedef struct {
87         MonoType *generic_type;
88         gpointer reflection_info;
89 } MonoInflatedField;
90
91 /*
92  * MonoClassField is just a runtime representation of the metadata for
93  * field, it doesn't contain the data directly.  Static fields are
94  * stored in MonoVTable->data.  Instance fields are allocated in the
95  * objects after the object header.
96  */
97 struct _MonoClassField {
98         /* Type of the field */
99         MonoType        *type;
100
101         /* If this is an instantiated generic type, this is the
102          * "original" type, ie. the MONO_TYPE_VAR or MONO_TYPE_GENERICINST
103          * it was instantiated from.
104          */
105         MonoInflatedField  *generic_info;
106
107         /*
108          * Offset where this field is stored; if it is an instance
109          * field, it's the offset from the start of the object, if
110          * it's static, it's from the start of the memory chunk
111          * allocated for statics for the class.
112          */
113         int              offset;
114
115         const char      *name;
116
117         /*
118          * If the field is constant, pointer to the metadata constant
119          * value.
120          * If the field has an RVA flag, pointer to the data.
121          * Else, invalid.
122          */
123         const char      *data;
124
125         /* Type where the field was defined */
126         MonoClass       *parent;
127
128         /*
129          * If the field is constant, the type of the constant.
130          */
131         MonoTypeEnum     def_type;
132 };
133
134 /* a field is ignored if it's named "_Deleted" and it has the specialname and rtspecialname flags set */
135 #define mono_field_is_deleted(field) ((field)->name[0] == '_' && ((field)->type->attrs & 0x600) && (strcmp ((field)->name, "_Deleted") == 0))
136
137 typedef struct {
138         MonoClassField *field;
139         guint32 offset;
140         MonoMarshalSpec *mspec;
141 } MonoMarshalField;
142
143 typedef struct {
144         guint32 native_size;
145         guint32 num_fields;
146         MonoMarshalField fields [MONO_ZERO_LEN_ARRAY];
147 } MonoMarshalType;
148
149 struct _MonoProperty {
150         MonoClass *parent;
151         const char *name;
152         MonoMethod *get;
153         MonoMethod *set;
154         guint32 attrs;
155 };
156
157 struct _MonoEvent {
158         MonoClass *parent;
159         const char *name;
160         MonoMethod *add;
161         MonoMethod *remove;
162         MonoMethod *raise;
163         MonoMethod **other;
164         guint32 attrs;
165 };
166
167 struct _MonoClass {
168         MonoImage *image;
169
170         /* The underlying type of the enum */
171         MonoType *enum_basetype;
172         /* element class for arrays and enum */
173         MonoClass *element_class; 
174         /* used for subtype checks */
175         MonoClass *cast_class; 
176         /* array dimension */
177         guint32    rank;          
178
179         guint inited          : 1;
180         /* We use init_pending to detect cyclic calls to mono_class_init */
181         guint init_pending    : 1;
182
183         /* A class contains static and non static data. Static data can be
184          * of the same type as the class itselfs, but it does not influence
185          * the instance size of the class. To avoid cyclic calls to 
186          * mono_class_init (from mono_class_instance_size ()) we first 
187          * initialise all non static fields. After that we set size_inited 
188          * to 1, because we know the instance size now. After that we 
189          * initialise all static fields.
190          */
191         guint size_inited     : 1;
192         guint valuetype       : 1; /* derives from System.ValueType */
193         guint enumtype        : 1; /* derives from System.Enum */
194         guint blittable       : 1; /* class is blittable */
195         guint unicode         : 1; /* class uses unicode char when marshalled */
196         guint wastypebuilder  : 1; /* class was created at runtime from a TypeBuilder */
197         /* next byte */
198         guint min_align       : 4;
199         guint packing_size    : 4;
200         /* next byte */
201         guint ghcimpl         : 1; /* class has its own GetHashCode impl */ 
202         guint has_finalize    : 1; /* class has its own Finalize impl */ 
203         guint marshalbyref    : 1; /* class is a MarshalByRefObject */
204         guint contextbound    : 1; /* class is a ContextBoundObject */
205         guint delegate        : 1; /* class is a Delegate */
206         guint gc_descr_inited : 1; /* gc_descr is initialized */
207         guint dummy           : 1; /* temporary hack */
208
209         MonoClass  *parent;
210         MonoClass  *nested_in;
211         GList      *nested_classes;
212
213         guint32    type_token;
214         const char *name;
215         const char *name_space;
216         
217         guint       interface_count;
218         guint       interface_id;        /* unique inderface id (for interfaces) */
219         guint       max_interface_id;
220         gint       *interface_offsets;   
221         MonoClass **interfaces;
222
223         /* for fast subtype checks */
224         guint       idepth;
225         MonoClass **supertypes;
226
227         /*
228          * Computed object instance size, total.
229          */
230         int        instance_size;
231         int        class_size;
232         int        vtable_size; /* number of slots */
233
234         /*
235          * From the TypeDef table
236          */
237         guint32    flags;
238         struct {
239                 guint32 first, last;
240                 int count;
241         } field, method, property, event;
242
243         /* loaded on demand */
244         MonoMarshalType *marshal_info;
245
246         /*
247          * Field information: Type and location from object base
248          */
249         MonoClassField *fields;
250
251         MonoProperty *properties;
252         
253         MonoEvent *events;
254
255         MonoMethod **methods;
256
257         /* used as the type of the this argument and when passing the arg by value */
258         MonoType this_arg;
259         MonoType byval_arg;
260
261         MonoGenericInst *generic_inst;
262         MonoGenericParam *gen_params;
263         guint16 num_gen_params;
264
265         void *reflection_info;
266
267         void *gc_descr;
268         guint64 gc_bitmap;
269
270         MonoMethod *ptr_to_str;
271         MonoMethod *str_to_ptr;
272
273         MonoVTable *cached_vtable;
274         MonoMethod **vtable;    
275 };
276
277 struct MonoVTable {
278         MonoClass  *klass;
279     /*
280          * According to comments in gc_gcj.h, this should be the second word in
281          * the vtable.
282          */
283         void *gc_descr;         
284         MonoDomain *domain;  /* each object/vtable belongs to exactly one domain */
285         guint32       max_interface_id;
286         gpointer   *interface_offsets;   
287         gpointer    data; /* to store static class data */
288         gpointer    type; /* System.Type type for klass */
289         guint remote          : 1; /* class is remotely activated */
290         guint initialized     : 1; /* cctor has been run */
291         /* do not add any fields after vtable, the structure is dynamically extended */
292         gpointer    vtable [MONO_ZERO_LEN_ARRAY];       
293 };
294
295 /*
296  * Generic instantiation data type encoding.
297  */
298 struct _MonoGenericInst {
299         MonoGenericContext *context;
300         MonoClass *klass;
301         MonoType *parent;
302         int count_ifaces;
303         MonoType **ifaces;
304         MonoType *generic_type;
305         MonoDynamicGenericInst *dynamic_info;
306         int type_argc;
307         MonoType **type_argv;
308         guint is_open       : 1;
309         guint initialized   : 1;
310         guint init_pending  : 1;
311         guint is_dynamic    : 1;
312 };
313
314 struct _MonoDynamicGenericInst {
315         int count_methods;
316         MonoMethod **methods;
317         int count_ctors;
318         MonoMethod **ctors;
319         int count_fields;
320         MonoClassField *fields;
321         int count_properties;
322         MonoProperty *properties;
323         int count_events;
324         MonoEvent *events;
325 };
326
327 struct _MonoGenericMethod {
328         gpointer reflection_info;
329         int mtype_argc;
330         MonoType **mtype_argv;
331         guint is_open       : 1;
332 };
333
334 struct _MonoGenericContext {
335         MonoGenericInst *ginst;
336         MonoGenericMethod *gmethod;
337 };
338
339 struct _MonoGenericParam {
340         MonoClass *pklass;
341         MonoMethod *method;
342         const char *name;
343         guint16 flags;
344         guint16 num;
345         MonoClass** constraints; /* NULL means end of list */
346 };
347
348 typedef struct {
349         const char *name;
350         gconstpointer func;
351         gconstpointer wrapper;
352         MonoMethodSignature *sig;
353 } MonoJitICallInfo;
354
355 #define mono_class_has_parent(klass,parent) (((klass)->idepth >= (parent)->idepth) && ((klass)->supertypes [(parent)->idepth - 1] == (parent)))
356
357 typedef struct {
358         gulong new_object_count;
359         gulong initialized_class_count;
360         gulong used_class_count;
361         gulong class_vtable_size;
362         gulong class_static_data_size;
363         gulong generic_instance_count;
364         gulong inflated_method_count;
365         gulong inflated_type_count;
366         gulong generics_metadata_size;
367 } MonoStats;
368
369 extern MonoStats mono_stats;
370
371 typedef gpointer (*MonoTrampoline)       (MonoMethod *method);
372
373 typedef gpointer (*MonoLookupDynamicToken) (MonoImage *image, guint32 token);
374
375 void
376 mono_classes_init (void);
377
378 void
379 mono_class_layout_fields   (MonoClass *klass);
380
381 void
382 mono_class_setup_vtable    (MonoClass *klass, MonoMethod **overrides, int onum);
383
384 void
385 mono_class_setup_mono_type (MonoClass *klass);
386
387 void
388 mono_class_setup_parent    (MonoClass *klass, MonoClass *parent);
389
390 void
391 mono_class_setup_supertypes (MonoClass *klass);
392
393 gboolean
394 mono_class_is_open_constructed_type (MonoType *t);
395
396 MonoMethod**
397 mono_class_get_overrides   (MonoImage *image, guint32 type_token, gint32 *num_overrides);
398
399 gboolean
400 mono_class_needs_cctor_run (MonoClass *klass, MonoMethod *caller);
401
402 void
403 mono_install_trampoline (MonoTrampoline func);
404
405 void
406 mono_install_remoting_trampoline (MonoTrampoline func);
407
408 gpointer
409 mono_lookup_dynamic_token (MonoImage *image, guint32 token);
410
411 void
412 mono_install_lookup_dynamic_token (MonoLookupDynamicToken func);
413
414 typedef struct {
415         MonoImage *corlib;
416         MonoClass *object_class;
417         MonoClass *byte_class;
418         MonoClass *void_class;
419         MonoClass *boolean_class;
420         MonoClass *sbyte_class;
421         MonoClass *int16_class;
422         MonoClass *uint16_class;
423         MonoClass *int32_class;
424         MonoClass *uint32_class;
425         MonoClass *int_class;
426         MonoClass *uint_class;
427         MonoClass *int64_class;
428         MonoClass *uint64_class;
429         MonoClass *single_class;
430         MonoClass *double_class;
431         MonoClass *char_class;
432         MonoClass *string_class;
433         MonoClass *enum_class;
434         MonoClass *array_class;
435         MonoClass *delegate_class;
436         MonoClass *multicastdelegate_class;
437         MonoClass *asyncresult_class;
438         MonoClass *waithandle_class;
439         MonoClass *typehandle_class;
440         MonoClass *fieldhandle_class;
441         MonoClass *methodhandle_class;
442         MonoClass *monotype_class;
443         MonoClass *exception_class;
444         MonoClass *threadabortexception_class;
445         MonoClass *thread_class;
446         MonoClass *transparent_proxy_class;
447         MonoClass *real_proxy_class;
448         MonoClass *mono_method_message_class;
449         MonoClass *appdomain_class;
450         MonoClass *field_info_class;
451         MonoClass *method_info_class;
452         MonoClass *stringbuilder_class;
453         MonoClass *math_class;
454         MonoClass *stack_frame_class;
455         MonoClass *stack_trace_class;
456         MonoClass *marshal_class;
457         MonoClass *iserializeable_class;
458         MonoClass *serializationinfo_class;
459         MonoClass *streamingcontext_class;
460         MonoClass *typed_reference_class;
461         MonoClass *argumenthandle_class;
462         MonoClass *marshalbyrefobject_class;
463         MonoClass *monitor_class;
464         MonoClass *iremotingtypeinfo_class;
465 } MonoDefaults;
466
467 extern MonoDefaults mono_defaults;
468
469 void
470 mono_loader_init           (void);
471
472 void
473 mono_loader_lock           (void);
474
475 void
476 mono_loader_unlock           (void);
477
478 void 
479 mono_init_icall            (void);
480
481 gpointer
482 mono_method_get_wrapper_data (MonoMethod *method, guint32 id);
483
484 void
485 mono_install_stack_walk (MonoStackWalkImpl func);
486
487 MonoGenericParam *mono_metadata_load_generic_params (MonoImage *image, guint32 token, guint32 *num);
488
489 MonoMethodSignature*
490 mono_create_icall_signature (const char *sigstr);
491
492 MonoJitICallInfo *
493 mono_register_jit_icall (gconstpointer func, const char *name, MonoMethodSignature *sig, gboolean is_save);
494
495 void
496 mono_register_jit_icall_wrapper (MonoJitICallInfo *info, gconstpointer wrapper);
497
498 MonoJitICallInfo *
499 mono_find_jit_icall_by_name (const char *name);
500
501 MonoJitICallInfo *
502 mono_find_jit_icall_by_addr (gconstpointer addr);
503
504 #endif /* __MONO_METADATA_CLASS_INTERBALS_H__ */
505