81f3cb2d4ca7e89a72a9a98d697f4ae24a7f2c8d
[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 #if HAVE_BOEHM_GC
33 #include <gc/gc.h>
34 #endif
35
36 #define CSIZE(x) (sizeof (x) / 4)
37
38 MonoStats mono_stats;
39
40 gboolean mono_print_vtable = FALSE;
41
42 static MonoClass * mono_class_create_from_typedef (MonoImage *image, guint32 type_token);
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
53         mono_metadata_decode_row (t, (type_token&0xffffff)-1, cols, MONO_TYPEREF_SIZE);
54
55         name = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAME]);
56         nspace = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAMESPACE]);
57         
58         idx = cols [MONO_TYPEREF_SCOPE] >> RESOLTION_SCOPE_BITS;
59         switch (cols [MONO_TYPEREF_SCOPE] & RESOLTION_SCOPE_MASK) {
60         case RESOLTION_SCOPE_MODULE:
61                 if (!idx)
62                         g_error ("null ResolutionScope not yet handled");
63                 /* a typedef in disguise */
64                 return mono_class_from_name (image, nspace, name);
65         case RESOLTION_SCOPE_MODULEREF:
66                 return mono_class_from_name (image->assembly->modules [idx - 1], nspace, name);
67         case RESOLTION_SCOPE_TYPEREF: {
68                 MonoClass *enclosing = mono_class_from_typeref (image, MONO_TOKEN_TYPE_REF | idx);
69                 GList *tmp;
70                 mono_class_init (enclosing);
71                 for (tmp = enclosing->nested_classes; tmp; tmp = tmp->next) {
72                         res = tmp->data;
73                         if (strcmp (res->name, name) == 0)
74                                 return res;
75                 }
76                 g_warning ("TypeRef ResolutionScope not yet handled (%d)", idx);
77                 return NULL;
78         }
79         case RESOLTION_SCOPE_ASSEMBLYREF:
80                 break;
81         }
82
83         if (!image->references ||  !image->references [idx-1]) {
84                 /* 
85                  * detected a reference to mscorlib, we simply return a reference to a dummy 
86                  * until we have a better solution.
87                  */
88                 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])); 
89                 
90                 res = mono_class_from_name (image, "System", "MonoDummy");
91                 /* prevent method loading */
92                 res->dummy = 1;
93                 /* some storage if the type is used  - very ugly hack */
94                 res->instance_size = 2*sizeof (gpointer);
95                 return res;
96         }       
97
98         /* load referenced assembly */
99         image = image->references [idx-1]->image;
100
101         return mono_class_from_name (image, nspace, name);
102 }
103
104 /** 
105  * class_compute_field_layout:
106  * @m: pointer to the metadata.
107  * @class: The class to initialize
108  * @pack: the value from the .pack directive (or 0)
109  *
110  * Initializes the class->fields.
111  *
112  * Currently we only support AUTO_LAYOUT, and do not even try to do
113  * a good job at it.  This is temporary to get the code for Paolo.
114  */
115 static void
116 class_compute_field_layout (MonoClass *class, int pack)
117 {
118         MonoImage *m = class->image; 
119         const int top = class->field.count;
120         guint32 layout = class->flags & TYPE_ATTRIBUTE_LAYOUT_MASK;
121         MonoTableInfo *t = &m->tables [MONO_TABLE_FIELD];
122         int i;
123         guint32 rva;
124
125         /*
126          * Fetch all the field information.
127          */
128         for (i = 0; i < top; i++){
129                 const char *sig;
130                 guint32 cols [MONO_FIELD_SIZE];
131                 int idx = class->field.first + i;
132                 
133                 mono_metadata_decode_row (t, idx, cols, CSIZE (cols));
134                 /* The name is needed for fieldrefs */
135                 class->fields [i].name = mono_metadata_string_heap (m, cols [MONO_FIELD_NAME]);
136                 sig = mono_metadata_blob_heap (m, cols [MONO_FIELD_SIGNATURE]);
137                 mono_metadata_decode_value (sig, &sig);
138                 /* FIELD signature == 0x06 */
139                 g_assert (*sig == 0x06);
140                 class->fields [i].type = mono_metadata_parse_field_type (
141                         m, cols [MONO_FIELD_FLAGS], sig + 1, &sig);
142                 if (cols [MONO_FIELD_FLAGS] & FIELD_ATTRIBUTE_HAS_FIELD_RVA) {
143                         mono_metadata_field_info (m, idx, NULL, &rva, NULL);
144                         if (!rva)
145                                 g_warning ("field %s in %s should have RVA data, but hasn't", class->fields [i].name, class->name);
146                         class->fields [i].data = mono_cli_rva_map (class->image->image_info, rva);
147                 }
148                 if (class->enumtype && !(cols [MONO_FIELD_FLAGS] & FIELD_ATTRIBUTE_STATIC)) {
149                         class->enum_basetype = class->fields [i].type;
150                         class->element_class = mono_class_from_mono_type (class->enum_basetype);
151                 }
152         }
153         if (class->enumtype && !class->enum_basetype) {
154                 if (!((strcmp (class->name, "Enum") == 0) && (strcmp (class->name_space, "System") == 0)))
155                         G_BREAKPOINT ();
156         }
157         /*
158          * Compute field layout and total size (not considering static fields)
159          */
160         switch (layout) {
161         case TYPE_ATTRIBUTE_AUTO_LAYOUT:
162         case TYPE_ATTRIBUTE_SEQUENTIAL_LAYOUT:
163                 for (i = 0; i < top; i++){
164                         int size, align;
165                         
166                         if (class->fields [i].type->attrs & FIELD_ATTRIBUTE_STATIC)
167                                 continue;
168
169                         size = mono_type_size (class->fields [i].type, &align);
170
171                         /* FIXME (LAMESPEC): should we also change the min alignment according to pack? */
172                         align = pack? MIN (pack, align): align;
173                         class->min_align = MAX (align, class->min_align);
174                         class->fields [i].offset = class->instance_size;
175                         class->fields [i].offset += align - 1;
176                         class->fields [i].offset &= ~(align - 1);
177                         class->instance_size = class->fields [i].offset + size;
178                 }
179        
180                 if (class->instance_size & (class->min_align - 1)) {
181                         class->instance_size += class->min_align - 1;
182                         class->instance_size &= ~(class->min_align - 1);
183                 }
184                 break;
185         case TYPE_ATTRIBUTE_EXPLICIT_LAYOUT:
186                 for (i = 0; i < top; i++) {
187                         int size, align;
188                         int idx = class->field.first + i;
189
190                         /*
191                          * There must be info about all the fields in a type if it
192                          * uses explicit layout.
193                          */
194
195                         if (class->fields [i].type->attrs & FIELD_ATTRIBUTE_STATIC)
196                                 continue;
197
198                         size = mono_type_size (class->fields [i].type, &align);
199                         
200                         mono_metadata_field_info (m, idx, &class->fields [i].offset, NULL, NULL);
201                         if (class->fields [i].offset == (guint32)-1)
202                                 g_warning ("%s not initialized correctly (missing field layout info for %s)", class->name, class->fields [i].name);
203                         /*
204                          * The offset is from the start of the object: this works for both
205                          * classes and valuetypes.
206                          */
207                         class->fields [i].offset += sizeof (MonoObject);
208                         /*
209                          * Calc max size.
210                          */
211                         size += class->fields [i].offset;
212                         class->instance_size = MAX (class->instance_size, size);
213                 }
214                 break;
215         }
216
217         class->size_inited = 1;
218
219         /*
220          * Compute static field layout and size
221          */
222         switch (layout) {
223         case TYPE_ATTRIBUTE_AUTO_LAYOUT:
224         case TYPE_ATTRIBUTE_SEQUENTIAL_LAYOUT:
225                 for (i = 0; i < top; i++){
226                         int size, align;
227                         
228                         if (!(class->fields [i].type->attrs & FIELD_ATTRIBUTE_STATIC))
229                                 continue;
230                         
231                         size = mono_type_size (class->fields [i].type, &align);
232                         class->fields [i].offset = class->class_size;
233                         class->fields [i].offset += align - 1;
234                         class->fields [i].offset &= ~(align - 1);
235                         class->class_size = class->fields [i].offset + size;
236                 }
237                 break;
238         case TYPE_ATTRIBUTE_EXPLICIT_LAYOUT:
239                 for (i = 0; i < top; i++){
240                         int size, align;
241
242                         /*
243                          * There must be info about all the fields in a type if it
244                          * uses explicit layout.
245                          */
246
247                         
248                         if (!(class->fields [i].type->attrs & FIELD_ATTRIBUTE_STATIC))
249                                 continue;
250
251                         size = mono_type_size (class->fields [i].type, &align);
252                         class->fields [i].offset = class->class_size;
253                         class->fields [i].offset += align - 1;
254                         class->fields [i].offset &= ~(align - 1);
255                         class->class_size = class->fields [i].offset + size;
256                 }
257                 break;
258         }
259 }
260
261 static void
262 init_properties (MonoClass *class)
263 {
264         guint startm, endm, i, j;
265         guint32 cols [MONO_PROPERTY_SIZE];
266         MonoTableInfo *pt = &class->image->tables [MONO_TABLE_PROPERTY];
267         MonoTableInfo *msemt = &class->image->tables [MONO_TABLE_METHODSEMANTICS];
268
269         class->property.first = mono_metadata_properties_from_typedef (class->image, mono_metadata_token_index (class->type_token) - 1, &class->property.last);
270         class->property.count = class->property.last - class->property.first;
271
272         class->properties = g_new0 (MonoProperty, class->property.count);
273         for (i = class->property.first; i < class->property.last; ++i) {
274                 mono_metadata_decode_row (pt, i, cols, MONO_PROPERTY_SIZE);
275                 class->properties [i - class->property.first].attrs = cols [MONO_PROPERTY_FLAGS];
276                 class->properties [i - class->property.first].name = mono_metadata_string_heap (class->image, cols [MONO_PROPERTY_NAME]);
277
278                 startm = mono_metadata_methods_from_property (class->image, i, &endm);
279                 for (j = startm; j < endm; ++j) {
280                         mono_metadata_decode_row (msemt, j, cols, MONO_METHOD_SEMA_SIZE);
281                         switch (cols [MONO_METHOD_SEMA_SEMANTICS]) {
282                         case METHOD_SEMANTIC_SETTER:
283                                 class->properties [i - class->property.first].set = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
284                                 break;
285                         case METHOD_SEMANTIC_GETTER:
286                                 class->properties [i - class->property.first].get = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
287                                 break;
288                         default:
289                                 break;
290                         }
291                 }
292         }
293 }
294
295 static void
296 init_events (MonoClass *class)
297 {
298         guint startm, endm, i, j;
299         guint32 cols [MONO_EVENT_SIZE];
300         MonoTableInfo *pt = &class->image->tables [MONO_TABLE_EVENT];
301         MonoTableInfo *msemt = &class->image->tables [MONO_TABLE_METHODSEMANTICS];
302
303         class->event.first = mono_metadata_events_from_typedef (class->image, mono_metadata_token_index (class->type_token) - 1, &class->event.last);
304         class->event.count = class->event.last - class->event.first;
305
306         class->events = g_new0 (MonoEvent, class->event.count);
307         for (i = class->event.first; i < class->event.last; ++i) {
308                 mono_metadata_decode_row (pt, i, cols, MONO_EVENT_SIZE);
309                 class->events [i - class->event.first].attrs = cols [MONO_EVENT_FLAGS];
310                 class->events [i - class->event.first].name = mono_metadata_string_heap (class->image, cols [MONO_EVENT_NAME]);
311
312                 startm = mono_metadata_methods_from_event (class->image, i, &endm);
313                 for (j = startm; j < endm; ++j) {
314                         mono_metadata_decode_row (msemt, j, cols, MONO_METHOD_SEMA_SIZE);
315                         switch (cols [MONO_METHOD_SEMA_SEMANTICS]) {
316                         case METHOD_SEMANTIC_ADD_ON:
317                                 class->events [i - class->event.first].add = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
318                                 break;
319                         case METHOD_SEMANTIC_REMOVE_ON:
320                                 class->events [i - class->event.first].remove = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
321                                 break;
322                         case METHOD_SEMANTIC_FIRE:
323                                 class->events [i - class->event.first].raise = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
324                                 break;
325                         case METHOD_SEMANTIC_OTHER: /* don't care for now */
326                         default:
327                                 break;
328                         }
329                 }
330         }
331 }
332
333 static guint
334 mono_get_unique_iid (MonoClass *class)
335 {
336         static GHashTable *iid_hash = NULL;
337         static guint iid = 0;
338
339         char *str;
340         gpointer value;
341         
342         g_assert (class->flags & TYPE_ATTRIBUTE_INTERFACE);
343
344         if (!iid_hash)
345                 iid_hash = g_hash_table_new (g_str_hash, g_str_equal);
346
347         str = g_strdup_printf ("%s|%s.%s\n", class->image->name, class->name_space, class->name);
348
349         if (g_hash_table_lookup_extended (iid_hash, str, NULL, &value)) {
350                 g_free (str);
351                 return (guint)value;
352         } else {
353                 g_hash_table_insert (iid_hash, str, (gpointer)iid);
354                 ++iid;
355         }
356
357         return iid - 1;
358 }
359
360 /**
361  * mono_class_init:
362  * @class: the class to initialize
363  *
364  * compute the instance_size, class_size and other infos that cannot be 
365  * computed at mono_class_get() time. Also compute a generic vtable and 
366  * the method slot numbers. We use this infos later to create a domain
367  * specific vtable.  
368  */
369 void
370 mono_class_init (MonoClass *class)
371 {
372         MonoClass *k, *ic;
373         MonoMethod **vtable;
374         int i, max_vtsize = 0, max_iid, cur_slot = 0;
375         static MonoMethod *default_ghc = NULL;
376         static MonoMethod *default_finalize = NULL;
377         static int finalize_slot = -1;
378         static int ghc_slot = -1;
379         guint32 packing_size = 0;
380
381         g_assert (class);
382
383         if (class->inited)
384                 return;
385
386         if (class->init_pending) {
387                 /* this indicates a cyclic dependency */
388                 g_error ("pending init %s.%s\n", class->name_space, class->name);
389         }
390
391         class->init_pending = 1;
392
393         mono_stats.initialized_class_count++;
394
395         if (class->parent) {
396                 if (!class->parent->inited)
397                         mono_class_init (class->parent);
398                 class->instance_size += class->parent->instance_size;
399                 class->class_size += class->parent->class_size;
400                 class->min_align = class->parent->min_align;
401                 cur_slot = class->parent->vtable_size;
402         } else
403                 class->min_align = 1;
404
405         if (mono_metadata_packing_from_typedef (class->image, class->type_token, &packing_size, &class->instance_size))
406                 class->instance_size += sizeof (MonoObject);
407         /* use packing_size in field layout */
408
409         /*
410          * Computes the size used by the fields, and their locations
411          */
412         if (!class->size_inited && class->field.count > 0){
413                 class->fields = g_new0 (MonoClassField, class->field.count);
414                 class_compute_field_layout (class, packing_size);
415         }
416
417         if (!(class->flags & TYPE_ATTRIBUTE_INTERFACE)) {
418                 for (i = 0; i < class->interface_count; i++) 
419                         max_vtsize += class->interfaces [i]->method.count;
420         
421                 if (class->parent)
422                         max_vtsize += class->parent->vtable_size;
423
424                 max_vtsize += class->method.count;
425         }
426
427         vtable = alloca (sizeof (gpointer) * max_vtsize);
428         memset (vtable, 0, sizeof (gpointer) * max_vtsize);
429
430         /* initialize method pointers */
431         class->methods = g_new (MonoMethod*, class->method.count);
432         for (i = 0; i < class->method.count; ++i)
433                 class->methods [i] = mono_get_method (class->image,
434                         MONO_TOKEN_METHOD_DEF | (i + class->method.first + 1), class);
435
436         init_properties (class);
437         init_events (class);
438
439         i = mono_metadata_nesting_typedef (class->image, class->type_token);
440         while (i) {
441                 MonoClass* nclass;
442                 guint32 cols [MONO_NESTED_CLASS_SIZE];
443                 mono_metadata_decode_row (&class->image->tables [MONO_TABLE_NESTEDCLASS], i - 1, cols, MONO_NESTED_CLASS_SIZE);
444                 if (cols [MONO_NESTED_CLASS_ENCLOSING] != mono_metadata_token_index (class->type_token))
445                         break;
446                 nclass = mono_class_create_from_typedef (class->image, MONO_TOKEN_TYPE_DEF | cols [MONO_NESTED_CLASS_NESTED]);
447                 class->nested_classes = g_list_prepend (class->nested_classes, nclass);
448                 ++i;
449         }
450
451         if (class->flags & TYPE_ATTRIBUTE_INTERFACE) {
452                 for (i = 0; i < class->method.count; ++i)
453                         class->methods [i]->slot = i;
454                 class->init_pending = 0;
455                 class->inited = 1;
456                 return;
457         }
458
459         /* printf ("METAINIT %s.%s\n", class->name_space, class->name); */
460
461         /* compute maximum number of slots and maximum interface id */
462         max_iid = 0;
463         for (k = class; k ; k = k->parent) {
464                 for (i = 0; i < k->interface_count; i++) {
465                         ic = k->interfaces [i];
466
467                         if (!ic->inited)
468                                 mono_class_init (ic);
469
470                         if (max_iid < ic->interface_id)
471                                 max_iid = ic->interface_id;
472                 }
473         }
474         
475         class->max_interface_id = max_iid;
476         /* compute vtable offset for interfaces */
477         class->interface_offsets = g_malloc (sizeof (gpointer) * (max_iid + 1));
478
479         for (i = 0; i <= max_iid; i++)
480                 class->interface_offsets [i] = -1;
481
482         for (i = 0; i < class->interface_count; i++) {
483                 ic = class->interfaces [i];
484                 class->interface_offsets [ic->interface_id] = cur_slot;
485                 cur_slot += ic->method.count;
486         }
487
488         for (k = class->parent; k ; k = k->parent) {
489                 for (i = 0; i < k->interface_count; i++) {
490                         ic = k->interfaces [i]; 
491                         if (class->interface_offsets [ic->interface_id] == -1) {
492                                 int io = k->interface_offsets [ic->interface_id];
493
494                                 g_assert (io >= 0);
495                                 g_assert (io <= max_vtsize);
496
497                                 class->interface_offsets [ic->interface_id] = io;
498                         }
499                 }
500         }
501
502         if (class->parent && class->parent->vtable_size)
503                 memcpy (vtable, class->parent->vtable,  sizeof (gpointer) * class->parent->vtable_size);
504  
505         for (k = class; k ; k = k->parent) {
506                 for (i = 0; i < k->interface_count; i++) {
507                         int j, l, io;
508
509                         ic = k->interfaces [i];
510                         io = k->interface_offsets [ic->interface_id];
511                         
512                         g_assert (io >= 0);
513                         g_assert (io <= max_vtsize);
514
515                         if (k == class) {
516                                 for (l = 0; l < ic->method.count; l++) {
517                                         MonoMethod *im = ic->methods [l];                                               
518                                         for (j = 0; j < class->method.count; ++j) {
519                                                 MonoMethod *cm = class->methods [j];
520                                                 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
521                                                     !(cm->flags & METHOD_ATTRIBUTE_PUBLIC) ||
522                                                     !(cm->flags & METHOD_ATTRIBUTE_NEW_SLOT))
523                                                         continue;
524                                                 if (!strcmp(cm->name, im->name) && 
525                                                     mono_metadata_signature_equal (cm->signature, im->signature)) {
526                                                         g_assert (io + l <= max_vtsize);
527                                                         vtable [io + l] = cm;
528                                                 }
529                                         }
530                                 }
531                         } else {
532                                 /* already implemented */
533                                 if (io >= k->vtable_size)
534                                         continue;
535                         }
536                                 
537                         for (l = 0; l < ic->method.count; l++) {
538                                 MonoMethod *im = ic->methods [l];                                               
539                                 MonoClass *k1;
540
541                                 g_assert (io + l <= max_vtsize);
542
543                                 if (vtable [io + l])
544                                         continue;
545                                         
546                                 for (k1 = class; k1; k1 = k1->parent) {
547                                         for (j = 0; j < k1->method.count; ++j) {
548                                                 MonoMethod *cm = k1->methods [j];
549
550                                                 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
551                                                     !(cm->flags & METHOD_ATTRIBUTE_PUBLIC))
552                                                         continue;
553                                                 
554                                                 if (!strcmp(cm->name, im->name) && 
555                                                     mono_metadata_signature_equal (cm->signature, im->signature)) {
556                                                         g_assert (io + l <= max_vtsize);
557                                                         vtable [io + l] = cm;
558                                                         break;
559                                                 }
560                                                 
561                                         }
562                                         g_assert (io + l <= max_vtsize);
563                                         if (vtable [io + l])
564                                                 break;
565                                 }
566                         }
567
568                         for (l = 0; l < ic->method.count; l++) {
569                                 MonoMethod *im = ic->methods [l];                                               
570                                 char *qname, *fqname;
571                                 
572                                 qname = g_strconcat (ic->name, ".", im->name, NULL); 
573                                 if (ic->name_space && ic->name_space [0])
574                                         fqname = g_strconcat (ic->name_space, ".", ic->name, ".", im->name, NULL);
575                                 else
576                                         fqname = NULL;
577
578                                 for (j = 0; j < class->method.count; ++j) {
579                                         MonoMethod *cm = class->methods [j];
580
581                                         if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL))
582                                                 continue;
583                                         
584                                         if (((fqname && !strcmp (cm->name, fqname)) || !strcmp (cm->name, qname)) &&
585                                             mono_metadata_signature_equal (cm->signature, im->signature)) {
586                                                 g_assert (io + l <= max_vtsize);
587                                                 vtable [io + l] = cm;
588                                                 break;
589                                         }
590                                 }
591                                 g_free (qname);
592                                 g_free (fqname);
593                         }
594
595                         
596                         if (!(class->flags & TYPE_ATTRIBUTE_ABSTRACT)) {
597                                 for (l = 0; l < ic->method.count; l++) {
598                                         char *msig;
599                                         MonoMethod *im = ic->methods [l];                                               
600                                         g_assert (io + l <= max_vtsize);
601                                         if (!(vtable [io + l])) {
602                                                 msig = mono_signature_get_desc (im->signature, FALSE);
603                                                 printf ("no implementation for interface method %s.%s::%s(%s) in class %s.%s\n",
604                                                         ic->name_space, ic->name, im->name, msig, class->name_space, class->name);
605                                                 g_free (msig);
606                                                 for (j = 0; j < class->method.count; ++j) {
607                                                         MonoMethod *cm = class->methods [j];
608                                                         msig = mono_signature_get_desc (cm->signature, FALSE);
609                                                         
610                                                         printf ("METHOD %s(%s)\n", cm->name, msig);
611                                                         g_free (msig);
612                                                 }
613                                                 g_assert_not_reached ();
614                                         }
615                                 }
616                         }
617                 
618                         for (l = 0; l < ic->method.count; l++) {
619                                 MonoMethod *im = vtable [io + l];
620
621                                 if (im) {
622                                         g_assert (io + l <= max_vtsize);
623                                         if (im->slot < 0) {
624                                                 /* FIXME: why do we need this ? */
625                                                 im->slot = io + l;
626                                                 /* g_assert_not_reached (); */
627                                         }
628                                 }
629                         }
630                 }
631         } 
632
633         for (i = 0; i < class->method.count; ++i) {
634                 MonoMethod *cm;
635                
636                 cm = class->methods [i];
637
638                 
639 #if 0
640                 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
641                     (cm->slot >= 0))
642                         continue;
643 #else  /* EXT_VTABLE_HACK */
644                 if (cm->slot >= 0)
645                         continue;
646 #endif
647
648                 if (!(cm->flags & METHOD_ATTRIBUTE_NEW_SLOT) && (cm->flags & METHOD_ATTRIBUTE_VIRTUAL)) {
649                         for (k = class->parent; k ; k = k->parent) {
650                                 int j;
651                                 for (j = 0; j < k->method.count; ++j) {
652                                         MonoMethod *m1 = k->methods [j];
653                                         if (!(m1->flags & METHOD_ATTRIBUTE_VIRTUAL))
654                                                 continue;
655                                         if (!strcmp(cm->name, m1->name) && 
656                                             mono_metadata_signature_equal (cm->signature, m1->signature)) {
657                                                 cm->slot = k->methods [j]->slot;
658                                                 g_assert (cm->slot < max_vtsize);
659                                                 break;
660                                         }
661                                 }
662                                 if (cm->slot >= 0) 
663                                         break;
664                         }
665                 }
666
667                 if (cm->slot < 0)
668                         cm->slot = cur_slot++;
669
670                 if (!(cm->flags & METHOD_ATTRIBUTE_ABSTRACT))
671                         vtable [cm->slot] = cm;
672         }
673
674
675         class->vtable_size = cur_slot;
676         class->vtable = g_malloc0 (sizeof (gpointer) * class->vtable_size);
677         memcpy (class->vtable, vtable,  sizeof (gpointer) * class->vtable_size);
678
679         class->inited = 1;
680         class->init_pending = 0;
681
682         if (mono_print_vtable) {
683                 int icount = 0;
684
685                 for (i = 0; i <= max_iid; i++)
686                         if (class->interface_offsets [i] != -1)
687                                 icount++;
688
689                 printf ("VTable %s.%s (size = %d, interfaces = %d)\n", class->name_space, 
690                         class->name, class->vtable_size, icount); 
691
692                 for (i = 0; i < class->vtable_size; ++i) {
693                         MonoMethod *cm;
694                
695                         cm = vtable [i];
696                         if (cm) {
697                                 printf ("  slot %03d(%03d) %s.%s:%s\n", i, cm->slot,
698                                         cm->klass->name_space, cm->klass->name,
699                                         cm->name);
700                         }
701                 }
702
703
704                 if (icount) {
705                         printf ("Interfaces %s.%s (max_iid = %d)\n", class->name_space, 
706                                 class->name, max_iid);
707         
708                         for (i = 0; i < class->interface_count; i++) {
709                                 ic = class->interfaces [i];
710                                 printf ("  slot %03d(%03d) %s.%s\n",  
711                                         class->interface_offsets [ic->interface_id],
712                                         ic->method.count, ic->name_space, ic->name);
713                         }
714
715                         for (k = class->parent; k ; k = k->parent) {
716                                 for (i = 0; i < k->interface_count; i++) {
717                                         ic = k->interfaces [i]; 
718                                         printf ("  slot %03d(%03d) %s.%s\n", 
719                                                 class->interface_offsets [ic->interface_id],
720                                                 ic->method.count, ic->name_space, ic->name);
721                                 }
722                         }
723                 }
724         }
725
726         if (!default_ghc) {
727                 if (class == mono_defaults.object_class) { 
728                        
729                         for (i = 0; i < class->vtable_size; ++i) {
730                                 MonoMethod *cm = vtable [i];
731                
732                                 if (!strcmp (cm->name, "GetHashCode")) {
733                                         ghc_slot = i;
734                                         break;
735                                 }
736                         }
737
738                         g_assert (ghc_slot > 0);
739
740                         default_ghc = vtable [ghc_slot];
741                 }
742         }
743         
744         class->ghcimpl = 1;
745         if (class != mono_defaults.object_class) { 
746
747                 if (vtable [ghc_slot] == default_ghc) {
748                         class->ghcimpl = 0;
749                 }
750         }
751
752         if (!default_finalize) {
753                 if (class == mono_defaults.object_class) { 
754                        
755                         for (i = 0; i < class->vtable_size; ++i) {
756                                 MonoMethod *cm = vtable [i];
757                
758                                 if (!strcmp (cm->name, "Finalize")) {
759                                         finalize_slot = i;
760                                         break;
761                                 }
762                         }
763
764                         g_assert (finalize_slot > 0);
765
766                         default_finalize = vtable [finalize_slot];
767                 }
768         }
769
770         /* Object::Finalize should have empty implemenatation */
771         class->has_finalize = 0;
772         if (class != mono_defaults.object_class) { 
773                 if (vtable [finalize_slot] != default_finalize)
774                         class->has_finalize = 1;
775         }
776 }
777
778
779 /*
780  * Compute a relative numbering of the class hierarchy as described in
781  * "Java for Large-Scale Scientific Computations?"
782  */
783 static void
784 mono_compute_relative_numbering (MonoClass *class, int *c)
785 {
786         GList *s;
787
788         (*c)++;
789
790         class->baseval = *c;
791
792         for (s = class->subclasses; s; s = s->next)
793                 mono_compute_relative_numbering ((MonoClass *)s->data, c); 
794         
795         class->diffval = *c -  class->baseval;
796 }
797
798 void
799 mono_class_setup_mono_type (MonoClass *class)
800 {
801         const char *name = class->name;
802         const char *nspace = class->name_space;
803
804         class->this_arg.byref = 1;
805         class->this_arg.data.klass = class;
806         class->this_arg.type = MONO_TYPE_CLASS;
807         class->byval_arg.data.klass = class;
808         class->byval_arg.type = MONO_TYPE_CLASS;
809
810         if (!strcmp (nspace, "System")) {
811                 if (!strcmp (name, "ValueType")) {
812                         /*
813                          * do not set the valuetype bit for System.ValueType.
814                          * class->valuetype = 1;
815                          */
816                 } else if (!strcmp (name, "Enum")) {
817                         /*
818                          * do not set the valuetype bit for System.Enum.
819                          * class->valuetype = 1;
820                          */
821                         class->valuetype = 0;
822                         class->enumtype = 0;
823                 } else if (!strcmp (name, "Object")) {
824                         class->this_arg.type = class->byval_arg.type = MONO_TYPE_OBJECT;
825                 } else if (!strcmp (name, "String")) {
826                         class->this_arg.type = class->byval_arg.type = MONO_TYPE_STRING;
827                 }
828         }
829         
830         if (class->valuetype) {
831                 int t = MONO_TYPE_VALUETYPE;
832                 if (!strcmp (nspace, "System")) {
833                         switch (*name) {
834                         case 'B':
835                                 if (!strcmp (name, "Boolean")) {
836                                         t = MONO_TYPE_BOOLEAN;
837                                 } else if (!strcmp(name, "Byte")) {
838                                         t = MONO_TYPE_U1;
839                                 }
840                                 break;
841                         case 'C':
842                                 if (!strcmp (name, "Char")) {
843                                         t = MONO_TYPE_CHAR;
844                                 }
845                                 break;
846                         case 'D':
847                                 if (!strcmp (name, "Double")) {
848                                         t = MONO_TYPE_R8;
849                                 }
850                                 break;
851                         case 'I':
852                                 if (!strcmp (name, "Int32")) {
853                                         t = MONO_TYPE_I4;
854                                 } else if (!strcmp(name, "Int16")) {
855                                         t = MONO_TYPE_I2;
856                                 } else if (!strcmp(name, "Int64")) {
857                                         t = MONO_TYPE_I8;
858                                 } else if (!strcmp(name, "IntPtr")) {
859                                         t = MONO_TYPE_I;
860                                 }
861                                 break;
862                         case 'S':
863                                 if (!strcmp (name, "Single")) {
864                                         t = MONO_TYPE_R4;
865                                 } else if (!strcmp(name, "SByte")) {
866                                         t = MONO_TYPE_I1;
867                                 }
868                                 break;
869                         case 'U':
870                                 if (!strcmp (name, "UInt32")) {
871                                         t = MONO_TYPE_U4;
872                                 } else if (!strcmp(name, "UInt16")) {
873                                         t = MONO_TYPE_U2;
874                                 } else if (!strcmp(name, "UInt64")) {
875                                         t = MONO_TYPE_U8;
876                                 } else if (!strcmp(name, "UIntPtr")) {
877                                         t = MONO_TYPE_U;
878                                 }
879                                 break;
880                         case 'V':
881                                 if (!strcmp (name, "Void")) {
882                                         t = MONO_TYPE_VOID;
883                                 }
884                                 break;
885                         default:
886                                 break;
887                         }
888                 }
889                 class->this_arg.type = class->byval_arg.type = t;
890         }
891 }
892
893 void
894 mono_class_setup_parent (MonoClass *class, MonoClass *parent)
895 {
896         gboolean system_namespace;
897
898         system_namespace = !strcmp (class->name_space, "System");
899
900         /* if root of the hierarchy */
901         if (system_namespace && !strcmp (class->name, "Object")) {
902                 class->parent = NULL;
903                 class->instance_size = sizeof (MonoObject);
904                 return;
905         }
906         if (!strcmp (class->name, "<Module>")) {
907                 class->parent = NULL;
908                 class->instance_size = 0;
909                 return;
910         }
911
912         if (!(class->flags & TYPE_ATTRIBUTE_INTERFACE)) {
913                 int rnum = 0;
914                 class->parent = parent;
915
916                 if (!parent)
917                         g_assert_not_reached (); /* FIXME */
918
919                 class->marshalbyref = parent->marshalbyref;
920                 class->contextbound  = parent->contextbound;
921                 class->delegate  = parent->delegate;
922                 
923                 if (system_namespace) {
924                         if (*class->name == 'M' && !strcmp (class->name, "MarshalByRefObject"))
925                                 class->marshalbyref = 1;
926
927                         if (*class->name == 'C' && !strcmp (class->name, "ContextBoundObject")) 
928                                 class->contextbound  = 1;
929
930                         if (*class->name == 'D' && !strcmp (class->name, "Delegate")) 
931                                 class->delegate  = 1;
932                 }
933
934                 if (class->parent->enumtype || ((strcmp (class->parent->name, "ValueType") == 0) && 
935                                                 (strcmp (class->parent->name_space, "System") == 0)))
936                         class->valuetype = 1;
937                 if (((strcmp (class->parent->name, "Enum") == 0) && (strcmp (class->parent->name_space, "System") == 0))) {
938                         class->valuetype = class->enumtype = 1;
939                 }
940                 /*class->enumtype = class->parent->enumtype; */
941                 class->parent->subclasses = g_list_prepend (class->parent->subclasses, class);
942                 mono_compute_relative_numbering (mono_defaults.object_class, &rnum);
943         } else {
944                 class->parent = NULL;
945         }
946
947 }
948
949 /**
950  * @image: context where the image is created
951  * @type_token:  typedef token
952  */
953 static MonoClass *
954 mono_class_create_from_typedef (MonoImage *image, guint32 type_token)
955 {
956         MonoTableInfo *tt = &image->tables [MONO_TABLE_TYPEDEF];
957         MonoClass *class, *parent = NULL;
958         guint32 cols [MONO_TYPEDEF_SIZE];
959         guint32 cols_next [MONO_TYPEDEF_SIZE];
960         guint tidx = mono_metadata_token_index (type_token);
961         const char *name, *nspace;
962         guint icount = 0; 
963         MonoClass **interfaces;
964
965         if ((class = g_hash_table_lookup (image->class_cache, GUINT_TO_POINTER (type_token))))
966                 return class;
967
968         g_assert (mono_metadata_token_table (type_token) == MONO_TABLE_TYPEDEF);
969         
970         mono_metadata_decode_row (tt, tidx - 1, cols, MONO_TYPEDEF_SIZE);
971         
972         name = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
973         nspace = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
974
975         if (cols [MONO_TYPEDEF_EXTENDS])
976                 parent = mono_class_get (image, mono_metadata_token_from_dor (cols [MONO_TYPEDEF_EXTENDS]));
977         interfaces = mono_metadata_interfaces_from_typedef (image, type_token, &icount);
978
979         class = g_malloc0 (sizeof (MonoClass));
980                            
981         g_hash_table_insert (image->class_cache, GUINT_TO_POINTER (type_token), class);
982
983         class->interfaces = interfaces;
984         class->interface_count = icount;
985
986         class->name = name;
987         class->name_space = nspace;
988
989         class->image = image;
990         class->type_token = type_token;
991         class->flags = cols [MONO_TYPEDEF_FLAGS];
992
993         class->element_class = class;
994
995         /*g_print ("Init class %s\n", name);*/
996
997         mono_class_setup_parent (class, parent);
998
999         mono_class_setup_mono_type (class);
1000
1001         /*
1002          * Compute the field and method lists
1003          */
1004         class->field.first  = cols [MONO_TYPEDEF_FIELD_LIST] - 1;
1005         class->method.first = cols [MONO_TYPEDEF_METHOD_LIST] - 1;
1006
1007         if (tt->rows > tidx){           
1008                 mono_metadata_decode_row (tt, tidx, cols_next, CSIZE (cols_next));
1009                 class->field.last  = cols_next [MONO_TYPEDEF_FIELD_LIST] - 1;
1010                 class->method.last = cols_next [MONO_TYPEDEF_METHOD_LIST] - 1;
1011         } else {
1012                 class->field.last  = image->tables [MONO_TABLE_FIELD].rows;
1013                 class->method.last = image->tables [MONO_TABLE_METHOD].rows;
1014         }
1015
1016         if (cols [MONO_TYPEDEF_FIELD_LIST] && 
1017             cols [MONO_TYPEDEF_FIELD_LIST] <= image->tables [MONO_TABLE_FIELD].rows)
1018                 class->field.count = class->field.last - class->field.first;
1019         else
1020                 class->field.count = 0;
1021
1022         if (cols [MONO_TYPEDEF_METHOD_LIST] <= image->tables [MONO_TABLE_METHOD].rows)
1023                 class->method.count = class->method.last - class->method.first;
1024         else
1025                 class->method.count = 0;
1026
1027         /* reserve space to store vector pointer in arrays */
1028         if (!strcmp (nspace, "System") && !strcmp (name, "Array")) {
1029                 class->instance_size += 2 * sizeof (gpointer);
1030                 g_assert (class->field.count == 0);
1031         }
1032
1033         if (class->flags & TYPE_ATTRIBUTE_INTERFACE)
1034                 class->interface_id = mono_get_unique_iid (class);
1035
1036         /*class->interfaces = mono_metadata_interfaces_from_typedef (image, type_token, &class->interface_count); */
1037
1038         if (class->enumtype) {
1039                 class->fields = g_new0 (MonoClassField, class->field.count);
1040                 class_compute_field_layout (class, 0);
1041         } 
1042
1043         if ((type_token = mono_metadata_nested_in_typedef (image, type_token)))
1044                 class->nested_in = mono_class_create_from_typedef (image, type_token);
1045         return class;
1046 }
1047
1048 MonoClass *
1049 mono_ptr_class_get (MonoType *type)
1050 {
1051         MonoClass *result;
1052         MonoClass *el_class;
1053         static GHashTable *ptr_hash = NULL;
1054
1055         if (!ptr_hash)
1056                 ptr_hash = g_hash_table_new (g_direct_hash, g_direct_equal);
1057         el_class = mono_class_from_mono_type (type);
1058         if ((result = g_hash_table_lookup (ptr_hash, el_class)))
1059                 return result;
1060         result = g_new0 (MonoClass, 1);
1061
1062         result->parent = NULL; /* no parent for PTR types */
1063         result->name = "System";
1064         result->name_space = "MonoPtrFakeClass";
1065         result->image = el_class->image;
1066         result->inited = TRUE;
1067         /* Can pointers get boxed? */
1068         result->instance_size = sizeof (gpointer);
1069         /*
1070          * baseval, diffval: need them to allow casting ?
1071          */
1072         result->element_class = el_class;
1073         result->enum_basetype = &result->element_class->byval_arg;
1074
1075         result->this_arg.type = result->byval_arg.type = MONO_TYPE_PTR;
1076         result->this_arg.data.type = result->byval_arg.data.type = result->enum_basetype;
1077         result->this_arg.byref = TRUE;
1078
1079         g_hash_table_insert (ptr_hash, el_class, result);
1080
1081         return result;
1082 }
1083
1084 MonoClass *
1085 mono_class_from_mono_type (MonoType *type)
1086 {
1087         switch (type->type) {
1088         case MONO_TYPE_OBJECT:
1089                 return mono_defaults.object_class;
1090         case MONO_TYPE_VOID:
1091                 return mono_defaults.void_class;
1092         case MONO_TYPE_BOOLEAN:
1093                 return mono_defaults.boolean_class;
1094         case MONO_TYPE_CHAR:
1095                 return mono_defaults.char_class;
1096         case MONO_TYPE_I1:
1097                 return mono_defaults.sbyte_class;
1098         case MONO_TYPE_U1:
1099                 return mono_defaults.byte_class;
1100         case MONO_TYPE_I2:
1101                 return mono_defaults.int16_class;
1102         case MONO_TYPE_U2:
1103                 return mono_defaults.uint16_class;
1104         case MONO_TYPE_I4:
1105                 return mono_defaults.int32_class;
1106         case MONO_TYPE_U4:
1107                 return mono_defaults.uint32_class;
1108         case MONO_TYPE_I:
1109                 return mono_defaults.int_class;
1110         case MONO_TYPE_U:
1111                 return mono_defaults.uint_class;
1112         case MONO_TYPE_I8:
1113                 return mono_defaults.int64_class;
1114         case MONO_TYPE_U8:
1115                 return mono_defaults.uint64_class;
1116         case MONO_TYPE_R4:
1117                 return mono_defaults.single_class;
1118         case MONO_TYPE_R8:
1119                 return mono_defaults.double_class;
1120         case MONO_TYPE_STRING:
1121                 return mono_defaults.string_class;
1122         case MONO_TYPE_ARRAY:
1123                 return mono_array_class_get (type->data.array->type, type->data.array->rank);
1124         case MONO_TYPE_PTR:
1125                 return mono_ptr_class_get (type->data.type);
1126         case MONO_TYPE_SZARRAY:
1127                 return mono_array_class_get (type->data.type, 1);
1128         case MONO_TYPE_CLASS:
1129         case MONO_TYPE_VALUETYPE:
1130                 return type->data.klass;
1131         default:
1132                 g_warning ("implement me %02x\n", type->type);
1133                 g_assert_not_reached ();
1134         }
1135         
1136         return NULL;
1137 }
1138
1139 /**
1140  * @image: context where the image is created
1141  * @type_spec:  typespec token
1142  */
1143 static MonoClass *
1144 mono_class_create_from_typespec (MonoImage *image, guint32 type_spec)
1145 {
1146         MonoType *type;
1147         MonoClass *class;
1148
1149         type = mono_type_create_from_typespec (image, type_spec);
1150
1151         switch (type->type) {
1152         case MONO_TYPE_ARRAY:
1153                 class = mono_array_class_get (type->data.array->type, type->data.array->rank);
1154                 break;
1155         case MONO_TYPE_SZARRAY:
1156                 class = mono_array_class_get (type->data.type, 1);
1157                 break;
1158         case MONO_TYPE_PTR:
1159                 class = mono_class_from_mono_type (type->data.type);
1160                 break;
1161         default:
1162                 /* it seems any type can be stored in TypeSpec as well */
1163                 class = mono_class_from_mono_type (type);
1164                 break;
1165         }
1166
1167         mono_metadata_free_type (type);
1168         
1169         return class;
1170 }
1171
1172 /**
1173  * mono_array_class_get:
1174  * @element_type: element type 
1175  * @rank: the dimension of the array class
1176  *
1177  * Returns: a class object describing the array with element type @element_type and 
1178  * dimension @rank. 
1179  */
1180 MonoClass *
1181 mono_array_class_get (MonoType *element_type, guint32 rank)
1182 {
1183         MonoClass *eclass;
1184         MonoImage *image;
1185         MonoClass *class;
1186         MonoClass *parent = NULL;
1187         GSList *list;
1188         int rnum = 0;
1189
1190         eclass = mono_class_from_mono_type (element_type);
1191         g_assert (rank <= 255);
1192
1193         parent = mono_defaults.array_class;
1194
1195         if (!parent->inited)
1196                 mono_class_init (parent);
1197
1198         image = eclass->image;
1199
1200         if ((list = g_hash_table_lookup (image->array_cache, element_type))) {
1201                 for (; list; list = list->next) {
1202                         class = list->data;
1203                         if (class->rank == rank)
1204                                 return class;
1205                 }
1206         }
1207         
1208         class = g_malloc0 (sizeof (MonoClass) + parent->vtable_size * sizeof (gpointer));
1209
1210         class->image = image;
1211         class->name_space = "System";
1212         class->name = "Array";
1213         class->type_token = 0;
1214         class->flags = TYPE_ATTRIBUTE_CLASS;
1215         class->parent = parent;
1216         class->instance_size = mono_class_instance_size (class->parent);
1217         class->class_size = 0;
1218         class->vtable_size = parent->vtable_size;
1219         class->parent->subclasses = g_list_prepend (class->parent->subclasses, class);
1220         mono_compute_relative_numbering (mono_defaults.object_class, &rnum);
1221
1222         class->rank = rank;
1223         class->element_class = eclass;
1224         if (rank > 1) {
1225                 MonoArrayType *at = g_new0 (MonoArrayType, 1);
1226                 class->byval_arg.type = MONO_TYPE_ARRAY;
1227                 class->byval_arg.data.array = at;
1228                 at->type = &eclass->byval_arg;
1229                 at->rank = rank;
1230                 /* FIXME: complete.... */
1231         } else {
1232                 /* FIXME: this is not correct. the lbound could be >0 */
1233                 class->byval_arg.type = MONO_TYPE_SZARRAY;
1234                 class->byval_arg.data.type = &eclass->byval_arg;
1235         }
1236         class->this_arg = class->byval_arg;
1237         class->this_arg.byref = 1;
1238
1239         list = g_slist_append (list, class);
1240         g_hash_table_insert (image->array_cache, &class->element_class->byval_arg, list);
1241         return class;
1242 }
1243
1244 /**
1245  * mono_class_instance_size:
1246  * @klass: a class 
1247  * 
1248  * Returns: the size of an object instance
1249  */
1250 gint32
1251 mono_class_instance_size (MonoClass *klass)
1252 {
1253         
1254         if (!klass->size_inited)
1255                 mono_class_init (klass);
1256
1257         return klass->instance_size;
1258 }
1259
1260 /**
1261  * mono_class_value_size:
1262  * @klass: a class 
1263  *
1264  * This function is used for value types, and return the
1265  * space and the alignment to store that kind of value object.
1266  *
1267  * Returns: the size of a value of kind @klass
1268  */
1269 gint32
1270 mono_class_value_size      (MonoClass *klass, guint32 *align)
1271 {
1272         gint32 size;
1273
1274         /* fixme: check disable, because we still have external revereces to
1275          * mscorlib and Dummy Objects 
1276          */
1277         /*g_assert (klass->valuetype);*/
1278
1279         size = mono_class_instance_size (klass) - sizeof (MonoObject);
1280
1281         if (align)
1282                 *align = klass->min_align;
1283
1284         return size;
1285 }
1286
1287 /**
1288  * mono_class_data_size:
1289  * @klass: a class 
1290  * 
1291  * Returns: the size of the static class data
1292  */
1293 gint32
1294 mono_class_data_size (MonoClass *klass)
1295 {
1296         
1297         if (!klass->inited)
1298                 mono_class_init (klass);
1299
1300         return klass->class_size;
1301 }
1302
1303 /*
1304  * Auxiliary routine to mono_class_get_field
1305  *
1306  * Takes a field index instead of a field token.
1307  */
1308 static MonoClassField *
1309 mono_class_get_field_idx (MonoClass *class, int idx)
1310 {
1311         if (class->field.count){
1312                 if ((idx >= class->field.first) && (idx < class->field.last)){
1313                         return &class->fields [idx - class->field.first];
1314                 }
1315         }
1316
1317         if (!class->parent)
1318                 return NULL;
1319         
1320         return mono_class_get_field_idx (class->parent, idx);
1321 }
1322
1323 /**
1324  * mono_class_get_field:
1325  * @class: the class to lookup the field.
1326  * @field_token: the field token
1327  *
1328  * Returns: A MonoClassField representing the type and offset of
1329  * the field, or a NULL value if the field does not belong to this
1330  * class.
1331  */
1332 MonoClassField *
1333 mono_class_get_field (MonoClass *class, guint32 field_token)
1334 {
1335         int idx = mono_metadata_token_index (field_token);
1336
1337         g_assert (mono_metadata_token_code (field_token) == MONO_TOKEN_FIELD_DEF);
1338
1339         return mono_class_get_field_idx (class, idx - 1);
1340 }
1341
1342 MonoClassField *
1343 mono_class_get_field_from_name (MonoClass *klass, const char *name)
1344 {
1345         int i;
1346
1347         for (i = 0; i < klass->field.count; ++i) {
1348                 if (strcmp (name, klass->fields [i].name) == 0)
1349                         return &klass->fields [i];
1350         }
1351         return NULL;
1352 }
1353
1354 /**
1355  * mono_class_get:
1356  * @image: the image where the class resides
1357  * @type_token: the token for the class
1358  * @at: an optional pointer to return the array element type
1359  *
1360  * Returns: the MonoClass that represents @type_token in @image
1361  */
1362 MonoClass *
1363 mono_class_get (MonoImage *image, guint32 type_token)
1364 {
1365         MonoClass *class;
1366
1367         switch (type_token & 0xff000000){
1368         case MONO_TOKEN_TYPE_DEF:
1369                 class = mono_class_create_from_typedef (image, type_token);
1370                 break;          
1371         case MONO_TOKEN_TYPE_REF:
1372                 class = mono_class_from_typeref (image, type_token);
1373                 break;
1374         case MONO_TOKEN_TYPE_SPEC:
1375                 class = mono_class_create_from_typespec (image, type_token);
1376                 break;
1377         default:
1378                 g_warning ("unknown token type %x", type_token & 0xff000000);
1379                 g_assert_not_reached ();
1380         }
1381
1382         if (!class)
1383                 g_warning ("Could not load class from token 0x%08x in %s", type_token, image->name);
1384         return class;
1385 }
1386
1387 MonoClass *
1388 mono_class_from_name_case (MonoImage *image, const char* name_space, const char *name)
1389 {
1390         MonoTableInfo  *t = &image->tables [MONO_TABLE_TYPEDEF];
1391         guint32 cols [MONO_TYPEDEF_SIZE];
1392         const char *n;
1393         const char *nspace;
1394         guint32 i, visib;
1395
1396         /* add a cache if needed */
1397         for (i = 1; i <= t->rows; ++i) {
1398                 mono_metadata_decode_row (t, i - 1, cols, MONO_TYPEDEF_SIZE);
1399                 /* nested types are accessed from the nesting name */
1400                 visib = cols [MONO_TYPEDEF_FLAGS] & TYPE_ATTRIBUTE_VISIBILITY_MASK;
1401                 if (visib > TYPE_ATTRIBUTE_PUBLIC && visib < TYPE_ATTRIBUTE_NESTED_ASSEMBLY)
1402                         continue;
1403                 n = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
1404                 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
1405                 if (g_strcasecmp (n, name) == 0 && g_strcasecmp (nspace, name_space) == 0)
1406                         return mono_class_get (image, MONO_TOKEN_TYPE_DEF | i);
1407         }
1408         return NULL;
1409 }
1410
1411 MonoClass *
1412 mono_class_from_name (MonoImage *image, const char* name_space, const char *name)
1413 {
1414         GHashTable *nspace_table;
1415         guint32 token;
1416
1417         nspace_table = g_hash_table_lookup (image->name_cache, name_space);
1418         if (!nspace_table)
1419                 return 0;
1420         token = GPOINTER_TO_UINT (g_hash_table_lookup (nspace_table, name));
1421         
1422         if (!token) {
1423                 /*g_warning ("token not found for %s.%s in image %s", name_space, name, image->name);*/
1424                 return NULL;
1425         }
1426
1427         token = MONO_TOKEN_TYPE_DEF | token;
1428
1429         return mono_class_get (image, token);
1430 }
1431
1432 /**
1433  * mono_array_element_size:
1434  * @ac: pointer to a #MonoArrayClass
1435  *
1436  * Returns: the size of single array element.
1437  */
1438 gint32
1439 mono_array_element_size (MonoClass *ac)
1440 {
1441         if (ac->element_class->valuetype)
1442                 return mono_class_instance_size (ac->element_class) - sizeof (MonoObject);
1443         else
1444                 return sizeof (gpointer);
1445 }
1446
1447 gpointer
1448 mono_ldtoken (MonoImage *image, guint32 token, MonoClass **handle_class)
1449 {
1450         switch (token & 0xff000000) {
1451         case MONO_TOKEN_TYPE_DEF:
1452         case MONO_TOKEN_TYPE_REF: {
1453                 MonoClass *class;
1454                 if (handle_class)
1455                         *handle_class = mono_defaults.typehandle_class;
1456                 class = mono_class_get (image, token);
1457                 mono_class_init (class);
1458                 /* We return a MonoType* as handle */
1459                 return &class->byval_arg;
1460         }
1461         case MONO_TOKEN_TYPE_SPEC: {
1462                 MonoClass *class;
1463                 if (handle_class)
1464                         *handle_class = mono_defaults.typehandle_class;
1465                 class = mono_class_create_from_typespec (image, token);
1466                 mono_class_init (class);
1467                 return &class->byval_arg;
1468         }
1469         case MONO_TOKEN_FIELD_DEF: {
1470                 MonoClass *class;
1471                 guint32 type = mono_metadata_typedef_from_field (image, mono_metadata_token_index (token));
1472                 class = mono_class_get (image, MONO_TOKEN_TYPE_DEF | type);
1473                 mono_class_init (class);
1474                 if (handle_class)
1475                                 *handle_class = mono_class_from_name (mono_defaults.corlib, "System", "RuntimeFieldHandle");
1476                 return mono_class_get_field (class, token);
1477         }
1478         case MONO_TOKEN_METHOD_DEF:
1479         case MONO_TOKEN_MEMBER_REF:
1480         default:
1481                 g_warning ("Unknown token 0x%08x in ldtoken", token);
1482                 break;
1483         }
1484         return NULL;
1485 }
1486