Пример #1
0
        private static InvocationExpressionSyntax Merge(
            InvocationExpressionSyntax outerMostInvocation,
            MemberAccessExpressionSyntax innerMostWhereAccess,
            List <ExpressionSyntax> whereArguments,
            SemanticModel semanticModel)
        {
            var firstArgument = whereArguments[0];

            string               parameterName;
            ParameterSyntax      firstParameter;
            IdentifierNameSyntax firstParameterIdentifier;
            ExpressionSyntax     filterExpression;

            if (firstArgument.IsKind(SyntaxKind.SimpleLambdaExpression))
            {
                var lambda = (SimpleLambdaExpressionSyntax)firstArgument;
                firstParameter           = lambda.Parameter;
                parameterName            = firstParameter.Identifier.Text;
                firstParameterIdentifier = SyntaxFactory.IdentifierName(firstParameter.Identifier);
                filterExpression         = MakeExpressionFromLambdaBody(lambda.Body);
            }
            else
            {
                parameterName = NameHelper.GetLambdaParameterName(
                    outerMostInvocation.SpanStart,
                    semanticModel);

                var parameterIdentifier = SyntaxFactory
                                          .Identifier(parameterName)
                                          .WithAdditionalAnnotations(RenameAnnotation.Create());

                firstParameter = SyntaxFactory.Parameter(parameterIdentifier);

                firstParameterIdentifier = SyntaxFactory.IdentifierName(parameterIdentifier);

                filterExpression = ExtendedSyntaxFactory.MakeInvocation(
                    firstArgument,
                    firstParameterIdentifier);
            }


            for (int i = 1; i < whereArguments.Count; ++i)
            {
                ExpressionSyntax andOperand;

                if (whereArguments[i].IsKind(SyntaxKind.SimpleLambdaExpression))
                {
                    var currentLambda        = (SimpleLambdaExpressionSyntax)whereArguments[i];
                    var currentParameter     = currentLambda.Parameter;
                    var currentParameterName = currentParameter.Identifier.Text;

                    if (currentParameterName != parameterName)
                    {
                        var parameterSymbol = semanticModel.GetDeclaredSymbol(currentParameter);

                        var substituteRewriter = new SubstituteRewriter(
                            currentParameterName,
                            parameterSymbol,
                            semanticModel,
                            firstParameterIdentifier);

                        var newBody = (CSharpSyntaxNode)currentLambda.Body.Accept(substituteRewriter);

                        andOperand = MakeExpressionFromLambdaBody(newBody);
                    }
                    else
                    {
                        andOperand = MakeExpressionFromLambdaBody(currentLambda.Body);
                    }
                }
                else
                {
                    andOperand = ExtendedSyntaxFactory.MakeInvocation(
                        whereArguments[i],
                        firstParameterIdentifier);
                }

                filterExpression = SyntaxFactory.BinaryExpression(
                    SyntaxKind.LogicalAndExpression,
                    filterExpression,
                    andOperand);
            }

            var newLambda = SyntaxFactory.SimpleLambdaExpression(
                firstParameter,
                filterExpression);

            var newInvocation = ExtendedSyntaxFactory.MakeInvocation(
                innerMostWhereAccess,
                newLambda);

            return(newInvocation);
        }
