Exemplo n.º 1
0
        protected override BoundStatement RewriteForStatement(BoundForStatement node)
        {
            // for <var> = <lower> to <upper>
            //      <body>
            //
            // ---->
            //
            // {
            //      var <var> = <lower>
            //      let upperBound = <upper>
            //      while (<var> <= upperBound)
            //      {
            //          <body>
            //          continue:
            //          <var> = <var> + 1
            //      }
            // }


            var lowerBound = VariableDeclaration(node.Variable, node.LowerBound);
            var upperBound = ConstantDeclaration("upperBound", node.UpperBound);
            var result     = Block(lowerBound,
                                   upperBound,
                                   While(LessOrEqual(Variable(lowerBound), Variable(upperBound)),
                                         Block(node.Body,
                                               Label(node.ContinueLabel),
                                               Increment(Variable(lowerBound))),
                                         node.BreakLabel,
                                         continueLabel: GenerateLabel())
                                   );


            return(RewriteStatement(result));
        }
Exemplo n.º 2
0
        public override object VisitForStatement(BoundForStatement node, object arg)
        {
            VisitStatement(node.Initializer);
            LoopHead(node);
            bool isTrue;
            bool isFalse;

            if (node.Condition != null)
            {
                isFalse = IsConstantFalse(node.Condition);
                isTrue  = IsConstantTrue(node.Condition);
                VisitCondition(node.Condition);
            }
            else
            {
                isTrue     = true;
                isFalse    = false;
                this.state = new FlowAnalysisLocalState(this.state.Reachable, this.state.Assigned, BitArray.AllSet(nextVariableSlot));
            }
            var bodyState          = new FlowAnalysisLocalState(this.state.Reachable && !isFalse, this.state.AssignedWhenTrue);
            var breakState         = new FlowAnalysisLocalState(this.state.Reachable && !isTrue, this.state.AssignedWhenFalse);
            var oldPendingBranches = this.pendingBranches;

            this.pendingBranches = ArrayBuilder <PendingBranch> .GetInstance();

            this.state = bodyState;
            VisitStatement(node.Body);
            ResolveContinues(node.ContinueLabel);
            VisitStatement(node.Increment);
            LoopTail(node);
            ResolveBreaks(oldPendingBranches, breakState, node.BreakLabel);
            return(null);
        }
Exemplo n.º 3
0
        protected override BoundStatement RewriteForStatement(BoundForStatement statement)
        {
            /*  for(<assignment>, <condition>, <step>) <body>
             *
             *  <assignment>
             *  while (<condition>) {
             *    body
             *    step
             *  }
             */

            var whileStatements = ImmutableArray.CreateBuilder <BoundStatement>();

            whileStatements.AddRange(new BoundStatement[] {
                statement.Body,
                new BoundLabel(statement.ContinueLabel),
                new BoundExpressionStatement(statement.Step)
            });

            var whileStatement = new BoundWhileStatement(statement.Condition, new BoundBlock(whileStatements.ToImmutable()), statement.BreakLabel, statement.ContinueLabel, false);

            var finalStatements = ImmutableArray.CreateBuilder <BoundStatement>();

            finalStatements.AddRange(new BoundStatement[] {
                statement.Assignment,
                whileStatement
            });

            return(RewriteStatement(new BoundBlock(finalStatements.ToImmutable())));
        }
