public override SyntaxList <StatementSyntax> VisitReturnStatement(CSS.ReturnStatementSyntax node)
            {
                StatementSyntax stmt;

                if (node.Expression == null)
                {
                    stmt = SyntaxFactory.ReturnStatement();
                }
                else
                {
                    stmt = SyntaxFactory.ReturnStatement((ExpressionSyntax)node.Expression.Accept(nodesVisitor));
                }
                return(SyntaxFactory.SingletonList(stmt));
            }
        private VariableDeclaratorSyntax ConvertToVariableDeclarator(IsPatternExpressionSyntax node)
        {
            return(node.Pattern.TypeSwitch(
                       (DeclarationPatternSyntax d) => {
                var id = ((IdentifierNameSyntax)d.Designation.Accept(_nodesVisitor)).Identifier;
                var ids = SyntaxFactory.SingletonSeparatedList(SyntaxFactory.ModifiedIdentifier(id));
                TypeSyntax right = (TypeSyntax)d.Type.Accept(_nodesVisitor);

                var simpleAsClauseSyntax = SyntaxFactory.SimpleAsClause(right);
                var equalsValueSyntax = SyntaxFactory.EqualsValue(SyntaxFactory.LiteralExpression(SyntaxKind.NothingLiteralExpression, SyntaxFactory.Token(SyntaxKind.NothingKeyword)));
                return SyntaxFactory.VariableDeclarator(ids, simpleAsClauseSyntax, equalsValueSyntax);
            },
                       p => throw new ArgumentOutOfRangeException(nameof(p), p, null)));
        }
        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)
                           )
                       ));
        }
        private static SyntaxToken CSharpDefaultVisibility(SyntaxKindExtensions.TokenContext context)
        {
            switch (context)
            {
            case SyntaxKindExtensions.TokenContext.Global:
                return(SyntaxFactory.Token(SyntaxKind.FriendKeyword));

            case SyntaxKindExtensions.TokenContext.Local:
            case SyntaxKindExtensions.TokenContext.VariableOrConst:
            case SyntaxKindExtensions.TokenContext.Member:
                return(SyntaxFactory.Token(SyntaxKind.PrivateKeyword));
            }
            throw new ArgumentOutOfRangeException(nameof(context));
        }
Exemplo n.º 5
0
        public LambdaExpressionSyntax ConvertLambdaExpression(CSS.AnonymousFunctionExpressionSyntax node, CS.CSharpSyntaxNode body, IEnumerable <ParameterSyntax> parameters, SyntaxTokenList modifiers)
        {
            var symbol        = (IMethodSymbol)ModelExtensions.GetSymbolInfo(_semanticModel, node).Symbol;
            var parameterList = SyntaxFactory.ParameterList(SyntaxFactory.SeparatedList(parameters.Select(p => (Microsoft.CodeAnalysis.VisualBasic.Syntax.ParameterSyntax)p.Accept(_nodesVisitor))));
            LambdaHeaderSyntax      header;
            EndBlockStatementSyntax endBlock;
            SyntaxKind multiLineExpressionKind;
            SyntaxKind singleLineExpressionKind;
            bool       isSub = symbol.ReturnsVoid;

            if (isSub)
            {
                header = SyntaxFactory.SubLambdaHeader(SyntaxFactory.List <AttributeListSyntax>(),
                                                       ConvertModifiers(modifiers, TokenContext.Local), parameterList, null);
                endBlock = SyntaxFactory.EndBlockStatement(SyntaxKind.EndSubStatement,
                                                           SyntaxFactory.Token(SyntaxKind.SubKeyword));
                multiLineExpressionKind  = SyntaxKind.MultiLineSubLambdaExpression;
                singleLineExpressionKind = SyntaxKind.SingleLineSubLambdaExpression;
            }
            else
            {
                header = CreateFunctionHeader(modifiers, parameterList, out endBlock, out multiLineExpressionKind);
                singleLineExpressionKind = SyntaxKind.SingleLineFunctionLambdaExpression;
            }

            SyntaxList <StatementSyntax> statements;

            if (body is CSS.BlockSyntax block)
            {
                statements = ConvertStatements(block.Statements);
            }
            else if (body.Kind() == CSSyntaxKind.ThrowExpression)
            {
                var csThrowExpression = (CSS.ThrowExpressionSyntax)body;
                var vbThrowExpression = (ExpressionSyntax)csThrowExpression.Expression.Accept(_nodesVisitor);
                var vbThrowStatement  = SyntaxFactory.ThrowStatement(SyntaxFactory.Token(SyntaxKind.ThrowKeyword), vbThrowExpression);

                return(SyntaxFactory.MultiLineFunctionLambdaExpression(header,
                                                                       SyntaxFactory.SingletonList <StatementSyntax>(vbThrowStatement), endBlock));
            }
            else
            {
                var stmt = GetStatementSyntax(body.Accept(_nodesVisitor),
                                              expression => isSub ? (StatementSyntax)SyntaxFactory.ExpressionStatement(expression) : SyntaxFactory.ReturnStatement(expression));
                statements = InsertRequiredDeclarations(SyntaxFactory.SingletonList(stmt), body);
            }

            return(CreateLambdaExpression(singleLineExpressionKind, multiLineExpressionKind, header, statements, endBlock));
        }
