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