annotate dmd/IndexExp.d @ 73:ef02e2e203c2

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