Exemplo n.º 6
0
        public override VB.VisualBasicSyntaxNode VisitQualifiedCref(CSS.QualifiedCrefSyntax QualifiedCref)
        {
            var IdentifierOrTypeName = QualifiedCref.Container.Accept(this);

            VBS.CrefReferenceSyntax Value = (VBS.CrefReferenceSyntax)QualifiedCref.Member.Accept(this);
            VBS.NameSyntax          Identifier;
            Identifier = IdentifierOrTypeName is VBS.NameSyntax ? (VBS.NameSyntax)IdentifierOrTypeName : VBFactory.IdentifierName(IdentifierOrTypeName.ToString());
            var QualifiedNameSyntax = VBFactory.QualifiedName(left: Identifier, global::VisualBasicSyntaxFactory.DotToken, right: (VBS.SimpleNameSyntax)Value.Name);

            if (Value.Signature == null)
            {
                return(QualifiedNameSyntax);
            }
            return(VBFactory.CrefReference(QualifiedNameSyntax, Value.Signature, null));
        }
Exemplo n.º 7
0
        SyntaxList <StatementSyntax> ConvertSwitchSectionBlock(CSS.SwitchSectionSyntax section)
        {
            List <StatementSyntax> statements = new List <StatementSyntax>();
            var lastStatement = section.Statements.LastOrDefault();

            foreach (var s in section.Statements)
            {
                if (s == lastStatement && s is CSS.BreakStatementSyntax)
                {
                    continue;
                }
                statements.AddRange(ConvertBlock(s));
            }
            return(SyntaxFactory.List(statements));
        }
