2009-07-11 Michael Barker <mike@middlesoft.co.uk>
[mono.git] / mcs / class / dlr / Runtime / Microsoft.Scripting.Core / Actions / UnaryOperationBinder.cs
1 /* ****************************************************************************
2  *
3  * Copyright (c) Microsoft Corporation. 
4  *
5  * This source code is subject to terms and conditions of the Microsoft Public License. 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  Microsoft Public License, 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 Microsoft Public License.
10  *
11  * You must not remove this notice, or any other, from this software.
12  *
13  *
14  * ***************************************************************************/
15 using System; using Microsoft;
16
17
18 #if CODEPLEX_40
19 using System.Dynamic.Utils;
20 using System.Linq.Expressions;
21 #else
22 using Microsoft.Scripting.Utils;
23 using Microsoft.Linq.Expressions;
24 #endif
25
26 #if CODEPLEX_40
27 namespace System.Dynamic {
28 #else
29 namespace Microsoft.Scripting {
30 #endif
31     /// <summary>
32     /// Represents the unary dynamic operation at the call site, providing the binding semantic and the details about the operation.
33     /// </summary>
34     public abstract class UnaryOperationBinder : DynamicMetaObjectBinder {
35         private ExpressionType _operation;
36
37         /// <summary>
38         /// Initializes a new instance of the <see cref="BinaryOperationBinder"/> class.
39         /// </summary>
40         /// <param name="operation">The unary operation kind.</param>
41         protected UnaryOperationBinder(ExpressionType operation) {
42             ContractUtils.Requires(OperationIsValid(operation), "operation");
43             _operation = operation;
44         }
45
46         /// <summary>
47         /// The result type of the operation.
48         /// </summary>
49         public override sealed Type ReturnType {
50             get {
51                 switch(_operation) {
52                     case ExpressionType.IsFalse:
53                     case ExpressionType.IsTrue:
54                         return typeof(bool);
55                     default:
56                         return typeof(object);
57                 }
58             }
59         }
60
61         /// <summary>
62         /// The unary operation kind.
63         /// </summary>
64         public ExpressionType Operation {
65             get {
66                 return _operation;
67             }
68         }
69
70         /// <summary>
71         /// Performs the binding of the unary dynamic operation if the target dynamic object cannot bind.
72         /// </summary>
73         /// <param name="target">The target of the dynamic unary operation.</param>
74         /// <returns>The <see cref="DynamicMetaObject"/> representing the result of the binding.</returns>
75         public DynamicMetaObject FallbackUnaryOperation(DynamicMetaObject target) {
76             return FallbackUnaryOperation(target, null);
77         }
78
79         /// <summary>
80         /// Performs the binding of the unary dynamic operation if the target dynamic object cannot bind.
81         /// </summary>
82         /// <param name="target">The target of the dynamic unary operation.</param>
83         /// <param name="errorSuggestion">The binding result in case the binding fails, or null.</param>
84         /// <returns>The <see cref="DynamicMetaObject"/> representing the result of the binding.</returns>
85         public abstract DynamicMetaObject FallbackUnaryOperation(DynamicMetaObject target, DynamicMetaObject errorSuggestion);
86
87         /// <summary>
88         /// Performs the binding of the dynamic unary operation.
89         /// </summary>
90         /// <param name="target">The target of the dynamic operation.</param>
91         /// <param name="args">An array of arguments of the dynamic operation.</param>
92         /// <returns>The <see cref="DynamicMetaObject"/> representing the result of the binding.</returns>
93         public sealed override DynamicMetaObject Bind(DynamicMetaObject target, DynamicMetaObject[] args) {
94             ContractUtils.RequiresNotNull(target, "target");
95             ContractUtils.Requires(args == null || args.Length == 0, "args");
96
97             return target.BindUnaryOperation(this);
98         }
99
100         // this is a standard DynamicMetaObjectBinder
101         internal override sealed bool IsStandardBinder {
102             get {
103                 return true;
104             }
105         }
106
107         internal static bool OperationIsValid(ExpressionType operation) {
108             switch (operation) {
109                 #region Generated Unary Operation Binder Validator
110
111                 // *** BEGIN GENERATED CODE ***
112                 // generated by function: gen_unop_validator from: generate_tree.py
113
114                 case ExpressionType.Negate:
115                 case ExpressionType.UnaryPlus:
116                 case ExpressionType.Not:
117                 case ExpressionType.Decrement:
118                 case ExpressionType.Increment:
119                 case ExpressionType.OnesComplement:
120                 case ExpressionType.IsTrue:
121                 case ExpressionType.IsFalse:
122
123                 // *** END GENERATED CODE ***
124
125                 #endregion
126
127                 case ExpressionType.Extension:
128                     return true;
129
130                 default:
131                     return false;
132             }
133         }
134     }
135 }