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