2004-02-09 Martin Baulig <martin@ximian.com>
[mono.git] / mono / metadata / class.c
index 6a074d4aab3f7a00f79ac42952f82e20ddaf0894..6b8d75728face562a7abdf015799acb7d0a052bf 100644 (file)
@@ -20,6 +20,7 @@
 #include <stdlib.h>
 #include <signal.h>
 #include <mono/metadata/image.h>
+#include <mono/metadata/assembly.h>
 #include <mono/metadata/cil-coff.h>
 #include <mono/metadata/metadata.h>
 #include <mono/metadata/tabledefs.h>
 #include <mono/metadata/appdomain.h>
 #include <mono/metadata/mono-endian.h>
 #include <mono/metadata/debug-helpers.h>
+#include <mono/metadata/reflection.h>
 #include <mono/os/gc_wrapper.h>
 
-#define CSIZE(x) (sizeof (x) / 4)
-
 MonoStats mono_stats;
 
 gboolean mono_print_vtable = FALSE;
@@ -49,6 +49,7 @@ mono_class_from_typeref (MonoImage *image, guint32 type_token)
        guint32 idx;
        const char *name, *nspace;
        MonoClass *res;
+       MonoAssembly **references;
 
        mono_metadata_decode_row (t, (type_token&0xffffff)-1, cols, MONO_TYPEREF_SIZE);
 
@@ -80,7 +81,8 @@ mono_class_from_typeref (MonoImage *image, guint32 type_token)
                break;
        }
 
