annotate dmd/StructLiteralExp.d @ 63:cab4c37afb89

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