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/assembly.h>
24 #include <mono/metadata/cil-coff.h>
25 #include <mono/metadata/metadata.h>
26 #include <mono/metadata/tabledefs.h>
27 #include <mono/metadata/tokentype.h>
28 #include <mono/metadata/class.h>
29 #include <mono/metadata/object.h>
30 #include <mono/metadata/appdomain.h>
31 #include <mono/metadata/mono-endian.h>
32 #include <mono/metadata/debug-helpers.h>
33 #include <mono/metadata/reflection.h>
34 #include <mono/os/gc_wrapper.h>
36 #define CSIZE(x) (sizeof (x) / 4)
40 gboolean mono_print_vtable = FALSE;
42 static MonoClass * mono_class_create_from_typedef (MonoImage *image, guint32 type_token);
44 void (*mono_debugger_class_init_func) (MonoClass *klass) = NULL;
47 mono_class_from_typeref (MonoImage *image, guint32 type_token)
49 guint32 cols [MONO_TYPEREF_SIZE];
50 MonoTableInfo *t = &image->tables [MONO_TABLE_TYPEREF];
52 const char *name, *nspace;
54 MonoAssembly **references;
56 mono_metadata_decode_row (t, (type_token&0xffffff)-1, cols, MONO_TYPEREF_SIZE);
58 name = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAME]);
59 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAMESPACE]);
61 idx = cols [MONO_TYPEREF_SCOPE] >> RESOLTION_SCOPE_BITS;
62 switch (cols [MONO_TYPEREF_SCOPE] & RESOLTION_SCOPE_MASK) {
63 case RESOLTION_SCOPE_MODULE:
65 g_error ("null ResolutionScope not yet handled");
66 /* a typedef in disguise */
67 return mono_class_from_name (image, nspace, name);
68 case RESOLTION_SCOPE_MODULEREF:
69 return mono_class_from_name (image->modules [idx - 1], nspace, name);
70 case RESOLTION_SCOPE_TYPEREF: {
71 MonoClass *enclosing = mono_class_from_typeref (image, MONO_TOKEN_TYPE_REF | idx);
73 mono_class_init (enclosing);
74 for (tmp = enclosing->nested_classes; tmp; tmp = tmp->next) {
76 if (strcmp (res->name, name) == 0)
79 g_warning ("TypeRef ResolutionScope not yet handled (%d)", idx);
82 case RESOLTION_SCOPE_ASSEMBLYREF:
86 references = image->references;
87 if (!references || !references [idx-1]) {
89 * detected a reference to mscorlib, we simply return a reference to a dummy
90 * until we have a better solution.
92 fprintf(stderr, "Sending dummy where %s.%s expected\n", mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAMESPACE]), mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAME]));
94 res = mono_class_from_name (image, "System", "MonoDummy");
95 /* prevent method loading */
97 /* some storage if the type is used - very ugly hack */
98 res->instance_size = 2*sizeof (gpointer);
102 /* load referenced assembly */
103 image = references [idx-1]->image;
105 return mono_class_from_name (image, nspace, name);
109 dup_type (MonoType* t)
111 MonoType *r = g_new0 (MonoType, 1);
117 mono_type_get_name_recurse (MonoType *type, GString *str)
121 switch (type->type) {
122 case MONO_TYPE_ARRAY: {
123 int i, rank = type->data.array->rank;
125 mono_type_get_name_recurse (&type->data.array->eklass->byval_arg, str);
126 g_string_append_c (str, '[');
127 for (i = 1; i < rank; i++)
128 g_string_append_c (str, ',');
129 g_string_append_c (str, ']');
132 case MONO_TYPE_SZARRAY:
133 mono_type_get_name_recurse (&type->data.klass->byval_arg, str);
134 g_string_append (str, "[]");
137 mono_type_get_name_recurse (type->data.type, str);
138 g_string_append_c (str, '*');
141 klass = mono_class_from_mono_type (type);
142 if (klass->nested_in) {
143 mono_type_get_name_recurse (&klass->nested_in->byval_arg, str);
144 g_string_append_c (str, '+');
146 if (*klass->name_space) {
147 g_string_append (str, klass->name_space);
148 g_string_append_c (str, '.');
150 g_string_append (str, klass->name);
156 * mono_type_get_name:
159 * Returns the string representation for type as required by System.Reflection.
160 * The inverse of mono_reflection_parse_type ().
163 mono_type_get_name (MonoType *type)
165 GString* result = g_string_new ("");
166 mono_type_get_name_recurse (type, result);
169 g_string_append_c (result, '&');
171 return g_string_free (result, FALSE);
175 mono_class_is_open_constructed_type (MonoType *t)
181 case MONO_TYPE_SZARRAY:
182 return mono_class_is_open_constructed_type (&t->data.klass->byval_arg);
183 case MONO_TYPE_ARRAY:
184 return mono_class_is_open_constructed_type (&t->data.array->eklass->byval_arg);
186 return mono_class_is_open_constructed_type (t->data.type);
187 case MONO_TYPE_GENERICINST: {
188 MonoGenericInst *ginst = t->data.generic_inst;
191 if (mono_class_is_open_constructed_type (ginst->generic_type))
193 for (i = 0; i < ginst->type_argc; i++)
194 if (mono_class_is_open_constructed_type (ginst->type_argv [i]))
204 mono_class_inflate_generic_type (MonoType *type, MonoGenericInst *ginst)
206 switch (type->type) {
209 return dup_type (ginst->type_argv [type->data.generic_param->num]);
212 case MONO_TYPE_VAR: {
213 MonoType *t = ginst->type_argv [type->data.generic_param->num];
215 if ((t->type == MONO_TYPE_VAR) || (t->type == MONO_TYPE_MVAR))
220 case MONO_TYPE_SZARRAY: {
221 MonoClass *eclass = type->data.klass;
224 if (eclass->byval_arg.type == MONO_TYPE_MVAR) {
225 nclass = mono_class_from_mono_type (ginst->type_argv [eclass->byval_arg.data.generic_param->num]);
226 } else if (eclass->byval_arg.type == MONO_TYPE_VAR) {
227 nclass = mono_class_from_mono_type (ginst->type_argv [eclass->byval_arg.data.generic_param->num]);
231 nt = dup_type (type);
232 nt->data.klass = nclass;
242 mono_class_inflate_generic_signature (MonoImage *image, MonoMethodSignature *sig, MonoGenericInst *ginst)
244 MonoMethodSignature *res;
246 res = mono_metadata_signature_alloc (image, sig->param_count);
247 res->ret = mono_class_inflate_generic_type (sig->ret, ginst);
248 for (i = 0; i < sig->param_count; ++i) {
249 res->params [i] = mono_class_inflate_generic_type (sig->params [i], ginst);
251 res->hasthis = sig->hasthis;
252 res->explicit_this = sig->explicit_this;
253 res->call_convention = sig->call_convention;
254 res->generic_param_count = sig->generic_param_count;
258 static MonoMethodHeader*
259 inflate_generic_header (MonoMethodHeader *header, MonoGenericInst *ginst)
261 MonoMethodHeader *res;
263 res = g_malloc0 (sizeof (MonoMethodHeader) + sizeof (gpointer) * header->num_locals);
264 res->code = header->code;
265 res->code_size = header->code_size;
266 res->max_stack = header->max_stack;
267 res->num_clauses = header->num_clauses;
268 res->init_locals = header->init_locals;
269 res->num_locals = header->num_locals;
270 res->clauses = header->clauses;
271 res->gen_params = header->gen_params;
272 res->geninst = ginst;
273 for (i = 0; i < header->num_locals; ++i) {
274 res->locals [i] = mono_class_inflate_generic_type (header->locals [i], ginst);
280 mono_class_inflate_generic_method (MonoMethod *method, MonoGenericInst *ginst)
283 if ((method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) || (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)) {
284 MonoMethodPInvoke *nmethod = g_new0 (MonoMethodPInvoke, 1);
285 *nmethod = *(MonoMethodPInvoke*)method;
286 result = (MonoMethod*)nmethod;
288 MonoMethodNormal *nmethod = g_new0 (MonoMethodNormal, 1);
289 *nmethod = *(MonoMethodNormal*)method;
290 result = (MonoMethod*)nmethod;
292 nmethod->header = inflate_generic_header (nmethod->header, ginst);
295 result->klass = ginst->klass;
296 result->signature = mono_class_inflate_generic_signature (method->klass->image, result->signature, ginst);
301 * class_compute_field_layout:
302 * @m: pointer to the metadata.
303 * @class: The class to initialize
305 * Initializes the class->fields.
307 * Currently we only support AUTO_LAYOUT, and do not even try to do
308 * a good job at it. This is temporary to get the code for Paolo.
311 class_compute_field_layout (MonoClass *class)
313 MonoImage *m = class->image;
314 const int top = class->field.count;
315 guint32 layout = class->flags & TYPE_ATTRIBUTE_LAYOUT_MASK;
316 MonoTableInfo *t = &m->tables [MONO_TABLE_FIELD];
317 int i, blittable = TRUE, real_size = 0;
319 guint32 packing_size = 0;
320 gboolean explicit_size;
321 MonoClassField *field;
323 if (class->size_inited)
327 if (!class->parent->size_inited)
328 class_compute_field_layout (class->parent);
329 class->instance_size += class->parent->instance_size;
330 class->min_align = class->parent->min_align;
331 blittable = class->blittable;
333 class->instance_size = sizeof (MonoObject);
334 class->min_align = 1;
337 /* Get the real size */
338 explicit_size = mono_metadata_packing_from_typedef (class->image, class->type_token, &packing_size, &real_size);
341 g_assert ((packing_size & 0xfffffff0) == 0);
342 class->packing_size = packing_size;
343 real_size += class->instance_size;
347 if (explicit_size && real_size) {
348 class->instance_size = MAX (real_size, class->instance_size);
350 class->size_inited = 1;
354 class->fields = g_new0 (MonoClassField, top);
357 * Fetch all the field information.
359 for (i = 0; i < top; i++){
361 guint32 cols [MONO_FIELD_SIZE];
362 int idx = class->field.first + i;
364 field = &class->fields [i];
365 mono_metadata_decode_row (t, idx, cols, CSIZE (cols));
366 /* The name is needed for fieldrefs */
367 field->name = mono_metadata_string_heap (m, cols [MONO_FIELD_NAME]);
368 sig = mono_metadata_blob_heap (m, cols [MONO_FIELD_SIGNATURE]);
369 mono_metadata_decode_value (sig, &sig);
370 /* FIELD signature == 0x06 */
371 g_assert (*sig == 0x06);
372 field->type = mono_metadata_parse_field_type (
373 m, cols [MONO_FIELD_FLAGS], sig + 1, &sig);
374 if (mono_field_is_deleted (field))
376 if (class->generic_inst) {
377 field->type = mono_class_inflate_generic_type (field->type, class->generic_inst->data.generic_inst);
378 field->type->attrs = cols [MONO_FIELD_FLAGS];
381 field->parent = class;
383 if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC)) {
384 if (field->type->byref) {
387 MonoClass *field_class = mono_class_from_mono_type (field->type);
388 if (!field_class || !field_class->blittable)
392 if (layout == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT) {
393 mono_metadata_field_info (m, idx, &field->offset, NULL, NULL);
394 if (field->offset == (guint32)-1)
395 g_warning ("%s not initialized correctly (missing field layout info for %s)", class->name, field->name);
398 if (cols [MONO_FIELD_FLAGS] & FIELD_ATTRIBUTE_HAS_FIELD_RVA) {
399 mono_metadata_field_info (m, idx, NULL, &rva, NULL);
401 g_warning ("field %s in %s should have RVA data, but hasn't", field->name, class->name);
402 field->data = mono_cli_rva_map (class->image->image_info, rva);
405 if (class->enumtype && !(cols [MONO_FIELD_FLAGS] & FIELD_ATTRIBUTE_STATIC)) {
406 class->enum_basetype = field->type;
407 class->cast_class = class->element_class = mono_class_from_mono_type (class->enum_basetype);
408 blittable = class->element_class->blittable;
411 /* The def_value of fields is compute lazily during vtable creation */
414 if (class == mono_defaults.string_class)
417 class->blittable = blittable;
419 if (class->enumtype && !class->enum_basetype) {
420 if (!((strcmp (class->name, "Enum") == 0) && (strcmp (class->name_space, "System") == 0)))
423 if (explicit_size && real_size) {
424 class->instance_size = MAX (real_size, class->instance_size);
427 if (class->gen_params)
430 mono_class_layout_fields (class);
434 mono_class_layout_fields (MonoClass *class)
437 const int top = class->field.count;
438 guint32 layout = class->flags & TYPE_ATTRIBUTE_LAYOUT_MASK;
439 guint32 pass, passes, real_size;
440 gboolean gc_aware_layout = FALSE;
441 MonoClassField *field;
444 * Enable GC aware auto layout: in this mode, reference
445 * fields are grouped together inside objects, increasing collector
447 * Requires that all classes whose layout is known to native code be annotated
448 * with [StructLayout (LayoutKind.Sequential)]
450 /* corlib is missing [StructLayout] directives in many places */
451 if (layout == TYPE_ATTRIBUTE_AUTO_LAYOUT) {
452 if (class->image != mono_defaults.corlib)
453 gc_aware_layout = TRUE;
457 * Compute field layout and total size (not considering static fields)
461 case TYPE_ATTRIBUTE_AUTO_LAYOUT:
462 case TYPE_ATTRIBUTE_SEQUENTIAL_LAYOUT:
469 if (layout != TYPE_ATTRIBUTE_AUTO_LAYOUT)
473 real_size = class->parent->instance_size;
475 real_size = sizeof (MonoObject);
477 for (pass = 0; pass < passes; ++pass) {
478 for (i = 0; i < top; i++){
480 field = &class->fields [i];
482 if (mono_field_is_deleted (field))
484 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
487 if (gc_aware_layout) {
489 * We process fields with reference type in the first pass,
490 * and fields with non-reference type in the second pass.
491 * We use IS_POINTER instead of IS_REFERENCE because in
492 * some internal structures, we store GC_MALLOCed memory
493 * in IntPtr fields...
495 if (MONO_TYPE_IS_POINTER (field->type)) {
504 if ((top == 1) && (class->instance_size == sizeof (MonoObject)) &&
505 (strcmp (field->name, "$PRIVATE$") == 0)) {
506 /* This field is a hack inserted by MCS to empty structures */
510 size = mono_type_size (field->type, &align);
512 /* FIXME (LAMESPEC): should we also change the min alignment according to pack? */
513 align = class->packing_size ? MIN (class->packing_size, align): align;
514 class->min_align = MAX (align, class->min_align);
515 field->offset = real_size;
516 field->offset += align - 1;
517 field->offset &= ~(align - 1);
518 real_size = field->offset + size;
521 class->instance_size = MAX (real_size, class->instance_size);
523 if (class->instance_size & (class->min_align - 1)) {
524 class->instance_size += class->min_align - 1;
525 class->instance_size &= ~(class->min_align - 1);
529 case TYPE_ATTRIBUTE_EXPLICIT_LAYOUT:
531 for (i = 0; i < top; i++) {
533 field = &class->fields [i];
536 * There must be info about all the fields in a type if it
537 * uses explicit layout.
540 if (mono_field_is_deleted (field))
542 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
545 size = mono_type_size (field->type, &align);
548 * When we get here, field->offset is already set by the
549 * loader (for either runtime fields or fields loaded from metadata).
550 * The offset is from the start of the object: this works for both
551 * classes and valuetypes.
553 field->offset += sizeof (MonoObject);
558 real_size = MAX (real_size, size + field->offset);
560 class->instance_size = MAX (real_size, class->instance_size);
564 class->size_inited = 1;
567 * Compute static field layout and size
569 for (i = 0; i < top; i++){
571 field = &class->fields [i];
573 if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC))
575 if (mono_field_is_deleted (field))
578 size = mono_type_size (field->type, &align);
579 field->offset = class->class_size;
580 field->offset += align - 1;
581 field->offset &= ~(align - 1);
582 class->class_size = field->offset + size;
587 init_properties (MonoClass *class)
589 guint startm, endm, i, j;
590 guint32 cols [MONO_PROPERTY_SIZE];
591 MonoTableInfo *pt = &class->image->tables [MONO_TABLE_PROPERTY];
592 MonoTableInfo *msemt = &class->image->tables [MONO_TABLE_METHODSEMANTICS];
594 class->property.first = mono_metadata_properties_from_typedef (class->image, mono_metadata_token_index (class->type_token) - 1, &class->property.last);
595 class->property.count = class->property.last - class->property.first;
597 class->properties = g_new0 (MonoProperty, class->property.count);
598 for (i = class->property.first; i < class->property.last; ++i) {
599 mono_metadata_decode_row (pt, i, cols, MONO_PROPERTY_SIZE);
600 class->properties [i - class->property.first].attrs = cols [MONO_PROPERTY_FLAGS];
601 class->properties [i - class->property.first].name = mono_metadata_string_heap (class->image, cols [MONO_PROPERTY_NAME]);
603 startm = mono_metadata_methods_from_property (class->image, i, &endm);
604 for (j = startm; j < endm; ++j) {
605 mono_metadata_decode_row (msemt, j, cols, MONO_METHOD_SEMA_SIZE);
606 switch (cols [MONO_METHOD_SEMA_SEMANTICS]) {
607 case METHOD_SEMANTIC_SETTER:
608 class->properties [i - class->property.first].set = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
610 case METHOD_SEMANTIC_GETTER:
611 class->properties [i - class->property.first].get = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
621 init_events (MonoClass *class)
623 guint startm, endm, i, j;
624 guint32 cols [MONO_EVENT_SIZE];
625 MonoTableInfo *pt = &class->image->tables [MONO_TABLE_EVENT];
626 MonoTableInfo *msemt = &class->image->tables [MONO_TABLE_METHODSEMANTICS];
628 class->event.first = mono_metadata_events_from_typedef (class->image, mono_metadata_token_index (class->type_token) - 1, &class->event.last);
629 class->event.count = class->event.last - class->event.first;
631 class->events = g_new0 (MonoEvent, class->event.count);
632 for (i = class->event.first; i < class->event.last; ++i) {
633 mono_metadata_decode_row (pt, i, cols, MONO_EVENT_SIZE);
634 class->events [i - class->event.first].attrs = cols [MONO_EVENT_FLAGS];
635 class->events [i - class->event.first].name = mono_metadata_string_heap (class->image, cols [MONO_EVENT_NAME]);
637 startm = mono_metadata_methods_from_event (class->image, i, &endm);
638 for (j = startm; j < endm; ++j) {
639 mono_metadata_decode_row (msemt, j, cols, MONO_METHOD_SEMA_SIZE);
640 switch (cols [MONO_METHOD_SEMA_SEMANTICS]) {
641 case METHOD_SEMANTIC_ADD_ON:
642 class->events [i - class->event.first].add = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
644 case METHOD_SEMANTIC_REMOVE_ON:
645 class->events [i - class->event.first].remove = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
647 case METHOD_SEMANTIC_FIRE:
648 class->events [i - class->event.first].raise = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
650 case METHOD_SEMANTIC_OTHER: /* don't care for now */
659 mono_get_unique_iid (MonoClass *class)
661 static GHashTable *iid_hash = NULL;
662 static guint iid = 0;
667 g_assert (class->flags & TYPE_ATTRIBUTE_INTERFACE);
672 iid_hash = g_hash_table_new (g_str_hash, g_str_equal);
674 str = g_strdup_printf ("%s|%s.%s\n", class->image->name, class->name_space, class->name);
676 if (g_hash_table_lookup_extended (iid_hash, str, NULL, &value)) {
677 mono_loader_unlock ();
681 g_hash_table_insert (iid_hash, str, (gpointer)iid);
685 mono_loader_unlock ();
691 collect_implemented_interfaces_aux (MonoClass *klass, GPtrArray **res)
696 for (i = 0; i < klass->interface_count; i++) {
697 ic = klass->interfaces [i];
700 *res = g_ptr_array_new ();
701 g_ptr_array_add (*res, ic);
703 collect_implemented_interfaces_aux (ic, res);
707 static inline GPtrArray*
708 collect_implemented_interfaces (MonoClass *klass)
710 GPtrArray *res = NULL;
712 collect_implemented_interfaces_aux (klass, &res);
717 setup_interface_offsets (MonoClass *class, int cur_slot)
723 /* compute maximum number of slots and maximum interface id */
725 for (k = class; k ; k = k->parent) {
726 for (i = 0; i < k->interface_count; i++) {
727 ic = k->interfaces [i];
730 mono_class_init (ic);
732 if (max_iid < ic->interface_id)
733 max_iid = ic->interface_id;
737 if (class->flags & TYPE_ATTRIBUTE_INTERFACE) {
738 if (max_iid < class->interface_id)
739 max_iid = class->interface_id;
741 class->max_interface_id = max_iid;
742 /* compute vtable offset for interfaces */
743 class->interface_offsets = g_malloc (sizeof (gpointer) * (max_iid + 1));
745 for (i = 0; i <= max_iid; i++)
746 class->interface_offsets [i] = -1;
748 ifaces = collect_implemented_interfaces (class);
750 for (i = 0; i < ifaces->len; ++i) {
751 ic = g_ptr_array_index (ifaces, i);
752 class->interface_offsets [ic->interface_id] = cur_slot;
753 cur_slot += ic->method.count;
755 g_ptr_array_free (ifaces, TRUE);
758 for (k = class->parent; k ; k = k->parent) {
759 ifaces = collect_implemented_interfaces (k);
761 for (i = 0; i < ifaces->len; ++i) {
762 ic = g_ptr_array_index (ifaces, i);
764 if (class->interface_offsets [ic->interface_id] == -1) {
765 int io = k->interface_offsets [ic->interface_id];
769 class->interface_offsets [ic->interface_id] = io;
772 g_ptr_array_free (ifaces, TRUE);
776 if (class->flags & TYPE_ATTRIBUTE_INTERFACE)
777 class->interface_offsets [class->interface_id] = cur_slot;
783 mono_class_setup_vtable (MonoClass *class, MonoMethod **overrides, int onum)
787 int i, max_vtsize = 0, max_iid, cur_slot = 0;
789 MonoGHashTable *override_map = NULL;
791 /* setup_vtable() must be called only once on the type */
792 if (class->interface_offsets) {
793 g_warning ("vtable already computed in %s.%s", class->name_space, class->name);
797 ifaces = collect_implemented_interfaces (class);
799 for (i = 0; i < ifaces->len; i++) {
800 MonoClass *ic = g_ptr_array_index (ifaces, i);
801 max_vtsize += ic->method.count;
803 g_ptr_array_free (ifaces, TRUE);
807 max_vtsize += class->parent->vtable_size;
808 cur_slot = class->parent->vtable_size;
811 max_vtsize += class->method.count;
813 vtable = alloca (sizeof (gpointer) * max_vtsize);
814 memset (vtable, 0, sizeof (gpointer) * max_vtsize);
816 /* printf ("METAINIT %s.%s\n", class->name_space, class->name); */
818 cur_slot = setup_interface_offsets (class, cur_slot);
819 max_iid = class->max_interface_id;
821 if (class->parent && class->parent->vtable_size)
822 memcpy (vtable, class->parent->vtable, sizeof (gpointer) * class->parent->vtable_size);
824 /* override interface methods */
825 for (i = 0; i < onum; i++) {
826 MonoMethod *decl = overrides [i*2];
827 if (decl->klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
829 g_assert (decl->slot != -1);
830 dslot = decl->slot + class->interface_offsets [decl->klass->interface_id];
831 vtable [dslot] = overrides [i*2 + 1];
832 vtable [dslot]->slot = dslot;
834 override_map = mono_g_hash_table_new (NULL, NULL);
836 mono_g_hash_table_insert (override_map, overrides [i * 2], overrides [i * 2 + 1]);
840 for (k = class; k ; k = k->parent) {
842 ifaces = collect_implemented_interfaces (k);
844 nifaces = ifaces->len;
845 for (i = 0; i < nifaces; i++) {
848 ic = g_ptr_array_index (ifaces, i);
849 io = k->interface_offsets [ic->interface_id];
852 g_assert (io <= max_vtsize);
855 for (l = 0; l < ic->method.count; l++) {
856 MonoMethod *im = ic->methods [l];
858 if (vtable [io + l] && !(vtable [io + l]->flags & METHOD_ATTRIBUTE_ABSTRACT))
861 for (j = 0; j < class->method.count; ++j) {
862 MonoMethod *cm = class->methods [j];
863 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
864 !((cm->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == METHOD_ATTRIBUTE_PUBLIC) ||
865 !(cm->flags & METHOD_ATTRIBUTE_NEW_SLOT))
867 if (!strcmp(cm->name, im->name) &&
868 mono_metadata_signature_equal (cm->signature, im->signature)) {
869 g_assert (io + l <= max_vtsize);
870 vtable [io + l] = cm;
875 /* already implemented */
876 if (io >= k->vtable_size)
880 for (l = 0; l < ic->method.count; l++) {
881 MonoMethod *im = ic->methods [l];
884 g_assert (io + l <= max_vtsize);
886 if (vtable [io + l] && !(vtable [io + l]->flags & METHOD_ATTRIBUTE_ABSTRACT))
889 for (k1 = class; k1; k1 = k1->parent) {
890 for (j = 0; j < k1->method.count; ++j) {
891 MonoMethod *cm = k1->methods [j];
893 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
894 !(cm->flags & METHOD_ATTRIBUTE_PUBLIC))
897 if (!strcmp(cm->name, im->name) &&
898 mono_metadata_signature_equal (cm->signature, im->signature)) {
899 g_assert (io + l <= max_vtsize);
900 vtable [io + l] = cm;
905 g_assert (io + l <= max_vtsize);
906 if (vtable [io + l] && !(vtable [io + l]->flags & METHOD_ATTRIBUTE_ABSTRACT))
911 for (l = 0; l < ic->method.count; l++) {
912 MonoMethod *im = ic->methods [l];
913 char *qname, *fqname;
919 qname = g_strconcat (ic->name, ".", im->name, NULL);
920 if (ic->name_space && ic->name_space [0])
921 fqname = g_strconcat (ic->name_space, ".", ic->name, ".", im->name, NULL);
925 for (k1 = class; k1; k1 = k1->parent) {
926 for (j = 0; j < k1->method.count; ++j) {
927 MonoMethod *cm = k1->methods [j];
929 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL))
932 if (((fqname && !strcmp (cm->name, fqname)) || !strcmp (cm->name, qname)) &&
933 mono_metadata_signature_equal (cm->signature, im->signature)) {
934 g_assert (io + l <= max_vtsize);
935 vtable [io + l] = cm;
945 if (!(class->flags & TYPE_ATTRIBUTE_ABSTRACT)) {
946 for (l = 0; l < ic->method.count; l++) {
948 MonoMethod *im = ic->methods [l];
949 if (im->flags & METHOD_ATTRIBUTE_STATIC)
951 g_assert (io + l <= max_vtsize);
952 if (!(vtable [io + l])) {
953 for (j = 0; j < onum; ++j) {
954 g_print (" at slot %d: %s (%d) overrides %s (%d)\n", io+l, overrides [j*2+1]->name,
955 overrides [j*2+1]->slot, overrides [j*2]->name, overrides [j*2]->slot);
957 msig = mono_signature_get_desc (im->signature, FALSE);
958 printf ("no implementation for interface method %s.%s::%s(%s) in class %s.%s\n",
959 ic->name_space, ic->name, im->name, msig, class->name_space, class->name);
961 for (j = 0; j < class->method.count; ++j) {
962 MonoMethod *cm = class->methods [j];
963 msig = mono_signature_get_desc (cm->signature, FALSE);
965 printf ("METHOD %s(%s)\n", cm->name, msig);
968 g_assert_not_reached ();
973 for (l = 0; l < ic->method.count; l++) {
974 MonoMethod *im = vtable [io + l];
977 g_assert (io + l <= max_vtsize);
979 /* FIXME: why do we need this ? */
981 /* g_assert_not_reached (); */
987 g_ptr_array_free (ifaces, TRUE);
990 for (i = 0; i < class->method.count; ++i) {
993 cm = class->methods [i];
995 if (!(cm->flags & METHOD_ATTRIBUTE_NEW_SLOT) && (cm->flags & METHOD_ATTRIBUTE_VIRTUAL)) {
997 for (k = class->parent; k ; k = k->parent) {
999 for (j = 0; j < k->method.count; ++j) {
1000 MonoMethod *m1 = k->methods [j];
1001 if (!(m1->flags & METHOD_ATTRIBUTE_VIRTUAL))
1003 if (!strcmp(cm->name, m1->name) &&
1004 mono_metadata_signature_equal (cm->signature, m1->signature)) {
1005 slot = k->methods [j]->slot;
1006 g_assert (cm->slot < max_vtsize);
1008 override_map = mono_g_hash_table_new (NULL, NULL);
1009 mono_g_hash_table_insert (override_map, m1, cm);
1021 cm->slot = cur_slot++;
1023 if (!(cm->flags & METHOD_ATTRIBUTE_ABSTRACT) && !cm->signature->generic_param_count)
1024 vtable [cm->slot] = cm;
1027 /* override non interface methods */
1028 for (i = 0; i < onum; i++) {
1029 MonoMethod *decl = overrides [i*2];
1030 if (!(decl->klass->flags & TYPE_ATTRIBUTE_INTERFACE)) {
1031 g_assert (decl->slot != -1);
1032 vtable [decl->slot] = overrides [i*2 + 1];
1033 overrides [i * 2 + 1]->slot = decl->slot;
1035 override_map = mono_g_hash_table_new (NULL, NULL);
1036 mono_g_hash_table_insert (override_map, decl, overrides [i * 2 + 1]);
1041 * If a method occupies more than one place in the vtable, and it is
1042 * overriden, then change the other occurances too.
1045 for (i = 0; i < max_vtsize; ++i)
1047 MonoMethod *cm = mono_g_hash_table_lookup (override_map, vtable [i]);
1052 mono_g_hash_table_destroy (override_map);
1055 class->vtable_size = cur_slot;
1056 class->vtable = g_malloc0 (sizeof (gpointer) * class->vtable_size);
1057 memcpy (class->vtable, vtable, sizeof (gpointer) * class->vtable_size);
1059 if (mono_print_vtable) {
1062 for (i = 0; i <= max_iid; i++)
1063 if (class->interface_offsets [i] != -1)
1066 printf ("VTable %s.%s (size = %d, interfaces = %d)\n", class->name_space,
1067 class->name, class->vtable_size, icount);
1069 for (i = 0; i < class->vtable_size; ++i) {
1074 printf (" slot %03d(%03d) %s.%s:%s\n", i, cm->slot,
1075 cm->klass->name_space, cm->klass->name,
1082 printf ("Interfaces %s.%s (max_iid = %d)\n", class->name_space,
1083 class->name, max_iid);
1085 for (i = 0; i < class->interface_count; i++) {
1086 ic = class->interfaces [i];
1087 printf (" slot %03d(%03d) %s.%s\n",
1088 class->interface_offsets [ic->interface_id],
1089 ic->method.count, ic->name_space, ic->name);
1092 for (k = class->parent; k ; k = k->parent) {
1093 for (i = 0; i < k->interface_count; i++) {
1094 ic = k->interfaces [i];
1095 printf (" slot %03d(%03d) %s.%s\n",
1096 class->interface_offsets [ic->interface_id],
1097 ic->method.count, ic->name_space, ic->name);
1106 * @class: the class to initialize
1108 * compute the instance_size, class_size and other infos that cannot be
1109 * computed at mono_class_get() time. Also compute a generic vtable and
1110 * the method slot numbers. We use this infos later to create a domain
1114 mono_class_init (MonoClass *class)
1117 static MonoMethod *default_ghc = NULL;
1118 static MonoMethod *default_finalize = NULL;
1119 static int finalize_slot = -1;
1120 static int ghc_slot = -1;
1121 MonoMethod **overrides;
1129 /*g_print ("Init class %s\n", class->name);*/
1131 /* We do everything inside the lock to prevent races */
1132 mono_loader_lock ();
1134 if (class->inited) {
1135 mono_loader_unlock ();
1136 /* Somebody might have gotten in before us */
1140 if (class->init_pending) {
1141 /* this indicates a cyclic dependency */
1142 g_error ("pending init %s.%s\n", class->name_space, class->name);
1145 class->init_pending = 1;
1147 mono_stats.initialized_class_count++;
1149 if (class->parent && !class->parent->inited)
1150 mono_class_init (class->parent);
1153 * Computes the size used by the fields, and their locations
1155 if (!class->size_inited)
1156 class_compute_field_layout (class);
1158 /* initialize method pointers */
1161 MonoMethodSignature *sig;
1162 class->method.count = class->rank > 1? 2: 1;
1163 sig = mono_metadata_signature_alloc (class->image, class->rank);
1164 sig->ret = &mono_defaults.void_class->byval_arg;
1165 sig->pinvoke = TRUE;
1166 for (i = 0; i < class->rank; ++i)
1167 sig->params [i] = &mono_defaults.int32_class->byval_arg;
1169 ctor = (MonoMethod *) g_new0 (MonoMethodPInvoke, 1);
1170 ctor->klass = class;
1171 ctor->flags = METHOD_ATTRIBUTE_PUBLIC | METHOD_ATTRIBUTE_RT_SPECIAL_NAME | METHOD_ATTRIBUTE_SPECIAL_NAME;
1172 ctor->iflags = METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL;
1173 ctor->signature = sig;
1174 ctor->name = ".ctor";
1176 class->methods = g_new (MonoMethod*, class->method.count);
1177 class->methods [0] = ctor;
1178 if (class->rank > 1) {
1179 sig = mono_metadata_signature_alloc (class->image, class->rank * 2);
1180 sig->ret = &mono_defaults.void_class->byval_arg;
1181 sig->pinvoke = TRUE;
1182 for (i = 0; i < class->rank * 2; ++i)
1183 sig->params [i] = &mono_defaults.int32_class->byval_arg;
1185 ctor = (MonoMethod *) g_new0 (MonoMethodPInvoke, 1);
1186 ctor->klass = class;
1187 ctor->flags = METHOD_ATTRIBUTE_PUBLIC | METHOD_ATTRIBUTE_RT_SPECIAL_NAME | METHOD_ATTRIBUTE_SPECIAL_NAME;
1188 ctor->iflags = METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL;
1189 ctor->signature = sig;
1190 ctor->name = ".ctor";
1192 class->methods [1] = ctor;
1195 if (!class->generic_inst && !class->methods) {
1196 class->methods = g_new (MonoMethod*, class->method.count);
1197 for (i = 0; i < class->method.count; ++i) {
1198 class->methods [i] = mono_get_method (class->image,
1199 MONO_TOKEN_METHOD_DEF | (i + class->method.first + 1), class);
1204 init_properties (class);
1205 init_events (class);
1207 i = mono_metadata_nesting_typedef (class->image, class->type_token, 1);
1210 guint32 cols [MONO_NESTED_CLASS_SIZE];
1211 mono_metadata_decode_row (&class->image->tables [MONO_TABLE_NESTEDCLASS], i - 1, cols, MONO_NESTED_CLASS_SIZE);
1212 nclass = mono_class_create_from_typedef (class->image, MONO_TOKEN_TYPE_DEF | cols [MONO_NESTED_CLASS_NESTED]);
1213 class->nested_classes = g_list_prepend (class->nested_classes, nclass);
1215 i = mono_metadata_nesting_typedef (class->image, class->type_token, i + 1);
1218 mono_class_setup_supertypes (class);
1220 if (class->flags & TYPE_ATTRIBUTE_INTERFACE) {
1221 for (i = 0; i < class->method.count; ++i)
1222 class->methods [i]->slot = i;
1223 class->init_pending = 0;
1226 * class->interface_offsets is needed for the castclass/isinst code, so
1227 * we have to setup them for interfaces, too.
1229 setup_interface_offsets (class, 0);
1230 mono_loader_unlock ();
1234 overrides = mono_class_get_overrides (class->image, class->type_token, &onum);
1235 mono_class_setup_vtable (class, overrides, onum);
1239 class->init_pending = 0;
1242 if (class == mono_defaults.object_class) {
1244 for (i = 0; i < class->vtable_size; ++i) {
1245 MonoMethod *cm = class->vtable [i];
1247 if (!strcmp (cm->name, "GetHashCode")) {
1253 g_assert (ghc_slot > 0);
1255 default_ghc = class->vtable [ghc_slot];
1260 if (class->parent) {
1262 if (class->vtable [ghc_slot] == default_ghc) {
1267 if (!default_finalize) {
1268 if (class == mono_defaults.object_class) {
1270 for (i = 0; i < class->vtable_size; ++i) {
1271 MonoMethod *cm = class->vtable [i];
1273 if (!strcmp (cm->name, "Finalize")) {
1279 g_assert (finalize_slot > 0);
1281 default_finalize = class->vtable [finalize_slot];
1285 /* Object::Finalize should have empty implemenatation */
1286 class->has_finalize = 0;
1287 if (class->parent) {
1288 if (class->vtable [finalize_slot] != default_finalize)
1289 class->has_finalize = 1;
1292 mono_loader_unlock ();
1294 if (mono_debugger_class_init_func)
1295 mono_debugger_class_init_func (class);
1300 mono_class_setup_mono_type (MonoClass *class)
1302 const char *name = class->name;
1303 const char *nspace = class->name_space;
1305 if (class->flags & TYPE_ATTRIBUTE_INTERFACE)
1306 class->interface_id = mono_get_unique_iid (class);
1308 class->this_arg.byref = 1;
1309 class->this_arg.data.klass = class;
1310 class->this_arg.type = MONO_TYPE_CLASS;
1311 class->byval_arg.data.klass = class;
1312 class->byval_arg.type = MONO_TYPE_CLASS;
1314 if (!strcmp (nspace, "System")) {
1315 if (!strcmp (name, "ValueType")) {
1317 * do not set the valuetype bit for System.ValueType.
1318 * class->valuetype = 1;
1320 } else if (!strcmp (name, "Enum")) {
1322 * do not set the valuetype bit for System.Enum.
1323 * class->valuetype = 1;
1325 class->valuetype = 0;
1326 class->enumtype = 0;
1327 } else if (!strcmp (name, "Object")) {
1328 class->this_arg.type = class->byval_arg.type = MONO_TYPE_OBJECT;
1329 } else if (!strcmp (name, "String")) {
1330 class->this_arg.type = class->byval_arg.type = MONO_TYPE_STRING;
1331 } else if (!strcmp (name, "TypedReference")) {
1332 class->this_arg.type = class->byval_arg.type = MONO_TYPE_TYPEDBYREF;
1336 if (class->valuetype) {
1337 int t = MONO_TYPE_VALUETYPE;
1338 if (!strcmp (nspace, "System")) {
1341 if (!strcmp (name, "Boolean")) {
1342 t = MONO_TYPE_BOOLEAN;
1343 } else if (!strcmp(name, "Byte")) {
1345 class->blittable = TRUE;
1349 if (!strcmp (name, "Char")) {
1354 if (!strcmp (name, "Double")) {
1356 class->blittable = TRUE;
1360 if (!strcmp (name, "Int32")) {
1362 class->blittable = TRUE;
1363 } else if (!strcmp(name, "Int16")) {
1365 class->blittable = TRUE;
1366 } else if (!strcmp(name, "Int64")) {
1368 class->blittable = TRUE;
1369 } else if (!strcmp(name, "IntPtr")) {
1371 class->blittable = TRUE;
1375 if (!strcmp (name, "Single")) {
1377 class->blittable = TRUE;
1378 } else if (!strcmp(name, "SByte")) {
1380 class->blittable = TRUE;
1384 if (!strcmp (name, "UInt32")) {
1386 class->blittable = TRUE;
1387 } else if (!strcmp(name, "UInt16")) {
1389 class->blittable = TRUE;
1390 } else if (!strcmp(name, "UInt64")) {
1392 class->blittable = TRUE;
1393 } else if (!strcmp(name, "UIntPtr")) {
1395 class->blittable = TRUE;
1399 if (!strcmp (name, "TypedReference")) {
1400 t = MONO_TYPE_TYPEDBYREF;
1401 class->blittable = TRUE;
1405 if (!strcmp (name, "Void")) {
1413 class->this_arg.type = class->byval_arg.type = t;
1418 mono_class_setup_parent (MonoClass *class, MonoClass *parent)
1420 gboolean system_namespace;
1422 system_namespace = !strcmp (class->name_space, "System");
1424 /* if root of the hierarchy */
1425 if (system_namespace && !strcmp (class->name, "Object")) {
1426 class->parent = NULL;
1427 class->instance_size = sizeof (MonoObject);
1430 if (!strcmp (class->name, "<Module>")) {
1431 class->parent = NULL;
1432 class->instance_size = 0;
1436 if (!(class->flags & TYPE_ATTRIBUTE_INTERFACE)) {
1437 class->parent = parent;
1440 g_assert_not_reached (); /* FIXME */
1442 if (parent->generic_inst && !parent->name) {
1444 * If the parent is a generic instance, we may get
1445 * called before it is fully initialized, especially
1446 * before it has its name.
1451 class->marshalbyref = parent->marshalbyref;
1452 class->contextbound = parent->contextbound;
1453 class->delegate = parent->delegate;
1455 if (system_namespace) {
1456 if (*class->name == 'M' && !strcmp (class->name, "MarshalByRefObject"))
1457 class->marshalbyref = 1;
1459 if (*class->name == 'C' && !strcmp (class->name, "ContextBoundObject"))
1460 class->contextbound = 1;
1462 if (*class->name == 'D' && !strcmp (class->name, "Delegate"))
1463 class->delegate = 1;
1466 if (class->parent->enumtype || ((strcmp (class->parent->name, "ValueType") == 0) &&
1467 (strcmp (class->parent->name_space, "System") == 0)))
1468 class->valuetype = 1;
1469 if (((strcmp (class->parent->name, "Enum") == 0) && (strcmp (class->parent->name_space, "System") == 0))) {
1470 class->valuetype = class->enumtype = 1;
1472 /*class->enumtype = class->parent->enumtype; */
1473 mono_class_setup_supertypes (class);
1475 class->parent = NULL;
1481 mono_class_setup_supertypes (MonoClass *class)
1486 if (class->supertypes)
1490 for (k = class; k ; k = k->parent) {
1494 ms = MAX (MONO_DEFAULT_SUPERTABLE_SIZE, class->idepth);
1495 class->supertypes = g_new0 (MonoClass *, ms);
1497 if (class->parent) {
1498 for (i = class->idepth, k = class; k ; k = k->parent)
1499 class->supertypes [--i] = k;
1501 class->supertypes [0] = class;
1506 * @image: context where the image is created
1507 * @type_token: typedef token
1510 mono_class_create_from_typedef (MonoImage *image, guint32 type_token)
1512 MonoTableInfo *tt = &image->tables [MONO_TABLE_TYPEDEF];
1513 MonoClass *class, *parent = NULL;
1514 guint32 cols [MONO_TYPEDEF_SIZE];
1515 guint32 cols_next [MONO_TYPEDEF_SIZE];
1516 guint tidx = mono_metadata_token_index (type_token);
1517 const char *name, *nspace;
1519 MonoClass **interfaces;
1521 mono_loader_lock ();
1523 if ((class = g_hash_table_lookup (image->class_cache, GUINT_TO_POINTER (type_token)))) {
1524 mono_loader_unlock ();
1528 g_assert (mono_metadata_token_table (type_token) == MONO_TABLE_TYPEDEF);
1530 mono_metadata_decode_row (tt, tidx - 1, cols, MONO_TYPEDEF_SIZE);
1532 name = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
1533 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
1535 if (cols [MONO_TYPEDEF_EXTENDS])
1536 parent = mono_class_get (image, mono_metadata_token_from_dor (cols [MONO_TYPEDEF_EXTENDS]));
1537 interfaces = mono_metadata_interfaces_from_typedef (image, type_token, &icount);
1539 class = g_malloc0 (sizeof (MonoClass));
1541 g_hash_table_insert (image->class_cache, GUINT_TO_POINTER (type_token), class);
1543 class->interfaces = interfaces;
1544 class->interface_count = icount;
1547 class->name_space = nspace;
1549 class->image = image;
1550 class->type_token = type_token;
1551 class->flags = cols [MONO_TYPEDEF_FLAGS];
1553 if ((class->flags & TYPE_ATTRIBUTE_STRING_FORMAT_MASK) == TYPE_ATTRIBUTE_UNICODE_CLASS)
1555 /* fixme: maybe we must set this on windows
1556 if ((class->flags & TYPE_ATTRIBUTE_STRING_FORMAT_MASK) == TYPE_ATTRIBUTE_AUTO_CLASS)
1560 class->cast_class = class->element_class = class;
1562 /*g_print ("Load class %s\n", name);*/
1564 mono_class_setup_parent (class, parent);
1566 mono_class_setup_mono_type (class);
1569 * Compute the field and method lists
1571 class->field.first = cols [MONO_TYPEDEF_FIELD_LIST] - 1;
1572 class->method.first = cols [MONO_TYPEDEF_METHOD_LIST] - 1;
1574 if (tt->rows > tidx){
1575 mono_metadata_decode_row (tt, tidx, cols_next, CSIZE (cols_next));
1576 class->field.last = cols_next [MONO_TYPEDEF_FIELD_LIST] - 1;
1577 class->method.last = cols_next [MONO_TYPEDEF_METHOD_LIST] - 1;
1579 class->field.last = image->tables [MONO_TABLE_FIELD].rows;
1580 class->method.last = image->tables [MONO_TABLE_METHOD].rows;
1583 if (cols [MONO_TYPEDEF_FIELD_LIST] &&
1584 cols [MONO_TYPEDEF_FIELD_LIST] <= image->tables [MONO_TABLE_FIELD].rows)
1585 class->field.count = class->field.last - class->field.first;
1587 class->field.count = 0;
1589 if (cols [MONO_TYPEDEF_METHOD_LIST] <= image->tables [MONO_TABLE_METHOD].rows)
1590 class->method.count = class->method.last - class->method.first;
1592 class->method.count = 0;
1594 /* reserve space to store vector pointer in arrays */
1595 if (!strcmp (nspace, "System") && !strcmp (name, "Array")) {
1596 class->instance_size += 2 * sizeof (gpointer);
1597 g_assert (class->field.count == 0);
1600 if (class->enumtype)
1601 class_compute_field_layout (class);
1603 if ((type_token = mono_metadata_nested_in_typedef (image, type_token)))
1604 class->nested_in = mono_class_create_from_typedef (image, type_token);
1606 class->gen_params = mono_metadata_load_generic_params (image, class->type_token, &icount);
1607 class->num_gen_params = icount;
1609 mono_loader_unlock ();
1615 get_instantiation_name (const char *name, MonoGenericInst *ginst)
1617 GString *res = g_string_new (name);
1620 MonoClass *argclass;
1622 p = strchr (name, '<');
1624 g_string_truncate (res, (p - name) + 1);
1626 g_string_append_c (res, '<');
1628 for (i = 0; i < ginst->type_argc; ++i) {
1630 g_string_append_c (res, ',');
1631 argclass = mono_class_from_mono_type (ginst->type_argv [i]);
1632 g_string_append (res, argclass->name);
1634 g_string_append_c (res, '>');
1635 return g_string_free (res, FALSE);
1639 mono_class_initialize_generic (MonoGenericInst *ginst, gboolean inflate_methods)
1641 MonoClass *klass, *gklass;
1643 if (ginst->initialized || ginst->init_pending)
1646 gklass = mono_class_from_mono_type (ginst->generic_type);
1647 mono_class_init (gklass);
1649 klass = ginst->klass;
1650 klass->name = get_instantiation_name (gklass->name, ginst);
1652 mono_class_setup_parent (klass, gklass->parent);
1653 mono_class_setup_mono_type (klass);
1655 if (inflate_methods) {
1658 klass->field = gklass->field;
1659 klass->method = gklass->method;
1660 klass->methods = g_new0 (MonoMethod *, klass->method.count);
1661 for (i = 0; i < klass->method.count; i++)
1662 klass->methods [i] = mono_class_inflate_generic_method (gklass->methods [i], ginst);
1665 g_hash_table_insert (klass->image->generics_cache, ginst->generic_type, klass);
1666 ginst->initialized = TRUE;
1670 mono_class_from_generic (MonoType *gtype, gboolean inflate_methods)
1672 MonoGenericInst *ginst = gtype->data.generic_inst;
1673 MonoClass *class, *gklass;
1677 mono_class_initialize_generic (ginst, TRUE);
1678 return ginst->klass;
1681 mono_loader_lock ();
1683 gklass = mono_class_from_mono_type (ginst->generic_type);
1684 mono_class_init (gklass);
1686 image = gklass->image;
1687 if ((class = g_hash_table_lookup (image->generics_cache, gtype))) {
1688 mono_loader_unlock ();
1692 class = g_malloc0 (sizeof (MonoClass));
1693 class->name_space = gklass->name_space;
1694 class->name = get_instantiation_name (gklass->name, ginst);
1695 class->image = image;
1696 class->flags = gklass->flags;
1698 class->generic_inst = gtype;
1700 class->cast_class = class->element_class = class;
1702 if (inflate_methods) {
1705 mono_class_setup_parent (class, gklass->parent);
1706 mono_class_setup_mono_type (class);
1708 class->field = gklass->field;
1709 class->method = gklass->method;
1710 class->methods = g_new0 (MonoMethod *, class->method.count);
1711 for (i = 0; i < class->method.count; i++)
1712 class->methods [i] = mono_class_inflate_generic_method (gklass->methods [i], ginst);
1715 g_hash_table_insert (image->generics_cache, gtype, class);
1717 mono_loader_unlock ();
1723 mono_class_from_generic_parameter (MonoGenericParam *param, MonoImage *image, gboolean is_mvar)
1725 MonoClass *klass, **ptr;
1729 return param->pklass;
1731 klass = param->pklass = g_new0 (MonoClass, 1);
1733 for (count = 0, ptr = param->constraints; ptr && *ptr; ptr++, count++)
1737 if ((count > 0) && !(param->constraints [0]->flags & TYPE_ATTRIBUTE_INTERFACE)) {
1738 klass->parent = param->constraints [0];
1741 klass->parent = mono_defaults.object_class;
1743 if (count - pos > 0) {
1746 klass->interface_count = count - pos;
1747 klass->interfaces = g_new0 (MonoClass *, count - pos);
1748 for (i = pos; i < count; i++) {
1749 klass->interfaces [i - pos] = param->constraints [i];
1750 klass->method.count += param->constraints [i]->method.count;
1753 klass->methods = g_new0 (MonoMethod *, klass->method.count);
1754 for (i = pos; i < count; i++) {
1755 MonoClass *iface = klass->interfaces [i - pos];
1756 for (j = 0; j < iface->method.count; j++)
1757 klass->methods [klass->method.last++] = iface->methods [j];
1761 klass->name = g_strdup_printf (is_mvar ? "!!%d" : "!%d", param->num);
1762 klass->name_space = "";
1763 klass->image = image;
1764 klass->cast_class = klass->element_class = klass;
1765 klass->enum_basetype = &klass->element_class->byval_arg;
1766 klass->flags = TYPE_ATTRIBUTE_INTERFACE;
1768 klass->this_arg.type = klass->byval_arg.type = is_mvar ? MONO_TYPE_MVAR : MONO_TYPE_VAR;
1769 klass->this_arg.data.generic_param = klass->byval_arg.data.generic_param = param;
1770 klass->this_arg.byref = TRUE;
1772 mono_class_init (klass);
1778 my_mono_class_from_generic_parameter (MonoGenericParam *param, gboolean is_mvar)
1783 return param->pklass;
1785 klass = g_new0 (MonoClass, 1);
1787 klass->name = g_strdup_printf (is_mvar ? "!!%d" : "!%d", param->num);
1788 klass->name_space = "";
1789 klass->image = mono_defaults.corlib;
1790 klass->cast_class = klass->element_class = klass;
1791 klass->enum_basetype = &klass->element_class->byval_arg;
1792 klass->flags = TYPE_ATTRIBUTE_INTERFACE;
1794 klass->this_arg.type = klass->byval_arg.type = is_mvar ? MONO_TYPE_MVAR : MONO_TYPE_VAR;
1795 klass->this_arg.data.generic_param = klass->byval_arg.data.generic_param = param;
1796 klass->this_arg.byref = TRUE;
1798 mono_class_init (klass);
1804 mono_ptr_class_get (MonoType *type)
1807 MonoClass *el_class;
1808 static GHashTable *ptr_hash = NULL;
1810 mono_loader_lock ();
1813 ptr_hash = g_hash_table_new (NULL, NULL);
1814 el_class = mono_class_from_mono_type (type);
1815 if ((result = g_hash_table_lookup (ptr_hash, el_class))) {
1816 mono_loader_unlock ();
1819 result = g_new0 (MonoClass, 1);
1821 result->parent = NULL; /* no parent for PTR types */
1822 result->name = "System";
1823 result->name_space = "MonoPtrFakeClass";
1824 result->image = el_class->image;
1825 result->inited = TRUE;
1826 result->flags = TYPE_ATTRIBUTE_CLASS | (el_class->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK);
1827 /* Can pointers get boxed? */
1828 result->instance_size = sizeof (gpointer);
1829 result->cast_class = result->element_class = el_class;
1830 result->enum_basetype = &result->element_class->byval_arg;
1832 result->this_arg.type = result->byval_arg.type = MONO_TYPE_PTR;
1833 result->this_arg.data.type = result->byval_arg.data.type = result->enum_basetype;
1834 result->this_arg.byref = TRUE;
1836 mono_class_setup_supertypes (result);
1838 g_hash_table_insert (ptr_hash, el_class, result);
1840 mono_loader_unlock ();
1846 mono_fnptr_class_get (MonoMethodSignature *sig)
1849 static GHashTable *ptr_hash = NULL;
1851 mono_loader_lock ();
1854 ptr_hash = g_hash_table_new (NULL, NULL);
1856 if ((result = g_hash_table_lookup (ptr_hash, sig))) {
1857 mono_loader_unlock ();
1860 result = g_new0 (MonoClass, 1);
1862 result->parent = NULL; /* no parent for PTR types */
1863 result->name = "System";
1864 result->name_space = "MonoFNPtrFakeClass";
1865 result->image = NULL; /* need to fix... */
1866 result->inited = TRUE;
1867 result->flags = TYPE_ATTRIBUTE_CLASS; // | (el_class->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK);
1868 /* Can pointers get boxed? */
1869 result->instance_size = sizeof (gpointer);
1870 result->cast_class = result->element_class = result;
1872 result->this_arg.type = result->byval_arg.type = MONO_TYPE_FNPTR;
1873 result->this_arg.data.method = result->byval_arg.data.method = sig;
1874 result->this_arg.byref = TRUE;
1875 result->enum_basetype = &result->element_class->byval_arg;
1877 mono_class_setup_supertypes (result);
1879 g_hash_table_insert (ptr_hash, sig, result);
1881 mono_loader_unlock ();
1887 mono_class_from_mono_type (MonoType *type)
1889 switch (type->type) {
1890 case MONO_TYPE_OBJECT:
1891 return type->data.klass? type->data.klass: mono_defaults.object_class;
1892 case MONO_TYPE_VOID:
1893 return type->data.klass? type->data.klass: mono_defaults.void_class;
1894 case MONO_TYPE_BOOLEAN:
1895 return type->data.klass? type->data.klass: mono_defaults.boolean_class;
1896 case MONO_TYPE_CHAR:
1897 return type->data.klass? type->data.klass: mono_defaults.char_class;
1899 return type->data.klass? type->data.klass: mono_defaults.sbyte_class;
1901 return type->data.klass? type->data.klass: mono_defaults.byte_class;
1903 return type->data.klass? type->data.klass: mono_defaults.int16_class;
1905 return type->data.klass? type->data.klass: mono_defaults.uint16_class;
1907 return type->data.klass? type->data.klass: mono_defaults.int32_class;
1909 return type->data.klass? type->data.klass: mono_defaults.uint32_class;
1911 return type->data.klass? type->data.klass: mono_defaults.int_class;
1913 return type->data.klass? type->data.klass: mono_defaults.uint_class;
1915 return type->data.klass? type->data.klass: mono_defaults.int64_class;
1917 return type->data.klass? type->data.klass: mono_defaults.uint64_class;
1919 return type->data.klass? type->data.klass: mono_defaults.single_class;
1921 return type->data.klass? type->data.klass: mono_defaults.double_class;
1922 case MONO_TYPE_STRING:
1923 return type->data.klass? type->data.klass: mono_defaults.string_class;
1924 case MONO_TYPE_TYPEDBYREF:
1925 return type->data.klass? type->data.klass: mono_defaults.typed_reference_class;
1926 case MONO_TYPE_ARRAY:
1927 return mono_bounded_array_class_get (type->data.array->eklass, type->data.array->rank, TRUE);
1929 return mono_ptr_class_get (type->data.type);
1930 case MONO_TYPE_FNPTR:
1931 return mono_fnptr_class_get (type->data.method);
1932 case MONO_TYPE_SZARRAY:
1933 return mono_array_class_get (type->data.klass, 1);
1934 case MONO_TYPE_CLASS:
1935 case MONO_TYPE_VALUETYPE:
1936 return type->data.klass;
1937 case MONO_TYPE_GENERICINST:
1938 return mono_class_from_generic (type, TRUE);
1940 return my_mono_class_from_generic_parameter (type->data.generic_param, FALSE);
1941 case MONO_TYPE_MVAR:
1942 return my_mono_class_from_generic_parameter (type->data.generic_param, TRUE);
1944 g_warning ("implement me 0x%02x\n", type->type);
1945 g_assert_not_reached ();
1952 * @image: context where the image is created
1953 * @type_spec: typespec token
1956 mono_class_create_from_typespec (MonoImage *image, guint32 type_spec)
1961 type = mono_type_create_from_typespec (image, type_spec);
1963 switch (type->type) {
1964 case MONO_TYPE_ARRAY:
1965 class = mono_array_class_get (type->data.array->eklass, type->data.array->rank);
1967 case MONO_TYPE_SZARRAY:
1968 class = mono_array_class_get (type->data.klass, 1);
1971 class = mono_class_from_mono_type (type->data.type);
1973 case MONO_TYPE_GENERICINST:
1974 class = mono_class_from_generic (type, TRUE);
1977 /* it seems any type can be stored in TypeSpec as well */
1978 class = mono_class_from_mono_type (type);
1986 * mono_bounded_array_class_get:
1987 * @element_class: element class
1988 * @rank: the dimension of the array class
1989 * @bounded: whenever the array has non-zero bounds
1991 * Returns: a class object describing the array with element type @element_type and
1995 mono_bounded_array_class_get (MonoClass *eclass, guint32 rank, gboolean bounded)
1999 MonoClass *parent = NULL;
2000 GSList *list, *rootlist;
2003 gboolean corlib_type = FALSE;
2005 g_assert (rank <= 255);
2008 /* bounded only matters for one-dimensional arrays */
2011 image = eclass->image;
2013 mono_loader_lock ();
2015 if ((rootlist = list = g_hash_table_lookup (image->array_cache, eclass))) {
2016 for (; list; list = list->next) {
2018 if ((class->rank == rank) && (class->byval_arg.type == (bounded ? MONO_TYPE_ARRAY : MONO_TYPE_SZARRAY))) {
2019 mono_loader_unlock ();
2025 /* for the building corlib use System.Array from it */
2026 if (image->assembly && image->assembly->dynamic && strcmp (image->assembly_name, "mscorlib") == 0) {
2027 parent = mono_class_from_name (image, "System", "Array");
2030 parent = mono_defaults.array_class;
2031 if (!parent->inited)
2032 mono_class_init (parent);
2035 class = g_malloc0 (sizeof (MonoClass));
2037 class->image = image;
2038 class->name_space = eclass->name_space;
2039 nsize = strlen (eclass->name);
2040 name = g_malloc (nsize + 2 + rank);
2041 memcpy (name, eclass->name, nsize);
2044 memset (name + nsize + 1, ',', rank - 1);
2045 name [nsize + rank] = ']';
2046 name [nsize + rank + 1] = 0;
2048 class->type_token = 0;
2049 /* all arrays are marked serializable and sealed, bug #42779 */
2050 class->flags = TYPE_ATTRIBUTE_CLASS | TYPE_ATTRIBUTE_SERIALIZABLE | TYPE_ATTRIBUTE_SEALED |
2051 (eclass->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK);
2052 class->parent = parent;
2053 class->instance_size = mono_class_instance_size (class->parent);
2054 class->class_size = 0;
2055 mono_class_setup_supertypes (class);
2059 if (eclass->enumtype)
2060 class->cast_class = eclass->element_class;
2062 class->cast_class = eclass;
2064 class->element_class = eclass;
2066 if ((rank > 1) || bounded) {
2067 MonoArrayType *at = g_new0 (MonoArrayType, 1);
2068 class->byval_arg.type = MONO_TYPE_ARRAY;
2069 class->byval_arg.data.array = at;
2070 at->eklass = eclass;
2072 /* FIXME: complete.... */
2074 class->byval_arg.type = MONO_TYPE_SZARRAY;
2075 class->byval_arg.data.klass = eclass;
2077 class->this_arg = class->byval_arg;
2078 class->this_arg.byref = 1;
2083 list = g_slist_append (rootlist, class);
2084 g_hash_table_insert (image->array_cache, eclass, list);
2086 mono_loader_unlock ();
2092 * mono_array_class_get:
2093 * @element_class: element class
2094 * @rank: the dimension of the array class
2096 * Returns: a class object describing the array with element type @element_type and
2100 mono_array_class_get (MonoClass *eclass, guint32 rank)
2102 return mono_bounded_array_class_get (eclass, rank, FALSE);
2106 * mono_class_instance_size:
2109 * Returns: the size of an object instance
2112 mono_class_instance_size (MonoClass *klass)
2114 if (!klass->size_inited)
2115 mono_class_init (klass);
2117 return klass->instance_size;
2121 * mono_class_min_align:
2124 * Returns: minimm alignment requirements
2127 mono_class_min_align (MonoClass *klass)
2129 if (!klass->size_inited)
2130 mono_class_init (klass);
2132 return klass->min_align;
2136 * mono_class_value_size:
2139 * This function is used for value types, and return the
2140 * space and the alignment to store that kind of value object.
2142 * Returns: the size of a value of kind @klass
2145 mono_class_value_size (MonoClass *klass, guint32 *align)
2149 /* fixme: check disable, because we still have external revereces to
2150 * mscorlib and Dummy Objects
2152 /*g_assert (klass->valuetype);*/
2154 size = mono_class_instance_size (klass) - sizeof (MonoObject);
2157 *align = klass->min_align;
2163 * mono_class_data_size:
2166 * Returns: the size of the static class data
2169 mono_class_data_size (MonoClass *klass)
2172 mono_class_init (klass);
2174 return klass->class_size;
2178 * Auxiliary routine to mono_class_get_field
2180 * Takes a field index instead of a field token.
2182 static MonoClassField *
2183 mono_class_get_field_idx (MonoClass *class, int idx)
2185 if (class->field.count){
2186 if ((idx >= class->field.first) && (idx < class->field.last)){
2187 return &class->fields [idx - class->field.first];
2194 return mono_class_get_field_idx (class->parent, idx);
2198 * mono_class_get_field:
2199 * @class: the class to lookup the field.
2200 * @field_token: the field token
2202 * Returns: A MonoClassField representing the type and offset of
2203 * the field, or a NULL value if the field does not belong to this
2207 mono_class_get_field (MonoClass *class, guint32 field_token)
2209 int idx = mono_metadata_token_index (field_token);
2211 g_assert (mono_metadata_token_code (field_token) == MONO_TOKEN_FIELD_DEF);
2213 return mono_class_get_field_idx (class, idx - 1);
2217 mono_class_get_field_from_name (MonoClass *klass, const char *name)
2222 for (i = 0; i < klass->field.count; ++i) {
2223 if (strcmp (name, klass->fields [i].name) == 0)
2224 return &klass->fields [i];
2226 klass = klass->parent;
2232 mono_class_get_property_from_name (MonoClass *klass, const char *name)
2237 for (i = 0; i < klass->property.count; ++i) {
2238 if (strcmp (name, klass->properties [i].name) == 0)
2239 return &klass->properties [i];
2241 klass = klass->parent;
2248 * @image: the image where the class resides
2249 * @type_token: the token for the class
2250 * @at: an optional pointer to return the array element type
2252 * Returns: the MonoClass that represents @type_token in @image
2255 mono_class_get (MonoImage *image, guint32 type_token)
2257 MonoClass *class = NULL;
2260 return mono_lookup_dynamic_token (image, type_token);
2262 switch (type_token & 0xff000000){
2263 case MONO_TOKEN_TYPE_DEF:
2264 class = mono_class_create_from_typedef (image, type_token);
2266 case MONO_TOKEN_TYPE_REF:
2267 class = mono_class_from_typeref (image, type_token);
2269 case MONO_TOKEN_TYPE_SPEC:
2270 class = mono_class_create_from_typespec (image, type_token);
2273 g_warning ("unknown token type %x", type_token & 0xff000000);
2274 g_assert_not_reached ();
2278 g_warning ("Could not load class from token 0x%08x in %s", type_token, image->name);
2284 mono_class_from_name_case (MonoImage *image, const char* name_space, const char *name)
2286 MonoTableInfo *t = &image->tables [MONO_TABLE_TYPEDEF];
2287 guint32 cols [MONO_TYPEDEF_SIZE];
2292 /* add a cache if needed */
2293 for (i = 1; i <= t->rows; ++i) {
2294 mono_metadata_decode_row (t, i - 1, cols, MONO_TYPEDEF_SIZE);
2295 /* nested types are accessed from the nesting name */
2296 visib = cols [MONO_TYPEDEF_FLAGS] & TYPE_ATTRIBUTE_VISIBILITY_MASK;
2297 if (visib > TYPE_ATTRIBUTE_PUBLIC && visib <= TYPE_ATTRIBUTE_NESTED_ASSEMBLY)
2299 n = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
2300 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
2301 if (g_strcasecmp (n, name) == 0 && g_strcasecmp (nspace, name_space) == 0)
2302 return mono_class_get (image, MONO_TOKEN_TYPE_DEF | i);
2308 return_nested_in (MonoClass *class, char *nested) {
2310 char *s = strchr (nested, '/');
2317 for (tmp = class->nested_classes; tmp; tmp = tmp->next) {
2319 if (strcmp (found->name, nested) == 0) {
2321 return return_nested_in (found, s);
2329 mono_class_from_name (MonoImage *image, const char* name_space, const char *name)
2331 GHashTable *nspace_table;
2332 MonoImage *loaded_image;
2338 if ((nested = strchr (name, '/'))) {
2339 int pos = nested - name;
2340 int len = strlen (name);
2343 memcpy (buf, name, len + 1);
2345 nested = buf + pos + 1;
2349 mono_loader_lock ();
2351 nspace_table = g_hash_table_lookup (image->name_cache, name_space);
2354 token = GPOINTER_TO_UINT (g_hash_table_lookup (nspace_table, name));
2356 mono_loader_unlock ();
2361 if (mono_metadata_token_table (token) == MONO_TABLE_EXPORTEDTYPE) {
2362 MonoTableInfo *t = &image->tables [MONO_TABLE_EXPORTEDTYPE];
2363 guint32 cols [MONO_EXP_TYPE_SIZE];
2366 idx = mono_metadata_token_index (token);
2368 mono_metadata_decode_row (t, idx - 1, cols, MONO_EXP_TYPE_SIZE);
2370 impl = cols [MONO_EXP_TYPE_IMPLEMENTATION];
2371 if ((impl & IMPLEMENTATION_MASK) == IMPLEMENTATION_FILE) {
2372 loaded_image = mono_assembly_load_module (image->assembly, impl >> IMPLEMENTATION_BITS);
2375 class = mono_class_from_name (loaded_image, name_space, name);
2377 return return_nested_in (class, nested);
2380 g_error ("not yet implemented");
2384 token = MONO_TOKEN_TYPE_DEF | token;
2386 class = mono_class_get (image, token);
2388 return return_nested_in (class, nested);
2393 mono_class_is_subclass_of (MonoClass *klass, MonoClass *klassc,
2394 gboolean check_interfaces)
2396 if (check_interfaces && (klassc->flags & TYPE_ATTRIBUTE_INTERFACE) && !(klass->flags & TYPE_ATTRIBUTE_INTERFACE)) {
2397 if ((klassc->interface_id <= klass->max_interface_id) &&
2398 (klass->interface_offsets [klassc->interface_id] >= 0))
2400 } else if (check_interfaces && (klassc->flags & TYPE_ATTRIBUTE_INTERFACE) && (klass->flags & TYPE_ATTRIBUTE_INTERFACE)) {
2403 for (i = 0; i < klass->interface_count; i ++) {
2404 MonoClass *ic = klass->interfaces [i];
2409 if (!(klass->flags & TYPE_ATTRIBUTE_INTERFACE) && mono_class_has_parent (klass, klassc))
2414 * MS.NET thinks interfaces are a subclass of Object, so we think it as
2417 if (klassc == mono_defaults.object_class)
2424 mono_class_is_assignable_from (MonoClass *klass, MonoClass *oklass)
2427 mono_class_init (klass);
2429 if (!oklass->inited)
2430 mono_class_init (oklass);
2432 if (klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
2433 if ((klass->interface_id <= oklass->max_interface_id) &&
2434 (oklass->interface_offsets [klass->interface_id] != -1))
2438 MonoClass *eclass, *eoclass;
2440 if (oklass->rank != klass->rank)
2443 /* vectors vs. one dimensional arrays */
2444 if (oklass->byval_arg.type != klass->byval_arg.type)
2447 eclass = klass->cast_class;
2448 eoclass = oklass->cast_class;
2452 * a is b does not imply a[] is b[] when a is a valuetype, and
2453 * b is a reference type.
2456 if (eoclass->valuetype) {
2457 if ((eclass == mono_defaults.enum_class) ||
2458 (eclass == mono_defaults.enum_class->parent) ||
2459 (eclass == mono_defaults.object_class))
2463 return mono_class_is_assignable_from (klass->cast_class, oklass->cast_class);
2466 if (klass == mono_defaults.object_class)
2469 return mono_class_has_parent (oklass, klass);
2473 * mono_class_needs_cctor_run:
2475 * Determines whenever the class has a static constructor and whenever it
2476 * needs to be called when executing CALLER.
2479 mono_class_needs_cctor_run (MonoClass *klass, MonoMethod *caller)
2484 for (i = 0; i < klass->method.count; ++i) {
2485 method = klass->methods [i];
2486 if ((method->flags & METHOD_ATTRIBUTE_SPECIAL_NAME) &&
2487 (strcmp (".cctor", method->name) == 0)) {
2488 if (caller == method)
2497 * Returns the nnumber of bytes an element of type klass
2498 * uses when stored into an array.
2501 mono_class_array_element_size (MonoClass *klass)
2503 int t = klass->byval_arg.type;
2509 case MONO_TYPE_BOOLEAN:
2513 case MONO_TYPE_CHAR:
2522 case MONO_TYPE_CLASS:
2523 case MONO_TYPE_STRING:
2524 case MONO_TYPE_OBJECT:
2525 case MONO_TYPE_SZARRAY:
2526 case MONO_TYPE_ARRAY:
2528 case MONO_TYPE_MVAR:
2529 return sizeof (gpointer);
2534 case MONO_TYPE_VALUETYPE:
2535 if (klass->enumtype) {
2536 t = klass->enum_basetype->type;
2539 return mono_class_instance_size (klass) - sizeof (MonoObject);
2541 g_error ("unknown type 0x%02x in mono_class_array_element_size", t);
2547 * mono_array_element_size:
2548 * @ac: pointer to a #MonoArrayClass
2550 * Returns: the size of single array element.
2553 mono_array_element_size (MonoClass *ac)
2555 return mono_class_array_element_size (ac->element_class);
2559 mono_ldtoken (MonoImage *image, guint32 token, MonoClass **handle_class)
2561 if (image->dynamic) {
2562 gpointer obj = mono_lookup_dynamic_token (image, token);
2564 switch (token & 0xff000000) {
2565 case MONO_TOKEN_TYPE_DEF:
2566 case MONO_TOKEN_TYPE_REF:
2567 case MONO_TOKEN_TYPE_SPEC:
2569 *handle_class = mono_defaults.typehandle_class;
2570 return &((MonoClass*)obj)->byval_arg;
2571 case MONO_TOKEN_METHOD_DEF:
2573 *handle_class = mono_defaults.methodhandle_class;
2575 case MONO_TOKEN_FIELD_DEF:
2577 *handle_class = mono_defaults.fieldhandle_class;
2580 g_assert_not_reached ();
2584 switch (token & 0xff000000) {
2585 case MONO_TOKEN_TYPE_DEF:
2586 case MONO_TOKEN_TYPE_REF: {
2589 *handle_class = mono_defaults.typehandle_class;
2590 class = mono_class_get (image, token);
2591 mono_class_init (class);
2592 /* We return a MonoType* as handle */
2593 return &class->byval_arg;
2595 case MONO_TOKEN_TYPE_SPEC: {
2598 *handle_class = mono_defaults.typehandle_class;
2599 class = mono_class_create_from_typespec (image, token);
2600 mono_class_init (class);
2601 return &class->byval_arg;
2603 case MONO_TOKEN_FIELD_DEF: {
2605 guint32 type = mono_metadata_typedef_from_field (image, mono_metadata_token_index (token));
2606 class = mono_class_get (image, MONO_TOKEN_TYPE_DEF | type);
2607 mono_class_init (class);
2609 *handle_class = mono_defaults.fieldhandle_class;
2610 return mono_class_get_field (class, token);
2612 case MONO_TOKEN_METHOD_DEF: {
2614 meth = mono_get_method (image, token, NULL);
2616 *handle_class = mono_defaults.methodhandle_class;
2619 case MONO_TOKEN_MEMBER_REF: {
2620 guint32 cols [MONO_MEMBERREF_SIZE];
2622 mono_metadata_decode_row (&image->tables [MONO_TABLE_MEMBERREF], mono_metadata_token_index (token) - 1, cols, MONO_MEMBERREF_SIZE);
2623 sig = mono_metadata_blob_heap (image, cols [MONO_MEMBERREF_SIGNATURE]);
2624 mono_metadata_decode_blob_size (sig, &sig);
2625 if (*sig == 0x6) { /* it's a field */
2627 MonoClassField *field;
2628 field = mono_field_from_token (image, token, &klass);
2630 *handle_class = mono_defaults.fieldhandle_class;
2634 meth = mono_get_method (image, token, NULL);
2636 *handle_class = mono_defaults.methodhandle_class;
2641 g_warning ("Unknown token 0x%08x in ldtoken", token);
2648 * This function might need to call runtime functions so it can't be part
2649 * of the metadata library.
2651 static MonoLookupDynamicToken lookup_dynamic = NULL;
2654 mono_install_lookup_dynamic_token (MonoLookupDynamicToken func)
2656 lookup_dynamic = func;
2660 mono_lookup_dynamic_token (MonoImage *image, guint32 token)
2662 return lookup_dynamic (image, token);