public override IEnumerable <string> Visit(IfElseIfStatement node)
        {
            StringBuilder first = new StringBuilder("if ( ");

            foreach (var str in node.Nodes <BooleanExpression>()[0].Accept(this).Where(s => !string.IsNullOrWhiteSpace(s)))
            {
                first.Append(str);
            }
            first.Append(" )");
            yield return(first.ToString());

            yield return("{");

            foreach (var str in node.Nodes <Declaration>()[0].Accept(this))
            {
                yield return($"    {str}");
            }
            foreach (var str in node.Nodes <Statement>()[0].Accept(this))
            {
                yield return($"    {str}");
            }
            yield return("}");

            yield return("else");

            foreach (var str in node[9].Accept(this))
            {
                yield return(str);
            }
        }
 private Statement FixSwitchingStatement(Statement statement)
 {
     if (statement is SwitchStatement)
     {
         SwitchStatement theSwitch = statement as SwitchStatement;
         if (theSwitch.Condition.Equals(theIntVariable))
         {
             theSwitch.Condition = theStringVariable.CloneExpressionOnly();
         }
         foreach (SwitchCase @case in theSwitch.Cases)
         {
             if (@case is ConditionCase)
             {
                 ConditionCase condCase  = @case as ConditionCase;
                 int           caseValue = (int)(condCase.Condition as LiteralExpression).Value;
                 condCase.Condition = new LiteralExpression(valueDictionary[caseValue], theTypeSystem, null);
             }
         }
     }
     else if (statement is IfElseIfStatement)
     {
         IfElseIfStatement irregularSwitch = statement as IfElseIfStatement;
         foreach (KeyValuePair <Expression, BlockStatement> condPair in irregularSwitch.ConditionBlocks)
         {
             FixConditionExpression(condPair.Key as BinaryExpression);
         }
     }
     return(statement);
 }
Exemplo n.º 3
0
        private bool TryGetSwitchData(IfElseIfStatement node, out CreateCompilerOptimizedSwitchByStringStatementsStep.SwitchData data)
        {
            data = new CreateCompilerOptimizedSwitchByStringStatementsStep.SwitchData();
            V_0  = node.get_ConditionBlocks().GetEnumerator();
            try
            {
                while (V_0.MoveNext())
                {
                    V_1 = V_0.get_Current();
                    if (this.TryMatchCondition(V_1.get_Key(), V_1.get_Value(), data))
                    {
                        continue;
                    }
                    V_2 = false;
                    goto Label1;
                }
                goto Label0;
            }
            finally
            {
                ((IDisposable)V_0).Dispose();
            }
Label1:
            return(V_2);

Label0:
            if (node.get_Else() != null)
            {
                data.set_DefaultCase(node.get_Else());
            }
            return(true);
        }
        private IfElseIfStatement BuildIfElseIfStatement(IfStatement theIf)
        {
            // at this point we are sure, that the statement is either IfStatement or IfElseIfStatement
            Statement elseStatement = theIf.Else.Statements[0];

            if (elseStatement.CodeNodeType == CodeNodeType.IfStatement)
            {
                IfStatement innerIf = (IfStatement)elseStatement;
                List <KeyValuePair <Expression, BlockStatement> > ifChain       = new List <KeyValuePair <Expression, BlockStatement> >();
                KeyValuePair <Expression, BlockStatement>         theFirstPair  = new KeyValuePair <Expression, BlockStatement>(theIf.Condition, theIf.Then);
                KeyValuePair <Expression, BlockStatement>         theSecondPair = new KeyValuePair <Expression, BlockStatement>(innerIf.Condition, innerIf.Then);
                ifChain.Add(theFirstPair);
                ifChain.Add(theSecondPair);
                IfElseIfStatement result = new IfElseIfStatement(ifChain, innerIf.Else);
                return(result);
            }
            else
            {
                IfElseIfStatement innerIfElseIf = (IfElseIfStatement)elseStatement;
                List <KeyValuePair <Expression, BlockStatement> > ifChain   = innerIfElseIf.ConditionBlocks;
                KeyValuePair <Expression, BlockStatement>         outerPair = new KeyValuePair <Expression, BlockStatement>(theIf.Condition, theIf.Then);
                ifChain.Insert(0, outerPair);
                theIf.Then.Parent = innerIfElseIf;
                return(innerIfElseIf);
            }
        }
