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>
29 #include <mono/metadata/appdomain.h>
30 #include <mono/metadata/mono-endian.h>
31 #include <mono/metadata/debug-helpers.h>
36 #define CSIZE(x) (sizeof (x) / 4)
40 gboolean mono_print_vtable = FALSE;
42 static MonoClass * mono_class_create_from_typedef (MonoImage *image, guint32 type_token);
45 mono_class_from_typeref (MonoImage *image, guint32 type_token)
47 guint32 cols [MONO_TYPEREF_SIZE];
48 MonoTableInfo *t = &image->tables [MONO_TABLE_TYPEREF];
50 const char *name, *nspace;
53 mono_metadata_decode_row (t, (type_token&0xffffff)-1, cols, MONO_TYPEREF_SIZE);
55 name = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAME]);
56 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAMESPACE]);
58 idx = cols [MONO_TYPEREF_SCOPE] >> RESOLTION_SCOPE_BITS;
59 switch (cols [MONO_TYPEREF_SCOPE] & RESOLTION_SCOPE_MASK) {
60 case RESOLTION_SCOPE_MODULE:
62 g_error ("null ResolutionScope not yet handled");
63 /* a typedef in disguise */
64 return mono_class_from_name (image, nspace, name);
65 case RESOLTION_SCOPE_MODULEREF:
66 return mono_class_from_name (image->assembly->modules [idx - 1], nspace, name);
67 case RESOLTION_SCOPE_TYPEREF: {
68 MonoClass *enclosing = mono_class_from_typeref (image, MONO_TOKEN_TYPE_REF | idx);
70 mono_class_init (enclosing);
71 for (tmp = enclosing->nested_classes; tmp; tmp = tmp->next) {
73 if (strcmp (res->name, name) == 0)
76 g_warning ("TypeRef ResolutionScope not yet handled (%d)", idx);
79 case RESOLTION_SCOPE_ASSEMBLYREF:
83 if (!image->references || !image->references [idx-1]) {
85 * detected a reference to mscorlib, we simply return a reference to a dummy
86 * until we have a better solution.
88 fprintf(stderr, "Sending dummy where %s.%s expected\n", mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAMESPACE]), mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAME]));
90 res = mono_class_from_name (image, "System", "MonoDummy");
91 /* prevent method loading */
93 /* some storage if the type is used - very ugly hack */
94 res->instance_size = 2*sizeof (gpointer);
98 /* load referenced assembly */
99 image = image->references [idx-1]->image;
101 return mono_class_from_name (image, nspace, name);
105 mono_marshal_load_type_info (MonoClass* klass)
108 MonoMarshalType *info;
111 g_assert (klass != NULL);
113 if (klass->marshal_info)
114 return klass->marshal_info;
117 mono_class_init (klass);
119 for (i = 0; i < klass->field.count; ++i) {
120 if (klass->fields [i].type->attrs & FIELD_ATTRIBUTE_STATIC)
125 layout = klass->flags & TYPE_ATTRIBUTE_LAYOUT_MASK;
127 klass->marshal_info = info = g_malloc0 (sizeof (MonoMarshalType) + sizeof (MonoMarshalField) * count);
128 info->num_fields = count;
131 info->native_size = mono_class_native_size (klass->parent);
133 for (j = i = 0; i < klass->field.count; ++i) {
136 if (klass->fields [i].type->attrs & FIELD_ATTRIBUTE_STATIC)
139 if (klass->fields [i].type->attrs & FIELD_ATTRIBUTE_HAS_FIELD_MARSHAL)
140 mono_metadata_field_info (klass->image, klass->field.first + i,
141 NULL, NULL, &info->fields [j].mspec);
143 info->fields [j].field = &klass->fields [i];
146 case TYPE_ATTRIBUTE_AUTO_LAYOUT:
147 case TYPE_ATTRIBUTE_SEQUENTIAL_LAYOUT:
148 size = mono_marshal_type_size (klass->fields [i].type, info->fields [j].mspec,
149 &align, TRUE, klass->unicode);
150 align = klass->packing_size ? MIN (klass->packing_size, align): align;
151 info->fields [j].offset = info->native_size;
152 info->fields [j].offset += align - 1;
153 info->fields [j].offset &= ~(align - 1);
154 info->native_size = info->fields [j].offset + size;
156 case TYPE_ATTRIBUTE_EXPLICIT_LAYOUT:
158 info->fields [j].offset = klass->fields [i].offset - sizeof (MonoObject);
159 info->native_size = klass->instance_size - sizeof (MonoObject);
165 if (info->native_size & (klass->min_align - 1)) {
166 info->native_size += klass->min_align - 1;
167 info->native_size &= ~(klass->min_align - 1);
170 return klass->marshal_info;
174 * class_compute_field_layout:
175 * @m: pointer to the metadata.
176 * @class: The class to initialize
178 * Initializes the class->fields.
180 * Currently we only support AUTO_LAYOUT, and do not even try to do
181 * a good job at it. This is temporary to get the code for Paolo.
184 class_compute_field_layout (MonoClass *class)
186 MonoImage *m = class->image;
187 const int top = class->field.count;
188 guint32 layout = class->flags & TYPE_ATTRIBUTE_LAYOUT_MASK;
189 MonoTableInfo *t = &m->tables [MONO_TABLE_FIELD];
190 int i, blittable = TRUE;
194 * Fetch all the field information.
196 for (i = 0; i < top; i++){
198 guint32 cols [MONO_FIELD_SIZE];
199 int idx = class->field.first + i;
201 mono_metadata_decode_row (t, idx, cols, CSIZE (cols));
202 /* The name is needed for fieldrefs */
203 class->fields [i].name = mono_metadata_string_heap (m, cols [MONO_FIELD_NAME]);
204 sig = mono_metadata_blob_heap (m, cols [MONO_FIELD_SIGNATURE]);
205 mono_metadata_decode_value (sig, &sig);
206 /* FIELD signature == 0x06 */
207 g_assert (*sig == 0x06);
208 class->fields [i].type = mono_metadata_parse_field_type (
209 m, cols [MONO_FIELD_FLAGS], sig + 1, &sig);
211 if (!(class->fields [i].type->attrs & FIELD_ATTRIBUTE_STATIC)) {
212 if (class->fields [i].type->byref) {
215 MonoClass *field_class = mono_class_from_mono_type (class->fields [i].type);
216 if (!field_class->blittable)
221 if (cols [MONO_FIELD_FLAGS] & FIELD_ATTRIBUTE_HAS_FIELD_RVA) {
222 mono_metadata_field_info (m, idx, NULL, &rva, NULL);
224 g_warning ("field %s in %s should have RVA data, but hasn't", class->fields [i].name, class->name);
225 class->fields [i].data = mono_cli_rva_map (class->image->image_info, rva);
228 if (class->enumtype && !(cols [MONO_FIELD_FLAGS] & FIELD_ATTRIBUTE_STATIC)) {
229 class->enum_basetype = class->fields [i].type;
230 class->element_class = mono_class_from_mono_type (class->enum_basetype);
231 blittable = class->element_class->blittable;
235 class->blittable = blittable;
237 if (class->enumtype && !class->enum_basetype) {
238 if (!((strcmp (class->name, "Enum") == 0) && (strcmp (class->name_space, "System") == 0)))
242 * Compute field layout and total size (not considering static fields)
245 case TYPE_ATTRIBUTE_AUTO_LAYOUT:
246 case TYPE_ATTRIBUTE_SEQUENTIAL_LAYOUT:
247 for (i = 0; i < top; i++){
250 if (class->fields [i].type->attrs & FIELD_ATTRIBUTE_STATIC)
253 size = mono_type_size (class->fields [i].type, &align);
255 /* FIXME (LAMESPEC): should we also change the min alignment according to pack? */
256 align = class->packing_size ? MIN (class->packing_size, align): align;
257 class->min_align = MAX (align, class->min_align);
258 class->fields [i].offset = class->instance_size;
259 class->fields [i].offset += align - 1;
260 class->fields [i].offset &= ~(align - 1);
261 class->instance_size = class->fields [i].offset + size;
265 if (class->instance_size & (class->min_align - 1)) {
266 class->instance_size += class->min_align - 1;
267 class->instance_size &= ~(class->min_align - 1);
270 case TYPE_ATTRIBUTE_EXPLICIT_LAYOUT:
271 for (i = 0; i < top; i++) {
273 int idx = class->field.first + i;
276 * There must be info about all the fields in a type if it
277 * uses explicit layout.
280 if (class->fields [i].type->attrs & FIELD_ATTRIBUTE_STATIC)
283 size = mono_type_size (class->fields [i].type, &align);
285 mono_metadata_field_info (m, idx, &class->fields [i].offset, NULL, NULL);
286 if (class->fields [i].offset == (guint32)-1)
287 g_warning ("%s not initialized correctly (missing field layout info for %s)", class->name, class->fields [i].name);
289 * The offset is from the start of the object: this works for both
290 * classes and valuetypes.
292 class->fields [i].offset += sizeof (MonoObject);
296 class->instance_size = MAX (class->instance_size, size + class->fields [i].offset);
301 class->size_inited = 1;
304 * Compute static field layout and size
307 case TYPE_ATTRIBUTE_AUTO_LAYOUT:
308 case TYPE_ATTRIBUTE_SEQUENTIAL_LAYOUT:
309 for (i = 0; i < top; i++){
312 if (!(class->fields [i].type->attrs & FIELD_ATTRIBUTE_STATIC))
315 size = mono_type_size (class->fields [i].type, &align);
316 class->fields [i].offset = class->class_size;
317 class->fields [i].offset += align - 1;
318 class->fields [i].offset &= ~(align - 1);
319 class->class_size = class->fields [i].offset + size;
322 case TYPE_ATTRIBUTE_EXPLICIT_LAYOUT:
323 for (i = 0; i < top; i++){
327 * There must be info about all the fields in a type if it
328 * uses explicit layout.
332 if (!(class->fields [i].type->attrs & FIELD_ATTRIBUTE_STATIC))
335 size = mono_type_size (class->fields [i].type, &align);
336 class->fields [i].offset = class->class_size;
337 class->fields [i].offset += align - 1;
338 class->fields [i].offset &= ~(align - 1);
339 class->class_size = class->fields [i].offset + size;
346 init_properties (MonoClass *class)
348 guint startm, endm, i, j;
349 guint32 cols [MONO_PROPERTY_SIZE];
350 MonoTableInfo *pt = &class->image->tables [MONO_TABLE_PROPERTY];
351 MonoTableInfo *msemt = &class->image->tables [MONO_TABLE_METHODSEMANTICS];
353 class->property.first = mono_metadata_properties_from_typedef (class->image, mono_metadata_token_index (class->type_token) - 1, &class->property.last);
354 class->property.count = class->property.last - class->property.first;
356 class->properties = g_new0 (MonoProperty, class->property.count);
357 for (i = class->property.first; i < class->property.last; ++i) {
358 mono_metadata_decode_row (pt, i, cols, MONO_PROPERTY_SIZE);
359 class->properties [i - class->property.first].attrs = cols [MONO_PROPERTY_FLAGS];
360 class->properties [i - class->property.first].name = mono_metadata_string_heap (class->image, cols [MONO_PROPERTY_NAME]);
362 startm = mono_metadata_methods_from_property (class->image, i, &endm);
363 for (j = startm; j < endm; ++j) {
364 mono_metadata_decode_row (msemt, j, cols, MONO_METHOD_SEMA_SIZE);
365 switch (cols [MONO_METHOD_SEMA_SEMANTICS]) {
366 case METHOD_SEMANTIC_SETTER:
367 class->properties [i - class->property.first].set = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
369 case METHOD_SEMANTIC_GETTER:
370 class->properties [i - class->property.first].get = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
380 init_events (MonoClass *class)
382 guint startm, endm, i, j;
383 guint32 cols [MONO_EVENT_SIZE];
384 MonoTableInfo *pt = &class->image->tables [MONO_TABLE_EVENT];
385 MonoTableInfo *msemt = &class->image->tables [MONO_TABLE_METHODSEMANTICS];
387 class->event.first = mono_metadata_events_from_typedef (class->image, mono_metadata_token_index (class->type_token) - 1, &class->event.last);
388 class->event.count = class->event.last - class->event.first;
390 class->events = g_new0 (MonoEvent, class->event.count);
391 for (i = class->event.first; i < class->event.last; ++i) {
392 mono_metadata_decode_row (pt, i, cols, MONO_EVENT_SIZE);
393 class->events [i - class->event.first].attrs = cols [MONO_EVENT_FLAGS];
394 class->events [i - class->event.first].name = mono_metadata_string_heap (class->image, cols [MONO_EVENT_NAME]);
396 startm = mono_metadata_methods_from_event (class->image, i, &endm);
397 for (j = startm; j < endm; ++j) {
398 mono_metadata_decode_row (msemt, j, cols, MONO_METHOD_SEMA_SIZE);
399 switch (cols [MONO_METHOD_SEMA_SEMANTICS]) {
400 case METHOD_SEMANTIC_ADD_ON:
401 class->events [i - class->event.first].add = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
403 case METHOD_SEMANTIC_REMOVE_ON:
404 class->events [i - class->event.first].remove = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
406 case METHOD_SEMANTIC_FIRE:
407 class->events [i - class->event.first].raise = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
409 case METHOD_SEMANTIC_OTHER: /* don't care for now */
418 mono_get_unique_iid (MonoClass *class)
420 static GHashTable *iid_hash = NULL;
421 static guint iid = 0;
426 g_assert (class->flags & TYPE_ATTRIBUTE_INTERFACE);
429 iid_hash = g_hash_table_new (g_str_hash, g_str_equal);
431 str = g_strdup_printf ("%s|%s.%s\n", class->image->name, class->name_space, class->name);
433 if (g_hash_table_lookup_extended (iid_hash, str, NULL, &value)) {
437 g_hash_table_insert (iid_hash, str, (gpointer)iid);
446 * @class: the class to initialize
448 * compute the instance_size, class_size and other infos that cannot be
449 * computed at mono_class_get() time. Also compute a generic vtable and
450 * the method slot numbers. We use this infos later to create a domain
454 mono_class_init (MonoClass *class)
458 int i, max_vtsize = 0, max_iid, cur_slot = 0;
459 static MonoMethod *default_ghc = NULL;
460 static MonoMethod *default_finalize = NULL;
461 static int finalize_slot = -1;
462 static int ghc_slot = -1;
463 guint32 packing_size = 0;
470 if (class->init_pending) {
471 /* this indicates a cyclic dependency */
472 g_error ("pending init %s.%s\n", class->name_space, class->name);
475 class->init_pending = 1;
477 mono_stats.initialized_class_count++;
480 if (!class->parent->inited)
481 mono_class_init (class->parent);
482 class->instance_size += class->parent->instance_size;
483 class->class_size += class->parent->class_size;
484 class->min_align = class->parent->min_align;
485 cur_slot = class->parent->vtable_size;
487 class->min_align = 1;
489 if (mono_metadata_packing_from_typedef (class->image, class->type_token, &packing_size, &class->instance_size)) {
490 class->instance_size += sizeof (MonoObject);
493 g_assert ((packing_size & 0xfffffff0) == 0);
494 class->packing_size = packing_size;
497 * Computes the size used by the fields, and their locations
499 if (!class->size_inited && class->field.count > 0){
500 class->fields = g_new0 (MonoClassField, class->field.count);
501 class_compute_field_layout (class);
504 if (!(class->flags & TYPE_ATTRIBUTE_INTERFACE)) {
505 for (i = 0; i < class->interface_count; i++)
506 max_vtsize += class->interfaces [i]->method.count;
509 max_vtsize += class->parent->vtable_size;
511 max_vtsize += class->method.count;
514 vtable = alloca (sizeof (gpointer) * max_vtsize);
515 memset (vtable, 0, sizeof (gpointer) * max_vtsize);
517 /* initialize method pointers */
518 class->methods = g_new (MonoMethod*, class->method.count);
519 for (i = 0; i < class->method.count; ++i)
520 class->methods [i] = mono_get_method (class->image,
521 MONO_TOKEN_METHOD_DEF | (i + class->method.first + 1), class);
523 init_properties (class);
526 i = mono_metadata_nesting_typedef (class->image, class->type_token);
529 guint32 cols [MONO_NESTED_CLASS_SIZE];
530 mono_metadata_decode_row (&class->image->tables [MONO_TABLE_NESTEDCLASS], i - 1, cols, MONO_NESTED_CLASS_SIZE);
531 if (cols [MONO_NESTED_CLASS_ENCLOSING] != mono_metadata_token_index (class->type_token))
533 nclass = mono_class_create_from_typedef (class->image, MONO_TOKEN_TYPE_DEF | cols [MONO_NESTED_CLASS_NESTED]);
534 class->nested_classes = g_list_prepend (class->nested_classes, nclass);
538 if (class->flags & TYPE_ATTRIBUTE_INTERFACE) {
539 for (i = 0; i < class->method.count; ++i)
540 class->methods [i]->slot = i;
541 class->init_pending = 0;
546 /* printf ("METAINIT %s.%s\n", class->name_space, class->name); */
548 /* compute maximum number of slots and maximum interface id */
550 for (k = class; k ; k = k->parent) {
551 for (i = 0; i < k->interface_count; i++) {
552 ic = k->interfaces [i];
555 mono_class_init (ic);
557 if (max_iid < ic->interface_id)
558 max_iid = ic->interface_id;
562 class->max_interface_id = max_iid;
563 /* compute vtable offset for interfaces */
564 class->interface_offsets = g_malloc (sizeof (gpointer) * (max_iid + 1));
566 for (i = 0; i <= max_iid; i++)
567 class->interface_offsets [i] = -1;
569 for (i = 0; i < class->interface_count; i++) {
570 ic = class->interfaces [i];
571 class->interface_offsets [ic->interface_id] = cur_slot;
572 cur_slot += ic->method.count;
575 for (k = class->parent; k ; k = k->parent) {
576 for (i = 0; i < k->interface_count; i++) {
577 ic = k->interfaces [i];
578 if (class->interface_offsets [ic->interface_id] == -1) {
579 int io = k->interface_offsets [ic->interface_id];
582 g_assert (io <= max_vtsize);
584 class->interface_offsets [ic->interface_id] = io;
589 if (class->parent && class->parent->vtable_size)
590 memcpy (vtable, class->parent->vtable, sizeof (gpointer) * class->parent->vtable_size);
592 for (k = class; k ; k = k->parent) {
593 for (i = 0; i < k->interface_count; i++) {
596 ic = k->interfaces [i];
597 io = k->interface_offsets [ic->interface_id];
600 g_assert (io <= max_vtsize);
603 for (l = 0; l < ic->method.count; l++) {
604 MonoMethod *im = ic->methods [l];
605 for (j = 0; j < class->method.count; ++j) {
606 MonoMethod *cm = class->methods [j];
607 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
608 !(cm->flags & METHOD_ATTRIBUTE_PUBLIC) ||
609 !(cm->flags & METHOD_ATTRIBUTE_NEW_SLOT))
611 if (!strcmp(cm->name, im->name) &&
612 mono_metadata_signature_equal (cm->signature, im->signature)) {
613 g_assert (io + l <= max_vtsize);
614 vtable [io + l] = cm;
619 /* already implemented */
620 if (io >= k->vtable_size)
624 for (l = 0; l < ic->method.count; l++) {
625 MonoMethod *im = ic->methods [l];
628 g_assert (io + l <= max_vtsize);
633 for (k1 = class; k1; k1 = k1->parent) {
634 for (j = 0; j < k1->method.count; ++j) {
635 MonoMethod *cm = k1->methods [j];
637 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
638 !(cm->flags & METHOD_ATTRIBUTE_PUBLIC))
641 if (!strcmp(cm->name, im->name) &&
642 mono_metadata_signature_equal (cm->signature, im->signature)) {
643 g_assert (io + l <= max_vtsize);
644 vtable [io + l] = cm;
649 g_assert (io + l <= max_vtsize);
655 for (l = 0; l < ic->method.count; l++) {
656 MonoMethod *im = ic->methods [l];
657 char *qname, *fqname;
659 qname = g_strconcat (ic->name, ".", im->name, NULL);
660 if (ic->name_space && ic->name_space [0])
661 fqname = g_strconcat (ic->name_space, ".", ic->name, ".", im->name, NULL);
665 for (j = 0; j < class->method.count; ++j) {
666 MonoMethod *cm = class->methods [j];
668 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL))
671 if (((fqname && !strcmp (cm->name, fqname)) || !strcmp (cm->name, qname)) &&
672 mono_metadata_signature_equal (cm->signature, im->signature)) {
673 g_assert (io + l <= max_vtsize);
674 vtable [io + l] = cm;
683 if (!(class->flags & TYPE_ATTRIBUTE_ABSTRACT)) {
684 for (l = 0; l < ic->method.count; l++) {
686 MonoMethod *im = ic->methods [l];
687 g_assert (io + l <= max_vtsize);
688 if (!(vtable [io + l])) {
689 msig = mono_signature_get_desc (im->signature, FALSE);
690 printf ("no implementation for interface method %s.%s::%s(%s) in class %s.%s\n",
691 ic->name_space, ic->name, im->name, msig, class->name_space, class->name);
693 for (j = 0; j < class->method.count; ++j) {
694 MonoMethod *cm = class->methods [j];
695 msig = mono_signature_get_desc (cm->signature, FALSE);
697 printf ("METHOD %s(%s)\n", cm->name, msig);
700 g_assert_not_reached ();
705 for (l = 0; l < ic->method.count; l++) {
706 MonoMethod *im = vtable [io + l];
709 g_assert (io + l <= max_vtsize);
711 /* FIXME: why do we need this ? */
713 /* g_assert_not_reached (); */
720 for (i = 0; i < class->method.count; ++i) {
723 cm = class->methods [i];
727 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
730 #else /* EXT_VTABLE_HACK */
735 if (!(cm->flags & METHOD_ATTRIBUTE_NEW_SLOT) && (cm->flags & METHOD_ATTRIBUTE_VIRTUAL)) {
736 for (k = class->parent; k ; k = k->parent) {
738 for (j = 0; j < k->method.count; ++j) {
739 MonoMethod *m1 = k->methods [j];
740 if (!(m1->flags & METHOD_ATTRIBUTE_VIRTUAL))
742 if (!strcmp(cm->name, m1->name) &&
743 mono_metadata_signature_equal (cm->signature, m1->signature)) {
744 cm->slot = k->methods [j]->slot;
745 g_assert (cm->slot < max_vtsize);
755 cm->slot = cur_slot++;
757 if (!(cm->flags & METHOD_ATTRIBUTE_ABSTRACT))
758 vtable [cm->slot] = cm;
762 class->vtable_size = cur_slot;
763 class->vtable = g_malloc0 (sizeof (gpointer) * class->vtable_size);
764 memcpy (class->vtable, vtable, sizeof (gpointer) * class->vtable_size);
767 class->init_pending = 0;
769 if (mono_print_vtable) {
772 for (i = 0; i <= max_iid; i++)
773 if (class->interface_offsets [i] != -1)
776 printf ("VTable %s.%s (size = %d, interfaces = %d)\n", class->name_space,
777 class->name, class->vtable_size, icount);
779 for (i = 0; i < class->vtable_size; ++i) {
784 printf (" slot %03d(%03d) %s.%s:%s\n", i, cm->slot,
785 cm->klass->name_space, cm->klass->name,
792 printf ("Interfaces %s.%s (max_iid = %d)\n", class->name_space,
793 class->name, max_iid);
795 for (i = 0; i < class->interface_count; i++) {
796 ic = class->interfaces [i];
797 printf (" slot %03d(%03d) %s.%s\n",
798 class->interface_offsets [ic->interface_id],
799 ic->method.count, ic->name_space, ic->name);
802 for (k = class->parent; k ; k = k->parent) {
803 for (i = 0; i < k->interface_count; i++) {
804 ic = k->interfaces [i];
805 printf (" slot %03d(%03d) %s.%s\n",
806 class->interface_offsets [ic->interface_id],
807 ic->method.count, ic->name_space, ic->name);
814 if (class == mono_defaults.object_class) {
816 for (i = 0; i < class->vtable_size; ++i) {
817 MonoMethod *cm = vtable [i];
819 if (!strcmp (cm->name, "GetHashCode")) {
825 g_assert (ghc_slot > 0);
827 default_ghc = vtable [ghc_slot];
832 if (class != mono_defaults.object_class) {
834 if (vtable [ghc_slot] == default_ghc) {
839 if (!default_finalize) {
840 if (class == mono_defaults.object_class) {
842 for (i = 0; i < class->vtable_size; ++i) {
843 MonoMethod *cm = vtable [i];
845 if (!strcmp (cm->name, "Finalize")) {
851 g_assert (finalize_slot > 0);
853 default_finalize = vtable [finalize_slot];
857 /* Object::Finalize should have empty implemenatation */
858 class->has_finalize = 0;
859 if (class != mono_defaults.object_class) {
860 if (vtable [finalize_slot] != default_finalize)
861 class->has_finalize = 1;
867 * Compute a relative numbering of the class hierarchy as described in
868 * "Java for Large-Scale Scientific Computations?"
871 mono_compute_relative_numbering (MonoClass *class, int *c)
879 for (s = class->subclasses; s; s = s->next)
880 mono_compute_relative_numbering ((MonoClass *)s->data, c);
882 class->diffval = *c - class->baseval;
886 mono_class_setup_mono_type (MonoClass *class)
888 const char *name = class->name;
889 const char *nspace = class->name_space;
891 class->this_arg.byref = 1;
892 class->this_arg.data.klass = class;
893 class->this_arg.type = MONO_TYPE_CLASS;
894 class->byval_arg.data.klass = class;
895 class->byval_arg.type = MONO_TYPE_CLASS;
897 if (!strcmp (nspace, "System")) {
898 if (!strcmp (name, "ValueType")) {
900 * do not set the valuetype bit for System.ValueType.
901 * class->valuetype = 1;
903 } else if (!strcmp (name, "Enum")) {
905 * do not set the valuetype bit for System.Enum.
906 * class->valuetype = 1;
908 class->valuetype = 0;
910 } else if (!strcmp (name, "Object")) {
911 class->this_arg.type = class->byval_arg.type = MONO_TYPE_OBJECT;
912 } else if (!strcmp (name, "String")) {
913 class->this_arg.type = class->byval_arg.type = MONO_TYPE_STRING;
917 if (class->valuetype) {
918 int t = MONO_TYPE_VALUETYPE;
919 if (!strcmp (nspace, "System")) {
922 if (!strcmp (name, "Boolean")) {
923 t = MONO_TYPE_BOOLEAN;
924 } else if (!strcmp(name, "Byte")) {
926 class->blittable = TRUE;
930 if (!strcmp (name, "Char")) {
935 if (!strcmp (name, "Double")) {
940 if (!strcmp (name, "Int32")) {
942 class->blittable = TRUE;
943 } else if (!strcmp(name, "Int16")) {
945 class->blittable = TRUE;
946 } else if (!strcmp(name, "Int64")) {
948 class->blittable = TRUE;
949 } else if (!strcmp(name, "IntPtr")) {
951 class->blittable = TRUE;
955 if (!strcmp (name, "Single")) {
957 } else if (!strcmp(name, "SByte")) {
959 class->blittable = TRUE;
963 if (!strcmp (name, "UInt32")) {
965 class->blittable = TRUE;
966 } else if (!strcmp(name, "UInt16")) {
968 class->blittable = TRUE;
969 } else if (!strcmp(name, "UInt64")) {
971 class->blittable = TRUE;
972 } else if (!strcmp(name, "UIntPtr")) {
974 class->blittable = TRUE;
978 if (!strcmp (name, "Void")) {
986 class->this_arg.type = class->byval_arg.type = t;
991 mono_class_setup_parent (MonoClass *class, MonoClass *parent)
993 gboolean system_namespace;
995 system_namespace = !strcmp (class->name_space, "System");
997 /* if root of the hierarchy */
998 if (system_namespace && !strcmp (class->name, "Object")) {
999 class->parent = NULL;
1000 class->instance_size = sizeof (MonoObject);
1003 if (!strcmp (class->name, "<Module>")) {
1004 class->parent = NULL;
1005 class->instance_size = 0;
1009 if (!(class->flags & TYPE_ATTRIBUTE_INTERFACE)) {
1011 class->parent = parent;
1014 g_assert_not_reached (); /* FIXME */
1016 class->marshalbyref = parent->marshalbyref;
1017 class->contextbound = parent->contextbound;
1018 class->delegate = parent->delegate;
1020 if (system_namespace) {
1021 if (*class->name == 'M' && !strcmp (class->name, "MarshalByRefObject"))
1022 class->marshalbyref = 1;
1024 if (*class->name == 'C' && !strcmp (class->name, "ContextBoundObject"))
1025 class->contextbound = 1;
1027 if (*class->name == 'D' && !strcmp (class->name, "Delegate"))
1028 class->delegate = 1;
1031 if (class->parent->enumtype || ((strcmp (class->parent->name, "ValueType") == 0) &&
1032 (strcmp (class->parent->name_space, "System") == 0)))
1033 class->valuetype = 1;
1034 if (((strcmp (class->parent->name, "Enum") == 0) && (strcmp (class->parent->name_space, "System") == 0))) {
1035 class->valuetype = class->enumtype = 1;
1037 /*class->enumtype = class->parent->enumtype; */
1038 class->parent->subclasses = g_list_prepend (class->parent->subclasses, class);
1039 mono_compute_relative_numbering (mono_defaults.object_class, &rnum);
1041 class->parent = NULL;
1047 * @image: context where the image is created
1048 * @type_token: typedef token
1051 mono_class_create_from_typedef (MonoImage *image, guint32 type_token)
1053 MonoTableInfo *tt = &image->tables [MONO_TABLE_TYPEDEF];
1054 MonoClass *class, *parent = NULL;
1055 guint32 cols [MONO_TYPEDEF_SIZE];
1056 guint32 cols_next [MONO_TYPEDEF_SIZE];
1057 guint tidx = mono_metadata_token_index (type_token);
1058 const char *name, *nspace;
1060 MonoClass **interfaces;
1062 if ((class = g_hash_table_lookup (image->class_cache, GUINT_TO_POINTER (type_token))))
1065 g_assert (mono_metadata_token_table (type_token) == MONO_TABLE_TYPEDEF);
1067 mono_metadata_decode_row (tt, tidx - 1, cols, MONO_TYPEDEF_SIZE);
1069 name = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
1070 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
1072 if (cols [MONO_TYPEDEF_EXTENDS])
1073 parent = mono_class_get (image, mono_metadata_token_from_dor (cols [MONO_TYPEDEF_EXTENDS]));
1074 interfaces = mono_metadata_interfaces_from_typedef (image, type_token, &icount);
1076 class = g_malloc0 (sizeof (MonoClass));
1078 g_hash_table_insert (image->class_cache, GUINT_TO_POINTER (type_token), class);
1080 class->interfaces = interfaces;
1081 class->interface_count = icount;
1084 class->name_space = nspace;
1086 class->image = image;
1087 class->type_token = type_token;
1088 class->flags = cols [MONO_TYPEDEF_FLAGS];
1090 if ((class->flags & TYPE_ATTRIBUTE_STRING_FORMAT_MASK) == TYPE_ATTRIBUTE_UNICODE_CLASS)
1092 /* fixme: maybe we must set this on windows
1093 if ((class->flags & TYPE_ATTRIBUTE_STRING_FORMAT_MASK) == TYPE_ATTRIBUTE_AUTO_CLASS)
1097 class->element_class = class;
1099 /*g_print ("Init class %s\n", name);*/
1101 mono_class_setup_parent (class, parent);
1103 mono_class_setup_mono_type (class);
1106 * Compute the field and method lists
1108 class->field.first = cols [MONO_TYPEDEF_FIELD_LIST] - 1;
1109 class->method.first = cols [MONO_TYPEDEF_METHOD_LIST] - 1;
1111 if (tt->rows > tidx){
1112 mono_metadata_decode_row (tt, tidx, cols_next, CSIZE (cols_next));
1113 class->field.last = cols_next [MONO_TYPEDEF_FIELD_LIST] - 1;
1114 class->method.last = cols_next [MONO_TYPEDEF_METHOD_LIST] - 1;
1116 class->field.last = image->tables [MONO_TABLE_FIELD].rows;
1117 class->method.last = image->tables [MONO_TABLE_METHOD].rows;
1120 if (cols [MONO_TYPEDEF_FIELD_LIST] &&
1121 cols [MONO_TYPEDEF_FIELD_LIST] <= image->tables [MONO_TABLE_FIELD].rows)
1122 class->field.count = class->field.last - class->field.first;
1124 class->field.count = 0;
1126 if (cols [MONO_TYPEDEF_METHOD_LIST] <= image->tables [MONO_TABLE_METHOD].rows)
1127 class->method.count = class->method.last - class->method.first;
1129 class->method.count = 0;
1131 /* reserve space to store vector pointer in arrays */
1132 if (!strcmp (nspace, "System") && !strcmp (name, "Array")) {
1133 class->instance_size += 2 * sizeof (gpointer);
1134 g_assert (class->field.count == 0);
1137 if (class->flags & TYPE_ATTRIBUTE_INTERFACE)
1138 class->interface_id = mono_get_unique_iid (class);
1140 /*class->interfaces = mono_metadata_interfaces_from_typedef (image, type_token, &class->interface_count); */
1142 if (class->enumtype) {
1143 class->fields = g_new0 (MonoClassField, class->field.count);
1144 class_compute_field_layout (class);
1147 if ((type_token = mono_metadata_nested_in_typedef (image, type_token)))
1148 class->nested_in = mono_class_create_from_typedef (image, type_token);
1154 mono_ptr_class_get (MonoType *type)
1157 MonoClass *el_class;
1158 static GHashTable *ptr_hash = NULL;
1161 ptr_hash = g_hash_table_new (g_direct_hash, g_direct_equal);
1162 el_class = mono_class_from_mono_type (type);
1163 if ((result = g_hash_table_lookup (ptr_hash, el_class)))
1165 result = g_new0 (MonoClass, 1);
1167 result->parent = NULL; /* no parent for PTR types */
1168 result->name = "System";
1169 result->name_space = "MonoPtrFakeClass";
1170 result->image = el_class->image;
1171 result->inited = TRUE;
1172 /* Can pointers get boxed? */
1173 result->instance_size = sizeof (gpointer);
1175 * baseval, diffval: need them to allow casting ?
1177 result->element_class = el_class;
1178 result->enum_basetype = &result->element_class->byval_arg;
1180 result->this_arg.type = result->byval_arg.type = MONO_TYPE_PTR;
1181 result->this_arg.data.type = result->byval_arg.data.type = result->enum_basetype;
1182 result->this_arg.byref = TRUE;
1184 g_hash_table_insert (ptr_hash, el_class, result);
1190 mono_class_from_mono_type (MonoType *type)
1192 switch (type->type) {
1193 case MONO_TYPE_OBJECT:
1194 return mono_defaults.object_class;
1195 case MONO_TYPE_VOID:
1196 return mono_defaults.void_class;
1197 case MONO_TYPE_BOOLEAN:
1198 return mono_defaults.boolean_class;
1199 case MONO_TYPE_CHAR:
1200 return mono_defaults.char_class;
1202 return mono_defaults.sbyte_class;
1204 return mono_defaults.byte_class;
1206 return mono_defaults.int16_class;
1208 return mono_defaults.uint16_class;
1210 return mono_defaults.int32_class;
1212 return mono_defaults.uint32_class;
1214 return mono_defaults.int_class;
1216 return mono_defaults.uint_class;
1218 return mono_defaults.int64_class;
1220 return mono_defaults.uint64_class;
1222 return mono_defaults.single_class;
1224 return mono_defaults.double_class;
1225 case MONO_TYPE_STRING:
1226 return mono_defaults.string_class;
1227 case MONO_TYPE_ARRAY:
1228 return mono_array_class_get (type->data.array->type, type->data.array->rank);
1230 return mono_ptr_class_get (type->data.type);
1231 case MONO_TYPE_SZARRAY:
1232 return mono_array_class_get (type->data.type, 1);
1233 case MONO_TYPE_CLASS:
1234 case MONO_TYPE_VALUETYPE:
1235 return type->data.klass;
1237 g_warning ("implement me %02x\n", type->type);
1238 g_assert_not_reached ();
1245 * @image: context where the image is created
1246 * @type_spec: typespec token
1249 mono_class_create_from_typespec (MonoImage *image, guint32 type_spec)
1254 type = mono_type_create_from_typespec (image, type_spec);
1256 switch (type->type) {
1257 case MONO_TYPE_ARRAY:
1258 class = mono_array_class_get (type->data.array->type, type->data.array->rank);
1260 case MONO_TYPE_SZARRAY:
1261 class = mono_array_class_get (type->data.type, 1);
1264 class = mono_class_from_mono_type (type->data.type);
1267 /* it seems any type can be stored in TypeSpec as well */
1268 class = mono_class_from_mono_type (type);
1272 mono_metadata_free_type (type);
1278 * mono_array_class_get:
1279 * @element_type: element type
1280 * @rank: the dimension of the array class
1282 * Returns: a class object describing the array with element type @element_type and
1286 mono_array_class_get (MonoType *element_type, guint32 rank)
1291 MonoClass *parent = NULL;
1295 eclass = mono_class_from_mono_type (element_type);
1296 g_assert (rank <= 255);
1298 parent = mono_defaults.array_class;
1300 if (!parent->inited)
1301 mono_class_init (parent);
1303 image = eclass->image;
1305 if ((list = g_hash_table_lookup (image->array_cache, element_type))) {
1306 for (; list; list = list->next) {
1308 if (class->rank == rank)
1313 class = g_malloc0 (sizeof (MonoClass) + parent->vtable_size * sizeof (gpointer));
1315 class->image = image;
1316 class->name_space = "System";
1317 class->name = "Array";
1318 class->type_token = 0;
1319 class->flags = TYPE_ATTRIBUTE_CLASS;
1320 class->parent = parent;
1321 class->instance_size = mono_class_instance_size (class->parent);
1322 class->class_size = 0;
1323 class->vtable_size = parent->vtable_size;
1324 class->parent->subclasses = g_list_prepend (class->parent->subclasses, class);
1325 mono_compute_relative_numbering (mono_defaults.object_class, &rnum);
1328 class->element_class = eclass;
1330 MonoArrayType *at = g_new0 (MonoArrayType, 1);
1331 class->byval_arg.type = MONO_TYPE_ARRAY;
1332 class->byval_arg.data.array = at;
1333 at->type = &eclass->byval_arg;
1335 /* FIXME: complete.... */
1337 /* FIXME: this is not correct. the lbound could be >0 */
1338 class->byval_arg.type = MONO_TYPE_SZARRAY;
1339 class->byval_arg.data.type = &eclass->byval_arg;
1341 class->this_arg = class->byval_arg;
1342 class->this_arg.byref = 1;
1344 if (rank == 1 && class->element_class->blittable)
1345 class->blittable = TRUE;
1347 list = g_slist_append (list, class);
1348 g_hash_table_insert (image->array_cache, &class->element_class->byval_arg, list);
1353 * mono_class_instance_size:
1356 * Returns: the size of an object instance
1359 mono_class_instance_size (MonoClass *klass)
1362 if (!klass->size_inited)
1363 mono_class_init (klass);
1365 return klass->instance_size;
1369 * mono_class_native_size:
1372 * Returns: the native size of an object instance (when marshaled
1373 * to unmanaged code)
1376 mono_class_native_size (MonoClass *klass)
1379 if (!klass->marshal_info)
1380 mono_marshal_load_type_info (klass);
1382 return klass->marshal_info->native_size;
1386 * mono_class_min_align:
1389 * Returns: minimm alignment requirements
1392 mono_class_min_align (MonoClass *klass)
1395 if (!klass->size_inited)
1396 mono_class_init (klass);
1398 return klass->min_align;
1402 * mono_class_value_size:
1405 * This function is used for value types, and return the
1406 * space and the alignment to store that kind of value object.
1408 * Returns: the size of a value of kind @klass
1411 mono_class_value_size (MonoClass *klass, guint32 *align)
1415 /* fixme: check disable, because we still have external revereces to
1416 * mscorlib and Dummy Objects
1418 /*g_assert (klass->valuetype);*/
1420 size = mono_class_instance_size (klass) - sizeof (MonoObject);
1423 *align = klass->min_align;
1429 * mono_class_data_size:
1432 * Returns: the size of the static class data
1435 mono_class_data_size (MonoClass *klass)
1439 mono_class_init (klass);
1441 return klass->class_size;
1445 * Auxiliary routine to mono_class_get_field
1447 * Takes a field index instead of a field token.
1449 static MonoClassField *
1450 mono_class_get_field_idx (MonoClass *class, int idx)
1452 if (class->field.count){
1453 if ((idx >= class->field.first) && (idx < class->field.last)){
1454 return &class->fields [idx - class->field.first];
1461 return mono_class_get_field_idx (class->parent, idx);
1465 * mono_class_get_field:
1466 * @class: the class to lookup the field.
1467 * @field_token: the field token
1469 * Returns: A MonoClassField representing the type and offset of
1470 * the field, or a NULL value if the field does not belong to this
1474 mono_class_get_field (MonoClass *class, guint32 field_token)
1476 int idx = mono_metadata_token_index (field_token);
1478 g_assert (mono_metadata_token_code (field_token) == MONO_TOKEN_FIELD_DEF);
1480 return mono_class_get_field_idx (class, idx - 1);
1484 mono_class_get_field_from_name (MonoClass *klass, const char *name)
1488 for (i = 0; i < klass->field.count; ++i) {
1489 if (strcmp (name, klass->fields [i].name) == 0)
1490 return &klass->fields [i];
1497 * @image: the image where the class resides
1498 * @type_token: the token for the class
1499 * @at: an optional pointer to return the array element type
1501 * Returns: the MonoClass that represents @type_token in @image
1504 mono_class_get (MonoImage *image, guint32 type_token)
1508 switch (type_token & 0xff000000){
1509 case MONO_TOKEN_TYPE_DEF:
1510 class = mono_class_create_from_typedef (image, type_token);
1512 case MONO_TOKEN_TYPE_REF:
1513 class = mono_class_from_typeref (image, type_token);
1515 case MONO_TOKEN_TYPE_SPEC:
1516 class = mono_class_create_from_typespec (image, type_token);
1519 g_warning ("unknown token type %x", type_token & 0xff000000);
1520 g_assert_not_reached ();
1524 g_warning ("Could not load class from token 0x%08x in %s", type_token, image->name);
1530 mono_class_from_name_case (MonoImage *image, const char* name_space, const char *name)
1532 MonoTableInfo *t = &image->tables [MONO_TABLE_TYPEDEF];
1533 guint32 cols [MONO_TYPEDEF_SIZE];
1538 /* add a cache if needed */
1539 for (i = 1; i <= t->rows; ++i) {
1540 mono_metadata_decode_row (t, i - 1, cols, MONO_TYPEDEF_SIZE);
1541 /* nested types are accessed from the nesting name */
1542 visib = cols [MONO_TYPEDEF_FLAGS] & TYPE_ATTRIBUTE_VISIBILITY_MASK;
1543 if (visib > TYPE_ATTRIBUTE_PUBLIC && visib <= TYPE_ATTRIBUTE_NESTED_ASSEMBLY)
1545 n = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
1546 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
1547 if (g_strcasecmp (n, name) == 0 && g_strcasecmp (nspace, name_space) == 0)
1548 return mono_class_get (image, MONO_TOKEN_TYPE_DEF | i);
1554 mono_class_from_name (MonoImage *image, const char* name_space, const char *name)
1556 GHashTable *nspace_table;
1559 nspace_table = g_hash_table_lookup (image->name_cache, name_space);
1562 token = GPOINTER_TO_UINT (g_hash_table_lookup (nspace_table, name));
1565 /*g_warning ("token not found for %s.%s in image %s", name_space, name, image->name);*/
1569 token = MONO_TOKEN_TYPE_DEF | token;
1571 return mono_class_get (image, token);
1575 * mono_array_element_size:
1576 * @ac: pointer to a #MonoArrayClass
1578 * Returns: the size of single array element.
1581 mono_array_element_size (MonoClass *ac)
1583 if (ac->element_class->valuetype)
1584 return mono_class_instance_size (ac->element_class) - sizeof (MonoObject);
1586 return sizeof (gpointer);
1590 mono_ldtoken (MonoImage *image, guint32 token, MonoClass **handle_class)
1592 switch (token & 0xff000000) {
1593 case MONO_TOKEN_TYPE_DEF:
1594 case MONO_TOKEN_TYPE_REF: {
1597 *handle_class = mono_defaults.typehandle_class;
1598 class = mono_class_get (image, token);
1599 mono_class_init (class);
1600 /* We return a MonoType* as handle */
1601 return &class->byval_arg;
1603 case MONO_TOKEN_TYPE_SPEC: {
1606 *handle_class = mono_defaults.typehandle_class;
1607 class = mono_class_create_from_typespec (image, token);
1608 mono_class_init (class);
1609 return &class->byval_arg;
1611 case MONO_TOKEN_FIELD_DEF: {
1613 guint32 type = mono_metadata_typedef_from_field (image, mono_metadata_token_index (token));
1614 class = mono_class_get (image, MONO_TOKEN_TYPE_DEF | type);
1615 mono_class_init (class);
1617 *handle_class = mono_class_from_name (mono_defaults.corlib, "System", "RuntimeFieldHandle");
1618 return mono_class_get_field (class, token);
1620 case MONO_TOKEN_METHOD_DEF:
1621 case MONO_TOKEN_MEMBER_REF:
1623 g_warning ("Unknown token 0x%08x in ldtoken", token);