Exemple #1
0
        public override SyntaxList <StatementSyntax> VisitForStatement(CSS.ForStatementSyntax node)
        {
            StatementSyntax block;

            if (!ConvertForToSimpleForNext(node, out block))
            {
                var stmts = ConvertBlock(node.Statement)
                            .AddRange(node.Incrementors.Select(ConvertSingleExpression));
                var condition = node.Condition == null?CommonConversions.Literal(true) : (ExpressionSyntax)node.Condition.Accept(_nodesVisitor);

                block = SyntaxFactory.WhileBlock(
                    SyntaxFactory.WhileStatement(condition),
                    stmts
                    );

                var declarations = new List <StatementSyntax>();
                if (node.Declaration != null)
                {
                    var syntaxTokenList = SyntaxFactory.TokenList(SyntaxFactory.Token(SyntaxKind.DimKeyword));
                    declarations.Add(SyntaxFactory.LocalDeclarationStatement(syntaxTokenList, _commonConversions.RemodelVariableDeclaration(node.Declaration)));
                }

                return(SyntaxFactory.List(declarations.Concat(node.Initializers.Select(ConvertSingleExpression))).Add(block));
            }
            return(SyntaxFactory.SingletonList(block));
        }
Exemple #2
0
        public override SyntaxList <StatementSyntax> VisitForEachStatement(CSS.ForEachStatementSyntax node)
        {
            VisualBasicSyntaxNode variable;

            if (node.Type.IsVar)
            {
                variable = SyntaxFactory.IdentifierName(CommonConversions.ConvertIdentifier(node.Identifier));
            }
            else
            {
                variable = SyntaxFactory.VariableDeclarator(
                    SyntaxFactory.SingletonSeparatedList(SyntaxFactory.ModifiedIdentifier(CommonConversions.ConvertIdentifier(node.Identifier))),
                    SyntaxFactory.SimpleAsClause((TypeSyntax)node.Type.Accept(_nodesVisitor)),
                    null
                    );
            }
            var expression = (ExpressionSyntax)node.Expression.Accept(_nodesVisitor);
            var stmt       = ConvertBlock(node.Statement);
            var block      = SyntaxFactory.ForEachBlock(
                SyntaxFactory.ForEachStatement(variable, expression),
                stmt,
                SyntaxFactory.NextStatement()
                );

            return(SyntaxFactory.SingletonList <StatementSyntax>(block));
        }
Exemple #3
0
 public MethodBodyExecutableStatementVisitor(SemanticModel semanticModel,
                                             CommentConvertingVisitorWrapper <VisualBasicSyntaxNode> nodesVisitor, CommonConversions commonConversions)
 {
     this._semanticModel      = semanticModel;
     this._nodesVisitor       = nodesVisitor;
     _commonConversions       = commonConversions;
     CommentConvertingVisitor = new CommentConvertingMethodBodyVisitor(this);
 }
Exemple #4
0
 public MethodBodyVisitor(SemanticModel semanticModel,
                          CSharpSyntaxVisitor <VisualBasicSyntaxNode> nodesVisitor, TriviaConverter triviaConverter,
                          CommonConversions commonConversions)
 {
     this._semanticModel      = semanticModel;
     this._nodesVisitor       = nodesVisitor;
     _commonConversions       = commonConversions;
     CommentConvertingVisitor = new CommentConvertingMethodBodyVisitor(this, triviaConverter);
 }
Exemple #5
0
        public override SyntaxList <StatementSyntax> VisitLocalDeclarationStatement(CSS.LocalDeclarationStatementSyntax node)
        {
            var modifiers = CommonConversions.ConvertModifiers(node.Modifiers, TokenContext.Local);

            if (modifiers.Count == 0)
            {
                modifiers = modifiers.Add(SyntaxFactory.Token(SyntaxKind.DimKeyword));
            }
            return(SyntaxFactory.SingletonList <StatementSyntax>(
                       SyntaxFactory.LocalDeclarationStatement(
                           modifiers, _commonConversions.RemodelVariableDeclaration(node.Declaration)
                           )
                       ));
        }
Exemple #6
0
        public override SyntaxList <StatementSyntax> VisitForEachVariableStatement(CSS.ForEachVariableStatementSyntax node)
        {
            var loopVar         = node.Variable.Accept(_nodesVisitor);
            var extraStatements = new List <StatementSyntax>();

            if (node.Variable is CSS.DeclarationExpressionSyntax des && des.Designation is CSS.ParenthesizedVariableDesignationSyntax pv)
            {
                var tupleName = CommonConversions.GetTupleName(pv);
                extraStatements.AddRange(pv.Variables.Select((v, i) => {
                    var initializer = SyntaxFactory.EqualsValue(SyntaxFactory.SimpleMemberAccessExpression(
                                                                    SyntaxFactory.IdentifierName(tupleName),
                                                                    SyntaxFactory.IdentifierName("Item" + (i + 1).ToString())));
                    VariableDeclaratorSyntax variableDeclaratorSyntax = SyntaxFactory.VariableDeclarator(
                        SyntaxFactory.ModifiedIdentifier(SyntaxFactory.Identifier(v.ToString())))
                                                                        .WithInitializer(initializer);
                    return(CommonConversions.CreateLocalDeclarationStatement(variableDeclaratorSyntax));
                }));
            }
            return(CreateForEachStatement(loopVar, node.Expression, node.Statement, extraStatements.ToArray()));
        }
