Mercurial > projects > ddmd
annotate dmd/expression/Index.d @ 191:52188e7e3fb5
Fixed deprecated features, now compiles with DMD2.058
Also changed Array allocation policy:
Now doesn't reallocate but malloc's, followed by a memcpy (no free).
(this fixes a crash while compiling druntime. Same bug in dmd)
author | korDen@korDen-pc |
---|---|
date | Sun, 25 Mar 2012 03:11:12 +0400 |
parents | e28b18c23469 |
children |
rev | line source |
---|---|
0 | 1 module dmd.expression.Index; |
2 | |
114 | 3 import dmd.common; |
0 | 4 import dmd.Type; |
5 import dmd.Loc; | |
6 import dmd.StringExp; | |
7 import dmd.TOK; | |
8 import dmd.Expression; | |
9 import dmd.GlobalExpressions; | |
10 import dmd.IntegerExp; | |
11 import dmd.TY; | |
12 import dmd.TypeSArray; | |
13 import dmd.ArrayLiteralExp; | |
14 import dmd.AssocArrayLiteralExp; | |
15 | |
16 import dmd.expression.Equal; | |
17 | |
18 /* Also return EXP_CANT_INTERPRET if this fails | |
19 */ | |
20 Expression Index(Type type, Expression e1, Expression e2) | |
21 { | |
22 Expression e = EXP_CANT_INTERPRET; | |
23 Loc loc = e1.loc; | |
24 | |
25 //printf("Index(e1 = %s, e2 = %s)\n", e1.toChars(), e2.toChars()); | |
26 assert(e1.type); | |
27 if (e1.op == TOKstring && e2.op == TOKint64) | |
28 { | |
29 StringExp es1 = cast(StringExp)e1; | |
30 ulong i = e2.toInteger(); | |
31 | |
32 if (i >= es1.len) | |
33 e1.error("string index %ju is out of bounds [0 .. %zu]", i, es1.len); | |
34 else | |
35 { | |
36 uint value = es1.charAt(cast(uint)i); | |
37 e = new IntegerExp(loc, value, type); | |
38 } | |
39 } | |
40 else if (e1.type.toBasetype().ty == Tsarray && e2.op == TOKint64) | |
41 { | |
42 TypeSArray tsa = cast(TypeSArray)e1.type.toBasetype(); | |
43 ulong length = tsa.dim.toInteger(); | |
44 ulong i = e2.toInteger(); | |
45 | |
46 if (i >= length) | |
47 { | |
48 e2.error("array index %ju is out of bounds %s[0 .. %ju]", i, e1.toChars(), length); | |
49 } | |
50 else if (e1.op == TOKarrayliteral && !e1.checkSideEffect(2)) | |
51 { | |
90
39648eb578f6
more Expressions work
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
0
diff
changeset
|
52 auto ale = cast(ArrayLiteralExp)e1; |
39648eb578f6
more Expressions work
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
0
diff
changeset
|
53 e = ale.elements[cast(uint)i]; |
0 | 54 e.type = type; |
55 } | |
56 } | |
57 else if (e1.type.toBasetype().ty == Tarray && e2.op == TOKint64) | |
58 { | |
59 ulong i = e2.toInteger(); | |
60 | |
61 if (e1.op == TOKarrayliteral && !e1.checkSideEffect(2)) | |
62 { | |
90
39648eb578f6
more Expressions work
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
0
diff
changeset
|
63 auto ale = cast(ArrayLiteralExp)e1; |
0 | 64 if (i >= ale.elements.dim) |
65 { | |
66 e2.error("array index %ju is out of bounds %s[0 .. %u]", i, e1.toChars(), ale.elements.dim); | |
67 } | |
68 else | |
69 { | |
90
39648eb578f6
more Expressions work
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
0
diff
changeset
|
70 e = ale.elements[cast(uint)i]; |
0 | 71 e.type = type; |
72 } | |
73 } | |
74 } | |
75 else if (e1.op == TOKassocarrayliteral && !e1.checkSideEffect(2)) | |
76 { | |
77 AssocArrayLiteralExp ae = cast(AssocArrayLiteralExp)e1; | |
78 /* Search the keys backwards, in case there are duplicate keys | |
79 */ | |
80 for (size_t i = ae.keys.dim; i;) | |
81 { | |
82 i--; | |
90
39648eb578f6
more Expressions work
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
0
diff
changeset
|
83 auto ekey = ae.keys[i]; |
0 | 84 Expression ex = Equal(TOKequal, Type.tbool, ekey, e2); |
85 if (ex is EXP_CANT_INTERPRET) | |
86 return ex; | |
87 if (ex.isBool(true)) | |
88 { | |
90
39648eb578f6
more Expressions work
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
0
diff
changeset
|
89 e = ae.values[i]; |
0 | 90 e.type = type; |
91 break; | |
92 } | |
93 } | |
94 } | |
95 | |
96 return e; | |
97 } |