1 /* ****************************************************************************
3 * Copyright (c) Microsoft Corporation.
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.
11 * You must not remove this notice, or any other, from this software.
14 * ***************************************************************************/
15 using System; using Microsoft;
18 using System.Diagnostics;
20 using System.Dynamic.Utils;
22 using Microsoft.Scripting.Utils;
26 namespace System.Linq.Expressions {
28 namespace Microsoft.Linq.Expressions {
32 /// Represents a named parameter expression.
35 [DebuggerTypeProxy(typeof(Expression.ParameterExpressionProxy))]
37 public class ParameterExpression : Expression {
38 private readonly string _name;
40 internal ParameterExpression(string name) {
44 internal static ParameterExpression Make(Type type, string name, bool isByRef) {
46 return new ByRefParameterExpression(type, name);
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);
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);
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);
82 return new TypedParameterExpression(type, name);
86 /// Gets the static type of the expression that this <see cref="Expression" /> represents. (Inherited from <see cref="Expression"/>.)
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); }
94 /// Returns the node type of this <see cref="Expression" />. (Inherited from <see cref="Expression" />.)
96 /// <returns>The <see cref="ExpressionType"/> that represents this expression.</returns>
97 public sealed override ExpressionType NodeType {
98 get { return ExpressionType.Parameter; }
102 /// The Name of the parameter or variable.
105 get { return _name; }
109 /// Indicates that this ParameterExpression is to be treated as a ByRef parameter.
111 public bool IsByRef {
117 internal virtual bool GetIsByRef() {
121 internal override Expression Accept(ExpressionVisitor visitor) {
122 return visitor.VisitParameter(this);
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.
131 internal sealed class ByRefParameterExpression : TypedParameterExpression {
132 internal ByRefParameterExpression(Type type, string name)
136 internal override bool GetIsByRef() {
142 /// Specialized subclass which holds onto the type of the expression for
145 internal class TypedParameterExpression : ParameterExpression {
146 private readonly Type _paramType;
148 internal TypedParameterExpression(Type type, string name)
153 public sealed override Type Type {
154 get { return _paramType; }
159 /// Generic type to avoid needing explicit storage for primitive data types
160 /// which are commonly used.
162 internal sealed class PrimitiveParameterExpression<T> : ParameterExpression {
163 internal PrimitiveParameterExpression(string name)
167 public sealed override Type Type {
168 get { return typeof(T); }
172 public partial class Expression {
175 /// Creates a <see cref="ParameterExpression" /> node that can be used to identify a parameter or a variable in an expression tree.
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);
184 /// Creates a <see cref="ParameterExpression" /> node that can be used to identify a parameter or a variable in an expression tree.
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);
193 /// Creates a <see cref="ParameterExpression" /> node that can be used to identify a parameter or a variable in an expression tree.
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");
201 if (type == typeof(void)) {
202 throw Error.ArgumentCannotBeOfTypeVoid();
205 bool byref = type.IsByRef;
207 type = type.GetElementType();
210 return ParameterExpression.Make(type, name, byref);
214 /// Creates a <see cref="ParameterExpression" /> node that can be used to identify a parameter or a variable in an expression tree.
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);