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 {
|
|
33 this(Loc loc, Loc endloc)
|
|
34 {
|
|
35 super(loc, endloc, Identifier.generateId("_staticCtor"), STCstatic, null);
|
|
36 }
|
|
37
|
72
|
38 override Dsymbol syntaxCopy(Dsymbol)
|
0
|
39 {
|
|
40 assert(false);
|
|
41 }
|
|
42
|
72
|
43 override void semantic(Scope sc)
|
0
|
44 {
|
|
45 //printf("StaticCtorDeclaration.semantic()\n");
|
|
46
|
|
47 type = new TypeFunction(null, Type.tvoid, false, LINK.LINKd);
|
|
48
|
|
49 /* If the static ctor appears within a template instantiation,
|
|
50 * it could get called multiple times by the module constructors
|
|
51 * for different modules. Thus, protect it with a gate.
|
|
52 */
|
|
53 if (inTemplateInstance())
|
|
54 {
|
|
55 /* Add this prefix to the function:
|
|
56 * static int gate;
|
|
57 * if (++gate != 1) return;
|
|
58 * Note that this is not thread safe; should not have threads
|
|
59 * during static construction.
|
|
60 */
|
|
61 Identifier id = Lexer.idPool("__gate");
|
|
62 VarDeclaration v = new VarDeclaration(Loc(0), Type.tint32, id, null);
|
|
63 v.storage_class = STCstatic;
|
|
64 Statements sa = new Statements();
|
|
65 Statement s = new DeclarationStatement(Loc(0), v);
|
|
66 sa.push(cast(void*)s);
|
|
67 Expression e = new IdentifierExp(Loc(0), id);
|
|
68 e = new AddAssignExp(Loc(0), e, new IntegerExp(1));
|
|
69 e = new EqualExp(TOKnotequal, Loc(0), e, new IntegerExp(1));
|
|
70 s = new IfStatement(Loc(0), null, e, new ReturnStatement(Loc(0), null), null);
|
|
71 sa.push(cast(void*)s);
|
|
72 if (fbody)
|
|
73 sa.push(cast(void*)fbody);
|
|
74 fbody = new CompoundStatement(Loc(0), sa);
|
|
75 }
|
|
76
|
|
77 FuncDeclaration.semantic(sc);
|
|
78
|
|
79 // We're going to need ModuleInfo
|
|
80 Module m = getModule();
|
|
81 if (!m)
|
|
82 m = sc.module_;
|
|
83
|
|
84 if (m)
|
|
85 {
|
|
86 m.needmoduleinfo = 1;
|
|
87 version (IN_GCC) {
|
|
88 m.strictlyneedmoduleinfo = 1;
|
|
89 }
|
|
90 }
|
|
91 }
|
|
92
|
72
|
93 override AggregateDeclaration isThis()
|
0
|
94 {
|
|
95 return null;
|
|
96 }
|
|
97
|
72
|
98 override bool isStaticConstructor()
|
0
|
99 {
|
|
100 return true;
|
|
101 }
|
|
102
|
72
|
103 override bool isVirtual()
|
0
|
104 {
|
|
105 return false;
|
|
106 }
|
|
107
|
72
|
108 override bool addPreInvariant()
|
0
|
109 {
|
|
110 return false;
|
|
111 }
|
|
112
|
72
|
113 override bool addPostInvariant()
|
0
|
114 {
|
|
115 return false;
|
|
116 }
|
|
117
|
72
|
118 override void emitComment(Scope sc)
|
0
|
119 {
|
|
120 assert(false);
|
|
121 }
|
|
122
|
72
|
123 override void toCBuffer(OutBuffer buf, HdrGenState* hgs)
|
0
|
124 {
|
|
125 assert(false);
|
|
126 }
|
|
127
|
72
|
128 override StaticCtorDeclaration isStaticCtorDeclaration() { return this; }
|
|
129 }
|