annotate dmd/SwitchStatement.d @ 0:10317f0c89a5

Initial commit
author korDen
date Sat, 24 Oct 2009 08:42:06 +0400
parents
children 7427ded8caf7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1 module dmd.SwitchStatement;
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.Expression;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
5 import dmd.DefaultStatement;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
6 import dmd.TryFinallyStatement;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
7 import dmd.Array;
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.OutBuffer;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
11 import dmd.HdrGenState;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
12 import dmd.InlineScanState;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
13 import dmd.IRState;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
14 import dmd.InterState;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
15 import dmd.BE;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
16 import dmd.TY;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
17 import dmd.WANT;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
18 import dmd.GotoCaseStatement;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
19 import dmd.CaseStatement;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
20 import dmd.ArrayTypes;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
21 import dmd.CompoundStatement;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
22 import dmd.Global;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
23 import dmd.SwitchErrorStatement;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
24 import dmd.Type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
25 import dmd.HaltExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
26 import dmd.ExpStatement;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
27 import dmd.BreakStatement;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
28 import dmd.EnumDeclaration;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
29 import dmd.TypeEnum;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
30 import dmd.Dsymbol;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
31 import dmd.EnumMember;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
32 import dmd.TypeTypedef;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
33 import dmd.TOK;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
34 import dmd.StringExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
35
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
36 import dmd.backend.Util;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
37 import dmd.backend.block;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
38 import dmd.backend.Blockx;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
39 import dmd.backend.elem;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
40 import dmd.backend.OPER;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
41 import dmd.backend.TYM;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
42 import dmd.backend.BC;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
43 import dmd.backend.dt_t;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
44 import dmd.backend.Symbol;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
45 import dmd.backend.SC;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
46 import dmd.backend.FL;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
47 import dmd.backend.RTLSYM;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
48 import dmd.backend.targ_types;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
49
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
50 class SwitchStatement : Statement
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
51 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
52 Expression condition;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
53 Statement body_;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
54 bool isFinal;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
55
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
56 DefaultStatement sdefault = null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
57 TryFinallyStatement tf = null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
58 Array gotoCases; // array of unresolved GotoCaseStatement's
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
59 Array cases; // array of CaseStatement's
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
60 int hasNoDefault = 0; // !=0 if no default statement
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
61 int hasVars = 0; // !=0 if has variable case values
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
62
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
63 this(Loc loc, Expression c, Statement b, bool isFinal)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
64 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
65 super(loc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
66
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
67 this.condition = c;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
68 this.body_ = b;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
69 this.isFinal = isFinal;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
70
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
71 gotoCases = new Array();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
72 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
73
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
74 Statement syntaxCopy()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
75 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
76 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
77 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
78
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
79 Statement semantic(Scope sc)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
80 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
81 //printf("SwitchStatement.semantic(%p)\n", this);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
82 tf = sc.tf;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
83 assert(!cases); // ensure semantic() is only run once
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
84 condition = condition.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
85 condition = resolveProperties(sc, condition);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
86 if (condition.type.isString())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
87 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
88 // If it's not an array, cast it to one
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
89 if (condition.type.ty != Tarray)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
90 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
91 condition = condition.implicitCastTo(sc, condition.type.nextOf().arrayOf());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
92 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
93 condition.type = condition.type.constOf();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
94 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
95 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
96 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
97 condition = condition.integralPromotions(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
98 condition.checkIntegral();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
99 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
100 condition = condition.optimize(WANTvalue);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
101
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
102 sc = sc.push();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
103 sc.sbreak = this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
104 sc.sw = this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
105
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
106 cases = new Array();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
107 sc.noctor++; // BUG: should use Scope.mergeCallSuper() for each case instead
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
108 body_ = body_.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
109 sc.noctor--;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
110
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
111 // Resolve any goto case's with exp
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
112 for (int i = 0; i < gotoCases.dim; i++)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
113 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
114 GotoCaseStatement gcs = cast(GotoCaseStatement)gotoCases.data[i];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
115
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
116 if (!gcs.exp)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
117 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
118 gcs.error("no case statement following goto case;");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
119 break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
120 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
121
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
122 for (Scope scx = sc; scx; scx = scx.enclosing)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
123 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
124 if (!scx.sw)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
125 continue;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
126 for (int j = 0; j < scx.sw.cases.dim; j++)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
127 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
128 CaseStatement cs = cast(CaseStatement)scx.sw.cases.data[j];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
129
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
130 if (cs.exp.equals(gcs.exp))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
131 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
132 gcs.cs = cs;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
133 goto Lfoundcase;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
134 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
135 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
136 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
137 gcs.error("case %s not found", gcs.exp.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
138
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
139 Lfoundcase:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
140 ;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
141 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
142
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
143 if (!sc.sw.sdefault && !isFinal)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
144 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
145 hasNoDefault = 1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
146
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
147 warning("switch statement has no default");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
148
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
149 // Generate runtime error if the default is hit
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
150 Statements a = new Statements();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
151 CompoundStatement cs;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
152 Statement s;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
153
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
154 if (global.params.useSwitchError)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
155 s = new SwitchErrorStatement(loc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
156 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
157 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
158 Expression e = new HaltExp(loc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
159 s = new ExpStatement(loc, e);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
160 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
161
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
162 a.reserve(4);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
163 a.push(cast(void*)body_);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
164 a.push(cast(void*)new BreakStatement(loc, null));
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
165 sc.sw.sdefault = new DefaultStatement(loc, s);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
166 a.push(cast(void*)sc.sw.sdefault);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
167 cs = new CompoundStatement(loc, a);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
168 body_ = cs;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
169 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
170
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
171 version (DMDV2) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
172 if (isFinal)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
173 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
174 Type t = condition.type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
175 while (t.ty == Ttypedef)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
176 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
177 // Don't use toBasetype() because that will skip past enums
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
178 t = (cast(TypeTypedef)t).sym.basetype;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
179 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
180 if (condition.type.ty == Tenum)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
181 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
182 TypeEnum te = cast(TypeEnum)condition.type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
183 EnumDeclaration ed = te.toDsymbol(sc).isEnumDeclaration();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
184 assert(ed);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
185 size_t dim = ed.members.dim;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
186 for (size_t i = 0; i < dim; i++)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
187 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
188 EnumMember em = (cast(Dsymbol)ed.members.data[i]).isEnumMember();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
189 if (em)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
190 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
191 for (size_t j = 0; j < cases.dim; j++)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
192 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
193 CaseStatement cs = cast(CaseStatement)cases.data[j];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
194 if (cs.exp.equals(em.value))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
195 goto L1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
196 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
197 error("enum member %s not represented in final switch", em.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
198 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
199 L1:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
200 ;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
201 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
202 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
203 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
204 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
205
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
206 sc.pop();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
207 return this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
208 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
209
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
210 bool hasBreak()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
211 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
212 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
213 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
214
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
215 bool usesEH()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
216 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
217 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
218 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
219
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
220 BE blockExit()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
221 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
222 BE result = BE.BEnone;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
223 if (condition.canThrow())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
224 result |= BE.BEthrow;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
225
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
226 if (body_)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
227 { result |= body_.blockExit();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
228 if (result & BE.BEbreak)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
229 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
230 result |= BE.BEfallthru;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
231 result &= ~BE.BEbreak;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
232 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
233 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
234 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
235 result |= BE.BEfallthru;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
236
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
237 return result;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
238 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
239
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
240 Expression interpret(InterState* istate)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
241 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
242 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
243 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
244
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
245 void toCBuffer(OutBuffer buf, HdrGenState* hgs)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
246 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
247 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
248 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
249
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
250 Statement inlineScan(InlineScanState* iss)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
251 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
252 //printf("SwitchStatement.inlineScan()\n");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
253 condition = condition.inlineScan(iss);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
254 body_ = body_ ? body_.inlineScan(iss) : null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
255 if (sdefault)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
256 sdefault = cast(DefaultStatement)sdefault.inlineScan(iss);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
257 if (cases)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
258 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
259 for (int i = 0; i < cases.dim; i++)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
260 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
261 Statement s = cast(Statement)cases.data[i];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
262 cases.data[i] = cast(void*)s.inlineScan(iss);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
263 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
264 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
265 return this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
266 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
267
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
268 void toIR(IRState* irs)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
269 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
270 int string;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
271 Blockx* blx = irs.blx;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
272
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
273 //printf("SwitchStatement.toIR()\n");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
274 IRState mystate = IRState(irs,this);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
275
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
276 mystate.switchBlock = blx.curblock;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
277
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
278 /* Block for where "break" goes to
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
279 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
280 mystate.breakBlock = block_calloc(blx);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
281
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
282 /* Block for where "default" goes to.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
283 * If there is a default statement, then that is where default goes.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
284 * If not, then do:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
285 * default: break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
286 * by making the default block the same as the break block.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
287 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
288 mystate.defaultBlock = sdefault ? block_calloc(blx) : mystate.breakBlock;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
289
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
290 int numcases = 0;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
291 if (cases)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
292 numcases = cases.dim;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
293
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
294 incUsage(irs, loc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
295 elem* econd = condition.toElem(&mystate);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
296
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
297 version (DMDV2) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
298 if (hasVars)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
299 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
300 /* Generate a sequence of if-then-else blocks for the cases.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
301 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
302 if (econd.Eoper != OPvar)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
303 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
304 elem* e = exp2_copytotemp(econd);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
305 block_appendexp(mystate.switchBlock, e);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
306 econd = e.E2;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
307 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
308
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
309 for (int i = 0; i < numcases; i++)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
310 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
311 CaseStatement cs = cast(CaseStatement)cases.data[i];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
312
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
313 elem* ecase = cs.exp.toElem(&mystate);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
314 elem* e = el_bin(OPeqeq, TYbool, el_copytree(econd), ecase);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
315 block* b = blx.curblock;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
316 block_appendexp(b, e);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
317 block* bcase = block_calloc(blx);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
318 cs.cblock = bcase;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
319 block_next(blx, BCiftrue, null);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
320 list_append(&b.Bsucc, bcase);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
321 list_append(&b.Bsucc, blx.curblock);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
322 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
323
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
324 /* The final 'else' clause goes to the default
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
325 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
326 block* b = blx.curblock;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
327 block_next(blx, BCgoto, null);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
328 list_append(&b.Bsucc, mystate.defaultBlock);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
329
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
330 body_.toIR(&mystate);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
331
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
332 /* Have the end of the switch body fall through to the block
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
333 * following the switch statement.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
334 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
335 block_goto(blx, BCgoto, mystate.breakBlock);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
336 return;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
337 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
338 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
339
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
340 if (condition.type.isString())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
341 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
342 // Number the cases so we can unscramble things after the sort()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
343 for (int i = 0; i < numcases; i++)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
344 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
345 CaseStatement cs = cast(CaseStatement)cases.data[i];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
346 cs.index = i;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
347 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
348
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
349 cases.sort();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
350
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
351 /* Create a sorted array of the case strings, and si
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
352 * will be the symbol for it.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
353 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
354 dt_t* dt = null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
355 Symbol* si = symbol_generate(SCstatic,type_fake(TYullong));
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
356 version (MACHOBJ) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
357 si.Sseg = Segment.DATA;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
358 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
359 dtdword(&dt, numcases);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
360 dtxoff(&dt, si, 8, TYnptr);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
361
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
362 for (int i = 0; i < numcases; i++)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
363 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
364 CaseStatement cs = cast(CaseStatement)cases.data[i];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
365
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
366 if (cs.exp.op != TOKstring)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
367 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
368 error("case '%s' is not a string", cs.exp.toChars()); // BUG: this should be an assert
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
369 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
370 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
371 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
372 StringExp se = cast(StringExp)(cs.exp);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
373 uint len = se.len;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
374 dtdword(&dt, len);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
375 dtabytes(&dt, TYnptr, 0, se.len * se.sz, cast(char*)se.string_);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
376 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
377 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
378
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
379 si.Sdt = dt;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
380 si.Sfl = FLdata;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
381 outdata(si);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
382
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
383 /* Call:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
384 * _d_switch_string(string[] si, string econd)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
385 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
386 elem* eparam = el_param(econd, el_var(si));
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
387 switch (condition.type.nextOf().ty)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
388 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
389 case Tchar:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
390 econd = el_bin(OPcall, TYint, el_var(rtlsym[RTLSYM_SWITCH_STRING]), eparam);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
391 break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
392 case Twchar:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
393 econd = el_bin(OPcall, TYint, el_var(rtlsym[RTLSYM_SWITCH_USTRING]), eparam);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
394 break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
395 case Tdchar: // BUG: implement
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
396 econd = el_bin(OPcall, TYint, el_var(rtlsym[RTLSYM_SWITCH_DSTRING]), eparam);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
397 break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
398 default:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
399 assert(0);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
400 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
401 elem_setLoc(econd, loc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
402 string = 1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
403 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
404 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
405 string = 0;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
406 block_appendexp(mystate.switchBlock, econd);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
407 block_next(blx,BCswitch,null);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
408
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
409 targ_llong* pu = cast(targ_llong*) malloc(targ_llong.sizeof * (numcases + 1));
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
410 mystate.switchBlock.Bswitch = pu;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
411 /* First pair is the number of cases, and the default block
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
412 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
413 *pu++ = numcases;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
414 list_append(&mystate.switchBlock.Bsucc, mystate.defaultBlock);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
415
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
416 /* Fill in the first entry in each pair, which is the case value.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
417 * CaseStatement.toIR() will fill in
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
418 * the second entry for each pair with the block.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
419 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
420 for (int i = 0; i < numcases; i++)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
421 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
422 CaseStatement cs = cast(CaseStatement)cases.data[i];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
423 if (string)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
424 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
425 pu[cs.index] = i;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
426 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
427 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
428 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
429 pu[i] = cs.exp.toInteger();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
430 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
431 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
432
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
433 body_.toIR(&mystate);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
434
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
435 /* Have the end of the switch body fall through to the block
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
436 * following the switch statement.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
437 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
438 block_goto(blx, BCgoto, mystate.breakBlock);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
439 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
440 }