diff --git a/Decompiler.csproj b/Decompiler.csproj index 8cb0cef3c..0e8675aa1 100644 --- a/Decompiler.csproj +++ b/Decompiler.csproj @@ -65,6 +65,7 @@ + MainForm.cs diff --git a/src/AstBuilder.cs b/src/AstBuilder.cs index a1da9ac62..216a69a44 100644 --- a/src/AstBuilder.cs +++ b/src/AstBuilder.cs @@ -22,6 +22,11 @@ namespace Decompiler { CSharpOutputVisitor csOutVisitor = new CSharpOutputVisitor(); + astCompileUnit.AcceptVisitor(new Transforms.Ast.RemoveGotos(), null); + astCompileUnit.AcceptVisitor(new Transforms.Ast.RemoveDeadLabels(), null); + astCompileUnit.AcceptVisitor(new Transforms.Ast.SimplifyTypeReferences(), null); + + astCompileUnit.AcceptVisitor(csOutVisitor, null); string code = csOutVisitor.Text; diff --git a/src/AstMetodBodyBuilder.cs b/src/AstMetodBodyBuilder.cs index 6232f642b..b75b45c0e 100644 --- a/src/AstMetodBodyBuilder.cs +++ b/src/AstMetodBodyBuilder.cs @@ -50,9 +50,6 @@ namespace Decompiler astBlock.Children.AddRange(TransformNodes(bodyGraph.Childs)); - astBlock.AcceptVisitor(new Transforms.Ast.RemoveGotos(), null); - astBlock.AcceptVisitor(new Transforms.Ast.RemoveDeadLabels(), null); - return astBlock; } @@ -280,41 +277,41 @@ namespace Decompiler case Code.Clt_Un: return new Ast.BinaryOperatorExpression(arg1, BinaryOperatorType.LessThan, arg2); #endregion #region Conversions - case Code.Conv_I: return new Ast.CastExpression(new Ast.TypeReference(typeof(int).Name), arg1, CastType.Cast); // TODO - case Code.Conv_I1: return new Ast.CastExpression(new Ast.TypeReference(typeof(SByte).Name), arg1, CastType.Cast); - case Code.Conv_I2: return new Ast.CastExpression(new Ast.TypeReference(typeof(Int16).Name), arg1, CastType.Cast); - case Code.Conv_I4: return new Ast.CastExpression(new Ast.TypeReference(typeof(Int32).Name), arg1, CastType.Cast); - case Code.Conv_I8: return new Ast.CastExpression(new Ast.TypeReference(typeof(Int64).Name), arg1, CastType.Cast); - case Code.Conv_U: return new Ast.CastExpression(new Ast.TypeReference(typeof(uint).Name), arg1, CastType.Cast); // TODO - case Code.Conv_U1: return new Ast.CastExpression(new Ast.TypeReference(typeof(Byte).Name), arg1, CastType.Cast); - case Code.Conv_U2: return new Ast.CastExpression(new Ast.TypeReference(typeof(UInt16).Name), arg1, CastType.Cast); - case Code.Conv_U4: return new Ast.CastExpression(new Ast.TypeReference(typeof(UInt32).Name), arg1, CastType.Cast); - case Code.Conv_U8: return new Ast.CastExpression(new Ast.TypeReference(typeof(UInt64).Name), arg1, CastType.Cast); - case Code.Conv_R4: return new Ast.CastExpression(new Ast.TypeReference(typeof(float).Name), arg1, CastType.Cast); - case Code.Conv_R8: return new Ast.CastExpression(new Ast.TypeReference(typeof(double).Name), arg1, CastType.Cast); - case Code.Conv_R_Un: return new Ast.CastExpression(new Ast.TypeReference(typeof(double).Name), arg1, CastType.Cast); // TODO + case Code.Conv_I: return new Ast.CastExpression(new Ast.TypeReference(typeof(int).FullName), arg1, CastType.Cast); // TODO + case Code.Conv_I1: return new Ast.CastExpression(new Ast.TypeReference(typeof(SByte).FullName), arg1, CastType.Cast); + case Code.Conv_I2: return new Ast.CastExpression(new Ast.TypeReference(typeof(Int16).FullName), arg1, CastType.Cast); + case Code.Conv_I4: return new Ast.CastExpression(new Ast.TypeReference(typeof(Int32).FullName), arg1, CastType.Cast); + case Code.Conv_I8: return new Ast.CastExpression(new Ast.TypeReference(typeof(Int64).FullName), arg1, CastType.Cast); + case Code.Conv_U: return new Ast.CastExpression(new Ast.TypeReference(typeof(uint).FullName), arg1, CastType.Cast); // TODO + case Code.Conv_U1: return new Ast.CastExpression(new Ast.TypeReference(typeof(Byte).FullName), arg1, CastType.Cast); + case Code.Conv_U2: return new Ast.CastExpression(new Ast.TypeReference(typeof(UInt16).FullName), arg1, CastType.Cast); + case Code.Conv_U4: return new Ast.CastExpression(new Ast.TypeReference(typeof(UInt32).FullName), arg1, CastType.Cast); + case Code.Conv_U8: return new Ast.CastExpression(new Ast.TypeReference(typeof(UInt64).FullName), arg1, CastType.Cast); + case Code.Conv_R4: return new Ast.CastExpression(new Ast.TypeReference(typeof(float).FullName), arg1, CastType.Cast); + case Code.Conv_R8: return new Ast.CastExpression(new Ast.TypeReference(typeof(double).FullName), arg1, CastType.Cast); + case Code.Conv_R_Un: return new Ast.CastExpression(new Ast.TypeReference(typeof(double).FullName), arg1, CastType.Cast); // TODO - case Code.Conv_Ovf_I: return new Ast.CastExpression(new Ast.TypeReference(typeof(int).Name), arg1, CastType.Cast); // TODO - case Code.Conv_Ovf_I1: return new Ast.CastExpression(new Ast.TypeReference(typeof(SByte).Name), arg1, CastType.Cast); - case Code.Conv_Ovf_I2: return new Ast.CastExpression(new Ast.TypeReference(typeof(Int16).Name), arg1, CastType.Cast); - case Code.Conv_Ovf_I4: return new Ast.CastExpression(new Ast.TypeReference(typeof(Int32).Name), arg1, CastType.Cast); - case Code.Conv_Ovf_I8: return new Ast.CastExpression(new Ast.TypeReference(typeof(Int64).Name), arg1, CastType.Cast); - case Code.Conv_Ovf_U: return new Ast.CastExpression(new Ast.TypeReference(typeof(uint).Name), arg1, CastType.Cast); // TODO - case Code.Conv_Ovf_U1: return new Ast.CastExpression(new Ast.TypeReference(typeof(Byte).Name), arg1, CastType.Cast); - case Code.Conv_Ovf_U2: return new Ast.CastExpression(new Ast.TypeReference(typeof(UInt16).Name), arg1, CastType.Cast); - case Code.Conv_Ovf_U4: return new Ast.CastExpression(new Ast.TypeReference(typeof(UInt32).Name), arg1, CastType.Cast); - case Code.Conv_Ovf_U8: return new Ast.CastExpression(new Ast.TypeReference(typeof(UInt64).Name), arg1, CastType.Cast); + case Code.Conv_Ovf_I: return new Ast.CastExpression(new Ast.TypeReference(typeof(int).FullName), arg1, CastType.Cast); // TODO + case Code.Conv_Ovf_I1: return new Ast.CastExpression(new Ast.TypeReference(typeof(SByte).FullName), arg1, CastType.Cast); + case Code.Conv_Ovf_I2: return new Ast.CastExpression(new Ast.TypeReference(typeof(Int16).FullName), arg1, CastType.Cast); + case Code.Conv_Ovf_I4: return new Ast.CastExpression(new Ast.TypeReference(typeof(Int32).FullName), arg1, CastType.Cast); + case Code.Conv_Ovf_I8: return new Ast.CastExpression(new Ast.TypeReference(typeof(Int64).FullName), arg1, CastType.Cast); + case Code.Conv_Ovf_U: return new Ast.CastExpression(new Ast.TypeReference(typeof(uint).FullName), arg1, CastType.Cast); // TODO + case Code.Conv_Ovf_U1: return new Ast.CastExpression(new Ast.TypeReference(typeof(Byte).FullName), arg1, CastType.Cast); + case Code.Conv_Ovf_U2: return new Ast.CastExpression(new Ast.TypeReference(typeof(UInt16).FullName), arg1, CastType.Cast); + case Code.Conv_Ovf_U4: return new Ast.CastExpression(new Ast.TypeReference(typeof(UInt32).FullName), arg1, CastType.Cast); + case Code.Conv_Ovf_U8: return new Ast.CastExpression(new Ast.TypeReference(typeof(UInt64).FullName), arg1, CastType.Cast); - case Code.Conv_Ovf_I_Un: return new Ast.CastExpression(new Ast.TypeReference(typeof(int).Name), arg1, CastType.Cast); // TODO - case Code.Conv_Ovf_I1_Un: return new Ast.CastExpression(new Ast.TypeReference(typeof(SByte).Name), arg1, CastType.Cast); - case Code.Conv_Ovf_I2_Un: return new Ast.CastExpression(new Ast.TypeReference(typeof(Int16).Name), arg1, CastType.Cast); - case Code.Conv_Ovf_I4_Un: return new Ast.CastExpression(new Ast.TypeReference(typeof(Int32).Name), arg1, CastType.Cast); - case Code.Conv_Ovf_I8_Un: return new Ast.CastExpression(new Ast.TypeReference(typeof(Int64).Name), arg1, CastType.Cast); - case Code.Conv_Ovf_U_Un: return new Ast.CastExpression(new Ast.TypeReference(typeof(uint).Name), arg1, CastType.Cast); // TODO - case Code.Conv_Ovf_U1_Un: return new Ast.CastExpression(new Ast.TypeReference(typeof(Byte).Name), arg1, CastType.Cast); - case Code.Conv_Ovf_U2_Un: return new Ast.CastExpression(new Ast.TypeReference(typeof(UInt16).Name), arg1, CastType.Cast); - case Code.Conv_Ovf_U4_Un: return new Ast.CastExpression(new Ast.TypeReference(typeof(UInt32).Name), arg1, CastType.Cast); - case Code.Conv_Ovf_U8_Un: return new Ast.CastExpression(new Ast.TypeReference(typeof(UInt64).Name), arg1, CastType.Cast); + case Code.Conv_Ovf_I_Un: return new Ast.CastExpression(new Ast.TypeReference(typeof(int).FullName), arg1, CastType.Cast); // TODO + case Code.Conv_Ovf_I1_Un: return new Ast.CastExpression(new Ast.TypeReference(typeof(SByte).FullName), arg1, CastType.Cast); + case Code.Conv_Ovf_I2_Un: return new Ast.CastExpression(new Ast.TypeReference(typeof(Int16).FullName), arg1, CastType.Cast); + case Code.Conv_Ovf_I4_Un: return new Ast.CastExpression(new Ast.TypeReference(typeof(Int32).FullName), arg1, CastType.Cast); + case Code.Conv_Ovf_I8_Un: return new Ast.CastExpression(new Ast.TypeReference(typeof(Int64).FullName), arg1, CastType.Cast); + case Code.Conv_Ovf_U_Un: return new Ast.CastExpression(new Ast.TypeReference(typeof(uint).FullName), arg1, CastType.Cast); // TODO + case Code.Conv_Ovf_U1_Un: return new Ast.CastExpression(new Ast.TypeReference(typeof(Byte).FullName), arg1, CastType.Cast); + case Code.Conv_Ovf_U2_Un: return new Ast.CastExpression(new Ast.TypeReference(typeof(UInt16).FullName), arg1, CastType.Cast); + case Code.Conv_Ovf_U4_Un: return new Ast.CastExpression(new Ast.TypeReference(typeof(UInt32).FullName), arg1, CastType.Cast); + case Code.Conv_Ovf_U8_Un: return new Ast.CastExpression(new Ast.TypeReference(typeof(UInt64).FullName), arg1, CastType.Cast); #endregion #region Indirect case Code.Ldind_I: throw new NotImplementedException(); diff --git a/src/Transforms/Ast/SimplifyTypeReferences.cs b/src/Transforms/Ast/SimplifyTypeReferences.cs new file mode 100644 index 000000000..5be0b9a06 --- /dev/null +++ b/src/Transforms/Ast/SimplifyTypeReferences.cs @@ -0,0 +1,77 @@ +using System; + +using ICSharpCode.NRefactory.Ast; +using ICSharpCode.NRefactory.Visitors; + +namespace Decompiler.Transforms.Ast +{ + public class SimplifyTypeReferences: AbstractAstTransformer + { + string currentClass = null; + + public override object VisitTypeDeclaration(TypeDeclaration typeDeclaration, object data) + { + currentClass = typeDeclaration.Name; + base.VisitTypeDeclaration(typeDeclaration, data); + currentClass = null; + return null; + } + + public override object VisitMemberReferenceExpression(MemberReferenceExpression memberReferenceExpression, object data) + { + IdentifierExpression id = memberReferenceExpression.TargetObject as IdentifierExpression; + if (id != null) { + if (id.Identifier == "System" || id.Identifier == currentClass) { + ReplaceCurrentNode(new IdentifierExpression(memberReferenceExpression.FieldName)); + return null; + } + if (id.Identifier.StartsWith("System.")) { + id.Identifier = id.Identifier.Replace("System.", ""); + return null; + } + } + return base.VisitMemberReferenceExpression(memberReferenceExpression, data); + } + + public override object VisitTypeReference(TypeReference typeReference, object data) + { + string fullName = typeReference.Type; + string shortName = GetShortName(fullName); + if (shortName != null) { + typeReference.Type = shortName; + return null; + } + if (fullName.EndsWith("[]")) { + shortName = GetShortName(fullName.Replace("[]","")) + "[]"; + if (shortName != null) { + typeReference.Type = shortName; + return null; + } + } + return null; + } + + public string GetShortName(string fullName) + { + switch(fullName) { + case "System.Boolean": return "bool"; + case "System.Byte": return "byte"; + case "System.Char": return "char"; + case "System.Decimal": return "decimal"; + case "System.Double": return "double"; + case "System.Single": return "float"; + case "System.Int32": return "int"; + case "System.Int64": return "long"; + case "System.Object": return "object"; + case "System.SByte": return "sbyte"; + case "System.Int16": return "short"; + case "System.String": return "string"; + case "System.UInt32": return "uint"; + case "System.UInt64": return "ulong"; + case "System.UInt16": return "ushort"; + case "System.Void": return "void"; + } + return null; + } + } +}