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 VarDeclaration(Loc loc
, Type
*t
, Identifier
*id
, Initializer
*init
);
244 Dsymbol
*syntaxCopy(Dsymbol
*);
245 void semantic(Scope
*sc
);
246 void semantic2(Scope
*sc
);
248 void toCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
);
254 int isImportedSymbol();
257 int canTakeAddressOf();
259 Expression
*callAutoDtor(Scope
*sc
);
260 ExpInitializer
*getExpInitializer();
261 Expression
*getConstInitializer();
262 void checkCtorConstInit();
263 void checkNestedReference(Scope
*sc
, Loc loc
);
267 void toObjFile(int multiobj
); // compile to .obj file
268 int cvMember(unsigned char *p
);
270 // Eliminate need for dynamic_cast
271 VarDeclaration
*isVarDeclaration() { return (VarDeclaration
*)this; }
275 /**************************************************************/
277 // This is a shell around a back end symbol
279 struct SymbolDeclaration
: Declaration
282 StructDeclaration
*dsym
;
284 SymbolDeclaration(Loc loc
, Symbol
*s
, StructDeclaration
*dsym
);
288 // Eliminate need for dynamic_cast
289 SymbolDeclaration
*isSymbolDeclaration() { return (SymbolDeclaration
*)this; }
292 struct ClassInfoDeclaration
: VarDeclaration
294 ClassDeclaration
*cd
;
296 ClassInfoDeclaration(ClassDeclaration
*cd
);
297 Dsymbol
*syntaxCopy(Dsymbol
*);
298 void semantic(Scope
*sc
);
300 void emitComment(Scope
*sc
);
305 struct ModuleInfoDeclaration
: VarDeclaration
309 ModuleInfoDeclaration(Module
*mod
);
310 Dsymbol
*syntaxCopy(Dsymbol
*);
311 void semantic(Scope
*sc
);
313 void emitComment(Scope
*sc
);
318 struct TypeInfoDeclaration
: VarDeclaration
322 TypeInfoDeclaration(Type
*tinfo
, int internal
);
323 Dsymbol
*syntaxCopy(Dsymbol
*);
324 void semantic(Scope
*sc
);
326 void emitComment(Scope
*sc
);
329 void toObjFile(int multiobj
); // compile to .obj file
330 virtual void toDt(dt_t
**pdt
);
333 struct TypeInfoStructDeclaration
: TypeInfoDeclaration
335 TypeInfoStructDeclaration(Type
*tinfo
);
337 void toDt(dt_t
**pdt
);
340 struct TypeInfoClassDeclaration
: TypeInfoDeclaration
342 TypeInfoClassDeclaration(Type
*tinfo
);
344 void toDt(dt_t
**pdt
);
347 struct TypeInfoInterfaceDeclaration
: TypeInfoDeclaration
349 TypeInfoInterfaceDeclaration(Type
*tinfo
);
351 void toDt(dt_t
**pdt
);
354 struct TypeInfoTypedefDeclaration
: TypeInfoDeclaration
356 TypeInfoTypedefDeclaration(Type
*tinfo
);
358 void toDt(dt_t
**pdt
);
361 struct TypeInfoPointerDeclaration
: TypeInfoDeclaration
363 TypeInfoPointerDeclaration(Type
*tinfo
);
365 void toDt(dt_t
**pdt
);
368 struct TypeInfoArrayDeclaration
: TypeInfoDeclaration
370 TypeInfoArrayDeclaration(Type
*tinfo
);
372 void toDt(dt_t
**pdt
);
375 struct TypeInfoStaticArrayDeclaration
: TypeInfoDeclaration
377 TypeInfoStaticArrayDeclaration(Type
*tinfo
);
379 void toDt(dt_t
**pdt
);
382 struct TypeInfoAssociativeArrayDeclaration
: TypeInfoDeclaration
384 TypeInfoAssociativeArrayDeclaration(Type
*tinfo
);
386 void toDt(dt_t
**pdt
);
389 struct TypeInfoEnumDeclaration
: TypeInfoDeclaration
391 TypeInfoEnumDeclaration(Type
*tinfo
);
393 void toDt(dt_t
**pdt
);
396 struct TypeInfoFunctionDeclaration
: TypeInfoDeclaration
398 TypeInfoFunctionDeclaration(Type
*tinfo
);
400 void toDt(dt_t
**pdt
);
403 struct TypeInfoDelegateDeclaration
: TypeInfoDeclaration
405 TypeInfoDelegateDeclaration(Type
*tinfo
);
407 void toDt(dt_t
**pdt
);
410 struct TypeInfoTupleDeclaration
: TypeInfoDeclaration
412 TypeInfoTupleDeclaration(Type
*tinfo
);
414 void toDt(dt_t
**pdt
);
418 struct TypeInfoConstDeclaration
: TypeInfoDeclaration
420 TypeInfoConstDeclaration(Type
*tinfo
);
422 void toDt(dt_t
**pdt
);
425 struct TypeInfoInvariantDeclaration
: TypeInfoDeclaration
427 TypeInfoInvariantDeclaration(Type
*tinfo
);
429 void toDt(dt_t
**pdt
);
433 /**************************************************************/
435 struct ThisDeclaration
: VarDeclaration
437 ThisDeclaration(Type
*t
);
438 Dsymbol
*syntaxCopy(Dsymbol
*);
443 ILSuninitialized
, // not computed yet
444 ILSno
, // cannot inline
445 ILSyes
, // can inline
448 /**************************************************************/
453 BUILTINunknown
= -1, // not known if this is a builtin
454 BUILTINnot
, // this is not a builtin
455 BUILTINsin
, // std.math.sin
456 BUILTINcos
, // std.math.cos
457 BUILTINtan
, // std.math.tan
458 BUILTINsqrt
, // std.math.sqrt
459 BUILTINfabs
, // std.math.fabs
462 Expression
*eval_builtin(enum BUILTIN builtin
, Expressions
*arguments
);
466 struct FuncDeclaration
: Declaration
468 Array
*fthrows
; // Array of Type's of exceptions (not used)
473 Identifier
*outId
; // identifier for out statement
474 VarDeclaration
*vresult
; // variable corresponding to outId
475 LabelDsymbol
*returnLabel
; // where the return goes
477 DsymbolTable
*localsymtab
; // used to prevent symbols in different
478 // scopes from having the same name
479 VarDeclaration
*vthis
; // 'this' parameter (member and nested)
480 VarDeclaration
*v_arguments
; // '_arguments' parameter
482 VarDeclaration
*v_arguments_var
; // '_arguments' variable
483 VarDeclaration
*v_argptr
; // '_argptr' variable
485 Dsymbols
*parameters
; // Array of VarDeclaration's for parameters
486 DsymbolTable
*labtab
; // statement label symbol table
487 Declaration
*overnext
; // next in overload list
488 Loc endloc
; // location of closing curly bracket
489 int vtblIndex
; // for member functions, index into vtbl[]
490 int naked
; // !=0 if naked
491 int inlineAsm
; // !=0 if has inline assembler
493 int inlineNest
; // !=0 if nested inline
494 int cantInterpret
; // !=0 if cannot interpret function
495 int semanticRun
; // !=0 if semantic3() had been run
496 // this function's frame ptr
497 ForeachStatement
*fes
; // if foreach body, this is the foreach
498 int introducing
; // !=0 if 'introducing' function
499 Type
*tintro
; // if !=NULL, then this is the type
500 // of the 'introducing' function
501 // this one is overriding
502 int inferRetType
; // !=0 if return type is to be inferred
503 Scope
*scope
; // !=NULL means context to use
505 // Things that should really go into Scope
506 int hasReturnExp
; // 1 if there's a return exp; statement
507 // 2 if there's a throw statement
508 // 4 if there's an assert(0)
509 // 8 if there's inline asm
511 // Support for NRVO (named return value optimization)
512 int nrvo_can
; // !=0 means we can do it
513 VarDeclaration
*nrvo_var
; // variable to replace with shidden
514 Symbol
*shidden
; // hidden pointer passed to function
517 enum BUILTIN builtin
; // set if this is a known, builtin
518 // function we can evaluate at compile
521 int tookAddressOf
; // set if someone took the address of
523 Dsymbols closureVars
; // local variables in this function
524 // which are referenced by nested
527 int nestedFrameRef
; // !=0 if nested variables referenced
530 FuncDeclaration(Loc loc
, Loc endloc
, Identifier
*id
, enum STC storage_class
, Type
*type
);
531 Dsymbol
*syntaxCopy(Dsymbol
*);
532 void semantic(Scope
*sc
);
533 void semantic2(Scope
*sc
);
534 void semantic3(Scope
*sc
);
535 void toCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
);
536 void bodyToCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
);
537 int overrides(FuncDeclaration
*fd
);
538 int findVtblIndex(Array
*vtbl
, int dim
);
539 int overloadInsert(Dsymbol
*s
);
540 FuncDeclaration
*overloadExactMatch(Type
*t
);
541 FuncDeclaration
*overloadResolve(Loc loc
, Expression
*ethis
, Expressions
*arguments
, int flags
= 0);
542 MATCH
leastAsSpecialized(FuncDeclaration
*g
);
543 LabelDsymbol
*searchLabel(Identifier
*ident
);
544 AggregateDeclaration
*isThis();
545 AggregateDeclaration
*isMember2();
546 int getLevel(Loc loc
, FuncDeclaration
*fd
); // lexical nesting level difference
547 void appendExp(Expression
*e
);
548 void appendState(Statement
*s
);
553 enum BUILTIN
isBuiltin();
555 int isImportedSymbol();
558 int isOverloadable();
559 virtual int isNested();
561 virtual int isVirtual();
562 virtual int isFinal();
563 virtual int addPreInvariant();
564 virtual int addPostInvariant();
565 Expression
*interpret(InterState
*istate
, Expressions
*arguments
);
567 int canInline(int hasthis
, int hdrscan
= 0);
568 Expression
*doInline(InlineScanState
*iss
, Expression
*ethis
, Array
*arguments
);
570 void toDocBuffer(OutBuffer
*buf
);
571 FuncDeclaration
*isUnique();
574 static FuncDeclaration
*genCfunc(Type
*treturn
, char *name
,
575 Type
*t1
= 0, Type
*t2
= 0, Type
*t3
= 0);
576 static FuncDeclaration
*genCfunc(Type
*treturn
, Identifier
*id
,
577 Type
*t1
= 0, Type
*t2
= 0, Type
*t3
= 0);
580 Symbol
*toThunkSymbol(target_ptrdiff_t offset
); // thunk version
581 void toObjFile(int multiobj
); // compile to .obj file
582 int cvMember(unsigned char *p
);
583 void buildClosure(IRState
*irs
);
585 FuncDeclaration
*isFuncDeclaration() { return this; }
588 struct FuncAliasDeclaration
: FuncDeclaration
590 FuncDeclaration
*funcalias
;
592 FuncAliasDeclaration(FuncDeclaration
*funcalias
);
594 FuncAliasDeclaration
*isFuncAliasDeclaration() { return this; }
599 struct FuncLiteralDeclaration
: FuncDeclaration
601 enum TOK tok
; // TOKfunction or TOKdelegate
603 FuncLiteralDeclaration(Loc loc
, Loc endloc
, Type
*type
, enum TOK tok
,
604 ForeachStatement
*fes
);
605 void toCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
);
606 Dsymbol
*syntaxCopy(Dsymbol
*);
609 FuncLiteralDeclaration
*isFuncLiteralDeclaration() { return this; }
613 struct CtorDeclaration
: FuncDeclaration
614 { Arguments
*arguments
;
617 CtorDeclaration(Loc loc
, Loc endloc
, Arguments
*arguments
, int varargs
);
618 Dsymbol
*syntaxCopy(Dsymbol
*);
619 void semantic(Scope
*sc
);
620 void toCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
);
624 int addPreInvariant();
625 int addPostInvariant();
626 void toDocBuffer(OutBuffer
*buf
);
628 CtorDeclaration
*isCtorDeclaration() { return this; }
631 struct PostBlitDeclaration
: FuncDeclaration
633 PostBlitDeclaration(Loc loc
, Loc endloc
);
634 PostBlitDeclaration(Loc loc
, Loc endloc
, Identifier
*id
);
635 Dsymbol
*syntaxCopy(Dsymbol
*);
636 void semantic(Scope
*sc
);
637 void toCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
);
639 int addPreInvariant();
640 int addPostInvariant();
641 int overloadInsert(Dsymbol
*s
);
642 void emitComment(Scope
*sc
);
644 PostBlitDeclaration
*isPostBlitDeclaration() { return this; }
647 struct DtorDeclaration
: FuncDeclaration
649 DtorDeclaration(Loc loc
, Loc endloc
);
650 DtorDeclaration(Loc loc
, Loc endloc
, Identifier
*id
);
651 Dsymbol
*syntaxCopy(Dsymbol
*);
652 void semantic(Scope
*sc
);
653 void toCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
);
655 int addPreInvariant();
656 int addPostInvariant();
657 int overloadInsert(Dsymbol
*s
);
658 void emitComment(Scope
*sc
);
660 DtorDeclaration
*isDtorDeclaration() { return this; }
663 struct StaticCtorDeclaration
: FuncDeclaration
665 StaticCtorDeclaration(Loc loc
, Loc endloc
);
666 Dsymbol
*syntaxCopy(Dsymbol
*);
667 void semantic(Scope
*sc
);
668 AggregateDeclaration
*isThis();
669 int isStaticConstructor();
671 int addPreInvariant();
672 int addPostInvariant();
673 void emitComment(Scope
*sc
);
674 void toCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
);
676 StaticCtorDeclaration
*isStaticCtorDeclaration() { return this; }
679 struct StaticDtorDeclaration
: FuncDeclaration
681 StaticDtorDeclaration(Loc loc
, Loc endloc
);
682 Dsymbol
*syntaxCopy(Dsymbol
*);
683 void semantic(Scope
*sc
);
684 AggregateDeclaration
*isThis();
685 int isStaticDestructor();
687 int addPreInvariant();
688 int addPostInvariant();
689 void emitComment(Scope
*sc
);
690 void toCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
);
692 StaticDtorDeclaration
*isStaticDtorDeclaration() { return this; }
695 struct InvariantDeclaration
: FuncDeclaration
697 InvariantDeclaration(Loc loc
, Loc endloc
);
698 Dsymbol
*syntaxCopy(Dsymbol
*);
699 void semantic(Scope
*sc
);
701 int addPreInvariant();
702 int addPostInvariant();
703 void emitComment(Scope
*sc
);
704 void toCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
);
706 InvariantDeclaration
*isInvariantDeclaration() { return this; }
710 struct UnitTestDeclaration
: FuncDeclaration
712 UnitTestDeclaration(Loc loc
, Loc endloc
);
713 Dsymbol
*syntaxCopy(Dsymbol
*);
714 void semantic(Scope
*sc
);
715 AggregateDeclaration
*isThis();
717 int addPreInvariant();
718 int addPostInvariant();
719 void toCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
);
721 UnitTestDeclaration
*isUnitTestDeclaration() { return this; }
724 struct NewDeclaration
: FuncDeclaration
725 { Arguments
*arguments
;
728 NewDeclaration(Loc loc
, Loc endloc
, Arguments
*arguments
, int varargs
);
729 Dsymbol
*syntaxCopy(Dsymbol
*);
730 void semantic(Scope
*sc
);
731 void toCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
);
734 int addPreInvariant();
735 int addPostInvariant();
737 NewDeclaration
*isNewDeclaration() { return this; }
741 struct DeleteDeclaration
: FuncDeclaration
742 { Arguments
*arguments
;
744 DeleteDeclaration(Loc loc
, Loc endloc
, Arguments
*arguments
);
745 Dsymbol
*syntaxCopy(Dsymbol
*);
746 void semantic(Scope
*sc
);
747 void toCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
);
751 int addPreInvariant();
752 int addPostInvariant();
754 DeleteDeclaration
*isDeleteDeclaration() { return this; }
758 #endif /* DMD_DECLARATION_H */