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