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