Пример #2
0
        private static InvocationExpressionSyntax Merge(
            InvocationExpressionSyntax outerMostInvocation,
            MemberAccessExpressionSyntax innerMostWhereAccess,
            List <ExpressionSyntax> selectArguments,
            SemanticModel semanticModel)
        {
            var firstArgument = selectArguments[0];

            string                     parameterName;
            ParameterSyntax            firstParameter;
            IdentifierNameSyntax       firstParameterIdentifier;
            InvocationExpressionSyntax resultInvocation;

            if (firstArgument.IsKind(SyntaxKind.SimpleLambdaExpression))
            {
                var lambda = (SimpleLambdaExpressionSyntax)firstArgument;
                firstParameter           = lambda.Parameter;
                parameterName            = firstParameter.Identifier.Text;
                firstParameterIdentifier = SyntaxFactory.IdentifierName(firstParameter.Identifier);
                resultInvocation         = (InvocationExpressionSyntax)lambda.Body;
            }
            else
            {
                parameterName = NameHelper.GetLambdaParameterName(
                    outerMostInvocation.SpanStart,
                    semanticModel);

                var parameterIdentifier = SyntaxFactory
                                          .Identifier(parameterName)
                                          .WithAdditionalAnnotations(RenameAnnotation.Create());

                firstParameter = SyntaxFactory.Parameter(parameterIdentifier);

                firstParameterIdentifier = SyntaxFactory.IdentifierName(parameterIdentifier);

                resultInvocation = ExtendedSyntaxFactory.MakeInvocation(
                    firstArgument,
                    firstParameterIdentifier);
            }

            for (int i = 1; i < selectArguments.Count; ++i)
            {
                if (selectArguments[i].IsKind(SyntaxKind.SimpleLambdaExpression))
                {
                    var currentLambda        = (SimpleLambdaExpressionSyntax)selectArguments[i];
                    var currentParameter     = currentLambda.Parameter;
                    var currentParameterName = currentParameter.Identifier.Text;

                    var parameterSymbol = semanticModel.GetDeclaredSymbol(currentParameter);

                    var substituteRewriter = new SubstituteRewriter(
                        currentParameterName,
                        parameterSymbol,
                        semanticModel,
                        resultInvocation);

                    resultInvocation = (InvocationExpressionSyntax)currentLambda
                                       .Body
                                       .Accept(substituteRewriter);
                }
                else
                {
                    resultInvocation = ExtendedSyntaxFactory.MakeInvocation(
                        selectArguments[i],
                        resultInvocation);
                }
            }

            var newLambda = SyntaxFactory.SimpleLambdaExpression(
                firstParameter,
                resultInvocation);

            var newInvocation = ExtendedSyntaxFactory.MakeInvocation(
                innerMostWhereAccess,
                newLambda);

            return(newInvocation);
        }
Пример #3
0
        private static ForStatementSyntax ConvertToFor(
            ForEachStatementSyntax forEachStatement,
            SemanticModel semanticModel,
            string lengthMemberName)
        {
            var collectionExpression = forEachStatement.Expression;

            var collectionType = semanticModel.GetTypeInfo(collectionExpression).Type;

            string counterName = NameHelper.GetLoopCounterName(forEachStatement.Statement.SpanStart, semanticModel);

            var counterIdentifier = SyntaxFactory
                                    .IdentifierName(counterName)
                                    .WithAdditionalAnnotations(RenameAnnotation.Create());

            var initializer = SyntaxFactory.EqualsValueClause(
                SyntaxFactory.LiteralExpression(
                    SyntaxKind.NumericLiteralExpression,
                    SyntaxFactory.Literal(0)
                    )
                );

            var declarator = SyntaxFactory.VariableDeclarator(
                SyntaxFactory.Identifier(counterName)
                .WithAdditionalAnnotations(RenameAnnotation.Create()),
                null,
                initializer);

            var counterDeclaration = SyntaxFactory.VariableDeclaration(
                SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.IntKeyword)),
                SyntaxFactory.SingletonSeparatedList(declarator));

            var lengthAccess =
                SyntaxFactory.MemberAccessExpression(
                    SyntaxKind.SimpleMemberAccessExpression,
                    collectionExpression,
                    SyntaxFactory.IdentifierName(lengthMemberName)
                    );

            var condition = SyntaxFactory.BinaryExpression(
                SyntaxKind.LessThanExpression,
                counterIdentifier,
                lengthAccess);

            var counterIncrementor = SyntaxFactory.PostfixUnaryExpression(
                SyntaxKind.PostIncrementExpression,
                counterIdentifier);

            var elementAccess =
                SyntaxFactory.ElementAccessExpression(
                    collectionExpression,
                    SyntaxFactory.BracketedArgumentList(
                        SyntaxFactory.SingletonSeparatedList(
                            SyntaxFactory.Argument(counterIdentifier)
                            )
                        )
                    );

            var rewriter = new ForeachToForLoopBodyRewriter(
                elementAccess,
                forEachStatement.Identifier.Text,
                semanticModel.GetDeclaredSymbol(forEachStatement),
                semanticModel);

            var newLoopBody = (StatementSyntax)forEachStatement.Statement.Accept(rewriter);

            var forStatement = SyntaxFactory.ForStatement(
                counterDeclaration,
                SyntaxFactory.SeparatedList <ExpressionSyntax>(),
                condition,
                SyntaxFactory.SingletonSeparatedList <ExpressionSyntax>(counterIncrementor),
                newLoopBody);

            forStatement = forStatement
                           .WithTriviaFrom(forEachStatement)
                           .WithAdditionalAnnotations(Simplifier.Annotation);

            return(forStatement);
        }