0
|
1 module dmd.StaticDtorDeclaration;
|
|
2
|
|
3 import dmd.FuncDeclaration;
|
|
4 import dmd.VarDeclaration;
|
|
5 import dmd.Dsymbol;
|
|
6 import dmd.Loc;
|
|
7 import dmd.Scope;
|
|
8 import dmd.AggregateDeclaration;
|
|
9 import dmd.OutBuffer;
|
|
10 import dmd.HdrGenState;
|
|
11 import dmd.STC;
|
|
12 import dmd.Identifier;
|
|
13 import dmd.ClassDeclaration;
|
|
14 import dmd.Type;
|
|
15 import dmd.TypeFunction;
|
|
16 import dmd.LINK;
|
|
17 import dmd.Lexer;
|
|
18 import dmd.Statement;
|
|
19 import dmd.Expression;
|
|
20 import dmd.EqualExp;
|
|
21 import dmd.ArrayTypes;
|
|
22 import dmd.DeclarationStatement;
|
|
23 import dmd.IdentifierExp;
|
|
24 import dmd.AddAssignExp;
|
|
25 import dmd.IntegerExp;
|
|
26 import dmd.TOK;
|
|
27 import dmd.IfStatement;
|
|
28 import dmd.ReturnStatement;
|
|
29 import dmd.CompoundStatement;
|
|
30 import dmd.Module;
|
|
31
|
|
32 class StaticDtorDeclaration : FuncDeclaration
|
|
33 {
|
|
34 VarDeclaration vgate; // 'gate' variable
|
|
35
|
|
36 this(Loc loc, Loc endloc)
|
|
37 {
|
|
38 super(loc, endloc, Identifier.generateId("_staticDtor"), STCstatic, null);
|
|
39 }
|
|
40
|
72
|
41 override Dsymbol syntaxCopy(Dsymbol)
|
0
|
42 {
|
|
43 assert(false);
|
|
44 }
|
|
45
|
72
|
46 override void semantic(Scope sc)
|
0
|
47 {
|
|
48 ClassDeclaration cd;
|
|
49 Type tret;
|
|
50
|
|
51 cd = sc.scopesym.isClassDeclaration();
|
|
52 if (!cd)
|
|
53 {
|
|
54 }
|
|
55 type = new TypeFunction(null, Type.tvoid, false, LINK.LINKd);
|
|
56
|
|
57 /* If the static ctor appears within a template instantiation,
|
|
58 * it could get called multiple times by the module constructors
|
|
59 * for different modules. Thus, protect it with a gate.
|
|
60 */
|
|
61 if (inTemplateInstance())
|
|
62 {
|
|
63 /* Add this prefix to the function:
|
|
64 * static int gate;
|
|
65 * if (--gate != 0) return;
|
|
66 * Increment gate during constructor execution.
|
|
67 * Note that this is not thread safe; should not have threads
|
|
68 * during static destruction.
|
|
69 */
|
|
70 Identifier id = Lexer.idPool("__gate");
|
|
71 VarDeclaration v = new VarDeclaration(Loc(0), Type.tint32, id, null);
|
|
72 v.storage_class = STCstatic;
|
|
73 Statements sa = new Statements();
|
|
74 Statement s = new DeclarationStatement(Loc(0), v);
|
|
75 sa.push(cast(void*)s);
|
|
76 Expression e = new IdentifierExp(Loc(0), id);
|
|
77 e = new AddAssignExp(Loc(0), e, new IntegerExp(-1));
|
|
78 e = new EqualExp(TOKnotequal, Loc(0), e, new IntegerExp(0));
|
|
79 s = new IfStatement(Loc(0), null, e, new ReturnStatement(Loc(0), null), null);
|
|
80 sa.push(cast(void*)s);
|
|
81 if (fbody)
|
|
82 sa.push(cast(void*)fbody);
|
|
83 fbody = new CompoundStatement(Loc(0), sa);
|
|
84 vgate = v;
|
|
85 }
|
|
86
|
|
87 FuncDeclaration.semantic(sc);
|
|
88
|
|
89 // We're going to need ModuleInfo
|
|
90 Module m = getModule();
|
|
91 if (!m)
|
|
92 m = sc.module_;
|
|
93 if (m)
|
|
94 {
|
|
95 m.needmoduleinfo = 1;
|
|
96 version (IN_GCC) {
|
|
97 m.strictlyneedmoduleinfo = 1;
|
|
98 }
|
|
99 }
|
|
100 }
|
|
101
|
72
|
102 override AggregateDeclaration isThis()
|
0
|
103 {
|
|
104 return null;
|
|
105 }
|
|
106
|
72
|
107 override bool isStaticDestructor()
|
0
|
108 {
|
|
109 return true;
|
|
110 }
|
|
111
|
72
|
112 override bool isVirtual()
|
0
|
113 {
|
|
114 return false;
|
|
115 }
|
|
116
|
72
|
117 override bool addPreInvariant()
|
0
|
118 {
|
|
119 return false;
|
|
120 }
|
|
121
|
72
|
122 override bool addPostInvariant()
|
0
|
123 {
|
|
124 return false;
|
|
125 }
|
|
126
|
72
|
127 override void emitComment(Scope sc)
|
0
|
128 {
|
|
129 assert(false);
|
|
130 }
|
|
131
|
72
|
132 override void toCBuffer(OutBuffer buf, HdrGenState* hgs)
|
0
|
133 {
|
|
134 assert(false);
|
|
135 }
|
|
136
|
79
|
137 override void toJsonBuffer(OutBuffer buf)
|
|
138 {
|
|
139 }
|
|
140
|
72
|
141 override StaticDtorDeclaration isStaticDtorDeclaration() { return this; }
|
|
142 }
|