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