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