-       if (!image->references ||  !image->references [idx-1]) {
+       references = image->references;
+       if (!references ||  !references [idx-1]) {
                /* 
                 * detected a reference to mscorlib, we simply return a reference to a dummy 
                 * until we have a better solution.
@@ -96,78 +98,227 @@ mono_class_from_typeref (MonoImage *image, guint32 type_token)
        }       
 
        /* load referenced assembly */
-       image = image->references [idx-1]->image;
+       image = references [idx-1]->image;
 
        return mono_class_from_name (image, nspace, name);
 }
 
-MonoMarshalType *
-mono_marshal_load_type_info (MonoClass* klass)
+static MonoType*
+dup_type (MonoType* t)
 {
-       int i, j, count = 0;
-       MonoMarshalType *info;
-       guint32 layout;
+       MonoType *r = g_new0 (MonoType, 1);
+       *r = *t;
+       return r;
+}
 
-       g_assert (klass != NULL);
+static void
+mono_type_get_name_recurse (MonoType *type, GString *str)
+{
+       MonoClass *klass;
+       
+       switch (type->type) {
+       case MONO_TYPE_ARRAY: {
+               int i, rank = type->data.array->rank;
+
+               mono_type_get_name_recurse (&type->data.array->eklass->byval_arg, str);
+               g_string_append_c (str, '[');
+               for (i = 1; i < rank; i++)
+                       g_string_append_c (str, ',');
+               g_string_append_c (str, ']');
+               break;
+       }
+       case MONO_TYPE_SZARRAY:
+               mono_type_get_name_recurse (&type->data.klass->byval_arg, str);
+               g_string_append (str, "[]");
+               break;
+       case MONO_TYPE_PTR:
+               mono_type_get_name_recurse (type->data.type, str);
+               g_string_append_c (str, '*');
+               break;
+       default:
+               klass = mono_class_from_mono_type (type);
+               if (klass->nested_in) {
+                       mono_type_get_name_recurse (&klass->nested_in->byval_arg, str);
+                       g_string_append_c (str, '+');
+               }
+               if (*klass->name_space) {
+                       g_string_append (str, klass->name_space);
+                       g_string_append_c (str, '.');
+               }
+               g_string_append (str, klass->name);
+               break;
+       }
+}
+
+/*
+ * mono_type_get_name:
+ * @type: a type
+ *
+ * Returns the string representation for type as required by System.Reflection.
+ * The inverse of mono_reflection_parse_type ().
+ */
+char*
+mono_type_get_name (MonoType *type)
+{
+       GString* result = g_string_new ("");
+       mono_type_get_name_recurse (type, result);
 
-       if (klass->marshal_info)
-               return klass->marshal_info;
+       if (type->byref)
+               g_string_append_c (result, '&');
 
-       if (!klass->inited)
-               mono_class_init (klass);
-       
-       for (i = 0; i < klass->field.count; ++i) {
-               if (klass->fields [i].type->attrs & FIELD_ATTRIBUTE_STATIC)
-                       continue;
-               count++;
+       return g_string_free (result, FALSE);
+}
+
+gboolean
+mono_class_is_open_constructed_type (MonoType *t)
+{
+       switch (t->type) {
+       case MONO_TYPE_VAR:
+       case MONO_TYPE_MVAR:
+               return TRUE;
+       case MONO_TYPE_SZARRAY:
+               return mono_class_is_open_constructed_type (&t->data.klass->byval_arg);
+       case MONO_TYPE_ARRAY:
+               return mono_class_is_open_constructed_type (&t->data.array->eklass->byval_arg);
+       case MONO_TYPE_PTR:
+               return mono_class_is_open_constructed_type (t->data.type);
+       case MONO_TYPE_GENERICINST: {
+               MonoGenericInst *ginst = t->data.generic_inst;
+               int i;
+
+               if (mono_class_is_open_constructed_type (ginst->generic_type))
+                       return TRUE;
+               for (i = 0; i < ginst->type_argc; i++)
+                       if (mono_class_is_open_constructed_type (ginst->type_argv [i]))
+                               return TRUE;
+               return FALSE;
        }
+       default:
+               return FALSE;
+       }
+}
 
-       layout = klass->flags & TYPE_ATTRIBUTE_LAYOUT_MASK;
+MonoType*
+mono_class_inflate_generic_type (MonoType *type, MonoGenericInst *ginst,
+                                MonoGenericMethod *gmethod)
+{
+       switch (type->type) {
+       case MONO_TYPE_MVAR:
+               if (gmethod && gmethod->mtype_argv)
+                       return dup_type (gmethod->mtype_argv [type->data.generic_param->num]);
+               else
+                       return type;
+       case MONO_TYPE_VAR:
+               if (ginst) {
+                       MonoType *t = ginst->type_argv [type->data.generic_param->num];
+
+                       if ((t->type == MONO_TYPE_VAR) || (t->type == MONO_TYPE_MVAR))
+                               return type;
+                       else
+                               return dup_type (t);
+               } else
+                       return type;
+       case MONO_TYPE_SZARRAY: {
+               MonoClass *eclass = type->data.klass;
+               MonoClass *nclass;
+               MonoType *nt;
+               if ((eclass->byval_arg.type == MONO_TYPE_MVAR) && gmethod) {
+                       nclass = mono_class_from_mono_type (gmethod->mtype_argv [eclass->byval_arg.data.generic_param->num]);
+               } else if ((eclass->byval_arg.type == MONO_TYPE_VAR) && ginst) {
+                       nclass = mono_class_from_mono_type (ginst->type_argv [eclass->byval_arg.data.generic_param->num]);
+               } else {
+                       return type;
+               }
+               nt = dup_type (type);
+               nt->data.klass = nclass;
+               return nt;
+       }
+       case MONO_TYPE_GENERICINST: {
+               MonoGenericInst *oginst = type->data.generic_inst;
+               MonoGenericInst *nginst;
+               MonoType *nt;
+               int i;
 
-       klass->marshal_info = info = g_malloc0 (sizeof (MonoMarshalType) + sizeof (MonoMarshalField) * count);
-       info->num_fields = count;
-       
-       if (klass->parent)
-               info->native_size = mono_class_native_size (klass->parent, NULL);
+               nginst = g_new0 (MonoGenericInst, 1);
+               *nginst = *oginst;
 
-       for (j = i = 0; i < klass->field.count; ++i) {
-               int size, align;
-               
-               if (klass->fields [i].type->attrs & FIELD_ATTRIBUTE_STATIC)
-                       continue;
+               nginst->type_argv = g_new0 (MonoType *, oginst->type_argc);
+
+               for (i = 0; i < oginst->type_argc; i++) {
+                       MonoType *t = oginst->type_argv [i];
+                       nginst->type_argv [i] = mono_class_inflate_generic_type (t, ginst, gmethod);
+               };
+
+               nt = dup_type (type);
+               nt->data.generic_inst = nginst;
+               return nt;
+       }
+       default:
+               return type;
+       }
+       return type;
+}
 
-               if (klass->fields [i].type->attrs & FIELD_ATTRIBUTE_HAS_FIELD_MARSHAL)
-                       mono_metadata_field_info (klass->image, klass->field.first + i, 
-                                                 NULL, NULL, &info->fields [j].mspec);
-
-               info->fields [j].field = &klass->fields [i];
-
-               switch (layout) {
-               case TYPE_ATTRIBUTE_AUTO_LAYOUT:
-               case TYPE_ATTRIBUTE_SEQUENTIAL_LAYOUT:
-                       size = mono_marshal_type_size (klass->fields [i].type, info->fields [j].mspec, 
-                                                      &align, TRUE, klass->unicode);
-                       align = klass->packing_size ? MIN (klass->packing_size, align): align;  
-                       info->fields [j].offset = info->native_size;
-                       info->fields [j].offset += align - 1;
-                       info->fields [j].offset &= ~(align - 1);
-                       info->native_size = info->fields [j].offset + size;
-                       break;
-               case TYPE_ATTRIBUTE_EXPLICIT_LAYOUT:
-                       /* FIXME: */
-                       info->fields [j].offset = klass->fields [i].offset - sizeof (MonoObject);
-                       info->native_size = klass->instance_size - sizeof (MonoObject);
-                       break;
-               }       
-               j++;
-       }
-
-       if (info->native_size & (klass->min_align - 1)) {
-               info->native_size += klass->min_align - 1;
-               info->native_size &= ~(klass->min_align - 1);
-       }
-
-       return klass->marshal_info;
+static MonoMethodSignature*
+inflate_generic_signature (MonoImage *image, MonoMethodSignature *sig,
+                          MonoGenericMethod *gmethod)
+{
+       MonoMethodSignature *res;
+       int i;
+       res = mono_metadata_signature_alloc (image, sig->param_count);
+       res->ret = mono_class_inflate_generic_type (sig->ret, gmethod->generic_inst, gmethod);
+       for (i = 0; i < sig->param_count; ++i)
+               res->params [i] = mono_class_inflate_generic_type (sig->params [i],
+                                                                  gmethod->generic_inst,
+                                                                  gmethod);
+       res->hasthis = sig->hasthis;
+       res->explicit_this = sig->explicit_this;
+       res->call_convention = sig->call_convention;
+       res->generic_param_count = sig->generic_param_count;
+       return res;
+}
+
+static MonoMethodHeader*
+inflate_generic_header (MonoMethodHeader *header, MonoGenericMethod *gmethod)
+{
+       MonoMethodHeader *res;
+       int i;
+       res = g_malloc0 (sizeof (MonoMethodHeader) + sizeof (gpointer) * header->num_locals);
+       res->code = header->code;
+       res->code_size = header->code_size;
+       res->max_stack = header->max_stack;
+       res->num_clauses = header->num_clauses;
+       res->init_locals = header->init_locals;
+       res->num_locals = header->num_locals;
+       res->clauses = header->clauses;
+       res->gen_params = header->gen_params;
+       res->gen_method = gmethod;
+       for (i = 0; i < header->num_locals; ++i)
+               res->locals [i] = mono_class_inflate_generic_type (header->locals [i],
+                                                                  gmethod->generic_inst,
+                                                                  gmethod);
+       return res;
+}
+
+MonoMethod*
+mono_class_inflate_generic_method (MonoMethod *method, MonoGenericMethod *gmethod)
+{
+       MonoMethod *result;
+       if ((method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) || (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)) {
+               MonoMethodPInvoke *nmethod = g_new0 (MonoMethodPInvoke, 1);
+               *nmethod = *(MonoMethodPInvoke*)method;
+               result = (MonoMethod*)nmethod;
+       } else {
+               MonoMethodNormal *nmethod = g_new0 (MonoMethodNormal, 1);
+               *nmethod = *(MonoMethodNormal*)method;
+               result = (MonoMethod*)nmethod;
+               if (nmethod->header)
+                       nmethod->header = inflate_generic_header (nmethod->header, gmethod);
+       }
+       result->klass = gmethod->klass;
+       result->signature = inflate_generic_signature (
+               method->klass->image, result->signature, gmethod);
+       return result;
 }
 
 /** 
@@ -187,9 +338,11 @@ class_compute_field_layout (MonoClass *class)
        const int top = class->field.count;
        guint32 layout = class->flags & TYPE_ATTRIBUTE_LAYOUT_MASK;
        MonoTableInfo *t = &m->tables [MONO_TABLE_FIELD];
-       int i, blittable = TRUE;
+       int i, blittable = TRUE, real_size = 0;
        guint32 rva;
        guint32 packing_size = 0;
+       gboolean explicit_size;
+       MonoClassField *field;
 
        if (class->size_inited)
                return;
@@ -199,19 +352,25 @@ class_compute_field_layout (MonoClass *class)
                        class_compute_field_layout (class->parent);
                class->instance_size += class->parent->instance_size;
                class->min_align = class->parent->min_align;
+               blittable = class->blittable;
        } else {
                class->instance_size = sizeof (MonoObject);
                class->min_align = 1;
        }
 
-       if (mono_metadata_packing_from_typedef (class->image, class->type_token, &packing_size, &class->instance_size)) {
-               class->instance_size += sizeof (MonoObject);
-       }
+       /* Get the real size */
+       explicit_size = mono_metadata_packing_from_typedef (class->image, class->type_token, &packing_size, &real_size);
 
-       g_assert ((packing_size & 0xfffffff0) == 0);
-       class->packing_size = packing_size;
+       if (explicit_size) {
+               g_assert ((packing_size & 0xfffffff0) == 0);
+               class->packing_size = packing_size;
+               real_size += class->instance_size;
+       }
 
        if (!top) {
+               if (explicit_size && real_size) {
+                       class->instance_size = MAX (real_size, class->instance_size);
+               }
                class->size_inited = 1;
                return;
        }
@@ -225,46 +384,55 @@ class_compute_field_layout (MonoClass *class)
                const char *sig;
                guint32 cols [MONO_FIELD_SIZE];
                int idx = class->field.first + i;
-               
-               mono_metadata_decode_row (t, idx, cols, CSIZE (cols));
+
+               field = &class->fields [i];
+               mono_metadata_decode_row (t, idx, cols, MONO_FIELD_SIZE);
                /* The name is needed for fieldrefs */
-               class->fields [i].name = mono_metadata_string_heap (m, cols [MONO_FIELD_NAME]);
+               field->name = mono_metadata_string_heap (m, cols [MONO_FIELD_NAME]);
                sig = mono_metadata_blob_heap (m, cols [MONO_FIELD_SIGNATURE]);
                mono_metadata_decode_value (sig, &sig);
                /* FIELD signature == 0x06 */
                g_assert (*sig == 0x06);
-               class->fields [i].type = mono_metadata_parse_field_type (
+               field->type = mono_metadata_parse_field_type (
                        m, cols [MONO_FIELD_FLAGS], sig + 1, &sig);
+               if (mono_field_is_deleted (field))
+                       continue;
+               if (class->generic_inst) {
+                       field->type = mono_class_inflate_generic_type (field->type, class->generic_inst, NULL);
+                       field->type->attrs = cols [MONO_FIELD_FLAGS];
+               }
 
-               class->fields [i].parent = class;
+               field->parent = class;
 
-               if (!(class->fields [i].type->attrs & FIELD_ATTRIBUTE_STATIC)) {
-                       if (class->fields [i].type->byref) {
+               if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC)) {
+                       if (field->type->byref) {
                                blittable = FALSE;
                        } else {
-                               MonoClass *field_class = mono_class_from_mono_type (class->fields [i].type);
-                               if (!field_class->blittable)
+                               MonoClass *field_class = mono_class_from_mono_type (field->type);
+                               if (!field_class || !field_class->blittable)
                                        blittable = FALSE;
                        }
                }
                if (layout == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT) {
-                       mono_metadata_field_info (m, idx, &class->fields [i].offset, NULL, NULL);
-                       if (class->fields [i].offset == (guint32)-1)
-                               g_warning ("%s not initialized correctly (missing field layout info for %s)", class->name, class->fields [i].name);
+                       mono_metadata_field_info (m, idx, &field->offset, NULL, NULL);
+                       if (field->offset == (guint32)-1)
+                               g_warning ("%s not initialized correctly (missing field layout info for %s)", class->name, field->name);
                }
 
                if (cols [MONO_FIELD_FLAGS] & FIELD_ATTRIBUTE_HAS_FIELD_RVA) {
                        mono_metadata_field_info (m, idx, NULL, &rva, NULL);
                        if (!rva)
-                               g_warning ("field %s in %s should have RVA data, but hasn't", class->fields [i].name, class->name);
-                       class->fields [i].data = mono_cli_rva_map (class->image->image_info, rva);
+                               g_warning ("field %s in %s should have RVA data, but hasn't", field->name, class->name);
+                       field->data = mono_cli_rva_map (class->image->image_info, rva);
                }
 
                if (class->enumtype && !(cols [MONO_FIELD_FLAGS] & FIELD_ATTRIBUTE_STATIC)) {
-                       class->enum_basetype = class->fields [i].type;
+                       class->enum_basetype = field->type;
                        class->cast_class = class->element_class = mono_class_from_mono_type (class->enum_basetype);
                        blittable = class->element_class->blittable;
                }
+
+               /* The def_value of fields is compute lazily during vtable creation */
        }
 
        if (class == mono_defaults.string_class)
@@ -276,6 +444,13 @@ class_compute_field_layout (MonoClass *class)
                if (!((strcmp (class->name, "Enum") == 0) && (strcmp (class->name_space, "System") == 0)))
                        G_BREAKPOINT ();
        }
+       if (explicit_size && real_size) {
+               class->instance_size = MAX (real_size, class->instance_size);
+       }
+
+       if (class->gen_params)
+               return;
+
        mono_class_layout_fields (class);
 }
 
@@ -285,63 +460,128 @@ mono_class_layout_fields (MonoClass *class)
        int i;
        const int top = class->field.count;
        guint32 layout = class->flags & TYPE_ATTRIBUTE_LAYOUT_MASK;
+       guint32 pass, passes, real_size;
+       gboolean gc_aware_layout = FALSE;
+       MonoClassField *field;
+
+       /*
+        * Enable GC aware auto layout: in this mode, reference
+        * fields are grouped together inside objects, increasing collector 
+        * performance.
+        * Requires that all classes whose layout is known to native code be annotated
+        * with [StructLayout (LayoutKind.Sequential)]
+        */
+        /* corlib is missing [StructLayout] directives in many places */
+       if (layout == TYPE_ATTRIBUTE_AUTO_LAYOUT) {
+               if (class->image != mono_defaults.corlib)
+                       gc_aware_layout = TRUE;
+       }
 
        /*
         * Compute field layout and total size (not considering static fields)
         */
