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