ConstantExpression.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. //
  2. // ConstantExpression.cs
  3. //
  4. // Author:
  5. // Jb Evain ([email protected])
  6. // Miguel de Icaza ([email protected])
  7. //
  8. // Some code is based on the Mono C# compiler:
  9. // Marek Safar ([email protected])
  10. // Martin Baulig ([email protected])
  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. using System;
  34. using System.Reflection;
  35. #if !FULL_AOT_RUNTIME
  36. using System.Reflection.Emit;
  37. #endif
  38. namespace System.Linq.Expressions {
  39. public sealed class ConstantExpression : Expression {
  40. object value;
  41. public object Value {
  42. get { return value; }
  43. }
  44. internal ConstantExpression (object value, Type type)
  45. : base (ExpressionType.Constant, type)
  46. {
  47. this.value = value;
  48. }
  49. internal static bool IsNull (Expression e)
  50. {
  51. var c = e as ConstantExpression;
  52. return c != null && c.value == null;
  53. }
  54. #if !FULL_AOT_RUNTIME
  55. internal override void Emit (EmitContext ec)
  56. {
  57. if (Type.IsNullable ()) {
  58. EmitNullableConstant (ec, Type, value);
  59. return;
  60. }
  61. EmitConstant (ec, Type, value);
  62. }
  63. void EmitNullableConstant (EmitContext ec, Type type, object value)
  64. {
  65. if (value == null) {
  66. var local = ec.ig.DeclareLocal (type);
  67. ec.EmitNullableInitialize (local);
  68. } else {
  69. EmitConstant (ec, type.GetFirstGenericArgument (), value);
  70. ec.EmitNullableNew (type);
  71. }
  72. }
  73. void EmitConstant (EmitContext ec, Type type, object value)
  74. {
  75. var ig = ec.ig;
  76. switch (Type.GetTypeCode (type)){
  77. case TypeCode.Byte:
  78. ig.Emit (OpCodes.Ldc_I4, (int) ((byte)value));
  79. return;
  80. case TypeCode.SByte:
  81. ig.Emit (OpCodes.Ldc_I4, (int) ((sbyte)value));
  82. return;
  83. case TypeCode.Int16:
  84. ig.Emit (OpCodes.Ldc_I4, (int) ((short)value));
  85. return;
  86. case TypeCode.UInt16:
  87. ig.Emit (OpCodes.Ldc_I4, (int) ((ushort)value));
  88. return;
  89. case TypeCode.Int32:
  90. ig.Emit (OpCodes.Ldc_I4, (int) value);
  91. return;
  92. case TypeCode.UInt32:
  93. ig.Emit (OpCodes.Ldc_I4, unchecked ((int) ((uint)Value)));
  94. return;
  95. case TypeCode.Int64:
  96. ig.Emit (OpCodes.Ldc_I8, (long) value);
  97. return;
  98. case TypeCode.UInt64:
  99. ig.Emit (OpCodes.Ldc_I8, unchecked ((long) ((ulong)value)));
  100. return;
  101. case TypeCode.Boolean:
  102. if ((bool) Value)
  103. ig.Emit (OpCodes.Ldc_I4_1);
  104. else
  105. ec.ig.Emit (OpCodes.Ldc_I4_0);
  106. return;
  107. case TypeCode.Char:
  108. ig.Emit (OpCodes.Ldc_I4, (int) ((char) value));
  109. return;
  110. case TypeCode.Single:
  111. ig.Emit (OpCodes.Ldc_R4, (float) value);
  112. return;
  113. case TypeCode.Double:
  114. ig.Emit (OpCodes.Ldc_R8, (double) value);
  115. return;
  116. case TypeCode.Decimal: {
  117. Decimal v = (decimal) value;
  118. int [] words = Decimal.GetBits (v);
  119. int power = (words [3] >> 16) & 0xff;
  120. Type ti = typeof (int);
  121. if (power == 0 && v <= int.MaxValue && v >= int.MinValue) {
  122. ig.Emit (OpCodes.Ldc_I4, (int) v);
  123. ig.Emit (OpCodes.Newobj, typeof (Decimal).GetConstructor (new Type [1] { ti }));
  124. return;
  125. }
  126. ig.Emit (OpCodes.Ldc_I4, words [0]);
  127. ig.Emit (OpCodes.Ldc_I4, words [1]);
  128. ig.Emit (OpCodes.Ldc_I4, words [2]);
  129. // sign
  130. ig.Emit (OpCodes.Ldc_I4, words [3] >> 31);
  131. // power
  132. ig.Emit (OpCodes.Ldc_I4, power);
  133. ig.Emit (OpCodes.Newobj, typeof (Decimal).GetConstructor (new Type [5] { ti, ti, ti, typeof(bool), typeof(byte) }));
  134. return;
  135. }
  136. case TypeCode.DateTime: {
  137. var date = (DateTime) value;
  138. var local = ig.DeclareLocal (typeof (DateTime));
  139. ig.Emit (OpCodes.Ldloca, local);
  140. ig.Emit (OpCodes.Ldc_I8, date.Ticks);
  141. ig.Emit (OpCodes.Ldc_I4, (int) date.Kind);
  142. ig.Emit (OpCodes.Call, typeof (DateTime).GetConstructor (new [] { typeof (long), typeof (DateTimeKind) }));
  143. ig.Emit (OpCodes.Ldloc, local);
  144. return;
  145. }
  146. case TypeCode.DBNull:
  147. ig.Emit (OpCodes.Ldsfld, typeof (DBNull).GetField ("Value", BindingFlags.Public | BindingFlags.Static));
  148. return;
  149. case TypeCode.String:
  150. EmitIfNotNull (ec, c => c.ig.Emit (OpCodes.Ldstr, (string) value));
  151. return;
  152. case TypeCode.Object:
  153. EmitIfNotNull (ec, c => c.EmitReadGlobal (value));
  154. return;
  155. }
  156. throw new NotImplementedException (String.Format ("No support for constants of type {0} yet", Type));
  157. }
  158. void EmitIfNotNull (EmitContext ec, Action<EmitContext> emit)
  159. {
  160. if (value == null) {
  161. ec.ig.Emit (OpCodes.Ldnull);
  162. return;
  163. }
  164. emit (ec);
  165. }
  166. #endif
  167. }
  168. }