72
|
1 module dmd.StructLiteralExp;
|
|
2
|
|
3 import dmd.Expression;
|
|
4 import dmd.MOD;
|
|
5 import dmd.TypeStruct;
|
|
6 import dmd.TypeSArray;
|
|
7 import dmd.expression.Util;
|
|
8 import dmd.ErrorExp;
|
|
9 import dmd.Dsymbol;
|
|
10 import dmd.VarDeclaration;
|
|
11 import dmd.StructDeclaration;
|
|
12 import dmd.FuncDeclaration;
|
|
13 import dmd.ThisDeclaration;
|
|
14 import dmd.backend.elem;
|
|
15 import dmd.InterState;
|
|
16 import dmd.MATCH;
|
|
17 import dmd.WANT;
|
|
18 import dmd.TY;
|
|
19 import dmd.Type;
|
|
20 import dmd.OutBuffer;
|
|
21 import dmd.Loc;
|
|
22 import dmd.Scope;
|
|
23 import dmd.InlineCostState;
|
|
24 import dmd.IRState;
|
|
25 import dmd.InlineDoState;
|
|
26 import dmd.backend.Symbol;
|
|
27 import dmd.HdrGenState;
|
|
28 import dmd.backend.dt_t;
|
|
29 import dmd.InlineScanState;
|
79
|
30 import dmd.ArrayLiteralExp;
|
72
|
31 import dmd.ArrayTypes;
|
|
32 import dmd.TOK;
|
|
33
|
|
34 import dmd.codegen.Util;
|
|
35 import dmd.backend.Util;
|
|
36 import dmd.backend.RTLSYM;
|
|
37 import dmd.backend.TYM;
|
|
38 import dmd.backend.mTY;
|
|
39 import dmd.backend.OPER;
|
|
40
|
|
41
|
0
|
42 class StructLiteralExp : Expression
|
|
43 {
|
72
|
44 StructDeclaration sd; // which aggregate this is for
|
79
|
45 Expressions elements; // parallels sd.fields[] with
|
72
|
46 // NULL entries for fields to skip
|
|
47
|
|
48 Symbol* sym; // back end symbol to initialize with literal
|
|
49 size_t soffset; // offset from start of s
|
0
|
50 int fillHoles; // fill alignment 'holes' with zero
|
|
51
|
|
52 this(Loc loc, StructDeclaration sd, Expressions elements)
|
|
53 {
|
72
|
54 super(loc, TOKstructliteral, StructLiteralExp.sizeof);
|
|
55 this.sd = sd;
|
|
56 this.elements = elements;
|
|
57 this.sym = null;
|
|
58 this.soffset = 0;
|
|
59 this.fillHoles = 1;
|
0
|
60 }
|
|
61
|
72
|
62 override Expression syntaxCopy()
|
0
|
63 {
|
|
64 assert(false);
|
|
65 }
|
|
66
|
72
|
67 override Expression semantic(Scope sc)
|
0
|
68 {
|
72
|
69 Expression e;
|
|
70 int nfields = sd.fields.dim - sd.isnested;
|
|
71
|
|
72 version (LOGSEMANTIC) {
|
|
73 printf("StructLiteralExp.semantic('%s')\n", toChars());
|
|
74 }
|
|
75 if (type)
|
|
76 return this;
|
|
77
|
|
78 // Run semantic() on each element
|
|
79 for (size_t i = 0; i < elements.dim; i++)
|
|
80 {
|
|
81 e = cast(Expression)elements.data[i];
|
|
82 if (!e)
|
|
83 continue;
|
|
84 e = e.semantic(sc);
|
|
85 elements.data[i] = cast(void*)e;
|
|
86 }
|
|
87 expandTuples(elements);
|
|
88 size_t offset = 0;
|
|
89 for (size_t i = 0; i < elements.dim; i++)
|
|
90 {
|
|
91 e = cast(Expression)elements.data[i];
|
|
92 if (!e)
|
|
93 continue;
|
|
94
|
|
95 if (!e.type)
|
|
96 error("%s has no value", e.toChars());
|
|
97 e = resolveProperties(sc, e);
|
|
98 if (i >= nfields)
|
|
99 {
|
|
100 error("more initializers than fields of %s", sd.toChars());
|
|
101 return new ErrorExp();
|
|
102 }
|
79
|
103 Dsymbol s = sd.fields[i];
|
72
|
104 VarDeclaration v = s.isVarDeclaration();
|
|
105 assert(v);
|
|
106 if (v.offset < offset)
|
|
107 error("overlapping initialization for %s", v.toChars());
|
|
108 offset = v.offset + cast(uint)v.type.size();
|
|
109
|
|
110 Type telem = v.type;
|
|
111 while (!e.implicitConvTo(telem) && telem.toBasetype().ty == Tsarray)
|
|
112 {
|
|
113 /* Static array initialization, as in:
|
|
114 * T[3][5] = e;
|
|
115 */
|
|
116 telem = telem.toBasetype().nextOf();
|
|
117 }
|
|
118
|
|
119 e = e.implicitCastTo(sc, telem);
|
|
120
|
|
121 elements.data[i] = cast(void*)e;
|
|
122 }
|
|
123
|
|
124 /* Fill out remainder of elements[] with default initializers for fields[]
|
|
125 */
|
|
126 for (size_t i = elements.dim; i < nfields; i++)
|
|
127 {
|
79
|
128 Dsymbol s = sd.fields[i];
|
72
|
129 VarDeclaration v = s.isVarDeclaration();
|
|
130 assert(v);
|
|
131 assert(!v.isThisDeclaration());
|
|
132
|
|
133 if (v.offset < offset)
|
|
134 {
|
|
135 e = null;
|
|
136 sd.hasUnions = 1;
|
|
137 }
|
|
138 else
|
|
139 {
|
|
140 if (v.init)
|
|
141 {
|
|
142 e = v.init.toExpression();
|
|
143 if (!e)
|
|
144 error("cannot make expression out of initializer for %s", v.toChars());
|
|
145 }
|
|
146 else
|
|
147 {
|
|
148 e = v.type.defaultInit(Loc(0));
|
|
149 e.loc = loc;
|
|
150 }
|
|
151 offset = v.offset + cast(uint)v.type.size();
|
|
152 }
|
|
153 elements.push(cast(void*)e);
|
|
154 }
|
|
155
|
|
156 type = sd.type;
|
63
|
157 return this;
|
0
|
158 }
|
|
159
|
|
160 Expression getField(Type type, uint offset)
|
|
161 {
|
79
|
162 //printf("StructLiteralExp.getField(this = %s, type = %s, offset = %u)\n",
|
|
163 // /*toChars()*/"", type.toChars(), offset);
|
|
164 Expression e = null;
|
|
165 int i = getFieldIndex(type, offset);
|
|
166
|
|
167 if (i != -1)
|
|
168 {
|
|
169 //printf("\ti = %d\n", i);
|
|
170 assert(i < elements.dim);
|
|
171 e = cast(Expression)elements.data[i];
|
|
172 if (e)
|
|
173 {
|
|
174 //writef("e = %s, e.type = %s\n", e.toChars(), e.type.toChars());
|
|
175
|
|
176 /* If type is a static array, and e is an initializer for that array,
|
|
177 * then the field initializer should be an array literal of e.
|
|
178 */
|
|
179 if (e.type != type && type.ty == Tsarray)
|
|
180 {
|
|
181 TypeSArray tsa = cast(TypeSArray)type;
|
|
182 size_t length = cast(size_t) tsa.dim.toInteger();
|
|
183 Expressions z = new Expressions;
|
|
184 z.setDim(length);
|
|
185 for (int q = 0; q < length; ++q)
|
|
186 z.data[q] = cast(void*) e.copy();
|
|
187 e = new ArrayLiteralExp(loc, z);
|
|
188 e.type = type;
|
|
189 }
|
|
190 else
|
|
191 {
|
|
192 e = e.copy();
|
|
193 e.type = type;
|
|
194 }
|
|
195 }
|
|
196 }
|
|
197 return e;
|
0
|
198 }
|
|
199
|
|
200 int getFieldIndex(Type type, uint offset)
|
|
201 {
|
79
|
202 /* Find which field offset is by looking at the field offsets
|
|
203 */
|
|
204 if (elements.dim)
|
|
205 {
|
|
206 foreach (size_t i, Dsymbol s; sd.fields)
|
|
207 {
|
|
208 VarDeclaration v = s.isVarDeclaration();
|
|
209 assert(v);
|
|
210
|
|
211 if (offset == v.offset && type.size() == v.type.size())
|
|
212 {
|
|
213 Expression e = cast(Expression)elements.data[i];
|
|
214 if (e)
|
|
215 {
|
|
216 return i;
|
|
217 }
|
|
218 break;
|
|
219 }
|
|
220 }
|
|
221 }
|
|
222 return -1;
|
0
|
223 }
|
|
224
|
72
|
225 override elem* toElem(IRState* irs)
|
0
|
226 {
|
72
|
227 elem* e;
|
|
228 size_t dim;
|
|
229
|
|
230 //printf("StructLiteralExp.toElem() %s\n", toChars());
|
|
231
|
|
232 // struct symbol to initialize with the literal
|
|
233 Symbol* stmp = sym ? sym : symbol_genauto(sd.type.toCtype());
|
|
234
|
|
235 e = null;
|
|
236
|
|
237 if (fillHoles)
|
|
238 {
|
|
239 /* Initialize all alignment 'holes' to zero.
|
|
240 * Do before initializing fields, as the hole filling process
|
|
241 * can spill over into the fields.
|
|
242 */
|
|
243 size_t offset = 0;
|
79
|
244 foreach (Dsymbol s; sd.fields)
|
72
|
245 {
|
|
246 VarDeclaration v = s.isVarDeclaration();
|
|
247 assert(v);
|
|
248
|
|
249 e = el_combine(e, fillHole(stmp, &offset, v.offset, sd.structsize));
|
|
250 size_t vend = v.offset + cast(uint)v.type.size();
|
|
251 if (offset < vend)
|
|
252 offset = vend;
|
|
253 }
|
|
254 e = el_combine(e, fillHole(stmp, &offset, sd.structsize, sd.structsize));
|
|
255 }
|
|
256
|
|
257 if (elements)
|
|
258 {
|
|
259 dim = elements.dim;
|
|
260 assert(dim <= sd.fields.dim);
|
|
261 for (size_t i = 0; i < dim; i++)
|
|
262 {
|
|
263 Expression el = cast(Expression)elements.data[i];
|
|
264 if (!el)
|
|
265 continue;
|
|
266
|
79
|
267 Dsymbol s = sd.fields[i];
|
72
|
268 VarDeclaration v = s.isVarDeclaration();
|
|
269 assert(v);
|
|
270 assert(!v.isThisDeclaration());
|
|
271
|
|
272 elem* e1;
|
|
273 if (tybasic(stmp.Stype.Tty) == TYnptr)
|
|
274 {
|
|
275 e1 = el_var(stmp);
|
|
276 e1.EV.sp.Voffset = soffset;
|
|
277 }
|
|
278 else
|
|
279 {
|
|
280 e1 = el_ptr(stmp);
|
|
281 if (soffset)
|
|
282 e1 = el_bin(OPadd, TYnptr, e1, el_long(TYsize_t, soffset));
|
|
283 }
|
|
284 e1 = el_bin(OPadd, TYnptr, e1, el_long(TYsize_t, v.offset));
|
|
285 elem* ec = e1; // pointer to destination
|
|
286
|
|
287 elem* ep = el.toElem(irs);
|
|
288
|
|
289 Type t1b = v.type.toBasetype();
|
|
290 Type t2b = el.type.toBasetype();
|
|
291 if (t1b.ty == Tsarray)
|
|
292 {
|
|
293 if (t2b.implicitConvTo(t1b))
|
|
294 {
|
|
295 ///version (DMDV2) {
|
|
296 // Determine if postblit is needed
|
|
297 int postblit = 0;
|
|
298 if (needsPostblit(t1b))
|
|
299 postblit = 1;
|
|
300
|
|
301 if (postblit)
|
|
302 {
|
|
303 /* Generate:
|
|
304 * _d_arrayctor(ti, From: ep, To: e1)
|
|
305 */
|
|
306 Expression ti = t1b.nextOf().toBasetype().getTypeInfo(null);
|
|
307 elem* esize = el_long(TYsize_t, (cast(TypeSArray)t1b).dim.toInteger());
|
|
308 e1 = el_pair(TYdarray, esize, e1);
|
|
309 ep = el_pair(TYdarray, el_copytree(esize), array_toPtr(el.type, ep));
|
|
310 ep = el_params(e1, ep, ti.toElem(irs), null);
|
|
311 int rtl = RTLSYM_ARRAYCTOR;
|
|
312 e1 = el_bin(OPcall, type.totym(), el_var(rtlsym[rtl]), ep);
|
|
313 }
|
|
314 else
|
|
315 ///}
|
|
316 {
|
|
317 elem* esize = el_long(TYsize_t, t1b.size());
|
|
318 ep = array_toPtr(el.type, ep);
|
|
319 e1 = el_bin(OPmemcpy, TYnptr, e1, el_param(ep, esize));
|
|
320 }
|
|
321 }
|
|
322 else
|
|
323 {
|
|
324 elem* edim = el_long(TYsize_t, t1b.size() / t2b.size());
|
|
325 e1 = setArray(e1, edim, t2b, ep, irs, TOKconstruct);
|
|
326 }
|
|
327 }
|
|
328 else
|
|
329 {
|
|
330 tym_t ty = v.type.totym();
|
|
331 e1 = el_una(OPind, ty, e1);
|
|
332 if (tybasic(ty) == TYstruct)
|
|
333 e1.Enumbytes = cast(uint)v.type.size();
|
|
334 e1 = el_bin(OPeq, ty, e1, ep);
|
|
335 if (tybasic(ty) == TYstruct)
|
|
336 {
|
|
337 e1.Eoper = OPstreq;
|
|
338 e1.Enumbytes = cast(uint)v.type.size();
|
|
339 }
|
|
340 version (DMDV2) {
|
|
341 /* Call postblit() on e1
|
|
342 */
|
|
343 StructDeclaration sd = needsPostblit(v.type);
|
|
344 if (sd)
|
|
345 {
|
|
346 FuncDeclaration fd = sd.postblit;
|
|
347 ec = el_copytree(ec);
|
|
348 ec = callfunc(loc, irs, 1, Type.tvoid, ec, sd.type.pointerTo(), fd, fd.type, null, null);
|
|
349 e1 = el_bin(OPcomma, ec.Ety, e1, ec);
|
|
350 }
|
|
351 }
|
|
352 }
|
|
353 e = el_combine(e, e1);
|
|
354 }
|
|
355 }
|
|
356
|
79
|
357 version (DMDV2)
|
|
358 {
|
72
|
359 if (sd.isnested)
|
|
360 { // Initialize the hidden 'this' pointer
|
|
361 assert(sd.fields.dim);
|
79
|
362 Dsymbol s = sd.fields[sd.fields.dim - 1];
|
72
|
363 ThisDeclaration v = s.isThisDeclaration();
|
|
364 assert(v);
|
|
365
|
|
366 elem* e1;
|
|
367 if (tybasic(stmp.Stype.Tty) == TYnptr)
|
|
368 {
|
|
369 e1 = el_var(stmp);
|
|
370 e1.EV.sp.Voffset = soffset;
|
|
371 }
|
|
372 else
|
|
373 {
|
|
374 e1 = el_ptr(stmp);
|
|
375 if (soffset)
|
|
376 e1 = el_bin(OPadd, TYnptr, e1, el_long(TYsize_t, soffset));
|
|
377 }
|
|
378 e1 = el_bin(OPadd, TYnptr, e1, el_long(TYsize_t, v.offset));
|
|
379 e1 = setEthis(loc, irs, e1, sd);
|
|
380
|
|
381 e = el_combine(e, e1);
|
|
382 }
|
|
383 }
|
|
384
|
|
385 elem* ev = el_var(stmp);
|
|
386 ev.Enumbytes = sd.structsize;
|
|
387 e = el_combine(e, ev);
|
|
388 el_setLoc(e,loc);
|
67
|
389 return e;
|
0
|
390 }
|
|
391
|
72
|
392 override bool checkSideEffect(int flag)
|
0
|
393 {
|
79
|
394 bool f = 0;
|
|
395
|
|
396 for (size_t i = 0; i < elements.dim; i++)
|
|
397 {
|
|
398 Expression e = cast(Expression)elements.data[i];
|
|
399 if (!e)
|
|
400 continue;
|
|
401
|
|
402 f |= e.checkSideEffect(2);
|
|
403 }
|
|
404 if (flag == 0 && f == 0)
|
|
405 Expression.checkSideEffect(0);
|
|
406 return f;
|
0
|
407 }
|
|
408
|
72
|
409 override void toCBuffer(OutBuffer buf, HdrGenState* hgs)
|
0
|
410 {
|
79
|
411 buf.writestring(sd.toChars());
|
|
412 buf.writeByte('(');
|
|
413 argsToCBuffer(buf, elements, hgs);
|
|
414 buf.writeByte(')');
|
0
|
415 }
|
|
416
|
72
|
417 override void toMangleBuffer(OutBuffer buf)
|
0
|
418 {
|
79
|
419 size_t dim = elements ? elements.dim : 0;
|
|
420 buf.printf("S%u", dim);
|
|
421 for (size_t i = 0; i < dim; i++)
|
|
422 {
|
|
423 Expression e = cast(Expression)elements.data[i];
|
|
424 if (e)
|
|
425 e.toMangleBuffer(buf);
|
|
426 else
|
|
427 buf.writeByte('v'); // 'v' for void
|
|
428 }
|
0
|
429 }
|
|
430
|
72
|
431 override void scanForNestedRef(Scope sc)
|
0
|
432 {
|
|
433 assert(false);
|
|
434 }
|
|
435
|
72
|
436 override Expression optimize(int result)
|
0
|
437 {
|
72
|
438 if (elements)
|
|
439 {
|
|
440 for (size_t i = 0; i < elements.dim; i++)
|
|
441 {
|
|
442 Expression e = cast(Expression)elements.data[i];
|
|
443 if (!e)
|
|
444 continue;
|
|
445 e = e.optimize(WANTvalue | (result & WANTinterpret));
|
|
446 elements.data[i] = cast(void*)e;
|
|
447 }
|
|
448 }
|
56
|
449 return this;
|
0
|
450 }
|
|
451
|
72
|
452 override Expression interpret(InterState istate)
|
0
|
453 {
|
|
454 assert(false);
|
|
455 }
|
|
456
|
72
|
457 override dt_t** toDt(dt_t** pdt)
|
0
|
458 {
|
|
459 assert(false);
|
|
460 }
|
|
461
|
79
|
462 version(DMDV2)
|
|
463 {
|
72
|
464 override int isLvalue()
|
0
|
465 {
|
79
|
466 return 1;
|
0
|
467 }
|
79
|
468 }
|
0
|
469
|
72
|
470 override Expression toLvalue(Scope sc, Expression e)
|
0
|
471 {
|
79
|
472 return this;
|
0
|
473 }
|
|
474
|
79
|
475 version(DMDV2)
|
|
476 {
|
72
|
477 override bool canThrow()
|
0
|
478 {
|
64
|
479 return arrayExpressionCanThrow(elements);
|
0
|
480 }
|
79
|
481 }
|
0
|
482
|
72
|
483 override MATCH implicitConvTo(Type t)
|
0
|
484 {
|
72
|
485 static if (false) {
|
|
486 printf("StructLiteralExp.implicitConvTo(this=%.*s, type=%.*s, t=%.*s)\n",
|
|
487 toChars(), type.toChars(), t.toChars());
|
|
488 }
|
|
489 MATCH m = Expression.implicitConvTo(t);
|
|
490 if (m != MATCHnomatch)
|
|
491 return m;
|
|
492 if (type.ty == t.ty && type.ty == Tstruct && (cast(TypeStruct)type).sym == (cast(TypeStruct)t).sym)
|
|
493 {
|
|
494 m = MATCHconst;
|
|
495 for (int i = 0; i < elements.dim; i++)
|
|
496 {
|
|
497 Expression e = cast(Expression)elements.data[i];
|
|
498 Type te = e.type;
|
|
499 if (t.mod == 0)
|
|
500 te = te.mutableOf();
|
|
501 else
|
|
502 {
|
|
503 assert(t.mod == MODinvariant);
|
|
504 te = te.invariantOf();
|
|
505 }
|
|
506 MATCH m2 = e.implicitConvTo(te);
|
|
507 //printf("\t%s => %s, match = %d\n", e.toChars(), te.toChars(), m2);
|
|
508 if (m2 < m)
|
|
509 m = m2;
|
|
510 }
|
|
511 }
|
64
|
512 return m;
|
0
|
513 }
|
|
514
|
72
|
515 override int inlineCost(InlineCostState* ics)
|
0
|
516 {
|
|
517 assert(false);
|
|
518 }
|
|
519
|
72
|
520 override Expression doInline(InlineDoState ids)
|
0
|
521 {
|
|
522 assert(false);
|
|
523 }
|
|
524
|
72
|
525 override Expression inlineScan(InlineScanState* iss)
|
0
|
526 {
|
|
527 assert(false);
|
|
528 }
|
|
529 }
|
|
530
|