1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
//! How control leaves a statement.
//!
//! The parser works this out for each function body once it has read
//! it, to warn about code that cannot be reached and about functions
//! that return a value on some paths and not on others. The code
//! generator asks again for a `switch` and a `finally`, to leave out
//! jumps that could never be taken.
use crate::ast::{LabelSym, Stmt, StmtKind, flow};
/// What working out the control flow needs to know, and what it finds.
pub(crate) struct Flow<'a> {
/// Whether to collect warnings.
pub warn: bool,
/// The source is being read only to learn what it defines.
pub syntax_only: bool,
/// The labels of the function the statement is in.
pub labels: &'a [LabelSym],
/// The labels that a `goto`, `break`, or `continue` naming a label
/// is recorded as a use of. While a function is being read, they
/// are its own. Afterward the reference still has the labels of the
/// last function it read, and it is those that get marked, whatever
/// function the statement is in.
pub mark: &'a [LabelSym],
/// The warnings: where, and what.
pub warnings: Vec<(usize, u32, &'static str)>,
}
impl Flow<'_> {
fn mark(&self, name: &[u8], flag: u32) {
if let Some(label) = self.mark.iter().find(|label| label.name == name) {
label.flags.set(label.flags.get() | flag);
}
}
fn part(&mut self, part: &Option<Box<Stmt>>) -> u32 {
match part {
Some(part) => self.control_flow(part),
None => flow::NEXT,
}
}
/// The ways a statement can be left.
pub(crate) fn control_flow(&mut self, stmt: &Stmt) -> u32 {
match &stmt.kind {
StmtKind::Comp { stmts, .. } => {
let mut flags = flow::NEXT;
let mut index = 0;
while index < stmts.len() {
let flags1 = self.control_flow(&stmts[index]);
flags |= flags1
& (flow::THROW
| flow::RET_VOID
| flow::RET_VAL
| flow::GOTO
| flow::BREAK
| flow::CONT);
if flags1 & flow::NEXT == 0 {
// Nothing after this can be reached, unless
// something further on has a label.
flags &= !flow::NEXT;
if let Some(next) = stmts.get(index + 1) {
if self.warn && !next.has_code_label() && !self.syntax_only {
self.warnings
.push((next.file, next.line, "unreachable statement"));
}
let mut found = index + 1;
while found < stmts.len() && !stmts[found].has_code_label() {
found += 1;
}
if found < stmts.len() {
flags |= flow::NEXT;
}
index = found;
continue;
}
}
index += 1;
}
flags
}
StmtKind::If {
cond,
then_part,
else_part,
} => {
if let Some(value) = cond.as_const() {
let part = if value.is_true() {
then_part
} else {
else_part
};
return self.part(part);
}
let flags1 = self.part(then_part);
let flags2 = self.part(else_part);
flags1 | flags2
}
StmtKind::Return(expr) => {
if expr.is_some() {
flow::RET_VAL
} else {
flow::RET_VOID
}
}
StmtKind::Foreach { body, .. } => match body {
Some(body) => (self.control_flow(body) | flow::NEXT) & !(flow::BREAK | flow::CONT),
None => flow::NEXT,
},
StmtKind::For {
init, cond, body, ..
} => {
let cond_const = cond.as_ref().and_then(|cond| cond.as_const());
if let (Some(value), Some(body)) = (cond_const, body)
&& !value.is_true()
&& !body.has_code_label()
&& !self.syntax_only
{
if self.warn && !value.ctc {
self.warnings.push((
stmt.file,
stmt.line,
"\"for\" condition is always false - body and reinitializer are unreachable",
));
}
return flow::NEXT;
}
let mut flags = match body {
Some(body) => self.control_flow(body),
None => 0,
};
flags &= !flow::NEXT;
if flags & flow::BREAK != 0 {
flags |= flow::NEXT;
}
if cond.is_some() && cond_const.is_none_or(|value| !value.is_true()) {
flags |= flow::NEXT;
}
// A loop over a collection or a range ends.
let mut for_ins = Vec::new();
if let Some(init) = init {
init.for_ins(&mut for_ins);
}
if !for_ins.is_empty() {
flags |= flow::NEXT;
}
flags & !(flow::BREAK | flow::CONT)
}
StmtKind::While { cond, body, .. } => {
let cond_const = cond.as_const();
if let (Some(value), Some(body)) = (cond_const, body) {
// The reference only takes this way out when it is
// reading for symbols, so that is when it warns.
if !value.is_true() && !body.has_code_label() && self.syntax_only {
if self.warn && !value.ctc {
self.warnings.push((
stmt.file,
stmt.line,
"\"while\" condition is always false - loop body is unreachable",
));
}
return flow::NEXT;
}
}
let mut flags = match body {
Some(body) => self.control_flow(body),
None => 0,
};
flags &= !flow::NEXT;
if flags & flow::BREAK != 0 {
flags |= flow::NEXT;
}
if cond_const.is_none_or(|value| !value.is_true()) {
flags |= flow::NEXT;
}
flags & !(flow::BREAK | flow::CONT)
}
StmtKind::DoWhile { body, cond, .. } => {
let mut flags = match body {
Some(body) => self.control_flow(body),
None => 0,
};
if flags & flow::NEXT != 0 {
flags &= !flow::NEXT;
if cond.as_const().is_none_or(|value| !value.is_true()) {
flags |= flow::NEXT;
}
}
if flags & flow::BREAK != 0 {
flags |= flow::NEXT;
}
flags & !(flow::BREAK | flow::CONT)
}
StmtKind::Break(label) => match label {
None => flow::BREAK,
Some(name) => {
self.mark(name, flow::BREAK);
flow::GOTO
}
},
StmtKind::Continue(label) => match label {
None => flow::CONT,
Some(name) => {
self.mark(name, flow::CONT);
flow::GOTO
}
},
StmtKind::Switch {
body, has_default, ..
} => {
let mut flags = self.control_flow(body);
if flags & flow::BREAK != 0 {
flags |= flow::NEXT;
flags &= !flow::BREAK;
}
// Without a `default`, some value probably matches no
// case.
if !has_default {
flags |= flow::NEXT;
}
flags
}
StmtKind::Case { stm, .. } | StmtKind::Default(stm) => self.part(stm),
StmtKind::Label { label, stm, .. } => {
let mut flags = self.part(stm);
// A `break` that names the label goes to what follows.
let used = label.map_or(0, |label| self.labels[label].flags.get());
if used & flow::BREAK != 0 {
flags |= flow::NEXT;
}
flags
}
StmtKind::Goto(name) => {
self.mark(name, flow::GOTO);
flow::GOTO
}
StmtKind::Try {
body,
catches,
finally,
..
} => {
let mut flags = self.part(body);
// Any exception could arrive in the protected code, so
// every `catch` can be entered.
for catch in catches {
flags |= self.part(&catch.body);
}
let mut fin_flags = match finally {
Some(finally) => self.part(&finally.body),
None => flow::NEXT,
};
// Everything leaves through the `finally`; if that
// never finishes, nothing else gets to do what it was
// going to.
if fin_flags & flow::NEXT == 0 {
flags &= !(flow::NEXT
| flow::BREAK
| flow::CONT
| flow::RET_VAL
| flow::RET_VOID
| flow::GOTO);
}
fin_flags &= !flow::NEXT;
flags | fin_flags
}
StmtKind::Throw(_) => flow::THROW,
StmtKind::ImplicitCtor(_) | StmtKind::MmInit => flow::NEXT,
StmtKind::MmStub(_) => flow::RET_VAL,
StmtKind::StaticPropInit(..) => flow::RET_VAL,
StmtKind::Null | StmtKind::Expr(_) => flow::NEXT,
}
}
}