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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1 module dmd.expression.Index;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
2
114
e28b18c23469 added a module dmd.common for commonly used stuff
Trass3r
parents: 90
diff changeset
3 import dmd.common;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
4 import dmd.Type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
5 import dmd.Loc;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
6 import dmd.StringExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
7 import dmd.TOK;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
8 import dmd.Expression;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
9 import dmd.GlobalExpressions;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
10 import dmd.IntegerExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
11 import dmd.TY;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
12 import dmd.TypeSArray;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
13 import dmd.ArrayLiteralExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
14 import dmd.AssocArrayLiteralExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
15
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
16 import dmd.expression.Equal;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
17
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
18 /* Also return EXP_CANT_INTERPRET if this fails
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
19 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
20 Expression Index(Type type, Expression e1, Expression e2)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
21 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
22 Expression e = EXP_CANT_INTERPRET;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
23 Loc loc = e1.loc;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
24
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
25 //printf("Index(e1 = %s, e2 = %s)\n", e1.toChars(), e2.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
26 assert(e1.type);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
27 if (e1.op == TOKstring && e2.op == TOKint64)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
28 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
29 StringExp es1 = cast(StringExp)e1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
30 ulong i = e2.toInteger();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
31
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
32 if (i >= es1.len)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
33 e1.error("string index %ju is out of bounds [0 .. %zu]", i, es1.len);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
34 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
35 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
36 uint value = es1.charAt(cast(uint)i);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
37 e = new IntegerExp(loc, value, type);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
38 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
39 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
40 else if (e1.type.toBasetype().ty == Tsarray && e2.op == TOKint64)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
41 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
42 TypeSArray tsa = cast(TypeSArray)e1.type.toBasetype();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
43 ulong length = tsa.dim.toInteger();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
44 ulong i = e2.toInteger();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
45
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
46 if (i >= length)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
47 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
48 e2.error("array index %ju is out of bounds %s[0 .. %ju]", i, e1.toChars(), length);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
49 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
50 else if (e1.op == TOKarrayliteral && !e1.checkSideEffect(2))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
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
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
54 e.type = type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
55 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
56 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
57 else if (e1.type.toBasetype().ty == Tarray && e2.op == TOKint64)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
58 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
59 ulong i = e2.toInteger();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
60
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
61 if (e1.op == TOKarrayliteral && !e1.checkSideEffect(2))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
62 {
90
39648eb578f6 more Expressions work
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 0
diff changeset
63 auto ale = cast(ArrayLiteralExp)e1;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
64 if (i >= ale.elements.dim)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
65 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
66 e2.error("array index %ju is out of bounds %s[0 .. %u]", i, e1.toChars(), ale.elements.dim);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
67 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
68 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
69 {
90
39648eb578f6 more Expressions work
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 0
diff changeset
70 e = ale.elements[cast(uint)i];
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
71 e.type = type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
72 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
73 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
74 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
75 else if (e1.op == TOKassocarrayliteral && !e1.checkSideEffect(2))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
76 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
77 AssocArrayLiteralExp ae = cast(AssocArrayLiteralExp)e1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
78 /* Search the keys backwards, in case there are duplicate keys
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
79 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
80 for (size_t i = ae.keys.dim; i;)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
81 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
82 i--;
90
39648eb578f6 more Expressions work
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 0
diff changeset
83 auto ekey = ae.keys[i];
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
84 Expression ex = Equal(TOKequal, Type.tbool, ekey, e2);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
85 if (ex is EXP_CANT_INTERPRET)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
86 return ex;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
87 if (ex.isBool(true))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
88 {
90
39648eb578f6 more Expressions work
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 0
diff changeset
89 e = ae.values[i];
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
90 e.type = type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
91 break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
92 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
93 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
94 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
95
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
96 return e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
97 }