Mercurial > projects > ldc
annotate dmd/statement.c @ 1578:1dee66f6ec0b
Removed a chunk of code in favour of a shorter and more portable method
author | Robert Clipsham <robert@octarineparrot.com> |
---|---|
date | Tue, 08 Sep 2009 11:21:30 +0100 |
parents | 05c235309d6f |
children | def7a1d494fd |
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 | |
1530
05c235309d6f
Make the auto storage class never have the same meaning as scope.
Christian Kamm <kamm incasoftware de>
parents:
1416
diff
changeset
|
336 e = v->callScopeDtor(); |
159 | 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; | |
1367
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1358
diff
changeset
|
1131 if (body) |
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1358
diff
changeset
|
1132 body = body->semantic(sc); |
159 | 1133 sc->noctor--; |
1134 | |
1135 sc->pop(); | |
1136 return this; | |
1137 } | |
1138 | |
1139 void ForStatement::scopeCode(Statement **sentry, Statement **sexception, Statement **sfinally) | |
1140 { | |
1141 //printf("ForStatement::scopeCode()\n"); | |
1142 //print(); | |
1143 if (init) | |
1144 init->scopeCode(sentry, sexception, sfinally); | |
1145 else | |
1146 Statement::scopeCode(sentry, sexception, sfinally); | |
1147 } | |
1148 | |
1149 int ForStatement::hasBreak() | |
1150 { | |
1151 //printf("ForStatement::hasBreak()\n"); | |
1152 return TRUE; | |
1153 } | |
1154 | |
1155 int ForStatement::hasContinue() | |
1156 { | |
1157 return TRUE; | |
1158 } | |
1159 | |
1160 int ForStatement::usesEH() | |
1161 { | |
1162 return (init && init->usesEH()) || body->usesEH(); | |
1163 } | |
1164 | |
336 | 1165 int ForStatement::blockExit() |
1166 { int result = BEfallthru; | |
1167 | |
1168 if (init) | |
1169 { result = init->blockExit(); | |
1170 if (!(result & BEfallthru)) | |
1171 return result; | |
1172 } | |
1173 if (condition) | |
1174 { if (condition->canThrow()) | |
1175 result |= BEthrow; | |
1176 } | |
1177 else | |
1178 result &= ~BEfallthru; // the body must do the exiting | |
1179 if (body) | |
1180 { int r = body->blockExit(); | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
945
diff
changeset
|
1181 if (r & (BEbreak | BEgoto)) |
336 | 1182 result |= BEfallthru; |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
945
diff
changeset
|
1183 result |= r & ~(BEfallthru | BEbreak | BEcontinue); |
336 | 1184 } |
1185 if (increment && increment->canThrow()) | |
1186 result |= BEthrow; | |
1187 return result; | |
1188 } | |
1189 | |
1195
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1164
diff
changeset
|
1190 |
159 | 1191 int ForStatement::comeFrom() |
1192 { | |
1193 //printf("ForStatement::comeFrom()\n"); | |
1194 if (body) | |
1195 { int result = body->comeFrom(); | |
1196 //printf("result = %d\n", result); | |
1197 return result; | |
1198 } | |
1199 return FALSE; | |
1200 } | |
1201 | |
1202 void ForStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
1203 { | |
1204 buf->writestring("for ("); | |
1205 if (init) | |
1206 { | |
1207 hgs->FLinit.init++; | |
1208 init->toCBuffer(buf, hgs); | |
1209 hgs->FLinit.init--; | |
1210 } | |
1211 else | |
1212 buf->writebyte(';'); | |
1213 if (condition) | |
1214 { buf->writebyte(' '); | |
1215 condition->toCBuffer(buf, hgs); | |
1216 } | |
1217 buf->writebyte(';'); | |
1218 if (increment) | |
1219 { buf->writebyte(' '); | |
1220 increment->toCBuffer(buf, hgs); | |
1221 } | |
1222 buf->writebyte(')'); | |
1223 buf->writenl(); | |
1224 buf->writebyte('{'); | |
1225 buf->writenl(); | |
1226 body->toCBuffer(buf, hgs); | |
1227 buf->writebyte('}'); | |
1228 buf->writenl(); | |
1229 } | |
1230 | |
1231 /******************************** ForeachStatement ***************************/ | |
1232 | |
1233 ForeachStatement::ForeachStatement(Loc loc, enum TOK op, Arguments *arguments, | |
1234 Expression *aggr, Statement *body) | |
1235 : Statement(loc) | |
1236 { | |
1237 this->op = op; | |
1238 this->arguments = arguments; | |
1239 this->aggr = aggr; | |
1240 this->body = body; | |
1241 | |
1242 this->key = NULL; | |
1243 this->value = NULL; | |
1244 | |
1245 this->func = NULL; | |
1246 } | |
1247 | |
1248 Statement *ForeachStatement::syntaxCopy() | |
1249 { | |
1250 Arguments *args = Argument::arraySyntaxCopy(arguments); | |
1251 Expression *exp = aggr->syntaxCopy(); | |
1252 ForeachStatement *s = new ForeachStatement(loc, op, args, exp, | |
1253 body ? body->syntaxCopy() : NULL); | |
1254 return s; | |
1255 } | |
1256 | |
1257 Statement *ForeachStatement::semantic(Scope *sc) | |
1258 { | |
1259 //printf("ForeachStatement::semantic() %p\n", this); | |
1260 ScopeDsymbol *sym; | |
1261 Statement *s = this; | |
846
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1262 size_t dim = arguments->dim; |
159 | 1263 TypeAArray *taa = NULL; |
1264 | |
1265 Type *tn = NULL; | |
1266 Type *tnv = NULL; | |
1267 | |
1268 func = sc->func; | |
1269 if (func->fes) | |
1270 func = func->fes->func; | |
1271 | |
1272 aggr = aggr->semantic(sc); | |
1273 aggr = resolveProperties(sc, aggr); | |
846
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1274 aggr = aggr->optimize(WANTvalue); |
159 | 1275 if (!aggr->type) |
1276 { | |
1277 error("invalid foreach aggregate %s", aggr->toChars()); | |
1278 return this; | |
1279 } | |
1280 | |
1358
78038e540342
Fix overload resolution issue in dmd bug 313/314 fix.
Christian Kamm <kamm incasoftware de>
parents:
1195
diff
changeset
|
1281 inferApplyArgTypes(op, arguments, aggr, sc->module); |
159 | 1282 |
1283 /* Check for inference errors | |
1284 */ | |
1285 if (dim != arguments->dim) | |
1286 { | |
1287 //printf("dim = %d, arguments->dim = %d\n", dim, arguments->dim); | |
1288 error("cannot uniquely infer foreach argument types"); | |
1289 return this; | |
1290 } | |
1291 | |
1292 Type *tab = aggr->type->toBasetype(); | |
1293 | |
1294 if (tab->ty == Ttuple) // don't generate new scope for tuple loops | |
1295 { | |
1296 if (dim < 1 || dim > 2) | |
1297 { | |
1298 error("only one (value) or two (key,value) arguments for tuple foreach"); | |
1299 return s; | |
1300 } | |
1301 | |
1302 TypeTuple *tuple = (TypeTuple *)tab; | |
1303 Statements *statements = new Statements(); | |
1304 //printf("aggr: op = %d, %s\n", aggr->op, aggr->toChars()); | |
1305 size_t n; | |
1306 TupleExp *te = NULL; | |
1307 if (aggr->op == TOKtuple) // expression tuple | |
1308 { te = (TupleExp *)aggr; | |
1309 n = te->exps->dim; | |
1310 } | |
1311 else if (aggr->op == TOKtype) // type tuple | |
1312 { | |
1313 n = Argument::dim(tuple->arguments); | |
1314 } | |
1315 else | |
1316 assert(0); | |
1317 for (size_t j = 0; j < n; j++) | |
1318 { size_t k = (op == TOKforeach) ? j : n - 1 - j; | |
1319 Expression *e; | |
1320 Type *t; | |
1321 if (te) | |
1322 e = (Expression *)te->exps->data[k]; | |
1323 else | |
1324 t = Argument::getNth(tuple->arguments, k)->type; | |
1325 Argument *arg = (Argument *)arguments->data[0]; | |
1326 Statements *st = new Statements(); | |
1327 | |
1328 if (dim == 2) | |
1329 { // Declare key | |
1330 if (arg->storageClass & (STCout | STCref | STClazy)) | |
1331 error("no storage class for key %s", arg->ident->toChars()); | |
1332 TY keyty = arg->type->ty; | |
1195
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1164
diff
changeset
|
1333 if (keyty != Tint32 && keyty != Tuns32) |
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1164
diff
changeset
|
1334 { |
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1164
diff
changeset
|
1335 if (global.params.is64bit) |
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1164
diff
changeset
|
1336 { |
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1164
diff
changeset
|
1337 if (keyty != Tint64 && keyty != Tuns64) |
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1164
diff
changeset
|
1338 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
|
1339 } |
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1164
diff
changeset
|
1340 else |
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1164
diff
changeset
|
1341 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
|
1342 } |
159 | 1343 Initializer *ie = new ExpInitializer(0, new IntegerExp(k)); |
1344 VarDeclaration *var = new VarDeclaration(loc, arg->type, arg->ident, ie); | |
1345 var->storage_class |= STCconst; | |
1346 DeclarationExp *de = new DeclarationExp(loc, var); | |
1347 st->push(new ExpStatement(loc, de)); | |
1348 arg = (Argument *)arguments->data[1]; // value | |
1349 } | |
1350 // Declare value | |
1351 if (arg->storageClass & (STCout | STCref | STClazy)) | |
1352 error("no storage class for value %s", arg->ident->toChars()); | |
1353 Dsymbol *var; | |
1354 if (te) | |
1355 { | |
1356 if (e->type->toBasetype()->ty == Tfunction && | |
1357 e->op == TOKvar) | |
1358 { VarExp *ve = (VarExp *)e; | |
1359 var = new AliasDeclaration(loc, arg->ident, ve->var); | |
1360 } | |
1361 else | |
1362 { | |
1363 arg->type = e->type; | |
1364 Initializer *ie = new ExpInitializer(0, e); | |
1365 VarDeclaration *v = new VarDeclaration(loc, arg->type, arg->ident, ie); | |
1366 if (e->isConst()) | |
1367 v->storage_class |= STCconst; | |
1368 var = v; | |
1369 } | |
1370 } | |
1371 else | |
1372 { | |
1373 var = new AliasDeclaration(loc, arg->ident, t); | |
1374 } | |
1375 DeclarationExp *de = new DeclarationExp(loc, var); | |
1376 st->push(new ExpStatement(loc, de)); | |
1377 | |
1378 st->push(body->syntaxCopy()); | |
1379 s = new CompoundStatement(loc, st); | |
1380 s = new ScopeStatement(loc, s); | |
1381 statements->push(s); | |
1382 } | |
1383 | |
1384 s = new UnrolledLoopStatement(loc, statements); | |
1385 s = s->semantic(sc); | |
1386 return s; | |
1387 } | |
1388 | |
846
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1389 for (size_t i = 0; i < dim; i++) |
159 | 1390 { Argument *arg = (Argument *)arguments->data[i]; |
1391 if (!arg->type) | |
1392 { | |
1393 error("cannot infer type for %s", arg->ident->toChars()); | |
1394 return this; | |
1395 } | |
1396 } | |
1397 | |
1398 sym = new ScopeDsymbol(); | |
1399 sym->parent = sc->scopesym; | |
1400 sc = sc->push(sym); | |
1401 | |
1402 sc->noctor++; | |
1403 | |
1404 switch (tab->ty) | |
1405 { | |
1406 case Tarray: | |
1407 case Tsarray: | |
1408 if (dim < 1 || dim > 2) | |
1409 { | |
1410 error("only one or two arguments for array foreach"); | |
1411 break; | |
1412 } | |
1413 | |
1414 /* Look for special case of parsing char types out of char type | |
1415 * array. | |
1416 */ | |
1417 tn = tab->nextOf()->toBasetype(); | |
1418 if (tn->ty == Tchar || tn->ty == Twchar || tn->ty == Tdchar) | |
1419 { Argument *arg; | |
1420 | |
846
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1421 int i = (dim == 1) ? 0 : 1; // index of value |
159 | 1422 arg = (Argument *)arguments->data[i]; |
1423 arg->type = arg->type->semantic(loc, sc); | |
1424 tnv = arg->type->toBasetype(); | |
1425 if (tnv->ty != tn->ty && | |
1426 (tnv->ty == Tchar || tnv->ty == Twchar || tnv->ty == Tdchar)) | |
1427 { | |
1428 if (arg->storageClass & STCref) | |
1429 error("foreach: value of UTF conversion cannot be ref"); | |
1430 if (dim == 2) | |
1431 { arg = (Argument *)arguments->data[0]; | |
1432 if (arg->storageClass & STCref) | |
1433 error("foreach: key cannot be ref"); | |
1434 } | |
1435 goto Lapply; | |
1436 } | |
1437 } | |
1438 | |
846
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1439 for (size_t i = 0; i < dim; i++) |
159 | 1440 { // Declare args |
1441 Argument *arg = (Argument *)arguments->data[i]; | |
1442 VarDeclaration *var; | |
1443 | |
1444 var = new VarDeclaration(loc, arg->type, arg->ident, NULL); | |
1445 var->storage_class |= STCforeach; | |
1446 var->storage_class |= arg->storageClass & (STCin | STCout | STCref); | |
1447 #if 1 | |
1448 DeclarationExp *de = new DeclarationExp(loc, var); | |
1449 de->semantic(sc); | |
1450 #else | |
1451 var->semantic(sc); | |
1452 if (!sc->insert(var)) | |
1453 error("%s already defined", var->ident->toChars()); | |
1454 #endif | |
1455 if (dim == 2 && i == 0) | |
1456 key = var; | |
1457 else | |
1458 value = var; | |
1459 } | |
1460 | |
1461 sc->sbreak = this; | |
1462 sc->scontinue = this; | |
1463 body = body->semantic(sc); | |
1464 | |
1465 if (!value->type->equals(tab->next)) | |
1466 { | |
1467 if (aggr->op == TOKstring) | |
1468 aggr = aggr->implicitCastTo(sc, value->type->arrayOf()); | |
1469 else | |
846
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1470 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
|
1471 tab->toChars(), value->type->toChars()); |
159 | 1472 } |
1473 | |
1195
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1164
diff
changeset
|
1474 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
|
1475 { |
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1164
diff
changeset
|
1476 if (global.params.is64bit) |
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1164
diff
changeset
|
1477 { |
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1164
diff
changeset
|
1478 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
|
1479 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
|
1480 } |
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1164
diff
changeset
|
1481 else |
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1164
diff
changeset
|
1482 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
|
1483 } |
159 | 1484 |
1485 if (key && key->storage_class & (STCout | STCref)) | |
1486 error("foreach: key cannot be out or ref"); | |
1487 break; | |
1488 | |
1489 case Taarray: | |
1490 taa = (TypeAArray *)tab; | |
1491 if (dim < 1 || dim > 2) | |
1492 { | |
1493 error("only one or two arguments for associative array foreach"); | |
1494 break; | |
1495 } | |
1496 if (op == TOKforeach_reverse) | |
1497 { | |
1498 error("no reverse iteration on associative arrays"); | |
1499 } | |
1500 goto Lapply; | |
1501 | |
1502 case Tclass: | |
1503 case Tstruct: | |
846
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1504 #if DMDV2 |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1505 { /* 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
|
1506 * .empty, .next, .retreat, .head and .rear |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1507 * foreach (e; range) { ... } |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1508 * translates to: |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1509 * 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
|
1510 * { auto e = __r.head; |
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 */ |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1514 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
|
1515 goto Lapply; |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1516 AggregateDeclaration *ad = (tab->ty == Tclass) |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1517 ? (AggregateDeclaration *)((TypeClass *)tab)->sym |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1518 : (AggregateDeclaration *)((TypeStruct *)tab)->sym; |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1519 Identifier *idhead; |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1520 Identifier *idnext; |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1521 if (op == TOKforeach) |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1522 { idhead = Id::Fhead; |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1523 idnext = Id::Fnext; |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1524 } |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1525 else |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1526 { idhead = Id::Frear; |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1527 idnext = Id::Fretreat; |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1528 } |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1529 Dsymbol *shead = search_function(ad, idhead); |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1530 if (!shead) |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1531 goto Lapply; |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1532 |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1533 /* 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
|
1534 */ |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1535 Identifier *id = Identifier::generateId("__r"); |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1536 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
|
1537 r->semantic(sc); |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1538 Statement *init = new DeclarationStatement(loc, r); |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1539 |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1540 // !__r.empty |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1541 Expression *e = new VarExp(loc, r); |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1542 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
|
1543 Expression *condition = new NotExp(loc, e); |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1544 |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1545 // __r.next |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1546 e = new VarExp(loc, r); |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1547 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
|
1548 |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1549 /* Declaration statement for e: |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1550 * auto e = __r.idhead; |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1551 */ |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1552 e = new VarExp(loc, r); |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1553 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
|
1554 einit = einit->semantic(sc); |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1555 Argument *arg = (Argument *)arguments->data[0]; |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1556 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
|
1557 ve->storage_class |= STCforeach; |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1558 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
|
1559 |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1560 DeclarationExp *de = new DeclarationExp(loc, ve); |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1561 |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1562 Statement *body = new CompoundStatement(loc, |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1563 new DeclarationStatement(loc, de), this->body); |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1564 |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1565 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
|
1566 s = s->semantic(sc); |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1567 break; |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1568 } |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1569 #endif |
159 | 1570 case Tdelegate: |
1571 Lapply: | |
1572 { FuncDeclaration *fdapply; | |
1573 Arguments *args; | |
1574 Expression *ec; | |
1575 Expression *e; | |
1576 FuncLiteralDeclaration *fld; | |
1577 Argument *a; | |
1578 Type *t; | |
1579 Expression *flde; | |
1580 Identifier *id; | |
1581 Type *tret; | |
719
7261ff0f95ff
Implemented first class delegates. closes #101
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
715
diff
changeset
|
1582 TypeDelegate* dgty; |
7261ff0f95ff
Implemented first class delegates. closes #101
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
715
diff
changeset
|
1583 TypeDelegate* dgty2; |
7261ff0f95ff
Implemented first class delegates. closes #101
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
715
diff
changeset
|
1584 TypeDelegate* fldeTy; |
159 | 1585 |
1586 tret = func->type->nextOf(); | |
1587 | |
1588 // Need a variable to hold value from any return statements in body. | |
1589 if (!sc->func->vresult && tret && tret != Type::tvoid) | |
1590 { VarDeclaration *v; | |
1591 | |
1592 v = new VarDeclaration(loc, tret, Id::result, NULL); | |
1530
05c235309d6f
Make the auto storage class never have the same meaning as scope.
Christian Kamm <kamm incasoftware de>
parents:
1416
diff
changeset
|
1593 v->noscope = 1; |
159 | 1594 v->semantic(sc); |
1595 if (!sc->insert(v)) | |
1596 assert(0); | |
1597 v->parent = sc->func; | |
1598 sc->func->vresult = v; | |
1599 } | |
1600 | |
1601 /* Turn body into the function literal: | |
1602 * int delegate(ref T arg) { body } | |
1603 */ | |
1604 args = new Arguments(); | |
846
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1605 for (size_t i = 0; i < dim; i++) |
159 | 1606 { Argument *arg = (Argument *)arguments->data[i]; |
1607 | |
1608 arg->type = arg->type->semantic(loc, sc); | |
1609 if (arg->storageClass & STCref) | |
1610 id = arg->ident; | |
1611 else | |
1612 { // Make a copy of the ref argument so it isn't | |
1613 // a reference. | |
1614 VarDeclaration *v; | |
1615 Initializer *ie; | |
846
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1616 |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
1617 id = Lexer::uniqueId("__applyArg", i); |
159 | 1618 |
1619 ie = new ExpInitializer(0, new IdentifierExp(0, id)); | |
1620 v = new VarDeclaration(0, arg->type, arg->ident, ie); | |
1621 s = new DeclarationStatement(0, v); | |
1622 body = new CompoundStatement(loc, s, body); | |
1623 } | |
1624 a = new Argument(STCref, arg->type, id, NULL); | |
1625 args->push(a); | |
1626 } | |
1627 t = new TypeFunction(args, Type::tint32, 0, LINKd); | |
1628 fld = new FuncLiteralDeclaration(loc, 0, t, TOKdelegate, this); | |
1629 fld->fbody = body; | |
1630 flde = new FuncExp(loc, fld); | |
1631 flde = flde->semantic(sc); | |
1632 | |
1633 // Resolve any forward referenced goto's | |
1634 for (int i = 0; i < gotos.dim; i++) | |
1635 { CompoundStatement *cs = (CompoundStatement *)gotos.data[i]; | |
1636 GotoStatement *gs = (GotoStatement *)cs->statements->data[0]; | |
1637 | |
1638 if (!gs->label->statement) | |
1639 { // 'Promote' it to this scope, and replace with a return | |
1640 cases.push(gs); | |
1641 s = new ReturnStatement(0, new IntegerExp(cases.dim + 1)); | |
1642 cs->statements->data[0] = (void *)s; | |
1643 } | |
1644 } | |
1645 | |
1646 if (tab->ty == Taarray) | |
1647 { | |
1648 // Check types | |
1649 Argument *arg = (Argument *)arguments->data[0]; | |
1650 if (dim == 2) | |
1651 { | |
1652 if (arg->storageClass & STCref) | |
1653 error("foreach: index cannot be ref"); | |
1654 if (!arg->type->equals(taa->index)) | |
1655 error("foreach: index must be type %s, not %s", taa->index->toChars(), arg->type->toChars()); | |
1656 arg = (Argument *)arguments->data[1]; | |
1657 } | |
1658 if (!arg->type->equals(taa->nextOf())) | |
1659 error("foreach: value must be type %s, not %s", taa->nextOf()->toChars(), arg->type->toChars()); | |
1660 | |
1661 /* Call: | |
1662 * _aaApply(aggr, keysize, flde) | |
1663 */ | |
664
eef8ac26c66c
Some missed LLVMDC -> LDC.
Christian Kamm <kamm incasoftware de>
parents:
510
diff
changeset
|
1664 //LDC: Build arguments. |
389
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
380
diff
changeset
|
1665 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
|
1666 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
|
1667 if(!aaApply2_fd) { |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
380
diff
changeset
|
1668 Arguments* args = new Arguments; |
715
30b42a283c8e
Removed TypeOpaque from DMD.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
664
diff
changeset
|
1669 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
|
1670 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
|
1671 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
|
1672 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
|
1673 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
|
1674 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
|
1675 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
|
1676 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
|
1677 } |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
380
diff
changeset
|
1678 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
|
1679 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
|
1680 if(!aaApply_fd) { |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
380
diff
changeset
|
1681 Arguments* args = new Arguments; |
715
30b42a283c8e
Removed TypeOpaque from DMD.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
664
diff
changeset
|
1682 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
|
1683 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
|
1684 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
|
1685 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
|
1686 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
|
1687 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
|
1688 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
|
1689 } |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
380
diff
changeset
|
1690 if (dim == 2) { |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
380
diff
changeset
|
1691 fdapply = aaApply2_fd; |
719
7261ff0f95ff
Implemented first class delegates. closes #101
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
715
diff
changeset
|
1692 fldeTy = aaApply2_dg; |
389
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
380
diff
changeset
|
1693 } else { |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
380
diff
changeset
|
1694 fdapply = aaApply_fd; |
719
7261ff0f95ff
Implemented first class delegates. closes #101
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
715
diff
changeset
|
1695 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
|
1696 } |
159 | 1697 ec = new VarExp(0, fdapply); |
1698 Expressions *exps = new Expressions(); | |
1699 exps->push(aggr); | |
1700 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
|
1701 keysize = (keysize + (PTRSIZE-1)) & ~(PTRSIZE-1); |
159 | 1702 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
|
1703 |
7261ff0f95ff
Implemented first class delegates. closes #101
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
715
diff
changeset
|
1704 // 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
|
1705 if (!fldeTy->equals(flde->type)) |
7261ff0f95ff
Implemented first class delegates. closes #101
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
715
diff
changeset
|
1706 { |
7261ff0f95ff
Implemented first class delegates. closes #101
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
715
diff
changeset
|
1707 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
|
1708 flde->type = fldeTy; |
7261ff0f95ff
Implemented first class delegates. closes #101
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
715
diff
changeset
|
1709 } |
159 | 1710 exps->push(flde); |
1711 e = new CallExp(loc, ec, exps); | |
1712 e->type = Type::tindex; // don't run semantic() on e | |
1713 } | |
1714 else if (tab->ty == Tarray || tab->ty == Tsarray) | |
1715 { | |
1716 /* Call: | |
1717 * _aApply(aggr, flde) | |
1718 */ | |
1719 static char fntab[9][3] = | |
1720 { "cc","cw","cd", | |
1721 "wc","cc","wd", | |
1722 "dc","dw","dd" | |
1723 }; | |
1724 char fdname[7+1+2+ sizeof(dim)*3 + 1]; | |
1725 int flag; | |
1726 | |
1727 switch (tn->ty) | |
1728 { | |
1729 case Tchar: flag = 0; break; | |
1730 case Twchar: flag = 3; break; | |
1731 case Tdchar: flag = 6; break; | |
1732 default: assert(0); | |
1733 } | |
1734 switch (tnv->ty) | |
1735 { | |
1736 case Tchar: flag += 0; break; | |
1737 case Twchar: flag += 1; break; | |
1738 case Tdchar: flag += 2; break; | |
1739 default: assert(0); | |
1740 } | |
1741 const char *r = (op == TOKforeach_reverse) ? "R" : ""; | |
1416
17268b0a3ab0
Fix for mingw32 segfault
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
1367
diff
changeset
|
1742 #ifdef __MINGW32__ |
17268b0a3ab0
Fix for mingw32 segfault
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
1367
diff
changeset
|
1743 int j = sprintf(fdname, "_aApply%s%.*s%lu", r, 2, fntab[flag], dim); |
17268b0a3ab0
Fix for mingw32 segfault
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
1367
diff
changeset
|
1744 #else |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
945
diff
changeset
|
1745 int j = sprintf(fdname, "_aApply%s%.*s%zu", r, 2, fntab[flag], dim); |
1416
17268b0a3ab0
Fix for mingw32 segfault
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
1367
diff
changeset
|
1746 #endif |
159 | 1747 assert(j < sizeof(fdname)); |
664
eef8ac26c66c
Some missed LLVMDC -> LDC.
Christian Kamm <kamm incasoftware de>
parents:
510
diff
changeset
|
1748 //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
|
1749 Arguments* args = new Arguments; |
715
30b42a283c8e
Removed TypeOpaque from DMD.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
664
diff
changeset
|
1750 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
|
1751 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
|
1752 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
|
1753 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
|
1754 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
|
1755 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
|
1756 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
|
1757 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
|
1758 } else { |
d8234836b40f
Get rid of runTimeHack and instead add proper argument info to the frontend
Christian Kamm <kamm incasoftware de>
parents:
353
diff
changeset
|
1759 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
|
1760 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
|
1761 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
|
1762 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
|
1763 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
|
1764 } |
159 | 1765 |
1766 ec = new VarExp(0, fdapply); | |
1767 Expressions *exps = new Expressions(); | |
1768 if (tab->ty == Tsarray) | |
1769 aggr = aggr->castTo(sc, tn->arrayOf()); | |
715
30b42a283c8e
Removed TypeOpaque from DMD.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
664
diff
changeset
|
1770 exps->push(aggr); |
719
7261ff0f95ff
Implemented first class delegates. closes #101
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
715
diff
changeset
|
1771 |
7261ff0f95ff
Implemented first class delegates. closes #101
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
715
diff
changeset
|
1772 // 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
|
1773 if (!dgty->equals(flde->type)) |
7261ff0f95ff
Implemented first class delegates. closes #101
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
715
diff
changeset
|
1774 { |
7261ff0f95ff
Implemented first class delegates. closes #101
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
715
diff
changeset
|
1775 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
|
1776 flde->type = dgty; |
7261ff0f95ff
Implemented first class delegates. closes #101
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
715
diff
changeset
|
1777 } |
159 | 1778 exps->push(flde); |
1779 e = new CallExp(loc, ec, exps); | |
1780 e->type = Type::tindex; // don't run semantic() on e | |
1781 } | |
1782 else if (tab->ty == Tdelegate) | |
1783 { | |
1784 /* Call: | |
1785 * aggr(flde) | |
1786 */ | |
1787 Expressions *exps = new Expressions(); | |
1788 exps->push(flde); | |
1789 e = new CallExp(loc, aggr, exps); | |
1790 e = e->semantic(sc); | |
1791 if (e->type != Type::tint32) | |
1792 error("opApply() function for %s must return an int", tab->toChars()); | |
1793 } | |
1794 else | |
1795 { | |
1796 /* Call: | |
1797 * aggr.apply(flde) | |
1798 */ | |
1799 ec = new DotIdExp(loc, aggr, | |
1800 (op == TOKforeach_reverse) ? Id::applyReverse | |
1801 : Id::apply); | |
1802 Expressions *exps = new Expressions(); | |
1803 exps->push(flde); | |
1804 e = new CallExp(loc, ec, exps); | |
1805 e = e->semantic(sc); | |
1806 if (e->type != Type::tint32) | |
1807 error("opApply() function for %s must return an int", tab->toChars()); | |
1808 } | |
1809 | |
1810 if (!cases.dim) | |
1811 // Easy case, a clean exit from the loop | |
1812 s = new ExpStatement(loc, e); | |
1813 else | |
1814 { // Construct a switch statement around the return value | |
1815 // of the apply function. | |
1816 Statements *a = new Statements(); | |
1817 | |
1818 // default: break; takes care of cases 0 and 1 | |
1819 s = new BreakStatement(0, NULL); | |
1820 s = new DefaultStatement(0, s); | |
1821 a->push(s); | |
1822 | |
1823 // cases 2... | |
1824 for (int i = 0; i < cases.dim; i++) | |
1825 { | |
1826 s = (Statement *)cases.data[i]; | |
1827 s = new CaseStatement(0, new IntegerExp(i + 2), s); | |
1828 a->push(s); | |
1829 } | |
1830 | |
1831 s = new CompoundStatement(loc, a); | |
1832 s = new SwitchStatement(loc, e, s); | |
1833 s = s->semantic(sc); | |
1834 } | |
1835 break; | |
1836 } | |
1837 | |
1838 default: | |
1839 error("foreach: %s is not an aggregate type", aggr->type->toChars()); | |
1840 break; | |
1841 } | |
1842 sc->noctor--; | |
1843 sc->pop(); | |
1844 return s; | |
1845 } | |
1846 | |
1847 int ForeachStatement::hasBreak() | |
1848 { | |
1849 return TRUE; | |
1850 } | |
1851 | |
1852 int ForeachStatement::hasContinue() | |
1853 { | |
1854 return TRUE; | |
1855 } | |
1856 | |
1857 int ForeachStatement::usesEH() | |
1858 { | |
1859 return body->usesEH(); | |
1860 } | |
1861 | |
336 | 1862 int ForeachStatement::blockExit() |
1863 { int result = BEfallthru; | |
1864 | |
1865 if (aggr->canThrow()) | |
1866 result |= BEthrow; | |
1867 | |
1868 if (body) | |
1869 { | |
1870 result |= body->blockExit() & ~(BEbreak | BEcontinue); | |
1871 } | |
1872 return result; | |
1873 } | |
1874 | |
159 | 1875 int ForeachStatement::comeFrom() |
1876 { | |
1877 if (body) | |
1878 return body->comeFrom(); | |
1879 return FALSE; | |
1880 } | |
1881 | |
1882 void ForeachStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
1883 { | |
1884 buf->writestring(Token::toChars(op)); | |
1885 buf->writestring(" ("); | |
1886 for (int i = 0; i < arguments->dim; i++) | |
1887 { | |
1888 Argument *a = (Argument *)arguments->data[i]; | |
1889 if (i) | |
1890 buf->writestring(", "); | |
1891 if (a->storageClass & STCref) | |
1892 buf->writestring((global.params.Dversion == 1) | |
1893 ? (char*)"inout " : (char*)"ref "); | |
1894 if (a->type) | |
1895 a->type->toCBuffer(buf, a->ident, hgs); | |
1896 else | |
1897 buf->writestring(a->ident->toChars()); | |
1898 } | |
1899 buf->writestring("; "); | |
1900 aggr->toCBuffer(buf, hgs); | |
1901 buf->writebyte(')'); | |
1902 buf->writenl(); | |
1903 buf->writebyte('{'); | |
1904 buf->writenl(); | |
1905 if (body) | |
1906 body->toCBuffer(buf, hgs); | |
1907 buf->writebyte('}'); | |
1908 buf->writenl(); | |
1909 } | |
1910 | |
1911 /******************************** IfStatement ***************************/ | |
1912 | |
1913 IfStatement::IfStatement(Loc loc, Argument *arg, Expression *condition, Statement *ifbody, Statement *elsebody) | |
1914 : Statement(loc) | |
1915 { | |
1916 this->arg = arg; | |
1917 this->condition = condition; | |
1918 this->ifbody = ifbody; | |
1919 this->elsebody = elsebody; | |
1920 this->match = NULL; | |
1921 } | |
1922 | |
1923 Statement *IfStatement::syntaxCopy() | |
1924 { | |
1925 Statement *i = NULL; | |
1926 if (ifbody) | |
1927 i = ifbody->syntaxCopy(); | |
1928 | |
1929 Statement *e = NULL; | |
1930 if (elsebody) | |
1931 e = elsebody->syntaxCopy(); | |
1932 | |
1933 Argument *a = arg ? arg->syntaxCopy() : NULL; | |
1934 IfStatement *s = new IfStatement(loc, a, condition->syntaxCopy(), i, e); | |
1935 return s; | |
1936 } | |
1937 | |
1938 Statement *IfStatement::semantic(Scope *sc) | |
1939 { | |
1940 condition = condition->semantic(sc); | |
1941 condition = resolveProperties(sc, condition); | |
1942 condition = condition->checkToBoolean(); | |
1943 | |
1944 // If we can short-circuit evaluate the if statement, don't do the | |
1945 // semantic analysis of the skipped code. | |
1946 // This feature allows a limited form of conditional compilation. | |
1947 condition = condition->optimize(WANTflags); | |
1948 | |
1949 // Evaluate at runtime | |
1950 unsigned cs0 = sc->callSuper; | |
1951 unsigned cs1; | |
1952 | |
1953 Scope *scd; | |
1954 if (arg) | |
1955 { /* Declare arg, which we will set to be the | |
1956 * result of condition. | |
1957 */ | |
1958 ScopeDsymbol *sym = new ScopeDsymbol(); | |
1959 sym->parent = sc->scopesym; | |
1960 scd = sc->push(sym); | |
1961 | |
1962 Type *t = arg->type ? arg->type : condition->type; | |
1963 match = new VarDeclaration(loc, t, arg->ident, NULL); | |
1530
05c235309d6f
Make the auto storage class never have the same meaning as scope.
Christian Kamm <kamm incasoftware de>
parents:
1416
diff
changeset
|
1964 match->noscope = 1; |
159 | 1965 match->semantic(scd); |
1966 if (!scd->insert(match)) | |
1967 assert(0); | |
1968 match->parent = sc->func; | |
1969 | |
1970 /* Generate: | |
1971 * (arg = condition) | |
1972 */ | |
1973 VarExp *v = new VarExp(0, match); | |
1974 condition = new AssignExp(loc, v, condition); | |
1975 condition = condition->semantic(scd); | |
1976 } | |
1977 else | |
1978 scd = sc->push(); | |
1979 ifbody = ifbody->semantic(scd); | |
1980 scd->pop(); | |
1981 | |
1982 cs1 = sc->callSuper; | |
1983 sc->callSuper = cs0; | |
1984 if (elsebody) | |
1985 elsebody = elsebody->semanticScope(sc, NULL, NULL); | |
1986 sc->mergeCallSuper(loc, cs1); | |
1987 | |
1988 return this; | |
1989 } | |
1990 | |
1991 int IfStatement::usesEH() | |
1992 { | |
1993 return (ifbody && ifbody->usesEH()) || (elsebody && elsebody->usesEH()); | |
1994 } | |
1995 | |
336 | 1996 int IfStatement::blockExit() |
1997 { | |
1998 //printf("IfStatement::blockExit(%p)\n", this); | |
1999 | |
2000 int result = BEnone; | |
2001 if (condition->canThrow()) | |
2002 result |= BEthrow; | |
2003 if (condition->isBool(TRUE)) | |
2004 { | |
2005 if (ifbody) | |
2006 result |= ifbody->blockExit(); | |
2007 else | |
2008 result |= BEfallthru; | |
2009 } | |
2010 else if (condition->isBool(FALSE)) | |
2011 { | |
2012 if (elsebody) | |
2013 result |= elsebody->blockExit(); | |
2014 else | |
2015 result |= BEfallthru; | |
2016 } | |
2017 else | |
2018 { | |
2019 if (ifbody) | |
2020 result |= ifbody->blockExit(); | |
2021 else | |
2022 result |= BEfallthru; | |
2023 if (elsebody) | |
2024 result |= elsebody->blockExit(); | |
2025 else | |
2026 result |= BEfallthru; | |
2027 } | |
2028 //printf("IfStatement::blockExit(%p) = x%x\n", this, result); | |
2029 return result; | |
2030 } | |
2031 | |
159 | 2032 void IfStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
2033 { | |
2034 buf->writestring("if ("); | |
2035 if (arg) | |
2036 { | |
2037 if (arg->type) | |
2038 arg->type->toCBuffer(buf, arg->ident, hgs); | |
2039 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
|
2040 { buf->writestring("auto "); |
159 | 2041 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
|
2042 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
389
diff
changeset
|
2043 buf->writestring(" = "); |
159 | 2044 } |
2045 condition->toCBuffer(buf, hgs); | |
2046 buf->writebyte(')'); | |
2047 buf->writenl(); | |
2048 ifbody->toCBuffer(buf, hgs); | |
2049 if (elsebody) | |
2050 { buf->writestring("else"); | |
2051 buf->writenl(); | |
2052 elsebody->toCBuffer(buf, hgs); | |
2053 } | |
2054 } | |
2055 | |
2056 /******************************** ConditionalStatement ***************************/ | |
2057 | |
2058 ConditionalStatement::ConditionalStatement(Loc loc, Condition *condition, Statement *ifbody, Statement *elsebody) | |
2059 : Statement(loc) | |
2060 { | |
2061 this->condition = condition; | |
2062 this->ifbody = ifbody; | |
2063 this->elsebody = elsebody; | |
2064 } | |
2065 | |
2066 Statement *ConditionalStatement::syntaxCopy() | |
2067 { | |
2068 Statement *e = NULL; | |
2069 if (elsebody) | |
2070 e = elsebody->syntaxCopy(); | |
2071 ConditionalStatement *s = new ConditionalStatement(loc, | |
2072 condition->syntaxCopy(), ifbody->syntaxCopy(), e); | |
2073 return s; | |
2074 } | |
2075 | |
2076 Statement *ConditionalStatement::semantic(Scope *sc) | |
2077 { | |
2078 //printf("ConditionalStatement::semantic()\n"); | |
2079 | |
2080 // If we can short-circuit evaluate the if statement, don't do the | |
2081 // semantic analysis of the skipped code. | |
2082 // This feature allows a limited form of conditional compilation. | |
2083 if (condition->include(sc, NULL)) | |
2084 { | |
2085 ifbody = ifbody->semantic(sc); | |
2086 return ifbody; | |
2087 } | |
2088 else | |
2089 { | |
2090 if (elsebody) | |
2091 elsebody = elsebody->semantic(sc); | |
2092 return elsebody; | |
2093 } | |
2094 } | |
2095 | |
2096 Statements *ConditionalStatement::flatten(Scope *sc) | |
2097 { | |
2098 Statement *s; | |
2099 | |
2100 if (condition->include(sc, NULL)) | |
2101 s = ifbody; | |
2102 else | |
2103 s = elsebody; | |
2104 | |
2105 Statements *a = new Statements(); | |
2106 a->push(s); | |
2107 return a; | |
2108 } | |
2109 | |
2110 int ConditionalStatement::usesEH() | |
2111 { | |
2112 return (ifbody && ifbody->usesEH()) || (elsebody && elsebody->usesEH()); | |
2113 } | |
2114 | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
389
diff
changeset
|
2115 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
|
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 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
|
2118 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
|
2119 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
|
2120 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
|
2121 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
389
diff
changeset
|
2122 |
159 | 2123 void ConditionalStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
2124 { | |
2125 condition->toCBuffer(buf, hgs); | |
2126 buf->writenl(); | |
336 | 2127 buf->writeByte('{'); |
2128 buf->writenl(); | |
159 | 2129 if (ifbody) |
2130 ifbody->toCBuffer(buf, hgs); | |
336 | 2131 buf->writeByte('}'); |
2132 buf->writenl(); | |
159 | 2133 if (elsebody) |
2134 { | |
2135 buf->writestring("else"); | |
2136 buf->writenl(); | |
336 | 2137 buf->writeByte('{'); |
2138 buf->writenl(); | |
159 | 2139 elsebody->toCBuffer(buf, hgs); |
336 | 2140 buf->writeByte('}'); |
2141 buf->writenl(); | |
159 | 2142 } |
2143 buf->writenl(); | |
2144 } | |
2145 | |
2146 | |
2147 /******************************** PragmaStatement ***************************/ | |
2148 | |
2149 PragmaStatement::PragmaStatement(Loc loc, Identifier *ident, Expressions *args, Statement *body) | |
2150 : Statement(loc) | |
2151 { | |
2152 this->ident = ident; | |
2153 this->args = args; | |
2154 this->body = body; | |
2155 } | |
2156 | |
2157 Statement *PragmaStatement::syntaxCopy() | |
2158 { | |
2159 Statement *b = NULL; | |
2160 if (body) | |
2161 b = body->syntaxCopy(); | |
2162 PragmaStatement *s = new PragmaStatement(loc, | |
2163 ident, Expression::arraySyntaxCopy(args), b); | |
2164 return s; | |
2165 } | |
2166 | |
2167 Statement *PragmaStatement::semantic(Scope *sc) | |
2168 { // Should be merged with PragmaDeclaration | |
2169 //printf("PragmaStatement::semantic() %s\n", toChars()); | |
2170 //printf("body = %p\n", body); | |
2171 if (ident == Id::msg) | |
2172 { | |
2173 if (args) | |
2174 { | |
2175 for (size_t i = 0; i < args->dim; i++) | |
2176 { | |
2177 Expression *e = (Expression *)args->data[i]; | |
2178 | |
2179 e = e->semantic(sc); | |
2180 e = e->optimize(WANTvalue | WANTinterpret); | |
2181 if (e->op == TOKstring) | |
2182 { | |
2183 StringExp *se = (StringExp *)e; | |
794
661384d6a936
Fix warnings on x86-64. By fvbommel.
Christian Kamm <kamm incasoftware de>
parents:
765
diff
changeset
|
2184 fprintf(stdmsg, "%.*s", (int)se->len, (char*)se->string); |
159 | 2185 } |
2186 else | |
2187 error("string expected for message, not '%s'", e->toChars()); | |
2188 } | |
2189 fprintf(stdmsg, "\n"); | |
2190 } | |
2191 } | |
2192 else if (ident == Id::lib) | |
2193 { | |
846
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
2194 #if 1 |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
2195 /* Should this be allowed? |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
2196 */ |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
2197 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
|
2198 #else |
159 | 2199 if (!args || args->dim != 1) |
2200 error("string expected for library name"); | |
2201 else | |
2202 { | |
2203 Expression *e = (Expression *)args->data[0]; | |
2204 | |
2205 e = e->semantic(sc); | |
2206 e = e->optimize(WANTvalue | WANTinterpret); | |
2207 args->data[0] = (void *)e; | |
2208 if (e->op != TOKstring) | |
2209 error("string expected for library name, not '%s'", e->toChars()); | |
2210 else if (global.params.verbose) | |
2211 { | |
2212 StringExp *se = (StringExp *)e; | |
2213 char *name = (char *)mem.malloc(se->len + 1); | |
2214 memcpy(name, se->string, se->len); | |
2215 name[se->len] = 0; | |
2216 printf("library %s\n", name); | |
2217 mem.free(name); | |
2218 } | |
2219 } | |
846
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
2220 #endif |
159 | 2221 } |
945
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 // LDC |
03d7c4aac654
SWITCHED TO LLVM 2.5 !
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
908
diff
changeset
|
2224 else if (ident == Id::allow_inline) |
03d7c4aac654
SWITCHED TO LLVM 2.5 !
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
908
diff
changeset
|
2225 { |
03d7c4aac654
SWITCHED TO LLVM 2.5 !
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
908
diff
changeset
|
2226 sc->func->allowInlining = true; |
03d7c4aac654
SWITCHED TO LLVM 2.5 !
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
908
diff
changeset
|
2227 } |
03d7c4aac654
SWITCHED TO LLVM 2.5 !
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
908
diff
changeset
|
2228 |
159 | 2229 else |
2230 error("unrecognized pragma(%s)", ident->toChars()); | |
2231 | |
2232 if (body) | |
2233 { | |
2234 body = body->semantic(sc); | |
2235 } | |
2236 return body; | |
2237 } | |
2238 | |
2239 int PragmaStatement::usesEH() | |
2240 { | |
2241 return body && body->usesEH(); | |
2242 } | |
2243 | |
336 | 2244 int PragmaStatement::blockExit() |
2245 { | |
2246 int result = BEfallthru; | |
2247 #if 0 // currently, no code is generated for Pragma's, so it's just fallthru | |
2248 if (arrayExpressionCanThrow(args)) | |
2249 result |= BEthrow; | |
2250 if (body) | |
2251 result |= body->blockExit(); | |
2252 #endif | |
2253 return result; | |
2254 } | |
2255 | |
159 | 2256 void PragmaStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
2257 { | |
2258 buf->writestring("pragma ("); | |
2259 buf->writestring(ident->toChars()); | |
2260 if (args && args->dim) | |
2261 { | |
2262 buf->writestring(", "); | |
2263 argsToCBuffer(buf, args, hgs); | |
2264 } | |
2265 buf->writeByte(')'); | |
2266 if (body) | |
2267 { | |
2268 buf->writenl(); | |
2269 buf->writeByte('{'); | |
2270 buf->writenl(); | |
2271 | |
2272 body->toCBuffer(buf, hgs); | |
2273 | |
2274 buf->writeByte('}'); | |
2275 buf->writenl(); | |
2276 } | |
2277 else | |
2278 { | |
2279 buf->writeByte(';'); | |
2280 buf->writenl(); | |
2281 } | |
2282 } | |
2283 | |
2284 | |
2285 /******************************** StaticAssertStatement ***************************/ | |
2286 | |
2287 StaticAssertStatement::StaticAssertStatement(StaticAssert *sa) | |
2288 : Statement(sa->loc) | |
2289 { | |
2290 this->sa = sa; | |
2291 } | |
2292 | |
2293 Statement *StaticAssertStatement::syntaxCopy() | |
2294 { | |
2295 StaticAssertStatement *s = new StaticAssertStatement((StaticAssert *)sa->syntaxCopy(NULL)); | |
2296 return s; | |
2297 } | |
2298 | |
2299 Statement *StaticAssertStatement::semantic(Scope *sc) | |
2300 { | |
2301 sa->semantic2(sc); | |
2302 return NULL; | |
2303 } | |
2304 | |
2305 void StaticAssertStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2306 { | |
2307 sa->toCBuffer(buf, hgs); | |
2308 } | |
2309 | |
2310 | |
2311 /******************************** SwitchStatement ***************************/ | |
2312 | |
2313 SwitchStatement::SwitchStatement(Loc loc, Expression *c, Statement *b) | |
2314 : Statement(loc) | |
2315 { | |
2316 condition = c; | |
2317 body = b; | |
2318 sdefault = NULL; | |
2319 cases = NULL; | |
2320 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
|
2321 // LDC |
c6d6a68bb5db
Add back some enclosing scope-exit information to the frontend to produce
Christian Kamm <kamm incasoftware de>
parents:
1141
diff
changeset
|
2322 enclosingScopeExit = NULL; |
159 | 2323 } |
2324 | |
2325 Statement *SwitchStatement::syntaxCopy() | |
2326 { | |
2327 SwitchStatement *s = new SwitchStatement(loc, | |
2328 condition->syntaxCopy(), body->syntaxCopy()); | |
2329 return s; | |
2330 } | |
2331 | |
2332 Statement *SwitchStatement::semantic(Scope *sc) | |
2333 { | |
2334 //printf("SwitchStatement::semantic(%p)\n", this); | |
2335 assert(!cases); // ensure semantic() is only run once | |
2336 | |
1159
c6d6a68bb5db
Add back some enclosing scope-exit information to the frontend to produce
Christian Kamm <kamm incasoftware de>
parents:
1141
diff
changeset
|
2337 // LDC |
c6d6a68bb5db
Add back some enclosing scope-exit information to the frontend to produce
Christian Kamm <kamm incasoftware de>
parents:
1141
diff
changeset
|
2338 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
|
2339 |
159 | 2340 condition = condition->semantic(sc); |
2341 condition = resolveProperties(sc, condition); | |
2342 if (condition->type->isString()) | |
2343 { | |
2344 // If it's not an array, cast it to one | |
2345 if (condition->type->ty != Tarray) | |
2346 { | |
2347 condition = condition->implicitCastTo(sc, condition->type->nextOf()->arrayOf()); | |
2348 } | |
2349 } | |
2350 else | |
2351 { condition = condition->integralPromotions(sc); | |
2352 condition->checkIntegral(); | |
2353 } | |
2354 condition = condition->optimize(WANTvalue); | |
2355 | |
2356 sc = sc->push(); | |
2357 sc->sbreak = this; | |
2358 sc->sw = this; | |
2359 | |
2360 cases = new Array(); | |
2361 sc->noctor++; // BUG: should use Scope::mergeCallSuper() for each case instead | |
2362 body = body->semantic(sc); | |
2363 sc->noctor--; | |
2364 | |
2365 // Resolve any goto case's with exp | |
2366 for (int i = 0; i < gotoCases.dim; i++) | |
2367 { | |
2368 GotoCaseStatement *gcs = (GotoCaseStatement *)gotoCases.data[i]; | |
2369 | |
2370 if (!gcs->exp) | |
2371 { | |
2372 gcs->error("no case statement following goto case;"); | |
2373 break; | |
2374 } | |
2375 | |
2376 for (Scope *scx = sc; scx; scx = scx->enclosing) | |
2377 { | |
2378 if (!scx->sw) | |
2379 continue; | |
2380 for (int j = 0; j < scx->sw->cases->dim; j++) | |
2381 { | |
2382 CaseStatement *cs = (CaseStatement *)scx->sw->cases->data[j]; | |
2383 | |
2384 if (cs->exp->equals(gcs->exp)) | |
2385 { | |
2386 gcs->cs = cs; | |
2387 goto Lfoundcase; | |
2388 } | |
2389 } | |
2390 } | |
2391 gcs->error("case %s not found", gcs->exp->toChars()); | |
2392 | |
2393 Lfoundcase: | |
2394 ; | |
2395 } | |
2396 | |
2397 if (!sc->sw->sdefault) | |
2398 { hasNoDefault = 1; | |
2399 | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
945
diff
changeset
|
2400 warning("switch statement has no default"); |
159 | 2401 |
2402 // Generate runtime error if the default is hit | |
2403 Statements *a = new Statements(); | |
2404 CompoundStatement *cs; | |
2405 Statement *s; | |
2406 | |
2407 if (global.params.useSwitchError) | |
2408 s = new SwitchErrorStatement(loc); | |
2409 else | |
2410 { Expression *e = new HaltExp(loc); | |
2411 s = new ExpStatement(loc, e); | |
2412 } | |
2413 | |
2414 a->reserve(4); | |
2415 a->push(body); | |
901 | 2416 |
2417 // LDC needs semantic to be run on break | |
2418 Statement *breakstmt = new BreakStatement(loc, NULL); | |
2419 breakstmt->semantic(sc); | |
2420 a->push(breakstmt); | |
2421 | |
159 | 2422 sc->sw->sdefault = new DefaultStatement(loc, s); |
2423 a->push(sc->sw->sdefault); | |
2424 cs = new CompoundStatement(loc, a); | |
2425 body = cs; | |
2426 } | |
2427 | |
2428 sc->pop(); | |
2429 return this; | |
2430 } | |
2431 | |
2432 int SwitchStatement::hasBreak() | |
2433 { | |
2434 return TRUE; | |
2435 } | |
2436 | |
2437 int SwitchStatement::usesEH() | |
2438 { | |
2439 return body ? body->usesEH() : 0; | |
2440 } | |
2441 | |
336 | 2442 int SwitchStatement::blockExit() |
2443 { int result = BEnone; | |
2444 if (condition->canThrow()) | |
2445 result |= BEthrow; | |
2446 | |
2447 if (body) | |
2448 { result |= body->blockExit(); | |
2449 if (result & BEbreak) | |
2450 { result |= BEfallthru; | |
2451 result &= ~BEbreak; | |
2452 } | |
2453 } | |
2454 else | |
2455 result |= BEfallthru; | |
2456 | |
2457 return result; | |
2458 } | |
2459 | |
159 | 2460 void SwitchStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
2461 { | |
2462 buf->writestring("switch ("); | |
2463 condition->toCBuffer(buf, hgs); | |
2464 buf->writebyte(')'); | |
2465 buf->writenl(); | |
2466 if (body) | |
2467 { | |
2468 if (!body->isScopeStatement()) | |
2469 { buf->writebyte('{'); | |
2470 buf->writenl(); | |
2471 body->toCBuffer(buf, hgs); | |
2472 buf->writebyte('}'); | |
2473 buf->writenl(); | |
2474 } | |
2475 else | |
2476 { | |
2477 body->toCBuffer(buf, hgs); | |
2478 } | |
2479 } | |
2480 } | |
2481 | |
2482 /******************************** CaseStatement ***************************/ | |
2483 | |
2484 CaseStatement::CaseStatement(Loc loc, Expression *exp, Statement *s) | |
2485 : Statement(loc) | |
2486 { | |
2487 this->exp = exp; | |
2488 this->statement = s; | |
2489 cblock = NULL; | |
2490 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
|
2491 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
|
2492 // LDC |
c6d6a68bb5db
Add back some enclosing scope-exit information to the frontend to produce
Christian Kamm <kamm incasoftware de>
parents:
1141
diff
changeset
|
2493 enclosingScopeExit = NULL; |
159 | 2494 } |
2495 | |
2496 Statement *CaseStatement::syntaxCopy() | |
2497 { | |
2498 CaseStatement *s = new CaseStatement(loc, exp->syntaxCopy(), statement->syntaxCopy()); | |
2499 return s; | |
2500 } | |
2501 | |
2502 Statement *CaseStatement::semantic(Scope *sc) | |
2503 { SwitchStatement *sw = sc->sw; | |
2504 | |
2505 //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
|
2506 |
159 | 2507 exp = exp->semantic(sc); |
2508 if (sw) | |
846
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
2509 { |
1164
166042b48c28
Fix bug in CaseStatement::semantic when there's no enclosing switch.
Christian Kamm <kamm incasoftware de>
parents:
1159
diff
changeset
|
2510 // LDC |
166042b48c28
Fix bug in CaseStatement::semantic when there's no enclosing switch.
Christian Kamm <kamm incasoftware de>
parents:
1159
diff
changeset
|
2511 enclosingScopeExit = sc->enclosingScopeExit; |
166042b48c28
Fix bug in CaseStatement::semantic when there's no enclosing switch.
Christian Kamm <kamm incasoftware de>
parents:
1159
diff
changeset
|
2512 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
|
2513 { |
166042b48c28
Fix bug in CaseStatement::semantic when there's no enclosing switch.
Christian Kamm <kamm incasoftware de>
parents:
1159
diff
changeset
|
2514 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
|
2515 } |
166042b48c28
Fix bug in CaseStatement::semantic when there's no enclosing switch.
Christian Kamm <kamm incasoftware de>
parents:
1159
diff
changeset
|
2516 |
159 | 2517 exp = exp->implicitCastTo(sc, sw->condition->type); |
2518 exp = exp->optimize(WANTvalue | WANTinterpret); | |
2519 if (exp->op != TOKstring && exp->op != TOKint64) | |
2520 { | |
2521 error("case must be a string or an integral constant, not %s", exp->toChars()); | |
2522 exp = new IntegerExp(0); | |
2523 } | |
2524 | |
846
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
2525 for (int i = 0; i < sw->cases->dim; i++) |
159 | 2526 { |
2527 CaseStatement *cs = (CaseStatement *)sw->cases->data[i]; | |
2528 | |
2529 //printf("comparing '%s' with '%s'\n", exp->toChars(), cs->exp->toChars()); | |
2530 if (cs->exp->equals(exp)) | |
2531 { error("duplicate case %s in switch statement", exp->toChars()); | |
2532 break; | |
2533 } | |
2534 } | |
2535 | |
2536 sw->cases->push(this); | |
2537 | |
2538 // 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
|
2539 for (size_t i = 0; i < sw->gotoCases.dim; i++) |
159 | 2540 { |
2541 GotoCaseStatement *gcs = (GotoCaseStatement *)sw->gotoCases.data[i]; | |
2542 | |
2543 if (!gcs->exp) | |
2544 { | |
2545 gcs->cs = this; | |
2546 sw->gotoCases.remove(i); // remove from array | |
2547 } | |
2548 } | |
2549 } | |
2550 else | |
2551 error("case not in switch statement"); | |
2552 statement = statement->semantic(sc); | |
2553 return this; | |
2554 } | |
2555 | |
2556 int CaseStatement::compare(Object *obj) | |
2557 { | |
2558 // Sort cases so we can do an efficient lookup | |
2559 CaseStatement *cs2 = (CaseStatement *)(obj); | |
2560 | |
2561 return exp->compare(cs2->exp); | |
2562 } | |
2563 | |
2564 int CaseStatement::usesEH() | |
2565 { | |
2566 return statement->usesEH(); | |
2567 } | |
2568 | |
336 | 2569 int CaseStatement::blockExit() |
2570 { | |
2571 return statement->blockExit(); | |
2572 } | |
2573 | |
159 | 2574 int CaseStatement::comeFrom() |
2575 { | |
2576 return TRUE; | |
2577 } | |
2578 | |
2579 void CaseStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2580 { | |
2581 buf->writestring("case "); | |
2582 exp->toCBuffer(buf, hgs); | |
2583 buf->writebyte(':'); | |
2584 buf->writenl(); | |
2585 statement->toCBuffer(buf, hgs); | |
2586 } | |
2587 | |
2588 /******************************** DefaultStatement ***************************/ | |
2589 | |
2590 DefaultStatement::DefaultStatement(Loc loc, Statement *s) | |
2591 : Statement(loc) | |
2592 { | |
2593 this->statement = s; | |
2594 #if IN_GCC | |
2595 + cblock = NULL; | |
2596 #endif | |
2597 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
|
2598 // LDC |
c6d6a68bb5db
Add back some enclosing scope-exit information to the frontend to produce
Christian Kamm <kamm incasoftware de>
parents:
1141
diff
changeset
|
2599 enclosingScopeExit = NULL; |
159 | 2600 } |
2601 | |
2602 Statement *DefaultStatement::syntaxCopy() | |
2603 { | |
2604 DefaultStatement *s = new DefaultStatement(loc, statement->syntaxCopy()); | |
2605 return s; | |
2606 } | |
2607 | |
2608 Statement *DefaultStatement::semantic(Scope *sc) | |
2609 { | |
336 | 2610 //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
|
2611 |
159 | 2612 if (sc->sw) |
2613 { | |
2614 if (sc->sw->sdefault) | |
2615 { | |
2616 error("switch statement already has a default"); | |
2617 } | |
2618 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
|
2619 |
c6d6a68bb5db
Add back some enclosing scope-exit information to the frontend to produce
Christian Kamm <kamm incasoftware de>
parents:
1141
diff
changeset
|
2620 // LDC |
c6d6a68bb5db
Add back some enclosing scope-exit information to the frontend to produce
Christian Kamm <kamm incasoftware de>
parents:
1141
diff
changeset
|
2621 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
|
2622 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
|
2623 { |
c6d6a68bb5db
Add back some enclosing scope-exit information to the frontend to produce
Christian Kamm <kamm incasoftware de>
parents:
1141
diff
changeset
|
2624 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
|
2625 } |
159 | 2626 } |
2627 else | |
2628 error("default not in switch statement"); | |
2629 statement = statement->semantic(sc); | |
2630 return this; | |
2631 } | |
2632 | |
2633 int DefaultStatement::usesEH() | |
2634 { | |
2635 return statement->usesEH(); | |
2636 } | |
2637 | |
336 | 2638 int DefaultStatement::blockExit() |
2639 { | |
2640 return statement->blockExit(); | |
2641 } | |
2642 | |
159 | 2643 int DefaultStatement::comeFrom() |
2644 { | |
2645 return TRUE; | |
2646 } | |
2647 | |
2648 void DefaultStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2649 { | |
2650 buf->writestring("default:\n"); | |
2651 statement->toCBuffer(buf, hgs); | |
2652 } | |
2653 | |
2654 /******************************** GotoDefaultStatement ***************************/ | |
2655 | |
2656 GotoDefaultStatement::GotoDefaultStatement(Loc loc) | |
2657 : Statement(loc) | |
2658 { | |
2659 sw = NULL; | |
2660 } | |
2661 | |
2662 Statement *GotoDefaultStatement::syntaxCopy() | |
2663 { | |
2664 GotoDefaultStatement *s = new GotoDefaultStatement(loc); | |
2665 return s; | |
2666 } | |
2667 | |
2668 Statement *GotoDefaultStatement::semantic(Scope *sc) | |
2669 { | |
2670 sw = sc->sw; | |
2671 if (!sw) | |
2672 error("goto default not in switch statement"); | |
2673 return this; | |
2674 } | |
2675 | |
336 | 2676 int GotoDefaultStatement::blockExit() |
2677 { | |
2678 return BEgoto; | |
2679 } | |
2680 | |
159 | 2681 void GotoDefaultStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
2682 { | |
2683 buf->writestring("goto default;\n"); | |
2684 } | |
2685 | |
2686 /******************************** GotoCaseStatement ***************************/ | |
2687 | |
2688 GotoCaseStatement::GotoCaseStatement(Loc loc, Expression *exp) | |
2689 : Statement(loc) | |
2690 { | |
2691 cs = NULL; | |
2692 this->exp = exp; | |
2693 sw = NULL; | |
2694 } | |
2695 | |
2696 Statement *GotoCaseStatement::syntaxCopy() | |
2697 { | |
2698 Expression *e = exp ? exp->syntaxCopy() : NULL; | |
2699 GotoCaseStatement *s = new GotoCaseStatement(loc, e); | |
2700 return s; | |
2701 } | |
2702 | |
2703 Statement *GotoCaseStatement::semantic(Scope *sc) | |
2704 { | |
2705 if (exp) | |
2706 exp = exp->semantic(sc); | |
2707 | |
2708 if (!sc->sw) | |
2709 error("goto case not in switch statement"); | |
2710 else | |
2711 { | |
2712 sw = sc->sw; | |
2713 sc->sw->gotoCases.push(this); | |
2714 if (exp) | |
2715 { | |
2716 exp = exp->implicitCastTo(sc, sc->sw->condition->type); | |
2717 exp = exp->optimize(WANTvalue); | |
2718 } | |
2719 } | |
2720 return this; | |
2721 } | |
2722 | |
336 | 2723 int GotoCaseStatement::blockExit() |
2724 { | |
2725 return BEgoto; | |
2726 } | |
2727 | |
159 | 2728 void GotoCaseStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
2729 { | |
2730 buf->writestring("goto case"); | |
2731 if (exp) | |
2732 { buf->writebyte(' '); | |
2733 exp->toCBuffer(buf, hgs); | |
2734 } | |
2735 buf->writebyte(';'); | |
2736 buf->writenl(); | |
2737 } | |
2738 | |
2739 /******************************** SwitchErrorStatement ***************************/ | |
2740 | |
2741 SwitchErrorStatement::SwitchErrorStatement(Loc loc) | |
2742 : Statement(loc) | |
2743 { | |
2744 } | |
2745 | |
336 | 2746 int SwitchErrorStatement::blockExit() |
2747 { | |
2748 return BEthrow; | |
2749 } | |
2750 | |
159 | 2751 void SwitchErrorStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
2752 { | |
2753 buf->writestring("SwitchErrorStatement::toCBuffer()"); | |
2754 buf->writenl(); | |
2755 } | |
2756 | |
2757 /******************************** ReturnStatement ***************************/ | |
2758 | |
2759 ReturnStatement::ReturnStatement(Loc loc, Expression *exp) | |
2760 : Statement(loc) | |
2761 { | |
2762 this->exp = exp; | |
2763 } | |
2764 | |
2765 Statement *ReturnStatement::syntaxCopy() | |
2766 { | |
2767 Expression *e = NULL; | |
2768 if (exp) | |
2769 e = exp->syntaxCopy(); | |
2770 ReturnStatement *s = new ReturnStatement(loc, e); | |
2771 return s; | |
2772 } | |
2773 | |
2774 Statement *ReturnStatement::semantic(Scope *sc) | |
2775 { | |
2776 //printf("ReturnStatement::semantic() %s\n", toChars()); | |
2777 | |
2778 FuncDeclaration *fd = sc->parent->isFuncDeclaration(); | |
2779 Scope *scx = sc; | |
2780 int implicit0 = 0; | |
2781 | |
2782 if (sc->fes) | |
2783 { | |
2784 // Find scope of function foreach is in | |
2785 for (; 1; scx = scx->enclosing) | |
2786 { | |
2787 assert(scx); | |
2788 if (scx->func != fd) | |
2789 { fd = scx->func; // fd is now function enclosing foreach | |
2790 break; | |
2791 } | |
2792 } | |
2793 } | |
2794 | |
2795 Type *tret = fd->type->nextOf(); | |
2796 if (fd->tintro) | |
336 | 2797 /* We'll be implicitly casting the return expression to tintro |
2798 */ | |
159 | 2799 tret = fd->tintro->nextOf(); |
2800 Type *tbret = NULL; | |
2801 | |
2802 if (tret) | |
2803 tbret = tret->toBasetype(); | |
2804 | |
2805 // main() returns 0, even if it returns void | |
2806 if (!exp && (!tbret || tbret->ty == Tvoid) && fd->isMain()) | |
2807 { implicit0 = 1; | |
2808 exp = new IntegerExp(0); | |
2809 } | |
2810 | |
2811 if (sc->incontract || scx->incontract) | |
2812 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
|
2813 if (sc->enclosingFinally || scx->enclosingFinally) |
159 | 2814 error("return statements cannot be in finally, scope(exit) or scope(success) bodies"); |
2815 | |
2816 if (fd->isCtorDeclaration()) | |
2817 { | |
2818 // Constructors implicitly do: | |
2819 // return this; | |
2820 if (exp && exp->op != TOKthis) | |
2821 error("cannot return expression from constructor"); | |
2822 exp = new ThisExp(0); | |
2823 } | |
2824 | |
2825 if (!exp) | |
2826 fd->nrvo_can = 0; | |
2827 | |
2828 if (exp) | |
2829 { | |
2830 fd->hasReturnExp |= 1; | |
2831 | |
2832 exp = exp->semantic(sc); | |
2833 exp = resolveProperties(sc, exp); | |
2834 exp = exp->optimize(WANTvalue); | |
2835 | |
2836 if (fd->nrvo_can && exp->op == TOKvar) | |
2837 { VarExp *ve = (VarExp *)exp; | |
2838 VarDeclaration *v = ve->var->isVarDeclaration(); | |
2839 | |
2840 if (!v || v->isOut() || v->isRef()) | |
2841 fd->nrvo_can = 0; | |
2842 else if (fd->nrvo_var == NULL) | |
2843 { if (!v->isDataseg() && !v->isParameter() && v->toParent2() == fd) | |
2844 fd->nrvo_var = v; | |
2845 else | |
2846 fd->nrvo_can = 0; | |
2847 } | |
2848 else if (fd->nrvo_var != v) | |
2849 fd->nrvo_can = 0; | |
2850 } | |
2851 else | |
2852 fd->nrvo_can = 0; | |
2853 | |
2854 if (fd->returnLabel && tbret->ty != Tvoid) | |
2855 { | |
2856 } | |
2857 else if (fd->inferRetType) | |
2858 { | |
2859 if (fd->type->nextOf()) | |
2860 { | |
2861 if (!exp->type->equals(fd->type->nextOf())) | |
2862 error("mismatched function return type inference of %s and %s", | |
2863 exp->type->toChars(), fd->type->nextOf()->toChars()); | |
2864 } | |
2865 else | |
2866 { | |
2867 fd->type->next = exp->type; | |
2868 fd->type = fd->type->semantic(loc, sc); | |
2869 if (!fd->tintro) | |
2870 { tret = fd->type->nextOf(); | |
2871 tbret = tret->toBasetype(); | |
2872 } | |
2873 } | |
2874 } | |
2875 else if (tbret->ty != Tvoid) | |
2876 { | |
2877 exp = exp->implicitCastTo(sc, tret); | |
2878 } | |
2879 } | |
2880 else if (fd->inferRetType) | |
2881 { | |
2882 if (fd->type->nextOf()) | |
2883 { | |
2884 if (fd->type->nextOf()->ty != Tvoid) | |
2885 error("mismatched function return type inference of void and %s", | |
2886 fd->type->nextOf()->toChars()); | |
2887 } | |
2888 else | |
2889 { | |
2890 fd->type->next = Type::tvoid; | |
2891 fd->type = fd->type->semantic(loc, sc); | |
2892 if (!fd->tintro) | |
2893 { tret = Type::tvoid; | |
2894 tbret = tret; | |
2895 } | |
2896 } | |
2897 } | |
2898 else if (tbret->ty != Tvoid) // if non-void return | |
2899 error("return expression expected"); | |
2900 | |
2901 if (sc->fes) | |
2902 { | |
2903 Statement *s; | |
2904 | |
2905 if (exp && !implicit0) | |
2906 { | |
2907 exp = exp->implicitCastTo(sc, tret); | |
2908 } | |
2909 if (!exp || exp->op == TOKint64 || exp->op == TOKfloat64 || | |
2910 exp->op == TOKimaginary80 || exp->op == TOKcomplex80 || | |
2911 exp->op == TOKthis || exp->op == TOKsuper || exp->op == TOKnull || | |
2912 exp->op == TOKstring) | |
2913 { | |
2914 sc->fes->cases.push(this); | |
846
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
2915 // Construct: return cases.dim+1; |
159 | 2916 s = new ReturnStatement(0, new IntegerExp(sc->fes->cases.dim + 1)); |
2917 } | |
2918 else if (fd->type->nextOf()->toBasetype() == Type::tvoid) | |
2919 { | |
2920 s = new ReturnStatement(0, NULL); | |
2921 sc->fes->cases.push(s); | |
2922 | |
2923 // 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
|
2924 Statement *s1 = new ExpStatement(loc, exp); |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
2925 Statement *s2 = new ReturnStatement(0, new IntegerExp(sc->fes->cases.dim + 1)); |
159 | 2926 s = new CompoundStatement(loc, s1, s2); |
2927 } | |
2928 else | |
2929 { | |
2930 // Construct: return vresult; | |
2931 if (!fd->vresult) | |
846
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
2932 { // Declare vresult |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
2933 VarDeclaration *v = new VarDeclaration(loc, tret, Id::result, NULL); |
1530
05c235309d6f
Make the auto storage class never have the same meaning as scope.
Christian Kamm <kamm incasoftware de>
parents:
1416
diff
changeset
|
2934 v->noscope = 1; |
159 | 2935 v->semantic(scx); |
2936 if (!scx->insert(v)) | |
2937 assert(0); | |
2938 v->parent = fd; | |
2939 fd->vresult = v; | |
2940 } | |
2941 | |
846
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
2942 s = new ReturnStatement(0, new VarExp(0, fd->vresult)); |
159 | 2943 sc->fes->cases.push(s); |
2944 | |
2945 // 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
|
2946 exp = new AssignExp(loc, new VarExp(0, fd->vresult), exp); |
159 | 2947 exp = exp->semantic(sc); |
846
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
2948 Statement *s1 = new ExpStatement(loc, exp); |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
2949 Statement *s2 = new ReturnStatement(0, new IntegerExp(sc->fes->cases.dim + 1)); |
159 | 2950 s = new CompoundStatement(loc, s1, s2); |
2951 } | |
2952 return s; | |
2953 } | |
2954 | |
2955 if (exp) | |
2956 { | |
2957 if (fd->returnLabel && tbret->ty != Tvoid) | |
2958 { | |
2959 assert(fd->vresult); | |
2960 VarExp *v = new VarExp(0, fd->vresult); | |
2961 | |
2962 exp = new AssignExp(loc, v, exp); | |
2963 exp = exp->semantic(sc); | |
2964 } | |
2965 //exp->dump(0); | |
2966 //exp->print(); | |
2967 exp->checkEscape(); | |
2968 } | |
2969 | |
2970 /* BUG: need to issue an error on: | |
2971 * this | |
2972 * { if (x) return; | |
2973 * super(); | |
2974 * } | |
2975 */ | |
2976 | |
2977 if (sc->callSuper & CSXany_ctor && | |
2978 !(sc->callSuper & (CSXthis_ctor | CSXsuper_ctor))) | |
2979 error("return without calling constructor"); | |
2980 | |
2981 sc->callSuper |= CSXreturn; | |
2982 | |
2983 // See if all returns are instead to be replaced with a goto returnLabel; | |
2984 if (fd->returnLabel) | |
2985 { | |
2986 GotoStatement *gs = new GotoStatement(loc, Id::returnLabel); | |
2987 | |
2988 gs->label = fd->returnLabel; | |
2989 if (exp) | |
846
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
2990 { /* Replace: return exp; |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
2991 * with: exp; goto returnLabel; |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
2992 */ |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
2993 Statement *s = new ExpStatement(0, exp); |
159 | 2994 return new CompoundStatement(loc, s, gs); |
2995 } | |
2996 return gs; | |
2997 } | |
2998 | |
2999 if (exp && tbret->ty == Tvoid && !fd->isMain()) | |
336 | 3000 { |
3001 /* Replace: | |
3002 * return exp; | |
3003 * with: | |
3004 * exp; return; | |
3005 */ | |
3006 Statement *s = new ExpStatement(loc, exp); | |
159 | 3007 loc = 0; |
3008 exp = NULL; | |
3009 return new CompoundStatement(loc, s, this); | |
3010 } | |
3011 | |
3012 return this; | |
3013 } | |
3014 | |
336 | 3015 int ReturnStatement::blockExit() |
3016 { int result = BEreturn; | |
3017 | |
3018 if (exp && exp->canThrow()) | |
3019 result |= BEthrow; | |
3020 return result; | |
3021 } | |
3022 | |
159 | 3023 void ReturnStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
3024 { | |
3025 buf->printf("return "); | |
3026 if (exp) | |
3027 exp->toCBuffer(buf, hgs); | |
3028 buf->writeByte(';'); | |
3029 buf->writenl(); | |
3030 } | |
3031 | |
3032 /******************************** BreakStatement ***************************/ | |
3033 | |
3034 BreakStatement::BreakStatement(Loc loc, Identifier *ident) | |
3035 : Statement(loc) | |
3036 { | |
3037 this->ident = ident; | |
3038 } | |
3039 | |
3040 Statement *BreakStatement::syntaxCopy() | |
3041 { | |
3042 BreakStatement *s = new BreakStatement(loc, ident); | |
3043 return s; | |
3044 } | |
3045 | |
3046 Statement *BreakStatement::semantic(Scope *sc) | |
3047 { | |
336 | 3048 //printf("BreakStatement::semantic()\n"); |
159 | 3049 // If: |
3050 // break Identifier; | |
3051 if (ident) | |
3052 { | |
3053 Scope *scx; | |
3054 FuncDeclaration *thisfunc = sc->func; | |
3055 | |
3056 for (scx = sc; scx; scx = scx->enclosing) | |
3057 { | |
3058 LabelStatement *ls; | |
3059 | |
3060 if (scx->func != thisfunc) // if in enclosing function | |
3061 { | |
3062 if (sc->fes) // if this is the body of a foreach | |
3063 { | |
3064 /* Post this statement to the fes, and replace | |
3065 * it with a return value that caller will put into | |
3066 * a switch. Caller will figure out where the break | |
3067 * label actually is. | |
3068 * Case numbers start with 2, not 0, as 0 is continue | |
3069 * and 1 is break. | |
3070 */ | |
3071 Statement *s; | |
3072 sc->fes->cases.push(this); | |
3073 s = new ReturnStatement(0, new IntegerExp(sc->fes->cases.dim + 1)); | |
3074 return s; | |
3075 } | |
3076 break; // can't break to it | |
3077 } | |
3078 | |
3079 ls = scx->slabel; | |
3080 if (ls && ls->ident == ident) | |
3081 { | |
3082 Statement *s = ls->statement; | |
3083 | |
3084 if (!s->hasBreak()) | |
3085 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
|
3086 if (ls->enclosingFinally != sc->enclosingFinally) |
159 | 3087 error("cannot break out of finally block"); |
3088 | |
3089 this->target = ls; | |
3090 return this; | |
3091 } | |
3092 } | |
3093 error("enclosing label '%s' for break not found", ident->toChars()); | |
3094 } | |
3095 else if (!sc->sbreak) | |
3096 { | |
3097 if (sc->fes) | |
3098 { Statement *s; | |
3099 | |
3100 // Replace break; with return 1; | |
3101 s = new ReturnStatement(0, new IntegerExp(1)); | |
3102 return s; | |
3103 } | |
3104 error("break is not inside a loop or switch"); | |
3105 } | |
3106 return this; | |
3107 } | |
3108 | |
336 | 3109 int BreakStatement::blockExit() |
3110 { | |
3111 //printf("BreakStatement::blockExit(%p) = x%x\n", this, ident ? BEgoto : BEbreak); | |
3112 return ident ? BEgoto : BEbreak; | |
3113 } | |
3114 | |
159 | 3115 void BreakStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
3116 { | |
3117 buf->writestring("break"); | |
3118 if (ident) | |
3119 { buf->writebyte(' '); | |
3120 buf->writestring(ident->toChars()); | |
3121 } | |
3122 buf->writebyte(';'); | |
3123 buf->writenl(); | |
3124 } | |
3125 | |
3126 /******************************** ContinueStatement ***************************/ | |
3127 | |
3128 ContinueStatement::ContinueStatement(Loc loc, Identifier *ident) | |
3129 : Statement(loc) | |
3130 { | |
3131 this->ident = ident; | |
3132 } | |
3133 | |
3134 Statement *ContinueStatement::syntaxCopy() | |
3135 { | |
3136 ContinueStatement *s = new ContinueStatement(loc, ident); | |
3137 return s; | |
3138 } | |
3139 | |
3140 Statement *ContinueStatement::semantic(Scope *sc) | |
3141 { | |
3142 //printf("ContinueStatement::semantic() %p\n", this); | |
3143 if (ident) | |
3144 { | |
3145 Scope *scx; | |
3146 FuncDeclaration *thisfunc = sc->func; | |
3147 | |
3148 for (scx = sc; scx; scx = scx->enclosing) | |
3149 { | |
3150 LabelStatement *ls; | |
3151 | |
3152 if (scx->func != thisfunc) // if in enclosing function | |
3153 { | |
3154 if (sc->fes) // if this is the body of a foreach | |
3155 { | |
3156 for (; scx; scx = scx->enclosing) | |
3157 { | |
3158 ls = scx->slabel; | |
3159 if (ls && ls->ident == ident && ls->statement == sc->fes) | |
3160 { | |
3161 // Replace continue ident; with return 0; | |
3162 return new ReturnStatement(0, new IntegerExp(0)); | |
3163 } | |
3164 } | |
3165 | |
3166 /* Post this statement to the fes, and replace | |
3167 * it with a return value that caller will put into | |
3168 * a switch. Caller will figure out where the break | |
3169 * label actually is. | |
3170 * Case numbers start with 2, not 0, as 0 is continue | |
3171 * and 1 is break. | |
3172 */ | |
3173 Statement *s; | |
3174 sc->fes->cases.push(this); | |
3175 s = new ReturnStatement(0, new IntegerExp(sc->fes->cases.dim + 1)); | |
3176 return s; | |
3177 } | |
3178 break; // can't continue to it | |
3179 } | |
3180 | |
3181 ls = scx->slabel; | |
3182 if (ls && ls->ident == ident) | |
3183 { | |
3184 Statement *s = ls->statement; | |
3185 | |
3186 if (!s->hasContinue()) | |
3187 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
|
3188 if (ls->enclosingFinally != sc->enclosingFinally) |
159 | 3189 error("cannot continue out of finally block"); |
3190 | |
3191 this->target = ls; | |
3192 return this; | |
3193 } | |
3194 } | |
3195 error("enclosing label '%s' for continue not found", ident->toChars()); | |
3196 } | |
3197 else if (!sc->scontinue) | |
3198 { | |
3199 if (sc->fes) | |
3200 { Statement *s; | |
3201 | |
3202 // Replace continue; with return 0; | |
3203 s = new ReturnStatement(0, new IntegerExp(0)); | |
3204 return s; | |
3205 } | |
3206 error("continue is not inside a loop"); | |
3207 } | |
3208 return this; | |
3209 } | |
3210 | |
336 | 3211 int ContinueStatement::blockExit() |
3212 { | |
3213 return ident ? BEgoto : BEcontinue; | |
3214 } | |
3215 | |
159 | 3216 void ContinueStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
3217 { | |
3218 buf->writestring("continue"); | |
3219 if (ident) | |
3220 { buf->writebyte(' '); | |
3221 buf->writestring(ident->toChars()); | |
3222 } | |
3223 buf->writebyte(';'); | |
3224 buf->writenl(); | |
3225 } | |
3226 | |
3227 /******************************** SynchronizedStatement ***************************/ | |
3228 | |
3229 SynchronizedStatement::SynchronizedStatement(Loc loc, Expression *exp, Statement *body) | |
3230 : Statement(loc) | |
3231 { | |
3232 this->exp = exp; | |
3233 this->body = body; | |
3234 this->esync = NULL; | |
664
eef8ac26c66c
Some missed LLVMDC -> LDC.
Christian Kamm <kamm incasoftware de>
parents:
510
diff
changeset
|
3235 // LDC |
309 | 3236 this->llsync = NULL; |
159 | 3237 } |
3238 | |
3239 SynchronizedStatement::SynchronizedStatement(Loc loc, elem *esync, Statement *body) | |
3240 : Statement(loc) | |
3241 { | |
3242 this->exp = NULL; | |
3243 this->body = body; | |
3244 this->esync = esync; | |
664
eef8ac26c66c
Some missed LLVMDC -> LDC.
Christian Kamm <kamm incasoftware de>
parents:
510
diff
changeset
|
3245 // LDC |
309 | 3246 this->llsync = NULL; |
159 | 3247 } |
3248 | |
3249 Statement *SynchronizedStatement::syntaxCopy() | |
3250 { | |
3251 Expression *e = exp ? exp->syntaxCopy() : NULL; | |
3252 SynchronizedStatement *s = new SynchronizedStatement(loc, e, body ? body->syntaxCopy() : NULL); | |
3253 return s; | |
3254 } | |
3255 | |
3256 Statement *SynchronizedStatement::semantic(Scope *sc) | |
3257 { | |
3258 if (exp) | |
3259 { ClassDeclaration *cd; | |
3260 | |
3261 exp = exp->semantic(sc); | |
3262 exp = resolveProperties(sc, exp); | |
3263 cd = exp->type->isClassHandle(); | |
3264 if (!cd) | |
3265 error("can only synchronize on class objects, not '%s'", exp->type->toChars()); | |
3266 else if (cd->isInterfaceDeclaration()) | |
3267 { Type *t = new TypeIdentifier(0, Id::Object); | |
3268 | |
3269 t = t->semantic(0, sc); | |
3270 exp = new CastExp(loc, exp, t); | |
3271 exp = exp->semantic(sc); | |
3272 } | |
3273 } | |
3274 if (body) | |
309 | 3275 { |
1141
f99a3b393c03
Reorganize EnclosingHandlers to require less changes to the frontend and allow us to
Christian Kamm <kamm incasoftware de>
parents:
1133
diff
changeset
|
3276 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
|
3277 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
|
3278 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
|
3279 sc->enclosingScopeExit = oldScopeExit; |
309 | 3280 } |
159 | 3281 return this; |
3282 } | |
3283 | |
3284 int SynchronizedStatement::hasBreak() | |
3285 { | |
3286 return FALSE; //TRUE; | |
3287 } | |
3288 | |
3289 int SynchronizedStatement::hasContinue() | |
3290 { | |
3291 return FALSE; //TRUE; | |
3292 } | |
3293 | |
3294 int SynchronizedStatement::usesEH() | |
3295 { | |
3296 return TRUE; | |
3297 } | |
3298 | |
336 | 3299 int SynchronizedStatement::blockExit() |
3300 { | |
3301 return body ? body->blockExit() : BEfallthru; | |
3302 } | |
3303 | |
159 | 3304 void SynchronizedStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
3305 { | |
3306 buf->writestring("synchronized"); | |
3307 if (exp) | |
3308 { buf->writebyte('('); | |
3309 exp->toCBuffer(buf, hgs); | |
3310 buf->writebyte(')'); | |
3311 } | |
3312 if (body) | |
3313 { | |
3314 buf->writebyte(' '); | |
3315 body->toCBuffer(buf, hgs); | |
3316 } | |
3317 } | |
3318 | |
3319 /******************************** WithStatement ***************************/ | |
3320 | |
3321 WithStatement::WithStatement(Loc loc, Expression *exp, Statement *body) | |
3322 : Statement(loc) | |
3323 { | |
3324 this->exp = exp; | |
3325 this->body = body; | |
3326 wthis = NULL; | |
3327 } | |
3328 | |
3329 Statement *WithStatement::syntaxCopy() | |
3330 { | |
3331 WithStatement *s = new WithStatement(loc, exp->syntaxCopy(), body ? body->syntaxCopy() : NULL); | |
3332 return s; | |
3333 } | |
3334 | |
3335 Statement *WithStatement::semantic(Scope *sc) | |
3336 { ScopeDsymbol *sym; | |
3337 Initializer *init; | |
3338 | |
3339 //printf("WithStatement::semantic()\n"); | |
3340 exp = exp->semantic(sc); | |
3341 exp = resolveProperties(sc, exp); | |
3342 if (exp->op == TOKimport) | |
3343 { ScopeExp *es = (ScopeExp *)exp; | |
3344 | |
3345 sym = es->sds; | |
3346 } | |
3347 else if (exp->op == TOKtype) | |
3348 { TypeExp *es = (TypeExp *)exp; | |
3349 | |
3350 sym = es->type->toDsymbol(sc)->isScopeDsymbol(); | |
3351 if (!sym) | |
3352 { error("%s has no members", es->toChars()); | |
3353 body = body->semantic(sc); | |
3354 return this; | |
3355 } | |
3356 } | |
3357 else | |
3358 { Type *t = exp->type; | |
3359 | |
3360 assert(t); | |
3361 t = t->toBasetype(); | |
3362 if (t->isClassHandle()) | |
3363 { | |
3364 init = new ExpInitializer(loc, exp); | |
3365 wthis = new VarDeclaration(loc, exp->type, Id::withSym, init); | |
3366 wthis->semantic(sc); | |
3367 | |
3368 sym = new WithScopeSymbol(this); | |
3369 sym->parent = sc->scopesym; | |
3370 } | |
3371 else if (t->ty == Tstruct) | |
3372 { | |
3373 Expression *e = exp->addressOf(sc); | |
3374 init = new ExpInitializer(loc, e); | |
3375 wthis = new VarDeclaration(loc, e->type, Id::withSym, init); | |
3376 wthis->semantic(sc); | |
3377 sym = new WithScopeSymbol(this); | |
3378 sym->parent = sc->scopesym; | |
3379 } | |
3380 else | |
3381 { error("with expressions must be class objects, not '%s'", exp->type->toChars()); | |
3382 return NULL; | |
3383 } | |
3384 } | |
3385 sc = sc->push(sym); | |
3386 | |
3387 if (body) | |
3388 body = body->semantic(sc); | |
3389 | |
3390 sc->pop(); | |
3391 | |
3392 return this; | |
3393 } | |
3394 | |
3395 void WithStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
3396 { | |
3397 buf->writestring("with ("); | |
3398 exp->toCBuffer(buf, hgs); | |
3399 buf->writestring(")\n"); | |
3400 if (body) | |
3401 body->toCBuffer(buf, hgs); | |
3402 } | |
3403 | |
3404 int WithStatement::usesEH() | |
3405 { | |
3406 return body ? body->usesEH() : 0; | |
3407 } | |
3408 | |
336 | 3409 int WithStatement::blockExit() |
3410 { | |
3411 int result = BEnone; | |
3412 if (exp->canThrow()) | |
3413 result = BEthrow; | |
3414 if (body) | |
3415 result |= body->blockExit(); | |
3416 else | |
3417 result |= BEfallthru; | |
3418 return result; | |
3419 } | |
3420 | |
159 | 3421 /******************************** TryCatchStatement ***************************/ |
3422 | |
3423 TryCatchStatement::TryCatchStatement(Loc loc, Statement *body, Array *catches) | |
3424 : Statement(loc) | |
3425 { | |
3426 this->body = body; | |
3427 this->catches = catches; | |
3428 } | |
3429 | |
3430 Statement *TryCatchStatement::syntaxCopy() | |
3431 { | |
3432 Array *a = new Array(); | |
3433 a->setDim(catches->dim); | |
3434 for (int i = 0; i < a->dim; i++) | |
3435 { Catch *c; | |
3436 | |
3437 c = (Catch *)catches->data[i]; | |
3438 c = c->syntaxCopy(); | |
3439 a->data[i] = c; | |
3440 } | |
3441 TryCatchStatement *s = new TryCatchStatement(loc, body->syntaxCopy(), a); | |
3442 return s; | |
3443 } | |
3444 | |
3445 Statement *TryCatchStatement::semantic(Scope *sc) | |
3446 { | |
3447 body = body->semanticScope(sc, NULL /*this*/, NULL); | |
3448 | |
336 | 3449 /* Even if body is NULL, still do semantic analysis on catches |
3450 */ | |
3451 for (size_t i = 0; i < catches->dim; i++) | |
3452 { Catch *c = (Catch *)catches->data[i]; | |
159 | 3453 c->semantic(sc); |
3454 | |
3455 // Determine if current catch 'hides' any previous catches | |
336 | 3456 for (size_t j = 0; j < i; j++) |
159 | 3457 { Catch *cj = (Catch *)catches->data[j]; |
3458 char *si = c->loc.toChars(); | |
3459 char *sj = cj->loc.toChars(); | |
3460 | |
3461 if (c->type->toBasetype()->implicitConvTo(cj->type->toBasetype())) | |
3462 error("catch at %s hides catch at %s", sj, si); | |
3463 } | |
3464 } | |
3465 return this; | |
3466 } | |
3467 | |
3468 int TryCatchStatement::hasBreak() | |
3469 { | |
3470 return FALSE; //TRUE; | |
3471 } | |
3472 | |
3473 int TryCatchStatement::usesEH() | |
3474 { | |
3475 return TRUE; | |
3476 } | |
3477 | |
336 | 3478 int TryCatchStatement::blockExit() |
3479 { int result; | |
3480 | |
3481 assert(body); | |
3482 result = body->blockExit(); | |
3483 | |
3484 for (size_t i = 0; i < catches->dim; i++) | |
3485 { | |
3486 Catch *c = (Catch *)catches->data[i]; | |
3487 result |= c->blockExit(); | |
3488 } | |
3489 return result; | |
3490 } | |
3491 | |
159 | 3492 void TryCatchStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
3493 { | |
3494 buf->writestring("try"); | |
3495 buf->writenl(); | |
3496 if (body) | |
3497 body->toCBuffer(buf, hgs); | |
336 | 3498 for (size_t i = 0; i < catches->dim; i++) |
159 | 3499 { |
3500 Catch *c = (Catch *)catches->data[i]; | |
3501 c->toCBuffer(buf, hgs); | |
3502 } | |
3503 } | |
3504 | |
3505 /******************************** Catch ***************************/ | |
3506 | |
3507 Catch::Catch(Loc loc, Type *t, Identifier *id, Statement *handler) | |
3508 { | |
3509 //printf("Catch(%s, loc = %s)\n", id->toChars(), loc.toChars()); | |
3510 this->loc = loc; | |
3511 this->type = t; | |
3512 this->ident = id; | |
3513 this->handler = handler; | |
3514 var = NULL; | |
3515 } | |
3516 | |
3517 Catch *Catch::syntaxCopy() | |
3518 { | |
3519 Catch *c = new Catch(loc, | |
3520 (type ? type->syntaxCopy() : NULL), | |
3521 ident, | |
3522 (handler ? handler->syntaxCopy() : NULL)); | |
3523 return c; | |
3524 } | |
3525 | |
3526 void Catch::semantic(Scope *sc) | |
3527 { ScopeDsymbol *sym; | |
3528 | |
3529 //printf("Catch::semantic(%s)\n", ident->toChars()); | |
3530 | |
3531 #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
|
3532 if (sc->enclosingFinally) |
159 | 3533 { |
3534 /* This is because the _d_local_unwind() gets the stack munged | |
3535 * up on this. The workaround is to place any try-catches into | |
3536 * a separate function, and call that. | |
3537 * To fix, have the compiler automatically convert the finally | |
3538 * body into a nested function. | |
3539 */ | |
3540 error(loc, "cannot put catch statement inside finally block"); | |
3541 } | |
3542 #endif | |
3543 | |
3544 sym = new ScopeDsymbol(); | |
3545 sym->parent = sc->scopesym; | |
3546 sc = sc->push(sym); | |
3547 | |
3548 if (!type) | |
3549 type = new TypeIdentifier(0, Id::Object); | |
3550 type = type->semantic(loc, sc); | |
3551 if (!type->toBasetype()->isClassHandle()) | |
3552 error("can only catch class objects, not '%s'", type->toChars()); | |
3553 else if (ident) | |
3554 { | |
3555 var = new VarDeclaration(loc, type, ident, NULL); | |
3556 var->parent = sc->parent; | |
3557 sc->insert(var); | |
3558 } | |
3559 handler = handler->semantic(sc); | |
3560 | |
3561 sc->pop(); | |
3562 } | |
3563 | |
336 | 3564 int Catch::blockExit() |
3565 { | |
3566 return handler ? handler->blockExit() : BEfallthru; | |
3567 } | |
3568 | |
159 | 3569 void Catch::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
3570 { | |
3571 buf->writestring("catch"); | |
3572 if (type) | |
3573 { buf->writebyte('('); | |
3574 type->toCBuffer(buf, ident, hgs); | |
3575 buf->writebyte(')'); | |
3576 } | |
3577 buf->writenl(); | |
3578 buf->writebyte('{'); | |
3579 buf->writenl(); | |
336 | 3580 if (handler) |
3581 handler->toCBuffer(buf, hgs); | |
159 | 3582 buf->writebyte('}'); |
3583 buf->writenl(); | |
3584 } | |
3585 | |
3586 /****************************** TryFinallyStatement ***************************/ | |
3587 | |
3588 TryFinallyStatement::TryFinallyStatement(Loc loc, Statement *body, Statement *finalbody) | |
3589 : Statement(loc) | |
3590 { | |
3591 this->body = body; | |
3592 this->finalbody = finalbody; | |
3593 } | |
3594 | |
3595 Statement *TryFinallyStatement::syntaxCopy() | |
3596 { | |
3597 TryFinallyStatement *s = new TryFinallyStatement(loc, | |
3598 body->syntaxCopy(), finalbody->syntaxCopy()); | |
3599 return s; | |
3600 } | |
3601 | |
3602 Statement *TryFinallyStatement::semantic(Scope *sc) | |
3603 { | |
3604 //printf("TryFinallyStatement::semantic()\n"); | |
3605 | |
1141
f99a3b393c03
Reorganize EnclosingHandlers to require less changes to the frontend and allow us to
Christian Kamm <kamm incasoftware de>
parents:
1133
diff
changeset
|
3606 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
|
3607 sc->enclosingScopeExit = this; |
159 | 3608 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
|
3609 sc->enclosingScopeExit = oldScopeExit; |
159 | 3610 |
3611 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
|
3612 sc->enclosingFinally = this; |
159 | 3613 sc->sbreak = NULL; |
3614 sc->scontinue = NULL; // no break or continue out of finally block | |
3615 finalbody = finalbody->semantic(sc); | |
3616 sc->pop(); | |
3617 return this; | |
3618 } | |
3619 | |
3620 void TryFinallyStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
3621 { | |
3622 buf->printf("try\n{\n"); | |
3623 body->toCBuffer(buf, hgs); | |
3624 buf->printf("}\nfinally\n{\n"); | |
3625 finalbody->toCBuffer(buf, hgs); | |
3626 buf->writeByte('}'); | |
3627 buf->writenl(); | |
3628 } | |
3629 | |
3630 int TryFinallyStatement::hasBreak() | |
3631 { | |
3632 return FALSE; //TRUE; | |
3633 } | |
3634 | |
3635 int TryFinallyStatement::hasContinue() | |
3636 { | |
3637 return FALSE; //TRUE; | |
3638 } | |
3639 | |
3640 int TryFinallyStatement::usesEH() | |
3641 { | |
3642 return TRUE; | |
3643 } | |
3644 | |
336 | 3645 int TryFinallyStatement::blockExit() |
3646 { | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3647 if (body) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3648 return body->blockExit(); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3649 return BEfallthru; |
336 | 3650 } |
3651 | |
159 | 3652 |
3653 /****************************** OnScopeStatement ***************************/ | |
3654 | |
3655 OnScopeStatement::OnScopeStatement(Loc loc, TOK tok, Statement *statement) | |
3656 : Statement(loc) | |
3657 { | |
3658 this->tok = tok; | |
3659 this->statement = statement; | |
3660 } | |
3661 | |
3662 Statement *OnScopeStatement::syntaxCopy() | |
3663 { | |
3664 OnScopeStatement *s = new OnScopeStatement(loc, | |
3665 tok, statement->syntaxCopy()); | |
3666 return s; | |
3667 } | |
3668 | |
3669 Statement *OnScopeStatement::semantic(Scope *sc) | |
3670 { | |
3671 /* semantic is called on results of scopeCode() */ | |
3672 return this; | |
3673 } | |
3674 | |
336 | 3675 int OnScopeStatement::blockExit() |
3676 { // At this point, this statement is just an empty placeholder | |
3677 return BEfallthru; | |
3678 } | |
3679 | |
159 | 3680 void OnScopeStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
3681 { | |
3682 buf->writestring(Token::toChars(tok)); | |
3683 buf->writebyte(' '); | |
3684 statement->toCBuffer(buf, hgs); | |
3685 } | |
3686 | |
3687 int OnScopeStatement::usesEH() | |
3688 { | |
3689 return (tok != TOKon_scope_success); | |
3690 } | |
3691 | |
3692 void OnScopeStatement::scopeCode(Statement **sentry, Statement **sexception, Statement **sfinally) | |
3693 { | |
3694 //printf("OnScopeStatement::scopeCode()\n"); | |
3695 //print(); | |
3696 *sentry = NULL; | |
3697 *sexception = NULL; | |
3698 *sfinally = NULL; | |
3699 switch (tok) | |
3700 { | |
3701 case TOKon_scope_exit: | |
3702 *sfinally = statement; | |
3703 break; | |
3704 | |
3705 case TOKon_scope_failure: | |
3706 *sexception = statement; | |
3707 break; | |
3708 | |
3709 case TOKon_scope_success: | |
3710 { | |
3711 /* Create: | |
3712 * sentry: int x = 0; | |
3713 * sexception: x = 1; | |
3714 * sfinally: if (!x) statement; | |
3715 */ | |
846
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
794
diff
changeset
|
3716 Identifier *id = Lexer::uniqueId("__os"); |
159 | 3717 |
3718 ExpInitializer *ie = new ExpInitializer(loc, new IntegerExp(0)); | |
3719 VarDeclaration *v = new VarDeclaration(loc, Type::tint32, id, ie); | |
3720 *sentry = new DeclarationStatement(loc, v); | |
3721 | |
3722 Expression *e = new IntegerExp(1); | |
3723 e = new AssignExp(0, new VarExp(0, v), e); | |
3724 *sexception = new ExpStatement(0, e); | |
3725 | |
3726 e = new VarExp(0, v); | |
3727 e = new NotExp(0, e); | |
3728 *sfinally = new IfStatement(0, NULL, e, statement, NULL); | |
3729 | |
3730 break; | |
3731 } | |
3732 | |
3733 default: | |
3734 assert(0); | |
3735 } | |
3736 } | |
3737 | |
3738 /******************************** ThrowStatement ***************************/ | |
3739 | |
3740 ThrowStatement::ThrowStatement(Loc loc, Expression *exp) | |
3741 : Statement(loc) | |
3742 { | |
3743 this->exp = exp; | |
3744 } | |
3745 | |
3746 Statement *ThrowStatement::syntaxCopy() | |
3747 { | |
3748 ThrowStatement *s = new ThrowStatement(loc, exp->syntaxCopy()); | |
3749 return s; | |
3750 } | |
3751 | |
3752 Statement *ThrowStatement::semantic(Scope *sc) | |
3753 { | |
3754 //printf("ThrowStatement::semantic()\n"); | |
3755 | |
3756 FuncDeclaration *fd = sc->parent->isFuncDeclaration(); | |
3757 fd->hasReturnExp |= 2; | |
3758 | |
3759 if (sc->incontract) | |
3760 error("Throw statements cannot be in contracts"); | |
3761 exp = exp->semantic(sc); | |
3762 exp = resolveProperties(sc, exp); | |
3763 if (!exp->type->toBasetype()->isClassHandle()) | |
3764 error("can only throw class objects, not type %s", exp->type->toChars()); | |
3765 return this; | |
3766 } | |
3767 | |
336 | 3768 int ThrowStatement::blockExit() |
3769 { | |
3770 return BEthrow; // obviously | |
3771 } | |
3772 | |
159 | 3773 void ThrowStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
3774 { | |
3775 buf->printf("throw "); | |
3776 exp->toCBuffer(buf, hgs); | |
3777 buf->writeByte(';'); | |
3778 buf->writenl(); | |
3779 } | |
3780 | |
3781 /******************************** VolatileStatement **************************/ | |
3782 | |
3783 VolatileStatement::VolatileStatement(Loc loc, Statement *statement) | |
3784 : Statement(loc) | |
3785 { | |
3786 this->statement = statement; | |
3787 } | |
3788 | |
3789 Statement *VolatileStatement::syntaxCopy() | |
3790 { | |
3791 VolatileStatement *s = new VolatileStatement(loc, | |
3792 statement ? statement->syntaxCopy() : NULL); | |
3793 return s; | |
3794 } | |
3795 | |
3796 Statement *VolatileStatement::semantic(Scope *sc) | |
3797 { | |
309 | 3798 if (statement) |
3799 { | |
1141
f99a3b393c03
Reorganize EnclosingHandlers to require less changes to the frontend and allow us to
Christian Kamm <kamm incasoftware de>
parents:
1133
diff
changeset
|
3800 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
|
3801 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
|
3802 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
|
3803 sc->enclosingScopeExit = oldScopeExit; |
309 | 3804 } |
159 | 3805 return this; |
3806 } | |
3807 | |
3808 Statements *VolatileStatement::flatten(Scope *sc) | |
3809 { | |
3810 Statements *a; | |
3811 | |
3812 a = statement ? statement->flatten(sc) : NULL; | |
3813 if (a) | |
3814 { for (int i = 0; i < a->dim; i++) | |
3815 { Statement *s = (Statement *)a->data[i]; | |
3816 | |
3817 s = new VolatileStatement(loc, s); | |
3818 a->data[i] = s; | |
3819 } | |
3820 } | |
3821 | |
3822 return a; | |
3823 } | |
3824 | |
336 | 3825 int VolatileStatement::blockExit() |
3826 { | |
3827 return statement ? statement->blockExit() : BEfallthru; | |
3828 } | |
3829 | |
159 | 3830 void VolatileStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
3831 { | |
3832 buf->writestring("volatile"); | |
3833 if (statement) | |
3834 { if (statement->isScopeStatement()) | |
3835 buf->writenl(); | |
3836 else | |
3837 buf->writebyte(' '); | |
3838 statement->toCBuffer(buf, hgs); | |
3839 } | |
3840 } | |
3841 | |
3842 | |
3843 /******************************** GotoStatement ***************************/ | |
3844 | |
3845 GotoStatement::GotoStatement(Loc loc, Identifier *ident) | |
3846 : Statement(loc) | |
3847 { | |
3848 this->ident = ident; | |
3849 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
|
3850 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
|
3851 this->enclosingScopeExit = NULL; |
159 | 3852 } |
3853 | |
3854 Statement *GotoStatement::syntaxCopy() | |
3855 { | |
3856 GotoStatement *s = new GotoStatement(loc, ident); | |
3857 return s; | |
3858 } | |
3859 | |
3860 Statement *GotoStatement::semantic(Scope *sc) | |
3861 { FuncDeclaration *fd = sc->parent->isFuncDeclaration(); | |
3862 | |
3863 //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
|
3864 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
|
3865 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
|
3866 |
159 | 3867 label = fd->searchLabel(ident); |
3868 if (!label->statement && sc->fes) | |
3869 { | |
3870 /* Either the goto label is forward referenced or it | |
3871 * is in the function that the enclosing foreach is in. | |
3872 * Can't know yet, so wrap the goto in a compound statement | |
3873 * so we can patch it later, and add it to a 'look at this later' | |
3874 * list. | |
3875 */ | |
3876 Statements *a = new Statements(); | |
3877 Statement *s; | |
3878 | |
3879 a->push(this); | |
3880 s = new CompoundStatement(loc, a); | |
3881 sc->fes->gotos.push(s); // 'look at this later' list | |
3882 return s; | |
3883 } | |
1141
f99a3b393c03
Reorganize EnclosingHandlers to require less changes to the frontend and allow us to
Christian Kamm <kamm incasoftware de>
parents:
1133
diff
changeset
|
3884 if (label->statement && label->statement->enclosingFinally != sc->enclosingFinally) |
159 | 3885 error("cannot goto in or out of finally block"); |
3886 return this; | |
3887 } | |
3888 | |
336 | 3889 int GotoStatement::blockExit() |
3890 { | |
3891 //printf("GotoStatement::blockExit(%p)\n", this); | |
3892 return BEgoto; | |
3893 } | |
3894 | |
159 | 3895 void GotoStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
3896 { | |
3897 buf->writestring("goto "); | |
3898 buf->writestring(ident->toChars()); | |
3899 buf->writebyte(';'); | |
3900 buf->writenl(); | |
3901 } | |
3902 | |
3903 /******************************** LabelStatement ***************************/ | |
3904 | |
3905 LabelStatement::LabelStatement(Loc loc, Identifier *ident, Statement *statement) | |
3906 : Statement(loc) | |
3907 { | |
3908 this->ident = ident; | |
3909 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
|
3910 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
|
3911 this->enclosingScopeExit = NULL; |
159 | 3912 this->lblock = NULL; |
3913 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
|
3914 this->asmLabel = false; |
159 | 3915 } |
3916 | |
3917 Statement *LabelStatement::syntaxCopy() | |
3918 { | |
3919 LabelStatement *s = new LabelStatement(loc, ident, statement->syntaxCopy()); | |
3920 return s; | |
3921 } | |
3922 | |
3923 Statement *LabelStatement::semantic(Scope *sc) | |
3924 { LabelDsymbol *ls; | |
3925 FuncDeclaration *fd = sc->parent->isFuncDeclaration(); | |
3926 | |
3927 //printf("LabelStatement::semantic()\n"); | |
3928 ls = fd->searchLabel(ident); | |
3929 if (ls->statement) | |
3930 error("Label '%s' already defined", ls->toChars()); | |
3931 else | |
3932 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
|
3933 |
f99a3b393c03
Reorganize EnclosingHandlers to require less changes to the frontend and allow us to
Christian Kamm <kamm incasoftware de>
parents:
1133
diff
changeset
|
3934 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
|
3935 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
|
3936 |
159 | 3937 sc = sc->push(); |
3938 sc->scopesym = sc->enclosing->scopesym; | |
3939 sc->callSuper |= CSXlabel; | |
3940 sc->slabel = this; | |
3941 if (statement) | |
3942 statement = statement->semantic(sc); | |
3943 sc->pop(); | |
305
2b72433d5c8c
[svn r326] Fixed a bunch of issues with printf's that MinGW32 did not support.
lindquist
parents:
304
diff
changeset
|
3944 |
664
eef8ac26c66c
Some missed LLVMDC -> LDC.
Christian Kamm <kamm incasoftware de>
parents:
510
diff
changeset
|
3945 // 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
|
3946 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
|
3947 |
159 | 3948 return this; |
3949 } | |
3950 | |
3951 Statements *LabelStatement::flatten(Scope *sc) | |
3952 { | |
3953 Statements *a = NULL; | |
3954 | |
3955 if (statement) | |
3956 { | |
3957 a = statement->flatten(sc); | |
3958 if (a) | |
3959 { | |
3960 if (!a->dim) | |
3961 { | |
3962 a->push(new ExpStatement(loc, NULL)); | |
3963 } | |
3964 Statement *s = (Statement *)a->data[0]; | |
3965 | |
3966 s = new LabelStatement(loc, ident, s); | |
3967 a->data[0] = s; | |
3968 } | |
3969 } | |
3970 | |
3971 return a; | |
3972 } | |
3973 | |
3974 | |
3975 int LabelStatement::usesEH() | |
3976 { | |
3977 return statement ? statement->usesEH() : FALSE; | |
3978 } | |
3979 | |
336 | 3980 int LabelStatement::blockExit() |
3981 { | |
3982 //printf("LabelStatement::blockExit(%p)\n", this); | |
3983 return statement ? statement->blockExit() : BEfallthru; | |
3984 } | |
3985 | |
159 | 3986 int LabelStatement::comeFrom() |
3987 { | |
3988 //printf("LabelStatement::comeFrom()\n"); | |
3989 return TRUE; | |
3990 } | |
3991 | |
3992 void LabelStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
3993 { | |
3994 buf->writestring(ident->toChars()); | |
3995 buf->writebyte(':'); | |
3996 buf->writenl(); | |
3997 if (statement) | |
3998 statement->toCBuffer(buf, hgs); | |
3999 } | |
4000 | |
4001 | |
4002 /******************************** LabelDsymbol ***************************/ | |
4003 | |
4004 LabelDsymbol::LabelDsymbol(Identifier *ident) | |
4005 : Dsymbol(ident) | |
4006 { | |
4007 statement = NULL; | |
4008 } | |
4009 | |
4010 LabelDsymbol *LabelDsymbol::isLabel() // is this a LabelDsymbol()? | |
4011 { | |
4012 return this; | |
4013 } |