Mercurial > projects > ddmd
annotate dmd/CaseStatement.d @ 187:b0d41ff5e0df
Added expandability scheme outlined in http://www.dsource.org/forums/viewtopic.php?t=5659&sid=6f2150ff5b0bffcd47512a6a7608d218
author | Abscissa |
---|---|
date | Tue, 07 Jun 2011 23:37:34 -0400 |
parents | e3afd1303184 |
children |
rev | line source |
---|---|
0 | 1 module dmd.CaseStatement; |
2 | |
114 | 3 import dmd.common; |
0 | 4 import dmd.Statement; |
5 import dmd.Expression; | |
6 import dmd.Statement; | |
7 import dmd.Scope; | |
8 import dmd.Loc; | |
9 import dmd.IRState; | |
10 import dmd.InlineScanState; | |
11 import dmd.HdrGenState; | |
12 import dmd.OutBuffer; | |
13 import dmd.InterState; | |
14 import dmd.BE; | |
15 import dmd.SwitchStatement; | |
16 import dmd.WANT; | |
17 import dmd.TOK; | |
18 import dmd.VarExp; | |
19 import dmd.VarDeclaration; | |
20 import dmd.Type; | |
21 import dmd.TY; | |
22 import dmd.IntegerExp; | |
23 import dmd.GotoCaseStatement; | |
24 | |
25 import dmd.backend.block; | |
26 import dmd.backend.Blockx; | |
27 import dmd.backend.Util; | |
28 import dmd.backend.BC; | |
29 | |
187
b0d41ff5e0df
Added expandability scheme outlined in http://www.dsource.org/forums/viewtopic.php?t=5659&sid=6f2150ff5b0bffcd47512a6a7608d218
Abscissa
parents:
178
diff
changeset
|
30 import dmd.DDMDExtensions; |
b0d41ff5e0df
Added expandability scheme outlined in http://www.dsource.org/forums/viewtopic.php?t=5659&sid=6f2150ff5b0bffcd47512a6a7608d218
Abscissa
parents:
178
diff
changeset
|
31 |
0 | 32 class CaseStatement : Statement |
33 { | |
187
b0d41ff5e0df
Added expandability scheme outlined in http://www.dsource.org/forums/viewtopic.php?t=5659&sid=6f2150ff5b0bffcd47512a6a7608d218
Abscissa
parents:
178
diff
changeset
|
34 mixin insertMemberExtension!(typeof(this)); |
b0d41ff5e0df
Added expandability scheme outlined in http://www.dsource.org/forums/viewtopic.php?t=5659&sid=6f2150ff5b0bffcd47512a6a7608d218
Abscissa
parents:
178
diff
changeset
|
35 |
0 | 36 Expression exp; |
37 Statement statement; | |
38 | |
39 int index = 0; // which case it is (since we sort this) | |
40 block* cblock = null; // back end: label for the block | |
41 | |
42 this(Loc loc, Expression exp, Statement s) | |
43 { | |
178 | 44 register(); |
45 | |
0 | 46 super(loc); |
47 | |
48 this.exp = exp; | |
49 this.statement = s; | |
50 } | |
51 | |
72 | 52 override Statement syntaxCopy() |
0 | 53 { |
53 | 54 CaseStatement s = new CaseStatement(loc, exp.syntaxCopy(), statement.syntaxCopy()); |
55 return s; | |
0 | 56 } |
57 | |
72 | 58 override Statement semantic(Scope sc) |
0 | 59 { |
60 SwitchStatement sw = sc.sw; | |
61 | |
62 //printf("CaseStatement.semantic() %s\n", toChars()); | |
63 exp = exp.semantic(sc); | |
64 if (sw) | |
65 { | |
66 exp = exp.implicitCastTo(sc, sw.condition.type); | |
67 exp = exp.optimize(WANTvalue | WANTinterpret); | |
68 | |
69 /* This is where variables are allowed as case expressions. | |
70 */ | |
71 if (exp.op == TOKvar) | |
72 { VarExp ve = cast(VarExp)exp; | |
73 VarDeclaration v = ve.var.isVarDeclaration(); | |
74 Type t = exp.type.toBasetype(); | |
75 if (v && (t.isintegral() || t.ty == Tclass)) | |
76 { | |
77 /* Flag that we need to do special code generation | |
78 * for this, i.e. generate a sequence of if-then-else | |
79 */ | |
80 sw.hasVars = 1; | |
81 if (sw.isFinal) | |
82 error("case variables not allowed in final switch statements"); | |
83 goto L1; | |
84 } | |
85 } | |
86 | |
87 if (exp.op != TOKstring && exp.op != TOKint64) | |
88 { | |
89 error("case must be a string or an integral constant, not %s", exp.toChars()); | |
90 exp = new IntegerExp(0); | |
91 } | |
92 | |
93 L1: | |
94 for (int i = 0; i < sw.cases.dim; i++) | |
95 { | |
96 CaseStatement cs = cast(CaseStatement)sw.cases.data[i]; | |
97 | |
98 //printf("comparing '%s' with '%s'\n", exp.toChars(), cs.exp.toChars()); | |
99 if (cs.exp.equals(exp)) | |
100 { | |
101 error("duplicate case %s in switch statement", exp.toChars()); | |
102 break; | |
103 } | |
104 } | |
105 | |
106 sw.cases.push(cast(void*)this); | |
107 | |
108 // Resolve any goto case's with no exp to this case statement | |
109 for (int i = 0; i < sw.gotoCases.dim; i++) | |
110 { | |
111 GotoCaseStatement gcs = cast(GotoCaseStatement)sw.gotoCases.data[i]; | |
112 | |
113 if (!gcs.exp) | |
114 { | |
115 gcs.cs = this; | |
116 sw.gotoCases.remove(i); // remove from array | |
117 } | |
118 } | |
119 | |
120 if (sc.sw.tf !is sc.tf) | |
121 error("switch and case are in different finally blocks"); | |
122 } | |
123 else | |
124 error("case not in switch statement"); | |
125 statement = statement.semantic(sc); | |
126 return this; | |
127 } | |
128 | |
72 | 129 override int opCmp(Object obj) |
0 | 130 { |
68
ee3a9f34dc48
final bits of codegen implementation to compile Phobos
korDen
parents:
63
diff
changeset
|
131 // Sort cases so we can do an efficient lookup |
ee3a9f34dc48
final bits of codegen implementation to compile Phobos
korDen
parents:
63
diff
changeset
|
132 CaseStatement cs2 = cast(CaseStatement)obj; |
ee3a9f34dc48
final bits of codegen implementation to compile Phobos
korDen
parents:
63
diff
changeset
|
133 |
ee3a9f34dc48
final bits of codegen implementation to compile Phobos
korDen
parents:
63
diff
changeset
|
134 return exp.opCmp(cs2.exp); |
0 | 135 } |
136 | |
72 | 137 override bool usesEH() |
0 | 138 { |
139 assert(false); | |
140 } | |
141 | |
72 | 142 override BE blockExit() |
0 | 143 { |
144 return statement.blockExit(); | |
145 } | |
146 | |
72 | 147 override bool comeFrom() |
0 | 148 { |
149 return true; | |
150 } | |
151 | |
72 | 152 override Expression interpret(InterState istate) |
0 | 153 { |
163 | 154 version (LOG) { |
155 printf("CaseStatement.interpret(%s) this = %p\n", exp.toChars(), this); | |
156 } | |
157 if (istate.start is this) | |
158 istate.start = null; | |
159 if (statement) | |
160 return statement.interpret(istate); | |
161 else | |
162 return null; | |
0 | 163 } |
164 | |
72 | 165 override void toCBuffer(OutBuffer buf, HdrGenState* hgs) |
0 | 166 { |
174 | 167 buf.writestring("case "); |
168 exp.toCBuffer(buf, hgs); | |
169 buf.writebyte(':'); | |
170 buf.writenl(); | |
171 statement.toCBuffer(buf, hgs); | |
0 | 172 } |
173 | |
72 | 174 override Statement inlineScan(InlineScanState* iss) |
0 | 175 { |
176 //printf("CaseStatement.inlineScan()\n"); | |
177 exp = exp.inlineScan(iss); | |
178 if (statement) | |
179 statement = statement.inlineScan(iss); | |
180 return this; | |
181 } | |
182 | |
72 | 183 override void toIR(IRState *irs) |
0 | 184 { |
185 Blockx* blx = irs.blx; | |
186 block* bcase = blx.curblock; | |
187 if (!cblock) | |
188 cblock = block_calloc(blx); | |
189 block_next(blx,BCgoto,cblock); | |
190 block* bsw = irs.getSwitchBlock(); | |
191 if (bsw.BC == BCswitch) | |
192 list_append(&bsw.Bsucc,cblock); // second entry in pair | |
193 list_append(&bcase.Bsucc,cblock); | |
194 if (blx.tryblock != bsw.Btry) | |
195 error("case cannot be in different try block level from switch"); | |
196 incUsage(irs, loc); | |
197 if (statement) | |
198 statement.toIR(irs); | |
199 } | |
72 | 200 } |