Mercurial > projects > ddmd
annotate dmd/ForeachRangeStatement.d @ 130:60bb0fe4563e
dmdfe 2.037 first main iteration
author | Eldar Insafutdinov <e.insafutdinov@gmail.com> |
---|---|
date | Thu, 09 Sep 2010 22:51:44 +0100 |
parents | c77e9f4f1793 |
children | e3afd1303184 |
rev | line source |
---|---|
0 | 1 module dmd.ForeachRangeStatement; |
2 | |
114 | 3 import dmd.common; |
0 | 4 import dmd.Statement; |
5 import dmd.TOK; | |
93
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
72
diff
changeset
|
6 import dmd.Token; |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
122
diff
changeset
|
7 import dmd.Parameter; |
0 | 8 import dmd.Expression; |
9 import dmd.Statement; | |
10 import dmd.VarDeclaration; | |
11 import dmd.Scope; | |
12 import dmd.ExpInitializer; | |
13 import dmd.Identifier; | |
14 import dmd.Lexer; | |
15 import dmd.ArrayTypes; | |
16 import dmd.DeclarationStatement; | |
17 import dmd.CompoundDeclarationStatement; | |
18 import dmd.DeclarationExp; | |
19 import dmd.PostExp; | |
20 import dmd.VarExp; | |
21 import dmd.ForStatement; | |
22 import dmd.IntegerExp; | |
23 import dmd.AddAssignExp; | |
24 import dmd.CmpExp; | |
25 import dmd.IRState; | |
26 import dmd.InterState; | |
27 import dmd.OutBuffer; | |
28 import dmd.AddExp; | |
29 import dmd.WANT; | |
30 import dmd.ScopeDsymbol; | |
31 import dmd.HdrGenState; | |
32 import dmd.InlineScanState; | |
33 import dmd.Loc; | |
34 import dmd.BE; | |
35 | |
93
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
72
diff
changeset
|
36 version(DMDV2) |
0 | 37 class ForeachRangeStatement : Statement |
38 { | |
39 TOK op; // TOK.TOKforeach or TOK.TOKforeach_reverse | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
122
diff
changeset
|
40 Parameter arg; // loop index variable |
0 | 41 Expression lwr; |
42 Expression upr; | |
43 Statement body_; | |
44 | |
45 VarDeclaration key = null; | |
46 | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
122
diff
changeset
|
47 this(Loc loc, TOK op, Parameter arg, Expression lwr, Expression upr, Statement body_) |
0 | 48 { |
49 super(loc); | |
50 this.op = op; | |
51 this.arg = arg; | |
52 this.lwr = lwr; | |
53 this.upr = upr; | |
54 this.body_ = body_; | |
55 } | |
56 | |
72 | 57 override Statement syntaxCopy() |
0 | 58 { |
59 ForeachRangeStatement s = new ForeachRangeStatement(loc, op, | |
60 arg.syntaxCopy(), | |
61 lwr.syntaxCopy(), | |
62 upr.syntaxCopy(), | |
63 body_ ? body_.syntaxCopy() : null); | |
64 | |
65 return s; | |
66 } | |
67 | |
72 | 68 override Statement semantic(Scope sc) |
0 | 69 { |
70 //printf("ForeachRangeStatement.semantic() %p\n", this); | |
71 ScopeDsymbol sym; | |
72 Statement s = this; | |
73 | |
74 lwr = lwr.semantic(sc); | |
75 lwr = resolveProperties(sc, lwr); | |
76 lwr = lwr.optimize(WANTvalue); | |
77 if (!lwr.type) | |
78 { | |
79 error("invalid range lower bound %s", lwr.toChars()); | |
80 return this; | |
81 } | |
82 | |
83 upr = upr.semantic(sc); | |
84 upr = resolveProperties(sc, upr); | |
85 upr = upr.optimize(WANTvalue); | |
86 if (!upr.type) | |
87 { | |
88 error("invalid range upper bound %s", upr.toChars()); | |
89 return this; | |
90 } | |
91 | |
92 if (arg.type) | |
93 { | |
94 arg.type = arg.type.semantic(loc, sc); | |
95 lwr = lwr.implicitCastTo(sc, arg.type); | |
96 upr = upr.implicitCastTo(sc, arg.type); | |
97 } | |
98 else | |
99 { | |
100 /* Must infer types from lwr and upr | |
101 */ | |
102 scope AddExp ea = new AddExp(loc, lwr, upr); | |
103 ea.typeCombine(sc); | |
104 arg.type = ea.type.mutableOf(); | |
105 lwr = ea.e1; | |
106 upr = ea.e2; | |
107 } | |
108 static if (true) { | |
109 /* Convert to a for loop: | |
110 * foreach (key; lwr .. upr) => | |
111 * for (auto key = lwr, auto tmp = upr; key < tmp; ++key) | |
112 * | |
113 * foreach_reverse (key; lwr .. upr) => | |
114 * for (auto tmp = lwr, auto key = upr; key-- > tmp;) | |
115 */ | |
116 | |
117 ExpInitializer ie = new ExpInitializer(loc, (op == TOKforeach) ? lwr : upr); | |
118 key = new VarDeclaration(loc, arg.type, arg.ident, ie); | |
119 | |
120 Identifier id = Lexer.uniqueId("__limit"); | |
121 ie = new ExpInitializer(loc, (op == TOKforeach) ? upr : lwr); | |
122 VarDeclaration tmp = new VarDeclaration(loc, arg.type, id, ie); | |
123 | |
122
c77e9f4f1793
Statements -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
124 auto cs = new Statements(); |
0 | 125 // Keep order of evaluation as lwr, then upr |
126 if (op == TOKforeach) | |
127 { | |
122
c77e9f4f1793
Statements -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
128 cs.push(new DeclarationStatement(loc, new DeclarationExp(loc, key))); |
c77e9f4f1793
Statements -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
129 cs.push(new DeclarationStatement(loc, new DeclarationExp(loc, tmp))); |
0 | 130 } |
131 else | |
132 { | |
122
c77e9f4f1793
Statements -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
133 cs.push(new DeclarationStatement(loc, new DeclarationExp(loc, tmp))); |
c77e9f4f1793
Statements -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
134 cs.push(new DeclarationStatement(loc, new DeclarationExp(loc, key))); |
0 | 135 } |
136 Statement forinit = new CompoundDeclarationStatement(loc, cs); | |
137 | |
138 Expression cond; | |
139 if (op == TOKforeach_reverse) | |
140 { // key-- > tmp | |
141 cond = new PostExp(TOKminusminus, loc, new VarExp(loc, key)); | |
142 cond = new CmpExp(TOKgt, loc, cond, new VarExp(loc, tmp)); | |
143 } | |
144 else | |
145 // key < tmp | |
146 cond = new CmpExp(TOKlt, loc, new VarExp(loc, key), new VarExp(loc, tmp)); | |
147 | |
148 Expression increment = null; | |
149 if (op == TOKforeach) | |
150 // key += 1 | |
151 increment = new AddAssignExp(loc, new VarExp(loc, key), new IntegerExp(1)); | |
152 | |
153 ForStatement fs = new ForStatement(loc, forinit, cond, increment, body_); | |
154 s = fs.semantic(sc); | |
155 return s; | |
156 } else { | |
157 if (!arg.type.isscalar()) | |
158 error("%s is not a scalar type", arg.type.toChars()); | |
159 | |
160 sym = new ScopeDsymbol(); | |
161 sym.parent = sc.scopesym; | |
162 sc = sc.push(sym); | |
163 | |
164 sc.noctor++; | |
165 | |
166 key = new VarDeclaration(loc, arg.type, arg.ident, null); | |
167 DeclarationExp de = new DeclarationExp(loc, key); | |
168 de.semantic(sc); | |
169 | |
170 if (key.storage_class) | |
171 error("foreach range: key cannot have storage class"); | |
172 | |
173 sc.sbreak = this; | |
174 sc.scontinue = this; | |
175 body_ = body_.semantic(sc); | |
176 | |
177 sc.noctor--; | |
178 sc.pop(); | |
179 return s; | |
180 } | |
181 } | |
182 | |
93
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
72
diff
changeset
|
183 override bool hasBreak() |
0 | 184 { |
93
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
72
diff
changeset
|
185 return true; |
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
72
diff
changeset
|
186 } |
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
72
diff
changeset
|
187 |
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
72
diff
changeset
|
188 override bool hasContinue() |
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
72
diff
changeset
|
189 { |
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
72
diff
changeset
|
190 return true; |
0 | 191 } |
192 | |
93
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
72
diff
changeset
|
193 override bool usesEH() |
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
72
diff
changeset
|
194 { |
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
72
diff
changeset
|
195 assert(false); // from dmd |
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
72
diff
changeset
|
196 return body_.usesEH(); |
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
72
diff
changeset
|
197 } |
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
72
diff
changeset
|
198 |
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
72
diff
changeset
|
199 override BE blockExit() |
0 | 200 { |
93
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
72
diff
changeset
|
201 assert(false); // from dmd |
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
72
diff
changeset
|
202 BE result = BE.BEfallthru; |
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
72
diff
changeset
|
203 |
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
72
diff
changeset
|
204 if (lwr && lwr.canThrow()) |
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
72
diff
changeset
|
205 result |= BE.BEthrow; |
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
72
diff
changeset
|
206 else if (upr && upr.canThrow()) |
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
72
diff
changeset
|
207 result |= BE.BEthrow; |
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
72
diff
changeset
|
208 |
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
72
diff
changeset
|
209 if (body_) |
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
72
diff
changeset
|
210 { |
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
72
diff
changeset
|
211 result |= body_.blockExit() & ~(BE.BEbreak | BE.BEcontinue); |
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
72
diff
changeset
|
212 } |
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
72
diff
changeset
|
213 return result; |
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
72
diff
changeset
|
214 } |
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
72
diff
changeset
|
215 |
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
72
diff
changeset
|
216 override bool comeFrom() |
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
72
diff
changeset
|
217 { |
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
72
diff
changeset
|
218 assert(false); // from dmd |
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
72
diff
changeset
|
219 if (body_) |
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
72
diff
changeset
|
220 return body_.comeFrom(); |
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
72
diff
changeset
|
221 return false; |
0 | 222 } |
223 | |
93
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
72
diff
changeset
|
224 override Expression interpret(InterState istate) |
0 | 225 { |
226 assert(false); | |
227 } | |
228 | |
93
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
72
diff
changeset
|
229 override void toCBuffer(OutBuffer buf, HdrGenState* hgs) |
0 | 230 { |
93
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
72
diff
changeset
|
231 buf.writestring(Token.toChars(op)); |
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
72
diff
changeset
|
232 buf.writestring(" ("); |
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
72
diff
changeset
|
233 |
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
72
diff
changeset
|
234 if (arg.type) |
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
72
diff
changeset
|
235 arg.type.toCBuffer(buf, arg.ident, hgs); |
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
72
diff
changeset
|
236 else |
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
72
diff
changeset
|
237 buf.writestring(arg.ident.toChars()); |
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
72
diff
changeset
|
238 |
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
72
diff
changeset
|
239 buf.writestring("; "); |
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
72
diff
changeset
|
240 lwr.toCBuffer(buf, hgs); |
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
72
diff
changeset
|
241 buf.writestring(" .. "); |
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
72
diff
changeset
|
242 upr.toCBuffer(buf, hgs); |
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
72
diff
changeset
|
243 buf.writebyte(')'); |
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
72
diff
changeset
|
244 buf.writenl(); |
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
72
diff
changeset
|
245 buf.writebyte('{'); |
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
72
diff
changeset
|
246 buf.writenl(); |
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
72
diff
changeset
|
247 if (body_) |
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
72
diff
changeset
|
248 body_.toCBuffer(buf, hgs); |
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
72
diff
changeset
|
249 buf.writebyte('}'); |
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
72
diff
changeset
|
250 buf.writenl(); |
0 | 251 } |
252 | |
93
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
72
diff
changeset
|
253 override Statement inlineScan(InlineScanState* iss) |
0 | 254 { |
93
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
72
diff
changeset
|
255 lwr = lwr.inlineScan(iss); |
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
72
diff
changeset
|
256 upr = upr.inlineScan(iss); |
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
72
diff
changeset
|
257 if (body_) |
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
72
diff
changeset
|
258 body_ = body_.inlineScan(iss); |
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
72
diff
changeset
|
259 return this; |
0 | 260 } |
261 | |
72 | 262 override void toIR(IRState* irs) |
0 | 263 { |
264 assert(false); | |
265 } | |
72 | 266 } |