2 * class.c: Class management for the Mono runtime
5 * Miguel de Icaza (miguel@ximian.com)
7 * (C) 2001 Ximian, Inc.
9 * Possible Optimizations:
10 * in mono_class_create, do not allocate the class right away,
11 * but wait until you know the size of the FieldMap, so that
12 * the class embeds directly the FieldMap after the vtable.
22 #include <mono/metadata/image.h>
23 #include <mono/metadata/cil-coff.h>
24 #include <mono/metadata/metadata.h>
25 #include <mono/metadata/tabledefs.h>
26 #include <mono/metadata/tokentype.h>
27 #include <mono/metadata/class.h>
28 #include <mono/metadata/object.h>
29 #include <mono/metadata/appdomain.h>
30 #include <mono/metadata/mono-endian.h>
35 #define CSIZE(x) (sizeof (x) / 4)
37 gboolean mono_print_vtable = FALSE;
39 static MonoClass * mono_class_create_from_typedef (MonoImage *image, guint32 type_token);
42 mono_class_from_typeref (MonoImage *image, guint32 type_token)
44 guint32 cols [MONO_TYPEREF_SIZE];
45 MonoTableInfo *t = &image->tables [MONO_TABLE_TYPEREF];
47 const char *name, *nspace;
50 mono_metadata_decode_row (t, (type_token&0xffffff)-1, cols, MONO_TYPEREF_SIZE);
52 name = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAME]);
53 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAMESPACE]);
55 idx = cols [MONO_TYPEREF_SCOPE] >> RESOLTION_SCOPE_BITS;
56 switch (cols [MONO_TYPEREF_SCOPE] & RESOLTION_SCOPE_MASK) {
57 case RESOLTION_SCOPE_MODULE:
59 g_error ("null ResolutionScope not yet handled");
60 /* a typedef in disguise */
61 return mono_class_from_name (image, nspace, name);
62 case RESOLTION_SCOPE_MODULEREF:
63 g_error ("ModuleRef ResolutionScope not yet handled");
64 case RESOLTION_SCOPE_TYPEREF:
65 g_error ("TypeRef ResolutionScope not yet handled");
66 case RESOLTION_SCOPE_ASSEMBLYREF:
70 if (!image->references || !image->references [idx-1]) {
72 * detected a reference to mscorlib, we simply return a reference to a dummy
73 * until we have a better solution.
75 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]));
77 res = mono_class_from_name (image, "System", "MonoDummy");
78 /* prevent method loading */
80 /* some storage if the type is used - very ugly hack */
81 res->instance_size = 2*sizeof (gpointer);
85 /* load referenced assembly */
86 image = image->references [idx-1]->image;
88 return mono_class_from_name (image, nspace, name);
92 * class_compute_field_layout:
93 * @m: pointer to the metadata.
94 * @class: The class to initialize
96 * Initializes the class->fields.
98 * Currently we only support AUTO_LAYOUT, and do not even try to do
99 * a good job at it. This is temporary to get the code for Paolo.
102 class_compute_field_layout (MonoClass *class)
104 MonoImage *m = class->image;
105 const int top = class->field.count;
106 guint32 layout = class->flags & TYPE_ATTRIBUTE_LAYOUT_MASK;
107 MonoTableInfo *t = &m->tables [MONO_TABLE_FIELD];
111 * Fetch all the field information.
113 for (i = 0; i < top; i++){
115 guint32 cols [MONO_FIELD_SIZE];
116 int idx = class->field.first + i;
118 mono_metadata_decode_row (t, idx, cols, CSIZE (cols));
119 /* The name is needed for fieldrefs */
120 class->fields [i].name = mono_metadata_string_heap (m, cols [MONO_FIELD_NAME]);
121 sig = mono_metadata_blob_heap (m, cols [MONO_FIELD_SIGNATURE]);
122 mono_metadata_decode_value (sig, &sig);
123 /* FIELD signature == 0x06 */
124 g_assert (*sig == 0x06);
125 class->fields [i].type = mono_metadata_parse_field_type (
126 m, cols [MONO_FIELD_FLAGS], sig + 1, &sig);
127 if (cols [MONO_FIELD_FLAGS] & FIELD_ATTRIBUTE_HAS_FIELD_RVA) {
128 mono_metadata_field_info (m, idx, NULL, &class->fields [i].data, NULL);
129 if (!class->fields [i].data)
130 g_warning ("field %s in %s should have RVA data, but hasn't", class->fields [i].name, class->name);
132 if (class->enumtype && !(cols [MONO_FIELD_FLAGS] & FIELD_ATTRIBUTE_STATIC)) {
133 class->enum_basetype = class->fields [i].type;
134 class->element_class = mono_class_from_mono_type (class->enum_basetype);
137 if (class->enumtype && !class->enum_basetype) {
138 if (!((strcmp (class->name, "Enum") == 0) && (strcmp (class->name_space, "System") == 0)))
142 * Compute field layout and total size (not considering static fields)
145 case TYPE_ATTRIBUTE_AUTO_LAYOUT:
146 case TYPE_ATTRIBUTE_SEQUENTIAL_LAYOUT:
147 for (i = 0; i < top; i++){
150 if (class->fields [i].type->attrs & FIELD_ATTRIBUTE_STATIC)
153 size = mono_type_size (class->fields [i].type, &align);
155 class->min_align = MAX (align, class->min_align);
156 class->fields [i].offset = class->instance_size;
157 class->fields [i].offset += align - 1;
158 class->fields [i].offset &= ~(align - 1);
159 class->instance_size = class->fields [i].offset + size;
162 if (class->instance_size & (class->min_align - 1)) {
163 class->instance_size += class->min_align - 1;
164 class->instance_size &= ~(class->min_align - 1);
167 case TYPE_ATTRIBUTE_EXPLICIT_LAYOUT:
168 for (i = 0; i < top; i++) {
170 int idx = class->field.first + i;
173 * There must be info about all the fields in a type if it
174 * uses explicit layout.
177 if (class->fields [i].type->attrs & FIELD_ATTRIBUTE_STATIC)
180 size = mono_type_size (class->fields [i].type, &align);
182 mono_metadata_field_info (m, idx, &class->fields [i].offset, NULL, NULL);
183 if (class->fields [i].offset == (guint32)-1)
184 g_warning ("%s not initialized correctly (missing field layout info for %s)", class->name, class->fields [i].name);
186 * The offset is from the start of the object: this works for both
187 * classes and valuetypes.
189 class->fields [i].offset += sizeof (MonoObject);
193 size += class->fields [i].offset;
194 class->instance_size = MAX (class->instance_size, size);
199 class->size_inited = 1;
202 * Compute static field layout and size
205 case TYPE_ATTRIBUTE_AUTO_LAYOUT:
206 case TYPE_ATTRIBUTE_SEQUENTIAL_LAYOUT:
207 for (i = 0; i < top; i++){
210 if (!(class->fields [i].type->attrs & FIELD_ATTRIBUTE_STATIC))
213 size = mono_type_size (class->fields [i].type, &align);
214 class->fields [i].offset = class->class_size;
215 class->fields [i].offset += align - 1;
216 class->fields [i].offset &= ~(align - 1);
217 class->class_size = class->fields [i].offset + size;
220 case TYPE_ATTRIBUTE_EXPLICIT_LAYOUT:
221 for (i = 0; i < top; i++){
225 * There must be info about all the fields in a type if it
226 * uses explicit layout.
230 if (!(class->fields [i].type->attrs & FIELD_ATTRIBUTE_STATIC))
233 size = mono_type_size (class->fields [i].type, &align);
234 class->fields [i].offset = class->class_size;
235 class->fields [i].offset += align - 1;
236 class->fields [i].offset &= ~(align - 1);
237 class->class_size = class->fields [i].offset + size;
244 init_properties (MonoClass *class)
246 guint startm, endm, i, j;
247 guint32 cols [MONO_PROPERTY_SIZE];
248 MonoTableInfo *pt = &class->image->tables [MONO_TABLE_PROPERTY];
249 MonoTableInfo *msemt = &class->image->tables [MONO_TABLE_METHODSEMANTICS];
251 class->property.first = mono_metadata_properties_from_typedef (class->image, mono_metadata_token_index (class->type_token) - 1, &class->property.last);
252 class->property.count = class->property.last - class->property.first;
254 class->properties = g_new0 (MonoProperty, class->property.count);
255 for (i = class->property.first; i < class->property.last; ++i) {
256 mono_metadata_decode_row (pt, i, cols, MONO_PROPERTY_SIZE);
257 class->properties [i - class->property.first].attrs = cols [MONO_PROPERTY_FLAGS];
258 class->properties [i - class->property.first].name = mono_metadata_string_heap (class->image, cols [MONO_PROPERTY_NAME]);
260 startm = mono_metadata_methods_from_property (class->image, i, &endm);
261 for (j = startm; j < endm; ++j) {
262 mono_metadata_decode_row (msemt, j, cols, MONO_METHOD_SEMA_SIZE);
263 switch (cols [MONO_METHOD_SEMA_SEMANTICS]) {
264 case METHOD_SEMANTIC_SETTER:
265 class->properties [i - class->property.first].set = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
267 case METHOD_SEMANTIC_GETTER:
268 class->properties [i - class->property.first].get = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
278 init_events (MonoClass *class)
280 guint startm, endm, i, j;
281 guint32 cols [MONO_EVENT_SIZE];
282 MonoTableInfo *pt = &class->image->tables [MONO_TABLE_EVENT];
283 MonoTableInfo *msemt = &class->image->tables [MONO_TABLE_METHODSEMANTICS];
285 class->event.first = mono_metadata_events_from_typedef (class->image, mono_metadata_token_index (class->type_token) - 1, &class->event.last);
286 class->event.count = class->event.last - class->event.first;
288 class->events = g_new0 (MonoEvent, class->event.count);
289 for (i = class->event.first; i < class->event.last; ++i) {
290 mono_metadata_decode_row (pt, i, cols, MONO_EVENT_SIZE);
291 class->events [i - class->event.first].attrs = cols [MONO_EVENT_FLAGS];
292 class->events [i - class->event.first].name = mono_metadata_string_heap (class->image, cols [MONO_EVENT_NAME]);
294 startm = mono_metadata_methods_from_event (class->image, i, &endm);
295 for (j = startm; j < endm; ++j) {
296 mono_metadata_decode_row (msemt, j, cols, MONO_METHOD_SEMA_SIZE);
297 switch (cols [MONO_METHOD_SEMA_SEMANTICS]) {
298 case METHOD_SEMANTIC_ADD_ON:
299 class->events [i - class->event.first].add = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
301 case METHOD_SEMANTIC_REMOVE_ON:
302 class->events [i - class->event.first].remove = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
304 case METHOD_SEMANTIC_FIRE:
305 class->events [i - class->event.first].raise = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
307 case METHOD_SEMANTIC_OTHER: /* don't care for now */
316 mono_get_unique_iid (MonoClass *class)
318 static GHashTable *iid_hash = NULL;
319 static guint iid = 0;
324 g_assert (class->flags & TYPE_ATTRIBUTE_INTERFACE);
327 iid_hash = g_hash_table_new (g_str_hash, g_str_equal);
329 str = g_strdup_printf ("%s|%s.%s\n", class->image->name, class->name_space, class->name);
331 if (g_hash_table_lookup_extended (iid_hash, str, NULL, &value)) {
335 g_hash_table_insert (iid_hash, str, (gpointer)iid);
344 * @class: the class to initialize
346 * compute the instance_size, class_size and other infos that cannot be
347 * computed at mono_class_get() time. Also compute a generic vtable and
348 * the method slot numbers. We use this infos later to create a domain
352 mono_class_init (MonoClass *class)
356 int i, max_vtsize = 0, max_iid, cur_slot = 0;
357 static MonoMethod *default_ghc = NULL;
358 static MonoMethod *default_finalize = NULL;
359 static int finalize_slot = -1;
360 static int ghc_slot = -1;
367 if (class->init_pending) {
368 /* this indicates a cyclic dependency */
369 g_error ("pending init %s.%s\n", class->name_space, class->name);
372 class->init_pending = 1;
374 // mono_stats.initialized_class_count++;
377 if (!class->parent->inited)
378 mono_class_init (class->parent);
379 class->instance_size += class->parent->instance_size;
380 class->class_size += class->parent->class_size;
381 class->min_align = class->parent->min_align;
382 cur_slot = class->parent->vtable_size;
384 class->min_align = 1;
387 * Computes the size used by the fields, and their locations
389 if (!class->size_inited && class->field.count > 0){
390 class->fields = g_new0 (MonoClassField, class->field.count);
391 class_compute_field_layout (class);
394 if (!(class->flags & TYPE_ATTRIBUTE_INTERFACE)) {
395 for (i = 0; i < class->interface_count; i++)
396 max_vtsize += class->interfaces [i]->method.count;
399 max_vtsize += class->parent->vtable_size;
401 max_vtsize += class->method.count;
404 vtable = alloca (sizeof (gpointer) * max_vtsize);
405 memset (vtable, 0, sizeof (gpointer) * max_vtsize);
407 /* initialize method pointers */
408 class->methods = g_new (MonoMethod*, class->method.count);
409 for (i = 0; i < class->method.count; ++i)
410 class->methods [i] = mono_get_method (class->image,
411 MONO_TOKEN_METHOD_DEF | (i + class->method.first + 1), class);
413 init_properties (class);
416 i = mono_metadata_nesting_typedef (class->image, class->type_token);
419 guint32 cols [MONO_NESTED_CLASS_SIZE];
420 mono_metadata_decode_row (&class->image->tables [MONO_TABLE_NESTEDCLASS], i - 1, cols, MONO_NESTED_CLASS_SIZE);
421 if (cols [MONO_NESTED_CLASS_ENCLOSING] != mono_metadata_token_index (class->type_token))
423 nclass = mono_class_create_from_typedef (class->image, MONO_TOKEN_TYPE_DEF | cols [MONO_NESTED_CLASS_NESTED]);
424 class->nested_classes = g_list_prepend (class->nested_classes, nclass);
428 if (class->flags & TYPE_ATTRIBUTE_INTERFACE) {
429 for (i = 0; i < class->method.count; ++i)
430 class->methods [i]->slot = i;
431 class->init_pending = 0;
436 //printf ("METAINIT %s.%s\n", class->name_space, class->name);
438 /* compute maximum number of slots and maximum interface id */
440 for (k = class; k ; k = k->parent) {
441 for (i = 0; i < k->interface_count; i++) {
442 ic = k->interfaces [i];
445 mono_class_init (ic);
447 if (max_iid < ic->interface_id)
448 max_iid = ic->interface_id;
452 class->max_interface_id = max_iid;
453 /* compute vtable offset for interfaces */
454 class->interface_offsets = g_malloc (sizeof (gpointer) * (max_iid + 1));
456 for (i = 0; i <= max_iid; i++)
457 class->interface_offsets [i] = -1;
459 for (i = 0; i < class->interface_count; i++) {
460 ic = class->interfaces [i];
461 class->interface_offsets [ic->interface_id] = cur_slot;
462 cur_slot += ic->method.count;
465 for (k = class->parent; k ; k = k->parent) {
466 for (i = 0; i < k->interface_count; i++) {
467 ic = k->interfaces [i];
468 if (class->interface_offsets [ic->interface_id] == -1) {
469 int io = k->interface_offsets [ic->interface_id];
472 g_assert (io <= max_vtsize);
474 class->interface_offsets [ic->interface_id] = io;
479 if (class->parent && class->parent->vtable_size)
480 memcpy (vtable, class->parent->vtable, sizeof (gpointer) * class->parent->vtable_size);
482 for (k = class; k ; k = k->parent) {
483 for (i = 0; i < k->interface_count; i++) {
486 ic = k->interfaces [i];
487 io = k->interface_offsets [ic->interface_id];
490 g_assert (io <= max_vtsize);
493 for (l = 0; l < ic->method.count; l++) {
494 MonoMethod *im = ic->methods [l];
495 for (j = 0; j < class->method.count; ++j) {
496 MonoMethod *cm = class->methods [j];
497 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
498 !(cm->flags & METHOD_ATTRIBUTE_PUBLIC) ||
499 !(cm->flags & METHOD_ATTRIBUTE_NEW_SLOT))
501 if (!strcmp(cm->name, im->name) &&
502 mono_metadata_signature_equal (cm->signature, im->signature)) {
503 g_assert (io + l <= max_vtsize);
504 vtable [io + l] = cm;
509 /* already implemented */
510 if (io >= k->vtable_size)
514 for (l = 0; l < ic->method.count; l++) {
515 MonoMethod *im = ic->methods [l];
518 g_assert (io + l <= max_vtsize);
523 for (k1 = class; k1; k1 = k1->parent) {
524 for (j = 0; j < k1->method.count; ++j) {
525 MonoMethod *cm = k1->methods [j];
527 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
528 !(cm->flags & METHOD_ATTRIBUTE_PUBLIC))
531 if (!strcmp(cm->name, im->name) &&
532 mono_metadata_signature_equal (cm->signature, im->signature)) {
533 g_assert (io + l <= max_vtsize);
534 vtable [io + l] = cm;
539 g_assert (io + l <= max_vtsize);
545 for (l = 0; l < ic->method.count; l++) {
546 MonoMethod *im = ic->methods [l];
547 char *qname, *fqname;
549 qname = g_strconcat (ic->name, ".", im->name, NULL);
550 if (ic->name_space && ic->name_space [0])
551 fqname = g_strconcat (ic->name_space, ".", ic->name, ".", im->name, NULL);
555 for (j = 0; j < class->method.count; ++j) {
556 MonoMethod *cm = class->methods [j];
558 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL))
561 if (((fqname && !strcmp (cm->name, fqname)) || !strcmp (cm->name, qname)) &&
562 mono_metadata_signature_equal (cm->signature, im->signature)) {
563 g_assert (io + l <= max_vtsize);
564 vtable [io + l] = cm;
573 if (!(class->flags & TYPE_ATTRIBUTE_ABSTRACT)) {
574 for (l = 0; l < ic->method.count; l++) {
575 MonoMethod *im = ic->methods [l];
576 g_assert (io + l <= max_vtsize);
577 if (!(vtable [io + l])) {
578 printf ("no implementation for interface method %s.%s::%s in class %s.%s\n",
579 ic->name_space, ic->name, im->name, class->name_space, class->name);
581 for (j = 0; j < class->method.count; ++j) {
582 MonoMethod *cm = class->methods [j];
584 printf ("METHOD %s\n", cm->name);
586 g_assert_not_reached ();
591 for (l = 0; l < ic->method.count; l++) {
592 MonoMethod *im = vtable [io + l];
595 g_assert (io + l <= max_vtsize);
597 // fixme: why do we need this ?
599 // g_assert_not_reached ();
606 for (i = 0; i < class->method.count; ++i) {
609 cm = class->methods [i];
613 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
616 #else /* EXT_VTABLE_HACK */
621 if (!(cm->flags & METHOD_ATTRIBUTE_NEW_SLOT) && (cm->flags & METHOD_ATTRIBUTE_VIRTUAL)) {
622 for (k = class->parent; k ; k = k->parent) {
624 for (j = 0; j < k->method.count; ++j) {
625 MonoMethod *m1 = k->methods [j];
626 if (!(m1->flags & METHOD_ATTRIBUTE_VIRTUAL))
628 if (!strcmp(cm->name, m1->name) &&
629 mono_metadata_signature_equal (cm->signature, m1->signature)) {
630 cm->slot = k->methods [j]->slot;
631 g_assert (cm->slot < max_vtsize);
641 cm->slot = cur_slot++;
643 if (!(cm->flags & METHOD_ATTRIBUTE_ABSTRACT))
644 vtable [cm->slot] = cm;
648 class->vtable_size = cur_slot;
649 class->vtable = g_malloc0 (sizeof (gpointer) * class->vtable_size);
650 memcpy (class->vtable, vtable, sizeof (gpointer) * class->vtable_size);
653 class->init_pending = 0;
655 if (mono_print_vtable) {
658 for (i = 0; i <= max_iid; i++)
659 if (class->interface_offsets [i] != -1)
662 printf ("VTable %s.%s (size = %d, interfaces = %d)\n", class->name_space,
663 class->name, class->vtable_size, icount);
665 for (i = 0; i < class->vtable_size; ++i) {
670 printf (" slot %03d(%03d) %s.%s:%s\n", i, cm->slot,
671 cm->klass->name_space, cm->klass->name,
678 printf ("Interfaces %s.%s (max_iid = %d)\n", class->name_space,
679 class->name, max_iid);
681 for (i = 0; i < class->interface_count; i++) {
682 ic = class->interfaces [i];
683 printf (" slot %03d(%03d) %s.%s\n",
684 class->interface_offsets [ic->interface_id],
685 ic->method.count, ic->name_space, ic->name);
688 for (k = class->parent; k ; k = k->parent) {
689 for (i = 0; i < k->interface_count; i++) {
690 ic = k->interfaces [i];
691 printf (" slot %03d(%03d) %s.%s\n",
692 class->interface_offsets [ic->interface_id],
693 ic->method.count, ic->name_space, ic->name);
700 if (class == mono_defaults.object_class) {
702 for (i = 0; i < class->vtable_size; ++i) {
703 MonoMethod *cm = vtable [i];
705 if (!strcmp (cm->name, "GetHashCode")) {
711 g_assert (ghc_slot > 0);
713 default_ghc = vtable [ghc_slot];
718 if (class != mono_defaults.object_class) {
720 if (vtable [ghc_slot] == default_ghc) {
725 if (!default_finalize) {
726 if (class == mono_defaults.object_class) {
728 for (i = 0; i < class->vtable_size; ++i) {
729 MonoMethod *cm = vtable [i];
731 if (!strcmp (cm->name, "Finalize")) {
737 g_assert (finalize_slot > 0);
739 default_finalize = vtable [finalize_slot];
743 /* Object::Finalize should have empty implemenatation */
744 class->has_finalize = 0;
745 if (class != mono_defaults.object_class) {
746 if (vtable [finalize_slot] != default_finalize)
747 class->has_finalize = 1;
753 * Compute a relative numbering of the class hierarchy as described in
754 * "Java for Large-Scale Scientific Computations?"
757 mono_compute_relative_numbering (MonoClass *class, int *c)
765 for (s = class->subclasses; s; s = s->next)
766 mono_compute_relative_numbering ((MonoClass *)s->data, c);
768 class->diffval = *c - class->baseval;
772 mono_class_setup_mono_type (MonoClass *class)
774 const char *name = class->name;
775 const char *nspace = class->name_space;
777 class->this_arg.byref = 1;
778 class->this_arg.data.klass = class;
779 class->this_arg.type = MONO_TYPE_CLASS;
780 class->byval_arg.data.klass = class;
781 class->byval_arg.type = MONO_TYPE_CLASS;
783 if (!strcmp (nspace, "System")) {
784 if (!strcmp (name, "ValueType")) {
786 * do not set the valuetype bit for System.ValueType.
787 * class->valuetype = 1;
789 } else if (!strcmp (name, "Enum")) {
791 * do not set the valuetype bit for System.Enum.
792 * class->valuetype = 1;
794 class->valuetype = 0;
796 } else if (!strcmp (name, "Object")) {
797 class->this_arg.type = class->byval_arg.type = MONO_TYPE_OBJECT;
798 } else if (!strcmp (name, "String")) {
799 class->this_arg.type = class->byval_arg.type = MONO_TYPE_STRING;
803 if (class->valuetype) {
804 int t = MONO_TYPE_VALUETYPE;
805 if (!strcmp (nspace, "System")) {
808 if (!strcmp (name, "Boolean")) {
809 t = MONO_TYPE_BOOLEAN;
810 } else if (!strcmp(name, "Byte")) {
815 if (!strcmp (name, "Char")) {
820 if (!strcmp (name, "Double")) {
825 if (!strcmp (name, "Int32")) {
827 } else if (!strcmp(name, "Int16")) {
829 } else if (!strcmp(name, "Int64")) {
831 } else if (!strcmp(name, "IntPtr")) {
836 if (!strcmp (name, "Single")) {
838 } else if (!strcmp(name, "SByte")) {
843 if (!strcmp (name, "UInt32")) {
845 } else if (!strcmp(name, "UInt16")) {
847 } else if (!strcmp(name, "UInt64")) {
849 } else if (!strcmp(name, "UIntPtr")) {
854 if (!strcmp (name, "Void")) {
862 class->this_arg.type = class->byval_arg.type = t;
867 mono_class_setup_parent (MonoClass *class, MonoClass *parent)
869 gboolean system_namespace;
871 system_namespace = !strcmp (class->name_space, "System");
873 /* if root of the hierarchy */
874 if (system_namespace && !strcmp (class->name, "Object")) {
875 class->parent = NULL;
876 class->instance_size = sizeof (MonoObject);
880 if (!(class->flags & TYPE_ATTRIBUTE_INTERFACE)) {
882 class->parent = parent;
885 g_assert_not_reached (); /* FIXME */
887 class->marshalbyref = parent->marshalbyref;
888 class->contextbound = parent->contextbound;
889 class->delegate = parent->delegate;
891 if (system_namespace) {
892 if (*class->name == 'M' && !strcmp (class->name, "MarshalByRefObject"))
893 class->marshalbyref = 1;
895 if (*class->name == 'C' && !strcmp (class->name, "ContextBoundObject"))
896 class->contextbound = 1;
898 if (*class->name == 'D' && !strcmp (class->name, "Delegate"))
902 if (class->parent->enumtype || ((strcmp (class->parent->name, "ValueType") == 0) &&
903 (strcmp (class->parent->name_space, "System") == 0)))
904 class->valuetype = 1;
905 if (((strcmp (class->parent->name, "Enum") == 0) && (strcmp (class->parent->name_space, "System") == 0))) {
906 class->valuetype = class->enumtype = 1;
908 //class->enumtype = class->parent->enumtype;
909 class->parent->subclasses = g_list_prepend (class->parent->subclasses, class);
910 mono_compute_relative_numbering (mono_defaults.object_class, &rnum);
912 class->parent = NULL;
918 * @image: context where the image is created
919 * @type_token: typedef token
922 mono_class_create_from_typedef (MonoImage *image, guint32 type_token)
924 MonoTableInfo *tt = &image->tables [MONO_TABLE_TYPEDEF];
925 MonoClass *class, *parent = NULL;
926 guint32 cols [MONO_TYPEDEF_SIZE];
927 guint32 cols_next [MONO_TYPEDEF_SIZE];
928 guint tidx = mono_metadata_token_index (type_token);
929 const char *name, *nspace;
931 MonoClass **interfaces;
933 if ((class = g_hash_table_lookup (image->class_cache, GUINT_TO_POINTER (type_token))))
936 g_assert (mono_metadata_token_table (type_token) == MONO_TABLE_TYPEDEF);
938 mono_metadata_decode_row (tt, tidx - 1, cols, CSIZE (cols));
940 name = mono_metadata_string_heap (image, cols[1]);
941 nspace = mono_metadata_string_heap (image, cols[2]);
943 if (!(!strcmp (nspace, "System") && !strcmp (name, "Object")) &&
944 !(cols [0] & TYPE_ATTRIBUTE_INTERFACE)) {
945 parent = mono_class_get (image, mono_metadata_token_from_dor (cols [3]));
947 interfaces = mono_metadata_interfaces_from_typedef (image, type_token, &icount);
949 class = g_malloc0 (sizeof (MonoClass));
951 g_hash_table_insert (image->class_cache, GUINT_TO_POINTER (type_token), class);
953 class->interfaces = interfaces;
954 class->interface_count = icount;
957 class->name_space = nspace;
959 class->image = image;
960 class->type_token = type_token;
961 class->flags = cols [MONO_TYPEDEF_FLAGS];
963 class->element_class = class;
965 /*g_print ("Init class %s\n", name);*/
967 mono_class_setup_parent (class, parent);
969 mono_class_setup_mono_type (class);
972 * Compute the field and method lists
974 class->field.first = cols [MONO_TYPEDEF_FIELD_LIST] - 1;
975 class->method.first = cols [MONO_TYPEDEF_METHOD_LIST] - 1;
977 if (tt->rows > tidx){
978 mono_metadata_decode_row (tt, tidx, cols_next, CSIZE (cols_next));
979 class->field.last = cols_next [MONO_TYPEDEF_FIELD_LIST] - 1;
980 class->method.last = cols_next [MONO_TYPEDEF_METHOD_LIST] - 1;
982 class->field.last = image->tables [MONO_TABLE_FIELD].rows;
983 class->method.last = image->tables [MONO_TABLE_METHOD].rows;
986 if (cols [MONO_TYPEDEF_FIELD_LIST] &&
987 cols [MONO_TYPEDEF_FIELD_LIST] <= image->tables [MONO_TABLE_FIELD].rows)
988 class->field.count = class->field.last - class->field.first;
990 class->field.count = 0;
992 if (cols [MONO_TYPEDEF_METHOD_LIST] <= image->tables [MONO_TABLE_METHOD].rows)
993 class->method.count = class->method.last - class->method.first;
995 class->method.count = 0;
997 /* reserve space to store vector pointer in arrays */
998 if (!strcmp (nspace, "System") && !strcmp (name, "Array")) {
999 class->instance_size += 2 * sizeof (gpointer);
1000 g_assert (class->field.count == 0);
1003 if (class->flags & TYPE_ATTRIBUTE_INTERFACE)
1004 class->interface_id = mono_get_unique_iid (class);
1006 //class->interfaces = mono_metadata_interfaces_from_typedef (image, type_token, &class->interface_count);
1008 if (class->enumtype) {
1009 class->fields = g_new0 (MonoClassField, class->field.count);
1010 class_compute_field_layout (class);
1013 if ((type_token = mono_metadata_nested_in_typedef (image, type_token)))
1014 class->nested_in = mono_class_create_from_typedef (image, type_token);
1019 mono_ptr_class_get (MonoType *type)
1022 MonoClass *el_class;
1023 static GHashTable *ptr_hash = NULL;
1026 ptr_hash = g_hash_table_new (g_direct_hash, g_direct_equal);
1027 el_class = mono_class_from_mono_type (type);
1028 if ((result = g_hash_table_lookup (ptr_hash, el_class)))
1030 result = g_new0 (MonoClass, 1);
1032 result->parent = NULL; /* no parent for PTR types */
1033 result->name = "System";
1034 result->name_space = "MonoPtrFakeClass";
1035 result->image = el_class->image;
1036 result->inited = TRUE;
1037 /* Can pointers get boxed? */
1038 result->instance_size = sizeof (gpointer);
1040 * baseval, diffval: need them to allow casting ?
1042 result->element_class = el_class;
1043 result->enum_basetype = &result->element_class->byval_arg;
1045 result->this_arg.type = result->byval_arg.type = MONO_TYPE_PTR;
1046 result->this_arg.data.type = result->byval_arg.data.type = result->enum_basetype;
1047 result->this_arg.byref = TRUE;
1049 g_hash_table_insert (ptr_hash, el_class, result);
1055 mono_class_from_mono_type (MonoType *type)
1057 switch (type->type) {
1058 case MONO_TYPE_OBJECT:
1059 return mono_defaults.object_class;
1060 case MONO_TYPE_VOID:
1061 return mono_defaults.void_class;
1062 case MONO_TYPE_BOOLEAN:
1063 return mono_defaults.boolean_class;
1064 case MONO_TYPE_CHAR:
1065 return mono_defaults.char_class;
1067 return mono_defaults.sbyte_class;
1069 return mono_defaults.byte_class;
1071 return mono_defaults.int16_class;
1073 return mono_defaults.uint16_class;
1075 return mono_defaults.int32_class;
1077 return mono_defaults.uint32_class;
1079 return mono_defaults.int_class;
1081 return mono_defaults.uint_class;
1083 return mono_defaults.int64_class;
1085 return mono_defaults.uint64_class;
1087 return mono_defaults.single_class;
1089 return mono_defaults.double_class;
1090 case MONO_TYPE_STRING:
1091 return mono_defaults.string_class;
1092 case MONO_TYPE_ARRAY:
1093 return mono_array_class_get (type->data.array->type, type->data.array->rank);
1095 return mono_ptr_class_get (type->data.type);
1096 case MONO_TYPE_SZARRAY:
1097 return mono_array_class_get (type->data.type, 1);
1098 case MONO_TYPE_CLASS:
1099 case MONO_TYPE_VALUETYPE:
1100 return type->data.klass;
1102 g_warning ("implement me %02x\n", type->type);
1103 g_assert_not_reached ();
1110 * @image: context where the image is created
1111 * @type_spec: typespec token
1114 mono_class_create_from_typespec (MonoImage *image, guint32 type_spec)
1119 type = mono_type_create_from_typespec (image, type_spec);
1121 switch (type->type) {
1122 case MONO_TYPE_ARRAY:
1123 class = mono_array_class_get (type->data.array->type, type->data.array->rank);
1125 case MONO_TYPE_SZARRAY:
1126 class = mono_array_class_get (type->data.type, 1);
1129 class = mono_class_from_mono_type (type->data.type);
1132 /* it seems any type can be stored in TypeSpec as well */
1133 class = mono_class_from_mono_type (type);
1137 mono_metadata_free_type (type);
1143 * mono_array_class_get:
1144 * @element_type: element type
1145 * @rank: the dimension of the array class
1147 * Returns: a class object describing the array with element type @element_type and
1151 mono_array_class_get (MonoType *element_type, guint32 rank)
1156 MonoClass *parent = NULL;
1160 eclass = mono_class_from_mono_type (element_type);
1161 g_assert (rank <= 255);
1163 parent = mono_defaults.array_class;
1165 if (!parent->inited)
1166 mono_class_init (parent);
1168 image = eclass->image;
1170 if ((list = g_hash_table_lookup (image->array_cache, element_type))) {
1171 for (; list; list = list->next) {
1173 if (class->rank == rank)
1178 class = g_malloc0 (sizeof (MonoClass) + parent->vtable_size * sizeof (gpointer));
1180 class->image = image;
1181 class->name_space = "System";
1182 class->name = "Array";
1183 class->type_token = 0;
1184 class->flags = TYPE_ATTRIBUTE_CLASS;
1185 class->parent = parent;
1186 class->instance_size = mono_class_instance_size (class->parent);
1187 class->class_size = 0;
1188 class->vtable_size = parent->vtable_size;
1189 class->parent->subclasses = g_list_prepend (class->parent->subclasses, class);
1190 mono_compute_relative_numbering (mono_defaults.object_class, &rnum);
1193 class->element_class = eclass;
1195 MonoArrayType *at = g_new0 (MonoArrayType, 1);
1196 class->byval_arg.type = MONO_TYPE_ARRAY;
1197 class->byval_arg.data.array = at;
1198 at->type = &eclass->byval_arg;
1200 /* FIXME: complete.... */
1202 /* FIXME: this is not correct. the lbound could be >0 */
1203 class->byval_arg.type = MONO_TYPE_SZARRAY;
1204 class->byval_arg.data.type = &eclass->byval_arg;
1206 class->this_arg = class->byval_arg;
1207 class->this_arg.byref = 1;
1209 list = g_slist_append (list, class);
1210 g_hash_table_insert (image->array_cache, &class->element_class->byval_arg, list);
1215 * mono_class_instance_size:
1218 * Returns: the size of an object instance
1221 mono_class_instance_size (MonoClass *klass)
1224 if (!klass->size_inited)
1225 mono_class_init (klass);
1227 return klass->instance_size;
1231 * mono_class_value_size:
1234 * This function is used for value types, and return the
1235 * space and the alignment to store that kind of value object.
1237 * Returns: the size of a value of kind @klass
1240 mono_class_value_size (MonoClass *klass, guint32 *align)
1244 /* fixme: check disable, because we still have external revereces to
1245 * mscorlib and Dummy Objects
1247 /*g_assert (klass->valuetype);*/
1249 size = mono_class_instance_size (klass) - sizeof (MonoObject);
1252 *align = klass->min_align;
1258 * mono_class_data_size:
1261 * Returns: the size of the static class data
1264 mono_class_data_size (MonoClass *klass)
1268 mono_class_init (klass);
1270 return klass->class_size;
1274 * Auxiliary routine to mono_class_get_field
1276 * Takes a field index instead of a field token.
1278 static MonoClassField *
1279 mono_class_get_field_idx (MonoClass *class, int idx)
1281 if (class->field.count){
1282 if ((idx >= class->field.first) && (idx < class->field.last)){
1283 return &class->fields [idx - class->field.first];
1290 return mono_class_get_field_idx (class->parent, idx);
1294 * mono_class_get_field:
1295 * @class: the class to lookup the field.
1296 * @field_token: the field token
1298 * Returns: A MonoClassField representing the type and offset of
1299 * the field, or a NULL value if the field does not belong to this
1303 mono_class_get_field (MonoClass *class, guint32 field_token)
1305 int idx = mono_metadata_token_index (field_token);
1307 g_assert (mono_metadata_token_code (field_token) == MONO_TOKEN_FIELD_DEF);
1309 return mono_class_get_field_idx (class, idx - 1);
1313 mono_class_get_field_from_name (MonoClass *klass, const char *name)
1317 for (i = 0; i < klass->field.count; ++i) {
1318 if (strcmp (name, klass->fields [i].name) == 0)
1319 return &klass->fields [i];
1326 * @image: the image where the class resides
1327 * @type_token: the token for the class
1328 * @at: an optional pointer to return the array element type
1330 * Returns: the MonoClass that represents @type_token in @image
1333 mono_class_get (MonoImage *image, guint32 type_token)
1337 switch (type_token & 0xff000000){
1338 case MONO_TOKEN_TYPE_DEF:
1339 class = mono_class_create_from_typedef (image, type_token);
1341 case MONO_TOKEN_TYPE_REF:
1342 class = mono_class_from_typeref (image, type_token);
1344 case MONO_TOKEN_TYPE_SPEC:
1345 class = mono_class_create_from_typespec (image, type_token);
1348 g_warning ("unknown token type %x", type_token & 0xff000000);
1349 g_assert_not_reached ();
1353 g_warning ("Could not load class from token 0x%08x in %s", type_token, image->name);
1358 mono_class_from_name (MonoImage *image, const char* name_space, const char *name)
1360 GHashTable *nspace_table;
1363 nspace_table = g_hash_table_lookup (image->name_cache, name_space);
1366 token = GPOINTER_TO_UINT (g_hash_table_lookup (nspace_table, name));
1369 /*g_warning ("token not found for %s.%s in image %s", name_space, name, image->name);*/
1373 token = MONO_TOKEN_TYPE_DEF | token;
1375 return mono_class_get (image, token);
1379 * mono_array_element_size:
1380 * @ac: pointer to a #MonoArrayClass
1382 * Returns: the size of single array element.
1385 mono_array_element_size (MonoClass *ac)
1387 if (ac->element_class->valuetype)
1388 return mono_class_instance_size (ac->element_class) - sizeof (MonoObject);
1390 return sizeof (gpointer);
1394 mono_ldtoken (MonoImage *image, guint32 token, MonoClass **handle_class)
1396 switch (token & 0xff000000) {
1397 case MONO_TOKEN_TYPE_DEF:
1398 case MONO_TOKEN_TYPE_REF: {
1401 *handle_class = mono_defaults.typehandle_class;
1402 class = mono_class_get (image, token);
1403 mono_class_init (class);
1404 /* We return a MonoType* as handle */
1405 return &class->byval_arg;
1407 case MONO_TOKEN_TYPE_SPEC: {
1410 *handle_class = mono_defaults.typehandle_class;
1411 class = mono_class_create_from_typespec (image, token);
1412 mono_class_init (class);
1413 return &class->byval_arg;
1415 case MONO_TOKEN_FIELD_DEF: {
1417 guint32 type = mono_metadata_typedef_from_field (image, mono_metadata_token_index (token));
1418 class = mono_class_get (image, MONO_TOKEN_TYPE_DEF | type);
1419 mono_class_init (class);
1421 *handle_class = mono_class_from_name (mono_defaults.corlib, "System", "RuntimeFieldHandle");
1422 return mono_class_get_field (class, token);
1424 case MONO_TOKEN_METHOD_DEF:
1425 case MONO_TOKEN_MEMBER_REF:
1427 g_warning ("Unknown token 0x%08x in ldtoken", token);