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