Mercurial > projects > ddmd
annotate dmd/StaticCtorDeclaration.d @ 178:e3afd1303184
Many small bugs fixed
Made all classes derive from TObject to detect memory leaks (functionality is disabled for now)
Began work on overriding backend memory allocations (to avoid memory leaks)
author | korDen |
---|---|
date | Sun, 17 Oct 2010 07:42:00 +0400 |
parents | c77e9f4f1793 |
children | cd48cb899aee |
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 { |
178 | 36 register(); |
0 | 37 super(loc, endloc, Identifier.generateId("_staticCtor"), STCstatic, null); |
38 } | |
39 | |
98 | 40 override Dsymbol syntaxCopy(Dsymbol s) |
0 | 41 { |
98 | 42 assert(!s); |
43 StaticCtorDeclaration scd = new StaticCtorDeclaration(loc, endloc); | |
44 return FuncDeclaration.syntaxCopy(scd); | |
0 | 45 } |
46 | |
98 | 47 override void semantic(Scope sc) |
0 | 48 { |
49 //printf("StaticCtorDeclaration.semantic()\n"); | |
50 | |
51 type = new TypeFunction(null, Type.tvoid, false, LINK.LINKd); | |
52 | |
53 /* If the static ctor appears within a template instantiation, | |
54 * it could get called multiple times by the module constructors | |
55 * for different modules. Thus, protect it with a gate. | |
56 */ | |
57 if (inTemplateInstance()) | |
58 { | |
59 /* Add this prefix to the function: | |
60 * static int gate; | |
61 * if (++gate != 1) return; | |
62 * Note that this is not thread safe; should not have threads | |
63 * during static construction. | |
64 */ | |
65 Identifier id = Lexer.idPool("__gate"); | |
66 VarDeclaration v = new VarDeclaration(Loc(0), Type.tint32, id, null); | |
67 v.storage_class = STCstatic; | |
68 Statements sa = new Statements(); | |
69 Statement s = new DeclarationStatement(Loc(0), v); | |
122
c77e9f4f1793
Statements -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
70 sa.push(s); |
0 | 71 Expression e = new IdentifierExp(Loc(0), id); |
72 e = new AddAssignExp(Loc(0), e, new IntegerExp(1)); | |
73 e = new EqualExp(TOKnotequal, Loc(0), e, new IntegerExp(1)); | |
74 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
|
75 sa.push(s); |
0 | 76 if (fbody) |
122
c77e9f4f1793
Statements -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
77 sa.push(fbody); |
0 | 78 fbody = new CompoundStatement(Loc(0), sa); |
79 } | |
80 | |
81 FuncDeclaration.semantic(sc); | |
82 | |
83 // We're going to need ModuleInfo | |
84 Module m = getModule(); | |
85 if (!m) | |
86 m = sc.module_; | |
87 | |
88 if (m) | |
89 { | |
90 m.needmoduleinfo = 1; | |
98 | 91 // writef("module1 %s needs moduleinfo\n", m.toChars()); |
0 | 92 version (IN_GCC) { |
93 m.strictlyneedmoduleinfo = 1; | |
94 } | |
95 } | |
96 } | |
97 | |
98 | 98 override AggregateDeclaration isThis() |
0 | 99 { |
100 return null; | |
101 } | |
102 | |
98 | 103 override bool isStaticConstructor() |
0 | 104 { |
105 return true; | |
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 } |