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