CodeLiteral.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. //
  2. // Permission is hereby granted, free of charge, to any person obtaining
  3. // a copy of this software and associated documentation files (the
  4. // "Software"), to deal in the Software without restriction, including
  5. // without limitation the rights to use, copy, modify, merge, publish,
  6. // distribute, sublicense, and/or sell copies of the Software, and to
  7. // permit persons to whom the Software is furnished to do so, subject to
  8. // the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be
  11. // included in all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  15. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  17. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  18. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  19. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. //
  21. // Copyright (C) Lluis Sanchez Gual, 2004
  22. //
  23. using System;
  24. using System.Globalization;
  25. using System.Reflection;
  26. using System.Reflection.Emit;
  27. namespace Mono.CodeGeneration
  28. {
  29. public class CodeLiteral: CodeExpression
  30. {
  31. object value;
  32. Type type;
  33. public CodeLiteral (object value)
  34. {
  35. this.value = value;
  36. if (value != null) type = value.GetType ();
  37. else type = typeof(object);
  38. }
  39. public CodeLiteral (object value, Type type)
  40. {
  41. this.value = value;
  42. this.type = type;
  43. }
  44. public object Value {
  45. get { return value; }
  46. }
  47. public override void Generate (ILGenerator gen)
  48. {
  49. object value = this.value;
  50. if (value == null)
  51. {
  52. gen.Emit (OpCodes.Ldnull);
  53. return;
  54. }
  55. if (value is Enum)
  56. value = Convert.ChangeType (value, (value.GetType().UnderlyingSystemType), CultureInfo.InvariantCulture);
  57. if (value is Type) {
  58. gen.Emit (OpCodes.Ldtoken, (Type)value);
  59. gen.Emit (OpCodes.Call, typeof(Type).GetMethod ("GetTypeFromHandle"));
  60. return;
  61. }
  62. switch (Type.GetTypeCode (type))
  63. {
  64. case TypeCode.String:
  65. gen.Emit (OpCodes.Ldstr, (string)value);
  66. break;
  67. case TypeCode.Byte:
  68. case TypeCode.SByte:
  69. case TypeCode.Int16:
  70. case TypeCode.UInt16:
  71. case TypeCode.Int32:
  72. case TypeCode.UInt32:
  73. int i = (int)value;
  74. switch (i)
  75. {
  76. case 0: gen.Emit (OpCodes.Ldc_I4_0); break;
  77. case 1: gen.Emit (OpCodes.Ldc_I4_1); break;
  78. case 2: gen.Emit (OpCodes.Ldc_I4_2); break;
  79. case 3: gen.Emit (OpCodes.Ldc_I4_3); break;
  80. case 4: gen.Emit (OpCodes.Ldc_I4_4); break;
  81. case 5: gen.Emit (OpCodes.Ldc_I4_5); break;
  82. case 6: gen.Emit (OpCodes.Ldc_I4_6); break;
  83. case 7: gen.Emit (OpCodes.Ldc_I4_7); break;
  84. case 8: gen.Emit (OpCodes.Ldc_I4_8); break;
  85. case -1: gen.Emit (OpCodes.Ldc_I4_M1); break;
  86. default: gen.Emit (OpCodes.Ldc_I4, i); break;
  87. }
  88. break;
  89. case TypeCode.Int64:
  90. case TypeCode.UInt64:
  91. gen.Emit (OpCodes.Ldc_I8, (long)value);
  92. break;
  93. case TypeCode.Single:
  94. gen.Emit (OpCodes.Ldc_R4, (float)value);
  95. break;
  96. case TypeCode.Double:
  97. gen.Emit (OpCodes.Ldc_R8, (double)value);
  98. break;
  99. case TypeCode.Boolean:
  100. if ((bool)value)
  101. gen.Emit (OpCodes.Ldc_I4_1);
  102. else
  103. gen.Emit (OpCodes.Ldc_I4_0);
  104. break;
  105. default:
  106. throw new InvalidOperationException ("Literal type " + value.GetType() + " not supported");
  107. }
  108. }
  109. public override void PrintCode (CodeWriter cp)
  110. {
  111. if (value is string)
  112. cp.Write ("\"").Write (value.ToString ()).Write ("\"");
  113. else if (value == null)
  114. cp.Write ("null");
  115. else if (value is Type)
  116. cp.Write ("typeof(" + ((Type)value).Name + ")");
  117. else if (value is Enum)
  118. cp.Write (value.GetType().Name + "." + value);
  119. else
  120. cp.Write (value.ToString ());
  121. }
  122. public override Type GetResultType ()
  123. {
  124. return type;
  125. }
  126. }
  127. }