Mercurial > projects > ldc
annotate dmd/statement.c @ 1639:41145d30acb8
Merge.
author | Christian Kamm <kamm incasoftware de> |
---|---|
date | Mon, 08 Mar 2010 20:09:18 +0100 |
parents | 44b145be2ef5 |
children | 9bf06e02070b |
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); | |
1630
44b145be2ef5
Merge dmd 1.056.
Robert Clipsham <robert@octarineparrot.com>
parents:
1607
diff
changeset
|
2486 #if 1 |
159 | 2487 e = e->optimize(WANTvalue | WANTinterpret); |
1630
44b145be2ef5
Merge dmd 1.056.
Robert Clipsham <robert@octarineparrot.com>
parents:
1607
diff
changeset
|
2488 #else |
44b145be2ef5
Merge dmd 1.056.
Robert Clipsham <robert@octarineparrot.com>
parents:
1607
diff
changeset
|
2489 e = e->interpret(NULL); |
44b145be2ef5
Merge dmd 1.056.
Robert Clipsham <robert@octarineparrot.com>
parents:
1607
diff
changeset
|
2490 if (e == EXP_CANT_INTERPRET) |
44b145be2ef5
Merge dmd 1.056.
Robert Clipsham <robert@octarineparrot.com>
parents:
1607
diff
changeset
|
2491 fprintf(stdmsg, ((Expression *)args->data[i])->toChars()); |
44b145be2ef5
Merge dmd 1.056.
Robert Clipsham <robert@octarineparrot.com>
parents:
1607
diff
changeset
|
2492 else |
44b145be2ef5
Merge dmd 1.056.
Robert Clipsham <robert@octarineparrot.com>
parents:
1607
diff
changeset
|
2493 #endif |
159 | 2494 if (e->op == TOKstring) |
2495 { | |
2496 StringExp *se = (StringExp *)e; | |
1587 | 2497 fprintf(stdmsg, "%.*s", (int)se->len, (char *)se->string); |
159 | 2498 } |
2499 else | |
1587 | 2500 fprintf(stdmsg, e->toChars()); |
159 | 2501 } |
2502 fprintf(stdmsg, "\n"); | |
2503 } | |
2504 } | |
2505 else if (ident == Id::lib) | |
2506 { | |
846
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
2507 #if 1 |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
2508 /* Should this be allowed? |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
2509 */ |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
2510 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
|
2511 #else |
159 | 2512 if (!args || args->dim != 1) |
2513 error("string expected for library name"); | |
2514 else | |
2515 { | |
2516 Expression *e = (Expression *)args->data[0]; | |
2517 | |
2518 e = e->semantic(sc); | |
2519 e = e->optimize(WANTvalue | WANTinterpret); | |
2520 args->data[0] = (void *)e; | |
2521 if (e->op != TOKstring) | |
2522 error("string expected for library name, not '%s'", e->toChars()); | |
2523 else if (global.params.verbose) | |
2524 { | |
2525 StringExp *se = (StringExp *)e; | |
2526 char *name = (char *)mem.malloc(se->len + 1); | |
2527 memcpy(name, se->string, se->len); | |
2528 name[se->len] = 0; | |
2529 printf("library %s\n", name); | |
2530 mem.free(name); | |
2531 } | |
2532 } | |
846
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
2533 #endif |
159 | 2534 } |
945
03d7c4aac654
SWITCHED TO LLVM 2.5 !
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
908
diff
changeset
|
2535 |
03d7c4aac654
SWITCHED TO LLVM 2.5 !
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
908
diff
changeset
|
2536 // LDC |
03d7c4aac654
SWITCHED TO LLVM 2.5 !
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
908
diff
changeset
|
2537 else if (ident == Id::allow_inline) |
03d7c4aac654
SWITCHED TO LLVM 2.5 !
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
908
diff
changeset
|
2538 { |
03d7c4aac654
SWITCHED TO LLVM 2.5 !
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
908
diff
changeset
|
2539 sc->func->allowInlining = true; |
03d7c4aac654
SWITCHED TO LLVM 2.5 !
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
908
diff
changeset
|
2540 } |
1587 | 2541 #if DMDV2 |
2542 else if (ident == Id::startaddress) | |
2543 { | |
2544 if (!args || args->dim != 1) | |
2545 error("function name expected for start address"); | |
2546 else | |
2547 { | |
2548 Expression *e = (Expression *)args->data[0]; | |
2549 e = e->semantic(sc); | |
2550 e = e->optimize(WANTvalue | WANTinterpret); | |
2551 args->data[0] = (void *)e; | |
2552 Dsymbol *sa = getDsymbol(e); | |
2553 if (!sa || !sa->isFuncDeclaration()) | |
2554 error("function name expected for start address, not '%s'", e->toChars()); | |
2555 if (body) | |
2556 { | |
2557 body = body->semantic(sc); | |
2558 } | |
2559 return this; | |
2560 } | |
2561 } | |
2562 #endif | |
159 | 2563 else |
2564 error("unrecognized pragma(%s)", ident->toChars()); | |
2565 | |
2566 if (body) | |
2567 { | |
2568 body = body->semantic(sc); | |
2569 } | |
2570 return body; | |
2571 } | |
2572 | |
2573 int PragmaStatement::usesEH() | |
2574 { | |
2575 return body && body->usesEH(); | |
2576 } | |
2577 | |
336 | 2578 int PragmaStatement::blockExit() |
2579 { | |
2580 int result = BEfallthru; | |
2581 #if 0 // currently, no code is generated for Pragma's, so it's just fallthru | |
2582 if (arrayExpressionCanThrow(args)) | |
2583 result |= BEthrow; | |
2584 if (body) | |
2585 result |= body->blockExit(); | |
2586 #endif | |
2587 return result; | |
2588 } | |
2589 | |
1587 | 2590 |
159 | 2591 void PragmaStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
2592 { | |
2593 buf->writestring("pragma ("); | |
2594 buf->writestring(ident->toChars()); | |
2595 if (args && args->dim) | |
2596 { | |
2597 buf->writestring(", "); | |
2598 argsToCBuffer(buf, args, hgs); | |
2599 } | |
2600 buf->writeByte(')'); | |
2601 if (body) | |
2602 { | |
2603 buf->writenl(); | |
2604 buf->writeByte('{'); | |
2605 buf->writenl(); | |
2606 | |
2607 body->toCBuffer(buf, hgs); | |
2608 | |
2609 buf->writeByte('}'); | |
2610 buf->writenl(); | |
2611 } | |
2612 else | |
2613 { | |
2614 buf->writeByte(';'); | |
2615 buf->writenl(); | |
2616 } | |
2617 } | |
2618 | |
2619 | |
2620 /******************************** StaticAssertStatement ***************************/ | |
2621 | |
2622 StaticAssertStatement::StaticAssertStatement(StaticAssert *sa) | |
2623 : Statement(sa->loc) | |
2624 { | |
2625 this->sa = sa; | |
2626 } | |
2627 | |
2628 Statement *StaticAssertStatement::syntaxCopy() | |
2629 { | |
2630 StaticAssertStatement *s = new StaticAssertStatement((StaticAssert *)sa->syntaxCopy(NULL)); | |
2631 return s; | |
2632 } | |
2633 | |
2634 Statement *StaticAssertStatement::semantic(Scope *sc) | |
2635 { | |
2636 sa->semantic2(sc); | |
2637 return NULL; | |
2638 } | |
2639 | |
1587 | 2640 int StaticAssertStatement::blockExit() |
2641 { | |
2642 return BEfallthru; | |
2643 } | |
2644 | |
159 | 2645 void StaticAssertStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
2646 { | |
2647 sa->toCBuffer(buf, hgs); | |
2648 } | |
2649 | |
2650 | |
2651 /******************************** SwitchStatement ***************************/ | |
2652 | |
2653 SwitchStatement::SwitchStatement(Loc loc, Expression *c, Statement *b) | |
2654 : Statement(loc) | |
2655 { | |
2656 condition = c; | |
2657 body = b; | |
2658 sdefault = NULL; | |
2659 cases = NULL; | |
2660 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
|
2661 // LDC |
c6d6a68bb5db
Add back some enclosing scope-exit information to the frontend to produce
Christian Kamm <kamm incasoftware de>
parents:
1141
diff
changeset
|
2662 enclosingScopeExit = NULL; |
159 | 2663 } |
2664 | |
2665 Statement *SwitchStatement::syntaxCopy() | |
2666 { | |
2667 SwitchStatement *s = new SwitchStatement(loc, | |
2668 condition->syntaxCopy(), body->syntaxCopy()); | |
2669 return s; | |
2670 } | |
2671 | |
2672 Statement *SwitchStatement::semantic(Scope *sc) | |
2673 { | |
2674 //printf("SwitchStatement::semantic(%p)\n", this); | |
2675 assert(!cases); // ensure semantic() is only run once | |
2676 | |
1159
c6d6a68bb5db
Add back some enclosing scope-exit information to the frontend to produce
Christian Kamm <kamm incasoftware de>
parents:
1141
diff
changeset
|
2677 // LDC |
c6d6a68bb5db
Add back some enclosing scope-exit information to the frontend to produce
Christian Kamm <kamm incasoftware de>
parents:
1141
diff
changeset
|
2678 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
|
2679 |
159 | 2680 condition = condition->semantic(sc); |
2681 condition = resolveProperties(sc, condition); | |
2682 if (condition->type->isString()) | |
2683 { | |
2684 // If it's not an array, cast it to one | |
2685 if (condition->type->ty != Tarray) | |
2686 { | |
2687 condition = condition->implicitCastTo(sc, condition->type->nextOf()->arrayOf()); | |
2688 } | |
2689 } | |
2690 else | |
2691 { condition = condition->integralPromotions(sc); | |
2692 condition->checkIntegral(); | |
2693 } | |
2694 condition = condition->optimize(WANTvalue); | |
2695 | |
2696 sc = sc->push(); | |
2697 sc->sbreak = this; | |
2698 sc->sw = this; | |
2699 | |
2700 cases = new Array(); | |
2701 sc->noctor++; // BUG: should use Scope::mergeCallSuper() for each case instead | |
2702 body = body->semantic(sc); | |
2703 sc->noctor--; | |
2704 | |
2705 // Resolve any goto case's with exp | |
2706 for (int i = 0; i < gotoCases.dim; i++) | |
2707 { | |
2708 GotoCaseStatement *gcs = (GotoCaseStatement *)gotoCases.data[i]; | |
2709 | |
2710 if (!gcs->exp) | |
2711 { | |
2712 gcs->error("no case statement following goto case;"); | |
2713 break; | |
2714 } | |
2715 | |
2716 for (Scope *scx = sc; scx; scx = scx->enclosing) | |
2717 { | |
2718 if (!scx->sw) | |
2719 continue; | |
2720 for (int j = 0; j < scx->sw->cases->dim; j++) | |
2721 { | |
2722 CaseStatement *cs = (CaseStatement *)scx->sw->cases->data[j]; | |
2723 | |
2724 if (cs->exp->equals(gcs->exp)) | |
2725 { | |
2726 gcs->cs = cs; | |
2727 goto Lfoundcase; | |
2728 } | |
2729 } | |
2730 } | |
2731 gcs->error("case %s not found", gcs->exp->toChars()); | |
2732 | |
2733 Lfoundcase: | |
2734 ; | |
2735 } | |
2736 | |
2737 if (!sc->sw->sdefault) | |
2738 { hasNoDefault = 1; | |
2739 | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
945
diff
changeset
|
2740 warning("switch statement has no default"); |
159 | 2741 |
2742 // Generate runtime error if the default is hit | |
2743 Statements *a = new Statements(); | |
2744 CompoundStatement *cs; | |
2745 Statement *s; | |
2746 | |
2747 if (global.params.useSwitchError) | |
2748 s = new SwitchErrorStatement(loc); | |
2749 else | |
2750 { Expression *e = new HaltExp(loc); | |
2751 s = new ExpStatement(loc, e); | |
2752 } | |
2753 | |
2754 a->reserve(4); | |
2755 a->push(body); | |
901 | 2756 |
2757 // LDC needs semantic to be run on break | |
2758 Statement *breakstmt = new BreakStatement(loc, NULL); | |
2759 breakstmt->semantic(sc); | |
2760 a->push(breakstmt); | |
2761 | |
159 | 2762 sc->sw->sdefault = new DefaultStatement(loc, s); |
2763 a->push(sc->sw->sdefault); | |
2764 cs = new CompoundStatement(loc, a); | |
2765 body = cs; | |
2766 } | |
2767 | |
1587 | 2768 #if DMDV2 |
2769 if (isFinal) | |
2770 { Type *t = condition->type; | |
2771 while (t->ty == Ttypedef) | |
2772 { // Don't use toBasetype() because that will skip past enums | |
2773 t = ((TypeTypedef *)t)->sym->basetype; | |
2774 } | |
2775 if (condition->type->ty == Tenum) | |
2776 { TypeEnum *te = (TypeEnum *)condition->type; | |
2777 EnumDeclaration *ed = te->toDsymbol(sc)->isEnumDeclaration(); | |
2778 assert(ed); | |
2779 size_t dim = ed->members->dim; | |
2780 for (size_t i = 0; i < dim; i++) | |
2781 { | |
2782 EnumMember *em = ((Dsymbol *)ed->members->data[i])->isEnumMember(); | |
2783 if (em) | |
2784 { | |
2785 for (size_t j = 0; j < cases->dim; j++) | |
2786 { CaseStatement *cs = (CaseStatement *)cases->data[j]; | |
2787 if (cs->exp->equals(em->value)) | |
2788 goto L1; | |
2789 } | |
2790 error("enum member %s not represented in final switch", em->toChars()); | |
2791 } | |
2792 L1: | |
2793 ; | |
2794 } | |
2795 } | |
2796 } | |
2797 #endif | |
2798 | |
159 | 2799 sc->pop(); |
2800 return this; | |
2801 } | |
2802 | |
2803 int SwitchStatement::hasBreak() | |
2804 { | |
2805 return TRUE; | |
2806 } | |
2807 | |
2808 int SwitchStatement::usesEH() | |
2809 { | |
2810 return body ? body->usesEH() : 0; | |
2811 } | |
2812 | |
336 | 2813 int SwitchStatement::blockExit() |
2814 { int result = BEnone; | |
2815 if (condition->canThrow()) | |
2816 result |= BEthrow; | |
2817 | |
2818 if (body) | |
2819 { result |= body->blockExit(); | |
2820 if (result & BEbreak) | |
2821 { result |= BEfallthru; | |
2822 result &= ~BEbreak; | |
2823 } | |
2824 } | |
2825 else | |
2826 result |= BEfallthru; | |
2827 | |
2828 return result; | |
2829 } | |
2830 | |
1587 | 2831 |
159 | 2832 void SwitchStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
2833 { | |
2834 buf->writestring("switch ("); | |
2835 condition->toCBuffer(buf, hgs); | |
2836 buf->writebyte(')'); | |
2837 buf->writenl(); | |
2838 if (body) | |
2839 { | |
2840 if (!body->isScopeStatement()) | |
2841 { buf->writebyte('{'); | |
2842 buf->writenl(); | |
2843 body->toCBuffer(buf, hgs); | |
2844 buf->writebyte('}'); | |
2845 buf->writenl(); | |
2846 } | |
2847 else | |
2848 { | |
2849 body->toCBuffer(buf, hgs); | |
2850 } | |
2851 } | |
2852 } | |
2853 | |
2854 /******************************** CaseStatement ***************************/ | |
2855 | |
2856 CaseStatement::CaseStatement(Loc loc, Expression *exp, Statement *s) | |
2857 : Statement(loc) | |
2858 { | |
2859 this->exp = exp; | |
2860 this->statement = s; | |
2861 cblock = NULL; | |
2862 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
|
2863 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
|
2864 // LDC |
c6d6a68bb5db
Add back some enclosing scope-exit information to the frontend to produce
Christian Kamm <kamm incasoftware de>
parents:
1141
diff
changeset
|
2865 enclosingScopeExit = NULL; |
159 | 2866 } |
2867 | |
2868 Statement *CaseStatement::syntaxCopy() | |
2869 { | |
2870 CaseStatement *s = new CaseStatement(loc, exp->syntaxCopy(), statement->syntaxCopy()); | |
2871 return s; | |
2872 } | |
2873 | |
2874 Statement *CaseStatement::semantic(Scope *sc) | |
2875 { SwitchStatement *sw = sc->sw; | |
2876 | |
2877 //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
|
2878 |
159 | 2879 exp = exp->semantic(sc); |
2880 if (sw) | |
846
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
2881 { |
1164
166042b48c28
Fix bug in CaseStatement::semantic when there's no enclosing switch.
Christian Kamm <kamm incasoftware de>
parents:
1159
diff
changeset
|
2882 // LDC |
166042b48c28
Fix bug in CaseStatement::semantic when there's no enclosing switch.
Christian Kamm <kamm incasoftware de>
parents:
1159
diff
changeset
|
2883 enclosingScopeExit = sc->enclosingScopeExit; |
166042b48c28
Fix bug in CaseStatement::semantic when there's no enclosing switch.
Christian Kamm <kamm incasoftware de>
parents:
1159
diff
changeset
|
2884 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
|
2885 { |
166042b48c28
Fix bug in CaseStatement::semantic when there's no enclosing switch.
Christian Kamm <kamm incasoftware de>
parents:
1159
diff
changeset
|
2886 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
|
2887 } |
166042b48c28
Fix bug in CaseStatement::semantic when there's no enclosing switch.
Christian Kamm <kamm incasoftware de>
parents:
1159
diff
changeset
|
2888 |
159 | 2889 exp = exp->implicitCastTo(sc, sw->condition->type); |
2890 exp = exp->optimize(WANTvalue | WANTinterpret); | |
2891 if (exp->op != TOKstring && exp->op != TOKint64) | |
2892 { | |
2893 error("case must be a string or an integral constant, not %s", exp->toChars()); | |
2894 exp = new IntegerExp(0); | |
2895 } | |
2896 | |
846
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
2897 for (int i = 0; i < sw->cases->dim; i++) |
159 | 2898 { |
2899 CaseStatement *cs = (CaseStatement *)sw->cases->data[i]; | |
2900 | |
2901 //printf("comparing '%s' with '%s'\n", exp->toChars(), cs->exp->toChars()); | |
2902 if (cs->exp->equals(exp)) | |
2903 { error("duplicate case %s in switch statement", exp->toChars()); | |
2904 break; | |
2905 } | |
2906 } | |
2907 | |
2908 sw->cases->push(this); | |
2909 | |
2910 // 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
|
2911 for (size_t i = 0; i < sw->gotoCases.dim; i++) |
159 | 2912 { |
2913 GotoCaseStatement *gcs = (GotoCaseStatement *)sw->gotoCases.data[i]; | |
2914 | |
2915 if (!gcs->exp) | |
2916 { | |
2917 gcs->cs = this; | |
2918 sw->gotoCases.remove(i); // remove from array | |
2919 } | |
2920 } | |
2921 } | |
2922 else | |
2923 error("case not in switch statement"); | |
2924 statement = statement->semantic(sc); | |
2925 return this; | |
2926 } | |
2927 | |
2928 int CaseStatement::compare(Object *obj) | |
2929 { | |
2930 // Sort cases so we can do an efficient lookup | |
2931 CaseStatement *cs2 = (CaseStatement *)(obj); | |
2932 | |
2933 return exp->compare(cs2->exp); | |
2934 } | |
2935 | |
2936 int CaseStatement::usesEH() | |
2937 { | |
2938 return statement->usesEH(); | |
2939 } | |
2940 | |
336 | 2941 int CaseStatement::blockExit() |
2942 { | |
2943 return statement->blockExit(); | |
2944 } | |
2945 | |
1587 | 2946 |
159 | 2947 int CaseStatement::comeFrom() |
2948 { | |
2949 return TRUE; | |
2950 } | |
2951 | |
2952 void CaseStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2953 { | |
2954 buf->writestring("case "); | |
2955 exp->toCBuffer(buf, hgs); | |
2956 buf->writebyte(':'); | |
2957 buf->writenl(); | |
2958 statement->toCBuffer(buf, hgs); | |
2959 } | |
2960 | |
1587 | 2961 /******************************** CaseRangeStatement ***************************/ |
2962 | |
2963 #if DMDV2 | |
2964 | |
2965 CaseRangeStatement::CaseRangeStatement(Loc loc, Expression *first, | |
2966 Expression *last, Statement *s) | |
2967 : Statement(loc) | |
2968 { | |
2969 this->first = first; | |
2970 this->last = last; | |
2971 this->statement = s; | |
2972 } | |
2973 | |
2974 Statement *CaseRangeStatement::syntaxCopy() | |
2975 { | |
2976 CaseRangeStatement *s = new CaseRangeStatement(loc, | |
2977 first->syntaxCopy(), last->syntaxCopy(), statement->syntaxCopy()); | |
2978 return s; | |
2979 } | |
2980 | |
2981 Statement *CaseRangeStatement::semantic(Scope *sc) | |
2982 { SwitchStatement *sw = sc->sw; | |
2983 | |
2984 //printf("CaseRangeStatement::semantic() %s\n", toChars()); | |
2985 if (sw->isFinal) | |
2986 error("case ranges not allowed in final switch"); | |
2987 | |
2988 first = first->semantic(sc); | |
2989 first = first->implicitCastTo(sc, sw->condition->type); | |
2990 first = first->optimize(WANTvalue | WANTinterpret); | |
2991 dinteger_t fval = first->toInteger(); | |
2992 | |
2993 last = last->semantic(sc); | |
2994 last = last->implicitCastTo(sc, sw->condition->type); | |
2995 last = last->optimize(WANTvalue | WANTinterpret); | |
2996 dinteger_t lval = last->toInteger(); | |
2997 | |
2998 if (lval - fval > 256) | |
2999 { error("more than 256 cases in case range"); | |
3000 lval = fval + 256; | |
3001 } | |
3002 | |
3003 /* This works by replacing the CaseRange with an array of Case's. | |
3004 * | |
3005 * case a: .. case b: s; | |
3006 * => | |
3007 * case a: | |
3008 * [...] | |
3009 * case b: | |
3010 * s; | |
3011 */ | |
3012 | |
3013 Statements *statements = new Statements(); | |
3014 for (dinteger_t i = fval; i <= lval; i++) | |
3015 { | |
3016 Statement *s = statement; | |
3017 if (i != lval) | |
3018 s = new ExpStatement(loc, NULL); | |
3019 Expression *e = new IntegerExp(loc, i, first->type); | |
3020 Statement *cs = new CaseStatement(loc, e, s); | |
3021 statements->push(cs); | |
3022 } | |
3023 Statement *s = new CompoundStatement(loc, statements); | |
3024 s = s->semantic(sc); | |
3025 return s; | |
3026 } | |
3027 | |
3028 void CaseRangeStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
3029 { | |
3030 buf->writestring("case "); | |
3031 first->toCBuffer(buf, hgs); | |
3032 buf->writestring(": .. case "); | |
3033 last->toCBuffer(buf, hgs); | |
3034 buf->writenl(); | |
3035 statement->toCBuffer(buf, hgs); | |
3036 } | |
3037 | |
3038 #endif | |
3039 | |
159 | 3040 /******************************** DefaultStatement ***************************/ |
3041 | |
3042 DefaultStatement::DefaultStatement(Loc loc, Statement *s) | |
3043 : Statement(loc) | |
3044 { | |
3045 this->statement = s; | |
3046 #if IN_GCC | |
3047 + cblock = NULL; | |
3048 #endif | |
3049 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
|
3050 // LDC |
c6d6a68bb5db
Add back some enclosing scope-exit information to the frontend to produce
Christian Kamm <kamm incasoftware de>
parents:
1141
diff
changeset
|
3051 enclosingScopeExit = NULL; |
159 | 3052 } |
3053 | |
3054 Statement *DefaultStatement::syntaxCopy() | |
3055 { | |
3056 DefaultStatement *s = new DefaultStatement(loc, statement->syntaxCopy()); | |
3057 return s; | |
3058 } | |
3059 | |
3060 Statement *DefaultStatement::semantic(Scope *sc) | |
3061 { | |
336 | 3062 //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
|
3063 |
159 | 3064 if (sc->sw) |
3065 { | |
3066 if (sc->sw->sdefault) | |
3067 { | |
3068 error("switch statement already has a default"); | |
3069 } | |
3070 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
|
3071 |
c6d6a68bb5db
Add back some enclosing scope-exit information to the frontend to produce
Christian Kamm <kamm incasoftware de>
parents:
1141
diff
changeset
|
3072 // LDC |
c6d6a68bb5db
Add back some enclosing scope-exit information to the frontend to produce
Christian Kamm <kamm incasoftware de>
parents:
1141
diff
changeset
|
3073 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
|
3074 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
|
3075 { |
c6d6a68bb5db
Add back some enclosing scope-exit information to the frontend to produce
Christian Kamm <kamm incasoftware de>
parents:
1141
diff
changeset
|
3076 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
|
3077 } |
159 | 3078 } |
3079 else | |
3080 error("default not in switch statement"); | |
3081 statement = statement->semantic(sc); | |
3082 return this; | |
3083 } | |
3084 | |
3085 int DefaultStatement::usesEH() | |
3086 { | |
3087 return statement->usesEH(); | |
3088 } | |
3089 | |
336 | 3090 int DefaultStatement::blockExit() |
3091 { | |
3092 return statement->blockExit(); | |
3093 } | |
3094 | |
1587 | 3095 |
159 | 3096 int DefaultStatement::comeFrom() |
3097 { | |
3098 return TRUE; | |
3099 } | |
3100 | |
3101 void DefaultStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
3102 { | |
3103 buf->writestring("default:\n"); | |
3104 statement->toCBuffer(buf, hgs); | |
3105 } | |
3106 | |
3107 /******************************** GotoDefaultStatement ***************************/ | |
3108 | |
3109 GotoDefaultStatement::GotoDefaultStatement(Loc loc) | |
3110 : Statement(loc) | |
3111 { | |
3112 sw = NULL; | |
3113 } | |
3114 | |
3115 Statement *GotoDefaultStatement::syntaxCopy() | |
3116 { | |
3117 GotoDefaultStatement *s = new GotoDefaultStatement(loc); | |
3118 return s; | |
3119 } | |
3120 | |
3121 Statement *GotoDefaultStatement::semantic(Scope *sc) | |
3122 { | |
3123 sw = sc->sw; | |
3124 if (!sw) | |
3125 error("goto default not in switch statement"); | |
3126 return this; | |
3127 } | |
3128 | |
336 | 3129 int GotoDefaultStatement::blockExit() |
3130 { | |
3131 return BEgoto; | |
3132 } | |
3133 | |
1587 | 3134 |
159 | 3135 void GotoDefaultStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
3136 { | |
3137 buf->writestring("goto default;\n"); | |
3138 } | |
3139 | |
3140 /******************************** GotoCaseStatement ***************************/ | |
3141 | |
3142 GotoCaseStatement::GotoCaseStatement(Loc loc, Expression *exp) | |
3143 : Statement(loc) | |
3144 { | |
3145 cs = NULL; | |
3146 this->exp = exp; | |
3147 sw = NULL; | |
3148 } | |
3149 | |
3150 Statement *GotoCaseStatement::syntaxCopy() | |
3151 { | |
3152 Expression *e = exp ? exp->syntaxCopy() : NULL; | |
3153 GotoCaseStatement *s = new GotoCaseStatement(loc, e); | |
3154 return s; | |
3155 } | |
3156 | |
3157 Statement *GotoCaseStatement::semantic(Scope *sc) | |
3158 { | |
3159 if (exp) | |
3160 exp = exp->semantic(sc); | |
3161 | |
3162 if (!sc->sw) | |
3163 error("goto case not in switch statement"); | |
3164 else | |
3165 { | |
3166 sw = sc->sw; | |
3167 sc->sw->gotoCases.push(this); | |
3168 if (exp) | |
3169 { | |
3170 exp = exp->implicitCastTo(sc, sc->sw->condition->type); | |
3171 exp = exp->optimize(WANTvalue); | |
3172 } | |
3173 } | |
3174 return this; | |
3175 } | |
3176 | |
336 | 3177 int GotoCaseStatement::blockExit() |
3178 { | |
3179 return BEgoto; | |
3180 } | |
3181 | |
1587 | 3182 |
159 | 3183 void GotoCaseStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
3184 { | |
3185 buf->writestring("goto case"); | |
3186 if (exp) | |
3187 { buf->writebyte(' '); | |
3188 exp->toCBuffer(buf, hgs); | |
3189 } | |
3190 buf->writebyte(';'); | |
3191 buf->writenl(); | |
3192 } | |
3193 | |
3194 /******************************** SwitchErrorStatement ***************************/ | |
3195 | |
3196 SwitchErrorStatement::SwitchErrorStatement(Loc loc) | |
3197 : Statement(loc) | |
3198 { | |
3199 } | |
3200 | |
336 | 3201 int SwitchErrorStatement::blockExit() |
3202 { | |
3203 return BEthrow; | |
3204 } | |
3205 | |
1587 | 3206 |
159 | 3207 void SwitchErrorStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
3208 { | |
3209 buf->writestring("SwitchErrorStatement::toCBuffer()"); | |
3210 buf->writenl(); | |
3211 } | |
3212 | |
3213 /******************************** ReturnStatement ***************************/ | |
3214 | |
3215 ReturnStatement::ReturnStatement(Loc loc, Expression *exp) | |
3216 : Statement(loc) | |
3217 { | |
3218 this->exp = exp; | |
3219 } | |
3220 | |
3221 Statement *ReturnStatement::syntaxCopy() | |
3222 { | |
3223 Expression *e = NULL; | |
3224 if (exp) | |
3225 e = exp->syntaxCopy(); | |
3226 ReturnStatement *s = new ReturnStatement(loc, e); | |
3227 return s; | |
3228 } | |
3229 | |
3230 Statement *ReturnStatement::semantic(Scope *sc) | |
3231 { | |
3232 //printf("ReturnStatement::semantic() %s\n", toChars()); | |
3233 | |
3234 FuncDeclaration *fd = sc->parent->isFuncDeclaration(); | |
3235 Scope *scx = sc; | |
3236 int implicit0 = 0; | |
3237 | |
3238 if (sc->fes) | |
3239 { | |
3240 // Find scope of function foreach is in | |
3241 for (; 1; scx = scx->enclosing) | |
3242 { | |
3243 assert(scx); | |
3244 if (scx->func != fd) | |
3245 { fd = scx->func; // fd is now function enclosing foreach | |
3246 break; | |
3247 } | |
3248 } | |
3249 } | |
3250 | |
3251 Type *tret = fd->type->nextOf(); | |
3252 if (fd->tintro) | |
336 | 3253 /* We'll be implicitly casting the return expression to tintro |
3254 */ | |
159 | 3255 tret = fd->tintro->nextOf(); |
3256 Type *tbret = NULL; | |
3257 | |
3258 if (tret) | |
3259 tbret = tret->toBasetype(); | |
3260 | |
3261 // main() returns 0, even if it returns void | |
3262 if (!exp && (!tbret || tbret->ty == Tvoid) && fd->isMain()) | |
3263 { implicit0 = 1; | |
3264 exp = new IntegerExp(0); | |
3265 } | |
3266 | |
3267 if (sc->incontract || scx->incontract) | |
3268 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
|
3269 if (sc->enclosingFinally || scx->enclosingFinally) |
159 | 3270 error("return statements cannot be in finally, scope(exit) or scope(success) bodies"); |
3271 | |
3272 if (fd->isCtorDeclaration()) | |
3273 { | |
3274 // Constructors implicitly do: | |
3275 // return this; | |
3276 if (exp && exp->op != TOKthis) | |
3277 error("cannot return expression from constructor"); | |
3278 exp = new ThisExp(0); | |
3279 } | |
3280 | |
3281 if (!exp) | |
3282 fd->nrvo_can = 0; | |
3283 | |
3284 if (exp) | |
3285 { | |
3286 fd->hasReturnExp |= 1; | |
3287 | |
3288 exp = exp->semantic(sc); | |
3289 exp = resolveProperties(sc, exp); | |
3290 exp = exp->optimize(WANTvalue); | |
3291 | |
3292 if (fd->nrvo_can && exp->op == TOKvar) | |
3293 { VarExp *ve = (VarExp *)exp; | |
3294 VarDeclaration *v = ve->var->isVarDeclaration(); | |
3295 | |
3296 if (!v || v->isOut() || v->isRef()) | |
3297 fd->nrvo_can = 0; | |
3298 else if (fd->nrvo_var == NULL) | |
3299 { if (!v->isDataseg() && !v->isParameter() && v->toParent2() == fd) | |
1587 | 3300 { //printf("Setting nrvo to %s\n", v->toChars()); |
159 | 3301 fd->nrvo_var = v; |
1587 | 3302 } |
159 | 3303 else |
3304 fd->nrvo_can = 0; | |
3305 } | |
3306 else if (fd->nrvo_var != v) | |
3307 fd->nrvo_can = 0; | |
3308 } | |
3309 else | |
3310 fd->nrvo_can = 0; | |
3311 | |
3312 if (fd->returnLabel && tbret->ty != Tvoid) | |
3313 { | |
3314 } | |
3315 else if (fd->inferRetType) | |
3316 { | |
3317 if (fd->type->nextOf()) | |
3318 { | |
3319 if (!exp->type->equals(fd->type->nextOf())) | |
3320 error("mismatched function return type inference of %s and %s", | |
3321 exp->type->toChars(), fd->type->nextOf()->toChars()); | |
3322 } | |
3323 else | |
3324 { | |
3325 fd->type->next = exp->type; | |
3326 fd->type = fd->type->semantic(loc, sc); | |
3327 if (!fd->tintro) | |
3328 { tret = fd->type->nextOf(); | |
3329 tbret = tret->toBasetype(); | |
3330 } | |
3331 } | |
3332 } | |
3333 else if (tbret->ty != Tvoid) | |
3334 { | |
3335 exp = exp->implicitCastTo(sc, tret); | |
3336 } | |
3337 } | |
3338 else if (fd->inferRetType) | |
3339 { | |
3340 if (fd->type->nextOf()) | |
3341 { | |
3342 if (fd->type->nextOf()->ty != Tvoid) | |
3343 error("mismatched function return type inference of void and %s", | |
3344 fd->type->nextOf()->toChars()); | |
3345 } | |
3346 else | |
3347 { | |
3348 fd->type->next = Type::tvoid; | |
3349 fd->type = fd->type->semantic(loc, sc); | |
3350 if (!fd->tintro) | |
3351 { tret = Type::tvoid; | |
3352 tbret = tret; | |
3353 } | |
3354 } | |
3355 } | |
3356 else if (tbret->ty != Tvoid) // if non-void return | |
3357 error("return expression expected"); | |
3358 | |
3359 if (sc->fes) | |
3360 { | |
3361 Statement *s; | |
3362 | |
3363 if (exp && !implicit0) | |
3364 { | |
3365 exp = exp->implicitCastTo(sc, tret); | |
3366 } | |
3367 if (!exp || exp->op == TOKint64 || exp->op == TOKfloat64 || | |
3368 exp->op == TOKimaginary80 || exp->op == TOKcomplex80 || | |
3369 exp->op == TOKthis || exp->op == TOKsuper || exp->op == TOKnull || | |
3370 exp->op == TOKstring) | |
3371 { | |
3372 sc->fes->cases.push(this); | |
846
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
3373 // Construct: return cases.dim+1; |
159 | 3374 s = new ReturnStatement(0, new IntegerExp(sc->fes->cases.dim + 1)); |
3375 } | |
3376 else if (fd->type->nextOf()->toBasetype() == Type::tvoid) | |
3377 { | |
3378 s = new ReturnStatement(0, NULL); | |
3379 sc->fes->cases.push(s); | |
3380 | |
3381 // 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
|
3382 Statement *s1 = new ExpStatement(loc, exp); |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
3383 Statement *s2 = new ReturnStatement(0, new IntegerExp(sc->fes->cases.dim + 1)); |
159 | 3384 s = new CompoundStatement(loc, s1, s2); |
3385 } | |
3386 else | |
3387 { | |
3388 // Construct: return vresult; | |
3389 if (!fd->vresult) | |
846
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
3390 { // Declare vresult |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
3391 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
|
3392 v->noscope = 1; |
159 | 3393 v->semantic(scx); |
3394 if (!scx->insert(v)) | |
3395 assert(0); | |
3396 v->parent = fd; | |
3397 fd->vresult = v; | |
3398 } | |
3399 | |
846
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
3400 s = new ReturnStatement(0, new VarExp(0, fd->vresult)); |
159 | 3401 sc->fes->cases.push(s); |
3402 | |
3403 // 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
|
3404 exp = new AssignExp(loc, new VarExp(0, fd->vresult), exp); |
1587 | 3405 exp->op = TOKconstruct; |
159 | 3406 exp = exp->semantic(sc); |
846
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
3407 Statement *s1 = new ExpStatement(loc, exp); |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
3408 Statement *s2 = new ReturnStatement(0, new IntegerExp(sc->fes->cases.dim + 1)); |
159 | 3409 s = new CompoundStatement(loc, s1, s2); |
3410 } | |
3411 return s; | |
3412 } | |
3413 | |
3414 if (exp) | |
3415 { | |
3416 if (fd->returnLabel && tbret->ty != Tvoid) | |
3417 { | |
3418 assert(fd->vresult); | |
3419 VarExp *v = new VarExp(0, fd->vresult); | |
3420 | |
3421 exp = new AssignExp(loc, v, exp); | |
1587 | 3422 exp->op = TOKconstruct; |
159 | 3423 exp = exp->semantic(sc); |
3424 } | |
3425 //exp->dump(0); | |
3426 //exp->print(); | |
3427 exp->checkEscape(); | |
3428 } | |
3429 | |
3430 /* BUG: need to issue an error on: | |
3431 * this | |
3432 * { if (x) return; | |
3433 * super(); | |
3434 * } | |
3435 */ | |
3436 | |
3437 if (sc->callSuper & CSXany_ctor && | |
3438 !(sc->callSuper & (CSXthis_ctor | CSXsuper_ctor))) | |
3439 error("return without calling constructor"); | |
3440 | |
3441 sc->callSuper |= CSXreturn; | |
3442 | |
3443 // See if all returns are instead to be replaced with a goto returnLabel; | |
3444 if (fd->returnLabel) | |
3445 { | |
3446 GotoStatement *gs = new GotoStatement(loc, Id::returnLabel); | |
3447 | |
3448 gs->label = fd->returnLabel; | |
3449 if (exp) | |
846
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
3450 { /* Replace: return exp; |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
3451 * with: exp; goto returnLabel; |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
3452 */ |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
3453 Statement *s = new ExpStatement(0, exp); |
159 | 3454 return new CompoundStatement(loc, s, gs); |
3455 } | |
3456 return gs; | |
3457 } | |
3458 | |
1587 | 3459 if (exp && tbret->ty == Tvoid && !implicit0) |
336 | 3460 { |
3461 /* Replace: | |
3462 * return exp; | |
3463 * with: | |
3464 * exp; return; | |
3465 */ | |
3466 Statement *s = new ExpStatement(loc, exp); | |
1587 | 3467 exp = NULL; |
3468 s = s->semantic(sc); | |
159 | 3469 loc = 0; |
3470 return new CompoundStatement(loc, s, this); | |
3471 } | |
3472 | |
3473 return this; | |
3474 } | |
3475 | |
336 | 3476 int ReturnStatement::blockExit() |
3477 { int result = BEreturn; | |
3478 | |
3479 if (exp && exp->canThrow()) | |
3480 result |= BEthrow; | |
3481 return result; | |
3482 } | |
3483 | |
1587 | 3484 |
159 | 3485 void ReturnStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
3486 { | |
3487 buf->printf("return "); | |
3488 if (exp) | |
3489 exp->toCBuffer(buf, hgs); | |
3490 buf->writeByte(';'); | |
3491 buf->writenl(); | |
3492 } | |
3493 | |
3494 /******************************** BreakStatement ***************************/ | |
3495 | |
3496 BreakStatement::BreakStatement(Loc loc, Identifier *ident) | |
3497 : Statement(loc) | |
3498 { | |
3499 this->ident = ident; | |
3500 } | |
3501 | |
3502 Statement *BreakStatement::syntaxCopy() | |
3503 { | |
3504 BreakStatement *s = new BreakStatement(loc, ident); | |
3505 return s; | |
3506 } | |
3507 | |
3508 Statement *BreakStatement::semantic(Scope *sc) | |
3509 { | |
336 | 3510 //printf("BreakStatement::semantic()\n"); |
159 | 3511 // If: |
3512 // break Identifier; | |
3513 if (ident) | |
3514 { | |
3515 Scope *scx; | |
3516 FuncDeclaration *thisfunc = sc->func; | |
3517 | |
3518 for (scx = sc; scx; scx = scx->enclosing) | |
3519 { | |
3520 LabelStatement *ls; | |
3521 | |
3522 if (scx->func != thisfunc) // if in enclosing function | |
3523 { | |
3524 if (sc->fes) // if this is the body of a foreach | |
3525 { | |
3526 /* Post this statement to the fes, and replace | |
3527 * it with a return value that caller will put into | |
3528 * a switch. Caller will figure out where the break | |
3529 * label actually is. | |
3530 * Case numbers start with 2, not 0, as 0 is continue | |
3531 * and 1 is break. | |
3532 */ | |
3533 Statement *s; | |
3534 sc->fes->cases.push(this); | |
3535 s = new ReturnStatement(0, new IntegerExp(sc->fes->cases.dim + 1)); | |
3536 return s; | |
3537 } | |
3538 break; // can't break to it | |
3539 } | |
3540 | |
3541 ls = scx->slabel; | |
3542 if (ls && ls->ident == ident) | |
3543 { | |
3544 Statement *s = ls->statement; | |
3545 | |
3546 if (!s->hasBreak()) | |
3547 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
|
3548 if (ls->enclosingFinally != sc->enclosingFinally) |
159 | 3549 error("cannot break out of finally block"); |
3550 | |
3551 this->target = ls; | |
3552 return this; | |
3553 } | |
3554 } | |
3555 error("enclosing label '%s' for break not found", ident->toChars()); | |
3556 } | |
3557 else if (!sc->sbreak) | |
3558 { | |
3559 if (sc->fes) | |
3560 { Statement *s; | |
3561 | |
3562 // Replace break; with return 1; | |
3563 s = new ReturnStatement(0, new IntegerExp(1)); | |
3564 return s; | |
3565 } | |
3566 error("break is not inside a loop or switch"); | |
3567 } | |
3568 return this; | |
3569 } | |
3570 | |
336 | 3571 int BreakStatement::blockExit() |
3572 { | |
3573 //printf("BreakStatement::blockExit(%p) = x%x\n", this, ident ? BEgoto : BEbreak); | |
3574 return ident ? BEgoto : BEbreak; | |
3575 } | |
3576 | |
1587 | 3577 |
159 | 3578 void BreakStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
3579 { | |
3580 buf->writestring("break"); | |
3581 if (ident) | |
3582 { buf->writebyte(' '); | |
3583 buf->writestring(ident->toChars()); | |
3584 } | |
3585 buf->writebyte(';'); | |
3586 buf->writenl(); | |
3587 } | |
3588 | |
3589 /******************************** ContinueStatement ***************************/ | |
3590 | |
3591 ContinueStatement::ContinueStatement(Loc loc, Identifier *ident) | |
3592 : Statement(loc) | |
3593 { | |
3594 this->ident = ident; | |
3595 } | |
3596 | |
3597 Statement *ContinueStatement::syntaxCopy() | |
3598 { | |
3599 ContinueStatement *s = new ContinueStatement(loc, ident); | |
3600 return s; | |
3601 } | |
3602 | |
3603 Statement *ContinueStatement::semantic(Scope *sc) | |
3604 { | |
3605 //printf("ContinueStatement::semantic() %p\n", this); | |
3606 if (ident) | |
3607 { | |
3608 Scope *scx; | |
3609 FuncDeclaration *thisfunc = sc->func; | |
3610 | |
3611 for (scx = sc; scx; scx = scx->enclosing) | |
3612 { | |
3613 LabelStatement *ls; | |
3614 | |
3615 if (scx->func != thisfunc) // if in enclosing function | |
3616 { | |
3617 if (sc->fes) // if this is the body of a foreach | |
3618 { | |
3619 for (; scx; scx = scx->enclosing) | |
3620 { | |
3621 ls = scx->slabel; | |
3622 if (ls && ls->ident == ident && ls->statement == sc->fes) | |
3623 { | |
3624 // Replace continue ident; with return 0; | |
3625 return new ReturnStatement(0, new IntegerExp(0)); | |
3626 } | |
3627 } | |
3628 | |
3629 /* Post this statement to the fes, and replace | |
3630 * it with a return value that caller will put into | |
3631 * a switch. Caller will figure out where the break | |
3632 * label actually is. | |
3633 * Case numbers start with 2, not 0, as 0 is continue | |
3634 * and 1 is break. | |
3635 */ | |
3636 Statement *s; | |
3637 sc->fes->cases.push(this); | |
3638 s = new ReturnStatement(0, new IntegerExp(sc->fes->cases.dim + 1)); | |
3639 return s; | |
3640 } | |
3641 break; // can't continue to it | |
3642 } | |
3643 | |
3644 ls = scx->slabel; | |
3645 if (ls && ls->ident == ident) | |
3646 { | |
3647 Statement *s = ls->statement; | |
3648 | |
3649 if (!s->hasContinue()) | |
3650 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
|
3651 if (ls->enclosingFinally != sc->enclosingFinally) |
159 | 3652 error("cannot continue out of finally block"); |
3653 | |
3654 this->target = ls; | |
3655 return this; | |
3656 } | |
3657 } | |
3658 error("enclosing label '%s' for continue not found", ident->toChars()); | |
3659 } | |
3660 else if (!sc->scontinue) | |
3661 { | |
3662 if (sc->fes) | |
3663 { Statement *s; | |
3664 | |
3665 // Replace continue; with return 0; | |
3666 s = new ReturnStatement(0, new IntegerExp(0)); | |
3667 return s; | |
3668 } | |
3669 error("continue is not inside a loop"); | |
3670 } | |
3671 return this; | |
3672 } | |
3673 | |
336 | 3674 int ContinueStatement::blockExit() |
3675 { | |
3676 return ident ? BEgoto : BEcontinue; | |
3677 } | |
3678 | |
1587 | 3679 |
159 | 3680 void ContinueStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
3681 { | |
3682 buf->writestring("continue"); | |
3683 if (ident) | |
3684 { buf->writebyte(' '); | |
3685 buf->writestring(ident->toChars()); | |
3686 } | |
3687 buf->writebyte(';'); | |
3688 buf->writenl(); | |
3689 } | |
3690 | |
3691 /******************************** SynchronizedStatement ***************************/ | |
3692 | |
3693 SynchronizedStatement::SynchronizedStatement(Loc loc, Expression *exp, Statement *body) | |
3694 : Statement(loc) | |
3695 { | |
3696 this->exp = exp; | |
3697 this->body = body; | |
3698 this->esync = NULL; | |
664
eef8ac26c66c
Some missed LLVMDC -> LDC.
Christian Kamm <kamm incasoftware de>
parents:
510
diff
changeset
|
3699 // LDC |
309 | 3700 this->llsync = NULL; |
159 | 3701 } |
3702 | |
3703 SynchronizedStatement::SynchronizedStatement(Loc loc, elem *esync, Statement *body) | |
3704 : Statement(loc) | |
3705 { | |
3706 this->exp = NULL; | |
3707 this->body = body; | |
3708 this->esync = esync; | |
664
eef8ac26c66c
Some missed LLVMDC -> LDC.
Christian Kamm <kamm incasoftware de>
parents:
510
diff
changeset
|
3709 // LDC |
309 | 3710 this->llsync = NULL; |
159 | 3711 } |
3712 | |
3713 Statement *SynchronizedStatement::syntaxCopy() | |
3714 { | |
3715 Expression *e = exp ? exp->syntaxCopy() : NULL; | |
3716 SynchronizedStatement *s = new SynchronizedStatement(loc, e, body ? body->syntaxCopy() : NULL); | |
3717 return s; | |
3718 } | |
3719 | |
3720 Statement *SynchronizedStatement::semantic(Scope *sc) | |
3721 { | |
3722 if (exp) | |
1587 | 3723 { |
159 | 3724 exp = exp->semantic(sc); |
3725 exp = resolveProperties(sc, exp); | |
1587 | 3726 ClassDeclaration *cd = exp->type->isClassHandle(); |
159 | 3727 if (!cd) |
3728 error("can only synchronize on class objects, not '%s'", exp->type->toChars()); | |
3729 else if (cd->isInterfaceDeclaration()) | |
1587 | 3730 { /* Cast the interface to an object, as the object has the monitor, |
3731 * not the interface. | |
3732 */ | |
3733 Type *t = new TypeIdentifier(0, Id::Object); | |
159 | 3734 |
3735 t = t->semantic(0, sc); | |
3736 exp = new CastExp(loc, exp, t); | |
3737 exp = exp->semantic(sc); | |
3738 } | |
1587 | 3739 |
3740 #if 0 | |
3741 /* Rewrite as: | |
3742 * auto tmp = exp; | |
3743 * _d_monitorenter(tmp); | |
3744 * try { body } finally { _d_monitorexit(tmp); } | |
3745 */ | |
3746 Identifier *id = Lexer::uniqueId("__sync"); | |
3747 ExpInitializer *ie = new ExpInitializer(loc, exp); | |
3748 VarDeclaration *tmp = new VarDeclaration(loc, exp->type, id, ie); | |
3749 | |
3750 Statements *cs = new Statements(); | |
3751 cs->push(new DeclarationStatement(loc, new DeclarationExp(loc, tmp))); | |
3752 | |
3753 FuncDeclaration *fdenter = FuncDeclaration::genCfunc(Type::tvoid, Id::monitorenter); | |
3754 Expression *e = new CallExp(loc, new VarExp(loc, fdenter), new VarExp(loc, tmp)); | |
3755 e->type = Type::tvoid; // do not run semantic on e | |
3756 cs->push(new ExpStatement(loc, e)); | |
3757 | |
3758 FuncDeclaration *fdexit = FuncDeclaration::genCfunc(Type::tvoid, Id::monitorexit); | |
3759 e = new CallExp(loc, new VarExp(loc, fdexit), new VarExp(loc, tmp)); | |
3760 e->type = Type::tvoid; // do not run semantic on e | |
3761 Statement *s = new ExpStatement(loc, e); | |
3762 s = new TryFinallyStatement(loc, body, s); | |
3763 cs->push(s); | |
3764 | |
3765 s = new CompoundStatement(loc, cs); | |
3766 return s->semantic(sc); | |
3767 #endif | |
159 | 3768 } |
1587 | 3769 #if 0 |
3770 else | |
3771 { /* Generate our own critical section, then rewrite as: | |
3772 * __gshared byte[CriticalSection.sizeof] critsec; | |
3773 * _d_criticalenter(critsec.ptr); | |
3774 * try { body } finally { _d_criticalexit(critsec.ptr); } | |
3775 */ | |
3776 Identifier *id = Lexer::uniqueId("__critsec"); | |
3777 Type *t = new TypeSArray(Type::tint8, new IntegerExp(PTRSIZE + os_critsecsize())); | |
3778 VarDeclaration *tmp = new VarDeclaration(loc, t, id, NULL); | |
3779 tmp->storage_class |= STCgshared | STCstatic; | |
3780 | |
3781 Statements *cs = new Statements(); | |
3782 cs->push(new DeclarationStatement(loc, new DeclarationExp(loc, tmp))); | |
3783 | |
3784 FuncDeclaration *fdenter = FuncDeclaration::genCfunc(Type::tvoid, Id::criticalenter); | |
3785 Expression *e = new DotIdExp(loc, new VarExp(loc, tmp), Id::ptr); | |
3786 e = e->semantic(sc); | |
3787 e = new CallExp(loc, new VarExp(loc, fdenter), e); | |
3788 e->type = Type::tvoid; // do not run semantic on e | |
3789 cs->push(new ExpStatement(loc, e)); | |
3790 | |
3791 FuncDeclaration *fdexit = FuncDeclaration::genCfunc(Type::tvoid, Id::criticalexit); | |
3792 e = new DotIdExp(loc, new VarExp(loc, tmp), Id::ptr); | |
3793 e = e->semantic(sc); | |
3794 e = new CallExp(loc, new VarExp(loc, fdexit), e); | |
3795 e->type = Type::tvoid; // do not run semantic on e | |
3796 Statement *s = new ExpStatement(loc, e); | |
3797 s = new TryFinallyStatement(loc, body, s); | |
3798 cs->push(s); | |
3799 | |
3800 s = new CompoundStatement(loc, cs); | |
3801 return s->semantic(sc); | |
3802 } | |
3803 #endif | |
159 | 3804 if (body) |
309 | 3805 { |
1141
f99a3b393c03
Reorganize EnclosingHandlers to require less changes to the frontend and allow us to
Christian Kamm <kamm incasoftware de>
parents:
1133
diff
changeset
|
3806 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
|
3807 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
|
3808 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
|
3809 sc->enclosingScopeExit = oldScopeExit; |
309 | 3810 } |
159 | 3811 return this; |
3812 } | |
3813 | |
3814 int SynchronizedStatement::hasBreak() | |
3815 { | |
3816 return FALSE; //TRUE; | |
3817 } | |
3818 | |
3819 int SynchronizedStatement::hasContinue() | |
3820 { | |
3821 return FALSE; //TRUE; | |
3822 } | |
3823 | |
3824 int SynchronizedStatement::usesEH() | |
3825 { | |
3826 return TRUE; | |
3827 } | |
3828 | |
336 | 3829 int SynchronizedStatement::blockExit() |
3830 { | |
3831 return body ? body->blockExit() : BEfallthru; | |
3832 } | |
3833 | |
1587 | 3834 |
159 | 3835 void SynchronizedStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
3836 { | |
3837 buf->writestring("synchronized"); | |
3838 if (exp) | |
3839 { buf->writebyte('('); | |
3840 exp->toCBuffer(buf, hgs); | |
3841 buf->writebyte(')'); | |
3842 } | |
3843 if (body) | |
3844 { | |
3845 buf->writebyte(' '); | |
3846 body->toCBuffer(buf, hgs); | |
3847 } | |
3848 } | |
3849 | |
3850 /******************************** WithStatement ***************************/ | |
3851 | |
3852 WithStatement::WithStatement(Loc loc, Expression *exp, Statement *body) | |
3853 : Statement(loc) | |
3854 { | |
3855 this->exp = exp; | |
3856 this->body = body; | |
3857 wthis = NULL; | |
3858 } | |
3859 | |
3860 Statement *WithStatement::syntaxCopy() | |
3861 { | |
3862 WithStatement *s = new WithStatement(loc, exp->syntaxCopy(), body ? body->syntaxCopy() : NULL); | |
3863 return s; | |
3864 } | |
3865 | |
3866 Statement *WithStatement::semantic(Scope *sc) | |
3867 { ScopeDsymbol *sym; | |
3868 Initializer *init; | |
3869 | |
3870 //printf("WithStatement::semantic()\n"); | |
3871 exp = exp->semantic(sc); | |
3872 exp = resolveProperties(sc, exp); | |
3873 if (exp->op == TOKimport) | |
3874 { ScopeExp *es = (ScopeExp *)exp; | |
3875 | |
3876 sym = es->sds; | |
3877 } | |
3878 else if (exp->op == TOKtype) | |
3879 { TypeExp *es = (TypeExp *)exp; | |
3880 | |
3881 sym = es->type->toDsymbol(sc)->isScopeDsymbol(); | |
3882 if (!sym) | |
3883 { error("%s has no members", es->toChars()); | |
3884 body = body->semantic(sc); | |
3885 return this; | |
3886 } | |
3887 } | |
3888 else | |
3889 { Type *t = exp->type; | |
3890 | |
3891 assert(t); | |
3892 t = t->toBasetype(); | |
3893 if (t->isClassHandle()) | |
3894 { | |
3895 init = new ExpInitializer(loc, exp); | |
3896 wthis = new VarDeclaration(loc, exp->type, Id::withSym, init); | |
3897 wthis->semantic(sc); | |
3898 | |
3899 sym = new WithScopeSymbol(this); | |
3900 sym->parent = sc->scopesym; | |
3901 } | |
3902 else if (t->ty == Tstruct) | |
3903 { | |
3904 Expression *e = exp->addressOf(sc); | |
3905 init = new ExpInitializer(loc, e); | |
3906 wthis = new VarDeclaration(loc, e->type, Id::withSym, init); | |
3907 wthis->semantic(sc); | |
3908 sym = new WithScopeSymbol(this); | |
3909 sym->parent = sc->scopesym; | |
3910 } | |
3911 else | |
3912 { error("with expressions must be class objects, not '%s'", exp->type->toChars()); | |
3913 return NULL; | |
3914 } | |
3915 } | |
3916 sc = sc->push(sym); | |
3917 | |
3918 if (body) | |
3919 body = body->semantic(sc); | |
3920 | |
3921 sc->pop(); | |
3922 | |
3923 return this; | |
3924 } | |
3925 | |
3926 void WithStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
3927 { | |
3928 buf->writestring("with ("); | |
3929 exp->toCBuffer(buf, hgs); | |
3930 buf->writestring(")\n"); | |
3931 if (body) | |
3932 body->toCBuffer(buf, hgs); | |
3933 } | |
3934 | |
3935 int WithStatement::usesEH() | |
3936 { | |
3937 return body ? body->usesEH() : 0; | |
3938 } | |
3939 | |
336 | 3940 int WithStatement::blockExit() |
3941 { | |
3942 int result = BEnone; | |
3943 if (exp->canThrow()) | |
3944 result = BEthrow; | |
3945 if (body) | |
3946 result |= body->blockExit(); | |
3947 else | |
3948 result |= BEfallthru; | |
3949 return result; | |
3950 } | |
3951 | |
1587 | 3952 |
159 | 3953 /******************************** TryCatchStatement ***************************/ |
3954 | |
3955 TryCatchStatement::TryCatchStatement(Loc loc, Statement *body, Array *catches) | |
3956 : Statement(loc) | |
3957 { | |
3958 this->body = body; | |
3959 this->catches = catches; | |
3960 } | |
3961 | |
3962 Statement *TryCatchStatement::syntaxCopy() | |
3963 { | |
3964 Array *a = new Array(); | |
3965 a->setDim(catches->dim); | |
3966 for (int i = 0; i < a->dim; i++) | |
3967 { Catch *c; | |
3968 | |
3969 c = (Catch *)catches->data[i]; | |
3970 c = c->syntaxCopy(); | |
3971 a->data[i] = c; | |
3972 } | |
3973 TryCatchStatement *s = new TryCatchStatement(loc, body->syntaxCopy(), a); | |
3974 return s; | |
3975 } | |
3976 | |
3977 Statement *TryCatchStatement::semantic(Scope *sc) | |
3978 { | |
3979 body = body->semanticScope(sc, NULL /*this*/, NULL); | |
3980 | |
336 | 3981 /* Even if body is NULL, still do semantic analysis on catches |
3982 */ | |
3983 for (size_t i = 0; i < catches->dim; i++) | |
3984 { Catch *c = (Catch *)catches->data[i]; | |
159 | 3985 c->semantic(sc); |
3986 | |
3987 // Determine if current catch 'hides' any previous catches | |
336 | 3988 for (size_t j = 0; j < i; j++) |
159 | 3989 { Catch *cj = (Catch *)catches->data[j]; |
3990 char *si = c->loc.toChars(); | |
3991 char *sj = cj->loc.toChars(); | |
3992 | |
3993 if (c->type->toBasetype()->implicitConvTo(cj->type->toBasetype())) | |
3994 error("catch at %s hides catch at %s", sj, si); | |
3995 } | |
3996 } | |
1587 | 3997 |
3998 if (!body || body->isEmpty()) | |
3999 { | |
4000 return NULL; | |
4001 } | |
159 | 4002 return this; |
4003 } | |
4004 | |
4005 int TryCatchStatement::hasBreak() | |
4006 { | |
4007 return FALSE; //TRUE; | |
4008 } | |
4009 | |
4010 int TryCatchStatement::usesEH() | |
4011 { | |
4012 return TRUE; | |
4013 } | |
4014 | |
336 | 4015 int TryCatchStatement::blockExit() |
1587 | 4016 { |
336 | 4017 assert(body); |
1587 | 4018 int result = body->blockExit(); |
336 | 4019 |
4020 for (size_t i = 0; i < catches->dim; i++) | |
4021 { | |
4022 Catch *c = (Catch *)catches->data[i]; | |
4023 result |= c->blockExit(); | |
4024 } | |
4025 return result; | |
4026 } | |
4027 | |
159 | 4028 void TryCatchStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
4029 { | |
4030 buf->writestring("try"); | |
4031 buf->writenl(); | |
4032 if (body) | |
4033 body->toCBuffer(buf, hgs); | |
336 | 4034 for (size_t i = 0; i < catches->dim; i++) |
159 | 4035 { |
4036 Catch *c = (Catch *)catches->data[i]; | |
4037 c->toCBuffer(buf, hgs); | |
4038 } | |
4039 } | |
4040 | |
4041 /******************************** Catch ***************************/ | |
4042 | |
4043 Catch::Catch(Loc loc, Type *t, Identifier *id, Statement *handler) | |
4044 { | |
4045 //printf("Catch(%s, loc = %s)\n", id->toChars(), loc.toChars()); | |
4046 this->loc = loc; | |
4047 this->type = t; | |
4048 this->ident = id; | |
4049 this->handler = handler; | |
4050 var = NULL; | |
4051 } | |
4052 | |
4053 Catch *Catch::syntaxCopy() | |
4054 { | |
4055 Catch *c = new Catch(loc, | |
4056 (type ? type->syntaxCopy() : NULL), | |
4057 ident, | |
4058 (handler ? handler->syntaxCopy() : NULL)); | |
4059 return c; | |
4060 } | |
4061 | |
4062 void Catch::semantic(Scope *sc) | |
4063 { ScopeDsymbol *sym; | |
4064 | |
4065 //printf("Catch::semantic(%s)\n", ident->toChars()); | |
4066 | |
4067 #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
|
4068 if (sc->enclosingFinally) |
159 | 4069 { |
4070 /* This is because the _d_local_unwind() gets the stack munged | |
4071 * up on this. The workaround is to place any try-catches into | |
4072 * a separate function, and call that. | |
4073 * To fix, have the compiler automatically convert the finally | |
4074 * body into a nested function. | |
4075 */ | |
4076 error(loc, "cannot put catch statement inside finally block"); | |
4077 } | |
4078 #endif | |
4079 | |
4080 sym = new ScopeDsymbol(); | |
4081 sym->parent = sc->scopesym; | |
4082 sc = sc->push(sym); | |
4083 | |
4084 if (!type) | |
4085 type = new TypeIdentifier(0, Id::Object); | |
4086 type = type->semantic(loc, sc); | |
4087 if (!type->toBasetype()->isClassHandle()) | |
4088 error("can only catch class objects, not '%s'", type->toChars()); | |
4089 else if (ident) | |
4090 { | |
4091 var = new VarDeclaration(loc, type, ident, NULL); | |
4092 var->parent = sc->parent; | |
4093 sc->insert(var); | |
4094 } | |
4095 handler = handler->semantic(sc); | |
4096 | |
4097 sc->pop(); | |
4098 } | |
4099 | |
336 | 4100 int Catch::blockExit() |
4101 { | |
4102 return handler ? handler->blockExit() : BEfallthru; | |
4103 } | |
4104 | |
159 | 4105 void Catch::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
4106 { | |
4107 buf->writestring("catch"); | |
4108 if (type) | |
4109 { buf->writebyte('('); | |
4110 type->toCBuffer(buf, ident, hgs); | |
4111 buf->writebyte(')'); | |
4112 } | |
4113 buf->writenl(); | |
4114 buf->writebyte('{'); | |
4115 buf->writenl(); | |
336 | 4116 if (handler) |
4117 handler->toCBuffer(buf, hgs); | |
159 | 4118 buf->writebyte('}'); |
4119 buf->writenl(); | |
4120 } | |
4121 | |
4122 /****************************** TryFinallyStatement ***************************/ | |
4123 | |
4124 TryFinallyStatement::TryFinallyStatement(Loc loc, Statement *body, Statement *finalbody) | |
4125 : Statement(loc) | |
4126 { | |
4127 this->body = body; | |
4128 this->finalbody = finalbody; | |
4129 } | |
4130 | |
4131 Statement *TryFinallyStatement::syntaxCopy() | |
4132 { | |
4133 TryFinallyStatement *s = new TryFinallyStatement(loc, | |
4134 body->syntaxCopy(), finalbody->syntaxCopy()); | |
4135 return s; | |
4136 } | |
4137 | |
4138 Statement *TryFinallyStatement::semantic(Scope *sc) | |
4139 { | |
4140 //printf("TryFinallyStatement::semantic()\n"); | |
4141 | |
1141
f99a3b393c03
Reorganize EnclosingHandlers to require less changes to the frontend and allow us to
Christian Kamm <kamm incasoftware de>
parents:
1133
diff
changeset
|
4142 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
|
4143 sc->enclosingScopeExit = this; |
159 | 4144 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
|
4145 sc->enclosingScopeExit = oldScopeExit; |
159 | 4146 |
4147 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
|
4148 sc->enclosingFinally = this; |
159 | 4149 sc->sbreak = NULL; |
4150 sc->scontinue = NULL; // no break or continue out of finally block | |
4151 finalbody = finalbody->semantic(sc); | |
4152 sc->pop(); | |
4153 return this; | |
4154 } | |
4155 | |
4156 void TryFinallyStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
4157 { | |
4158 buf->printf("try\n{\n"); | |
4159 body->toCBuffer(buf, hgs); | |
4160 buf->printf("}\nfinally\n{\n"); | |
4161 finalbody->toCBuffer(buf, hgs); | |
4162 buf->writeByte('}'); | |
4163 buf->writenl(); | |
4164 } | |
4165 | |
4166 int TryFinallyStatement::hasBreak() | |
4167 { | |
4168 return FALSE; //TRUE; | |
4169 } | |
4170 | |
4171 int TryFinallyStatement::hasContinue() | |
4172 { | |
4173 return FALSE; //TRUE; | |
4174 } | |
4175 | |
4176 int TryFinallyStatement::usesEH() | |
4177 { | |
4178 return TRUE; | |
4179 } | |
4180 | |
336 | 4181 int TryFinallyStatement::blockExit() |
4182 { | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4183 if (body) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4184 return body->blockExit(); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4185 return BEfallthru; |
336 | 4186 } |
4187 | |
159 | 4188 |
4189 /****************************** OnScopeStatement ***************************/ | |
4190 | |
4191 OnScopeStatement::OnScopeStatement(Loc loc, TOK tok, Statement *statement) | |
4192 : Statement(loc) | |
4193 { | |
4194 this->tok = tok; | |
4195 this->statement = statement; | |
4196 } | |
4197 | |
4198 Statement *OnScopeStatement::syntaxCopy() | |
4199 { | |
4200 OnScopeStatement *s = new OnScopeStatement(loc, | |
4201 tok, statement->syntaxCopy()); | |
4202 return s; | |
4203 } | |
4204 | |
4205 Statement *OnScopeStatement::semantic(Scope *sc) | |
4206 { | |
4207 /* semantic is called on results of scopeCode() */ | |
4208 return this; | |
4209 } | |
4210 | |
336 | 4211 int OnScopeStatement::blockExit() |
4212 { // At this point, this statement is just an empty placeholder | |
4213 return BEfallthru; | |
4214 } | |
4215 | |
159 | 4216 void OnScopeStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
4217 { | |
4218 buf->writestring(Token::toChars(tok)); | |
4219 buf->writebyte(' '); | |
4220 statement->toCBuffer(buf, hgs); | |
4221 } | |
4222 | |
4223 int OnScopeStatement::usesEH() | |
4224 { | |
4225 return (tok != TOKon_scope_success); | |
4226 } | |
4227 | |
1587 | 4228 void OnScopeStatement::scopeCode(Scope *sc, Statement **sentry, Statement **sexception, Statement **sfinally) |
159 | 4229 { |
4230 //printf("OnScopeStatement::scopeCode()\n"); | |
4231 //print(); | |
4232 *sentry = NULL; | |
4233 *sexception = NULL; | |
4234 *sfinally = NULL; | |
4235 switch (tok) | |
4236 { | |
4237 case TOKon_scope_exit: | |
4238 *sfinally = statement; | |
4239 break; | |
4240 | |
4241 case TOKon_scope_failure: | |
4242 *sexception = statement; | |
4243 break; | |
4244 | |
4245 case TOKon_scope_success: | |
4246 { | |
4247 /* Create: | |
4248 * sentry: int x = 0; | |
4249 * sexception: x = 1; | |
4250 * sfinally: if (!x) statement; | |
4251 */ | |
846
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
4252 Identifier *id = Lexer::uniqueId("__os"); |
159 | 4253 |
4254 ExpInitializer *ie = new ExpInitializer(loc, new IntegerExp(0)); | |
4255 VarDeclaration *v = new VarDeclaration(loc, Type::tint32, id, ie); | |
4256 *sentry = new DeclarationStatement(loc, v); | |
4257 | |
4258 Expression *e = new IntegerExp(1); | |
4259 e = new AssignExp(0, new VarExp(0, v), e); | |
4260 *sexception = new ExpStatement(0, e); | |
4261 | |
4262 e = new VarExp(0, v); | |
4263 e = new NotExp(0, e); | |
4264 *sfinally = new IfStatement(0, NULL, e, statement, NULL); | |
4265 | |
4266 break; | |
4267 } | |
4268 | |
4269 default: | |
4270 assert(0); | |
4271 } | |
4272 } | |
4273 | |
4274 /******************************** ThrowStatement ***************************/ | |
4275 | |
4276 ThrowStatement::ThrowStatement(Loc loc, Expression *exp) | |
4277 : Statement(loc) | |
4278 { | |
4279 this->exp = exp; | |
4280 } | |
4281 | |
4282 Statement *ThrowStatement::syntaxCopy() | |
4283 { | |
4284 ThrowStatement *s = new ThrowStatement(loc, exp->syntaxCopy()); | |
4285 return s; | |
4286 } | |
4287 | |
4288 Statement *ThrowStatement::semantic(Scope *sc) | |
4289 { | |
4290 //printf("ThrowStatement::semantic()\n"); | |
4291 | |
4292 FuncDeclaration *fd = sc->parent->isFuncDeclaration(); | |
4293 fd->hasReturnExp |= 2; | |
4294 | |
4295 if (sc->incontract) | |
4296 error("Throw statements cannot be in contracts"); | |
4297 exp = exp->semantic(sc); | |
4298 exp = resolveProperties(sc, exp); | |
4299 if (!exp->type->toBasetype()->isClassHandle()) | |
4300 error("can only throw class objects, not type %s", exp->type->toChars()); | |
4301 return this; | |
4302 } | |
4303 | |
336 | 4304 int ThrowStatement::blockExit() |
4305 { | |
4306 return BEthrow; // obviously | |
4307 } | |
4308 | |
1587 | 4309 |
159 | 4310 void ThrowStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
4311 { | |
4312 buf->printf("throw "); | |
4313 exp->toCBuffer(buf, hgs); | |
4314 buf->writeByte(';'); | |
4315 buf->writenl(); | |
4316 } | |
4317 | |
4318 /******************************** VolatileStatement **************************/ | |
4319 | |
4320 VolatileStatement::VolatileStatement(Loc loc, Statement *statement) | |
4321 : Statement(loc) | |
4322 { | |
4323 this->statement = statement; | |
4324 } | |
4325 | |
4326 Statement *VolatileStatement::syntaxCopy() | |
4327 { | |
4328 VolatileStatement *s = new VolatileStatement(loc, | |
4329 statement ? statement->syntaxCopy() : NULL); | |
4330 return s; | |
4331 } | |
4332 | |
4333 Statement *VolatileStatement::semantic(Scope *sc) | |
4334 { | |
309 | 4335 if (statement) |
4336 { | |
1141
f99a3b393c03
Reorganize EnclosingHandlers to require less changes to the frontend and allow us to
Christian Kamm <kamm incasoftware de>
parents:
1133
diff
changeset
|
4337 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
|
4338 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
|
4339 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
|
4340 sc->enclosingScopeExit = oldScopeExit; |
309 | 4341 } |
159 | 4342 return this; |
4343 } | |
4344 | |
4345 Statements *VolatileStatement::flatten(Scope *sc) | |
4346 { | |
4347 Statements *a; | |
4348 | |
4349 a = statement ? statement->flatten(sc) : NULL; | |
4350 if (a) | |
4351 { for (int i = 0; i < a->dim; i++) | |
4352 { Statement *s = (Statement *)a->data[i]; | |
4353 | |
4354 s = new VolatileStatement(loc, s); | |
4355 a->data[i] = s; | |
4356 } | |
4357 } | |
4358 | |
4359 return a; | |
4360 } | |
4361 | |
336 | 4362 int VolatileStatement::blockExit() |
4363 { | |
4364 return statement ? statement->blockExit() : BEfallthru; | |
4365 } | |
4366 | |
1587 | 4367 |
159 | 4368 void VolatileStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
4369 { | |
4370 buf->writestring("volatile"); | |
4371 if (statement) | |
4372 { if (statement->isScopeStatement()) | |
4373 buf->writenl(); | |
4374 else | |
4375 buf->writebyte(' '); | |
4376 statement->toCBuffer(buf, hgs); | |
4377 } | |
4378 } | |
4379 | |
4380 | |
4381 /******************************** GotoStatement ***************************/ | |
4382 | |
4383 GotoStatement::GotoStatement(Loc loc, Identifier *ident) | |
4384 : Statement(loc) | |
4385 { | |
4386 this->ident = ident; | |
4387 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
|
4388 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
|
4389 this->enclosingScopeExit = NULL; |
159 | 4390 } |
4391 | |
4392 Statement *GotoStatement::syntaxCopy() | |
4393 { | |
4394 GotoStatement *s = new GotoStatement(loc, ident); | |
4395 return s; | |
4396 } | |
4397 | |
4398 Statement *GotoStatement::semantic(Scope *sc) | |
4399 { FuncDeclaration *fd = sc->parent->isFuncDeclaration(); | |
4400 | |
4401 //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
|
4402 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
|
4403 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
|
4404 |
159 | 4405 label = fd->searchLabel(ident); |
4406 if (!label->statement && sc->fes) | |
4407 { | |
4408 /* Either the goto label is forward referenced or it | |
4409 * is in the function that the enclosing foreach is in. | |
4410 * Can't know yet, so wrap the goto in a compound statement | |
4411 * so we can patch it later, and add it to a 'look at this later' | |
4412 * list. | |
4413 */ | |
4414 Statements *a = new Statements(); | |
4415 Statement *s; | |
4416 | |
4417 a->push(this); | |
4418 s = new CompoundStatement(loc, a); | |
4419 sc->fes->gotos.push(s); // 'look at this later' list | |
4420 return s; | |
4421 } | |
1141
f99a3b393c03
Reorganize EnclosingHandlers to require less changes to the frontend and allow us to
Christian Kamm <kamm incasoftware de>
parents:
1133
diff
changeset
|
4422 if (label->statement && label->statement->enclosingFinally != sc->enclosingFinally) |
159 | 4423 error("cannot goto in or out of finally block"); |
4424 return this; | |
4425 } | |
4426 | |
336 | 4427 int GotoStatement::blockExit() |
4428 { | |
4429 //printf("GotoStatement::blockExit(%p)\n", this); | |
4430 return BEgoto; | |
4431 } | |
4432 | |
1587 | 4433 |
159 | 4434 void GotoStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
4435 { | |
4436 buf->writestring("goto "); | |
4437 buf->writestring(ident->toChars()); | |
4438 buf->writebyte(';'); | |
4439 buf->writenl(); | |
4440 } | |
4441 | |
4442 /******************************** LabelStatement ***************************/ | |
4443 | |
4444 LabelStatement::LabelStatement(Loc loc, Identifier *ident, Statement *statement) | |
4445 : Statement(loc) | |
4446 { | |
4447 this->ident = ident; | |
4448 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
|
4449 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
|
4450 this->enclosingScopeExit = NULL; |
159 | 4451 this->lblock = NULL; |
4452 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
|
4453 this->asmLabel = false; |
159 | 4454 } |
4455 | |
4456 Statement *LabelStatement::syntaxCopy() | |
4457 { | |
4458 LabelStatement *s = new LabelStatement(loc, ident, statement->syntaxCopy()); | |
4459 return s; | |
4460 } | |
4461 | |
4462 Statement *LabelStatement::semantic(Scope *sc) | |
4463 { LabelDsymbol *ls; | |
4464 FuncDeclaration *fd = sc->parent->isFuncDeclaration(); | |
4465 | |
4466 //printf("LabelStatement::semantic()\n"); | |
4467 ls = fd->searchLabel(ident); | |
4468 if (ls->statement) | |
4469 error("Label '%s' already defined", ls->toChars()); | |
4470 else | |
4471 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
|
4472 |
f99a3b393c03
Reorganize EnclosingHandlers to require less changes to the frontend and allow us to
Christian Kamm <kamm incasoftware de>
parents:
1133
diff
changeset
|
4473 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
|
4474 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
|
4475 |
159 | 4476 sc = sc->push(); |
4477 sc->scopesym = sc->enclosing->scopesym; | |
4478 sc->callSuper |= CSXlabel; | |
4479 sc->slabel = this; | |
4480 if (statement) | |
4481 statement = statement->semantic(sc); | |
4482 sc->pop(); | |
305
2b72433d5c8c
[svn r326] Fixed a bunch of issues with printf's that MinGW32 did not support.
lindquist
parents:
304
diff
changeset
|
4483 |
664
eef8ac26c66c
Some missed LLVMDC -> LDC.
Christian Kamm <kamm incasoftware de>
parents:
510
diff
changeset
|
4484 // 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
|
4485 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
|
4486 |
159 | 4487 return this; |
4488 } | |
4489 | |
4490 Statements *LabelStatement::flatten(Scope *sc) | |
4491 { | |
4492 Statements *a = NULL; | |
4493 | |
4494 if (statement) | |
4495 { | |
4496 a = statement->flatten(sc); | |
4497 if (a) | |
4498 { | |
4499 if (!a->dim) | |
4500 { | |
4501 a->push(new ExpStatement(loc, NULL)); | |
4502 } | |
4503 Statement *s = (Statement *)a->data[0]; | |
4504 | |
4505 s = new LabelStatement(loc, ident, s); | |
4506 a->data[0] = s; | |
4507 } | |
4508 } | |
4509 | |
4510 return a; | |
4511 } | |
4512 | |
4513 | |
4514 int LabelStatement::usesEH() | |
4515 { | |
4516 return statement ? statement->usesEH() : FALSE; | |
4517 } | |
4518 | |
336 | 4519 int LabelStatement::blockExit() |
4520 { | |
4521 //printf("LabelStatement::blockExit(%p)\n", this); | |
4522 return statement ? statement->blockExit() : BEfallthru; | |
4523 } | |
4524 | |
1587 | 4525 |
159 | 4526 int LabelStatement::comeFrom() |
4527 { | |
4528 //printf("LabelStatement::comeFrom()\n"); | |
4529 return TRUE; | |
4530 } | |
4531 | |
4532 void LabelStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
4533 { | |
4534 buf->writestring(ident->toChars()); | |
4535 buf->writebyte(':'); | |
4536 buf->writenl(); | |
4537 if (statement) | |
4538 statement->toCBuffer(buf, hgs); | |
4539 } | |
4540 | |
4541 | |
4542 /******************************** LabelDsymbol ***************************/ | |
4543 | |
4544 LabelDsymbol::LabelDsymbol(Identifier *ident) | |
4545 : Dsymbol(ident) | |
4546 { | |
4547 statement = NULL; | |
4548 } | |
4549 | |
4550 LabelDsymbol *LabelDsymbol::isLabel() // is this a LabelDsymbol()? | |
4551 { | |
4552 return this; | |
4553 } |