示例#1
0
            public ContinueForRewriter(VBS.ForBlockSyntax vbFor, string gotoLabel)
            {
                if (vbFor == null)
                {
                    throw new ArgumentNullException("vbFor");
                }

                this._vbFor = vbFor;
                this.Label  = gotoLabel;
            }
            public override SyntaxList <StatementSyntax> VisitForBlock(VBSyntax.ForBlockSyntax node)
            {
                var stmt = node.ForStatement;
                ExpressionSyntax          startValue  = (ExpressionSyntax)stmt.FromValue.Accept(nodesVisitor);
                VariableDeclarationSyntax declaration = null;
                ExpressionSyntax          id;

                if (stmt.ControlVariable is VBSyntax.VariableDeclaratorSyntax)
                {
                    var v = (VBSyntax.VariableDeclaratorSyntax)stmt.ControlVariable;
                    declaration = SplitVariableDeclarations(v, nodesVisitor, semanticModel).Values.Single();
                    declaration = declaration.WithVariables(SyntaxFactory.SingletonSeparatedList(declaration.Variables[0].WithInitializer(SyntaxFactory.EqualsValueClause(startValue))));
                    id          = SyntaxFactory.IdentifierName(declaration.Variables[0].Identifier);
                }
                else
                {
                    var v = (ExpressionSyntax)stmt.ControlVariable.Accept(nodesVisitor);
                    startValue = SyntaxFactory.AssignmentExpression(SyntaxKind.SimpleAssignmentExpression, v, startValue);
                    id         = v;
                }

                var step = (ExpressionSyntax)stmt.StepClause?.StepValue.Accept(nodesVisitor);
                PrefixUnaryExpressionSyntax value = step.SkipParens() as PrefixUnaryExpressionSyntax;
                ExpressionSyntax            condition;

                if (value == null)
                {
                    condition = SyntaxFactory.BinaryExpression(SyntaxKind.LessThanOrEqualExpression, id, (ExpressionSyntax)stmt.ToValue.Accept(nodesVisitor));
                }
                else
                {
                    condition = SyntaxFactory.BinaryExpression(SyntaxKind.GreaterThanOrEqualExpression, id, (ExpressionSyntax)stmt.ToValue.Accept(nodesVisitor));
                }
                if (step == null)
                {
                    step = SyntaxFactory.PostfixUnaryExpression(SyntaxKind.PostIncrementExpression, id);
                }
                else
                {
                    step = SyntaxFactory.AssignmentExpression(SyntaxKind.AddAssignmentExpression, id, step);
                }
                var block = SyntaxFactory.Block(node.Statements.SelectMany(s => s.Accept(this)));

                return(SingleStatement(SyntaxFactory.ForStatement(
                                           declaration,
                                           declaration != null ? SyntaxFactory.SeparatedList <ExpressionSyntax>() : SyntaxFactory.SingletonSeparatedList(startValue),
                                           condition,
                                           SyntaxFactory.SingletonSeparatedList(step),
                                           block)));
            }
            public override SyntaxList <StatementSyntax> VisitForBlock(VBSyntax.ForBlockSyntax node)
            {
                var stmt = node.ForStatement;
                ExpressionSyntax          startValue  = (ExpressionSyntax)stmt.FromValue.Accept(_nodesVisitor);
                VariableDeclarationSyntax declaration = null;
                ExpressionSyntax          id;

                if (stmt.ControlVariable is VBSyntax.VariableDeclaratorSyntax)
                {
                    var v = (VBSyntax.VariableDeclaratorSyntax)stmt.ControlVariable;
                    declaration = CommonConversions.SplitVariableDeclarations(v).Values.Single();
                    declaration = declaration.WithVariables(SyntaxFactory.SingletonSeparatedList(declaration.Variables[0].WithInitializer(SyntaxFactory.EqualsValueClause(startValue))));
                    id          = SyntaxFactory.IdentifierName(declaration.Variables[0].Identifier);
                }
                else
                {
                    id = (ExpressionSyntax)stmt.ControlVariable.Accept(_nodesVisitor);
                    var symbol = _semanticModel.GetSymbolInfo(stmt.ControlVariable).Symbol;
                    if (symbol != null && !_semanticModel.LookupSymbols(node.FullSpan.Start, name: symbol.Name).Any())
                    {
                        declaration = CommonConversions.CreateVariableDeclarationAndAssignment(symbol.Name, startValue);
                    }
                    else
                    {
                        startValue = SyntaxFactory.AssignmentExpression(SyntaxKind.SimpleAssignmentExpression, id, startValue);
                    }
                }

                var step = (ExpressionSyntax)stmt.StepClause?.StepValue.Accept(_nodesVisitor);
                PrefixUnaryExpressionSyntax value = step.SkipParens() as PrefixUnaryExpressionSyntax;
                ExpressionSyntax            condition;

                // In Visual Basic, the To expression is only evaluated once, but in C# will be evaluated every loop.
                // If it could evaluate differently or has side effects, it must be extracted as a variable
                var preLoopStatements = new List <SyntaxNode>();
                var csToValue         = (ExpressionSyntax)stmt.ToValue.Accept(_nodesVisitor);

                if (!_semanticModel.GetConstantValue(stmt.ToValue).HasValue)
                {
                    var loopToVariableName = GetUniqueVariableNameInScope(node, "loopTo");
                    var loopEndDeclaration = SyntaxFactory.LocalDeclarationStatement(CommonConversions.CreateVariableDeclarationAndAssignment(loopToVariableName, csToValue));
                    // Does not do anything about porting newline trivia upwards to maintain spacing above the loop
                    preLoopStatements.Add(loopEndDeclaration);
                    csToValue = SyntaxFactory.IdentifierName(loopToVariableName);
                }
                ;

                if (value == null)
                {
                    condition = SyntaxFactory.BinaryExpression(SyntaxKind.LessThanOrEqualExpression, id, csToValue);
                }
                else
                {
                    condition = SyntaxFactory.BinaryExpression(SyntaxKind.GreaterThanOrEqualExpression, id, csToValue);
                }
                if (step == null)
                {
                    step = SyntaxFactory.PostfixUnaryExpression(SyntaxKind.PostIncrementExpression, id);
                }
                else
                {
                    step = SyntaxFactory.AssignmentExpression(SyntaxKind.AddAssignmentExpression, id, step);
                }
                var block = SyntaxFactory.Block(node.Statements.SelectMany(s => s.Accept(CommentConvertingVisitor)));
                var forStatementSyntax = SyntaxFactory.ForStatement(
                    declaration,
                    declaration != null
                        ? SyntaxFactory.SeparatedList <ExpressionSyntax>()
                        : SyntaxFactory.SingletonSeparatedList(startValue),
                    condition,
                    SyntaxFactory.SingletonSeparatedList(step),
                    block.UnpackNonNestedBlock());

                return(SyntaxFactory.List(preLoopStatements.Concat(new[] { forStatementSyntax })));
            }