+
        switch (layout) {
        case TYPE_ATTRIBUTE_AUTO_LAYOUT:
        case TYPE_ATTRIBUTE_SEQUENTIAL_LAYOUT:
-               for (i = 0; i < top; i++){
-                       int size, align;
-                       
-                       if (class->fields [i].type->attrs & FIELD_ATTRIBUTE_STATIC)
-                               continue;
 
-                       size = mono_type_size (class->fields [i].type, &align);
+               if (gc_aware_layout)
+                       passes = 2;
+               else
+                       passes = 1;
+
+               if (layout != TYPE_ATTRIBUTE_AUTO_LAYOUT)
+                       passes = 1;
+
+               if (class->parent)
+                       real_size = class->parent->instance_size;
+               else
+                       real_size = sizeof (MonoObject);
+
+               for (pass = 0; pass < passes; ++pass) {
+                       for (i = 0; i < top; i++){
+                               int size, align;
+                               field = &class->fields [i];
+
+                               if (mono_field_is_deleted (field))
+                                       continue;
+                               if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
+                                       continue;
+
+                               if (gc_aware_layout) {
+                                       /* 
+                                        * We process fields with reference type in the first pass,
+                                        * and fields with non-reference type in the second pass.
+                                        * We use IS_POINTER instead of IS_REFERENCE because in
+                                        * some internal structures, we store GC_MALLOCed memory
+                                        * in IntPtr fields...
+                                        */
+                                       if (MONO_TYPE_IS_POINTER (field->type)) {
+                                               if (pass == 1)
+                                                       continue;
+                                       } else {
+                                               if (pass == 0)
+                                                       continue;
+                                       }
+                               }
+
+                               if ((top == 1) && (class->instance_size == sizeof (MonoObject)) &&
+                                       (strcmp (field->name, "$PRIVATE$") == 0)) {
+                                       /* This field is a hack inserted by MCS to empty structures */
+                                       continue;
+                               }
+
+                               size = mono_type_size (field->type, &align);
                        
-                       /* FIXME (LAMESPEC): should we also change the min alignment according to pack? */
-                       align = class->packing_size ? MIN (class->packing_size, align): align;
-                       class->min_align = MAX (align, class->min_align);
-                       class->fields [i].offset = class->instance_size;
-                       class->fields [i].offset += align - 1;
-                       class->fields [i].offset &= ~(align - 1);
-                       class->instance_size = class->fields [i].offset + size;
+                               /* FIXME (LAMESPEC): should we also change the min alignment according to pack? */
+                               align = class->packing_size ? MIN (class->packing_size, align): align;
+                               class->min_align = MAX (align, class->min_align);
+                               field->offset = real_size;
+                               field->offset += align - 1;
+                               field->offset &= ~(align - 1);
+                               real_size = field->offset + size;
+                       }
 
-               }
+                       class->instance_size = MAX (real_size, class->instance_size);
        
-               if (class->instance_size & (class->min_align - 1)) {
-                       class->instance_size += class->min_align - 1;
-                       class->instance_size &= ~(class->min_align - 1);
+                       if (class->instance_size & (class->min_align - 1)) {
+                               class->instance_size += class->min_align - 1;
+                               class->instance_size &= ~(class->min_align - 1);
+                       }
                }
                break;
        case TYPE_ATTRIBUTE_EXPLICIT_LAYOUT:
+               real_size = 0;
                for (i = 0; i < top; i++) {
                        int size, align;
+                       field = &class->fields [i];
 
                        /*
                         * There must be info about all the fields in a type if it
                         * uses explicit layout.
                         */
 
-                       if (class->fields [i].type->attrs & FIELD_ATTRIBUTE_STATIC)
+                       if (mono_field_is_deleted (field))
+                               continue;
+                       if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
                                continue;
 
-                       size = mono_type_size (class->fields [i].type, &align);
+                       size = mono_type_size (field->type, &align);
                        
                        /*
-                        * When we get here, class->fields [i].offset is already set by the
+                        * When we get here, field->offset is already set by the
                         * loader (for either runtime fields or fields loaded from metadata).
                         * The offset is from the start of the object: this works for both
                         * classes and valuetypes.
                         */
-                       class->fields [i].offset += sizeof (MonoObject);
+                       field->offset += sizeof (MonoObject);
 
                        /*
                         * Calc max size.
                         */
-                       class->instance_size = MAX (class->instance_size, size + class->fields [i].offset);
+                       real_size = MAX (real_size, size + field->offset);
                }
+               class->instance_size = MAX (real_size, class->instance_size);
                break;
        }
 
@@ -350,42 +590,20 @@ mono_class_layout_fields (MonoClass *class)
        /*
         * Compute static field layout and size
         */
-       switch (layout) {
-       case TYPE_ATTRIBUTE_AUTO_LAYOUT:
-       case TYPE_ATTRIBUTE_SEQUENTIAL_LAYOUT:
-               for (i = 0; i < top; i++){
-                       int size, align;
-                       
-                       if (!(class->fields [i].type->attrs & FIELD_ATTRIBUTE_STATIC))
-                               continue;
+       for (i = 0; i < top; i++){
+               int size, align;
+               field = &class->fields [i];
                        
-                       size = mono_type_size (class->fields [i].type, &align);
-                       class->fields [i].offset = class->class_size;
-                       class->fields [i].offset += align - 1;
-                       class->fields [i].offset &= ~(align - 1);
-                       class->class_size = class->fields [i].offset + size;
-
-               }
-               break;
-       case TYPE_ATTRIBUTE_EXPLICIT_LAYOUT:
-               for (i = 0; i < top; i++){
-                       int size, align;
-
-                       /*
-                        * There must be info about all the fields in a type if it
-                        * uses explicit layout.
-                        */
+               if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC))
+                       continue;
+               if (mono_field_is_deleted (field))
+                       continue;
                        
-                       if (!(class->fields [i].type->attrs & FIELD_ATTRIBUTE_STATIC))
-                               continue;
-
-                       size = mono_type_size (class->fields [i].type, &align);
-                       class->fields [i].offset = class->class_size;
-                       class->fields [i].offset += align - 1;
-                       class->fields [i].offset &= ~(align - 1);
-                       class->class_size = class->fields [i].offset + size;
-               }
-               break;
+               size = mono_type_size (field->type, &align);
+               field->offset = class->class_size;
+               field->offset += align - 1;
+               field->offset &= ~(align - 1);
+               class->class_size = field->offset + size;
        }
 }
 
@@ -472,12 +690,15 @@ mono_get_unique_iid (MonoClass *class)
        
        g_assert (class->flags & TYPE_ATTRIBUTE_INTERFACE);
 
+       mono_loader_lock ();
+
        if (!iid_hash)
                iid_hash = g_hash_table_new (g_str_hash, g_str_equal);
 
        str = g_strdup_printf ("%s|%s.%s\n", class->image->name, class->name_space, class->name);
 
        if (g_hash_table_lookup_extended (iid_hash, str, NULL, &value)) {
+               mono_loader_unlock ();
                g_free (str);
                return (guint)value;
        } else {
@@ -485,14 +706,43 @@ mono_get_unique_iid (MonoClass *class)
                ++iid;
        }
 
+       mono_loader_unlock ();
+
        return iid - 1;
 }
 
