Mercurial > projects > ddmd
annotate dmd/SliceExp.d @ 12:832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
Some unittest-specific functions implemented
author | korDen |
---|---|
date | Mon, 12 Apr 2010 15:13:00 +0400 |
parents | 10317f0c89a5 |
children | a8b50ff7f201 |
rev | line source |
---|---|
0 | 1 module dmd.SliceExp; |
2 | |
3 import dmd.Expression; | |
4 import dmd.backend.elem; | |
5 import dmd.UnaExp; | |
12
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
6 import dmd.Identifier; |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
7 import dmd.IdentifierExp; |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
8 import dmd.ArrayExp; |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
9 import dmd.STC; |
0 | 10 import dmd.InterState; |
11 import dmd.ScopeDsymbol; | |
12 import dmd.WANT; | |
13 import dmd.ArrayScopeSymbol; | |
14 import dmd.CallExp; | |
15 import dmd.DotIdExp; | |
16 import dmd.Id; | |
17 import dmd.expression.Util; | |
18 import dmd.TypeTuple; | |
19 import dmd.TupleExp; | |
20 import dmd.TypeStruct; | |
21 import dmd.TypeClass; | |
22 import dmd.TY; | |
23 import dmd.Type; | |
24 import dmd.AggregateDeclaration; | |
25 import dmd.OutBuffer; | |
26 import dmd.Loc; | |
27 import dmd.Scope; | |
28 import dmd.InlineCostState; | |
29 import dmd.VarDeclaration; | |
30 import dmd.ErrorExp; | |
31 import dmd.TypeExp; | |
32 import dmd.Argument; | |
33 import dmd.ExpInitializer; | |
34 import dmd.IRState; | |
35 import dmd.InlineDoState; | |
36 import dmd.ArrayTypes; | |
37 import dmd.HdrGenState; | |
38 import dmd.InlineScanState; | |
39 import dmd.TOK; | |
40 import dmd.TypeSArray; | |
41 import dmd.GlobalExpressions; | |
42 import dmd.Global; | |
43 import dmd.PREC; | |
44 | |
45 import dmd.expression.Slice; | |
46 import dmd.expression.Util; | |
47 | |
48 import dmd.backend.Util; | |
49 import dmd.backend.Symbol; | |
50 import dmd.backend.OPER; | |
51 import dmd.backend.TYM; | |
52 import dmd.codegen.Util; | |
53 | |
54 import core.stdc.string; | |
55 | |
56 class SliceExp : UnaExp | |
57 { | |
58 Expression upr; // null if implicit 0 | |
59 Expression lwr; // null if implicit [length - 1] | |
60 | |
61 VarDeclaration lengthVar = null; | |
62 | |
63 this(Loc loc, Expression e1, Expression lwr, Expression upr) | |
64 { | |
65 super(loc, TOK.TOKslice, SliceExp.sizeof, e1); | |
66 this.upr = upr; | |
67 this.lwr = lwr; | |
68 } | |
69 | |
70 Expression syntaxCopy() | |
71 { | |
72 assert(false); | |
73 } | |
74 | |
75 Expression semantic(Scope sc) | |
76 { | |
77 Expression e; | |
78 AggregateDeclaration ad; | |
79 //FuncDeclaration fd; | |
80 ScopeDsymbol sym; | |
81 | |
82 version (LOGSEMANTIC) { | |
83 printf("SliceExp.semantic('%s')\n", toChars()); | |
84 } | |
85 if (type) | |
86 return this; | |
87 | |
88 UnaExp.semantic(sc); | |
89 e1 = resolveProperties(sc, e1); | |
90 | |
91 e = this; | |
92 | |
93 Type t = e1.type.toBasetype(); | |
94 if (t.ty == Tpointer) | |
95 { | |
96 if (!lwr || !upr) | |
97 error("need upper and lower bound to slice pointer"); | |
98 } | |
99 else if (t.ty == Tarray) | |
100 { | |
101 } | |
102 else if (t.ty == Tsarray) | |
103 { | |
104 } | |
105 else if (t.ty == Tclass) | |
106 { | |
107 ad = (cast(TypeClass)t).sym; | |
108 goto L1; | |
109 } | |
110 else if (t.ty == Tstruct) | |
111 { | |
112 ad = (cast(TypeStruct)t).sym; | |
113 | |
114 L1: | |
115 if (search_function(ad, Id.slice)) | |
116 { | |
117 // Rewrite as e1.slice(lwr, upr) | |
118 e = new DotIdExp(loc, e1, Id.slice); | |
119 | |
120 if (lwr) | |
121 { | |
122 assert(upr); | |
123 e = new CallExp(loc, e, lwr, upr); | |
124 } | |
125 else | |
126 { | |
127 assert(!upr); | |
128 e = new CallExp(loc, e); | |
129 } | |
130 e = e.semantic(sc); | |
131 return e; | |
132 } | |
133 goto Lerror; | |
134 } | |
135 else if (t.ty == Ttuple) | |
136 { | |
137 if (!lwr && !upr) | |
138 return e1; | |
139 if (!lwr || !upr) | |
140 { error("need upper and lower bound to slice tuple"); | |
141 goto Lerror; | |
142 } | |
143 } | |
144 else | |
145 goto Lerror; | |
146 | |
147 { | |
148 Scope sc2 = sc; | |
149 if (t.ty == Tsarray || t.ty == Tarray || t.ty == Ttuple) | |
150 { | |
151 sym = new ArrayScopeSymbol(sc, this); | |
152 sym.loc = loc; | |
153 sym.parent = sc.scopesym; | |
154 sc2 = sc.push(sym); | |
155 } | |
156 | |
157 if (lwr) | |
158 { | |
159 lwr = lwr.semantic(sc2); | |
160 lwr = resolveProperties(sc2, lwr); | |
161 lwr = lwr.implicitCastTo(sc2, Type.tsize_t); | |
162 } | |
163 if (upr) | |
164 { | |
165 upr = upr.semantic(sc2); | |
166 upr = resolveProperties(sc2, upr); | |
167 upr = upr.implicitCastTo(sc2, Type.tsize_t); | |
168 } | |
169 | |
170 if (sc2 != sc) | |
171 sc2.pop(); | |
172 } | |
173 | |
174 if (t.ty == Ttuple) | |
175 { | |
176 lwr = lwr.optimize(WANTvalue); | |
177 upr = upr.optimize(WANTvalue); | |
178 ulong i1 = lwr.toUInteger(); | |
179 ulong i2 = upr.toUInteger(); | |
180 | |
181 size_t length; | |
182 TupleExp te; | |
183 TypeTuple tup; | |
184 | |
185 if (e1.op == TOKtuple) // slicing an expression tuple | |
186 { | |
187 te = cast(TupleExp)e1; | |
188 length = te.exps.dim; | |
189 } | |
190 else if (e1.op == TOKtype) // slicing a type tuple | |
191 { | |
192 tup = cast(TypeTuple)t; | |
193 length = Argument.dim(tup.arguments); | |
194 } | |
195 else | |
196 assert(0); | |
197 | |
198 if (i1 <= i2 && i2 <= length) | |
199 { | |
200 size_t j1 = cast(size_t) i1; | |
201 size_t j2 = cast(size_t) i2; | |
202 | |
203 if (e1.op == TOKtuple) | |
204 { | |
205 Expressions exps = new Expressions; | |
206 exps.setDim(j2 - j1); | |
207 for (size_t i = 0; i < j2 - j1; i++) | |
208 { | |
209 Expression e2 = cast(Expression)te.exps.data[j1 + i]; | |
210 exps.data[i] = cast(void*)e2; | |
211 } | |
212 e = new TupleExp(loc, exps); | |
213 } | |
214 else | |
215 { | |
216 Arguments args = new Arguments; | |
217 args.reserve(j2 - j1); | |
218 for (size_t i = j1; i < j2; i++) | |
219 { | |
220 Argument arg = Argument.getNth(tup.arguments, i); | |
221 args.push(cast(void*)arg); | |
222 } | |
223 e = new TypeExp(e1.loc, new TypeTuple(args)); | |
224 } | |
225 e = e.semantic(sc); | |
226 } | |
227 else | |
228 { | |
229 error("string slice [%ju .. %ju] is out of bounds", i1, i2); | |
230 e = new ErrorExp(); | |
231 } | |
232 return e; | |
233 } | |
234 | |
235 if (t.ty == Tarray) | |
236 { | |
237 type = e1.type; | |
238 } | |
239 else | |
240 type = t.nextOf().arrayOf(); | |
241 return e; | |
242 | |
243 Lerror: | |
244 string s; | |
245 if (t.ty == Tvoid) | |
246 s = e1.toChars(); | |
247 else | |
248 s = t.toChars(); | |
249 error("%s cannot be sliced with []", s); | |
250 e = new ErrorExp(); | |
251 return e; | |
252 } | |
253 | |
254 void checkEscape() | |
255 { | |
256 e1.checkEscape(); | |
257 } | |
258 | |
259 version (DMDV2) { | |
260 int isLvalue() | |
261 { | |
262 return 1; | |
263 } | |
264 } | |
265 Expression toLvalue(Scope sc, Expression e) | |
266 { | |
267 return this; | |
268 } | |
269 | |
270 Expression modifiableLvalue(Scope sc, Expression e) | |
271 { | |
272 error("slice expression %s is not a modifiable lvalue", toChars()); | |
273 return this; | |
274 } | |
275 | |
276 void toCBuffer(OutBuffer buf, HdrGenState* hgs) | |
277 { | |
278 expToCBuffer(buf, hgs, e1, precedence[op]); | |
279 buf.writeByte('['); | |
280 if (upr || lwr) | |
281 { | |
282 if (lwr) | |
283 expToCBuffer(buf, hgs, lwr, PREC.PREC_assign); | |
284 else | |
285 buf.writeByte('0'); | |
286 buf.writestring(".."); | |
287 if (upr) | |
288 expToCBuffer(buf, hgs, upr, PREC.PREC_assign); | |
289 else | |
290 buf.writestring("length"); // BUG: should be array.length | |
291 } | |
292 buf.writeByte(']'); | |
293 } | |
294 | |
295 Expression optimize(int result) | |
296 { | |
297 Expression e; | |
298 | |
299 //printf("SliceExp::optimize(result = %d) %s\n", result, toChars()); | |
300 e = this; | |
301 e1 = e1.optimize(WANTvalue | (result & WANTinterpret)); | |
302 if (!lwr) | |
303 { | |
304 if (e1.op == TOKstring) | |
305 { | |
306 // Convert slice of string literal into dynamic array | |
307 Type t = e1.type.toBasetype(); | |
308 if (t.nextOf()) | |
309 e = e1.castTo(null, t.nextOf().arrayOf()); | |
310 } | |
311 return e; | |
312 } | |
313 e1 = fromConstInitializer(result, e1); | |
314 lwr = lwr.optimize(WANTvalue | (result & WANTinterpret)); | |
315 upr = upr.optimize(WANTvalue | (result & WANTinterpret)); | |
316 e = Slice(type, e1, lwr, upr); | |
317 if (e is EXP_CANT_INTERPRET) | |
318 e = this; | |
319 //printf("-SliceExp::optimize() %s\n", e->toChars()); | |
320 return e; | |
321 } | |
322 | |
323 Expression interpret(InterState* istate) | |
324 { | |
325 assert(false); | |
326 } | |
327 | |
328 void dump(int indent) | |
329 { | |
330 assert(false); | |
331 } | |
332 | |
333 elem* toElem(IRState* irs) | |
334 { | |
335 elem* e; | |
336 Type t1; | |
337 | |
338 //printf("SliceExp.toElem()\n"); | |
339 t1 = e1.type.toBasetype(); | |
340 e = e1.toElem(irs); | |
341 if (lwr) | |
342 { | |
343 elem* elwr; | |
344 elem* elwr2; | |
345 elem* eupr; | |
346 elem* eptr; | |
347 elem* einit; | |
348 int sz; | |
349 | |
350 einit = resolveLengthVar(lengthVar, &e, t1); | |
351 | |
352 sz = cast(uint)t1.nextOf().size(); | |
353 | |
354 elwr = lwr.toElem(irs); | |
355 eupr = upr.toElem(irs); | |
356 | |
357 elwr2 = el_same(&elwr); | |
358 | |
359 // Create an array reference where: | |
360 // length is (upr - lwr) | |
361 // pointer is (ptr + lwr*sz) | |
362 // Combine as (length pair ptr) | |
363 | |
364 if (global.params.useArrayBounds) | |
365 { | |
366 // Checks (unsigned compares): | |
367 // upr <= array.length | |
368 // lwr <= upr | |
369 | |
370 elem *c1; | |
371 elem *c2; | |
372 elem *ea; | |
373 elem *eb; | |
374 elem *eupr2; | |
375 elem *elength; | |
376 | |
377 if (t1.ty == Tpointer) | |
378 { | |
379 // Just do lwr <= upr check | |
380 | |
381 eupr2 = el_same(&eupr); | |
382 eupr2.Ety = TYuint; // make sure unsigned comparison | |
383 c1 = el_bin(OPle, TYint, elwr2, eupr2); | |
384 c1 = el_combine(eupr, c1); | |
385 goto L2; | |
386 } | |
387 else if (t1.ty == Tsarray) | |
388 { | |
389 TypeSArray tsa = cast(TypeSArray)t1; | |
390 ulong length = tsa.dim.toInteger(); | |
391 | |
392 elength = el_long(TYuint, length); | |
393 goto L1; | |
394 } | |
395 else if (t1.ty == Tarray) | |
396 { | |
397 if (lengthVar) | |
398 elength = el_var(lengthVar.toSymbol()); | |
399 else | |
400 { | |
401 elength = e; | |
402 e = el_same(&elength); | |
403 elength = el_una(OP64_32, TYuint, elength); | |
404 } | |
405 L1: | |
406 eupr2 = el_same(&eupr); | |
407 c1 = el_bin(OPle, TYint, eupr, elength); | |
408 eupr2.Ety = TYuint; // make sure unsigned comparison | |
409 c2 = el_bin(OPle, TYint, elwr2, eupr2); | |
410 c1 = el_bin(OPandand, TYint, c1, c2); // (c1 && c2) | |
411 | |
412 L2: | |
413 // Construct: (c1 || ModuleArray(line)) | |
414 Symbol* sassert; | |
415 | |
416 sassert = irs.blx.module_.toModuleArray(); | |
417 ea = el_bin(OPcall,TYvoid,el_var(sassert), el_long(TYint, loc.linnum)); | |
418 eb = el_bin(OPoror,TYvoid,c1,ea); | |
419 elwr = el_combine(elwr, eb); | |
420 | |
421 elwr2 = el_copytree(elwr2); | |
422 eupr = el_copytree(eupr2); | |
423 } | |
424 } | |
425 | |
426 eptr = array_toPtr(e1.type, e); | |
427 | |
428 elem *elength = el_bin(OPmin, TYint, eupr, elwr2); | |
429 eptr = el_bin(OPadd, TYnptr, eptr, el_bin(OPmul, TYint, el_copytree(elwr2), el_long(TYint, sz))); | |
430 | |
431 e = el_pair(TYullong, elength, eptr); | |
432 e = el_combine(elwr, e); | |
433 e = el_combine(einit, e); | |
434 } | |
435 else if (t1.ty == Tsarray) | |
436 { | |
437 e = sarray_toDarray(loc, t1, null, e); | |
438 } | |
439 | |
440 el_setLoc(e,loc); | |
441 return e; | |
442 } | |
443 | |
444 void scanForNestedRef(Scope sc) | |
445 { | |
446 assert(false); | |
447 } | |
448 | |
449 void buildArrayIdent(OutBuffer buf, Expressions arguments) | |
450 { | |
12
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
451 buf.writestring("Slice"); |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
452 arguments.shift(cast(void*)this); |
0 | 453 } |
454 | |
455 Expression buildArrayLoop(Arguments fparams) | |
456 { | |
12
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
457 Identifier id = Identifier.generateId("p", fparams.dim); |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
458 Argument param = new Argument(STCconst, type, id, null); |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
459 fparams.shift(cast(void*)param); |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
460 Expression e = new IdentifierExp(Loc(0), id); |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
461 Expressions arguments = new Expressions(); |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
462 Expression index = new IdentifierExp(Loc(0), Id.p); |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
463 arguments.push(cast(void*)index); |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
464 e = new ArrayExp(Loc(0), e, arguments); |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
465 return e; |
0 | 466 } |
467 | |
468 int inlineCost(InlineCostState* ics) | |
469 { | |
470 int cost = 1 + e1.inlineCost(ics); | |
471 if (lwr) | |
472 cost += lwr.inlineCost(ics); | |
473 if (upr) | |
474 cost += upr.inlineCost(ics); | |
475 return cost; | |
476 } | |
477 | |
478 Expression doInline(InlineDoState ids) | |
479 { | |
480 SliceExp are = cast(SliceExp)copy(); | |
481 | |
482 are.e1 = e1.doInline(ids); | |
483 | |
484 if (lengthVar) | |
485 { | |
486 //printf("lengthVar\n"); | |
487 VarDeclaration vd = lengthVar; | |
488 ExpInitializer ie; | |
489 ExpInitializer ieto; | |
490 VarDeclaration vto; | |
491 | |
492 vto = new VarDeclaration(vd.loc, vd.type, vd.ident, vd.init); | |
493 ///*vto = *vd; | |
494 memcpy(cast(void*)vto, cast(void*)vd, VarDeclaration.classinfo.init.length); | |
495 | |
496 vto.parent = ids.parent; | |
497 vto.csym = null; | |
498 vto.isym = null; | |
499 | |
500 ids.from.push(cast(void*)vd); | |
501 ids.to.push(cast(void*)vto); | |
502 | |
503 if (vd.init) | |
504 { | |
505 ie = vd.init.isExpInitializer(); | |
506 assert(ie); | |
507 ieto = new ExpInitializer(ie.loc, ie.exp.doInline(ids)); | |
508 vto.init = ieto; | |
509 } | |
510 | |
511 are.lengthVar = vto; | |
512 } | |
513 | |
514 if (lwr) | |
515 are.lwr = lwr.doInline(ids); | |
516 if (upr) | |
517 are.upr = upr.doInline(ids); | |
518 return are; | |
519 } | |
520 | |
521 Expression inlineScan(InlineScanState* iss) | |
522 { | |
523 e1 = e1.inlineScan(iss); | |
524 if (lwr) | |
525 lwr = lwr.inlineScan(iss); | |
526 if (upr) | |
527 upr = upr.inlineScan(iss); | |
528 return this; | |
529 } | |
530 } | |
531 |