Wed Aug 22 17:26:02 CEST 2007 Paolo Molaro <lupus@ximian.com>
[mono.git] / mono / metadata / loader.c
index 14b3df70e511f20cef2676c44f945505c9ef4959..77ef3dff6ed32a0a05004532ecf08c2525a504a2 100644 (file)
@@ -100,7 +100,7 @@ mono_loader_set_error_assembly_load (const char *assembly_name, gboolean ref_onl
                return;
 
        error = g_new0 (MonoLoaderError, 1);
-       error->kind = MONO_LOADER_ERROR_ASSEMBLY;
+       error->exception_type = MONO_EXCEPTION_FILE_NOT_FOUND;
        error->assembly_name = g_strdup (assembly_name);
        error->ref_only = ref_only;
 
@@ -131,7 +131,7 @@ mono_loader_set_error_type_load (const char *class_name, const char *assembly_na
                return;
 
        error = g_new0 (MonoLoaderError, 1);
-       error->kind = MONO_LOADER_ERROR_TYPE;
+       error->exception_type = MONO_EXCEPTION_TYPE_LOAD;
        error->class_name = g_strdup (class_name);
        error->assembly_name = g_strdup (assembly_name);
 
@@ -161,7 +161,7 @@ mono_loader_set_error_method_load (const char *class_name, const char *member_na
                return;
 
        error = g_new0 (MonoLoaderError, 1);
-       error->kind = MONO_LOADER_ERROR_METHOD;
+       error->exception_type = MONO_EXCEPTION_MISSING_METHOD;
        error->class_name = g_strdup (class_name);
        error->member_name = member_name;
 
@@ -184,7 +184,7 @@ mono_loader_set_error_field_load (MonoClass *klass, const char *member_name)
                return;
 
        error = g_new0 (MonoLoaderError, 1);
-       error->kind = MONO_LOADER_ERROR_FIELD;
+       error->exception_type = MONO_EXCEPTION_MISSING_FIELD;
        error->klass = klass;
        error->member_name = member_name;
 
@@ -235,8 +235,8 @@ mono_loader_error_prepare_exception (MonoLoaderError *error)
 {
        MonoException *ex = NULL;
 
-       switch (error->kind) {
-       case MONO_LOADER_ERROR_TYPE: {
+       switch (error->exception_type) {
+       case MONO_EXCEPTION_TYPE_LOAD: {
                char *cname = g_strdup (error->class_name);
                char *aname = g_strdup (error->assembly_name);
                MonoString *class_name;
@@ -250,7 +250,7 @@ mono_loader_error_prepare_exception (MonoLoaderError *error)
                g_free (aname);
                 break;
         }
-       case MONO_LOADER_ERROR_METHOD: {
+       case MONO_EXCEPTION_MISSING_METHOD: {
                char *cname = g_strdup (error->class_name);
                char *aname = g_strdup (error->member_name);
                
@@ -261,7 +261,7 @@ mono_loader_error_prepare_exception (MonoLoaderError *error)
                break;
        }
                
-       case MONO_LOADER_ERROR_FIELD: {
+       case MONO_EXCEPTION_MISSING_FIELD: {
                char *cnspace = g_strdup (*error->klass->name_space ? error->klass->name_space : "");
                char *cname = g_strdup (error->klass->name);
                char *cmembername = g_strdup (error->member_name);
@@ -278,17 +278,20 @@ mono_loader_error_prepare_exception (MonoLoaderError *error)
                 break;
         }
        
-       case MONO_LOADER_ERROR_ASSEMBLY: {
+       case MONO_EXCEPTION_FILE_NOT_FOUND: {
                char *msg;
+               char *filename;
 
                if (error->ref_only)
                        msg = g_strdup_printf ("Cannot resolve dependency to assembly '%s' because it has not been preloaded. When using the ReflectionOnly APIs, dependent assemblies must be pre-loaded or loaded on demand through the ReflectionOnlyAssemblyResolve event.", error->assembly_name);
                else
                        msg = g_strdup_printf ("Could not load file or assembly '%s' or one of its dependencies.", error->assembly_name);
-
-               ex = mono_get_exception_file_not_found2 (msg, mono_string_new (mono_domain_get (), error->assembly_name));
+               filename = g_strdup (error->assembly_name);
+               /* Has to call this before calling anything which might call mono_class_init () */
                mono_loader_clear_error ();
+               ex = mono_get_exception_file_not_found2 (msg, mono_string_new (mono_domain_get (), filename));
                g_free (msg);
+               g_free (filename);
                break;
        }
        
@@ -467,7 +470,8 @@ find_method_in_class (MonoClass *in_class, const char *name, const char *qname,
                MonoMethod *m = in_class->methods [i];
 
                if (!((fqname && !strcmp (m->name, fqname)) ||
-                     (qname && !strcmp (m->name, qname)) || !strcmp (m->name, name)))
+                     (qname && !strcmp (m->name, qname)) ||
+                     (name && !strcmp (m->name, name))))
                        continue;
 
                if (sig->call_convention == MONO_CALL_VARARG) {
@@ -521,8 +525,19 @@ find_method (MonoClass *in_class, MonoClass *ic, const char* name, MonoMethodSig
                for (i = 0; i < in_class->interface_count; i++) {
                        MonoClass *ic = in_class->interfaces [i];
                        MonoClass *from_ic = from_class->interfaces [i];
+                       char *ic_qname, *ic_fqname, *ic_class_name;
+                       
+                       ic_class_name = mono_type_get_name_full (&ic->byval_arg, MONO_TYPE_NAME_FORMAT_IL);
+                       ic_qname = g_strconcat (ic_class_name, ".", name, NULL); 
+                       if (ic->name_space && ic->name_space [0])
+                               ic_fqname = g_strconcat (ic->name_space, ".", ic_class_name, ".", name, NULL);
+                       else
+                               ic_fqname = NULL;
 
-                       result = find_method_in_class (ic, name, qname, fqname, sig, from_ic);
+                       result = find_method_in_class (ic, NULL, ic_qname, ic_fqname, sig, from_ic);
+                       g_free (ic_class_name);
+                       g_free (ic_fqname);
+                       g_free (ic_qname);
                        if (result)
                                goto out;
                }
@@ -679,7 +694,8 @@ search_in_array_class (MonoClass *klass, const char *name, MonoMethodSignature *
 }
 
 static MonoMethod *
-method_from_memberref (MonoImage *image, guint32 idx, MonoGenericContext *typespec_context)
+method_from_memberref (MonoImage *image, guint32 idx, MonoGenericContext *typespec_context,
+                      gboolean *used_context)
 {
        MonoClass *klass = NULL;
        MonoMethod *method = NULL;
@@ -698,6 +714,14 @@ method_from_memberref (MonoImage *image, guint32 idx, MonoGenericContext *typesp
 
        mname = mono_metadata_string_heap (image, cols [MONO_MEMBERREF_NAME]);
 
+       /*
+        * Whether we actually used the `typespec_context' or not.
+        * This is used to tell our caller whether or not it's safe to insert the returned
+        * method into a cache.
+        */
+       if (used_context)
+               *used_context = class == MONO_MEMBERREF_PARENT_TYPESPEC;
+
        switch (class) {
        case MONO_MEMBERREF_PARENT_TYPEREF:
                klass = mono_class_from_typeref (image, MONO_TOKEN_TYPE_REF | nindex);
@@ -811,10 +835,10 @@ static MonoMethod *
 method_from_methodspec (MonoImage *image, MonoGenericContext *context, guint32 idx)
 {
        MonoMethod *method, *inflated;
+       MonoClass *klass;
        MonoTableInfo *tables = image->tables;
-       MonoGenericContext *new_context = NULL;
-       MonoGenericMethod *gmethod;
-       MonoGenericContainer *container = NULL;
+       MonoGenericContext new_context;
+       MonoGenericInst *inst;
        const char *ptr;
        guint32 cols [MONO_METHODSPEC_SIZE];
        guint32 token, nindex, param_count;
@@ -859,23 +883,19 @@ method_from_methodspec (MonoImage *image, MonoGenericContext *context, guint32 i
        if ((token & MONO_METHODDEFORREF_MASK) == MONO_METHODDEFORREF_METHODDEF)
                method = mono_get_method_full (image, MONO_TOKEN_METHOD_DEF | nindex, NULL, context);
        else
-               method = method_from_memberref (image, nindex, context);
+               method = method_from_memberref (image, nindex, context, NULL);
 
-       method = mono_get_inflated_method (method);
+       klass = method->klass;
 
-       container = method->generic_container;
-       g_assert (container);
+       /* FIXME: Is this invalid metadata?  Should we throw an exception instead?  */
+       g_assert (!klass->generic_container);
 
-       gmethod = g_new0 (MonoGenericMethod, 1);
-       if (method->klass->generic_class)
-               gmethod->class_inst = method->klass->generic_class->inst;
-       gmethod->container = container;
+       if (klass->generic_class) {
+               g_assert (method->is_inflated);
+               method = ((MonoMethodInflated *) method)->declaring;
+       }
 
-       new_context = g_new0 (MonoGenericContext, 1);
-       new_context->gmethod = gmethod;
-       /* FIXME: Is this correct? */
-       if (container->parent)
-               new_context->class_inst = container->parent->context.class_inst;
+       new_context.class_inst = klass->generic_class ? klass->generic_class->context.class_inst : NULL;
 
        /*
         * When parsing the methodspec signature, we're in the old context again:
@@ -898,29 +918,14 @@ method_from_methodspec (MonoImage *image, MonoGenericContext *context, guint32 i
         * ie. instantiate the method as `Foo.Hello<float>.
         */
 
-       gmethod->inst = mono_metadata_parse_generic_inst (image, NULL, param_count, ptr, &ptr);
+       inst = mono_metadata_parse_generic_inst (image, NULL, param_count, ptr, &ptr);
 
-       if (context && gmethod->inst->is_open)
-               gmethod->inst = mono_metadata_inflate_generic_inst (gmethod->inst, context);
+       if (context && inst->is_open)
+               inst = mono_metadata_inflate_generic_inst (inst, context);
 
-       if (!container->method_hash)
-               container->method_hash = g_hash_table_new (
-                       (GHashFunc)mono_metadata_generic_method_hash, (GEqualFunc)mono_metadata_generic_method_equal);
-
-       inflated = g_hash_table_lookup (container->method_hash, gmethod);
-       if (inflated) {
-               g_free (gmethod);
-               g_free (new_context);
-               return inflated;
-       }
+       new_context.method_inst = inst;
 
-       context = new_context;
-
-       mono_stats.generics_metadata_size += sizeof (MonoGenericMethod) +
-               sizeof (MonoGenericContext) + param_count * sizeof (MonoType);
-
-       inflated = mono_class_inflate_generic_method_full (method, method->klass, new_context);
-       g_hash_table_insert (container->method_hash, gmethod, inflated);
+       inflated = mono_class_inflate_generic_method_full (method, klass, &new_context);
 
        return inflated;
 }
@@ -1305,20 +1310,12 @@ mono_lookup_pinvoke_call (MonoMethod *method, const char **exc_class, const char
        return piinfo->addr;
 }
 
-MonoGenericMethod *
-mono_get_shared_generic_method (MonoGenericContainer *container)
-{
-       MonoGenericMethod *gmethod = g_new0 (MonoGenericMethod, 1);
-       gmethod->container = container;
-       gmethod->class_inst = container->context.class_inst;
-       gmethod->inst = mono_get_shared_generic_inst (container);
-
-       return gmethod;
-}
-
+/*
+ * LOCKING: assumes the loader lock to be taken.
+ */
 static MonoMethod *
 mono_get_method_from_token (MonoImage *image, guint32 token, MonoClass *klass,
-                           MonoGenericContext *context)
+                           MonoGenericContext *context, gboolean *used_context)
 {
        MonoMethod *result;
        int table = mono_metadata_token_table (token);
@@ -1333,16 +1330,18 @@ mono_get_method_from_token (MonoImage *image, guint32 token, MonoClass *klass,
                return mono_lookup_dynamic_token (image, token);
 
        if (table != MONO_TABLE_METHOD) {
-               if (table == MONO_TABLE_METHODSPEC)
+               if (table == MONO_TABLE_METHODSPEC) {
+                       if (used_context) *used_context = TRUE;
                        return method_from_methodspec (image, context, idx);
+               }
                if (table != MONO_TABLE_MEMBERREF)
                        g_print("got wrong token: 0x%08x\n", token);
                g_assert (table == MONO_TABLE_MEMBERREF);
-               result = method_from_memberref (image, idx, context);
-
-               return result;
+               return method_from_memberref (image, idx, context, used_context);
        }
 
+       if (used_context) *used_context = FALSE;
+
        mono_metadata_decode_row (&image->tables [MONO_TABLE_METHOD], idx - 1, cols, 6);
 
        if ((cols [2] & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
@@ -1370,13 +1369,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) {
-               MonoGenericContext *context;
-
                generic_container->owner.method = result;
-               context = &generic_container->context;
-               if (container)
-                       context->class_inst = container->context.class_inst;
-               context->gmethod = mono_get_shared_generic_method (generic_container);
 
                mono_metadata_load_generic_param_constraints (image, token, generic_container);
 
@@ -1386,6 +1379,7 @@ mono_get_method_from_token (MonoImage *image, guint32 token, MonoClass *klass,
                container = generic_container;
        }
 
+
        if (!sig) /* already taken from the methodref */
                sig = mono_metadata_blob_heap (image, cols [4]);
        size = mono_metadata_decode_blob_size (sig, &sig);
@@ -1426,6 +1420,7 @@ mono_get_method_full (MonoImage *image, guint32 token, MonoClass *klass,
                      MonoGenericContext *context)
 {
        MonoMethod *result;
+       gboolean used_context = FALSE;
 
        /* We do everything inside the lock to prevent creation races */
 
@@ -1436,11 +1431,21 @@ mono_get_method_full (MonoImage *image, guint32 token, MonoClass *klass,
                return result;
        }
 
-       result = mono_get_method_from_token (image, token, klass, context);
+       result = mono_get_method_from_token (image, token, klass, context, &used_context);
 
        //printf ("GET: %s\n", mono_method_full_name (result, TRUE));
 
-       if (!(result && result->is_inflated))
+#if 0
+       g_message (G_STRLOC ": %s - %d - %d", mono_method_full_name (result, TRUE),
+                  result->is_inflated, used_context);
+#endif
+
+       /*
+        * `used_context' specifies whether or not mono_get_method_from_token() actually
+        * 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);
 
        mono_loader_unlock ();
@@ -1468,14 +1473,14 @@ mono_get_method_constrained (MonoImage *image, guint32 token, MonoClass *constra
 
        mono_loader_lock ();
 
-       *cil_method = mono_get_method_from_token (image, token, NULL, context);
+       *cil_method = mono_get_method_from_token (image, token, NULL, context, NULL);
        if (!*cil_method) {
                mono_loader_unlock ();
                return NULL;
        }
 
        mono_class_init (constrained_class);
-       method = mono_get_inflated_method (*cil_method);
+       method = *cil_method;
        sig = mono_method_signature (method);
 
        if (method->is_inflated && sig->generic_param_count) {
@@ -1524,21 +1529,20 @@ mono_free_method  (MonoMethod *method)
        
        if (method->dynamic) {
                MonoMethodWrapper *mw = (MonoMethodWrapper*)method;
-               
+               int i;
+
                g_free ((char*)method->name);
-               if (mw->method.header)
+               if (mw->method.header) {
                        g_free ((char*)mw->method.header->code);
+                       for (i = 0; i < mw->method.header->num_locals; ++i)
+                               g_free (mw->method.header->locals [i]);
+                       g_free (mw->method.header->clauses);
+                       g_free (mw->method.header);
+               }
                g_free (mw->method_data);
-       }
-
-       if (method->dynamic && !(method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) && ((MonoMethodNormal *)method)->header) {
-               /* FIXME: Ditto */
-               /* mono_metadata_free_mh (((MonoMethodNormal *)method)->header); */
-               g_free (((MonoMethodNormal*)method)->header);
-       }
-
-       if (method->dynamic)
+               g_free (method->signature);
                g_free (method);
+       }
 }
 
 void
@@ -1601,9 +1605,6 @@ mono_method_get_param_token (MonoMethod *method, int index)
        MonoTableInfo *methodt;
        guint32 idx;
 
-       if (klass->generic_class)
-               g_assert_not_reached ();
-
        mono_class_init (klass);
 
        if (klass->image->dynamic) {
@@ -1615,7 +1616,11 @@ mono_method_get_param_token (MonoMethod *method, int index)
        if (idx > 0) {
                guint param_index = mono_metadata_decode_row_col (methodt, idx - 1, MONO_METHOD_PARAMLIST);
 
-               return mono_metadata_make_token (MONO_TABLE_PARAM, param_index + index);
+               if (index == -1)
+                       /* Return value */
+                       return mono_metadata_make_token (MONO_TABLE_PARAM, 0);
+               else
+                       return mono_metadata_make_token (MONO_TABLE_PARAM, param_index + index);
        }
 
        return 0;