comparison dmd/SliceExp.d @ 0:10317f0c89a5

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