* Makefile.am (opcode.def): Make $(srcdir)!=$(builddir) safe.
[mono.git] / mono / metadata / class.c
1 /*
2  * class.c: Class management for the Mono runtime
3  *
4  * Author:
5  *   Miguel de Icaza (miguel@ximian.com)
6  *
7  * (C) 2001 Ximian, Inc.
8  *
9  * Possible Optimizations:
10  *     in mono_class_create, do not allocate the class right away,
11  *     but wait until you know the size of the FieldMap, so that
12  *     the class embeds directly the FieldMap after the vtable.
13  *
14  * 
15  */
16 #include <config.h>
17 #include <glib.h>
18 #include <stdio.h>
19 #include <string.h>
20 #include <stdlib.h>
21 #include <signal.h>
22 #include <mono/metadata/image.h>
23 #include <mono/metadata/assembly.h>
24 #include <mono/metadata/cil-coff.h>
25 #include <mono/metadata/metadata.h>
26 #include <mono/metadata/metadata-internals.h>
27 #include <mono/metadata/tabledefs.h>
28 #include <mono/metadata/tokentype.h>
29 #include <mono/metadata/class-internals.h>
30 #include <mono/metadata/object.h>
31 #include <mono/metadata/appdomain.h>
32 #include <mono/metadata/mono-endian.h>
33 #include <mono/metadata/debug-helpers.h>
34 #include <mono/metadata/reflection.h>
35 #include <mono/os/gc_wrapper.h>
36
37 MonoStats mono_stats;
38
39 gboolean mono_print_vtable = FALSE;
40
41 static MonoClass * mono_class_create_from_typedef (MonoImage *image, guint32 type_token);
42
43 void (*mono_debugger_start_class_init_func) (MonoClass *klass) = NULL;
44 void (*mono_debugger_class_init_func) (MonoClass *klass) = NULL;
45
46 MonoClass *
47 mono_class_from_typeref (MonoImage *image, guint32 type_token)
48 {
49         guint32 cols [MONO_TYPEREF_SIZE];
50         MonoTableInfo  *t = &image->tables [MONO_TABLE_TYPEREF];
51         guint32 idx;
52         const char *name, *nspace;
53         MonoClass *res;
54         MonoAssembly **references;
55
56         mono_metadata_decode_row (t, (type_token&0xffffff)-1, cols, MONO_TYPEREF_SIZE);
57
58         name = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAME]);
59         nspace = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAMESPACE]);
60         
61         idx = cols [MONO_TYPEREF_SCOPE] >> MONO_RESOLTION_SCOPE_BITS;
62         switch (cols [MONO_TYPEREF_SCOPE] & MONO_RESOLTION_SCOPE_MASK) {
63         case MONO_RESOLTION_SCOPE_MODULE:
64                 if (!idx)
65                         g_error ("null ResolutionScope not yet handled");
66                 /* a typedef in disguise */
67                 return mono_class_from_name (image, nspace, name);
68         case MONO_RESOLTION_SCOPE_MODULEREF:
69                 return mono_class_from_name (image->modules [idx - 1], nspace, name);
70         case MONO_RESOLTION_SCOPE_TYPEREF: {
71                 MonoClass *enclosing = mono_class_from_typeref (image, MONO_TOKEN_TYPE_REF | idx);
72                 GList *tmp;
73                 mono_class_init (enclosing);
74                 for (tmp = enclosing->nested_classes; tmp; tmp = tmp->next) {
75                         res = tmp->data;
76                         if (strcmp (res->name, name) == 0)
77                                 return res;
78                 }
79                 g_warning ("TypeRef ResolutionScope not yet handled (%d)", idx);
80                 return NULL;
81         }
82         case MONO_RESOLTION_SCOPE_ASSEMBLYREF:
83                 break;
84         }
85
86         references = image->references;
87         if (!references ||  !references [idx-1]) {
88                 /* 
89                  * detected a reference to mscorlib, we simply return a reference to a dummy 
90                  * until we have a better solution.
91                  * 
92                  * once a better solution is in place, the System.MonoDummy
93                  * class should be removed from CVS.
94                  */
95                 fprintf(stderr, "Sending dummy where %s.%s expected\n", mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAMESPACE]), mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAME])); 
96                 
97                 res = mono_class_from_name (image, "System", "MonoDummy");
98                 /* prevent method loading */
99                 res->dummy = 1;
100                 /* some storage if the type is used  - very ugly hack */
101                 res->instance_size = 2*sizeof (gpointer);
102                 return res;
103         }       
104
105         /* load referenced assembly */
106         image = references [idx-1]->image;
107
108         return mono_class_from_name (image, nspace, name);
109 }
110
111 static MonoType*
112 dup_type (MonoType* t, const MonoType *original)
113 {
114         MonoType *r = g_new0 (MonoType, 1);
115         *r = *t;
116         r->attrs = original->attrs;
117         mono_stats.generics_metadata_size += sizeof (MonoType);
118         return r;
119 }
120
121 static void
122 mono_type_get_name_recurse (MonoType *type, GString *str, gboolean is_recursed)
123 {
124         MonoClass *klass;
125         
126         switch (type->type) {
127         case MONO_TYPE_ARRAY: {
128                 int i, rank = type->data.array->rank;
129
130                 mono_type_get_name_recurse (&type->data.array->eklass->byval_arg, str, FALSE);
131                 g_string_append_c (str, '[');
132                 for (i = 1; i < rank; i++)
133                         g_string_append_c (str, ',');
134                 g_string_append_c (str, ']');
135                 break;
136         }
137         case MONO_TYPE_SZARRAY:
138                 mono_type_get_name_recurse (&type->data.klass->byval_arg, str, FALSE);
139                 g_string_append (str, "[]");
140                 break;
141         case MONO_TYPE_PTR:
142                 mono_type_get_name_recurse (type->data.type, str, FALSE);
143                 g_string_append_c (str, '*');
144                 break;
145         default:
146                 klass = mono_class_from_mono_type (type);
147                 if (klass->nested_in) {
148                         mono_type_get_name_recurse (&klass->nested_in->byval_arg, str, TRUE);
149                         g_string_append_c (str, '+');
150                 }
151                 if (*klass->name_space) {
152                         g_string_append (str, klass->name_space);
153                         g_string_append_c (str, '.');
154                 }
155                 g_string_append (str, klass->name);
156                 if (is_recursed)
157                         break;
158                 if (klass->generic_inst) {
159                         MonoGenericInst *ginst = klass->generic_inst;
160                         int i;
161
162                         g_string_append_c (str, '[');
163                         for (i = 0; i < ginst->type_argc; i++) {
164                                 if (i)
165                                         g_string_append_c (str, ',');
166                                 mono_type_get_name_recurse (ginst->type_argv [i], str, FALSE);
167                         }
168                         g_string_append_c (str, ']');
169                 } else if (klass->gen_params) {
170                         int i;
171
172                         g_string_append_c (str, '[');
173                         for (i = 0; i < klass->num_gen_params; i++) {
174                                 if (i)
175                                         g_string_append_c (str, ',');
176                                 g_string_append (str, klass->gen_params [i].name);
177                         }
178                         g_string_append_c (str, ']');
179                 }
180                 break;
181         }
182 }
183
184 /*
185  * mono_type_get_name:
186  * @type: a type
187  *
188  * Returns the string representation for type as required by System.Reflection.
189  * The inverse of mono_reflection_parse_type ().
190  */
191 char*
192 mono_type_get_name (MonoType *type)
193 {
194         GString* result = g_string_new ("");
195         mono_type_get_name_recurse (type, result, FALSE);
196
197         if (type->byref)
198                 g_string_append_c (result, '&');
199
200         return g_string_free (result, FALSE);
201 }
202
203 gboolean
204 mono_class_is_open_constructed_type (MonoType *t)
205 {
206         switch (t->type) {
207         case MONO_TYPE_VAR:
208         case MONO_TYPE_MVAR:
209                 return TRUE;
210         case MONO_TYPE_SZARRAY:
211                 return mono_class_is_open_constructed_type (&t->data.klass->byval_arg);
212         case MONO_TYPE_ARRAY:
213                 return mono_class_is_open_constructed_type (&t->data.array->eklass->byval_arg);
214         case MONO_TYPE_PTR:
215                 return mono_class_is_open_constructed_type (t->data.type);
216         case MONO_TYPE_GENERICINST: {
217                 MonoGenericInst *ginst = t->data.generic_inst;
218                 int i;
219
220                 if (mono_class_is_open_constructed_type (ginst->generic_type))
221                         return TRUE;
222                 for (i = 0; i < ginst->type_argc; i++)
223                         if (mono_class_is_open_constructed_type (ginst->type_argv [i]))
224                                 return TRUE;
225                 return FALSE;
226         }
227         default:
228                 return FALSE;
229         }
230 }
231
232 static MonoType*
233 inflate_generic_type (MonoType *type, MonoGenericContext *context)
234 {
235         switch (type->type) {
236         case MONO_TYPE_MVAR:
237                 if (context->gmethod && context->gmethod->mtype_argv)
238                         return dup_type (
239                                 context->gmethod->mtype_argv [type->data.generic_param->num],
240                                 type);
241                 else
242                         return NULL;
243         case MONO_TYPE_VAR:
244                 if (context->ginst)
245                         return dup_type (
246                                 context->ginst->type_argv [type->data.generic_param->num],
247                                 type);
248                 else
249                         return NULL;
250         case MONO_TYPE_SZARRAY: {
251                 MonoClass *eclass = type->data.klass;
252                 MonoType *nt, *inflated = inflate_generic_type (&eclass->byval_arg, context);
253                 if (!inflated)
254                         return NULL;
255                 nt = dup_type (type, type);
256                 nt->data.klass = mono_class_from_mono_type (inflated);
257                 return nt;
258         }
259         case MONO_TYPE_GENERICINST: {
260                 MonoGenericInst *oginst = type->data.generic_inst;
261                 MonoGenericInst *nginst;
262                 MonoType *nt;
263                 int i;
264
265                 nginst = g_new0 (MonoGenericInst, 1);
266                 *nginst = *oginst;
267
268                 nginst->is_open = FALSE;
269
270                 nginst->type_argv = g_new0 (MonoType *, oginst->type_argc);
271
272                 for (i = 0; i < oginst->type_argc; i++) {
273                         MonoType *t = oginst->type_argv [i];
274                         nginst->type_argv [i] = mono_class_inflate_generic_type (t, context);
275
276                         if (!nginst->is_open)
277                                 nginst->is_open = mono_class_is_open_constructed_type (nginst->type_argv [i]);
278                 };
279
280                 nginst->klass = NULL;
281
282                 nginst->context = g_new0 (MonoGenericContext, 1);
283                 nginst->context->ginst = nginst;
284
285                 mono_loader_lock ();
286                 nt = g_hash_table_lookup (oginst->klass->image->generic_inst_cache, nginst);
287
288                 if (nt) {
289                         g_free (nginst->type_argv);
290                         g_free (nginst);
291                         mono_loader_unlock ();
292                         return nt;
293                 }
294
295                 nginst->dynamic_info = NULL;
296                 nginst->initialized = FALSE;
297
298                 mono_class_create_generic (nginst);
299
300                 mono_stats.generic_instance_count++;
301                 mono_stats.generics_metadata_size += sizeof (MonoGenericInst) +
302                         sizeof (MonoGenericContext) +
303                         nginst->type_argc * sizeof (MonoType);
304
305                 nt = dup_type (type, type);
306                 nt->data.generic_inst = nginst;
307                 g_hash_table_insert (oginst->klass->image->generic_inst_cache, nginst, nt);
308                 mono_loader_unlock ();
309                 return nt;
310         }
311         default:
312                 return NULL;
313         }
314         return NULL;
315 }
316
317 MonoType*
318 mono_class_inflate_generic_type (MonoType *type, MonoGenericContext *context)
319 {
320         MonoType *inflated = inflate_generic_type (type, context);
321
322         if (!inflated)
323                 return type;
324
325         mono_stats.inflated_type_count++;
326         return inflated;
327 }
328
329 static MonoMethodSignature*
330 inflate_generic_signature (MonoImage *image, MonoMethodSignature *sig,
331                            MonoGenericContext *context)
332 {
333         MonoMethodSignature *res;
334         int i;
335         res = mono_metadata_signature_alloc (image, sig->param_count);
336         res->ret = mono_class_inflate_generic_type (sig->ret, context);
337         for (i = 0; i < sig->param_count; ++i)
338                 res->params [i] = mono_class_inflate_generic_type (sig->params [i], context);
339         res->hasthis = sig->hasthis;
340         res->explicit_this = sig->explicit_this;
341         res->call_convention = sig->call_convention;
342         res->generic_param_count = sig->generic_param_count;
343         res->is_inflated = 1;
344         return res;
345 }
346
347 static MonoMethodHeader*
348 inflate_generic_header (MonoMethodHeader *header, MonoGenericContext *context)
349 {
350         MonoMethodHeader *res;
351         int i;
352         res = g_malloc0 (sizeof (MonoMethodHeader) + sizeof (gpointer) * header->num_locals);
353         res->code = header->code;
354         res->code_size = header->code_size;
355         res->max_stack = header->max_stack;
356         res->num_clauses = header->num_clauses;
357         res->init_locals = header->init_locals;
358         res->num_locals = header->num_locals;
359         res->clauses = header->clauses;
360         res->gen_params = header->gen_params;
361         for (i = 0; i < header->num_locals; ++i)
362                 res->locals [i] = mono_class_inflate_generic_type (header->locals [i], context);
363         return res;
364 }
365
366 MonoMethod*
367 mono_class_inflate_generic_method (MonoMethod *method, MonoGenericContext *context,
368                                    MonoClass *klass)
369 {
370         MonoMethodInflated *result;
371
372         if ((method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
373             (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL))
374                 return method;
375
376         mono_stats.inflated_method_count++;
377         mono_stats.generics_metadata_size +=
378                 sizeof (MonoMethodInflated) - sizeof (MonoMethodNormal);
379
380         result = g_new0 (MonoMethodInflated, 1);
381         result->nmethod = *(MonoMethodNormal*)method;
382
383         if (result->nmethod.header)
384                 result->nmethod.header = inflate_generic_header (
385                         result->nmethod.header, context);
386
387         if (klass)
388                 result->nmethod.method.klass = klass;
389         else {
390                 MonoType *declaring = mono_class_inflate_generic_type (
391                         &method->klass->byval_arg, context);
392                 result->nmethod.method.klass = mono_class_from_mono_type (declaring);
393         }
394
395         result->nmethod.method.signature = inflate_generic_signature (
396                 method->klass->image, method->signature, context);
397
398         if (context->gmethod) {
399                 result->context = g_new0 (MonoGenericContext, 1);
400                 result->context->gmethod = context->gmethod;
401                 result->context->ginst = result->nmethod.method.klass->generic_inst;
402
403                 mono_stats.generics_metadata_size += sizeof (MonoGenericContext);
404         } else
405                 result->context = result->nmethod.method.klass->generic_inst->context;
406
407         if (method->signature->is_inflated)
408                 result->declaring = ((MonoMethodInflated *) method)->declaring;
409         else
410                 result->declaring = method;
411
412         return (MonoMethod *) result;
413 }
414
415 /** 
416  * class_compute_field_layout:
417  * @m: pointer to the metadata.
418  * @class: The class to initialize
419  *
420  * Initializes the class->fields.
421  *
422  * Currently we only support AUTO_LAYOUT, and do not even try to do
423  * a good job at it.  This is temporary to get the code for Paolo.
424  */
425 static void
426 class_compute_field_layout (MonoClass *class)
427 {
428         MonoImage *m = class->image; 
429         const int top = class->field.count;
430         guint32 layout = class->flags & TYPE_ATTRIBUTE_LAYOUT_MASK;
431         MonoTableInfo *t = &m->tables [MONO_TABLE_FIELD];
432         int i, blittable = TRUE, real_size = 0;
433         guint32 rva;
434         guint32 packing_size = 0;
435         gboolean explicit_size;
436         MonoClassField *field;
437
438         if (class->size_inited)
439                 return;
440
441         if (class->parent) {
442                 if (!class->parent->size_inited)
443                         class_compute_field_layout (class->parent);
444                 class->instance_size += class->parent->instance_size;
445                 class->min_align = class->parent->min_align;
446                 blittable = class->parent->blittable;
447         } else {
448                 class->instance_size = sizeof (MonoObject);
449                 class->min_align = 1;
450         }
451
452         /* Get the real size */
453         explicit_size = mono_metadata_packing_from_typedef (class->image, class->type_token, &packing_size, &real_size);
454
455         if (explicit_size) {
456                 g_assert ((packing_size & 0xfffffff0) == 0);
457                 class->packing_size = packing_size;
458                 real_size += class->instance_size;
459         }
460
461         if (!top) {
462                 if (explicit_size && real_size) {
463                         class->instance_size = MAX (real_size, class->instance_size);
464                 }
465                 class->size_inited = 1;
466                 return;
467         }
468
469         class->fields = g_new0 (MonoClassField, top);
470
471         /*
472          * Fetch all the field information.
473          */
474         for (i = 0; i < top; i++){
475                 const char *sig;
476                 guint32 cols [MONO_FIELD_SIZE];
477                 int idx = class->field.first + i;
478
479                 field = &class->fields [i];
480                 mono_metadata_decode_row (t, idx, cols, MONO_FIELD_SIZE);
481                 /* The name is needed for fieldrefs */
482                 field->name = mono_metadata_string_heap (m, cols [MONO_FIELD_NAME]);
483                 sig = mono_metadata_blob_heap (m, cols [MONO_FIELD_SIGNATURE]);
484                 mono_metadata_decode_value (sig, &sig);
485                 /* FIELD signature == 0x06 */
486                 g_assert (*sig == 0x06);
487                 field->type = mono_metadata_parse_field_type (
488                         m, cols [MONO_FIELD_FLAGS], sig + 1, &sig);
489                 if (mono_field_is_deleted (field))
490                         continue;
491                 if (class->generic_inst) {
492                         field->type = mono_class_inflate_generic_type (
493                                 field->type, class->generic_inst->context);
494                         field->type->attrs = cols [MONO_FIELD_FLAGS];
495                 }
496
497                 field->parent = class;
498
499                 /* Only do these checks if we still think this type is blittable */
500                 if (blittable && !(field->type->attrs & FIELD_ATTRIBUTE_STATIC)) {
501                         if (field->type->byref) {
502                                 blittable = FALSE;
503                         } else {
504                                 MonoClass *field_class = mono_class_from_mono_type (field->type);
505                                 if (!field_class || !field_class->blittable)
506                                         blittable = FALSE;
507                         }
508                 }
509
510                 if (layout == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT) {
511                         mono_metadata_field_info (m, idx, &field->offset, NULL, NULL);
512                         if (field->offset == (guint32)-1 && !(field->type->attrs & FIELD_ATTRIBUTE_STATIC))
513                                 g_warning ("%s not initialized correctly (missing field layout info for %s)", class->name, field->name);
514                 }
515
516                 if (cols [MONO_FIELD_FLAGS] & FIELD_ATTRIBUTE_HAS_FIELD_RVA) {
517                         mono_metadata_field_info (m, idx, NULL, &rva, NULL);
518                         if (!rva)
519                                 g_warning ("field %s in %s should have RVA data, but hasn't", field->name, class->name);
520                         field->data = mono_image_rva_map (class->image, rva);
521                 }
522
523                 if (class->enumtype && !(cols [MONO_FIELD_FLAGS] & FIELD_ATTRIBUTE_STATIC)) {
524                         class->enum_basetype = field->type;
525                         class->cast_class = class->element_class = mono_class_from_mono_type (class->enum_basetype);
526                         blittable = class->element_class->blittable;
527                 }
528
529                 /* The def_value of fields is compute lazily during vtable creation */
530         }
531
532         if (class == mono_defaults.string_class)
533                 blittable = FALSE;
534
535         class->blittable = blittable;
536
537         if (class->enumtype && !class->enum_basetype) {
538                 if (!((strcmp (class->name, "Enum") == 0) && (strcmp (class->name_space, "System") == 0)))
539                         G_BREAKPOINT ();
540         }
541         if (explicit_size && real_size) {
542                 class->instance_size = MAX (real_size, class->instance_size);
543         }
544
545         if (class->gen_params)
546                 return;
547
548         mono_class_layout_fields (class);
549 }
550
551 void
552 mono_class_layout_fields (MonoClass *class)
553 {
554         int i;
555         const int top = class->field.count;
556         guint32 layout = class->flags & TYPE_ATTRIBUTE_LAYOUT_MASK;
557         guint32 pass, passes, real_size;
558         gboolean gc_aware_layout = FALSE;
559         MonoClassField *field;
560
561         /*
562          * Enable GC aware auto layout: in this mode, reference
563          * fields are grouped together inside objects, increasing collector 
564          * performance.
565          * Requires that all classes whose layout is known to native code be annotated
566          * with [StructLayout (LayoutKind.Sequential)]
567          * Value types have gc_aware_layout disabled by default, as per
568          * what the default is for other runtimes.
569          */
570          /* corlib is missing [StructLayout] directives in many places */
571         if (layout == TYPE_ATTRIBUTE_AUTO_LAYOUT) {
572                 if (class->image != mono_defaults.corlib &&
573                         class->byval_arg.type != MONO_TYPE_VALUETYPE)
574                         gc_aware_layout = TRUE;
575         }
576
577         /*
578          * Compute field layout and total size (not considering static fields)
579          */
580
581         switch (layout) {
582         case TYPE_ATTRIBUTE_AUTO_LAYOUT:
583         case TYPE_ATTRIBUTE_SEQUENTIAL_LAYOUT:
584
585                 if (gc_aware_layout)
586                         passes = 2;
587                 else
588                         passes = 1;
589
590                 if (layout != TYPE_ATTRIBUTE_AUTO_LAYOUT)
591                         passes = 1;
592
593                 if (class->parent)
594                         real_size = class->parent->instance_size;
595                 else
596                         real_size = sizeof (MonoObject);
597
598                 for (pass = 0; pass < passes; ++pass) {
599                         for (i = 0; i < top; i++){
600                                 int size, align;
601                                 field = &class->fields [i];
602
603                                 if (mono_field_is_deleted (field))
604                                         continue;
605                                 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
606                                         continue;
607
608                                 if (gc_aware_layout) {
609                                         /* 
610                                          * We process fields with reference type in the first pass,
611                                          * and fields with non-reference type in the second pass.
612                                          * We use IS_POINTER instead of IS_REFERENCE because in
613                                          * some internal structures, we store GC_MALLOCed memory
614                                          * in IntPtr fields...
615                                          */
616                                         if (MONO_TYPE_IS_POINTER (field->type)) {
617                                                 if (pass == 1)
618                                                         continue;
619                                         } else {
620                                                 if (pass == 0)
621                                                         continue;
622                                         }
623                                 }
624
625                                 if ((top == 1) && (class->instance_size == sizeof (MonoObject)) &&
626                                         (strcmp (field->name, "$PRIVATE$") == 0)) {
627                                         /* This field is a hack inserted by MCS to empty structures */
628                                         continue;
629                                 }
630
631                                 size = mono_type_size (field->type, &align);
632                         
633                                 /* FIXME (LAMESPEC): should we also change the min alignment according to pack? */
634                                 align = class->packing_size ? MIN (class->packing_size, align): align;
635                                 class->min_align = MAX (align, class->min_align);
636                                 field->offset = real_size;
637                                 field->offset += align - 1;
638                                 field->offset &= ~(align - 1);
639                                 real_size = field->offset + size;
640                         }
641
642                         class->instance_size = MAX (real_size, class->instance_size);
643        
644                         if (class->instance_size & (class->min_align - 1)) {
645                                 class->instance_size += class->min_align - 1;
646                                 class->instance_size &= ~(class->min_align - 1);
647                         }
648                 }
649                 break;
650         case TYPE_ATTRIBUTE_EXPLICIT_LAYOUT:
651                 real_size = 0;
652                 for (i = 0; i < top; i++) {
653                         int size, align;
654                         field = &class->fields [i];
655
656                         /*
657                          * There must be info about all the fields in a type if it
658                          * uses explicit layout.
659                          */
660
661                         if (mono_field_is_deleted (field))
662                                 continue;
663                         if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
664                                 continue;
665
666                         size = mono_type_size (field->type, &align);
667                         
668                         /*
669                          * When we get here, field->offset is already set by the
670                          * loader (for either runtime fields or fields loaded from metadata).
671                          * The offset is from the start of the object: this works for both
672                          * classes and valuetypes.
673                          */
674                         field->offset += sizeof (MonoObject);
675
676                         /*
677                          * Calc max size.
678                          */
679                         real_size = MAX (real_size, size + field->offset);
680                 }
681                 class->instance_size = MAX (real_size, class->instance_size);
682                 break;
683         }
684
685         class->size_inited = 1;
686
687         /*
688          * Compute static field layout and size
689          */
690         for (i = 0; i < top; i++){
691                 int size, align;
692                 field = &class->fields [i];
693                         
694                 if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC) || field->type->attrs & FIELD_ATTRIBUTE_LITERAL)
695                         continue;
696                 if (mono_field_is_deleted (field))
697                         continue;
698                         
699                 size = mono_type_size (field->type, &align);
700                 field->offset = class->class_size;
701                 field->offset += align - 1;
702                 field->offset &= ~(align - 1);
703                 class->class_size = field->offset + size;
704         }
705 }
706
707 static void
708 init_properties (MonoClass *class)
709 {
710         guint startm, endm, i, j;
711         guint32 cols [MONO_PROPERTY_SIZE];
712         MonoTableInfo *pt = &class->image->tables [MONO_TABLE_PROPERTY];
713         MonoTableInfo *msemt = &class->image->tables [MONO_TABLE_METHODSEMANTICS];
714
715         class->property.first = mono_metadata_properties_from_typedef (class->image, mono_metadata_token_index (class->type_token) - 1, &class->property.last);
716         class->property.count = class->property.last - class->property.first;
717
718         class->properties = g_new0 (MonoProperty, class->property.count);
719         for (i = class->property.first; i < class->property.last; ++i) {
720                 mono_metadata_decode_row (pt, i, cols, MONO_PROPERTY_SIZE);
721                 class->properties [i - class->property.first].parent = class;
722                 class->properties [i - class->property.first].attrs = cols [MONO_PROPERTY_FLAGS];
723                 class->properties [i - class->property.first].name = mono_metadata_string_heap (class->image, cols [MONO_PROPERTY_NAME]);
724
725                 startm = mono_metadata_methods_from_property (class->image, i, &endm);
726                 for (j = startm; j < endm; ++j) {
727                         mono_metadata_decode_row (msemt, j, cols, MONO_METHOD_SEMA_SIZE);
728                         switch (cols [MONO_METHOD_SEMA_SEMANTICS]) {
729                         case METHOD_SEMANTIC_SETTER:
730                                 class->properties [i - class->property.first].set = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
731                                 break;
732                         case METHOD_SEMANTIC_GETTER:
733                                 class->properties [i - class->property.first].get = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
734                                 break;
735                         default:
736                                 break;
737                         }
738                 }
739         }
740 }
741
742 static void
743 init_events (MonoClass *class)
744 {
745         guint startm, endm, i, j;
746         guint32 cols [MONO_EVENT_SIZE];
747         MonoTableInfo *pt = &class->image->tables [MONO_TABLE_EVENT];
748         MonoTableInfo *msemt = &class->image->tables [MONO_TABLE_METHODSEMANTICS];
749
750         class->event.first = mono_metadata_events_from_typedef (class->image, mono_metadata_token_index (class->type_token) - 1, &class->event.last);
751         class->event.count = class->event.last - class->event.first;
752
753         class->events = g_new0 (MonoEvent, class->event.count);
754         for (i = class->event.first; i < class->event.last; ++i) {
755                 mono_metadata_decode_row (pt, i, cols, MONO_EVENT_SIZE);
756                 class->events [i - class->event.first].parent = class;
757                 class->events [i - class->event.first].attrs = cols [MONO_EVENT_FLAGS];
758                 class->events [i - class->event.first].name = mono_metadata_string_heap (class->image, cols [MONO_EVENT_NAME]);
759
760                 startm = mono_metadata_methods_from_event (class->image, i, &endm);
761                 for (j = startm; j < endm; ++j) {
762                         mono_metadata_decode_row (msemt, j, cols, MONO_METHOD_SEMA_SIZE);
763                         switch (cols [MONO_METHOD_SEMA_SEMANTICS]) {
764                         case METHOD_SEMANTIC_ADD_ON:
765                                 class->events [i - class->event.first].add = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
766                                 break;
767                         case METHOD_SEMANTIC_REMOVE_ON:
768                                 class->events [i - class->event.first].remove = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
769                                 break;
770                         case METHOD_SEMANTIC_FIRE:
771                                 class->events [i - class->event.first].raise = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
772                                 break;
773                         case METHOD_SEMANTIC_OTHER: /* don't care for now */
774                         default:
775                                 break;
776                         }
777                 }
778         }
779 }
780
781 static guint
782 mono_get_unique_iid (MonoClass *class)
783 {
784         static GHashTable *iid_hash = NULL;
785         static guint iid = 0;
786
787         char *str;
788         gpointer value;
789         
790         g_assert (MONO_CLASS_IS_INTERFACE (class));
791
792         mono_loader_lock ();
793
794         if (!iid_hash)
795                 iid_hash = g_hash_table_new (g_str_hash, g_str_equal);
796
797         str = g_strdup_printf ("%s|%s.%s\n", class->image->name, class->name_space, class->name);
798
799         if (g_hash_table_lookup_extended (iid_hash, str, NULL, &value)) {
800                 mono_loader_unlock ();
801                 g_free (str);
802                 return (guint)value;
803         } else {
804                 g_hash_table_insert (iid_hash, str, (gpointer)iid);
805                 ++iid;
806         }
807
808         mono_loader_unlock ();
809
810         return iid - 1;
811 }
812
813 static void
814 collect_implemented_interfaces_aux (MonoClass *klass, GPtrArray **res)
815 {
816         int i;
817         MonoClass *ic;
818         
819         for (i = 0; i < klass->interface_count; i++) {
820                 ic = klass->interfaces [i];
821
822                 if (*res == NULL)
823                         *res = g_ptr_array_new ();
824                 g_ptr_array_add (*res, ic);
825
826                 collect_implemented_interfaces_aux (ic, res);
827         }
828 }
829
830 static inline GPtrArray*
831 collect_implemented_interfaces (MonoClass *klass)
832 {
833         GPtrArray *res = NULL;
834
835         collect_implemented_interfaces_aux (klass, &res);
836         return res;
837 }
838
839 static int
840 setup_interface_offsets (MonoClass *class, int cur_slot)
841 {
842         MonoClass *k, *ic;
843         int i, max_iid;
844         GPtrArray *ifaces;
845
846         /* compute maximum number of slots and maximum interface id */
847         max_iid = 0;
848         for (k = class; k ; k = k->parent) {
849                 for (i = 0; i < k->interface_count; i++) {
850                         ic = k->interfaces [i];
851
852                         if (!ic->inited)
853                                 mono_class_init (ic);
854
855                         if (max_iid < ic->interface_id)
856                                 max_iid = ic->interface_id;
857                 }
858         }
859
860         if (MONO_CLASS_IS_INTERFACE (class)) {
861                 if (max_iid < class->interface_id)
862                         max_iid = class->interface_id;
863         }
864         class->max_interface_id = max_iid;
865         /* compute vtable offset for interfaces */
866         class->interface_offsets = g_malloc (sizeof (gpointer) * (max_iid + 1));
867
868         for (i = 0; i <= max_iid; i++)
869                 class->interface_offsets [i] = -1;
870
871         ifaces = collect_implemented_interfaces (class);
872         if (ifaces) {
873                 for (i = 0; i < ifaces->len; ++i) {
874                         ic = g_ptr_array_index (ifaces, i);
875                         class->interface_offsets [ic->interface_id] = cur_slot;
876                         cur_slot += ic->method.count;
877                 }
878                 g_ptr_array_free (ifaces, TRUE);
879         }
880
881         for (k = class->parent; k ; k = k->parent) {
882                 ifaces = collect_implemented_interfaces (k);
883                 if (ifaces) {
884                         for (i = 0; i < ifaces->len; ++i) {
885                                 ic = g_ptr_array_index (ifaces, i);
886
887                                 if (class->interface_offsets [ic->interface_id] == -1) {
888                                         int io = k->interface_offsets [ic->interface_id];
889
890                                         g_assert (io >= 0);
891
892                                         class->interface_offsets [ic->interface_id] = io;
893                                 }
894                         }
895                         g_ptr_array_free (ifaces, TRUE);
896                 }
897         }
898
899         if (MONO_CLASS_IS_INTERFACE (class))
900                 class->interface_offsets [class->interface_id] = cur_slot;
901
902         return cur_slot;
903 }
904
905 void
906 mono_class_setup_vtable (MonoClass *class, MonoMethod **overrides, int onum)
907 {
908         MonoClass *k, *ic;
909         MonoMethod **vtable;
910         int i, max_vtsize = 0, max_iid, cur_slot = 0;
911         GPtrArray *ifaces;
912         MonoGHashTable *override_map = NULL;
913
914         /* setup_vtable() must be called only once on the type */
915         if (class->interface_offsets) {
916                 g_warning ("vtable already computed in %s.%s", class->name_space, class->name);
917                 return;
918         }
919
920         ifaces = collect_implemented_interfaces (class);
921         if (ifaces) {
922                 for (i = 0; i < ifaces->len; i++) {
923                         MonoClass *ic = g_ptr_array_index (ifaces, i);
924                         max_vtsize += ic->method.count;
925                 }
926                 g_ptr_array_free (ifaces, TRUE);
927         }
928         
929         if (class->parent) {
930                 max_vtsize += class->parent->vtable_size;
931                 cur_slot = class->parent->vtable_size;
932         }
933
934         max_vtsize += class->method.count;
935
936         vtable = alloca (sizeof (gpointer) * max_vtsize);
937         memset (vtable, 0, sizeof (gpointer) * max_vtsize);
938
939         /* printf ("METAINIT %s.%s\n", class->name_space, class->name); */
940
941         cur_slot = setup_interface_offsets (class, cur_slot);
942         max_iid = class->max_interface_id;
943
944         if (class->parent && class->parent->vtable_size)
945                 memcpy (vtable, class->parent->vtable,  sizeof (gpointer) * class->parent->vtable_size);
946
947         /* override interface methods */
948         for (i = 0; i < onum; i++) {
949                 MonoMethod *decl = overrides [i*2];
950                 if (MONO_CLASS_IS_INTERFACE (decl->klass)) {
951                         int dslot;
952                         g_assert (decl->slot != -1);
953                         dslot = decl->slot + class->interface_offsets [decl->klass->interface_id];
954                         vtable [dslot] = overrides [i*2 + 1];
955                         vtable [dslot]->slot = dslot;
956                         if (!override_map)
957                                 override_map = mono_g_hash_table_new (NULL, NULL);
958
959                         mono_g_hash_table_insert (override_map, overrides [i * 2], overrides [i * 2 + 1]);
960                 }
961         }
962
963         for (k = class; k ; k = k->parent) {
964                 int nifaces = 0;
965                 ifaces = collect_implemented_interfaces (k);
966                 if (ifaces)
967                         nifaces = ifaces->len;
968                 for (i = 0; i < nifaces; i++) {
969                         int j, l, io;
970
971                         ic = g_ptr_array_index (ifaces, i);
972                         io = k->interface_offsets [ic->interface_id];
973
974                         g_assert (io >= 0);
975                         g_assert (io <= max_vtsize);
976
977                         if (k == class) {
978                                 for (l = 0; l < ic->method.count; l++) {
979                                         MonoMethod *im = ic->methods [l];                                               
980
981                                         if (vtable [io + l] && !(vtable [io + l]->flags & METHOD_ATTRIBUTE_ABSTRACT))
982                                                 continue;
983
984                                         for (j = 0; j < class->method.count; ++j) {
985                                                 MonoMethod *cm = class->methods [j];
986                                                 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
987                                                     !((cm->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == METHOD_ATTRIBUTE_PUBLIC) ||
988                                                     !(cm->flags & METHOD_ATTRIBUTE_NEW_SLOT))
989                                                         continue;
990                                                 if (!strcmp(cm->name, im->name) && 
991                                                     mono_metadata_signature_equal (cm->signature, im->signature)) {
992                                                         g_assert (io + l <= max_vtsize);
993                                                         vtable [io + l] = cm;
994                                                 }
995                                         }
996                                 }
997                         } else {
998                                 /* already implemented */
999                                 if (io >= k->vtable_size)
1000                                         continue;
1001                         }
1002                                 
1003                         for (l = 0; l < ic->method.count; l++) {
1004                                 MonoMethod *im = ic->methods [l];                                               
1005                                 MonoClass *k1;
1006
1007                                 g_assert (io + l <= max_vtsize);
1008
1009                                 if (vtable [io + l] && !(vtable [io + l]->flags & METHOD_ATTRIBUTE_ABSTRACT))
1010                                         continue;
1011                                         
1012                                 for (k1 = class; k1; k1 = k1->parent) {
1013                                         for (j = 0; j < k1->method.count; ++j) {
1014                                                 MonoMethod *cm = k1->methods [j];
1015
1016                                                 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
1017                                                     !(cm->flags & METHOD_ATTRIBUTE_PUBLIC))
1018                                                         continue;
1019                                                 
1020                                                 if (!strcmp(cm->name, im->name) && 
1021                                                     mono_metadata_signature_equal (cm->signature, im->signature)) {
1022                                                         g_assert (io + l <= max_vtsize);
1023                                                         vtable [io + l] = cm;
1024                                                         break;
1025                                                 }
1026                                                 
1027                                         }
1028                                         g_assert (io + l <= max_vtsize);
1029                                         if (vtable [io + l] && !(vtable [io + l]->flags & METHOD_ATTRIBUTE_ABSTRACT))
1030                                                 break;
1031                                 }
1032                         }
1033
1034                         for (l = 0; l < ic->method.count; l++) {
1035                                 MonoMethod *im = ic->methods [l];                                               
1036                                 char *qname, *fqname;
1037                                 MonoClass *k1;
1038                                 
1039                                 if (vtable [io + l])
1040                                         continue;
1041                                         
1042                                 qname = g_strconcat (ic->name, ".", im->name, NULL); 
1043                                 if (ic->name_space && ic->name_space [0])
1044                                         fqname = g_strconcat (ic->name_space, ".", ic->name, ".", im->name, NULL);
1045                                 else
1046                                         fqname = NULL;
1047
1048                                 for (k1 = class; k1; k1 = k1->parent) {
1049                                         for (j = 0; j < k1->method.count; ++j) {
1050                                                 MonoMethod *cm = k1->methods [j];
1051
1052                                                 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL))
1053                                                         continue;
1054                                         
1055                                                 if (((fqname && !strcmp (cm->name, fqname)) || !strcmp (cm->name, qname)) &&
1056                                                     mono_metadata_signature_equal (cm->signature, im->signature)) {
1057                                                         g_assert (io + l <= max_vtsize);
1058                                                         vtable [io + l] = cm;
1059                                                         break;
1060                                                 }
1061                                         }
1062                                 }
1063                                 g_free (qname);
1064                                 g_free (fqname);
1065                         }
1066
1067                         
1068                         if (!(class->flags & TYPE_ATTRIBUTE_ABSTRACT)) {
1069                                 for (l = 0; l < ic->method.count; l++) {
1070                                         char *msig;
1071                                         MonoMethod *im = ic->methods [l];
1072                                         if (im->flags & METHOD_ATTRIBUTE_STATIC)
1073                                                         continue;
1074                                         g_assert (io + l <= max_vtsize);
1075
1076                                         /* 
1077                                          * If one of our parents already implements this interface
1078                                          * we can inherit the implementation.
1079                                          */
1080                                         if (!(vtable [io + l])) {
1081                                                 MonoClass *parent = class->parent;
1082
1083                                                 if ((ic->interface_id <= parent->max_interface_id) && 
1084                                                         (parent->interface_offsets [ic->interface_id]) &&
1085                                                         parent->vtable)
1086                                                         vtable [io + l] = parent->vtable [parent->interface_offsets [ic->interface_id] + l];
1087                                         }
1088
1089                                         if (!(vtable [io + l])) {
1090                                                 for (j = 0; j < onum; ++j) {
1091                                                         g_print (" at slot %d: %s (%d) overrides %s (%d)\n", io+l, overrides [j*2+1]->name, 
1092                                                                  overrides [j*2+1]->slot, overrides [j*2]->name, overrides [j*2]->slot);
1093                                                 }
1094                                                 msig = mono_signature_get_desc (im->signature, FALSE);
1095                                                 printf ("no implementation for interface method %s.%s::%s(%s) in class %s.%s\n",
1096                                                         ic->name_space, ic->name, im->name, msig, class->name_space, class->name);
1097                                                 g_free (msig);
1098                                                 for (j = 0; j < class->method.count; ++j) {
1099                                                         MonoMethod *cm = class->methods [j];
1100                                                         msig = mono_signature_get_desc (cm->signature, FALSE);
1101                                                         
1102                                                         printf ("METHOD %s(%s)\n", cm->name, msig);
1103                                                         g_free (msig);
1104                                                 }
1105                                                 g_assert_not_reached ();
1106                                         }
1107                                 }
1108                         }
1109                 
1110                         for (l = 0; l < ic->method.count; l++) {
1111                                 MonoMethod *im = vtable [io + l];
1112
1113                                 if (im) {
1114                                         g_assert (io + l <= max_vtsize);
1115                                         if (im->slot < 0) {
1116                                                 /* FIXME: why do we need this ? */
1117                                                 im->slot = io + l;
1118                                                 /* g_assert_not_reached (); */
1119                                         }
1120                                 }
1121                         }
1122                 }
1123                 if (ifaces)
1124                         g_ptr_array_free (ifaces, TRUE);
1125         } 
1126
1127         for (i = 0; i < class->method.count; ++i) {
1128                 MonoMethod *cm;
1129                
1130                 cm = class->methods [i];
1131                 
1132                 /*
1133                  * Non-virtual method have no place in the vtable.
1134                  * This also catches static methods (since they are not virtual).
1135                  */
1136                 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL))
1137                         continue;
1138                 
1139                 /*
1140                  * If the method is REUSE_SLOT, we must check in the
1141                  * base class for a method to override.
1142                  */
1143                 if (!(cm->flags & METHOD_ATTRIBUTE_NEW_SLOT)) {
1144                         int slot = -1;
1145                         for (k = class->parent; k ; k = k->parent) {
1146                                 int j;
1147                                 for (j = 0; j < k->method.count; ++j) {
1148                                         MonoMethod *m1 = k->methods [j];
1149                                         if (!(m1->flags & METHOD_ATTRIBUTE_VIRTUAL))
1150                                                 continue;
1151                                         if (!strcmp(cm->name, m1->name) && 
1152                                             mono_metadata_signature_equal (cm->signature, m1->signature)) {
1153                                                 slot = k->methods [j]->slot;
1154                                                 g_assert (cm->slot < max_vtsize);
1155                                                 if (!override_map)
1156                                                         override_map = mono_g_hash_table_new (NULL, NULL);
1157                                                 mono_g_hash_table_insert (override_map, m1, cm);
1158                                                 break;
1159                                         }
1160                                 }
1161                                 if (slot >= 0) 
1162                                         break;
1163                         }
1164                         if (slot >= 0)
1165                                 cm->slot = slot;
1166                 }
1167
1168                 if (cm->slot < 0)
1169                         cm->slot = cur_slot++;
1170
1171                 if (!(cm->flags & METHOD_ATTRIBUTE_ABSTRACT) && !cm->signature->generic_param_count)
1172                         vtable [cm->slot] = cm;
1173         }
1174
1175         /* override non interface methods */
1176         for (i = 0; i < onum; i++) {
1177                 MonoMethod *decl = overrides [i*2];
1178                 if (!MONO_CLASS_IS_INTERFACE (decl->klass)) {
1179                         g_assert (decl->slot != -1);
1180                         vtable [decl->slot] = overrides [i*2 + 1];
1181                         overrides [i * 2 + 1]->slot = decl->slot;
1182                         if (!override_map)
1183                                 override_map = mono_g_hash_table_new (NULL, NULL);
1184                         mono_g_hash_table_insert (override_map, decl, overrides [i * 2 + 1]);
1185                 }
1186         }
1187
1188         /*
1189          * If a method occupies more than one place in the vtable, and it is
1190          * overriden, then change the other occurances too.
1191          */
1192         if (override_map) {
1193                 for (i = 0; i < max_vtsize; ++i)
1194                         if (vtable [i]) {
1195                                 MonoMethod *cm = mono_g_hash_table_lookup (override_map, vtable [i]);
1196                                 if (cm)
1197                                         vtable [i] = cm;
1198                         }
1199
1200                 mono_g_hash_table_destroy (override_map);
1201         }
1202
1203         if (class->generic_inst) {
1204                 MonoClass *gklass = mono_class_from_mono_type (class->generic_inst->generic_type);
1205
1206                 mono_class_init (gklass);
1207                 class->vtable_size = gklass->vtable_size;
1208         } else       
1209                 class->vtable_size = cur_slot;
1210
1211         class->vtable = g_malloc0 (sizeof (gpointer) * class->vtable_size);
1212         memcpy (class->vtable, vtable,  sizeof (gpointer) * class->vtable_size);
1213
1214         if (mono_print_vtable) {
1215                 int icount = 0;
1216
1217                 for (i = 0; i <= max_iid; i++)
1218                         if (class->interface_offsets [i] != -1)
1219                                 icount++;
1220
1221                 printf ("VTable %s.%s (size = %d, interfaces = %d)\n", class->name_space, 
1222                         class->name, class->vtable_size, icount); 
1223
1224                 for (i = 0; i < class->vtable_size; ++i) {
1225                         MonoMethod *cm;
1226                
1227                         cm = vtable [i];
1228                         if (cm) {
1229                                 printf ("  slot %03d(%03d) %s.%s:%s\n", i, cm->slot,
1230                                         cm->klass->name_space, cm->klass->name,
1231                                         cm->name);
1232                         }
1233                 }
1234
1235
1236                 if (icount) {
1237                         printf ("Interfaces %s.%s (max_iid = %d)\n", class->name_space, 
1238                                 class->name, max_iid);
1239         
1240                         for (i = 0; i < class->interface_count; i++) {
1241                                 ic = class->interfaces [i];
1242                                 printf ("  slot %03d(%03d) %s.%s\n",  
1243                                         class->interface_offsets [ic->interface_id],
1244                                         ic->method.count, ic->name_space, ic->name);
1245                         }
1246
1247                         for (k = class->parent; k ; k = k->parent) {
1248                                 for (i = 0; i < k->interface_count; i++) {
1249                                         ic = k->interfaces [i]; 
1250                                         printf ("  slot %03d(%03d) %s.%s\n", 
1251                                                 class->interface_offsets [ic->interface_id],
1252                                                 ic->method.count, ic->name_space, ic->name);
1253                                 }
1254                         }
1255                 }
1256         }
1257 }
1258
1259 /**
1260  * mono_class_init:
1261  * @class: the class to initialize
1262  *
1263  * compute the instance_size, class_size and other infos that cannot be 
1264  * computed at mono_class_get() time. Also compute a generic vtable and 
1265  * the method slot numbers. We use this infos later to create a domain
1266  * specific vtable.  
1267  */
1268 void
1269 mono_class_init (MonoClass *class)
1270 {
1271         int i;
1272         static MonoMethod *default_ghc = NULL;
1273         static MonoMethod *default_finalize = NULL;
1274         static int finalize_slot = -1;
1275         static int ghc_slot = -1;
1276         MonoMethod **overrides;
1277         int onum = 0;
1278
1279         g_assert (class);
1280
1281         if (class->inited)
1282                 return;
1283
1284         /*g_print ("Init class %s\n", class->name);*/
1285
1286         /* We do everything inside the lock to prevent races */
1287         mono_loader_lock ();
1288
1289         if (class->inited) {
1290                 mono_loader_unlock ();
1291                 /* Somebody might have gotten in before us */
1292                 return;
1293         }
1294
1295         if (class->init_pending) {
1296                 /* this indicates a cyclic dependency */
1297                 g_error ("pending init %s.%s\n", class->name_space, class->name);
1298         }
1299
1300         class->init_pending = 1;
1301
1302         if (mono_debugger_start_class_init_func)
1303                 mono_debugger_start_class_init_func (class);
1304
1305         mono_stats.initialized_class_count++;
1306
1307         if (class->generic_inst && !class->generic_inst->is_dynamic) {
1308                 MonoGenericInst *ginst = class->generic_inst;
1309                 MonoClass *gklass;
1310                 GList *list;
1311
1312                 gklass = mono_class_from_mono_type (ginst->generic_type);
1313                 mono_class_init (gklass);
1314
1315                 if (ginst->parent)
1316                         class->parent = mono_class_from_mono_type (ginst->parent);
1317                 else
1318                         class->parent = gklass->parent;
1319
1320                 mono_class_setup_parent (class, class->parent);
1321
1322                 if (MONO_CLASS_IS_INTERFACE (class))
1323                         class->interface_id = mono_get_unique_iid (class);
1324
1325                 class->method = gklass->method;
1326                 class->methods = g_new0 (MonoMethod *, class->method.count);
1327
1328                 for (i = 0; i < class->method.count; i++)
1329                         class->methods [i] = mono_class_inflate_generic_method (
1330                                 gklass->methods [i], ginst->context, ginst->klass);
1331
1332                 class->field = gklass->field;
1333                 class->fields = g_new0 (MonoClassField, class->field.count);
1334
1335                 for (i = 0; i < class->field.count; i++) {
1336                         MonoInflatedField *ifield = g_new0 (MonoInflatedField, 1);
1337                         ifield->generic_type = gklass->fields [i].type;
1338
1339                         class->fields [i] = gklass->fields [i];
1340                         class->fields [i].generic_info = ifield;
1341                         class->fields [i].parent = class;
1342                         class->fields [i].type = mono_class_inflate_generic_type (
1343                                 class->fields [i].type, ginst->context);
1344                 }
1345
1346                 class->property = gklass->property;
1347                 class->properties = g_new0 (MonoProperty, class->property.count);
1348
1349                 for (i = 0; i < class->property.count; i++) {
1350                         MonoProperty *prop = &class->properties [i];
1351
1352                         *prop = gklass->properties [i];
1353
1354                         if (prop->get)
1355                                 prop->get = mono_class_inflate_generic_method (
1356                                         prop->get, ginst->context, ginst->klass);
1357                         if (prop->set)
1358                                 prop->set = mono_class_inflate_generic_method (
1359                                         prop->set, ginst->context, ginst->klass);
1360
1361                         prop->parent = class;
1362                 }
1363
1364                 class->interface_count = gklass->interface_count;
1365                 class->interfaces = g_new0 (MonoClass *, class->interface_count);
1366                 for (i = 0; i < class->interface_count; i++) {
1367                         MonoType *it = &gklass->interfaces [i]->byval_arg;
1368                         MonoType *inflated = mono_class_inflate_generic_type (
1369                                 it, ginst->context);
1370                         class->interfaces [i] = mono_class_from_mono_type (inflated);
1371                         mono_class_init (class->interfaces [i]);
1372                 }
1373
1374                 for (list = gklass->nested_classes; list; list = list->next)
1375                         class->nested_classes = g_list_append (
1376                                 class->nested_classes, list->data);
1377         }
1378
1379         if (class->parent && !class->parent->inited)
1380                 mono_class_init (class->parent);
1381
1382         /*
1383          * Computes the size used by the fields, and their locations
1384          */
1385         if (!class->size_inited)
1386                 class_compute_field_layout (class);
1387
1388         /* initialize method pointers */
1389         if (class->rank) {
1390                 MonoMethod *ctor;
1391                 MonoMethodSignature *sig;
1392                 class->method.count = class->rank > 1? 2: 1;
1393                 sig = mono_metadata_signature_alloc (class->image, class->rank);
1394                 sig->ret = &mono_defaults.void_class->byval_arg;
1395                 sig->pinvoke = TRUE;
1396                 for (i = 0; i < class->rank; ++i)
1397                         sig->params [i] = &mono_defaults.int32_class->byval_arg;
1398
1399                 ctor = (MonoMethod *) g_new0 (MonoMethodPInvoke, 1);
1400                 ctor->klass = class;
1401                 ctor->flags = METHOD_ATTRIBUTE_PUBLIC | METHOD_ATTRIBUTE_RT_SPECIAL_NAME | METHOD_ATTRIBUTE_SPECIAL_NAME;
1402                 ctor->iflags = METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL;
1403                 ctor->signature = sig;
1404                 ctor->name = ".ctor";
1405                 ctor->slot = -1;
1406                 class->methods = g_new (MonoMethod*, class->method.count);
1407                 class->methods [0] = ctor;
1408                 if (class->rank > 1) {
1409                         sig = mono_metadata_signature_alloc (class->image, class->rank * 2);
1410                         sig->ret = &mono_defaults.void_class->byval_arg;
1411                         sig->pinvoke = TRUE;
1412                         for (i = 0; i < class->rank * 2; ++i)
1413                                 sig->params [i] = &mono_defaults.int32_class->byval_arg;
1414
1415                         ctor = (MonoMethod *) g_new0 (MonoMethodPInvoke, 1);
1416                         ctor->klass = class;
1417                         ctor->flags = METHOD_ATTRIBUTE_PUBLIC | METHOD_ATTRIBUTE_RT_SPECIAL_NAME | METHOD_ATTRIBUTE_SPECIAL_NAME;
1418                         ctor->iflags = METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL;
1419                         ctor->signature = sig;
1420                         ctor->name = ".ctor";
1421                         ctor->slot = -1;
1422                         class->methods [1] = ctor;
1423                 }
1424         } else {
1425                 if (!class->generic_inst && !class->methods) {
1426                         class->methods = g_new (MonoMethod*, class->method.count);
1427                         for (i = 0; i < class->method.count; ++i) {
1428                                 class->methods [i] = mono_get_method (class->image,
1429                                                                       MONO_TOKEN_METHOD_DEF | (i + class->method.first + 1), class);
1430                         }
1431                 }
1432         }
1433
1434         if (!class->generic_inst) {
1435                 init_properties (class);
1436                 init_events (class);
1437
1438                 i = mono_metadata_nesting_typedef (class->image, class->type_token, 1);
1439                 while (i) {
1440                         MonoClass* nclass;
1441                         guint32 cols [MONO_NESTED_CLASS_SIZE];
1442                         mono_metadata_decode_row (&class->image->tables [MONO_TABLE_NESTEDCLASS], i - 1, cols, MONO_NESTED_CLASS_SIZE);
1443                         nclass = mono_class_create_from_typedef (class->image, MONO_TOKEN_TYPE_DEF | cols [MONO_NESTED_CLASS_NESTED]);
1444                         class->nested_classes = g_list_prepend (class->nested_classes, nclass);
1445
1446                         i = mono_metadata_nesting_typedef (class->image, class->type_token, i + 1);
1447                 }
1448         }
1449
1450         mono_class_setup_supertypes (class);
1451
1452         if (MONO_CLASS_IS_INTERFACE (class)) {
1453                 for (i = 0; i < class->method.count; ++i)
1454                         class->methods [i]->slot = i;
1455                 class->init_pending = 0;
1456                 class->inited = 1;
1457                 /* 
1458                  * class->interface_offsets is needed for the castclass/isinst code, so
1459                  * we have to setup them for interfaces, too.
1460                  */
1461                 setup_interface_offsets (class, 0);
1462                 mono_loader_unlock ();
1463
1464                 if (mono_debugger_class_init_func)
1465                         mono_debugger_class_init_func (class);
1466
1467                 return;
1468         }
1469
1470         overrides = mono_class_get_overrides (class->image, class->type_token, &onum);  
1471         mono_class_setup_vtable (class, overrides, onum);
1472         g_free (overrides);
1473
1474         class->inited = 1;
1475         class->init_pending = 0;
1476
1477         if (!default_ghc) {
1478                 if (class == mono_defaults.object_class) { 
1479                        
1480                         for (i = 0; i < class->vtable_size; ++i) {
1481                                 MonoMethod *cm = class->vtable [i];
1482                
1483                                 if (!strcmp (cm->name, "GetHashCode")) {
1484                                         ghc_slot = i;
1485                                         break;
1486                                 }
1487                         }
1488
1489                         g_assert (ghc_slot > 0);
1490
1491                         default_ghc = class->vtable [ghc_slot];
1492                 }
1493         }
1494         
1495         class->ghcimpl = 1;
1496         if (class->parent) { 
1497
1498                 if (class->vtable [ghc_slot] == default_ghc) {
1499                         class->ghcimpl = 0;
1500                 }
1501         }
1502
1503         if (!default_finalize) {
1504                 if (class == mono_defaults.object_class) { 
1505                        
1506                         for (i = 0; i < class->vtable_size; ++i) {
1507                                 MonoMethod *cm = class->vtable [i];
1508                
1509                                 if (!strcmp (cm->name, "Finalize")) {
1510                                         finalize_slot = i;
1511                                         break;
1512                                 }
1513                         }
1514
1515                         g_assert (finalize_slot > 0);
1516
1517                         default_finalize = class->vtable [finalize_slot];
1518                 }
1519         }
1520
1521         /* Object::Finalize should have empty implemenatation */
1522         class->has_finalize = 0;
1523         if (class->parent) { 
1524                 if (class->vtable [finalize_slot] != default_finalize)
1525                         class->has_finalize = 1;
1526         }
1527
1528         mono_loader_unlock ();
1529
1530         if (mono_debugger_class_init_func)
1531                 mono_debugger_class_init_func (class);
1532 }
1533
1534 void
1535 mono_class_setup_mono_type (MonoClass *class)
1536 {
1537         const char *name = class->name;
1538         const char *nspace = class->name_space;
1539
1540         if (MONO_CLASS_IS_INTERFACE (class))
1541                 class->interface_id = mono_get_unique_iid (class);
1542
1543         class->this_arg.byref = 1;
1544         class->this_arg.data.klass = class;
1545         class->this_arg.type = MONO_TYPE_CLASS;
1546         class->byval_arg.data.klass = class;
1547         class->byval_arg.type = MONO_TYPE_CLASS;
1548
1549         if (!strcmp (nspace, "System")) {
1550                 if (!strcmp (name, "ValueType")) {
1551                         /*
1552                          * do not set the valuetype bit for System.ValueType.
1553                          * class->valuetype = 1;
1554                          */
1555                         class->blittable = TRUE;
1556                 } else if (!strcmp (name, "Enum")) {
1557                         /*
1558                          * do not set the valuetype bit for System.Enum.
1559                          * class->valuetype = 1;
1560                          */
1561                         class->valuetype = 0;
1562                         class->enumtype = 0;
1563                 } else if (!strcmp (name, "Object")) {
1564                         class->this_arg.type = class->byval_arg.type = MONO_TYPE_OBJECT;
1565                 } else if (!strcmp (name, "String")) {
1566                         class->this_arg.type = class->byval_arg.type = MONO_TYPE_STRING;
1567                 } else if (!strcmp (name, "TypedReference")) {
1568                         class->this_arg.type = class->byval_arg.type = MONO_TYPE_TYPEDBYREF;
1569                 }
1570         }
1571         
1572         if (class->valuetype) {
1573                 int t = MONO_TYPE_VALUETYPE;
1574                 if (!strcmp (nspace, "System")) {
1575                         switch (*name) {
1576                         case 'B':
1577                                 if (!strcmp (name, "Boolean")) {
1578                                         t = MONO_TYPE_BOOLEAN;
1579                                 } else if (!strcmp(name, "Byte")) {
1580                                         t = MONO_TYPE_U1;
1581                                         class->blittable = TRUE;                                                
1582                                 }
1583                                 break;
1584                         case 'C':
1585                                 if (!strcmp (name, "Char")) {
1586                                         t = MONO_TYPE_CHAR;
1587                                 }
1588                                 break;
1589                         case 'D':
1590                                 if (!strcmp (name, "Double")) {
1591                                         t = MONO_TYPE_R8;
1592                                         class->blittable = TRUE;                                                
1593                                 }
1594                                 break;
1595                         case 'I':
1596                                 if (!strcmp (name, "Int32")) {
1597                                         t = MONO_TYPE_I4;
1598                                         class->blittable = TRUE;
1599                                 } else if (!strcmp(name, "Int16")) {
1600                                         t = MONO_TYPE_I2;
1601                                         class->blittable = TRUE;
1602                                 } else if (!strcmp(name, "Int64")) {
1603                                         t = MONO_TYPE_I8;
1604                                         class->blittable = TRUE;
1605                                 } else if (!strcmp(name, "IntPtr")) {
1606                                         t = MONO_TYPE_I;
1607                                         class->blittable = TRUE;
1608                                 }
1609                                 break;
1610                         case 'S':
1611                                 if (!strcmp (name, "Single")) {
1612                                         t = MONO_TYPE_R4;
1613                                         class->blittable = TRUE;                                                
1614                                 } else if (!strcmp(name, "SByte")) {
1615                                         t = MONO_TYPE_I1;
1616                                         class->blittable = TRUE;
1617                                 }
1618                                 break;
1619                         case 'U':
1620                                 if (!strcmp (name, "UInt32")) {
1621                                         t = MONO_TYPE_U4;
1622                                         class->blittable = TRUE;
1623                                 } else if (!strcmp(name, "UInt16")) {
1624                                         t = MONO_TYPE_U2;
1625                                         class->blittable = TRUE;
1626                                 } else if (!strcmp(name, "UInt64")) {
1627                                         t = MONO_TYPE_U8;
1628                                         class->blittable = TRUE;
1629                                 } else if (!strcmp(name, "UIntPtr")) {
1630                                         t = MONO_TYPE_U;
1631                                         class->blittable = TRUE;
1632                                 }
1633                                 break;
1634                         case 'T':
1635                                 if (!strcmp (name, "TypedReference")) {
1636                                         t = MONO_TYPE_TYPEDBYREF;
1637                                         class->blittable = TRUE;
1638                                 }
1639                                 break;
1640                         case 'V':
1641                                 if (!strcmp (name, "Void")) {
1642                                         t = MONO_TYPE_VOID;
1643                                 }
1644                                 break;
1645                         default:
1646                                 break;
1647                         }
1648                 }
1649                 class->this_arg.type = class->byval_arg.type = t;
1650         }
1651 }
1652
1653 void
1654 mono_class_setup_parent (MonoClass *class, MonoClass *parent)
1655 {
1656         gboolean system_namespace;
1657
1658         system_namespace = !strcmp (class->name_space, "System");
1659
1660         /* if root of the hierarchy */
1661         if (system_namespace && !strcmp (class->name, "Object")) {
1662                 class->parent = NULL;
1663                 class->instance_size = sizeof (MonoObject);
1664                 return;
1665         }
1666         if (!strcmp (class->name, "<Module>")) {
1667                 class->parent = NULL;
1668                 class->instance_size = 0;
1669                 return;
1670         }
1671
1672         if (!MONO_CLASS_IS_INTERFACE (class)) {
1673                 class->parent = parent;
1674
1675                 if (!parent)
1676                         g_assert_not_reached (); /* FIXME */
1677
1678                 if (parent->generic_inst && !parent->name) {
1679                         /*
1680                          * If the parent is a generic instance, we may get
1681                          * called before it is fully initialized, especially
1682                          * before it has its name.
1683                          */
1684                         return;
1685                 }
1686
1687                 class->marshalbyref = parent->marshalbyref;
1688                 class->contextbound  = parent->contextbound;
1689                 class->delegate  = parent->delegate;
1690                 
1691                 if (system_namespace) {
1692                         if (*class->name == 'M' && !strcmp (class->name, "MarshalByRefObject"))
1693                                 class->marshalbyref = 1;
1694
1695                         if (*class->name == 'C' && !strcmp (class->name, "ContextBoundObject")) 
1696                                 class->contextbound  = 1;
1697
1698                         if (*class->name == 'D' && !strcmp (class->name, "Delegate")) 
1699                                 class->delegate  = 1;
1700                 }
1701
1702                 if (class->parent->enumtype || ((strcmp (class->parent->name, "ValueType") == 0) && 
1703                                                 (strcmp (class->parent->name_space, "System") == 0)))
1704                         class->valuetype = 1;
1705                 if (((strcmp (class->parent->name, "Enum") == 0) && (strcmp (class->parent->name_space, "System") == 0))) {
1706                         class->valuetype = class->enumtype = 1;
1707                 }
1708                 /*class->enumtype = class->parent->enumtype; */
1709                 mono_class_setup_supertypes (class);
1710         } else {
1711                 class->parent = NULL;
1712         }
1713
1714 }
1715
1716 void
1717 mono_class_setup_supertypes (MonoClass *class)
1718 {
1719         MonoClass *k;
1720         int ms, i;
1721
1722         if (class->supertypes)
1723                 return;
1724
1725         class->idepth = 0;
1726         for (k = class; k ; k = k->parent) {
1727                 class->idepth++;
1728         }
1729
1730         ms = MAX (MONO_DEFAULT_SUPERTABLE_SIZE, class->idepth);
1731         class->supertypes = g_new0 (MonoClass *, ms);
1732
1733         if (class->parent) {
1734                 for (i = class->idepth, k = class; k ; k = k->parent)
1735                         class->supertypes [--i] = k;
1736         } else {
1737                 class->supertypes [0] = class;
1738         }
1739 }       
1740
1741 /**
1742  * @image: context where the image is created
1743  * @type_token:  typedef token
1744  */
1745 static MonoClass *
1746 mono_class_create_from_typedef (MonoImage *image, guint32 type_token)
1747 {
1748         MonoTableInfo *tt = &image->tables [MONO_TABLE_TYPEDEF];
1749         MonoClass *class, *parent = NULL;
1750         guint32 cols [MONO_TYPEDEF_SIZE];
1751         guint32 cols_next [MONO_TYPEDEF_SIZE];
1752         guint tidx = mono_metadata_token_index (type_token);
1753         const char *name, *nspace;
1754         guint icount = 0; 
1755         MonoClass **interfaces;
1756
1757         mono_loader_lock ();
1758
1759         if ((class = g_hash_table_lookup (image->class_cache, GUINT_TO_POINTER (type_token)))) {
1760                 mono_loader_unlock ();
1761                 return class;
1762         }
1763
1764         g_assert (mono_metadata_token_table (type_token) == MONO_TABLE_TYPEDEF);
1765
1766         mono_metadata_decode_row (tt, tidx - 1, cols, MONO_TYPEDEF_SIZE);
1767         
1768         name = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
1769         nspace = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
1770
1771         class = g_malloc0 (sizeof (MonoClass));
1772
1773         class->name = name;
1774         class->name_space = nspace;
1775
1776         class->image = image;
1777         class->type_token = type_token;
1778         class->flags = cols [MONO_TYPEDEF_FLAGS];
1779
1780         g_hash_table_insert (image->class_cache, GUINT_TO_POINTER (type_token), class);
1781
1782         if (cols [MONO_TYPEDEF_EXTENDS])
1783                 parent = mono_class_get (image, mono_metadata_token_from_dor (cols [MONO_TYPEDEF_EXTENDS]));
1784         interfaces = mono_metadata_interfaces_from_typedef (image, type_token, &icount);
1785
1786         class->interfaces = interfaces;
1787         class->interface_count = icount;
1788
1789         if ((class->flags & TYPE_ATTRIBUTE_STRING_FORMAT_MASK) == TYPE_ATTRIBUTE_UNICODE_CLASS)
1790                 class->unicode = 1;
1791         /* fixme: maybe we must set this on windows 
1792         if ((class->flags & TYPE_ATTRIBUTE_STRING_FORMAT_MASK) == TYPE_ATTRIBUTE_AUTO_CLASS)
1793                 class->unicode = 1;
1794         */
1795
1796         class->cast_class = class->element_class = class;
1797
1798         /*g_print ("Load class %s\n", name);*/
1799
1800         mono_class_setup_parent (class, parent);
1801
1802         mono_class_setup_mono_type (class);
1803
1804         /*
1805          * Compute the field and method lists
1806          */
1807         class->field.first  = cols [MONO_TYPEDEF_FIELD_LIST] - 1;
1808         class->method.first = cols [MONO_TYPEDEF_METHOD_LIST] - 1;
1809
1810         if (tt->rows > tidx){           
1811                 mono_metadata_decode_row (tt, tidx, cols_next, MONO_TYPEDEF_SIZE);
1812                 class->field.last  = cols_next [MONO_TYPEDEF_FIELD_LIST] - 1;
1813                 class->method.last = cols_next [MONO_TYPEDEF_METHOD_LIST] - 1;
1814         } else {
1815                 class->field.last  = image->tables [MONO_TABLE_FIELD].rows;
1816                 class->method.last = image->tables [MONO_TABLE_METHOD].rows;
1817         }
1818
1819         if (cols [MONO_TYPEDEF_FIELD_LIST] && 
1820             cols [MONO_TYPEDEF_FIELD_LIST] <= image->tables [MONO_TABLE_FIELD].rows)
1821                 class->field.count = class->field.last - class->field.first;
1822         else
1823                 class->field.count = 0;
1824
1825         if (cols [MONO_TYPEDEF_METHOD_LIST] <= image->tables [MONO_TABLE_METHOD].rows)
1826                 class->method.count = class->method.last - class->method.first;
1827         else
1828                 class->method.count = 0;
1829
1830         /* reserve space to store vector pointer in arrays */
1831         if (!strcmp (nspace, "System") && !strcmp (name, "Array")) {
1832                 class->instance_size += 2 * sizeof (gpointer);
1833                 g_assert (class->field.count == 0);
1834         }
1835
1836         if (class->enumtype)
1837                 class_compute_field_layout (class);
1838
1839         if ((type_token = mono_metadata_nested_in_typedef (image, type_token)))
1840                 class->nested_in = mono_class_create_from_typedef (image, type_token);
1841
1842         class->gen_params = mono_metadata_load_generic_params (image, class->type_token, &icount);
1843         class->num_gen_params = icount;
1844
1845         mono_loader_unlock ();
1846
1847         return class;
1848 }
1849
1850 MonoClass*
1851 mono_class_create_generic (MonoGenericInst *ginst)
1852 {
1853         MonoClass *klass, *gklass;
1854
1855         if (!ginst->klass)
1856                 ginst->klass = g_malloc0 (sizeof (MonoClass));
1857         klass = ginst->klass;
1858
1859         gklass = mono_class_from_mono_type (ginst->generic_type);
1860
1861         klass->nested_in = gklass->nested_in;
1862
1863         klass->name = gklass->name;
1864         klass->name_space = gklass->name_space;
1865         klass->image = gklass->image;
1866         klass->flags = gklass->flags;
1867
1868         klass->generic_inst = ginst;
1869
1870         klass->this_arg.type = klass->byval_arg.type = MONO_TYPE_GENERICINST;
1871         klass->this_arg.data.generic_inst = klass->byval_arg.data.generic_inst = ginst;
1872         klass->this_arg.byref = TRUE;
1873
1874         klass->cast_class = klass->element_class = klass;
1875
1876         if (ginst->is_dynamic) {
1877                 klass->instance_size = gklass->instance_size;
1878                 klass->class_size = gklass->class_size;
1879                 klass->size_inited = 1;
1880
1881                 klass->valuetype = gklass->valuetype;
1882         }
1883
1884         return klass;
1885 }
1886
1887 MonoClass *
1888 mono_class_from_generic_parameter (MonoGenericParam *param, MonoImage *image, gboolean is_mvar)
1889 {
1890         MonoClass *klass, **ptr;
1891         int count, pos, i;
1892
1893         if (param->pklass)
1894                 return param->pklass;
1895
1896         klass = param->pklass = g_new0 (MonoClass, 1);
1897
1898         for (count = 0, ptr = param->constraints; ptr && *ptr; ptr++, count++)
1899                 ;
1900
1901         pos = 0;
1902         if ((count > 0) && !MONO_CLASS_IS_INTERFACE (param->constraints [0])) {
1903                 klass->parent = param->constraints [0];
1904                 pos++;
1905         }
1906
1907         if (count - pos > 0) {
1908                 klass->interface_count = count - pos;
1909                 klass->interfaces = g_new0 (MonoClass *, count - pos);
1910                 for (i = pos; i < count; i++)
1911                         klass->interfaces [i - pos] = param->constraints [i];
1912         }
1913
1914         g_assert (param->name);
1915
1916         klass->name = param->name;
1917         klass->name_space = "";
1918         klass->image = image;
1919         klass->cast_class = klass->element_class = klass;
1920         klass->enum_basetype = &klass->element_class->byval_arg;
1921         klass->flags = TYPE_ATTRIBUTE_PUBLIC;
1922
1923         klass->this_arg.type = klass->byval_arg.type = is_mvar ? MONO_TYPE_MVAR : MONO_TYPE_VAR;
1924         klass->this_arg.data.generic_param = klass->byval_arg.data.generic_param = param;
1925         klass->this_arg.byref = TRUE;
1926
1927         mono_class_init (klass);
1928
1929         return klass;
1930 }
1931
1932 static MonoClass *
1933 my_mono_class_from_generic_parameter (MonoGenericParam *param, gboolean is_mvar)
1934 {
1935         MonoClass *klass;
1936
1937         if (param->pklass)
1938                 return param->pklass;
1939
1940         klass = g_new0 (MonoClass, 1);
1941
1942         if (param->name)
1943                 klass->name = param->name;
1944         else
1945                 klass->name = g_strdup_printf (is_mvar ? "!!%d" : "!%d", param->num);
1946         klass->name_space = "";
1947         klass->image = mono_defaults.corlib;
1948         klass->cast_class = klass->element_class = klass;
1949         klass->enum_basetype = &klass->element_class->byval_arg;
1950         klass->flags = TYPE_ATTRIBUTE_PUBLIC;
1951
1952         klass->this_arg.type = klass->byval_arg.type = is_mvar ? MONO_TYPE_MVAR : MONO_TYPE_VAR;
1953         klass->this_arg.data.generic_param = klass->byval_arg.data.generic_param = param;
1954         klass->this_arg.byref = TRUE;
1955
1956         mono_class_init (klass);
1957
1958         return klass;
1959 }
1960
1961 MonoClass *
1962 mono_ptr_class_get (MonoType *type)
1963 {
1964         MonoClass *result;
1965         MonoClass *el_class;
1966         static GHashTable *ptr_hash = NULL;
1967
1968         mono_loader_lock ();
1969
1970         if (!ptr_hash)
1971                 ptr_hash = g_hash_table_new (NULL, NULL);
1972         el_class = mono_class_from_mono_type (type);
1973         if ((result = g_hash_table_lookup (ptr_hash, el_class))) {
1974                 mono_loader_unlock ();
1975                 return result;
1976         }
1977         result = g_new0 (MonoClass, 1);
1978
1979         result->parent = NULL; /* no parent for PTR types */
1980         result->name = "System";
1981         result->name_space = "MonoPtrFakeClass";
1982         result->image = el_class->image;
1983         result->inited = TRUE;
1984         result->flags = TYPE_ATTRIBUTE_CLASS | (el_class->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK);
1985         /* Can pointers get boxed? */
1986         result->instance_size = sizeof (gpointer);
1987         result->cast_class = result->element_class = el_class;
1988         result->enum_basetype = &result->element_class->byval_arg;
1989         result->blittable = TRUE;
1990
1991         result->this_arg.type = result->byval_arg.type = MONO_TYPE_PTR;
1992         result->this_arg.data.type = result->byval_arg.data.type = result->enum_basetype;
1993         result->this_arg.byref = TRUE;
1994
1995         mono_class_setup_supertypes (result);
1996
1997         g_hash_table_insert (ptr_hash, el_class, result);
1998
1999         mono_loader_unlock ();
2000
2001         return result;
2002 }
2003
2004 static MonoClass *
2005 mono_fnptr_class_get (MonoMethodSignature *sig)
2006 {
2007         MonoClass *result;
2008         static GHashTable *ptr_hash = NULL;
2009
2010         mono_loader_lock ();
2011
2012         if (!ptr_hash)
2013                 ptr_hash = g_hash_table_new (NULL, NULL);
2014         
2015         if ((result = g_hash_table_lookup (ptr_hash, sig))) {
2016                 mono_loader_unlock ();
2017                 return result;
2018         }
2019         result = g_new0 (MonoClass, 1);
2020
2021         result->parent = NULL; /* no parent for PTR types */
2022         result->name = "System";
2023         result->name_space = "MonoFNPtrFakeClass";
2024         result->image = NULL; /* need to fix... */
2025         result->inited = TRUE;
2026         result->flags = TYPE_ATTRIBUTE_CLASS; /* | (el_class->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK); */
2027         /* Can pointers get boxed? */
2028         result->instance_size = sizeof (gpointer);
2029         result->cast_class = result->element_class = result;
2030         result->blittable = TRUE;
2031
2032         result->this_arg.type = result->byval_arg.type = MONO_TYPE_FNPTR;
2033         result->this_arg.data.method = result->byval_arg.data.method = sig;
2034         result->this_arg.byref = TRUE;
2035         result->enum_basetype = &result->element_class->byval_arg;
2036         result->blittable = TRUE;
2037
2038         mono_class_setup_supertypes (result);
2039
2040         g_hash_table_insert (ptr_hash, sig, result);
2041
2042         mono_loader_unlock ();
2043
2044         return result;
2045 }
2046
2047 MonoClass *
2048 mono_class_from_mono_type (MonoType *type)
2049 {
2050         switch (type->type) {
2051         case MONO_TYPE_OBJECT:
2052                 return type->data.klass? type->data.klass: mono_defaults.object_class;
2053         case MONO_TYPE_VOID:
2054                 return type->data.klass? type->data.klass: mono_defaults.void_class;
2055         case MONO_TYPE_BOOLEAN:
2056                 return type->data.klass? type->data.klass: mono_defaults.boolean_class;
2057         case MONO_TYPE_CHAR:
2058                 return type->data.klass? type->data.klass: mono_defaults.char_class;
2059         case MONO_TYPE_I1:
2060                 return type->data.klass? type->data.klass: mono_defaults.sbyte_class;
2061         case MONO_TYPE_U1:
2062                 return type->data.klass? type->data.klass: mono_defaults.byte_class;
2063         case MONO_TYPE_I2:
2064                 return type->data.klass? type->data.klass: mono_defaults.int16_class;
2065         case MONO_TYPE_U2:
2066                 return type->data.klass? type->data.klass: mono_defaults.uint16_class;
2067         case MONO_TYPE_I4:
2068                 return type->data.klass? type->data.klass: mono_defaults.int32_class;
2069         case MONO_TYPE_U4:
2070                 return type->data.klass? type->data.klass: mono_defaults.uint32_class;
2071         case MONO_TYPE_I:
2072                 return type->data.klass? type->data.klass: mono_defaults.int_class;
2073         case MONO_TYPE_U:
2074                 return type->data.klass? type->data.klass: mono_defaults.uint_class;
2075         case MONO_TYPE_I8:
2076                 return type->data.klass? type->data.klass: mono_defaults.int64_class;
2077         case MONO_TYPE_U8:
2078                 return type->data.klass? type->data.klass: mono_defaults.uint64_class;
2079         case MONO_TYPE_R4:
2080                 return type->data.klass? type->data.klass: mono_defaults.single_class;
2081         case MONO_TYPE_R8:
2082                 return type->data.klass? type->data.klass: mono_defaults.double_class;
2083         case MONO_TYPE_STRING:
2084                 return type->data.klass? type->data.klass: mono_defaults.string_class;
2085         case MONO_TYPE_TYPEDBYREF:
2086                 return type->data.klass? type->data.klass: mono_defaults.typed_reference_class;
2087         case MONO_TYPE_ARRAY:
2088                 return mono_bounded_array_class_get (type->data.array->eklass, type->data.array->rank, TRUE);
2089         case MONO_TYPE_PTR:
2090                 return mono_ptr_class_get (type->data.type);
2091         case MONO_TYPE_FNPTR:
2092                 return mono_fnptr_class_get (type->data.method);
2093         case MONO_TYPE_SZARRAY:
2094                 return mono_array_class_get (type->data.klass, 1);
2095         case MONO_TYPE_CLASS:
2096         case MONO_TYPE_VALUETYPE:
2097                 return type->data.klass;
2098         case MONO_TYPE_GENERICINST:
2099                 g_assert (type->data.generic_inst->klass);
2100                 return type->data.generic_inst->klass;
2101         case MONO_TYPE_VAR:
2102                 return my_mono_class_from_generic_parameter (type->data.generic_param, FALSE);
2103         case MONO_TYPE_MVAR:
2104                 return my_mono_class_from_generic_parameter (type->data.generic_param, TRUE);
2105         default:
2106                 g_warning ("implement me 0x%02x\n", type->type);
2107                 g_assert_not_reached ();
2108         }
2109         
2110         return NULL;
2111 }
2112
2113 /**
2114  * @image: context where the image is created
2115  * @type_spec:  typespec token
2116  */
2117 static MonoClass *
2118 mono_class_create_from_typespec (MonoImage *image, guint32 type_spec,
2119                                  MonoGenericContext *context)
2120 {
2121         MonoType *type, *inflated;
2122         MonoClass *class;
2123
2124         type = mono_type_create_from_typespec (image, type_spec);
2125
2126         switch (type->type) {
2127         case MONO_TYPE_ARRAY:
2128                 class = mono_array_class_get (type->data.array->eklass, type->data.array->rank);
2129                 break;
2130         case MONO_TYPE_SZARRAY:
2131                 class = mono_array_class_get (type->data.klass, 1);
2132                 break;
2133         case MONO_TYPE_PTR:
2134                 class = mono_ptr_class_get (type->data.type);
2135                 break;
2136         case MONO_TYPE_GENERICINST:
2137                 g_assert (type->data.generic_inst->klass);
2138                 class = type->data.generic_inst->klass;
2139                 break;
2140         default:
2141                 /* it seems any type can be stored in TypeSpec as well */
2142                 class = mono_class_from_mono_type (type);
2143                 break;
2144         }
2145
2146         if (!class || !context)
2147                 return class;
2148
2149         inflated = mono_class_inflate_generic_type (&class->byval_arg, context);
2150
2151         return mono_class_from_mono_type (inflated);
2152 }
2153
2154 /**
2155  * mono_bounded_array_class_get:
2156  * @element_class: element class 
2157  * @rank: the dimension of the array class
2158  * @bounded: whenever the array has non-zero bounds
2159  *
2160  * Returns: a class object describing the array with element type @element_type and 
2161  * dimension @rank. 
2162  */
2163 MonoClass *
2164 mono_bounded_array_class_get (MonoClass *eclass, guint32 rank, gboolean bounded)
2165 {
2166         MonoImage *image;
2167         MonoClass *class;
2168         MonoClass *parent = NULL;
2169         GSList *list, *rootlist;
2170         int nsize;
2171         char *name;
2172         gboolean corlib_type = FALSE;
2173
2174         g_assert (rank <= 255);
2175
2176         if (rank > 1)
2177                 /* bounded only matters for one-dimensional arrays */
2178                 bounded = FALSE;
2179
2180         image = eclass->image;
2181
2182         mono_loader_lock ();
2183
2184         if ((rootlist = list = g_hash_table_lookup (image->array_cache, eclass))) {
2185                 for (; list; list = list->next) {
2186                         class = list->data;
2187                         if ((class->rank == rank) && (class->byval_arg.type == (bounded ? MONO_TYPE_ARRAY : MONO_TYPE_SZARRAY))) {
2188                                 mono_loader_unlock ();
2189                                 return class;
2190                         }
2191                 }
2192         }
2193
2194         /* for the building corlib use System.Array from it */
2195         if (image->assembly && image->assembly->dynamic && strcmp (image->assembly_name, "mscorlib") == 0) {
2196                 parent = mono_class_from_name (image, "System", "Array");
2197                 corlib_type = TRUE;
2198         } else {
2199                 parent = mono_defaults.array_class;
2200                 if (!parent->inited)
2201                         mono_class_init (parent);
2202         }
2203
2204         class = g_malloc0 (sizeof (MonoClass));
2205
2206         class->image = image;
2207         class->name_space = eclass->name_space;
2208         nsize = strlen (eclass->name);
2209         name = g_malloc (nsize + 2 + rank);
2210         memcpy (name, eclass->name, nsize);
2211         name [nsize] = '[';
2212         if (rank > 1)
2213                 memset (name + nsize + 1, ',', rank - 1);
2214         name [nsize + rank] = ']';
2215         name [nsize + rank + 1] = 0;
2216         class->name = name;
2217         class->type_token = 0;
2218         /* all arrays are marked serializable and sealed, bug #42779 */
2219         class->flags = TYPE_ATTRIBUTE_CLASS | TYPE_ATTRIBUTE_SERIALIZABLE | TYPE_ATTRIBUTE_SEALED |
2220                 (eclass->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK);
2221         class->parent = parent;
2222         class->instance_size = mono_class_instance_size (class->parent);
2223         class->class_size = 0;
2224         mono_class_setup_supertypes (class);
2225
2226         class->rank = rank;
2227         
2228         if (eclass->enumtype)
2229                 class->cast_class = eclass->element_class;
2230         else
2231                 class->cast_class = eclass;
2232
2233         class->element_class = eclass;
2234
2235         if ((rank > 1) || bounded) {
2236                 MonoArrayType *at = g_new0 (MonoArrayType, 1);
2237                 class->byval_arg.type = MONO_TYPE_ARRAY;
2238                 class->byval_arg.data.array = at;
2239                 at->eklass = eclass;
2240                 at->rank = rank;
2241                 /* FIXME: complete.... */
2242         } else {
2243                 class->byval_arg.type = MONO_TYPE_SZARRAY;
2244                 class->byval_arg.data.klass = eclass;
2245         }
2246         class->this_arg = class->byval_arg;
2247         class->this_arg.byref = 1;
2248         if (corlib_type) {
2249                 class->inited = 1;
2250         }
2251
2252         list = g_slist_append (rootlist, class);
2253         g_hash_table_insert (image->array_cache, eclass, list);
2254
2255         mono_loader_unlock ();
2256
2257         return class;
2258 }
2259
2260 /**
2261  * mono_array_class_get:
2262  * @element_class: element class 
2263  * @rank: the dimension of the array class
2264  *
2265  * Returns: a class object describing the array with element type @element_type and 
2266  * dimension @rank. 
2267  */
2268 MonoClass *
2269 mono_array_class_get (MonoClass *eclass, guint32 rank)
2270 {
2271         return mono_bounded_array_class_get (eclass, rank, FALSE);
2272 }
2273
2274 /**
2275  * mono_class_instance_size:
2276  * @klass: a class 
2277  * 
2278  * Returns: the size of an object instance
2279  */
2280 gint32
2281 mono_class_instance_size (MonoClass *klass)
2282 {       
2283         if (!klass->size_inited)
2284                 mono_class_init (klass);
2285
2286         return klass->instance_size;
2287 }
2288
2289 /**
2290  * mono_class_min_align:
2291  * @klass: a class 
2292  * 
2293  * Returns: minimm alignment requirements 
2294  */
2295 gint32
2296 mono_class_min_align (MonoClass *klass)
2297 {       
2298         if (!klass->size_inited)
2299                 mono_class_init (klass);
2300
2301         return klass->min_align;
2302 }
2303
2304 /**
2305  * mono_class_value_size:
2306  * @klass: a class 
2307  *
2308  * This function is used for value types, and return the
2309  * space and the alignment to store that kind of value object.
2310  *
2311  * Returns: the size of a value of kind @klass
2312  */
2313 gint32
2314 mono_class_value_size      (MonoClass *klass, guint32 *align)
2315 {
2316         gint32 size;
2317
2318         /* fixme: check disable, because we still have external revereces to
2319          * mscorlib and Dummy Objects 
2320          */
2321         /*g_assert (klass->valuetype);*/
2322
2323         size = mono_class_instance_size (klass) - sizeof (MonoObject);
2324
2325         if (align)
2326                 *align = klass->min_align;
2327
2328         return size;
2329 }
2330
2331 /**
2332  * mono_class_data_size:
2333  * @klass: a class 
2334  * 
2335  * Returns: the size of the static class data
2336  */
2337 gint32
2338 mono_class_data_size (MonoClass *klass)
2339 {       
2340         if (!klass->inited)
2341                 mono_class_init (klass);
2342
2343         return klass->class_size;
2344 }
2345
2346 /*
2347  * Auxiliary routine to mono_class_get_field
2348  *
2349  * Takes a field index instead of a field token.
2350  */
2351 static MonoClassField *
2352 mono_class_get_field_idx (MonoClass *class, int idx)
2353 {
2354         if (class->field.count){
2355                 if ((idx >= class->field.first) && (idx < class->field.last)){
2356                         return &class->fields [idx - class->field.first];
2357                 }
2358         }
2359
2360         if (!class->parent)
2361                 return NULL;
2362         
2363         return mono_class_get_field_idx (class->parent, idx);
2364 }
2365
2366 /**
2367  * mono_class_get_field:
2368  * @class: the class to lookup the field.
2369  * @field_token: the field token
2370  *
2371  * Returns: A MonoClassField representing the type and offset of
2372  * the field, or a NULL value if the field does not belong to this
2373  * class.
2374  */
2375 MonoClassField *
2376 mono_class_get_field (MonoClass *class, guint32 field_token)
2377 {
2378         int idx = mono_metadata_token_index (field_token);
2379
2380         g_assert (mono_metadata_token_code (field_token) == MONO_TOKEN_FIELD_DEF);
2381
2382         return mono_class_get_field_idx (class, idx - 1);
2383 }
2384
2385 MonoClassField *
2386 mono_class_get_field_from_name (MonoClass *klass, const char *name)
2387 {
2388         int i;
2389
2390         while (klass) {
2391                 for (i = 0; i < klass->field.count; ++i) {
2392                         if (strcmp (name, klass->fields [i].name) == 0)
2393                                 return &klass->fields [i];
2394                 }
2395                 klass = klass->parent;
2396         }
2397         return NULL;
2398 }
2399
2400 void *
2401 mono_vtable_get_static_field_data (MonoVTable *vt)
2402 {
2403         return vt->data;
2404 }
2405
2406 MonoProperty*
2407 mono_class_get_property_from_name (MonoClass *klass, const char *name)
2408 {
2409         int i;
2410
2411         while (klass) {
2412                 for (i = 0; i < klass->property.count; ++i) {
2413                         if (strcmp (name, klass->properties [i].name) == 0)
2414                                 return &klass->properties [i];
2415                 }
2416                 klass = klass->parent;
2417         }
2418         return NULL;
2419 }
2420
2421 /**
2422  * mono_class_get:
2423  * @image: the image where the class resides
2424  * @type_token: the token for the class
2425  * @at: an optional pointer to return the array element type
2426  *
2427  * Returns: the MonoClass that represents @type_token in @image
2428  */
2429 MonoClass *
2430 mono_class_get (MonoImage *image, guint32 type_token)
2431 {
2432         MonoClass *class = NULL;
2433
2434         if (image->dynamic)
2435                 return mono_lookup_dynamic_token (image, type_token);
2436
2437         switch (type_token & 0xff000000){
2438         case MONO_TOKEN_TYPE_DEF:
2439                 class = mono_class_create_from_typedef (image, type_token);
2440                 break;          
2441         case MONO_TOKEN_TYPE_REF:
2442                 class = mono_class_from_typeref (image, type_token);
2443                 break;
2444         case MONO_TOKEN_TYPE_SPEC:
2445                 class = mono_class_create_from_typespec (image, type_token, NULL);
2446                 break;
2447         default:
2448                 g_warning ("unknown token type %x", type_token & 0xff000000);
2449                 g_assert_not_reached ();
2450         }
2451
2452         if (!class)
2453                 g_warning ("Could not load class from token 0x%08x in %s", type_token, image->name);
2454
2455         return class;
2456 }
2457
2458 MonoClass *
2459 mono_class_get_full (MonoImage *image, guint32 type_token, MonoGenericContext *context)
2460 {
2461         MonoClass *class = mono_class_get (image, type_token);
2462         MonoType *inflated;
2463
2464         if (!class || !context)
2465                 return class;
2466
2467         switch (class->byval_arg.type) {
2468         case MONO_TYPE_GENERICINST:
2469                 if (!class->generic_inst->is_open)
2470                         return class;
2471                 break;
2472         case MONO_TYPE_VAR:
2473         case MONO_TYPE_MVAR:
2474                 break;
2475         default:
2476                 return class;
2477         }
2478
2479         inflated = inflate_generic_type (&class->byval_arg, context);
2480         if (!inflated)
2481                 return class;
2482
2483         return mono_class_from_mono_type (inflated);
2484 }
2485
2486 /**
2487  * mono_class_from_name_case:
2488  * @image: The MonoImage where the type is looked up in, or NULL for looking up in all loaded assemblies
2489  * @name_space: the type namespace
2490  * @name: the type short name.
2491  *
2492  * Obtains a MonoClass with a given namespace and a given name which
2493  * is located in the given MonoImage.   The namespace and name
2494  * lookups are case insensitive.
2495  *
2496  * You can also pass `NULL' to the image, and that will lookup for
2497  * a type with the given namespace and name in all of the loaded
2498  * assemblies: notice that since there might be a name clash in this
2499  * case, passing NULL is not encouraged if you need a precise type.
2500  *
2501  */
2502 MonoClass *
2503 mono_class_from_name_case (MonoImage *image, const char* name_space, const char *name)
2504 {
2505         MonoTableInfo  *t = &image->tables [MONO_TABLE_TYPEDEF];
2506         guint32 cols [MONO_TYPEDEF_SIZE];
2507         const char *n;
2508         const char *nspace;
2509         guint32 i, visib;
2510
2511         /* add a cache if needed */
2512         for (i = 1; i <= t->rows; ++i) {
2513                 mono_metadata_decode_row (t, i - 1, cols, MONO_TYPEDEF_SIZE);
2514                 /* nested types are accessed from the nesting name */
2515                 visib = cols [MONO_TYPEDEF_FLAGS] & TYPE_ATTRIBUTE_VISIBILITY_MASK;
2516                 if (visib > TYPE_ATTRIBUTE_PUBLIC && visib <= TYPE_ATTRIBUTE_NESTED_ASSEMBLY)
2517                         continue;
2518                 n = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
2519                 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
2520                 if (g_strcasecmp (n, name) == 0 && g_strcasecmp (nspace, name_space) == 0)
2521                         return mono_class_get (image, MONO_TOKEN_TYPE_DEF | i);
2522         }
2523         return NULL;
2524 }
2525
2526 static MonoClass*
2527 return_nested_in (MonoClass *class, char *nested) {
2528         MonoClass *found;
2529         char *s = strchr (nested, '/');
2530         GList *tmp;
2531
2532         if (s) {
2533                 *s = 0;
2534                 s++;
2535         }
2536         for (tmp = class->nested_classes; tmp; tmp = tmp->next) {
2537                 found = tmp->data;
2538                 if (strcmp (found->name, nested) == 0) {
2539                         if (s)
2540                                 return return_nested_in (found, s);
2541                         return found;
2542                 }
2543         }
2544         return NULL;
2545 }
2546
2547
2548 /**
2549  * mono_class_from_name_case:
2550  * @image: The MonoImage where the type is looked up in, or NULL for looking up in all loaded assemblies
2551  * @name_space: the type namespace
2552  * @name: the type short name.
2553  *
2554  * Obtains a MonoClass with a given namespace and a given name which
2555  * is located in the given MonoImage.   
2556  *
2557  * You can also pass `NULL' to the image, and that will lookup for
2558  * a type with the given namespace and name in all of the loaded
2559  * assemblies: notice that since there might be a name clash in this
2560  * case, passing NULL is not encouraged if you need a precise type.
2561  *
2562  */
2563 MonoClass *
2564 mono_class_from_name (MonoImage *image, const char* name_space, const char *name)
2565 {
2566         GHashTable *nspace_table;
2567         MonoImage *loaded_image;
2568         guint32 token = 0;
2569         MonoClass *class;
2570         char *nested;
2571         char buf [1024];
2572
2573         if ((nested = strchr (name, '/'))) {
2574                 int pos = nested - name;
2575                 int len = strlen (name);
2576                 if (len > 1023)
2577                         return NULL;
2578                 memcpy (buf, name, len + 1);
2579                 buf [pos] = 0;
2580                 nested = buf + pos + 1;
2581                 name = buf;
2582         }
2583
2584         mono_loader_lock ();
2585
2586         nspace_table = g_hash_table_lookup (image->name_cache, name_space);
2587
2588         if (nspace_table)
2589                 token = GPOINTER_TO_UINT (g_hash_table_lookup (nspace_table, name));
2590
2591         mono_loader_unlock ();
2592
2593         if (!token)
2594                 return NULL;
2595
2596         if (mono_metadata_token_table (token) == MONO_TABLE_EXPORTEDTYPE) {
2597                 MonoTableInfo  *t = &image->tables [MONO_TABLE_EXPORTEDTYPE];
2598                 guint32 cols [MONO_EXP_TYPE_SIZE];
2599                 guint32 idx, impl;
2600
2601                 idx = mono_metadata_token_index (token);
2602
2603                 mono_metadata_decode_row (t, idx - 1, cols, MONO_EXP_TYPE_SIZE);
2604
2605                 impl = cols [MONO_EXP_TYPE_IMPLEMENTATION];
2606                 if ((impl & MONO_IMPLEMENTATION_MASK) == MONO_IMPLEMENTATION_FILE) {
2607                         loaded_image = mono_assembly_load_module (image->assembly, impl >> MONO_IMPLEMENTATION_BITS);
2608                         if (!loaded_image)
2609                                 return NULL;
2610                         class = mono_class_from_name (loaded_image, name_space, name);
2611                         if (nested)
2612                                 return return_nested_in (class, nested);
2613                         return class;
2614                 } else {
2615                         g_error ("not yet implemented");
2616                 }
2617         }
2618
2619         token = MONO_TOKEN_TYPE_DEF | token;
2620
2621         class = mono_class_get (image, token);
2622         if (nested)
2623                 return return_nested_in (class, nested);
2624         return class;
2625 }
2626
2627 gboolean
2628 mono_class_is_subclass_of (MonoClass *klass, MonoClass *klassc, 
2629                            gboolean check_interfaces)
2630 {
2631  again:
2632         if (check_interfaces && MONO_CLASS_IS_INTERFACE (klassc) && !MONO_CLASS_IS_INTERFACE (klass)) {
2633                 if ((klassc->interface_id <= klass->max_interface_id) &&
2634                         (klass->interface_offsets [klassc->interface_id] >= 0))
2635                         return TRUE;
2636         } else if (check_interfaces && MONO_CLASS_IS_INTERFACE (klassc) && MONO_CLASS_IS_INTERFACE (klass)) {
2637                 int i;
2638
2639                 for (i = 0; i < klass->interface_count; i ++) {
2640                         MonoClass *ic =  klass->interfaces [i];
2641                         if (ic == klassc)
2642                                 return TRUE;
2643                 }
2644         } else {
2645                 if (!MONO_CLASS_IS_INTERFACE (klass) && mono_class_has_parent (klass, klassc))
2646                         return TRUE;
2647                 if (klass->generic_inst) {
2648                         MonoType *parent = klass->generic_inst->parent;
2649                         if (!parent)
2650                                 return FALSE;
2651
2652                         if (mono_metadata_type_equal (parent, &klassc->byval_arg))
2653                                 return TRUE;
2654                         klass = mono_class_from_mono_type (parent);
2655                         goto again;
2656                 }
2657         }
2658
2659         /* 
2660          * MS.NET thinks interfaces are a subclass of Object, so we think it as
2661          * well.
2662          */
2663         if (klassc == mono_defaults.object_class)
2664                 return TRUE;
2665         
2666         return FALSE;
2667 }
2668
2669 gboolean
2670 mono_class_is_assignable_from (MonoClass *klass, MonoClass *oklass)
2671 {
2672         if (!klass->inited)
2673                 mono_class_init (klass);
2674
2675         if (!oklass->inited)
2676                 mono_class_init (oklass);
2677
2678         if (MONO_CLASS_IS_INTERFACE (klass)) {
2679                 if ((klass->interface_id <= oklass->max_interface_id) &&
2680                     (oklass->interface_offsets [klass->interface_id] != -1))
2681                         return TRUE;
2682         } else
2683                 if (klass->rank) {
2684                         MonoClass *eclass, *eoclass;
2685
2686                         if (oklass->rank != klass->rank)
2687                                 return FALSE;
2688
2689                         /* vectors vs. one dimensional arrays */
2690                         if (oklass->byval_arg.type != klass->byval_arg.type)
2691                                 return FALSE;
2692
2693                         eclass = klass->cast_class;
2694                         eoclass = oklass->cast_class;
2695
2696
2697                         /* 
2698                          * a is b does not imply a[] is b[] when a is a valuetype, and
2699                          * b is a reference type.
2700                          */
2701
2702                         if (eoclass->valuetype) {
2703                                 if ((eclass == mono_defaults.enum_class) || 
2704                                         (eclass == mono_defaults.enum_class->parent) ||
2705                                         (eclass == mono_defaults.object_class))
2706                                         return FALSE;
2707                         }
2708
2709                         return mono_class_is_assignable_from (klass->cast_class, oklass->cast_class);
2710                 }
2711         else
2712                 if (klass == mono_defaults.object_class)
2713                         return TRUE;
2714
2715         return mono_class_has_parent (oklass, klass);
2716 }       
2717
2718 /*
2719  * mono_class_needs_cctor_run:
2720  *
2721  *  Determines whenever the class has a static constructor and whenever it
2722  * needs to be called when executing CALLER.
2723  */
2724 gboolean
2725 mono_class_needs_cctor_run (MonoClass *klass, MonoMethod *caller)
2726 {
2727         int i;
2728         MonoMethod *method;
2729         
2730         for (i = 0; i < klass->method.count; ++i) {
2731                 method = klass->methods [i];
2732                 if ((method->flags & METHOD_ATTRIBUTE_SPECIAL_NAME) && 
2733                     (strcmp (".cctor", method->name) == 0)) {
2734                         if (caller == method)
2735                                 return FALSE;
2736                         return TRUE;
2737                 }
2738         }
2739         return FALSE;
2740 }
2741
2742 /*
2743  * Returns the nnumber of bytes an element of type klass
2744  * uses when stored into an array.
2745  */
2746 gint32
2747 mono_class_array_element_size (MonoClass *klass)
2748 {
2749         int t = klass->byval_arg.type;
2750         
2751 handle_enum:
2752         switch (t) {
2753         case MONO_TYPE_I1:
2754         case MONO_TYPE_U1:
2755         case MONO_TYPE_BOOLEAN:
2756                 return 1;
2757         case MONO_TYPE_I2:
2758         case MONO_TYPE_U2:
2759         case MONO_TYPE_CHAR:
2760                 return 2;
2761         case MONO_TYPE_I4:
2762         case MONO_TYPE_U4:
2763         case MONO_TYPE_R4:
2764                 return 4;
2765         case MONO_TYPE_I:
2766         case MONO_TYPE_U:
2767         case MONO_TYPE_PTR:
2768         case MONO_TYPE_CLASS:
2769         case MONO_TYPE_STRING:
2770         case MONO_TYPE_OBJECT:
2771         case MONO_TYPE_SZARRAY:
2772         case MONO_TYPE_ARRAY: 
2773         case MONO_TYPE_VAR:
2774         case MONO_TYPE_MVAR:   
2775                 return sizeof (gpointer);
2776         case MONO_TYPE_I8:
2777         case MONO_TYPE_U8:
2778         case MONO_TYPE_R8:
2779                 return 8;
2780         case MONO_TYPE_VALUETYPE:
2781                 if (klass->enumtype) {
2782                         t = klass->enum_basetype->type;
2783                         goto handle_enum;
2784                 }
2785                 return mono_class_instance_size (klass) - sizeof (MonoObject);
2786         default:
2787                 g_error ("unknown type 0x%02x in mono_class_array_element_size", t);
2788         }
2789         return -1;
2790 }
2791
2792 /**
2793  * mono_array_element_size:
2794  * @ac: pointer to a #MonoArrayClass
2795  *
2796  * Returns: the size of single array element.
2797  */
2798 gint32
2799 mono_array_element_size (MonoClass *ac)
2800 {
2801         return mono_class_array_element_size (ac->element_class);
2802 }
2803
2804 gpointer
2805 mono_ldtoken (MonoImage *image, guint32 token, MonoClass **handle_class,
2806               MonoGenericContext *context)
2807 {
2808         if (image->dynamic) {
2809                 gpointer obj = mono_lookup_dynamic_token (image, token);
2810
2811                 switch (token & 0xff000000) {
2812                 case MONO_TOKEN_TYPE_DEF:
2813                 case MONO_TOKEN_TYPE_REF:
2814                 case MONO_TOKEN_TYPE_SPEC:
2815                         if (handle_class)
2816                                 *handle_class = mono_defaults.typehandle_class;
2817                         return &((MonoClass*)obj)->byval_arg;
2818                 case MONO_TOKEN_METHOD_DEF:
2819                         if (handle_class)
2820                                 *handle_class = mono_defaults.methodhandle_class;
2821                         return obj;
2822                 case MONO_TOKEN_FIELD_DEF:
2823                         if (handle_class)
2824                                 *handle_class = mono_defaults.fieldhandle_class;
2825                         return obj;
2826                 default:
2827                         g_assert_not_reached ();
2828                 }
2829         }
2830
2831         switch (token & 0xff000000) {
2832         case MONO_TOKEN_TYPE_DEF:
2833         case MONO_TOKEN_TYPE_REF: {
2834                 MonoClass *class;
2835                 if (handle_class)
2836                         *handle_class = mono_defaults.typehandle_class;
2837                 class = mono_class_get_full (image, token, context);
2838                 mono_class_init (class);
2839                 /* We return a MonoType* as handle */
2840                 return &class->byval_arg;
2841         }
2842         case MONO_TOKEN_TYPE_SPEC: {
2843                 MonoClass *class;
2844                 if (handle_class)
2845                         *handle_class = mono_defaults.typehandle_class;
2846                 class = mono_class_create_from_typespec (image, token, context);
2847                 mono_class_init (class);
2848                 return &class->byval_arg;
2849         }
2850         case MONO_TOKEN_FIELD_DEF: {
2851                 MonoClass *class;
2852                 guint32 type = mono_metadata_typedef_from_field (image, mono_metadata_token_index (token));
2853                 class = mono_class_get_full (image, MONO_TOKEN_TYPE_DEF | type, context);
2854                 mono_class_init (class);
2855                 if (handle_class)
2856                         *handle_class = mono_defaults.fieldhandle_class;
2857                 return mono_class_get_field (class, token);
2858         }
2859         case MONO_TOKEN_METHOD_DEF: {
2860                 MonoMethod *meth;
2861                 meth = mono_get_method_full (image, token, NULL, context);
2862                 if (handle_class)
2863                         *handle_class = mono_defaults.methodhandle_class;
2864                 return meth;
2865         }
2866         case MONO_TOKEN_MEMBER_REF: {
2867                 guint32 cols [MONO_MEMBERREF_SIZE];
2868                 const char *sig;
2869                 mono_metadata_decode_row (&image->tables [MONO_TABLE_MEMBERREF], mono_metadata_token_index (token) - 1, cols, MONO_MEMBERREF_SIZE);
2870                 sig = mono_metadata_blob_heap (image, cols [MONO_MEMBERREF_SIGNATURE]);
2871                 mono_metadata_decode_blob_size (sig, &sig);
2872                 if (*sig == 0x6) { /* it's a field */
2873                         MonoClass *klass;
2874                         MonoClassField *field;
2875                         field = mono_field_from_token (image, token, &klass, context);
2876                         if (handle_class)
2877                                 *handle_class = mono_defaults.fieldhandle_class;
2878                         return field;
2879                 } else {
2880                         MonoMethod *meth;
2881                         meth = mono_get_method_full (image, token, NULL, context);
2882                         if (handle_class)
2883                                 *handle_class = mono_defaults.methodhandle_class;
2884                         return meth;
2885                 }
2886         }
2887         default:
2888                 g_warning ("Unknown token 0x%08x in ldtoken", token);
2889                 break;
2890         }
2891         return NULL;
2892 }
2893
2894 /**
2895  * This function might need to call runtime functions so it can't be part
2896  * of the metadata library.
2897  */
2898 static MonoLookupDynamicToken lookup_dynamic = NULL;
2899
2900 void
2901 mono_install_lookup_dynamic_token (MonoLookupDynamicToken func)
2902 {
2903         lookup_dynamic = func;
2904 }
2905
2906 gpointer
2907 mono_lookup_dynamic_token (MonoImage *image, guint32 token)
2908 {
2909         return lookup_dynamic (image, token);
2910 }
2911
2912 MonoImage*
2913 mono_class_get_image (MonoClass *klass)
2914 {
2915         return klass->image;
2916 }
2917
2918 /**
2919  * mono_class_get_element_class:
2920  * @klass: the MonoClass to act on
2921  *
2922  * Returns the element class of an array or an enumeration.
2923  */
2924 MonoClass*
2925 mono_class_get_element_class (MonoClass *klass)
2926 {
2927         return klass->element_class;
2928 }
2929
2930 /**
2931  * mono_class_is_valuetype:
2932  * @klass: the MonoClass to act on
2933  *
2934  * Returns true if the MonoClass represents a ValueType.
2935  */
2936 gboolean
2937 mono_class_is_valuetype (MonoClass *klass)
2938 {
2939         return klass->valuetype;
2940 }
2941
2942 /**
2943  * mono_class_is_enum:
2944  * @klass: the MonoClass to act on
2945  *
2946  * Returns true if the MonoClass represents an enumeration.
2947  */
2948 gboolean
2949 mono_class_is_enum (MonoClass *klass)
2950 {
2951         return klass->enumtype;
2952 }
2953
2954 /**
2955  * mono_class_enum_basetype:
2956  * @klass: the MonoClass to act on
2957  *
2958  * Returns the underlying type representation for an enumeration.
2959  */
2960 MonoType*
2961 mono_class_enum_basetype (MonoClass *klass)
2962 {
2963         return klass->enum_basetype;
2964 }
2965
2966 /**
2967  * mono_class_get_parent
2968  * @klass: the MonoClass to act on
2969  *
2970  * Returns the parent class for this class.
2971  */
2972 MonoClass*
2973 mono_class_get_parent (MonoClass *klass)
2974 {
2975         return klass->parent;
2976 }
2977
2978 /**
2979  * mono_class_get_nesting_type;
2980  * @klass: the MonoClass to act on
2981  *
2982  * Returns the container type where this type is nested or NULL if this type is not a nested type.
2983  */
2984 MonoClass*
2985 mono_class_get_nesting_type (MonoClass *klass)
2986 {
2987         return klass->nested_in;
2988 }
2989
2990 /**
2991  * mono_class_get_rank:
2992  * @klass: the MonoClass to act on
2993  *
2994  * Returns the rank for the array (the number of dimensions).
2995  */
2996 int
2997 mono_class_get_rank (MonoClass *klass)
2998 {
2999         return klass->rank;
3000 }
3001
3002 /**
3003  * mono_class_get_flags:
3004  * @klass: the MonoClass to act on
3005  *
3006  * The type flags from the TypeDef table from the metadata.
3007  * see the TYPE_ATTRIBUTE_* definitions on tabledefs.h for the
3008  * different values.
3009  *
3010  * Returns the flags from the TypeDef table.
3011  */
3012 guint32
3013 mono_class_get_flags (MonoClass *klass)
3014 {
3015         return klass->flags;
3016 }
3017
3018 /**
3019  * mono_class_get_name
3020  * @klass: the MonoClass to act on
3021  *
3022  * Returns the name of the class.
3023  */
3024 const char*
3025 mono_class_get_name (MonoClass *klass)
3026 {
3027         return klass->name;
3028 }
3029
3030 /**
3031  * mono_class_get_namespace:
3032  * @klass: the MonoClass to act on
3033  *
3034  * Returns the namespace of the class.
3035  */
3036 const char*
3037 mono_class_get_namespace (MonoClass *klass)
3038 {
3039         return klass->name_space;
3040 }
3041
3042 /**
3043  * mono_class_get_type:
3044  * @klass: the MonoClass to act on
3045  *
3046  * This method returns the internal Type representation for the class.
3047  *
3048  * Returns the MonoType from the class.
3049  */
3050 MonoType*
3051 mono_class_get_type (MonoClass *klass)
3052 {
3053         return &klass->byval_arg;
3054 }
3055
3056 /**
3057  * mono_class_get_byref_type:
3058  * @klass: the MonoClass to act on
3059  *
3060  * 
3061  */
3062 MonoType*
3063 mono_class_get_byref_type (MonoClass *klass)
3064 {
3065         return &klass->this_arg;
3066 }
3067
3068 /**
3069  * mono_class_num_fields:
3070  * @klass: the MonoClass to act on
3071  *
3072  * Returns the number of static and instance fields in the class.
3073  */
3074 int
3075 mono_class_num_fields (MonoClass *klass)
3076 {
3077         return klass->field.count;
3078 }
3079
3080 /**
3081  * mono_class_num_methods:
3082  * @klass: the MonoClass to act on
3083  *
3084  * Returns the number of methods in the class.
3085  */
3086 int
3087 mono_class_num_methods (MonoClass *klass)
3088 {
3089         return klass->method.count;
3090 }
3091
3092 /**
3093  * mono_class_num_properties
3094  * @klass: the MonoClass to act on
3095  *
3096  * Returns the number of properties in the class.
3097  */
3098 int
3099 mono_class_num_properties (MonoClass *klass)
3100 {
3101         return klass->property.count;
3102 }
3103
3104 /**
3105  * mono_class_num_events:
3106  * @klass: the MonoClass to act on
3107  *
3108  * Returns the number of events in the class.
3109  */
3110 int
3111 mono_class_num_events (MonoClass *klass)
3112 {
3113         return klass->event.count;
3114 }
3115
3116 /**
3117  * mono_class_get_fields:
3118  * @klass: the MonoClass to act on
3119  *
3120  * This routine is an iterator routine for retrieving the fields in a class.
3121  *
3122  * You must pass a gpointer that points to zero and is treated as an opaque handle to
3123  * iterate over all of the elements.  When no more values are
3124  * available, the return value is NULL.
3125  *
3126  * Returns a `MonoClassField *' on each iteration, or NULL when no more fields are available.
3127  */
3128 MonoClassField*
3129 mono_class_get_fields (MonoClass* klass, gpointer *iter)
3130 {
3131         MonoClassField* field;
3132         if (!iter)
3133                 return NULL;
3134         if (!klass->inited)
3135                 mono_class_init (klass);
3136         if (!*iter) {
3137                 /* start from the first */
3138                 if (klass->field.count) {
3139                         return *iter = &klass->fields [0];
3140                 } else {
3141                         /* no fields */
3142                         return NULL;
3143                 }
3144         }
3145         field = *iter;
3146         field++;
3147         if (field < &klass->fields [klass->field.count]) {
3148                 return *iter = field;
3149         }
3150         return NULL;
3151 }
3152
3153 /**
3154  * mono_class_get_methods
3155  * @klass: the MonoClass to act on
3156  *
3157  * This routine is an iterator routine for retrieving the fields in a class.
3158  *
3159  * You must pass a gpointer that points to zero and is treated as an opaque handle to
3160  * iterate over all of the elements.  When no more values are
3161  * available, the return value is NULL.
3162  *
3163  * Returns a MonoMethod on each iteration or NULL when no more methods are available.
3164  */
3165 MonoMethod*
3166 mono_class_get_methods (MonoClass* klass, gpointer *iter)
3167 {
3168         MonoMethod** method;
3169         if (!iter)
3170                 return NULL;
3171         if (!klass->inited)
3172                 mono_class_init (klass);
3173         if (!*iter) {
3174                 /* start from the first */
3175                 if (klass->method.count) {
3176                         *iter = &klass->methods [0];
3177                         return klass->methods [0];
3178                 } else {
3179                         /* no method */
3180                         return NULL;
3181                 }
3182         }
3183         method = *iter;
3184         method++;
3185         if (method < &klass->methods [klass->method.count]) {
3186                 *iter = method;
3187                 return *method;
3188         }
3189         return NULL;
3190 }
3191
3192 /**
3193  * mono_class_get_properties:
3194  * @klass: the MonoClass to act on
3195  *
3196  * This routine is an iterator routine for retrieving the properties in a class.
3197  *
3198  * You must pass a gpointer that points to zero and is treated as an opaque handle to
3199  * iterate over all of the elements.  When no more values are
3200  * available, the return value is NULL.
3201  *
3202  * Returns a `MonoProperty *' on each invocation, or NULL when no more are available.
3203  */
3204 MonoProperty*
3205 mono_class_get_properties (MonoClass* klass, gpointer *iter)
3206 {
3207         MonoProperty* property;
3208         if (!iter)
3209                 return NULL;
3210         if (!klass->inited)
3211                 mono_class_init (klass);
3212         if (!*iter) {
3213                 /* start from the first */
3214                 if (klass->property.count) {
3215                         return *iter = &klass->properties [0];
3216                 } else {
3217                         /* no fields */
3218                         return NULL;
3219                 }
3220         }
3221         property = *iter;
3222         property++;
3223         if (property < &klass->properties [klass->property.count]) {
3224                 return *iter = property;
3225         }
3226         return NULL;
3227 }
3228
3229 /**
3230  * mono_class_get_events:
3231  * @klass: the MonoClass to act on
3232  *
3233  * This routine is an iterator routine for retrieving the properties in a class.
3234  *
3235  * You must pass a gpointer that points to zero and is treated as an opaque handle to
3236  * iterate over all of the elements.  When no more values are
3237  * available, the return value is NULL.
3238  *
3239  * Returns a `MonoEvent *' on each invocation, or NULL when no more are available.
3240  */
3241 MonoEvent*
3242 mono_class_get_events (MonoClass* klass, gpointer *iter)
3243 {
3244         MonoEvent* event;
3245         if (!iter)
3246                 return NULL;
3247         if (!klass->inited)
3248                 mono_class_init (klass);
3249         if (!*iter) {
3250                 /* start from the first */
3251                 if (klass->event.count) {
3252                         return *iter = &klass->events [0];
3253                 } else {
3254                         /* no fields */
3255                         return NULL;
3256                 }
3257         }
3258         event = *iter;
3259         event++;
3260         if (event < &klass->events [klass->event.count]) {
3261                 return *iter = event;
3262         }
3263         return NULL;
3264 }
3265
3266 /**
3267  * mono_class_get_interfaaces
3268  * @klass: the MonoClass to act on
3269  *
3270  * This routine is an iterator routine for retrieving the interfaces implemented by this class.
3271  *
3272  * You must pass a gpointer that points to zero and is treated as an opaque handle to
3273  * iterate over all of the elements.  When no more values are
3274  * available, the return value is NULL.
3275  *
3276  * Returns a `Monoclass *' on each invocation, or NULL when no more are available.
3277  */
3278 MonoClass*
3279 mono_class_get_interfaces (MonoClass* klass, gpointer *iter)
3280 {
3281         MonoClass** iface;
3282         if (!iter)
3283                 return NULL;
3284         if (!klass->inited)
3285                 mono_class_init (klass);
3286         if (!*iter) {
3287                 /* start from the first */
3288                 if (klass->interface_count) {
3289                         *iter = &klass->interfaces [0];
3290                         return klass->interfaces [0];
3291                 } else {
3292                         /* no interface */
3293                         return NULL;
3294                 }
3295         }
3296         iface = *iter;
3297         iface++;
3298         if (iface < &klass->interfaces [klass->interface_count]) {
3299                 *iter = iface;
3300                 return *iface;
3301         }
3302         return NULL;
3303 }
3304
3305 /**
3306  * mono_class_get_interfaaces
3307  * @klass: the MonoClass to act on
3308  *
3309  * This routine is an iterator routine for retrieving the nested types of a class.
3310  *
3311  * You must pass a gpointer that points to zero and is treated as an opaque handle to
3312  * iterate over all of the elements.  When no more values are
3313  * available, the return value is NULL.
3314  *
3315  * Returns a `Monoclass *' on each invocation, or NULL when no more are available.
3316  */
3317 MonoClass*
3318 mono_class_get_nested_types (MonoClass* klass, gpointer *iter)
3319 {
3320         GList *item;
3321         if (!iter)
3322                 return NULL;
3323         if (!klass->inited)
3324                 mono_class_init (klass);
3325         if (!*iter) {
3326                 /* start from the first */
3327                 if (klass->nested_classes) {
3328                         *iter = klass->nested_classes;
3329                         return klass->nested_classes->data;
3330                 } else {
3331                         /* no nested types */
3332                         return NULL;
3333                 }
3334         }
3335         item = *iter;
3336         item = item->next;
3337         if (item) {
3338                 *iter = item;
3339                 return item->data;
3340         }
3341         return NULL;
3342 }
3343
3344 /**
3345  * mono_field_get_name:
3346  * @field: the MonoClassField to act on
3347  *
3348  * Returns the name of the field.
3349  */
3350 const char*
3351 mono_field_get_name (MonoClassField *field)
3352 {
3353         return field->name;
3354 }
3355
3356 /**
3357  * mono_field_get_type:
3358  * @field: the MonoClassField to act on
3359  *
3360  * Returns MonoType of the field.
3361  */
3362 MonoType*
3363 mono_field_get_type (MonoClassField *field)
3364 {
3365         return field->type;
3366 }
3367
3368 /**
3369  * mono_field_get_type:
3370  * @field: the MonoClassField to act on
3371  *
3372  * Returns MonoClass where the field was defined.
3373  */
3374 MonoClass*
3375 mono_field_get_parent (MonoClassField *field)
3376 {
3377         return field->parent;
3378 }
3379
3380 /**
3381  * mono_field_get_flags;
3382  * @field: the MonoClassField to act on
3383  *
3384  * The metadata flags for a field are encoded using the
3385  * FIELD_ATTRIBUTE_* constants.  See the tabledefs.h file for details.
3386  *
3387  * Returns the flags for the field.
3388  */
3389 guint32
3390 mono_field_get_flags (MonoClassField *field)
3391 {
3392         return field->type->attrs;
3393 }
3394
3395 /**
3396  * mono_property_get_name: 
3397  * @prop: the MonoProperty to act on
3398  *
3399  * Returns the name of the property
3400  */
3401 const char*
3402 mono_property_get_name (MonoProperty *prop)
3403 {
3404         return prop->name;
3405 }
3406
3407 /**
3408  * mono_property_get_set_method
3409  * @prop: the MonoProperty to act on.
3410  *
3411  * Returns the setter method of the property (A MonoMethod)
3412  */
3413 MonoMethod*
3414 mono_property_get_set_method (MonoProperty *prop)
3415 {
3416         return prop->set;
3417 }
3418
3419 /**
3420  * mono_property_get_get_method
3421  * @prop: the MonoProperty to act on.
3422  *
3423  * Returns the setter method of the property (A MonoMethod)
3424  */
3425 MonoMethod*
3426 mono_property_get_get_method (MonoProperty *prop)
3427 {
3428         return prop->get;
3429 }
3430
3431 /**
3432  * mono_property_get_parent:
3433  * @prop: the MonoProperty to act on.
3434  *
3435  * Returns the MonoClass where the property was defined.
3436  */
3437 MonoClass*
3438 mono_property_get_parent (MonoProperty *prop)
3439 {
3440         return prop->parent;
3441 }
3442
3443 /**
3444  * mono_property_get_flags:
3445  * @prop: the MonoProperty to act on.
3446  *
3447  * The metadata flags for a property are encoded using the
3448  * PROPERTY_ATTRIBUTE_* constants.  See the tabledefs.h file for details.
3449  *
3450  * Returns the flags for the property.
3451  */
3452 guint32
3453 mono_property_get_flags (MonoProperty *prop)
3454 {
3455         return prop->attrs;
3456 }
3457
3458 /**
3459  * mono_event_get_name:
3460  * @event: the MonoEvent to act on
3461  *
3462  * Returns the name of the event.
3463  */
3464 const char*
3465 mono_event_get_name (MonoEvent *event)
3466 {
3467         return event->name;
3468 }
3469
3470 /**
3471  * mono_event_get_add_method:
3472  * @event: The MonoEvent to act on.
3473  *
3474  * Returns the `add' method for the event (a MonoMethod).
3475  */
3476 MonoMethod*
3477 mono_event_get_add_method (MonoEvent *event)
3478 {
3479         return event->add;
3480 }
3481
3482 /**
3483  * mono_event_get_remove_method:
3484  * @event: The MonoEvent to act on.
3485  *
3486  * Returns the `remove' method for the event (a MonoMethod).
3487  */
3488 MonoMethod*
3489 mono_event_get_remove_method (MonoEvent *event)
3490 {
3491         return event->remove;
3492 }
3493
3494 /**
3495  * mono_event_get_raise_method:
3496  * @event: The MonoEvent to act on.
3497  *
3498  * Returns the `raise' method for the event (a MonoMethod).
3499  */
3500 MonoMethod*
3501 mono_event_get_raise_method (MonoEvent *event)
3502 {
3503         return event->raise;
3504 }
3505
3506 /**
3507  * mono_event_get_parent:
3508  * @event: the MonoEvent to act on.
3509  *
3510  * Returns the MonoClass where the event is defined.
3511  */
3512 MonoClass*
3513 mono_event_get_parent (MonoEvent *event)
3514 {
3515         return event->parent;
3516 }
3517
3518 /**
3519  * mono_event_get_flags
3520  * @event: the MonoEvent to act on.
3521  *
3522  * The metadata flags for an event are encoded using the
3523  * EVENT_* constants.  See the tabledefs.h file for details.
3524  *
3525  * Returns the flags for the event.
3526  */
3527 guint32
3528 mono_event_get_flags (MonoEvent *event)
3529 {
3530         return event->attrs;
3531 }
3532