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
|
|
49 Statement syntaxCopy()
|
|
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
|
|
65 void toCBuffer(OutBuffer buf, HdrGenState* hgs)
|
|
66 {
|
|
67 assert(false);
|
|
68 }
|
|
69
|
|
70 static int indent = 0;
|
|
71 static int depth = 0;
|
|
72
|
|
73 Statement semantic(Scope sc)
|
|
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 }
|
|
92
|
|
93 statements.data[i] = cast(void*)s;
|
|
94 if (s)
|
|
95 {
|
|
96 Statement sentry;
|
|
97 Statement sexception;
|
|
98 Statement sfinally;
|
|
99
|
|
100 s.scopeCode(sc, &sentry, &sexception, &sfinally);
|
|
101 if (sentry)
|
|
102 {
|
|
103 sentry = sentry.semantic(sc);
|
|
104 if (s.isDeclarationStatement())
|
|
105 {
|
|
106 statements.insert(i, cast(void*)sentry);
|
|
107 i++;
|
|
108 }
|
|
109 else
|
|
110 statements.data[i] = cast(void*)sentry;
|
|
111 }
|
|
112 if (sexception)
|
|
113 {
|
|
114 if (i + 1 == statements.dim && !sfinally)
|
|
115 {
|
|
116 static if (true) {
|
|
117 sexception = sexception.semantic(sc);
|
|
118 } else {
|
|
119 statements.push(sexception);
|
|
120 if (sfinally)
|
|
121 // Assume sexception does not throw
|
|
122 statements.push(sfinally);
|
|
123 }
|
|
124 }
|
|
125 else
|
|
126 {
|
|
127 /* Rewrite:
|
|
128 * s; s1; s2;
|
|
129 * As:
|
|
130 * s;
|
|
131 * try { s1; s2; }
|
|
132 * catch (Object __o)
|
|
133 * { sexception; throw __o; }
|
|
134 */
|
|
135 Statement body_;
|
|
136 Statements aa = new Statements();
|
|
137
|
|
138 for (int j = i + 1; j < statements.dim; j++)
|
|
139 {
|
|
140 aa.push(statements.data[j]);
|
|
141 }
|
|
142 body_ = new CompoundStatement(Loc(0), aa);
|
|
143 body_ = new ScopeStatement(Loc(0), body_);
|
|
144
|
|
145 Identifier id = Lexer.uniqueId("__o");
|
|
146
|
|
147 Statement handler = new ThrowStatement(Loc(0), new IdentifierExp(Loc(0), id));
|
|
148 handler = new CompoundStatement(Loc(0), sexception, handler);
|
|
149
|
|
150 Array catches = new Array();
|
|
151 Catch ctch = new Catch(Loc(0), null, id, handler);
|
|
152 catches.push(cast(void*)ctch);
|
|
153 s = new TryCatchStatement(Loc(0), body_, catches);
|
|
154
|
|
155 if (sfinally)
|
|
156 s = new TryFinallyStatement(Loc(0), s, sfinally);
|
|
157 s = s.semantic(sc);
|
|
158 statements.setDim(i + 1);
|
|
159 statements.push(cast(void*)s);
|
|
160 break;
|
|
161 }
|
|
162 }
|
|
163 else if (sfinally)
|
|
164 {
|
|
165 if (0 && i + 1 == statements.dim)
|
|
166 {
|
|
167 statements.push(cast(void*)sfinally);
|
|
168 }
|
|
169 else
|
|
170 {
|
|
171 /* Rewrite:
|
|
172 * s; s1; s2;
|
|
173 * As:
|
|
174 * s; try { s1; s2; } finally { sfinally; }
|
|
175 */
|
|
176 Statement body_;
|
|
177 Statements aa = new Statements();
|
|
178
|
|
179 for (int j = i + 1; j < statements.dim; j++)
|
|
180 {
|
|
181 aa.push(statements.data[j]);
|
|
182 }
|
|
183 body_ = new CompoundStatement(Loc(0), aa);
|
|
184 s = new TryFinallyStatement(Loc(0), body_, sfinally);
|
|
185 s = s.semantic(sc);
|
|
186 statements.setDim(i + 1);
|
|
187 statements.push(cast(void*)s);
|
|
188 break;
|
|
189 }
|
|
190 }
|
|
191 }
|
|
192 }
|
|
193 i++;
|
|
194 }
|
|
195 if (statements.dim == 1)
|
|
196 {
|
|
197 return cast(Statement)statements.data[0];
|
|
198 }
|
|
199 return this;
|
|
200 }
|
|
201
|
|
202 bool usesEH()
|
|
203 {
|
|
204 assert(false);
|
|
205 }
|
|
206
|
|
207 BE blockExit()
|
|
208 {
|
|
209 //printf("CompoundStatement::blockExit(%p) %d\n", this, statements->dim);
|
|
210 BE result = BE.BEfallthru;
|
|
211 for (size_t i = 0; i < statements.dim; i++)
|
|
212 { Statement s = cast(Statement)statements.data[i];
|
|
213 if (s)
|
|
214 {
|
|
215 //printf("result = x%x\n", result);
|
|
216 //printf("%s\n", s->toChars());
|
|
217 if (!(result & BE.BEfallthru) && !s.comeFrom())
|
|
218 {
|
|
219 if (s.blockExit() != BE.BEhalt)
|
|
220 s.warning("statement is not reachable");
|
|
221 }
|
|
222
|
|
223 result &= ~BE.BEfallthru;
|
|
224 result |= s.blockExit();
|
|
225 }
|
|
226 }
|
|
227
|
|
228 return result;
|
|
229 }
|
|
230
|
|
231 bool comeFrom()
|
|
232 {
|
|
233 assert(false);
|
|
234 }
|
|
235
|
|
236 bool isEmpty()
|
|
237 {
|
|
238 for (int i = 0; i < statements.dim; i++)
|
|
239 {
|
|
240 Statement s = cast(Statement) statements.data[i];
|
|
241 if (s && !s.isEmpty())
|
|
242 return false;
|
|
243 }
|
|
244 return true;
|
|
245 }
|
|
246
|
|
247 Statements flatten(Scope sc)
|
|
248 {
|
|
249 return statements;
|
|
250 }
|
|
251
|
|
252 ReturnStatement isReturnStatement()
|
|
253 {
|
|
254 ReturnStatement rs = null;
|
|
255
|
|
256 for (int i = 0; i < statements.dim; i++)
|
|
257 {
|
|
258 Statement s = cast(Statement) statements.data[i];
|
|
259 if (s)
|
|
260 {
|
|
261 rs = s.isReturnStatement();
|
|
262 if (rs)
|
|
263 break;
|
|
264 }
|
|
265 }
|
|
266 return rs;
|
|
267 }
|
|
268
|
|
269 Expression interpret(InterState* istate)
|
|
270 {
|
|
271 assert(false);
|
|
272 }
|
|
273
|
|
274 int inlineCost(InlineCostState* ics)
|
|
275 {
|
|
276 int cost = 0;
|
|
277
|
|
278 for (size_t i = 0; i < statements.dim; i++)
|
|
279 {
|
|
280 Statement s = cast(Statement)statements.data[i];
|
|
281 if (s)
|
|
282 {
|
|
283 cost += s.inlineCost(ics);
|
|
284 if (cost >= COST_MAX)
|
|
285 break;
|
|
286 }
|
|
287 }
|
|
288
|
|
289 return cost;
|
|
290 }
|
|
291
|
|
292 Expression doInline(InlineDoState ids)
|
|
293 {
|
|
294 Expression e = null;
|
|
295
|
|
296 //printf("CompoundStatement.doInline() %d\n", statements.dim);
|
|
297 for (size_t i = 0; i < statements.dim; i++)
|
|
298 {
|
|
299 Statement s = cast(Statement)statements.data[i];
|
|
300 if (s)
|
|
301 {
|
|
302 Expression e2 = s.doInline(ids);
|
|
303 e = Expression.combine(e, e2);
|
|
304 if (s.isReturnStatement())
|
|
305 break;
|
|
306
|
|
307 /* Check for:
|
|
308 * if (condition)
|
|
309 * return exp1;
|
|
310 * else
|
|
311 * return exp2;
|
|
312 */
|
|
313 IfStatement ifs = s.isIfStatement();
|
|
314 if (ifs && ifs.elsebody && ifs.ifbody &&
|
|
315 ifs.ifbody.isReturnStatement() &&
|
|
316 ifs.elsebody.isReturnStatement()
|
|
317 )
|
|
318 break;
|
|
319 }
|
|
320 }
|
|
321 return e;
|
|
322 }
|
|
323
|
|
324 Statement inlineScan(InlineScanState* iss)
|
|
325 {
|
|
326 for (size_t i = 0; i < statements.dim; i++)
|
|
327 {
|
|
328 Statement s = cast(Statement) statements.data[i];
|
|
329 if (s)
|
|
330 statements.data[i] = cast(void*)s.inlineScan(iss);
|
|
331 }
|
|
332
|
|
333 return this;
|
|
334 }
|
|
335
|
|
336 void toIR(IRState* irs)
|
|
337 {
|
|
338 if (statements)
|
|
339 {
|
|
340 size_t dim = statements.dim;
|
|
341 for (size_t i = 0 ; i < dim ; i++)
|
|
342 {
|
|
343 Statement s = cast(Statement)statements.data[i];
|
|
344 if (s !is null)
|
|
345 {
|
|
346 s.toIR(irs);
|
|
347 }
|
|
348 }
|
|
349 }
|
|
350 }
|
|
351
|
|
352 CompoundStatement isCompoundStatement() { return this; }
|
|
353 } |