CodeTryBlock.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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) 2009 Novell, Inc
  22. //
  23. #if !FULL_AOT_RUNTIME
  24. using System;
  25. using System.Collections;
  26. using System.Collections.Generic;
  27. using System.Reflection;
  28. using System.Reflection.Emit;
  29. using DictionaryEntry = System.Collections.Generic.KeyValuePair<
  30. Mono.CodeGeneration.CodeVariableDeclaration,
  31. Mono.CodeGeneration.CodeBlock>;
  32. using ArrayList = System.Collections.Generic.List<
  33. System.Collections.Generic.KeyValuePair<
  34. Mono.CodeGeneration.CodeVariableDeclaration,
  35. Mono.CodeGeneration.CodeBlock>>;
  36. namespace Mono.CodeGeneration
  37. {
  38. public class CodeTry: CodeStatement
  39. {
  40. CodeExpression condition;
  41. CodeBlock tryBlock, finallyBlock;
  42. ArrayList catchBlocks = new ArrayList ();
  43. public CodeTry ()
  44. {
  45. tryBlock = new CodeBlock ();
  46. catchBlocks = new ArrayList ();
  47. finallyBlock = new CodeBlock ();
  48. }
  49. public override void Generate (ILGenerator gen)
  50. {
  51. gen.BeginExceptionBlock ();
  52. tryBlock.Generate (gen);
  53. foreach (DictionaryEntry de in catchBlocks) {
  54. CodeVariableDeclaration vd = (CodeVariableDeclaration) de.Key;
  55. gen.BeginCatchBlock (vd.Variable.Type);
  56. if (vd.Variable.Name.Length > 0) {
  57. vd.Generate (gen);
  58. // FIXME: assign exception to this local declaration
  59. }
  60. ((CodeBlock) de.Value).Generate (gen);
  61. }
  62. if (!finallyBlock.IsEmpty) {
  63. gen.BeginFinallyBlock ();
  64. finallyBlock.Generate (gen);
  65. }
  66. gen.EndExceptionBlock ();
  67. }
  68. public override void PrintCode (CodeWriter cp)
  69. {
  70. if (tryBlock == null) return;
  71. cp.Write ("try {");
  72. cp.Indent ();
  73. condition.PrintCode (cp);
  74. cp.Unindent ();
  75. foreach (DictionaryEntry de in catchBlocks) {
  76. CodeVariableDeclaration vd = (CodeVariableDeclaration) de.Key;
  77. cp.Write ("} catch (");
  78. if (vd.Variable.Name.Length > 0)
  79. vd.PrintCode (cp);
  80. else
  81. cp.Write (vd.Variable.Type.FullName);
  82. cp.Write (") {");
  83. cp.Indent ();
  84. ((CodeBlock) de.Value).PrintCode (cp);
  85. cp.Unindent ();
  86. }
  87. if (!finallyBlock.IsEmpty) {
  88. cp.Write ("} finally {");
  89. cp.Indent ();
  90. finallyBlock.PrintCode (cp);
  91. cp.Unindent ();
  92. }
  93. cp.Write ("}");
  94. }
  95. public CodeBlock TryBlock
  96. {
  97. get { return tryBlock; }
  98. set { tryBlock = value; }
  99. }
  100. public ArrayList CatchBlocks
  101. {
  102. get { return catchBlocks; }
  103. }
  104. public CodeBlock FinallyBlock
  105. {
  106. get { return finallyBlock; }
  107. set { finallyBlock = value; }
  108. }
  109. }
  110. }
  111. #endif