CodeTryBlock.cs 3.5 KB

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