1 /* ****************************************************************************
3 * Copyright (c) Microsoft Corporation.
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.
11 * You must not remove this notice, or any other, from this software.
14 * ***************************************************************************/
17 using Microsoft.Scripting.Ast;
19 using System.Linq.Expressions;
22 using System.Dynamic.Utils;
24 namespace System.Dynamic {
26 /// Represents the binary dynamic operation at the call site, providing the binding semantic and the details about the operation.
28 public abstract class BinaryOperationBinder : DynamicMetaObjectBinder {
29 private ExpressionType _operation;
32 /// Initializes a new instance of the <see cref="BinaryOperationBinder"/> class.
34 /// <param name="operation">The binary operation kind.</param>
35 protected BinaryOperationBinder(ExpressionType operation) {
36 ContractUtils.Requires(OperationIsValid(operation), "operation");
37 _operation = operation;
41 /// The result type of the operation.
43 public override sealed Type ReturnType {
44 get { return typeof(object); }
48 /// The binary operation kind.
50 public ExpressionType Operation {
57 /// Performs the binding of the binary dynamic operation if the target dynamic object cannot bind.
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);
67 /// When overridden in the derived class, performs the binding of the binary dynamic operation if the target dynamic object cannot bind.
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);
76 /// Performs the binding of the dynamic binary operation.
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");
87 ContractUtils.RequiresNotNull(arg0, "args");
89 return target.BindBinaryOperation(this, arg0);
92 // this is a standard DynamicMetaObjectBinder
93 internal override sealed bool IsStandardBinder {
99 [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity")]
100 internal static bool OperationIsValid(ExpressionType operation) {
102 #region Generated Binary Operation Binder Validator
104 // *** BEGIN GENERATED CODE ***
105 // generated by function: gen_binop_validator from: generate_tree.py
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:
136 // *** END GENERATED CODE ***
140 case ExpressionType.Extension: