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