Exemple #7
0
        public override SyntaxList <StatementSyntax> VisitGotoStatement(CSS.GotoStatementSyntax node)
        {
            LabelSyntax label;

            if (node.IsKind(CS.SyntaxKind.GotoCaseStatement, CS.SyntaxKind.GotoDefaultStatement))
            {
                if (_blockInfo.Count == 0)
                {
                    throw new InvalidOperationException("goto case/goto default outside switch is illegal!");
                }
                var labelExpression = node.Expression?.Accept(_nodesVisitor) ?? SyntaxFactory.ElseCaseClause();
                _blockInfo.Peek().GotoCaseExpressions.Add(labelExpression);
                label = SyntaxFactory.Label(SyntaxKind.IdentifierLabel, MakeGotoSwitchLabel(labelExpression));
            }
            else
            {
                label = SyntaxFactory.Label(SyntaxKind.IdentifierLabel, CommonConversions.ConvertIdentifier(((CSS.IdentifierNameSyntax)node.Expression).Identifier));
            }
            return(SyntaxFactory.SingletonList <StatementSyntax>(SyntaxFactory.GoToStatement(label)));
        }
Exemple #8
0
        public override SyntaxList <StatementSyntax> VisitForEachStatement(CSS.ForEachStatementSyntax node)
        {
            VisualBasicSyntaxNode variable;

            if (node.Type.IsVar)
            {
                variable = SyntaxFactory.IdentifierName(CommonConversions.ConvertIdentifier(node.Identifier));
            }
            else
            {
                variable = SyntaxFactory.VariableDeclarator(
                    SyntaxFactory.SingletonSeparatedList(
                        SyntaxFactory.ModifiedIdentifier(CommonConversions.ConvertIdentifier(node.Identifier))),
                    SyntaxFactory.SimpleAsClause((TypeSyntax)node.Type.Accept(_nodesVisitor)),
                    null
                    );
            }

            return(CreateForEachStatement(variable, node.Expression, node.Statement));
        }