Exemplo n.º 4
0
        protected override BoundStatement RewriteForStatement(BoundForStatement node)
        {
            //For Statements are now all While Statements
            var variableDeclaration   = new BoundVariableDeclaration(node.Variable, node.LowerBound);
            var variableExpression    = new BoundVariableExpression(node.Variable);
            var upperBoundSymbol      = new VariableSymbol("upperBound", true, typeof(int));
            var upperBoundDeclaration = new BoundVariableDeclaration(upperBoundSymbol, node.UpperBound);
            var condition             = new BoundBinaryExpression(
                variableExpression,
                BoundBinaryOperator.Bind(SyntaxKind.LessOrEqualsToken, typeof(int), typeof(int)),
                new BoundVariableExpression(upperBoundSymbol)
                );
            var increment = new BoundExpressionStatement(
                new BoundAssignmentExpression(
                    node.Variable,
                    new BoundBinaryExpression(
                        variableExpression,
                        BoundBinaryOperator.Bind(SyntaxKind.PlusToken, typeof(int), typeof(int)),
                        new BoundLiteralExpression(1)
                        )
                    )
                );
            var whileBody      = new BoundBlockStatement(ImmutableArray.Create <BoundStatement>(node.Body, increment));
            var whileStatement = new BoundWhileStatement(condition, whileBody);
            var result         = new BoundBlockStatement(ImmutableArray.Create <BoundStatement>(
                                                             variableDeclaration,
                                                             upperBoundDeclaration,
                                                             whileStatement
                                                             ));

            return(RewriteStatement(result));
        }
Exemplo n.º 5
0
        protected override BoundStatement RewriteForStatement(BoundForStatement node)
        {
            /*
             *  {
             *      let upperBound = <UpperBound>
             *      <variable> = <lowerBound>
             *      while <variable> < <UpperBound>
             *      {
             *          <statement>
             *          <variable> = <variable> + 1
             *      }
             *  }
             */

            var upperBoundSymbol      = new VariableSymbol("upperBound", true, node.UpperBound.Type, true);
            var upperBoundDeclaration = new BoundVariableDeclaration(upperBoundSymbol, node.UpperBound);
            var upperBoundExpression  = new BoundVariableExpression(upperBoundSymbol);
            var variableDeclaration   = new BoundVariableDeclaration(node.Variable, node.LowerBound); // LowerBound shouldn't be rewriten?
            var variableExpression    = new BoundVariableExpression(node.Variable);
            var conditionOperator     = BoundBinaryOperator.Bind(SyntaxKind.LessToken, TypeSymbol.Int, TypeSymbol.Int);
            var condition             = new BoundBinaryExpression(variableExpression, conditionOperator, upperBoundExpression); // UpperBound shouldn't be rewriten?

            var incrementOperator   = BoundBinaryOperator.Bind(SyntaxKind.PlusToken, node.Variable.Type, TypeSymbol.Int);
            var increment           = new BoundBinaryExpression(variableExpression, incrementOperator, new BoundLiteralExpression(1));
            var incrementAssignment = new BoundAssignmentExpression(node.Variable, increment);
            var incrementStatement  = new BoundExpressionStatement(incrementAssignment);

            var whileBlockStatement = new BoundBlockStatement(ImmutableArray.Create <BoundStatement>(node.Statement, incrementStatement));
            var whileStatement      = new BoundWhileStatement(condition, whileBlockStatement);

            var result = new BoundBlockStatement(ImmutableArray.Create <BoundStatement>(upperBoundDeclaration, variableDeclaration, whileStatement));

            return(RewriteStatement(result));
        }