+static void
+collect_implemented_interfaces_aux (MonoClass *klass, GPtrArray **res)
+{
+       int i;
+       MonoClass *ic;
+       
+       for (i = 0; i < klass->interface_count; i++) {
+               ic = klass->interfaces [i];
+
+               if (*res == NULL)
+                       *res = g_ptr_array_new ();
+               g_ptr_array_add (*res, ic);
+
+               collect_implemented_interfaces_aux (ic, res);
+       }
+}
+
+static inline GPtrArray*
+collect_implemented_interfaces (MonoClass *klass)
+{
+       GPtrArray *res = NULL;
+
+       collect_implemented_interfaces_aux (klass, &res);
+       return res;
+}
+
 static int
 setup_interface_offsets (MonoClass *class, int cur_slot)
 {
        MonoClass *k, *ic;
        int i, max_iid;
+       GPtrArray *ifaces;
 
        /* compute maximum number of slots and maximum interface id */
        max_iid = 0;
@@ -519,24 +769,37 @@ setup_interface_offsets (MonoClass *class, int cur_slot)
        for (i = 0; i <= max_iid; i++)
                class->interface_offsets [i] = -1;
 
-       for (i = 0; i < class->interface_count; i++) {
-               ic = class->interfaces [i];
-               class->interface_offsets [ic->interface_id] = cur_slot;
-               cur_slot += ic->method.count;
+       ifaces = collect_implemented_interfaces (class);
+       if (ifaces) {
+               for (i = 0; i < ifaces->len; ++i) {
+                       ic = g_ptr_array_index (ifaces, i);
+                       class->interface_offsets [ic->interface_id] = cur_slot;
+                       cur_slot += ic->method.count;
+               }
+               g_ptr_array_free (ifaces, TRUE);
        }
 
        for (k = class->parent; k ; k = k->parent) {
-               for (i = 0; i < k->interface_count; i++) {
-                       ic = k->interfaces [i]; 
-                       if (class->interface_offsets [ic->interface_id] == -1) {
-                               int io = k->interface_offsets [ic->interface_id];
+               ifaces = collect_implemented_interfaces (k);
+               if (ifaces) {
+                       for (i = 0; i < ifaces->len; ++i) {
+                               ic = g_ptr_array_index (ifaces, i);
+
+                               if (class->interface_offsets [ic->interface_id] == -1) {
+                                       int io = k->interface_offsets [ic->interface_id];
 
-                               g_assert (io >= 0);
+                                       g_assert (io >= 0);
 
-                               class->interface_offsets [ic->interface_id] = io;
+                                       class->interface_offsets [ic->interface_id] = io;
+                               }
                        }
+                       g_ptr_array_free (ifaces, TRUE);
                }
        }
+
+       if (class->flags & TYPE_ATTRIBUTE_INTERFACE)
+               class->interface_offsets [class->interface_id] = cur_slot;
+
        return cur_slot;
 }
 
@@ -545,7 +808,9 @@ mono_class_setup_vtable (MonoClass *class, MonoMethod **overrides, int onum)
 {
        MonoClass *k, *ic;
        MonoMethod **vtable;
-       int i, ms, max_vtsize = 0, max_iid, cur_slot = 0;
+       int i, max_vtsize = 0, max_iid, cur_slot = 0;
+       GPtrArray *ifaces;
+       MonoGHashTable *override_map = NULL;
 
        /* setup_vtable() must be called only once on the type */
        if (class->interface_offsets) {
@@ -553,8 +818,14 @@ mono_class_setup_vtable (MonoClass *class, MonoMethod **overrides, int onum)
                return;
        }
 
-       for (i = 0; i < class->interface_count; i++) 
-               max_vtsize += class->interfaces [i]->method.count;
+       ifaces = collect_implemented_interfaces (class);
+       if (ifaces) {
+               for (i = 0; i < ifaces->len; i++) {
+                       MonoClass *ic = g_ptr_array_index (ifaces, i);
+                       max_vtsize += ic->method.count;
+               }
+               g_ptr_array_free (ifaces, TRUE);
+       }
        
        if (class->parent) {
                max_vtsize += class->parent->vtable_size;
@@ -582,27 +853,39 @@ mono_class_setup_vtable (MonoClass *class, MonoMethod **overrides, int onum)
                        g_assert (decl->slot != -1);
                        dslot = decl->slot + class->interface_offsets [decl->klass->interface_id];
                        vtable [dslot] = overrides [i*2 + 1];
+                       vtable [dslot]->slot = dslot;
+                       if (!override_map)
+                               override_map = mono_g_hash_table_new (NULL, NULL);
+
+                       mono_g_hash_table_insert (override_map, overrides [i * 2], overrides [i * 2 + 1]);
                }
        }
 
        for (k = class; k ; k = k->parent) {
-               class->idepth++;
-               for (i = 0; i < k->interface_count; i++) {
+               int nifaces = 0;
+               ifaces = collect_implemented_interfaces (k);
+               if (ifaces)
+                       nifaces = ifaces->len;
+               for (i = 0; i < nifaces; i++) {
                        int j, l, io;
 
-                       ic = k->interfaces [i];
+                       ic = g_ptr_array_index (ifaces, i);
                        io = k->interface_offsets [ic->interface_id];
-                       
+
                        g_assert (io >= 0);
                        g_assert (io <= max_vtsize);
 
                        if (k == class) {
                                for (l = 0; l < ic->method.count; l++) {
                                        MonoMethod *im = ic->methods [l];                                               
+
+                                       if (vtable [io + l] && !(vtable [io + l]->flags & METHOD_ATTRIBUTE_ABSTRACT))
+                                               continue;
+
                                        for (j = 0; j < class->method.count; ++j) {
                                                MonoMethod *cm = class->methods [j];
                                                if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
-                                                   !(cm->flags & METHOD_ATTRIBUTE_PUBLIC) ||
+                                                   !((cm->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == METHOD_ATTRIBUTE_PUBLIC) ||
                                                    !(cm->flags & METHOD_ATTRIBUTE_NEW_SLOT))
                                                        continue;
                                                if (!strcmp(cm->name, im->name) && 
@@ -690,6 +973,20 @@ mono_class_setup_vtable (MonoClass *class, MonoMethod **overrides, int onum)
                                        if (im->flags & METHOD_ATTRIBUTE_STATIC)
                                                        continue;
                                        g_assert (io + l <= max_vtsize);
+
+                                       /* 
+                                        * If one of our parents already implements this interface
+                                        * we can inherit the implementation.
+                                        */
+                                       if (!(vtable [io + l])) {
+                                               MonoClass *parent = class->parent;
+
+                                               if ((ic->interface_id <= parent->max_interface_id) && 
+                                                       (parent->interface_offsets [ic->interface_id]) &&
+                                                       parent->vtable)
+                                                       vtable [io + l] = parent->vtable [parent->interface_offsets [ic->interface_id] + l];
+                                       }
+
                                        if (!(vtable [io + l])) {
                                                for (j = 0; j < onum; ++j) {
                                                        g_print (" at slot %d: %s (%d) overrides %s (%d)\n", io+l, overrides [j*2+1]->name, 
@@ -724,6 +1021,8 @@ mono_class_setup_vtable (MonoClass *class, MonoMethod **overrides, int onum)
                                }
                        }
                }
+               if (ifaces)
+                       g_ptr_array_free (ifaces, TRUE);
        } 
 
        for (i = 0; i < class->method.count; ++i) {
@@ -743,6 +1042,9 @@ mono_class_setup_vtable (MonoClass *class, MonoMethod **overrides, int onum)
                                            mono_metadata_signature_equal (cm->signature, m1->signature)) {
                                                slot = k->methods [j]->slot;
                                                g_assert (cm->slot < max_vtsize);
+                                               if (!override_map)
+                                                       override_map = mono_g_hash_table_new (NULL, NULL);
+                                               mono_g_hash_table_insert (override_map, m1, cm);
                                                break;
                                        }
                                }
@@ -756,7 +1058,7 @@ mono_class_setup_vtable (MonoClass *class, MonoMethod **overrides, int onum)
                if (cm->slot < 0)
                        cm->slot = cur_slot++;
 
-               if (!(cm->flags & METHOD_ATTRIBUTE_ABSTRACT))
+               if (!(cm->flags & METHOD_ATTRIBUTE_ABSTRACT) && !cm->signature->generic_param_count)
                        vtable [cm->slot] = cm;
        }
 
@@ -767,23 +1069,31 @@ mono_class_setup_vtable (MonoClass *class, MonoMethod **overrides, int onum)
                        g_assert (decl->slot != -1);
                        vtable [decl->slot] = overrides [i*2 + 1];
                        overrides [i * 2 + 1]->slot = decl->slot;
+                       if (!override_map)
+                               override_map = mono_g_hash_table_new (NULL, NULL);
+                       mono_g_hash_table_insert (override_map, decl, overrides [i * 2 + 1]);
                }
        }
+
+       /*
+        * If a method occupies more than one place in the vtable, and it is
+        * overriden, then change the other occurances too.
+        */
+       if (override_map) {
+               for (i = 0; i < max_vtsize; ++i)
+                       if (vtable [i]) {
+                               MonoMethod *cm = mono_g_hash_table_lookup (override_map, vtable [i]);
+                               if (cm)
+                                       vtable [i] = cm;
+                       }
+
+               mono_g_hash_table_destroy (override_map);
+       }
        
        class->vtable_size = cur_slot;
        class->vtable = g_malloc0 (sizeof (gpointer) * class->vtable_size);
        memcpy (class->vtable, vtable,  sizeof (gpointer) * class->vtable_size);
 
-       ms = MAX (MONO_DEFAULT_SUPERTABLE_SIZE, class->idepth);
-       class->supertypes = g_new0 (MonoClass *, ms);
-
-       if (class->parent) {
-               for (i = class->idepth, k = class; k ; k = k->parent)
-                       class->supertypes [--i] = k;
-       } else {
-               class->supertypes [0] = class;
-       }
-       
        if (mono_print_vtable) {
                int icount = 0;
 
@@ -827,7 +1137,6 @@ mono_class_setup_vtable (MonoClass *class, MonoMethod **overrides, int onum)
                        }
                }
        }
-
 }
 
 /**
@@ -855,6 +1164,17 @@ mono_class_init (MonoClass *class)
        if (class->inited)
                return;
 
+       /*g_print ("Init class %s\n", class->name);*/
+
+       /* We do everything inside the lock to prevent races */
+       mono_loader_lock ();
+
+       if (class->inited) {
+               mono_loader_unlock ();
+               /* Somebody might have gotten in before us */
+               return;
+       }
+
        if (class->init_pending) {
                /* this indicates a cyclic dependency */
                g_error ("pending init %s.%s\n", class->name_space, class->name);
@@ -874,26 +1194,67 @@ mono_class_init (MonoClass *class)
                class_compute_field_layout (class);
 
        /* initialize method pointers */
-       class->methods = g_new (MonoMethod*, class->method.count);
-       for (i = 0; i < class->method.count; ++i)
-               class->methods [i] = mono_get_method (class->image,
-                       MONO_TOKEN_METHOD_DEF | (i + class->method.first + 1), class);
+       if (class->rank) {
+               MonoMethod *ctor;
+               MonoMethodSignature *sig;
+               class->method.count = class->rank > 1? 2: 1;
+               sig = mono_metadata_signature_alloc (class->image, class->rank);
+               sig->ret = &mono_defaults.void_class->byval_arg;
+               sig->pinvoke = TRUE;
+               for (i = 0; i < class->rank; ++i)
+                       sig->params [i] = &mono_defaults.int32_class->byval_arg;
+
+               ctor = (MonoMethod *) g_new0 (MonoMethodPInvoke, 1);
+               ctor->klass = class;
+               ctor->flags = METHOD_ATTRIBUTE_PUBLIC | METHOD_ATTRIBUTE_RT_SPECIAL_NAME | METHOD_ATTRIBUTE_SPECIAL_NAME;
+               ctor->iflags = METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL;
+               ctor->signature = sig;
+               ctor->name = ".ctor";
+               ctor->slot = -1;
+               class->methods = g_new (MonoMethod*, class->method.count);
+               class->methods [0] = ctor;
+               if (class->rank > 1) {
+                       sig = mono_metadata_signature_alloc (class->image, class->rank * 2);
+                       sig->ret = &mono_defaults.void_class->byval_arg;
+                       sig->pinvoke = TRUE;
+                       for (i = 0; i < class->rank * 2; ++i)
+                               sig->params [i] = &mono_defaults.int32_class->byval_arg;
+
+                       ctor = (MonoMethod *) g_new0 (MonoMethodPInvoke, 1);
+                       ctor->klass = class;
+                       ctor->flags = METHOD_ATTRIBUTE_PUBLIC | METHOD_ATTRIBUTE_RT_SPECIAL_NAME | METHOD_ATTRIBUTE_SPECIAL_NAME;
+                       ctor->iflags = METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL;
+                       ctor->signature = sig;
+                       ctor->name = ".ctor";
+                       ctor->slot = -1;
+                       class->methods [1] = ctor;
+               }
+       } else {
+               if (!class->generic_inst && !class->methods) {
+                       class->methods = g_new (MonoMethod*, class->method.count);
+                       for (i = 0; i < class->method.count; ++i) {
+                               class->methods [i] = mono_get_method (class->image,
+                                                                     MONO_TOKEN_METHOD_DEF | (i + class->method.first + 1), class);
+                       }
+               }
+       }
 
        init_properties (class);
        init_events (class);
 
-       i = mono_metadata_nesting_typedef (class->image, class->type_token);
+       i = mono_metadata_nesting_typedef (class->image, class->type_token, 1);
        while (i) {
                MonoClass* nclass;
                guint32 cols [MONO_NESTED_CLASS_SIZE];
                mono_metadata_decode_row (&class->image->tables [MONO_TABLE_NESTEDCLASS], i - 1, cols, MONO_NESTED_CLASS_SIZE);
-               if (cols [MONO_NESTED_CLASS_ENCLOSING] != mono_metadata_token_index (class->type_token))
-                       break;
                nclass = mono_class_create_from_typedef (class->image, MONO_TOKEN_TYPE_DEF | cols [MONO_NESTED_CLASS_NESTED]);
                class->nested_classes = g_list_prepend (class->nested_classes, nclass);
-               ++i;
+
+               i = mono_metadata_nesting_typedef (class->image, class->type_token, i + 1);
        }
 
+       mono_class_setup_supertypes (class);
+
        if (class->flags & TYPE_ATTRIBUTE_INTERFACE) {
                for (i = 0; i < class->method.count; ++i)
                        class->methods [i]->slot = i;
@@ -904,6 +1265,7 @@ mono_class_init (MonoClass *class)
                 * we have to setup them for interfaces, too.
                 */
                setup_interface_offsets (class, 0);
+               mono_loader_unlock ();
                return;
        }
 
@@ -965,30 +1327,13 @@ mono_class_init (MonoClass *class)
                        class->has_finalize = 1;
        }
 
+       mono_loader_unlock ();
+
        if (mono_debugger_class_init_func)
                mono_debugger_class_init_func (class);
 }
 
 
-/*
- * Compute a relative numbering of the class hierarchy as described in
- * "Java for Large-Scale Scientific Computations?"
- */
-static void
-mono_compute_relative_numbering (MonoClass *class, int *c)
-{
-       GList *s;
-
-       (*c)++;
-
-       class->baseval = *c;
-
-       for (s = class->subclasses; s; s = s->next)
-               mono_compute_relative_numbering ((MonoClass *)s->data, c); 
-       
-       class->diffval = *c -  class->baseval;
-}
-
 void
 mono_class_setup_mono_type (MonoClass *class)
 {
@@ -1088,6 +1433,12 @@ mono_class_setup_mono_type (MonoClass *class)
                                        class->blittable = TRUE;
                                }
                                break;
+                       case 'T':
+                               if (!strcmp (name, "TypedReference")) {
+                                       t = MONO_TYPE_TYPEDBYREF;
+                                       class->blittable = TRUE;
+                               }
+                               break;
                        case 'V':
                                if (!strcmp (name, "Void")) {
                                        t = MONO_TYPE_VOID;
@@ -1121,12 +1472,20 @@ mono_class_setup_parent (MonoClass *class, MonoClass *parent)
        }
 
        if (!(class->flags & TYPE_ATTRIBUTE_INTERFACE)) {
-               int rnum = 0;
                class->parent = parent;
 
                if (!parent)
                        g_assert_not_reached (); /* FIXME */
 
+               if (parent->generic_inst && !parent->name) {
+                       /*
+                        * If the parent is a generic instance, we may get
+                        * called before it is fully initialized, especially
+                        * before it has its name.
+                        */
+                       return;
+               }
+
                class->marshalbyref = parent->marshalbyref;
                class->contextbound  = parent->contextbound;
                class->delegate  = parent->delegate;
@@ -1149,14 +1508,38 @@ mono_class_setup_parent (MonoClass *class, MonoClass *parent)
                        class->valuetype = class->enumtype = 1;
                }
                /*class->enumtype = class->parent->enumtype; */
-               class->parent->subclasses = g_list_prepend (class->parent->subclasses, class);
-               mono_compute_relative_numbering (mono_defaults.object_class, &rnum);
+               mono_class_setup_supertypes (class);
        } else {
                class->parent = NULL;
        }
 
 }
 
