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