2001-08-28 Dietmar Maurer <dietmar@ximian.com>
[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 <mono/metadata/image.h>
20 #include <mono/metadata/cil-coff.h>
21 #include <mono/metadata/metadata.h>
22 #include <mono/metadata/tabledefs.h>
23 #include <mono/metadata/tokentype.h>
24 #include <mono/metadata/class.h>
25 #include <mono/metadata/object.h>
26
27 #define CSIZE(x) (sizeof (x) / 4)
28
29 static MonoClass *
30 mono_class_create_from_typeref (MonoImage *image, guint32 type_token)
31 {
32         guint32 cols [MONO_TYPEREF_SIZE];
33         MonoTableInfo  *t = &image->tables [MONO_TABLE_TYPEREF];
34         guint32 idx;
35         const char *name, *nspace;
36         MonoClass *res;
37
38         mono_metadata_decode_row (t, (type_token&0xffffff)-1, cols, MONO_TYPEREF_SIZE);
39         g_assert ((cols [MONO_TYPEREF_SCOPE] & 0x3) == 2);
40         idx = cols [MONO_TYPEREF_SCOPE] >> 2;
41
42         if (!image->references ||  !image->references [idx-1]) {
43                 /* 
44                  * detected a reference to mscorlib, we simply return a reference to a dummy 
45                  * until we have a better solution.
46                  */
47                 res = mono_class_from_name (image, "System", "MonoDummy");
48                 /* prevent method loading */
49                 res->dummy = 1;
50                 /* some storage if the type is used  - very ugly hack */
51                 res->instance_size = 2*sizeof (gpointer);
52                 return res;
53         }       
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         /* load referenced assembly */
59         image = image->references [idx-1]->image;
60
61         return mono_class_from_name (image, nspace, name);
62 }
63
64 /** 
65  * class_compute_field_layout:
66  * @m: pointer to the metadata.
67  * @class: The class to initialize
68  *
69  * Initializes the class->fields.
70  *
71  * Currently we only support AUTO_LAYOUT, and do not even try to do
72  * a good job at it.  This is temporary to get the code for Paolo.
73  */
74 static void
75 class_compute_field_layout (MonoClass *class)
76 {
77         MonoImage *m = class->image; 
78         const int top = class->field.count;
79         guint32 layout = class->flags & TYPE_ATTRIBUTE_LAYOUT_MASK;
80         MonoTableInfo *t = &m->tables [MONO_TABLE_FIELD];
81         int i, j;
82
83         /*
84          * Fetch all the field information.
85          */
86         for (i = 0; i < top; i++){
87                 const char *sig;
88                 guint32 cols [3];
89                 int idx = class->field.first + i;
90                 
91                 mono_metadata_decode_row (t, idx, cols, CSIZE (cols));
92                 sig = mono_metadata_blob_heap (m, cols [2]);
93                 mono_metadata_decode_value (sig, &sig);
94                 /* FIELD signature == 0x06 */
95                 g_assert (*sig == 0x06);
96                 class->fields [i].type = mono_metadata_parse_field_type (
97                         m, cols [MONO_FIELD_FLAGS], sig + 1, &sig);
98         }
99         /*
100          * Compute field layout and total size.
101          */
102         switch (layout){
103         case TYPE_ATTRIBUTE_AUTO_LAYOUT:
104         case TYPE_ATTRIBUTE_SEQUENTIAL_LAYOUT:
105                 for (i = 0; i < top; i++){
106                         int size, align;
107                         
108                         size = mono_type_size (class->fields [i].type, &align);
109                         if (class->fields [i].type->attrs & FIELD_ATTRIBUTE_STATIC) {
110                                 class->fields [i].offset = class->class_size;
111                                 class->class_size += (class->class_size % align);
112                                 class->class_size += size;
113                         } else {
114                                 class->fields [i].offset = class->instance_size;
115                                 class->instance_size += (class->instance_size % align);
116                                 class->instance_size += size;
117                         }
118                 }
119                 break;
120         case TYPE_ATTRIBUTE_EXPLICIT_LAYOUT:
121                 for (i = 0; i < top; i++){
122                         guint32 cols [2];
123                         int size, align;
124                         int idx = class->field.first + i;
125
126                         t = &m->tables [MONO_TABLE_FIELDLAYOUT];
127
128                         for (j = 0; j < t->rows; j++) {
129
130                                 mono_metadata_decode_row (t, j, cols, CSIZE (cols));
131                                 if (cols [1] == idx) {
132                                         g_warning ("TODO: Explicit layout not supported yet");
133                                 }
134                         }
135                         
136                         size = mono_type_size (class->fields [i].type, &align);
137                         if (class->fields [i].type->attrs & FIELD_ATTRIBUTE_STATIC) {
138                                 class->fields [i].offset = class->class_size;
139                                 class->class_size += (class->class_size % align);
140                                 class->class_size += size;
141                         } else {
142                                 class->fields [i].offset = class->instance_size;
143                                 class->instance_size += (class->instance_size % align);
144                                 class->instance_size += size;
145                         }
146                 }
147                 break;
148         }
149 }
150
151 void
152 mono_class_metadata_init (MonoClass *class)
153 {
154         int i;
155
156         if (class->metadata_inited)
157                 return;
158
159         if (class->parent) {
160                 if (!class->parent->metadata_inited)
161                         mono_class_metadata_init (class->parent);
162                 class->instance_size = class->parent->instance_size;
163                 class->class_size = class->parent->class_size;
164         }
165
166         class->metadata_inited = 1;
167
168         /*
169          * Computes the size used by the fields, and their locations
170          */
171         if (class->field.count > 0){
172                 class->fields = g_new (MonoClassField, class->field.count);
173                 class_compute_field_layout (class);
174         }
175
176         if (!class->method.count)
177                 return;
178
179         class->methods = g_new (MonoMethod*, class->method.count);
180         for (i = class->method.first; i < class->method.last; ++i)
181                 class->methods [i - class->method.first] = 
182                         mono_get_method (class->image,
183                                          MONO_TOKEN_METHOD_DEF | (i + 1), 
184                                          class);
185 }
186
187 /**
188  * @image: context where the image is created
189  * @type_token:  typedef token
190  */
191 static MonoClass *
192 mono_class_create_from_typedef (MonoImage *image, guint32 type_token)
193 {
194         MonoTableInfo *tt = &image->tables [MONO_TABLE_TYPEDEF];
195         MonoClass *class;
196         guint32 cols [MONO_TYPEDEF_SIZE], parent_token;
197         guint tidx = mono_metadata_token_index (type_token);
198         const char *name, *nspace;
199      
200         g_assert (mono_metadata_token_table (type_token) == MONO_TABLE_TYPEDEF);
201
202         class = g_malloc0 (sizeof (MonoClass));
203
204         class->this_arg.byref = 1;
205         class->this_arg.data.klass = class;
206         class->this_arg.type = MONO_TYPE_CLASS;
207
208         mono_metadata_decode_row (tt, tidx-1, cols, CSIZE (cols));
209         class->name = name = mono_metadata_string_heap (image, cols[1]);
210         class->name_space = nspace = mono_metadata_string_heap (image, cols[2]);
211
212         class->image = image;
213         class->type_token = type_token;
214         class->flags = cols [0];
215
216         /*g_print ("Init class %s\n", name);*/
217
218         /* if root of the hierarchy */
219         if (!strcmp (nspace, "System") && !strcmp (name, "Object")) {
220                 class->parent = NULL;
221                 class->instance_size = sizeof (MonoObject);
222         } else if (!(cols [0] & TYPE_ATTRIBUTE_INTERFACE)) {
223                 parent_token = mono_metadata_token_from_dor (cols [3]);
224                 class->parent = mono_class_get (image, parent_token);
225                 class->valuetype = class->parent->valuetype;
226                 class->enumtype = class->parent->enumtype;
227         }
228
229         if (!strcmp (nspace, "System")) {
230                 if (!strcmp (name, "ValueType")) {
231                         class->valuetype = 1;
232                 } else if (!strcmp (name, "Enum")) {
233                         class->valuetype = 1;
234                         class->enumtype = 1;
235                 }
236         }
237         if (class->valuetype)
238                 class->this_arg.type = MONO_TYPE_VALUETYPE;
239         
240         /*
241          * Compute the field and method lists
242          */
243         class->field.first  = cols [MONO_TYPEDEF_FIELD_LIST] - 1;
244         class->method.first = cols [MONO_TYPEDEF_METHOD_LIST] - 1;
245
246         if (tt->rows > tidx){
247                 guint32 cols_next [MONO_TYPEDEF_SIZE];
248                 
249                 mono_metadata_decode_row (tt, tidx, cols_next, CSIZE (cols_next));
250                 class->field.last  = cols_next [MONO_TYPEDEF_FIELD_LIST] - 1;
251                 class->method.last = cols_next [MONO_TYPEDEF_METHOD_LIST] - 1;
252         } else {
253                 class->field.last  = image->tables [MONO_TABLE_FIELD].rows;
254                 class->method.last = image->tables [MONO_TABLE_METHOD].rows;
255         }
256
257         if (cols [MONO_TYPEDEF_FIELD_LIST] && 
258             cols [MONO_TYPEDEF_FIELD_LIST] <= image->tables [MONO_TABLE_FIELD].rows)
259                 class->field.count = class->field.last - class->field.first;
260         else
261                 class->field.count = 0;
262
263         if (cols [MONO_TYPEDEF_METHOD_LIST] <= image->tables [MONO_TABLE_METHOD].rows)
264                 class->method.count = class->method.last - class->method.first;
265         else
266                 class->method.count = 0;
267
268         /* reserve space to store vector pointer in arrays */
269         if (!strcmp (nspace, "System") && !strcmp (name, "Array")) {
270                 class->instance_size += 2 * sizeof (gpointer);
271                 g_assert (class->field.count == 0);
272         }
273
274         class->interfaces = mono_metadata_interfaces_from_typedef (image, type_token);
275         return class;
276 }
277
278 MonoClass *
279 mono_class_from_mono_type (MonoType *type)
280 {
281         MonoImage *corlib;
282         MonoClass *res;
283
284         corlib = mono_defaults.corlib;
285
286         switch (type->type) {
287         case MONO_TYPE_OBJECT:
288                 res = mono_defaults.object_class;
289                 break;
290         case MONO_TYPE_VOID:
291                 res = mono_defaults.void_class;
292                 break;
293         case MONO_TYPE_BOOLEAN:
294                 res = mono_defaults.boolean_class;
295                 break;
296         case MONO_TYPE_CHAR:
297                 res = mono_defaults.char_class;
298                 break;
299         case MONO_TYPE_I1:
300                 res = mono_defaults.byte_class;
301                 break;
302         case MONO_TYPE_U1:
303                 res = mono_defaults.sbyte_class;
304                 break;
305         case MONO_TYPE_I2:
306                 res = mono_defaults.int16_class;
307                 break;
308         case MONO_TYPE_U2:
309                 res = mono_defaults.uint16_class;
310                 break;
311         case MONO_TYPE_I4:
312                 res = mono_defaults.int32_class;
313                 break;
314         case MONO_TYPE_U4:
315                 res = mono_defaults.uint32_class;
316                 break;
317         case MONO_TYPE_I:
318                 res = mono_defaults.int_class;
319                 break;
320         case MONO_TYPE_U:
321                 res = mono_defaults.uint_class;
322                 break;
323         case MONO_TYPE_I8:
324                 res = mono_defaults.int64_class;
325                 break;
326         case MONO_TYPE_U8:
327                 res = mono_defaults.uint64_class;
328                 break;
329         case MONO_TYPE_R4:
330                 res = mono_defaults.single_class;
331                 break;
332         case MONO_TYPE_R8:
333                 res = mono_defaults.double_class;
334                 break;
335         case MONO_TYPE_STRING:
336                 res = mono_defaults.string_class;
337                 break;
338         case MONO_TYPE_ARRAY:
339                 res = mono_defaults.array_class;
340                 break;
341         case MONO_TYPE_SZARRAY:
342         case MONO_TYPE_PTR:
343                 /* Not really sure about these. */
344                 res = mono_class_from_mono_type (type->data.type);
345                 break;
346         case MONO_TYPE_CLASS:
347         case MONO_TYPE_VALUETYPE:
348                 res = type->data.klass;
349                 break;
350         default:
351                 g_warning ("implement me %02x\n", type->type);
352                 g_assert_not_reached ();
353         }
354         
355         return res;
356 }
357
358 /**
359  * @image: context where the image is created
360  * @type_spec:  typespec token
361  * @at: an optional pointer to return the array type
362  */
363 static MonoClass *
364 mono_class_create_from_typespec (MonoImage *image, guint32 type_spec)
365 {
366         guint32 idx = mono_metadata_token_index (type_spec);
367         MonoTableInfo *t;
368         guint32 cols [MONO_TYPESPEC_SIZE];       
369         const char *ptr;
370         guint32 len;
371         MonoType *type;
372         MonoClass *class, *eclass;
373
374         t = &image->tables [MONO_TABLE_TYPESPEC];
375         
376         mono_metadata_decode_row (t, idx-1, cols, MONO_TYPESPEC_SIZE);
377         ptr = mono_metadata_blob_heap (image, cols [MONO_TYPESPEC_SIGNATURE]);
378         len = mono_metadata_decode_value (ptr, &ptr);
379         type = mono_metadata_parse_type (image, MONO_PARSE_TYPE, 0, ptr, &ptr);
380
381         switch (type->type) {
382         case MONO_TYPE_ARRAY:
383                 eclass = mono_class_from_mono_type (type->data.array->type);
384                 class = mono_array_class_get (eclass, type->data.array->rank);
385                 break;
386         case MONO_TYPE_SZARRAY:
387                 eclass = mono_class_from_mono_type (type->data.type);
388                 class = mono_array_class_get (eclass, 1);
389                 break;
390         default:
391                 g_warning ("implement me: %08x", type->type);
392                 g_assert_not_reached ();                
393         }
394
395         mono_metadata_free_type (type);
396         
397         return class;
398 }
399
400 /**
401  * mono_array_class_get:
402  * @eclass: element type class
403  * @rank: the dimension of the array class
404  *
405  * Returns: a class object describing the array with element type @etype and 
406  * dimension @rank. 
407  */
408 MonoClass *
409 mono_array_class_get (MonoClass *eclass, guint32 rank)
410 {
411         MonoImage *image;
412         MonoClass *class;
413         static MonoClass *parent = NULL;
414         MonoArrayClass *aclass;
415         guint32 key;
416
417         g_assert (rank <= 255);
418
419         if (!parent)
420                 parent = mono_defaults.array_class;
421
422         image = eclass->image;
423
424         g_assert (!eclass->type_token ||
425                   mono_metadata_token_table (eclass->type_token) == MONO_TABLE_TYPEDEF);
426         
427         key = ((rank & 0xff) << 24) | (eclass->type_token & 0xffffff);
428         if ((class = g_hash_table_lookup (image->array_cache, GUINT_TO_POINTER (key))))
429                 return class;
430         
431         aclass = g_new0 (MonoArrayClass, 1);
432         class = (MonoClass *)aclass;
433        
434         class->image = image;
435         class->name_space = "System";
436         class->name = "Array";
437         class->type_token = 0;
438         class->flags = TYPE_ATTRIBUTE_CLASS;
439         class->parent = parent;
440         class->instance_size = mono_class_instance_size (class->parent);
441         class->class_size = 0;
442
443         aclass->rank = rank;
444         aclass->element_class = eclass;
445         
446         g_hash_table_insert (image->array_cache, GUINT_TO_POINTER (key), class);
447         return class;
448 }
449
450 /**
451  * mono_class_instance_size:
452  * @klass: a class 
453  * 
454  * Returns: the size of an object instance
455  */
456 gint32
457 mono_class_instance_size (MonoClass *klass)
458 {
459         
460         if (!klass->metadata_inited)
461                 mono_class_metadata_init (klass);
462
463         return klass->instance_size;
464 }
465
466 /**
467  * mono_class_value_size:
468  * @klass: a class 
469  *
470  * This function is used for value types, and return the
471  * space and the alignment to store that kind of value object.
472  *
473  * Returns: the size of a value of kind @klass
474  */
475 gint32
476 mono_class_value_size      (MonoClass *klass, guint32 *align)
477 {
478         gint32 size;
479
480         /* fixme: check disable, because we still have external revereces to
481          * mscorlib and Dummy Objects 
482          */
483         //g_assert (klass->valuetype);
484
485         size = mono_class_instance_size (klass) - sizeof (MonoObject);
486
487         if (align) {
488                 if (size <= 4)
489                         *align = 4;
490                 else
491                         *align = 8;
492         }
493
494         return size;
495 }
496
497 /**
498  * mono_class_data_size:
499  * @klass: a class 
500  * 
501  * Returns: the size of the static class data
502  */
503 gint32
504 mono_class_data_size (MonoClass *klass)
505 {
506         
507         if (!klass->metadata_inited)
508                 mono_class_metadata_init (klass);
509
510         return klass->class_size;
511 }
512
513 /*
514  * Auxiliary routine to mono_class_get_field
515  *
516  * Takes a field index instead of a field token.
517  */
518 static MonoClassField *
519 mono_class_get_field_idx (MonoClass *class, int idx)
520 {
521         if (class->field.count){
522                 if ((idx >= class->field.first) && (idx < class->field.last)){
523                         return &class->fields [idx - class->field.first];
524                 }
525         }
526
527         if (!class->parent)
528                 return NULL;
529         
530         return mono_class_get_field_idx (class->parent, idx);
531 }
532
533 /**
534  * mono_class_get_field:
535  * @class: the class to lookup the field.
536  * @field_token: the field token
537  *
538  * Returns: A MonoClassField representing the type and offset of
539  * the field, or a NULL value if the field does not belong to this
540  * class.
541  */
542 MonoClassField *
543 mono_class_get_field (MonoClass *class, guint32 field_token)
544 {
545         int idx = mono_metadata_token_index (field_token);
546
547         if (mono_metadata_token_code (field_token) == MONO_TOKEN_MEMBER_REF)
548                 g_error ("Unsupported Field Token is a MemberRef, implement me");
549
550         g_assert (mono_metadata_token_code (field_token) == MONO_TOKEN_FIELD_DEF);
551
552         return mono_class_get_field_idx (class, idx - 1);
553 }
554
555 /**
556  * mono_class_get:
557  * @image: the image where the class resides
558  * @type_token: the token for the class
559  * @at: an optional pointer to return the array element type
560  *
561  * Returns: the MonoClass that represents @type_token in @image
562  */
563 MonoClass *
564 mono_class_get (MonoImage *image, guint32 type_token)
565 {
566         MonoClass *class;
567
568         switch (type_token & 0xff000000){
569         case MONO_TOKEN_TYPE_DEF:
570                 if ((class = g_hash_table_lookup (image->class_cache, 
571                                                   GUINT_TO_POINTER (type_token))))
572                         return class;
573                 class = mono_class_create_from_typedef (image, type_token);
574                 break;          
575         case MONO_TOKEN_TYPE_REF:
576                 return mono_class_create_from_typeref (image, type_token);
577         case MONO_TOKEN_TYPE_SPEC:
578                 if ((class = g_hash_table_lookup (image->class_cache, 
579                                                   GUINT_TO_POINTER (type_token))))
580                         return class;
581
582                 class = mono_class_create_from_typespec (image, type_token);
583                 break;
584         default:
585                 g_assert_not_reached ();
586         }
587         
588         g_hash_table_insert (image->class_cache, GUINT_TO_POINTER (type_token), class);
589
590         return class;
591 }
592
593 MonoClass *
594 mono_class_from_name (MonoImage *image, const char* name_space, const char *name)
595 {
596         GHashTable *nspace_table;
597         guint32 token;
598
599         nspace_table = g_hash_table_lookup (image->name_cache, name_space);
600         if (!nspace_table)
601                 return 0;
602         token = GPOINTER_TO_UINT (g_hash_table_lookup (nspace_table, name));
603         
604         if (!token)
605                 g_error ("token not found for %s.%s in image %s", name_space, name, image->name);
606
607         token = MONO_TOKEN_TYPE_DEF | token;
608
609         return mono_class_get (image, token);
610 }
611
612 /**
613  * mono_array_element_size:
614  * @ac: pointer to a #MonoArrayClass
615  *
616  * Returns: the size of single array element.
617  */
618 gint32
619 mono_array_element_size (MonoArrayClass *ac)
620 {
621         gint32 esize;
622
623         esize = mono_class_instance_size (ac->element_class);
624         
625         if (ac->element_class->valuetype)
626                 esize -= sizeof (MonoObject);
627         
628         return esize;
629 }