private static SyntaxNode GetNegationOfConstantPattern( SyntaxNode pattern, SyntaxGenerator generator, SyntaxGeneratorInternal generatorInternal ) { var syntaxFacts = generatorInternal.SyntaxFacts; // If we have `is true/false` just swap that to be `is false/true`. var expression = syntaxFacts.GetExpressionOfConstantPattern(pattern); if (syntaxFacts.IsTrueLiteralExpression(expression)) { return(generatorInternal.ConstantPattern(generator.FalseLiteralExpression())); } if (syntaxFacts.IsFalseLiteralExpression(expression)) { return(generatorInternal.ConstantPattern(generator.TrueLiteralExpression())); } // Otherwise, just negate the entire pattern, we don't have anything else special we can do here. return(generatorInternal.NotPattern(pattern)); }
private static SyntaxNode GetNegationOfBinaryExpression( SyntaxNode expressionNode, SyntaxGenerator generator, SyntaxGeneratorInternal generatorInternal, SemanticModel semanticModel, CancellationToken cancellationToken) { var syntaxFacts = generatorInternal.SyntaxFacts; syntaxFacts.GetPartsOfBinaryExpression(expressionNode, out var leftOperand, out var operatorToken, out var rightOperand); var operation = semanticModel.GetOperation(expressionNode, cancellationToken); if (operation is not IBinaryOperation binaryOperation) { if (syntaxFacts.IsIsTypeExpression(expressionNode)) { // `is object` -> `is null` if (syntaxFacts.IsPredefinedType(rightOperand, PredefinedType.Object) && generatorInternal.SupportsPatterns(semanticModel.SyntaxTree.Options)) { return(generatorInternal.IsPatternExpression(leftOperand, operatorToken, generatorInternal.ConstantPattern(generator.NullLiteralExpression()))); } // `is y` -> `is not y` if (syntaxFacts.SupportsNotPattern(semanticModel.SyntaxTree.Options)) { return(generatorInternal.IsPatternExpression(leftOperand, operatorToken, generatorInternal.NotPattern(generatorInternal.TypePattern(rightOperand)))); } } // Apply the logical not operator if it is not a binary operation. return(generator.LogicalNotExpression(expressionNode)); } if (!s_negatedBinaryMap.TryGetValue(binaryOperation.OperatorKind, out var negatedKind)) { return(generator.LogicalNotExpression(expressionNode)); } if (binaryOperation.OperatorKind is BinaryOperatorKind.Or or BinaryOperatorKind.And or BinaryOperatorKind.ConditionalAnd or BinaryOperatorKind.ConditionalOr) { leftOperand = generator.Negate(generatorInternal, leftOperand, semanticModel, cancellationToken); rightOperand = generator.Negate(generatorInternal, rightOperand, semanticModel, cancellationToken); } var newBinaryExpressionSyntax = negatedKind is BinaryOperatorKind.Equals or BinaryOperatorKind.NotEquals ? generatorInternal.NegateEquality(generator, expressionNode, leftOperand, negatedKind, rightOperand) : NegateRelational(generator, binaryOperation, leftOperand, negatedKind, rightOperand); newBinaryExpressionSyntax = newBinaryExpressionSyntax.WithTriviaFrom(expressionNode); var newToken = syntaxFacts.GetOperatorTokenOfBinaryExpression(newBinaryExpressionSyntax); return(newBinaryExpressionSyntax.ReplaceToken( newToken, newToken.WithTriviaFrom(operatorToken))); }