Exemple #9
0
        bool ConvertForToSimpleForNext(CSS.ForStatementSyntax node, out StatementSyntax block)
        {
            //   ForStatement -> ForNextStatement when for-loop is simple

            // only the following forms of the for-statement are allowed:
            // for (TypeReference name = start; name < oneAfterEnd; name += step)
            // for (name = start; name < oneAfterEnd; name += step)
            // for (TypeReference name = start; name <= end; name += step)
            // for (name = start; name <= end; name += step)
            // for (TypeReference name = start; name > oneAfterEnd; name -= step)
            // for (name = start; name > oneAfterEnd; name -= step)
            // for (TypeReference name = start; name >= end; name -= step)
            // for (name = start; name >= end; name -= step)

            block = null;

            // check if the form is valid and collect TypeReference, name, start, end and step
            bool hasVariable = node.Declaration != null && node.Declaration.Variables.Count == 1;

            if (!hasVariable && node.Initializers.Count != 1)
            {
                return(false);
            }
            if (node.Incrementors.Count != 1)
            {
                return(false);
            }
            var iterator = node.Incrementors.FirstOrDefault()?.Accept(_nodesVisitor) as AssignmentStatementSyntax;

            if (iterator == null || !iterator.IsKind(SyntaxKind.AddAssignmentStatement, SyntaxKind.SubtractAssignmentStatement))
            {
                return(false);
            }
            var iteratorIdentifier = iterator.Left as IdentifierNameSyntax;

            if (iteratorIdentifier == null)
            {
                return(false);
            }
            var stepExpression = iterator.Right as LiteralExpressionSyntax;

            if (stepExpression == null || !(stepExpression.Token.Value is int))
            {
                return(false);
            }
            int step = (int)stepExpression.Token.Value;

            if (SyntaxTokenExtensions.IsKind(iterator.OperatorToken, SyntaxKind.MinusEqualsToken))
            {
                step = -step;
            }

            var condition = node.Condition as CSS.BinaryExpressionSyntax;

            if (condition == null || !(condition.Left is CSS.IdentifierNameSyntax))
            {
                return(false);
            }
            if (((CSS.IdentifierNameSyntax)condition.Left).Identifier.IsEquivalentTo(iteratorIdentifier.Identifier))
            {
                return(false);
            }

            ExpressionSyntax end;

            if (iterator.IsKind(SyntaxKind.SubtractAssignmentStatement))
            {
                if (condition.IsKind(CS.SyntaxKind.GreaterThanOrEqualExpression))
                {
                    end = (ExpressionSyntax)condition.Right.Accept(_nodesVisitor);
                }
                else if (condition.IsKind(CS.SyntaxKind.GreaterThanExpression))
                {
                    end = SyntaxFactory.BinaryExpression(SyntaxKind.AddExpression, (ExpressionSyntax)condition.Right.Accept(_nodesVisitor), SyntaxFactory.Token(SyntaxKind.PlusToken), CommonConversions.Literal(1));
                }
                else
                {
                    return(false);
                }
            }
            else
            {
                if (condition.IsKind(CS.SyntaxKind.LessThanOrEqualExpression))
                {
                    end = (ExpressionSyntax)condition.Right.Accept(_nodesVisitor);
                }
                else if (condition.IsKind(CS.SyntaxKind.LessThanExpression))
                {
                    end = SyntaxFactory.BinaryExpression(SyntaxKind.SubtractExpression, (ExpressionSyntax)condition.Right.Accept(_nodesVisitor), SyntaxFactory.Token(SyntaxKind.MinusToken), CommonConversions.Literal(1));
                }
                else
                {
                    return(false);
                }
            }

            VisualBasicSyntaxNode variable;
            ExpressionSyntax      start;

            if (hasVariable)
            {
                var v = node.Declaration.Variables[0];
                start = (ExpressionSyntax)v.Initializer?.Value.Accept(_nodesVisitor);
                if (start == null)
                {
                    return(false);
                }
                variable = SyntaxFactory.VariableDeclarator(
                    SyntaxFactory.SingletonSeparatedList(SyntaxFactory.ModifiedIdentifier(_commonConversions.ConvertIdentifier(v.Identifier))),
                    node.Declaration.Type.IsVar ? null : SyntaxFactory.SimpleAsClause((TypeSyntax)node.Declaration.Type.Accept(_nodesVisitor)),
                    null
                    );
            }
            else
            {
                var initializer = node.Initializers.FirstOrDefault() as CSS.AssignmentExpressionSyntax;
                if (initializer == null || !initializer.IsKind(CS.SyntaxKind.SimpleAssignmentExpression))
                {
                    return(false);
                }
                if (!(initializer.Left is CSS.IdentifierNameSyntax))
                {
                    return(false);
                }
                if (((CSS.IdentifierNameSyntax)initializer.Left).Identifier.IsEquivalentTo(iteratorIdentifier.Identifier))
                {
                    return(false);
                }
                variable = initializer.Left.Accept(_nodesVisitor);
                start    = (ExpressionSyntax)initializer.Right.Accept(_nodesVisitor);
            }

            block = SyntaxFactory.ForBlock(
                SyntaxFactory.ForStatement(variable, start, end, step == 1 ? null : SyntaxFactory.ForStepClause(CommonConversions.Literal(step))),
                ConvertBlock(node.Statement),
                SyntaxFactory.NextStatement()
                );
            return(true);
        }
Exemple #10
0
 public override SyntaxList <StatementSyntax> VisitLabeledStatement(CSS.LabeledStatementSyntax node)
 {
     return(SyntaxFactory.SingletonList <StatementSyntax>(SyntaxFactory.LabelStatement(CommonConversions.ConvertIdentifier(node.Identifier)))
            .AddRange(ConvertBlock(node.Statement)));
 }
Exemple #11
0
        CatchBlockSyntax ConvertCatchClause(int index, CSS.CatchClauseSyntax catchClause)
        {
            var statements = ConvertBlock(catchClause.Block);

            if (catchClause.Declaration == null)
            {
                return(SyntaxFactory.CatchBlock(SyntaxFactory.CatchStatement(), statements));
            }
            var    type = (TypeSyntax)catchClause.Declaration.Type.Accept(_nodesVisitor);
            string simpleTypeName;

            if (type is QualifiedNameSyntax)
            {
                simpleTypeName = ((QualifiedNameSyntax)type).Right.ToString();
            }
            else
            {
                simpleTypeName = type.ToString();
            }
            return(SyntaxFactory.CatchBlock(
                       SyntaxFactory.CatchStatement(
                           SyntaxFactory.IdentifierName(SyntaxTokenExtensions.IsKind(catchClause.Declaration.Identifier, CS.SyntaxKind.None) ? SyntaxFactory.Identifier($"__unused{simpleTypeName}{index + 1}__") : CommonConversions.ConvertIdentifier(catchClause.Declaration.Identifier)),
                           SyntaxFactory.SimpleAsClause(type),
                           catchClause.Filter == null ? null : SyntaxFactory.CatchFilterClause((ExpressionSyntax)catchClause.Filter.FilterExpression.Accept(_nodesVisitor))
                           ), statements
                       ));
        }