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