Skip to content

Commit

Permalink
Improve C++ decompiler
Browse files Browse the repository at this point in the history
Support pointers and generics

Issue: #98
  • Loading branch information
MSDN-WhiteKnight committed Aug 23, 2022
1 parent 9f53538 commit c929174
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 72 deletions.
106 changes: 42 additions & 64 deletions CilView.Core/SourceCode/CppDecompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,50 @@ static void GetTypeTokens(Type t, List<SourceToken> target)
return;
}

if (t.IsPointer)
{
Type elementType = t.GetElementType();

if (Utils.StringEquals(elementType.FullName, "System.Void"))
{
target.Add(new SourceToken("void", TokenKind.Keyword));
}
else
{
GetTypeTokens(elementType, target);
}

target.Add(new SourceToken("*", TokenKind.Punctuation));
return;
}

if (t.IsGenericParameter)
{
target.Add(new SourceToken(t.Name, TokenKind.Name));
return;
}

if (t.IsGenericType)
{
StringBuilder sb = new StringBuilder(100);
sb.Append(GetGenericDefinitionName(t.Name));
sb.Append('<');
target.Add(new SourceToken(GetGenericDefinitionName(t.Name), TokenKind.TypeName));
target.Add(new SourceToken("<", TokenKind.Punctuation));
Type[] args = t.GetGenericArguments();

for (int i = 0; i < args.Length; i++)
{
if (i >= 1) sb.Append(", ");
if (i >= 1) target.Add(new SourceToken(",", TokenKind.Punctuation, "", " "));

GetTypeTokens(args[i], target);
}

sb.Append(GetTypeString(args[i]));
target.Add(new SourceToken(">", TokenKind.Punctuation));

if (t.IsClass || t.IsInterface)
{
target.Add(new SourceToken("^", TokenKind.Punctuation, " ", ""));
return;
}

sb.Append("> ^");
target.Add(new SourceToken(sb.ToString(), TokenKind.Unknown));
return;
}

Expand Down Expand Up @@ -92,54 +120,6 @@ static void GetTypeTokens(Type t, List<SourceToken> target)
target.Add(new SourceToken(t.Name, TokenKind.TypeName));
}
}

static string GetTypeString(Type t)
{
if (t == null) return string.Empty;

if (t.IsArray && t.GetArrayRank() == 1)
{
StringBuilder sb = new StringBuilder(100);
sb.Append("array <");
sb.Append(GetTypeString(t.GetElementType()));
sb.Append("> ^");
return sb.ToString();
}

if (t.IsGenericType)
{
StringBuilder sb = new StringBuilder(100);
sb.Append(GetGenericDefinitionName(t.Name));
sb.Append('<');
Type[] args = t.GetGenericArguments();

for (int i = 0; i < args.Length; i++)
{
if (i >= 1) sb.Append(", ");

sb.Append(GetTypeString(args[i]));
}

sb.Append("> ^");
return sb.ToString();
}

//reference types are represented by handles in C++/CLI
if (t.IsClass || t.IsInterface) return t.Name + " ^";

//process built-in types
SourceToken tok = ProcessCommonTypes(t);

if (tok != null) return tok.Content;

if (Utils.TypeEquals(t, typeof(uint))) return "unsigned int";
else if (Utils.TypeEquals(t, typeof(ushort))) return "unsigned short";
else if (Utils.TypeEquals(t, typeof(byte))) return "unsigned char";
else if (Utils.TypeEquals(t, typeof(sbyte))) return "signed char";
else if (Utils.TypeEquals(t, typeof(char))) return "wchar_t";

return t.Name;
}

public override IEnumerable<SourceToken> GetMethodSigTokens()
{
Expand Down Expand Up @@ -172,21 +152,19 @@ public override IEnumerable<SourceToken> GetMethodSigTokens()

if (m.IsGenericMethod)
{
StringBuilder sb = new StringBuilder(100);
sb.Append("generic <");

ret.Add(new SourceToken("generic", TokenKind.Keyword, "", " "));
ret.Add(new SourceToken("<", TokenKind.Punctuation));
Type[] args = m.GetGenericArguments();

for (int i = 0; i < args.Length; i++)
{
if (i >= 1) sb.Append(", ");
if (i >= 1) ret.Add(new SourceToken(",", TokenKind.Punctuation, "", " "));

sb.Append("typename ");
sb.Append(args[i].Name);
ret.Add(new SourceToken("typename", TokenKind.Keyword, "", " "));
ret.Add(new SourceToken(args[i].Name, TokenKind.Name));
}

sb.Append('>');
sb.AppendLine();
ret.Add(new SourceToken(sb.ToString(), TokenKind.Unknown));
ret.Add(new SourceToken(">", TokenKind.Punctuation, "", Environment.NewLine));
}

if (!isGlobalFunc)
Expand Down
18 changes: 10 additions & 8 deletions tests/CilView.Tests/SourceCode/DecompilerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void Test_Decompiler_CalcSum(string lang, string expected)

[TestMethod]
[DataRow(".cs", "public static T[] GenerateArray<T>(int len)")]
[DataRow(".cpp", "public: generic <typename T>\nstatic array <T ^> ^ GenerateArray(int len){")]
[DataRow(".cpp", "public: generic <typename T>\nstatic array <T> ^ GenerateArray(int len){")]
public void Test_Decompiler_Generic(string lang, string expected)
{
MethodBase mb = typeof(SampleMethods).GetMethod("GenerateArray");
Expand Down Expand Up @@ -86,22 +86,24 @@ public void Test_Decompiler_ByRef()
string expected = "public static bool DivideNumbers(int x, int y, ref int result)";
Assert.AreEqual(expected, s);
}

[TestMethod]
public void Test_Decompiler_Pointer()
[DataRow(".cs", "public static object Box(void* ptr, Type type)")]
[DataRow(".cpp", "public: static Object ^ Box(void* ptr, Type ^ type){")]
public void Test_Decompiler_Pointer(string lang, string expected)
{
MethodBase mb = typeof(Pointer).GetMethod("Box");
string s = Decompiler.GetMethodSignatureString(".cs", mb);
string expected = "public static object Box(void* ptr, Type type)";
string s = Decompiler.GetMethodSignatureString(lang, mb);
Assert.AreEqual(expected, s);
}

[TestMethod]
public void Test_Decompiler_GenericType()
[DataRow(".cs", "IEnumerator<T> GetEnumerator();")]
[DataRow(".cpp", "public: IEnumerator<T> ^ GetEnumerator(){")]
public void Test_Decompiler_GenericType(string lang, string expected)
{
MethodBase mb = typeof(IEnumerable<>).GetMethod("GetEnumerator");
string s = Decompiler.GetMethodSignatureString(".cs", mb);
string expected = "IEnumerator<T> GetEnumerator();";
string s = Decompiler.GetMethodSignatureString(lang, mb);
Assert.AreEqual(expected, s);
}

Expand Down

0 comments on commit c929174

Please sign in to comment.