annotate dmd/CompoundStatement.d @ 27:0613413fa94c

Accidentally removed line restored.
author korDen
date Tue, 13 Apr 2010 15:34:41 +0400
parents 3f834bed4f13
children cab4c37afb89
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1 module dmd.CompoundStatement;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
2
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
3 import dmd.Loc;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
4 import dmd.Statement;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
5 import dmd.Array;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
6 import dmd.TryCatchStatement;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
7 import dmd.TryFinallyStatement;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
8 import dmd.Catch;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
9 import dmd.ScopeStatement;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
10 import dmd.Identifier;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
11 import dmd.Lexer;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
12 import dmd.ThrowStatement;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
13 import dmd.IdentifierExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
14 import dmd.Scope;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
15 import dmd.OutBuffer;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
16 import dmd.HdrGenState;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
17 import dmd.ArrayTypes;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
18 import dmd.ReturnStatement;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
19 import dmd.Expression;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
20 import dmd.InterState;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
21 import dmd.InlineDoState;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
22 import dmd.InlineCostState;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
23 import dmd.InlineScanState;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
24 import dmd.IfStatement;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
25 import dmd.IRState;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
26 import dmd.BE;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
27 import dmd.Util;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
28
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
29 class CompoundStatement : Statement
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
30 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
31 Statements statements;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
32
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
33 this(Loc loc, Statements s)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
34 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
35 super(loc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
36 statements = s;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
37 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
38
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
39 this(Loc loc, Statement s1, Statement s2)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
40 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
41 super(loc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
42
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
43 statements = new Statements();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
44 statements.reserve(2);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
45 statements.push(cast(void*)s1);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
46 statements.push(cast(void*)s2);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
47 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
48
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
49 Statement syntaxCopy()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
50 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
51 Statements a = new Statements();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
52 a.setDim(statements.dim);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
53
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
54 for (size_t i = 0; i < statements.dim; i++)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
55 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
56 Statement s = cast(Statement)statements.data[i];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
57 if (s)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
58 s = s.syntaxCopy();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
59 a.data[i] = cast(void*)s;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
60 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
61
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
62 return new CompoundStatement(loc, a);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
63 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
64
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
65 void toCBuffer(OutBuffer buf, HdrGenState* hgs)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
66 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
67 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
68 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
69
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
70 static int indent = 0;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
71 static int depth = 0;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
72
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
73 Statement semantic(Scope sc)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
74 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
75 Statement s;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
76
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
77 //printf("CompoundStatement.semantic(this = %p, sc = %p)\n", this, sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
78
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
79 for (size_t i = 0; i < statements.dim; )
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
80 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
81 s = cast(Statement) statements.data[i];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
82 if (s)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
83 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
84 Statements a = s.flatten(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
85
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
86 if (a)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
87 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
88 statements.remove(i);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
89 statements.insert(i, a);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
90 continue;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
91 }
27
0613413fa94c Accidentally removed line restored.
korDen
parents: 25
diff changeset
92
0613413fa94c Accidentally removed line restored.
korDen
parents: 25
diff changeset
93 s = s.semantic(sc);
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
94
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
95 statements.data[i] = cast(void*)s;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
96 if (s)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
97 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
98 Statement sentry;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
99 Statement sexception;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
100 Statement sfinally;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
101
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
102 s.scopeCode(sc, &sentry, &sexception, &sfinally);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
103 if (sentry)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
104 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
105 sentry = sentry.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
106 if (s.isDeclarationStatement())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
107 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
108 statements.insert(i, cast(void*)sentry);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
109 i++;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
110 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
111 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
112 statements.data[i] = cast(void*)sentry;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
113 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
114 if (sexception)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
115 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
116 if (i + 1 == statements.dim && !sfinally)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
117 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
118 static if (true) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
119 sexception = sexception.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
120 } else {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
121 statements.push(sexception);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
122 if (sfinally)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
123 // Assume sexception does not throw
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
124 statements.push(sfinally);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
125 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
126 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
127 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
128 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
129 /* Rewrite:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
130 * s; s1; s2;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
131 * As:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
132 * s;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
133 * try { s1; s2; }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
134 * catch (Object __o)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
135 * { sexception; throw __o; }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
136 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
137 Statement body_;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
138 Statements aa = new Statements();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
139
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
140 for (int j = i + 1; j < statements.dim; j++)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
141 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
142 aa.push(statements.data[j]);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
143 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
144 body_ = new CompoundStatement(Loc(0), aa);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
145 body_ = new ScopeStatement(Loc(0), body_);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
146
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
147 Identifier id = Lexer.uniqueId("__o");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
148
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
149 Statement handler = new ThrowStatement(Loc(0), new IdentifierExp(Loc(0), id));
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
150 handler = new CompoundStatement(Loc(0), sexception, handler);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
151
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
152 Array catches = new Array();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
153 Catch ctch = new Catch(Loc(0), null, id, handler);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
154 catches.push(cast(void*)ctch);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
155 s = new TryCatchStatement(Loc(0), body_, catches);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
156
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
157 if (sfinally)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
158 s = new TryFinallyStatement(Loc(0), s, sfinally);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
159 s = s.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
160 statements.setDim(i + 1);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
161 statements.push(cast(void*)s);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
162 break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
163 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
164 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
165 else if (sfinally)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
166 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
167 if (0 && i + 1 == statements.dim)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
168 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
169 statements.push(cast(void*)sfinally);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
170 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
171 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
172 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
173 /* Rewrite:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
174 * s; s1; s2;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
175 * As:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
176 * s; try { s1; s2; } finally { sfinally; }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
177 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
178 Statement body_;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
179 Statements aa = new Statements();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
180
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
181 for (int j = i + 1; j < statements.dim; j++)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
182 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
183 aa.push(statements.data[j]);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
184 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
185 body_ = new CompoundStatement(Loc(0), aa);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
186 s = new TryFinallyStatement(Loc(0), body_, sfinally);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
187 s = s.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
188 statements.setDim(i + 1);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
189 statements.push(cast(void*)s);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
190 break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
191 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
192 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
193 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
194 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
195 i++;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
196 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
197 if (statements.dim == 1)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
198 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
199 return cast(Statement)statements.data[0];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
200 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
201 return this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
202 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
203
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
204 bool usesEH()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
205 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
206 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
207 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
208
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
209 BE blockExit()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
210 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
211 //printf("CompoundStatement::blockExit(%p) %d\n", this, statements->dim);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
212 BE result = BE.BEfallthru;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
213 for (size_t i = 0; i < statements.dim; i++)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
214 { Statement s = cast(Statement)statements.data[i];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
215 if (s)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
216 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
217 //printf("result = x%x\n", result);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
218 //printf("%s\n", s->toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
219 if (!(result & BE.BEfallthru) && !s.comeFrom())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
220 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
221 if (s.blockExit() != BE.BEhalt)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
222 s.warning("statement is not reachable");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
223 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
224
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
225 result &= ~BE.BEfallthru;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
226 result |= s.blockExit();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
227 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
228 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
229
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
230 return result;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
231 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
232
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
233 bool comeFrom()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
234 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
235 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
236 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
237
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
238 bool isEmpty()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
239 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
240 for (int i = 0; i < statements.dim; i++)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
241 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
242 Statement s = cast(Statement) statements.data[i];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
243 if (s && !s.isEmpty())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
244 return false;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
245 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
246 return true;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
247 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
248
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
249 Statements flatten(Scope sc)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
250 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
251 return statements;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
252 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
253
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
254 ReturnStatement isReturnStatement()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
255 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
256 ReturnStatement rs = null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
257
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
258 for (int i = 0; i < statements.dim; i++)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
259 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
260 Statement s = cast(Statement) statements.data[i];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
261 if (s)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
262 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
263 rs = s.isReturnStatement();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
264 if (rs)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
265 break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
266 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
267 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
268 return rs;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
269 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
270
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
271 Expression interpret(InterState* istate)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
272 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
273 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
274 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
275
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
276 int inlineCost(InlineCostState* ics)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
277 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
278 int cost = 0;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
279
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
280 for (size_t i = 0; i < statements.dim; i++)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
281 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
282 Statement s = cast(Statement)statements.data[i];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
283 if (s)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
284 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
285 cost += s.inlineCost(ics);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
286 if (cost >= COST_MAX)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
287 break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
288 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
289 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
290
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
291 return cost;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
292 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
293
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
294 Expression doInline(InlineDoState ids)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
295 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
296 Expression e = null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
297
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
298 //printf("CompoundStatement.doInline() %d\n", statements.dim);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
299 for (size_t i = 0; i < statements.dim; i++)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
300 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
301 Statement s = cast(Statement)statements.data[i];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
302 if (s)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
303 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
304 Expression e2 = s.doInline(ids);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
305 e = Expression.combine(e, e2);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
306 if (s.isReturnStatement())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
307 break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
308
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
309 /* Check for:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
310 * if (condition)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
311 * return exp1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
312 * else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
313 * return exp2;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
314 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
315 IfStatement ifs = s.isIfStatement();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
316 if (ifs && ifs.elsebody && ifs.ifbody &&
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
317 ifs.ifbody.isReturnStatement() &&
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
318 ifs.elsebody.isReturnStatement()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
319 )
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
320 break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
321 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
322 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
323 return e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
324 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
325
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
326 Statement inlineScan(InlineScanState* iss)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
327 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
328 for (size_t i = 0; i < statements.dim; i++)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
329 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
330 Statement s = cast(Statement) statements.data[i];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
331 if (s)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
332 statements.data[i] = cast(void*)s.inlineScan(iss);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
333 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
334
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
335 return this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
336 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
337
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
338 void toIR(IRState* irs)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
339 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
340 if (statements)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
341 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
342 size_t dim = statements.dim;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
343 for (size_t i = 0 ; i < dim ; i++)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
344 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
345 Statement s = cast(Statement)statements.data[i];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
346 if (s !is null)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
347 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
348 s.toIR(irs);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
349 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
350 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
351 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
352 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
353
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
354 CompoundStatement isCompoundStatement() { return this; }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
355 }