2 // Compiler implementation of the D programming language
3 // Copyright (c) 1999-2008 by Digital Mars
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
21 #include "declaration.h"
26 #include "aggregate.h"
29 #include "expression.h"
32 /********************************* Declaration ****************************/
34 Declaration::Declaration(Identifier
*id
)
39 storage_class
= STCundefined
;
40 protection
= PROTundefined
;
41 linkage
= LINKdefault
;
45 void Declaration::semantic(Scope
*sc
)
49 char *Declaration::kind()
54 target_size_t
Declaration::size(Loc loc
)
60 int Declaration::isStaticConstructor()
65 int Declaration::isStaticDestructor()
70 int Declaration::isDelete()
75 int Declaration::isDataseg()
80 int Declaration::isCodeseg()
85 enum PROT
Declaration::prot()
90 /*************************************
91 * Check to see if declaration can be modified in this context (sc).
95 void Declaration::checkModify(Loc loc
, Scope
*sc
, Type
*t
)
97 if (sc
->incontract
&& isParameter())
98 error(loc
, "cannot modify parameter '%s' in contract", toChars());
101 { // It's only modifiable if inside the right constructor
102 Dsymbol
*s
= sc
->func
;
105 FuncDeclaration
*fd
= NULL
;
107 fd
= s
->isFuncDeclaration();
109 ((fd
->isCtorDeclaration() && storage_class
& STCfield
) ||
110 (fd
->isStaticCtorDeclaration() && !(storage_class
& STCfield
))) &&
111 fd
->toParent() == toParent()
114 VarDeclaration
*v
= isVarDeclaration();
117 //printf("setting ctorinit\n");
122 { s
= s
->toParent2();
127 const char *p
= isStatic() ? "static " : "";
128 error(loc
, "can only initialize %sconst %s inside %sconstructor",
137 VarDeclaration
*v
= isVarDeclaration();
138 if (v
&& v
->canassign
== 0)
143 else if (isInvariant())
145 else if (storage_class
& STCmanifest
)
146 p
= "manifest constant";
147 else if (!t
->isAssignable())
148 p
= "struct with immutable members";
150 { error(loc
, "cannot modify %s", p
);
158 /********************************* TupleDeclaration ****************************/
160 TupleDeclaration::TupleDeclaration(Loc loc
, Identifier
*id
, Objects
*objects
)
164 this->objects
= objects
;
166 this->tupletype
= NULL
;
169 Dsymbol
*TupleDeclaration::syntaxCopy(Dsymbol
*s
)
175 char *TupleDeclaration::kind()
180 Type
*TupleDeclaration::getType()
182 /* If this tuple represents a type, return that type
185 //printf("TupleDeclaration::getType() %s\n", toChars());
190 /* It's only a type tuple if all the Object's are types
192 for (size_t i
= 0; i
< objects
->dim
; i
++)
193 { Object
*o
= (Object
*)objects
->data
[i
];
195 if (o
->dyncast() != DYNCAST_TYPE
)
197 //printf("\tnot[%d], %p, %d\n", i, o, o->dyncast());
202 /* We know it's a type tuple, so build the TypeTuple
204 Arguments
*args
= new Arguments();
205 args
->setDim(objects
->dim
);
207 for (size_t i
= 0; i
< objects
->dim
; i
++)
208 { Type
*t
= (Type
*)objects
->data
[i
];
210 //printf("type = %s\n", t->toChars());
212 buf
.printf("_%s_%d", ident
->toChars(), i
);
213 char *name
= (char *)buf
.extractData();
214 Identifier
*id
= new Identifier(name
, TOKidentifier
);
215 Argument
*arg
= new Argument(STCin
, t
, id
, NULL
);
217 Argument
*arg
= new Argument(0, t
, NULL
, NULL
);
219 args
->data
[i
] = (void *)arg
;
222 tupletype
= new TypeTuple(args
);
228 int TupleDeclaration::needThis()
230 //printf("TupleDeclaration::needThis(%s)\n", toChars());
231 for (size_t i
= 0; i
< objects
->dim
; i
++)
232 { Object
*o
= (Object
*)objects
->data
[i
];
233 if (o
->dyncast() == DYNCAST_EXPRESSION
)
234 { Expression
*e
= (Expression
*)o
;
235 if (e
->op
== TOKdsymbol
)
236 { DsymbolExp
*ve
= (DsymbolExp
*)e
;
237 Declaration
*d
= ve
->s
->isDeclaration();
238 if (d
&& d
->needThis())
248 /********************************* TypedefDeclaration ****************************/
250 TypedefDeclaration::TypedefDeclaration(Loc loc
, Identifier
*id
, Type
*basetype
, Initializer
*init
)
253 this->type
= new TypeTypedef(this);
254 this->basetype
= basetype
->toBasetype();
258 this->hbasetype
= NULL
;
266 Dsymbol
*TypedefDeclaration::syntaxCopy(Dsymbol
*s
)
268 Type
*basetype
= this->basetype
->syntaxCopy();
270 Initializer
*init
= NULL
;
272 init
= this->init
->syntaxCopy();
275 TypedefDeclaration
*st
;
276 st
= new TypedefDeclaration(loc
, ident
, basetype
, init
);
278 // Syntax copy for header file
279 if (!htype
) // Don't overwrite original
280 { if (type
) // Make copy for both old and new instances
281 { htype
= type
->syntaxCopy();
282 st
->htype
= type
->syntaxCopy();
285 else // Make copy of original for new instance
286 st
->htype
= htype
->syntaxCopy();
289 { hbasetype
= basetype
->syntaxCopy();
290 st
->hbasetype
= basetype
->syntaxCopy();
294 st
->hbasetype
= hbasetype
->syntaxCopy();
299 void TypedefDeclaration::semantic(Scope
*sc
)
301 //printf("TypedefDeclaration::semantic(%s) sem = %d\n", toChars(), sem);
304 basetype
= basetype
->semantic(loc
, sc
);
306 type
= type
->semantic(loc
, sc
);
308 attributes
->append(sc
->attributes
);
310 attributes
= sc
->attributes
;
311 if (sc
->parent
->isFuncDeclaration() && init
)
316 error("circular definition");
320 void TypedefDeclaration::semantic2(Scope
*sc
)
322 //printf("TypedefDeclaration::semantic2(%s) sem = %d\n", toChars(), sem);
327 init
= init
->semantic(sc
, basetype
);
329 ExpInitializer
*ie
= init
->isExpInitializer();
332 if (ie
->exp
->type
== basetype
)
333 ie
->exp
->type
= type
;
339 char *TypedefDeclaration::kind()
344 Type
*TypedefDeclaration::getType()
349 void TypedefDeclaration::toCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
)
351 buf
->writestring("typedef ");
352 basetype
->toCBuffer(buf
, ident
, hgs
);
355 buf
->writestring(" = ");
356 init
->toCBuffer(buf
, hgs
);
362 /********************************* AliasDeclaration ****************************/
364 AliasDeclaration::AliasDeclaration(Loc loc
, Identifier
*id
, Type
*type
)
367 //printf("AliasDeclaration(id = '%s', type = %p)\n", id->toChars(), type);
368 //printf("type = '%s'\n", type->toChars());
371 this->aliassym
= NULL
;
374 this->haliassym
= NULL
;
376 this->overnext
= NULL
;
377 this->inSemantic
= 0;
381 AliasDeclaration::AliasDeclaration(Loc loc
, Identifier
*id
, Dsymbol
*s
)
384 //printf("AliasDeclaration(id = '%s', s = %p)\n", id->toChars(), s);
391 this->haliassym
= NULL
;
393 this->overnext
= NULL
;
394 this->inSemantic
= 0;
398 Dsymbol
*AliasDeclaration::syntaxCopy(Dsymbol
*s
)
400 //printf("AliasDeclaration::syntaxCopy()\n");
402 AliasDeclaration
*sa
;
404 sa
= new AliasDeclaration(loc
, ident
, type
->syntaxCopy());
406 sa
= new AliasDeclaration(loc
, ident
, aliassym
->syntaxCopy(NULL
));
408 // Syntax copy for header file
409 if (!htype
) // Don't overwrite original
410 { if (type
) // Make copy for both old and new instances
411 { htype
= type
->syntaxCopy();
412 sa
->htype
= type
->syntaxCopy();
415 else // Make copy of original for new instance
416 sa
->htype
= htype
->syntaxCopy();
419 { haliassym
= aliassym
->syntaxCopy(s
);
420 sa
->haliassym
= aliassym
->syntaxCopy(s
);
424 sa
->haliassym
= haliassym
->syntaxCopy(s
);
429 void AliasDeclaration::semantic(Scope
*sc
)
431 //printf("AliasDeclaration::semantic() %s\n", toChars());
434 if (aliassym
->isTemplateInstance())
435 aliassym
->semantic(sc
);
438 this->inSemantic
= 1;
440 if (storage_class
& STCconst
)
441 error("cannot be const");
443 storage_class
|= sc
->stc
& STCdeprecated
;
446 // alias foo.bar.abc def;
447 // it is not knowable from the syntax whether this is an alias
448 // for a type or an alias for a symbol. It is up to the semantic()
449 // pass to distinguish.
450 // If it is a type, then type is set and getType() will return that
451 // type. If it is a symbol, then aliassym is set and type is NULL -
452 // toAlias() will return aliasssym.
458 /* This section is needed because resolve() will:
461 * try to alias y to 3.
463 s
= type
->toDsymbol(sc
);
464 if (s
&& ((s
->getType() && type
->equals(s
->getType())) || s
->isEnumMember()))
465 goto L2
; // it's a symbolic alias
467 //printf("alias type is %s\n", type->toChars());
468 type
->resolve(loc
, sc
, &e
, &t
, &s
);
475 // Try to convert Expression to Dsymbol
480 error("cannot alias an expression %s", e
->toChars());
486 ScopeDsymbol::multiplyDefined(0, this, overnext
);
487 this->inSemantic
= 0;
491 //printf("alias is a symbol %s %s\n", s->kind(), s->toChars());
493 VarDeclaration
*v
= s
->isVarDeclaration();
494 if (v
&& v
->linkage
== LINKdefault
)
496 error("forward reference of %s", v
->toChars());
501 FuncDeclaration
*f
= s
->toAlias()->isFuncDeclaration();
506 FuncAliasDeclaration
*fa
= new FuncAliasDeclaration(f
);
507 if (!fa
->overloadInsert(overnext
))
508 ScopeDsymbol::multiplyDefined(0, f
, overnext
);
511 s
->parent
= sc
->parent
;
515 ScopeDsymbol::multiplyDefined(0, s
, overnext
);
518 assert(global
.errors
);
523 this->inSemantic
= 0;
526 int AliasDeclaration::overloadInsert(Dsymbol
*s
)
528 /* Don't know yet what the aliased symbol is, so assume it can
529 * be overloaded and check later for correctness.
532 //printf("AliasDeclaration::overloadInsert('%s')\n", s->toChars());
533 if (overnext
== NULL
)
539 return overnext
->overloadInsert(s
);
543 char *AliasDeclaration::kind()
548 Type
*AliasDeclaration::getType()
553 Dsymbol
*AliasDeclaration::toAlias()
555 //printf("AliasDeclaration::toAlias('%s', this = %p, aliassym = %p, kind = '%s')\n", toChars(), this, aliassym, aliassym ? aliassym->kind() : "");
556 assert(this != aliassym
);
557 //static int count; if (++count == 10) *(char*)0=0;
559 { error("recursive alias declaration");
562 Dsymbol
*s
= aliassym
? aliassym
->toAlias() : this;
566 void AliasDeclaration::toCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
)
568 buf
->writestring("alias ");
574 haliassym
->toCBuffer(buf
, hgs
);
576 buf
->writestring(ident
->toChars());
579 htype
->toCBuffer(buf
, ident
, hgs
);
586 aliassym
->toCBuffer(buf
, hgs
);
588 buf
->writestring(ident
->toChars());
591 type
->toCBuffer(buf
, ident
, hgs
);
597 /********************************* VarDeclaration ****************************/
599 VarDeclaration::VarDeclaration(Loc loc
, Type
*type
, Identifier
*id
, Initializer
*init
)
602 //printf("VarDeclaration('%s')\n", id->toChars());
605 { printf("VarDeclaration('%s')\n", id
->toChars());
609 assert(type
|| init
);
628 Dsymbol
*VarDeclaration::syntaxCopy(Dsymbol
*s
)
630 //printf("VarDeclaration::syntaxCopy(%s)\n", toChars());
634 { sv
= (VarDeclaration
*)s
;
638 Initializer
*init
= NULL
;
640 { init
= this->init
->syntaxCopy();
641 //init->isExpInitializer()->exp->print();
642 //init->isExpInitializer()->exp->dump(0);
645 sv
= new VarDeclaration(loc
, type
? type
->syntaxCopy() : NULL
, ident
, init
);
646 sv
->storage_class
= storage_class
;
649 // Syntax copy for header file
650 if (!htype
) // Don't overwrite original
651 { if (type
) // Make copy for both old and new instances
652 { htype
= type
->syntaxCopy();
653 sv
->htype
= type
->syntaxCopy();
656 else // Make copy of original for new instance
657 sv
->htype
= htype
->syntaxCopy();
660 { hinit
= init
->syntaxCopy();
661 sv
->hinit
= init
->syntaxCopy();
665 sv
->hinit
= hinit
->syntaxCopy();
670 void VarDeclaration::semantic(Scope
*sc
)
672 //printf("VarDeclaration::semantic('%s', parent = '%s')\n", toChars(), sc->parent->toChars());
673 //printf(" type = %s\n", type ? type->toChars() : "null");
674 //printf(" stc = x%x\n", sc->stc);
675 //printf(" storage_class = x%x\n", storage_class);
676 //printf("linkage = %d\n", sc->linkage);
677 //if (strcmp(toChars(), "mul") == 0) halt();
679 storage_class
|= sc
->stc
;
680 if (storage_class
& STCextern
&& init
)
681 error("extern symbols cannot have initializers");
683 /* If auto type inference, do the inference
688 type
= init
->inferType(sc
);
692 /* This is a kludge to support the existing syntax for RAII
695 storage_class
&= ~STCauto
;
701 type
= type
->semantic(loc
, sc
);
703 //printf(" semantic type = %s\n", type ? type->toChars() : "null");
705 type
->checkDeprecated(loc
, sc
);
706 linkage
= sc
->linkage
;
707 this->parent
= sc
->parent
;
708 //printf("this = %p, parent = %p, '%s'\n", this, parent, parent->toChars());
709 protection
= sc
->protection
;
711 attributes
->append(sc
->attributes
);
713 attributes
= sc
->attributes
;
714 //printf("sc->stc = %x\n", sc->stc);
715 //printf("storage_class = x%x\n", storage_class);
717 Dsymbol
*parent
= toParent();
718 FuncDeclaration
*fd
= parent
->isFuncDeclaration();
720 Type
*tb
= type
->toBasetype();
721 if (tb
->ty
== Tvoid
&& !(storage_class
& STClazy
))
722 { error("voids have no value");
726 if (tb
->ty
== Tfunction
)
727 { error("cannot be declared to be a function");
731 if (tb
->ty
== Tstruct
)
732 { TypeStruct
*ts
= (TypeStruct
*)tb
;
734 if (!ts
->sym
->members
)
736 error("no definition of struct %s", ts
->toChars());
740 if (tb
->ty
== Ttuple
)
741 { /* Instead, declare variables for each of the tuple elements
744 TypeTuple
*tt
= (TypeTuple
*)tb
;
745 size_t nelems
= Argument::dim(tt
->arguments
);
746 Objects
*exps
= new Objects();
747 exps
->setDim(nelems
);
748 Expression
*ie
= init
? init
->toExpression() : NULL
;
750 for (size_t i
= 0; i
< nelems
; i
++)
751 { Argument
*arg
= Argument::getNth(tt
->arguments
, i
);
754 buf
.printf("_%s_field_%"PRIuSIZE
, ident
->toChars(), i
);
756 char *name
= (char *)buf
.extractData();
757 Identifier
*id
= new Identifier(name
, TOKidentifier
);
759 Expression
*einit
= ie
;
760 if (ie
&& ie
->op
== TOKtuple
)
761 { einit
= (Expression
*)((TupleExp
*)ie
)->exps
->data
[i
];
763 Initializer
*ti
= init
;
765 { ti
= new ExpInitializer(einit
->loc
, einit
);
768 VarDeclaration
*v
= new VarDeclaration(loc
, arg
->type
, id
, ti
);
769 //printf("declaring field %s of type %s\n", v->toChars(), v->type->toChars());
773 { //printf("adding %s to %s\n", v->toChars(), sc->scopesym->toChars());
774 if (sc
->scopesym
->members
)
775 sc
->scopesym
->members
->push(v
);
778 Expression
*e
= new DsymbolExp(loc
, v
);
781 TupleDeclaration
*v2
= new TupleDeclaration(loc
, ident
, exps
);
788 if (storage_class
& STCinvariant
)
790 type
= type
->invariantOf();
792 else if (storage_class
& (STCconst
| STCin
))
794 if (!type
->isInvariant())
795 type
= type
->constOf();
797 else if (type
->isConst())
798 storage_class
|= STCconst
;
799 else if (type
->isInvariant())
800 storage_class
|= STCinvariant
;
802 if (isSynchronized())
804 error("variable %s cannot be synchronized", toChars());
806 else if (isOverride())
808 error("override cannot be applied to variable");
810 else if (isAbstract())
812 error("abstract cannot be applied to variable");
814 else if (storage_class
& STCfinal
)
816 error("final cannot be applied to variable");
819 if (storage_class
& (STCstatic
| STCextern
| STCmanifest
| STCtemplateparameter
| STCtls
))
824 AggregateDeclaration
*aad
= sc
->anonAgg
;
826 aad
= parent
->isAggregateDeclaration();
828 { assert(!(storage_class
& (STCextern
| STCstatic
| STCtls
)));
830 if (storage_class
& (STCconst
| STCinvariant
) && init
)
832 if (!type
->toBasetype()->isTypeBasic())
833 storage_class
|= STCstatic
;
836 aad
->addField(sc
, this);
839 InterfaceDeclaration
*id
= parent
->isInterfaceDeclaration();
842 error("field not allowed in interface");
845 /* Templates cannot add fields to aggregates
847 TemplateInstance
*ti
= parent
->isTemplateInstance();
850 // Take care of nested templates
853 TemplateInstance
*ti2
= ti
->tempdecl
->parent
->isTemplateInstance();
859 // If it's a member template
860 AggregateDeclaration
*ad
= ti
->tempdecl
->isMember();
861 if (ad
&& storage_class
!= STCundefined
)
863 error("cannot use template to add field to aggregate '%s'", ad
->toChars());
868 if (type
->isauto() && !noauto
)
870 if (storage_class
& (STCfield
| STCout
| STCref
| STCstatic
| STCmanifest
| STCtls
) || !fd
)
872 error("globals, statics, fields, manifest constants, ref and out parameters cannot be auto");
875 if (!(storage_class
& (STCauto
| STCscope
)))
877 if (!(storage_class
& STCparameter
) && ident
!= Id::withSym
)
878 error("reference to scope class must be scope");
882 if ((isConst() || isInvariant()) && !init
&& !fd
)
883 { // Initialize by constructor only
884 storage_class
|= STCctorinit
;
888 storage_class
|= STCinit
; // remember we had an explicit initializer
889 else if (storage_class
& STCmanifest
)
890 error("manifest constants must have initializers");
892 enum TOK op
= TOKconstruct
;
893 if (!init
&& !sc
->inunion
&& !isStatic() && fd
&&
894 (!(storage_class
& (STCfield
| STCin
| STCforeach
| STCparameter
)) || (storage_class
& STCout
)) &&
897 // Provide a default initializer
898 //printf("Providing default initializer for '%s'\n", toChars());
899 if (type
->ty
== Tstruct
&&
900 ((TypeStruct
*)type
)->sym
->zeroInit
== 1)
901 { /* If a struct is all zeros, as a special case
902 * set it's initializer to the integer 0.
903 * In AssignExp::toElem(), we check for this and issue
904 * a memset() to initialize the struct.
905 * Must do same check in interpreter.
907 Expression
*e
= new IntegerExp(loc
, 0, Type::tint32
);
909 e1
= new VarExp(loc
, this);
910 e
= new AssignExp(loc
, e1
, e
);
911 e
->type
= e1
->type
; // don't type check this, it would fail
912 init
= new ExpInitializer(loc
, e
);
915 else if (type
->ty
== Ttypedef
)
916 { TypeTypedef
*td
= (TypeTypedef
*)type
;
918 { init
= td
->sym
->init
;
919 ExpInitializer
*ie
= init
->isExpInitializer();
921 // Make copy so we can modify it
922 init
= new ExpInitializer(ie
->loc
, ie
->exp
);
925 init
= getExpInitializer();
929 init
= getExpInitializer();
931 // Default initializer is always a blit
937 ArrayInitializer
*ai
= init
->isArrayInitializer();
938 if (ai
&& tb
->ty
== Taarray
)
940 init
= ai
->toAssocArrayInitializer();
943 StructInitializer
*si
= init
->isStructInitializer();
944 ExpInitializer
*ei
= init
->isExpInitializer();
946 // See if we can allocate on the stack
947 if (ei
&& isScope() && ei
->exp
->op
== TOKnew
)
948 { NewExp
*ne
= (NewExp
*)ei
->exp
;
949 if (!(ne
->newargs
&& ne
->newargs
->dim
))
952 if (type
->isBaseOf(ne
->newtype
->semantic(loc
, sc
), NULL
))
957 // If inside function, there is no semantic3() call
960 // If local variable, use AssignExp to handle all the various
962 if (fd
&& !isStatic() && !(storage_class
& STCmanifest
) &&
963 !init
->isVoidInitializer())
965 //printf("fd = '%s', var = '%s'\n", fd->toChars(), toChars());
968 Expression
*e
= init
->toExpression();
971 init
= init
->semantic(sc
, type
);
972 e
= init
->toExpression();
974 { error("is not a static and cannot have static initializer");
978 ei
= new ExpInitializer(init
->loc
, e
);
982 Expression
*e1
= new VarExp(loc
, this);
984 Type
*t
= type
->toBasetype();
985 if (t
->ty
== Tsarray
)
987 ei
->exp
= ei
->exp
->semantic(sc
);
988 if (!ei
->exp
->implicitConvTo(type
))
990 sinteger_t dim
= ((TypeSArray
*)t
)->dim
->toInteger();
991 // If multidimensional static array, treat as one large array
994 t
= t
->nextOf()->toBasetype();
995 if (t
->ty
!= Tsarray
)
997 dim
*= ((TypeSArray
*)t
)->dim
->toInteger();
998 e1
->type
= new TypeSArray(t
->nextOf(), new IntegerExp(0, dim
, Type::tindex
));
1001 e1
= new SliceExp(loc
, e1
, NULL
, NULL
);
1003 else if (t
->ty
== Tstruct
)
1005 ei
->exp
= ei
->exp
->semantic(sc
);
1006 if (!ei
->exp
->implicitConvTo(type
))
1007 { Type
*ti
= ei
->exp
->type
->toBasetype();
1008 // Don't cast away invariant or mutability in initializer
1009 if (!(ti
->ty
== Tstruct
&& t
->toDsymbol(sc
) == ti
->toDsymbol(sc
)))
1010 ei
->exp
= new CastExp(loc
, ei
->exp
, type
);
1013 ei
->exp
= new AssignExp(loc
, e1
, ei
->exp
);
1016 ei
->exp
= ei
->exp
->semantic(sc
);
1018 ei
->exp
->optimize(WANTvalue
);
1022 init
= init
->semantic(sc
, type
);
1025 else if (storage_class
& (STCconst
| STCinvariant
| STCmanifest
) ||
1026 type
->isConst() || type
->isInvariant())
1028 /* Because we may need the results of a const declaration in a
1029 * subsequent type, such as an array dimension, before semantic2()
1030 * gets ordinarily run, try to run semantic2() now.
1034 if (!global
.errors
&& !inferred
)
1036 unsigned errors
= global
.errors
;
1040 Initializer
*i2
= init
;
1044 e
= ei
->exp
->syntaxCopy();
1045 e
= e
->semantic(sc
);
1046 e
= e
->implicitCastTo(sc
, type
);
1049 { i2
= init
->syntaxCopy();
1050 i2
= i2
->semantic(sc
, type
);
1055 if (errors
!= global
.errors
) // if errors happened
1057 if (global
.gag
== 0)
1058 global
.errors
= errors
; // act as if nothing happened
1060 /* Save scope for later use, to try again
1062 scope
= new Scope(*sc
);
1068 /* static const/invariant does CTFE
1070 e
= e
->optimize(WANTvalue
| WANTinterpret
);
1072 e
= e
->optimize(WANTvalue
);
1073 if (e
->op
== TOKint64
|| e
->op
== TOKstring
)
1075 ei
->exp
= e
; // no errors, keep result
1079 /* Save scope for later use, to try again
1081 scope
= new Scope(*sc
);
1086 init
= i2
; // no errors, keep result
1092 void VarDeclaration::semantic2(Scope
*sc
)
1094 //printf("VarDeclaration::semantic2('%s')\n", toChars());
1095 if (init
&& !toParent()->isFuncDeclaration())
1098 ExpInitializer
*ei
= init
->isExpInitializer();
1102 printf("type = %p\n", ei
->exp
->type
);
1105 init
= init
->semantic(sc
, type
);
1110 char *VarDeclaration::kind()
1115 Dsymbol
*VarDeclaration::toAlias()
1117 //printf("VarDeclaration::toAlias('%s', this = %p, aliassym = %p)\n", toChars(), this, aliassym);
1118 assert(this != aliassym
);
1119 Dsymbol
*s
= aliassym
? aliassym
->toAlias() : this;
1123 void VarDeclaration::toCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
)
1125 if (storage_class
& STCmanifest
)
1126 buf
->writestring("manifest ");
1127 if (storage_class
& STCstatic
)
1128 buf
->writestring("static ");
1129 if (storage_class
& STCtls
)
1130 buf
->writestring("__tls ");
1131 if (storage_class
& STCconst
)
1132 buf
->writestring("const ");
1133 if (storage_class
& STCinvariant
)
1134 buf
->writestring("invariant ");
1137 type
->toCBuffer(buf
, ident
, hgs
);
1139 buf
->writestring(ident
->toChars());
1141 { buf
->writestring(" = ");
1142 ExpInitializer
*ie
= init
->isExpInitializer();
1143 if (ie
&& (ie
->exp
->op
== TOKconstruct
|| ie
->exp
->op
== TOKblit
))
1144 ((AssignExp
*)ie
->exp
)->e2
->toCBuffer(buf
, hgs
);
1146 init
->toCBuffer(buf
, hgs
);
1148 buf
->writeByte(';');
1152 int VarDeclaration::needThis()
1154 //printf("VarDeclaration::needThis(%s, x%x)\n", toChars(), storage_class);
1155 return storage_class
& STCfield
;
1158 int VarDeclaration::isImportedSymbol()
1160 if (protection
== PROTexport
&& !init
&&
1161 (storage_class
& STCstatic
|| parent
->isModule()))
1166 void VarDeclaration::checkCtorConstInit()
1168 if (ctorinit
== 0 && isCtorinit() && !(storage_class
& STCfield
))
1169 error("missing initializer in static constructor for const variable");
1172 /************************************
1173 * Check to see if this variable is actually in an enclosing function
1174 * rather than the current one.
1177 void VarDeclaration::checkNestedReference(Scope
*sc
, Loc loc
)
1179 if (parent
&& !isDataseg() && parent
!= sc
->parent
&&
1180 !(storage_class
& STCmanifest
))
1182 // The function that this variable is in
1183 FuncDeclaration
*fdv
= toParent()->isFuncDeclaration();
1184 // The current function
1185 FuncDeclaration
*fdthis
= sc
->parent
->isFuncDeclaration();
1187 if (fdv
&& fdthis
&& fdv
!= fdthis
)
1190 fdthis
->getLevel(loc
, fdv
);
1192 for (int i
= 0; i
< nestedrefs
.dim
; i
++)
1193 { FuncDeclaration
*f
= (FuncDeclaration
*)nestedrefs
.data
[i
];
1197 nestedrefs
.push(fdthis
);
1201 for (int i
= 0; i
< fdv
->closureVars
.dim
; i
++)
1202 { Dsymbol
*s
= (Dsymbol
*)fdv
->closureVars
.data
[i
];
1207 fdv
->closureVars
.push(this);
1210 //printf("var %s in function %s is nested ref\n", toChars(), fdv->toChars());
1215 /****************************
1216 * Get ExpInitializer for a variable, if there is one.
1219 ExpInitializer
*VarDeclaration::getExpInitializer()
1224 ei
= init
->isExpInitializer();
1227 Expression
*e
= type
->defaultInit();
1229 ei
= new ExpInitializer(loc
, e
);
1236 /*******************************************
1237 * If variable has a constant expression initializer, get it.
1238 * Otherwise, return NULL.
1241 Expression
*VarDeclaration::getConstInitializer()
1243 if ((isConst() || isInvariant() || storage_class
& STCmanifest
) &&
1244 storage_class
& STCinit
)
1246 ExpInitializer
*ei
= getExpInitializer();
1254 /*************************************
1255 * Return !=0 if we can take the address of this variable.
1258 int VarDeclaration::canTakeAddressOf()
1261 /* Global variables and struct/class fields of the form:
1263 * are not stored and hence cannot have their address taken.
1265 if ((isConst() || isInvariant()) &&
1266 storage_class
& STCinit
&&
1267 (!(storage_class
& (STCstatic
| STCextern
)) || (storage_class
& STCfield
)) &&
1268 (!parent
|| toParent()->isModule() || toParent()->isTemplateInstance()) &&
1269 type
->toBasetype()->isTypeBasic()
1275 if (storage_class
& STCmanifest
)
1281 /*******************************
1282 * Does symbol go into data segment?
1283 * Includes extern variables.
1286 int VarDeclaration::isDataseg()
1289 printf("VarDeclaration::isDataseg(%p, '%s')\n", this, toChars());
1290 printf("%x, %p, %p\n", storage_class
& (STCstatic
| STCconst
), parent
->isModule(), parent
->isTemplateInstance());
1291 printf("parent = '%s'\n", parent
->toChars());
1293 if (storage_class
& STCmanifest
)
1295 Dsymbol
*parent
= this->toParent();
1296 if (!parent
&& !(storage_class
& STCstatic
))
1297 { error("forward referenced");
1298 type
= Type::terror
;
1301 return canTakeAddressOf() &&
1302 (storage_class
& (STCstatic
| STCextern
| STCtls
) ||
1303 toParent()->isModule() ||
1304 toParent()->isTemplateInstance());
1307 int VarDeclaration::hasPointers()
1309 //printf("VarDeclaration::hasPointers() %s, ty = %d\n", toChars(), type->ty);
1310 return (!isDataseg() && type
->hasPointers());
1313 /******************************************
1314 * Return TRUE if variable needs to call the destructor.
1317 int VarDeclaration::needsAutoDtor()
1319 //printf("VarDeclaration::needsAutoDtor() %s\n", toChars());
1321 if (noauto
|| storage_class
& STCnodtor
)
1324 // Destructors for structs and arrays of structs
1325 Type
*tv
= type
->toBasetype();
1326 while (tv
->ty
== Tsarray
)
1327 { TypeSArray
*ta
= (TypeSArray
*)tv
;
1328 tv
= tv
->nextOf()->toBasetype();
1330 if (tv
->ty
== Tstruct
)
1331 { TypeStruct
*ts
= (TypeStruct
*)tv
;
1332 StructDeclaration
*sd
= ts
->sym
;
1337 // Destructors for classes
1338 if (storage_class
& (STCauto
| STCscope
))
1340 if (type
->isClassHandle())
1347 /******************************************
1348 * If a variable has an auto destructor call, return call for it.
1349 * Otherwise, return NULL.
1352 Expression
*VarDeclaration::callAutoDtor(Scope
*sc
)
1353 { Expression
*e
= NULL
;
1355 //printf("VarDeclaration::callAutoDtor() %s\n", toChars());
1357 if (noauto
|| storage_class
& STCnodtor
)
1360 // Destructors for structs and arrays of structs
1362 Type
*tv
= type
->toBasetype();
1363 while (tv
->ty
== Tsarray
)
1364 { TypeSArray
*ta
= (TypeSArray
*)tv
;
1366 tv
= tv
->nextOf()->toBasetype();
1368 if (tv
->ty
== Tstruct
)
1369 { TypeStruct
*ts
= (TypeStruct
*)tv
;
1370 StructDeclaration
*sd
= ts
->sym
;
1375 // Typeinfo.destroy(cast(void*)&v);
1376 Expression
*ea
= new SymOffExp(loc
, this, 0, 0);
1377 ea
= new CastExp(loc
, ea
, Type::tvoid
->pointerTo());
1378 Expressions
*args
= new Expressions();
1381 Expression
*et
= type
->getTypeInfo(sc
);
1382 et
= new DotIdExp(loc
, et
, Id::destroy
);
1384 e
= new CallExp(loc
, et
, args
);
1388 e
= new VarExp(loc
, this);
1389 e
= new DotVarExp(loc
, e
, sd
->dtor
, 0);
1390 e
= new CallExp(loc
, e
);
1396 // Destructors for classes
1397 if (storage_class
& (STCauto
| STCscope
))
1399 for (ClassDeclaration
*cd
= type
->isClassHandle();
1403 /* We can do better if there's a way with onstack
1404 * classes to determine if there's no way the monitor
1407 //if (cd->isInterfaceDeclaration())
1408 //error("interface %s cannot be scope", cd->toChars());
1409 if (1 || onstack
|| cd
->dtors
.dim
) // if any destructors
1414 ec
= new VarExp(loc
, this);
1415 e
= new DeleteExp(loc
, ec
);
1416 e
->type
= Type::tvoid
;
1425 /********************************* ClassInfoDeclaration ****************************/
1427 ClassInfoDeclaration::ClassInfoDeclaration(ClassDeclaration
*cd
)
1428 : VarDeclaration(0, ClassDeclaration::classinfo
->type
, cd
->ident
, NULL
)
1431 storage_class
= STCstatic
;
1434 Dsymbol
*ClassInfoDeclaration::syntaxCopy(Dsymbol
*s
)
1436 assert(0); // should never be produced by syntax
1440 void ClassInfoDeclaration::semantic(Scope
*sc
)
1444 /********************************* ModuleInfoDeclaration ****************************/
1446 ModuleInfoDeclaration::ModuleInfoDeclaration(Module
*mod
)
1447 : VarDeclaration(0, Module::moduleinfo
->type
, mod
->ident
, NULL
)
1450 storage_class
= STCstatic
;
1453 Dsymbol
*ModuleInfoDeclaration::syntaxCopy(Dsymbol
*s
)
1455 assert(0); // should never be produced by syntax
1459 void ModuleInfoDeclaration::semantic(Scope
*sc
)
1463 /********************************* TypeInfoDeclaration ****************************/
1465 TypeInfoDeclaration::TypeInfoDeclaration(Type
*tinfo
, int internal
)
1466 : VarDeclaration(0, Type::typeinfo
->type
, tinfo
->getTypeInfoIdent(internal
), NULL
)
1468 this->tinfo
= tinfo
;
1469 storage_class
= STCstatic
;
1470 protection
= PROTpublic
;
1474 Dsymbol
*TypeInfoDeclaration::syntaxCopy(Dsymbol
*s
)
1476 assert(0); // should never be produced by syntax
1480 void TypeInfoDeclaration::semantic(Scope
*sc
)
1482 assert(linkage
== LINKc
);
1485 /***************************** TypeInfoConstDeclaration **********************/
1488 TypeInfoConstDeclaration::TypeInfoConstDeclaration(Type
*tinfo
)
1489 : TypeInfoDeclaration(tinfo
, 0)
1494 /***************************** TypeInfoInvariantDeclaration **********************/
1497 TypeInfoInvariantDeclaration::TypeInfoInvariantDeclaration(Type
*tinfo
)
1498 : TypeInfoDeclaration(tinfo
, 0)
1503 /***************************** TypeInfoStructDeclaration **********************/
1505 TypeInfoStructDeclaration::TypeInfoStructDeclaration(Type
*tinfo
)
1506 : TypeInfoDeclaration(tinfo
, 0)
1510 /***************************** TypeInfoClassDeclaration ***********************/
1512 TypeInfoClassDeclaration::TypeInfoClassDeclaration(Type
*tinfo
)
1513 : TypeInfoDeclaration(tinfo
, 0)
1517 /***************************** TypeInfoInterfaceDeclaration *******************/
1519 TypeInfoInterfaceDeclaration::TypeInfoInterfaceDeclaration(Type
*tinfo
)
1520 : TypeInfoDeclaration(tinfo
, 0)
1524 /***************************** TypeInfoTypedefDeclaration *********************/
1526 TypeInfoTypedefDeclaration::TypeInfoTypedefDeclaration(Type
*tinfo
)
1527 : TypeInfoDeclaration(tinfo
, 0)
1531 /***************************** TypeInfoPointerDeclaration *********************/
1533 TypeInfoPointerDeclaration::TypeInfoPointerDeclaration(Type
*tinfo
)
1534 : TypeInfoDeclaration(tinfo
, 0)
1538 /***************************** TypeInfoArrayDeclaration ***********************/
1540 TypeInfoArrayDeclaration::TypeInfoArrayDeclaration(Type
*tinfo
)
1541 : TypeInfoDeclaration(tinfo
, 0)
1545 /***************************** TypeInfoStaticArrayDeclaration *****************/
1547 TypeInfoStaticArrayDeclaration::TypeInfoStaticArrayDeclaration(Type
*tinfo
)
1548 : TypeInfoDeclaration(tinfo
, 0)
1552 /***************************** TypeInfoAssociativeArrayDeclaration ************/
1554 TypeInfoAssociativeArrayDeclaration::TypeInfoAssociativeArrayDeclaration(Type
*tinfo
)
1555 : TypeInfoDeclaration(tinfo
, 0)
1559 /***************************** TypeInfoEnumDeclaration ***********************/
1561 TypeInfoEnumDeclaration::TypeInfoEnumDeclaration(Type
*tinfo
)
1562 : TypeInfoDeclaration(tinfo
, 0)
1566 /***************************** TypeInfoFunctionDeclaration ********************/
1568 TypeInfoFunctionDeclaration::TypeInfoFunctionDeclaration(Type
*tinfo
)
1569 : TypeInfoDeclaration(tinfo
, 0)
1573 /***************************** TypeInfoDelegateDeclaration ********************/
1575 TypeInfoDelegateDeclaration::TypeInfoDelegateDeclaration(Type
*tinfo
)
1576 : TypeInfoDeclaration(tinfo
, 0)
1580 /***************************** TypeInfoTupleDeclaration **********************/
1582 TypeInfoTupleDeclaration::TypeInfoTupleDeclaration(Type
*tinfo
)
1583 : TypeInfoDeclaration(tinfo
, 0)
1587 /********************************* ThisDeclaration ****************************/
1589 // For the "this" parameter to member functions
1591 ThisDeclaration::ThisDeclaration(Type
*t
)
1592 : VarDeclaration(0, t
, Id::This
, NULL
)
1597 Dsymbol
*ThisDeclaration::syntaxCopy(Dsymbol
*s
)
1599 assert(0); // should never be produced by syntax