comparison dmd/ArrayLiteralExp.d @ 0:10317f0c89a5

Initial commit
author korDen
date Sat, 24 Oct 2009 08:42:06 +0400
parents
children cab4c37afb89
comparison
equal deleted inserted replaced
-1:000000000000 0:10317f0c89a5
1 module dmd.ArrayLiteralExp;
2
3 import dmd.Expression;
4 import dmd.backend.elem;
5 import dmd.InterState;
6 import dmd.MATCH;
7 import dmd.Type;
8 import dmd.OutBuffer;
9 import dmd.Loc;
10 import dmd.WANT;
11 import dmd.Scope;
12 import dmd.InlineCostState;
13 import dmd.IRState;
14 import dmd.InlineDoState;
15 import dmd.HdrGenState;
16 import dmd.backend.dt_t;
17 import dmd.InlineScanState;
18 import dmd.ArrayTypes;
19 import dmd.TOK;
20 import dmd.IntegerExp;
21 import dmd.TypeSArray;
22 import dmd.TY;
23 import dmd.StringExp;
24
25 import dmd.expression.Util;
26 import dmd.backend.Util;
27 import dmd.backend.RTLSYM;
28 import dmd.backend.OPER;
29 import dmd.backend.TYM;
30 import dmd.backend.mTY;
31
32 class ArrayLiteralExp : Expression
33 {
34 Expressions elements;
35
36 this(Loc loc, Expressions elements)
37 {
38 super(loc, TOK.TOKarrayliteral, ArrayLiteralExp.sizeof);
39 this.elements = elements;
40 }
41
42 this(Loc loc, Expression e)
43 {
44 super(loc, TOK.TOKarrayliteral, ArrayLiteralExp.sizeof);
45 elements = new Expressions();
46 elements.push(cast(void*)e);
47 }
48
49 Expression syntaxCopy()
50 {
51 return new ArrayLiteralExp(loc, arraySyntaxCopy(elements));
52 }
53
54 Expression semantic(Scope sc)
55 {
56 Expression e;
57 Type t0 = null;
58
59 version (LOGSEMANTIC) {
60 printf("ArrayLiteralExp.semantic('%s')\n", toChars());
61 }
62 if (type)
63 return this;
64
65 // Run semantic() on each element
66 for (int i = 0; i < elements.dim; i++)
67 {
68 e = cast(Expression)elements.data[i];
69 e = e.semantic(sc);
70 assert(e.type);
71 elements.data[i] = cast(void*)e;
72 }
73
74 expandTuples(elements);
75
76 for (int i = 0; i < elements.dim; i++)
77 {
78 e = cast(Expression)elements.data[i];
79
80 if (!e.type)
81 error("%s has no value", e.toChars());
82
83 e = resolveProperties(sc, e);
84
85 ubyte committed = 1;
86 if (e.op == TOKstring)
87 committed = (cast(StringExp)e).committed;
88
89 if (!t0)
90 {
91 t0 = e.type;
92 // Convert any static arrays to dynamic arrays
93 if (t0.ty == Tsarray)
94 {
95 t0 = (cast(TypeSArray)t0).next.arrayOf();
96 e = e.implicitCastTo(sc, t0);
97 }
98 }
99 else
100 e = e.implicitCastTo(sc, t0);
101
102 if (!committed && e.op == TOKstring)
103 {
104 StringExp se = cast(StringExp)e;
105 se.committed = 0;
106 }
107 elements.data[i] = cast(void*)e;
108 }
109
110 if (!t0)
111 t0 = Type.tvoid;
112 type = new TypeSArray(t0, new IntegerExp(elements.dim));
113 type = type.semantic(loc, sc);
114 return this;
115 }
116
117 bool isBool(bool result)
118 {
119 size_t dim = elements ? elements.dim : 0;
120 return result ? (dim != 0) : (dim == 0);
121 }
122
123 elem* toElem(IRState* irs)
124 {
125 elem* e;
126 size_t dim;
127
128 //printf("ArrayLiteralExp.toElem() %s\n", toChars());
129 if (elements)
130 {
131 scope Expressions args = new Expressions();
132 dim = elements.dim;
133 args.setDim(dim + 1); // +1 for number of args parameter
134 e = el_long(TYint, dim);
135 args.data[dim] = cast(void*)e;
136 for (size_t i = 0; i < dim; i++)
137 {
138 Expression el = cast(Expression)elements.data[i];
139 elem* ep = el.toElem(irs);
140
141 if (tybasic(ep.Ety) == TYstruct || tybasic(ep.Ety) == TYarray)
142 {
143 ep = el_una(OPstrpar, TYstruct, ep);
144 ep.Enumbytes = cast(uint)el.type.size();
145 }
146 args.data[dim - (i + 1)] = cast(void *)ep;
147 }
148
149 /* Because the number of parameters can get very large, produce
150 * a balanced binary tree so we don't blow up the stack in
151 * the subsequent tree walking code.
152 */
153 e = el_params(args.data, dim + 1);
154 }
155 else
156 {
157 dim = 0;
158 e = el_long(TYint, 0);
159 }
160 Type tb = type.toBasetype();
161 static if (true) {
162 e = el_param(e, type.getTypeInfo(null).toElem(irs));
163
164 // call _d_arrayliteralT(ti, dim, ...)
165 e = el_bin(OPcall,TYnptr,el_var(rtlsym[RTLSYM_ARRAYLITERALT]),e);
166 } else {
167 e = el_param(e, el_long(TYint, tb.next.size()));
168
169 // call _d_arrayliteral(size, dim, ...)
170 e = el_bin(OPcall,TYnptr,el_var(rtlsym[RTLSYM_ARRAYLITERAL]),e);
171 }
172 if (tb.ty == Tarray)
173 {
174 e = el_pair(TYullong, el_long(TYint, dim), e);
175 }
176 else if (tb.ty == Tpointer)
177 {
178 }
179 else
180 {
181 e = el_una(OPind,TYstruct,e);
182 e.Enumbytes = cast(uint)type.size();
183 }
184
185 el_setLoc(e,loc);
186 return e;
187 }
188
189 bool checkSideEffect(int flag)
190 {
191 bool f = false;
192
193 for (size_t i = 0; i < elements.dim; i++)
194 {
195 Expression e = cast(Expression)elements.data[i];
196 f |= e.checkSideEffect(2);
197 }
198 if (flag == 0 && f == false)
199 Expression.checkSideEffect(0);
200
201 return f;
202 }
203
204 void toCBuffer(OutBuffer buf, HdrGenState* hgs)
205 {
206 buf.writeByte('[');
207 argsToCBuffer(buf, elements, hgs);
208 buf.writeByte(']');
209 }
210
211 void toMangleBuffer(OutBuffer buf)
212 {
213 size_t dim = elements ? elements.dim : 0;
214 buf.printf("A%u", dim);
215 for (size_t i = 0; i < dim; i++)
216 {
217 Expression e = cast(Expression)elements.data[i];
218 e.toMangleBuffer(buf);
219 }
220 }
221
222 void scanForNestedRef(Scope sc)
223 {
224 assert(false);
225 }
226
227 Expression optimize(int result)
228 {
229 if (elements)
230 {
231 for (size_t i = 0; i < elements.dim; i++)
232 {
233 Expression e = cast(Expression)elements.data[i];
234
235 e = e.optimize(WANTvalue | (result & WANTinterpret));
236 elements.data[i] = cast(void*)e;
237 }
238 }
239
240 return this;
241 }
242
243 Expression interpret(InterState* istate)
244 {
245 assert(false);
246 }
247
248 MATCH implicitConvTo(Type t)
249 {
250 MATCH result = MATCHexact;
251
252 static if (false) {
253 printf("ArrayLiteralExp.implicitConvTo(this=%s, type=%s, t=%s)\n",
254 toChars(), type.toChars(), t.toChars());
255 }
256 Type typeb = type.toBasetype();
257 Type tb = t.toBasetype();
258 if ((tb.ty == Tarray || tb.ty == Tsarray) &&
259 (typeb.ty == Tarray || typeb.ty == Tsarray))
260 {
261 if (tb.ty == Tsarray)
262 {
263 TypeSArray tsa = cast(TypeSArray)tb;
264 if (elements.dim != tsa.dim.toInteger())
265 result = MATCHnomatch;
266 }
267
268 for (int i = 0; i < elements.dim; i++)
269 {
270 Expression e = cast(Expression)elements.data[i];
271 MATCH m = cast(MATCH)e.implicitConvTo(tb.nextOf());
272 if (m < result)
273 result = m; // remember worst match
274 if (result == MATCHnomatch)
275 break; // no need to check for worse
276 }
277 return result;
278 }
279 else
280 return Expression.implicitConvTo(t);
281 }
282
283 Expression castTo(Scope sc, Type t)
284 {
285 static if (false) {
286 printf("ArrayLiteralExp.castTo(this=%s, type=%s, => %s)\n",
287 toChars(), type.toChars(), t.toChars());
288 }
289 if (type == t)
290 return this;
291 ArrayLiteralExp e = this;
292 Type typeb = type.toBasetype();
293 Type tb = t.toBasetype();
294 if ((tb.ty == Tarray || tb.ty == Tsarray) &&
295 (typeb.ty == Tarray || typeb.ty == Tsarray) &&
296 // Not trying to convert non-void[] to void[]
297 !(tb.nextOf().toBasetype().ty == Tvoid && typeb.nextOf().toBasetype().ty != Tvoid))
298 {
299 if (tb.ty == Tsarray)
300 {
301 TypeSArray tsa = cast(TypeSArray)tb;
302 if (elements.dim != tsa.dim.toInteger())
303 goto L1;
304 }
305
306 e = cast(ArrayLiteralExp)copy();
307 e.elements = cast(Expressions)elements.copy();
308 for (int i = 0; i < elements.dim; i++)
309 {
310 Expression ex = cast(Expression)elements.data[i];
311 ex = ex.castTo(sc, tb.nextOf());
312 e.elements.data[i] = cast(void*)ex;
313 }
314 e.type = t;
315 return e;
316 }
317 if (tb.ty == Tpointer && typeb.ty == Tsarray)
318 {
319 Type tp = typeb.nextOf().pointerTo();
320 if (!tp.equals(e.type))
321 {
322 e = cast(ArrayLiteralExp)copy();
323 e.type = tp;
324 }
325 }
326 L1:
327 return e.Expression.castTo(sc, t);
328 }
329
330 dt_t** toDt(dt_t** pdt)
331 {
332 assert(false);
333 }
334
335 version (DMDV2) {
336 bool canThrow()
337 {
338 return 1; // because it can fail allocating memory
339 }
340 }
341 int inlineCost(InlineCostState* ics)
342 {
343 return 1 + arrayInlineCost(ics, elements);
344 }
345
346 Expression doInline(InlineDoState ids)
347 {
348 ArrayLiteralExp ce = cast(ArrayLiteralExp)copy();
349 ce.elements = arrayExpressiondoInline(elements, ids);
350 return ce;
351 }
352
353 Expression inlineScan(InlineScanState* iss)
354 {
355 Expression e = this;
356
357 //printf("ArrayLiteralExp.inlineScan()\n");
358 arrayInlineScan(iss, elements);
359
360 return e;
361 }
362 }
363