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