2
// AssignmentExpression.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
/// Left Operator= Right
35
public class AssignmentExpression : Expression
37
// reuse roles from BinaryOperatorExpression
38
public readonly static Role<Expression> LeftRole = BinaryOperatorExpression.LeftRole;
39
public readonly static Role<Expression> RightRole = BinaryOperatorExpression.RightRole;
41
public readonly static TokenRole AssignRole = new TokenRole ("=");
42
public readonly static TokenRole AddRole = new TokenRole ("+=");
43
public readonly static TokenRole SubtractRole = new TokenRole ("-=");
44
public readonly static TokenRole MultiplyRole = new TokenRole ("*=");
45
public readonly static TokenRole DivideRole = new TokenRole ("/=");
46
public readonly static TokenRole ModulusRole = new TokenRole ("%=");
47
public readonly static TokenRole ShiftLeftRole = new TokenRole ("<<=");
48
public readonly static TokenRole ShiftRightRole = new TokenRole (">>=");
49
public readonly static TokenRole BitwiseAndRole = new TokenRole ("&=");
50
public readonly static TokenRole BitwiseOrRole = new TokenRole ("|=");
51
public readonly static TokenRole ExclusiveOrRole = new TokenRole ("^=");
53
public AssignmentExpression()
57
public AssignmentExpression(Expression left, Expression right)
63
public AssignmentExpression(Expression left, AssignmentOperatorType op, Expression right)
70
public AssignmentOperatorType Operator {
75
public Expression Left {
76
get { return GetChildByRole (LeftRole); }
77
set { SetChildByRole(LeftRole, value); }
80
public CSharpTokenNode OperatorToken {
81
get { return GetChildByRole (GetOperatorRole(Operator)); }
84
public Expression Right {
85
get { return GetChildByRole (RightRole); }
86
set { SetChildByRole(RightRole, value); }
89
public override void AcceptVisitor (IAstVisitor visitor)
91
visitor.VisitAssignmentExpression (this);
94
public override T AcceptVisitor<T> (IAstVisitor<T> visitor)
96
return visitor.VisitAssignmentExpression (this);
99
public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data)
101
return visitor.VisitAssignmentExpression (this, data);
104
protected internal override bool DoMatch(AstNode other, PatternMatching.Match match)
106
AssignmentExpression o = other as AssignmentExpression;
107
return o != null && (this.Operator == AssignmentOperatorType.Any || this.Operator == o.Operator)
108
&& this.Left.DoMatch(o.Left, match) && this.Right.DoMatch(o.Right, match);
111
public static TokenRole GetOperatorRole(AssignmentOperatorType op)
114
case AssignmentOperatorType.Assign:
116
case AssignmentOperatorType.Add:
118
case AssignmentOperatorType.Subtract:
120
case AssignmentOperatorType.Multiply:
122
case AssignmentOperatorType.Divide:
124
case AssignmentOperatorType.Modulus:
126
case AssignmentOperatorType.ShiftLeft:
127
return ShiftLeftRole;
128
case AssignmentOperatorType.ShiftRight:
129
return ShiftRightRole;
130
case AssignmentOperatorType.BitwiseAnd:
131
return BitwiseAndRole;
132
case AssignmentOperatorType.BitwiseOr:
133
return BitwiseOrRole;
134
case AssignmentOperatorType.ExclusiveOr:
135
return ExclusiveOrRole;
137
throw new NotSupportedException("Invalid value for AssignmentOperatorType");
142
/// Gets the binary operator for the specified compound assignment operator.
143
/// Returns null if 'op' is not a compound assignment.
145
public static BinaryOperatorType? GetCorrespondingBinaryOperator(AssignmentOperatorType op)
148
case AssignmentOperatorType.Assign:
150
case AssignmentOperatorType.Add:
151
return BinaryOperatorType.Add;
152
case AssignmentOperatorType.Subtract:
153
return BinaryOperatorType.Subtract;
154
case AssignmentOperatorType.Multiply:
155
return BinaryOperatorType.Multiply;
156
case AssignmentOperatorType.Divide:
157
return BinaryOperatorType.Divide;
158
case AssignmentOperatorType.Modulus:
159
return BinaryOperatorType.Modulus;
160
case AssignmentOperatorType.ShiftLeft:
161
return BinaryOperatorType.ShiftLeft;
162
case AssignmentOperatorType.ShiftRight:
163
return BinaryOperatorType.ShiftRight;
164
case AssignmentOperatorType.BitwiseAnd:
165
return BinaryOperatorType.BitwiseAnd;
166
case AssignmentOperatorType.BitwiseOr:
167
return BinaryOperatorType.BitwiseOr;
168
case AssignmentOperatorType.ExclusiveOr:
169
return BinaryOperatorType.ExclusiveOr;
171
throw new NotSupportedException("Invalid value for AssignmentOperatorType");
175
public static ExpressionType GetLinqNodeType(AssignmentOperatorType op, bool checkForOverflow)
178
case AssignmentOperatorType.Assign:
179
return ExpressionType.Assign;
180
case AssignmentOperatorType.Add:
181
return checkForOverflow ? ExpressionType.AddAssignChecked : ExpressionType.AddAssign;
182
case AssignmentOperatorType.Subtract:
183
return checkForOverflow ? ExpressionType.SubtractAssignChecked : ExpressionType.SubtractAssign;
184
case AssignmentOperatorType.Multiply:
185
return checkForOverflow ? ExpressionType.MultiplyAssignChecked : ExpressionType.MultiplyAssign;
186
case AssignmentOperatorType.Divide:
187
return ExpressionType.DivideAssign;
188
case AssignmentOperatorType.Modulus:
189
return ExpressionType.ModuloAssign;
190
case AssignmentOperatorType.ShiftLeft:
191
return ExpressionType.LeftShiftAssign;
192
case AssignmentOperatorType.ShiftRight:
193
return ExpressionType.RightShiftAssign;
194
case AssignmentOperatorType.BitwiseAnd:
195
return ExpressionType.AndAssign;
196
case AssignmentOperatorType.BitwiseOr:
197
return ExpressionType.OrAssign;
198
case AssignmentOperatorType.ExclusiveOr:
199
return ExpressionType.ExclusiveOrAssign;
201
throw new NotSupportedException("Invalid value for AssignmentOperatorType");
206
public enum AssignmentOperatorType
208
/// <summary>left = right</summary>
211
/// <summary>left += right</summary>
213
/// <summary>left -= right</summary>
215
/// <summary>left *= right</summary>
217
/// <summary>left /= right</summary>
219
/// <summary>left %= right</summary>
222
/// <summary>left <<= right</summary>
224
/// <summary>left >>= right</summary>
227
/// <summary>left &= right</summary>
229
/// <summary>left |= right</summary>
231
/// <summary>left ^= right</summary>
234
/// <summary>Any operator (for pattern matching)</summary>