CodeTryBlock.cs 3.5 KB

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