Mercurial > projects > ddmd
annotate dmd/StructDeclaration.d @ 89:37b95c347975
Fixed an obscure bug with i variable being reused
author | korDen |
---|---|
date | Mon, 30 Aug 2010 23:25:50 +0400 |
parents | 23280d154c5b |
children | ae5b11064a9a |
rev | line source |
---|---|
0 | 1 module dmd.StructDeclaration; |
2 | |
3 import dmd.AggregateDeclaration; | |
4 import dmd.FuncDeclaration; | |
5 import dmd.DeclarationExp; | |
6 import dmd.VoidInitializer; | |
7 import dmd.Initializer; | |
8 import dmd.ExpInitializer; | |
9 import dmd.TOK; | |
10 import dmd.Statement; | |
11 import dmd.VarExp; | |
12 import dmd.CompoundStatement; | |
13 import dmd.AssignExp; | |
14 import dmd.DotVarExp; | |
15 import dmd.AddrExp; | |
16 import dmd.CastExp; | |
17 import dmd.PostBlitDeclaration; | |
18 import dmd.Lexer; | |
19 import dmd.ExpStatement; | |
20 import dmd.DotIdExp; | |
21 import dmd.TypeSArray; | |
22 import dmd.ThisExp; | |
23 import dmd.ThisDeclaration; | |
24 import dmd.TypeFunction; | |
25 import dmd.Argument; | |
26 import dmd.Id; | |
27 import dmd.TY; | |
28 import dmd.LINK; | |
29 import dmd.Type; | |
30 import dmd.DsymbolTable; | |
31 import dmd.ArrayTypes; | |
32 import dmd.Loc; | |
33 import dmd.STC; | |
34 import dmd.Identifier; | |
35 import dmd.TemplateInstance; | |
36 import dmd.Dsymbol; | |
37 import dmd.Scope; | |
38 import dmd.OutBuffer; | |
39 import dmd.HdrGenState; | |
40 import dmd.PROT; | |
41 import dmd.TypeStruct; | |
42 import dmd.expression.Util; | |
43 import dmd.Expression; | |
44 import dmd.IdentifierExp; | |
45 import dmd.PtrExp; | |
46 import dmd.CallExp; | |
47 import dmd.ReturnStatement; | |
48 import dmd.ScopeDsymbol; | |
49 import dmd.Module; | |
50 import dmd.VarDeclaration; | |
51 import dmd.InvariantDeclaration; | |
52 import dmd.NewDeclaration; | |
53 import dmd.DeleteDeclaration; | |
54 import dmd.Global; | |
55 | |
56 import dmd.backend.dt_t; | |
57 import dmd.backend.Util; | |
58 import dmd.backend.SC; | |
59 import dmd.backend.DT; | |
60 import dmd.backend.FL; | |
61 import dmd.backend.glue; | |
62 | |
63 class StructDeclaration : AggregateDeclaration | |
64 { | |
65 bool zeroInit; // true if initialize with 0 fill | |
66 | |
67 version (DMDV2) { | |
68 int hasIdentityAssign; // !=0 if has identity opAssign | |
69 FuncDeclaration cpctor; // generated copy-constructor, if any | |
70 | |
71 FuncDeclarations postblits; // Array of postblit functions | |
72 FuncDeclaration postblit; // aggregate postblit | |
73 } | |
74 | |
75 this(Loc loc, Identifier id) | |
76 { | |
77 super(loc, id); | |
78 | |
79 // For forward references | |
80 type = new TypeStruct(this); | |
81 | |
82 postblits = new FuncDeclarations(); /// | |
83 } | |
84 | |
72 | 85 override Dsymbol syntaxCopy(Dsymbol s) |
0 | 86 { |
49 | 87 StructDeclaration sd; |
88 | |
89 if (s) | |
90 sd = cast(StructDeclaration)s; | |
91 else | |
92 sd = new StructDeclaration(loc, ident); | |
93 ScopeDsymbol.syntaxCopy(sd); | |
94 return sd; | |
0 | 95 } |
96 | |
72 | 97 override void semantic(Scope sc) |
0 | 98 { |
99 Scope sc2; | |
100 | |
101 //printf("+StructDeclaration.semantic(this=%p, '%s')\n", this, toChars()); | |
102 | |
103 //static int count; if (++count == 20) *(char*)0=0; | |
104 | |
105 assert(type); | |
106 if (!members) // if forward reference | |
107 return; | |
108 | |
109 if (symtab) | |
110 { if (!scope_) | |
111 return; // semantic() already completed | |
112 } | |
113 else | |
114 symtab = new DsymbolTable(); | |
115 | |
116 Scope scx = null; | |
117 if (scope_) | |
118 { sc = scope_; | |
119 scx = scope_; // save so we don't make redundant copies | |
120 scope_ = null; | |
121 } | |
122 | |
123 parent = sc.parent; | |
124 type = type.semantic(loc, sc); | |
125 version (STRUCTTHISREF) { | |
126 handle = type; | |
127 } else { | |
128 handle = type.pointerTo(); | |
129 } | |
130 structalign = sc.structalign; | |
131 protection = sc.protection; | |
132 storage_class |= sc.stc; | |
133 if (sc.stc & STC.STCdeprecated) | |
134 isdeprecated = 1; | |
135 assert(!isAnonymous()); | |
136 if (sc.stc & STC.STCabstract) | |
137 error("structs, unions cannot be abstract"); | |
138 version (DMDV2) { | |
139 if (storage_class & STC.STCimmutable) | |
140 type = type.invariantOf(); | |
141 else if (storage_class & STC.STCconst) | |
142 type = type.constOf(); | |
143 else if (storage_class & STC.STCshared) | |
144 type = type.sharedOf(); | |
145 } | |
146 | |
147 if (sizeok == 0) // if not already done the addMember step | |
148 { | |
149 int hasfunctions = 0; | |
77
ad4792a1cfd6
more D-ification container accessing
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
74
diff
changeset
|
150 foreach(Dsymbol s; members) |
0 | 151 { |
152 //printf("adding member '%s' to '%s'\n", s.toChars(), this.toChars()); | |
13 | 153 s.addMember(sc, this, true); |
0 | 154 if (s.isFuncDeclaration()) |
155 hasfunctions = 1; | |
156 } | |
157 | |
158 // If nested struct, add in hidden 'this' pointer to outer scope | |
159 if (hasfunctions && !(storage_class & STC.STCstatic)) | |
160 { Dsymbol s = toParent2(); | |
161 if (s) | |
162 { | |
163 AggregateDeclaration ad = s.isAggregateDeclaration(); | |
164 FuncDeclaration fd = s.isFuncDeclaration(); | |
165 | |
166 TemplateInstance ti; | |
167 if (ad && (ti = ad.parent.isTemplateInstance()) !is null && ti.isnested || fd) | |
168 { isnested = true; | |
169 Type t; | |
170 if (ad) | |
171 t = ad.handle; | |
172 else if (fd) | |
173 { | |
174 AggregateDeclaration add = fd.isMember2(); | |
175 if (add) | |
176 t = add.handle; | |
177 else | |
178 t = Type.tvoidptr; | |
179 } | |
180 else | |
181 assert(0); | |
182 if (t.ty == TY.Tstruct) | |
183 t = Type.tvoidptr; // t should not be a ref type | |
184 assert(!vthis); | |
185 vthis = new ThisDeclaration(loc, t); | |
186 //vthis.storage_class |= STC.STCref; | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
72
diff
changeset
|
187 members.push(vthis); |
0 | 188 } |
189 } | |
190 } | |
191 } | |
192 | |
193 sizeok = 0; | |
194 sc2 = sc.push(this); | |
195 sc2.stc &= storage_class & STC.STC_TYPECTOR; | |
196 sc2.parent = this; | |
197 if (isUnionDeclaration()) | |
198 sc2.inunion = 1; | |
199 sc2.protection = PROT.PROTpublic; | |
200 sc2.explicitProtection = 0; | |
201 | |
202 int members_dim = members.dim; | |
77
ad4792a1cfd6
more D-ification container accessing
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
74
diff
changeset
|
203 foreach(Dsymbol s; members) |
0 | 204 { |
205 s.semantic(sc2); | |
206 if (isUnionDeclaration()) | |
207 sc2.offset = 0; | |
208 static if (false) { | |
209 if (sizeok == 2) | |
210 { //printf("forward reference\n"); | |
211 break; | |
212 } | |
213 } | |
214 Type t; | |
215 if (s.isDeclaration() && | |
216 (t = s.isDeclaration().type) !is null && | |
217 t.toBasetype().ty == TY.Tstruct) | |
218 { StructDeclaration sd = cast(StructDeclaration)t.toDsymbol(sc); | |
219 if (sd.isnested) | |
220 error("inner struct %s cannot be a field", sd.toChars()); | |
221 } | |
222 } | |
223 | |
224 /* The TypeInfo_Struct is expecting an opEquals and opCmp with | |
225 * a parameter that is a pointer to the struct. But if there | |
226 * isn't one, but is an opEquals or opCmp with a value, write | |
227 * another that is a shell around the value: | |
228 * int opCmp(struct *p) { return opCmp(*p); } | |
229 */ | |
230 | |
231 TypeFunction tfeqptr; | |
232 { | |
233 Arguments arguments = new Arguments; | |
234 Argument arg = new Argument(STC.STCin, handle, Id.p, null); | |
235 | |
236 arguments.push(cast(void*)arg); | |
237 tfeqptr = new TypeFunction(arguments, Type.tint32, 0, LINK.LINKd); | |
238 tfeqptr = cast(TypeFunction)tfeqptr.semantic(Loc(0), sc); | |
239 } | |
240 | |
241 TypeFunction tfeq; | |
242 { | |
243 Arguments arguments = new Arguments; | |
244 Argument arg = new Argument(STC.STCin, type, null, null); | |
245 | |
246 arguments.push(cast(void*)arg); | |
247 tfeq = new TypeFunction(arguments, Type.tint32, 0, LINK.LINKd); | |
248 tfeq = cast(TypeFunction)tfeq.semantic(Loc(0), sc); | |
249 } | |
250 | |
251 Identifier id = Id.eq; | |
252 for (int j = 0; j < 2; j++) | |
253 { | |
254 Dsymbol s = search_function(this, id); | |
255 FuncDeclaration fdx = s ? s.isFuncDeclaration() : null; | |
256 if (fdx) | |
257 { FuncDeclaration fd = fdx.overloadExactMatch(tfeqptr); | |
258 if (!fd) | |
259 { fd = fdx.overloadExactMatch(tfeq); | |
260 if (fd) | |
261 { // Create the thunk, fdptr | |
262 FuncDeclaration fdptr = new FuncDeclaration(loc, loc, fdx.ident, STC.STCundefined, tfeqptr); | |
263 Expression e = new IdentifierExp(loc, Id.p); | |
264 e = new PtrExp(loc, e); | |
84
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
79
diff
changeset
|
265 auto args = new Expressions(); |
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
79
diff
changeset
|
266 args.push(e); |
0 | 267 e = new IdentifierExp(loc, id); |
268 e = new CallExp(loc, e, args); | |
269 fdptr.fbody = new ReturnStatement(loc, e); | |
270 ScopeDsymbol ss = fdx.parent.isScopeDsymbol(); | |
271 assert(ss); | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
72
diff
changeset
|
272 ss.members.push(fdptr); |
13 | 273 fdptr.addMember(sc, ss, true); |
0 | 274 fdptr.semantic(sc2); |
275 } | |
276 } | |
277 } | |
278 | |
279 id = Id.cmp; | |
280 } | |
281 version (DMDV2) { | |
282 dtor = buildDtor(sc2); | |
283 postblit = buildPostBlit(sc2); | |
284 cpctor = buildCpCtor(sc2); | |
285 buildOpAssign(sc2); | |
286 } | |
287 | |
288 sc2.pop(); | |
289 | |
290 if (sizeok == 2) | |
291 { // semantic() failed because of forward references. | |
292 // Unwind what we did, and defer it for later | |
293 fields.setDim(0); | |
294 structsize = 0; | |
295 alignsize = 0; | |
296 structalign = 0; | |
297 | |
87
b17640f0e4e8
Fixed a bug with a Scope.this(Scope enclosing) being called instead of Scope.clone() method (as a copy ctor replacement)
korDen
parents:
79
diff
changeset
|
298 scope_ = scx ? scx : sc.clone(); |
0 | 299 scope_.setNoFree(); |
300 scope_.module_.addDeferredSemantic(this); | |
301 //printf("\tdeferring %s\n", toChars()); | |
302 return; | |
303 } | |
304 | |
305 // 0 sized struct's are set to 1 byte | |
306 if (structsize == 0) | |
307 { | |
308 structsize = 1; | |
309 alignsize = 1; | |
310 } | |
311 | |
312 // Round struct size up to next alignsize boundary. | |
313 // This will ensure that arrays of structs will get their internals | |
314 // aligned properly. | |
315 structsize = (structsize + alignsize - 1) & ~(alignsize - 1); | |
316 | |
317 sizeok = 1; | |
318 Module.dprogress++; | |
319 | |
320 //printf("-StructDeclaration.semantic(this=%p, '%s')\n", this, toChars()); | |
321 | |
322 // Determine if struct is all zeros or not | |
323 zeroInit = true; | |
85
8e69d041a99d
Previous commit didn't compile. Fixed.
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
84
diff
changeset
|
324 foreach (VarDeclaration vd; fields) |
0 | 325 { |
79 | 326 if (vd && !vd.isDataseg()) |
0 | 327 { |
79 | 328 if (vd.init) |
329 { | |
330 // Should examine init to see if it is really all 0's | |
0 | 331 zeroInit = false; |
332 break; | |
79 | 333 } |
334 else | |
335 { | |
336 if (!vd.type.isZeroInit(loc)) | |
337 { | |
338 zeroInit = false; | |
339 break; | |
340 } | |
341 } | |
0 | 342 } |
343 } | |
344 | |
345 /* Look for special member functions. | |
346 */ | |
347 version (DMDV2) { | |
348 ctor = search(Loc(0), Id.ctor, 0); | |
349 } | |
350 inv = cast(InvariantDeclaration)search(Loc(0), Id.classInvariant, 0); | |
351 aggNew = cast(NewDeclaration)search(Loc(0), Id.classNew, 0); | |
352 aggDelete = cast(DeleteDeclaration)search(Loc(0), Id.classDelete, 0); | |
353 | |
354 if (sc.func) | |
355 { | |
356 semantic2(sc); | |
357 semantic3(sc); | |
358 } | |
359 } | |
360 | |
72 | 361 override void toCBuffer(OutBuffer buf, HdrGenState* hgs) |
0 | 362 { |
363 assert(false); | |
364 } | |
365 | |
72 | 366 override string mangle() |
0 | 367 { |
368 //printf("StructDeclaration.mangle() '%s'\n", toChars()); | |
369 return Dsymbol.mangle(); | |
370 } | |
371 | |
72 | 372 override string kind() |
0 | 373 { |
374 assert(false); | |
375 } | |
376 | |
377 /******************************************* | |
378 * We need an opAssign for the struct if | |
379 * it has a destructor or a postblit. | |
380 * We need to generate one if a user-specified one does not exist. | |
381 */ | |
382 bool needOpAssign() | |
383 { | |
384 static if (false) { | |
385 printf("StructDeclaration.needOpAssign() %s\n", toChars()); | |
386 } | |
387 if (hasIdentityAssign) | |
388 goto Ldontneed; | |
389 | |
390 if (dtor || postblit) | |
391 goto Lneed; | |
392 | |
393 /* If any of the fields need an opAssign, then we | |
394 * need it too. | |
395 */ | |
85
8e69d041a99d
Previous commit didn't compile. Fixed.
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
84
diff
changeset
|
396 foreach (VarDeclaration v; fields) |
0 | 397 { |
398 assert(v && v.storage_class & STC.STCfield); | |
399 if (v.storage_class & STC.STCref) | |
400 continue; | |
401 Type tv = v.type.toBasetype(); | |
402 while (tv.ty == TY.Tsarray) | |
403 { TypeSArray ta = cast(TypeSArray)tv; | |
404 tv = tv.nextOf().toBasetype(); | |
405 } | |
406 if (tv.ty == TY.Tstruct) | |
407 { TypeStruct ts = cast(TypeStruct)tv; | |
408 StructDeclaration sd = ts.sym; | |
409 if (sd.needOpAssign()) | |
410 goto Lneed; | |
411 } | |
412 } | |
413 Ldontneed: | |
414 static if (false) { | |
415 printf("\tdontneed\n"); | |
416 } | |
417 return false; | |
418 | |
419 Lneed: | |
420 static if (false) { | |
421 printf("\tneed\n"); | |
422 } | |
423 return true; | |
424 } | |
425 | |
426 /****************************************** | |
427 * Build opAssign for struct. | |
428 * S* opAssign(S s) { ... } | |
429 */ | |
430 FuncDeclaration buildOpAssign(Scope sc) | |
431 { | |
432 if (!needOpAssign()) | |
433 return null; | |
434 | |
435 //printf("StructDeclaration.buildOpAssign() %s\n", toChars()); | |
436 | |
437 FuncDeclaration fop = null; | |
438 | |
439 Argument param = new Argument(STC.STCnodtor, type, Id.p, null); | |
440 Arguments fparams = new Arguments; | |
441 fparams.push(cast(void*)param); | |
442 Type ftype = new TypeFunction(fparams, handle, false, LINK.LINKd); | |
443 version (STRUCTTHISREF) { | |
444 (cast(TypeFunction)ftype).isref = 1; | |
445 } | |
446 | |
447 fop = new FuncDeclaration(Loc(0), Loc(0), Id.assign, STC.STCundefined, ftype); | |
448 | |
449 Expression e = null; | |
450 if (postblit) | |
451 { /* Swap: | |
452 * tmp = *this; *this = s; tmp.dtor(); | |
453 */ | |
454 //printf("\tswap copy\n"); | |
455 Identifier idtmp = Lexer.uniqueId("__tmp"); | |
456 VarDeclaration tmp; | |
457 AssignExp ec = null; | |
458 if (dtor) | |
459 { | |
460 tmp = new VarDeclaration(Loc(0), type, idtmp, new VoidInitializer(Loc(0))); | |
461 tmp.noauto = true; | |
462 e = new DeclarationExp(Loc(0), tmp); | |
463 | |
464 Expression e2; | |
465 version (STRUCTTHISREF) { | |
466 e2 = new ThisExp(Loc(0)); | |
467 } else { | |
468 e2 = new PtrExp(Loc(0), new ThisExp(Loc(0))); | |
469 } | |
470 ec = new AssignExp(Loc(0), new VarExp(Loc(0), tmp), e2); | |
471 ec.op = TOK.TOKblit; | |
472 e = Expression.combine(e, ec); | |
473 } | |
474 Expression e2; | |
475 version (STRUCTTHISREF) { | |
476 e2 = new ThisExp(Loc(0)); | |
477 } else { | |
478 e2 = new PtrExp(Loc(0), new ThisExp(Loc(0))); | |
479 } | |
480 | |
481 ec = new AssignExp(Loc(0), e2, new IdentifierExp(Loc(0), Id.p)); | |
482 ec.op = TOK.TOKblit; | |
483 e = Expression.combine(e, ec); | |
484 if (dtor) | |
485 { | |
486 /* Instead of running the destructor on s, run it | |
487 * on tmp. This avoids needing to copy tmp back in to s. | |
488 */ | |
489 Expression ecc = new DotVarExp(Loc(0), new VarExp(Loc(0), tmp), dtor, 0); | |
490 ecc = new CallExp(Loc(0), ecc); | |
491 e = Expression.combine(e, ecc); | |
492 } | |
493 } | |
494 else | |
495 { /* Do memberwise copy | |
496 */ | |
497 //printf("\tmemberwise copy\n"); | |
85
8e69d041a99d
Previous commit didn't compile. Fixed.
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
84
diff
changeset
|
498 foreach (VarDeclaration v; fields) |
0 | 499 { |
500 assert(v && v.storage_class & STC.STCfield); | |
501 // this.v = s.v; | |
502 AssignExp ec = new AssignExp(Loc(0), new DotVarExp(Loc(0), new ThisExp(Loc(0)), v, 0), new DotVarExp(Loc(0), new IdentifierExp(Loc(0), Id.p), v, 0)); | |
503 ec.op = TOK.TOKblit; | |
504 e = Expression.combine(e, ec); | |
505 } | |
506 } | |
507 Statement s1 = new ExpStatement(Loc(0), e); | |
508 | |
509 /* Add: | |
510 * return this; | |
511 */ | |
512 e = new ThisExp(Loc(0)); | |
513 Statement s2 = new ReturnStatement(Loc(0), e); | |
514 | |
515 fop.fbody = new CompoundStatement(Loc(0), s1, s2); | |
516 | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
72
diff
changeset
|
517 members.push(fop); |
13 | 518 fop.addMember(sc, this, true); |
0 | 519 |
520 sc = sc.push(); | |
521 sc.stc = STC.STCundefined; | |
522 sc.linkage = LINK.LINKd; | |
523 | |
524 fop.semantic(sc); | |
525 | |
526 sc.pop(); | |
527 | |
528 //printf("-StructDeclaration.buildOpAssign() %s\n", toChars()); | |
529 | |
530 return fop; | |
531 } | |
532 | |
533 /***************************************** | |
534 * Create inclusive postblit for struct by aggregating | |
535 * all the postblits in postblits[] with the postblits for | |
536 * all the members. | |
537 * Note the close similarity with AggregateDeclaration.buildDtor(), | |
538 * and the ordering changes (runs forward instead of backwards). | |
539 */ | |
540 | |
541 version (DMDV2) { | |
542 FuncDeclaration buildPostBlit(Scope sc) | |
543 { | |
544 //printf("StructDeclaration.buildPostBlit() %s\n", toChars()); | |
545 Expression e = null; | |
546 | |
85
8e69d041a99d
Previous commit didn't compile. Fixed.
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
84
diff
changeset
|
547 foreach (VarDeclaration v; fields) |
0 | 548 { |
79 | 549 assert(v && v.storage_class & STC.STCfield); |
550 if (v.storage_class & STC.STCref) | |
551 continue; | |
552 Type tv = v.type.toBasetype(); | |
553 size_t dim = 1; | |
554 while (tv.ty == TY.Tsarray) | |
555 { TypeSArray ta = cast(TypeSArray)tv; | |
556 dim *= (cast(TypeSArray)tv).dim.toInteger(); | |
557 tv = tv.nextOf().toBasetype(); | |
0 | 558 } |
79 | 559 if (tv.ty == TY.Tstruct) |
560 { TypeStruct ts = cast(TypeStruct)tv; | |
561 StructDeclaration sd = ts.sym; | |
562 if (sd.postblit) | |
563 { Expression ex; | |
564 | |
565 // this.v | |
566 ex = new ThisExp(Loc(0)); | |
567 ex = new DotVarExp(Loc(0), ex, v, 0); | |
568 | |
569 if (dim == 1) | |
570 { // this.v.postblit() | |
571 ex = new DotVarExp(Loc(0), ex, sd.postblit, 0); | |
572 ex = new CallExp(Loc(0), ex); | |
573 } | |
574 else | |
575 { | |
576 // Typeinfo.postblit(cast(void*)&this.v); | |
577 Expression ea = new AddrExp(Loc(0), ex); | |
578 ea = new CastExp(Loc(0), ea, Type.tvoid.pointerTo()); | |
579 | |
580 Expression et = v.type.getTypeInfo(sc); | |
581 et = new DotIdExp(Loc(0), et, Id.postblit); | |
582 | |
583 ex = new CallExp(Loc(0), et, ea); | |
584 } | |
585 e = Expression.combine(e, ex); // combine in forward order | |
586 } | |
0 | 587 } |
588 } | |
589 | |
590 /* Build our own "postblit" which executes e | |
591 */ | |
592 if (e) | |
593 { //printf("Building __fieldPostBlit()\n"); | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
72
diff
changeset
|
594 auto dd = new PostBlitDeclaration(Loc(0), Loc(0), Lexer.idPool("__fieldPostBlit")); |
0 | 595 dd.fbody = new ExpStatement(Loc(0), e); |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
72
diff
changeset
|
596 postblits.shift(dd); |
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
72
diff
changeset
|
597 members.push(dd); |
0 | 598 dd.semantic(sc); |
599 } | |
600 | |
601 switch (postblits.dim) | |
602 { | |
603 case 0: | |
604 return null; | |
605 | |
606 case 1: | |
77
ad4792a1cfd6
more D-ification container accessing
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
74
diff
changeset
|
607 return cast(FuncDeclaration)postblits[0]; |
0 | 608 |
609 default: | |
610 e = null; | |
77
ad4792a1cfd6
more D-ification container accessing
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
74
diff
changeset
|
611 foreach(FuncDeclaration fd; postblits) |
ad4792a1cfd6
more D-ification container accessing
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
74
diff
changeset
|
612 { |
0 | 613 Expression ex = new ThisExp(Loc(0)); |
614 ex = new DotVarExp(Loc(0), ex, fd, 0); | |
615 ex = new CallExp(Loc(0), ex); | |
616 e = Expression.combine(e, ex); | |
617 } | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
72
diff
changeset
|
618 auto dd = new PostBlitDeclaration(Loc(0), Loc(0), Lexer.idPool("__aggrPostBlit")); |
0 | 619 dd.fbody = new ExpStatement(Loc(0), e); |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
72
diff
changeset
|
620 members.push(dd); |
0 | 621 dd.semantic(sc); |
622 return dd; | |
623 } | |
624 } | |
625 } | |
626 | |
627 /******************************************* | |
628 * Build copy constructor for struct. | |
629 * Copy constructors are compiler generated only, and are only | |
630 * callable from the compiler. They are not user accessible. | |
631 * A copy constructor is: | |
632 * void cpctpr(ref S s) | |
633 * { | |
634 * *this = s; | |
635 * this.postBlit(); | |
636 * } | |
637 * This is done so: | |
638 * - postBlit() never sees uninitialized data | |
639 * - memcpy can be much more efficient than memberwise copy | |
640 * - no fields are overlooked | |
641 */ | |
642 FuncDeclaration buildCpCtor(Scope sc) | |
643 { | |
644 //printf("StructDeclaration.buildCpCtor() %s\n", toChars()); | |
645 FuncDeclaration fcp = null; | |
646 | |
647 /* Copy constructor is only necessary if there is a postblit function, | |
648 * otherwise the code generator will just do a bit copy. | |
649 */ | |
650 if (postblit) | |
651 { | |
652 //printf("generating cpctor\n"); | |
653 | |
654 Argument param = new Argument(STC.STCref, type, Id.p, null); | |
655 Arguments fparams = new Arguments; | |
656 fparams.push(cast(void*)param); | |
657 Type ftype = new TypeFunction(fparams, Type.tvoid, false, LINK.LINKd); | |
658 | |
659 fcp = new FuncDeclaration(Loc(0), Loc(0), Id.cpctor, STC.STCundefined, ftype); | |
660 | |
661 // Build *this = p; | |
662 Expression e = new ThisExp(Loc(0)); | |
663 version (STRUCTTHISREF) { | |
664 } else { | |
665 e = new PtrExp(Loc(0), e); | |
666 } | |
667 AssignExp ea = new AssignExp(Loc(0), e, new IdentifierExp(Loc(0), Id.p)); | |
668 ea.op = TOK.TOKblit; | |
669 Statement s = new ExpStatement(Loc(0), ea); | |
670 | |
671 // Build postBlit(); | |
672 e = new VarExp(Loc(0), postblit, 0); | |
673 e = new CallExp(Loc(0), e); | |
674 | |
675 s = new CompoundStatement(Loc(0), s, new ExpStatement(Loc(0), e)); | |
676 fcp.fbody = s; | |
677 | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
72
diff
changeset
|
678 members.push(fcp); |
0 | 679 |
680 sc = sc.push(); | |
681 sc.stc = STC.STCundefined; | |
682 sc.linkage = LINK.LINKd; | |
683 | |
684 fcp.semantic(sc); | |
685 | |
686 sc.pop(); | |
687 } | |
688 | |
689 return fcp; | |
690 } | |
691 | |
72 | 692 override void toDocBuffer(OutBuffer buf) |
0 | 693 { |
694 assert(false); | |
695 } | |
696 | |
72 | 697 override PROT getAccess(Dsymbol smember) // determine access to smember |
0 | 698 { |
699 assert(false); | |
700 } | |
701 | |
72 | 702 override void toObjFile(int multiobj) // compile to .obj file |
0 | 703 { |
704 //printf("StructDeclaration.toObjFile('%s')\n", toChars()); | |
705 | |
706 if (multiobj) | |
707 { | |
708 obj_append(this); | |
709 return; | |
710 } | |
711 | |
712 // Anonymous structs/unions only exist as part of others, | |
713 // do not output forward referenced structs's | |
714 if (!isAnonymous() && members) | |
715 { | |
716 if (global.params.symdebug) { | |
717 toDebug(); | |
718 } | |
719 | |
720 type.getTypeInfo(null); // generate TypeInfo | |
721 | |
722 if (true) | |
723 { | |
724 // Generate static initializer | |
725 toInitializer(); | |
726 | |
727 static if (false) { | |
728 sinit.Sclass = SC.SCcomdat; | |
729 } else { | |
730 if (inTemplateInstance()) | |
731 { | |
732 sinit.Sclass = SC.SCcomdat; | |
733 } | |
734 else | |
735 { | |
736 sinit.Sclass = SC.SCglobal; | |
737 } | |
738 } | |
739 sinit.Sfl = FL.FLdata; | |
740 | |
741 toDt(&sinit.Sdt); | |
742 | |
743 version (OMFOBJ) { | |
744 /* For OMF, common blocks aren't pulled in from the library. | |
745 */ | |
746 /* ELF comdef's generate multiple | |
747 * definition errors for them from the gnu linker. | |
748 * Need to figure out how to generate proper comdef's for ELF. | |
749 */ | |
750 // See if we can convert a comdat to a comdef, | |
751 // which saves on exe file space. | |
752 if (sinit.Sclass == SCcomdat && | |
753 sinit.Sdt && | |
754 sinit.Sdt.dt == DT.DT_azeros && | |
755 sinit.Sdt.DTnext == null && | |
756 !global.params.multiobj) | |
757 { | |
758 sinit.Sclass = SC.SCglobal; | |
759 sinit.Sdt.dt = DT.DT_common; | |
760 } | |
761 } | |
762 | |
763 version (ELFOBJ) { | |
764 sinit.Sseg = Segment.CDATA; | |
765 } | |
766 version (MACHOBJ) { | |
767 sinit.Sseg = Segment.DATA; | |
768 } | |
769 outdata(sinit); | |
770 } | |
771 | |
772 // Put out the members | |
77
ad4792a1cfd6
more D-ification container accessing
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
74
diff
changeset
|
773 foreach(Dsymbol member; members) |
0 | 774 member.toObjFile(0); |
775 } | |
776 } | |
777 | |
778 void toDt(dt_t** pdt) | |
779 { | |
780 uint offset; | |
781 dt_t* dt; | |
782 | |
783 //printf("StructDeclaration.toDt(), this='%s'\n", toChars()); | |
784 offset = 0; | |
785 | |
786 // Note equivalence of this loop to class's | |
79 | 787 for (uint i = 0; i < fields.dim; i++) |
0 | 788 { |
79 | 789 VarDeclaration v = cast(VarDeclaration)fields[i]; |
0 | 790 //printf("\tfield '%s' voffset %d, offset = %d\n", v.toChars(), v.offset, offset); |
791 dt = null; | |
792 int sz; | |
793 | |
794 if (v.storage_class & STC.STCref) | |
795 { | |
796 sz = PTRSIZE; | |
797 if (v.offset >= offset) | |
798 dtnzeros(&dt, sz); | |
799 } | |
800 else | |
801 { | |
802 sz = cast(uint)v.type.size(); | |
803 Initializer init = v.init; | |
804 if (init) | |
805 { | |
806 //printf("\t\thas initializer %s\n", init.toChars()); | |
807 ExpInitializer ei = init.isExpInitializer(); | |
808 Type tb = v.type.toBasetype(); | |
809 if (ei && tb.ty == TY.Tsarray) | |
810 (cast(TypeSArray)tb).toDtElem(&dt, ei.exp); | |
811 else | |
812 dt = init.toDt(); | |
813 } | |
814 else if (v.offset >= offset) | |
815 v.type.toDt(&dt); | |
816 } | |
817 if (dt) | |
818 { | |
819 if (v.offset < offset) | |
820 error("overlapping initialization for struct %s.%s", toChars(), v.toChars()); | |
821 else | |
822 { | |
823 if (offset < v.offset) | |
824 dtnzeros(pdt, v.offset - offset); | |
825 dtcat(pdt, dt); | |
826 offset = v.offset + sz; | |
827 } | |
828 } | |
829 } | |
830 | |
831 if (offset < structsize) | |
832 dtnzeros(pdt, structsize - offset); | |
833 | |
834 dt_optimize(*pdt); | |
835 } | |
836 | |
837 void toDebug() // to symbolic debug info | |
838 { | |
839 assert(false); | |
840 } | |
841 | |
72 | 842 override StructDeclaration isStructDeclaration() { return this; } |
843 } |