From d1230081d7054cdb9e1e081514c9adaf3476eb06 Mon Sep 17 00:00:00 2001 From: Daniel Grunwald Date: Tue, 15 Feb 2011 01:15:27 +0100 Subject: [PATCH] Add ConvertConstructorCallIntoInitializer transformation step. --- ICSharpCode.Decompiler/Ast/AstBuilder.cs | 3 +- .../Ast/NRefactoryExtensions.cs | 3 +- .../ConvertConstructorCallIntoInitializer.cs | 42 +++++++++++++++++++ .../ICSharpCode.Decompiler.csproj | 1 + 4 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 ICSharpCode.Decompiler/Ast/Transforms/ConvertConstructorCallIntoInitializer.cs diff --git a/ICSharpCode.Decompiler/Ast/AstBuilder.cs b/ICSharpCode.Decompiler/Ast/AstBuilder.cs index 0d634fab4..5c105f0f2 100644 --- a/ICSharpCode.Decompiler/Ast/AstBuilder.cs +++ b/ICSharpCode.Decompiler/Ast/AstBuilder.cs @@ -37,8 +37,7 @@ namespace Decompiler //astCompileUnit.AcceptVisitor(new Transforms.Ast.RestoreLoop(), null); } - // Run ReplaceMethodCallsWithOperators at the end only - this step transforms custom operator calls - // into operator expressions, and we don't want other simplification steps to change those later on. + astCompileUnit.AcceptVisitor(new Transforms.Ast.ConvertConstructorCallIntoInitializer(), null); astCompileUnit.AcceptVisitor(new Transforms.Ast.ReplaceMethodCallsWithOperators(), null); astCompileUnit.AcceptVisitor(new InsertParenthesesVisitor { InsertParenthesesForReadability = true }, null); diff --git a/ICSharpCode.Decompiler/Ast/NRefactoryExtensions.cs b/ICSharpCode.Decompiler/Ast/NRefactoryExtensions.cs index 729caba28..f7fe21949 100644 --- a/ICSharpCode.Decompiler/Ast/NRefactoryExtensions.cs +++ b/ICSharpCode.Decompiler/Ast/NRefactoryExtensions.cs @@ -10,7 +10,8 @@ namespace Decompiler { public static T WithAnnotation(this T node, object annotation) where T : AstNode { - node.AddAnnotation(annotation); + if (annotation != null) + node.AddAnnotation(annotation); return node; } } diff --git a/ICSharpCode.Decompiler/Ast/Transforms/ConvertConstructorCallIntoInitializer.cs b/ICSharpCode.Decompiler/Ast/Transforms/ConvertConstructorCallIntoInitializer.cs new file mode 100644 index 000000000..247649042 --- /dev/null +++ b/ICSharpCode.Decompiler/Ast/Transforms/ConvertConstructorCallIntoInitializer.cs @@ -0,0 +1,42 @@ +// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt) +// This code is distributed under MIT X11 license (for details please see \doc\license.txt) + +using System; +using System.Linq; +using ICSharpCode.NRefactory.CSharp; +using Mono.Cecil; + +namespace Decompiler.Transforms.Ast +{ + public class ConvertConstructorCallIntoInitializer : DepthFirstAstVisitor + { + public override object VisitConstructorDeclaration(ConstructorDeclaration constructorDeclaration, object data) + { + ExpressionStatement stmt = constructorDeclaration.Body.Statements.FirstOrDefault() as ExpressionStatement; + if (stmt == null) + return null; + InvocationExpression invocation = stmt.Expression as InvocationExpression; + if (invocation == null) + return null; + MemberReferenceExpression mre = invocation.Target as MemberReferenceExpression; + if (mre != null && mre.MemberName == ".ctor") { + ConstructorInitializer ci = new ConstructorInitializer(); + if (mre.Target is ThisReferenceExpression) + ci.ConstructorInitializerType = ConstructorInitializerType.This; + else if (mre.Target is BaseReferenceExpression) + ci.ConstructorInitializerType = ConstructorInitializerType.Base; + else + return null; + // Move arguments from invocation to initializer: + var arguments = invocation.Arguments.ToArray(); + invocation.Arguments = null; + ci.Arguments = arguments; + // Add the initializer: + constructorDeclaration.Initializer = ci.WithAnnotation(invocation.Annotation()); + // Remove the statement: + stmt.Remove(); + } + return null; + } + } +} diff --git a/ICSharpCode.Decompiler/ICSharpCode.Decompiler.csproj b/ICSharpCode.Decompiler/ICSharpCode.Decompiler.csproj index 6dda36d1b..a0ed6defa 100644 --- a/ICSharpCode.Decompiler/ICSharpCode.Decompiler.csproj +++ b/ICSharpCode.Decompiler/ICSharpCode.Decompiler.csproj @@ -54,6 +54,7 @@ +