ConstantExpression.cs 5.2 KB

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