annotate dmd/BreakStatement.d @ 137:09c858522d55

merge
author Trass3r
date Mon, 13 Sep 2010 23:29:00 +0200
parents e28b18c23469
children af724d3510d7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1 module dmd.BreakStatement;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
2
114
e28b18c23469 added a module dmd.common for commonly used stuff
Trass3r
parents: 72
diff changeset
3 import dmd.common;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
4 import dmd.Statement;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
5 import dmd.Loc;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
6 import dmd.Identifier;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
7 import dmd.Scope;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
8 import dmd.Expression;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
9 import dmd.InterState;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
10 import dmd.OutBuffer;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
11 import dmd.HdrGenState;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
12 import dmd.IRState;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
13 import dmd.BE;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
14 import dmd.FuncDeclaration;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
15 import dmd.LabelStatement;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
16 import dmd.ReturnStatement;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
17 import dmd.IntegerExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
18
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
19 import dmd.backend.Util;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
20 import dmd.backend.block;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
21 import dmd.backend.Blockx;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
22 import dmd.backend.BC;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
23
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
24 class BreakStatement : Statement
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
25 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
26 Identifier ident;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
27
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
28 this(Loc loc, Identifier ident)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
29 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
30 super(loc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
31 this.ident = ident;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
32 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
33
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 63
diff changeset
34 override Statement syntaxCopy()
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
35 {
53
a8b50ff7f201 ForeachStatement.syntaxCopy
korDen
parents: 0
diff changeset
36 BreakStatement s = new BreakStatement(loc, ident);
a8b50ff7f201 ForeachStatement.syntaxCopy
korDen
parents: 0
diff changeset
37 return s;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
38 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
39
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 63
diff changeset
40 override Statement semantic(Scope sc)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
41 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
42 //printf("BreakStatement::semantic()\n");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
43 // If:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
44 // break Identifier;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
45 if (ident)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
46 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
47 Scope scx;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
48 FuncDeclaration thisfunc = sc.func;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
49
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
50 for (scx = sc; scx; scx = scx.enclosing)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
51 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
52 LabelStatement ls;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
53
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
54 if (scx.func != thisfunc) // if in enclosing function
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
55 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
56 if (sc.fes) // if this is the body of a foreach
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
57 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
58 /* Post this statement to the fes, and replace
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
59 * it with a return value that caller will put into
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
60 * a switch. Caller will figure out where the break
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
61 * label actually is.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
62 * Case numbers start with 2, not 0, as 0 is continue
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
63 * and 1 is break.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
64 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
65 Statement s;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
66 sc.fes.cases.push(cast(void*)this);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
67 s = new ReturnStatement(Loc(0), new IntegerExp(sc.fes.cases.dim + 1));
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
68 return s;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
69 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
70 break; // can't break to it
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
71 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
72
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
73 ls = scx.slabel;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
74 if (ls && ls.ident == ident)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
75 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
76 Statement s = ls.statement;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
77
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
78 if (!s.hasBreak())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
79 error("label '%s' has no break", ident.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
80 if (ls.tf != sc.tf)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
81 error("cannot break out of finally block");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
82 return this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
83 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
84 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
85 error("enclosing label '%s' for break not found", ident.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
86 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
87 else if (!sc.sbreak)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
88 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
89 if (sc.fes)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
90 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
91 Statement s;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
92
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
93 // Replace break; with return 1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
94 s = new ReturnStatement(Loc(0), new IntegerExp(1));
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
95 return s;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
96 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
97 error("break is not inside a loop or switch");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
98 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
99 return this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
100 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
101
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 63
diff changeset
102 override Expression interpret(InterState istate)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
103 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
104 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
105 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
106
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 63
diff changeset
107 override BE blockExit()
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
108 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
109 //printf("BreakStatement::blockExit(%p) = x%x\n", this, ident ? BEgoto : BEbreak);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
110 return ident ? BE.BEgoto : BE.BEbreak;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
111 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
112
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 63
diff changeset
113 override void toCBuffer(OutBuffer buf, HdrGenState* hgs)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
114 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
115 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
116 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
117
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 63
diff changeset
118 override void toIR(IRState* irs)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
119 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
120 block* bbreak;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
121 block* b;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
122 Blockx* blx = irs.blx;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
123
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
124 bbreak = irs.getBreakBlock(ident);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
125 assert(bbreak);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
126 b = blx.curblock;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
127 incUsage(irs, loc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
128
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
129 // Adjust exception handler scope index if in different try blocks
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
130 if (b.Btry != bbreak.Btry)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
131 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
132 //setScopeIndex(blx, b, bbreak.Btry ? bbreak.Btry.Bscope_index : -1);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
133 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
134
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
135 /* Nothing more than a 'goto' to the current break destination
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
136 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
137 list_append(&b.Bsucc, bbreak);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
138 block_next(blx, BCgoto, null);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
139 }
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 63
diff changeset
140 }