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