Mercurial > projects > ddmd
annotate dmd/IndexExp.d @ 129:010eb8f0e18d
further work on dmd test suite
author | korDen |
---|---|
date | Sun, 05 Sep 2010 15:32:22 +0400 |
parents | 9e39c7de8438 |
children | 60bb0fe4563e |
rev | line source |
---|---|
72 | 1 module dmd.IndexExp; |
2 | |
114 | 3 import dmd.common; |
72 | 4 import dmd.Expression; |
5 import dmd.backend.elem; | |
6 import dmd.InterState; | |
7 import dmd.OutBuffer; | |
8 import dmd.Loc; | |
9 import dmd.Scope; | |
10 import dmd.VarDeclaration; | |
0 | 11 import dmd.InlineDoState; |
12 import dmd.Type; | |
13 import dmd.ScopeDsymbol; | |
14 import dmd.TY; | |
15 import dmd.ArrayScopeSymbol; | |
129 | 16 import dmd.PREC; |
0 | 17 import dmd.TypeNext; |
18 import dmd.TypeSArray; | |
19 import dmd.TypeAArray; | |
72 | 20 import dmd.UnaExp; |
21 import dmd.IRState; | |
22 import dmd.BinExp; | |
23 import dmd.HdrGenState; | |
0 | 24 import dmd.TOK; |
25 import dmd.WANT; | |
26 import dmd.TupleExp; | |
27 import dmd.TypeTuple; | |
28 import dmd.Argument; | |
29 import dmd.TypeExp; | |
30 import dmd.VarExp; | |
31 import dmd.STC; | |
32 import dmd.GlobalExpressions; | |
33 import dmd.ExpInitializer; | |
34 import dmd.Global; | |
35 | |
36 import dmd.expression.util.arrayTypeCompatible; | |
37 import dmd.expression.Util; | |
38 import dmd.expression.Index; | |
129 | 39 import dmd.expression.ArrayLength; |
0 | 40 |
41 import dmd.backend.Symbol; | |
42 import dmd.backend.Util; | |
43 import dmd.codegen.Util; | |
44 import dmd.backend.OPER; | |
45 import dmd.backend.mTY; | |
46 import dmd.backend.TYM; | |
47 | |
72 | 48 import core.stdc.string; |
73 | 49 import core.stdc.stdio; |
50 | |
0 | 51 class IndexExp : BinExp |
52 { | |
53 VarDeclaration lengthVar; | |
54 int modifiable = 0; // assume it is an rvalue | |
55 | |
56 this(Loc loc, Expression e1, Expression e2) | |
57 { | |
58 super(loc, TOK.TOKindex, IndexExp.sizeof, e1, e2); | |
59 //printf("IndexExp.IndexExp('%s')\n", toChars()); | |
60 } | |
61 | |
72 | 62 override Expression semantic(Scope sc) |
0 | 63 { |
64 Expression e; | |
65 BinExp b; | |
66 UnaExp u; | |
67 Type t1; | |
68 ScopeDsymbol sym; | |
69 | |
70 version (LOGSEMANTIC) { | |
71 printf("IndexExp.semantic('%s')\n", toChars()); | |
72 } | |
73 if (type) | |
74 return this; | |
75 if (!e1.type) | |
76 e1 = e1.semantic(sc); | |
77 assert(e1.type); // semantic() should already be run on it | |
78 e = this; | |
79 | |
80 // Note that unlike C we do not implement the int[ptr] | |
81 | |
82 t1 = e1.type.toBasetype(); | |
83 | |
84 if (t1.ty == Tsarray || t1.ty == Tarray || t1.ty == Ttuple) | |
85 { | |
86 // Create scope for 'length' variable | |
87 sym = new ArrayScopeSymbol(sc, this); | |
88 sym.loc = loc; | |
89 sym.parent = sc.scopesym; | |
90 sc = sc.push(sym); | |
91 } | |
92 | |
93 e2 = e2.semantic(sc); | |
94 if (!e2.type) | |
95 { | |
96 error("%s has no value", e2.toChars()); | |
97 e2.type = Type.terror; | |
98 } | |
99 e2 = resolveProperties(sc, e2); | |
100 | |
101 if (t1.ty == Tsarray || t1.ty == Tarray || t1.ty == Ttuple) | |
102 sc = sc.pop(); | |
103 | |
104 switch (t1.ty) | |
105 { | |
106 case Tpointer: | |
107 case Tarray: | |
108 e2 = e2.implicitCastTo(sc, Type.tsize_t); | |
109 e.type = (cast(TypeNext)t1).next; | |
110 break; | |
111 | |
112 case Tsarray: | |
113 { | |
114 e2 = e2.implicitCastTo(sc, Type.tsize_t); | |
115 | |
116 TypeSArray tsa = cast(TypeSArray)t1; | |
117 | |
118 static if (false) { | |
119 // Don't do now, because it might be short-circuit evaluated | |
120 // Do compile time array bounds checking if possible | |
121 e2 = e2.optimize(WANTvalue); | |
122 if (e2.op == TOKint64) | |
123 { | |
124 ulong index = e2.toInteger(); | |
125 ulong length = tsa.dim.toInteger(); | |
126 if (index < 0 || index >= length) | |
127 error("array index [%lld] is outside array bounds [0 .. %lld]", index, length); | |
128 } | |
129 } | |
130 e.type = t1.nextOf(); | |
131 break; | |
132 } | |
133 | |
134 case Taarray: | |
135 { | |
136 TypeAArray taa = cast(TypeAArray)t1; | |
137 if (!arrayTypeCompatible(e2.loc, e2.type, taa.index)) | |
138 { | |
139 e2 = e2.implicitCastTo(sc, taa.index); // type checking | |
140 } | |
141 type = taa.next; | |
142 break; | |
143 } | |
144 | |
145 case Ttuple: | |
146 { | |
147 e2 = e2.implicitCastTo(sc, Type.tsize_t); | |
148 e2 = e2.optimize(WANTvalue | WANTinterpret); | |
149 ulong index = e2.toUInteger(); | |
150 size_t length; | |
151 TupleExp te; | |
152 TypeTuple tup; | |
153 | |
154 if (e1.op == TOKtuple) | |
155 { | |
156 te = cast(TupleExp)e1; | |
157 length = te.exps.dim; | |
158 } | |
159 else if (e1.op == TOKtype) | |
160 { | |
161 tup = cast(TypeTuple)t1; | |
162 length = Argument.dim(tup.arguments); | |
163 } | |
164 else | |
165 assert(0); | |
166 | |
167 if (index < length) | |
168 { | |
169 if (e1.op == TOKtuple) | |
113
3482c73a991b
More cleanup for arrays
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
79
diff
changeset
|
170 e = te.exps[cast(size_t)index]; |
0 | 171 else |
172 e = new TypeExp(e1.loc, Argument.getNth(tup.arguments, cast(size_t)index).type); | |
173 } | |
174 else | |
175 { | |
176 error("array index [%ju] is outside array bounds [0 .. %zu]", index, length); | |
177 e = e1; | |
178 } | |
179 break; | |
180 } | |
181 | |
182 default: | |
183 error("%s must be an array or pointer type, not %s", e1.toChars(), e1.type.toChars()); | |
184 type = Type.tint32; | |
185 break; | |
186 } | |
187 | |
188 return e; | |
189 } | |
190 | |
72 | 191 override int isLvalue() |
0 | 192 { |
129 | 193 return true; |
0 | 194 } |
195 | |
72 | 196 override Expression toLvalue(Scope sc, Expression e) |
0 | 197 { |
198 // if (type && type.toBasetype().ty == Tvoid) | |
199 // error("voids have no value"); | |
200 return this; | |
201 } | |
202 | |
72 | 203 override Expression modifiableLvalue(Scope sc, Expression e) |
0 | 204 { |
205 //printf("IndexExp::modifiableLvalue(%s)\n", toChars()); | |
206 modifiable = 1; | |
207 if (e1.op == TOKstring) | |
208 error("string literals are immutable"); | |
209 if (type && !type.isMutable()) | |
210 error("%s isn't mutable", e.toChars()); | |
211 if (e1.type.toBasetype().ty == Taarray) | |
212 e1 = e1.modifiableLvalue(sc, e1); | |
213 return toLvalue(sc, e); | |
214 } | |
215 | |
72 | 216 override void toCBuffer(OutBuffer buf, HdrGenState* hgs) |
0 | 217 { |
129 | 218 expToCBuffer(buf, hgs, e1, PREC.PREC_primary); |
219 buf.writeByte('['); | |
220 expToCBuffer(buf, hgs, e2, PREC.PREC_assign); | |
221 buf.writeByte(']'); | |
0 | 222 } |
223 | |
72 | 224 override Expression optimize(int result) |
0 | 225 { |
226 Expression e; | |
227 | |
228 //printf("IndexExp::optimize(result = %d) %s\n", result, toChars()); | |
229 Expression e1 = this.e1.optimize(WANTvalue | (result & WANTinterpret)); | |
230 e1 = fromConstInitializer(result, e1); | |
231 if (this.e1.op == TOKvar) | |
232 { | |
233 VarExp ve = cast(VarExp)this.e1; | |
234 if (ve.var.storage_class & STCmanifest) | |
235 { | |
236 /* We generally don't want to have more than one copy of an | |
237 * array literal, but if it's an enum we have to because the | |
238 * enum isn't stored elsewhere. See Bugzilla 2559 | |
239 */ | |
240 this.e1 = e1; | |
241 } | |
242 } | |
243 | |
244 e2 = e2.optimize(WANTvalue | (result & WANTinterpret)); | |
245 e = Index(type, e1, e2); | |
246 if (e is EXP_CANT_INTERPRET) | |
247 e = this; | |
248 | |
249 return e; | |
250 } | |
251 | |
72 | 252 override Expression interpret(InterState istate) |
0 | 253 { |
129 | 254 Expression e; |
255 Expression e1; | |
256 Expression e2; | |
257 | |
258 version (LOG) { | |
259 printf("IndexExp.interpret() %s\n", toChars()); | |
260 } | |
261 e1 = this.e1.interpret(istate); | |
262 if (e1 is EXP_CANT_INTERPRET) | |
263 goto Lcant; | |
264 | |
265 if (e1.op == TOKstring || e1.op == TOKarrayliteral) | |
266 { | |
267 /* Set the $ variable | |
268 */ | |
269 e = ArrayLength(Type.tsize_t, e1); | |
270 if (e is EXP_CANT_INTERPRET) | |
271 goto Lcant; | |
272 if (lengthVar) | |
273 lengthVar.value = e; | |
274 } | |
275 | |
276 e2 = this.e2.interpret(istate); | |
277 if (e2 is EXP_CANT_INTERPRET) | |
278 goto Lcant; | |
279 return Index(type, e1, e2); | |
280 | |
281 Lcant: | |
282 return EXP_CANT_INTERPRET; | |
0 | 283 } |
284 | |
72 | 285 override Expression doInline(InlineDoState ids) |
0 | 286 { |
287 IndexExp are = cast(IndexExp)copy(); | |
288 | |
289 are.e1 = e1.doInline(ids); | |
290 | |
291 if (lengthVar) | |
292 { //printf("lengthVar\n"); | |
293 VarDeclaration vd = lengthVar; | |
294 ExpInitializer ie; | |
295 ExpInitializer ieto; | |
296 VarDeclaration vto; | |
297 | |
298 vto = new VarDeclaration(vd.loc, vd.type, vd.ident, vd.init); | |
299 ///*vto = *vd; | |
300 memcpy(cast(void*)vto, cast(void*)vd, VarDeclaration.classinfo.init.length); | |
301 vto.parent = ids.parent; | |
302 vto.csym = null; | |
303 vto.isym = null; | |
304 | |
305 ids.from.push(cast(void*)vd); | |
306 ids.to.push(cast(void*)vto); | |
307 | |
308 if (vd.init) | |
309 { | |
310 ie = vd.init.isExpInitializer(); | |
311 assert(ie); | |
312 ieto = new ExpInitializer(ie.loc, ie.exp.doInline(ids)); | |
313 vto.init = ieto; | |
314 } | |
315 | |
316 are.lengthVar = vto; | |
317 } | |
318 are.e2 = e2.doInline(ids); | |
319 return are; | |
320 } | |
321 | |
72 | 322 override void scanForNestedRef(Scope sc) |
0 | 323 { |
123 | 324 e1.scanForNestedRef(sc); |
325 | |
326 if (lengthVar) | |
327 { | |
328 //printf("lengthVar\n"); | |
329 lengthVar.parent = sc.parent; | |
330 } | |
331 e2.scanForNestedRef(sc); | |
0 | 332 } |
333 | |
72 | 334 override elem* toElem(IRState* irs) |
0 | 335 { |
336 elem* e; | |
337 elem* n1 = e1.toElem(irs); | |
338 elem* eb = null; | |
339 | |
340 //printf("IndexExp.toElem() %s\n", toChars()); | |
79 | 341 Type t1 = e1.type.toBasetype(); |
0 | 342 if (t1.ty == Taarray) |
343 { | |
344 // set to: | |
345 // *aaGet(aa, keyti, valuesize, index); | |
346 | |
347 TypeAArray taa = cast(TypeAArray)t1; | |
348 elem* keyti; | |
349 elem* ep; | |
350 int vsize = cast(int)taa.next.size(); | |
351 Symbol* s; | |
352 | |
353 // n2 becomes the index, also known as the key | |
79 | 354 elem* n2 = e2.toElem(irs); |
0 | 355 if (tybasic(n2.Ety) == TYstruct || tybasic(n2.Ety) == TYarray) |
356 { | |
357 n2 = el_una(OPstrpar, TYstruct, n2); | |
358 n2.Enumbytes = n2.E1.Enumbytes; | |
79 | 359 if (taa.index.ty == Tsarray) |
360 { | |
361 assert(e2.type.size() == taa.index.size()); | |
362 n2.Enumbytes = cast(size_t) taa.index.size(); | |
363 } | |
0 | 364 //printf("numbytes = %d\n", n2.Enumbytes); |
365 assert(n2.Enumbytes); | |
366 } | |
79 | 367 elem* valuesize = el_long(TYuint, vsize); // BUG: should be TYsize_t |
0 | 368 //printf("valuesize: "); elem_print(valuesize); |
369 if (modifiable) | |
370 { | |
371 n1 = el_una(OPaddr, TYnptr, n1); | |
372 s = taa.aaGetSymbol("Get", 1); | |
373 } | |
374 else | |
375 { | |
376 s = taa.aaGetSymbol("GetRvalue", 1); | |
377 } | |
378 //printf("taa.index = %s\n", taa.index.toChars()); | |
379 keyti = taa.index.getInternalTypeInfo(null).toElem(irs); | |
380 //keyti = taa.index.getTypeInfo(null).toElem(irs); | |
381 //printf("keyti:\n"); | |
382 //elem_print(keyti); | |
383 ep = el_params(n2, valuesize, keyti, n1, null); | |
384 e = el_bin(OPcall, TYnptr, el_var(s), ep); | |
79 | 385 |
0 | 386 if (global.params.useArrayBounds) |
387 { | |
388 elem* ea; | |
389 | |
79 | 390 elem* n = el_same(&e); |
0 | 391 |
392 // Construct: ((e || ModuleAssert(line)),n) | |
79 | 393 Symbol* sassert = irs.blx.module_.toModuleArray(); |
394 | |
0 | 395 ea = el_bin(OPcall,TYvoid,el_var(sassert), |
396 el_long(TYint, loc.linnum)); | |
397 e = el_bin(OPoror,TYvoid,e,ea); | |
398 e = el_bin(OPcomma, TYnptr, e, n); | |
399 } | |
400 e = el_una(OPind, type.totym(), e); | |
401 if (tybasic(e.Ety) == TYstruct) | |
402 e.Enumbytes = cast(uint)type.size(); | |
403 } | |
404 else | |
405 { | |
79 | 406 elem* einit = resolveLengthVar(lengthVar, &n1, t1); |
407 elem* n2 = e2.toElem(irs); | |
0 | 408 |
409 if (global.params.useArrayBounds) | |
410 { | |
411 elem* elength; | |
412 elem* n2x; | |
413 elem* ea; | |
414 | |
415 if (t1.ty == Tsarray) | |
416 { | |
417 TypeSArray tsa = cast(TypeSArray)t1; | |
418 ulong length = tsa.dim.toInteger(); | |
419 | |
420 elength = el_long(TYuint, length); | |
421 goto L1; | |
422 } | |
423 else if (t1.ty == Tarray) | |
424 { | |
425 elength = n1; | |
426 n1 = el_same(&elength); | |
427 elength = el_una(OP64_32, TYuint, elength); | |
428 L1: | |
429 n2x = n2; | |
430 n2 = el_same(&n2x); | |
431 n2x = el_bin(OPlt, TYint, n2x, elength); | |
432 | |
433 // Construct: (n2x || ModuleAssert(line)) | |
434 Symbol* sassert; | |
435 | |
436 sassert = irs.blx.module_.toModuleArray(); | |
437 ea = el_bin(OPcall,TYvoid,el_var(sassert), | |
438 el_long(TYint, loc.linnum)); | |
439 eb = el_bin(OPoror,TYvoid,n2x,ea); | |
440 } | |
441 } | |
442 | |
443 n1 = array_toPtr(t1, n1); | |
444 | |
445 { | |
446 elem* escale; | |
447 | |
448 escale = el_long(TYint, t1.nextOf().size()); | |
449 n2 = el_bin(OPmul, TYint, n2, escale); | |
450 e = el_bin(OPadd, TYnptr, n1, n2); | |
451 e = el_una(OPind, type.totym(), e); | |
452 if (tybasic(e.Ety) == TYstruct || tybasic(e.Ety) == TYarray) | |
453 { | |
454 e.Ety = TYstruct; | |
455 e.Enumbytes = cast(uint)type.size(); | |
456 } | |
457 } | |
458 | |
459 eb = el_combine(einit, eb); | |
460 e = el_combine(eb, e); | |
461 } | |
462 | |
463 el_setLoc(e,loc); | |
464 | |
465 return e; | |
466 } | |
467 } | |
468 |