Exemplo n.º 6
0
        private void EmitForStatement(BoundForStatement statement)
        {
            string beforeCheckLabel  = this.GenerateJumpLabel();
            string positiveLoopLabel = this.GenerateJumpLabel();
            string negativeLoopLabel = this.GenerateJumpLabel();
            string afterCheckLabel   = this.GenerateJumpLabel();
            string endOfBlockLabel   = this.GenerateJumpLabel();

            this.EmitExpression(statement.FromExpression);
            this.instructions.Add(new StoreVariableInstruction(statement.Identifier, statement.Syntax.IdentifierToken.Range));
            this.instructions.Add(new TransientLabelInstruction(beforeCheckLabel, statement.Syntax.IdentifierToken.Range));

            if (!statement.StepExpressionOpt.IsDefault())
            {
                this.EmitExpression(statement.StepExpressionOpt);

                TextRange stepRange = statement.Syntax.StepClauseOpt.StepToken.Range;
                this.instructions.Add(new PushValueInstruction(new NumberValue(0), stepRange));
                this.instructions.Add(new LessThanInstruction(stepRange));
                this.instructions.Add(new TransientConditionalGoToInstruction(negativeLoopLabel, positiveLoopLabel, stepRange));
            }

            TextRange toExpressionRange = statement.Syntax.ToExpression.Range;

            this.instructions.Add(new TransientLabelInstruction(positiveLoopLabel, toExpressionRange));
            this.EmitExpression(statement.ToExpression);
            this.instructions.Add(new LoadVariableInstruction(statement.Identifier, toExpressionRange));
            this.instructions.Add(new LessThanInstruction(toExpressionRange));
            this.instructions.Add(new TransientConditionalGoToInstruction(endOfBlockLabel, afterCheckLabel, toExpressionRange));

            this.instructions.Add(new TransientLabelInstruction(negativeLoopLabel, toExpressionRange));
            this.instructions.Add(new LoadVariableInstruction(statement.Identifier, toExpressionRange));
            this.EmitExpression(statement.ToExpression);
            this.instructions.Add(new LessThanInstruction(toExpressionRange));
            this.instructions.Add(new TransientConditionalGoToInstruction(endOfBlockLabel, null, toExpressionRange));

            this.instructions.Add(new TransientLabelInstruction(afterCheckLabel, toExpressionRange));

            this.EmitBlockStatement(statement.Body);

            TextRange endForRange = statement.Syntax.EndForToken.Range;

            this.instructions.Add(new LoadVariableInstruction(statement.Identifier, endForRange));

            if (statement.StepExpressionOpt.IsDefault())
            {
                this.instructions.Add(new PushValueInstruction(new NumberValue(1), endForRange));
            }
            else
            {
                this.EmitExpression(statement.StepExpressionOpt);
            }

            this.instructions.Add(new AddInstruction(endForRange));
            this.instructions.Add(new StoreVariableInstruction(statement.Identifier, endForRange));
            this.instructions.Add(new TransientUnconditionalGoToInstruction(beforeCheckLabel, endForRange));

            this.instructions.Add(new TransientLabelInstruction(endOfBlockLabel, endForRange));
        }
Exemplo n.º 7
0
 public override BoundNode VisitForStatement(BoundForStatement node)
 {
     if (IsInside)
     {
         _labelsInside.Add(node.BreakLabel);
         _labelsInside.Add(node.ContinueLabel);
     }
     return(base.VisitForStatement(node));
 }
Exemplo n.º 8
0
        protected override BoundStatement RewriteForStatement(BoundForStatement node)
        {
            /*
             *  for <var> = <lower> to <upper>
             *      <body>
             *
             *  ----->
             *  {
             *      var <var> = <lower>
             *      let upperBound = <upper>
             *      while (<var> <= <upperBound>)
             *      {
             *          <body>
             *          <var> = <var> + 1
             *      }
             *  }
             */
            // var <var> = <lower>
            var variableDeclaration = new BoundVariableDeclaration(node.Variable, node.LowerBound);

            // let upperBound = <upper>
            var upperBoundSymbol      = new VariableSymbol("upperBound", true, TypeSymbol.Int);
            var upperBoundDeclaration = new BoundVariableDeclaration(upperBoundSymbol, node.UpperBound);

            // <var>
            var variableExpression = new BoundVariableExpression(node.Variable);

            //<var> <= <upperBound>
            var condition = new BoundBinaryExpression(
                variableExpression,
                BoundBinaryOperator.Bind(SyntaxKind.LessOrEqualsToken, TypeSymbol.Int, TypeSymbol.Int),
                new BoundVariableExpression(upperBoundSymbol)
                );

            // <var> = <var> + 1
            var increment = new BoundExpressionStatement(
                new BoundAssignmentExpression(
                    node.Variable,
                    new BoundBinaryExpression(
                        variableExpression,
                        BoundBinaryOperator.Bind(SyntaxKind.PlusToken, TypeSymbol.Int, TypeSymbol.Int),
                        new BoundLiteralExpression(1)
                        )
                    )
                );

            var whileBlock     = new BoundBlockStatement(ImmutableArray.Create(node.Body, increment));
            var whileStatement = new BoundWhileStatement(condition, whileBlock);

            var result = new BoundBlockStatement(ImmutableArray.Create <BoundStatement>(
                                                     variableDeclaration,
                                                     upperBoundDeclaration,
                                                     whileStatement));

            return(RewriteStatement(result));
        }
