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