CodeIncrement.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 CodeIncrement: CodeValueReference
  29. {
  30. CodeValueReference exp;
  31. public CodeIncrement (CodeValueReference exp)
  32. {
  33. this.exp = exp;
  34. }
  35. public override void Generate (ILGenerator gen)
  36. {
  37. exp.GenerateSet (gen, new CodeAddOne (exp));
  38. exp.Generate (gen);
  39. }
  40. public override void GenerateAsStatement (ILGenerator gen)
  41. {
  42. exp.GenerateSet (gen, new CodeAddOne (exp));
  43. }
  44. public override void GenerateSet (ILGenerator gen, CodeExpression value)
  45. {
  46. exp.GenerateSet (gen, value);
  47. }
  48. public override void PrintCode (CodeWriter cp)
  49. {
  50. exp.PrintCode (cp);
  51. cp.Write ("++");
  52. }
  53. public override Type GetResultType ()
  54. {
  55. return exp.GetResultType();
  56. }
  57. }
  58. public class CodeAddOne: CodeExpression
  59. {
  60. CodeValueReference exp;
  61. MethodInfo incMet;
  62. public CodeAddOne (CodeValueReference exp)
  63. {
  64. this.exp = exp;
  65. if (!exp.IsNumber) {
  66. incMet = exp.GetResultType ().GetMethod ("op_Increment");
  67. if (incMet == null)
  68. throw new InvalidOperationException ("Operator '++' cannot be applied to operand of type '" + exp.GetResultType().FullName + "'");
  69. }
  70. }
  71. public override void Generate (ILGenerator gen)
  72. {
  73. if (incMet != null) {
  74. CodeGenerationHelper.GenerateMethodCall (gen, null, incMet, exp);
  75. return;
  76. }
  77. exp.Generate (gen);
  78. Type t = exp.GetResultType ();
  79. switch (Type.GetTypeCode (t))
  80. {
  81. case TypeCode.Byte:
  82. gen.Emit (OpCodes.Ldc_I4_1);
  83. gen.Emit (OpCodes.Add);
  84. gen.Emit (OpCodes.Conv_U1);
  85. break;
  86. case TypeCode.Double:
  87. gen.Emit (OpCodes.Ldc_R8, 1);
  88. gen.Emit (OpCodes.Add);
  89. break;
  90. case TypeCode.Int16:
  91. gen.Emit (OpCodes.Ldc_I4_1);
  92. gen.Emit (OpCodes.Add);
  93. gen.Emit (OpCodes.Conv_I2);
  94. break;
  95. case TypeCode.UInt32:
  96. case TypeCode.Int32:
  97. gen.Emit (OpCodes.Ldc_I4_1);
  98. gen.Emit (OpCodes.Add);
  99. break;
  100. case TypeCode.UInt64:
  101. case TypeCode.Int64:
  102. gen.Emit (OpCodes.Ldc_I4_1);
  103. gen.Emit (OpCodes.Add);
  104. gen.Emit (OpCodes.Conv_U8);
  105. break;
  106. case TypeCode.SByte:
  107. gen.Emit (OpCodes.Ldc_I4_1);
  108. gen.Emit (OpCodes.Add);
  109. gen.Emit (OpCodes.Conv_I1);
  110. break;
  111. case TypeCode.Single:
  112. gen.Emit (OpCodes.Ldc_R4, 1);
  113. gen.Emit (OpCodes.Add);
  114. break;
  115. case TypeCode.UInt16:
  116. gen.Emit (OpCodes.Ldc_I4_1);
  117. gen.Emit (OpCodes.Add);
  118. gen.Emit (OpCodes.Conv_U2);
  119. break;
  120. }
  121. }
  122. public override void PrintCode (CodeWriter cp)
  123. {
  124. exp.PrintCode (cp);
  125. cp.Write (" + 1");
  126. }
  127. public override Type GetResultType ()
  128. {
  129. return exp.GetResultType();
  130. }
  131. }
  132. }