CodeWhen.cs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. namespace Mono.CodeGeneration
  28. {
  29. public class CodeWhen: CodeExpression
  30. {
  31. CodeExpression condition;
  32. CodeExpression trueBlock;
  33. CodeExpression falseBlock;
  34. public CodeWhen (CodeExpression condition, CodeExpression trueResult, CodeExpression falseResult)
  35. {
  36. this.condition = condition;
  37. if (condition.GetResultType () != typeof(bool))
  38. throw new InvalidOperationException ("Condition expression is not boolean");
  39. if (trueResult.GetResultType() != falseResult.GetResultType())
  40. throw new InvalidOperationException ("The types of the true and false expressions must be the same");
  41. trueBlock = trueResult;
  42. falseBlock = falseResult;
  43. }
  44. public override void Generate (ILGenerator gen)
  45. {
  46. Label falseLabel = gen.DefineLabel ();
  47. Label endLabel = gen.DefineLabel ();
  48. GenerateCondition (gen, falseLabel);
  49. trueBlock.Generate (gen);
  50. gen.Emit (OpCodes.Br, endLabel);
  51. gen.MarkLabel(falseLabel);
  52. falseBlock.Generate (gen);
  53. gen.MarkLabel(endLabel);
  54. }
  55. void GenerateCondition (ILGenerator gen, Label falseLabel)
  56. {
  57. if (condition is CodeConditionExpression)
  58. ((CodeConditionExpression)condition).GenerateForBranch (gen, falseLabel, false);
  59. else {
  60. condition.Generate (gen);
  61. gen.Emit (OpCodes.Brfalse, falseLabel);
  62. }
  63. }
  64. public override void PrintCode (CodeWriter cp)
  65. {
  66. cp.Write ("(");
  67. condition.PrintCode (cp);
  68. cp.Write (") ? ");
  69. trueBlock.PrintCode (cp);
  70. cp.Write (" : ");
  71. falseBlock.PrintCode (cp);
  72. }
  73. public override Type GetResultType ()
  74. {
  75. return trueBlock.GetResultType();
  76. }
  77. }
  78. }
  79. #endif