+void
+mono_class_setup_supertypes (MonoClass *class)
+{
+       MonoClass *k;
+       int ms, i;
+
+       if (class->supertypes)
+               return;
+
+       class->idepth = 0;
+       for (k = class; k ; k = k->parent) {
+               class->idepth++;
+       }
+
+       ms = MAX (MONO_DEFAULT_SUPERTABLE_SIZE, class->idepth);
+       class->supertypes = g_new0 (MonoClass *, ms);
+
+       if (class->parent) {
+               for (i = class->idepth, k = class; k ; k = k->parent)
+                       class->supertypes [--i] = k;
+       } else {
+               class->supertypes [0] = class;
+       }
+}      
+
 /**
  * @image: context where the image is created
  * @type_token:  typedef token
@@ -1173,8 +1556,12 @@ mono_class_create_from_typedef (MonoImage *image, guint32 type_token)
        guint icount = 0; 
        MonoClass **interfaces;
 
-       if ((class = g_hash_table_lookup (image->class_cache, GUINT_TO_POINTER (type_token)))) 
+       mono_loader_lock ();
+
+       if ((class = g_hash_table_lookup (image->class_cache, GUINT_TO_POINTER (type_token)))) {
+               mono_loader_unlock ();
                return class;
+       }
 
        g_assert (mono_metadata_token_table (type_token) == MONO_TABLE_TYPEDEF);
        
@@ -1210,7 +1597,7 @@ mono_class_create_from_typedef (MonoImage *image, guint32 type_token)
 
        class->cast_class = class->element_class = class;
 
-       /*g_print ("Init class %s\n", name);*/
+       /*g_print ("Load class %s\n", name);*/
 
        mono_class_setup_parent (class, parent);
 
@@ -1223,7 +1610,7 @@ mono_class_create_from_typedef (MonoImage *image, guint32 type_token)
        class->method.first = cols [MONO_TYPEDEF_METHOD_LIST] - 1;
 
        if (tt->rows > tidx){           
-               mono_metadata_decode_row (tt, tidx, cols_next, CSIZE (cols_next));
+               mono_metadata_decode_row (tt, tidx, cols_next, MONO_TYPEDEF_SIZE);
                class->field.last  = cols_next [MONO_TYPEDEF_FIELD_LIST] - 1;
                class->method.last = cols_next [MONO_TYPEDEF_METHOD_LIST] - 1;
        } else {
@@ -1254,9 +1641,182 @@ mono_class_create_from_typedef (MonoImage *image, guint32 type_token)
        if ((type_token = mono_metadata_nested_in_typedef (image, type_token)))
                class->nested_in = mono_class_create_from_typedef (image, type_token);
 
+       class->gen_params = mono_metadata_load_generic_params (image, class->type_token, &icount);
+       class->num_gen_params = icount;
+
+       mono_loader_unlock ();
+
+       return class;
+}
+
+static char*
+get_instantiation_name (const char *name, MonoGenericInst *ginst)
+{
+       GString *res = g_string_new (name);
+       const char *p;
+       int i;
+       MonoClass *argclass;
+       
+       p = strchr (name, '<');
+       if (p) {
+               g_string_truncate (res, (p - name) + 1);
+       } else {
+               g_string_append_c (res, '<');
+       }
+       for (i = 0; i < ginst->type_argc; ++i) {
+               if (i > 0)
+                       g_string_append_c (res, ',');
+               argclass = mono_class_from_mono_type (ginst->type_argv [i]);
+               g_string_append (res, argclass->name);
+       }
+       g_string_append_c (res, '>');
+       return g_string_free (res, FALSE);
+}
+
+static void
+mono_class_initialize_generic (MonoGenericInst *ginst, gboolean inflate_methods)
+{
+       MonoClass *klass, *gklass, *pklass;
+
+       if (ginst->initialized || ginst->init_pending)
+               return;
+
+       gklass = mono_class_from_mono_type (ginst->generic_type);
+       mono_class_init (gklass);
+
+       klass = ginst->klass;
+       klass->name = get_instantiation_name (gklass->name, ginst);
+
+       if (ginst->parent)
+               pklass = mono_class_from_mono_type (ginst->parent);
+       else
+               pklass = gklass->parent;
+
+       mono_class_setup_parent (klass, pklass);
+       mono_class_setup_mono_type (klass);
+
+       if (inflate_methods) {
+               int i;
+
+               klass->field = gklass->field;
+               klass->method = gklass->method;
+               klass->methods = g_new0 (MonoMethod *, klass->method.count);
+
+               for (i = 0; i < klass->method.count; i++) {
+                       MonoGenericMethod *gmethod = g_new0 (MonoGenericMethod, 1);
+
+                       gmethod->klass = klass;
+                       gmethod->generic_method = gklass->methods [i];
+                       gmethod->generic_inst = ginst;
+
+                       klass->methods [i] = mono_class_inflate_generic_method (
+                               gklass->methods [i], gmethod);
+               }
+       }
+
+       ginst->initialized = TRUE;
+}
+
+MonoClass*
+mono_class_from_generic (MonoGenericInst *ginst)
+{
+       MonoClass *class, *gklass, *pklass;
+
+       if (ginst->klass) {
+               mono_class_initialize_generic (ginst, TRUE);
+               return ginst->klass;
+       }
+
+       mono_loader_lock ();
+
+       gklass = mono_class_from_mono_type (ginst->generic_type);
+       mono_class_init (gklass);
+
+       class = ginst->klass = g_malloc0 (sizeof (MonoClass));
+       class->name_space = gklass->name_space;
+       class->name = get_instantiation_name (gklass->name, ginst);
+       class->image = gklass->image;
+       class->flags = gklass->flags;
+
+       class->generic_inst = ginst;
+
+       class->cast_class = class->element_class = class;
+
+       mono_loader_unlock ();
+
        return class;
 }
 
