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