* ssa.c: In mono_ssa_rename_vars, forced the creation of a new SSA
[mono.git] / mono / metadata / loader.c
index 239efa0e8c40f0ec0e392834f28faa782e09213b..f533d4e42247e51f69448be3c6ae924eae0f01b3 100644 (file)
@@ -187,7 +187,6 @@ static MonoMethod *
 find_method (MonoClass *klass, MonoClass *ic, const char* name, MonoMethodSignature *sig)
 {
        int i;
-       MonoClass *sclass = klass;
        char *qname, *fqname;
 
        if (ic) {
@@ -208,10 +207,10 @@ find_method (MonoClass *klass, MonoClass *ic, const char* name, MonoMethodSignat
                                continue;
 
                        if (sig->call_convention == MONO_CALL_VARARG) {
-                               if (mono_metadata_signature_vararg_match (sig, m->signature))
+                               if (mono_metadata_signature_vararg_match (sig, mono_method_signature (m)))
                                        return m;
                        } else {
-                               if (mono_metadata_signature_equal (sig, m->signature))
+                               if (mono_metadata_signature_equal (sig, mono_method_signature (m)))
                                        return m;
                        }
                }
@@ -222,21 +221,6 @@ find_method (MonoClass *klass, MonoClass *ic, const char* name, MonoMethodSignat
                klass = klass->parent;
        }
 
-       if (sclass->generic_class) {
-               MonoMethod *res;
-
-               mono_class_init (sclass->generic_class->container_class);
-
-               res = find_method (sclass->generic_class->container_class, ic, name, sig);
-               if (!res)
-                       return NULL;
-               for (i = 0; i < res->klass->method.count; ++i) {
-                       if (res == res->klass->methods [i]) {
-                               return sclass->methods [i];
-                       }
-               }
-       }
-
        return NULL;
 }
 
@@ -254,23 +238,23 @@ mono_method_get_signature_full (MonoMethod *method, MonoImage *image, guint32 to
 
        /* !table is for wrappers: we should really assign their own token to them */
        if (!table || table == MONO_TABLE_METHOD)
-               return method->signature;
+               return mono_method_signature (method);
 
        if (table == MONO_TABLE_METHODSPEC) {
                g_assert (!(method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) &&
                          !(method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) &&
-                         method->signature);
+                         mono_method_signature (method));
                g_assert (method->is_inflated);
 
-               return method->signature;
+               return mono_method_signature (method);
        }
 
        if (method->klass->generic_class)
-               return method->signature;
+               return mono_method_signature (method);
 
        if (image->dynamic)
                /* FIXME: This might be incorrect for vararg methods */
-               return method->signature;
+               return mono_method_signature (method);
 
        if (!(sig = g_hash_table_lookup (image->memberref_signatures, GUINT_TO_POINTER (token)))) {
                mono_metadata_decode_row (&image->tables [MONO_TABLE_MEMBERREF], idx-1, cols, MONO_MEMBERREF_SIZE);
@@ -296,10 +280,11 @@ static MonoMethod *
 method_from_memberref (MonoImage *image, guint32 idx, MonoGenericContext *context)
 {
        MonoClass *klass = NULL;
-       MonoMethod *method;
+       MonoMethod *method = NULL;
        MonoTableInfo *tables = image->tables;
        guint32 cols[6];
        guint32 nindex, class;
+       MonoGenericClass *gclass = NULL;
        MonoGenericContainer *container = NULL;
        const char *mname;
        MonoMethodSignature *sig;
@@ -342,14 +327,14 @@ method_from_memberref (MonoImage *image, guint32 idx, MonoGenericContext *contex
                g_assert_not_reached ();
        }
        g_assert (klass);
-       mono_class_init (klass);
 
+       if (klass->generic_class) {
+               gclass = klass->generic_class;
+               klass = gclass->container_class;
+       }
        if (klass->generic_container)
                container = klass->generic_container;
-       else if (klass->generic_class) {
-               container = klass->generic_class->container_class->generic_container;
-               g_assert (container);
-       }
+       mono_class_init (klass);
 
        ptr = mono_metadata_blob_heap (image, cols [MONO_MEMBERREF_SIGNATURE]);
        mono_metadata_decode_blob_size (ptr, &ptr);
@@ -361,7 +346,7 @@ method_from_memberref (MonoImage *image, guint32 idx, MonoGenericContext *contex
                if (!method)
                        g_warning ("Missing method %s in assembly %s typeref index %d", mname, image->name, nindex);
                mono_metadata_free_method_signature (sig);
-               return method;
+               break;
        case MONO_MEMBERREF_PARENT_TYPESPEC: {
                MonoType *type;
                MonoMethod *result;
@@ -376,7 +361,7 @@ method_from_memberref (MonoImage *image, guint32 idx, MonoGenericContext *contex
                                method = mono_class_inflate_generic_method (
                                        method, klass->generic_class->context, klass);
                        mono_metadata_free_method_signature (sig);
-                       return method;
+                       break;
                }
 
                result = (MonoMethod *)g_new0 (MonoMethodPInvoke, 1);
@@ -419,13 +404,16 @@ method_from_memberref (MonoImage *image, guint32 idx, MonoGenericContext *contex
                if (!method)
                        g_warning ("Missing method %s in assembly %s typeref index %d", mname, image->name, nindex);
                mono_metadata_free_method_signature (sig);
-               return method;
+               break;
        default:
                g_error ("Memberref parent unknown: class: %d, index %d", class, nindex);
                g_assert_not_reached ();
        }
 
-       return NULL;
+       if (gclass)
+               method = mono_class_inflate_generic_method (method, gclass->context, gclass->klass);
+
+       return method;
 }
 
 static MonoMethod *
@@ -438,7 +426,7 @@ method_from_methodspec (MonoImage *image, MonoGenericContext *context, guint32 i
        MonoGenericContainer *container = NULL;
        const char *ptr;
        guint32 cols [MONO_METHODSPEC_SIZE];
-       guint32 token, param_count, i;
+       guint32 token, param_count;
 
        mono_metadata_decode_row (&tables [MONO_TABLE_METHODSPEC], idx - 1, cols, MONO_METHODSPEC_SIZE);
        token = cols [MONO_METHODSPEC_METHOD];
@@ -632,7 +620,7 @@ mono_lookup_pinvoke_call (MonoMethod *method, const char **exc_class, const char
 
        if (method->klass->image->dynamic) {
                MonoReflectionMethodAux *method_aux = 
-                       mono_g_hash_table_lookup (
+                       g_hash_table_lookup (
                                ((MonoDynamicImage*)method->klass->image)->method_aux_hash, method);
                if (!method_aux)
                        return NULL;
@@ -661,7 +649,11 @@ mono_lookup_pinvoke_call (MonoMethod *method, const char **exc_class, const char
                *exc_class = NULL;
                *exc_arg = NULL;
        }
-       
+
+       /* we allow a special name to dlopen from the running process namespace */
+       if (strcmp (new_scope, "__Internal") == 0)
+               gmodule = g_module_open (NULL, G_MODULE_BIND_LAZY);
+               
        /*
         * Try loading the module using a variety of names
         */
@@ -777,12 +769,12 @@ mono_lookup_pinvoke_call (MonoMethod *method, const char **exc_class, const char
                /* Try the stdcall mangled name */
                if (!piinfo->addr) {
                        /* FIX: Compute this correctly */
-                       mangled_name = g_strdup_printf ("%s@%d", import, method->signature->param_count * sizeof (gpointer));
+                       mangled_name = g_strdup_printf ("%s@%d", import, mono_method_signature (method)->param_count * sizeof (gpointer));
                        g_module_symbol (gmodule, mangled_name, &piinfo->addr); 
                        g_free (mangled_name);
                }
                if (!piinfo->addr) {
-                       mangled_name = g_strdup_printf ("_%s@%d", import, method->signature->param_count * sizeof (gpointer));
+                       mangled_name = g_strdup_printf ("_%s@%d", import, mono_method_signature (method)->param_count * sizeof (gpointer));
                        g_module_symbol (gmodule, mangled_name, &piinfo->addr); 
                        g_free (mangled_name);
                }
@@ -859,8 +851,12 @@ mono_get_method_from_token (MonoImage *image, guint32 token, MonoClass *klass,
        if (!sig) /* already taken from the methodref */
                sig = mono_metadata_blob_heap (image, cols [4]);
        size = mono_metadata_decode_blob_size (sig, &sig);
-       result->signature = mono_metadata_parse_method_signature_full (
-               image, (MonoGenericContext *) container, idx, sig, NULL);
+       
+       /* there are generic params, or a container. FIXME: be lazy here for generics*/
+       if (* sig & 0x10 || container)
+               result->signature = mono_metadata_parse_method_signature_full (
+                   image, (MonoGenericContext *) container, idx, sig, NULL);
+
 
        if (!result->klass) {
                guint32 type = mono_metadata_typedef_from_method (image, token);
@@ -870,42 +866,14 @@ mono_get_method_from_token (MonoImage *image, guint32 token, MonoClass *klass,
        if (cols [1] & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) {
                if (result->klass == mono_defaults.string_class && !strcmp (result->name, ".ctor"))
                        result->string_ctor = 1;
-
-               result->signature->pinvoke = 1;
        } else if ((cols [2] & METHOD_ATTRIBUTE_PINVOKE_IMPL) && (!(cols [1] & METHOD_IMPL_ATTRIBUTE_NATIVE))) {
                MonoMethodPInvoke *piinfo = (MonoMethodPInvoke *)result;
                MonoTableInfo *im = &tables [MONO_TABLE_IMPLMAP];
-               MonoCallConvention conv = 0;
-
-               result->signature->pinvoke = 1;
+               
                piinfo->implmap_idx = mono_metadata_implmap_from_method (image, idx - 1);
                piinfo->piflags = mono_metadata_decode_row_col (im, piinfo->implmap_idx - 1, MONO_IMPLMAP_FLAGS);
-
-               switch (piinfo->piflags & PINVOKE_ATTRIBUTE_CALL_CONV_MASK) {
-               case PINVOKE_ATTRIBUTE_CALL_CONV_WINAPI:
-                       conv = MONO_CALL_DEFAULT;
-                       break;
-               case PINVOKE_ATTRIBUTE_CALL_CONV_CDECL:
-                       conv = MONO_CALL_C;
-                       break;
-               case PINVOKE_ATTRIBUTE_CALL_CONV_STDCALL:
-                       conv = MONO_CALL_STDCALL;
-                       break;
-               case PINVOKE_ATTRIBUTE_CALL_CONV_THISCALL:
-                       conv = MONO_CALL_THISCALL;
-                       break;
-               case PINVOKE_ATTRIBUTE_CALL_CONV_FASTCALL:
-                       conv = MONO_CALL_FASTCALL;
-                       break;
-               case PINVOKE_ATTRIBUTE_CALL_CONV_GENERIC:
-               case PINVOKE_ATTRIBUTE_CALL_CONV_GENERICINST:
-               default:
-                       g_warning ("unsupported calling convention");
-                       g_assert_not_reached ();
-               }       
-               result->signature->call_convention = conv;
        } else {
-               if (result->signature->generic_param_count) {
+               if (result->signature && result->signature->generic_param_count) {
                        MonoMethodSignature *sig = result->signature;
 
                        for (i = 0; i < sig->generic_param_count; i++) {
@@ -981,6 +949,7 @@ mono_get_method_constrained (MonoImage *image, guint32 token, MonoClass *constra
 {
        MonoMethod *method, *result;
        MonoClass *ic = NULL;
+       MonoGenericClass *gclass = NULL;
 
        mono_loader_lock ();
 
@@ -991,15 +960,22 @@ mono_get_method_constrained (MonoImage *image, guint32 token, MonoClass *constra
        }
 
        mono_class_init (constrained_class);
+       method = mono_get_inflated_method (method);
 
        if ((constrained_class != method->klass) && (method->klass->interface_id != 0))
                ic = method->klass;
 
-       result = find_method (constrained_class, ic, method->name, method->signature);
+       if (constrained_class->generic_class)
+               gclass = constrained_class->generic_class;
+
+       result = find_method (constrained_class, ic, method->name, mono_method_signature (method));
        if (!result)
                g_warning ("Missing method %s in assembly %s token %x", method->name,
                           image->name, token);
 
+       if (gclass)
+               result = mono_class_inflate_generic_method (result, gclass->context, gclass->klass);
+
        mono_loader_unlock ();
        return result;
 }
@@ -1007,7 +983,8 @@ mono_get_method_constrained (MonoImage *image, guint32 token, MonoClass *constra
 void
 mono_free_method  (MonoMethod *method)
 {
-       mono_metadata_free_method_signature (method->signature);
+       if (method->signature)
+               mono_metadata_free_method_signature (method->signature);
        if (!(method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) && ((MonoMethodNormal *)method)->header) {
                mono_metadata_free_mh (((MonoMethodNormal *)method)->header);
        }
@@ -1023,9 +1000,9 @@ mono_method_get_param_names (MonoMethod *method, const char **names)
        MonoTableInfo *methodt;
        MonoTableInfo *paramt;
 
-       if (!method->signature->param_count)
+       if (!mono_method_signature (method)->param_count)
                return;
-       for (i = 0; i < method->signature->param_count; ++i)
+       for (i = 0; i < mono_method_signature (method)->param_count; ++i)
                names [i] = "";
 
        if (klass->generic_class) /* copy the names later */
@@ -1035,10 +1012,10 @@ mono_method_get_param_names (MonoMethod *method, const char **names)
 
        if (klass->image->dynamic) {
                MonoReflectionMethodAux *method_aux = 
-                       mono_g_hash_table_lookup (
+                       g_hash_table_lookup (
                                ((MonoDynamicImage*)method->klass->image)->method_aux_hash, method);
                if (method_aux && method_aux->param_names) {
-                       for (i = 0; i < method->signature->param_count; ++i)
+                       for (i = 0; i < mono_method_signature (method)->param_count; ++i)
                                if (method_aux->param_names [i + 1])
                                        names [i] = method_aux->param_names [i + 1];
                }
@@ -1104,16 +1081,16 @@ mono_method_get_marshal_info (MonoMethod *method, MonoMarshalSpec **mspecs)
        MonoTableInfo *methodt;
        MonoTableInfo *paramt;
 
-       for (i = 0; i < method->signature->param_count + 1; ++i)
+       for (i = 0; i < mono_method_signature (method)->param_count + 1; ++i)
                mspecs [i] = NULL;
 
        if (method->klass->image->dynamic) {
                MonoReflectionMethodAux *method_aux = 
-                       mono_g_hash_table_lookup (
+                       g_hash_table_lookup (
                                ((MonoDynamicImage*)method->klass->image)->method_aux_hash, method);
                if (method_aux && method_aux->param_marshall) {
                        MonoMarshalSpec **dyn_specs = method_aux->param_marshall;
-                       for (i = 0; i < method->signature->param_count + 1; ++i)
+                       for (i = 0; i < mono_method_signature (method)->param_count + 1; ++i)
                                if (dyn_specs [i]) {
                                        mspecs [i] = g_new0 (MonoMarshalSpec, 1);
                                        memcpy (mspecs [i], dyn_specs [i], sizeof (MonoMarshalSpec));
@@ -1164,11 +1141,11 @@ mono_method_has_marshal_info (MonoMethod *method)
 
        if (method->klass->image->dynamic) {
                MonoReflectionMethodAux *method_aux = 
-                       mono_g_hash_table_lookup (
+                       g_hash_table_lookup (
                                ((MonoDynamicImage*)method->klass->image)->method_aux_hash, method);
                MonoMarshalSpec **dyn_specs = method_aux->param_marshall;
                if (dyn_specs) {
-                       for (i = 0; i < method->signature->param_count + 1; ++i)
+                       for (i = 0; i < mono_method_signature (method)->param_count + 1; ++i)
                                if (dyn_specs [i])
                                        return TRUE;
                }
@@ -1206,14 +1183,14 @@ mono_method_has_marshal_info (MonoMethod *method)
 gpointer
 mono_method_get_wrapper_data (MonoMethod *method, guint32 id)
 {
-       GList *l;
+       void **data;
        g_assert (method != NULL);
        g_assert (method->wrapper_type != MONO_WRAPPER_NONE);
 
-       if (!(l = g_list_nth (((MonoMethodWrapper *)method)->data, id - 1)))
-               g_assert_not_reached ();
-
-       return l->data;
+       data = ((MonoMethodWrapper *)method)->method_data;
+       g_assert (data != NULL);
+       g_assert (id <= GPOINTER_TO_UINT (*data));
+       return data [id];
 }
 
 static void
@@ -1272,9 +1249,66 @@ mono_loader_unlock (void)
 }
 
 MonoMethodSignature* 
-mono_method_signature (MonoMethod *method)
+mono_method_signature (MonoMethod *m)
 {
-       return method->signature;
+       int idx;
+       int size;
+       MonoImage* img;
+       const char *sig;
+       
+       if (m->signature)
+               return m->signature;
+               
+       mono_loader_lock ();
+       
+       if (m->signature) {
+               mono_loader_unlock ();
+               return m->signature;
+       }
+       
+       g_assert (mono_metadata_token_table (m->token) == MONO_TABLE_METHOD);
+       idx = mono_metadata_token_index (m->token);
+       img = m->klass->image;
+       
+       sig = mono_metadata_blob_heap (img, mono_metadata_decode_row_col (&img->tables [MONO_TABLE_METHOD], idx - 1, MONO_METHOD_SIGNATURE));
+       size = mono_metadata_decode_blob_size (sig, &sig);
+       
+       m->signature = mono_metadata_parse_method_signature_full (img, NULL, idx, sig, NULL);
+       
+       if (m->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
+               m->signature->pinvoke = 1;
+       else if ((m->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) && (!(m->iflags & METHOD_IMPL_ATTRIBUTE_NATIVE))) {
+               MonoCallConvention conv = 0;
+               MonoMethodPInvoke *piinfo = (MonoMethodPInvoke *)m;
+               m->signature->pinvoke = 1;
+               
+               switch (piinfo->piflags & PINVOKE_ATTRIBUTE_CALL_CONV_MASK) {
+               case PINVOKE_ATTRIBUTE_CALL_CONV_WINAPI:
+                       conv = MONO_CALL_DEFAULT;
+                       break;
+               case PINVOKE_ATTRIBUTE_CALL_CONV_CDECL:
+                       conv = MONO_CALL_C;
+                       break;
+               case PINVOKE_ATTRIBUTE_CALL_CONV_STDCALL:
+                       conv = MONO_CALL_STDCALL;
+                       break;
+               case PINVOKE_ATTRIBUTE_CALL_CONV_THISCALL:
+                       conv = MONO_CALL_THISCALL;
+                       break;
+               case PINVOKE_ATTRIBUTE_CALL_CONV_FASTCALL:
+                       conv = MONO_CALL_FASTCALL;
+                       break;
+               case PINVOKE_ATTRIBUTE_CALL_CONV_GENERIC:
+               case PINVOKE_ATTRIBUTE_CALL_CONV_GENERICINST:
+               default:
+                       g_warning ("unsupported calling convention");
+                       g_assert_not_reached ();
+               }       
+               m->signature->call_convention = conv;
+       }
+       
+       mono_loader_unlock ();
+       return m->signature;
 }
 
 const char*