5
// Mike Krüger <mkrueger@novell.com>
7
// Copyright (c) 2011 Novell, Inc (http://www.novell.com)
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
10
// of this software and associated documentation files (the "Software"), to deal
11
// in the Software without restriction, including without limitation the rights
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
// copies of the Software, and to permit persons to whom the Software is
14
// furnished to do so, subject to the following conditions:
16
// The above copyright notice and this permission notice shall be included in
17
// all copies or substantial portions of the Software.
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27
using ICSharpCode.NRefactory.CSharp;
29
namespace ICSharpCode.NRefactory.CSharp
31
public static class CSharpUtil
33
public static Expression InvertCondition (Expression condition)
35
return InvertConditionInternal (condition.Clone ());
38
static Expression InvertConditionInternal (Expression condition)
40
if (condition is ParenthesizedExpression) {
41
((ParenthesizedExpression)condition).Expression = InvertCondition (((ParenthesizedExpression)condition).Expression);
45
if (condition is UnaryOperatorExpression) {
46
var uOp = (UnaryOperatorExpression)condition;
47
if (uOp.Operator == UnaryOperatorType.Not)
48
return uOp.Expression;
49
return new UnaryOperatorExpression (UnaryOperatorType.Not, uOp);
52
if (condition is BinaryOperatorExpression) {
53
var bOp = (BinaryOperatorExpression)condition;
54
switch (bOp.Operator) {
55
case BinaryOperatorType.GreaterThan:
56
bOp.Operator = BinaryOperatorType.LessThanOrEqual;
58
case BinaryOperatorType.GreaterThanOrEqual:
59
bOp.Operator = BinaryOperatorType.LessThan;
61
case BinaryOperatorType.Equality:
62
bOp.Operator = BinaryOperatorType.InEquality;
64
case BinaryOperatorType.InEquality:
65
bOp.Operator = BinaryOperatorType.Equality;
67
case BinaryOperatorType.LessThan:
68
bOp.Operator = BinaryOperatorType.GreaterThanOrEqual;
70
case BinaryOperatorType.LessThanOrEqual:
71
bOp.Operator = BinaryOperatorType.GreaterThan;
74
return new UnaryOperatorExpression (UnaryOperatorType.Not, new ParenthesizedExpression (condition));
77
if (condition is ConditionalExpression) {
78
var cEx = condition as ConditionalExpression;
79
cEx.Condition = InvertCondition (cEx.Condition);
82
if (condition is PrimitiveExpression) {
83
var pex = condition as PrimitiveExpression;
84
if (pex.Value is bool) {
85
return new PrimitiveExpression (!((bool)pex.Value));
89
return new UnaryOperatorExpression (UnaryOperatorType.Not, condition);