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>
35 #define CSIZE(x) (sizeof (x) / 4)
37 gboolean mono_print_vtable = FALSE;
39 static MonoClass * mono_class_create_from_typedef (MonoImage *image, guint32 type_token);
42 mono_class_create_from_typeref (MonoImage *image, guint32 type_token)
44 guint32 cols [MONO_TYPEREF_SIZE];
45 MonoTableInfo *t = &image->tables [MONO_TABLE_TYPEREF];
47 const char *name, *nspace;
50 mono_metadata_decode_row (t, (type_token&0xffffff)-1, cols, MONO_TYPEREF_SIZE);
51 idx = cols [MONO_TYPEREF_SCOPE] >> RESOLTION_SCOPE_BITS;
52 switch (cols [MONO_TYPEREF_SCOPE] & RESOLTION_SCOPE_MASK) {
53 case RESOLTION_SCOPE_MODULE:
55 g_error ("null ResolutionScope not yet handled");
56 /* a typedef in disguise */
57 return mono_class_create_from_typedef (image, MONO_TOKEN_TYPE_DEF | (1+idx));
58 case RESOLTION_SCOPE_MODULEREF:
59 g_error ("ModuleRef ResolutionScope not yet handled");
60 case RESOLTION_SCOPE_TYPEREF:
61 g_error ("TypeRef ResolutionScope not yet handled");
62 case RESOLTION_SCOPE_ASSEMBLYREF:
66 if (!image->references || !image->references [idx-1]) {
68 * detected a reference to mscorlib, we simply return a reference to a dummy
69 * until we have a better solution.
71 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]));
73 res = mono_class_from_name (image, "System", "MonoDummy");
74 /* prevent method loading */
76 /* some storage if the type is used - very ugly hack */
77 res->instance_size = 2*sizeof (gpointer);
81 name = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAME]);
82 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAMESPACE]);
84 /* load referenced assembly */
85 image = image->references [idx-1]->image;
87 return mono_class_from_name (image, nspace, name);
91 * class_compute_field_layout:
92 * @m: pointer to the metadata.
93 * @class: The class to initialize
95 * Initializes the class->fields.
97 * Currently we only support AUTO_LAYOUT, and do not even try to do
98 * a good job at it. This is temporary to get the code for Paolo.
101 class_compute_field_layout (MonoClass *class)
103 MonoImage *m = class->image;
104 const int top = class->field.count;
105 guint32 layout = class->flags & TYPE_ATTRIBUTE_LAYOUT_MASK;
106 MonoTableInfo *t = &m->tables [MONO_TABLE_FIELD];
110 * Fetch all the field information.
112 for (i = 0; i < top; i++){
114 guint32 cols [MONO_FIELD_SIZE];
115 int idx = class->field.first + i;
117 mono_metadata_decode_row (t, idx, cols, CSIZE (cols));
118 /* The name is needed for fieldrefs */
119 class->fields [i].name = mono_metadata_string_heap (m, cols [MONO_FIELD_NAME]);
120 sig = mono_metadata_blob_heap (m, cols [MONO_FIELD_SIGNATURE]);
121 mono_metadata_decode_value (sig, &sig);
122 /* FIELD signature == 0x06 */
123 g_assert (*sig == 0x06);
124 class->fields [i].type = mono_metadata_parse_field_type (
125 m, cols [MONO_FIELD_FLAGS], sig + 1, &sig);
126 if (cols [MONO_FIELD_FLAGS] & FIELD_ATTRIBUTE_HAS_FIELD_RVA) {
127 mono_metadata_field_info (m, idx, NULL, &class->fields [i].data, NULL);
128 if (!class->fields [i].data)
129 g_warning ("field %s in %s should have RVA data, but hasn't", class->fields [i].name, class->name);
131 if (class->enumtype && !(cols [MONO_FIELD_FLAGS] & FIELD_ATTRIBUTE_STATIC)) {
132 class->enum_basetype = class->fields [i].type;
133 class->element_class = mono_class_from_mono_type (class->enum_basetype);
136 if (class->enumtype && !class->enum_basetype) {
137 if (!((strcmp (class->name, "Enum") == 0) && (strcmp (class->name_space, "System") == 0)))
141 * Compute field layout and total size (not considering static fields)
144 case TYPE_ATTRIBUTE_AUTO_LAYOUT:
145 case TYPE_ATTRIBUTE_SEQUENTIAL_LAYOUT:
146 for (i = 0; i < top; i++){
149 if (class->fields [i].type->attrs & FIELD_ATTRIBUTE_STATIC)
152 size = mono_type_size (class->fields [i].type, &align);
154 class->min_align = MAX (align, class->min_align);
155 class->fields [i].offset = class->instance_size;
156 class->fields [i].offset += align - 1;
157 class->fields [i].offset &= ~(align - 1);
158 class->instance_size = class->fields [i].offset + size;
161 if (class->instance_size & (class->min_align - 1)) {
162 class->instance_size += class->min_align - 1;
163 class->instance_size &= ~(class->min_align - 1);
166 case TYPE_ATTRIBUTE_EXPLICIT_LAYOUT:
167 for (i = 0; i < top; i++) {
169 int idx = class->field.first + i;
172 * There must be info about all the fields in a type if it
173 * uses explicit layout.
176 if (class->fields [i].type->attrs & FIELD_ATTRIBUTE_STATIC)
179 size = mono_type_size (class->fields [i].type, &align);
181 mono_metadata_field_info (m, idx, &class->fields [i].offset, NULL, NULL);
182 if (class->fields [i].offset == (guint32)-1)
183 g_warning ("%s not initialized correctly (missing field layout info for %s)", class->name, class->fields [i].name);
185 * The offset is from the start of the object: this works for both
186 * classes and valuetypes.
188 class->fields [i].offset += sizeof (MonoObject);
192 size += class->fields [i].offset;
193 class->instance_size = MAX (class->instance_size, size);
198 class->size_inited = 1;
201 * Compute static field layout and size
204 case TYPE_ATTRIBUTE_AUTO_LAYOUT:
205 case TYPE_ATTRIBUTE_SEQUENTIAL_LAYOUT:
206 for (i = 0; i < top; i++){
209 if (!(class->fields [i].type->attrs & FIELD_ATTRIBUTE_STATIC))
212 size = mono_type_size (class->fields [i].type, &align);
213 class->fields [i].offset = class->class_size;
214 class->fields [i].offset += align - 1;
215 class->fields [i].offset &= ~(align - 1);
216 class->class_size = class->fields [i].offset + size;
219 case TYPE_ATTRIBUTE_EXPLICIT_LAYOUT:
220 for (i = 0; i < top; i++){
224 * There must be info about all the fields in a type if it
225 * uses explicit layout.
229 if (!(class->fields [i].type->attrs & FIELD_ATTRIBUTE_STATIC))
232 size = mono_type_size (class->fields [i].type, &align);
233 class->fields [i].offset = class->class_size;
234 class->fields [i].offset += align - 1;
235 class->fields [i].offset &= ~(align - 1);
236 class->class_size = class->fields [i].offset + size;
243 init_properties (MonoClass *class)
245 guint startm, endm, i, j;
246 guint32 cols [MONO_PROPERTY_SIZE];
247 MonoTableInfo *pt = &class->image->tables [MONO_TABLE_PROPERTY];
248 MonoTableInfo *msemt = &class->image->tables [MONO_TABLE_METHODSEMANTICS];
250 class->property.first = mono_metadata_properties_from_typedef (class->image, mono_metadata_token_index (class->type_token) - 1, &class->property.last);
251 class->property.count = class->property.last - class->property.first;
253 class->properties = g_new0 (MonoProperty, class->property.count);
254 for (i = class->property.first; i < class->property.last; ++i) {
255 mono_metadata_decode_row (pt, i, cols, MONO_PROPERTY_SIZE);
256 class->properties [i - class->property.first].attrs = cols [MONO_PROPERTY_FLAGS];
257 class->properties [i - class->property.first].name = mono_metadata_string_heap (class->image, cols [MONO_PROPERTY_NAME]);
259 startm = mono_metadata_methods_from_property (class->image, i, &endm);
260 for (j = startm; j < endm; ++j) {
261 mono_metadata_decode_row (msemt, j, cols, MONO_METHOD_SEMA_SIZE);
262 switch (cols [MONO_METHOD_SEMA_SEMANTICS]) {
263 case METHOD_SEMANTIC_SETTER:
264 class->properties [i - class->property.first].set = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
266 case METHOD_SEMANTIC_GETTER:
267 class->properties [i - class->property.first].get = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
277 init_events (MonoClass *class)
279 guint startm, endm, i, j;
280 guint32 cols [MONO_EVENT_SIZE];
281 MonoTableInfo *pt = &class->image->tables [MONO_TABLE_EVENT];
282 MonoTableInfo *msemt = &class->image->tables [MONO_TABLE_METHODSEMANTICS];
284 class->event.first = mono_metadata_events_from_typedef (class->image, mono_metadata_token_index (class->type_token) - 1, &class->event.last);
285 class->event.count = class->event.last - class->event.first;
287 class->events = g_new0 (MonoEvent, class->event.count);
288 for (i = class->event.first; i < class->event.last; ++i) {
289 mono_metadata_decode_row (pt, i, cols, MONO_EVENT_SIZE);
290 class->events [i - class->event.first].attrs = cols [MONO_EVENT_FLAGS];
291 class->events [i - class->event.first].name = mono_metadata_string_heap (class->image, cols [MONO_EVENT_NAME]);
293 startm = mono_metadata_methods_from_event (class->image, i, &endm);
294 for (j = startm; j < endm; ++j) {
295 mono_metadata_decode_row (msemt, j, cols, MONO_METHOD_SEMA_SIZE);
296 switch (cols [MONO_METHOD_SEMA_SEMANTICS]) {
297 case METHOD_SEMANTIC_ADD_ON:
298 class->events [i - class->event.first].add = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
300 case METHOD_SEMANTIC_REMOVE_ON:
301 class->events [i - class->event.first].remove = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
303 case METHOD_SEMANTIC_FIRE:
304 class->events [i - class->event.first].raise = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
306 case METHOD_SEMANTIC_OTHER: /* don't care for now */
315 mono_get_unique_iid (MonoClass *class)
317 static GHashTable *iid_hash = NULL;
318 static guint iid = 0;
323 g_assert (class->flags & TYPE_ATTRIBUTE_INTERFACE);
326 iid_hash = g_hash_table_new (g_str_hash, g_str_equal);
328 str = g_strdup_printf ("%s|%s.%s\n", class->image->name, class->name_space, class->name);
330 if (g_hash_table_lookup_extended (iid_hash, str, NULL, &value)) {
334 g_hash_table_insert (iid_hash, str, (gpointer)iid);
343 * @class: the class to initialize
345 * compute the instance_size, class_size and other infos that cannot be
346 * computed at mono_class_get() time. Also compute a generic vtable and
347 * the method slot numbers. We use this infos later to create a domain
351 mono_class_init (MonoClass *class)
355 int i, max_vtsize = 0, max_iid, cur_slot = 0;
356 static MonoMethod *default_ghc = NULL;
357 static int ghc_slot = -1;
364 if (class->init_pending) {
365 /* this indicates a cyclic dependency */
366 g_error ("pending init %s.%s\n", class->name_space, class->name);
369 class->init_pending = 1;
371 // mono_stats.initialized_class_count++;
374 if (!class->parent->inited)
375 mono_class_init (class->parent);
376 class->instance_size += class->parent->instance_size;
377 class->class_size += class->parent->class_size;
378 class->min_align = class->parent->min_align;
379 cur_slot = class->parent->vtable_size;
381 class->min_align = 1;
384 * Computes the size used by the fields, and their locations
386 if (!class->size_inited && class->field.count > 0){
387 class->fields = g_new0 (MonoClassField, class->field.count);
388 class_compute_field_layout (class);
391 if (!(class->flags & TYPE_ATTRIBUTE_INTERFACE)) {
392 for (i = 0; i < class->interface_count; i++)
393 max_vtsize += class->interfaces [i]->method.count;
396 max_vtsize += class->parent->vtable_size;
398 max_vtsize += class->method.count;
401 vtable = alloca (sizeof (gpointer) * max_vtsize);
402 memset (vtable, 0, sizeof (gpointer) * max_vtsize);
404 /* initialize method pointers */
405 class->methods = g_new (MonoMethod*, class->method.count);
406 for (i = 0; i < class->method.count; ++i)
407 class->methods [i] = mono_get_method (class->image,
408 MONO_TOKEN_METHOD_DEF | (i + class->method.first + 1), class);
410 init_properties (class);
413 i = mono_metadata_nesting_typedef (class->image, class->type_token);
416 guint32 cols [MONO_NESTED_CLASS_SIZE];
417 mono_metadata_decode_row (&class->image->tables [MONO_TABLE_NESTEDCLASS], i - 1, cols, MONO_NESTED_CLASS_SIZE);
418 if (cols [MONO_NESTED_CLASS_ENCLOSING] != mono_metadata_token_index (class->type_token))
420 nclass = mono_class_create_from_typedef (class->image, MONO_TOKEN_TYPE_DEF | cols [MONO_NESTED_CLASS_NESTED]);
421 class->nested_classes = g_list_prepend (class->nested_classes, nclass);
425 if (class->flags & TYPE_ATTRIBUTE_INTERFACE) {
426 for (i = 0; i < class->method.count; ++i)
427 class->methods [i]->slot = i;
428 class->init_pending = 0;
433 //printf ("METAINIT %s.%s\n", class->name_space, class->name);
435 /* compute maximum number of slots and maximum interface id */
437 for (k = class; k ; k = k->parent) {
438 for (i = 0; i < k->interface_count; i++) {
439 ic = k->interfaces [i];
442 mono_class_init (ic);
444 if (max_iid < ic->interface_id)
445 max_iid = ic->interface_id;
449 class->max_interface_id = max_iid;
450 /* compute vtable offset for interfaces */
451 class->interface_offsets = g_malloc (sizeof (gpointer) * (max_iid + 1));
453 for (i = 0; i <= max_iid; i++)
454 class->interface_offsets [i] = -1;
456 for (i = 0; i < class->interface_count; i++) {
457 ic = class->interfaces [i];
458 class->interface_offsets [ic->interface_id] = cur_slot;
459 cur_slot += ic->method.count;
462 for (k = class->parent; k ; k = k->parent) {
463 for (i = 0; i < k->interface_count; i++) {
464 ic = k->interfaces [i];
465 if (class->interface_offsets [ic->interface_id] == -1) {
466 int io = k->interface_offsets [ic->interface_id];
469 g_assert (io <= max_vtsize);
471 class->interface_offsets [ic->interface_id] = io;
476 if (class->parent && class->parent->vtable_size)
477 memcpy (vtable, class->parent->vtable, sizeof (gpointer) * class->parent->vtable_size);
479 for (k = class; k ; k = k->parent) {
480 for (i = 0; i < k->interface_count; i++) {
483 ic = k->interfaces [i];
484 io = k->interface_offsets [ic->interface_id];
487 g_assert (io <= max_vtsize);
490 for (l = 0; l < ic->method.count; l++) {
491 MonoMethod *im = ic->methods [l];
492 for (j = 0; j < class->method.count; ++j) {
493 MonoMethod *cm = class->methods [j];
494 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
495 !(cm->flags & METHOD_ATTRIBUTE_PUBLIC) ||
496 !(cm->flags & METHOD_ATTRIBUTE_NEW_SLOT))
498 if (!strcmp(cm->name, im->name) &&
499 mono_metadata_signature_equal (cm->signature, im->signature)) {
500 g_assert (io + l <= max_vtsize);
501 vtable [io + l] = cm;
506 /* already implemented */
507 if (io >= k->vtable_size)
511 for (l = 0; l < ic->method.count; l++) {
512 MonoMethod *im = ic->methods [l];
515 g_assert (io + l <= max_vtsize);
520 for (k1 = class; k1; k1 = k1->parent) {
521 for (j = 0; j < k1->method.count; ++j) {
522 MonoMethod *cm = k1->methods [j];
524 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
525 !(cm->flags & METHOD_ATTRIBUTE_PUBLIC))
528 if (!strcmp(cm->name, im->name) &&
529 mono_metadata_signature_equal (cm->signature, im->signature)) {
530 g_assert (io + l <= max_vtsize);
531 vtable [io + l] = cm;
536 g_assert (io + l <= max_vtsize);
542 for (l = 0; l < ic->method.count; l++) {
543 MonoMethod *im = ic->methods [l];
544 char *qname, *fqname;
546 qname = g_strconcat (ic->name, ".", im->name, NULL);
547 if (ic->name_space && ic->name_space [0])
548 fqname = g_strconcat (ic->name_space, ".", ic->name, ".", im->name, NULL);
552 for (j = 0; j < class->method.count; ++j) {
553 MonoMethod *cm = class->methods [j];
555 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL))
558 if (((fqname && !strcmp (cm->name, fqname)) || !strcmp (cm->name, qname)) &&
559 mono_metadata_signature_equal (cm->signature, im->signature)) {
560 g_assert (io + l <= max_vtsize);
561 vtable [io + l] = cm;
570 if (!(class->flags & TYPE_ATTRIBUTE_ABSTRACT)) {
571 for (l = 0; l < ic->method.count; l++) {
572 MonoMethod *im = ic->methods [l];
573 g_assert (io + l <= max_vtsize);
574 if (!(vtable [io + l])) {
575 printf ("no implementation for interface method %s.%s::%s in class %s.%s\n",
576 ic->name_space, ic->name, im->name, class->name_space, class->name);
578 for (j = 0; j < class->method.count; ++j) {
579 MonoMethod *cm = class->methods [j];
581 printf ("METHOD %s\n", cm->name);
583 g_assert_not_reached ();
588 for (l = 0; l < ic->method.count; l++) {
589 MonoMethod *im = vtable [io + l];
592 g_assert (io + l <= max_vtsize);
594 // fixme: why do we need this ?
596 // g_assert_not_reached ();
603 for (i = 0; i < class->method.count; ++i) {
606 cm = class->methods [i];
610 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
613 #else /* EXT_VTABLE_HACK */
618 if (!(cm->flags & METHOD_ATTRIBUTE_NEW_SLOT) && (cm->flags & METHOD_ATTRIBUTE_VIRTUAL)) {
619 for (k = class->parent; k ; k = k->parent) {
621 for (j = 0; j < k->method.count; ++j) {
622 MonoMethod *m1 = k->methods [j];
623 if (!(m1->flags & METHOD_ATTRIBUTE_VIRTUAL))
625 if (!strcmp(cm->name, m1->name) &&
626 mono_metadata_signature_equal (cm->signature, m1->signature)) {
627 cm->slot = k->methods [j]->slot;
628 g_assert (cm->slot < max_vtsize);
638 cm->slot = cur_slot++;
640 if (!(cm->flags & METHOD_ATTRIBUTE_ABSTRACT))
641 vtable [cm->slot] = cm;
645 class->vtable_size = cur_slot;
646 class->vtable = g_malloc0 (sizeof (gpointer) * class->vtable_size);
647 memcpy (class->vtable, vtable, sizeof (gpointer) * class->vtable_size);
650 class->init_pending = 0;
652 if (mono_print_vtable) {
655 for (i = 0; i <= max_iid; i++)
656 if (class->interface_offsets [i] != -1)
659 printf ("VTable %s.%s (size = %d, interfaces = %d)\n", class->name_space,
660 class->name, class->vtable_size, icount);
662 for (i = 0; i < class->vtable_size; ++i) {
667 printf (" slot %03d(%03d) %s.%s:%s\n", i, cm->slot,
668 cm->klass->name_space, cm->klass->name,
675 printf ("Interfaces %s.%s (max_iid = %d)\n", class->name_space,
676 class->name, max_iid);
678 for (i = 0; i < class->interface_count; i++) {
679 ic = class->interfaces [i];
680 printf (" slot %03d(%03d) %s.%s\n",
681 class->interface_offsets [ic->interface_id],
682 ic->method.count, ic->name_space, ic->name);
685 for (k = class->parent; k ; k = k->parent) {
686 for (i = 0; i < k->interface_count; i++) {
687 ic = k->interfaces [i];
688 printf (" slot %03d(%03d) %s.%s\n",
689 class->interface_offsets [ic->interface_id],
690 ic->method.count, ic->name_space, ic->name);
697 if (class == mono_defaults.object_class) {
699 for (i = 0; i < class->vtable_size; ++i) {
700 MonoMethod *cm = vtable [i];
702 if (!strcmp (cm->name, "GetHashCode")) {
708 g_assert (ghc_slot > 0);
710 default_ghc = vtable [ghc_slot];
715 if (class != mono_defaults.object_class) {
717 if (vtable [ghc_slot] == default_ghc) {
726 * Compute a relative numbering of the class hierarchy as described in
727 * "Java for Large-Scale Scientific Computations?"
730 mono_compute_relative_numbering (MonoClass *class, int *c)
738 for (s = class->subclasses; s; s = s->next)
739 mono_compute_relative_numbering ((MonoClass *)s->data, c);
741 class->diffval = *c - class->baseval;
745 mono_class_setup_mono_type (MonoClass *class)
747 const char *name = class->name;
748 const char *nspace = class->name_space;
750 class->this_arg.byref = 1;
751 class->this_arg.data.klass = class;
752 class->this_arg.type = MONO_TYPE_CLASS;
753 class->byval_arg.data.klass = class;
754 class->byval_arg.type = MONO_TYPE_CLASS;
756 if (!strcmp (nspace, "System")) {
757 if (!strcmp (name, "ValueType")) {
759 * do not set the valuetype bit for System.ValueType.
760 * class->valuetype = 1;
762 } else if (!strcmp (name, "Enum")) {
764 * do not set the valuetype bit for System.Enum.
765 * class->valuetype = 1;
767 class->valuetype = 0;
769 } else if (!strcmp (name, "Object")) {
770 class->this_arg.type = class->byval_arg.type = MONO_TYPE_OBJECT;
771 } else if (!strcmp (name, "String")) {
772 class->this_arg.type = class->byval_arg.type = MONO_TYPE_STRING;
776 if (class->valuetype) {
777 int t = MONO_TYPE_VALUETYPE;
778 if (!strcmp (nspace, "System")) {
781 if (!strcmp (name, "Boolean")) {
782 t = MONO_TYPE_BOOLEAN;
783 } else if (!strcmp(name, "Byte")) {
788 if (!strcmp (name, "Char")) {
793 if (!strcmp (name, "Double")) {
798 if (!strcmp (name, "Int32")) {
800 } else if (!strcmp(name, "Int16")) {
802 } else if (!strcmp(name, "Int64")) {
804 } else if (!strcmp(name, "IntPtr")) {
809 if (!strcmp (name, "Single")) {
811 } else if (!strcmp(name, "SByte")) {
816 if (!strcmp (name, "UInt32")) {
818 } else if (!strcmp(name, "UInt16")) {
820 } else if (!strcmp(name, "UInt64")) {
822 } else if (!strcmp(name, "UIntPtr")) {
827 if (!strcmp (name, "Void")) {
835 class->this_arg.type = class->byval_arg.type = t;
840 mono_class_setup_parent (MonoClass *class, MonoClass *parent)
842 gboolean system_namespace;
844 system_namespace = !strcmp (class->name_space, "System");
846 /* if root of the hierarchy */
847 if (system_namespace && !strcmp (class->name, "Object")) {
848 class->parent = NULL;
849 class->instance_size = sizeof (MonoObject);
853 if (!(class->flags & TYPE_ATTRIBUTE_INTERFACE)) {
855 class->parent = parent;
857 class->marshalbyref = parent->marshalbyref;
858 class->contextbound = parent->contextbound;
859 class->delegate = parent->delegate;
861 if (system_namespace) {
862 if (*class->name == 'M' && !strcmp (class->name, "MarshalByRefObject"))
863 class->marshalbyref = 1;
865 if (*class->name == 'C' && !strcmp (class->name, "ContextBoundObject"))
866 class->contextbound = 1;
868 if (*class->name == 'D' && !strcmp (class->name, "Delegate"))
872 if (class->parent->enumtype || ((strcmp (class->parent->name, "ValueType") == 0) &&
873 (strcmp (class->parent->name_space, "System") == 0)))
874 class->valuetype = 1;
875 if (((strcmp (class->parent->name, "Enum") == 0) && (strcmp (class->parent->name_space, "System") == 0))) {
876 class->valuetype = class->enumtype = 1;
878 //class->enumtype = class->parent->enumtype;
879 class->parent->subclasses = g_list_prepend (class->parent->subclasses, class);
880 mono_compute_relative_numbering (mono_defaults.object_class, &rnum);
882 class->parent = NULL;
888 * @image: context where the image is created
889 * @type_token: typedef token
892 mono_class_create_from_typedef (MonoImage *image, guint32 type_token)
894 MonoTableInfo *tt = &image->tables [MONO_TABLE_TYPEDEF];
895 MonoClass *class, *parent = NULL;
896 guint32 cols [MONO_TYPEDEF_SIZE];
897 guint32 cols_next [MONO_TYPEDEF_SIZE];
898 guint tidx = mono_metadata_token_index (type_token);
899 const char *name, *nspace;
901 MonoClass **interfaces;
903 if ((class = g_hash_table_lookup (image->class_cache, GUINT_TO_POINTER (type_token))))
906 g_assert (mono_metadata_token_table (type_token) == MONO_TABLE_TYPEDEF);
908 mono_metadata_decode_row (tt, tidx - 1, cols, CSIZE (cols));
910 name = mono_metadata_string_heap (image, cols[1]);
911 nspace = mono_metadata_string_heap (image, cols[2]);
913 if (!(!strcmp (nspace, "System") && !strcmp (name, "Object")) &&
914 !(cols [0] & TYPE_ATTRIBUTE_INTERFACE)) {
915 parent = mono_class_get (image, mono_metadata_token_from_dor (cols [3]));
917 interfaces = mono_metadata_interfaces_from_typedef (image, type_token, &icount);
919 class = g_malloc0 (sizeof (MonoClass));
921 g_hash_table_insert (image->class_cache, GUINT_TO_POINTER (type_token), class);
923 class->interfaces = interfaces;
924 class->interface_count = icount;
927 class->name_space = nspace;
929 class->image = image;
930 class->type_token = type_token;
931 class->flags = cols [MONO_TYPEDEF_FLAGS];
933 class->element_class = class;
935 /*g_print ("Init class %s\n", name);*/
937 mono_class_setup_parent (class, parent);
939 mono_class_setup_mono_type (class);
942 * Compute the field and method lists
944 class->field.first = cols [MONO_TYPEDEF_FIELD_LIST] - 1;
945 class->method.first = cols [MONO_TYPEDEF_METHOD_LIST] - 1;
947 if (tt->rows > tidx){
948 mono_metadata_decode_row (tt, tidx, cols_next, CSIZE (cols_next));
949 class->field.last = cols_next [MONO_TYPEDEF_FIELD_LIST] - 1;
950 class->method.last = cols_next [MONO_TYPEDEF_METHOD_LIST] - 1;
952 class->field.last = image->tables [MONO_TABLE_FIELD].rows;
953 class->method.last = image->tables [MONO_TABLE_METHOD].rows;
956 if (cols [MONO_TYPEDEF_FIELD_LIST] &&
957 cols [MONO_TYPEDEF_FIELD_LIST] <= image->tables [MONO_TABLE_FIELD].rows)
958 class->field.count = class->field.last - class->field.first;
960 class->field.count = 0;
962 if (cols [MONO_TYPEDEF_METHOD_LIST] <= image->tables [MONO_TABLE_METHOD].rows)
963 class->method.count = class->method.last - class->method.first;
965 class->method.count = 0;
967 /* reserve space to store vector pointer in arrays */
968 if (!strcmp (nspace, "System") && !strcmp (name, "Array")) {
969 class->instance_size += 2 * sizeof (gpointer);
970 g_assert (class->field.count == 0);
973 if (class->flags & TYPE_ATTRIBUTE_INTERFACE)
974 class->interface_id = mono_get_unique_iid (class);
976 //class->interfaces = mono_metadata_interfaces_from_typedef (image, type_token, &class->interface_count);
978 if (class->enumtype) {
979 class->fields = g_new0 (MonoClassField, class->field.count);
980 class_compute_field_layout (class);
983 if ((type_token = mono_metadata_nested_in_typedef (image, type_token)))
984 class->nested_in = mono_class_create_from_typedef (image, type_token);
989 mono_ptr_class_get (MonoType *type)
993 static GHashTable *ptr_hash = NULL;
996 ptr_hash = g_hash_table_new (g_direct_hash, g_direct_equal);
997 el_class = mono_class_from_mono_type (type);
998 if ((result = g_hash_table_lookup (ptr_hash, el_class)))
1000 result = g_new0 (MonoClass, 1);
1002 result->parent = NULL; /* no parent for PTR types */
1003 result->name = "System";
1004 result->name_space = "MonoPtrFakeClass";
1005 result->image = el_class->image;
1006 result->inited = TRUE;
1007 /* Can pointers get boxed? */
1008 result->instance_size = sizeof (gpointer);
1010 * baseval, diffval: need them to allow casting ?
1012 result->element_class = el_class;
1013 result->enum_basetype = &result->element_class->byval_arg;
1015 result->this_arg.type = result->byval_arg.type = MONO_TYPE_PTR;
1016 result->this_arg.data.type = result->byval_arg.data.type = result->enum_basetype;
1017 result->this_arg.byref = TRUE;
1019 g_hash_table_insert (ptr_hash, el_class, result);
1025 mono_class_from_mono_type (MonoType *type)
1027 switch (type->type) {
1028 case MONO_TYPE_OBJECT:
1029 return mono_defaults.object_class;
1030 case MONO_TYPE_VOID:
1031 return mono_defaults.void_class;
1032 case MONO_TYPE_BOOLEAN:
1033 return mono_defaults.boolean_class;
1034 case MONO_TYPE_CHAR:
1035 return mono_defaults.char_class;
1037 return mono_defaults.sbyte_class;
1039 return mono_defaults.byte_class;
1041 return mono_defaults.int16_class;
1043 return mono_defaults.uint16_class;
1045 return mono_defaults.int32_class;
1047 return mono_defaults.uint32_class;
1049 return mono_defaults.int_class;
1051 return mono_defaults.uint_class;
1053 return mono_defaults.int64_class;
1055 return mono_defaults.uint64_class;
1057 return mono_defaults.single_class;
1059 return mono_defaults.double_class;
1060 case MONO_TYPE_STRING:
1061 return mono_defaults.string_class;
1062 case MONO_TYPE_ARRAY:
1063 return mono_array_class_get (type->data.array->type, type->data.array->rank);
1065 return mono_ptr_class_get (type->data.type);
1066 case MONO_TYPE_SZARRAY:
1067 return mono_array_class_get (type->data.type, 1);
1068 case MONO_TYPE_CLASS:
1069 case MONO_TYPE_VALUETYPE:
1070 return type->data.klass;
1072 g_warning ("implement me %02x\n", type->type);
1073 g_assert_not_reached ();
1080 * @image: context where the image is created
1081 * @type_spec: typespec token
1084 mono_class_create_from_typespec (MonoImage *image, guint32 type_spec)
1089 type = mono_type_create_from_typespec (image, type_spec);
1091 switch (type->type) {
1092 case MONO_TYPE_ARRAY:
1093 class = mono_array_class_get (type->data.array->type, type->data.array->rank);
1095 case MONO_TYPE_SZARRAY:
1096 class = mono_array_class_get (type->data.type, 1);
1099 class = mono_class_from_mono_type (type->data.type);
1102 g_warning ("implement me: %08x", type->type);
1103 g_assert_not_reached ();
1106 mono_metadata_free_type (type);
1112 * mono_array_class_get:
1113 * @element_type: element type
1114 * @rank: the dimension of the array class
1116 * Returns: a class object describing the array with element type @element_type and
1120 mono_array_class_get (MonoType *element_type, guint32 rank)
1125 static MonoClass *parent = NULL;
1129 eclass = mono_class_from_mono_type (element_type);
1130 g_assert (rank <= 255);
1133 parent = mono_defaults.array_class;
1135 if (!parent->inited)
1136 mono_class_init (parent);
1138 image = eclass->image;
1140 g_assert (!eclass->type_token ||
1141 mono_metadata_token_table (eclass->type_token) == MONO_TABLE_TYPEDEF);
1143 key = ((rank & 0xff) << 24) | (eclass->type_token & 0xffffff);
1144 if ((class = g_hash_table_lookup (image->array_cache, GUINT_TO_POINTER (key))))
1147 class = g_malloc0 (sizeof (MonoClass) + parent->vtable_size * sizeof (gpointer));
1149 class->image = image;
1150 class->name_space = "System";
1151 class->name = "Array";
1152 class->type_token = 0;
1153 class->flags = TYPE_ATTRIBUTE_CLASS;
1154 class->parent = parent;
1155 class->instance_size = mono_class_instance_size (class->parent);
1156 class->class_size = 0;
1157 class->vtable_size = parent->vtable_size;
1158 class->parent->subclasses = g_list_prepend (class->parent->subclasses, class);
1159 mono_compute_relative_numbering (mono_defaults.object_class, &rnum);
1162 class->element_class = eclass;
1164 MonoArrayType *at = g_new0 (MonoArrayType, 1);
1165 class->byval_arg.type = MONO_TYPE_ARRAY;
1166 class->byval_arg.data.array = at;
1167 at->type = &eclass->byval_arg;
1169 /* FIXME: complete.... */
1171 /* FIXME: this is not correct. the lbound could be >0 */
1172 class->byval_arg.type = MONO_TYPE_SZARRAY;
1173 class->byval_arg.data.type = &eclass->byval_arg;
1175 class->this_arg = class->byval_arg;
1176 class->this_arg.byref = 1;
1178 g_hash_table_insert (image->array_cache, GUINT_TO_POINTER (key), class);
1183 * mono_class_instance_size:
1186 * Returns: the size of an object instance
1189 mono_class_instance_size (MonoClass *klass)
1192 if (!klass->size_inited)
1193 mono_class_init (klass);
1195 return klass->instance_size;
1199 * mono_class_value_size:
1202 * This function is used for value types, and return the
1203 * space and the alignment to store that kind of value object.
1205 * Returns: the size of a value of kind @klass
1208 mono_class_value_size (MonoClass *klass, guint32 *align)
1212 /* fixme: check disable, because we still have external revereces to
1213 * mscorlib and Dummy Objects
1215 /*g_assert (klass->valuetype);*/
1217 size = mono_class_instance_size (klass) - sizeof (MonoObject);
1220 *align = klass->min_align;
1226 * mono_class_data_size:
1229 * Returns: the size of the static class data
1232 mono_class_data_size (MonoClass *klass)
1236 mono_class_init (klass);
1238 return klass->class_size;
1242 * Auxiliary routine to mono_class_get_field
1244 * Takes a field index instead of a field token.
1246 static MonoClassField *
1247 mono_class_get_field_idx (MonoClass *class, int idx)
1249 if (class->field.count){
1250 if ((idx >= class->field.first) && (idx < class->field.last)){
1251 return &class->fields [idx - class->field.first];
1258 return mono_class_get_field_idx (class->parent, idx);
1262 * mono_class_get_field:
1263 * @class: the class to lookup the field.
1264 * @field_token: the field token
1266 * Returns: A MonoClassField representing the type and offset of
1267 * the field, or a NULL value if the field does not belong to this
1271 mono_class_get_field (MonoClass *class, guint32 field_token)
1273 int idx = mono_metadata_token_index (field_token);
1275 g_assert (mono_metadata_token_code (field_token) == MONO_TOKEN_FIELD_DEF);
1277 return mono_class_get_field_idx (class, idx - 1);
1281 mono_class_get_field_from_name (MonoClass *klass, const char *name)
1285 for (i = 0; i < klass->field.count; ++i) {
1286 if (strcmp (name, klass->fields [i].name) == 0)
1287 return &klass->fields [i];
1294 * @image: the image where the class resides
1295 * @type_token: the token for the class
1296 * @at: an optional pointer to return the array element type
1298 * Returns: the MonoClass that represents @type_token in @image
1301 mono_class_get (MonoImage *image, guint32 type_token)
1305 switch (type_token & 0xff000000){
1306 case MONO_TOKEN_TYPE_DEF:
1307 class = mono_class_create_from_typedef (image, type_token);
1309 case MONO_TOKEN_TYPE_REF:
1310 class = mono_class_create_from_typeref (image, type_token);
1312 case MONO_TOKEN_TYPE_SPEC:
1313 class = mono_class_create_from_typespec (image, type_token);
1316 g_warning ("unknown token type %x", type_token & 0xff000000);
1317 g_assert_not_reached ();
1321 g_warning ("Could not load class from token 0x%08x", type_token);
1326 mono_class_from_name (MonoImage *image, const char* name_space, const char *name)
1328 GHashTable *nspace_table;
1331 nspace_table = g_hash_table_lookup (image->name_cache, name_space);
1334 token = GPOINTER_TO_UINT (g_hash_table_lookup (nspace_table, name));
1337 /*g_warning ("token not found for %s.%s in image %s", name_space, name, image->name);*/
1341 token = MONO_TOKEN_TYPE_DEF | token;
1343 return mono_class_get (image, token);
1347 * mono_array_element_size:
1348 * @ac: pointer to a #MonoArrayClass
1350 * Returns: the size of single array element.
1353 mono_array_element_size (MonoClass *ac)
1355 if (ac->element_class->valuetype)
1356 return mono_class_instance_size (ac->element_class) - sizeof (MonoObject);
1358 return sizeof (gpointer);
1362 mono_ldtoken (MonoImage *image, guint32 token, MonoClass **handle_class)
1364 switch (token & 0xff000000) {
1365 case MONO_TOKEN_TYPE_DEF:
1366 case MONO_TOKEN_TYPE_REF: {
1369 *handle_class = mono_defaults.typehandle_class;
1370 class = mono_class_get (image, token);
1371 mono_class_init (class);
1372 /* We return a MonoType* as handle */
1373 return &class->byval_arg;
1375 case MONO_TOKEN_TYPE_SPEC: {
1378 *handle_class = mono_defaults.typehandle_class;
1379 class = mono_class_create_from_typespec (image, token);
1380 mono_class_init (class);
1381 return &class->byval_arg;
1383 case MONO_TOKEN_FIELD_DEF: {
1385 guint32 type = mono_metadata_typedef_from_field (image, mono_metadata_token_index (token));
1386 class = mono_class_get (image, MONO_TOKEN_TYPE_DEF | type);
1387 mono_class_init (class);
1389 *handle_class = mono_class_from_name (mono_defaults.corlib, "System", "RuntimeFieldHandle");
1390 return mono_class_get_field (class, token);
1392 case MONO_TOKEN_METHOD_DEF:
1393 case MONO_TOKEN_MEMBER_REF:
1395 g_warning ("Unknown token 0x%08x in ldtoken", token);