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