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