Mercurial > projects > ldc
annotate dmd/statement.c @ 380:2bf65352fb28
Mixed up argument types for aApply and aaApply.
author | Christian Kamm <kamm incasoftware de> |
---|---|
date | Wed, 23 Jul 2008 17:55:20 +0200 |
parents | d632801b15f0 |
children | 722f5e90c39c |
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 */ | |
378
d8234836b40f
Get rid of runTimeHack and instead add proper argument info to the frontend
Christian Kamm <kamm incasoftware de>
parents:
353
diff
changeset
|
1601 //LLVMDC: Build arguments. |
d8234836b40f
Get rid of runTimeHack and instead add proper argument info to the frontend
Christian Kamm <kamm incasoftware de>
parents:
353
diff
changeset
|
1602 Arguments* args = new Arguments; |
380
2bf65352fb28
Mixed up argument types for aApply and aaApply.
Christian Kamm <kamm incasoftware de>
parents:
379
diff
changeset
|
1603 args->push(new Argument(STCin, Type::topaque->pointerTo(), NULL, NULL)); |
378
d8234836b40f
Get rid of runTimeHack and instead add proper argument info to the frontend
Christian Kamm <kamm incasoftware de>
parents:
353
diff
changeset
|
1604 args->push(new Argument(STCin, Type::tsize_t, NULL, NULL)); |
d8234836b40f
Get rid of runTimeHack and instead add proper argument info to the frontend
Christian Kamm <kamm incasoftware de>
parents:
353
diff
changeset
|
1605 if (dim == 2) { |
d8234836b40f
Get rid of runTimeHack and instead add proper argument info to the frontend
Christian Kamm <kamm incasoftware de>
parents:
353
diff
changeset
|
1606 Arguments* dgargs = new Arguments; |
d8234836b40f
Get rid of runTimeHack and instead add proper argument info to the frontend
Christian Kamm <kamm incasoftware de>
parents:
353
diff
changeset
|
1607 dgargs->push(new Argument(STCin, Type::tvoidptr, NULL, NULL)); |
d8234836b40f
Get rid of runTimeHack and instead add proper argument info to the frontend
Christian Kamm <kamm incasoftware de>
parents:
353
diff
changeset
|
1608 dgargs->push(new Argument(STCin, Type::tvoidptr, NULL, NULL)); |
d8234836b40f
Get rid of runTimeHack and instead add proper argument info to the frontend
Christian Kamm <kamm incasoftware de>
parents:
353
diff
changeset
|
1609 TypeDelegate* dgty = new TypeDelegate(new TypeFunction(dgargs, Type::tindex, 0, LINKd)); |
d8234836b40f
Get rid of runTimeHack and instead add proper argument info to the frontend
Christian Kamm <kamm incasoftware de>
parents:
353
diff
changeset
|
1610 args->push(new Argument(STCin, dgty, NULL, NULL)); |
d8234836b40f
Get rid of runTimeHack and instead add proper argument info to the frontend
Christian Kamm <kamm incasoftware de>
parents:
353
diff
changeset
|
1611 fdapply = FuncDeclaration::genCfunc(args, Type::tindex, "_aaApply2"); |
d8234836b40f
Get rid of runTimeHack and instead add proper argument info to the frontend
Christian Kamm <kamm incasoftware de>
parents:
353
diff
changeset
|
1612 } else { |
d8234836b40f
Get rid of runTimeHack and instead add proper argument info to the frontend
Christian Kamm <kamm incasoftware de>
parents:
353
diff
changeset
|
1613 Arguments* dgargs = new Arguments; |
d8234836b40f
Get rid of runTimeHack and instead add proper argument info to the frontend
Christian Kamm <kamm incasoftware de>
parents:
353
diff
changeset
|
1614 dgargs->push(new Argument(STCin, Type::tvoidptr, NULL, NULL)); |
d8234836b40f
Get rid of runTimeHack and instead add proper argument info to the frontend
Christian Kamm <kamm incasoftware de>
parents:
353
diff
changeset
|
1615 TypeDelegate* dgty = new TypeDelegate(new TypeFunction(dgargs, Type::tindex, 0, LINKd)); |
d8234836b40f
Get rid of runTimeHack and instead add proper argument info to the frontend
Christian Kamm <kamm incasoftware de>
parents:
353
diff
changeset
|
1616 args->push(new Argument(STCin, dgty, NULL, NULL)); |
d8234836b40f
Get rid of runTimeHack and instead add proper argument info to the frontend
Christian Kamm <kamm incasoftware de>
parents:
353
diff
changeset
|
1617 fdapply = FuncDeclaration::genCfunc(args, Type::tindex, "_aaApply"); |
d8234836b40f
Get rid of runTimeHack and instead add proper argument info to the frontend
Christian Kamm <kamm incasoftware de>
parents:
353
diff
changeset
|
1618 } |
159 | 1619 ec = new VarExp(0, fdapply); |
1620 Expressions *exps = new Expressions(); | |
1621 exps->push(aggr); | |
1622 size_t keysize = taa->key->size(); | |
1623 keysize = (keysize + 3) & ~3; | |
1624 exps->push(new IntegerExp(0, keysize, Type::tsize_t)); | |
1625 exps->push(flde); | |
1626 e = new CallExp(loc, ec, exps); | |
1627 e->type = Type::tindex; // don't run semantic() on e | |
1628 } | |
1629 else if (tab->ty == Tarray || tab->ty == Tsarray) | |
1630 { | |
1631 /* Call: | |
1632 * _aApply(aggr, flde) | |
1633 */ | |
1634 static char fntab[9][3] = | |
1635 { "cc","cw","cd", | |
1636 "wc","cc","wd", | |
1637 "dc","dw","dd" | |
1638 }; | |
1639 char fdname[7+1+2+ sizeof(dim)*3 + 1]; | |
1640 int flag; | |
1641 | |
1642 switch (tn->ty) | |
1643 { | |
1644 case Tchar: flag = 0; break; | |
1645 case Twchar: flag = 3; break; | |
1646 case Tdchar: flag = 6; break; | |
1647 default: assert(0); | |
1648 } | |
1649 switch (tnv->ty) | |
1650 { | |
1651 case Tchar: flag += 0; break; | |
1652 case Twchar: flag += 1; break; | |
1653 case Tdchar: flag += 2; break; | |
1654 default: assert(0); | |
1655 } | |
1656 const char *r = (op == TOKforeach_reverse) ? "R" : ""; | |
1657 int j = sprintf(fdname, "_aApply%s%.*s%d", r, 2, fntab[flag], dim); | |
1658 assert(j < sizeof(fdname)); | |
378
d8234836b40f
Get rid of runTimeHack and instead add proper argument info to the frontend
Christian Kamm <kamm incasoftware de>
parents:
353
diff
changeset
|
1659 //LLVMDC: Build arguments. |
d8234836b40f
Get rid of runTimeHack and instead add proper argument info to the frontend
Christian Kamm <kamm incasoftware de>
parents:
353
diff
changeset
|
1660 Arguments* args = new Arguments; |
380
2bf65352fb28
Mixed up argument types for aApply and aaApply.
Christian Kamm <kamm incasoftware de>
parents:
379
diff
changeset
|
1661 args->push(new Argument(STCin, Type::topaque->arrayOf(), NULL, NULL)); |
378
d8234836b40f
Get rid of runTimeHack and instead add proper argument info to the frontend
Christian Kamm <kamm incasoftware de>
parents:
353
diff
changeset
|
1662 if (dim == 2) { |
d8234836b40f
Get rid of runTimeHack and instead add proper argument info to the frontend
Christian Kamm <kamm incasoftware de>
parents:
353
diff
changeset
|
1663 Arguments* dgargs = new Arguments; |
d8234836b40f
Get rid of runTimeHack and instead add proper argument info to the frontend
Christian Kamm <kamm incasoftware de>
parents:
353
diff
changeset
|
1664 dgargs->push(new Argument(STCin, Type::tvoidptr, NULL, NULL)); |
d8234836b40f
Get rid of runTimeHack and instead add proper argument info to the frontend
Christian Kamm <kamm incasoftware de>
parents:
353
diff
changeset
|
1665 dgargs->push(new Argument(STCin, Type::tvoidptr, NULL, NULL)); |
d8234836b40f
Get rid of runTimeHack and instead add proper argument info to the frontend
Christian Kamm <kamm incasoftware de>
parents:
353
diff
changeset
|
1666 TypeDelegate* dgty = new TypeDelegate(new TypeFunction(dgargs, Type::tindex, 0, LINKd)); |
d8234836b40f
Get rid of runTimeHack and instead add proper argument info to the frontend
Christian Kamm <kamm incasoftware de>
parents:
353
diff
changeset
|
1667 args->push(new Argument(STCin, dgty, NULL, NULL)); |
d8234836b40f
Get rid of runTimeHack and instead add proper argument info to the frontend
Christian Kamm <kamm incasoftware de>
parents:
353
diff
changeset
|
1668 fdapply = FuncDeclaration::genCfunc(args, Type::tindex, fdname); |
d8234836b40f
Get rid of runTimeHack and instead add proper argument info to the frontend
Christian Kamm <kamm incasoftware de>
parents:
353
diff
changeset
|
1669 } else { |
d8234836b40f
Get rid of runTimeHack and instead add proper argument info to the frontend
Christian Kamm <kamm incasoftware de>
parents:
353
diff
changeset
|
1670 Arguments* dgargs = new Arguments; |
d8234836b40f
Get rid of runTimeHack and instead add proper argument info to the frontend
Christian Kamm <kamm incasoftware de>
parents:
353
diff
changeset
|
1671 dgargs->push(new Argument(STCin, Type::tvoidptr, NULL, NULL)); |
d8234836b40f
Get rid of runTimeHack and instead add proper argument info to the frontend
Christian Kamm <kamm incasoftware de>
parents:
353
diff
changeset
|
1672 TypeDelegate* dgty = new TypeDelegate(new TypeFunction(dgargs, Type::tindex, 0, LINKd)); |
d8234836b40f
Get rid of runTimeHack and instead add proper argument info to the frontend
Christian Kamm <kamm incasoftware de>
parents:
353
diff
changeset
|
1673 args->push(new Argument(STCin, dgty, NULL, NULL)); |
d8234836b40f
Get rid of runTimeHack and instead add proper argument info to the frontend
Christian Kamm <kamm incasoftware de>
parents:
353
diff
changeset
|
1674 fdapply = FuncDeclaration::genCfunc(args, Type::tindex, fdname); |
d8234836b40f
Get rid of runTimeHack and instead add proper argument info to the frontend
Christian Kamm <kamm incasoftware de>
parents:
353
diff
changeset
|
1675 } |
159 | 1676 |
1677 ec = new VarExp(0, fdapply); | |
1678 Expressions *exps = new Expressions(); | |
1679 if (tab->ty == Tsarray) | |
1680 aggr = aggr->castTo(sc, tn->arrayOf()); | |
1681 exps->push(aggr); | |
1682 exps->push(flde); | |
1683 e = new CallExp(loc, ec, exps); | |
1684 e->type = Type::tindex; // don't run semantic() on e | |
1685 } | |
1686 else if (tab->ty == Tdelegate) | |
1687 { | |
1688 /* Call: | |
1689 * aggr(flde) | |
1690 */ | |
1691 Expressions *exps = new Expressions(); | |
1692 exps->push(flde); | |
1693 e = new CallExp(loc, aggr, exps); | |
1694 e = e->semantic(sc); | |
1695 if (e->type != Type::tint32) | |
1696 error("opApply() function for %s must return an int", tab->toChars()); | |
1697 } | |
1698 else | |
1699 { | |
1700 /* Call: | |
1701 * aggr.apply(flde) | |
1702 */ | |
1703 ec = new DotIdExp(loc, aggr, | |
1704 (op == TOKforeach_reverse) ? Id::applyReverse | |
1705 : Id::apply); | |
1706 Expressions *exps = new Expressions(); | |
1707 exps->push(flde); | |
1708 e = new CallExp(loc, ec, exps); | |
1709 e = e->semantic(sc); | |
1710 if (e->type != Type::tint32) | |
1711 error("opApply() function for %s must return an int", tab->toChars()); | |
1712 } | |
1713 | |
1714 if (!cases.dim) | |
1715 // Easy case, a clean exit from the loop | |
1716 s = new ExpStatement(loc, e); | |
1717 else | |
1718 { // Construct a switch statement around the return value | |
1719 // of the apply function. | |
1720 Statements *a = new Statements(); | |
1721 | |
1722 // default: break; takes care of cases 0 and 1 | |
1723 s = new BreakStatement(0, NULL); | |
1724 s = new DefaultStatement(0, s); | |
1725 a->push(s); | |
1726 | |
1727 // cases 2... | |
1728 for (int i = 0; i < cases.dim; i++) | |
1729 { | |
1730 s = (Statement *)cases.data[i]; | |
1731 s = new CaseStatement(0, new IntegerExp(i + 2), s); | |
1732 a->push(s); | |
1733 } | |
1734 | |
1735 s = new CompoundStatement(loc, a); | |
1736 s = new SwitchStatement(loc, e, s); | |
1737 s = s->semantic(sc); | |
1738 } | |
1739 break; | |
1740 } | |
1741 | |
1742 default: | |
1743 error("foreach: %s is not an aggregate type", aggr->type->toChars()); | |
1744 break; | |
1745 } | |
1746 sc->noctor--; | |
1747 sc->pop(); | |
1748 return s; | |
1749 } | |
1750 | |
1751 int ForeachStatement::hasBreak() | |
1752 { | |
1753 return TRUE; | |
1754 } | |
1755 | |
1756 int ForeachStatement::hasContinue() | |
1757 { | |
1758 return TRUE; | |
1759 } | |
1760 | |
1761 int ForeachStatement::usesEH() | |
1762 { | |
1763 return body->usesEH(); | |
1764 } | |
1765 | |
336 | 1766 int ForeachStatement::blockExit() |
1767 { int result = BEfallthru; | |
1768 | |
1769 if (aggr->canThrow()) | |
1770 result |= BEthrow; | |
1771 | |
1772 if (body) | |
1773 { | |
1774 result |= body->blockExit() & ~(BEbreak | BEcontinue); | |
1775 } | |
1776 return result; | |
1777 } | |
1778 | |
159 | 1779 int ForeachStatement::fallOffEnd() |
1780 { | |
1781 if (body) | |
1782 body->fallOffEnd(); | |
1783 return TRUE; | |
1784 } | |
1785 | |
1786 int ForeachStatement::comeFrom() | |
1787 { | |
1788 if (body) | |
1789 return body->comeFrom(); | |
1790 return FALSE; | |
1791 } | |
1792 | |
1793 void ForeachStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
1794 { | |
1795 buf->writestring(Token::toChars(op)); | |
1796 buf->writestring(" ("); | |
1797 for (int i = 0; i < arguments->dim; i++) | |
1798 { | |
1799 Argument *a = (Argument *)arguments->data[i]; | |
1800 if (i) | |
1801 buf->writestring(", "); | |
1802 if (a->storageClass & STCref) | |
1803 buf->writestring((global.params.Dversion == 1) | |
1804 ? (char*)"inout " : (char*)"ref "); | |
1805 if (a->type) | |
1806 a->type->toCBuffer(buf, a->ident, hgs); | |
1807 else | |
1808 buf->writestring(a->ident->toChars()); | |
1809 } | |
1810 buf->writestring("; "); | |
1811 aggr->toCBuffer(buf, hgs); | |
1812 buf->writebyte(')'); | |
1813 buf->writenl(); | |
1814 buf->writebyte('{'); | |
1815 buf->writenl(); | |
1816 if (body) | |
1817 body->toCBuffer(buf, hgs); | |
1818 buf->writebyte('}'); | |
1819 buf->writenl(); | |
1820 } | |
1821 | |
1822 /******************************** IfStatement ***************************/ | |
1823 | |
1824 IfStatement::IfStatement(Loc loc, Argument *arg, Expression *condition, Statement *ifbody, Statement *elsebody) | |
1825 : Statement(loc) | |
1826 { | |
1827 this->arg = arg; | |
1828 this->condition = condition; | |
1829 this->ifbody = ifbody; | |
1830 this->elsebody = elsebody; | |
1831 this->match = NULL; | |
1832 } | |
1833 | |
1834 Statement *IfStatement::syntaxCopy() | |
1835 { | |
1836 Statement *i = NULL; | |
1837 if (ifbody) | |
1838 i = ifbody->syntaxCopy(); | |
1839 | |
1840 Statement *e = NULL; | |
1841 if (elsebody) | |
1842 e = elsebody->syntaxCopy(); | |
1843 | |
1844 Argument *a = arg ? arg->syntaxCopy() : NULL; | |
1845 IfStatement *s = new IfStatement(loc, a, condition->syntaxCopy(), i, e); | |
1846 return s; | |
1847 } | |
1848 | |
1849 Statement *IfStatement::semantic(Scope *sc) | |
1850 { | |
1851 condition = condition->semantic(sc); | |
1852 condition = resolveProperties(sc, condition); | |
1853 condition = condition->checkToBoolean(); | |
1854 | |
1855 // If we can short-circuit evaluate the if statement, don't do the | |
1856 // semantic analysis of the skipped code. | |
1857 // This feature allows a limited form of conditional compilation. | |
1858 condition = condition->optimize(WANTflags); | |
1859 | |
1860 // Evaluate at runtime | |
1861 unsigned cs0 = sc->callSuper; | |
1862 unsigned cs1; | |
1863 | |
1864 Scope *scd; | |
1865 if (arg) | |
1866 { /* Declare arg, which we will set to be the | |
1867 * result of condition. | |
1868 */ | |
1869 ScopeDsymbol *sym = new ScopeDsymbol(); | |
1870 sym->parent = sc->scopesym; | |
1871 scd = sc->push(sym); | |
1872 | |
1873 Type *t = arg->type ? arg->type : condition->type; | |
1874 match = new VarDeclaration(loc, t, arg->ident, NULL); | |
1875 match->noauto = 1; | |
1876 match->semantic(scd); | |
1877 if (!scd->insert(match)) | |
1878 assert(0); | |
1879 match->parent = sc->func; | |
1880 | |
1881 /* Generate: | |
1882 * (arg = condition) | |
1883 */ | |
1884 VarExp *v = new VarExp(0, match); | |
1885 condition = new AssignExp(loc, v, condition); | |
1886 condition = condition->semantic(scd); | |
1887 } | |
1888 else | |
1889 scd = sc->push(); | |
1890 ifbody = ifbody->semantic(scd); | |
1891 scd->pop(); | |
1892 | |
1893 cs1 = sc->callSuper; | |
1894 sc->callSuper = cs0; | |
1895 if (elsebody) | |
1896 elsebody = elsebody->semanticScope(sc, NULL, NULL); | |
1897 sc->mergeCallSuper(loc, cs1); | |
1898 | |
1899 return this; | |
1900 } | |
1901 | |
1902 int IfStatement::usesEH() | |
1903 { | |
1904 return (ifbody && ifbody->usesEH()) || (elsebody && elsebody->usesEH()); | |
1905 } | |
1906 | |
336 | 1907 int IfStatement::blockExit() |
1908 { | |
1909 //printf("IfStatement::blockExit(%p)\n", this); | |
1910 | |
1911 int result = BEnone; | |
1912 if (condition->canThrow()) | |
1913 result |= BEthrow; | |
1914 if (condition->isBool(TRUE)) | |
1915 { | |
1916 if (ifbody) | |
1917 result |= ifbody->blockExit(); | |
1918 else | |
1919 result |= BEfallthru; | |
1920 } | |
1921 else if (condition->isBool(FALSE)) | |
1922 { | |
1923 if (elsebody) | |
1924 result |= elsebody->blockExit(); | |
1925 else | |
1926 result |= BEfallthru; | |
1927 } | |
1928 else | |
1929 { | |
1930 if (ifbody) | |
1931 result |= ifbody->blockExit(); | |
1932 else | |
1933 result |= BEfallthru; | |
1934 if (elsebody) | |
1935 result |= elsebody->blockExit(); | |
1936 else | |
1937 result |= BEfallthru; | |
1938 } | |
1939 //printf("IfStatement::blockExit(%p) = x%x\n", this, result); | |
1940 return result; | |
1941 } | |
1942 | |
159 | 1943 int IfStatement::fallOffEnd() |
1944 { | |
1945 if (!ifbody || ifbody->fallOffEnd() || | |
1946 !elsebody || elsebody->fallOffEnd()) | |
1947 return TRUE; | |
1948 return FALSE; | |
1949 } | |
1950 | |
1951 | |
1952 void IfStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
1953 { | |
1954 buf->writestring("if ("); | |
1955 if (arg) | |
1956 { | |
1957 if (arg->type) | |
1958 arg->type->toCBuffer(buf, arg->ident, hgs); | |
1959 else | |
1960 buf->writestring(arg->ident->toChars()); | |
1961 buf->writebyte(';'); | |
1962 } | |
1963 condition->toCBuffer(buf, hgs); | |
1964 buf->writebyte(')'); | |
1965 buf->writenl(); | |
1966 ifbody->toCBuffer(buf, hgs); | |
1967 if (elsebody) | |
1968 { buf->writestring("else"); | |
1969 buf->writenl(); | |
1970 elsebody->toCBuffer(buf, hgs); | |
1971 } | |
1972 } | |
1973 | |
1974 /******************************** ConditionalStatement ***************************/ | |
1975 | |
1976 ConditionalStatement::ConditionalStatement(Loc loc, Condition *condition, Statement *ifbody, Statement *elsebody) | |
1977 : Statement(loc) | |
1978 { | |
1979 this->condition = condition; | |
1980 this->ifbody = ifbody; | |
1981 this->elsebody = elsebody; | |
1982 } | |
1983 | |
1984 Statement *ConditionalStatement::syntaxCopy() | |
1985 { | |
1986 Statement *e = NULL; | |
1987 if (elsebody) | |
1988 e = elsebody->syntaxCopy(); | |
1989 ConditionalStatement *s = new ConditionalStatement(loc, | |
1990 condition->syntaxCopy(), ifbody->syntaxCopy(), e); | |
1991 return s; | |
1992 } | |
1993 | |
1994 Statement *ConditionalStatement::semantic(Scope *sc) | |
1995 { | |
1996 //printf("ConditionalStatement::semantic()\n"); | |
1997 | |
1998 // If we can short-circuit evaluate the if statement, don't do the | |
1999 // semantic analysis of the skipped code. | |
2000 // This feature allows a limited form of conditional compilation. | |
2001 if (condition->include(sc, NULL)) | |
2002 { | |
2003 ifbody = ifbody->semantic(sc); | |
2004 return ifbody; | |
2005 } | |
2006 else | |
2007 { | |
2008 if (elsebody) | |
2009 elsebody = elsebody->semantic(sc); | |
2010 return elsebody; | |
2011 } | |
2012 } | |
2013 | |
2014 Statements *ConditionalStatement::flatten(Scope *sc) | |
2015 { | |
2016 Statement *s; | |
2017 | |
2018 if (condition->include(sc, NULL)) | |
2019 s = ifbody; | |
2020 else | |
2021 s = elsebody; | |
2022 | |
2023 Statements *a = new Statements(); | |
2024 a->push(s); | |
2025 return a; | |
2026 } | |
2027 | |
2028 int ConditionalStatement::usesEH() | |
2029 { | |
2030 return (ifbody && ifbody->usesEH()) || (elsebody && elsebody->usesEH()); | |
2031 } | |
2032 | |
2033 void ConditionalStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2034 { | |
2035 condition->toCBuffer(buf, hgs); | |
2036 buf->writenl(); | |
336 | 2037 buf->writeByte('{'); |
2038 buf->writenl(); | |
159 | 2039 if (ifbody) |
2040 ifbody->toCBuffer(buf, hgs); | |
336 | 2041 buf->writeByte('}'); |
2042 buf->writenl(); | |
159 | 2043 if (elsebody) |
2044 { | |
2045 buf->writestring("else"); | |
2046 buf->writenl(); | |
336 | 2047 buf->writeByte('{'); |
2048 buf->writenl(); | |
159 | 2049 elsebody->toCBuffer(buf, hgs); |
336 | 2050 buf->writeByte('}'); |
2051 buf->writenl(); | |
159 | 2052 } |
2053 buf->writenl(); | |
2054 } | |
2055 | |
2056 | |
2057 /******************************** PragmaStatement ***************************/ | |
2058 | |
2059 PragmaStatement::PragmaStatement(Loc loc, Identifier *ident, Expressions *args, Statement *body) | |
2060 : Statement(loc) | |
2061 { | |
2062 this->ident = ident; | |
2063 this->args = args; | |
2064 this->body = body; | |
2065 } | |
2066 | |
2067 Statement *PragmaStatement::syntaxCopy() | |
2068 { | |
2069 Statement *b = NULL; | |
2070 if (body) | |
2071 b = body->syntaxCopy(); | |
2072 PragmaStatement *s = new PragmaStatement(loc, | |
2073 ident, Expression::arraySyntaxCopy(args), b); | |
2074 return s; | |
2075 } | |
2076 | |
2077 Statement *PragmaStatement::semantic(Scope *sc) | |
2078 { // Should be merged with PragmaDeclaration | |
2079 //printf("PragmaStatement::semantic() %s\n", toChars()); | |
2080 //printf("body = %p\n", body); | |
2081 if (ident == Id::msg) | |
2082 { | |
2083 if (args) | |
2084 { | |
2085 for (size_t i = 0; i < args->dim; i++) | |
2086 { | |
2087 Expression *e = (Expression *)args->data[i]; | |
2088 | |
2089 e = e->semantic(sc); | |
2090 e = e->optimize(WANTvalue | WANTinterpret); | |
2091 if (e->op == TOKstring) | |
2092 { | |
2093 StringExp *se = (StringExp *)e; | |
2094 fprintf(stdmsg, "%.*s", (int)se->len, se->string); | |
2095 } | |
2096 else | |
2097 error("string expected for message, not '%s'", e->toChars()); | |
2098 } | |
2099 fprintf(stdmsg, "\n"); | |
2100 } | |
2101 } | |
2102 else if (ident == Id::lib) | |
2103 { | |
2104 if (!args || args->dim != 1) | |
2105 error("string expected for library name"); | |
2106 else | |
2107 { | |
2108 Expression *e = (Expression *)args->data[0]; | |
2109 | |
2110 e = e->semantic(sc); | |
2111 e = e->optimize(WANTvalue | WANTinterpret); | |
2112 args->data[0] = (void *)e; | |
2113 if (e->op != TOKstring) | |
2114 error("string expected for library name, not '%s'", e->toChars()); | |
2115 else if (global.params.verbose) | |
2116 { | |
2117 StringExp *se = (StringExp *)e; | |
2118 char *name = (char *)mem.malloc(se->len + 1); | |
2119 memcpy(name, se->string, se->len); | |
2120 name[se->len] = 0; | |
2121 printf("library %s\n", name); | |
2122 mem.free(name); | |
2123 } | |
2124 } | |
2125 } | |
2126 else | |
2127 error("unrecognized pragma(%s)", ident->toChars()); | |
2128 | |
2129 if (body) | |
2130 { | |
2131 body = body->semantic(sc); | |
2132 } | |
2133 return body; | |
2134 } | |
2135 | |
2136 int PragmaStatement::usesEH() | |
2137 { | |
2138 return body && body->usesEH(); | |
2139 } | |
2140 | |
336 | 2141 int PragmaStatement::blockExit() |
2142 { | |
2143 int result = BEfallthru; | |
2144 #if 0 // currently, no code is generated for Pragma's, so it's just fallthru | |
2145 if (arrayExpressionCanThrow(args)) | |
2146 result |= BEthrow; | |
2147 if (body) | |
2148 result |= body->blockExit(); | |
2149 #endif | |
2150 return result; | |
2151 } | |
2152 | |
159 | 2153 int PragmaStatement::fallOffEnd() |
2154 { | |
2155 if (body) | |
2156 return body->fallOffEnd(); | |
2157 return TRUE; | |
2158 } | |
2159 | |
2160 void PragmaStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2161 { | |
2162 buf->writestring("pragma ("); | |
2163 buf->writestring(ident->toChars()); | |
2164 if (args && args->dim) | |
2165 { | |
2166 buf->writestring(", "); | |
2167 argsToCBuffer(buf, args, hgs); | |
2168 } | |
2169 buf->writeByte(')'); | |
2170 if (body) | |
2171 { | |
2172 buf->writenl(); | |
2173 buf->writeByte('{'); | |
2174 buf->writenl(); | |
2175 | |
2176 body->toCBuffer(buf, hgs); | |
2177 | |
2178 buf->writeByte('}'); | |
2179 buf->writenl(); | |
2180 } | |
2181 else | |
2182 { | |
2183 buf->writeByte(';'); | |
2184 buf->writenl(); | |
2185 } | |
2186 } | |
2187 | |
2188 | |
2189 /******************************** StaticAssertStatement ***************************/ | |
2190 | |
2191 StaticAssertStatement::StaticAssertStatement(StaticAssert *sa) | |
2192 : Statement(sa->loc) | |
2193 { | |
2194 this->sa = sa; | |
2195 } | |
2196 | |
2197 Statement *StaticAssertStatement::syntaxCopy() | |
2198 { | |
2199 StaticAssertStatement *s = new StaticAssertStatement((StaticAssert *)sa->syntaxCopy(NULL)); | |
2200 return s; | |
2201 } | |
2202 | |
2203 Statement *StaticAssertStatement::semantic(Scope *sc) | |
2204 { | |
2205 sa->semantic2(sc); | |
2206 return NULL; | |
2207 } | |
2208 | |
2209 void StaticAssertStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2210 { | |
2211 sa->toCBuffer(buf, hgs); | |
2212 } | |
2213 | |
2214 | |
2215 /******************************** SwitchStatement ***************************/ | |
2216 | |
2217 SwitchStatement::SwitchStatement(Loc loc, Expression *c, Statement *b) | |
2218 : Statement(loc) | |
2219 { | |
2220 condition = c; | |
2221 body = b; | |
2222 sdefault = NULL; | |
2223 cases = NULL; | |
2224 hasNoDefault = 0; | |
2225 // LLVMDC | |
309 | 2226 enclosinghandler = NULL; |
159 | 2227 } |
2228 | |
2229 Statement *SwitchStatement::syntaxCopy() | |
2230 { | |
2231 SwitchStatement *s = new SwitchStatement(loc, | |
2232 condition->syntaxCopy(), body->syntaxCopy()); | |
2233 return s; | |
2234 } | |
2235 | |
2236 Statement *SwitchStatement::semantic(Scope *sc) | |
2237 { | |
2238 //printf("SwitchStatement::semantic(%p)\n", this); | |
2239 assert(!cases); // ensure semantic() is only run once | |
2240 | |
309 | 2241 enclosinghandler = sc->tfOfTry; |
159 | 2242 |
2243 condition = condition->semantic(sc); | |
2244 condition = resolveProperties(sc, condition); | |
2245 if (condition->type->isString()) | |
2246 { | |
2247 // If it's not an array, cast it to one | |
2248 if (condition->type->ty != Tarray) | |
2249 { | |
2250 condition = condition->implicitCastTo(sc, condition->type->nextOf()->arrayOf()); | |
2251 } | |
2252 } | |
2253 else | |
2254 { condition = condition->integralPromotions(sc); | |
2255 condition->checkIntegral(); | |
2256 } | |
2257 condition = condition->optimize(WANTvalue); | |
2258 | |
2259 sc = sc->push(); | |
2260 sc->sbreak = this; | |
2261 sc->sw = this; | |
2262 | |
2263 cases = new Array(); | |
2264 sc->noctor++; // BUG: should use Scope::mergeCallSuper() for each case instead | |
2265 body = body->semantic(sc); | |
2266 sc->noctor--; | |
2267 | |
2268 // Resolve any goto case's with exp | |
2269 for (int i = 0; i < gotoCases.dim; i++) | |
2270 { | |
2271 GotoCaseStatement *gcs = (GotoCaseStatement *)gotoCases.data[i]; | |
2272 | |
2273 if (!gcs->exp) | |
2274 { | |
2275 gcs->error("no case statement following goto case;"); | |
2276 break; | |
2277 } | |
2278 | |
2279 for (Scope *scx = sc; scx; scx = scx->enclosing) | |
2280 { | |
2281 if (!scx->sw) | |
2282 continue; | |
2283 for (int j = 0; j < scx->sw->cases->dim; j++) | |
2284 { | |
2285 CaseStatement *cs = (CaseStatement *)scx->sw->cases->data[j]; | |
2286 | |
2287 if (cs->exp->equals(gcs->exp)) | |
2288 { | |
2289 gcs->cs = cs; | |
2290 goto Lfoundcase; | |
2291 } | |
2292 } | |
2293 } | |
2294 gcs->error("case %s not found", gcs->exp->toChars()); | |
2295 | |
2296 Lfoundcase: | |
2297 ; | |
2298 } | |
2299 | |
2300 if (!sc->sw->sdefault) | |
2301 { hasNoDefault = 1; | |
2302 | |
2303 if (global.params.warnings) | |
215
a58d8f4b84df
[svn r231] Changed: warnings are no longer treated as an error.
lindquist
parents:
159
diff
changeset
|
2304 { warning("%s: switch statement has no default", loc.toChars()); |
159 | 2305 } |
2306 | |
2307 // Generate runtime error if the default is hit | |
2308 Statements *a = new Statements(); | |
2309 CompoundStatement *cs; | |
2310 Statement *s; | |
2311 | |
2312 if (global.params.useSwitchError) | |
2313 s = new SwitchErrorStatement(loc); | |
2314 else | |
2315 { Expression *e = new HaltExp(loc); | |
2316 s = new ExpStatement(loc, e); | |
2317 } | |
2318 | |
2319 a->reserve(4); | |
2320 a->push(body); | |
2321 a->push(new BreakStatement(loc, NULL)); | |
2322 sc->sw->sdefault = new DefaultStatement(loc, s); | |
2323 a->push(sc->sw->sdefault); | |
2324 cs = new CompoundStatement(loc, a); | |
2325 body = cs; | |
2326 } | |
2327 | |
2328 sc->pop(); | |
2329 return this; | |
2330 } | |
2331 | |
2332 int SwitchStatement::hasBreak() | |
2333 { | |
2334 return TRUE; | |
2335 } | |
2336 | |
2337 int SwitchStatement::usesEH() | |
2338 { | |
2339 return body ? body->usesEH() : 0; | |
2340 } | |
2341 | |
336 | 2342 int SwitchStatement::blockExit() |
2343 { int result = BEnone; | |
2344 if (condition->canThrow()) | |
2345 result |= BEthrow; | |
2346 | |
2347 if (body) | |
2348 { result |= body->blockExit(); | |
2349 if (result & BEbreak) | |
2350 { result |= BEfallthru; | |
2351 result &= ~BEbreak; | |
2352 } | |
2353 } | |
2354 else | |
2355 result |= BEfallthru; | |
2356 | |
2357 return result; | |
2358 } | |
2359 | |
159 | 2360 int SwitchStatement::fallOffEnd() |
2361 { | |
2362 if (body) | |
2363 body->fallOffEnd(); | |
2364 return TRUE; // need to do this better | |
2365 } | |
2366 | |
2367 void SwitchStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2368 { | |
2369 buf->writestring("switch ("); | |
2370 condition->toCBuffer(buf, hgs); | |
2371 buf->writebyte(')'); | |
2372 buf->writenl(); | |
2373 if (body) | |
2374 { | |
2375 if (!body->isScopeStatement()) | |
2376 { buf->writebyte('{'); | |
2377 buf->writenl(); | |
2378 body->toCBuffer(buf, hgs); | |
2379 buf->writebyte('}'); | |
2380 buf->writenl(); | |
2381 } | |
2382 else | |
2383 { | |
2384 body->toCBuffer(buf, hgs); | |
2385 } | |
2386 } | |
2387 } | |
2388 | |
2389 /******************************** CaseStatement ***************************/ | |
2390 | |
2391 CaseStatement::CaseStatement(Loc loc, Expression *exp, Statement *s) | |
2392 : Statement(loc) | |
2393 { | |
2394 this->exp = exp; | |
2395 this->statement = s; | |
2396 cblock = NULL; | |
2397 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
|
2398 llvmIdx = NULL; |
159 | 2399 } |
2400 | |
2401 Statement *CaseStatement::syntaxCopy() | |
2402 { | |
2403 CaseStatement *s = new CaseStatement(loc, exp->syntaxCopy(), statement->syntaxCopy()); | |
2404 return s; | |
2405 } | |
2406 | |
2407 Statement *CaseStatement::semantic(Scope *sc) | |
2408 { SwitchStatement *sw = sc->sw; | |
2409 | |
2410 //printf("CaseStatement::semantic() %s\n", toChars()); | |
2411 exp = exp->semantic(sc); | |
2412 if (sw) | |
2413 { int i; | |
2414 | |
2415 exp = exp->implicitCastTo(sc, sw->condition->type); | |
2416 exp = exp->optimize(WANTvalue | WANTinterpret); | |
2417 if (exp->op != TOKstring && exp->op != TOKint64) | |
2418 { | |
2419 error("case must be a string or an integral constant, not %s", exp->toChars()); | |
2420 exp = new IntegerExp(0); | |
2421 } | |
2422 | |
2423 for (i = 0; i < sw->cases->dim; i++) | |
2424 { | |
2425 CaseStatement *cs = (CaseStatement *)sw->cases->data[i]; | |
2426 | |
2427 //printf("comparing '%s' with '%s'\n", exp->toChars(), cs->exp->toChars()); | |
2428 if (cs->exp->equals(exp)) | |
2429 { error("duplicate case %s in switch statement", exp->toChars()); | |
2430 break; | |
2431 } | |
2432 } | |
2433 | |
2434 sw->cases->push(this); | |
2435 | |
2436 // Resolve any goto case's with no exp to this case statement | |
2437 for (i = 0; i < sw->gotoCases.dim; i++) | |
2438 { | |
2439 GotoCaseStatement *gcs = (GotoCaseStatement *)sw->gotoCases.data[i]; | |
2440 | |
2441 if (!gcs->exp) | |
2442 { | |
2443 gcs->cs = this; | |
2444 sw->gotoCases.remove(i); // remove from array | |
2445 } | |
2446 } | |
2447 } | |
2448 else | |
2449 error("case not in switch statement"); | |
2450 statement = statement->semantic(sc); | |
2451 return this; | |
2452 } | |
2453 | |
2454 int CaseStatement::compare(Object *obj) | |
2455 { | |
2456 // Sort cases so we can do an efficient lookup | |
2457 CaseStatement *cs2 = (CaseStatement *)(obj); | |
2458 | |
2459 return exp->compare(cs2->exp); | |
2460 } | |
2461 | |
2462 int CaseStatement::usesEH() | |
2463 { | |
2464 return statement->usesEH(); | |
2465 } | |
2466 | |
336 | 2467 int CaseStatement::blockExit() |
2468 { | |
2469 return statement->blockExit(); | |
2470 } | |
2471 | |
159 | 2472 int CaseStatement::fallOffEnd() |
2473 { | |
2474 return statement->fallOffEnd(); | |
2475 } | |
2476 | |
2477 int CaseStatement::comeFrom() | |
2478 { | |
2479 return TRUE; | |
2480 } | |
2481 | |
2482 void CaseStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2483 { | |
2484 buf->writestring("case "); | |
2485 exp->toCBuffer(buf, hgs); | |
2486 buf->writebyte(':'); | |
2487 buf->writenl(); | |
2488 statement->toCBuffer(buf, hgs); | |
2489 } | |
2490 | |
2491 /******************************** DefaultStatement ***************************/ | |
2492 | |
2493 DefaultStatement::DefaultStatement(Loc loc, Statement *s) | |
2494 : Statement(loc) | |
2495 { | |
2496 this->statement = s; | |
2497 #if IN_GCC | |
2498 + cblock = NULL; | |
2499 #endif | |
2500 bodyBB = NULL; | |
2501 } | |
2502 | |
2503 Statement *DefaultStatement::syntaxCopy() | |
2504 { | |
2505 DefaultStatement *s = new DefaultStatement(loc, statement->syntaxCopy()); | |
2506 return s; | |
2507 } | |
2508 | |
2509 Statement *DefaultStatement::semantic(Scope *sc) | |
2510 { | |
336 | 2511 //printf("DefaultStatement::semantic()\n"); |
159 | 2512 if (sc->sw) |
2513 { | |
2514 if (sc->sw->sdefault) | |
2515 { | |
2516 error("switch statement already has a default"); | |
2517 } | |
2518 sc->sw->sdefault = this; | |
2519 } | |
2520 else | |
2521 error("default not in switch statement"); | |
2522 statement = statement->semantic(sc); | |
2523 return this; | |
2524 } | |
2525 | |
2526 int DefaultStatement::usesEH() | |
2527 { | |
2528 return statement->usesEH(); | |
2529 } | |
2530 | |
336 | 2531 int DefaultStatement::blockExit() |
2532 { | |
2533 return statement->blockExit(); | |
2534 } | |
2535 | |
159 | 2536 int DefaultStatement::fallOffEnd() |
2537 { | |
2538 return statement->fallOffEnd(); | |
2539 } | |
2540 | |
2541 int DefaultStatement::comeFrom() | |
2542 { | |
2543 return TRUE; | |
2544 } | |
2545 | |
2546 void DefaultStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2547 { | |
2548 buf->writestring("default:\n"); | |
2549 statement->toCBuffer(buf, hgs); | |
2550 } | |
2551 | |
2552 /******************************** GotoDefaultStatement ***************************/ | |
2553 | |
2554 GotoDefaultStatement::GotoDefaultStatement(Loc loc) | |
2555 : Statement(loc) | |
2556 { | |
2557 sw = NULL; | |
309 | 2558 enclosinghandler = NULL; |
159 | 2559 } |
2560 | |
2561 Statement *GotoDefaultStatement::syntaxCopy() | |
2562 { | |
2563 GotoDefaultStatement *s = new GotoDefaultStatement(loc); | |
2564 return s; | |
2565 } | |
2566 | |
2567 Statement *GotoDefaultStatement::semantic(Scope *sc) | |
2568 { | |
309 | 2569 enclosinghandler = sc->tfOfTry; |
159 | 2570 sw = sc->sw; |
2571 if (!sw) | |
2572 error("goto default not in switch statement"); | |
2573 return this; | |
2574 } | |
2575 | |
336 | 2576 int GotoDefaultStatement::blockExit() |
2577 { | |
2578 return BEgoto; | |
2579 } | |
2580 | |
159 | 2581 int GotoDefaultStatement::fallOffEnd() |
2582 { | |
2583 return FALSE; | |
2584 } | |
2585 | |
2586 void GotoDefaultStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2587 { | |
2588 buf->writestring("goto default;\n"); | |
2589 } | |
2590 | |
2591 /******************************** GotoCaseStatement ***************************/ | |
2592 | |
2593 GotoCaseStatement::GotoCaseStatement(Loc loc, Expression *exp) | |
2594 : Statement(loc) | |
2595 { | |
2596 cs = NULL; | |
2597 this->exp = exp; | |
309 | 2598 enclosinghandler = NULL; |
159 | 2599 sw = NULL; |
2600 } | |
2601 | |
2602 Statement *GotoCaseStatement::syntaxCopy() | |
2603 { | |
2604 Expression *e = exp ? exp->syntaxCopy() : NULL; | |
2605 GotoCaseStatement *s = new GotoCaseStatement(loc, e); | |
2606 return s; | |
2607 } | |
2608 | |
2609 Statement *GotoCaseStatement::semantic(Scope *sc) | |
2610 { | |
309 | 2611 enclosinghandler = sc->tfOfTry; |
159 | 2612 if (exp) |
2613 exp = exp->semantic(sc); | |
2614 | |
2615 if (!sc->sw) | |
2616 error("goto case not in switch statement"); | |
2617 else | |
2618 { | |
2619 sw = sc->sw; | |
2620 sc->sw->gotoCases.push(this); | |
2621 if (exp) | |
2622 { | |
2623 exp = exp->implicitCastTo(sc, sc->sw->condition->type); | |
2624 exp = exp->optimize(WANTvalue); | |
2625 } | |
2626 } | |
2627 return this; | |
2628 } | |
2629 | |
336 | 2630 int GotoCaseStatement::blockExit() |
2631 { | |
2632 return BEgoto; | |
2633 } | |
2634 | |
159 | 2635 int GotoCaseStatement::fallOffEnd() |
2636 { | |
2637 return FALSE; | |
2638 } | |
2639 | |
2640 void GotoCaseStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2641 { | |
2642 buf->writestring("goto case"); | |
2643 if (exp) | |
2644 { buf->writebyte(' '); | |
2645 exp->toCBuffer(buf, hgs); | |
2646 } | |
2647 buf->writebyte(';'); | |
2648 buf->writenl(); | |
2649 } | |
2650 | |
2651 /******************************** SwitchErrorStatement ***************************/ | |
2652 | |
2653 SwitchErrorStatement::SwitchErrorStatement(Loc loc) | |
2654 : Statement(loc) | |
2655 { | |
2656 } | |
2657 | |
336 | 2658 int SwitchErrorStatement::blockExit() |
2659 { | |
2660 return BEthrow; | |
2661 } | |
2662 | |
159 | 2663 int SwitchErrorStatement::fallOffEnd() |
2664 { | |
2665 return FALSE; | |
2666 } | |
2667 | |
2668 void SwitchErrorStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2669 { | |
2670 buf->writestring("SwitchErrorStatement::toCBuffer()"); | |
2671 buf->writenl(); | |
2672 } | |
2673 | |
2674 /******************************** ReturnStatement ***************************/ | |
2675 | |
2676 ReturnStatement::ReturnStatement(Loc loc, Expression *exp) | |
2677 : Statement(loc) | |
2678 { | |
2679 this->exp = exp; | |
309 | 2680 this->enclosinghandler = NULL; |
159 | 2681 } |
2682 | |
2683 Statement *ReturnStatement::syntaxCopy() | |
2684 { | |
2685 Expression *e = NULL; | |
2686 if (exp) | |
2687 e = exp->syntaxCopy(); | |
2688 ReturnStatement *s = new ReturnStatement(loc, e); | |
2689 return s; | |
2690 } | |
2691 | |
2692 Statement *ReturnStatement::semantic(Scope *sc) | |
2693 { | |
2694 //printf("ReturnStatement::semantic() %s\n", toChars()); | |
309 | 2695 this->enclosinghandler = sc->tfOfTry; |
159 | 2696 |
2697 FuncDeclaration *fd = sc->parent->isFuncDeclaration(); | |
2698 Scope *scx = sc; | |
2699 int implicit0 = 0; | |
2700 | |
2701 if (sc->fes) | |
2702 { | |
2703 // Find scope of function foreach is in | |
2704 for (; 1; scx = scx->enclosing) | |
2705 { | |
2706 assert(scx); | |
2707 if (scx->func != fd) | |
2708 { fd = scx->func; // fd is now function enclosing foreach | |
2709 break; | |
2710 } | |
2711 } | |
2712 } | |
2713 | |
2714 Type *tret = fd->type->nextOf(); | |
2715 if (fd->tintro) | |
336 | 2716 /* We'll be implicitly casting the return expression to tintro |
2717 */ | |
159 | 2718 tret = fd->tintro->nextOf(); |
2719 Type *tbret = NULL; | |
2720 | |
2721 if (tret) | |
2722 tbret = tret->toBasetype(); | |
2723 | |
2724 // main() returns 0, even if it returns void | |
2725 if (!exp && (!tbret || tbret->ty == Tvoid) && fd->isMain()) | |
2726 { implicit0 = 1; | |
2727 exp = new IntegerExp(0); | |
2728 } | |
2729 | |
2730 if (sc->incontract || scx->incontract) | |
2731 error("return statements cannot be in contracts"); | |
2732 if (sc->tf || scx->tf) | |
2733 error("return statements cannot be in finally, scope(exit) or scope(success) bodies"); | |
2734 | |
2735 if (fd->isCtorDeclaration()) | |
2736 { | |
2737 // Constructors implicitly do: | |
2738 // return this; | |
2739 if (exp && exp->op != TOKthis) | |
2740 error("cannot return expression from constructor"); | |
2741 exp = new ThisExp(0); | |
2742 } | |
2743 | |
2744 if (!exp) | |
2745 fd->nrvo_can = 0; | |
2746 | |
2747 if (exp) | |
2748 { | |
2749 fd->hasReturnExp |= 1; | |
2750 | |
2751 exp = exp->semantic(sc); | |
2752 exp = resolveProperties(sc, exp); | |
2753 exp = exp->optimize(WANTvalue); | |
2754 | |
2755 if (fd->nrvo_can && exp->op == TOKvar) | |
2756 { VarExp *ve = (VarExp *)exp; | |
2757 VarDeclaration *v = ve->var->isVarDeclaration(); | |
2758 | |
2759 if (!v || v->isOut() || v->isRef()) | |
2760 fd->nrvo_can = 0; | |
2761 else if (fd->nrvo_var == NULL) | |
2762 { if (!v->isDataseg() && !v->isParameter() && v->toParent2() == fd) | |
2763 fd->nrvo_var = v; | |
2764 else | |
2765 fd->nrvo_can = 0; | |
2766 } | |
2767 else if (fd->nrvo_var != v) | |
2768 fd->nrvo_can = 0; | |
2769 } | |
2770 else | |
2771 fd->nrvo_can = 0; | |
2772 | |
2773 if (fd->returnLabel && tbret->ty != Tvoid) | |
2774 { | |
2775 } | |
2776 else if (fd->inferRetType) | |
2777 { | |
2778 if (fd->type->nextOf()) | |
2779 { | |
2780 if (!exp->type->equals(fd->type->nextOf())) | |
2781 error("mismatched function return type inference of %s and %s", | |
2782 exp->type->toChars(), fd->type->nextOf()->toChars()); | |
2783 } | |
2784 else | |
2785 { | |
2786 fd->type->next = exp->type; | |
2787 fd->type = fd->type->semantic(loc, sc); | |
2788 if (!fd->tintro) | |
2789 { tret = fd->type->nextOf(); | |
2790 tbret = tret->toBasetype(); | |
2791 } | |
2792 } | |
2793 } | |
2794 else if (tbret->ty != Tvoid) | |
2795 { | |
2796 exp = exp->implicitCastTo(sc, tret); | |
2797 } | |
2798 } | |
2799 else if (fd->inferRetType) | |
2800 { | |
2801 if (fd->type->nextOf()) | |
2802 { | |
2803 if (fd->type->nextOf()->ty != Tvoid) | |
2804 error("mismatched function return type inference of void and %s", | |
2805 fd->type->nextOf()->toChars()); | |
2806 } | |
2807 else | |
2808 { | |
2809 fd->type->next = Type::tvoid; | |
2810 fd->type = fd->type->semantic(loc, sc); | |
2811 if (!fd->tintro) | |
2812 { tret = Type::tvoid; | |
2813 tbret = tret; | |
2814 } | |
2815 } | |
2816 } | |
2817 else if (tbret->ty != Tvoid) // if non-void return | |
2818 error("return expression expected"); | |
2819 | |
2820 if (sc->fes) | |
2821 { | |
2822 Statement *s; | |
2823 | |
2824 if (exp && !implicit0) | |
2825 { | |
2826 exp = exp->implicitCastTo(sc, tret); | |
2827 } | |
2828 if (!exp || exp->op == TOKint64 || exp->op == TOKfloat64 || | |
2829 exp->op == TOKimaginary80 || exp->op == TOKcomplex80 || | |
2830 exp->op == TOKthis || exp->op == TOKsuper || exp->op == TOKnull || | |
2831 exp->op == TOKstring) | |
2832 { | |
2833 sc->fes->cases.push(this); | |
2834 s = new ReturnStatement(0, new IntegerExp(sc->fes->cases.dim + 1)); | |
2835 } | |
2836 else if (fd->type->nextOf()->toBasetype() == Type::tvoid) | |
2837 { | |
2838 Statement *s1; | |
2839 Statement *s2; | |
2840 | |
2841 s = new ReturnStatement(0, NULL); | |
2842 sc->fes->cases.push(s); | |
2843 | |
2844 // Construct: { exp; return cases.dim + 1; } | |
2845 s1 = new ExpStatement(loc, exp); | |
2846 s2 = new ReturnStatement(0, new IntegerExp(sc->fes->cases.dim + 1)); | |
2847 s = new CompoundStatement(loc, s1, s2); | |
2848 } | |
2849 else | |
2850 { | |
2851 VarExp *v; | |
2852 Statement *s1; | |
2853 Statement *s2; | |
2854 | |
2855 // Construct: return vresult; | |
2856 if (!fd->vresult) | |
2857 { VarDeclaration *v; | |
2858 | |
2859 v = new VarDeclaration(loc, tret, Id::result, NULL); | |
2860 v->noauto = 1; | |
2861 v->semantic(scx); | |
2862 if (!scx->insert(v)) | |
2863 assert(0); | |
2864 v->parent = fd; | |
2865 fd->vresult = v; | |
2866 } | |
2867 | |
2868 v = new VarExp(0, fd->vresult); | |
2869 s = new ReturnStatement(0, v); | |
2870 sc->fes->cases.push(s); | |
2871 | |
2872 // Construct: { vresult = exp; return cases.dim + 1; } | |
2873 v = new VarExp(0, fd->vresult); | |
2874 exp = new AssignExp(loc, v, exp); | |
2875 exp = exp->semantic(sc); | |
2876 s1 = new ExpStatement(loc, exp); | |
2877 s2 = new ReturnStatement(0, new IntegerExp(sc->fes->cases.dim + 1)); | |
2878 s = new CompoundStatement(loc, s1, s2); | |
2879 } | |
2880 return s; | |
2881 } | |
2882 | |
2883 if (exp) | |
2884 { | |
2885 if (fd->returnLabel && tbret->ty != Tvoid) | |
2886 { | |
2887 assert(fd->vresult); | |
2888 VarExp *v = new VarExp(0, fd->vresult); | |
2889 | |
2890 exp = new AssignExp(loc, v, exp); | |
2891 exp = exp->semantic(sc); | |
2892 } | |
2893 //exp->dump(0); | |
2894 //exp->print(); | |
2895 exp->checkEscape(); | |
2896 } | |
2897 | |
2898 /* BUG: need to issue an error on: | |
2899 * this | |
2900 * { if (x) return; | |
2901 * super(); | |
2902 * } | |
2903 */ | |
2904 | |
2905 if (sc->callSuper & CSXany_ctor && | |
2906 !(sc->callSuper & (CSXthis_ctor | CSXsuper_ctor))) | |
2907 error("return without calling constructor"); | |
2908 | |
2909 sc->callSuper |= CSXreturn; | |
2910 | |
2911 // See if all returns are instead to be replaced with a goto returnLabel; | |
2912 if (fd->returnLabel) | |
2913 { | |
2914 GotoStatement *gs = new GotoStatement(loc, Id::returnLabel); | |
2915 | |
2916 gs->label = fd->returnLabel; | |
2917 if (exp) | |
2918 { Statement *s; | |
2919 | |
2920 s = new ExpStatement(0, exp); | |
2921 return new CompoundStatement(loc, s, gs); | |
2922 } | |
2923 return gs; | |
2924 } | |
2925 | |
2926 if (exp && tbret->ty == Tvoid && !fd->isMain()) | |
336 | 2927 { |
2928 /* Replace: | |
2929 * return exp; | |
2930 * with: | |
2931 * exp; return; | |
2932 */ | |
2933 Statement *s = new ExpStatement(loc, exp); | |
159 | 2934 loc = 0; |
2935 exp = NULL; | |
2936 return new CompoundStatement(loc, s, this); | |
2937 } | |
2938 | |
2939 return this; | |
2940 } | |
2941 | |
336 | 2942 int ReturnStatement::blockExit() |
2943 { int result = BEreturn; | |
2944 | |
2945 if (exp && exp->canThrow()) | |
2946 result |= BEthrow; | |
2947 return result; | |
2948 } | |
2949 | |
159 | 2950 int ReturnStatement::fallOffEnd() |
2951 { | |
2952 return FALSE; | |
2953 } | |
2954 | |
2955 void ReturnStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2956 { | |
2957 buf->printf("return "); | |
2958 if (exp) | |
2959 exp->toCBuffer(buf, hgs); | |
2960 buf->writeByte(';'); | |
2961 buf->writenl(); | |
2962 } | |
2963 | |
2964 /******************************** BreakStatement ***************************/ | |
2965 | |
2966 BreakStatement::BreakStatement(Loc loc, Identifier *ident) | |
2967 : Statement(loc) | |
2968 { | |
2969 this->ident = ident; | |
309 | 2970 this->enclosinghandler = NULL; |
159 | 2971 } |
2972 | |
2973 Statement *BreakStatement::syntaxCopy() | |
2974 { | |
2975 BreakStatement *s = new BreakStatement(loc, ident); | |
2976 return s; | |
2977 } | |
2978 | |
2979 Statement *BreakStatement::semantic(Scope *sc) | |
2980 { | |
336 | 2981 //printf("BreakStatement::semantic()\n"); |
309 | 2982 enclosinghandler = sc->tfOfTry; |
159 | 2983 // If: |
2984 // break Identifier; | |
2985 if (ident) | |
2986 { | |
2987 Scope *scx; | |
2988 FuncDeclaration *thisfunc = sc->func; | |
2989 | |
2990 for (scx = sc; scx; scx = scx->enclosing) | |
2991 { | |
2992 LabelStatement *ls; | |
2993 | |
2994 if (scx->func != thisfunc) // if in enclosing function | |
2995 { | |
2996 if (sc->fes) // if this is the body of a foreach | |
2997 { | |
2998 /* Post this statement to the fes, and replace | |
2999 * it with a return value that caller will put into | |
3000 * a switch. Caller will figure out where the break | |
3001 * label actually is. | |
3002 * Case numbers start with 2, not 0, as 0 is continue | |
3003 * and 1 is break. | |
3004 */ | |
3005 Statement *s; | |
3006 sc->fes->cases.push(this); | |
3007 s = new ReturnStatement(0, new IntegerExp(sc->fes->cases.dim + 1)); | |
3008 return s; | |
3009 } | |
3010 break; // can't break to it | |
3011 } | |
3012 | |
3013 ls = scx->slabel; | |
3014 if (ls && ls->ident == ident) | |
3015 { | |
3016 Statement *s = ls->statement; | |
3017 | |
3018 if (!s->hasBreak()) | |
3019 error("label '%s' has no break", ident->toChars()); | |
3020 if (ls->tf != sc->tf) | |
3021 error("cannot break out of finally block"); | |
3022 | |
3023 this->target = ls; | |
3024 return this; | |
3025 } | |
3026 } | |
3027 error("enclosing label '%s' for break not found", ident->toChars()); | |
3028 } | |
3029 else if (!sc->sbreak) | |
3030 { | |
3031 if (sc->fes) | |
3032 { Statement *s; | |
3033 | |
3034 // Replace break; with return 1; | |
3035 s = new ReturnStatement(0, new IntegerExp(1)); | |
3036 return s; | |
3037 } | |
3038 error("break is not inside a loop or switch"); | |
3039 } | |
3040 return this; | |
3041 } | |
3042 | |
336 | 3043 int BreakStatement::blockExit() |
3044 { | |
3045 //printf("BreakStatement::blockExit(%p) = x%x\n", this, ident ? BEgoto : BEbreak); | |
3046 return ident ? BEgoto : BEbreak; | |
3047 } | |
3048 | |
159 | 3049 int BreakStatement::fallOffEnd() |
3050 { | |
3051 return FALSE; | |
3052 } | |
3053 | |
3054 void BreakStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
3055 { | |
3056 buf->writestring("break"); | |
3057 if (ident) | |
3058 { buf->writebyte(' '); | |
3059 buf->writestring(ident->toChars()); | |
3060 } | |
3061 buf->writebyte(';'); | |
3062 buf->writenl(); | |
3063 } | |
3064 | |
3065 /******************************** ContinueStatement ***************************/ | |
3066 | |
3067 ContinueStatement::ContinueStatement(Loc loc, Identifier *ident) | |
3068 : Statement(loc) | |
3069 { | |
3070 this->ident = ident; | |
309 | 3071 this->enclosinghandler = NULL; |
159 | 3072 } |
3073 | |
3074 Statement *ContinueStatement::syntaxCopy() | |
3075 { | |
3076 ContinueStatement *s = new ContinueStatement(loc, ident); | |
3077 return s; | |
3078 } | |
3079 | |
3080 Statement *ContinueStatement::semantic(Scope *sc) | |
3081 { | |
309 | 3082 enclosinghandler = sc->tfOfTry; |
159 | 3083 //printf("ContinueStatement::semantic() %p\n", this); |
3084 if (ident) | |
3085 { | |
3086 Scope *scx; | |
3087 FuncDeclaration *thisfunc = sc->func; | |
3088 | |
3089 for (scx = sc; scx; scx = scx->enclosing) | |
3090 { | |
3091 LabelStatement *ls; | |
3092 | |
3093 if (scx->func != thisfunc) // if in enclosing function | |
3094 { | |
3095 if (sc->fes) // if this is the body of a foreach | |
3096 { | |
3097 for (; scx; scx = scx->enclosing) | |
3098 { | |
3099 ls = scx->slabel; | |
3100 if (ls && ls->ident == ident && ls->statement == sc->fes) | |
3101 { | |
3102 // Replace continue ident; with return 0; | |
3103 return new ReturnStatement(0, new IntegerExp(0)); | |
3104 } | |
3105 } | |
3106 | |
3107 /* Post this statement to the fes, and replace | |
3108 * it with a return value that caller will put into | |
3109 * a switch. Caller will figure out where the break | |
3110 * label actually is. | |
3111 * Case numbers start with 2, not 0, as 0 is continue | |
3112 * and 1 is break. | |
3113 */ | |
3114 Statement *s; | |
3115 sc->fes->cases.push(this); | |
3116 s = new ReturnStatement(0, new IntegerExp(sc->fes->cases.dim + 1)); | |
3117 return s; | |
3118 } | |
3119 break; // can't continue to it | |
3120 } | |
3121 | |
3122 ls = scx->slabel; | |
3123 if (ls && ls->ident == ident) | |
3124 { | |
3125 Statement *s = ls->statement; | |
3126 | |
3127 if (!s->hasContinue()) | |
3128 error("label '%s' has no continue", ident->toChars()); | |
3129 if (ls->tf != sc->tf) | |
3130 error("cannot continue out of finally block"); | |
3131 | |
3132 this->target = ls; | |
3133 return this; | |
3134 } | |
3135 } | |
3136 error("enclosing label '%s' for continue not found", ident->toChars()); | |
3137 } | |
3138 else if (!sc->scontinue) | |
3139 { | |
3140 if (sc->fes) | |
3141 { Statement *s; | |
3142 | |
3143 // Replace continue; with return 0; | |
3144 s = new ReturnStatement(0, new IntegerExp(0)); | |
3145 return s; | |
3146 } | |
3147 error("continue is not inside a loop"); | |
3148 } | |
3149 return this; | |
3150 } | |
3151 | |
336 | 3152 int ContinueStatement::blockExit() |
3153 { | |
3154 return ident ? BEgoto : BEcontinue; | |
3155 } | |
3156 | |
159 | 3157 int ContinueStatement::fallOffEnd() |
3158 { | |
3159 return FALSE; | |
3160 } | |
3161 | |
3162 void ContinueStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
3163 { | |
3164 buf->writestring("continue"); | |
3165 if (ident) | |
3166 { buf->writebyte(' '); | |
3167 buf->writestring(ident->toChars()); | |
3168 } | |
3169 buf->writebyte(';'); | |
3170 buf->writenl(); | |
3171 } | |
3172 | |
3173 /******************************** SynchronizedStatement ***************************/ | |
3174 | |
3175 SynchronizedStatement::SynchronizedStatement(Loc loc, Expression *exp, Statement *body) | |
3176 : Statement(loc) | |
3177 { | |
3178 this->exp = exp; | |
3179 this->body = body; | |
3180 this->esync = NULL; | |
310
d20cf0dec9c1
[svn r331] Fixed, forgot to default initialize the SynchronizedStatement enclosinghandler.
lindquist
parents:
309
diff
changeset
|
3181 this->enclosinghandler = NULL; |
309 | 3182 // LLVMDC |
3183 this->llsync = NULL; | |
159 | 3184 } |
3185 | |
3186 SynchronizedStatement::SynchronizedStatement(Loc loc, elem *esync, Statement *body) | |
3187 : Statement(loc) | |
3188 { | |
3189 this->exp = NULL; | |
3190 this->body = body; | |
3191 this->esync = esync; | |
310
d20cf0dec9c1
[svn r331] Fixed, forgot to default initialize the SynchronizedStatement enclosinghandler.
lindquist
parents:
309
diff
changeset
|
3192 this->enclosinghandler = NULL; |
309 | 3193 // LLVMDC |
3194 this->llsync = NULL; | |
159 | 3195 } |
3196 | |
3197 Statement *SynchronizedStatement::syntaxCopy() | |
3198 { | |
3199 Expression *e = exp ? exp->syntaxCopy() : NULL; | |
3200 SynchronizedStatement *s = new SynchronizedStatement(loc, e, body ? body->syntaxCopy() : NULL); | |
3201 return s; | |
3202 } | |
3203 | |
3204 Statement *SynchronizedStatement::semantic(Scope *sc) | |
3205 { | |
3206 if (exp) | |
3207 { ClassDeclaration *cd; | |
3208 | |
3209 exp = exp->semantic(sc); | |
3210 exp = resolveProperties(sc, exp); | |
3211 cd = exp->type->isClassHandle(); | |
3212 if (!cd) | |
3213 error("can only synchronize on class objects, not '%s'", exp->type->toChars()); | |
3214 else if (cd->isInterfaceDeclaration()) | |
3215 { Type *t = new TypeIdentifier(0, Id::Object); | |
3216 | |
3217 t = t->semantic(0, sc); | |
3218 exp = new CastExp(loc, exp, t); | |
3219 exp = exp->semantic(sc); | |
3220 } | |
3221 } | |
3222 if (body) | |
309 | 3223 { |
3224 enclosinghandler = sc->tfOfTry; | |
3225 sc->tfOfTry = new EnclosingSynchro(this); | |
3226 body = body->semantic(sc); | |
3227 sc->tfOfTry = enclosinghandler; | |
3228 } | |
159 | 3229 return this; |
3230 } | |
3231 | |
3232 int SynchronizedStatement::hasBreak() | |
3233 { | |
3234 return FALSE; //TRUE; | |
3235 } | |
3236 | |
3237 int SynchronizedStatement::hasContinue() | |
3238 { | |
3239 return FALSE; //TRUE; | |
3240 } | |
3241 | |
3242 int SynchronizedStatement::usesEH() | |
3243 { | |
3244 return TRUE; | |
3245 } | |
3246 | |
336 | 3247 int SynchronizedStatement::blockExit() |
3248 { | |
3249 return body ? body->blockExit() : BEfallthru; | |
3250 } | |
3251 | |
159 | 3252 int SynchronizedStatement::fallOffEnd() |
3253 { | |
3254 return body ? body->fallOffEnd() : TRUE; | |
3255 } | |
3256 | |
3257 void SynchronizedStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
3258 { | |
3259 buf->writestring("synchronized"); | |
3260 if (exp) | |
3261 { buf->writebyte('('); | |
3262 exp->toCBuffer(buf, hgs); | |
3263 buf->writebyte(')'); | |
3264 } | |
3265 if (body) | |
3266 { | |
3267 buf->writebyte(' '); | |
3268 body->toCBuffer(buf, hgs); | |
3269 } | |
3270 } | |
3271 | |
3272 /******************************** WithStatement ***************************/ | |
3273 | |
3274 WithStatement::WithStatement(Loc loc, Expression *exp, Statement *body) | |
3275 : Statement(loc) | |
3276 { | |
3277 this->exp = exp; | |
3278 this->body = body; | |
3279 wthis = NULL; | |
3280 } | |
3281 | |
3282 Statement *WithStatement::syntaxCopy() | |
3283 { | |
3284 WithStatement *s = new WithStatement(loc, exp->syntaxCopy(), body ? body->syntaxCopy() : NULL); | |
3285 return s; | |
3286 } | |
3287 | |
3288 Statement *WithStatement::semantic(Scope *sc) | |
3289 { ScopeDsymbol *sym; | |
3290 Initializer *init; | |
3291 | |
3292 //printf("WithStatement::semantic()\n"); | |
3293 exp = exp->semantic(sc); | |
3294 exp = resolveProperties(sc, exp); | |
3295 if (exp->op == TOKimport) | |
3296 { ScopeExp *es = (ScopeExp *)exp; | |
3297 | |
3298 sym = es->sds; | |
3299 } | |
3300 else if (exp->op == TOKtype) | |
3301 { TypeExp *es = (TypeExp *)exp; | |
3302 | |
3303 sym = es->type->toDsymbol(sc)->isScopeDsymbol(); | |
3304 if (!sym) | |
3305 { error("%s has no members", es->toChars()); | |
3306 body = body->semantic(sc); | |
3307 return this; | |
3308 } | |
3309 } | |
3310 else | |
3311 { Type *t = exp->type; | |
3312 | |
3313 assert(t); | |
3314 t = t->toBasetype(); | |
3315 if (t->isClassHandle()) | |
3316 { | |
3317 init = new ExpInitializer(loc, exp); | |
3318 wthis = new VarDeclaration(loc, exp->type, Id::withSym, init); | |
3319 wthis->semantic(sc); | |
3320 | |
3321 sym = new WithScopeSymbol(this); | |
3322 sym->parent = sc->scopesym; | |
3323 } | |
3324 else if (t->ty == Tstruct) | |
3325 { | |
3326 Expression *e = exp->addressOf(sc); | |
3327 init = new ExpInitializer(loc, e); | |
3328 wthis = new VarDeclaration(loc, e->type, Id::withSym, init); | |
3329 wthis->semantic(sc); | |
3330 sym = new WithScopeSymbol(this); | |
3331 sym->parent = sc->scopesym; | |
3332 } | |
3333 else | |
3334 { error("with expressions must be class objects, not '%s'", exp->type->toChars()); | |
3335 return NULL; | |
3336 } | |
3337 } | |
3338 sc = sc->push(sym); | |
3339 | |
3340 if (body) | |
3341 body = body->semantic(sc); | |
3342 | |
3343 sc->pop(); | |
3344 | |
3345 return this; | |
3346 } | |
3347 | |
3348 void WithStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
3349 { | |
3350 buf->writestring("with ("); | |
3351 exp->toCBuffer(buf, hgs); | |
3352 buf->writestring(")\n"); | |
3353 if (body) | |
3354 body->toCBuffer(buf, hgs); | |
3355 } | |
3356 | |
3357 int WithStatement::usesEH() | |
3358 { | |
3359 return body ? body->usesEH() : 0; | |
3360 } | |
3361 | |
336 | 3362 int WithStatement::blockExit() |
3363 { | |
3364 int result = BEnone; | |
3365 if (exp->canThrow()) | |
3366 result = BEthrow; | |
3367 if (body) | |
3368 result |= body->blockExit(); | |
3369 else | |
3370 result |= BEfallthru; | |
3371 return result; | |
3372 } | |
3373 | |
159 | 3374 int WithStatement::fallOffEnd() |
3375 { | |
3376 return body ? body->fallOffEnd() : TRUE; | |
3377 } | |
3378 | |
3379 /******************************** TryCatchStatement ***************************/ | |
3380 | |
3381 TryCatchStatement::TryCatchStatement(Loc loc, Statement *body, Array *catches) | |
3382 : Statement(loc) | |
3383 { | |
3384 this->body = body; | |
3385 this->catches = catches; | |
3386 } | |
3387 | |
3388 Statement *TryCatchStatement::syntaxCopy() | |
3389 { | |
3390 Array *a = new Array(); | |
3391 a->setDim(catches->dim); | |
3392 for (int i = 0; i < a->dim; i++) | |
3393 { Catch *c; | |
3394 | |
3395 c = (Catch *)catches->data[i]; | |
3396 c = c->syntaxCopy(); | |
3397 a->data[i] = c; | |
3398 } | |
3399 TryCatchStatement *s = new TryCatchStatement(loc, body->syntaxCopy(), a); | |
3400 return s; | |
3401 } | |
3402 | |
3403 Statement *TryCatchStatement::semantic(Scope *sc) | |
3404 { | |
3405 body = body->semanticScope(sc, NULL /*this*/, NULL); | |
3406 | |
336 | 3407 /* Even if body is NULL, still do semantic analysis on catches |
3408 */ | |
3409 for (size_t i = 0; i < catches->dim; i++) | |
3410 { Catch *c = (Catch *)catches->data[i]; | |
159 | 3411 c->semantic(sc); |
3412 | |
3413 // Determine if current catch 'hides' any previous catches | |
336 | 3414 for (size_t j = 0; j < i; j++) |
159 | 3415 { Catch *cj = (Catch *)catches->data[j]; |
3416 char *si = c->loc.toChars(); | |
3417 char *sj = cj->loc.toChars(); | |
3418 | |
3419 if (c->type->toBasetype()->implicitConvTo(cj->type->toBasetype())) | |
3420 error("catch at %s hides catch at %s", sj, si); | |
3421 } | |
3422 } | |
3423 return this; | |
3424 } | |
3425 | |
3426 int TryCatchStatement::hasBreak() | |
3427 { | |
3428 return FALSE; //TRUE; | |
3429 } | |
3430 | |
3431 int TryCatchStatement::usesEH() | |
3432 { | |
3433 return TRUE; | |
3434 } | |
3435 | |
336 | 3436 int TryCatchStatement::blockExit() |
3437 { int result; | |
3438 | |
3439 assert(body); | |
3440 result = body->blockExit(); | |
3441 | |
3442 for (size_t i = 0; i < catches->dim; i++) | |
3443 { | |
3444 Catch *c = (Catch *)catches->data[i]; | |
3445 result |= c->blockExit(); | |
3446 } | |
3447 return result; | |
3448 } | |
3449 | |
159 | 3450 int TryCatchStatement::fallOffEnd() |
3451 { | |
3452 int result = FALSE; | |
3453 | |
3454 if (body) | |
3455 result = body->fallOffEnd(); | |
3456 for (int i = 0; i < catches->dim; i++) | |
3457 { Catch *c; | |
3458 | |
3459 c = (Catch *)catches->data[i]; | |
3460 if (c->handler) | |
3461 result |= c->handler->fallOffEnd(); | |
3462 } | |
3463 return result; | |
3464 } | |
3465 | |
3466 void TryCatchStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
3467 { | |
3468 buf->writestring("try"); | |
3469 buf->writenl(); | |
3470 if (body) | |
3471 body->toCBuffer(buf, hgs); | |
336 | 3472 for (size_t i = 0; i < catches->dim; i++) |
159 | 3473 { |
3474 Catch *c = (Catch *)catches->data[i]; | |
3475 c->toCBuffer(buf, hgs); | |
3476 } | |
3477 } | |
3478 | |
3479 /******************************** Catch ***************************/ | |
3480 | |
3481 Catch::Catch(Loc loc, Type *t, Identifier *id, Statement *handler) | |
3482 { | |
3483 //printf("Catch(%s, loc = %s)\n", id->toChars(), loc.toChars()); | |
3484 this->loc = loc; | |
3485 this->type = t; | |
3486 this->ident = id; | |
3487 this->handler = handler; | |
3488 var = NULL; | |
3489 } | |
3490 | |
3491 Catch *Catch::syntaxCopy() | |
3492 { | |
3493 Catch *c = new Catch(loc, | |
3494 (type ? type->syntaxCopy() : NULL), | |
3495 ident, | |
3496 (handler ? handler->syntaxCopy() : NULL)); | |
3497 return c; | |
3498 } | |
3499 | |
3500 void Catch::semantic(Scope *sc) | |
3501 { ScopeDsymbol *sym; | |
3502 | |
3503 //printf("Catch::semantic(%s)\n", ident->toChars()); | |
3504 | |
3505 #ifndef IN_GCC | |
3506 if (sc->tf) | |
3507 { | |
3508 /* This is because the _d_local_unwind() gets the stack munged | |
3509 * up on this. The workaround is to place any try-catches into | |
3510 * a separate function, and call that. | |
3511 * To fix, have the compiler automatically convert the finally | |
3512 * body into a nested function. | |
3513 */ | |
3514 error(loc, "cannot put catch statement inside finally block"); | |
3515 } | |
3516 #endif | |
3517 | |
3518 sym = new ScopeDsymbol(); | |
3519 sym->parent = sc->scopesym; | |
3520 sc = sc->push(sym); | |
3521 | |
3522 if (!type) | |
3523 type = new TypeIdentifier(0, Id::Object); | |
3524 type = type->semantic(loc, sc); | |
3525 if (!type->toBasetype()->isClassHandle()) | |
3526 error("can only catch class objects, not '%s'", type->toChars()); | |
3527 else if (ident) | |
3528 { | |
3529 var = new VarDeclaration(loc, type, ident, NULL); | |
3530 var->parent = sc->parent; | |
3531 sc->insert(var); | |
3532 } | |
3533 handler = handler->semantic(sc); | |
3534 | |
3535 sc->pop(); | |
3536 } | |
3537 | |
336 | 3538 int Catch::blockExit() |
3539 { | |
3540 return handler ? handler->blockExit() : BEfallthru; | |
3541 } | |
3542 | |
159 | 3543 void Catch::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
3544 { | |
3545 buf->writestring("catch"); | |
3546 if (type) | |
3547 { buf->writebyte('('); | |
3548 type->toCBuffer(buf, ident, hgs); | |
3549 buf->writebyte(')'); | |
3550 } | |
3551 buf->writenl(); | |
3552 buf->writebyte('{'); | |
3553 buf->writenl(); | |
336 | 3554 if (handler) |
3555 handler->toCBuffer(buf, hgs); | |
159 | 3556 buf->writebyte('}'); |
3557 buf->writenl(); | |
3558 } | |
3559 | |
3560 /****************************** TryFinallyStatement ***************************/ | |
3561 | |
3562 TryFinallyStatement::TryFinallyStatement(Loc loc, Statement *body, Statement *finalbody) | |
3563 : Statement(loc) | |
3564 { | |
3565 this->body = body; | |
3566 this->finalbody = finalbody; | |
309 | 3567 this->enclosinghandler = NULL; |
159 | 3568 } |
3569 | |
3570 Statement *TryFinallyStatement::syntaxCopy() | |
3571 { | |
3572 TryFinallyStatement *s = new TryFinallyStatement(loc, | |
3573 body->syntaxCopy(), finalbody->syntaxCopy()); | |
3574 return s; | |
3575 } | |
3576 | |
3577 Statement *TryFinallyStatement::semantic(Scope *sc) | |
3578 { | |
3579 //printf("TryFinallyStatement::semantic()\n"); | |
3580 | |
309 | 3581 enclosinghandler = sc->tfOfTry; |
3582 sc->tfOfTry = new EnclosingTryFinally(this); | |
159 | 3583 body = body->semantic(sc); |
309 | 3584 sc->tfOfTry = enclosinghandler; |
159 | 3585 |
3586 sc = sc->push(); | |
3587 sc->tf = this; | |
3588 sc->sbreak = NULL; | |
3589 sc->scontinue = NULL; // no break or continue out of finally block | |
3590 finalbody = finalbody->semantic(sc); | |
3591 sc->pop(); | |
3592 return this; | |
3593 } | |
3594 | |
3595 void TryFinallyStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
3596 { | |
3597 buf->printf("try\n{\n"); | |
3598 body->toCBuffer(buf, hgs); | |
3599 buf->printf("}\nfinally\n{\n"); | |
3600 finalbody->toCBuffer(buf, hgs); | |
3601 buf->writeByte('}'); | |
3602 buf->writenl(); | |
3603 } | |
3604 | |
3605 int TryFinallyStatement::hasBreak() | |
3606 { | |
3607 return FALSE; //TRUE; | |
3608 } | |
3609 | |
3610 int TryFinallyStatement::hasContinue() | |
3611 { | |
3612 return FALSE; //TRUE; | |
3613 } | |
3614 | |
3615 int TryFinallyStatement::usesEH() | |
3616 { | |
3617 return TRUE; | |
3618 } | |
3619 | |
336 | 3620 int TryFinallyStatement::blockExit() |
3621 { | |
3622 int result = body->blockExit(); | |
3623 return result; | |
3624 } | |
3625 | |
159 | 3626 int TryFinallyStatement::fallOffEnd() |
3627 { int result; | |
3628 | |
3629 result = body ? body->fallOffEnd() : TRUE; | |
3630 // if (finalbody) | |
3631 // result = finalbody->fallOffEnd(); | |
3632 return result; | |
3633 } | |
3634 | |
3635 /****************************** OnScopeStatement ***************************/ | |
3636 | |
3637 OnScopeStatement::OnScopeStatement(Loc loc, TOK tok, Statement *statement) | |
3638 : Statement(loc) | |
3639 { | |
3640 this->tok = tok; | |
3641 this->statement = statement; | |
3642 } | |
3643 | |
3644 Statement *OnScopeStatement::syntaxCopy() | |
3645 { | |
3646 OnScopeStatement *s = new OnScopeStatement(loc, | |
3647 tok, statement->syntaxCopy()); | |
3648 return s; | |
3649 } | |
3650 | |
3651 Statement *OnScopeStatement::semantic(Scope *sc) | |
3652 { | |
3653 /* semantic is called on results of scopeCode() */ | |
3654 return this; | |
3655 } | |
3656 | |
336 | 3657 int OnScopeStatement::blockExit() |
3658 { // At this point, this statement is just an empty placeholder | |
3659 return BEfallthru; | |
3660 } | |
3661 | |
159 | 3662 void OnScopeStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
3663 { | |
3664 buf->writestring(Token::toChars(tok)); | |
3665 buf->writebyte(' '); | |
3666 statement->toCBuffer(buf, hgs); | |
3667 } | |
3668 | |
3669 int OnScopeStatement::usesEH() | |
3670 { | |
3671 return (tok != TOKon_scope_success); | |
3672 } | |
3673 | |
3674 void OnScopeStatement::scopeCode(Statement **sentry, Statement **sexception, Statement **sfinally) | |
3675 { | |
3676 //printf("OnScopeStatement::scopeCode()\n"); | |
3677 //print(); | |
3678 *sentry = NULL; | |
3679 *sexception = NULL; | |
3680 *sfinally = NULL; | |
3681 switch (tok) | |
3682 { | |
3683 case TOKon_scope_exit: | |
3684 *sfinally = statement; | |
3685 break; | |
3686 | |
3687 case TOKon_scope_failure: | |
3688 *sexception = statement; | |
3689 break; | |
3690 | |
3691 case TOKon_scope_success: | |
3692 { | |
3693 /* Create: | |
3694 * sentry: int x = 0; | |
3695 * sexception: x = 1; | |
3696 * sfinally: if (!x) statement; | |
3697 */ | |
3698 static int num; | |
3699 char name[5 + sizeof(num) * 3 + 1]; | |
3700 sprintf(name, "__osf%d", ++num); | |
3701 Identifier *id = Lexer::idPool(name); | |
3702 | |
3703 ExpInitializer *ie = new ExpInitializer(loc, new IntegerExp(0)); | |
3704 VarDeclaration *v = new VarDeclaration(loc, Type::tint32, id, ie); | |
3705 *sentry = new DeclarationStatement(loc, v); | |
3706 | |
3707 Expression *e = new IntegerExp(1); | |
3708 e = new AssignExp(0, new VarExp(0, v), e); | |
3709 *sexception = new ExpStatement(0, e); | |
3710 | |
3711 e = new VarExp(0, v); | |
3712 e = new NotExp(0, e); | |
3713 *sfinally = new IfStatement(0, NULL, e, statement, NULL); | |
3714 | |
3715 break; | |
3716 } | |
3717 | |
3718 default: | |
3719 assert(0); | |
3720 } | |
3721 } | |
3722 | |
3723 /******************************** ThrowStatement ***************************/ | |
3724 | |
3725 ThrowStatement::ThrowStatement(Loc loc, Expression *exp) | |
3726 : Statement(loc) | |
3727 { | |
3728 this->exp = exp; | |
3729 } | |
3730 | |
3731 Statement *ThrowStatement::syntaxCopy() | |
3732 { | |
3733 ThrowStatement *s = new ThrowStatement(loc, exp->syntaxCopy()); | |
3734 return s; | |
3735 } | |
3736 | |
3737 Statement *ThrowStatement::semantic(Scope *sc) | |
3738 { | |
3739 //printf("ThrowStatement::semantic()\n"); | |
3740 | |
3741 FuncDeclaration *fd = sc->parent->isFuncDeclaration(); | |
3742 fd->hasReturnExp |= 2; | |
3743 | |
3744 if (sc->incontract) | |
3745 error("Throw statements cannot be in contracts"); | |
3746 exp = exp->semantic(sc); | |
3747 exp = resolveProperties(sc, exp); | |
3748 if (!exp->type->toBasetype()->isClassHandle()) | |
3749 error("can only throw class objects, not type %s", exp->type->toChars()); | |
3750 return this; | |
3751 } | |
3752 | |
336 | 3753 int ThrowStatement::blockExit() |
3754 { | |
3755 return BEthrow; // obviously | |
3756 } | |
3757 | |
159 | 3758 int ThrowStatement::fallOffEnd() |
3759 { | |
3760 return FALSE; | |
3761 } | |
3762 | |
3763 void ThrowStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
3764 { | |
3765 buf->printf("throw "); | |
3766 exp->toCBuffer(buf, hgs); | |
3767 buf->writeByte(';'); | |
3768 buf->writenl(); | |
3769 } | |
3770 | |
3771 /******************************** VolatileStatement **************************/ | |
3772 | |
3773 VolatileStatement::VolatileStatement(Loc loc, Statement *statement) | |
3774 : Statement(loc) | |
3775 { | |
3776 this->statement = statement; | |
309 | 3777 this->enclosinghandler = NULL; |
159 | 3778 } |
3779 | |
3780 Statement *VolatileStatement::syntaxCopy() | |
3781 { | |
3782 VolatileStatement *s = new VolatileStatement(loc, | |
3783 statement ? statement->syntaxCopy() : NULL); | |
3784 return s; | |
3785 } | |
3786 | |
3787 Statement *VolatileStatement::semantic(Scope *sc) | |
3788 { | |
309 | 3789 if (statement) |
3790 { | |
3791 enclosinghandler = sc->tfOfTry; | |
3792 sc->tfOfTry = new EnclosingVolatile(this); | |
3793 statement = statement->semantic(sc); | |
3794 sc->tfOfTry = enclosinghandler; | |
3795 } | |
159 | 3796 return this; |
3797 } | |
3798 | |
3799 Statements *VolatileStatement::flatten(Scope *sc) | |
3800 { | |
3801 Statements *a; | |
3802 | |
3803 a = statement ? statement->flatten(sc) : NULL; | |
3804 if (a) | |
3805 { for (int i = 0; i < a->dim; i++) | |
3806 { Statement *s = (Statement *)a->data[i]; | |
3807 | |
3808 s = new VolatileStatement(loc, s); | |
3809 a->data[i] = s; | |
3810 } | |
3811 } | |
3812 | |
3813 return a; | |
3814 } | |
3815 | |
336 | 3816 int VolatileStatement::blockExit() |
3817 { | |
3818 return statement ? statement->blockExit() : BEfallthru; | |
3819 } | |
3820 | |
159 | 3821 int VolatileStatement::fallOffEnd() |
3822 { | |
3823 return statement ? statement->fallOffEnd() : TRUE; | |
3824 } | |
3825 | |
3826 void VolatileStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
3827 { | |
3828 buf->writestring("volatile"); | |
3829 if (statement) | |
3830 { if (statement->isScopeStatement()) | |
3831 buf->writenl(); | |
3832 else | |
3833 buf->writebyte(' '); | |
3834 statement->toCBuffer(buf, hgs); | |
3835 } | |
3836 } | |
3837 | |
3838 | |
3839 /******************************** GotoStatement ***************************/ | |
3840 | |
3841 GotoStatement::GotoStatement(Loc loc, Identifier *ident) | |
3842 : Statement(loc) | |
3843 { | |
3844 this->ident = ident; | |
3845 this->label = NULL; | |
3846 this->tf = NULL; | |
309 | 3847 this->enclosinghandler = NULL; |
159 | 3848 } |
3849 | |
3850 Statement *GotoStatement::syntaxCopy() | |
3851 { | |
3852 GotoStatement *s = new GotoStatement(loc, ident); | |
3853 return s; | |
3854 } | |
3855 | |
3856 Statement *GotoStatement::semantic(Scope *sc) | |
3857 { FuncDeclaration *fd = sc->parent->isFuncDeclaration(); | |
3858 | |
3859 //printf("GotoStatement::semantic()\n"); | |
3860 tf = sc->tf; | |
309 | 3861 enclosinghandler = sc->tfOfTry; |
159 | 3862 label = fd->searchLabel(ident); |
3863 if (!label->statement && sc->fes) | |
3864 { | |
3865 /* Either the goto label is forward referenced or it | |
3866 * is in the function that the enclosing foreach is in. | |
3867 * Can't know yet, so wrap the goto in a compound statement | |
3868 * so we can patch it later, and add it to a 'look at this later' | |
3869 * list. | |
3870 */ | |
3871 Statements *a = new Statements(); | |
3872 Statement *s; | |
3873 | |
3874 a->push(this); | |
3875 s = new CompoundStatement(loc, a); | |
3876 sc->fes->gotos.push(s); // 'look at this later' list | |
3877 return s; | |
3878 } | |
3879 if (label->statement && label->statement->tf != sc->tf) | |
3880 error("cannot goto in or out of finally block"); | |
3881 return this; | |
3882 } | |
3883 | |
336 | 3884 int GotoStatement::blockExit() |
3885 { | |
3886 //printf("GotoStatement::blockExit(%p)\n", this); | |
3887 return BEgoto; | |
3888 } | |
3889 | |
159 | 3890 int GotoStatement::fallOffEnd() |
3891 { | |
3892 return FALSE; | |
3893 } | |
3894 | |
3895 void GotoStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
3896 { | |
3897 buf->writestring("goto "); | |
3898 buf->writestring(ident->toChars()); | |
3899 buf->writebyte(';'); | |
3900 buf->writenl(); | |
3901 } | |
3902 | |
3903 /******************************** LabelStatement ***************************/ | |
3904 | |
3905 LabelStatement::LabelStatement(Loc loc, Identifier *ident, Statement *statement) | |
3906 : Statement(loc) | |
3907 { | |
3908 this->ident = ident; | |
3909 this->statement = statement; | |
3910 this->tf = NULL; | |
309 | 3911 this->enclosinghandler = NULL; |
159 | 3912 this->lblock = NULL; |
3913 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
|
3914 this->asmLabel = false; |
159 | 3915 } |
3916 | |
3917 Statement *LabelStatement::syntaxCopy() | |
3918 { | |
3919 LabelStatement *s = new LabelStatement(loc, ident, statement->syntaxCopy()); | |
3920 return s; | |
3921 } | |
3922 | |
3923 Statement *LabelStatement::semantic(Scope *sc) | |
3924 { LabelDsymbol *ls; | |
3925 FuncDeclaration *fd = sc->parent->isFuncDeclaration(); | |
3926 | |
3927 //printf("LabelStatement::semantic()\n"); | |
3928 ls = fd->searchLabel(ident); | |
3929 if (ls->statement) | |
3930 error("Label '%s' already defined", ls->toChars()); | |
3931 else | |
3932 ls->statement = this; | |
3933 tf = sc->tf; | |
309 | 3934 enclosinghandler = sc->tfOfTry; |
159 | 3935 sc = sc->push(); |
3936 sc->scopesym = sc->enclosing->scopesym; | |
3937 sc->callSuper |= CSXlabel; | |
3938 sc->slabel = this; | |
3939 if (statement) | |
3940 statement = statement->semantic(sc); | |
3941 sc->pop(); | |
305
2b72433d5c8c
[svn r326] Fixed a bunch of issues with printf's that MinGW32 did not support.
lindquist
parents:
304
diff
changeset
|
3942 |
2b72433d5c8c
[svn r326] Fixed a bunch of issues with printf's that MinGW32 did not support.
lindquist
parents:
304
diff
changeset
|
3943 // 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
|
3944 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
|
3945 |
159 | 3946 return this; |
3947 } | |
3948 | |
3949 Statements *LabelStatement::flatten(Scope *sc) | |
3950 { | |
3951 Statements *a = NULL; | |
3952 | |
3953 if (statement) | |
3954 { | |
3955 a = statement->flatten(sc); | |
3956 if (a) | |
3957 { | |
3958 if (!a->dim) | |
3959 { | |
3960 a->push(new ExpStatement(loc, NULL)); | |
3961 } | |
3962 Statement *s = (Statement *)a->data[0]; | |
3963 | |
3964 s = new LabelStatement(loc, ident, s); | |
3965 a->data[0] = s; | |
3966 } | |
3967 } | |
3968 | |
3969 return a; | |
3970 } | |
3971 | |
3972 | |
3973 int LabelStatement::usesEH() | |
3974 { | |
3975 return statement ? statement->usesEH() : FALSE; | |
3976 } | |
3977 | |
336 | 3978 int LabelStatement::blockExit() |
3979 { | |
3980 //printf("LabelStatement::blockExit(%p)\n", this); | |
3981 return statement ? statement->blockExit() : BEfallthru; | |
3982 } | |
3983 | |
159 | 3984 int LabelStatement::fallOffEnd() |
3985 { | |
3986 return statement ? statement->fallOffEnd() : TRUE; | |
3987 } | |
3988 | |
3989 int LabelStatement::comeFrom() | |
3990 { | |
3991 //printf("LabelStatement::comeFrom()\n"); | |
3992 return TRUE; | |
3993 } | |
3994 | |
3995 void LabelStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
3996 { | |
3997 buf->writestring(ident->toChars()); | |
3998 buf->writebyte(':'); | |
3999 buf->writenl(); | |
4000 if (statement) | |
4001 statement->toCBuffer(buf, hgs); | |
4002 } | |
4003 | |
4004 | |
4005 /******************************** LabelDsymbol ***************************/ | |
4006 | |
4007 LabelDsymbol::LabelDsymbol(Identifier *ident) | |
4008 : Dsymbol(ident) | |
4009 { | |
4010 statement = NULL; | |
4011 } | |
4012 | |
4013 LabelDsymbol *LabelDsymbol::isLabel() // is this a LabelDsymbol()? | |
4014 { | |
4015 return this; | |
4016 } | |
4017 | |
4018 |