Exemplo n.º 8
0
        public void ConvertBaseList(BaseTypeDeclarationSyntax type, List <InheritsStatementSyntax> inherits, List <ImplementsStatementSyntax> implements)
        {
            TypeSyntax[] arr;
            switch (type.Kind())
            {
            case Microsoft.CodeAnalysis.CSharp.SyntaxKind.ClassDeclaration:
                var classOrInterface = type.BaseList?.Types.FirstOrDefault()?.Type;
                if (classOrInterface == null)
                {
                    return;
                }
                var classOrInterfaceSymbol = ModelExtensions.GetSymbolInfo(_semanticModel, classOrInterface).Symbol;
                if (classOrInterfaceSymbol?.IsInterfaceType() == true)
                {
                    arr = type.BaseList?.Types.Select(t => (TypeSyntax)t.Type.Accept(_nodesVisitor)).ToArray();
                    if (arr.Length > 0)
                    {
                        implements.Add(SyntaxFactory.ImplementsStatement(arr));
                    }
                }
                else
                {
                    inherits.Add(SyntaxFactory.InheritsStatement((TypeSyntax)classOrInterface.Accept(_nodesVisitor)));
                    arr = type.BaseList?.Types.Skip(1).Select(t => (TypeSyntax)t.Type.Accept(_nodesVisitor)).ToArray();
                    if (arr.Length > 0)
                    {
                        implements.Add(SyntaxFactory.ImplementsStatement(arr));
                    }
                }
                break;

            case Microsoft.CodeAnalysis.CSharp.SyntaxKind.StructDeclaration:
                arr = type.BaseList?.Types.Select(t => (TypeSyntax)t.Type.Accept(_nodesVisitor)).ToArray();
                if (arr?.Length > 0)
                {
                    implements.Add(SyntaxFactory.ImplementsStatement(arr));
                }
                break;

            case Microsoft.CodeAnalysis.CSharp.SyntaxKind.InterfaceDeclaration:
                arr = type.BaseList?.Types.Select(t => (TypeSyntax)t.Type.Accept(_nodesVisitor)).ToArray();
                if (arr?.Length > 0)
                {
                    inherits.Add(SyntaxFactory.InheritsStatement(arr));
                }
                break;
            }
        }
Exemplo n.º 9
0
 public override VB.VisualBasicSyntaxNode VisitNameMemberCref(CSS.NameMemberCrefSyntax node)
 {
     var Name = node.Name.Accept(this);
     var CrefParameters = new List<VBS.CrefSignaturePartSyntax>();
     VBS.CrefSignatureSyntax Signature = null;
     if (node.Parameters != null)
     {
         foreach (CSS.CrefParameterSyntax p in node.Parameters.Parameters)
         {
             VBS.TypeSyntax TypeSyntax1 = (VBS.TypeSyntax)p.Accept(this);
             CrefParameters.Add(VBFactory.CrefSignaturePart(modifier: default(SyntaxToken), TypeSyntax1));
         }
         Signature = VBFactory.CrefSignature(CrefParameters.ToArray());
     }
     return VBFactory.CrefReference((VBS.TypeSyntax)Name, signature: Signature, asClause: null);
 }
 IEnumerable <CaseBlockSyntax> AddLabels(CaseBlockSyntax[] blocks, List <VisualBasicSyntaxNode> gotoLabels)
 {
     foreach (var _block in blocks)
     {
         var block = _block;
         foreach (var caseClause in block.CaseStatement.Cases)
         {
             var expression = caseClause is ElseCaseClauseSyntax ? (VisualBasicSyntaxNode)caseClause : ((SimpleCaseClauseSyntax)caseClause).Value;
             if (gotoLabels.Any(label => label.IsEquivalentTo(expression)))
             {
                 block = block.WithStatements(block.Statements.Insert(0, SyntaxFactory.LabelStatement(MakeGotoSwitchLabel(expression))));
             }
         }
         yield return(block);
     }
 }
