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