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