2
0

CodeCast.cs 3.5 KB

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