Exemplo n.º 9
0
        public override BoundNode VisitForStatement(BoundForStatement node)
        {
            var             newOuterLocals = RewriteLocals(node.OuterLocals);
            BoundStatement  initializer    = (BoundStatement)this.Visit(node.Initializer);
            BoundExpression condition      = (BoundExpression)this.Visit(node.Condition);
            BoundStatement  increment      = (BoundStatement)this.Visit(node.Increment);
            BoundStatement  body           = (BoundStatement)this.Visit(node.Body);

            return(node.Update(newOuterLocals, initializer, condition, increment, body, node.BreakLabel, node.ContinueLabel));
        }
Exemplo n.º 10
0
        protected override BoundStatement RewriteForStatement(BoundForStatement node)
        {
            // for <var> = <lower> to <upper>
            //     <body>
            //
            // ---->
            // {
            //      var <var> = <lower>
            //      let upperBound = <upper>
            //      while (<var> <= upperBound)
            //      {
            //          <body>
            //          continue:
            //          <var> = <var> + 1
            //      }
            // }

            var variableDeclaration   = new BoundVariableDeclaration(node.Variable, node.LowerBound);
            var variableExpression    = new BoundVariableExpression(node.Variable);
            var upperBoundSymbol      = new LocalVariableSymbol("upperBound", true, TypeSymbol.Int);
            var upperBoundDeclaration = new BoundVariableDeclaration(upperBoundSymbol, node.UpperBound);
            var condition             = new BoundBinaryExpression(
                variableExpression,
                BoundBinaryOperator.Bind(SyntaxKind.LessOrEqualsToken, TypeSymbol.Int, TypeSymbol.Int),
                new BoundVariableExpression(upperBoundSymbol)
                );
            var continueLabelStatement = new BoundLabelStatement(node.ContinueLabel);
            var increment = new BoundExpressionStatement(
                new BoundAssignmentExpression(
                    node.Variable,
                    new BoundBinaryExpression(
                        variableExpression,
                        BoundBinaryOperator.Bind(SyntaxKind.PlusToken, TypeSymbol.Int, TypeSymbol.Int),
                        new BoundLiteralExpression(1)
                        )
                    )
                );
            var whileBody = new BoundBlockStatement(
                ImmutableArray.Create <BoundStatement>(
                    node.Body,
                    continueLabelStatement,
                    increment
                    )
                );
            var whileStatement = new BoundWhileStatement(condition, whileBody, node.BreakLabel, GenerateLabel());
            var result         = new BoundBlockStatement(
                ImmutableArray.Create <BoundStatement>(
                    variableDeclaration,
                    upperBoundDeclaration,
                    whileStatement
                    )
                );

            return(RewriteStatement(result));
        }
Exemplo n.º 11
0
        private void EvaluateForStatement(BoundForStatement node)
        {
            var lowerBound = (int)EvaluateExpression(node.LowerBound);
            var upperBound = (int)EvaluateExpression(node.UpperBound);

            for (var i = lowerBound; i <= upperBound; i++)
            {
                _variables[node.Variable] = i;
                EvaluateStatement(node.Body);
            }
        }
