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