CodeCast.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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.Reflection;
  26. using System.Reflection.Emit;
  27. using Mono.CodeGeneration;
  28. namespace Mono.CodeGeneration
  29. {
  30. public class CodeCast: CodeExpression
  31. {
  32. Type type;
  33. CodeExpression exp;
  34. public CodeCast (Type type, CodeExpression exp)
  35. {
  36. this.type = type;
  37. this.exp = exp;
  38. }
  39. public override void Generate (ILGenerator gen)
  40. {
  41. exp.Generate (gen);
  42. Type typeObj = exp.GetResultType ();
  43. if (type.IsAssignableFrom (typeObj)) {
  44. if (typeObj.IsValueType)
  45. gen.Emit (OpCodes.Box, typeObj);
  46. return;
  47. }
  48. else if (type.IsValueType && typeObj == typeof(object)) {
  49. // Unbox
  50. gen.Emit (OpCodes.Unbox, type);
  51. CodeGenerationHelper.LoadFromPtr (gen, type);
  52. return;
  53. }
  54. else if (typeObj.IsAssignableFrom (type)) {
  55. // Sub s = (Sub)base
  56. gen.Emit (OpCodes.Castclass, type);
  57. return;
  58. }
  59. else if (CodeGenerationHelper.IsNumber (type) && CodeGenerationHelper.IsNumber (typeObj)) {
  60. switch (Type.GetTypeCode (type))
  61. {
  62. case TypeCode.Byte:
  63. gen.Emit (OpCodes.Conv_U1);
  64. return;
  65. case TypeCode.Double:
  66. gen.Emit (OpCodes.Conv_R8);
  67. return;
  68. case TypeCode.Int16:
  69. gen.Emit (OpCodes.Conv_I2);
  70. return;
  71. case TypeCode.Int32:
  72. gen.Emit (OpCodes.Conv_I4);
  73. return;
  74. case TypeCode.Int64:
  75. gen.Emit (OpCodes.Conv_I8);
  76. return;
  77. case TypeCode.SByte:
  78. gen.Emit (OpCodes.Conv_I1);
  79. return;
  80. case TypeCode.Single:
  81. gen.Emit (OpCodes.Conv_R4);
  82. return;
  83. case TypeCode.UInt16:
  84. gen.Emit (OpCodes.Conv_U2);
  85. return;
  86. case TypeCode.UInt32:
  87. gen.Emit (OpCodes.Conv_U4);
  88. return;
  89. case TypeCode.UInt64:
  90. gen.Emit (OpCodes.Conv_U8);
  91. return;
  92. }
  93. }
  94. MethodInfo imp = type.GetMethod ("op_Implicit", new Type[] { typeObj });
  95. if (imp != null) {
  96. gen.Emit (OpCodes.Call, imp);
  97. return;
  98. }
  99. foreach (MethodInfo m in typeObj.GetMember ("op_Explicit"))
  100. if (m.ReturnType == type) {
  101. gen.Emit (OpCodes.Call, m);
  102. return;
  103. }
  104. throw new InvalidOperationException ("Can't cast from " + typeObj + " to " + type);
  105. }
  106. public override void PrintCode (CodeWriter cp)
  107. {
  108. Type typeObj = exp.GetResultType ();
  109. if (type.IsAssignableFrom (typeObj)) {
  110. exp.PrintCode (cp);
  111. return;
  112. }
  113. cp.Write ("((" + type.FullName + ") ");
  114. exp.PrintCode (cp);
  115. cp.Write (")");
  116. }
  117. public override Type GetResultType ()
  118. {
  119. return type;
  120. }
  121. }
  122. }
  123. #endif