ce6cd34a15b78a8b6ab181ed88c0f72e3f3c7512
[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
30 #define CSIZE(x) (sizeof (x) / 4)
31
32 static MonoClass * mono_class_create_from_typedef (MonoImage *image, guint32 type_token);
33
34 static gpointer
35 default_trampoline (MonoMethod *method)
36 {
37         return method;
38 }
39
40 static void
41 default_runtime_class_init (MonoClass *klass)
42 {
43         return;
44 }
45
46 static MonoTrampoline arch_create_jit_trampoline = default_trampoline;
47 static MonoRuntimeClassInit mono_runtime_class_init = default_runtime_class_init;
48
49 void
50 mono_install_trampoline (MonoTrampoline func) 
51 {
52         arch_create_jit_trampoline = func? func: default_trampoline;
53 }
54
55 void
56 mono_install_runtime_class_init (MonoRuntimeClassInit func)
57 {
58         mono_runtime_class_init = func? func: default_runtime_class_init;
59 }
60
61 static MonoClass *
62 mono_class_create_from_typeref (MonoImage *image, guint32 type_token)
63 {
64         guint32 cols [MONO_TYPEREF_SIZE];
65         MonoTableInfo  *t = &image->tables [MONO_TABLE_TYPEREF];
66         guint32 idx;
67         const char *name, *nspace;
68         MonoClass *res;
69
70         mono_metadata_decode_row (t, (type_token&0xffffff)-1, cols, MONO_TYPEREF_SIZE);
71         g_assert ((cols [MONO_TYPEREF_SCOPE] & 0x3) == 2);
72         idx = cols [MONO_TYPEREF_SCOPE] >> 2;
73
74         if (!image->references ||  !image->references [idx-1]) {
75                 /* 
76                  * detected a reference to mscorlib, we simply return a reference to a dummy 
77                  * until we have a better solution.
78                  */
79                 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])); 
80                 res = mono_class_from_name (image, "System", "MonoDummy");
81                 /* prevent method loading */
82                 res->dummy = 1;
83                 /* some storage if the type is used  - very ugly hack */
84                 res->instance_size = 2*sizeof (gpointer);
85                 return res;
86         }       
87
88         name = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAME]);
89         nspace = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAMESPACE]);
90         
91         /* load referenced assembly */
92         image = image->references [idx-1]->image;
93
94         return mono_class_from_name (image, nspace, name);
95 }
96
97 /** 
98  * class_compute_field_layout:
99  * @m: pointer to the metadata.
100  * @class: The class to initialize
101  *
102  * Initializes the class->fields.
103  *
104  * Currently we only support AUTO_LAYOUT, and do not even try to do
105  * a good job at it.  This is temporary to get the code for Paolo.
106  */
107 static void
108 class_compute_field_layout (MonoClass *class)
109 {
110         MonoImage *m = class->image; 
111         const int top = class->field.count;
112         guint32 layout = class->flags & TYPE_ATTRIBUTE_LAYOUT_MASK;
113         MonoTableInfo *t = &m->tables [MONO_TABLE_FIELD];
114         int i;
115
116         /*
117          * Fetch all the field information.
118          */
119         for (i = 0; i < top; i++){
120                 const char *sig;
121                 guint32 cols [MONO_FIELD_SIZE];
122                 int idx = class->field.first + i;
123                 
124                 mono_metadata_decode_row (t, idx, cols, CSIZE (cols));
125                 /* The name is needed for fieldrefs */
126                 class->fields [i].name = mono_metadata_string_heap (m, cols [MONO_FIELD_NAME]);
127                 sig = mono_metadata_blob_heap (m, cols [MONO_FIELD_SIGNATURE]);
128                 mono_metadata_decode_value (sig, &sig);
129                 /* FIELD signature == 0x06 */
130                 g_assert (*sig == 0x06);
131                 class->fields [i].type = mono_metadata_parse_field_type (
132                         m, cols [MONO_FIELD_FLAGS], sig + 1, &sig);
133                 if (cols [MONO_FIELD_FLAGS] & FIELD_ATTRIBUTE_HAS_FIELD_RVA) {
134                         mono_metadata_field_info (m, idx, NULL, &class->fields [i].data, NULL);
135                         if (!class->fields [i].data)
136                                 g_warning ("field %s in %s should have RVA data, but hasn't", class->fields [i].name, class->name);
137                 }
138                 if (class->enumtype && !(cols [MONO_FIELD_FLAGS] & FIELD_ATTRIBUTE_STATIC))
139                         class->enum_basetype = class->fields [i].type;
140         }
141         if (class->enumtype && !class->enum_basetype) {
142                 if (!((strcmp (class->name, "Enum") == 0) && (strcmp (class->name_space, "System") == 0)))
143                         G_BREAKPOINT ();
144         }
145         /*
146          * Compute field layout and total size.
147          */
148         switch (layout){
149         case TYPE_ATTRIBUTE_AUTO_LAYOUT:
150         case TYPE_ATTRIBUTE_SEQUENTIAL_LAYOUT:
151                 for (i = 0; i < top; i++){
152                         int size, align;
153                         
154                         size = mono_type_size (class->fields [i].type, &align);
155                         if (class->fields [i].type->attrs & FIELD_ATTRIBUTE_STATIC) {
156                                 class->fields [i].offset = class->class_size;
157                                 class->class_size += (class->class_size % align);
158                                 class->class_size += size;
159                         } else {
160                                 class->min_align = MAX (align, class->min_align);
161                                 class->fields [i].offset = class->instance_size;
162                                 class->instance_size += (class->instance_size % align);
163                                 class->instance_size += size;
164                         }
165                 }
166                 if (class->instance_size & (class->min_align - 1)) {
167                         class->instance_size += class->min_align - 1;
168                         class->instance_size &= ~(class->min_align - 1);
169                 }
170                 break;
171         case TYPE_ATTRIBUTE_EXPLICIT_LAYOUT:
172                 for (i = 0; i < top; i++){
173                         int size, align;
174                         int idx = class->field.first + i;
175
176                         /*
177                          * There must be info about all the fields in a type if it
178                          * uses explicit layout.
179                          */
180
181                         size = mono_type_size (class->fields [i].type, &align);
182                         if (class->fields [i].type->attrs & FIELD_ATTRIBUTE_STATIC) {
183                                 class->fields [i].offset = class->class_size;
184                                 class->class_size += (class->class_size % align);
185                                 class->class_size += size;
186                         } else {
187                                 mono_metadata_field_info (m, idx, &class->fields [i].offset, NULL, NULL);
188                                 if (class->fields [i].offset == (guint32)-1)
189                                                 g_warning ("%s not initialized correctly (missing field layout info for %s)", class->name, class->fields [i].name);
190                                 /*
191                                  * The offset is from the start of the object: this works for both
192                                  * classes and valuetypes.
193                                  */
194                                 class->fields [i].offset += sizeof (MonoObject);
195                                 /*
196                                  * Calc max size.
197                                  */
198                                 size += class->fields [i].offset;
199                                 class->instance_size = MAX (class->instance_size, size);
200                         }
201                 }
202                 break;
203         }
204 }
205
206 static void
207 init_properties (MonoClass *class)
208 {
209         guint startm, endm, i, j;
210         guint32 cols [MONO_PROPERTY_SIZE];
211         MonoTableInfo *pt = &class->image->tables [MONO_TABLE_PROPERTY];
212         MonoTableInfo *msemt = &class->image->tables [MONO_TABLE_METHODSEMANTICS];
213
214         class->property.first = mono_metadata_properties_from_typedef (class->image, mono_metadata_token_index (class->type_token) - 1, &class->property.last);
215         class->property.count = class->property.last - class->property.first;
216
217         class->properties = g_new0 (MonoProperty, class->property.count);
218         for (i = class->property.first; i < class->property.last; ++i) {
219                 mono_metadata_decode_row (pt, i, cols, MONO_PROPERTY_SIZE);
220                 class->properties [i - class->property.first].attrs = cols [MONO_PROPERTY_FLAGS];
221                 class->properties [i - class->property.first].name = mono_metadata_string_heap (class->image, cols [MONO_PROPERTY_NAME]);
222
223                 startm = mono_metadata_methods_from_property (class->image, i, &endm);
224                 for (j = startm; j < endm; ++j) {
225                         mono_metadata_decode_row (msemt, j, cols, MONO_METHOD_SEMA_SIZE);
226                         switch (cols [MONO_METHOD_SEMA_SEMANTICS]) {
227                         case 1: /* set */
228                                 class->properties [i - class->property.first].set = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
229                                 break;
230                         case 2: /* get */
231                                 class->properties [i - class->property.first].get = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
232                                 break;
233                         default:
234                                 break;
235                         }
236                 }
237         }
238 }
239
240 void
241 mono_class_init (MonoClass *class)
242 {
243         MonoClass *k, *ic;
244         MonoMethod **tmp_vtable, **vtable = (MonoMethod **)class->vtable;
245         int i, max_iid, cur_slot = 0;
246
247         g_assert (class);
248
249         if (class->inited)
250                 return;
251         class->inited = 1;
252
253         if (class->parent) {
254                 if (!class->parent->inited)
255                         mono_class_init (class->parent);
256                 class->instance_size += class->parent->instance_size;
257                 class->class_size += class->parent->class_size;
258                 class->min_align = class->parent->min_align;
259                 cur_slot = class->parent->vtable_size;
260         } else
261                 class->min_align = 1;
262
263         /*
264          * Computes the size used by the fields, and their locations
265          */
266         if (class->field.count > 0){
267                 class->fields = g_new0 (MonoClassField, class->field.count);
268                 class_compute_field_layout (class);
269         }
270
271         if (class->class_size)
272                 class->data = g_malloc0 (class->class_size);
273
274         /* initialize method pointers */
275         class->methods = g_new (MonoMethod*, class->method.count);
276         for (i = 0; i < class->method.count; ++i)
277                 class->methods [i] = mono_get_method (class->image,
278                         MONO_TOKEN_METHOD_DEF | (i + class->method.first + 1), class);
279
280         if (class->flags & TYPE_ATTRIBUTE_INTERFACE) {
281                 for (i = 0; i < class->method.count; ++i)
282                         class->methods [i]->slot = i;
283                 return;
284         }
285
286         //printf ("METAINIT %s.%s\n", class->name_space, class->name);
287
288         /* compute maximum number of slots and maximum interface id */
289         max_iid = 0;
290         for (k = class; k ; k = k->parent) {
291                 for (i = 0; i < k->interface_count; i++) {
292                         ic = k->interfaces [i];
293
294                         if (!ic->inited)
295                                 mono_class_init (ic);
296
297                         if (max_iid < ic->interface_id)
298                                 max_iid = ic->interface_id;
299                 }
300         }
301         
302         class->max_interface_id = max_iid;
303         /* compute vtable offset for interfaces */
304         class->interface_offsets = g_malloc (sizeof (gpointer) * (max_iid + 1));
305
306         for (i = 0; i <= max_iid; i++)
307                 class->interface_offsets [i] = NULL;
308
309         for (i = 0; i < class->interface_count; i++) {
310                 ic = class->interfaces [i];
311                 class->interface_offsets [ic->interface_id] = &class->vtable [cur_slot];
312                 cur_slot += ic->method.count;
313         }
314
315         for (k = class->parent; k ; k = k->parent) {
316                 for (i = 0; i < k->interface_count; i++) {
317                         ic = k->interfaces [i]; 
318                         if (class->interface_offsets [ic->interface_id] == NULL) {
319                                 int io = (k->interface_offsets [ic->interface_id] - (gpointer)k->vtable)>>2;
320
321                                 g_assert (io >= 0);
322                                 g_assert (io <= class->vtable_size);
323
324                                 class->interface_offsets [ic->interface_id] = &class->vtable [io];
325                         }
326                 }
327         }
328
329         if (class->parent && class->parent->vtable_size)
330                 memcpy (class->vtable, class->parent->vtable,  sizeof (gpointer) * class->parent->vtable_size);
331  
332         tmp_vtable = alloca (class->vtable_size * sizeof (gpointer));
333         memset (tmp_vtable, 0, class->vtable_size * sizeof (gpointer));
334
335         for (k = class; k ; k = k->parent) {
336                 for (i = 0; i < k->interface_count; i++) {
337                         int j, l, io;
338                         ic = k->interfaces [i];
339
340                         io = (k->interface_offsets [ic->interface_id] - (gpointer)k->vtable)>>2;
341                         
342                         g_assert (io >= 0);
343                         g_assert (io <= class->vtable_size);
344
345                         if (k == class) {
346                                 for (l = 0; l < ic->method.count; l++) {
347                                         MonoMethod *im = ic->methods [l];                                               
348                                         for (j = 0; j < class->method.count; ++j) {
349                                                 MonoMethod *cm = class->methods [j];
350                                                 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
351                                                     !(cm->flags & METHOD_ATTRIBUTE_PUBLIC) ||
352                                                     !(cm->flags & METHOD_ATTRIBUTE_NEW_SLOT))
353                                                         continue;
354                                                 if (!strcmp(cm->name, im->name) && 
355                                                     mono_metadata_signature_equal (cm->signature, im->signature)) {
356                                                         g_assert (io + l <= class->vtable_size);
357                                                         tmp_vtable [io + l] = cm;
358                                                 }
359                                         }
360                                 }
361                         } else {
362                                 /* already implemented */
363                                 if (io >= k->vtable_size)
364                                         continue;
365                         }
366                                 
367                         for (l = 0; l < ic->method.count; l++) {
368                                 MonoMethod *im = ic->methods [l];                                               
369                                 MonoClass *k1;
370
371                                 g_assert (io + l <= class->vtable_size);
372
373                                 if (tmp_vtable [io + l] || vtable [io + l])
374                                         continue;
375                                         
376                                 for (k1 = class; k1; k1 = k1->parent) {
377                                         for (j = 0; j < k1->method.count; ++j) {
378                                                 MonoMethod *cm = k1->methods [j];
379
380                                                 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
381                                                     !(cm->flags & METHOD_ATTRIBUTE_PUBLIC))
382                                                         continue;
383                                                 
384                                                 if (!strcmp(cm->name, im->name) && 
385                                                     mono_metadata_signature_equal (cm->signature, im->signature)) {
386                                                         g_assert (io + l <= class->vtable_size);
387                                                         tmp_vtable [io + l] = cm;
388                                                         break;
389                                                 }
390                                                 
391                                         }
392                                         g_assert (io + l <= class->vtable_size);
393                                         if (tmp_vtable [io + l])
394                                                 break;
395                                 }
396                         }
397
398                         for (l = 0; l < ic->method.count; l++) {
399                                 MonoMethod *im = ic->methods [l];                                               
400                                 char *qname;
401                                 if (ic->name_space && ic->name_space [0])
402                                         qname = g_strconcat (ic->name_space, ".", ic->name, ".", im->name, NULL);
403                                 else
404                                         qname = g_strconcat (ic->name, ".", im->name, NULL); 
405
406                                 for (j = 0; j < class->method.count; ++j) {
407                                         MonoMethod *cm = class->methods [j];
408
409                                         if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL))
410                                                 continue;
411                                         
412                                         if (!strcmp (cm->name, qname) &&
413                                             mono_metadata_signature_equal (cm->signature, im->signature)) {
414                                                 g_assert (io + l <= class->vtable_size);
415                                                 tmp_vtable [io + l] = cm;
416                                                 break;
417                                         }
418                                 }
419                                 g_free (qname);
420                         }
421
422                         
423                         if (!(class->flags & TYPE_ATTRIBUTE_ABSTRACT)) {
424                                 for (l = 0; l < ic->method.count; l++) {
425                                         MonoMethod *im = ic->methods [l];                                               
426                                         g_assert (io + l <= class->vtable_size);
427                                         if (!(tmp_vtable [io + l] || vtable [io + l])) {
428                                                 printf ("no implementation for interface method %s.%s::%s in class %s.%s\n",
429                                                         ic->name_space, ic->name, im->name, class->name_space, class->name);
430                                                 
431                                                 for (j = 0; j < class->method.count; ++j) {
432                                                         MonoMethod *cm = class->methods [j];
433                                                         
434                                                         printf ("METHOD %s\n", cm->name);
435                                                 }
436                                                 g_assert_not_reached ();
437                                         }
438                                 }
439                         }
440                 
441                         for (l = 0; l < ic->method.count; l++) {
442                                 MonoMethod *im = tmp_vtable [io + l];
443
444                                 if (im) {
445                                         g_assert (io + l <= class->vtable_size);
446                                         if (im->slot < 0)
447                                                 im->slot = io + l;
448                                         if (!(im->flags & METHOD_ATTRIBUTE_ABSTRACT)) {
449                                                 //printf ("  ASLOT%d %s.%s:%s %s.%s:%s\n", io + l, ic->name_space, ic->name, 
450                                                 // im->name, im->klass->name_space,  im->klass->name, im->name);
451                                                 vtable [io + l] = arch_create_jit_trampoline (im);
452                                         }
453                                 }
454                         }
455                 }
456         } 
457
458         for (i = 0; i < class->method.count; ++i) {
459                 MonoMethod *cm;
460                
461                 cm = class->methods [i];
462
463                 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
464                     (cm->slot >= 0))
465                         continue;
466                 
467                 if (!(cm->flags & METHOD_ATTRIBUTE_NEW_SLOT)) {
468                         for (k = class->parent; k ; k = k->parent) {
469                                 int j;
470                                 for (j = 0; j < k->method.count; ++j) {
471                                         MonoMethod *m1 = k->methods [j];
472                                         if (!(m1->flags & METHOD_ATTRIBUTE_VIRTUAL))
473                                                 continue;
474                                         if (!strcmp(cm->name, m1->name) && 
475                                             mono_metadata_signature_equal (cm->signature, m1->signature)) {
476                                                 cm->slot = k->methods [j]->slot;
477                                                 g_assert (cm->slot < class->vtable_size);
478                                                 break;
479                                         }
480                                 }
481                                 if (cm->slot >= 0) 
482                                         break;
483                         }
484                 }
485
486                 if (cm->slot < 0)
487                         cm->slot = cur_slot++;
488
489                 if (!(cm->flags & METHOD_ATTRIBUTE_ABSTRACT))
490                         vtable [cm->slot] = arch_create_jit_trampoline (cm);
491         }
492
493         init_properties (class);
494
495         i = mono_metadata_nesting_typedef (class->image, class->type_token);
496         while (i) {
497                 MonoClass* nclass;
498                 guint32 cols [MONO_NESTED_CLASS_SIZE];
499                 mono_metadata_decode_row (&class->image->tables [MONO_TABLE_NESTEDCLASS], i - 1, cols, MONO_NESTED_CLASS_SIZE);
500                 if (cols [MONO_NESTED_CLASS_ENCLOSING] != mono_metadata_token_index (class->type_token))
501                         break;
502                 nclass = mono_class_create_from_typedef (class->image, MONO_TOKEN_TYPE_DEF | cols [MONO_NESTED_CLASS_NESTED]);
503                 class->nested_classes = g_list_prepend (class->nested_classes, nclass);
504                 ++i;
505         }
506
507         mono_runtime_class_init (class);
508
509         /*
510         printf ("VTABLE %s.%s\n", class->name_space, class->name); 
511
512         for (i = 0; i < class->vtable_size; ++i) {
513                 MonoMethod *cm;
514                
515                 cm = vtable [i];
516                 if (cm) {
517                         printf ("  METH%d %p %s %d\n", i, cm, cm->name, cm->slot);
518                 }
519         }
520         
521         printf ("METAEND %s.%s\n", class->name_space, class->name); 
522         */
523 }
524
525 /*
526  * Compute a relative numbering of the class hierarchy as described in
527  * "Java for Large-Scale Scientific Computations?"
528  */
529 static void
530 mono_compute_relative_numbering (MonoClass *class, int *c)
531 {
532         GList *s;
533
534         (*c)++;
535
536         class->baseval = *c;
537
538         for (s = class->subclasses; s; s = s->next)
539                 mono_compute_relative_numbering ((MonoClass *)s->data, c); 
540         
541         class->diffval = *c -  class->baseval;
542 }
543
544 /**
545  * @image: context where the image is created
546  * @type_token:  typedef token
547  */
548 static MonoClass *
549 mono_class_create_from_typedef (MonoImage *image, guint32 type_token)
550 {
551         MonoTableInfo *tt = &image->tables [MONO_TABLE_TYPEDEF];
552         MonoClass *class, *parent = NULL;
553         guint32 cols [MONO_TYPEDEF_SIZE];
554         guint32 cols_next [MONO_TYPEDEF_SIZE];
555         guint tidx = mono_metadata_token_index (type_token);
556         const char *name, *nspace;
557         guint vtsize = 0, icount = 0; 
558         static guint interface_id = 0;
559         MonoClass **interfaces;
560         int i;
561
562         if ((class = g_hash_table_lookup (image->class_cache, GUINT_TO_POINTER (type_token))))
563                 return class;
564
565         g_assert (mono_metadata_token_table (type_token) == MONO_TABLE_TYPEDEF);
566         
567         mono_metadata_decode_row (tt, tidx - 1, cols, CSIZE (cols));
568         
569         if (tt->rows > tidx) {          
570                 mono_metadata_decode_row (tt, tidx, cols_next, CSIZE (cols_next));
571                 vtsize += cols_next [MONO_TYPEDEF_METHOD_LIST] - cols [MONO_TYPEDEF_METHOD_LIST];
572         } else {
573                 vtsize += image->tables [MONO_TABLE_METHOD].rows - cols [MONO_TYPEDEF_METHOD_LIST] + 1;
574         }
575
576         name = mono_metadata_string_heap (image, cols[1]);
577         nspace = mono_metadata_string_heap (image, cols[2]);
578
579         if (!(!strcmp (nspace, "System") && !strcmp (name, "Object")) &&
580             !(cols [0] & TYPE_ATTRIBUTE_INTERFACE)) {
581                 parent = mono_class_get (image, mono_metadata_token_from_dor (cols [3]));
582         }
583         interfaces = mono_metadata_interfaces_from_typedef (image, type_token, &icount);
584
585         for (i = 0; i < icount; i++) 
586                 vtsize += interfaces [i]->method.count;
587         
588         if (parent)
589                 vtsize += parent->vtable_size;
590
591         if (cols [0] & TYPE_ATTRIBUTE_INTERFACE)
592                 vtsize = 0;
593
594         class = g_malloc0 (sizeof (MonoClass) + vtsize * sizeof (gpointer));
595         
596         g_hash_table_insert (image->class_cache, GUINT_TO_POINTER (type_token), class);
597
598         class->parent = parent;
599         class->interfaces = interfaces;
600         class->interface_count = icount;
601         class->vtable_size = vtsize;
602
603         class->this_arg.byref = 1;
604         class->this_arg.data.klass = class;
605         class->this_arg.type = MONO_TYPE_CLASS;
606         class->byval_arg.data.klass = class;
607         class->byval_arg.type = MONO_TYPE_CLASS;
608
609         class->name = name;
610         class->name_space = nspace;
611
612         class->image = image;
613         class->type_token = type_token;
614         class->flags = cols [0];
615
616         /*g_print ("Init class %s\n", name);*/
617
618         /* if root of the hierarchy */
619         if (!strcmp (nspace, "System") && !strcmp (name, "Object")) {
620                 class->parent = NULL;
621                 class->instance_size = sizeof (MonoObject);
622         } else if (!(cols [0] & TYPE_ATTRIBUTE_INTERFACE)) {
623                 int rnum = 0;
624                 class->parent = mono_class_get (image,  mono_metadata_token_from_dor (cols [3]));
625                 if ((strcmp (class->parent->name, "ValueType") == 0) && (strcmp (class->parent->name_space, "System") == 0))
626                         class->valuetype = 1;
627                 else
628                         class->valuetype = class->parent->valuetype;
629                 class->enumtype = class->parent->enumtype;
630                 class->parent->subclasses = g_list_prepend (class->parent->subclasses, class);
631                 mono_compute_relative_numbering (mono_defaults.object_class, &rnum);
632         }
633
634         if (!strcmp (nspace, "System")) {
635                 if (!strcmp (name, "ValueType")) {
636                         /*
637                          * do not set the valuetype bit for System.ValueType.
638                          * class->valuetype = 1;i
639                          */
640                 } else if (!strcmp (name, "Enum")) {
641                         class->valuetype = 1;
642                         class->enumtype = 1;
643                 } else if (!strcmp (name, "Object")) {
644                         class->this_arg.type = class->byval_arg.type = MONO_TYPE_OBJECT;
645                 } else if (!strcmp (name, "String")) {
646                         class->this_arg.type = class->byval_arg.type = MONO_TYPE_STRING;
647                 }
648         }
649         
650         if (class->valuetype) {
651                 int t = MONO_TYPE_VALUETYPE;
652                 if (!strcmp (nspace, "System")) {
653                         switch (*name) {
654                         case 'B':
655                                 if (!strcmp (name, "Boolean")) {
656                                         t = MONO_TYPE_BOOLEAN;
657                                 } else if (!strcmp(name, "Byte")) {
658                                         t = MONO_TYPE_U1;
659                                 }
660                                 break;
661                         case 'C':
662                                 if (!strcmp (name, "Char")) {
663                                         t = MONO_TYPE_CHAR;
664                                 }
665                                 break;
666                         case 'D':
667                                 if (!strcmp (name, "Double")) {
668                                         t = MONO_TYPE_R8;
669                                 }
670                                 break;
671                         case 'I':
672                                 if (!strcmp (name, "Int32")) {
673                                         t = MONO_TYPE_I4;
674                                 } else if (!strcmp(name, "Int16")) {
675                                         t = MONO_TYPE_I2;
676                                 } else if (!strcmp(name, "Int64")) {
677                                         t = MONO_TYPE_I8;
678                                 } else if (!strcmp(name, "IntPtr")) {
679                                         t = MONO_TYPE_I;
680                                 }
681                                 break;
682                         case 'S':
683                                 if (!strcmp (name, "Single")) {
684                                         t = MONO_TYPE_R4;
685                                 } else if (!strcmp(name, "SByte")) {
686                                         t = MONO_TYPE_I1;
687                                 }
688                                 break;
689                         case 'U':
690                                 if (!strcmp (name, "UInt32")) {
691                                         t = MONO_TYPE_U4;
692                                 } else if (!strcmp(name, "UInt16")) {
693                                         t = MONO_TYPE_U2;
694                                 } else if (!strcmp(name, "UInt64")) {
695                                         t = MONO_TYPE_U8;
696                                 } else if (!strcmp(name, "UIntPtr")) {
697                                         t = MONO_TYPE_U;
698                                 }
699                                 break;
700                         case 'V':
701                                 if (!strcmp (name, "Void")) {
702                                         t = MONO_TYPE_VOID;
703                                 }
704                                 break;
705                         default:
706                                 break;
707                         }
708                 }
709                 class->this_arg.type = class->byval_arg.type = t;
710         }
711
712         /*
713          * Compute the field and method lists
714          */
715         class->field.first  = cols [MONO_TYPEDEF_FIELD_LIST] - 1;
716         class->method.first = cols [MONO_TYPEDEF_METHOD_LIST] - 1;
717
718         if (tt->rows > tidx){           
719                 mono_metadata_decode_row (tt, tidx, cols_next, CSIZE (cols_next));
720                 class->field.last  = cols_next [MONO_TYPEDEF_FIELD_LIST] - 1;
721                 class->method.last = cols_next [MONO_TYPEDEF_METHOD_LIST] - 1;
722         } else {
723                 class->field.last  = image->tables [MONO_TABLE_FIELD].rows;
724                 class->method.last = image->tables [MONO_TABLE_METHOD].rows;
725         }
726
727         if (cols [MONO_TYPEDEF_FIELD_LIST] && 
728             cols [MONO_TYPEDEF_FIELD_LIST] <= image->tables [MONO_TABLE_FIELD].rows)
729                 class->field.count = class->field.last - class->field.first;
730         else
731                 class->field.count = 0;
732
733         if (cols [MONO_TYPEDEF_METHOD_LIST] <= image->tables [MONO_TABLE_METHOD].rows)
734                 class->method.count = class->method.last - class->method.first;
735         else
736                 class->method.count = 0;
737
738         /* reserve space to store vector pointer in arrays */
739         if (!strcmp (nspace, "System") && !strcmp (name, "Array")) {
740                 class->instance_size += 2 * sizeof (gpointer);
741                 g_assert (class->field.count == 0);
742         }
743
744         if (class->flags & TYPE_ATTRIBUTE_INTERFACE)
745                 class->interface_id = interface_id++;
746
747         //class->interfaces = mono_metadata_interfaces_from_typedef (image, type_token, &class->interface_count);
748
749         if (class->enumtype)
750                 mono_class_init (class);
751
752         if ((type_token = mono_metadata_nested_in_typedef (image, type_token)))
753                 class->nested_in = mono_class_create_from_typedef (image, type_token);
754         return class;
755 }
756
757 MonoClass *
758 mono_class_from_mono_type (MonoType *type)
759 {
760         switch (type->type) {
761         case MONO_TYPE_OBJECT:
762                 return mono_defaults.object_class;
763         case MONO_TYPE_VOID:
764                 return mono_defaults.void_class;
765         case MONO_TYPE_BOOLEAN:
766                 return mono_defaults.boolean_class;
767         case MONO_TYPE_CHAR:
768                 return mono_defaults.char_class;
769         case MONO_TYPE_I1:
770                 return mono_defaults.byte_class;
771         case MONO_TYPE_U1:
772                 return mono_defaults.sbyte_class;
773         case MONO_TYPE_I2:
774                 return mono_defaults.int16_class;
775         case MONO_TYPE_U2:
776                 return mono_defaults.uint16_class;
777         case MONO_TYPE_I4:
778                 return mono_defaults.int32_class;
779         case MONO_TYPE_U4:
780                 return mono_defaults.uint32_class;
781         case MONO_TYPE_I:
782                 return mono_defaults.int_class;
783         case MONO_TYPE_U:
784                 return mono_defaults.uint_class;
785         case MONO_TYPE_I8:
786                 return mono_defaults.int64_class;
787         case MONO_TYPE_U8:
788                 return mono_defaults.uint64_class;
789         case MONO_TYPE_R4:
790                 return mono_defaults.single_class;
791         case MONO_TYPE_R8:
792                 return mono_defaults.double_class;
793         case MONO_TYPE_STRING:
794                 return mono_defaults.string_class;
795         case MONO_TYPE_ARRAY:
796                 return mono_defaults.array_class;
797         case MONO_TYPE_PTR:
798                 /* FIXME: this is wrong. Need to handle PTR types */
799                 return mono_class_from_mono_type (type->data.type);
800         case MONO_TYPE_SZARRAY:
801                 return mono_array_class_get (mono_class_from_mono_type (type->data.type), 1);
802         case MONO_TYPE_CLASS:
803         case MONO_TYPE_VALUETYPE:
804                 return type->data.klass;
805         default:
806                 g_warning ("implement me %02x\n", type->type);
807                 g_assert_not_reached ();
808         }
809         
810         return NULL;
811 }
812
813 /**
814  * @image: context where the image is created
815  * @type_spec:  typespec token
816  * @at: an optional pointer to return the array type
817  */
818 static MonoClass *
819 mono_class_create_from_typespec (MonoImage *image, guint32 type_spec)
820 {
821         guint32 idx = mono_metadata_token_index (type_spec);
822         MonoTableInfo *t;
823         guint32 cols [MONO_TYPESPEC_SIZE];       
824         const char *ptr;
825         guint32 len;
826         MonoType *type;
827         MonoClass *class, *eclass;
828
829         t = &image->tables [MONO_TABLE_TYPESPEC];
830         
831         mono_metadata_decode_row (t, idx-1, cols, MONO_TYPESPEC_SIZE);
832         ptr = mono_metadata_blob_heap (image, cols [MONO_TYPESPEC_SIGNATURE]);
833         len = mono_metadata_decode_value (ptr, &ptr);
834         type = mono_metadata_parse_type (image, MONO_PARSE_TYPE, 0, ptr, &ptr);
835
836         switch (type->type) {
837         case MONO_TYPE_ARRAY:
838                 eclass = mono_class_from_mono_type (type->data.array->type);
839                 class = mono_array_class_get (eclass, type->data.array->rank);
840                 break;
841         case MONO_TYPE_SZARRAY:
842                 eclass = mono_class_from_mono_type (type->data.type);
843                 class = mono_array_class_get (eclass, 1);
844                 break;
845         default:
846                 g_warning ("implement me: %08x", type->type);
847                 g_assert_not_reached ();                
848         }
849
850         mono_metadata_free_type (type);
851         
852         return class;
853 }
854
855 /**
856  * mono_array_class_get:
857  * @eclass: element type class
858  * @rank: the dimension of the array class
859  *
860  * Returns: a class object describing the array with element type @etype and 
861  * dimension @rank. 
862  */
863 MonoClass *
864 mono_array_class_get (MonoClass *eclass, guint32 rank)
865 {
866         MonoImage *image;
867         MonoClass *class;
868         static MonoClass *parent = NULL;
869         guint32 key;
870         int rnum = 0;
871
872         g_assert (rank <= 255);
873
874         if (!parent)
875                 parent = mono_defaults.array_class;
876
877         if (!parent->inited)
878                 mono_class_init (parent);
879
880         image = eclass->image;
881
882         g_assert (!eclass->type_token ||
883                   mono_metadata_token_table (eclass->type_token) == MONO_TABLE_TYPEDEF);
884
885         key = ((rank & 0xff) << 24) | (eclass->type_token & 0xffffff);
886         if ((class = g_hash_table_lookup (image->array_cache, GUINT_TO_POINTER (key))))
887                 return class;
888         
889         class = g_malloc0 (sizeof (MonoClass) + parent->vtable_size * sizeof (gpointer));
890
891         class->image = image;
892         class->name_space = "System";
893         class->name = "Array";
894         class->type_token = 0;
895         class->flags = TYPE_ATTRIBUTE_CLASS;
896         class->parent = parent;
897         class->instance_size = mono_class_instance_size (class->parent);
898         class->class_size = 0;
899         class->vtable_size = parent->vtable_size;
900         class->parent->subclasses = g_list_prepend (class->parent->subclasses, class);
901         mono_compute_relative_numbering (mono_defaults.object_class, &rnum);
902
903         class->rank = rank;
904         class->element_class = eclass;
905         if (rank > 1) {
906                 class->byval_arg.type = MONO_TYPE_ARRAY;
907                 /* FIXME: complete.... */
908         } else {
909                 class->byval_arg.type = MONO_TYPE_SZARRAY;
910                 class->byval_arg.data.type = &eclass->byval_arg;
911         }
912         class->this_arg = class->byval_arg;
913         class->this_arg.byref = 1;
914         
915         g_hash_table_insert (image->array_cache, GUINT_TO_POINTER (key), class);
916         return class;
917 }
918
919 /**
920  * mono_class_instance_size:
921  * @klass: a class 
922  * 
923  * Returns: the size of an object instance
924  */
925 gint32
926 mono_class_instance_size (MonoClass *klass)
927 {
928         
929         if (!klass->inited)
930                 mono_class_init (klass);
931
932         return klass->instance_size;
933 }
934
935 /**
936  * mono_class_value_size:
937  * @klass: a class 
938  *
939  * This function is used for value types, and return the
940  * space and the alignment to store that kind of value object.
941  *
942  * Returns: the size of a value of kind @klass
943  */
944 gint32
945 mono_class_value_size      (MonoClass *klass, guint32 *align)
946 {
947         gint32 size;
948
949         /* fixme: check disable, because we still have external revereces to
950          * mscorlib and Dummy Objects 
951          */
952         /*g_assert (klass->valuetype);*/
953
954         size = mono_class_instance_size (klass) - sizeof (MonoObject);
955
956         if (align)
957                 *align = klass->min_align;
958
959         return size;
960 }
961
962 /**
963  * mono_class_data_size:
964  * @klass: a class 
965  * 
966  * Returns: the size of the static class data
967  */
968 gint32
969 mono_class_data_size (MonoClass *klass)
970 {
971         
972         if (!klass->inited)
973                 mono_class_init (klass);
974
975         return klass->class_size;
976 }
977
978 /*
979  * Auxiliary routine to mono_class_get_field
980  *
981  * Takes a field index instead of a field token.
982  */
983 static MonoClassField *
984 mono_class_get_field_idx (MonoClass *class, int idx)
985 {
986         if (class->field.count){
987                 if ((idx >= class->field.first) && (idx < class->field.last)){
988                         return &class->fields [idx - class->field.first];
989                 }
990         }
991
992         if (!class->parent)
993                 return NULL;
994         
995         return mono_class_get_field_idx (class->parent, idx);
996 }
997
998 /**
999  * mono_class_get_field:
1000  * @class: the class to lookup the field.
1001  * @field_token: the field token
1002  *
1003  * Returns: A MonoClassField representing the type and offset of
1004  * the field, or a NULL value if the field does not belong to this
1005  * class.
1006  */
1007 MonoClassField *
1008 mono_class_get_field (MonoClass *class, guint32 field_token)
1009 {
1010         int idx = mono_metadata_token_index (field_token);
1011
1012         g_assert (mono_metadata_token_code (field_token) == MONO_TOKEN_FIELD_DEF);
1013
1014         return mono_class_get_field_idx (class, idx - 1);
1015 }
1016
1017 MonoClassField *
1018 mono_class_get_field_from_name (MonoClass *klass, const char *name)
1019 {
1020         int i;
1021         guint32 token;
1022         MonoTableInfo *t = &klass->image->tables [MONO_TABLE_FIELD];
1023
1024         for (i = 0; i < klass->field.count; ++i) {
1025                 token = mono_metadata_decode_row_col (t, klass->field.first + i, MONO_FIELD_NAME);
1026                 if (strcmp (name, mono_metadata_string_heap (klass->image, token)) == 0)
1027                         return &klass->fields [i];
1028         }
1029         return NULL;
1030 }
1031
1032 /**
1033  * mono_class_get:
1034  * @image: the image where the class resides
1035  * @type_token: the token for the class
1036  * @at: an optional pointer to return the array element type
1037  *
1038  * Returns: the MonoClass that represents @type_token in @image
1039  */
1040 MonoClass *
1041 mono_class_get (MonoImage *image, guint32 type_token)
1042 {
1043         MonoClass *class;
1044
1045         switch (type_token & 0xff000000){
1046         case MONO_TOKEN_TYPE_DEF:
1047                 class = mono_class_create_from_typedef (image, type_token);
1048                 break;          
1049         case MONO_TOKEN_TYPE_REF:
1050                 return mono_class_create_from_typeref (image, type_token);
1051         case MONO_TOKEN_TYPE_SPEC:
1052                 class = mono_class_create_from_typespec (image, type_token);
1053                 break;
1054         default:
1055                 g_warning ("unknown token type %x", type_token & 0xff000000);
1056                 g_assert_not_reached ();
1057         }
1058
1059         return class;
1060 }
1061
1062 MonoClass *
1063 mono_class_from_name (MonoImage *image, const char* name_space, const char *name)
1064 {
1065         GHashTable *nspace_table;
1066         guint32 token;
1067
1068         nspace_table = g_hash_table_lookup (image->name_cache, name_space);
1069         if (!nspace_table)
1070                 return 0;
1071         token = GPOINTER_TO_UINT (g_hash_table_lookup (nspace_table, name));
1072         
1073         if (!token) {
1074                 /*g_warning ("token not found for %s.%s in image %s", name_space, name, image->name);*/
1075                 return NULL;
1076         }
1077
1078         token = MONO_TOKEN_TYPE_DEF | token;
1079
1080         return mono_class_get (image, token);
1081 }
1082
1083 /**
1084  * mono_array_element_size:
1085  * @ac: pointer to a #MonoArrayClass
1086  *
1087  * Returns: the size of single array element.
1088  */
1089 gint32
1090 mono_array_element_size (MonoClass *ac)
1091 {
1092         if (ac->element_class->valuetype)
1093                 return mono_class_instance_size (ac->element_class) - sizeof (MonoObject);
1094         else
1095                 return sizeof (gpointer);
1096 }
1097
1098 gpointer
1099 mono_ldtoken (MonoImage *image, guint32 token, MonoClass **handle_class)
1100 {
1101         switch (token & 0xff000000) {
1102         case MONO_TOKEN_TYPE_DEF:
1103         case MONO_TOKEN_TYPE_REF: {
1104                 MonoClass *class;
1105                 if (handle_class)
1106                         *handle_class = mono_defaults.typehandle_class;
1107                 class = mono_class_get (image, token);
1108                 /* We return a MonoType* as handle */
1109                 return &class->byval_arg;
1110         }
1111         case MONO_TOKEN_TYPE_SPEC: {
1112                 MonoClass *class;
1113                 if (handle_class)
1114                         *handle_class = mono_defaults.typehandle_class;
1115                 class = mono_class_create_from_typespec (image, token);
1116                 return &class->byval_arg;
1117         }
1118         case MONO_TOKEN_FIELD_DEF: {
1119                 MonoClass *class;
1120                 guint32 type = mono_metadata_typedef_from_field (image, mono_metadata_token_index (token));
1121                 class = mono_class_get (image, MONO_TOKEN_TYPE_DEF | type);
1122                 mono_class_init (class);
1123                 if (handle_class)
1124                                 *handle_class = mono_class_from_name (mono_defaults.corlib, "System", "RuntimeFieldHandle");
1125                 return mono_class_get_field (class, token);
1126         }
1127         case MONO_TOKEN_METHOD_DEF:
1128         case MONO_TOKEN_MEMBER_REF:
1129         default:
1130                 g_warning ("Unknown token 0x%08x in ldtoken", token);
1131                 break;
1132         }
1133         return NULL;
1134 }
1135