annotate dmd/TryCatchStatement.d @ 132:c494af1dba80

Fixes for dmd 2.037
author Eldar Insafutdinov <e.insafutdinov@gmail.com>
date Fri, 10 Sep 2010 19:14:09 +0100
parents e28b18c23469
children af724d3510d7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1 module dmd.TryCatchStatement;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
2
114
e28b18c23469 added a module dmd.common for commonly used stuff
Trass3r
parents: 72
diff changeset
3 import dmd.common;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
4 import dmd.Statement;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
5 import dmd.Array;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
6 import dmd.Loc;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
7 import dmd.Id;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
8 import dmd.Identifier;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
9 import dmd.Scope;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
10 import dmd.InlineScanState;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
11 import dmd.IRState;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
12 import dmd.OutBuffer;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
13 import dmd.Catch;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
14 import dmd.HdrGenState;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
15 import dmd.BE;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
16
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
17 import dmd.backend.BC;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
18 import dmd.codegen.Util;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
19 import dmd.backend.Util;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
20 import dmd.backend.Blockx;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
21 import dmd.backend.block;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
22 import dmd.backend.mTY;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
23 import dmd.backend.TYM;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
24
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
25 class TryCatchStatement : Statement
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
26 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
27 Statement body_;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
28 Array catches;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
29
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
30 this(Loc loc, Statement body_, Array catches)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
31 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
32 super(loc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
33 this.body_ = body_;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
34 this.catches = catches;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
35 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
36
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 63
diff changeset
37 override Statement syntaxCopy()
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
38 {
63
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
39 Array a = new Array();
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
40 a.setDim(catches.dim);
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
41 for (int i = 0; i < a.dim; i++)
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
42 {
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
43 Catch c;
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
44
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
45 c = cast(Catch)catches.data[i];
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
46 c = c.syntaxCopy();
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
47 a.data[i] = cast(void*)c;
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
48 }
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
49 TryCatchStatement s = new TryCatchStatement(loc, body_.syntaxCopy(), a);
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
50 return s;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
51 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
52
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 63
diff changeset
53 override Statement semantic(Scope sc)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
54 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
55 body_ = body_.semanticScope(sc, null /*this*/, null);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
56
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
57 /* Even if body is null, still do semantic analysis on catches
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
58 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
59 for (size_t i = 0; i < catches.dim; i++)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
60 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
61 Catch c = cast(Catch)catches.data[i];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
62 c.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
63
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
64 // Determine if current catch 'hides' any previous catches
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
65 for (size_t j = 0; j < i; j++)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
66 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
67 Catch cj = cast(Catch)catches.data[j];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
68 string si = c.loc.toChars();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
69 string sj = cj.loc.toChars();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
70
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
71 if (c.type.toBasetype().implicitConvTo(cj.type.toBasetype()))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
72 error("catch at %s hides catch at %s", sj, si);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
73 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
74 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
75
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
76 if (!body_ || body_.isEmpty())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
77 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
78 return null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
79 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
80 return this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
81 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
82
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 63
diff changeset
83 override bool hasBreak()
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
84 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
85 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
86 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
87
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 63
diff changeset
88 override bool usesEH()
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
89 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
90 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
91 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
92
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 63
diff changeset
93 override BE blockExit()
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
94 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
95 assert(body_);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
96 BE result = body_.blockExit();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
97
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
98 BE catchresult = BE.BEnone;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
99 for (size_t i = 0; i < catches.dim; i++)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
100 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
101 Catch c = cast(Catch)catches.data[i];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
102 catchresult |= c.blockExit();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
103
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
104 /* If we're catching Object, then there is no throwing
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
105 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
106 Identifier id = c.type.toBasetype().isClassHandle().ident;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
107 if (i == 0 &&
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
108 (id is Id.Object_ || id is Id.Throwable || id is Id.Exception))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
109 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
110 result &= ~BE.BEthrow;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
111 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
112 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
113 return result | catchresult;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
114 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
115
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 63
diff changeset
116 override Statement inlineScan(InlineScanState* iss)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
117 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
118 if (body_)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
119 body_ = body_.inlineScan(iss);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
120 if (catches)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
121 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
122 for (int i = 0; i < catches.dim; i++)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
123 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
124 Catch c = cast(Catch)catches.data[i];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
125
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
126 if (c.handler)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
127 c.handler = c.handler.inlineScan(iss);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
128 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
129 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
130 return this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
131 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
132
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
133 /***************************************
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
134 * Builds the following:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
135 * _try
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
136 * block
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
137 * jcatch
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
138 * handler
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
139 * A try-catch statement.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
140 */
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 63
diff changeset
141 override void toIR(IRState *irs)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
142 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
143 Blockx *blx = irs.blx;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
144
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
145 version (SEH) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
146 nteh_declarvars(blx);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
147 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
148
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
149 IRState mystate = IRState(irs, this);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
150
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
151 block* tryblock = block_goto(blx,BCgoto,null);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
152
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
153 int previndex = blx.scope_index;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
154 tryblock.Blast_index = previndex;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
155 blx.scope_index = tryblock.Bscope_index = blx.next_index++;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
156
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
157 // Set the current scope index
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
158 setScopeIndex(blx,tryblock,tryblock.Bscope_index);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
159
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
160 // This is the catch variable
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
161 tryblock.jcatchvar = symbol_genauto(type_fake(mTYvolatile | TYnptr));
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
162
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
163 blx.tryblock = tryblock;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
164 block *breakblock = block_calloc(blx);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
165 block_goto(blx,BC_try,null);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
166 if (body_)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
167 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
168 body_.toIR(&mystate);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
169 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
170 blx.tryblock = tryblock.Btry;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
171
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
172 // break block goes here
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
173 block_goto(blx, BCgoto, breakblock);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
174
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
175 setScopeIndex(blx,blx.curblock, previndex);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
176 blx.scope_index = previndex;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
177
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
178 // create new break block that follows all the catches
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
179 breakblock = block_calloc(blx);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
180
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
181 list_append(&blx.curblock.Bsucc, breakblock);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
182 block_next(blx,BCgoto,null);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
183
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
184 assert(catches);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
185 for (int i = 0 ; i < catches.dim; i++)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
186 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
187 Catch cs = cast(Catch)(catches.data[i]);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
188 if (cs.var)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
189 cs.var.csym = tryblock.jcatchvar;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
190 block* bcatch = blx.curblock;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
191 if (cs.type)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
192 bcatch.Bcatchtype = cs.type.toBasetype().toSymbol();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
193 list_append(&tryblock.Bsucc,bcatch);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
194 block_goto(blx,BCjcatch,null);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
195 if (cs.handler !is null)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
196 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
197 IRState catchState = IRState(irs, this);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
198 cs.handler.toIR(&catchState);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
199 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
200 list_append(&blx.curblock.Bsucc, breakblock);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
201 block_next(blx, BCgoto, null);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
202 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
203
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
204 block_next(blx,cast(BC)blx.curblock.BC, breakblock);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
205 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
206
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 63
diff changeset
207 override void toCBuffer(OutBuffer buf, HdrGenState* hgs)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
208 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
209 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
210 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
211
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 63
diff changeset
212 override TryCatchStatement isTryCatchStatement() { return this; }
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 63
diff changeset
213 }