Mercurial > projects > ddmd
annotate dmd/ArrayLiteralExp.d @ 72:2e2a5c3f943a
reduced warnings by adding override to the methods
think this also normalizes different line endings used all over the place
author | Trass3r |
---|---|
date | Sat, 28 Aug 2010 16:19:48 +0200 |
parents | ee3a9f34dc48 |
children | be2ab491772e |
rev | line source |
---|---|
72 | 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; | |
0 | 10 import dmd.WANT; |
72 | 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; | |
0 | 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; | |
72 | 28 import dmd.backend.OPER; |
68
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; |
72 | 31 import dmd.backend.mTY; |
32 import dmd.codegen.Util; | |
33 | |
0 | 34 class ArrayLiteralExp : Expression |
35 { | |
36 Expressions elements; | |
37 | |
38 this(Loc loc, Expressions elements) | |
72 | 39 { |
0 | 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 | |
72 | 51 override Expression syntaxCopy() |
0 | 52 { |
53 return new ArrayLiteralExp(loc, arraySyntaxCopy(elements)); | |
54 } | |
55 | |
72 | 56 override Expression semantic(Scope sc) |
0 | 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 | |
72 | 119 override bool isBool(bool result) |
0 | 120 { |
121 size_t dim = elements ? elements.dim : 0; | |
122 return result ? (dim != 0) : (dim == 0); | |
123 } | |
124 | |
72 | 125 override elem* toElem(IRState* irs) |
0 | 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 | |
72 | 191 override bool checkSideEffect(int flag) |
0 | 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 | |
72 | 206 override void toCBuffer(OutBuffer buf, HdrGenState* hgs) |
0 | 207 { |
208 buf.writeByte('['); | |
209 argsToCBuffer(buf, elements, hgs); | |
210 buf.writeByte(']'); | |
211 } | |
212 | |
72 | 213 override void toMangleBuffer(OutBuffer buf) |
0 | 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 | |
72 | 224 override void scanForNestedRef(Scope sc) |
0 | 225 { |
226 assert(false); | |
227 } | |
228 | |
72 | 229 override Expression optimize(int result) |
0 | 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 | |
72 | 245 override Expression interpret(InterState istate) |
0 | 246 { |
247 assert(false); | |
248 } | |
249 | |
72 | 250 override MATCH implicitConvTo(Type t) |
0 | 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 | |
72 | 285 override Expression castTo(Scope sc, Type t) |
0 | 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 | |
72 | 332 override dt_t** toDt(dt_t** pdt) |
0 | 333 { |
72 | 334 //printf("ArrayLiteralExp.toDt() '%s', type = %s\n", toChars(), type.toChars()); |
335 | |
336 dt_t *d; | |
337 dt_t **pdtend; | |
338 | |
339 d = null; | |
340 pdtend = &d; | |
341 for (int i = 0; i < elements.dim; i++) | |
342 { | |
343 Expression e = cast(Expression)elements.data[i]; | |
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 |