Exemplo n.º 11
0
        public override VB.VisualBasicSyntaxNode VisitXmlElement(CSS.XmlElementSyntax node)
        {
            var Content = new SyntaxList <VBS.XmlNodeSyntax>();

            VBS.XmlElementStartTagSyntax StartTag = (VBS.XmlElementStartTagSyntax)node.StartTag.Accept(this);

            bool NoEndTag = string.IsNullOrWhiteSpace(node.EndTag.Name.LocalName.ValueText);
            var  EndTag   = NoEndTag ? VBFactory.XmlElementEndTag(((VBS.XmlNameSyntax)StartTag.Name)) : VBFactory.XmlElementEndTag((VBS.XmlNameSyntax)node.EndTag.Name.Accept(this));

            try
            {
                for (int i = 0, loopTo = node.Content.Count - 1; i <= loopTo; i++)
                {
                    var C = node.Content[i];
                    VBS.XmlNodeSyntax Node1 = (VBS.XmlNodeSyntax)C.Accept(this);
                    if (NoEndTag)
                    {
                        var LastToken = Node1.GetLastToken();
                        if (LastToken.ValueText.IsNewLine())
                        {
                            Node1 = Node1.ReplaceToken(LastToken, default(SyntaxToken));
                        }
                    }
                    Content = Content.Add(Node1);
                }

                if (node.EndTag?.HasLeadingTrivia == true && node.EndTag.GetLeadingTrivia()[0].IsKind(CS.SyntaxKind.DocumentationCommentExteriorTrivia) == true)
                {
                    var NewLeadingTriviaList = new SyntaxTriviaList();
                    NewLeadingTriviaList = NewLeadingTriviaList.Add(VBFactory.DocumentationCommentExteriorTrivia(node.EndTag.GetLeadingTrivia()[0].ToString().Replace("///", "'''")));
                    var NewTokenList = new SyntaxTokenList();
                    NewTokenList = NewTokenList.Add(VBFactory.XmlTextLiteralToken(NewLeadingTriviaList, " ", " ", new SyntaxTriviaList()));
                    Content      = Content.Add(VBFactory.XmlText(NewTokenList));
                    EndTag       = EndTag.WithoutLeadingTrivia();
                }
            }
            catch (OperationCanceledException ex)
            {
                throw;
            }
            catch (Exception ex)
            {
            }
            var XmlElement = VBFactory.XmlElement(StartTag, Content, EndTag);

            return(XmlElement);
        }
Exemplo n.º 12
0
        public override SyntaxList <StatementSyntax> VisitIfStatement(CSS.IfStatementSyntax node)
        {
            IdentifierNameSyntax  name;
            List <ArgumentSyntax> arguments = new List <ArgumentSyntax>();
            StatementSyntax       stmt;

            if (node.Else == null && TryConvertRaiseEvent(node, out name, arguments))
            {
                stmt = SyntaxFactory.RaiseEventStatement(name, SyntaxFactory.ArgumentList(SyntaxFactory.SeparatedList(arguments)));
                return(SyntaxFactory.SingletonList(stmt));
            }
            var             elseIfBlocks = new List <ElseIfBlockSyntax>();
            ElseBlockSyntax elseBlock    = null;

            CollectElseBlocks(node, elseIfBlocks, ref elseBlock);
            if (node.Statement is CSS.BlockSyntax)
            {
                stmt = SyntaxFactory.MultiLineIfBlock(
                    SyntaxFactory.IfStatement((ExpressionSyntax)node.Condition.Accept(_nodesVisitor)).WithThenKeyword(SyntaxFactory.Token(SyntaxKind.ThenKeyword)),
                    ConvertBlock(node.Statement),
                    SyntaxFactory.List(elseIfBlocks),
                    elseBlock
                    );
            }
            else
            {
                if (elseIfBlocks.Any() || elseBlock != null || !IsSimpleStatement(node.Statement))
                {
                    stmt = SyntaxFactory.MultiLineIfBlock(
                        SyntaxFactory.IfStatement((ExpressionSyntax)node.Condition.Accept(_nodesVisitor)).WithThenKeyword(SyntaxFactory.Token(SyntaxKind.ThenKeyword)),
                        ConvertBlock(node.Statement),
                        SyntaxFactory.List(elseIfBlocks),
                        elseBlock
                        );
                }
                else
                {
                    stmt = SyntaxFactory.SingleLineIfStatement(
                        (ExpressionSyntax)node.Condition.Accept(_nodesVisitor),
                        ConvertBlock(node.Statement),
                        null
                        ).WithThenKeyword(SyntaxFactory.Token(SyntaxKind.ThenKeyword));
                }
            }
            return(SyntaxFactory.SingletonList(stmt));
        }