Exemplo n.º 5
0
        private bool CheckSwitcingIfBody(BlockStatement body)
        {
            if (body.Statements.Count < 1)
            {
                return(false);
            }

            Statement theStatement = body.Statements[0];

            if (theStatement is SwitchStatement)
            {
                SwitchStatement theSwitch = theStatement as SwitchStatement;
                if (theSwitch.Condition.Equals(IntVariable))
                {
                    return(true);
                }
                return(false);
            }
            if (theStatement is IfElseIfStatement)
            {
                IfElseIfStatement theIrregularSwitch = theStatement as IfElseIfStatement;
                foreach (KeyValuePair <Expression, BlockStatement> condBlock in theIrregularSwitch.ConditionBlocks)
                {
                    BinaryExpression theCondition = condBlock.Key as BinaryExpression;
                    if (theCondition == null || !CheckIrregularSwitchCaseCondition(theCondition))
                    {
                        return(false);
                    }
                }
                return(true);
            }
            return(false);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Checks if after the execution of the statements in <paramref name="caseBody"/> the control flow will be transfered to the next statement.
        /// </summary>
        /// <param name="caseBody">The block to be checked.</param>
        /// <returns>Returns false if all code paths in the block exit at a point, different from the follow node (possibly even out of the method via
        /// throw ot return). Otherwise returns true.</returns>
        public static bool BlockHasFallThroughSemantics(BlockStatement caseBody)
        {
            if (caseBody == null)
            {
                return(false);
            }

            if (caseBody.Statements.Count == 0)
            {
                return(true);
            }

            var lastStatement = caseBody.Statements[caseBody.Statements.Count - 1];

            if (lastStatement.CodeNodeType == CodeNodeType.ExpressionStatement)
            {
                Expression expression = (lastStatement as ExpressionStatement).Expression;
                if (expression != null && expression.CodeNodeType == CodeNodeType.ReturnExpression || expression.CodeNodeType == CodeNodeType.ThrowExpression)
                {
                    return(false);
                }
            }
            else if (lastStatement.CodeNodeType == CodeNodeType.BreakStatement ||
                     lastStatement.CodeNodeType == CodeNodeType.ContinueStatement ||
                     lastStatement.CodeNodeType == CodeNodeType.GotoStatement)
            {
                return(false);
            }
            else if (lastStatement.CodeNodeType == CodeNodeType.IfStatement)
            {
                IfStatement theIf = lastStatement as IfStatement;
                if (theIf.Else != null)
                {
                    return(BlockHasFallThroughSemantics(theIf.Else) || BlockHasFallThroughSemantics(theIf.Then));
                }
            }
            else if (lastStatement.CodeNodeType == CodeNodeType.IfElseIfStatement)
            {
                IfElseIfStatement theIfElseIf = lastStatement as IfElseIfStatement;
                if (theIfElseIf.Else == null)
                {
                    return(true);
                }
                bool result = BlockHasFallThroughSemantics(theIfElseIf.Else);
                if (result == false)
                {
                    return(false);
                }
                foreach (KeyValuePair <Expression, BlockStatement> pair in theIfElseIf.ConditionBlocks)
                {
                    result |= BlockHasFallThroughSemantics(pair.Value);
                    if (result == false)
                    {
                        return(false);
                    }
                }
            }
            return(true);
        }
Exemplo n.º 7
0
 public override ICodeNode VisitIfElseIfStatement(IfElseIfStatement node)
 {
     if (!this.IsSwitchByString(node) || !this.TryGetSwitchData(node, out V_0))
     {
         return(this.VisitIfElseIfStatement(node));
     }
     return(this.Visit(this.ComposeSwitch(V_0)));
 }
 public override ICodeNode VisitIfElseIfStatement(IfElseIfStatement node)
 {
     stackVariable1  = node.get_ConditionBlocks();
     V_0             = node.get_ConditionBlocks().get_Item(0);
     stackVariable11 = (Expression)this.Visit(V_0.get_Key());
     V_0             = node.get_ConditionBlocks().get_Item(0);
     stackVariable1.set_Item(0, new KeyValuePair <Expression, BlockStatement>(stackVariable11, V_0.get_Value()));
     return(node);
 }
        private IfElseIfStatement HandleDirectIfStatement(IfStatement theIf)
        {
            /// At this point we are sure, that if-else-if statement can be created without reverting the condition
            /// Now we need to perform checks if we can create if-else-if statement after reversing and to chose which one to create
            ///

            if (theIf.Then.Statements.Count != 1)
            {
                /// There is only one way in which we can create the if-else-if statement

                IfElseIfStatement result = BuildIfElseIfStatement(theIf);
                return(result);
            }
            Statement thenStatement = theIf.Then.Statements[0];

            if (thenStatement.CodeNodeType != CodeNodeType.IfElseIfStatement && thenStatement.CodeNodeType != CodeNodeType.IfStatement)
            {
                /// There is only one way in which we can create the if-else-if statement

                IfElseIfStatement result = BuildIfElseIfStatement(theIf);
                return(result);
            }
            Statement elseStatement = theIf.Else.Statements[0];

            if (elseStatement.CodeNodeType == CodeNodeType.IfStatement)
            {
                /// If the else statement is only if-statement, then we invert the code anyways
                /// best case: the then statement was if-else-if, and we get one level nesting less
                /// worst case: the then statemnt was if-statement as well and we get the same level of nesting as if we haven't inverted
                InvertIfStatement(theIf);
                IfElseIfStatement result = BuildIfElseIfStatement(theIf);
                return(result);
            }
            /// from this point the else statement is known to be if-else-if statement
            ///

            if (thenStatement.CodeNodeType == CodeNodeType.IfStatement)
            {
                /// This way we will continue the if-else-if statement in the else block
                IfElseIfStatement result = BuildIfElseIfStatement(theIf);
                return(result);
            }

            IfElseIfStatement elseIfElseStatement = (IfElseIfStatement)elseStatement;
            IfElseIfStatement thenIfElseStatement = (IfElseIfStatement)thenStatement;

            if (thenIfElseStatement.ConditionBlocks.Count >= elseIfElseStatement.ConditionBlocks.Count)
            {
                InvertIfStatement(theIf);
            }

            IfElseIfStatement endResult = BuildIfElseIfStatement(theIf);

            return(endResult);
        }
Exemplo n.º 10
0
        public virtual void VisitIfElseIfStatement(IfElseIfStatement node)
        {
            for (int i = 0; i < node.ConditionBlocks.Count; i++)
            {
                KeyValuePair <Expression, BlockStatement> pair = node.ConditionBlocks[i];
                Visit(pair.Key);
                Visit(pair.Value);
            }

            Visit(node.Else);
        }
Exemplo n.º 11
0
        public override ICodeNode VisitIfElseIfStatement(IfElseIfStatement node)
        {
            SwitchData data;

            if (IsSwitchByString(node) && TryGetSwitchData(node, out data))
            {
                return(Visit(ComposeSwitch(data)));
            }

            return(base.VisitIfElseIfStatement(node));
        }
Exemplo n.º 12
0
 public virtual void VisitIfElseIfStatement(IfElseIfStatement node)
 {
     V_0 = 0;
     while (V_0 < node.get_ConditionBlocks().get_Count())
     {
         V_1 = node.get_ConditionBlocks().get_Item(V_0);
         this.Visit(V_1.get_Key());
         this.Visit(V_1.get_Value());
         V_0 = V_0 + 1;
     }
     this.Visit(node.get_Else());
     return;
 }
Exemplo n.º 13
0
        public virtual ICodeNode VisitIfElseIfStatement(IfElseIfStatement node)
        {
            for (int i = 0; i < node.ConditionBlocks.Count; i++)
            {
                Expression     transformedCondition = (Expression)Visit(node.ConditionBlocks[i].Key);
                BlockStatement transformedBlock     = (BlockStatement)Visit(node.ConditionBlocks[i].Value);
                transformedBlock.Parent = node;
                node.ConditionBlocks[i] = new KeyValuePair <Expression, BlockStatement>(transformedCondition, transformedBlock);
            }

            node.Else = (BlockStatement)Visit(node.Else);

            return(node);
        }
Exemplo n.º 14
0
        private bool IsSwitchByString(IfElseIfStatement node)
        {
            foreach (KeyValuePair <int, List <int> > pair in this.switchByStringData.SwitchBlocksToCasesMap)
            {
                foreach (int caseOffset in pair.Value)
                {
                    if (node.SearchableUnderlyingSameMethodInstructionOffsets.Contains(caseOffset))
                    {
                        return(true);
                    }
                }
            }

            return(false);
        }
 public virtual ICodeNode VisitIfElseIfStatement(IfElseIfStatement node)
 {
     V_0 = 0;
     while (V_0 < node.get_ConditionBlocks().get_Count())
     {
         V_3 = node.get_ConditionBlocks().get_Item(V_0);
         V_1 = (Expression)this.Visit(V_3.get_Key());
         V_3 = node.get_ConditionBlocks().get_Item(V_0);
         V_2 = (BlockStatement)this.Visit(V_3.get_Value());
         V_2.set_Parent(node);
         node.get_ConditionBlocks().set_Item(V_0, new KeyValuePair <Expression, BlockStatement>(V_1, V_2));
         V_0 = V_0 + 1;
     }
     node.set_Else((BlockStatement)this.Visit(node.get_Else()));
     return(node);
 }
        public override ICodeNode VisitIfStatement(IfStatement node)
        {
            ICodeNode transformedNode = base.VisitIfStatement(node);

            if (transformedNode.CodeNodeType != CodeNodeType.IfStatement)
            {
                return(transformedNode);
            }

            IfStatement theIf = (IfStatement)transformedNode;

            if (theIf.Else == null)
            {
                return(theIf);
            }

            if (theIf.Else.Statements.Count == 1)
            {
                Statement elseStatement = theIf.Else.Statements[0];
                if (elseStatement.CodeNodeType == CodeNodeType.IfStatement || elseStatement.CodeNodeType == CodeNodeType.IfElseIfStatement)
                {
                    IfElseIfStatement resultingIfElseIf = HandleDirectIfStatement(theIf);
                    return(resultingIfElseIf);
                }
            }

            if (theIf.Then.Statements.Count != 1)
            {
                return(theIf);
            }
            Statement thenStatement = theIf.Then.Statements[0];

            if (thenStatement.CodeNodeType != CodeNodeType.IfStatement && thenStatement.CodeNodeType != CodeNodeType.IfElseIfStatement)
            {
                return(theIf);
            }

            InvertIfStatement(theIf);

            IfElseIfStatement result = BuildIfElseIfStatement(theIf);

            return(result);
        }
Exemplo n.º 17
0
        private bool TryGetSwitchData(IfElseIfStatement node, out SwitchData data)
        {
            data = new SwitchData();

            foreach (KeyValuePair <Expression, BlockStatement> pair in node.ConditionBlocks)
            {
                if (!TryMatchCondition(pair.Key, pair.Value, data))
                {
                    return(false);
                }
            }

            if (node.Else != null)
            {
                data.DefaultCase = node.Else;
            }

            return(true);
        }
Exemplo n.º 18
0
        private bool IsSwitchByString(IfElseIfStatement node)
        {
            V_0 = this.switchByStringData.get_SwitchBlocksToCasesMap().GetEnumerator();
            try
            {
                while (V_0.MoveNext())
                {
                    V_1 = V_0.get_Current();
                    V_2 = V_1.get_Value().GetEnumerator();
                    try
                    {
                        while (V_2.MoveNext())
                        {
                            V_3 = V_2.get_Current();
                            if (!node.get_SearchableUnderlyingSameMethodInstructionOffsets().Contains(V_3))
                            {
                                continue;
                            }
                            V_4 = true;
                            goto Label1;
                        }
                    }
                    finally
                    {
                        ((IDisposable)V_2).Dispose();
                    }
                }
                goto Label0;
            }
            finally
            {
                ((IDisposable)V_0).Dispose();
            }
Label1:
            return(V_4);

Label0:
            return(false);
        }
 public override void VisitIfElseIfStatement(IfElseIfStatement node)
 {
     V_0 = node.get_ConditionBlocks().get_Item(0);
     this.Visit(V_0.get_Key());
     return;
 }