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
78 STCinject
= 0x10000000, // injected dependency
83 int count
; // number of matches found
84 MATCH last
; // match level of lastf
85 FuncDeclaration
*lastf
; // last matching function we found
86 FuncDeclaration
*nextf
; // current matching function
87 FuncDeclaration
*anyf
; // pick a func, any func, to use for error recovery
90 void overloadResolveX(Match
*m
, FuncDeclaration
*f
,
91 Expression
*ethis
, Expressions
*arguments
);
92 int overloadApply(FuncDeclaration
*fstart
,
93 int (*fp
)(void *, FuncDeclaration
*),
96 /**************************************************************/
98 struct Declaration
: Dsymbol
101 Type
*originalType
; // before semantic analysis
102 unsigned storage_class
;
103 enum PROT protection
;
105 Expressions
* attributes
; // GCC decl/type attributes
107 Declaration(Identifier
*id
);
108 void semantic(Scope
*sc
);
110 target_size_t
size(Loc loc
);
111 void checkModify(Loc loc
, Scope
*sc
, Type
*t
);
113 void emitComment(Scope
*sc
);
114 void toDocBuffer(OutBuffer
*buf
);
117 int isStatic() { return storage_class
& STCstatic
; }
118 virtual int isStaticConstructor();
119 virtual int isStaticDestructor();
120 virtual int isDelete();
121 virtual int isDataseg();
122 virtual int isCodeseg();
123 int isCtorinit() { return storage_class
& STCctorinit
; }
124 int isFinal() { return storage_class
& STCfinal
; }
125 int isAbstract() { return storage_class
& STCabstract
; }
126 int isConst() { return storage_class
& STCconst
; }
127 int isInvariant() { return storage_class
& STCinvariant
; }
128 int isAuto() { return storage_class
& STCauto
; }
129 int isScope() { return storage_class
& (STCscope
| STCauto
); }
130 int isSynchronized() { return storage_class
& STCsynchronized
; }
131 int isParameter() { return storage_class
& STCparameter
; }
132 int isDeprecated() { return storage_class
& STCdeprecated
; }
133 int isOverride() { return storage_class
& STCoverride
; }
135 int isIn() { return storage_class
& STCin
; }
136 int isOut() { return storage_class
& STCout
; }
137 int isRef() { return storage_class
& STCref
; }
141 Declaration
*isDeclaration() { return this; }
144 /**************************************************************/
146 struct TupleDeclaration
: Declaration
149 int isexp
; // 1: expression tuple
151 TypeTuple
*tupletype
; // !=NULL if this is a type tuple
153 TupleDeclaration(Loc loc
, Identifier
*ident
, Objects
*objects
);
154 Dsymbol
*syntaxCopy(Dsymbol
*);
159 TupleDeclaration
*isTupleDeclaration() { return this; }
162 /**************************************************************/
164 struct TypedefDeclaration
: Declaration
168 int sem
; // 0: semantic() has not been run
169 // 1: semantic() is in progress
170 // 2: semantic() has been run
171 // 3: semantic2() has been run
172 int inuse
; // used to detect typedef cycles
174 TypedefDeclaration(Loc loc
, Identifier
*ident
, Type
*basetype
, Initializer
*init
);
175 Dsymbol
*syntaxCopy(Dsymbol
*);
176 void semantic(Scope
*sc
);
177 void semantic2(Scope
*sc
);
181 void toCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
);
187 void toDocBuffer(OutBuffer
*buf
);
189 void toObjFile(int multiobj
); // compile to .obj file
191 int cvMember(unsigned char *p
);
193 TypedefDeclaration
*isTypedefDeclaration() { return this; }
196 Symbol
*toInitializer();
199 /**************************************************************/
201 struct AliasDeclaration
: Declaration
204 Dsymbol
*overnext
; // next in overload list
207 AliasDeclaration(Loc loc
, Identifier
*ident
, Type
*type
);
208 AliasDeclaration(Loc loc
, Identifier
*ident
, Dsymbol
*s
);
209 Dsymbol
*syntaxCopy(Dsymbol
*);
210 void semantic(Scope
*sc
);
211 int overloadInsert(Dsymbol
*s
);
215 void toCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
);
221 void toDocBuffer(OutBuffer
*buf
);
223 AliasDeclaration
*isAliasDeclaration() { return this; }
226 /**************************************************************/
228 struct VarDeclaration
: Declaration
231 target_size_t offset
;
232 int noauto
; // no auto semantics
233 FuncDeclarations nestedrefs
; // referenced by these lexically nested functions
235 int ctorinit
; // it has been initialized in a ctor
236 int onstack
; // 1: it has been allocated on the stack
237 // 2: on stack, run destructor anyway
238 int canassign
; // it can be assigned to
239 Dsymbol
*aliassym
; // if redone as alias to another symbol
240 Expression
*value
; // when interpreting, this is the value
241 // (NULL if value not determinable)
242 Scope
*scope
; // !=NULL means context to use
244 // If this turns out to be a pointer or class, make sure it's initialised
245 // Used to ensure non-null variables get initialised in Delight
246 bool requirePointerInit
;
248 // Reject static variables (during semantic pass)
251 VarDeclaration(Loc loc
, Type
*t
, Identifier
*id
, Initializer
*init
);
252 Dsymbol
*syntaxCopy(Dsymbol
*);
253 void semantic(Scope
*sc
);
254 void semantic2(Scope
*sc
);
256 void toCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
);
262 int isImportedSymbol();
265 int canTakeAddressOf();
267 Expression
*callAutoDtor(Scope
*sc
);
268 ExpInitializer
*getExpInitializer();
269 Expression
*getConstInitializer();
270 void checkCtorConstInit();
271 void checkNestedReference(Scope
*sc
, Loc loc
);
275 void toObjFile(int multiobj
); // compile to .obj file
276 int cvMember(unsigned char *p
);
278 // Eliminate need for dynamic_cast
279 VarDeclaration
*isVarDeclaration() { return (VarDeclaration
*)this; }
283 /**************************************************************/
285 // This is a shell around a back end symbol
287 struct SymbolDeclaration
: Declaration
290 StructDeclaration
*dsym
;
292 SymbolDeclaration(Loc loc
, Symbol
*s
, StructDeclaration
*dsym
);
296 // Eliminate need for dynamic_cast
297 SymbolDeclaration
*isSymbolDeclaration() { return (SymbolDeclaration
*)this; }
300 struct ClassInfoDeclaration
: VarDeclaration
302 ClassDeclaration
*cd
;
304 ClassInfoDeclaration(ClassDeclaration
*cd
);
305 Dsymbol
*syntaxCopy(Dsymbol
*);
306 void semantic(Scope
*sc
);
308 void emitComment(Scope
*sc
);
313 struct ModuleInfoDeclaration
: VarDeclaration
317 ModuleInfoDeclaration(Module
*mod
);
318 Dsymbol
*syntaxCopy(Dsymbol
*);
319 void semantic(Scope
*sc
);
321 void emitComment(Scope
*sc
);
326 struct TypeInfoDeclaration
: VarDeclaration
330 TypeInfoDeclaration(Type
*tinfo
, int internal
);
331 Dsymbol
*syntaxCopy(Dsymbol
*);
332 void semantic(Scope
*sc
);
334 void emitComment(Scope
*sc
);
337 void toObjFile(int multiobj
); // compile to .obj file
338 virtual void toDt(dt_t
**pdt
);
341 struct TypeInfoStructDeclaration
: TypeInfoDeclaration
343 TypeInfoStructDeclaration(Type
*tinfo
);
345 void toDt(dt_t
**pdt
);
348 struct TypeInfoClassDeclaration
: TypeInfoDeclaration
350 TypeInfoClassDeclaration(Type
*tinfo
);
352 void toDt(dt_t
**pdt
);
355 struct TypeInfoInterfaceDeclaration
: TypeInfoDeclaration
357 TypeInfoInterfaceDeclaration(Type
*tinfo
);
359 void toDt(dt_t
**pdt
);
362 struct TypeInfoTypedefDeclaration
: TypeInfoDeclaration
364 TypeInfoTypedefDeclaration(Type
*tinfo
);
366 void toDt(dt_t
**pdt
);
369 struct TypeInfoPointerDeclaration
: TypeInfoDeclaration
371 TypeInfoPointerDeclaration(Type
*tinfo
);
373 void toDt(dt_t
**pdt
);
376 struct TypeInfoMaybeDeclaration
: TypeInfoDeclaration
378 TypeInfoMaybeDeclaration(Type
*tinfo
);
380 void toDt(dt_t
**pdt
);
383 struct TypeInfoArrayDeclaration
: TypeInfoDeclaration
385 TypeInfoArrayDeclaration(Type
*tinfo
);
387 void toDt(dt_t
**pdt
);
390 struct TypeInfoStaticArrayDeclaration
: TypeInfoDeclaration
392 TypeInfoStaticArrayDeclaration(Type
*tinfo
);
394 void toDt(dt_t
**pdt
);
397 struct TypeInfoAssociativeArrayDeclaration
: TypeInfoDeclaration
399 TypeInfoAssociativeArrayDeclaration(Type
*tinfo
);
401 void toDt(dt_t
**pdt
);
404 struct TypeInfoEnumDeclaration
: TypeInfoDeclaration
406 TypeInfoEnumDeclaration(Type
*tinfo
);
408 void toDt(dt_t
**pdt
);
411 struct TypeInfoFunctionDeclaration
: TypeInfoDeclaration
413 TypeInfoFunctionDeclaration(Type
*tinfo
);
415 void toDt(dt_t
**pdt
);
418 struct TypeInfoDelegateDeclaration
: TypeInfoDeclaration
420 TypeInfoDelegateDeclaration(Type
*tinfo
);
422 void toDt(dt_t
**pdt
);
425 struct TypeInfoTupleDeclaration
: TypeInfoDeclaration
427 TypeInfoTupleDeclaration(Type
*tinfo
);
429 void toDt(dt_t
**pdt
);
433 struct TypeInfoConstDeclaration
: TypeInfoDeclaration
435 TypeInfoConstDeclaration(Type
*tinfo
);
437 void toDt(dt_t
**pdt
);
440 struct TypeInfoInvariantDeclaration
: TypeInfoDeclaration
442 TypeInfoInvariantDeclaration(Type
*tinfo
);
444 void toDt(dt_t
**pdt
);
448 /**************************************************************/
450 struct ThisDeclaration
: VarDeclaration
452 ThisDeclaration(Type
*t
);
453 Dsymbol
*syntaxCopy(Dsymbol
*);
458 ILSuninitialized
, // not computed yet
459 ILSno
, // cannot inline
460 ILSyes
, // can inline
463 /**************************************************************/
468 BUILTINunknown
= -1, // not known if this is a builtin
469 BUILTINnot
, // this is not a builtin
470 BUILTINsin
, // std.math.sin
471 BUILTINcos
, // std.math.cos
472 BUILTINtan
, // std.math.tan
473 BUILTINsqrt
, // std.math.sqrt
474 BUILTINfabs
, // std.math.fabs
477 Expression
*eval_builtin(enum BUILTIN builtin
, Expressions
*arguments
);
481 struct FuncDeclaration
: Declaration
483 Array
*fthrows
; // Array of Type's of exceptions (not used)
488 Identifier
*outId
; // identifier for out statement
489 VarDeclaration
*vresult
; // variable corresponding to outId
490 LabelDsymbol
*returnLabel
; // where the return goes
492 DsymbolTable
*localsymtab
; // used to prevent symbols in different
493 // scopes from having the same name
494 VarDeclaration
*vthis
; // 'this' parameter (member and nested)
495 VarDeclaration
*v_arguments
; // '_arguments' parameter
497 VarDeclaration
*v_arguments_var
; // '_arguments' variable
498 VarDeclaration
*v_argptr
; // '_argptr' variable
500 Dsymbols
*parameters
; // Array of VarDeclaration's for parameters
501 DsymbolTable
*labtab
; // statement label symbol table
502 Declaration
*overnext
; // next in overload list
503 Loc endloc
; // location of closing curly bracket
504 int vtblIndex
; // for member functions, index into vtbl[]
505 int naked
; // !=0 if naked
506 int inlineAsm
; // !=0 if has inline assembler
508 int inlineNest
; // !=0 if nested inline
509 int cantInterpret
; // !=0 if cannot interpret function
510 int semanticRun
; // !=0 if semantic3() had been run
511 // this function's frame ptr
512 ForeachStatement
*fes
; // if foreach body, this is the foreach
513 int introducing
; // !=0 if 'introducing' function
514 Type
*tintro
; // if !=NULL, then this is the type
515 // of the 'introducing' function
516 // this one is overriding
517 int inferRetType
; // !=0 if return type is to be inferred
518 Scope
*scope
; // !=NULL means context to use
520 // Things that should really go into Scope
521 int hasReturnExp
; // 1 if there's a return exp; statement
522 // 2 if there's a throw statement
523 // 4 if there's an assert(0)
524 // 8 if there's inline asm
526 // Support for NRVO (named return value optimization)
527 int nrvo_can
; // !=0 means we can do it
528 VarDeclaration
*nrvo_var
; // variable to replace with shidden
529 Symbol
*shidden
; // hidden pointer passed to function
532 enum BUILTIN builtin
; // set if this is a known, builtin
533 // function we can evaluate at compile
536 int tookAddressOf
; // set if someone took the address of
538 Dsymbols closureVars
; // local variables in this function
539 // which are referenced by nested
542 int nestedFrameRef
; // !=0 if nested variables referenced
545 FuncDeclaration(Loc loc
, Loc endloc
, Identifier
*id
, enum STC storage_class
, Type
*type
);
546 Dsymbol
*syntaxCopy(Dsymbol
*);
547 void semantic(Scope
*sc
);
548 void semantic2(Scope
*sc
);
549 void semantic3(Scope
*sc
);
550 void toCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
);
551 void bodyToCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
);
552 int overrides(FuncDeclaration
*fd
);
553 int findVtblIndex(Array
*vtbl
, int dim
);
554 int overloadInsert(Dsymbol
*s
);
555 FuncDeclaration
*overloadExactMatch(Type
*t
);
556 FuncDeclaration
*overloadResolve(Loc loc
, Expression
*ethis
, Expressions
*arguments
, int flags
= 0);
557 MATCH
leastAsSpecialized(FuncDeclaration
*g
);
558 LabelDsymbol
*searchLabel(Identifier
*ident
);
559 AggregateDeclaration
*isThis();
560 AggregateDeclaration
*isMember2();
561 int getLevel(Loc loc
, FuncDeclaration
*fd
); // lexical nesting level difference
562 void appendExp(Expression
*e
);
563 void appendState(Statement
*s
);
568 enum BUILTIN
isBuiltin();
570 int isImportedSymbol();
573 int isOverloadable();
574 virtual int isNested();
576 virtual int isVirtual();
577 virtual int isFinal();
578 virtual int addPreInvariant();
579 virtual int addPostInvariant();
580 Expression
*interpret(InterState
*istate
, Expressions
*arguments
);
582 int canInline(int hasthis
, int hdrscan
= 0);
583 Expression
*doInline(InlineScanState
*iss
, Expression
*ethis
, Array
*arguments
);
585 void toDocBuffer(OutBuffer
*buf
);
586 FuncDeclaration
*isUnique();
589 static FuncDeclaration
*genCfunc(Type
*treturn
, char *name
,
590 Type
*t1
= 0, Type
*t2
= 0, Type
*t3
= 0);
591 static FuncDeclaration
*genCfunc(Type
*treturn
, Identifier
*id
,
592 Type
*t1
= 0, Type
*t2
= 0, Type
*t3
= 0);
595 Symbol
*toThunkSymbol(target_ptrdiff_t offset
); // thunk version
596 void toObjFile(int multiobj
); // compile to .obj file
597 int cvMember(unsigned char *p
);
598 void buildClosure(IRState
*irs
);
600 FuncDeclaration
*isFuncDeclaration() { return this; }
603 struct FuncAliasDeclaration
: FuncDeclaration
605 FuncDeclaration
*funcalias
;
607 FuncAliasDeclaration(FuncDeclaration
*funcalias
);
609 FuncAliasDeclaration
*isFuncAliasDeclaration() { return this; }
614 struct FuncLiteralDeclaration
: FuncDeclaration
616 enum TOK tok
; // TOKfunction or TOKdelegate
618 FuncLiteralDeclaration(Loc loc
, Loc endloc
, Type
*type
, enum TOK tok
,
619 ForeachStatement
*fes
);
620 void toCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
);
621 Dsymbol
*syntaxCopy(Dsymbol
*);
624 FuncLiteralDeclaration
*isFuncLiteralDeclaration() { return this; }
628 struct CtorDeclaration
: FuncDeclaration
629 { Arguments
*arguments
;
632 CtorDeclaration(Loc loc
, Loc endloc
, Arguments
*arguments
, int varargs
);
633 Dsymbol
*syntaxCopy(Dsymbol
*);
634 void semantic(Scope
*sc
);
635 void toCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
);
639 int addPreInvariant();
640 int addPostInvariant();
641 void toDocBuffer(OutBuffer
*buf
);
643 CtorDeclaration
*isCtorDeclaration() { return this; }
646 struct PostBlitDeclaration
: FuncDeclaration
648 PostBlitDeclaration(Loc loc
, Loc endloc
);
649 PostBlitDeclaration(Loc loc
, Loc endloc
, Identifier
*id
);
650 Dsymbol
*syntaxCopy(Dsymbol
*);
651 void semantic(Scope
*sc
);
652 void toCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
);
654 int addPreInvariant();
655 int addPostInvariant();
656 int overloadInsert(Dsymbol
*s
);
657 void emitComment(Scope
*sc
);
659 PostBlitDeclaration
*isPostBlitDeclaration() { return this; }
662 struct DtorDeclaration
: FuncDeclaration
664 DtorDeclaration(Loc loc
, Loc endloc
);
665 DtorDeclaration(Loc loc
, Loc endloc
, Identifier
*id
);
666 Dsymbol
*syntaxCopy(Dsymbol
*);
667 void semantic(Scope
*sc
);
668 void toCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
);
670 int addPreInvariant();
671 int addPostInvariant();
672 int overloadInsert(Dsymbol
*s
);
673 void emitComment(Scope
*sc
);
675 DtorDeclaration
*isDtorDeclaration() { return this; }
678 struct StaticCtorDeclaration
: FuncDeclaration
680 StaticCtorDeclaration(Loc loc
, Loc endloc
);
681 Dsymbol
*syntaxCopy(Dsymbol
*);
682 void semantic(Scope
*sc
);
683 AggregateDeclaration
*isThis();
684 int isStaticConstructor();
686 int addPreInvariant();
687 int addPostInvariant();
688 void emitComment(Scope
*sc
);
689 void toCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
);
691 StaticCtorDeclaration
*isStaticCtorDeclaration() { return this; }
694 struct StaticDtorDeclaration
: FuncDeclaration
696 StaticDtorDeclaration(Loc loc
, Loc endloc
);
697 Dsymbol
*syntaxCopy(Dsymbol
*);
698 void semantic(Scope
*sc
);
699 AggregateDeclaration
*isThis();
700 int isStaticDestructor();
702 int addPreInvariant();
703 int addPostInvariant();
704 void emitComment(Scope
*sc
);
705 void toCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
);
707 StaticDtorDeclaration
*isStaticDtorDeclaration() { return this; }
710 struct InvariantDeclaration
: FuncDeclaration
712 InvariantDeclaration(Loc loc
, Loc endloc
);
713 Dsymbol
*syntaxCopy(Dsymbol
*);
714 void semantic(Scope
*sc
);
716 int addPreInvariant();
717 int addPostInvariant();
718 void emitComment(Scope
*sc
);
719 void toCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
);
721 InvariantDeclaration
*isInvariantDeclaration() { return this; }
725 struct UnitTestDeclaration
: FuncDeclaration
727 UnitTestDeclaration(Loc loc
, Loc endloc
);
728 Dsymbol
*syntaxCopy(Dsymbol
*);
729 void semantic(Scope
*sc
);
730 AggregateDeclaration
*isThis();
732 int addPreInvariant();
733 int addPostInvariant();
734 void toCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
);
736 UnitTestDeclaration
*isUnitTestDeclaration() { return this; }
739 struct NewDeclaration
: FuncDeclaration
740 { Arguments
*arguments
;
743 NewDeclaration(Loc loc
, Loc endloc
, Arguments
*arguments
, int varargs
);
744 Dsymbol
*syntaxCopy(Dsymbol
*);
745 void semantic(Scope
*sc
);
746 void toCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
);
749 int addPreInvariant();
750 int addPostInvariant();
752 NewDeclaration
*isNewDeclaration() { return this; }
756 struct DeleteDeclaration
: FuncDeclaration
757 { Arguments
*arguments
;
759 DeleteDeclaration(Loc loc
, Loc endloc
, Arguments
*arguments
);
760 Dsymbol
*syntaxCopy(Dsymbol
*);
761 void semantic(Scope
*sc
);
762 void toCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
);
766 int addPreInvariant();
767 int addPostInvariant();
769 DeleteDeclaration
*isDeleteDeclaration() { return this; }
773 #endif /* DMD_DECLARATION_H */