Merge pull request #409 from Alkarex/patch-1
[mono.git] / mcs / class / System.Core / System.Linq.Expressions / ConstantExpression.cs
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)
11 //
12 // (C) 2001-2008 Novell, Inc. (http://www.novell.com)
13 //
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:
21 //
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 //
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.
32 //
33
34 using System;
35 using System.Reflection;
36 using System.Reflection.Emit;
37
38 namespace System.Linq.Expressions {
39
40         public sealed class ConstantExpression : Expression {
41                 object value;
42
43                 public object Value {
44                         get { return value; }
45                 }
46
47                 internal ConstantExpression (object value, Type type)
48                         : base (ExpressionType.Constant, type)
49                 {
50                         this.value = value;
51                 }
52
53 #if !FULL_AOT_RUNTIME
54                 internal override void Emit (EmitContext ec)
55                 {
56                         if (Type.IsNullable ()) {
57                                 EmitNullableConstant (ec, Type, value);
58                                 return;
59                         }
60
61                         EmitConstant (ec, Type, value);
62                 }
63
64                 void EmitNullableConstant (EmitContext ec, Type type, object value)
65                 {
66                         if (value == null) {
67                                 var local = ec.ig.DeclareLocal (type);
68                                 ec.EmitNullableInitialize (local);
69                         } else {
70                                 EmitConstant (ec, type.GetFirstGenericArgument (), value);
71                                 ec.EmitNullableNew (type);
72                         }
73                 }
74
75                 void EmitConstant (EmitContext ec, Type type, object value)
76                 {
77                         var ig = ec.ig;
78
79                         switch (Type.GetTypeCode (type)){
80                         case TypeCode.Byte:
81                                 ig.Emit (OpCodes.Ldc_I4, (int) ((byte)value));
82                                 return;
83
84                         case TypeCode.SByte:
85                                 ig.Emit (OpCodes.Ldc_I4, (int) ((sbyte)value));
86                                 return;
87
88                         case TypeCode.Int16:
89                                 ig.Emit (OpCodes.Ldc_I4, (int) ((short)value));
90                                 return;
91
92                         case TypeCode.UInt16:
93                                 ig.Emit (OpCodes.Ldc_I4, (int) ((ushort)value));
94                                 return;
95
96                         case TypeCode.Int32:
97                                 ig.Emit (OpCodes.Ldc_I4, (int) value);
98                                 return;
99
100                         case TypeCode.UInt32:
101                                 ig.Emit (OpCodes.Ldc_I4, unchecked ((int) ((uint)Value)));
102                                 return;
103
104                         case TypeCode.Int64:
105                                 ig.Emit (OpCodes.Ldc_I8, (long) value);
106                                 return;
107
108                         case TypeCode.UInt64:
109                                 ig.Emit (OpCodes.Ldc_I8, unchecked ((long) ((ulong)value)));
110                                 return;
111
112                         case TypeCode.Boolean:
113                                 if ((bool) Value)
114                                         ig.Emit (OpCodes.Ldc_I4_1);
115                                 else
116                                         ec.ig.Emit (OpCodes.Ldc_I4_0);
117                                 return;
118
119                         case TypeCode.Char:
120                                 ig.Emit (OpCodes.Ldc_I4, (int) ((char) value));
121                                 return;
122
123                         case TypeCode.Single:
124                                 ig.Emit (OpCodes.Ldc_R4, (float) value);
125                                 return;
126
127                         case TypeCode.Double:
128                                 ig.Emit (OpCodes.Ldc_R8, (double) value);
129                                 return;
130
131                         case TypeCode.Decimal: {
132                                 Decimal v = (decimal) value;
133                                 int [] words = Decimal.GetBits (v);
134                                 int power = (words [3] >> 16) & 0xff;
135                                 Type ti = typeof (int);
136
137                                 if (power == 0 && v <= int.MaxValue && v >= int.MinValue) {
138                                         ig.Emit (OpCodes.Ldc_I4, (int) v);
139
140                                         ig.Emit (OpCodes.Newobj, typeof (Decimal).GetConstructor (new Type [1] { ti }));
141                                         return;
142                                 }
143                                 ig.Emit (OpCodes.Ldc_I4, words [0]);
144                                 ig.Emit (OpCodes.Ldc_I4, words [1]);
145                                 ig.Emit (OpCodes.Ldc_I4, words [2]);
146                                 // sign
147                                 ig.Emit (OpCodes.Ldc_I4, words [3] >> 31);
148
149                                 // power
150                                 ig.Emit (OpCodes.Ldc_I4, power);
151
152                                 ig.Emit (OpCodes.Newobj, typeof (Decimal).GetConstructor (new Type [5] { ti, ti, ti, typeof(bool), typeof(byte) }));
153                                 return;
154                         }
155
156                         case TypeCode.DateTime: {
157                                 var date = (DateTime) value;
158                                 var local = ig.DeclareLocal (typeof (DateTime));
159
160                                 ig.Emit (OpCodes.Ldloca, local);
161                                 ig.Emit (OpCodes.Ldc_I8, date.Ticks);
162                                 ig.Emit (OpCodes.Ldc_I4, (int) date.Kind);
163                                 ig.Emit (OpCodes.Call, typeof (DateTime).GetConstructor (new [] { typeof (long), typeof (DateTimeKind) }));
164                                 ig.Emit (OpCodes.Ldloc, local);
165
166                                 return;
167                         }
168
169                         case TypeCode.DBNull:
170                                 ig.Emit (OpCodes.Ldsfld, typeof (DBNull).GetField ("Value", BindingFlags.Public | BindingFlags.Static));
171                                 return;
172
173                         case TypeCode.String:
174                                 EmitIfNotNull (ec, c => c.ig.Emit (OpCodes.Ldstr, (string) value));
175                                 return;
176
177                         case TypeCode.Object:
178                                 EmitIfNotNull (ec, c => c.EmitReadGlobal (value));
179                                 return;
180                         }
181
182                         throw new NotImplementedException (String.Format ("No support for constants of type {0} yet", Type));
183                 }
184
185                 void EmitIfNotNull (EmitContext ec, Action<EmitContext> emit)
186                 {
187                         if (value == null) {
188                                 ec.ig.Emit (OpCodes.Ldnull);
189                                 return;
190                         }
191
192                         emit (ec);
193                 }
194 #endif
195         }
196 }