Revert patch as it introduced a regression while building: ** ERROR **: file metadat...
[mono.git] / mono / metadata / class.c
index fd42e2154cc6a7050b64d1f92a143b3d4ebfb71d..f8fb5f366d850bcddc3306561991943c44645cc8 100644 (file)
@@ -158,17 +158,6 @@ mono_metadata_signature_deep_dup (MonoMethodSignature *sig)
        return sig;
 }
 
-static char*
-_mono_stringify_aname (MonoAssemblyName *aname)
-{
-       return g_strdup_printf (
-               "%s, Version=%d.%d.%d.%d, Culture=%s, PublicKeyToken=%s",
-               aname->name,
-               aname->major, aname->minor, aname->build, aname->revision,
-               aname->culture && *aname->culture? aname->culture: "neutral",
-               aname->public_key_token [0] ? (char *)aname->public_key_token : "null");
-}
-
 static void
 _mono_type_get_assembly_name (MonoClass *klass, GString *str)
 {
@@ -414,7 +403,7 @@ inflate_generic_class (MonoGenericClass *ogclass, MonoGenericContext *context)
        igclass->klass = NULL;
 
        ngclass->context = g_new0 (MonoGenericContext, 1);
-       ngclass->context->container = ngclass->container_class->generic_container;
+       ngclass->context->container = context->container;
        ngclass->context->gclass = ngclass;
 
        mono_loader_lock ();
@@ -580,11 +569,6 @@ inflate_generic_context (MonoGenericContext *context, MonoGenericContext *inflat
        } else
                res->gmethod = inflate_with->gmethod;
 
-       if (res->gmethod) {
-               res->gmethod->container->parent = res->container;
-               res->container = res->gmethod->container;
-       }
-
        return res;
 }
 
