Browse Source

Replace 'i += 1' with 'i++'. Closes #33.

pull/37/head
Daniel Grunwald 15 years ago
parent
commit
e8f9febcc0
  1. 8
      ICSharpCode.Decompiler/Ast/NRefactoryExtensions.cs
  2. 20
      ICSharpCode.Decompiler/Ast/Transforms/ReplaceMethodCallsWithOperators.cs

8
ICSharpCode.Decompiler/Ast/NRefactoryExtensions.cs

@ -15,6 +15,14 @@ namespace Decompiler
return node; return node;
} }
public static T CopyAnnotationsFrom<T>(this T node, AstNode other) where T : AstNode
{
foreach (var annotation in other.Annotations<object>()) {
node.AddAnnotation(annotation);
}
return node;
}
public static T Detach<T>(this T node) where T : AstNode public static T Detach<T>(this T node) where T : AstNode
{ {
node.Remove(); node.Remove();

20
ICSharpCode.Decompiler/Ast/Transforms/ReplaceMethodCallsWithOperators.cs

@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using ICSharpCode.NRefactory.CSharp.PatternMatching;
using Mono.Cecil; using Mono.Cecil;
using Ast = ICSharpCode.NRefactory.CSharp; using Ast = ICSharpCode.NRefactory.CSharp;
using ICSharpCode.NRefactory.CSharp; using ICSharpCode.NRefactory.CSharp;
@ -142,7 +143,7 @@ namespace Decompiler.Transforms
public override object VisitAssignmentExpression(AssignmentExpression assignment, object data) public override object VisitAssignmentExpression(AssignmentExpression assignment, object data)
{ {
base.VisitAssignmentExpression(assignment, data); base.VisitAssignmentExpression(assignment, data);
// First, combine "x = x op y" into "x op= y" // Combine "x = x op y" into "x op= y"
BinaryOperatorExpression binary = assignment.Right as BinaryOperatorExpression; BinaryOperatorExpression binary = assignment.Right as BinaryOperatorExpression;
if (binary != null && assignment.Operator == AssignmentOperatorType.Assign) { if (binary != null && assignment.Operator == AssignmentOperatorType.Assign) {
if (IsWithoutSideEffects(assignment.Left) && assignment.Left.Match(binary.Left) != null) { if (IsWithoutSideEffects(assignment.Left) && assignment.Left.Match(binary.Left) != null) {
@ -180,10 +181,27 @@ namespace Decompiler.Transforms
} }
if (assignment.Operator != AssignmentOperatorType.Assign) { if (assignment.Operator != AssignmentOperatorType.Assign) {
// If we found a shorter operator, get rid of the BinaryOperatorExpression: // If we found a shorter operator, get rid of the BinaryOperatorExpression:
assignment.CopyAnnotationsFrom(binary);
assignment.Right = binary.Right; assignment.Right = binary.Right;
} }
} }
} }
if (assignment.Operator == AssignmentOperatorType.Add || assignment.Operator == AssignmentOperatorType.Subtract) {
// detect increment/decrement
if (assignment.Right.Match(new PrimitiveExpression(1)) != null) {
// only if it's not a custom operator
if (assignment.Annotation<MethodReference>() == null) {
UnaryOperatorType type;
// When the parent is an expression statement, pre- or post-increment doesn't matter;
// so we can pick post-increment which is more commonly used (for (int i = 0; i < x; i++))
if (assignment.Parent is ExpressionStatement)
type = (assignment.Operator == AssignmentOperatorType.Add) ? UnaryOperatorType.PostIncrement : UnaryOperatorType.PostDecrement;
else
type = (assignment.Operator == AssignmentOperatorType.Add) ? UnaryOperatorType.Increment : UnaryOperatorType.Decrement;
assignment.ReplaceWith(new UnaryOperatorExpression(type, assignment.Left.Detach()).CopyAnnotationsFrom(assignment));
}
}
}
return null; return null;
} }

Loading…
Cancel
Save