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