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