示例#4
0
            public override SyntaxList <StatementSyntax> VisitForBlock(VBSyntax.ForBlockSyntax node)
            {
                var stmt = node.ForStatement;
                ExpressionSyntax          startValue  = (ExpressionSyntax)stmt.FromValue.Accept(_nodesVisitor);
                VariableDeclarationSyntax declaration = null;
                ExpressionSyntax          id;

                if (stmt.ControlVariable is VBSyntax.VariableDeclaratorSyntax)
                {
                    var v = (VBSyntax.VariableDeclaratorSyntax)stmt.ControlVariable;
                    declaration = VisualBasicConverter.SplitVariableDeclarations(v, _nodesVisitor, _semanticModel).Values.Single();
                    declaration = declaration.WithVariables(SyntaxFactory.SingletonSeparatedList(declaration.Variables[0].WithInitializer(SyntaxFactory.EqualsValueClause(startValue))));
                    id          = SyntaxFactory.IdentifierName(declaration.Variables[0].Identifier);
                }
                else
                {
                    id = (ExpressionSyntax)stmt.ControlVariable.Accept(_nodesVisitor);
                    var symbol = _semanticModel.GetSymbolInfo(stmt.ControlVariable).Symbol;
                    if (!_semanticModel.LookupSymbols(node.FullSpan.Start, name: symbol.Name).Any())
                    {
                        var variableDeclaratorSyntax = SyntaxFactory.VariableDeclarator(SyntaxFactory.Identifier(symbol.Name), null,
                                                                                        SyntaxFactory.EqualsValueClause(startValue));
                        declaration = SyntaxFactory.VariableDeclaration(
                            SyntaxFactory.IdentifierName("var"),
                            SyntaxFactory.SingletonSeparatedList(variableDeclaratorSyntax));
                    }
                    else
                    {
                        startValue = SyntaxFactory.AssignmentExpression(SyntaxKind.SimpleAssignmentExpression, id, startValue);
                    }
                }

                var step = (ExpressionSyntax)stmt.StepClause?.StepValue.Accept(_nodesVisitor);
                PrefixUnaryExpressionSyntax value = step.SkipParens() as PrefixUnaryExpressionSyntax;
                ExpressionSyntax            condition;

                if (value == null)
                {
                    condition = SyntaxFactory.BinaryExpression(SyntaxKind.LessThanOrEqualExpression, id, (ExpressionSyntax)stmt.ToValue.Accept(_nodesVisitor));
                }
                else
                {
                    condition = SyntaxFactory.BinaryExpression(SyntaxKind.GreaterThanOrEqualExpression, id, (ExpressionSyntax)stmt.ToValue.Accept(_nodesVisitor));
                }
                if (step == null)
                {
                    step = SyntaxFactory.PostfixUnaryExpression(SyntaxKind.PostIncrementExpression, id);
                }
                else
                {
                    step = SyntaxFactory.AssignmentExpression(SyntaxKind.AddAssignmentExpression, id, step);
                }
                var block = SyntaxFactory.Block(node.Statements.SelectMany(s => s.Accept(CommentConvertingVisitor)));

                return(SingleStatement(SyntaxFactory.ForStatement(
                                           declaration,
                                           declaration != null ? SyntaxFactory.SeparatedList <ExpressionSyntax>() : SyntaxFactory.SingletonSeparatedList(startValue),
                                           condition,
                                           SyntaxFactory.SingletonSeparatedList(step),
                                           block.UnpackNonNestedBlock())));
            }
 public override SyntaxList <StatementSyntax> VisitForBlock(VBSyntax.ForBlockSyntax node) => AddLocalVariables(node);
示例#6
0
 public override Task <SyntaxList <StatementSyntax> > VisitForBlock(VBSyntax.ForBlockSyntax node) => AddLocalVariablesAsync(node, VBasic.SyntaxKind.ForKeyword, true);
示例#7
0
 public override Task <SyntaxList <StatementSyntax> > VisitForBlock(VBSyntax.ForBlockSyntax node) => AddLocalVariablesAsync(node);