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