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