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