annotate dmd/SynchronizedStatement.d @ 0:10317f0c89a5

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