Пример #1
0
        private SyntaxList <StatementSyntax> ConvertStatements(SyntaxList <Microsoft.CodeAnalysis.CSharp.Syntax.StatementSyntax> statements,
                                                               MethodBodyVisitor iteratorState = null)
        {
            var methodBodyVisitor = CreateMethodBodyVisitor(iteratorState);

            return(SyntaxFactory.List(statements.SelectMany(s => ConvertStatement(s, methodBodyVisitor))));
        }
Пример #2
0
        public AccessorBlockSyntax ConvertAccessor(AccessorDeclarationSyntax node, out bool isIterator)
        {
            SyntaxKind blockKind;
            AccessorStatementSyntax      stmt;
            EndBlockStatementSyntax      endStmt;
            SyntaxList <StatementSyntax> body;

            isIterator = false;
            var isIteratorState = new MethodBodyVisitor(_semanticModel, _nodesVisitor, _triviaConverter, this);

            body       = ConvertBody(node.Body, node.ExpressionBody, isIteratorState);
            isIterator = isIteratorState.IsIterator;
            var attributes = SyntaxFactory.List(node.AttributeLists.Select(a => (AttributeListSyntax)a.Accept(_nodesVisitor)));
            var modifiers  = ConvertModifiers(node.Modifiers, SyntaxKindExtensions.TokenContext.Local);
            var parent     = (BasePropertyDeclarationSyntax)node.Parent.Parent;

            Microsoft.CodeAnalysis.VisualBasic.Syntax.ParameterSyntax valueParam;

            switch (CSharpExtensions.Kind(node))
            {
            case Microsoft.CodeAnalysis.CSharp.SyntaxKind.GetAccessorDeclaration:
                blockKind = SyntaxKind.GetAccessorBlock;
                stmt      = SyntaxFactory.GetAccessorStatement(attributes, modifiers, null);
                endStmt   = SyntaxFactory.EndGetStatement();
                break;

            case Microsoft.CodeAnalysis.CSharp.SyntaxKind.SetAccessorDeclaration:
                blockKind  = SyntaxKind.SetAccessorBlock;
                valueParam = SyntaxFactory.Parameter(SyntaxFactory.ModifiedIdentifier("value"))
                             .WithAsClause(SyntaxFactory.SimpleAsClause((TypeSyntax)parent.Type.Accept(_nodesVisitor)))
                             .WithModifiers(SyntaxFactory.TokenList(SyntaxFactory.Token(SyntaxKind.ByValKeyword)));
                stmt    = SyntaxFactory.SetAccessorStatement(attributes, modifiers, SyntaxFactory.ParameterList(SyntaxFactory.SingletonSeparatedList(valueParam)));
                endStmt = SyntaxFactory.EndSetStatement();
                break;

            case Microsoft.CodeAnalysis.CSharp.SyntaxKind.AddAccessorDeclaration:
                blockKind  = SyntaxKind.AddHandlerAccessorBlock;
                valueParam = SyntaxFactory.Parameter(SyntaxFactory.ModifiedIdentifier("value"))
                             .WithAsClause(SyntaxFactory.SimpleAsClause((TypeSyntax)parent.Type.Accept(_nodesVisitor)))
                             .WithModifiers(SyntaxFactory.TokenList(SyntaxFactory.Token(SyntaxKind.ByValKeyword)));
                stmt    = SyntaxFactory.AddHandlerAccessorStatement(attributes, modifiers, SyntaxFactory.ParameterList(SyntaxFactory.SingletonSeparatedList(valueParam)));
                endStmt = SyntaxFactory.EndAddHandlerStatement();
                break;

            case Microsoft.CodeAnalysis.CSharp.SyntaxKind.RemoveAccessorDeclaration:
                blockKind  = SyntaxKind.RemoveHandlerAccessorBlock;
                valueParam = SyntaxFactory.Parameter(SyntaxFactory.ModifiedIdentifier("value"))
                             .WithAsClause(SyntaxFactory.SimpleAsClause((TypeSyntax)parent.Type.Accept(_nodesVisitor)))
                             .WithModifiers(SyntaxFactory.TokenList(SyntaxFactory.Token(SyntaxKind.ByValKeyword)));
                stmt    = SyntaxFactory.RemoveHandlerAccessorStatement(attributes, modifiers, SyntaxFactory.ParameterList(SyntaxFactory.SingletonSeparatedList(valueParam)));
                endStmt = SyntaxFactory.EndRemoveHandlerStatement();
                break;

            default:
                throw new NotSupportedException();
            }
            return(SyntaxFactory.AccessorBlock(blockKind, stmt, body, endStmt));
        }
Пример #3
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>());
        }
Пример #4
0
        private CSharpSyntaxVisitor <SyntaxList <StatementSyntax> > CreateMethodBodyVisitor(MethodBodyVisitor methodBodyVisitor = null)
        {
            var visitor = methodBodyVisitor ?? new MethodBodyVisitor(_semanticModel, _nodesVisitor, _triviaConverter, this);

            return(visitor.CommentConvertingVisitor);
        }