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
17 #ifndef DMD_DECLARATION_H
18 #define DMD_DECLARATION_H
33 struct InlineScanState
;
34 struct ForeachStatement
;
35 struct FuncDeclaration
;
36 struct ExpInitializer
;
37 struct StructDeclaration
;
59 STCsynchronized
= 0x200,
60 STCdeprecated
= 0x400,
61 STCin
= 0x800, // in parameter
62 STCout
= 0x1000, // out parameter
63 STClazy
= 0x2000, // lazy parameter
64 STCforeach
= 0x4000, // variable for foreach loop
65 STCcomdat
= 0x8000, // should go into COMDAT record
66 STCvariadic
= 0x10000, // variadic function argument
67 STCctorinit
= 0x20000, // can only be set inside constructor
68 STCtemplateparameter
= 0x40000, // template parameter
69 STCscope
= 0x80000, // template parameter
70 STCinvariant
= 0x100000,
72 STCinit
= 0x400000, // has explicit initializer
73 STCmanifest
= 0x800000, // manifest constant
74 STCnodtor
= 0x1000000, // don't run destructor
75 STCnothrow
= 0x2000000, // never throws exceptions
76 STCpure
= 0x4000000, // pure function
77 STCtls
= 0x8000000, // thread local
82 int count
; // number of matches found
83 MATCH last
; // match level of lastf
84 FuncDeclaration
*lastf
; // last matching function we found
85 FuncDeclaration
*nextf
; // current matching function
86 FuncDeclaration
*anyf
; // pick a func, any func, to use for error recovery
89 void overloadResolveX(Match
*m
, FuncDeclaration
*f
,
90 Expression
*ethis
, Expressions
*arguments
);
91 int overloadApply(FuncDeclaration
*fstart
,
92 int (*fp
)(void *, FuncDeclaration
*),
95 /**************************************************************/
97 struct Declaration
: Dsymbol
100 Type
*originalType
; // before semantic analysis
101 unsigned storage_class
;
102 enum PROT protection
;
104 Expressions
* attributes
; // GCC decl/type attributes
106 Declaration(Identifier
*id
);
107 void semantic(Scope
*sc
);
109 target_size_t
size(Loc loc
);
110 void checkModify(Loc loc
, Scope
*sc
, Type
*t
);
112 void emitComment(Scope
*sc
);
113 void toDocBuffer(OutBuffer
*buf
);
116 int isStatic() { return storage_class
& STCstatic
; }
117 virtual int isStaticConstructor();
118 virtual int isStaticDestructor();
119 virtual int isDelete();
120 virtual int isDataseg();
121 virtual int isCodeseg();
122 int isCtorinit() { return storage_class
& STCctorinit
; }
123 int isFinal() { return storage_class
& STCfinal
; }
124 int isAbstract() { return storage_class
& STCabstract
; }
125 int isConst() { return storage_class
& STCconst
; }
126 int isInvariant() { return storage_class
& STCinvariant
; }
127 int isAuto() { return storage_class
& STCauto
; }
128 int isScope() { return storage_class
& (STCscope
| STCauto
); }
129 int isSynchronized() { return storage_class
& STCsynchronized
; }
130 int isParameter() { return storage_class
& STCparameter
; }
131 int isDeprecated() { return storage_class
& STCdeprecated
; }
132 int isOverride() { return storage_class
& STCoverride
; }
134 int isIn() { return storage_class
& STCin
; }
135 int isOut() { return storage_class
& STCout
; }
136 int isRef() { return storage_class
& STCref
; }
140 Declaration
*isDeclaration() { return this; }
143 /**************************************************************/
145 struct TupleDeclaration
: Declaration
148 int isexp
; // 1: expression tuple
150 TypeTuple
*tupletype
; // !=NULL if this is a type tuple
152 TupleDeclaration(Loc loc
, Identifier
*ident
, Objects
*objects
);
153 Dsymbol
*syntaxCopy(Dsymbol
*);
158 TupleDeclaration
*isTupleDeclaration() { return this; }
161 /**************************************************************/
163 struct TypedefDeclaration
: Declaration
167 int sem
; // 0: semantic() has not been run
168 // 1: semantic() is in progress
169 // 2: semantic() has been run
170 // 3: semantic2() has been run
171 int inuse
; // used to detect typedef cycles
173 TypedefDeclaration(Loc loc
, Identifier
*ident
, Type
*basetype
, Initializer
*init
);
174 Dsymbol
*syntaxCopy(Dsymbol
*);
175 void semantic(Scope
*sc
);
176 void semantic2(Scope
*sc
);
180 void toCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
);
186 void toDocBuffer(OutBuffer
*buf
);
188 void toObjFile(int multiobj
); // compile to .obj file
190 int cvMember(unsigned char *p
);
192 TypedefDeclaration
*isTypedefDeclaration() { return this; }
195 Symbol
*toInitializer();
198 /**************************************************************/
200 struct AliasDeclaration
: Declaration
203 Dsymbol
*overnext
; // next in overload list
206 AliasDeclaration(Loc loc
, Identifier
*ident
, Type
*type
);
207 AliasDeclaration(Loc loc
, Identifier
*ident
, Dsymbol
*s
);
208 Dsymbol
*syntaxCopy(Dsymbol
*);
209 void semantic(Scope
*sc
);
210 int overloadInsert(Dsymbol
*s
);
214 void toCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
);
220 void toDocBuffer(OutBuffer
*buf
);
222 AliasDeclaration
*isAliasDeclaration() { return this; }
225 /**************************************************************/
227 struct VarDeclaration
: Declaration
230 target_size_t offset
;
231 int noauto
; // no auto semantics
232 FuncDeclarations nestedrefs
; // referenced by these lexically nested functions
234 int ctorinit
; // it has been initialized in a ctor
235 int onstack
; // 1: it has been allocated on the stack
236 // 2: on stack, run destructor anyway
237 int canassign
; // it can be assigned to
238 Dsymbol
*aliassym
; // if redone as alias to another symbol
239 Expression
*value
; // when interpreting, this is the value
240 // (NULL if value not determinable)
241 Scope
*scope
; // !=NULL means context to use
243 // We'll make sure this gets initialised, so don't worry about it.
244 // Used for the fensure argument.
247 // Reject static variables (during semantic pass)
250 VarDeclaration(Loc loc
, Type
*t
, Identifier
*id
, Initializer
*init
);
251 Dsymbol
*syntaxCopy(Dsymbol
*);
252 void semantic(Scope
*sc
);
253 void semantic2(Scope
*sc
);
255 void toCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
);
261 int isImportedSymbol();
264 int canTakeAddressOf();
266 Expression
*callAutoDtor(Scope
*sc
);
267 ExpInitializer
*getExpInitializer();
268 Expression
*getConstInitializer();
269 void checkCtorConstInit();
270 void checkNestedReference(Scope
*sc
, Loc loc
);
274 void toObjFile(int multiobj
); // compile to .obj file
275 int cvMember(unsigned char *p
);
277 // Eliminate need for dynamic_cast
278 VarDeclaration
*isVarDeclaration() { return (VarDeclaration
*)this; }
282 /**************************************************************/
284 // This is a shell around a back end symbol
286 struct SymbolDeclaration
: Declaration
289 StructDeclaration
*dsym
;
291 SymbolDeclaration(Loc loc
, Symbol
*s
, StructDeclaration
*dsym
);
295 // Eliminate need for dynamic_cast
296 SymbolDeclaration
*isSymbolDeclaration() { return (SymbolDeclaration
*)this; }
299 struct ClassInfoDeclaration
: VarDeclaration
301 ClassDeclaration
*cd
;
303 ClassInfoDeclaration(ClassDeclaration
*cd
);
304 Dsymbol
*syntaxCopy(Dsymbol
*);
305 void semantic(Scope
*sc
);
307 void emitComment(Scope
*sc
);
312 struct ModuleInfoDeclaration
: VarDeclaration
316 ModuleInfoDeclaration(Module
*mod
);
317 Dsymbol
*syntaxCopy(Dsymbol
*);
318 void semantic(Scope
*sc
);
320 void emitComment(Scope
*sc
);
325 struct TypeInfoDeclaration
: VarDeclaration
329 TypeInfoDeclaration(Type
*tinfo
, int internal
);
330 Dsymbol
*syntaxCopy(Dsymbol
*);
331 void semantic(Scope
*sc
);
333 void emitComment(Scope
*sc
);
336 void toObjFile(int multiobj
); // compile to .obj file
337 virtual void toDt(dt_t
**pdt
);
340 struct TypeInfoStructDeclaration
: TypeInfoDeclaration
342 TypeInfoStructDeclaration(Type
*tinfo
);
344 void toDt(dt_t
**pdt
);
347 struct TypeInfoClassDeclaration
: TypeInfoDeclaration
349 TypeInfoClassDeclaration(Type
*tinfo
);
351 void toDt(dt_t
**pdt
);
354 struct TypeInfoInterfaceDeclaration
: TypeInfoDeclaration
356 TypeInfoInterfaceDeclaration(Type
*tinfo
);
358 void toDt(dt_t
**pdt
);
361 struct TypeInfoTypedefDeclaration
: TypeInfoDeclaration
363 TypeInfoTypedefDeclaration(Type
*tinfo
);
365 void toDt(dt_t
**pdt
);
368 struct TypeInfoPointerDeclaration
: TypeInfoDeclaration
370 TypeInfoPointerDeclaration(Type
*tinfo
);
372 void toDt(dt_t
**pdt
);
375 struct TypeInfoMaybeDeclaration
: TypeInfoDeclaration
377 TypeInfoMaybeDeclaration(Type
*tinfo
);
379 void toDt(dt_t
**pdt
);
382 struct TypeInfoArrayDeclaration
: TypeInfoDeclaration
384 TypeInfoArrayDeclaration(Type
*tinfo
);
386 void toDt(dt_t
**pdt
);
389 struct TypeInfoStaticArrayDeclaration
: TypeInfoDeclaration
391 TypeInfoStaticArrayDeclaration(Type
*tinfo
);
393 void toDt(dt_t
**pdt
);
396 struct TypeInfoAssociativeArrayDeclaration
: TypeInfoDeclaration
398 TypeInfoAssociativeArrayDeclaration(Type
*tinfo
);
400 void toDt(dt_t
**pdt
);
403 struct TypeInfoEnumDeclaration
: TypeInfoDeclaration
405 TypeInfoEnumDeclaration(Type
*tinfo
);
407 void toDt(dt_t
**pdt
);
410 struct TypeInfoFunctionDeclaration
: TypeInfoDeclaration
412 TypeInfoFunctionDeclaration(Type
*tinfo
);
414 void toDt(dt_t
**pdt
);
417 struct TypeInfoDelegateDeclaration
: TypeInfoDeclaration
419 TypeInfoDelegateDeclaration(Type
*tinfo
);
421 void toDt(dt_t
**pdt
);
424 struct TypeInfoTupleDeclaration
: TypeInfoDeclaration
426 TypeInfoTupleDeclaration(Type
*tinfo
);
428 void toDt(dt_t
**pdt
);
432 struct TypeInfoConstDeclaration
: TypeInfoDeclaration
434 TypeInfoConstDeclaration(Type
*tinfo
);
436 void toDt(dt_t
**pdt
);
439 struct TypeInfoInvariantDeclaration
: TypeInfoDeclaration
441 TypeInfoInvariantDeclaration(Type
*tinfo
);
443 void toDt(dt_t
**pdt
);
447 /**************************************************************/
449 struct ThisDeclaration
: VarDeclaration
451 ThisDeclaration(Type
*t
);
452 Dsymbol
*syntaxCopy(Dsymbol
*);
457 ILSuninitialized
, // not computed yet
458 ILSno
, // cannot inline
459 ILSyes
, // can inline
462 /**************************************************************/
467 BUILTINunknown
= -1, // not known if this is a builtin
468 BUILTINnot
, // this is not a builtin
469 BUILTINsin
, // std.math.sin
470 BUILTINcos
, // std.math.cos
471 BUILTINtan
, // std.math.tan
472 BUILTINsqrt
, // std.math.sqrt
473 BUILTINfabs
, // std.math.fabs
476 Expression
*eval_builtin(enum BUILTIN builtin
, Expressions
*arguments
);
480 struct FuncDeclaration
: Declaration
482 Array
*fthrows
; // Array of Type's of exceptions (not used)
487 Identifier
*outId
; // identifier for out statement
488 VarDeclaration
*vresult
; // variable corresponding to outId
489 LabelDsymbol
*returnLabel
; // where the return goes
491 DsymbolTable
*localsymtab
; // used to prevent symbols in different
492 // scopes from having the same name
493 VarDeclaration
*vthis
; // 'this' parameter (member and nested)
494 VarDeclaration
*v_arguments
; // '_arguments' parameter
496 VarDeclaration
*v_arguments_var
; // '_arguments' variable
497 VarDeclaration
*v_argptr
; // '_argptr' variable
499 Dsymbols
*parameters
; // Array of VarDeclaration's for parameters
500 DsymbolTable
*labtab
; // statement label symbol table
501 Declaration
*overnext
; // next in overload list
502 Loc endloc
; // location of closing curly bracket
503 int vtblIndex
; // for member functions, index into vtbl[]
504 int naked
; // !=0 if naked
505 int inlineAsm
; // !=0 if has inline assembler
507 int inlineNest
; // !=0 if nested inline
508 int cantInterpret
; // !=0 if cannot interpret function
509 int semanticRun
; // !=0 if semantic3() had been run
510 // this function's frame ptr
511 ForeachStatement
*fes
; // if foreach body, this is the foreach
512 int introducing
; // !=0 if 'introducing' function
513 Type
*tintro
; // if !=NULL, then this is the type
514 // of the 'introducing' function
515 // this one is overriding
516 int inferRetType
; // !=0 if return type is to be inferred
517 Scope
*scope
; // !=NULL means context to use
519 // Things that should really go into Scope
520 int hasReturnExp
; // 1 if there's a return exp; statement
521 // 2 if there's a throw statement
522 // 4 if there's an assert(0)
523 // 8 if there's inline asm
525 // Support for NRVO (named return value optimization)
526 int nrvo_can
; // !=0 means we can do it
527 VarDeclaration
*nrvo_var
; // variable to replace with shidden
528 Symbol
*shidden
; // hidden pointer passed to function
531 enum BUILTIN builtin
; // set if this is a known, builtin
532 // function we can evaluate at compile
535 int tookAddressOf
; // set if someone took the address of
537 Dsymbols closureVars
; // local variables in this function
538 // which are referenced by nested
541 int nestedFrameRef
; // !=0 if nested variables referenced
544 FuncDeclaration(Loc loc
, Loc endloc
, Identifier
*id
, enum STC storage_class
, Type
*type
);
545 Dsymbol
*syntaxCopy(Dsymbol
*);
546 void semantic(Scope
*sc
);
547 void semantic2(Scope
*sc
);
548 void semantic3(Scope
*sc
);
549 void toCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
);
550 void bodyToCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
);
551 int overrides(FuncDeclaration
*fd
);
552 int findVtblIndex(Array
*vtbl
, int dim
);
553 int overloadInsert(Dsymbol
*s
);
554 FuncDeclaration
*overloadExactMatch(Type
*t
);
555 FuncDeclaration
*overloadResolve(Loc loc
, Expression
*ethis
, Expressions
*arguments
, int flags
= 0);
556 MATCH
leastAsSpecialized(FuncDeclaration
*g
);
557 LabelDsymbol
*searchLabel(Identifier
*ident
);
558 AggregateDeclaration
*isThis();
559 AggregateDeclaration
*isMember2();
560 int getLevel(Loc loc
, FuncDeclaration
*fd
); // lexical nesting level difference
561 void appendExp(Expression
*e
);
562 void appendState(Statement
*s
);
567 enum BUILTIN
isBuiltin();
569 int isImportedSymbol();
572 int isOverloadable();
573 virtual int isNested();
575 virtual int isVirtual();
576 virtual int isFinal();
577 virtual int addPreInvariant();
578 virtual int addPostInvariant();
579 Expression
*interpret(InterState
*istate
, Expressions
*arguments
);
581 int canInline(int hasthis
, int hdrscan
= 0);
582 Expression
*doInline(InlineScanState
*iss
, Expression
*ethis
, Array
*arguments
);
584 void toDocBuffer(OutBuffer
*buf
);
585 FuncDeclaration
*isUnique();
588 static FuncDeclaration
*genCfunc(Type
*treturn
, char *name
,
589 Type
*t1
= 0, Type
*t2
= 0, Type
*t3
= 0);
590 static FuncDeclaration
*genCfunc(Type
*treturn
, Identifier
*id
,
591 Type
*t1
= 0, Type
*t2
= 0, Type
*t3
= 0);
594 Symbol
*toThunkSymbol(target_ptrdiff_t offset
); // thunk version
595 void toObjFile(int multiobj
); // compile to .obj file
596 int cvMember(unsigned char *p
);
597 void buildClosure(IRState
*irs
);
599 FuncDeclaration
*isFuncDeclaration() { return this; }
602 struct FuncAliasDeclaration
: FuncDeclaration
604 FuncDeclaration
*funcalias
;
606 FuncAliasDeclaration(FuncDeclaration
*funcalias
);
608 FuncAliasDeclaration
*isFuncAliasDeclaration() { return this; }
613 struct FuncLiteralDeclaration
: FuncDeclaration
615 enum TOK tok
; // TOKfunction or TOKdelegate
617 FuncLiteralDeclaration(Loc loc
, Loc endloc
, Type
*type
, enum TOK tok
,
618 ForeachStatement
*fes
);
619 void toCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
);
620 Dsymbol
*syntaxCopy(Dsymbol
*);
623 FuncLiteralDeclaration
*isFuncLiteralDeclaration() { return this; }
627 struct CtorDeclaration
: FuncDeclaration
628 { Arguments
*arguments
;
631 CtorDeclaration(Loc loc
, Loc endloc
, Arguments
*arguments
, int varargs
);
632 Dsymbol
*syntaxCopy(Dsymbol
*);
633 void semantic(Scope
*sc
);
634 void toCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
);
638 int addPreInvariant();
639 int addPostInvariant();
640 void toDocBuffer(OutBuffer
*buf
);
642 CtorDeclaration
*isCtorDeclaration() { return this; }
645 struct PostBlitDeclaration
: FuncDeclaration
647 PostBlitDeclaration(Loc loc
, Loc endloc
);
648 PostBlitDeclaration(Loc loc
, Loc endloc
, Identifier
*id
);
649 Dsymbol
*syntaxCopy(Dsymbol
*);
650 void semantic(Scope
*sc
);
651 void toCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
);
653 int addPreInvariant();
654 int addPostInvariant();
655 int overloadInsert(Dsymbol
*s
);
656 void emitComment(Scope
*sc
);
658 PostBlitDeclaration
*isPostBlitDeclaration() { return this; }
661 struct DtorDeclaration
: FuncDeclaration
663 DtorDeclaration(Loc loc
, Loc endloc
);
664 DtorDeclaration(Loc loc
, Loc endloc
, Identifier
*id
);
665 Dsymbol
*syntaxCopy(Dsymbol
*);
666 void semantic(Scope
*sc
);
667 void toCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
);
669 int addPreInvariant();
670 int addPostInvariant();
671 int overloadInsert(Dsymbol
*s
);
672 void emitComment(Scope
*sc
);
674 DtorDeclaration
*isDtorDeclaration() { return this; }
677 struct StaticCtorDeclaration
: FuncDeclaration
679 StaticCtorDeclaration(Loc loc
, Loc endloc
);
680 Dsymbol
*syntaxCopy(Dsymbol
*);
681 void semantic(Scope
*sc
);
682 AggregateDeclaration
*isThis();
683 int isStaticConstructor();
685 int addPreInvariant();
686 int addPostInvariant();
687 void emitComment(Scope
*sc
);
688 void toCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
);
690 StaticCtorDeclaration
*isStaticCtorDeclaration() { return this; }
693 struct StaticDtorDeclaration
: FuncDeclaration
695 StaticDtorDeclaration(Loc loc
, Loc endloc
);
696 Dsymbol
*syntaxCopy(Dsymbol
*);
697 void semantic(Scope
*sc
);
698 AggregateDeclaration
*isThis();
699 int isStaticDestructor();
701 int addPreInvariant();
702 int addPostInvariant();
703 void emitComment(Scope
*sc
);
704 void toCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
);
706 StaticDtorDeclaration
*isStaticDtorDeclaration() { return this; }
709 struct InvariantDeclaration
: FuncDeclaration
711 InvariantDeclaration(Loc loc
, Loc endloc
);
712 Dsymbol
*syntaxCopy(Dsymbol
*);
713 void semantic(Scope
*sc
);
715 int addPreInvariant();
716 int addPostInvariant();
717 void emitComment(Scope
*sc
);
718 void toCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
);
720 InvariantDeclaration
*isInvariantDeclaration() { return this; }
724 struct UnitTestDeclaration
: FuncDeclaration
726 UnitTestDeclaration(Loc loc
, Loc endloc
);
727 Dsymbol
*syntaxCopy(Dsymbol
*);
728 void semantic(Scope
*sc
);
729 AggregateDeclaration
*isThis();
731 int addPreInvariant();
732 int addPostInvariant();
733 void toCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
);
735 UnitTestDeclaration
*isUnitTestDeclaration() { return this; }
738 struct NewDeclaration
: FuncDeclaration
739 { Arguments
*arguments
;
742 NewDeclaration(Loc loc
, Loc endloc
, Arguments
*arguments
, int varargs
);
743 Dsymbol
*syntaxCopy(Dsymbol
*);
744 void semantic(Scope
*sc
);
745 void toCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
);
748 int addPreInvariant();
749 int addPostInvariant();
751 NewDeclaration
*isNewDeclaration() { return this; }
755 struct DeleteDeclaration
: FuncDeclaration
756 { Arguments
*arguments
;
758 DeleteDeclaration(Loc loc
, Loc endloc
, Arguments
*arguments
);
759 Dsymbol
*syntaxCopy(Dsymbol
*);
760 void semantic(Scope
*sc
);
761 void toCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
);
765 int addPreInvariant();
766 int addPostInvariant();
768 DeleteDeclaration
*isDeleteDeclaration() { return this; }
772 #endif /* DMD_DECLARATION_H */