@@ -592,9 +576,8 @@ MonoMethod*
 mono_class_inflate_generic_method (MonoMethod *method, MonoGenericContext *context)
 {
        MonoMethodInflated *result;
-       MonoGenericContainer *container = context ? context->container : NULL;
 
-       if (method->is_inflated || (mono_method_signature_full (method, container)->is_inflated)) {
+       if (method->is_inflated || mono_method_signature_full (method, context)->is_inflated) {
                MonoMethodInflated *imethod = (MonoMethodInflated *) method;
 
                context = inflate_generic_context (imethod->context, context);
@@ -651,68 +634,12 @@ mono_get_inflated_method (MonoMethod *method)
        return (MonoMethod *) res;
 }
 
-/** 
- * mono_class_find_enum_basetype:
- * @class: The enum class
- *
- *   Determine the basetype of an enum by iterating through its fields. We do this
- * in a separate function since it is cheaper than calling mono_class_setup_fields.
- */
-static MonoType*
-mono_class_find_enum_basetype (MonoClass *class)
-{
-       MonoImage *m = class->image; 
-       const int top = class->field.count;
-       MonoTableInfo *t = &m->tables [MONO_TABLE_FIELD];
-       int i;
-
-       g_assert (class->enumtype);
-
-       /*
-        * Fetch all the field information.
-        */
-       for (i = 0; i < top; i++){
-               const char *sig;
-               guint32 cols [MONO_FIELD_SIZE];
-               int idx = class->field.first + i;
-               MonoGenericContainer *container = NULL;
-               MonoType *ftype;
-
-               mono_metadata_decode_row (t, idx, cols, MONO_FIELD_SIZE);
-               sig = mono_metadata_blob_heap (m, cols [MONO_FIELD_SIGNATURE]);
-               mono_metadata_decode_value (sig, &sig);
-               /* FIELD signature == 0x06 */
-               g_assert (*sig == 0x06);
-               if (class->generic_container)
-                       container = class->generic_container;
-               else if (class->generic_class) {
-                       MonoClass *gklass = class->generic_class->container_class;
-
-                       container = gklass->generic_container;
-                       g_assert (container);
-               }
-               ftype = mono_metadata_parse_type_full (
-                       m, (MonoGenericContext *) container, MONO_PARSE_FIELD,
-                       cols [MONO_FIELD_FLAGS], sig + 1, &sig);
-               if (class->generic_class) {
-                       ftype = mono_class_inflate_generic_type (
-                               ftype, class->generic_class->context);
-                       ftype->attrs = cols [MONO_FIELD_FLAGS];
-               }
-
-               if (class->enumtype && !(cols [MONO_FIELD_FLAGS] & FIELD_ATTRIBUTE_STATIC))
-                       return ftype;
-       }
-
-       return NULL;
-}
-
 /** 
  * mono_class_setup_fields:
+ * @m: pointer to the metadata.
  * @class: The class to initialize
  *
  * Initializes the class->fields.
- * Assumes the loader lock is held.
  */
 static void
 mono_class_setup_fields (MonoClass *class)
@@ -730,9 +657,6 @@ mono_class_setup_fields (MonoClass *class)
        if (class->size_inited)
                return;
 
-       if (class->inited)
-               mono_class_init (class);
-
        class->instance_size = 0;
        class->class_size = 0;
 
@@ -773,7 +697,7 @@ mono_class_setup_fields (MonoClass *class)
        /* Prevent infinite loops if the class references itself */
        class->size_inited = 1;
 
-       class->fields = mono_mempool_alloc0 (class->image->mempool, sizeof (MonoClassField) * top);
+       class->fields = g_new0 (MonoClassField, top);
 
        /*
         * Fetch all the field information.
@@ -866,21 +790,6 @@ mono_class_setup_fields (MonoClass *class)
        mono_class_layout_fields (class);
 }
 
-/** 
- * mono_class_setup_fields_locking:
- * @class: The class to initialize
- *
- * Initializes the class->fields.
- * Aquires the loader lock.
- */
-static void
-mono_class_setup_fields_locking (MonoClass *class)
-{
-       mono_loader_lock ();
-       mono_class_setup_fields (class);
-       mono_loader_unlock ();
-}
-
 /* useful until we keep track of gc-references in corlib etc. */
 #define IS_GC_REFERENCE(t) ((t)->type == MONO_TYPE_U || (t)->type == MONO_TYPE_I || (t)->type == MONO_TYPE_PTR)
 
@@ -1090,9 +999,10 @@ mono_class_setup_methods (MonoClass *class)
        //printf ("INIT: %s.%s\n", class->name_space, class->name);
 
        if (!class->generic_class && !class->methods) {
-               methods = mono_mempool_alloc (class->image->mempool, sizeof (MonoMethod*) * class->method.count);
+               methods = g_new (MonoMethod*, class->method.count);
                for (i = 0; i < class->method.count; ++i) {
-                       methods [i] = mono_get_method (class->image, MONO_TOKEN_METHOD_DEF | (i + class->method.first + 1), class);
+                       methods [i] = mono_get_method (class->image,
+                                                                                  MONO_TOKEN_METHOD_DEF | (i + class->method.first + 1), class);
                }
        }
 
@@ -1100,7 +1010,6 @@ mono_class_setup_methods (MonoClass *class)
                for (i = 0; i < class->method.count; ++i)
                        methods [i]->slot = i;
 
-       /* Leave this assignment as the last op in this function */
        class->methods = methods;
 
        mono_loader_unlock ();
@@ -1115,7 +1024,6 @@ mono_class_setup_properties (MonoClass *class)
        MonoTableInfo *pt = &class->image->tables [MONO_TABLE_PROPERTY];
        MonoTableInfo *msemt = &class->image->tables [MONO_TABLE_METHODSEMANTICS];
        MonoProperty *properties;
-       guint32 last;
 
        if (class->properties)
                return;
@@ -1127,14 +1035,14 @@ mono_class_setup_properties (MonoClass *class)
                return;
        }
 
-       class->property.first = mono_metadata_properties_from_typedef (class->image, mono_metadata_token_index (class->type_token) - 1, &last);
-       class->property.count = last - class->property.first;
+       class->property.first = mono_metadata_properties_from_typedef (class->image, mono_metadata_token_index (class->type_token) - 1, &class->property.last);
+       class->property.count = class->property.last - class->property.first;
 
        if (class->property.count)
                mono_class_setup_methods (class);
 
-       properties = mono_mempool_alloc0 (class->image->mempool, sizeof (MonoProperty) * class->property.count);
-       for (i = class->property.first; i < last; ++i) {
+       properties = g_new0 (MonoProperty, class->property.count);
+       for (i = class->property.first; i < class->property.last; ++i) {
                mono_metadata_decode_row (pt, i, cols, MONO_PROPERTY_SIZE);
                properties [i - class->property.first].parent = class;
                properties [i - class->property.first].attrs = cols [MONO_PROPERTY_FLAGS];
@@ -1156,7 +1064,6 @@ mono_class_setup_properties (MonoClass *class)
                }
        }
 
-       /* Leave this assignment as the last op in the function */
        class->properties = properties;
 
        mono_loader_unlock ();
@@ -1169,27 +1076,19 @@ mono_class_setup_events (MonoClass *class)
        guint32 cols [MONO_EVENT_SIZE];
        MonoTableInfo *pt = &class->image->tables [MONO_TABLE_EVENT];
        MonoTableInfo *msemt = &class->image->tables [MONO_TABLE_METHODSEMANTICS];
-       guint32 last;
-       MonoEvent *events;
 
        if (class->events)
                return;
 
-       mono_loader_lock ();
-
-       if (class->events) {
-               mono_loader_unlock ();
-               return;
-       }
-       class->event.first = mono_metadata_events_from_typedef (class->image, mono_metadata_token_index (class->type_token) - 1, &last);
-       class->event.count = last - class->event.first;
+       class->event.first = mono_metadata_events_from_typedef (class->image, mono_metadata_token_index (class->type_token) - 1, &class->event.last);
+       class->event.count = class->event.last - class->event.first;
 
        if (class->event.count)
                mono_class_setup_methods (class);
 
-       events = mono_mempool_alloc0 (class->image->mempool, sizeof (MonoEvent) * class->event.count);
-       for (i = class->event.first; i < last; ++i) {
-               MonoEvent *event = &events [i - class->event.first];
+       class->events = g_new0 (MonoEvent, class->event.count);
+       for (i = class->event.first; i < class->event.last; ++i) {
+               MonoEvent *event = &class->events [i - class->event.first];
                        
                mono_metadata_decode_row (pt, i, cols, MONO_EVENT_SIZE);
                event->parent = class;
@@ -1227,10 +1126,6 @@ mono_class_setup_events (MonoClass *class)
                        }
                }
        }
