ConstantExpression.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. ILGenerator ig = ec.ig;
  50. switch (Type.GetTypeCode (Type)){
  51. case TypeCode.Byte:
  52. ig.Emit (OpCodes.Ldc_I4, (int) ((byte)value));
  53. return;
  54. case TypeCode.SByte:
  55. ig.Emit (OpCodes.Ldc_I4, (int) ((sbyte)value));
  56. return;
  57. case TypeCode.Int16:
  58. ig.Emit (OpCodes.Ldc_I4, (int) ((short)value));
  59. return;
  60. case TypeCode.UInt16:
  61. ig.Emit (OpCodes.Ldc_I4, (int) ((ushort)value));
  62. return;
  63. case TypeCode.Int32:
  64. ig.Emit (OpCodes.Ldc_I4, (int) value);
  65. return;
  66. case TypeCode.UInt32:
  67. ig.Emit (OpCodes.Ldc_I4, unchecked ((int) ((uint)Value)));
  68. return;
  69. case TypeCode.Int64:
  70. ig.Emit (OpCodes.Ldc_I8, (long) value);
  71. return;
  72. case TypeCode.UInt64:
  73. ig.Emit (OpCodes.Ldc_I8, unchecked ((long) ((ulong)value)));
  74. return;
  75. case TypeCode.Boolean:
  76. if ((bool) Value)
  77. ig.Emit (OpCodes.Ldc_I4_1);
  78. else
  79. ec.ig.Emit (OpCodes.Ldc_I4_0);
  80. return;
  81. case TypeCode.Char:
  82. ig.Emit (OpCodes.Ldc_I4, (int) ((char) value));
  83. return;
  84. case TypeCode.Single:
  85. ig.Emit (OpCodes.Ldc_R4, (float) value);
  86. return;
  87. case TypeCode.Double:
  88. ig.Emit (OpCodes.Ldc_R8, (double) value);
  89. return;
  90. case TypeCode.Decimal: {
  91. Decimal v = (decimal) value;
  92. int [] words = Decimal.GetBits (v);
  93. int power = (words [3] >> 16) & 0xff;
  94. Type ti = typeof (int);
  95. if (power == 0 && v <= int.MaxValue && v >= int.MinValue) {
  96. ig.Emit (OpCodes.Ldc_I4, (int) v);
  97. ig.Emit (OpCodes.Newobj, typeof (Decimal).GetConstructor (new Type [1] { ti }));
  98. return;
  99. }
  100. ig.Emit (OpCodes.Ldc_I4, words [0]);
  101. ig.Emit (OpCodes.Ldc_I4, words [1]);
  102. ig.Emit (OpCodes.Ldc_I4, words [2]);
  103. // sign
  104. ig.Emit (OpCodes.Ldc_I4, words [3] >> 31);
  105. // power
  106. ig.Emit (OpCodes.Ldc_I4, power);
  107. ig.Emit (OpCodes.Newobj, typeof (Decimal).GetConstructor (new Type [5] { ti, ti, ti, typeof(bool), typeof(byte) }));
  108. return;
  109. }
  110. case TypeCode.String:
  111. ig.Emit (OpCodes.Ldstr, (string) value);
  112. return;
  113. case TypeCode.Object:
  114. break;
  115. }
  116. throw new NotImplementedException (String.Format ("No support for constants of type {0} yet", Type));
  117. }
  118. }
  119. }