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>
32 #define CSIZE(x) (sizeof (x) / 4)
34 gboolean mono_print_vtable = FALSE;
36 static MonoClass * mono_class_create_from_typedef (MonoImage *image, guint32 type_token);
39 default_trampoline (MonoMethod *method)
45 default_runtime_class_init (MonoClass *klass)
50 static MonoTrampoline arch_create_jit_trampoline = default_trampoline;
51 static MonoRuntimeClassInit mono_runtime_class_init = default_runtime_class_init;
54 mono_install_trampoline (MonoTrampoline func)
56 arch_create_jit_trampoline = func? func: default_trampoline;
60 mono_install_runtime_class_init (MonoRuntimeClassInit func)
62 mono_runtime_class_init = func? func: default_runtime_class_init;
66 mono_class_create_from_typeref (MonoImage *image, guint32 type_token)
68 guint32 cols [MONO_TYPEREF_SIZE];
69 MonoTableInfo *t = &image->tables [MONO_TABLE_TYPEREF];
71 const char *name, *nspace;
74 mono_metadata_decode_row (t, (type_token&0xffffff)-1, cols, MONO_TYPEREF_SIZE);
75 g_assert ((cols [MONO_TYPEREF_SCOPE] & 0x3) == 2);
76 idx = cols [MONO_TYPEREF_SCOPE] >> 2;
78 if (!image->references || !image->references [idx-1]) {
80 * detected a reference to mscorlib, we simply return a reference to a dummy
81 * until we have a better solution.
83 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]));
85 res = mono_class_from_name (image, "System", "MonoDummy");
86 /* prevent method loading */
88 /* some storage if the type is used - very ugly hack */
89 res->instance_size = 2*sizeof (gpointer);
93 name = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAME]);
94 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAMESPACE]);
96 /* load referenced assembly */
97 image = image->references [idx-1]->image;
99 return mono_class_from_name (image, nspace, name);
103 * class_compute_field_layout:
104 * @m: pointer to the metadata.
105 * @class: The class to initialize
107 * Initializes the class->fields.
109 * Currently we only support AUTO_LAYOUT, and do not even try to do
110 * a good job at it. This is temporary to get the code for Paolo.
113 class_compute_field_layout (MonoClass *class)
115 MonoImage *m = class->image;
116 const int top = class->field.count;
117 guint32 layout = class->flags & TYPE_ATTRIBUTE_LAYOUT_MASK;
118 MonoTableInfo *t = &m->tables [MONO_TABLE_FIELD];
122 * Fetch all the field information.
124 for (i = 0; i < top; i++){
126 guint32 cols [MONO_FIELD_SIZE];
127 int idx = class->field.first + i;
129 mono_metadata_decode_row (t, idx, cols, CSIZE (cols));
130 /* The name is needed for fieldrefs */
131 class->fields [i].name = mono_metadata_string_heap (m, cols [MONO_FIELD_NAME]);
132 sig = mono_metadata_blob_heap (m, cols [MONO_FIELD_SIGNATURE]);
133 mono_metadata_decode_value (sig, &sig);
134 /* FIELD signature == 0x06 */
135 g_assert (*sig == 0x06);
136 class->fields [i].type = mono_metadata_parse_field_type (
137 m, cols [MONO_FIELD_FLAGS], sig + 1, &sig);
138 if (cols [MONO_FIELD_FLAGS] & FIELD_ATTRIBUTE_HAS_FIELD_RVA) {
139 mono_metadata_field_info (m, idx, NULL, &class->fields [i].data, NULL);
140 if (!class->fields [i].data)
141 g_warning ("field %s in %s should have RVA data, but hasn't", class->fields [i].name, class->name);
143 if (class->enumtype && !(cols [MONO_FIELD_FLAGS] & FIELD_ATTRIBUTE_STATIC)) {
144 class->enum_basetype = class->fields [i].type;
145 class->element_class = mono_class_from_mono_type (class->enum_basetype);
148 if (class->enumtype && !class->enum_basetype) {
149 if (!((strcmp (class->name, "Enum") == 0) && (strcmp (class->name_space, "System") == 0)))
153 * Compute field layout and total size (not considering static fields)
156 case TYPE_ATTRIBUTE_AUTO_LAYOUT:
157 case TYPE_ATTRIBUTE_SEQUENTIAL_LAYOUT:
158 for (i = 0; i < top; i++){
161 if (class->fields [i].type->attrs & FIELD_ATTRIBUTE_STATIC)
164 size = mono_type_size (class->fields [i].type, &align);
166 class->min_align = MAX (align, class->min_align);
167 class->fields [i].offset = class->instance_size;
168 class->fields [i].offset += align - 1;
169 class->fields [i].offset &= ~(align - 1);
170 class->instance_size = class->fields [i].offset + size;
173 if (class->instance_size & (class->min_align - 1)) {
174 class->instance_size += class->min_align - 1;
175 class->instance_size &= ~(class->min_align - 1);
178 case TYPE_ATTRIBUTE_EXPLICIT_LAYOUT:
179 for (i = 0; i < top; i++) {
181 int idx = class->field.first + i;
184 * There must be info about all the fields in a type if it
185 * uses explicit layout.
188 if (class->fields [i].type->attrs & FIELD_ATTRIBUTE_STATIC)
191 size = mono_type_size (class->fields [i].type, &align);
193 mono_metadata_field_info (m, idx, &class->fields [i].offset, NULL, NULL);
194 if (class->fields [i].offset == (guint32)-1)
195 g_warning ("%s not initialized correctly (missing field layout info for %s)", class->name, class->fields [i].name);
197 * The offset is from the start of the object: this works for both
198 * classes and valuetypes.
200 class->fields [i].offset += sizeof (MonoObject);
204 size += class->fields [i].offset;
205 class->instance_size = MAX (class->instance_size, size);
210 class->size_inited = 1;
213 * Compute static field layout and size
216 case TYPE_ATTRIBUTE_AUTO_LAYOUT:
217 case TYPE_ATTRIBUTE_SEQUENTIAL_LAYOUT:
218 for (i = 0; i < top; i++){
221 if (!(class->fields [i].type->attrs & FIELD_ATTRIBUTE_STATIC))
224 size = mono_type_size (class->fields [i].type, &align);
225 class->fields [i].offset = class->class_size;
226 class->fields [i].offset += align - 1;
227 class->fields [i].offset &= ~(align - 1);
228 class->class_size = class->fields [i].offset + size;
231 case TYPE_ATTRIBUTE_EXPLICIT_LAYOUT:
232 for (i = 0; i < top; i++){
234 int idx = class->field.first + i;
237 * There must be info about all the fields in a type if it
238 * uses explicit layout.
242 if (!(class->fields [i].type->attrs & FIELD_ATTRIBUTE_STATIC))
245 size = mono_type_size (class->fields [i].type, &align);
246 class->fields [i].offset = class->class_size;
247 class->fields [i].offset += align - 1;
248 class->fields [i].offset &= ~(align - 1);
249 class->class_size = class->fields [i].offset + size;
256 init_properties (MonoClass *class)
258 guint startm, endm, i, j;
259 guint32 cols [MONO_PROPERTY_SIZE];
260 MonoTableInfo *pt = &class->image->tables [MONO_TABLE_PROPERTY];
261 MonoTableInfo *msemt = &class->image->tables [MONO_TABLE_METHODSEMANTICS];
263 class->property.first = mono_metadata_properties_from_typedef (class->image, mono_metadata_token_index (class->type_token) - 1, &class->property.last);
264 class->property.count = class->property.last - class->property.first;
266 class->properties = g_new0 (MonoProperty, class->property.count);
267 for (i = class->property.first; i < class->property.last; ++i) {
268 mono_metadata_decode_row (pt, i, cols, MONO_PROPERTY_SIZE);
269 class->properties [i - class->property.first].attrs = cols [MONO_PROPERTY_FLAGS];
270 class->properties [i - class->property.first].name = mono_metadata_string_heap (class->image, cols [MONO_PROPERTY_NAME]);
272 startm = mono_metadata_methods_from_property (class->image, i, &endm);
273 for (j = startm; j < endm; ++j) {
274 mono_metadata_decode_row (msemt, j, cols, MONO_METHOD_SEMA_SIZE);
275 switch (cols [MONO_METHOD_SEMA_SEMANTICS]) {
276 case METHOD_SEMANTIC_SETTER:
277 class->properties [i - class->property.first].set = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
279 case METHOD_SEMANTIC_GETTER:
280 class->properties [i - class->property.first].get = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
290 init_events (MonoClass *class)
292 guint startm, endm, i, j;
293 guint32 cols [MONO_EVENT_SIZE];
294 MonoTableInfo *pt = &class->image->tables [MONO_TABLE_EVENT];
295 MonoTableInfo *msemt = &class->image->tables [MONO_TABLE_METHODSEMANTICS];
297 class->event.first = mono_metadata_events_from_typedef (class->image, mono_metadata_token_index (class->type_token) - 1, &class->event.last);
298 class->event.count = class->event.last - class->event.first;
300 class->events = g_new0 (MonoEvent, class->event.count);
301 for (i = class->event.first; i < class->event.last; ++i) {
302 mono_metadata_decode_row (pt, i, cols, MONO_EVENT_SIZE);
303 class->events [i - class->event.first].attrs = cols [MONO_EVENT_FLAGS];
304 class->events [i - class->event.first].name = mono_metadata_string_heap (class->image, cols [MONO_EVENT_NAME]);
306 startm = mono_metadata_methods_from_event (class->image, i, &endm);
307 for (j = startm; j < endm; ++j) {
308 mono_metadata_decode_row (msemt, j, cols, MONO_METHOD_SEMA_SIZE);
309 switch (cols [MONO_METHOD_SEMA_SEMANTICS]) {
310 case METHOD_SEMANTIC_ADD_ON:
311 class->events [i - class->event.first].add = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
313 case METHOD_SEMANTIC_REMOVE_ON:
314 class->events [i - class->event.first].remove = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
316 case METHOD_SEMANTIC_FIRE:
317 class->events [i - class->event.first].raise = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
319 case METHOD_SEMANTIC_OTHER: /* don't care for now */
328 mono_get_unique_iid (MonoClass *class)
330 static GHashTable *iid_hash = NULL;
331 static guint iid = 0;
336 g_assert (class->flags & TYPE_ATTRIBUTE_INTERFACE);
339 iid_hash = g_hash_table_new (g_str_hash, g_str_equal);
341 str = g_strdup_printf ("%s|%s.%s\n", class->image->name, class->name_space, class->name);
343 if (g_hash_table_lookup_extended (iid_hash, str, NULL, &value)) {
347 g_hash_table_insert (iid_hash, str, (gpointer)iid);
356 * @class: the class to initialize
358 * compute the instance_size, class_size and other infos that cannot be
359 * computed at mono_class_get() time. Also compute a generic vtable and
360 * the method slot numbers. We use this infos later to create a domain
364 mono_class_init (MonoClass *class)
367 MonoMethod **vtable = class->vtable;
368 int i, max_iid, cur_slot = 0;
369 static MonoMethod *default_ghc = NULL;
370 static int ghc_slot = -1;
377 if (class->init_pending) {
378 /* this indicates a cyclic dependency */
379 g_error ("pending init %s.%s\n", class->name_space, class->name);
382 class->init_pending = 1;
385 if (!class->parent->inited)
386 mono_class_init (class->parent);
387 class->instance_size += class->parent->instance_size;
388 class->class_size += class->parent->class_size;
389 class->min_align = class->parent->min_align;
390 cur_slot = class->parent->vtable_size;
392 class->min_align = 1;
395 * Computes the size used by the fields, and their locations
397 if (!class->size_inited && class->field.count > 0){
398 class->fields = g_new0 (MonoClassField, class->field.count);
399 class_compute_field_layout (class);
402 /* initialize method pointers */
403 class->methods = g_new (MonoMethod*, class->method.count);
404 for (i = 0; i < class->method.count; ++i)
405 class->methods [i] = mono_get_method (class->image,
406 MONO_TOKEN_METHOD_DEF | (i + class->method.first + 1), class);
408 if (class->flags & TYPE_ATTRIBUTE_INTERFACE) {
409 for (i = 0; i < class->method.count; ++i)
410 class->methods [i]->slot = i;
411 class->init_pending = 0;
416 //printf ("METAINIT %s.%s\n", class->name_space, class->name);
418 /* compute maximum number of slots and maximum interface id */
420 for (k = class; k ; k = k->parent) {
421 for (i = 0; i < k->interface_count; i++) {
422 ic = k->interfaces [i];
425 mono_class_init (ic);
427 if (max_iid < ic->interface_id)
428 max_iid = ic->interface_id;
432 class->max_interface_id = max_iid;
433 /* compute vtable offset for interfaces */
434 class->interface_offsets = g_malloc (sizeof (gpointer) * (max_iid + 1));
436 for (i = 0; i <= max_iid; i++)
437 class->interface_offsets [i] = -1;
439 for (i = 0; i < class->interface_count; i++) {
440 ic = class->interfaces [i];
441 class->interface_offsets [ic->interface_id] = cur_slot;
442 cur_slot += ic->method.count;
445 for (k = class->parent; k ; k = k->parent) {
446 for (i = 0; i < k->interface_count; i++) {
447 ic = k->interfaces [i];
448 if (class->interface_offsets [ic->interface_id] == -1) {
449 int io = k->interface_offsets [ic->interface_id];
452 g_assert (io <= class->vtable_size);
454 class->interface_offsets [ic->interface_id] = io;
459 if (class->parent && class->parent->vtable_size)
460 memcpy (class->vtable, class->parent->vtable, sizeof (gpointer) * class->parent->vtable_size);
462 for (k = class; k ; k = k->parent) {
463 for (i = 0; i < k->interface_count; i++) {
466 ic = k->interfaces [i];
467 io = k->interface_offsets [ic->interface_id];
470 g_assert (io <= class->vtable_size);
473 for (l = 0; l < ic->method.count; l++) {
474 MonoMethod *im = ic->methods [l];
475 for (j = 0; j < class->method.count; ++j) {
476 MonoMethod *cm = class->methods [j];
477 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
478 !(cm->flags & METHOD_ATTRIBUTE_PUBLIC) ||
479 !(cm->flags & METHOD_ATTRIBUTE_NEW_SLOT))
481 if (!strcmp(cm->name, im->name) &&
482 mono_metadata_signature_equal (cm->signature, im->signature)) {
483 g_assert (io + l <= class->vtable_size);
484 vtable [io + l] = cm;
489 /* already implemented */
490 if (io >= k->vtable_size)
494 for (l = 0; l < ic->method.count; l++) {
495 MonoMethod *im = ic->methods [l];
498 g_assert (io + l <= class->vtable_size);
503 for (k1 = class; k1; k1 = k1->parent) {
504 for (j = 0; j < k1->method.count; ++j) {
505 MonoMethod *cm = k1->methods [j];
507 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
508 !(cm->flags & METHOD_ATTRIBUTE_PUBLIC))
511 if (!strcmp(cm->name, im->name) &&
512 mono_metadata_signature_equal (cm->signature, im->signature)) {
513 g_assert (io + l <= class->vtable_size);
514 vtable [io + l] = cm;
519 g_assert (io + l <= class->vtable_size);
525 for (l = 0; l < ic->method.count; l++) {
526 MonoMethod *im = ic->methods [l];
528 if (ic->name_space && ic->name_space [0])
529 qname = g_strconcat (ic->name_space, ".", ic->name, ".", im->name, NULL);
531 qname = g_strconcat (ic->name, ".", im->name, NULL);
533 for (j = 0; j < class->method.count; ++j) {
534 MonoMethod *cm = class->methods [j];
536 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL))
539 if (!strcmp (cm->name, qname) &&
540 mono_metadata_signature_equal (cm->signature, im->signature)) {
541 g_assert (io + l <= class->vtable_size);
542 vtable [io + l] = cm;
550 if (!(class->flags & TYPE_ATTRIBUTE_ABSTRACT)) {
551 for (l = 0; l < ic->method.count; l++) {
552 MonoMethod *im = ic->methods [l];
553 g_assert (io + l <= class->vtable_size);
554 if (!(vtable [io + l])) {
555 printf ("no implementation for interface method %s.%s::%s in class %s.%s\n",
556 ic->name_space, ic->name, im->name, class->name_space, class->name);
558 for (j = 0; j < class->method.count; ++j) {
559 MonoMethod *cm = class->methods [j];
561 printf ("METHOD %s\n", cm->name);
563 g_assert_not_reached ();
568 for (l = 0; l < ic->method.count; l++) {
569 MonoMethod *im = vtable [io + l];
572 g_assert (io + l <= class->vtable_size);
574 // fixme: why do we need this ?
576 // g_assert_not_reached ();
583 for (i = 0; i < class->method.count; ++i) {
586 cm = class->methods [i];
588 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
592 if (!(cm->flags & METHOD_ATTRIBUTE_NEW_SLOT)) {
593 for (k = class->parent; k ; k = k->parent) {
595 for (j = 0; j < k->method.count; ++j) {
596 MonoMethod *m1 = k->methods [j];
597 if (!(m1->flags & METHOD_ATTRIBUTE_VIRTUAL))
599 if (!strcmp(cm->name, m1->name) &&
600 mono_metadata_signature_equal (cm->signature, m1->signature)) {
601 cm->slot = k->methods [j]->slot;
602 g_assert (cm->slot < class->vtable_size);
612 cm->slot = cur_slot++;
614 if (!(cm->flags & METHOD_ATTRIBUTE_ABSTRACT))
615 vtable [cm->slot] = cm;
618 init_properties (class);
621 i = mono_metadata_nesting_typedef (class->image, class->type_token);
624 guint32 cols [MONO_NESTED_CLASS_SIZE];
625 mono_metadata_decode_row (&class->image->tables [MONO_TABLE_NESTEDCLASS], i - 1, cols, MONO_NESTED_CLASS_SIZE);
626 if (cols [MONO_NESTED_CLASS_ENCLOSING] != mono_metadata_token_index (class->type_token))
628 nclass = mono_class_create_from_typedef (class->image, MONO_TOKEN_TYPE_DEF | cols [MONO_NESTED_CLASS_NESTED]);
629 class->nested_classes = g_list_prepend (class->nested_classes, nclass);
633 if (mono_print_vtable) {
636 for (i = 0; i <= max_iid; i++)
637 if (class->interface_offsets [i] != -1)
640 printf ("VTable %s.%s (size = %d, interfaces = %d)\n", class->name_space,
641 class->name, class->vtable_size, icount);
643 for (i = 0; i < class->vtable_size; ++i) {
648 printf (" slot %03d(%03d) %s.%s:%s\n", i, cm->slot,
649 cm->klass->name_space, cm->klass->name,
656 printf ("Interfaces %s.%s (max_iid = %d)\n", class->name_space,
657 class->name, max_iid);
659 for (i = 0; i < class->interface_count; i++) {
660 ic = class->interfaces [i];
661 printf (" slot %03d(%03d) %s.%s\n",
662 class->interface_offsets [ic->interface_id],
663 ic->method.count, ic->name_space, ic->name);
666 for (k = class->parent; k ; k = k->parent) {
667 for (i = 0; i < k->interface_count; i++) {
668 ic = k->interfaces [i];
669 printf (" slot %03d(%03d) %s.%s\n",
670 class->interface_offsets [ic->interface_id],
671 ic->method.count, ic->name_space, ic->name);
678 class->init_pending = 0;
681 if (class == mono_defaults.object_class) {
683 for (i = 0; i < class->vtable_size; ++i) {
684 MonoMethod *cm = vtable [i];
686 if (!strcmp (cm->name, "GetHashCode")) {
692 g_assert (ghc_slot > 0);
694 default_ghc = vtable [ghc_slot];
699 if (class != mono_defaults.object_class) {
701 if (vtable [ghc_slot] == default_ghc) {
710 * @domain: the application domain
711 * @class: the class to initialize
713 * VTables are domain specific because we create domain specific code, and
714 * they contain the domain specific static class data.
717 mono_class_vtable (MonoDomain *domain, MonoClass *class)
721 MonoClassField *field;
723 guint32 cols [MONO_CONSTANT_SIZE];
730 /* can interfaces have static fields? */
731 if (class->flags & TYPE_ATTRIBUTE_INTERFACE)
732 g_assert_not_reached ();
734 if ((vt = g_hash_table_lookup (domain->class_vtable_hash, class)))
738 mono_class_init (class);
740 vt = g_malloc0 (sizeof (MonoVTable) + class->vtable_size * sizeof (gpointer));
744 if (class->class_size) {
745 vt->data = g_malloc0 (class->class_size + 8);
746 /* align: fixme not 64 bit clean */
747 if (((guint32)vt->data) & 0x7)
748 vt->data += 8 - (((guint32)vt->data) & 0x7);
751 for (i = class->field.first; i < class->field.last; ++i) {
752 field = &class->fields [i - class->field.first];
753 if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC))
755 if (!(field->type->attrs & FIELD_ATTRIBUTE_HAS_DEFAULT))
757 cindex = mono_metadata_get_constant_index (class->image, MONO_TOKEN_FIELD_DEF | (i + 1));
759 g_warning ("constant for field %s not found", field->name);
762 mono_metadata_decode_row (&class->image->tables [MONO_TABLE_CONSTANT], cindex - 1, cols, MONO_CONSTANT_SIZE);
763 p = mono_metadata_blob_heap (class->image, cols [MONO_CONSTANT_VALUE]);
764 mono_metadata_decode_blob_size (p, &p);
765 t = (char*)vt->data + field->offset;
766 /* should we check that the type matches? */
767 switch (cols [MONO_CONSTANT_TYPE]) {
768 case MONO_TYPE_BOOLEAN:
776 guint16 *val = (guint16*)t;
782 guint32 *val = (guint32*)t;
788 guint64 *val = (guint64*)t;
793 float *val = (float*)t;
798 double *val = (double*)t;
802 case MONO_TYPE_STRING: {
803 g_warning ("we don't handle strings in constant table");
806 case MONO_TYPE_CLASS:
807 /* nothing to do, we malloc0 the data and the value can be 0 only */
810 g_warning ("type 0x%02x should not be in constant table", cols [MONO_CONSTANT_TYPE]);
814 vt->interface_offsets = g_malloc0 (sizeof (gpointer) * (class->max_interface_id + 1));
816 /* initialize interface offsets */
817 for (k = class; k ; k = k->parent) {
818 for (i = 0; i < k->interface_count; i++) {
820 MonoClass *ic = k->interfaces [i];
821 slot = class->interface_offsets [ic->interface_id];
822 vt->interface_offsets [ic->interface_id] = &vt->vtable [slot];
826 /* initialize vtable */
827 for (i = 0; i < class->vtable_size; ++i) {
830 if ((cm = class->vtable [i]))
831 vt->vtable [i] = arch_create_jit_trampoline (cm);
834 g_hash_table_insert (domain->class_vtable_hash, class, vt);
836 mono_runtime_class_init (class);
842 * Compute a relative numbering of the class hierarchy as described in
843 * "Java for Large-Scale Scientific Computations?"
846 mono_compute_relative_numbering (MonoClass *class, int *c)
854 for (s = class->subclasses; s; s = s->next)
855 mono_compute_relative_numbering ((MonoClass *)s->data, c);
857 class->diffval = *c - class->baseval;
861 mono_class_setup_mono_type (MonoClass *class)
863 const char *name = class->name;
864 const char *nspace = class->name_space;
866 class->this_arg.byref = 1;
867 class->this_arg.data.klass = class;
868 class->this_arg.type = MONO_TYPE_CLASS;
869 class->byval_arg.data.klass = class;
870 class->byval_arg.type = MONO_TYPE_CLASS;
872 if (!strcmp (nspace, "System")) {
873 if (!strcmp (name, "ValueType")) {
875 * do not set the valuetype bit for System.ValueType.
876 * class->valuetype = 1;
878 } else if (!strcmp (name, "Enum")) {
880 * do not set the valuetype bit for System.Enum.
881 * class->valuetype = 1;
883 class->valuetype = 0;
885 } else if (!strcmp (name, "Object")) {
886 class->this_arg.type = class->byval_arg.type = MONO_TYPE_OBJECT;
887 } else if (!strcmp (name, "String")) {
888 class->this_arg.type = class->byval_arg.type = MONO_TYPE_STRING;
892 if (class->valuetype) {
893 int t = MONO_TYPE_VALUETYPE;
894 if (!strcmp (nspace, "System")) {
897 if (!strcmp (name, "Boolean")) {
898 t = MONO_TYPE_BOOLEAN;
899 } else if (!strcmp(name, "Byte")) {
904 if (!strcmp (name, "Char")) {
909 if (!strcmp (name, "Double")) {
914 if (!strcmp (name, "Int32")) {
916 } else if (!strcmp(name, "Int16")) {
918 } else if (!strcmp(name, "Int64")) {
920 } else if (!strcmp(name, "IntPtr")) {
925 if (!strcmp (name, "Single")) {
927 } else if (!strcmp(name, "SByte")) {
932 if (!strcmp (name, "UInt32")) {
934 } else if (!strcmp(name, "UInt16")) {
936 } else if (!strcmp(name, "UInt64")) {
938 } else if (!strcmp(name, "UIntPtr")) {
943 if (!strcmp (name, "Void")) {
951 class->this_arg.type = class->byval_arg.type = t;
956 mono_class_setup_parent (MonoClass *class, MonoClass *parent)
958 /* if root of the hierarchy */
959 if (!strcmp (class->name_space, "System") && !strcmp (class->name, "Object")) {
960 class->parent = NULL;
961 class->instance_size = sizeof (MonoObject);
962 } else if (!(class->flags & TYPE_ATTRIBUTE_INTERFACE)) {
964 class->parent = parent;
965 if (class->parent->enumtype || ((strcmp (class->parent->name, "ValueType") == 0) && (strcmp (class->parent->name_space, "System") == 0)))
966 class->valuetype = 1;
967 if (((strcmp (class->parent->name, "Enum") == 0) && (strcmp (class->parent->name_space, "System") == 0))) {
968 class->valuetype = class->enumtype = 1;
970 //class->enumtype = class->parent->enumtype;
971 class->parent->subclasses = g_list_prepend (class->parent->subclasses, class);
972 mono_compute_relative_numbering (mono_defaults.object_class, &rnum);
974 class->parent = NULL;
980 * @image: context where the image is created
981 * @type_token: typedef token
984 mono_class_create_from_typedef (MonoImage *image, guint32 type_token)
986 MonoTableInfo *tt = &image->tables [MONO_TABLE_TYPEDEF];
987 MonoClass *class, *parent = NULL;
988 guint32 cols [MONO_TYPEDEF_SIZE];
989 guint32 cols_next [MONO_TYPEDEF_SIZE];
990 guint tidx = mono_metadata_token_index (type_token);
991 const char *name, *nspace;
992 guint vtsize = 0, icount = 0;
993 MonoClass **interfaces;
996 if ((class = g_hash_table_lookup (image->class_cache, GUINT_TO_POINTER (type_token))))
999 g_assert (mono_metadata_token_table (type_token) == MONO_TABLE_TYPEDEF);
1001 mono_metadata_decode_row (tt, tidx - 1, cols, CSIZE (cols));
1003 if (tt->rows > tidx) {
1004 mono_metadata_decode_row (tt, tidx, cols_next, CSIZE (cols_next));
1005 vtsize += cols_next [MONO_TYPEDEF_METHOD_LIST] - cols [MONO_TYPEDEF_METHOD_LIST];
1007 vtsize += image->tables [MONO_TABLE_METHOD].rows - cols [MONO_TYPEDEF_METHOD_LIST] + 1;
1010 name = mono_metadata_string_heap (image, cols[1]);
1011 nspace = mono_metadata_string_heap (image, cols[2]);
1013 if (!(!strcmp (nspace, "System") && !strcmp (name, "Object")) &&
1014 !(cols [0] & TYPE_ATTRIBUTE_INTERFACE)) {
1015 parent = mono_class_get (image, mono_metadata_token_from_dor (cols [3]));
1017 interfaces = mono_metadata_interfaces_from_typedef (image, type_token, &icount);
1019 for (i = 0; i < icount; i++)
1020 vtsize += interfaces [i]->method.count;
1023 vtsize += parent->vtable_size;
1025 if (cols [0] & TYPE_ATTRIBUTE_INTERFACE)
1028 class = g_malloc0 (sizeof (MonoClass) + vtsize * sizeof (gpointer));
1030 g_hash_table_insert (image->class_cache, GUINT_TO_POINTER (type_token), class);
1032 class->interfaces = interfaces;
1033 class->interface_count = icount;
1034 class->vtable_size = vtsize;
1037 class->name_space = nspace;
1039 class->image = image;
1040 class->type_token = type_token;
1041 class->flags = cols [MONO_TYPEDEF_FLAGS];
1043 class->element_class = class;
1045 /*g_print ("Init class %s\n", name);*/
1047 mono_class_setup_parent (class, parent);
1049 mono_class_setup_mono_type (class);
1052 * Compute the field and method lists
1054 class->field.first = cols [MONO_TYPEDEF_FIELD_LIST] - 1;
1055 class->method.first = cols [MONO_TYPEDEF_METHOD_LIST] - 1;
1057 if (tt->rows > tidx){
1058 mono_metadata_decode_row (tt, tidx, cols_next, CSIZE (cols_next));
1059 class->field.last = cols_next [MONO_TYPEDEF_FIELD_LIST] - 1;
1060 class->method.last = cols_next [MONO_TYPEDEF_METHOD_LIST] - 1;
1062 class->field.last = image->tables [MONO_TABLE_FIELD].rows;
1063 class->method.last = image->tables [MONO_TABLE_METHOD].rows;
1066 if (cols [MONO_TYPEDEF_FIELD_LIST] &&
1067 cols [MONO_TYPEDEF_FIELD_LIST] <= image->tables [MONO_TABLE_FIELD].rows)
1068 class->field.count = class->field.last - class->field.first;
1070 class->field.count = 0;
1072 if (cols [MONO_TYPEDEF_METHOD_LIST] <= image->tables [MONO_TABLE_METHOD].rows)
1073 class->method.count = class->method.last - class->method.first;
1075 class->method.count = 0;
1077 /* reserve space to store vector pointer in arrays */
1078 if (!strcmp (nspace, "System") && !strcmp (name, "Array")) {
1079 class->instance_size += 2 * sizeof (gpointer);
1080 g_assert (class->field.count == 0);
1083 if (class->flags & TYPE_ATTRIBUTE_INTERFACE)
1084 class->interface_id = mono_get_unique_iid (class);
1086 //class->interfaces = mono_metadata_interfaces_from_typedef (image, type_token, &class->interface_count);
1088 if (class->enumtype) {
1089 class->fields = g_new0 (MonoClassField, class->field.count);
1090 class_compute_field_layout (class);
1093 if ((type_token = mono_metadata_nested_in_typedef (image, type_token)))
1094 class->nested_in = mono_class_create_from_typedef (image, type_token);
1099 mono_ptr_class_get (MonoType *type)
1102 MonoClass *el_class;
1103 static GHashTable *ptr_hash = NULL;
1106 ptr_hash = g_hash_table_new (g_direct_hash, g_direct_equal);
1107 el_class = mono_class_from_mono_type (type);
1108 if ((result = g_hash_table_lookup (ptr_hash, el_class)))
1110 result = g_new0 (MonoClass, 1);
1112 result->parent = NULL; /* no parent for PTR types */
1113 result->name = "System";
1114 result->name_space = "MonoPtrFakeClass";
1115 result->image = el_class->image;
1116 result->inited = TRUE;
1117 /* Can pointers get boxed? */
1118 result->instance_size = sizeof (gpointer);
1120 * baseval, diffval: need them to allow casting ?
1122 result->element_class = el_class;
1123 result->enum_basetype = &result->element_class->byval_arg;
1125 result->this_arg.type = result->byval_arg.type = MONO_TYPE_PTR;
1126 result->this_arg.data.type = result->byval_arg.data.type = result->enum_basetype;
1127 result->this_arg.byref = TRUE;
1129 g_hash_table_insert (ptr_hash, el_class, result);
1135 mono_class_from_mono_type (MonoType *type)
1137 switch (type->type) {
1138 case MONO_TYPE_OBJECT:
1139 return mono_defaults.object_class;
1140 case MONO_TYPE_VOID:
1141 return mono_defaults.void_class;
1142 case MONO_TYPE_BOOLEAN:
1143 return mono_defaults.boolean_class;
1144 case MONO_TYPE_CHAR:
1145 return mono_defaults.char_class;
1147 return mono_defaults.sbyte_class;
1149 return mono_defaults.byte_class;
1151 return mono_defaults.int16_class;
1153 return mono_defaults.uint16_class;
1155 return mono_defaults.int32_class;
1157 return mono_defaults.uint32_class;
1159 return mono_defaults.int_class;
1161 return mono_defaults.uint_class;
1163 return mono_defaults.int64_class;
1165 return mono_defaults.uint64_class;
1167 return mono_defaults.single_class;
1169 return mono_defaults.double_class;
1170 case MONO_TYPE_STRING:
1171 return mono_defaults.string_class;
1172 case MONO_TYPE_ARRAY:
1173 return mono_array_class_get (type->data.array->type, type->data.array->rank);
1175 return mono_ptr_class_get (type->data.type);
1176 case MONO_TYPE_SZARRAY:
1177 return mono_array_class_get (type->data.type, 1);
1178 case MONO_TYPE_CLASS:
1179 case MONO_TYPE_VALUETYPE:
1180 return type->data.klass;
1182 g_warning ("implement me %02x\n", type->type);
1183 g_assert_not_reached ();
1190 * @image: context where the image is created
1191 * @type_spec: typespec token
1194 mono_class_create_from_typespec (MonoImage *image, guint32 type_spec)
1199 type = mono_type_create_from_typespec (image, type_spec);
1201 switch (type->type) {
1202 case MONO_TYPE_ARRAY:
1203 class = mono_array_class_get (type->data.array->type, type->data.array->rank);
1205 case MONO_TYPE_SZARRAY:
1206 class = mono_array_class_get (type->data.type, 1);
1209 class = mono_class_from_mono_type (type->data.type);
1212 g_warning ("implement me: %08x", type->type);
1213 g_assert_not_reached ();
1216 mono_metadata_free_type (type);
1222 * mono_array_class_get:
1223 * @element_type: element type
1224 * @rank: the dimension of the array class
1226 * Returns: a class object describing the array with element type @element_type and
1230 mono_array_class_get (MonoType *element_type, guint32 rank)
1235 static MonoClass *parent = NULL;
1239 eclass = mono_class_from_mono_type (element_type);
1240 g_assert (rank <= 255);
1243 parent = mono_defaults.array_class;
1245 if (!parent->inited)
1246 mono_class_init (parent);
1248 image = eclass->image;
1250 g_assert (!eclass->type_token ||
1251 mono_metadata_token_table (eclass->type_token) == MONO_TABLE_TYPEDEF);
1253 key = ((rank & 0xff) << 24) | (eclass->type_token & 0xffffff);
1254 if ((class = g_hash_table_lookup (image->array_cache, GUINT_TO_POINTER (key))))
1257 class = g_malloc0 (sizeof (MonoClass) + parent->vtable_size * sizeof (gpointer));
1259 class->image = image;
1260 class->name_space = "System";
1261 class->name = "Array";
1262 class->type_token = 0;
1263 class->flags = TYPE_ATTRIBUTE_CLASS;
1264 class->parent = parent;
1265 class->instance_size = mono_class_instance_size (class->parent);
1266 class->class_size = 0;
1267 class->vtable_size = parent->vtable_size;
1268 class->parent->subclasses = g_list_prepend (class->parent->subclasses, class);
1269 mono_compute_relative_numbering (mono_defaults.object_class, &rnum);
1272 class->element_class = eclass;
1274 MonoArrayType *at = g_new0 (MonoArrayType, 1);
1275 class->byval_arg.type = MONO_TYPE_ARRAY;
1276 class->byval_arg.data.array = at;
1277 at->type = &eclass->byval_arg;
1279 /* FIXME: complete.... */
1281 class->byval_arg.type = MONO_TYPE_SZARRAY;
1282 class->byval_arg.data.type = &eclass->byval_arg;
1284 class->this_arg = class->byval_arg;
1285 class->this_arg.byref = 1;
1287 g_hash_table_insert (image->array_cache, GUINT_TO_POINTER (key), class);
1292 * mono_class_instance_size:
1295 * Returns: the size of an object instance
1298 mono_class_instance_size (MonoClass *klass)
1301 if (!klass->size_inited)
1302 mono_class_init (klass);
1304 return klass->instance_size;
1308 * mono_class_value_size:
1311 * This function is used for value types, and return the
1312 * space and the alignment to store that kind of value object.
1314 * Returns: the size of a value of kind @klass
1317 mono_class_value_size (MonoClass *klass, guint32 *align)
1321 /* fixme: check disable, because we still have external revereces to
1322 * mscorlib and Dummy Objects
1324 /*g_assert (klass->valuetype);*/
1326 size = mono_class_instance_size (klass) - sizeof (MonoObject);
1329 *align = klass->min_align;
1335 * mono_class_data_size:
1338 * Returns: the size of the static class data
1341 mono_class_data_size (MonoClass *klass)
1345 mono_class_init (klass);
1347 return klass->class_size;
1351 * Auxiliary routine to mono_class_get_field
1353 * Takes a field index instead of a field token.
1355 static MonoClassField *
1356 mono_class_get_field_idx (MonoClass *class, int idx)
1358 if (class->field.count){
1359 if ((idx >= class->field.first) && (idx < class->field.last)){
1360 return &class->fields [idx - class->field.first];
1367 return mono_class_get_field_idx (class->parent, idx);
1371 * mono_class_get_field:
1372 * @class: the class to lookup the field.
1373 * @field_token: the field token
1375 * Returns: A MonoClassField representing the type and offset of
1376 * the field, or a NULL value if the field does not belong to this
1380 mono_class_get_field (MonoClass *class, guint32 field_token)
1382 int idx = mono_metadata_token_index (field_token);
1384 g_assert (mono_metadata_token_code (field_token) == MONO_TOKEN_FIELD_DEF);
1386 return mono_class_get_field_idx (class, idx - 1);
1390 mono_class_get_field_from_name (MonoClass *klass, const char *name)
1394 for (i = 0; i < klass->field.count; ++i) {
1395 if (strcmp (name, klass->fields [i].name) == 0)
1396 return &klass->fields [i];
1403 * @image: the image where the class resides
1404 * @type_token: the token for the class
1405 * @at: an optional pointer to return the array element type
1407 * Returns: the MonoClass that represents @type_token in @image
1410 mono_class_get (MonoImage *image, guint32 type_token)
1414 switch (type_token & 0xff000000){
1415 case MONO_TOKEN_TYPE_DEF:
1416 class = mono_class_create_from_typedef (image, type_token);
1418 case MONO_TOKEN_TYPE_REF:
1419 return mono_class_create_from_typeref (image, type_token);
1420 case MONO_TOKEN_TYPE_SPEC:
1421 class = mono_class_create_from_typespec (image, type_token);
1424 g_warning ("unknown token type %x", type_token & 0xff000000);
1425 g_assert_not_reached ();
1432 mono_class_from_name (MonoImage *image, const char* name_space, const char *name)
1434 GHashTable *nspace_table;
1437 nspace_table = g_hash_table_lookup (image->name_cache, name_space);
1440 token = GPOINTER_TO_UINT (g_hash_table_lookup (nspace_table, name));
1443 /*g_warning ("token not found for %s.%s in image %s", name_space, name, image->name);*/
1447 token = MONO_TOKEN_TYPE_DEF | token;
1449 return mono_class_get (image, token);
1453 * mono_array_element_size:
1454 * @ac: pointer to a #MonoArrayClass
1456 * Returns: the size of single array element.
1459 mono_array_element_size (MonoClass *ac)
1461 if (ac->element_class->valuetype)
1462 return mono_class_instance_size (ac->element_class) - sizeof (MonoObject);
1464 return sizeof (gpointer);
1468 mono_ldtoken (MonoImage *image, guint32 token, MonoClass **handle_class)
1470 switch (token & 0xff000000) {
1471 case MONO_TOKEN_TYPE_DEF:
1472 case MONO_TOKEN_TYPE_REF: {
1475 *handle_class = mono_defaults.typehandle_class;
1476 class = mono_class_get (image, token);
1477 mono_class_init (class);
1478 /* We return a MonoType* as handle */
1479 return &class->byval_arg;
1481 case MONO_TOKEN_TYPE_SPEC: {
1484 *handle_class = mono_defaults.typehandle_class;
1485 class = mono_class_create_from_typespec (image, token);
1486 mono_class_init (class);
1487 return &class->byval_arg;
1489 case MONO_TOKEN_FIELD_DEF: {
1491 guint32 type = mono_metadata_typedef_from_field (image, mono_metadata_token_index (token));
1492 class = mono_class_get (image, MONO_TOKEN_TYPE_DEF | type);
1493 mono_class_init (class);
1495 *handle_class = mono_class_from_name (mono_defaults.corlib, "System", "RuntimeFieldHandle");
1496 return mono_class_get_field (class, token);
1498 case MONO_TOKEN_METHOD_DEF:
1499 case MONO_TOKEN_MEMBER_REF:
1501 g_warning ("Unknown token 0x%08x in ldtoken", token);