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