2 // ConstantExpression.cs
5 // Jb Evain (jbevain@novell.com)
6 // Miguel de Icaza (miguel@novell.com)
8 // Some code is based on the Mono C# compiler:
9 // Marek Safar (marek.safar@seznam.cz)
10 // Martin Baulig (martin@ximian.com)
12 // (C) 2001-2008 Novell, Inc. (http://www.novell.com)
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 using System.Reflection;
36 using System.Reflection.Emit;
38 namespace System.Linq.Expressions {
40 public sealed class ConstantExpression : Expression {
47 internal ConstantExpression (object value, Type type)
48 : base (ExpressionType.Constant, type)
53 internal override void Emit (EmitContext ec)
55 if (Type.IsNullable ()) {
56 EmitNullableConstant (ec, Type, value);
60 EmitConstant (ec, Type, value);
63 void EmitNullableConstant (EmitContext ec, Type type, object value)
66 var local = ec.ig.DeclareLocal (type);
67 ec.EmitNullableInitialize (local);
69 EmitConstant (ec, type.GetFirstGenericArgument (), value);
70 ec.EmitNullableNew (type);
74 void EmitConstant (EmitContext ec, Type type, object value)
78 switch (Type.GetTypeCode (type)){
80 ig.Emit (OpCodes.Ldc_I4, (int) ((byte)value));
84 ig.Emit (OpCodes.Ldc_I4, (int) ((sbyte)value));
88 ig.Emit (OpCodes.Ldc_I4, (int) ((short)value));
92 ig.Emit (OpCodes.Ldc_I4, (int) ((ushort)value));
96 ig.Emit (OpCodes.Ldc_I4, (int) value);
100 ig.Emit (OpCodes.Ldc_I4, unchecked ((int) ((uint)Value)));
104 ig.Emit (OpCodes.Ldc_I8, (long) value);
107 case TypeCode.UInt64:
108 ig.Emit (OpCodes.Ldc_I8, unchecked ((long) ((ulong)value)));
111 case TypeCode.Boolean:
113 ig.Emit (OpCodes.Ldc_I4_1);
115 ec.ig.Emit (OpCodes.Ldc_I4_0);
119 ig.Emit (OpCodes.Ldc_I4, (int) ((char) value));
122 case TypeCode.Single:
123 ig.Emit (OpCodes.Ldc_R4, (float) value);
126 case TypeCode.Double:
127 ig.Emit (OpCodes.Ldc_R8, (double) value);
130 case TypeCode.Decimal: {
131 Decimal v = (decimal) value;
132 int [] words = Decimal.GetBits (v);
133 int power = (words [3] >> 16) & 0xff;
134 Type ti = typeof (int);
136 if (power == 0 && v <= int.MaxValue && v >= int.MinValue) {
137 ig.Emit (OpCodes.Ldc_I4, (int) v);
139 ig.Emit (OpCodes.Newobj, typeof (Decimal).GetConstructor (new Type [1] { ti }));
142 ig.Emit (OpCodes.Ldc_I4, words [0]);
143 ig.Emit (OpCodes.Ldc_I4, words [1]);
144 ig.Emit (OpCodes.Ldc_I4, words [2]);
146 ig.Emit (OpCodes.Ldc_I4, words [3] >> 31);
149 ig.Emit (OpCodes.Ldc_I4, power);
151 ig.Emit (OpCodes.Newobj, typeof (Decimal).GetConstructor (new Type [5] { ti, ti, ti, typeof(bool), typeof(byte) }));
155 case TypeCode.DateTime: {
156 var date = (DateTime) value;
157 var local = ig.DeclareLocal (typeof (DateTime));
159 ig.Emit (OpCodes.Ldloca, local);
160 ig.Emit (OpCodes.Ldc_I8, date.Ticks);
161 ig.Emit (OpCodes.Ldc_I4, (int) date.Kind);
162 ig.Emit (OpCodes.Call, typeof (DateTime).GetConstructor (new [] { typeof (long), typeof (DateTimeKind) }));
163 ig.Emit (OpCodes.Ldloc, local);
168 case TypeCode.DBNull:
169 ig.Emit (OpCodes.Ldsfld, typeof (DBNull).GetField ("Value", BindingFlags.Public | BindingFlags.Static));
172 case TypeCode.String:
173 EmitIfNotNull (ec, c => c.ig.Emit (OpCodes.Ldstr, (string) value));
176 case TypeCode.Object:
177 EmitIfNotNull (ec, c => c.EmitReadGlobal (value));
181 throw new NotImplementedException (String.Format ("No support for constants of type {0} yet", Type));
184 void EmitIfNotNull (EmitContext ec, Action<EmitContext> emit)
187 ec.ig.Emit (OpCodes.Ldnull);