~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/nrefactory/ICSharpCode.NRefactory.CSharp/Ast/Expressions/AssignmentExpression.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
ļ»æ// 
 
2
// AssignmentExpression.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
        /// Left Operator= Right
 
34
        /// </summary>
 
35
        public class AssignmentExpression : Expression
 
36
        {
 
37
                // reuse roles from BinaryOperatorExpression
 
38
                public readonly static Role<Expression> LeftRole = BinaryOperatorExpression.LeftRole;
 
39
                public readonly static Role<Expression> RightRole = BinaryOperatorExpression.RightRole;
 
40
                
 
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 ("^=");
 
52
                
 
53
                public AssignmentExpression()
 
54
                {
 
55
                }
 
56
                
 
57
                public AssignmentExpression(Expression left, Expression right)
 
58
                {
 
59
                        this.Left = left;
 
60
                        this.Right = right;
 
61
                }
 
62
                
 
63
                public AssignmentExpression(Expression left, AssignmentOperatorType op, Expression right)
 
64
                {
 
65
                        this.Left = left;
 
66
                        this.Operator = op;
 
67
                        this.Right = right;
 
68
                }
 
69
                
 
70
                public AssignmentOperatorType Operator {
 
71
                        get;
 
72
                        set;
 
73
                }
 
74
                
 
75
                public Expression Left {
 
76
                        get { return GetChildByRole (LeftRole); }
 
77
                        set { SetChildByRole(LeftRole, value); }
 
78
                }
 
79
                
 
80
                public CSharpTokenNode OperatorToken {
 
81
                        get { return GetChildByRole (GetOperatorRole(Operator)); }
 
82
                }
 
83
                
 
84
                public Expression Right {
 
85
                        get { return GetChildByRole (RightRole); }
 
86
                        set { SetChildByRole(RightRole, value); }
 
87
                }
 
88
                
 
89
                public override void AcceptVisitor (IAstVisitor visitor)
 
90
                {
 
91
                        visitor.VisitAssignmentExpression (this);
 
92
                }
 
93
                        
 
94
                public override T AcceptVisitor<T> (IAstVisitor<T> visitor)
 
95
                {
 
96
                        return visitor.VisitAssignmentExpression (this);
 
97
                }
 
98
                
 
99
                public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data)
 
100
                {
 
101
                        return visitor.VisitAssignmentExpression (this, data);
 
102
                }
 
103
                
 
104
                protected internal override bool DoMatch(AstNode other, PatternMatching.Match match)
 
105
                {
 
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);
 
109
                }
 
110
                
 
111
                public static TokenRole GetOperatorRole(AssignmentOperatorType op)
 
112
                {
 
113
                        switch (op) {
 
114
                                case AssignmentOperatorType.Assign:
 
115
                                        return AssignRole;
 
116
                                case AssignmentOperatorType.Add:
 
117
                                        return AddRole;
 
118
                                case AssignmentOperatorType.Subtract:
 
119
                                        return SubtractRole;
 
120
                                case AssignmentOperatorType.Multiply:
 
121
                                        return MultiplyRole;
 
122
                                case AssignmentOperatorType.Divide:
 
123
                                        return DivideRole;
 
124
                                case AssignmentOperatorType.Modulus:
 
125
                                        return ModulusRole;
 
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;
 
136
                                default:
 
137
                                        throw new NotSupportedException("Invalid value for AssignmentOperatorType");
 
138
                        }
 
139
                }
 
140
                
 
141
                /// <summary>
 
142
                /// Gets the binary operator for the specified compound assignment operator.
 
143
                /// Returns null if 'op' is not a compound assignment.
 
144
                /// </summary>
 
145
                public static BinaryOperatorType? GetCorrespondingBinaryOperator(AssignmentOperatorType op)
 
146
                {
 
147
                        switch (op) {
 
148
                                case AssignmentOperatorType.Assign:
 
149
                                        return null;
 
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;
 
170
                                default:
 
171
                                        throw new NotSupportedException("Invalid value for AssignmentOperatorType");
 
172
                        }
 
173
                }
 
174
                
 
175
                public static ExpressionType GetLinqNodeType(AssignmentOperatorType op, bool checkForOverflow)
 
176
                {
 
177
                        switch (op) {
 
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;
 
200
                                default:
 
201
                                        throw new NotSupportedException("Invalid value for AssignmentOperatorType");
 
202
                        }
 
203
                }
 
204
        }
 
205
        
 
206
        public enum AssignmentOperatorType
 
207
        {
 
208
                /// <summary>left = right</summary>
 
209
                Assign,
 
210
                
 
211
                /// <summary>left += right</summary>
 
212
                Add,
 
213
                /// <summary>left -= right</summary>
 
214
                Subtract,
 
215
                /// <summary>left *= right</summary>
 
216
                Multiply,
 
217
                /// <summary>left /= right</summary>
 
218
                Divide,
 
219
                /// <summary>left %= right</summary>
 
220
                Modulus,
 
221
                
 
222
                /// <summary>left <<= right</summary>
 
223
                ShiftLeft,
 
224
                /// <summary>left >>= right</summary>
 
225
                ShiftRight,
 
226
                
 
227
                /// <summary>left &= right</summary>
 
228
                BitwiseAnd,
 
229
                /// <summary>left |= right</summary>
 
230
                BitwiseOr,
 
231
                /// <summary>left ^= right</summary>
 
232
                ExclusiveOr,
 
233
                
 
234
                /// <summary>Any operator (for pattern matching)</summary>
 
235
                Any
 
236
        }
 
237
}