Exemplo n.º 13
0
 public override VB.VisualBasicSyntaxNode VisitXmlEmptyElement(CSS.XmlEmptyElementSyntax node)
 {
     try
     {
         VBS.XmlNodeSyntax Name = (VBS.XmlNodeSyntax)node.Name.Accept(this);
         var ListOfAttributes   = GatherAttributes(node.Attributes);
         return(VBFactory.XmlEmptyElement(Name, ListOfAttributes));
     }
     catch (OperationCanceledException ex)
     {
         throw;
     }
     catch (Exception ex)
     {
         return(VBFactory.XmlText(node.GetText().ToString()));
     }
 }
        public static SyntaxToken ConvertIdentifier(SyntaxToken id)
        {
            var idText = id.ValueText;

            // Underscore is a special character in VB lexer which continues lines - not sure where to find the whole set of other similar tokens if any
            // Rather than a complicated contextual rename, just add an extra dash to all identifiers and hope this method is consistently used
            if (idText.All(c => c == '_'))
            {
                idText += "_";
            }

            if (IsKeywordThatNeedsEscaped(id))
            {
                return(SyntaxFactory.Identifier("[" + idText + "]"));
            }
            return(SyntaxFactory.Identifier(idText));
        }
Exemplo n.º 15
0
        public override SyntaxList <StatementSyntax> VisitUsingStatement(CSS.UsingStatementSyntax node)
        {
            UsingStatementSyntax stmt;

            if (node.Declaration == null)
            {
                stmt = SyntaxFactory.UsingStatement(
                    (ExpressionSyntax)node.Expression?.Accept(_nodesVisitor),
                    SyntaxFactory.SeparatedList <VariableDeclaratorSyntax>()
                    );
            }
            else
            {
                stmt = SyntaxFactory.UsingStatement(null, _commonConversions.RemodelVariableDeclaration(node.Declaration));
            }
            return(SyntaxFactory.SingletonList <StatementSyntax>(SyntaxFactory.UsingBlock(stmt, ConvertBlock(node.Statement))));
        }
Exemplo n.º 16
0
        public override SyntaxList <StatementSyntax> VisitSwitchStatement(CSS.SwitchStatementSyntax node)
        {
            StatementSyntax stmt;

            _blockInfo.Push(new BlockInfo());
            try {
                var blocks = node.Sections.OrderBy(IsDefaultSwitchStatement).Select(ConvertSwitchSection).ToArray();
                stmt = SyntaxFactory.SelectBlock(
                    SyntaxFactory.SelectStatement((ExpressionSyntax)node.Expression.Accept(_nodesVisitor)).WithCaseKeyword(SyntaxFactory.Token(SyntaxKind.CaseKeyword)),
                    SyntaxFactory.List(AddLabels(blocks, _blockInfo.Peek().GotoCaseExpressions))
                    );
                _switchCount++;
            } finally {
                _blockInfo.Pop();
            }
            return(SyntaxFactory.SingletonList(stmt));
        }
