Tue Aug 22 15:53:29 CEST 2006 Paolo Molaro <lupus@ximian.com>
[mono.git] / mono / dis / get.c
index 6c2521d5c8064e44f6611a6f922dec346bf3cfcc..4386b66cb462d5692bfa6ec8ecc4c8e5b4e77e4f 100644 (file)
@@ -16,6 +16,7 @@
 #include "meta.h"
 #include "util.h"
 #include "get.h"
+#include <mono/utils/mono-compiler.h>
 #include <mono/metadata/class.h>
 #include <mono/metadata/marshal.h>
 
@@ -26,8 +27,13 @@ get_memberref_context (MonoImage *m, guint32 mrp_token, MonoGenericContext *cont
 
 static char *
 get_memberref_parent (MonoImage *m, guint32 mrp_token, MonoGenericContext *context);
+static gboolean
+cant_print_generic_param_name (MonoGenericParam *gparam);
 
 GHashTable *key_table = NULL;
+GHashTable *mono_generic_params_with_ambiguous_names = NULL;
+GHashTable *generic_containers = NULL;
 gboolean show_method_tokens = FALSE;
 gboolean show_tokens = FALSE;
 
@@ -39,6 +45,10 @@ get_typedef (MonoImage *m, int idx)
        char *tstring, *result;
         guint32 token;
         
+       if (idx == 1)
+               /* <Module> */
+               return NULL;
+
        mono_metadata_decode_row (&m->tables [MONO_TABLE_TYPEDEF], idx - 1, cols, MONO_TYPEDEF_SIZE);
 
         ns = mono_metadata_string_heap (m, cols [MONO_TYPEDEF_NAMESPACE]);
@@ -82,7 +92,7 @@ get_module (MonoImage *m, int idx)
            
        mono_metadata_decode_row (&m->tables [MONO_TABLE_MODULE], idx - 1, cols, MONO_MODULE_SIZE);
 
-       return g_strdup (mono_metadata_string_heap (m, cols [MONO_MODULE_NAME]));
+       return get_escaped_name (mono_metadata_string_heap (m, cols [MONO_MODULE_NAME]));
 }
 
 char *
@@ -92,7 +102,7 @@ get_moduleref (MonoImage *m, int idx)
        
        mono_metadata_decode_row (&m->tables [MONO_TABLE_MODULEREF], idx - 1, cols, MONO_MODULEREF_SIZE);
 
-       return g_strdup (mono_metadata_string_heap (m, cols [MONO_MODULEREF_NAME]));
+       return get_escaped_name (mono_metadata_string_heap (m, cols [MONO_MODULEREF_NAME]));
 }
 
 char *
@@ -102,7 +112,31 @@ get_assemblyref (MonoImage *m, int idx)
        
        mono_metadata_decode_row (&m->tables [MONO_TABLE_ASSEMBLYREF], idx - 1, cols, MONO_ASSEMBLYREF_SIZE);
 
