Error on pointless maybe annotations
[delight/core.git] / dmd / declaration.c
blob38f3edd49e7b65f58c0d8b6c919b06e1f58c8c9e
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.
11 /* NOTE: This file has been patched from the original DMD distribution to
12 work with the GDC compiler.
14 Modified by David Friedman, December 2006
17 #include <stdio.h>
18 #include <assert.h>
20 #include "init.h"
21 #include "declaration.h"
22 #include "attrib.h"
23 #include "mtype.h"
24 #include "template.h"
25 #include "scope.h"
26 #include "aggregate.h"
27 #include "module.h"
28 #include "id.h"
29 #include "expression.h"
30 #include "hdrgen.h"
32 /********************************* Declaration ****************************/
34 Declaration::Declaration(Identifier *id)
35 : Dsymbol(id)
37 type = NULL;
38 originalType = NULL;
39 storage_class = STCundefined;
40 protection = PROTundefined;
41 linkage = LINKdefault;
42 attributes = NULL;
45 void Declaration::semantic(Scope *sc)
49 char *Declaration::kind()
51 return "declaration";
54 target_size_t Declaration::size(Loc loc)
56 assert(type);
57 return type->size();
60 int Declaration::isStaticConstructor()
62 return FALSE;
65 int Declaration::isStaticDestructor()
67 return FALSE;
70 int Declaration::isDelete()
72 return FALSE;
75 int Declaration::isDataseg()
77 return FALSE;
80 int Declaration::isCodeseg()
82 return FALSE;
85 enum PROT Declaration::prot()
87 return protection;
90 /********************************* TupleDeclaration ****************************/
92 TupleDeclaration::TupleDeclaration(Loc loc, Identifier *id, Objects *objects)
93 : Declaration(id)
95 this->type = NULL;
96 this->objects = objects;
97 this->isexp = 0;
98 this->tupletype = NULL;
101 Dsymbol *TupleDeclaration::syntaxCopy(Dsymbol *s)
103 assert(0);
104 return NULL;
107 char *TupleDeclaration::kind()
109 return "tuple";
112 Type *TupleDeclaration::getType()
114 /* If this tuple represents a type, return that type
117 //printf("TupleDeclaration::getType() %s\n", toChars());
118 if (isexp)
119 return NULL;
120 if (!tupletype)
122 /* It's only a type tuple if all the Object's are types
124 for (size_t i = 0; i < objects->dim; i++)
125 { Object *o = (Object *)objects->data[i];
127 if (o->dyncast() != DYNCAST_TYPE)
129 //printf("\tnot[%d], %p, %d\n", i, o, o->dyncast());
130 return NULL;
134 /* We know it's a type tuple, so build the TypeTuple
136 Arguments *args = new Arguments();
137 args->setDim(objects->dim);
138 OutBuffer buf;
139 for (size_t i = 0; i < objects->dim; i++)
140 { Type *t = (Type *)objects->data[i];
142 //printf("type = %s\n", t->toChars());
143 #if 0
144 buf.printf("_%s_%d", ident->toChars(), i);
145 char *name = (char *)buf.extractData();
146 Identifier *id = new Identifier(name, TOKidentifier);
147 Argument *arg = new Argument(STCin, t, id, NULL);
148 #else
149 Argument *arg = new Argument(STCin, t, NULL, NULL);
150 #endif
151 args->data[i] = (void *)arg;
154 tupletype = new TypeTuple(args);
157 return tupletype;
160 int TupleDeclaration::needThis()
162 //printf("TupleDeclaration::needThis(%s)\n", toChars());
163 for (size_t i = 0; i < objects->dim; i++)
164 { Object *o = (Object *)objects->data[i];
165 if (o->dyncast() == DYNCAST_EXPRESSION)
166 { Expression *e = (Expression *)o;
167 if (e->op == TOKdsymbol)
168 { DsymbolExp *ve = (DsymbolExp *)e;
169 Declaration *d = ve->s->isDeclaration();
170 if (d && d->needThis())
172 return 1;
177 return 0;
180 /********************************* TypedefDeclaration ****************************/
182 TypedefDeclaration::TypedefDeclaration(Loc loc, Identifier *id, Type *basetype, Initializer *init)
183 : Declaration(id)
185 this->type = new TypeTypedef(this);
186 this->basetype = basetype->toBasetype();
187 this->init = init;
188 #ifdef _DH
189 this->htype = NULL;
190 this->hbasetype = NULL;
191 #endif
192 this->sem = 0;
193 this->inuse = 0;
194 this->loc = loc;
195 this->sinit = NULL;
198 Dsymbol *TypedefDeclaration::syntaxCopy(Dsymbol *s)
200 Type *basetype = this->basetype->syntaxCopy();
202 Initializer *init = NULL;
203 if (this->init)
204 init = this->init->syntaxCopy();
206 assert(!s);
207 TypedefDeclaration *st;
208 st = new TypedefDeclaration(loc, ident, basetype, init);
209 #ifdef _DH
210 // Syntax copy for header file
211 if (!htype) // Don't overwrite original
212 { if (type) // Make copy for both old and new instances
213 { htype = type->syntaxCopy();
214 st->htype = type->syntaxCopy();
217 else // Make copy of original for new instance
218 st->htype = htype->syntaxCopy();
219 if (!hbasetype)
220 { if (basetype)
221 { hbasetype = basetype->syntaxCopy();
222 st->hbasetype = basetype->syntaxCopy();
225 else
226 st->hbasetype = hbasetype->syntaxCopy();
227 #endif
228 return st;
231 void TypedefDeclaration::semantic(Scope *sc)
233 //printf("TypedefDeclaration::semantic(%s) sem = %d\n", toChars(), sem);
234 if (sem == 0)
235 { sem = 1;
236 basetype = basetype->semantic(loc, sc);
237 sem = 2;
238 type = type->semantic(loc, sc);
239 if (attributes)
240 attributes->append(sc->attributes);
241 else
242 attributes = sc->attributes;
243 if (sc->parent->isFuncDeclaration() && init)
244 semantic2(sc);
246 else if (sem == 1)
248 error("circular definition");
252 void TypedefDeclaration::semantic2(Scope *sc)
254 //printf("TypedefDeclaration::semantic2(%s) sem = %d\n", toChars(), sem);
255 if (sem == 2)
256 { sem = 3;
257 if (init)
259 init = init->semantic(sc, basetype);
261 ExpInitializer *ie = init->isExpInitializer();
262 if (ie)
264 if (ie->exp->type == basetype)
265 ie->exp->type = type;
271 char *TypedefDeclaration::kind()
273 return "typedef";
276 Type *TypedefDeclaration::getType()
278 return type;
281 void TypedefDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
283 buf->writestring("typedef ");
284 basetype->toCBuffer(buf, ident, hgs);
285 if (init)
287 buf->writestring(" = ");
288 init->toCBuffer(buf, hgs);
290 buf->writeByte(';');
291 buf->writenl();
294 /********************************* AliasDeclaration ****************************/
296 AliasDeclaration::AliasDeclaration(Loc loc, Identifier *id, Type *type)
297 : Declaration(id)
299 //printf("AliasDeclaration(id = '%s', type = %p)\n", id->toChars(), type);
300 //printf("type = '%s'\n", type->toChars());
301 this->loc = loc;
302 this->type = type;
303 this->aliassym = NULL;
304 #ifdef _DH
305 this->htype = NULL;
306 this->haliassym = NULL;
307 #endif
308 this->overnext = NULL;
309 this->inSemantic = 0;
310 assert(type);
313 AliasDeclaration::AliasDeclaration(Loc loc, Identifier *id, Dsymbol *s)
314 : Declaration(id)
316 //printf("AliasDeclaration(id = '%s', s = %p)\n", id->toChars(), s);
317 assert(s != this);
318 this->loc = loc;
319 this->type = NULL;
320 this->aliassym = s;
321 #ifdef _DH
322 this->htype = NULL;
323 this->haliassym = NULL;
324 #endif
325 this->overnext = NULL;
326 this->inSemantic = 0;
327 assert(s);
330 Dsymbol *AliasDeclaration::syntaxCopy(Dsymbol *s)
332 assert(!s);
333 AliasDeclaration *sa;
334 if (type)
335 sa = new AliasDeclaration(loc, ident, type->syntaxCopy());
336 else
337 sa = new AliasDeclaration(loc, ident, aliassym->syntaxCopy(NULL));
338 #ifdef _DH
339 // Syntax copy for header file
340 if (!htype) // Don't overwrite original
341 { if (type) // Make copy for both old and new instances
342 { htype = type->syntaxCopy();
343 sa->htype = type->syntaxCopy();
346 else // Make copy of original for new instance
347 sa->htype = htype->syntaxCopy();
348 if (!haliassym)
349 { if (aliassym)
350 { haliassym = aliassym->syntaxCopy(s);
351 sa->haliassym = aliassym->syntaxCopy(s);
354 else
355 sa->haliassym = haliassym->syntaxCopy(s);
356 #endif
357 return sa;
360 void AliasDeclaration::semantic(Scope *sc)
362 //printf("AliasDeclaration::semantic() %s\n", toChars());
363 if (aliassym)
365 if (aliassym->isTemplateInstance())
366 aliassym->semantic(sc);
367 return;
369 this->inSemantic = 1;
371 if (storage_class & STCconst)
372 error("cannot be const");
374 storage_class |= sc->stc & STCdeprecated;
376 // Given:
377 // alias foo.bar.abc def;
378 // it is not knowable from the syntax whether this is an alias
379 // for a type or an alias for a symbol. It is up to the semantic()
380 // pass to distinguish.
381 // If it is a type, then type is set and getType() will return that
382 // type. If it is a symbol, then aliassym is set and type is NULL -
383 // toAlias() will return aliasssym.
385 Dsymbol *s;
386 Type *t;
387 Expression *e;
389 /* This section is needed because resolve() will:
390 * const x = 3;
391 * alias x y;
392 * try to alias y to 3.
394 s = type->toDsymbol(sc);
395 if (s)
396 goto L2; // it's a symbolic alias
398 //printf("alias type is %s\n", type->toChars());
399 type->resolve(loc, sc, &e, &t, &s);
400 if (s)
402 goto L2;
404 else if (e)
406 // Try to convert Expression to Dsymbol
407 if (e->op == TOKvar)
408 { s = ((VarExp *)e)->var;
409 goto L2;
411 else if (e->op == TOKfunction)
412 { s = ((FuncExp *)e)->fd;
413 goto L2;
415 else
416 { error("cannot alias an expression %s", e->toChars());
417 t = e->type;
420 else if (t)
421 type = t;
422 if (overnext)
423 ScopeDsymbol::multiplyDefined(0, this, overnext);
424 this->inSemantic = 0;
425 return;
428 //printf("alias is a symbol %s %s\n", s->kind(), s->toChars());
429 type = NULL;
430 VarDeclaration *v = s->isVarDeclaration();
431 if (v && v->linkage == LINKdefault)
433 error("forward reference of %s", v->toChars());
434 s = NULL;
436 else
438 FuncDeclaration *f = s->toAlias()->isFuncDeclaration();
439 if (f)
441 if (overnext)
443 FuncAliasDeclaration *fa = new FuncAliasDeclaration(f);
444 if (!fa->overloadInsert(overnext))
445 ScopeDsymbol::multiplyDefined(0, f, overnext);
446 overnext = NULL;
447 s = fa;
448 s->parent = sc->parent;
451 if (overnext)
452 ScopeDsymbol::multiplyDefined(0, s, overnext);
453 if (s == this)
455 assert(global.errors);
456 s = NULL;
459 aliassym = s;
460 this->inSemantic = 0;
463 int AliasDeclaration::overloadInsert(Dsymbol *s)
465 /* Don't know yet what the aliased symbol is, so assume it can
466 * be overloaded and check later for correctness.
469 //printf("AliasDeclaration::overloadInsert('%s')\n", s->toChars());
470 if (overnext == NULL)
471 { overnext = s;
472 return TRUE;
474 else
476 return overnext->overloadInsert(s);
480 char *AliasDeclaration::kind()
482 return "alias";
485 Type *AliasDeclaration::getType()
487 return type;
490 Dsymbol *AliasDeclaration::toAlias()
492 //printf("AliasDeclaration::toAlias('%s', this = %p, aliassym = %p, kind = '%s')\n", toChars(), this, aliassym, aliassym ? aliassym->kind() : "");
493 assert(this != aliassym);
494 //static int count; if (++count == 10) *(char*)0=0;
495 if (inSemantic)
496 { error("recursive alias declaration");
497 // return this;
499 Dsymbol *s = aliassym ? aliassym->toAlias() : this;
500 return s;
503 void AliasDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
505 buf->writestring("alias ");
506 #if 0 && _DH
507 if (hgs->hdrgen)
509 if (haliassym)
511 haliassym->toCBuffer(buf, hgs);
512 buf->writeByte(' ');
513 buf->writestring(ident->toChars());
515 else
516 htype->toCBuffer(buf, ident, hgs);
518 else
519 #endif
521 if (aliassym)
523 aliassym->toCBuffer(buf, hgs);
524 buf->writeByte(' ');
525 buf->writestring(ident->toChars());
527 else
528 type->toCBuffer(buf, ident, hgs);
530 buf->writeByte(';');
531 buf->writenl();
534 /********************************* VarDeclaration ****************************/
536 VarDeclaration::VarDeclaration(Loc loc, Type *type, Identifier *id, Initializer *init)
537 : Declaration(id)
539 //printf("VarDeclaration('%s')\n", id->toChars());
540 #ifdef DEBUG
541 if (!type && !init)
542 { printf("VarDeclaration('%s')\n", id->toChars());
543 //*(char*)0=0;
545 #endif
546 assert(type || init);
547 this->type = type;
548 this->init = init;
549 #ifdef _DH
550 this->htype = NULL;
551 this->hinit = NULL;
552 #endif
553 this->loc = loc;
554 offset = 0;
555 noauto = 0;
556 skipnullcheck = 0;
557 nestedref = 0;
558 inuse = 0;
559 ctorinit = 0;
560 aliassym = NULL;
561 onstack = 0;
562 canassign = 0;
563 value = NULL;
566 Dsymbol *VarDeclaration::syntaxCopy(Dsymbol *s)
568 //printf("VarDeclaration::syntaxCopy(%s)\n", toChars());
570 VarDeclaration *sv;
571 if (s)
572 { sv = (VarDeclaration *)s;
574 else
576 Initializer *init = NULL;
577 if (this->init)
578 { init = this->init->syntaxCopy();
579 //init->isExpInitializer()->exp->print();
580 //init->isExpInitializer()->exp->dump(0);
583 sv = new VarDeclaration(loc, type ? type->syntaxCopy() : NULL, ident, init);
584 sv->storage_class = storage_class;
586 #ifdef _DH
587 // Syntax copy for header file
588 if (!htype) // Don't overwrite original
589 { if (type) // Make copy for both old and new instances
590 { htype = type->syntaxCopy();
591 sv->htype = type->syntaxCopy();
594 else // Make copy of original for new instance
595 sv->htype = htype->syntaxCopy();
596 if (!hinit)
597 { if (init)
598 { hinit = init->syntaxCopy();
599 sv->hinit = init->syntaxCopy();
602 else
603 sv->hinit = hinit->syntaxCopy();
604 #endif
605 return sv;
608 void VarDeclaration::semantic(Scope *sc)
610 //printf("VarDeclaration::semantic('%s', parent = '%s')\n", toChars(), sc->parent->toChars());
611 //printf("type = %s\n", type->toChars());
612 //printf("linkage = %d\n", sc->linkage);
613 //if (strcmp(toChars(), "mul") == 0) halt();
615 storage_class |= sc->stc;
616 if (storage_class & STCextern && init)
617 error("extern symbols cannot have initializers");
619 /* If auto type inference, do the inference
621 int inferred = 0;
622 if (!type)
623 { inuse++;
624 type = init->inferType(sc);
625 inuse--;
626 inferred = 1;
628 /* This is a kludge to support the existing syntax for RAII
629 * declarations.
631 storage_class &= ~STCauto;
632 originalType = type;
634 else
635 { if (!originalType)
636 originalType = type;
637 type = type->semantic(loc, sc);
640 type->checkDeprecated(loc, sc);
641 linkage = sc->linkage;
642 this->parent = sc->parent;
643 //printf("this = %p, parent = %p, '%s'\n", this, parent, parent->toChars());
644 protection = sc->protection;
645 if (attributes)
646 attributes->append(sc->attributes);
647 else
648 attributes = sc->attributes;
649 //printf("sc->stc = %x\n", sc->stc);
650 //printf("storage_class = %x\n", storage_class);
652 Dsymbol *parent = toParent();
653 FuncDeclaration *fd = parent->isFuncDeclaration();
655 Type *tb = type->toBasetype();
656 if (tb->ty == Tvoid && !(storage_class & STClazy))
657 { error("voids have no value");
658 type = Type::terror;
659 tb = type;
661 if (tb->ty == Tfunction)
662 { error("cannot be declared to be a function");
663 type = Type::terror;
664 tb = type;
666 if (tb->ty == Tstruct)
667 { TypeStruct *ts = (TypeStruct *)tb;
669 if (!ts->sym->members)
671 error("no definition of struct %s", ts->toChars());
675 if (tb->ty == Ttuple)
676 { /* Instead, declare variables for each of the tuple elements
677 * and add those.
679 TypeTuple *tt = (TypeTuple *)tb;
680 size_t nelems = Argument::dim(tt->arguments);
681 Objects *exps = new Objects();
682 exps->setDim(nelems);
683 Expression *ie = init ? init->toExpression() : NULL;
685 for (size_t i = 0; i < nelems; i++)
686 { Argument *arg = Argument::getNth(tt->arguments, i);
688 OutBuffer buf;
689 buf.printf("_%s_field_%"PRIuSIZE, ident->toChars(), i);
690 buf.writeByte(0);
691 char *name = (char *)buf.extractData();
692 Identifier *id = new Identifier(name, TOKidentifier);
694 Expression *einit = ie;
695 if (ie && ie->op == TOKtuple)
696 { einit = (Expression *)((TupleExp *)ie)->exps->data[i];
698 Initializer *ti = init;
699 if (einit)
700 { ti = new ExpInitializer(einit->loc, einit);
703 VarDeclaration *v = new VarDeclaration(loc, arg->type, id, ti);
704 //printf("declaring field %s of type %s\n", v->toChars(), v->type->toChars());
705 v->semantic(sc);
707 if (sc->scopesym)
708 { //printf("adding %s to %s\n", v->toChars(), sc->scopesym->toChars());
709 if (sc->scopesym->members)
710 sc->scopesym->members->push(v);
713 Expression *e = new DsymbolExp(loc, v);
714 exps->data[i] = e;
716 TupleDeclaration *v2 = new TupleDeclaration(loc, ident, exps);
717 v2->isexp = 1;
718 aliassym = v2;
719 return;
722 if (storage_class & STCconst && !init && !fd)
723 // Initialize by constructor only
724 storage_class = (storage_class & ~STCconst) | STCctorinit;
726 if (isConst())
729 else if (isStatic())
732 else if (isSynchronized())
734 error("variable %s cannot be synchronized", toChars());
736 else if (isOverride())
738 error("override cannot be applied to variable");
740 else if (isAbstract())
742 error("abstract cannot be applied to variable");
744 else if (storage_class & STCtemplateparameter)
747 else
749 AggregateDeclaration *aad = sc->anonAgg;
750 if (!aad)
751 aad = parent->isAggregateDeclaration();
752 if (aad)
754 aad->addField(sc, this);
757 InterfaceDeclaration *id = parent->isInterfaceDeclaration();
758 if (id)
760 error("field not allowed in interface");
763 TemplateInstance *ti = parent->isTemplateInstance();
764 if (ti)
766 // Take care of nested templates
767 while (1)
769 TemplateInstance *ti2 = ti->tempdecl->parent->isTemplateInstance();
770 if (!ti2)
771 break;
772 ti = ti2;
775 // If it's a member template
776 AggregateDeclaration *ad = ti->tempdecl->isMember();
777 if (ad && storage_class != STCundefined)
779 error("cannot use template to add field to aggregate '%s'", ad->toChars());
784 if (type->isauto() && !noauto)
786 if (storage_class & (STCfield | STCout | STCref | STCstatic) || !fd)
788 error("globals, statics, fields, ref and out parameters cannot be auto");
791 if (!(storage_class & (STCauto | STCscope)))
793 if (!(storage_class & STCparameter) && ident != Id::withSym)
794 error("reference to scope class must be scope");
798 if (!init && !sc->inunion && !isStatic() && !isConst() && fd &&
799 !(storage_class & (STCfield | STCin | STCforeach)) &&
800 type->size() != 0)
802 // Provide a default initializer
803 //printf("Providing default initializer for '%s'\n", toChars());
804 if (type->ty == Tstruct &&
805 ((TypeStruct *)type)->sym->zeroInit == 1)
806 { /* If a struct is all zeros, as a special case
807 * set it's initializer to the integer 0.
808 * In AssignExp::toElem(), we check for this and issue
809 * a memset() to initialize the struct.
810 * Must do same check in interpreter.
812 Expression *e = new IntegerExp(loc, 0, Type::tint32);
813 Expression *e1;
814 e1 = new VarExp(loc, this);
815 e = new AssignExp(loc, e1, e);
816 e->type = e1->type;
817 init = new ExpInitializer(loc, e/*->type->defaultInit()*/);
818 return;
820 else if (type->ty == Ttypedef)
821 { TypeTypedef *td = (TypeTypedef *)type;
822 if (td->sym->init)
823 { init = td->sym->init;
824 ExpInitializer *ie = init->isExpInitializer();
825 if (ie)
826 // Make copy so we can modify it
827 init = new ExpInitializer(ie->loc, ie->exp);
829 else
830 init = getExpInitializer();
832 else
834 init = getExpInitializer();
838 if (init)
840 ArrayInitializer *ai = init->isArrayInitializer();
841 if (ai && tb->ty == Taarray)
843 init = ai->toAssocArrayInitializer();
846 StructInitializer *si = init->isStructInitializer();
847 ExpInitializer *ei = init->isExpInitializer();
849 // See if we can allocate on the stack
850 if (ei && isScope() && ei->exp->op == TOKnew)
851 { NewExp *ne = (NewExp *)ei->exp;
852 if (!(ne->newargs && ne->newargs->dim))
853 { ne->onstack = 1;
854 onstack = 1;
855 if (type->isBaseOf(ne->newtype->semantic(loc, sc), NULL))
856 onstack = 2;
860 // If inside function, there is no semantic3() call
861 if (sc->func)
863 // If local variable, use AssignExp to handle all the various
864 // possibilities.
865 if (fd && !isStatic() && !isConst() && !init->isVoidInitializer())
867 Expression *e1;
868 Type *t;
869 sinteger_t dim;
871 //printf("fd = '%s', var = '%s'\n", fd->toChars(), toChars());
872 if (!ei)
874 Expression *e = init->toExpression();
875 if (!e)
877 init = init->semantic(sc, type);
878 e = init->toExpression();
879 if (!e)
880 { error("is not a static and cannot have static initializer");
881 return;
884 ei = new ExpInitializer(init->loc, e);
885 init = ei;
888 e1 = new VarExp(loc, this);
890 t = type->toBasetype();
891 if (t->ty == Tsarray)
893 ei->exp = ei->exp->semantic(sc);
894 if (!ei->exp->implicitConvTo(type))
896 dim = ((TypeSArray *)t)->dim->toInteger();
897 // If multidimensional static array, treat as one large array
898 while (1)
900 t = t->nextOf()->toBasetype();
901 if (t->ty != Tsarray)
902 break;
903 dim *= ((TypeSArray *)t)->dim->toInteger();
904 e1->type = new TypeSArray(t->nextOf(), new IntegerExp(0, dim, Type::tindex));
907 e1 = new SliceExp(loc, e1, NULL, NULL);
909 else if (t->ty == Tstruct)
911 ei->exp = ei->exp->semantic(sc);
912 if (!ei->exp->implicitConvTo(type))
913 ei->exp = new CastExp(loc, ei->exp, type);
915 ei->exp = new AssignExp(loc, e1, ei->exp);
916 ei->exp->op = TOKconstruct;
917 canassign++;
918 ei->exp = ei->exp->semantic(sc);
919 canassign--;
920 ei->exp->optimize(WANTvalue);
922 else
924 init = init->semantic(sc, type);
925 if (fd && isConst() && !isStatic())
926 { // Make it static
927 storage_class |= STCstatic;
931 else if (isConst() || isFinal())
933 /* Because we may need the results of a const declaration in a
934 * subsequent type, such as an array dimension, before semantic2()
935 * gets ordinarily run, try to run semantic2() now.
936 * Ignore failure.
939 if (!global.errors && !inferred)
941 unsigned errors = global.errors;
942 global.gag++;
943 //printf("+gag\n");
944 Expression *e;
945 Initializer *i2 = init;
946 inuse++;
947 if (ei)
949 e = ei->exp->syntaxCopy();
950 e = e->semantic(sc);
951 e = e->implicitCastTo(sc, type);
953 else if (si || ai)
954 { i2 = init->syntaxCopy();
955 i2 = i2->semantic(sc, type);
957 inuse--;
958 global.gag--;
959 //printf("-gag\n");
960 if (errors != global.errors) // if errors happened
962 if (global.gag == 0)
963 global.errors = errors; // act as if nothing happened
965 else if (ei)
967 e = e->optimize(WANTvalue | WANTinterpret);
968 if (e->op == TOKint64 || e->op == TOKstring)
970 ei->exp = e; // no errors, keep result
973 else
974 init = i2; // no errors, keep result
980 ExpInitializer *VarDeclaration::getExpInitializer()
982 ExpInitializer *ei;
984 if (init)
985 ei = init->isExpInitializer();
986 else
988 if ((type->ty == Tclass || type->ty == Tpointer) && !skipnullcheck)
989 error("missing initialiser for non-null variable %s", this->toChars());
991 Expression *e = type->defaultInit();
992 if (e)
993 ei = new ExpInitializer(loc, e);
994 else
995 ei = NULL;
997 return ei;
1000 void VarDeclaration::semantic2(Scope *sc)
1002 //printf("VarDeclaration::semantic2('%s')\n", toChars());
1003 if (init && !toParent()->isFuncDeclaration())
1004 { inuse++;
1005 #if 0
1006 ExpInitializer *ei = init->isExpInitializer();
1007 if (ei)
1009 ei->exp->dump(0);
1010 printf("type = %p\n", ei->exp->type);
1012 #endif
1013 init = init->semantic(sc, type);
1014 inuse--;
1018 char *VarDeclaration::kind()
1020 return "variable";
1023 Dsymbol *VarDeclaration::toAlias()
1025 //printf("VarDeclaration::toAlias('%s', this = %p, aliassym = %p)\n", toChars(), this, aliassym);
1026 assert(this != aliassym);
1027 Dsymbol *s = aliassym ? aliassym->toAlias() : this;
1028 return s;
1031 void VarDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
1033 if (storage_class & STCconst)
1034 buf->writestring("const ");
1035 if (storage_class & STCstatic)
1036 buf->writestring("static ");
1037 if (type)
1038 type->toCBuffer(buf, ident, hgs);
1039 else
1040 buf->writestring(ident->toChars());
1041 if (init)
1042 { buf->writestring(" = ");
1043 init->toCBuffer(buf, hgs);
1045 buf->writeByte(';');
1046 buf->writenl();
1049 int VarDeclaration::needThis()
1051 //printf("VarDeclaration::needThis(%s, x%x)\n", toChars(), storage_class);
1052 return storage_class & STCfield;
1055 int VarDeclaration::isImportedSymbol()
1057 if (protection == PROTexport && !init && (isStatic() || isConst() || parent->isModule()))
1058 return TRUE;
1059 return FALSE;
1062 void VarDeclaration::checkCtorConstInit()
1064 if (ctorinit == 0 && isCtorinit() && !(storage_class & STCfield))
1065 error("missing initializer in static constructor for const variable");
1068 /************************************
1069 * Check to see if variable is a reference to an enclosing function
1070 * or not.
1073 void VarDeclaration::checkNestedReference(Scope *sc, Loc loc)
1075 if (parent && !isDataseg() && parent != sc->parent)
1077 FuncDeclaration *fdv = toParent()->isFuncDeclaration();
1078 FuncDeclaration *fdthis = sc->parent->isFuncDeclaration();
1080 if (fdv && fdthis)
1082 if (loc.filename)
1083 fdthis->getLevel(loc, fdv);
1084 nestedref = 1;
1085 fdv->nestedFrameRef = 1;
1086 //printf("var %s in function %s is nested ref\n", toChars(), fdv->toChars());
1091 /*******************************
1092 * Does symbol go into data segment?
1095 int VarDeclaration::isDataseg()
1097 #if 0
1098 printf("VarDeclaration::isDataseg(%p, '%s')\n", this, toChars());
1099 printf("%x, %p, %p\n", storage_class & (STCstatic | STCconst), parent->isModule(), parent->isTemplateInstance());
1100 printf("parent = '%s'\n", parent->toChars());
1101 #endif
1102 Dsymbol *parent = this->toParent();
1103 if (!parent && !(storage_class & (STCstatic | STCconst)))
1104 { error("forward referenced");
1105 type = Type::terror;
1106 return 0;
1108 return (storage_class & (STCstatic | STCconst) ||
1109 parent->isModule() ||
1110 parent->isTemplateInstance());
1113 int VarDeclaration::hasPointers()
1115 return (!isDataseg() && type->hasPointers());
1118 /******************************************
1119 * If a variable has an auto destructor call, return call for it.
1120 * Otherwise, return NULL.
1123 Expression *VarDeclaration::callAutoDtor()
1124 { Expression *e = NULL;
1126 //printf("VarDeclaration::callAutoDtor() %s\n", toChars());
1127 if (storage_class & (STCauto | STCscope) && !noauto)
1129 for (ClassDeclaration *cd = type->isClassHandle();
1131 cd = cd->baseClass)
1133 /* We can do better if there's a way with onstack
1134 * classes to determine if there's no way the monitor
1135 * could be set.
1137 //if (cd->isInterfaceDeclaration())
1138 //error("interface %s cannot be scope", cd->toChars());
1139 if (1 || onstack || cd->dtors.dim) // if any destructors
1141 // delete this;
1142 Expression *ec;
1144 ec = new VarExp(loc, this);
1145 e = new DeleteExp(loc, ec);
1146 e->type = Type::tvoid;
1147 break;
1151 return e;
1155 /********************************* ClassInfoDeclaration ****************************/
1157 ClassInfoDeclaration::ClassInfoDeclaration(ClassDeclaration *cd)
1158 : VarDeclaration(0, ClassDeclaration::classinfo->type, cd->ident, NULL)
1160 this->cd = cd;
1161 storage_class = STCstatic;
1164 Dsymbol *ClassInfoDeclaration::syntaxCopy(Dsymbol *s)
1166 assert(0); // should never be produced by syntax
1167 return NULL;
1170 void ClassInfoDeclaration::semantic(Scope *sc)
1174 /********************************* ModuleInfoDeclaration ****************************/
1176 ModuleInfoDeclaration::ModuleInfoDeclaration(Module *mod)
1177 : VarDeclaration(0, Module::moduleinfo->type, mod->ident, NULL)
1179 this->mod = mod;
1180 storage_class = STCstatic;
1183 Dsymbol *ModuleInfoDeclaration::syntaxCopy(Dsymbol *s)
1185 assert(0); // should never be produced by syntax
1186 return NULL;
1189 void ModuleInfoDeclaration::semantic(Scope *sc)
1193 /********************************* TypeInfoDeclaration ****************************/
1195 TypeInfoDeclaration::TypeInfoDeclaration(Type *tinfo, int internal)
1196 : VarDeclaration(0, Type::typeinfo->type, tinfo->getTypeInfoIdent(internal), NULL)
1198 this->tinfo = tinfo;
1199 storage_class = STCstatic;
1200 protection = PROTpublic;
1201 linkage = LINKc;
1204 Dsymbol *TypeInfoDeclaration::syntaxCopy(Dsymbol *s)
1206 assert(0); // should never be produced by syntax
1207 return NULL;
1210 void TypeInfoDeclaration::semantic(Scope *sc)
1212 assert(linkage == LINKc);
1215 /***************************** TypeInfoConstDeclaration **********************/
1217 #if V2
1218 TypeInfoConstDeclaration::TypeInfoConstDeclaration(Type *tinfo)
1219 : TypeInfoDeclaration(tinfo, 0)
1222 #endif
1224 /***************************** TypeInfoInvariantDeclaration **********************/
1226 #if V2
1227 TypeInfoInvariantDeclaration::TypeInfoInvariantDeclaration(Type *tinfo)
1228 : TypeInfoDeclaration(tinfo, 0)
1231 #endif
1233 /***************************** TypeInfoStructDeclaration **********************/
1235 TypeInfoStructDeclaration::TypeInfoStructDeclaration(Type *tinfo)
1236 : TypeInfoDeclaration(tinfo, 0)
1240 /***************************** TypeInfoClassDeclaration ***********************/
1242 TypeInfoClassDeclaration::TypeInfoClassDeclaration(Type *tinfo)
1243 : TypeInfoDeclaration(tinfo, 0)
1247 /***************************** TypeInfoInterfaceDeclaration *******************/
1249 TypeInfoInterfaceDeclaration::TypeInfoInterfaceDeclaration(Type *tinfo)
1250 : TypeInfoDeclaration(tinfo, 0)
1254 /***************************** TypeInfoTypedefDeclaration *********************/
1256 TypeInfoTypedefDeclaration::TypeInfoTypedefDeclaration(Type *tinfo)
1257 : TypeInfoDeclaration(tinfo, 0)
1261 /***************************** TypeInfoPointerDeclaration *********************/
1263 TypeInfoPointerDeclaration::TypeInfoPointerDeclaration(Type *tinfo)
1264 : TypeInfoDeclaration(tinfo, 0)
1268 /***************************** TypeInfoMaybeDeclaration *********************/
1270 TypeInfoMaybeDeclaration::TypeInfoMaybeDeclaration(Type *tinfo)
1271 : TypeInfoDeclaration(tinfo, 0)
1275 /***************************** TypeInfoArrayDeclaration ***********************/
1277 TypeInfoArrayDeclaration::TypeInfoArrayDeclaration(Type *tinfo)
1278 : TypeInfoDeclaration(tinfo, 0)
1282 /***************************** TypeInfoStaticArrayDeclaration *****************/
1284 TypeInfoStaticArrayDeclaration::TypeInfoStaticArrayDeclaration(Type *tinfo)
1285 : TypeInfoDeclaration(tinfo, 0)
1289 /***************************** TypeInfoAssociativeArrayDeclaration ************/
1291 TypeInfoAssociativeArrayDeclaration::TypeInfoAssociativeArrayDeclaration(Type *tinfo)
1292 : TypeInfoDeclaration(tinfo, 0)
1296 /***************************** TypeInfoEnumDeclaration ***********************/
1298 TypeInfoEnumDeclaration::TypeInfoEnumDeclaration(Type *tinfo)
1299 : TypeInfoDeclaration(tinfo, 0)
1303 /***************************** TypeInfoFunctionDeclaration ********************/
1305 TypeInfoFunctionDeclaration::TypeInfoFunctionDeclaration(Type *tinfo)
1306 : TypeInfoDeclaration(tinfo, 0)
1310 /***************************** TypeInfoDelegateDeclaration ********************/
1312 TypeInfoDelegateDeclaration::TypeInfoDelegateDeclaration(Type *tinfo)
1313 : TypeInfoDeclaration(tinfo, 0)
1317 /***************************** TypeInfoTupleDeclaration **********************/
1319 TypeInfoTupleDeclaration::TypeInfoTupleDeclaration(Type *tinfo)
1320 : TypeInfoDeclaration(tinfo, 0)
1324 /********************************* ThisDeclaration ****************************/
1326 // For the "this" parameter to member functions
1328 ThisDeclaration::ThisDeclaration(Type *t)
1329 : VarDeclaration(0, t, Id::This, NULL)
1331 noauto = 1;
1334 Dsymbol *ThisDeclaration::syntaxCopy(Dsymbol *s)
1336 assert(0); // should never be produced by syntax
1337 return NULL;