2005-06-29 Miguel de Icaza <miguel@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/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
253         guint32    declsec_flags;       /* declarative security attributes flags */
254         guint32    exception_type;      /* MONO_EXCEPTION_* */
255         void*      exception_data;      /* Additional information about the exception */
256
257         MonoClass  *parent;
258         MonoClass  *nested_in;
259         GList      *nested_classes;
260
261         guint32    type_token;
262         const char *name;
263         const char *name_space;
264         
265         /* for fast subtype checks */
266         MonoClass **supertypes;
267         guint16     idepth;
268
269         guint16     interface_count;
270         guint16     interface_id;        /* unique inderface id (for interfaces) */
271         guint16     max_interface_id;
272         gint       *interface_offsets;   
273         MonoClass **interfaces;
274
275         /*
276          * Computed object instance size, total.
277          */
278         int        instance_size;
279         int        class_size;
280         int        vtable_size; /* number of slots */
281
282         /*
283          * From the TypeDef table
284          */
285         guint32    flags;
286         struct {
287                 guint32 first, last;
288                 int 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         MonoClass *klass;
359         MonoType *parent;
360         guint count_ifaces  : 29;
361         guint is_dynamic    :  1;
362         MonoType **ifaces;
363 };
364
365 struct _MonoDynamicGenericClass {
366         MonoGenericClass generic_class;
367         int count_methods;
368         MonoMethod **methods;
369         int count_ctors;
370         MonoMethod **ctors;
371         int count_fields;
372         MonoClassField *fields;
373         int count_properties;
374         MonoProperty *properties;
375         int count_events;
376         MonoEvent *events;
377         guint initialized;
378 };
379
380 struct _MonoGenericMethod {
381         MonoGenericInst *inst;
382         MonoGenericClass *generic_class;
383         MonoGenericContainer *container;
384         gpointer reflection_info;
385 };
386
387 struct _MonoGenericContext {
388         MonoGenericContainer *container;
389         MonoGenericClass *gclass;
390         MonoGenericMethod *gmethod;
391 };
392
393 struct _MonoGenericContainer {
394         MonoGenericContext context;
395         MonoGenericContainer *parent;
396         GHashTable *method_hash;
397         MonoClass *klass;
398         int type_argc    : 6;
399         int is_method    : 1;
400         int is_signature : 1;
401         MonoGenericParam *type_params;
402 };
403
404 struct _MonoGenericParam {
405         MonoGenericContainer *owner;
406         MonoClass *pklass;
407         MonoMethod *method;
408         const char *name;
409         guint16 flags;
410         guint16 num;
411         MonoClass** constraints; /* NULL means end of list */
412 };
413
414 /*
415  * Class information which might be cached by the runtime in the AOT file for
416  * example. Caching this allows us to avoid computing a generic vtable
417  * (class->vtable) in most cases, saving time and avoiding creation of lots of
418  * MonoMethod structures.
419  */
420 typedef struct MonoCachedClassInfo {
421         guint32 vtable_size;
422         guint has_finalize : 1;
423         guint ghcimpl : 1;
424         guint has_cctor : 1;
425         guint32 cctor_token;
426         MonoImage *finalize_image;
427         guint32 finalize_token;
428 } MonoCachedClassInfo;
429
430 typedef struct {
431         const char *name;
432         gconstpointer func;
433         gconstpointer wrapper;
434         MonoMethodSignature *sig;
435 } MonoJitICallInfo;
436
437 #define mono_class_has_parent(klass,parent) (((klass)->idepth >= (parent)->idepth) && ((klass)->supertypes [(parent)->idepth - 1] == (parent)))
438
439 typedef struct {
440         gulong new_object_count;
441         gulong initialized_class_count;
442         gulong used_class_count;
443         gulong class_vtable_size;
444         gulong class_static_data_size;
445         gulong generic_instance_count;
446         gulong generic_class_count;
447         gulong inflated_method_count;
448         gulong inflated_method_count_2;
449         gulong inflated_type_count;
450         gulong generics_metadata_size;
451         gboolean enabled;
452 } MonoStats;
453
454 extern MonoStats mono_stats;
455
456 typedef gpointer (*MonoTrampoline)       (MonoMethod *method);
457 typedef gpointer (*MonoRemotingTrampoline)       (MonoMethod *method, MonoRemotingTarget target);
458
459 typedef gpointer (*MonoLookupDynamicToken) (MonoImage *image, guint32 token, MonoClass **handle_class);
460
461 typedef gboolean (*MonoGetCachedClassInfo) (MonoClass *klass, MonoCachedClassInfo *res);
462
463 void
464 mono_classes_init (void);
465
466 void
467 mono_class_layout_fields   (MonoClass *klass);
468
469 void
470 mono_class_setup_vtable_general (MonoClass *klass, MonoMethod **overrides, int onum);
471
472 void
473 mono_class_setup_vtable (MonoClass *klass);
474
475 void
476 mono_class_setup_methods (MonoClass *klass);
477
478 void
479 mono_class_setup_mono_type (MonoClass *klass);
480
481 void
482 mono_class_setup_parent    (MonoClass *klass, MonoClass *parent);
483
484 void
485 mono_class_setup_supertypes (MonoClass *klass);
486
487 GPtrArray*
488 mono_class_get_implemented_interfaces (MonoClass *klass);
489
490 gboolean
491 mono_class_is_open_constructed_type (MonoType *t);
492
493 MonoMethod**
494 mono_class_get_overrides_full (MonoImage *image, guint32 type_token, gint32 *num_overrides,
495                                MonoGenericContext *generic_context);
496
497 MonoMethod*
498 mono_class_get_cctor (MonoClass *klass);
499
500 MonoMethod*
501 mono_class_get_finalizer (MonoClass *klass);
502
503 gboolean
504 mono_class_needs_cctor_run (MonoClass *klass, MonoMethod *caller);
505
506 void
507 mono_install_trampoline (MonoTrampoline func);
508
509 void
510 mono_install_remoting_trampoline (MonoRemotingTrampoline func);
511
512 gpointer
513 mono_lookup_dynamic_token (MonoImage *image, guint32 token);
514
515 gpointer
516 mono_lookup_dynamic_token_class (MonoImage *image, guint32 token, MonoClass **handle_class);
517
518 void
519 mono_install_lookup_dynamic_token (MonoLookupDynamicToken func);
520
521 void
522 mono_install_get_cached_class_info (MonoGetCachedClassInfo func);
523
524 void
525 mono_class_create_generic (MonoGenericClass *gclass);
526
527 typedef struct {
528         MonoImage *corlib;
529         MonoClass *object_class;
530         MonoClass *byte_class;
531         MonoClass *void_class;
532         MonoClass *boolean_class;
533         MonoClass *sbyte_class;
534         MonoClass *int16_class;
535         MonoClass *uint16_class;
536         MonoClass *int32_class;
537         MonoClass *uint32_class;
538         MonoClass *int_class;
539         MonoClass *uint_class;
540         MonoClass *int64_class;
541         MonoClass *uint64_class;
542         MonoClass *single_class;
543         MonoClass *double_class;
544         MonoClass *char_class;
545         MonoClass *string_class;
546         MonoClass *enum_class;
547         MonoClass *array_class;
548         MonoClass *delegate_class;
549         MonoClass *multicastdelegate_class;
550         MonoClass *asyncresult_class;
551         MonoClass *waithandle_class;
552         MonoClass *typehandle_class;
553         MonoClass *fieldhandle_class;
554         MonoClass *methodhandle_class;
555         MonoClass *monotype_class;
556         MonoClass *exception_class;
557         MonoClass *threadabortexception_class;
558         MonoClass *thread_class;
559         MonoClass *transparent_proxy_class;
560         MonoClass *real_proxy_class;
561         MonoClass *mono_method_message_class;
562         MonoClass *appdomain_class;
563         MonoClass *field_info_class;
564         MonoClass *method_info_class;
565         MonoClass *stringbuilder_class;
566         MonoClass *math_class;
567         MonoClass *stack_frame_class;
568         MonoClass *stack_trace_class;
569         MonoClass *marshal_class;
570         MonoClass *iserializeable_class;
571         MonoClass *serializationinfo_class;
572         MonoClass *streamingcontext_class;
573         MonoClass *typed_reference_class;
574         MonoClass *argumenthandle_class;
575         MonoClass *marshalbyrefobject_class;
576         MonoClass *monitor_class;
577         MonoClass *iremotingtypeinfo_class;
578         MonoClass *runtimesecurityframe_class;
579         MonoClass *executioncontext_class;
580         MonoClass *generic_array_class;
581 } MonoDefaults;
582
583 extern MonoDefaults mono_defaults;
584
585 void
586 mono_loader_init           (void);
587
588 void
589 mono_loader_lock           (void);
590
591 void
592 mono_loader_unlock         (void);
593
594 void 
595 mono_icall_init            (void);
596
597 void
598 mono_icall_cleanup         (void);
599
600 gpointer
601 mono_method_get_wrapper_data (MonoMethod *method, guint32 id);
602
603 void
604 mono_install_stack_walk (MonoStackWalkImpl func);
605
606 gboolean
607 mono_metadata_has_generic_params (MonoImage *image, guint32 token);
608
609 MonoGenericContainer *mono_metadata_load_generic_params (MonoImage *image, guint32 token,
610                                                          MonoGenericContainer *parent_container);
611
612 MonoMethodSignature*
613 mono_create_icall_signature (const char *sigstr);
614
615 MonoJitICallInfo *
616 mono_register_jit_icall (gconstpointer func, const char *name, MonoMethodSignature *sig, gboolean is_save);
617
618 void
619 mono_register_jit_icall_wrapper (MonoJitICallInfo *info, gconstpointer wrapper);
620
621 MonoJitICallInfo *
622 mono_find_jit_icall_by_name (const char *name);
623
624 MonoJitICallInfo *
625 mono_find_jit_icall_by_addr (gconstpointer addr);
626
627 MonoMethodSignature*
628 mono_class_inflate_generic_signature (MonoImage *image, MonoMethodSignature *sig, MonoGenericContext *context);
629
630 MonoMethodSignature *
631 mono_method_signature_full (MonoMethod *image, MonoGenericContext *context);
632
633 MonoGenericClass *
634 mono_get_shared_generic_class (MonoGenericContainer *container, gboolean is_dynamic);
635
636 gboolean
637 mono_class_set_failure (MonoClass *klass, guint32 ex_type, void *ex_data);
638
639 MonoException*
640 mono_class_get_exception_for_failure (MonoClass *klass);
641
642 char*
643 mono_type_get_name_full (MonoType *type, MonoTypeNameFormat format);
644
645 #endif /* __MONO_METADATA_CLASS_INTERBALS_H__ */
646