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