Mercurial > projects > ddmd
comparison dmd/StructInitializer.d @ 128:e6e542f37b94
Some more Array -> Vector conversions
author | Eldar Insafutdinov <e.insafutdinov@gmail.com> |
---|---|
date | Sat, 04 Sep 2010 01:33:05 +0100 |
parents | 9ee9b55452cb |
children | 60bb0fe4563e |
comparison
equal
deleted
inserted
replaced
127:9ee9b55452cb | 128:e6e542f37b94 |
---|---|
34 class StructInitializer : Initializer | 34 class StructInitializer : Initializer |
35 { | 35 { |
36 Identifiers field; // of Identifier *'s | 36 Identifiers field; // of Identifier *'s |
37 Initializers value; // parallel array of Initializer *'s | 37 Initializers value; // parallel array of Initializer *'s |
38 | 38 |
39 Array vars; // parallel array of VarDeclaration *'s | 39 VarDeclarations vars; // parallel array of VarDeclaration *'s |
40 AggregateDeclaration ad; // which aggregate this is for | 40 AggregateDeclaration ad; // which aggregate this is for |
41 | 41 |
42 this(Loc loc) | 42 this(Loc loc) |
43 { | 43 { |
44 super(loc); | 44 super(loc); |
45 ad = null; | 45 ad = null; |
46 | 46 |
47 field = new Identifiers(); | 47 field = new Identifiers(); |
48 value = new Initializers(); | 48 value = new Initializers(); |
49 | 49 |
50 vars = new Array(); | 50 vars = new VarDeclarations(); |
51 } | 51 } |
52 | 52 |
53 override Initializer syntaxCopy() | 53 override Initializer syntaxCopy() |
54 { | 54 { |
55 auto ai = new StructInitializer(loc); | 55 auto ai = new StructInitializer(loc); |
136 } | 136 } |
137 if (s && (v = s.isVarDeclaration()) !is null) | 137 if (s && (v = s.isVarDeclaration()) !is null) |
138 { | 138 { |
139 val = val.semantic(sc, v.type); | 139 val = val.semantic(sc, v.type); |
140 value[i] = val; | 140 value[i] = val; |
141 vars.data[i] = cast(void*)v; | 141 vars[i] = v; |
142 } | 142 } |
143 else | 143 else |
144 { | 144 { |
145 error(loc, "%s is not a field of %s", id ? id.toChars() : s.toChars(), ad.toChars()); | 145 error(loc, "%s is not a field of %s", id ? id.toChars() : s.toChars(), ad.toChars()); |
146 errors = 1; | 146 errors = 1; |
150 } | 150 } |
151 else if (t.ty == Tdelegate && value.dim == 0) | 151 else if (t.ty == Tdelegate && value.dim == 0) |
152 { | 152 { |
153 /* Rewrite as empty delegate literal { } | 153 /* Rewrite as empty delegate literal { } |
154 */ | 154 */ |
155 Arguments arguments = new Arguments; | 155 auto arguments = new Arguments; |
156 Type tf = new TypeFunction(arguments, null, 0, LINK.LINKd); | 156 Type tf = new TypeFunction(arguments, null, 0, LINK.LINKd); |
157 FuncLiteralDeclaration fd = new FuncLiteralDeclaration(loc, Loc(0), tf, TOK.TOKdelegate, null); | 157 FuncLiteralDeclaration fd = new FuncLiteralDeclaration(loc, Loc(0), tf, TOK.TOKdelegate, null); |
158 fd.fbody = new CompoundStatement(loc, new Statements()); | 158 fd.fbody = new CompoundStatement(loc, new Statements()); |
159 fd.endloc = loc; | 159 fd.endloc = loc; |
160 Expression e = new FuncExp(loc, fd); | 160 Expression e = new FuncExp(loc, fd); |
220 assert(false); | 220 assert(false); |
221 } | 221 } |
222 | 222 |
223 override dt_t* toDt() | 223 override dt_t* toDt() |
224 { | 224 { |
225 scope Array dts = new Array(); | 225 scope dts = new Vector!(dt_t*); |
226 uint i; | 226 uint i; |
227 uint j; | 227 uint j; |
228 dt_t* dt; | 228 dt_t* dt; |
229 dt_t* d; | 229 dt_t* d; |
230 dt_t** pdtend; | 230 dt_t** pdtend; |
234 dts.setDim(ad.fields.dim); | 234 dts.setDim(ad.fields.dim); |
235 dts.zero(); | 235 dts.zero(); |
236 | 236 |
237 for (i = 0; i < vars.dim; i++) | 237 for (i = 0; i < vars.dim; i++) |
238 { | 238 { |
239 VarDeclaration v = cast(VarDeclaration)vars.data[i]; | 239 VarDeclaration v = vars[i]; |
240 Initializer val = value[i]; | 240 Initializer val = value[i]; |
241 | 241 |
242 //printf("vars[%d] = %s\n", i, v.toChars()); | 242 //printf("vars[%d] = %s\n", i, v.toChars()); |
243 | 243 |
244 for (j = 0; 1; j++) | 244 for (j = 0; 1; j++) |
245 { | 245 { |
246 assert(j < dts.dim); | 246 assert(j < dts.dim); |
247 //printf(" adfield[%d] = %s\n", j, ((VarDeclaration *)ad.fields[j]).toChars()); | 247 //printf(" adfield[%d] = %s\n", j, ((VarDeclaration *)ad.fields[j]).toChars()); |
248 if (cast(VarDeclaration)ad.fields[j] == v) // TODO: check if 'is' needs to be used here | 248 if (cast(VarDeclaration)ad.fields[j] == v) // TODO: check if 'is' needs to be used here |
249 { | 249 { |
250 if (dts.data[j]) | 250 if (dts[j]) |
251 error(loc, "field %s of %s already initialized", v.toChars(), ad.toChars()); | 251 error(loc, "field %s of %s already initialized", v.toChars(), ad.toChars()); |
252 dts.data[j] = cast(void*)val.toDt(); | 252 dts[j] = val.toDt(); |
253 break; | 253 break; |
254 } | 254 } |
255 } | 255 } |
256 } | 256 } |
257 | 257 |
258 dt = null; | 258 dt = null; |
259 pdtend = &dt; | 259 pdtend = &dt; |
260 offset = 0; | 260 offset = 0; |
261 for (j = 0; j < dts.dim; j++) | 261 for (j = 0; j < dts.dim; j++) |
262 { | 262 { |
263 VarDeclaration v = cast(VarDeclaration)ad.fields[j]; | 263 VarDeclaration v = ad.fields[j]; |
264 | 264 |
265 d = cast(dt_t*)dts.data[j]; | 265 d = dts[j]; |
266 if (!d) | 266 if (!d) |
267 { | 267 { |
268 // An instance specific initializer was not provided. | 268 // An instance specific initializer was not provided. |
269 // Look to see if there's a default initializer from the | 269 // Look to see if there's a default initializer from the |
270 // struct definition | 270 // struct definition |
283 if (k == dts.dim) // didn't find any overlap | 283 if (k == dts.dim) // didn't find any overlap |
284 { | 284 { |
285 v.type.toDt(&d); | 285 v.type.toDt(&d); |
286 break; | 286 break; |
287 } | 287 } |
288 VarDeclaration v2 = cast(VarDeclaration)ad.fields[k]; | 288 VarDeclaration v2 = ad.fields[k]; |
289 | 289 |
290 if (v2.offset < offset2 && dts.data[k]) | 290 if (v2.offset < offset2 && dts[k]) |
291 break; // overlap | 291 break; // overlap |
292 } | 292 } |
293 } | 293 } |
294 } | 294 } |
295 if (d) | 295 if (d) |