comparison dmd/statement.h @ 1:c53b6e3fe49a trunk

[svn r5] Initial commit. Most things are very rough.
author lindquist
date Sat, 01 Sep 2007 21:43:27 +0200
parents
children 77cdca8c210f
comparison
equal deleted inserted replaced
0:a9e71648e74d 1:c53b6e3fe49a
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 #ifndef DMD_STATEMENT_H
12 #define DMD_STATEMENT_H
13
14 #ifdef __DMC__
15 #pragma once
16 #endif /* __DMC__ */
17
18 #include "root.h"
19
20 #include "arraytypes.h"
21 #include "dsymbol.h"
22 #include "lexer.h"
23
24 struct OutBuffer;
25 struct Scope;
26 struct Expression;
27 struct LabelDsymbol;
28 struct Identifier;
29 struct IfStatement;
30 struct DeclarationStatement;
31 struct DefaultStatement;
32 struct VarDeclaration;
33 struct Condition;
34 struct Module;
35 struct Token;
36 struct InlineCostState;
37 struct InlineDoState;
38 struct InlineScanState;
39 struct ReturnStatement;
40 struct CompoundStatement;
41 struct Argument;
42 struct StaticAssert;
43 struct AsmStatement;
44 struct GotoStatement;
45 struct ScopeStatement;
46 struct TryCatchStatement;
47 struct HdrGenState;
48 struct InterState;
49
50 enum TOK;
51
52 namespace llvm
53 {
54 class Value;
55 }
56
57 // Back end
58 struct IRState;
59 struct Blockx;
60 #if IN_GCC
61 union tree_node; typedef union tree_node block;
62 union tree_node; typedef union tree_node elem;
63 #else
64 struct block;
65 struct elem;
66 #endif
67 struct code;
68
69 struct Statement : Object
70 {
71 Loc loc;
72
73 Statement(Loc loc);
74 virtual Statement *syntaxCopy();
75
76 void print();
77 char *toChars();
78
79 void error(const char *format, ...);
80 virtual void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
81 virtual TryCatchStatement *isTryCatchStatement() { return NULL; }
82 virtual GotoStatement *isGotoStatement() { return NULL; }
83 virtual AsmStatement *isAsmStatement() { return NULL; }
84 #ifdef _DH
85 int incontract;
86 #endif
87 virtual ScopeStatement *isScopeStatement() { return NULL; }
88 virtual Statement *semantic(Scope *sc);
89 Statement *semanticScope(Scope *sc, Statement *sbreak, Statement *scontinue);
90 virtual int hasBreak();
91 virtual int hasContinue();
92 virtual int usesEH();
93 virtual int fallOffEnd();
94 virtual int comeFrom();
95 virtual void scopeCode(Statement **sentry, Statement **sexit, Statement **sfinally);
96 virtual Statements *flatten(Scope *sc);
97 virtual Expression *interpret(InterState *istate);
98
99 virtual int inlineCost(InlineCostState *ics);
100 virtual Expression *doInline(InlineDoState *ids);
101 virtual Statement *inlineScan(InlineScanState *iss);
102
103 // Back end
104 virtual void toIR(IRState *irs);
105
106 // Avoid dynamic_cast
107 virtual DeclarationStatement *isDeclarationStatement() { return NULL; }
108 virtual CompoundStatement *isCompoundStatement() { return NULL; }
109 virtual ReturnStatement *isReturnStatement() { return NULL; }
110 virtual IfStatement *isIfStatement() { return NULL; }
111 };
112
113 struct ExpStatement : Statement
114 {
115 Expression *exp;
116
117 ExpStatement(Loc loc, Expression *exp);
118 Statement *syntaxCopy();
119 void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
120 Statement *semantic(Scope *sc);
121 Expression *interpret(InterState *istate);
122 int fallOffEnd();
123
124 int inlineCost(InlineCostState *ics);
125 Expression *doInline(InlineDoState *ids);
126 Statement *inlineScan(InlineScanState *iss);
127
128 void toIR(IRState *irs);
129 };
130
131 struct CompileStatement : Statement
132 {
133 Expression *exp;
134
135 CompileStatement(Loc loc, Expression *exp);
136 Statement *syntaxCopy();
137 void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
138 Statement *semantic(Scope *sc);
139 };
140
141 struct DeclarationStatement : ExpStatement
142 {
143 // Doing declarations as an expression, rather than a statement,
144 // makes inlining functions much easier.
145
146 DeclarationStatement(Loc loc, Dsymbol *s);
147 DeclarationStatement(Loc loc, Expression *exp);
148 Statement *syntaxCopy();
149 void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
150 void scopeCode(Statement **sentry, Statement **sexit, Statement **sfinally);
151
152 DeclarationStatement *isDeclarationStatement() { return this; }
153 };
154
155 struct CompoundStatement : Statement
156 {
157 Statements *statements;
158
159 CompoundStatement(Loc loc, Statements *s);
160 CompoundStatement(Loc loc, Statement *s1, Statement *s2);
161 Statement *syntaxCopy();
162 void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
163 Statement *semantic(Scope *sc);
164 int usesEH();
165 int fallOffEnd();
166 int comeFrom();
167 Statements *flatten(Scope *sc);
168 ReturnStatement *isReturnStatement();
169 Expression *interpret(InterState *istate);
170
171 int inlineCost(InlineCostState *ics);
172 Expression *doInline(InlineDoState *ids);
173 Statement *inlineScan(InlineScanState *iss);
174
175 void toIR(IRState *irs);
176
177 CompoundStatement *isCompoundStatement() { return this; }
178 };
179
180 /* The purpose of this is so that continue will go to the next
181 * of the statements, and break will go to the end of the statements.
182 */
183 struct UnrolledLoopStatement : Statement
184 {
185 Statements *statements;
186
187 UnrolledLoopStatement(Loc loc, Statements *statements);
188 Statement *syntaxCopy();
189 Statement *semantic(Scope *sc);
190 int hasBreak();
191 int hasContinue();
192 int usesEH();
193 int fallOffEnd();
194 int comeFrom();
195 Expression *interpret(InterState *istate);
196 void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
197
198 int inlineCost(InlineCostState *ics);
199 Expression *doInline(InlineDoState *ids);
200 Statement *inlineScan(InlineScanState *iss);
201
202 void toIR(IRState *irs);
203 };
204
205 struct ScopeStatement : Statement
206 {
207 Statement *statement;
208
209 ScopeStatement(Loc loc, Statement *s);
210 Statement *syntaxCopy();
211 void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
212 ScopeStatement *isScopeStatement() { return this; }
213 Statement *semantic(Scope *sc);
214 int hasBreak();
215 int hasContinue();
216 int usesEH();
217 int fallOffEnd();
218 int comeFrom();
219 Expression *interpret(InterState *istate);
220
221 Statement *inlineScan(InlineScanState *iss);
222
223 void toIR(IRState *irs);
224 };
225
226 struct WhileStatement : Statement
227 {
228 Expression *condition;
229 Statement *body;
230
231 WhileStatement(Loc loc, Expression *c, Statement *b);
232 Statement *syntaxCopy();
233 Statement *semantic(Scope *sc);
234 int hasBreak();
235 int hasContinue();
236 int usesEH();
237 int fallOffEnd();
238 int comeFrom();
239 Expression *interpret(InterState *istate);
240 void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
241
242 Statement *inlineScan(InlineScanState *iss);
243
244 void toIR(IRState *irs);
245 };
246
247 struct DoStatement : Statement
248 {
249 Statement *body;
250 Expression *condition;
251
252 DoStatement(Loc loc, Statement *b, Expression *c);
253 Statement *syntaxCopy();
254 Statement *semantic(Scope *sc);
255 int hasBreak();
256 int hasContinue();
257 int usesEH();
258 int fallOffEnd();
259 int comeFrom();
260 Expression *interpret(InterState *istate);
261 void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
262
263 Statement *inlineScan(InlineScanState *iss);
264
265 void toIR(IRState *irs);
266 };
267
268 struct ForStatement : Statement
269 {
270 Statement *init;
271 Expression *condition;
272 Expression *increment;
273 Statement *body;
274
275 ForStatement(Loc loc, Statement *init, Expression *condition, Expression *increment, Statement *body);
276 Statement *syntaxCopy();
277 Statement *semantic(Scope *sc);
278 void scopeCode(Statement **sentry, Statement **sexit, Statement **sfinally);
279 int hasBreak();
280 int hasContinue();
281 int usesEH();
282 int fallOffEnd();
283 int comeFrom();
284 Expression *interpret(InterState *istate);
285 void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
286
287 Statement *inlineScan(InlineScanState *iss);
288
289 void toIR(IRState *irs);
290 };
291
292 struct ForeachStatement : Statement
293 {
294 enum TOK op; // TOKforeach or TOKforeach_reverse
295 Arguments *arguments; // array of Argument*'s
296 Expression *aggr;
297 Statement *body;
298
299 VarDeclaration *key;
300 VarDeclaration *value;
301
302 FuncDeclaration *func; // function we're lexically in
303
304 Array cases; // put breaks, continues, gotos and returns here
305 Array gotos; // forward referenced goto's go here
306
307 ForeachStatement(Loc loc, enum TOK op, Arguments *arguments, Expression *aggr, Statement *body);
308 Statement *syntaxCopy();
309 Statement *semantic(Scope *sc);
310 int hasBreak();
311 int hasContinue();
312 int usesEH();
313 int fallOffEnd();
314 int comeFrom();
315 Expression *interpret(InterState *istate);
316 void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
317
318 Statement *inlineScan(InlineScanState *iss);
319
320 void toIR(IRState *irs);
321 };
322
323 struct IfStatement : Statement
324 {
325 Argument *arg;
326 Expression *condition;
327 Statement *ifbody;
328 Statement *elsebody;
329
330 VarDeclaration *match; // for MatchExpression results
331
332 IfStatement(Loc loc, Argument *arg, Expression *condition, Statement *ifbody, Statement *elsebody);
333 Statement *syntaxCopy();
334 Statement *semantic(Scope *sc);
335 Expression *interpret(InterState *istate);
336 void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
337 int usesEH();
338 int fallOffEnd();
339 IfStatement *isIfStatement() { return this; }
340
341 int inlineCost(InlineCostState *ics);
342 Expression *doInline(InlineDoState *ids);
343 Statement *inlineScan(InlineScanState *iss);
344
345 void toIR(IRState *irs);
346 };
347
348 struct ConditionalStatement : Statement
349 {
350 Condition *condition;
351 Statement *ifbody;
352 Statement *elsebody;
353
354 ConditionalStatement(Loc loc, Condition *condition, Statement *ifbody, Statement *elsebody);
355 Statement *syntaxCopy();
356 Statement *semantic(Scope *sc);
357 Statements *flatten(Scope *sc);
358 int usesEH();
359
360 void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
361 };
362
363 struct PragmaStatement : Statement
364 {
365 Identifier *ident;
366 Expressions *args; // array of Expression's
367 Statement *body;
368
369 PragmaStatement(Loc loc, Identifier *ident, Expressions *args, Statement *body);
370 Statement *syntaxCopy();
371 Statement *semantic(Scope *sc);
372 int usesEH();
373 int fallOffEnd();
374
375 void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
376 };
377
378 struct StaticAssertStatement : Statement
379 {
380 StaticAssert *sa;
381
382 StaticAssertStatement(StaticAssert *sa);
383 Statement *syntaxCopy();
384 Statement *semantic(Scope *sc);
385
386 void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
387 };
388
389 struct SwitchStatement : Statement
390 {
391 Expression *condition;
392 Statement *body;
393 DefaultStatement *sdefault;
394
395 Array gotoCases; // array of unresolved GotoCaseStatement's
396 Array *cases; // array of CaseStatement's
397 int hasNoDefault; // !=0 if no default statement
398
399 SwitchStatement(Loc loc, Expression *c, Statement *b);
400 Statement *syntaxCopy();
401 Statement *semantic(Scope *sc);
402 int hasBreak();
403 int usesEH();
404 int fallOffEnd();
405 Expression *interpret(InterState *istate);
406 void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
407
408 Statement *inlineScan(InlineScanState *iss);
409
410 void toIR(IRState *irs);
411 };
412
413 struct CaseStatement : Statement
414 {
415 Expression *exp;
416 Statement *statement;
417 int index; // which case it is (since we sort this)
418 block *cblock; // back end: label for the block
419
420 CaseStatement(Loc loc, Expression *exp, Statement *s);
421 Statement *syntaxCopy();
422 Statement *semantic(Scope *sc);
423 int compare(Object *obj);
424 int usesEH();
425 int fallOffEnd();
426 int comeFrom();
427 Expression *interpret(InterState *istate);
428 void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
429
430 Statement *inlineScan(InlineScanState *iss);
431
432 void toIR(IRState *irs);
433 };
434
435 struct DefaultStatement : Statement
436 {
437 Statement *statement;
438 #if IN_GCC
439 block *cblock; // back end: label for the block
440 #endif
441
442 DefaultStatement(Loc loc, Statement *s);
443 Statement *syntaxCopy();
444 Statement *semantic(Scope *sc);
445 int usesEH();
446 int fallOffEnd();
447 int comeFrom();
448 Expression *interpret(InterState *istate);
449 void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
450
451 Statement *inlineScan(InlineScanState *iss);
452
453 void toIR(IRState *irs);
454 };
455
456 struct GotoDefaultStatement : Statement
457 {
458 SwitchStatement *sw;
459
460 GotoDefaultStatement(Loc loc);
461 Statement *syntaxCopy();
462 Statement *semantic(Scope *sc);
463 Expression *interpret(InterState *istate);
464 int fallOffEnd();
465 void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
466
467 void toIR(IRState *irs);
468 };
469
470 struct GotoCaseStatement : Statement
471 {
472 Expression *exp; // NULL, or which case to goto
473 CaseStatement *cs; // case statement it resolves to
474
475 GotoCaseStatement(Loc loc, Expression *exp);
476 Statement *syntaxCopy();
477 Statement *semantic(Scope *sc);
478 Expression *interpret(InterState *istate);
479 int fallOffEnd();
480 void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
481
482 void toIR(IRState *irs);
483 };
484
485 struct SwitchErrorStatement : Statement
486 {
487 SwitchErrorStatement(Loc loc);
488 int fallOffEnd();
489 void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
490
491 void toIR(IRState *irs);
492 };
493
494 struct ReturnStatement : Statement
495 {
496 Expression *exp;
497
498 ReturnStatement(Loc loc, Expression *exp);
499 Statement *syntaxCopy();
500 void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
501 Statement *semantic(Scope *sc);
502 int fallOffEnd();
503 Expression *interpret(InterState *istate);
504
505 int inlineCost(InlineCostState *ics);
506 Expression *doInline(InlineDoState *ids);
507 Statement *inlineScan(InlineScanState *iss);
508
509 void toIR(IRState *irs);
510
511 ReturnStatement *isReturnStatement() { return this; }
512 };
513
514 struct BreakStatement : Statement
515 {
516 Identifier *ident;
517
518 BreakStatement(Loc loc, Identifier *ident);
519 Statement *syntaxCopy();
520 Statement *semantic(Scope *sc);
521 Expression *interpret(InterState *istate);
522 int fallOffEnd();
523 void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
524
525 void toIR(IRState *irs);
526 };
527
528 struct ContinueStatement : Statement
529 {
530 Identifier *ident;
531
532 ContinueStatement(Loc loc, Identifier *ident);
533 Statement *syntaxCopy();
534 Statement *semantic(Scope *sc);
535 Expression *interpret(InterState *istate);
536 int fallOffEnd();
537 void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
538
539 void toIR(IRState *irs);
540 };
541
542 struct SynchronizedStatement : Statement
543 {
544 Expression *exp;
545 Statement *body;
546
547 SynchronizedStatement(Loc loc, Expression *exp, Statement *body);
548 Statement *syntaxCopy();
549 Statement *semantic(Scope *sc);
550 int hasBreak();
551 int hasContinue();
552 int usesEH();
553 int fallOffEnd();
554 void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
555
556 Statement *inlineScan(InlineScanState *iss);
557
558 // Back end
559 elem *esync;
560 SynchronizedStatement(Loc loc, elem *esync, Statement *body);
561 void toIR(IRState *irs);
562 };
563
564 struct WithStatement : Statement
565 {
566 Expression *exp;
567 Statement *body;
568 VarDeclaration *wthis;
569
570 WithStatement(Loc loc, Expression *exp, Statement *body);
571 Statement *syntaxCopy();
572 Statement *semantic(Scope *sc);
573 void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
574 int usesEH();
575 int fallOffEnd();
576
577 Statement *inlineScan(InlineScanState *iss);
578
579 void toIR(IRState *irs);
580 };
581
582 struct TryCatchStatement : Statement
583 {
584 Statement *body;
585 Array *catches;
586
587 TryCatchStatement(Loc loc, Statement *body, Array *catches);
588 Statement *syntaxCopy();
589 Statement *semantic(Scope *sc);
590 int hasBreak();
591 int usesEH();
592 int fallOffEnd();
593
594 Statement *inlineScan(InlineScanState *iss);
595
596 void toIR(IRState *irs);
597 void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
598 TryCatchStatement *isTryCatchStatement() { return this; }
599 };
600
601 struct Catch : Object
602 {
603 Loc loc;
604 Type *type;
605 Identifier *ident;
606 VarDeclaration *var;
607 Statement *handler;
608
609 Catch(Loc loc, Type *t, Identifier *id, Statement *handler);
610 Catch *syntaxCopy();
611 void semantic(Scope *sc);
612 void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
613 };
614
615 struct TryFinallyStatement : Statement
616 {
617 Statement *body;
618 Statement *finalbody;
619
620 TryFinallyStatement(Loc loc, Statement *body, Statement *finalbody);
621 Statement *syntaxCopy();
622 void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
623 Statement *semantic(Scope *sc);
624 int hasBreak();
625 int hasContinue();
626 int usesEH();
627 int fallOffEnd();
628
629 Statement *inlineScan(InlineScanState *iss);
630
631 void toIR(IRState *irs);
632 };
633
634 struct OnScopeStatement : Statement
635 {
636 TOK tok;
637 Statement *statement;
638
639 OnScopeStatement(Loc loc, TOK tok, Statement *statement);
640 Statement *syntaxCopy();
641 void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
642 Statement *semantic(Scope *sc);
643 int usesEH();
644 void scopeCode(Statement **sentry, Statement **sexit, Statement **sfinally);
645
646 void toIR(IRState *irs);
647 };
648
649 struct ThrowStatement : Statement
650 {
651 Expression *exp;
652
653 ThrowStatement(Loc loc, Expression *exp);
654 Statement *syntaxCopy();
655 Statement *semantic(Scope *sc);
656 void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
657 int fallOffEnd();
658
659 Statement *inlineScan(InlineScanState *iss);
660
661 void toIR(IRState *irs);
662 };
663
664 struct VolatileStatement : Statement
665 {
666 Statement *statement;
667
668 VolatileStatement(Loc loc, Statement *statement);
669 Statement *syntaxCopy();
670 Statement *semantic(Scope *sc);
671 Statements *flatten(Scope *sc);
672 int fallOffEnd();
673 void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
674
675 Statement *inlineScan(InlineScanState *iss);
676
677 void toIR(IRState *irs);
678 };
679
680 struct GotoStatement : Statement
681 {
682 Identifier *ident;
683 LabelDsymbol *label;
684 TryFinallyStatement *tf;
685
686 GotoStatement(Loc loc, Identifier *ident);
687 Statement *syntaxCopy();
688 Statement *semantic(Scope *sc);
689 int fallOffEnd();
690 Expression *interpret(InterState *istate);
691
692 void toIR(IRState *irs);
693 void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
694 GotoStatement *isGotoStatement() { return this; }
695 };
696
697 struct LabelStatement : Statement
698 {
699 Identifier *ident;
700 Statement *statement;
701 TryFinallyStatement *tf;
702 block *lblock; // back end
703 int isReturnLabel;
704
705 LabelStatement(Loc loc, Identifier *ident, Statement *statement);
706 Statement *syntaxCopy();
707 Statement *semantic(Scope *sc);
708 Statements *flatten(Scope *sc);
709 int usesEH();
710 int fallOffEnd();
711 int comeFrom();
712 Expression *interpret(InterState *istate);
713 void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
714
715 Statement *inlineScan(InlineScanState *iss);
716
717 void toIR(IRState *irs);
718 };
719
720 struct LabelDsymbol : Dsymbol
721 {
722 LabelStatement *statement;
723 #if IN_GCC
724 unsigned asmLabelNum; // GCC-specific
725 #endif
726
727 LabelDsymbol(Identifier *ident);
728 LabelDsymbol *isLabel();
729 };
730
731 struct AsmStatement : Statement
732 {
733 Token *tokens;
734 code *asmcode;
735 unsigned asmalign; // alignment of this statement
736 unsigned refparam; // !=0 if function parameter is referenced
737 unsigned naked; // !=0 if function is to be naked
738 unsigned regs; // mask of registers modified
739
740 AsmStatement(Loc loc, Token *tokens);
741 Statement *syntaxCopy();
742 Statement *semantic(Scope *sc);
743 int comeFrom();
744
745 void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
746 virtual AsmStatement *isAsmStatement() { return this; }
747
748 void toIR(IRState *irs);
749 };
750
751 #endif /* DMD_STATEMENT_H */