Skip to content

Commit

Permalink
Improve C# decompiler
Browse files Browse the repository at this point in the history
Support byrefs, pointers and generic types

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

if (t.IsByRef)
{
target.Add(new SourceToken("ref", TokenKind.Keyword, "", " "));
GetTypeTokens(t.GetElementType(), 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, "", " "));

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

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

Expand All @@ -67,51 +95,6 @@ static void GetTypeTokens(Type t, List<SourceToken> target)
else if (Utils.TypeEquals(t, typeof(object))) target.Add(new SourceToken("object", TokenKind.Keyword));
else 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)
{
return GetTypeString(t.GetElementType()) + "[]";
}

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();
}

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

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

if (Utils.TypeEquals(t, typeof(string))) return "string";
else if (Utils.TypeEquals(t, typeof(uint))) return "uint";
else if (Utils.TypeEquals(t, typeof(ushort))) return "ushort";
else if (Utils.TypeEquals(t, typeof(long))) return "long";
else if (Utils.TypeEquals(t, typeof(ulong))) return "ulong";
else if (Utils.TypeEquals(t, typeof(byte))) return "byte";
else if (Utils.TypeEquals(t, typeof(sbyte))) return "sbyte";
else if (Utils.TypeEquals(t, typeof(char))) return "char";
else if (Utils.TypeEquals(t, typeof(object))) return "object";

return t.Name;
}

static void GetModifiers(MethodBase m, List<SourceToken> target)
{
Expand Down Expand Up @@ -191,19 +174,17 @@ public override IEnumerable<SourceToken> GetMethodSigTokens()

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

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(args[i].Name);
ret.Add(new SourceToken(args[i].Name, TokenKind.Name));
}

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

ret.Add(new SourceToken("(", TokenKind.Punctuation));
Expand Down
27 changes: 27 additions & 0 deletions tests/CilView.Tests/SourceCode/DecompilerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,33 @@ public void Test_Decompiler_InstanceMethod(string lang, string expected)
Assert.AreEqual(expected, s);
}

[TestMethod]
public void Test_Decompiler_ByRef()
{
MethodBase mb = typeof(SampleMethods).GetMethod("DivideNumbers");
string s = Decompiler.GetMethodSignatureString(".cs", mb);
string expected = "public static bool DivideNumbers(int x, int y, ref int result)";
Assert.AreEqual(expected, s);
}

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

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

[TestMethod]
public void Test_DecompileMethodSignature_Csharp()
{
Expand Down

0 comments on commit 9f53538

Please sign in to comment.