Browse Source

Add ConvertConstructorCallIntoInitializer transformation step.

pull/10/head
Daniel Grunwald 15 years ago
parent
commit
d1230081d7
  1. 3
      ICSharpCode.Decompiler/Ast/AstBuilder.cs
  2. 3
      ICSharpCode.Decompiler/Ast/NRefactoryExtensions.cs
  3. 42
      ICSharpCode.Decompiler/Ast/Transforms/ConvertConstructorCallIntoInitializer.cs
  4. 1
      ICSharpCode.Decompiler/ICSharpCode.Decompiler.csproj

3
ICSharpCode.Decompiler/Ast/AstBuilder.cs

@ -37,8 +37,7 @@ namespace Decompiler @@ -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);

3
ICSharpCode.Decompiler/Ast/NRefactoryExtensions.cs

@ -10,7 +10,8 @@ namespace Decompiler @@ -10,7 +10,8 @@ namespace Decompiler
{
public static T WithAnnotation<T>(this T node, object annotation) where T : AstNode
{
node.AddAnnotation(annotation);
if (annotation != null)
node.AddAnnotation(annotation);
return node;
}
}

42
ICSharpCode.Decompiler/Ast/Transforms/ConvertConstructorCallIntoInitializer.cs

@ -0,0 +1,42 @@ @@ -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<object, object>
{
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<MethodReference>());
// Remove the statement:
stmt.Remove();
}
return null;
}
}
}

1
ICSharpCode.Decompiler/ICSharpCode.Decompiler.csproj

@ -54,6 +54,7 @@ @@ -54,6 +54,7 @@
<Compile Include="Ast\CommentStatement.cs" />
<Compile Include="Ast\NRefactoryExtensions.cs" />
<Compile Include="Ast\TextOutputFormatter.cs" />
<Compile Include="Ast\Transforms\ConvertConstructorCallIntoInitializer.cs" />
<Compile Include="Ast\Transforms\ReplaceMethodCallsWithOperators.cs" />
<Compile Include="Ast\Transforms\PushNegation.cs" />
<Compile Include="Ast\Transforms\RemoveEmptyElseBody.cs" />

Loading…
Cancel
Save