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/os/gc_wrapper.h>
34 #define CSIZE(x) (sizeof (x) / 4)
38 gboolean mono_print_vtable = FALSE;
40 static MonoClass * mono_class_create_from_typedef (MonoImage *image, guint32 type_token);
42 void (*mono_debugger_class_init_func) (MonoClass *klass) = NULL;
45 mono_class_from_typeref (MonoImage *image, guint32 type_token)
47 guint32 cols [MONO_TYPEREF_SIZE];
48 MonoTableInfo *t = &image->tables [MONO_TABLE_TYPEREF];
50 const char *name, *nspace;
52 MonoAssembly **references;
53 MonoImageOpenStatus status;
55 mono_metadata_decode_row (t, (type_token&0xffffff)-1, cols, MONO_TYPEREF_SIZE);
57 name = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAME]);
58 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAMESPACE]);
60 idx = cols [MONO_TYPEREF_SCOPE] >> RESOLTION_SCOPE_BITS;
61 switch (cols [MONO_TYPEREF_SCOPE] & RESOLTION_SCOPE_MASK) {
62 case RESOLTION_SCOPE_MODULE:
64 g_error ("null ResolutionScope not yet handled");
65 /* a typedef in disguise */
66 return mono_class_from_name (image, nspace, name);
67 case RESOLTION_SCOPE_MODULEREF:
68 return mono_class_from_name (image->modules [idx - 1], nspace, name);
69 case RESOLTION_SCOPE_TYPEREF: {
70 MonoClass *enclosing = mono_class_from_typeref (image, MONO_TOKEN_TYPE_REF | idx);
72 mono_class_init (enclosing);
73 for (tmp = enclosing->nested_classes; tmp; tmp = tmp->next) {
75 if (strcmp (res->name, name) == 0)
78 g_warning ("TypeRef ResolutionScope not yet handled (%d)", idx);
81 case RESOLTION_SCOPE_ASSEMBLYREF:
85 mono_image_load_references (image, &status);
86 references = image->references;
87 if (!references || !references [idx-1]) {
89 * detected a reference to mscorlib, we simply return a reference to a dummy
90 * until we have a better solution.
92 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]));
94 res = mono_class_from_name (image, "System", "MonoDummy");
95 /* prevent method loading */
97 /* some storage if the type is used - very ugly hack */
98 res->instance_size = 2*sizeof (gpointer);
102 /* load referenced assembly */
103 image = references [idx-1]->image;
105 return mono_class_from_name (image, nspace, name);
109 mono_marshal_load_type_info (MonoClass* klass)
112 MonoMarshalType *info;
115 g_assert (klass != NULL);
117 if (klass->marshal_info)
118 return klass->marshal_info;
121 mono_class_init (klass);
123 for (i = 0; i < klass->field.count; ++i) {
124 if (klass->fields [i].type->attrs & FIELD_ATTRIBUTE_STATIC)
129 layout = klass->flags & TYPE_ATTRIBUTE_LAYOUT_MASK;
131 klass->marshal_info = info = g_malloc0 (sizeof (MonoMarshalType) + sizeof (MonoMarshalField) * count);
132 info->num_fields = count;
135 info->native_size = mono_class_native_size (klass->parent, NULL);
137 for (j = i = 0; i < klass->field.count; ++i) {
140 if (klass->fields [i].type->attrs & FIELD_ATTRIBUTE_STATIC)
143 if (klass->fields [i].type->attrs & FIELD_ATTRIBUTE_HAS_FIELD_MARSHAL)
144 mono_metadata_field_info (klass->image, klass->field.first + i,
145 NULL, NULL, &info->fields [j].mspec);
147 info->fields [j].field = &klass->fields [i];
150 case TYPE_ATTRIBUTE_AUTO_LAYOUT:
151 case TYPE_ATTRIBUTE_SEQUENTIAL_LAYOUT:
152 size = mono_marshal_type_size (klass->fields [i].type, info->fields [j].mspec,
153 &align, TRUE, klass->unicode);
154 align = klass->packing_size ? MIN (klass->packing_size, align): align;
155 info->fields [j].offset = info->native_size;
156 info->fields [j].offset += align - 1;
157 info->fields [j].offset &= ~(align - 1);
158 info->native_size = info->fields [j].offset + size;
160 case TYPE_ATTRIBUTE_EXPLICIT_LAYOUT:
162 info->fields [j].offset = klass->fields [i].offset - sizeof (MonoObject);
163 info->native_size = klass->instance_size - sizeof (MonoObject);
169 if (info->native_size & (klass->min_align - 1)) {
170 info->native_size += klass->min_align - 1;
171 info->native_size &= ~(klass->min_align - 1);
174 return klass->marshal_info;
178 * class_compute_field_layout:
179 * @m: pointer to the metadata.
180 * @class: The class to initialize
182 * Initializes the class->fields.
184 * Currently we only support AUTO_LAYOUT, and do not even try to do
185 * a good job at it. This is temporary to get the code for Paolo.
188 class_compute_field_layout (MonoClass *class)
190 MonoImage *m = class->image;
191 const int top = class->field.count;
192 guint32 layout = class->flags & TYPE_ATTRIBUTE_LAYOUT_MASK;
193 MonoTableInfo *t = &m->tables [MONO_TABLE_FIELD];
194 int i, blittable = TRUE;
196 guint32 packing_size = 0;
198 if (class->size_inited)
202 if (!class->parent->size_inited)
203 class_compute_field_layout (class->parent);
204 class->instance_size += class->parent->instance_size;
205 class->min_align = class->parent->min_align;
207 class->instance_size = sizeof (MonoObject);
208 class->min_align = 1;
211 if (mono_metadata_packing_from_typedef (class->image, class->type_token, &packing_size, &class->instance_size)) {
212 class->instance_size += sizeof (MonoObject);
215 g_assert ((packing_size & 0xfffffff0) == 0);
216 class->packing_size = packing_size;
219 class->size_inited = 1;
223 class->fields = g_new0 (MonoClassField, top);
226 * Fetch all the field information.
228 for (i = 0; i < top; i++){
230 guint32 cols [MONO_FIELD_SIZE];
231 int idx = class->field.first + i;
233 mono_metadata_decode_row (t, idx, cols, CSIZE (cols));
234 /* The name is needed for fieldrefs */
235 class->fields [i].name = mono_metadata_string_heap (m, cols [MONO_FIELD_NAME]);
236 sig = mono_metadata_blob_heap (m, cols [MONO_FIELD_SIGNATURE]);
237 mono_metadata_decode_value (sig, &sig);
238 /* FIELD signature == 0x06 */
239 g_assert (*sig == 0x06);
240 class->fields [i].type = mono_metadata_parse_field_type (
241 m, cols [MONO_FIELD_FLAGS], sig + 1, &sig);
243 class->fields [i].parent = class;
245 if (!(class->fields [i].type->attrs & FIELD_ATTRIBUTE_STATIC)) {
246 if (class->fields [i].type->byref) {
249 MonoClass *field_class = mono_class_from_mono_type (class->fields [i].type);
250 if (!field_class || !field_class->blittable)
254 if (layout == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT) {
255 mono_metadata_field_info (m, idx, &class->fields [i].offset, NULL, NULL);
256 if (class->fields [i].offset == (guint32)-1)
257 g_warning ("%s not initialized correctly (missing field layout info for %s)", class->name, class->fields [i].name);
260 if (cols [MONO_FIELD_FLAGS] & FIELD_ATTRIBUTE_HAS_FIELD_RVA) {
261 mono_metadata_field_info (m, idx, NULL, &rva, NULL);
263 g_warning ("field %s in %s should have RVA data, but hasn't", class->fields [i].name, class->name);
264 class->fields [i].data = mono_cli_rva_map (class->image->image_info, rva);
267 if (class->enumtype && !(cols [MONO_FIELD_FLAGS] & FIELD_ATTRIBUTE_STATIC)) {
268 class->enum_basetype = class->fields [i].type;
269 class->cast_class = class->element_class = mono_class_from_mono_type (class->enum_basetype);
270 blittable = class->element_class->blittable;
274 if (class == mono_defaults.string_class)
277 class->blittable = blittable;
279 if (class->enumtype && !class->enum_basetype) {
280 if (!((strcmp (class->name, "Enum") == 0) && (strcmp (class->name_space, "System") == 0)))
283 mono_class_layout_fields (class);
287 mono_class_layout_fields (MonoClass *class)
290 const int top = class->field.count;
291 guint32 layout = class->flags & TYPE_ATTRIBUTE_LAYOUT_MASK;
294 * Compute field layout and total size (not considering static fields)
297 case TYPE_ATTRIBUTE_AUTO_LAYOUT:
298 case TYPE_ATTRIBUTE_SEQUENTIAL_LAYOUT:
299 for (i = 0; i < top; i++){
302 if (class->fields [i].type->attrs & FIELD_ATTRIBUTE_STATIC)
305 size = mono_type_size (class->fields [i].type, &align);
307 /* FIXME (LAMESPEC): should we also change the min alignment according to pack? */
308 align = class->packing_size ? MIN (class->packing_size, align): align;
309 class->min_align = MAX (align, class->min_align);
310 class->fields [i].offset = class->instance_size;
311 class->fields [i].offset += align - 1;
312 class->fields [i].offset &= ~(align - 1);
313 class->instance_size = class->fields [i].offset + size;
317 if (class->instance_size & (class->min_align - 1)) {
318 class->instance_size += class->min_align - 1;
319 class->instance_size &= ~(class->min_align - 1);
322 case TYPE_ATTRIBUTE_EXPLICIT_LAYOUT:
323 for (i = 0; i < top; i++) {
327 * There must be info about all the fields in a type if it
328 * uses explicit layout.
331 if (class->fields [i].type->attrs & FIELD_ATTRIBUTE_STATIC)
334 size = mono_type_size (class->fields [i].type, &align);
337 * When we get here, class->fields [i].offset is already set by the
338 * loader (for either runtime fields or fields loaded from metadata).
339 * The offset is from the start of the object: this works for both
340 * classes and valuetypes.
342 class->fields [i].offset += sizeof (MonoObject);
347 class->instance_size = MAX (class->instance_size, size + class->fields [i].offset);
352 class->size_inited = 1;
355 * Compute static field layout and size
358 case TYPE_ATTRIBUTE_AUTO_LAYOUT:
359 case TYPE_ATTRIBUTE_SEQUENTIAL_LAYOUT:
360 for (i = 0; i < top; i++){
363 if (!(class->fields [i].type->attrs & FIELD_ATTRIBUTE_STATIC))
366 size = mono_type_size (class->fields [i].type, &align);
367 class->fields [i].offset = class->class_size;
368 class->fields [i].offset += align - 1;
369 class->fields [i].offset &= ~(align - 1);
370 class->class_size = class->fields [i].offset + size;
374 case TYPE_ATTRIBUTE_EXPLICIT_LAYOUT:
375 for (i = 0; i < top; i++){
379 * There must be info about all the fields in a type if it
380 * uses explicit layout.
383 if (!(class->fields [i].type->attrs & FIELD_ATTRIBUTE_STATIC))
386 size = mono_type_size (class->fields [i].type, &align);
387 class->fields [i].offset = class->class_size;
388 class->fields [i].offset += align - 1;
389 class->fields [i].offset &= ~(align - 1);
390 class->class_size = class->fields [i].offset + size;
397 init_properties (MonoClass *class)
399 guint startm, endm, i, j;
400 guint32 cols [MONO_PROPERTY_SIZE];
401 MonoTableInfo *pt = &class->image->tables [MONO_TABLE_PROPERTY];
402 MonoTableInfo *msemt = &class->image->tables [MONO_TABLE_METHODSEMANTICS];
404 class->property.first = mono_metadata_properties_from_typedef (class->image, mono_metadata_token_index (class->type_token) - 1, &class->property.last);
405 class->property.count = class->property.last - class->property.first;
407 class->properties = g_new0 (MonoProperty, class->property.count);
408 for (i = class->property.first; i < class->property.last; ++i) {
409 mono_metadata_decode_row (pt, i, cols, MONO_PROPERTY_SIZE);
410 class->properties [i - class->property.first].attrs = cols [MONO_PROPERTY_FLAGS];
411 class->properties [i - class->property.first].name = mono_metadata_string_heap (class->image, cols [MONO_PROPERTY_NAME]);
413 startm = mono_metadata_methods_from_property (class->image, i, &endm);
414 for (j = startm; j < endm; ++j) {
415 mono_metadata_decode_row (msemt, j, cols, MONO_METHOD_SEMA_SIZE);
416 switch (cols [MONO_METHOD_SEMA_SEMANTICS]) {
417 case METHOD_SEMANTIC_SETTER:
418 class->properties [i - class->property.first].set = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
420 case METHOD_SEMANTIC_GETTER:
421 class->properties [i - class->property.first].get = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
431 init_events (MonoClass *class)
433 guint startm, endm, i, j;
434 guint32 cols [MONO_EVENT_SIZE];
435 MonoTableInfo *pt = &class->image->tables [MONO_TABLE_EVENT];
436 MonoTableInfo *msemt = &class->image->tables [MONO_TABLE_METHODSEMANTICS];
438 class->event.first = mono_metadata_events_from_typedef (class->image, mono_metadata_token_index (class->type_token) - 1, &class->event.last);
439 class->event.count = class->event.last - class->event.first;
441 class->events = g_new0 (MonoEvent, class->event.count);
442 for (i = class->event.first; i < class->event.last; ++i) {
443 mono_metadata_decode_row (pt, i, cols, MONO_EVENT_SIZE);
444 class->events [i - class->event.first].attrs = cols [MONO_EVENT_FLAGS];
445 class->events [i - class->event.first].name = mono_metadata_string_heap (class->image, cols [MONO_EVENT_NAME]);
447 startm = mono_metadata_methods_from_event (class->image, i, &endm);
448 for (j = startm; j < endm; ++j) {
449 mono_metadata_decode_row (msemt, j, cols, MONO_METHOD_SEMA_SIZE);
450 switch (cols [MONO_METHOD_SEMA_SEMANTICS]) {
451 case METHOD_SEMANTIC_ADD_ON:
452 class->events [i - class->event.first].add = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
454 case METHOD_SEMANTIC_REMOVE_ON:
455 class->events [i - class->event.first].remove = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
457 case METHOD_SEMANTIC_FIRE:
458 class->events [i - class->event.first].raise = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
460 case METHOD_SEMANTIC_OTHER: /* don't care for now */
469 mono_get_unique_iid (MonoClass *class)
471 static GHashTable *iid_hash = NULL;
472 static guint iid = 0;
477 g_assert (class->flags & TYPE_ATTRIBUTE_INTERFACE);
480 iid_hash = g_hash_table_new (g_str_hash, g_str_equal);
482 str = g_strdup_printf ("%s|%s.%s\n", class->image->name, class->name_space, class->name);
484 if (g_hash_table_lookup_extended (iid_hash, str, NULL, &value)) {
488 g_hash_table_insert (iid_hash, str, (gpointer)iid);
496 collect_implemented_interfaces_aux (MonoClass *klass, GPtrArray *res)
501 for (i = 0; i < klass->interface_count; i++) {
502 ic = klass->interfaces [i];
504 g_ptr_array_add (res, ic);
506 collect_implemented_interfaces_aux (ic, res);
511 collect_implemented_interfaces (MonoClass *klass)
513 GPtrArray *res = g_ptr_array_new ();
515 collect_implemented_interfaces_aux (klass, res);
520 setup_interface_offsets (MonoClass *class, int cur_slot)
526 /* compute maximum number of slots and maximum interface id */
528 for (k = class; k ; k = k->parent) {
529 for (i = 0; i < k->interface_count; i++) {
530 ic = k->interfaces [i];
533 mono_class_init (ic);
535 if (max_iid < ic->interface_id)
536 max_iid = ic->interface_id;
540 if (class->flags & TYPE_ATTRIBUTE_INTERFACE) {
541 if (max_iid < class->interface_id)
542 max_iid = class->interface_id;
544 class->max_interface_id = max_iid;
545 /* compute vtable offset for interfaces */
546 class->interface_offsets = g_malloc (sizeof (gpointer) * (max_iid + 1));
548 for (i = 0; i <= max_iid; i++)
549 class->interface_offsets [i] = -1;
551 ifaces = collect_implemented_interfaces (class);
552 for (i = 0; i < ifaces->len; ++i) {
553 ic = g_ptr_array_index (ifaces, i);
554 class->interface_offsets [ic->interface_id] = cur_slot;
555 cur_slot += ic->method.count;
557 g_ptr_array_free (ifaces, FALSE);
559 for (k = class->parent; k ; k = k->parent) {
560 ifaces = collect_implemented_interfaces (k);
561 for (i = 0; i < ifaces->len; ++i) {
562 ic = g_ptr_array_index (ifaces, i);
564 if (class->interface_offsets [ic->interface_id] == -1) {
565 int io = k->interface_offsets [ic->interface_id];
569 class->interface_offsets [ic->interface_id] = io;
572 g_ptr_array_free (ifaces, FALSE);
578 mono_class_setup_vtable (MonoClass *class, MonoMethod **overrides, int onum)
582 int i, max_vtsize = 0, max_iid, cur_slot = 0;
585 /* setup_vtable() must be called only once on the type */
586 if (class->interface_offsets) {
587 g_warning ("vtable already computed in %s.%s", class->name_space, class->name);
591 ifaces = collect_implemented_interfaces (class);
592 for (i = 0; i < ifaces->len; i++) {
593 MonoClass *ic = g_ptr_array_index (ifaces, i);
594 max_vtsize += ic->method.count;
596 g_ptr_array_free (ifaces, FALSE);
599 max_vtsize += class->parent->vtable_size;
600 cur_slot = class->parent->vtable_size;
603 max_vtsize += class->method.count;
605 vtable = alloca (sizeof (gpointer) * max_vtsize);
606 memset (vtable, 0, sizeof (gpointer) * max_vtsize);
608 /* printf ("METAINIT %s.%s\n", class->name_space, class->name); */
610 cur_slot = setup_interface_offsets (class, cur_slot);
611 max_iid = class->max_interface_id;
613 if (class->parent && class->parent->vtable_size)
614 memcpy (vtable, class->parent->vtable, sizeof (gpointer) * class->parent->vtable_size);
616 /* override interface methods */
617 for (i = 0; i < onum; i++) {
618 MonoMethod *decl = overrides [i*2];
619 if (decl->klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
621 g_assert (decl->slot != -1);
622 dslot = decl->slot + class->interface_offsets [decl->klass->interface_id];
623 vtable [dslot] = overrides [i*2 + 1];
627 for (k = class; k ; k = k->parent) {
628 ifaces = collect_implemented_interfaces (k);
629 for (i = 0; i < ifaces->len; i++) {
632 ic = g_ptr_array_index (ifaces, i);
633 io = k->interface_offsets [ic->interface_id];
636 g_assert (io <= max_vtsize);
639 for (l = 0; l < ic->method.count; l++) {
640 MonoMethod *im = ic->methods [l];
641 for (j = 0; j < class->method.count; ++j) {
642 MonoMethod *cm = class->methods [j];
643 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
644 !(cm->flags & METHOD_ATTRIBUTE_PUBLIC) ||
645 !(cm->flags & METHOD_ATTRIBUTE_NEW_SLOT))
647 if (!strcmp(cm->name, im->name) &&
648 mono_metadata_signature_equal (cm->signature, im->signature)) {
649 g_assert (io + l <= max_vtsize);
650 vtable [io + l] = cm;
655 /* already implemented */
656 if (io >= k->vtable_size)
660 for (l = 0; l < ic->method.count; l++) {
661 MonoMethod *im = ic->methods [l];
664 g_assert (io + l <= max_vtsize);
666 if (vtable [io + l] && !(vtable [io + l]->flags & METHOD_ATTRIBUTE_ABSTRACT))
669 for (k1 = class; k1; k1 = k1->parent) {
670 for (j = 0; j < k1->method.count; ++j) {
671 MonoMethod *cm = k1->methods [j];
673 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
674 !(cm->flags & METHOD_ATTRIBUTE_PUBLIC))
677 if (!strcmp(cm->name, im->name) &&
678 mono_metadata_signature_equal (cm->signature, im->signature)) {
679 g_assert (io + l <= max_vtsize);
680 vtable [io + l] = cm;
685 g_assert (io + l <= max_vtsize);
686 if (vtable [io + l] && !(vtable [io + l]->flags & METHOD_ATTRIBUTE_ABSTRACT))
691 for (l = 0; l < ic->method.count; l++) {
692 MonoMethod *im = ic->methods [l];
693 char *qname, *fqname;
699 qname = g_strconcat (ic->name, ".", im->name, NULL);
700 if (ic->name_space && ic->name_space [0])
701 fqname = g_strconcat (ic->name_space, ".", ic->name, ".", im->name, NULL);
705 for (k1 = class; k1; k1 = k1->parent) {
706 for (j = 0; j < k1->method.count; ++j) {
707 MonoMethod *cm = k1->methods [j];
709 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL))
712 if (((fqname && !strcmp (cm->name, fqname)) || !strcmp (cm->name, qname)) &&
713 mono_metadata_signature_equal (cm->signature, im->signature)) {
714 g_assert (io + l <= max_vtsize);
715 vtable [io + l] = cm;
725 if (!(class->flags & TYPE_ATTRIBUTE_ABSTRACT)) {
726 for (l = 0; l < ic->method.count; l++) {
728 MonoMethod *im = ic->methods [l];
729 if (im->flags & METHOD_ATTRIBUTE_STATIC)
731 g_assert (io + l <= max_vtsize);
732 if (!(vtable [io + l])) {
733 for (j = 0; j < onum; ++j) {
734 g_print (" at slot %d: %s (%d) overrides %s (%d)\n", io+l, overrides [j*2+1]->name,
735 overrides [j*2+1]->slot, overrides [j*2]->name, overrides [j*2]->slot);
737 msig = mono_signature_get_desc (im->signature, FALSE);
738 printf ("no implementation for interface method %s.%s::%s(%s) in class %s.%s\n",
739 ic->name_space, ic->name, im->name, msig, class->name_space, class->name);
741 for (j = 0; j < class->method.count; ++j) {
742 MonoMethod *cm = class->methods [j];
743 msig = mono_signature_get_desc (cm->signature, FALSE);
745 printf ("METHOD %s(%s)\n", cm->name, msig);
748 g_assert_not_reached ();
753 for (l = 0; l < ic->method.count; l++) {
754 MonoMethod *im = vtable [io + l];
757 g_assert (io + l <= max_vtsize);
759 /* FIXME: why do we need this ? */
761 /* g_assert_not_reached (); */
766 g_ptr_array_free (ifaces, FALSE);
769 for (i = 0; i < class->method.count; ++i) {
772 cm = class->methods [i];
774 if (!(cm->flags & METHOD_ATTRIBUTE_NEW_SLOT) && (cm->flags & METHOD_ATTRIBUTE_VIRTUAL)) {
776 for (k = class->parent; k ; k = k->parent) {
778 for (j = 0; j < k->method.count; ++j) {
779 MonoMethod *m1 = k->methods [j];
780 if (!(m1->flags & METHOD_ATTRIBUTE_VIRTUAL))
782 if (!strcmp(cm->name, m1->name) &&
783 mono_metadata_signature_equal (cm->signature, m1->signature)) {
784 slot = k->methods [j]->slot;
785 g_assert (cm->slot < max_vtsize);
797 cm->slot = cur_slot++;
799 if (!(cm->flags & METHOD_ATTRIBUTE_ABSTRACT))
800 vtable [cm->slot] = cm;
803 /* override non interface methods */
804 for (i = 0; i < onum; i++) {
805 MonoMethod *decl = overrides [i*2];
806 if (!(decl->klass->flags & TYPE_ATTRIBUTE_INTERFACE)) {
807 g_assert (decl->slot != -1);
808 vtable [decl->slot] = overrides [i*2 + 1];
809 overrides [i * 2 + 1]->slot = decl->slot;
813 class->vtable_size = cur_slot;
814 class->vtable = g_malloc0 (sizeof (gpointer) * class->vtable_size);
815 memcpy (class->vtable, vtable, sizeof (gpointer) * class->vtable_size);
817 if (mono_print_vtable) {
820 for (i = 0; i <= max_iid; i++)
821 if (class->interface_offsets [i] != -1)
824 printf ("VTable %s.%s (size = %d, interfaces = %d)\n", class->name_space,
825 class->name, class->vtable_size, icount);
827 for (i = 0; i < class->vtable_size; ++i) {
832 printf (" slot %03d(%03d) %s.%s:%s\n", i, cm->slot,
833 cm->klass->name_space, cm->klass->name,
840 printf ("Interfaces %s.%s (max_iid = %d)\n", class->name_space,
841 class->name, max_iid);
843 for (i = 0; i < class->interface_count; i++) {
844 ic = class->interfaces [i];
845 printf (" slot %03d(%03d) %s.%s\n",
846 class->interface_offsets [ic->interface_id],
847 ic->method.count, ic->name_space, ic->name);
850 for (k = class->parent; k ; k = k->parent) {
851 for (i = 0; i < k->interface_count; i++) {
852 ic = k->interfaces [i];
853 printf (" slot %03d(%03d) %s.%s\n",
854 class->interface_offsets [ic->interface_id],
855 ic->method.count, ic->name_space, ic->name);
864 * @class: the class to initialize
866 * compute the instance_size, class_size and other infos that cannot be
867 * computed at mono_class_get() time. Also compute a generic vtable and
868 * the method slot numbers. We use this infos later to create a domain
872 mono_class_init (MonoClass *class)
875 static MonoMethod *default_ghc = NULL;
876 static MonoMethod *default_finalize = NULL;
877 static int finalize_slot = -1;
878 static int ghc_slot = -1;
879 MonoMethod **overrides;
887 if (class->init_pending) {
888 /* this indicates a cyclic dependency */
889 g_error ("pending init %s.%s\n", class->name_space, class->name);
892 class->init_pending = 1;
894 mono_stats.initialized_class_count++;
896 if (class->parent && !class->parent->inited)
897 mono_class_init (class->parent);
900 * Computes the size used by the fields, and their locations
902 if (!class->size_inited)
903 class_compute_field_layout (class);
905 /* initialize method pointers */
906 class->methods = g_new (MonoMethod*, class->method.count);
907 for (i = 0; i < class->method.count; ++i)
908 class->methods [i] = mono_get_method (class->image,
909 MONO_TOKEN_METHOD_DEF | (i + class->method.first + 1), class);
911 init_properties (class);
914 i = mono_metadata_nesting_typedef (class->image, class->type_token, 1);
917 guint32 cols [MONO_NESTED_CLASS_SIZE];
918 mono_metadata_decode_row (&class->image->tables [MONO_TABLE_NESTEDCLASS], i - 1, cols, MONO_NESTED_CLASS_SIZE);
919 nclass = mono_class_create_from_typedef (class->image, MONO_TOKEN_TYPE_DEF | cols [MONO_NESTED_CLASS_NESTED]);
920 class->nested_classes = g_list_prepend (class->nested_classes, nclass);
922 i = mono_metadata_nesting_typedef (class->image, class->type_token, i + 1);
925 mono_class_setup_supertypes (class);
927 if (class->flags & TYPE_ATTRIBUTE_INTERFACE) {
928 for (i = 0; i < class->method.count; ++i)
929 class->methods [i]->slot = i;
930 class->init_pending = 0;
933 * class->interface_offsets is needed for the castclass/isinst code, so
934 * we have to setup them for interfaces, too.
936 setup_interface_offsets (class, 0);
940 overrides = mono_class_get_overrides (class->image, class->type_token, &onum);
941 mono_class_setup_vtable (class, overrides, onum);
945 class->init_pending = 0;
948 if (class == mono_defaults.object_class) {
950 for (i = 0; i < class->vtable_size; ++i) {
951 MonoMethod *cm = class->vtable [i];
953 if (!strcmp (cm->name, "GetHashCode")) {
959 g_assert (ghc_slot > 0);
961 default_ghc = class->vtable [ghc_slot];
968 if (class->vtable [ghc_slot] == default_ghc) {
973 if (!default_finalize) {
974 if (class == mono_defaults.object_class) {
976 for (i = 0; i < class->vtable_size; ++i) {
977 MonoMethod *cm = class->vtable [i];
979 if (!strcmp (cm->name, "Finalize")) {
985 g_assert (finalize_slot > 0);
987 default_finalize = class->vtable [finalize_slot];
991 /* Object::Finalize should have empty implemenatation */
992 class->has_finalize = 0;
994 if (class->vtable [finalize_slot] != default_finalize)
995 class->has_finalize = 1;
998 if (mono_debugger_class_init_func)
999 mono_debugger_class_init_func (class);
1004 * Compute a relative numbering of the class hierarchy as described in
1005 * "Java for Large-Scale Scientific Computations?"
1008 mono_compute_relative_numbering (MonoClass *class, int *c)
1014 class->baseval = *c;
1016 for (s = class->subclasses; s; s = s->next)
1017 mono_compute_relative_numbering ((MonoClass *)s->data, c);
1019 class->diffval = *c - class->baseval;
1023 mono_class_setup_mono_type (MonoClass *class)
1025 const char *name = class->name;
1026 const char *nspace = class->name_space;
1028 if (class->flags & TYPE_ATTRIBUTE_INTERFACE)
1029 class->interface_id = mono_get_unique_iid (class);
1031 class->this_arg.byref = 1;
1032 class->this_arg.data.klass = class;
1033 class->this_arg.type = MONO_TYPE_CLASS;
1034 class->byval_arg.data.klass = class;
1035 class->byval_arg.type = MONO_TYPE_CLASS;
1037 if (!strcmp (nspace, "System")) {
1038 if (!strcmp (name, "ValueType")) {
1040 * do not set the valuetype bit for System.ValueType.
1041 * class->valuetype = 1;
1043 } else if (!strcmp (name, "Enum")) {
1045 * do not set the valuetype bit for System.Enum.
1046 * class->valuetype = 1;
1048 class->valuetype = 0;
1049 class->enumtype = 0;
1050 } else if (!strcmp (name, "Object")) {
1051 class->this_arg.type = class->byval_arg.type = MONO_TYPE_OBJECT;
1052 } else if (!strcmp (name, "String")) {
1053 class->this_arg.type = class->byval_arg.type = MONO_TYPE_STRING;
1054 } else if (!strcmp (name, "TypedReference")) {
1055 class->this_arg.type = class->byval_arg.type = MONO_TYPE_TYPEDBYREF;
1059 if (class->valuetype) {
1060 int t = MONO_TYPE_VALUETYPE;
1061 if (!strcmp (nspace, "System")) {
1064 if (!strcmp (name, "Boolean")) {
1065 t = MONO_TYPE_BOOLEAN;
1066 } else if (!strcmp(name, "Byte")) {
1068 class->blittable = TRUE;
1072 if (!strcmp (name, "Char")) {
1077 if (!strcmp (name, "Double")) {
1079 class->blittable = TRUE;
1083 if (!strcmp (name, "Int32")) {
1085 class->blittable = TRUE;
1086 } else if (!strcmp(name, "Int16")) {
1088 class->blittable = TRUE;
1089 } else if (!strcmp(name, "Int64")) {
1091 class->blittable = TRUE;
1092 } else if (!strcmp(name, "IntPtr")) {
1094 class->blittable = TRUE;
1098 if (!strcmp (name, "Single")) {
1100 class->blittable = TRUE;
1101 } else if (!strcmp(name, "SByte")) {
1103 class->blittable = TRUE;
1107 if (!strcmp (name, "UInt32")) {
1109 class->blittable = TRUE;
1110 } else if (!strcmp(name, "UInt16")) {
1112 class->blittable = TRUE;
1113 } else if (!strcmp(name, "UInt64")) {
1115 class->blittable = TRUE;
1116 } else if (!strcmp(name, "UIntPtr")) {
1118 class->blittable = TRUE;
1122 if (!strcmp (name, "Void")) {
1130 class->this_arg.type = class->byval_arg.type = t;
1135 mono_class_setup_parent (MonoClass *class, MonoClass *parent)
1137 gboolean system_namespace;
1139 system_namespace = !strcmp (class->name_space, "System");
1141 /* if root of the hierarchy */
1142 if (system_namespace && !strcmp (class->name, "Object")) {
1143 class->parent = NULL;
1144 class->instance_size = sizeof (MonoObject);
1147 if (!strcmp (class->name, "<Module>")) {
1148 class->parent = NULL;
1149 class->instance_size = 0;
1153 if (!(class->flags & TYPE_ATTRIBUTE_INTERFACE)) {
1155 class->parent = parent;
1158 g_assert_not_reached (); /* FIXME */
1160 class->marshalbyref = parent->marshalbyref;
1161 class->contextbound = parent->contextbound;
1162 class->delegate = parent->delegate;
1164 if (system_namespace) {
1165 if (*class->name == 'M' && !strcmp (class->name, "MarshalByRefObject"))
1166 class->marshalbyref = 1;
1168 if (*class->name == 'C' && !strcmp (class->name, "ContextBoundObject"))
1169 class->contextbound = 1;
1171 if (*class->name == 'D' && !strcmp (class->name, "Delegate"))
1172 class->delegate = 1;
1175 if (class->parent->enumtype || ((strcmp (class->parent->name, "ValueType") == 0) &&
1176 (strcmp (class->parent->name_space, "System") == 0)))
1177 class->valuetype = 1;
1178 if (((strcmp (class->parent->name, "Enum") == 0) && (strcmp (class->parent->name_space, "System") == 0))) {
1179 class->valuetype = class->enumtype = 1;
1181 /*class->enumtype = class->parent->enumtype; */
1182 class->parent->subclasses = g_list_prepend (class->parent->subclasses, class);
1183 mono_compute_relative_numbering (mono_defaults.object_class, &rnum);
1185 class->parent = NULL;
1191 mono_class_setup_supertypes (MonoClass *class)
1197 for (k = class; k ; k = k->parent) {
1201 ms = MAX (MONO_DEFAULT_SUPERTABLE_SIZE, class->idepth);
1202 class->supertypes = g_new0 (MonoClass *, ms);
1204 if (class->parent) {
1205 for (i = class->idepth, k = class; k ; k = k->parent)
1206 class->supertypes [--i] = k;
1208 class->supertypes [0] = class;
1213 * @image: context where the image is created
1214 * @type_token: typedef token
1217 mono_class_create_from_typedef (MonoImage *image, guint32 type_token)
1219 MonoTableInfo *tt = &image->tables [MONO_TABLE_TYPEDEF];
1220 MonoClass *class, *parent = NULL;
1221 guint32 cols [MONO_TYPEDEF_SIZE];
1222 guint32 cols_next [MONO_TYPEDEF_SIZE];
1223 guint tidx = mono_metadata_token_index (type_token);
1224 const char *name, *nspace;
1226 MonoClass **interfaces;
1228 if ((class = g_hash_table_lookup (image->class_cache, GUINT_TO_POINTER (type_token))))
1231 g_assert (mono_metadata_token_table (type_token) == MONO_TABLE_TYPEDEF);
1233 mono_metadata_decode_row (tt, tidx - 1, cols, MONO_TYPEDEF_SIZE);
1235 name = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
1236 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
1238 if (cols [MONO_TYPEDEF_EXTENDS])
1239 parent = mono_class_get (image, mono_metadata_token_from_dor (cols [MONO_TYPEDEF_EXTENDS]));
1240 interfaces = mono_metadata_interfaces_from_typedef (image, type_token, &icount);
1242 class = g_malloc0 (sizeof (MonoClass));
1244 g_hash_table_insert (image->class_cache, GUINT_TO_POINTER (type_token), class);
1246 class->interfaces = interfaces;
1247 class->interface_count = icount;
1250 class->name_space = nspace;
1252 class->image = image;
1253 class->type_token = type_token;
1254 class->flags = cols [MONO_TYPEDEF_FLAGS];
1256 if ((class->flags & TYPE_ATTRIBUTE_STRING_FORMAT_MASK) == TYPE_ATTRIBUTE_UNICODE_CLASS)
1258 /* fixme: maybe we must set this on windows
1259 if ((class->flags & TYPE_ATTRIBUTE_STRING_FORMAT_MASK) == TYPE_ATTRIBUTE_AUTO_CLASS)
1263 class->cast_class = class->element_class = class;
1265 /*g_print ("Init class %s\n", name);*/
1267 mono_class_setup_parent (class, parent);
1269 mono_class_setup_mono_type (class);
1272 * Compute the field and method lists
1274 class->field.first = cols [MONO_TYPEDEF_FIELD_LIST] - 1;
1275 class->method.first = cols [MONO_TYPEDEF_METHOD_LIST] - 1;
1277 if (tt->rows > tidx){
1278 mono_metadata_decode_row (tt, tidx, cols_next, CSIZE (cols_next));
1279 class->field.last = cols_next [MONO_TYPEDEF_FIELD_LIST] - 1;
1280 class->method.last = cols_next [MONO_TYPEDEF_METHOD_LIST] - 1;
1282 class->field.last = image->tables [MONO_TABLE_FIELD].rows;
1283 class->method.last = image->tables [MONO_TABLE_METHOD].rows;
1286 if (cols [MONO_TYPEDEF_FIELD_LIST] &&
1287 cols [MONO_TYPEDEF_FIELD_LIST] <= image->tables [MONO_TABLE_FIELD].rows)
1288 class->field.count = class->field.last - class->field.first;
1290 class->field.count = 0;
1292 if (cols [MONO_TYPEDEF_METHOD_LIST] <= image->tables [MONO_TABLE_METHOD].rows)
1293 class->method.count = class->method.last - class->method.first;
1295 class->method.count = 0;
1297 /* reserve space to store vector pointer in arrays */
1298 if (!strcmp (nspace, "System") && !strcmp (name, "Array")) {
1299 class->instance_size += 2 * sizeof (gpointer);
1300 g_assert (class->field.count == 0);
1303 if (class->enumtype)
1304 class_compute_field_layout (class);
1306 if ((type_token = mono_metadata_nested_in_typedef (image, type_token)))
1307 class->nested_in = mono_class_create_from_typedef (image, type_token);
1313 mono_ptr_class_get (MonoType *type)
1316 MonoClass *el_class;
1317 static GHashTable *ptr_hash = NULL;
1320 ptr_hash = g_hash_table_new (g_direct_hash, g_direct_equal);
1321 el_class = mono_class_from_mono_type (type);
1322 if ((result = g_hash_table_lookup (ptr_hash, el_class)))
1324 result = g_new0 (MonoClass, 1);
1326 result->parent = NULL; /* no parent for PTR types */
1327 result->name = "System";
1328 result->name_space = "MonoPtrFakeClass";
1329 result->image = el_class->image;
1330 result->inited = TRUE;
1331 result->flags = TYPE_ATTRIBUTE_CLASS | (el_class->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK);
1332 /* Can pointers get boxed? */
1333 result->instance_size = sizeof (gpointer);
1335 * baseval, diffval: need them to allow casting ?
1337 result->cast_class = result->element_class = el_class;
1338 result->enum_basetype = &result->element_class->byval_arg;
1340 result->this_arg.type = result->byval_arg.type = MONO_TYPE_PTR;
1341 result->this_arg.data.type = result->byval_arg.data.type = result->enum_basetype;
1342 result->this_arg.byref = TRUE;
1344 g_hash_table_insert (ptr_hash, el_class, result);
1350 mono_fnptr_class_get (MonoMethodSignature *sig)
1353 static GHashTable *ptr_hash = NULL;
1356 ptr_hash = g_hash_table_new (g_direct_hash, g_direct_equal);
1358 if ((result = g_hash_table_lookup (ptr_hash, sig)))
1360 result = g_new0 (MonoClass, 1);
1362 result->parent = NULL; /* no parent for PTR types */
1363 result->name = "System";
1364 result->name_space = "MonoFNPtrFakeClass";
1365 result->image = NULL; /* need to fix... */
1366 result->inited = TRUE;
1367 result->flags = TYPE_ATTRIBUTE_CLASS; // | (el_class->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK);
1368 /* Can pointers get boxed? */
1369 result->instance_size = sizeof (gpointer);
1371 * baseval, diffval: need them to allow casting ?
1373 result->cast_class = result->element_class = result;
1375 result->this_arg.type = result->byval_arg.type = MONO_TYPE_FNPTR;
1376 result->this_arg.data.method = result->byval_arg.data.method = sig;
1377 result->this_arg.byref = TRUE;
1378 result->enum_basetype = &result->element_class->byval_arg;
1380 g_hash_table_insert (ptr_hash, sig, result);
1386 mono_class_from_mono_type (MonoType *type)
1388 switch (type->type) {
1389 case MONO_TYPE_OBJECT:
1390 return mono_defaults.object_class;
1391 case MONO_TYPE_VOID:
1392 return mono_defaults.void_class;
1393 case MONO_TYPE_BOOLEAN:
1394 return mono_defaults.boolean_class;
1395 case MONO_TYPE_CHAR:
1396 return mono_defaults.char_class;
1398 return mono_defaults.sbyte_class;
1400 return mono_defaults.byte_class;
1402 return mono_defaults.int16_class;
1404 return mono_defaults.uint16_class;
1406 return mono_defaults.int32_class;
1408 return mono_defaults.uint32_class;
1410 return mono_defaults.int_class;
1412 return mono_defaults.uint_class;
1414 return mono_defaults.int64_class;
1416 return mono_defaults.uint64_class;
1418 return mono_defaults.single_class;
1420 return mono_defaults.double_class;
1421 case MONO_TYPE_STRING:
1422 return mono_defaults.string_class;
1423 case MONO_TYPE_TYPEDBYREF:
1424 return mono_defaults.typed_reference_class;
1425 case MONO_TYPE_ARRAY:
1426 return mono_array_class_get (type->data.array->type, type->data.array->rank);
1428 return mono_ptr_class_get (type->data.type);
1429 case MONO_TYPE_FNPTR:
1430 return mono_fnptr_class_get (type->data.method);
1431 case MONO_TYPE_SZARRAY:
1432 return mono_array_class_get (type->data.type, 1);
1433 case MONO_TYPE_CLASS:
1434 case MONO_TYPE_VALUETYPE:
1435 return type->data.klass;
1437 g_warning ("implement me %02x\n", type->type);
1438 g_assert_not_reached ();
1445 * @image: context where the image is created
1446 * @type_spec: typespec token
1449 mono_class_create_from_typespec (MonoImage *image, guint32 type_spec)
1454 type = mono_type_create_from_typespec (image, type_spec);
1456 switch (type->type) {
1457 case MONO_TYPE_ARRAY:
1458 class = mono_array_class_get (type->data.array->type, type->data.array->rank);
1460 case MONO_TYPE_SZARRAY:
1461 class = mono_array_class_get (type->data.type, 1);
1464 class = mono_class_from_mono_type (type->data.type);
1467 /* it seems any type can be stored in TypeSpec as well */
1468 class = mono_class_from_mono_type (type);
1472 mono_metadata_free_type (type);
1478 * mono_array_class_get:
1479 * @element_type: element type
1480 * @rank: the dimension of the array class
1482 * Returns: a class object describing the array with element type @element_type and
1486 mono_array_class_get (MonoType *element_type, guint32 rank)
1491 MonoClass *parent = NULL;
1493 int rnum = 0, nsize;
1496 eclass = mono_class_from_mono_type (element_type);
1497 g_assert (rank <= 255);
1499 parent = mono_defaults.array_class;
1501 if (!parent->inited)
1502 mono_class_init (parent);
1504 image = eclass->image;
1506 if ((list = g_hash_table_lookup (image->array_cache, element_type))) {
1507 for (; list; list = list->next) {
1509 if (class->rank == rank)
1514 class = g_malloc0 (sizeof (MonoClass) + parent->vtable_size * sizeof (gpointer));
1516 class->image = image;
1517 class->name_space = eclass->name_space;
1518 nsize = strlen (eclass->name);
1519 name = g_malloc (nsize + 2 + rank);
1520 memcpy (name, eclass->name, nsize);
1523 memset (name + nsize + 1, ',', rank - 1);
1524 name [nsize + rank] = ']';
1525 name [nsize + rank + 1] = 0;
1527 class->type_token = 0;
1528 class->flags = TYPE_ATTRIBUTE_CLASS | (eclass->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK);
1529 class->parent = parent;
1530 class->instance_size = mono_class_instance_size (class->parent);
1531 class->class_size = 0;
1532 class->vtable_size = parent->vtable_size;
1533 class->parent->subclasses = g_list_prepend (class->parent->subclasses, class);
1534 mono_compute_relative_numbering (mono_defaults.object_class, &rnum);
1538 if (eclass->enumtype)
1539 class->cast_class = eclass->element_class;
1541 class->cast_class = eclass;
1543 class->element_class = eclass;
1546 MonoArrayType *at = g_new0 (MonoArrayType, 1);
1547 class->byval_arg.type = MONO_TYPE_ARRAY;
1548 class->byval_arg.data.array = at;
1549 at->type = &eclass->byval_arg;
1551 /* FIXME: complete.... */
1553 /* FIXME: this is not correct. the lbound could be >0 */
1554 class->byval_arg.type = MONO_TYPE_SZARRAY;
1555 class->byval_arg.data.type = &eclass->byval_arg;
1557 class->this_arg = class->byval_arg;
1558 class->this_arg.byref = 1;
1560 list = g_slist_append (list, class);
1561 g_hash_table_insert (image->array_cache, &class->element_class->byval_arg, list);
1566 * mono_class_instance_size:
1569 * Returns: the size of an object instance
1572 mono_class_instance_size (MonoClass *klass)
1575 if (!klass->size_inited)
1576 mono_class_init (klass);
1578 return klass->instance_size;
1582 * mono_class_native_size:
1585 * Returns: the native size of an object instance (when marshaled
1586 * to unmanaged code)
1589 mono_class_native_size (MonoClass *klass, guint32 *align)
1592 if (!klass->marshal_info)
1593 mono_marshal_load_type_info (klass);
1596 *align = klass->min_align;
1598 return klass->marshal_info->native_size;
1602 * mono_class_min_align:
1605 * Returns: minimm alignment requirements
1608 mono_class_min_align (MonoClass *klass)
1611 if (!klass->size_inited)
1612 mono_class_init (klass);
1614 return klass->min_align;
1618 * mono_class_value_size:
1621 * This function is used for value types, and return the
1622 * space and the alignment to store that kind of value object.
1624 * Returns: the size of a value of kind @klass
1627 mono_class_value_size (MonoClass *klass, guint32 *align)
1631 /* fixme: check disable, because we still have external revereces to
1632 * mscorlib and Dummy Objects
1634 /*g_assert (klass->valuetype);*/
1636 size = mono_class_instance_size (klass) - sizeof (MonoObject);
1639 *align = klass->min_align;
1645 * mono_class_data_size:
1648 * Returns: the size of the static class data
1651 mono_class_data_size (MonoClass *klass)
1655 mono_class_init (klass);
1657 return klass->class_size;
1661 * Auxiliary routine to mono_class_get_field
1663 * Takes a field index instead of a field token.
1665 static MonoClassField *
1666 mono_class_get_field_idx (MonoClass *class, int idx)
1668 if (class->field.count){
1669 if ((idx >= class->field.first) && (idx < class->field.last)){
1670 return &class->fields [idx - class->field.first];
1677 return mono_class_get_field_idx (class->parent, idx);
1681 * mono_class_get_field:
1682 * @class: the class to lookup the field.
1683 * @field_token: the field token
1685 * Returns: A MonoClassField representing the type and offset of
1686 * the field, or a NULL value if the field does not belong to this
1690 mono_class_get_field (MonoClass *class, guint32 field_token)
1692 int idx = mono_metadata_token_index (field_token);
1694 g_assert (mono_metadata_token_code (field_token) == MONO_TOKEN_FIELD_DEF);
1696 return mono_class_get_field_idx (class, idx - 1);
1700 mono_class_get_field_from_name (MonoClass *klass, const char *name)
1705 for (i = 0; i < klass->field.count; ++i) {
1706 if (strcmp (name, klass->fields [i].name) == 0)
1707 return &klass->fields [i];
1709 klass = klass->parent;
1715 mono_class_get_property_from_name (MonoClass *klass, const char *name)
1720 for (i = 0; i < klass->property.count; ++i) {
1721 if (strcmp (name, klass->properties [i].name) == 0)
1722 return &klass->properties [i];
1724 klass = klass->parent;
1731 * @image: the image where the class resides
1732 * @type_token: the token for the class
1733 * @at: an optional pointer to return the array element type
1735 * Returns: the MonoClass that represents @type_token in @image
1738 mono_class_get (MonoImage *image, guint32 type_token)
1742 if (image->assembly->dynamic)
1743 return mono_lookup_dynamic_token (image, type_token);
1745 switch (type_token & 0xff000000){
1746 case MONO_TOKEN_TYPE_DEF:
1747 class = mono_class_create_from_typedef (image, type_token);
1749 case MONO_TOKEN_TYPE_REF:
1750 class = mono_class_from_typeref (image, type_token);
1752 case MONO_TOKEN_TYPE_SPEC:
1753 class = mono_class_create_from_typespec (image, type_token);
1756 g_warning ("unknown token type %x", type_token & 0xff000000);
1757 g_assert_not_reached ();
1761 g_warning ("Could not load class from token 0x%08x in %s", type_token, image->name);
1767 mono_class_from_name_case (MonoImage *image, const char* name_space, const char *name)
1769 MonoTableInfo *t = &image->tables [MONO_TABLE_TYPEDEF];
1770 guint32 cols [MONO_TYPEDEF_SIZE];
1775 /* add a cache if needed */
1776 for (i = 1; i <= t->rows; ++i) {
1777 mono_metadata_decode_row (t, i - 1, cols, MONO_TYPEDEF_SIZE);
1778 /* nested types are accessed from the nesting name */
1779 visib = cols [MONO_TYPEDEF_FLAGS] & TYPE_ATTRIBUTE_VISIBILITY_MASK;
1780 if (visib > TYPE_ATTRIBUTE_PUBLIC && visib <= TYPE_ATTRIBUTE_NESTED_ASSEMBLY)
1782 n = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
1783 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
1784 if (g_strcasecmp (n, name) == 0 && g_strcasecmp (nspace, name_space) == 0)
1785 return mono_class_get (image, MONO_TOKEN_TYPE_DEF | i);
1791 load_file_for_image (MonoImage *image, int fileidx)
1793 char *base_dir, *name;
1795 MonoTableInfo *t = &image->tables [MONO_TABLE_FILE];
1799 if (fileidx < 1 || fileidx > t->rows)
1801 fname_id = mono_metadata_decode_row_col (t, fileidx - 1, MONO_FILE_NAME);
1802 fname = mono_metadata_string_heap (image, fname_id);
1803 base_dir = g_path_get_dirname (image->name);
1804 name = g_build_filename (base_dir, fname, NULL);
1805 res = mono_image_open (name, NULL);
1808 t = &res->tables [MONO_TABLE_MODULEREF];
1809 //g_print ("loaded file %s from %s (%p)\n", name, image->name, image->assembly);
1810 res->assembly = image->assembly;
1811 for (i = 0; i < t->rows; ++i) {
1812 if (res->modules [i] && !res->modules [i]->assembly)
1813 res->modules [i]->assembly = image->assembly;
1815 mono_image_load_references (image, NULL);
1823 return_nested_in (MonoClass *class, char *nested) {
1825 char *s = strchr (nested, '/');
1832 for (tmp = class->nested_classes; tmp; tmp = tmp->next) {
1834 if (strcmp (found->name, nested) == 0) {
1836 return return_nested_in (found, s);
1844 mono_class_from_name (MonoImage *image, const char* name_space, const char *name)
1846 GHashTable *nspace_table;
1847 MonoImage *loaded_image;
1853 if ((nested = strchr (name, '/'))) {
1854 int pos = nested - name;
1855 int len = strlen (name);
1858 memcpy (buf, name, len + 1);
1860 nested = buf + pos + 1;
1864 nspace_table = g_hash_table_lookup (image->name_cache, name_space);
1866 if (!nspace_table || !(token = GPOINTER_TO_UINT (g_hash_table_lookup (nspace_table, name)))) {
1867 MonoTableInfo *t = &image->tables [MONO_TABLE_EXPORTEDTYPE];
1868 guint32 cols [MONO_EXP_TYPE_SIZE];
1871 for (i = 0; i < t->rows; ++i) {
1872 const char *ename, *enspace;
1873 mono_metadata_decode_row (t, i, cols, MONO_EXP_TYPE_SIZE);
1874 ename = mono_metadata_string_heap (image, cols [MONO_EXP_TYPE_NAME]);
1875 enspace = mono_metadata_string_heap (image, cols [MONO_EXP_TYPE_NAMESPACE]);
1877 if (strcmp (name, ename) == 0 && strcmp (name_space, enspace) == 0) {
1878 guint32 impl = cols [MONO_EXP_TYPE_IMPLEMENTATION];
1879 if ((impl & IMPLEMENTATION_MASK) == IMPLEMENTATION_FILE) {
1880 loaded_image = load_file_for_image (image, impl >> IMPLEMENTATION_BITS);
1883 class = mono_class_from_name (loaded_image, name_space, name);
1885 return return_nested_in (class, nested);
1888 g_error ("not yet implemented");
1892 /*g_warning ("token not found for %s.%s in image %s", name_space, name, image->name);*/
1896 token = MONO_TOKEN_TYPE_DEF | token;
1898 class = mono_class_get (image, token);
1900 return return_nested_in (class, nested);
1905 * Returns the nnumber of bytes an element of type klass
1906 * uses when stored into an array.
1909 mono_class_array_element_size (MonoClass *klass)
1911 int t = klass->byval_arg.type;
1917 case MONO_TYPE_BOOLEAN:
1921 case MONO_TYPE_CHAR:
1930 case MONO_TYPE_CLASS:
1931 case MONO_TYPE_STRING:
1932 case MONO_TYPE_OBJECT:
1933 case MONO_TYPE_SZARRAY:
1934 case MONO_TYPE_ARRAY:
1935 return sizeof (gpointer);
1940 case MONO_TYPE_VALUETYPE:
1941 if (klass->enumtype) {
1942 t = klass->enum_basetype->type;
1945 return mono_class_instance_size (klass) - sizeof (MonoObject);
1947 g_error ("unknown type 0x%02x in mono_class_array_element_size", t);
1953 * mono_array_element_size:
1954 * @ac: pointer to a #MonoArrayClass
1956 * Returns: the size of single array element.
1959 mono_array_element_size (MonoClass *ac)
1961 return mono_class_array_element_size (ac->element_class);
1965 mono_ldtoken (MonoImage *image, guint32 token, MonoClass **handle_class)
1967 if (image->assembly->dynamic) {
1968 gpointer obj = mono_lookup_dynamic_token (image, token);
1970 switch (token & 0xff000000) {
1971 case MONO_TOKEN_TYPE_DEF:
1972 case MONO_TOKEN_TYPE_REF:
1973 case MONO_TOKEN_TYPE_SPEC:
1975 *handle_class = mono_defaults.typehandle_class;
1976 return &((MonoClass*)obj)->byval_arg;
1977 case MONO_TOKEN_METHOD_DEF:
1979 *handle_class = mono_defaults.methodhandle_class;
1981 case MONO_TOKEN_FIELD_DEF:
1983 *handle_class = mono_defaults.fieldhandle_class;
1986 g_assert_not_reached ();
1990 switch (token & 0xff000000) {
1991 case MONO_TOKEN_TYPE_DEF:
1992 case MONO_TOKEN_TYPE_REF: {
1995 *handle_class = mono_defaults.typehandle_class;
1996 class = mono_class_get (image, token);
1997 mono_class_init (class);
1998 /* We return a MonoType* as handle */
1999 return &class->byval_arg;
2001 case MONO_TOKEN_TYPE_SPEC: {
2004 *handle_class = mono_defaults.typehandle_class;
2005 class = mono_class_create_from_typespec (image, token);
2006 mono_class_init (class);
2007 return &class->byval_arg;
2009 case MONO_TOKEN_FIELD_DEF: {
2011 guint32 type = mono_metadata_typedef_from_field (image, mono_metadata_token_index (token));
2012 class = mono_class_get (image, MONO_TOKEN_TYPE_DEF | type);
2013 mono_class_init (class);
2015 *handle_class = mono_defaults.fieldhandle_class;
2016 return mono_class_get_field (class, token);
2018 case MONO_TOKEN_METHOD_DEF:
2019 case MONO_TOKEN_MEMBER_REF:
2021 g_warning ("Unknown token 0x%08x in ldtoken", token);
2028 * This function might need to call runtime functions so it can't be part
2029 * of the metadata library.
2031 static MonoLookupDynamicToken lookup_dynamic = NULL;
2034 mono_install_lookup_dynamic_token (MonoLookupDynamicToken func)
2036 lookup_dynamic = func;
2040 mono_lookup_dynamic_token (MonoImage *image, guint32 token)
2042 return lookup_dynamic (image, token);