Normalize line endings.
[mono.git] / mcs / class / Microsoft.CSharp / Microsoft.CSharp.RuntimeBinder / CSharpBinaryOperationBinder.cs
1 //
2 // CSharpBinaryOperationBinder.cs
3 //
4 // Authors:
5 //      Marek Safar  <marek.safar@gmail.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
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 using System;
30 using System.Dynamic;
31 using System.Collections.Generic;
32 using System.Linq;
33 using System.Linq.Expressions;
34 using System.Runtime.CompilerServices;
35 using Compiler = Mono.CSharp;
36
37 namespace Microsoft.CSharp.RuntimeBinder
38 {
39         class CSharpBinaryOperationBinder : BinaryOperationBinder
40         {
41                 IList<CSharpArgumentInfo> argumentInfo;
42                 readonly CSharpBinderFlags flags;
43                 readonly Type context;
44                 
45                 public CSharpBinaryOperationBinder (ExpressionType operation, CSharpBinderFlags flags, Type context, IEnumerable<CSharpArgumentInfo> argumentInfo)
46                         : base (operation)
47                 {
48                         this.argumentInfo = new ReadOnlyCollectionBuilder<CSharpArgumentInfo> (argumentInfo);
49                         if (this.argumentInfo.Count != 2)
50                                 throw new ArgumentException ("Binary operation requires 2 arguments");
51
52                         this.flags = flags;
53                         this.context = context;
54                 }
55
56                 Compiler.Binary.Operator GetOperator (out bool isCompound)
57                 {
58                         isCompound = false;
59                         switch (Operation) {
60                         case ExpressionType.Add:
61                                 return Compiler.Binary.Operator.Addition;
62                         case ExpressionType.AddAssign:
63                                 isCompound = true;
64                                 return Compiler.Binary.Operator.Addition;
65                         case ExpressionType.And:
66                                 return (flags & CSharpBinderFlags.BinaryOperationLogical) != 0 ?
67                                         Compiler.Binary.Operator.LogicalAnd : Compiler.Binary.Operator.BitwiseAnd;
68                         case ExpressionType.AndAssign:
69                                 isCompound = true;
70                                 return Compiler.Binary.Operator.BitwiseAnd;
71                         case ExpressionType.Divide:
72                                 return Compiler.Binary.Operator.Division;
73                         case ExpressionType.DivideAssign:
74                                 isCompound = true;
75                                 return Compiler.Binary.Operator.Division;
76                         case ExpressionType.Equal:
77                                 return Compiler.Binary.Operator.Equality;
78                         case ExpressionType.ExclusiveOr:
79                                 return Compiler.Binary.Operator.ExclusiveOr;
80                         case ExpressionType.ExclusiveOrAssign:
81                                 isCompound = true;
82                                 return Compiler.Binary.Operator.ExclusiveOr;
83                         case ExpressionType.GreaterThan:
84                                 return Compiler.Binary.Operator.GreaterThan;
85                         case ExpressionType.GreaterThanOrEqual:
86                                 return Compiler.Binary.Operator.GreaterThanOrEqual;
87                         case ExpressionType.LeftShift:
88                                 return Compiler.Binary.Operator.LeftShift;
89                         case ExpressionType.LeftShiftAssign:
90                                 isCompound = true;
91                                 return Compiler.Binary.Operator.LeftShift;
92                         case ExpressionType.LessThan:
93                                 return Compiler.Binary.Operator.LessThan;
94                         case ExpressionType.LessThanOrEqual:
95                                 return Compiler.Binary.Operator.LessThanOrEqual;
96                         case ExpressionType.Modulo:
97                                 return Compiler.Binary.Operator.Modulus;
98                         case ExpressionType.ModuloAssign:
99                                 isCompound = true;
100                                 return Compiler.Binary.Operator.Modulus;
101                         case ExpressionType.Multiply:
102                                 return Compiler.Binary.Operator.Multiply;
103                         case ExpressionType.MultiplyAssign:
104                                 isCompound = true;
105                                 return Compiler.Binary.Operator.Multiply;
106                         case ExpressionType.NotEqual:
107                                 return Compiler.Binary.Operator.Inequality;
108                         case ExpressionType.Or:
109                                 return (flags & CSharpBinderFlags.BinaryOperationLogical) != 0 ?
110                                         Compiler.Binary.Operator.LogicalOr : Compiler.Binary.Operator.BitwiseOr;
111                         case ExpressionType.OrAssign:
112                                 isCompound = true;
113                                 return Compiler.Binary.Operator.BitwiseOr;
114                         case ExpressionType.OrElse:
115                                 return Compiler.Binary.Operator.LogicalOr;
116                         case ExpressionType.RightShift:
117                                 return Compiler.Binary.Operator.RightShift;
118                         case ExpressionType.RightShiftAssign:
119                                 isCompound = true;
120                                 return Compiler.Binary.Operator.RightShift;
121                         case ExpressionType.Subtract:
122                                 return Compiler.Binary.Operator.Subtraction;
123                         case ExpressionType.SubtractAssign:
124                                 isCompound = true;
125                                 return Compiler.Binary.Operator.Subtraction;
126                         default:
127                                 throw new NotImplementedException (Operation.ToString ());
128                         }
129                 }
130                 
131                 public override DynamicMetaObject FallbackBinaryOperation (DynamicMetaObject target, DynamicMetaObject arg, DynamicMetaObject errorSuggestion)
132                 {
133                         var left = CSharpBinder.CreateCompilerExpression (argumentInfo [0], target);
134                         var right = CSharpBinder.CreateCompilerExpression (argumentInfo [1], arg);
135                         
136                         bool is_compound;
137                         var oper = GetOperator (out is_compound);
138                         Compiler.Expression expr;
139
140                         if (is_compound) {
141                                 var target_expr = CSharpBinder.CreateCompilerExpression (argumentInfo[0], target);
142                                 expr = new Compiler.CompoundAssign (oper, target_expr, right, left, Compiler.Location.Null);
143                         } else {
144                                 expr = new Compiler.Binary (oper, left, right, Compiler.Location.Null);
145                         }
146
147                         expr = new Compiler.Cast (new Compiler.TypeExpression (TypeImporter.Import (ReturnType), Compiler.Location.Null), expr, Compiler.Location.Null);
148                         
149                         if ((flags & CSharpBinderFlags.CheckedContext) != 0)
150                                 expr = new Compiler.CheckedExpr (expr, Compiler.Location.Null);
151
152                         var binder = new CSharpBinder (this, expr, errorSuggestion);
153                         binder.AddRestrictions (target);
154                         binder.AddRestrictions (arg);
155
156                         return binder.Bind (context);
157                 }
158         }
159 }