Exemplo n.º 17
0
        public LambdaExpressionSyntax ConvertLambdaExpression(AnonymousFunctionExpressionSyntax node, CSharpSyntaxNode body, IEnumerable <ParameterSyntax> parameters, SyntaxTokenList modifiers)
        {
            var symbol        = ModelExtensions.GetSymbolInfo(_semanticModel, node).Symbol as IMethodSymbol;
            var parameterList = SyntaxFactory.ParameterList(SyntaxFactory.SeparatedList(parameters.Select(p => (Microsoft.CodeAnalysis.VisualBasic.Syntax.ParameterSyntax)p.Accept(_nodesVisitor))));
            LambdaHeaderSyntax      header;
            EndBlockStatementSyntax endBlock;
            SyntaxKind multiLineExpressionKind;
            SyntaxKind singleLineExpressionKind;

            if (symbol.ReturnsVoid)
            {
                header = SyntaxFactory.SubLambdaHeader(SyntaxFactory.List <AttributeListSyntax>(),
                                                       ConvertModifiers(modifiers, SyntaxKindExtensions.TokenContext.Local), parameterList, null);
                endBlock = SyntaxFactory.EndBlockStatement(SyntaxKind.EndSubStatement,
                                                           SyntaxFactory.Token(SyntaxKind.SubKeyword));
                multiLineExpressionKind  = SyntaxKind.MultiLineSubLambdaExpression;
                singleLineExpressionKind = SyntaxKind.SingleLineSubLambdaExpression;
            }
            else
            {
                header = CreateFunctionHeader(modifiers, parameterList, out endBlock, out multiLineExpressionKind);
                singleLineExpressionKind = SyntaxKind.SingleLineFunctionLambdaExpression;
            }

            SyntaxList <StatementSyntax> statements;

            if (body is BlockSyntax block)
            {
                statements = ConvertStatements(block.Statements);
            }
            else
            {
                statements = InsertRequiredDeclarations(
                    SyntaxFactory.SingletonList <StatementSyntax>(
                        SyntaxFactory.ReturnStatement((ExpressionSyntax)body.Accept(_nodesVisitor))),
                    body);
            }

            if (statements.Count == 1 && UnpackExpressionFromStatement(statements[0], out var expression))
            {
                return(SyntaxFactory.SingleLineLambdaExpression(singleLineExpressionKind, header, expression));
            }

            return(SyntaxFactory.MultiLineLambdaExpression(multiLineExpressionKind, header, statements, endBlock));
        }
            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?Literal(true) : (ExpressionSyntax)node.Condition.Accept(nodesVisitor);

                    block = SyntaxFactory.WhileBlock(
                        SyntaxFactory.WhileStatement(condition),
                        stmts
                        );
                    return(SyntaxFactory.List(node.Initializers.Select(ConvertSingleExpression)).Add(block));
                }
                return(SyntaxFactory.SingletonList(block));
            }
        private StatementSyntax WrapRootExpression(ExpressionSyntax expressionSyntax)
        {
            InvocationExpressionSyntax invocationExpression = expressionSyntax as InvocationExpressionSyntax;

            if (invocationExpression != null)
            {
                var lastExpression = expressionSyntax.FollowProperty(exp => exp.TypeSwitch(
                                                                         (InvocationExpressionSyntax e) => e.Expression,
                                                                         (ParenthesizedExpressionSyntax e) => e.Expression,
                                                                         (CastExpressionSyntax e) => e.Expression,
                                                                         (MemberAccessExpressionSyntax e) => e.Expression
                                                                         )).LastOrDefault();
                if (lastExpression != null && !(lastExpression is IdentifierNameSyntax || lastExpression is InstanceExpressionSyntax))
                {
                    return(SyntaxFactory.CallStatement(expressionSyntax));
                }
            }
            return(SyntaxFactory.ExpressionStatement(expressionSyntax));
        }
Exemplo n.º 20
0
        private static SyntaxToken CSharpDefaultVisibility(TokenContext context)
        {
            switch (context)
            {
            case TokenContext.Global:
            case TokenContext.InterfaceOrModule:
                return(SyntaxFactory.Token(SyntaxKind.FriendKeyword));

            case TokenContext.Local:
            case TokenContext.MemberInClass:
            case TokenContext.MemberInModule:
            case TokenContext.MemberInStruct:
                return(SyntaxFactory.Token(SyntaxKind.PrivateKeyword));

            case TokenContext.MemberInInterface:
                return(SyntaxFactory.Token(SyntaxKind.PublicKeyword));
            }
            throw new ArgumentOutOfRangeException(nameof(context));
        }
Exemplo n.º 21
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)));
        }
Exemplo n.º 22
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));
        }
Exemplo n.º 23
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()));
        }
