0
|
1 module dmd.CompoundStatement;
|
|
2
|
|
3 import dmd.Loc;
|
|
4 import dmd.Statement;
|
|
5 import dmd.Array;
|
|
6 import dmd.TryCatchStatement;
|
|
7 import dmd.TryFinallyStatement;
|
|
8 import dmd.Catch;
|
|
9 import dmd.ScopeStatement;
|
|
10 import dmd.Identifier;
|
|
11 import dmd.Lexer;
|
|
12 import dmd.ThrowStatement;
|
|
13 import dmd.IdentifierExp;
|
|
14 import dmd.Scope;
|
|
15 import dmd.OutBuffer;
|
|
16 import dmd.HdrGenState;
|
|
17 import dmd.ArrayTypes;
|
|
18 import dmd.ReturnStatement;
|
|
19 import dmd.Expression;
|
|
20 import dmd.InterState;
|
|
21 import dmd.InlineDoState;
|
|
22 import dmd.InlineCostState;
|
|
23 import dmd.InlineScanState;
|
|
24 import dmd.IfStatement;
|
|
25 import dmd.IRState;
|
|
26 import dmd.BE;
|
|
27 import dmd.Util;
|
|
28
|
|
29 class CompoundStatement : Statement
|
|
30 {
|
|
31 Statements statements;
|
|
32
|
|
33 this(Loc loc, Statements s)
|
|
34 {
|
|
35 super(loc);
|
|
36 statements = s;
|
|
37 }
|
|
38
|
|
39 this(Loc loc, Statement s1, Statement s2)
|
|
40 {
|
|
41 super(loc);
|
|
42
|
|
43 statements = new Statements();
|
|
44 statements.reserve(2);
|
|
45 statements.push(cast(void*)s1);
|
|
46 statements.push(cast(void*)s2);
|
|
47 }
|
|
48
|
72
|
49 override Statement syntaxCopy()
|
0
|
50 {
|
|
51 Statements a = new Statements();
|
|
52 a.setDim(statements.dim);
|
|
53
|
|
54 for (size_t i = 0; i < statements.dim; i++)
|
|
55 {
|
|
56 Statement s = cast(Statement)statements.data[i];
|
|
57 if (s)
|
|
58 s = s.syntaxCopy();
|
|
59 a.data[i] = cast(void*)s;
|
|
60 }
|
|
61
|
|
62 return new CompoundStatement(loc, a);
|
|
63 }
|
|
64
|
72
|
65 override void toCBuffer(OutBuffer buf, HdrGenState* hgs)
|
0
|
66 {
|
|
67 assert(false);
|
|
68 }
|
|
69
|
|
70 static int indent = 0;
|
|
71 static int depth = 0;
|
|
72
|
72
|
73 override Statement semantic(Scope sc)
|
0
|
74 {
|
|
75 Statement s;
|
|
76
|
|
77 //printf("CompoundStatement.semantic(this = %p, sc = %p)\n", this, sc);
|
|
78
|
|
79 for (size_t i = 0; i < statements.dim; )
|
|
80 {
|
|
81 s = cast(Statement) statements.data[i];
|
|
82 if (s)
|
|
83 {
|
|
84 Statements a = s.flatten(sc);
|
|
85
|
|
86 if (a)
|
|
87 {
|
|
88 statements.remove(i);
|
|
89 statements.insert(i, a);
|
|
90 continue;
|
|
91 }
|
27
|
92
|
|
93 s = s.semantic(sc);
|
0
|
94
|
|
95 statements.data[i] = cast(void*)s;
|
|
96 if (s)
|
|
97 {
|
|
98 Statement sentry;
|
|
99 Statement sexception;
|
|
100 Statement sfinally;
|
|
101
|
|
102 s.scopeCode(sc, &sentry, &sexception, &sfinally);
|
|
103 if (sentry)
|
|
104 {
|
|
105 sentry = sentry.semantic(sc);
|
|
106 if (s.isDeclarationStatement())
|
|
107 {
|
|
108 statements.insert(i, cast(void*)sentry);
|
|
109 i++;
|
|
110 }
|
|
111 else
|
|
112 statements.data[i] = cast(void*)sentry;
|
|
113 }
|
|
114 if (sexception)
|
|
115 {
|
|
116 if (i + 1 == statements.dim && !sfinally)
|
|
117 {
|
|
118 static if (true) {
|
|
119 sexception = sexception.semantic(sc);
|
|
120 } else {
|
|
121 statements.push(sexception);
|
|
122 if (sfinally)
|
|
123 // Assume sexception does not throw
|
|
124 statements.push(sfinally);
|
|
125 }
|
|
126 }
|
|
127 else
|
|
128 {
|
|
129 /* Rewrite:
|
|
130 * s; s1; s2;
|
|
131 * As:
|
|
132 * s;
|
|
133 * try { s1; s2; }
|
|
134 * catch (Object __o)
|
|
135 * { sexception; throw __o; }
|
|
136 */
|
|
137 Statement body_;
|
|
138 Statements aa = new Statements();
|
|
139
|
|
140 for (int j = i + 1; j < statements.dim; j++)
|
|
141 {
|
|
142 aa.push(statements.data[j]);
|
|
143 }
|
|
144 body_ = new CompoundStatement(Loc(0), aa);
|
|
145 body_ = new ScopeStatement(Loc(0), body_);
|
|
146
|
|
147 Identifier id = Lexer.uniqueId("__o");
|
|
148
|
|
149 Statement handler = new ThrowStatement(Loc(0), new IdentifierExp(Loc(0), id));
|
|
150 handler = new CompoundStatement(Loc(0), sexception, handler);
|
|
151
|
|
152 Array catches = new Array();
|
|
153 Catch ctch = new Catch(Loc(0), null, id, handler);
|
|
154 catches.push(cast(void*)ctch);
|
|
155 s = new TryCatchStatement(Loc(0), body_, catches);
|
|
156
|
|
157 if (sfinally)
|
|
158 s = new TryFinallyStatement(Loc(0), s, sfinally);
|
|
159 s = s.semantic(sc);
|
|
160 statements.setDim(i + 1);
|
|
161 statements.push(cast(void*)s);
|
|
162 break;
|
|
163 }
|
|
164 }
|
|
165 else if (sfinally)
|
|
166 {
|
|
167 if (0 && i + 1 == statements.dim)
|
|
168 {
|
|
169 statements.push(cast(void*)sfinally);
|
|
170 }
|
|
171 else
|
|
172 {
|
|
173 /* Rewrite:
|
|
174 * s; s1; s2;
|
|
175 * As:
|
|
176 * s; try { s1; s2; } finally { sfinally; }
|
|
177 */
|
|
178 Statement body_;
|
|
179 Statements aa = new Statements();
|
|
180
|
|
181 for (int j = i + 1; j < statements.dim; j++)
|
|
182 {
|
|
183 aa.push(statements.data[j]);
|
|
184 }
|
|
185 body_ = new CompoundStatement(Loc(0), aa);
|
|
186 s = new TryFinallyStatement(Loc(0), body_, sfinally);
|
|
187 s = s.semantic(sc);
|
|
188 statements.setDim(i + 1);
|
|
189 statements.push(cast(void*)s);
|
|
190 break;
|
|
191 }
|
|
192 }
|
|
193 }
|
|
194 }
|
|
195 i++;
|
|
196 }
|
|
197 if (statements.dim == 1)
|
|
198 {
|
|
199 return cast(Statement)statements.data[0];
|
|
200 }
|
|
201 return this;
|
|
202 }
|
|
203
|
72
|
204 override bool usesEH()
|
0
|
205 {
|
|
206 assert(false);
|
|
207 }
|
|
208
|
72
|
209 override BE blockExit()
|
0
|
210 {
|
|
211 //printf("CompoundStatement::blockExit(%p) %d\n", this, statements->dim);
|
|
212 BE result = BE.BEfallthru;
|
|
213 for (size_t i = 0; i < statements.dim; i++)
|
|
214 { Statement s = cast(Statement)statements.data[i];
|
|
215 if (s)
|
|
216 {
|
|
217 //printf("result = x%x\n", result);
|
|
218 //printf("%s\n", s->toChars());
|
|
219 if (!(result & BE.BEfallthru) && !s.comeFrom())
|
|
220 {
|
79
|
221 if (s.blockExit() != BE.BEhalt && !s.isEmpty())
|
0
|
222 s.warning("statement is not reachable");
|
|
223 }
|
79
|
224 else
|
|
225 {
|
|
226 result &= ~BE.BEfallthru;
|
|
227 result |= s.blockExit();
|
|
228 }
|
0
|
229 }
|
|
230 }
|
|
231
|
|
232 return result;
|
|
233 }
|
|
234
|
72
|
235 override bool comeFrom()
|
0
|
236 {
|
|
237 assert(false);
|
|
238 }
|
|
239
|
72
|
240 override bool isEmpty()
|
0
|
241 {
|
|
242 for (int i = 0; i < statements.dim; i++)
|
|
243 {
|
|
244 Statement s = cast(Statement) statements.data[i];
|
|
245 if (s && !s.isEmpty())
|
|
246 return false;
|
|
247 }
|
|
248 return true;
|
|
249 }
|
|
250
|
72
|
251 override Statements flatten(Scope sc)
|
0
|
252 {
|
|
253 return statements;
|
|
254 }
|
|
255
|
72
|
256 override ReturnStatement isReturnStatement()
|
0
|
257 {
|
|
258 ReturnStatement rs = null;
|
|
259
|
|
260 for (int i = 0; i < statements.dim; i++)
|
|
261 {
|
|
262 Statement s = cast(Statement) statements.data[i];
|
|
263 if (s)
|
|
264 {
|
|
265 rs = s.isReturnStatement();
|
|
266 if (rs)
|
|
267 break;
|
|
268 }
|
|
269 }
|
|
270 return rs;
|
|
271 }
|
|
272
|
72
|
273 override Expression interpret(InterState istate)
|
0
|
274 {
|
63
|
275 Expression e = null;
|
|
276
|
|
277 version (LOG) {
|
|
278 printf("CompoundStatement.interpret()\n");
|
|
279 }
|
|
280 if (istate.start == this)
|
|
281 istate.start = null;
|
|
282 if (statements)
|
|
283 {
|
|
284 for (size_t i = 0; i < statements.dim; i++)
|
|
285 {
|
|
286 Statement s = cast(Statement)statements.data[i];
|
|
287
|
|
288 if (s)
|
|
289 {
|
|
290 e = s.interpret(istate);
|
|
291 if (e)
|
|
292 break;
|
|
293 }
|
|
294 }
|
|
295 }
|
|
296 version (LOG) {
|
|
297 printf("-CompoundStatement.interpret() %p\n", e);
|
|
298 }
|
|
299 return e;
|
0
|
300 }
|
|
301
|
72
|
302 override int inlineCost(InlineCostState* ics)
|
0
|
303 {
|
|
304 int cost = 0;
|
|
305
|
|
306 for (size_t i = 0; i < statements.dim; i++)
|
|
307 {
|
|
308 Statement s = cast(Statement)statements.data[i];
|
|
309 if (s)
|
|
310 {
|
|
311 cost += s.inlineCost(ics);
|
|
312 if (cost >= COST_MAX)
|
|
313 break;
|
|
314 }
|
|
315 }
|
|
316
|
|
317 return cost;
|
|
318 }
|
|
319
|
72
|
320 override Expression doInline(InlineDoState ids)
|
0
|
321 {
|
|
322 Expression e = null;
|
|
323
|
|
324 //printf("CompoundStatement.doInline() %d\n", statements.dim);
|
|
325 for (size_t i = 0; i < statements.dim; i++)
|
|
326 {
|
|
327 Statement s = cast(Statement)statements.data[i];
|
|
328 if (s)
|
|
329 {
|
|
330 Expression e2 = s.doInline(ids);
|
|
331 e = Expression.combine(e, e2);
|
|
332 if (s.isReturnStatement())
|
|
333 break;
|
|
334
|
|
335 /* Check for:
|
|
336 * if (condition)
|
|
337 * return exp1;
|
|
338 * else
|
|
339 * return exp2;
|
|
340 */
|
|
341 IfStatement ifs = s.isIfStatement();
|
|
342 if (ifs && ifs.elsebody && ifs.ifbody &&
|
|
343 ifs.ifbody.isReturnStatement() &&
|
|
344 ifs.elsebody.isReturnStatement()
|
|
345 )
|
|
346 break;
|
|
347 }
|
|
348 }
|
|
349 return e;
|
|
350 }
|
|
351
|
72
|
352 override Statement inlineScan(InlineScanState* iss)
|
0
|
353 {
|
|
354 for (size_t i = 0; i < statements.dim; i++)
|
|
355 {
|
|
356 Statement s = cast(Statement) statements.data[i];
|
|
357 if (s)
|
|
358 statements.data[i] = cast(void*)s.inlineScan(iss);
|
|
359 }
|
|
360
|
|
361 return this;
|
|
362 }
|
|
363
|
72
|
364 override void toIR(IRState* irs)
|
0
|
365 {
|
|
366 if (statements)
|
|
367 {
|
|
368 size_t dim = statements.dim;
|
|
369 for (size_t i = 0 ; i < dim ; i++)
|
|
370 {
|
|
371 Statement s = cast(Statement)statements.data[i];
|
|
372 if (s !is null)
|
|
373 {
|
|
374 s.toIR(irs);
|
|
375 }
|
|
376 }
|
|
377 }
|
|
378 }
|
|
379
|
72
|
380 override CompoundStatement isCompoundStatement() { return this; }
|
|
381 }
|