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 * class_compute_field_layout:
106 * @m: pointer to the metadata.
107 * @class: The class to initialize
108 * @pack: the value from the .pack directive (or 0)
110 * Initializes the class->fields.
112 * Currently we only support AUTO_LAYOUT, and do not even try to do
113 * a good job at it. This is temporary to get the code for Paolo.
116 class_compute_field_layout (MonoClass *class, int pack)
118 MonoImage *m = class->image;
119 const int top = class->field.count;
120 guint32 layout = class->flags & TYPE_ATTRIBUTE_LAYOUT_MASK;
121 MonoTableInfo *t = &m->tables [MONO_TABLE_FIELD];
126 * Fetch all the field information.
128 for (i = 0; i < top; i++){
130 guint32 cols [MONO_FIELD_SIZE];
131 int idx = class->field.first + i;
133 mono_metadata_decode_row (t, idx, cols, CSIZE (cols));
134 /* The name is needed for fieldrefs */
135 class->fields [i].name = mono_metadata_string_heap (m, cols [MONO_FIELD_NAME]);
136 sig = mono_metadata_blob_heap (m, cols [MONO_FIELD_SIGNATURE]);
137 mono_metadata_decode_value (sig, &sig);
138 /* FIELD signature == 0x06 */
139 g_assert (*sig == 0x06);
140 class->fields [i].type = mono_metadata_parse_field_type (
141 m, cols [MONO_FIELD_FLAGS], sig + 1, &sig);
142 if (cols [MONO_FIELD_FLAGS] & FIELD_ATTRIBUTE_HAS_FIELD_RVA) {
143 mono_metadata_field_info (m, idx, NULL, &rva, NULL);
145 g_warning ("field %s in %s should have RVA data, but hasn't", class->fields [i].name, class->name);
146 class->fields [i].data = mono_cli_rva_map (class->image->image_info, rva);
148 if (class->enumtype && !(cols [MONO_FIELD_FLAGS] & FIELD_ATTRIBUTE_STATIC)) {
149 class->enum_basetype = class->fields [i].type;
150 class->element_class = mono_class_from_mono_type (class->enum_basetype);
153 if (class->enumtype && !class->enum_basetype) {
154 if (!((strcmp (class->name, "Enum") == 0) && (strcmp (class->name_space, "System") == 0)))
158 * Compute field layout and total size (not considering static fields)
161 case TYPE_ATTRIBUTE_AUTO_LAYOUT:
162 case TYPE_ATTRIBUTE_SEQUENTIAL_LAYOUT:
163 for (i = 0; i < top; i++){
166 if (class->fields [i].type->attrs & FIELD_ATTRIBUTE_STATIC)
169 size = mono_type_size (class->fields [i].type, &align);
171 /* FIXME (LAMESPEC): should we also change the min alignment according to pack? */
172 align = pack? MIN (pack, align): align;
173 class->min_align = MAX (align, class->min_align);
174 class->fields [i].offset = class->instance_size;
175 class->fields [i].offset += align - 1;
176 class->fields [i].offset &= ~(align - 1);
177 class->instance_size = class->fields [i].offset + size;
180 if (class->instance_size & (class->min_align - 1)) {
181 class->instance_size += class->min_align - 1;
182 class->instance_size &= ~(class->min_align - 1);
185 case TYPE_ATTRIBUTE_EXPLICIT_LAYOUT:
186 for (i = 0; i < top; i++) {
188 int idx = class->field.first + i;
191 * There must be info about all the fields in a type if it
192 * uses explicit layout.
195 if (class->fields [i].type->attrs & FIELD_ATTRIBUTE_STATIC)
198 size = mono_type_size (class->fields [i].type, &align);
200 mono_metadata_field_info (m, idx, &class->fields [i].offset, NULL, NULL);
201 if (class->fields [i].offset == (guint32)-1)
202 g_warning ("%s not initialized correctly (missing field layout info for %s)", class->name, class->fields [i].name);
204 * The offset is from the start of the object: this works for both
205 * classes and valuetypes.
207 class->fields [i].offset += sizeof (MonoObject);
211 size += class->fields [i].offset;
212 class->instance_size = MAX (class->instance_size, size);
217 class->size_inited = 1;
220 * Compute static field layout and size
223 case TYPE_ATTRIBUTE_AUTO_LAYOUT:
224 case TYPE_ATTRIBUTE_SEQUENTIAL_LAYOUT:
225 for (i = 0; i < top; i++){
228 if (!(class->fields [i].type->attrs & FIELD_ATTRIBUTE_STATIC))
231 size = mono_type_size (class->fields [i].type, &align);
232 class->fields [i].offset = class->class_size;
233 class->fields [i].offset += align - 1;
234 class->fields [i].offset &= ~(align - 1);
235 class->class_size = class->fields [i].offset + size;
238 case TYPE_ATTRIBUTE_EXPLICIT_LAYOUT:
239 for (i = 0; i < top; i++){
243 * There must be info about all the fields in a type if it
244 * uses explicit layout.
248 if (!(class->fields [i].type->attrs & FIELD_ATTRIBUTE_STATIC))
251 size = mono_type_size (class->fields [i].type, &align);
252 class->fields [i].offset = class->class_size;
253 class->fields [i].offset += align - 1;
254 class->fields [i].offset &= ~(align - 1);
255 class->class_size = class->fields [i].offset + size;
262 init_properties (MonoClass *class)
264 guint startm, endm, i, j;
265 guint32 cols [MONO_PROPERTY_SIZE];
266 MonoTableInfo *pt = &class->image->tables [MONO_TABLE_PROPERTY];
267 MonoTableInfo *msemt = &class->image->tables [MONO_TABLE_METHODSEMANTICS];
269 class->property.first = mono_metadata_properties_from_typedef (class->image, mono_metadata_token_index (class->type_token) - 1, &class->property.last);
270 class->property.count = class->property.last - class->property.first;
272 class->properties = g_new0 (MonoProperty, class->property.count);
273 for (i = class->property.first; i < class->property.last; ++i) {
274 mono_metadata_decode_row (pt, i, cols, MONO_PROPERTY_SIZE);
275 class->properties [i - class->property.first].attrs = cols [MONO_PROPERTY_FLAGS];
276 class->properties [i - class->property.first].name = mono_metadata_string_heap (class->image, cols [MONO_PROPERTY_NAME]);
278 startm = mono_metadata_methods_from_property (class->image, i, &endm);
279 for (j = startm; j < endm; ++j) {
280 mono_metadata_decode_row (msemt, j, cols, MONO_METHOD_SEMA_SIZE);
281 switch (cols [MONO_METHOD_SEMA_SEMANTICS]) {
282 case METHOD_SEMANTIC_SETTER:
283 class->properties [i - class->property.first].set = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
285 case METHOD_SEMANTIC_GETTER:
286 class->properties [i - class->property.first].get = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
296 init_events (MonoClass *class)
298 guint startm, endm, i, j;
299 guint32 cols [MONO_EVENT_SIZE];
300 MonoTableInfo *pt = &class->image->tables [MONO_TABLE_EVENT];
301 MonoTableInfo *msemt = &class->image->tables [MONO_TABLE_METHODSEMANTICS];
303 class->event.first = mono_metadata_events_from_typedef (class->image, mono_metadata_token_index (class->type_token) - 1, &class->event.last);
304 class->event.count = class->event.last - class->event.first;
306 class->events = g_new0 (MonoEvent, class->event.count);
307 for (i = class->event.first; i < class->event.last; ++i) {
308 mono_metadata_decode_row (pt, i, cols, MONO_EVENT_SIZE);
309 class->events [i - class->event.first].attrs = cols [MONO_EVENT_FLAGS];
310 class->events [i - class->event.first].name = mono_metadata_string_heap (class->image, cols [MONO_EVENT_NAME]);
312 startm = mono_metadata_methods_from_event (class->image, i, &endm);
313 for (j = startm; j < endm; ++j) {
314 mono_metadata_decode_row (msemt, j, cols, MONO_METHOD_SEMA_SIZE);
315 switch (cols [MONO_METHOD_SEMA_SEMANTICS]) {
316 case METHOD_SEMANTIC_ADD_ON:
317 class->events [i - class->event.first].add = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
319 case METHOD_SEMANTIC_REMOVE_ON:
320 class->events [i - class->event.first].remove = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
322 case METHOD_SEMANTIC_FIRE:
323 class->events [i - class->event.first].raise = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
325 case METHOD_SEMANTIC_OTHER: /* don't care for now */
334 mono_get_unique_iid (MonoClass *class)
336 static GHashTable *iid_hash = NULL;
337 static guint iid = 0;
342 g_assert (class->flags & TYPE_ATTRIBUTE_INTERFACE);
345 iid_hash = g_hash_table_new (g_str_hash, g_str_equal);
347 str = g_strdup_printf ("%s|%s.%s\n", class->image->name, class->name_space, class->name);
349 if (g_hash_table_lookup_extended (iid_hash, str, NULL, &value)) {
353 g_hash_table_insert (iid_hash, str, (gpointer)iid);
362 * @class: the class to initialize
364 * compute the instance_size, class_size and other infos that cannot be
365 * computed at mono_class_get() time. Also compute a generic vtable and
366 * the method slot numbers. We use this infos later to create a domain
370 mono_class_init (MonoClass *class)
374 int i, max_vtsize = 0, max_iid, cur_slot = 0;
375 static MonoMethod *default_ghc = NULL;
376 static MonoMethod *default_finalize = NULL;
377 static int finalize_slot = -1;
378 static int ghc_slot = -1;
379 guint32 packing_size = 0;
386 if (class->init_pending) {
387 /* this indicates a cyclic dependency */
388 g_error ("pending init %s.%s\n", class->name_space, class->name);
391 class->init_pending = 1;
393 mono_stats.initialized_class_count++;
396 if (!class->parent->inited)
397 mono_class_init (class->parent);
398 class->instance_size += class->parent->instance_size;
399 class->class_size += class->parent->class_size;
400 class->min_align = class->parent->min_align;
401 cur_slot = class->parent->vtable_size;
403 class->min_align = 1;
405 if (mono_metadata_packing_from_typedef (class->image, class->type_token, &packing_size, &class->instance_size))
406 class->instance_size += sizeof (MonoObject);
407 /* use packing_size in field layout */
410 * Computes the size used by the fields, and their locations
412 if (!class->size_inited && class->field.count > 0){
413 class->fields = g_new0 (MonoClassField, class->field.count);
414 class_compute_field_layout (class, packing_size);
417 if (!(class->flags & TYPE_ATTRIBUTE_INTERFACE)) {
418 for (i = 0; i < class->interface_count; i++)
419 max_vtsize += class->interfaces [i]->method.count;
422 max_vtsize += class->parent->vtable_size;
424 max_vtsize += class->method.count;
427 vtable = alloca (sizeof (gpointer) * max_vtsize);
428 memset (vtable, 0, sizeof (gpointer) * max_vtsize);
430 /* initialize method pointers */
431 class->methods = g_new (MonoMethod*, class->method.count);
432 for (i = 0; i < class->method.count; ++i)
433 class->methods [i] = mono_get_method (class->image,
434 MONO_TOKEN_METHOD_DEF | (i + class->method.first + 1), class);
436 init_properties (class);
439 i = mono_metadata_nesting_typedef (class->image, class->type_token);
442 guint32 cols [MONO_NESTED_CLASS_SIZE];
443 mono_metadata_decode_row (&class->image->tables [MONO_TABLE_NESTEDCLASS], i - 1, cols, MONO_NESTED_CLASS_SIZE);
444 if (cols [MONO_NESTED_CLASS_ENCLOSING] != mono_metadata_token_index (class->type_token))
446 nclass = mono_class_create_from_typedef (class->image, MONO_TOKEN_TYPE_DEF | cols [MONO_NESTED_CLASS_NESTED]);
447 class->nested_classes = g_list_prepend (class->nested_classes, nclass);
451 if (class->flags & TYPE_ATTRIBUTE_INTERFACE) {
452 for (i = 0; i < class->method.count; ++i)
453 class->methods [i]->slot = i;
454 class->init_pending = 0;
459 /* printf ("METAINIT %s.%s\n", class->name_space, class->name); */
461 /* compute maximum number of slots and maximum interface id */
463 for (k = class; k ; k = k->parent) {
464 for (i = 0; i < k->interface_count; i++) {
465 ic = k->interfaces [i];
468 mono_class_init (ic);
470 if (max_iid < ic->interface_id)
471 max_iid = ic->interface_id;
475 class->max_interface_id = max_iid;
476 /* compute vtable offset for interfaces */
477 class->interface_offsets = g_malloc (sizeof (gpointer) * (max_iid + 1));
479 for (i = 0; i <= max_iid; i++)
480 class->interface_offsets [i] = -1;
482 for (i = 0; i < class->interface_count; i++) {
483 ic = class->interfaces [i];
484 class->interface_offsets [ic->interface_id] = cur_slot;
485 cur_slot += ic->method.count;
488 for (k = class->parent; k ; k = k->parent) {
489 for (i = 0; i < k->interface_count; i++) {
490 ic = k->interfaces [i];
491 if (class->interface_offsets [ic->interface_id] == -1) {
492 int io = k->interface_offsets [ic->interface_id];
495 g_assert (io <= max_vtsize);
497 class->interface_offsets [ic->interface_id] = io;
502 if (class->parent && class->parent->vtable_size)
503 memcpy (vtable, class->parent->vtable, sizeof (gpointer) * class->parent->vtable_size);
505 for (k = class; k ; k = k->parent) {
506 for (i = 0; i < k->interface_count; i++) {
509 ic = k->interfaces [i];
510 io = k->interface_offsets [ic->interface_id];
513 g_assert (io <= max_vtsize);
516 for (l = 0; l < ic->method.count; l++) {
517 MonoMethod *im = ic->methods [l];
518 for (j = 0; j < class->method.count; ++j) {
519 MonoMethod *cm = class->methods [j];
520 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
521 !(cm->flags & METHOD_ATTRIBUTE_PUBLIC) ||
522 !(cm->flags & METHOD_ATTRIBUTE_NEW_SLOT))
524 if (!strcmp(cm->name, im->name) &&
525 mono_metadata_signature_equal (cm->signature, im->signature)) {
526 g_assert (io + l <= max_vtsize);
527 vtable [io + l] = cm;
532 /* already implemented */
533 if (io >= k->vtable_size)
537 for (l = 0; l < ic->method.count; l++) {
538 MonoMethod *im = ic->methods [l];
541 g_assert (io + l <= max_vtsize);
546 for (k1 = class; k1; k1 = k1->parent) {
547 for (j = 0; j < k1->method.count; ++j) {
548 MonoMethod *cm = k1->methods [j];
550 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
551 !(cm->flags & METHOD_ATTRIBUTE_PUBLIC))
554 if (!strcmp(cm->name, im->name) &&
555 mono_metadata_signature_equal (cm->signature, im->signature)) {
556 g_assert (io + l <= max_vtsize);
557 vtable [io + l] = cm;
562 g_assert (io + l <= max_vtsize);
568 for (l = 0; l < ic->method.count; l++) {
569 MonoMethod *im = ic->methods [l];
570 char *qname, *fqname;
572 qname = g_strconcat (ic->name, ".", im->name, NULL);
573 if (ic->name_space && ic->name_space [0])
574 fqname = g_strconcat (ic->name_space, ".", ic->name, ".", im->name, NULL);
578 for (j = 0; j < class->method.count; ++j) {
579 MonoMethod *cm = class->methods [j];
581 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL))
584 if (((fqname && !strcmp (cm->name, fqname)) || !strcmp (cm->name, qname)) &&
585 mono_metadata_signature_equal (cm->signature, im->signature)) {
586 g_assert (io + l <= max_vtsize);
587 vtable [io + l] = cm;
596 if (!(class->flags & TYPE_ATTRIBUTE_ABSTRACT)) {
597 for (l = 0; l < ic->method.count; l++) {
599 MonoMethod *im = ic->methods [l];
600 g_assert (io + l <= max_vtsize);
601 if (!(vtable [io + l])) {
602 msig = mono_signature_get_desc (im->signature, FALSE);
603 printf ("no implementation for interface method %s.%s::%s(%s) in class %s.%s\n",
604 ic->name_space, ic->name, im->name, msig, class->name_space, class->name);
606 for (j = 0; j < class->method.count; ++j) {
607 MonoMethod *cm = class->methods [j];
608 msig = mono_signature_get_desc (cm->signature, FALSE);
610 printf ("METHOD %s(%s)\n", cm->name, msig);
613 g_assert_not_reached ();
618 for (l = 0; l < ic->method.count; l++) {
619 MonoMethod *im = vtable [io + l];
622 g_assert (io + l <= max_vtsize);
624 /* FIXME: why do we need this ? */
626 /* g_assert_not_reached (); */
633 for (i = 0; i < class->method.count; ++i) {
636 cm = class->methods [i];
640 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
643 #else /* EXT_VTABLE_HACK */
648 if (!(cm->flags & METHOD_ATTRIBUTE_NEW_SLOT) && (cm->flags & METHOD_ATTRIBUTE_VIRTUAL)) {
649 for (k = class->parent; k ; k = k->parent) {
651 for (j = 0; j < k->method.count; ++j) {
652 MonoMethod *m1 = k->methods [j];
653 if (!(m1->flags & METHOD_ATTRIBUTE_VIRTUAL))
655 if (!strcmp(cm->name, m1->name) &&
656 mono_metadata_signature_equal (cm->signature, m1->signature)) {
657 cm->slot = k->methods [j]->slot;
658 g_assert (cm->slot < max_vtsize);
668 cm->slot = cur_slot++;
670 if (!(cm->flags & METHOD_ATTRIBUTE_ABSTRACT))
671 vtable [cm->slot] = cm;
675 class->vtable_size = cur_slot;
676 class->vtable = g_malloc0 (sizeof (gpointer) * class->vtable_size);
677 memcpy (class->vtable, vtable, sizeof (gpointer) * class->vtable_size);
680 class->init_pending = 0;
682 if (mono_print_vtable) {
685 for (i = 0; i <= max_iid; i++)
686 if (class->interface_offsets [i] != -1)
689 printf ("VTable %s.%s (size = %d, interfaces = %d)\n", class->name_space,
690 class->name, class->vtable_size, icount);
692 for (i = 0; i < class->vtable_size; ++i) {
697 printf (" slot %03d(%03d) %s.%s:%s\n", i, cm->slot,
698 cm->klass->name_space, cm->klass->name,
705 printf ("Interfaces %s.%s (max_iid = %d)\n", class->name_space,
706 class->name, max_iid);
708 for (i = 0; i < class->interface_count; i++) {
709 ic = class->interfaces [i];
710 printf (" slot %03d(%03d) %s.%s\n",
711 class->interface_offsets [ic->interface_id],
712 ic->method.count, ic->name_space, ic->name);
715 for (k = class->parent; k ; k = k->parent) {
716 for (i = 0; i < k->interface_count; i++) {
717 ic = k->interfaces [i];
718 printf (" slot %03d(%03d) %s.%s\n",
719 class->interface_offsets [ic->interface_id],
720 ic->method.count, ic->name_space, ic->name);
727 if (class == mono_defaults.object_class) {
729 for (i = 0; i < class->vtable_size; ++i) {
730 MonoMethod *cm = vtable [i];
732 if (!strcmp (cm->name, "GetHashCode")) {
738 g_assert (ghc_slot > 0);
740 default_ghc = vtable [ghc_slot];
745 if (class != mono_defaults.object_class) {
747 if (vtable [ghc_slot] == default_ghc) {
752 if (!default_finalize) {
753 if (class == mono_defaults.object_class) {
755 for (i = 0; i < class->vtable_size; ++i) {
756 MonoMethod *cm = vtable [i];
758 if (!strcmp (cm->name, "Finalize")) {
764 g_assert (finalize_slot > 0);
766 default_finalize = vtable [finalize_slot];
770 /* Object::Finalize should have empty implemenatation */
771 class->has_finalize = 0;
772 if (class != mono_defaults.object_class) {
773 if (vtable [finalize_slot] != default_finalize)
774 class->has_finalize = 1;
780 * Compute a relative numbering of the class hierarchy as described in
781 * "Java for Large-Scale Scientific Computations?"
784 mono_compute_relative_numbering (MonoClass *class, int *c)
792 for (s = class->subclasses; s; s = s->next)
793 mono_compute_relative_numbering ((MonoClass *)s->data, c);
795 class->diffval = *c - class->baseval;
799 mono_class_setup_mono_type (MonoClass *class)
801 const char *name = class->name;
802 const char *nspace = class->name_space;
804 class->this_arg.byref = 1;
805 class->this_arg.data.klass = class;
806 class->this_arg.type = MONO_TYPE_CLASS;
807 class->byval_arg.data.klass = class;
808 class->byval_arg.type = MONO_TYPE_CLASS;
810 if (!strcmp (nspace, "System")) {
811 if (!strcmp (name, "ValueType")) {
813 * do not set the valuetype bit for System.ValueType.
814 * class->valuetype = 1;
816 } else if (!strcmp (name, "Enum")) {
818 * do not set the valuetype bit for System.Enum.
819 * class->valuetype = 1;
821 class->valuetype = 0;
823 } else if (!strcmp (name, "Object")) {
824 class->this_arg.type = class->byval_arg.type = MONO_TYPE_OBJECT;
825 } else if (!strcmp (name, "String")) {
826 class->this_arg.type = class->byval_arg.type = MONO_TYPE_STRING;
830 if (class->valuetype) {
831 int t = MONO_TYPE_VALUETYPE;
832 if (!strcmp (nspace, "System")) {
835 if (!strcmp (name, "Boolean")) {
836 t = MONO_TYPE_BOOLEAN;
837 } else if (!strcmp(name, "Byte")) {
842 if (!strcmp (name, "Char")) {
847 if (!strcmp (name, "Double")) {
852 if (!strcmp (name, "Int32")) {
854 } else if (!strcmp(name, "Int16")) {
856 } else if (!strcmp(name, "Int64")) {
858 } else if (!strcmp(name, "IntPtr")) {
863 if (!strcmp (name, "Single")) {
865 } else if (!strcmp(name, "SByte")) {
870 if (!strcmp (name, "UInt32")) {
872 } else if (!strcmp(name, "UInt16")) {
874 } else if (!strcmp(name, "UInt64")) {
876 } else if (!strcmp(name, "UIntPtr")) {
881 if (!strcmp (name, "Void")) {
889 class->this_arg.type = class->byval_arg.type = t;
894 mono_class_setup_parent (MonoClass *class, MonoClass *parent)
896 gboolean system_namespace;
898 system_namespace = !strcmp (class->name_space, "System");
900 /* if root of the hierarchy */
901 if (system_namespace && !strcmp (class->name, "Object")) {
902 class->parent = NULL;
903 class->instance_size = sizeof (MonoObject);
906 if (!strcmp (class->name, "<Module>")) {
907 class->parent = NULL;
908 class->instance_size = 0;
912 if (!(class->flags & TYPE_ATTRIBUTE_INTERFACE)) {
914 class->parent = parent;
917 g_assert_not_reached (); /* FIXME */
919 class->marshalbyref = parent->marshalbyref;
920 class->contextbound = parent->contextbound;
921 class->delegate = parent->delegate;
923 if (system_namespace) {
924 if (*class->name == 'M' && !strcmp (class->name, "MarshalByRefObject"))
925 class->marshalbyref = 1;
927 if (*class->name == 'C' && !strcmp (class->name, "ContextBoundObject"))
928 class->contextbound = 1;
930 if (*class->name == 'D' && !strcmp (class->name, "Delegate"))
934 if (class->parent->enumtype || ((strcmp (class->parent->name, "ValueType") == 0) &&
935 (strcmp (class->parent->name_space, "System") == 0)))
936 class->valuetype = 1;
937 if (((strcmp (class->parent->name, "Enum") == 0) && (strcmp (class->parent->name_space, "System") == 0))) {
938 class->valuetype = class->enumtype = 1;
940 /*class->enumtype = class->parent->enumtype; */
941 class->parent->subclasses = g_list_prepend (class->parent->subclasses, class);
942 mono_compute_relative_numbering (mono_defaults.object_class, &rnum);
944 class->parent = NULL;
950 * @image: context where the image is created
951 * @type_token: typedef token
954 mono_class_create_from_typedef (MonoImage *image, guint32 type_token)
956 MonoTableInfo *tt = &image->tables [MONO_TABLE_TYPEDEF];
957 MonoClass *class, *parent = NULL;
958 guint32 cols [MONO_TYPEDEF_SIZE];
959 guint32 cols_next [MONO_TYPEDEF_SIZE];
960 guint tidx = mono_metadata_token_index (type_token);
961 const char *name, *nspace;
963 MonoClass **interfaces;
965 if ((class = g_hash_table_lookup (image->class_cache, GUINT_TO_POINTER (type_token))))
968 g_assert (mono_metadata_token_table (type_token) == MONO_TABLE_TYPEDEF);
970 mono_metadata_decode_row (tt, tidx - 1, cols, MONO_TYPEDEF_SIZE);
972 name = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
973 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
975 if (cols [MONO_TYPEDEF_EXTENDS])
976 parent = mono_class_get (image, mono_metadata_token_from_dor (cols [MONO_TYPEDEF_EXTENDS]));
977 interfaces = mono_metadata_interfaces_from_typedef (image, type_token, &icount);
979 class = g_malloc0 (sizeof (MonoClass));
981 g_hash_table_insert (image->class_cache, GUINT_TO_POINTER (type_token), class);
983 class->interfaces = interfaces;
984 class->interface_count = icount;
987 class->name_space = nspace;
989 class->image = image;
990 class->type_token = type_token;
991 class->flags = cols [MONO_TYPEDEF_FLAGS];
993 class->element_class = class;
995 /*g_print ("Init class %s\n", name);*/
997 mono_class_setup_parent (class, parent);
999 mono_class_setup_mono_type (class);
1002 * Compute the field and method lists
1004 class->field.first = cols [MONO_TYPEDEF_FIELD_LIST] - 1;
1005 class->method.first = cols [MONO_TYPEDEF_METHOD_LIST] - 1;
1007 if (tt->rows > tidx){
1008 mono_metadata_decode_row (tt, tidx, cols_next, CSIZE (cols_next));
1009 class->field.last = cols_next [MONO_TYPEDEF_FIELD_LIST] - 1;
1010 class->method.last = cols_next [MONO_TYPEDEF_METHOD_LIST] - 1;
1012 class->field.last = image->tables [MONO_TABLE_FIELD].rows;
1013 class->method.last = image->tables [MONO_TABLE_METHOD].rows;
1016 if (cols [MONO_TYPEDEF_FIELD_LIST] &&
1017 cols [MONO_TYPEDEF_FIELD_LIST] <= image->tables [MONO_TABLE_FIELD].rows)
1018 class->field.count = class->field.last - class->field.first;
1020 class->field.count = 0;
1022 if (cols [MONO_TYPEDEF_METHOD_LIST] <= image->tables [MONO_TABLE_METHOD].rows)
1023 class->method.count = class->method.last - class->method.first;
1025 class->method.count = 0;
1027 /* reserve space to store vector pointer in arrays */
1028 if (!strcmp (nspace, "System") && !strcmp (name, "Array")) {
1029 class->instance_size += 2 * sizeof (gpointer);
1030 g_assert (class->field.count == 0);
1033 if (class->flags & TYPE_ATTRIBUTE_INTERFACE)
1034 class->interface_id = mono_get_unique_iid (class);
1036 /*class->interfaces = mono_metadata_interfaces_from_typedef (image, type_token, &class->interface_count); */
1038 if (class->enumtype) {
1039 class->fields = g_new0 (MonoClassField, class->field.count);
1040 class_compute_field_layout (class, 0);
1043 if ((type_token = mono_metadata_nested_in_typedef (image, type_token)))
1044 class->nested_in = mono_class_create_from_typedef (image, type_token);
1049 mono_ptr_class_get (MonoType *type)
1052 MonoClass *el_class;
1053 static GHashTable *ptr_hash = NULL;
1056 ptr_hash = g_hash_table_new (g_direct_hash, g_direct_equal);
1057 el_class = mono_class_from_mono_type (type);
1058 if ((result = g_hash_table_lookup (ptr_hash, el_class)))
1060 result = g_new0 (MonoClass, 1);
1062 result->parent = NULL; /* no parent for PTR types */
1063 result->name = "System";
1064 result->name_space = "MonoPtrFakeClass";
1065 result->image = el_class->image;
1066 result->inited = TRUE;
1067 /* Can pointers get boxed? */
1068 result->instance_size = sizeof (gpointer);
1070 * baseval, diffval: need them to allow casting ?
1072 result->element_class = el_class;
1073 result->enum_basetype = &result->element_class->byval_arg;
1075 result->this_arg.type = result->byval_arg.type = MONO_TYPE_PTR;
1076 result->this_arg.data.type = result->byval_arg.data.type = result->enum_basetype;
1077 result->this_arg.byref = TRUE;
1079 g_hash_table_insert (ptr_hash, el_class, result);
1085 mono_class_from_mono_type (MonoType *type)
1087 switch (type->type) {
1088 case MONO_TYPE_OBJECT:
1089 return mono_defaults.object_class;
1090 case MONO_TYPE_VOID:
1091 return mono_defaults.void_class;
1092 case MONO_TYPE_BOOLEAN:
1093 return mono_defaults.boolean_class;
1094 case MONO_TYPE_CHAR:
1095 return mono_defaults.char_class;
1097 return mono_defaults.sbyte_class;
1099 return mono_defaults.byte_class;
1101 return mono_defaults.int16_class;
1103 return mono_defaults.uint16_class;
1105 return mono_defaults.int32_class;
1107 return mono_defaults.uint32_class;
1109 return mono_defaults.int_class;
1111 return mono_defaults.uint_class;
1113 return mono_defaults.int64_class;
1115 return mono_defaults.uint64_class;
1117 return mono_defaults.single_class;
1119 return mono_defaults.double_class;
1120 case MONO_TYPE_STRING:
1121 return mono_defaults.string_class;
1122 case MONO_TYPE_ARRAY:
1123 return mono_array_class_get (type->data.array->type, type->data.array->rank);
1125 return mono_ptr_class_get (type->data.type);
1126 case MONO_TYPE_SZARRAY:
1127 return mono_array_class_get (type->data.type, 1);
1128 case MONO_TYPE_CLASS:
1129 case MONO_TYPE_VALUETYPE:
1130 return type->data.klass;
1132 g_warning ("implement me %02x\n", type->type);
1133 g_assert_not_reached ();
1140 * @image: context where the image is created
1141 * @type_spec: typespec token
1144 mono_class_create_from_typespec (MonoImage *image, guint32 type_spec)
1149 type = mono_type_create_from_typespec (image, type_spec);
1151 switch (type->type) {
1152 case MONO_TYPE_ARRAY:
1153 class = mono_array_class_get (type->data.array->type, type->data.array->rank);
1155 case MONO_TYPE_SZARRAY:
1156 class = mono_array_class_get (type->data.type, 1);
1159 class = mono_class_from_mono_type (type->data.type);
1162 /* it seems any type can be stored in TypeSpec as well */
1163 class = mono_class_from_mono_type (type);
1167 mono_metadata_free_type (type);
1173 * mono_array_class_get:
1174 * @element_type: element type
1175 * @rank: the dimension of the array class
1177 * Returns: a class object describing the array with element type @element_type and
1181 mono_array_class_get (MonoType *element_type, guint32 rank)
1186 MonoClass *parent = NULL;
1190 eclass = mono_class_from_mono_type (element_type);
1191 g_assert (rank <= 255);
1193 parent = mono_defaults.array_class;
1195 if (!parent->inited)
1196 mono_class_init (parent);
1198 image = eclass->image;
1200 if ((list = g_hash_table_lookup (image->array_cache, element_type))) {
1201 for (; list; list = list->next) {
1203 if (class->rank == rank)
1208 class = g_malloc0 (sizeof (MonoClass) + parent->vtable_size * sizeof (gpointer));
1210 class->image = image;
1211 class->name_space = "System";
1212 class->name = "Array";
1213 class->type_token = 0;
1214 class->flags = TYPE_ATTRIBUTE_CLASS;
1215 class->parent = parent;
1216 class->instance_size = mono_class_instance_size (class->parent);
1217 class->class_size = 0;
1218 class->vtable_size = parent->vtable_size;
1219 class->parent->subclasses = g_list_prepend (class->parent->subclasses, class);
1220 mono_compute_relative_numbering (mono_defaults.object_class, &rnum);
1223 class->element_class = eclass;
1225 MonoArrayType *at = g_new0 (MonoArrayType, 1);
1226 class->byval_arg.type = MONO_TYPE_ARRAY;
1227 class->byval_arg.data.array = at;
1228 at->type = &eclass->byval_arg;
1230 /* FIXME: complete.... */
1232 /* FIXME: this is not correct. the lbound could be >0 */
1233 class->byval_arg.type = MONO_TYPE_SZARRAY;
1234 class->byval_arg.data.type = &eclass->byval_arg;
1236 class->this_arg = class->byval_arg;
1237 class->this_arg.byref = 1;
1239 list = g_slist_append (list, class);
1240 g_hash_table_insert (image->array_cache, &class->element_class->byval_arg, list);
1245 * mono_class_instance_size:
1248 * Returns: the size of an object instance
1251 mono_class_instance_size (MonoClass *klass)
1254 if (!klass->size_inited)
1255 mono_class_init (klass);
1257 return klass->instance_size;
1261 * mono_class_value_size:
1264 * This function is used for value types, and return the
1265 * space and the alignment to store that kind of value object.
1267 * Returns: the size of a value of kind @klass
1270 mono_class_value_size (MonoClass *klass, guint32 *align)
1274 /* fixme: check disable, because we still have external revereces to
1275 * mscorlib and Dummy Objects
1277 /*g_assert (klass->valuetype);*/
1279 size = mono_class_instance_size (klass) - sizeof (MonoObject);
1282 *align = klass->min_align;
1288 * mono_class_data_size:
1291 * Returns: the size of the static class data
1294 mono_class_data_size (MonoClass *klass)
1298 mono_class_init (klass);
1300 return klass->class_size;
1304 * Auxiliary routine to mono_class_get_field
1306 * Takes a field index instead of a field token.
1308 static MonoClassField *
1309 mono_class_get_field_idx (MonoClass *class, int idx)
1311 if (class->field.count){
1312 if ((idx >= class->field.first) && (idx < class->field.last)){
1313 return &class->fields [idx - class->field.first];
1320 return mono_class_get_field_idx (class->parent, idx);
1324 * mono_class_get_field:
1325 * @class: the class to lookup the field.
1326 * @field_token: the field token
1328 * Returns: A MonoClassField representing the type and offset of
1329 * the field, or a NULL value if the field does not belong to this
1333 mono_class_get_field (MonoClass *class, guint32 field_token)
1335 int idx = mono_metadata_token_index (field_token);
1337 g_assert (mono_metadata_token_code (field_token) == MONO_TOKEN_FIELD_DEF);
1339 return mono_class_get_field_idx (class, idx - 1);
1343 mono_class_get_field_from_name (MonoClass *klass, const char *name)
1347 for (i = 0; i < klass->field.count; ++i) {
1348 if (strcmp (name, klass->fields [i].name) == 0)
1349 return &klass->fields [i];
1356 * @image: the image where the class resides
1357 * @type_token: the token for the class
1358 * @at: an optional pointer to return the array element type
1360 * Returns: the MonoClass that represents @type_token in @image
1363 mono_class_get (MonoImage *image, guint32 type_token)
1367 switch (type_token & 0xff000000){
1368 case MONO_TOKEN_TYPE_DEF:
1369 class = mono_class_create_from_typedef (image, type_token);
1371 case MONO_TOKEN_TYPE_REF:
1372 class = mono_class_from_typeref (image, type_token);
1374 case MONO_TOKEN_TYPE_SPEC:
1375 class = mono_class_create_from_typespec (image, type_token);
1378 g_warning ("unknown token type %x", type_token & 0xff000000);
1379 g_assert_not_reached ();
1383 g_warning ("Could not load class from token 0x%08x in %s", type_token, image->name);
1388 mono_class_from_name_case (MonoImage *image, const char* name_space, const char *name)
1390 MonoTableInfo *t = &image->tables [MONO_TABLE_TYPEDEF];
1391 guint32 cols [MONO_TYPEDEF_SIZE];
1396 /* add a cache if needed */
1397 for (i = 1; i <= t->rows; ++i) {
1398 mono_metadata_decode_row (t, i - 1, cols, MONO_TYPEDEF_SIZE);
1399 /* nested types are accessed from the nesting name */
1400 visib = cols [MONO_TYPEDEF_FLAGS] & TYPE_ATTRIBUTE_VISIBILITY_MASK;
1401 if (visib > TYPE_ATTRIBUTE_PUBLIC && visib < TYPE_ATTRIBUTE_NESTED_ASSEMBLY)
1403 n = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
1404 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
1405 if (g_strcasecmp (n, name) == 0 && g_strcasecmp (nspace, name_space) == 0)
1406 return mono_class_get (image, MONO_TOKEN_TYPE_DEF | i);
1412 mono_class_from_name (MonoImage *image, const char* name_space, const char *name)
1414 GHashTable *nspace_table;
1417 nspace_table = g_hash_table_lookup (image->name_cache, name_space);
1420 token = GPOINTER_TO_UINT (g_hash_table_lookup (nspace_table, name));
1423 /*g_warning ("token not found for %s.%s in image %s", name_space, name, image->name);*/
1427 token = MONO_TOKEN_TYPE_DEF | token;
1429 return mono_class_get (image, token);
1433 * mono_array_element_size:
1434 * @ac: pointer to a #MonoArrayClass
1436 * Returns: the size of single array element.
1439 mono_array_element_size (MonoClass *ac)
1441 if (ac->element_class->valuetype)
1442 return mono_class_instance_size (ac->element_class) - sizeof (MonoObject);
1444 return sizeof (gpointer);
1448 mono_ldtoken (MonoImage *image, guint32 token, MonoClass **handle_class)
1450 switch (token & 0xff000000) {
1451 case MONO_TOKEN_TYPE_DEF:
1452 case MONO_TOKEN_TYPE_REF: {
1455 *handle_class = mono_defaults.typehandle_class;
1456 class = mono_class_get (image, token);
1457 mono_class_init (class);
1458 /* We return a MonoType* as handle */
1459 return &class->byval_arg;
1461 case MONO_TOKEN_TYPE_SPEC: {
1464 *handle_class = mono_defaults.typehandle_class;
1465 class = mono_class_create_from_typespec (image, token);
1466 mono_class_init (class);
1467 return &class->byval_arg;
1469 case MONO_TOKEN_FIELD_DEF: {
1471 guint32 type = mono_metadata_typedef_from_field (image, mono_metadata_token_index (token));
1472 class = mono_class_get (image, MONO_TOKEN_TYPE_DEF | type);
1473 mono_class_init (class);
1475 *handle_class = mono_class_from_name (mono_defaults.corlib, "System", "RuntimeFieldHandle");
1476 return mono_class_get_field (class, token);
1478 case MONO_TOKEN_METHOD_DEF:
1479 case MONO_TOKEN_MEMBER_REF:
1481 g_warning ("Unknown token 0x%08x in ldtoken", token);