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 unary dynamic operation at the call site, providing the binding semantic and the details about the operation.
28 public abstract class UnaryOperationBinder : DynamicMetaObjectBinder {
29 private ExpressionType _operation;
32 /// Initializes a new instance of the <see cref="BinaryOperationBinder"/> class.
34 /// <param name="operation">The unary operation kind.</param>
35 protected UnaryOperationBinder(ExpressionType operation) {
36 ContractUtils.Requires(OperationIsValid(operation), "operation");
37 _operation = operation;
41 /// The result type of the operation.
43 public override sealed Type ReturnType {
46 case ExpressionType.IsFalse:
47 case ExpressionType.IsTrue:
50 return typeof(object);
56 /// The unary operation kind.
58 public ExpressionType Operation {
65 /// Performs the binding of the unary dynamic operation if the target dynamic object cannot bind.
67 /// <param name="target">The target of the dynamic unary operation.</param>
68 /// <returns>The <see cref="DynamicMetaObject"/> representing the result of the binding.</returns>
69 public DynamicMetaObject FallbackUnaryOperation(DynamicMetaObject target) {
70 return FallbackUnaryOperation(target, null);
74 /// Performs the binding of the unary dynamic operation if the target dynamic object cannot bind.
76 /// <param name="target">The target of the dynamic unary operation.</param>
77 /// <param name="errorSuggestion">The binding result in case the binding fails, or null.</param>
78 /// <returns>The <see cref="DynamicMetaObject"/> representing the result of the binding.</returns>
79 public abstract DynamicMetaObject FallbackUnaryOperation(DynamicMetaObject target, DynamicMetaObject errorSuggestion);
82 /// Performs the binding of the dynamic unary operation.
84 /// <param name="target">The target of the dynamic operation.</param>
85 /// <param name="args">An array of arguments of the dynamic operation.</param>
86 /// <returns>The <see cref="DynamicMetaObject"/> representing the result of the binding.</returns>
87 public sealed override DynamicMetaObject Bind(DynamicMetaObject target, DynamicMetaObject[] args) {
88 ContractUtils.RequiresNotNull(target, "target");
89 ContractUtils.Requires(args == null || args.Length == 0, "args");
91 return target.BindUnaryOperation(this);
94 // this is a standard DynamicMetaObjectBinder
95 internal override sealed bool IsStandardBinder {
101 internal static bool OperationIsValid(ExpressionType operation) {
103 #region Generated Unary Operation Binder Validator
105 // *** BEGIN GENERATED CODE ***
106 // generated by function: gen_unop_validator from: generate_tree.py
108 case ExpressionType.Negate:
109 case ExpressionType.UnaryPlus:
110 case ExpressionType.Not:
111 case ExpressionType.Decrement:
112 case ExpressionType.Increment:
113 case ExpressionType.OnesComplement:
114 case ExpressionType.IsTrue:
115 case ExpressionType.IsFalse:
117 // *** END GENERATED CODE ***
121 case ExpressionType.Extension: