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>
38 gboolean mono_print_vtable = FALSE;
40 static MonoClass * mono_class_create_from_typedef (MonoImage *image, guint32 type_token);
42 void (*mono_debugger_class_init_func) (MonoClass *klass) = NULL;
45 mono_class_from_typeref (MonoImage *image, guint32 type_token)
47 guint32 cols [MONO_TYPEREF_SIZE];
48 MonoTableInfo *t = &image->tables [MONO_TABLE_TYPEREF];
50 const char *name, *nspace;
52 MonoAssembly **references;
54 mono_metadata_decode_row (t, (type_token&0xffffff)-1, cols, MONO_TYPEREF_SIZE);
56 name = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAME]);
57 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAMESPACE]);
59 idx = cols [MONO_TYPEREF_SCOPE] >> RESOLTION_SCOPE_BITS;
60 switch (cols [MONO_TYPEREF_SCOPE] & RESOLTION_SCOPE_MASK) {
61 case RESOLTION_SCOPE_MODULE:
63 g_error ("null ResolutionScope not yet handled");
64 /* a typedef in disguise */
65 return mono_class_from_name (image, nspace, name);
66 case RESOLTION_SCOPE_MODULEREF:
67 return mono_class_from_name (image->modules [idx - 1], nspace, name);
68 case RESOLTION_SCOPE_TYPEREF: {
69 MonoClass *enclosing = mono_class_from_typeref (image, MONO_TOKEN_TYPE_REF | idx);
71 mono_class_init (enclosing);
72 for (tmp = enclosing->nested_classes; tmp; tmp = tmp->next) {
74 if (strcmp (res->name, name) == 0)
77 g_warning ("TypeRef ResolutionScope not yet handled (%d)", idx);
80 case RESOLTION_SCOPE_ASSEMBLYREF:
84 references = image->references;
85 if (!references || !references [idx-1]) {
87 * detected a reference to mscorlib, we simply return a reference to a dummy
88 * until we have a better solution.
90 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]));
92 res = mono_class_from_name (image, "System", "MonoDummy");
93 /* prevent method loading */
95 /* some storage if the type is used - very ugly hack */
96 res->instance_size = 2*sizeof (gpointer);
100 /* load referenced assembly */
101 image = references [idx-1]->image;
103 return mono_class_from_name (image, nspace, name);
107 dup_type (MonoType* t)
109 MonoType *r = g_new0 (MonoType, 1);
115 mono_type_get_name_recurse (MonoType *type, GString *str)
119 switch (type->type) {
120 case MONO_TYPE_ARRAY: {
121 int i, rank = type->data.array->rank;
123 mono_type_get_name_recurse (&type->data.array->eklass->byval_arg, str);
124 g_string_append_c (str, '[');
125 for (i = 1; i < rank; i++)
126 g_string_append_c (str, ',');
127 g_string_append_c (str, ']');
130 case MONO_TYPE_SZARRAY:
131 mono_type_get_name_recurse (&type->data.klass->byval_arg, str);
132 g_string_append (str, "[]");
135 mono_type_get_name_recurse (type->data.type, str);
136 g_string_append_c (str, '*');
139 klass = mono_class_from_mono_type (type);
140 if (klass->nested_in) {
141 mono_type_get_name_recurse (&klass->nested_in->byval_arg, str);
142 g_string_append_c (str, '+');
144 if (*klass->name_space) {
145 g_string_append (str, klass->name_space);
146 g_string_append_c (str, '.');
148 g_string_append (str, klass->name);
154 * mono_type_get_name:
157 * Returns the string representation for type as required by System.Reflection.
158 * The inverse of mono_reflection_parse_type ().
161 mono_type_get_name (MonoType *type)
163 GString* result = g_string_new ("");
164 mono_type_get_name_recurse (type, result);
167 g_string_append_c (result, '&');
169 return g_string_free (result, FALSE);
173 mono_class_is_open_constructed_type (MonoType *t)
179 case MONO_TYPE_SZARRAY:
180 return mono_class_is_open_constructed_type (&t->data.klass->byval_arg);
181 case MONO_TYPE_ARRAY:
182 return mono_class_is_open_constructed_type (&t->data.array->eklass->byval_arg);
184 return mono_class_is_open_constructed_type (t->data.type);
185 case MONO_TYPE_GENERICINST: {
186 MonoGenericInst *ginst = t->data.generic_inst;
189 if (mono_class_is_open_constructed_type (ginst->generic_type))
191 for (i = 0; i < ginst->type_argc; i++)
192 if (mono_class_is_open_constructed_type (ginst->type_argv [i]))
202 mono_class_inflate_generic_type (MonoType *type, MonoGenericInst *ginst,
203 MonoGenericMethod *gmethod)
205 switch (type->type) {
207 if (gmethod && gmethod->mtype_argv)
208 return dup_type (gmethod->mtype_argv [type->data.generic_param->num]);
213 return dup_type (ginst->type_argv [type->data.generic_param->num]);
216 case MONO_TYPE_SZARRAY: {
217 MonoClass *eclass = type->data.klass;
220 if ((eclass->byval_arg.type == MONO_TYPE_MVAR) && gmethod) {
221 nclass = mono_class_from_mono_type (gmethod->mtype_argv [eclass->byval_arg.data.generic_param->num]);
222 } else if ((eclass->byval_arg.type == MONO_TYPE_VAR) && ginst) {
223 nclass = mono_class_from_mono_type (ginst->type_argv [eclass->byval_arg.data.generic_param->num]);
227 nt = dup_type (type);
228 nt->data.klass = nclass;
231 case MONO_TYPE_GENERICINST: {
232 MonoGenericInst *oginst = type->data.generic_inst;
233 MonoGenericInst *nginst;
237 nginst = g_new0 (MonoGenericInst, 1);
240 nginst->is_open = FALSE;
242 nginst->type_argv = g_new0 (MonoType *, oginst->type_argc);
244 for (i = 0; i < oginst->type_argc; i++) {
245 MonoType *t = oginst->type_argv [i];
246 nginst->type_argv [i] = mono_class_inflate_generic_type (t, ginst, gmethod);
248 if (!nginst->is_open)
249 nginst->is_open = mono_class_is_open_constructed_type (nginst->type_argv [i]);
252 nginst->klass = NULL;
255 nt = g_hash_table_lookup (oginst->klass->image->generic_inst_cache, nginst);
258 g_free (nginst->type_argv);
260 mono_loader_unlock ();
264 mono_class_create_generic (nginst);
266 nt = dup_type (type);
267 nt->data.generic_inst = nginst;
268 g_hash_table_insert (oginst->klass->image->generic_inst_cache, nginst, nt);
269 mono_loader_unlock ();
278 static MonoMethodSignature*
279 inflate_generic_signature (MonoImage *image, MonoMethodSignature *sig,
280 MonoGenericMethod *gmethod)
282 MonoMethodSignature *res;
284 res = mono_metadata_signature_alloc (image, sig->param_count);
285 res->ret = mono_class_inflate_generic_type (sig->ret, gmethod->generic_inst, gmethod);
286 for (i = 0; i < sig->param_count; ++i)
287 res->params [i] = mono_class_inflate_generic_type (sig->params [i],
288 gmethod->generic_inst,
290 res->hasthis = sig->hasthis;
291 res->explicit_this = sig->explicit_this;
292 res->call_convention = sig->call_convention;
293 res->generic_param_count = sig->generic_param_count;
294 res->gen_method = gmethod;
298 static MonoMethodHeader*
299 inflate_generic_header (MonoMethodHeader *header, MonoGenericMethod *gmethod)
301 MonoMethodHeader *res;
303 res = g_malloc0 (sizeof (MonoMethodHeader) + sizeof (gpointer) * header->num_locals);
304 res->code = header->code;
305 res->code_size = header->code_size;
306 res->max_stack = header->max_stack;
307 res->num_clauses = header->num_clauses;
308 res->init_locals = header->init_locals;
309 res->num_locals = header->num_locals;
310 res->clauses = header->clauses;
311 res->gen_params = header->gen_params;
312 for (i = 0; i < header->num_locals; ++i)
313 res->locals [i] = mono_class_inflate_generic_type (header->locals [i],
314 gmethod->generic_inst,
320 mono_class_inflate_generic_method (MonoMethod *method, MonoGenericMethod *gmethod,
324 if ((method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) || (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)) {
325 MonoMethodPInvoke *nmethod = g_new0 (MonoMethodPInvoke, 1);
326 *nmethod = *(MonoMethodPInvoke*)method;
327 result = (MonoMethod*)nmethod;
329 MonoMethodNormal *nmethod = g_new0 (MonoMethodNormal, 1);
330 *nmethod = *(MonoMethodNormal*)method;
331 result = (MonoMethod*)nmethod;
333 nmethod->header = inflate_generic_header (nmethod->header, gmethod);
336 result->klass = klass;
338 MonoType *declaring = mono_class_inflate_generic_type (
339 &method->klass->byval_arg, gmethod->generic_inst, gmethod);
340 result->klass = mono_class_from_mono_type (declaring);
343 result->signature = inflate_generic_signature (
344 method->klass->image, result->signature, gmethod);
349 * class_compute_field_layout:
350 * @m: pointer to the metadata.
351 * @class: The class to initialize
353 * Initializes the class->fields.
355 * Currently we only support AUTO_LAYOUT, and do not even try to do
356 * a good job at it. This is temporary to get the code for Paolo.
359 class_compute_field_layout (MonoClass *class)
361 MonoImage *m = class->image;
362 const int top = class->field.count;
363 guint32 layout = class->flags & TYPE_ATTRIBUTE_LAYOUT_MASK;
364 MonoTableInfo *t = &m->tables [MONO_TABLE_FIELD];
365 int i, blittable = TRUE, real_size = 0;
367 guint32 packing_size = 0;
368 gboolean explicit_size;
369 MonoClassField *field;
371 if (class->size_inited)
375 if (!class->parent->size_inited)
376 class_compute_field_layout (class->parent);
377 class->instance_size += class->parent->instance_size;
378 class->min_align = class->parent->min_align;
379 blittable = class->blittable;
381 class->instance_size = sizeof (MonoObject);
382 class->min_align = 1;
385 /* Get the real size */
386 explicit_size = mono_metadata_packing_from_typedef (class->image, class->type_token, &packing_size, &real_size);
389 g_assert ((packing_size & 0xfffffff0) == 0);
390 class->packing_size = packing_size;
391 real_size += class->instance_size;
395 if (explicit_size && real_size) {
396 class->instance_size = MAX (real_size, class->instance_size);
398 class->size_inited = 1;
402 class->fields = g_new0 (MonoClassField, top);
405 * Fetch all the field information.
407 for (i = 0; i < top; i++){
409 guint32 cols [MONO_FIELD_SIZE];
410 int idx = class->field.first + i;
412 field = &class->fields [i];
413 mono_metadata_decode_row (t, idx, cols, MONO_FIELD_SIZE);
414 /* The name is needed for fieldrefs */
415 field->name = mono_metadata_string_heap (m, cols [MONO_FIELD_NAME]);
416 sig = mono_metadata_blob_heap (m, cols [MONO_FIELD_SIGNATURE]);
417 mono_metadata_decode_value (sig, &sig);
418 /* FIELD signature == 0x06 */
419 g_assert (*sig == 0x06);
420 field->type = mono_metadata_parse_field_type (
421 m, cols [MONO_FIELD_FLAGS], sig + 1, &sig);
422 if (mono_field_is_deleted (field))
424 if (class->generic_inst) {
425 field->type = mono_class_inflate_generic_type (field->type, class->generic_inst, NULL);
426 field->type->attrs = cols [MONO_FIELD_FLAGS];
429 field->parent = class;
431 if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC)) {
432 if (field->type->byref) {
435 MonoClass *field_class = mono_class_from_mono_type (field->type);
436 if (!field_class || !field_class->blittable)
440 if (layout == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT) {
441 mono_metadata_field_info (m, idx, &field->offset, NULL, NULL);
442 if (field->offset == (guint32)-1)
443 g_warning ("%s not initialized correctly (missing field layout info for %s)", class->name, field->name);
446 if (cols [MONO_FIELD_FLAGS] & FIELD_ATTRIBUTE_HAS_FIELD_RVA) {
447 mono_metadata_field_info (m, idx, NULL, &rva, NULL);
449 g_warning ("field %s in %s should have RVA data, but hasn't", field->name, class->name);
450 field->data = mono_cli_rva_map (class->image->image_info, rva);
453 if (class->enumtype && !(cols [MONO_FIELD_FLAGS] & FIELD_ATTRIBUTE_STATIC)) {
454 class->enum_basetype = field->type;
455 class->cast_class = class->element_class = mono_class_from_mono_type (class->enum_basetype);
456 blittable = class->element_class->blittable;
459 /* The def_value of fields is compute lazily during vtable creation */
462 if (class == mono_defaults.string_class)
465 class->blittable = blittable;
467 if (class->enumtype && !class->enum_basetype) {
468 if (!((strcmp (class->name, "Enum") == 0) && (strcmp (class->name_space, "System") == 0)))
471 if (explicit_size && real_size) {
472 class->instance_size = MAX (real_size, class->instance_size);
475 if (class->gen_params)
478 mono_class_layout_fields (class);
482 mono_class_layout_fields (MonoClass *class)
485 const int top = class->field.count;
486 guint32 layout = class->flags & TYPE_ATTRIBUTE_LAYOUT_MASK;
487 guint32 pass, passes, real_size;
488 gboolean gc_aware_layout = FALSE;
489 MonoClassField *field;
492 * Enable GC aware auto layout: in this mode, reference
493 * fields are grouped together inside objects, increasing collector
495 * Requires that all classes whose layout is known to native code be annotated
496 * with [StructLayout (LayoutKind.Sequential)]
498 /* corlib is missing [StructLayout] directives in many places */
499 if (layout == TYPE_ATTRIBUTE_AUTO_LAYOUT) {
500 if (class->image != mono_defaults.corlib)
501 gc_aware_layout = TRUE;
505 * Compute field layout and total size (not considering static fields)
509 case TYPE_ATTRIBUTE_AUTO_LAYOUT:
510 case TYPE_ATTRIBUTE_SEQUENTIAL_LAYOUT:
517 if (layout != TYPE_ATTRIBUTE_AUTO_LAYOUT)
521 real_size = class->parent->instance_size;
523 real_size = sizeof (MonoObject);
525 for (pass = 0; pass < passes; ++pass) {
526 for (i = 0; i < top; i++){
528 field = &class->fields [i];
530 if (mono_field_is_deleted (field))
532 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
535 if (gc_aware_layout) {
537 * We process fields with reference type in the first pass,
538 * and fields with non-reference type in the second pass.
539 * We use IS_POINTER instead of IS_REFERENCE because in
540 * some internal structures, we store GC_MALLOCed memory
541 * in IntPtr fields...
543 if (MONO_TYPE_IS_POINTER (field->type)) {
552 if ((top == 1) && (class->instance_size == sizeof (MonoObject)) &&
553 (strcmp (field->name, "$PRIVATE$") == 0)) {
554 /* This field is a hack inserted by MCS to empty structures */
558 size = mono_type_size (field->type, &align);
560 /* FIXME (LAMESPEC): should we also change the min alignment according to pack? */
561 align = class->packing_size ? MIN (class->packing_size, align): align;
562 class->min_align = MAX (align, class->min_align);
563 field->offset = real_size;
564 field->offset += align - 1;
565 field->offset &= ~(align - 1);
566 real_size = field->offset + size;
569 class->instance_size = MAX (real_size, class->instance_size);
571 if (class->instance_size & (class->min_align - 1)) {
572 class->instance_size += class->min_align - 1;
573 class->instance_size &= ~(class->min_align - 1);
577 case TYPE_ATTRIBUTE_EXPLICIT_LAYOUT:
579 for (i = 0; i < top; i++) {
581 field = &class->fields [i];
584 * There must be info about all the fields in a type if it
585 * uses explicit layout.
588 if (mono_field_is_deleted (field))
590 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
593 size = mono_type_size (field->type, &align);
596 * When we get here, field->offset is already set by the
597 * loader (for either runtime fields or fields loaded from metadata).
598 * The offset is from the start of the object: this works for both
599 * classes and valuetypes.
601 field->offset += sizeof (MonoObject);
606 real_size = MAX (real_size, size + field->offset);
608 class->instance_size = MAX (real_size, class->instance_size);
612 class->size_inited = 1;
615 * Compute static field layout and size
617 for (i = 0; i < top; i++){
619 field = &class->fields [i];
621 if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC))
623 if (mono_field_is_deleted (field))
626 size = mono_type_size (field->type, &align);
627 field->offset = class->class_size;
628 field->offset += align - 1;
629 field->offset &= ~(align - 1);
630 class->class_size = field->offset + size;
635 init_properties (MonoClass *class)
637 guint startm, endm, i, j;
638 guint32 cols [MONO_PROPERTY_SIZE];
639 MonoTableInfo *pt = &class->image->tables [MONO_TABLE_PROPERTY];
640 MonoTableInfo *msemt = &class->image->tables [MONO_TABLE_METHODSEMANTICS];
642 class->property.first = mono_metadata_properties_from_typedef (class->image, mono_metadata_token_index (class->type_token) - 1, &class->property.last);
643 class->property.count = class->property.last - class->property.first;
645 class->properties = g_new0 (MonoProperty, class->property.count);
646 for (i = class->property.first; i < class->property.last; ++i) {
647 mono_metadata_decode_row (pt, i, cols, MONO_PROPERTY_SIZE);
648 class->properties [i - class->property.first].parent = class;
649 class->properties [i - class->property.first].attrs = cols [MONO_PROPERTY_FLAGS];
650 class->properties [i - class->property.first].name = mono_metadata_string_heap (class->image, cols [MONO_PROPERTY_NAME]);
652 startm = mono_metadata_methods_from_property (class->image, i, &endm);
653 for (j = startm; j < endm; ++j) {
654 mono_metadata_decode_row (msemt, j, cols, MONO_METHOD_SEMA_SIZE);
655 switch (cols [MONO_METHOD_SEMA_SEMANTICS]) {
656 case METHOD_SEMANTIC_SETTER:
657 class->properties [i - class->property.first].set = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
659 case METHOD_SEMANTIC_GETTER:
660 class->properties [i - class->property.first].get = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
670 init_events (MonoClass *class)
672 guint startm, endm, i, j;
673 guint32 cols [MONO_EVENT_SIZE];
674 MonoTableInfo *pt = &class->image->tables [MONO_TABLE_EVENT];
675 MonoTableInfo *msemt = &class->image->tables [MONO_TABLE_METHODSEMANTICS];
677 class->event.first = mono_metadata_events_from_typedef (class->image, mono_metadata_token_index (class->type_token) - 1, &class->event.last);
678 class->event.count = class->event.last - class->event.first;
680 class->events = g_new0 (MonoEvent, class->event.count);
681 for (i = class->event.first; i < class->event.last; ++i) {
682 mono_metadata_decode_row (pt, i, cols, MONO_EVENT_SIZE);
683 class->events [i - class->event.first].parent = class;
684 class->events [i - class->event.first].attrs = cols [MONO_EVENT_FLAGS];
685 class->events [i - class->event.first].name = mono_metadata_string_heap (class->image, cols [MONO_EVENT_NAME]);
687 startm = mono_metadata_methods_from_event (class->image, i, &endm);
688 for (j = startm; j < endm; ++j) {
689 mono_metadata_decode_row (msemt, j, cols, MONO_METHOD_SEMA_SIZE);
690 switch (cols [MONO_METHOD_SEMA_SEMANTICS]) {
691 case METHOD_SEMANTIC_ADD_ON:
692 class->events [i - class->event.first].add = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
694 case METHOD_SEMANTIC_REMOVE_ON:
695 class->events [i - class->event.first].remove = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
697 case METHOD_SEMANTIC_FIRE:
698 class->events [i - class->event.first].raise = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
700 case METHOD_SEMANTIC_OTHER: /* don't care for now */
709 mono_get_unique_iid (MonoClass *class)
711 static GHashTable *iid_hash = NULL;
712 static guint iid = 0;
717 g_assert (class->flags & TYPE_ATTRIBUTE_INTERFACE);
722 iid_hash = g_hash_table_new (g_str_hash, g_str_equal);
724 str = g_strdup_printf ("%s|%s.%s\n", class->image->name, class->name_space, class->name);
726 if (g_hash_table_lookup_extended (iid_hash, str, NULL, &value)) {
727 mono_loader_unlock ();
731 g_hash_table_insert (iid_hash, str, (gpointer)iid);
735 mono_loader_unlock ();
741 collect_implemented_interfaces_aux (MonoClass *klass, GPtrArray **res)
746 for (i = 0; i < klass->interface_count; i++) {
747 ic = klass->interfaces [i];
750 *res = g_ptr_array_new ();
751 g_ptr_array_add (*res, ic);
753 collect_implemented_interfaces_aux (ic, res);
757 static inline GPtrArray*
758 collect_implemented_interfaces (MonoClass *klass)
760 GPtrArray *res = NULL;
762 collect_implemented_interfaces_aux (klass, &res);
767 setup_interface_offsets (MonoClass *class, int cur_slot)
773 /* compute maximum number of slots and maximum interface id */
775 for (k = class; k ; k = k->parent) {
776 for (i = 0; i < k->interface_count; i++) {
777 ic = k->interfaces [i];
780 mono_class_init (ic);
782 if (max_iid < ic->interface_id)
783 max_iid = ic->interface_id;
787 if (class->flags & TYPE_ATTRIBUTE_INTERFACE) {
788 if (max_iid < class->interface_id)
789 max_iid = class->interface_id;
791 class->max_interface_id = max_iid;
792 /* compute vtable offset for interfaces */
793 class->interface_offsets = g_malloc (sizeof (gpointer) * (max_iid + 1));
795 for (i = 0; i <= max_iid; i++)
796 class->interface_offsets [i] = -1;
798 ifaces = collect_implemented_interfaces (class);
800 for (i = 0; i < ifaces->len; ++i) {
801 ic = g_ptr_array_index (ifaces, i);
802 class->interface_offsets [ic->interface_id] = cur_slot;
803 cur_slot += ic->method.count;
805 g_ptr_array_free (ifaces, TRUE);
808 for (k = class->parent; k ; k = k->parent) {
809 ifaces = collect_implemented_interfaces (k);
811 for (i = 0; i < ifaces->len; ++i) {
812 ic = g_ptr_array_index (ifaces, i);
814 if (class->interface_offsets [ic->interface_id] == -1) {
815 int io = k->interface_offsets [ic->interface_id];
819 class->interface_offsets [ic->interface_id] = io;
822 g_ptr_array_free (ifaces, TRUE);
826 if (class->flags & TYPE_ATTRIBUTE_INTERFACE)
827 class->interface_offsets [class->interface_id] = cur_slot;
833 mono_class_setup_vtable (MonoClass *class, MonoMethod **overrides, int onum)
837 int i, max_vtsize = 0, max_iid, cur_slot = 0;
839 MonoGHashTable *override_map = NULL;
841 /* setup_vtable() must be called only once on the type */
842 if (class->interface_offsets) {
843 g_warning ("vtable already computed in %s.%s", class->name_space, class->name);
847 ifaces = collect_implemented_interfaces (class);
849 for (i = 0; i < ifaces->len; i++) {
850 MonoClass *ic = g_ptr_array_index (ifaces, i);
851 max_vtsize += ic->method.count;
853 g_ptr_array_free (ifaces, TRUE);
857 max_vtsize += class->parent->vtable_size;
858 cur_slot = class->parent->vtable_size;
861 max_vtsize += class->method.count;
863 vtable = alloca (sizeof (gpointer) * max_vtsize);
864 memset (vtable, 0, sizeof (gpointer) * max_vtsize);
866 /* printf ("METAINIT %s.%s\n", class->name_space, class->name); */
868 cur_slot = setup_interface_offsets (class, cur_slot);
869 max_iid = class->max_interface_id;
871 if (class->parent && class->parent->vtable_size)
872 memcpy (vtable, class->parent->vtable, sizeof (gpointer) * class->parent->vtable_size);
874 /* override interface methods */
875 for (i = 0; i < onum; i++) {
876 MonoMethod *decl = overrides [i*2];
877 if (decl->klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
879 g_assert (decl->slot != -1);
880 dslot = decl->slot + class->interface_offsets [decl->klass->interface_id];
881 vtable [dslot] = overrides [i*2 + 1];
882 vtable [dslot]->slot = dslot;
884 override_map = mono_g_hash_table_new (NULL, NULL);
886 mono_g_hash_table_insert (override_map, overrides [i * 2], overrides [i * 2 + 1]);
890 for (k = class; k ; k = k->parent) {
892 ifaces = collect_implemented_interfaces (k);
894 nifaces = ifaces->len;
895 for (i = 0; i < nifaces; i++) {
898 ic = g_ptr_array_index (ifaces, i);
899 io = k->interface_offsets [ic->interface_id];
902 g_assert (io <= max_vtsize);
905 for (l = 0; l < ic->method.count; l++) {
906 MonoMethod *im = ic->methods [l];
908 if (vtable [io + l] && !(vtable [io + l]->flags & METHOD_ATTRIBUTE_ABSTRACT))
911 for (j = 0; j < class->method.count; ++j) {
912 MonoMethod *cm = class->methods [j];
913 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
914 !((cm->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == METHOD_ATTRIBUTE_PUBLIC) ||
915 !(cm->flags & METHOD_ATTRIBUTE_NEW_SLOT))
917 if (!strcmp(cm->name, im->name) &&
918 mono_metadata_signature_equal (cm->signature, im->signature)) {
919 g_assert (io + l <= max_vtsize);
920 vtable [io + l] = cm;
925 /* already implemented */
926 if (io >= k->vtable_size)
930 for (l = 0; l < ic->method.count; l++) {
931 MonoMethod *im = ic->methods [l];
934 g_assert (io + l <= max_vtsize);
936 if (vtable [io + l] && !(vtable [io + l]->flags & METHOD_ATTRIBUTE_ABSTRACT))
939 for (k1 = class; k1; k1 = k1->parent) {
940 for (j = 0; j < k1->method.count; ++j) {
941 MonoMethod *cm = k1->methods [j];
943 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
944 !(cm->flags & METHOD_ATTRIBUTE_PUBLIC))
947 if (!strcmp(cm->name, im->name) &&
948 mono_metadata_signature_equal (cm->signature, im->signature)) {
949 g_assert (io + l <= max_vtsize);
950 vtable [io + l] = cm;
955 g_assert (io + l <= max_vtsize);
956 if (vtable [io + l] && !(vtable [io + l]->flags & METHOD_ATTRIBUTE_ABSTRACT))
961 for (l = 0; l < ic->method.count; l++) {
962 MonoMethod *im = ic->methods [l];
963 char *qname, *fqname;
969 qname = g_strconcat (ic->name, ".", im->name, NULL);
970 if (ic->name_space && ic->name_space [0])
971 fqname = g_strconcat (ic->name_space, ".", ic->name, ".", im->name, NULL);
975 for (k1 = class; k1; k1 = k1->parent) {
976 for (j = 0; j < k1->method.count; ++j) {
977 MonoMethod *cm = k1->methods [j];
979 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL))
982 if (((fqname && !strcmp (cm->name, fqname)) || !strcmp (cm->name, qname)) &&
983 mono_metadata_signature_equal (cm->signature, im->signature)) {
984 g_assert (io + l <= max_vtsize);
985 vtable [io + l] = cm;
995 if (!(class->flags & TYPE_ATTRIBUTE_ABSTRACT)) {
996 for (l = 0; l < ic->method.count; l++) {
998 MonoMethod *im = ic->methods [l];
999 if (im->flags & METHOD_ATTRIBUTE_STATIC)
1001 g_assert (io + l <= max_vtsize);
1004 * If one of our parents already implements this interface
1005 * we can inherit the implementation.
1007 if (!(vtable [io + l])) {
1008 MonoClass *parent = class->parent;
1010 if ((ic->interface_id <= parent->max_interface_id) &&
1011 (parent->interface_offsets [ic->interface_id]) &&
1013 vtable [io + l] = parent->vtable [parent->interface_offsets [ic->interface_id] + l];
1016 if (!(vtable [io + l])) {
1017 for (j = 0; j < onum; ++j) {
1018 g_print (" at slot %d: %s (%d) overrides %s (%d)\n", io+l, overrides [j*2+1]->name,
1019 overrides [j*2+1]->slot, overrides [j*2]->name, overrides [j*2]->slot);
1021 msig = mono_signature_get_desc (im->signature, FALSE);
1022 printf ("no implementation for interface method %s.%s::%s(%s) in class %s.%s\n",
1023 ic->name_space, ic->name, im->name, msig, class->name_space, class->name);
1025 for (j = 0; j < class->method.count; ++j) {
1026 MonoMethod *cm = class->methods [j];
1027 msig = mono_signature_get_desc (cm->signature, FALSE);
1029 printf ("METHOD %s(%s)\n", cm->name, msig);
1032 g_assert_not_reached ();
1037 for (l = 0; l < ic->method.count; l++) {
1038 MonoMethod *im = vtable [io + l];
1041 g_assert (io + l <= max_vtsize);
1043 /* FIXME: why do we need this ? */
1045 /* g_assert_not_reached (); */
1051 g_ptr_array_free (ifaces, TRUE);
1054 for (i = 0; i < class->method.count; ++i) {
1057 cm = class->methods [i];
1059 if (!(cm->flags & METHOD_ATTRIBUTE_NEW_SLOT) && (cm->flags & METHOD_ATTRIBUTE_VIRTUAL)) {
1061 for (k = class->parent; k ; k = k->parent) {
1063 for (j = 0; j < k->method.count; ++j) {
1064 MonoMethod *m1 = k->methods [j];
1065 if (!(m1->flags & METHOD_ATTRIBUTE_VIRTUAL))
1067 if (!strcmp(cm->name, m1->name) &&
1068 mono_metadata_signature_equal (cm->signature, m1->signature)) {
1069 slot = k->methods [j]->slot;
1070 g_assert (cm->slot < max_vtsize);
1072 override_map = mono_g_hash_table_new (NULL, NULL);
1073 mono_g_hash_table_insert (override_map, m1, cm);
1085 cm->slot = cur_slot++;
1087 if (!(cm->flags & METHOD_ATTRIBUTE_ABSTRACT) && !cm->signature->generic_param_count)
1088 vtable [cm->slot] = cm;
1091 /* override non interface methods */
1092 for (i = 0; i < onum; i++) {
1093 MonoMethod *decl = overrides [i*2];
1094 if (!(decl->klass->flags & TYPE_ATTRIBUTE_INTERFACE)) {
1095 g_assert (decl->slot != -1);
1096 vtable [decl->slot] = overrides [i*2 + 1];
1097 overrides [i * 2 + 1]->slot = decl->slot;
1099 override_map = mono_g_hash_table_new (NULL, NULL);
1100 mono_g_hash_table_insert (override_map, decl, overrides [i * 2 + 1]);
1105 * If a method occupies more than one place in the vtable, and it is
1106 * overriden, then change the other occurances too.
1109 for (i = 0; i < max_vtsize; ++i)
1111 MonoMethod *cm = mono_g_hash_table_lookup (override_map, vtable [i]);
1116 mono_g_hash_table_destroy (override_map);
1119 class->vtable_size = cur_slot;
1120 class->vtable = g_malloc0 (sizeof (gpointer) * class->vtable_size);
1121 memcpy (class->vtable, vtable, sizeof (gpointer) * class->vtable_size);
1123 if (mono_print_vtable) {
1126 for (i = 0; i <= max_iid; i++)
1127 if (class->interface_offsets [i] != -1)
1130 printf ("VTable %s.%s (size = %d, interfaces = %d)\n", class->name_space,
1131 class->name, class->vtable_size, icount);
1133 for (i = 0; i < class->vtable_size; ++i) {
1138 printf (" slot %03d(%03d) %s.%s:%s\n", i, cm->slot,
1139 cm->klass->name_space, cm->klass->name,
1146 printf ("Interfaces %s.%s (max_iid = %d)\n", class->name_space,
1147 class->name, max_iid);
1149 for (i = 0; i < class->interface_count; i++) {
1150 ic = class->interfaces [i];
1151 printf (" slot %03d(%03d) %s.%s\n",
1152 class->interface_offsets [ic->interface_id],
1153 ic->method.count, ic->name_space, ic->name);
1156 for (k = class->parent; k ; k = k->parent) {
1157 for (i = 0; i < k->interface_count; i++) {
1158 ic = k->interfaces [i];
1159 printf (" slot %03d(%03d) %s.%s\n",
1160 class->interface_offsets [ic->interface_id],
1161 ic->method.count, ic->name_space, ic->name);
1169 inflate_method (MonoGenericInst *ginst, MonoMethod *method)
1171 MonoGenericMethod *gmethod = g_new0 (MonoGenericMethod, 1);
1173 gmethod->generic_method = method;
1174 gmethod->generic_inst = ginst;
1176 return mono_class_inflate_generic_method (method, gmethod, ginst->klass);
1181 * @class: the class to initialize
1183 * compute the instance_size, class_size and other infos that cannot be
1184 * computed at mono_class_get() time. Also compute a generic vtable and
1185 * the method slot numbers. We use this infos later to create a domain
1189 mono_class_init (MonoClass *class)
1192 static MonoMethod *default_ghc = NULL;
1193 static MonoMethod *default_finalize = NULL;
1194 static int finalize_slot = -1;
1195 static int ghc_slot = -1;
1196 MonoMethod **overrides;
1204 /*g_print ("Init class %s\n", class->name);*/
1206 /* We do everything inside the lock to prevent races */
1207 mono_loader_lock ();
1209 if (class->inited) {
1210 mono_loader_unlock ();
1211 /* Somebody might have gotten in before us */
1215 if (class->init_pending) {
1216 /* this indicates a cyclic dependency */
1217 g_error ("pending init %s.%s\n", class->name_space, class->name);
1220 class->init_pending = 1;
1222 mono_stats.initialized_class_count++;
1224 if (class->generic_inst && !class->generic_inst->is_dynamic) {
1225 MonoGenericInst *ginst = class->generic_inst;
1228 gklass = mono_class_from_mono_type (ginst->generic_type);
1229 mono_class_init (gklass);
1232 class->parent = mono_class_from_mono_type (ginst->parent);
1234 class->parent = gklass->parent;
1236 mono_class_setup_parent (class, class->parent);
1238 if (class->flags & TYPE_ATTRIBUTE_INTERFACE)
1239 class->interface_id = mono_get_unique_iid (class);
1241 class->method = gklass->method;
1242 class->methods = g_new0 (MonoMethod *, class->method.count);
1244 for (i = 0; i < class->method.count; i++)
1245 class->methods [i] = inflate_method (ginst, gklass->methods [i]);
1247 class->field = gklass->field;
1248 class->fields = g_new0 (MonoClassField, class->field.count);
1250 for (i = 0; i < class->field.count; i++) {
1251 class->fields [i] = gklass->fields [i];
1252 class->fields [i].generic_type = gklass->fields [i].type;
1253 class->fields [i].parent = class;
1254 class->fields [i].type = mono_class_inflate_generic_type (
1255 class->fields [i].type, ginst, NULL);
1258 class->property = gklass->property;
1259 class->properties = g_new0 (MonoProperty, class->property.count);
1261 for (i = 0; i < class->property.count; i++) {
1262 MonoProperty *prop = &class->properties [i];
1264 *prop = gklass->properties [i];
1267 prop->get = inflate_method (ginst, prop->get);
1269 prop->set = inflate_method (ginst, prop->set);
1272 class->interface_count = gklass->interface_count;
1273 class->interfaces = g_new0 (MonoClass *, class->interface_count);
1274 for (i = 0; i < class->interface_count; i++) {
1275 MonoType *it = &gklass->interfaces [i]->byval_arg;
1276 MonoType *inflated = mono_class_inflate_generic_type (it, ginst, NULL);
1277 class->interfaces [i] = mono_class_from_mono_type (inflated);
1278 mono_class_init (class->interfaces [i]);
1282 if (class->parent && !class->parent->inited)
1283 mono_class_init (class->parent);
1286 * Computes the size used by the fields, and their locations
1288 if (!class->size_inited)
1289 class_compute_field_layout (class);
1291 /* initialize method pointers */
1294 MonoMethodSignature *sig;
1295 class->method.count = class->rank > 1? 2: 1;
1296 sig = mono_metadata_signature_alloc (class->image, class->rank);
1297 sig->ret = &mono_defaults.void_class->byval_arg;
1298 sig->pinvoke = TRUE;
1299 for (i = 0; i < class->rank; ++i)
1300 sig->params [i] = &mono_defaults.int32_class->byval_arg;
1302 ctor = (MonoMethod *) g_new0 (MonoMethodPInvoke, 1);
1303 ctor->klass = class;
1304 ctor->flags = METHOD_ATTRIBUTE_PUBLIC | METHOD_ATTRIBUTE_RT_SPECIAL_NAME | METHOD_ATTRIBUTE_SPECIAL_NAME;
1305 ctor->iflags = METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL;
1306 ctor->signature = sig;
1307 ctor->name = ".ctor";
1309 class->methods = g_new (MonoMethod*, class->method.count);
1310 class->methods [0] = ctor;
1311 if (class->rank > 1) {
1312 sig = mono_metadata_signature_alloc (class->image, class->rank * 2);
1313 sig->ret = &mono_defaults.void_class->byval_arg;
1314 sig->pinvoke = TRUE;
1315 for (i = 0; i < class->rank * 2; ++i)
1316 sig->params [i] = &mono_defaults.int32_class->byval_arg;
1318 ctor = (MonoMethod *) g_new0 (MonoMethodPInvoke, 1);
1319 ctor->klass = class;
1320 ctor->flags = METHOD_ATTRIBUTE_PUBLIC | METHOD_ATTRIBUTE_RT_SPECIAL_NAME | METHOD_ATTRIBUTE_SPECIAL_NAME;
1321 ctor->iflags = METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL;
1322 ctor->signature = sig;
1323 ctor->name = ".ctor";
1325 class->methods [1] = ctor;
1328 if (!class->generic_inst && !class->methods) {
1329 class->methods = g_new (MonoMethod*, class->method.count);
1330 for (i = 0; i < class->method.count; ++i) {
1331 class->methods [i] = mono_get_method (class->image,
1332 MONO_TOKEN_METHOD_DEF | (i + class->method.first + 1), class);
1337 init_properties (class);
1338 init_events (class);
1340 i = mono_metadata_nesting_typedef (class->image, class->type_token, 1);
1343 guint32 cols [MONO_NESTED_CLASS_SIZE];
1344 mono_metadata_decode_row (&class->image->tables [MONO_TABLE_NESTEDCLASS], i - 1, cols, MONO_NESTED_CLASS_SIZE);
1345 nclass = mono_class_create_from_typedef (class->image, MONO_TOKEN_TYPE_DEF | cols [MONO_NESTED_CLASS_NESTED]);
1346 class->nested_classes = g_list_prepend (class->nested_classes, nclass);
1348 i = mono_metadata_nesting_typedef (class->image, class->type_token, i + 1);
1351 mono_class_setup_supertypes (class);
1353 if (class->flags & TYPE_ATTRIBUTE_INTERFACE) {
1354 for (i = 0; i < class->method.count; ++i)
1355 class->methods [i]->slot = i;
1356 class->init_pending = 0;
1359 * class->interface_offsets is needed for the castclass/isinst code, so
1360 * we have to setup them for interfaces, too.
1362 setup_interface_offsets (class, 0);
1363 mono_loader_unlock ();
1367 overrides = mono_class_get_overrides (class->image, class->type_token, &onum);
1368 mono_class_setup_vtable (class, overrides, onum);
1372 class->init_pending = 0;
1375 if (class == mono_defaults.object_class) {
1377 for (i = 0; i < class->vtable_size; ++i) {
1378 MonoMethod *cm = class->vtable [i];
1380 if (!strcmp (cm->name, "GetHashCode")) {
1386 g_assert (ghc_slot > 0);
1388 default_ghc = class->vtable [ghc_slot];
1393 if (class->parent) {
1395 if (class->vtable [ghc_slot] == default_ghc) {
1400 if (!default_finalize) {
1401 if (class == mono_defaults.object_class) {
1403 for (i = 0; i < class->vtable_size; ++i) {
1404 MonoMethod *cm = class->vtable [i];
1406 if (!strcmp (cm->name, "Finalize")) {
1412 g_assert (finalize_slot > 0);
1414 default_finalize = class->vtable [finalize_slot];
1418 /* Object::Finalize should have empty implemenatation */
1419 class->has_finalize = 0;
1420 if (class->parent) {
1421 if (class->vtable [finalize_slot] != default_finalize)
1422 class->has_finalize = 1;
1425 mono_loader_unlock ();
1427 if (mono_debugger_class_init_func)
1428 mono_debugger_class_init_func (class);
1433 mono_class_setup_mono_type (MonoClass *class)
1435 const char *name = class->name;
1436 const char *nspace = class->name_space;
1438 if (class->flags & TYPE_ATTRIBUTE_INTERFACE)
1439 class->interface_id = mono_get_unique_iid (class);
1441 class->this_arg.byref = 1;
1442 class->this_arg.data.klass = class;
1443 class->this_arg.type = MONO_TYPE_CLASS;
1444 class->byval_arg.data.klass = class;
1445 class->byval_arg.type = MONO_TYPE_CLASS;
1447 if (!strcmp (nspace, "System")) {
1448 if (!strcmp (name, "ValueType")) {
1450 * do not set the valuetype bit for System.ValueType.
1451 * class->valuetype = 1;
1453 } else if (!strcmp (name, "Enum")) {
1455 * do not set the valuetype bit for System.Enum.
1456 * class->valuetype = 1;
1458 class->valuetype = 0;
1459 class->enumtype = 0;
1460 } else if (!strcmp (name, "Object")) {
1461 class->this_arg.type = class->byval_arg.type = MONO_TYPE_OBJECT;
1462 } else if (!strcmp (name, "String")) {
1463 class->this_arg.type = class->byval_arg.type = MONO_TYPE_STRING;
1464 } else if (!strcmp (name, "TypedReference")) {
1465 class->this_arg.type = class->byval_arg.type = MONO_TYPE_TYPEDBYREF;
1469 if (class->valuetype) {
1470 int t = MONO_TYPE_VALUETYPE;
1471 if (!strcmp (nspace, "System")) {
1474 if (!strcmp (name, "Boolean")) {
1475 t = MONO_TYPE_BOOLEAN;
1476 } else if (!strcmp(name, "Byte")) {
1478 class->blittable = TRUE;
1482 if (!strcmp (name, "Char")) {
1487 if (!strcmp (name, "Double")) {
1489 class->blittable = TRUE;
1493 if (!strcmp (name, "Int32")) {
1495 class->blittable = TRUE;
1496 } else if (!strcmp(name, "Int16")) {
1498 class->blittable = TRUE;
1499 } else if (!strcmp(name, "Int64")) {
1501 class->blittable = TRUE;
1502 } else if (!strcmp(name, "IntPtr")) {
1504 class->blittable = TRUE;
1508 if (!strcmp (name, "Single")) {
1510 class->blittable = TRUE;
1511 } else if (!strcmp(name, "SByte")) {
1513 class->blittable = TRUE;
1517 if (!strcmp (name, "UInt32")) {
1519 class->blittable = TRUE;
1520 } else if (!strcmp(name, "UInt16")) {
1522 class->blittable = TRUE;
1523 } else if (!strcmp(name, "UInt64")) {
1525 class->blittable = TRUE;
1526 } else if (!strcmp(name, "UIntPtr")) {
1528 class->blittable = TRUE;
1532 if (!strcmp (name, "TypedReference")) {
1533 t = MONO_TYPE_TYPEDBYREF;
1534 class->blittable = TRUE;
1538 if (!strcmp (name, "Void")) {
1546 class->this_arg.type = class->byval_arg.type = t;
1551 mono_class_setup_parent (MonoClass *class, MonoClass *parent)
1553 gboolean system_namespace;
1555 system_namespace = !strcmp (class->name_space, "System");
1557 /* if root of the hierarchy */
1558 if (system_namespace && !strcmp (class->name, "Object")) {
1559 class->parent = NULL;
1560 class->instance_size = sizeof (MonoObject);
1563 if (!strcmp (class->name, "<Module>")) {
1564 class->parent = NULL;
1565 class->instance_size = 0;
1569 if (!(class->flags & TYPE_ATTRIBUTE_INTERFACE)) {
1570 class->parent = parent;
1573 g_assert_not_reached (); /* FIXME */
1575 if (parent->generic_inst && !parent->name) {
1577 * If the parent is a generic instance, we may get
1578 * called before it is fully initialized, especially
1579 * before it has its name.
1584 class->marshalbyref = parent->marshalbyref;
1585 class->contextbound = parent->contextbound;
1586 class->delegate = parent->delegate;
1588 if (system_namespace) {
1589 if (*class->name == 'M' && !strcmp (class->name, "MarshalByRefObject"))
1590 class->marshalbyref = 1;
1592 if (*class->name == 'C' && !strcmp (class->name, "ContextBoundObject"))
1593 class->contextbound = 1;
1595 if (*class->name == 'D' && !strcmp (class->name, "Delegate"))
1596 class->delegate = 1;
1599 if (class->parent->enumtype || ((strcmp (class->parent->name, "ValueType") == 0) &&
1600 (strcmp (class->parent->name_space, "System") == 0)))
1601 class->valuetype = 1;
1602 if (((strcmp (class->parent->name, "Enum") == 0) && (strcmp (class->parent->name_space, "System") == 0))) {
1603 class->valuetype = class->enumtype = 1;
1605 /*class->enumtype = class->parent->enumtype; */
1606 mono_class_setup_supertypes (class);
1608 class->parent = NULL;
1614 mono_class_setup_supertypes (MonoClass *class)
1619 if (class->supertypes)
1623 for (k = class; k ; k = k->parent) {
1627 ms = MAX (MONO_DEFAULT_SUPERTABLE_SIZE, class->idepth);
1628 class->supertypes = g_new0 (MonoClass *, ms);
1630 if (class->parent) {
1631 for (i = class->idepth, k = class; k ; k = k->parent)
1632 class->supertypes [--i] = k;
1634 class->supertypes [0] = class;
1639 * @image: context where the image is created
1640 * @type_token: typedef token
1643 mono_class_create_from_typedef (MonoImage *image, guint32 type_token)
1645 MonoTableInfo *tt = &image->tables [MONO_TABLE_TYPEDEF];
1646 MonoClass *class, *parent = NULL;
1647 guint32 cols [MONO_TYPEDEF_SIZE];
1648 guint32 cols_next [MONO_TYPEDEF_SIZE];
1649 guint tidx = mono_metadata_token_index (type_token);
1650 const char *name, *nspace;
1652 MonoClass **interfaces;
1654 mono_loader_lock ();
1656 if ((class = g_hash_table_lookup (image->class_cache, GUINT_TO_POINTER (type_token)))) {
1657 mono_loader_unlock ();
1661 g_assert (mono_metadata_token_table (type_token) == MONO_TABLE_TYPEDEF);
1663 mono_metadata_decode_row (tt, tidx - 1, cols, MONO_TYPEDEF_SIZE);
1665 name = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
1666 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
1668 if (cols [MONO_TYPEDEF_EXTENDS])
1669 parent = mono_class_get (image, mono_metadata_token_from_dor (cols [MONO_TYPEDEF_EXTENDS]));
1670 interfaces = mono_metadata_interfaces_from_typedef (image, type_token, &icount);
1672 class = g_malloc0 (sizeof (MonoClass));
1674 g_hash_table_insert (image->class_cache, GUINT_TO_POINTER (type_token), class);
1676 class->interfaces = interfaces;
1677 class->interface_count = icount;
1680 class->name_space = nspace;
1682 class->image = image;
1683 class->type_token = type_token;
1684 class->flags = cols [MONO_TYPEDEF_FLAGS];
1686 if ((class->flags & TYPE_ATTRIBUTE_STRING_FORMAT_MASK) == TYPE_ATTRIBUTE_UNICODE_CLASS)
1688 /* fixme: maybe we must set this on windows
1689 if ((class->flags & TYPE_ATTRIBUTE_STRING_FORMAT_MASK) == TYPE_ATTRIBUTE_AUTO_CLASS)
1693 class->cast_class = class->element_class = class;
1695 /*g_print ("Load class %s\n", name);*/
1697 mono_class_setup_parent (class, parent);
1699 mono_class_setup_mono_type (class);
1702 * Compute the field and method lists
1704 class->field.first = cols [MONO_TYPEDEF_FIELD_LIST] - 1;
1705 class->method.first = cols [MONO_TYPEDEF_METHOD_LIST] - 1;
1707 if (tt->rows > tidx){
1708 mono_metadata_decode_row (tt, tidx, cols_next, MONO_TYPEDEF_SIZE);
1709 class->field.last = cols_next [MONO_TYPEDEF_FIELD_LIST] - 1;
1710 class->method.last = cols_next [MONO_TYPEDEF_METHOD_LIST] - 1;
1712 class->field.last = image->tables [MONO_TABLE_FIELD].rows;
1713 class->method.last = image->tables [MONO_TABLE_METHOD].rows;
1716 if (cols [MONO_TYPEDEF_FIELD_LIST] &&
1717 cols [MONO_TYPEDEF_FIELD_LIST] <= image->tables [MONO_TABLE_FIELD].rows)
1718 class->field.count = class->field.last - class->field.first;
1720 class->field.count = 0;
1722 if (cols [MONO_TYPEDEF_METHOD_LIST] <= image->tables [MONO_TABLE_METHOD].rows)
1723 class->method.count = class->method.last - class->method.first;
1725 class->method.count = 0;
1727 /* reserve space to store vector pointer in arrays */
1728 if (!strcmp (nspace, "System") && !strcmp (name, "Array")) {
1729 class->instance_size += 2 * sizeof (gpointer);
1730 g_assert (class->field.count == 0);
1733 if (class->enumtype)
1734 class_compute_field_layout (class);
1736 if ((type_token = mono_metadata_nested_in_typedef (image, type_token)))
1737 class->nested_in = mono_class_create_from_typedef (image, type_token);
1739 class->gen_params = mono_metadata_load_generic_params (image, class->type_token, &icount);
1740 class->num_gen_params = icount;
1742 mono_loader_unlock ();
1748 _mono_class_get_instantiation_name (const char *name, MonoGenericInst *ginst, int offset)
1750 GString *res = g_string_new (name);
1753 MonoClass *argclass;
1755 p = strchr (name, '<');
1756 count = ginst->type_argc - offset;
1758 g_string_truncate (res, (p - name) + 1);
1760 g_string_append_c (res, '<');
1762 for (i = offset; i < ginst->type_argc; ++i) {
1764 g_string_append_c (res, ',');
1765 argclass = mono_class_from_mono_type (ginst->type_argv [i]);
1766 g_string_append (res, argclass->name);
1769 g_string_append_c (res, '>');
1770 return g_string_free (res, FALSE);
1774 mono_class_create_generic (MonoGenericInst *ginst)
1776 MonoClass *klass, *gklass;
1779 ginst->klass = g_malloc0 (sizeof (MonoClass));
1780 klass = ginst->klass;
1782 gklass = mono_class_from_mono_type (ginst->generic_type);
1784 if (ginst->nested_in) {
1785 MonoGenericInst *nginst = ginst->nested_in->data.generic_inst;
1786 int offset = nginst->type_argc;
1788 klass->name = _mono_class_get_instantiation_name (gklass->name, ginst, nginst->type_argc);
1789 } else if (!ginst->init_pending)
1790 klass->name = _mono_class_get_instantiation_name (gklass->name, ginst, 0);
1792 klass->name = gklass->name;
1793 klass->name_space = gklass->name_space;
1794 klass->image = gklass->image;
1795 klass->flags = gklass->flags;
1797 klass->generic_inst = ginst;
1799 klass->this_arg.type = klass->byval_arg.type = MONO_TYPE_GENERICINST;
1800 klass->this_arg.data.generic_inst = klass->byval_arg.data.generic_inst = ginst;
1801 klass->this_arg.byref = TRUE;
1803 klass->cast_class = klass->element_class = klass;
1805 if (ginst->is_dynamic) {
1806 klass->instance_size = gklass->instance_size;
1807 klass->class_size = gklass->class_size;
1808 klass->size_inited = 1;
1810 klass->valuetype = gklass->valuetype;
1817 mono_class_from_generic_parameter (MonoGenericParam *param, MonoImage *image, gboolean is_mvar)
1819 MonoClass *klass, **ptr;
1823 return param->pklass;
1825 klass = param->pklass = g_new0 (MonoClass, 1);
1827 for (count = 0, ptr = param->constraints; ptr && *ptr; ptr++, count++)
1831 if ((count > 0) && !(param->constraints [0]->flags & TYPE_ATTRIBUTE_INTERFACE)) {
1832 klass->parent = param->constraints [0];
1835 klass->parent = mono_defaults.object_class;
1837 if (count - pos > 0) {
1838 klass->interface_count = count - pos;
1839 klass->interfaces = g_new0 (MonoClass *, count - pos);
1840 for (i = pos; i < count; i++)
1841 klass->interfaces [i - pos] = param->constraints [i];
1844 klass->name = g_strdup_printf (is_mvar ? "!!%d" : "!%d", param->num);
1845 klass->name_space = "";
1846 klass->image = image;
1847 klass->cast_class = klass->element_class = klass;
1848 klass->enum_basetype = &klass->element_class->byval_arg;
1849 klass->flags = TYPE_ATTRIBUTE_INTERFACE | TYPE_ATTRIBUTE_PUBLIC;
1851 klass->this_arg.type = klass->byval_arg.type = is_mvar ? MONO_TYPE_MVAR : MONO_TYPE_VAR;
1852 klass->this_arg.data.generic_param = klass->byval_arg.data.generic_param = param;
1853 klass->this_arg.byref = TRUE;
1855 mono_class_init (klass);
1861 my_mono_class_from_generic_parameter (MonoGenericParam *param, gboolean is_mvar)
1866 return param->pklass;
1868 klass = g_new0 (MonoClass, 1);
1870 klass->name = g_strdup_printf (is_mvar ? "!!%d" : "!%d", param->num);
1871 klass->name_space = "";
1872 klass->image = mono_defaults.corlib;
1873 klass->cast_class = klass->element_class = klass;
1874 klass->enum_basetype = &klass->element_class->byval_arg;
1875 klass->flags = TYPE_ATTRIBUTE_INTERFACE | TYPE_ATTRIBUTE_PUBLIC;
1877 klass->this_arg.type = klass->byval_arg.type = is_mvar ? MONO_TYPE_MVAR : MONO_TYPE_VAR;
1878 klass->this_arg.data.generic_param = klass->byval_arg.data.generic_param = param;
1879 klass->this_arg.byref = TRUE;
1881 mono_class_init (klass);
1887 mono_ptr_class_get (MonoType *type)
1890 MonoClass *el_class;
1891 static GHashTable *ptr_hash = NULL;
1893 mono_loader_lock ();
1896 ptr_hash = g_hash_table_new (NULL, NULL);
1897 el_class = mono_class_from_mono_type (type);
1898 if ((result = g_hash_table_lookup (ptr_hash, el_class))) {
1899 mono_loader_unlock ();
1902 result = g_new0 (MonoClass, 1);
1904 result->parent = NULL; /* no parent for PTR types */
1905 result->name = "System";
1906 result->name_space = "MonoPtrFakeClass";
1907 result->image = el_class->image;
1908 result->inited = TRUE;
1909 result->flags = TYPE_ATTRIBUTE_CLASS | (el_class->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK);
1910 /* Can pointers get boxed? */
1911 result->instance_size = sizeof (gpointer);
1912 result->cast_class = result->element_class = el_class;
1913 result->enum_basetype = &result->element_class->byval_arg;
1915 result->this_arg.type = result->byval_arg.type = MONO_TYPE_PTR;
1916 result->this_arg.data.type = result->byval_arg.data.type = result->enum_basetype;
1917 result->this_arg.byref = TRUE;
1919 mono_class_setup_supertypes (result);
1921 g_hash_table_insert (ptr_hash, el_class, result);
1923 mono_loader_unlock ();
1929 mono_fnptr_class_get (MonoMethodSignature *sig)
1932 static GHashTable *ptr_hash = NULL;
1934 mono_loader_lock ();
1937 ptr_hash = g_hash_table_new (NULL, NULL);
1939 if ((result = g_hash_table_lookup (ptr_hash, sig))) {
1940 mono_loader_unlock ();
1943 result = g_new0 (MonoClass, 1);
1945 result->parent = NULL; /* no parent for PTR types */
1946 result->name = "System";
1947 result->name_space = "MonoFNPtrFakeClass";
1948 result->image = NULL; /* need to fix... */
1949 result->inited = TRUE;
1950 result->flags = TYPE_ATTRIBUTE_CLASS; // | (el_class->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK);
1951 /* Can pointers get boxed? */
1952 result->instance_size = sizeof (gpointer);
1953 result->cast_class = result->element_class = result;
1955 result->this_arg.type = result->byval_arg.type = MONO_TYPE_FNPTR;
1956 result->this_arg.data.method = result->byval_arg.data.method = sig;
1957 result->this_arg.byref = TRUE;
1958 result->enum_basetype = &result->element_class->byval_arg;
1960 mono_class_setup_supertypes (result);
1962 g_hash_table_insert (ptr_hash, sig, result);
1964 mono_loader_unlock ();
1970 mono_class_from_mono_type (MonoType *type)
1972 switch (type->type) {
1973 case MONO_TYPE_OBJECT:
1974 return type->data.klass? type->data.klass: mono_defaults.object_class;
1975 case MONO_TYPE_VOID:
1976 return type->data.klass? type->data.klass: mono_defaults.void_class;
1977 case MONO_TYPE_BOOLEAN:
1978 return type->data.klass? type->data.klass: mono_defaults.boolean_class;
1979 case MONO_TYPE_CHAR:
1980 return type->data.klass? type->data.klass: mono_defaults.char_class;
1982 return type->data.klass? type->data.klass: mono_defaults.sbyte_class;
1984 return type->data.klass? type->data.klass: mono_defaults.byte_class;
1986 return type->data.klass? type->data.klass: mono_defaults.int16_class;
1988 return type->data.klass? type->data.klass: mono_defaults.uint16_class;
1990 return type->data.klass? type->data.klass: mono_defaults.int32_class;
1992 return type->data.klass? type->data.klass: mono_defaults.uint32_class;
1994 return type->data.klass? type->data.klass: mono_defaults.int_class;
1996 return type->data.klass? type->data.klass: mono_defaults.uint_class;
1998 return type->data.klass? type->data.klass: mono_defaults.int64_class;
2000 return type->data.klass? type->data.klass: mono_defaults.uint64_class;
2002 return type->data.klass? type->data.klass: mono_defaults.single_class;
2004 return type->data.klass? type->data.klass: mono_defaults.double_class;
2005 case MONO_TYPE_STRING:
2006 return type->data.klass? type->data.klass: mono_defaults.string_class;
2007 case MONO_TYPE_TYPEDBYREF:
2008 return type->data.klass? type->data.klass: mono_defaults.typed_reference_class;
2009 case MONO_TYPE_ARRAY:
2010 return mono_bounded_array_class_get (type->data.array->eklass, type->data.array->rank, TRUE);
2012 return mono_ptr_class_get (type->data.type);
2013 case MONO_TYPE_FNPTR:
2014 return mono_fnptr_class_get (type->data.method);
2015 case MONO_TYPE_SZARRAY:
2016 return mono_array_class_get (type->data.klass, 1);
2017 case MONO_TYPE_CLASS:
2018 case MONO_TYPE_VALUETYPE:
2019 return type->data.klass;
2020 case MONO_TYPE_GENERICINST:
2021 g_assert (type->data.generic_inst->klass);
2022 return type->data.generic_inst->klass;
2024 return my_mono_class_from_generic_parameter (type->data.generic_param, FALSE);
2025 case MONO_TYPE_MVAR:
2026 return my_mono_class_from_generic_parameter (type->data.generic_param, TRUE);
2028 g_warning ("implement me 0x%02x\n", type->type);
2029 g_assert_not_reached ();
2036 * @image: context where the image is created
2037 * @type_spec: typespec token
2040 mono_class_create_from_typespec (MonoImage *image, guint32 type_spec)
2045 type = mono_type_create_from_typespec (image, type_spec);
2047 switch (type->type) {
2048 case MONO_TYPE_ARRAY:
2049 class = mono_array_class_get (type->data.array->eklass, type->data.array->rank);
2051 case MONO_TYPE_SZARRAY:
2052 class = mono_array_class_get (type->data.klass, 1);
2055 class = mono_class_from_mono_type (type->data.type);
2057 case MONO_TYPE_GENERICINST:
2058 g_assert (type->data.generic_inst->klass);
2059 return type->data.generic_inst->klass;
2062 /* it seems any type can be stored in TypeSpec as well */
2063 class = mono_class_from_mono_type (type);
2071 * mono_bounded_array_class_get:
2072 * @element_class: element class
2073 * @rank: the dimension of the array class
2074 * @bounded: whenever the array has non-zero bounds
2076 * Returns: a class object describing the array with element type @element_type and
2080 mono_bounded_array_class_get (MonoClass *eclass, guint32 rank, gboolean bounded)
2084 MonoClass *parent = NULL;
2085 GSList *list, *rootlist;
2088 gboolean corlib_type = FALSE;
2090 g_assert (rank <= 255);
2093 /* bounded only matters for one-dimensional arrays */
2096 image = eclass->image;
2098 mono_loader_lock ();
2100 if ((rootlist = list = g_hash_table_lookup (image->array_cache, eclass))) {
2101 for (; list; list = list->next) {
2103 if ((class->rank == rank) && (class->byval_arg.type == (bounded ? MONO_TYPE_ARRAY : MONO_TYPE_SZARRAY))) {
2104 mono_loader_unlock ();
2110 /* for the building corlib use System.Array from it */
2111 if (image->assembly && image->assembly->dynamic && strcmp (image->assembly_name, "mscorlib") == 0) {
2112 parent = mono_class_from_name (image, "System", "Array");
2115 parent = mono_defaults.array_class;
2116 if (!parent->inited)
2117 mono_class_init (parent);
2120 class = g_malloc0 (sizeof (MonoClass));
2122 class->image = image;
2123 class->name_space = eclass->name_space;
2124 nsize = strlen (eclass->name);
2125 name = g_malloc (nsize + 2 + rank);
2126 memcpy (name, eclass->name, nsize);
2129 memset (name + nsize + 1, ',', rank - 1);
2130 name [nsize + rank] = ']';
2131 name [nsize + rank + 1] = 0;
2133 class->type_token = 0;
2134 /* all arrays are marked serializable and sealed, bug #42779 */
2135 class->flags = TYPE_ATTRIBUTE_CLASS | TYPE_ATTRIBUTE_SERIALIZABLE | TYPE_ATTRIBUTE_SEALED |
2136 (eclass->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK);
2137 class->parent = parent;
2138 class->instance_size = mono_class_instance_size (class->parent);
2139 class->class_size = 0;
2140 mono_class_setup_supertypes (class);
2144 if (eclass->enumtype)
2145 class->cast_class = eclass->element_class;
2147 class->cast_class = eclass;
2149 class->element_class = eclass;
2151 if ((rank > 1) || bounded) {
2152 MonoArrayType *at = g_new0 (MonoArrayType, 1);
2153 class->byval_arg.type = MONO_TYPE_ARRAY;
2154 class->byval_arg.data.array = at;
2155 at->eklass = eclass;
2157 /* FIXME: complete.... */
2159 class->byval_arg.type = MONO_TYPE_SZARRAY;
2160 class->byval_arg.data.klass = eclass;
2162 class->this_arg = class->byval_arg;
2163 class->this_arg.byref = 1;
2168 list = g_slist_append (rootlist, class);
2169 g_hash_table_insert (image->array_cache, eclass, list);
2171 mono_loader_unlock ();
2177 * mono_array_class_get:
2178 * @element_class: element class
2179 * @rank: the dimension of the array class
2181 * Returns: a class object describing the array with element type @element_type and
2185 mono_array_class_get (MonoClass *eclass, guint32 rank)
2187 return mono_bounded_array_class_get (eclass, rank, FALSE);
2191 * mono_class_instance_size:
2194 * Returns: the size of an object instance
2197 mono_class_instance_size (MonoClass *klass)
2199 if (!klass->size_inited)
2200 mono_class_init (klass);
2202 return klass->instance_size;
2206 * mono_class_min_align:
2209 * Returns: minimm alignment requirements
2212 mono_class_min_align (MonoClass *klass)
2214 if (!klass->size_inited)
2215 mono_class_init (klass);
2217 return klass->min_align;
2221 * mono_class_value_size:
2224 * This function is used for value types, and return the
2225 * space and the alignment to store that kind of value object.
2227 * Returns: the size of a value of kind @klass
2230 mono_class_value_size (MonoClass *klass, guint32 *align)
2234 /* fixme: check disable, because we still have external revereces to
2235 * mscorlib and Dummy Objects
2237 /*g_assert (klass->valuetype);*/
2239 size = mono_class_instance_size (klass) - sizeof (MonoObject);
2242 *align = klass->min_align;
2248 * mono_class_data_size:
2251 * Returns: the size of the static class data
2254 mono_class_data_size (MonoClass *klass)
2257 mono_class_init (klass);
2259 return klass->class_size;
2263 * Auxiliary routine to mono_class_get_field
2265 * Takes a field index instead of a field token.
2267 static MonoClassField *
2268 mono_class_get_field_idx (MonoClass *class, int idx)
2270 if (class->field.count){
2271 if ((idx >= class->field.first) && (idx < class->field.last)){
2272 return &class->fields [idx - class->field.first];
2279 return mono_class_get_field_idx (class->parent, idx);
2283 * mono_class_get_field:
2284 * @class: the class to lookup the field.
2285 * @field_token: the field token
2287 * Returns: A MonoClassField representing the type and offset of
2288 * the field, or a NULL value if the field does not belong to this
2292 mono_class_get_field (MonoClass *class, guint32 field_token)
2294 int idx = mono_metadata_token_index (field_token);
2296 g_assert (mono_metadata_token_code (field_token) == MONO_TOKEN_FIELD_DEF);
2298 return mono_class_get_field_idx (class, idx - 1);
2302 mono_class_get_field_from_name (MonoClass *klass, const char *name)
2307 for (i = 0; i < klass->field.count; ++i) {
2308 if (strcmp (name, klass->fields [i].name) == 0)
2309 return &klass->fields [i];
2311 klass = klass->parent;
2317 mono_class_get_property_from_name (MonoClass *klass, const char *name)
2322 for (i = 0; i < klass->property.count; ++i) {
2323 if (strcmp (name, klass->properties [i].name) == 0)
2324 return &klass->properties [i];
2326 klass = klass->parent;
2333 * @image: the image where the class resides
2334 * @type_token: the token for the class
2335 * @at: an optional pointer to return the array element type
2337 * Returns: the MonoClass that represents @type_token in @image
2340 mono_class_get (MonoImage *image, guint32 type_token)
2342 MonoClass *class = NULL;
2345 return mono_lookup_dynamic_token (image, type_token);
2347 switch (type_token & 0xff000000){
2348 case MONO_TOKEN_TYPE_DEF:
2349 class = mono_class_create_from_typedef (image, type_token);
2351 case MONO_TOKEN_TYPE_REF:
2352 class = mono_class_from_typeref (image, type_token);
2354 case MONO_TOKEN_TYPE_SPEC:
2355 class = mono_class_create_from_typespec (image, type_token);
2358 g_warning ("unknown token type %x", type_token & 0xff000000);
2359 g_assert_not_reached ();
2363 g_warning ("Could not load class from token 0x%08x in %s", type_token, image->name);
2369 mono_class_from_name_case (MonoImage *image, const char* name_space, const char *name)
2371 MonoTableInfo *t = &image->tables [MONO_TABLE_TYPEDEF];
2372 guint32 cols [MONO_TYPEDEF_SIZE];
2377 /* add a cache if needed */
2378 for (i = 1; i <= t->rows; ++i) {
2379 mono_metadata_decode_row (t, i - 1, cols, MONO_TYPEDEF_SIZE);
2380 /* nested types are accessed from the nesting name */
2381 visib = cols [MONO_TYPEDEF_FLAGS] & TYPE_ATTRIBUTE_VISIBILITY_MASK;
2382 if (visib > TYPE_ATTRIBUTE_PUBLIC && visib <= TYPE_ATTRIBUTE_NESTED_ASSEMBLY)
2384 n = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
2385 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
2386 if (g_strcasecmp (n, name) == 0 && g_strcasecmp (nspace, name_space) == 0)
2387 return mono_class_get (image, MONO_TOKEN_TYPE_DEF | i);
2393 return_nested_in (MonoClass *class, char *nested) {
2395 char *s = strchr (nested, '/');
2402 for (tmp = class->nested_classes; tmp; tmp = tmp->next) {
2404 if (strcmp (found->name, nested) == 0) {
2406 return return_nested_in (found, s);
2414 mono_class_from_name (MonoImage *image, const char* name_space, const char *name)
2416 GHashTable *nspace_table;
2417 MonoImage *loaded_image;
2423 if ((nested = strchr (name, '/'))) {
2424 int pos = nested - name;
2425 int len = strlen (name);
2428 memcpy (buf, name, len + 1);
2430 nested = buf + pos + 1;
2434 mono_loader_lock ();
2436 nspace_table = g_hash_table_lookup (image->name_cache, name_space);
2439 token = GPOINTER_TO_UINT (g_hash_table_lookup (nspace_table, name));
2441 mono_loader_unlock ();
2446 if (mono_metadata_token_table (token) == MONO_TABLE_EXPORTEDTYPE) {
2447 MonoTableInfo *t = &image->tables [MONO_TABLE_EXPORTEDTYPE];
2448 guint32 cols [MONO_EXP_TYPE_SIZE];
2451 idx = mono_metadata_token_index (token);
2453 mono_metadata_decode_row (t, idx - 1, cols, MONO_EXP_TYPE_SIZE);
2455 impl = cols [MONO_EXP_TYPE_IMPLEMENTATION];
2456 if ((impl & IMPLEMENTATION_MASK) == IMPLEMENTATION_FILE) {
2457 loaded_image = mono_assembly_load_module (image->assembly, impl >> IMPLEMENTATION_BITS);
2460 class = mono_class_from_name (loaded_image, name_space, name);
2462 return return_nested_in (class, nested);
2465 g_error ("not yet implemented");
2469 token = MONO_TOKEN_TYPE_DEF | token;
2471 class = mono_class_get (image, token);
2473 return return_nested_in (class, nested);
2478 mono_class_is_subclass_of (MonoClass *klass, MonoClass *klassc,
2479 gboolean check_interfaces)
2482 if (check_interfaces && (klassc->flags & TYPE_ATTRIBUTE_INTERFACE) && !(klass->flags & TYPE_ATTRIBUTE_INTERFACE)) {
2483 if ((klassc->interface_id <= klass->max_interface_id) &&
2484 (klass->interface_offsets [klassc->interface_id] >= 0))
2486 } else if (check_interfaces && (klassc->flags & TYPE_ATTRIBUTE_INTERFACE) && (klass->flags & TYPE_ATTRIBUTE_INTERFACE)) {
2489 for (i = 0; i < klass->interface_count; i ++) {
2490 MonoClass *ic = klass->interfaces [i];
2495 if (!(klass->flags & TYPE_ATTRIBUTE_INTERFACE) && mono_class_has_parent (klass, klassc))
2497 if (klass->generic_inst) {
2498 MonoType *parent = klass->generic_inst->parent;
2502 if (mono_metadata_type_equal (parent, &klassc->byval_arg))
2504 klass = mono_class_from_mono_type (parent);
2510 * MS.NET thinks interfaces are a subclass of Object, so we think it as
2513 if (klassc == mono_defaults.object_class)
2520 mono_class_is_assignable_from (MonoClass *klass, MonoClass *oklass)
2523 mono_class_init (klass);
2525 if (!oklass->inited)
2526 mono_class_init (oklass);
2528 if (klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
2529 if ((klass->interface_id <= oklass->max_interface_id) &&
2530 (oklass->interface_offsets [klass->interface_id] != -1))
2534 MonoClass *eclass, *eoclass;
2536 if (oklass->rank != klass->rank)
2539 /* vectors vs. one dimensional arrays */
2540 if (oklass->byval_arg.type != klass->byval_arg.type)
2543 eclass = klass->cast_class;
2544 eoclass = oklass->cast_class;
2548 * a is b does not imply a[] is b[] when a is a valuetype, and
2549 * b is a reference type.
2552 if (eoclass->valuetype) {
2553 if ((eclass == mono_defaults.enum_class) ||
2554 (eclass == mono_defaults.enum_class->parent) ||
2555 (eclass == mono_defaults.object_class))
2559 return mono_class_is_assignable_from (klass->cast_class, oklass->cast_class);
2562 if (klass == mono_defaults.object_class)
2565 return mono_class_has_parent (oklass, klass);
2569 * mono_class_needs_cctor_run:
2571 * Determines whenever the class has a static constructor and whenever it
2572 * needs to be called when executing CALLER.
2575 mono_class_needs_cctor_run (MonoClass *klass, MonoMethod *caller)
2580 for (i = 0; i < klass->method.count; ++i) {
2581 method = klass->methods [i];
2582 if ((method->flags & METHOD_ATTRIBUTE_SPECIAL_NAME) &&
2583 (strcmp (".cctor", method->name) == 0)) {
2584 if (caller == method)
2593 * Returns the nnumber of bytes an element of type klass
2594 * uses when stored into an array.
2597 mono_class_array_element_size (MonoClass *klass)
2599 int t = klass->byval_arg.type;
2605 case MONO_TYPE_BOOLEAN:
2609 case MONO_TYPE_CHAR:
2618 case MONO_TYPE_CLASS:
2619 case MONO_TYPE_STRING:
2620 case MONO_TYPE_OBJECT:
2621 case MONO_TYPE_SZARRAY:
2622 case MONO_TYPE_ARRAY:
2624 case MONO_TYPE_MVAR:
2625 return sizeof (gpointer);
2630 case MONO_TYPE_VALUETYPE:
2631 if (klass->enumtype) {
2632 t = klass->enum_basetype->type;
2635 return mono_class_instance_size (klass) - sizeof (MonoObject);
2637 g_error ("unknown type 0x%02x in mono_class_array_element_size", t);
2643 * mono_array_element_size:
2644 * @ac: pointer to a #MonoArrayClass
2646 * Returns: the size of single array element.
2649 mono_array_element_size (MonoClass *ac)
2651 return mono_class_array_element_size (ac->element_class);
2655 mono_ldtoken (MonoImage *image, guint32 token, MonoClass **handle_class)
2657 if (image->dynamic) {
2658 gpointer obj = mono_lookup_dynamic_token (image, token);
2660 switch (token & 0xff000000) {
2661 case MONO_TOKEN_TYPE_DEF:
2662 case MONO_TOKEN_TYPE_REF:
2663 case MONO_TOKEN_TYPE_SPEC:
2665 *handle_class = mono_defaults.typehandle_class;
2666 return &((MonoClass*)obj)->byval_arg;
2667 case MONO_TOKEN_METHOD_DEF:
2669 *handle_class = mono_defaults.methodhandle_class;
2671 case MONO_TOKEN_FIELD_DEF:
2673 *handle_class = mono_defaults.fieldhandle_class;
2676 g_assert_not_reached ();
2680 switch (token & 0xff000000) {
2681 case MONO_TOKEN_TYPE_DEF:
2682 case MONO_TOKEN_TYPE_REF: {
2685 *handle_class = mono_defaults.typehandle_class;
2686 class = mono_class_get (image, token);
2687 mono_class_init (class);
2688 /* We return a MonoType* as handle */
2689 return &class->byval_arg;
2691 case MONO_TOKEN_TYPE_SPEC: {
2694 *handle_class = mono_defaults.typehandle_class;
2695 class = mono_class_create_from_typespec (image, token);
2696 mono_class_init (class);
2697 return &class->byval_arg;
2699 case MONO_TOKEN_FIELD_DEF: {
2701 guint32 type = mono_metadata_typedef_from_field (image, mono_metadata_token_index (token));
2702 class = mono_class_get (image, MONO_TOKEN_TYPE_DEF | type);
2703 mono_class_init (class);
2705 *handle_class = mono_defaults.fieldhandle_class;
2706 return mono_class_get_field (class, token);
2708 case MONO_TOKEN_METHOD_DEF: {
2710 meth = mono_get_method (image, token, NULL);
2712 *handle_class = mono_defaults.methodhandle_class;
2715 case MONO_TOKEN_MEMBER_REF: {
2716 guint32 cols [MONO_MEMBERREF_SIZE];
2718 mono_metadata_decode_row (&image->tables [MONO_TABLE_MEMBERREF], mono_metadata_token_index (token) - 1, cols, MONO_MEMBERREF_SIZE);
2719 sig = mono_metadata_blob_heap (image, cols [MONO_MEMBERREF_SIGNATURE]);
2720 mono_metadata_decode_blob_size (sig, &sig);
2721 if (*sig == 0x6) { /* it's a field */
2723 MonoClassField *field;
2724 field = mono_field_from_token (image, token, &klass);
2726 *handle_class = mono_defaults.fieldhandle_class;
2730 meth = mono_get_method (image, token, NULL);
2732 *handle_class = mono_defaults.methodhandle_class;
2737 g_warning ("Unknown token 0x%08x in ldtoken", token);
2744 * This function might need to call runtime functions so it can't be part
2745 * of the metadata library.
2747 static MonoLookupDynamicToken lookup_dynamic = NULL;
2750 mono_install_lookup_dynamic_token (MonoLookupDynamicToken func)
2752 lookup_dynamic = func;
2756 mono_lookup_dynamic_token (MonoImage *image, guint32 token)
2758 return lookup_dynamic (image, token);