0
|
1 module dmd.StructInitializer;
|
|
2
|
|
3 import dmd.Initializer;
|
2
|
4 import dmd.TOK;
|
|
5 import dmd.FuncLiteralDeclaration;
|
|
6 import dmd.TypeFunction;
|
56
|
7 import dmd.StructDeclaration;
|
|
8 import dmd.StructLiteralExp;
|
0
|
9 import dmd.ArrayTypes;
|
|
10 import dmd.Array;
|
|
11 import dmd.Loc;
|
|
12 import dmd.Type;
|
|
13 import dmd.Scope;
|
|
14 import dmd.Identifier;
|
2
|
15 import dmd.CompoundStatement;
|
0
|
16 import dmd.AggregateDeclaration;
|
|
17 import dmd.OutBuffer;
|
|
18 import dmd.HdrGenState;
|
|
19 import dmd.Expression;
|
2
|
20 import dmd.TypeStruct;
|
|
21 import dmd.TY;
|
|
22 import dmd.VarDeclaration;
|
|
23 import dmd.Dsymbol;
|
|
24 import dmd.Util;
|
|
25 import dmd.ExpInitializer;
|
|
26 import dmd.FuncExp;
|
|
27 import dmd.LINK;
|
0
|
28
|
|
29 import dmd.backend.dt_t;
|
|
30
|
|
31 class StructInitializer : Initializer
|
|
32 {
|
|
33 Identifiers field; // of Identifier *'s
|
|
34 Initializers value; // parallel array of Initializer *'s
|
|
35
|
|
36 Array vars; // parallel array of VarDeclaration *'s
|
|
37 AggregateDeclaration ad; // which aggregate this is for
|
|
38
|
|
39 this(Loc loc)
|
|
40 {
|
|
41 super(loc);
|
2
|
42 ad = null;
|
|
43
|
|
44 field = new Identifiers();
|
|
45 value = new Initializers();
|
51
|
46
|
|
47 vars = new Array();
|
0
|
48 }
|
|
49
|
|
50 Initializer syntaxCopy()
|
|
51 {
|
2
|
52 StructInitializer ai = new StructInitializer(loc);
|
|
53
|
|
54 assert(field.dim == value.dim);
|
|
55 ai.field.setDim(field.dim);
|
|
56 ai.value.setDim(value.dim);
|
|
57 for (int i = 0; i < field.dim; i++)
|
|
58 {
|
|
59 ai.field.data[i] = field.data[i];
|
|
60
|
|
61 Initializer init = cast(Initializer)value.data[i];
|
|
62 init = init.syntaxCopy();
|
|
63 ai.value.data[i] = cast(void*)init;
|
|
64 }
|
|
65
|
|
66 return ai;
|
0
|
67 }
|
|
68
|
|
69 void addInit(Identifier field, Initializer value)
|
|
70 {
|
2
|
71 //printf("StructInitializer.addInit(field = %p, value = %p)\n", field, value);
|
|
72 this.field.push(cast(void*)field);
|
|
73 this.value.push(cast(void*)value);
|
0
|
74 }
|
|
75
|
|
76 Initializer semantic(Scope sc, Type t)
|
|
77 {
|
2
|
78 TypeStruct ts;
|
|
79 int errors = 0;
|
|
80
|
|
81 //printf("StructInitializer.semantic(t = %s) %s\n", t.toChars(), toChars());
|
|
82 vars.setDim(field.dim);
|
|
83 t = t.toBasetype();
|
|
84 if (t.ty == Tstruct)
|
|
85 {
|
|
86 uint i;
|
|
87 uint fieldi = 0;
|
|
88
|
|
89 ts = cast(TypeStruct)t;
|
|
90 ad = ts.sym;
|
|
91 for (i = 0; i < field.dim; i++)
|
|
92 {
|
|
93 Identifier id = cast(Identifier)field.data[i];
|
|
94 Initializer val = cast(Initializer)value.data[i];
|
|
95 Dsymbol s;
|
|
96 VarDeclaration v;
|
|
97
|
|
98 if (id is null)
|
|
99 {
|
|
100 if (fieldi >= ad.fields.dim)
|
|
101 {
|
|
102 error(loc, "too many initializers for %s", ad.toChars());
|
|
103 field.remove(i);
|
|
104 i--;
|
|
105 continue;
|
|
106 }
|
|
107 else
|
|
108 {
|
|
109 s = cast(Dsymbol)ad.fields.data[fieldi];
|
|
110 }
|
|
111 }
|
|
112 else
|
|
113 {
|
|
114 //s = ad.symtab.lookup(id);
|
|
115 s = ad.search(loc, id, 0);
|
|
116 if (!s)
|
|
117 {
|
|
118 error(loc, "'%s' is not a member of '%s'", id.toChars(), t.toChars());
|
|
119 continue;
|
|
120 }
|
|
121
|
|
122 // Find out which field index it is
|
|
123 for (fieldi = 0; 1; fieldi++)
|
|
124 {
|
|
125 if (fieldi >= ad.fields.dim)
|
|
126 {
|
|
127 s.error("is not a per-instance initializable field");
|
|
128 break;
|
|
129 }
|
|
130 if (s == cast(Dsymbol)ad.fields.data[fieldi])
|
|
131 break;
|
|
132 }
|
|
133 }
|
|
134 if (s && (v = s.isVarDeclaration()) !is null)
|
|
135 {
|
|
136 val = val.semantic(sc, v.type);
|
|
137 value.data[i] = cast(void*)val;
|
|
138 vars.data[i] = cast(void*)v;
|
|
139 }
|
|
140 else
|
|
141 {
|
|
142 error(loc, "%s is not a field of %s", id ? id.toChars() : s.toChars(), ad.toChars());
|
|
143 errors = 1;
|
|
144 }
|
|
145 fieldi++;
|
|
146 }
|
|
147 }
|
|
148 else if (t.ty == Tdelegate && value.dim == 0)
|
|
149 {
|
|
150 /* Rewrite as empty delegate literal { }
|
|
151 */
|
|
152 Arguments arguments = new Arguments;
|
|
153 Type tf = new TypeFunction(arguments, null, 0, LINK.LINKd);
|
|
154 FuncLiteralDeclaration fd = new FuncLiteralDeclaration(loc, Loc(0), tf, TOK.TOKdelegate, null);
|
|
155 fd.fbody = new CompoundStatement(loc, new Statements());
|
|
156 fd.endloc = loc;
|
|
157 Expression e = new FuncExp(loc, fd);
|
|
158 ExpInitializer ie = new ExpInitializer(loc, e);
|
|
159 return ie.semantic(sc, t);
|
|
160 }
|
|
161 else
|
|
162 {
|
|
163 error(loc, "a struct is not a valid initializer for a %s", t.toChars());
|
|
164 errors = 1;
|
|
165 }
|
|
166 if (errors)
|
|
167 {
|
|
168 field.setDim(0);
|
|
169 value.setDim(0);
|
|
170 vars.setDim(0);
|
|
171 }
|
|
172 return this;
|
0
|
173 }
|
|
174
|
56
|
175 /***************************************
|
|
176 * This works by transforming a struct initializer into
|
|
177 * a struct literal. In the future, the two should be the
|
|
178 * same thing.
|
|
179 */
|
0
|
180 Expression toExpression()
|
|
181 {
|
56
|
182 Expression e;
|
|
183
|
|
184 //printf("StructInitializer.toExpression() %s\n", toChars());
|
|
185 if (!ad) // if fwd referenced
|
|
186 {
|
|
187 return null;
|
|
188 }
|
|
189 StructDeclaration sd = ad.isStructDeclaration();
|
|
190 if (!sd)
|
|
191 return null;
|
|
192 Expressions elements = new Expressions();
|
|
193 for (size_t i = 0; i < value.dim; i++)
|
|
194 {
|
|
195 if (field.data[i])
|
|
196 goto Lno;
|
|
197 Initializer iz = cast(Initializer)value.data[i];
|
|
198 if (!iz)
|
|
199 goto Lno;
|
|
200 Expression ex = iz.toExpression();
|
|
201 if (!ex)
|
|
202 goto Lno;
|
|
203 elements.push(cast(void*)ex);
|
|
204 }
|
|
205 e = new StructLiteralExp(loc, sd, elements);
|
|
206 e.type = sd.type;
|
|
207 return e;
|
|
208
|
|
209 Lno:
|
|
210 delete elements;
|
|
211 //error(loc, "struct initializers as expressions are not allowed");
|
|
212 return null;
|
0
|
213 }
|
|
214
|
|
215 void toCBuffer(OutBuffer buf, HdrGenState* hgs)
|
|
216 {
|
|
217 assert(false);
|
|
218 }
|
|
219
|
|
220 dt_t* toDt()
|
|
221 {
|
|
222 assert(false);
|
|
223 }
|
|
224
|
|
225 StructInitializer isStructInitializer() { return this; }
|
|
226 } |