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

« back to all changes in this revision

Viewing changes to external/nrefactory/ICSharpCode.NRefactory.CSharp/Ast/Expressions/BinaryOperatorExpression.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
// BinaryOperatorExpression.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 BinaryOperatorExpression : Expression
 
36
        {
 
37
                public readonly static TokenRole BitwiseAndRole = new TokenRole ("&");
 
38
                public readonly static TokenRole BitwiseOrRole = new TokenRole ("|");
 
39
                public readonly static TokenRole ConditionalAndRole = new TokenRole ("&&");
 
40
                public readonly static TokenRole ConditionalOrRole = new TokenRole ("||");
 
41
                public readonly static TokenRole ExclusiveOrRole = new TokenRole ("^");
 
42
                public readonly static TokenRole GreaterThanRole = new TokenRole (">");
 
43
                public readonly static TokenRole GreaterThanOrEqualRole = new TokenRole (">=");
 
44
                public readonly static TokenRole EqualityRole = new TokenRole ("==");
 
45
                public readonly static TokenRole InEqualityRole = new TokenRole ("!=");
 
46
                public readonly static TokenRole LessThanRole = new TokenRole ("<");
 
47
                public readonly static TokenRole LessThanOrEqualRole = new TokenRole ("<=");
 
48
                public readonly static TokenRole AddRole = new TokenRole ("+");
 
49
                public readonly static TokenRole SubtractRole = new TokenRole ("-");
 
50
                public readonly static TokenRole MultiplyRole = new TokenRole ("*");
 
51
                public readonly static TokenRole DivideRole = new TokenRole ("/");
 
52
                public readonly static TokenRole ModulusRole = new TokenRole ("%");
 
53
                public readonly static TokenRole ShiftLeftRole = new TokenRole ("<<");
 
54
                public readonly static TokenRole ShiftRightRole = new TokenRole (">>");
 
55
                public readonly static TokenRole NullCoalescingRole = new TokenRole ("??");
 
56
                
 
57
                public readonly static Role<Expression> LeftRole = new Role<Expression>("Left", Expression.Null);
 
58
                public readonly static Role<Expression> RightRole = new Role<Expression>("Right", Expression.Null);
 
59
                
 
60
                public BinaryOperatorExpression()
 
61
                {
 
62
                }
 
63
                
 
64
                public BinaryOperatorExpression(Expression left, BinaryOperatorType op, Expression right)
 
65
                {
 
66
                        this.Left = left;
 
67
                        this.Operator = op;
 
68
                        this.Right = right;
 
69
                }
 
70
                
 
71
                public BinaryOperatorType Operator {
 
72
                        get;
 
73
                        set;
 
74
                }
 
75
                
 
76
                public Expression Left {
 
77
                        get { return GetChildByRole (LeftRole); }
 
78
                        set { SetChildByRole(LeftRole, value); }
 
79
                }
 
80
                
 
81
                public CSharpTokenNode OperatorToken {
 
82
                        get { return GetChildByRole (GetOperatorRole (Operator)); }
 
83
                }
 
84
                
 
85
                public Expression Right {
 
86
                        get { return GetChildByRole (RightRole); }
 
87
                        set { SetChildByRole (RightRole, value); }
 
88
                }
 
89
                
 
90
                public override void AcceptVisitor (IAstVisitor visitor)
 
91
                {
 
92
                        visitor.VisitBinaryOperatorExpression (this);
 
93
                }
 
94
                        
 
95
                public override T AcceptVisitor<T> (IAstVisitor<T> visitor)
 
96
                {
 
97
                        return visitor.VisitBinaryOperatorExpression (this);
 
98
                }
 
99
                
 
100
                public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data)
 
101
                {
 
102
                        return visitor.VisitBinaryOperatorExpression (this, data);
 
103
                }
 
104
                
 
105
                protected internal override bool DoMatch(AstNode other, PatternMatching.Match match)
 
106
                {
 
107
                        BinaryOperatorExpression o = other as BinaryOperatorExpression;
 
108
                        return o != null && (this.Operator == BinaryOperatorType.Any || this.Operator == o.Operator)
 
109
                                && this.Left.DoMatch(o.Left, match) && this.Right.DoMatch(o.Right, match);
 
110
                }
 
111
                
 
112
                public static TokenRole GetOperatorRole (BinaryOperatorType op)
 
113
                {
 
114
                        switch (op) {
 
115
                                case BinaryOperatorType.BitwiseAnd:
 
116
                                        return BitwiseAndRole;
 
117
                                case BinaryOperatorType.BitwiseOr:
 
118
                                        return BitwiseOrRole;
 
119
                                case BinaryOperatorType.ConditionalAnd:
 
120
                                        return ConditionalAndRole;
 
121
                                case BinaryOperatorType.ConditionalOr:
 
122
                                        return ConditionalOrRole;
 
123
                                case BinaryOperatorType.ExclusiveOr:
 
124
                                        return ExclusiveOrRole;
 
125
                                case BinaryOperatorType.GreaterThan:
 
126
                                        return GreaterThanRole;
 
127
                                case BinaryOperatorType.GreaterThanOrEqual:
 
128
                                        return GreaterThanOrEqualRole;
 
129
                                case BinaryOperatorType.Equality:
 
130
                                        return EqualityRole;
 
131
                                case BinaryOperatorType.InEquality:
 
132
                                        return InEqualityRole;
 
133
                                case BinaryOperatorType.LessThan:
 
134
                                        return LessThanRole;
 
135
                                case BinaryOperatorType.LessThanOrEqual:
 
136
                                        return LessThanOrEqualRole;
 
137
                                case BinaryOperatorType.Add:
 
138
                                        return AddRole;
 
139
                                case BinaryOperatorType.Subtract:
 
140
                                        return SubtractRole;
 
141
                                case BinaryOperatorType.Multiply:
 
142
                                        return MultiplyRole;
 
143
                                case BinaryOperatorType.Divide:
 
144
                                        return DivideRole;
 
145
                                case BinaryOperatorType.Modulus:
 
146
                                        return ModulusRole;
 
147
                                case BinaryOperatorType.ShiftLeft:
 
148
                                        return ShiftLeftRole;
 
149
                                case BinaryOperatorType.ShiftRight:
 
150
                                        return ShiftRightRole;
 
151
                                case BinaryOperatorType.NullCoalescing:
 
152
                                        return NullCoalescingRole;
 
153
                                default:
 
154
                                        throw new NotSupportedException("Invalid value for BinaryOperatorType");
 
155
                        }
 
156
                }
 