-       return g_strdup (mono_metadata_string_heap (m, cols [MONO_ASSEMBLYREF_NAME]));
+       return get_escaped_name (mono_metadata_string_heap (m, cols [MONO_ASSEMBLYREF_NAME]));
+}
+
+static char *
+stringify_array (guint32 rank, guint32 num_sizes, guint32 num_lo_bounds, gint32 *sizes, gint32 *lo_bounds)
+{
+       GString *res = g_string_new ("[");
+       int i;
+
+       for (i = 0; i < rank; i++) {
+               if (i)
+                       g_string_append_c (res, ',');
+               if (i < num_lo_bounds)
+                       g_string_sprintfa (res, "%d...", lo_bounds [i]);
+               if (i < num_sizes) {
+                       if (i < num_lo_bounds)
+                               g_string_sprintfa (res, "%d", lo_bounds [i] + sizes [i] - 1);
+                       else
+                               g_string_sprintfa (res, "%d", sizes [i]);
+               }
+
+       }
+       g_string_append (res, "]");
+
+       return g_string_free (res, FALSE);
 }
 
 /*
@@ -112,52 +146,30 @@ get_assemblyref (MonoImage *m, int idx)
 static const char *
 get_array_shape (MonoImage *m, const char *ptr, char **result)
 {
-       GString *res = g_string_new ("[");
        guint32 rank, num_sizes, num_lo_bounds;
-       guint32 *sizes = NULL, *lo_bounds = NULL;
-       int i, r;
-       char buffer [80];
+       gint32 *sizes = NULL, *lo_bounds = NULL;
+       int i;
        
        rank = mono_metadata_decode_value (ptr, &ptr);
        num_sizes = mono_metadata_decode_value (ptr, &ptr);
 
        if (num_sizes > 0)
-               sizes = g_new (guint32, num_sizes);
+               sizes = g_new (gint32, num_sizes);
        
        for (i = 0; i < num_sizes; i++)
                sizes [i] = mono_metadata_decode_value (ptr, &ptr);
 
        num_lo_bounds = mono_metadata_decode_value (ptr, &ptr);
        if (num_lo_bounds > 0)
-               lo_bounds = g_new (guint32, num_lo_bounds);
+               lo_bounds = g_new (gint32, num_lo_bounds);
        
        for (i = 0; i < num_lo_bounds; i++)
-               lo_bounds [i] = mono_metadata_decode_value (ptr, &ptr);
-
-       for (r = 0; r < rank; r++){
-               if (r < num_sizes){
-                       if (r < num_lo_bounds){
-                               sprintf (buffer, "%d..%d", lo_bounds [r], lo_bounds [r] + sizes [r] - 1);
-                       } else {
-                               sprintf (buffer, "0..%d", sizes [r] - 1);
-                       }
-               } else
-                       buffer [0] = 0;
-               
-               g_string_append (res, buffer);
-               if ((r + 1) != rank)
-                       g_string_append (res, ", ");
-       }
-       g_string_append (res, "]");
-       
-       if (sizes)
-               g_free (sizes);
+               lo_bounds [i] = mono_metadata_decode_signed_value (ptr, &ptr);
 
-       if (lo_bounds)
-               g_free (lo_bounds);
+       *result = stringify_array (rank, num_sizes, num_lo_bounds, sizes, lo_bounds);
 
-       *result = res->str;
-       g_string_free (res, FALSE);
+       g_free (sizes);
+       g_free (lo_bounds);
 
        return ptr;
 }
@@ -177,15 +189,12 @@ get_typespec (MonoImage *m, guint32 idx, gboolean is_def, MonoGenericContext *co
        char *s, *result;
        GString *res = g_string_new ("");
        int len;
-
-       MonoType *type;
-
-       type = mono_type_create_from_typespec_full (m, context, idx);
+       MonoMethodSignature *sig;
 
        mono_metadata_decode_row (&m->tables [MONO_TABLE_TYPESPEC], idx-1, cols, MONO_TYPESPEC_SIZE);
        ptr = mono_metadata_blob_heap (m, cols [MONO_TYPESPEC_SIGNATURE]);
        len = mono_metadata_decode_value (ptr, &ptr);
-       
+
        switch (*ptr++){
        case MONO_TYPE_PTR:
                ptr = get_custom_mod (m, ptr, &s);
@@ -194,26 +203,27 @@ get_typespec (MonoImage *m, guint32 idx, gboolean is_def, MonoGenericContext *co
                        g_string_append_c (res, ' ');
                        g_free (s);
                }
-               
+
                if (*ptr == MONO_TYPE_VOID)
                        g_string_append (res, "void");
                else {
-                       ptr = get_type (m, ptr, &s, context);
+                       ptr = get_type (m, ptr, &s, is_def, context);
                        if (s)
                                g_string_append (res, s);
                }
                g_string_append (res, "*");
                break;
-               
+
        case MONO_TYPE_FNPTR:
-               s = dis_stringify_function_ptr (m, type->data.method);
+               sig = mono_metadata_parse_method_signature_full (m, context ? context->container : NULL, 0, ptr, &ptr);
+               s = dis_stringify_function_ptr (m, sig);
                g_string_append (res, "method ");
                g_string_append (res, s);
                g_free (s);
                break;
-                       
+
        case MONO_TYPE_ARRAY:
-               ptr = get_type (m, ptr, &s, context);
+               ptr = get_type (m, ptr, &s, is_def, context);
                g_string_append (res, s);
                g_free (s);
                g_string_append_c (res, ' ');
@@ -221,7 +231,7 @@ get_typespec (MonoImage *m, guint32 idx, gboolean is_def, MonoGenericContext *co
                g_string_append (res, s);
                g_free (s);
                break;
-               
+
        case MONO_TYPE_SZARRAY:
                ptr = get_custom_mod (m, ptr, &s);
                if (s){
@@ -229,14 +239,14 @@ get_typespec (MonoImage *m, guint32 idx, gboolean is_def, MonoGenericContext *co
                        g_string_append_c (res, ' ');
                        g_free (s);
                }
-               ptr = get_type (m, ptr, &s, context);
+               ptr = get_type (m, ptr, &s, is_def, context);
                g_string_append (res, s);
                g_string_append (res, "[]");
                g_free (s);
                break;
 
        default:
-               s = dis_stringify_type (m, type, is_def);
+               ptr = get_type (m, ptr - 1, &s, is_def, context);
                g_string_append (res, s);
                g_free (s);
                break;
@@ -411,19 +421,19 @@ const char *
 get_custom_mod (MonoImage *m, const char *ptr, char **return_value)
 {
        char *s;
-       const char *reqd;
+       const char *mod;
        
        *return_value = NULL;
        while ((*ptr == MONO_TYPE_CMOD_OPT) ||
                   (*ptr == MONO_TYPE_CMOD_REQD)) {
-               reqd = (*ptr == MONO_TYPE_CMOD_REQD) ? "reqd" : "opt";
+               mod = (*ptr == MONO_TYPE_CMOD_REQD) ? "modreq" : "modopt";
                ptr++;
                ptr = get_encoded_typedef_or_ref (m, ptr, &s);
 
                if (*return_value == NULL)
-                       *return_value = g_strconcat (reqd, " ", s, NULL);
+                       *return_value = g_strconcat (" ", mod, " (", s, ")", NULL);
                else
-                       *return_value = g_strconcat (*return_value, " ", reqd, " ", s, NULL);
+                       *return_value = g_strconcat (*return_value, " ", mod, " (", s, ")", NULL);
                g_free (s);
        }
        return ptr;
@@ -480,28 +490,16 @@ dis_stringify_token (MonoImage *m, guint32 token)
 char*
 dis_stringify_array (MonoImage *m, MonoArrayType *array, gboolean is_def)
 {
-       char *type;
-       GString *s = g_string_new("");
-       int i;
+       char *type, *arr_str, *ret;
        
        type = dis_stringify_type (m, &array->eklass->byval_arg, is_def);
-       g_string_append (s, type);
+       arr_str = stringify_array (array->rank, array->numsizes, array->numlobounds, array->sizes, array->lobounds);
+
+       ret = g_strconcat (type, arr_str, NULL);
+       
+       g_free (arr_str);
        g_free (type);
-       g_string_append_c (s, '[');
-       for (i = 0; i < array->rank; ++i) {
-               if (i)
-                       g_string_append_c (s, ',');
-               if (i < array->numsizes) {
-                       if (i < array->numlobounds && array->lobounds[i] != 0)
-                               g_string_sprintfa (s, "%d..%d", array->lobounds[i], array->sizes[i]);
-                       else
-                               g_string_sprintfa (s, "%d", array->sizes[i]);
-               }
-       }
-       g_string_append_c (s, ']');
-       type = s->str;
-       g_string_free (s, FALSE);
-       return type;
+       return ret;
 }
 
 char*
@@ -514,7 +512,7 @@ dis_stringify_modifiers (MonoImage *m, int n, MonoCustomMod *mod)
                char *tok = dis_stringify_token (m, mod[i].token);
                if (i > 0)
                        g_string_sprintfa (s, " ");
-               g_string_sprintfa (s, "%s (%s)", mod[i].required ? "modreq": "modopt", tok);
+               g_string_sprintfa (s, " %s (%s)", mod[i].required ? "modreq": "modopt", tok);
                g_free (tok);
        }
        g_string_append_c (s, ' ');
@@ -799,11 +797,11 @@ get_generic_param (MonoImage *m, MonoGenericContainer *container)
                        g_string_append (result, ",");
 
                flags = param->flags & GENERIC_PARAMETER_ATTRIBUTE_SPECIAL_CONSTRAINTS_MASK;
-               if (flags == GENERIC_PARAMETER_ATTRIBUTE_REFERENCE_TYPE_CONSTRAINT)
+               if ((flags & GENERIC_PARAMETER_ATTRIBUTE_REFERENCE_TYPE_CONSTRAINT) == GENERIC_PARAMETER_ATTRIBUTE_REFERENCE_TYPE_CONSTRAINT)
                        g_string_append (result, "class ");
-               else if (flags == GENERIC_PARAMETER_ATTRIBUTE_VALUE_TYPE_CONSTRAINT)
+               if ((flags & GENERIC_PARAMETER_ATTRIBUTE_VALUE_TYPE_CONSTRAINT) == GENERIC_PARAMETER_ATTRIBUTE_VALUE_TYPE_CONSTRAINT)
                        g_string_append (result, "valuetype ");
-               else if (flags == GENERIC_PARAMETER_ATTRIBUTE_CONSTRUCTOR_CONSTRAINT)
+               if ((flags & GENERIC_PARAMETER_ATTRIBUTE_CONSTRUCTOR_CONSTRAINT) == GENERIC_PARAMETER_ATTRIBUTE_CONSTRUCTOR_CONSTRAINT)
                        g_string_append (result, ".ctor ");
 
                for (constr = param->constraints; constr && *constr; constr++) {
@@ -834,6 +832,13 @@ get_generic_param (MonoImage *m, MonoGenericContainer *container)
         return retval;
 }
 
+char*
+dis_stringify_method_signature (MonoImage *m, MonoMethodSignature *method, int methoddef_row,
+                               MonoGenericContext *context, gboolean fully_qualified)
+{
+       return dis_stringify_method_signature_full (m, method, methoddef_row, context, fully_qualified, TRUE);
+}
+
 /* 
  * @m: metadata context
  * @method: MonoMethodSignature to dis-stringify
@@ -845,14 +850,14 @@ get_generic_param (MonoImage *m, MonoGenericContainer *container)
  * Returns: Allocated stringified method signature
  */
 char*