+MonoClass *
+mono_class_from_generic_parameter (MonoGenericParam *param, MonoImage *image, gboolean is_mvar)
+{
+       MonoClass *klass, **ptr;
+       int count, pos, i;
+
+       if (param->pklass)
+               return param->pklass;
+
+       klass = param->pklass = g_new0 (MonoClass, 1);
+
+       for (count = 0, ptr = param->constraints; ptr && *ptr; ptr++, count++)
+               ;
+
+       pos = 0;
+       if ((count > 0) && !(param->constraints [0]->flags & TYPE_ATTRIBUTE_INTERFACE)) {
+               klass->parent = param->constraints [0];
+               pos++;
+       } else
+               klass->parent = mono_defaults.object_class;
+
+       if (count - pos > 0) {
+               klass->interface_count = count - pos;
+               klass->interfaces = g_new0 (MonoClass *, count - pos);
+               for (i = pos; i < count; i++)
+                       klass->interfaces [i - pos] = param->constraints [i];
+       }
+
+       klass->name = g_strdup_printf (is_mvar ? "!!%d" : "!%d", param->num);
+       klass->name_space = "";
+       klass->image = image;
+       klass->cast_class = klass->element_class = klass;
+       klass->enum_basetype = &klass->element_class->byval_arg;
+       klass->flags = TYPE_ATTRIBUTE_INTERFACE | TYPE_ATTRIBUTE_PUBLIC;
+
+       klass->this_arg.type = klass->byval_arg.type = is_mvar ? MONO_TYPE_MVAR : MONO_TYPE_VAR;
+       klass->this_arg.data.generic_param = klass->byval_arg.data.generic_param = param;
+       klass->this_arg.byref = TRUE;
+
+       mono_class_init (klass);
+
+       return klass;
+}
+
+static MonoClass *
+my_mono_class_from_generic_parameter (MonoGenericParam *param, gboolean is_mvar)
+{
+       MonoClass *klass;
+
+       if (param->pklass)
+               return param->pklass;
+
+       klass = g_new0 (MonoClass, 1);
+
+       klass->name = g_strdup_printf (is_mvar ? "!!%d" : "!%d", param->num);
+       klass->name_space = "";
+       klass->image = mono_defaults.corlib;
+       klass->cast_class = klass->element_class = klass;
+       klass->enum_basetype = &klass->element_class->byval_arg;
+       klass->flags = TYPE_ATTRIBUTE_INTERFACE | TYPE_ATTRIBUTE_PUBLIC;
+
+       klass->this_arg.type = klass->byval_arg.type = is_mvar ? MONO_TYPE_MVAR : MONO_TYPE_VAR;
+       klass->this_arg.data.generic_param = klass->byval_arg.data.generic_param = param;
+       klass->this_arg.byref = TRUE;
+
+       mono_class_init (klass);
+
+       return klass;
+}
+
 MonoClass *
 mono_ptr_class_get (MonoType *type)
 {
@@ -1264,11 +1824,15 @@ mono_ptr_class_get (MonoType *type)
        MonoClass *el_class;
        static GHashTable *ptr_hash = NULL;
 
+       mono_loader_lock ();
+
        if (!ptr_hash)
-               ptr_hash = g_hash_table_new (g_direct_hash, g_direct_equal);
+               ptr_hash = g_hash_table_new (NULL, NULL);
        el_class = mono_class_from_mono_type (type);
-       if ((result = g_hash_table_lookup (ptr_hash, el_class)))
+       if ((result = g_hash_table_lookup (ptr_hash, el_class))) {
+               mono_loader_unlock ();
                return result;
+       }
        result = g_new0 (MonoClass, 1);
 
        result->parent = NULL; /* no parent for PTR types */
@@ -1279,9 +1843,6 @@ mono_ptr_class_get (MonoType *type)
        result->flags = TYPE_ATTRIBUTE_CLASS | (el_class->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK);
        /* Can pointers get boxed? */
        result->instance_size = sizeof (gpointer);
-       /*
-        * baseval, diffval: need them to allow casting ?
-        */
        result->cast_class = result->element_class = el_class;
        result->enum_basetype = &result->element_class->byval_arg;
 
@@ -1289,8 +1850,53 @@ mono_ptr_class_get (MonoType *type)
        result->this_arg.data.type = result->byval_arg.data.type = result->enum_basetype;
        result->this_arg.byref = TRUE;
 
+       mono_class_setup_supertypes (result);
+
        g_hash_table_insert (ptr_hash, el_class, result);
 
+       mono_loader_unlock ();
+
+       return result;
+}
+
+static MonoClass *
+mono_fnptr_class_get (MonoMethodSignature *sig)
+{
+       MonoClass *result;
+       static GHashTable *ptr_hash = NULL;
+
+       mono_loader_lock ();
+
+       if (!ptr_hash)
+               ptr_hash = g_hash_table_new (NULL, NULL);
+       
+       if ((result = g_hash_table_lookup (ptr_hash, sig))) {
+               mono_loader_unlock ();
+               return result;
+       }
+       result = g_new0 (MonoClass, 1);
+
+       result->parent = NULL; /* no parent for PTR types */
+       result->name = "System";
+       result->name_space = "MonoFNPtrFakeClass";
+       result->image = NULL; /* need to fix... */
+       result->inited = TRUE;
+       result->flags = TYPE_ATTRIBUTE_CLASS; // | (el_class->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK);
+       /* Can pointers get boxed? */
+       result->instance_size = sizeof (gpointer);
+       result->cast_class = result->element_class = result;
+
+       result->this_arg.type = result->byval_arg.type = MONO_TYPE_FNPTR;
+       result->this_arg.data.method = result->byval_arg.data.method = sig;
+       result->this_arg.byref = TRUE;
+       result->enum_basetype = &result->element_class->byval_arg;
+
+       mono_class_setup_supertypes (result);
+
+       g_hash_table_insert (ptr_hash, sig, result);
+
+       mono_loader_unlock ();
+
        return result;
 }
 
