Mercurial > projects > ddmd
annotate dmd/ForeachStatement.d @ 135:af1bebfd96a4 dmd2037
dmd 2.038
author | Eldar Insafutdinov <e.insafutdinov@gmail.com> |
---|---|
date | Mon, 13 Sep 2010 22:19:42 +0100 |
parents | 60bb0fe4563e |
children | 94b6033c07f3 |
rev | line source |
---|---|
0 | 1 module dmd.ForeachStatement; |
2 | |
114 | 3 import dmd.common; |
0 | 4 import dmd.Statement; |
5 import dmd.TOK; | |
66 | 6 import dmd.Token; |
0 | 7 import dmd.Loc; |
8 import dmd.LINK; | |
9 import dmd.ArrayTypes; | |
10 import dmd.Expression; | |
11 import dmd.VarDeclaration; | |
12 import dmd.FuncDeclaration; | |
13 import dmd.Array; | |
14 import dmd.Scope; | |
15 import dmd.InterState; | |
16 import dmd.InlineScanState; | |
17 import dmd.OutBuffer; | |
18 import dmd.HdrGenState; | |
19 import dmd.IRState; | |
20 import dmd.BE; | |
21 import dmd.ScopeDsymbol; | |
22 import dmd.TypeAArray; | |
23 import dmd.Type; | |
24 import dmd.CallExp; | |
25 import dmd.WANT; | |
26 import dmd.TY; | |
27 import dmd.TypeTuple; | |
28 import dmd.TupleExp; | |
29 import dmd.Global; | |
30 import dmd.Initializer; | |
31 import dmd.ExpInitializer; | |
32 import dmd.IntegerExp; | |
33 import dmd.ExpStatement; | |
34 import dmd.DeclarationExp; | |
35 import dmd.Dsymbol; | |
36 import dmd.BreakStatement; | |
37 import dmd.DefaultStatement; | |
38 import dmd.CaseStatement; | |
39 import dmd.SwitchStatement; | |
40 import dmd.VarExp; | |
41 import dmd.AliasDeclaration; | |
42 import dmd.CompoundStatement; | |
43 import dmd.ScopeStatement; | |
44 import dmd.UnrolledLoopStatement; | |
45 import dmd.Identifier; | |
46 import dmd.Lexer; | |
47 import dmd.DeclarationStatement; | |
48 import dmd.CompoundDeclarationStatement; | |
49 import dmd.AggregateDeclaration; | |
50 import dmd.TypeClass; | |
51 import dmd.NotExp; | |
52 import dmd.TypeStruct; | |
53 import dmd.FuncLiteralDeclaration; | |
54 import dmd.IdentifierExp; | |
55 import dmd.TypeFunction; | |
56 import dmd.GotoStatement; | |
57 import dmd.FuncExp; | |
58 import dmd.ReturnStatement; | |
59 import dmd.IndexExp; | |
60 import dmd.ForStatement; | |
61 import dmd.SliceExp; | |
62 import dmd.DotIdExp; | |
63 import dmd.PostExp; | |
64 import dmd.AddAssignExp; | |
65 import dmd.CmpExp; | |
66 import dmd.Id; | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
67 import dmd.Parameter; |
0 | 68 import dmd.STC; |
69 | |
70 import dmd.expression.Util; | |
71 | |
72 import core.stdc.stdio; | |
73 | |
74 class ForeachStatement : Statement | |
75 { | |
76 TOK op; // TOKforeach or TOKforeach_reverse | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
77 Parameters arguments; // array of Argument*'s |
0 | 78 Expression aggr; |
79 Statement body_; | |
80 | |
81 VarDeclaration key; | |
82 VarDeclaration value; | |
83 | |
84 FuncDeclaration func; // function we're lexically in | |
85 | |
86 Array cases; // put breaks, continues, gotos and returns here | |
87 Array gotos; // forward referenced goto's go here | |
88 | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
89 this(Loc loc, TOK op, Parameters arguments, Expression aggr, Statement body_) |
0 | 90 { |
91 super(loc); | |
92 | |
93 this.op = op; | |
94 this.arguments = arguments; | |
95 this.aggr = aggr; | |
96 this.body_ = body_; | |
97 | |
98 gotos = new Array(); | |
99 cases = new Array(); | |
100 } | |
101 | |
72 | 102 override Statement syntaxCopy() |
0 | 103 { |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
104 auto args = Parameter.arraySyntaxCopy(arguments); |
53 | 105 Expression exp = aggr.syntaxCopy(); |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
106 auto s = new ForeachStatement(loc, op, args, exp, |
53 | 107 body_ ? body_.syntaxCopy() : null); |
108 return s; | |
0 | 109 } |
110 | |
72 | 111 override Statement semantic(Scope sc) |
0 | 112 { |
113 //printf("ForeachStatement.semantic() %p\n", this); | |
114 ScopeDsymbol sym; | |
115 Statement s = this; | |
116 size_t dim = arguments.dim; | |
117 TypeAArray taa = null; | |
135 | 118 Dsymbol sapply = null; |
0 | 119 |
120 Type tn = null; | |
121 Type tnv = null; | |
122 | |
123 func = sc.func; | |
124 if (func.fes) | |
125 func = func.fes.func; | |
126 | |
127 aggr = aggr.semantic(sc); | |
128 aggr = resolveProperties(sc, aggr); | |
129 aggr = aggr.optimize(WANT.WANTvalue); | |
130 if (!aggr.type) | |
131 { | |
132 error("invalid foreach aggregate %s", aggr.toChars()); | |
133 return this; | |
134 } | |
135 | |
136 inferApplyArgTypes(op, arguments, aggr); | |
137 | |
138 /* Check for inference errors | |
139 */ | |
140 if (dim != arguments.dim) | |
141 { | |
142 //printf("dim = %d, arguments.dim = %d\n", dim, arguments.dim); | |
143 error("cannot uniquely infer foreach argument types"); | |
144 return this; | |
145 } | |
146 | |
147 Type tab = aggr.type.toBasetype(); | |
148 | |
149 if (tab.ty == TY.Ttuple) // don't generate new scope for tuple loops | |
150 { | |
151 if (dim < 1 || dim > 2) | |
152 { | |
153 error("only one (value) or two (key,value) arguments for tuple foreach"); | |
154 return s; | |
155 } | |
156 | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
157 auto tuple = cast(TypeTuple)tab; |
0 | 158 Statements statements = new Statements(); |
159 //printf("aggr: op = %d, %s\n", aggr.op, aggr.toChars()); | |
160 size_t n; | |
161 TupleExp te = null; | |
162 if (aggr.op == TOK.TOKtuple) // expression tuple | |
163 { | |
164 te = cast(TupleExp)aggr; | |
165 n = te.exps.dim; | |
166 } | |
167 else if (aggr.op == TOK.TOKtype) // type tuple | |
168 { | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
169 n = Parameter.dim(tuple.arguments); |
0 | 170 } |
171 else | |
172 assert(0); | |
173 | |
174 for (size_t j = 0; j < n; j++) | |
175 { | |
176 size_t k = (op == TOK.TOKforeach) ? j : n - 1 - j; | |
177 Expression e; | |
178 Type t; | |
179 if (te) | |
90
39648eb578f6
more Expressions work
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
84
diff
changeset
|
180 e = te.exps[k]; |
0 | 181 else |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
182 t = Parameter.getNth(tuple.arguments, k).type; |
0 | 183 |
126
1765f3ef917d
ClassDeclarations, Arguments -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
122
diff
changeset
|
184 auto arg = arguments[0]; |
1765f3ef917d
ClassDeclarations, Arguments -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
122
diff
changeset
|
185 auto st = new Statements(); |
0 | 186 |
187 if (dim == 2) | |
188 { | |
189 // Declare key | |
190 if (arg.storageClass & (STC.STCout | STC.STCref | STC.STClazy)) | |
191 error("no storage class for key %s", arg.ident.toChars()); | |
192 TY keyty = arg.type.ty; | |
193 if (keyty != TY.Tint32 && keyty != TY.Tuns32) | |
194 { | |
195 if (global.params.isX86_64) | |
196 { | |
197 if (keyty != TY.Tint64 && keyty != TY.Tuns64) | |
198 error("foreach: key type must be int or uint, long or ulong, not %s", arg.type.toChars()); | |
199 } | |
200 else | |
201 error("foreach: key type must be int or uint, not %s", arg.type.toChars()); | |
202 } | |
203 Initializer ie = new ExpInitializer(Loc(0), new IntegerExp(k)); | |
204 VarDeclaration var = new VarDeclaration(loc, arg.type, arg.ident, ie); | |
205 var.storage_class |= STC.STCmanifest; | |
206 DeclarationExp de = new DeclarationExp(loc, var); | |
122
c77e9f4f1793
Statements -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
207 st.push(new ExpStatement(loc, de)); |
126
1765f3ef917d
ClassDeclarations, Arguments -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
122
diff
changeset
|
208 arg = arguments[1]; // value |
0 | 209 } |
210 // Declare value | |
211 if (arg.storageClass & (STC.STCout | STC.STCref | STC.STClazy)) | |
212 error("no storage class for value %s", arg.ident.toChars()); | |
213 Dsymbol var; | |
214 if (te) | |
215 { | |
216 Type tb = e.type.toBasetype(); | |
217 if ((tb.ty == TY.Tfunction || tb.ty == TY.Tsarray) && e.op == TOK.TOKvar) | |
218 { | |
126
1765f3ef917d
ClassDeclarations, Arguments -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
122
diff
changeset
|
219 auto ve = cast(VarExp)e; |
0 | 220 var = new AliasDeclaration(loc, arg.ident, ve.var); |
221 } | |
222 else | |
223 { | |
224 arg.type = e.type; | |
225 Initializer ie = new ExpInitializer(Loc(0), e); | |
226 VarDeclaration v = new VarDeclaration(loc, arg.type, arg.ident, ie); | |
227 if (e.isConst()) | |
228 v.storage_class |= STC.STCconst; | |
229 | |
230 var = v; | |
231 } | |
232 } | |
233 else | |
234 { | |
235 var = new AliasDeclaration(loc, arg.ident, t); | |
236 } | |
237 | |
122
c77e9f4f1793
Statements -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
238 auto de = new DeclarationExp(loc, var); |
c77e9f4f1793
Statements -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
239 st.push(new ExpStatement(loc, de)); |
0 | 240 |
122
c77e9f4f1793
Statements -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
241 st.push(body_.syntaxCopy()); |
0 | 242 s = new CompoundStatement(loc, st); |
243 s = new ScopeStatement(loc, s); | |
122
c77e9f4f1793
Statements -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
244 statements.push(s); |
0 | 245 } |
246 | |
247 s = new UnrolledLoopStatement(loc, statements); | |
248 s = s.semantic(sc); | |
249 return s; | |
250 } | |
251 | |
252 sym = new ScopeDsymbol(); | |
253 sym.parent = sc.scopesym; | |
254 sc = sc.push(sym); | |
255 | |
256 sc.noctor++; | |
257 | |
96 | 258 Lagain: |
135 | 259 Identifier idapply = (op == TOK.TOKforeach_reverse) |
260 ? Id.applyReverse : Id.apply; | |
261 sapply = null; | |
0 | 262 switch (tab.ty) |
263 { | |
264 case TY.Tarray: | |
265 case TY.Tsarray: | |
266 if (!checkForArgTypes()) | |
267 return this; | |
268 | |
269 if (dim < 1 || dim > 2) | |
270 { | |
271 error("only one or two arguments for array foreach"); | |
272 break; | |
273 } | |
274 | |
275 /* Look for special case of parsing char types out of char type | |
276 * array. | |
277 */ | |
278 tn = tab.nextOf().toBasetype(); | |
279 if (tn.ty == TY.Tchar || tn.ty == TY.Twchar || tn.ty == TY.Tdchar) | |
280 { | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
281 Parameter arg; |
0 | 282 |
283 int i = (dim == 1) ? 0 : 1; // index of value | |
126
1765f3ef917d
ClassDeclarations, Arguments -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
122
diff
changeset
|
284 arg = arguments[i]; |
0 | 285 arg.type = arg.type.semantic(loc, sc); |
286 tnv = arg.type.toBasetype(); | |
287 if (tnv.ty != tn.ty && (tnv.ty == TY.Tchar || tnv.ty == TY.Twchar || tnv.ty == TY.Tdchar)) | |
288 { | |
289 if (arg.storageClass & STC.STCref) | |
290 error("foreach: value of UTF conversion cannot be ref"); | |
291 if (dim == 2) | |
292 { | |
126
1765f3ef917d
ClassDeclarations, Arguments -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
122
diff
changeset
|
293 arg = arguments[0]; |
0 | 294 if (arg.storageClass & STC.STCref) |
295 error("foreach: key cannot be ref"); | |
296 } | |
297 goto Lapply; | |
298 } | |
299 } | |
300 | |
301 for (size_t i = 0; i < dim; i++) | |
302 { | |
303 // Declare args | |
126
1765f3ef917d
ClassDeclarations, Arguments -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
122
diff
changeset
|
304 auto arg = arguments[i]; |
0 | 305 Type argtype = arg.type.semantic(loc, sc); |
126
1765f3ef917d
ClassDeclarations, Arguments -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
122
diff
changeset
|
306 auto var = new VarDeclaration(loc, argtype, arg.ident, null); |
0 | 307 var.storage_class |= STC.STCforeach; |
308 var.storage_class |= arg.storageClass & (STC.STCin | STC.STCout | STC.STCref | STC.STC_TYPECTOR); | |
309 if (var.storage_class & (STC.STCref | STC.STCout)) | |
310 var.storage_class |= STC.STCnodtor; | |
311 | |
312 if (dim == 2 && i == 0) | |
313 { | |
314 key = var; | |
315 //var.storage_class |= STCfinal; | |
316 } | |
317 else | |
318 { | |
319 value = var; | |
320 /* Reference to immutable data should be marked as const | |
321 */ | |
322 if (var.storage_class & STC.STCref && !tn.isMutable()) | |
323 { | |
324 var.storage_class |= STC.STCconst; | |
325 } | |
326 } | |
96 | 327 static if (false) |
328 { | |
0 | 329 DeclarationExp de = new DeclarationExp(loc, var); |
330 de.semantic(sc); | |
331 } | |
332 } | |
333 | |
96 | 334 static if (true) |
335 { | |
0 | 336 { |
337 /* Convert to a ForStatement | |
338 * foreach (key, value; a) body => | |
339 * for (T[] tmp = a[], size_t key; key < tmp.length; ++key) | |
340 * { T value = tmp[k]; body } | |
341 * | |
342 * foreach_reverse (key, value; a) body => | |
343 * for (T[] tmp = a[], size_t key = tmp.length; key--; ) | |
344 * { T value = tmp[k]; body } | |
345 */ | |
346 Identifier id = Lexer.uniqueId("__aggr"); | |
347 ExpInitializer ie = new ExpInitializer(loc, new SliceExp(loc, aggr, null, null)); | |
348 VarDeclaration tmp = new VarDeclaration(loc, aggr.type.nextOf().arrayOf(), id, ie); | |
349 | |
350 Expression tmp_length = new DotIdExp(loc, new VarExp(loc, tmp), Id.length); | |
351 | |
352 if (!key) | |
353 { | |
354 Identifier id2 = Lexer.uniqueId("__key"); | |
355 key = new VarDeclaration(loc, Type.tsize_t, id2, null); | |
356 } | |
357 | |
358 if (op == TOK.TOKforeach_reverse) | |
359 key.init = new ExpInitializer(loc, tmp_length); | |
360 else | |
361 key.init = new ExpInitializer(loc, new IntegerExp(0)); | |
362 | |
122
c77e9f4f1793
Statements -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
363 auto cs = new Statements(); |
c77e9f4f1793
Statements -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
364 cs.push(new DeclarationStatement(loc, new DeclarationExp(loc, tmp))); |
c77e9f4f1793
Statements -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
365 cs.push(new DeclarationStatement(loc, new DeclarationExp(loc, key))); |
0 | 366 Statement forinit = new CompoundDeclarationStatement(loc, cs); |
367 | |
368 Expression cond; | |
369 if (op == TOK.TOKforeach_reverse) | |
370 // key-- | |
371 cond = new PostExp(TOK.TOKminusminus, loc, new VarExp(loc, key)); | |
372 else | |
373 // key < tmp.length | |
374 cond = new CmpExp(TOK.TOKlt, loc, new VarExp(loc, key), tmp_length); | |
375 | |
376 Expression increment = null; | |
377 if (op == TOK.TOKforeach) | |
378 // key += 1 | |
379 increment = new AddAssignExp(loc, new VarExp(loc, key), new IntegerExp(1)); | |
380 | |
381 // T value = tmp[key]; | |
382 value.init = new ExpInitializer(loc, new IndexExp(loc, new VarExp(loc, tmp), new VarExp(loc, key))); | |
383 Statement ds = new DeclarationStatement(loc, new DeclarationExp(loc, value)); | |
384 | |
385 body_ = new CompoundStatement(loc, ds, body_); | |
386 | |
387 ForStatement fs = new ForStatement(loc, forinit, cond, increment, body_); | |
388 s = fs.semantic(sc); | |
389 break; | |
390 } | |
391 } else { | |
392 if (tab.nextOf().implicitConvTo(value.type) < MATCH.MATCHconst) | |
393 { | |
394 if (aggr.op == TOK.TOKstring) | |
395 aggr = aggr.implicitCastTo(sc, value.type.arrayOf()); | |
396 else | |
397 error("foreach: %s is not an array of %s", | |
398 tab.toChars(), value.type.toChars()); | |
399 } | |
400 | |
401 if (key) | |
402 { | |
403 if (key.type.ty != Tint32 && key.type.ty != Tuns32) | |
404 { | |
405 if (global.params.isX86_64) | |
406 { | |
407 if (key.type.ty != Tint64 && key.type.ty != Tuns64) | |
408 error("foreach: key type must be int or uint, long or ulong, not %s", key.type.toChars()); | |
409 } | |
410 else | |
411 error("foreach: key type must be int or uint, not %s", key.type.toChars()); | |
412 } | |
413 | |
414 if (key.storage_class & (STCout | STCref)) | |
415 error("foreach: key cannot be out or ref"); | |
416 } | |
417 | |
418 sc.sbreak = this; | |
419 sc.scontinue = this; | |
420 body_ = body_.semantic(sc); | |
421 break; | |
422 } | |
423 | |
424 case TY.Taarray: | |
425 if (!checkForArgTypes()) | |
426 return this; | |
427 | |
428 taa = cast(TypeAArray)tab; | |
429 if (dim < 1 || dim > 2) | |
430 { | |
431 error("only one or two arguments for associative array foreach"); | |
432 break; | |
433 } | |
96 | 434 version(SARRAYVALUE) |
435 { | |
436 /* This only works if Key or Value is a static array. | |
437 */ | |
438 tab = taa.getImpl().type; | |
439 goto Lagain; | |
440 } | |
441 else | |
442 { | |
0 | 443 if (op == TOK.TOKforeach_reverse) |
444 { | |
445 error("no reverse iteration on associative arrays"); | |
446 } | |
447 goto Lapply; | |
96 | 448 } |
0 | 449 case TY.Tclass: |
450 case TY.Tstruct: | |
451 version (DMDV2) { | |
135 | 452 /* Prefer using opApply, if it exists |
453 */ | |
454 if (dim != 1) // only one argument allowed with ranges | |
455 goto Lapply; | |
456 | |
457 sapply = search_function(cast(AggregateDeclaration)tab.toDsymbol(sc), idapply); | |
458 if (sapply) | |
459 goto Lapply; | |
460 | |
0 | 461 { /* Look for range iteration, i.e. the properties |
462 * .empty, .next, .retreat, .head and .rear | |
463 * foreach (e; aggr) { ... } | |
464 * translates to: | |
465 * for (auto __r = aggr[]; !__r.empty; __r.next) | |
466 * { auto e = __r.head; | |
467 * ... | |
468 * } | |
469 */ | |
470 AggregateDeclaration ad = (tab.ty == TY.Tclass) | |
471 ? cast(AggregateDeclaration)(cast(TypeClass)tab).sym | |
472 : cast(AggregateDeclaration)(cast(TypeStruct)tab).sym; | |
473 | |
474 Identifier idhead; | |
475 Identifier idnext; | |
476 if (op == TOK.TOKforeach) | |
477 { | |
478 idhead = Id.Fhead; | |
479 idnext = Id.Fnext; | |
480 } | |
481 else | |
482 { | |
483 idhead = Id.Ftoe; | |
484 idnext = Id.Fretreat; | |
485 } | |
486 | |
487 Dsymbol shead = search_function(ad, idhead); | |
488 if (!shead) | |
489 goto Lapply; | |
490 | |
491 /* Generate a temporary __r and initialize it with the aggregate. | |
492 */ | |
493 Identifier id = Identifier.generateId("__r"); | |
494 Expression rinit = new SliceExp(loc, aggr, null, null); | |
495 rinit = rinit.trySemantic(sc); | |
496 if (!rinit) // if application of [] failed | |
497 rinit = aggr; | |
498 | |
499 VarDeclaration r = new VarDeclaration(loc, null, id, new ExpInitializer(loc, rinit)); | |
500 | |
501 // r.semantic(sc); | |
502 //printf("r: %s, init: %s\n", r.toChars(), r.init.toChars()); | |
503 Statement init = new DeclarationStatement(loc, r); | |
504 //printf("init: %s\n", init.toChars()); | |
505 | |
506 // !__r.empty | |
507 Expression e = new VarExp(loc, r); | |
508 e = new DotIdExp(loc, e, Id.Fempty); | |
509 Expression condition = new NotExp(loc, e); | |
510 | |
511 // __r.next | |
512 e = new VarExp(loc, r); | |
513 Expression increment = new DotIdExp(loc, e, idnext); | |
514 | |
515 /* Declaration statement for e: | |
516 * auto e = __r.idhead; | |
517 */ | |
518 e = new VarExp(loc, r); | |
519 Expression einit = new DotIdExp(loc, e, idhead); | |
520 // einit = einit.semantic(sc); | |
126
1765f3ef917d
ClassDeclarations, Arguments -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
122
diff
changeset
|
521 auto arg = arguments[0]; |
1765f3ef917d
ClassDeclarations, Arguments -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
122
diff
changeset
|
522 auto ve = new VarDeclaration(loc, arg.type, arg.ident, new ExpInitializer(loc, einit)); |
0 | 523 ve.storage_class |= STC.STCforeach; |
524 ve.storage_class |= arg.storageClass & (STC.STCin | STC.STCout | STC.STCref | STC.STC_TYPECTOR); | |
525 | |
126
1765f3ef917d
ClassDeclarations, Arguments -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
122
diff
changeset
|
526 auto de = new DeclarationExp(loc, ve); |
0 | 527 |
528 Statement body2 = new CompoundStatement(loc, new DeclarationStatement(loc, de), this.body_); | |
529 s = new ForStatement(loc, init, condition, increment, body2); | |
530 | |
531 static if (false) { | |
532 printf("init: %s\n", init.toChars()); | |
533 printf("condition: %s\n", condition.toChars()); | |
534 printf("increment: %s\n", increment.toChars()); | |
535 printf("body: %s\n", body2.toChars()); | |
536 } | |
537 s = s.semantic(sc); | |
538 break; | |
539 } | |
540 } | |
541 case TY.Tdelegate: | |
542 Lapply: | |
543 { | |
544 Expression ec; | |
545 Expression e; | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
546 Parameter a; |
0 | 547 |
548 if (!checkForArgTypes()) | |
549 { | |
550 body_ = body_.semantic(sc); | |
551 return this; | |
552 } | |
553 | |
135 | 554 Type tret = func.type.nextOf(); |
0 | 555 |
556 // Need a variable to hold value from any return statements in body. | |
557 if (!sc.func.vresult && tret && tret != Type.tvoid) | |
558 { | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
559 auto v = new VarDeclaration(loc, tret, Id.result, null); |
0 | 560 v.noauto = true; |
561 v.semantic(sc); | |
562 if (!sc.insert(v)) | |
563 assert(0); | |
564 | |
565 v.parent = sc.func; | |
566 sc.func.vresult = v; | |
567 } | |
568 | |
569 /* Turn body into the function literal: | |
570 * int delegate(ref T arg) { body } | |
571 */ | |
135 | 572 auto args = new Parameters(); |
0 | 573 for (size_t i = 0; i < dim; i++) |
574 { | |
126
1765f3ef917d
ClassDeclarations, Arguments -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
122
diff
changeset
|
575 auto arg = arguments[i]; |
135 | 576 Identifier id; |
0 | 577 |
578 arg.type = arg.type.semantic(loc, sc); | |
579 if (arg.storageClass & STC.STCref) | |
580 id = arg.ident; | |
581 else | |
582 { // Make a copy of the ref argument so it isn't | |
583 // a reference. | |
584 id = Lexer.uniqueId("__applyArg", i); | |
585 | |
135 | 586 Initializer ie = new ExpInitializer(Loc(0), new IdentifierExp(Loc(0), id)); |
587 auto v = new VarDeclaration(Loc(0), arg.type, arg.ident, ie); | |
0 | 588 s = new DeclarationStatement(Loc(0), v); |
589 body_ = new CompoundStatement(loc, s, body_); | |
590 } | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
591 a = new Parameter(STC.STCref, arg.type, id, null); |
126
1765f3ef917d
ClassDeclarations, Arguments -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
122
diff
changeset
|
592 args.push(a); |
0 | 593 } |
135 | 594 Type t = new TypeFunction(args, Type.tint32, 0, LINK.LINKd); |
595 FuncLiteralDeclaration fld = new FuncLiteralDeclaration(loc, Loc(0), t, TOK.TOKdelegate, this); | |
0 | 596 fld.fbody = body_; |
135 | 597 Expression flde = new FuncExp(loc, fld); |
0 | 598 flde = flde.semantic(sc); |
599 fld.tookAddressOf = 0; | |
600 | |
601 // Resolve any forward referenced goto's | |
135 | 602 for (size_t i = 0; i < gotos.dim; i++) |
0 | 603 { |
122
c77e9f4f1793
Statements -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
604 auto cs = cast(CompoundStatement)gotos.data[i]; |
c77e9f4f1793
Statements -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
605 auto gs = cast(GotoStatement)cs.statements[0]; |
0 | 606 |
607 if (!gs.label.statement) | |
608 { | |
609 // 'Promote' it to this scope, and replace with a return | |
610 cases.push(cast(void*)gs); | |
611 s = new ReturnStatement(Loc(0), new IntegerExp(cases.dim + 1)); | |
122
c77e9f4f1793
Statements -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
612 cs.statements[0] = s; |
0 | 613 } |
614 } | |
615 | |
616 if (tab.ty == TY.Taarray) | |
617 { | |
618 // Check types | |
126
1765f3ef917d
ClassDeclarations, Arguments -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
122
diff
changeset
|
619 auto arg = arguments[0]; |
0 | 620 if (dim == 2) |
621 { | |
622 if (arg.storageClass & STC.STCref) | |
623 error("foreach: index cannot be ref"); | |
624 if (!arg.type.equals(taa.index)) | |
625 error("foreach: index must be type %s, not %s", taa.index.toChars(), arg.type.toChars()); | |
626 | |
126
1765f3ef917d
ClassDeclarations, Arguments -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
122
diff
changeset
|
627 arg = arguments[1]; |
0 | 628 } |
629 if (!arg.type.equals(taa.nextOf())) | |
630 error("foreach: value must be type %s, not %s", taa.nextOf().toChars(), arg.type.toChars()); | |
631 | |
632 /* Call: | |
633 * _aaApply(aggr, keysize, flde) | |
634 */ | |
135 | 635 FuncDeclaration fdapply; |
0 | 636 if (dim == 2) |
637 fdapply = FuncDeclaration.genCfunc(Type.tindex, "_aaApply2"); | |
638 else | |
639 fdapply = FuncDeclaration.genCfunc(Type.tindex, "_aaApply"); | |
640 | |
641 ec = new VarExp(Loc(0), fdapply); | |
84
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
73
diff
changeset
|
642 auto exps = new Expressions(); |
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
73
diff
changeset
|
643 exps.push(aggr); |
0 | 644 size_t keysize = cast(uint)taa.index.size(); |
645 keysize = (keysize + (PTRSIZE-1)) & ~(PTRSIZE-1); | |
84
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
73
diff
changeset
|
646 exps.push(new IntegerExp(Loc(0), keysize, Type.tsize_t)); |
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
73
diff
changeset
|
647 exps.push(flde); |
0 | 648 e = new CallExp(loc, ec, exps); |
649 e.type = Type.tindex; // don't run semantic() on e | |
650 } | |
651 else if (tab.ty == TY.Tarray || tab.ty == TY.Tsarray) | |
652 { | |
653 /* Call: | |
654 * _aApply(aggr, flde) | |
655 */ | |
656 static char fntab[9][3] = | |
657 [ "cc","cw","cd", | |
658 "wc","cc","wd", | |
659 "dc","dw","dd" | |
660 ]; | |
661 char fdname[7+1+2+ dim.sizeof*3 + 1]; | |
662 int flag; | |
663 | |
664 switch (tn.ty) | |
665 { | |
666 case TY.Tchar: flag = 0; break; | |
667 case TY.Twchar: flag = 3; break; | |
668 case TY.Tdchar: flag = 6; break; | |
669 } | |
670 switch (tnv.ty) | |
671 { | |
672 case TY.Tchar: flag += 0; break; | |
673 case TY.Twchar: flag += 1; break; | |
674 case TY.Tdchar: flag += 2; break; | |
675 } | |
676 string r = (op == TOK.TOKforeach_reverse) ? "R" : ""; | |
73 | 677 int j = sprintf(fdname.ptr, "_aApply%.*s%.*s%zd".ptr, r, 2, fntab[flag].ptr, dim); |
0 | 678 assert(j < fdname.sizeof); |
135 | 679 FuncDeclaration fdapply = FuncDeclaration.genCfunc(Type.tindex, fdname[0..j].idup); |
0 | 680 |
681 ec = new VarExp(Loc(0), fdapply); | |
84
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
73
diff
changeset
|
682 auto exps = new Expressions(); |
0 | 683 if (tab.ty == TY.Tsarray) |
684 aggr = aggr.castTo(sc, tn.arrayOf()); | |
84
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
73
diff
changeset
|
685 exps.push(aggr); |
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
73
diff
changeset
|
686 exps.push(flde); |
0 | 687 e = new CallExp(loc, ec, exps); |
688 e.type = Type.tindex; // don't run semantic() on e | |
689 } | |
690 else if (tab.ty == TY.Tdelegate) | |
691 { | |
692 /* Call: | |
693 * aggr(flde) | |
694 */ | |
84
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
73
diff
changeset
|
695 auto exps = new Expressions(); |
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
73
diff
changeset
|
696 exps.push(flde); |
0 | 697 e = new CallExp(loc, aggr, exps); |
698 e = e.semantic(sc); | |
699 if (e.type != Type.tint32) | |
700 error("opApply() function for %s must return an int", tab.toChars()); | |
701 } | |
702 else | |
703 { | |
704 assert(tab.ty == TY.Tstruct || tab.ty == TY.Tclass); | |
135 | 705 auto exps = new Expressions(); |
706 if (!sapply) | |
707 sapply = search_function(cast(AggregateDeclaration)tab.toDsymbol(sc), idapply); | |
0 | 708 static if (false) { |
709 TemplateDeclaration td; | |
710 if (sapply && (td = sapply.isTemplateDeclaration()) !is null) | |
711 { | |
712 /* Call: | |
713 * aggr.apply!(fld)() | |
714 */ | |
715 Objects tiargs = new Objects(); | |
716 tiargs.push(cast(void*)fld); | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
717 ec = new DotTemplateInstanceExp(loc, aggr, idapply, tiargs); |
0 | 718 } |
719 else | |
720 { | |
721 /* Call: | |
722 * aggr.apply(flde) | |
723 */ | |
724 ec = new DotIdExp(loc, aggr, idapply); | |
725 exps.push(cast(void*)flde); | |
726 } | |
727 } else { | |
728 ec = new DotIdExp(loc, aggr, idapply); | |
84
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
73
diff
changeset
|
729 exps.push(flde); |
0 | 730 } |
731 e = new CallExp(loc, ec, exps); | |
732 e = e.semantic(sc); | |
733 if (e.type != Type.tint32) { | |
734 error("opApply() function for %s must return an int", tab.toChars()); | |
735 } | |
736 } | |
737 | |
738 if (!cases.dim) | |
739 { | |
740 // Easy case, a clean exit from the loop | |
741 s = new ExpStatement(loc, e); | |
742 } | |
743 else | |
744 { // Construct a switch statement around the return value | |
745 // of the apply function. | |
122
c77e9f4f1793
Statements -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
746 auto a2 = new Statements(); |
0 | 747 |
748 // default: break; takes care of cases 0 and 1 | |
749 s = new BreakStatement(Loc(0), null); | |
750 s = new DefaultStatement(Loc(0), s); | |
122
c77e9f4f1793
Statements -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
751 a2.push(s); |
0 | 752 |
753 // cases 2... | |
754 for (int i = 0; i < cases.dim; i++) | |
755 { | |
756 s = cast(Statement)cases.data[i]; | |
757 s = new CaseStatement(Loc(0), new IntegerExp(i + 2), s); | |
122
c77e9f4f1793
Statements -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
758 a2.push(s); |
0 | 759 } |
760 | |
761 s = new CompoundStatement(loc, a2); | |
762 s = new SwitchStatement(loc, e, s, false); | |
763 s = s.semantic(sc); | |
764 } | |
765 break; | |
766 } | |
767 | |
768 default: | |
769 error("foreach: %s is not an aggregate type", aggr.type.toChars()); | |
770 s = null; // error recovery | |
771 break; | |
772 } | |
773 | |
774 sc.noctor--; | |
775 sc.pop(); | |
776 return s; | |
777 } | |
778 | |
779 bool checkForArgTypes() | |
780 { | |
781 bool result = true; | |
782 | |
126
1765f3ef917d
ClassDeclarations, Arguments -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
122
diff
changeset
|
783 foreach (arg; arguments) |
0 | 784 { |
785 if (!arg.type) | |
786 { | |
787 error("cannot infer type for %s", arg.ident.toChars()); | |
788 arg.type = Type.terror; | |
789 result = false; | |
790 } | |
791 } | |
792 return result; | |
793 } | |
794 | |
72 | 795 override bool hasBreak() |
0 | 796 { |
66 | 797 return true; |
0 | 798 } |
799 | |
72 | 800 override bool hasContinue() |
0 | 801 { |
66 | 802 return true; |
0 | 803 } |
804 | |
72 | 805 override bool usesEH() |
0 | 806 { |
66 | 807 return body_.usesEH(); |
0 | 808 } |
809 | |
72 | 810 override BE blockExit() |
0 | 811 { |
66 | 812 BE result = BEfallthru; |
813 | |
814 if (aggr.canThrow()) | |
815 result |= BEthrow; | |
816 | |
817 if (body_) | |
818 { | |
819 result |= body_.blockExit() & ~(BEbreak | BEcontinue); | |
820 } | |
821 return result; | |
0 | 822 } |
823 | |
72 | 824 override bool comeFrom() |
0 | 825 { |
66 | 826 if (body_) |
827 return body_.comeFrom(); | |
828 | |
829 return false; | |
0 | 830 } |
831 | |
72 | 832 override Expression interpret(InterState istate) |
0 | 833 { |
834 assert(false); | |
835 } | |
836 | |
72 | 837 override void toCBuffer(OutBuffer buf, HdrGenState* hgs) |
0 | 838 { |
66 | 839 buf.writestring(Token.toChars(op)); |
840 buf.writestring(" ("); | |
841 for (int i = 0; i < arguments.dim; i++) | |
842 { | |
126
1765f3ef917d
ClassDeclarations, Arguments -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
122
diff
changeset
|
843 auto a = arguments[i]; |
66 | 844 if (i) |
845 buf.writestring(", "); | |
846 if (a.storageClass & STCref) | |
847 buf.writestring((global.params.Dversion == 1) ? "inout " : "ref "); | |
848 if (a.type) | |
849 a.type.toCBuffer(buf, a.ident, hgs); | |
850 else | |
851 buf.writestring(a.ident.toChars()); | |
852 } | |
853 buf.writestring("; "); | |
854 aggr.toCBuffer(buf, hgs); | |
855 buf.writebyte(')'); | |
856 buf.writenl(); | |
857 buf.writebyte('{'); | |
858 buf.writenl(); | |
859 if (body_) | |
860 body_.toCBuffer(buf, hgs); | |
861 buf.writebyte('}'); | |
862 buf.writenl(); | |
0 | 863 } |
864 | |
72 | 865 override Statement inlineScan(InlineScanState* iss) |
0 | 866 { |
867 aggr = aggr.inlineScan(iss); | |
868 if (body_) | |
869 body_ = body_.inlineScan(iss); | |
870 return this; | |
871 } | |
872 | |
72 | 873 override void toIR(IRState* irs) |
0 | 874 { |
875 assert(false); | |
876 } | |
72 | 877 } |