Exemplo n.º 24
0
        public override SyntaxList <StatementSyntax> VisitBreakStatement(CSS.BreakStatementSyntax node)
        {
            var statementKind = SyntaxKind.None;
            var keywordKind   = SyntaxKind.None;

            foreach (var stmt in node.GetAncestors <CSS.StatementSyntax>())
            {
                if (stmt is CSS.DoStatementSyntax)
                {
                    statementKind = SyntaxKind.ExitDoStatement;
                    keywordKind   = SyntaxKind.DoKeyword;
                    break;
                }
                if (stmt is CSS.WhileStatementSyntax)
                {
                    statementKind = SyntaxKind.ExitWhileStatement;
                    keywordKind   = SyntaxKind.WhileKeyword;
                    break;
                }
                if (stmt is CSS.ForEachStatementSyntax)
                {
                    statementKind = SyntaxKind.ExitForStatement;
                    keywordKind   = SyntaxKind.ForKeyword;
                    break;
                }
                if (stmt is CSS.ForStatementSyntax fs)
                {
                    bool isFor = ConvertForToSimpleForNextWithoutStatements(fs, out _);
                    statementKind = isFor ? SyntaxKind.ExitForStatement : SyntaxKind.ExitWhileStatement;
                    keywordKind   = isFor ? SyntaxKind.ForKeyword : SyntaxKind.WhileKeyword;
                    break;
                }
                if (stmt is CSS.SwitchStatementSyntax)
                {
                    statementKind = SyntaxKind.ExitSelectStatement;
                    keywordKind   = SyntaxKind.SelectKeyword;
                    break;
                }
            }
            return(SyntaxFactory.SingletonList <StatementSyntax>(SyntaxFactory.ExitStatement(statementKind, SyntaxFactory.Token(keywordKind))));
        }
Exemplo n.º 25
0
        private VariableDeclaratorSyntax ConvertToVariableDeclarator(DeclarationExpressionSyntax des)
        {
            var        id  = ((IdentifierNameSyntax)des.Accept(_nodesVisitor)).Identifier;
            var        ids = SyntaxFactory.SingletonSeparatedList(SyntaxFactory.ModifiedIdentifier(id));
            TypeSyntax typeSyntax;

            if (des.Type.IsVar)
            {
                var typeSymbol = (ITypeSymbol)ModelExtensions.GetSymbolInfo(_semanticModel, des.Type).ExtractBestMatch();
                typeSyntax = typeSymbol.ToVbSyntax(_semanticModel, des.Type);
            }
            else
            {
                typeSyntax = (TypeSyntax)des.Type.Accept(_nodesVisitor);
            }

            var simpleAsClauseSyntax = SyntaxFactory.SimpleAsClause(typeSyntax);
            var equalsValueSyntax    = SyntaxFactory.EqualsValue(SyntaxFactory.LiteralExpression(SyntaxKind.NothingLiteralExpression, SyntaxFactory.Token(SyntaxKind.NothingKeyword)));

            return(SyntaxFactory.VariableDeclarator(ids, simpleAsClauseSyntax, equalsValueSyntax));
        }
Exemplo n.º 26
0
        public SyntaxList <StatementSyntax> ConvertBody(BlockSyntax body,
                                                        ArrowExpressionClauseSyntax expressionBody, MethodBodyVisitor iteratorState = null)
        {
            if (body != null)
            {
                return(ConvertStatements(body.Statements, iteratorState));
            }

            if (expressionBody != null)
            {
                var convertedBody = expressionBody.Expression.Accept(_nodesVisitor);
                if (convertedBody is ExpressionSyntax convertedBodyExpression)
                {
                    convertedBody = SyntaxFactory.ReturnStatement(convertedBodyExpression);
                }

                return(SyntaxFactory.SingletonList((StatementSyntax)convertedBody));
            }

            return(SyntaxFactory.List <StatementSyntax>());
        }
