Mercurial > projects > ddmd
annotate dmd/StaticCtorDeclaration.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.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 | |
187
b0d41ff5e0df
Added expandability scheme outlined in http://www.dsource.org/forums/viewtopic.php?t=5659&sid=6f2150ff5b0bffcd47512a6a7608d218
Abscissa
parents:
179
diff
changeset
|
32 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
|
33 |
0 | 34 class StaticCtorDeclaration : FuncDeclaration |
35 { | |
187
b0d41ff5e0df
Added expandability scheme outlined in http://www.dsource.org/forums/viewtopic.php?t=5659&sid=6f2150ff5b0bffcd47512a6a7608d218
Abscissa
parents:
179
diff
changeset
|
36 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
|
37 |
179 | 38 this(Loc loc, Loc endloc, string name = "_staticCtor") |
0 | 39 { |
178 | 40 register(); |
0 | 41 super(loc, endloc, Identifier.generateId("_staticCtor"), STCstatic, null); |
42 } | |
179 | 43 |
98 | 44 override Dsymbol syntaxCopy(Dsymbol s) |
0 | 45 { |
98 | 46 assert(!s); |
47 StaticCtorDeclaration scd = new StaticCtorDeclaration(loc, endloc); | |
48 return FuncDeclaration.syntaxCopy(scd); | |
0 | 49 } |
50 | |
98 | 51 override void semantic(Scope sc) |
0 | 52 { |
53 //printf("StaticCtorDeclaration.semantic()\n"); | |
54 | |
179 | 55 if (!type) |
56 type = new TypeFunction(null, Type.tvoid, false, LINK.LINKd); | |
0 | 57 |
58 /* If the static ctor appears within a template instantiation, | |
59 * it could get called multiple times by the module constructors | |
60 * for different modules. Thus, protect it with a gate. | |
61 */ | |
179 | 62 if (inTemplateInstance() && semanticRun < PASSsemantic) |
0 | 63 { |
64 /* Add this prefix to the function: | |
65 * static int gate; | |
66 * if (++gate != 1) return; | |
67 * Note that this is not thread safe; should not have threads | |
68 * during static construction. | |
69 */ | |
70 Identifier id = Lexer.idPool("__gate"); | |
71 VarDeclaration v = new VarDeclaration(Loc(0), Type.tint32, id, null); | |
179 | 72 v.storage_class = isSharedStaticCtorDeclaration() ? STCstatic : STCtls; |
0 | 73 Statements sa = new Statements(); |
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(1)); | |
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 } | |
85 | |
86 FuncDeclaration.semantic(sc); | |
87 | |
88 // We're going to need ModuleInfo | |
89 Module m = getModule(); | |
90 if (!m) | |
91 m = sc.module_; | |
92 | |
93 if (m) | |
94 { | |
95 m.needmoduleinfo = 1; | |
98 | 96 // writef("module1 %s needs moduleinfo\n", m.toChars()); |
0 | 97 version (IN_GCC) { |
98 m.strictlyneedmoduleinfo = 1; | |
99 } | |
100 } | |
101 } | |
102 | |
98 | 103 override AggregateDeclaration isThis() |
0 | 104 { |
105 return null; | |
106 } | |
107 | |
98 | 108 override bool isVirtual() |
0 | 109 { |
110 return false; | |
111 } | |
112 | |
98 | 113 override bool addPreInvariant() |
114 { | |
115 return false; | |
116 } | |
117 | |
118 override bool addPostInvariant() | |
0 | 119 { |
120 return false; | |
121 } | |
122 | |
98 | 123 override void emitComment(Scope sc) |
0 | 124 { |
125 } | |
126 | |
98 | 127 override void toCBuffer(OutBuffer buf, HdrGenState* hgs) |
0 | 128 { |
98 | 129 if (hgs.hdrgen) |
130 { | |
131 buf.writestring("static this();\n"); | |
132 return; | |
133 } | |
134 buf.writestring("static this()"); | |
135 bodyToCBuffer(buf, hgs); | |
0 | 136 } |
137 | |
79 | 138 override void toJsonBuffer(OutBuffer buf) |
139 { | |
140 } | |
141 | |
142 override StaticCtorDeclaration isStaticCtorDeclaration() { return this; } | |
72 | 143 } |