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