Workaround from Dick to the string.Replace bug
[mono.git] / mono / metadata / class.h
1 #ifndef _MONO_CLI_CLASS_H_
2 #define _MONO_CLI_CLASS_H_
3
4 #include <mono/metadata/metadata.h>
5 #include <mono/metadata/image.h>
6 #include <mono/metadata/loader.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 typedef struct MonoVTable MonoVTable;
14
15 typedef struct {
16         MonoTypeEnum type;
17         gpointer value;
18 } MonoConstant;
19
20 /*
21  * MonoClassField is just a runtime representation of the metadata for
22  * field, it doesn't contain the data directly.  Static fields are
23  * stored in MonoVTable->data.  Instance fields are allocated in the
24  * objects after the object header.
25  */
26 typedef struct {
27         /* Type of the field */
28         MonoType        *type;
29
30         /*
31          * Offset where this field is stored; if it is an instance
32          * field, it's the offset from the start of the object, if
33          * it's static, it's from the start of the memory chunk
34          * allocated for statics for the class.
35          */
36         int              offset;
37
38         const char      *name;
39
40         /*
41          * Pointer to the data (from the RVA address, valid only for
42          * fields with the has rva flag).
43          */
44         const char      *data;
45
46         /* Type where the field was defined */
47         MonoClass       *parent;
48
49         /*
50          * If the field is constant, pointer to the metadata where the
51          * constant value can be loaded. Initialized lazily during vtable creation.
52          */
53         MonoConstant    *def_value;
54 } MonoClassField;
55
56 /* a field is ignored if it's named "_Deleted" and it has the specialname and rtspecialname flags set */
57 #define mono_field_is_deleted(field) ((field)->name[0] == '_' && ((field)->type->attrs & 0x600) && (strcmp ((field)->name, "_Deleted") == 0))
58
59 typedef struct {
60         MonoClassField *field;
61         guint32 offset;
62         MonoMarshalSpec *mspec;
63 } MonoMarshalField;
64
65 typedef struct {
66         guint32 native_size;
67         guint32 num_fields;
68         MonoMarshalField fields [MONO_ZERO_LEN_ARRAY];
69 } MonoMarshalType;
70
71 typedef struct {
72         const char *name;
73         MonoMethod *get;
74         MonoMethod *set;
75         guint32 attrs;
76 } MonoProperty;
77
78 typedef struct {
79         const char *name;
80         MonoMethod *add;
81         MonoMethod *remove;
82         MonoMethod *raise;
83         MonoMethod **other;
84         guint32 attrs;
85 } MonoEvent;
86
87 struct _MonoClass {
88         MonoImage *image;
89
90         /* The underlying type of the enum */
91         MonoType *enum_basetype;
92         /* element class for arrays and enum */
93         MonoClass *element_class; 
94         /* used for subtype checks */
95         MonoClass *cast_class; 
96         /* array dimension */
97         guint32    rank;          
98
99         guint inited          : 1;
100         /* We use init_pending to detect cyclic calls to mono_class_init */
101         guint init_pending    : 1;
102
103         /* A class contains static and non static data. Static data can be
104          * of the same type as the class itselfs, but it does not influence
105          * the instance size of the class. To avoid cyclic calls to 
106          * mono_class_init (from mono_class_instance_size ()) we first 
107          * initialise all non static fields. After that we set size_inited 
108          * to 1, because we know the instance size now. After that we 
109          * initialise all static fields.
110          */
111         guint size_inited     : 1;
112         guint valuetype       : 1; /* derives from System.ValueType */
113         guint enumtype        : 1; /* derives from System.Enum */
114         guint blittable       : 1; /* class is blittable */
115         guint unicode         : 1; /* class uses unicode char when marshalled */
116         guint wastypebuilder  : 1; /* class was created at runtime from a TypeBuilder */
117         /* next byte */
118         guint min_align       : 4;
119         guint packing_size    : 4;
120         /* next byte */
121         guint ghcimpl         : 1; /* class has its own GetHashCode impl */ 
122         guint has_finalize    : 1; /* class has its own Finalize impl */ 
123         guint marshalbyref    : 1; /* class is a MarshalByRefObject */
124         guint contextbound    : 1; /* class is a ContextBoundObject */
125         guint delegate        : 1; /* class is a Delegate */
126         guint gc_descr_inited : 1; /* gc_descr is initialized */
127         guint dummy           : 1; /* temporary hack */
128
129         MonoClass  *parent;
130         MonoClass  *nested_in;
131         GList      *nested_classes;
132
133         guint32    type_token;
134         const char *name;
135         const char *name_space;
136         
137         guint       interface_count;
138         guint       interface_id;        /* unique inderface id (for interfaces) */
139         guint       max_interface_id;
140         gint       *interface_offsets;   
141         MonoClass **interfaces;
142
143         /* for fast subtype checks */
144         guint       idepth;
145         MonoClass **supertypes;
146
147         /*
148          * Computed object instance size, total.
149          */
150         int        instance_size;
151         int        class_size;
152         int        vtable_size; /* number of slots */
153
154         /*
155          * From the TypeDef table
156          */
157         guint32    flags;
158         struct {
159                 guint32 first, last;
160                 int count;
161         } field, method, property, event;
162
163         /* loaded on demand */
164         MonoMarshalType *marshal_info;
165
166         /*
167          * Field information: Type and location from object base
168          */
169         MonoClassField *fields;
170
171         MonoProperty *properties;
172         
173         MonoEvent *events;
174
175         MonoMethod **methods;
176
177         /* used as the type of the this argument and when passing the arg by value */
178         MonoType this_arg;
179         MonoType byval_arg;
180
181         MonoType *generic_inst;
182         MonoGenericParam *gen_params;
183         guint16 num_gen_params;
184
185         void *reflection_info;
186
187         void *gc_descr;
188         guint64 gc_bitmap;
189
190         MonoMethod *ptr_to_str;
191         MonoMethod *str_to_ptr;
192
193         MonoVTable *cached_vtable;
194         MonoMethod **vtable;    
195 };
196
197 struct MonoVTable {
198         MonoClass  *klass;
199     /*
200          * According to comments in gc_gcj.h, this should be the second word in
201          * the vtable.
202          */
203         void *gc_descr;         
204         MonoDomain *domain;  /* each object/vtable belongs to exactly one domain */
205         guint       max_interface_id;
206         gpointer   *interface_offsets;   
207         gpointer    data; /* to store static class data */
208         gpointer    type; /* System.Type type for klass */
209         guint remote          : 1; /* class is remotely activated */
210         guint initialized     : 1; /* cctor has been run */
211         guint initializing    : 1; /* cctor is running */
212         /* do not add any fields after vtable, the structure is dynamically extended */
213         gpointer    vtable [MONO_ZERO_LEN_ARRAY];       
214 };
215
216 /*
217  * Generic instantiation data type encoding.
218  */
219 struct _MonoGenericInst {
220         MonoClass *klass;
221         MonoType *generic_type;
222         MonoMethod *generic_method;
223         int type_argc;
224         MonoType **type_argv;
225         int mtype_argc;
226         MonoType **mtype_argv;
227         guint is_open       : 1;
228         guint initialized   : 1;
229         guint init_pending  : 1;
230 };
231
232 struct _MonoGenericParam {
233         MonoClass *pklass;
234         MonoMethod *method;
235         const char *name;
236         guint16 flags;
237         guint16 num;
238         MonoClass** constraints; /* NULL means end of list */
239 };
240
241 #define mono_class_has_parent(klass,parent) ((klass)->idepth >= (parent)->idepth) && ((klass)->supertypes [(parent)->idepth - 1] == (parent))
242
243 typedef struct {
244         gulong new_object_count;
245         gulong initialized_class_count;
246         gulong used_class_count;
247         gulong class_vtable_size;
248         gulong class_static_data_size;
249 } MonoStats;
250
251 extern MonoStats mono_stats;
252
253 typedef gpointer (*MonoTrampoline)       (MonoMethod *method);
254
255 typedef gpointer (*MonoLookupDynamicToken) (MonoImage *image, guint32 token);
256
257 void
258 mono_classes_init (void);
259
260 MonoClass *
261 mono_class_get             (MonoImage *image, guint32 type_token);
262
263 void
264 mono_class_init            (MonoClass *klass);
265
266 void
267 mono_class_layout_fields   (MonoClass *klass);
268
269 void
270 mono_class_setup_vtable    (MonoClass *klass, MonoMethod **overrides, int onum);
271
272 MonoVTable *
273 mono_class_vtable          (MonoDomain *domain, MonoClass *klass);
274
275 MonoVTable *
276 mono_class_proxy_vtable    (MonoDomain *domain, MonoClass *klass);
277
278 void
279 mono_class_setup_mono_type (MonoClass *klass);
280
281 void
282 mono_class_setup_parent    (MonoClass *klass, MonoClass *parent);
283
284 void
285 mono_class_setup_supertypes (MonoClass *klass);
286
287 MonoClass *
288 mono_class_from_name       (MonoImage *image, const char* name_space, const char *name);
289
290 MonoClass *
291 mono_class_from_name_case  (MonoImage *image, const char* name_space, const char *name);
292
293 MonoClass * 
294 mono_class_from_typeref    (MonoImage *image, guint32 type_token);
295
296 gboolean
297 mono_class_is_open_constructed_type (MonoType *t);
298
299 MonoClass *
300 mono_class_from_generic_parameter (MonoGenericParam *param, MonoImage *image, gboolean is_mvar);
301
302 MonoClass*
303 mono_class_from_generic    (MonoType *gtype, gboolean inflate_methods);
304
305 MonoType*
306 mono_class_inflate_generic_type (MonoType *type, MonoGenericInst *ginst);
307
308 MonoMethodSignature*
309 mono_class_inflate_generic_signature (MonoImage *image, MonoMethodSignature *sig, MonoGenericInst *ginst);
310
311 MonoMethod*
312 mono_class_inflate_generic_method (MonoMethod *method, MonoGenericInst *ginst);
313
314 MonoClassField*
315 mono_field_from_memberref  (MonoImage *image, guint32 token, MonoClass **retklass);
316
317 MonoClassField*
318 mono_field_from_token      (MonoImage *image, guint32 token, MonoClass **retklass);
319
320 MonoMethod**
321 mono_class_get_overrides   (MonoImage *image, guint32 type_token, gint32 *num_overrides);
322
323 MonoClass *
324 mono_bounded_array_class_get (MonoClass *element_class, guint32 rank, gboolean bounded);
325
326 MonoClass *
327 mono_array_class_get       (MonoClass *element_class, guint32 rank);
328
329 MonoClass *
330 mono_ptr_class_get         (MonoType *type);
331
332 MonoClassField *
333 mono_class_get_field       (MonoClass *klass, guint32 field_token);
334
335 MonoClassField *
336 mono_class_get_field_from_name (MonoClass *klass, const char *name);
337
338 MonoProperty*
339 mono_class_get_property_from_name (MonoClass *klass, const char *name);
340
341 gint32
342 mono_array_element_size    (MonoClass *ac);
343
344 gint32
345 mono_class_instance_size   (MonoClass *klass);
346
347 gint32
348 mono_class_array_element_size (MonoClass *klass);
349
350 gint32
351 mono_class_data_size       (MonoClass *klass);
352
353 gint32
354 mono_class_value_size      (MonoClass *klass, guint32 *align);
355
356 gint32
357 mono_class_min_align       (MonoClass *klass);
358
359 MonoClass *
360 mono_class_from_mono_type  (MonoType *type);
361
362 gboolean
363 mono_class_is_subclass_of (MonoClass *klass, MonoClass *klassc, 
364                                                    gboolean check_interfaces);
365
366 gboolean
367 mono_class_is_assignable_from (MonoClass *klass, MonoClass *oklass);
368
369 gboolean
370 mono_class_needs_cctor_run (MonoClass *klass, MonoMethod *caller);
371
372 gpointer
373 mono_ldtoken               (MonoImage *image, guint32 token, MonoClass **retclass);
374
375 char*         
376 mono_type_get_name         (MonoType *type);
377
378 void
379 mono_install_trampoline (MonoTrampoline func);
380
381 void
382 mono_install_remoting_trampoline (MonoTrampoline func);
383
384 gpointer
385 mono_lookup_dynamic_token (MonoImage *image, guint32 token);
386
387 void
388 mono_install_lookup_dynamic_token (MonoLookupDynamicToken func);
389
390 void    
391 mono_install_get_config_dir(void);
392
393 #endif /* _MONO_CLI_CLASS_H_ */