-       /* Leave this assignment as the last op in the function */
-       class->events = events;
-
-       mono_loader_unlock ();
 }
 
 static guint
@@ -1467,7 +1362,8 @@ mono_class_setup_vtable (MonoClass *class)
        else
                context = (MonoGenericContext *) class->generic_container;              
 
-       mono_class_get_overrides_full (class->image, class->type_token, &overrides, &onum, context);
+       overrides = mono_class_get_overrides_full (
+               class->image, class->type_token, &onum, context);
        mono_class_setup_vtable_general (class, overrides, onum);
        g_free (overrides);
 
@@ -2501,7 +2397,6 @@ mono_class_create_from_typedef (MonoImage *image, guint32 type_token)
        const char *name, *nspace;
        guint icount = 0; 
        MonoClass **interfaces;
-       guint32 field_last, method_last;
 
        mono_loader_lock ();
 
@@ -2536,21 +2431,18 @@ mono_class_create_from_typedef (MonoImage *image, guint32 type_token)
                context->gclass = mono_get_shared_generic_class (context->container, FALSE);
        }
 
-       if (cols [MONO_TYPEDEF_EXTENDS]) {
+       if (cols [MONO_TYPEDEF_EXTENDS])
                parent = mono_class_get_full (
                        image, mono_metadata_token_from_dor (cols [MONO_TYPEDEF_EXTENDS]), context);
-       }
 
        mono_class_setup_parent (class, parent);
 
        mono_class_setup_mono_type (class);
 
-       if (!class->enumtype) {
-               mono_metadata_interfaces_from_typedef_full (image, type_token, &interfaces, &icount, context);
+       interfaces = mono_metadata_interfaces_from_typedef_full (image, type_token, &icount, context);
 
-               class->interfaces = interfaces;
-               class->interface_count = icount;
-       }
+       class->interfaces = interfaces;
+       class->interface_count = icount;
 
        if ((class->flags & TYPE_ATTRIBUTE_STRING_FORMAT_MASK) == TYPE_ATTRIBUTE_UNICODE_CLASS)
                class->unicode = 1;
@@ -2571,21 +2463,21 @@ mono_class_create_from_typedef (MonoImage *image, guint32 type_token)
 
        if (tt->rows > tidx){           
                mono_metadata_decode_row (tt, tidx, cols_next, MONO_TYPEDEF_SIZE);
-               field_last  = cols_next [MONO_TYPEDEF_FIELD_LIST] - 1;
-               method_last = cols_next [MONO_TYPEDEF_METHOD_LIST] - 1;
+               class->field.last  = cols_next [MONO_TYPEDEF_FIELD_LIST] - 1;
+               class->method.last = cols_next [MONO_TYPEDEF_METHOD_LIST] - 1;
        } else {
-               field_last  = image->tables [MONO_TABLE_FIELD].rows;
-               method_last = image->tables [MONO_TABLE_METHOD].rows;
+               class->field.last  = image->tables [MONO_TABLE_FIELD].rows;
+               class->method.last = image->tables [MONO_TABLE_METHOD].rows;
        }
 
        if (cols [MONO_TYPEDEF_FIELD_LIST] && 
            cols [MONO_TYPEDEF_FIELD_LIST] <= image->tables [MONO_TABLE_FIELD].rows)
