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