annotate dmd/IfStatement.d @ 174:af724d3510d7

lot os toCBuffer methods implemented moved shared Type.* stuff into Global
author korDen
date Sun, 10 Oct 2010 03:47:23 +0400
parents 14feb7ae01a6
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.IfStatement;
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;
130
60bb0fe4563e dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 114
diff changeset
5 import dmd.Parameter;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
6 import dmd.Loc;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
7 import dmd.Expression;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
8 import dmd.VarDeclaration;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
9 import dmd.Scope;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
10 import dmd.InterState;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
11 import dmd.OutBuffer;
154
14feb7ae01a6 * changed the build system to build a release version if the debug one compiles
trass3r
parents: 130
diff changeset
12 import dmd.GlobalExpressions;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
13 import dmd.HdrGenState;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
14 import dmd.InlineCostState;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
15 import dmd.InlineDoState;
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.BE;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
19 import dmd.WANT;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
20 import dmd.ScopeDsymbol;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
21 import dmd.Type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
22 import dmd.CondExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
23 import dmd.AndAndExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
24 import dmd.OrOrExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
25 import dmd.AssignExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
26 import dmd.VarExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
27
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
28 import dmd.backend.elem;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
29 import dmd.backend.Util;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
30 import dmd.backend.block;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
31 import dmd.backend.Blockx;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
32 import dmd.backend.BC;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
33
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
34 class IfStatement : Statement
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
35 {
130
60bb0fe4563e dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 114
diff changeset
36 Parameter arg;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
37 Expression condition;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
38 Statement ifbody;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
39 Statement elsebody;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
40
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
41 VarDeclaration match; // for MatchExpression results
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
42
130
60bb0fe4563e dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 114
diff changeset
43 this(Loc loc, Parameter arg, Expression condition, Statement ifbody, Statement elsebody)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
44 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
45 super(loc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
46 this.arg = arg;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
47 this.condition = condition;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
48 this.ifbody = ifbody;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
49 this.elsebody = elsebody;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
50 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
51
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 63
diff changeset
52 override Statement syntaxCopy()
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
53 {
51
b7d29f613539 StaticAssertStatement.syntaxCopy
korDen
parents: 0
diff changeset
54 Statement i = null;
b7d29f613539 StaticAssertStatement.syntaxCopy
korDen
parents: 0
diff changeset
55 if (ifbody)
b7d29f613539 StaticAssertStatement.syntaxCopy
korDen
parents: 0
diff changeset
56 i = ifbody.syntaxCopy();
b7d29f613539 StaticAssertStatement.syntaxCopy
korDen
parents: 0
diff changeset
57
b7d29f613539 StaticAssertStatement.syntaxCopy
korDen
parents: 0
diff changeset
58 Statement e = null;
b7d29f613539 StaticAssertStatement.syntaxCopy
korDen
parents: 0
diff changeset
59 if (elsebody)
b7d29f613539 StaticAssertStatement.syntaxCopy
korDen
parents: 0
diff changeset
60 e = elsebody.syntaxCopy();
b7d29f613539 StaticAssertStatement.syntaxCopy
korDen
parents: 0
diff changeset
61
130
60bb0fe4563e dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 114
diff changeset
62 Parameter a = arg ? arg.syntaxCopy() : null;
51
b7d29f613539 StaticAssertStatement.syntaxCopy
korDen
parents: 0
diff changeset
63 IfStatement s = new IfStatement(loc, a, condition.syntaxCopy(), i, e);
b7d29f613539 StaticAssertStatement.syntaxCopy
korDen
parents: 0
diff changeset
64 return s;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
65 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
66
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 63
diff changeset
67 override Statement semantic(Scope sc)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
68 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
69 condition = condition.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
70 condition = resolveProperties(sc, condition);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
71 condition = condition.checkToBoolean();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
72
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
73 // If we can short-circuit evaluate the if statement, don't do the
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
74 // semantic analysis of the skipped code.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
75 // This feature allows a limited form of conditional compilation.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
76 condition = condition.optimize(WANT.WANTflags);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
77
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
78 // Evaluate at runtime
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
79 uint cs0 = sc.callSuper;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
80 uint cs1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
81
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
82 Scope scd;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
83 if (arg)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
84 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
85 /* Declare arg, which we will set to be the
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
86 * result of condition.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
87 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
88 ScopeDsymbol sym = new ScopeDsymbol();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
89 sym.parent = sc.scopesym;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
90 scd = sc.push(sym);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
91
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
92 Type t = arg.type ? arg.type : condition.type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
93 match = new VarDeclaration(loc, t, arg.ident, null);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
94 match.noauto = true;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
95 match.semantic(scd);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
96 if (!scd.insert(match))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
97 assert(0);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
98
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
99 match.parent = sc.func;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
100
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
101 /* Generate:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
102 * (arg = condition)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
103 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
104 VarExp v = new VarExp(Loc(0), match);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
105 condition = new AssignExp(loc, v, condition);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
106 condition = condition.semantic(scd);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
107 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
108 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
109 scd = sc.push();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
110
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
111 ifbody = ifbody.semantic(scd);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
112 scd.pop();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
113
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
114 cs1 = sc.callSuper;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
115 sc.callSuper = cs0;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
116 if (elsebody)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
117 elsebody = elsebody.semanticScope(sc, null, null);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
118
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
119 sc.mergeCallSuper(loc, cs1);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
120
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
121 return this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
122 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
123
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 63
diff changeset
124 override Expression interpret(InterState istate)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
125 {
154
14feb7ae01a6 * changed the build system to build a release version if the debug one compiles
trass3r
parents: 130
diff changeset
126 version(LOG)
14feb7ae01a6 * changed the build system to build a release version if the debug one compiles
trass3r
parents: 130
diff changeset
127 writef("IfStatement::interpret(%s)\n", condition.toChars());
14feb7ae01a6 * changed the build system to build a release version if the debug one compiles
trass3r
parents: 130
diff changeset
128
14feb7ae01a6 * changed the build system to build a release version if the debug one compiles
trass3r
parents: 130
diff changeset
129 if (istate.start is this)
14feb7ae01a6 * changed the build system to build a release version if the debug one compiles
trass3r
parents: 130
diff changeset
130 istate.start = null;
14feb7ae01a6 * changed the build system to build a release version if the debug one compiles
trass3r
parents: 130
diff changeset
131 if (istate.start)
14feb7ae01a6 * changed the build system to build a release version if the debug one compiles
trass3r
parents: 130
diff changeset
132 {
14feb7ae01a6 * changed the build system to build a release version if the debug one compiles
trass3r
parents: 130
diff changeset
133 Expression e = null;
14feb7ae01a6 * changed the build system to build a release version if the debug one compiles
trass3r
parents: 130
diff changeset
134 if (ifbody)
14feb7ae01a6 * changed the build system to build a release version if the debug one compiles
trass3r
parents: 130
diff changeset
135 e = ifbody.interpret(istate);
14feb7ae01a6 * changed the build system to build a release version if the debug one compiles
trass3r
parents: 130
diff changeset
136 if (istate.start && elsebody)
14feb7ae01a6 * changed the build system to build a release version if the debug one compiles
trass3r
parents: 130
diff changeset
137 e = elsebody.interpret(istate);
14feb7ae01a6 * changed the build system to build a release version if the debug one compiles
trass3r
parents: 130
diff changeset
138 return e;
14feb7ae01a6 * changed the build system to build a release version if the debug one compiles
trass3r
parents: 130
diff changeset
139 }
14feb7ae01a6 * changed the build system to build a release version if the debug one compiles
trass3r
parents: 130
diff changeset
140
14feb7ae01a6 * changed the build system to build a release version if the debug one compiles
trass3r
parents: 130
diff changeset
141 Expression e = condition.interpret(istate);
14feb7ae01a6 * changed the build system to build a release version if the debug one compiles
trass3r
parents: 130
diff changeset
142 assert(e);
14feb7ae01a6 * changed the build system to build a release version if the debug one compiles
trass3r
parents: 130
diff changeset
143 //if (e is EXP_CANT_INTERPRET) writef("cannot interpret\n");
14feb7ae01a6 * changed the build system to build a release version if the debug one compiles
trass3r
parents: 130
diff changeset
144 if (e !is EXP_CANT_INTERPRET)
14feb7ae01a6 * changed the build system to build a release version if the debug one compiles
trass3r
parents: 130
diff changeset
145 {
14feb7ae01a6 * changed the build system to build a release version if the debug one compiles
trass3r
parents: 130
diff changeset
146 if (e.isBool(true))
14feb7ae01a6 * changed the build system to build a release version if the debug one compiles
trass3r
parents: 130
diff changeset
147 e = ifbody ? ifbody.interpret(istate) : null;
14feb7ae01a6 * changed the build system to build a release version if the debug one compiles
trass3r
parents: 130
diff changeset
148 else if (e.isBool(false))
14feb7ae01a6 * changed the build system to build a release version if the debug one compiles
trass3r
parents: 130
diff changeset
149 e = elsebody ? elsebody.interpret(istate) : null;
14feb7ae01a6 * changed the build system to build a release version if the debug one compiles
trass3r
parents: 130
diff changeset
150 else
14feb7ae01a6 * changed the build system to build a release version if the debug one compiles
trass3r
parents: 130
diff changeset
151 {
14feb7ae01a6 * changed the build system to build a release version if the debug one compiles
trass3r
parents: 130
diff changeset
152 e = EXP_CANT_INTERPRET;
14feb7ae01a6 * changed the build system to build a release version if the debug one compiles
trass3r
parents: 130
diff changeset
153 }
14feb7ae01a6 * changed the build system to build a release version if the debug one compiles
trass3r
parents: 130
diff changeset
154 }
14feb7ae01a6 * changed the build system to build a release version if the debug one compiles
trass3r
parents: 130
diff changeset
155 return e;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
156 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
157
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 63
diff changeset
158 override void toCBuffer(OutBuffer buf, HdrGenState* hgs)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
159 {
174
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 154
diff changeset
160 buf.writestring("if (");
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 154
diff changeset
161 if (arg)
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 154
diff changeset
162 {
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 154
diff changeset
163 if (arg.type)
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 154
diff changeset
164 arg.type.toCBuffer(buf, arg.ident, hgs);
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 154
diff changeset
165 else
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 154
diff changeset
166 {
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 154
diff changeset
167 buf.writestring("auto ");
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 154
diff changeset
168 buf.writestring(arg.ident.toChars());
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 154
diff changeset
169 }
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 154
diff changeset
170 buf.writestring(" = ");
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 154
diff changeset
171 }
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 154
diff changeset
172 condition.toCBuffer(buf, hgs);
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 154
diff changeset
173 buf.writebyte(')');
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 154
diff changeset
174 buf.writenl();
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 154
diff changeset
175 ifbody.toCBuffer(buf, hgs);
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 154
diff changeset
176 if (elsebody)
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 154
diff changeset
177 {
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 154
diff changeset
178 buf.writestring("else");
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 154
diff changeset
179 buf.writenl();
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 154
diff changeset
180 elsebody.toCBuffer(buf, hgs);
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 154
diff changeset
181 }
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
182 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
183
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 63
diff changeset
184 override bool usesEH()
0
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 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
188
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 63
diff changeset
189 override BE blockExit()
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
190 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
191 //printf("IfStatement::blockExit(%p)\n", this);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
192
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
193 BE result = BE.BEnone;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
194 if (condition.canThrow())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
195 result |= BE.BEthrow;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
196 if (condition.isBool(true))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
197 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
198 if (ifbody)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
199 result |= ifbody.blockExit();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
200 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
201 result |= BE.BEfallthru;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
202 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
203 else if (condition.isBool(false))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
204 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
205 if (elsebody)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
206 result |= elsebody.blockExit();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
207 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
208 result |= BE.BEfallthru;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
209 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
210 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
211 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
212 if (ifbody)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
213 result |= ifbody.blockExit();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
214 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
215 result |= BE.BEfallthru;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
216
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
217 if (elsebody)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
218 result |= elsebody.blockExit();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
219 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
220 result |= BE.BEfallthru;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
221 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
222
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
223 //printf("IfStatement::blockExit(%p) = x%x\n", this, result);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
224 return result;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
225 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
226
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 63
diff changeset
227 override IfStatement isIfStatement() { return this; }
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
228
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 63
diff changeset
229 override int inlineCost(InlineCostState* ics)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
230 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
231 int cost;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
232
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
233 /* Can't declare variables inside ?: expressions, so
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
234 * we cannot inline if a variable is declared.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
235 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
236 if (arg)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
237 return COST_MAX;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
238
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
239 cost = condition.inlineCost(ics);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
240
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
241 /* Specifically allow:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
242 * if (condition)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
243 * return exp1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
244 * else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
245 * return exp2;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
246 * Otherwise, we can't handle return statements nested in if's.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
247 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
248
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
249 if (elsebody && ifbody &&
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
250 ifbody.isReturnStatement() &&
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
251 elsebody.isReturnStatement())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
252 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
253 cost += ifbody.inlineCost(ics);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
254 cost += elsebody.inlineCost(ics);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
255 //printf("cost = %d\n", cost);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
256 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
257 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
258 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
259 ics.nested += 1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
260 if (ifbody)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
261 cost += ifbody.inlineCost(ics);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
262 if (elsebody)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
263 cost += elsebody.inlineCost(ics);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
264 ics.nested -= 1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
265 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
266 return cost;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
267 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
268
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 63
diff changeset
269 override Expression doInline(InlineDoState ids)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
270 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
271 Expression econd;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
272 Expression e1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
273 Expression e2;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
274 Expression e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
275
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
276 assert(!arg);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
277 econd = condition.doInline(ids);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
278 assert(econd);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
279 if (ifbody)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
280 e1 = ifbody.doInline(ids);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
281 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
282 e1 = null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
283 if (elsebody)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
284 e2 = elsebody.doInline(ids);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
285 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
286 e2 = null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
287 if (e1 && e2)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
288 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
289 e = new CondExp(econd.loc, econd, e1, e2);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
290 e.type = e1.type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
291 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
292 else if (e1)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
293 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
294 e = new AndAndExp(econd.loc, econd, e1);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
295 e.type = Type.tvoid;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
296 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
297 else if (e2)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
298 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
299 e = new OrOrExp(econd.loc, econd, e2);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
300 e.type = Type.tvoid;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
301 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
302 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
303 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
304 e = econd;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
305 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
306 return e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
307 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
308
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 63
diff changeset
309 override Statement inlineScan(InlineScanState* iss)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
310 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
311 condition = condition.inlineScan(iss);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
312 if (ifbody)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
313 ifbody = ifbody.inlineScan(iss);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
314 if (elsebody)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
315 elsebody = elsebody.inlineScan(iss);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
316 return this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
317 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
318
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 63
diff changeset
319 override void toIR(IRState* irs)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
320 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
321 elem* e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
322 Blockx* blx = irs.blx;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
323
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
324 //printf("IfStatement::toIR('%s')\n", condition.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
325
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
326 IRState mystate = IRState(irs, this);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
327
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
328 // bexit is the block that gets control after this IfStatement is done
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
329 block* bexit = mystate.breakBlock ? mystate.breakBlock : block_calloc();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
330
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
331 incUsage(irs, loc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
332 static if (false) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
333 if (match)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
334 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
335 /* Generate:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
336 * if (match = RTLSYM_IFMATCH(string, pattern)) ...
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
337 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
338 assert(condition.op == TOK.TOKmatch);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
339 e = matchexp_toelem(cast(MatchExp)condition, &mystate, RTLSYM.RTLSYM_IFMATCH);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
340 Symbol *s = match.toSymbol();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
341 symbol_add(s);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
342 e = el_bin(OPeq, TYnptr, el_var(s), e);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
343 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
344 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
345 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
346 e = condition.toElem(&mystate);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
347 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
348 } else {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
349 e = condition.toElem(&mystate);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
350 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
351 block_appendexp(blx.curblock, e);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
352 block* bcond = blx.curblock;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
353 block_next(blx, BC.BCiftrue, null);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
354
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
355 list_append(&bcond.Bsucc, blx.curblock);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
356 if (ifbody)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
357 ifbody.toIR(&mystate);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
358
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
359 list_append(&blx.curblock.Bsucc, bexit);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
360
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
361 if (elsebody)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
362 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
363 block_next(blx, BC.BCgoto, null);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
364 list_append(&bcond.Bsucc, blx.curblock);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
365 elsebody.toIR(&mystate);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
366 list_append(&blx.curblock.Bsucc, bexit);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
367 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
368 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
369 list_append(&bcond.Bsucc, bexit);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
370
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
371 block_next(blx, BC.BCgoto, bexit);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
372 }
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 63
diff changeset
373 }