public static SyntaxNode Negate(
            this SyntaxGenerator generator,
            SyntaxGeneratorInternal generatorInternal,
            SyntaxNode expression,
            SemanticModel semanticModel,
            bool negateBinary,
            CancellationToken cancellationToken)
        {
            var syntaxFacts = generatorInternal.SyntaxFacts;

            if (syntaxFacts.IsParenthesizedExpression(expression))
            {
                return(generatorInternal.AddParentheses(
                           generator.Negate(
                               generatorInternal,
                               syntaxFacts.GetExpressionOfParenthesizedExpression(expression),
                               semanticModel,
                               negateBinary,
                               cancellationToken))
                       .WithTriviaFrom(expression));
            }
            if (negateBinary && syntaxFacts.IsBinaryExpression(expression))
            {
                return(GetNegationOfBinaryExpression(expression, generator, generatorInternal, semanticModel, cancellationToken));
            }
            else if (syntaxFacts.IsLiteralExpression(expression))
            {
                return(GetNegationOfLiteralExpression(expression, generator, semanticModel));
            }
            else if (syntaxFacts.IsLogicalNotExpression(expression))
            {
                return(GetNegationOfLogicalNotExpression(expression, syntaxFacts));
            }

            return(generator.LogicalNotExpression(expression));
        }
Exemplo n.º 2
0
        public static SyntaxNode Negate(
            this SyntaxGenerator generator,
            SyntaxGeneratorInternal generatorInternal,
            SyntaxNode expressionOrPattern,
            SemanticModel semanticModel,
            bool negateBinary,
            CancellationToken cancellationToken)
        {
            var syntaxFacts = generatorInternal.SyntaxFacts;

            if (syntaxFacts.IsParenthesizedExpression(expressionOrPattern))
            {
                return(generatorInternal.AddParentheses(
                           generator.Negate(
                               generatorInternal,
                               syntaxFacts.GetExpressionOfParenthesizedExpression(expressionOrPattern),
                               semanticModel,
                               negateBinary,
                               cancellationToken))
                       .WithTriviaFrom(expressionOrPattern));
            }

            if (negateBinary && syntaxFacts.IsBinaryExpression(expressionOrPattern))
            {
                return(GetNegationOfBinaryExpression(expressionOrPattern, generator, generatorInternal, semanticModel, cancellationToken));
            }

            if (syntaxFacts.IsLiteralExpression(expressionOrPattern))
            {
                return(GetNegationOfLiteralExpression(expressionOrPattern, generator, semanticModel));
            }

            if (syntaxFacts.IsLogicalNotExpression(expressionOrPattern))
            {
                return(GetNegationOfLogicalNotExpression(expressionOrPattern, syntaxFacts));
            }

            if (negateBinary && syntaxFacts.IsIsPatternExpression(expressionOrPattern))
            {
                return(GetNegationOfIsPatternExpression(expressionOrPattern, generator, generatorInternal, semanticModel, cancellationToken));
            }

            if (syntaxFacts.IsParenthesizedPattern(expressionOrPattern))
            {
                // Push the negation inside the parenthesized pattern.
                return(generatorInternal.AddParentheses(
                           generator.Negate(
                               generatorInternal,
                               syntaxFacts.GetPatternOfParenthesizedPattern(expressionOrPattern),
                               semanticModel,
                               negateBinary,
                               cancellationToken))
                       .WithTriviaFrom(expressionOrPattern));
            }

            if (negateBinary && syntaxFacts.IsBinaryPattern(expressionOrPattern))
            {
                return(GetNegationOfBinaryPattern(expressionOrPattern, generator, generatorInternal, semanticModel, cancellationToken));
            }

            if (syntaxFacts.IsConstantPattern(expressionOrPattern))
            {
                return(GetNegationOfConstantPattern(expressionOrPattern, generator, generatorInternal));
            }

            if (syntaxFacts.IsUnaryPattern(expressionOrPattern))
            {
                return(GetNegationOfUnaryPattern(expressionOrPattern, generatorInternal, syntaxFacts));
            }

            // TODO(cyrusn): We could support negating relational patterns in the future.  i.e.
            //
            //      not >= 0   ->    < 0

            return(syntaxFacts.IsAnyPattern(expressionOrPattern)
                ? generatorInternal.NotPattern(expressionOrPattern)
                : generator.LogicalNotExpression(expressionOrPattern));
        }