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