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