Exemplo n.º 12
0
        protected override BoundStatement RewriteForStatement(BoundForStatement node)
        {
            var initializer            = new BoundExpressionStatement(node.Initializer);
            var loop                   = new BoundExpressionStatement(node.Loop);
            var continueLabelStatement = new BoundLabelStatement(node.ContinueLabel);

            var whileBlock     = new BoundBlockStatement(ImmutableArray.Create <BoundStatement>(node.Body, continueLabelStatement, loop));
            var whileStatement = new BoundWhileStatement(node.Condition, whileBlock, node.BreakLabel, GenerateLabel());
            var result         = new BoundBlockStatement(ImmutableArray.Create <BoundStatement>(initializer, whileStatement));

            return(RewriteStatement(result));
        }
Exemplo n.º 13
0
 public override BoundNode VisitForStatement(BoundForStatement node)
 {
     AddAll(node.OuterLocals);
     this.Visit(node.Initializer);
     AddAll(node.InnerLocals);
     this.Visit(node.Condition);
     this.Visit(node.Increment);
     this.Visit(node.Body);
     RemoveAll(node.InnerLocals);
     RemoveAll(node.OuterLocals);
     return(null);
 }
Exemplo n.º 14
0
        protected virtual BoundStatement RewriteForStatement(BoundForStatement statement)
        {
            var assignment = RewriteStatement(statement.Assignment);
            var cond       = RewriteExpression(statement.Condition);
            var step       = RewriteExpression(statement.Step);

            var body = RewriteStatement(statement.Body);

            if (assignment == statement.Assignment && cond == statement.Condition && step == statement.Step && body == statement.Body)
            {
                return(statement);
            }

            return(new BoundForStatement(assignment, cond, step, body, statement.BreakLabel, statement.ContinueLabel));
        }
Exemplo n.º 15
0
        protected override BoundStatement RewriteForStatement(BoundForStatement node)
        {
            // for <var> = <lower> to <upper>
            //      <body>
            //
            // ---->
            //
            // {
            //      var <var> = <lower>
            //      while (<var> <= <upper>)
            //      {
            //          <body>
            //          <var> = <var> + 1
            //      }
            // }

            var variableDeclaration   = new BoundVeriableDeclaration(node.Variable, node.LowerBound);
            var variableExpression    = new BoundVariableExpression(node.Variable);
            var upperBoundSybmle      = new VariableSymble("upperBound", true, typeof(int));
            var upperBoundDeclaration = new BoundVeriableDeclaration(upperBoundSybmle, node.UpperBound);
            var condition             = new BoundBinaryExpression(
                variableExpression,
                BoundBinaryOperator.Bind(SyntaxKind.LessOrEqualToken, typeof(int), typeof(int)),
                new BoundVariableExpression(upperBoundSybmle)
                );
            var increment = new BoundExpressionStatemnet(
                new BoundAssignmentExpression(
                    node.Variable,
                    new BoundBinaryExpression(
                        variableExpression,
                        BoundBinaryOperator.Bind(SyntaxKind.PlusToken, typeof(int), typeof(int)),
                        new BoundLiteralExpression(1)
                        )
                    )
                );
            var whileBody      = new BoundBlockStatemnet(ImmutableArray.Create <BoundStatement>(node.Body, increment));
            var whileStatement = new BoundWhileStatement(condition, whileBody);
            var result         = new BoundBlockStatemnet(ImmutableArray.Create <BoundStatement>(
                                                             variableDeclaration, upperBoundDeclaration, whileStatement));

            return(RewriteStatement(result));
        }
Exemplo n.º 16
0
        protected override BoundStatement RewriteForStatement(BoundForStatement node)
        {
            var condition = node.Condition;

            var continueLabelStatement = new BoundLabelStatement(node.ContinueLabel);
            var increment = new BoundExpressionStatement(
                node.Action
                );
            var whileBody = new BoundBlockStatement(ImmutableArray.Create <BoundStatement>(
                                                        node.Body,
                                                        continueLabelStatement,
                                                        increment)
                                                    );
            var whileStatement = new BoundWhileStatement(condition, whileBody, node.BreakLabel, GenerateLabel());
            var result         = new BoundBlockStatement(ImmutableArray.Create <BoundStatement>(
                                                             node.Variable,
                                                             whileStatement
                                                             ));

            return(RewriteStatement(result));
        }
