CodeLiteral.cs 4.0 KB

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