CodeLiteral.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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.Reflection;
  25. using System.Reflection.Emit;
  26. namespace Mono.CodeGeneration
  27. {
  28. public class CodeLiteral: CodeExpression
  29. {
  30. object value;
  31. Type type;
  32. public CodeLiteral (object value)
  33. {
  34. this.value = value;
  35. if (value != null) type = value.GetType ();
  36. else type = typeof(object);
  37. }
  38. public CodeLiteral (object value, Type type)
  39. {
  40. this.value = value;
  41. this.type = type;
  42. }
  43. public object Value {
  44. get { return value; }
  45. }
  46. public override void Generate (ILGenerator gen)
  47. {
  48. object value = this.value;
  49. if (value == null)
  50. {
  51. gen.Emit (OpCodes.Ldnull);
  52. return;
  53. }
  54. if (value is Enum)
  55. value = Convert.ChangeType (value, (value.GetType().UnderlyingSystemType));
  56. if (value is Type) {
  57. gen.Emit (OpCodes.Ldtoken, (Type)value);
  58. gen.Emit (OpCodes.Call, typeof(Type).GetMethod ("GetTypeFromHandle"));
  59. return;
  60. }
  61. switch (Type.GetTypeCode (type))
  62. {
  63. case TypeCode.String:
  64. gen.Emit (OpCodes.Ldstr, (string)value);
  65. break;
  66. case TypeCode.Byte:
  67. case TypeCode.SByte:
  68. case TypeCode.Int16:
  69. case TypeCode.UInt16:
  70. case TypeCode.Int32:
  71. case TypeCode.UInt32:
  72. int i = (int)value;
  73. switch (i)
  74. {
  75. case 0: gen.Emit (OpCodes.Ldc_I4_0); break;
  76. case 1: gen.Emit (OpCodes.Ldc_I4_1); break;
  77. case 2: gen.Emit (OpCodes.Ldc_I4_2); break;
  78. case 3: gen.Emit (OpCodes.Ldc_I4_3); break;
  79. case 4: gen.Emit (OpCodes.Ldc_I4_4); break;
  80. case 5: gen.Emit (OpCodes.Ldc_I4_5); break;
  81. case 6: gen.Emit (OpCodes.Ldc_I4_6); break;
  82. case 7: gen.Emit (OpCodes.Ldc_I4_7); break;
  83. case 8: gen.Emit (OpCodes.Ldc_I4_8); break;
  84. case -1: gen.Emit (OpCodes.Ldc_I4_M1); break;
  85. default: gen.Emit (OpCodes.Ldc_I4, i); break;
  86. }
  87. break;
  88. case TypeCode.Int64:
  89. case TypeCode.UInt64:
  90. gen.Emit (OpCodes.Ldc_I8, (long)value);
  91. break;
  92. case TypeCode.Single:
  93. gen.Emit (OpCodes.Ldc_R4, (float)value);
  94. break;
  95. case TypeCode.Double:
  96. gen.Emit (OpCodes.Ldc_R8, (double)value);
  97. break;
  98. case TypeCode.Boolean:
  99. if ((bool)value)
  100. gen.Emit (OpCodes.Ldc_I4_1);
  101. else
  102. gen.Emit (OpCodes.Ldc_I4_0);
  103. break;
  104. default:
  105. throw new InvalidOperationException ("Literal type " + value.GetType() + " not supported");
  106. }
  107. }
  108. public override void PrintCode (CodeWriter cp)
  109. {
  110. if (value is string)
  111. cp.Write ("\"").Write (value.ToString ()).Write ("\"");
  112. else if (value == null)
  113. cp.Write ("null");
  114. else if (value is Type)
  115. cp.Write ("typeof(" + ((Type)value).Name + ")");
  116. else if (value is Enum)
  117. cp.Write (value.GetType().Name + "." + value);
  118. else
  119. cp.Write (value.ToString ());
  120. }
  121. public override Type GetResultType ()
  122. {
  123. return type;
  124. }
  125. }
  126. }