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:
10 // The above copyright notice and this permission notice shall be
11 // included in all copies or substantial portions of the Software.
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.
21 // Copyright (C) 2009 Novell, Inc
26 using System.Collections;
28 using System.Collections.Generic;
30 using System.Collections.Specialized;
32 using System.Reflection;
33 using System.Reflection.Emit;
36 using DictionaryEntry = System.Collections.Generic.KeyValuePair<
37 Mono.CodeGeneration.CodeVariableDeclaration,
38 Mono.CodeGeneration.CodeBlock>;
39 using ArrayList = System.Collections.Generic.List<
40 System.Collections.Generic.KeyValuePair<
41 Mono.CodeGeneration.CodeVariableDeclaration,
42 Mono.CodeGeneration.CodeBlock>>;
45 namespace Mono.CodeGeneration
47 public class CodeTry: CodeStatement
49 CodeExpression condition;
50 CodeBlock tryBlock, finallyBlock;
51 ArrayList catchBlocks = new ArrayList ();
55 tryBlock = new CodeBlock ();
56 catchBlocks = new ArrayList ();
57 finallyBlock = new CodeBlock ();
60 public override void Generate (ILGenerator gen)
62 gen.BeginExceptionBlock ();
63 tryBlock.Generate (gen);
64 foreach (DictionaryEntry de in catchBlocks) {
65 CodeVariableDeclaration vd = (CodeVariableDeclaration) de.Key;
66 gen.BeginCatchBlock (vd.Variable.Type);
67 if (vd.Variable.Name.Length > 0) {
69 // FIXME: assign exception to this local declaration
71 ((CodeBlock) de.Value).Generate (gen);
73 if (!finallyBlock.IsEmpty) {
74 gen.BeginFinallyBlock ();
75 finallyBlock.Generate (gen);
77 gen.EndExceptionBlock ();
80 public override void PrintCode (CodeWriter cp)
82 if (tryBlock == null) return;
86 condition.PrintCode (cp);
88 foreach (DictionaryEntry de in catchBlocks) {
89 CodeVariableDeclaration vd = (CodeVariableDeclaration) de.Key;
90 cp.Write ("} catch (");
91 if (vd.Variable.Name.Length > 0)
94 cp.Write (vd.Variable.Type.FullName);
97 ((CodeBlock) de.Value).PrintCode (cp);
100 if (!finallyBlock.IsEmpty) {
101 cp.Write ("} finally {");
103 finallyBlock.PrintCode (cp);
109 public CodeBlock TryBlock
111 get { return tryBlock; }
112 set { tryBlock = value; }
115 public ArrayList CatchBlocks
117 get { return catchBlocks; }
120 public CodeBlock FinallyBlock
122 get { return finallyBlock; }
123 set { finallyBlock = value; }