Mercurial > projects > ldc
annotate dmd/declaration.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 | 70d6113eeb8c |
children | a7dfa0ed966c |
rev | line source |
---|---|
1 | 1 |
2 // Compiler implementation of the D programming language | |
3 // Copyright (c) 1999-2007 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 "init.h" | |
15 #include "declaration.h" | |
16 #include "attrib.h" | |
17 #include "mtype.h" | |
18 #include "template.h" | |
19 #include "scope.h" | |
20 #include "aggregate.h" | |
21 #include "module.h" | |
22 #include "id.h" | |
23 #include "expression.h" | |
24 #include "hdrgen.h" | |
25 | |
26 /********************************* Declaration ****************************/ | |
27 | |
28 Declaration::Declaration(Identifier *id) | |
29 : Dsymbol(id) | |
30 { | |
31 type = NULL; | |
32 storage_class = STCundefined; | |
33 protection = PROTundefined; | |
50
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
34 linkage = LINKdefault; |
1 | 35 } |
36 | |
37 void Declaration::semantic(Scope *sc) | |
38 { | |
39 } | |
40 | |
41 char *Declaration::kind() | |
42 { | |
43 return "declaration"; | |
44 } | |
45 | |
46 unsigned Declaration::size(Loc loc) | |
47 { | |
48 assert(type); | |
49 return type->size(); | |
50 } | |
51 | |
52 int Declaration::isStaticConstructor() | |
53 { | |
54 return FALSE; | |
55 } | |
56 | |
57 int Declaration::isStaticDestructor() | |
58 { | |
59 return FALSE; | |
60 } | |
61 | |
62 int Declaration::isDelete() | |
63 { | |
64 return FALSE; | |
65 } | |
66 | |
67 int Declaration::isDataseg() | |
68 { | |
69 return FALSE; | |
70 } | |
71 | |
72 int Declaration::isCodeseg() | |
73 { | |
74 return FALSE; | |
75 } | |
76 | |
77 enum PROT Declaration::prot() | |
78 { | |
79 return protection; | |
80 } | |
81 | |
82 /********************************* TupleDeclaration ****************************/ | |
83 | |
84 TupleDeclaration::TupleDeclaration(Loc loc, Identifier *id, Objects *objects) | |
85 : Declaration(id) | |
86 { | |
87 this->type = NULL; | |
88 this->objects = objects; | |
89 this->isexp = 0; | |
90 this->tupletype = NULL; | |
91 } | |
92 | |
93 Dsymbol *TupleDeclaration::syntaxCopy(Dsymbol *s) | |
94 { | |
95 assert(0); | |
96 return NULL; | |
97 } | |
98 | |
99 char *TupleDeclaration::kind() | |
100 { | |
101 return "tuple"; | |
102 } | |
103 | |
104 Type *TupleDeclaration::getType() | |
105 { | |
106 /* If this tuple represents a type, return that type | |
107 */ | |
108 | |
109 //printf("TupleDeclaration::getType() %s\n", toChars()); | |
110 if (isexp) | |
111 return NULL; | |
112 if (!tupletype) | |
113 { | |
114 /* It's only a type tuple if all the Object's are types | |
115 */ | |
116 for (size_t i = 0; i < objects->dim; i++) | |
117 { Object *o = (Object *)objects->data[i]; | |
118 | |
119 if (o->dyncast() != DYNCAST_TYPE) | |
120 { | |
121 //printf("\tnot[%d], %p, %d\n", i, o, o->dyncast()); | |
122 return NULL; | |
123 } | |
124 } | |
125 | |
126 /* We know it's a type tuple, so build the TypeTuple | |
127 */ | |
128 Arguments *args = new Arguments(); | |
129 args->setDim(objects->dim); | |
130 OutBuffer buf; | |
131 for (size_t i = 0; i < objects->dim; i++) | |
132 { Type *t = (Type *)objects->data[i]; | |
133 | |
134 //printf("type = %s\n", t->toChars()); | |
135 #if 0 | |
136 buf.printf("_%s_%d", ident->toChars(), i); | |
137 char *name = (char *)buf.extractData(); | |
138 Identifier *id = new Identifier(name, TOKidentifier); | |
139 Argument *arg = new Argument(STCin, t, id, NULL); | |
140 #else | |
141 Argument *arg = new Argument(STCin, t, NULL, NULL); | |
142 #endif | |
143 args->data[i] = (void *)arg; | |
144 } | |
145 | |
146 tupletype = new TypeTuple(args); | |
147 } | |
148 | |
149 return tupletype; | |
150 } | |
151 | |
152 int TupleDeclaration::needThis() | |
153 { | |
154 //printf("TupleDeclaration::needThis(%s)\n", toChars()); | |
155 for (size_t i = 0; i < objects->dim; i++) | |
156 { Object *o = (Object *)objects->data[i]; | |
157 if (o->dyncast() == DYNCAST_EXPRESSION) | |
158 { Expression *e = (Expression *)o; | |
159 if (e->op == TOKdsymbol) | |
160 { DsymbolExp *ve = (DsymbolExp *)e; | |
161 Declaration *d = ve->s->isDeclaration(); | |
162 if (d && d->needThis()) | |
163 { | |
164 return 1; | |
165 } | |
166 } | |
167 } | |
168 } | |
169 return 0; | |
170 } | |
171 | |
172 /********************************* TypedefDeclaration ****************************/ | |
173 | |
174 TypedefDeclaration::TypedefDeclaration(Loc loc, Identifier *id, Type *basetype, Initializer *init) | |
175 : Declaration(id) | |
176 { | |
177 this->type = new TypeTypedef(this); | |
178 this->basetype = basetype->toBasetype(); | |
179 this->init = init; | |
180 #ifdef _DH | |
181 this->htype = NULL; | |
182 this->hbasetype = NULL; | |
183 #endif | |
184 this->sem = 0; | |
185 this->inuse = 0; | |
186 this->loc = loc; | |
187 this->sinit = NULL; | |
188 } | |
189 | |
190 Dsymbol *TypedefDeclaration::syntaxCopy(Dsymbol *s) | |
191 { | |
192 Type *basetype = this->basetype->syntaxCopy(); | |
193 | |
194 Initializer *init = NULL; | |
195 if (this->init) | |
196 init = this->init->syntaxCopy(); | |
197 | |
198 assert(!s); | |
199 TypedefDeclaration *st; | |
200 st = new TypedefDeclaration(loc, ident, basetype, init); | |
201 #ifdef _DH | |
202 // Syntax copy for header file | |
203 if (!htype) // Don't overwrite original | |
204 { if (type) // Make copy for both old and new instances | |
205 { htype = type->syntaxCopy(); | |
206 st->htype = type->syntaxCopy(); | |
207 } | |
208 } | |
209 else // Make copy of original for new instance | |
210 st->htype = htype->syntaxCopy(); | |
211 if (!hbasetype) | |
212 { if (basetype) | |
213 { hbasetype = basetype->syntaxCopy(); | |
214 st->hbasetype = basetype->syntaxCopy(); | |
215 } | |
216 } | |
217 else | |
218 st->hbasetype = hbasetype->syntaxCopy(); | |
219 #endif | |
220 return st; | |
221 } | |
222 | |
223 void TypedefDeclaration::semantic(Scope *sc) | |
224 { | |
225 //printf("TypedefDeclaration::semantic(%s) sem = %d\n", toChars(), sem); | |
226 if (sem == 0) | |
227 { sem = 1; | |
228 basetype = basetype->semantic(loc, sc); | |
229 sem = 2; | |
230 type = type->semantic(loc, sc); | |
231 if (sc->parent->isFuncDeclaration() && init) | |
232 semantic2(sc); | |
233 } | |
234 else if (sem == 1) | |
235 { | |
236 error("circular definition"); | |
237 } | |
238 } | |
239 | |
240 void TypedefDeclaration::semantic2(Scope *sc) | |
241 { | |
242 //printf("TypedefDeclaration::semantic2(%s) sem = %d\n", toChars(), sem); | |
243 if (sem == 2) | |
244 { sem = 3; | |
245 if (init) | |
246 { | |
247 init = init->semantic(sc, basetype); | |
248 | |
249 ExpInitializer *ie = init->isExpInitializer(); | |
250 if (ie) | |
251 { | |
252 if (ie->exp->type == basetype) | |
253 ie->exp->type = type; | |
254 } | |
255 } | |
256 } | |
257 } | |
258 | |
259 char *TypedefDeclaration::kind() | |
260 { | |
261 return "typedef"; | |
262 } | |
263 | |
264 Type *TypedefDeclaration::getType() | |
265 { | |
266 return type; | |
267 } | |
268 | |
269 void TypedefDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
270 { | |
271 buf->writestring("typedef "); | |
272 basetype->toCBuffer(buf, ident, hgs); | |
273 if (init) | |
274 { | |
275 buf->writestring(" = "); | |
276 init->toCBuffer(buf, hgs); | |
277 } | |
278 buf->writeByte(';'); | |
279 buf->writenl(); | |
280 } | |
281 | |
282 /********************************* AliasDeclaration ****************************/ | |
283 | |
284 AliasDeclaration::AliasDeclaration(Loc loc, Identifier *id, Type *type) | |
285 : Declaration(id) | |
286 { | |
287 //printf("AliasDeclaration(id = '%s', type = %p)\n", id->toChars(), type); | |
288 //printf("type = '%s'\n", type->toChars()); | |
289 this->loc = loc; | |
290 this->type = type; | |
291 this->aliassym = NULL; | |
292 #ifdef _DH | |
293 this->htype = NULL; | |
294 this->haliassym = NULL; | |
295 #endif | |
296 this->overnext = NULL; | |
297 this->inSemantic = 0; | |
298 assert(type); | |
299 } | |
300 | |
301 AliasDeclaration::AliasDeclaration(Loc loc, Identifier *id, Dsymbol *s) | |
302 : Declaration(id) | |
303 { | |
304 //printf("AliasDeclaration(id = '%s', s = %p)\n", id->toChars(), s); | |
305 assert(s != this); | |
306 this->loc = loc; | |
307 this->type = NULL; | |
308 this->aliassym = s; | |
309 #ifdef _DH | |
310 this->htype = NULL; | |
311 this->haliassym = NULL; | |
312 #endif | |
313 this->overnext = NULL; | |
314 this->inSemantic = 0; | |
315 assert(s); | |
316 } | |
317 | |
318 Dsymbol *AliasDeclaration::syntaxCopy(Dsymbol *s) | |
319 { | |
320 assert(!s); | |
321 AliasDeclaration *sa; | |
322 if (type) | |
323 sa = new AliasDeclaration(loc, ident, type->syntaxCopy()); | |
324 else | |
325 sa = new AliasDeclaration(loc, ident, aliassym->syntaxCopy(NULL)); | |
326 #ifdef _DH | |
327 // Syntax copy for header file | |
328 if (!htype) // Don't overwrite original | |
329 { if (type) // Make copy for both old and new instances | |
330 { htype = type->syntaxCopy(); | |
331 sa->htype = type->syntaxCopy(); | |
332 } | |
333 } | |
334 else // Make copy of original for new instance | |
335 sa->htype = htype->syntaxCopy(); | |
336 if (!haliassym) | |
337 { if (aliassym) | |
338 { haliassym = aliassym->syntaxCopy(s); | |
339 sa->haliassym = aliassym->syntaxCopy(s); | |
340 } | |
341 } | |
342 else | |
343 sa->haliassym = haliassym->syntaxCopy(s); | |
344 #endif | |
345 return sa; | |
346 } | |
347 | |
348 void AliasDeclaration::semantic(Scope *sc) | |
349 { | |
350 //printf("AliasDeclaration::semantic() %s\n", toChars()); | |
351 if (aliassym) | |
352 { | |
353 if (aliassym->isTemplateInstance()) | |
354 aliassym->semantic(sc); | |
355 return; | |
356 } | |
357 this->inSemantic = 1; | |
358 | |
359 if (storage_class & STCconst) | |
360 error("cannot be const"); | |
361 | |
362 storage_class |= sc->stc & STCdeprecated; | |
363 | |
364 // Given: | |
365 // alias foo.bar.abc def; | |
366 // it is not knowable from the syntax whether this is an alias | |
367 // for a type or an alias for a symbol. It is up to the semantic() | |
368 // pass to distinguish. | |
369 // If it is a type, then type is set and getType() will return that | |
370 // type. If it is a symbol, then aliassym is set and type is NULL - | |
371 // toAlias() will return aliasssym. | |
372 | |
373 Dsymbol *s; | |
374 Type *t; | |
375 Expression *e; | |
376 | |
377 /* This section is needed because resolve() will: | |
378 * const x = 3; | |
379 * alias x y; | |
380 * try to alias y to 3. | |
381 */ | |
382 s = type->toDsymbol(sc); | |
383 if (s) | |
384 goto L2; // it's a symbolic alias | |
385 | |
386 //printf("alias type is %s\n", type->toChars()); | |
387 type->resolve(loc, sc, &e, &t, &s); | |
388 if (s) | |
389 { | |
390 goto L2; | |
391 } | |
392 else if (e) | |
393 { | |
394 // Try to convert Expression to Dsymbol | |
395 if (e->op == TOKvar) | |
396 { s = ((VarExp *)e)->var; | |
397 goto L2; | |
398 } | |
399 else if (e->op == TOKfunction) | |
400 { s = ((FuncExp *)e)->fd; | |
401 goto L2; | |
402 } | |
403 else | |
404 { error("cannot alias an expression %s", e->toChars()); | |
405 t = e->type; | |
406 } | |
407 } | |
408 else if (t) | |
409 type = t; | |
410 if (overnext) | |
411 ScopeDsymbol::multiplyDefined(0, this, overnext); | |
412 this->inSemantic = 0; | |
413 return; | |
414 | |
415 L2: | |
416 //printf("alias is a symbol %s %s\n", s->kind(), s->toChars()); | |
417 type = NULL; | |
418 VarDeclaration *v = s->isVarDeclaration(); | |
419 if (v && v->linkage == LINKdefault) | |
420 { | |
421 error("forward reference of %s", v->toChars()); | |
422 s = NULL; | |
423 } | |
424 else | |
425 { | |
426 FuncDeclaration *f = s->toAlias()->isFuncDeclaration(); | |
427 if (f) | |
428 { | |
429 if (overnext) | |
430 { | |
431 FuncAliasDeclaration *fa = new FuncAliasDeclaration(f); | |
432 if (!fa->overloadInsert(overnext)) | |
433 ScopeDsymbol::multiplyDefined(0, f, overnext); | |
434 overnext = NULL; | |
435 s = fa; | |
436 s->parent = sc->parent; | |
437 } | |
438 } | |
439 if (overnext) | |
440 ScopeDsymbol::multiplyDefined(0, s, overnext); | |
441 if (s == this) | |
442 { | |
443 assert(global.errors); | |
444 s = NULL; | |
445 } | |
446 } | |
447 aliassym = s; | |
448 this->inSemantic = 0; | |
449 } | |
450 | |
451 int AliasDeclaration::overloadInsert(Dsymbol *s) | |
452 { | |
453 /* Don't know yet what the aliased symbol is, so assume it can | |
454 * be overloaded and check later for correctness. | |
455 */ | |
456 | |
457 //printf("AliasDeclaration::overloadInsert('%s')\n", s->toChars()); | |
458 if (overnext == NULL) | |
459 { overnext = s; | |
460 return TRUE; | |
461 } | |
462 else | |
463 { | |
464 return overnext->overloadInsert(s); | |
465 } | |
466 } | |
467 | |
468 char *AliasDeclaration::kind() | |
469 { | |
470 return "alias"; | |
471 } | |
472 | |
473 Type *AliasDeclaration::getType() | |
474 { | |
475 return type; | |
476 } | |
477 | |
478 Dsymbol *AliasDeclaration::toAlias() | |
479 { | |
480 //printf("AliasDeclaration::toAlias('%s', this = %p, aliassym = %p, kind = '%s')\n", toChars(), this, aliassym, aliassym ? aliassym->kind() : ""); | |
481 assert(this != aliassym); | |
482 //static int count; if (++count == 10) *(char*)0=0; | |
483 if (inSemantic) | |
484 { error("recursive alias declaration"); | |
485 // return this; | |
486 } | |
487 Dsymbol *s = aliassym ? aliassym->toAlias() : this; | |
488 return s; | |
489 } | |
490 | |
491 void AliasDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
492 { | |
493 buf->writestring("alias "); | |
494 #if 0 && _DH | |
495 if (hgs->hdrgen) | |
496 { | |
497 if (haliassym) | |
498 { | |
499 haliassym->toCBuffer(buf, hgs); | |
500 buf->writeByte(' '); | |
501 buf->writestring(ident->toChars()); | |
502 } | |
503 else | |
504 htype->toCBuffer(buf, ident, hgs); | |
505 } | |
506 else | |
507 #endif | |
508 { | |
509 if (aliassym) | |
510 { | |
511 aliassym->toCBuffer(buf, hgs); | |
512 buf->writeByte(' '); | |
513 buf->writestring(ident->toChars()); | |
514 } | |
515 else | |
516 type->toCBuffer(buf, ident, hgs); | |
517 } | |
518 buf->writeByte(';'); | |
519 buf->writenl(); | |
520 } | |
521 | |
522 /********************************* VarDeclaration ****************************/ | |
523 | |
524 VarDeclaration::VarDeclaration(Loc loc, Type *type, Identifier *id, Initializer *init) | |
525 : Declaration(id) | |
526 { | |
527 //printf("VarDeclaration('%s')\n", id->toChars()); | |
528 #ifdef DEBUG | |
529 if (!type && !init) | |
530 { printf("VarDeclaration('%s')\n", id->toChars()); | |
531 //*(char*)0=0; | |
532 } | |
533 #endif | |
534 assert(type || init); | |
535 this->type = type; | |
536 this->init = init; | |
537 #ifdef _DH | |
538 this->htype = NULL; | |
539 this->hinit = NULL; | |
540 #endif | |
541 this->loc = loc; | |
542 offset = 0; | |
543 noauto = 0; | |
544 nestedref = 0; | |
545 inuse = 0; | |
546 ctorinit = 0; | |
547 aliassym = NULL; | |
548 onstack = 0; | |
549 canassign = 0; | |
550 value = NULL; | |
50
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
551 llvmNestedIndex = -1; |
73 | 552 llvmFieldIndex = -1; |
553 llvmFieldIndexOffset = 0; | |
82
d8dd47ef3973
[svn r86] Changed the way arguments are given storage. It is now detected if they will need it during semantic passes.
lindquist
parents:
73
diff
changeset
|
554 llvmNeedsStorage = false; |
100 | 555 llvmConstInit = NULL; |
556 llvmIRGlobal = NULL; | |
1 | 557 } |
558 | |
559 Dsymbol *VarDeclaration::syntaxCopy(Dsymbol *s) | |
560 { | |
561 //printf("VarDeclaration::syntaxCopy(%s)\n", toChars()); | |
562 | |
563 VarDeclaration *sv; | |
564 if (s) | |
565 { sv = (VarDeclaration *)s; | |
566 } | |
567 else | |
568 { | |
569 Initializer *init = NULL; | |
570 if (this->init) | |
571 { init = this->init->syntaxCopy(); | |
572 //init->isExpInitializer()->exp->print(); | |
573 //init->isExpInitializer()->exp->dump(0); | |
574 } | |
575 | |
576 sv = new VarDeclaration(loc, type ? type->syntaxCopy() : NULL, ident, init); | |
577 sv->storage_class = storage_class; | |
578 } | |
579 #ifdef _DH | |
580 // Syntax copy for header file | |
581 if (!htype) // Don't overwrite original | |
582 { if (type) // Make copy for both old and new instances | |
583 { htype = type->syntaxCopy(); | |
584 sv->htype = type->syntaxCopy(); | |
585 } | |
586 } | |
587 else // Make copy of original for new instance | |
588 sv->htype = htype->syntaxCopy(); | |
589 if (!hinit) | |
590 { if (init) | |
591 { hinit = init->syntaxCopy(); | |
592 sv->hinit = init->syntaxCopy(); | |
593 } | |
594 } | |
595 else | |
596 sv->hinit = hinit->syntaxCopy(); | |
597 #endif | |
598 return sv; | |
599 } | |
600 | |
601 void VarDeclaration::semantic(Scope *sc) | |
602 { | |
603 //printf("VarDeclaration::semantic('%s', parent = '%s')\n", toChars(), sc->parent->toChars()); | |
604 //printf("type = %s\n", type->toChars()); | |
605 //printf("linkage = %d\n", sc->linkage); | |
606 //if (strcmp(toChars(), "mul") == 0) halt(); | |
607 | |
608 storage_class |= sc->stc; | |
609 if (storage_class & STCextern && init) | |
610 error("extern symbols cannot have initializers"); | |
611 | |
612 /* If auto type inference, do the inference | |
613 */ | |
614 int inferred = 0; | |
615 if (!type) | |
616 { inuse++; | |
617 type = init->inferType(sc); | |
618 inuse--; | |
619 inferred = 1; | |
620 | |
621 /* This is a kludge to support the existing syntax for RAII | |
622 * declarations. | |
623 */ | |
624 storage_class &= ~STCauto; | |
625 } | |
626 else | |
627 type = type->semantic(loc, sc); | |
628 | |
629 type->checkDeprecated(loc, sc); | |
630 linkage = sc->linkage; | |
631 this->parent = sc->parent; | |
632 //printf("this = %p, parent = %p, '%s'\n", this, parent, parent->toChars()); | |
633 protection = sc->protection; | |
634 //printf("sc->stc = %x\n", sc->stc); | |
635 //printf("storage_class = %x\n", storage_class); | |
636 | |
637 Dsymbol *parent = toParent(); | |
638 FuncDeclaration *fd = parent->isFuncDeclaration(); | |
639 | |
640 Type *tb = type->toBasetype(); | |
641 if (tb->ty == Tvoid && !(storage_class & STClazy)) | |
642 { error("voids have no value"); | |
643 type = Type::terror; | |
644 tb = type; | |
645 } | |
646 if (tb->ty == Tfunction) | |
647 { error("cannot be declared to be a function"); | |
648 type = Type::terror; | |
649 tb = type; | |
650 } | |
651 if (tb->ty == Tstruct) | |
652 { TypeStruct *ts = (TypeStruct *)tb; | |
653 | |
654 if (!ts->sym->members) | |
655 { | |
656 error("no definition of struct %s", ts->toChars()); | |
657 } | |
658 } | |
659 | |
660 if (tb->ty == Ttuple) | |
661 { /* Instead, declare variables for each of the tuple elements | |
662 * and add those. | |
663 */ | |
664 TypeTuple *tt = (TypeTuple *)tb; | |
665 size_t nelems = Argument::dim(tt->arguments); | |
666 Objects *exps = new Objects(); | |
667 exps->setDim(nelems); | |
668 | |
669 for (size_t i = 0; i < nelems; i++) | |
670 { Argument *arg = Argument::getNth(tt->arguments, i); | |
671 | |
672 OutBuffer buf; | |
673 buf.printf("_%s_field_%zu", ident->toChars(), i); | |
674 buf.writeByte(0); | |
675 char *name = (char *)buf.extractData(); | |
676 Identifier *id = new Identifier(name, TOKidentifier); | |
677 | |
678 VarDeclaration *v = new VarDeclaration(loc, arg->type, id, NULL); | |
679 //printf("declaring field %s of type %s\n", v->toChars(), v->type->toChars()); | |
680 v->semantic(sc); | |
681 | |
682 if (sc->scopesym) | |
683 { //printf("adding %s to %s\n", v->toChars(), sc->scopesym->toChars()); | |
684 if (sc->scopesym->members) | |
685 sc->scopesym->members->push(v); | |
686 } | |
687 | |
688 Expression *e = new DsymbolExp(loc, v); | |
689 exps->data[i] = e; | |
690 } | |
691 TupleDeclaration *v2 = new TupleDeclaration(loc, ident, exps); | |
692 v2->isexp = 1; | |
693 aliassym = v2; | |
694 return; | |
695 } | |
696 | |
697 if (storage_class & STCconst && !init && !fd) | |
698 // Initialize by constructor only | |
699 storage_class = (storage_class & ~STCconst) | STCctorinit; | |
700 | |
701 if (isConst()) | |
702 { | |
703 } | |
704 else if (isStatic()) | |
705 { | |
706 } | |
707 else if (isSynchronized()) | |
708 { | |
709 error("variable %s cannot be synchronized", toChars()); | |
710 } | |
711 else if (isOverride()) | |
712 { | |
713 error("override cannot be applied to variable"); | |
714 } | |
715 else if (isAbstract()) | |
716 { | |
717 error("abstract cannot be applied to variable"); | |
718 } | |
719 else if (storage_class & STCtemplateparameter) | |
720 { | |
721 } | |
722 else | |
723 { | |
724 AggregateDeclaration *aad = sc->anonAgg; | |
725 if (!aad) | |
726 aad = parent->isAggregateDeclaration(); | |
727 if (aad) | |
728 { | |
729 aad->addField(sc, this); | |
730 } | |
731 | |
732 InterfaceDeclaration *id = parent->isInterfaceDeclaration(); | |
733 if (id) | |
734 { | |
735 error("field not allowed in interface"); | |
736 } | |
737 | |
738 TemplateInstance *ti = parent->isTemplateInstance(); | |
739 if (ti) | |
740 { | |
741 // Take care of nested templates | |
742 while (1) | |
743 { | |
744 TemplateInstance *ti2 = ti->tempdecl->parent->isTemplateInstance(); | |
745 if (!ti2) | |
746 break; | |
747 ti = ti2; | |
748 } | |
749 | |
750 // If it's a member template | |
751 AggregateDeclaration *ad = ti->tempdecl->isMember(); | |
752 if (ad && storage_class != STCundefined) | |
753 { | |
754 error("cannot use template to add field to aggregate '%s'", ad->toChars()); | |
755 } | |
756 } | |
757 } | |
758 | |
759 if (type->isauto() && !noauto) | |
760 { | |
761 if (storage_class & (STCfield | STCout | STCref | STCstatic) || !fd) | |
762 { | |
763 error("globals, statics, fields, ref and out parameters cannot be auto"); | |
764 } | |
765 | |
766 if (!(storage_class & (STCauto | STCscope))) | |
767 { | |
768 if (!(storage_class & STCparameter) && ident != Id::withSym) | |
769 error("reference to scope class must be scope"); | |
770 } | |
771 } | |
772 | |
773 if (!init && !sc->inunion && !isStatic() && !isConst() && fd && | |
92 | 774 !(storage_class & (STCfield | STCin | STCforeach)) && |
775 type->size() != 0) | |
1 | 776 { |
777 // Provide a default initializer | |
778 //printf("Providing default initializer for '%s'\n", toChars()); | |
779 if (type->ty == Tstruct && | |
780 ((TypeStruct *)type)->sym->zeroInit == 1) | |
19 | 781 { /* If a struct is all zeros, as a special case |
782 * set it's initializer to the integer 0. | |
783 * In AssignExp::toElem(), we check for this and issue | |
784 * a memset() to initialize the struct. | |
785 * Must do same check in interpreter. | |
786 */ | |
1 | 787 Expression *e = new IntegerExp(loc, 0, Type::tint32); |
788 Expression *e1; | |
789 e1 = new VarExp(loc, this); | |
790 e = new AssignExp(loc, e1, e); | |
791 e->type = e1->type; | |
792 init = new ExpInitializer(loc, e/*->type->defaultInit()*/); | |
793 return; | |
794 } | |
795 else if (type->ty == Ttypedef) | |
796 { TypeTypedef *td = (TypeTypedef *)type; | |
797 if (td->sym->init) | |
798 { init = td->sym->init; | |
799 ExpInitializer *ie = init->isExpInitializer(); | |
800 if (ie) | |
801 // Make copy so we can modify it | |
802 init = new ExpInitializer(ie->loc, ie->exp); | |
803 } | |
804 else | |
805 init = getExpInitializer(); | |
806 } | |
807 else | |
808 { | |
809 init = getExpInitializer(); | |
810 } | |
811 } | |
812 | |
813 if (init) | |
814 { | |
815 ArrayInitializer *ai = init->isArrayInitializer(); | |
19 | 816 if (ai && tb->ty == Taarray) |
1 | 817 { |
818 init = ai->toAssocArrayInitializer(); | |
819 } | |
820 | |
19 | 821 StructInitializer *si = init->isStructInitializer(); |
1 | 822 ExpInitializer *ei = init->isExpInitializer(); |
823 | |
824 // See if we can allocate on the stack | |
825 if (ei && isScope() && ei->exp->op == TOKnew) | |
826 { NewExp *ne = (NewExp *)ei->exp; | |
827 if (!(ne->newargs && ne->newargs->dim)) | |
828 { ne->onstack = 1; | |
829 onstack = 1; | |
830 if (type->isBaseOf(ne->newtype->semantic(loc, sc), NULL)) | |
831 onstack = 2; | |
832 } | |
833 } | |
834 | |
835 // If inside function, there is no semantic3() call | |
836 if (sc->func) | |
837 { | |
838 // If local variable, use AssignExp to handle all the various | |
839 // possibilities. | |
840 if (fd && !isStatic() && !isConst() && !init->isVoidInitializer()) | |
841 { | |
842 Expression *e1; | |
843 Type *t; | |
844 int dim; | |
845 | |
846 //printf("fd = '%s', var = '%s'\n", fd->toChars(), toChars()); | |
847 if (!ei) | |
848 { | |
849 Expression *e = init->toExpression(); | |
850 if (!e) | |
851 { | |
852 init = init->semantic(sc, type); | |
853 e = init->toExpression(); | |
854 if (!e) | |
855 { error("is not a static and cannot have static initializer"); | |
856 return; | |
857 } | |
858 } | |
859 ei = new ExpInitializer(init->loc, e); | |
860 init = ei; | |
861 } | |
862 | |
863 e1 = new VarExp(loc, this); | |
864 | |
865 t = type->toBasetype(); | |
866 if (t->ty == Tsarray) | |
867 { | |
92 | 868 ei->exp = ei->exp->semantic(sc); |
869 if (!ei->exp->implicitConvTo(type)) | |
1 | 870 { |
92 | 871 dim = ((TypeSArray *)t)->dim->toInteger(); |
872 // If multidimensional static array, treat as one large array | |
873 while (1) | |
874 { | |
875 t = t->nextOf()->toBasetype(); | |
876 if (t->ty != Tsarray) | |
877 break; | |
1 | 878 dim *= ((TypeSArray *)t)->dim->toInteger(); |
92 | 879 e1->type = new TypeSArray(t->nextOf(), new IntegerExp(0, dim, Type::tindex)); |
880 } | |
1 | 881 } |
882 e1 = new SliceExp(loc, e1, NULL, NULL); | |
883 } | |
884 else if (t->ty == Tstruct) | |
885 { | |
886 ei->exp = ei->exp->semantic(sc); | |
887 if (!ei->exp->implicitConvTo(type)) | |
888 ei->exp = new CastExp(loc, ei->exp, type); | |
889 } | |
890 ei->exp = new AssignExp(loc, e1, ei->exp); | |
891 ei->exp->op = TOKconstruct; | |
892 canassign++; | |
893 ei->exp = ei->exp->semantic(sc); | |
894 canassign--; | |
895 ei->exp->optimize(WANTvalue); | |
896 } | |
897 else | |
898 { | |
899 init = init->semantic(sc, type); | |
900 if (fd && isConst() && !isStatic()) | |
901 { // Make it static | |
902 storage_class |= STCstatic; | |
903 } | |
904 } | |
905 } | |
906 else if (isConst() || isFinal()) | |
907 { | |
908 /* Because we may need the results of a const declaration in a | |
909 * subsequent type, such as an array dimension, before semantic2() | |
910 * gets ordinarily run, try to run semantic2() now. | |
911 * Ignore failure. | |
912 */ | |
913 | |
19 | 914 if (!global.errors && !inferred) |
1 | 915 { |
916 unsigned errors = global.errors; | |
917 global.gag++; | |
918 //printf("+gag\n"); | |
19 | 919 Expression *e; |
920 Initializer *i2 = init; | |
1 | 921 inuse++; |
19 | 922 if (ei) |
923 { | |
924 e = ei->exp->syntaxCopy(); | |
925 e = e->semantic(sc); | |
926 e = e->implicitCastTo(sc, type); | |
927 } | |
928 else if (si || ai) | |
929 { i2 = init->syntaxCopy(); | |
930 i2 = i2->semantic(sc, type); | |
931 } | |
1 | 932 inuse--; |
933 global.gag--; | |
934 //printf("-gag\n"); | |
935 if (errors != global.errors) // if errors happened | |
936 { | |
937 if (global.gag == 0) | |
938 global.errors = errors; // act as if nothing happened | |
939 } | |
19 | 940 else if (ei) |
1 | 941 { |
942 e = e->optimize(WANTvalue | WANTinterpret); | |
943 if (e->op == TOKint64 || e->op == TOKstring) | |
944 { | |
945 ei->exp = e; // no errors, keep result | |
946 } | |
947 } | |
19 | 948 else |
949 init = i2; // no errors, keep result | |
1 | 950 } |
951 } | |
952 } | |
953 } | |
954 | |
955 ExpInitializer *VarDeclaration::getExpInitializer() | |
956 { | |
957 ExpInitializer *ei; | |
958 | |
959 if (init) | |
960 ei = init->isExpInitializer(); | |
961 else | |
962 { | |
963 Expression *e = type->defaultInit(); | |
964 if (e) | |
965 ei = new ExpInitializer(loc, e); | |
966 else | |
967 ei = NULL; | |
968 } | |
969 return ei; | |
970 } | |
971 | |
972 void VarDeclaration::semantic2(Scope *sc) | |
973 { | |
974 //printf("VarDeclaration::semantic2('%s')\n", toChars()); | |
975 if (init && !toParent()->isFuncDeclaration()) | |
976 { inuse++; | |
977 #if 0 | |
978 ExpInitializer *ei = init->isExpInitializer(); | |
979 if (ei) | |
980 { | |
981 ei->exp->dump(0); | |
982 printf("type = %p\n", ei->exp->type); | |
983 } | |
984 #endif | |
985 init = init->semantic(sc, type); | |
986 inuse--; | |
987 } | |
988 } | |
989 | |
990 char *VarDeclaration::kind() | |
991 { | |
992 return "variable"; | |
993 } | |
994 | |
995 Dsymbol *VarDeclaration::toAlias() | |
996 { | |
997 //printf("VarDeclaration::toAlias('%s', this = %p, aliassym = %p)\n", toChars(), this, aliassym); | |
998 assert(this != aliassym); | |
999 Dsymbol *s = aliassym ? aliassym->toAlias() : this; | |
1000 return s; | |
1001 } | |
1002 | |
1003 void VarDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
1004 { | |
1005 if (storage_class & STCconst) | |
1006 buf->writestring("const "); | |
1007 if (storage_class & STCstatic) | |
1008 buf->writestring("static "); | |
1009 if (type) | |
1010 type->toCBuffer(buf, ident, hgs); | |
1011 else | |
1012 buf->writestring(ident->toChars()); | |
1013 if (init) | |
1014 { buf->writestring(" = "); | |
1015 init->toCBuffer(buf, hgs); | |
1016 } | |
1017 buf->writeByte(';'); | |
1018 buf->writenl(); | |
1019 } | |
1020 | |
1021 int VarDeclaration::needThis() | |
1022 { | |
1023 //printf("VarDeclaration::needThis(%s, x%x)\n", toChars(), storage_class); | |
1024 return storage_class & STCfield; | |
1025 } | |
1026 | |
1027 int VarDeclaration::isImportedSymbol() | |
1028 { | |
1029 if (protection == PROTexport && !init && (isStatic() || isConst() || parent->isModule())) | |
1030 return TRUE; | |
1031 return FALSE; | |
1032 } | |
1033 | |
1034 void VarDeclaration::checkCtorConstInit() | |
1035 { | |
1036 if (ctorinit == 0 && isCtorinit() && !(storage_class & STCfield)) | |
1037 error("missing initializer in static constructor for const variable"); | |
1038 } | |
1039 | |
1040 /************************************ | |
1041 * Check to see if variable is a reference to an enclosing function | |
1042 * or not. | |
1043 */ | |
1044 | |
1045 void VarDeclaration::checkNestedReference(Scope *sc, Loc loc) | |
1046 { | |
92 | 1047 if (parent && !isDataseg() && parent != sc->parent) |
1 | 1048 { |
1049 FuncDeclaration *fdv = toParent()->isFuncDeclaration(); | |
1050 FuncDeclaration *fdthis = sc->parent->isFuncDeclaration(); | |
1051 | |
1052 if (fdv && fdthis) | |
1053 { | |
1054 if (loc.filename) | |
1055 fdthis->getLevel(loc, fdv); | |
1056 nestedref = 1; | |
1057 fdv->nestedFrameRef = 1; | |
50
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
1058 fdv->llvmNestedVars.insert(this); |
1 | 1059 //printf("var %s in function %s is nested ref\n", toChars(), fdv->toChars()); |
1060 } | |
1061 } | |
1062 } | |
1063 | |
1064 /******************************* | |
1065 * Does symbol go into data segment? | |
1066 */ | |
1067 | |
1068 int VarDeclaration::isDataseg() | |
1069 { | |
1070 #if 0 | |
1071 printf("VarDeclaration::isDataseg(%p, '%s')\n", this, toChars()); | |
1072 printf("%x, %p, %p\n", storage_class & (STCstatic | STCconst), parent->isModule(), parent->isTemplateInstance()); | |
1073 printf("parent = '%s'\n", parent->toChars()); | |
1074 #endif | |
1075 Dsymbol *parent = this->toParent(); | |
1076 if (!parent && !(storage_class & (STCstatic | STCconst))) | |
1077 { error("forward referenced"); | |
1078 type = Type::terror; | |
1079 return 0; | |
1080 } | |
1081 return (storage_class & (STCstatic | STCconst) || | |
1082 parent->isModule() || | |
1083 parent->isTemplateInstance()); | |
1084 } | |
1085 | |
1086 int VarDeclaration::hasPointers() | |
1087 { | |
1088 return (!isDataseg() && type->hasPointers()); | |
1089 } | |
1090 | |
1091 /****************************************** | |
1092 * If a variable has an auto destructor call, return call for it. | |
1093 * Otherwise, return NULL. | |
1094 */ | |
1095 | |
1096 Expression *VarDeclaration::callAutoDtor() | |
1097 { Expression *e = NULL; | |
1098 | |
1099 if (storage_class & (STCauto | STCscope) && !noauto) | |
1100 { | |
1101 for (ClassDeclaration *cd = type->isClassHandle(); | |
1102 cd; | |
1103 cd = cd->baseClass) | |
1104 { | |
1105 /* We can do better if there's a way with onstack | |
1106 * classes to determine if there's no way the monitor | |
1107 * could be set. | |
1108 */ | |
1109 if (1 || onstack || cd->dtors.dim) // if any destructors | |
1110 { | |
1111 // delete this; | |
1112 Expression *ec; | |
1113 | |
1114 ec = new VarExp(loc, this); | |
1115 e = new DeleteExp(loc, ec); | |
1116 e->type = Type::tvoid; | |
1117 break; | |
1118 } | |
1119 } | |
1120 } | |
1121 return e; | |
1122 } | |
1123 | |
1124 | |
1125 /********************************* ClassInfoDeclaration ****************************/ | |
1126 | |
1127 ClassInfoDeclaration::ClassInfoDeclaration(ClassDeclaration *cd) | |
1128 : VarDeclaration(0, ClassDeclaration::classinfo->type, cd->ident, NULL) | |
1129 { | |
1130 this->cd = cd; | |
1131 storage_class = STCstatic; | |
1132 } | |
1133 | |
1134 Dsymbol *ClassInfoDeclaration::syntaxCopy(Dsymbol *s) | |
1135 { | |
1136 assert(0); // should never be produced by syntax | |
1137 return NULL; | |
1138 } | |
1139 | |
1140 void ClassInfoDeclaration::semantic(Scope *sc) | |
1141 { | |
1142 } | |
1143 | |
1144 /********************************* ModuleInfoDeclaration ****************************/ | |
1145 | |
1146 ModuleInfoDeclaration::ModuleInfoDeclaration(Module *mod) | |
1147 : VarDeclaration(0, Module::moduleinfo->type, mod->ident, NULL) | |
1148 { | |
1149 this->mod = mod; | |
1150 storage_class = STCstatic; | |
1151 } | |
1152 | |
1153 Dsymbol *ModuleInfoDeclaration::syntaxCopy(Dsymbol *s) | |
1154 { | |
1155 assert(0); // should never be produced by syntax | |
1156 return NULL; | |
1157 } | |
1158 | |
1159 void ModuleInfoDeclaration::semantic(Scope *sc) | |
1160 { | |
1161 } | |
1162 | |
1163 /********************************* TypeInfoDeclaration ****************************/ | |
1164 | |
1165 TypeInfoDeclaration::TypeInfoDeclaration(Type *tinfo, int internal) | |
1166 : VarDeclaration(0, Type::typeinfo->type, tinfo->getTypeInfoIdent(internal), NULL) | |
1167 { | |
1168 this->tinfo = tinfo; | |
1169 storage_class = STCstatic; | |
1170 protection = PROTpublic; | |
1171 linkage = LINKc; | |
1172 } | |
1173 | |
1174 Dsymbol *TypeInfoDeclaration::syntaxCopy(Dsymbol *s) | |
1175 { | |
1176 assert(0); // should never be produced by syntax | |
1177 return NULL; | |
1178 } | |
1179 | |
1180 void TypeInfoDeclaration::semantic(Scope *sc) | |
1181 { | |
1182 assert(linkage == LINKc); | |
1183 } | |
1184 | |
92 | 1185 /***************************** TypeInfoConstDeclaration **********************/ |
1186 | |
1187 #if V2 | |
1188 TypeInfoConstDeclaration::TypeInfoConstDeclaration(Type *tinfo) | |
1189 : TypeInfoDeclaration(tinfo, 0) | |
1190 { | |
1191 } | |
1192 #endif | |
1193 | |
1194 /***************************** TypeInfoInvariantDeclaration **********************/ | |
1195 | |
1196 #if V2 | |
1197 TypeInfoInvariantDeclaration::TypeInfoInvariantDeclaration(Type *tinfo) | |
1198 : TypeInfoDeclaration(tinfo, 0) | |
1199 { | |
1200 } | |
1201 #endif | |
1202 | |
1 | 1203 /***************************** TypeInfoStructDeclaration **********************/ |
1204 | |
1205 TypeInfoStructDeclaration::TypeInfoStructDeclaration(Type *tinfo) | |
1206 : TypeInfoDeclaration(tinfo, 0) | |
1207 { | |
1208 } | |
1209 | |
1210 /***************************** TypeInfoClassDeclaration ***********************/ | |
1211 | |
1212 TypeInfoClassDeclaration::TypeInfoClassDeclaration(Type *tinfo) | |
1213 : TypeInfoDeclaration(tinfo, 0) | |
1214 { | |
1215 } | |
1216 | |
1217 /***************************** TypeInfoInterfaceDeclaration *******************/ | |
1218 | |
1219 TypeInfoInterfaceDeclaration::TypeInfoInterfaceDeclaration(Type *tinfo) | |
1220 : TypeInfoDeclaration(tinfo, 0) | |
1221 { | |
1222 } | |
1223 | |
1224 /***************************** TypeInfoTypedefDeclaration *********************/ | |
1225 | |
1226 TypeInfoTypedefDeclaration::TypeInfoTypedefDeclaration(Type *tinfo) | |
1227 : TypeInfoDeclaration(tinfo, 0) | |
1228 { | |
1229 } | |
1230 | |
1231 /***************************** TypeInfoPointerDeclaration *********************/ | |
1232 | |
1233 TypeInfoPointerDeclaration::TypeInfoPointerDeclaration(Type *tinfo) | |
1234 : TypeInfoDeclaration(tinfo, 0) | |
1235 { | |
1236 } | |
1237 | |
1238 /***************************** TypeInfoArrayDeclaration ***********************/ | |
1239 | |
1240 TypeInfoArrayDeclaration::TypeInfoArrayDeclaration(Type *tinfo) | |
1241 : TypeInfoDeclaration(tinfo, 0) | |
1242 { | |
1243 } | |
1244 | |
1245 /***************************** TypeInfoStaticArrayDeclaration *****************/ | |
1246 | |
1247 TypeInfoStaticArrayDeclaration::TypeInfoStaticArrayDeclaration(Type *tinfo) | |
1248 : TypeInfoDeclaration(tinfo, 0) | |
1249 { | |
1250 } | |
1251 | |
1252 /***************************** TypeInfoAssociativeArrayDeclaration ************/ | |
1253 | |
1254 TypeInfoAssociativeArrayDeclaration::TypeInfoAssociativeArrayDeclaration(Type *tinfo) | |
1255 : TypeInfoDeclaration(tinfo, 0) | |
1256 { | |
1257 } | |
1258 | |
1259 /***************************** TypeInfoEnumDeclaration ***********************/ | |
1260 | |
1261 TypeInfoEnumDeclaration::TypeInfoEnumDeclaration(Type *tinfo) | |
1262 : TypeInfoDeclaration(tinfo, 0) | |
1263 { | |
1264 } | |
1265 | |
1266 /***************************** TypeInfoFunctionDeclaration ********************/ | |
1267 | |
1268 TypeInfoFunctionDeclaration::TypeInfoFunctionDeclaration(Type *tinfo) | |
1269 : TypeInfoDeclaration(tinfo, 0) | |
1270 { | |
1271 } | |
1272 | |
1273 /***************************** TypeInfoDelegateDeclaration ********************/ | |
1274 | |
1275 TypeInfoDelegateDeclaration::TypeInfoDelegateDeclaration(Type *tinfo) | |
1276 : TypeInfoDeclaration(tinfo, 0) | |
1277 { | |
1278 } | |
1279 | |
1280 /***************************** TypeInfoTupleDeclaration **********************/ | |
1281 | |
1282 TypeInfoTupleDeclaration::TypeInfoTupleDeclaration(Type *tinfo) | |
1283 : TypeInfoDeclaration(tinfo, 0) | |
1284 { | |
1285 } | |
1286 | |
1287 /********************************* ThisDeclaration ****************************/ | |
1288 | |
1289 // For the "this" parameter to member functions | |
1290 | |
1291 ThisDeclaration::ThisDeclaration(Type *t) | |
1292 : VarDeclaration(0, t, Id::This, NULL) | |
1293 { | |
1294 noauto = 1; | |
1295 } | |
1296 | |
1297 Dsymbol *ThisDeclaration::syntaxCopy(Dsymbol *s) | |
1298 { | |
1299 assert(0); // should never be produced by syntax | |
1300 return NULL; | |
1301 } | |
1302 | |
1303 |