Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2389-EpsLexicase/HeuristicLab.ExtLibs/HeuristicLab.NRefactory/5.5.0/NRefactory.CSharp-5.5.0/Ast/Expressions/BinaryOperatorExpression.cs

Last change on this file was 11700, checked in by jkarder, 10 years ago

#2077: created branch and added first version

File size: 11.1 KB
Line 
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
27using System;
28using System.Linq.Expressions;
29using System.Collections.Generic;
30
31namespace ICSharpCode.NRefactory.CSharp
32{
33  /// <summary>
34  /// Left Operator Right
35  /// </summary>
36  public class BinaryOperatorExpression : Expression
37  {
38    public readonly static TokenRole BitwiseAndRole = new TokenRole ("&");
39    public readonly static TokenRole BitwiseOrRole = new TokenRole ("|");
40    public readonly static TokenRole ConditionalAndRole = new TokenRole ("&&");
41    public readonly static TokenRole ConditionalOrRole = new TokenRole ("||");
42    public readonly static TokenRole ExclusiveOrRole = new TokenRole ("^");
43    public readonly static TokenRole GreaterThanRole = new TokenRole (">");
44    public readonly static TokenRole GreaterThanOrEqualRole = new TokenRole (">=");
45    public readonly static TokenRole EqualityRole = new TokenRole ("==");
46    public readonly static TokenRole InEqualityRole = new TokenRole ("!=");
47    public readonly static TokenRole LessThanRole = new TokenRole ("<");
48    public readonly static TokenRole LessThanOrEqualRole = new TokenRole ("<=");
49    public readonly static TokenRole AddRole = new TokenRole ("+");
50    public readonly static TokenRole SubtractRole = new TokenRole ("-");
51    public readonly static TokenRole MultiplyRole = new TokenRole ("*");
52    public readonly static TokenRole DivideRole = new TokenRole ("/");
53    public readonly static TokenRole ModulusRole = new TokenRole ("%");
54    public readonly static TokenRole ShiftLeftRole = new TokenRole ("<<");
55    public readonly static TokenRole ShiftRightRole = new TokenRole (">>");
56    public readonly static TokenRole NullCoalescingRole = new TokenRole ("??");
57   
58    public readonly static Role<Expression> LeftRole = new Role<Expression>("Left", Expression.Null);
59    public readonly static Role<Expression> RightRole = new Role<Expression>("Right", Expression.Null);
60   
61    public BinaryOperatorExpression()
62    {
63    }
64   
65    public BinaryOperatorExpression(Expression left, BinaryOperatorType op, Expression right)
66    {
67      this.Left = left;
68      this.Operator = op;
69      this.Right = right;
70    }
71   
72    public BinaryOperatorType Operator {
73      get;
74      set;
75    }
76   
77    public Expression Left {
78      get { return GetChildByRole (LeftRole); }
79      set { SetChildByRole(LeftRole, value); }
80    }
81   
82    public CSharpTokenNode OperatorToken {
83      get { return GetChildByRole (GetOperatorRole (Operator)); }
84    }
85   
86    public Expression Right {
87      get { return GetChildByRole (RightRole); }
88      set { SetChildByRole (RightRole, value); }
89    }
90   
91    public override void AcceptVisitor (IAstVisitor visitor)
92    {
93      visitor.VisitBinaryOperatorExpression (this);
94    }
95     
96    public override T AcceptVisitor<T> (IAstVisitor<T> visitor)
97    {
98      return visitor.VisitBinaryOperatorExpression (this);
99    }
100   
101    public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data)
102    {
103      return visitor.VisitBinaryOperatorExpression (this, data);
104    }
105   
106    protected internal override bool DoMatch(AstNode other, PatternMatching.Match match)
107    {
108      BinaryOperatorExpression o = other as BinaryOperatorExpression;
109      return o != null && (this.Operator == BinaryOperatorType.Any || this.Operator == o.Operator)
110        && this.Left.DoMatch(o.Left, match) && this.Right.DoMatch(o.Right, match);
111    }
112   
113    public static TokenRole GetOperatorRole (BinaryOperatorType op)
114    {
115      switch (op) {
116        case BinaryOperatorType.BitwiseAnd:
117          return BitwiseAndRole;
118        case BinaryOperatorType.BitwiseOr:
119          return BitwiseOrRole;
120        case BinaryOperatorType.ConditionalAnd:
121          return ConditionalAndRole;
122        case BinaryOperatorType.ConditionalOr:
123          return ConditionalOrRole;
124        case BinaryOperatorType.ExclusiveOr:
125          return ExclusiveOrRole;
126        case BinaryOperatorType.GreaterThan:
127          return GreaterThanRole;
128        case BinaryOperatorType.GreaterThanOrEqual:
129          return GreaterThanOrEqualRole;
130        case BinaryOperatorType.Equality:
131          return EqualityRole;
132        case BinaryOperatorType.InEquality:
133          return InEqualityRole;
134        case BinaryOperatorType.LessThan:
135          return LessThanRole;
136        case BinaryOperatorType.LessThanOrEqual:
137          return LessThanOrEqualRole;
138        case BinaryOperatorType.Add:
139          return AddRole;
140        case BinaryOperatorType.Subtract:
141          return SubtractRole;
142        case BinaryOperatorType.Multiply:
143          return MultiplyRole;
144        case BinaryOperatorType.Divide:
145          return DivideRole;
146        case BinaryOperatorType.Modulus:
147          return ModulusRole;
148        case BinaryOperatorType.ShiftLeft:
149          return ShiftLeftRole;
150        case BinaryOperatorType.ShiftRight:
151          return ShiftRightRole;
152        case BinaryOperatorType.NullCoalescing:
153          return NullCoalescingRole;
154        default:
155          throw new NotSupportedException("Invalid value for BinaryOperatorType");
156      }
157    }
158   
159    public static ExpressionType GetLinqNodeType(BinaryOperatorType op, bool checkForOverflow)
160    {
161      switch (op) {
162        case BinaryOperatorType.BitwiseAnd:
163          return ExpressionType.And;
164        case BinaryOperatorType.BitwiseOr:
165          return ExpressionType.Or;
166        case BinaryOperatorType.ConditionalAnd:
167          return ExpressionType.AndAlso;
168        case BinaryOperatorType.ConditionalOr:
169          return ExpressionType.OrElse;
170        case BinaryOperatorType.ExclusiveOr:
171          return ExpressionType.ExclusiveOr;
172        case BinaryOperatorType.GreaterThan:
173          return ExpressionType.GreaterThan;
174        case BinaryOperatorType.GreaterThanOrEqual:
175          return ExpressionType.GreaterThanOrEqual;
176        case BinaryOperatorType.Equality:
177          return ExpressionType.Equal;
178        case BinaryOperatorType.InEquality:
179          return ExpressionType.NotEqual;
180        case BinaryOperatorType.LessThan:
181          return ExpressionType.LessThan;
182        case BinaryOperatorType.LessThanOrEqual:
183          return ExpressionType.LessThanOrEqual;
184        case BinaryOperatorType.Add:
185          return checkForOverflow ? ExpressionType.AddChecked : ExpressionType.Add;
186        case BinaryOperatorType.Subtract:
187          return checkForOverflow ? ExpressionType.SubtractChecked : ExpressionType.Subtract;
188        case BinaryOperatorType.Multiply:
189          return checkForOverflow ? ExpressionType.MultiplyChecked : ExpressionType.Multiply;
190        case BinaryOperatorType.Divide:
191          return ExpressionType.Divide;
192        case BinaryOperatorType.Modulus:
193          return ExpressionType.Modulo;
194        case BinaryOperatorType.ShiftLeft:
195          return ExpressionType.LeftShift;
196        case BinaryOperatorType.ShiftRight:
197          return ExpressionType.RightShift;
198        case BinaryOperatorType.NullCoalescing:
199          return ExpressionType.Coalesce;
200        default:
201          throw new NotSupportedException("Invalid value for BinaryOperatorType");
202      }
203    }
204    #region Builder methods
205    public override MemberReferenceExpression Member(string memberName)
206    {
207      return new MemberReferenceExpression { Target = this, MemberName = memberName };
208    }
209
210    public override IndexerExpression Indexer(IEnumerable<Expression> arguments)
211    {
212      IndexerExpression expr = new IndexerExpression();
213      expr.Target = new ParenthesizedExpression(this);
214      expr.Arguments.AddRange(arguments);
215      return expr;
216    }
217
218    public override IndexerExpression Indexer(params Expression[] arguments)
219    {
220      IndexerExpression expr = new IndexerExpression();
221      expr.Target = new ParenthesizedExpression(this);
222      expr.Arguments.AddRange(arguments);
223      return expr;
224    }
225
226    public override InvocationExpression Invoke(string methodName, IEnumerable<AstType> typeArguments, IEnumerable<Expression> arguments)
227    {
228      InvocationExpression ie = new InvocationExpression();
229      MemberReferenceExpression mre = new MemberReferenceExpression();
230      mre.Target = new ParenthesizedExpression(this);
231      mre.MemberName = methodName;
232      mre.TypeArguments.AddRange(typeArguments);
233      ie.Target = mre;
234      ie.Arguments.AddRange(arguments);
235      return ie;
236    }
237
238    public override InvocationExpression Invoke(IEnumerable<Expression> arguments)
239    {
240      InvocationExpression ie = new InvocationExpression();
241      ie.Target = new ParenthesizedExpression(this);
242      ie.Arguments.AddRange(arguments);
243      return ie;
244    }
245
246    public override InvocationExpression Invoke(params Expression[] arguments)
247    {
248      InvocationExpression ie = new InvocationExpression();
249      ie.Target = new ParenthesizedExpression(this);
250      ie.Arguments.AddRange(arguments);
251      return ie;
252    }
253
254    public override CastExpression CastTo(AstType type)
255    {
256      return new CastExpression { Type = type,  Expression = new ParenthesizedExpression(this) };
257    }
258
259    public override AsExpression CastAs(AstType type)
260    {
261      return new AsExpression { Type = type,  Expression = new ParenthesizedExpression(this) };
262    }
263
264    public override IsExpression IsType(AstType type)
265    {
266      return new IsExpression { Type = type,  Expression = new ParenthesizedExpression(this) };
267    }
268    #endregion
269  }
270 
271  public enum BinaryOperatorType
272  {
273    /// <summary>
274    /// Any binary operator (used in pattern matching)
275    /// </summary>
276    Any,
277   
278    // We avoid 'logical or' on purpose, because it's not clear if that refers to the bitwise
279    // or to the short-circuiting (conditional) operator:
280    // MCS and old NRefactory used bitwise='|', logical='||'
281    // but the C# spec uses logical='|', conditional='||'
282    /// <summary>left &amp; right</summary>
283    BitwiseAnd,
284    /// <summary>left | right</summary>
285    BitwiseOr,
286    /// <summary>left &amp;&amp; right</summary>
287    ConditionalAnd,
288    /// <summary>left || right</summary>
289    ConditionalOr,
290    /// <summary>left ^ right</summary>
291    ExclusiveOr,
292   
293    /// <summary>left &gt; right</summary>
294    GreaterThan,
295    /// <summary>left &gt;= right</summary>
296    GreaterThanOrEqual,
297    /// <summary>left == right</summary>
298    Equality,
299    /// <summary>left != right</summary>
300    InEquality,
301    /// <summary>left &lt; right</summary>
302    LessThan,
303    /// <summary>left &lt;= right</summary>
304    LessThanOrEqual,
305   
306    /// <summary>left + right</summary>
307    Add,
308    /// <summary>left - right</summary>
309    Subtract,
310    /// <summary>left * right</summary>
311    Multiply,
312    /// <summary>left / right</summary>
313    Divide,
314    /// <summary>left % right</summary>
315    Modulus,
316   
317    /// <summary>left &lt;&lt; right</summary>
318    ShiftLeft,
319    /// <summary>left &gt;&gt; right</summary>
320    ShiftRight,
321   
322    /// <summary>left ?? right</summary>
323    NullCoalescing
324  }
325}
Note: See TracBrowser for help on using the repository browser.