Add methods to TypePrinter for renaming system types to CSharp types and vice versa
authorIgor Kulaychuk <i.kulaychuk@samsung.com>
Mon, 24 Jul 2017 11:06:46 +0000 (14:06 +0300)
committerIgor Kulaychuk <i.kulaychuk@samsung.com>
Mon, 13 Nov 2017 19:22:40 +0000 (22:22 +0300)
src/debug/netcoredbg/expr.cpp
src/debug/netcoredbg/typeprinter.cpp
src/debug/netcoredbg/typeprinter.h

index b40abc1..934d641 100644 (file)
@@ -378,31 +378,6 @@ HRESULT FindTypeInModule(ICorDebugModule *pModule, const std::vector<std::string
     return S_OK;
 }
 
-static std::string RenameType(const std::string &shortTypeName)
-{
-    static const std::unordered_map<std::string, std::string> short2long = {
-        {"bool",    "System.Boolean"},
-        {"byte",    "System.Byte"},
-        {"sbyte",   "System.SByte"},
-        {"char",    "System.Char"},
-        {"decimal", "System.Decimal"},
-        {"double",  "System.Double"},
-        {"float",   "System.Single"},
-        {"int",     "System.Int32"},
-        {"uint",    "System.UInt32"},
-        {"long",    "System.Int64"},
-        {"ulong",   "System.UInt64"},
-        {"object",  "System.Object"},
-        {"short",   "System.Int16"},
-        {"ushort",  "System.UInt16"},
-        {"string",  "System.String"},
-        {"IntPtr",  "System.IntPtr"},
-        {"UIntPtr", "System.UIntPtr"}
-    };
-    auto renamed = short2long.find(shortTypeName);
-    return renamed != short2long.end() ? renamed->second : shortTypeName;
-}
-
 HRESULT FindType(
     const std::vector<std::string> &parts,
     int &nextPart, ICorDebugThread *pThread,
@@ -418,7 +393,7 @@ HRESULT GetType(const std::string &typeName,
     std::vector<int> ranks;
     std::vector<std::string> classParts = ParseType(typeName, ranks);
     if (classParts.size() == 1)
-        classParts[0] = RenameType(classParts[0]);
+        classParts[0] = TypePrinter::RenameToSystem(classParts[0]);
 
     ToRelease<ICorDebugType> pType;
     int nextClassPart = 0;
index febfc0a..d915352 100644 (file)
@@ -65,6 +65,56 @@ static std::string ConsumeGenericArgs(const std::string &name, std::list<std::st
     return ss.str();
 }
 
+std::string TypePrinter::RenameToSystem(const std::string &typeName)
+{
+    static const std::unordered_map<std::string, std::string> cs2system = {
+        {"bool",    "System.Boolean"},
+        {"byte",    "System.Byte"},
+        {"sbyte",   "System.SByte"},
+        {"char",    "System.Char"},
+        {"decimal", "System.Decimal"},
+        {"double",  "System.Double"},
+        {"float",   "System.Single"},
+        {"int",     "System.Int32"},
+        {"uint",    "System.UInt32"},
+        {"long",    "System.Int64"},
+        {"ulong",   "System.UInt64"},
+        {"object",  "System.Object"},
+        {"short",   "System.Int16"},
+        {"ushort",  "System.UInt16"},
+        {"string",  "System.String"},
+        {"IntPtr",  "System.IntPtr"},
+        {"UIntPtr", "System.UIntPtr"}
+    };
+    auto renamed = cs2system.find(typeName);
+    return renamed != cs2system.end() ? renamed->second : typeName;
+}
+
+std::string TypePrinter::RenameToCSharp(const std::string &typeName)
+{
+    static const std::unordered_map<std::string, std::string> system2cs = {
+        {"System.Boolean", "bool"},
+        {"System.Byte",    "byte"},
+        {"System.SByte",   "sbyte"},
+        {"System.Char",    "char"},
+        {"System.Decimal", "decimal"},
+        {"System.Double",  "double"},
+        {"System.Single",  "float"},
+        {"System.Int32",   "int"},
+        {"System.UInt32",  "uint"},
+        {"System.Int64",   "long"},
+        {"System.UInt64",  "ulong"},
+        {"System.Object",  "object"},
+        {"System.Int16",   "short"},
+        {"System.UInt16",  "ushort"},
+        {"System.String",  "string"},
+        {"System.IntPtr",  "IntPtr"},
+        {"System.UIntPtr", "UIntPtr"}
+    };
+    auto renamed = system2cs.find(typeName);
+    return renamed != system2cs.end() ? renamed->second : typeName;
+}
+
 // From metadata.cpp
 
 /**********************************************************************\
@@ -219,6 +269,10 @@ HRESULT TypePrinter::NameForToken(mdTypeDef mb,
             hr = E_FAIL;
     }
     PAL_CPP_ENDTRY
+    if (SUCCEEDED(hr))
+    {
+        mdName = RenameToCSharp(mdName);
+    }
     return hr;
 }
 
@@ -344,10 +398,7 @@ HRESULT TypePrinter::GetTypeOfValue(ICorDebugType *pType, std::string &elementTy
 
                 if(SUCCEEDED(NameForToken(TokenFromRid(typeDef, mdtTypeDef), pMD, name, false, args)))
                 {
-                    if (name == "System.Decimal")
-                        ss << "decimal";
-                    else
-                        ss << name;
+                    ss << name;
                 }
             }
             elementType = ss.str();
index 6902515..974954f 100644 (file)
@@ -14,4 +14,6 @@ public:
     static HRESULT GetTypeOfValue(ICorDebugType *pType, std::string &elementType, std::string &arrayType);
     static HRESULT GetMethodName(ICorDebugFrame *pFrame, std::string &output);
     static HRESULT GetTypeAndMethod(ICorDebugFrame *pFrame, std::string &typeName, std::string &methodName);
+    static std::string RenameToSystem(const std::string &typeName);
+    static std::string RenameToCSharp(const std::string &typeName);
 };