@@ -1299,52 +1905,60 @@ mono_class_from_mono_type (MonoType *type)
 {
        switch (type->type) {
        case MONO_TYPE_OBJECT:
-               return mono_defaults.object_class;
+               return type->data.klass? type->data.klass: mono_defaults.object_class;
        case MONO_TYPE_VOID:
-               return mono_defaults.void_class;
+               return type->data.klass? type->data.klass: mono_defaults.void_class;
        case MONO_TYPE_BOOLEAN:
-               return mono_defaults.boolean_class;
+               return type->data.klass? type->data.klass: mono_defaults.boolean_class;
        case MONO_TYPE_CHAR:
-               return mono_defaults.char_class;
+               return type->data.klass? type->data.klass: mono_defaults.char_class;
        case MONO_TYPE_I1:
-               return mono_defaults.sbyte_class;
+               return type->data.klass? type->data.klass: mono_defaults.sbyte_class;
        case MONO_TYPE_U1:
-               return mono_defaults.byte_class;
+               return type->data.klass? type->data.klass: mono_defaults.byte_class;
        case MONO_TYPE_I2:
-               return mono_defaults.int16_class;
+               return type->data.klass? type->data.klass: mono_defaults.int16_class;
        case MONO_TYPE_U2:
-               return mono_defaults.uint16_class;
+               return type->data.klass? type->data.klass: mono_defaults.uint16_class;
        case MONO_TYPE_I4:
-               return mono_defaults.int32_class;
+               return type->data.klass? type->data.klass: mono_defaults.int32_class;
        case MONO_TYPE_U4:
-               return mono_defaults.uint32_class;
+               return type->data.klass? type->data.klass: mono_defaults.uint32_class;
        case MONO_TYPE_I:
-               return mono_defaults.int_class;
+               return type->data.klass? type->data.klass: mono_defaults.int_class;
        case MONO_TYPE_U:
-               return mono_defaults.uint_class;
+               return type->data.klass? type->data.klass: mono_defaults.uint_class;
        case MONO_TYPE_I8:
-               return mono_defaults.int64_class;
+               return type->data.klass? type->data.klass: mono_defaults.int64_class;
        case MONO_TYPE_U8:
-               return mono_defaults.uint64_class;
+               return type->data.klass? type->data.klass: mono_defaults.uint64_class;
        case MONO_TYPE_R4:
-               return mono_defaults.single_class;
+               return type->data.klass? type->data.klass: mono_defaults.single_class;
        case MONO_TYPE_R8:
-               return mono_defaults.double_class;
+               return type->data.klass? type->data.klass: mono_defaults.double_class;
        case MONO_TYPE_STRING:
-               return mono_defaults.string_class;
+               return type->data.klass? type->data.klass: mono_defaults.string_class;
        case MONO_TYPE_TYPEDBYREF:
-               return mono_defaults.typed_reference_class;
+               return type->data.klass? type->data.klass: mono_defaults.typed_reference_class;
        case MONO_TYPE_ARRAY:
-               return mono_array_class_get (type->data.array->type, type->data.array->rank);
+               return mono_bounded_array_class_get (type->data.array->eklass, type->data.array->rank, TRUE);
        case MONO_TYPE_PTR:
                return mono_ptr_class_get (type->data.type);
+       case MONO_TYPE_FNPTR:
+               return mono_fnptr_class_get (type->data.method);
        case MONO_TYPE_SZARRAY:
-               return mono_array_class_get (type->data.type, 1);
+               return mono_array_class_get (type->data.klass, 1);
        case MONO_TYPE_CLASS:
        case MONO_TYPE_VALUETYPE:
                return type->data.klass;
+       case MONO_TYPE_GENERICINST:
+               return mono_class_from_generic (type->data.generic_inst);
+       case MONO_TYPE_VAR:
+               return my_mono_class_from_generic_parameter (type->data.generic_param, FALSE);
+       case MONO_TYPE_MVAR:
+               return my_mono_class_from_generic_parameter (type->data.generic_param, TRUE);
        default:
-               g_warning ("implement me %02x\n", type->type);
+               g_warning ("implement me 0x%02x\n", type->type);
                g_assert_not_reached ();
        }
        
@@ -1365,63 +1979,77 @@ mono_class_create_from_typespec (MonoImage *image, guint32 type_spec)
 
        switch (type->type) {
        case MONO_TYPE_ARRAY:
-               class = mono_array_class_get (type->data.array->type, type->data.array->rank);
+               class = mono_array_class_get (type->data.array->eklass, type->data.array->rank);
                break;
        case MONO_TYPE_SZARRAY:
-               class = mono_array_class_get (type->data.type, 1);
+               class = mono_array_class_get (type->data.klass, 1);
                break;
        case MONO_TYPE_PTR:
                class = mono_class_from_mono_type (type->data.type);
                break;
+       case MONO_TYPE_GENERICINST:
+               class = mono_class_from_generic (type->data.generic_inst);
+               break;
        default:
                /* it seems any type can be stored in TypeSpec as well */
                class = mono_class_from_mono_type (type);
                break;
        }
 
-       mono_metadata_free_type (type);
-       
        return class;
 }
 
 /**
- * mono_array_class_get:
- * @element_type: element type 
+ * mono_bounded_array_class_get:
+ * @element_class: element class 
  * @rank: the dimension of the array class
+ * @bounded: whenever the array has non-zero bounds
  *
  * Returns: a class object describing the array with element type @element_type and 
  * dimension @rank. 
  */
 MonoClass *
