Merge pull request #323 from crazyjncsu/master
[mono.git] / mcs / class / dlr / Runtime / Microsoft.Scripting.Core / Actions / BinaryOperationBinder.cs
1 /* ****************************************************************************
2  *
3  * Copyright (c) Microsoft Corporation. 
4  *
5  * This source code is subject to terms and conditions of the Apache License, Version 2.0. A 
6  * copy of the license can be found in the License.html file at the root of this distribution. If 
7  * you cannot locate the  Apache License, Version 2.0, please send an email to 
8  * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound 
9  * by the terms of the Apache License, Version 2.0.
10  *
11  * You must not remove this notice, or any other, from this software.
12  *
13  *
14  * ***************************************************************************/
15
16 #if !FEATURE_CORE_DLR
17 using Microsoft.Scripting.Ast;
18 #else
19 using System.Linq.Expressions;
20 #endif
21
22 using System.Dynamic.Utils;
23
24 namespace System.Dynamic {
25     /// <summary>
26     /// Represents the binary dynamic operation at the call site, providing the binding semantic and the details about the operation.
27     /// </summary>
28     public abstract class BinaryOperationBinder : DynamicMetaObjectBinder {
29         private ExpressionType _operation;
30
31         /// <summary>
32         /// Initializes a new instance of the <see cref="BinaryOperationBinder"/> class.
33         /// </summary>
34         /// <param name="operation">The binary operation kind.</param>
35         protected BinaryOperationBinder(ExpressionType operation) {
36             ContractUtils.Requires(OperationIsValid(operation), "operation");
37             _operation = operation;
38         }
39
40         /// <summary>
41         /// The result type of the operation.
42         /// </summary>
43         public override sealed Type ReturnType {
44             get { return typeof(object); }
45         }
46
47         /// <summary>
48         /// The binary operation kind.
49         /// </summary>
50         public ExpressionType Operation {
51             get {
52                 return _operation;
53             }
54         }
55
56         /// <summary>
57         /// Performs the binding of the binary dynamic operation if the target dynamic object cannot bind.
58         /// </summary>
59         /// <param name="target">The target of the dynamic binary operation.</param>
60         /// <param name="arg">The right hand side operand of the dynamic binary operation.</param>
61         /// <returns>The <see cref="DynamicMetaObject"/> representing the result of the binding.</returns>
62         public DynamicMetaObject FallbackBinaryOperation(DynamicMetaObject target, DynamicMetaObject arg) {
63             return FallbackBinaryOperation(target, arg, null);
64         }
65
66         /// <summary>
67         /// When overridden in the derived class, performs the binding of the binary dynamic operation if the target dynamic object cannot bind.
68         /// </summary>
69         /// <param name="target">The target of the dynamic binary operation.</param>
70         /// <param name="arg">The right hand side operand of the dynamic binary operation.</param>
71         /// <param name="errorSuggestion">The binding result in case the binding fails, or null.</param>
72         /// <returns>The <see cref="DynamicMetaObject"/> representing the result of the binding.</returns>
73         public abstract DynamicMetaObject FallbackBinaryOperation(DynamicMetaObject target, DynamicMetaObject arg, DynamicMetaObject errorSuggestion);
74
75         /// <summary>
76         /// Performs the binding of the dynamic binary operation.
77         /// </summary>
78         /// <param name="target">The target of the dynamic operation.</param>
79         /// <param name="args">An array of arguments of the dynamic operation.</param>
80         /// <returns>The <see cref="DynamicMetaObject"/> representing the result of the binding.</returns>
81         public sealed override DynamicMetaObject Bind(DynamicMetaObject target, DynamicMetaObject[] args) {
82             ContractUtils.RequiresNotNull(target, "target");
83             ContractUtils.RequiresNotNull(args, "args");
84             ContractUtils.Requires(args.Length == 1, "args");
85
86             var arg0 = args[0];
87             ContractUtils.RequiresNotNull(arg0, "args");
88
89             return target.BindBinaryOperation(this, arg0);
90         }
91
92         // this is a standard DynamicMetaObjectBinder
93         internal override sealed bool IsStandardBinder {
94             get {
95                 return true;
96             }
97         }
98
99         [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity")]
100         internal static bool OperationIsValid(ExpressionType operation) {
101             switch (operation) {
102                 #region Generated Binary Operation Binder Validator
103
104                 // *** BEGIN GENERATED CODE ***
105                 // generated by function: gen_binop_validator from: generate_tree.py
106
107                 case ExpressionType.Add:
108                 case ExpressionType.And:
109                 case ExpressionType.Divide:
110                 case ExpressionType.Equal:
111                 case ExpressionType.ExclusiveOr:
112                 case ExpressionType.GreaterThan:
113                 case ExpressionType.GreaterThanOrEqual:
114                 case ExpressionType.LeftShift:
115                 case ExpressionType.LessThan:
116                 case ExpressionType.LessThanOrEqual:
117                 case ExpressionType.Modulo:
118                 case ExpressionType.Multiply:
119                 case ExpressionType.NotEqual:
120                 case ExpressionType.Or:
121                 case ExpressionType.Power:
122                 case ExpressionType.RightShift:
123                 case ExpressionType.Subtract:
124                 case ExpressionType.AddAssign:
125                 case ExpressionType.AndAssign:
126                 case ExpressionType.DivideAssign:
127                 case ExpressionType.ExclusiveOrAssign:
128                 case ExpressionType.LeftShiftAssign:
129                 case ExpressionType.ModuloAssign:
130                 case ExpressionType.MultiplyAssign:
131                 case ExpressionType.OrAssign:
132                 case ExpressionType.PowerAssign:
133                 case ExpressionType.RightShiftAssign:
134                 case ExpressionType.SubtractAssign:
135
136                 // *** END GENERATED CODE ***
137
138                 #endregion
139
140                 case ExpressionType.Extension:
141                     return true;
142
143                 default:
144                     return false;
145             }
146         }
147     }
148 }