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