annotate dmd/CaseStatement.d @ 0:10317f0c89a5

Initial commit
author korDen
date Sat, 24 Oct 2009 08:42:06 +0400
parents
children a8b50ff7f201
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1 module dmd.CaseStatement;
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.Statement;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
6 import dmd.Scope;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
7 import dmd.Loc;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
8 import dmd.IRState;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
9 import dmd.InlineScanState;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
10 import dmd.HdrGenState;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
11 import dmd.OutBuffer;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
12 import dmd.InterState;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
13 import dmd.BE;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
14 import dmd.SwitchStatement;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
15 import dmd.WANT;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
16 import dmd.TOK;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
17 import dmd.VarExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
18 import dmd.VarDeclaration;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
19 import dmd.Type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
20 import dmd.TY;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
21 import dmd.IntegerExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
22 import dmd.GotoCaseStatement;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
23
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
24 import dmd.backend.block;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
25 import dmd.backend.Blockx;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
26 import dmd.backend.Util;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
27 import dmd.backend.BC;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
28
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
29 class CaseStatement : Statement
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
30 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
31 Expression exp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
32 Statement statement;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
33
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
34 int index = 0; // which case it is (since we sort this)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
35 block* cblock = null; // back end: label for the block
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
36
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
37 this(Loc loc, Expression exp, Statement s)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
38 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
39 super(loc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
40
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
41 this.exp = exp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
42 this.statement = s;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
43 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
44
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
45 Statement syntaxCopy()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
46 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
47 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
48 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
49
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
50 Statement semantic(Scope sc)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
51 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
52 SwitchStatement sw = sc.sw;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
53
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
54 //printf("CaseStatement.semantic() %s\n", toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
55 exp = exp.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
56 if (sw)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
57 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
58 exp = exp.implicitCastTo(sc, sw.condition.type);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
59 exp = exp.optimize(WANTvalue | WANTinterpret);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
60
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
61 /* This is where variables are allowed as case expressions.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
62 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
63 if (exp.op == TOKvar)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
64 { VarExp ve = cast(VarExp)exp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
65 VarDeclaration v = ve.var.isVarDeclaration();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
66 Type t = exp.type.toBasetype();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
67 if (v && (t.isintegral() || t.ty == Tclass))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
68 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
69 /* Flag that we need to do special code generation
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
70 * for this, i.e. generate a sequence of if-then-else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
71 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
72 sw.hasVars = 1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
73 if (sw.isFinal)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
74 error("case variables not allowed in final switch statements");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
75 goto L1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
76 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
77 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
78
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
79 if (exp.op != TOKstring && exp.op != TOKint64)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
80 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
81 error("case must be a string or an integral constant, not %s", exp.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
82 exp = new IntegerExp(0);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
83 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
84
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
85 L1:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
86 for (int i = 0; i < sw.cases.dim; i++)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
87 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
88 CaseStatement cs = cast(CaseStatement)sw.cases.data[i];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
89
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
90 //printf("comparing '%s' with '%s'\n", exp.toChars(), cs.exp.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
91 if (cs.exp.equals(exp))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
92 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
93 error("duplicate case %s in switch statement", exp.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
94 break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
95 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
96 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
97
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
98 sw.cases.push(cast(void*)this);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
99
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
100 // Resolve any goto case's with no exp to this case statement
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
101 for (int i = 0; i < sw.gotoCases.dim; i++)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
102 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
103 GotoCaseStatement gcs = cast(GotoCaseStatement)sw.gotoCases.data[i];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
104
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
105 if (!gcs.exp)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
106 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
107 gcs.cs = this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
108 sw.gotoCases.remove(i); // remove from array
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
109 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
110 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
111
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
112 if (sc.sw.tf !is sc.tf)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
113 error("switch and case are in different finally blocks");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
114 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
115 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
116 error("case not in switch statement");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
117 statement = statement.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
118 return this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
119 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
120
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
121 int compare(Object obj)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
122 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
123 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
124 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
125
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
126 bool usesEH()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
127 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
128 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
129 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
130
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
131 BE blockExit()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
132 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
133 return statement.blockExit();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
134 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
135
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
136 bool comeFrom()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
137 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
138 return true;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
139 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
140
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
141 Expression interpret(InterState *istate)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
142 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
143 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
144 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
145
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
146 void toCBuffer(OutBuffer buf, HdrGenState* hgs)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
147 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
148 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
149 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
150
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
151 Statement inlineScan(InlineScanState* iss)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
152 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
153 //printf("CaseStatement.inlineScan()\n");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
154 exp = exp.inlineScan(iss);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
155 if (statement)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
156 statement = statement.inlineScan(iss);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
157 return this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
158 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
159
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
160 void toIR(IRState *irs)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
161 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
162 Blockx* blx = irs.blx;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
163 block* bcase = blx.curblock;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
164 if (!cblock)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
165 cblock = block_calloc(blx);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
166 block_next(blx,BCgoto,cblock);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
167 block* bsw = irs.getSwitchBlock();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
168 if (bsw.BC == BCswitch)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
169 list_append(&bsw.Bsucc,cblock); // second entry in pair
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
170 list_append(&bcase.Bsucc,cblock);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
171 if (blx.tryblock != bsw.Btry)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
172 error("case cannot be in different try block level from switch");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
173 incUsage(irs, loc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
174 if (statement)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
175 statement.toIR(irs);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
176 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
177 }