Revert patch as it introduced a regression while building: ** ERROR **: file metadat...
[mono.git] / mono / metadata / class.c
index d0d666b025c5d06438719f050207346d0c080a5d..f8fb5f366d850bcddc3306561991943c44645cc8 100644 (file)
@@ -41,7 +41,7 @@ MonoStats mono_stats;
 gboolean mono_print_vtable = FALSE;
 
 static MonoClass * mono_class_create_from_typedef (MonoImage *image, guint32 type_token);
-static void mono_class_create_generic_2 (MonoGenericClass *gclass);
+static void mono_class_create_generic (MonoInflatedGenericClass *gclass);
 static gboolean mono_class_get_cached_class_info (MonoClass *klass, MonoCachedClassInfo *res);
 
 void (*mono_debugger_start_class_init_func) (MonoClass *klass) = NULL;
@@ -74,11 +74,27 @@ mono_class_from_typeref (MonoImage *image, guint32 type_token)
        case MONO_RESOLTION_SCOPE_TYPEREF: {
                MonoClass *enclosing = mono_class_from_typeref (image, MONO_TOKEN_TYPE_REF | idx);
                GList *tmp;
-               mono_class_init (enclosing);
-               for (tmp = enclosing->nested_classes; tmp; tmp = tmp->next) {
-                       res = tmp->data;
-                       if (strcmp (res->name, name) == 0)
-                               return res;
+
+               if (enclosing->inited) {
+                       /* Micro-optimization: don't scan the metadata tables if enclosing is already inited */
+                       for (tmp = enclosing->nested_classes; tmp; tmp = tmp->next) {
+                               res = tmp->data;
+                               if (strcmp (res->name, name) == 0)
+                                       return res;
+                       }
+               } else {
+                       /* Don't call mono_class_init as we might've been called by it recursively */
+                       int i = mono_metadata_nesting_typedef (enclosing->image, enclosing->type_token, 1);
+                       while (i) {
+                               guint32 class_nested = mono_metadata_decode_row_col (&enclosing->image->tables [MONO_TABLE_NESTEDCLASS], i - 1, MONO_NESTED_CLASS_NESTED);
+                               guint32 string_offset = mono_metadata_decode_row_col (&enclosing->image->tables [MONO_TABLE_TYPEDEF], class_nested - 1, MONO_TYPEDEF_NAME);
+                               const char *nname = mono_metadata_string_heap (enclosing->image, string_offset);
+
+                               if (strcmp (nname, name) == 0)
+                                       return mono_class_create_from_typedef (enclosing->image, MONO_TOKEN_TYPE_DEF | class_nested);
+
+                               i = mono_metadata_nesting_typedef (enclosing->image, enclosing->type_token, i + 1);
+                       }
                }
                g_warning ("TypeRef ResolutionScope not yet handled (%d)", idx);
                return NULL;
@@ -107,10 +123,41 @@ dup_type (MonoType* t, const MonoType *original)
        r->byref = original->byref;
        if (t->type == MONO_TYPE_PTR)
                t->data.type = dup_type (t->data.type, original->data.type);
+       else if (t->type == MONO_TYPE_ARRAY)
+               t->data.array = mono_dup_array_type (t->data.array);
+       else if (t->type == MONO_TYPE_FNPTR)
+               t->data.method = mono_metadata_signature_deep_dup (t->data.method);
        mono_stats.generics_metadata_size += sizeof (MonoType);
        return r;
 }
 
+/* Copy everything mono_metadata_free_array free. */
+MonoArrayType *
+mono_dup_array_type (MonoArrayType *a)
+{
+       a = g_memdup (a, sizeof (MonoArrayType));
+       if (a->sizes)
+               a->sizes = g_memdup (a->sizes, a->numsizes * sizeof (int));
+       if (a->lobounds)
+               a->lobounds = g_memdup (a->lobounds, a->numlobounds * sizeof (int));
+       return a;
+}
+
+/* Copy everything mono_metadata_free_method_signature free. */
+MonoMethodSignature*
+mono_metadata_signature_deep_dup (MonoMethodSignature *sig)
+{
+       int i;
+       
+       sig = mono_metadata_signature_dup (sig);
+       
+       sig->ret = dup_type (sig->ret, sig->ret);
+       for (i = 0; i < sig->param_count; ++i)
+               sig->params [i] = dup_type (sig->params [i], sig->params [i]);
+       
+       return sig;
+}
+
 static void
 _mono_type_get_assembly_name (MonoClass *klass, GString *str)
 {
@@ -155,15 +202,25 @@ mono_type_get_name_recurse (MonoType *type, GString *str, gboolean is_recursed,
                        MONO_TYPE_NAME_FORMAT_FULL_NAME : format;
 
                mono_type_get_name_recurse (
-                       &type->data.klass->byval_arg, str, FALSE, format);
+                       &type->data.klass->byval_arg, str, FALSE, nested_format);
                g_string_append (str, "[]");
+               if (format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED)
+                       _mono_type_get_assembly_name (type->data.klass, str);
                break;
        }
-       case MONO_TYPE_PTR:
+       case MONO_TYPE_PTR: {
+               MonoTypeNameFormat nested_format;
+
+               nested_format = format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED ?
+                       MONO_TYPE_NAME_FORMAT_FULL_NAME : format;
+
                mono_type_get_name_recurse (
-                       type->data.type, str, FALSE, format);
-               g_string_append_c (str, '*');
+                       type->data.type, str, FALSE, nested_format);
+               g_string_append (str, "*");
+               if (format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED)
+                       _mono_type_get_assembly_name (type->data.klass, str);
                break;
+       }
        default:
                klass = mono_class_from_mono_type (type);
                if (klass->nested_in) {
@@ -173,8 +230,7 @@ mono_type_get_name_recurse (MonoType *type, GString *str, gboolean is_recursed,
                                g_string_append_c (str, '.');
                        else
                                g_string_append_c (str, '+');
-               }
-               if (*klass->name_space) {
+               } else if (*klass->name_space) {
                        g_string_append (str, klass->name_space);
                        g_string_append_c (str, '.');
                }
@@ -200,13 +256,17 @@ mono_type_get_name_recurse (MonoType *type, GString *str, gboolean is_recursed,
                        else
                                g_string_append_c (str, '[');
                        for (i = 0; i < gclass->inst->type_argc; i++) {
+                               MonoType *t = gclass->inst->type_argv [i];
+
                                if (i)
                                        g_string_append_c (str, ',');
-                               if (nested_format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED)
+                               if ((nested_format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED) &&
+                                   (t->type != MONO_TYPE_VAR) && (type->type != MONO_TYPE_MVAR))
                                        g_string_append_c (str, '[');
                                mono_type_get_name_recurse (
                                        gclass->inst->type_argv [i], str, FALSE, nested_format);
-                               if (nested_format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED)
+                               if ((nested_format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED) &&
+                                   (t->type != MONO_TYPE_VAR) && (type->type != MONO_TYPE_MVAR))
                                        g_string_append_c (str, ']');
                        }
                        if (format == MONO_TYPE_NAME_FORMAT_IL) 
@@ -249,7 +309,14 @@ mono_type_get_name_recurse (MonoType *type, GString *str, gboolean is_recursed,
 char*
 mono_type_get_name_full (MonoType *type, MonoTypeNameFormat format)
 {
-       GString* result = g_string_new ("");
+       GString* result;
+
+       if (format == MONO_TYPE_NAME_FORMAT_FULL_NAME &&
+           ((type->type == MONO_TYPE_VAR) || (type->type == MONO_TYPE_MVAR) ||
+            ((type->type == MONO_TYPE_GENERICINST) && type->data.generic_class->inst->is_open)))
+               return NULL;
+
+       result = g_string_new ("");
 
        mono_type_get_name_recurse (type, result, FALSE, format);
 
@@ -314,20 +381,26 @@ mono_class_is_open_constructed_type (MonoType *t)
 static MonoGenericClass *
 inflate_generic_class (MonoGenericClass *ogclass, MonoGenericContext *context)
 {
+       MonoInflatedGenericClass *igclass;
        MonoGenericClass *ngclass, *cached;
 
        if (ogclass->is_dynamic) {
                MonoDynamicGenericClass *dgclass = g_new0 (MonoDynamicGenericClass, 1);
-               ngclass = &dgclass->generic_class;
+               igclass = &dgclass->generic_class;
+               ngclass = &igclass->generic_class;
+               ngclass->is_inflated = 1;
                ngclass->is_dynamic = 1;
-       } else
-               ngclass = g_new0 (MonoGenericClass, 1);
+       } else {
+               igclass = g_new0 (MonoInflatedGenericClass, 1);
+               ngclass = &igclass->generic_class;
+               ngclass->is_inflated = 1;
+       }
 
        *ngclass = *ogclass;
 
        ngclass->inst = mono_metadata_inflate_generic_inst (ogclass->inst, context);
 
-       ngclass->klass = NULL;
+       igclass->klass = NULL;
 
        ngclass->context = g_new0 (MonoGenericContext, 1);
        ngclass->context->container = context->container;
@@ -337,12 +410,11 @@ inflate_generic_class (MonoGenericClass *ogclass, MonoGenericContext *context)
        cached = mono_metadata_lookup_generic_class (ngclass);
        mono_loader_unlock ();
        if (cached) {
+               g_free (ngclass->context);
                g_free (ngclass);
                return cached;
        }
 
-       mono_class_create_generic (ngclass);
-
        return ngclass;
 }
 
@@ -411,6 +483,14 @@ inflate_generic_type (MonoType *type, MonoGenericContext *context)
        return NULL;
 }
 
+MonoInflatedGenericClass*
+mono_get_inflated_generic_class (MonoGenericClass *gclass)
+{
+       g_assert (gclass->is_inflated);
+       mono_class_create_generic ((MonoInflatedGenericClass *) gclass);
+       return (MonoInflatedGenericClass *) gclass;
+}
+
 MonoType*
 mono_class_inflate_generic_type (MonoType *type, MonoGenericContext *context)
 {
@@ -444,6 +524,7 @@ mono_class_inflate_generic_signature (MonoImage *image, MonoMethodSignature *sig
        res->hasthis = sig->hasthis;
        res->explicit_this = sig->explicit_this;
        res->call_convention = sig->call_convention;
+       res->pinvoke = sig->pinvoke;
        res->generic_param_count = sig->generic_param_count;
        res->sentinelpos = sig->sentinelpos;
        res->has_type_parameters = is_open;
@@ -492,15 +573,10 @@ inflate_generic_context (MonoGenericContext *context, MonoGenericContext *inflat
 }
 
 MonoMethod*
-mono_class_inflate_generic_method (MonoMethod *method, MonoGenericContext *context,
-                                  MonoClass *klass)
+mono_class_inflate_generic_method (MonoMethod *method, MonoGenericContext *context)
 {
        MonoMethodInflated *result;
 
-       if ((method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
-           (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL))
-               return method;
-
        if (method->is_inflated || mono_method_signature_full (method, context)->is_inflated) {
                MonoMethodInflated *imethod = (MonoMethodInflated *) method;
 
@@ -559,14 +635,14 @@ mono_get_inflated_method (MonoMethod *method)
 }
 
 /** 
- * class_compute_field_layout:
+ * mono_class_setup_fields:
  * @m: pointer to the metadata.
  * @class: The class to initialize
  *
  * Initializes the class->fields.
  */
 static void
-class_compute_field_layout (MonoClass *class)
+mono_class_setup_fields (MonoClass *class)
 {
        MonoImage *m = class->image; 
        const int top = class->field.count;
@@ -581,9 +657,12 @@ class_compute_field_layout (MonoClass *class)
        if (class->size_inited)
                return;
 
+       class->instance_size = 0;
+       class->class_size = 0;
+
        if (class->parent) {
                if (!class->parent->size_inited)
-                       class_compute_field_layout (class->parent);
+                       mono_class_setup_fields (class->parent);
                class->instance_size += class->parent->instance_size;
                class->min_align = class->parent->min_align;
                /* we use |= since it may have been set already */
@@ -708,10 +787,6 @@ class_compute_field_layout (MonoClass *class)
                class->instance_size = MAX (real_size, class->instance_size);
        }
 
-       if (class->generic_container ||
-           (class->generic_class && class->generic_class->inst->is_open))
-               return;
-
        mono_class_layout_fields (class);
 }
 
@@ -731,6 +806,10 @@ mono_class_layout_fields (MonoClass *class)
        gboolean gc_aware_layout = FALSE;
        MonoClassField *field;
 
+       if (class->generic_container ||
+           (class->generic_class && class->generic_class->inst->is_open))
+               return;
+
        /*
         * Enable GC aware auto layout: in this mode, reference
         * fields are grouped together inside objects, increasing collector 
@@ -747,6 +826,20 @@ mono_class_layout_fields (MonoClass *class)
                        gc_aware_layout = TRUE;
        }
 
+       for (i = 0; i < top; i++) {
+               MonoType *ftype;
+
+               field = &class->fields [i];
+
+               ftype = mono_type_get_underlying_type (field->type);
+               if (MONO_TYPE_IS_REFERENCE (ftype) || IS_GC_REFERENCE (ftype) || ((MONO_TYPE_ISSTRUCT (ftype) && mono_class_from_mono_type (ftype)->has_references))) {
+                       if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
+                               class->has_static_refs = TRUE;
+                       else
+                               class->has_references = TRUE;
+               }
+       }
+
        /*
         * Compute field layout and total size (not considering static fields)
         */
@@ -771,6 +864,7 @@ mono_class_layout_fields (MonoClass *class)
                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))
@@ -810,10 +904,6 @@ mono_class_layout_fields (MonoClass *class)
                                field->offset += align - 1;
                                field->offset &= ~(align - 1);
                                real_size = field->offset + size;
-                               if (MONO_TYPE_IS_REFERENCE (field->type) || IS_GC_REFERENCE (field->type))
-                                       class->has_references = TRUE;
-                               else if (field->type->type == MONO_TYPE_VALUETYPE && field->type->data.klass->has_references)
-                                       class->has_references = TRUE;
                        }
 
                        class->instance_size = MAX (real_size, class->instance_size);
@@ -828,6 +918,7 @@ mono_class_layout_fields (MonoClass *class)
                real_size = 0;
                for (i = 0; i < top; i++) {
                        int size, align;
+
                        field = &class->fields [i];
 
                        /*
@@ -840,10 +931,6 @@ mono_class_layout_fields (MonoClass *class)
                        if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
                                continue;
 
-                       if (MONO_TYPE_IS_REFERENCE (field->type) || IS_GC_REFERENCE (field->type))
-                               class->has_references = TRUE;
-                       else if (field->type->type == MONO_TYPE_VALUETYPE && field->type->data.klass->has_references)
-                               class->has_references = TRUE;
                        size = mono_type_size (field->type, &align);
                        
                        /*
@@ -870,6 +957,7 @@ mono_class_layout_fields (MonoClass *class)
         */
        for (i = 0; i < top; i++){
                int size, align;
+
                field = &class->fields [i];
                        
                if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC) || field->type->attrs & FIELD_ATTRIBUTE_LITERAL)
@@ -877,10 +965,6 @@ mono_class_layout_fields (MonoClass *class)
                if (mono_field_is_deleted (field))
                        continue;
 
-               if (MONO_TYPE_IS_REFERENCE (field->type) || IS_GC_REFERENCE (field->type))
-                       class->has_static_refs = TRUE;
-               else if (field->type->type == MONO_TYPE_VALUETYPE && field->type->data.klass->has_references)
-                       class->has_static_refs = TRUE;
                size = mono_type_size (field->type, &align);
                field->offset = class->class_size;
                field->offset += align - 1;
@@ -1255,6 +1339,7 @@ void
 mono_class_setup_vtable (MonoClass *class)
 {
        MonoMethod **overrides;
+       MonoGenericContext *context;
        int onum = 0;
 
        if (class->vtable)
@@ -1272,7 +1357,13 @@ mono_class_setup_vtable (MonoClass *class)
                return;
        }
 
-       overrides = mono_class_get_overrides (class->image, class->type_token, &onum);  
+       if (class->generic_class)
+               context = class->generic_class->context;
+       else
+               context = (MonoGenericContext *) class->generic_container;              
+
+       overrides = mono_class_get_overrides_full (
+               class->image, class->type_token, &onum, context);
        mono_class_setup_vtable_general (class, overrides, onum);
        g_free (overrides);
 
@@ -1282,15 +1373,16 @@ mono_class_setup_vtable (MonoClass *class)
 static void
 setup_generic_vtable (MonoClass *class, MonoMethod **overrides, int onum)
 {
-       MonoClass *gklass = class->generic_class->container_class;
-       MonoGenericContext *gcontext = class->generic_class->context;
+       MonoClass *gklass;
        int i;
 
+       gklass = class->generic_class->container_class;
+
        mono_class_init (gklass);
        class->vtable_size = gklass->vtable_size;
 
-       class->vtable = g_new0 (gpointer, class->vtable_size);
-       memcpy (class->vtable, gklass->vtable,  sizeof (gpointer) * class->vtable_size);
+       class->vtable = g_new0 (MonoMethod*, class->vtable_size);
+       memcpy (class->vtable, gklass->vtable,  sizeof (MonoMethod*) * class->vtable_size);
 
        for (i = 0; i < class->vtable_size; i++) {
                MonoMethod *m = class->vtable [i];
@@ -1298,14 +1390,14 @@ setup_generic_vtable (MonoClass *class, MonoMethod **overrides, int onum)
                if (!m)
                        continue;
 
-               m = mono_class_inflate_generic_method (m, gcontext, class);
-               class->vtable [i] = mono_get_inflated_method (m);
+               m = mono_class_inflate_generic_method (m, class->generic_class->context);
+               class->vtable [i] = m;
        }
 
        class->max_interface_id = gklass->max_interface_id;
-       class->interface_offsets = g_new0 (gpointer, gklass->max_interface_id + 1);
+       class->interface_offsets = g_new0 (gint, gklass->max_interface_id + 1);
        memcpy (class->interface_offsets, gklass->interface_offsets,
-               sizeof (gpointer) * (gklass->max_interface_id + 1));
+               sizeof (gint) * (gklass->max_interface_id + 1));
 }
 
 /*
@@ -1469,7 +1561,6 @@ mono_class_setup_vtable_general (MonoClass *class, MonoMethod **overrides, int o
                                        continue;
 
                                if (ic->generic_class) {
-                                       MonoClass *the_ic = ic->generic_class->container_class;
                                        the_cname = mono_type_get_name_full (&ic->byval_arg, MONO_TYPE_NAME_FORMAT_IL);
                                        cname = the_cname;
                                } else {
@@ -1725,6 +1816,7 @@ mono_class_init (MonoClass *class)
        static int finalize_slot = -1;
        static int ghc_slot = -1;
        MonoCachedClassInfo cached_info;
+       gboolean has_cached_info;
 
        g_assert (class);
 
@@ -1743,17 +1835,7 @@ mono_class_init (MonoClass *class)
        }
 
        if (class->init_pending) {
-               /*
-                * We might be called recursively from mono_class_from_typeref if
-                * one of our fields has a type which is a nested type of this class,
-                * and the compiler encodes it as a typeref, like older versions of
-                * MS ilasm do.
-                */
-               if (class->size_inited) {
-                       mono_loader_unlock ();
-                       return;
-               }
-
+               mono_loader_unlock ();
                /* this indicates a cyclic dependency */
                g_error ("pending init %s.%s\n", class->name_space, class->name);
        }
@@ -1771,16 +1853,17 @@ mono_class_init (MonoClass *class)
        mono_stats.initialized_class_count++;
 
        if (class->generic_class && !class->generic_class->is_dynamic) {
-               MonoGenericClass *gclass = class->generic_class;
-               MonoClass *gklass = gclass->container_class;
+               MonoInflatedGenericClass *gclass;
+               MonoClass *gklass;
+
+               gclass = mono_get_inflated_generic_class (class->generic_class);
+               gklass = gclass->generic_class.container_class;
 
                mono_stats.generic_class_count++;
 
                class->method = gklass->method;
                class->field = gklass->field;
 
-               mono_class_create_generic_2 (gclass);
-
                mono_class_init (gklass);
                mono_class_setup_methods (gklass);
                mono_class_setup_properties (gklass);
@@ -1793,27 +1876,11 @@ mono_class_init (MonoClass *class)
 
                for (i = 0; i < class->method.count; i++) {
                        MonoMethod *inflated = mono_class_inflate_generic_method (
-                               gklass->methods [i], gclass->context, gclass->klass);
+                               gklass->methods [i], gclass->generic_class.context);
 
                        class->methods [i] = mono_get_inflated_method (inflated);
                }
 
-#if 0
-               g_assert (class->field.count == gklass->field.count);
-               class->fields = g_new0 (MonoClassField, class->field.count);
-
-               for (i = 0; i < class->field.count; i++) {
-                       MonoInflatedField *ifield = g_new0 (MonoInflatedField, 1);
-                       ifield->generic_type = gklass->fields [i].type;
-
-                       class->fields [i] = gklass->fields [i];
-                       class->fields [i].generic_info = ifield;
-                       class->fields [i].parent = class;
-                       class->fields [i].type = mono_class_inflate_generic_type (
-                               class->fields [i].type, gclass->context);
-               }
-#endif
-
                class->property = gklass->property;
                class->properties = g_new0 (MonoProperty, class->property.count);
 
@@ -1824,10 +1891,10 @@ mono_class_init (MonoClass *class)
 
                        if (prop->get)
                                prop->get = mono_class_inflate_generic_method (
-                                       prop->get, gclass->context, gclass->klass);
+                                       prop->get, gclass->generic_class.context);
                        if (prop->set)
                                prop->set = mono_class_inflate_generic_method (
-                                       prop->set, gclass->context, gclass->klass);
+                                       prop->set, gclass->generic_class.context);
 
                        prop->parent = class;
                }
