ConstantExpression.cs 5.2 KB

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