* ssa.c: In mono_ssa_rename_vars, forced the creation of a new SSA
[mono.git] / mono / metadata / loader.c
index 757717d4d5ae0814ea9effe33849c7485c0515b1..f533d4e42247e51f69448be3c6ae924eae0f01b3 100644 (file)
@@ -152,7 +152,7 @@ mono_field_from_token (MonoImage *image, guint32 token, MonoClass **retklass,
        }
 
        mono_loader_lock ();
-       if (!field->parent->generic_inst)
+       if (!field->parent->generic_class)
                g_hash_table_insert (image->field_cache, GUINT_TO_POINTER (token), field);
        mono_loader_unlock ();
        return field;
@@ -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,23 +221,6 @@ find_method (MonoClass *klass, MonoClass *ic, const char* name, MonoMethodSignat
                klass = klass->parent;
        }
 
-       if (sclass->generic_inst) {
-               MonoClass *gclass;
-               MonoMethod *res;
-
-               gclass = mono_class_from_mono_type (sclass->generic_inst->generic_type);
-               mono_class_init (gclass);
-
-               res = find_method (gclass, 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;
 }
 
@@ -246,7 +228,7 @@ find_method (MonoClass *klass, MonoClass *ic, const char* name, MonoMethodSignat
  * token is the method_ref or method_def token used in a call IL instruction.
  */
 MonoMethodSignature*
-mono_method_get_signature (MonoMethod *method, MonoImage *image, guint32 token)
+mono_method_get_signature_full (MonoMethod *method, MonoImage *image, guint32 token, MonoGenericContext *context)
 {
        int table = mono_metadata_token_table (token);
        int idx = mono_metadata_token_index (token);
@@ -256,44 +238,53 @@ mono_method_get_signature (MonoMethod *method, MonoImage *image, guint32 token)
 
        /* !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);
-               g_assert (method->signature->is_inflated);
+                         mono_method_signature (method));
+               g_assert (method->is_inflated);
 
-               return method->signature;
+               return mono_method_signature (method);
        }
 
-       if (method->klass->generic_inst)
-               return method->signature;
+       if (method->klass->generic_class)
+               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);
        
                ptr = mono_metadata_blob_heap (image, cols [MONO_MEMBERREF_SIGNATURE]);
                mono_metadata_decode_blob_size (ptr, &ptr);
-               sig = mono_metadata_parse_method_signature (image, 0, ptr, NULL);
+               sig = mono_metadata_parse_method_signature_full (image, context, 0, ptr, NULL);
                g_hash_table_insert (image->memberref_signatures, GUINT_TO_POINTER (token), sig);
        }
 
+       sig = mono_class_inflate_generic_signature (image, sig, context);
+
        return sig;
 }
 
+MonoMethodSignature*
+mono_method_get_signature (MonoMethod *method, MonoImage *image, guint32 token)
+{
+       return mono_method_get_signature_full (method, image, token, NULL);
+}
+
 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;
@@ -336,18 +327,18 @@ 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_inst) {
-               g_assert (klass->generic_inst->container);
-               container = klass->generic_inst->container;
-       }
+       mono_class_init (klass);
 
        ptr = mono_metadata_blob_heap (image, cols [MONO_MEMBERREF_SIGNATURE]);
        mono_metadata_decode_blob_size (ptr, &ptr);
-       sig = mono_metadata_parse_method_signature_full (image, container, 0, ptr, NULL);
+       sig = mono_metadata_parse_method_signature_full (image, (MonoGenericContext *) container, 0, ptr, NULL);
 
        switch (class) {
        case MONO_MEMBERREF_PARENT_TYPEREF:
@@ -355,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;
@@ -366,11 +357,11 @@ method_from_memberref (MonoImage *image, guint32 idx, MonoGenericContext *contex
                        method = find_method (klass, NULL, mname, sig);
                        if (!method)
                                g_warning ("Missing method %s in assembly %s typeref index %d", mname, image->name, nindex);
-                       else if (klass->generic_inst && (klass != method->klass))
+                       else if (klass->generic_class && (klass != method->klass))
                                method = mono_class_inflate_generic_method (
-                                       method, klass->generic_inst->context, klass);
+                                       method, klass->generic_class->context, klass);
                        mono_metadata_free_method_signature (sig);
-                       return method;
+                       break;
                }
 
                result = (MonoMethod *)g_new0 (MonoMethodPInvoke, 1);
@@ -413,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 *
@@ -432,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];
@@ -450,36 +444,49 @@ method_from_methodspec (MonoImage *image, MonoGenericContext *context, guint32 i
        param_count = mono_metadata_decode_value (ptr, &ptr);
 
        g_assert (param_count);
-       if (method->signature->is_inflated)
+       if (method->is_inflated)
                container = ((MonoMethodNormal *) ((MonoMethodInflated *) method)->declaring)->generic_container;
        else
                container = ((MonoMethodNormal *) method)->generic_container;
        g_assert (container && container->is_method);
 
        if (context) {
-               if (context->ginst) {
-                       g_assert (context->ginst->container);
-                       container->parent = context->ginst->container;
-               } else
-                       container->parent = context->container;
+               g_assert (context->container);
+               container->parent = context->container;
+               if (container->parent->is_method)
+                       container->parent = container->parent->parent;
        }
 
        gmethod = g_new0 (MonoGenericMethod, 1);
        gmethod->container = container;
-       gmethod->mtype_argc = param_count;
-       gmethod->mtype_argv = g_new0 (MonoType *, param_count);
 
-       for (i = 0; i < param_count; i++) {
-               gmethod->mtype_argv [i] = mono_metadata_parse_type_full (
-                       image, container, MONO_PARSE_TYPE, 0, ptr, &ptr);
+       gmethod->inst = mono_metadata_parse_generic_inst (
+               image, (MonoGenericContext *) container, param_count, ptr, &ptr);
+
+       if (context)
+               gmethod->inst = mono_metadata_inflate_generic_inst (gmethod->inst, context);
 
-               if (!gmethod->is_open)
-                       gmethod->is_open = mono_class_is_open_constructed_type (gmethod->mtype_argv [i]);
+       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);
+               return inflated;
        }
 
        if (!context) {
                new_context = g_new0 (MonoGenericContext, 1);
+               new_context->container = container;
+               new_context->gmethod = gmethod;
+
+               context = new_context;
+       } else {
+               new_context = g_new0 (MonoGenericContext, 1);
+               new_context->container = container;
                new_context->gmethod = gmethod;
+               new_context->gclass = context->gclass;
 
                context = new_context;
        }
@@ -488,9 +495,10 @@ method_from_methodspec (MonoImage *image, MonoGenericContext *context, guint32 i
                sizeof (MonoGenericContext) + param_count * sizeof (MonoType);
 
        inflated = mono_class_inflate_generic_method (method, context, NULL);
+       g_hash_table_insert (container->method_hash, gmethod, inflated);
 
        if (new_context)
-               context->ginst = inflated->klass->generic_inst;
+               context->gclass = inflated->klass->generic_class;
        return inflated;
 }
 
@@ -612,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;
@@ -641,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
         */
@@ -719,7 +731,7 @@ mono_lookup_pinvoke_call (MonoMethod *method, const char **exc_class, const char
 
                if (exc_class) {
                        *exc_class = "DllNotFoundException";
-                       *exc_arg = orig_scope;
+                       *exc_arg = new_scope;
                }
                return NULL;
        }
@@ -757,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);
                }
@@ -798,11 +810,8 @@ mono_get_method_from_token (MonoImage *image, guint32 token, MonoClass *klass,
        if (table != MONO_TABLE_METHOD) {
                MonoGenericContainer *generic_container = NULL;
                if (context) {
-                       if (context->ginst) {
-                               g_assert (context->ginst->container);
-                               generic_container = context->ginst->container;
-                       } else
-                               generic_container = context->container;
+                       g_assert (context->container);
+                       generic_container = context->container;
                }
                if (table == MONO_TABLE_METHODSPEC)
                        return method_from_methodspec (image, context, idx);
@@ -834,18 +843,20 @@ mono_get_method_from_token (MonoImage *image, guint32 token, MonoClass *klass,
 
        if (!(cols [1] & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) &&
            (!(cols [2] & METHOD_ATTRIBUTE_PINVOKE_IMPL) || cols [1] & METHOD_IMPL_ATTRIBUTE_NATIVE)) {
-               generic_container = mono_metadata_load_generic_params (image, token);
-               if (generic_container) {
-                       generic_container->parent = container;
-                       generic_container->is_method = 1;
+               generic_container = mono_metadata_load_generic_params (image, token, container);
+               if (generic_container)
                        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);
-       result->signature = mono_metadata_parse_method_signature_full (image, 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);
@@ -855,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++) {
@@ -919,7 +902,8 @@ mono_get_method_from_token (MonoImage *image, guint32 token, MonoClass *klass,
                    !(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);
+                       ((MonoMethodNormal *) result)->header = mono_metadata_parse_mh_full (
+                               image, (MonoGenericContext *) container, loc);
                }
                
                ((MonoMethodNormal *) result)->generic_container = generic_container;
@@ -951,7 +935,7 @@ mono_get_method_full (MonoImage *image, guint32 token, MonoClass *klass,
 
        result = mono_get_method_from_token (image, token, klass, context);
 
-       if (!(result && result->signature->is_inflated))
+       if (!(result && result->is_inflated))
                g_hash_table_insert (image->method_cache, GINT_TO_POINTER (token), result);
 
        mono_loader_unlock ();
@@ -965,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 ();
 
@@ -975,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;
 }
@@ -991,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);
        }
@@ -1007,22 +1000,22 @@ 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_inst) /* copy the names later */
+       if (klass->generic_class) /* copy the names later */
                return;
 
        mono_class_init (klass);
 
        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];
                }
@@ -1058,7 +1051,7 @@ mono_method_get_param_token (MonoMethod *method, int index)
        MonoClass *klass = method->klass;
        MonoTableInfo *methodt;
 
-       if (klass->generic_inst)
+       if (klass->generic_class)
                g_assert_not_reached ();
 
        mono_class_init (klass);
@@ -1088,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));
@@ -1148,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;
                }
@@ -1190,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
@@ -1256,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*
@@ -1313,7 +1363,7 @@ mono_method_get_header (MonoMethod *method)
        
        g_assert (loc);
        
-       mn->header = mono_metadata_parse_mh_full (img, mn->generic_container, loc);
+       mn->header = mono_metadata_parse_mh_full (img, (MonoGenericContext *) mn->generic_container, loc);
        
        mono_loader_unlock ();
        return mn->header;