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