2 // CSharpBinaryOperationBinder.cs
5 // Marek Safar <marek.safar@gmail.com>
7 // Copyright (C) 2009 Novell, Inc (http://www.novell.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
31 using System.Collections.Generic;
33 using System.Linq.Expressions;
34 using System.Runtime.CompilerServices;
35 using Compiler = Mono.CSharp;
37 namespace Microsoft.CSharp.RuntimeBinder
39 class CSharpBinaryOperationBinder : BinaryOperationBinder
41 IList<CSharpArgumentInfo> argumentInfo;
42 readonly CSharpBinderFlags flags;
43 readonly Type context;
45 public CSharpBinaryOperationBinder (ExpressionType operation, CSharpBinderFlags flags, Type context, IEnumerable<CSharpArgumentInfo> argumentInfo)
48 this.argumentInfo = new ReadOnlyCollectionBuilder<CSharpArgumentInfo> (argumentInfo);
49 if (this.argumentInfo.Count != 2)
50 throw new ArgumentException ("Binary operation requires 2 arguments");
53 this.context = context;
56 Compiler.Binary.Operator GetOperator (out bool isCompound)
60 case ExpressionType.Add:
61 return Compiler.Binary.Operator.Addition;
62 case ExpressionType.AddAssign:
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:
70 return Compiler.Binary.Operator.BitwiseAnd;
71 case ExpressionType.Divide:
72 return Compiler.Binary.Operator.Division;
73 case ExpressionType.DivideAssign:
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:
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:
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:
100 return Compiler.Binary.Operator.Modulus;
101 case ExpressionType.Multiply:
102 return Compiler.Binary.Operator.Multiply;
103 case ExpressionType.MultiplyAssign:
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:
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:
120 return Compiler.Binary.Operator.RightShift;
121 case ExpressionType.Subtract:
122 return Compiler.Binary.Operator.Subtraction;
123 case ExpressionType.SubtractAssign:
125 return Compiler.Binary.Operator.Subtraction;
127 throw new NotImplementedException (Operation.ToString ());
131 public override DynamicMetaObject FallbackBinaryOperation (DynamicMetaObject target, DynamicMetaObject arg, DynamicMetaObject errorSuggestion)
133 var left = CSharpBinder.CreateCompilerExpression (argumentInfo [0], target);
134 var right = CSharpBinder.CreateCompilerExpression (argumentInfo [1], arg);
137 var oper = GetOperator (out is_compound);
138 Compiler.Expression expr;
141 var target_expr = CSharpBinder.CreateCompilerExpression (argumentInfo[0], target);
142 expr = new Compiler.CompoundAssign (oper, target_expr, right, left, Compiler.Location.Null);
144 expr = new Compiler.Binary (oper, left, right, Compiler.Location.Null);
147 expr = new Compiler.Cast (new Compiler.TypeExpression (TypeImporter.Import (ReturnType), Compiler.Location.Null), expr, Compiler.Location.Null);
149 if ((flags & CSharpBinderFlags.CheckedContext) != 0)
150 expr = new Compiler.CheckedExpr (expr, Compiler.Location.Null);
152 var binder = new CSharpBinder (this, expr, errorSuggestion);
153 binder.AddRestrictions (target);
154 binder.AddRestrictions (arg);
156 return binder.Bind (context);