private ForStatementSyntax CreateForZeroToValueLoop(SimpleNameSyntax loopVariableIdentifier, StatementSyntax loopStatement, ExpressionSyntax inclusiveLoopUpperBound) { var loopVariableAssignment = CommonConversions.CreateVariableDeclarationAndAssignment(loopVariableIdentifier.Identifier.Text, CommonConversions.Literal(0)); var lessThanSourceBounds = SyntaxFactory.BinaryExpression(SyntaxKind.LessThanOrEqualExpression, loopVariableIdentifier, inclusiveLoopUpperBound); var incrementors = SyntaxFactory.SingletonSeparatedList <ExpressionSyntax>( SyntaxFactory.PrefixUnaryExpression(SyntaxKind.PreIncrementExpression, loopVariableIdentifier)); var forStatementSyntax = SyntaxFactory.ForStatement(loopVariableAssignment, SyntaxFactory.SeparatedList <ExpressionSyntax>(), lessThanSourceBounds, incrementors, loopStatement); return(forStatementSyntax); }
/// <summary>Creates a new ForStatementSyntax instance.</summary> public static ForStatementSyntax ForStatement( VariableDeclarationSyntax declaration, SeparatedSyntaxList <ExpressionSyntax> initializers, ExpressionSyntax condition, SeparatedSyntaxList <ExpressionSyntax> incrementors, StatementSyntax statement) { return(SyntaxFactory.ForStatement( SyntaxFactory.Token(SyntaxKind.ForKeyword), SyntaxFactory.Token(SyntaxKind.OpenParenToken), null, declaration, initializers, SyntaxFactory.Token(SyntaxKind.SemicolonToken), condition, SyntaxFactory.Token(SyntaxKind.SemicolonToken), incrementors, SyntaxFactory.Token(SyntaxKind.CloseParenToken), statement)); }
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 }))); }
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()))); }