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