annotate dmd/SynchronizedStatement.d @ 122:c77e9f4f1793

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