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