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