0
|
1 module dmd.DoStatement;
|
|
2
|
114
|
3 import dmd.common;
|
0
|
4 import dmd.Statement;
|
|
5 import dmd.Expression;
|
|
6 import dmd.Loc;
|
|
7 import dmd.Scope;
|
|
8 import dmd.InterState;
|
155
|
9 import dmd.GlobalExpressions;
|
0
|
10 import dmd.HdrGenState;
|
|
11 import dmd.OutBuffer;
|
|
12 import dmd.InlineScanState;
|
|
13 import dmd.IRState;
|
|
14 import dmd.BE;
|
|
15 import dmd.WANT;
|
|
16
|
|
17 import dmd.backend.block;
|
|
18 import dmd.backend.Blockx;
|
|
19 import dmd.backend.Util;
|
|
20 import dmd.backend.BC;
|
|
21
|
|
22 class DoStatement : Statement
|
|
23 {
|
|
24 Statement body_;
|
|
25 Expression condition;
|
|
26
|
|
27 this(Loc loc, Statement b, Expression c)
|
|
28 {
|
178
|
29 register();
|
0
|
30 super(loc);
|
|
31 body_ = b;
|
|
32 condition = c;
|
|
33 }
|
|
34
|
72
|
35 override Statement syntaxCopy()
|
0
|
36 {
|
|
37 DoStatement s = new DoStatement(loc, body_ ? body_.syntaxCopy() : null, condition.syntaxCopy());
|
|
38 return s;
|
|
39 }
|
|
40
|
72
|
41 override Statement semantic(Scope sc)
|
0
|
42 {
|
|
43 sc.noctor++;
|
|
44 if (body_)
|
|
45 body_ = body_.semanticScope(sc, this, this);
|
|
46 sc.noctor--;
|
|
47 condition = condition.semantic(sc);
|
|
48 condition = resolveProperties(sc, condition);
|
|
49 condition = condition.optimize(WANTvalue);
|
|
50
|
|
51 condition = condition.checkToBoolean();
|
|
52
|
|
53 return this;
|
|
54 }
|
|
55
|
72
|
56 override bool hasBreak()
|
0
|
57 {
|
|
58 return true;
|
|
59 }
|
|
60
|
72
|
61 override bool hasContinue()
|
0
|
62 {
|
|
63 return true;
|
|
64 }
|
|
65
|
72
|
66 override bool usesEH()
|
0
|
67 {
|
|
68 return body_ ? body_.usesEH() : false;
|
|
69 }
|
|
70
|
72
|
71 override BE blockExit()
|
0
|
72 {
|
|
73 BE result;
|
|
74
|
|
75 if (body_)
|
|
76 {
|
|
77 result = body_.blockExit();
|
|
78 if (result == BE.BEbreak)
|
|
79 return BE.BEfallthru;
|
|
80 if (result & BE.BEcontinue)
|
|
81 result |= BE.BEfallthru;
|
|
82 }
|
|
83 else
|
|
84 result = BE.BEfallthru;
|
|
85
|
|
86 if (result & BE.BEfallthru)
|
|
87 {
|
|
88 if (condition.canThrow())
|
|
89 result |= BE.BEthrow;
|
|
90 if (!(result & BE.BEbreak) && condition.isBool(true))
|
|
91 result &= ~BE.BEfallthru;
|
|
92 }
|
|
93 result &= ~(BE.BEbreak | BE.BEcontinue);
|
|
94
|
|
95 return result;
|
|
96 }
|
|
97
|
72
|
98 override bool comeFrom()
|
0
|
99 {
|
|
100 assert(false);
|
|
101 }
|
|
102
|
72
|
103 override Expression interpret(InterState istate)
|
0
|
104 {
|
155
|
105 version(LOG)
|
|
106 writef("DoStatement::interpret()\n");
|
|
107
|
|
108 if (istate.start == this)
|
|
109 istate.start = null;
|
|
110 Expression e;
|
|
111
|
|
112 if (istate.start)
|
|
113 {
|
|
114 e = body_ ? body_.interpret(istate) : null;
|
|
115 if (istate.start)
|
|
116 return null;
|
|
117 if (e is EXP_CANT_INTERPRET)
|
|
118 return e;
|
|
119 if (e is EXP_BREAK_INTERPRET)
|
|
120 return null;
|
|
121 if (e is EXP_CONTINUE_INTERPRET)
|
|
122 goto Lcontinue;
|
|
123 if (e)
|
|
124 return e;
|
|
125 }
|
|
126
|
|
127 while (1)
|
|
128 {
|
|
129 e = body_ ? body_.interpret(istate) : null;
|
|
130 if (e is EXP_CANT_INTERPRET)
|
|
131 break;
|
|
132 if (e is EXP_BREAK_INTERPRET)
|
|
133 {
|
|
134 e = null;
|
|
135 break;
|
|
136 }
|
|
137 if (e && e !is EXP_CONTINUE_INTERPRET)
|
|
138 break;
|
|
139
|
|
140 Lcontinue:
|
|
141 e = condition.interpret(istate);
|
|
142 if (e is EXP_CANT_INTERPRET)
|
|
143 break;
|
|
144 if (!e.isConst())
|
|
145 {
|
|
146 e = EXP_CANT_INTERPRET;
|
|
147 break;
|
|
148 }
|
|
149 if (e.isBool(true))
|
|
150 {
|
|
151 }
|
|
152 else if (e.isBool(false))
|
|
153 {
|
|
154 e = null;
|
|
155 break;
|
|
156 }
|
|
157 else
|
|
158 assert(0);
|
|
159 }
|
|
160 return e;
|
0
|
161 }
|
|
162
|
72
|
163 override void toCBuffer(OutBuffer buf, HdrGenState* hgs)
|
0
|
164 {
|
174
|
165 buf.writestring("do");
|
|
166 buf.writenl();
|
|
167 if (body_)
|
|
168 body_.toCBuffer(buf, hgs);
|
|
169 buf.writestring("while (");
|
|
170 condition.toCBuffer(buf, hgs);
|
|
171 buf.writebyte(')');
|
0
|
172 }
|
|
173
|
72
|
174 override Statement inlineScan(InlineScanState* iss)
|
0
|
175 {
|
|
176 body_ = body_ ? body_.inlineScan(iss) : null;
|
|
177 condition = condition.inlineScan(iss);
|
|
178 return this;
|
|
179 }
|
|
180
|
72
|
181 override void toIR(IRState* irs)
|
0
|
182 {
|
|
183 Blockx *blx = irs.blx;
|
|
184
|
|
185 IRState mystate = IRState(irs,this);
|
|
186 mystate.breakBlock = block_calloc(blx);
|
|
187 mystate.contBlock = block_calloc(blx);
|
|
188
|
|
189 block* bpre = blx.curblock;
|
|
190 block_next(blx, BCgoto, null);
|
|
191 list_append(&bpre.Bsucc, blx.curblock);
|
|
192
|
|
193 list_append(&mystate.contBlock.Bsucc, blx.curblock);
|
|
194 list_append(&mystate.contBlock.Bsucc, mystate.breakBlock);
|
|
195
|
|
196 if (body_)
|
|
197 body_.toIR(&mystate);
|
|
198 list_append(&blx.curblock.Bsucc, mystate.contBlock);
|
|
199
|
|
200 block_next(blx, BCgoto, mystate.contBlock);
|
|
201 incUsage(irs, condition.loc);
|
|
202 block_appendexp(mystate.contBlock, condition.toElem(&mystate));
|
|
203 block_next(blx, BCiftrue, mystate.breakBlock);
|
|
204 }
|
72
|
205 }
|