-mono_array_class_get (MonoType *element_type, guint32 rank)
+mono_bounded_array_class_get (MonoClass *eclass, guint32 rank, gboolean bounded)
 {
-       MonoClass *eclass;
        MonoImage *image;
        MonoClass *class;
        MonoClass *parent = NULL;
-       GSList *list;
-       int rnum = 0, nsize;
+       GSList *list, *rootlist;
+       int nsize;
        char *name;
+       gboolean corlib_type = FALSE;
 
-       eclass = mono_class_from_mono_type (element_type);
        g_assert (rank <= 255);
 
-       parent = mono_defaults.array_class;
-
-       if (!parent->inited)
-               mono_class_init (parent);
+       if (rank > 1)
+               /* bounded only matters for one-dimensional arrays */
+               bounded = FALSE;
 
        image = eclass->image;
 
-       if ((list = g_hash_table_lookup (image->array_cache, element_type))) {
+       mono_loader_lock ();
+
+       if ((rootlist = list = g_hash_table_lookup (image->array_cache, eclass))) {
                for (; list; list = list->next) {
                        class = list->data;
-                       if (class->rank == rank)
+                       if ((class->rank == rank) && (class->byval_arg.type == (bounded ? MONO_TYPE_ARRAY : MONO_TYPE_SZARRAY))) {
+                               mono_loader_unlock ();
                                return class;
+                       }
                }
        }
-       
-       class = g_malloc0 (sizeof (MonoClass) + parent->vtable_size * sizeof (gpointer));
+
+       /* for the building corlib use System.Array from it */
+       if (image->assembly && image->assembly->dynamic && strcmp (image->assembly_name, "mscorlib") == 0) {
+               parent = mono_class_from_name (image, "System", "Array");
+               corlib_type = TRUE;
+       } else {
+               parent = mono_defaults.array_class;
+               if (!parent->inited)
+                       mono_class_init (parent);
+       }
+
+       class = g_malloc0 (sizeof (MonoClass));
 
        class->image = image;
        class->name_space = eclass->name_space;
@@ -1435,13 +2063,13 @@ mono_array_class_get (MonoType *element_type, guint32 rank)
        name [nsize + rank + 1] = 0;
        class->name = name;
        class->type_token = 0;
-       class->flags = TYPE_ATTRIBUTE_CLASS | (eclass->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK);
+       /* all arrays are marked serializable and sealed, bug #42779 */
+       class->flags = TYPE_ATTRIBUTE_CLASS | TYPE_ATTRIBUTE_SERIALIZABLE | TYPE_ATTRIBUTE_SEALED |
+               (eclass->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK);
        class->parent = parent;
        class->instance_size = mono_class_instance_size (class->parent);
        class->class_size = 0;
-       class->vtable_size = parent->vtable_size;
-       class->parent->subclasses = g_list_prepend (class->parent->subclasses, class);
-       mono_compute_relative_numbering (mono_defaults.object_class, &rnum);
+       mono_class_setup_supertypes (class);
 
        class->rank = rank;
        
@@ -1451,27 +2079,46 @@ mono_array_class_get (MonoType *element_type, guint32 rank)
                class->cast_class = eclass;
 
        class->element_class = eclass;
-       
-       if (rank > 1) {
+
+       if ((rank > 1) || bounded) {
                MonoArrayType *at = g_new0 (MonoArrayType, 1);
                class->byval_arg.type = MONO_TYPE_ARRAY;
                class->byval_arg.data.array = at;
-               at->type = &eclass->byval_arg;
+               at->eklass = eclass;
                at->rank = rank;
                /* FIXME: complete.... */
        } else {
-               /* FIXME: this is not correct. the lbound could be >0 */
                class->byval_arg.type = MONO_TYPE_SZARRAY;
-               class->byval_arg.data.type = &eclass->byval_arg;
+               class->byval_arg.data.klass = eclass;
        }
        class->this_arg = class->byval_arg;
        class->this_arg.byref = 1;
+       if (corlib_type) {
+               class->inited = 1;
+       }
+
+       list = g_slist_append (rootlist, class);
+       g_hash_table_insert (image->array_cache, eclass, list);
+
+       mono_loader_unlock ();
 
-       list = g_slist_append (list, class);
-       g_hash_table_insert (image->array_cache, &class->element_class->byval_arg, list);
        return class;
 }
 
+/**
+ * mono_array_class_get:
+ * @element_class: element class 
+ * @rank: the dimension of the array class
+ *
+ * Returns: a class object describing the array with element type @element_type and 
+ * dimension @rank. 
+ */
+MonoClass *
+mono_array_class_get (MonoClass *eclass, guint32 rank)
+{
+       return mono_bounded_array_class_get (eclass, rank, FALSE);
+}
+
 /**
  * mono_class_instance_size:
  * @klass: a class 
@@ -1480,34 +2127,13 @@ mono_array_class_get (MonoType *element_type, guint32 rank)
  */
 gint32
 mono_class_instance_size (MonoClass *klass)
-{
-       
+{      
        if (!klass->size_inited)
                mono_class_init (klass);
 
        return klass->instance_size;
 }
 
-/**
- * mono_class_native_size:
- * @klass: a class 
- * 
- * Returns: the native size of an object instance (when marshaled 
- * to unmanaged code) 
- */
-gint32
-mono_class_native_size (MonoClass *klass, guint32 *align)
-{
-       
-       if (!klass->marshal_info)
-               mono_marshal_load_type_info (klass);
-
-       if (align)
-               *align = klass->min_align;
-
-       return klass->marshal_info->native_size;
-}
-
 /**
  * mono_class_min_align:
  * @klass: a class 
@@ -1516,8 +2142,7 @@ mono_class_native_size (MonoClass *klass, guint32 *align)
  */
 gint32
 mono_class_min_align (MonoClass *klass)
-{
-       
+{      
        if (!klass->size_inited)
                mono_class_init (klass);
 
@@ -1559,8 +2184,7 @@ mono_class_value_size      (MonoClass *klass, guint32 *align)
  */
 gint32
 mono_class_data_size (MonoClass *klass)
-{
-       
+{      
        if (!klass->inited)
                mono_class_init (klass);
 
@@ -1647,9 +2271,9 @@ mono_class_get_property_from_name (MonoClass *klass, const char *name)
 MonoClass *
 mono_class_get (MonoImage *image, guint32 type_token)
 {
-       MonoClass *class;
+       MonoClass *class = NULL;
 
-       if (image->assembly->dynamic)
+       if (image->dynamic)
                return mono_lookup_dynamic_token (image, type_token);
 
        switch (type_token & 0xff000000){
@@ -1697,25 +2321,193 @@ mono_class_from_name_case (MonoImage *image, const char* name_space, const char
        return NULL;
 }
 
+static MonoClass*
+return_nested_in (MonoClass *class, char *nested) {
+       MonoClass *found;
+       char *s = strchr (nested, '/');
+       GList *tmp;
+
+       if (s) {
+               *s = 0;
+               s++;
+       }
+       for (tmp = class->nested_classes; tmp; tmp = tmp->next) {
+               found = tmp->data;
+               if (strcmp (found->name, nested) == 0) {
+                       if (s)
+                               return return_nested_in (found, s);
+                       return found;
+               }
+       }
+       return NULL;
+}
+
 MonoClass *
 mono_class_from_name (MonoImage *image, const char* name_space, const char *name)
 {
        GHashTable *nspace_table;
-       guint32 token;
+       MonoImage *loaded_image;
+       guint32 token = 0;
+       MonoClass *class;
+       char *nested;
+       char buf [1024];
+
+       if ((nested = strchr (name, '/'))) {
+               int pos = nested - name;
+               int len = strlen (name);
+               if (len > 1023)
+                       return NULL;
+               memcpy (buf, name, len + 1);
+               buf [pos] = 0;
+               nested = buf + pos + 1;
+               name = buf;
+       }
+
+       mono_loader_lock ();
 
        nspace_table = g_hash_table_lookup (image->name_cache, name_space);
-       if (!nspace_table)
-               return 0;
-       token = GPOINTER_TO_UINT (g_hash_table_lookup (nspace_table, name));
-       
-       if (!token) {
-               /*g_warning ("token not found for %s.%s in image %s", name_space, name, image->name);*/
+
+       if (nspace_table)
+               token = GPOINTER_TO_UINT (g_hash_table_lookup (nspace_table, name));
+
+       mono_loader_unlock ();
+
+       if (!token)
                return NULL;
+
+       if (mono_metadata_token_table (token) == MONO_TABLE_EXPORTEDTYPE) {
+               MonoTableInfo  *t = &image->tables [MONO_TABLE_EXPORTEDTYPE];
+               guint32 cols [MONO_EXP_TYPE_SIZE];
+               guint32 idx, impl;
+
+               idx = mono_metadata_token_index (token);
+
+               mono_metadata_decode_row (t, idx - 1, cols, MONO_EXP_TYPE_SIZE);
+
+               impl = cols [MONO_EXP_TYPE_IMPLEMENTATION];
+               if ((impl & IMPLEMENTATION_MASK) == IMPLEMENTATION_FILE) {
+                       loaded_image = mono_assembly_load_module (image->assembly, impl >> IMPLEMENTATION_BITS);
+                       if (!loaded_image)
+                               return NULL;
+                       class = mono_class_from_name (loaded_image, name_space, name);
+                       if (nested)
+                               return return_nested_in (class, nested);
+                       return class;
+               } else {
+                       g_error ("not yet implemented");
+               }
        }
 
        token = MONO_TOKEN_TYPE_DEF | token;
 
-       return mono_class_get (image, token);
+       class = mono_class_get (image, token);
+       if (nested)
+               return return_nested_in (class, nested);
+       return class;
+}
+
+gboolean
+mono_class_is_subclass_of (MonoClass *klass, MonoClass *klassc, 
+                                                  gboolean check_interfaces)
+{
+       if (check_interfaces && (klassc->flags & TYPE_ATTRIBUTE_INTERFACE) && !(klass->flags & TYPE_ATTRIBUTE_INTERFACE)) {
+               if ((klassc->interface_id <= klass->max_interface_id) &&
+                       (klass->interface_offsets [klassc->interface_id] >= 0))
+                       return TRUE;
+       } else if (check_interfaces && (klassc->flags & TYPE_ATTRIBUTE_INTERFACE) && (klass->flags & TYPE_ATTRIBUTE_INTERFACE)) {
+               int i;
+
+               for (i = 0; i < klass->interface_count; i ++) {
+                       MonoClass *ic =  klass->interfaces [i];
+                       if (ic == klassc)
+                               return TRUE;
+               }
+       } else {
+               if (!(klass->flags & TYPE_ATTRIBUTE_INTERFACE) && mono_class_has_parent (klass, klassc))
+                       return TRUE;
+       }
+
+       /* 
+        * MS.NET thinks interfaces are a subclass of Object, so we think it as
+        * well.
+        */
+       if (klassc == mono_defaults.object_class)
+               return TRUE;
+       
+       return FALSE;
+}
+
+gboolean
+mono_class_is_assignable_from (MonoClass *klass, MonoClass *oklass)
+{
+       if (!klass->inited)
+               mono_class_init (klass);
+
+       if (!oklass->inited)
+               mono_class_init (oklass);
+
+       if (klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
+               if ((klass->interface_id <= oklass->max_interface_id) &&
+                   (oklass->interface_offsets [klass->interface_id] != -1))
+                       return TRUE;
+       } else
+               if (klass->rank) {
+                       MonoClass *eclass, *eoclass;
+
+                       if (oklass->rank != klass->rank)
+                               return FALSE;
+
+                       /* vectors vs. one dimensional arrays */
+                       if (oklass->byval_arg.type != klass->byval_arg.type)
+                               return FALSE;
+
+                       eclass = klass->cast_class;
+                       eoclass = oklass->cast_class;
+
+
+                       /* 
+                        * a is b does not imply a[] is b[] when a is a valuetype, and
+                        * b is a reference type.
+                        */
+
+                       if (eoclass->valuetype) {
+                               if ((eclass == mono_defaults.enum_class) || 
+                                       (eclass == mono_defaults.enum_class->parent) ||
+                                       (eclass == mono_defaults.object_class))
+                                       return FALSE;
+                       }
+
+                       return mono_class_is_assignable_from (klass->cast_class, oklass->cast_class);
+               }
+       else
+               if (klass == mono_defaults.object_class)
+                       return TRUE;
+
+       return mono_class_has_parent (oklass, klass);
+}      
+
+/*
+ * mono_class_needs_cctor_run:
+ *
+ *  Determines whenever the class has a static constructor and whenever it
+ * needs to be called when executing CALLER.
+ */
+gboolean
+mono_class_needs_cctor_run (MonoClass *klass, MonoMethod *caller)
+{
+       int i;
+       MonoMethod *method;
+       
+       for (i = 0; i < klass->method.count; ++i) {
+               method = klass->methods [i];
+               if ((method->flags & METHOD_ATTRIBUTE_SPECIAL_NAME) && 
+                   (strcmp (".cctor", method->name) == 0)) {
+                       if (caller == method)
+                               return FALSE;
+                       return TRUE;
+               }
+       }
+       return FALSE;
 }
 
 /*
@@ -1748,7 +2540,9 @@ handle_enum:
        case MONO_TYPE_STRING:
        case MONO_TYPE_OBJECT:
        case MONO_TYPE_SZARRAY:
-       case MONO_TYPE_ARRAY:    
+       case MONO_TYPE_ARRAY: 
+       case MONO_TYPE_VAR:
+       case MONO_TYPE_MVAR:   
                return sizeof (gpointer);
        case MONO_TYPE_I8:
        case MONO_TYPE_U8:
@@ -1781,7 +2575,7 @@ mono_array_element_size (MonoClass *ac)
 gpointer
 mono_ldtoken (MonoImage *image, guint32 token, MonoClass **handle_class)
 {
-       if (image->assembly->dynamic) {
+       if (image->dynamic) {
                gpointer obj = mono_lookup_dynamic_token (image, token);
 
                switch (token & 0xff000000) {
@@ -1829,11 +2623,37 @@ mono_ldtoken (MonoImage *image, guint32 token, MonoClass **handle_class)
                class = mono_class_get (image, MONO_TOKEN_TYPE_DEF | type);
                mono_class_init (class);
                if (handle_class)
-                               *handle_class = mono_defaults.fieldhandle_class;
+                       *handle_class = mono_defaults.fieldhandle_class;
                return mono_class_get_field (class, token);
        }
-       case MONO_TOKEN_METHOD_DEF:
-       case MONO_TOKEN_MEMBER_REF:
+       case MONO_TOKEN_METHOD_DEF: {
+               MonoMethod *meth;
+               meth = mono_get_method (image, token, NULL);
+               if (handle_class)
+                       *handle_class = mono_defaults.methodhandle_class;
+               return meth;
+       }
+       case MONO_TOKEN_MEMBER_REF: {
+               guint32 cols [MONO_MEMBERREF_SIZE];
+               const char *sig;
+               mono_metadata_decode_row (&image->tables [MONO_TABLE_MEMBERREF], mono_metadata_token_index (token) - 1, cols, MONO_MEMBERREF_SIZE);
+               sig = mono_metadata_blob_heap (image, cols [MONO_MEMBERREF_SIGNATURE]);
+               mono_metadata_decode_blob_size (sig, &sig);
+               if (*sig == 0x6) { /* it's a field */
+                       MonoClass *klass;
+                       MonoClassField *field;
+                       field = mono_field_from_token (image, token, &klass);
+                       if (handle_class)
+                               *handle_class = mono_defaults.fieldhandle_class;
+                       return field;
+               } else {
+                       MonoMethod *meth;
+                       meth = mono_get_method (image, token, NULL);
+                       if (handle_class)
+                               *handle_class = mono_defaults.methodhandle_class;
+                       return meth;
+               }
+       }
        default:
                g_warning ("Unknown token 0x%08x in ldtoken", token);
                break;
@@ -1858,3 +2678,6 @@ mono_lookup_dynamic_token (MonoImage *image, guint32 token)
 {
        return lookup_dynamic (image, token);
 }
+
+
+