Mercurial > projects > ldc
annotate dmd/statement.c @ 1130:30663babccff
Bring FrameInfo definition up to date with recent Tango developments. Thanks fawzi!
author | Christian Kamm <kamm incasoftware de> |
---|---|
date | Sun, 22 Mar 2009 14:51:03 +0100 |
parents | e7f0c2b48047 |
children | eeb8b95ea92e |
rev | line source |
---|---|
159 | 1 |
2 // Compiler implementation of the D programming language | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
945
diff
changeset
|
3 // Copyright (c) 1999-2009 by Digital Mars |
159 | 4 // All Rights Reserved |
5 // written by Walter Bright | |
6 // http://www.digitalmars.com | |
7 // License for redistribution is by either the Artistic License | |
8 // in artistic.txt, or the GNU General Public License in gnu.txt. | |
9 // See the included readme.txt for details. | |
10 | |
11 #include <stdio.h> | |
12 #include <stdlib.h> | |
13 #include <assert.h> | |
14 | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
945
diff
changeset
|
15 #include "rmem.h" |
159 | 16 |
17 #include "statement.h" | |
18 #include "expression.h" | |
19 #include "cond.h" | |
20 #include "init.h" | |
21 #include "staticassert.h" | |
22 #include "mtype.h" | |
23 #include "scope.h" | |
24 #include "declaration.h" | |
25 #include "aggregate.h" | |
26 #include "id.h" | |
27 #include "hdrgen.h" | |
28 #include "parse.h" | |
846
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
29 #include "template.h" |
159 | 30 |
31 /******************************** Statement ***************************/ | |
32 | |
33 Statement::Statement(Loc loc) | |
34 : loc(loc) | |
35 { | |
36 #ifdef _DH | |
37 // If this is an in{} contract scope statement (skip for determining | |
38 // inlineStatus of a function body for header content) | |
39 incontract = 0; | |
40 #endif | |
41 } | |
42 | |
43 Statement *Statement::syntaxCopy() | |
44 { | |
45 assert(0); | |
46 return NULL; | |
47 } | |
48 | |
49 void Statement::print() | |
50 { | |
51 fprintf(stdmsg, "%s\n", toChars()); | |
52 fflush(stdmsg); | |
53 } | |
54 | |
55 char *Statement::toChars() | |
56 { OutBuffer *buf; | |
57 HdrGenState hgs; | |
58 | |
59 buf = new OutBuffer(); | |
60 toCBuffer(buf, &hgs); | |
61 return buf->toChars(); | |
62 } | |
63 | |
64 void Statement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
65 { | |
66 buf->printf("Statement::toCBuffer()"); | |
67 buf->writenl(); | |
68 } | |
69 | |
70 Statement *Statement::semantic(Scope *sc) | |
71 { | |
72 return this; | |
73 } | |
74 | |
75 // Same as semantic(), but do create a new scope | |
76 | |
77 Statement *Statement::semanticScope(Scope *sc, Statement *sbreak, Statement *scontinue) | |
78 { Scope *scd; | |
79 Statement *s; | |
80 | |
81 scd = sc->push(); | |
82 if (sbreak) | |
83 scd->sbreak = sbreak; | |
84 if (scontinue) | |
85 scd->scontinue = scontinue; | |
86 s = semantic(scd); | |
87 scd->pop(); | |
88 return s; | |
89 } | |
90 | |
91 void Statement::error(const char *format, ...) | |
92 { | |
93 va_list ap; | |
94 va_start(ap, format); | |
95 ::verror(loc, format, ap); | |
96 va_end( ap ); | |
97 } | |
98 | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
945
diff
changeset
|
99 void Statement::warning(const char *format, ...) |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
945
diff
changeset
|
100 { |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
945
diff
changeset
|
101 if (global.params.warnings && !global.gag) |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
945
diff
changeset
|
102 { |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
945
diff
changeset
|
103 va_list ap; |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
945
diff
changeset
|
104 va_start(ap, format); |
1124
e7f0c2b48047
Fix a bug where ::warning() was called with a va_list argument instead of an
Frits van Bommel <fvbommel wxs.nl>
parents:
1103
diff
changeset
|
105 ::vwarning(loc, format, ap); |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
945
diff
changeset
|
106 va_end( ap ); |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
945
diff
changeset
|
107 } |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
945
diff
changeset
|
108 } |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
945
diff
changeset
|
109 |
159 | 110 int Statement::hasBreak() |
111 { | |
112 //printf("Statement::hasBreak()\n"); | |
113 return FALSE; | |
114 } | |
115 | |
116 int Statement::hasContinue() | |
117 { | |
118 return FALSE; | |
119 } | |
120 | |
121 // TRUE if statement uses exception handling | |
122 | |
123 int Statement::usesEH() | |
124 { | |
125 return FALSE; | |
126 } | |
127 | |
336 | 128 /* Only valid after semantic analysis |
129 */ | |
130 int Statement::blockExit() | |
131 { | |
132 printf("Statement::blockExit(%p)\n", this); | |
133 printf("%s\n", toChars()); | |
134 assert(0); | |
135 return BEany; | |
136 } | |
137 | |
159 | 138 // TRUE if statement 'comes from' somewhere else, like a goto |
139 | |
140 int Statement::comeFrom() | |
141 { | |
142 //printf("Statement::comeFrom()\n"); | |
143 return FALSE; | |
144 } | |
145 | |
146 /**************************************** | |
147 * If this statement has code that needs to run in a finally clause | |
148 * at the end of the current scope, return that code in the form of | |
149 * a Statement. | |
150 * Output: | |
151 * *sentry code executed upon entry to the scope | |
152 * *sexception code executed upon exit from the scope via exception | |
153 * *sfinally code executed in finally block | |
154 */ | |
155 | |
156 void Statement::scopeCode(Statement **sentry, Statement **sexception, Statement **sfinally) | |
157 { | |
158 //printf("Statement::scopeCode()\n"); | |
159 //print(); | |
160 *sentry = NULL; | |
161 *sexception = NULL; | |
162 *sfinally = NULL; | |
163 } | |
164 | |
165 /********************************* | |
166 * Flatten out the scope by presenting the statement | |
167 * as an array of statements. | |
168 * Returns NULL if no flattening necessary. | |
169 */ | |
170 | |
171 Statements *Statement::flatten(Scope *sc) | |
172 { | |
173 return NULL; | |
174 } | |
175 | |
176 | |
177 /******************************** ExpStatement ***************************/ | |
178 | |
179 ExpStatement::ExpStatement(Loc loc, Expression *exp) | |
180 : Statement(loc) | |
181 { | |
182 this->exp = exp; | |
183 } | |
184 | |
185 Statement *ExpStatement::syntaxCopy() | |
186 { | |
187 Expression *e = exp ? exp->syntaxCopy() : NULL; | |
188 ExpStatement *es = new ExpStatement(loc, e); | |
189 return es; | |
190 } | |
191 | |
192 void ExpStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
193 { | |
194 if (exp) | |
195 exp->toCBuffer(buf, hgs); | |
196 buf->writeByte(';'); | |
197 if (!hgs->FLinit.init) | |
198 buf->writenl(); | |
199 } | |
200 | |
201 Statement *ExpStatement::semantic(Scope *sc) | |
202 { | |
203 if (exp) | |
204 { | |
205 //printf("ExpStatement::semantic() %s\n", exp->toChars()); | |
206 exp = exp->semantic(sc); | |
207 exp = resolveProperties(sc, exp); | |
208 exp->checkSideEffect(0); | |
209 exp = exp->optimize(0); | |
210 //exp = exp->optimize(isDeclarationStatement() ? WANTvalue : 0); | |
211 } | |
212 return this; | |
213 } | |
214 | |
336 | 215 int ExpStatement::blockExit() |
216 { int result = BEfallthru; | |
217 | |
218 if (exp) | |
219 { | |
220 if (exp->op == TOKhalt) | |
221 return BEhalt; | |
222 if (exp->op == TOKassert) | |
223 { AssertExp *a = (AssertExp *)exp; | |
224 | |
225 if (a->e1->isBool(FALSE)) // if it's an assert(0) | |
226 return BEhalt; | |
227 } | |
228 if (exp->canThrow()) | |
229 result |= BEthrow; | |
230 } | |
231 return result; | |
232 } | |
233 | |
159 | 234 |
235 /******************************** CompileStatement ***************************/ | |
236 | |
237 CompileStatement::CompileStatement(Loc loc, Expression *exp) | |
238 : Statement(loc) | |
239 { | |
240 this->exp = exp; | |
241 } | |
242 | |
243 Statement *CompileStatement::syntaxCopy() | |
244 { | |
245 Expression *e = exp->syntaxCopy(); | |
246 CompileStatement *es = new CompileStatement(loc, e); | |
247 return es; | |
248 } | |
249 | |
250 void CompileStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
251 { | |
252 buf->writestring("mixin("); | |
253 exp->toCBuffer(buf, hgs); | |
254 buf->writestring(");"); | |
255 if (!hgs->FLinit.init) | |
256 buf->writenl(); | |
257 } | |
258 | |
336 | 259 Statements *CompileStatement::flatten(Scope *sc) |
159 | 260 { |
336 | 261 //printf("CompileStatement::flatten() %s\n", exp->toChars()); |
159 | 262 exp = exp->semantic(sc); |
263 exp = resolveProperties(sc, exp); | |
264 exp = exp->optimize(WANTvalue | WANTinterpret); | |
265 if (exp->op != TOKstring) | |
266 { error("argument to mixin must be a string, not (%s)", exp->toChars()); | |
336 | 267 return NULL; |
159 | 268 } |
269 StringExp *se = (StringExp *)exp; | |
270 se = se->toUTF8(sc); | |
271 Parser p(sc->module, (unsigned char *)se->string, se->len, 0); | |
272 p.loc = loc; | |
273 p.nextToken(); | |
274 | |
336 | 275 Statements *a = new Statements(); |
159 | 276 while (p.token.value != TOKeof) |
277 { | |
278 Statement *s = p.parseStatement(PSsemi | PScurlyscope); | |
336 | 279 a->push(s); |
159 | 280 } |
336 | 281 return a; |
282 } | |
283 | |
284 Statement *CompileStatement::semantic(Scope *sc) | |
285 { | |
286 //printf("CompileStatement::semantic() %s\n", exp->toChars()); | |
287 Statements *a = flatten(sc); | |
288 if (!a) | |
289 return NULL; | |
290 Statement *s = new CompoundStatement(loc, a); | |
159 | 291 return s->semantic(sc); |
292 } | |
293 | |
294 | |
295 /******************************** DeclarationStatement ***************************/ | |
296 | |
297 DeclarationStatement::DeclarationStatement(Loc loc, Dsymbol *declaration) | |
298 : ExpStatement(loc, new DeclarationExp(loc, declaration)) | |
299 { | |
300 } | |
301 | |
302 DeclarationStatement::DeclarationStatement(Loc loc, Expression *exp) | |
303 : ExpStatement(loc, exp) | |
304 { | |
305 } | |
306 | |
307 Statement *DeclarationStatement::syntaxCopy() | |
308 { | |
309 DeclarationStatement *ds = new DeclarationStatement(loc, exp->syntaxCopy()); | |
310 return ds; | |
311 } | |
312 | |
313 void DeclarationStatement::scopeCode(Statement **sentry, Statement **sexception, Statement **sfinally) | |
314 { | |
315 //printf("DeclarationStatement::scopeCode()\n"); | |
316 //print(); | |
317 | |
318 *sentry = NULL; | |
319 *sexception = NULL; | |
320 *sfinally = NULL; | |
321 | |
322 if (exp) | |
323 { | |
324 if (exp->op == TOKdeclaration) | |
325 { | |
326 DeclarationExp *de = (DeclarationExp *)(exp); | |
327 VarDeclaration *v = de->declaration->isVarDeclaration(); | |
328 if (v) | |
329 { Expression *e; | |
330 | |
331 e = v->callAutoDtor(); | |
332 if (e) | |
333 { | |
334 //printf("dtor is: "); e->print(); | |
335 *sfinally = new ExpStatement(loc, e); | |
336 } | |
337 } | |
338 } | |
339 } | |
340 } | |
341 | |
342 void DeclarationStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
343 { | |
344 exp->toCBuffer(buf, hgs); | |
345 } | |
346 | |
347 | |
348 /******************************** CompoundStatement ***************************/ | |
349 | |
350 CompoundStatement::CompoundStatement(Loc loc, Statements *s) | |
351 : Statement(loc) | |
352 { | |
353 statements = s; | |
354 } | |
355 | |
356 CompoundStatement::CompoundStatement(Loc loc, Statement *s1, Statement *s2) | |
357 : Statement(loc) | |
358 { | |
359 statements = new Statements(); | |
360 statements->reserve(2); | |
361 statements->push(s1); | |
362 statements->push(s2); | |
363 } | |
364 | |
365 Statement *CompoundStatement::syntaxCopy() | |
366 { | |
367 Statements *a = new Statements(); | |
368 a->setDim(statements->dim); | |
369 for (size_t i = 0; i < statements->dim; i++) | |
370 { Statement *s = (Statement *)statements->data[i]; | |
371 if (s) | |
372 s = s->syntaxCopy(); | |
373 a->data[i] = s; | |
374 } | |
375 CompoundStatement *cs = new CompoundStatement(loc, a); | |
376 return cs; | |
377 } | |
378 | |
379 | |
380 Statement *CompoundStatement::semantic(Scope *sc) | |
381 { Statement *s; | |
382 | |
383 //printf("CompoundStatement::semantic(this = %p, sc = %p)\n", this, sc); | |
384 | |
385 for (size_t i = 0; i < statements->dim; ) | |
386 { | |
387 s = (Statement *) statements->data[i]; | |
388 if (s) | |
389 { Statements *a = s->flatten(sc); | |
390 | |
391 if (a) | |
392 { | |
393 statements->remove(i); | |
394 statements->insert(i, a); | |
395 continue; | |
396 } | |
397 s = s->semantic(sc); | |
398 statements->data[i] = s; | |
399 if (s) | |
400 { | |
401 Statement *sentry; | |
402 Statement *sexception; | |
403 Statement *sfinally; | |
404 | |
405 s->scopeCode(&sentry, &sexception, &sfinally); | |
406 if (sentry) | |
407 { | |
408 sentry = sentry->semantic(sc); | |
409 statements->data[i] = sentry; | |
410 } | |
411 if (sexception) | |
412 { | |
413 if (i + 1 == statements->dim && !sfinally) | |
414 { | |
415 #if 1 | |
416 sexception = sexception->semantic(sc); | |
417 #else | |
418 statements->push(sexception); | |
419 if (sfinally) | |
420 // Assume sexception does not throw | |
421 statements->push(sfinally); | |
422 #endif | |
423 } | |
424 else | |
425 { | |
426 /* Rewrite: | |
427 * s; s1; s2; | |
428 * As: | |
429 * s; | |
430 * try { s1; s2; } | |
431 * catch (Object __o) | |
432 * { sexception; throw __o; } | |
433 */ | |
434 Statement *body; | |
435 Statements *a = new Statements(); | |
436 | |
437 for (int j = i + 1; j < statements->dim; j++) | |
438 { | |
439 a->push(statements->data[j]); | |
440 } | |
441 body = new CompoundStatement(0, a); | |
442 body = new ScopeStatement(0, body); | |
443 | |
846
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
444 Identifier *id = Lexer::uniqueId("__o"); |
159 | 445 |
446 Statement *handler = new ThrowStatement(0, new IdentifierExp(0, id)); | |
447 handler = new CompoundStatement(0, sexception, handler); | |
448 | |
449 Array *catches = new Array(); | |
450 Catch *ctch = new Catch(0, NULL, id, handler); | |
451 catches->push(ctch); | |
452 s = new TryCatchStatement(0, body, catches); | |
453 | |
454 if (sfinally) | |
455 s = new TryFinallyStatement(0, s, sfinally); | |
456 s = s->semantic(sc); | |
457 statements->setDim(i + 1); | |
458 statements->push(s); | |
459 break; | |
460 } | |
461 } | |
462 else if (sfinally) | |
463 { | |
464 if (0 && i + 1 == statements->dim) | |
465 { | |
466 statements->push(sfinally); | |
467 } | |
468 else | |
469 { | |
470 /* Rewrite: | |
471 * s; s1; s2; | |
472 * As: | |
473 * s; try { s1; s2; } finally { sfinally; } | |
474 */ | |
475 Statement *body; | |
476 Statements *a = new Statements(); | |
477 | |
478 for (int j = i + 1; j < statements->dim; j++) | |
479 { | |
480 a->push(statements->data[j]); | |
481 } | |
482 body = new CompoundStatement(0, a); | |
483 s = new TryFinallyStatement(0, body, sfinally); | |
484 s = s->semantic(sc); | |
485 statements->setDim(i + 1); | |
486 statements->push(s); | |
487 break; | |
488 } | |
489 } | |
490 } | |
491 } | |
492 i++; | |
493 } | |
232
092468448d25
[svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents:
215
diff
changeset
|
494 if (statements->dim == 1 && !isAsmBlockStatement()) |
336 | 495 { |
496 return (Statement *)statements->data[0]; | |
497 } | |
159 | 498 return this; |
499 } | |
500 | |
501 Statements *CompoundStatement::flatten(Scope *sc) | |
502 { | |
503 return statements; | |
504 } | |
505 | |
506 ReturnStatement *CompoundStatement::isReturnStatement() | |
336 | 507 { |
159 | 508 ReturnStatement *rs = NULL; |
509 | |
336 | 510 for (int i = 0; i < statements->dim; i++) |
511 { Statement *s = (Statement *) statements->data[i]; | |
159 | 512 if (s) |
513 { | |
514 rs = s->isReturnStatement(); | |
515 if (rs) | |
516 break; | |
517 } | |
518 } | |
519 return rs; | |
520 } | |
521 | |
522 void CompoundStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
336 | 523 { |
524 for (int i = 0; i < statements->dim; i++) | |
525 { Statement *s = (Statement *) statements->data[i]; | |
159 | 526 if (s) |
527 s->toCBuffer(buf, hgs); | |
528 } | |
529 } | |
530 | |
531 int CompoundStatement::usesEH() | |
532 { | |
533 for (int i = 0; i < statements->dim; i++) | |
336 | 534 { Statement *s = (Statement *) statements->data[i]; |
159 | 535 if (s && s->usesEH()) |
536 return TRUE; | |
537 } | |
538 return FALSE; | |
539 } | |
540 | |
336 | 541 int CompoundStatement::blockExit() |
542 { | |
543 //printf("CompoundStatement::blockExit(%p) %d\n", this, statements->dim); | |
544 int result = BEfallthru; | |
545 for (size_t i = 0; i < statements->dim; i++) | |
546 { Statement *s = (Statement *) statements->data[i]; | |
547 if (s) | |
548 { | |
549 //printf("result = x%x\n", result); | |
550 //printf("%s\n", s->toChars()); | |
551 if (!(result & BEfallthru) && !s->comeFrom()) | |
552 { | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
945
diff
changeset
|
553 s->warning("statement is not reachable"); |
336 | 554 } |
555 | |
556 result &= ~BEfallthru; | |
557 result |= s->blockExit(); | |
558 } | |
559 } | |
560 return result; | |
561 } | |
562 | |
159 | 563 int CompoundStatement::comeFrom() |
564 { int comefrom = FALSE; | |
565 | |
566 //printf("CompoundStatement::comeFrom()\n"); | |
567 for (int i = 0; i < statements->dim; i++) | |
568 { Statement *s = (Statement *)statements->data[i]; | |
569 | |
570 if (!s) | |
571 continue; | |
572 | |
573 comefrom |= s->comeFrom(); | |
574 } | |
575 return comefrom; | |
576 } | |
577 | |
578 | |
579 /**************************** UnrolledLoopStatement ***************************/ | |
580 | |
581 UnrolledLoopStatement::UnrolledLoopStatement(Loc loc, Statements *s) | |
582 : Statement(loc) | |
583 { | |
584 statements = s; | |
309 | 585 enclosinghandler = NULL; |
159 | 586 } |
587 | |
588 Statement *UnrolledLoopStatement::syntaxCopy() | |
589 { | |
590 Statements *a = new Statements(); | |
591 a->setDim(statements->dim); | |
592 for (size_t i = 0; i < statements->dim; i++) | |
593 { Statement *s = (Statement *)statements->data[i]; | |
594 if (s) | |
595 s = s->syntaxCopy(); | |
596 a->data[i] = s; | |
597 } | |
598 UnrolledLoopStatement *cs = new UnrolledLoopStatement(loc, a); | |
599 return cs; | |
600 } | |
601 | |
602 | |
603 Statement *UnrolledLoopStatement::semantic(Scope *sc) | |
604 { | |
605 //printf("UnrolledLoopStatement::semantic(this = %p, sc = %p)\n", this, sc); | |
606 | |
309 | 607 enclosinghandler = sc->tfOfTry; |
159 | 608 |
609 sc->noctor++; | |
610 Scope *scd = sc->push(); | |
611 scd->sbreak = this; | |
612 scd->scontinue = this; | |
613 | |
614 for (size_t i = 0; i < statements->dim; i++) | |
615 { | |
616 Statement *s = (Statement *) statements->data[i]; | |
617 if (s) | |
618 { | |
619 s = s->semantic(scd); | |
620 statements->data[i] = s; | |
621 } | |
622 } | |
623 | |
624 scd->pop(); | |
625 sc->noctor--; | |
626 return this; | |
627 } | |
628 | |
629 void UnrolledLoopStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
630 { | |
631 buf->writestring("unrolled {"); | |
632 buf->writenl(); | |
633 | |
634 for (size_t i = 0; i < statements->dim; i++) | |
635 { Statement *s; | |
636 | |
637 s = (Statement *) statements->data[i]; | |
638 if (s) | |
639 s->toCBuffer(buf, hgs); | |
640 } | |
641 | |
642 buf->writeByte('}'); | |
643 buf->writenl(); | |
644 } | |
645 | |
646 int UnrolledLoopStatement::hasBreak() | |
647 { | |
648 return TRUE; | |
649 } | |
650 | |
651 int UnrolledLoopStatement::hasContinue() | |
652 { | |
653 return TRUE; | |
654 } | |
655 | |
656 int UnrolledLoopStatement::usesEH() | |
657 { | |
658 for (size_t i = 0; i < statements->dim; i++) | |
336 | 659 { Statement *s = (Statement *) statements->data[i]; |
159 | 660 if (s && s->usesEH()) |
661 return TRUE; | |
662 } | |
663 return FALSE; | |
664 } | |
665 | |
336 | 666 int UnrolledLoopStatement::blockExit() |
667 { | |
668 int result = BEfallthru; | |
669 for (size_t i = 0; i < statements->dim; i++) | |
670 { Statement *s = (Statement *) statements->data[i]; | |
671 if (s) | |
672 { | |
673 int r = s->blockExit(); | |
674 result |= r & ~(BEbreak | BEcontinue); | |
675 } | |
676 } | |
677 return result; | |
678 } | |
679 | |
159 | 680 int UnrolledLoopStatement::comeFrom() |
681 { int comefrom = FALSE; | |
682 | |
683 //printf("UnrolledLoopStatement::comeFrom()\n"); | |
684 for (size_t i = 0; i < statements->dim; i++) | |
685 { Statement *s = (Statement *)statements->data[i]; | |
686 | |
687 if (!s) | |
688 continue; | |
689 | |
690 comefrom |= s->comeFrom(); | |
691 } | |
692 return comefrom; | |
693 } | |
694 | |
695 | |
696 /******************************** ScopeStatement ***************************/ | |
697 | |
698 ScopeStatement::ScopeStatement(Loc loc, Statement *s) | |
699 : Statement(loc) | |
700 { | |
701 this->statement = s; | |
702 } | |
703 | |
704 Statement *ScopeStatement::syntaxCopy() | |
705 { | |
706 Statement *s; | |
707 | |
708 s = statement ? statement->syntaxCopy() : NULL; | |
709 s = new ScopeStatement(loc, s); | |
710 return s; | |
711 } | |
712 | |
713 | |
714 Statement *ScopeStatement::semantic(Scope *sc) | |
715 { ScopeDsymbol *sym; | |
716 | |
717 //printf("ScopeStatement::semantic(sc = %p)\n", sc); | |
718 if (statement) | |
719 { Statements *a; | |
720 | |
721 sym = new ScopeDsymbol(); | |
722 sym->parent = sc->scopesym; | |
723 sc = sc->push(sym); | |
724 | |
725 a = statement->flatten(sc); | |
726 if (a) | |
727 { | |
728 statement = new CompoundStatement(loc, a); | |
729 } | |
730 | |
731 statement = statement->semantic(sc); | |
732 if (statement) | |
733 { | |
734 Statement *sentry; | |
735 Statement *sexception; | |
736 Statement *sfinally; | |
737 | |
738 statement->scopeCode(&sentry, &sexception, &sfinally); | |
739 if (sfinally) | |
740 { | |
741 //printf("adding sfinally\n"); | |
742 statement = new CompoundStatement(loc, statement, sfinally); | |
743 } | |
744 } | |
745 | |
746 sc->pop(); | |
747 } | |
748 return this; | |
749 } | |
750 | |
751 int ScopeStatement::hasBreak() | |
752 { | |
753 //printf("ScopeStatement::hasBreak() %s\n", toChars()); | |
754 return statement ? statement->hasBreak() : FALSE; | |
755 } | |
756 | |
757 int ScopeStatement::hasContinue() | |
758 { | |
759 return statement ? statement->hasContinue() : FALSE; | |
760 } | |
761 | |
762 int ScopeStatement::usesEH() | |
763 { | |
764 return statement ? statement->usesEH() : FALSE; | |
765 } | |
766 | |
336 | 767 int ScopeStatement::blockExit() |
768 { | |
769 //printf("ScopeStatement::blockExit(%p)\n", statement); | |
770 return statement ? statement->blockExit() : BEfallthru; | |
771 } | |
772 | |
159 | 773 int ScopeStatement::comeFrom() |
774 { | |
775 //printf("ScopeStatement::comeFrom()\n"); | |
776 return statement ? statement->comeFrom() : FALSE; | |
777 } | |
778 | |
779 void ScopeStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
780 { | |
781 buf->writeByte('{'); | |
782 buf->writenl(); | |
783 | |
784 if (statement) | |
785 statement->toCBuffer(buf, hgs); | |
786 | |
787 buf->writeByte('}'); | |
788 buf->writenl(); | |
789 } | |
790 | |
791 /******************************** WhileStatement ***************************/ | |
792 | |
793 WhileStatement::WhileStatement(Loc loc, Expression *c, Statement *b) | |
794 : Statement(loc) | |
795 { | |
796 condition = c; | |
797 body = b; | |
309 | 798 enclosinghandler = NULL; |
159 | 799 } |
800 | |
801 Statement *WhileStatement::syntaxCopy() | |
802 { | |
803 WhileStatement *s = new WhileStatement(loc, condition->syntaxCopy(), body ? body->syntaxCopy() : NULL); | |
804 return s; | |
805 } | |
806 | |
807 | |
808 Statement *WhileStatement::semantic(Scope *sc) | |
809 { | |
810 #if 0 | |
811 if (condition->op == TOKmatch) | |
812 { | |
813 /* Rewrite while (condition) body as: | |
814 * if (condition) | |
815 * do | |
816 * body | |
817 * while ((_match = _match.opNext), _match); | |
818 */ | |
819 | |
820 Expression *ew = new IdentifierExp(0, Id::_match); | |
821 ew = new DotIdExp(0, ew, Id::next); | |
822 ew = new AssignExp(0, new IdentifierExp(0, Id::_match), ew); | |
823 ////ew = new EqualExp(TOKnotequal, 0, ew, new NullExp(0)); | |
824 Expression *ev = new IdentifierExp(0, Id::_match); | |
825 //ev = new CastExp(0, ev, Type::tvoidptr); | |
826 ew = new CommaExp(0, ew, ev); | |
827 Statement *sw = new DoStatement(loc, body, ew); | |
828 Statement *si = new IfStatement(loc, condition, sw, NULL); | |
829 return si->semantic(sc); | |
830 } | |
831 #endif | |
832 | |
309 | 833 enclosinghandler = sc->tfOfTry; |
159 | 834 |
835 condition = condition->semantic(sc); | |
836 condition = resolveProperties(sc, condition); | |
837 condition = condition->optimize(WANTvalue); | |
838 condition = condition->checkToBoolean(); | |
839 | |
840 sc->noctor++; | |
841 | |
842 Scope *scd = sc->push(); | |
843 scd->sbreak = this; | |
844 scd->scontinue = this; | |
845 if (body) | |
846 body = body->semantic(scd); | |
847 scd->pop(); | |
848 | |
849 sc->noctor--; | |
850 | |
851 return this; | |
852 } | |
853 | |
854 int WhileStatement::hasBreak() | |
855 { | |
856 return TRUE; | |
857 } | |
858 | |
859 int WhileStatement::hasContinue() | |
860 { | |
861 return TRUE; | |
862 } | |
863 | |
864 int WhileStatement::usesEH() | |
865 { | |
866 return body ? body->usesEH() : 0; | |
867 } | |
868 | |
336 | 869 int WhileStatement::blockExit() |
870 { | |
871 //printf("WhileStatement::blockExit(%p)\n", this); | |
872 | |
873 int result = BEnone; | |
874 if (condition->canThrow()) | |
875 result |= BEthrow; | |
876 if (condition->isBool(TRUE)) | |
877 { | |
878 if (body) | |
879 { result |= body->blockExit(); | |
880 if (result & BEbreak) | |
881 result |= BEfallthru; | |
882 } | |
883 } | |
884 else if (condition->isBool(FALSE)) | |
885 { | |
886 result |= BEfallthru; | |
887 } | |
888 else | |
889 { | |
890 if (body) | |
891 result |= body->blockExit(); | |
892 result |= BEfallthru; | |
893 } | |
894 result &= ~(BEbreak | BEcontinue); | |
895 return result; | |
896 } | |
897 | |
159 | 898 int WhileStatement::comeFrom() |
899 { | |
900 if (body) | |
901 return body->comeFrom(); | |
902 return FALSE; | |
903 } | |
904 | |
905 void WhileStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
906 { | |
907 buf->writestring("while ("); | |
908 condition->toCBuffer(buf, hgs); | |
909 buf->writebyte(')'); | |
910 buf->writenl(); | |
911 if (body) | |
912 body->toCBuffer(buf, hgs); | |
913 } | |
914 | |
915 /******************************** DoStatement ***************************/ | |
916 | |
917 DoStatement::DoStatement(Loc loc, Statement *b, Expression *c) | |
918 : Statement(loc) | |
919 { | |
920 body = b; | |
921 condition = c; | |
309 | 922 enclosinghandler = NULL; |
159 | 923 } |
924 | |
925 Statement *DoStatement::syntaxCopy() | |
926 { | |
927 DoStatement *s = new DoStatement(loc, body ? body->syntaxCopy() : NULL, condition->syntaxCopy()); | |
928 return s; | |
929 } | |
930 | |
931 | |
932 Statement *DoStatement::semantic(Scope *sc) | |
933 { | |
309 | 934 enclosinghandler = sc->tfOfTry; |
159 | 935 |
936 sc->noctor++; | |
937 if (body) | |
938 body = body->semanticScope(sc, this, this); | |
939 sc->noctor--; | |
940 condition = condition->semantic(sc); | |
941 condition = resolveProperties(sc, condition); | |
942 condition = condition->optimize(WANTvalue); | |
943 | |
944 condition = condition->checkToBoolean(); | |
945 | |
946 return this; | |
947 } | |
948 | |
949 int DoStatement::hasBreak() | |
950 { | |
951 return TRUE; | |
952 } | |
953 | |
954 int DoStatement::hasContinue() | |
955 { | |
956 return TRUE; | |
957 } | |
958 | |
959 int DoStatement::usesEH() | |
960 { | |
961 return body ? body->usesEH() : 0; | |
962 } | |
963 | |
336 | 964 int DoStatement::blockExit() |
965 { int result; | |
966 | |
967 if (body) | |
968 { result = body->blockExit(); | |
969 if (result == BEbreak) | |
970 return BEfallthru; | |
971 if (result & BEcontinue) | |
972 result |= BEfallthru; | |
973 } | |
974 else | |
975 result = BEfallthru; | |
976 if (result & BEfallthru) | |
977 { if (condition->canThrow()) | |
978 result |= BEthrow; | |
979 if (!(result & BEbreak) && condition->isBool(TRUE)) | |
980 result &= ~BEfallthru; | |
981 } | |
982 result &= ~(BEbreak | BEcontinue); | |
983 return result; | |
984 } | |
985 | |
159 | 986 int DoStatement::comeFrom() |
987 { | |
988 if (body) | |
989 return body->comeFrom(); | |
990 return FALSE; | |
991 } | |
992 | |
993 void DoStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
994 { | |
995 buf->writestring("do"); | |
996 buf->writenl(); | |
997 if (body) | |
998 body->toCBuffer(buf, hgs); | |
999 buf->writestring("while ("); | |
1000 condition->toCBuffer(buf, hgs); | |
1001 buf->writebyte(')'); | |
1002 } | |
1003 | |
1004 /******************************** ForStatement ***************************/ | |
1005 | |
1006 ForStatement::ForStatement(Loc loc, Statement *init, Expression *condition, Expression *increment, Statement *body) | |
1007 : Statement(loc) | |
1008 { | |
1009 this->init = init; | |
1010 this->condition = condition; | |
1011 this->increment = increment; | |
1012 this->body = body; | |
309 | 1013 this->enclosinghandler = NULL; |
159 | 1014 } |
1015 | |
1016 Statement *ForStatement::syntaxCopy() | |
1017 { | |
1018 Statement *i = NULL; | |
1019 if (init) | |
1020 i = init->syntaxCopy(); | |
1021 Expression *c = NULL; | |
1022 if (condition) | |
1023 c = condition->syntaxCopy(); | |
1024 Expression *inc = NULL; | |
1025 if (increment) | |
1026 inc = increment->syntaxCopy(); | |
1027 ForStatement *s = new ForStatement(loc, i, c, inc, body->syntaxCopy()); | |
1028 return s; | |
1029 } | |
1030 | |
1031 Statement *ForStatement::semantic(Scope *sc) | |
1032 { | |
309 | 1033 enclosinghandler = sc->tfOfTry; |
159 | 1034 |
1035 ScopeDsymbol *sym = new ScopeDsymbol(); | |
1036 sym->parent = sc->scopesym; | |
1037 sc = sc->push(sym); | |
1038 if (init) | |
1039 init = init->semantic(sc); | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
945
diff
changeset
|
1040 #if 0 |
159 | 1041 if (!condition) |
1042 // Use a default value | |
1043 condition = new IntegerExp(loc, 1, Type::tboolean); | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
945
diff
changeset
|
1044 #endif |
159 | 1045 sc->noctor++; |
846
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1046 if (condition) |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1047 { |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1048 condition = condition->semantic(sc); |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1049 condition = resolveProperties(sc, condition); |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1050 condition = condition->optimize(WANTvalue); |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1051 condition = condition->checkToBoolean(); |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1052 } |
159 | 1053 if (increment) |
846
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1054 { increment = increment->semantic(sc); |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1055 increment = resolveProperties(sc, increment); |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1056 } |
159 | 1057 |
1058 sc->sbreak = this; | |
1059 sc->scontinue = this; | |
1060 body = body->semantic(sc); | |
1061 sc->noctor--; | |
1062 | |
1063 sc->pop(); | |
1064 return this; | |
1065 } | |
1066 | |
1067 void ForStatement::scopeCode(Statement **sentry, Statement **sexception, Statement **sfinally) | |
1068 { | |
1069 //printf("ForStatement::scopeCode()\n"); | |
1070 //print(); | |
1071 if (init) | |
1072 init->scopeCode(sentry, sexception, sfinally); | |
1073 else | |
1074 Statement::scopeCode(sentry, sexception, sfinally); | |
1075 } | |
1076 | |
1077 int ForStatement::hasBreak() | |
1078 { | |
1079 //printf("ForStatement::hasBreak()\n"); | |
1080 return TRUE; | |
1081 } | |
1082 | |
1083 int ForStatement::hasContinue() | |
1084 { | |
1085 return TRUE; | |
1086 } | |
1087 | |
1088 int ForStatement::usesEH() | |
1089 { | |
1090 return (init && init->usesEH()) || body->usesEH(); | |
1091 } | |
1092 | |
336 | 1093 int ForStatement::blockExit() |
1094 { int result = BEfallthru; | |
1095 | |
1096 if (init) | |
1097 { result = init->blockExit(); | |
1098 if (!(result & BEfallthru)) | |
1099 return result; | |
1100 } | |
1101 if (condition) | |
1102 { if (condition->canThrow()) | |
1103 result |= BEthrow; | |
1104 } | |
1105 else | |
1106 result &= ~BEfallthru; // the body must do the exiting | |
1107 if (body) | |
1108 { int r = body->blockExit(); | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
945
diff
changeset
|
1109 if (r & (BEbreak | BEgoto)) |
336 | 1110 result |= BEfallthru; |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
945
diff
changeset
|
1111 result |= r & ~(BEfallthru | BEbreak | BEcontinue); |
336 | 1112 } |
1113 if (increment && increment->canThrow()) | |
1114 result |= BEthrow; | |
1115 return result; | |
1116 } | |
1117 | |
159 | 1118 int ForStatement::comeFrom() |
1119 { | |
1120 //printf("ForStatement::comeFrom()\n"); | |
1121 if (body) | |
1122 { int result = body->comeFrom(); | |
1123 //printf("result = %d\n", result); | |
1124 return result; | |
1125 } | |
1126 return FALSE; | |
1127 } | |
1128 | |
1129 void ForStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
1130 { | |
1131 buf->writestring("for ("); | |
1132 if (init) | |
1133 { | |
1134 hgs->FLinit.init++; | |
1135 hgs->FLinit.decl = 0; | |
1136 init->toCBuffer(buf, hgs); | |
1137 if (hgs->FLinit.decl > 0) | |
1138 buf->writebyte(';'); | |
1139 hgs->FLinit.decl = 0; | |
1140 hgs->FLinit.init--; | |
1141 } | |
1142 else | |
1143 buf->writebyte(';'); | |
1144 if (condition) | |
1145 { buf->writebyte(' '); | |
1146 condition->toCBuffer(buf, hgs); | |
1147 } | |
1148 buf->writebyte(';'); | |
1149 if (increment) | |
1150 { buf->writebyte(' '); | |
1151 increment->toCBuffer(buf, hgs); | |
1152 } | |
1153 buf->writebyte(')'); | |
1154 buf->writenl(); | |
1155 buf->writebyte('{'); | |
1156 buf->writenl(); | |
1157 body->toCBuffer(buf, hgs); | |
1158 buf->writebyte('}'); | |
1159 buf->writenl(); | |
1160 } | |
1161 | |
1162 /******************************** ForeachStatement ***************************/ | |
1163 | |
1164 ForeachStatement::ForeachStatement(Loc loc, enum TOK op, Arguments *arguments, | |
1165 Expression *aggr, Statement *body) | |
1166 : Statement(loc) | |
1167 { | |
1168 this->op = op; | |
1169 this->arguments = arguments; | |
1170 this->aggr = aggr; | |
1171 this->body = body; | |
309 | 1172 this->enclosinghandler = NULL; |
159 | 1173 |
1174 this->key = NULL; | |
1175 this->value = NULL; | |
1176 | |
1177 this->func = NULL; | |
1178 } | |
1179 | |
1180 Statement *ForeachStatement::syntaxCopy() | |
1181 { | |
1182 Arguments *args = Argument::arraySyntaxCopy(arguments); | |
1183 Expression *exp = aggr->syntaxCopy(); | |
1184 ForeachStatement *s = new ForeachStatement(loc, op, args, exp, | |
1185 body ? body->syntaxCopy() : NULL); | |
1186 return s; | |
1187 } | |
1188 | |
1189 Statement *ForeachStatement::semantic(Scope *sc) | |
1190 { | |
1191 //printf("ForeachStatement::semantic() %p\n", this); | |
1192 ScopeDsymbol *sym; | |
1193 Statement *s = this; | |
846
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1194 size_t dim = arguments->dim; |
159 | 1195 TypeAArray *taa = NULL; |
1196 | |
1197 Type *tn = NULL; | |
1198 Type *tnv = NULL; | |
1199 | |
309 | 1200 enclosinghandler = sc->tfOfTry; |
159 | 1201 |
1202 func = sc->func; | |
1203 if (func->fes) | |
1204 func = func->fes->func; | |
1205 | |
1206 aggr = aggr->semantic(sc); | |
1207 aggr = resolveProperties(sc, aggr); | |
846
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1208 aggr = aggr->optimize(WANTvalue); |
159 | 1209 if (!aggr->type) |
1210 { | |
1211 error("invalid foreach aggregate %s", aggr->toChars()); | |
1212 return this; | |
1213 } | |
1214 | |
1215 inferApplyArgTypes(op, arguments, aggr); | |
1216 | |
1217 /* Check for inference errors | |
1218 */ | |
1219 if (dim != arguments->dim) | |
1220 { | |
1221 //printf("dim = %d, arguments->dim = %d\n", dim, arguments->dim); | |
1222 error("cannot uniquely infer foreach argument types"); | |
1223 return this; | |
1224 } | |
1225 | |
1226 Type *tab = aggr->type->toBasetype(); | |
1227 | |
1228 if (tab->ty == Ttuple) // don't generate new scope for tuple loops | |
1229 { | |
1230 if (dim < 1 || dim > 2) | |
1231 { | |
1232 error("only one (value) or two (key,value) arguments for tuple foreach"); | |
1233 return s; | |
1234 } | |
1235 | |
1236 TypeTuple *tuple = (TypeTuple *)tab; | |
1237 Statements *statements = new Statements(); | |
1238 //printf("aggr: op = %d, %s\n", aggr->op, aggr->toChars()); | |
1239 size_t n; | |
1240 TupleExp *te = NULL; | |
1241 if (aggr->op == TOKtuple) // expression tuple | |
1242 { te = (TupleExp *)aggr; | |
1243 n = te->exps->dim; | |
1244 } | |
1245 else if (aggr->op == TOKtype) // type tuple | |
1246 { | |
1247 n = Argument::dim(tuple->arguments); | |
1248 } | |
1249 else | |
1250 assert(0); | |
1251 for (size_t j = 0; j < n; j++) | |
1252 { size_t k = (op == TOKforeach) ? j : n - 1 - j; | |
1253 Expression *e; | |
1254 Type *t; | |
1255 if (te) | |
1256 e = (Expression *)te->exps->data[k]; | |
1257 else | |
1258 t = Argument::getNth(tuple->arguments, k)->type; | |
1259 Argument *arg = (Argument *)arguments->data[0]; | |
1260 Statements *st = new Statements(); | |
1261 | |
1262 if (dim == 2) | |
1263 { // Declare key | |
1264 if (arg->storageClass & (STCout | STCref | STClazy)) | |
1265 error("no storage class for key %s", arg->ident->toChars()); | |
1266 TY keyty = arg->type->ty; | |
275
665b81613475
[svn r296] Removed: the 'suite' dir, it never took off!
lindquist
parents:
262
diff
changeset
|
1267 if (global.params.is64bit) |
665b81613475
[svn r296] Removed: the 'suite' dir, it never took off!
lindquist
parents:
262
diff
changeset
|
1268 { |
665b81613475
[svn r296] Removed: the 'suite' dir, it never took off!
lindquist
parents:
262
diff
changeset
|
1269 if (keyty != Tint32 && keyty != Tuns32 && keyty != Tint64 && keyty != Tuns64) |
665b81613475
[svn r296] Removed: the 'suite' dir, it never took off!
lindquist
parents:
262
diff
changeset
|
1270 { |
665b81613475
[svn r296] Removed: the 'suite' dir, it never took off!
lindquist
parents:
262
diff
changeset
|
1271 error("foreach: key type must be int, uint, long or ulong, not %s", key->type->toChars()); |
665b81613475
[svn r296] Removed: the 'suite' dir, it never took off!
lindquist
parents:
262
diff
changeset
|
1272 } |
665b81613475
[svn r296] Removed: the 'suite' dir, it never took off!
lindquist
parents:
262
diff
changeset
|
1273 } |
665b81613475
[svn r296] Removed: the 'suite' dir, it never took off!
lindquist
parents:
262
diff
changeset
|
1274 else if (keyty != Tint32 && keyty != Tuns32) |
665b81613475
[svn r296] Removed: the 'suite' dir, it never took off!
lindquist
parents:
262
diff
changeset
|
1275 { |
665b81613475
[svn r296] Removed: the 'suite' dir, it never took off!
lindquist
parents:
262
diff
changeset
|
1276 error("foreach: key type must be int or uint, not %s", key->type->toChars()); |
665b81613475
[svn r296] Removed: the 'suite' dir, it never took off!
lindquist
parents:
262
diff
changeset
|
1277 } |
159 | 1278 Initializer *ie = new ExpInitializer(0, new IntegerExp(k)); |
1279 VarDeclaration *var = new VarDeclaration(loc, arg->type, arg->ident, ie); | |
1280 var->storage_class |= STCconst; | |
1281 DeclarationExp *de = new DeclarationExp(loc, var); | |
1282 st->push(new ExpStatement(loc, de)); | |
1283 arg = (Argument *)arguments->data[1]; // value | |
1284 } | |
1285 // Declare value | |
1286 if (arg->storageClass & (STCout | STCref | STClazy)) | |
1287 error("no storage class for value %s", arg->ident->toChars()); | |
1288 Dsymbol *var; | |
1289 if (te) | |
1290 { | |
1291 if (e->type->toBasetype()->ty == Tfunction && | |
1292 e->op == TOKvar) | |
1293 { VarExp *ve = (VarExp *)e; | |
1294 var = new AliasDeclaration(loc, arg->ident, ve->var); | |
1295 } | |
1296 else | |
1297 { | |
1298 arg->type = e->type; | |
1299 Initializer *ie = new ExpInitializer(0, e); | |
1300 VarDeclaration *v = new VarDeclaration(loc, arg->type, arg->ident, ie); | |
1301 if (e->isConst()) | |
1302 v->storage_class |= STCconst; | |
1303 var = v; | |
1304 } | |
1305 } | |
1306 else | |
1307 { | |
1308 var = new AliasDeclaration(loc, arg->ident, t); | |
1309 } | |
1310 DeclarationExp *de = new DeclarationExp(loc, var); | |
1311 st->push(new ExpStatement(loc, de)); | |
1312 | |
1313 st->push(body->syntaxCopy()); | |
1314 s = new CompoundStatement(loc, st); | |
1315 s = new ScopeStatement(loc, s); | |
1316 statements->push(s); | |
1317 } | |
1318 | |
1319 s = new UnrolledLoopStatement(loc, statements); | |
1320 s = s->semantic(sc); | |
1321 return s; | |
1322 } | |
1323 | |
846
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1324 for (size_t i = 0; i < dim; i++) |
159 | 1325 { Argument *arg = (Argument *)arguments->data[i]; |
1326 if (!arg->type) | |
1327 { | |
1328 error("cannot infer type for %s", arg->ident->toChars()); | |
1329 return this; | |
1330 } | |
1331 } | |
1332 | |
1333 sym = new ScopeDsymbol(); | |
1334 sym->parent = sc->scopesym; | |
1335 sc = sc->push(sym); | |
1336 | |
1337 sc->noctor++; | |
1338 | |
1339 switch (tab->ty) | |
1340 { | |
1341 case Tarray: | |
1342 case Tsarray: | |
1343 if (dim < 1 || dim > 2) | |
1344 { | |
1345 error("only one or two arguments for array foreach"); | |
1346 break; | |
1347 } | |
1348 | |
1349 /* Look for special case of parsing char types out of char type | |
1350 * array. | |
1351 */ | |
1352 tn = tab->nextOf()->toBasetype(); | |
1353 if (tn->ty == Tchar || tn->ty == Twchar || tn->ty == Tdchar) | |
1354 { Argument *arg; | |
1355 | |
846
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1356 int i = (dim == 1) ? 0 : 1; // index of value |
159 | 1357 arg = (Argument *)arguments->data[i]; |
1358 arg->type = arg->type->semantic(loc, sc); | |
1359 tnv = arg->type->toBasetype(); | |
1360 if (tnv->ty != tn->ty && | |
1361 (tnv->ty == Tchar || tnv->ty == Twchar || tnv->ty == Tdchar)) | |
1362 { | |
1363 if (arg->storageClass & STCref) | |
1364 error("foreach: value of UTF conversion cannot be ref"); | |
1365 if (dim == 2) | |
1366 { arg = (Argument *)arguments->data[0]; | |
1367 if (arg->storageClass & STCref) | |
1368 error("foreach: key cannot be ref"); | |
1369 } | |
1370 goto Lapply; | |
1371 } | |
1372 } | |
1373 | |
846
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1374 for (size_t i = 0; i < dim; i++) |
159 | 1375 { // Declare args |
1376 Argument *arg = (Argument *)arguments->data[i]; | |
1377 VarDeclaration *var; | |
1378 | |
1379 var = new VarDeclaration(loc, arg->type, arg->ident, NULL); | |
1380 var->storage_class |= STCforeach; | |
1381 var->storage_class |= arg->storageClass & (STCin | STCout | STCref); | |
1382 #if 1 | |
1383 DeclarationExp *de = new DeclarationExp(loc, var); | |
1384 de->semantic(sc); | |
1385 #else | |
1386 var->semantic(sc); | |
1387 if (!sc->insert(var)) | |
1388 error("%s already defined", var->ident->toChars()); | |
1389 #endif | |
1390 if (dim == 2 && i == 0) | |
1391 key = var; | |
1392 else | |
1393 value = var; | |
1394 } | |
1395 | |
1396 sc->sbreak = this; | |
1397 sc->scontinue = this; | |
1398 body = body->semantic(sc); | |
1399 | |
1400 if (!value->type->equals(tab->next)) | |
1401 { | |
1402 if (aggr->op == TOKstring) | |
1403 aggr = aggr->implicitCastTo(sc, value->type->arrayOf()); | |
1404 else | |
846
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1405 error("foreach: %s is not an array of %s", |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1406 tab->toChars(), value->type->toChars()); |
159 | 1407 } |
1408 | |
275
665b81613475
[svn r296] Removed: the 'suite' dir, it never took off!
lindquist
parents:
262
diff
changeset
|
1409 if (key) |
665b81613475
[svn r296] Removed: the 'suite' dir, it never took off!
lindquist
parents:
262
diff
changeset
|
1410 { |
665b81613475
[svn r296] Removed: the 'suite' dir, it never took off!
lindquist
parents:
262
diff
changeset
|
1411 if (global.params.is64bit) |
665b81613475
[svn r296] Removed: the 'suite' dir, it never took off!
lindquist
parents:
262
diff
changeset
|
1412 { |
665b81613475
[svn r296] Removed: the 'suite' dir, it never took off!
lindquist
parents:
262
diff
changeset
|
1413 if (key->type->ty != Tint32 && key->type->ty != Tuns32 && key->type->ty != Tint64 && key->type->ty != Tuns64) |
665b81613475
[svn r296] Removed: the 'suite' dir, it never took off!
lindquist
parents:
262
diff
changeset
|
1414 { |
665b81613475
[svn r296] Removed: the 'suite' dir, it never took off!
lindquist
parents:
262
diff
changeset
|
1415 error("foreach: key type must be int, uint, long or ulong, not %s", key->type->toChars()); |
665b81613475
[svn r296] Removed: the 'suite' dir, it never took off!
lindquist
parents:
262
diff
changeset
|
1416 } |
665b81613475
[svn r296] Removed: the 'suite' dir, it never took off!
lindquist
parents:
262
diff
changeset
|
1417 } |
665b81613475
[svn r296] Removed: the 'suite' dir, it never took off!
lindquist
parents:
262
diff
changeset
|
1418 else if (key->type->ty != Tint32 && key->type->ty != Tuns32) |
665b81613475
[svn r296] Removed: the 'suite' dir, it never took off!
lindquist
parents:
262
diff
changeset
|
1419 { |
665b81613475
[svn r296] Removed: the 'suite' dir, it never took off!
lindquist
parents:
262
diff
changeset
|
1420 error("foreach: key type must be int or uint, not %s", key->type->toChars()); |
665b81613475
[svn r296] Removed: the 'suite' dir, it never took off!
lindquist
parents:
262
diff
changeset
|
1421 } |
665b81613475
[svn r296] Removed: the 'suite' dir, it never took off!
lindquist
parents:
262
diff
changeset
|
1422 } |
159 | 1423 |
1424 if (key && key->storage_class & (STCout | STCref)) | |
1425 error("foreach: key cannot be out or ref"); | |
1426 break; | |
1427 | |
1428 case Taarray: | |
1429 taa = (TypeAArray *)tab; | |
1430 if (dim < 1 || dim > 2) | |
1431 { | |
1432 error("only one or two arguments for associative array foreach"); | |
1433 break; | |
1434 } | |
1435 if (op == TOKforeach_reverse) | |
1436 { | |
1437 error("no reverse iteration on associative arrays"); | |
1438 } | |
1439 goto Lapply; | |
1440 | |
1441 case Tclass: | |
1442 case Tstruct: | |
846
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1443 #if DMDV2 |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1444 { /* Look for range iteration, i.e. the properties |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1445 * .empty, .next, .retreat, .head and .rear |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1446 * foreach (e; range) { ... } |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1447 * translates to: |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1448 * for (auto __r = range; !__r.empty; __r.next) |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1449 * { auto e = __r.head; |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1450 * ... |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1451 * } |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1452 */ |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1453 if (dim != 1) // only one argument allowed with ranges |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1454 goto Lapply; |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1455 AggregateDeclaration *ad = (tab->ty == Tclass) |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1456 ? (AggregateDeclaration *)((TypeClass *)tab)->sym |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1457 : (AggregateDeclaration *)((TypeStruct *)tab)->sym; |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1458 Identifier *idhead; |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1459 Identifier *idnext; |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1460 if (op == TOKforeach) |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1461 { idhead = Id::Fhead; |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1462 idnext = Id::Fnext; |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1463 } |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1464 else |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1465 { idhead = Id::Frear; |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1466 idnext = Id::Fretreat; |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1467 } |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1468 Dsymbol *shead = search_function(ad, idhead); |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1469 if (!shead) |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1470 goto Lapply; |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1471 |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1472 /* Generate a temporary __r and initialize it with the aggregate. |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1473 */ |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1474 Identifier *id = Identifier::generateId("__r"); |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1475 VarDeclaration *r = new VarDeclaration(loc, NULL, id, new ExpInitializer(loc, aggr)); |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1476 r->semantic(sc); |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1477 Statement *init = new DeclarationStatement(loc, r); |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1478 |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1479 // !__r.empty |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1480 Expression *e = new VarExp(loc, r); |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1481 e = new DotIdExp(loc, e, Id::Fempty); |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1482 Expression *condition = new NotExp(loc, e); |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1483 |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1484 // __r.next |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1485 e = new VarExp(loc, r); |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1486 Expression *increment = new DotIdExp(loc, e, idnext); |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1487 |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1488 /* Declaration statement for e: |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1489 * auto e = __r.idhead; |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1490 */ |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1491 e = new VarExp(loc, r); |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1492 Expression *einit = new DotIdExp(loc, e, idhead); |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1493 einit = einit->semantic(sc); |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1494 Argument *arg = (Argument *)arguments->data[0]; |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1495 VarDeclaration *ve = new VarDeclaration(loc, arg->type, arg->ident, new ExpInitializer(loc, einit)); |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1496 ve->storage_class |= STCforeach; |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1497 ve->storage_class |= arg->storageClass & (STCin | STCout | STCref | STCconst | STCinvariant); |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1498 |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1499 DeclarationExp *de = new DeclarationExp(loc, ve); |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1500 |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1501 Statement *body = new CompoundStatement(loc, |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1502 new DeclarationStatement(loc, de), this->body); |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1503 |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1504 s = new ForStatement(loc, init, condition, increment, body); |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1505 s = s->semantic(sc); |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1506 break; |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1507 } |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1508 #endif |
159 | 1509 case Tdelegate: |
1510 Lapply: | |
1511 { FuncDeclaration *fdapply; | |
1512 Arguments *args; | |
1513 Expression *ec; | |
1514 Expression *e; | |
1515 FuncLiteralDeclaration *fld; | |
1516 Argument *a; | |
1517 Type *t; | |
1518 Expression *flde; | |
1519 Identifier *id; | |
1520 Type *tret; | |
719
7261ff0f95ff
Implemented first class delegates. closes #101
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
715
diff
changeset
|
1521 TypeDelegate* dgty; |
7261ff0f95ff
Implemented first class delegates. closes #101
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
715
diff
changeset
|
1522 TypeDelegate* dgty2; |
7261ff0f95ff
Implemented first class delegates. closes #101
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
715
diff
changeset
|
1523 TypeDelegate* fldeTy; |
159 | 1524 |
1525 tret = func->type->nextOf(); | |
1526 | |
1527 // Need a variable to hold value from any return statements in body. | |
1528 if (!sc->func->vresult && tret && tret != Type::tvoid) | |
1529 { VarDeclaration *v; | |
1530 | |
1531 v = new VarDeclaration(loc, tret, Id::result, NULL); | |
1532 v->noauto = 1; | |
1533 v->semantic(sc); | |
1534 if (!sc->insert(v)) | |
1535 assert(0); | |
1536 v->parent = sc->func; | |
1537 sc->func->vresult = v; | |
1538 } | |
1539 | |
1540 /* Turn body into the function literal: | |
1541 * int delegate(ref T arg) { body } | |
1542 */ | |
1543 args = new Arguments(); | |
846
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1544 for (size_t i = 0; i < dim; i++) |
159 | 1545 { Argument *arg = (Argument *)arguments->data[i]; |
1546 | |
1547 arg->type = arg->type->semantic(loc, sc); | |
1548 if (arg->storageClass & STCref) | |
1549 id = arg->ident; | |
1550 else | |
1551 { // Make a copy of the ref argument so it isn't | |
1552 // a reference. | |
1553 VarDeclaration *v; | |
1554 Initializer *ie; | |
846
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1555 |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1556 id = Lexer::uniqueId("__applyArg", i); |
159 | 1557 |
1558 ie = new ExpInitializer(0, new IdentifierExp(0, id)); | |
1559 v = new VarDeclaration(0, arg->type, arg->ident, ie); | |
1560 s = new DeclarationStatement(0, v); | |
1561 body = new CompoundStatement(loc, s, body); | |
1562 } | |
1563 a = new Argument(STCref, arg->type, id, NULL); | |
1564 args->push(a); | |
1565 } | |
1566 t = new TypeFunction(args, Type::tint32, 0, LINKd); | |
1567 fld = new FuncLiteralDeclaration(loc, 0, t, TOKdelegate, this); | |
1568 fld->fbody = body; | |
1569 flde = new FuncExp(loc, fld); | |
1570 flde = flde->semantic(sc); | |
1571 | |
1572 // Resolve any forward referenced goto's | |
1573 for (int i = 0; i < gotos.dim; i++) | |
1574 { CompoundStatement *cs = (CompoundStatement *)gotos.data[i]; | |
1575 GotoStatement *gs = (GotoStatement *)cs->statements->data[0]; | |
1576 | |
1577 if (!gs->label->statement) | |
1578 { // 'Promote' it to this scope, and replace with a return | |
1579 cases.push(gs); | |
1580 s = new ReturnStatement(0, new IntegerExp(cases.dim + 1)); | |
1581 cs->statements->data[0] = (void *)s; | |
1582 } | |
1583 } | |
1584 | |
1585 if (tab->ty == Taarray) | |
1586 { | |
1587 // Check types | |
1588 Argument *arg = (Argument *)arguments->data[0]; | |
1589 if (dim == 2) | |
1590 { | |
1591 if (arg->storageClass & STCref) | |
1592 error("foreach: index cannot be ref"); | |
1593 if (!arg->type->equals(taa->index)) | |
1594 error("foreach: index must be type %s, not %s", taa->index->toChars(), arg->type->toChars()); | |
1595 arg = (Argument *)arguments->data[1]; | |
1596 } | |
1597 if (!arg->type->equals(taa->nextOf())) | |
1598 error("foreach: value must be type %s, not %s", taa->nextOf()->toChars(), arg->type->toChars()); | |
1599 | |
1600 /* Call: | |
1601 * _aaApply(aggr, keysize, flde) | |
1602 */ | |
664
eef8ac26c66c
Some missed LLVMDC -> LDC.
Christian Kamm <kamm incasoftware de>
parents:
510
diff
changeset
|
1603 //LDC: Build arguments. |
389
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
380
diff
changeset
|
1604 static FuncDeclaration *aaApply2_fd = NULL; |
719
7261ff0f95ff
Implemented first class delegates. closes #101
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
715
diff
changeset
|
1605 static TypeDelegate* aaApply2_dg; |
389
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
380
diff
changeset
|
1606 if(!aaApply2_fd) { |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
380
diff
changeset
|
1607 Arguments* args = new Arguments; |
715
30b42a283c8e
Removed TypeOpaque from DMD.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
664
diff
changeset
|
1608 args->push(new Argument(STCin, Type::tvoid->pointerTo(), NULL, NULL)); |
389
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
380
diff
changeset
|
1609 args->push(new Argument(STCin, Type::tsize_t, NULL, NULL)); |
378
d8234836b40f
Get rid of runTimeHack and instead add proper argument info to the frontend
Christian Kamm <kamm incasoftware de>
parents:
353
diff
changeset
|
1610 Arguments* dgargs = new Arguments; |
d8234836b40f
Get rid of runTimeHack and instead add proper argument info to the frontend
Christian Kamm <kamm incasoftware de>
parents:
353
diff
changeset
|
1611 dgargs->push(new Argument(STCin, Type::tvoidptr, NULL, NULL)); |
d8234836b40f
Get rid of runTimeHack and instead add proper argument info to the frontend
Christian Kamm <kamm incasoftware de>
parents:
353
diff
changeset
|
1612 dgargs->push(new Argument(STCin, Type::tvoidptr, NULL, NULL)); |
719
7261ff0f95ff
Implemented first class delegates. closes #101
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
715
diff
changeset
|
1613 aaApply2_dg = new TypeDelegate(new TypeFunction(dgargs, Type::tindex, 0, LINKd)); |
7261ff0f95ff
Implemented first class delegates. closes #101
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
715
diff
changeset
|
1614 args->push(new Argument(STCin, aaApply2_dg, NULL, NULL)); |
389
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
380
diff
changeset
|
1615 aaApply2_fd = FuncDeclaration::genCfunc(args, Type::tindex, "_aaApply2"); |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
380
diff
changeset
|
1616 } |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
380
diff
changeset
|
1617 static FuncDeclaration *aaApply_fd = NULL; |
719
7261ff0f95ff
Implemented first class delegates. closes #101
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
715
diff
changeset
|
1618 static TypeDelegate* aaApply_dg; |
389
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
380
diff
changeset
|
1619 if(!aaApply_fd) { |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
380
diff
changeset
|
1620 Arguments* args = new Arguments; |
715
30b42a283c8e
Removed TypeOpaque from DMD.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
664
diff
changeset
|
1621 args->push(new Argument(STCin, Type::tvoid->pointerTo(), NULL, NULL)); |
389
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
380
diff
changeset
|
1622 args->push(new Argument(STCin, Type::tsize_t, NULL, NULL)); |
378
d8234836b40f
Get rid of runTimeHack and instead add proper argument info to the frontend
Christian Kamm <kamm incasoftware de>
parents:
353
diff
changeset
|
1623 Arguments* dgargs = new Arguments; |
d8234836b40f
Get rid of runTimeHack and instead add proper argument info to the frontend
Christian Kamm <kamm incasoftware de>
parents:
353
diff
changeset
|
1624 dgargs->push(new Argument(STCin, Type::tvoidptr, NULL, NULL)); |
719
7261ff0f95ff
Implemented first class delegates. closes #101
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
715
diff
changeset
|
1625 aaApply_dg = new TypeDelegate(new TypeFunction(dgargs, Type::tindex, 0, LINKd)); |
7261ff0f95ff
Implemented first class delegates. closes #101
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
715
diff
changeset
|
1626 args->push(new Argument(STCin, aaApply_dg, NULL, NULL)); |
389
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
380
diff
changeset
|
1627 aaApply_fd = FuncDeclaration::genCfunc(args, Type::tindex, "_aaApply"); |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
380
diff
changeset
|
1628 } |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
380
diff
changeset
|
1629 if (dim == 2) { |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
380
diff
changeset
|
1630 fdapply = aaApply2_fd; |
719
7261ff0f95ff
Implemented first class delegates. closes #101
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
715
diff
changeset
|
1631 fldeTy = aaApply2_dg; |
389
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
380
diff
changeset
|
1632 } else { |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
380
diff
changeset
|
1633 fdapply = aaApply_fd; |
719
7261ff0f95ff
Implemented first class delegates. closes #101
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
715
diff
changeset
|
1634 fldeTy = aaApply_dg; |
378
d8234836b40f
Get rid of runTimeHack and instead add proper argument info to the frontend
Christian Kamm <kamm incasoftware de>
parents:
353
diff
changeset
|
1635 } |
159 | 1636 ec = new VarExp(0, fdapply); |
1637 Expressions *exps = new Expressions(); | |
1638 exps->push(aggr); | |
1639 size_t keysize = taa->key->size(); | |
765
f08e0ff8d28c
Fixed 32bit assumption in statement.c when calling aaApply(2).
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
719
diff
changeset
|
1640 keysize = (keysize + (PTRSIZE-1)) & ~(PTRSIZE-1); |
159 | 1641 exps->push(new IntegerExp(0, keysize, Type::tsize_t)); |
719
7261ff0f95ff
Implemented first class delegates. closes #101
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
715
diff
changeset
|
1642 |
7261ff0f95ff
Implemented first class delegates. closes #101
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
715
diff
changeset
|
1643 // LDC paint delegate argument to the type runtime expects |
7261ff0f95ff
Implemented first class delegates. closes #101
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
715
diff
changeset
|
1644 if (!fldeTy->equals(flde->type)) |
7261ff0f95ff
Implemented first class delegates. closes #101
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
715
diff
changeset
|
1645 { |
7261ff0f95ff
Implemented first class delegates. closes #101
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
715
diff
changeset
|
1646 flde = new CastExp(loc, flde, flde->type); |
7261ff0f95ff
Implemented first class delegates. closes #101
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
715
diff
changeset
|
1647 flde->type = fldeTy; |
7261ff0f95ff
Implemented first class delegates. closes #101
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
715
diff
changeset
|
1648 } |
159 | 1649 exps->push(flde); |
719
7261ff0f95ff
Implemented first class delegates. closes #101
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
715
diff
changeset
|
1650 |
159 | 1651 e = new CallExp(loc, ec, exps); |
1652 e->type = Type::tindex; // don't run semantic() on e | |
1653 } | |
1654 else if (tab->ty == Tarray || tab->ty == Tsarray) | |
1655 { | |
1656 /* Call: | |
1657 * _aApply(aggr, flde) | |
1658 */ | |
1659 static char fntab[9][3] = | |
1660 { "cc","cw","cd", | |
1661 "wc","cc","wd", | |
1662 "dc","dw","dd" | |
1663 }; | |
1664 char fdname[7+1+2+ sizeof(dim)*3 + 1]; | |
1665 int flag; | |
1666 | |
1667 switch (tn->ty) | |
1668 { | |
1669 case Tchar: flag = 0; break; | |
1670 case Twchar: flag = 3; break; | |
1671 case Tdchar: flag = 6; break; | |
1672 default: assert(0); | |
1673 } | |
1674 switch (tnv->ty) | |
1675 { | |
1676 case Tchar: flag += 0; break; | |
1677 case Twchar: flag += 1; break; | |
1678 case Tdchar: flag += 2; break; | |
1679 default: assert(0); | |
1680 } | |
1681 const char *r = (op == TOKforeach_reverse) ? "R" : ""; | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
945
diff
changeset
|
1682 int j = sprintf(fdname, "_aApply%s%.*s%zu", r, 2, fntab[flag], dim); |
159 | 1683 assert(j < sizeof(fdname)); |
664
eef8ac26c66c
Some missed LLVMDC -> LDC.
Christian Kamm <kamm incasoftware de>
parents:
510
diff
changeset
|
1684 //LDC: Build arguments. |
378
d8234836b40f
Get rid of runTimeHack and instead add proper argument info to the frontend
Christian Kamm <kamm incasoftware de>
parents:
353
diff
changeset
|
1685 Arguments* args = new Arguments; |
715
30b42a283c8e
Removed TypeOpaque from DMD.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
664
diff
changeset
|
1686 args->push(new Argument(STCin, tn->arrayOf(), NULL, NULL)); |
378
d8234836b40f
Get rid of runTimeHack and instead add proper argument info to the frontend
Christian Kamm <kamm incasoftware de>
parents:
353
diff
changeset
|
1687 if (dim == 2) { |
d8234836b40f
Get rid of runTimeHack and instead add proper argument info to the frontend
Christian Kamm <kamm incasoftware de>
parents:
353
diff
changeset
|
1688 Arguments* dgargs = new Arguments; |
d8234836b40f
Get rid of runTimeHack and instead add proper argument info to the frontend
Christian Kamm <kamm incasoftware de>
parents:
353
diff
changeset
|
1689 dgargs->push(new Argument(STCin, Type::tvoidptr, NULL, NULL)); |
d8234836b40f
Get rid of runTimeHack and instead add proper argument info to the frontend
Christian Kamm <kamm incasoftware de>
parents:
353
diff
changeset
|
1690 dgargs->push(new Argument(STCin, Type::tvoidptr, NULL, NULL)); |
719
7261ff0f95ff
Implemented first class delegates. closes #101
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
715
diff
changeset
|
1691 dgty = new TypeDelegate(new TypeFunction(dgargs, Type::tindex, 0, LINKd)); |
378
d8234836b40f
Get rid of runTimeHack and instead add proper argument info to the frontend
Christian Kamm <kamm incasoftware de>
parents:
353
diff
changeset
|
1692 args->push(new Argument(STCin, dgty, NULL, NULL)); |
d8234836b40f
Get rid of runTimeHack and instead add proper argument info to the frontend
Christian Kamm <kamm incasoftware de>
parents:
353
diff
changeset
|
1693 fdapply = FuncDeclaration::genCfunc(args, Type::tindex, fdname); |
d8234836b40f
Get rid of runTimeHack and instead add proper argument info to the frontend
Christian Kamm <kamm incasoftware de>
parents:
353
diff
changeset
|
1694 } else { |
d8234836b40f
Get rid of runTimeHack and instead add proper argument info to the frontend
Christian Kamm <kamm incasoftware de>
parents:
353
diff
changeset
|
1695 Arguments* dgargs = new Arguments; |
d8234836b40f
Get rid of runTimeHack and instead add proper argument info to the frontend
Christian Kamm <kamm incasoftware de>
parents:
353
diff
changeset
|
1696 dgargs->push(new Argument(STCin, Type::tvoidptr, NULL, NULL)); |
719
7261ff0f95ff
Implemented first class delegates. closes #101
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
715
diff
changeset
|
1697 dgty = new TypeDelegate(new TypeFunction(dgargs, Type::tindex, 0, LINKd)); |
378
d8234836b40f
Get rid of runTimeHack and instead add proper argument info to the frontend
Christian Kamm <kamm incasoftware de>
parents:
353
diff
changeset
|
1698 args->push(new Argument(STCin, dgty, NULL, NULL)); |
d8234836b40f
Get rid of runTimeHack and instead add proper argument info to the frontend
Christian Kamm <kamm incasoftware de>
parents:
353
diff
changeset
|
1699 fdapply = FuncDeclaration::genCfunc(args, Type::tindex, fdname); |
d8234836b40f
Get rid of runTimeHack and instead add proper argument info to the frontend
Christian Kamm <kamm incasoftware de>
parents:
353
diff
changeset
|
1700 } |
159 | 1701 |
1702 ec = new VarExp(0, fdapply); | |
1703 Expressions *exps = new Expressions(); | |
1704 if (tab->ty == Tsarray) | |
1705 aggr = aggr->castTo(sc, tn->arrayOf()); | |
715
30b42a283c8e
Removed TypeOpaque from DMD.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
664
diff
changeset
|
1706 exps->push(aggr); |
719
7261ff0f95ff
Implemented first class delegates. closes #101
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
715
diff
changeset
|
1707 |
7261ff0f95ff
Implemented first class delegates. closes #101
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
715
diff
changeset
|
1708 // LDC paint delegate argument to the type runtime expects |
7261ff0f95ff
Implemented first class delegates. closes #101
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
715
diff
changeset
|
1709 if (!dgty->equals(flde->type)) |
7261ff0f95ff
Implemented first class delegates. closes #101
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
715
diff
changeset
|
1710 { |
7261ff0f95ff
Implemented first class delegates. closes #101
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
715
diff
changeset
|
1711 flde = new CastExp(loc, flde, flde->type); |
7261ff0f95ff
Implemented first class delegates. closes #101
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
715
diff
changeset
|
1712 flde->type = dgty; |
7261ff0f95ff
Implemented first class delegates. closes #101
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
715
diff
changeset
|
1713 } |
159 | 1714 exps->push(flde); |
719
7261ff0f95ff
Implemented first class delegates. closes #101
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
715
diff
changeset
|
1715 |
159 | 1716 e = new CallExp(loc, ec, exps); |
1717 e->type = Type::tindex; // don't run semantic() on e | |
1718 } | |
1719 else if (tab->ty == Tdelegate) | |
1720 { | |
1721 /* Call: | |
1722 * aggr(flde) | |
1723 */ | |
1724 Expressions *exps = new Expressions(); | |
1725 exps->push(flde); | |
1726 e = new CallExp(loc, aggr, exps); | |
1727 e = e->semantic(sc); | |
1728 if (e->type != Type::tint32) | |
1729 error("opApply() function for %s must return an int", tab->toChars()); | |
1730 } | |
1731 else | |
1732 { | |
1733 /* Call: | |
1734 * aggr.apply(flde) | |
1735 */ | |
1736 ec = new DotIdExp(loc, aggr, | |
1737 (op == TOKforeach_reverse) ? Id::applyReverse | |
1738 : Id::apply); | |
1739 Expressions *exps = new Expressions(); | |
1740 exps->push(flde); | |
1741 e = new CallExp(loc, ec, exps); | |
1742 e = e->semantic(sc); | |
1743 if (e->type != Type::tint32) | |
1744 error("opApply() function for %s must return an int", tab->toChars()); | |
1745 } | |
1746 | |
1747 if (!cases.dim) | |
1748 // Easy case, a clean exit from the loop | |
1749 s = new ExpStatement(loc, e); | |
1750 else | |
1751 { // Construct a switch statement around the return value | |
1752 // of the apply function. | |
1753 Statements *a = new Statements(); | |
1754 | |
1755 // default: break; takes care of cases 0 and 1 | |
1756 s = new BreakStatement(0, NULL); | |
1757 s = new DefaultStatement(0, s); | |
1758 a->push(s); | |
1759 | |
1760 // cases 2... | |
1761 for (int i = 0; i < cases.dim; i++) | |
1762 { | |
1763 s = (Statement *)cases.data[i]; | |
1764 s = new CaseStatement(0, new IntegerExp(i + 2), s); | |
1765 a->push(s); | |
1766 } | |
1767 | |
1768 s = new CompoundStatement(loc, a); | |
1769 s = new SwitchStatement(loc, e, s); | |
1770 s = s->semantic(sc); | |
1771 } | |
1772 break; | |
1773 } | |
1774 | |
1775 default: | |
1776 error("foreach: %s is not an aggregate type", aggr->type->toChars()); | |
1777 break; | |
1778 } | |
1779 sc->noctor--; | |
1780 sc->pop(); | |
1781 return s; | |
1782 } | |
1783 | |
1784 int ForeachStatement::hasBreak() | |
1785 { | |
1786 return TRUE; | |
1787 } | |
1788 | |
1789 int ForeachStatement::hasContinue() | |
1790 { | |
1791 return TRUE; | |
1792 } | |
1793 | |
1794 int ForeachStatement::usesEH() | |
1795 { | |
1796 return body->usesEH(); | |
1797 } | |
1798 | |
336 | 1799 int ForeachStatement::blockExit() |
1800 { int result = BEfallthru; | |
1801 | |
1802 if (aggr->canThrow()) | |
1803 result |= BEthrow; | |
1804 | |
1805 if (body) | |
1806 { | |
1807 result |= body->blockExit() & ~(BEbreak | BEcontinue); | |
1808 } | |
1809 return result; | |
1810 } | |
1811 | |
159 | 1812 int ForeachStatement::comeFrom() |
1813 { | |
1814 if (body) | |
1815 return body->comeFrom(); | |
1816 return FALSE; | |
1817 } | |
1818 | |
1819 void ForeachStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
1820 { | |
1821 buf->writestring(Token::toChars(op)); | |
1822 buf->writestring(" ("); | |
1823 for (int i = 0; i < arguments->dim; i++) | |
1824 { | |
1825 Argument *a = (Argument *)arguments->data[i]; | |
1826 if (i) | |
1827 buf->writestring(", "); | |
1828 if (a->storageClass & STCref) | |
1829 buf->writestring((global.params.Dversion == 1) | |
1830 ? (char*)"inout " : (char*)"ref "); | |
1831 if (a->type) | |
1832 a->type->toCBuffer(buf, a->ident, hgs); | |
1833 else | |
1834 buf->writestring(a->ident->toChars()); | |
1835 } | |
1836 buf->writestring("; "); | |
1837 aggr->toCBuffer(buf, hgs); | |
1838 buf->writebyte(')'); | |
1839 buf->writenl(); | |
1840 buf->writebyte('{'); | |
1841 buf->writenl(); | |
1842 if (body) | |
1843 body->toCBuffer(buf, hgs); | |
1844 buf->writebyte('}'); | |
1845 buf->writenl(); | |
1846 } | |
1847 | |
1848 /******************************** IfStatement ***************************/ | |
1849 | |
1850 IfStatement::IfStatement(Loc loc, Argument *arg, Expression *condition, Statement *ifbody, Statement *elsebody) | |
1851 : Statement(loc) | |
1852 { | |
1853 this->arg = arg; | |
1854 this->condition = condition; | |
1855 this->ifbody = ifbody; | |
1856 this->elsebody = elsebody; | |
1857 this->match = NULL; | |
1858 } | |
1859 | |
1860 Statement *IfStatement::syntaxCopy() | |
1861 { | |
1862 Statement *i = NULL; | |
1863 if (ifbody) | |
1864 i = ifbody->syntaxCopy(); | |
1865 | |
1866 Statement *e = NULL; | |
1867 if (elsebody) | |
1868 e = elsebody->syntaxCopy(); | |
1869 | |
1870 Argument *a = arg ? arg->syntaxCopy() : NULL; | |
1871 IfStatement *s = new IfStatement(loc, a, condition->syntaxCopy(), i, e); | |
1872 return s; | |
1873 } | |
1874 | |
1875 Statement *IfStatement::semantic(Scope *sc) | |
1876 { | |
1877 condition = condition->semantic(sc); | |
1878 condition = resolveProperties(sc, condition); | |
1879 condition = condition->checkToBoolean(); | |
1880 | |
1881 // If we can short-circuit evaluate the if statement, don't do the | |
1882 // semantic analysis of the skipped code. | |
1883 // This feature allows a limited form of conditional compilation. | |
1884 condition = condition->optimize(WANTflags); | |
1885 | |
1886 // Evaluate at runtime | |
1887 unsigned cs0 = sc->callSuper; | |
1888 unsigned cs1; | |
1889 | |
1890 Scope *scd; | |
1891 if (arg) | |
1892 { /* Declare arg, which we will set to be the | |
1893 * result of condition. | |
1894 */ | |
1895 ScopeDsymbol *sym = new ScopeDsymbol(); | |
1896 sym->parent = sc->scopesym; | |
1897 scd = sc->push(sym); | |
1898 | |
1899 Type *t = arg->type ? arg->type : condition->type; | |
1900 match = new VarDeclaration(loc, t, arg->ident, NULL); | |
1901 match->noauto = 1; | |
1902 match->semantic(scd); | |
1903 if (!scd->insert(match)) | |
1904 assert(0); | |
1905 match->parent = sc->func; | |
1906 | |
1907 /* Generate: | |
1908 * (arg = condition) | |
1909 */ | |
1910 VarExp *v = new VarExp(0, match); | |
1911 condition = new AssignExp(loc, v, condition); | |
1912 condition = condition->semantic(scd); | |
1913 } | |
1914 else | |
1915 scd = sc->push(); | |
1916 ifbody = ifbody->semantic(scd); | |
1917 scd->pop(); | |
1918 | |
1919 cs1 = sc->callSuper; | |
1920 sc->callSuper = cs0; | |
1921 if (elsebody) | |
1922 elsebody = elsebody->semanticScope(sc, NULL, NULL); | |
1923 sc->mergeCallSuper(loc, cs1); | |
1924 | |
1925 return this; | |
1926 } | |
1927 | |
1928 int IfStatement::usesEH() | |
1929 { | |
1930 return (ifbody && ifbody->usesEH()) || (elsebody && elsebody->usesEH()); | |
1931 } | |
1932 | |
336 | 1933 int IfStatement::blockExit() |
1934 { | |
1935 //printf("IfStatement::blockExit(%p)\n", this); | |
1936 | |
1937 int result = BEnone; | |
1938 if (condition->canThrow()) | |
1939 result |= BEthrow; | |
1940 if (condition->isBool(TRUE)) | |
1941 { | |
1942 if (ifbody) | |
1943 result |= ifbody->blockExit(); | |
1944 else | |
1945 result |= BEfallthru; | |
1946 } | |
1947 else if (condition->isBool(FALSE)) | |
1948 { | |
1949 if (elsebody) | |
1950 result |= elsebody->blockExit(); | |
1951 else | |
1952 result |= BEfallthru; | |
1953 } | |
1954 else | |
1955 { | |
1956 if (ifbody) | |
1957 result |= ifbody->blockExit(); | |
1958 else | |
1959 result |= BEfallthru; | |
1960 if (elsebody) | |
1961 result |= elsebody->blockExit(); | |
1962 else | |
1963 result |= BEfallthru; | |
1964 } | |
1965 //printf("IfStatement::blockExit(%p) = x%x\n", this, result); | |
1966 return result; | |
1967 } | |
1968 | |
159 | 1969 void IfStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
1970 { | |
1971 buf->writestring("if ("); | |
1972 if (arg) | |
1973 { | |
1974 if (arg->type) | |
1975 arg->type->toCBuffer(buf, arg->ident, hgs); | |
1976 else | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
389
diff
changeset
|
1977 { buf->writestring("auto "); |
159 | 1978 buf->writestring(arg->ident->toChars()); |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
389
diff
changeset
|
1979 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
389
diff
changeset
|
1980 buf->writestring(" = "); |
159 | 1981 } |
1982 condition->toCBuffer(buf, hgs); | |
1983 buf->writebyte(')'); | |
1984 buf->writenl(); | |
1985 ifbody->toCBuffer(buf, hgs); | |
1986 if (elsebody) | |
1987 { buf->writestring("else"); | |
1988 buf->writenl(); | |
1989 elsebody->toCBuffer(buf, hgs); | |
1990 } | |
1991 } | |
1992 | |
1993 /******************************** ConditionalStatement ***************************/ | |
1994 | |
1995 ConditionalStatement::ConditionalStatement(Loc loc, Condition *condition, Statement *ifbody, Statement *elsebody) | |
1996 : Statement(loc) | |
1997 { | |
1998 this->condition = condition; | |
1999 this->ifbody = ifbody; | |
2000 this->elsebody = elsebody; | |
2001 } | |
2002 | |
2003 Statement *ConditionalStatement::syntaxCopy() | |
2004 { | |
2005 Statement *e = NULL; | |
2006 if (elsebody) | |
2007 e = elsebody->syntaxCopy(); | |
2008 ConditionalStatement *s = new ConditionalStatement(loc, | |
2009 condition->syntaxCopy(), ifbody->syntaxCopy(), e); | |
2010 return s; | |
2011 } | |
2012 | |
2013 Statement *ConditionalStatement::semantic(Scope *sc) | |
2014 { | |
2015 //printf("ConditionalStatement::semantic()\n"); | |
2016 | |
2017 // If we can short-circuit evaluate the if statement, don't do the | |
2018 // semantic analysis of the skipped code. | |
2019 // This feature allows a limited form of conditional compilation. | |
2020 if (condition->include(sc, NULL)) | |
2021 { | |
2022 ifbody = ifbody->semantic(sc); | |
2023 return ifbody; | |
2024 } | |
2025 else | |
2026 { | |
2027 if (elsebody) | |
2028 elsebody = elsebody->semantic(sc); | |
2029 return elsebody; | |
2030 } | |
2031 } | |
2032 | |
2033 Statements *ConditionalStatement::flatten(Scope *sc) | |
2034 { | |
2035 Statement *s; | |
2036 | |
2037 if (condition->include(sc, NULL)) | |
2038 s = ifbody; | |
2039 else | |
2040 s = elsebody; | |
2041 | |
2042 Statements *a = new Statements(); | |
2043 a->push(s); | |
2044 return a; | |
2045 } | |
2046 | |
2047 int ConditionalStatement::usesEH() | |
2048 { | |
2049 return (ifbody && ifbody->usesEH()) || (elsebody && elsebody->usesEH()); | |
2050 } | |
2051 | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
389
diff
changeset
|
2052 int ConditionalStatement::blockExit() |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
389
diff
changeset
|
2053 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
389
diff
changeset
|
2054 int result = ifbody->blockExit(); |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
389
diff
changeset
|
2055 if (elsebody) |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
389
diff
changeset
|
2056 result |= elsebody->blockExit(); |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
389
diff
changeset
|
2057 return result; |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
389
diff
changeset
|
2058 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
389
diff
changeset
|
2059 |
159 | 2060 void ConditionalStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
2061 { | |
2062 condition->toCBuffer(buf, hgs); | |
2063 buf->writenl(); | |
336 | 2064 buf->writeByte('{'); |
2065 buf->writenl(); | |
159 | 2066 if (ifbody) |
2067 ifbody->toCBuffer(buf, hgs); | |
336 | 2068 buf->writeByte('}'); |
2069 buf->writenl(); | |
159 | 2070 if (elsebody) |
2071 { | |
2072 buf->writestring("else"); | |
2073 buf->writenl(); | |
336 | 2074 buf->writeByte('{'); |
2075 buf->writenl(); | |
159 | 2076 elsebody->toCBuffer(buf, hgs); |
336 | 2077 buf->writeByte('}'); |
2078 buf->writenl(); | |
159 | 2079 } |
2080 buf->writenl(); | |
2081 } | |
2082 | |
2083 | |
2084 /******************************** PragmaStatement ***************************/ | |
2085 | |
2086 PragmaStatement::PragmaStatement(Loc loc, Identifier *ident, Expressions *args, Statement *body) | |
2087 : Statement(loc) | |
2088 { | |
2089 this->ident = ident; | |
2090 this->args = args; | |
2091 this->body = body; | |
2092 } | |
2093 | |
2094 Statement *PragmaStatement::syntaxCopy() | |
2095 { | |
2096 Statement *b = NULL; | |
2097 if (body) | |
2098 b = body->syntaxCopy(); | |
2099 PragmaStatement *s = new PragmaStatement(loc, | |
2100 ident, Expression::arraySyntaxCopy(args), b); | |
2101 return s; | |
2102 } | |
2103 | |
2104 Statement *PragmaStatement::semantic(Scope *sc) | |
2105 { // Should be merged with PragmaDeclaration | |
2106 //printf("PragmaStatement::semantic() %s\n", toChars()); | |
2107 //printf("body = %p\n", body); | |
2108 if (ident == Id::msg) | |
2109 { | |
2110 if (args) | |
2111 { | |
2112 for (size_t i = 0; i < args->dim; i++) | |
2113 { | |
2114 Expression *e = (Expression *)args->data[i]; | |
2115 | |
2116 e = e->semantic(sc); | |
2117 e = e->optimize(WANTvalue | WANTinterpret); | |
2118 if (e->op == TOKstring) | |
2119 { | |
2120 StringExp *se = (StringExp *)e; | |
794
661384d6a936
Fix warnings on x86-64. By fvbommel.
Christian Kamm <kamm incasoftware de>
parents:
765
diff
changeset
|
2121 fprintf(stdmsg, "%.*s", (int)se->len, (char*)se->string); |
159 | 2122 } |
2123 else | |
2124 error("string expected for message, not '%s'", e->toChars()); | |
2125 } | |
2126 fprintf(stdmsg, "\n"); | |
2127 } | |
2128 } | |
2129 else if (ident == Id::lib) | |
2130 { | |
846
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
2131 #if 1 |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
2132 /* Should this be allowed? |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
2133 */ |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
2134 error("pragma(lib) not allowed as statement"); |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
2135 #else |
159 | 2136 if (!args || args->dim != 1) |
2137 error("string expected for library name"); | |
2138 else | |
2139 { | |
2140 Expression *e = (Expression *)args->data[0]; | |
2141 | |
2142 e = e->semantic(sc); | |
2143 e = e->optimize(WANTvalue | WANTinterpret); | |
2144 args->data[0] = (void *)e; | |
2145 if (e->op != TOKstring) | |
2146 error("string expected for library name, not '%s'", e->toChars()); | |
2147 else if (global.params.verbose) | |
2148 { | |
2149 StringExp *se = (StringExp *)e; | |
2150 char *name = (char *)mem.malloc(se->len + 1); | |
2151 memcpy(name, se->string, se->len); | |
2152 name[se->len] = 0; | |
2153 printf("library %s\n", name); | |
2154 mem.free(name); | |
2155 } | |
2156 } | |
846
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
2157 #endif |
159 | 2158 } |
945
03d7c4aac654
SWITCHED TO LLVM 2.5 !
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
908
diff
changeset
|
2159 |
03d7c4aac654
SWITCHED TO LLVM 2.5 !
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
908
diff
changeset
|
2160 // LDC |
03d7c4aac654
SWITCHED TO LLVM 2.5 !
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
908
diff
changeset
|
2161 else if (ident == Id::allow_inline) |
03d7c4aac654
SWITCHED TO LLVM 2.5 !
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
908
diff
changeset
|
2162 { |
03d7c4aac654
SWITCHED TO LLVM 2.5 !
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
908
diff
changeset
|
2163 sc->func->allowInlining = true; |
03d7c4aac654
SWITCHED TO LLVM 2.5 !
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
908
diff
changeset
|
2164 } |
03d7c4aac654
SWITCHED TO LLVM 2.5 !
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
908
diff
changeset
|
2165 |
159 | 2166 else |
2167 error("unrecognized pragma(%s)", ident->toChars()); | |
2168 | |
2169 if (body) | |
2170 { | |
2171 body = body->semantic(sc); | |
2172 } | |
2173 return body; | |
2174 } | |
2175 | |
2176 int PragmaStatement::usesEH() | |
2177 { | |
2178 return body && body->usesEH(); | |
2179 } | |
2180 | |
336 | 2181 int PragmaStatement::blockExit() |
2182 { | |
2183 int result = BEfallthru; | |
2184 #if 0 // currently, no code is generated for Pragma's, so it's just fallthru | |
2185 if (arrayExpressionCanThrow(args)) | |
2186 result |= BEthrow; | |
2187 if (body) | |
2188 result |= body->blockExit(); | |
2189 #endif | |
2190 return result; | |
2191 } | |
2192 | |
159 | 2193 void PragmaStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
2194 { | |
2195 buf->writestring("pragma ("); | |
2196 buf->writestring(ident->toChars()); | |
2197 if (args && args->dim) | |
2198 { | |
2199 buf->writestring(", "); | |
2200 argsToCBuffer(buf, args, hgs); | |
2201 } | |
2202 buf->writeByte(')'); | |
2203 if (body) | |
2204 { | |
2205 buf->writenl(); | |
2206 buf->writeByte('{'); | |
2207 buf->writenl(); | |
2208 | |
2209 body->toCBuffer(buf, hgs); | |
2210 | |
2211 buf->writeByte('}'); | |
2212 buf->writenl(); | |
2213 } | |
2214 else | |
2215 { | |
2216 buf->writeByte(';'); | |
2217 buf->writenl(); | |
2218 } | |
2219 } | |
2220 | |
2221 | |
2222 /******************************** StaticAssertStatement ***************************/ | |
2223 | |
2224 StaticAssertStatement::StaticAssertStatement(StaticAssert *sa) | |
2225 : Statement(sa->loc) | |
2226 { | |
2227 this->sa = sa; | |
2228 } | |
2229 | |
2230 Statement *StaticAssertStatement::syntaxCopy() | |
2231 { | |
2232 StaticAssertStatement *s = new StaticAssertStatement((StaticAssert *)sa->syntaxCopy(NULL)); | |
2233 return s; | |
2234 } | |
2235 | |
2236 Statement *StaticAssertStatement::semantic(Scope *sc) | |
2237 { | |
2238 sa->semantic2(sc); | |
2239 return NULL; | |
2240 } | |
2241 | |
2242 void StaticAssertStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2243 { | |
2244 sa->toCBuffer(buf, hgs); | |
2245 } | |
2246 | |
2247 | |
2248 /******************************** SwitchStatement ***************************/ | |
2249 | |
2250 SwitchStatement::SwitchStatement(Loc loc, Expression *c, Statement *b) | |
2251 : Statement(loc) | |
2252 { | |
2253 condition = c; | |
2254 body = b; | |
2255 sdefault = NULL; | |
2256 cases = NULL; | |
2257 hasNoDefault = 0; | |
664
eef8ac26c66c
Some missed LLVMDC -> LDC.
Christian Kamm <kamm incasoftware de>
parents:
510
diff
changeset
|
2258 // LDC |
309 | 2259 enclosinghandler = NULL; |
159 | 2260 } |
2261 | |
2262 Statement *SwitchStatement::syntaxCopy() | |
2263 { | |
2264 SwitchStatement *s = new SwitchStatement(loc, | |
2265 condition->syntaxCopy(), body->syntaxCopy()); | |
2266 return s; | |
2267 } | |
2268 | |
2269 Statement *SwitchStatement::semantic(Scope *sc) | |
2270 { | |
2271 //printf("SwitchStatement::semantic(%p)\n", this); | |
2272 assert(!cases); // ensure semantic() is only run once | |
2273 | |
309 | 2274 enclosinghandler = sc->tfOfTry; |
159 | 2275 |
2276 condition = condition->semantic(sc); | |
2277 condition = resolveProperties(sc, condition); | |
2278 if (condition->type->isString()) | |
2279 { | |
2280 // If it's not an array, cast it to one | |
2281 if (condition->type->ty != Tarray) | |
2282 { | |
2283 condition = condition->implicitCastTo(sc, condition->type->nextOf()->arrayOf()); | |
2284 } | |
2285 } | |
2286 else | |
2287 { condition = condition->integralPromotions(sc); | |
2288 condition->checkIntegral(); | |
2289 } | |
2290 condition = condition->optimize(WANTvalue); | |
2291 | |
2292 sc = sc->push(); | |
2293 sc->sbreak = this; | |
2294 sc->sw = this; | |
2295 | |
2296 cases = new Array(); | |
2297 sc->noctor++; // BUG: should use Scope::mergeCallSuper() for each case instead | |
2298 body = body->semantic(sc); | |
2299 sc->noctor--; | |
2300 | |
2301 // Resolve any goto case's with exp | |
2302 for (int i = 0; i < gotoCases.dim; i++) | |
2303 { | |
2304 GotoCaseStatement *gcs = (GotoCaseStatement *)gotoCases.data[i]; | |
2305 | |
2306 if (!gcs->exp) | |
2307 { | |
2308 gcs->error("no case statement following goto case;"); | |
2309 break; | |
2310 } | |
2311 | |
2312 for (Scope *scx = sc; scx; scx = scx->enclosing) | |
2313 { | |
2314 if (!scx->sw) | |
2315 continue; | |
2316 for (int j = 0; j < scx->sw->cases->dim; j++) | |
2317 { | |
2318 CaseStatement *cs = (CaseStatement *)scx->sw->cases->data[j]; | |
2319 | |
2320 if (cs->exp->equals(gcs->exp)) | |
2321 { | |
2322 gcs->cs = cs; | |
2323 goto Lfoundcase; | |
2324 } | |
2325 } | |
2326 } | |
2327 gcs->error("case %s not found", gcs->exp->toChars()); | |
2328 | |
2329 Lfoundcase: | |
2330 ; | |
2331 } | |
2332 | |
2333 if (!sc->sw->sdefault) | |
2334 { hasNoDefault = 1; | |
2335 | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
945
diff
changeset
|
2336 warning("switch statement has no default"); |
159 | 2337 |
2338 // Generate runtime error if the default is hit | |
2339 Statements *a = new Statements(); | |
2340 CompoundStatement *cs; | |
2341 Statement *s; | |
2342 | |
2343 if (global.params.useSwitchError) | |
2344 s = new SwitchErrorStatement(loc); | |
2345 else | |
2346 { Expression *e = new HaltExp(loc); | |
2347 s = new ExpStatement(loc, e); | |
2348 } | |
2349 | |
2350 a->reserve(4); | |
2351 a->push(body); | |
901 | 2352 |
2353 // LDC needs semantic to be run on break | |
2354 Statement *breakstmt = new BreakStatement(loc, NULL); | |
2355 breakstmt->semantic(sc); | |
2356 a->push(breakstmt); | |
2357 | |
159 | 2358 sc->sw->sdefault = new DefaultStatement(loc, s); |
2359 a->push(sc->sw->sdefault); | |
2360 cs = new CompoundStatement(loc, a); | |
2361 body = cs; | |
2362 } | |
2363 | |
2364 sc->pop(); | |
2365 return this; | |
2366 } | |
2367 | |
2368 int SwitchStatement::hasBreak() | |
2369 { | |
2370 return TRUE; | |
2371 } | |
2372 | |
2373 int SwitchStatement::usesEH() | |
2374 { | |
2375 return body ? body->usesEH() : 0; | |
2376 } | |
2377 | |
336 | 2378 int SwitchStatement::blockExit() |
2379 { int result = BEnone; | |
2380 if (condition->canThrow()) | |
2381 result |= BEthrow; | |
2382 | |
2383 if (body) | |
2384 { result |= body->blockExit(); | |
2385 if (result & BEbreak) | |
2386 { result |= BEfallthru; | |
2387 result &= ~BEbreak; | |
2388 } | |
2389 } | |
2390 else | |
2391 result |= BEfallthru; | |
2392 | |
2393 return result; | |
2394 } | |
2395 | |
159 | 2396 void SwitchStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
2397 { | |
2398 buf->writestring("switch ("); | |
2399 condition->toCBuffer(buf, hgs); | |
2400 buf->writebyte(')'); | |
2401 buf->writenl(); | |
2402 if (body) | |
2403 { | |
2404 if (!body->isScopeStatement()) | |
2405 { buf->writebyte('{'); | |
2406 buf->writenl(); | |
2407 body->toCBuffer(buf, hgs); | |
2408 buf->writebyte('}'); | |
2409 buf->writenl(); | |
2410 } | |
2411 else | |
2412 { | |
2413 body->toCBuffer(buf, hgs); | |
2414 } | |
2415 } | |
2416 } | |
2417 | |
2418 /******************************** CaseStatement ***************************/ | |
2419 | |
2420 CaseStatement::CaseStatement(Loc loc, Expression *exp, Statement *s) | |
2421 : Statement(loc) | |
2422 { | |
2423 this->exp = exp; | |
2424 this->statement = s; | |
2425 cblock = NULL; | |
2426 bodyBB = NULL; | |
262
88252a1af660
[svn r280] Fixed a bunch of issues with switch statements. Ended up a bit far reaching...
lindquist
parents:
232
diff
changeset
|
2427 llvmIdx = NULL; |
159 | 2428 } |
2429 | |
2430 Statement *CaseStatement::syntaxCopy() | |
2431 { | |
2432 CaseStatement *s = new CaseStatement(loc, exp->syntaxCopy(), statement->syntaxCopy()); | |
2433 return s; | |
2434 } | |
2435 | |
2436 Statement *CaseStatement::semantic(Scope *sc) | |
2437 { SwitchStatement *sw = sc->sw; | |
2438 | |
2439 //printf("CaseStatement::semantic() %s\n", toChars()); | |
2440 exp = exp->semantic(sc); | |
2441 if (sw) | |
846
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
2442 { |
159 | 2443 exp = exp->implicitCastTo(sc, sw->condition->type); |
2444 exp = exp->optimize(WANTvalue | WANTinterpret); | |
2445 if (exp->op != TOKstring && exp->op != TOKint64) | |
2446 { | |
2447 error("case must be a string or an integral constant, not %s", exp->toChars()); | |
2448 exp = new IntegerExp(0); | |
2449 } | |
2450 | |
846
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
2451 for (int i = 0; i < sw->cases->dim; i++) |
159 | 2452 { |
2453 CaseStatement *cs = (CaseStatement *)sw->cases->data[i]; | |
2454 | |
2455 //printf("comparing '%s' with '%s'\n", exp->toChars(), cs->exp->toChars()); | |
2456 if (cs->exp->equals(exp)) | |
2457 { error("duplicate case %s in switch statement", exp->toChars()); | |
2458 break; | |
2459 } | |
2460 } | |
2461 | |
2462 sw->cases->push(this); | |
2463 | |
2464 // Resolve any goto case's with no exp to this case statement | |
846
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
2465 for (size_t i = 0; i < sw->gotoCases.dim; i++) |
159 | 2466 { |
2467 GotoCaseStatement *gcs = (GotoCaseStatement *)sw->gotoCases.data[i]; | |
2468 | |
2469 if (!gcs->exp) | |
2470 { | |
2471 gcs->cs = this; | |
2472 sw->gotoCases.remove(i); // remove from array | |
2473 } | |
2474 } | |
2475 } | |
2476 else | |
2477 error("case not in switch statement"); | |
2478 statement = statement->semantic(sc); | |
2479 return this; | |
2480 } | |
2481 | |
2482 int CaseStatement::compare(Object *obj) | |
2483 { | |
2484 // Sort cases so we can do an efficient lookup | |
2485 CaseStatement *cs2 = (CaseStatement *)(obj); | |
2486 | |
2487 return exp->compare(cs2->exp); | |
2488 } | |
2489 | |
2490 int CaseStatement::usesEH() | |
2491 { | |
2492 return statement->usesEH(); | |
2493 } | |
2494 | |
336 | 2495 int CaseStatement::blockExit() |
2496 { | |
2497 return statement->blockExit(); | |
2498 } | |
2499 | |
159 | 2500 int CaseStatement::comeFrom() |
2501 { | |
2502 return TRUE; | |
2503 } | |
2504 | |
2505 void CaseStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2506 { | |
2507 buf->writestring("case "); | |
2508 exp->toCBuffer(buf, hgs); | |
2509 buf->writebyte(':'); | |
2510 buf->writenl(); | |
2511 statement->toCBuffer(buf, hgs); | |
2512 } | |
2513 | |
2514 /******************************** DefaultStatement ***************************/ | |
2515 | |
2516 DefaultStatement::DefaultStatement(Loc loc, Statement *s) | |
2517 : Statement(loc) | |
2518 { | |
2519 this->statement = s; | |
2520 #if IN_GCC | |
2521 + cblock = NULL; | |
2522 #endif | |
2523 bodyBB = NULL; | |
2524 } | |
2525 | |
2526 Statement *DefaultStatement::syntaxCopy() | |
2527 { | |
2528 DefaultStatement *s = new DefaultStatement(loc, statement->syntaxCopy()); | |
2529 return s; | |
2530 } | |
2531 | |
2532 Statement *DefaultStatement::semantic(Scope *sc) | |
2533 { | |
336 | 2534 //printf("DefaultStatement::semantic()\n"); |
159 | 2535 if (sc->sw) |
2536 { | |
2537 if (sc->sw->sdefault) | |
2538 { | |
2539 error("switch statement already has a default"); | |
2540 } | |
2541 sc->sw->sdefault = this; | |
2542 } | |
2543 else | |
2544 error("default not in switch statement"); | |
2545 statement = statement->semantic(sc); | |
2546 return this; | |
2547 } | |
2548 | |
2549 int DefaultStatement::usesEH() | |
2550 { | |
2551 return statement->usesEH(); | |
2552 } | |
2553 | |
336 | 2554 int DefaultStatement::blockExit() |
2555 { | |
2556 return statement->blockExit(); | |
2557 } | |
2558 | |
159 | 2559 int DefaultStatement::comeFrom() |
2560 { | |
2561 return TRUE; | |
2562 } | |
2563 | |
2564 void DefaultStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2565 { | |
2566 buf->writestring("default:\n"); | |
2567 statement->toCBuffer(buf, hgs); | |
2568 } | |
2569 | |
2570 /******************************** GotoDefaultStatement ***************************/ | |
2571 | |
2572 GotoDefaultStatement::GotoDefaultStatement(Loc loc) | |
2573 : Statement(loc) | |
2574 { | |
2575 sw = NULL; | |
309 | 2576 enclosinghandler = NULL; |
159 | 2577 } |
2578 | |
2579 Statement *GotoDefaultStatement::syntaxCopy() | |
2580 { | |
2581 GotoDefaultStatement *s = new GotoDefaultStatement(loc); | |
2582 return s; | |
2583 } | |
2584 | |
2585 Statement *GotoDefaultStatement::semantic(Scope *sc) | |
2586 { | |
309 | 2587 enclosinghandler = sc->tfOfTry; |
159 | 2588 sw = sc->sw; |
2589 if (!sw) | |
2590 error("goto default not in switch statement"); | |
2591 return this; | |
2592 } | |
2593 | |
336 | 2594 int GotoDefaultStatement::blockExit() |
2595 { | |
2596 return BEgoto; | |
2597 } | |
2598 | |
159 | 2599 void GotoDefaultStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
2600 { | |
2601 buf->writestring("goto default;\n"); | |
2602 } | |
2603 | |
2604 /******************************** GotoCaseStatement ***************************/ | |
2605 | |
2606 GotoCaseStatement::GotoCaseStatement(Loc loc, Expression *exp) | |
2607 : Statement(loc) | |
2608 { | |
2609 cs = NULL; | |
2610 this->exp = exp; | |
309 | 2611 enclosinghandler = NULL; |
159 | 2612 sw = NULL; |
2613 } | |
2614 | |
2615 Statement *GotoCaseStatement::syntaxCopy() | |
2616 { | |
2617 Expression *e = exp ? exp->syntaxCopy() : NULL; | |
2618 GotoCaseStatement *s = new GotoCaseStatement(loc, e); | |
2619 return s; | |
2620 } | |
2621 | |
2622 Statement *GotoCaseStatement::semantic(Scope *sc) | |
2623 { | |
309 | 2624 enclosinghandler = sc->tfOfTry; |
159 | 2625 if (exp) |
2626 exp = exp->semantic(sc); | |
2627 | |
2628 if (!sc->sw) | |
2629 error("goto case not in switch statement"); | |
2630 else | |
2631 { | |
2632 sw = sc->sw; | |
2633 sc->sw->gotoCases.push(this); | |
2634 if (exp) | |
2635 { | |
2636 exp = exp->implicitCastTo(sc, sc->sw->condition->type); | |
2637 exp = exp->optimize(WANTvalue); | |
2638 } | |
2639 } | |
2640 return this; | |
2641 } | |
2642 | |
336 | 2643 int GotoCaseStatement::blockExit() |
2644 { | |
2645 return BEgoto; | |
2646 } | |
2647 | |
159 | 2648 void GotoCaseStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
2649 { | |
2650 buf->writestring("goto case"); | |
2651 if (exp) | |
2652 { buf->writebyte(' '); | |
2653 exp->toCBuffer(buf, hgs); | |
2654 } | |
2655 buf->writebyte(';'); | |
2656 buf->writenl(); | |
2657 } | |
2658 | |
2659 /******************************** SwitchErrorStatement ***************************/ | |
2660 | |
2661 SwitchErrorStatement::SwitchErrorStatement(Loc loc) | |
2662 : Statement(loc) | |
2663 { | |
2664 } | |
2665 | |
336 | 2666 int SwitchErrorStatement::blockExit() |
2667 { | |
2668 return BEthrow; | |
2669 } | |
2670 | |
159 | 2671 void SwitchErrorStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
2672 { | |
2673 buf->writestring("SwitchErrorStatement::toCBuffer()"); | |
2674 buf->writenl(); | |
2675 } | |
2676 | |
2677 /******************************** ReturnStatement ***************************/ | |
2678 | |
2679 ReturnStatement::ReturnStatement(Loc loc, Expression *exp) | |
2680 : Statement(loc) | |
2681 { | |
2682 this->exp = exp; | |
309 | 2683 this->enclosinghandler = NULL; |
159 | 2684 } |
2685 | |
2686 Statement *ReturnStatement::syntaxCopy() | |
2687 { | |
2688 Expression *e = NULL; | |
2689 if (exp) | |
2690 e = exp->syntaxCopy(); | |
2691 ReturnStatement *s = new ReturnStatement(loc, e); | |
2692 return s; | |
2693 } | |
2694 | |
2695 Statement *ReturnStatement::semantic(Scope *sc) | |
2696 { | |
2697 //printf("ReturnStatement::semantic() %s\n", toChars()); | |
309 | 2698 this->enclosinghandler = sc->tfOfTry; |
159 | 2699 |
2700 FuncDeclaration *fd = sc->parent->isFuncDeclaration(); | |
2701 Scope *scx = sc; | |
2702 int implicit0 = 0; | |
2703 | |
2704 if (sc->fes) | |
2705 { | |
2706 // Find scope of function foreach is in | |
2707 for (; 1; scx = scx->enclosing) | |
2708 { | |
2709 assert(scx); | |
2710 if (scx->func != fd) | |
2711 { fd = scx->func; // fd is now function enclosing foreach | |
2712 break; | |
2713 } | |
2714 } | |
2715 } | |
2716 | |
2717 Type *tret = fd->type->nextOf(); | |
2718 if (fd->tintro) | |
336 | 2719 /* We'll be implicitly casting the return expression to tintro |
2720 */ | |
159 | 2721 tret = fd->tintro->nextOf(); |
2722 Type *tbret = NULL; | |
2723 | |
2724 if (tret) | |
2725 tbret = tret->toBasetype(); | |
2726 | |
2727 // main() returns 0, even if it returns void | |
2728 if (!exp && (!tbret || tbret->ty == Tvoid) && fd->isMain()) | |
2729 { implicit0 = 1; | |
2730 exp = new IntegerExp(0); | |
2731 } | |
2732 | |
2733 if (sc->incontract || scx->incontract) | |
2734 error("return statements cannot be in contracts"); | |
2735 if (sc->tf || scx->tf) | |
2736 error("return statements cannot be in finally, scope(exit) or scope(success) bodies"); | |
2737 | |
2738 if (fd->isCtorDeclaration()) | |
2739 { | |
2740 // Constructors implicitly do: | |
2741 // return this; | |
2742 if (exp && exp->op != TOKthis) | |
2743 error("cannot return expression from constructor"); | |
2744 exp = new ThisExp(0); | |
2745 } | |
2746 | |
2747 if (!exp) | |
2748 fd->nrvo_can = 0; | |
2749 | |
2750 if (exp) | |
2751 { | |
2752 fd->hasReturnExp |= 1; | |
2753 | |
2754 exp = exp->semantic(sc); | |
2755 exp = resolveProperties(sc, exp); | |
2756 exp = exp->optimize(WANTvalue); | |
2757 | |
2758 if (fd->nrvo_can && exp->op == TOKvar) | |
2759 { VarExp *ve = (VarExp *)exp; | |
2760 VarDeclaration *v = ve->var->isVarDeclaration(); | |
2761 | |
2762 if (!v || v->isOut() || v->isRef()) | |
2763 fd->nrvo_can = 0; | |
2764 else if (fd->nrvo_var == NULL) | |
2765 { if (!v->isDataseg() && !v->isParameter() && v->toParent2() == fd) | |
2766 fd->nrvo_var = v; | |
2767 else | |
2768 fd->nrvo_can = 0; | |
2769 } | |
2770 else if (fd->nrvo_var != v) | |
2771 fd->nrvo_can = 0; | |
2772 } | |
2773 else | |
2774 fd->nrvo_can = 0; | |
2775 | |
2776 if (fd->returnLabel && tbret->ty != Tvoid) | |
2777 { | |
2778 } | |
2779 else if (fd->inferRetType) | |
2780 { | |
2781 if (fd->type->nextOf()) | |
2782 { | |
2783 if (!exp->type->equals(fd->type->nextOf())) | |
2784 error("mismatched function return type inference of %s and %s", | |
2785 exp->type->toChars(), fd->type->nextOf()->toChars()); | |
2786 } | |
2787 else | |
2788 { | |
2789 fd->type->next = exp->type; | |
2790 fd->type = fd->type->semantic(loc, sc); | |
2791 if (!fd->tintro) | |
2792 { tret = fd->type->nextOf(); | |
2793 tbret = tret->toBasetype(); | |
2794 } | |
2795 } | |
2796 } | |
2797 else if (tbret->ty != Tvoid) | |
2798 { | |
2799 exp = exp->implicitCastTo(sc, tret); | |
2800 } | |
2801 } | |
2802 else if (fd->inferRetType) | |
2803 { | |
2804 if (fd->type->nextOf()) | |
2805 { | |
2806 if (fd->type->nextOf()->ty != Tvoid) | |
2807 error("mismatched function return type inference of void and %s", | |
2808 fd->type->nextOf()->toChars()); | |
2809 } | |
2810 else | |
2811 { | |
2812 fd->type->next = Type::tvoid; | |
2813 fd->type = fd->type->semantic(loc, sc); | |
2814 if (!fd->tintro) | |
2815 { tret = Type::tvoid; | |
2816 tbret = tret; | |
2817 } | |
2818 } | |
2819 } | |
2820 else if (tbret->ty != Tvoid) // if non-void return | |
2821 error("return expression expected"); | |
2822 | |
2823 if (sc->fes) | |
2824 { | |
2825 Statement *s; | |
2826 | |
2827 if (exp && !implicit0) | |
2828 { | |
2829 exp = exp->implicitCastTo(sc, tret); | |
2830 } | |
2831 if (!exp || exp->op == TOKint64 || exp->op == TOKfloat64 || | |
2832 exp->op == TOKimaginary80 || exp->op == TOKcomplex80 || | |
2833 exp->op == TOKthis || exp->op == TOKsuper || exp->op == TOKnull || | |
2834 exp->op == TOKstring) | |
2835 { | |
2836 sc->fes->cases.push(this); | |
846
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
2837 // Construct: return cases.dim+1; |
159 | 2838 s = new ReturnStatement(0, new IntegerExp(sc->fes->cases.dim + 1)); |
2839 } | |
2840 else if (fd->type->nextOf()->toBasetype() == Type::tvoid) | |
2841 { | |
2842 s = new ReturnStatement(0, NULL); | |
2843 sc->fes->cases.push(s); | |
2844 | |
2845 // Construct: { exp; return cases.dim + 1; } | |
846
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
2846 Statement *s1 = new ExpStatement(loc, exp); |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
2847 Statement *s2 = new ReturnStatement(0, new IntegerExp(sc->fes->cases.dim + 1)); |
159 | 2848 s = new CompoundStatement(loc, s1, s2); |
2849 } | |
2850 else | |
2851 { | |
2852 // Construct: return vresult; | |
2853 if (!fd->vresult) | |
846
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
2854 { // Declare vresult |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
2855 VarDeclaration *v = new VarDeclaration(loc, tret, Id::result, NULL); |
159 | 2856 v->noauto = 1; |
2857 v->semantic(scx); | |
2858 if (!scx->insert(v)) | |
2859 assert(0); | |
2860 v->parent = fd; | |
2861 fd->vresult = v; | |
2862 } | |
2863 | |
846
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
2864 s = new ReturnStatement(0, new VarExp(0, fd->vresult)); |
159 | 2865 sc->fes->cases.push(s); |
2866 | |
2867 // Construct: { vresult = exp; return cases.dim + 1; } | |
846
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
2868 exp = new AssignExp(loc, new VarExp(0, fd->vresult), exp); |
159 | 2869 exp = exp->semantic(sc); |
846
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
2870 Statement *s1 = new ExpStatement(loc, exp); |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
2871 Statement *s2 = new ReturnStatement(0, new IntegerExp(sc->fes->cases.dim + 1)); |
159 | 2872 s = new CompoundStatement(loc, s1, s2); |
2873 } | |
2874 return s; | |
2875 } | |
2876 | |
2877 if (exp) | |
2878 { | |
2879 if (fd->returnLabel && tbret->ty != Tvoid) | |
2880 { | |
2881 assert(fd->vresult); | |
2882 VarExp *v = new VarExp(0, fd->vresult); | |
2883 | |
2884 exp = new AssignExp(loc, v, exp); | |
2885 exp = exp->semantic(sc); | |
2886 } | |
2887 //exp->dump(0); | |
2888 //exp->print(); | |
2889 exp->checkEscape(); | |
2890 } | |
2891 | |
2892 /* BUG: need to issue an error on: | |
2893 * this | |
2894 * { if (x) return; | |
2895 * super(); | |
2896 * } | |
2897 */ | |
2898 | |
2899 if (sc->callSuper & CSXany_ctor && | |
2900 !(sc->callSuper & (CSXthis_ctor | CSXsuper_ctor))) | |
2901 error("return without calling constructor"); | |
2902 | |
2903 sc->callSuper |= CSXreturn; | |
2904 | |
2905 // See if all returns are instead to be replaced with a goto returnLabel; | |
2906 if (fd->returnLabel) | |
2907 { | |
2908 GotoStatement *gs = new GotoStatement(loc, Id::returnLabel); | |
2909 | |
2910 gs->label = fd->returnLabel; | |
2911 if (exp) | |
846
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
2912 { /* Replace: return exp; |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
2913 * with: exp; goto returnLabel; |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
2914 */ |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
2915 Statement *s = new ExpStatement(0, exp); |
159 | 2916 return new CompoundStatement(loc, s, gs); |
2917 } | |
2918 return gs; | |
2919 } | |
2920 | |
2921 if (exp && tbret->ty == Tvoid && !fd->isMain()) | |
336 | 2922 { |
2923 /* Replace: | |
2924 * return exp; | |
2925 * with: | |
2926 * exp; return; | |
2927 */ | |
2928 Statement *s = new ExpStatement(loc, exp); | |
159 | 2929 loc = 0; |
2930 exp = NULL; | |
2931 return new CompoundStatement(loc, s, this); | |
2932 } | |
2933 | |
2934 return this; | |
2935 } | |
2936 | |
336 | 2937 int ReturnStatement::blockExit() |
2938 { int result = BEreturn; | |
2939 | |
2940 if (exp && exp->canThrow()) | |
2941 result |= BEthrow; | |
2942 return result; | |
2943 } | |
2944 | |
159 | 2945 void ReturnStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
2946 { | |
2947 buf->printf("return "); | |
2948 if (exp) | |
2949 exp->toCBuffer(buf, hgs); | |
2950 buf->writeByte(';'); | |
2951 buf->writenl(); | |
2952 } | |
2953 | |
2954 /******************************** BreakStatement ***************************/ | |
2955 | |
2956 BreakStatement::BreakStatement(Loc loc, Identifier *ident) | |
2957 : Statement(loc) | |
2958 { | |
2959 this->ident = ident; | |
309 | 2960 this->enclosinghandler = NULL; |
159 | 2961 } |
2962 | |
2963 Statement *BreakStatement::syntaxCopy() | |
2964 { | |
2965 BreakStatement *s = new BreakStatement(loc, ident); | |
2966 return s; | |
2967 } | |
2968 | |
2969 Statement *BreakStatement::semantic(Scope *sc) | |
2970 { | |
336 | 2971 //printf("BreakStatement::semantic()\n"); |
309 | 2972 enclosinghandler = sc->tfOfTry; |
159 | 2973 // If: |
2974 // break Identifier; | |
2975 if (ident) | |
2976 { | |
2977 Scope *scx; | |
2978 FuncDeclaration *thisfunc = sc->func; | |
2979 | |
2980 for (scx = sc; scx; scx = scx->enclosing) | |
2981 { | |
2982 LabelStatement *ls; | |
2983 | |
2984 if (scx->func != thisfunc) // if in enclosing function | |
2985 { | |
2986 if (sc->fes) // if this is the body of a foreach | |
2987 { | |
2988 /* Post this statement to the fes, and replace | |
2989 * it with a return value that caller will put into | |
2990 * a switch. Caller will figure out where the break | |
2991 * label actually is. | |
2992 * Case numbers start with 2, not 0, as 0 is continue | |
2993 * and 1 is break. | |
2994 */ | |
2995 Statement *s; | |
2996 sc->fes->cases.push(this); | |
2997 s = new ReturnStatement(0, new IntegerExp(sc->fes->cases.dim + 1)); | |
2998 return s; | |
2999 } | |
3000 break; // can't break to it | |
3001 } | |
3002 | |
3003 ls = scx->slabel; | |
3004 if (ls && ls->ident == ident) | |
3005 { | |
3006 Statement *s = ls->statement; | |
3007 | |
3008 if (!s->hasBreak()) | |
3009 error("label '%s' has no break", ident->toChars()); | |
3010 if (ls->tf != sc->tf) | |
3011 error("cannot break out of finally block"); | |
3012 | |
3013 this->target = ls; | |
3014 return this; | |
3015 } | |
3016 } | |
3017 error("enclosing label '%s' for break not found", ident->toChars()); | |
3018 } | |
3019 else if (!sc->sbreak) | |
3020 { | |
3021 if (sc->fes) | |
3022 { Statement *s; | |
3023 | |
3024 // Replace break; with return 1; | |
3025 s = new ReturnStatement(0, new IntegerExp(1)); | |
3026 return s; | |
3027 } | |
3028 error("break is not inside a loop or switch"); | |
3029 } | |
3030 return this; | |
3031 } | |
3032 | |
336 | 3033 int BreakStatement::blockExit() |
3034 { | |
3035 //printf("BreakStatement::blockExit(%p) = x%x\n", this, ident ? BEgoto : BEbreak); | |
3036 return ident ? BEgoto : BEbreak; | |
3037 } | |
3038 | |
159 | 3039 void BreakStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
3040 { | |
3041 buf->writestring("break"); | |
3042 if (ident) | |
3043 { buf->writebyte(' '); | |
3044 buf->writestring(ident->toChars()); | |
3045 } | |
3046 buf->writebyte(';'); | |
3047 buf->writenl(); | |
3048 } | |
3049 | |
3050 /******************************** ContinueStatement ***************************/ | |
3051 | |
3052 ContinueStatement::ContinueStatement(Loc loc, Identifier *ident) | |
3053 : Statement(loc) | |
3054 { | |
3055 this->ident = ident; | |
309 | 3056 this->enclosinghandler = NULL; |
159 | 3057 } |
3058 | |
3059 Statement *ContinueStatement::syntaxCopy() | |
3060 { | |
3061 ContinueStatement *s = new ContinueStatement(loc, ident); | |
3062 return s; | |
3063 } | |
3064 | |
3065 Statement *ContinueStatement::semantic(Scope *sc) | |
3066 { | |
309 | 3067 enclosinghandler = sc->tfOfTry; |
159 | 3068 //printf("ContinueStatement::semantic() %p\n", this); |
3069 if (ident) | |
3070 { | |
3071 Scope *scx; | |
3072 FuncDeclaration *thisfunc = sc->func; | |
3073 | |
3074 for (scx = sc; scx; scx = scx->enclosing) | |
3075 { | |
3076 LabelStatement *ls; | |
3077 | |
3078 if (scx->func != thisfunc) // if in enclosing function | |
3079 { | |
3080 if (sc->fes) // if this is the body of a foreach | |
3081 { | |
3082 for (; scx; scx = scx->enclosing) | |
3083 { | |
3084 ls = scx->slabel; | |
3085 if (ls && ls->ident == ident && ls->statement == sc->fes) | |
3086 { | |
3087 // Replace continue ident; with return 0; | |
3088 return new ReturnStatement(0, new IntegerExp(0)); | |
3089 } | |
3090 } | |
3091 | |
3092 /* Post this statement to the fes, and replace | |
3093 * it with a return value that caller will put into | |
3094 * a switch. Caller will figure out where the break | |
3095 * label actually is. | |
3096 * Case numbers start with 2, not 0, as 0 is continue | |
3097 * and 1 is break. | |
3098 */ | |
3099 Statement *s; | |
3100 sc->fes->cases.push(this); | |
3101 s = new ReturnStatement(0, new IntegerExp(sc->fes->cases.dim + 1)); | |
3102 return s; | |
3103 } | |
3104 break; // can't continue to it | |
3105 } | |
3106 | |
3107 ls = scx->slabel; | |
3108 if (ls && ls->ident == ident) | |
3109 { | |
3110 Statement *s = ls->statement; | |
3111 | |
3112 if (!s->hasContinue()) | |
3113 error("label '%s' has no continue", ident->toChars()); | |
3114 if (ls->tf != sc->tf) | |
3115 error("cannot continue out of finally block"); | |
3116 | |
3117 this->target = ls; | |
3118 return this; | |
3119 } | |
3120 } | |
3121 error("enclosing label '%s' for continue not found", ident->toChars()); | |
3122 } | |
3123 else if (!sc->scontinue) | |
3124 { | |
3125 if (sc->fes) | |
3126 { Statement *s; | |
3127 | |
3128 // Replace continue; with return 0; | |
3129 s = new ReturnStatement(0, new IntegerExp(0)); | |
3130 return s; | |
3131 } | |
3132 error("continue is not inside a loop"); | |
3133 } | |
3134 return this; | |
3135 } | |
3136 | |
336 | 3137 int ContinueStatement::blockExit() |
3138 { | |
3139 return ident ? BEgoto : BEcontinue; | |
3140 } | |
3141 | |
159 | 3142 void ContinueStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
3143 { | |
3144 buf->writestring("continue"); | |
3145 if (ident) | |
3146 { buf->writebyte(' '); | |
3147 buf->writestring(ident->toChars()); | |
3148 } | |
3149 buf->writebyte(';'); | |
3150 buf->writenl(); | |
3151 } | |
3152 | |
3153 /******************************** SynchronizedStatement ***************************/ | |
3154 | |
3155 SynchronizedStatement::SynchronizedStatement(Loc loc, Expression *exp, Statement *body) | |
3156 : Statement(loc) | |
3157 { | |
3158 this->exp = exp; | |
3159 this->body = body; | |
3160 this->esync = NULL; | |
310
d20cf0dec9c1
[svn r331] Fixed, forgot to default initialize the SynchronizedStatement enclosinghandler.
lindquist
parents:
309
diff
changeset
|
3161 this->enclosinghandler = NULL; |
664
eef8ac26c66c
Some missed LLVMDC -> LDC.
Christian Kamm <kamm incasoftware de>
parents:
510
diff
changeset
|
3162 // LDC |
309 | 3163 this->llsync = NULL; |
159 | 3164 } |
3165 | |
3166 SynchronizedStatement::SynchronizedStatement(Loc loc, elem *esync, Statement *body) | |
3167 : Statement(loc) | |
3168 { | |
3169 this->exp = NULL; | |
3170 this->body = body; | |
3171 this->esync = esync; | |
310
d20cf0dec9c1
[svn r331] Fixed, forgot to default initialize the SynchronizedStatement enclosinghandler.
lindquist
parents:
309
diff
changeset
|
3172 this->enclosinghandler = NULL; |
664
eef8ac26c66c
Some missed LLVMDC -> LDC.
Christian Kamm <kamm incasoftware de>
parents:
510
diff
changeset
|
3173 // LDC |
309 | 3174 this->llsync = NULL; |
159 | 3175 } |
3176 | |
3177 Statement *SynchronizedStatement::syntaxCopy() | |
3178 { | |
3179 Expression *e = exp ? exp->syntaxCopy() : NULL; | |
3180 SynchronizedStatement *s = new SynchronizedStatement(loc, e, body ? body->syntaxCopy() : NULL); | |
3181 return s; | |
3182 } | |
3183 | |
3184 Statement *SynchronizedStatement::semantic(Scope *sc) | |
3185 { | |
3186 if (exp) | |
3187 { ClassDeclaration *cd; | |
3188 | |
3189 exp = exp->semantic(sc); | |
3190 exp = resolveProperties(sc, exp); | |
3191 cd = exp->type->isClassHandle(); | |
3192 if (!cd) | |
3193 error("can only synchronize on class objects, not '%s'", exp->type->toChars()); | |
3194 else if (cd->isInterfaceDeclaration()) | |
3195 { Type *t = new TypeIdentifier(0, Id::Object); | |
3196 | |
3197 t = t->semantic(0, sc); | |
3198 exp = new CastExp(loc, exp, t); | |
3199 exp = exp->semantic(sc); | |
3200 } | |
3201 } | |
3202 if (body) | |
309 | 3203 { |
3204 enclosinghandler = sc->tfOfTry; | |
3205 sc->tfOfTry = new EnclosingSynchro(this); | |
3206 body = body->semantic(sc); | |
3207 sc->tfOfTry = enclosinghandler; | |
3208 } | |
159 | 3209 return this; |
3210 } | |
3211 | |
3212 int SynchronizedStatement::hasBreak() | |
3213 { | |
3214 return FALSE; //TRUE; | |
3215 } | |
3216 | |
3217 int SynchronizedStatement::hasContinue() | |
3218 { | |
3219 return FALSE; //TRUE; | |
3220 } | |
3221 | |
3222 int SynchronizedStatement::usesEH() | |
3223 { | |
3224 return TRUE; | |
3225 } | |
3226 | |
336 | 3227 int SynchronizedStatement::blockExit() |
3228 { | |
3229 return body ? body->blockExit() : BEfallthru; | |
3230 } | |
3231 | |
159 | 3232 void SynchronizedStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
3233 { | |
3234 buf->writestring("synchronized"); | |
3235 if (exp) | |
3236 { buf->writebyte('('); | |
3237 exp->toCBuffer(buf, hgs); | |
3238 buf->writebyte(')'); | |
3239 } | |
3240 if (body) | |
3241 { | |
3242 buf->writebyte(' '); | |
3243 body->toCBuffer(buf, hgs); | |
3244 } | |
3245 } | |
3246 | |
3247 /******************************** WithStatement ***************************/ | |
3248 | |
3249 WithStatement::WithStatement(Loc loc, Expression *exp, Statement *body) | |
3250 : Statement(loc) | |
3251 { | |
3252 this->exp = exp; | |
3253 this->body = body; | |
3254 wthis = NULL; | |
3255 } | |
3256 | |
3257 Statement *WithStatement::syntaxCopy() | |
3258 { | |
3259 WithStatement *s = new WithStatement(loc, exp->syntaxCopy(), body ? body->syntaxCopy() : NULL); | |
3260 return s; | |
3261 } | |
3262 | |
3263 Statement *WithStatement::semantic(Scope *sc) | |
3264 { ScopeDsymbol *sym; | |
3265 Initializer *init; | |
3266 | |
3267 //printf("WithStatement::semantic()\n"); | |
3268 exp = exp->semantic(sc); | |
3269 exp = resolveProperties(sc, exp); | |
3270 if (exp->op == TOKimport) | |
3271 { ScopeExp *es = (ScopeExp *)exp; | |
3272 | |
3273 sym = es->sds; | |
3274 } | |
3275 else if (exp->op == TOKtype) | |
3276 { TypeExp *es = (TypeExp *)exp; | |
3277 | |
3278 sym = es->type->toDsymbol(sc)->isScopeDsymbol(); | |
3279 if (!sym) | |
3280 { error("%s has no members", es->toChars()); | |
3281 body = body->semantic(sc); | |
3282 return this; | |
3283 } | |
3284 } | |
3285 else | |
3286 { Type *t = exp->type; | |
3287 | |
3288 assert(t); | |
3289 t = t->toBasetype(); | |
3290 if (t->isClassHandle()) | |
3291 { | |
3292 init = new ExpInitializer(loc, exp); | |
3293 wthis = new VarDeclaration(loc, exp->type, Id::withSym, init); | |
3294 wthis->semantic(sc); | |
3295 | |
3296 sym = new WithScopeSymbol(this); | |
3297 sym->parent = sc->scopesym; | |
3298 } | |
3299 else if (t->ty == Tstruct) | |
3300 { | |
3301 Expression *e = exp->addressOf(sc); | |
3302 init = new ExpInitializer(loc, e); | |
3303 wthis = new VarDeclaration(loc, e->type, Id::withSym, init); | |
3304 wthis->semantic(sc); | |
3305 sym = new WithScopeSymbol(this); | |
3306 sym->parent = sc->scopesym; | |
3307 } | |
3308 else | |
3309 { error("with expressions must be class objects, not '%s'", exp->type->toChars()); | |
3310 return NULL; | |
3311 } | |
3312 } | |
3313 sc = sc->push(sym); | |
3314 | |
3315 if (body) | |
3316 body = body->semantic(sc); | |
3317 | |
3318 sc->pop(); | |
3319 | |
3320 return this; | |
3321 } | |
3322 | |
3323 void WithStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
3324 { | |
3325 buf->writestring("with ("); | |
3326 exp->toCBuffer(buf, hgs); | |
3327 buf->writestring(")\n"); | |
3328 if (body) | |
3329 body->toCBuffer(buf, hgs); | |
3330 } | |
3331 | |
3332 int WithStatement::usesEH() | |
3333 { | |
3334 return body ? body->usesEH() : 0; | |
3335 } | |
3336 | |
336 | 3337 int WithStatement::blockExit() |
3338 { | |
3339 int result = BEnone; | |
3340 if (exp->canThrow()) | |
3341 result = BEthrow; | |
3342 if (body) | |
3343 result |= body->blockExit(); | |
3344 else | |
3345 result |= BEfallthru; | |
3346 return result; | |
3347 } | |
3348 | |
159 | 3349 /******************************** TryCatchStatement ***************************/ |
3350 | |
3351 TryCatchStatement::TryCatchStatement(Loc loc, Statement *body, Array *catches) | |
3352 : Statement(loc) | |
3353 { | |
3354 this->body = body; | |
3355 this->catches = catches; | |
3356 } | |
3357 | |
3358 Statement *TryCatchStatement::syntaxCopy() | |
3359 { | |
3360 Array *a = new Array(); | |
3361 a->setDim(catches->dim); | |
3362 for (int i = 0; i < a->dim; i++) | |
3363 { Catch *c; | |
3364 | |
3365 c = (Catch *)catches->data[i]; | |
3366 c = c->syntaxCopy(); | |
3367 a->data[i] = c; | |
3368 } | |
3369 TryCatchStatement *s = new TryCatchStatement(loc, body->syntaxCopy(), a); | |
3370 return s; | |
3371 } | |
3372 | |
3373 Statement *TryCatchStatement::semantic(Scope *sc) | |
3374 { | |
3375 body = body->semanticScope(sc, NULL /*this*/, NULL); | |
3376 | |
336 | 3377 /* Even if body is NULL, still do semantic analysis on catches |
3378 */ | |
3379 for (size_t i = 0; i < catches->dim; i++) | |
3380 { Catch *c = (Catch *)catches->data[i]; | |
159 | 3381 c->semantic(sc); |
3382 | |
3383 // Determine if current catch 'hides' any previous catches | |
336 | 3384 for (size_t j = 0; j < i; j++) |
159 | 3385 { Catch *cj = (Catch *)catches->data[j]; |
3386 char *si = c->loc.toChars(); | |
3387 char *sj = cj->loc.toChars(); | |
3388 | |
3389 if (c->type->toBasetype()->implicitConvTo(cj->type->toBasetype())) | |
3390 error("catch at %s hides catch at %s", sj, si); | |
3391 } | |
3392 } | |
3393 return this; | |
3394 } | |
3395 | |
3396 int TryCatchStatement::hasBreak() | |
3397 { | |
3398 return FALSE; //TRUE; | |
3399 } | |
3400 | |
3401 int TryCatchStatement::usesEH() | |
3402 { | |
3403 return TRUE; | |
3404 } | |
3405 | |
336 | 3406 int TryCatchStatement::blockExit() |
3407 { int result; | |
3408 | |
3409 assert(body); | |
3410 result = body->blockExit(); | |
3411 | |
3412 for (size_t i = 0; i < catches->dim; i++) | |
3413 { | |
3414 Catch *c = (Catch *)catches->data[i]; | |
3415 result |= c->blockExit(); | |
3416 } | |
3417 return result; | |
3418 } | |
3419 | |
159 | 3420 void TryCatchStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
3421 { | |
3422 buf->writestring("try"); | |
3423 buf->writenl(); | |
3424 if (body) | |
3425 body->toCBuffer(buf, hgs); | |
336 | 3426 for (size_t i = 0; i < catches->dim; i++) |
159 | 3427 { |
3428 Catch *c = (Catch *)catches->data[i]; | |
3429 c->toCBuffer(buf, hgs); | |
3430 } | |
3431 } | |
3432 | |
3433 /******************************** Catch ***************************/ | |
3434 | |
3435 Catch::Catch(Loc loc, Type *t, Identifier *id, Statement *handler) | |
3436 { | |
3437 //printf("Catch(%s, loc = %s)\n", id->toChars(), loc.toChars()); | |
3438 this->loc = loc; | |
3439 this->type = t; | |
3440 this->ident = id; | |
3441 this->handler = handler; | |
3442 var = NULL; | |
3443 } | |
3444 | |
3445 Catch *Catch::syntaxCopy() | |
3446 { | |
3447 Catch *c = new Catch(loc, | |
3448 (type ? type->syntaxCopy() : NULL), | |
3449 ident, | |
3450 (handler ? handler->syntaxCopy() : NULL)); | |
3451 return c; | |
3452 } | |
3453 | |
3454 void Catch::semantic(Scope *sc) | |
3455 { ScopeDsymbol *sym; | |
3456 | |
3457 //printf("Catch::semantic(%s)\n", ident->toChars()); | |
3458 | |
3459 #ifndef IN_GCC | |
3460 if (sc->tf) | |
3461 { | |
3462 /* This is because the _d_local_unwind() gets the stack munged | |
3463 * up on this. The workaround is to place any try-catches into | |
3464 * a separate function, and call that. | |
3465 * To fix, have the compiler automatically convert the finally | |
3466 * body into a nested function. | |
3467 */ | |
3468 error(loc, "cannot put catch statement inside finally block"); | |
3469 } | |
3470 #endif | |
3471 | |
3472 sym = new ScopeDsymbol(); | |
3473 sym->parent = sc->scopesym; | |
3474 sc = sc->push(sym); | |
3475 | |
3476 if (!type) | |
3477 type = new TypeIdentifier(0, Id::Object); | |
3478 type = type->semantic(loc, sc); | |
3479 if (!type->toBasetype()->isClassHandle()) | |
3480 error("can only catch class objects, not '%s'", type->toChars()); | |
3481 else if (ident) | |
3482 { | |
3483 var = new VarDeclaration(loc, type, ident, NULL); | |
3484 var->parent = sc->parent; | |
3485 sc->insert(var); | |
3486 } | |
3487 handler = handler->semantic(sc); | |
3488 | |
3489 sc->pop(); | |
3490 } | |
3491 | |
336 | 3492 int Catch::blockExit() |
3493 { | |
3494 return handler ? handler->blockExit() : BEfallthru; | |
3495 } | |
3496 | |
159 | 3497 void Catch::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
3498 { | |
3499 buf->writestring("catch"); | |
3500 if (type) | |
3501 { buf->writebyte('('); | |
3502 type->toCBuffer(buf, ident, hgs); | |
3503 buf->writebyte(')'); | |
3504 } | |
3505 buf->writenl(); | |
3506 buf->writebyte('{'); | |
3507 buf->writenl(); | |
336 | 3508 if (handler) |
3509 handler->toCBuffer(buf, hgs); | |
159 | 3510 buf->writebyte('}'); |
3511 buf->writenl(); | |
3512 } | |
3513 | |
3514 /****************************** TryFinallyStatement ***************************/ | |
3515 | |
3516 TryFinallyStatement::TryFinallyStatement(Loc loc, Statement *body, Statement *finalbody) | |
3517 : Statement(loc) | |
3518 { | |
3519 this->body = body; | |
3520 this->finalbody = finalbody; | |
309 | 3521 this->enclosinghandler = NULL; |
159 | 3522 } |
3523 | |
3524 Statement *TryFinallyStatement::syntaxCopy() | |
3525 { | |
3526 TryFinallyStatement *s = new TryFinallyStatement(loc, | |
3527 body->syntaxCopy(), finalbody->syntaxCopy()); | |
3528 return s; | |
3529 } | |
3530 | |
3531 Statement *TryFinallyStatement::semantic(Scope *sc) | |
3532 { | |
3533 //printf("TryFinallyStatement::semantic()\n"); | |
3534 | |
309 | 3535 enclosinghandler = sc->tfOfTry; |
3536 sc->tfOfTry = new EnclosingTryFinally(this); | |
159 | 3537 body = body->semantic(sc); |
309 | 3538 sc->tfOfTry = enclosinghandler; |
159 | 3539 |
3540 sc = sc->push(); | |
3541 sc->tf = this; | |
3542 sc->sbreak = NULL; | |
3543 sc->scontinue = NULL; // no break or continue out of finally block | |
3544 finalbody = finalbody->semantic(sc); | |
3545 sc->pop(); | |
3546 return this; | |
3547 } | |
3548 | |
3549 void TryFinallyStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
3550 { | |
3551 buf->printf("try\n{\n"); | |
3552 body->toCBuffer(buf, hgs); | |
3553 buf->printf("}\nfinally\n{\n"); | |
3554 finalbody->toCBuffer(buf, hgs); | |
3555 buf->writeByte('}'); | |
3556 buf->writenl(); | |
3557 } | |
3558 | |
3559 int TryFinallyStatement::hasBreak() | |
3560 { | |
3561 return FALSE; //TRUE; | |
3562 } | |
3563 | |
3564 int TryFinallyStatement::hasContinue() | |
3565 { | |
3566 return FALSE; //TRUE; | |
3567 } | |
3568 | |
3569 int TryFinallyStatement::usesEH() | |
3570 { | |
3571 return TRUE; | |
3572 } | |
3573 | |
336 | 3574 int TryFinallyStatement::blockExit() |
3575 { | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3576 if (body) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3577 return body->blockExit(); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3578 return BEfallthru; |
336 | 3579 } |
3580 | |
159 | 3581 |
3582 /****************************** OnScopeStatement ***************************/ | |
3583 | |
3584 OnScopeStatement::OnScopeStatement(Loc loc, TOK tok, Statement *statement) | |
3585 : Statement(loc) | |
3586 { | |
3587 this->tok = tok; | |
3588 this->statement = statement; | |
3589 } | |
3590 | |
3591 Statement *OnScopeStatement::syntaxCopy() | |
3592 { | |
3593 OnScopeStatement *s = new OnScopeStatement(loc, | |
3594 tok, statement->syntaxCopy()); | |
3595 return s; | |
3596 } | |
3597 | |
3598 Statement *OnScopeStatement::semantic(Scope *sc) | |
3599 { | |
3600 /* semantic is called on results of scopeCode() */ | |
3601 return this; | |
3602 } | |
3603 | |
336 | 3604 int OnScopeStatement::blockExit() |
3605 { // At this point, this statement is just an empty placeholder | |
3606 return BEfallthru; | |
3607 } | |
3608 | |
159 | 3609 void OnScopeStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
3610 { | |
3611 buf->writestring(Token::toChars(tok)); | |
3612 buf->writebyte(' '); | |
3613 statement->toCBuffer(buf, hgs); | |
3614 } | |
3615 | |
3616 int OnScopeStatement::usesEH() | |
3617 { | |
3618 return (tok != TOKon_scope_success); | |
3619 } | |
3620 | |
3621 void OnScopeStatement::scopeCode(Statement **sentry, Statement **sexception, Statement **sfinally) | |
3622 { | |
3623 //printf("OnScopeStatement::scopeCode()\n"); | |
3624 //print(); | |
3625 *sentry = NULL; | |
3626 *sexception = NULL; | |
3627 *sfinally = NULL; | |
3628 switch (tok) | |
3629 { | |
3630 case TOKon_scope_exit: | |
3631 *sfinally = statement; | |
3632 break; | |
3633 | |
3634 case TOKon_scope_failure: | |
3635 *sexception = statement; | |
3636 break; | |
3637 | |
3638 case TOKon_scope_success: | |
3639 { | |
3640 /* Create: | |
3641 * sentry: int x = 0; | |
3642 * sexception: x = 1; | |
3643 * sfinally: if (!x) statement; | |
3644 */ | |
846
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
3645 Identifier *id = Lexer::uniqueId("__os"); |
159 | 3646 |
3647 ExpInitializer *ie = new ExpInitializer(loc, new IntegerExp(0)); | |
3648 VarDeclaration *v = new VarDeclaration(loc, Type::tint32, id, ie); | |
3649 *sentry = new DeclarationStatement(loc, v); | |
3650 | |
3651 Expression *e = new IntegerExp(1); | |
3652 e = new AssignExp(0, new VarExp(0, v), e); | |
3653 *sexception = new ExpStatement(0, e); | |
3654 | |
3655 e = new VarExp(0, v); | |
3656 e = new NotExp(0, e); | |
3657 *sfinally = new IfStatement(0, NULL, e, statement, NULL); | |
3658 | |
3659 break; | |
3660 } | |
3661 | |
3662 default: | |
3663 assert(0); | |
3664 } | |
3665 } | |
3666 | |
3667 /******************************** ThrowStatement ***************************/ | |
3668 | |
3669 ThrowStatement::ThrowStatement(Loc loc, Expression *exp) | |
3670 : Statement(loc) | |
3671 { | |
3672 this->exp = exp; | |
3673 } | |
3674 | |
3675 Statement *ThrowStatement::syntaxCopy() | |
3676 { | |
3677 ThrowStatement *s = new ThrowStatement(loc, exp->syntaxCopy()); | |
3678 return s; | |
3679 } | |
3680 | |
3681 Statement *ThrowStatement::semantic(Scope *sc) | |
3682 { | |
3683 //printf("ThrowStatement::semantic()\n"); | |
3684 | |
3685 FuncDeclaration *fd = sc->parent->isFuncDeclaration(); | |
3686 fd->hasReturnExp |= 2; | |
3687 | |
3688 if (sc->incontract) | |
3689 error("Throw statements cannot be in contracts"); | |
3690 exp = exp->semantic(sc); | |
3691 exp = resolveProperties(sc, exp); | |
3692 if (!exp->type->toBasetype()->isClassHandle()) | |
3693 error("can only throw class objects, not type %s", exp->type->toChars()); | |
3694 return this; | |
3695 } | |
3696 | |
336 | 3697 int ThrowStatement::blockExit() |
3698 { | |
3699 return BEthrow; // obviously | |
3700 } | |
3701 | |
159 | 3702 void ThrowStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
3703 { | |
3704 buf->printf("throw "); | |
3705 exp->toCBuffer(buf, hgs); | |
3706 buf->writeByte(';'); | |
3707 buf->writenl(); | |
3708 } | |
3709 | |
3710 /******************************** VolatileStatement **************************/ | |
3711 | |
3712 VolatileStatement::VolatileStatement(Loc loc, Statement *statement) | |
3713 : Statement(loc) | |
3714 { | |
3715 this->statement = statement; | |
309 | 3716 this->enclosinghandler = NULL; |
159 | 3717 } |
3718 | |
3719 Statement *VolatileStatement::syntaxCopy() | |
3720 { | |
3721 VolatileStatement *s = new VolatileStatement(loc, | |
3722 statement ? statement->syntaxCopy() : NULL); | |
3723 return s; | |
3724 } | |
3725 | |
3726 Statement *VolatileStatement::semantic(Scope *sc) | |
3727 { | |
309 | 3728 if (statement) |
3729 { | |
3730 enclosinghandler = sc->tfOfTry; | |
3731 sc->tfOfTry = new EnclosingVolatile(this); | |
3732 statement = statement->semantic(sc); | |
3733 sc->tfOfTry = enclosinghandler; | |
3734 } | |
159 | 3735 return this; |
3736 } | |
3737 | |
3738 Statements *VolatileStatement::flatten(Scope *sc) | |
3739 { | |
3740 Statements *a; | |
3741 | |
3742 a = statement ? statement->flatten(sc) : NULL; | |
3743 if (a) | |
3744 { for (int i = 0; i < a->dim; i++) | |
3745 { Statement *s = (Statement *)a->data[i]; | |
3746 | |
3747 s = new VolatileStatement(loc, s); | |
3748 a->data[i] = s; | |
3749 } | |
3750 } | |
3751 | |
3752 return a; | |
3753 } | |
3754 | |
336 | 3755 int VolatileStatement::blockExit() |
3756 { | |
3757 return statement ? statement->blockExit() : BEfallthru; | |
3758 } | |
3759 | |
159 | 3760 void VolatileStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
3761 { | |
3762 buf->writestring("volatile"); | |
3763 if (statement) | |
3764 { if (statement->isScopeStatement()) | |
3765 buf->writenl(); | |
3766 else | |
3767 buf->writebyte(' '); | |
3768 statement->toCBuffer(buf, hgs); | |
3769 } | |
3770 } | |
3771 | |
3772 | |
3773 /******************************** GotoStatement ***************************/ | |
3774 | |
3775 GotoStatement::GotoStatement(Loc loc, Identifier *ident) | |
3776 : Statement(loc) | |
3777 { | |
3778 this->ident = ident; | |
3779 this->label = NULL; | |
3780 this->tf = NULL; | |
309 | 3781 this->enclosinghandler = NULL; |
159 | 3782 } |
3783 | |
3784 Statement *GotoStatement::syntaxCopy() | |
3785 { | |
3786 GotoStatement *s = new GotoStatement(loc, ident); | |
3787 return s; | |
3788 } | |
3789 | |
3790 Statement *GotoStatement::semantic(Scope *sc) | |
3791 { FuncDeclaration *fd = sc->parent->isFuncDeclaration(); | |
3792 | |
3793 //printf("GotoStatement::semantic()\n"); | |
3794 tf = sc->tf; | |
309 | 3795 enclosinghandler = sc->tfOfTry; |
159 | 3796 label = fd->searchLabel(ident); |
3797 if (!label->statement && sc->fes) | |
3798 { | |
3799 /* Either the goto label is forward referenced or it | |
3800 * is in the function that the enclosing foreach is in. | |
3801 * Can't know yet, so wrap the goto in a compound statement | |
3802 * so we can patch it later, and add it to a 'look at this later' | |
3803 * list. | |
3804 */ | |
3805 Statements *a = new Statements(); | |
3806 Statement *s; | |
3807 | |
3808 a->push(this); | |
3809 s = new CompoundStatement(loc, a); | |
3810 sc->fes->gotos.push(s); // 'look at this later' list | |
3811 return s; | |
3812 } | |
3813 if (label->statement && label->statement->tf != sc->tf) | |
3814 error("cannot goto in or out of finally block"); | |
3815 return this; | |
3816 } | |
3817 | |
336 | 3818 int GotoStatement::blockExit() |
3819 { | |
3820 //printf("GotoStatement::blockExit(%p)\n", this); | |
3821 return BEgoto; | |
3822 } | |
3823 | |
159 | 3824 void GotoStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
3825 { | |
3826 buf->writestring("goto "); | |
3827 buf->writestring(ident->toChars()); | |
3828 buf->writebyte(';'); | |
3829 buf->writenl(); | |
3830 } | |
3831 | |
3832 /******************************** LabelStatement ***************************/ | |
3833 | |
3834 LabelStatement::LabelStatement(Loc loc, Identifier *ident, Statement *statement) | |
3835 : Statement(loc) | |
3836 { | |
3837 this->ident = ident; | |
3838 this->statement = statement; | |
3839 this->tf = NULL; | |
309 | 3840 this->enclosinghandler = NULL; |
159 | 3841 this->lblock = NULL; |
3842 this->isReturnLabel = 0; | |
305
2b72433d5c8c
[svn r326] Fixed a bunch of issues with printf's that MinGW32 did not support.
lindquist
parents:
304
diff
changeset
|
3843 this->asmLabel = false; |
159 | 3844 } |
3845 | |
3846 Statement *LabelStatement::syntaxCopy() | |
3847 { | |
3848 LabelStatement *s = new LabelStatement(loc, ident, statement->syntaxCopy()); | |
3849 return s; | |
3850 } | |
3851 | |
3852 Statement *LabelStatement::semantic(Scope *sc) | |
3853 { LabelDsymbol *ls; | |
3854 FuncDeclaration *fd = sc->parent->isFuncDeclaration(); | |
3855 | |
3856 //printf("LabelStatement::semantic()\n"); | |
3857 ls = fd->searchLabel(ident); | |
3858 if (ls->statement) | |
3859 error("Label '%s' already defined", ls->toChars()); | |
3860 else | |
3861 ls->statement = this; | |
3862 tf = sc->tf; | |
309 | 3863 enclosinghandler = sc->tfOfTry; |
159 | 3864 sc = sc->push(); |
3865 sc->scopesym = sc->enclosing->scopesym; | |
3866 sc->callSuper |= CSXlabel; | |
3867 sc->slabel = this; | |
3868 if (statement) | |
3869 statement = statement->semantic(sc); | |
3870 sc->pop(); | |
305
2b72433d5c8c
[svn r326] Fixed a bunch of issues with printf's that MinGW32 did not support.
lindquist
parents:
304
diff
changeset
|
3871 |
664
eef8ac26c66c
Some missed LLVMDC -> LDC.
Christian Kamm <kamm incasoftware de>
parents:
510
diff
changeset
|
3872 // LDC put in labmap |
305
2b72433d5c8c
[svn r326] Fixed a bunch of issues with printf's that MinGW32 did not support.
lindquist
parents:
304
diff
changeset
|
3873 fd->labmap[ident->toChars()] = this; |
2b72433d5c8c
[svn r326] Fixed a bunch of issues with printf's that MinGW32 did not support.
lindquist
parents:
304
diff
changeset
|
3874 |
159 | 3875 return this; |
3876 } | |
3877 | |
3878 Statements *LabelStatement::flatten(Scope *sc) | |
3879 { | |
3880 Statements *a = NULL; | |
3881 | |
3882 if (statement) | |
3883 { | |
3884 a = statement->flatten(sc); | |
3885 if (a) | |
3886 { | |
3887 if (!a->dim) | |
3888 { | |
3889 a->push(new ExpStatement(loc, NULL)); | |
3890 } | |
3891 Statement *s = (Statement *)a->data[0]; | |
3892 | |
3893 s = new LabelStatement(loc, ident, s); | |
3894 a->data[0] = s; | |
3895 } | |
3896 } | |
3897 | |
3898 return a; | |
3899 } | |
3900 | |
3901 | |
3902 int LabelStatement::usesEH() | |
3903 { | |
3904 return statement ? statement->usesEH() : FALSE; | |
3905 } | |
3906 | |
336 | 3907 int LabelStatement::blockExit() |
3908 { | |
3909 //printf("LabelStatement::blockExit(%p)\n", this); | |
3910 return statement ? statement->blockExit() : BEfallthru; | |
3911 } | |
3912 | |
159 | 3913 int LabelStatement::comeFrom() |
3914 { | |
3915 //printf("LabelStatement::comeFrom()\n"); | |
3916 return TRUE; | |
3917 } | |
3918 | |
3919 void LabelStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
3920 { | |
3921 buf->writestring(ident->toChars()); | |
3922 buf->writebyte(':'); | |
3923 buf->writenl(); | |
3924 if (statement) | |
3925 statement->toCBuffer(buf, hgs); | |
3926 } | |
3927 | |
3928 | |
3929 /******************************** LabelDsymbol ***************************/ | |
3930 | |
3931 LabelDsymbol::LabelDsymbol(Identifier *ident) | |
3932 : Dsymbol(ident) | |
3933 { | |
3934 statement = NULL; | |
3935 } | |
3936 | |
3937 LabelDsymbol *LabelDsymbol::isLabel() // is this a LabelDsymbol()? | |
3938 { | |
3939 return this; | |
3940 } |