2
// UnaryOperatorExpression.cs
5
// Mike Krüger <mkrueger@novell.com>
7
// Copyright (c) 2009 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
28
using System.Linq.Expressions;
30
namespace ICSharpCode.NRefactory.CSharp
33
/// Operator Expression
35
public class UnaryOperatorExpression : Expression
37
public readonly static TokenRole NotRole = new TokenRole ("!");
38
public readonly static TokenRole BitNotRole = new TokenRole ("~");
39
public readonly static TokenRole MinusRole = new TokenRole ("-");
40
public readonly static TokenRole PlusRole = new TokenRole ("+");
41
public readonly static TokenRole IncrementRole = new TokenRole ("++");
42
public readonly static TokenRole DecrementRole = new TokenRole ("--");
43
public readonly static TokenRole DereferenceRole = new TokenRole ("*");
44
public readonly static TokenRole AddressOfRole = new TokenRole ("&");
45
public readonly static TokenRole AwaitRole = new TokenRole ("await");
47
public UnaryOperatorExpression()
51
public UnaryOperatorExpression(UnaryOperatorType op, Expression expression)
54
this.Expression = expression;
57
public UnaryOperatorType Operator {
62
public CSharpTokenNode OperatorToken {
63
get { return GetChildByRole (GetOperatorRole (Operator)); }
66
public Expression Expression {
67
get { return GetChildByRole (Roles.Expression); }
68
set { SetChildByRole (Roles.Expression, value); }
71
public override void AcceptVisitor (IAstVisitor visitor)
73
visitor.VisitUnaryOperatorExpression (this);
76
public override T AcceptVisitor<T> (IAstVisitor<T> visitor)
78
return visitor.VisitUnaryOperatorExpression (this);
81
public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data)
83
return visitor.VisitUnaryOperatorExpression (this, data);
86
protected internal override bool DoMatch(AstNode other, PatternMatching.Match match)
88
UnaryOperatorExpression o = other as UnaryOperatorExpression;
89
return o != null && (this.Operator == UnaryOperatorType.Any || this.Operator == o.Operator)
90
&& this.Expression.DoMatch(o.Expression, match);
93
public static TokenRole GetOperatorRole(UnaryOperatorType op)
96
case UnaryOperatorType.Not:
98
case UnaryOperatorType.BitNot:
100
case UnaryOperatorType.Minus:
102
case UnaryOperatorType.Plus:
104
case UnaryOperatorType.Increment:
105
case UnaryOperatorType.PostIncrement:
106
return IncrementRole;
107
case UnaryOperatorType.PostDecrement:
108
case UnaryOperatorType.Decrement:
109
return DecrementRole;
110
case UnaryOperatorType.Dereference:
111
return DereferenceRole;
112
case UnaryOperatorType.AddressOf:
113
return AddressOfRole;
114
case UnaryOperatorType.Await:
117
throw new NotSupportedException("Invalid value for UnaryOperatorType");
121
public static ExpressionType GetLinqNodeType(UnaryOperatorType op, bool checkForOverflow)
124
case UnaryOperatorType.Not:
125
return ExpressionType.Not;
126
case UnaryOperatorType.BitNot:
127
return ExpressionType.OnesComplement;
128
case UnaryOperatorType.Minus:
129
return checkForOverflow ? ExpressionType.NegateChecked : ExpressionType.Negate;
130
case UnaryOperatorType.Plus:
131
return ExpressionType.UnaryPlus;
132
case UnaryOperatorType.Increment:
133
return ExpressionType.PreIncrementAssign;
134
case UnaryOperatorType.Decrement:
135
return ExpressionType.PreDecrementAssign;
136
case UnaryOperatorType.PostIncrement:
137
return ExpressionType.PostIncrementAssign;
138
case UnaryOperatorType.PostDecrement:
139
return ExpressionType.PostDecrementAssign;
140
case UnaryOperatorType.Dereference:
141
case UnaryOperatorType.AddressOf:
142
case UnaryOperatorType.Await:
143
return ExpressionType.Extension;
145
throw new NotSupportedException("Invalid value for UnaryOperatorType");
150
public enum UnaryOperatorType
153
/// Any unary operator (used in pattern matching)
157
/// <summary>Logical not (!a)</summary>
159
/// <summary>Bitwise not (~a)</summary>
161
/// <summary>Unary minus (-a)</summary>
163
/// <summary>Unary plus (+a)</summary>
165
/// <summary>Pre increment (++a)</summary>
167
/// <summary>Pre decrement (--a)</summary>
169
/// <summary>Post increment (a++)</summary>
171
/// <summary>Post decrement (a--)</summary>
173
/// <summary>Dereferencing (*a)</summary>
175
/// <summary>Get address (&a)</summary>
177
/// <summary>C# 5.0 await</summary>