@@ -1838,7 +1905,9 @@ mono_class_init (MonoClass *class)
        if (class->parent && !class->parent->inited)
                mono_class_init (class->parent);
 
-       if (!class->generic_class) {
+       has_cached_info = mono_class_get_cached_class_info (class, &cached_info);
+
+       if (!class->generic_class && (!has_cached_info || (has_cached_info && cached_info.has_nested_classes))) {
                i = mono_metadata_nesting_typedef (class->image, class->type_token, 1);
                while (i) {
                        MonoClass* nclass;
@@ -1854,8 +1923,18 @@ mono_class_init (MonoClass *class)
        /*
         * Computes the size used by the fields, and their locations
         */
-       if (!class->size_inited)
-               class_compute_field_layout (class);
+       if (has_cached_info) {
+               class->instance_size = cached_info.instance_size;
+               class->class_size = cached_info.class_size;
+               class->packing_size = cached_info.packing_size;
+               class->min_align = cached_info.min_align;
+               class->blittable = cached_info.blittable;
+               class->has_references = cached_info.has_references;
+               class->has_static_refs = cached_info.has_static_refs;
+       }
+       else
+               if (!class->size_inited)
+                       mono_class_setup_fields (class);
 
        /* initialize method pointers */
        if (class->rank) {
@@ -1897,22 +1976,6 @@ mono_class_init (MonoClass *class)
 
        mono_class_setup_supertypes (class);
 
-       if (MONO_CLASS_IS_INTERFACE (class)) {
-               class->init_pending = 0;
-               class->inited = 1;
-               /* 
-                * class->interface_offsets is needed for the castclass/isinst code, so
-                * we have to setup them for interfaces, too.
-                */
-               setup_interface_offsets (class, 0);
-               mono_loader_unlock ();
-
-               if (mono_debugger_class_init_func)
-                       mono_debugger_class_init_func (class);
-
-               return;
-       }
-
        if (!default_ghc) {
                if (class == mono_defaults.object_class) { 
                        mono_class_setup_vtable (class);                       
@@ -1953,7 +2016,7 @@ mono_class_init (MonoClass *class)
         * If possible, avoid the creation of the generic vtable by requesting
         * cached info from the runtime.
         */
-       if (mono_class_get_cached_class_info (class, &cached_info)) {
+       if (has_cached_info) {
                guint32 cur_slot = 0;
 
                class->vtable_size = cached_info.vtable_size;
@@ -1973,7 +2036,10 @@ mono_class_init (MonoClass *class)
        
                class->ghcimpl = 1;
                if (class->parent) { 
-                       if (class->vtable [ghc_slot] == default_ghc) {
+                       MonoMethod *cmethod = class->vtable [ghc_slot];
+                       if (cmethod->is_inflated)
+                               cmethod = ((MonoMethodInflated*)cmethod)->declaring;
+                       if (cmethod == default_ghc) {
                                class->ghcimpl = 0;
                        }
                }
@@ -1981,8 +2047,12 @@ mono_class_init (MonoClass *class)
                /* Object::Finalize should have empty implemenatation */
                class->has_finalize = 0;
                if (class->parent) { 
-                       if (class->vtable [finalize_slot] != default_finalize)
+                       MonoMethod *cmethod = class->vtable [finalize_slot];
+                       if (cmethod->is_inflated)
+                               cmethod = ((MonoMethodInflated*)cmethod)->declaring;
+                       if (cmethod != default_finalize) {
                                class->has_finalize = 1;
+                       }
                }
 
                for (i = 0; i < class->method.count; ++i) {
@@ -2000,6 +2070,14 @@ mono_class_init (MonoClass *class)
        
        mono_loader_unlock ();
 
+       if (MONO_CLASS_IS_INTERFACE (class)) {
+               /* 
+                * class->interface_offsets is needed for the castclass/isinst code, so
+                * we have to setup them for interfaces, too.
+                */
+               setup_interface_offsets (class, 0);
+       }
+
        if (mono_debugger_class_init_func)
                mono_debugger_class_init_func (class);
 }
@@ -2148,9 +2226,6 @@ mono_class_setup_parent (MonoClass *class, MonoClass *parent)
                return;
        }
 
-       if (parent && parent->generic_class)
-               mono_class_create_generic_2 (parent->generic_class);
-
        if (!MONO_CLASS_IS_INTERFACE (class)) {
                class->parent = parent;
 
@@ -2273,14 +2348,20 @@ get_shared_inst (MonoGenericContainer *container)
 MonoGenericClass *
 mono_get_shared_generic_class (MonoGenericContainer *container, gboolean is_dynamic)
 {
+       MonoInflatedGenericClass *igclass;
        MonoGenericClass *gclass;
 
        if (is_dynamic) {
                MonoDynamicGenericClass *dgclass = g_new0 (MonoDynamicGenericClass, 1);
-               gclass = &dgclass->generic_class;
+               igclass = &dgclass->generic_class;
+               gclass = &igclass->generic_class;
+               gclass->is_inflated = 1;
                gclass->is_dynamic = 1;
-       } else
-               gclass = g_new0 (MonoGenericClass, 1);
+       } else {
+               igclass = g_new0 (MonoInflatedGenericClass, 1);
+               gclass = &igclass->generic_class;
+               gclass->is_inflated = 1;
+       }
 
        gclass->context = &container->context;
        gclass->container_class = container->klass;
@@ -2295,7 +2376,7 @@ mono_get_shared_generic_class (MonoGenericContainer *container, gboolean is_dyna
                }
        }
 
-       gclass->klass = container->klass;
+       igclass->klass = container->klass;
 
        return gclass;
 }
@@ -2407,7 +2488,7 @@ mono_class_create_from_typedef (MonoImage *image, guint32 type_token)
        }
 
        if (class->enumtype)
-               class_compute_field_layout (class);
+               mono_class_setup_fields (class);
 
        if ((type_token = mono_metadata_nested_in_typedef (image, type_token)))
                class->nested_in = mono_class_create_from_typedef (image, type_token);
@@ -2420,16 +2501,21 @@ mono_class_create_from_typedef (MonoImage *image, guint32 type_token)
        return class;
 }
 
-void
-mono_class_create_generic (MonoGenericClass *gclass)
+static void
+mono_class_create_generic (MonoInflatedGenericClass *gclass)
 {
        MonoClass *klass, *gklass;
+       int i;
+
+       if (gclass->is_initialized)
+               return;
+       gclass->is_initialized = TRUE;
 
        if (!gclass->klass)
                gclass->klass = g_malloc0 (sizeof (MonoClass));
        klass = gclass->klass;
 
-       gklass = gclass->container_class;
+       gklass = gclass->generic_class.container_class;
 
        klass->nested_in = gklass->nested_in;
 
@@ -2438,15 +2524,15 @@ mono_class_create_generic (MonoGenericClass *gclass)
        klass->image = gklass->image;
        klass->flags = gklass->flags;
 
-       klass->generic_class = gclass;
+       klass->generic_class = &gclass->generic_class;
 
        klass->this_arg.type = klass->byval_arg.type = MONO_TYPE_GENERICINST;
-       klass->this_arg.data.generic_class = klass->byval_arg.data.generic_class = gclass;
+       klass->this_arg.data.generic_class = klass->byval_arg.data.generic_class = &gclass->generic_class;
        klass->this_arg.byref = TRUE;
 
        klass->cast_class = klass->element_class = klass;
 
-       if (gclass->is_dynamic) {
+       if (gclass->generic_class.is_dynamic) {
                klass->instance_size = gklass->instance_size;
                klass->class_size = gklass->class_size;
                klass->size_inited = 1;
@@ -2456,23 +2542,13 @@ mono_class_create_generic (MonoGenericClass *gclass)
 
                mono_class_setup_supertypes (klass);
        }
-}
-
-static void
-mono_class_create_generic_2 (MonoGenericClass *gclass)
-{
-       MonoClass *klass, *gklass;
-       GList *list;
-       int i;
-
-       klass = gclass->klass;
-       gklass = gclass->container_class;
 
        klass->interface_count = gklass->interface_count;
        klass->interfaces = g_new0 (MonoClass *, klass->interface_count);
        for (i = 0; i < klass->interface_count; i++) {
                MonoType *it = &gklass->interfaces [i]->byval_arg;
-               MonoType *inflated = mono_class_inflate_generic_type (it, gclass->context);
+               MonoType *inflated = mono_class_inflate_generic_type (
+                       it, gclass->generic_class.context);
                klass->interfaces [i] = mono_class_from_mono_type (inflated);
        }
 
@@ -2487,10 +2563,14 @@ mono_class_create_generic_2 (MonoGenericClass *gclass)
                i = mono_metadata_nesting_typedef (klass->image, gklass->type_token, i + 1);
        }
 
-       if (gclass->parent)
-               klass->parent = mono_class_from_mono_type (gclass->parent);
-       else if (gklass->parent) {
-               MonoType *inflated = mono_class_inflate_generic_type (&gklass->parent->byval_arg, gclass->context);
+       if (gclass->generic_class.is_dynamic) {
+               MonoDynamicGenericClass *dgclass = (MonoDynamicGenericClass *) gclass;
+
+               if (dgclass->parent)
+                       klass->parent = mono_class_from_mono_type (dgclass->parent);
+       } else if (gklass->parent) {
+               MonoType *inflated = mono_class_inflate_generic_type (
+                       &gklass->parent->byval_arg, gclass->generic_class.context);
 
                klass->parent = mono_class_from_mono_type (inflated);
        }
@@ -2517,7 +2597,10 @@ mono_class_from_generic_parameter (MonoGenericParam *param, MonoImage *image, gb
        if ((count > 0) && !MONO_CLASS_IS_INTERFACE (param->constraints [0])) {
                klass->parent = param->constraints [0];
                pos++;
-       }
+       } else if (param->flags & GENERIC_PARAMETER_ATTRIBUTE_VALUE_TYPE_CONSTRAINT)
+               klass->parent = mono_class_from_name (mono_defaults.corlib, "System", "ValueType");
+       else
+               klass->parent = mono_defaults.object_class;
 
        if (count - pos > 0) {
                klass->interface_count = count - pos;
@@ -2531,6 +2614,7 @@ mono_class_from_generic_parameter (MonoGenericParam *param, MonoImage *image, gb
        klass->name = param->name;
        klass->name_space = "";
        klass->image = image;
+       klass->inited = TRUE;
        klass->cast_class = klass->element_class = klass;
        klass->enum_basetype = &klass->element_class->byval_arg;
        klass->flags = TYPE_ATTRIBUTE_PUBLIC;
@@ -2539,7 +2623,7 @@ mono_class_from_generic_parameter (MonoGenericParam *param, MonoImage *image, gb
        klass->this_arg.data.generic_param = klass->byval_arg.data.generic_param = param;
        klass->this_arg.byref = TRUE;
 
-       mono_class_init (klass);
+       mono_class_setup_supertypes (klass);
 
        return klass;
 }
@@ -2562,6 +2646,7 @@ my_mono_class_from_generic_parameter (MonoGenericParam *param, gboolean is_mvar)
                klass->name = g_strdup_printf (is_mvar ? "!!%d" : "!%d", param->num);
        klass->name_space = "";
        klass->image = mono_defaults.corlib;
+       klass->inited = TRUE;
        klass->cast_class = klass->element_class = klass;
        klass->enum_basetype = &klass->element_class->byval_arg;
        klass->flags = TYPE_ATTRIBUTE_PUBLIC;
@@ -2570,7 +2655,7 @@ my_mono_class_from_generic_parameter (MonoGenericParam *param, gboolean is_mvar)
        klass->this_arg.data.generic_param = klass->byval_arg.data.generic_param = param;
        klass->this_arg.byref = TRUE;
 
-       mono_class_init (klass);
+       mono_class_setup_supertypes (klass);
 
        return klass;
 }
@@ -2712,9 +2797,12 @@ mono_class_from_mono_type (MonoType *type)
        case MONO_TYPE_CLASS:
        case MONO_TYPE_VALUETYPE:
                return type->data.klass;
-       case MONO_TYPE_GENERICINST:
-               g_assert (type->data.generic_class->klass);
-               return type->data.generic_class->klass;
+       case MONO_TYPE_GENERICINST: {
+               MonoInflatedGenericClass *gclass;
+               gclass = mono_get_inflated_generic_class (type->data.generic_class);
+               g_assert (gclass->klass);
+               return gclass->klass;
+       }
        case MONO_TYPE_VAR:
                return my_mono_class_from_generic_parameter (type->data.generic_param, FALSE);
        case MONO_TYPE_MVAR:
@@ -2742,7 +2830,7 @@ 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->eklass, type->data.array->rank);
+               class = mono_bounded_array_class_get (type->data.array->eklass, type->data.array->rank, TRUE);
                break;
        case MONO_TYPE_SZARRAY:
                class = mono_array_class_get (type->data.klass, 1);
@@ -2750,10 +2838,13 @@ mono_class_create_from_typespec (MonoImage *image, guint32 type_spec,
        case MONO_TYPE_PTR:
                class = mono_ptr_class_get (type->data.type);
                break;
-       case MONO_TYPE_GENERICINST:
-               g_assert (type->data.generic_class->klass);
-               class = type->data.generic_class->klass;
+       case MONO_TYPE_GENERICINST: {
+               MonoInflatedGenericClass *gclass;
+               gclass = mono_get_inflated_generic_class (type->data.generic_class);
+               g_assert (gclass->klass);
+               class = gclass->klass;
                break;
+       }
        default:
                /* it seems any type can be stored in TypeSpec as well */
                class = mono_class_from_mono_type (type);
@@ -2809,9 +2900,21 @@ mono_bounded_array_class_get (MonoClass *eclass, guint32 rank, gboolean bounded)
        }
 
        /* for the building corlib use System.Array from it */
-       if (image->assembly && image->assembly->dynamic && strcmp (image->assembly_name, "mscorlib") == 0) {
+       if (image->assembly && image->assembly->dynamic && image->assembly_name && strcmp (image->assembly_name, "mscorlib") == 0) {
                parent = mono_class_from_name (image, "System", "Array");
                corlib_type = TRUE;
+       } else if (mono_defaults.generic_array_class) {
+               MonoType *inflated, **args;
+
+               args = g_new0 (MonoType *, 1);
+               args [0] = &eclass->byval_arg;
+
+               inflated = mono_class_bind_generic_parameters (
+                       &mono_defaults.generic_array_class->byval_arg, 1, args);
+               parent = mono_class_from_mono_type (inflated);
+
+               if (!parent->inited)
+                       mono_class_init (parent);
        } else {
                parent = mono_defaults.array_class;
                if (!parent->inited)
@@ -2842,7 +2945,7 @@ mono_bounded_array_class_get (MonoClass *eclass, guint32 rank, gboolean bounded)
        if (eclass->generic_class)
                mono_class_init (eclass);
        if (!eclass->size_inited)
-               class_compute_field_layout (eclass);
+               mono_class_setup_fields (eclass);
        class->has_references = MONO_TYPE_IS_REFERENCE (&eclass->byval_arg) || eclass->has_references? TRUE: FALSE;
 
        class->rank = rank;
@@ -2905,8 +3008,6 @@ mono_class_instance_size (MonoClass *klass)
        if (!klass->size_inited)
                mono_class_init (klass);
 
-       g_assert (!klass->generic_container &&
-                 (!klass->generic_class || !klass->generic_class->inst->is_open));
        return klass->instance_size;
 }
 
@@ -2975,6 +3076,8 @@ mono_class_data_size (MonoClass *klass)
 static MonoClassField *
 mono_class_get_field_idx (MonoClass *class, int idx)
 {
+       mono_class_setup_fields (class);
+
        if (class->field.count){
                if ((idx >= class->field.first) && (idx < class->field.last)){
                        return &class->fields [idx - class->field.first];
@@ -3012,6 +3115,7 @@ mono_class_get_field_from_name (MonoClass *klass, const char *name)
        int i;
 
        while (klass) {
+               mono_class_setup_fields (klass);
                for (i = 0; i < klass->field.count; ++i) {
                        if (strcmp (name, klass->fields [i].name) == 0)
                                return &klass->fields [i];
@@ -3028,6 +3132,7 @@ mono_class_get_field_token (MonoClassField *field)
        int i;
 
        while (klass) {
+               mono_class_setup_fields (klass);
                for (i = 0; i < klass->field.count; ++i) {
                        if (&klass->fields [i] == field)
                                return mono_metadata_make_token (MONO_TABLE_FIELD, klass->field.first + i + 1);
@@ -3217,6 +3322,21 @@ mono_class_get_full (MonoImage *image, guint32 type_token, MonoGenericContext *c
        return mono_class_from_mono_type (inflated);
 }
 
+typedef struct {
+       gconstpointer key;
+       gpointer value;
+} FindUserData;
+
+static void
+find_nocase (gpointer key, gpointer value, gpointer user_data)
+{
+       char *name = (char*)key;
+       FindUserData *data = (FindUserData*)user_data;
+
+       if (!data->value && (g_strcasecmp (name, (char*)data->key) == 0))
+               data->value = value;
+}
+
 /**
  * mono_class_from_name_case:
  * @image: The MonoImage where the type is looked up in, or NULL for looking up in all loaded assemblies
@@ -3242,6 +3362,37 @@ mono_class_from_name_case (MonoImage *image, const char* name_space, const char
        const char *nspace;
        guint32 i, visib;
 
+       if (image->dynamic) {
+               guint32 token = 0;
+               FindUserData user_data;
+
+               mono_loader_lock ();
+
+               user_data.key = name_space;
+               user_data.value = NULL;
+               g_hash_table_foreach (image->name_cache, find_nocase, &user_data);
+
+               if (user_data.value) {
+                       GHashTable *nspace_table = (GHashTable*)user_data.value;
+
+                       user_data.key = name;
+                       user_data.value = NULL;
+
+                       g_hash_table_foreach (nspace_table, find_nocase, &user_data);
+                       
+                       if (user_data.value)
+                               token = GPOINTER_TO_UINT (user_data.value);
+               }
+
+               mono_loader_unlock ();
+               
+               if (token)
+                       return mono_class_get (image, MONO_TOKEN_TYPE_DEF | token);
+               else
+                       return NULL;
+
+       }
+
        /* add a cache if needed */
        for (i = 1; i <= t->rows; ++i) {
                mono_metadata_decode_row (t, i - 1, cols, MONO_TYPEDEF_SIZE);
@@ -3280,7 +3431,7 @@ return_nested_in (MonoClass *class, char *nested) {
 
 
 /**
- * mono_class_from_name_case:
+ * mono_class_from_name:
  * @image: The MonoImage where the type is looked up in, or NULL for looking up in all loaded assemblies
  * @name_space: the type namespace
  * @name: the type short name.
@@ -3388,14 +3539,15 @@ mono_class_is_subclass_of (MonoClass *klass, MonoClass *klassc,
        if (klassc == mono_defaults.object_class)
                return TRUE;
 
-       if (klass->generic_class) {
-               MonoType *parent = klass->generic_class->parent;
-               if (!parent)
+       if (klass->generic_class && klass->generic_class->is_dynamic) {
+               MonoDynamicGenericClass *dgclass = (MonoDynamicGenericClass *) klass->generic_class;
+
+               if (!dgclass->parent)
                        return FALSE;
 
-               if (mono_metadata_type_equal (parent, &klassc->byval_arg))
+               if (mono_metadata_type_equal (dgclass->parent, &klassc->byval_arg))
                        return TRUE;
-               klass = mono_class_from_mono_type (parent);
+               klass = mono_class_from_mono_type (dgclass->parent);
                goto again;
        }
        
@@ -3974,6 +4126,7 @@ mono_class_get_fields (MonoClass* klass, gpointer *iter)
        if (!klass->inited)
                mono_class_init (klass);
        if (!*iter) {
+               mono_class_setup_fields (klass);
                /* start from the first */
                if (klass->field.count) {
                        return *iter = &klass->fields [0];
@@ -4476,7 +4629,7 @@ mono_class_get_exception_for_failure (MonoClass *klass)
                MonoDomain *domain = mono_domain_get ();
                MonoSecurityManager* secman = mono_security_manager_get_methods ();
                MonoMethod *method = klass->exception_data;
-               guint32 error = (method) ? MONO_METADATA_INHERITANCEDEMAND_CLASS : MONO_METADATA_INHERITANCEDEMAND_METHOD;
+               guint32 error = (method) ? MONO_METADATA_INHERITANCEDEMAND_METHOD : MONO_METADATA_INHERITANCEDEMAND_CLASS;
                MonoObject *exc = NULL;
                gpointer args [4];