ConstantExpression.cs 5.1 KB

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