Mercurial > projects > ddmd
annotate dmd/StaticDtorDeclaration.d @ 187:b0d41ff5e0df
Added expandability scheme outlined in http://www.dsource.org/forums/viewtopic.php?t=5659&sid=6f2150ff5b0bffcd47512a6a7608d218
author | Abscissa |
---|---|
date | Tue, 07 Jun 2011 23:37:34 -0400 |
parents | cd48cb899aee |
children |
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 | |
187
b0d41ff5e0df
Added expandability scheme outlined in http://www.dsource.org/forums/viewtopic.php?t=5659&sid=6f2150ff5b0bffcd47512a6a7608d218
Abscissa
parents:
179
diff
changeset
|
33 import dmd.DDMDExtensions; |
b0d41ff5e0df
Added expandability scheme outlined in http://www.dsource.org/forums/viewtopic.php?t=5659&sid=6f2150ff5b0bffcd47512a6a7608d218
Abscissa
parents:
179
diff
changeset
|
34 |
0 | 35 class StaticDtorDeclaration : FuncDeclaration |
36 { | |
187
b0d41ff5e0df
Added expandability scheme outlined in http://www.dsource.org/forums/viewtopic.php?t=5659&sid=6f2150ff5b0bffcd47512a6a7608d218
Abscissa
parents:
179
diff
changeset
|
37 mixin insertMemberExtension!(typeof(this)); |
b0d41ff5e0df
Added expandability scheme outlined in http://www.dsource.org/forums/viewtopic.php?t=5659&sid=6f2150ff5b0bffcd47512a6a7608d218
Abscissa
parents:
179
diff
changeset
|
38 |
0 | 39 VarDeclaration vgate; // 'gate' variable |
40 | |
179 | 41 this(Loc loc, Loc endloc, string name = "_staticDtor") |
0 | 42 { |
178 | 43 register(); |
179 | 44 super(loc, endloc, Identifier.generateId(name), STCstatic, null); |
45 vgate = null; | |
0 | 46 } |
47 | |
100 | 48 override Dsymbol syntaxCopy(Dsymbol s) |
0 | 49 { |
100 | 50 assert(!s); |
51 StaticDtorDeclaration sdd = new StaticDtorDeclaration(loc, endloc); | |
52 return super.syntaxCopy(sdd); | |
0 | 53 } |
54 | |
100 | 55 override void semantic(Scope sc) |
0 | 56 { |
179 | 57 ClassDeclaration cd = sc.scopesym.isClassDeclaration(); |
58 if (!type) | |
59 type = new TypeFunction(null, Type.tvoid, false, LINK.LINKd); | |
0 | 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 */ | |
179 | 65 if (inTemplateInstance() && semanticRun < PASSsemantic) |
0 | 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); | |
179 | 76 v.storage_class = isSharedStaticDtorDeclaration() ? STCstatic : STCtls; |
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 isVirtual() |
0 | 113 { |
114 return false; | |
115 } | |
116 | |
100 | 117 override bool addPreInvariant() |
118 { | |
119 return false; | |
120 } | |
121 | |
122 override bool addPostInvariant() | |
0 | 123 { |
124 return false; | |
125 } | |
126 | |
100 | 127 override void emitComment(Scope sc) |
0 | 128 { |
129 } | |
130 | |
100 | 131 override void toCBuffer(OutBuffer buf, HdrGenState* hgs) |
0 | 132 { |
100 | 133 if (hgs.hdrgen) |
134 return; | |
135 buf.writestring("static ~this()"); | |
136 bodyToCBuffer(buf, hgs); | |
0 | 137 } |
138 | |
79 | 139 override void toJsonBuffer(OutBuffer buf) |
140 { | |
141 } | |
142 | |
100 | 143 override StaticDtorDeclaration isStaticDtorDeclaration() { return this; } |
72 | 144 } |