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/metadata-internals.h>
27 #include <mono/metadata/tabledefs.h>
28 #include <mono/metadata/tokentype.h>
29 #include <mono/metadata/class-internals.h>
30 #include <mono/metadata/object.h>
31 #include <mono/metadata/appdomain.h>
32 #include <mono/metadata/mono-endian.h>
33 #include <mono/metadata/debug-helpers.h>
34 #include <mono/metadata/reflection.h>
35 #include <mono/metadata/mono-debug-debugger.h>
36 #include <mono/os/gc_wrapper.h>
40 gboolean mono_print_vtable = FALSE;
42 static MonoClass * mono_class_create_from_typedef (MonoImage *image, guint32 type_token);
43 static void mono_class_create_generic_2 (MonoGenericClass *gclass);
44 static gboolean mono_class_get_cached_class_info (MonoClass *klass, MonoCachedClassInfo *res);
46 void (*mono_debugger_start_class_init_func) (MonoClass *klass) = NULL;
47 void (*mono_debugger_class_init_func) (MonoClass *klass) = NULL;
50 mono_class_from_typeref (MonoImage *image, guint32 type_token)
52 guint32 cols [MONO_TYPEREF_SIZE];
53 MonoTableInfo *t = &image->tables [MONO_TABLE_TYPEREF];
55 const char *name, *nspace;
57 MonoAssembly **references;
59 mono_metadata_decode_row (t, (type_token&0xffffff)-1, cols, MONO_TYPEREF_SIZE);
61 name = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAME]);
62 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAMESPACE]);
64 idx = cols [MONO_TYPEREF_SCOPE] >> MONO_RESOLTION_SCOPE_BITS;
65 switch (cols [MONO_TYPEREF_SCOPE] & MONO_RESOLTION_SCOPE_MASK) {
66 case MONO_RESOLTION_SCOPE_MODULE:
68 g_error ("null ResolutionScope not yet handled");
69 /* a typedef in disguise */
70 return mono_class_from_name (image, nspace, name);
71 case MONO_RESOLTION_SCOPE_MODULEREF:
72 return mono_class_from_name (image->modules [idx - 1], nspace, name);
73 case MONO_RESOLTION_SCOPE_TYPEREF: {
74 MonoClass *enclosing = mono_class_from_typeref (image, MONO_TOKEN_TYPE_REF | idx);
76 mono_class_init (enclosing);
77 for (tmp = enclosing->nested_classes; tmp; tmp = tmp->next) {
79 if (strcmp (res->name, name) == 0)
82 g_warning ("TypeRef ResolutionScope not yet handled (%d)", idx);
85 case MONO_RESOLTION_SCOPE_ASSEMBLYREF:
89 references = image->references;
90 if (!references [idx-1])
91 mono_assembly_load_reference (image, idx - 1);
92 if (references [idx - 1] == (gpointer)-1)
95 image = references [idx-1]->image;
97 return mono_class_from_name (image, nspace, name);
101 dup_type (MonoType* t, const MonoType *original)
103 MonoType *r = g_new0 (MonoType, 1);
105 r->attrs = original->attrs;
106 r->byref = original->byref;
107 mono_stats.generics_metadata_size += sizeof (MonoType);
112 mono_type_get_name_recurse (MonoType *type, GString *str, gboolean is_recursed,
113 gboolean include_ns, gboolean include_arity)
117 switch (type->type) {
118 case MONO_TYPE_ARRAY: {
119 int i, rank = type->data.array->rank;
121 mono_type_get_name_recurse (
122 &type->data.array->eklass->byval_arg, str,
123 FALSE, include_ns, include_arity);
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 (
132 &type->data.klass->byval_arg, str, FALSE, include_ns, include_arity);
133 g_string_append (str, "[]");
136 mono_type_get_name_recurse (type->data.type, str, FALSE, include_ns, include_arity);
137 g_string_append_c (str, '*');
140 klass = mono_class_from_mono_type (type);
141 if (klass->nested_in) {
142 mono_type_get_name_recurse (
143 &klass->nested_in->byval_arg, str, TRUE, include_ns, include_arity);
144 g_string_append_c (str, '+');
146 if (include_ns && *klass->name_space) {
147 g_string_append (str, klass->name_space);
148 g_string_append_c (str, '.');
150 if (!include_arity) {
151 char *s = strchr (klass->name, '`');
152 int len = s ? s - klass->name : strlen (klass->name);
154 g_string_append_len (str, klass->name, len);
156 g_string_append (str, klass->name);
159 if (klass->generic_class) {
160 MonoGenericClass *gclass = klass->generic_class;
163 g_string_append_c (str, '[');
164 for (i = 0; i < gclass->inst->type_argc; i++) {
166 g_string_append_c (str, ',');
167 mono_type_get_name_recurse (
168 gclass->inst->type_argv [i], str, FALSE, include_ns, include_arity);
170 g_string_append_c (str, ']');
171 } else if (klass->generic_container) {
174 g_string_append_c (str, '[');
175 for (i = 0; i < klass->generic_container->type_argc; i++) {
177 g_string_append_c (str, ',');
178 g_string_append (str, klass->generic_container->type_params [i].name);
180 g_string_append_c (str, ']');
187 * mono_type_get_name:
190 * Returns: the string representation for type as required by System.Reflection.
191 * The inverse of mono_reflection_parse_type ().
194 _mono_type_get_name (MonoType *type, gboolean is_recursed, gboolean include_ns,
195 gboolean include_arity)
197 GString* result = g_string_new ("");
198 mono_type_get_name_recurse (type, result, is_recursed, include_ns, include_arity);
201 g_string_append_c (result, '&');
203 return g_string_free (result, FALSE);
207 mono_type_get_name (MonoType *type)
209 return _mono_type_get_name (type, TRUE, TRUE, TRUE);
213 mono_type_get_full_name (MonoType *type)
215 return _mono_type_get_name (type, FALSE, TRUE, TRUE);
219 mono_type_get_underlying_type (MonoType *type)
221 switch (type->type) {
222 case MONO_TYPE_VALUETYPE:
223 if (type->data.klass->enumtype)
224 return type->data.klass->enum_basetype;
226 case MONO_TYPE_GENERICINST:
227 return mono_type_get_underlying_type (&type->data.generic_class->container_class->byval_arg);
236 mono_class_is_open_constructed_type (MonoType *t)
242 case MONO_TYPE_SZARRAY:
243 return mono_class_is_open_constructed_type (&t->data.klass->byval_arg);
244 case MONO_TYPE_ARRAY:
245 return mono_class_is_open_constructed_type (&t->data.array->eklass->byval_arg);
247 return mono_class_is_open_constructed_type (t->data.type);
248 case MONO_TYPE_GENERICINST: {
249 MonoGenericClass *gclass = t->data.generic_class;
252 if (mono_class_is_open_constructed_type (&gclass->container_class->byval_arg))
254 for (i = 0; i < gclass->inst->type_argc; i++)
255 if (mono_class_is_open_constructed_type (gclass->inst->type_argv [i]))
264 static MonoGenericClass *
265 inflate_generic_class (MonoGenericClass *ogclass, MonoGenericContext *context)
267 MonoGenericClass *ngclass, *cached;
269 if (ogclass->is_dynamic) {
270 MonoDynamicGenericClass *dgclass = g_new0 (MonoDynamicGenericClass, 1);
271 ngclass = &dgclass->generic_class;
272 ngclass->is_dynamic = 1;
274 ngclass = g_new0 (MonoGenericClass, 1);
278 ngclass->inst = mono_metadata_inflate_generic_inst (ogclass->inst, context);
280 ngclass->klass = NULL;
282 ngclass->context = g_new0 (MonoGenericContext, 1);
283 ngclass->context->container = context->container;
284 ngclass->context->gclass = ngclass;
287 cached = mono_metadata_lookup_generic_class (ngclass);
288 mono_loader_unlock ();
294 mono_class_create_generic (ngclass);
300 inflate_generic_type (MonoType *type, MonoGenericContext *context)
302 switch (type->type) {
304 if (context->gmethod && context->gmethod->inst->type_argv)
306 context->gmethod->inst->type_argv [type->data.generic_param->num],
313 context->gclass->inst->type_argv [type->data.generic_param->num],
317 case MONO_TYPE_SZARRAY: {
318 MonoClass *eclass = type->data.klass;
319 MonoType *nt, *inflated = inflate_generic_type (&eclass->byval_arg, context);
322 nt = dup_type (type, type);
323 nt->data.klass = mono_class_from_mono_type (inflated);
326 case MONO_TYPE_GENERICINST: {
327 MonoGenericClass *ngclass = inflate_generic_class (type->data.generic_class, context);
328 MonoType *nt = dup_type (type, type);
329 nt->data.generic_class = ngclass;
332 case MONO_TYPE_CLASS:
333 case MONO_TYPE_VALUETYPE: {
334 MonoClass *klass = type->data.klass;
335 MonoGenericClass *gclass;
338 if (!klass->generic_container)
341 gclass = inflate_generic_class (klass->generic_container->context.gclass, context);
343 nt = dup_type (type, type);
344 nt->type = MONO_TYPE_GENERICINST;
345 nt->data.generic_class = gclass;
355 mono_class_inflate_generic_type (MonoType *type, MonoGenericContext *context)
357 MonoType *inflated = inflate_generic_type (type, context);
360 return dup_type (type, type);
362 mono_stats.inflated_type_count++;
367 mono_class_inflate_generic_signature (MonoImage *image, MonoMethodSignature *sig, MonoGenericContext *context)
369 MonoMethodSignature *res;
376 res = mono_metadata_signature_alloc (image, sig->param_count);
377 res->ret = mono_class_inflate_generic_type (sig->ret, context);
378 is_open = mono_class_is_open_constructed_type (res->ret);
379 for (i = 0; i < sig->param_count; ++i) {
380 res->params [i] = mono_class_inflate_generic_type (sig->params [i], context);
382 is_open = mono_class_is_open_constructed_type (res->params [i]);
384 res->hasthis = sig->hasthis;
385 res->explicit_this = sig->explicit_this;
386 res->call_convention = sig->call_convention;
387 res->generic_param_count = sig->generic_param_count;
388 res->sentinelpos = sig->sentinelpos;
389 res->has_type_parameters = is_open;
390 res->is_inflated = 1;
394 static MonoMethodHeader*
395 inflate_generic_header (MonoMethodHeader *header, MonoGenericContext *context)
397 MonoMethodHeader *res;
399 res = g_malloc0 (sizeof (MonoMethodHeader) + sizeof (gpointer) * header->num_locals);
400 res->code = header->code;
401 res->code_size = header->code_size;
402 res->max_stack = header->max_stack;
403 res->num_clauses = header->num_clauses;
404 res->init_locals = header->init_locals;
405 res->num_locals = header->num_locals;
406 res->clauses = header->clauses;
407 for (i = 0; i < header->num_locals; ++i)
408 res->locals [i] = mono_class_inflate_generic_type (header->locals [i], context);
412 static MonoGenericContext *
413 inflate_generic_context (MonoGenericContext *context, MonoGenericContext *inflate_with)
415 MonoGenericContext *res = g_new0 (MonoGenericContext, 1);
417 res->container = context->container;
420 res->gclass = inflate_generic_class (context->gclass, inflate_with);
422 if (context->gmethod) {
423 res->gmethod = g_new0 (MonoGenericMethod, 1);
425 res->gmethod->container = context->gmethod->container;
426 res->gmethod->inst = mono_metadata_inflate_generic_inst (context->gmethod->inst, inflate_with);
428 res->gmethod = inflate_with->gmethod;
434 mono_class_inflate_generic_method (MonoMethod *method, MonoGenericContext *context,
437 MonoMethodInflated *result;
439 if ((method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
440 (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL))
443 if (method->is_inflated || mono_method_signature (method)->is_inflated) {
444 MonoMethodInflated *imethod = (MonoMethodInflated *) method;
446 context = inflate_generic_context (imethod->context, context);
447 method = imethod->declaring;
450 mono_stats.inflated_method_count++;
452 result = g_new0 (MonoMethodInflated, 1);
453 result->nmethod = *(MonoMethodNormal*)method;
454 result->nmethod.method.is_inflated = 1;
455 result->context = context;
456 result->declaring = method;
458 if (result->nmethod.method.klass->generic_class)
459 result->nmethod.method.klass = result->nmethod.method.klass->generic_class->container_class;
461 return (MonoMethod *) result;
465 mono_get_inflated_method (MonoMethod *method)
467 MonoMethodInflated *imethod, *res;
468 MonoMethodHeader *mh;
472 if (!method->is_inflated)
475 imethod = (MonoMethodInflated *) method;
476 if (imethod->inflated)
477 return (MonoMethod *) imethod->inflated;
479 mono_stats.inflated_method_count_2++;
480 mono_stats.generics_metadata_size +=
481 sizeof (MonoMethodInflated) - sizeof (MonoMethodNormal);
483 res = g_new0 (MonoMethodInflated, 1);
485 res->inflated = imethod->inflated = res;
487 mh = mono_method_get_header (method);
489 res->nmethod.header = inflate_generic_header (mh, imethod->context);
491 dtype = mono_class_inflate_generic_type (&method->klass->byval_arg, imethod->context);
492 rklass = res->nmethod.method.klass = mono_class_from_mono_type (dtype);
494 res->nmethod.method.signature = mono_class_inflate_generic_signature (
495 method->klass->image, mono_method_signature (method), imethod->context);
497 return (MonoMethod *) res;
501 * class_compute_field_layout:
502 * @m: pointer to the metadata.
503 * @class: The class to initialize
505 * Initializes the class->fields.
507 * Currently we only support AUTO_LAYOUT, and do not even try to do
508 * a good job at it. This is temporary to get the code for Paolo.
511 class_compute_field_layout (MonoClass *class)
513 MonoImage *m = class->image;
514 const int top = class->field.count;
515 guint32 layout = class->flags & TYPE_ATTRIBUTE_LAYOUT_MASK;
516 MonoTableInfo *t = &m->tables [MONO_TABLE_FIELD];
517 int i, blittable = TRUE, real_size = 0;
519 guint32 packing_size = 0;
520 gboolean explicit_size;
521 MonoClassField *field;
523 if (class->size_inited)
527 if (!class->parent->size_inited)
528 class_compute_field_layout (class->parent);
529 class->instance_size += class->parent->instance_size;
530 class->min_align = class->parent->min_align;
531 /* we use |= since it may have been set already */
532 class->has_references |= class->parent->has_references;
533 blittable = class->parent->blittable;
535 class->instance_size = sizeof (MonoObject);
536 class->min_align = 1;
539 /* Get the real size */
540 explicit_size = mono_metadata_packing_from_typedef (class->image, class->type_token, &packing_size, &real_size);
543 g_assert ((packing_size & 0xfffffff0) == 0);
544 class->packing_size = packing_size;
545 real_size += class->instance_size;
549 if (explicit_size && real_size) {
550 class->instance_size = MAX (real_size, class->instance_size);
552 class->size_inited = 1;
553 class->blittable = blittable;
557 if (layout == TYPE_ATTRIBUTE_AUTO_LAYOUT)
560 class->fields = g_new0 (MonoClassField, top);
563 * Fetch all the field information.
565 for (i = 0; i < top; i++){
567 guint32 cols [MONO_FIELD_SIZE];
568 int idx = class->field.first + i;
569 MonoGenericContainer *container = NULL;
571 field = &class->fields [i];
572 mono_metadata_decode_row (t, idx, cols, MONO_FIELD_SIZE);
573 /* The name is needed for fieldrefs */
574 field->name = mono_metadata_string_heap (m, cols [MONO_FIELD_NAME]);
575 sig = mono_metadata_blob_heap (m, cols [MONO_FIELD_SIGNATURE]);
576 mono_metadata_decode_value (sig, &sig);
577 /* FIELD signature == 0x06 */
578 g_assert (*sig == 0x06);
579 if (class->generic_container)
580 container = class->generic_container;
581 else if (class->generic_class) {
582 container = class->generic_class->container_class->generic_container;
583 g_assert (container);
585 field->type = mono_metadata_parse_type_full (
586 m, (MonoGenericContext *) container, MONO_PARSE_FIELD,
587 cols [MONO_FIELD_FLAGS], sig + 1, &sig);
588 if (mono_field_is_deleted (field))
590 if (class->generic_class) {
591 field->type = mono_class_inflate_generic_type (
592 field->type, class->generic_class->context);
593 field->type->attrs = cols [MONO_FIELD_FLAGS];
596 field->parent = class;
598 /* Only do these checks if we still think this type is blittable */
599 if (blittable && !(field->type->attrs & FIELD_ATTRIBUTE_STATIC)) {
600 if (field->type->byref || MONO_TYPE_IS_REFERENCE (field->type)) {
603 MonoClass *field_class = mono_class_from_mono_type (field->type);
604 if (!field_class || !field_class->blittable)
609 if (layout == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT) {
610 mono_metadata_field_info (m, idx, &field->offset, NULL, NULL);
611 if (field->offset == (guint32)-1 && !(field->type->attrs & FIELD_ATTRIBUTE_STATIC))
612 g_warning ("%s not initialized correctly (missing field layout info for %s)", class->name, field->name);
615 if (cols [MONO_FIELD_FLAGS] & FIELD_ATTRIBUTE_HAS_FIELD_RVA) {
616 mono_metadata_field_info (m, idx, NULL, &rva, NULL);
618 g_warning ("field %s in %s should have RVA data, but hasn't", field->name, class->name);
619 field->data = mono_image_rva_map (class->image, rva);
622 if (class->enumtype && !(cols [MONO_FIELD_FLAGS] & FIELD_ATTRIBUTE_STATIC)) {
623 class->enum_basetype = field->type;
624 class->cast_class = class->element_class = mono_class_from_mono_type (class->enum_basetype);
625 blittable = class->element_class->blittable;
628 /* The def_value of fields is compute lazily during vtable creation */
631 if (class == mono_defaults.string_class)
634 class->blittable = blittable;
636 if (class->enumtype && !class->enum_basetype) {
637 if (!((strcmp (class->name, "Enum") == 0) && (strcmp (class->name_space, "System") == 0)))
640 if (explicit_size && real_size) {
641 class->instance_size = MAX (real_size, class->instance_size);
644 if (class->generic_container ||
645 (class->generic_class && class->generic_class->inst->is_open))
648 mono_class_layout_fields (class);
651 /* useful until we keep track of gc-references in corlib etc. */
652 #define IS_GC_REFERENCE(t) ((t)->type == MONO_TYPE_U || (t)->type == MONO_TYPE_I || (t)->type == MONO_TYPE_PTR)
655 mono_class_layout_fields (MonoClass *class)
658 const int top = class->field.count;
659 guint32 layout = class->flags & TYPE_ATTRIBUTE_LAYOUT_MASK;
660 guint32 pass, passes, real_size;
661 gboolean gc_aware_layout = FALSE;
662 MonoClassField *field;
665 * Enable GC aware auto layout: in this mode, reference
666 * fields are grouped together inside objects, increasing collector
668 * Requires that all classes whose layout is known to native code be annotated
669 * with [StructLayout (LayoutKind.Sequential)]
670 * Value types have gc_aware_layout disabled by default, as per
671 * what the default is for other runtimes.
673 /* corlib is missing [StructLayout] directives in many places */
674 if (layout == TYPE_ATTRIBUTE_AUTO_LAYOUT) {
675 if (class->image != mono_defaults.corlib &&
676 class->byval_arg.type != MONO_TYPE_VALUETYPE)
677 gc_aware_layout = TRUE;
681 * Compute field layout and total size (not considering static fields)
685 case TYPE_ATTRIBUTE_AUTO_LAYOUT:
686 case TYPE_ATTRIBUTE_SEQUENTIAL_LAYOUT:
693 if (layout != TYPE_ATTRIBUTE_AUTO_LAYOUT)
697 real_size = class->parent->instance_size;
699 real_size = sizeof (MonoObject);
701 for (pass = 0; pass < passes; ++pass) {
702 for (i = 0; i < top; i++){
704 field = &class->fields [i];
706 if (mono_field_is_deleted (field))
708 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
711 if (gc_aware_layout) {
713 * We process fields with reference type in the first pass,
714 * and fields with non-reference type in the second pass.
715 * We use IS_POINTER instead of IS_REFERENCE because in
716 * some internal structures, we store GC_MALLOCed memory
717 * in IntPtr fields...
719 if (MONO_TYPE_IS_POINTER (field->type)) {
728 if ((top == 1) && (class->instance_size == sizeof (MonoObject)) &&
729 (strcmp (field->name, "$PRIVATE$") == 0)) {
730 /* This field is a hack inserted by MCS to empty structures */
734 size = mono_type_size (field->type, &align);
736 /* FIXME (LAMESPEC): should we also change the min alignment according to pack? */
737 align = class->packing_size ? MIN (class->packing_size, align): align;
738 class->min_align = MAX (align, class->min_align);
739 field->offset = real_size;
740 field->offset += align - 1;
741 field->offset &= ~(align - 1);
742 real_size = field->offset + size;
743 if (MONO_TYPE_IS_REFERENCE (field->type) || IS_GC_REFERENCE (field->type))
744 class->has_references = TRUE;
747 class->instance_size = MAX (real_size, class->instance_size);
749 if (class->instance_size & (class->min_align - 1)) {
750 class->instance_size += class->min_align - 1;
751 class->instance_size &= ~(class->min_align - 1);
755 case TYPE_ATTRIBUTE_EXPLICIT_LAYOUT:
757 for (i = 0; i < top; i++) {
759 field = &class->fields [i];
762 * There must be info about all the fields in a type if it
763 * uses explicit layout.
766 if (mono_field_is_deleted (field))
768 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
771 if (MONO_TYPE_IS_REFERENCE (field->type) || IS_GC_REFERENCE (field->type))
772 class->has_references = TRUE;
773 size = mono_type_size (field->type, &align);
776 * When we get here, field->offset is already set by the
777 * loader (for either runtime fields or fields loaded from metadata).
778 * The offset is from the start of the object: this works for both
779 * classes and valuetypes.
781 field->offset += sizeof (MonoObject);
786 real_size = MAX (real_size, size + field->offset);
788 class->instance_size = MAX (real_size, class->instance_size);
792 class->size_inited = 1;
795 * Compute static field layout and size
797 for (i = 0; i < top; i++){
799 field = &class->fields [i];
801 if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC) || field->type->attrs & FIELD_ATTRIBUTE_LITERAL)
803 if (mono_field_is_deleted (field))
806 if (MONO_TYPE_IS_REFERENCE (field->type) || IS_GC_REFERENCE (field->type))
807 class->has_static_refs = TRUE;
808 size = mono_type_size (field->type, &align);
809 field->offset = class->class_size;
810 field->offset += align - 1;
811 field->offset &= ~(align - 1);
812 class->class_size = field->offset + size;
817 * mono_class_setup_methods:
819 * Initializes the 'methods' array in the klass.
820 * Calling this method should be avoided if possible since it allocates a lot
821 * of long-living MonoMethod structures.
824 mono_class_setup_methods (MonoClass *class)
827 MonoMethod **methods;
829 if (class->methods || class->generic_class)
834 if (class->methods) {
835 mono_loader_unlock ();
839 //printf ("INIT: %s.%s\n", class->name_space, class->name);
841 if (!class->generic_class && !class->methods) {
842 methods = g_new (MonoMethod*, class->method.count);
843 for (i = 0; i < class->method.count; ++i) {
844 methods [i] = mono_get_method (class->image,
845 MONO_TOKEN_METHOD_DEF | (i + class->method.first + 1), class);
849 if (MONO_CLASS_IS_INTERFACE (class))
850 for (i = 0; i < class->method.count; ++i)
851 methods [i]->slot = i;
853 class->methods = methods;
855 mono_loader_unlock ();
860 mono_class_setup_properties (MonoClass *class)
862 guint startm, endm, i, j;
863 guint32 cols [MONO_PROPERTY_SIZE];
864 MonoTableInfo *pt = &class->image->tables [MONO_TABLE_PROPERTY];
865 MonoTableInfo *msemt = &class->image->tables [MONO_TABLE_METHODSEMANTICS];
866 MonoProperty *properties;
868 if (class->properties)
873 if (class->properties) {
874 mono_loader_unlock ();
878 class->property.first = mono_metadata_properties_from_typedef (class->image, mono_metadata_token_index (class->type_token) - 1, &class->property.last);
879 class->property.count = class->property.last - class->property.first;
881 if (class->property.count)
882 mono_class_setup_methods (class);
884 properties = g_new0 (MonoProperty, class->property.count);
885 for (i = class->property.first; i < class->property.last; ++i) {
886 mono_metadata_decode_row (pt, i, cols, MONO_PROPERTY_SIZE);
887 properties [i - class->property.first].parent = class;
888 properties [i - class->property.first].attrs = cols [MONO_PROPERTY_FLAGS];
889 properties [i - class->property.first].name = mono_metadata_string_heap (class->image, cols [MONO_PROPERTY_NAME]);
891 startm = mono_metadata_methods_from_property (class->image, i, &endm);
892 for (j = startm; j < endm; ++j) {
893 mono_metadata_decode_row (msemt, j, cols, MONO_METHOD_SEMA_SIZE);
894 switch (cols [MONO_METHOD_SEMA_SEMANTICS]) {
895 case METHOD_SEMANTIC_SETTER:
896 properties [i - class->property.first].set = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
898 case METHOD_SEMANTIC_GETTER:
899 properties [i - class->property.first].get = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
907 class->properties = properties;
909 mono_loader_unlock ();
913 mono_class_setup_events (MonoClass *class)
915 guint startm, endm, i, j;
916 guint32 cols [MONO_EVENT_SIZE];
917 MonoTableInfo *pt = &class->image->tables [MONO_TABLE_EVENT];
918 MonoTableInfo *msemt = &class->image->tables [MONO_TABLE_METHODSEMANTICS];
923 class->event.first = mono_metadata_events_from_typedef (class->image, mono_metadata_token_index (class->type_token) - 1, &class->event.last);
924 class->event.count = class->event.last - class->event.first;
926 if (class->event.count)
927 mono_class_setup_methods (class);
929 class->events = g_new0 (MonoEvent, class->event.count);
930 for (i = class->event.first; i < class->event.last; ++i) {
931 MonoEvent *event = &class->events [i - class->event.first];
933 mono_metadata_decode_row (pt, i, cols, MONO_EVENT_SIZE);
934 event->parent = class;
935 event->attrs = cols [MONO_EVENT_FLAGS];
936 event->name = mono_metadata_string_heap (class->image, cols [MONO_EVENT_NAME]);
938 startm = mono_metadata_methods_from_event (class->image, i, &endm);
939 for (j = startm; j < endm; ++j) {
940 mono_metadata_decode_row (msemt, j, cols, MONO_METHOD_SEMA_SIZE);
941 switch (cols [MONO_METHOD_SEMA_SEMANTICS]) {
942 case METHOD_SEMANTIC_ADD_ON:
943 event->add = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
945 case METHOD_SEMANTIC_REMOVE_ON:
946 event->remove = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
948 case METHOD_SEMANTIC_FIRE:
949 event->raise = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
951 case METHOD_SEMANTIC_OTHER: {
954 if (event->other == NULL) {
955 event->other = g_new0 (MonoMethod*, 1);
957 while (event->other [n])
959 event->other = g_realloc (event->other, (n + 1) * sizeof (MonoMethod*));
961 event->other [n] = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
972 mono_get_unique_iid (MonoClass *class)
974 static GHashTable *iid_hash = NULL;
975 static guint iid = 0;
980 g_assert (MONO_CLASS_IS_INTERFACE (class));
985 iid_hash = g_hash_table_new (g_str_hash, g_str_equal);
987 str = g_strdup_printf ("%s|%s.%s\n", class->image->name, class->name_space, class->name);
989 if (g_hash_table_lookup_extended (iid_hash, str, NULL, &value)) {
990 mono_loader_unlock ();
992 return GPOINTER_TO_INT (value);
994 g_hash_table_insert (iid_hash, str, GINT_TO_POINTER (iid));
998 mono_loader_unlock ();
1000 g_assert (iid <= 65535);
1005 collect_implemented_interfaces_aux (MonoClass *klass, GPtrArray **res)
1010 for (i = 0; i < klass->interface_count; i++) {
1011 ic = klass->interfaces [i];
1014 *res = g_ptr_array_new ();
1015 g_ptr_array_add (*res, ic);
1016 mono_class_init (ic);
1018 collect_implemented_interfaces_aux (ic, res);
1023 mono_class_get_implemented_interfaces (MonoClass *klass)
1025 GPtrArray *res = NULL;
1027 collect_implemented_interfaces_aux (klass, &res);
1032 setup_interface_offsets (MonoClass *class, int cur_slot)
1038 /* compute maximum number of slots and maximum interface id */
1040 for (k = class; k ; k = k->parent) {
1041 for (i = 0; i < k->interface_count; i++) {
1042 ic = k->interfaces [i];
1045 mono_class_init (ic);
1047 if (max_iid < ic->interface_id)
1048 max_iid = ic->interface_id;
1050 ifaces = mono_class_get_implemented_interfaces (k);
1052 for (i = 0; i < ifaces->len; ++i) {
1053 ic = g_ptr_array_index (ifaces, i);
1054 if (max_iid < ic->interface_id)
1055 max_iid = ic->interface_id;
1057 g_ptr_array_free (ifaces, TRUE);
1061 if (MONO_CLASS_IS_INTERFACE (class)) {
1062 if (max_iid < class->interface_id)
1063 max_iid = class->interface_id;
1065 class->max_interface_id = max_iid;
1066 /* compute vtable offset for interfaces */
1067 class->interface_offsets = g_malloc (sizeof (gpointer) * (max_iid + 1));
1069 for (i = 0; i <= max_iid; i++)
1070 class->interface_offsets [i] = -1;
1072 ifaces = mono_class_get_implemented_interfaces (class);
1074 for (i = 0; i < ifaces->len; ++i) {
1075 ic = g_ptr_array_index (ifaces, i);
1076 class->interface_offsets [ic->interface_id] = cur_slot;
1077 cur_slot += ic->method.count;
1079 g_ptr_array_free (ifaces, TRUE);
1082 for (k = class->parent; k ; k = k->parent) {
1083 ifaces = mono_class_get_implemented_interfaces (k);
1085 for (i = 0; i < ifaces->len; ++i) {
1086 ic = g_ptr_array_index (ifaces, i);
1088 if (class->interface_offsets [ic->interface_id] == -1) {
1089 int io = k->interface_offsets [ic->interface_id];
1093 class->interface_offsets [ic->interface_id] = io;
1096 g_ptr_array_free (ifaces, TRUE);
1100 if (MONO_CLASS_IS_INTERFACE (class))
1101 class->interface_offsets [class->interface_id] = cur_slot;
1107 mono_class_setup_vtable (MonoClass *class)
1109 MonoMethod **overrides;
1115 mono_class_setup_methods (class);
1117 if (MONO_CLASS_IS_INTERFACE (class))
1120 mono_loader_lock ();
1122 if (class->vtable) {
1123 mono_loader_unlock ();
1127 overrides = mono_class_get_overrides (class->image, class->type_token, &onum);
1128 mono_class_setup_vtable_general (class, overrides, onum);
1131 mono_loader_unlock ();
1135 mono_class_setup_vtable_general (MonoClass *class, MonoMethod **overrides, int onum)
1138 MonoMethod **vtable;
1139 int i, max_vtsize = 0, max_iid, cur_slot = 0;
1141 GHashTable *override_map = NULL;
1146 ifaces = mono_class_get_implemented_interfaces (class);
1148 for (i = 0; i < ifaces->len; i++) {
1149 MonoClass *ic = g_ptr_array_index (ifaces, i);
1150 max_vtsize += ic->method.count;
1152 g_ptr_array_free (ifaces, TRUE);
1155 if (class->parent) {
1156 mono_class_init (class->parent);
1157 mono_class_setup_vtable (class->parent);
1158 max_vtsize += class->parent->vtable_size;
1159 cur_slot = class->parent->vtable_size;
1162 max_vtsize += class->method.count;
1164 vtable = alloca (sizeof (gpointer) * max_vtsize);
1165 memset (vtable, 0, sizeof (gpointer) * max_vtsize);
1167 /* printf ("METAINIT %s.%s\n", class->name_space, class->name); */
1169 cur_slot = setup_interface_offsets (class, cur_slot);
1170 max_iid = class->max_interface_id;
1172 if (class->parent && class->parent->vtable_size)
1173 memcpy (vtable, class->parent->vtable, sizeof (gpointer) * class->parent->vtable_size);
1175 /* override interface methods */
1176 for (i = 0; i < onum; i++) {
1177 MonoMethod *decl = overrides [i*2];
1178 if (MONO_CLASS_IS_INTERFACE (decl->klass)) {
1180 mono_class_setup_methods (decl->klass);
1181 g_assert (decl->slot != -1);
1182 dslot = decl->slot + class->interface_offsets [decl->klass->interface_id];
1183 vtable [dslot] = overrides [i*2 + 1];
1184 vtable [dslot]->slot = dslot;
1186 override_map = g_hash_table_new (mono_aligned_addr_hash, NULL);
1188 g_hash_table_insert (override_map, overrides [i * 2], overrides [i * 2 + 1]);
1192 for (k = class; k ; k = k->parent) {
1194 ifaces = mono_class_get_implemented_interfaces (k);
1196 nifaces = ifaces->len;
1197 for (i = 0; i < nifaces; i++) {
1200 ic = g_ptr_array_index (ifaces, i);
1201 io = k->interface_offsets [ic->interface_id];
1204 g_assert (io <= max_vtsize);
1207 mono_class_setup_methods (ic);
1208 for (l = 0; l < ic->method.count; l++) {
1209 MonoMethod *im = ic->methods [l];
1211 if (vtable [io + l] && !(vtable [io + l]->flags & METHOD_ATTRIBUTE_ABSTRACT))
1214 for (j = 0; j < class->method.count; ++j) {
1215 MonoMethod *cm = class->methods [j];
1216 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
1217 !((cm->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == METHOD_ATTRIBUTE_PUBLIC) ||
1218 !(cm->flags & METHOD_ATTRIBUTE_NEW_SLOT))
1220 if (!strcmp(cm->name, im->name) &&
1221 mono_metadata_signature_equal (mono_method_signature (cm), mono_method_signature (im))) {
1222 g_assert (io + l <= max_vtsize);
1223 vtable [io + l] = cm;
1228 /* already implemented */
1229 if (io >= k->vtable_size)
1233 for (l = 0; l < ic->method.count; l++) {
1234 MonoMethod *im = ic->methods [l];
1237 g_assert (io + l <= max_vtsize);
1239 if (vtable [io + l] && !(vtable [io + l]->flags & METHOD_ATTRIBUTE_ABSTRACT))
1242 for (k1 = class; k1; k1 = k1->parent) {
1243 for (j = 0; j < k1->method.count; ++j) {
1244 MonoMethod *cm = k1->methods [j];
1246 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
1247 !(cm->flags & METHOD_ATTRIBUTE_PUBLIC))
1250 if (!strcmp(cm->name, im->name) &&
1251 mono_metadata_signature_equal (mono_method_signature (cm), mono_method_signature (im))) {
1252 g_assert (io + l <= max_vtsize);
1253 vtable [io + l] = cm;
1258 g_assert (io + l <= max_vtsize);
1259 if (vtable [io + l] && !(vtable [io + l]->flags & METHOD_ATTRIBUTE_ABSTRACT))
1264 for (l = 0; l < ic->method.count; l++) {
1265 MonoMethod *im = ic->methods [l];
1266 char *qname, *fqname, *cname, *the_cname;
1269 if (vtable [io + l])
1272 if (ic->generic_class) {
1273 MonoClass *the_ic = ic->generic_class->container_class;
1274 the_cname = _mono_type_get_name (&the_ic->byval_arg, TRUE, FALSE, TRUE);
1278 cname = (char*)ic->name;
1281 qname = g_strconcat (cname, ".", im->name, NULL);
1282 if (ic->name_space && ic->name_space [0])
1283 fqname = g_strconcat (ic->name_space, ".", cname, ".", im->name, NULL);
1287 for (k1 = class; k1; k1 = k1->parent) {
1288 for (j = 0; j < k1->method.count; ++j) {
1289 MonoMethod *cm = k1->methods [j];
1291 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL))
1294 if (((fqname && !strcmp (cm->name, fqname)) || !strcmp (cm->name, qname)) &&
1295 mono_metadata_signature_equal (mono_method_signature (cm), mono_method_signature (im))) {
1296 g_assert (io + l <= max_vtsize);
1297 vtable [io + l] = cm;
1308 if (!(class->flags & TYPE_ATTRIBUTE_ABSTRACT)) {
1309 for (l = 0; l < ic->method.count; l++) {
1311 MonoMethod *im = ic->methods [l];
1312 if (im->flags & METHOD_ATTRIBUTE_STATIC)
1314 g_assert (io + l <= max_vtsize);
1317 * If one of our parents already implements this interface
1318 * we can inherit the implementation.
1320 if (!(vtable [io + l])) {
1321 MonoClass *parent = class->parent;
1323 if ((ic->interface_id <= parent->max_interface_id) &&
1324 (parent->interface_offsets [ic->interface_id]) &&
1326 vtable [io + l] = parent->vtable [parent->interface_offsets [ic->interface_id] + l];
1329 if (!(vtable [io + l])) {
1330 for (j = 0; j < onum; ++j) {
1331 g_print (" at slot %d: %s (%d) overrides %s (%d)\n", io+l, overrides [j*2+1]->name,
1332 overrides [j*2+1]->slot, overrides [j*2]->name, overrides [j*2]->slot);
1334 msig = mono_signature_get_desc (mono_method_signature (im), FALSE);
1335 printf ("no implementation for interface method %s.%s::%s(%s) in class %s.%s\n",
1336 ic->name_space, ic->name, im->name, msig, class->name_space, class->name);
1338 for (j = 0; j < class->method.count; ++j) {
1339 MonoMethod *cm = class->methods [j];
1340 msig = mono_signature_get_desc (mono_method_signature (cm), FALSE);
1342 printf ("METHOD %s(%s)\n", cm->name, msig);
1345 g_assert_not_reached ();
1350 for (l = 0; l < ic->method.count; l++) {
1351 MonoMethod *im = vtable [io + l];
1354 g_assert (io + l <= max_vtsize);
1356 /* FIXME: why do we need this ? */
1358 /* g_assert_not_reached (); */
1364 g_ptr_array_free (ifaces, TRUE);
1367 for (i = 0; i < class->method.count; ++i) {
1370 cm = class->methods [i];
1373 * Non-virtual method have no place in the vtable.
1374 * This also catches static methods (since they are not virtual).
1376 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL))
1380 * If the method is REUSE_SLOT, we must check in the
1381 * base class for a method to override.
1383 if (!(cm->flags & METHOD_ATTRIBUTE_NEW_SLOT)) {
1385 for (k = class->parent; k ; k = k->parent) {
1387 for (j = 0; j < k->method.count; ++j) {
1388 MonoMethod *m1 = k->methods [j];
1389 if (!(m1->flags & METHOD_ATTRIBUTE_VIRTUAL))
1391 if (!strcmp(cm->name, m1->name) &&
1392 mono_metadata_signature_equal (mono_method_signature (cm), mono_method_signature (m1))) {
1393 slot = k->methods [j]->slot;
1394 g_assert (cm->slot < max_vtsize);
1396 override_map = g_hash_table_new (mono_aligned_addr_hash, NULL);
1397 g_hash_table_insert (override_map, m1, cm);
1409 cm->slot = cur_slot++;
1411 if (!(cm->flags & METHOD_ATTRIBUTE_ABSTRACT) && ! mono_method_signature (cm)->generic_param_count)
1412 vtable [cm->slot] = cm;
1415 /* override non interface methods */
1416 for (i = 0; i < onum; i++) {
1417 MonoMethod *decl = overrides [i*2];
1418 if (!MONO_CLASS_IS_INTERFACE (decl->klass)) {
1419 g_assert (decl->slot != -1);
1420 vtable [decl->slot] = overrides [i*2 + 1];
1421 overrides [i * 2 + 1]->slot = decl->slot;
1423 override_map = g_hash_table_new (mono_aligned_addr_hash, NULL);
1424 g_hash_table_insert (override_map, decl, overrides [i * 2 + 1]);
1429 * If a method occupies more than one place in the vtable, and it is
1430 * overriden, then change the other occurances too.
1433 for (i = 0; i < max_vtsize; ++i)
1435 MonoMethod *cm = g_hash_table_lookup (override_map, vtable [i]);
1440 g_hash_table_destroy (override_map);
1443 if (class->generic_class) {
1444 MonoClass *gklass = class->generic_class->container_class;
1446 mono_class_init (gklass);
1447 class->vtable_size = gklass->vtable_size;
1449 class->vtable_size = cur_slot;
1451 class->vtable = g_malloc0 (sizeof (gpointer) * class->vtable_size);
1452 memcpy (class->vtable, vtable, sizeof (gpointer) * class->vtable_size);
1454 if (mono_print_vtable) {
1457 for (i = 0; i <= max_iid; i++)
1458 if (class->interface_offsets [i] != -1)
1461 printf ("VTable %s.%s (size = %d, interfaces = %d)\n", class->name_space,
1462 class->name, class->vtable_size, icount);
1464 for (i = 0; i < class->vtable_size; ++i) {
1469 printf (" slot %03d(%03d) %s.%s:%s\n", i, cm->slot,
1470 cm->klass->name_space, cm->klass->name,
1477 printf ("Interfaces %s.%s (max_iid = %d)\n", class->name_space,
1478 class->name, max_iid);
1480 for (i = 0; i < class->interface_count; i++) {
1481 ic = class->interfaces [i];
1482 printf (" slot %03d(%03d) %s.%s\n",
1483 class->interface_offsets [ic->interface_id],
1484 ic->method.count, ic->name_space, ic->name);
1487 for (k = class->parent; k ; k = k->parent) {
1488 for (i = 0; i < k->interface_count; i++) {
1489 ic = k->interfaces [i];
1490 printf (" slot %03d(%03d) %s.%s\n",
1491 class->interface_offsets [ic->interface_id],
1492 ic->method.count, ic->name_space, ic->name);
1501 * @class: the class to initialize
1503 * compute the instance_size, class_size and other infos that cannot be
1504 * computed at mono_class_get() time. Also compute a generic vtable and
1505 * the method slot numbers. We use this infos later to create a domain
1509 mono_class_init (MonoClass *class)
1512 static MonoMethod *default_ghc = NULL;
1513 static MonoMethod *default_finalize = NULL;
1514 static int finalize_slot = -1;
1515 static int ghc_slot = -1;
1516 MonoCachedClassInfo cached_info;
1523 /*g_print ("Init class %s\n", class->name);*/
1525 /* We do everything inside the lock to prevent races */
1526 mono_loader_lock ();
1528 if (class->inited) {
1529 mono_loader_unlock ();
1530 /* Somebody might have gotten in before us */
1534 if (class->init_pending) {
1535 /* this indicates a cyclic dependency */
1536 g_error ("pending init %s.%s\n", class->name_space, class->name);
1539 class->init_pending = 1;
1541 if (mono_debugger_start_class_init_func)
1542 mono_debugger_start_class_init_func (class);
1544 mono_stats.initialized_class_count++;
1546 if (class->generic_class && !class->generic_class->is_dynamic) {
1547 MonoGenericClass *gclass = class->generic_class;
1548 MonoClass *gklass = gclass->container_class;
1550 mono_stats.generic_class_count++;
1552 class->method = gklass->method;
1553 class->field = gklass->field;
1555 mono_class_create_generic_2 (gclass);
1557 mono_class_init (gklass);
1558 mono_class_setup_methods (gklass);
1559 mono_class_setup_properties (gklass);
1561 if (MONO_CLASS_IS_INTERFACE (class))
1562 class->interface_id = mono_get_unique_iid (class);
1564 g_assert (class->method.count == gklass->method.count);
1565 class->methods = g_new0 (MonoMethod *, class->method.count);
1567 for (i = 0; i < class->method.count; i++) {
1568 MonoMethod *inflated = mono_class_inflate_generic_method (
1569 gklass->methods [i], gclass->context, gclass->klass);
1571 class->methods [i] = mono_get_inflated_method (inflated);
1574 g_assert (class->field.count == gklass->field.count);
1575 class->fields = g_new0 (MonoClassField, class->field.count);
1577 for (i = 0; i < class->field.count; i++) {
1578 MonoInflatedField *ifield = g_new0 (MonoInflatedField, 1);
1579 ifield->generic_type = gklass->fields [i].type;
1581 class->fields [i] = gklass->fields [i];
1582 class->fields [i].generic_info = ifield;
1583 class->fields [i].parent = class;
1584 class->fields [i].type = mono_class_inflate_generic_type (
1585 class->fields [i].type, gclass->context);
1588 class->property = gklass->property;
1589 class->properties = g_new0 (MonoProperty, class->property.count);
1591 for (i = 0; i < class->property.count; i++) {
1592 MonoProperty *prop = &class->properties [i];
1594 *prop = gklass->properties [i];
1597 prop->get = mono_class_inflate_generic_method (
1598 prop->get, gclass->context, gclass->klass);
1600 prop->set = mono_class_inflate_generic_method (
1601 prop->set, gclass->context, gclass->klass);
1603 prop->parent = class;
1606 g_assert (class->interface_count == gklass->interface_count);
1609 if (class->parent && !class->parent->inited)
1610 mono_class_init (class->parent);
1613 * Computes the size used by the fields, and their locations
1615 if (!class->size_inited)
1616 class_compute_field_layout (class);
1618 /* initialize method pointers */
1621 MonoMethodSignature *sig;
1622 class->method.count = class->rank > 1? 2: 1;
1623 sig = mono_metadata_signature_alloc (class->image, class->rank);
1624 sig->ret = &mono_defaults.void_class->byval_arg;
1625 sig->pinvoke = TRUE;
1626 for (i = 0; i < class->rank; ++i)
1627 sig->params [i] = &mono_defaults.int32_class->byval_arg;
1629 ctor = (MonoMethod *) g_new0 (MonoMethodPInvoke, 1);
1630 ctor->klass = class;
1631 ctor->flags = METHOD_ATTRIBUTE_PUBLIC | METHOD_ATTRIBUTE_RT_SPECIAL_NAME | METHOD_ATTRIBUTE_SPECIAL_NAME;
1632 ctor->iflags = METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL;
1633 ctor->signature = sig;
1634 ctor->name = ".ctor";
1636 class->methods = g_new (MonoMethod*, class->method.count);
1637 class->methods [0] = ctor;
1638 if (class->rank > 1) {
1639 sig = mono_metadata_signature_alloc (class->image, class->rank * 2);
1640 sig->ret = &mono_defaults.void_class->byval_arg;
1641 sig->pinvoke = TRUE;
1642 for (i = 0; i < class->rank * 2; ++i)
1643 sig->params [i] = &mono_defaults.int32_class->byval_arg;
1645 ctor = (MonoMethod *) g_new0 (MonoMethodPInvoke, 1);
1646 ctor->klass = class;
1647 ctor->flags = METHOD_ATTRIBUTE_PUBLIC | METHOD_ATTRIBUTE_RT_SPECIAL_NAME | METHOD_ATTRIBUTE_SPECIAL_NAME;
1648 ctor->iflags = METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL;
1649 ctor->signature = sig;
1650 ctor->name = ".ctor";
1652 class->methods [1] = ctor;
1656 if (!class->generic_class) {
1657 i = mono_metadata_nesting_typedef (class->image, class->type_token, 1);
1660 guint32 cols [MONO_NESTED_CLASS_SIZE];
1661 mono_metadata_decode_row (&class->image->tables [MONO_TABLE_NESTEDCLASS], i - 1, cols, MONO_NESTED_CLASS_SIZE);
1662 nclass = mono_class_create_from_typedef (class->image, MONO_TOKEN_TYPE_DEF | cols [MONO_NESTED_CLASS_NESTED]);
1663 class->nested_classes = g_list_prepend (class->nested_classes, nclass);
1665 i = mono_metadata_nesting_typedef (class->image, class->type_token, i + 1);
1669 mono_class_setup_supertypes (class);
1671 if (MONO_CLASS_IS_INTERFACE (class)) {
1672 class->init_pending = 0;
1675 * class->interface_offsets is needed for the castclass/isinst code, so
1676 * we have to setup them for interfaces, too.
1678 setup_interface_offsets (class, 0);
1679 mono_loader_unlock ();
1681 if (mono_debugger_class_init_func)
1682 mono_debugger_class_init_func (class);
1688 if (class == mono_defaults.object_class) {
1689 mono_class_setup_vtable (class);
1690 for (i = 0; i < class->vtable_size; ++i) {
1691 MonoMethod *cm = class->vtable [i];
1693 if (!strcmp (cm->name, "GetHashCode")) {
1699 g_assert (ghc_slot > 0);
1701 default_ghc = class->vtable [ghc_slot];
1705 if (!default_finalize) {
1706 if (class == mono_defaults.object_class) {
1707 mono_class_setup_vtable (class);
1708 for (i = 0; i < class->vtable_size; ++i) {
1709 MonoMethod *cm = class->vtable [i];
1711 if (!strcmp (cm->name, "Finalize")) {
1717 g_assert (finalize_slot > 0);
1719 default_finalize = class->vtable [finalize_slot];
1724 * If possible, avoid the creation of the generic vtable by requesting
1725 * cached info from the runtime.
1727 if (mono_class_get_cached_class_info (class, &cached_info)) {
1728 guint32 cur_slot = 0;
1730 class->vtable_size = cached_info.vtable_size;
1731 class->has_finalize = cached_info.has_finalize;
1732 class->ghcimpl = cached_info.ghcimpl;
1733 class->has_cctor = cached_info.has_cctor;
1735 if (class->parent) {
1736 mono_class_init (class->parent);
1737 cur_slot = class->parent->vtable_size;
1740 setup_interface_offsets (class, cur_slot);
1743 mono_class_setup_vtable (class);
1746 if (class->parent) {
1747 if (class->vtable [ghc_slot] == default_ghc) {
1752 /* Object::Finalize should have empty implemenatation */
1753 class->has_finalize = 0;
1754 if (class->parent) {
1755 if (class->vtable [finalize_slot] != default_finalize)
1756 class->has_finalize = 1;
1759 for (i = 0; i < class->method.count; ++i) {
1760 MonoMethod *method = class->methods [i];
1761 if ((method->flags & METHOD_ATTRIBUTE_SPECIAL_NAME) &&
1762 (strcmp (".cctor", method->name) == 0)) {
1763 class->has_cctor = 1;
1770 class->init_pending = 0;
1772 mono_loader_unlock ();
1774 if (mono_debugger_class_init_func)
1775 mono_debugger_class_init_func (class);
1779 mono_class_setup_mono_type (MonoClass *class)
1781 const char *name = class->name;
1782 const char *nspace = class->name_space;
1784 if (MONO_CLASS_IS_INTERFACE (class))
1785 class->interface_id = mono_get_unique_iid (class);
1787 class->this_arg.byref = 1;
1788 class->this_arg.data.klass = class;
1789 class->this_arg.type = MONO_TYPE_CLASS;
1790 class->byval_arg.data.klass = class;
1791 class->byval_arg.type = MONO_TYPE_CLASS;
1793 if (!strcmp (nspace, "System")) {
1794 if (!strcmp (name, "ValueType")) {
1796 * do not set the valuetype bit for System.ValueType.
1797 * class->valuetype = 1;
1799 class->blittable = TRUE;
1800 } else if (!strcmp (name, "Enum")) {
1802 * do not set the valuetype bit for System.Enum.
1803 * class->valuetype = 1;
1805 class->valuetype = 0;
1806 class->enumtype = 0;
1807 } else if (!strcmp (name, "Object")) {
1808 class->this_arg.type = class->byval_arg.type = MONO_TYPE_OBJECT;
1809 } else if (!strcmp (name, "String")) {
1810 class->this_arg.type = class->byval_arg.type = MONO_TYPE_STRING;
1811 } else if (!strcmp (name, "TypedReference")) {
1812 class->this_arg.type = class->byval_arg.type = MONO_TYPE_TYPEDBYREF;
1816 if (class->valuetype) {
1817 int t = MONO_TYPE_VALUETYPE;
1818 if (!strcmp (nspace, "System")) {
1821 if (!strcmp (name, "Boolean")) {
1822 t = MONO_TYPE_BOOLEAN;
1823 } else if (!strcmp(name, "Byte")) {
1825 class->blittable = TRUE;
1829 if (!strcmp (name, "Char")) {
1834 if (!strcmp (name, "Double")) {
1836 class->blittable = TRUE;
1840 if (!strcmp (name, "Int32")) {
1842 class->blittable = TRUE;
1843 } else if (!strcmp(name, "Int16")) {
1845 class->blittable = TRUE;
1846 } else if (!strcmp(name, "Int64")) {
1848 class->blittable = TRUE;
1849 } else if (!strcmp(name, "IntPtr")) {
1851 class->blittable = TRUE;
1855 if (!strcmp (name, "Single")) {
1857 class->blittable = TRUE;
1858 } else if (!strcmp(name, "SByte")) {
1860 class->blittable = TRUE;
1864 if (!strcmp (name, "UInt32")) {
1866 class->blittable = TRUE;
1867 } else if (!strcmp(name, "UInt16")) {
1869 class->blittable = TRUE;
1870 } else if (!strcmp(name, "UInt64")) {
1872 class->blittable = TRUE;
1873 } else if (!strcmp(name, "UIntPtr")) {
1875 class->blittable = TRUE;
1879 if (!strcmp (name, "TypedReference")) {
1880 t = MONO_TYPE_TYPEDBYREF;
1881 class->blittable = TRUE;
1885 if (!strcmp (name, "Void")) {
1893 class->this_arg.type = class->byval_arg.type = t;
1898 mono_class_setup_parent (MonoClass *class, MonoClass *parent)
1900 gboolean system_namespace;
1902 system_namespace = !strcmp (class->name_space, "System");
1904 /* if root of the hierarchy */
1905 if (system_namespace && !strcmp (class->name, "Object")) {
1906 class->parent = NULL;
1907 class->instance_size = sizeof (MonoObject);
1910 if (!strcmp (class->name, "<Module>")) {
1911 class->parent = NULL;
1912 class->instance_size = 0;
1916 if (parent && parent->generic_class)
1917 mono_class_create_generic_2 (parent->generic_class);
1919 if (!MONO_CLASS_IS_INTERFACE (class)) {
1920 class->parent = parent;
1923 g_assert_not_reached (); /* FIXME */
1925 if (parent->generic_class && !parent->name) {
1927 * If the parent is a generic instance, we may get
1928 * called before it is fully initialized, especially
1929 * before it has its name.
1934 class->marshalbyref = parent->marshalbyref;
1935 class->contextbound = parent->contextbound;
1936 class->delegate = parent->delegate;
1938 if (system_namespace) {
1939 if (*class->name == 'M' && !strcmp (class->name, "MarshalByRefObject"))
1940 class->marshalbyref = 1;
1942 if (*class->name == 'C' && !strcmp (class->name, "ContextBoundObject"))
1943 class->contextbound = 1;
1945 if (*class->name == 'D' && !strcmp (class->name, "Delegate"))
1946 class->delegate = 1;
1949 if (class->parent->enumtype || ((strcmp (class->parent->name, "ValueType") == 0) &&
1950 (strcmp (class->parent->name_space, "System") == 0)))
1951 class->valuetype = 1;
1952 if (((strcmp (class->parent->name, "Enum") == 0) && (strcmp (class->parent->name_space, "System") == 0))) {
1953 class->valuetype = class->enumtype = 1;
1955 /*class->enumtype = class->parent->enumtype; */
1956 mono_class_setup_supertypes (class);
1958 class->parent = NULL;
1964 mono_class_setup_supertypes (MonoClass *class)
1969 if (class->supertypes)
1973 for (k = class; k ; k = k->parent) {
1977 ms = MAX (MONO_DEFAULT_SUPERTABLE_SIZE, class->idepth);
1978 class->supertypes = g_new0 (MonoClass *, ms);
1980 if (class->parent) {
1981 for (i = class->idepth, k = class; k ; k = k->parent)
1982 class->supertypes [--i] = k;
1984 class->supertypes [0] = class;
1989 * If we inherit a type parameter from an outer class, set its owner to that class.
1992 set_generic_param_owner (MonoGenericContainer *container, MonoClass *klass, int pos)
1994 MonoGenericContainer *gc;
1997 if (klass->nested_in)
1998 pos = set_generic_param_owner (container, klass->nested_in, pos);
2000 if (!klass->generic_container)
2003 gc = klass->generic_container;
2004 for (i = pos; i < gc->type_argc; i++)
2005 container->type_params [i].owner = gc;
2007 return pos + gc->type_argc;
2010 static MonoGenericInst *
2011 get_shared_inst (MonoGenericContainer *container)
2013 MonoGenericInst *nginst;
2016 nginst = g_new0 (MonoGenericInst, 1);
2017 nginst->type_argc = container->type_argc;
2018 nginst->type_argv = g_new0 (MonoType *, nginst->type_argc);
2019 nginst->is_reference = 1;
2020 nginst->is_open = 1;
2022 for (i = 0; i < nginst->type_argc; i++) {
2023 MonoType *t = g_new0 (MonoType, 1);
2025 t->type = container->is_method ? MONO_TYPE_MVAR : MONO_TYPE_VAR;
2026 t->data.generic_param = &container->type_params [i];
2028 nginst->type_argv [i] = t;
2031 return mono_metadata_lookup_generic_inst (nginst);
2035 mono_get_shared_generic_class (MonoGenericContainer *container, gboolean is_dynamic)
2037 MonoGenericClass *gclass;
2040 MonoDynamicGenericClass *dgclass = g_new0 (MonoDynamicGenericClass, 1);
2041 gclass = &dgclass->generic_class;
2042 gclass->is_dynamic = 1;
2044 gclass = g_new0 (MonoGenericClass, 1);
2046 gclass->context = &container->context;
2047 gclass->container_class = container->klass;
2048 gclass->inst = get_shared_inst (container);
2051 MonoGenericClass *cached = mono_metadata_lookup_generic_class (gclass);
2059 gclass->klass = container->klass;
2065 * @image: context where the image is created
2066 * @type_token: typedef token
2069 mono_class_create_from_typedef (MonoImage *image, guint32 type_token)
2071 MonoTableInfo *tt = &image->tables [MONO_TABLE_TYPEDEF];
2072 MonoClass *class, *parent = NULL;
2073 guint32 cols [MONO_TYPEDEF_SIZE];
2074 guint32 cols_next [MONO_TYPEDEF_SIZE];
2075 guint tidx = mono_metadata_token_index (type_token);
2076 MonoGenericContext *context = NULL;
2077 const char *name, *nspace;
2079 MonoClass **interfaces;
2081 mono_loader_lock ();
2083 if ((class = g_hash_table_lookup (image->class_cache, GUINT_TO_POINTER (type_token)))) {
2084 mono_loader_unlock ();
2088 g_assert (mono_metadata_token_table (type_token) == MONO_TABLE_TYPEDEF);
2090 mono_metadata_decode_row (tt, tidx - 1, cols, MONO_TYPEDEF_SIZE);
2092 name = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
2093 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
2095 class = g_malloc0 (sizeof (MonoClass));
2098 class->name_space = nspace;
2100 class->image = image;
2101 class->type_token = type_token;
2102 class->flags = cols [MONO_TYPEDEF_FLAGS];
2104 g_hash_table_insert (image->class_cache, GUINT_TO_POINTER (type_token), class);
2106 class->generic_container = mono_metadata_load_generic_params (image, class->type_token, NULL);
2107 if (class->generic_container) {
2108 class->generic_container->klass = class;
2109 context = &class->generic_container->context;
2111 context->gclass = mono_get_shared_generic_class (context->container, FALSE);
2114 if (cols [MONO_TYPEDEF_EXTENDS])
2115 parent = mono_class_get_full (
2116 image, mono_metadata_token_from_dor (cols [MONO_TYPEDEF_EXTENDS]), context);
2118 mono_class_setup_parent (class, parent);
2120 mono_class_setup_mono_type (class);
2122 interfaces = mono_metadata_interfaces_from_typedef_full (image, type_token, &icount, context);
2124 class->interfaces = interfaces;
2125 class->interface_count = icount;
2127 if ((class->flags & TYPE_ATTRIBUTE_STRING_FORMAT_MASK) == TYPE_ATTRIBUTE_UNICODE_CLASS)
2129 /* fixme: maybe we must set this on windows
2130 if ((class->flags & TYPE_ATTRIBUTE_STRING_FORMAT_MASK) == TYPE_ATTRIBUTE_AUTO_CLASS)
2134 class->cast_class = class->element_class = class;
2136 /*g_print ("Load class %s\n", name);*/
2139 * Compute the field and method lists
2141 class->field.first = cols [MONO_TYPEDEF_FIELD_LIST] - 1;
2142 class->method.first = cols [MONO_TYPEDEF_METHOD_LIST] - 1;
2144 if (tt->rows > tidx){
2145 mono_metadata_decode_row (tt, tidx, cols_next, MONO_TYPEDEF_SIZE);
2146 class->field.last = cols_next [MONO_TYPEDEF_FIELD_LIST] - 1;
2147 class->method.last = cols_next [MONO_TYPEDEF_METHOD_LIST] - 1;
2149 class->field.last = image->tables [MONO_TABLE_FIELD].rows;
2150 class->method.last = image->tables [MONO_TABLE_METHOD].rows;
2153 if (cols [MONO_TYPEDEF_FIELD_LIST] &&
2154 cols [MONO_TYPEDEF_FIELD_LIST] <= image->tables [MONO_TABLE_FIELD].rows)
2155 class->field.count = class->field.last - class->field.first;
2157 class->field.count = 0;
2159 if (cols [MONO_TYPEDEF_METHOD_LIST] <= image->tables [MONO_TABLE_METHOD].rows)
2160 class->method.count = class->method.last - class->method.first;
2162 class->method.count = 0;
2164 /* reserve space to store vector pointer in arrays */
2165 if (!strcmp (nspace, "System") && !strcmp (name, "Array")) {
2166 class->instance_size += 2 * sizeof (gpointer);
2167 g_assert (class->field.count == 0);
2170 if (class->enumtype)
2171 class_compute_field_layout (class);
2173 if ((type_token = mono_metadata_nested_in_typedef (image, type_token)))
2174 class->nested_in = mono_class_create_from_typedef (image, type_token);
2176 if (class->nested_in && class->generic_container)
2177 set_generic_param_owner (class->generic_container, class->nested_in, 0);
2179 mono_loader_unlock ();
2185 mono_class_create_generic (MonoGenericClass *gclass)
2187 MonoClass *klass, *gklass;
2190 gclass->klass = g_malloc0 (sizeof (MonoClass));
2191 klass = gclass->klass;
2193 gklass = gclass->container_class;
2195 klass->nested_in = gklass->nested_in;
2197 klass->name = gklass->name;
2198 klass->name_space = gklass->name_space;
2199 klass->image = gklass->image;
2200 klass->flags = gklass->flags;
2202 klass->generic_class = gclass;
2204 klass->this_arg.type = klass->byval_arg.type = MONO_TYPE_GENERICINST;
2205 klass->this_arg.data.generic_class = klass->byval_arg.data.generic_class = gclass;
2206 klass->this_arg.byref = TRUE;
2208 klass->cast_class = klass->element_class = klass;
2210 if (gclass->is_dynamic) {
2211 klass->instance_size = gklass->instance_size;
2212 klass->class_size = gklass->class_size;
2213 klass->size_inited = 1;
2216 klass->valuetype = gklass->valuetype;
2218 mono_class_setup_supertypes (klass);
2223 mono_class_create_generic_2 (MonoGenericClass *gclass)
2225 MonoClass *klass, *gklass;
2229 klass = gclass->klass;
2230 gklass = gclass->container_class;
2232 klass->interface_count = gklass->interface_count;
2233 klass->interfaces = g_new0 (MonoClass *, klass->interface_count);
2234 for (i = 0; i < klass->interface_count; i++) {
2235 MonoType *it = &gklass->interfaces [i]->byval_arg;
2236 MonoType *inflated = mono_class_inflate_generic_type (it, gclass->context);
2237 klass->interfaces [i] = mono_class_from_mono_type (inflated);
2240 for (list = gklass->nested_classes; list; list = list->next)
2241 klass->nested_classes = g_list_append (
2242 klass->nested_classes, list->data);
2245 klass->parent = mono_class_from_mono_type (gclass->parent);
2246 else if (gklass->parent) {
2247 MonoType *inflated = mono_class_inflate_generic_type (&gklass->parent->byval_arg, gclass->context);
2249 klass->parent = mono_class_from_mono_type (inflated);
2253 mono_class_setup_parent (klass, klass->parent);
2257 mono_class_from_generic_parameter (MonoGenericParam *param, MonoImage *image, gboolean is_mvar)
2259 MonoClass *klass, **ptr;
2263 return param->pklass;
2265 klass = param->pklass = g_new0 (MonoClass, 1);
2267 for (count = 0, ptr = param->constraints; ptr && *ptr; ptr++, count++)
2271 if ((count > 0) && !MONO_CLASS_IS_INTERFACE (param->constraints [0])) {
2272 klass->parent = param->constraints [0];
2276 if (count - pos > 0) {
2277 klass->interface_count = count - pos;
2278 klass->interfaces = g_new0 (MonoClass *, count - pos);
2279 for (i = pos; i < count; i++)
2280 klass->interfaces [i - pos] = param->constraints [i];
2283 g_assert (param->name && param->owner);
2285 klass->name = param->name;
2286 klass->name_space = "";
2287 klass->image = image;
2288 klass->cast_class = klass->element_class = klass;
2289 klass->enum_basetype = &klass->element_class->byval_arg;
2290 klass->flags = TYPE_ATTRIBUTE_PUBLIC;
2292 klass->this_arg.type = klass->byval_arg.type = is_mvar ? MONO_TYPE_MVAR : MONO_TYPE_VAR;
2293 klass->this_arg.data.generic_param = klass->byval_arg.data.generic_param = param;
2294 klass->this_arg.byref = TRUE;
2296 mono_class_init (klass);
2302 my_mono_class_from_generic_parameter (MonoGenericParam *param, gboolean is_mvar)
2307 return param->pklass;
2309 g_assert (param->owner);
2311 klass = g_new0 (MonoClass, 1);
2314 klass->name = param->name;
2316 klass->name = g_strdup_printf (is_mvar ? "!!%d" : "!%d", param->num);
2317 klass->name_space = "";
2318 klass->image = mono_defaults.corlib;
2319 klass->cast_class = klass->element_class = klass;
2320 klass->enum_basetype = &klass->element_class->byval_arg;
2321 klass->flags = TYPE_ATTRIBUTE_PUBLIC;
2323 klass->this_arg.type = klass->byval_arg.type = is_mvar ? MONO_TYPE_MVAR : MONO_TYPE_VAR;
2324 klass->this_arg.data.generic_param = klass->byval_arg.data.generic_param = param;
2325 klass->this_arg.byref = TRUE;
2327 mono_class_init (klass);
2333 mono_ptr_class_get (MonoType *type)
2336 MonoClass *el_class;
2337 static GHashTable *ptr_hash = NULL;
2339 mono_loader_lock ();
2342 ptr_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
2343 el_class = mono_class_from_mono_type (type);
2344 if ((result = g_hash_table_lookup (ptr_hash, el_class))) {
2345 mono_loader_unlock ();
2348 result = g_new0 (MonoClass, 1);
2350 result->parent = NULL; /* no parent for PTR types */
2351 result->name_space = el_class->name_space;
2352 result->name = g_strdup_printf ("%s*", el_class->name);
2353 result->image = el_class->image;
2354 result->inited = TRUE;
2355 result->flags = TYPE_ATTRIBUTE_CLASS | (el_class->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK);
2356 /* Can pointers get boxed? */
2357 result->instance_size = sizeof (gpointer);
2358 result->cast_class = result->element_class = el_class;
2359 result->enum_basetype = &result->element_class->byval_arg;
2360 result->blittable = TRUE;
2362 result->this_arg.type = result->byval_arg.type = MONO_TYPE_PTR;
2363 result->this_arg.data.type = result->byval_arg.data.type = result->enum_basetype;
2364 result->this_arg.byref = TRUE;
2366 mono_class_setup_supertypes (result);
2368 g_hash_table_insert (ptr_hash, el_class, result);
2370 mono_loader_unlock ();
2376 mono_fnptr_class_get (MonoMethodSignature *sig)
2379 static GHashTable *ptr_hash = NULL;
2381 mono_loader_lock ();
2384 ptr_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
2386 if ((result = g_hash_table_lookup (ptr_hash, sig))) {
2387 mono_loader_unlock ();
2390 result = g_new0 (MonoClass, 1);
2392 result->parent = NULL; /* no parent for PTR types */
2393 result->name = "System";
2394 result->name_space = "MonoFNPtrFakeClass";
2395 result->image = NULL; /* need to fix... */
2396 result->inited = TRUE;
2397 result->flags = TYPE_ATTRIBUTE_CLASS; /* | (el_class->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK); */
2398 /* Can pointers get boxed? */
2399 result->instance_size = sizeof (gpointer);
2400 result->cast_class = result->element_class = result;
2401 result->blittable = TRUE;
2403 result->this_arg.type = result->byval_arg.type = MONO_TYPE_FNPTR;
2404 result->this_arg.data.method = result->byval_arg.data.method = sig;
2405 result->this_arg.byref = TRUE;
2406 result->enum_basetype = &result->element_class->byval_arg;
2407 result->blittable = TRUE;
2409 mono_class_setup_supertypes (result);
2411 g_hash_table_insert (ptr_hash, sig, result);
2413 mono_loader_unlock ();
2419 mono_class_from_mono_type (MonoType *type)
2421 switch (type->type) {
2422 case MONO_TYPE_OBJECT:
2423 return type->data.klass? type->data.klass: mono_defaults.object_class;
2424 case MONO_TYPE_VOID:
2425 return type->data.klass? type->data.klass: mono_defaults.void_class;
2426 case MONO_TYPE_BOOLEAN:
2427 return type->data.klass? type->data.klass: mono_defaults.boolean_class;
2428 case MONO_TYPE_CHAR:
2429 return type->data.klass? type->data.klass: mono_defaults.char_class;
2431 return type->data.klass? type->data.klass: mono_defaults.sbyte_class;
2433 return type->data.klass? type->data.klass: mono_defaults.byte_class;
2435 return type->data.klass? type->data.klass: mono_defaults.int16_class;
2437 return type->data.klass? type->data.klass: mono_defaults.uint16_class;
2439 return type->data.klass? type->data.klass: mono_defaults.int32_class;
2441 return type->data.klass? type->data.klass: mono_defaults.uint32_class;
2443 return type->data.klass? type->data.klass: mono_defaults.int_class;
2445 return type->data.klass? type->data.klass: mono_defaults.uint_class;
2447 return type->data.klass? type->data.klass: mono_defaults.int64_class;
2449 return type->data.klass? type->data.klass: mono_defaults.uint64_class;
2451 return type->data.klass? type->data.klass: mono_defaults.single_class;
2453 return type->data.klass? type->data.klass: mono_defaults.double_class;
2454 case MONO_TYPE_STRING:
2455 return type->data.klass? type->data.klass: mono_defaults.string_class;
2456 case MONO_TYPE_TYPEDBYREF:
2457 return type->data.klass? type->data.klass: mono_defaults.typed_reference_class;
2458 case MONO_TYPE_ARRAY:
2459 return mono_bounded_array_class_get (type->data.array->eklass, type->data.array->rank, TRUE);
2461 return mono_ptr_class_get (type->data.type);
2462 case MONO_TYPE_FNPTR:
2463 return mono_fnptr_class_get (type->data.method);
2464 case MONO_TYPE_SZARRAY:
2465 return mono_array_class_get (type->data.klass, 1);
2466 case MONO_TYPE_CLASS:
2467 case MONO_TYPE_VALUETYPE:
2468 return type->data.klass;
2469 case MONO_TYPE_GENERICINST:
2470 g_assert (type->data.generic_class->klass);
2471 return type->data.generic_class->klass;
2473 return my_mono_class_from_generic_parameter (type->data.generic_param, FALSE);
2474 case MONO_TYPE_MVAR:
2475 return my_mono_class_from_generic_parameter (type->data.generic_param, TRUE);
2477 g_warning ("implement me 0x%02x\n", type->type);
2478 g_assert_not_reached ();
2485 * @image: context where the image is created
2486 * @type_spec: typespec token
2489 mono_class_create_from_typespec (MonoImage *image, guint32 type_spec,
2490 MonoGenericContext *context)
2492 MonoType *type, *inflated;
2495 type = mono_type_create_from_typespec_full (image, context, type_spec);
2497 switch (type->type) {
2498 case MONO_TYPE_ARRAY:
2499 class = mono_array_class_get (type->data.array->eklass, type->data.array->rank);
2501 case MONO_TYPE_SZARRAY:
2502 class = mono_array_class_get (type->data.klass, 1);
2505 class = mono_ptr_class_get (type->data.type);
2507 case MONO_TYPE_GENERICINST:
2508 g_assert (type->data.generic_class->klass);
2509 class = type->data.generic_class->klass;
2512 /* it seems any type can be stored in TypeSpec as well */
2513 class = mono_class_from_mono_type (type);
2517 if (!class || !context || (!context->gclass && !context->gmethod))
2520 inflated = mono_class_inflate_generic_type (&class->byval_arg, context);
2522 return mono_class_from_mono_type (inflated);
2526 * mono_bounded_array_class_get:
2527 * @element_class: element class
2528 * @rank: the dimension of the array class
2529 * @bounded: whenever the array has non-zero bounds
2531 * Returns: a class object describing the array with element type @element_type and
2535 mono_bounded_array_class_get (MonoClass *eclass, guint32 rank, gboolean bounded)
2539 MonoClass *parent = NULL;
2540 GSList *list, *rootlist;
2543 gboolean corlib_type = FALSE;
2545 g_assert (rank <= 255);
2548 /* bounded only matters for one-dimensional arrays */
2551 image = eclass->image;
2553 mono_loader_lock ();
2555 if ((rootlist = list = g_hash_table_lookup (image->array_cache, eclass))) {
2556 for (; list; list = list->next) {
2558 if ((class->rank == rank) && (class->byval_arg.type == (bounded ? MONO_TYPE_ARRAY : MONO_TYPE_SZARRAY))) {
2559 mono_loader_unlock ();
2565 /* for the building corlib use System.Array from it */
2566 if (image->assembly && image->assembly->dynamic && strcmp (image->assembly_name, "mscorlib") == 0) {
2567 parent = mono_class_from_name (image, "System", "Array");
2570 parent = mono_defaults.array_class;
2571 if (!parent->inited)
2572 mono_class_init (parent);
2575 class = g_malloc0 (sizeof (MonoClass));
2577 class->image = image;
2578 class->name_space = eclass->name_space;
2579 nsize = strlen (eclass->name);
2580 name = g_malloc (nsize + 2 + rank);
2581 memcpy (name, eclass->name, nsize);
2584 memset (name + nsize + 1, ',', rank - 1);
2585 name [nsize + rank] = ']';
2586 name [nsize + rank + 1] = 0;
2588 class->type_token = 0;
2589 /* all arrays are marked serializable and sealed, bug #42779 */
2590 class->flags = TYPE_ATTRIBUTE_CLASS | TYPE_ATTRIBUTE_SERIALIZABLE | TYPE_ATTRIBUTE_SEALED |
2591 (eclass->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK);
2592 class->parent = parent;
2593 class->instance_size = mono_class_instance_size (class->parent);
2594 class->class_size = 0;
2595 mono_class_setup_supertypes (class);
2596 if (!eclass->size_inited)
2597 class_compute_field_layout (eclass);
2598 class->has_references = MONO_TYPE_IS_REFERENCE (&eclass->byval_arg) || eclass->has_references? TRUE: FALSE;
2602 if (eclass->enumtype)
2603 class->cast_class = eclass->element_class;
2605 class->cast_class = eclass;
2607 class->element_class = eclass;
2609 if ((rank > 1) || bounded) {
2610 MonoArrayType *at = g_new0 (MonoArrayType, 1);
2611 class->byval_arg.type = MONO_TYPE_ARRAY;
2612 class->byval_arg.data.array = at;
2613 at->eklass = eclass;
2615 /* FIXME: complete.... */
2617 class->byval_arg.type = MONO_TYPE_SZARRAY;
2618 class->byval_arg.data.klass = eclass;
2620 class->this_arg = class->byval_arg;
2621 class->this_arg.byref = 1;
2626 list = g_slist_append (rootlist, class);
2627 g_hash_table_insert (image->array_cache, eclass, list);
2629 mono_loader_unlock ();
2635 * mono_array_class_get:
2636 * @element_class: element class
2637 * @rank: the dimension of the array class
2639 * Returns: a class object describing the array with element type @element_type and
2643 mono_array_class_get (MonoClass *eclass, guint32 rank)
2645 return mono_bounded_array_class_get (eclass, rank, FALSE);
2649 * mono_class_instance_size:
2652 * Returns: the size of an object instance
2655 mono_class_instance_size (MonoClass *klass)
2657 if (!klass->size_inited)
2658 mono_class_init (klass);
2660 g_assert (!klass->generic_container &&
2661 (!klass->generic_class || !klass->generic_class->inst->is_open));
2662 return klass->instance_size;
2666 * mono_class_min_align:
2669 * Returns: minimm alignment requirements
2672 mono_class_min_align (MonoClass *klass)
2674 if (!klass->size_inited)
2675 mono_class_init (klass);
2677 return klass->min_align;
2681 * mono_class_value_size:
2684 * This function is used for value types, and return the
2685 * space and the alignment to store that kind of value object.
2687 * Returns: the size of a value of kind @klass
2690 mono_class_value_size (MonoClass *klass, guint32 *align)
2694 /* fixme: check disable, because we still have external revereces to
2695 * mscorlib and Dummy Objects
2697 /*g_assert (klass->valuetype);*/
2699 size = mono_class_instance_size (klass) - sizeof (MonoObject);
2702 *align = klass->min_align;
2708 * mono_class_data_size:
2711 * Returns: the size of the static class data
2714 mono_class_data_size (MonoClass *klass)
2717 mono_class_init (klass);
2719 return klass->class_size;
2723 * Auxiliary routine to mono_class_get_field
2725 * Takes a field index instead of a field token.
2727 static MonoClassField *
2728 mono_class_get_field_idx (MonoClass *class, int idx)
2730 if (class->field.count){
2731 if ((idx >= class->field.first) && (idx < class->field.last)){
2732 return &class->fields [idx - class->field.first];
2739 return mono_class_get_field_idx (class->parent, idx);
2743 * mono_class_get_field:
2744 * @class: the class to lookup the field.
2745 * @field_token: the field token
2747 * Returns: A MonoClassField representing the type and offset of
2748 * the field, or a NULL value if the field does not belong to this
2752 mono_class_get_field (MonoClass *class, guint32 field_token)
2754 int idx = mono_metadata_token_index (field_token);
2756 g_assert (mono_metadata_token_code (field_token) == MONO_TOKEN_FIELD_DEF);
2758 return mono_class_get_field_idx (class, idx - 1);
2762 mono_class_get_field_from_name (MonoClass *klass, const char *name)
2767 for (i = 0; i < klass->field.count; ++i) {
2768 if (strcmp (name, klass->fields [i].name) == 0)
2769 return &klass->fields [i];
2771 klass = klass->parent;
2777 mono_class_get_field_token (MonoClassField *field)
2779 MonoClass *klass = field->parent;
2783 for (i = 0; i < klass->field.count; ++i) {
2784 if (&klass->fields [i] == field)
2785 return mono_metadata_make_token (MONO_TABLE_FIELD, klass->field.first + i + 1);
2787 klass = klass->parent;
2790 g_assert_not_reached ();
2795 mono_class_get_event_token (MonoEvent *event)
2797 MonoClass *klass = event->parent;
2801 for (i = 0; i < klass->event.count; ++i) {
2802 if (&klass->events [i] == event)
2803 return mono_metadata_make_token (MONO_TABLE_EVENT, klass->event.first + i + 1);
2805 klass = klass->parent;
2808 g_assert_not_reached ();
2813 mono_vtable_get_static_field_data (MonoVTable *vt)
2819 mono_class_get_property_from_name (MonoClass *klass, const char *name)
2823 gpointer iter = NULL;
2824 while ((p = mono_class_get_properties (klass, &iter))) {
2825 if (! strcmp (name, p->name))
2828 klass = klass->parent;
2834 mono_class_get_property_token (MonoProperty *prop)
2836 MonoClass *klass = prop->parent;
2840 gpointer iter = NULL;
2841 while ((p = mono_class_get_properties (klass, &iter))) {
2842 if (&klass->properties [i] == prop)
2843 return mono_metadata_make_token (MONO_TABLE_PROPERTY, klass->property.first + i + 1);
2847 klass = klass->parent;
2850 g_assert_not_reached ();
2855 mono_class_name_from_token (MonoImage *image, guint32 type_token, MonoGenericContext *context)
2857 const char *name, *nspace;
2859 return g_strdup_printf ("DynamicType 0x%08x", type_token);
2861 switch (type_token & 0xff000000){
2862 case MONO_TOKEN_TYPE_DEF: {
2863 guint32 cols [MONO_TYPEDEF_SIZE];
2864 MonoTableInfo *tt = &image->tables [MONO_TABLE_TYPEDEF];
2865 guint tidx = mono_metadata_token_index (type_token);
2867 mono_metadata_decode_row (tt, tidx - 1, cols, MONO_TYPEDEF_SIZE);
2868 name = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
2869 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
2870 return g_strdup_printf ("%s.%s", nspace, name);
2873 case MONO_TOKEN_TYPE_REF: {
2874 guint32 cols [MONO_TYPEREF_SIZE];
2875 MonoTableInfo *t = &image->tables [MONO_TABLE_TYPEREF];
2877 mono_metadata_decode_row (t, (type_token&0xffffff)-1, cols, MONO_TYPEREF_SIZE);
2878 name = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAME]);
2879 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAMESPACE]);
2880 return g_strdup_printf ("%s.%s", nspace, name);
2883 case MONO_TOKEN_TYPE_SPEC:
2884 return g_strdup_printf ("Typespec 0x%08x", type_token);
2886 g_assert_not_reached ();
2894 * @image: the image where the class resides
2895 * @type_token: the token for the class
2896 * @at: an optional pointer to return the array element type
2898 * Returns: the MonoClass that represents @type_token in @image
2901 _mono_class_get (MonoImage *image, guint32 type_token, MonoGenericContext *context)
2903 MonoClass *class = NULL;
2906 return mono_lookup_dynamic_token (image, type_token);
2908 switch (type_token & 0xff000000){
2909 case MONO_TOKEN_TYPE_DEF:
2910 class = mono_class_create_from_typedef (image, type_token);
2912 case MONO_TOKEN_TYPE_REF:
2913 class = mono_class_from_typeref (image, type_token);
2915 case MONO_TOKEN_TYPE_SPEC:
2916 class = mono_class_create_from_typespec (image, type_token, context);
2919 g_warning ("unknown token type %x", type_token & 0xff000000);
2920 g_assert_not_reached ();
2924 char *name = mono_class_name_from_token (image, type_token, context);
2925 g_warning ("Could not load class from %s (token 0x%08x) in %s", name, type_token, image->name);
2933 mono_class_get (MonoImage *image, guint32 type_token)
2935 return _mono_class_get (image, type_token, NULL);
2939 mono_class_get_full (MonoImage *image, guint32 type_token, MonoGenericContext *context)
2941 MonoClass *class = _mono_class_get (image, type_token, context);
2944 if (!class || !context || (!context->gclass && !context->gmethod))
2947 switch (class->byval_arg.type) {
2948 case MONO_TYPE_GENERICINST:
2949 if (!class->generic_class->inst->is_open)
2953 case MONO_TYPE_MVAR:
2959 inflated = inflate_generic_type (&class->byval_arg, context);
2963 return mono_class_from_mono_type (inflated);
2967 * mono_class_from_name_case:
2968 * @image: The MonoImage where the type is looked up in, or NULL for looking up in all loaded assemblies
2969 * @name_space: the type namespace
2970 * @name: the type short name.
2972 * Obtains a MonoClass with a given namespace and a given name which
2973 * is located in the given MonoImage. The namespace and name
2974 * lookups are case insensitive.
2976 * You can also pass @NULL to the image, and that will lookup for
2977 * a type with the given namespace and name in all of the loaded
2978 * assemblies: notice that since there might be a name clash in this
2979 * case, passing @NULL is not encouraged if you need a precise type.
2983 mono_class_from_name_case (MonoImage *image, const char* name_space, const char *name)
2985 MonoTableInfo *t = &image->tables [MONO_TABLE_TYPEDEF];
2986 guint32 cols [MONO_TYPEDEF_SIZE];
2991 /* add a cache if needed */
2992 for (i = 1; i <= t->rows; ++i) {
2993 mono_metadata_decode_row (t, i - 1, cols, MONO_TYPEDEF_SIZE);
2994 /* nested types are accessed from the nesting name */
2995 visib = cols [MONO_TYPEDEF_FLAGS] & TYPE_ATTRIBUTE_VISIBILITY_MASK;
2996 if (visib > TYPE_ATTRIBUTE_PUBLIC && visib <= TYPE_ATTRIBUTE_NESTED_ASSEMBLY)
2998 n = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
2999 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
3000 if (g_strcasecmp (n, name) == 0 && g_strcasecmp (nspace, name_space) == 0)
3001 return mono_class_get (image, MONO_TOKEN_TYPE_DEF | i);
3007 return_nested_in (MonoClass *class, char *nested) {
3009 char *s = strchr (nested, '/');
3016 for (tmp = class->nested_classes; tmp; tmp = tmp->next) {
3018 if (strcmp (found->name, nested) == 0) {
3020 return return_nested_in (found, s);
3029 * mono_class_from_name_case:
3030 * @image: The MonoImage where the type is looked up in, or NULL for looking up in all loaded assemblies
3031 * @name_space: the type namespace
3032 * @name: the type short name.
3034 * Obtains a MonoClass with a given namespace and a given name which
3035 * is located in the given MonoImage.
3037 * You can also pass `NULL' to the image, and that will lookup for
3038 * a type with the given namespace and name in all of the loaded
3039 * assemblies: notice that since there might be a name clash in this
3040 * case, passing NULL is not encouraged if you need a precise type.
3044 mono_class_from_name (MonoImage *image, const char* name_space, const char *name)
3046 GHashTable *nspace_table;
3047 MonoImage *loaded_image;
3053 if ((nested = strchr (name, '/'))) {
3054 int pos = nested - name;
3055 int len = strlen (name);
3058 memcpy (buf, name, len + 1);
3060 nested = buf + pos + 1;
3064 mono_loader_lock ();
3066 nspace_table = g_hash_table_lookup (image->name_cache, name_space);
3069 token = GPOINTER_TO_UINT (g_hash_table_lookup (nspace_table, name));
3071 mono_loader_unlock ();
3076 if (mono_metadata_token_table (token) == MONO_TABLE_EXPORTEDTYPE) {
3077 MonoTableInfo *t = &image->tables [MONO_TABLE_EXPORTEDTYPE];
3078 guint32 cols [MONO_EXP_TYPE_SIZE];
3081 idx = mono_metadata_token_index (token);
3083 mono_metadata_decode_row (t, idx - 1, cols, MONO_EXP_TYPE_SIZE);
3085 impl = cols [MONO_EXP_TYPE_IMPLEMENTATION];
3086 if ((impl & MONO_IMPLEMENTATION_MASK) == MONO_IMPLEMENTATION_FILE) {
3087 loaded_image = mono_assembly_load_module (image->assembly, impl >> MONO_IMPLEMENTATION_BITS);
3090 class = mono_class_from_name (loaded_image, name_space, name);
3092 return return_nested_in (class, nested);
3095 g_error ("not yet implemented");
3099 token = MONO_TOKEN_TYPE_DEF | token;
3101 class = mono_class_get (image, token);
3103 return return_nested_in (class, nested);
3108 mono_class_is_subclass_of (MonoClass *klass, MonoClass *klassc,
3109 gboolean check_interfaces)
3112 g_assert (klassc->idepth > 0);
3113 if (check_interfaces && MONO_CLASS_IS_INTERFACE (klassc) && !MONO_CLASS_IS_INTERFACE (klass)) {
3114 if ((klassc->interface_id <= klass->max_interface_id) &&
3115 (klass->interface_offsets [klassc->interface_id] >= 0))
3117 } else if (check_interfaces && MONO_CLASS_IS_INTERFACE (klassc) && MONO_CLASS_IS_INTERFACE (klass)) {
3120 for (i = 0; i < klass->interface_count; i ++) {
3121 MonoClass *ic = klass->interfaces [i];
3126 if (!MONO_CLASS_IS_INTERFACE (klass) && mono_class_has_parent (klass, klassc))
3131 * MS.NET thinks interfaces are a subclass of Object, so we think it as
3134 if (klassc == mono_defaults.object_class)
3137 if (klass->generic_class) {
3138 MonoType *parent = klass->generic_class->parent;
3142 if (mono_metadata_type_equal (parent, &klassc->byval_arg))
3144 klass = mono_class_from_mono_type (parent);
3152 mono_class_is_assignable_from (MonoClass *klass, MonoClass *oklass)
3155 mono_class_init (klass);
3157 if (!oklass->inited)
3158 mono_class_init (oklass);
3160 if (klass->generic_class)
3161 klass = klass->generic_class->container_class;
3163 if (MONO_CLASS_IS_INTERFACE (klass)) {
3164 if ((klass->interface_id <= oklass->max_interface_id) &&
3165 (oklass->interface_offsets [klass->interface_id] != -1))
3169 MonoClass *eclass, *eoclass;
3171 if (oklass->rank != klass->rank)
3174 /* vectors vs. one dimensional arrays */
3175 if (oklass->byval_arg.type != klass->byval_arg.type)
3178 eclass = klass->cast_class;
3179 eoclass = oklass->cast_class;
3183 * a is b does not imply a[] is b[] when a is a valuetype, and
3184 * b is a reference type.
3187 if (eoclass->valuetype) {
3188 if ((eclass == mono_defaults.enum_class) ||
3189 (eclass == mono_defaults.enum_class->parent) ||
3190 (eclass == mono_defaults.object_class))
3194 return mono_class_is_assignable_from (klass->cast_class, oklass->cast_class);
3197 if (klass == mono_defaults.object_class)
3200 return mono_class_has_parent (oklass, klass);
3204 * mono_class_get_cctor:
3206 * Returns the static constructor of @klass if it exists, NULL otherwise.
3209 mono_class_get_cctor (MonoClass *klass)
3211 MonoCachedClassInfo cached_info;
3213 if (!klass->has_cctor)
3216 if (mono_class_get_cached_class_info (klass, &cached_info))
3217 return mono_get_method (klass->image, cached_info.cctor_token, klass);
3219 return mono_class_get_method_from_name_flags (klass, ".cctor", -1, METHOD_ATTRIBUTE_SPECIAL_NAME);
3223 * mono_class_get_finalizer:
3225 * Returns the finalizer method of @klass if it exists, NULL otherwise.
3228 mono_class_get_finalizer (MonoClass *klass)
3230 MonoCachedClassInfo cached_info;
3231 static int finalize_slot = -1;
3233 if (finalize_slot < 0) {
3235 MonoClass* obj_class = mono_get_object_class ();
3236 mono_class_setup_vtable (obj_class);
3237 for (i = 0; i < obj_class->vtable_size; ++i) {
3238 MonoMethod *cm = obj_class->vtable [i];
3240 if (!strcmp (mono_method_get_name (cm), "Finalize")) {
3247 if (!klass->has_finalize)
3250 if (mono_class_get_cached_class_info (klass, &cached_info))
3251 return mono_get_method (cached_info.finalize_image, cached_info.finalize_token, NULL);
3253 mono_class_setup_vtable (klass);
3254 return klass->vtable [finalize_slot];
3259 * mono_class_needs_cctor_run:
3261 * Determines whenever the class has a static constructor and whenever it
3262 * needs to be called when executing CALLER.
3265 mono_class_needs_cctor_run (MonoClass *klass, MonoMethod *caller)
3269 method = mono_class_get_cctor (klass);
3271 return (method == caller) ? FALSE : TRUE;
3277 * mono_class_array_element_size:
3280 * Returns: the number of bytes an element of type @klass
3281 * uses when stored into an array.
3284 mono_class_array_element_size (MonoClass *klass)
3286 MonoType *type = &klass->byval_arg;
3289 switch (type->type) {
3292 case MONO_TYPE_BOOLEAN:
3296 case MONO_TYPE_CHAR:
3305 case MONO_TYPE_CLASS:
3306 case MONO_TYPE_STRING:
3307 case MONO_TYPE_OBJECT:
3308 case MONO_TYPE_SZARRAY:
3309 case MONO_TYPE_ARRAY:
3311 case MONO_TYPE_MVAR:
3312 return sizeof (gpointer);
3317 case MONO_TYPE_VALUETYPE:
3318 if (type->data.klass->enumtype) {
3319 type = type->data.klass->enum_basetype;
3320 klass = klass->element_class;
3323 return mono_class_instance_size (klass) - sizeof (MonoObject);
3324 case MONO_TYPE_GENERICINST:
3325 type = &type->data.generic_class->container_class->byval_arg;
3328 g_error ("unknown type 0x%02x in mono_class_array_element_size", type->type);
3334 * mono_array_element_size:
3335 * @ac: pointer to a #MonoArrayClass
3337 * Returns: the size of single array element.
3340 mono_array_element_size (MonoClass *ac)
3342 return mono_class_array_element_size (ac->element_class);
3346 mono_ldtoken (MonoImage *image, guint32 token, MonoClass **handle_class,
3347 MonoGenericContext *context)
3349 if (image->dynamic) {
3350 gpointer obj = mono_lookup_dynamic_token (image, token);
3352 switch (token & 0xff000000) {
3353 case MONO_TOKEN_TYPE_DEF:
3354 case MONO_TOKEN_TYPE_REF:
3355 case MONO_TOKEN_TYPE_SPEC:
3357 *handle_class = mono_defaults.typehandle_class;
3358 return &((MonoClass*)obj)->byval_arg;
3359 case MONO_TOKEN_METHOD_DEF:
3361 *handle_class = mono_defaults.methodhandle_class;
3363 case MONO_TOKEN_FIELD_DEF:
3365 *handle_class = mono_defaults.fieldhandle_class;
3368 g_assert_not_reached ();
3372 switch (token & 0xff000000) {
3373 case MONO_TOKEN_TYPE_DEF:
3374 case MONO_TOKEN_TYPE_REF: {
3377 *handle_class = mono_defaults.typehandle_class;
3378 class = mono_class_get_full (image, token, context);
3379 mono_class_init (class);
3380 /* We return a MonoType* as handle */
3381 return &class->byval_arg;
3383 case MONO_TOKEN_TYPE_SPEC: {
3386 *handle_class = mono_defaults.typehandle_class;
3387 class = mono_class_create_from_typespec (image, token, context);
3388 mono_class_init (class);
3389 return &class->byval_arg;
3391 case MONO_TOKEN_FIELD_DEF: {
3393 guint32 type = mono_metadata_typedef_from_field (image, mono_metadata_token_index (token));
3394 class = mono_class_get_full (image, MONO_TOKEN_TYPE_DEF | type, context);
3395 mono_class_init (class);
3397 *handle_class = mono_defaults.fieldhandle_class;
3398 return mono_class_get_field (class, token);
3400 case MONO_TOKEN_METHOD_DEF: {
3402 meth = mono_get_method_full (image, token, NULL, context);
3404 *handle_class = mono_defaults.methodhandle_class;
3407 case MONO_TOKEN_MEMBER_REF: {
3408 guint32 cols [MONO_MEMBERREF_SIZE];
3410 mono_metadata_decode_row (&image->tables [MONO_TABLE_MEMBERREF], mono_metadata_token_index (token) - 1, cols, MONO_MEMBERREF_SIZE);
3411 sig = mono_metadata_blob_heap (image, cols [MONO_MEMBERREF_SIGNATURE]);
3412 mono_metadata_decode_blob_size (sig, &sig);
3413 if (*sig == 0x6) { /* it's a field */
3415 MonoClassField *field;
3416 field = mono_field_from_token (image, token, &klass, context);
3418 *handle_class = mono_defaults.fieldhandle_class;
3422 meth = mono_get_method_full (image, token, NULL, context);
3424 *handle_class = mono_defaults.methodhandle_class;
3429 g_warning ("Unknown token 0x%08x in ldtoken", token);
3436 * This function might need to call runtime functions so it can't be part
3437 * of the metadata library.
3439 static MonoLookupDynamicToken lookup_dynamic = NULL;
3442 mono_install_lookup_dynamic_token (MonoLookupDynamicToken func)
3444 lookup_dynamic = func;
3448 mono_lookup_dynamic_token (MonoImage *image, guint32 token)
3450 return lookup_dynamic (image, token);
3453 static MonoGetCachedClassInfo get_cached_class_info = NULL;
3456 mono_install_get_cached_class_info (MonoGetCachedClassInfo func)
3458 get_cached_class_info = func;
3462 mono_class_get_cached_class_info (MonoClass *klass, MonoCachedClassInfo *res)
3464 if (!get_cached_class_info)
3467 return get_cached_class_info (klass, res);
3471 mono_class_get_image (MonoClass *klass)
3473 return klass->image;
3477 * mono_class_get_element_class:
3478 * @klass: the MonoClass to act on
3480 * Returns: the element class of an array or an enumeration.
3483 mono_class_get_element_class (MonoClass *klass)
3485 return klass->element_class;
3489 * mono_class_is_valuetype:
3490 * @klass: the MonoClass to act on
3492 * Returns: true if the MonoClass represents a ValueType.
3495 mono_class_is_valuetype (MonoClass *klass)
3497 return klass->valuetype;
3501 * mono_class_is_enum:
3502 * @klass: the MonoClass to act on
3504 * Returns: true if the MonoClass represents an enumeration.
3507 mono_class_is_enum (MonoClass *klass)
3509 return klass->enumtype;
3513 * mono_class_enum_basetype:
3514 * @klass: the MonoClass to act on
3516 * Returns: the underlying type representation for an enumeration.
3519 mono_class_enum_basetype (MonoClass *klass)
3521 return klass->enum_basetype;
3525 * mono_class_get_parent
3526 * @klass: the MonoClass to act on
3528 * Returns: the parent class for this class.
3531 mono_class_get_parent (MonoClass *klass)
3533 return klass->parent;
3537 * mono_class_get_nesting_type;
3538 * @klass: the MonoClass to act on
3540 * Returns: the container type where this type is nested or NULL if this type is not a nested type.
3543 mono_class_get_nesting_type (MonoClass *klass)
3545 return klass->nested_in;
3549 * mono_class_get_rank:
3550 * @klass: the MonoClass to act on
3552 * Returns: the rank for the array (the number of dimensions).
3555 mono_class_get_rank (MonoClass *klass)
3561 * mono_class_get_flags:
3562 * @klass: the MonoClass to act on
3564 * The type flags from the TypeDef table from the metadata.
3565 * see the TYPE_ATTRIBUTE_* definitions on tabledefs.h for the
3568 * Returns: the flags from the TypeDef table.
3571 mono_class_get_flags (MonoClass *klass)
3573 return klass->flags;
3577 * mono_class_get_name
3578 * @klass: the MonoClass to act on
3580 * Returns: the name of the class.
3583 mono_class_get_name (MonoClass *klass)
3589 * mono_class_get_namespace:
3590 * @klass: the MonoClass to act on
3592 * Returns: the namespace of the class.
3595 mono_class_get_namespace (MonoClass *klass)
3597 return klass->name_space;
3601 * mono_class_get_type:
3602 * @klass: the MonoClass to act on
3604 * This method returns the internal Type representation for the class.
3606 * Returns: the MonoType from the class.
3609 mono_class_get_type (MonoClass *klass)
3611 return &klass->byval_arg;
3615 * mono_class_get_byref_type:
3616 * @klass: the MonoClass to act on
3621 mono_class_get_byref_type (MonoClass *klass)
3623 return &klass->this_arg;
3627 * mono_class_num_fields:
3628 * @klass: the MonoClass to act on
3630 * Returns: the number of static and instance fields in the class.
3633 mono_class_num_fields (MonoClass *klass)
3635 return klass->field.count;
3639 * mono_class_num_methods:
3640 * @klass: the MonoClass to act on
3642 * Returns: the number of methods in the class.
3645 mono_class_num_methods (MonoClass *klass)
3647 return klass->method.count;
3651 * mono_class_num_properties
3652 * @klass: the MonoClass to act on
3654 * Returns: the number of properties in the class.
3657 mono_class_num_properties (MonoClass *klass)
3659 mono_class_setup_properties (klass);
3661 return klass->property.count;
3665 * mono_class_num_events:
3666 * @klass: the MonoClass to act on
3668 * Returns: the number of events in the class.
3671 mono_class_num_events (MonoClass *klass)
3673 mono_class_setup_events (klass);
3675 return klass->event.count;
3679 * mono_class_get_fields:
3680 * @klass: the MonoClass to act on
3682 * This routine is an iterator routine for retrieving the fields in a class.
3684 * You must pass a gpointer that points to zero and is treated as an opaque handle to
3685 * iterate over all of the elements. When no more values are
3686 * available, the return value is NULL.
3688 * Returns: a @MonoClassField* on each iteration, or NULL when no more fields are available.
3691 mono_class_get_fields (MonoClass* klass, gpointer *iter)
3693 MonoClassField* field;
3697 mono_class_init (klass);
3699 /* start from the first */
3700 if (klass->field.count) {
3701 return *iter = &klass->fields [0];
3709 if (field < &klass->fields [klass->field.count]) {
3710 return *iter = field;
3716 * mono_class_get_methods
3717 * @klass: the MonoClass to act on
3719 * This routine is an iterator routine for retrieving the fields in a class.
3721 * You must pass a gpointer that points to zero and is treated as an opaque handle to
3722 * iterate over all of the elements. When no more values are
3723 * available, the return value is NULL.
3725 * Returns: a MonoMethod on each iteration or NULL when no more methods are available.
3728 mono_class_get_methods (MonoClass* klass, gpointer *iter)
3730 MonoMethod** method;
3734 mono_class_init (klass);
3736 mono_class_setup_methods (klass);
3737 /* start from the first */
3738 if (klass->method.count) {
3739 *iter = &klass->methods [0];
3740 return klass->methods [0];
3748 if (method < &klass->methods [klass->method.count]) {
3756 * mono_class_get_properties:
3757 * @klass: the MonoClass to act on
3759 * This routine is an iterator routine for retrieving the properties in a class.
3761 * You must pass a gpointer that points to zero and is treated as an opaque handle to
3762 * iterate over all of the elements. When no more values are
3763 * available, the return value is NULL.
3765 * Returns: a @MonoProperty* on each invocation, or NULL when no more are available.
3768 mono_class_get_properties (MonoClass* klass, gpointer *iter)
3770 MonoProperty* property;
3774 mono_class_init (klass);
3776 mono_class_setup_properties (klass);
3777 /* start from the first */
3778 if (klass->property.count) {
3779 return *iter = &klass->properties [0];
3787 if (property < &klass->properties [klass->property.count]) {
3788 return *iter = property;
3794 * mono_class_get_events:
3795 * @klass: the MonoClass to act on
3797 * This routine is an iterator routine for retrieving the properties in a class.
3799 * You must pass a gpointer that points to zero and is treated as an opaque handle to
3800 * iterate over all of the elements. When no more values are
3801 * available, the return value is NULL.
3803 * Returns: a @MonoEvent* on each invocation, or NULL when no more are available.
3806 mono_class_get_events (MonoClass* klass, gpointer *iter)
3812 mono_class_init (klass);
3814 mono_class_setup_events (klass);
3815 /* start from the first */
3816 if (klass->event.count) {
3817 return *iter = &klass->events [0];
3825 if (event < &klass->events [klass->event.count]) {
3826 return *iter = event;
3832 * mono_class_get_interfaces
3833 * @klass: the MonoClass to act on
3835 * This routine is an iterator routine for retrieving the interfaces implemented by this class.
3837 * You must pass a gpointer that points to zero and is treated as an opaque handle to
3838 * iterate over all of the elements. When no more values are
3839 * available, the return value is NULL.
3841 * Returns: a @Monoclass* on each invocation, or NULL when no more are available.
3844 mono_class_get_interfaces (MonoClass* klass, gpointer *iter)
3850 mono_class_init (klass);
3852 /* start from the first */
3853 if (klass->interface_count) {
3854 *iter = &klass->interfaces [0];
3855 return klass->interfaces [0];
3863 if (iface < &klass->interfaces [klass->interface_count]) {
3871 * mono_class_get_nested_types
3872 * @klass: the MonoClass to act on
3874 * This routine is an iterator routine for retrieving the nested types of a class.
3876 * You must pass a gpointer that points to zero and is treated as an opaque handle to
3877 * iterate over all of the elements. When no more values are
3878 * available, the return value is NULL.
3880 * Returns: a @Monoclass* on each invocation, or NULL when no more are available.
3883 mono_class_get_nested_types (MonoClass* klass, gpointer *iter)
3889 mono_class_init (klass);
3891 /* start from the first */
3892 if (klass->nested_classes) {
3893 *iter = klass->nested_classes;
3894 return klass->nested_classes->data;
3896 /* no nested types */
3910 * mono_field_get_name:
3911 * @field: the MonoClassField to act on
3913 * Returns: the name of the field.
3916 mono_field_get_name (MonoClassField *field)
3922 * mono_field_get_type:
3923 * @field: the MonoClassField to act on
3925 * Returns: MonoType of the field.
3928 mono_field_get_type (MonoClassField *field)
3934 * mono_field_get_type:
3935 * @field: the MonoClassField to act on
3937 * Returns: MonoClass where the field was defined.
3940 mono_field_get_parent (MonoClassField *field)
3942 return field->parent;
3946 * mono_field_get_flags;
3947 * @field: the MonoClassField to act on
3949 * The metadata flags for a field are encoded using the
3950 * FIELD_ATTRIBUTE_* constants. See the tabledefs.h file for details.
3952 * Returns: the flags for the field.
3955 mono_field_get_flags (MonoClassField *field)
3957 return field->type->attrs;
3961 * mono_property_get_name:
3962 * @prop: the MonoProperty to act on
3964 * Returns: the name of the property
3967 mono_property_get_name (MonoProperty *prop)
3973 * mono_property_get_set_method
3974 * @prop: the MonoProperty to act on.
3976 * Returns: the setter method of the property (A MonoMethod)
3979 mono_property_get_set_method (MonoProperty *prop)
3985 * mono_property_get_get_method
3986 * @prop: the MonoProperty to act on.
3988 * Returns: the setter method of the property (A MonoMethod)
3991 mono_property_get_get_method (MonoProperty *prop)
3997 * mono_property_get_parent:
3998 * @prop: the MonoProperty to act on.
4000 * Returns: the MonoClass where the property was defined.
4003 mono_property_get_parent (MonoProperty *prop)
4005 return prop->parent;
4009 * mono_property_get_flags:
4010 * @prop: the MonoProperty to act on.
4012 * The metadata flags for a property are encoded using the
4013 * PROPERTY_ATTRIBUTE_* constants. See the tabledefs.h file for details.
4015 * Returns: the flags for the property.
4018 mono_property_get_flags (MonoProperty *prop)
4024 * mono_event_get_name:
4025 * @event: the MonoEvent to act on
4027 * Returns: the name of the event.
4030 mono_event_get_name (MonoEvent *event)
4036 * mono_event_get_add_method:
4037 * @event: The MonoEvent to act on.
4039 * Returns: the @add' method for the event (a MonoMethod).
4042 mono_event_get_add_method (MonoEvent *event)
4048 * mono_event_get_remove_method:
4049 * @event: The MonoEvent to act on.
4051 * Returns: the @remove method for the event (a MonoMethod).
4054 mono_event_get_remove_method (MonoEvent *event)
4056 return event->remove;
4060 * mono_event_get_raise_method:
4061 * @event: The MonoEvent to act on.
4063 * Returns: the @raise method for the event (a MonoMethod).
4066 mono_event_get_raise_method (MonoEvent *event)
4068 return event->raise;
4072 * mono_event_get_parent:
4073 * @event: the MonoEvent to act on.
4075 * Returns: the MonoClass where the event is defined.
4078 mono_event_get_parent (MonoEvent *event)
4080 return event->parent;
4084 * mono_event_get_flags
4085 * @event: the MonoEvent to act on.
4087 * The metadata flags for an event are encoded using the
4088 * EVENT_* constants. See the tabledefs.h file for details.
4090 * Returns: the flags for the event.
4093 mono_event_get_flags (MonoEvent *event)
4095 return event->attrs;
4099 * mono_class_get_method_from_name:
4100 * @klass: where to look for the method
4101 * @name_space: name of the method
4102 * @param_count: number of parameters. -1 for any number.
4104 * Obtains a MonoMethod with a given name and number of parameters.
4105 * It only works if there are no multiple signatures for any given method name.
4108 mono_class_get_method_from_name (MonoClass *klass, const char *name, int param_count)
4110 return mono_class_get_method_from_name_flags (klass, name, param_count, 0);
4114 * mono_class_get_method_from_name_flags:
4115 * @klass: where to look for the method
4116 * @name_space: name of the method
4117 * @param_count: number of parameters. -1 for any number.
4118 * @flags: flags which must be set in the method
4120 * Obtains a MonoMethod with a given name and number of parameters.
4121 * It only works if there are no multiple signatures for any given method name.
4124 mono_class_get_method_from_name_flags (MonoClass *klass, const char *name, int param_count, int flags)
4126 MonoMethod *res = NULL;
4129 mono_class_init (klass);
4131 if (klass->methods) {
4132 mono_class_setup_methods (klass);
4133 for (i = 0; i < klass->method.count; ++i) {
4134 MonoMethod *method = klass->methods [i];
4136 if (method->name[0] == name [0] &&
4137 !strcmp (name, method->name) &&
4138 (param_count == -1 || mono_method_signature (method)->param_count == param_count) &&
4139 ((method->flags & flags) == flags)) {
4146 /* Search directly in the metadata to avoid calling setup_methods () */
4147 for (i = 0; i < klass->method.count; ++i) {
4148 guint32 cols [MONO_METHOD_SIZE];
4151 mono_metadata_decode_row (&klass->image->tables [MONO_TABLE_METHOD], klass->method.first + i, cols, MONO_METHOD_SIZE);
4153 if (!strcmp (mono_metadata_string_heap (klass->image, cols [MONO_METHOD_NAME]), name)) {
4154 method = mono_get_method (klass->image, MONO_TOKEN_METHOD_DEF | (klass->method.first + i + 1), klass);
4155 if ((param_count == -1) || mono_method_signature (method)->param_count == param_count) {