Browse Source

Reduce "String.Concat(a, b)" to "a + b"

pull/1/head^2
David Srbecký 18 years ago
parent
commit
27d70d6897
  1. 1
      Decompiler.csproj
  2. 1
      src/AstBuilder.cs
  3. 35
      src/Transforms/Ast/Idioms.cs

1
Decompiler.csproj

@ -66,6 +66,7 @@ @@ -66,6 +66,7 @@
<Compile Include="src\ByteCode.StackAnalysis.cs" />
<Compile Include="src\StackExpression.cs" />
<Compile Include="src\StackExpressionCollection.cs" />
<Compile Include="src\Transforms\Ast\Idioms.cs" />
<Compile Include="src\Transforms\Ast\RemoveDeadLabels.cs" />
<Compile Include="src\Transforms\Ast\RemoveEmptyElseBody.cs" />
<Compile Include="src\Transforms\Ast\RemoveGotos.cs" />

1
src/AstBuilder.cs

@ -27,6 +27,7 @@ namespace Decompiler @@ -27,6 +27,7 @@ namespace Decompiler
astCompileUnit.AcceptVisitor(new Transforms.Ast.RemoveGotos(), null);
astCompileUnit.AcceptVisitor(new Transforms.Ast.RemoveDeadLabels(), null);
astCompileUnit.AcceptVisitor(new Transforms.Ast.SimplifyTypeReferences(), null);
astCompileUnit.AcceptVisitor(new Transforms.Ast.Idioms(), null);
astCompileUnit.AcceptVisitor(new Transforms.Ast.RemoveEmptyElseBody(), null);
astCompileUnit.AcceptVisitor(new Transforms.Ast.RestoreLoop(), null);
astCompileUnit.AcceptVisitor(new Transforms.Ast.RemoveDeadLabels(), null);

35
src/Transforms/Ast/Idioms.cs

@ -0,0 +1,35 @@ @@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;
using Ast = ICSharpCode.NRefactory.Ast;
using ICSharpCode.NRefactory.Ast;
using ICSharpCode.NRefactory.Visitors;
namespace Decompiler.Transforms.Ast
{
public class Idioms: AbstractAstTransformer
{
public override object VisitInvocationExpression(InvocationExpression invocationExpression, object data)
{
base.VisitInvocationExpression(invocationExpression, data);
// Reduce "String.Concat(a, b)" to "a + b"
MemberReferenceExpression target = invocationExpression.TargetObject as MemberReferenceExpression;
if (target != null &&
target.MemberName == "Concat" &&
invocationExpression.Arguments.Count == 2 &&
target.TargetObject is IdentifierExpression &&
(target.TargetObject as IdentifierExpression).Identifier == "String")
{
ReplaceCurrentNode(
new BinaryOperatorExpression(
invocationExpression.Arguments[0],
BinaryOperatorType.Add,
invocationExpression.Arguments[1]
)
);
}
return null;
}
}
}
Loading…
Cancel
Save