157
                
 
158
                public static ExpressionType GetLinqNodeType(BinaryOperatorType op, bool checkForOverflow)
 
159
                {
 
160
                        switch (op) {
 
161
                                case BinaryOperatorType.BitwiseAnd:
 
162
                                        return ExpressionType.And;
 
163
                                case BinaryOperatorType.BitwiseOr:
 
164
                                        return ExpressionType.Or;
 
165
                                case BinaryOperatorType.ConditionalAnd:
 
166
                                        return ExpressionType.AndAlso;
 
167
                                case BinaryOperatorType.ConditionalOr:
 
168
                                        return ExpressionType.OrElse;
 
169
                                case BinaryOperatorType.ExclusiveOr:
 
170
                                        return ExpressionType.ExclusiveOr;
 
171
                                case BinaryOperatorType.GreaterThan:
 
172
                                        return ExpressionType.GreaterThan;
 
173
                                case BinaryOperatorType.GreaterThanOrEqual:
 
174
                                        return ExpressionType.GreaterThanOrEqual;
 
175
                                case BinaryOperatorType.Equality:
 
176
                                        return ExpressionType.Equal;
 
177
                                case BinaryOperatorType.InEquality:
 
178
                                        return ExpressionType.NotEqual;
 
179
                                case BinaryOperatorType.LessThan:
 
180
                                        return ExpressionType.LessThan;
 
181
                                case BinaryOperatorType.LessThanOrEqual:
 
182
                                        return ExpressionType.LessThanOrEqual;
 
183
                                case BinaryOperatorType.Add:
 
184
                                        return checkForOverflow ? ExpressionType.AddChecked : ExpressionType.Add;
 
185
                                case BinaryOperatorType.Subtract:
 
186
                                        return checkForOverflow ? ExpressionType.SubtractChecked : ExpressionType.Subtract;
 
187
                                case BinaryOperatorType.Multiply:
 
188
                                        return checkForOverflow ? ExpressionType.MultiplyChecked : ExpressionType.Multiply;
 
189
                                case BinaryOperatorType.Divide:
 
190
                                        return ExpressionType.Divide;
 
191
                                case BinaryOperatorType.Modulus:
 
192
                                        return ExpressionType.Modulo;
 
193
                                case BinaryOperatorType.ShiftLeft:
 
194
                                        return ExpressionType.LeftShift;
 
195
                                case BinaryOperatorType.ShiftRight:
 
196
                                        return ExpressionType.RightShift;
 
197
                                case BinaryOperatorType.NullCoalescing:
 
198
                                        return ExpressionType.Coalesce;
 
199
                                default:
 
200
                                        throw new NotSupportedException("Invalid value for BinaryOperatorType");
 
201
                        }
 
202
                }
 
203
        }
 
204
        
 
205
        public enum BinaryOperatorType
 
206
        {
 
207
                /// <summary>
 
208
                /// Any binary operator (used in pattern matching)
 
209
                /// </summary>
 
210
                Any,
 
211
                
 
212
                // We avoid 'logical or' on purpose, because it's not clear if that refers to the bitwise
 
213
                // or to the short-circuiting (conditional) operator:
 
214
                // MCS and old NRefactory used bitwise='|', logical='||'
 
215
                // but the C# spec uses logical='|', conditional='||'
 
216
                /// <summary>left &amp; right</summary>
 
217
                BitwiseAnd,
 
218
                /// <summary>left | right</summary>
 
219
                BitwiseOr,
 
220
                /// <summary>left &amp;&amp; right</summary>
 
221
                ConditionalAnd,
 
222
                /// <summary>left || right</summary>
 
223
                ConditionalOr,
 
224
                /// <summary>left ^ right</summary>
 
225
                ExclusiveOr,
 
226
                
 
227
                /// <summary>left &gt; right</summary>
 
228
                GreaterThan,
 
229
                /// <summary>left &gt;= right</summary>
 
230
                GreaterThanOrEqual,
 
231
                /// <summary>left == right</summary>
 
232
                Equality,
 
233
                /// <summary>left != right</summary>
 
234
                InEquality,
 
235
                /// <summary>left &lt; right</summary>
 
236
                LessThan,
 
237
                /// <summary>left &lt;= right</summary>
 
238
                LessThanOrEqual,
 
239
                
 
240
                /// <summary>left + right</summary>
 
241
                Add,
 
242
                /// <summary>left - right</summary>
 
243
                Subtract,
 
244
                /// <summary>left * right</summary>
 
245
                Multiply,
 
246
                /// <summary>left / right</summary>
 
247
                Divide,
 
248
                /// <summary>left % right</summary>
 
249
                Modulus,
 
250
                
 
251
                /// <summary>left &lt;&lt; right</summary>
 
252
                ShiftLeft,
 
253
                /// <summary>left &gt;&gt; right</summary>
 
254
                ShiftRight,
 
255
                
 
256
                /// <summary>left ?? right</summary>
 
257
                NullCoalescing
 
258
        }
 
259
}