2009-09-03 Jb Evain <jbevain@novell.com>
[mcs.git] / class / System.Core / System.Linq.Expressions / ConstantExpression.cs
blobf59a05d6b638b8b5cc2a5dfef0bb3b54c2ae7622
1 //
2 // ConstantExpression.cs
3 //
4 // Author:
5 // Jb Evain (jbevain@novell.com)
6 // Miguel de Icaza (miguel@novell.com)
7 //
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.
34 using System;
35 using System.Reflection;
36 using System.Reflection.Emit;
38 namespace System.Linq.Expressions {
40 public sealed class ConstantExpression : Expression {
41 object value;
43 public object Value {
44 get { return value; }
47 internal ConstantExpression (object value, Type type)
48 : base (ExpressionType.Constant, type)
50 this.value = value;
53 internal override void Emit (EmitContext ec)
55 ILGenerator ig = ec.ig;
57 switch (Type.GetTypeCode (Type)){
58 case TypeCode.Byte:
59 ig.Emit (OpCodes.Ldc_I4, (int) ((byte)value));
60 return;
62 case TypeCode.SByte:
63 ig.Emit (OpCodes.Ldc_I4, (int) ((sbyte)value));
64 return;
66 case TypeCode.Int16:
67 ig.Emit (OpCodes.Ldc_I4, (int) ((short)value));
68 return;
70 case TypeCode.UInt16:
71 ig.Emit (OpCodes.Ldc_I4, (int) ((ushort)value));
72 return;
74 case TypeCode.Int32:
75 ig.Emit (OpCodes.Ldc_I4, (int) value);
76 return;
78 case TypeCode.UInt32:
79 ig.Emit (OpCodes.Ldc_I4, unchecked ((int) ((uint)Value)));
80 return;
82 case TypeCode.Int64:
83 ig.Emit (OpCodes.Ldc_I8, (long) value);
84 return;
86 case TypeCode.UInt64:
87 ig.Emit (OpCodes.Ldc_I8, unchecked ((long) ((ulong)value)));
88 return;
90 case TypeCode.Boolean:
91 if ((bool) Value)
92 ig.Emit (OpCodes.Ldc_I4_1);
93 else
94 ec.ig.Emit (OpCodes.Ldc_I4_0);
95 return;
97 case TypeCode.Char:
98 ig.Emit (OpCodes.Ldc_I4, (int) ((char) value));
99 return;
101 case TypeCode.Single:
102 ig.Emit (OpCodes.Ldc_R4, (float) value);
103 return;
105 case TypeCode.Double:
106 ig.Emit (OpCodes.Ldc_R8, (double) value);
107 return;
109 case TypeCode.Decimal: {
110 Decimal v = (decimal) value;
111 int [] words = Decimal.GetBits (v);
112 int power = (words [3] >> 16) & 0xff;
113 Type ti = typeof (int);
115 if (power == 0 && v <= int.MaxValue && v >= int.MinValue) {
116 ig.Emit (OpCodes.Ldc_I4, (int) v);
118 ig.Emit (OpCodes.Newobj, typeof (Decimal).GetConstructor (new Type [1] { ti }));
119 return;
121 ig.Emit (OpCodes.Ldc_I4, words [0]);
122 ig.Emit (OpCodes.Ldc_I4, words [1]);
123 ig.Emit (OpCodes.Ldc_I4, words [2]);
124 // sign
125 ig.Emit (OpCodes.Ldc_I4, words [3] >> 31);
127 // power
128 ig.Emit (OpCodes.Ldc_I4, power);
130 ig.Emit (OpCodes.Newobj, typeof (Decimal).GetConstructor (new Type [5] { ti, ti, ti, typeof(bool), typeof(byte) }));
131 return;
134 case TypeCode.DateTime: {
135 var date = (DateTime) value;
136 var local = ig.DeclareLocal (typeof (DateTime));
138 ig.Emit (OpCodes.Ldloca, local);
139 ig.Emit (OpCodes.Ldc_I8, date.Ticks);
140 ig.Emit (OpCodes.Ldc_I4, (int) date.Kind);
141 ig.Emit (OpCodes.Call, typeof (DateTime).GetConstructor (new [] { typeof (long), typeof (DateTimeKind) }));
142 ig.Emit (OpCodes.Ldloc, local);
144 return;
147 case TypeCode.String:
148 EmitIfNotNull (ec, c => c.ig.Emit (OpCodes.Ldstr, (string) value));
149 return;
151 case TypeCode.Object:
152 EmitIfNotNull (ec, c => c.EmitReadGlobal (value));
153 return;
156 throw new NotImplementedException (String.Format ("No support for constants of type {0} yet", Type));
159 void EmitIfNotNull (EmitContext ec, Action<EmitContext> emit)
161 if (value == null) {
162 var ig = ec.ig;
164 if (Type.IsValueType) { // happens for nullable types
165 var local = ig.DeclareLocal (Type);
166 ig.Emit (OpCodes.Ldloca, local);
167 ig.Emit (OpCodes.Initobj, Type);
168 ig.Emit (OpCodes.Ldloc, local);
169 } else {
170 ec.ig.Emit (OpCodes.Ldnull);
173 return;
176 emit (ec);