Merge pull request #799 from kebby/master
[mono.git] / mcs / class / System.ServiceModel / Mono.CodeGeneration / CodeTryBlock.cs
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
24 #if !FULL_AOT_RUNTIME
25 using System;
26 using System.Collections;
27 #if NET_2_0
28 using System.Collections.Generic;
29 #else
30 using System.Collections.Specialized;
31 #endif
32 using System.Reflection;
33 using System.Reflection.Emit;
34
35 #if NET_2_0
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>>;
43 #endif
44
45 namespace Mono.CodeGeneration
46 {
47         public class CodeTry: CodeStatement
48         {
49                 CodeExpression condition;
50                 CodeBlock tryBlock, finallyBlock;
51                 ArrayList catchBlocks = new ArrayList ();
52                 
53                 public CodeTry ()
54                 {
55                         tryBlock = new CodeBlock ();
56                         catchBlocks = new ArrayList ();
57                         finallyBlock = new CodeBlock ();
58                 }
59                 
60                 public override void Generate (ILGenerator gen)
61                 {
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) {
68                                         vd.Generate (gen);
69                                         // FIXME: assign exception to this local declaration
70                                 }
71                                 ((CodeBlock) de.Value).Generate (gen);
72                         }
73                         if (!finallyBlock.IsEmpty) {
74                                 gen.BeginFinallyBlock ();
75                                 finallyBlock.Generate (gen);
76                         }
77                         gen.EndExceptionBlock ();
78                 }
79                                 
80                 public override void PrintCode (CodeWriter cp)
81                 {
82                         if (tryBlock == null) return;
83                         
84                         cp.Write ("try {");
85                         cp.Indent ();
86                         condition.PrintCode (cp);
87                         cp.Unindent ();
88                         foreach (DictionaryEntry de in catchBlocks) {
89                                 CodeVariableDeclaration vd = (CodeVariableDeclaration) de.Key;
90                                 cp.Write ("} catch (");
91                                 if (vd.Variable.Name.Length > 0)
92                                         vd.PrintCode (cp);
93                                 else
94                                         cp.Write (vd.Variable.Type.FullName);
95                                 cp.Write (") {");
96                                 cp.Indent ();
97                                 ((CodeBlock) de.Value).PrintCode (cp);
98                                 cp.Unindent ();
99                         }
100                         if (!finallyBlock.IsEmpty) {
101                                 cp.Write ("} finally {");
102                                 cp.Indent ();
103                                 finallyBlock.PrintCode (cp);
104                                 cp.Unindent ();
105                         }
106                         cp.Write ("}");
107                 }
108                 
109                 public CodeBlock TryBlock
110                 {
111                         get { return tryBlock; }
112                         set { tryBlock = value; }
113                 }
114
115                 public ArrayList CatchBlocks
116                 {
117                         get { return catchBlocks; }
118                 }
119                 
120                 public CodeBlock FinallyBlock
121                 {
122                         get { return finallyBlock; }
123                         set { finallyBlock = value; }
124                 }
125         }
126 }
127 #endif