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