2 // Compiler implementation of the D programming language
3 // Copyright (c) 1999-2008 by Digital Mars
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.
11 #ifndef DMD_STATEMENT_H
12 #define DMD_STATEMENT_H
20 #include "arraytypes.h"
29 struct DeclarationStatement
;
30 struct DefaultStatement
;
31 struct VarDeclaration
;
35 struct InlineCostState
;
37 struct InlineScanState
;
38 struct ReturnStatement
;
39 struct CompoundStatement
;
44 struct ScopeStatement
;
45 struct TryCatchStatement
;
46 struct TryFinallyStatement
;
56 union tree_node
; typedef union tree_node block
;
57 union tree_node
; typedef union tree_node elem
;
64 /* How a statement exits
76 BEany
= (BEfallthru
| BEthrow
| BEreturn
| BEgoto
| BEhalt
),
79 struct Statement
: Object
84 virtual Statement
*syntaxCopy();
89 void error(const char *format
, ...);
90 virtual void toCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
);
91 virtual TryCatchStatement
*isTryCatchStatement() { return NULL
; }
92 virtual GotoStatement
*isGotoStatement() { return NULL
; }
93 virtual AsmStatement
*isAsmStatement() { return NULL
; }
97 virtual ScopeStatement
*isScopeStatement() { return NULL
; }
98 virtual Statement
*semantic(Scope
*sc
);
99 Statement
*semanticScope(Scope
*sc
, Statement
*sbreak
, Statement
*scontinue
);
100 virtual int hasBreak();
101 virtual int hasContinue();
102 virtual int usesEH();
103 virtual int fallOffEnd();
104 virtual int blockExit();
105 virtual int comeFrom();
106 virtual void scopeCode(Scope
*sc
, Statement
**sentry
, Statement
**sexit
, Statement
**sfinally
);
107 virtual Statements
*flatten(Scope
*sc
);
108 virtual Expression
*interpret(InterState
*istate
);
110 virtual int inlineCost(InlineCostState
*ics
);
111 virtual Expression
*doInline(InlineDoState
*ids
);
112 virtual Statement
*inlineScan(InlineScanState
*iss
);
115 virtual void toIR(IRState
*irs
);
117 // Avoid dynamic_cast
118 virtual DeclarationStatement
*isDeclarationStatement() { return NULL
; }
119 virtual CompoundStatement
*isCompoundStatement() { return NULL
; }
120 virtual ReturnStatement
*isReturnStatement() { return NULL
; }
121 virtual IfStatement
*isIfStatement() { return NULL
; }
124 struct ExpStatement
: Statement
128 ExpStatement(Loc loc
, Expression
*exp
);
129 Statement
*syntaxCopy();
130 void toCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
);
131 Statement
*semantic(Scope
*sc
);
132 Expression
*interpret(InterState
*istate
);
136 int inlineCost(InlineCostState
*ics
);
137 Expression
*doInline(InlineDoState
*ids
);
138 Statement
*inlineScan(InlineScanState
*iss
);
140 void toIR(IRState
*irs
);
143 struct CompileStatement
: Statement
147 CompileStatement(Loc loc
, Expression
*exp
);
148 Statement
*syntaxCopy();
149 void toCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
);
150 Statement
*semantic(Scope
*sc
);
153 struct DeclarationStatement
: ExpStatement
155 // Doing declarations as an expression, rather than a statement,
156 // makes inlining functions much easier.
158 DeclarationStatement(Loc loc
, Dsymbol
*s
);
159 DeclarationStatement(Loc loc
, Expression
*exp
);
160 Statement
*syntaxCopy();
161 void toCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
);
162 void scopeCode(Scope
*sc
, Statement
**sentry
, Statement
**sexit
, Statement
**sfinally
);
164 DeclarationStatement
*isDeclarationStatement() { return this; }
167 struct CompoundStatement
: Statement
169 Statements
*statements
;
171 CompoundStatement(Loc loc
, Statements
*s
);
172 CompoundStatement(Loc loc
, Statement
*s1
, Statement
*s2
);
173 Statement
*syntaxCopy();
174 void toCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
);
175 Statement
*semantic(Scope
*sc
);
180 Statements
*flatten(Scope
*sc
);
181 ReturnStatement
*isReturnStatement();
182 Expression
*interpret(InterState
*istate
);
184 int inlineCost(InlineCostState
*ics
);
185 Expression
*doInline(InlineDoState
*ids
);
186 Statement
*inlineScan(InlineScanState
*iss
);
188 void toIR(IRState
*irs
);
190 CompoundStatement
*isCompoundStatement() { return this; }
193 /* The purpose of this is so that continue will go to the next
194 * of the statements, and break will go to the end of the statements.
196 struct UnrolledLoopStatement
: Statement
198 Statements
*statements
;
200 UnrolledLoopStatement(Loc loc
, Statements
*statements
);
201 Statement
*syntaxCopy();
202 Statement
*semantic(Scope
*sc
);
209 Expression
*interpret(InterState
*istate
);
210 void toCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
);
212 int inlineCost(InlineCostState
*ics
);
213 Expression
*doInline(InlineDoState
*ids
);
214 Statement
*inlineScan(InlineScanState
*iss
);
216 void toIR(IRState
*irs
);
219 struct ScopeStatement
: Statement
221 Statement
*statement
;
223 ScopeStatement(Loc loc
, Statement
*s
);
224 Statement
*syntaxCopy();
225 void toCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
);
226 ScopeStatement
*isScopeStatement() { return this; }
227 Statement
*semantic(Scope
*sc
);
234 Expression
*interpret(InterState
*istate
);
236 Statement
*inlineScan(InlineScanState
*iss
);
238 void toIR(IRState
*irs
);
241 struct WhileStatement
: Statement
243 Expression
*condition
;
246 WhileStatement(Loc loc
, Expression
*c
, Statement
*b
);
247 Statement
*syntaxCopy();
248 Statement
*semantic(Scope
*sc
);
255 Expression
*interpret(InterState
*istate
);
256 void toCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
);
258 Statement
*inlineScan(InlineScanState
*iss
);
260 void toIR(IRState
*irs
);
263 struct DoStatement
: Statement
266 Expression
*condition
;
268 DoStatement(Loc loc
, Statement
*b
, Expression
*c
);
269 Statement
*syntaxCopy();
270 Statement
*semantic(Scope
*sc
);
277 Expression
*interpret(InterState
*istate
);
278 void toCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
);
280 Statement
*inlineScan(InlineScanState
*iss
);
282 void toIR(IRState
*irs
);
285 struct ForStatement
: Statement
288 Expression
*condition
;
289 Expression
*increment
;
292 ForStatement(Loc loc
, Statement
*init
, Expression
*condition
, Expression
*increment
, Statement
*body
);
293 Statement
*syntaxCopy();
294 Statement
*semantic(Scope
*sc
);
295 void scopeCode(Scope
*sc
, Statement
**sentry
, Statement
**sexit
, Statement
**sfinally
);
302 Expression
*interpret(InterState
*istate
);
303 void toCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
);
305 Statement
*inlineScan(InlineScanState
*iss
);
307 void toIR(IRState
*irs
);
310 struct ForeachStatement
: Statement
312 enum TOK op
; // TOKforeach or TOKforeach_reverse
313 Arguments
*arguments
; // array of Argument*'s
318 VarDeclaration
*value
;
320 FuncDeclaration
*func
; // function we're lexically in
322 Array cases
; // put breaks, continues, gotos and returns here
323 Array gotos
; // forward referenced goto's go here
325 ForeachStatement(Loc loc
, enum TOK op
, Arguments
*arguments
, Expression
*aggr
, Statement
*body
);
326 Statement
*syntaxCopy();
327 Statement
*semantic(Scope
*sc
);
334 Expression
*interpret(InterState
*istate
);
335 void toCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
);
337 Statement
*inlineScan(InlineScanState
*iss
);
339 void toIR(IRState
*irs
);
342 struct ForeachRangeStatement
: Statement
344 enum TOK op
; // TOKforeach or TOKforeach_reverse
345 Argument
*arg
; // loop index variable
352 ForeachRangeStatement(Loc loc
, enum TOK op
, Argument
*arg
,
353 Expression
*lwr
, Expression
*upr
, Statement
*body
);
354 Statement
*syntaxCopy();
355 Statement
*semantic(Scope
*sc
);
362 Expression
*interpret(InterState
*istate
);
363 void toCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
);
365 Statement
*inlineScan(InlineScanState
*iss
);
367 void toIR(IRState
*irs
);
370 struct IfStatement
: Statement
373 Expression
*condition
;
377 VarDeclaration
*match
; // for MatchExpression results
379 IfStatement(Loc loc
, Argument
*arg
, Expression
*condition
, Statement
*ifbody
, Statement
*elsebody
);
380 Statement
*syntaxCopy();
381 Statement
*semantic(Scope
*sc
);
382 Expression
*interpret(InterState
*istate
);
383 void toCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
);
387 IfStatement
*isIfStatement() { return this; }
389 int inlineCost(InlineCostState
*ics
);
390 Expression
*doInline(InlineDoState
*ids
);
391 Statement
*inlineScan(InlineScanState
*iss
);
393 void toIR(IRState
*irs
);
396 struct ConditionalStatement
: Statement
398 Condition
*condition
;
402 ConditionalStatement(Loc loc
, Condition
*condition
, Statement
*ifbody
, Statement
*elsebody
);
403 Statement
*syntaxCopy();
404 Statement
*semantic(Scope
*sc
);
405 Statements
*flatten(Scope
*sc
);
408 void toCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
);
411 struct PragmaStatement
: Statement
414 Expressions
*args
; // array of Expression's
417 PragmaStatement(Loc loc
, Identifier
*ident
, Expressions
*args
, Statement
*body
);
418 Statement
*syntaxCopy();
419 Statement
*semantic(Scope
*sc
);
424 void toCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
);
426 void toIR(IRState
*irs
);
429 struct StaticAssertStatement
: Statement
433 StaticAssertStatement(StaticAssert
*sa
);
434 Statement
*syntaxCopy();
435 Statement
*semantic(Scope
*sc
);
437 void toCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
);
440 struct SwitchStatement
: Statement
442 Expression
*condition
;
445 DefaultStatement
*sdefault
;
446 TryFinallyStatement
*tf
;
447 Array gotoCases
; // array of unresolved GotoCaseStatement's
448 Array
*cases
; // array of CaseStatement's
449 int hasNoDefault
; // !=0 if no default statement
450 int hasVars
; // !=0 if has variable case values
452 SwitchStatement(Loc loc
, Expression
*c
, Statement
*b
);
453 Statement
*syntaxCopy();
454 Statement
*semantic(Scope
*sc
);
459 Expression
*interpret(InterState
*istate
);
460 void toCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
);
462 Statement
*inlineScan(InlineScanState
*iss
);
464 void toIR(IRState
*irs
);
467 struct CaseStatement
: Statement
470 Statement
*statement
;
471 int index
; // which case it is (since we sort this)
472 block
*cblock
; // back end: label for the block
474 CaseStatement(Loc loc
, Expression
*exp
, Statement
*s
);
475 Statement
*syntaxCopy();
476 Statement
*semantic(Scope
*sc
);
477 int compare(Object
*obj
);
482 Expression
*interpret(InterState
*istate
);
483 void toCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
);
485 Statement
*inlineScan(InlineScanState
*iss
);
487 void toIR(IRState
*irs
);
490 struct DefaultStatement
: Statement
492 Statement
*statement
;
494 block
*cblock
; // back end: label for the block
497 DefaultStatement(Loc loc
, Statement
*s
);
498 Statement
*syntaxCopy();
499 Statement
*semantic(Scope
*sc
);
504 Expression
*interpret(InterState
*istate
);
505 void toCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
);
507 Statement
*inlineScan(InlineScanState
*iss
);
509 void toIR(IRState
*irs
);
512 struct GotoDefaultStatement
: Statement
516 GotoDefaultStatement(Loc loc
);
517 Statement
*syntaxCopy();
518 Statement
*semantic(Scope
*sc
);
519 Expression
*interpret(InterState
*istate
);
522 void toCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
);
524 void toIR(IRState
*irs
);
527 struct GotoCaseStatement
: Statement
529 Expression
*exp
; // NULL, or which case to goto
530 CaseStatement
*cs
; // case statement it resolves to
532 GotoCaseStatement(Loc loc
, Expression
*exp
);
533 Statement
*syntaxCopy();
534 Statement
*semantic(Scope
*sc
);
535 Expression
*interpret(InterState
*istate
);
538 void toCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
);
540 void toIR(IRState
*irs
);
543 struct SwitchErrorStatement
: Statement
545 SwitchErrorStatement(Loc loc
);
548 void toCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
);
550 void toIR(IRState
*irs
);
553 struct ReturnStatement
: Statement
557 ReturnStatement(Loc loc
, Expression
*exp
);
558 Statement
*syntaxCopy();
559 void toCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
);
560 Statement
*semantic(Scope
*sc
);
563 Expression
*interpret(InterState
*istate
);
565 int inlineCost(InlineCostState
*ics
);
566 Expression
*doInline(InlineDoState
*ids
);
567 Statement
*inlineScan(InlineScanState
*iss
);
569 void toIR(IRState
*irs
);
571 ReturnStatement
*isReturnStatement() { return this; }
574 struct BreakStatement
: Statement
578 BreakStatement(Loc loc
, Identifier
*ident
);
579 Statement
*syntaxCopy();
580 Statement
*semantic(Scope
*sc
);
581 Expression
*interpret(InterState
*istate
);
584 void toCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
);
586 void toIR(IRState
*irs
);
589 struct ContinueStatement
: Statement
593 ContinueStatement(Loc loc
, Identifier
*ident
);
594 Statement
*syntaxCopy();
595 Statement
*semantic(Scope
*sc
);
596 Expression
*interpret(InterState
*istate
);
599 void toCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
);
601 void toIR(IRState
*irs
);
604 struct SynchronizedStatement
: Statement
609 SynchronizedStatement(Loc loc
, Expression
*exp
, Statement
*body
);
610 Statement
*syntaxCopy();
611 Statement
*semantic(Scope
*sc
);
617 void toCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
);
619 Statement
*inlineScan(InlineScanState
*iss
);
623 SynchronizedStatement(Loc loc
, elem
*esync
, Statement
*body
);
624 void toIR(IRState
*irs
);
627 struct WithStatement
: Statement
631 VarDeclaration
*wthis
;
633 WithStatement(Loc loc
, Expression
*exp
, Statement
*body
);
634 Statement
*syntaxCopy();
635 Statement
*semantic(Scope
*sc
);
636 void toCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
);
641 Statement
*inlineScan(InlineScanState
*iss
);
643 void toIR(IRState
*irs
);
646 struct TryCatchStatement
: Statement
651 TryCatchStatement(Loc loc
, Statement
*body
, Array
*catches
);
652 Statement
*syntaxCopy();
653 Statement
*semantic(Scope
*sc
);
659 Statement
*inlineScan(InlineScanState
*iss
);
661 void toIR(IRState
*irs
);
662 void toCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
);
663 TryCatchStatement
*isTryCatchStatement() { return this; }
666 struct Catch
: Object
674 Catch(Loc loc
, Type
*t
, Identifier
*id
, Statement
*handler
);
676 void semantic(Scope
*sc
);
678 void toCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
);
681 struct TryFinallyStatement
: Statement
684 Statement
*finalbody
;
686 TryFinallyStatement(Loc loc
, Statement
*body
, Statement
*finalbody
);
687 Statement
*syntaxCopy();
688 void toCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
);
689 Statement
*semantic(Scope
*sc
);
696 Statement
*inlineScan(InlineScanState
*iss
);
698 void toIR(IRState
*irs
);
701 struct OnScopeStatement
: Statement
704 Statement
*statement
;
706 OnScopeStatement(Loc loc
, TOK tok
, Statement
*statement
);
707 Statement
*syntaxCopy();
709 void toCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
);
710 Statement
*semantic(Scope
*sc
);
712 void scopeCode(Scope
*sc
, Statement
**sentry
, Statement
**sexit
, Statement
**sfinally
);
714 void toIR(IRState
*irs
);
717 struct ThrowStatement
: Statement
721 ThrowStatement(Loc loc
, Expression
*exp
);
722 Statement
*syntaxCopy();
723 Statement
*semantic(Scope
*sc
);
724 void toCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
);
728 Statement
*inlineScan(InlineScanState
*iss
);
730 void toIR(IRState
*irs
);
733 struct VolatileStatement
: Statement
735 Statement
*statement
;
737 VolatileStatement(Loc loc
, Statement
*statement
);
738 Statement
*syntaxCopy();
739 Statement
*semantic(Scope
*sc
);
740 Statements
*flatten(Scope
*sc
);
743 void toCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
);
745 Statement
*inlineScan(InlineScanState
*iss
);
747 void toIR(IRState
*irs
);
750 struct GotoStatement
: Statement
754 TryFinallyStatement
*tf
;
756 GotoStatement(Loc loc
, Identifier
*ident
);
757 Statement
*syntaxCopy();
758 Statement
*semantic(Scope
*sc
);
761 Expression
*interpret(InterState
*istate
);
763 void toIR(IRState
*irs
);
764 void toCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
);
765 GotoStatement
*isGotoStatement() { return this; }
768 struct LabelStatement
: Statement
771 Statement
*statement
;
772 TryFinallyStatement
*tf
;
773 block
*lblock
; // back end
776 LabelStatement(Loc loc
, Identifier
*ident
, Statement
*statement
);
777 Statement
*syntaxCopy();
778 Statement
*semantic(Scope
*sc
);
779 Statements
*flatten(Scope
*sc
);
784 Expression
*interpret(InterState
*istate
);
785 void toCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
);
787 Statement
*inlineScan(InlineScanState
*iss
);
789 void toIR(IRState
*irs
);
792 struct LabelDsymbol
: Dsymbol
794 LabelStatement
*statement
;
796 unsigned asmLabelNum
; // GCC-specific
799 LabelDsymbol(Identifier
*ident
);
800 LabelDsymbol
*isLabel();
803 struct AsmStatement
: Statement
807 unsigned asmalign
; // alignment of this statement
808 unsigned refparam
; // !=0 if function parameter is referenced
809 unsigned naked
; // !=0 if function is to be naked
810 unsigned regs
; // mask of registers modified
812 AsmStatement(Loc loc
, Token
*tokens
);
813 Statement
*syntaxCopy();
814 Statement
*semantic(Scope
*sc
);
818 void toCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
);
819 virtual AsmStatement
*isAsmStatement() { return this; }
821 void toIR(IRState
*irs
);
826 // Assembler instructions with D expression operands
827 struct ExtAsmStatement
: Statement
829 Expression
*insnTemplate
;
831 Array
*argNames
; // of NULL or Identifier*
832 Expressions
*argConstraints
; // of StringExp*
833 unsigned nOutputArgs
;
834 Expressions
*clobbers
; // of StringExp*
836 ExtAsmStatement(Loc loc
, Expression
*insnTemplate
, Expressions
*args
, Array
*argNames
,
837 Expressions
*argConstraints
, int nOutputArgs
, Expressions
*clobbers
);
838 Statement
*syntaxCopy();
839 Statement
*semantic(Scope
*sc
);
841 void toIR(IRState
*irs
);
846 #endif /* DMD_STATEMENT_H */