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 the runtime 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)
277 for (pass = 0; pass < passes; ++pass) {
279 real_size = class->parent->instance_size;
281 real_size = sizeof (MonoObject);
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:
329 for (i = 0; i < top; i++) {
333 * There must be info about all the fields in a type if it
334 * uses explicit layout.
337 if (class->fields [i].type->attrs & FIELD_ATTRIBUTE_STATIC)
340 size = mono_type_size (class->fields [i].type, &align);
343 * When we get here, class->fields [i].offset is already set by the
344 * loader (for either runtime fields or fields loaded from metadata).
345 * The offset is from the start of the object: this works for both
346 * classes and valuetypes.
348 class->fields [i].offset += sizeof (MonoObject);
353 real_size = MAX (real_size, size + class->fields [i].offset);
355 class->instance_size = MAX (real_size, class->instance_size);
359 class->size_inited = 1;
362 * Compute static field layout and size
364 for (i = 0; i < top; i++){
367 if (!(class->fields [i].type->attrs & FIELD_ATTRIBUTE_STATIC))
370 size = mono_type_size (class->fields [i].type, &align);
371 class->fields [i].offset = class->class_size;
372 class->fields [i].offset += align - 1;
373 class->fields [i].offset &= ~(align - 1);
374 class->class_size = class->fields [i].offset + size;
379 init_properties (MonoClass *class)
381 guint startm, endm, i, j;
382 guint32 cols [MONO_PROPERTY_SIZE];
383 MonoTableInfo *pt = &class->image->tables [MONO_TABLE_PROPERTY];
384 MonoTableInfo *msemt = &class->image->tables [MONO_TABLE_METHODSEMANTICS];
386 class->property.first = mono_metadata_properties_from_typedef (class->image, mono_metadata_token_index (class->type_token) - 1, &class->property.last);
387 class->property.count = class->property.last - class->property.first;
389 class->properties = g_new0 (MonoProperty, class->property.count);
390 for (i = class->property.first; i < class->property.last; ++i) {
391 mono_metadata_decode_row (pt, i, cols, MONO_PROPERTY_SIZE);
392 class->properties [i - class->property.first].attrs = cols [MONO_PROPERTY_FLAGS];
393 class->properties [i - class->property.first].name = mono_metadata_string_heap (class->image, cols [MONO_PROPERTY_NAME]);
395 startm = mono_metadata_methods_from_property (class->image, i, &endm);
396 for (j = startm; j < endm; ++j) {
397 mono_metadata_decode_row (msemt, j, cols, MONO_METHOD_SEMA_SIZE);
398 switch (cols [MONO_METHOD_SEMA_SEMANTICS]) {
399 case METHOD_SEMANTIC_SETTER:
400 class->properties [i - class->property.first].set = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
402 case METHOD_SEMANTIC_GETTER:
403 class->properties [i - class->property.first].get = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
413 init_events (MonoClass *class)
415 guint startm, endm, i, j;
416 guint32 cols [MONO_EVENT_SIZE];
417 MonoTableInfo *pt = &class->image->tables [MONO_TABLE_EVENT];
418 MonoTableInfo *msemt = &class->image->tables [MONO_TABLE_METHODSEMANTICS];
420 class->event.first = mono_metadata_events_from_typedef (class->image, mono_metadata_token_index (class->type_token) - 1, &class->event.last);
421 class->event.count = class->event.last - class->event.first;
423 class->events = g_new0 (MonoEvent, class->event.count);
424 for (i = class->event.first; i < class->event.last; ++i) {
425 mono_metadata_decode_row (pt, i, cols, MONO_EVENT_SIZE);
426 class->events [i - class->event.first].attrs = cols [MONO_EVENT_FLAGS];
427 class->events [i - class->event.first].name = mono_metadata_string_heap (class->image, cols [MONO_EVENT_NAME]);
429 startm = mono_metadata_methods_from_event (class->image, i, &endm);
430 for (j = startm; j < endm; ++j) {
431 mono_metadata_decode_row (msemt, j, cols, MONO_METHOD_SEMA_SIZE);
432 switch (cols [MONO_METHOD_SEMA_SEMANTICS]) {
433 case METHOD_SEMANTIC_ADD_ON:
434 class->events [i - class->event.first].add = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
436 case METHOD_SEMANTIC_REMOVE_ON:
437 class->events [i - class->event.first].remove = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
439 case METHOD_SEMANTIC_FIRE:
440 class->events [i - class->event.first].raise = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
442 case METHOD_SEMANTIC_OTHER: /* don't care for now */
451 mono_get_unique_iid (MonoClass *class)
453 static GHashTable *iid_hash = NULL;
454 static guint iid = 0;
459 g_assert (class->flags & TYPE_ATTRIBUTE_INTERFACE);
462 iid_hash = g_hash_table_new (g_str_hash, g_str_equal);
464 str = g_strdup_printf ("%s|%s.%s\n", class->image->name, class->name_space, class->name);
466 if (g_hash_table_lookup_extended (iid_hash, str, NULL, &value)) {
470 g_hash_table_insert (iid_hash, str, (gpointer)iid);
478 collect_implemented_interfaces_aux (MonoClass *klass, GPtrArray *res)
483 for (i = 0; i < klass->interface_count; i++) {
484 ic = klass->interfaces [i];
486 g_ptr_array_add (res, ic);
488 collect_implemented_interfaces_aux (ic, res);
493 collect_implemented_interfaces (MonoClass *klass)
495 GPtrArray *res = g_ptr_array_new ();
497 collect_implemented_interfaces_aux (klass, res);
502 setup_interface_offsets (MonoClass *class, int cur_slot)
508 /* compute maximum number of slots and maximum interface id */
510 for (k = class; k ; k = k->parent) {
511 for (i = 0; i < k->interface_count; i++) {
512 ic = k->interfaces [i];
515 mono_class_init (ic);
517 if (max_iid < ic->interface_id)
518 max_iid = ic->interface_id;
522 if (class->flags & TYPE_ATTRIBUTE_INTERFACE) {
523 if (max_iid < class->interface_id)
524 max_iid = class->interface_id;
526 class->max_interface_id = max_iid;
527 /* compute vtable offset for interfaces */
528 class->interface_offsets = g_malloc (sizeof (gpointer) * (max_iid + 1));
530 for (i = 0; i <= max_iid; i++)
531 class->interface_offsets [i] = -1;
533 ifaces = collect_implemented_interfaces (class);
534 for (i = 0; i < ifaces->len; ++i) {
535 ic = g_ptr_array_index (ifaces, i);
536 class->interface_offsets [ic->interface_id] = cur_slot;
537 cur_slot += ic->method.count;
539 g_ptr_array_free (ifaces, TRUE);
541 for (k = class->parent; k ; k = k->parent) {
542 ifaces = collect_implemented_interfaces (k);
543 for (i = 0; i < ifaces->len; ++i) {
544 ic = g_ptr_array_index (ifaces, i);
546 if (class->interface_offsets [ic->interface_id] == -1) {
547 int io = k->interface_offsets [ic->interface_id];
551 class->interface_offsets [ic->interface_id] = io;
554 g_ptr_array_free (ifaces, TRUE);
560 mono_class_setup_vtable (MonoClass *class, MonoMethod **overrides, int onum)
564 int i, max_vtsize = 0, max_iid, cur_slot = 0;
566 MonoGHashTable *override_map;
568 /* setup_vtable() must be called only once on the type */
569 if (class->interface_offsets) {
570 g_warning ("vtable already computed in %s.%s", class->name_space, class->name);
574 ifaces = collect_implemented_interfaces (class);
575 for (i = 0; i < ifaces->len; i++) {
576 MonoClass *ic = g_ptr_array_index (ifaces, i);
577 max_vtsize += ic->method.count;
579 g_ptr_array_free (ifaces, TRUE);
582 max_vtsize += class->parent->vtable_size;
583 cur_slot = class->parent->vtable_size;
586 max_vtsize += class->method.count;
588 vtable = alloca (sizeof (gpointer) * max_vtsize);
589 memset (vtable, 0, sizeof (gpointer) * max_vtsize);
591 /* printf ("METAINIT %s.%s\n", class->name_space, class->name); */
593 cur_slot = setup_interface_offsets (class, cur_slot);
594 max_iid = class->max_interface_id;
596 if (class->parent && class->parent->vtable_size)
597 memcpy (vtable, class->parent->vtable, sizeof (gpointer) * class->parent->vtable_size);
599 override_map = mono_g_hash_table_new (NULL, NULL);
601 /* override interface methods */
602 for (i = 0; i < onum; i++) {
603 MonoMethod *decl = overrides [i*2];
604 if (decl->klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
606 g_assert (decl->slot != -1);
607 dslot = decl->slot + class->interface_offsets [decl->klass->interface_id];
608 vtable [dslot] = overrides [i*2 + 1];
609 vtable [dslot]->slot = dslot;
610 mono_g_hash_table_insert (override_map, overrides [i * 2], overrides [i * 2 + 1]);
614 for (k = class; k ; k = k->parent) {
615 ifaces = collect_implemented_interfaces (k);
616 for (i = 0; i < ifaces->len; i++) {
619 ic = g_ptr_array_index (ifaces, i);
620 io = k->interface_offsets [ic->interface_id];
623 g_assert (io <= max_vtsize);
626 for (l = 0; l < ic->method.count; l++) {
627 MonoMethod *im = ic->methods [l];
629 if (vtable [io + l] && !(vtable [io + l]->flags & METHOD_ATTRIBUTE_ABSTRACT))
632 for (j = 0; j < class->method.count; ++j) {
633 MonoMethod *cm = class->methods [j];
634 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
635 !((cm->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == METHOD_ATTRIBUTE_PUBLIC) ||
636 !(cm->flags & METHOD_ATTRIBUTE_NEW_SLOT))
638 if (!strcmp(cm->name, im->name) &&
639 mono_metadata_signature_equal (cm->signature, im->signature)) {
640 g_assert (io + l <= max_vtsize);
641 vtable [io + l] = cm;
646 /* already implemented */
647 if (io >= k->vtable_size)
651 for (l = 0; l < ic->method.count; l++) {
652 MonoMethod *im = ic->methods [l];
655 g_assert (io + l <= max_vtsize);
657 if (vtable [io + l] && !(vtable [io + l]->flags & METHOD_ATTRIBUTE_ABSTRACT))
660 for (k1 = class; k1; k1 = k1->parent) {
661 for (j = 0; j < k1->method.count; ++j) {
662 MonoMethod *cm = k1->methods [j];
664 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
665 !(cm->flags & METHOD_ATTRIBUTE_PUBLIC))
668 if (!strcmp(cm->name, im->name) &&
669 mono_metadata_signature_equal (cm->signature, im->signature)) {
670 g_assert (io + l <= max_vtsize);
671 vtable [io + l] = cm;
676 g_assert (io + l <= max_vtsize);
677 if (vtable [io + l] && !(vtable [io + l]->flags & METHOD_ATTRIBUTE_ABSTRACT))
682 for (l = 0; l < ic->method.count; l++) {
683 MonoMethod *im = ic->methods [l];
684 char *qname, *fqname;
690 qname = g_strconcat (ic->name, ".", im->name, NULL);
691 if (ic->name_space && ic->name_space [0])
692 fqname = g_strconcat (ic->name_space, ".", ic->name, ".", im->name, NULL);
696 for (k1 = class; k1; k1 = k1->parent) {
697 for (j = 0; j < k1->method.count; ++j) {
698 MonoMethod *cm = k1->methods [j];
700 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL))
703 if (((fqname && !strcmp (cm->name, fqname)) || !strcmp (cm->name, qname)) &&
704 mono_metadata_signature_equal (cm->signature, im->signature)) {
705 g_assert (io + l <= max_vtsize);
706 vtable [io + l] = cm;
716 if (!(class->flags & TYPE_ATTRIBUTE_ABSTRACT)) {
717 for (l = 0; l < ic->method.count; l++) {
719 MonoMethod *im = ic->methods [l];
720 if (im->flags & METHOD_ATTRIBUTE_STATIC)
722 g_assert (io + l <= max_vtsize);
723 if (!(vtable [io + l])) {
724 for (j = 0; j < onum; ++j) {
725 g_print (" at slot %d: %s (%d) overrides %s (%d)\n", io+l, overrides [j*2+1]->name,
726 overrides [j*2+1]->slot, overrides [j*2]->name, overrides [j*2]->slot);
728 msig = mono_signature_get_desc (im->signature, FALSE);
729 printf ("no implementation for interface method %s.%s::%s(%s) in class %s.%s\n",
730 ic->name_space, ic->name, im->name, msig, class->name_space, class->name);
732 for (j = 0; j < class->method.count; ++j) {
733 MonoMethod *cm = class->methods [j];
734 msig = mono_signature_get_desc (cm->signature, FALSE);
736 printf ("METHOD %s(%s)\n", cm->name, msig);
739 g_assert_not_reached ();
744 for (l = 0; l < ic->method.count; l++) {
745 MonoMethod *im = vtable [io + l];
748 g_assert (io + l <= max_vtsize);
750 /* FIXME: why do we need this ? */
752 /* g_assert_not_reached (); */
757 g_ptr_array_free (ifaces, TRUE);
760 for (i = 0; i < class->method.count; ++i) {
763 cm = class->methods [i];
765 if (!(cm->flags & METHOD_ATTRIBUTE_NEW_SLOT) && (cm->flags & METHOD_ATTRIBUTE_VIRTUAL)) {
767 for (k = class->parent; k ; k = k->parent) {
769 for (j = 0; j < k->method.count; ++j) {
770 MonoMethod *m1 = k->methods [j];
771 if (!(m1->flags & METHOD_ATTRIBUTE_VIRTUAL))
773 if (!strcmp(cm->name, m1->name) &&
774 mono_metadata_signature_equal (cm->signature, m1->signature)) {
775 slot = k->methods [j]->slot;
776 g_assert (cm->slot < max_vtsize);
777 mono_g_hash_table_insert (override_map, m1, cm);
789 cm->slot = cur_slot++;
791 if (!(cm->flags & METHOD_ATTRIBUTE_ABSTRACT))
792 vtable [cm->slot] = cm;
795 /* override non interface methods */
796 for (i = 0; i < onum; i++) {
797 MonoMethod *decl = overrides [i*2];
798 if (!(decl->klass->flags & TYPE_ATTRIBUTE_INTERFACE)) {
799 g_assert (decl->slot != -1);
800 vtable [decl->slot] = overrides [i*2 + 1];
801 overrides [i * 2 + 1]->slot = decl->slot;
802 mono_g_hash_table_insert (override_map, decl, overrides [i * 2 + 1]);
807 * If a method occupies more than one place in the vtable, and it is
808 * overriden, then change the other occurances too.
810 for (i = 0; i < max_vtsize; ++i)
812 MonoMethod *cm = mono_g_hash_table_lookup (override_map, vtable [i]);
816 mono_g_hash_table_destroy (override_map);
819 class->vtable_size = cur_slot;
820 class->vtable = g_malloc0 (sizeof (gpointer) * class->vtable_size);
821 memcpy (class->vtable, vtable, sizeof (gpointer) * class->vtable_size);
823 if (mono_print_vtable) {
826 for (i = 0; i <= max_iid; i++)
827 if (class->interface_offsets [i] != -1)
830 printf ("VTable %s.%s (size = %d, interfaces = %d)\n", class->name_space,
831 class->name, class->vtable_size, icount);
833 for (i = 0; i < class->vtable_size; ++i) {
838 printf (" slot %03d(%03d) %s.%s:%s\n", i, cm->slot,
839 cm->klass->name_space, cm->klass->name,
846 printf ("Interfaces %s.%s (max_iid = %d)\n", class->name_space,
847 class->name, max_iid);
849 for (i = 0; i < class->interface_count; i++) {
850 ic = class->interfaces [i];
851 printf (" slot %03d(%03d) %s.%s\n",
852 class->interface_offsets [ic->interface_id],
853 ic->method.count, ic->name_space, ic->name);
856 for (k = class->parent; k ; k = k->parent) {
857 for (i = 0; i < k->interface_count; i++) {
858 ic = k->interfaces [i];
859 printf (" slot %03d(%03d) %s.%s\n",
860 class->interface_offsets [ic->interface_id],
861 ic->method.count, ic->name_space, ic->name);
870 * @class: the class to initialize
872 * compute the instance_size, class_size and other infos that cannot be
873 * computed at mono_class_get() time. Also compute a generic vtable and
874 * the method slot numbers. We use this infos later to create a domain
878 mono_class_init (MonoClass *class)
881 static MonoMethod *default_ghc = NULL;
882 static MonoMethod *default_finalize = NULL;
883 static int finalize_slot = -1;
884 static int ghc_slot = -1;
885 MonoMethod **overrides;
893 if (class->init_pending) {
894 /* this indicates a cyclic dependency */
895 g_error ("pending init %s.%s\n", class->name_space, class->name);
898 class->init_pending = 1;
900 mono_stats.initialized_class_count++;
902 if (class->parent && !class->parent->inited)
903 mono_class_init (class->parent);
906 * Computes the size used by the fields, and their locations
908 if (!class->size_inited)
909 class_compute_field_layout (class);
911 /* initialize method pointers */
914 MonoMethodSignature *sig;
915 class->method.count = class->rank > 1? 2: 1;
916 sig = mono_metadata_signature_alloc (class->image, class->rank);
917 sig->ret = &mono_defaults.void_class->byval_arg;
919 for (i = 0; i < class->rank; ++i)
920 sig->params [i] = &mono_defaults.int32_class->byval_arg;
922 ctor = (MonoMethod *) g_new0 (MonoMethodPInvoke, 1);
924 ctor->flags = METHOD_ATTRIBUTE_PUBLIC | METHOD_ATTRIBUTE_RT_SPECIAL_NAME | METHOD_ATTRIBUTE_SPECIAL_NAME;
925 ctor->iflags = METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL;
926 ctor->signature = sig;
927 ctor->name = ".ctor";
928 class->methods = g_new (MonoMethod*, class->method.count);
929 class->methods [0] = ctor;
930 if (class->rank > 1) {
931 sig = mono_metadata_signature_alloc (class->image, class->rank * 2);
932 sig->ret = &mono_defaults.void_class->byval_arg;
934 for (i = 0; i < class->rank * 2; ++i)
935 sig->params [i] = &mono_defaults.int32_class->byval_arg;
937 ctor = (MonoMethod *) g_new0 (MonoMethodPInvoke, 1);
939 ctor->flags = METHOD_ATTRIBUTE_PUBLIC | METHOD_ATTRIBUTE_RT_SPECIAL_NAME | METHOD_ATTRIBUTE_SPECIAL_NAME;
940 ctor->iflags = METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL;
941 ctor->signature = sig;
942 ctor->name = ".ctor";
943 class->methods [1] = ctor;
946 class->methods = g_new (MonoMethod*, class->method.count);
947 for (i = 0; i < class->method.count; ++i) {
948 class->methods [i] = mono_get_method (class->image,
949 MONO_TOKEN_METHOD_DEF | (i + class->method.first + 1), class);
953 init_properties (class);
956 i = mono_metadata_nesting_typedef (class->image, class->type_token, 1);
959 guint32 cols [MONO_NESTED_CLASS_SIZE];
960 mono_metadata_decode_row (&class->image->tables [MONO_TABLE_NESTEDCLASS], i - 1, cols, MONO_NESTED_CLASS_SIZE);
961 nclass = mono_class_create_from_typedef (class->image, MONO_TOKEN_TYPE_DEF | cols [MONO_NESTED_CLASS_NESTED]);
962 class->nested_classes = g_list_prepend (class->nested_classes, nclass);
964 i = mono_metadata_nesting_typedef (class->image, class->type_token, i + 1);
967 mono_class_setup_supertypes (class);
969 if (class->flags & TYPE_ATTRIBUTE_INTERFACE) {
970 for (i = 0; i < class->method.count; ++i)
971 class->methods [i]->slot = i;
972 class->init_pending = 0;
975 * class->interface_offsets is needed for the castclass/isinst code, so
976 * we have to setup them for interfaces, too.
978 setup_interface_offsets (class, 0);
982 overrides = mono_class_get_overrides (class->image, class->type_token, &onum);
983 mono_class_setup_vtable (class, overrides, onum);
987 class->init_pending = 0;
990 if (class == mono_defaults.object_class) {
992 for (i = 0; i < class->vtable_size; ++i) {
993 MonoMethod *cm = class->vtable [i];
995 if (!strcmp (cm->name, "GetHashCode")) {
1001 g_assert (ghc_slot > 0);
1003 default_ghc = class->vtable [ghc_slot];
1008 if (class->parent) {
1010 if (class->vtable [ghc_slot] == default_ghc) {
1015 if (!default_finalize) {
1016 if (class == mono_defaults.object_class) {
1018 for (i = 0; i < class->vtable_size; ++i) {
1019 MonoMethod *cm = class->vtable [i];
1021 if (!strcmp (cm->name, "Finalize")) {
1027 g_assert (finalize_slot > 0);
1029 default_finalize = class->vtable [finalize_slot];
1033 /* Object::Finalize should have empty implemenatation */
1034 class->has_finalize = 0;
1035 if (class->parent) {
1036 if (class->vtable [finalize_slot] != default_finalize)
1037 class->has_finalize = 1;
1040 if (mono_debugger_class_init_func)
1041 mono_debugger_class_init_func (class);
1046 mono_class_setup_mono_type (MonoClass *class)
1048 const char *name = class->name;
1049 const char *nspace = class->name_space;
1051 if (class->flags & TYPE_ATTRIBUTE_INTERFACE)
1052 class->interface_id = mono_get_unique_iid (class);
1054 class->this_arg.byref = 1;
1055 class->this_arg.data.klass = class;
1056 class->this_arg.type = MONO_TYPE_CLASS;
1057 class->byval_arg.data.klass = class;
1058 class->byval_arg.type = MONO_TYPE_CLASS;
1060 if (!strcmp (nspace, "System")) {
1061 if (!strcmp (name, "ValueType")) {
1063 * do not set the valuetype bit for System.ValueType.
1064 * class->valuetype = 1;
1066 } else if (!strcmp (name, "Enum")) {
1068 * do not set the valuetype bit for System.Enum.
1069 * class->valuetype = 1;
1071 class->valuetype = 0;
1072 class->enumtype = 0;
1073 } else if (!strcmp (name, "Object")) {
1074 class->this_arg.type = class->byval_arg.type = MONO_TYPE_OBJECT;
1075 } else if (!strcmp (name, "String")) {
1076 class->this_arg.type = class->byval_arg.type = MONO_TYPE_STRING;
1077 } else if (!strcmp (name, "TypedReference")) {
1078 class->this_arg.type = class->byval_arg.type = MONO_TYPE_TYPEDBYREF;
1082 if (class->valuetype) {
1083 int t = MONO_TYPE_VALUETYPE;
1084 if (!strcmp (nspace, "System")) {
1087 if (!strcmp (name, "Boolean")) {
1088 t = MONO_TYPE_BOOLEAN;
1089 } else if (!strcmp(name, "Byte")) {
1091 class->blittable = TRUE;
1095 if (!strcmp (name, "Char")) {
1100 if (!strcmp (name, "Double")) {
1102 class->blittable = TRUE;
1106 if (!strcmp (name, "Int32")) {
1108 class->blittable = TRUE;
1109 } else if (!strcmp(name, "Int16")) {
1111 class->blittable = TRUE;
1112 } else if (!strcmp(name, "Int64")) {
1114 class->blittable = TRUE;
1115 } else if (!strcmp(name, "IntPtr")) {
1117 class->blittable = TRUE;
1121 if (!strcmp (name, "Single")) {
1123 class->blittable = TRUE;
1124 } else if (!strcmp(name, "SByte")) {
1126 class->blittable = TRUE;
1130 if (!strcmp (name, "UInt32")) {
1132 class->blittable = TRUE;
1133 } else if (!strcmp(name, "UInt16")) {
1135 class->blittable = TRUE;
1136 } else if (!strcmp(name, "UInt64")) {
1138 class->blittable = TRUE;
1139 } else if (!strcmp(name, "UIntPtr")) {
1141 class->blittable = TRUE;
1145 if (!strcmp (name, "TypedReference")) {
1146 t = MONO_TYPE_TYPEDBYREF;
1147 class->blittable = TRUE;
1151 if (!strcmp (name, "Void")) {
1159 class->this_arg.type = class->byval_arg.type = t;
1164 mono_class_setup_parent (MonoClass *class, MonoClass *parent)
1166 gboolean system_namespace;
1168 system_namespace = !strcmp (class->name_space, "System");
1170 /* if root of the hierarchy */
1171 if (system_namespace && !strcmp (class->name, "Object")) {
1172 class->parent = NULL;
1173 class->instance_size = sizeof (MonoObject);
1176 if (!strcmp (class->name, "<Module>")) {
1177 class->parent = NULL;
1178 class->instance_size = 0;
1182 if (!(class->flags & TYPE_ATTRIBUTE_INTERFACE)) {
1183 class->parent = parent;
1186 g_assert_not_reached (); /* FIXME */
1188 class->marshalbyref = parent->marshalbyref;
1189 class->contextbound = parent->contextbound;
1190 class->delegate = parent->delegate;
1192 if (system_namespace) {
1193 if (*class->name == 'M' && !strcmp (class->name, "MarshalByRefObject"))
1194 class->marshalbyref = 1;
1196 if (*class->name == 'C' && !strcmp (class->name, "ContextBoundObject"))
1197 class->contextbound = 1;
1199 if (*class->name == 'D' && !strcmp (class->name, "Delegate"))
1200 class->delegate = 1;
1203 if (class->parent->enumtype || ((strcmp (class->parent->name, "ValueType") == 0) &&
1204 (strcmp (class->parent->name_space, "System") == 0)))
1205 class->valuetype = 1;
1206 if (((strcmp (class->parent->name, "Enum") == 0) && (strcmp (class->parent->name_space, "System") == 0))) {
1207 class->valuetype = class->enumtype = 1;
1209 /*class->enumtype = class->parent->enumtype; */
1210 class->parent->subclasses = g_list_prepend (class->parent->subclasses, class);
1211 mono_class_setup_supertypes (class);
1213 class->parent = NULL;
1219 mono_class_setup_supertypes (MonoClass *class)
1224 if (class->supertypes)
1228 for (k = class; k ; k = k->parent) {
1232 ms = MAX (MONO_DEFAULT_SUPERTABLE_SIZE, class->idepth);
1233 class->supertypes = g_new0 (MonoClass *, ms);
1235 if (class->parent) {
1236 for (i = class->idepth, k = class; k ; k = k->parent)
1237 class->supertypes [--i] = k;
1239 class->supertypes [0] = class;
1244 * @image: context where the image is created
1245 * @type_token: typedef token
1248 mono_class_create_from_typedef (MonoImage *image, guint32 type_token)
1250 MonoTableInfo *tt = &image->tables [MONO_TABLE_TYPEDEF];
1251 MonoClass *class, *parent = NULL;
1252 guint32 cols [MONO_TYPEDEF_SIZE];
1253 guint32 cols_next [MONO_TYPEDEF_SIZE];
1254 guint tidx = mono_metadata_token_index (type_token);
1255 const char *name, *nspace;
1257 MonoClass **interfaces;
1259 if ((class = g_hash_table_lookup (image->class_cache, GUINT_TO_POINTER (type_token))))
1262 g_assert (mono_metadata_token_table (type_token) == MONO_TABLE_TYPEDEF);
1264 mono_metadata_decode_row (tt, tidx - 1, cols, MONO_TYPEDEF_SIZE);
1266 name = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
1267 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
1269 if (cols [MONO_TYPEDEF_EXTENDS])
1270 parent = mono_class_get (image, mono_metadata_token_from_dor (cols [MONO_TYPEDEF_EXTENDS]));
1271 interfaces = mono_metadata_interfaces_from_typedef (image, type_token, &icount);
1273 class = g_malloc0 (sizeof (MonoClass));
1275 g_hash_table_insert (image->class_cache, GUINT_TO_POINTER (type_token), class);
1277 class->interfaces = interfaces;
1278 class->interface_count = icount;
1281 class->name_space = nspace;
1283 class->image = image;
1284 class->type_token = type_token;
1285 class->flags = cols [MONO_TYPEDEF_FLAGS];
1287 if ((class->flags & TYPE_ATTRIBUTE_STRING_FORMAT_MASK) == TYPE_ATTRIBUTE_UNICODE_CLASS)
1289 /* fixme: maybe we must set this on windows
1290 if ((class->flags & TYPE_ATTRIBUTE_STRING_FORMAT_MASK) == TYPE_ATTRIBUTE_AUTO_CLASS)
1294 class->cast_class = class->element_class = class;
1296 /*g_print ("Init class %s\n", name);*/
1298 mono_class_setup_parent (class, parent);
1300 mono_class_setup_mono_type (class);
1303 * Compute the field and method lists
1305 class->field.first = cols [MONO_TYPEDEF_FIELD_LIST] - 1;
1306 class->method.first = cols [MONO_TYPEDEF_METHOD_LIST] - 1;
1308 if (tt->rows > tidx){
1309 mono_metadata_decode_row (tt, tidx, cols_next, CSIZE (cols_next));
1310 class->field.last = cols_next [MONO_TYPEDEF_FIELD_LIST] - 1;
1311 class->method.last = cols_next [MONO_TYPEDEF_METHOD_LIST] - 1;
1313 class->field.last = image->tables [MONO_TABLE_FIELD].rows;
1314 class->method.last = image->tables [MONO_TABLE_METHOD].rows;
1317 if (cols [MONO_TYPEDEF_FIELD_LIST] &&
1318 cols [MONO_TYPEDEF_FIELD_LIST] <= image->tables [MONO_TABLE_FIELD].rows)
1319 class->field.count = class->field.last - class->field.first;
1321 class->field.count = 0;
1323 if (cols [MONO_TYPEDEF_METHOD_LIST] <= image->tables [MONO_TABLE_METHOD].rows)
1324 class->method.count = class->method.last - class->method.first;
1326 class->method.count = 0;
1328 /* reserve space to store vector pointer in arrays */
1329 if (!strcmp (nspace, "System") && !strcmp (name, "Array")) {
1330 class->instance_size += 2 * sizeof (gpointer);
1331 g_assert (class->field.count == 0);
1334 if (class->enumtype)
1335 class_compute_field_layout (class);
1337 if ((type_token = mono_metadata_nested_in_typedef (image, type_token)))
1338 class->nested_in = mono_class_create_from_typedef (image, type_token);
1344 mono_ptr_class_get (MonoType *type)
1347 MonoClass *el_class;
1348 static GHashTable *ptr_hash = NULL;
1351 ptr_hash = g_hash_table_new (g_direct_hash, g_direct_equal);
1352 el_class = mono_class_from_mono_type (type);
1353 if ((result = g_hash_table_lookup (ptr_hash, el_class)))
1355 result = g_new0 (MonoClass, 1);
1357 result->parent = NULL; /* no parent for PTR types */
1358 result->name = "System";
1359 result->name_space = "MonoPtrFakeClass";
1360 result->image = el_class->image;
1361 result->inited = TRUE;
1362 result->flags = TYPE_ATTRIBUTE_CLASS | (el_class->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK);
1363 /* Can pointers get boxed? */
1364 result->instance_size = sizeof (gpointer);
1365 result->cast_class = result->element_class = el_class;
1366 result->enum_basetype = &result->element_class->byval_arg;
1368 result->this_arg.type = result->byval_arg.type = MONO_TYPE_PTR;
1369 result->this_arg.data.type = result->byval_arg.data.type = result->enum_basetype;
1370 result->this_arg.byref = TRUE;
1372 mono_class_setup_supertypes (result);
1374 g_hash_table_insert (ptr_hash, el_class, result);
1380 mono_fnptr_class_get (MonoMethodSignature *sig)
1383 static GHashTable *ptr_hash = NULL;
1386 ptr_hash = g_hash_table_new (g_direct_hash, g_direct_equal);
1388 if ((result = g_hash_table_lookup (ptr_hash, sig)))
1390 result = g_new0 (MonoClass, 1);
1392 result->parent = NULL; /* no parent for PTR types */
1393 result->name = "System";
1394 result->name_space = "MonoFNPtrFakeClass";
1395 result->image = NULL; /* need to fix... */
1396 result->inited = TRUE;
1397 result->flags = TYPE_ATTRIBUTE_CLASS; // | (el_class->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK);
1398 /* Can pointers get boxed? */
1399 result->instance_size = sizeof (gpointer);
1400 result->cast_class = result->element_class = result;
1402 result->this_arg.type = result->byval_arg.type = MONO_TYPE_FNPTR;
1403 result->this_arg.data.method = result->byval_arg.data.method = sig;
1404 result->this_arg.byref = TRUE;
1405 result->enum_basetype = &result->element_class->byval_arg;
1407 mono_class_setup_supertypes (result);
1409 g_hash_table_insert (ptr_hash, sig, result);
1415 mono_class_from_mono_type (MonoType *type)
1417 switch (type->type) {
1418 case MONO_TYPE_OBJECT:
1419 return mono_defaults.object_class;
1420 case MONO_TYPE_VOID:
1421 return mono_defaults.void_class;
1422 case MONO_TYPE_BOOLEAN:
1423 return mono_defaults.boolean_class;
1424 case MONO_TYPE_CHAR:
1425 return mono_defaults.char_class;
1427 return mono_defaults.sbyte_class;
1429 return mono_defaults.byte_class;
1431 return mono_defaults.int16_class;
1433 return mono_defaults.uint16_class;
1435 return mono_defaults.int32_class;
1437 return mono_defaults.uint32_class;
1439 return mono_defaults.int_class;
1441 return mono_defaults.uint_class;
1443 return mono_defaults.int64_class;
1445 return mono_defaults.uint64_class;
1447 return mono_defaults.single_class;
1449 return mono_defaults.double_class;
1450 case MONO_TYPE_STRING:
1451 return mono_defaults.string_class;
1452 case MONO_TYPE_TYPEDBYREF:
1453 return mono_defaults.typed_reference_class;
1454 case MONO_TYPE_ARRAY:
1455 return mono_array_class_get (type->data.array->type, type->data.array->rank);
1457 return mono_ptr_class_get (type->data.type);
1458 case MONO_TYPE_FNPTR:
1459 return mono_fnptr_class_get (type->data.method);
1460 case MONO_TYPE_SZARRAY:
1461 return mono_array_class_get (type->data.type, 1);
1462 case MONO_TYPE_CLASS:
1463 case MONO_TYPE_VALUETYPE:
1464 return type->data.klass;
1466 case MONO_TYPE_GENERICINST:
1467 g_warning ("mono_class_from_type: implement me MONO_TYPE_GENERICINST");
1468 g_assert_not_reached ();
1471 g_warning ("mono_class_from_type: implement me MONO_TYPE_VAR");
1472 g_assert_not_reached ();
1474 case MONO_TYPE_MVAR:
1475 g_warning ("mono_class_from_type: implement me MONO_TYPE_MVAR");
1476 g_assert_not_reached ();
1479 g_warning ("implement me 0x%02x\n", type->type);
1480 g_assert_not_reached ();
1487 * @image: context where the image is created
1488 * @type_spec: typespec token
1491 mono_class_create_from_typespec (MonoImage *image, guint32 type_spec)
1496 type = mono_type_create_from_typespec (image, type_spec);
1498 switch (type->type) {
1499 case MONO_TYPE_ARRAY:
1500 class = mono_array_class_get (type->data.array->type, type->data.array->rank);
1502 case MONO_TYPE_SZARRAY:
1503 class = mono_array_class_get (type->data.type, 1);
1506 class = mono_class_from_mono_type (type->data.type);
1509 /* it seems any type can be stored in TypeSpec as well */
1510 class = mono_class_from_mono_type (type);
1514 mono_metadata_free_type (type);
1520 * mono_array_class_get:
1521 * @element_type: element type
1522 * @rank: the dimension of the array class
1524 * Returns: a class object describing the array with element type @element_type and
1528 mono_array_class_get (MonoType *element_type, guint32 rank)
1533 MonoClass *parent = NULL;
1538 eclass = mono_class_from_mono_type (element_type);
1539 g_assert (rank <= 255);
1541 parent = mono_defaults.array_class;
1543 if (!parent->inited)
1544 mono_class_init (parent);
1546 image = eclass->image;
1548 if ((list = g_hash_table_lookup (image->array_cache, &eclass->byval_arg))) {
1549 for (; list; list = list->next) {
1551 if (class->rank == rank)
1556 class = g_malloc0 (sizeof (MonoClass) + parent->vtable_size * sizeof (gpointer));
1558 class->image = image;
1559 class->name_space = eclass->name_space;
1560 nsize = strlen (eclass->name);
1561 name = g_malloc (nsize + 2 + rank);
1562 memcpy (name, eclass->name, nsize);
1565 memset (name + nsize + 1, ',', rank - 1);
1566 name [nsize + rank] = ']';
1567 name [nsize + rank + 1] = 0;
1569 class->type_token = 0;
1570 /* all arrays are marked serializable and sealed, bug #42779 */
1571 class->flags = TYPE_ATTRIBUTE_CLASS | TYPE_ATTRIBUTE_SERIALIZABLE | TYPE_ATTRIBUTE_SEALED |
1572 (eclass->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK);
1573 class->parent = parent;
1574 class->instance_size = mono_class_instance_size (class->parent);
1575 class->class_size = 0;
1576 class->vtable_size = parent->vtable_size;
1577 class->parent->subclasses = g_list_prepend (class->parent->subclasses, class);
1578 mono_class_setup_supertypes (class);
1582 if (eclass->enumtype)
1583 class->cast_class = eclass->element_class;
1585 class->cast_class = eclass;
1587 class->element_class = eclass;
1590 MonoArrayType *at = g_new0 (MonoArrayType, 1);
1591 class->byval_arg.type = MONO_TYPE_ARRAY;
1592 class->byval_arg.data.array = at;
1593 at->type = &eclass->byval_arg;
1595 /* FIXME: complete.... */
1597 /* FIXME: this is not correct. the lbound could be >0 */
1598 class->byval_arg.type = MONO_TYPE_SZARRAY;
1599 class->byval_arg.data.type = &eclass->byval_arg;
1601 class->this_arg = class->byval_arg;
1602 class->this_arg.byref = 1;
1604 list = g_slist_append (list, class);
1605 g_hash_table_insert (image->array_cache, &class->element_class->byval_arg, list);
1610 * mono_class_instance_size:
1613 * Returns: the size of an object instance
1616 mono_class_instance_size (MonoClass *klass)
1619 if (!klass->size_inited)
1620 mono_class_init (klass);
1622 return klass->instance_size;
1626 * mono_class_min_align:
1629 * Returns: minimm alignment requirements
1632 mono_class_min_align (MonoClass *klass)
1635 if (!klass->size_inited)
1636 mono_class_init (klass);
1638 return klass->min_align;
1642 * mono_class_value_size:
1645 * This function is used for value types, and return the
1646 * space and the alignment to store that kind of value object.
1648 * Returns: the size of a value of kind @klass
1651 mono_class_value_size (MonoClass *klass, guint32 *align)
1655 /* fixme: check disable, because we still have external revereces to
1656 * mscorlib and Dummy Objects
1658 /*g_assert (klass->valuetype);*/
1660 size = mono_class_instance_size (klass) - sizeof (MonoObject);
1663 *align = klass->min_align;
1669 * mono_class_data_size:
1672 * Returns: the size of the static class data
1675 mono_class_data_size (MonoClass *klass)
1679 mono_class_init (klass);
1681 return klass->class_size;
1685 * Auxiliary routine to mono_class_get_field
1687 * Takes a field index instead of a field token.
1689 static MonoClassField *
1690 mono_class_get_field_idx (MonoClass *class, int idx)
1692 if (class->field.count){
1693 if ((idx >= class->field.first) && (idx < class->field.last)){
1694 return &class->fields [idx - class->field.first];
1701 return mono_class_get_field_idx (class->parent, idx);
1705 * mono_class_get_field:
1706 * @class: the class to lookup the field.
1707 * @field_token: the field token
1709 * Returns: A MonoClassField representing the type and offset of
1710 * the field, or a NULL value if the field does not belong to this
1714 mono_class_get_field (MonoClass *class, guint32 field_token)
1716 int idx = mono_metadata_token_index (field_token);
1718 g_assert (mono_metadata_token_code (field_token) == MONO_TOKEN_FIELD_DEF);
1720 return mono_class_get_field_idx (class, idx - 1);
1724 mono_class_get_field_from_name (MonoClass *klass, const char *name)
1729 for (i = 0; i < klass->field.count; ++i) {
1730 if (strcmp (name, klass->fields [i].name) == 0)
1731 return &klass->fields [i];
1733 klass = klass->parent;
1739 mono_class_get_property_from_name (MonoClass *klass, const char *name)
1744 for (i = 0; i < klass->property.count; ++i) {
1745 if (strcmp (name, klass->properties [i].name) == 0)
1746 return &klass->properties [i];
1748 klass = klass->parent;
1755 * @image: the image where the class resides
1756 * @type_token: the token for the class
1757 * @at: an optional pointer to return the array element type
1759 * Returns: the MonoClass that represents @type_token in @image
1762 mono_class_get (MonoImage *image, guint32 type_token)
1766 if (image->assembly->dynamic)
1767 return mono_lookup_dynamic_token (image, type_token);
1769 switch (type_token & 0xff000000){
1770 case MONO_TOKEN_TYPE_DEF:
1771 class = mono_class_create_from_typedef (image, type_token);
1773 case MONO_TOKEN_TYPE_REF:
1774 class = mono_class_from_typeref (image, type_token);
1776 case MONO_TOKEN_TYPE_SPEC:
1777 class = mono_class_create_from_typespec (image, type_token);
1780 g_warning ("unknown token type %x", type_token & 0xff000000);
1781 g_assert_not_reached ();
1785 g_warning ("Could not load class from token 0x%08x in %s", type_token, image->name);
1791 mono_class_from_name_case (MonoImage *image, const char* name_space, const char *name)
1793 MonoTableInfo *t = &image->tables [MONO_TABLE_TYPEDEF];
1794 guint32 cols [MONO_TYPEDEF_SIZE];
1799 /* add a cache if needed */
1800 for (i = 1; i <= t->rows; ++i) {
1801 mono_metadata_decode_row (t, i - 1, cols, MONO_TYPEDEF_SIZE);
1802 /* nested types are accessed from the nesting name */
1803 visib = cols [MONO_TYPEDEF_FLAGS] & TYPE_ATTRIBUTE_VISIBILITY_MASK;
1804 if (visib > TYPE_ATTRIBUTE_PUBLIC && visib <= TYPE_ATTRIBUTE_NESTED_ASSEMBLY)
1806 n = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
1807 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
1808 if (g_strcasecmp (n, name) == 0 && g_strcasecmp (nspace, name_space) == 0)
1809 return mono_class_get (image, MONO_TOKEN_TYPE_DEF | i);
1815 load_file_for_image (MonoImage *image, int fileidx)
1817 char *base_dir, *name;
1819 MonoTableInfo *t = &image->tables [MONO_TABLE_FILE];
1823 if (fileidx < 1 || fileidx > t->rows)
1825 fname_id = mono_metadata_decode_row_col (t, fileidx - 1, MONO_FILE_NAME);
1826 fname = mono_metadata_string_heap (image, fname_id);
1827 base_dir = g_path_get_dirname (image->name);
1828 name = g_build_filename (base_dir, fname, NULL);
1829 res = mono_image_open (name, NULL);
1832 t = &res->tables [MONO_TABLE_MODULEREF];
1833 //g_print ("loaded file %s from %s (%p)\n", name, image->name, image->assembly);
1834 res->assembly = image->assembly;
1835 for (i = 0; i < t->rows; ++i) {
1836 if (res->modules [i] && !res->modules [i]->assembly)
1837 res->modules [i]->assembly = image->assembly;
1839 mono_image_load_references (image, NULL);
1847 return_nested_in (MonoClass *class, char *nested) {
1849 char *s = strchr (nested, '/');
1856 for (tmp = class->nested_classes; tmp; tmp = tmp->next) {
1858 if (strcmp (found->name, nested) == 0) {
1860 return return_nested_in (found, s);
1868 mono_class_from_name (MonoImage *image, const char* name_space, const char *name)
1870 GHashTable *nspace_table;
1871 MonoImage *loaded_image;
1877 if ((nested = strchr (name, '/'))) {
1878 int pos = nested - name;
1879 int len = strlen (name);
1882 memcpy (buf, name, len + 1);
1884 nested = buf + pos + 1;
1888 nspace_table = g_hash_table_lookup (image->name_cache, name_space);
1890 if (!nspace_table || !(token = GPOINTER_TO_UINT (g_hash_table_lookup (nspace_table, name)))) {
1891 MonoTableInfo *t = &image->tables [MONO_TABLE_EXPORTEDTYPE];
1892 guint32 cols [MONO_EXP_TYPE_SIZE];
1895 for (i = 0; i < t->rows; ++i) {
1896 const char *ename, *enspace;
1897 mono_metadata_decode_row (t, i, cols, MONO_EXP_TYPE_SIZE);
1898 ename = mono_metadata_string_heap (image, cols [MONO_EXP_TYPE_NAME]);
1899 enspace = mono_metadata_string_heap (image, cols [MONO_EXP_TYPE_NAMESPACE]);
1901 if (strcmp (name, ename) == 0 && strcmp (name_space, enspace) == 0) {
1902 guint32 impl = cols [MONO_EXP_TYPE_IMPLEMENTATION];
1903 if ((impl & IMPLEMENTATION_MASK) == IMPLEMENTATION_FILE) {
1904 loaded_image = load_file_for_image (image, impl >> IMPLEMENTATION_BITS);
1907 class = mono_class_from_name (loaded_image, name_space, name);
1909 return return_nested_in (class, nested);
1912 g_error ("not yet implemented");
1916 /*g_warning ("token not found for %s.%s in image %s", name_space, name, image->name);*/
1920 token = MONO_TOKEN_TYPE_DEF | token;
1922 class = mono_class_get (image, token);
1924 return return_nested_in (class, nested);
1929 mono_class_is_subclass_of (MonoClass *klass, MonoClass *klassc,
1930 gboolean check_interfaces)
1932 if (check_interfaces && (klassc->flags & TYPE_ATTRIBUTE_INTERFACE) && !(klass->flags & TYPE_ATTRIBUTE_INTERFACE)) {
1933 if ((klassc->interface_id <= klass->max_interface_id) &&
1934 (klass->interface_offsets [klassc->interface_id] >= 0))
1936 } else if (check_interfaces && (klassc->flags & TYPE_ATTRIBUTE_INTERFACE) && (klass->flags & TYPE_ATTRIBUTE_INTERFACE)) {
1939 for (i = 0; i < klass->interface_count; i ++) {
1940 MonoClass *ic = klass->interfaces [i];
1945 if (!(klass->flags & TYPE_ATTRIBUTE_INTERFACE) && mono_class_has_parent (klass, klassc))
1953 * Returns the nnumber of bytes an element of type klass
1954 * uses when stored into an array.
1957 mono_class_array_element_size (MonoClass *klass)
1959 int t = klass->byval_arg.type;
1965 case MONO_TYPE_BOOLEAN:
1969 case MONO_TYPE_CHAR:
1978 case MONO_TYPE_CLASS:
1979 case MONO_TYPE_STRING:
1980 case MONO_TYPE_OBJECT:
1981 case MONO_TYPE_SZARRAY:
1982 case MONO_TYPE_ARRAY:
1983 return sizeof (gpointer);
1988 case MONO_TYPE_VALUETYPE:
1989 if (klass->enumtype) {
1990 t = klass->enum_basetype->type;
1993 return mono_class_instance_size (klass) - sizeof (MonoObject);
1995 g_error ("unknown type 0x%02x in mono_class_array_element_size", t);
2001 * mono_array_element_size:
2002 * @ac: pointer to a #MonoArrayClass
2004 * Returns: the size of single array element.
2007 mono_array_element_size (MonoClass *ac)
2009 return mono_class_array_element_size (ac->element_class);
2013 mono_ldtoken (MonoImage *image, guint32 token, MonoClass **handle_class)
2015 if (image->assembly->dynamic) {
2016 gpointer obj = mono_lookup_dynamic_token (image, token);
2018 switch (token & 0xff000000) {
2019 case MONO_TOKEN_TYPE_DEF:
2020 case MONO_TOKEN_TYPE_REF:
2021 case MONO_TOKEN_TYPE_SPEC:
2023 *handle_class = mono_defaults.typehandle_class;
2024 return &((MonoClass*)obj)->byval_arg;
2025 case MONO_TOKEN_METHOD_DEF:
2027 *handle_class = mono_defaults.methodhandle_class;
2029 case MONO_TOKEN_FIELD_DEF:
2031 *handle_class = mono_defaults.fieldhandle_class;
2034 g_assert_not_reached ();
2038 switch (token & 0xff000000) {
2039 case MONO_TOKEN_TYPE_DEF:
2040 case MONO_TOKEN_TYPE_REF: {
2043 *handle_class = mono_defaults.typehandle_class;
2044 class = mono_class_get (image, token);
2045 mono_class_init (class);
2046 /* We return a MonoType* as handle */
2047 return &class->byval_arg;
2049 case MONO_TOKEN_TYPE_SPEC: {
2052 *handle_class = mono_defaults.typehandle_class;
2053 class = mono_class_create_from_typespec (image, token);
2054 mono_class_init (class);
2055 return &class->byval_arg;
2057 case MONO_TOKEN_FIELD_DEF: {
2059 guint32 type = mono_metadata_typedef_from_field (image, mono_metadata_token_index (token));
2060 class = mono_class_get (image, MONO_TOKEN_TYPE_DEF | type);
2061 mono_class_init (class);
2063 *handle_class = mono_defaults.fieldhandle_class;
2064 return mono_class_get_field (class, token);
2066 case MONO_TOKEN_METHOD_DEF:
2067 case MONO_TOKEN_MEMBER_REF:
2069 g_warning ("Unknown token 0x%08x in ldtoken", token);
2076 * This function might need to call runtime functions so it can't be part
2077 * of the metadata library.
2079 static MonoLookupDynamicToken lookup_dynamic = NULL;
2082 mono_install_lookup_dynamic_token (MonoLookupDynamicToken func)
2084 lookup_dynamic = func;
2088 mono_lookup_dynamic_token (MonoImage *image, guint32 token)
2090 return lookup_dynamic (image, token);