2 * class.c: Class management for the Mono runtime
5 * Miguel de Icaza (miguel@ximian.com)
7 * (C) 2001 Ximian, Inc.
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.
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>
30 #define CSIZE(x) (sizeof (x) / 4)
32 static MonoClass * mono_class_create_from_typedef (MonoImage *image, guint32 type_token);
35 default_trampoline (MonoMethod *method)
41 default_runtime_class_init (MonoClass *klass)
46 static MonoTrampoline arch_create_jit_trampoline = default_trampoline;
47 static MonoRuntimeClassInit mono_runtime_class_init = default_runtime_class_init;
50 mono_install_trampoline (MonoTrampoline func)
52 arch_create_jit_trampoline = func? func: default_trampoline;
56 mono_install_runtime_class_init (MonoRuntimeClassInit func)
58 mono_runtime_class_init = func? func: default_runtime_class_init;
62 mono_class_create_from_typeref (MonoImage *image, guint32 type_token)
64 guint32 cols [MONO_TYPEREF_SIZE];
65 MonoTableInfo *t = &image->tables [MONO_TABLE_TYPEREF];
67 const char *name, *nspace;
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;
74 if (!image->references || !image->references [idx-1]) {
76 * detected a reference to mscorlib, we simply return a reference to a dummy
77 * until we have a better solution.
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 */
83 /* some storage if the type is used - very ugly hack */
84 res->instance_size = 2*sizeof (gpointer);
88 name = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAME]);
89 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAMESPACE]);
91 /* load referenced assembly */
92 image = image->references [idx-1]->image;
94 return mono_class_from_name (image, nspace, name);
98 * class_compute_field_layout:
99 * @m: pointer to the metadata.
100 * @class: The class to initialize
102 * Initializes the class->fields.
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.
108 class_compute_field_layout (MonoClass *class)
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];
117 * Fetch all the field information.
119 for (i = 0; i < top; i++){
121 guint32 cols [MONO_FIELD_SIZE];
122 int idx = class->field.first + i;
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);
138 if (class->enumtype && !(cols [MONO_FIELD_FLAGS] & FIELD_ATTRIBUTE_STATIC))
139 class->enum_basetype = class->fields [i].type;
141 if (class->enumtype && !class->enum_basetype) {
142 if (!((strcmp (class->name, "Enum") == 0) && (strcmp (class->name_space, "System") == 0)))
146 * Compute field layout and total size.
149 case TYPE_ATTRIBUTE_AUTO_LAYOUT:
150 case TYPE_ATTRIBUTE_SEQUENTIAL_LAYOUT:
151 for (i = 0; i < top; i++){
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;
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;
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);
171 case TYPE_ATTRIBUTE_EXPLICIT_LAYOUT:
172 for (i = 0; i < top; i++){
174 int idx = class->field.first + i;
177 * There must be info about all the fields in a type if it
178 * uses explicit layout.
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;
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);
191 * The offset is from the start of the object: this works for both
192 * classes and valuetypes.
194 class->fields [i].offset += sizeof (MonoObject);
198 size += class->fields [i].offset;
199 class->instance_size = MAX (class->instance_size, size);
207 init_properties (MonoClass *class)
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];
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;
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]);
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]) {
228 class->properties [i - class->property.first].set = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
231 class->properties [i - class->property.first].get = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
241 mono_class_init (MonoClass *class)
244 MonoMethod **tmp_vtable, **vtable = (MonoMethod **)class->vtable;
245 int i, max_iid, cur_slot = 0;
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;
261 class->min_align = 1;
264 * Computes the size used by the fields, and their locations
266 if (class->field.count > 0){
267 class->fields = g_new0 (MonoClassField, class->field.count);
268 class_compute_field_layout (class);
271 if (class->class_size)
272 class->data = g_malloc0 (class->class_size);
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);
280 if (class->flags & TYPE_ATTRIBUTE_INTERFACE) {
281 for (i = 0; i < class->method.count; ++i)
282 class->methods [i]->slot = i;
286 //printf ("METAINIT %s.%s\n", class->name_space, class->name);
288 /* compute maximum number of slots and maximum interface id */
290 for (k = class; k ; k = k->parent) {
291 for (i = 0; i < k->interface_count; i++) {
292 ic = k->interfaces [i];
295 mono_class_init (ic);
297 if (max_iid < ic->interface_id)
298 max_iid = ic->interface_id;
302 class->max_interface_id = max_iid;
303 /* compute vtable offset for interfaces */
304 class->interface_offsets = g_malloc (sizeof (gpointer) * (max_iid + 1));
306 for (i = 0; i <= max_iid; i++)
307 class->interface_offsets [i] = NULL;
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;
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;
322 g_assert (io <= class->vtable_size);
324 class->interface_offsets [ic->interface_id] = &class->vtable [io];
329 if (class->parent && class->parent->vtable_size)
330 memcpy (class->vtable, class->parent->vtable, sizeof (gpointer) * class->parent->vtable_size);
332 tmp_vtable = alloca (class->vtable_size * sizeof (gpointer));
333 memset (tmp_vtable, 0, class->vtable_size * sizeof (gpointer));
335 for (k = class; k ; k = k->parent) {
336 for (i = 0; i < k->interface_count; i++) {
338 ic = k->interfaces [i];
340 io = (k->interface_offsets [ic->interface_id] - (gpointer)k->vtable)>>2;
343 g_assert (io <= class->vtable_size);
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))
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;
362 /* already implemented */
363 if (io >= k->vtable_size)
367 for (l = 0; l < ic->method.count; l++) {
368 MonoMethod *im = ic->methods [l];
371 g_assert (io + l <= class->vtable_size);
373 if (tmp_vtable [io + l] || vtable [io + l])
376 for (k1 = class; k1; k1 = k1->parent) {
377 for (j = 0; j < k1->method.count; ++j) {
378 MonoMethod *cm = k1->methods [j];
380 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
381 !(cm->flags & METHOD_ATTRIBUTE_PUBLIC))
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;
392 g_assert (io + l <= class->vtable_size);
393 if (tmp_vtable [io + l])
398 for (l = 0; l < ic->method.count; l++) {
399 MonoMethod *im = ic->methods [l];
401 if (ic->name_space && ic->name_space [0])
402 qname = g_strconcat (ic->name_space, ".", ic->name, ".", im->name, NULL);
404 qname = g_strconcat (ic->name, ".", im->name, NULL);
406 for (j = 0; j < class->method.count; ++j) {
407 MonoMethod *cm = class->methods [j];
409 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL))
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;
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);
431 for (j = 0; j < class->method.count; ++j) {
432 MonoMethod *cm = class->methods [j];
434 printf ("METHOD %s\n", cm->name);
436 g_assert_not_reached ();
441 for (l = 0; l < ic->method.count; l++) {
442 MonoMethod *im = tmp_vtable [io + l];
445 g_assert (io + l <= class->vtable_size);
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);
458 for (i = 0; i < class->method.count; ++i) {
461 cm = class->methods [i];
463 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
467 if (!(cm->flags & METHOD_ATTRIBUTE_NEW_SLOT)) {
468 for (k = class->parent; k ; k = k->parent) {
470 for (j = 0; j < k->method.count; ++j) {
471 MonoMethod *m1 = k->methods [j];
472 if (!(m1->flags & METHOD_ATTRIBUTE_VIRTUAL))
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);
487 cm->slot = cur_slot++;
489 if (!(cm->flags & METHOD_ATTRIBUTE_ABSTRACT))
490 vtable [cm->slot] = arch_create_jit_trampoline (cm);
493 init_properties (class);
495 i = mono_metadata_nesting_typedef (class->image, class->type_token);
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))
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);
507 mono_runtime_class_init (class);
510 printf ("VTABLE %s.%s\n", class->name_space, class->name);
512 for (i = 0; i < class->vtable_size; ++i) {
517 printf (" METH%d %p %s %d\n", i, cm, cm->name, cm->slot);
521 printf ("METAEND %s.%s\n", class->name_space, class->name);
526 * Compute a relative numbering of the class hierarchy as described in
527 * "Java for Large-Scale Scientific Computations?"
530 mono_compute_relative_numbering (MonoClass *class, int *c)
538 for (s = class->subclasses; s; s = s->next)
539 mono_compute_relative_numbering ((MonoClass *)s->data, c);
541 class->diffval = *c - class->baseval;
545 * @image: context where the image is created
546 * @type_token: typedef token
549 mono_class_create_from_typedef (MonoImage *image, guint32 type_token)
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;
562 if ((class = g_hash_table_lookup (image->class_cache, GUINT_TO_POINTER (type_token))))
565 g_assert (mono_metadata_token_table (type_token) == MONO_TABLE_TYPEDEF);
567 mono_metadata_decode_row (tt, tidx - 1, cols, CSIZE (cols));
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];
573 vtsize += image->tables [MONO_TABLE_METHOD].rows - cols [MONO_TYPEDEF_METHOD_LIST] + 1;
576 name = mono_metadata_string_heap (image, cols[1]);
577 nspace = mono_metadata_string_heap (image, cols[2]);
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]));
583 interfaces = mono_metadata_interfaces_from_typedef (image, type_token, &icount);
585 for (i = 0; i < icount; i++)
586 vtsize += interfaces [i]->method.count;
589 vtsize += parent->vtable_size;
591 if (cols [0] & TYPE_ATTRIBUTE_INTERFACE)
594 class = g_malloc0 (sizeof (MonoClass) + vtsize * sizeof (gpointer));
596 g_hash_table_insert (image->class_cache, GUINT_TO_POINTER (type_token), class);
598 class->parent = parent;
599 class->interfaces = interfaces;
600 class->interface_count = icount;
601 class->vtable_size = vtsize;
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;
610 class->name_space = nspace;
612 class->image = image;
613 class->type_token = type_token;
614 class->flags = cols [0];
616 /*g_print ("Init class %s\n", name);*/
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)) {
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;
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);
634 if (!strcmp (nspace, "System")) {
635 if (!strcmp (name, "ValueType")) {
637 * do not set the valuetype bit for System.ValueType.
638 * class->valuetype = 1;i
640 } else if (!strcmp (name, "Enum")) {
641 class->valuetype = 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;
650 if (class->valuetype) {
651 int t = MONO_TYPE_VALUETYPE;
652 if (!strcmp (nspace, "System")) {
655 if (!strcmp (name, "Boolean")) {
656 t = MONO_TYPE_BOOLEAN;
657 } else if (!strcmp(name, "Byte")) {
662 if (!strcmp (name, "Char")) {
667 if (!strcmp (name, "Double")) {
672 if (!strcmp (name, "Int32")) {
674 } else if (!strcmp(name, "Int16")) {
676 } else if (!strcmp(name, "Int64")) {
678 } else if (!strcmp(name, "IntPtr")) {
683 if (!strcmp (name, "Single")) {
685 } else if (!strcmp(name, "SByte")) {
690 if (!strcmp (name, "UInt32")) {
692 } else if (!strcmp(name, "UInt16")) {
694 } else if (!strcmp(name, "UInt64")) {
696 } else if (!strcmp(name, "UIntPtr")) {
701 if (!strcmp (name, "Void")) {
709 class->this_arg.type = class->byval_arg.type = t;
713 * Compute the field and method lists
715 class->field.first = cols [MONO_TYPEDEF_FIELD_LIST] - 1;
716 class->method.first = cols [MONO_TYPEDEF_METHOD_LIST] - 1;
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;
723 class->field.last = image->tables [MONO_TABLE_FIELD].rows;
724 class->method.last = image->tables [MONO_TABLE_METHOD].rows;
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;
731 class->field.count = 0;
733 if (cols [MONO_TYPEDEF_METHOD_LIST] <= image->tables [MONO_TABLE_METHOD].rows)
734 class->method.count = class->method.last - class->method.first;
736 class->method.count = 0;
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);
744 if (class->flags & TYPE_ATTRIBUTE_INTERFACE)
745 class->interface_id = interface_id++;
747 //class->interfaces = mono_metadata_interfaces_from_typedef (image, type_token, &class->interface_count);
750 mono_class_init (class);
752 if ((type_token = mono_metadata_nested_in_typedef (image, type_token)))
753 class->nested_in = mono_class_create_from_typedef (image, type_token);
758 mono_class_from_mono_type (MonoType *type)
760 switch (type->type) {
761 case MONO_TYPE_OBJECT:
762 return mono_defaults.object_class;
764 return mono_defaults.void_class;
765 case MONO_TYPE_BOOLEAN:
766 return mono_defaults.boolean_class;
768 return mono_defaults.char_class;
770 return mono_defaults.byte_class;
772 return mono_defaults.sbyte_class;
774 return mono_defaults.int16_class;
776 return mono_defaults.uint16_class;
778 return mono_defaults.int32_class;
780 return mono_defaults.uint32_class;
782 return mono_defaults.int_class;
784 return mono_defaults.uint_class;
786 return mono_defaults.int64_class;
788 return mono_defaults.uint64_class;
790 return mono_defaults.single_class;
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;
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;
806 g_warning ("implement me %02x\n", type->type);
807 g_assert_not_reached ();
814 * @image: context where the image is created
815 * @type_spec: typespec token
816 * @at: an optional pointer to return the array type
819 mono_class_create_from_typespec (MonoImage *image, guint32 type_spec)
821 guint32 idx = mono_metadata_token_index (type_spec);
823 guint32 cols [MONO_TYPESPEC_SIZE];
827 MonoClass *class, *eclass;
829 t = &image->tables [MONO_TABLE_TYPESPEC];
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);
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);
841 case MONO_TYPE_SZARRAY:
842 eclass = mono_class_from_mono_type (type->data.type);
843 class = mono_array_class_get (eclass, 1);
846 g_warning ("implement me: %08x", type->type);
847 g_assert_not_reached ();
850 mono_metadata_free_type (type);
856 * mono_array_class_get:
857 * @eclass: element type class
858 * @rank: the dimension of the array class
860 * Returns: a class object describing the array with element type @etype and
864 mono_array_class_get (MonoClass *eclass, guint32 rank)
868 static MonoClass *parent = NULL;
872 g_assert (rank <= 255);
875 parent = mono_defaults.array_class;
878 mono_class_init (parent);
880 image = eclass->image;
882 g_assert (!eclass->type_token ||
883 mono_metadata_token_table (eclass->type_token) == MONO_TABLE_TYPEDEF);
885 key = ((rank & 0xff) << 24) | (eclass->type_token & 0xffffff);
886 if ((class = g_hash_table_lookup (image->array_cache, GUINT_TO_POINTER (key))))
889 class = g_malloc0 (sizeof (MonoClass) + parent->vtable_size * sizeof (gpointer));
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);
904 class->element_class = eclass;
906 class->byval_arg.type = MONO_TYPE_ARRAY;
907 /* FIXME: complete.... */
909 class->byval_arg.type = MONO_TYPE_SZARRAY;
910 class->byval_arg.data.type = &eclass->byval_arg;
912 class->this_arg = class->byval_arg;
913 class->this_arg.byref = 1;
915 g_hash_table_insert (image->array_cache, GUINT_TO_POINTER (key), class);
920 * mono_class_instance_size:
923 * Returns: the size of an object instance
926 mono_class_instance_size (MonoClass *klass)
930 mono_class_init (klass);
932 return klass->instance_size;
936 * mono_class_value_size:
939 * This function is used for value types, and return the
940 * space and the alignment to store that kind of value object.
942 * Returns: the size of a value of kind @klass
945 mono_class_value_size (MonoClass *klass, guint32 *align)
949 /* fixme: check disable, because we still have external revereces to
950 * mscorlib and Dummy Objects
952 /*g_assert (klass->valuetype);*/
954 size = mono_class_instance_size (klass) - sizeof (MonoObject);
957 *align = klass->min_align;
963 * mono_class_data_size:
966 * Returns: the size of the static class data
969 mono_class_data_size (MonoClass *klass)
973 mono_class_init (klass);
975 return klass->class_size;
979 * Auxiliary routine to mono_class_get_field
981 * Takes a field index instead of a field token.
983 static MonoClassField *
984 mono_class_get_field_idx (MonoClass *class, int idx)
986 if (class->field.count){
987 if ((idx >= class->field.first) && (idx < class->field.last)){
988 return &class->fields [idx - class->field.first];
995 return mono_class_get_field_idx (class->parent, idx);
999 * mono_class_get_field:
1000 * @class: the class to lookup the field.
1001 * @field_token: the field token
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
1008 mono_class_get_field (MonoClass *class, guint32 field_token)
1010 int idx = mono_metadata_token_index (field_token);
1012 g_assert (mono_metadata_token_code (field_token) == MONO_TOKEN_FIELD_DEF);
1014 return mono_class_get_field_idx (class, idx - 1);
1018 mono_class_get_field_from_name (MonoClass *klass, const char *name)
1022 MonoTableInfo *t = &klass->image->tables [MONO_TABLE_FIELD];
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];
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
1038 * Returns: the MonoClass that represents @type_token in @image
1041 mono_class_get (MonoImage *image, guint32 type_token)
1045 switch (type_token & 0xff000000){
1046 case MONO_TOKEN_TYPE_DEF:
1047 class = mono_class_create_from_typedef (image, type_token);
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);
1055 g_warning ("unknown token type %x", type_token & 0xff000000);
1056 g_assert_not_reached ();
1063 mono_class_from_name (MonoImage *image, const char* name_space, const char *name)
1065 GHashTable *nspace_table;
1068 nspace_table = g_hash_table_lookup (image->name_cache, name_space);
1071 token = GPOINTER_TO_UINT (g_hash_table_lookup (nspace_table, name));
1074 /*g_warning ("token not found for %s.%s in image %s", name_space, name, image->name);*/
1078 token = MONO_TOKEN_TYPE_DEF | token;
1080 return mono_class_get (image, token);
1084 * mono_array_element_size:
1085 * @ac: pointer to a #MonoArrayClass
1087 * Returns: the size of single array element.
1090 mono_array_element_size (MonoClass *ac)
1092 if (ac->element_class->valuetype)
1093 return mono_class_instance_size (ac->element_class) - sizeof (MonoObject);
1095 return sizeof (gpointer);
1099 mono_ldtoken (MonoImage *image, guint32 token, MonoClass **handle_class)
1101 switch (token & 0xff000000) {
1102 case MONO_TOKEN_TYPE_DEF:
1103 case MONO_TOKEN_TYPE_REF: {
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;
1111 case MONO_TOKEN_TYPE_SPEC: {
1114 *handle_class = mono_defaults.typehandle_class;
1115 class = mono_class_create_from_typespec (image, token);
1116 return &class->byval_arg;
1118 case MONO_TOKEN_FIELD_DEF: {
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);
1124 *handle_class = mono_class_from_name (mono_defaults.corlib, "System", "RuntimeFieldHandle");
1125 return mono_class_get_field (class, token);
1127 case MONO_TOKEN_METHOD_DEF:
1128 case MONO_TOKEN_MEMBER_REF:
1130 g_warning ("Unknown token 0x%08x in ldtoken", token);