Mercurial > projects > ddmd
annotate dmd/StaticDtorDeclaration.d @ 179:cd48cb899aee
Updated to dmd2.040
author | korDen |
---|---|
date | Sun, 17 Oct 2010 20:56:07 +0400 |
parents | e3afd1303184 |
children | b0d41ff5e0df |
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 | |
179 | 37 this(Loc loc, Loc endloc, string name = "_staticDtor") |
0 | 38 { |
178 | 39 register(); |
179 | 40 super(loc, endloc, Identifier.generateId(name), STCstatic, null); |
41 vgate = null; | |
0 | 42 } |
43 | |
100 | 44 override Dsymbol syntaxCopy(Dsymbol s) |
0 | 45 { |
100 | 46 assert(!s); |
47 StaticDtorDeclaration sdd = new StaticDtorDeclaration(loc, endloc); | |
48 return super.syntaxCopy(sdd); | |
0 | 49 } |
50 | |
100 | 51 override void semantic(Scope sc) |
0 | 52 { |
179 | 53 ClassDeclaration cd = sc.scopesym.isClassDeclaration(); |
54 if (!type) | |
55 type = new TypeFunction(null, Type.tvoid, false, LINK.LINKd); | |
0 | 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 */ | |
179 | 61 if (inTemplateInstance() && semanticRun < PASSsemantic) |
0 | 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); | |
179 | 72 v.storage_class = isSharedStaticDtorDeclaration() ? STCstatic : STCtls; |
122
c77e9f4f1793
Statements -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
73 auto sa = new Statements(); |
0 | 74 Statement s = new DeclarationStatement(Loc(0), v); |
122
c77e9f4f1793
Statements -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
75 sa.push(s); |
0 | 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); | |
122
c77e9f4f1793
Statements -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
80 sa.push(s); |
0 | 81 if (fbody) |
122
c77e9f4f1793
Statements -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
82 sa.push(fbody); |
0 | 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; | |
98 | 96 // writef("module2 %s needs moduleinfo\n", m.toChars()); |
0 | 97 version (IN_GCC) { |
98 m.strictlyneedmoduleinfo = 1; | |
99 } | |
100 } | |
101 } | |
102 | |
100 | 103 override AggregateDeclaration isThis() |
0 | 104 { |
105 return null; | |
106 } | |
107 | |
100 | 108 override bool isVirtual() |
0 | 109 { |
110 return false; | |
111 } | |
112 | |
100 | 113 override bool addPreInvariant() |
114 { | |
115 return false; | |
116 } | |
117 | |
118 override bool addPostInvariant() | |
0 | 119 { |
120 return false; | |
121 } | |
122 | |
100 | 123 override void emitComment(Scope sc) |
0 | 124 { |
125 } | |
126 | |
100 | 127 override void toCBuffer(OutBuffer buf, HdrGenState* hgs) |
0 | 128 { |
100 | 129 if (hgs.hdrgen) |
130 return; | |
131 buf.writestring("static ~this()"); | |
132 bodyToCBuffer(buf, hgs); | |
0 | 133 } |
134 | |
79 | 135 override void toJsonBuffer(OutBuffer buf) |
136 { | |
137 } | |
138 | |
100 | 139 override StaticDtorDeclaration isStaticDtorDeclaration() { return this; } |
72 | 140 } |