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