PageRenderTime 33ms CodeModel.GetById 16ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 1ms

/IronPython_Main/Runtime/Microsoft.Scripting.Core/Actions/UnaryOperationBinder.cs

#
C# | 129 lines | 63 code | 18 blank | 48 comment | 4 complexity | 4dbb2b3bb84faea8f98e1fa15fac605e MD5 | raw file
  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 CLR2
 17using Microsoft.Scripting.Ast;
 18#else
 19using System.Linq.Expressions;
 20#endif
 21
 22using System.Dynamic.Utils;
 23
 24namespace System.Dynamic {
 25    /// <summary>
 26    /// Represents the unary dynamic operation at the call site, providing the binding semantic and the details about the operation.
 27    /// </summary>
 28    public abstract class UnaryOperationBinder : 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 unary operation kind.</param>
 35        protected UnaryOperationBinder(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 {
 45                switch(_operation) {
 46                    case ExpressionType.IsFalse:
 47                    case ExpressionType.IsTrue:
 48                        return typeof(bool);
 49                    default:
 50                        return typeof(object);
 51                }
 52            }
 53        }
 54
 55        /// <summary>
 56        /// The unary operation kind.
 57        /// </summary>
 58        public ExpressionType Operation {
 59            get {
 60                return _operation;
 61            }
 62        }
 63
 64        /// <summary>
 65        /// Performs the binding of the unary dynamic operation if the target dynamic object cannot bind.
 66        /// </summary>
 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);
 71        }
 72
 73        /// <summary>
 74        /// Performs the binding of the unary dynamic operation if the target dynamic object cannot bind.
 75        /// </summary>
 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);
 80
 81        /// <summary>
 82        /// Performs the binding of the dynamic unary operation.
 83        /// </summary>
 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");
 90
 91            return target.BindUnaryOperation(this);
 92        }
 93
 94        // this is a standard DynamicMetaObjectBinder
 95        internal override sealed bool IsStandardBinder {
 96            get {
 97                return true;
 98            }
 99        }
100
101        internal static bool OperationIsValid(ExpressionType operation) {
102            switch (operation) {
103                #region Generated Unary Operation Binder Validator
104
105                // *** BEGIN GENERATED CODE ***
106                // generated by function: gen_unop_validator from: generate_tree.py
107
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:
116
117                // *** END GENERATED CODE ***
118
119                #endregion
120
121                case ExpressionType.Extension:
122                    return true;
123
124                default:
125                    return false;
126            }
127        }
128    }
129}