Mercurial > projects > ddmd
annotate dmd/StructDeclaration.d @ 79:43073c7c7769
updated to 2.035
also implemented a few missing functions
still crashes in Import.importAll though
author | Trass3r |
---|---|
date | Mon, 30 Aug 2010 03:57:51 +0200 |
parents | ad4792a1cfd6 |
children | be2ab491772e b17640f0e4e8 |
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 int i; | |
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 { | |
234 Arguments arguments = new Arguments; | |
235 Argument arg = new Argument(STC.STCin, handle, Id.p, null); | |
236 | |
237 arguments.push(cast(void*)arg); | |
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 { | |
244 Arguments arguments = new Arguments; | |
245 Argument arg = new Argument(STC.STCin, type, null, null); | |
246 | |
247 arguments.push(cast(void*)arg); | |
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); | |
266 Expressions args = new Expressions(); | |
267 args.push(cast(void*)e); | |
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 | |
299 scope_ = scx ? scx : new Scope(sc); | |
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; | |
79 | 325 foreach (Dsymbol s; fields) |
0 | 326 { |
79 | 327 VarDeclaration vd = s.isVarDeclaration(); |
328 if (vd && !vd.isDataseg()) | |
0 | 329 { |
79 | 330 if (vd.init) |
331 { | |
332 // Should examine init to see if it is really all 0's | |
0 | 333 zeroInit = false; |
334 break; | |
79 | 335 } |
336 else | |
337 { | |
338 if (!vd.type.isZeroInit(loc)) | |
339 { | |
340 zeroInit = false; | |
341 break; | |
342 } | |
343 } | |
0 | 344 } |
345 } | |
346 | |
347 /* Look for special member functions. | |
348 */ | |
349 version (DMDV2) { | |
350 ctor = search(Loc(0), Id.ctor, 0); | |
351 } | |
352 inv = cast(InvariantDeclaration)search(Loc(0), Id.classInvariant, 0); | |
353 aggNew = cast(NewDeclaration)search(Loc(0), Id.classNew, 0); | |
354 aggDelete = cast(DeleteDeclaration)search(Loc(0), Id.classDelete, 0); | |
355 | |
356 if (sc.func) | |
357 { | |
358 semantic2(sc); | |
359 semantic3(sc); | |
360 } | |
361 } | |
362 | |
72 | 363 override void toCBuffer(OutBuffer buf, HdrGenState* hgs) |
0 | 364 { |
365 assert(false); | |
366 } | |
367 | |
72 | 368 override string mangle() |
0 | 369 { |
370 //printf("StructDeclaration.mangle() '%s'\n", toChars()); | |
371 return Dsymbol.mangle(); | |
372 } | |
373 | |
72 | 374 override string kind() |
0 | 375 { |
376 assert(false); | |
377 } | |
378 | |
379 /******************************************* | |
380 * We need an opAssign for the struct if | |
381 * it has a destructor or a postblit. | |
382 * We need to generate one if a user-specified one does not exist. | |
383 */ | |
384 bool needOpAssign() | |
385 { | |
386 static if (false) { | |
387 printf("StructDeclaration.needOpAssign() %s\n", toChars()); | |
388 } | |
389 if (hasIdentityAssign) | |
390 goto Ldontneed; | |
391 | |
392 if (dtor || postblit) | |
393 goto Lneed; | |
394 | |
395 /* If any of the fields need an opAssign, then we | |
396 * need it too. | |
397 */ | |
79 | 398 foreach (Dsymbol s; fields) |
0 | 399 { |
400 VarDeclaration v = s.isVarDeclaration(); | |
401 assert(v && v.storage_class & STC.STCfield); | |
402 if (v.storage_class & STC.STCref) | |
403 continue; | |
404 Type tv = v.type.toBasetype(); | |
405 while (tv.ty == TY.Tsarray) | |
406 { TypeSArray ta = cast(TypeSArray)tv; | |
407 tv = tv.nextOf().toBasetype(); | |
408 } | |
409 if (tv.ty == TY.Tstruct) | |
410 { TypeStruct ts = cast(TypeStruct)tv; | |
411 StructDeclaration sd = ts.sym; | |
412 if (sd.needOpAssign()) | |
413 goto Lneed; | |
414 } | |
415 } | |
416 Ldontneed: | |
417 static if (false) { | |
418 printf("\tdontneed\n"); | |
419 } | |
420 return false; | |
421 | |
422 Lneed: | |
423 static if (false) { | |
424 printf("\tneed\n"); | |
425 } | |
426 return true; | |
427 } | |
428 | |
429 /****************************************** | |
430 * Build opAssign for struct. | |
431 * S* opAssign(S s) { ... } | |
432 */ | |
433 FuncDeclaration buildOpAssign(Scope sc) | |
434 { | |
435 if (!needOpAssign()) | |
436 return null; | |
437 | |
438 //printf("StructDeclaration.buildOpAssign() %s\n", toChars()); | |
439 | |
440 FuncDeclaration fop = null; | |
441 | |
442 Argument param = new Argument(STC.STCnodtor, type, Id.p, null); | |
443 Arguments fparams = new Arguments; | |
444 fparams.push(cast(void*)param); | |
445 Type ftype = new TypeFunction(fparams, handle, false, LINK.LINKd); | |
446 version (STRUCTTHISREF) { | |
447 (cast(TypeFunction)ftype).isref = 1; | |
448 } | |
449 | |
450 fop = new FuncDeclaration(Loc(0), Loc(0), Id.assign, STC.STCundefined, ftype); | |
451 | |
452 Expression e = null; | |
453 if (postblit) | |
454 { /* Swap: | |
455 * tmp = *this; *this = s; tmp.dtor(); | |
456 */ | |
457 //printf("\tswap copy\n"); | |
458 Identifier idtmp = Lexer.uniqueId("__tmp"); | |
459 VarDeclaration tmp; | |
460 AssignExp ec = null; | |
461 if (dtor) | |
462 { | |
463 tmp = new VarDeclaration(Loc(0), type, idtmp, new VoidInitializer(Loc(0))); | |
464 tmp.noauto = true; | |
465 e = new DeclarationExp(Loc(0), tmp); | |
466 | |
467 Expression e2; | |
468 version (STRUCTTHISREF) { | |
469 e2 = new ThisExp(Loc(0)); | |
470 } else { | |
471 e2 = new PtrExp(Loc(0), new ThisExp(Loc(0))); | |
472 } | |
473 ec = new AssignExp(Loc(0), new VarExp(Loc(0), tmp), e2); | |
474 ec.op = TOK.TOKblit; | |
475 e = Expression.combine(e, ec); | |
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 | |
484 ec = new AssignExp(Loc(0), e2, new IdentifierExp(Loc(0), Id.p)); | |
485 ec.op = TOK.TOKblit; | |
486 e = Expression.combine(e, ec); | |
487 if (dtor) | |
488 { | |
489 /* Instead of running the destructor on s, run it | |
490 * on tmp. This avoids needing to copy tmp back in to s. | |
491 */ | |
492 Expression ecc = new DotVarExp(Loc(0), new VarExp(Loc(0), tmp), dtor, 0); | |
493 ecc = new CallExp(Loc(0), ecc); | |
494 e = Expression.combine(e, ecc); | |
495 } | |
496 } | |
497 else | |
498 { /* Do memberwise copy | |
499 */ | |
500 //printf("\tmemberwise copy\n"); | |
79 | 501 foreach (Dsymbol s; fields) |
0 | 502 { |
503 VarDeclaration v = s.isVarDeclaration(); | |
504 assert(v && v.storage_class & STC.STCfield); | |
505 // this.v = s.v; | |
506 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)); | |
507 ec.op = TOK.TOKblit; | |
508 e = Expression.combine(e, ec); | |
509 } | |
510 } | |
511 Statement s1 = new ExpStatement(Loc(0), e); | |
512 | |
513 /* Add: | |
514 * return this; | |
515 */ | |
516 e = new ThisExp(Loc(0)); | |
517 Statement s2 = new ReturnStatement(Loc(0), e); | |
518 | |
519 fop.fbody = new CompoundStatement(Loc(0), s1, s2); | |
520 | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
72
diff
changeset
|
521 members.push(fop); |
13 | 522 fop.addMember(sc, this, true); |
0 | 523 |
524 sc = sc.push(); | |
525 sc.stc = STC.STCundefined; | |
526 sc.linkage = LINK.LINKd; | |
527 | |
528 fop.semantic(sc); | |
529 | |
530 sc.pop(); | |
531 | |
532 //printf("-StructDeclaration.buildOpAssign() %s\n", toChars()); | |
533 | |
534 return fop; | |
535 } | |
536 | |
537 /***************************************** | |
538 * Create inclusive postblit for struct by aggregating | |
539 * all the postblits in postblits[] with the postblits for | |
540 * all the members. | |
541 * Note the close similarity with AggregateDeclaration.buildDtor(), | |
542 * and the ordering changes (runs forward instead of backwards). | |
543 */ | |
544 | |
545 version (DMDV2) { | |
546 FuncDeclaration buildPostBlit(Scope sc) | |
547 { | |
548 //printf("StructDeclaration.buildPostBlit() %s\n", toChars()); | |
549 Expression e = null; | |
550 | |
79 | 551 foreach (Dsymbol s; fields) |
0 | 552 { |
79 | 553 VarDeclaration v = s.isVarDeclaration(); |
554 assert(v && v.storage_class & STC.STCfield); | |
555 if (v.storage_class & STC.STCref) | |
556 continue; | |
557 Type tv = v.type.toBasetype(); | |
558 size_t dim = 1; | |
559 while (tv.ty == TY.Tsarray) | |
560 { TypeSArray ta = cast(TypeSArray)tv; | |
561 dim *= (cast(TypeSArray)tv).dim.toInteger(); | |
562 tv = tv.nextOf().toBasetype(); | |
0 | 563 } |
79 | 564 if (tv.ty == TY.Tstruct) |
565 { TypeStruct ts = cast(TypeStruct)tv; | |
566 StructDeclaration sd = ts.sym; | |
567 if (sd.postblit) | |
568 { Expression ex; | |
569 | |
570 // this.v | |
571 ex = new ThisExp(Loc(0)); | |
572 ex = new DotVarExp(Loc(0), ex, v, 0); | |
573 | |
574 if (dim == 1) | |
575 { // this.v.postblit() | |
576 ex = new DotVarExp(Loc(0), ex, sd.postblit, 0); | |
577 ex = new CallExp(Loc(0), ex); | |
578 } | |
579 else | |
580 { | |
581 // Typeinfo.postblit(cast(void*)&this.v); | |
582 Expression ea = new AddrExp(Loc(0), ex); | |
583 ea = new CastExp(Loc(0), ea, Type.tvoid.pointerTo()); | |
584 | |
585 Expression et = v.type.getTypeInfo(sc); | |
586 et = new DotIdExp(Loc(0), et, Id.postblit); | |
587 | |
588 ex = new CallExp(Loc(0), et, ea); | |
589 } | |
590 e = Expression.combine(e, ex); // combine in forward order | |
591 } | |
0 | 592 } |
593 } | |
594 | |
595 /* Build our own "postblit" which executes e | |
596 */ | |
597 if (e) | |
598 { //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
|
599 auto dd = new PostBlitDeclaration(Loc(0), Loc(0), Lexer.idPool("__fieldPostBlit")); |
0 | 600 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
|
601 postblits.shift(dd); |
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
72
diff
changeset
|
602 members.push(dd); |
0 | 603 dd.semantic(sc); |
604 } | |
605 | |
606 switch (postblits.dim) | |
607 { | |
608 case 0: | |
609 return null; | |
610 | |
611 case 1: | |
77
ad4792a1cfd6
more D-ification container accessing
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
74
diff
changeset
|
612 return cast(FuncDeclaration)postblits[0]; |
0 | 613 |
614 default: | |
615 e = null; | |
77
ad4792a1cfd6
more D-ification container accessing
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
74
diff
changeset
|
616 foreach(FuncDeclaration fd; postblits) |
ad4792a1cfd6
more D-ification container accessing
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
74
diff
changeset
|
617 { |
0 | 618 Expression ex = new ThisExp(Loc(0)); |
619 ex = new DotVarExp(Loc(0), ex, fd, 0); | |
620 ex = new CallExp(Loc(0), ex); | |
621 e = Expression.combine(e, ex); | |
622 } | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
72
diff
changeset
|
623 auto dd = new PostBlitDeclaration(Loc(0), Loc(0), Lexer.idPool("__aggrPostBlit")); |
0 | 624 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
|
625 members.push(dd); |
0 | 626 dd.semantic(sc); |
627 return dd; | |
628 } | |
629 } | |
630 } | |
631 | |
632 /******************************************* | |
633 * Build copy constructor for struct. | |
634 * Copy constructors are compiler generated only, and are only | |
635 * callable from the compiler. They are not user accessible. | |
636 * A copy constructor is: | |
637 * void cpctpr(ref S s) | |
638 * { | |
639 * *this = s; | |
640 * this.postBlit(); | |
641 * } | |
642 * This is done so: | |
643 * - postBlit() never sees uninitialized data | |
644 * - memcpy can be much more efficient than memberwise copy | |
645 * - no fields are overlooked | |
646 */ | |
647 FuncDeclaration buildCpCtor(Scope sc) | |
648 { | |
649 //printf("StructDeclaration.buildCpCtor() %s\n", toChars()); | |
650 FuncDeclaration fcp = null; | |
651 | |
652 /* Copy constructor is only necessary if there is a postblit function, | |
653 * otherwise the code generator will just do a bit copy. | |
654 */ | |
655 if (postblit) | |
656 { | |
657 //printf("generating cpctor\n"); | |
658 | |
659 Argument param = new Argument(STC.STCref, type, Id.p, null); | |
660 Arguments fparams = new Arguments; | |
661 fparams.push(cast(void*)param); | |
662 Type ftype = new TypeFunction(fparams, Type.tvoid, false, LINK.LINKd); | |
663 | |
664 fcp = new FuncDeclaration(Loc(0), Loc(0), Id.cpctor, STC.STCundefined, ftype); | |
665 | |
666 // Build *this = p; | |
667 Expression e = new ThisExp(Loc(0)); | |
668 version (STRUCTTHISREF) { | |
669 } else { | |
670 e = new PtrExp(Loc(0), e); | |
671 } | |
672 AssignExp ea = new AssignExp(Loc(0), e, new IdentifierExp(Loc(0), Id.p)); | |
673 ea.op = TOK.TOKblit; | |
674 Statement s = new ExpStatement(Loc(0), ea); | |
675 | |
676 // Build postBlit(); | |
677 e = new VarExp(Loc(0), postblit, 0); | |
678 e = new CallExp(Loc(0), e); | |
679 | |
680 s = new CompoundStatement(Loc(0), s, new ExpStatement(Loc(0), e)); | |
681 fcp.fbody = s; | |
682 | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
72
diff
changeset
|
683 members.push(fcp); |
0 | 684 |
685 sc = sc.push(); | |
686 sc.stc = STC.STCundefined; | |
687 sc.linkage = LINK.LINKd; | |
688 | |
689 fcp.semantic(sc); | |
690 | |
691 sc.pop(); | |
692 } | |
693 | |
694 return fcp; | |
695 } | |
696 | |
72 | 697 override void toDocBuffer(OutBuffer buf) |
0 | 698 { |
699 assert(false); | |
700 } | |
701 | |
72 | 702 override PROT getAccess(Dsymbol smember) // determine access to smember |
0 | 703 { |
704 assert(false); | |
705 } | |
706 | |
72 | 707 override void toObjFile(int multiobj) // compile to .obj file |
0 | 708 { |
709 //printf("StructDeclaration.toObjFile('%s')\n", toChars()); | |
710 | |
711 if (multiobj) | |
712 { | |
713 obj_append(this); | |
714 return; | |
715 } | |
716 | |
717 // Anonymous structs/unions only exist as part of others, | |
718 // do not output forward referenced structs's | |
719 if (!isAnonymous() && members) | |
720 { | |
721 if (global.params.symdebug) { | |
722 toDebug(); | |
723 } | |
724 | |
725 type.getTypeInfo(null); // generate TypeInfo | |
726 | |
727 if (true) | |
728 { | |
729 // Generate static initializer | |
730 toInitializer(); | |
731 | |
732 static if (false) { | |
733 sinit.Sclass = SC.SCcomdat; | |
734 } else { | |
735 if (inTemplateInstance()) | |
736 { | |
737 sinit.Sclass = SC.SCcomdat; | |
738 } | |
739 else | |
740 { | |
741 sinit.Sclass = SC.SCglobal; | |
742 } | |
743 } | |
744 sinit.Sfl = FL.FLdata; | |
745 | |
746 toDt(&sinit.Sdt); | |
747 | |
748 version (OMFOBJ) { | |
749 /* For OMF, common blocks aren't pulled in from the library. | |
750 */ | |
751 /* ELF comdef's generate multiple | |
752 * definition errors for them from the gnu linker. | |
753 * Need to figure out how to generate proper comdef's for ELF. | |
754 */ | |
755 // See if we can convert a comdat to a comdef, | |
756 // which saves on exe file space. | |
757 if (sinit.Sclass == SCcomdat && | |
758 sinit.Sdt && | |
759 sinit.Sdt.dt == DT.DT_azeros && | |
760 sinit.Sdt.DTnext == null && | |
761 !global.params.multiobj) | |
762 { | |
763 sinit.Sclass = SC.SCglobal; | |
764 sinit.Sdt.dt = DT.DT_common; | |
765 } | |
766 } | |
767 | |
768 version (ELFOBJ) { | |
769 sinit.Sseg = Segment.CDATA; | |
770 } | |
771 version (MACHOBJ) { | |
772 sinit.Sseg = Segment.DATA; | |
773 } | |
774 outdata(sinit); | |
775 } | |
776 | |
777 // Put out the members | |
77
ad4792a1cfd6
more D-ification container accessing
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
74
diff
changeset
|
778 foreach(Dsymbol member; members) |
0 | 779 member.toObjFile(0); |
780 } | |
781 } | |
782 | |
783 void toDt(dt_t** pdt) | |
784 { | |
785 uint offset; | |
786 dt_t* dt; | |
787 | |
788 //printf("StructDeclaration.toDt(), this='%s'\n", toChars()); | |
789 offset = 0; | |
790 | |
791 // Note equivalence of this loop to class's | |
79 | 792 for (uint i = 0; i < fields.dim; i++) |
0 | 793 { |
79 | 794 VarDeclaration v = cast(VarDeclaration)fields[i]; |
0 | 795 //printf("\tfield '%s' voffset %d, offset = %d\n", v.toChars(), v.offset, offset); |
796 dt = null; | |
797 int sz; | |
798 | |
799 if (v.storage_class & STC.STCref) | |
800 { | |
801 sz = PTRSIZE; | |
802 if (v.offset >= offset) | |
803 dtnzeros(&dt, sz); | |
804 } | |
805 else | |
806 { | |
807 sz = cast(uint)v.type.size(); | |
808 Initializer init = v.init; | |
809 if (init) | |
810 { | |
811 //printf("\t\thas initializer %s\n", init.toChars()); | |
812 ExpInitializer ei = init.isExpInitializer(); | |
813 Type tb = v.type.toBasetype(); | |
814 if (ei && tb.ty == TY.Tsarray) | |
815 (cast(TypeSArray)tb).toDtElem(&dt, ei.exp); | |
816 else | |
817 dt = init.toDt(); | |
818 } | |
819 else if (v.offset >= offset) | |
820 v.type.toDt(&dt); | |
821 } | |
822 if (dt) | |
823 { | |
824 if (v.offset < offset) | |
825 error("overlapping initialization for struct %s.%s", toChars(), v.toChars()); | |
826 else | |
827 { | |
828 if (offset < v.offset) | |
829 dtnzeros(pdt, v.offset - offset); | |
830 dtcat(pdt, dt); | |
831 offset = v.offset + sz; | |
832 } | |
833 } | |
834 } | |
835 | |
836 if (offset < structsize) | |
837 dtnzeros(pdt, structsize - offset); | |
838 | |
839 dt_optimize(*pdt); | |
840 } | |
841 | |
842 void toDebug() // to symbolic debug info | |
843 { | |
844 assert(false); | |
845 } | |
846 | |
72 | 847 override StructDeclaration isStructDeclaration() { return this; } |
848 } |