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.
21 #include <mono/metadata/image.h>
22 #include <mono/metadata/cil-coff.h>
23 #include <mono/metadata/metadata.h>
24 #include <mono/metadata/tabledefs.h>
25 #include <mono/metadata/tokentype.h>
26 #include <mono/metadata/class.h>
27 #include <mono/metadata/object.h>
29 #define CSIZE(x) (sizeof (x) / 4)
32 default_trampoline (MonoMethod *method)
38 default_runtime_class_init (MonoClass *klass)
43 static MonoTrampoline arch_create_jit_trampoline = default_trampoline;
44 static MonoRuntimeClassInit mono_runtime_class_init = default_runtime_class_init;
47 mono_install_trampoline (MonoTrampoline func)
49 arch_create_jit_trampoline = func? func: default_trampoline;
53 mono_install_runtime_class_init (MonoRuntimeClassInit func)
55 mono_runtime_class_init = func? func: default_runtime_class_init;
59 mono_class_create_from_typeref (MonoImage *image, guint32 type_token)
61 guint32 cols [MONO_TYPEREF_SIZE];
62 MonoTableInfo *t = &image->tables [MONO_TABLE_TYPEREF];
64 const char *name, *nspace;
67 mono_metadata_decode_row (t, (type_token&0xffffff)-1, cols, MONO_TYPEREF_SIZE);
68 g_assert ((cols [MONO_TYPEREF_SCOPE] & 0x3) == 2);
69 idx = cols [MONO_TYPEREF_SCOPE] >> 2;
71 if (!image->references || !image->references [idx-1]) {
73 * detected a reference to mscorlib, we simply return a reference to a dummy
74 * until we have a better solution.
76 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 name = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAME]);
86 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAMESPACE]);
88 /* load referenced assembly */
89 image = image->references [idx-1]->image;
91 return mono_class_from_name (image, nspace, name);
95 * class_compute_field_layout:
96 * @m: pointer to the metadata.
97 * @class: The class to initialize
99 * Initializes the class->fields.
101 * Currently we only support AUTO_LAYOUT, and do not even try to do
102 * a good job at it. This is temporary to get the code for Paolo.
105 class_compute_field_layout (MonoClass *class)
107 MonoImage *m = class->image;
108 const int top = class->field.count;
109 guint32 layout = class->flags & TYPE_ATTRIBUTE_LAYOUT_MASK;
110 MonoTableInfo *t = &m->tables [MONO_TABLE_FIELD];
114 * Fetch all the field information.
116 for (i = 0; i < top; i++){
118 guint32 cols [MONO_FIELD_SIZE];
119 int idx = class->field.first + i;
121 mono_metadata_decode_row (t, idx, cols, CSIZE (cols));
122 /* The name is needed for fieldrefs */
123 class->fields [i].name = mono_metadata_string_heap (m, cols [MONO_FIELD_NAME]);
124 sig = mono_metadata_blob_heap (m, cols [MONO_FIELD_SIGNATURE]);
125 mono_metadata_decode_value (sig, &sig);
126 /* FIELD signature == 0x06 */
127 g_assert (*sig == 0x06);
128 class->fields [i].type = mono_metadata_parse_field_type (
129 m, cols [MONO_FIELD_FLAGS], sig + 1, &sig);
130 if (cols [MONO_FIELD_FLAGS] & FIELD_ATTRIBUTE_HAS_FIELD_RVA) {
131 mono_metadata_field_info (m, idx, NULL, &class->fields [i].data, NULL);
132 if (!class->fields [i].data)
133 g_warning ("field %s in %s should have RVA data, but hasn't", class->fields [i].name, class->name);
135 if (class->enumtype && !(cols [MONO_FIELD_FLAGS] & FIELD_ATTRIBUTE_STATIC))
136 class->enum_basetype = class->fields [i].type;
139 * Compute field layout and total size.
142 case TYPE_ATTRIBUTE_AUTO_LAYOUT:
143 case TYPE_ATTRIBUTE_SEQUENTIAL_LAYOUT:
144 for (i = 0; i < top; i++){
147 size = mono_type_size (class->fields [i].type, &align);
148 class->min_align = MAX (align, class->min_align);
149 if (class->fields [i].type->attrs & FIELD_ATTRIBUTE_STATIC) {
150 class->fields [i].offset = class->class_size;
151 class->class_size += (class->class_size % align);
152 class->class_size += size;
154 class->fields [i].offset = class->instance_size;
155 class->instance_size += (class->instance_size % align);
156 class->instance_size += size;
159 if (class->instance_size & (class->min_align - 1)) {
160 class->instance_size += class->min_align - 1;
161 class->instance_size &= ~(class->min_align - 1);
164 case TYPE_ATTRIBUTE_EXPLICIT_LAYOUT:
165 for (i = 0; i < top; i++){
167 int idx = class->field.first + i;
170 * There must be info about all the fields in a type if it
171 * uses explicit layout.
174 size = mono_type_size (class->fields [i].type, &align);
175 if (class->fields [i].type->attrs & FIELD_ATTRIBUTE_STATIC) {
176 class->fields [i].offset = class->class_size;
177 class->class_size += (class->class_size % align);
178 class->class_size += size;
180 mono_metadata_field_info (m, idx, &class->fields [i].offset, NULL, NULL);
181 if (class->fields [i].offset == (guint32)-1)
182 g_warning ("%s not initialized correctly (missing field layout info for %s)", class->name, class->fields [i].name);
184 * The offset is from the start of the object: this works for both
185 * classes and valuetypes.
187 class->fields [i].offset += sizeof (MonoObject);
191 size += class->fields [i].offset;
192 class->instance_size = MAX (class->instance_size, size);
200 mono_class_init (MonoClass *class)
203 MonoMethod **tmp_vtable, **vtable = (MonoMethod **)class->vtable;
204 int i, max_iid, cur_slot = 0;
213 if (!class->parent->inited)
214 mono_class_init (class->parent);
215 class->instance_size += class->parent->instance_size;
216 class->class_size += class->parent->class_size;
217 class->min_align = class->parent->min_align;
218 cur_slot = class->parent->vtable_size;
222 * Computes the size used by the fields, and their locations
224 if (class->field.count > 0){
225 class->fields = g_new0 (MonoClassField, class->field.count);
226 class_compute_field_layout (class);
229 if (class->class_size)
230 class->data = g_malloc0 (class->class_size);
232 /* initialize mothod pointers */
233 class->methods = g_new (MonoMethod*, class->method.count);
234 for (i = 0; i < class->method.count; ++i)
235 class->methods [i] = mono_get_method (class->image,
236 MONO_TOKEN_METHOD_DEF | (i + class->method.first + 1), class);
238 if (class->flags & TYPE_ATTRIBUTE_INTERFACE) {
239 for (i = 0; i < class->method.count; ++i)
240 class->methods [i]->slot = i;
244 //printf ("METAINIT %s.%s\n", class->name_space, class->name);
246 /* compute maximum number of slots and maximum interface id */
248 for (k = class; k ; k = k->parent) {
249 for (i = 0; i < k->interface_count; i++) {
250 ic = k->interfaces [i];
253 mono_class_init (ic);
255 if (max_iid < ic->interface_id)
256 max_iid = ic->interface_id;
260 class->max_interface_id = max_iid;
261 /* compute vtable offset for interfaces */
262 class->interface_offsets = g_malloc (sizeof (gpointer) * (max_iid + 1));
264 for (i = 0; i <= max_iid; i++)
265 class->interface_offsets [i] = NULL;
267 for (i = 0; i < class->interface_count; i++) {
268 ic = class->interfaces [i];
269 class->interface_offsets [ic->interface_id] = &class->vtable [cur_slot];
270 cur_slot += ic->method.count;
273 for (k = class->parent; k ; k = k->parent) {
274 for (i = 0; i < k->interface_count; i++) {
275 ic = k->interfaces [i];
276 if (class->interface_offsets [ic->interface_id] == NULL) {
277 int io = (k->interface_offsets [ic->interface_id] - (gpointer)k->vtable)>>2;
280 g_assert (io <= class->vtable_size);
282 class->interface_offsets [ic->interface_id] = &class->vtable [io];
287 if (class->parent && class->parent->vtable_size)
288 memcpy (class->vtable, class->parent->vtable, sizeof (gpointer) * class->parent->vtable_size);
290 tmp_vtable = alloca (class->vtable_size * sizeof (gpointer));
291 memset (tmp_vtable, 0, class->vtable_size * sizeof (gpointer));
293 for (k = class; k ; k = k->parent) {
294 for (i = 0; i < k->interface_count; i++) {
296 ic = k->interfaces [i];
298 io = (k->interface_offsets [ic->interface_id] - (gpointer)k->vtable)>>2;
301 g_assert (io <= class->vtable_size);
304 for (l = 0; l < ic->method.count; l++) {
305 MonoMethod *im = ic->methods [l];
306 for (j = 0; j < class->method.count; ++j) {
307 MonoMethod *cm = class->methods [j];
308 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
309 !(cm->flags & METHOD_ATTRIBUTE_PUBLIC) ||
310 !(cm->flags & METHOD_ATTRIBUTE_NEW_SLOT))
312 if (!strcmp(cm->name, im->name) &&
313 mono_metadata_signature_equal (cm->signature, im->signature)) {
314 tmp_vtable [io + l] = cm;
319 /* already implemented */
320 if (io >= k->vtable_size)
324 for (l = 0; l < ic->method.count; l++) {
325 MonoMethod *im = ic->methods [l];
328 g_assert (io <= class->vtable_size);
330 if (tmp_vtable [io + l] || vtable [io + l])
333 for (k1 = class; k1; k1 = k1->parent) {
334 for (j = 0; j < k1->method.count; ++j) {
335 MonoMethod *cm = k1->methods [j];
337 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
338 !(cm->flags & METHOD_ATTRIBUTE_PUBLIC))
341 if (!strcmp(cm->name, im->name) &&
342 mono_metadata_signature_equal (cm->signature, im->signature)) {
343 tmp_vtable [io + l] = cm;
348 if (tmp_vtable [io + l])
353 for (l = 0; l < ic->method.count; l++) {
354 MonoMethod *im = ic->methods [l];
356 if (ic->name_space && ic->name_space [0])
357 qname = g_strconcat (ic->name_space, ".", ic->name, ".", im->name, NULL);
359 qname = g_strconcat (ic->name, ".", im->name, NULL);
361 for (j = 0; j < class->method.count; ++j) {
362 MonoMethod *cm = class->methods [j];
364 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL))
367 if (!strcmp (cm->name, qname) &&
368 mono_metadata_signature_equal (cm->signature, im->signature)) {
369 tmp_vtable [io + l] = cm;
377 if (!(class->flags & TYPE_ATTRIBUTE_ABSTRACT)) {
378 for (l = 0; l < ic->method.count; l++) {
379 MonoMethod *im = ic->methods [l];
380 if (!(tmp_vtable [io + l] || vtable [io + l])) {
381 printf ("no implementation for interface method %s.%s::%s in class %s.%s\n",
382 ic->name_space, ic->name, im->name, class->name_space, class->name);
384 for (j = 0; j < class->method.count; ++j) {
385 MonoMethod *cm = class->methods [j];
387 printf ("METHOD %s\n", cm->name);
389 g_assert_not_reached ();
394 for (l = 0; l < ic->method.count; l++) {
395 MonoMethod *im = tmp_vtable [io + l];
397 if (im && !(im->flags & METHOD_ATTRIBUTE_ABSTRACT)) {
399 //printf (" ASLOT%d %s.%s:%s\n", io + l, ic->name_space, ic->name, im->name);
400 vtable [io + l] = arch_create_jit_trampoline (im);
406 for (i = 0; i < class->method.count; ++i) {
409 cm = class->methods [i];
411 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
415 if (!(cm->flags & METHOD_ATTRIBUTE_NEW_SLOT)) {
416 for (k = class->parent; k ; k = k->parent) {
418 for (j = 0; j < k->method.count; ++j) {
419 MonoMethod *m1 = k->methods [j];
420 if (!(m1->flags & METHOD_ATTRIBUTE_VIRTUAL))
422 if (!strcmp(cm->name, m1->name) &&
423 mono_metadata_signature_equal (cm->signature, m1->signature)) {
424 cm->slot = k->methods [j]->slot;
434 cm->slot = cur_slot++;
436 if (!(cm->flags & METHOD_ATTRIBUTE_ABSTRACT))
437 vtable [cm->slot] = arch_create_jit_trampoline (cm);
440 mono_runtime_class_init (class);
443 for (i = 0; i < class->vtable_size; ++i) {
448 printf (" METH%d %p %s %d\n", i, cm, cm->name, cm->slot);
453 //printf ("METAEND %s.%s\n", class->name_space, class->name);
457 * Compute a relative numbering of the class hierarchy as described in
458 * "Java for Large-Scale Scientific Computations?"
461 mono_compute_relative_numbering (MonoClass *class, int *c)
469 for (s = class->subclasses; s; s = s->next)
470 mono_compute_relative_numbering ((MonoClass *)s->data, c);
472 class->diffval = *c - class->baseval;
476 * @image: context where the image is created
477 * @type_token: typedef token
480 mono_class_create_from_typedef (MonoImage *image, guint32 type_token)
482 MonoTableInfo *tt = &image->tables [MONO_TABLE_TYPEDEF];
483 MonoClass *class, *parent = NULL;
484 guint32 cols [MONO_TYPEDEF_SIZE];
485 guint32 cols_next [MONO_TYPEDEF_SIZE];
486 guint tidx = mono_metadata_token_index (type_token);
487 const char *name, *nspace;
488 guint vtsize = 0, icount = 0;
489 static guint interface_id = 0;
490 MonoClass **interfaces;
493 g_assert (mono_metadata_token_table (type_token) == MONO_TABLE_TYPEDEF);
495 mono_metadata_decode_row (tt, tidx - 1, cols, CSIZE (cols));
497 if (tt->rows > tidx) {
498 mono_metadata_decode_row (tt, tidx, cols_next, CSIZE (cols_next));
499 vtsize += cols_next [MONO_TYPEDEF_METHOD_LIST] - cols [MONO_TYPEDEF_METHOD_LIST];
501 vtsize += image->tables [MONO_TABLE_METHOD].rows - cols [MONO_TYPEDEF_METHOD_LIST] + 1;
504 name = mono_metadata_string_heap (image, cols[1]);
505 nspace = mono_metadata_string_heap (image, cols[2]);
507 if (!(!strcmp (nspace, "System") && !strcmp (name, "Object")) &&
508 !(cols [0] & TYPE_ATTRIBUTE_INTERFACE)) {
509 parent = mono_class_get (image, mono_metadata_token_from_dor (cols [3]));
511 interfaces = mono_metadata_interfaces_from_typedef (image, type_token, &icount);
513 for (i = 0; i < icount; i++)
514 vtsize += interfaces [i]->method.count;
517 vtsize += parent->vtable_size;
519 if (cols [0] & TYPE_ATTRIBUTE_INTERFACE)
522 class = g_malloc0 (sizeof (MonoClass) + vtsize * sizeof (gpointer));
524 class->parent = parent;
525 class->interfaces = interfaces;
526 class->interface_count = icount;
527 class->vtable_size = vtsize;
529 class->this_arg.byref = 1;
530 class->this_arg.data.klass = class;
531 class->this_arg.type = MONO_TYPE_CLASS;
532 class->byval_arg.data.klass = class;
533 class->byval_arg.type = MONO_TYPE_CLASS;
536 class->name_space = nspace;
538 class->image = image;
539 class->type_token = type_token;
540 class->flags = cols [0];
542 /*g_print ("Init class %s\n", name);*/
544 /* if root of the hierarchy */
545 if (!strcmp (nspace, "System") && !strcmp (name, "Object")) {
546 class->parent = NULL;
547 class->instance_size = sizeof (MonoObject);
548 } else if (!(cols [0] & TYPE_ATTRIBUTE_INTERFACE)) {
550 class->parent = mono_class_get (image, mono_metadata_token_from_dor (cols [3]));
551 class->valuetype = class->parent->valuetype;
552 class->enumtype = class->parent->enumtype;
553 class->parent->subclasses = g_list_prepend (class->parent->subclasses, class);
554 mono_compute_relative_numbering (mono_defaults.object_class, &rnum);
557 if (!strcmp (nspace, "System")) {
558 if (!strcmp (name, "ValueType")) {
559 class->valuetype = 1;
560 } else if (!strcmp (name, "Enum")) {
561 class->valuetype = 1;
563 } else if (!strcmp (name, "Object")) {
564 class->this_arg.type = class->byval_arg.type = MONO_TYPE_OBJECT;
565 } else if (!strcmp (name, "String")) {
566 class->this_arg.type = class->byval_arg.type = MONO_TYPE_STRING;
570 if (class->valuetype) {
571 int t = MONO_TYPE_VALUETYPE;
572 if (!strcmp (nspace, "System")) {
575 if (!strcmp (name, "Boolean")) {
576 t = MONO_TYPE_BOOLEAN;
577 } else if (!strcmp(name, "Byte")) {
582 if (!strcmp (name, "Char")) {
587 if (!strcmp (name, "Double")) {
592 if (!strcmp (name, "Int32")) {
594 } else if (!strcmp(name, "Int16")) {
596 } else if (!strcmp(name, "Int64")) {
598 } else if (!strcmp(name, "IntPtr")) {
603 if (!strcmp (name, "Single")) {
605 } else if (!strcmp(name, "SByte")) {
610 if (!strcmp (name, "UInt32")) {
612 } else if (!strcmp(name, "UInt16")) {
614 } else if (!strcmp(name, "UInt64")) {
616 } else if (!strcmp(name, "UIntPtr")) {
621 if (!strcmp (name, "Void")) {
629 class->this_arg.type = class->byval_arg.type = t;
633 * Compute the field and method lists
635 class->field.first = cols [MONO_TYPEDEF_FIELD_LIST] - 1;
636 class->method.first = cols [MONO_TYPEDEF_METHOD_LIST] - 1;
638 if (tt->rows > tidx){
639 mono_metadata_decode_row (tt, tidx, cols_next, CSIZE (cols_next));
640 class->field.last = cols_next [MONO_TYPEDEF_FIELD_LIST] - 1;
641 class->method.last = cols_next [MONO_TYPEDEF_METHOD_LIST] - 1;
643 class->field.last = image->tables [MONO_TABLE_FIELD].rows;
644 class->method.last = image->tables [MONO_TABLE_METHOD].rows;
647 if (cols [MONO_TYPEDEF_FIELD_LIST] &&
648 cols [MONO_TYPEDEF_FIELD_LIST] <= image->tables [MONO_TABLE_FIELD].rows)
649 class->field.count = class->field.last - class->field.first;
651 class->field.count = 0;
653 if (cols [MONO_TYPEDEF_METHOD_LIST] <= image->tables [MONO_TABLE_METHOD].rows)
654 class->method.count = class->method.last - class->method.first;
656 class->method.count = 0;
658 /* reserve space to store vector pointer in arrays */
659 if (!strcmp (nspace, "System") && !strcmp (name, "Array")) {
660 class->instance_size += 2 * sizeof (gpointer);
661 g_assert (class->field.count == 0);
664 if (class->flags & TYPE_ATTRIBUTE_INTERFACE)
665 class->interface_id = interface_id++;
667 //class->interfaces = mono_metadata_interfaces_from_typedef (image, type_token, &class->interface_count);
673 mono_class_from_mono_type (MonoType *type)
675 switch (type->type) {
676 case MONO_TYPE_OBJECT:
677 return mono_defaults.object_class;
679 return mono_defaults.void_class;
680 case MONO_TYPE_BOOLEAN:
681 return mono_defaults.boolean_class;
683 return mono_defaults.char_class;
685 return mono_defaults.byte_class;
687 return mono_defaults.sbyte_class;
689 return mono_defaults.int16_class;
691 return mono_defaults.uint16_class;
693 return mono_defaults.int32_class;
695 return mono_defaults.uint32_class;
697 return mono_defaults.int_class;
699 return mono_defaults.uint_class;
701 return mono_defaults.int64_class;
703 return mono_defaults.uint64_class;
705 return mono_defaults.single_class;
707 return mono_defaults.double_class;
708 case MONO_TYPE_STRING:
709 return mono_defaults.string_class;
710 case MONO_TYPE_ARRAY:
711 return mono_defaults.array_class;
712 case MONO_TYPE_SZARRAY:
714 /* Not really sure about these. */
715 return mono_class_from_mono_type (type->data.type);
716 case MONO_TYPE_CLASS:
717 case MONO_TYPE_VALUETYPE:
718 return type->data.klass;
720 g_warning ("implement me %02x\n", type->type);
721 g_assert_not_reached ();
728 * @image: context where the image is created
729 * @type_spec: typespec token
730 * @at: an optional pointer to return the array type
733 mono_class_create_from_typespec (MonoImage *image, guint32 type_spec)
735 guint32 idx = mono_metadata_token_index (type_spec);
737 guint32 cols [MONO_TYPESPEC_SIZE];
741 MonoClass *class, *eclass;
743 t = &image->tables [MONO_TABLE_TYPESPEC];
745 mono_metadata_decode_row (t, idx-1, cols, MONO_TYPESPEC_SIZE);
746 ptr = mono_metadata_blob_heap (image, cols [MONO_TYPESPEC_SIGNATURE]);
747 len = mono_metadata_decode_value (ptr, &ptr);
748 type = mono_metadata_parse_type (image, MONO_PARSE_TYPE, 0, ptr, &ptr);
750 switch (type->type) {
751 case MONO_TYPE_ARRAY:
752 eclass = mono_class_from_mono_type (type->data.array->type);
753 class = mono_array_class_get (eclass, type->data.array->rank);
755 case MONO_TYPE_SZARRAY:
756 eclass = mono_class_from_mono_type (type->data.type);
757 class = mono_array_class_get (eclass, 1);
760 g_warning ("implement me: %08x", type->type);
761 g_assert_not_reached ();
764 mono_metadata_free_type (type);
770 * mono_array_class_get:
771 * @eclass: element type class
772 * @rank: the dimension of the array class
774 * Returns: a class object describing the array with element type @etype and
778 mono_array_class_get (MonoClass *eclass, guint32 rank)
782 static MonoClass *parent = NULL;
786 g_assert (rank <= 255);
789 parent = mono_defaults.array_class;
792 mono_class_init (parent);
794 image = eclass->image;
796 g_assert (!eclass->type_token ||
797 mono_metadata_token_table (eclass->type_token) == MONO_TABLE_TYPEDEF);
799 key = ((rank & 0xff) << 24) | (eclass->type_token & 0xffffff);
800 if ((class = g_hash_table_lookup (image->array_cache, GUINT_TO_POINTER (key))))
803 class = g_malloc0 (sizeof (MonoClass) + parent->vtable_size * sizeof (gpointer));
805 class->image = image;
806 class->name_space = "System";
807 class->name = "Array";
808 class->type_token = 0;
809 class->flags = TYPE_ATTRIBUTE_CLASS;
810 class->parent = parent;
811 class->instance_size = mono_class_instance_size (class->parent);
812 class->class_size = 0;
813 class->vtable_size = parent->vtable_size;
814 class->parent->subclasses = g_list_prepend (class->parent->subclasses, class);
815 mono_compute_relative_numbering (mono_defaults.object_class, &rnum);
818 class->element_class = eclass;
820 class->byval_arg.type = MONO_TYPE_ARRAY;
821 /* FIXME: complete.... */
823 class->byval_arg.type = MONO_TYPE_SZARRAY;
824 class->byval_arg.data.type = &eclass->byval_arg;
826 class->this_arg = class->byval_arg;
827 class->this_arg.byref = 1;
829 g_hash_table_insert (image->array_cache, GUINT_TO_POINTER (key), class);
834 * mono_class_instance_size:
837 * Returns: the size of an object instance
840 mono_class_instance_size (MonoClass *klass)
844 mono_class_init (klass);
846 return klass->instance_size;
850 * mono_class_value_size:
853 * This function is used for value types, and return the
854 * space and the alignment to store that kind of value object.
856 * Returns: the size of a value of kind @klass
859 mono_class_value_size (MonoClass *klass, guint32 *align)
863 /* fixme: check disable, because we still have external revereces to
864 * mscorlib and Dummy Objects
866 /*g_assert (klass->valuetype);*/
868 size = mono_class_instance_size (klass) - sizeof (MonoObject);
881 * mono_class_data_size:
884 * Returns: the size of the static class data
887 mono_class_data_size (MonoClass *klass)
891 mono_class_init (klass);
893 return klass->class_size;
897 * Auxiliary routine to mono_class_get_field
899 * Takes a field index instead of a field token.
901 static MonoClassField *
902 mono_class_get_field_idx (MonoClass *class, int idx)
904 if (class->field.count){
905 if ((idx >= class->field.first) && (idx < class->field.last)){
906 return &class->fields [idx - class->field.first];
913 return mono_class_get_field_idx (class->parent, idx);
917 * mono_class_get_field:
918 * @class: the class to lookup the field.
919 * @field_token: the field token
921 * Returns: A MonoClassField representing the type and offset of
922 * the field, or a NULL value if the field does not belong to this
926 mono_class_get_field (MonoClass *class, guint32 field_token)
928 int idx = mono_metadata_token_index (field_token);
930 if (mono_metadata_token_code (field_token) == MONO_TOKEN_MEMBER_REF)
931 g_error ("Unsupported Field Token is a MemberRef, implement me");
933 g_assert (mono_metadata_token_code (field_token) == MONO_TOKEN_FIELD_DEF);
935 return mono_class_get_field_idx (class, idx - 1);
939 mono_class_get_field_from_name (MonoClass *klass, const char *name)
943 MonoTableInfo *t = &klass->image->tables [MONO_TABLE_FIELD];
945 for (i = 0; i < klass->field.count; ++i) {
946 token = mono_metadata_decode_row_col (t, klass->field.first + i, MONO_FIELD_NAME);
947 if (strcmp (name, mono_metadata_string_heap (klass->image, token)) == 0)
948 return &klass->fields [i];
955 * @image: the image where the class resides
956 * @type_token: the token for the class
957 * @at: an optional pointer to return the array element type
959 * Returns: the MonoClass that represents @type_token in @image
962 mono_class_get (MonoImage *image, guint32 type_token)
966 switch (type_token & 0xff000000){
967 case MONO_TOKEN_TYPE_DEF:
968 if ((class = g_hash_table_lookup (image->class_cache,
969 GUINT_TO_POINTER (type_token))))
971 class = mono_class_create_from_typedef (image, type_token);
973 case MONO_TOKEN_TYPE_REF:
974 return mono_class_create_from_typeref (image, type_token);
975 case MONO_TOKEN_TYPE_SPEC:
976 if ((class = g_hash_table_lookup (image->class_cache,
977 GUINT_TO_POINTER (type_token))))
980 class = mono_class_create_from_typespec (image, type_token);
983 g_warning ("unknown token type %x", type_token & 0xff000000);
984 g_assert_not_reached ();
987 g_hash_table_insert (image->class_cache, GUINT_TO_POINTER (type_token), class);
993 mono_class_from_name (MonoImage *image, const char* name_space, const char *name)
995 GHashTable *nspace_table;
998 nspace_table = g_hash_table_lookup (image->name_cache, name_space);
1001 token = GPOINTER_TO_UINT (g_hash_table_lookup (nspace_table, name));
1004 g_warning ("token not found for %s.%s in image %s", name_space, name, image->name);
1008 token = MONO_TOKEN_TYPE_DEF | token;
1010 return mono_class_get (image, token);
1014 * mono_array_element_size:
1015 * @ac: pointer to a #MonoArrayClass
1017 * Returns: the size of single array element.
1020 mono_array_element_size (MonoClass *ac)
1024 esize = mono_class_instance_size (ac->element_class);
1026 if (ac->element_class->valuetype)
1027 esize -= sizeof (MonoObject);
1033 mono_ldtoken (MonoImage *image, guint32 token, MonoClass **handle_class)
1035 switch (token & 0xff000000) {
1036 case MONO_TOKEN_TYPE_DEF:
1037 case MONO_TOKEN_TYPE_REF: {
1040 *handle_class = mono_defaults.typehandle_class;
1041 class = mono_class_get (image, token);
1042 /* We return a MonoType* as handle */
1043 return &class->byval_arg;
1045 case MONO_TOKEN_TYPE_SPEC: {
1048 *handle_class = mono_defaults.typehandle_class;
1049 if ((class = g_hash_table_lookup (image->class_cache,
1050 GUINT_TO_POINTER (token))))
1051 return &class->byval_arg;
1052 class = mono_class_create_from_typespec (image, token);
1053 return &class->byval_arg;
1055 case MONO_TOKEN_FIELD_DEF: {
1057 guint32 type = mono_metadata_typedef_from_field (image, mono_metadata_token_index (token));
1058 class = mono_class_get (image, MONO_TOKEN_TYPE_DEF | type);
1059 mono_class_init (class);
1061 *handle_class = mono_class_from_name (mono_defaults.corlib, "System", "RuntimeFieldHandle");
1062 return mono_class_get_field (class, token);
1064 case MONO_TOKEN_METHOD_DEF:
1065 case MONO_TOKEN_MEMBER_REF:
1067 g_warning ("Unknown token 0x%08x in ldtoken", token);