From 8ee222b37370c9c851c6e4b733a2da4dd072939b Mon Sep 17 00:00:00 2001 From: Siegfried Pammer Date: Fri, 29 Sep 2017 23:55:06 +0200 Subject: [PATCH] Remove old switch-on-string code from PatternStatementTransform --- .../CSharp/CSharpDecompiler.cs | 8 +- .../Transforms/PatternStatementTransform.cs | 172 ------------------ 2 files changed, 4 insertions(+), 176 deletions(-) diff --git a/ICSharpCode.Decompiler/CSharp/CSharpDecompiler.cs b/ICSharpCode.Decompiler/CSharp/CSharpDecompiler.cs index b6ff80b98..b488578ba 100644 --- a/ICSharpCode.Decompiler/CSharp/CSharpDecompiler.cs +++ b/ICSharpCode.Decompiler/CSharp/CSharpDecompiler.cs @@ -201,8 +201,8 @@ namespace ICSharpCode.Decompiler.CSharp if (settings.AsyncAwait && AsyncAwaitDecompiler.IsCompilerGeneratedStateMachine(type)) return true; } else if (type.IsCompilerGenerated()) { -// if (type.Name.StartsWith("", StringComparison.Ordinal)) -// return true; + if (type.Name.StartsWith("", StringComparison.Ordinal)) + return true; if (settings.AnonymousTypes && type.IsAnonymousType()) return true; } @@ -215,8 +215,8 @@ namespace ICSharpCode.Decompiler.CSharp return true; if (settings.AutomaticProperties && IsAutomaticPropertyBackingField(field)) return true; -// if (settings.SwitchStatementOnString && IsSwitchOnStringCache(field)) -// return true; + if (settings.SwitchStatementOnString && IsSwitchOnStringCache(field)) + return true; } // event-fields are not [CompilerGenerated] if (settings.AutomaticEvents && field.DeclaringType.Events.Any(ev => ev.Name == field.Name)) diff --git a/ICSharpCode.Decompiler/CSharp/Transforms/PatternStatementTransform.cs b/ICSharpCode.Decompiler/CSharp/Transforms/PatternStatementTransform.cs index d0684ee4b..a8e29430e 100644 --- a/ICSharpCode.Decompiler/CSharp/Transforms/PatternStatementTransform.cs +++ b/ICSharpCode.Decompiler/CSharp/Transforms/PatternStatementTransform.cs @@ -96,11 +96,6 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms public override AstNode VisitIfElseStatement(IfElseStatement ifElseStatement) { - if (context.Settings.SwitchStatementOnString) { - AstNode result = TransformSwitchOnString(ifElseStatement); - if (result != null) - return result; - } AstNode simplifiedIfElse = SimplifyCascadingIfElseStatements(ifElseStatement); if (simplifiedIfElse != null) return simplifiedIfElse; @@ -256,174 +251,7 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms return null; } #endregion - - #region switch on strings - static readonly IfElseStatement switchOnStringPattern = new IfElseStatement { - Condition = new BinaryOperatorExpression { - Left = new AnyNode("switchExpr"), - Operator = BinaryOperatorType.InEquality, - Right = new NullReferenceExpression() - }, - TrueStatement = new BlockStatement { - new IfElseStatement { - Condition = new BinaryOperatorExpression { - Left = new AnyNode("cachedDict"), - Operator = BinaryOperatorType.Equality, - Right = new NullReferenceExpression() - }, - TrueStatement = new AnyNode("dictCreation") - }, - new IfElseStatement { - Condition = new InvocationExpression(new MemberReferenceExpression(new Backreference("cachedDict").ToExpression(), "TryGetValue"), - new NamedNode("switchVar", new IdentifierExpression(Pattern.AnyString)), - new DirectionExpression { - FieldDirection = FieldDirection.Out, - Expression = new IdentifierExpression(Pattern.AnyString).WithName("intVar") - }), - TrueStatement = new BlockStatement { - Statements = { - new NamedNode( - "switch", new SwitchStatement { - Expression = new IdentifierExpressionBackreference("intVar"), - SwitchSections = { new Repeat(new AnyNode()) } - }) - } - } - }, - new Repeat(new AnyNode("nonNullDefaultStmt")).ToStatement() - }, - FalseStatement = new OptionalNode("nullStmt", new BlockStatement { Statements = { new Repeat(new AnyNode()) } }) - }; - - public SwitchStatement TransformSwitchOnString(IfElseStatement node) - { - Match m = switchOnStringPattern.Match(node); - if (!m.Success) - return null; - // switchVar must be the same as switchExpr; or switchExpr must be an assignment and switchVar the left side of that assignment - if (!m.Get("switchVar").Single().IsMatch(m.Get("switchExpr").Single())) { - AssignmentExpression assign = m.Get("switchExpr").Single() as AssignmentExpression; - if (!(assign != null && m.Get("switchVar").Single().IsMatch(assign.Left))) - return null; - } - FieldReference cachedDictField = m.Get("cachedDict").Single().Annotation(); - if (cachedDictField == null) - return null; - List dictCreation = m.Get("dictCreation").Single().Statements.ToList(); - List> dict = BuildDictionary(dictCreation); - SwitchStatement sw = m.Get("switch").Single(); - sw.Expression = m.Get("switchExpr").Single().Detach(); - foreach (SwitchSection section in sw.SwitchSections) { - List labels = section.CaseLabels.ToList(); - section.CaseLabels.Clear(); - foreach (CaseLabel label in labels) { - PrimitiveExpression expr = label.Expression as PrimitiveExpression; - if (expr == null || !(expr.Value is int)) - continue; - int val = (int)expr.Value; - foreach (var pair in dict) { - if (pair.Value == val) - section.CaseLabels.Add(new CaseLabel { Expression = new PrimitiveExpression(pair.Key) }); - } - } - } - if (m.Has("nullStmt")) { - SwitchSection section = new SwitchSection(); - section.CaseLabels.Add(new CaseLabel { Expression = new NullReferenceExpression() }); - BlockStatement block = m.Get("nullStmt").Single(); - block.Statements.Add(new BreakStatement()); - section.Statements.Add(block.Detach()); - sw.SwitchSections.Add(section); - } else if (m.Has("nonNullDefaultStmt")) { - sw.SwitchSections.Add( - new SwitchSection { - CaseLabels = { new CaseLabel { Expression = new NullReferenceExpression() } }, - Statements = { new BlockStatement { new BreakStatement() } } - }); - } - if (m.Has("nonNullDefaultStmt")) { - SwitchSection section = new SwitchSection(); - section.CaseLabels.Add(new CaseLabel()); - BlockStatement block = new BlockStatement(); - block.Statements.AddRange(m.Get("nonNullDefaultStmt").Select(s => s.Detach())); - block.Add(new BreakStatement()); - section.Statements.Add(block); - sw.SwitchSections.Add(section); - } - node.ReplaceWith(sw); - return sw; - } - - List> BuildDictionary(List dictCreation) - { - if (context.Settings.ObjectOrCollectionInitializers && dictCreation.Count == 1) - return BuildDictionaryFromInitializer(dictCreation[0]); - return BuildDictionaryFromAddMethodCalls(dictCreation); - } - - static readonly Statement assignInitializedDictionary = new ExpressionStatement { - Expression = new AssignmentExpression { - Left = new AnyNode().ToExpression(), - Right = new ObjectCreateExpression { - Type = new AnyNode(), - Arguments = { new Repeat(new AnyNode()) }, - Initializer = new ArrayInitializerExpression { - Elements = { new Repeat(new AnyNode("dictJumpTable")) } - } - }, - }, - }; - - private List> BuildDictionaryFromInitializer(Statement statement) - { - List> dict = new List>(); - Match m = assignInitializedDictionary.Match(statement); - if (!m.Success) - return dict; - - foreach (ArrayInitializerExpression initializer in m.Get("dictJumpTable")) { - KeyValuePair pair; - if (TryGetPairFrom(initializer.Elements, out pair)) - dict.Add(pair); - } - - return dict; - } - - private static List> BuildDictionaryFromAddMethodCalls(List dictCreation) - { - List> dict = new List>(); - for (int i = 0; i < dictCreation.Count; i++) { - ExpressionStatement es = dictCreation[i] as ExpressionStatement; - if (es == null) - continue; - InvocationExpression ie = es.Expression as InvocationExpression; - if (ie == null) - continue; - - KeyValuePair pair; - if (TryGetPairFrom(ie.Arguments, out pair)) - dict.Add(pair); - } - return dict; - } - - private static bool TryGetPairFrom(AstNodeCollection expressions, out KeyValuePair pair) - { - PrimitiveExpression arg1 = expressions.ElementAtOrDefault(0) as PrimitiveExpression; - PrimitiveExpression arg2 = expressions.ElementAtOrDefault(1) as PrimitiveExpression; - if (arg1 != null && arg2 != null && arg1.Value is string && arg2.Value is int) { - pair = new KeyValuePair((string)arg1.Value, (int)arg2.Value); - return true; - } - - pair = default(KeyValuePair); - return false; - } - - #endregion - #region Automatic Properties static readonly PropertyDeclaration automaticPropertyPattern = new PropertyDeclaration { Attributes = { new Repeat(new AnyNode()) },