-dis_stringify_method_signature (MonoImage *m, MonoMethodSignature *method, int methoddef_row,
-                               MonoGenericContext *context, gboolean fully_qualified)
+dis_stringify_method_signature_full (MonoImage *m, MonoMethodSignature *method, int methoddef_row,
+                               MonoGenericContext *context, gboolean fully_qualified, gboolean with_marshal_info)
 {
        guint32 cols [MONO_METHOD_SIZE];
        guint32 pcols [MONO_PARAM_SIZE];
        guint32 param_index = 0, next_param_index = 0;
        gboolean has_param_row;
-       const char *name = "", *method_name = "";
+       const char *method_name = "";
        int free_method = 0;
        char *retval, *esname;
        char *type = NULL;
@@ -883,7 +888,7 @@ dis_stringify_method_signature (MonoImage *m, MonoMethodSignature *method, int m
                        }
 
                        mono_metadata_decode_blob_size (sig, &sig);
-                       method = mono_metadata_parse_method_signature_full (m, context, methoddef_row, sig, &sig);
+                       method = mono_metadata_parse_method_signature_full (m, container, methoddef_row, sig, &sig);
                        free_method = 1;
                } else if (context)
                        container = context->container;
@@ -902,8 +907,8 @@ dis_stringify_method_signature (MonoImage *m, MonoMethodSignature *method, int m
        start = method->hasthis ? 0 : 1;
        for (i = 0; i < method->param_count + 1; ++i) {
                marshal_info = NULL;
-               name = "";
                has_param_row = param_index && param_index < next_param_index;
+               esname = NULL;
 
                if (method->param_count == 0 && !has_param_row)
                        /* method has zero parameters, and no row for return val in the PARAM table */
@@ -914,9 +919,10 @@ dis_stringify_method_signature (MonoImage *m, MonoMethodSignature *method, int m
                
                if (has_param_row && i == pcols [MONO_PARAM_SEQUENCE]) {
                        if (i)
-                               name = mono_metadata_string_heap (m, pcols [MONO_PARAM_NAME]);
+                               esname = get_escaped_name (
+                                               mono_metadata_string_heap (m, pcols [MONO_PARAM_NAME]));
 
-                       if (pcols [MONO_PARAM_FLAGS] & PARAM_ATTRIBUTE_HAS_FIELD_MARSHAL) {
+                       if (with_marshal_info && (pcols [MONO_PARAM_FLAGS] & PARAM_ATTRIBUTE_HAS_FIELD_MARSHAL)) {
                                const char *tp;
                                MonoMarshalSpec *spec;
                                tp = mono_metadata_get_marshal_info (m, param_index - 1, FALSE);
@@ -931,7 +937,8 @@ dis_stringify_method_signature (MonoImage *m, MonoMethodSignature *method, int m
                        param_index ++;
                } else {
                        if (i)
-                               name = g_strdup_printf ("A_%i", i - start);
+                               /* A_[0-9]* does not require escaping */
+                               esname = g_strdup_printf ("A_%i", i - start);
                }
 
                if (!i)
@@ -942,7 +949,6 @@ dis_stringify_method_signature (MonoImage *m, MonoMethodSignature *method, int m
 
                retval = dis_stringify_param (m, method->params [i - 1]);
 
-               esname = get_escaped_name (name);
                g_string_append_printf (result, "%s%s %s", retval, marshal_info ? marshal_info : "", esname);
                g_free (retval);
                g_free (esname);
@@ -961,6 +967,7 @@ dis_stringify_method_signature (MonoImage *m, MonoMethodSignature *method, int m
                char *estype = get_escaped_name (type);
                g_string_sprintfa (result_ret, "%s::", estype);
                g_free (estype);
+               g_free (type);
        }
        esname = get_escaped_name (method_name);
        g_string_append (result_ret, esname);
@@ -1023,6 +1030,10 @@ get_escaped_class_name (MonoClass *c)
 {
        char *result, *esname;
 
+       if (c->type_token == mono_metadata_make_token (MONO_TABLE_TYPEDEF, 1))
+               /* <Module> */
+               return NULL;
+
        if (c->rank || c->byval_arg.type == MONO_TYPE_PTR) 
                g_assert (0);
 
@@ -1050,6 +1061,10 @@ dis_stringify_object_with_class (MonoImage *m, MonoClass *c, gboolean prefix, gb
        const char *otype = type->type == MONO_TYPE_VALUETYPE ? "valuetype " : "class " ;
        char *assemblyref = NULL, *result, *esname, *generic = NULL;
        
+       if (c->type_token == mono_metadata_make_token (MONO_TABLE_TYPEDEF, 1))
+               /* <Module> */
+               return NULL;
+
        if (m != c->image) {
                if (c->image->assembly_name) {
                        /* we cheat */
@@ -1168,17 +1183,15 @@ dis_stringify_type (MonoImage *m, MonoType *type, gboolean is_def)
                bare = g_strdup ("void");
                break;
        case MONO_TYPE_MVAR:
-               if (is_def) {
-                       g_assert (type->data.generic_param->name);
+               if (is_def && !cant_print_generic_param_name (type->data.generic_param))
                        bare = g_strdup_printf ("!!%s", get_escaped_name (type->data.generic_param->name));
-               else
+               else
                        bare = g_strdup_printf ("!!%d", type->data.generic_param->num);
                break;
        case MONO_TYPE_VAR:
-               if (is_def) {
-                       g_assert (type->data.generic_param->name);
+               if (is_def && !cant_print_generic_param_name (type->data.generic_param))
                        bare = g_strdup_printf ("!%s", get_escaped_name (type->data.generic_param->name));
-               else
+               else
                        bare = g_strdup_printf ("!%d", type->data.generic_param->num);
                break;
        case MONO_TYPE_GENERICINST: {
@@ -1209,8 +1222,12 @@ dis_stringify_type (MonoImage *m, MonoType *type, gboolean is_def)
 
        if (type->byref)
                byref = "&";
+       
+       if (!bare)
+               /* bare is NULL, for <Module> */
+               return bare;
                
-       result = g_strconcat (mods ? mods : "", bare, byref, pinned, NULL);
+       result = g_strconcat (bare, byref, pinned, mods ? mods : "", NULL);
 
        g_free (bare);
 
@@ -1229,7 +1246,7 @@ dis_stringify_type (MonoImage *m, MonoType *type, gboolean is_def)
  * Returns: the new ptr to continue decoding
  */
 const char *
-get_type (MonoImage *m, const char *ptr, char **result, MonoGenericContext *context)
+get_type (MonoImage *m, const char *ptr, char **result, gboolean is_def, MonoGenericContext *context)
 {
        const char *start = ptr;
        guint32 type;
@@ -1260,7 +1277,7 @@ get_type (MonoImage *m, const char *ptr, char **result, MonoGenericContext *cont
                int count, i;
                char *temp;
 
-               ptr = get_type (m, ptr, &temp, context);
+               ptr = get_type (m, ptr, &temp, is_def, context);
                g_string_append (str, temp);
                g_free (temp);
 
@@ -1270,7 +1287,7 @@ get_type (MonoImage *m, const char *ptr, char **result, MonoGenericContext *cont
                for (i = 0; i < count; i++) {
                        if (i)
                                g_string_append (str, ",");
-                       ptr = get_type (m, ptr, &temp, context);
+                       ptr = get_type (m, ptr, &temp, is_def, context);
                        g_string_append (str, temp);
                }
 
@@ -1281,8 +1298,8 @@ get_type (MonoImage *m, const char *ptr, char **result, MonoGenericContext *cont
        }
 
        default:
-               t = mono_metadata_parse_type_full (m, context, MONO_PARSE_TYPE, 0, start, &ptr);
-               *result = dis_stringify_type (m, t, FALSE);
+               t = mono_metadata_parse_type_full (m, context ? context->container : NULL, MONO_PARSE_TYPE, 0, start, &ptr);
+               *result = dis_stringify_type (m, t, is_def);
                mono_metadata_free_type (t);
                break;
        }
@@ -1311,12 +1328,12 @@ get_field_signature (MonoImage *m, guint32 blob_signature, MonoGenericContext *c
        ptr++; len--;
        
        ptr = get_custom_mod (m, ptr, &allocated_modifier_string);
-       ptr = get_type (m, ptr, &allocated_type_string, context);
+       ptr = get_type (m, ptr, &allocated_type_string, FALSE, context);
 
        res = g_strdup_printf (
-               "%s %s",
-               allocated_modifier_string ? allocated_modifier_string : "",
-               allocated_type_string);
+               "%s%s",
+               allocated_type_string,
+               allocated_modifier_string ? allocated_modifier_string : "");
        
        if (allocated_modifier_string)
                g_free (allocated_modifier_string);
@@ -1379,11 +1396,6 @@ get_ret_type (MonoImage *m, const char *ptr, char **ret_type, MonoGenericContext
        int has_byref = 0;
        
        ptr = get_custom_mod (m, ptr, &mod);
-       if (mod){
-               g_string_append (str, mod);
-               g_string_append_c (str, ' ');
-               g_free (mod);
-       }
 
        if (*ptr == MONO_TYPE_TYPEDBYREF){
                g_string_append (str, "typedref");
@@ -1397,13 +1409,19 @@ get_ret_type (MonoImage *m, const char *ptr, char **ret_type, MonoGenericContext
                        ptr++;
                }
 
-               ptr = get_type (m, ptr, &allocated_type_string, context);
+               ptr = get_type (m, ptr, &allocated_type_string, FALSE, context);
                g_string_append (str, allocated_type_string);
                if (has_byref)
                        g_string_append (str, "& ");
                g_free (allocated_type_string);
        }
 
+       if (mod){
+               g_string_append (str, mod);
+               g_string_append_c (str, ' ');
+               g_free (mod);
+       }
+
        *ret_type = str->str;
        g_string_free (str, FALSE);
 
@@ -1427,11 +1445,6 @@ get_param (MonoImage *m, const char *ptr, char **retval, MonoGenericContext *con
        char *allocated_mod_string, *allocated_type_string;
        
        ptr = get_custom_mod (m, ptr, &allocated_mod_string);
-       if (allocated_mod_string){
-               g_string_append (str, allocated_mod_string);
-               g_string_append_c (str, ' ');
-               g_free (allocated_mod_string);
-       }
        
        if (*ptr == MONO_TYPE_TYPEDBYREF){
                g_string_append (str, " typedref ");
@@ -1443,13 +1456,19 @@ get_param (MonoImage *m, const char *ptr, char **retval, MonoGenericContext *con
                        ptr++;
                        by_ref = 1;
                }
-               ptr = get_type (m, ptr, &allocated_type_string, context);
+               ptr = get_type (m, ptr, &allocated_type_string, FALSE, context);
                g_string_append (str, allocated_type_string);
                if (by_ref)
                        g_string_append_c (str, '&');
                g_free (allocated_type_string);
        }
 
+       if (allocated_mod_string){
+               g_string_append (str, allocated_mod_string);
+               g_string_append_c (str, ' ');
+               g_free (allocated_mod_string);
+       }
+
        *retval = str->str;
        g_string_free (str, FALSE);
        return ptr;
@@ -1497,6 +1516,9 @@ get_escaped_name (const char *name)
        const char *s;
        char *ret, *esc;
 
+       if (!name)
+               return NULL;
+
        g_assert (key_table);
 
        if (strlen (name) == 0)
@@ -1592,8 +1614,7 @@ field_flags (guint32 f)
  * Returns a stringifed representation of a MethodRefSig (22.2.2)
  */
 char *
-get_methodref_signature (MonoImage *m, guint32 blob_signature, const char *fancy_name,
-                        MonoGenericContext *context)
+get_methodref_signature (MonoImage *m, guint32 blob_signature, const char *fancy_name)
 {
        GString *res = g_string_new ("");
        const char *ptr = mono_metadata_blob_heap (m, blob_signature);
@@ -1627,7 +1648,7 @@ get_methodref_signature (MonoImage *m, guint32 blob_signature, const char *fancy
                gen_count = mono_metadata_decode_value (ptr, &ptr);
        param_count = mono_metadata_decode_value (ptr, &ptr);
        if (cconv != 0xa) {
-               ptr = get_ret_type (m, ptr, &allocated_ret_type, context);
+               ptr = get_ret_type (m, ptr, &allocated_ret_type, NULL);
                g_string_append (res, allocated_ret_type);
                g_free (allocated_ret_type);
        }
@@ -1658,7 +1679,7 @@ get_methodref_signature (MonoImage *m, guint32 blob_signature, const char *fancy
                        ptr++;
                }
 
-               ptr = get_param (m, ptr, &param, context);
+               ptr = get_param (m, ptr, &param, NULL);
                g_string_append (res, param);
                if (i+1 != param_count)
                        g_string_append (res, ", ");
@@ -1695,7 +1716,11 @@ get_fieldref_signature (MonoImage *m, int idx, MonoGenericContext *context)
        type = get_memberref_parent (m, cols [MONO_MEMBERREF_CLASS], context);
        esname = get_escaped_name (mono_metadata_string_heap (m, cols [MONO_MEMBERREF_NAME]));
 
-        full_sig = g_strdup_printf ("%s %s::%s", sig, type, esname);
+       full_sig = g_strdup_printf ("%s %s%s%s", 
+                       sig, 
+                       type ? type : "", 
+                       type ? "::" : "",
+                       esname);
         g_free (sig);
        g_free (type);
        g_free (esname);
@@ -1741,8 +1766,11 @@ get_field (MonoImage *m, guint32 token, MonoGenericContext *context)
        type = get_typedef (m, type_idx);
        estype = get_escaped_name (type);
        esname = get_escaped_name (mono_metadata_string_heap (m, cols [MONO_FIELD_NAME]));
-       res = g_strdup_printf ("%s %s::%s",
-                       sig, estype, esname);
+       res = g_strdup_printf ("%s %s%s%s",
+                       sig, 
+                       estype ? estype : "",
+                       estype ? "::" : "",
+                       esname);
 
        g_free (type);
        g_free (sig);
@@ -1831,14 +1859,15 @@ get_method_core (MonoImage *m, guint32 token, gboolean fullsig, MonoGenericConte
 
        mh = mono_get_method_full (m, token, NULL, context);
        if (mh) {
+               mh = mono_get_inflated_method (mh);
                if (mono_method_signature (mh)->is_inflated)
                        context = ((MonoMethodInflated *) mh)->context;
                esname = get_escaped_name (mh->name);
                sig = dis_stringify_type (m, &mh->klass->byval_arg, TRUE);
                if (show_tokens)
-                       name = g_strdup_printf ("%s/*%08x*/::%s", sig, token, esname);
+                       name = g_strdup_printf ("%s/*%08x*/%s%s", sig ? sig : "", token, sig ? "::" : "", esname);
                else
-                       name = g_strdup_printf ("%s::%s", sig, esname);
+                       name = g_strdup_printf ("%s%s%s", sig ? sig : "", sig ? "::" : "", esname);
                g_free (sig);
                g_free (esname);
        } else
@@ -1849,18 +1878,38 @@ get_method_core (MonoImage *m, guint32 token, gboolean fullsig, MonoGenericConte
                mono_metadata_decode_row (&m->tables [MONO_TABLE_METHOD], 
                                          idx - 1, method_cols, MONO_METHOD_SIZE);
 
-               sig = get_methodref_signature (m, method_cols [MONO_METHOD_SIGNATURE], name, context);
+               sig = get_methodref_signature (m, method_cols [MONO_METHOD_SIGNATURE], name);
                break;
                
        case MONO_TOKEN_MEMBER_REF: {
                mono_metadata_decode_row (&m->tables [MONO_TABLE_MEMBERREF],
                                          idx - 1, member_cols, MONO_MEMBERREF_SIZE);
-               if (!name)
-                       name = g_strdup_printf ("%s::%s",
-                                               get_memberref_parent (m, member_cols [MONO_MEMBERREF_CLASS], context),
+               if (!name) {
+                       char *parent = get_memberref_parent (m, member_cols [MONO_MEMBERREF_CLASS], context),
+                       name = g_strdup_printf ("%s%s%s",
+                                               parent ? parent : "", 
+                                               parent ? "::" : "",
                                                mono_metadata_string_heap (m, member_cols [MONO_MEMBERREF_NAME]));
+                       g_free (parent);
+               }
+               if (mh) {
+                       int arity = 0;
+
+                       if (mh->generic_container)
+                               arity = mh->generic_container->type_argc;
+                       else
+                       if (mh->is_inflated && ((MonoMethodInflated *)mh)->declaring->generic_container)
+                               arity = ((MonoMethodInflated*) mh)->declaring->generic_container->type_argc;
+
+                       if (arity > 0) {
+                               char *str = g_strdup_printf ("%s <[%d]>", name, arity);
+                               g_free (name);
+                               name = str;
+                       }
+               }
+                               
                sig = get_methodref_signature (
-                       m, member_cols [MONO_MEMBERREF_SIGNATURE], name, context);
+                       m, member_cols [MONO_MEMBERREF_SIGNATURE], name);
                break;
        }
        case MONO_TOKEN_METHOD_SPEC: {
@@ -1915,13 +1964,16 @@ get_methoddef (MonoImage *m, guint32 idx)
        mh = mono_get_method (m, MONO_TOKEN_METHOD_DEF | idx, NULL);
        if (mh) {
                sig = dis_stringify_type (m, &mh->klass->byval_arg, FALSE);
-               name = g_strdup_printf ("%s::%s", sig, mh->name);
+               name = g_strdup_printf ("%s%s%s", 
+                               sig ? sig : "",
+                               sig ? "::" : "",
+                               mh->name);
                g_free (sig);
        } else
                name = NULL;
         mono_metadata_decode_row (&m->tables [MONO_TABLE_METHOD], 
                         idx - 1, cols, MONO_METHOD_SIZE);
-        sig = get_methodref_signature (m, cols [MONO_METHOD_SIGNATURE], name, NULL);
+        sig = get_methodref_signature (m, cols [MONO_METHOD_SIGNATURE], name);
         
         return sig;
 }
@@ -2146,6 +2198,19 @@ get_encoded_user_string_or_bytearray (const unsigned char *ptr, int len)
        return result;
 }
 
+char *
+stringify_double (double r)
+{
+       char *ret, *ptr;
+
+       ret = g_strdup_printf ("%.17g.", r);
+       ptr = ret + strlen (ret) - 1;
+       if (strpbrk (ret, ".eE") != ptr)
+               *ptr = '\0';
+
+       return ret;
+}
+
 /**
  * get_constant:
  * @m: metadata context
@@ -2200,10 +2265,14 @@ get_constant (MonoImage *m, MonoTypeEnum t, guint32 blob_index)
 #else
                normal = isnormal (r);
 #endif
-               if (!normal)
+               if (!normal) {
                        return g_strdup_printf ("float32(0x%08x)", read32 (ptr));
-               else
-                       return g_strdup_printf ("float32(%.20g)", r);
+               } else {
+                       char *str = stringify_double ((double) r);
+                       char *ret = g_strdup_printf ("float32(%s)", str);
+                       g_free (str);
+                       return ret;
+               }
        }       
        case MONO_TYPE_R8: {
                gboolean normal;
@@ -2222,7 +2291,10 @@ get_constant (MonoImage *m, MonoTypeEnum t, guint32 blob_index)
                        high = read32 (ptr + 4);
                        return g_strdup_printf ("float64(0x%08x%08x)", high, low);
                } else {
-                       return g_strdup_printf ("float64(%.20g)", r);
+                       char *str = stringify_double (r);
+                       char *ret = g_strdup_printf ("float64(%s)", str);
+                       g_free (str);
+                       return ret;
                }
        }
        case MONO_TYPE_STRING:
@@ -2543,6 +2615,7 @@ init_key_table (void)
        g_hash_table_insert (key_table, (char *) "autochar", GINT_TO_POINTER (TRUE));
        g_hash_table_insert (key_table, (char *) "auto", GINT_TO_POINTER (TRUE));
        g_hash_table_insert (key_table, (char *) "beforefieldinit", GINT_TO_POINTER (TRUE));
+       g_hash_table_insert (key_table, (char *) "bestfit", GINT_TO_POINTER (TRUE));
        g_hash_table_insert (key_table, (char *) "beq", GINT_TO_POINTER (TRUE));
        g_hash_table_insert (key_table, (char *) "beq.s", GINT_TO_POINTER (TRUE));
        g_hash_table_insert (key_table, (char *) "bge", GINT_TO_POINTER (TRUE));
@@ -2597,6 +2670,7 @@ init_key_table (void)
        g_hash_table_insert (key_table, (char *) "cgt", GINT_TO_POINTER (TRUE));
        g_hash_table_insert (key_table, (char *) "cgt.un", GINT_TO_POINTER (TRUE));
        g_hash_table_insert (key_table, (char *) "char", GINT_TO_POINTER (TRUE));
+       g_hash_table_insert (key_table, (char *) "charmaperror", GINT_TO_POINTER (TRUE));
        g_hash_table_insert (key_table, (char *) "cil", GINT_TO_POINTER (TRUE));
        g_hash_table_insert (key_table, (char *) "ckfinite", GINT_TO_POINTER (TRUE));
        g_hash_table_insert (key_table, (char *) "class", GINT_TO_POINTER (TRUE));
@@ -2818,6 +2892,8 @@ init_key_table (void)
        g_hash_table_insert (key_table, (char *) "nullref", GINT_TO_POINTER (TRUE));
        g_hash_table_insert (key_table, (char *) "object", GINT_TO_POINTER (TRUE));
        g_hash_table_insert (key_table, (char *) "objectref", GINT_TO_POINTER (TRUE));
+       g_hash_table_insert (key_table, (char *) "off", GINT_TO_POINTER (TRUE));
+       g_hash_table_insert (key_table, (char *) "on", GINT_TO_POINTER (TRUE));
        g_hash_table_insert (key_table, (char *) "opt", GINT_TO_POINTER (TRUE));
        g_hash_table_insert (key_table, (char *) "optil", GINT_TO_POINTER (TRUE));
        g_hash_table_insert (key_table, (char *) "or", GINT_TO_POINTER (TRUE));
@@ -2839,6 +2915,7 @@ init_key_table (void)
        g_hash_table_insert (key_table, (char *) "preservesig", GINT_TO_POINTER (TRUE));
        g_hash_table_insert (key_table, (char *) "private", GINT_TO_POINTER (TRUE));
        g_hash_table_insert (key_table, (char *) "privatescope", GINT_TO_POINTER (TRUE));
+       g_hash_table_insert (key_table, (char *) "property", GINT_TO_POINTER (TRUE));
        g_hash_table_insert (key_table, (char *) "protected", GINT_TO_POINTER (TRUE));
        g_hash_table_insert (key_table, (char *) "public", GINT_TO_POINTER (TRUE));
        g_hash_table_insert (key_table, (char *) "readonly", GINT_TO_POINTER (TRUE));
@@ -2902,6 +2979,7 @@ init_key_table (void)
        g_hash_table_insert (key_table, (char *) "sub", GINT_TO_POINTER (TRUE));
        g_hash_table_insert (key_table, (char *) "sub.ovf", GINT_TO_POINTER (TRUE));
        g_hash_table_insert (key_table, (char *) "sub.ovf.un", GINT_TO_POINTER (TRUE));
+       g_hash_table_insert (key_table, (char *) "switch", GINT_TO_POINTER (TRUE));
        g_hash_table_insert (key_table, (char *) "synchronized", GINT_TO_POINTER (TRUE));
        g_hash_table_insert (key_table, (char *) "syschar", GINT_TO_POINTER (TRUE));
        g_hash_table_insert (key_table, (char *) "sysstring", GINT_TO_POINTER (TRUE));
@@ -2913,6 +2991,10 @@ init_key_table (void)
        g_hash_table_insert (key_table, (char *) "type", GINT_TO_POINTER (TRUE));
        g_hash_table_insert (key_table, (char *) "typedref", GINT_TO_POINTER (TRUE));
        g_hash_table_insert (key_table, (char *) "uint", GINT_TO_POINTER (TRUE));
+       g_hash_table_insert (key_table, (char *) "uint8", GINT_TO_POINTER (TRUE));
+       g_hash_table_insert (key_table, (char *) "uint16", GINT_TO_POINTER (TRUE));
+       g_hash_table_insert (key_table, (char *) "uint32", GINT_TO_POINTER (TRUE));
+       g_hash_table_insert (key_table, (char *) "uint64", GINT_TO_POINTER (TRUE));
        g_hash_table_insert (key_table, (char *) "unbox", GINT_TO_POINTER (TRUE));
        g_hash_table_insert (key_table, (char *) "unicode", GINT_TO_POINTER (TRUE));
        g_hash_table_insert (key_table, (char *) "unmanagedexp", GINT_TO_POINTER (TRUE));
@@ -2958,9 +3040,80 @@ get_method_override (MonoImage *m, guint32 token, MonoGenericContext *context)
                impl = method_dor_to_token (cols [MONO_METHODIMPL_BODY]);
                decl = method_dor_to_token (cols [MONO_METHODIMPL_DECLARATION]);
 
-               if (token == impl)
-                       return get_method_core (m, decl, FALSE, context);
+               if (token == impl) {
+                       MonoMethod *mh = NULL;
+                       mh = mono_get_method_full (m, decl, NULL, context);
+                       mh = mono_get_inflated_method (mh);
+
+                       if (mh && (mh->klass && (mh->klass->generic_class || mh->klass->generic_container))) {
+                               char *meth_str;
+                               char *ret;
+                               
+                               meth_str = get_method_core (m, decl, TRUE, context);
+                               ret = g_strdup_printf ("method %s", meth_str);
+                               g_free (meth_str);
+                               return ret;
+                       } else {
+                               return get_method_core (m, decl, FALSE, context);
+                       }
+               }
        }
 
        return NULL;
 }
+
+static void
+check_ambiguous_genparams (MonoGenericContainer *container)
+{
+       GSList *dup_list = NULL, *l;
+       GHashTable *table = NULL;
+       gpointer *p;
+       int i;
+
+       if (!container)
+               return;
+       
+       if (generic_containers && g_hash_table_lookup (generic_containers, container))
+               /* Already been checked for ambiguous gen params */
+               return;
+
+       table = g_hash_table_new (g_str_hash, g_str_equal);
+       for (i = 0; i < container->type_argc; i++) {
+               MonoGenericParam *param = &container->type_params [i];
+
+               if ((p = g_hash_table_lookup (table, param->name)))
+                       dup_list = g_slist_prepend (g_slist_prepend (dup_list, GUINT_TO_POINTER (i + 1)), p);
+               else
+                       g_hash_table_insert (table, (char*)param->name, GUINT_TO_POINTER (i + 1));
+       }
+
+       if (dup_list) {
+               if (!mono_generic_params_with_ambiguous_names)
+                       mono_generic_params_with_ambiguous_names = g_hash_table_new (NULL, NULL);
+               for (l = dup_list; l; l = l->next) {
+                       int param = GPOINTER_TO_UINT (l->data);
+                       g_hash_table_insert (mono_generic_params_with_ambiguous_names,
+                                &container->type_params [param-1],
+                                &container->type_params [param-1]);
+               }
+               g_slist_free (dup_list);
+       }
+
+       if (!generic_containers)
+               generic_containers = g_hash_table_new (NULL, NULL);
+
+       g_hash_table_insert (generic_containers, container, container);
+       g_hash_table_destroy (table);
+}
+       
+static gboolean
+cant_print_generic_param_name (MonoGenericParam *gparam)
+{
+       g_assert (gparam);
+
+       check_ambiguous_genparams (gparam->owner);
+       return (!gparam->owner || (mono_generic_params_with_ambiguous_names && 
+                       g_hash_table_lookup (mono_generic_params_with_ambiguous_names, gparam)));
+}
+
+