2009-04-13 Zoltan Varga <vargaz@gmail.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/metadata/object.h>
6 #include <mono/metadata/mempool.h>
7 #include <mono/io-layer/io-layer.h>
8 #include "mono/utils/mono-compiler.h"
9
10 #define MONO_CLASS_IS_ARRAY(c) ((c)->rank)
11
12 #define MONO_CLASS_HAS_STATIC_METADATA(klass) ((klass)->type_token && !(klass)->image->dynamic && !(klass)->generic_class)
13
14 #define MONO_DEFAULT_SUPERTABLE_SIZE 6
15
16 extern gboolean mono_print_vtable;
17
18 extern gboolean mono_setup_vtable_in_class_init;
19
20 typedef void     (*MonoStackWalkImpl) (MonoStackWalk func, gboolean do_il_offset, gpointer user_data);
21
22 typedef struct _MonoMethodNormal MonoMethodNormal;
23 typedef struct _MonoMethodWrapper MonoMethodWrapper;
24 typedef struct _MonoMethodInflated MonoMethodInflated;
25 typedef struct _MonoMethodPInvoke MonoMethodPInvoke;
26
27 /* Properties that applies to a group of structs should better use a higher number
28  * to avoid colision with type specific properties.
29  * 
30  * This prop applies to class, method, property, event, assembly and image.
31  */
32 #define MONO_PROP_DYNAMIC_CATTR 0x1000
33
34 typedef enum {
35 #define WRAPPER(e,n) MONO_WRAPPER_ ## e,
36 #include "wrapper-types.h"
37 #undef WRAPPER
38         MONO_WRAPPER_NUM
39 } MonoWrapperType;
40
41 typedef enum {
42         MONO_TYPE_NAME_FORMAT_IL,
43         MONO_TYPE_NAME_FORMAT_REFLECTION,
44         MONO_TYPE_NAME_FORMAT_FULL_NAME,
45         MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED
46 } MonoTypeNameFormat;
47
48 typedef enum {
49         MONO_REMOTING_TARGET_UNKNOWN,
50         MONO_REMOTING_TARGET_APPDOMAIN,
51         MONO_REMOTING_TARGET_COMINTEROP
52 } MonoRemotingTarget;
53
54 #define MONO_METHOD_PROP_GENERIC_CONTAINER 0
55
56 struct _MonoMethod {
57         guint16 flags;  /* method flags */
58         guint16 iflags; /* method implementation flags */
59         guint32 token;
60         MonoClass *klass;
61         MonoMethodSignature *signature;
62         /* name is useful mostly for debugging */
63         const char *name;
64         /* this is used by the inlining algorithm */
65         unsigned int inline_info:1;
66         unsigned int inline_failure:1;
67         unsigned int wrapper_type:5;
68         unsigned int string_ctor:1;
69         unsigned int save_lmf:1;
70         unsigned int dynamic:1; /* created & destroyed during runtime */
71         unsigned int is_generic:1; /* whenever this is a generic method definition */
72         unsigned int is_inflated:1; /* whether we're a MonoMethodInflated */
73         unsigned int skip_visibility:1; /* whenever to skip JIT visibility checks */
74         unsigned int verification_success:1; /* whether this method has been verified successfully.*/
75         /* TODO we MUST get rid of this field, it's an ugly hack nobody is proud of. */
76         unsigned int is_mb_open : 1;            /* This is the fully open instantiation of a generic method_builder. Worse than is_tb_open, but it's temporary */
77         signed int slot : 17;
78
79         /*
80          * If is_generic is TRUE, the generic_container is stored in image->property_hash, 
81          * using the key MONO_METHOD_PROP_GENERIC_CONTAINER.
82          */
83 };
84
85 struct _MonoMethodNormal {
86         MonoMethod method;
87         MonoMethodHeader *header;
88 };
89
90 struct _MonoMethodWrapper {
91         MonoMethodNormal method;
92         void *method_data;
93 };
94
95 struct _MonoMethodPInvoke {
96         MonoMethod method;
97         gpointer addr;
98         /* add marshal info */
99         guint16 piflags;  /* pinvoke flags */
100         guint16 implmap_idx;  /* index into IMPLMAP */
101 };
102
103 /* 
104  * Stores the default value / RVA of fields.
105  * This information is rarely needed, so it is stored separately from MonoClassField.
106  */
107 typedef struct MonoFieldDefaultValue {
108         /*
109          * If the field is constant, pointer to the metadata constant
110          * value.
111          * If the field has an RVA flag, pointer to the data.
112          * Else, invalid.
113          */
114         const char      *data;
115
116         /* If the field is constant, the type of the constant. */
117         MonoTypeEnum     def_type;
118 } MonoFieldDefaultValue;
119
120 /*
121  * MonoClassField is just a runtime representation of the metadata for
122  * field, it doesn't contain the data directly.  Static fields are
123  * stored in MonoVTable->data.  Instance fields are allocated in the
124  * objects after the object header.
125  */
126 struct _MonoClassField {
127         /* Type of the field */
128         MonoType        *type;
129
130         const char      *name;
131
132         /* Type where the field was defined */
133         MonoClass       *parent;
134
135         /*
136          * Offset where this field is stored; if it is an instance
137          * field, it's the offset from the start of the object, if
138          * it's static, it's from the start of the memory chunk
139          * allocated for statics for the class.
140          */
141         int              offset;
142 };
143
144 /* a field is ignored if it's named "_Deleted" and it has the specialname and rtspecialname flags set */
145 #define mono_field_is_deleted(field) (((field)->type->attrs & (FIELD_ATTRIBUTE_SPECIAL_NAME | FIELD_ATTRIBUTE_RT_SPECIAL_NAME)) \
146                                       && (strcmp (mono_field_get_name (field), "_Deleted") == 0))
147
148 typedef struct {
149         MonoClassField *field;
150         guint32 offset;
151         MonoMarshalSpec *mspec;
152 } MonoMarshalField;
153
154 typedef struct {
155         guint32 native_size, min_align;
156         guint32 num_fields;
157         MonoMethod *ptr_to_str;
158         MonoMethod *str_to_ptr;
159         MonoMarshalField fields [MONO_ZERO_LEN_ARRAY];
160 } MonoMarshalType;
161
162 struct _MonoProperty {
163         MonoClass *parent;
164         const char *name;
165         MonoMethod *get;
166         MonoMethod *set;
167         guint32 attrs;
168 };
169
170 struct _MonoEvent {
171         MonoClass *parent;
172         const char *name;
173         MonoMethod *add;
174         MonoMethod *remove;
175         MonoMethod *raise;
176         MonoMethod **other;
177         guint32 attrs;
178 };
179
180 /* type of exception being "on hold" for later processing (see exception_type) */
181 enum {
182         MONO_EXCEPTION_NONE = 0,
183         MONO_EXCEPTION_SECURITY_LINKDEMAND = 1,
184         MONO_EXCEPTION_SECURITY_INHERITANCEDEMAND = 2,
185         MONO_EXCEPTION_INVALID_PROGRAM = 3,
186         MONO_EXCEPTION_UNVERIFIABLE_IL = 4,
187         MONO_EXCEPTION_MISSING_METHOD = 5,
188         MONO_EXCEPTION_MISSING_FIELD = 6,
189         MONO_EXCEPTION_TYPE_LOAD = 7,
190         MONO_EXCEPTION_FILE_NOT_FOUND = 8,
191         MONO_EXCEPTION_METHOD_ACCESS = 9,
192         MONO_EXCEPTION_FIELD_ACCESS = 10,
193         MONO_EXCEPTION_GENERIC_SHARING_FAILED = 11,
194         MONO_EXCEPTION_BAD_IMAGE = 12,
195         MONO_EXCEPTION_OBJECT_SUPPLIED = 13 /*The exception object is already created.*/
196         /* add other exception type */
197 };
198
199 /* This struct collects the info needed for the runtime use of a class,
200  * like the vtables for a domain, the GC descriptor, etc.
201  */
202 typedef struct {
203         guint16 max_domain;
204         /* domain_vtables is indexed by the domain id and the size is max_domain + 1 */
205         MonoVTable *domain_vtables [MONO_ZERO_LEN_ARRAY];
206 } MonoClassRuntimeInfo;
207
208 enum {
209         MONO_RGCTX_INFO_STATIC_DATA,
210         MONO_RGCTX_INFO_KLASS,
211         MONO_RGCTX_INFO_VTABLE,
212         MONO_RGCTX_INFO_TYPE,
213         MONO_RGCTX_INFO_REFLECTION_TYPE,
214         MONO_RGCTX_INFO_METHOD,
215         MONO_RGCTX_INFO_GENERIC_METHOD_CODE,
216         MONO_RGCTX_INFO_CLASS_FIELD,
217         MONO_RGCTX_INFO_METHOD_RGCTX,
218         MONO_RGCTX_INFO_METHOD_CONTEXT
219 };
220
221 typedef struct _MonoRuntimeGenericContextOtherInfoTemplate {
222         int info_type;
223         gpointer data;
224         struct _MonoRuntimeGenericContextOtherInfoTemplate *next;
225 } MonoRuntimeGenericContextOtherInfoTemplate;
226
227 typedef struct {
228         MonoClass *next_subclass;
229         MonoRuntimeGenericContextOtherInfoTemplate *other_infos;
230         GSList *method_templates;
231 } MonoRuntimeGenericContextTemplate;
232
233 typedef struct {
234         MonoVTable *class_vtable; /* must be the first element */
235         MonoGenericInst *method_inst;
236         gpointer infos [MONO_ZERO_LEN_ARRAY];
237 } MonoMethodRuntimeGenericContext;
238
239 #define MONO_RGCTX_SLOT_MAKE_RGCTX(i)   (i)
240 #define MONO_RGCTX_SLOT_MAKE_MRGCTX(i)  ((i) | 0x80000000)
241 #define MONO_RGCTX_SLOT_INDEX(s)        ((s) & 0x7fffffff)
242 #define MONO_RGCTX_SLOT_IS_MRGCTX(s)    (((s) & 0x80000000) ? TRUE : FALSE)
243
244
245 #define MONO_CLASS_PROP_EXCEPTION_DATA 0
246
247 /* 
248  * This structure contains the rarely used fields of MonoClass
249  * Since using just one field causes the whole structure to be allocated, it should
250  * be used for fields which are only used in like 5% of all classes.
251  */
252 typedef struct {
253         struct {
254                 guint32 first, count;
255         } property, event;
256
257         /* Initialized by a call to mono_class_setup_properties () */
258         MonoProperty *properties;
259
260         /* Initialized by a call to mono_class_setup_events () */
261         MonoEvent *events;
262
263         guint32    declsec_flags;       /* declarative security attributes flags */
264
265         /* Default values/RVA for fields */
266         /* Accessed using mono_class_get_field_default_value () / mono_field_get_data () */
267         MonoFieldDefaultValue *field_def_values;
268
269         GList      *nested_classes;
270 } MonoClassExt;
271
272 struct _MonoClass {
273         /* element class for arrays and enum basetype for enums */
274         MonoClass *element_class; 
275         /* used for subtype checks */
276         MonoClass *cast_class; 
277
278         /* for fast subtype checks */
279         MonoClass **supertypes;
280         guint16     idepth;
281
282         /* array dimension */
283         guint8     rank;          
284
285         int        instance_size; /* object instance size */
286
287         guint inited          : 1;
288         /* We use init_pending to detect cyclic calls to mono_class_init */
289         guint init_pending    : 1;
290
291         /* A class contains static and non static data. Static data can be
292          * of the same type as the class itselfs, but it does not influence
293          * the instance size of the class. To avoid cyclic calls to 
294          * mono_class_init (from mono_class_instance_size ()) we first 
295          * initialise all non static fields. After that we set size_inited 
296          * to 1, because we know the instance size now. After that we 
297          * initialise all static fields.
298          */
299         guint size_inited     : 1;
300         guint valuetype       : 1; /* derives from System.ValueType */
301         guint enumtype        : 1; /* derives from System.Enum */
302         guint blittable       : 1; /* class is blittable */
303         guint unicode         : 1; /* class uses unicode char when marshalled */
304         guint wastypebuilder  : 1; /* class was created at runtime from a TypeBuilder */
305         /* next byte */
306         guint8 min_align;
307         /* next byte */
308         guint packing_size    : 4;
309         /* still 4 bits free */
310         /* next byte */
311         guint ghcimpl         : 1; /* class has its own GetHashCode impl */ 
312         guint has_finalize    : 1; /* class has its own Finalize impl */ 
313         guint marshalbyref    : 1; /* class is a MarshalByRefObject */
314         guint contextbound    : 1; /* class is a ContextBoundObject */
315         guint delegate        : 1; /* class is a Delegate */
316         guint gc_descr_inited : 1; /* gc_descr is initialized */
317         guint has_cctor       : 1; /* class has a cctor */
318         guint has_references  : 1; /* it has GC-tracked references in the instance */
319         /* next byte */
320         guint has_static_refs : 1; /* it has static fields that are GC-tracked */
321         guint no_special_static_fields : 1; /* has no thread/context static fields */
322         /* directly or indirectly derives from ComImport attributed class.
323          * this means we need to create a proxy for instances of this class
324          * for COM Interop. set this flag on loading so all we need is a quick check
325          * during object creation rather than having to traverse supertypes
326          */
327         guint is_com_object : 1; 
328         guint nested_classes_inited : 1; /* Whenever nested_class is initialized */
329         guint interfaces_inited : 1; /* interfaces is initialized */
330         guint simd_type : 1; /* class is a simd intrinsic type */
331         guint is_generic : 1; /* class is a generic type definition */
332         guint is_inflated : 1; /* class is a generic instance */
333
334         guint8     exception_type;      /* MONO_EXCEPTION_* */
335
336         /* Additional information about the exception */
337         /* Stored as property MONO_CLASS_PROP_EXCEPTION_DATA */
338         //void       *exception_data;
339
340         MonoClass  *parent;
341         MonoClass  *nested_in;
342
343         MonoImage *image;
344         const char *name;
345         const char *name_space;
346
347         guint32    type_token;
348         int        vtable_size; /* number of slots */
349
350         guint16     interface_count;
351         guint16     interface_id;        /* unique inderface id (for interfaces) */
352         guint16     max_interface_id;
353         
354         guint16     interface_offsets_count;
355         MonoClass **interfaces_packed;
356         guint16    *interface_offsets_packed;
357         guint8     *interface_bitmap;
358         
359         MonoClass **interfaces;
360
361         union {
362                 int class_size; /* size of area for static fields */
363                 int element_size; /* for array types */
364                 int generic_param_token; /* for generic param types, both var and mvar */
365         } sizes;
366
367         /*
368          * From the TypeDef table
369          */
370         guint32    flags;
371         struct {
372                 guint32 first, count;
373         } field, method;
374
375         /* loaded on demand */
376         MonoMarshalType *marshal_info;
377
378         /*
379          * Field information: Type and location from object base
380          */
381         MonoClassField *fields;
382
383         MonoMethod **methods;
384
385         /* used as the type of the this argument and when passing the arg by value */
386         MonoType this_arg;
387         MonoType byval_arg;
388
389         MonoGenericClass *generic_class;
390         MonoGenericContainer *generic_container;
391
392         void *reflection_info;
393
394         void *gc_descr;
395
396         MonoClassRuntimeInfo *runtime_info;
397
398         /* next element in the class_cache hash list (in MonoImage) */
399         MonoClass *next_class_cache;
400
401         /* Generic vtable. Initialized by a call to mono_class_setup_vtable () */
402         MonoMethod **vtable;
403
404         /* Rarely used fields of classes */
405         MonoClassExt *ext;
406 };
407
408 #define MONO_CLASS_IMPLEMENTS_INTERFACE(k,uiid) (((uiid) <= (k)->max_interface_id) && ((k)->interface_bitmap [(uiid) >> 3] & (1 << ((uiid)&7))))
409 int mono_class_interface_offset (MonoClass *klass, MonoClass *itf);
410
411 typedef gpointer MonoRuntimeGenericContext;
412
413 /* the interface_offsets array is stored in memory before this struct */
414 struct MonoVTable {
415         MonoClass  *klass;
416          /*
417          * According to comments in gc_gcj.h, this should be the second word in
418          * the vtable.
419          */
420         void *gc_descr;         
421         MonoDomain *domain;  /* each object/vtable belongs to exactly one domain */
422         gpointer    data; /* to store static class data */
423         gpointer    type; /* System.Type type for klass */
424         guint8     *interface_bitmap;
425         guint16     max_interface_id;
426         guint8      rank;
427         guint remote          : 1; /* class is remotely activated */
428         guint initialized     : 1; /* cctor has been run */
429         guint init_failed     : 1; /* cctor execution failed */
430         guint32     imt_collisions_bitmap;
431         MonoRuntimeGenericContext *runtime_generic_context;
432         /* do not add any fields after vtable, the structure is dynamically extended */
433         gpointer    vtable [MONO_ZERO_LEN_ARRAY];       
434 };
435
436 #define MONO_VTABLE_IMPLEMENTS_INTERFACE(vt,uiid) (((uiid) <= (vt)->max_interface_id) && ((vt)->interface_bitmap [(uiid) >> 3] & (1 << ((uiid)&7))))
437
438 /*
439  * Generic instantiation data type encoding.
440  */
441
442 /*
443  * A particular generic instantiation:
444  *
445  * All instantiations are cached and we don't distinguish between class and method
446  * instantiations here.
447  */
448 struct _MonoGenericInst {
449         guint id;                       /* unique ID for debugging */
450         guint type_argc    : 22;        /* number of type arguments */
451         guint is_open      :  1;        /* if this is an open type */
452         MonoType *type_argv [MONO_ZERO_LEN_ARRAY];
453 };
454
455 /*
456  * The generic context: an instantiation of a set of class and method generic parameters.
457  *
458  * NOTE: Never allocate this directly on the heap.  It have to be either allocated on the stack,
459  *       or embedded within other objects.  Don't store pointers to this, because it may be on the stack.
460  *       If you really have to, ensure you store a pointer to the embedding object along with it.
461  */
462 struct _MonoGenericContext {
463         /* The instantiation corresponding to the class generic parameters */
464         MonoGenericInst *class_inst;
465         /* The instantiation corresponding to the method generic parameters */
466         MonoGenericInst *method_inst;
467 };
468
469 /*
470  * Inflated generic method.
471  */
472 struct _MonoMethodInflated {
473         union {
474                 MonoMethod method;
475                 MonoMethodNormal normal;
476                 MonoMethodPInvoke pinvoke;
477         } method;
478         MonoMethod *declaring;          /* the generic method definition. */
479         MonoGenericContext context;     /* The current instantiation */
480 };
481
482 /*
483  * A particular instantiation of a generic type.
484  */
485 struct _MonoGenericClass {
486         MonoClass *container_class;     /* the generic type definition */
487         MonoGenericContext context;     /* a context that contains the type instantiation doesn't contain any method instantiation */
488         guint is_dynamic  : 1;          /* We're a MonoDynamicGenericClass */
489         guint is_tb_open  : 1;          /* This is the fully open instantiation for a type_builder. Quite ugly, but it's temporary.*/
490         MonoClass *cached_class;        /* if present, the MonoClass corresponding to the instantiation.  */
491 };
492
493 /*
494  * This is used when instantiating a generic type definition which is
495  * a TypeBuilder.
496  */
497 struct _MonoDynamicGenericClass {
498         MonoGenericClass generic_class;
499         int count_methods;
500         MonoMethod **methods;
501         int count_ctors;
502         MonoMethod **ctors;
503         int count_fields;
504         MonoClassField *fields;
505         int count_properties;
506         MonoProperty *properties;
507         int count_events;
508         MonoEvent *events;
509         guint initialized;
510         /* The non-inflated types of the fields */
511         MonoType **field_generic_types;
512         /* The managed objects representing the fields */
513         MonoObject **field_objects;
514 };
515
516 /*
517  * The generic container.
518  *
519  * Stores the type parameters of a generic type definition or a generic method definition.
520  */
521 struct _MonoGenericContainer {
522         MonoGenericContext context;
523         /* If we're a generic method definition in a generic type definition,
524            the generic container of the containing class. */
525         MonoGenericContainer *parent;
526         /* the generic type definition or the generic method definition corresponding to this container */
527         union {
528                 MonoClass *klass;
529                 MonoMethod *method;
530         } owner;
531         int type_argc    : 31;
532         /* If true, we're a generic method, otherwise a generic type definition. */
533         /* Invariant: parent != NULL => is_method */
534         int is_method    : 1;
535         /* Our type parameters. */
536         MonoGenericParam *type_params;
537 };
538
539 #define mono_generic_container_get_param(gc, i) ((gc)->type_params + (i))
540 #define mono_generic_container_get_param_info(gc, i) (mono_generic_param_info ((gc)->type_params + (i)))
541
542 /* Additional details about a MonoGenericParam */
543 typedef struct {
544         MonoClass *pklass;              /* The corresponding `MonoClass'. */
545         const char *name;
546         guint16 flags;
547         MonoClass** constraints; /* NULL means end of list */
548 } MonoGenericParamInfo;
549
550 /*
551  * A type parameter.
552  */
553 struct _MonoGenericParam {
554         MonoGenericContainer *owner;    /* Type or method this parameter was defined in. */
555         guint16 num;
556         /* If owner is NULL, this is the image whose mempool this struct was allocated from */
557         MonoImage *image;
558
559         MonoGenericParamInfo info;
560 };
561
562 #define mono_generic_param_owner(p)             ((p)->owner)
563 #define mono_generic_param_num(p)               ((p)->num)
564 #define mono_generic_param_info(p)              (&(p)->info)
565 #define mono_type_get_generic_param_owner(t)    (mono_generic_param_owner ((t)->data.generic_param))
566 #define mono_type_get_generic_param_num(t)      (mono_generic_param_num   ((t)->data.generic_param))
567
568 /*
569  * Class information which might be cached by the runtime in the AOT file for
570  * example. Caching this allows us to avoid computing a generic vtable
571  * (class->vtable) in most cases, saving time and avoiding creation of lots of
572  * MonoMethod structures.
573  */
574 typedef struct MonoCachedClassInfo {
575         guint32 vtable_size;
576         guint has_finalize : 1;
577         guint ghcimpl : 1;
578         guint has_cctor : 1;
579         guint has_nested_classes : 1;
580         guint blittable : 1;
581         guint has_references : 1;
582         guint has_static_refs : 1;
583         guint no_special_static_fields : 1;
584         guint32 cctor_token;
585         MonoImage *finalize_image;
586         guint32 finalize_token;
587         guint32 instance_size;
588         guint32 class_size;
589         guint32 packing_size;
590         guint32 min_align;
591 } MonoCachedClassInfo;
592
593 typedef struct {
594         const char *name;
595         gconstpointer func;
596         gconstpointer wrapper;
597         gconstpointer trampoline;
598         MonoMethodSignature *sig;
599 } MonoJitICallInfo;
600
601 typedef struct {
602         guint8 exception_type;
603         char *class_name; /* If kind == TYPE */
604         char *assembly_name; /* If kind == TYPE or ASSEMBLY */
605         MonoClass *klass; /* If kind != TYPE */
606         const char *member_name; /* If kind != TYPE */
607         gboolean ref_only; /* If kind == ASSEMBLY */
608         char *msg; /* If kind == BAD_IMAGE */
609 } MonoLoaderError;
610
611 #define mono_class_has_parent(klass,parent) (((klass)->idepth >= (parent)->idepth) && ((klass)->supertypes [(parent)->idepth - 1] == (parent)))
612
613 typedef struct {
614         gulong new_object_count;
615         gulong initialized_class_count;
616         gulong generic_vtable_count;
617         gulong used_class_count;
618         gulong method_count;
619         gulong class_vtable_size;
620         gulong class_static_data_size;
621         gulong generic_instance_count;
622         gulong generic_class_count;
623         gulong inflated_method_count;
624         gulong inflated_method_count_2;
625         gulong inflated_type_count;
626         gulong generics_metadata_size;
627         gulong dynamic_code_alloc_count;
628         gulong dynamic_code_bytes_count;
629         gulong dynamic_code_frees_count;
630         gulong delegate_creations;
631         gulong imt_tables_size;
632         gulong imt_number_of_tables;
633         gulong imt_number_of_methods;
634         gulong imt_used_slots;
635         gulong imt_slots_with_collisions;
636         gulong imt_max_collisions_in_slot;
637         gulong imt_method_count_when_max_collisions;
638         gulong imt_thunks_size;
639         gulong jit_info_table_insert_count;
640         gulong jit_info_table_remove_count;
641         gulong jit_info_table_lookup_count;
642         gulong hazardous_pointer_count;
643         gulong generics_sharable_methods;
644         gulong generics_unsharable_methods;
645         gulong generics_shared_methods;
646         gulong minor_gc_count;
647         gulong major_gc_count;
648         gulong minor_gc_time_usecs;
649         gulong major_gc_time_usecs;
650         gboolean enabled;
651 } MonoStats;
652
653 /* 
654  * new structure to hold performace counters values that are exported
655  * to managed code.
656  * Note: never remove fields from this structure and only add them to the end.
657  * Size of fields and type should not be changed as well.
658  */
659 typedef struct {
660         /* JIT category */
661         guint32 jit_methods;
662         guint32 jit_bytes;
663         guint32 jit_time;
664         guint32 jit_failures;
665         /* Exceptions category */
666         guint32 exceptions_thrown;
667         guint32 exceptions_filters;
668         guint32 exceptions_finallys;
669         guint32 exceptions_depth;
670         guint32 aspnet_requests_queued;
671         guint32 aspnet_requests;
672         /* Memory category */
673         guint32 gc_collections0;
674         guint32 gc_collections1;
675         guint32 gc_collections2;
676         guint32 gc_promotions0;
677         guint32 gc_promotions1;
678         guint32 gc_promotion_finalizers;
679         guint32 gc_gen0size;
680         guint32 gc_gen1size;
681         guint32 gc_gen2size;
682         guint32 gc_lossize;
683         guint32 gc_fin_survivors;
684         guint32 gc_num_handles;
685         guint32 gc_allocated;
686         guint32 gc_induced;
687         guint32 gc_time;
688         guint32 gc_total_bytes;
689         guint32 gc_committed_bytes;
690         guint32 gc_reserved_bytes;
691         guint32 gc_num_pinned;
692         guint32 gc_sync_blocks;
693         /* Remoting category */
694         guint32 remoting_calls;
695         guint32 remoting_channels;
696         guint32 remoting_proxies;
697         guint32 remoting_classes;
698         guint32 remoting_objects;
699         guint32 remoting_contexts;
700         /* Loader category */
701         guint32 loader_classes;
702         guint32 loader_total_classes;
703         guint32 loader_appdomains;
704         guint32 loader_total_appdomains;
705         guint32 loader_assemblies;
706         guint32 loader_total_assemblies;
707         guint32 loader_failures;
708         guint32 loader_bytes;
709         guint32 loader_appdomains_uloaded;
710         /* Threads and Locks category  */
711         guint32 thread_contentions;
712         guint32 thread_queue_len;
713         guint32 thread_queue_max;
714         guint32 thread_num_logical;
715         guint32 thread_num_physical;
716         guint32 thread_cur_recognized;
717         guint32 thread_num_recognized;
718         /* Interop category */
719         guint32 interop_num_ccw;
720         guint32 interop_num_stubs;
721         guint32 interop_num_marshals;
722         /* Security category */
723         guint32 security_num_checks;
724         guint32 security_num_link_checks;
725         guint32 security_time;
726         guint32 security_depth;
727         guint32 unused;
728 } MonoPerfCounters;
729
730 extern MonoPerfCounters *mono_perfcounters MONO_INTERNAL;
731
732 void mono_perfcounters_init (void);
733
734 /*
735  * The definition of the first field in SafeHandle,
736  * Keep in sync with SafeHandle.cs, this is only used
737  * to access the `handle' parameter.
738  */
739 typedef struct {
740         MonoObject  base;
741         void       *handle;
742 } MonoSafeHandle;
743
744 /*
745  * Keep in sync with HandleRef.cs
746  */
747 typedef struct {
748         MonoObject *wrapper;
749         void       *handle;
750 } MonoHandleRef;
751
752 enum {
753         MONO_GENERIC_SHARING_NONE,
754         MONO_GENERIC_SHARING_COLLECTIONS,
755         MONO_GENERIC_SHARING_CORLIB,
756         MONO_GENERIC_SHARING_ALL
757 };
758
759 /*
760  * Flags for which contexts were used in inflating a generic.
761  */
762 enum {
763         MONO_GENERIC_CONTEXT_USED_CLASS = 1,
764         MONO_GENERIC_CONTEXT_USED_METHOD = 2
765 };
766
767 #define MONO_GENERIC_CONTEXT_USED_BOTH          (MONO_GENERIC_CONTEXT_USED_CLASS | MONO_GENERIC_CONTEXT_USED_METHOD)
768
769 extern MonoStats mono_stats MONO_INTERNAL;
770
771 typedef gpointer (*MonoTrampoline)       (MonoMethod *method);
772 typedef gpointer (*MonoJumpTrampoline)       (MonoDomain *domain, MonoMethod *method, gboolean add_sync_wrapper);
773 typedef gpointer (*MonoRemotingTrampoline)       (MonoDomain *domain, MonoMethod *method, MonoRemotingTarget target);
774 typedef gpointer (*MonoDelegateTrampoline)       (MonoClass *klass);
775
776 typedef gpointer (*MonoLookupDynamicToken) (MonoImage *image, guint32 token, gboolean valid_token, MonoClass **handle_class, MonoGenericContext *context);
777
778 typedef gboolean (*MonoGetCachedClassInfo) (MonoClass *klass, MonoCachedClassInfo *res);
779
780 typedef gboolean (*MonoGetClassFromName) (MonoImage *image, const char *name_space, const char *name, MonoClass **res);
781
782 void
783 mono_classes_init (void) MONO_INTERNAL;
784
785 void
786 mono_classes_cleanup (void) MONO_INTERNAL;
787
788 void
789 mono_class_layout_fields   (MonoClass *klass) MONO_INTERNAL;
790
791 void
792 mono_class_setup_interface_offsets (MonoClass *klass) MONO_INTERNAL;
793
794 void
795 mono_class_setup_vtable_general (MonoClass *klass, MonoMethod **overrides, int onum) MONO_INTERNAL;
796
797 void
798 mono_class_setup_vtable (MonoClass *klass) MONO_INTERNAL;
799
800 void
801 mono_class_setup_methods (MonoClass *klass) MONO_INTERNAL;
802
803 void
804 mono_class_setup_mono_type (MonoClass *klass) MONO_INTERNAL;
805
806 void
807 mono_class_setup_parent    (MonoClass *klass, MonoClass *parent) MONO_INTERNAL;
808
809 void
810 mono_class_setup_supertypes (MonoClass *klass) MONO_INTERNAL;
811
812 MonoMethod*
813 mono_class_get_method_by_index (MonoClass *class, int index) MONO_INTERNAL;
814
815 MonoMethod*
816 mono_class_get_inflated_method (MonoClass *class, MonoMethod *method) MONO_INTERNAL;
817
818 MonoMethod*
819 mono_class_get_vtable_entry (MonoClass *class, int offset) MONO_INTERNAL;
820
821 GPtrArray*
822 mono_class_get_implemented_interfaces (MonoClass *klass) MONO_INTERNAL;
823
824 gboolean
825 mono_class_is_open_constructed_type (MonoType *t) MONO_INTERNAL;
826
827 gboolean
828 mono_class_get_overrides_full (MonoImage *image, guint32 type_token, MonoMethod ***overrides, gint32 *num_overrides,
829                                MonoGenericContext *generic_context) MONO_INTERNAL;
830
831 MonoMethod*
832 mono_class_get_cctor (MonoClass *klass) MONO_INTERNAL;
833
834 MonoMethod*
835 mono_class_get_finalizer (MonoClass *klass) MONO_INTERNAL;
836
837 gboolean
838 mono_class_needs_cctor_run (MonoClass *klass, MonoMethod *caller) MONO_INTERNAL;
839
840 gboolean
841 mono_class_field_is_special_static (MonoClassField *field) MONO_INTERNAL;
842
843 gboolean
844 mono_class_has_special_static_fields (MonoClass *klass) MONO_INTERNAL;
845
846 const char*
847 mono_class_get_field_default_value (MonoClassField *field, MonoTypeEnum *def_type) MONO_INTERNAL;
848
849 void
850 mono_install_trampoline (MonoTrampoline func) MONO_INTERNAL;
851
852 void
853 mono_install_jump_trampoline (MonoJumpTrampoline func) MONO_INTERNAL;
854
855 void
856 mono_install_remoting_trampoline (MonoRemotingTrampoline func) MONO_INTERNAL;
857
858 void
859 mono_install_delegate_trampoline (MonoDelegateTrampoline func) MONO_INTERNAL;
860
861 gpointer
862 mono_lookup_dynamic_token (MonoImage *image, guint32 token, MonoGenericContext *context) MONO_INTERNAL;
863
864 gpointer
865 mono_lookup_dynamic_token_class (MonoImage *image, guint32 token, gboolean check_token, MonoClass **handle_class, MonoGenericContext *context) MONO_INTERNAL;
866
867 void
868 mono_install_lookup_dynamic_token (MonoLookupDynamicToken func) MONO_INTERNAL;
869
870 gpointer
871 mono_runtime_create_jump_trampoline (MonoDomain *domain, MonoMethod *method, gboolean add_sync_wrapper) MONO_INTERNAL;
872
873 gpointer
874 mono_runtime_create_delegate_trampoline (MonoClass *klass) MONO_INTERNAL;
875
876 void
877 mono_install_get_cached_class_info (MonoGetCachedClassInfo func) MONO_INTERNAL;
878
879 void
880 mono_install_get_class_from_name (MonoGetClassFromName func) MONO_INTERNAL;
881
882 MonoGenericContext*
883 mono_class_get_context (MonoClass *class) MONO_INTERNAL;
884
885 MonoGenericContext*
886 mono_method_get_context_general (MonoMethod *method, gboolean uninflated) MONO_INTERNAL;
887
888 MonoGenericContext*
889 mono_method_get_context (MonoMethod *method) MONO_INTERNAL;
890
891 /* Used by monodis, thus cannot be MONO_INTERNAL */
892 MonoGenericContainer*
893 mono_method_get_generic_container (MonoMethod *method);
894
895 MonoGenericContext*
896 mono_generic_class_get_context (MonoGenericClass *gclass) MONO_INTERNAL;
897
898 MonoClass*
899 mono_generic_class_get_class (MonoGenericClass *gclass) MONO_INTERNAL;
900
901 void
902 mono_method_set_generic_container (MonoMethod *method, MonoGenericContainer* container) MONO_INTERNAL;
903
904 MonoMethod*
905 mono_class_inflate_generic_method_full (MonoMethod *method, MonoClass *klass_hint, MonoGenericContext *context);
906
907 MonoMethodInflated*
908 mono_method_inflated_lookup (MonoMethodInflated* method, gboolean cache) MONO_INTERNAL;
909
910 MonoMethodSignature *
911 mono_metadata_get_inflated_signature (MonoMethodSignature *sig, MonoGenericContext *context);
912
913 MonoType*
914 mono_class_inflate_generic_type_with_mempool (MonoImage *image, MonoType *type, MonoGenericContext *context) MONO_INTERNAL;
915
916 MonoClass*
917 mono_class_inflate_generic_class (MonoClass *gklass, MonoGenericContext *context) MONO_INTERNAL;
918
919 void
920 mono_metadata_free_inflated_signature (MonoMethodSignature *sig);
921
922 typedef struct {
923         MonoImage *corlib;
924         MonoClass *object_class;
925         MonoClass *byte_class;
926         MonoClass *void_class;
927         MonoClass *boolean_class;
928         MonoClass *sbyte_class;
929         MonoClass *int16_class;
930         MonoClass *uint16_class;
931         MonoClass *int32_class;
932         MonoClass *uint32_class;
933         MonoClass *int_class;
934         MonoClass *uint_class;
935         MonoClass *int64_class;
936         MonoClass *uint64_class;
937         MonoClass *single_class;
938         MonoClass *double_class;
939         MonoClass *char_class;
940         MonoClass *string_class;
941         MonoClass *enum_class;
942         MonoClass *array_class;
943         MonoClass *delegate_class;
944         MonoClass *multicastdelegate_class;
945         MonoClass *asyncresult_class;
946         MonoClass *waithandle_class;
947         MonoClass *typehandle_class;
948         MonoClass *fieldhandle_class;
949         MonoClass *methodhandle_class;
950         MonoClass *systemtype_class;
951         MonoClass *monotype_class;
952         MonoClass *exception_class;
953         MonoClass *threadabortexception_class;
954         MonoClass *thread_class;
955         MonoClass *transparent_proxy_class;
956         MonoClass *real_proxy_class;
957         MonoClass *mono_method_message_class;
958         MonoClass *appdomain_class;
959         MonoClass *field_info_class;
960         MonoClass *method_info_class;
961         MonoClass *stringbuilder_class;
962         MonoClass *math_class;
963         MonoClass *stack_frame_class;
964         MonoClass *stack_trace_class;
965         MonoClass *marshal_class;
966         MonoClass *iserializeable_class;
967         MonoClass *serializationinfo_class;
968         MonoClass *streamingcontext_class;
969         MonoClass *typed_reference_class;
970         MonoClass *argumenthandle_class;
971         MonoClass *marshalbyrefobject_class;
972         MonoClass *monitor_class;
973         MonoClass *iremotingtypeinfo_class;
974         MonoClass *runtimesecurityframe_class;
975         MonoClass *executioncontext_class;
976         MonoClass *internals_visible_class;
977         MonoClass *generic_ilist_class;
978         MonoClass *generic_nullable_class;
979         MonoClass *variant_class;
980         MonoClass *com_object_class;
981         MonoClass *com_interop_proxy_class;
982         MonoClass *iunknown_class;
983         MonoClass *idispatch_class;
984         MonoClass *safehandle_class;
985         MonoClass *handleref_class;
986         MonoClass *attribute_class;
987         MonoClass *customattribute_data_class;
988         MonoClass *critical_finalizer_object;
989 } MonoDefaults;
990
991 extern MonoDefaults mono_defaults MONO_INTERNAL;
992
993 void
994 mono_loader_init           (void) MONO_INTERNAL;
995
996 void
997 mono_loader_cleanup        (void) MONO_INTERNAL;
998
999 void
1000 mono_loader_lock           (void) MONO_INTERNAL;
1001
1002 void
1003 mono_loader_unlock         (void) MONO_INTERNAL;
1004
1005 void
1006 mono_loader_set_error_assembly_load (const char *assembly_name, gboolean ref_only) MONO_INTERNAL;
1007
1008 void
1009 mono_loader_set_error_type_load (const char *class_name, const char *assembly_name) MONO_INTERNAL;
1010
1011 void
1012 mono_loader_set_error_method_load (const char *class_name, const char *member_name) MONO_INTERNAL;
1013
1014 void
1015 mono_loader_set_error_field_load (MonoClass *klass, const char *member_name) MONO_INTERNAL;
1016 void
1017 mono_loader_set_error_bad_image (char *msg) MONO_INTERNAL;
1018
1019 MonoException *
1020 mono_loader_error_prepare_exception (MonoLoaderError *error) MONO_INTERNAL;
1021
1022 MonoLoaderError *
1023 mono_loader_get_last_error (void) MONO_INTERNAL;
1024
1025 void
1026 mono_loader_clear_error    (void) MONO_INTERNAL;
1027
1028 void
1029 mono_reflection_init       (void) MONO_INTERNAL;
1030
1031 void
1032 mono_icall_init            (void) MONO_INTERNAL;
1033
1034 void
1035 mono_icall_cleanup         (void) MONO_INTERNAL;
1036
1037 gpointer
1038 mono_method_get_wrapper_data (MonoMethod *method, guint32 id) MONO_INTERNAL;
1039
1040 void
1041 mono_install_stack_walk (MonoStackWalkImpl func) MONO_INTERNAL;
1042
1043 gboolean
1044 mono_metadata_has_generic_params (MonoImage *image, guint32 token) MONO_INTERNAL;
1045
1046 MonoGenericContainer *
1047 mono_metadata_load_generic_params (MonoImage *image, guint32 token,
1048                                    MonoGenericContainer *parent_container);
1049
1050 void
1051 mono_metadata_load_generic_param_constraints (MonoImage *image, guint32 token,
1052                                               MonoGenericContainer *container);
1053
1054 MonoMethodSignature*
1055 mono_create_icall_signature (const char *sigstr) MONO_INTERNAL;
1056
1057 MonoJitICallInfo *
1058 mono_register_jit_icall (gconstpointer func, const char *name, MonoMethodSignature *sig, gboolean is_save) MONO_INTERNAL;
1059
1060 void
1061 mono_register_jit_icall_wrapper (MonoJitICallInfo *info, gconstpointer wrapper) MONO_INTERNAL;
1062
1063 MonoJitICallInfo *
1064 mono_find_jit_icall_by_name (const char *name) MONO_INTERNAL;
1065
1066 MonoJitICallInfo *
1067 mono_find_jit_icall_by_addr (gconstpointer addr) MONO_INTERNAL;
1068
1069 GHashTable*
1070 mono_get_jit_icall_info (void) MONO_INTERNAL;
1071
1072 gboolean
1073 mono_class_set_failure (MonoClass *klass, guint32 ex_type, void *ex_data) MONO_INTERNAL;
1074
1075 gpointer
1076 mono_class_get_exception_data (MonoClass *klass) MONO_INTERNAL;
1077
1078 MonoException*
1079 mono_class_get_exception_for_failure (MonoClass *klass) MONO_INTERNAL;
1080
1081 char*
1082 mono_type_get_name_full (MonoType *type, MonoTypeNameFormat format) MONO_INTERNAL;
1083
1084 char*
1085 mono_type_get_full_name (MonoClass *class) MONO_INTERNAL;
1086
1087 MonoArrayType *mono_dup_array_type (MonoImage *image, MonoArrayType *a) MONO_INTERNAL;
1088 MonoMethodSignature *mono_metadata_signature_deep_dup (MonoImage *image, MonoMethodSignature *sig) MONO_INTERNAL;
1089
1090 void
1091 mono_image_init_name_cache (MonoImage *image);
1092
1093 gboolean mono_class_is_nullable (MonoClass *klass) MONO_INTERNAL;
1094 MonoClass *mono_class_get_nullable_param (MonoClass *klass) MONO_INTERNAL;
1095
1096 /* object debugging functions, for use inside gdb */
1097 void mono_object_describe        (MonoObject *obj);
1098 void mono_object_describe_fields (MonoObject *obj);
1099 void mono_value_describe_fields  (MonoClass* klass, const char* addr);
1100 void mono_class_describe_statics (MonoClass* klass);
1101
1102 /*Enum validation related functions*/
1103 gboolean
1104 mono_type_is_valid_enum_basetype (MonoType * type);
1105
1106 gboolean
1107 mono_class_is_valid_enum (MonoClass *klass);
1108
1109 MonoType *
1110 mono_type_get_full        (MonoImage *image, guint32 type_token, MonoGenericContext *context) MONO_INTERNAL;
1111
1112 gboolean
1113 mono_generic_class_is_generic_type_definition (MonoGenericClass *gklass) MONO_INTERNAL;
1114
1115 MonoMethod*
1116 mono_method_get_declaring_generic_method (MonoMethod *method) MONO_INTERNAL;
1117
1118 MonoMethod*
1119 mono_class_get_method_generic (MonoClass *klass, MonoMethod *method) MONO_INTERNAL;
1120
1121 MonoType*
1122 mono_type_get_basic_type_from_generic (MonoType *type) MONO_INTERNAL;
1123
1124 gboolean
1125 mono_class_generic_sharing_enabled (MonoClass *class) MONO_INTERNAL;
1126
1127 gpointer
1128 mono_class_fill_runtime_generic_context (MonoVTable *class_vtable, guint32 slot) MONO_INTERNAL;
1129
1130 gpointer
1131 mono_method_fill_runtime_generic_context (MonoMethodRuntimeGenericContext *mrgctx, guint32 slot) MONO_INTERNAL;
1132
1133 MonoMethodRuntimeGenericContext*
1134 mono_method_lookup_rgctx (MonoVTable *class_vtable, MonoGenericInst *method_inst) MONO_INTERNAL;
1135
1136 gboolean
1137 mono_method_needs_static_rgctx_invoke (MonoMethod *method, gboolean allow_type_vars) MONO_INTERNAL;
1138
1139 int
1140 mono_class_rgctx_get_array_size (int n, gboolean mrgctx) MONO_INTERNAL;
1141
1142 guint32
1143 mono_method_lookup_or_register_other_info (MonoMethod *method, gboolean in_mrgctx, gpointer data,
1144         int info_type, MonoGenericContext *generic_context) MONO_INTERNAL;
1145
1146 MonoGenericContext
1147 mono_method_construct_object_context (MonoMethod *method) MONO_INTERNAL;
1148
1149 int
1150 mono_generic_context_check_used (MonoGenericContext *context) MONO_INTERNAL;
1151
1152 int
1153 mono_class_check_context_used (MonoClass *class) MONO_INTERNAL;
1154
1155 gboolean
1156 mono_generic_context_is_sharable (MonoGenericContext *context, gboolean allow_type_vars) MONO_INTERNAL;
1157
1158 gboolean
1159 mono_method_is_generic_impl (MonoMethod *method) MONO_INTERNAL;
1160 gboolean
1161 mono_method_is_generic_sharable_impl (MonoMethod *method, gboolean allow_type_vars) MONO_INTERNAL;
1162
1163 void
1164 mono_class_unregister_image_generic_subclasses (MonoImage *image) MONO_INTERNAL;
1165
1166 gboolean
1167 mono_method_can_access_method_full (MonoMethod *method, MonoMethod *called, MonoClass *context_klass) MONO_INTERNAL;
1168
1169 gboolean
1170 mono_method_can_access_field_full (MonoMethod *method, MonoClassField *field, MonoClass *context_klass) MONO_INTERNAL;
1171
1172 MonoClass *
1173 mono_class_get_generic_type_definition (MonoClass *klass) MONO_INTERNAL;
1174
1175 gboolean
1176 mono_class_has_parent_and_ignore_generics (MonoClass *klass, MonoClass *parent) MONO_INTERNAL;
1177
1178 int
1179 mono_method_get_vtable_slot (MonoMethod *method) MONO_INTERNAL;
1180
1181 int
1182 mono_method_get_vtable_index (MonoMethod *method) MONO_INTERNAL;
1183
1184 MonoMethod*
1185 mono_method_search_in_array_class (MonoClass *klass, const char *name, MonoMethodSignature *sig) MONO_INTERNAL;
1186
1187 void
1188 mono_class_setup_interface_id (MonoClass *class) MONO_INTERNAL;
1189
1190 MonoGenericContainer*
1191 mono_class_get_generic_container (MonoClass *klass) MONO_INTERNAL;
1192
1193 MonoGenericClass*
1194 mono_class_get_generic_class (MonoClass *klass) MONO_INTERNAL;
1195
1196 void
1197 mono_class_alloc_ext (MonoClass *klass) MONO_INTERNAL;
1198
1199 void
1200 mono_class_setup_interfaces (MonoClass *klass) MONO_INTERNAL;
1201
1202 #endif /* __MONO_METADATA_CLASS_INTERBALS_H__ */