Mercurial > projects > ddmd
annotate dmd/StaticCtorDeclaration.d @ 168:ceed63f310fb
stringtable, stringbuffer and freelist moved to Global
author | korDen |
---|---|
date | Thu, 30 Sep 2010 12:57:13 +0400 |
parents | c77e9f4f1793 |
children | e3afd1303184 |
rev | line source |
---|---|
0 | 1 module dmd.StaticCtorDeclaration; |
2 | |
114 | 3 import dmd.common; |
0 | 4 import dmd.FuncDeclaration; |
5 import dmd.Loc; | |
6 import dmd.Dsymbol; | |
7 import dmd.AggregateDeclaration; | |
8 import dmd.Scope; | |
9 import dmd.OutBuffer; | |
10 import dmd.HdrGenState; | |
11 import dmd.STC; | |
12 import dmd.Identifier; | |
13 import dmd.TypeFunction; | |
14 import dmd.Type; | |
15 import dmd.LINK; | |
16 import dmd.Lexer; | |
17 import dmd.VarDeclaration; | |
18 import dmd.ArrayTypes; | |
19 import dmd.Expression; | |
20 import dmd.Statement; | |
21 import dmd.DeclarationStatement; | |
22 import dmd.AddAssignExp; | |
23 import dmd.EqualExp; | |
24 import dmd.TOK; | |
25 import dmd.IfStatement; | |
26 import dmd.CompoundStatement; | |
27 import dmd.Module; | |
28 import dmd.IntegerExp; | |
29 import dmd.ReturnStatement; | |
30 import dmd.IdentifierExp; | |
31 | |
32 class StaticCtorDeclaration : FuncDeclaration | |
33 { | |
98 | 34 this(Loc loc, Loc endloc) |
0 | 35 { |
36 super(loc, endloc, Identifier.generateId("_staticCtor"), STCstatic, null); | |
37 } | |
38 | |
98 | 39 override Dsymbol syntaxCopy(Dsymbol s) |
0 | 40 { |
98 | 41 assert(!s); |
42 StaticCtorDeclaration scd = new StaticCtorDeclaration(loc, endloc); | |
43 return FuncDeclaration.syntaxCopy(scd); | |
0 | 44 } |
45 | |
98 | 46 override void semantic(Scope sc) |
0 | 47 { |
48 //printf("StaticCtorDeclaration.semantic()\n"); | |
49 | |
50 type = new TypeFunction(null, Type.tvoid, false, LINK.LINKd); | |
51 | |
52 /* If the static ctor appears within a template instantiation, | |
53 * it could get called multiple times by the module constructors | |
54 * for different modules. Thus, protect it with a gate. | |
55 */ | |
56 if (inTemplateInstance()) | |
57 { | |
58 /* Add this prefix to the function: | |
59 * static int gate; | |
60 * if (++gate != 1) return; | |
61 * Note that this is not thread safe; should not have threads | |
62 * during static construction. | |
63 */ | |
64 Identifier id = Lexer.idPool("__gate"); | |
65 VarDeclaration v = new VarDeclaration(Loc(0), Type.tint32, id, null); | |
66 v.storage_class = STCstatic; | |
67 Statements sa = new Statements(); | |
68 Statement s = new DeclarationStatement(Loc(0), v); | |
122
c77e9f4f1793
Statements -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
69 sa.push(s); |
0 | 70 Expression e = new IdentifierExp(Loc(0), id); |
71 e = new AddAssignExp(Loc(0), e, new IntegerExp(1)); | |
72 e = new EqualExp(TOKnotequal, Loc(0), e, new IntegerExp(1)); | |
73 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
|
74 sa.push(s); |
0 | 75 if (fbody) |
122
c77e9f4f1793
Statements -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
76 sa.push(fbody); |
0 | 77 fbody = new CompoundStatement(Loc(0), sa); |
78 } | |
79 | |
80 FuncDeclaration.semantic(sc); | |
81 | |
82 // We're going to need ModuleInfo | |
83 Module m = getModule(); | |
84 if (!m) | |
85 m = sc.module_; | |
86 | |
87 if (m) | |
88 { | |
89 m.needmoduleinfo = 1; | |
98 | 90 // writef("module1 %s needs moduleinfo\n", m.toChars()); |
0 | 91 version (IN_GCC) { |
92 m.strictlyneedmoduleinfo = 1; | |
93 } | |
94 } | |
95 } | |
96 | |
98 | 97 override AggregateDeclaration isThis() |
0 | 98 { |
99 return null; | |
100 } | |
101 | |
98 | 102 override bool isStaticConstructor() |
0 | 103 { |
104 return true; | |
105 } | |
106 | |
98 | 107 override bool isVirtual() |
0 | 108 { |
109 return false; | |
110 } | |
111 | |
98 | 112 override bool addPreInvariant() |
113 { | |
114 return false; | |
115 } | |
116 | |
117 override bool addPostInvariant() | |
0 | 118 { |
119 return false; | |
120 } | |
121 | |
98 | 122 override void emitComment(Scope sc) |
0 | 123 { |
124 } | |
125 | |
98 | 126 override void toCBuffer(OutBuffer buf, HdrGenState* hgs) |
0 | 127 { |
98 | 128 if (hgs.hdrgen) |
129 { | |
130 buf.writestring("static this();\n"); | |
131 return; | |
132 } | |
133 buf.writestring("static this()"); | |
134 bodyToCBuffer(buf, hgs); | |
0 | 135 } |
136 | |
79 | 137 override void toJsonBuffer(OutBuffer buf) |
138 { | |
139 } | |
140 | |
141 override StaticCtorDeclaration isStaticCtorDeclaration() { return this; } | |
72 | 142 } |