-               class->field.count = field_last - class->field.first;
+               class->field.count = class->field.last - class->field.first;
        else
                class->field.count = 0;
 
        if (cols [MONO_TYPEDEF_METHOD_LIST] <= image->tables [MONO_TABLE_METHOD].rows)
-               class->method.count = method_last - class->method.first;
+               class->method.count = class->method.last - class->method.first;
        else
                class->method.count = 0;
 
@@ -2595,10 +2487,8 @@ mono_class_create_from_typedef (MonoImage *image, guint32 type_token)
                g_assert (class->field.count == 0);
        }
 
-       if (class->enumtype) {
-               class->enum_basetype = mono_class_find_enum_basetype (class);
-               class->cast_class = class->element_class = mono_class_from_mono_type (class->enum_basetype);
-       }
+       if (class->enumtype)
+               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);
@@ -2733,8 +2623,6 @@ 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;
 
-       klass->generic_container = param->owner;
-
        mono_class_setup_supertypes (klass);
 
        return klass;
@@ -2767,8 +2655,6 @@ 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;
 
-       klass->generic_container = param->owner;
-
        mono_class_setup_supertypes (klass);
 
        return klass;
@@ -3088,8 +2974,6 @@ mono_bounded_array_class_get (MonoClass *eclass, guint32 rank, gboolean bounded)
                class->inited = 1;
        }
 
-       class->generic_container = eclass->generic_container;
-
        list = g_slist_append (rootlist, class);
        g_hash_table_insert (image->array_cache, eclass, list);
 
@@ -3192,17 +3076,18 @@ mono_class_data_size (MonoClass *klass)
 static MonoClassField *
 mono_class_get_field_idx (MonoClass *class, int idx)
 {
-       mono_class_setup_fields_locking (class);
+       mono_class_setup_fields (class);
 
-       while (class) {
-               if (class->field.count) {
-                       if ((idx >= class->field.first) && (idx < class->field.first + class->field.count)){
-                               return &class->fields [idx - class->field.first];
-                       }
+       if (class->field.count){
+               if ((idx >= class->field.first) && (idx < class->field.last)){
+                       return &class->fields [idx - class->field.first];
                }
-               class = class->parent;
        }
-       return NULL;
+
+       if (!class->parent)
+               return NULL;
+       
+       return mono_class_get_field_idx (class->parent, idx);
 }
 
 /**
@@ -3224,22 +3109,13 @@ mono_class_get_field (MonoClass *class, guint32 field_token)
        return mono_class_get_field_idx (class, idx - 1);
 }
 
-/**
- * mono_class_get_field_from_name:
- * @klass: the class to lookup the field.
- * @name: the field name
- *
- * Search the class @klass and it's parents for a field with the name @name.
- * 
- * Returns: the MonoClassField pointer of the named field or NULL
- */
 MonoClassField *
 mono_class_get_field_from_name (MonoClass *klass, const char *name)
 {
        int i;
 
-       mono_class_setup_fields_locking (klass);
        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];
@@ -3249,23 +3125,14 @@ mono_class_get_field_from_name (MonoClass *klass, const char *name)
        return NULL;
 }
 
-/**
- * mono_class_get_field_token:
- * @field: the field we need the token of
- *
- * Get the token of a field. Note that the tokesn is only valid for the image
- * the field was loaded from. Don't use this function for fields in dynamic types.
- * 
- * Returns: the token representing the field in the image it was loaded from.
- */
 guint32
 mono_class_get_field_token (MonoClassField *field)
 {
        MonoClass *klass = field->parent;
        int i;
 
-       mono_class_setup_fields_locking (klass);
        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);
@@ -3338,7 +3205,7 @@ mono_class_get_property_token (MonoProperty *prop)
 }
 
 char *