Exemplo n.º 27
0
        public override VB.VisualBasicSyntaxNode VisitPredefinedType(CSS.PredefinedTypeSyntax node)
        {
            var Token = VBUtil.ConvertTypesTokenToKind(CS.CSharpExtensions.Kind(node.Keyword), true);
            var switchExpr = Token.RawKind;
            switch (switchExpr)
            {
                case (int)VB.SyntaxKind.EmptyToken:
                    {
                        return VBFactory.ParseTypeName(node.ToString());
                    }

                case (int)VB.SyntaxKind.NothingKeyword:
                    {
                        return global::VisualBasicSyntaxFactory.NothingExpression;
                    }

                default:
                    {
                        return VBFactory.PredefinedType(Token);
                    }
            }
        }
Exemplo n.º 28
0
 void CollectElseBlocks(CSS.IfStatementSyntax node, List <ElseIfBlockSyntax> elseIfBlocks, ref ElseBlockSyntax elseBlock)
 {
     if (node.Else == null)
     {
         return;
     }
     if (node.Else.Statement is CSS.IfStatementSyntax)
     {
         var elseIf = (CSS.IfStatementSyntax)node.Else.Statement;
         elseIfBlocks.Add(
             SyntaxFactory.ElseIfBlock(
                 SyntaxFactory.ElseIfStatement((ExpressionSyntax)elseIf.Condition.Accept(_nodesVisitor)).WithThenKeyword(SyntaxFactory.Token(SyntaxKind.ThenKeyword)),
                 ConvertBlock(elseIf.Statement)
                 )
             );
         CollectElseBlocks(elseIf, elseIfBlocks, ref elseBlock);
     }
     else
     {
         elseBlock = SyntaxFactory.ElseBlock(ConvertBlock(node.Else.Statement));
     }
 }
Exemplo n.º 29
0
        private VariableDeclaratorSyntax ConvertToVariableDeclarator(CSS.PropertyDeclarationSyntax des)
        {
            var        id  = GetVbPropertyBackingFieldName(des);
            var        ids = SyntaxFactory.SingletonSeparatedList(SyntaxFactory.ModifiedIdentifier(id));
            TypeSyntax typeSyntax;

            if (des.Type.IsVar)
            {
                var typeSymbol = ModelExtensions.GetSymbolInfo(_semanticModel, des.Type).ExtractBestMatch <ITypeSymbol>();
                typeSyntax = typeSymbol?.ToVbSyntax(_semanticModel, des.Type);
            }
            else
            {
                typeSyntax = (TypeSyntax)des.Type.Accept(_nodesVisitor);
            }

            var simpleAsClauseSyntax = typeSyntax != null?SyntaxFactory.SimpleAsClause(typeSyntax) : null;   //Gracefully degrade when no type information available

            EqualsValueSyntax equalsValueSyntax = null;

            return(SyntaxFactory.VariableDeclarator(ids, simpleAsClauseSyntax, equalsValueSyntax));
        }
Exemplo n.º 30
0
        private VariableDeclaratorSyntax ConvertToVariableDeclaratorOrNull(CSS.IsPatternExpressionSyntax node)
        {
            switch (node.Pattern)
            {
            case CSS.DeclarationPatternSyntax d: {
                var        id    = ((IdentifierNameSyntax)d.Designation.Accept(_nodesVisitor)).Identifier;
                var        ids   = SyntaxFactory.SingletonSeparatedList(SyntaxFactory.ModifiedIdentifier(id));
                TypeSyntax right = (TypeSyntax)d.Type.Accept(_nodesVisitor);

                var simpleAsClauseSyntax = SyntaxFactory.SimpleAsClause(right);
                var equalsValueSyntax    = SyntaxFactory.EqualsValue(
                    SyntaxFactory.LiteralExpression(SyntaxKind.NothingLiteralExpression,
                                                    SyntaxFactory.Token(SyntaxKind.NothingKeyword)));
                return(SyntaxFactory.VariableDeclarator(ids, simpleAsClauseSyntax, equalsValueSyntax));
            }

            case CSS.ConstantPatternSyntax _:
                return(null);

            default:
                throw new ArgumentOutOfRangeException(nameof(node.Pattern), node.Pattern, null);
            }
        }