0
|
1 module dmd.ForStatement;
|
|
2
|
114
|
3 import dmd.common;
|
0
|
4 import dmd.Statement;
|
|
5 import dmd.Expression;
|
123
|
6 import dmd.GlobalExpressions;
|
0
|
7 import dmd.Loc;
|
|
8 import dmd.Scope;
|
|
9 import dmd.InterState;
|
|
10 import dmd.OutBuffer;
|
|
11 import dmd.HdrGenState;
|
|
12 import dmd.InlineScanState;
|
|
13 import dmd.WANT;
|
|
14 import dmd.ScopeDsymbol;
|
|
15 import dmd.IRState;
|
|
16 import dmd.BE;
|
|
17
|
|
18 import dmd.backend.Blockx;
|
|
19 import dmd.backend.block;
|
|
20 import dmd.backend.Util;
|
|
21 import dmd.backend.BC;
|
|
22
|
|
23 class ForStatement : Statement
|
|
24 {
|
|
25 Statement init;
|
|
26 Expression condition;
|
|
27 Expression increment;
|
|
28 Statement body_;
|
|
29
|
|
30 this(Loc loc, Statement init, Expression condition, Expression increment, Statement body_)
|
|
31 {
|
|
32 super(loc);
|
|
33
|
|
34 this.init = init;
|
|
35 this.condition = condition;
|
|
36 this.increment = increment;
|
|
37 this.body_ = body_;
|
|
38 }
|
|
39
|
72
|
40 override Statement syntaxCopy()
|
0
|
41 {
|
|
42 Statement i = null;
|
|
43 if (init)
|
|
44 i = init.syntaxCopy();
|
|
45 Expression c = null;
|
|
46 if (condition)
|
|
47 c = condition.syntaxCopy();
|
|
48 Expression inc = null;
|
|
49 if (increment)
|
|
50 inc = increment.syntaxCopy();
|
|
51 ForStatement s = new ForStatement(loc, i, c, inc, body_.syntaxCopy());
|
|
52 return s;
|
|
53 }
|
|
54
|
72
|
55 override Statement semantic(Scope sc)
|
0
|
56 {
|
|
57 ScopeDsymbol sym = new ScopeDsymbol();
|
|
58 sym.parent = sc.scopesym;
|
|
59 sc = sc.push(sym);
|
|
60 if (init)
|
|
61 init = init.semantic(sc);
|
|
62 sc.noctor++;
|
|
63 if (condition)
|
|
64 {
|
|
65 condition = condition.semantic(sc);
|
|
66 condition = resolveProperties(sc, condition);
|
|
67 condition = condition.optimize(WANTvalue);
|
|
68 condition = condition.checkToBoolean();
|
|
69 }
|
|
70 if (increment)
|
|
71 {
|
|
72 increment = increment.semantic(sc);
|
|
73 increment = resolveProperties(sc, increment);
|
79
|
74 increment = increment.optimize(0);
|
0
|
75 }
|
|
76
|
|
77 sc.sbreak = this;
|
|
78 sc.scontinue = this;
|
|
79 if (body_)
|
|
80 body_ = body_.semantic(sc);
|
|
81 sc.noctor--;
|
|
82
|
|
83 sc.pop();
|
|
84 return this;
|
|
85 }
|
|
86
|
72
|
87 override void scopeCode(Scope sc, Statement* sentry, Statement* sexception, Statement* sfinally)
|
0
|
88 {
|
|
89 //printf("ForStatement::scopeCode()\n");
|
|
90 //print();
|
|
91 if (init)
|
|
92 init.scopeCode(sc, sentry, sexception, sfinally);
|
|
93 else
|
|
94 Statement.scopeCode(sc, sentry, sexception, sfinally);
|
|
95 }
|
|
96
|
72
|
97 override bool hasBreak()
|
0
|
98 {
|
|
99 //printf("ForStatement.hasBreak()\n");
|
|
100 return true;
|
|
101 }
|
|
102
|
72
|
103 override bool hasContinue()
|
0
|
104 {
|
|
105 return true;
|
|
106 }
|
|
107
|
72
|
108 override bool usesEH()
|
0
|
109 {
|
|
110 return (init && init.usesEH()) || body_.usesEH();
|
|
111 }
|
|
112
|
72
|
113 override BE blockExit()
|
0
|
114 {
|
|
115 BE result = BE.BEfallthru;
|
|
116
|
|
117 if (init)
|
|
118 {
|
|
119 result = init.blockExit();
|
|
120 if (!(result & BE.BEfallthru))
|
|
121 return result;
|
|
122 }
|
|
123 if (condition)
|
|
124 {
|
|
125 if (condition.canThrow())
|
|
126 result |= BE.BEthrow;
|
|
127 if (condition.isBool(true))
|
|
128 result &= ~BE.BEfallthru;
|
|
129 else if (condition.isBool(false))
|
|
130 return result;
|
|
131 }
|
|
132 else
|
|
133 result &= ~BE.BEfallthru; // the body must do the exiting
|
|
134 if (body_)
|
|
135 {
|
|
136 int r = body_.blockExit();
|
|
137 if (r & (BE.BEbreak | BE.BEgoto))
|
|
138 result |= BE.BEfallthru;
|
|
139 result |= r & ~(BE.BEfallthru | BE.BEbreak | BE.BEcontinue);
|
|
140 }
|
|
141 if (increment && increment.canThrow())
|
|
142 result |= BE.BEthrow;
|
|
143 return result;
|
|
144 }
|
|
145
|
72
|
146 override bool comeFrom()
|
0
|
147 {
|
|
148 //printf("ForStatement.comeFrom()\n");
|
|
149 if (body_)
|
|
150 {
|
|
151 bool result = body_.comeFrom();
|
|
152 //printf("result = %d\n", result);
|
|
153 return result;
|
|
154 }
|
|
155 return false;
|
|
156 }
|
|
157
|
72
|
158 override Expression interpret(InterState istate)
|
0
|
159 {
|
123
|
160 version (LOG) {
|
|
161 printf("ForStatement.interpret()\n");
|
|
162 }
|
|
163 if (istate.start == this)
|
|
164 istate.start = null;
|
|
165
|
|
166 Expression e;
|
|
167
|
|
168 if (init)
|
|
169 {
|
|
170 e = init.interpret(istate);
|
|
171 if (e is EXP_CANT_INTERPRET)
|
|
172 return e;
|
|
173 assert(!e);
|
|
174 }
|
|
175
|
|
176 if (istate.start)
|
|
177 {
|
|
178 e = body_ ? body_.interpret(istate) : null;
|
|
179 if (istate.start)
|
|
180 return null;
|
|
181 if (e is EXP_CANT_INTERPRET)
|
|
182 return e;
|
|
183 if (e is EXP_BREAK_INTERPRET)
|
|
184 return null;
|
|
185 if (e is EXP_CONTINUE_INTERPRET)
|
|
186 goto Lcontinue;
|
|
187 if (e)
|
|
188 return e;
|
|
189 }
|
|
190
|
|
191 while (true)
|
|
192 {
|
|
193 if (!condition)
|
|
194 goto Lhead;
|
|
195 e = condition.interpret(istate);
|
|
196 if (e is EXP_CANT_INTERPRET)
|
|
197 break;
|
|
198 if (!e.isConst())
|
|
199 {
|
|
200 e = EXP_CANT_INTERPRET;
|
|
201 break;
|
|
202 }
|
|
203 if (e.isBool(true))
|
|
204 {
|
|
205 Lhead:
|
|
206 e = body_ ? body_.interpret(istate) : null;
|
|
207 if (e is EXP_CANT_INTERPRET)
|
|
208 break;
|
|
209 if (e is EXP_BREAK_INTERPRET)
|
|
210 {
|
|
211 e = null;
|
|
212 break;
|
|
213 }
|
|
214 if (e && e !is EXP_CONTINUE_INTERPRET)
|
|
215 break;
|
|
216 Lcontinue:
|
|
217 if (increment)
|
|
218 {
|
|
219 e = increment.interpret(istate);
|
|
220 if (e is EXP_CANT_INTERPRET)
|
|
221 break;
|
|
222 }
|
|
223 }
|
|
224 else if (e.isBool(false))
|
|
225 {
|
|
226 e = null;
|
|
227 break;
|
|
228 }
|
|
229 else
|
|
230 assert(0);
|
|
231 }
|
|
232 return e;
|
0
|
233 }
|
|
234
|
72
|
235 override void toCBuffer(OutBuffer buf, HdrGenState* hgs)
|
0
|
236 {
|
|
237 buf.writestring("for (");
|
|
238 if (init)
|
|
239 {
|
|
240 hgs.FLinit.init++;
|
|
241 init.toCBuffer(buf, hgs);
|
|
242 hgs.FLinit.init--;
|
|
243 }
|
|
244 else
|
|
245 buf.writebyte(';');
|
|
246 if (condition)
|
|
247 { buf.writebyte(' ');
|
|
248 condition.toCBuffer(buf, hgs);
|
|
249 }
|
|
250 buf.writebyte(';');
|
|
251 if (increment)
|
|
252 {
|
|
253 buf.writebyte(' ');
|
|
254 increment.toCBuffer(buf, hgs);
|
|
255 }
|
|
256 buf.writebyte(')');
|
|
257 buf.writenl();
|
|
258 buf.writebyte('{');
|
|
259 buf.writenl();
|
|
260 body_.toCBuffer(buf, hgs);
|
|
261 buf.writebyte('}');
|
|
262 buf.writenl();
|
|
263 }
|
|
264
|
72
|
265 override Statement inlineScan(InlineScanState* iss)
|
0
|
266 {
|
|
267 if (init)
|
|
268 init = init.inlineScan(iss);
|
|
269 if (condition)
|
|
270 condition = condition.inlineScan(iss);
|
|
271 if (increment)
|
|
272 increment = increment.inlineScan(iss);
|
|
273 if (body_)
|
|
274 body_ = body_.inlineScan(iss);
|
|
275 return this;
|
|
276 }
|
|
277
|
72
|
278 override void toIR(IRState* irs)
|
0
|
279 {
|
|
280 Blockx* blx = irs.blx;
|
|
281
|
|
282 IRState mystate = IRState(irs,this);
|
|
283 mystate.breakBlock = block_calloc(blx);
|
|
284 mystate.contBlock = block_calloc(blx);
|
|
285
|
|
286 if (init)
|
|
287 init.toIR(&mystate);
|
|
288 block* bpre = blx.curblock;
|
|
289 block_next(blx,BCgoto,null);
|
|
290 block* bcond = blx.curblock;
|
|
291 list_append(&bpre.Bsucc, bcond);
|
|
292 list_append(&mystate.contBlock.Bsucc, bcond);
|
|
293 if (condition)
|
|
294 {
|
|
295 incUsage(irs, condition.loc);
|
|
296 block_appendexp(bcond, condition.toElem(&mystate));
|
|
297 block_next(blx,BCiftrue,null);
|
|
298 list_append(&bcond.Bsucc, blx.curblock);
|
|
299 list_append(&bcond.Bsucc, mystate.breakBlock);
|
|
300 }
|
|
301 else
|
|
302 {
|
|
303 /* No conditional, it's a straight goto
|
|
304 */
|
|
305 block_next(blx,BCgoto,null);
|
|
306 list_append(&bcond.Bsucc, blx.curblock);
|
|
307 }
|
|
308
|
|
309 if (body_)
|
|
310 body_.toIR(&mystate);
|
|
311 /* End of the body goes to the continue block
|
|
312 */
|
|
313 list_append(&blx.curblock.Bsucc, mystate.contBlock);
|
|
314 block_next(blx, BCgoto, mystate.contBlock);
|
|
315
|
|
316 if (increment)
|
|
317 {
|
|
318 incUsage(irs, increment.loc);
|
|
319 block_appendexp(mystate.contBlock, increment.toElem(&mystate));
|
|
320 }
|
|
321
|
|
322 /* The 'break' block follows the for statement.
|
|
323 */
|
|
324 block_next(blx,BCgoto, mystate.breakBlock);
|
|
325 }
|
72
|
326 }
|