0
|
1 module dmd.IfStatement;
|
|
2
|
|
3 import dmd.Statement;
|
|
4 import dmd.Argument;
|
|
5 import dmd.Loc;
|
|
6 import dmd.Expression;
|
|
7 import dmd.VarDeclaration;
|
|
8 import dmd.Scope;
|
|
9 import dmd.InterState;
|
|
10 import dmd.OutBuffer;
|
|
11 import dmd.HdrGenState;
|
|
12 import dmd.InlineCostState;
|
|
13 import dmd.InlineDoState;
|
|
14 import dmd.InlineScanState;
|
|
15 import dmd.IRState;
|
|
16 import dmd.BE;
|
|
17 import dmd.WANT;
|
|
18 import dmd.ScopeDsymbol;
|
|
19 import dmd.Type;
|
|
20 import dmd.CondExp;
|
|
21 import dmd.AndAndExp;
|
|
22 import dmd.OrOrExp;
|
|
23 import dmd.AssignExp;
|
|
24 import dmd.VarExp;
|
|
25
|
|
26 import dmd.backend.elem;
|
|
27 import dmd.backend.Util;
|
|
28 import dmd.backend.block;
|
|
29 import dmd.backend.Blockx;
|
|
30 import dmd.backend.BC;
|
|
31
|
|
32 class IfStatement : Statement
|
|
33 {
|
|
34 Argument arg;
|
|
35 Expression condition;
|
|
36 Statement ifbody;
|
|
37 Statement elsebody;
|
|
38
|
|
39 VarDeclaration match; // for MatchExpression results
|
|
40
|
|
41 this(Loc loc, Argument arg, Expression condition, Statement ifbody, Statement elsebody)
|
|
42 {
|
|
43 super(loc);
|
|
44 this.arg = arg;
|
|
45 this.condition = condition;
|
|
46 this.ifbody = ifbody;
|
|
47 this.elsebody = elsebody;
|
|
48 }
|
|
49
|
|
50 Statement syntaxCopy()
|
|
51 {
|
51
|
52 Statement i = null;
|
|
53 if (ifbody)
|
|
54 i = ifbody.syntaxCopy();
|
|
55
|
|
56 Statement e = null;
|
|
57 if (elsebody)
|
|
58 e = elsebody.syntaxCopy();
|
|
59
|
|
60 Argument a = arg ? arg.syntaxCopy() : null;
|
|
61 IfStatement s = new IfStatement(loc, a, condition.syntaxCopy(), i, e);
|
|
62 return s;
|
0
|
63 }
|
|
64
|
|
65 Statement semantic(Scope sc)
|
|
66 {
|
|
67 condition = condition.semantic(sc);
|
|
68 condition = resolveProperties(sc, condition);
|
|
69 condition = condition.checkToBoolean();
|
|
70
|
|
71 // If we can short-circuit evaluate the if statement, don't do the
|
|
72 // semantic analysis of the skipped code.
|
|
73 // This feature allows a limited form of conditional compilation.
|
|
74 condition = condition.optimize(WANT.WANTflags);
|
|
75
|
|
76 // Evaluate at runtime
|
|
77 uint cs0 = sc.callSuper;
|
|
78 uint cs1;
|
|
79
|
|
80 Scope scd;
|
|
81 if (arg)
|
|
82 {
|
|
83 /* Declare arg, which we will set to be the
|
|
84 * result of condition.
|
|
85 */
|
|
86 ScopeDsymbol sym = new ScopeDsymbol();
|
|
87 sym.parent = sc.scopesym;
|
|
88 scd = sc.push(sym);
|
|
89
|
|
90 Type t = arg.type ? arg.type : condition.type;
|
|
91 match = new VarDeclaration(loc, t, arg.ident, null);
|
|
92 match.noauto = true;
|
|
93 match.semantic(scd);
|
|
94 if (!scd.insert(match))
|
|
95 assert(0);
|
|
96
|
|
97 match.parent = sc.func;
|
|
98
|
|
99 /* Generate:
|
|
100 * (arg = condition)
|
|
101 */
|
|
102 VarExp v = new VarExp(Loc(0), match);
|
|
103 condition = new AssignExp(loc, v, condition);
|
|
104 condition = condition.semantic(scd);
|
|
105 }
|
|
106 else
|
|
107 scd = sc.push();
|
|
108
|
|
109 ifbody = ifbody.semantic(scd);
|
|
110 scd.pop();
|
|
111
|
|
112 cs1 = sc.callSuper;
|
|
113 sc.callSuper = cs0;
|
|
114 if (elsebody)
|
|
115 elsebody = elsebody.semanticScope(sc, null, null);
|
|
116
|
|
117 sc.mergeCallSuper(loc, cs1);
|
|
118
|
|
119 return this;
|
|
120 }
|
|
121
|
|
122 Expression interpret(InterState* istate)
|
|
123 {
|
|
124 assert(false);
|
|
125 }
|
|
126
|
|
127 void toCBuffer(OutBuffer buf, HdrGenState* hgs)
|
|
128 {
|
|
129 assert(false);
|
|
130 }
|
|
131
|
|
132 bool usesEH()
|
|
133 {
|
|
134 assert(false);
|
|
135 }
|
|
136
|
|
137 BE blockExit()
|
|
138 {
|
|
139 //printf("IfStatement::blockExit(%p)\n", this);
|
|
140
|
|
141 BE result = BE.BEnone;
|
|
142 if (condition.canThrow())
|
|
143 result |= BE.BEthrow;
|
|
144 if (condition.isBool(true))
|
|
145 {
|
|
146 if (ifbody)
|
|
147 result |= ifbody.blockExit();
|
|
148 else
|
|
149 result |= BE.BEfallthru;
|
|
150 }
|
|
151 else if (condition.isBool(false))
|
|
152 {
|
|
153 if (elsebody)
|
|
154 result |= elsebody.blockExit();
|
|
155 else
|
|
156 result |= BE.BEfallthru;
|
|
157 }
|
|
158 else
|
|
159 {
|
|
160 if (ifbody)
|
|
161 result |= ifbody.blockExit();
|
|
162 else
|
|
163 result |= BE.BEfallthru;
|
|
164
|
|
165 if (elsebody)
|
|
166 result |= elsebody.blockExit();
|
|
167 else
|
|
168 result |= BE.BEfallthru;
|
|
169 }
|
|
170
|
|
171 //printf("IfStatement::blockExit(%p) = x%x\n", this, result);
|
|
172 return result;
|
|
173 }
|
|
174
|
|
175 IfStatement isIfStatement() { return this; }
|
|
176
|
|
177 int inlineCost(InlineCostState* ics)
|
|
178 {
|
|
179 int cost;
|
|
180
|
|
181 /* Can't declare variables inside ?: expressions, so
|
|
182 * we cannot inline if a variable is declared.
|
|
183 */
|
|
184 if (arg)
|
|
185 return COST_MAX;
|
|
186
|
|
187 cost = condition.inlineCost(ics);
|
|
188
|
|
189 /* Specifically allow:
|
|
190 * if (condition)
|
|
191 * return exp1;
|
|
192 * else
|
|
193 * return exp2;
|
|
194 * Otherwise, we can't handle return statements nested in if's.
|
|
195 */
|
|
196
|
|
197 if (elsebody && ifbody &&
|
|
198 ifbody.isReturnStatement() &&
|
|
199 elsebody.isReturnStatement())
|
|
200 {
|
|
201 cost += ifbody.inlineCost(ics);
|
|
202 cost += elsebody.inlineCost(ics);
|
|
203 //printf("cost = %d\n", cost);
|
|
204 }
|
|
205 else
|
|
206 {
|
|
207 ics.nested += 1;
|
|
208 if (ifbody)
|
|
209 cost += ifbody.inlineCost(ics);
|
|
210 if (elsebody)
|
|
211 cost += elsebody.inlineCost(ics);
|
|
212 ics.nested -= 1;
|
|
213 }
|
|
214 return cost;
|
|
215 }
|
|
216
|
|
217 Expression doInline(InlineDoState ids)
|
|
218 {
|
|
219 Expression econd;
|
|
220 Expression e1;
|
|
221 Expression e2;
|
|
222 Expression e;
|
|
223
|
|
224 assert(!arg);
|
|
225 econd = condition.doInline(ids);
|
|
226 assert(econd);
|
|
227 if (ifbody)
|
|
228 e1 = ifbody.doInline(ids);
|
|
229 else
|
|
230 e1 = null;
|
|
231 if (elsebody)
|
|
232 e2 = elsebody.doInline(ids);
|
|
233 else
|
|
234 e2 = null;
|
|
235 if (e1 && e2)
|
|
236 {
|
|
237 e = new CondExp(econd.loc, econd, e1, e2);
|
|
238 e.type = e1.type;
|
|
239 }
|
|
240 else if (e1)
|
|
241 {
|
|
242 e = new AndAndExp(econd.loc, econd, e1);
|
|
243 e.type = Type.tvoid;
|
|
244 }
|
|
245 else if (e2)
|
|
246 {
|
|
247 e = new OrOrExp(econd.loc, econd, e2);
|
|
248 e.type = Type.tvoid;
|
|
249 }
|
|
250 else
|
|
251 {
|
|
252 e = econd;
|
|
253 }
|
|
254 return e;
|
|
255 }
|
|
256
|
|
257 Statement inlineScan(InlineScanState* iss)
|
|
258 {
|
|
259 condition = condition.inlineScan(iss);
|
|
260 if (ifbody)
|
|
261 ifbody = ifbody.inlineScan(iss);
|
|
262 if (elsebody)
|
|
263 elsebody = elsebody.inlineScan(iss);
|
|
264 return this;
|
|
265 }
|
|
266
|
|
267 void toIR(IRState* irs)
|
|
268 {
|
|
269 elem* e;
|
|
270 Blockx* blx = irs.blx;
|
|
271
|
|
272 //printf("IfStatement::toIR('%s')\n", condition.toChars());
|
|
273
|
|
274 IRState mystate = IRState(irs, this);
|
|
275
|
|
276 // bexit is the block that gets control after this IfStatement is done
|
|
277 block* bexit = mystate.breakBlock ? mystate.breakBlock : block_calloc();
|
|
278
|
|
279 incUsage(irs, loc);
|
|
280 static if (false) {
|
|
281 if (match)
|
|
282 {
|
|
283 /* Generate:
|
|
284 * if (match = RTLSYM_IFMATCH(string, pattern)) ...
|
|
285 */
|
|
286 assert(condition.op == TOK.TOKmatch);
|
|
287 e = matchexp_toelem(cast(MatchExp)condition, &mystate, RTLSYM.RTLSYM_IFMATCH);
|
|
288 Symbol *s = match.toSymbol();
|
|
289 symbol_add(s);
|
|
290 e = el_bin(OPeq, TYnptr, el_var(s), e);
|
|
291 }
|
|
292 else
|
|
293 {
|
|
294 e = condition.toElem(&mystate);
|
|
295 }
|
|
296 } else {
|
|
297 e = condition.toElem(&mystate);
|
|
298 }
|
|
299 block_appendexp(blx.curblock, e);
|
|
300 block* bcond = blx.curblock;
|
|
301 block_next(blx, BC.BCiftrue, null);
|
|
302
|
|
303 list_append(&bcond.Bsucc, blx.curblock);
|
|
304 if (ifbody)
|
|
305 ifbody.toIR(&mystate);
|
|
306
|
|
307 list_append(&blx.curblock.Bsucc, bexit);
|
|
308
|
|
309 if (elsebody)
|
|
310 {
|
|
311 block_next(blx, BC.BCgoto, null);
|
|
312 list_append(&bcond.Bsucc, blx.curblock);
|
|
313 elsebody.toIR(&mystate);
|
|
314 list_append(&blx.curblock.Bsucc, bexit);
|
|
315 }
|
|
316 else
|
|
317 list_append(&bcond.Bsucc, bexit);
|
|
318
|
|
319 block_next(blx, BC.BCgoto, bexit);
|
|
320 }
|
|
321 } |