Exemplo n.º 17
0
        protected virtual void VisitForStatement(BoundForStatement node)
        {
            if (node.Declarations != null)
            {
                VisitMultipleVariableDeclarations(node.Declarations);
            }

            if (node.Initializer != null)
            {
                VisitExpression(node.Initializer);
            }

            VisitExpression(node.Condition);

            if (node.Incrementor != null)
            {
                VisitExpression(node.Incrementor);
            }

            VisitStatement(node.Body);
        }
Exemplo n.º 18
0
        public override BoundNode VisitForStatement(BoundForStatement node)
        {
            Debug.Assert(node != null);

            var rewrittenInitializer = (BoundStatement)Visit(node.Initializer);
            var rewrittenCondition   = (BoundExpression)Visit(node.Condition);
            var rewrittenIncrement   = (BoundStatement)Visit(node.Increment);
            var rewrittenBody        = (BoundStatement)Visit(node.Body);

            SyntaxNodeOrToken conditionSyntax = ((BoundNode)rewrittenCondition ?? node).Syntax;

            return(RewriteForStatement(
                       node.Syntax,
                       node.Locals,
                       rewrittenInitializer,
                       rewrittenCondition,
                       conditionSyntax,
                       rewrittenIncrement,
                       rewrittenBody,
                       node.BreakLabel,
                       node.ContinueLabel, node.HasErrors));
        }
Exemplo n.º 19
0
        protected override BoundStatement RewriteForStatement(BoundForStatement node)
        {
            // for <var> = <lower> to <upper>
            //  <body>
            // is rewritten to
            //  {
            //    var <Var> = <lower>
            //  while ()
            //
            var variableDeclaration = new BoundVariableDeclaration(node.Variable, node.LowerBound);
            var variableExpression  = new BoundVariableExpression(node.Variable);

            var condition = new BoundBinaryExpression(
                variableExpression,
                BoundBinaryOperator.Bind(SyntaxKind.LessOrEqualsToken, typeof(int), typeof(int)),
                node.UpperBound);

            var increment = new BoundExpressionStatement(
                new BoundAssignmentExpression(
                    node.Variable,
                    new BoundBinaryExpression(
                        variableExpression,
                        BoundBinaryOperator.Bind(
                            SyntaxKind.PlusToken,
                            typeof(int),
                            typeof(int)),
                        new BoundLiteralExpression(1)
                        )
                    )
                );

            var whileBlock = new BoundBlockStatement(ImmutableArray.Create <BoundStatement>(node.Body, increment));

            var whileStatement = new BoundWhileStatement(condition, whileBlock);

            var result = new BoundBlockStatement(ImmutableArray.Create <BoundStatement>(variableDeclaration, whileStatement));

            return(RewriteStatement(result));
        }
Exemplo n.º 20
0
        protected override BoundStatement RewriteForStatement(BoundForStatement node)
        {
            BoundVariableDeclaration variableDeclaration      = new BoundVariableDeclaration(node.Variable, node.LowerBound);
            BoundVariableExpression  variableExpression       = new BoundVariableExpression(node.Variable);
            VariableSymbol           upperVariableSymbol      = new LocalVariableSymbol("upperBound", true, TypeSymbol.Int);
            BoundVariableDeclaration upperVariableDeclaration = new BoundVariableDeclaration(upperVariableSymbol, node.UpperBound);

            BoundBinaryExpression condition = new BoundBinaryExpression(
                variableExpression,
                BoundBinaryOperator.Bind(SyntaxKind.LessOrEqualsToken, TypeSymbol.Int, TypeSymbol.Int),
                new BoundVariableExpression(upperVariableSymbol)
                );
            BoundLabelStatement      continueLabelStatement = new BoundLabelStatement(node.ContinueLabel);
            BoundExpressionStatement increment = new BoundExpressionStatement(
                new BoundAssignmentExpression(
                    node.Variable,
                    new BoundBinaryExpression(
                        variableExpression,
                        BoundBinaryOperator.Bind(SyntaxKind.PlusToken, TypeSymbol.Int, TypeSymbol.Int),
                        new BoundLiteralExpression(1)
                        )
                    )
                );

            BoundBlockStatement whileBody = new BoundBlockStatement(ImmutableArray.Create <BoundStatement>(
                                                                        node.Body,
                                                                        continueLabelStatement,
                                                                        increment
                                                                        ));
            BoundWhileStatement whileStatement = new BoundWhileStatement(condition, whileBody, node.BreakLabel, GenerateLabel());

            BoundBlockStatement result = new BoundBlockStatement(ImmutableArray.Create <BoundStatement>(
                                                                     variableDeclaration,
                                                                     upperVariableDeclaration,
                                                                     whileStatement
                                                                     ));

            return(RewriteStatement(result));
        }
