2009-07-11 Michael Barker <mike@middlesoft.co.uk>
[mono.git] / mcs / class / dlr / Runtime / Microsoft.Scripting.Core / Ast / ParameterExpression.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 using System.Diagnostics;
19 #if CODEPLEX_40
20 using System.Dynamic.Utils;
21 #else
22 using Microsoft.Scripting.Utils;
23 #endif
24
25 #if CODEPLEX_40
26 namespace System.Linq.Expressions {
27 #else
28 namespace Microsoft.Linq.Expressions {
29 #endif
30
31     /// <summary>
32     /// Represents a named parameter expression.
33     /// </summary>
34 #if !SILVERLIGHT
35     [DebuggerTypeProxy(typeof(Expression.ParameterExpressionProxy))]
36 #endif
37     public class ParameterExpression : Expression {
38         private readonly string _name;
39
40         internal ParameterExpression(string name) {
41             _name = name;
42         }
43
44         internal static ParameterExpression Make(Type type, string name, bool isByRef) {
45             if (isByRef) {
46                 return new ByRefParameterExpression(type, name);
47             } else {
48                 if (!type.IsEnum) {
49                     switch (Type.GetTypeCode(type)) {
50                         case TypeCode.Boolean: return new PrimitiveParameterExpression<Boolean>(name);
51                         case TypeCode.Byte: return new PrimitiveParameterExpression<Byte>(name);
52                         case TypeCode.Char: return new PrimitiveParameterExpression<Char>(name);
53                         case TypeCode.DateTime: return new PrimitiveParameterExpression<DateTime>(name);
54                         case TypeCode.DBNull: return new PrimitiveParameterExpression<DBNull>(name);
55                         case TypeCode.Decimal: return new PrimitiveParameterExpression<Decimal>(name);
56                         case TypeCode.Double: return new PrimitiveParameterExpression<Double>(name);
57                         case TypeCode.Int16: return new PrimitiveParameterExpression<Int16>(name);
58                         case TypeCode.Int32: return new PrimitiveParameterExpression<Int32>(name);
59                         case TypeCode.Int64: return new PrimitiveParameterExpression<Int64>(name);
60                         case TypeCode.Object:
61                             // common reference types which we optimize go here.  Of course object is in
62                             // the list, the others are driven by profiling of various workloads.  This list
63                             // should be kept short.
64                             if (type == typeof(object)) {
65                                 return new ParameterExpression(name);
66                             } else if (type == typeof(Exception)) {
67                                 return new PrimitiveParameterExpression<Exception>(name);
68                             } else if (type == typeof(object[])) {
69                                 return new PrimitiveParameterExpression<object[]>(name);
70                             }
71                             break;
72                         case TypeCode.SByte: return new PrimitiveParameterExpression<SByte>(name);
73                         case TypeCode.Single: return new PrimitiveParameterExpression<Single>(name);
74                         case TypeCode.String: return new PrimitiveParameterExpression<String>(name);
75                         case TypeCode.UInt16: return new PrimitiveParameterExpression<UInt16>(name);
76                         case TypeCode.UInt32: return new PrimitiveParameterExpression<UInt32>(name);
77                         case TypeCode.UInt64: return new PrimitiveParameterExpression<UInt64>(name);
78                     }
79                 }
80             }
81
82             return new TypedParameterExpression(type, name);
83         }
84
85         /// <summary>
86         /// Gets the static type of the expression that this <see cref="Expression" /> represents. (Inherited from <see cref="Expression"/>.)
87         /// </summary>
88         /// <returns>The <see cref="Type"/> that represents the static type of the expression.</returns>
89         public override Type Type {
90             get { return typeof(object); }
91         }
92
93         /// <summary>
94         /// Returns the node type of this <see cref="Expression" />. (Inherited from <see cref="Expression" />.)
95         /// </summary>
96         /// <returns>The <see cref="ExpressionType"/> that represents this expression.</returns>
97         public sealed override ExpressionType NodeType {
98             get { return ExpressionType.Parameter; }
99         }
100
101         /// <summary>
102         /// The Name of the parameter or variable.
103         /// </summary>
104         public string Name {
105             get { return _name; }
106         }
107
108         /// <summary>
109         /// Indicates that this ParameterExpression is to be treated as a ByRef parameter.
110         /// </summary>
111         public bool IsByRef {
112             get {
113                 return GetIsByRef();
114             }
115         }
116
117         internal virtual bool GetIsByRef() {
118             return false;
119         }
120
121         internal override Expression Accept(ExpressionVisitor visitor) {
122             return visitor.VisitParameter(this);
123         }
124     }
125
126     /// <summary>
127     /// Specialized subclass to avoid holding onto the byref flag in a 
128     /// parameter expression.  This version always holds onto the expression
129     /// type explicitly and therefore derives from TypedParameterExpression.
130     /// </summary>
131     internal sealed class ByRefParameterExpression : TypedParameterExpression {
132         internal ByRefParameterExpression(Type type, string name)
133             : base(type, name) {
134         }
135
136         internal override bool GetIsByRef() {
137             return true;
138         }
139     }
140
141     /// <summary>
142     /// Specialized subclass which holds onto the type of the expression for
143     /// uncommon types.
144     /// </summary>
145     internal class TypedParameterExpression : ParameterExpression {
146         private readonly Type _paramType;
147
148         internal TypedParameterExpression(Type type, string name)
149             : base(name) {
150             _paramType = type;
151         }
152
153         public sealed override Type Type {
154             get { return _paramType; }
155         }
156     }
157
158     /// <summary>
159     /// Generic type to avoid needing explicit storage for primitive data types
160     /// which are commonly used.
161     /// </summary>
162     internal sealed class PrimitiveParameterExpression<T> : ParameterExpression {
163         internal PrimitiveParameterExpression(string name)
164             : base(name) {
165         }
166
167         public sealed override Type Type {
168             get { return typeof(T); }
169         }
170     }
171
172     public partial class Expression {
173
174         /// <summary>
175         /// Creates a <see cref="ParameterExpression" /> node that can be used to identify a parameter or a variable in an expression tree.
176         /// </summary>
177         /// <param name="type">The type of the parameter or variable.</param>
178         /// <returns>A <see cref="ParameterExpression" /> node with the specified name and type.</returns>
179         public static ParameterExpression Parameter(Type type) {
180             return Parameter(type, null);
181         }
182
183         /// <summary>
184         /// Creates a <see cref="ParameterExpression" /> node that can be used to identify a parameter or a variable in an expression tree.
185         /// </summary>
186         /// <param name="type">The type of the parameter or variable.</param>
187         /// <returns>A <see cref="ParameterExpression" /> node with the specified name and type.</returns>
188         public static ParameterExpression Variable(Type type) {
189             return Variable(type, null);
190         }
191
192         /// <summary>
193         /// Creates a <see cref="ParameterExpression" /> node that can be used to identify a parameter or a variable in an expression tree.
194         /// </summary>
195         /// <param name="type">The type of the parameter or variable.</param>
196         /// <param name="name">The name of the parameter or variable, used for debugging or pretty printing purpose only.</param>
197         /// <returns>A <see cref="ParameterExpression" /> node with the specified name and type.</returns>
198         public static ParameterExpression Parameter(Type type, string name) {
199             ContractUtils.RequiresNotNull(type, "type");
200
201             if (type == typeof(void)) {
202                 throw Error.ArgumentCannotBeOfTypeVoid();
203             }
204
205             bool byref = type.IsByRef;
206             if (byref) {
207                 type = type.GetElementType();
208             }
209
210             return ParameterExpression.Make(type, name, byref);
211         }
212
213         /// <summary>
214         /// Creates a <see cref="ParameterExpression" /> node that can be used to identify a parameter or a variable in an expression tree.
215         /// </summary>
216         /// <param name="type">The type of the parameter or variable.</param>
217         /// <param name="name">The name of the parameter or variable, used for debugging or pretty printing purpose only.</param>
218         /// <returns>A <see cref="ParameterExpression" /> node with the specified name and type.</returns>
219         public static ParameterExpression Variable(Type type, string name) {
220             ContractUtils.RequiresNotNull(type, "type");
221             ContractUtils.Requires(type != typeof(void), "type", Strings.ArgumentCannotBeOfTypeVoid);
222             ContractUtils.Requires(!type.IsByRef, "type", Strings.TypeMustNotBeByRef);
223             return ParameterExpression.Make(type, name, false);
224         }
225     }
226 }