comparison dmd/SliceExp.d @ 178:e3afd1303184

Many small bugs fixed Made all classes derive from TObject to detect memory leaks (functionality is disabled for now) Began work on overriding backend memory allocations (to avoid memory leaks)
author korDen
date Sun, 17 Oct 2010 07:42:00 +0400
parents af1bebfd96a4
children b0d41ff5e0df
comparison
equal deleted inserted replaced
177:1475fd394c9e 178:e3afd1303184
10 import dmd.ArrayExp; 10 import dmd.ArrayExp;
11 import dmd.STC; 11 import dmd.STC;
12 import dmd.InterState; 12 import dmd.InterState;
13 import dmd.ScopeDsymbol; 13 import dmd.ScopeDsymbol;
14 import dmd.WANT; 14 import dmd.WANT;
15 import dmd.Util;
15 import dmd.ArrayScopeSymbol; 16 import dmd.ArrayScopeSymbol;
16 import dmd.CallExp; 17 import dmd.CallExp;
17 import dmd.DotIdExp; 18 import dmd.DotIdExp;
18 import dmd.Id; 19 import dmd.Id;
19 import dmd.expression.Util; 20 import dmd.expression.Util;
62 63
63 VarDeclaration lengthVar = null; 64 VarDeclaration lengthVar = null;
64 65
65 this(Loc loc, Expression e1, Expression lwr, Expression upr) 66 this(Loc loc, Expression e1, Expression lwr, Expression upr)
66 { 67 {
68 register();
67 super(loc, TOK.TOKslice, SliceExp.sizeof, e1); 69 super(loc, TOK.TOKslice, SliceExp.sizeof, e1);
68 this.upr = upr; 70 this.upr = upr;
69 this.lwr = lwr; 71 this.lwr = lwr;
70 } 72 }
71 73
131 { 133 {
132 assert(upr); 134 assert(upr);
133 e = new CallExp(loc, e, lwr, upr); 135 e = new CallExp(loc, e, lwr, upr);
134 } 136 }
135 else 137 else
136 { 138 {
137 assert(!upr); 139 assert(!upr);
138 e = new CallExp(loc, e); 140 e = new CallExp(loc, e);
139 } 141 }
140 e = e.semantic(sc); 142 e = e.semantic(sc);
141 return e; 143 return e;
163 sym.parent = sc.scopesym; 165 sym.parent = sc.scopesym;
164 sc2 = sc.push(sym); 166 sc2 = sc.push(sym);
165 } 167 }
166 168
167 if (lwr) 169 if (lwr)
168 { 170 {
169 lwr = lwr.semantic(sc2); 171 lwr = lwr.semantic(sc2);
170 lwr = resolveProperties(sc2, lwr); 172 lwr = resolveProperties(sc2, lwr);
171 lwr = lwr.implicitCastTo(sc2, Type.tsize_t); 173 lwr = lwr.implicitCastTo(sc2, Type.tsize_t);
172 } 174 }
173 if (upr) 175 if (upr)
174 { 176 {
175 upr = upr.semantic(sc2); 177 upr = upr.semantic(sc2);
176 upr = resolveProperties(sc2, upr); 178 upr = resolveProperties(sc2, upr);
177 upr = upr.implicitCastTo(sc2, Type.tsize_t); 179 upr = upr.implicitCastTo(sc2, Type.tsize_t);
178 } 180 }
179 181
191 size_t length; 193 size_t length;
192 TupleExp te; 194 TupleExp te;
193 TypeTuple tup; 195 TypeTuple tup;
194 196
195 if (e1.op == TOKtuple) // slicing an expression tuple 197 if (e1.op == TOKtuple) // slicing an expression tuple
196 { 198 {
197 te = cast(TupleExp)e1; 199 te = cast(TupleExp)e1;
198 length = te.exps.dim; 200 length = te.exps.dim;
199 } 201 }
200 else if (e1.op == TOKtype) // slicing a type tuple 202 else if (e1.op == TOKtype) // slicing a type tuple
201 { 203 {
202 tup = cast(TypeTuple)t; 204 tup = cast(TypeTuple)t;
203 length = Parameter.dim(tup.arguments); 205 length = Parameter.dim(tup.arguments);
204 } 206 }
205 else 207 else
206 assert(0); 208 assert(0);
207 209
208 if (i1 <= i2 && i2 <= length) 210 if (i1 <= i2 && i2 <= length)
209 { 211 {
210 size_t j1 = cast(size_t) i1; 212 size_t j1 = cast(size_t) i1;
211 size_t j2 = cast(size_t) i2; 213 size_t j2 = cast(size_t) i2;
212 214
213 if (e1.op == TOKtuple) 215 if (e1.op == TOKtuple)
214 { 216 {
215 auto exps = new Expressions; 217 auto exps = new Expressions;
216 exps.setDim(j2 - j1); 218 exps.setDim(j2 - j1);
217 for (size_t i = 0; i < j2 - j1; i++) 219 for (size_t i = 0; i < j2 - j1; i++)
218 { 220 {
219 auto e2 = te.exps[j1 + i]; 221 auto e2 = te.exps[j1 + i];
220 exps[i] = e2; 222 exps[i] = e2;
221 } 223 }
222 e = new TupleExp(loc, exps); 224 e = new TupleExp(loc, exps);
223 } 225 }
224 else 226 else
225 { 227 {
226 auto args = new Parameters; 228 auto args = new Parameters;
227 args.reserve(j2 - j1); 229 args.reserve(j2 - j1);
228 for (size_t i = j1; i < j2; i++) 230 for (size_t i = j1; i < j2; i++)
229 { 231 {
230 auto arg = Parameter.getNth(tup.arguments, i); 232 auto arg = Parameter.getNth(tup.arguments, i);
231 args.push(arg); 233 args.push(arg);
232 } 234 }
233 e = new TypeExp(e1.loc, new TypeTuple(args)); 235 e = new TypeExp(e1.loc, new TypeTuple(args));
234 } 236 }
263 265
264 override void checkEscape() 266 override void checkEscape()
265 { 267 {
266 e1.checkEscape(); 268 e1.checkEscape();
267 } 269 }
268 270
269 override void checkEscapeRef() 271 override void checkEscapeRef()
270 { 272 {
271 e1.checkEscapeRef(); 273 e1.checkEscapeRef();
272 } 274 }
273 275
313 315
314 //printf("SliceExp::optimize(result = %d) %s\n", result, toChars()); 316 //printf("SliceExp::optimize(result = %d) %s\n", result, toChars());
315 e = this; 317 e = this;
316 e1 = e1.optimize(WANTvalue | (result & WANTinterpret)); 318 e1 = e1.optimize(WANTvalue | (result & WANTinterpret));
317 if (!lwr) 319 if (!lwr)
318 { 320 {
319 if (e1.op == TOKstring) 321 if (e1.op == TOKstring)
320 { 322 {
321 // Convert slice of string literal into dynamic array 323 // Convert slice of string literal into dynamic array
322 Type t = e1.type.toBasetype(); 324 Type t = e1.type.toBasetype();
323 if (t.nextOf()) 325 if (t.nextOf())
324 e = e1.castTo(null, t.nextOf().arrayOf()); 326 e = e1.castTo(null, t.nextOf().arrayOf());
325 } 327 }
425 c1 = el_bin(OPle, TYint, elwr2, eupr2); 427 c1 = el_bin(OPle, TYint, elwr2, eupr2);
426 c1 = el_combine(eupr, c1); 428 c1 = el_combine(eupr, c1);
427 goto L2; 429 goto L2;
428 } 430 }
429 else if (t1.ty == Tsarray) 431 else if (t1.ty == Tsarray)
430 { 432 {
431 TypeSArray tsa = cast(TypeSArray)t1; 433 TypeSArray tsa = cast(TypeSArray)t1;
432 ulong length = tsa.dim.toInteger(); 434 ulong length = tsa.dim.toInteger();
433 435
434 elength = el_long(TYuint, length); 436 elength = el_long(TYuint, length);
435 goto L1; 437 goto L1;
486 override void scanForNestedRef(Scope sc) 488 override void scanForNestedRef(Scope sc)
487 { 489 {
488 e1.scanForNestedRef(sc); 490 e1.scanForNestedRef(sc);
489 491
490 if (lengthVar) 492 if (lengthVar)
491 { 493 {
492 //printf("lengthVar\n"); 494 //printf("lengthVar\n");
493 lengthVar.parent = sc.parent; 495 lengthVar.parent = sc.parent;
494 } 496 }
495 if (lwr) 497 if (lwr)
496 lwr.scanForNestedRef(sc); 498 lwr.scanForNestedRef(sc);
532 SliceExp are = cast(SliceExp)copy(); 534 SliceExp are = cast(SliceExp)copy();
533 535
534 are.e1 = e1.doInline(ids); 536 are.e1 = e1.doInline(ids);
535 537
536 if (lengthVar) 538 if (lengthVar)
537 { 539 {
538 //printf("lengthVar\n"); 540 //printf("lengthVar\n");
539 VarDeclaration vd = lengthVar; 541 VarDeclaration vd = lengthVar;
540 ExpInitializer ie; 542 ExpInitializer ie;
541 ExpInitializer ieto; 543 ExpInitializer ieto;
542 VarDeclaration vto; 544 VarDeclaration vto;
543 545
544 vto = new VarDeclaration(vd.loc, vd.type, vd.ident, vd.init); 546 vto = cloneThis(vd);
545 ///*vto = *vd; 547
546 memcpy(cast(void*)vto, cast(void*)vd, VarDeclaration.classinfo.init.length);
547
548 vto.parent = ids.parent; 548 vto.parent = ids.parent;
549 vto.csym = null; 549 vto.csym = null;
550 vto.isym = null; 550 vto.isym = null;
551 551
552 ids.from.push(cast(void*)vd); 552 ids.from.push(cast(void*)vd);