1 | // |
---|
2 | // UnaryOperatorExpression.cs |
---|
3 | // |
---|
4 | // Author: |
---|
5 | // Mike Krüger <mkrueger@novell.com> |
---|
6 | // |
---|
7 | // Copyright (c) 2009 Novell, Inc (http://www.novell.com) |
---|
8 | // |
---|
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: |
---|
15 | // |
---|
16 | // The above copyright notice and this permission notice shall be included in |
---|
17 | // all copies or substantial portions of the Software. |
---|
18 | // |
---|
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 |
---|
25 | // THE SOFTWARE. |
---|
26 | |
---|
27 | using System; |
---|
28 | using System.Linq.Expressions; |
---|
29 | |
---|
30 | namespace ICSharpCode.NRefactory.CSharp |
---|
31 | { |
---|
32 | /// <summary> |
---|
33 | /// Operator Expression |
---|
34 | /// </summary> |
---|
35 | public class UnaryOperatorExpression : Expression |
---|
36 | { |
---|
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"); |
---|
46 | |
---|
47 | public UnaryOperatorExpression() |
---|
48 | { |
---|
49 | } |
---|
50 | |
---|
51 | public UnaryOperatorExpression(UnaryOperatorType op, Expression expression) |
---|
52 | { |
---|
53 | this.Operator = op; |
---|
54 | this.Expression = expression; |
---|
55 | } |
---|
56 | |
---|
57 | public UnaryOperatorType Operator { |
---|
58 | get; |
---|
59 | set; |
---|
60 | } |
---|
61 | |
---|
62 | public CSharpTokenNode OperatorToken { |
---|
63 | get { return GetChildByRole (GetOperatorRole (Operator)); } |
---|
64 | } |
---|
65 | |
---|
66 | static Expression NoUnaryExpressionError = new ErrorExpression ("No unary expression"); |
---|
67 | public Expression Expression { |
---|
68 | get { return GetChildByRole (Roles.Expression) ?? NoUnaryExpressionError; } |
---|
69 | set { SetChildByRole (Roles.Expression, value); } |
---|
70 | } |
---|
71 | |
---|
72 | public override void AcceptVisitor (IAstVisitor visitor) |
---|
73 | { |
---|
74 | visitor.VisitUnaryOperatorExpression (this); |
---|
75 | } |
---|
76 | |
---|
77 | public override T AcceptVisitor<T> (IAstVisitor<T> visitor) |
---|
78 | { |
---|
79 | return visitor.VisitUnaryOperatorExpression (this); |
---|
80 | } |
---|
81 | |
---|
82 | public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data) |
---|
83 | { |
---|
84 | return visitor.VisitUnaryOperatorExpression (this, data); |
---|
85 | } |
---|
86 | |
---|
87 | protected internal override bool DoMatch(AstNode other, PatternMatching.Match match) |
---|
88 | { |
---|
89 | UnaryOperatorExpression o = other as UnaryOperatorExpression; |
---|
90 | return o != null && (this.Operator == UnaryOperatorType.Any || this.Operator == o.Operator) |
---|
91 | && this.Expression.DoMatch(o.Expression, match); |
---|
92 | } |
---|
93 | |
---|
94 | public static TokenRole GetOperatorRole(UnaryOperatorType op) |
---|
95 | { |
---|
96 | switch (op) { |
---|
97 | case UnaryOperatorType.Not: |
---|
98 | return NotRole; |
---|
99 | case UnaryOperatorType.BitNot: |
---|
100 | return BitNotRole; |
---|
101 | case UnaryOperatorType.Minus: |
---|
102 | return MinusRole; |
---|
103 | case UnaryOperatorType.Plus: |
---|
104 | return PlusRole; |
---|
105 | case UnaryOperatorType.Increment: |
---|
106 | case UnaryOperatorType.PostIncrement: |
---|
107 | return IncrementRole; |
---|
108 | case UnaryOperatorType.PostDecrement: |
---|
109 | case UnaryOperatorType.Decrement: |
---|
110 | return DecrementRole; |
---|
111 | case UnaryOperatorType.Dereference: |
---|
112 | return DereferenceRole; |
---|
113 | case UnaryOperatorType.AddressOf: |
---|
114 | return AddressOfRole; |
---|
115 | case UnaryOperatorType.Await: |
---|
116 | return AwaitRole; |
---|
117 | default: |
---|
118 | throw new NotSupportedException("Invalid value for UnaryOperatorType"); |
---|
119 | } |
---|
120 | } |
---|
121 | |
---|
122 | public static ExpressionType GetLinqNodeType(UnaryOperatorType op, bool checkForOverflow) |
---|
123 | { |
---|
124 | switch (op) { |
---|
125 | case UnaryOperatorType.Not: |
---|
126 | return ExpressionType.Not; |
---|
127 | case UnaryOperatorType.BitNot: |
---|
128 | return ExpressionType.OnesComplement; |
---|
129 | case UnaryOperatorType.Minus: |
---|
130 | return checkForOverflow ? ExpressionType.NegateChecked : ExpressionType.Negate; |
---|
131 | case UnaryOperatorType.Plus: |
---|
132 | return ExpressionType.UnaryPlus; |
---|
133 | case UnaryOperatorType.Increment: |
---|
134 | return ExpressionType.PreIncrementAssign; |
---|
135 | case UnaryOperatorType.Decrement: |
---|
136 | return ExpressionType.PreDecrementAssign; |
---|
137 | case UnaryOperatorType.PostIncrement: |
---|
138 | return ExpressionType.PostIncrementAssign; |
---|
139 | case UnaryOperatorType.PostDecrement: |
---|
140 | return ExpressionType.PostDecrementAssign; |
---|
141 | case UnaryOperatorType.Dereference: |
---|
142 | case UnaryOperatorType.AddressOf: |
---|
143 | case UnaryOperatorType.Await: |
---|
144 | return ExpressionType.Extension; |
---|
145 | default: |
---|
146 | throw new NotSupportedException("Invalid value for UnaryOperatorType"); |
---|
147 | } |
---|
148 | } |
---|
149 | } |
---|
150 | |
---|
151 | public enum UnaryOperatorType |
---|
152 | { |
---|
153 | /// <summary> |
---|
154 | /// Any unary operator (used in pattern matching) |
---|
155 | /// </summary> |
---|
156 | Any, |
---|
157 | |
---|
158 | /// <summary>Logical not (!a)</summary> |
---|
159 | Not, |
---|
160 | /// <summary>Bitwise not (~a)</summary> |
---|
161 | BitNot, |
---|
162 | /// <summary>Unary minus (-a)</summary> |
---|
163 | Minus, |
---|
164 | /// <summary>Unary plus (+a)</summary> |
---|
165 | Plus, |
---|
166 | /// <summary>Pre increment (++a)</summary> |
---|
167 | Increment, |
---|
168 | /// <summary>Pre decrement (--a)</summary> |
---|
169 | Decrement, |
---|
170 | /// <summary>Post increment (a++)</summary> |
---|
171 | PostIncrement, |
---|
172 | /// <summary>Post decrement (a--)</summary> |
---|
173 | PostDecrement, |
---|
174 | /// <summary>Dereferencing (*a)</summary> |
---|
175 | Dereference, |
---|
176 | /// <summary>Get address (&a)</summary> |
---|
177 | AddressOf, |
---|
178 | /// <summary>C# 5.0 await</summary> |
---|
179 | Await |
---|
180 | } |
---|
181 | } |
---|