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