コード例 #1
0
    public AccessorBlockSyntax ConvertAccessor(CSSyntax.AccessorDeclarationSyntax node, out bool isIterator, bool isAutoImplementedProperty = false)
    {
        SyntaxKind blockKind;
        AccessorStatementSyntax      stmt;
        EndBlockStatementSyntax      endStmt;
        SyntaxList <StatementSyntax> body;

        isIterator = false;
        var accesorKind     = CSharpExtensions.Kind(node);
        var isIteratorState = new MethodBodyExecutableStatementVisitor(_semanticModel, _nodesVisitor, this);

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

        Microsoft.CodeAnalysis.VisualBasic.Syntax.ParameterSyntax valueParam;

        switch (accesorKind)
        {
        case CSSyntaxKind.GetAccessorDeclaration:
            blockKind = SyntaxKind.GetAccessorBlock;
            stmt      = SyntaxFactory.GetAccessorStatement(attributes, modifiers, null);
            endStmt   = SyntaxFactory.EndGetStatement();
            if (isAutoImplementedProperty)
            {
                body = body.Count > 0 ? body :
                       SyntaxFactory.SingletonList((StatementSyntax)SyntaxFactory.ReturnStatement(SyntaxFactory.IdentifierName(GetVbPropertyBackingFieldName(parent))));
            }
            break;

        case CSSyntaxKind.SetAccessorDeclaration:
            blockKind  = SyntaxKind.SetAccessorBlock;
            valueParam = SyntaxFactory.Parameter(SyntaxFactory.ModifiedIdentifier("value"))
                         .WithAsClause(SyntaxFactory.SimpleAsClause((TypeSyntax)parent.Type.Accept(_nodesVisitor, false)))
                         .WithModifiers(SyntaxFactory.TokenList(SyntaxFactory.Token(SyntaxKind.ByValKeyword)));
            stmt    = SyntaxFactory.SetAccessorStatement(attributes, modifiers, SyntaxFactory.ParameterList(SyntaxFactory.SingletonSeparatedList(valueParam)));
            endStmt = SyntaxFactory.EndSetStatement();
            if (isAutoImplementedProperty)
            {
                body = body.Count > 0 ? body :
                       SyntaxFactory.SingletonList((StatementSyntax)SyntaxFactory.AssignmentStatement(SyntaxKind.SimpleAssignmentStatement,
                                                                                                      SyntaxFactory.IdentifierName(GetVbPropertyBackingFieldName(parent)),
                                                                                                      SyntaxFactory.Token(SyntaxKind.SimpleAssignmentStatement.GetExpressionOperatorTokenKind()),
                                                                                                      SyntaxFactory.IdentifierName("value")
                                                                                                      ));
            }
            break;

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

        case CSSyntaxKind.RemoveAccessorDeclaration:
            blockKind  = SyntaxKind.RemoveHandlerAccessorBlock;
            valueParam = SyntaxFactory.Parameter(SyntaxFactory.ModifiedIdentifier("value"))
                         .WithAsClause(SyntaxFactory.SimpleAsClause((TypeSyntax)parent.Type.Accept(_nodesVisitor, false)))
                         .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).WithCsSourceMappingFrom(node));
    }
コード例 #2
0
    public SyntaxList <StatementSyntax> ConvertBody(CSSyntax.BlockSyntax body,
                                                    CSSyntax.ArrowExpressionClauseSyntax expressionBody, bool hasReturnType, MethodBodyExecutableStatementVisitor 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 = hasReturnType ? SyntaxFactory.ReturnStatement(convertedBodyExpression)
                    : SyntaxFactory.ExpressionStatement(convertedBodyExpression);
            }

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

        return(SyntaxFactory.List <StatementSyntax>());
    }
コード例 #3
0
    private CS.CSharpSyntaxVisitor <SyntaxList <StatementSyntax> > CreateMethodBodyVisitor(MethodBodyExecutableStatementVisitor methodBodyExecutableStatementVisitor = null)
    {
        var visitor = methodBodyExecutableStatementVisitor ?? new MethodBodyExecutableStatementVisitor(_semanticModel, _nodesVisitor, this);

        return(visitor.CommentConvertingVisitor);
    }