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>
32 #include <mono/metadata/reflection.h>
33 #include <mono/os/gc_wrapper.h>
36 * Uncomment this to enable GC aware auto layout: in this mode, reference
37 * fields are grouped together inside objects, increasing collector
39 * Requires that all classes whose layout is known to native code be annotated
40 * with [StructLayout (LayoutKind.Sequential)]
42 //#define GC_AWARE_AUTO_LAYOUT
44 #define CSIZE(x) (sizeof (x) / 4)
48 gboolean mono_print_vtable = FALSE;
50 static MonoClass * mono_class_create_from_typedef (MonoImage *image, guint32 type_token);
52 void (*mono_debugger_class_init_func) (MonoClass *klass) = NULL;
55 mono_class_from_typeref (MonoImage *image, guint32 type_token)
57 guint32 cols [MONO_TYPEREF_SIZE];
58 MonoTableInfo *t = &image->tables [MONO_TABLE_TYPEREF];
60 const char *name, *nspace;
62 MonoAssembly **references;
63 MonoImageOpenStatus status;
65 mono_metadata_decode_row (t, (type_token&0xffffff)-1, cols, MONO_TYPEREF_SIZE);
67 name = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAME]);
68 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAMESPACE]);
70 idx = cols [MONO_TYPEREF_SCOPE] >> RESOLTION_SCOPE_BITS;
71 switch (cols [MONO_TYPEREF_SCOPE] & RESOLTION_SCOPE_MASK) {
72 case RESOLTION_SCOPE_MODULE:
74 g_error ("null ResolutionScope not yet handled");
75 /* a typedef in disguise */
76 return mono_class_from_name (image, nspace, name);
77 case RESOLTION_SCOPE_MODULEREF:
78 return mono_class_from_name (image->modules [idx - 1], nspace, name);
79 case RESOLTION_SCOPE_TYPEREF: {
80 MonoClass *enclosing = mono_class_from_typeref (image, MONO_TOKEN_TYPE_REF | idx);
82 mono_class_init (enclosing);
83 for (tmp = enclosing->nested_classes; tmp; tmp = tmp->next) {
85 if (strcmp (res->name, name) == 0)
88 g_warning ("TypeRef ResolutionScope not yet handled (%d)", idx);
91 case RESOLTION_SCOPE_ASSEMBLYREF:
95 mono_image_load_references (image, &status);
96 references = image->references;
97 if (!references || !references [idx-1]) {
99 * detected a reference to mscorlib, we simply return a reference to a dummy
100 * until we have a better solution.
102 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]));
104 res = mono_class_from_name (image, "System", "MonoDummy");
105 /* prevent method loading */
107 /* some storage if the type is used - very ugly hack */
108 res->instance_size = 2*sizeof (gpointer);
112 /* load referenced assembly */
113 image = references [idx-1]->image;
115 return mono_class_from_name (image, nspace, name);
119 * class_compute_field_layout:
120 * @m: pointer to the metadata.
121 * @class: The class to initialize
123 * Initializes the class->fields.
125 * Currently we only support AUTO_LAYOUT, and do not even try to do
126 * a good job at it. This is temporary to get the code for Paolo.
129 class_compute_field_layout (MonoClass *class)
131 MonoImage *m = class->image;
132 const int top = class->field.count;
133 guint32 layout = class->flags & TYPE_ATTRIBUTE_LAYOUT_MASK;
134 MonoTableInfo *t = &m->tables [MONO_TABLE_FIELD];
135 int i, blittable = TRUE, real_size = 0;
137 guint32 packing_size = 0;
138 gboolean explicit_size;
140 if (class->size_inited)
144 if (!class->parent->size_inited)
145 class_compute_field_layout (class->parent);
146 class->instance_size += class->parent->instance_size;
147 class->min_align = class->parent->min_align;
148 blittable = class->blittable;
150 class->instance_size = sizeof (MonoObject);
151 class->min_align = 1;
154 /* Get the real size */
155 explicit_size = mono_metadata_packing_from_typedef (class->image, class->type_token, &packing_size, &real_size);
158 g_assert ((packing_size & 0xfffffff0) == 0);
159 class->packing_size = packing_size;
160 real_size += class->instance_size;
164 if (explicit_size && real_size) {
165 class->instance_size = MAX (real_size, class->instance_size);
167 class->size_inited = 1;
171 class->fields = g_new0 (MonoClassField, top);
174 * Fetch all the field information.
176 for (i = 0; i < top; i++){
178 guint32 cols [MONO_FIELD_SIZE];
179 guint32 constant_cols [MONO_CONSTANT_SIZE];
181 int idx = class->field.first + i;
183 mono_metadata_decode_row (t, idx, cols, CSIZE (cols));
184 /* The name is needed for fieldrefs */
185 class->fields [i].name = mono_metadata_string_heap (m, cols [MONO_FIELD_NAME]);
186 sig = mono_metadata_blob_heap (m, cols [MONO_FIELD_SIGNATURE]);
187 mono_metadata_decode_value (sig, &sig);
188 /* FIELD signature == 0x06 */
189 g_assert (*sig == 0x06);
190 class->fields [i].type = mono_metadata_parse_field_type (
191 m, cols [MONO_FIELD_FLAGS], sig + 1, &sig);
193 class->fields [i].parent = class;
195 if (!(class->fields [i].type->attrs & FIELD_ATTRIBUTE_STATIC)) {
196 if (class->fields [i].type->byref) {
199 MonoClass *field_class = mono_class_from_mono_type (class->fields [i].type);
200 if (!field_class || !field_class->blittable)
204 if (layout == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT) {
205 mono_metadata_field_info (m, idx, &class->fields [i].offset, NULL, NULL);
206 if (class->fields [i].offset == (guint32)-1)
207 g_warning ("%s not initialized correctly (missing field layout info for %s)", class->name, class->fields [i].name);
210 if (cols [MONO_FIELD_FLAGS] & FIELD_ATTRIBUTE_HAS_FIELD_RVA) {
211 mono_metadata_field_info (m, idx, NULL, &rva, NULL);
213 g_warning ("field %s in %s should have RVA data, but hasn't", class->fields [i].name, class->name);
214 class->fields [i].data = mono_cli_rva_map (class->image->image_info, rva);
217 if (class->enumtype && !(cols [MONO_FIELD_FLAGS] & FIELD_ATTRIBUTE_STATIC)) {
218 class->enum_basetype = class->fields [i].type;
219 class->cast_class = class->element_class = mono_class_from_mono_type (class->enum_basetype);
220 blittable = class->element_class->blittable;
223 if ((class->fields [i].type->attrs & FIELD_ATTRIBUTE_HAS_DEFAULT) &&
224 (class->fields [i].type->attrs & FIELD_ATTRIBUTE_STATIC)) {
225 cindex = mono_metadata_get_constant_index (class->image, MONO_TOKEN_FIELD_DEF | (class->field.first + i + 1));
227 g_warning ("constant for field %s:%s not found", class->name, class->fields [i].name);
230 mono_metadata_decode_row (&class->image->tables [MONO_TABLE_CONSTANT], cindex - 1, constant_cols, MONO_CONSTANT_SIZE);
231 class->fields [i].def_value = g_new0 (MonoConstant, 1);
232 class->fields [i].def_value->type = constant_cols [MONO_CONSTANT_TYPE];
233 class->fields [i].def_value->value = (gpointer)mono_metadata_blob_heap (class->image, constant_cols [MONO_CONSTANT_VALUE]);
237 if (class == mono_defaults.string_class)
240 class->blittable = blittable;
242 if (class->enumtype && !class->enum_basetype) {
243 if (!((strcmp (class->name, "Enum") == 0) && (strcmp (class->name_space, "System") == 0)))
246 if (explicit_size && real_size) {
247 class->instance_size = MAX (real_size, class->instance_size);
250 mono_class_layout_fields (class);
254 mono_class_layout_fields (MonoClass *class)
257 const int top = class->field.count;
258 guint32 layout = class->flags & TYPE_ATTRIBUTE_LAYOUT_MASK;
259 guint32 pass, passes, real_size;
262 * Compute field layout and total size (not considering static fields)
266 case TYPE_ATTRIBUTE_AUTO_LAYOUT:
267 case TYPE_ATTRIBUTE_SEQUENTIAL_LAYOUT:
269 #ifdef GC_AWARE_AUTO_LAYOUT
274 if (layout != TYPE_ATTRIBUTE_AUTO_LAYOUT)
278 real_size = class->parent->instance_size;
280 real_size = sizeof (MonoObject);
282 for (pass = 0; pass < passes; ++pass) {
283 for (i = 0; i < top; i++){
286 if (class->fields [i].type->attrs & FIELD_ATTRIBUTE_STATIC)
289 #ifdef GC_AWARE_AUTO_LAYOUT
290 /* FIXME: Fix mono_marshal_load_type_info () too */
291 if (layout == TYPE_ATTRIBUTE_AUTO_LAYOUT) {
293 * We process fields with reference type in the first pass,
294 * and fields with non-reference type in the second pass.
295 * We use IS_POINTER instead of IS_REFERENCE because in
296 * some internal structures, we store GC_MALLOCed memory
297 * in IntPtr fields...
299 if (MONO_TYPE_IS_POINTER (class->fields [i].type)) {
309 size = mono_type_size (class->fields [i].type, &align);
311 /* FIXME (LAMESPEC): should we also change the min alignment according to pack? */
312 align = class->packing_size ? MIN (class->packing_size, align): align;
313 class->min_align = MAX (align, class->min_align);
314 class->fields [i].offset = real_size;
315 class->fields [i].offset += align - 1;
316 class->fields [i].offset &= ~(align - 1);
317 real_size = class->fields [i].offset + size;
320 class->instance_size = MAX (real_size, class->instance_size);
322 if (class->instance_size & (class->min_align - 1)) {
323 class->instance_size += class->min_align - 1;
324 class->instance_size &= ~(class->min_align - 1);
328 case TYPE_ATTRIBUTE_EXPLICIT_LAYOUT:
330 for (i = 0; i < top; i++) {
334 * There must be info about all the fields in a type if it
335 * uses explicit layout.
338 if (class->fields [i].type->attrs & FIELD_ATTRIBUTE_STATIC)
341 size = mono_type_size (class->fields [i].type, &align);
344 * When we get here, class->fields [i].offset is already set by the
345 * loader (for either runtime fields or fields loaded from metadata).
346 * The offset is from the start of the object: this works for both
347 * classes and valuetypes.
349 class->fields [i].offset += sizeof (MonoObject);
354 real_size = MAX (real_size, size + class->fields [i].offset);
356 class->instance_size = MAX (real_size, class->instance_size);
360 class->size_inited = 1;
363 * Compute static field layout and size
365 for (i = 0; i < top; i++){
368 if (!(class->fields [i].type->attrs & FIELD_ATTRIBUTE_STATIC))
371 size = mono_type_size (class->fields [i].type, &align);
372 class->fields [i].offset = class->class_size;
373 class->fields [i].offset += align - 1;
374 class->fields [i].offset &= ~(align - 1);
375 class->class_size = class->fields [i].offset + size;
380 init_properties (MonoClass *class)
382 guint startm, endm, i, j;
383 guint32 cols [MONO_PROPERTY_SIZE];
384 MonoTableInfo *pt = &class->image->tables [MONO_TABLE_PROPERTY];
385 MonoTableInfo *msemt = &class->image->tables [MONO_TABLE_METHODSEMANTICS];
387 class->property.first = mono_metadata_properties_from_typedef (class->image, mono_metadata_token_index (class->type_token) - 1, &class->property.last);
388 class->property.count = class->property.last - class->property.first;
390 class->properties = g_new0 (MonoProperty, class->property.count);
391 for (i = class->property.first; i < class->property.last; ++i) {
392 mono_metadata_decode_row (pt, i, cols, MONO_PROPERTY_SIZE);
393 class->properties [i - class->property.first].attrs = cols [MONO_PROPERTY_FLAGS];
394 class->properties [i - class->property.first].name = mono_metadata_string_heap (class->image, cols [MONO_PROPERTY_NAME]);
396 startm = mono_metadata_methods_from_property (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_SETTER:
401 class->properties [i - class->property.first].set = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
403 case METHOD_SEMANTIC_GETTER:
404 class->properties [i - class->property.first].get = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
414 init_events (MonoClass *class)
416 guint startm, endm, i, j;
417 guint32 cols [MONO_EVENT_SIZE];
418 MonoTableInfo *pt = &class->image->tables [MONO_TABLE_EVENT];
419 MonoTableInfo *msemt = &class->image->tables [MONO_TABLE_METHODSEMANTICS];
421 class->event.first = mono_metadata_events_from_typedef (class->image, mono_metadata_token_index (class->type_token) - 1, &class->event.last);
422 class->event.count = class->event.last - class->event.first;
424 class->events = g_new0 (MonoEvent, class->event.count);
425 for (i = class->event.first; i < class->event.last; ++i) {
426 mono_metadata_decode_row (pt, i, cols, MONO_EVENT_SIZE);
427 class->events [i - class->event.first].attrs = cols [MONO_EVENT_FLAGS];
428 class->events [i - class->event.first].name = mono_metadata_string_heap (class->image, cols [MONO_EVENT_NAME]);
430 startm = mono_metadata_methods_from_event (class->image, i, &endm);
431 for (j = startm; j < endm; ++j) {
432 mono_metadata_decode_row (msemt, j, cols, MONO_METHOD_SEMA_SIZE);
433 switch (cols [MONO_METHOD_SEMA_SEMANTICS]) {
434 case METHOD_SEMANTIC_ADD_ON:
435 class->events [i - class->event.first].add = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
437 case METHOD_SEMANTIC_REMOVE_ON:
438 class->events [i - class->event.first].remove = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
440 case METHOD_SEMANTIC_FIRE:
441 class->events [i - class->event.first].raise = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
443 case METHOD_SEMANTIC_OTHER: /* don't care for now */
452 mono_get_unique_iid (MonoClass *class)
454 static GHashTable *iid_hash = NULL;
455 static guint iid = 0;
460 g_assert (class->flags & TYPE_ATTRIBUTE_INTERFACE);
463 iid_hash = g_hash_table_new (g_str_hash, g_str_equal);
465 str = g_strdup_printf ("%s|%s.%s\n", class->image->name, class->name_space, class->name);
467 if (g_hash_table_lookup_extended (iid_hash, str, NULL, &value)) {
471 g_hash_table_insert (iid_hash, str, (gpointer)iid);
479 collect_implemented_interfaces_aux (MonoClass *klass, GPtrArray *res)
484 for (i = 0; i < klass->interface_count; i++) {
485 ic = klass->interfaces [i];
487 g_ptr_array_add (res, ic);
489 collect_implemented_interfaces_aux (ic, res);
494 collect_implemented_interfaces (MonoClass *klass)
496 GPtrArray *res = g_ptr_array_new ();
498 collect_implemented_interfaces_aux (klass, res);
503 setup_interface_offsets (MonoClass *class, int cur_slot)
509 /* compute maximum number of slots and maximum interface id */
511 for (k = class; k ; k = k->parent) {
512 for (i = 0; i < k->interface_count; i++) {
513 ic = k->interfaces [i];
516 mono_class_init (ic);
518 if (max_iid < ic->interface_id)
519 max_iid = ic->interface_id;
523 if (class->flags & TYPE_ATTRIBUTE_INTERFACE) {
524 if (max_iid < class->interface_id)
525 max_iid = class->interface_id;
527 class->max_interface_id = max_iid;
528 /* compute vtable offset for interfaces */
529 class->interface_offsets = g_malloc (sizeof (gpointer) * (max_iid + 1));
531 for (i = 0; i <= max_iid; i++)
532 class->interface_offsets [i] = -1;
534 ifaces = collect_implemented_interfaces (class);
535 for (i = 0; i < ifaces->len; ++i) {
536 ic = g_ptr_array_index (ifaces, i);
537 class->interface_offsets [ic->interface_id] = cur_slot;
538 cur_slot += ic->method.count;
540 g_ptr_array_free (ifaces, TRUE);
542 for (k = class->parent; k ; k = k->parent) {
543 ifaces = collect_implemented_interfaces (k);
544 for (i = 0; i < ifaces->len; ++i) {
545 ic = g_ptr_array_index (ifaces, i);
547 if (class->interface_offsets [ic->interface_id] == -1) {
548 int io = k->interface_offsets [ic->interface_id];
552 class->interface_offsets [ic->interface_id] = io;
555 g_ptr_array_free (ifaces, TRUE);
562 mono_class_setup_vtable (MonoClass *class, MonoMethod **overrides, int onum)
566 int i, max_vtsize = 0, max_iid, cur_slot = 0;
568 MonoGHashTable *override_map;
570 /* setup_vtable() must be called only once on the type */
571 if (class->interface_offsets) {
572 g_warning ("vtable already computed in %s.%s", class->name_space, class->name);
576 ifaces = collect_implemented_interfaces (class);
577 for (i = 0; i < ifaces->len; i++) {
578 MonoClass *ic = g_ptr_array_index (ifaces, i);
579 max_vtsize += ic->method.count;
581 g_ptr_array_free (ifaces, TRUE);
584 max_vtsize += class->parent->vtable_size;
585 cur_slot = class->parent->vtable_size;
588 max_vtsize += class->method.count;
590 vtable = alloca (sizeof (gpointer) * max_vtsize);
591 memset (vtable, 0, sizeof (gpointer) * max_vtsize);
593 /* printf ("METAINIT %s.%s\n", class->name_space, class->name); */
595 cur_slot = setup_interface_offsets (class, cur_slot);
596 max_iid = class->max_interface_id;
598 if (class->parent && class->parent->vtable_size)
599 memcpy (vtable, class->parent->vtable, sizeof (gpointer) * class->parent->vtable_size);
601 override_map = mono_g_hash_table_new (NULL, NULL);
603 /* override interface methods */
604 for (i = 0; i < onum; i++) {
605 MonoMethod *decl = overrides [i*2];
606 if (decl->klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
608 g_assert (decl->slot != -1);
609 dslot = decl->slot + class->interface_offsets [decl->klass->interface_id];
610 vtable [dslot] = overrides [i*2 + 1];
611 vtable [dslot]->slot = dslot;
612 mono_g_hash_table_insert (override_map, overrides [i * 2], overrides [i * 2 + 1]);
616 for (k = class; k ; k = k->parent) {
617 ifaces = collect_implemented_interfaces (k);
618 for (i = 0; i < ifaces->len; i++) {
621 ic = g_ptr_array_index (ifaces, i);
622 io = k->interface_offsets [ic->interface_id];
625 g_assert (io <= max_vtsize);
628 for (l = 0; l < ic->method.count; l++) {
629 MonoMethod *im = ic->methods [l];
631 if (vtable [io + l] && !(vtable [io + l]->flags & METHOD_ATTRIBUTE_ABSTRACT))
634 for (j = 0; j < class->method.count; ++j) {
635 MonoMethod *cm = class->methods [j];
636 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
637 !((cm->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == METHOD_ATTRIBUTE_PUBLIC) ||
638 !(cm->flags & METHOD_ATTRIBUTE_NEW_SLOT))
640 if (!strcmp(cm->name, im->name) &&
641 mono_metadata_signature_equal (cm->signature, im->signature)) {
642 g_assert (io + l <= max_vtsize);
643 vtable [io + l] = cm;
648 /* already implemented */
649 if (io >= k->vtable_size)
653 for (l = 0; l < ic->method.count; l++) {
654 MonoMethod *im = ic->methods [l];
657 g_assert (io + l <= max_vtsize);
659 if (vtable [io + l] && !(vtable [io + l]->flags & METHOD_ATTRIBUTE_ABSTRACT))
662 for (k1 = class; k1; k1 = k1->parent) {
663 for (j = 0; j < k1->method.count; ++j) {
664 MonoMethod *cm = k1->methods [j];
666 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
667 !(cm->flags & METHOD_ATTRIBUTE_PUBLIC))
670 if (!strcmp(cm->name, im->name) &&
671 mono_metadata_signature_equal (cm->signature, im->signature)) {
672 g_assert (io + l <= max_vtsize);
673 vtable [io + l] = cm;
678 g_assert (io + l <= max_vtsize);
679 if (vtable [io + l] && !(vtable [io + l]->flags & METHOD_ATTRIBUTE_ABSTRACT))
684 for (l = 0; l < ic->method.count; l++) {
685 MonoMethod *im = ic->methods [l];
686 char *qname, *fqname;
692 qname = g_strconcat (ic->name, ".", im->name, NULL);
693 if (ic->name_space && ic->name_space [0])
694 fqname = g_strconcat (ic->name_space, ".", ic->name, ".", im->name, NULL);
698 for (k1 = class; k1; k1 = k1->parent) {
699 for (j = 0; j < k1->method.count; ++j) {
700 MonoMethod *cm = k1->methods [j];
702 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL))
705 if (((fqname && !strcmp (cm->name, fqname)) || !strcmp (cm->name, qname)) &&
706 mono_metadata_signature_equal (cm->signature, im->signature)) {
707 g_assert (io + l <= max_vtsize);
708 vtable [io + l] = cm;
718 if (!(class->flags & TYPE_ATTRIBUTE_ABSTRACT)) {
719 for (l = 0; l < ic->method.count; l++) {
721 MonoMethod *im = ic->methods [l];
722 if (im->flags & METHOD_ATTRIBUTE_STATIC)
724 g_assert (io + l <= max_vtsize);
725 if (!(vtable [io + l])) {
726 for (j = 0; j < onum; ++j) {
727 g_print (" at slot %d: %s (%d) overrides %s (%d)\n", io+l, overrides [j*2+1]->name,
728 overrides [j*2+1]->slot, overrides [j*2]->name, overrides [j*2]->slot);
730 msig = mono_signature_get_desc (im->signature, FALSE);
731 printf ("no implementation for interface method %s.%s::%s(%s) in class %s.%s\n",
732 ic->name_space, ic->name, im->name, msig, class->name_space, class->name);
734 for (j = 0; j < class->method.count; ++j) {
735 MonoMethod *cm = class->methods [j];
736 msig = mono_signature_get_desc (cm->signature, FALSE);
738 printf ("METHOD %s(%s)\n", cm->name, msig);
741 g_assert_not_reached ();
746 for (l = 0; l < ic->method.count; l++) {
747 MonoMethod *im = vtable [io + l];
750 g_assert (io + l <= max_vtsize);
752 /* FIXME: why do we need this ? */
754 /* g_assert_not_reached (); */
759 g_ptr_array_free (ifaces, TRUE);
762 for (i = 0; i < class->method.count; ++i) {
765 cm = class->methods [i];
767 if (!(cm->flags & METHOD_ATTRIBUTE_NEW_SLOT) && (cm->flags & METHOD_ATTRIBUTE_VIRTUAL)) {
769 for (k = class->parent; k ; k = k->parent) {
771 for (j = 0; j < k->method.count; ++j) {
772 MonoMethod *m1 = k->methods [j];
773 if (!(m1->flags & METHOD_ATTRIBUTE_VIRTUAL))
775 if (!strcmp(cm->name, m1->name) &&
776 mono_metadata_signature_equal (cm->signature, m1->signature)) {
777 slot = k->methods [j]->slot;
778 g_assert (cm->slot < max_vtsize);
779 mono_g_hash_table_insert (override_map, m1, cm);
791 cm->slot = cur_slot++;
793 if (!(cm->flags & METHOD_ATTRIBUTE_ABSTRACT))
794 vtable [cm->slot] = cm;
797 /* override non interface methods */
798 for (i = 0; i < onum; i++) {
799 MonoMethod *decl = overrides [i*2];
800 if (!(decl->klass->flags & TYPE_ATTRIBUTE_INTERFACE)) {
801 g_assert (decl->slot != -1);
802 vtable [decl->slot] = overrides [i*2 + 1];
803 overrides [i * 2 + 1]->slot = decl->slot;
804 mono_g_hash_table_insert (override_map, decl, overrides [i * 2 + 1]);
809 * If a method occupies more than one place in the vtable, and it is
810 * overriden, then change the other occurances too.
812 for (i = 0; i < max_vtsize; ++i)
814 MonoMethod *cm = mono_g_hash_table_lookup (override_map, vtable [i]);
818 mono_g_hash_table_destroy (override_map);
820 class->vtable_size = cur_slot;
821 class->vtable = g_malloc0 (sizeof (gpointer) * class->vtable_size);
822 memcpy (class->vtable, vtable, sizeof (gpointer) * class->vtable_size);
824 if (mono_print_vtable) {
827 for (i = 0; i <= max_iid; i++)
828 if (class->interface_offsets [i] != -1)
831 printf ("VTable %s.%s (size = %d, interfaces = %d)\n", class->name_space,
832 class->name, class->vtable_size, icount);
834 for (i = 0; i < class->vtable_size; ++i) {
839 printf (" slot %03d(%03d) %s.%s:%s\n", i, cm->slot,
840 cm->klass->name_space, cm->klass->name,
847 printf ("Interfaces %s.%s (max_iid = %d)\n", class->name_space,
848 class->name, max_iid);
850 for (i = 0; i < class->interface_count; i++) {
851 ic = class->interfaces [i];
852 printf (" slot %03d(%03d) %s.%s\n",
853 class->interface_offsets [ic->interface_id],
854 ic->method.count, ic->name_space, ic->name);
857 for (k = class->parent; k ; k = k->parent) {
858 for (i = 0; i < k->interface_count; i++) {
859 ic = k->interfaces [i];
860 printf (" slot %03d(%03d) %s.%s\n",
861 class->interface_offsets [ic->interface_id],
862 ic->method.count, ic->name_space, ic->name);
871 * @class: the class to initialize
873 * compute the instance_size, class_size and other infos that cannot be
874 * computed at mono_class_get() time. Also compute a generic vtable and
875 * the method slot numbers. We use this infos later to create a domain
879 mono_class_init (MonoClass *class)
882 static MonoMethod *default_ghc = NULL;
883 static MonoMethod *default_finalize = NULL;
884 static int finalize_slot = -1;
885 static int ghc_slot = -1;
886 MonoMethod **overrides;
894 if (class->init_pending) {
895 /* this indicates a cyclic dependency */
896 g_error ("pending init %s.%s\n", class->name_space, class->name);
899 class->init_pending = 1;
901 mono_stats.initialized_class_count++;
903 if (class->parent && !class->parent->inited)
904 mono_class_init (class->parent);
907 * Computes the size used by the fields, and their locations
909 if (!class->size_inited)
910 class_compute_field_layout (class);
912 /* initialize method pointers */
915 MonoMethodSignature *sig;
916 class->method.count = class->rank > 1? 2: 1;
917 sig = mono_metadata_signature_alloc (class->image, class->rank);
918 sig->ret = &mono_defaults.void_class->byval_arg;
920 for (i = 0; i < class->rank; ++i)
921 sig->params [i] = &mono_defaults.int32_class->byval_arg;
923 ctor = (MonoMethod *) g_new0 (MonoMethodPInvoke, 1);
925 ctor->flags = METHOD_ATTRIBUTE_PUBLIC | METHOD_ATTRIBUTE_RT_SPECIAL_NAME | METHOD_ATTRIBUTE_SPECIAL_NAME;
926 ctor->iflags = METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL;
927 ctor->signature = sig;
928 ctor->name = ".ctor";
929 class->methods = g_new (MonoMethod*, class->method.count);
930 class->methods [0] = ctor;
931 if (class->rank > 1) {
932 sig = mono_metadata_signature_alloc (class->image, class->rank * 2);
933 sig->ret = &mono_defaults.void_class->byval_arg;
935 for (i = 0; i < class->rank * 2; ++i)
936 sig->params [i] = &mono_defaults.int32_class->byval_arg;
938 ctor = (MonoMethod *) g_new0 (MonoMethodPInvoke, 1);
940 ctor->flags = METHOD_ATTRIBUTE_PUBLIC | METHOD_ATTRIBUTE_RT_SPECIAL_NAME | METHOD_ATTRIBUTE_SPECIAL_NAME;
941 ctor->iflags = METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL;
942 ctor->signature = sig;
943 ctor->name = ".ctor";
944 class->methods [1] = ctor;
947 class->methods = g_new (MonoMethod*, class->method.count);
948 for (i = 0; i < class->method.count; ++i) {
949 class->methods [i] = mono_get_method (class->image,
950 MONO_TOKEN_METHOD_DEF | (i + class->method.first + 1), class);
954 init_properties (class);
957 i = mono_metadata_nesting_typedef (class->image, class->type_token, 1);
960 guint32 cols [MONO_NESTED_CLASS_SIZE];
961 mono_metadata_decode_row (&class->image->tables [MONO_TABLE_NESTEDCLASS], i - 1, cols, MONO_NESTED_CLASS_SIZE);
962 nclass = mono_class_create_from_typedef (class->image, MONO_TOKEN_TYPE_DEF | cols [MONO_NESTED_CLASS_NESTED]);
963 class->nested_classes = g_list_prepend (class->nested_classes, nclass);
965 i = mono_metadata_nesting_typedef (class->image, class->type_token, i + 1);
968 mono_class_setup_supertypes (class);
970 if (class->flags & TYPE_ATTRIBUTE_INTERFACE) {
971 for (i = 0; i < class->method.count; ++i)
972 class->methods [i]->slot = i;
973 class->init_pending = 0;
976 * class->interface_offsets is needed for the castclass/isinst code, so
977 * we have to setup them for interfaces, too.
979 setup_interface_offsets (class, 0);
983 overrides = mono_class_get_overrides (class->image, class->type_token, &onum);
984 mono_class_setup_vtable (class, overrides, onum);
988 class->init_pending = 0;
991 if (class == mono_defaults.object_class) {
993 for (i = 0; i < class->vtable_size; ++i) {
994 MonoMethod *cm = class->vtable [i];
996 if (!strcmp (cm->name, "GetHashCode")) {
1002 g_assert (ghc_slot > 0);
1004 default_ghc = class->vtable [ghc_slot];
1009 if (class->parent) {
1011 if (class->vtable [ghc_slot] == default_ghc) {
1016 if (!default_finalize) {
1017 if (class == mono_defaults.object_class) {
1019 for (i = 0; i < class->vtable_size; ++i) {
1020 MonoMethod *cm = class->vtable [i];
1022 if (!strcmp (cm->name, "Finalize")) {
1028 g_assert (finalize_slot > 0);
1030 default_finalize = class->vtable [finalize_slot];
1034 /* Object::Finalize should have empty implemenatation */
1035 class->has_finalize = 0;
1036 if (class->parent) {
1037 if (class->vtable [finalize_slot] != default_finalize)
1038 class->has_finalize = 1;
1041 if (mono_debugger_class_init_func)
1042 mono_debugger_class_init_func (class);
1047 mono_class_setup_mono_type (MonoClass *class)
1049 const char *name = class->name;
1050 const char *nspace = class->name_space;
1052 if (class->flags & TYPE_ATTRIBUTE_INTERFACE)
1053 class->interface_id = mono_get_unique_iid (class);
1055 class->this_arg.byref = 1;
1056 class->this_arg.data.klass = class;
1057 class->this_arg.type = MONO_TYPE_CLASS;
1058 class->byval_arg.data.klass = class;
1059 class->byval_arg.type = MONO_TYPE_CLASS;
1061 if (!strcmp (nspace, "System")) {
1062 if (!strcmp (name, "ValueType")) {
1064 * do not set the valuetype bit for System.ValueType.
1065 * class->valuetype = 1;
1067 } else if (!strcmp (name, "Enum")) {
1069 * do not set the valuetype bit for System.Enum.
1070 * class->valuetype = 1;
1072 class->valuetype = 0;
1073 class->enumtype = 0;
1074 } else if (!strcmp (name, "Object")) {
1075 class->this_arg.type = class->byval_arg.type = MONO_TYPE_OBJECT;
1076 } else if (!strcmp (name, "String")) {
1077 class->this_arg.type = class->byval_arg.type = MONO_TYPE_STRING;
1078 } else if (!strcmp (name, "TypedReference")) {
1079 class->this_arg.type = class->byval_arg.type = MONO_TYPE_TYPEDBYREF;
1083 if (class->valuetype) {
1084 int t = MONO_TYPE_VALUETYPE;
1085 if (!strcmp (nspace, "System")) {
1088 if (!strcmp (name, "Boolean")) {
1089 t = MONO_TYPE_BOOLEAN;
1090 } else if (!strcmp(name, "Byte")) {
1092 class->blittable = TRUE;
1096 if (!strcmp (name, "Char")) {
1101 if (!strcmp (name, "Double")) {
1103 class->blittable = TRUE;
1107 if (!strcmp (name, "Int32")) {
1109 class->blittable = TRUE;
1110 } else if (!strcmp(name, "Int16")) {
1112 class->blittable = TRUE;
1113 } else if (!strcmp(name, "Int64")) {
1115 class->blittable = TRUE;
1116 } else if (!strcmp(name, "IntPtr")) {
1118 class->blittable = TRUE;
1122 if (!strcmp (name, "Single")) {
1124 class->blittable = TRUE;
1125 } else if (!strcmp(name, "SByte")) {
1127 class->blittable = TRUE;
1131 if (!strcmp (name, "UInt32")) {
1133 class->blittable = TRUE;
1134 } else if (!strcmp(name, "UInt16")) {
1136 class->blittable = TRUE;
1137 } else if (!strcmp(name, "UInt64")) {
1139 class->blittable = TRUE;
1140 } else if (!strcmp(name, "UIntPtr")) {
1142 class->blittable = TRUE;
1146 if (!strcmp (name, "TypedReference")) {
1147 t = MONO_TYPE_TYPEDBYREF;
1148 class->blittable = TRUE;
1152 if (!strcmp (name, "Void")) {
1160 class->this_arg.type = class->byval_arg.type = t;
1165 mono_class_setup_parent (MonoClass *class, MonoClass *parent)
1167 gboolean system_namespace;
1169 system_namespace = !strcmp (class->name_space, "System");
1171 /* if root of the hierarchy */
1172 if (system_namespace && !strcmp (class->name, "Object")) {
1173 class->parent = NULL;
1174 class->instance_size = sizeof (MonoObject);
1177 if (!strcmp (class->name, "<Module>")) {
1178 class->parent = NULL;
1179 class->instance_size = 0;
1183 if (!(class->flags & TYPE_ATTRIBUTE_INTERFACE)) {
1184 class->parent = parent;
1187 g_assert_not_reached (); /* FIXME */
1189 class->marshalbyref = parent->marshalbyref;
1190 class->contextbound = parent->contextbound;
1191 class->delegate = parent->delegate;
1193 if (system_namespace) {
1194 if (*class->name == 'M' && !strcmp (class->name, "MarshalByRefObject"))
1195 class->marshalbyref = 1;
1197 if (*class->name == 'C' && !strcmp (class->name, "ContextBoundObject"))
1198 class->contextbound = 1;
1200 if (*class->name == 'D' && !strcmp (class->name, "Delegate"))
1201 class->delegate = 1;
1204 if (class->parent->enumtype || ((strcmp (class->parent->name, "ValueType") == 0) &&
1205 (strcmp (class->parent->name_space, "System") == 0)))
1206 class->valuetype = 1;
1207 if (((strcmp (class->parent->name, "Enum") == 0) && (strcmp (class->parent->name_space, "System") == 0))) {
1208 class->valuetype = class->enumtype = 1;
1210 /*class->enumtype = class->parent->enumtype; */
1211 class->parent->subclasses = g_list_prepend (class->parent->subclasses, class);
1212 mono_class_setup_supertypes (class);
1214 class->parent = NULL;
1220 mono_class_setup_supertypes (MonoClass *class)
1225 if (class->supertypes)
1229 for (k = class; k ; k = k->parent) {
1233 ms = MAX (MONO_DEFAULT_SUPERTABLE_SIZE, class->idepth);
1234 class->supertypes = g_new0 (MonoClass *, ms);
1236 if (class->parent) {
1237 for (i = class->idepth, k = class; k ; k = k->parent)
1238 class->supertypes [--i] = k;
1240 class->supertypes [0] = class;
1245 * @image: context where the image is created
1246 * @type_token: typedef token
1249 mono_class_create_from_typedef (MonoImage *image, guint32 type_token)
1251 MonoTableInfo *tt = &image->tables [MONO_TABLE_TYPEDEF];
1252 MonoClass *class, *parent = NULL;
1253 guint32 cols [MONO_TYPEDEF_SIZE];
1254 guint32 cols_next [MONO_TYPEDEF_SIZE];
1255 guint tidx = mono_metadata_token_index (type_token);
1256 const char *name, *nspace;
1258 MonoClass **interfaces;
1260 if ((class = g_hash_table_lookup (image->class_cache, GUINT_TO_POINTER (type_token))))
1263 g_assert (mono_metadata_token_table (type_token) == MONO_TABLE_TYPEDEF);
1265 mono_metadata_decode_row (tt, tidx - 1, cols, MONO_TYPEDEF_SIZE);
1267 name = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
1268 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
1270 if (cols [MONO_TYPEDEF_EXTENDS])
1271 parent = mono_class_get (image, mono_metadata_token_from_dor (cols [MONO_TYPEDEF_EXTENDS]));
1272 interfaces = mono_metadata_interfaces_from_typedef (image, type_token, &icount);
1274 class = g_malloc0 (sizeof (MonoClass));
1276 g_hash_table_insert (image->class_cache, GUINT_TO_POINTER (type_token), class);
1278 class->interfaces = interfaces;
1279 class->interface_count = icount;
1282 class->name_space = nspace;
1284 class->image = image;
1285 class->type_token = type_token;
1286 class->flags = cols [MONO_TYPEDEF_FLAGS];
1288 if ((class->flags & TYPE_ATTRIBUTE_STRING_FORMAT_MASK) == TYPE_ATTRIBUTE_UNICODE_CLASS)
1290 /* fixme: maybe we must set this on windows
1291 if ((class->flags & TYPE_ATTRIBUTE_STRING_FORMAT_MASK) == TYPE_ATTRIBUTE_AUTO_CLASS)
1295 class->cast_class = class->element_class = class;
1297 /*g_print ("Init class %s\n", name);*/
1299 mono_class_setup_parent (class, parent);
1301 mono_class_setup_mono_type (class);
1304 * Compute the field and method lists
1306 class->field.first = cols [MONO_TYPEDEF_FIELD_LIST] - 1;
1307 class->method.first = cols [MONO_TYPEDEF_METHOD_LIST] - 1;
1309 if (tt->rows > tidx){
1310 mono_metadata_decode_row (tt, tidx, cols_next, CSIZE (cols_next));
1311 class->field.last = cols_next [MONO_TYPEDEF_FIELD_LIST] - 1;
1312 class->method.last = cols_next [MONO_TYPEDEF_METHOD_LIST] - 1;
1314 class->field.last = image->tables [MONO_TABLE_FIELD].rows;
1315 class->method.last = image->tables [MONO_TABLE_METHOD].rows;
1318 if (cols [MONO_TYPEDEF_FIELD_LIST] &&
1319 cols [MONO_TYPEDEF_FIELD_LIST] <= image->tables [MONO_TABLE_FIELD].rows)
1320 class->field.count = class->field.last - class->field.first;
1322 class->field.count = 0;
1324 if (cols [MONO_TYPEDEF_METHOD_LIST] <= image->tables [MONO_TABLE_METHOD].rows)
1325 class->method.count = class->method.last - class->method.first;
1327 class->method.count = 0;
1329 /* reserve space to store vector pointer in arrays */
1330 if (!strcmp (nspace, "System") && !strcmp (name, "Array")) {
1331 class->instance_size += 2 * sizeof (gpointer);
1332 g_assert (class->field.count == 0);
1335 if (class->enumtype)
1336 class_compute_field_layout (class);
1338 if ((type_token = mono_metadata_nested_in_typedef (image, type_token)))
1339 class->nested_in = mono_class_create_from_typedef (image, type_token);
1345 mono_ptr_class_get (MonoType *type)
1348 MonoClass *el_class;
1349 static GHashTable *ptr_hash = NULL;
1352 ptr_hash = g_hash_table_new (g_direct_hash, g_direct_equal);
1353 el_class = mono_class_from_mono_type (type);
1354 if ((result = g_hash_table_lookup (ptr_hash, el_class)))
1356 result = g_new0 (MonoClass, 1);
1358 result->parent = NULL; /* no parent for PTR types */
1359 result->name = "System";
1360 result->name_space = "MonoPtrFakeClass";
1361 result->image = el_class->image;
1362 result->inited = TRUE;
1363 result->flags = TYPE_ATTRIBUTE_CLASS | (el_class->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK);
1364 /* Can pointers get boxed? */
1365 result->instance_size = sizeof (gpointer);
1366 result->cast_class = result->element_class = el_class;
1367 result->enum_basetype = &result->element_class->byval_arg;
1369 result->this_arg.type = result->byval_arg.type = MONO_TYPE_PTR;
1370 result->this_arg.data.type = result->byval_arg.data.type = result->enum_basetype;
1371 result->this_arg.byref = TRUE;
1373 mono_class_setup_supertypes (result);
1375 g_hash_table_insert (ptr_hash, el_class, result);
1381 mono_fnptr_class_get (MonoMethodSignature *sig)
1384 static GHashTable *ptr_hash = NULL;
1387 ptr_hash = g_hash_table_new (g_direct_hash, g_direct_equal);
1389 if ((result = g_hash_table_lookup (ptr_hash, sig)))
1391 result = g_new0 (MonoClass, 1);
1393 result->parent = NULL; /* no parent for PTR types */
1394 result->name = "System";
1395 result->name_space = "MonoFNPtrFakeClass";
1396 result->image = NULL; /* need to fix... */
1397 result->inited = TRUE;
1398 result->flags = TYPE_ATTRIBUTE_CLASS; // | (el_class->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK);
1399 /* Can pointers get boxed? */
1400 result->instance_size = sizeof (gpointer);
1401 result->cast_class = result->element_class = result;
1403 result->this_arg.type = result->byval_arg.type = MONO_TYPE_FNPTR;
1404 result->this_arg.data.method = result->byval_arg.data.method = sig;
1405 result->this_arg.byref = TRUE;
1406 result->enum_basetype = &result->element_class->byval_arg;
1408 mono_class_setup_supertypes (result);
1410 g_hash_table_insert (ptr_hash, sig, result);
1416 mono_class_from_mono_type (MonoType *type)
1418 switch (type->type) {
1419 case MONO_TYPE_OBJECT:
1420 return mono_defaults.object_class;
1421 case MONO_TYPE_VOID:
1422 return mono_defaults.void_class;
1423 case MONO_TYPE_BOOLEAN:
1424 return mono_defaults.boolean_class;
1425 case MONO_TYPE_CHAR:
1426 return mono_defaults.char_class;
1428 return mono_defaults.sbyte_class;
1430 return mono_defaults.byte_class;
1432 return mono_defaults.int16_class;
1434 return mono_defaults.uint16_class;
1436 return mono_defaults.int32_class;
1438 return mono_defaults.uint32_class;
1440 return mono_defaults.int_class;
1442 return mono_defaults.uint_class;
1444 return mono_defaults.int64_class;
1446 return mono_defaults.uint64_class;
1448 return mono_defaults.single_class;
1450 return mono_defaults.double_class;
1451 case MONO_TYPE_STRING:
1452 return mono_defaults.string_class;
1453 case MONO_TYPE_TYPEDBYREF:
1454 return mono_defaults.typed_reference_class;
1455 case MONO_TYPE_ARRAY:
1456 return mono_array_class_get (type->data.array->eklass, type->data.array->rank);
1458 return mono_ptr_class_get (type->data.type);
1459 case MONO_TYPE_FNPTR:
1460 return mono_fnptr_class_get (type->data.method);
1461 case MONO_TYPE_SZARRAY:
1462 return mono_array_class_get (type->data.klass, 1);
1463 case MONO_TYPE_CLASS:
1464 case MONO_TYPE_VALUETYPE:
1465 return type->data.klass;
1467 case MONO_TYPE_GENERICINST:
1468 g_warning ("mono_class_from_type: implement me MONO_TYPE_GENERICINST");
1469 g_assert_not_reached ();
1472 g_warning ("mono_class_from_type: implement me MONO_TYPE_VAR");
1473 g_assert_not_reached ();
1475 case MONO_TYPE_MVAR:
1476 g_warning ("mono_class_from_type: implement me MONO_TYPE_MVAR");
1477 g_assert_not_reached ();
1480 g_warning ("implement me 0x%02x\n", type->type);
1481 g_assert_not_reached ();
1488 * @image: context where the image is created
1489 * @type_spec: typespec token
1492 mono_class_create_from_typespec (MonoImage *image, guint32 type_spec)
1497 type = mono_type_create_from_typespec (image, type_spec);
1499 switch (type->type) {
1500 case MONO_TYPE_ARRAY:
1501 class = mono_array_class_get (type->data.array->eklass, type->data.array->rank);
1503 case MONO_TYPE_SZARRAY:
1504 class = mono_array_class_get (type->data.klass, 1);
1507 class = mono_class_from_mono_type (type->data.type);
1510 /* it seems any type can be stored in TypeSpec as well */
1511 class = mono_class_from_mono_type (type);
1515 mono_metadata_free_type (type);
1521 * mono_array_class_get:
1522 * @element_class: element class
1523 * @rank: the dimension of the array class
1525 * Returns: a class object describing the array with element type @element_type and
1529 mono_array_class_get (MonoClass *eclass, guint32 rank)
1533 MonoClass *parent = NULL;
1534 GSList *list, *rootlist;
1538 g_assert (rank <= 255);
1540 parent = mono_defaults.array_class;
1542 if (!parent->inited)
1543 mono_class_init (parent);
1545 image = eclass->image;
1547 if ((rootlist = list = g_hash_table_lookup (image->array_cache, eclass))) {
1548 for (; list; list = list->next) {
1550 if (class->rank == rank)
1555 class = g_malloc0 (sizeof (MonoClass) + parent->vtable_size * sizeof (gpointer));
1557 class->image = image;
1558 class->name_space = eclass->name_space;
1559 nsize = strlen (eclass->name);
1560 name = g_malloc (nsize + 2 + rank);
1561 memcpy (name, eclass->name, nsize);
1564 memset (name + nsize + 1, ',', rank - 1);
1565 name [nsize + rank] = ']';
1566 name [nsize + rank + 1] = 0;
1568 class->type_token = 0;
1569 /* all arrays are marked serializable and sealed, bug #42779 */
1570 class->flags = TYPE_ATTRIBUTE_CLASS | TYPE_ATTRIBUTE_SERIALIZABLE | TYPE_ATTRIBUTE_SEALED |
1571 (eclass->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK);
1572 class->parent = parent;
1573 class->instance_size = mono_class_instance_size (class->parent);
1574 class->class_size = 0;
1575 class->vtable_size = parent->vtable_size;
1576 class->parent->subclasses = g_list_prepend (class->parent->subclasses, class);
1577 mono_class_setup_supertypes (class);
1581 if (eclass->enumtype)
1582 class->cast_class = eclass->element_class;
1584 class->cast_class = eclass;
1586 class->element_class = eclass;
1589 MonoArrayType *at = g_new0 (MonoArrayType, 1);
1590 class->byval_arg.type = MONO_TYPE_ARRAY;
1591 class->byval_arg.data.array = at;
1592 at->eklass = eclass;
1594 /* FIXME: complete.... */
1596 /* FIXME: this is not correct. the lbound could be >0 */
1597 class->byval_arg.type = MONO_TYPE_SZARRAY;
1598 class->byval_arg.data.klass = eclass;
1600 class->this_arg = class->byval_arg;
1601 class->this_arg.byref = 1;
1603 list = g_slist_append (rootlist, class);
1604 g_hash_table_insert (image->array_cache, eclass, list);
1609 * mono_class_instance_size:
1612 * Returns: the size of an object instance
1615 mono_class_instance_size (MonoClass *klass)
1618 if (!klass->size_inited)
1619 mono_class_init (klass);
1621 return klass->instance_size;
1625 * mono_class_min_align:
1628 * Returns: minimm alignment requirements
1631 mono_class_min_align (MonoClass *klass)
1634 if (!klass->size_inited)
1635 mono_class_init (klass);
1637 return klass->min_align;
1641 * mono_class_value_size:
1644 * This function is used for value types, and return the
1645 * space and the alignment to store that kind of value object.
1647 * Returns: the size of a value of kind @klass
1650 mono_class_value_size (MonoClass *klass, guint32 *align)
1654 /* fixme: check disable, because we still have external revereces to
1655 * mscorlib and Dummy Objects
1657 /*g_assert (klass->valuetype);*/
1659 size = mono_class_instance_size (klass) - sizeof (MonoObject);
1662 *align = klass->min_align;
1668 * mono_class_data_size:
1671 * Returns: the size of the static class data
1674 mono_class_data_size (MonoClass *klass)
1678 mono_class_init (klass);
1680 return klass->class_size;
1684 * Auxiliary routine to mono_class_get_field
1686 * Takes a field index instead of a field token.
1688 static MonoClassField *
1689 mono_class_get_field_idx (MonoClass *class, int idx)
1691 if (class->field.count){
1692 if ((idx >= class->field.first) && (idx < class->field.last)){
1693 return &class->fields [idx - class->field.first];
1700 return mono_class_get_field_idx (class->parent, idx);
1704 * mono_class_get_field:
1705 * @class: the class to lookup the field.
1706 * @field_token: the field token
1708 * Returns: A MonoClassField representing the type and offset of
1709 * the field, or a NULL value if the field does not belong to this
1713 mono_class_get_field (MonoClass *class, guint32 field_token)
1715 int idx = mono_metadata_token_index (field_token);
1717 g_assert (mono_metadata_token_code (field_token) == MONO_TOKEN_FIELD_DEF);
1719 return mono_class_get_field_idx (class, idx - 1);
1723 mono_class_get_field_from_name (MonoClass *klass, const char *name)
1728 for (i = 0; i < klass->field.count; ++i) {
1729 if (strcmp (name, klass->fields [i].name) == 0)
1730 return &klass->fields [i];
1732 klass = klass->parent;
1738 mono_class_get_property_from_name (MonoClass *klass, const char *name)
1743 for (i = 0; i < klass->property.count; ++i) {
1744 if (strcmp (name, klass->properties [i].name) == 0)
1745 return &klass->properties [i];
1747 klass = klass->parent;
1754 * @image: the image where the class resides
1755 * @type_token: the token for the class
1756 * @at: an optional pointer to return the array element type
1758 * Returns: the MonoClass that represents @type_token in @image
1761 mono_class_get (MonoImage *image, guint32 type_token)
1765 if (image->assembly->dynamic)
1766 return mono_lookup_dynamic_token (image, type_token);
1768 switch (type_token & 0xff000000){
1769 case MONO_TOKEN_TYPE_DEF:
1770 class = mono_class_create_from_typedef (image, type_token);
1772 case MONO_TOKEN_TYPE_REF:
1773 class = mono_class_from_typeref (image, type_token);
1775 case MONO_TOKEN_TYPE_SPEC:
1776 class = mono_class_create_from_typespec (image, type_token);
1779 g_warning ("unknown token type %x", type_token & 0xff000000);
1780 g_assert_not_reached ();
1784 g_warning ("Could not load class from token 0x%08x in %s", type_token, image->name);
1790 mono_class_from_name_case (MonoImage *image, const char* name_space, const char *name)
1792 MonoTableInfo *t = &image->tables [MONO_TABLE_TYPEDEF];
1793 guint32 cols [MONO_TYPEDEF_SIZE];
1798 /* add a cache if needed */
1799 for (i = 1; i <= t->rows; ++i) {
1800 mono_metadata_decode_row (t, i - 1, cols, MONO_TYPEDEF_SIZE);
1801 /* nested types are accessed from the nesting name */
1802 visib = cols [MONO_TYPEDEF_FLAGS] & TYPE_ATTRIBUTE_VISIBILITY_MASK;
1803 if (visib > TYPE_ATTRIBUTE_PUBLIC && visib <= TYPE_ATTRIBUTE_NESTED_ASSEMBLY)
1805 n = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
1806 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
1807 if (g_strcasecmp (n, name) == 0 && g_strcasecmp (nspace, name_space) == 0)
1808 return mono_class_get (image, MONO_TOKEN_TYPE_DEF | i);
1814 load_file_for_image (MonoImage *image, int fileidx)
1816 char *base_dir, *name;
1818 MonoTableInfo *t = &image->tables [MONO_TABLE_FILE];
1822 if (fileidx < 1 || fileidx > t->rows)
1824 fname_id = mono_metadata_decode_row_col (t, fileidx - 1, MONO_FILE_NAME);
1825 fname = mono_metadata_string_heap (image, fname_id);
1826 base_dir = g_path_get_dirname (image->name);
1827 name = g_build_filename (base_dir, fname, NULL);
1828 res = mono_image_open (name, NULL);
1831 t = &res->tables [MONO_TABLE_MODULEREF];
1832 //g_print ("loaded file %s from %s (%p)\n", name, image->name, image->assembly);
1833 res->assembly = image->assembly;
1834 for (i = 0; i < t->rows; ++i) {
1835 if (res->modules [i] && !res->modules [i]->assembly)
1836 res->modules [i]->assembly = image->assembly;
1838 mono_image_load_references (image, NULL);
1846 return_nested_in (MonoClass *class, char *nested) {
1848 char *s = strchr (nested, '/');
1855 for (tmp = class->nested_classes; tmp; tmp = tmp->next) {
1857 if (strcmp (found->name, nested) == 0) {
1859 return return_nested_in (found, s);
1867 mono_class_from_name (MonoImage *image, const char* name_space, const char *name)
1869 GHashTable *nspace_table;
1870 MonoImage *loaded_image;
1876 if ((nested = strchr (name, '/'))) {
1877 int pos = nested - name;
1878 int len = strlen (name);
1881 memcpy (buf, name, len + 1);
1883 nested = buf + pos + 1;
1887 nspace_table = g_hash_table_lookup (image->name_cache, name_space);
1889 if (!nspace_table || !(token = GPOINTER_TO_UINT (g_hash_table_lookup (nspace_table, name)))) {
1890 MonoTableInfo *t = &image->tables [MONO_TABLE_EXPORTEDTYPE];
1891 guint32 cols [MONO_EXP_TYPE_SIZE];
1894 for (i = 0; i < t->rows; ++i) {
1895 const char *ename, *enspace;
1896 mono_metadata_decode_row (t, i, cols, MONO_EXP_TYPE_SIZE);
1897 ename = mono_metadata_string_heap (image, cols [MONO_EXP_TYPE_NAME]);
1898 enspace = mono_metadata_string_heap (image, cols [MONO_EXP_TYPE_NAMESPACE]);
1900 if (strcmp (name, ename) == 0 && strcmp (name_space, enspace) == 0) {
1901 guint32 impl = cols [MONO_EXP_TYPE_IMPLEMENTATION];
1902 if ((impl & IMPLEMENTATION_MASK) == IMPLEMENTATION_FILE) {
1903 loaded_image = load_file_for_image (image, impl >> IMPLEMENTATION_BITS);
1906 class = mono_class_from_name (loaded_image, name_space, name);
1908 return return_nested_in (class, nested);
1911 g_error ("not yet implemented");
1915 /*g_warning ("token not found for %s.%s in image %s", name_space, name, image->name);*/
1919 token = MONO_TOKEN_TYPE_DEF | token;
1921 class = mono_class_get (image, token);
1923 return return_nested_in (class, nested);
1928 mono_class_is_subclass_of (MonoClass *klass, MonoClass *klassc,
1929 gboolean check_interfaces)
1931 if (check_interfaces && (klassc->flags & TYPE_ATTRIBUTE_INTERFACE) && !(klass->flags & TYPE_ATTRIBUTE_INTERFACE)) {
1932 if ((klassc->interface_id <= klass->max_interface_id) &&
1933 (klass->interface_offsets [klassc->interface_id] >= 0))
1935 } else if (check_interfaces && (klassc->flags & TYPE_ATTRIBUTE_INTERFACE) && (klass->flags & TYPE_ATTRIBUTE_INTERFACE)) {
1938 for (i = 0; i < klass->interface_count; i ++) {
1939 MonoClass *ic = klass->interfaces [i];
1944 if (!(klass->flags & TYPE_ATTRIBUTE_INTERFACE) && mono_class_has_parent (klass, klassc))
1949 * MS.NET thinks interfaces are a subclass of Object, so we think it as
1952 if (klassc == mono_defaults.object_class)
1959 mono_class_is_assignable_from (MonoClass *klass, MonoClass *oklass)
1962 mono_class_init (klass);
1964 if (!oklass->inited)
1965 mono_class_init (oklass);
1967 if (klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
1968 if ((klass->interface_id <= oklass->max_interface_id) &&
1969 (oklass->interface_offsets [klass->interface_id] != -1))
1973 MonoClass *eclass, *eoclass;
1975 if (oklass->rank != klass->rank)
1978 eclass = klass->cast_class;
1979 eoclass = oklass->cast_class;
1983 * a is b does not imply a[] is b[] when a is a valuetype, and
1984 * b is a reference type.
1987 if (eoclass->valuetype) {
1988 if ((eclass == mono_defaults.enum_class) ||
1989 (eclass == mono_defaults.enum_class->parent) ||
1990 (eclass == mono_defaults.object_class))
1994 return mono_class_is_assignable_from (klass->cast_class, oklass->cast_class);
1997 if (klass == mono_defaults.object_class)
2000 return mono_class_has_parent (oklass, klass);
2004 * Returns the nnumber of bytes an element of type klass
2005 * uses when stored into an array.
2008 mono_class_array_element_size (MonoClass *klass)
2010 int t = klass->byval_arg.type;
2016 case MONO_TYPE_BOOLEAN:
2020 case MONO_TYPE_CHAR:
2029 case MONO_TYPE_CLASS:
2030 case MONO_TYPE_STRING:
2031 case MONO_TYPE_OBJECT:
2032 case MONO_TYPE_SZARRAY:
2033 case MONO_TYPE_ARRAY:
2034 return sizeof (gpointer);
2039 case MONO_TYPE_VALUETYPE:
2040 if (klass->enumtype) {
2041 t = klass->enum_basetype->type;
2044 return mono_class_instance_size (klass) - sizeof (MonoObject);
2046 g_error ("unknown type 0x%02x in mono_class_array_element_size", t);
2052 * mono_array_element_size:
2053 * @ac: pointer to a #MonoArrayClass
2055 * Returns: the size of single array element.
2058 mono_array_element_size (MonoClass *ac)
2060 return mono_class_array_element_size (ac->element_class);
2064 mono_ldtoken (MonoImage *image, guint32 token, MonoClass **handle_class)
2066 if (image->assembly->dynamic) {
2067 gpointer obj = mono_lookup_dynamic_token (image, token);
2069 switch (token & 0xff000000) {
2070 case MONO_TOKEN_TYPE_DEF:
2071 case MONO_TOKEN_TYPE_REF:
2072 case MONO_TOKEN_TYPE_SPEC:
2074 *handle_class = mono_defaults.typehandle_class;
2075 return &((MonoClass*)obj)->byval_arg;
2076 case MONO_TOKEN_METHOD_DEF:
2078 *handle_class = mono_defaults.methodhandle_class;
2080 case MONO_TOKEN_FIELD_DEF:
2082 *handle_class = mono_defaults.fieldhandle_class;
2085 g_assert_not_reached ();
2089 switch (token & 0xff000000) {
2090 case MONO_TOKEN_TYPE_DEF:
2091 case MONO_TOKEN_TYPE_REF: {
2094 *handle_class = mono_defaults.typehandle_class;
2095 class = mono_class_get (image, token);
2096 mono_class_init (class);
2097 /* We return a MonoType* as handle */
2098 return &class->byval_arg;
2100 case MONO_TOKEN_TYPE_SPEC: {
2103 *handle_class = mono_defaults.typehandle_class;
2104 class = mono_class_create_from_typespec (image, token);
2105 mono_class_init (class);
2106 return &class->byval_arg;
2108 case MONO_TOKEN_FIELD_DEF: {
2110 guint32 type = mono_metadata_typedef_from_field (image, mono_metadata_token_index (token));
2111 class = mono_class_get (image, MONO_TOKEN_TYPE_DEF | type);
2112 mono_class_init (class);
2114 *handle_class = mono_defaults.fieldhandle_class;
2115 return mono_class_get_field (class, token);
2117 case MONO_TOKEN_METHOD_DEF:
2118 case MONO_TOKEN_MEMBER_REF:
2120 g_warning ("Unknown token 0x%08x in ldtoken", token);
2127 * This function might need to call runtime functions so it can't be part
2128 * of the metadata library.
2130 static MonoLookupDynamicToken lookup_dynamic = NULL;
2133 mono_install_lookup_dynamic_token (MonoLookupDynamicToken func)
2135 lookup_dynamic = func;
2139 mono_lookup_dynamic_token (MonoImage *image, guint32 token)
2141 return lookup_dynamic (image, token);