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