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