root/trunk/rebuild/declaration.h

Revision 915, 19.9 kB (checked in by Gregor, 3 months ago)

MERGE: dmdfe-2.0 r914 (DMD 2.019)

Line 
1 // Compiler implementation of the D programming language
2 // Copyright (c) 1999-2008 by Digital Mars
3 // All Rights Reserved
4 // written by Walter Bright
5 // http://www.digitalmars.com
6 // License for redistribution is by either the Artistic License
7 // in artistic.txt, or the GNU General Public License in gnu.txt.
8 // See the included readme.txt for details.
9
10 #ifndef DMD_DECLARATION_H
11 #define DMD_DECLARATION_H
12
13 #ifdef __DMC__
14 #pragma once
15 #endif /* __DMC__ */
16
17 #include "dsymbol.h"
18 #include "lexer.h"
19 #include "mtype.h"
20
21 struct Expression;
22 struct Statement;
23 struct LabelDsymbol;
24 struct Initializer;
25 struct Module;
26 struct InlineScanState;
27 struct ForeachStatement;
28 struct FuncDeclaration;
29 struct ExpInitializer;
30 struct StructDeclaration;
31 struct TupleType;
32 struct InterState;
33 struct IRState;
34
35 enum PROT;
36 enum LINK;
37 enum TOK;
38 enum MATCH;
39
40 enum STC
41 {
42     STCundefined    = 0,
43     STCstatic       = 1,
44     STCextern       = 2,
45     STCconst        = 4,
46     STCfinal        = 8,
47     STCabstract     = 0x10,
48     STCparameter    = 0x20,
49     STCfield        = 0x40,
50     STCoverride     = 0x80,
51     STCauto         = 0x100,
52     STCsynchronized = 0x200,
53     STCdeprecated   = 0x400,
54     STCin           = 0x800,        // in parameter
55     STCout          = 0x1000,       // out parameter
56     STClazy     = 0x2000,       // lazy parameter
57     STCforeach      = 0x4000,       // variable for foreach loop
58     STCcomdat       = 0x8000,       // should go into COMDAT record
59     STCvariadic     = 0x10000,      // variadic function argument
60     STCctorinit     = 0x20000,      // can only be set inside constructor
61     STCtemplateparameter = 0x40000, // template parameter
62     STCscope        = 0x80000,      // template parameter
63     STCinvariant    = 0x100000,
64     STCref      = 0x200000,
65     STCinit     = 0x400000,     // has explicit initializer
66     STCmanifest     = 0x800000,     // manifest constant
67     STCnodtor       = 0x1000000,    // don't run destructor
68     STCnothrow      = 0x2000000,    // never throws exceptions
69     STCpure     = 0x4000000,    // pure function
70     STCtls      = 0x8000000,    // thread local
71     STCalias        = 0x10000000,   // alias parameter
72 };
73
74 struct Match
75 {
76     int count;          // number of matches found
77     MATCH last;         // match level of lastf
78     FuncDeclaration *lastf; // last matching function we found
79     FuncDeclaration *nextf; // current matching function
80     FuncDeclaration *anyf;  // pick a func, any func, to use for error recovery
81 };
82
83 void overloadResolveX(Match *m, FuncDeclaration *f,
84     Expression *ethis, Expressions *arguments);
85 int overloadApply(FuncDeclaration *fstart,
86     int (*fp)(void *, FuncDeclaration *),
87     void *param);
88
89 /**************************************************************/
90
91 struct Declaration : Dsymbol
92 {
93     Type *type;
94     Type *originalType;     // before semantic analysis
95     unsigned storage_class;
96     enum PROT protection;
97     enum LINK linkage;
98
99     Declaration(Identifier *id);
100     void semantic(Scope *sc);
101     const char *kind();
102     unsigned size(Loc loc);
103     void checkModify(Loc loc, Scope *sc, Type *t);
104
105     void emitComment(Scope *sc);
106     void toDocBuffer(OutBuffer *buf);
107
108     char *mangle();
109     int isStatic() { return storage_class & STCstatic; }
110     virtual int isStaticConstructor();
111     virtual int isStaticDestructor();
112     virtual int isDelete();
113     virtual int isDataseg();
114     virtual int isCodeseg();
115     int isCtorinit()     { return storage_class & STCctorinit; }
116     int isFinal()        { return storage_class & STCfinal; }
117     int isAbstract()     { return storage_class & STCabstract; }
118     int isConst()        { return storage_class & STCconst; }
119     int isInvariant()    { return storage_class & STCinvariant; }
120     int isAuto()         { return storage_class & STCauto; }
121     int isScope()        { return storage_class & (STCscope | STCauto); }
122     int isSynchronized() { return storage_class & STCsynchronized; }
123     int isParameter()    { return storage_class & STCparameter; }
124     int isDeprecated()   { return storage_class & STCdeprecated; }
125     int isOverride()     { return storage_class & STCoverride; }
126
127     int isIn()    { return storage_class & STCin; }
128     int isOut()   { return storage_class & STCout; }
129     int isRef()   { return storage_class & STCref; }
130
131     enum PROT prot();
132
133     Declaration *isDeclaration() { return this; }
134 };
135
136 /**************************************************************/
137
138 struct TupleDeclaration : Declaration
139 {
140     Objects *objects;
141     int isexp;          // 1: expression tuple
142
143     TypeTuple *tupletype;   // !=NULL if this is a type tuple
144
145     TupleDeclaration(Loc loc, Identifier *ident, Objects *objects);
146     Dsymbol *syntaxCopy(Dsymbol *);
147     const char *kind();
148     Type *getType();
149     int needThis();
150
151     TupleDeclaration *isTupleDeclaration() { return this; }
152 };
153
154 /**************************************************************/
155
156 struct TypedefDeclaration : Declaration
157 {
158     Type *basetype;
159     Initializer *init;
160     int sem;            // 0: semantic() has not been run
161                 // 1: semantic() is in progress
162                 // 2: semantic() has been run
163                 // 3: semantic2() has been run
164     int inuse;          // used to detect typedef cycles
165
166     TypedefDeclaration(Loc loc, Identifier *ident, Type *basetype, Initializer *init);
167     Dsymbol *syntaxCopy(Dsymbol *);
168     void semantic(Scope *sc);
169     void semantic2(Scope *sc);
170     char *mangle();
171     const char *kind();
172     Type *getType();
173     void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
174 #ifdef _DH
175     Type *htype;
176     Type *hbasetype;
177 #endif
178
179     void toDocBuffer(OutBuffer *buf);
180
181     void toDebug();
182
183     TypedefDeclaration *isTypedefDeclaration() { return this; }
184
185     Symbol *sinit;
186 };
187
188 /**************************************************************/
189
190 struct AliasDeclaration : Declaration
191 {
192     Dsymbol *aliassym;
193     Dsymbol *overnext;      // next in overload list
194     int inSemantic;
195
196     AliasDeclaration(Loc loc, Identifier *ident, Type *type);
197     AliasDeclaration(Loc loc, Identifier *ident, Dsymbol *s);
198     Dsymbol *syntaxCopy(Dsymbol *);
199     void semantic(Scope *sc);
200     int overloadInsert(Dsymbol *s);
201     const char *kind();
202     Type *getType();
203     Dsymbol *toAlias();
204     void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
205 #ifdef _DH
206     Type *htype;
207     Dsymbol *haliassym;
208 #endif
209
210     void toDocBuffer(OutBuffer *buf);
211
212     AliasDeclaration *isAliasDeclaration() { return this; }
213 };
214
215 /**************************************************************/
216
217 struct VarDeclaration : Declaration
218 {
219     Initializer *init;
220     unsigned offset;
221     int noauto;         // no auto semantics
222     FuncDeclarations nestedrefs; // referenced by these lexically nested functions
223     int inuse;
224     int ctorinit;       // it has been initialized in a ctor
225     int onstack;        // 1: it has been allocated on the stack
226                 // 2: on stack, run destructor anyway
227     int canassign;      // it can be assigned to
228     Dsymbol *aliassym;      // if redone as alias to another symbol
229     Expression *value;      // when interpreting, this is the value
230                 // (NULL if value not determinable)
231     Scope *scope;       // !=NULL means context to use
232
233     VarDeclaration(Loc loc, Type *t, Identifier *id, Initializer *init);
234     Dsymbol *syntaxCopy(Dsymbol *);
235     void semantic(Scope *sc);
236     void semantic2(Scope *sc);
237     const char *kind();
238     void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
239 #ifdef _DH
240     Type *htype;
241     Initializer *hinit;
242 #endif
243     int needThis();
244     int isImportedSymbol();
245     int isDataseg();
246     int hasPointers();
247     int canTakeAddressOf();
248     int needsAutoDtor();
249     Expression *callAutoDtor(Scope *sc);
250     ExpInitializer *getExpInitializer();
251     Expression *getConstInitializer();
252     void checkCtorConstInit();
253     void checkNestedReference(Scope *sc, Loc loc);
254     Dsymbol *toAlias();
255
256     // Eliminate need for dynamic_cast
257     VarDeclaration *isVarDeclaration() { return (VarDeclaration *)this; }
258 };
259
260 /**************************************************************/
261
262 // This is a shell around a back end symbol
263
264 struct SymbolDeclaration : Declaration
265 {
266     Symbol *sym;
267     StructDeclaration *dsym;
268
269     SymbolDeclaration(Loc loc, Symbol *s, StructDeclaration *dsym);
270
271     // Eliminate need for dynamic_cast
272     SymbolDeclaration *isSymbolDeclaration() { return (SymbolDeclaration *)this; }
273 };
274
275 struct ClassInfoDeclaration : VarDeclaration
276 {
277     ClassDeclaration *cd;
278
279     ClassInfoDeclaration(ClassDeclaration *cd);
280     Dsymbol *syntaxCopy(Dsymbol *);
281     void semantic(Scope *sc);
282
283     void emitComment(Scope *sc);
284 };
285
286 struct ModuleInfoDeclaration : VarDeclaration
287 {
288     Module *mod;
289
290     ModuleInfoDeclaration(Module *mod);
291     Dsymbol *syntaxCopy(Dsymbol *);
292     void semantic(Scope *sc);
293
294     void emitComment(Scope *sc);
295 };
296
297 struct TypeInfoDeclaration : VarDeclaration
298 {
299     Type *tinfo;
300
301     TypeInfoDeclaration(Type *tinfo, int internal);
302     Dsymbol *syntaxCopy(Dsymbol *);
303     void semantic(Scope *sc);
304
305     void emitComment(Scope *sc);
306 };
307
308 struct TypeInfoStructDeclaration : TypeInfoDeclaration
309 {
310     TypeInfoStructDeclaration(Type *tinfo);
311 };
312
313 struct TypeInfoClassDeclaration : TypeInfoDeclaration
314 {
315     TypeInfoClassDeclaration(Type *tinfo);
316 };
317
318 struct TypeInfoInterfaceDeclaration : TypeInfoDeclaration
319 {
320     TypeInfoInterfaceDeclaration(Type *tinfo);
321 };
322
323 struct TypeInfoTypedefDeclaration : TypeInfoDeclaration
324 {
325     TypeInfoTypedefDeclaration(Type *tinfo);
326 };
327
328 struct TypeInfoPointerDeclaration : TypeInfoDeclaration
329 {
330     TypeInfoPointerDeclaration(Type *tinfo);
331 };
332
333 struct TypeInfoArrayDeclaration : TypeInfoDeclaration
334 {
335     TypeInfoArrayDeclaration(Type *tinfo);
336 };
337
338 struct TypeInfoStaticArrayDeclaration : TypeInfoDeclaration
339 {
340     TypeInfoStaticArrayDeclaration(Type *tinfo);
341 };
342
343 struct TypeInfoAssociativeArrayDeclaration : TypeInfoDeclaration
344 {
345     TypeInfoAssociativeArrayDeclaration(Type *tinfo);
346 };
347
348 struct TypeInfoEnumDeclaration : TypeInfoDeclaration
349 {
350     TypeInfoEnumDeclaration(Type *tinfo);
351 };
352
353 struct TypeInfoFunctionDeclaration : TypeInfoDeclaration
354 {
355     TypeInfoFunctionDeclaration(Type *tinfo);
356 };
357
358 struct TypeInfoDelegateDeclaration : TypeInfoDeclaration
359 {
360     TypeInfoDelegateDeclaration(Type *tinfo);
361 };
362
363 struct TypeInfoTupleDeclaration : TypeInfoDeclaration
364 {
365     TypeInfoTupleDeclaration(Type *tinfo);
366 };
367
368 #if V2
369 struct TypeInfoConstDeclaration : TypeInfoDeclaration
370 {
371     TypeInfoConstDeclaration(Type *tinfo);
372 };
373
374 struct TypeInfoInvariantDeclaration : TypeInfoDeclaration
375 {
376     TypeInfoInvariantDeclaration(Type *tinfo);
377 };
378 #endif
379
380 /**************************************************************/
381
382 struct ThisDeclaration : VarDeclaration
383 {
384     ThisDeclaration(Type *t);
385     Dsymbol *syntaxCopy(Dsymbol *);
386 };
387
388 enum ILS
389 {
390     ILSuninitialized,   // not computed yet
391     ILSno,      // cannot inline
392     ILSyes,     // can inline
393 };
394
395 /**************************************************************/
396 #if V2
397
398 enum BUILTIN
399 {
400     BUILTINunknown = -1,    // not known if this is a builtin
401     BUILTINnot,         // this is not a builtin
402     BUILTINsin,         // std.math.sin
403     BUILTINcos,         // std.math.cos
404     BUILTINtan,         // std.math.tan
405     BUILTINsqrt,        // std.math.sqrt
406     BUILTINfabs,        // std.math.fabs
407 };
408
409 #endif
410
411 struct FuncDeclaration : Declaration
412 {
413     Array *fthrows;         // Array of Type's of exceptions (not used)
414     Statement *frequire;
415     Statement *fensure;
416     Statement *fbody;
417
418     Identifier *outId;          // identifier for out statement
419     VarDeclaration *vresult;        // variable corresponding to outId
420     LabelDsymbol *returnLabel;      // where the return goes
421
422     DsymbolTable *localsymtab;      // used to prevent symbols in different
423                     // scopes from having the same name
424     VarDeclaration *vthis;      // 'this' parameter (member and nested)
425     VarDeclaration *v_arguments;    // '_arguments' parameter
426 #if IN_GCC
427     VarDeclaration *v_argptr;           // '_argptr' variable
428 #endif
429     Dsymbols *parameters;       // Array of VarDeclaration's for parameters
430     DsymbolTable *labtab;       // statement label symbol table
431     Declaration *overnext;      // next in overload list
432     Loc endloc;             // location of closing curly bracket
433     int vtblIndex;          // for member functions, index into vtbl[]
434     int naked;              // !=0 if naked
435     int inlineAsm;          // !=0 if has inline assembler
436     ILS inlineStatus;
437     int inlineNest;         // !=0 if nested inline
438     int cantInterpret;          // !=0 if cannot interpret function
439     int semanticRun;            // !=0 if semantic3() had been run
440                     // this function's frame ptr
441     ForeachStatement *fes;      // if foreach body, this is the foreach
442     int introducing;            // !=0 if 'introducing' function
443     Type *tintro;           // if !=NULL, then this is the type
444                     // of the 'introducing' function
445                     // this one is overriding
446     int inferRetType;           // !=0 if return type is to be inferred
447     Scope *scope;           // !=NULL means context to use
448
449     // Things that should really go into Scope
450     int hasReturnExp;           // 1 if there's a return exp; statement
451                     // 2 if there's a throw statement
452                     // 4 if there's an assert(0)
453                     // 8 if there's inline asm
454
455     // Support for NRVO (named return value optimization)
456     int nrvo_can;           // !=0 means we can do it
457     VarDeclaration *nrvo_var;       // variable to replace with shidden
458     Symbol *shidden;            // hidden pointer passed to function
459
460 #if V2
461     enum BUILTIN builtin;       // set if this is a known, builtin
462                     // function we can evaluate at compile
463                     // time
464
465     int tookAddressOf;          // set if someone took the address of
466                     // this function
467     Dsymbols closureVars;       // local variables in this function
468                     // which are referenced by nested
469                     // functions
470 #else
471     int nestedFrameRef;         // !=0 if nested variables referenced
472 #endif
473
474     FuncDeclaration(Loc loc, Loc endloc, Identifier *id, enum STC storage_class, Type *type);
475     Dsymbol *syntaxCopy(Dsymbol *);
476     void semantic(Scope *sc);
477     void semantic2(Scope *sc);
478     void semantic3(Scope *sc);
479     void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
480     void bodyToCBuffer(OutBuffer *buf, HdrGenState *hgs);
481     int overrides(FuncDeclaration *fd);
482     int findVtblIndex(Array *vtbl, int dim);
483     int overloadInsert(Dsymbol *s);
484     FuncDeclaration *overloadExactMatch(Type *t);
485     FuncDeclaration *overloadResolve(Loc loc, Expression *ethis, Expressions *arguments, int flags = 0);
486     MATCH leastAsSpecialized(FuncDeclaration *g);
487     LabelDsymbol *searchLabel(Identifier *ident);
488     AggregateDeclaration *isThis();
489     AggregateDeclaration *isMember2();
490     int getLevel(Loc loc, FuncDeclaration *fd); // lexical nesting level difference
491     void appendExp(Expression *e);
492     void appendState(Statement *s);
493     char *mangle();
494     int isMain();
495     int isWinMain();
496     int isDllMain();
497     int isExport();
498     int isImportedSymbol();
499     int isAbstract();
500     int isCodeseg();
501     int isOverloadable();
502     virtual int isNested();
503     int needThis();
504     virtual int isVirtual();
505     virtual int isFinal();
506     virtual int addPreInvariant();
507     virtual int addPostInvariant();
508     Expression *interpret(InterState *istate, Expressions *arguments);
509     void inlineScan();
510     int canInline(int hasthis, int hdrscan = 0);
511     Expression *doInline(InlineScanState *iss, Expression *ethis, Array *arguments);
512     const char *kind();
513     void toDocBuffer(OutBuffer *buf);
514     FuncDeclaration *isUnique();
515     int needsClosure();
516
517     static FuncDeclaration *genCfunc(Type *treturn, const char *name);
518     static FuncDeclaration *genCfunc(Type *treturn, Identifier *id);
519
520     Symbol *toThunkSymbol(int offset);  // thunk version
521     void buildClosure(IRState *irs);
522
523     FuncDeclaration *isFuncDeclaration() { return this; }
524 };
525
526 struct FuncAliasDeclaration : FuncDeclaration
527 {
528     FuncDeclaration *funcalias;
529
530     FuncAliasDeclaration(FuncDeclaration *funcalias);
531
532     FuncAliasDeclaration *isFuncAliasDeclaration() { return this; }
533     const char *kind();
534 };
535
536 struct FuncLiteralDeclaration : FuncDeclaration
537 {
538     enum TOK tok;           // TOKfunction or TOKdelegate
539
540     FuncLiteralDeclaration(Loc loc, Loc endloc, Type *type, enum TOK tok,
541     ForeachStatement *fes);
542     void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
543     Dsymbol *syntaxCopy(Dsymbol *);
544     int isNested();
545     int isVirtual();
546
547     FuncLiteralDeclaration *isFuncLiteralDeclaration() { return this; }
548     const char *kind();
549 };
550
551 struct CtorDeclaration : FuncDeclaration
552 {   Arguments *arguments;
553     int varargs;
554
555     CtorDeclaration(Loc loc, Loc endloc, Arguments *arguments, int varargs);
556     Dsymbol *syntaxCopy(Dsymbol *);
557     void semantic(Scope *sc);
558     void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
559     const char *kind();
560     char *toChars();
561     int isVirtual();
562     int addPreInvariant();
563     int addPostInvariant();
564     void toDocBuffer(OutBuffer *buf);
565
566     CtorDeclaration *isCtorDeclaration() { return this; }
567 };
568
569 #if V2
570 struct PostBlitDeclaration : FuncDeclaration
571 {
572     PostBlitDeclaration(Loc loc, Loc endloc);
573     PostBlitDeclaration(Loc loc, Loc endloc, Identifier *id);
574     Dsymbol *syntaxCopy(Dsymbol *);
575     void semantic(Scope *sc);
576     void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
577     int isVirtual();
578     int addPreInvariant();
579     int addPostInvariant();
580     int overloadInsert(Dsymbol *s);
581     void emitComment(Scope *sc);
582
583     PostBlitDeclaration *isPostBlitDeclaration() { return this; }
584 };
585 #endif
586
587 struct DtorDeclaration : FuncDeclaration
588 {
589     DtorDeclaration(Loc loc, Loc endloc);
590     DtorDeclaration(Loc loc, Loc endloc, Identifier *id);
591     Dsymbol *syntaxCopy(Dsymbol *);
592     void semantic(Scope *sc);
593     void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
594     int isVirtual();
595     int addPreInvariant();
596     int addPostInvariant();
597     int overloadInsert(Dsymbol *s);
598     void emitComment(Scope *sc);
599
600     DtorDeclaration *isDtorDeclaration() { return this; }
601 };
602
603 struct StaticCtorDeclaration : FuncDeclaration
604 {
605     StaticCtorDeclaration(Loc loc, Loc endloc);
606     Dsymbol *syntaxCopy(Dsymbol *);
607     void semantic(Scope *sc);
608     AggregateDeclaration *isThis();
609     int isStaticConstructor();
610     int isVirtual();
611     int addPreInvariant();
612     int addPostInvariant();
613     void emitComment(Scope *sc);
614     void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
615
616     StaticCtorDeclaration *isStaticCtorDeclaration() { return this; }
617 };
618
619 struct StaticDtorDeclaration : FuncDeclaration
620 {   VarDeclaration *vgate;  // 'gate' variable
621
622     StaticDtorDeclaration(Loc loc, Loc endloc);
623     Dsymbol *syntaxCopy(Dsymbol *);
624     void semantic(Scope *sc);
625     AggregateDeclaration *isThis();
626     int isStaticDestructor();
627     int isVirtual();
628     int addPreInvariant();
629     int addPostInvariant();
630     void emitComment(Scope *sc);
631     void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
632
633     StaticDtorDeclaration *isStaticDtorDeclaration() { return this; }
634 };
635
636 struct InvariantDeclaration : FuncDeclaration
637 {
638     InvariantDeclaration(Loc loc, Loc endloc);
639     Dsymbol *syntaxCopy(Dsymbol *);
640     void semantic(Scope *sc);
641     int isVirtual();
642     int addPreInvariant();
643     int addPostInvariant();
644     void emitComment(Scope *sc);
645     void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
646
647     InvariantDeclaration *isInvariantDeclaration() { return this; }
648 };
649
650
651 struct UnitTestDeclaration : FuncDeclaration
652 {
653     UnitTestDeclaration(Loc loc, Loc endloc);
654     Dsymbol *syntaxCopy(Dsymbol *);
655     void semantic(Scope *sc);
656     AggregateDeclaration *isThis();
657     int isVirtual();</