2005-09-26 Carlos Alberto Cortez <calberto.cortez@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/io-layer/io-layer.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
14 typedef void     (*MonoStackWalkImpl) (MonoStackWalk func, gboolean do_il_offset, gpointer user_data);
15
16 typedef struct _MonoMethodNormal MonoMethodNormal;
17 typedef struct _MonoMethodWrapper MonoMethodWrapper;
18 typedef struct _MonoMethodInflated MonoMethodInflated;
19 typedef struct _MonoMethodPInvoke MonoMethodPInvoke;
20
21 /*
22  * remember to update wrapper_type_names if you change something here
23  */
24 typedef enum {
25         MONO_WRAPPER_NONE,
26         MONO_WRAPPER_DELEGATE_INVOKE,
27         MONO_WRAPPER_DELEGATE_BEGIN_INVOKE,
28         MONO_WRAPPER_DELEGATE_END_INVOKE,
29         MONO_WRAPPER_RUNTIME_INVOKE,
30         MONO_WRAPPER_NATIVE_TO_MANAGED,
31         MONO_WRAPPER_MANAGED_TO_NATIVE,
32         MONO_WRAPPER_REMOTING_INVOKE,
33         MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK,
34         MONO_WRAPPER_XDOMAIN_INVOKE,
35         MONO_WRAPPER_XDOMAIN_DISPATCH,
36         MONO_WRAPPER_LDFLD,
37         MONO_WRAPPER_STFLD,
38         MONO_WRAPPER_LDFLD_REMOTE,
39         MONO_WRAPPER_STFLD_REMOTE,
40         MONO_WRAPPER_SYNCHRONIZED,
41         MONO_WRAPPER_DYNAMIC_METHOD,
42         MONO_WRAPPER_ISINST,
43         MONO_WRAPPER_CASTCLASS,
44         MONO_WRAPPER_PROXY_ISINST,
45         MONO_WRAPPER_STELEMREF,
46         MONO_WRAPPER_UNBOX,
47         MONO_WRAPPER_UNKNOWN
48 } MonoWrapperType;
49
50 typedef enum {
51         MONO_TYPE_NAME_FORMAT_IL,
52         MONO_TYPE_NAME_FORMAT_REFLECTION,
53         MONO_TYPE_NAME_FORMAT_FULL_NAME,
54         MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED
55 } MonoTypeNameFormat;
56
57 typedef enum {
58         MONO_REMOTING_TARGET_UNKNOWN,
59         MONO_REMOTING_TARGET_APPDOMAIN
60 } MonoRemotingTarget;
61
62 struct _MonoMethod {
63         guint16 flags;  /* method flags */
64         guint16 iflags; /* method implementation flags */
65         guint32 token;
66         MonoClass *klass;
67         MonoMethodSignature *signature;
68         /* name is useful mostly for debugging */
69         const char *name;
70         /* this is used by the inlining algorithm */
71         unsigned int inline_info:1;
72         unsigned int uses_this:1;
73         unsigned int wrapper_type:5;
74         unsigned int string_ctor:1;
75         unsigned int save_lmf:1;
76         unsigned int dynamic:1; /* created & destroyed during runtime */
77         unsigned int is_inflated:1;
78         signed int slot : 21;
79 };
80
81 struct _MonoMethodNormal {
82         MonoMethod method;
83         MonoGenericContainer *generic_container;
84         MonoMethodHeader *header;
85 };
86
87 struct _MonoMethodWrapper {
88         MonoMethodNormal method;
89         void *method_data;
90 };
91
92 struct _MonoMethodInflated {
93         MonoMethodNormal nmethod;
94         MonoGenericContext *context;
95         MonoMethod *declaring;
96         MonoMethodInflated *inflated;
97 };
98
99 struct _MonoMethodPInvoke {
100         MonoMethod method;
101         gpointer addr;
102         /* add marshal info */
103         guint16 piflags;  /* pinvoke flags */
104         guint16 implmap_idx;  /* index into IMPLMAP */
105 };
106
107 typedef struct {
108         MonoType *generic_type;
109         gpointer reflection_info;
110 } MonoInflatedField;
111
112 /*
113  * MonoClassField is just a runtime representation of the metadata for
114  * field, it doesn't contain the data directly.  Static fields are
115  * stored in MonoVTable->data.  Instance fields are allocated in the
116  * objects after the object header.
117  */
118 struct _MonoClassField {
119         /* Type of the field */
120         MonoType        *type;
121
122         /* If this is an instantiated generic type, this is the
123          * "original" type, ie. the MONO_TYPE_VAR or MONO_TYPE_GENERICINST
124          * it was instantiated from.
125          */
126         MonoInflatedField  *generic_info;
127
128         /*
129          * Offset where this field is stored; if it is an instance
130          * field, it's the offset from the start of the object, if
131          * it's static, it's from the start of the memory chunk
132          * allocated for statics for the class.
133          */
134         int              offset;
135
136         const char      *name;
137
138         /*
139          * If the field is constant, pointer to the metadata constant
140          * value.
141          * If the field has an RVA flag, pointer to the data.
142          * Else, invalid.
143          */
144         const char      *data;
145
146         /* Type where the field was defined */
147         MonoClass       *parent;
148
149         /*
150          * If the field is constant, the type of the constant.
151          */
152         MonoTypeEnum     def_type;
153 };
154
155 /* a field is ignored if it's named "_Deleted" and it has the specialname and rtspecialname flags set */
156 #define mono_field_is_deleted(field) ((field)->name[0] == '_' && ((field)->type->attrs & 0x600) && (strcmp ((field)->name, "_Deleted") == 0))
157
158 typedef struct {
159         MonoClassField *field;
160         guint32 offset;
161         MonoMarshalSpec *mspec;
162 } MonoMarshalField;
163
164 typedef struct {
165         guint32 native_size;
166         guint32 num_fields;
167         MonoMethod *ptr_to_str;
168         MonoMethod *str_to_ptr;
169         MonoMarshalField fields [MONO_ZERO_LEN_ARRAY];
170 } MonoMarshalType;
171
172 struct _MonoProperty {
173         MonoClass *parent;
174         const char *name;
175         MonoMethod *get;
176         MonoMethod *set;
177         guint32 attrs;
178 };
179
180 struct _MonoEvent {
181         MonoClass *parent;
182         const char *name;
183         MonoMethod *add;
184         MonoMethod *remove;
185         MonoMethod *raise;
186         MonoMethod **other;
187         guint32 attrs;
188 };
189
190 /* type of exception being "on hold" for later processing (see exception_type) */
191 enum {
192         MONO_EXCEPTION_NONE = 0,
193         MONO_EXCEPTION_SECURITY_LINKDEMAND = 1,
194         MONO_EXCEPTION_SECURITY_INHERITANCEDEMAND = 2
195         /* add other exception type */
196 };
197
198 /* This struct collects the info needed for the runtime use of a class,
199  * like the vtables for a domain, the GC descriptor, etc.
200  */
201 typedef struct {
202         guint16 max_domain;
203         /* domain_vtables is indexed by the domain id and the size is max_domain + 1 */
204         MonoVTable *domain_vtables [MONO_ZERO_LEN_ARRAY];
205 } MonoClassRuntimeInfo;
206
207 struct _MonoClass {
208         MonoImage *image;
209
210         /* The underlying type of the enum */
211         MonoType *enum_basetype;
212         /* element class for arrays and enum */
213         MonoClass *element_class; 
214         /* used for subtype checks */
215         MonoClass *cast_class; 
216         /* array dimension */
217         guint8     rank;          
218
219         guint inited          : 1;
220         /* We use init_pending to detect cyclic calls to mono_class_init */
221         guint init_pending    : 1;
222
223         /* A class contains static and non static data. Static data can be
224          * of the same type as the class itselfs, but it does not influence
225          * the instance size of the class. To avoid cyclic calls to 
226          * mono_class_init (from mono_class_instance_size ()) we first 
227          * initialise all non static fields. After that we set size_inited 
228          * to 1, because we know the instance size now. After that we 
229          * initialise all static fields.
230          */
231         guint size_inited     : 1;
232         guint valuetype       : 1; /* derives from System.ValueType */
233         guint enumtype        : 1; /* derives from System.Enum */
234         guint blittable       : 1; /* class is blittable */
235         guint unicode         : 1; /* class uses unicode char when marshalled */
236         guint wastypebuilder  : 1; /* class was created at runtime from a TypeBuilder */
237         /* next byte */
238         guint min_align       : 4;
239         guint packing_size    : 4;
240         /* next byte */
241         guint ghcimpl         : 1; /* class has its own GetHashCode impl */ 
242         guint has_finalize    : 1; /* class has its own Finalize impl */ 
243         guint marshalbyref    : 1; /* class is a MarshalByRefObject */
244         guint contextbound    : 1; /* class is a ContextBoundObject */
245         guint delegate        : 1; /* class is a Delegate */
246         guint gc_descr_inited : 1; /* gc_descr is initialized */
247         guint has_cctor       : 1; /* class has a cctor */
248         guint dummy           : 1; /* temporary hack */
249         /* next byte */
250         guint has_references  : 1; /* it has GC-tracked references in the instance */
251         guint has_static_refs : 1; /* it has static fields that are GC-tracked */
252         guint no_special_static_fields : 1; /* has no thread/context static fields */
253
254         guint8     exception_type;      /* MONO_EXCEPTION_* */
255         void*      exception_data;      /* Additional information about the exception */
256         guint32    declsec_flags;       /* declarative security attributes flags */
257
258         MonoClass  *parent;
259         MonoClass  *nested_in;
260         GList      *nested_classes;
261
262         guint32    type_token;
263         const char *name;
264         const char *name_space;
265         
266         /* for fast subtype checks */
267         MonoClass **supertypes;
268         guint16     idepth;
269
270         guint16     interface_count;
271         guint16     interface_id;        /* unique inderface id (for interfaces) */
272         guint16     max_interface_id;
273         gint       *interface_offsets;   
274         MonoClass **interfaces;
275
276         /*
277          * Computed object instance size, total.
278          */
279         int        instance_size;
280         int        class_size;
281         int        vtable_size; /* number of slots */
282
283         /*
284          * From the TypeDef table
285          */
286         guint32    flags;
287         struct {
288                 guint32 first, count;
289         } field, method, property, event;
290
291         /* loaded on demand */
292         MonoMarshalType *marshal_info;
293
294         /*
295          * Field information: Type and location from object base
296          */
297         MonoClassField *fields;
298
299         /* Initialized by a call to mono_class_setup_properties () */
300         MonoProperty *properties;
301
302         /* Initialized by a call to mono_class_setup_events () */
303         MonoEvent *events;
304
305         MonoMethod **methods;
306
307         /* used as the type of the this argument and when passing the arg by value */
308         MonoType this_arg;
309         MonoType byval_arg;
310
311         MonoGenericClass *generic_class;
312         MonoGenericContainer *generic_container;
313
314         void *reflection_info;
315
316         void *gc_descr;
317
318         MonoClassRuntimeInfo *runtime_info;
319
320         /* Generic vtable. Initialized by a call to mono_class_setup_vtable () */
321         MonoMethod **vtable;    
322 };
323
324 struct MonoVTable {
325         MonoClass  *klass;
326     /*
327          * According to comments in gc_gcj.h, this should be the second word in
328          * the vtable.
329          */
330         void *gc_descr;         
331         MonoDomain *domain;  /* each object/vtable belongs to exactly one domain */
332         gpointer   *interface_offsets;   
333         gpointer    data; /* to store static class data */
334         gpointer    type; /* System.Type type for klass */
335         guint16     max_interface_id;
336         guint8      rank;
337         guint remote          : 1; /* class is remotely activated */
338         guint initialized     : 1; /* cctor has been run */
339         /* do not add any fields after vtable, the structure is dynamically extended */
340         gpointer    vtable [MONO_ZERO_LEN_ARRAY];       
341 };
342
343 /*
344  * Generic instantiation data type encoding.
345  */
346 struct _MonoGenericInst {
347         guint id;
348         guint type_argc    : 22;
349         guint is_open      :  1;
350         guint is_reference :  1;
351         MonoType **type_argv;
352 };
353
354 struct _MonoGenericClass {
355         MonoGenericInst *inst;
356         MonoClass *container_class;
357         MonoGenericContext *context;
358         guint is_dynamic  : 1;
359         guint is_inflated : 1;
360 };
361
362 struct _MonoInflatedGenericClass {
363         MonoGenericClass generic_class;
364         guint is_initialized   : 1;
365         MonoClass *klass;
366 };
367
368 struct _MonoDynamicGenericClass {
369         MonoInflatedGenericClass generic_class;
370         MonoType *parent;
371         int count_ifaces;
372         MonoType **ifaces;
373         int count_methods;
374         MonoMethod **methods;
375         int count_ctors;
376         MonoMethod **ctors;
377         int count_fields;
378         MonoClassField *fields;
379         int count_properties;
380         MonoProperty *properties;
381         int count_events;
382         MonoEvent *events;
383         guint initialized;
384 };
385
386 struct _MonoGenericMethod {
387         MonoGenericInst *inst;
388         MonoGenericClass *generic_class;
389         MonoGenericContainer *container;
390         gpointer reflection_info;
391 };
392
393 struct _MonoGenericContext {
394         MonoGenericContainer *container;
395         MonoGenericClass *gclass;
396         MonoGenericMethod *gmethod;
397 };
398
399 struct _MonoGenericContainer {
400         MonoGenericContext context;
401         MonoGenericContainer *parent;
402         GHashTable *method_hash;
403         MonoClass *klass;
404         int type_argc    : 6;
405         int is_method    : 1;
406         int is_signature : 1;
407         MonoGenericParam *type_params;
408 };
409
410 struct _MonoGenericParam {
411         MonoGenericContainer *owner;
412         MonoClass *pklass;
413         MonoMethod *method;
414         const char *name;
415         guint16 flags;
416         guint16 num;
417         MonoClass** constraints; /* NULL means end of list */
418 };
419
420 /*
421  * Class information which might be cached by the runtime in the AOT file for
422  * example. Caching this allows us to avoid computing a generic vtable
423  * (class->vtable) in most cases, saving time and avoiding creation of lots of
424  * MonoMethod structures.
425  */
426 typedef struct MonoCachedClassInfo {
427         guint32 vtable_size;
428         guint has_finalize : 1;
429         guint ghcimpl : 1;
430         guint has_cctor : 1;
431         guint has_nested_classes : 1;
432         guint blittable : 1;
433         guint has_references : 1;
434         guint has_static_refs : 1;
435         guint no_special_static_fields : 1;
436         guint32 cctor_token;
437         MonoImage *finalize_image;
438         guint32 finalize_token;
439         guint32 instance_size;
440         guint32 class_size;
441         guint32 packing_size;
442         guint32 min_align;
443 } MonoCachedClassInfo;
444
445 typedef struct {
446         const char *name;
447         gconstpointer func;
448         gconstpointer wrapper;
449         MonoMethodSignature *sig;
450 } MonoJitICallInfo;
451
452 /*
453  * Information about a type load error encountered by the loader.
454  */
455 typedef enum {
456         MONO_LOADER_ERROR_TYPE,
457         MONO_LOADER_ERROR_METHOD,
458         MONO_LOADER_ERROR_FIELD
459 } MonoLoaderErrorKind;
460
461 typedef struct {
462         MonoLoaderErrorKind kind;
463         char *class_name, *assembly_name; /* If kind == TYPE */
464         MonoClass *klass; /* If kind != TYPE */
465         const char *member_name; /* If kind != TYPE */
466 } MonoLoaderError;
467
468 #define mono_class_has_parent(klass,parent) (((klass)->idepth >= (parent)->idepth) && ((klass)->supertypes [(parent)->idepth - 1] == (parent)))
469
470 typedef struct {
471         gulong new_object_count;
472         gulong initialized_class_count;
473         gulong used_class_count;
474         gulong class_vtable_size;
475         gulong class_static_data_size;
476         gulong generic_instance_count;
477         gulong generic_class_count;
478         gulong inflated_method_count;
479         gulong inflated_method_count_2;
480         gulong inflated_type_count;
481         gulong generics_metadata_size;
482         gboolean enabled;
483 } MonoStats;
484
485 extern MonoStats mono_stats;
486
487 typedef gpointer (*MonoTrampoline)       (MonoMethod *method);
488 typedef gpointer (*MonoRemotingTrampoline)       (MonoMethod *method, MonoRemotingTarget target);
489 typedef gpointer (*MonoDelegateTrampoline)       (MonoMethod *method, gpointer addr);
490
491 typedef gpointer (*MonoLookupDynamicToken) (MonoImage *image, guint32 token, MonoClass **handle_class);
492
493 typedef gboolean (*MonoGetCachedClassInfo) (MonoClass *klass, MonoCachedClassInfo *res);
494
495 void
496 mono_classes_init (void);
497
498 void
499 mono_class_layout_fields   (MonoClass *klass);
500
501 void
502 mono_class_setup_vtable_general (MonoClass *klass, MonoMethod **overrides, int onum);
503
504 void
505 mono_class_setup_vtable (MonoClass *klass);
506
507 void
508 mono_class_setup_methods (MonoClass *klass);
509
510 void
511 mono_class_setup_mono_type (MonoClass *klass);
512
513 void
514 mono_class_setup_parent    (MonoClass *klass, MonoClass *parent);
515
516 void
517 mono_class_setup_supertypes (MonoClass *klass);
518
519 GPtrArray*
520 mono_class_get_implemented_interfaces (MonoClass *klass);
521
522 gboolean
523 mono_class_is_open_constructed_type (MonoType *t);
524
525 gboolean
526 mono_class_get_overrides_full (MonoImage *image, guint32 type_token, MonoMethod ***overrides, gint32 *num_overrides,
527                                MonoGenericContext *generic_context);
528
529 MonoMethod*
530 mono_class_get_cctor (MonoClass *klass);
531
532 MonoMethod*
533 mono_class_get_finalizer (MonoClass *klass);
534
535 gboolean
536 mono_class_needs_cctor_run (MonoClass *klass, MonoMethod *caller);
537
538 gboolean
539 mono_class_has_special_static_fields (MonoClass *klass);
540
541 void
542 mono_install_trampoline (MonoTrampoline func);
543
544 void
545 mono_install_remoting_trampoline (MonoRemotingTrampoline func);
546
547 void
548 mono_install_delegate_trampoline (MonoDelegateTrampoline func);
549
550 gpointer
551 mono_lookup_dynamic_token (MonoImage *image, guint32 token);
552
553 gpointer
554 mono_lookup_dynamic_token_class (MonoImage *image, guint32 token, MonoClass **handle_class);
555
556 void
557 mono_install_lookup_dynamic_token (MonoLookupDynamicToken func);
558
559 void
560 mono_install_get_cached_class_info (MonoGetCachedClassInfo func);
561
562 MonoInflatedGenericClass*
563 mono_get_inflated_generic_class (MonoGenericClass *gclass);
564
565 typedef struct {
566         MonoImage *corlib;
567         MonoClass *object_class;
568         MonoClass *byte_class;
569         MonoClass *void_class;
570         MonoClass *boolean_class;
571         MonoClass *sbyte_class;
572         MonoClass *int16_class;
573         MonoClass *uint16_class;
574         MonoClass *int32_class;
575         MonoClass *uint32_class;
576         MonoClass *int_class;
577         MonoClass *uint_class;
578         MonoClass *int64_class;
579         MonoClass *uint64_class;
580         MonoClass *single_class;
581         MonoClass *double_class;
582         MonoClass *char_class;
583         MonoClass *string_class;
584         MonoClass *enum_class;
585         MonoClass *array_class;
586         MonoClass *delegate_class;
587         MonoClass *multicastdelegate_class;
588         MonoClass *asyncresult_class;
589         MonoClass *waithandle_class;
590         MonoClass *typehandle_class;
591         MonoClass *fieldhandle_class;
592         MonoClass *methodhandle_class;
593         MonoClass *monotype_class;
594         MonoClass *exception_class;
595         MonoClass *threadabortexception_class;
596         MonoClass *thread_class;
597         MonoClass *transparent_proxy_class;
598         MonoClass *real_proxy_class;
599         MonoClass *mono_method_message_class;
600         MonoClass *appdomain_class;
601         MonoClass *field_info_class;
602         MonoClass *method_info_class;
603         MonoClass *stringbuilder_class;
604         MonoClass *math_class;
605         MonoClass *stack_frame_class;
606         MonoClass *stack_trace_class;
607         MonoClass *marshal_class;
608         MonoClass *iserializeable_class;
609         MonoClass *serializationinfo_class;
610         MonoClass *streamingcontext_class;
611         MonoClass *typed_reference_class;
612         MonoClass *argumenthandle_class;
613         MonoClass *marshalbyrefobject_class;
614         MonoClass *monitor_class;
615         MonoClass *iremotingtypeinfo_class;
616         MonoClass *runtimesecurityframe_class;
617         MonoClass *executioncontext_class;
618         MonoClass *generic_array_class;
619 } MonoDefaults;
620
621 extern MonoDefaults mono_defaults;
622
623 void
624 mono_loader_init           (void);
625
626 void
627 mono_loader_lock           (void);
628
629 void
630 mono_loader_unlock         (void);
631
632 void
633 mono_loader_set_error_type_load (char *class_name, char *assembly_name);
634
635 void
636 mono_loader_set_error_method_load (MonoClass *klass, const char *member_name);
637
638 void
639 mono_loader_set_error_field_load (MonoClass *klass, const char *member_name);
640
641 MonoLoaderError*
642 mono_loader_get_last_error (void);
643
644 void
645 mono_loader_clear_error    (void);
646
647 void 
648 mono_icall_init            (void);
649
650 void
651 mono_icall_cleanup         (void);
652
653 gpointer
654 mono_method_get_wrapper_data (MonoMethod *method, guint32 id);
655
656 void
657 mono_install_stack_walk (MonoStackWalkImpl func);
658
659 gboolean
660 mono_metadata_has_generic_params (MonoImage *image, guint32 token);
661
662 MonoGenericContainer *mono_metadata_load_generic_params (MonoImage *image, guint32 token,
663                                                          MonoGenericContainer *parent_container);
664
665 MonoMethodSignature*
666 mono_create_icall_signature (const char *sigstr);
667
668 MonoJitICallInfo *
669 mono_register_jit_icall (gconstpointer func, const char *name, MonoMethodSignature *sig, gboolean is_save);
670
671 void
672 mono_register_jit_icall_wrapper (MonoJitICallInfo *info, gconstpointer wrapper);
673
674 MonoJitICallInfo *
675 mono_find_jit_icall_by_name (const char *name);
676
677 MonoJitICallInfo *
678 mono_find_jit_icall_by_addr (gconstpointer addr);
679
680 MonoMethodSignature*
681 mono_class_inflate_generic_signature (MonoImage *image, MonoMethodSignature *sig, MonoGenericContext *context);
682
683 MonoMethodSignature *
684 mono_method_signature_full (MonoMethod *image, MonoGenericContext *context);
685
686 MonoGenericClass *
687 mono_get_shared_generic_class (MonoGenericContainer *container, gboolean is_dynamic);
688
689 gboolean
690 mono_class_set_failure (MonoClass *klass, guint32 ex_type, void *ex_data);
691
692 MonoException*
693 mono_class_get_exception_for_failure (MonoClass *klass);
694
695 char*
696 mono_type_get_name_full (MonoType *type, MonoTypeNameFormat format);
697
698 MonoArrayType *mono_dup_array_type (MonoArrayType *a);
699 MonoMethodSignature *mono_metadata_signature_deep_dup (MonoMethodSignature *sig);
700
701 #endif /* __MONO_METADATA_CLASS_INTERBALS_H__ */
702