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