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