2008-06-13 Mark Probst <mark.probst@gmail.com>
[mono.git] / mono / metadata / loader.c
index 5302095ba25346a51bbf1ff73b85e104651bc774..17794ed099aafb5e18a24615f50d4b22bcc4c203 100644 (file)
@@ -333,13 +333,32 @@ mono_loader_error_prepare_exception (MonoLoaderError *error)
        return ex;
 }
 
+/*
+ * If @field belongs to an inflated generic class, return the corresponding field of the
+ * generic type definition class.
+ */
+static MonoClassField*
+mono_metadata_get_corresponding_field_from_generic_type_definition (MonoClassField *field)
+{
+       MonoClass *gtd;
+       int offset;
+
+       if (!field->parent->generic_class)
+               return field;
+
+       gtd = field->parent->generic_class->container_class;
+       offset = field - field->parent->fields;
+       return gtd->fields + offset;
+}
+
 static MonoClassField*
 field_from_memberref (MonoImage *image, guint32 token, MonoClass **retklass,
                      MonoGenericContext *context)
 {
        MonoClass *klass;
-       MonoClassField *field;
+       MonoClassField *field, *sig_field = NULL;
        MonoTableInfo *tables = image->tables;
+       MonoType *sig_type;
        guint32 cols[6];
        guint32 nindex, class;
        const char *fname;
@@ -356,6 +375,12 @@ field_from_memberref (MonoImage *image, guint32 token, MonoClass **retklass,
        mono_metadata_decode_blob_size (ptr, &ptr);
        /* we may want to check the signature here... */
 
+       if (*ptr++ != 0x6) {
+               mono_loader_set_error_bad_image (g_strdup_printf ("Bad field signature class token %08x field name %s token %08x", class, fname, token));
+               return NULL;
+       }
+       sig_type = mono_metadata_parse_type (image, MONO_PARSE_TYPE, 0, ptr, &ptr);
+
        switch (class) {
        case MONO_MEMBERREF_PARENT_TYPEDEF:
                klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF | nindex);
@@ -369,7 +394,7 @@ field_from_memberref (MonoImage *image, guint32 token, MonoClass **retklass,
                mono_class_init (klass);
                if (retklass)
                        *retklass = klass;
-               field = mono_class_get_field_from_name (klass, fname);
+               sig_field = field = mono_class_get_field_from_name (klass, fname);
                break;
        case MONO_MEMBERREF_PARENT_TYPEREF:
                klass = mono_class_from_typeref (image, MONO_TOKEN_TYPE_REF | nindex);
@@ -383,28 +408,16 @@ field_from_memberref (MonoImage *image, guint32 token, MonoClass **retklass,
                mono_class_init (klass);
                if (retklass)
                        *retklass = klass;
-               field = mono_class_get_field_from_name (klass, fname);
+               sig_field = field = mono_class_get_field_from_name (klass, fname);
                break;
        case MONO_MEMBERREF_PARENT_TYPESPEC: {
-               /*guint32 bcols [MONO_TYPESPEC_SIZE];
-               guint32 len;
-               MonoType *type;
-
-               mono_metadata_decode_row (&tables [MONO_TABLE_TYPESPEC], nindex - 1, 
-                                         bcols, MONO_TYPESPEC_SIZE);
-               ptr = mono_metadata_blob_heap (image, bcols [MONO_TYPESPEC_SIGNATURE]);
-               len = mono_metadata_decode_value (ptr, &ptr);   
-               type = mono_metadata_parse_type (image, MONO_PARSE_TYPE, 0, ptr, &ptr);
-
-               klass = mono_class_from_mono_type (type);
-               mono_class_init (klass);
-               g_print ("type in sig: %s\n", klass->name);*/
                klass = mono_class_get_full (image, MONO_TOKEN_TYPE_SPEC | nindex, context);
                //FIXME can't klass be null?
                mono_class_init (klass);
                if (retklass)
                        *retklass = klass;
                field = mono_class_get_field_from_name (klass, fname);
+               sig_field = mono_metadata_get_corresponding_field_from_generic_type_definition (field);
                break;
        }
        default:
@@ -414,6 +427,10 @@ field_from_memberref (MonoImage *image, guint32 token, MonoClass **retklass,
 
        if (!field)
                mono_loader_set_error_field_load (klass, fname);
+       else if (sig_field && !mono_metadata_type_equal_full (sig_type, sig_field->type, TRUE)) {
+               mono_loader_set_error_field_load (klass, fname);
+               return NULL;
+       }
 
        return field;
 }
@@ -520,11 +537,8 @@ find_method_in_class (MonoClass *in_class, const char *name, const char *qname,
                }
        }
 
-       if (i < in_class->method.count) {
-               mono_class_setup_methods (from_class);
-               g_assert (from_class->method.count == in_class->method.count);
-               return from_class->methods [i];
-       }
+       if (i < in_class->method.count)
+               return mono_class_get_method_by_index (from_class, i);
        return NULL;
 }
 
@@ -673,7 +687,6 @@ mono_method_get_signature_full (MonoMethod *method, MonoImage *image, guint32 to
 
        if (table == MONO_TABLE_METHODSPEC) {
                g_assert (!(method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) &&
-                         !(method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) &&
                          mono_method_signature (method));
                g_assert (method->is_inflated);
 
@@ -1377,6 +1390,7 @@ mono_get_method_from_token (MonoImage *image, guint32 token, MonoClass *klass,
        container = klass->generic_container;
        generic_container = mono_metadata_load_generic_params (image, token, container);
        if (generic_container) {
+               result->is_generic = TRUE;
                generic_container->owner.method = result;
 
                mono_metadata_load_generic_param_constraints (image, token, generic_container);
@@ -1411,16 +1425,8 @@ mono_get_method_from_token (MonoImage *image, guint32 token, MonoClass *klass,
                        piinfo->piflags = mono_metadata_decode_row_col (&tables [MONO_TABLE_IMPLMAP], piinfo->implmap_idx - 1, MONO_IMPLMAP_FLAGS);
        }
 
-       /* FIXME: lazyness for generics too, but how? */
-       if (!(result->flags & METHOD_ATTRIBUTE_ABSTRACT) &&
-           !(cols [1] & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) &&
-           !(result->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) && container) {
-               gpointer loc = mono_image_rva_map (image, cols [0]);
-               g_assert (loc);
-               ((MonoMethodNormal *) result)->header = mono_metadata_parse_mh_full (image, container, loc);
-       }
-
-       result->generic_container = generic_container;
+       if (generic_container)
+               mono_method_set_generic_container (result, generic_container);
 
        return result;
 }
@@ -1431,6 +1437,14 @@ mono_get_method (MonoImage *image, guint32 token, MonoClass *klass)
        return mono_get_method_full (image, token, klass, NULL);
 }
 
+static gpointer
+get_method_token (gpointer value)
+{
+       MonoMethod *m = (MonoMethod*)value;
+
+       return GUINT_TO_POINTER (m->token);
+}
+
 MonoMethod *
 mono_get_method_full (MonoImage *image, guint32 token, MonoClass *klass,
                      MonoGenericContext *context)
@@ -1442,7 +1456,16 @@ mono_get_method_full (MonoImage *image, guint32 token, MonoClass *klass,
 
        mono_loader_lock ();
 
-       if ((result = g_hash_table_lookup (image->method_cache, GINT_TO_POINTER (token)))) {
+       if (mono_metadata_token_table (token) == MONO_TABLE_METHOD) {
+               if (!image->method_cache)
+                       image->method_cache = mono_value_hash_table_new (NULL, NULL, get_method_token);
+               result = mono_value_hash_table_lookup (image->method_cache, GINT_TO_POINTER (token));
+       } else {
+               if (!image->methodref_cache)
+                       image->methodref_cache = g_hash_table_new (NULL, NULL);
+               result = g_hash_table_lookup (image->methodref_cache, GINT_TO_POINTER (token));
+       }
+       if (result) {
                mono_loader_unlock ();
                return result;
        }
@@ -1461,8 +1484,13 @@ mono_get_method_full (MonoImage *image, guint32 token, MonoClass *klass,
         * used the `context' to get the method.  See bug #80969.
         */
 
-       if (!used_context && !(result && result->is_inflated))
-               g_hash_table_insert (image->method_cache, GINT_TO_POINTER (token), result);
+       if (!used_context && !(result && result->is_inflated) && result) {
+               if (mono_metadata_token_table (token) == MONO_TABLE_METHOD) {
+                       mono_value_hash_table_insert (image->method_cache, GINT_TO_POINTER (token), result);
+               } else {
+                       g_hash_table_insert (image->methodref_cache, GINT_TO_POINTER (token), result);
+               }
+       }
 
        mono_loader_unlock ();
 
@@ -1485,7 +1513,7 @@ mono_get_method_constrained (MonoImage *image, guint32 token, MonoClass *constra
        MonoMethod *method, *result;
        MonoClass *ic = NULL;
        MonoGenericContext *class_context = NULL, *method_context = NULL;
-       MonoMethodSignature *sig;
+       MonoMethodSignature *sig, *original_sig;
 
        mono_loader_lock ();
 
@@ -1497,12 +1525,26 @@ mono_get_method_constrained (MonoImage *image, guint32 token, MonoClass *constra
 
        mono_class_init (constrained_class);
        method = *cil_method;
-       sig = mono_method_signature (method);
+       original_sig = sig = mono_method_signature (method);
 
        if (method->is_inflated && sig->generic_param_count) {
                MonoMethodInflated *imethod = (MonoMethodInflated *) method;
                sig = mono_method_signature (imethod->declaring);
                method_context = mono_method_get_context (method);
+
+               original_sig = sig;
+               /*
+                * We must inflate the signature with the class instantiation to work on
+                * cases where a class inherit from a generic type and the override replaces
+                * and type argument which a concrete type. See #325283.
+                */
+               if (method_context->class_inst) {
+                       MonoGenericContext ctx;
+                       ctx.method_inst = NULL;
+                       ctx.class_inst = method_context->class_inst;
+               
+                       sig = inflate_generic_signature (method->klass->image, sig, &ctx);
+               }
        }
 
        if ((constrained_class != method->klass) && (method->klass->interface_id != 0))
@@ -1512,6 +1554,9 @@ mono_get_method_constrained (MonoImage *image, guint32 token, MonoClass *constra
                class_context = mono_class_get_context (constrained_class);
 
        result = find_method (constrained_class, ic, method->name, sig, constrained_class);
+       if (sig != original_sig)
+               mono_metadata_free_inflated_signature (sig);
+
        if (!result) {
                g_warning ("Missing method %s.%s.%s in assembly %s token %x", method->klass->name_space,
                           method->klass->name, method->name, image->name, token);
@@ -1551,6 +1596,10 @@ mono_free_method  (MonoMethod *method)
                MonoMethodWrapper *mw = (MonoMethodWrapper*)method;
                int i;
 
+               mono_loader_lock ();
+               mono_property_hash_remove_object (method->klass->image->property_hash, method);
+               mono_loader_unlock ();
+
                g_free ((char*)method->name);
                if (mw->method.header) {
                        g_free ((char*)mw->method.header->code);
@@ -1868,7 +1917,7 @@ mono_method_signature (MonoMethod *m)
        sig = mono_metadata_blob_heap (img, mono_metadata_decode_row_col (&img->tables [MONO_TABLE_METHOD], idx - 1, MONO_METHOD_SIGNATURE));
 
        g_assert (!m->klass->generic_class);
-       container = m->generic_container;
+       container = mono_method_get_generic_container (m);
        if (!container)
                container = m->klass->generic_container;
 
@@ -2013,7 +2062,7 @@ mono_method_get_header (MonoMethod *method)
 
        g_assert (loc);
 
-       mn->header = mono_metadata_parse_mh_full (img, method->generic_container, loc);
+       mn->header = mono_metadata_parse_mh_full (img, mono_method_get_generic_container (method), loc);
 
        mono_loader_unlock ();
        return mn->header;