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