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