From 0e993afe5869110facbde878908a190615e8ee35 Mon Sep 17 00:00:00 2001 From: Pent Ploompuu Date: Tue, 26 Jul 2011 13:15:05 +0300 Subject: [PATCH] Add an annotation to redundant ParenthesizedExpressions for PushNegation transform --- ICSharpCode.Decompiler/Ast/Annotations.cs | 7 +++++++ ICSharpCode.Decompiler/Ast/AstMethodBodyBuilder.cs | 2 +- ICSharpCode.Decompiler/Ast/Transforms/PushNegation.cs | 9 ++++++--- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/ICSharpCode.Decompiler/Ast/Annotations.cs b/ICSharpCode.Decompiler/Ast/Annotations.cs index 257f889c0..6673b2ce4 100644 --- a/ICSharpCode.Decompiler/Ast/Annotations.cs +++ b/ICSharpCode.Decompiler/Ast/Annotations.cs @@ -12,4 +12,11 @@ namespace ICSharpCode.Decompiler.Ast this.InferredType = inferredType; } } + + sealed class InvisibleParenthesis + { + InvisibleParenthesis() { } + + public static readonly InvisibleParenthesis Value = new InvisibleParenthesis(); + } } diff --git a/ICSharpCode.Decompiler/Ast/AstMethodBodyBuilder.cs b/ICSharpCode.Decompiler/Ast/AstMethodBodyBuilder.cs index e66fb914e..53fcf2ade 100644 --- a/ICSharpCode.Decompiler/Ast/AstMethodBodyBuilder.cs +++ b/ICSharpCode.Decompiler/Ast/AstMethodBodyBuilder.cs @@ -809,7 +809,7 @@ namespace ICSharpCode.Decompiler.Ast case ILCode.InitializedObject: return new InitializedObjectExpression(); case ILCode.Wrap: - return new ParenthesizedExpression(arg1); + return new ParenthesizedExpression(arg1).WithAnnotation(InvisibleParenthesis.Value); case ILCode.AddressOf: return MakeRef(arg1); case ILCode.NullableOf: diff --git a/ICSharpCode.Decompiler/Ast/Transforms/PushNegation.cs b/ICSharpCode.Decompiler/Ast/Transforms/PushNegation.cs index 89c5b6c39..d1aa138fe 100644 --- a/ICSharpCode.Decompiler/Ast/Transforms/PushNegation.cs +++ b/ICSharpCode.Decompiler/Ast/Transforms/PushNegation.cs @@ -150,9 +150,12 @@ namespace ICSharpCode.Decompiler.Ast.Transforms public override object VisitParenthesizedExpression(ParenthesizedExpression expr, object data) { // extra parentheses are redundant after this transformation - var res = expr.Expression; - expr.ReplaceWith(res); - return res.AcceptVisitor(this, data); + if(expr.Annotation() != null) { + var res = expr.Expression; + expr.ReplaceWith(res); + return res.AcceptVisitor(this, data); + } + return base.VisitParenthesizedExpression(expr, data); } } }