Mercurial > projects > ddmd
annotate dmd/SwitchStatement.d @ 135:af1bebfd96a4 dmd2037
dmd 2.038
author | Eldar Insafutdinov <e.insafutdinov@gmail.com> |
---|---|
date | Mon, 13 Sep 2010 22:19:42 +0100 |
parents | c77e9f4f1793 |
children | 438eaa11eed4 |
rev | line source |
---|---|
0 | 1 module dmd.SwitchStatement; |
2 | |
114 | 3 import dmd.common; |
0 | 4 import dmd.Statement; |
5 import dmd.Expression; | |
6 import dmd.DefaultStatement; | |
7 import dmd.TryFinallyStatement; | |
8 import dmd.Array; | |
9 import dmd.Loc; | |
10 import dmd.Scope; | |
11 import dmd.OutBuffer; | |
12 import dmd.HdrGenState; | |
13 import dmd.InlineScanState; | |
14 import dmd.IRState; | |
15 import dmd.InterState; | |
16 import dmd.BE; | |
17 import dmd.TY; | |
18 import dmd.WANT; | |
19 import dmd.GotoCaseStatement; | |
20 import dmd.CaseStatement; | |
21 import dmd.ArrayTypes; | |
22 import dmd.CompoundStatement; | |
23 import dmd.Global; | |
24 import dmd.SwitchErrorStatement; | |
25 import dmd.Type; | |
26 import dmd.HaltExp; | |
27 import dmd.ExpStatement; | |
28 import dmd.BreakStatement; | |
29 import dmd.EnumDeclaration; | |
30 import dmd.TypeEnum; | |
31 import dmd.Dsymbol; | |
32 import dmd.EnumMember; | |
33 import dmd.TypeTypedef; | |
34 import dmd.TOK; | |
35 import dmd.StringExp; | |
36 | |
37 import dmd.backend.Util; | |
38 import dmd.backend.block; | |
39 import dmd.backend.Blockx; | |
40 import dmd.backend.elem; | |
41 import dmd.backend.OPER; | |
42 import dmd.backend.TYM; | |
43 import dmd.backend.BC; | |
44 import dmd.backend.dt_t; | |
45 import dmd.backend.Symbol; | |
46 import dmd.backend.SC; | |
47 import dmd.backend.FL; | |
48 import dmd.backend.RTLSYM; | |
49 import dmd.backend.targ_types; | |
50 | |
4 | 51 import core.memory; |
2 | 52 |
5
63623152e82a
Fixed memory corruption bug which was introduced when attempting to restore GC functionality
dkoroskin <>
parents:
4
diff
changeset
|
53 import core.stdc.stdlib; |
63623152e82a
Fixed memory corruption bug which was introduced when attempting to restore GC functionality
dkoroskin <>
parents:
4
diff
changeset
|
54 |
0 | 55 class SwitchStatement : Statement |
56 { | |
57 Expression condition; | |
58 Statement body_; | |
59 bool isFinal; | |
60 | |
61 DefaultStatement sdefault = null; | |
62 TryFinallyStatement tf = null; | |
63 Array gotoCases; // array of unresolved GotoCaseStatement's | |
64 Array cases; // array of CaseStatement's | |
65 int hasNoDefault = 0; // !=0 if no default statement | |
66 int hasVars = 0; // !=0 if has variable case values | |
67 | |
68 this(Loc loc, Expression c, Statement b, bool isFinal) | |
69 { | |
70 super(loc); | |
71 | |
72 this.condition = c; | |
73 this.body_ = b; | |
74 this.isFinal = isFinal; | |
75 | |
76 gotoCases = new Array(); | |
77 } | |
78 | |
72 | 79 override Statement syntaxCopy() |
0 | 80 { |
53 | 81 SwitchStatement s = new SwitchStatement(loc, |
82 condition.syntaxCopy(), body_.syntaxCopy(), isFinal); | |
83 return s; | |
0 | 84 } |
85 | |
72 | 86 override Statement semantic(Scope sc) |
0 | 87 { |
88 //printf("SwitchStatement.semantic(%p)\n", this); | |
89 tf = sc.tf; | |
90 assert(!cases); // ensure semantic() is only run once | |
91 condition = condition.semantic(sc); | |
92 condition = resolveProperties(sc, condition); | |
93 if (condition.type.isString()) | |
94 { | |
95 // If it's not an array, cast it to one | |
96 if (condition.type.ty != Tarray) | |
97 { | |
98 condition = condition.implicitCastTo(sc, condition.type.nextOf().arrayOf()); | |
99 } | |
100 condition.type = condition.type.constOf(); | |
101 } | |
102 else | |
103 { | |
104 condition = condition.integralPromotions(sc); | |
105 condition.checkIntegral(); | |
106 } | |
107 condition = condition.optimize(WANTvalue); | |
108 | |
109 sc = sc.push(); | |
110 sc.sbreak = this; | |
111 sc.sw = this; | |
112 | |
113 cases = new Array(); | |
114 sc.noctor++; // BUG: should use Scope.mergeCallSuper() for each case instead | |
115 body_ = body_.semantic(sc); | |
116 sc.noctor--; | |
117 | |
118 // Resolve any goto case's with exp | |
119 for (int i = 0; i < gotoCases.dim; i++) | |
120 { | |
121 GotoCaseStatement gcs = cast(GotoCaseStatement)gotoCases.data[i]; | |
122 | |
123 if (!gcs.exp) | |
124 { | |
125 gcs.error("no case statement following goto case;"); | |
126 break; | |
127 } | |
128 | |
129 for (Scope scx = sc; scx; scx = scx.enclosing) | |
130 { | |
131 if (!scx.sw) | |
132 continue; | |
133 for (int j = 0; j < scx.sw.cases.dim; j++) | |
134 { | |
135 CaseStatement cs = cast(CaseStatement)scx.sw.cases.data[j]; | |
136 | |
137 if (cs.exp.equals(gcs.exp)) | |
138 { | |
139 gcs.cs = cs; | |
140 goto Lfoundcase; | |
141 } | |
142 } | |
143 } | |
144 gcs.error("case %s not found", gcs.exp.toChars()); | |
145 | |
146 Lfoundcase: | |
147 ; | |
148 } | |
149 | |
150 if (!sc.sw.sdefault && !isFinal) | |
151 { | |
152 hasNoDefault = 1; | |
153 | |
154 warning("switch statement has no default"); | |
155 | |
156 // Generate runtime error if the default is hit | |
157 Statements a = new Statements(); | |
158 CompoundStatement cs; | |
159 Statement s; | |
160 | |
161 if (global.params.useSwitchError) | |
162 s = new SwitchErrorStatement(loc); | |
163 else | |
164 { | |
165 Expression e = new HaltExp(loc); | |
166 s = new ExpStatement(loc, e); | |
167 } | |
168 | |
169 a.reserve(4); | |
122
c77e9f4f1793
Statements -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
170 a.push(body_); |
c77e9f4f1793
Statements -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
171 a.push(new BreakStatement(loc, null)); |
0 | 172 sc.sw.sdefault = new DefaultStatement(loc, s); |
122
c77e9f4f1793
Statements -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
173 a.push(sc.sw.sdefault); |
0 | 174 cs = new CompoundStatement(loc, a); |
175 body_ = cs; | |
176 } | |
177 | |
178 version (DMDV2) { | |
179 if (isFinal) | |
180 { | |
181 Type t = condition.type; | |
182 while (t.ty == Ttypedef) | |
183 { | |
184 // Don't use toBasetype() because that will skip past enums | |
185 t = (cast(TypeTypedef)t).sym.basetype; | |
186 } | |
187 if (condition.type.ty == Tenum) | |
188 { | |
189 TypeEnum te = cast(TypeEnum)condition.type; | |
190 EnumDeclaration ed = te.toDsymbol(sc).isEnumDeclaration(); | |
191 assert(ed); | |
192 size_t dim = ed.members.dim; | |
77
ad4792a1cfd6
more D-ification container accessing
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
72
diff
changeset
|
193 foreach (Dsymbol s; ed.members) |
0 | 194 { |
77
ad4792a1cfd6
more D-ification container accessing
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
72
diff
changeset
|
195 if (auto em = s.isEnumMember()) |
0 | 196 { |
197 for (size_t j = 0; j < cases.dim; j++) | |
198 { | |
199 CaseStatement cs = cast(CaseStatement)cases.data[j]; | |
200 if (cs.exp.equals(em.value)) | |
201 goto L1; | |
202 } | |
203 error("enum member %s not represented in final switch", em.toChars()); | |
204 } | |
205 L1: | |
206 ; | |
207 } | |
208 } | |
209 } | |
210 } | |
211 | |
212 sc.pop(); | |
213 return this; | |
214 } | |
215 | |
72 | 216 override bool hasBreak() |
0 | 217 { |
218 assert(false); | |
219 } | |
220 | |
72 | 221 override bool usesEH() |
0 | 222 { |
223 assert(false); | |
224 } | |
225 | |
72 | 226 override BE blockExit() |
0 | 227 { |
228 BE result = BE.BEnone; | |
229 if (condition.canThrow()) | |
230 result |= BE.BEthrow; | |
231 | |
232 if (body_) | |
233 { result |= body_.blockExit(); | |
234 if (result & BE.BEbreak) | |
235 { | |
236 result |= BE.BEfallthru; | |
237 result &= ~BE.BEbreak; | |
238 } | |
239 } | |
240 else | |
241 result |= BE.BEfallthru; | |
242 | |
243 return result; | |
244 } | |
245 | |
72 | 246 override Expression interpret(InterState istate) |
0 | 247 { |
248 assert(false); | |
249 } | |
250 | |
72 | 251 override void toCBuffer(OutBuffer buf, HdrGenState* hgs) |
0 | 252 { |
253 assert(false); | |
254 } | |
255 | |
72 | 256 override Statement inlineScan(InlineScanState* iss) |
0 | 257 { |
258 //printf("SwitchStatement.inlineScan()\n"); | |
259 condition = condition.inlineScan(iss); | |
260 body_ = body_ ? body_.inlineScan(iss) : null; | |
261 if (sdefault) | |
262 sdefault = cast(DefaultStatement)sdefault.inlineScan(iss); | |
263 if (cases) | |
264 { | |
265 for (int i = 0; i < cases.dim; i++) | |
266 { | |
267 Statement s = cast(Statement)cases.data[i]; | |
268 cases.data[i] = cast(void*)s.inlineScan(iss); | |
269 } | |
270 } | |
271 return this; | |
272 } | |
273 | |
72 | 274 override void toIR(IRState* irs) |
0 | 275 { |
276 int string; | |
277 Blockx* blx = irs.blx; | |
278 | |
279 //printf("SwitchStatement.toIR()\n"); | |
280 IRState mystate = IRState(irs,this); | |
281 | |
282 mystate.switchBlock = blx.curblock; | |
283 | |
284 /* Block for where "break" goes to | |
285 */ | |
286 mystate.breakBlock = block_calloc(blx); | |
287 | |
288 /* Block for where "default" goes to. | |
289 * If there is a default statement, then that is where default goes. | |
290 * If not, then do: | |
291 * default: break; | |
292 * by making the default block the same as the break block. | |
293 */ | |
294 mystate.defaultBlock = sdefault ? block_calloc(blx) : mystate.breakBlock; | |
295 | |
296 int numcases = 0; | |
297 if (cases) | |
298 numcases = cases.dim; | |
299 | |
300 incUsage(irs, loc); | |
301 elem* econd = condition.toElem(&mystate); | |
302 | |
303 version (DMDV2) { | |
304 if (hasVars) | |
305 { | |
306 /* Generate a sequence of if-then-else blocks for the cases. | |
307 */ | |
308 if (econd.Eoper != OPvar) | |
309 { | |
310 elem* e = exp2_copytotemp(econd); | |
311 block_appendexp(mystate.switchBlock, e); | |
312 econd = e.E2; | |
313 } | |
314 | |
315 for (int i = 0; i < numcases; i++) | |
316 { | |
317 CaseStatement cs = cast(CaseStatement)cases.data[i]; | |
318 | |
319 elem* ecase = cs.exp.toElem(&mystate); | |
320 elem* e = el_bin(OPeqeq, TYbool, el_copytree(econd), ecase); | |
321 block* b = blx.curblock; | |
322 block_appendexp(b, e); | |
323 block* bcase = block_calloc(blx); | |
324 cs.cblock = bcase; | |
325 block_next(blx, BCiftrue, null); | |
326 list_append(&b.Bsucc, bcase); | |
327 list_append(&b.Bsucc, blx.curblock); | |
328 } | |
329 | |
330 /* The final 'else' clause goes to the default | |
331 */ | |
332 block* b = blx.curblock; | |
333 block_next(blx, BCgoto, null); | |
334 list_append(&b.Bsucc, mystate.defaultBlock); | |
335 | |
336 body_.toIR(&mystate); | |
337 | |
338 /* Have the end of the switch body fall through to the block | |
339 * following the switch statement. | |
340 */ | |
341 block_goto(blx, BCgoto, mystate.breakBlock); | |
342 return; | |
343 } | |
344 } | |
345 | |
346 if (condition.type.isString()) | |
347 { | |
348 // Number the cases so we can unscramble things after the sort() | |
349 for (int i = 0; i < numcases; i++) | |
350 { | |
351 CaseStatement cs = cast(CaseStatement)cases.data[i]; | |
352 cs.index = i; | |
353 } | |
354 | |
355 cases.sort(); | |
356 | |
357 /* Create a sorted array of the case strings, and si | |
358 * will be the symbol for it. | |
359 */ | |
360 dt_t* dt = null; | |
361 Symbol* si = symbol_generate(SCstatic,type_fake(TYullong)); | |
362 version (MACHOBJ) { | |
363 si.Sseg = Segment.DATA; | |
364 } | |
365 dtdword(&dt, numcases); | |
366 dtxoff(&dt, si, 8, TYnptr); | |
367 | |
368 for (int i = 0; i < numcases; i++) | |
369 { | |
370 CaseStatement cs = cast(CaseStatement)cases.data[i]; | |
371 | |
372 if (cs.exp.op != TOKstring) | |
373 { | |
374 error("case '%s' is not a string", cs.exp.toChars()); // BUG: this should be an assert | |
375 } | |
376 else | |
377 { | |
378 StringExp se = cast(StringExp)(cs.exp); | |
379 uint len = se.len; | |
380 dtdword(&dt, len); | |
381 dtabytes(&dt, TYnptr, 0, se.len * se.sz, cast(char*)se.string_); | |
382 } | |
383 } | |
384 | |
385 si.Sdt = dt; | |
386 si.Sfl = FLdata; | |
387 outdata(si); | |
388 | |
389 /* Call: | |
390 * _d_switch_string(string[] si, string econd) | |
391 */ | |
392 elem* eparam = el_param(econd, el_var(si)); | |
393 switch (condition.type.nextOf().ty) | |
394 { | |
395 case Tchar: | |
396 econd = el_bin(OPcall, TYint, el_var(rtlsym[RTLSYM_SWITCH_STRING]), eparam); | |
397 break; | |
398 case Twchar: | |
399 econd = el_bin(OPcall, TYint, el_var(rtlsym[RTLSYM_SWITCH_USTRING]), eparam); | |
400 break; | |
401 case Tdchar: // BUG: implement | |
402 econd = el_bin(OPcall, TYint, el_var(rtlsym[RTLSYM_SWITCH_DSTRING]), eparam); | |
403 break; | |
404 default: | |
405 assert(0); | |
406 } | |
407 elem_setLoc(econd, loc); | |
408 string = 1; | |
409 } | |
410 else | |
411 string = 0; | |
412 block_appendexp(mystate.switchBlock, econd); | |
413 block_next(blx,BCswitch,null); | |
414 | |
135 | 415 // Corresponding free is in block_free |
5
63623152e82a
Fixed memory corruption bug which was introduced when attempting to restore GC functionality
dkoroskin <>
parents:
4
diff
changeset
|
416 targ_llong* pu = cast(targ_llong*) malloc(targ_llong.sizeof * (numcases + 1)); |
0 | 417 mystate.switchBlock.Bswitch = pu; |
418 /* First pair is the number of cases, and the default block | |
419 */ | |
420 *pu++ = numcases; | |
421 list_append(&mystate.switchBlock.Bsucc, mystate.defaultBlock); | |
422 | |
423 /* Fill in the first entry in each pair, which is the case value. | |
424 * CaseStatement.toIR() will fill in | |
425 * the second entry for each pair with the block. | |
426 */ | |
427 for (int i = 0; i < numcases; i++) | |
428 { | |
429 CaseStatement cs = cast(CaseStatement)cases.data[i]; | |
430 if (string) | |
431 { | |
432 pu[cs.index] = i; | |
433 } | |
434 else | |
435 { | |
436 pu[i] = cs.exp.toInteger(); | |
437 } | |
438 } | |
439 | |
440 body_.toIR(&mystate); | |
441 | |
442 /* Have the end of the switch body fall through to the block | |
443 * following the switch statement. | |
444 */ | |
445 block_goto(blx, BCgoto, mystate.breakBlock); | |
446 } | |
72 | 447 } |