Wed Jun 25 18:12:29 CEST 2003 Paolo Molaro <lupus@ximian.com>
[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/cil-coff.h>
24 #include <mono/metadata/metadata.h>
25 #include <mono/metadata/tabledefs.h>
26 #include <mono/metadata/tokentype.h>
27 #include <mono/metadata/class.h>
28 #include <mono/metadata/object.h>
29 #include <mono/metadata/appdomain.h>
30 #include <mono/metadata/mono-endian.h>
31 #include <mono/metadata/debug-helpers.h>
32 #include <mono/metadata/reflection.h>
33 #include <mono/os/gc_wrapper.h>
34
35 /*
36  * Uncomment this to enable GC aware auto layout: in this mode, reference
37  * fields are grouped together inside objects, increasing collector 
38  * performance.
39  * Requires that all classes whose layout is known to the runtime be annotated
40  * with [StructLayout (LayoutKind.Sequential)]
41  */
42 //#define GC_AWARE_AUTO_LAYOUT
43
44 #define CSIZE(x) (sizeof (x) / 4)
45
46 MonoStats mono_stats;
47
48 gboolean mono_print_vtable = FALSE;
49
50 static MonoClass * mono_class_create_from_typedef (MonoImage *image, guint32 type_token);
51
52 void (*mono_debugger_class_init_func) (MonoClass *klass) = NULL;
53
54 MonoClass *
55 mono_class_from_typeref (MonoImage *image, guint32 type_token)
56 {
57         guint32 cols [MONO_TYPEREF_SIZE];
58         MonoTableInfo  *t = &image->tables [MONO_TABLE_TYPEREF];
59         guint32 idx;
60         const char *name, *nspace;
61         MonoClass *res;
62         MonoAssembly **references;
63         MonoImageOpenStatus status;
64
65         mono_metadata_decode_row (t, (type_token&0xffffff)-1, cols, MONO_TYPEREF_SIZE);
66
67         name = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAME]);
68         nspace = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAMESPACE]);
69         
70         idx = cols [MONO_TYPEREF_SCOPE] >> RESOLTION_SCOPE_BITS;
71         switch (cols [MONO_TYPEREF_SCOPE] & RESOLTION_SCOPE_MASK) {
72         case RESOLTION_SCOPE_MODULE:
73                 if (!idx)
74                         g_error ("null ResolutionScope not yet handled");
75                 /* a typedef in disguise */
76                 return mono_class_from_name (image, nspace, name);
77         case RESOLTION_SCOPE_MODULEREF:
78                 return mono_class_from_name (image->modules [idx - 1], nspace, name);
79         case RESOLTION_SCOPE_TYPEREF: {
80                 MonoClass *enclosing = mono_class_from_typeref (image, MONO_TOKEN_TYPE_REF | idx);
81                 GList *tmp;
82                 mono_class_init (enclosing);
83                 for (tmp = enclosing->nested_classes; tmp; tmp = tmp->next) {
84                         res = tmp->data;
85                         if (strcmp (res->name, name) == 0)
86                                 return res;
87                 }
88                 g_warning ("TypeRef ResolutionScope not yet handled (%d)", idx);
89                 return NULL;
90         }
91         case RESOLTION_SCOPE_ASSEMBLYREF:
92                 break;
93         }
94
95         mono_image_load_references (image, &status);
96         references = image->references;
97         if (!references ||  !references [idx-1]) {
98                 /* 
99                  * detected a reference to mscorlib, we simply return a reference to a dummy 
100                  * until we have a better solution.
101                  */
102                 fprintf(stderr, "Sending dummy where %s.%s expected\n", mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAMESPACE]), mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAME])); 
103                 
104                 res = mono_class_from_name (image, "System", "MonoDummy");
105                 /* prevent method loading */
106                 res->dummy = 1;
107                 /* some storage if the type is used  - very ugly hack */
108                 res->instance_size = 2*sizeof (gpointer);
109                 return res;
110         }       
111
112         /* load referenced assembly */
113         image = references [idx-1]->image;
114
115         return mono_class_from_name (image, nspace, name);
116 }
117
118 /** 
119  * class_compute_field_layout:
120  * @m: pointer to the metadata.
121  * @class: The class to initialize
122  *
123  * Initializes the class->fields.
124  *
125  * Currently we only support AUTO_LAYOUT, and do not even try to do
126  * a good job at it.  This is temporary to get the code for Paolo.
127  */
128 static void
129 class_compute_field_layout (MonoClass *class)
130 {
131         MonoImage *m = class->image; 
132         const int top = class->field.count;
133         guint32 layout = class->flags & TYPE_ATTRIBUTE_LAYOUT_MASK;
134         MonoTableInfo *t = &m->tables [MONO_TABLE_FIELD];
135         int i, blittable = TRUE, real_size = 0;
136         guint32 rva;
137         guint32 packing_size = 0;
138         gboolean explicit_size;
139
140         if (class->size_inited)
141                 return;
142
143         if (class->parent) {
144                 if (!class->parent->size_inited)
145                         class_compute_field_layout (class->parent);
146                 class->instance_size += class->parent->instance_size;
147                 class->min_align = class->parent->min_align;
148                 blittable = class->blittable;
149         } else {
150                 class->instance_size = sizeof (MonoObject);
151                 class->min_align = 1;
152         }
153
154         /* Get the real size */
155         explicit_size = mono_metadata_packing_from_typedef (class->image, class->type_token, &packing_size, &real_size);
156
157         if (explicit_size) {
158                 g_assert ((packing_size & 0xfffffff0) == 0);
159                 class->packing_size = packing_size;
160                 real_size += class->instance_size;
161         }
162
163         if (!top) {
164                 if (explicit_size && real_size) {
165                         class->instance_size = MAX (real_size, class->instance_size);
166                 }
167                 class->size_inited = 1;
168                 return;
169         }
170
171         class->fields = g_new0 (MonoClassField, top);
172
173         /*
174          * Fetch all the field information.
175          */
176         for (i = 0; i < top; i++){
177                 const char *sig;
178                 guint32 cols [MONO_FIELD_SIZE];
179                 guint32 constant_cols [MONO_CONSTANT_SIZE];
180                 guint32 cindex;
181                 int idx = class->field.first + i;
182                 
183                 mono_metadata_decode_row (t, idx, cols, CSIZE (cols));
184                 /* The name is needed for fieldrefs */
185                 class->fields [i].name = mono_metadata_string_heap (m, cols [MONO_FIELD_NAME]);
186                 sig = mono_metadata_blob_heap (m, cols [MONO_FIELD_SIGNATURE]);
187                 mono_metadata_decode_value (sig, &sig);
188                 /* FIELD signature == 0x06 */
189                 g_assert (*sig == 0x06);
190                 class->fields [i].type = mono_metadata_parse_field_type (
191                         m, cols [MONO_FIELD_FLAGS], sig + 1, &sig);
192
193                 class->fields [i].parent = class;
194
195                 if (!(class->fields [i].type->attrs & FIELD_ATTRIBUTE_STATIC)) {
196                         if (class->fields [i].type->byref) {
197                                 blittable = FALSE;
198                         } else {
199                                 MonoClass *field_class = mono_class_from_mono_type (class->fields [i].type);
200                                 if (!field_class || !field_class->blittable)
201                                         blittable = FALSE;
202                         }
203                 }
204                 if (layout == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT) {
205                         mono_metadata_field_info (m, idx, &class->fields [i].offset, NULL, NULL);
206                         if (class->fields [i].offset == (guint32)-1)
207                                 g_warning ("%s not initialized correctly (missing field layout info for %s)", class->name, class->fields [i].name);
208                 }
209
210                 if (cols [MONO_FIELD_FLAGS] & FIELD_ATTRIBUTE_HAS_FIELD_RVA) {
211                         mono_metadata_field_info (m, idx, NULL, &rva, NULL);
212                         if (!rva)
213                                 g_warning ("field %s in %s should have RVA data, but hasn't", class->fields [i].name, class->name);
214                         class->fields [i].data = mono_cli_rva_map (class->image->image_info, rva);
215                 }
216
217                 if (class->enumtype && !(cols [MONO_FIELD_FLAGS] & FIELD_ATTRIBUTE_STATIC)) {
218                         class->enum_basetype = class->fields [i].type;
219                         class->cast_class = class->element_class = mono_class_from_mono_type (class->enum_basetype);
220                         blittable = class->element_class->blittable;
221                 }
222
223                 if ((class->fields [i].type->attrs & FIELD_ATTRIBUTE_HAS_DEFAULT) &&
224                         (class->fields [i].type->attrs & FIELD_ATTRIBUTE_STATIC)) {
225                         cindex = mono_metadata_get_constant_index (class->image, MONO_TOKEN_FIELD_DEF | (class->field.first + i + 1));
226                         if (!cindex) {
227                                 g_warning ("constant for field %s:%s not found", class->name, class->fields [i].name);
228                                 continue;
229                         }
230                         mono_metadata_decode_row (&class->image->tables [MONO_TABLE_CONSTANT], cindex - 1, constant_cols, MONO_CONSTANT_SIZE);
231                         class->fields [i].def_value = g_new0 (MonoConstant, 1);
232                         class->fields [i].def_value->type = constant_cols [MONO_CONSTANT_TYPE];
233                         class->fields [i].def_value->value = (gpointer)mono_metadata_blob_heap (class->image, constant_cols [MONO_CONSTANT_VALUE]);
234                 }
235         }
236
237         if (class == mono_defaults.string_class)
238                 blittable = FALSE;
239
240         class->blittable = blittable;
241
242         if (class->enumtype && !class->enum_basetype) {
243                 if (!((strcmp (class->name, "Enum") == 0) && (strcmp (class->name_space, "System") == 0)))
244                         G_BREAKPOINT ();
245         }
246         if (explicit_size && real_size) {
247                 class->instance_size = MAX (real_size, class->instance_size);
248         }
249
250         mono_class_layout_fields (class);
251 }
252
253 void
254 mono_class_layout_fields (MonoClass *class)
255 {
256         int i;
257         const int top = class->field.count;
258         guint32 layout = class->flags & TYPE_ATTRIBUTE_LAYOUT_MASK;
259         guint32 pass, passes, real_size;
260
261         /*
262          * Compute field layout and total size (not considering static fields)
263          */
264
265         switch (layout) {
266         case TYPE_ATTRIBUTE_AUTO_LAYOUT:
267         case TYPE_ATTRIBUTE_SEQUENTIAL_LAYOUT:
268
269 #ifdef GC_AWARE_AUTO_LAYOUT
270                 passes = 2;
271 #else
272                 passes = 1;
273 #endif
274                 if (layout != TYPE_ATTRIBUTE_AUTO_LAYOUT)
275                         passes = 1;
276
277                 for (pass = 0; pass < passes; ++pass) {
278                         if (class->parent)
279                                 real_size = class->parent->instance_size;
280                         else
281                                 real_size = sizeof (MonoObject);
282
283                         for (i = 0; i < top; i++){
284                                 int size, align;
285
286                                 if (class->fields [i].type->attrs & FIELD_ATTRIBUTE_STATIC)
287                                         continue;
288
289 #ifdef GC_AWARE_AUTO_LAYOUT
290                                 /* FIXME: Fix mono_marshal_load_type_info () too */
291                                 if (layout == TYPE_ATTRIBUTE_AUTO_LAYOUT) {
292                                         /* 
293                                          * We process fields with reference type in the first pass,
294                                          * and fields with non-reference type in the second pass.
295                                          * We use IS_POINTER instead of IS_REFERENCE because in
296                                          * some internal structures, we store GC_MALLOCed memory
297                                          * in IntPtr fields...
298                                          */
299                                         if (MONO_TYPE_IS_POINTER (class->fields [i].type)) {
300                                                 if (pass == 1)
301                                                         continue;
302                                         } else {
303                                                 if (pass == 0)
304                                                         continue;
305                                         }
306                                 }
307 #endif
308
309                                 size = mono_type_size (class->fields [i].type, &align);
310                         
311                                 /* FIXME (LAMESPEC): should we also change the min alignment according to pack? */
312                                 align = class->packing_size ? MIN (class->packing_size, align): align;
313                                 class->min_align = MAX (align, class->min_align);
314                                 class->fields [i].offset = real_size;
315                                 class->fields [i].offset += align - 1;
316                                 class->fields [i].offset &= ~(align - 1);
317                                 real_size = class->fields [i].offset + size;
318                         }
319
320                         class->instance_size = MAX (real_size, class->instance_size);
321        
322                         if (class->instance_size & (class->min_align - 1)) {
323                                 class->instance_size += class->min_align - 1;
324                                 class->instance_size &= ~(class->min_align - 1);
325                         }
326                 }
327                 break;
328         case TYPE_ATTRIBUTE_EXPLICIT_LAYOUT:
329                 for (i = 0; i < top; i++) {
330                         int size, align;
331
332                         /*
333                          * There must be info about all the fields in a type if it
334                          * uses explicit layout.
335                          */
336
337                         if (class->fields [i].type->attrs & FIELD_ATTRIBUTE_STATIC)
338                                 continue;
339
340                         size = mono_type_size (class->fields [i].type, &align);
341                         
342                         /*
343                          * When we get here, class->fields [i].offset is already set by the
344                          * loader (for either runtime fields or fields loaded from metadata).
345                          * The offset is from the start of the object: this works for both
346                          * classes and valuetypes.
347                          */
348                         class->fields [i].offset += sizeof (MonoObject);
349
350                         /*
351                          * Calc max size.
352                          */
353                         real_size = MAX (real_size, size + class->fields [i].offset);
354                 }
355                 class->instance_size = MAX (real_size, class->instance_size);
356                 break;
357         }
358
359         class->size_inited = 1;
360
361         /*
362          * Compute static field layout and size
363          */
364         for (i = 0; i < top; i++){
365                 int size, align;
366                         
367                 if (!(class->fields [i].type->attrs & FIELD_ATTRIBUTE_STATIC))
368                         continue;
369                         
370                 size = mono_type_size (class->fields [i].type, &align);
371                 class->fields [i].offset = class->class_size;
372                 class->fields [i].offset += align - 1;
373                 class->fields [i].offset &= ~(align - 1);
374                 class->class_size = class->fields [i].offset + size;
375         }
376 }
377
378 static void
379 init_properties (MonoClass *class)
380 {
381         guint startm, endm, i, j;
382         guint32 cols [MONO_PROPERTY_SIZE];
383         MonoTableInfo *pt = &class->image->tables [MONO_TABLE_PROPERTY];
384         MonoTableInfo *msemt = &class->image->tables [MONO_TABLE_METHODSEMANTICS];
385
386         class->property.first = mono_metadata_properties_from_typedef (class->image, mono_metadata_token_index (class->type_token) - 1, &class->property.last);
387         class->property.count = class->property.last - class->property.first;
388
389         class->properties = g_new0 (MonoProperty, class->property.count);
390         for (i = class->property.first; i < class->property.last; ++i) {
391                 mono_metadata_decode_row (pt, i, cols, MONO_PROPERTY_SIZE);
392                 class->properties [i - class->property.first].attrs = cols [MONO_PROPERTY_FLAGS];
393                 class->properties [i - class->property.first].name = mono_metadata_string_heap (class->image, cols [MONO_PROPERTY_NAME]);
394
395                 startm = mono_metadata_methods_from_property (class->image, i, &endm);
396                 for (j = startm; j < endm; ++j) {
397                         mono_metadata_decode_row (msemt, j, cols, MONO_METHOD_SEMA_SIZE);
398                         switch (cols [MONO_METHOD_SEMA_SEMANTICS]) {
399                         case METHOD_SEMANTIC_SETTER:
400                                 class->properties [i - class->property.first].set = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
401                                 break;
402                         case METHOD_SEMANTIC_GETTER:
403                                 class->properties [i - class->property.first].get = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
404                                 break;
405                         default:
406                                 break;
407                         }
408                 }
409         }
410 }
411
412 static void
413 init_events (MonoClass *class)
414 {
415         guint startm, endm, i, j;
416         guint32 cols [MONO_EVENT_SIZE];
417         MonoTableInfo *pt = &class->image->tables [MONO_TABLE_EVENT];
418         MonoTableInfo *msemt = &class->image->tables [MONO_TABLE_METHODSEMANTICS];
419
420         class->event.first = mono_metadata_events_from_typedef (class->image, mono_metadata_token_index (class->type_token) - 1, &class->event.last);
421         class->event.count = class->event.last - class->event.first;
422
423         class->events = g_new0 (MonoEvent, class->event.count);
424         for (i = class->event.first; i < class->event.last; ++i) {
425                 mono_metadata_decode_row (pt, i, cols, MONO_EVENT_SIZE);
426                 class->events [i - class->event.first].attrs = cols [MONO_EVENT_FLAGS];
427                 class->events [i - class->event.first].name = mono_metadata_string_heap (class->image, cols [MONO_EVENT_NAME]);
428
429                 startm = mono_metadata_methods_from_event (class->image, i, &endm);
430                 for (j = startm; j < endm; ++j) {
431                         mono_metadata_decode_row (msemt, j, cols, MONO_METHOD_SEMA_SIZE);
432                         switch (cols [MONO_METHOD_SEMA_SEMANTICS]) {
433                         case METHOD_SEMANTIC_ADD_ON:
434                                 class->events [i - class->event.first].add = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
435                                 break;
436                         case METHOD_SEMANTIC_REMOVE_ON:
437                                 class->events [i - class->event.first].remove = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
438                                 break;
439                         case METHOD_SEMANTIC_FIRE:
440                                 class->events [i - class->event.first].raise = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
441                                 break;
442                         case METHOD_SEMANTIC_OTHER: /* don't care for now */
443                         default:
444                                 break;
445                         }
446                 }
447         }
448 }
449
450 static guint
451 mono_get_unique_iid (MonoClass *class)
452 {
453         static GHashTable *iid_hash = NULL;
454         static guint iid = 0;
455
456         char *str;
457         gpointer value;
458         
459         g_assert (class->flags & TYPE_ATTRIBUTE_INTERFACE);
460
461         if (!iid_hash)
462                 iid_hash = g_hash_table_new (g_str_hash, g_str_equal);
463
464         str = g_strdup_printf ("%s|%s.%s\n", class->image->name, class->name_space, class->name);
465
466         if (g_hash_table_lookup_extended (iid_hash, str, NULL, &value)) {
467                 g_free (str);
468                 return (guint)value;
469         } else {
470                 g_hash_table_insert (iid_hash, str, (gpointer)iid);
471                 ++iid;
472         }
473
474         return iid - 1;
475 }
476
477 static void
478 collect_implemented_interfaces_aux (MonoClass *klass, GPtrArray *res)
479 {
480         int i;
481         MonoClass *ic;
482         
483         for (i = 0; i < klass->interface_count; i++) {
484                 ic = klass->interfaces [i];
485
486                 g_ptr_array_add (res, ic);
487
488                 collect_implemented_interfaces_aux (ic, res);
489         }
490 }
491
492 static GPtrArray*
493 collect_implemented_interfaces (MonoClass *klass)
494 {
495         GPtrArray *res = g_ptr_array_new ();
496
497         collect_implemented_interfaces_aux (klass, res);
498         return res;
499 }
500
501 static int
502 setup_interface_offsets (MonoClass *class, int cur_slot)
503 {
504         MonoClass *k, *ic;
505         int i, max_iid;
506         GPtrArray *ifaces;
507
508         /* compute maximum number of slots and maximum interface id */
509         max_iid = 0;
510         for (k = class; k ; k = k->parent) {
511                 for (i = 0; i < k->interface_count; i++) {
512                         ic = k->interfaces [i];
513
514                         if (!ic->inited)
515                                 mono_class_init (ic);
516
517                         if (max_iid < ic->interface_id)
518                                 max_iid = ic->interface_id;
519                 }
520         }
521
522         if (class->flags & TYPE_ATTRIBUTE_INTERFACE) {
523                 if (max_iid < class->interface_id)
524                         max_iid = class->interface_id;
525         }
526         class->max_interface_id = max_iid;
527         /* compute vtable offset for interfaces */
528         class->interface_offsets = g_malloc (sizeof (gpointer) * (max_iid + 1));
529
530         for (i = 0; i <= max_iid; i++)
531                 class->interface_offsets [i] = -1;
532
533         ifaces = collect_implemented_interfaces (class);
534         for (i = 0; i < ifaces->len; ++i) {
535                 ic = g_ptr_array_index (ifaces, i);
536                 class->interface_offsets [ic->interface_id] = cur_slot;
537                 cur_slot += ic->method.count;
538         }
539         g_ptr_array_free (ifaces, TRUE);
540
541         for (k = class->parent; k ; k = k->parent) {
542                 ifaces = collect_implemented_interfaces (k);
543                 for (i = 0; i < ifaces->len; ++i) {
544                         ic = g_ptr_array_index (ifaces, i);
545
546                         if (class->interface_offsets [ic->interface_id] == -1) {
547                                 int io = k->interface_offsets [ic->interface_id];
548
549                                 g_assert (io >= 0);
550
551                                 class->interface_offsets [ic->interface_id] = io;
552                         }
553                 }
554                 g_ptr_array_free (ifaces, TRUE);
555         }
556         return cur_slot;
557 }
558
559 void
560 mono_class_setup_vtable (MonoClass *class, MonoMethod **overrides, int onum)
561 {
562         MonoClass *k, *ic;
563         MonoMethod **vtable;
564         int i, max_vtsize = 0, max_iid, cur_slot = 0;
565         GPtrArray *ifaces;
566         MonoGHashTable *override_map;
567
568         /* setup_vtable() must be called only once on the type */
569         if (class->interface_offsets) {
570                 g_warning ("vtable already computed in %s.%s", class->name_space, class->name);
571                 return;
572         }
573
574         ifaces = collect_implemented_interfaces (class);
575         for (i = 0; i < ifaces->len; i++) {
576                 MonoClass *ic = g_ptr_array_index (ifaces, i);
577                 max_vtsize += ic->method.count;
578         }
579         g_ptr_array_free (ifaces, TRUE);
580         
581         if (class->parent) {
582                 max_vtsize += class->parent->vtable_size;
583                 cur_slot = class->parent->vtable_size;
584         }
585
586         max_vtsize += class->method.count;
587
588         vtable = alloca (sizeof (gpointer) * max_vtsize);
589         memset (vtable, 0, sizeof (gpointer) * max_vtsize);
590
591         /* printf ("METAINIT %s.%s\n", class->name_space, class->name); */
592
593         cur_slot = setup_interface_offsets (class, cur_slot);
594         max_iid = class->max_interface_id;
595
596         if (class->parent && class->parent->vtable_size)
597                 memcpy (vtable, class->parent->vtable,  sizeof (gpointer) * class->parent->vtable_size);
598
599         override_map = mono_g_hash_table_new (NULL, NULL);
600
601         /* override interface methods */
602         for (i = 0; i < onum; i++) {
603                 MonoMethod *decl = overrides [i*2];
604                 if (decl->klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
605                         int dslot;
606                         g_assert (decl->slot != -1);
607                         dslot = decl->slot + class->interface_offsets [decl->klass->interface_id];
608                         vtable [dslot] = overrides [i*2 + 1];
609                         vtable [dslot]->slot = dslot;
610                         mono_g_hash_table_insert (override_map, overrides [i * 2], overrides [i * 2 + 1]);
611                 }
612         }
613
614         for (k = class; k ; k = k->parent) {
615                 ifaces = collect_implemented_interfaces (k);
616                 for (i = 0; i < ifaces->len; i++) {
617                         int j, l, io;
618
619                         ic = g_ptr_array_index (ifaces, i);
620                         io = k->interface_offsets [ic->interface_id];
621
622                         g_assert (io >= 0);
623                         g_assert (io <= max_vtsize);
624
625                         if (k == class) {
626                                 for (l = 0; l < ic->method.count; l++) {
627                                         MonoMethod *im = ic->methods [l];                                               
628
629                                         if (vtable [io + l] && !(vtable [io + l]->flags & METHOD_ATTRIBUTE_ABSTRACT))
630                                                 continue;
631
632                                         for (j = 0; j < class->method.count; ++j) {
633                                                 MonoMethod *cm = class->methods [j];
634                                                 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
635                                                     !((cm->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == METHOD_ATTRIBUTE_PUBLIC) ||
636                                                     !(cm->flags & METHOD_ATTRIBUTE_NEW_SLOT))
637                                                         continue;
638                                                 if (!strcmp(cm->name, im->name) && 
639                                                     mono_metadata_signature_equal (cm->signature, im->signature)) {
640                                                         g_assert (io + l <= max_vtsize);
641                                                         vtable [io + l] = cm;
642                                                 }
643                                         }
644                                 }
645                         } else {
646                                 /* already implemented */
647                                 if (io >= k->vtable_size)
648                                         continue;
649                         }
650                                 
651                         for (l = 0; l < ic->method.count; l++) {
652                                 MonoMethod *im = ic->methods [l];                                               
653                                 MonoClass *k1;
654
655                                 g_assert (io + l <= max_vtsize);
656
657                                 if (vtable [io + l] && !(vtable [io + l]->flags & METHOD_ATTRIBUTE_ABSTRACT))
658                                         continue;
659                                         
660                                 for (k1 = class; k1; k1 = k1->parent) {
661                                         for (j = 0; j < k1->method.count; ++j) {
662                                                 MonoMethod *cm = k1->methods [j];
663
664                                                 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
665                                                     !(cm->flags & METHOD_ATTRIBUTE_PUBLIC))
666                                                         continue;
667                                                 
668                                                 if (!strcmp(cm->name, im->name) && 
669                                                     mono_metadata_signature_equal (cm->signature, im->signature)) {
670                                                         g_assert (io + l <= max_vtsize);
671                                                         vtable [io + l] = cm;
672                                                         break;
673                                                 }
674                                                 
675                                         }
676                                         g_assert (io + l <= max_vtsize);
677                                         if (vtable [io + l] && !(vtable [io + l]->flags & METHOD_ATTRIBUTE_ABSTRACT))
678                                                 break;
679                                 }
680                         }
681
682                         for (l = 0; l < ic->method.count; l++) {
683                                 MonoMethod *im = ic->methods [l];                                               
684                                 char *qname, *fqname;
685                                 MonoClass *k1;
686                                 
687                                 if (vtable [io + l])
688                                         continue;
689                                         
690                                 qname = g_strconcat (ic->name, ".", im->name, NULL); 
691                                 if (ic->name_space && ic->name_space [0])
692                                         fqname = g_strconcat (ic->name_space, ".", ic->name, ".", im->name, NULL);
693                                 else
694                                         fqname = NULL;
695
696                                 for (k1 = class; k1; k1 = k1->parent) {
697                                         for (j = 0; j < k1->method.count; ++j) {
698                                                 MonoMethod *cm = k1->methods [j];
699
700                                                 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL))
701                                                         continue;
702                                         
703                                                 if (((fqname && !strcmp (cm->name, fqname)) || !strcmp (cm->name, qname)) &&
704                                                     mono_metadata_signature_equal (cm->signature, im->signature)) {
705                                                         g_assert (io + l <= max_vtsize);
706                                                         vtable [io + l] = cm;
707                                                         break;
708                                                 }
709                                         }
710                                 }
711                                 g_free (qname);
712                                 g_free (fqname);
713                         }
714
715                         
716                         if (!(class->flags & TYPE_ATTRIBUTE_ABSTRACT)) {
717                                 for (l = 0; l < ic->method.count; l++) {
718                                         char *msig;
719                                         MonoMethod *im = ic->methods [l];
720                                         if (im->flags & METHOD_ATTRIBUTE_STATIC)
721                                                         continue;
722                                         g_assert (io + l <= max_vtsize);
723                                         if (!(vtable [io + l])) {
724                                                 for (j = 0; j < onum; ++j) {
725                                                         g_print (" at slot %d: %s (%d) overrides %s (%d)\n", io+l, overrides [j*2+1]->name, 
726                                                                  overrides [j*2+1]->slot, overrides [j*2]->name, overrides [j*2]->slot);
727                                                 }
728                                                 msig = mono_signature_get_desc (im->signature, FALSE);
729                                                 printf ("no implementation for interface method %s.%s::%s(%s) in class %s.%s\n",
730                                                         ic->name_space, ic->name, im->name, msig, class->name_space, class->name);
731                                                 g_free (msig);
732                                                 for (j = 0; j < class->method.count; ++j) {
733                                                         MonoMethod *cm = class->methods [j];
734                                                         msig = mono_signature_get_desc (cm->signature, FALSE);
735                                                         
736                                                         printf ("METHOD %s(%s)\n", cm->name, msig);
737                                                         g_free (msig);
738                                                 }
739                                                 g_assert_not_reached ();
740                                         }
741                                 }
742                         }
743                 
744                         for (l = 0; l < ic->method.count; l++) {
745                                 MonoMethod *im = vtable [io + l];
746
747                                 if (im) {
748                                         g_assert (io + l <= max_vtsize);
749                                         if (im->slot < 0) {
750                                                 /* FIXME: why do we need this ? */
751                                                 im->slot = io + l;
752                                                 /* g_assert_not_reached (); */
753                                         }
754                                 }
755                         }
756                 }
757                 g_ptr_array_free (ifaces, TRUE);
758         } 
759
760         for (i = 0; i < class->method.count; ++i) {
761                 MonoMethod *cm;
762                
763                 cm = class->methods [i];
764
765                 if (!(cm->flags & METHOD_ATTRIBUTE_NEW_SLOT) && (cm->flags & METHOD_ATTRIBUTE_VIRTUAL)) {
766                         int slot = -1;
767                         for (k = class->parent; k ; k = k->parent) {
768                                 int j;
769                                 for (j = 0; j < k->method.count; ++j) {
770                                         MonoMethod *m1 = k->methods [j];
771                                         if (!(m1->flags & METHOD_ATTRIBUTE_VIRTUAL))
772                                                 continue;
773                                         if (!strcmp(cm->name, m1->name) && 
774                                             mono_metadata_signature_equal (cm->signature, m1->signature)) {
775                                                 slot = k->methods [j]->slot;
776                                                 g_assert (cm->slot < max_vtsize);
777                                                 mono_g_hash_table_insert (override_map, m1, cm);
778                                                 break;
779                                         }
780                                 }
781                                 if (slot >= 0) 
782                                         break;
783                         }
784                         if (slot >= 0)
785                                 cm->slot = slot;
786                 }
787
788                 if (cm->slot < 0)
789                         cm->slot = cur_slot++;
790
791                 if (!(cm->flags & METHOD_ATTRIBUTE_ABSTRACT))
792                         vtable [cm->slot] = cm;
793         }
794
795         /* override non interface methods */
796         for (i = 0; i < onum; i++) {
797                 MonoMethod *decl = overrides [i*2];
798                 if (!(decl->klass->flags & TYPE_ATTRIBUTE_INTERFACE)) {
799                         g_assert (decl->slot != -1);
800                         vtable [decl->slot] = overrides [i*2 + 1];
801                         overrides [i * 2 + 1]->slot = decl->slot;
802                         mono_g_hash_table_insert (override_map, decl, overrides [i * 2 + 1]);
803                 }
804         }
805
806         /*
807          * If a method occupies more than one place in the vtable, and it is
808          * overriden, then change the other occurances too.
809          */
810         for (i = 0; i < max_vtsize; ++i)
811                 if (vtable [i]) {
812                         MonoMethod *cm = mono_g_hash_table_lookup (override_map, vtable [i]);
813                         if (cm)
814                                 vtable [i] = cm;
815                 }
816         mono_g_hash_table_destroy (override_map);
817
818        
819         class->vtable_size = cur_slot;
820         class->vtable = g_malloc0 (sizeof (gpointer) * class->vtable_size);
821         memcpy (class->vtable, vtable,  sizeof (gpointer) * class->vtable_size);
822
823         if (mono_print_vtable) {
824                 int icount = 0;
825
826                 for (i = 0; i <= max_iid; i++)
827                         if (class->interface_offsets [i] != -1)
828                                 icount++;
829
830                 printf ("VTable %s.%s (size = %d, interfaces = %d)\n", class->name_space, 
831                         class->name, class->vtable_size, icount); 
832
833                 for (i = 0; i < class->vtable_size; ++i) {
834                         MonoMethod *cm;
835                
836                         cm = vtable [i];
837                         if (cm) {
838                                 printf ("  slot %03d(%03d) %s.%s:%s\n", i, cm->slot,
839                                         cm->klass->name_space, cm->klass->name,
840                                         cm->name);
841                         }
842                 }
843
844
845                 if (icount) {
846                         printf ("Interfaces %s.%s (max_iid = %d)\n", class->name_space, 
847                                 class->name, max_iid);
848         
849                         for (i = 0; i < class->interface_count; i++) {
850                                 ic = class->interfaces [i];
851                                 printf ("  slot %03d(%03d) %s.%s\n",  
852                                         class->interface_offsets [ic->interface_id],
853                                         ic->method.count, ic->name_space, ic->name);
854                         }
855
856                         for (k = class->parent; k ; k = k->parent) {
857                                 for (i = 0; i < k->interface_count; i++) {
858                                         ic = k->interfaces [i]; 
859                                         printf ("  slot %03d(%03d) %s.%s\n", 
860                                                 class->interface_offsets [ic->interface_id],
861                                                 ic->method.count, ic->name_space, ic->name);
862                                 }
863                         }
864                 }
865         }
866 }
867
868 /**
869  * mono_class_init:
870  * @class: the class to initialize
871  *
872  * compute the instance_size, class_size and other infos that cannot be 
873  * computed at mono_class_get() time. Also compute a generic vtable and 
874  * the method slot numbers. We use this infos later to create a domain
875  * specific vtable.  
876  */
877 void
878 mono_class_init (MonoClass *class)
879 {
880         int i;
881         static MonoMethod *default_ghc = NULL;
882         static MonoMethod *default_finalize = NULL;
883         static int finalize_slot = -1;
884         static int ghc_slot = -1;
885         MonoMethod **overrides;
886         int onum = 0;
887
888         g_assert (class);
889
890         if (class->inited)
891                 return;
892
893         if (class->init_pending) {
894                 /* this indicates a cyclic dependency */
895                 g_error ("pending init %s.%s\n", class->name_space, class->name);
896         }
897
898         class->init_pending = 1;
899
900         mono_stats.initialized_class_count++;
901
902         if (class->parent && !class->parent->inited)
903                 mono_class_init (class->parent);
904
905         /*
906          * Computes the size used by the fields, and their locations
907          */
908         if (!class->size_inited)
909                 class_compute_field_layout (class);
910
911         /* initialize method pointers */
912         if (class->rank) {
913                 MonoMethod *ctor;
914                 MonoMethodSignature *sig;
915                 class->method.count = class->rank > 1? 2: 1;
916                 sig = mono_metadata_signature_alloc (class->image, class->rank);
917                 sig->ret = &mono_defaults.void_class->byval_arg;
918                 sig->pinvoke = TRUE;
919                 for (i = 0; i < class->rank; ++i)
920                         sig->params [i] = &mono_defaults.int32_class->byval_arg;
921
922                 ctor = (MonoMethod *) g_new0 (MonoMethodPInvoke, 1);
923                 ctor->klass = class;
924                 ctor->flags = METHOD_ATTRIBUTE_PUBLIC | METHOD_ATTRIBUTE_RT_SPECIAL_NAME | METHOD_ATTRIBUTE_SPECIAL_NAME;
925                 ctor->iflags = METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL;
926                 ctor->signature = sig;
927                 ctor->name = ".ctor";
928                 class->methods = g_new (MonoMethod*, class->method.count);
929                 class->methods [0] = ctor;
930                 if (class->rank > 1) {
931                         sig = mono_metadata_signature_alloc (class->image, class->rank * 2);
932                         sig->ret = &mono_defaults.void_class->byval_arg;
933                         sig->pinvoke = TRUE;
934                         for (i = 0; i < class->rank * 2; ++i)
935                                 sig->params [i] = &mono_defaults.int32_class->byval_arg;
936
937                         ctor = (MonoMethod *) g_new0 (MonoMethodPInvoke, 1);
938                         ctor->klass = class;
939                         ctor->flags = METHOD_ATTRIBUTE_PUBLIC | METHOD_ATTRIBUTE_RT_SPECIAL_NAME | METHOD_ATTRIBUTE_SPECIAL_NAME;
940                         ctor->iflags = METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL;
941                         ctor->signature = sig;
942                         ctor->name = ".ctor";
943                         class->methods [1] = ctor;
944                 }
945         } else {
946                 class->methods = g_new (MonoMethod*, class->method.count);
947                 for (i = 0; i < class->method.count; ++i) {
948                         class->methods [i] = mono_get_method (class->image,
949                                 MONO_TOKEN_METHOD_DEF | (i + class->method.first + 1), class);
950                 }
951         }
952
953         init_properties (class);
954         init_events (class);
955
956         i = mono_metadata_nesting_typedef (class->image, class->type_token, 1);
957         while (i) {
958                 MonoClass* nclass;
959                 guint32 cols [MONO_NESTED_CLASS_SIZE];
960                 mono_metadata_decode_row (&class->image->tables [MONO_TABLE_NESTEDCLASS], i - 1, cols, MONO_NESTED_CLASS_SIZE);
961                 nclass = mono_class_create_from_typedef (class->image, MONO_TOKEN_TYPE_DEF | cols [MONO_NESTED_CLASS_NESTED]);
962                 class->nested_classes = g_list_prepend (class->nested_classes, nclass);
963
964                 i = mono_metadata_nesting_typedef (class->image, class->type_token, i + 1);
965         }
966
967         mono_class_setup_supertypes (class);
968
969         if (class->flags & TYPE_ATTRIBUTE_INTERFACE) {
970                 for (i = 0; i < class->method.count; ++i)
971                         class->methods [i]->slot = i;
972                 class->init_pending = 0;
973                 class->inited = 1;
974                 /* 
975                  * class->interface_offsets is needed for the castclass/isinst code, so
976                  * we have to setup them for interfaces, too.
977                  */
978                 setup_interface_offsets (class, 0);
979                 return;
980         }
981
982         overrides = mono_class_get_overrides (class->image, class->type_token, &onum);  
983         mono_class_setup_vtable (class, overrides, onum);
984         g_free (overrides);
985
986         class->inited = 1;
987         class->init_pending = 0;
988
989         if (!default_ghc) {
990                 if (class == mono_defaults.object_class) { 
991                        
992                         for (i = 0; i < class->vtable_size; ++i) {
993                                 MonoMethod *cm = class->vtable [i];
994                
995                                 if (!strcmp (cm->name, "GetHashCode")) {
996                                         ghc_slot = i;
997                                         break;
998                                 }
999                         }
1000
1001                         g_assert (ghc_slot > 0);
1002
1003                         default_ghc = class->vtable [ghc_slot];
1004                 }
1005         }
1006         
1007         class->ghcimpl = 1;
1008         if (class->parent) { 
1009
1010                 if (class->vtable [ghc_slot] == default_ghc) {
1011                         class->ghcimpl = 0;
1012                 }
1013         }
1014
1015         if (!default_finalize) {
1016                 if (class == mono_defaults.object_class) { 
1017                        
1018                         for (i = 0; i < class->vtable_size; ++i) {
1019                                 MonoMethod *cm = class->vtable [i];
1020                
1021                                 if (!strcmp (cm->name, "Finalize")) {
1022                                         finalize_slot = i;
1023                                         break;
1024                                 }
1025                         }
1026
1027                         g_assert (finalize_slot > 0);
1028
1029                         default_finalize = class->vtable [finalize_slot];
1030                 }
1031         }
1032
1033         /* Object::Finalize should have empty implemenatation */
1034         class->has_finalize = 0;
1035         if (class->parent) { 
1036                 if (class->vtable [finalize_slot] != default_finalize)
1037                         class->has_finalize = 1;
1038         }
1039
1040         if (mono_debugger_class_init_func)
1041                 mono_debugger_class_init_func (class);
1042 }
1043
1044
1045 void
1046 mono_class_setup_mono_type (MonoClass *class)
1047 {
1048         const char *name = class->name;
1049         const char *nspace = class->name_space;
1050
1051         if (class->flags & TYPE_ATTRIBUTE_INTERFACE)
1052                 class->interface_id = mono_get_unique_iid (class);
1053
1054         class->this_arg.byref = 1;
1055         class->this_arg.data.klass = class;
1056         class->this_arg.type = MONO_TYPE_CLASS;
1057         class->byval_arg.data.klass = class;
1058         class->byval_arg.type = MONO_TYPE_CLASS;
1059
1060         if (!strcmp (nspace, "System")) {
1061                 if (!strcmp (name, "ValueType")) {
1062                         /*
1063                          * do not set the valuetype bit for System.ValueType.
1064                          * class->valuetype = 1;
1065                          */
1066                 } else if (!strcmp (name, "Enum")) {
1067                         /*
1068                          * do not set the valuetype bit for System.Enum.
1069                          * class->valuetype = 1;
1070                          */
1071                         class->valuetype = 0;
1072                         class->enumtype = 0;
1073                 } else if (!strcmp (name, "Object")) {
1074                         class->this_arg.type = class->byval_arg.type = MONO_TYPE_OBJECT;
1075                 } else if (!strcmp (name, "String")) {
1076                         class->this_arg.type = class->byval_arg.type = MONO_TYPE_STRING;
1077                 } else if (!strcmp (name, "TypedReference")) {
1078                         class->this_arg.type = class->byval_arg.type = MONO_TYPE_TYPEDBYREF;
1079                 }
1080         }
1081         
1082         if (class->valuetype) {
1083                 int t = MONO_TYPE_VALUETYPE;
1084                 if (!strcmp (nspace, "System")) {
1085                         switch (*name) {
1086                         case 'B':
1087                                 if (!strcmp (name, "Boolean")) {
1088                                         t = MONO_TYPE_BOOLEAN;
1089                                 } else if (!strcmp(name, "Byte")) {
1090                                         t = MONO_TYPE_U1;
1091                                         class->blittable = TRUE;                                                
1092                                 }
1093                                 break;
1094                         case 'C':
1095                                 if (!strcmp (name, "Char")) {
1096                                         t = MONO_TYPE_CHAR;
1097                                 }
1098                                 break;
1099                         case 'D':
1100                                 if (!strcmp (name, "Double")) {
1101                                         t = MONO_TYPE_R8;
1102                                         class->blittable = TRUE;                                                
1103                                 }
1104                                 break;
1105                         case 'I':
1106                                 if (!strcmp (name, "Int32")) {
1107                                         t = MONO_TYPE_I4;
1108                                         class->blittable = TRUE;
1109                                 } else if (!strcmp(name, "Int16")) {
1110                                         t = MONO_TYPE_I2;
1111                                         class->blittable = TRUE;
1112                                 } else if (!strcmp(name, "Int64")) {
1113                                         t = MONO_TYPE_I8;
1114                                         class->blittable = TRUE;
1115                                 } else if (!strcmp(name, "IntPtr")) {
1116                                         t = MONO_TYPE_I;
1117                                         class->blittable = TRUE;
1118                                 }
1119                                 break;
1120                         case 'S':
1121                                 if (!strcmp (name, "Single")) {
1122                                         t = MONO_TYPE_R4;
1123                                         class->blittable = TRUE;                                                
1124                                 } else if (!strcmp(name, "SByte")) {
1125                                         t = MONO_TYPE_I1;
1126                                         class->blittable = TRUE;
1127                                 }
1128                                 break;
1129                         case 'U':
1130                                 if (!strcmp (name, "UInt32")) {
1131                                         t = MONO_TYPE_U4;
1132                                         class->blittable = TRUE;
1133                                 } else if (!strcmp(name, "UInt16")) {
1134                                         t = MONO_TYPE_U2;
1135                                         class->blittable = TRUE;
1136                                 } else if (!strcmp(name, "UInt64")) {
1137                                         t = MONO_TYPE_U8;
1138                                         class->blittable = TRUE;
1139                                 } else if (!strcmp(name, "UIntPtr")) {
1140                                         t = MONO_TYPE_U;
1141                                         class->blittable = TRUE;
1142                                 }
1143                                 break;
1144                         case 'T':
1145                                 if (!strcmp (name, "TypedReference")) {
1146                                         t = MONO_TYPE_TYPEDBYREF;
1147                                         class->blittable = TRUE;
1148                                 }
1149                                 break;
1150                         case 'V':
1151                                 if (!strcmp (name, "Void")) {
1152                                         t = MONO_TYPE_VOID;
1153                                 }
1154                                 break;
1155                         default:
1156                                 break;
1157                         }
1158                 }
1159                 class->this_arg.type = class->byval_arg.type = t;
1160         }
1161 }
1162
1163 void
1164 mono_class_setup_parent (MonoClass *class, MonoClass *parent)
1165 {
1166         gboolean system_namespace;
1167
1168         system_namespace = !strcmp (class->name_space, "System");
1169
1170         /* if root of the hierarchy */
1171         if (system_namespace && !strcmp (class->name, "Object")) {
1172                 class->parent = NULL;
1173                 class->instance_size = sizeof (MonoObject);
1174                 return;
1175         }
1176         if (!strcmp (class->name, "<Module>")) {
1177                 class->parent = NULL;
1178                 class->instance_size = 0;
1179                 return;
1180         }
1181
1182         if (!(class->flags & TYPE_ATTRIBUTE_INTERFACE)) {
1183                 class->parent = parent;
1184
1185                 if (!parent)
1186                         g_assert_not_reached (); /* FIXME */
1187
1188                 class->marshalbyref = parent->marshalbyref;
1189                 class->contextbound  = parent->contextbound;
1190                 class->delegate  = parent->delegate;
1191                 
1192                 if (system_namespace) {
1193                         if (*class->name == 'M' && !strcmp (class->name, "MarshalByRefObject"))
1194                                 class->marshalbyref = 1;
1195
1196                         if (*class->name == 'C' && !strcmp (class->name, "ContextBoundObject")) 
1197                                 class->contextbound  = 1;
1198
1199                         if (*class->name == 'D' && !strcmp (class->name, "Delegate")) 
1200                                 class->delegate  = 1;
1201                 }
1202
1203                 if (class->parent->enumtype || ((strcmp (class->parent->name, "ValueType") == 0) && 
1204                                                 (strcmp (class->parent->name_space, "System") == 0)))
1205                         class->valuetype = 1;
1206                 if (((strcmp (class->parent->name, "Enum") == 0) && (strcmp (class->parent->name_space, "System") == 0))) {
1207                         class->valuetype = class->enumtype = 1;
1208                 }
1209                 /*class->enumtype = class->parent->enumtype; */
1210                 class->parent->subclasses = g_list_prepend (class->parent->subclasses, class);
1211                 mono_class_setup_supertypes (class);
1212         } else {
1213                 class->parent = NULL;
1214         }
1215
1216 }
1217
1218 void
1219 mono_class_setup_supertypes (MonoClass *class)
1220 {
1221         MonoClass *k;
1222         int ms, i;
1223
1224         if (class->supertypes)
1225                 return;
1226
1227         class->idepth = 0;
1228         for (k = class; k ; k = k->parent) {
1229                 class->idepth++;
1230         }
1231
1232         ms = MAX (MONO_DEFAULT_SUPERTABLE_SIZE, class->idepth);
1233         class->supertypes = g_new0 (MonoClass *, ms);
1234
1235         if (class->parent) {
1236                 for (i = class->idepth, k = class; k ; k = k->parent)
1237                         class->supertypes [--i] = k;
1238         } else {
1239                 class->supertypes [0] = class;
1240         }
1241 }       
1242
1243 /**
1244  * @image: context where the image is created
1245  * @type_token:  typedef token
1246  */
1247 static MonoClass *
1248 mono_class_create_from_typedef (MonoImage *image, guint32 type_token)
1249 {
1250         MonoTableInfo *tt = &image->tables [MONO_TABLE_TYPEDEF];
1251         MonoClass *class, *parent = NULL;
1252         guint32 cols [MONO_TYPEDEF_SIZE];
1253         guint32 cols_next [MONO_TYPEDEF_SIZE];
1254         guint tidx = mono_metadata_token_index (type_token);
1255         const char *name, *nspace;
1256         guint icount = 0; 
1257         MonoClass **interfaces;
1258
1259         if ((class = g_hash_table_lookup (image->class_cache, GUINT_TO_POINTER (type_token)))) 
1260                 return class;
1261
1262         g_assert (mono_metadata_token_table (type_token) == MONO_TABLE_TYPEDEF);
1263         
1264         mono_metadata_decode_row (tt, tidx - 1, cols, MONO_TYPEDEF_SIZE);
1265         
1266         name = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
1267         nspace = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
1268
1269         if (cols [MONO_TYPEDEF_EXTENDS])
1270                 parent = mono_class_get (image, mono_metadata_token_from_dor (cols [MONO_TYPEDEF_EXTENDS]));
1271         interfaces = mono_metadata_interfaces_from_typedef (image, type_token, &icount);
1272
1273         class = g_malloc0 (sizeof (MonoClass));
1274                            
1275         g_hash_table_insert (image->class_cache, GUINT_TO_POINTER (type_token), class);
1276
1277         class->interfaces = interfaces;
1278         class->interface_count = icount;
1279
1280         class->name = name;
1281         class->name_space = nspace;
1282
1283         class->image = image;
1284         class->type_token = type_token;
1285         class->flags = cols [MONO_TYPEDEF_FLAGS];
1286
1287         if ((class->flags & TYPE_ATTRIBUTE_STRING_FORMAT_MASK) == TYPE_ATTRIBUTE_UNICODE_CLASS)
1288                 class->unicode = 1;
1289         /* fixme: maybe we must set this on windows 
1290         if ((class->flags & TYPE_ATTRIBUTE_STRING_FORMAT_MASK) == TYPE_ATTRIBUTE_AUTO_CLASS)
1291                 class->unicode = 1;
1292         */
1293
1294         class->cast_class = class->element_class = class;
1295
1296         /*g_print ("Init class %s\n", name);*/
1297
1298         mono_class_setup_parent (class, parent);
1299
1300         mono_class_setup_mono_type (class);
1301
1302         /*
1303          * Compute the field and method lists
1304          */
1305         class->field.first  = cols [MONO_TYPEDEF_FIELD_LIST] - 1;
1306         class->method.first = cols [MONO_TYPEDEF_METHOD_LIST] - 1;
1307
1308         if (tt->rows > tidx){           
1309                 mono_metadata_decode_row (tt, tidx, cols_next, CSIZE (cols_next));
1310                 class->field.last  = cols_next [MONO_TYPEDEF_FIELD_LIST] - 1;
1311                 class->method.last = cols_next [MONO_TYPEDEF_METHOD_LIST] - 1;
1312         } else {
1313                 class->field.last  = image->tables [MONO_TABLE_FIELD].rows;
1314                 class->method.last = image->tables [MONO_TABLE_METHOD].rows;
1315         }
1316
1317         if (cols [MONO_TYPEDEF_FIELD_LIST] && 
1318             cols [MONO_TYPEDEF_FIELD_LIST] <= image->tables [MONO_TABLE_FIELD].rows)
1319                 class->field.count = class->field.last - class->field.first;
1320         else
1321                 class->field.count = 0;
1322
1323         if (cols [MONO_TYPEDEF_METHOD_LIST] <= image->tables [MONO_TABLE_METHOD].rows)
1324                 class->method.count = class->method.last - class->method.first;
1325         else
1326                 class->method.count = 0;
1327
1328         /* reserve space to store vector pointer in arrays */
1329         if (!strcmp (nspace, "System") && !strcmp (name, "Array")) {
1330                 class->instance_size += 2 * sizeof (gpointer);
1331                 g_assert (class->field.count == 0);
1332         }
1333
1334         if (class->enumtype)
1335                 class_compute_field_layout (class);
1336
1337         if ((type_token = mono_metadata_nested_in_typedef (image, type_token)))
1338                 class->nested_in = mono_class_create_from_typedef (image, type_token);
1339
1340         return class;
1341 }
1342
1343 MonoClass *
1344 mono_ptr_class_get (MonoType *type)
1345 {
1346         MonoClass *result;
1347         MonoClass *el_class;
1348         static GHashTable *ptr_hash = NULL;
1349
1350         if (!ptr_hash)
1351                 ptr_hash = g_hash_table_new (g_direct_hash, g_direct_equal);
1352         el_class = mono_class_from_mono_type (type);
1353         if ((result = g_hash_table_lookup (ptr_hash, el_class)))
1354                 return result;
1355         result = g_new0 (MonoClass, 1);
1356
1357         result->parent = NULL; /* no parent for PTR types */
1358         result->name = "System";
1359         result->name_space = "MonoPtrFakeClass";
1360         result->image = el_class->image;
1361         result->inited = TRUE;
1362         result->flags = TYPE_ATTRIBUTE_CLASS | (el_class->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK);
1363         /* Can pointers get boxed? */
1364         result->instance_size = sizeof (gpointer);
1365         result->cast_class = result->element_class = el_class;
1366         result->enum_basetype = &result->element_class->byval_arg;
1367
1368         result->this_arg.type = result->byval_arg.type = MONO_TYPE_PTR;
1369         result->this_arg.data.type = result->byval_arg.data.type = result->enum_basetype;
1370         result->this_arg.byref = TRUE;
1371
1372         mono_class_setup_supertypes (result);
1373
1374         g_hash_table_insert (ptr_hash, el_class, result);
1375
1376         return result;
1377 }
1378
1379 static MonoClass *
1380 mono_fnptr_class_get (MonoMethodSignature *sig)
1381 {
1382         MonoClass *result;
1383         static GHashTable *ptr_hash = NULL;
1384
1385         if (!ptr_hash)
1386                 ptr_hash = g_hash_table_new (g_direct_hash, g_direct_equal);
1387         
1388         if ((result = g_hash_table_lookup (ptr_hash, sig)))
1389                 return result;
1390         result = g_new0 (MonoClass, 1);
1391
1392         result->parent = NULL; /* no parent for PTR types */
1393         result->name = "System";
1394         result->name_space = "MonoFNPtrFakeClass";
1395         result->image = NULL; /* need to fix... */
1396         result->inited = TRUE;
1397         result->flags = TYPE_ATTRIBUTE_CLASS; // | (el_class->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK);
1398         /* Can pointers get boxed? */
1399         result->instance_size = sizeof (gpointer);
1400         result->cast_class = result->element_class = result;
1401
1402         result->this_arg.type = result->byval_arg.type = MONO_TYPE_FNPTR;
1403         result->this_arg.data.method = result->byval_arg.data.method = sig;
1404         result->this_arg.byref = TRUE;
1405         result->enum_basetype = &result->element_class->byval_arg;
1406
1407         mono_class_setup_supertypes (result);
1408
1409         g_hash_table_insert (ptr_hash, sig, result);
1410
1411         return result;
1412 }
1413
1414 MonoClass *
1415 mono_class_from_mono_type (MonoType *type)
1416 {
1417         switch (type->type) {
1418         case MONO_TYPE_OBJECT:
1419                 return mono_defaults.object_class;
1420         case MONO_TYPE_VOID:
1421                 return mono_defaults.void_class;
1422         case MONO_TYPE_BOOLEAN:
1423                 return mono_defaults.boolean_class;
1424         case MONO_TYPE_CHAR:
1425                 return mono_defaults.char_class;
1426         case MONO_TYPE_I1:
1427                 return mono_defaults.sbyte_class;
1428         case MONO_TYPE_U1:
1429                 return mono_defaults.byte_class;
1430         case MONO_TYPE_I2:
1431                 return mono_defaults.int16_class;
1432         case MONO_TYPE_U2:
1433                 return mono_defaults.uint16_class;
1434         case MONO_TYPE_I4:
1435                 return mono_defaults.int32_class;
1436         case MONO_TYPE_U4:
1437                 return mono_defaults.uint32_class;
1438         case MONO_TYPE_I:
1439                 return mono_defaults.int_class;
1440         case MONO_TYPE_U:
1441                 return mono_defaults.uint_class;
1442         case MONO_TYPE_I8:
1443                 return mono_defaults.int64_class;
1444         case MONO_TYPE_U8:
1445                 return mono_defaults.uint64_class;
1446         case MONO_TYPE_R4:
1447                 return mono_defaults.single_class;
1448         case MONO_TYPE_R8:
1449                 return mono_defaults.double_class;
1450         case MONO_TYPE_STRING:
1451                 return mono_defaults.string_class;
1452         case MONO_TYPE_TYPEDBYREF:
1453                 return mono_defaults.typed_reference_class;
1454         case MONO_TYPE_ARRAY:
1455                 return mono_array_class_get (type->data.array->type, type->data.array->rank);
1456         case MONO_TYPE_PTR:
1457                 return mono_ptr_class_get (type->data.type);
1458         case MONO_TYPE_FNPTR:
1459                 return mono_fnptr_class_get (type->data.method);
1460         case MONO_TYPE_SZARRAY:
1461                 return mono_array_class_get (type->data.type, 1);
1462         case MONO_TYPE_CLASS:
1463         case MONO_TYPE_VALUETYPE:
1464                 return type->data.klass;
1465                 
1466         case MONO_TYPE_GENERICINST:
1467                 g_warning ("mono_class_from_type: implement me MONO_TYPE_GENERICINST");
1468                 g_assert_not_reached ();
1469                 
1470         case MONO_TYPE_VAR:
1471                 g_warning ("mono_class_from_type: implement me MONO_TYPE_VAR");
1472                 g_assert_not_reached ();
1473
1474         case MONO_TYPE_MVAR:
1475                 g_warning ("mono_class_from_type: implement me MONO_TYPE_MVAR");
1476                 g_assert_not_reached ();
1477                 
1478         default:
1479                 g_warning ("implement me 0x%02x\n", type->type);
1480                 g_assert_not_reached ();
1481         }
1482         
1483         return NULL;
1484 }
1485
1486 /**
1487  * @image: context where the image is created
1488  * @type_spec:  typespec token
1489  */
1490 static MonoClass *
1491 mono_class_create_from_typespec (MonoImage *image, guint32 type_spec)
1492 {
1493         MonoType *type;
1494         MonoClass *class;
1495
1496         type = mono_type_create_from_typespec (image, type_spec);
1497
1498         switch (type->type) {
1499         case MONO_TYPE_ARRAY:
1500                 class = mono_array_class_get (type->data.array->type, type->data.array->rank);
1501                 break;
1502         case MONO_TYPE_SZARRAY:
1503                 class = mono_array_class_get (type->data.type, 1);
1504                 break;
1505         case MONO_TYPE_PTR:
1506                 class = mono_class_from_mono_type (type->data.type);
1507                 break;
1508         default:
1509                 /* it seems any type can be stored in TypeSpec as well */
1510                 class = mono_class_from_mono_type (type);
1511                 break;
1512         }
1513
1514         mono_metadata_free_type (type);
1515         
1516         return class;
1517 }
1518
1519 /**
1520  * mono_array_class_get:
1521  * @element_type: element type 
1522  * @rank: the dimension of the array class
1523  *
1524  * Returns: a class object describing the array with element type @element_type and 
1525  * dimension @rank. 
1526  */
1527 MonoClass *
1528 mono_array_class_get (MonoType *element_type, guint32 rank)
1529 {
1530         MonoClass *eclass;
1531         MonoImage *image;
1532         MonoClass *class;
1533         MonoClass *parent = NULL;
1534         GSList *list;
1535         int nsize;
1536         char *name;
1537
1538         eclass = mono_class_from_mono_type (element_type);
1539         g_assert (rank <= 255);
1540
1541         parent = mono_defaults.array_class;
1542
1543         if (!parent->inited)
1544                 mono_class_init (parent);
1545
1546         image = eclass->image;
1547
1548         if ((list = g_hash_table_lookup (image->array_cache, &eclass->byval_arg))) {
1549                 for (; list; list = list->next) {
1550                         class = list->data;
1551                         if (class->rank == rank)
1552                                 return class;
1553                 }
1554         }
1555         
1556         class = g_malloc0 (sizeof (MonoClass) + parent->vtable_size * sizeof (gpointer));
1557
1558         class->image = image;
1559         class->name_space = eclass->name_space;
1560         nsize = strlen (eclass->name);
1561         name = g_malloc (nsize + 2 + rank);
1562         memcpy (name, eclass->name, nsize);
1563         name [nsize] = '[';
1564         if (rank > 1)
1565                 memset (name + nsize + 1, ',', rank - 1);
1566         name [nsize + rank] = ']';
1567         name [nsize + rank + 1] = 0;
1568         class->name = name;
1569         class->type_token = 0;
1570         /* all arrays are marked serializable and sealed, bug #42779 */
1571         class->flags = TYPE_ATTRIBUTE_CLASS | TYPE_ATTRIBUTE_SERIALIZABLE | TYPE_ATTRIBUTE_SEALED |
1572                 (eclass->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK);
1573         class->parent = parent;
1574         class->instance_size = mono_class_instance_size (class->parent);
1575         class->class_size = 0;
1576         class->vtable_size = parent->vtable_size;
1577         class->parent->subclasses = g_list_prepend (class->parent->subclasses, class);
1578         mono_class_setup_supertypes (class);
1579
1580         class->rank = rank;
1581         
1582         if (eclass->enumtype)
1583                 class->cast_class = eclass->element_class;
1584         else
1585                 class->cast_class = eclass;
1586
1587         class->element_class = eclass;
1588
1589         if (rank > 1) {
1590                 MonoArrayType *at = g_new0 (MonoArrayType, 1);
1591                 class->byval_arg.type = MONO_TYPE_ARRAY;
1592                 class->byval_arg.data.array = at;
1593                 at->type = &eclass->byval_arg;
1594                 at->rank = rank;
1595                 /* FIXME: complete.... */
1596         } else {
1597                 /* FIXME: this is not correct. the lbound could be >0 */
1598                 class->byval_arg.type = MONO_TYPE_SZARRAY;
1599                 class->byval_arg.data.type = &eclass->byval_arg;
1600         }
1601         class->this_arg = class->byval_arg;
1602         class->this_arg.byref = 1;
1603
1604         list = g_slist_append (list, class);
1605         g_hash_table_insert (image->array_cache, &class->element_class->byval_arg, list);
1606         return class;
1607 }
1608
1609 /**
1610  * mono_class_instance_size:
1611  * @klass: a class 
1612  * 
1613  * Returns: the size of an object instance
1614  */
1615 gint32
1616 mono_class_instance_size (MonoClass *klass)
1617 {
1618         
1619         if (!klass->size_inited)
1620                 mono_class_init (klass);
1621
1622         return klass->instance_size;
1623 }
1624
1625 /**
1626  * mono_class_min_align:
1627  * @klass: a class 
1628  * 
1629  * Returns: minimm alignment requirements 
1630  */
1631 gint32
1632 mono_class_min_align (MonoClass *klass)
1633 {
1634         
1635         if (!klass->size_inited)
1636                 mono_class_init (klass);
1637
1638         return klass->min_align;
1639 }
1640
1641 /**
1642  * mono_class_value_size:
1643  * @klass: a class 
1644  *
1645  * This function is used for value types, and return the
1646  * space and the alignment to store that kind of value object.
1647  *
1648  * Returns: the size of a value of kind @klass
1649  */
1650 gint32
1651 mono_class_value_size      (MonoClass *klass, guint32 *align)
1652 {
1653         gint32 size;
1654
1655         /* fixme: check disable, because we still have external revereces to
1656          * mscorlib and Dummy Objects 
1657          */
1658         /*g_assert (klass->valuetype);*/
1659
1660         size = mono_class_instance_size (klass) - sizeof (MonoObject);
1661
1662         if (align)
1663                 *align = klass->min_align;
1664
1665         return size;
1666 }
1667
1668 /**
1669  * mono_class_data_size:
1670  * @klass: a class 
1671  * 
1672  * Returns: the size of the static class data
1673  */
1674 gint32
1675 mono_class_data_size (MonoClass *klass)
1676 {
1677         
1678         if (!klass->inited)
1679                 mono_class_init (klass);
1680
1681         return klass->class_size;
1682 }
1683
1684 /*
1685  * Auxiliary routine to mono_class_get_field
1686  *
1687  * Takes a field index instead of a field token.
1688  */
1689 static MonoClassField *
1690 mono_class_get_field_idx (MonoClass *class, int idx)
1691 {
1692         if (class->field.count){
1693                 if ((idx >= class->field.first) && (idx < class->field.last)){
1694                         return &class->fields [idx - class->field.first];
1695                 }
1696         }
1697
1698         if (!class->parent)
1699                 return NULL;
1700         
1701         return mono_class_get_field_idx (class->parent, idx);
1702 }
1703
1704 /**
1705  * mono_class_get_field:
1706  * @class: the class to lookup the field.
1707  * @field_token: the field token
1708  *
1709  * Returns: A MonoClassField representing the type and offset of
1710  * the field, or a NULL value if the field does not belong to this
1711  * class.
1712  */
1713 MonoClassField *
1714 mono_class_get_field (MonoClass *class, guint32 field_token)
1715 {
1716         int idx = mono_metadata_token_index (field_token);
1717
1718         g_assert (mono_metadata_token_code (field_token) == MONO_TOKEN_FIELD_DEF);
1719
1720         return mono_class_get_field_idx (class, idx - 1);
1721 }
1722
1723 MonoClassField *
1724 mono_class_get_field_from_name (MonoClass *klass, const char *name)
1725 {
1726         int i;
1727
1728         while (klass) {
1729                 for (i = 0; i < klass->field.count; ++i) {
1730                         if (strcmp (name, klass->fields [i].name) == 0)
1731                                 return &klass->fields [i];
1732                 }
1733                 klass = klass->parent;
1734         }
1735         return NULL;
1736 }
1737
1738 MonoProperty*
1739 mono_class_get_property_from_name (MonoClass *klass, const char *name)
1740 {
1741         int i;
1742
1743         while (klass) {
1744                 for (i = 0; i < klass->property.count; ++i) {
1745                         if (strcmp (name, klass->properties [i].name) == 0)
1746                                 return &klass->properties [i];
1747                 }
1748                 klass = klass->parent;
1749         }
1750         return NULL;
1751 }
1752
1753 /**
1754  * mono_class_get:
1755  * @image: the image where the class resides
1756  * @type_token: the token for the class
1757  * @at: an optional pointer to return the array element type
1758  *
1759  * Returns: the MonoClass that represents @type_token in @image
1760  */
1761 MonoClass *
1762 mono_class_get (MonoImage *image, guint32 type_token)
1763 {
1764         MonoClass *class;
1765
1766         if (image->assembly->dynamic)
1767                 return mono_lookup_dynamic_token (image, type_token);
1768
1769         switch (type_token & 0xff000000){
1770         case MONO_TOKEN_TYPE_DEF:
1771                 class = mono_class_create_from_typedef (image, type_token);
1772                 break;          
1773         case MONO_TOKEN_TYPE_REF:
1774                 class = mono_class_from_typeref (image, type_token);
1775                 break;
1776         case MONO_TOKEN_TYPE_SPEC:
1777                 class = mono_class_create_from_typespec (image, type_token);
1778                 break;
1779         default:
1780                 g_warning ("unknown token type %x", type_token & 0xff000000);
1781                 g_assert_not_reached ();
1782         }
1783
1784         if (!class)
1785                 g_warning ("Could not load class from token 0x%08x in %s", type_token, image->name);
1786
1787         return class;
1788 }
1789
1790 MonoClass *
1791 mono_class_from_name_case (MonoImage *image, const char* name_space, const char *name)
1792 {
1793         MonoTableInfo  *t = &image->tables [MONO_TABLE_TYPEDEF];
1794         guint32 cols [MONO_TYPEDEF_SIZE];
1795         const char *n;
1796         const char *nspace;
1797         guint32 i, visib;
1798
1799         /* add a cache if needed */
1800         for (i = 1; i <= t->rows; ++i) {
1801                 mono_metadata_decode_row (t, i - 1, cols, MONO_TYPEDEF_SIZE);
1802                 /* nested types are accessed from the nesting name */
1803                 visib = cols [MONO_TYPEDEF_FLAGS] & TYPE_ATTRIBUTE_VISIBILITY_MASK;
1804                 if (visib > TYPE_ATTRIBUTE_PUBLIC && visib <= TYPE_ATTRIBUTE_NESTED_ASSEMBLY)
1805                         continue;
1806                 n = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
1807                 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
1808                 if (g_strcasecmp (n, name) == 0 && g_strcasecmp (nspace, name_space) == 0)
1809                         return mono_class_get (image, MONO_TOKEN_TYPE_DEF | i);
1810         }
1811         return NULL;
1812 }
1813
1814 static MonoImage*
1815 load_file_for_image (MonoImage *image, int fileidx)
1816 {
1817         char *base_dir, *name;
1818         MonoImage *res;
1819         MonoTableInfo  *t = &image->tables [MONO_TABLE_FILE];
1820         const char *fname;
1821         guint32 fname_id;
1822
1823         if (fileidx < 1 || fileidx > t->rows)
1824                 return NULL;
1825         fname_id = mono_metadata_decode_row_col (t, fileidx - 1, MONO_FILE_NAME);
1826         fname = mono_metadata_string_heap (image, fname_id);
1827         base_dir = g_path_get_dirname (image->name);
1828         name = g_build_filename (base_dir, fname, NULL);
1829         res = mono_image_open (name, NULL);
1830         if (res) {
1831                 int i;
1832                 t = &res->tables [MONO_TABLE_MODULEREF];
1833                 //g_print ("loaded file %s from %s (%p)\n", name, image->name, image->assembly);
1834                 res->assembly = image->assembly;
1835                 for (i = 0; i < t->rows; ++i) {
1836                         if (res->modules [i] && !res->modules [i]->assembly)
1837                                 res->modules [i]->assembly = image->assembly;
1838                 }
1839                 mono_image_load_references (image, NULL);
1840         }
1841         g_free (name);
1842         g_free (base_dir);
1843         return res;
1844 }
1845
1846 static MonoClass*
1847 return_nested_in (MonoClass *class, char *nested) {
1848         MonoClass *found;
1849         char *s = strchr (nested, '/');
1850         GList *tmp;
1851
1852         if (s) {
1853                 *s = 0;
1854                 s++;
1855         }
1856         for (tmp = class->nested_classes; tmp; tmp = tmp->next) {
1857                 found = tmp->data;
1858                 if (strcmp (found->name, nested) == 0) {
1859                         if (s)
1860                                 return return_nested_in (found, s);
1861                         return found;
1862                 }
1863         }
1864         return NULL;
1865 }
1866
1867 MonoClass *
1868 mono_class_from_name (MonoImage *image, const char* name_space, const char *name)
1869 {
1870         GHashTable *nspace_table;
1871         MonoImage *loaded_image;
1872         guint32 token;
1873         MonoClass *class;
1874         char *nested;
1875         char buf [1024];
1876
1877         if ((nested = strchr (name, '/'))) {
1878                 int pos = nested - name;
1879                 int len = strlen (name);
1880                 if (len > 1023)
1881                         return NULL;
1882                 memcpy (buf, name, len + 1);
1883                 buf [pos] = 0;
1884                 nested = buf + pos + 1;
1885                 name = buf;
1886         }
1887
1888         nspace_table = g_hash_table_lookup (image->name_cache, name_space);
1889         
1890         if (!nspace_table || !(token = GPOINTER_TO_UINT (g_hash_table_lookup (nspace_table, name)))) {
1891                 MonoTableInfo  *t = &image->tables [MONO_TABLE_EXPORTEDTYPE];
1892                 guint32 cols [MONO_EXP_TYPE_SIZE];
1893                 int i;
1894
1895                 for (i = 0; i < t->rows; ++i) {
1896                         const char *ename, *enspace;
1897                         mono_metadata_decode_row (t, i, cols, MONO_EXP_TYPE_SIZE);
1898                         ename = mono_metadata_string_heap (image, cols [MONO_EXP_TYPE_NAME]);
1899                         enspace = mono_metadata_string_heap (image, cols [MONO_EXP_TYPE_NAMESPACE]);
1900
1901                         if (strcmp (name, ename) == 0 && strcmp (name_space, enspace) == 0) {
1902                                 guint32 impl = cols [MONO_EXP_TYPE_IMPLEMENTATION];
1903                                 if ((impl & IMPLEMENTATION_MASK) == IMPLEMENTATION_FILE) {
1904                                         loaded_image = load_file_for_image (image, impl >> IMPLEMENTATION_BITS);
1905                                         if (!loaded_image)
1906                                                 return NULL;
1907                                         class = mono_class_from_name (loaded_image, name_space, name);
1908                                         if (nested)
1909                                                 return return_nested_in (class, nested);
1910                                         return class;
1911                                 } else {
1912                                         g_error ("not yet implemented");
1913                                 }
1914                         }
1915                 }
1916                 /*g_warning ("token not found for %s.%s in image %s", name_space, name, image->name);*/
1917                 return NULL;
1918         }
1919
1920         token = MONO_TOKEN_TYPE_DEF | token;
1921
1922         class = mono_class_get (image, token);
1923         if (nested)
1924                 return return_nested_in (class, nested);
1925         return class;
1926 }
1927
1928 gboolean
1929 mono_class_is_subclass_of (MonoClass *klass, MonoClass *klassc, 
1930                                                    gboolean check_interfaces)
1931 {
1932         if (check_interfaces && (klassc->flags & TYPE_ATTRIBUTE_INTERFACE) && !(klass->flags & TYPE_ATTRIBUTE_INTERFACE)) {
1933                 if ((klassc->interface_id <= klass->max_interface_id) &&
1934                         (klass->interface_offsets [klassc->interface_id] >= 0))
1935                         return TRUE;
1936         } else if (check_interfaces && (klassc->flags & TYPE_ATTRIBUTE_INTERFACE) && (klass->flags & TYPE_ATTRIBUTE_INTERFACE)) {
1937                 int i;
1938
1939                 for (i = 0; i < klass->interface_count; i ++) {
1940                         MonoClass *ic =  klass->interfaces [i];
1941                         if (ic == klassc)
1942                                 return TRUE;
1943                 }
1944         } else {
1945                 if (!(klass->flags & TYPE_ATTRIBUTE_INTERFACE) && mono_class_has_parent (klass, klassc))
1946                         return TRUE;
1947         }
1948         
1949         return FALSE;
1950 }
1951
1952 /*
1953  * Returns the nnumber of bytes an element of type klass
1954  * uses when stored into an array.
1955  */
1956 gint32
1957 mono_class_array_element_size (MonoClass *klass)
1958 {
1959         int t = klass->byval_arg.type;
1960         
1961 handle_enum:
1962         switch (t) {
1963         case MONO_TYPE_I1:
1964         case MONO_TYPE_U1:
1965         case MONO_TYPE_BOOLEAN:
1966                 return 1;
1967         case MONO_TYPE_I2:
1968         case MONO_TYPE_U2:
1969         case MONO_TYPE_CHAR:
1970                 return 2;
1971         case MONO_TYPE_I4:
1972         case MONO_TYPE_U4:
1973         case MONO_TYPE_R4:
1974                 return 4;
1975         case MONO_TYPE_I:
1976         case MONO_TYPE_U:
1977         case MONO_TYPE_PTR:
1978         case MONO_TYPE_CLASS:
1979         case MONO_TYPE_STRING:
1980         case MONO_TYPE_OBJECT:
1981         case MONO_TYPE_SZARRAY:
1982         case MONO_TYPE_ARRAY:    
1983                 return sizeof (gpointer);
1984         case MONO_TYPE_I8:
1985         case MONO_TYPE_U8:
1986         case MONO_TYPE_R8:
1987                 return 8;
1988         case MONO_TYPE_VALUETYPE:
1989                 if (klass->enumtype) {
1990                         t = klass->enum_basetype->type;
1991                         goto handle_enum;
1992                 }
1993                 return mono_class_instance_size (klass) - sizeof (MonoObject);
1994         default:
1995                 g_error ("unknown type 0x%02x in mono_class_array_element_size", t);
1996         }
1997         return -1;
1998 }
1999
2000 /**
2001  * mono_array_element_size:
2002  * @ac: pointer to a #MonoArrayClass
2003  *
2004  * Returns: the size of single array element.
2005  */
2006 gint32
2007 mono_array_element_size (MonoClass *ac)
2008 {
2009         return mono_class_array_element_size (ac->element_class);
2010 }
2011
2012 gpointer
2013 mono_ldtoken (MonoImage *image, guint32 token, MonoClass **handle_class)
2014 {
2015         if (image->assembly->dynamic) {
2016                 gpointer obj = mono_lookup_dynamic_token (image, token);
2017
2018                 switch (token & 0xff000000) {
2019                 case MONO_TOKEN_TYPE_DEF:
2020                 case MONO_TOKEN_TYPE_REF:
2021                 case MONO_TOKEN_TYPE_SPEC:
2022                         if (handle_class)
2023                                 *handle_class = mono_defaults.typehandle_class;
2024                         return &((MonoClass*)obj)->byval_arg;
2025                 case MONO_TOKEN_METHOD_DEF:
2026                         if (handle_class)
2027                                 *handle_class = mono_defaults.methodhandle_class;
2028                         return obj;
2029                 case MONO_TOKEN_FIELD_DEF:
2030                         if (handle_class)
2031                                 *handle_class = mono_defaults.fieldhandle_class;
2032                         return obj;
2033                 default:
2034                         g_assert_not_reached ();
2035                 }
2036         }
2037
2038         switch (token & 0xff000000) {
2039         case MONO_TOKEN_TYPE_DEF:
2040         case MONO_TOKEN_TYPE_REF: {
2041                 MonoClass *class;
2042                 if (handle_class)
2043                         *handle_class = mono_defaults.typehandle_class;
2044                 class = mono_class_get (image, token);
2045                 mono_class_init (class);
2046                 /* We return a MonoType* as handle */
2047                 return &class->byval_arg;
2048         }
2049         case MONO_TOKEN_TYPE_SPEC: {
2050                 MonoClass *class;
2051                 if (handle_class)
2052                         *handle_class = mono_defaults.typehandle_class;
2053                 class = mono_class_create_from_typespec (image, token);
2054                 mono_class_init (class);
2055                 return &class->byval_arg;
2056         }
2057         case MONO_TOKEN_FIELD_DEF: {
2058                 MonoClass *class;
2059                 guint32 type = mono_metadata_typedef_from_field (image, mono_metadata_token_index (token));
2060                 class = mono_class_get (image, MONO_TOKEN_TYPE_DEF | type);
2061                 mono_class_init (class);
2062                 if (handle_class)
2063                                 *handle_class = mono_defaults.fieldhandle_class;
2064                 return mono_class_get_field (class, token);
2065         }
2066         case MONO_TOKEN_METHOD_DEF:
2067         case MONO_TOKEN_MEMBER_REF:
2068         default:
2069                 g_warning ("Unknown token 0x%08x in ldtoken", token);
2070                 break;
2071         }
2072         return NULL;
2073 }
2074
2075 /**
2076  * This function might need to call runtime functions so it can't be part
2077  * of the metadata library.
2078  */
2079 static MonoLookupDynamicToken lookup_dynamic = NULL;
2080
2081 void
2082 mono_install_lookup_dynamic_token (MonoLookupDynamicToken func)
2083 {
2084         lookup_dynamic = func;
2085 }
2086
2087 gpointer
2088 mono_lookup_dynamic_token (MonoImage *image, guint32 token)
2089 {
2090         return lookup_dynamic (image, token);
2091 }