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