comparison dmd/ForStatement.d @ 0:10317f0c89a5

Initial commit
author korDen
date Sat, 24 Oct 2009 08:42:06 +0400
parents
children cab4c37afb89
comparison
equal deleted inserted replaced
-1:000000000000 0:10317f0c89a5
1 module dmd.ForStatement;
2
3 import dmd.Statement;
4 import dmd.Expression;
5 import dmd.Loc;
6 import dmd.Scope;
7 import dmd.InterState;
8 import dmd.OutBuffer;
9 import dmd.HdrGenState;
10 import dmd.InlineScanState;
11 import dmd.WANT;
12 import dmd.ScopeDsymbol;
13 import dmd.IRState;
14 import dmd.BE;
15
16 import dmd.backend.Blockx;
17 import dmd.backend.block;
18 import dmd.backend.Util;
19 import dmd.backend.BC;
20
21 class ForStatement : Statement
22 {
23 Statement init;
24 Expression condition;
25 Expression increment;
26 Statement body_;
27
28 this(Loc loc, Statement init, Expression condition, Expression increment, Statement body_)
29 {
30 super(loc);
31
32 this.init = init;
33 this.condition = condition;
34 this.increment = increment;
35 this.body_ = body_;
36 }
37
38 Statement syntaxCopy()
39 {
40 Statement i = null;
41 if (init)
42 i = init.syntaxCopy();
43 Expression c = null;
44 if (condition)
45 c = condition.syntaxCopy();
46 Expression inc = null;
47 if (increment)
48 inc = increment.syntaxCopy();
49 ForStatement s = new ForStatement(loc, i, c, inc, body_.syntaxCopy());
50 return s;
51 }
52
53 Statement semantic(Scope sc)
54 {
55 ScopeDsymbol sym = new ScopeDsymbol();
56 sym.parent = sc.scopesym;
57 sc = sc.push(sym);
58 if (init)
59 init = init.semantic(sc);
60 sc.noctor++;
61 if (condition)
62 {
63 condition = condition.semantic(sc);
64 condition = resolveProperties(sc, condition);
65 condition = condition.optimize(WANTvalue);
66 condition = condition.checkToBoolean();
67 }
68 if (increment)
69 {
70 increment = increment.semantic(sc);
71 increment = resolveProperties(sc, increment);
72 }
73
74 sc.sbreak = this;
75 sc.scontinue = this;
76 if (body_)
77 body_ = body_.semantic(sc);
78 sc.noctor--;
79
80 sc.pop();
81 return this;
82 }
83
84 void scopeCode(Scope sc, Statement* sentry, Statement* sexception, Statement* sfinally)
85 {
86 //printf("ForStatement::scopeCode()\n");
87 //print();
88 if (init)
89 init.scopeCode(sc, sentry, sexception, sfinally);
90 else
91 Statement.scopeCode(sc, sentry, sexception, sfinally);
92 }
93
94 bool hasBreak()
95 {
96 //printf("ForStatement.hasBreak()\n");
97 return true;
98 }
99
100 bool hasContinue()
101 {
102 return true;
103 }
104
105 bool usesEH()
106 {
107 return (init && init.usesEH()) || body_.usesEH();
108 }
109
110 BE blockExit()
111 {
112 BE result = BE.BEfallthru;
113
114 if (init)
115 {
116 result = init.blockExit();
117 if (!(result & BE.BEfallthru))
118 return result;
119 }
120 if (condition)
121 {
122 if (condition.canThrow())
123 result |= BE.BEthrow;
124 if (condition.isBool(true))
125 result &= ~BE.BEfallthru;
126 else if (condition.isBool(false))
127 return result;
128 }
129 else
130 result &= ~BE.BEfallthru; // the body must do the exiting
131 if (body_)
132 {
133 int r = body_.blockExit();
134 if (r & (BE.BEbreak | BE.BEgoto))
135 result |= BE.BEfallthru;
136 result |= r & ~(BE.BEfallthru | BE.BEbreak | BE.BEcontinue);
137 }
138 if (increment && increment.canThrow())
139 result |= BE.BEthrow;
140 return result;
141 }
142
143 bool comeFrom()
144 {
145 //printf("ForStatement.comeFrom()\n");
146 if (body_)
147 {
148 bool result = body_.comeFrom();
149 //printf("result = %d\n", result);
150 return result;
151 }
152 return false;
153 }
154
155 Expression interpret(InterState* istate)
156 {
157 assert(false);
158 }
159
160 void toCBuffer(OutBuffer buf, HdrGenState* hgs)
161 {
162 buf.writestring("for (");
163 if (init)
164 {
165 hgs.FLinit.init++;
166 init.toCBuffer(buf, hgs);
167 hgs.FLinit.init--;
168 }
169 else
170 buf.writebyte(';');
171 if (condition)
172 { buf.writebyte(' ');
173 condition.toCBuffer(buf, hgs);
174 }
175 buf.writebyte(';');
176 if (increment)
177 {
178 buf.writebyte(' ');
179 increment.toCBuffer(buf, hgs);
180 }
181 buf.writebyte(')');
182 buf.writenl();
183 buf.writebyte('{');
184 buf.writenl();
185 body_.toCBuffer(buf, hgs);
186 buf.writebyte('}');
187 buf.writenl();
188 }
189
190 Statement inlineScan(InlineScanState* iss)
191 {
192 if (init)
193 init = init.inlineScan(iss);
194 if (condition)
195 condition = condition.inlineScan(iss);
196 if (increment)
197 increment = increment.inlineScan(iss);
198 if (body_)
199 body_ = body_.inlineScan(iss);
200 return this;
201 }
202
203 void toIR(IRState* irs)
204 {
205 Blockx* blx = irs.blx;
206
207 IRState mystate = IRState(irs,this);
208 mystate.breakBlock = block_calloc(blx);
209 mystate.contBlock = block_calloc(blx);
210
211 if (init)
212 init.toIR(&mystate);
213 block* bpre = blx.curblock;
214 block_next(blx,BCgoto,null);
215 block* bcond = blx.curblock;
216 list_append(&bpre.Bsucc, bcond);
217 list_append(&mystate.contBlock.Bsucc, bcond);
218 if (condition)
219 {
220 incUsage(irs, condition.loc);
221 block_appendexp(bcond, condition.toElem(&mystate));
222 block_next(blx,BCiftrue,null);
223 list_append(&bcond.Bsucc, blx.curblock);
224 list_append(&bcond.Bsucc, mystate.breakBlock);
225 }
226 else
227 {
228 /* No conditional, it's a straight goto
229 */
230 block_next(blx,BCgoto,null);
231 list_append(&bcond.Bsucc, blx.curblock);
232 }
233
234 if (body_)
235 body_.toIR(&mystate);
236 /* End of the body goes to the continue block
237 */
238 list_append(&blx.curblock.Bsucc, mystate.contBlock);
239 block_next(blx, BCgoto, mystate.contBlock);
240
241 if (increment)
242 {
243 incUsage(irs, increment.loc);
244 block_appendexp(mystate.contBlock, increment.toElem(&mystate));
245 }
246
247 /* The 'break' block follows the for statement.
248 */
249 block_next(blx,BCgoto, mystate.breakBlock);
250 }
251 }