Exemplo n.º 21
0
        public override BoundNode VisitForStatement(BoundForStatement node)
        {
            Debug.Assert(node != null);

            var rewrittenInitializer = (BoundStatement)Visit(node.Initializer);
            var rewrittenCondition = (BoundExpression)Visit(node.Condition);
            var rewrittenIncrement = (BoundStatement)Visit(node.Increment);
            var rewrittenBody = (BoundStatement)Visit(node.Body);

            SyntaxNodeOrToken conditionSyntax = ((BoundNode)rewrittenCondition ?? node).Syntax;

            return RewriteForStatement(
                node.Syntax,
                node.Locals,
                rewrittenInitializer,
                rewrittenCondition,
                conditionSyntax,
                rewrittenIncrement,
                rewrittenBody,
                node.BreakLabel,
                node.ContinueLabel, node.HasErrors);
        }
Exemplo n.º 22
0
 public override BoundNode VisitForStatement(BoundForStatement node)
 {
     var newOuterLocals = RewriteLocals(node.OuterLocals);
     BoundStatement initializer = (BoundStatement)this.Visit(node.Initializer);
     var newInnerLocals = RewriteLocals(node.InnerLocals);
     BoundExpression condition = (BoundExpression)this.Visit(node.Condition);
     BoundStatement increment = (BoundStatement)this.Visit(node.Increment);
     BoundStatement body = (BoundStatement)this.Visit(node.Body);
     return node.Update(newOuterLocals, initializer, newInnerLocals, condition, increment, body, node.BreakLabel, node.ContinueLabel);
 }
Exemplo n.º 23
0
        protected override BoundStatement RewriteForStatement(BoundForStatement node)
        {
            /*
             *
             * for <decl>, <condition>, <increment> {
             *  <body>
             * }
             *
             *
             * ----->
             *
             * {
             *  <decl>
             *  while <condition>{
             *      <body>
             *      continue:
             *      <increment>
             *  }
             *  break:
             * }
             *
             *
             * ----->
             *
             *
             * <decl>
             * goto check
             * body:
             * <body>
             * continue:
             * <increment>
             * check:
             * goto body if <condtiton>
             * break:
             */


            var decl      = node.VariableDeclaration;
            var increment = new BoundExpressionStatement(node.Increment, node.IsValid, true);
            var body      = node.Body;
            var condition = node.Condition;

            var bodyLabel     = CreateLabel();
            var continueLabel = node.ContinueLabel;
            var checkLabel    = CreateLabel();
            var breakLabel    = node.BreakLabel;

            var bodyLabelStmt     = new BoundLabelStatement(bodyLabel, node.IsValid);
            var continueLabelStmt = new BoundLabelStatement(continueLabel, node.IsValid);
            var checkLabelStmt    = new BoundLabelStatement(checkLabel, node.IsValid);
            var breakLabelStmt    = new BoundLabelStatement(breakLabel, node.IsValid);


            var gotoCheck    = new BoundGotoStatement(checkLabel, node.IsValid);
            var gotoTrueBody = new BoundConditionalGotoStatement(bodyLabel, node.Condition, jumpIfFalse: false, node.IsValid);

            var res = new BoundBlockStatement(ImmutableArray.Create <BoundStatement>(
                                                  decl,
                                                  gotoCheck,
                                                  bodyLabelStmt,
                                                  body,
                                                  continueLabelStmt,
                                                  increment,
                                                  checkLabelStmt,
                                                  gotoTrueBody,
                                                  breakLabelStmt
                                                  ), node.IsValid);

            return(RewriteStatement(res));
        }
