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