Mercurial > projects > ddmd
comparison dmd/StructLiteralExp.d @ 84:be2ab491772e
Expressions -> Vector!Expression
author | Eldar Insafutdinov <e.insafutdinov@gmail.com> |
---|---|
date | Mon, 30 Aug 2010 16:12:19 +0100 |
parents | 43073c7c7769 |
children | 8e69d041a99d |
comparison
equal
deleted
inserted
replaced
83:ee670dd808a8 | 84:be2ab491772e |
---|---|
74 } | 74 } |
75 if (type) | 75 if (type) |
76 return this; | 76 return this; |
77 | 77 |
78 // Run semantic() on each element | 78 // Run semantic() on each element |
79 for (size_t i = 0; i < elements.dim; i++) | 79 foreach(size_t i, Expression e; elements) |
80 { | 80 { |
81 e = cast(Expression)elements.data[i]; | |
82 if (!e) | 81 if (!e) |
83 continue; | 82 continue; |
84 e = e.semantic(sc); | 83 e = e.semantic(sc); |
85 elements.data[i] = cast(void*)e; | 84 elements[i] = e; |
86 } | 85 } |
87 expandTuples(elements); | 86 expandTuples(elements); |
88 size_t offset = 0; | 87 size_t offset = 0; |
89 for (size_t i = 0; i < elements.dim; i++) | 88 foreach(size_t i, Expression e; elements) |
90 { | 89 { |
91 e = cast(Expression)elements.data[i]; | |
92 if (!e) | 90 if (!e) |
93 continue; | 91 continue; |
94 | 92 |
95 if (!e.type) | 93 if (!e.type) |
96 error("%s has no value", e.toChars()); | 94 error("%s has no value", e.toChars()); |
98 if (i >= nfields) | 96 if (i >= nfields) |
99 { | 97 { |
100 error("more initializers than fields of %s", sd.toChars()); | 98 error("more initializers than fields of %s", sd.toChars()); |
101 return new ErrorExp(); | 99 return new ErrorExp(); |
102 } | 100 } |
103 Dsymbol s = sd.fields[i]; | 101 auto s = sd.fields[i]; |
104 VarDeclaration v = s.isVarDeclaration(); | 102 VarDeclaration v = s.isVarDeclaration(); |
105 assert(v); | 103 assert(v); |
106 if (v.offset < offset) | 104 if (v.offset < offset) |
107 error("overlapping initialization for %s", v.toChars()); | 105 error("overlapping initialization for %s", v.toChars()); |
108 offset = v.offset + cast(uint)v.type.size(); | 106 offset = v.offset + cast(uint)v.type.size(); |
116 telem = telem.toBasetype().nextOf(); | 114 telem = telem.toBasetype().nextOf(); |
117 } | 115 } |
118 | 116 |
119 e = e.implicitCastTo(sc, telem); | 117 e = e.implicitCastTo(sc, telem); |
120 | 118 |
121 elements.data[i] = cast(void*)e; | 119 elements[i] = e; |
122 } | 120 } |
123 | 121 |
124 /* Fill out remainder of elements[] with default initializers for fields[] | 122 /* Fill out remainder of elements[] with default initializers for fields[] |
125 */ | 123 */ |
126 for (size_t i = elements.dim; i < nfields; i++) | 124 for (size_t i = elements.dim; i < nfields; i++) |
127 { | 125 { |
128 Dsymbol s = sd.fields[i]; | 126 VarDeclaration v = sd.fields[i]; |
129 VarDeclaration v = s.isVarDeclaration(); | |
130 assert(v); | 127 assert(v); |
131 assert(!v.isThisDeclaration()); | 128 assert(!v.isThisDeclaration()); |
132 | 129 |
133 if (v.offset < offset) | 130 if (v.offset < offset) |
134 { | 131 { |
148 e = v.type.defaultInit(Loc(0)); | 145 e = v.type.defaultInit(Loc(0)); |
149 e.loc = loc; | 146 e.loc = loc; |
150 } | 147 } |
151 offset = v.offset + cast(uint)v.type.size(); | 148 offset = v.offset + cast(uint)v.type.size(); |
152 } | 149 } |
153 elements.push(cast(void*)e); | 150 elements.push(e); |
154 } | 151 } |
155 | 152 |
156 type = sd.type; | 153 type = sd.type; |
157 return this; | 154 return this; |
158 } | 155 } |
239 /* Initialize all alignment 'holes' to zero. | 236 /* Initialize all alignment 'holes' to zero. |
240 * Do before initializing fields, as the hole filling process | 237 * Do before initializing fields, as the hole filling process |
241 * can spill over into the fields. | 238 * can spill over into the fields. |
242 */ | 239 */ |
243 size_t offset = 0; | 240 size_t offset = 0; |
244 foreach (Dsymbol s; sd.fields) | 241 foreach (VarDeclaration v; sd.fields) |
245 { | 242 { |
246 VarDeclaration v = s.isVarDeclaration(); | |
247 assert(v); | 243 assert(v); |
248 | 244 |
249 e = el_combine(e, fillHole(stmp, &offset, v.offset, sd.structsize)); | 245 e = el_combine(e, fillHole(stmp, &offset, v.offset, sd.structsize)); |
250 size_t vend = v.offset + cast(uint)v.type.size(); | 246 size_t vend = v.offset + cast(uint)v.type.size(); |
251 if (offset < vend) | 247 if (offset < vend) |
256 | 252 |
257 if (elements) | 253 if (elements) |
258 { | 254 { |
259 dim = elements.dim; | 255 dim = elements.dim; |
260 assert(dim <= sd.fields.dim); | 256 assert(dim <= sd.fields.dim); |
261 for (size_t i = 0; i < dim; i++) | 257 foreach (size_t i, Expression el; elements) |
262 { | 258 { |
263 Expression el = cast(Expression)elements.data[i]; | |
264 if (!el) | 259 if (!el) |
265 continue; | 260 continue; |
266 | 261 |
267 Dsymbol s = sd.fields[i]; | 262 VarDeclaration v = sd.fields[i]; |
268 VarDeclaration v = s.isVarDeclaration(); | |
269 assert(v); | 263 assert(v); |
270 assert(!v.isThisDeclaration()); | 264 assert(!v.isThisDeclaration()); |
271 | 265 |
272 elem* e1; | 266 elem* e1; |
273 if (tybasic(stmp.Stype.Tty) == TYnptr) | 267 if (tybasic(stmp.Stype.Tty) == TYnptr) |
357 version (DMDV2) | 351 version (DMDV2) |
358 { | 352 { |
359 if (sd.isnested) | 353 if (sd.isnested) |
360 { // Initialize the hidden 'this' pointer | 354 { // Initialize the hidden 'this' pointer |
361 assert(sd.fields.dim); | 355 assert(sd.fields.dim); |
362 Dsymbol s = sd.fields[sd.fields.dim - 1]; | 356 auto s = sd.fields[sd.fields.dim - 1]; |
363 ThisDeclaration v = s.isThisDeclaration(); | 357 auto v = s.isThisDeclaration(); |
364 assert(v); | 358 assert(v); |
365 | 359 |
366 elem* e1; | 360 elem* e1; |
367 if (tybasic(stmp.Stype.Tty) == TYnptr) | 361 if (tybasic(stmp.Stype.Tty) == TYnptr) |
368 { | 362 { |
435 | 429 |
436 override Expression optimize(int result) | 430 override Expression optimize(int result) |
437 { | 431 { |
438 if (elements) | 432 if (elements) |
439 { | 433 { |
440 for (size_t i = 0; i < elements.dim; i++) | 434 foreach (size_t i, Expression e; elements) |
441 { | 435 { |
442 Expression e = cast(Expression)elements.data[i]; | |
443 if (!e) | 436 if (!e) |
444 continue; | 437 continue; |
445 e = e.optimize(WANTvalue | (result & WANTinterpret)); | 438 e = e.optimize(WANTvalue | (result & WANTinterpret)); |
446 elements.data[i] = cast(void*)e; | 439 elements[i] = e; |
447 } | 440 } |
448 } | 441 } |
449 return this; | 442 return this; |
450 } | 443 } |
451 | 444 |
490 if (m != MATCHnomatch) | 483 if (m != MATCHnomatch) |
491 return m; | 484 return m; |
492 if (type.ty == t.ty && type.ty == Tstruct && (cast(TypeStruct)type).sym == (cast(TypeStruct)t).sym) | 485 if (type.ty == t.ty && type.ty == Tstruct && (cast(TypeStruct)type).sym == (cast(TypeStruct)t).sym) |
493 { | 486 { |
494 m = MATCHconst; | 487 m = MATCHconst; |
495 for (int i = 0; i < elements.dim; i++) | 488 foreach(e; elements) |
496 { | 489 { |
497 Expression e = cast(Expression)elements.data[i]; | |
498 Type te = e.type; | 490 Type te = e.type; |
499 if (t.mod == 0) | 491 if (t.mod == 0) |
500 te = te.mutableOf(); | 492 te = te.mutableOf(); |
501 else | 493 else |
502 { | 494 { |