Exemplo n.º 24
0
        protected virtual void VisitForStatement(BoundForStatement node)
        {
            if (node.Declarations != null)
                VisitMultipleVariableDeclarations(node.Declarations);

            if (node.Initializer != null)
                VisitExpression(node.Initializer);

            VisitExpression(node.Condition);

            if (node.Incrementor != null)
                VisitExpression(node.Incrementor);

            VisitStatement(node.Body);
        }
 public override BoundNode VisitForStatement(BoundForStatement node)
 {
     throw ExceptionUtilities.Unreachable; // for statements have been lowered away by now
 }
        private BoundStatement RewriteForStatement(
            BoundForStatement node,
            BoundStatement rewrittenInitializer,
            BoundExpression rewrittenCondition,
            BoundStatement rewrittenIncrement,
            BoundStatement rewrittenBody)
        {
            if (node.InnerLocals.IsEmpty)
            {
                return(RewriteForStatementWithoutInnerLocals(
                           node,
                           node.OuterLocals,
                           rewrittenInitializer,
                           rewrittenCondition,
                           rewrittenIncrement,
                           rewrittenBody,
                           node.BreakLabel,
                           node.ContinueLabel, node.HasErrors));
            }

            // We need to enter inner_scope-block from the top, that is where an instance of a display class will be created
            // if any local is captured within a lambda.

            // for (initializer; condition; increment)
            //   body;
            //
            // becomes the following (with block added for locals)
            //
            // {
            //   initializer;
            // start:
            //   {
            //     GotoIfFalse condition break;
            //     body;
            // continue:
            //     increment;
            //     goto start;
            //   }
            // break:
            // }

            Debug.Assert(rewrittenBody != null);

            SyntaxNode syntax           = node.Syntax;
            var        statementBuilder = ArrayBuilder <BoundStatement> .GetInstance();

            //  initializer;
            if (rewrittenInitializer != null)
            {
                statementBuilder.Add(rewrittenInitializer);
            }

            var startLabel = new GeneratedLabelSymbol("start");

            // start:
            BoundStatement startLabelStatement = new BoundLabelStatement(syntax, startLabel);

            if (Instrument)
            {
                startLabelStatement = new BoundSequencePoint(null, startLabelStatement);
            }

            statementBuilder.Add(startLabelStatement);

            var blockBuilder = ArrayBuilder <BoundStatement> .GetInstance();

            // GotoIfFalse condition break;
            if (rewrittenCondition != null)
            {
                BoundStatement ifNotConditionGotoBreak = new BoundConditionalGoto(rewrittenCondition.Syntax, rewrittenCondition, false, node.BreakLabel);
                blockBuilder.Add(ifNotConditionGotoBreak);
            }

            // body;
            blockBuilder.Add(rewrittenBody);

            // continue:
            //   increment;
            blockBuilder.Add(new BoundLabelStatement(syntax, node.ContinueLabel));
            if (rewrittenIncrement != null)
            {
                blockBuilder.Add(rewrittenIncrement);
            }

            // goto start;
            blockBuilder.Add(new BoundGotoStatement(syntax, startLabel));

            statementBuilder.Add(new BoundBlock(syntax, node.InnerLocals, blockBuilder.ToImmutableAndFree()));

            // break:
            statementBuilder.Add(new BoundLabelStatement(syntax, node.BreakLabel));

            var statements = statementBuilder.ToImmutableAndFree();

            return(new BoundBlock(syntax, node.OuterLocals, statements, node.HasErrors));
        }