annotate dmd/TryCatchStatement.d @ 192:eb38fdcb3e62 default tip

updated to compile with dmd2.062
author korDen
date Sat, 02 Mar 2013 01:25:52 -0800
parents b0d41ff5e0df
children
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
187
b0d41ff5e0df Added expandability scheme outlined in http://www.dsource.org/forums/viewtopic.php?t=5659&sid=6f2150ff5b0bffcd47512a6a7608d218
Abscissa
parents: 178
diff changeset
25 import dmd.DDMDExtensions;
b0d41ff5e0df Added expandability scheme outlined in http://www.dsource.org/forums/viewtopic.php?t=5659&sid=6f2150ff5b0bffcd47512a6a7608d218
Abscissa
parents: 178
diff changeset
26
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
27 class TryCatchStatement : Statement
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
28 {
187
b0d41ff5e0df Added expandability scheme outlined in http://www.dsource.org/forums/viewtopic.php?t=5659&sid=6f2150ff5b0bffcd47512a6a7608d218
Abscissa
parents: 178
diff changeset
29 mixin insertMemberExtension!(typeof(this));
b0d41ff5e0df Added expandability scheme outlined in http://www.dsource.org/forums/viewtopic.php?t=5659&sid=6f2150ff5b0bffcd47512a6a7608d218
Abscissa
parents: 178
diff changeset
30
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
31 Statement body_;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
32 Array catches;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
33
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
34 this(Loc loc, Statement body_, Array catches)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
35 {
178
e3afd1303184 Many small bugs fixed
korDen
parents: 174
diff changeset
36 register();
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
37 super(loc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
38 this.body_ = body_;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
39 this.catches = catches;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
40 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
41
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 63
diff changeset
42 override Statement syntaxCopy()
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
43 {
63
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
44 Array a = new Array();
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
45 a.setDim(catches.dim);
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
46 for (int i = 0; i < a.dim; i++)
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
47 {
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
48 Catch c;
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
49
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
50 c = cast(Catch)catches.data[i];
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
51 c = c.syntaxCopy();
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
52 a.data[i] = cast(void*)c;
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
53 }
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
54 TryCatchStatement s = new TryCatchStatement(loc, body_.syntaxCopy(), a);
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
55 return s;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
56 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
57
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 63
diff changeset
58 override Statement semantic(Scope sc)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
59 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
60 body_ = body_.semanticScope(sc, null /*this*/, null);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
61
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
62 /* Even if body is null, still do semantic analysis on catches
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
63 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
64 for (size_t i = 0; i < catches.dim; i++)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
65 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
66 Catch c = cast(Catch)catches.data[i];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
67 c.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
68
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
69 // Determine if current catch 'hides' any previous catches
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
70 for (size_t j = 0; j < i; j++)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
71 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
72 Catch cj = cast(Catch)catches.data[j];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
73 string si = c.loc.toChars();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
74 string sj = cj.loc.toChars();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
75
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
76 if (c.type.toBasetype().implicitConvTo(cj.type.toBasetype()))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
77 error("catch at %s hides catch at %s", sj, si);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
78 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
79 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
80
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
81 if (!body_ || body_.isEmpty())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
82 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
83 return null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
84 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
85 return this;
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 hasBreak()
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 bool usesEH()
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
94 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
95 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
96 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
97
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 63
diff changeset
98 override BE blockExit()
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
99 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
100 assert(body_);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
101 BE result = body_.blockExit();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
102
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
103 BE catchresult = BE.BEnone;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
104 for (size_t i = 0; i < catches.dim; i++)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
105 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
106 Catch c = cast(Catch)catches.data[i];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
107 catchresult |= c.blockExit();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
108
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
109 /* If we're catching Object, then there is no throwing
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
110 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
111 Identifier id = c.type.toBasetype().isClassHandle().ident;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
112 if (i == 0 &&
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
113 (id is Id.Object_ || id is Id.Throwable || id is Id.Exception))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
114 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
115 result &= ~BE.BEthrow;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
116 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
117 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
118 return result | catchresult;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
119 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
120
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 63
diff changeset
121 override Statement inlineScan(InlineScanState* iss)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
122 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
123 if (body_)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
124 body_ = body_.inlineScan(iss);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
125 if (catches)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
126 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
127 for (int i = 0; i < catches.dim; i++)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
128 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
129 Catch c = cast(Catch)catches.data[i];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
130
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
131 if (c.handler)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
132 c.handler = c.handler.inlineScan(iss);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
133 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
134 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
135 return this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
136 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
137
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
138 /***************************************
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
139 * Builds the following:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
140 * _try
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
141 * block
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
142 * jcatch
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
143 * handler
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
144 * A try-catch statement.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
145 */
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 63
diff changeset
146 override void toIR(IRState *irs)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
147 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
148 Blockx *blx = irs.blx;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
149
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
150 version (SEH) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
151 nteh_declarvars(blx);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
152 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
153
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
154 IRState mystate = IRState(irs, this);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
155
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
156 block* tryblock = block_goto(blx,BCgoto,null);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
157
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
158 int previndex = blx.scope_index;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
159 tryblock.Blast_index = previndex;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
160 blx.scope_index = tryblock.Bscope_index = blx.next_index++;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
161
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
162 // Set the current scope index
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
163 setScopeIndex(blx,tryblock,tryblock.Bscope_index);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
164
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
165 // This is the catch variable
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
166 tryblock.jcatchvar = symbol_genauto(type_fake(mTYvolatile | TYnptr));
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
167
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
168 blx.tryblock = tryblock;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
169 block *breakblock = block_calloc(blx);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
170 block_goto(blx,BC_try,null);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
171 if (body_)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
172 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
173 body_.toIR(&mystate);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
174 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
175 blx.tryblock = tryblock.Btry;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
176
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
177 // break block goes here
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
178 block_goto(blx, BCgoto, breakblock);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
179
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
180 setScopeIndex(blx,blx.curblock, previndex);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
181 blx.scope_index = previndex;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
182
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
183 // create new break block that follows all the catches
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
184 breakblock = block_calloc(blx);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
185
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
186 list_append(&blx.curblock.Bsucc, breakblock);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
187 block_next(blx,BCgoto,null);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
188
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
189 assert(catches);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
190 for (int i = 0 ; i < catches.dim; i++)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
191 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
192 Catch cs = cast(Catch)(catches.data[i]);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
193 if (cs.var)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
194 cs.var.csym = tryblock.jcatchvar;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
195 block* bcatch = blx.curblock;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
196 if (cs.type)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
197 bcatch.Bcatchtype = cs.type.toBasetype().toSymbol();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
198 list_append(&tryblock.Bsucc,bcatch);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
199 block_goto(blx,BCjcatch,null);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
200 if (cs.handler !is null)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
201 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
202 IRState catchState = IRState(irs, this);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
203 cs.handler.toIR(&catchState);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
204 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
205 list_append(&blx.curblock.Bsucc, breakblock);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
206 block_next(blx, BCgoto, null);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
207 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
208
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
209 block_next(blx,cast(BC)blx.curblock.BC, breakblock);
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 void toCBuffer(OutBuffer buf, HdrGenState* hgs)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
213 {
174
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 114
diff changeset
214 buf.writestring("try");
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 114
diff changeset
215 buf.writenl();
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 114
diff changeset
216 if (body_)
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 114
diff changeset
217 body_.toCBuffer(buf, hgs);
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 114
diff changeset
218 for (size_t i = 0; i < catches.dim; i++)
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 114
diff changeset
219 {
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 114
diff changeset
220 Catch c = cast(Catch)catches.data[i];
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 114
diff changeset
221 c.toCBuffer(buf, hgs);
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 114
diff changeset
222 }
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
223 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
224
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 63
diff changeset
225 override TryCatchStatement isTryCatchStatement() { return this; }
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 63
diff changeset
226 }