-mono_class_name_from_token (MonoImage *image, guint32 type_token)
+mono_class_name_from_token (MonoImage *image, guint32 type_token, MonoGenericContext *context)
 {
        const char *name, *nspace;
        if (image->dynamic)
@@ -3381,53 +3248,6 @@ mono_class_name_from_token (MonoImage *image, guint32 type_token)
        return NULL;
 }
 
-static char *
-mono_assembly_name_from_token (MonoImage *image, guint32 type_token)
-{
-       if (image->dynamic)
-               return g_strdup_printf ("DynamicAssembly %s", image->name);
-       
-       switch (type_token & 0xff000000){
-       case MONO_TOKEN_TYPE_DEF:
-               return _mono_stringify_aname (&image->assembly->aname);
-               break;
-       case MONO_TOKEN_TYPE_REF: {
-               MonoAssemblyName aname;
-               guint32 cols [MONO_TYPEREF_SIZE];
-               MonoTableInfo  *t = &image->tables [MONO_TABLE_TYPEREF];
-               guint32 idx;
-       
-               mono_metadata_decode_row (t, (type_token&0xffffff)-1, cols, MONO_TYPEREF_SIZE);
-
-               idx = cols [MONO_TYPEREF_SCOPE] >> MONO_RESOLTION_SCOPE_BITS;
-               switch (cols [MONO_TYPEREF_SCOPE] & MONO_RESOLTION_SCOPE_MASK) {
-               case MONO_RESOLTION_SCOPE_MODULE:
-                       /* FIXME: */
-                       return g_strdup ("");
-               case MONO_RESOLTION_SCOPE_MODULEREF:
-                       /* FIXME: */
-                       return g_strdup ("");
-               case MONO_RESOLTION_SCOPE_TYPEREF:
-                       /* FIXME: */
-                       return g_strdup ("");
-               case MONO_RESOLTION_SCOPE_ASSEMBLYREF:
-                       mono_assembly_get_assemblyref (image, idx - 1, &aname);
-                       return _mono_stringify_aname (&aname);
-               default:
-                       g_assert_not_reached ();
-               }
-               break;
-       }
-       case MONO_TOKEN_TYPE_SPEC:
-               /* FIXME: */
-               return g_strdup ("");
-       default:
-               g_assert_not_reached ();
-       }
-
-       return NULL;
-}
-
 /**
  * mono_class_get:
  * @image: the image where the class resides
@@ -3460,9 +3280,9 @@ _mono_class_get (MonoImage *image, guint32 type_token, MonoGenericContext *conte
        }
 
        if (!class){
-               char *name = mono_class_name_from_token (image, type_token);
-               char *assembly = mono_assembly_name_from_token (image, type_token);
-               mono_loader_set_error_type_load (name, assembly);
+               char *name = mono_class_name_from_token (image, type_token, context);
+               g_warning ("The class %s could not be loaded, used in %s (token 0x%08x)", name, image->name, type_token);
+               g_free (name);
        }
 
        return class;
@@ -3978,8 +3798,6 @@ mono_ldtoken (MonoImage *image, guint32 token, MonoClass **handle_class,
                if (handle_class)
                        *handle_class = mono_defaults.typehandle_class;
                class = mono_class_get_full (image, token, context);
-               if (!class)
-                       return NULL;
                mono_class_init (class);
                /* We return a MonoType* as handle */
                return &class->byval_arg;
@@ -3989,20 +3807,16 @@ mono_ldtoken (MonoImage *image, guint32 token, MonoClass **handle_class,
                if (handle_class)
                        *handle_class = mono_defaults.typehandle_class;
                class = mono_class_create_from_typespec (image, token, context);
-               if (!class)
-                       return NULL;
                mono_class_init (class);
                return &class->byval_arg;
        }
        case MONO_TOKEN_FIELD_DEF: {
                MonoClass *class;
                guint32 type = mono_metadata_typedef_from_field (image, mono_metadata_token_index (token));
-               if (handle_class)
-                       *handle_class = mono_defaults.fieldhandle_class;
                class = mono_class_get_full (image, MONO_TOKEN_TYPE_DEF | type, context);
-               if (!class)
-                       return NULL;
                mono_class_init (class);
+               if (handle_class)
+                       *handle_class = mono_defaults.fieldhandle_class;
                return mono_class_get_field (class, token);
        }
        case MONO_TOKEN_METHOD_DEF: {
@@ -4309,8 +4123,10 @@ mono_class_get_fields (MonoClass* klass, gpointer *iter)
        MonoClassField* field;
        if (!iter)
                return NULL;
-       mono_class_setup_fields_locking (klass);
+       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];