annotate dmd/SynchronizedStatement.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 b0d41ff5e0df
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1 module dmd.SynchronizedStatement;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
2
114
e28b18c23469 added a module dmd.common for commonly used stuff
Trass3r
parents: 72
diff changeset
3 import dmd.common;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
4 import dmd.Statement;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
5 import dmd.IntegerExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
6 import dmd.TypeSArray;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
7 import dmd.CompoundStatement;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
8 import dmd.Loc;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
9 import dmd.Scope;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
10 import dmd.Expression;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
11 import dmd.ClassDeclaration;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
12 import dmd.Id;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
13 import dmd.TypeIdentifier;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
14 import dmd.Type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
15 import dmd.OutBuffer;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
16 import dmd.HdrGenState;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
17 import dmd.InlineScanState;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
18 import dmd.IRState;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
19 import dmd.CastExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
20 import dmd.TryFinallyStatement;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
21 import dmd.ExpStatement;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
22 import dmd.CallExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
23 import dmd.DeclarationExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
24 import dmd.VarExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
25 import dmd.DeclarationStatement;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
26 import dmd.ArrayTypes;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
27 import dmd.Statement;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
28 import dmd.VarDeclaration;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
29 import dmd.ExpInitializer;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
30 import dmd.Lexer;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
31 import dmd.Identifier;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
32 import dmd.FuncDeclaration;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
33 import dmd.BE;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
34 import dmd.STC;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
35 import dmd.DotIdExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
36
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
37 import dmd.backend.elem;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
38 import dmd.backend.Util;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
39
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
40 class SynchronizedStatement : Statement
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
41 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
42 Expression exp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
43 Statement body_;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
44
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
45 this(Loc loc, Expression exp, Statement body_)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
46 {
178
e3afd1303184 Many small bugs fixed
korDen
parents: 122
diff changeset
47 register();
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
48 super(loc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
49
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
50 this.exp = exp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
51 this.body_ = body_;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
52 this.esync = null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
53 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
54
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 0
diff changeset
55 override Statement syntaxCopy()
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
56 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
57 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
58 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
59
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 0
diff changeset
60 override Statement semantic(Scope sc)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
61 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
62 if (exp)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
63 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
64 exp = exp.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
65 exp = resolveProperties(sc, exp);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
66 ClassDeclaration cd = exp.type.isClassHandle();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
67 if (!cd)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
68 error("can only synchronize on class objects, not '%s'", exp.type.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
69 else if (cd.isInterfaceDeclaration())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
70 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
71 /* Cast the interface to an object, as the object has the monitor,
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
72 * not the interface.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
73 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
74 Type t = new TypeIdentifier(Loc(0), Id.Object_);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
75
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
76 t = t.semantic(Loc(0), sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
77 exp = new CastExp(loc, exp, t);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
78 exp = exp.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
79 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
80
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
81 static if (true) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
82 /* Rewrite as:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
83 * auto tmp = exp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
84 * _d_monitorenter(tmp);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
85 * try { body } finally { _d_monitorexit(tmp); }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
86 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
87 Identifier id = Lexer.uniqueId("__sync");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
88 ExpInitializer ie = new ExpInitializer(loc, exp);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
89 VarDeclaration tmp = new VarDeclaration(loc, exp.type, id, ie);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
90
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
91 Statements cs = new Statements();
122
c77e9f4f1793 Statements -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 114
diff changeset
92 cs.push(new DeclarationStatement(loc, new DeclarationExp(loc, tmp)));
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
93
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
94 FuncDeclaration fdenter = FuncDeclaration.genCfunc(Type.tvoid, Id.monitorenter);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
95 Expression e = new CallExp(loc, new VarExp(loc, fdenter), new VarExp(loc, tmp));
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
96 e.type = Type.tvoid; // do not run semantic on e
122
c77e9f4f1793 Statements -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 114
diff changeset
97 cs.push(new ExpStatement(loc, e));
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
98
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
99 FuncDeclaration fdexit = FuncDeclaration.genCfunc(Type.tvoid, Id.monitorexit);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
100 e = new CallExp(loc, new VarExp(loc, fdexit), new VarExp(loc, tmp));
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
101 e.type = Type.tvoid; // do not run semantic on e
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
102 Statement s = new ExpStatement(loc, e);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
103 s = new TryFinallyStatement(loc, body_, s);
122
c77e9f4f1793 Statements -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 114
diff changeset
104 cs.push(s);
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
105
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
106 s = new CompoundStatement(loc, cs);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
107 return s.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
108 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
109 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
110 /// static if (true) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
111 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
112 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
113 /* Generate our own critical section, then rewrite as:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
114 * __gshared byte[CriticalSection.sizeof] critsec;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
115 * _d_criticalenter(critsec.ptr);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
116 * try { body } finally { _d_criticalexit(critsec.ptr); }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
117 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
118 Identifier id = Lexer.uniqueId("__critsec");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
119 Type t = new TypeSArray(Type.tint8, new IntegerExp(PTRSIZE + os_critsecsize()));
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
120 VarDeclaration tmp = new VarDeclaration(loc, t, id, null);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
121 tmp.storage_class |= STCgshared | STCstatic;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
122
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
123 Statements cs = new Statements();
122
c77e9f4f1793 Statements -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 114
diff changeset
124 cs.push(new DeclarationStatement(loc, new DeclarationExp(loc, tmp)));
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
125
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
126 FuncDeclaration fdenter = FuncDeclaration.genCfunc(Type.tvoid, Id.criticalenter);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
127 Expression e = new DotIdExp(loc, new VarExp(loc, tmp), Id.ptr);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
128 e = e.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
129 e = new CallExp(loc, new VarExp(loc, fdenter), e);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
130 e.type = Type.tvoid; // do not run semantic on e
122
c77e9f4f1793 Statements -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 114
diff changeset
131 cs.push(new ExpStatement(loc, e));
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
132
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
133 FuncDeclaration fdexit = FuncDeclaration.genCfunc(Type.tvoid, Id.criticalexit);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
134 e = new DotIdExp(loc, new VarExp(loc, tmp), Id.ptr);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
135 e = e.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
136 e = new CallExp(loc, new VarExp(loc, fdexit), e);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
137 e.type = Type.tvoid; // do not run semantic on e
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
138 Statement s = new ExpStatement(loc, e);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
139 s = new TryFinallyStatement(loc, body_, s);
122
c77e9f4f1793 Statements -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 114
diff changeset
140 cs.push(s);
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
141
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
142 s = new CompoundStatement(loc, cs);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
143 return s.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
144 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
145 /// }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
146 if (body_)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
147 body_ = body_.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
148
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
149 return this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
150 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
151
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 0
diff changeset
152 override bool hasBreak()
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
153 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
154 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
155 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
156
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 0
diff changeset
157 override bool hasContinue()
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
158 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
159 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
160 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
161
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 0
diff changeset
162 override bool usesEH()
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
163 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
164 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
165 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
166
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 0
diff changeset
167 override BE blockExit()
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
168 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
169 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
170 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
171
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 0
diff changeset
172 override void toCBuffer(OutBuffer buf, HdrGenState* hgs)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
173 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
174 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
175 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
176
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 0
diff changeset
177 override Statement inlineScan(InlineScanState* iss)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
178 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
179 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
180 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
181
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
182 // Back end
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
183 elem* esync;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
184
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
185 this(Loc loc, elem *esync, Statement body_)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
186 {
178
e3afd1303184 Many small bugs fixed
korDen
parents: 122
diff changeset
187 register();
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
188 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
189 super(loc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
190 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
191
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 0
diff changeset
192 override void toIR(IRState* irs)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
193 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
194 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
195 }
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 0
diff changeset
196 }