0
|
1 module dmd.StructLiteralExp;
|
|
2
|
|
3 import dmd.Expression;
|
64
|
4 import dmd.MOD;
|
|
5 import dmd.TypeStruct;
|
63
|
6 import dmd.expression.Util;
|
|
7 import dmd.ErrorExp;
|
|
8 import dmd.Dsymbol;
|
|
9 import dmd.VarDeclaration;
|
0
|
10 import dmd.StructDeclaration;
|
|
11 import dmd.backend.elem;
|
|
12 import dmd.InterState;
|
|
13 import dmd.MATCH;
|
56
|
14 import dmd.WANT;
|
63
|
15 import dmd.TY;
|
0
|
16 import dmd.Type;
|
|
17 import dmd.OutBuffer;
|
|
18 import dmd.Loc;
|
|
19 import dmd.Scope;
|
|
20 import dmd.InlineCostState;
|
|
21 import dmd.IRState;
|
|
22 import dmd.InlineDoState;
|
|
23 import dmd.backend.Symbol;
|
|
24 import dmd.HdrGenState;
|
|
25 import dmd.backend.dt_t;
|
|
26 import dmd.InlineScanState;
|
|
27 import dmd.ArrayTypes;
|
|
28 import dmd.TOK;
|
|
29
|
|
30
|
|
31 class StructLiteralExp : Expression
|
|
32 {
|
|
33 StructDeclaration sd; // which aggregate this is for
|
|
34 Expressions elements; // parallels sd->fields[] with
|
|
35 // NULL entries for fields to skip
|
|
36
|
|
37 Symbol* sym; // back end symbol to initialize with literal
|
|
38 size_t soffset; // offset from start of s
|
|
39 int fillHoles; // fill alignment 'holes' with zero
|
|
40
|
|
41 this(Loc loc, StructDeclaration sd, Expressions elements)
|
|
42 {
|
56
|
43 super(loc, TOKstructliteral, StructLiteralExp.sizeof);
|
|
44 this.sd = sd;
|
|
45 this.elements = elements;
|
|
46 this.sym = null;
|
|
47 this.soffset = 0;
|
|
48 this.fillHoles = 1;
|
0
|
49 }
|
|
50
|
|
51 Expression syntaxCopy()
|
|
52 {
|
|
53 assert(false);
|
|
54 }
|
|
55
|
|
56 Expression semantic(Scope sc)
|
|
57 {
|
63
|
58 Expression e;
|
|
59 int nfields = sd.fields.dim - sd.isnested;
|
|
60
|
|
61 version (LOGSEMANTIC) {
|
|
62 printf("StructLiteralExp.semantic('%s')\n", toChars());
|
|
63 }
|
|
64 if (type)
|
|
65 return this;
|
|
66
|
|
67 // Run semantic() on each element
|
|
68 for (size_t i = 0; i < elements.dim; i++)
|
|
69 {
|
|
70 e = cast(Expression)elements.data[i];
|
|
71 if (!e)
|
|
72 continue;
|
|
73 e = e.semantic(sc);
|
|
74 elements.data[i] = cast(void*)e;
|
|
75 }
|
|
76 expandTuples(elements);
|
|
77 size_t offset = 0;
|
|
78 for (size_t i = 0; i < elements.dim; i++)
|
|
79 {
|
|
80 e = cast(Expression)elements.data[i];
|
|
81 if (!e)
|
|
82 continue;
|
|
83
|
|
84 if (!e.type)
|
|
85 error("%s has no value", e.toChars());
|
|
86 e = resolveProperties(sc, e);
|
|
87 if (i >= nfields)
|
|
88 {
|
|
89 error("more initializers than fields of %s", sd.toChars());
|
|
90 return new ErrorExp();
|
|
91 }
|
|
92 Dsymbol s = cast(Dsymbol)sd.fields.data[i];
|
|
93 VarDeclaration v = s.isVarDeclaration();
|
|
94 assert(v);
|
|
95 if (v.offset < offset)
|
|
96 error("overlapping initialization for %s", v.toChars());
|
|
97 offset = v.offset + cast(uint)v.type.size();
|
|
98
|
|
99 Type telem = v.type;
|
|
100 while (!e.implicitConvTo(telem) && telem.toBasetype().ty == Tsarray)
|
|
101 {
|
|
102 /* Static array initialization, as in:
|
|
103 * T[3][5] = e;
|
|
104 */
|
|
105 telem = telem.toBasetype().nextOf();
|
|
106 }
|
|
107
|
|
108 e = e.implicitCastTo(sc, telem);
|
|
109
|
|
110 elements.data[i] = cast(void*)e;
|
|
111 }
|
|
112
|
|
113 /* Fill out remainder of elements[] with default initializers for fields[]
|
|
114 */
|
|
115 for (size_t i = elements.dim; i < nfields; i++)
|
|
116 {
|
|
117 Dsymbol s = cast(Dsymbol)sd.fields.data[i];
|
|
118 VarDeclaration v = s.isVarDeclaration();
|
|
119 assert(v);
|
|
120 assert(!v.isThisDeclaration());
|
|
121
|
|
122 if (v.offset < offset)
|
|
123 {
|
|
124 e = null;
|
|
125 sd.hasUnions = 1;
|
|
126 }
|
|
127 else
|
|
128 {
|
|
129 if (v.init)
|
|
130 {
|
|
131 e = v.init.toExpression();
|
|
132 if (!e)
|
|
133 error("cannot make expression out of initializer for %s", v.toChars());
|
|
134 }
|
|
135 else
|
|
136 {
|
|
137 e = v.type.defaultInit(Loc(0));
|
|
138 e.loc = loc;
|
|
139 }
|
|
140 offset = v.offset + cast(uint)v.type.size();
|
|
141 }
|
|
142 elements.push(cast(void*)e);
|
|
143 }
|
|
144
|
|
145 type = sd.type;
|
|
146 return this;
|
0
|
147 }
|
|
148
|
|
149 Expression getField(Type type, uint offset)
|
|
150 {
|
|
151 assert(false);
|
|
152 }
|
|
153
|
|
154 int getFieldIndex(Type type, uint offset)
|
|
155 {
|
|
156 assert(false);
|
|
157 }
|
|
158
|
|
159 elem* toElem(IRState* irs)
|
|
160 {
|
|
161 assert(false);
|
|
162 }
|
|
163
|
|
164 bool checkSideEffect(int flag)
|
|
165 {
|
|
166 assert(false);
|
|
167 }
|
|
168
|
|
169 void toCBuffer(OutBuffer buf, HdrGenState* hgs)
|
|
170 {
|
|
171 assert(false);
|
|
172 }
|
|
173
|
|
174 void toMangleBuffer(OutBuffer buf)
|
|
175 {
|
|
176 assert(false);
|
|
177 }
|
|
178
|
|
179 void scanForNestedRef(Scope sc)
|
|
180 {
|
|
181 assert(false);
|
|
182 }
|
|
183
|
|
184 Expression optimize(int result)
|
|
185 {
|
56
|
186 if (elements)
|
|
187 {
|
|
188 for (size_t i = 0; i < elements.dim; i++)
|
|
189 {
|
|
190 Expression e = cast(Expression)elements.data[i];
|
|
191 if (!e)
|
|
192 continue;
|
|
193 e = e.optimize(WANTvalue | (result & WANTinterpret));
|
|
194 elements.data[i] = cast(void*)e;
|
|
195 }
|
|
196 }
|
|
197 return this;
|
0
|
198 }
|
|
199
|
63
|
200 Expression interpret(InterState istate)
|
0
|
201 {
|
|
202 assert(false);
|
|
203 }
|
|
204
|
|
205 dt_t** toDt(dt_t** pdt)
|
|
206 {
|
|
207 assert(false);
|
|
208 }
|
|
209
|
|
210 int isLvalue()
|
|
211 {
|
|
212 assert(false);
|
|
213 }
|
|
214
|
|
215 Expression toLvalue(Scope sc, Expression e)
|
|
216 {
|
|
217 assert(false);
|
|
218 }
|
|
219
|
|
220 bool canThrow()
|
|
221 {
|
64
|
222 return arrayExpressionCanThrow(elements);
|
0
|
223 }
|
|
224
|
|
225 MATCH implicitConvTo(Type t)
|
|
226 {
|
64
|
227 static if (false) {
|
|
228 printf("StructLiteralExp.implicitConvTo(this=%.*s, type=%.*s, t=%.*s)\n",
|
|
229 toChars(), type.toChars(), t.toChars());
|
|
230 }
|
|
231 MATCH m = Expression.implicitConvTo(t);
|
|
232 if (m != MATCHnomatch)
|
|
233 return m;
|
|
234 if (type.ty == t.ty && type.ty == Tstruct && (cast(TypeStruct)type).sym == (cast(TypeStruct)t).sym)
|
|
235 {
|
|
236 m = MATCHconst;
|
|
237 for (int i = 0; i < elements.dim; i++)
|
|
238 {
|
|
239 Expression e = cast(Expression)elements.data[i];
|
|
240 Type te = e.type;
|
|
241 if (t.mod == 0)
|
|
242 te = te.mutableOf();
|
|
243 else
|
|
244 {
|
|
245 assert(t.mod == MODinvariant);
|
|
246 te = te.invariantOf();
|
|
247 }
|
|
248 MATCH m2 = e.implicitConvTo(te);
|
|
249 //printf("\t%s => %s, match = %d\n", e.toChars(), te.toChars(), m2);
|
|
250 if (m2 < m)
|
|
251 m = m2;
|
|
252 }
|
|
253 }
|
|
254 return m;
|
0
|
255 }
|
|
256
|
|
257 int inlineCost(InlineCostState* ics)
|
|
258 {
|
|
259 assert(false);
|
|
260 }
|
|
261
|
|
262 Expression doInline(InlineDoState ids)
|
|
263 {
|
|
264 assert(false);
|
|
265 }
|
|
266
|
|
267 Expression inlineScan(InlineScanState* iss)
|
|
268 {
|
|
269 assert(false);
|
|
270 }
|
|
271 }
|
|
272
|