Mercurial > projects > ldc
annotate dmd/struct.c @ 88:058d3925950e trunk
[svn r92] Fixed support for statically initialized unions. lots of bugfixes as cleanups too.
author | lindquist |
---|---|
date | Tue, 06 Nov 2007 10:03:14 +0100 |
parents | b706170e24a9 |
children | 5071469303d4 |
rev | line source |
---|---|
1 | 1 |
2 // Compiler implementation of the D programming language | |
3 // Copyright (c) 1999-2006 by Digital Mars | |
4 // All Rights Reserved | |
5 // written by Walter Bright | |
6 // http://www.digitalmars.com | |
7 // License for redistribution is by either the Artistic License | |
8 // in artistic.txt, or the GNU General Public License in gnu.txt. | |
9 // See the included readme.txt for details. | |
10 | |
11 #include <stdio.h> | |
12 #include <assert.h> | |
13 | |
14 #include "root.h" | |
15 #include "aggregate.h" | |
16 #include "scope.h" | |
17 #include "mtype.h" | |
18 #include "declaration.h" | |
19 #include "module.h" | |
20 #include "id.h" | |
21 #include "statement.h" | |
22 | |
23 /********************************* AggregateDeclaration ****************************/ | |
24 | |
25 AggregateDeclaration::AggregateDeclaration(Loc loc, Identifier *id) | |
26 : ScopeDsymbol(id) | |
27 { | |
28 this->loc = loc; | |
29 | |
30 storage_class = 0; | |
31 protection = PROTpublic; | |
32 type = NULL; | |
33 handle = NULL; | |
34 structsize = 0; // size of struct | |
35 alignsize = 0; // size of struct for alignment purposes | |
36 structalign = 0; // struct member alignment in effect | |
37 hasUnions = 0; | |
38 sizeok = 0; // size not determined yet | |
39 isdeprecated = 0; | |
40 inv = NULL; | |
41 aggNew = NULL; | |
42 aggDelete = NULL; | |
43 | |
44 stag = NULL; | |
45 sinit = NULL; | |
46 scope = NULL; | |
47 | |
48 llvmType = NULL; | |
49 llvmVtbl = NULL; | |
54
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
1
diff
changeset
|
50 llvmConstVtbl = NULL; |
1 | 51 llvmInitZ = NULL; |
52 llvmInProgress = false; | |
73 | 53 llvmHasUnions = false; |
88
058d3925950e
[svn r92] Fixed support for statically initialized unions. lots of bugfixes as cleanups too.
lindquist
parents:
73
diff
changeset
|
54 llvmUnion = NULL; |
1 | 55 } |
56 | |
57 enum PROT AggregateDeclaration::prot() | |
58 { | |
59 return protection; | |
60 } | |
61 | |
62 void AggregateDeclaration::semantic2(Scope *sc) | |
63 { | |
64 //printf("AggregateDeclaration::semantic2(%s)\n", toChars()); | |
65 if (scope) | |
66 { error("has forward references"); | |
67 return; | |
68 } | |
69 if (members) | |
70 { | |
71 sc = sc->push(this); | |
72 for (size_t i = 0; i < members->dim; i++) | |
73 { | |
74 Dsymbol *s = (Dsymbol *)members->data[i]; | |
75 s->semantic2(sc); | |
76 } | |
77 sc->pop(); | |
78 } | |
79 } | |
80 | |
81 void AggregateDeclaration::semantic3(Scope *sc) | |
82 { int i; | |
83 | |
84 //printf("AggregateDeclaration::semantic3(%s)\n", toChars()); | |
85 if (members) | |
86 { | |
87 sc = sc->push(this); | |
88 for (i = 0; i < members->dim; i++) | |
89 { | |
90 Dsymbol *s = (Dsymbol *)members->data[i]; | |
91 s->semantic3(sc); | |
92 } | |
93 sc->pop(); | |
94 } | |
95 } | |
96 | |
97 void AggregateDeclaration::inlineScan() | |
98 { int i; | |
99 | |
100 //printf("AggregateDeclaration::inlineScan(%s)\n", toChars()); | |
101 if (members) | |
102 { | |
103 for (i = 0; i < members->dim; i++) | |
104 { | |
105 Dsymbol *s = (Dsymbol *)members->data[i]; | |
106 //printf("inline scan aggregate symbol '%s'\n", s->toChars()); | |
107 s->inlineScan(); | |
108 } | |
109 } | |
110 } | |
111 | |
112 unsigned AggregateDeclaration::size(Loc loc) | |
113 { | |
114 //printf("AggregateDeclaration::size() = %d\n", structsize); | |
115 if (!members) | |
116 error(loc, "unknown size"); | |
117 if (sizeok != 1) | |
118 { error(loc, "no size yet for forward reference"); | |
119 //*(char*)0=0; | |
120 } | |
121 return structsize; | |
122 } | |
123 | |
124 Type *AggregateDeclaration::getType() | |
125 { | |
126 return type; | |
127 } | |
128 | |
129 int AggregateDeclaration::isDeprecated() | |
130 { | |
131 return isdeprecated; | |
132 } | |
133 | |
134 /**************************** | |
135 * Do byte or word alignment as necessary. | |
136 * Align sizes of 0, as we may not know array sizes yet. | |
137 */ | |
138 | |
139 void AggregateDeclaration::alignmember(unsigned salign, unsigned size, unsigned *poffset) | |
140 { | |
141 //printf("salign = %d, size = %d, offset = %d\n",salign,size,offset); | |
142 if (salign > 1) | |
143 { int sa; | |
144 | |
145 switch (size) | |
146 { case 1: | |
147 break; | |
148 case 2: | |
149 case_2: | |
150 *poffset = (*poffset + 1) & ~1; // align to word | |
151 break; | |
152 case 3: | |
153 case 4: | |
154 if (salign == 2) | |
155 goto case_2; | |
156 *poffset = (*poffset + 3) & ~3; // align to dword | |
157 break; | |
158 default: | |
159 *poffset = (*poffset + salign - 1) & ~(salign - 1); | |
160 break; | |
161 } | |
162 } | |
163 //printf("result = %d\n",offset); | |
164 } | |
165 | |
166 | |
167 void AggregateDeclaration::addField(Scope *sc, VarDeclaration *v) | |
168 { | |
169 unsigned memsize; // size of member | |
170 unsigned memalignsize; // size of member for alignment purposes | |
171 unsigned xalign; // alignment boundaries | |
172 | |
173 //printf("AggregateDeclaration::addField('%s') %s\n", v->toChars(), toChars()); | |
174 | |
175 // Check for forward referenced types which will fail the size() call | |
176 Type *t = v->type->toBasetype(); | |
177 if (t->ty == Tstruct /*&& isStructDeclaration()*/) | |
178 { TypeStruct *ts = (TypeStruct *)t; | |
179 | |
180 if (ts->sym->sizeok != 1) | |
181 { | |
182 sizeok = 2; // cannot finish; flag as forward referenced | |
183 return; | |
184 } | |
185 } | |
186 if (t->ty == Tident) | |
187 { | |
188 sizeok = 2; // cannot finish; flag as forward referenced | |
189 return; | |
190 } | |
191 | |
192 memsize = v->type->size(loc); | |
193 memalignsize = v->type->alignsize(); | |
194 xalign = v->type->memalign(sc->structalign); | |
195 alignmember(xalign, memalignsize, &sc->offset); | |
196 v->offset = sc->offset; | |
197 sc->offset += memsize; | |
198 if (sc->offset > structsize) | |
199 structsize = sc->offset; | |
200 if (sc->structalign < memalignsize) | |
201 memalignsize = sc->structalign; | |
202 if (alignsize < memalignsize) | |
203 alignsize = memalignsize; | |
204 //printf("\talignsize = %d\n", alignsize); | |
205 | |
206 v->storage_class |= STCfield; | |
207 //printf(" addField '%s' to '%s' at offset %d, size = %d\n", v->toChars(), toChars(), v->offset, memsize); | |
208 fields.push(v); | |
209 } | |
210 | |
211 | |
212 /********************************* StructDeclaration ****************************/ | |
213 | |
214 StructDeclaration::StructDeclaration(Loc loc, Identifier *id) | |
215 : AggregateDeclaration(loc, id) | |
216 { | |
217 zeroInit = 0; // assume false until we do semantic processing | |
218 | |
219 // For forward references | |
220 type = new TypeStruct(this); | |
221 } | |
222 | |
223 Dsymbol *StructDeclaration::syntaxCopy(Dsymbol *s) | |
224 { | |
225 StructDeclaration *sd; | |
226 | |
227 if (s) | |
228 sd = (StructDeclaration *)s; | |
229 else | |
230 sd = new StructDeclaration(loc, ident); | |
231 ScopeDsymbol::syntaxCopy(sd); | |
232 return sd; | |
233 } | |
234 | |
235 void StructDeclaration::semantic(Scope *sc) | |
236 { int i; | |
237 Scope *sc2; | |
238 | |
239 //printf("+StructDeclaration::semantic(this=%p, '%s')\n", this, toChars()); | |
240 | |
241 //static int count; if (++count == 20) *(char*)0=0; | |
242 | |
243 assert(type); | |
244 if (!members) // if forward reference | |
245 return; | |
246 | |
247 if (symtab) | |
248 { if (!scope) | |
249 return; // semantic() already completed | |
250 } | |
251 else | |
252 symtab = new DsymbolTable(); | |
253 | |
254 Scope *scx = NULL; | |
255 if (scope) | |
256 { sc = scope; | |
257 scx = scope; // save so we don't make redundant copies | |
258 scope = NULL; | |
259 } | |
260 | |
261 parent = sc->parent; | |
262 handle = type->pointerTo(); | |
263 structalign = sc->structalign; | |
264 protection = sc->protection; | |
265 assert(!isAnonymous()); | |
266 if (sc->stc & STCabstract) | |
267 error("structs, unions cannot be abstract"); | |
268 | |
269 if (sizeok == 0) // if not already done the addMember step | |
270 { | |
271 for (i = 0; i < members->dim; i++) | |
272 { | |
273 Dsymbol *s = (Dsymbol *)members->data[i]; | |
274 //printf("adding member '%s' to '%s'\n", s->toChars(), this->toChars()); | |
275 s->addMember(sc, this, 1); | |
276 } | |
277 } | |
278 | |
279 sizeok = 0; | |
280 sc2 = sc->push(this); | |
281 sc2->stc = 0; | |
282 sc2->parent = this; | |
283 if (isUnionDeclaration()) | |
284 sc2->inunion = 1; | |
285 sc2->protection = PROTpublic; | |
286 sc2->explicitProtection = 0; | |
287 | |
288 int members_dim = members->dim; | |
289 for (i = 0; i < members_dim; i++) | |
290 { | |
291 Dsymbol *s = (Dsymbol *)members->data[i]; | |
292 s->semantic(sc2); | |
293 if (isUnionDeclaration()) | |
294 sc2->offset = 0; | |
295 #if 0 | |
296 if (sizeok == 2) | |
297 { //printf("forward reference\n"); | |
298 break; | |
299 } | |
300 #endif | |
301 } | |
302 | |
303 /* The TypeInfo_Struct is expecting an opEquals and opCmp with | |
304 * a parameter that is a pointer to the struct. But if there | |
305 * isn't one, but is an opEquals or opCmp with a value, write | |
306 * another that is a shell around the value: | |
307 * int opCmp(struct *p) { return opCmp(*p); } | |
308 */ | |
309 | |
310 TypeFunction *tfeqptr; | |
311 { | |
312 Arguments *arguments = new Arguments; | |
313 Argument *arg = new Argument(STCin, handle, Id::p, NULL); | |
314 | |
315 arguments->push(arg); | |
316 tfeqptr = new TypeFunction(arguments, Type::tint32, 0, LINKd); | |
317 tfeqptr = (TypeFunction *)tfeqptr->semantic(0, sc); | |
318 } | |
319 | |
320 TypeFunction *tfeq; | |
321 { | |
322 Arguments *arguments = new Arguments; | |
323 Argument *arg = new Argument(STCin, type, NULL, NULL); | |
324 | |
325 arguments->push(arg); | |
326 tfeq = new TypeFunction(arguments, Type::tint32, 0, LINKd); | |
327 tfeq = (TypeFunction *)tfeq->semantic(0, sc); | |
328 } | |
329 | |
330 Identifier *id = Id::eq; | |
331 for (int i = 0; i < 2; i++) | |
332 { | |
333 Dsymbol *s = search_function(this, id); | |
334 FuncDeclaration *fdx = s ? s->isFuncDeclaration() : NULL; | |
335 if (fdx) | |
336 { FuncDeclaration *fd = fdx->overloadExactMatch(tfeqptr); | |
337 if (!fd) | |
338 { fd = fdx->overloadExactMatch(tfeq); | |
339 if (fd) | |
340 { // Create the thunk, fdptr | |
341 FuncDeclaration *fdptr = new FuncDeclaration(loc, loc, fdx->ident, STCundefined, tfeqptr); | |
342 Expression *e = new IdentifierExp(loc, Id::p); | |
343 e = new PtrExp(loc, e); | |
344 Expressions *args = new Expressions(); | |
345 args->push(e); | |
346 e = new IdentifierExp(loc, id); | |
347 e = new CallExp(loc, e, args); | |
348 fdptr->fbody = new ReturnStatement(loc, e); | |
349 ScopeDsymbol *s = fdx->parent->isScopeDsymbol(); | |
350 assert(s); | |
351 s->members->push(fdptr); | |
352 fdptr->addMember(sc, s, 1); | |
353 fdptr->semantic(sc2); | |
354 } | |
355 } | |
356 } | |
357 | |
358 id = Id::cmp; | |
359 } | |
360 | |
361 | |
362 sc2->pop(); | |
363 | |
364 if (sizeok == 2) | |
365 { // semantic() failed because of forward references. | |
366 // Unwind what we did, and defer it for later | |
367 fields.setDim(0); | |
368 structsize = 0; | |
369 alignsize = 0; | |
370 structalign = 0; | |
371 | |
372 scope = scx ? scx : new Scope(*sc); | |
373 scope->setNoFree(); | |
374 scope->module->addDeferredSemantic(this); | |
375 //printf("\tdeferring %s\n", toChars()); | |
376 return; | |
377 } | |
378 | |
379 // 0 sized struct's are set to 1 byte | |
380 if (structsize == 0) | |
381 { | |
382 structsize = 1; | |
383 alignsize = 1; | |
384 } | |
385 | |
386 // Round struct size up to next alignsize boundary. | |
387 // This will ensure that arrays of structs will get their internals | |
388 // aligned properly. | |
389 structsize = (structsize + alignsize - 1) & ~(alignsize - 1); | |
390 | |
391 sizeok = 1; | |
392 Module::dprogress++; | |
393 | |
394 //printf("-StructDeclaration::semantic(this=%p, '%s')\n", this, toChars()); | |
395 | |
396 // Determine if struct is all zeros or not | |
397 zeroInit = 1; | |
398 for (i = 0; i < fields.dim; i++) | |
399 { | |
400 Dsymbol *s = (Dsymbol *)fields.data[i]; | |
401 VarDeclaration *vd = s->isVarDeclaration(); | |
402 if (vd && !vd->isDataseg()) | |
403 { | |
404 if (vd->init) | |
405 { | |
406 // Should examine init to see if it is really all 0's | |
407 zeroInit = 0; | |
408 break; | |
409 } | |
410 else | |
411 { | |
412 if (!vd->type->isZeroInit()) | |
413 { | |
414 zeroInit = 0; | |
415 break; | |
416 } | |
417 } | |
418 } | |
419 } | |
420 | |
421 /* Look for special member functions. | |
422 */ | |
423 inv = (InvariantDeclaration *)search(0, Id::classInvariant, 0); | |
424 aggNew = (NewDeclaration *)search(0, Id::classNew, 0); | |
425 aggDelete = (DeleteDeclaration *)search(0, Id::classDelete, 0); | |
426 | |
427 if (sc->func) | |
428 { | |
429 semantic2(sc); | |
430 semantic3(sc); | |
431 } | |
432 } | |
433 | |
434 void StructDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
435 { int i; | |
436 | |
437 buf->printf("%s ", kind()); | |
438 if (!isAnonymous()) | |
439 buf->writestring(toChars()); | |
440 if (!members) | |
441 { | |
442 buf->writeByte(';'); | |
443 buf->writenl(); | |
444 return; | |
445 } | |
446 buf->writenl(); | |
447 buf->writeByte('{'); | |
448 buf->writenl(); | |
449 for (i = 0; i < members->dim; i++) | |
450 { | |
451 Dsymbol *s = (Dsymbol *)members->data[i]; | |
452 | |
453 buf->writestring(" "); | |
454 s->toCBuffer(buf, hgs); | |
455 } | |
456 buf->writeByte('}'); | |
457 buf->writenl(); | |
458 } | |
459 | |
460 | |
461 char *StructDeclaration::kind() | |
462 { | |
463 return "struct"; | |
464 } | |
465 | |
466 /********************************* UnionDeclaration ****************************/ | |
467 | |
468 UnionDeclaration::UnionDeclaration(Loc loc, Identifier *id) | |
469 : StructDeclaration(loc, id) | |
470 { | |
471 } | |
472 | |
473 Dsymbol *UnionDeclaration::syntaxCopy(Dsymbol *s) | |
474 { | |
475 UnionDeclaration *ud; | |
476 | |
477 if (s) | |
478 ud = (UnionDeclaration *)s; | |
479 else | |
480 ud = new UnionDeclaration(loc, ident); | |
481 StructDeclaration::syntaxCopy(ud); | |
482 return ud; | |
483 } | |
484 | |
485 | |
486 char *UnionDeclaration::kind() | |
487 { | |
488 return "union"; | |
489 } | |
490 | |
491 |