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