blob: 776067d49a218a1f01c21a9027f3c41c2af1da6c [file] [log] [blame]
Pythoner6aec34d82014-07-27 11:50:461// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
Niko Matsakis86b6e6e2013-05-10 17:10:352// file at the top-level directory of this distribution and at
3// http://rust-lang.org/COPYRIGHT.
4//
5// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8// option. This file may not be copied, modified, or distributed
9// except according to those terms.
10
11use middle::cfg::*;
Niko Matsakis0f03b562014-05-14 19:31:3012use middle::def;
Niko Matsakis86b6e6e2013-05-10 17:10:3513use middle::graph;
14use middle::typeck;
15use middle::ty;
Niko Matsakis86b6e6e2013-05-10 17:10:3516use syntax::ast;
17use syntax::ast_util;
Alex Crichtonbec7b762014-02-28 22:34:2618use util::nodemap::NodeMap;
Niko Matsakis86b6e6e2013-05-10 17:10:3519
Alex Crichton54c2a1e2014-05-16 17:15:3320use std::gc::Gc;
21
Eduard Burtescu9b1fee82014-03-06 03:07:4722struct CFGBuilder<'a> {
23 tcx: &'a ty::ctxt,
Alex Crichtonbec7b762014-02-28 22:34:2624 exit_map: NodeMap<CFGIndex>,
Niko Matsakis86b6e6e2013-05-10 17:10:3525 graph: CFGGraph,
Felix S. Klock II65b65fe2014-05-08 22:07:5726 fn_exit: CFGIndex,
27 loop_scopes: Vec<LoopScope>,
Niko Matsakis86b6e6e2013-05-10 17:10:3528}
29
30struct LoopScope {
Michael Woerister8a329772013-07-27 08:25:5931 loop_id: ast::NodeId, // id of loop/while node
Niko Matsakis86b6e6e2013-05-10 17:10:3532 continue_index: CFGIndex, // where to go on a `loop`
33 break_index: CFGIndex, // where to go on a `break
34}
35
Eduard Burtescu9b1fee82014-03-06 03:07:4736pub fn construct(tcx: &ty::ctxt,
Michael Woerister4bd14242013-07-19 05:38:5537 blk: &ast::Block) -> CFG {
Felix S. Klock II65b65fe2014-05-08 22:07:5738 let mut graph = graph::Graph::new();
39 let entry = add_initial_dummy_node(&mut graph);
40
41 // `fn_exit` is target of return exprs, which lies somewhere
42 // outside input `blk`. (Distinguishing `fn_exit` and `block_exit`
43 // also resolves chicken-and-egg problem that arises if you try to
44 // have return exprs jump to `block_exit` during construction.)
45 let fn_exit = add_initial_dummy_node(&mut graph);
46 let block_exit;
47
Niko Matsakis86b6e6e2013-05-10 17:10:3548 let mut cfg_builder = CFGBuilder {
Alex Crichtonbec7b762014-02-28 22:34:2649 exit_map: NodeMap::new(),
Felix S. Klock II65b65fe2014-05-08 22:07:5750 graph: graph,
51 fn_exit: fn_exit,
Niko Matsakis86b6e6e2013-05-10 17:10:3552 tcx: tcx,
Patrick Walton3b6e9d42014-03-04 18:02:4953 loop_scopes: Vec::new()
Niko Matsakis86b6e6e2013-05-10 17:10:3554 };
Felix S. Klock II65b65fe2014-05-08 22:07:5755 block_exit = cfg_builder.block(blk, entry);
56 cfg_builder.add_contained_edge(block_exit, fn_exit);
Alex Crichtonab387a62013-11-28 20:22:5357 let CFGBuilder {exit_map, graph, ..} = cfg_builder;
Niko Matsakis86b6e6e2013-05-10 17:10:3558 CFG {exit_map: exit_map,
59 graph: graph,
60 entry: entry,
Felix S. Klock II65b65fe2014-05-08 22:07:5761 exit: fn_exit}
62}
63
64fn add_initial_dummy_node(g: &mut CFGGraph) -> CFGIndex {
65 g.add_node(CFGNodeData { id: ast::DUMMY_NODE_ID })
Niko Matsakis86b6e6e2013-05-10 17:10:3566}
67
Eduard Burtescu9b1fee82014-03-06 03:07:4768impl<'a> CFGBuilder<'a> {
Michael Woerister4bd14242013-07-19 05:38:5569 fn block(&mut self, blk: &ast::Block, pred: CFGIndex) -> CFGIndex {
Niko Matsakis86b6e6e2013-05-10 17:10:3570 let mut stmts_exit = pred;
Alex Crichton54c2a1e2014-05-16 17:15:3371 for stmt in blk.stmts.iter() {
72 stmts_exit = self.stmt(stmt.clone(), stmts_exit);
Niko Matsakis86b6e6e2013-05-10 17:10:3573 }
74
Alex Crichton54c2a1e2014-05-16 17:15:3375 let expr_exit = self.opt_expr(blk.expr.clone(), stmts_exit);
Niko Matsakis86b6e6e2013-05-10 17:10:3576
Michael Woerister0cc70742013-07-16 18:08:3577 self.add_node(blk.id, [expr_exit])
Niko Matsakis86b6e6e2013-05-10 17:10:3578 }
79
Alex Crichton54c2a1e2014-05-16 17:15:3380 fn stmt(&mut self, stmt: Gc<ast::Stmt>, pred: CFGIndex) -> CFGIndex {
Niko Matsakis86b6e6e2013-05-10 17:10:3581 match stmt.node {
Niko Matsakis1b487a82014-08-28 01:46:5282 ast::StmtDecl(ref decl, id) => {
83 let exit = self.decl(&**decl, pred);
84 self.add_node(id, [exit])
Niko Matsakis86b6e6e2013-05-10 17:10:3585 }
86
Niko Matsakis1b487a82014-08-28 01:46:5287 ast::StmtExpr(ref expr, id) | ast::StmtSemi(ref expr, id) => {
88 let exit = self.expr(expr.clone(), pred);
89 self.add_node(id, [exit])
Niko Matsakis86b6e6e2013-05-10 17:10:3590 }
91
Alex Crichtonab387a62013-11-28 20:22:5392 ast::StmtMac(..) => {
Niko Matsakis86b6e6e2013-05-10 17:10:3593 self.tcx.sess.span_bug(stmt.span, "unexpanded macro");
94 }
95 }
96 }
97
Alex Crichton54c2a1e2014-05-16 17:15:3398 fn decl(&mut self, decl: &ast::Decl, pred: CFGIndex) -> CFGIndex {
Niko Matsakis86b6e6e2013-05-10 17:10:3599 match decl.node {
Alex Crichton54c2a1e2014-05-16 17:15:33100 ast::DeclLocal(ref local) => {
101 let init_exit = self.opt_expr(local.init.clone(), pred);
102 self.pat(&*local.pat, init_exit)
Niko Matsakis86b6e6e2013-05-10 17:10:35103 }
104
Marvin Löbel74190852013-09-02 01:45:37105 ast::DeclItem(_) => {
Niko Matsakis86b6e6e2013-05-10 17:10:35106 pred
107 }
108 }
109 }
110
Alex Crichton54c2a1e2014-05-16 17:15:33111 fn pat(&mut self, pat: &ast::Pat, pred: CFGIndex) -> CFGIndex {
Niko Matsakis86b6e6e2013-05-10 17:10:35112 match pat.node {
Marvin Löbel74190852013-09-02 01:45:37113 ast::PatIdent(_, _, None) |
114 ast::PatEnum(_, None) |
Alex Crichtonab387a62013-11-28 20:22:53115 ast::PatLit(..) |
116 ast::PatRange(..) |
Felix S. Klock IId3202352014-08-06 15:04:44117 ast::PatWild(_) => {
Niko Matsakis86b6e6e2013-05-10 17:10:35118 self.add_node(pat.id, [pred])
119 }
120
Alex Crichton54c2a1e2014-05-16 17:15:33121 ast::PatBox(ref subpat) |
122 ast::PatRegion(ref subpat) |
123 ast::PatIdent(_, _, Some(ref subpat)) => {
124 let subpat_exit = self.pat(&**subpat, pred);
Niko Matsakis86b6e6e2013-05-10 17:10:35125 self.add_node(pat.id, [subpat_exit])
126 }
127
Marvin Löbel74190852013-09-02 01:45:37128 ast::PatEnum(_, Some(ref subpats)) |
129 ast::PatTup(ref subpats) => {
Niko Matsakis86b6e6e2013-05-10 17:10:35130 let pats_exit =
Alex Crichton54c2a1e2014-05-16 17:15:33131 self.pats_all(subpats.iter().map(|p| p.clone()), pred);
Niko Matsakis86b6e6e2013-05-10 17:10:35132 self.add_node(pat.id, [pats_exit])
133 }
134
Marvin Löbel74190852013-09-02 01:45:37135 ast::PatStruct(_, ref subpats, _) => {
Niko Matsakis86b6e6e2013-05-10 17:10:35136 let pats_exit =
Alex Crichton54c2a1e2014-05-16 17:15:33137 self.pats_all(subpats.iter().map(|f| f.pat.clone()), pred);
Niko Matsakis86b6e6e2013-05-10 17:10:35138 self.add_node(pat.id, [pats_exit])
139 }
140
Marvin Löbel74190852013-09-02 01:45:37141 ast::PatVec(ref pre, ref vec, ref post) => {
Niko Matsakis86b6e6e2013-05-10 17:10:35142 let pre_exit =
Erick Tryzelaar68f40d22013-08-10 03:09:47143 self.pats_all(pre.iter().map(|p| *p), pred);
Niko Matsakis86b6e6e2013-05-10 17:10:35144 let vec_exit =
Erick Tryzelaar68f40d22013-08-10 03:09:47145 self.pats_all(vec.iter().map(|p| *p), pre_exit);
Niko Matsakis86b6e6e2013-05-10 17:10:35146 let post_exit =
Erick Tryzelaar68f40d22013-08-10 03:09:47147 self.pats_all(post.iter().map(|p| *p), vec_exit);
Niko Matsakis86b6e6e2013-05-10 17:10:35148 self.add_node(pat.id, [post_exit])
149 }
Keegan McAllister5fdd0e42014-05-19 20:29:41150
151 ast::PatMac(_) => {
152 self.tcx.sess.span_bug(pat.span, "unexpanded macro");
153 }
Niko Matsakis86b6e6e2013-05-10 17:10:35154 }
155 }
156
Alex Crichton54c2a1e2014-05-16 17:15:33157 fn pats_all<I: Iterator<Gc<ast::Pat>>>(&mut self,
Niko Matsakis86b6e6e2013-05-10 17:10:35158 pats: I,
159 pred: CFGIndex) -> CFGIndex {
160 //! Handles case where all of the patterns must match.
161 let mut pats = pats;
Alex Crichton54c2a1e2014-05-16 17:15:33162 pats.fold(pred, |pred, pat| self.pat(&*pat, pred))
Niko Matsakis86b6e6e2013-05-10 17:10:35163 }
164
165 fn pats_any(&mut self,
Alex Crichton54c2a1e2014-05-16 17:15:33166 pats: &[Gc<ast::Pat>],
Niko Matsakis86b6e6e2013-05-10 17:10:35167 pred: CFGIndex) -> CFGIndex {
168 //! Handles case where just one of the patterns must match.
169
170 if pats.len() == 1 {
Alex Crichton54c2a1e2014-05-16 17:15:33171 self.pat(&*pats[0], pred)
Niko Matsakis86b6e6e2013-05-10 17:10:35172 } else {
173 let collect = self.add_dummy_node([]);
Daniel Micay100894552013-08-03 16:45:23174 for &pat in pats.iter() {
Alex Crichton54c2a1e2014-05-16 17:15:33175 let pat_exit = self.pat(&*pat, pred);
Niko Matsakis86b6e6e2013-05-10 17:10:35176 self.add_contained_edge(pat_exit, collect);
177 }
178 collect
179 }
180 }
181
Alex Crichton54c2a1e2014-05-16 17:15:33182 fn expr(&mut self, expr: Gc<ast::Expr>, pred: CFGIndex) -> CFGIndex {
Niko Matsakis86b6e6e2013-05-10 17:10:35183 match expr.node {
Alex Crichton54c2a1e2014-05-16 17:15:33184 ast::ExprBlock(ref blk) => {
185 let blk_exit = self.block(&**blk, pred);
Niko Matsakis86b6e6e2013-05-10 17:10:35186 self.add_node(expr.id, [blk_exit])
187 }
188
Alex Crichton54c2a1e2014-05-16 17:15:33189 ast::ExprIf(ref cond, ref then, None) => {
Niko Matsakis86b6e6e2013-05-10 17:10:35190 //
191 // [pred]
192 // |
193 // v 1
194 // [cond]
195 // |
196 // / \
197 // / \
198 // v 2 *
199 // [then] |
200 // | |
201 // v 3 v 4
202 // [..expr..]
203 //
Alex Crichton54c2a1e2014-05-16 17:15:33204 let cond_exit = self.expr(cond.clone(), pred); // 1
205 let then_exit = self.block(&**then, cond_exit); // 2
206 self.add_node(expr.id, [cond_exit, then_exit]) // 3,4
Niko Matsakis86b6e6e2013-05-10 17:10:35207 }
208
Alex Crichton54c2a1e2014-05-16 17:15:33209 ast::ExprIf(ref cond, ref then, Some(ref otherwise)) => {
Niko Matsakis86b6e6e2013-05-10 17:10:35210 //
211 // [pred]
212 // |
213 // v 1
214 // [cond]
215 // |
216 // / \
217 // / \
218 // v 2 v 3
219 // [then][otherwise]
220 // | |
221 // v 4 v 5
222 // [..expr..]
223 //
Alex Crichton54c2a1e2014-05-16 17:15:33224 let cond_exit = self.expr(cond.clone(), pred); // 1
225 let then_exit = self.block(&**then, cond_exit); // 2
226 let else_exit = self.expr(otherwise.clone(), cond_exit); // 3
227 self.add_node(expr.id, [then_exit, else_exit]) // 4, 5
Niko Matsakis86b6e6e2013-05-10 17:10:35228 }
229
Pythoner6373b9d62014-07-26 00:12:51230 ast::ExprWhile(ref cond, ref body, _) => {
Niko Matsakis86b6e6e2013-05-10 17:10:35231 //
232 // [pred]
233 // |
234 // v 1
235 // [loopback] <--+ 5
236 // | |
237 // v 2 |
238 // +-----[cond] |
239 // | | |
240 // | v 4 |
241 // | [body] -----+
242 // v 3
243 // [expr]
244 //
Patrick Waltoncaa564b2014-07-22 03:54:28245 // Note that `break` and `continue` statements
Niko Matsakis86b6e6e2013-05-10 17:10:35246 // may cause additional edges.
247
Michael Sullivan7dbc5ae2013-07-30 23:47:22248 // Is the condition considered part of the loop?
Alex Crichton54c2a1e2014-05-16 17:15:33249 let loopback = self.add_dummy_node([pred]); // 1
250 let cond_exit = self.expr(cond.clone(), loopback); // 2
251 let expr_exit = self.add_node(expr.id, [cond_exit]); // 3
Niko Matsakis86b6e6e2013-05-10 17:10:35252 self.loop_scopes.push(LoopScope {
253 loop_id: expr.id,
254 continue_index: loopback,
255 break_index: expr_exit
256 });
Alex Crichton54c2a1e2014-05-16 17:15:33257 let body_exit = self.block(&**body, cond_exit); // 4
258 self.add_contained_edge(body_exit, loopback); // 5
Felix S. Klock IIbe9c2d12014-05-20 16:49:19259 self.loop_scopes.pop();
Niko Matsakis86b6e6e2013-05-10 17:10:35260 expr_exit
261 }
262
Patrick Waltoncaa564b2014-07-22 03:54:28263 ast::ExprForLoop(ref pat, ref head, ref body, _) => {
264 //
265 // [pred]
266 // |
267 // v 1
268 // [head]
269 // |
270 // v 2
271 // [loopback] <--+ 7
272 // | |
273 // v 3 |
274 // +------[cond] |
275 // | | |
276 // | v 5 |
277 // | [pat] |
278 // | | |
279 // | v 6 |
280 // v 4 [body] -----+
281 // [expr]
282 //
283 // Note that `break` and `continue` statements
284 // may cause additional edges.
285
286 let head = self.expr(head.clone(), pred); // 1
287 let loopback = self.add_dummy_node([head]); // 2
288 let cond = self.add_dummy_node([loopback]); // 3
289 let expr_exit = self.add_node(expr.id, [cond]); // 4
290 self.loop_scopes.push(LoopScope {
291 loop_id: expr.id,
292 continue_index: loopback,
293 break_index: expr_exit,
294 });
295 let pat = self.pat(&**pat, cond); // 5
296 let body = self.block(&**body, pat); // 6
297 self.add_contained_edge(body, loopback); // 7
298 self.loop_scopes.pop();
299 expr_exit
300 }
Graydon Hoarec29e9fb2013-07-30 00:25:00301
Alex Crichton54c2a1e2014-05-16 17:15:33302 ast::ExprLoop(ref body, _) => {
Niko Matsakis86b6e6e2013-05-10 17:10:35303 //
304 // [pred]
305 // |
306 // v 1
307 // [loopback] <---+
308 // | 4 |
309 // v 3 |
310 // [body] ------+
311 //
312 // [expr] 2
313 //
314 // Note that `break` and `loop` statements
315 // may cause additional edges.
316
Alex Crichton54c2a1e2014-05-16 17:15:33317 let loopback = self.add_dummy_node([pred]); // 1
318 let expr_exit = self.add_node(expr.id, []); // 2
Niko Matsakis86b6e6e2013-05-10 17:10:35319 self.loop_scopes.push(LoopScope {
320 loop_id: expr.id,
321 continue_index: loopback,
322 break_index: expr_exit,
323 });
Alex Crichton54c2a1e2014-05-16 17:15:33324 let body_exit = self.block(&**body, loopback); // 3
325 self.add_contained_edge(body_exit, loopback); // 4
Niko Matsakis86b6e6e2013-05-10 17:10:35326 self.loop_scopes.pop();
327 expr_exit
328 }
329
Alex Crichton54c2a1e2014-05-16 17:15:33330 ast::ExprMatch(ref discr, ref arms) => {
Niko Matsakis86b6e6e2013-05-10 17:10:35331 //
332 // [pred]
333 // |
334 // v 1
335 // [discr]
336 // |
337 // v 2
Patrick Waltonb2eb8882014-07-25 22:18:19338 // [cond1]
Niko Matsakis86b6e6e2013-05-10 17:10:35339 // / \
340 // | \
Patrick Waltonb2eb8882014-07-25 22:18:19341 // v 3 \
342 // [pat1] \
343 // | |
344 // v 4 |
345 // [guard1] |
346 // | |
347 // | |
348 // v 5 v
349 // [body1] [cond2]
350 // | / \
351 // | ... ...
352 // | | |
353 // v 6 v v
354 // [.....expr.....]
Niko Matsakis86b6e6e2013-05-10 17:10:35355 //
Alex Crichton54c2a1e2014-05-16 17:15:33356 let discr_exit = self.expr(discr.clone(), pred); // 1
Niko Matsakis86b6e6e2013-05-10 17:10:35357
358 let expr_exit = self.add_node(expr.id, []);
Patrick Waltonb2eb8882014-07-25 22:18:19359 let mut cond_exit = discr_exit;
Daniel Micay100894552013-08-03 16:45:23360 for arm in arms.iter() {
Patrick Waltonb2eb8882014-07-25 22:18:19361 cond_exit = self.add_dummy_node([cond_exit]); // 2
Patrick Waltonc1ed4d72014-02-28 23:25:15362 let pats_exit = self.pats_any(arm.pats.as_slice(),
Patrick Waltonb2eb8882014-07-25 22:18:19363 cond_exit); // 3
364 let guard_exit = self.opt_expr(arm.guard,
365 pats_exit); // 4
366 let body_exit = self.expr(arm.body.clone(),
367 guard_exit); // 5
368 self.add_contained_edge(body_exit, expr_exit); // 6
Niko Matsakis86b6e6e2013-05-10 17:10:35369 }
370 expr_exit
371 }
372
Alex Crichton54c2a1e2014-05-16 17:15:33373 ast::ExprBinary(op, ref l, ref r) if ast_util::lazy_binop(op) => {
Niko Matsakis86b6e6e2013-05-10 17:10:35374 //
375 // [pred]
376 // |
377 // v 1
378 // [l]
379 // |
380 // / \
381 // / \
382 // v 2 *
383 // [r] |
384 // | |
385 // v 3 v 4
386 // [..exit..]
387 //
Alex Crichton54c2a1e2014-05-16 17:15:33388 let l_exit = self.expr(l.clone(), pred); // 1
389 let r_exit = self.expr(r.clone(), l_exit); // 2
Niko Matsakis86b6e6e2013-05-10 17:10:35390 self.add_node(expr.id, [l_exit, r_exit]) // 3,4
391 }
392
Alex Crichton54c2a1e2014-05-16 17:15:33393 ast::ExprRet(ref v) => {
394 let v_exit = self.opt_expr(v.clone(), pred);
Felix S. Klock II65b65fe2014-05-08 22:07:57395 let b = self.add_node(expr.id, [v_exit]);
396 self.add_returning_edge(expr, b);
397 self.add_node(ast::DUMMY_NODE_ID, [])
Niko Matsakis86b6e6e2013-05-10 17:10:35398 }
399
Marvin Löbel74190852013-09-02 01:45:37400 ast::ExprBreak(label) => {
Niko Matsakis86b6e6e2013-05-10 17:10:35401 let loop_scope = self.find_scope(expr, label);
Felix S. Klock II65b65fe2014-05-08 22:07:57402 let b = self.add_node(expr.id, [pred]);
403 self.add_exiting_edge(expr, b,
Niko Matsakis86b6e6e2013-05-10 17:10:35404 loop_scope, loop_scope.break_index);
Felix S. Klock II65b65fe2014-05-08 22:07:57405 self.add_node(ast::DUMMY_NODE_ID, [])
Niko Matsakis86b6e6e2013-05-10 17:10:35406 }
407
Marvin Löbel74190852013-09-02 01:45:37408 ast::ExprAgain(label) => {
Niko Matsakis86b6e6e2013-05-10 17:10:35409 let loop_scope = self.find_scope(expr, label);
Felix S. Klock II65b65fe2014-05-08 22:07:57410 let a = self.add_node(expr.id, [pred]);
411 self.add_exiting_edge(expr, a,
Niko Matsakis86b6e6e2013-05-10 17:10:35412 loop_scope, loop_scope.continue_index);
Felix S. Klock II65b65fe2014-05-08 22:07:57413 self.add_node(ast::DUMMY_NODE_ID, [])
Niko Matsakis86b6e6e2013-05-10 17:10:35414 }
415
Eduard Burtescu7c48e532014-04-04 10:12:18416 ast::ExprVec(ref elems) => {
Patrick Waltonc1ed4d72014-02-28 23:25:15417 self.straightline(expr, pred, elems.as_slice())
Niko Matsakis86b6e6e2013-05-10 17:10:35418 }
419
Alex Crichton54c2a1e2014-05-16 17:15:33420 ast::ExprCall(ref func, ref args) => {
421 self.call(expr, pred, func.clone(), args.as_slice())
Niko Matsakis86b6e6e2013-05-10 17:10:35422 }
423
Eduard Burtescu05e4d942014-02-26 14:06:45424 ast::ExprMethodCall(_, _, ref args) => {
Patrick Waltonc1ed4d72014-02-28 23:25:15425 self.call(expr, pred, *args.get(0), args.slice_from(1))
Niko Matsakis86b6e6e2013-05-10 17:10:35426 }
427
Alex Crichton54c2a1e2014-05-16 17:15:33428 ast::ExprIndex(ref l, ref r) |
429 ast::ExprBinary(_, ref l, ref r) if self.is_method_call(&*expr) => {
430 self.call(expr, pred, l.clone(), [r.clone()])
Niko Matsakis86b6e6e2013-05-10 17:10:35431 }
432
Alex Crichton54c2a1e2014-05-16 17:15:33433 ast::ExprUnary(_, ref e) if self.is_method_call(&*expr) => {
434 self.call(expr, pred, e.clone(), [])
Niko Matsakis86b6e6e2013-05-10 17:10:35435 }
436
Marvin Löbel74190852013-09-02 01:45:37437 ast::ExprTup(ref exprs) => {
Patrick Waltonc1ed4d72014-02-28 23:25:15438 self.straightline(expr, pred, exprs.as_slice())
Niko Matsakis86b6e6e2013-05-10 17:10:35439 }
440
Marvin Löbel74190852013-09-02 01:45:37441 ast::ExprStruct(_, ref fields, base) => {
Niko Matsakis86b6e6e2013-05-10 17:10:35442 let base_exit = self.opt_expr(base, pred);
Alex Crichton54c2a1e2014-05-16 17:15:33443 let field_exprs: Vec<Gc<ast::Expr>> =
Erick Tryzelaar68f40d22013-08-10 03:09:47444 fields.iter().map(|f| f.expr).collect();
Felix S. Klock II43c07242014-03-08 20:36:22445 self.straightline(expr, base_exit, field_exprs.as_slice())
Niko Matsakis86b6e6e2013-05-10 17:10:35446 }
447
Eduard Burtescu7c48e532014-04-04 10:12:18448 ast::ExprRepeat(elem, count) => {
Niko Matsakis86b6e6e2013-05-10 17:10:35449 self.straightline(expr, pred, [elem, count])
450 }
451
Marvin Löbel74190852013-09-02 01:45:37452 ast::ExprAssign(l, r) |
Eduard Burtescu05e4d942014-02-26 14:06:45453 ast::ExprAssignOp(_, l, r) => {
Niko Matsakis86b6e6e2013-05-10 17:10:35454 self.straightline(expr, pred, [r, l])
455 }
456
Eduard Burtescu05e4d942014-02-26 14:06:45457 ast::ExprIndex(l, r) |
458 ast::ExprBinary(_, l, r) => { // NB: && and || handled earlier
Niko Matsakis86b6e6e2013-05-10 17:10:35459 self.straightline(expr, pred, [l, r])
460 }
461
Patrick Waltone1271152013-12-18 00:46:18462 ast::ExprBox(p, e) => {
463 self.straightline(expr, pred, [p, e])
464 }
465
Marvin Löbel74190852013-09-02 01:45:37466 ast::ExprAddrOf(_, e) |
Marvin Löbel74190852013-09-02 01:45:37467 ast::ExprCast(e, _) |
Eduard Burtescu05e4d942014-02-26 14:06:45468 ast::ExprUnary(_, e) |
Marvin Löbel74190852013-09-02 01:45:37469 ast::ExprParen(e) |
Marvin Löbel74190852013-09-02 01:45:37470 ast::ExprField(e, _, _) => {
Niko Matsakis86b6e6e2013-05-10 17:10:35471 self.straightline(expr, pred, [e])
472 }
473
Felix S. Klock IIbe9c2d12014-05-20 16:49:19474 ast::ExprInlineAsm(ref inline_asm) => {
475 let inputs = inline_asm.inputs.iter();
476 let outputs = inline_asm.outputs.iter();
Felix S. Klock IIbe9c2d12014-05-20 16:49:19477 let post_inputs = self.exprs(inputs.map(|a| {
478 debug!("cfg::construct InlineAsm id:{} input:{:?}", expr.id, a);
Piotr Czarnecki41556432014-08-19 19:39:26479 let &(_, expr) = a;
480 expr
Felix S. Klock IIbe9c2d12014-05-20 16:49:19481 }), pred);
482 let post_outputs = self.exprs(outputs.map(|a| {
483 debug!("cfg::construct InlineAsm id:{} output:{:?}", expr.id, a);
Piotr Czarnecki41556432014-08-19 19:39:26484 let &(_, expr, _) = a;
485 expr
Felix S. Klock IIbe9c2d12014-05-20 16:49:19486 }), post_inputs);
487 self.add_node(expr.id, [post_outputs])
488 }
489
Alex Crichtonab387a62013-11-28 20:22:53490 ast::ExprMac(..) |
Alex Crichtonab387a62013-11-28 20:22:53491 ast::ExprFnBlock(..) |
492 ast::ExprProc(..) |
Patrick Walton02adaca2014-05-29 05:26:56493 ast::ExprUnboxedFn(..) |
Alex Crichtonab387a62013-11-28 20:22:53494 ast::ExprLit(..) |
495 ast::ExprPath(..) => {
Niko Matsakis86b6e6e2013-05-10 17:10:35496 self.straightline(expr, pred, [])
497 }
498 }
499 }
500
501 fn call(&mut self,
Alex Crichton54c2a1e2014-05-16 17:15:33502 call_expr: Gc<ast::Expr>,
Niko Matsakis86b6e6e2013-05-10 17:10:35503 pred: CFGIndex,
Alex Crichton54c2a1e2014-05-16 17:15:33504 func_or_rcvr: Gc<ast::Expr>,
505 args: &[Gc<ast::Expr>]) -> CFGIndex {
Niko Matsakis86b6e6e2013-05-10 17:10:35506 let func_or_rcvr_exit = self.expr(func_or_rcvr, pred);
Felix S. Klock IIbe9c2d12014-05-20 16:49:19507 let ret = self.straightline(call_expr, func_or_rcvr_exit, args);
508
509 let return_ty = ty::node_id_to_type(self.tcx, call_expr.id);
510 let fails = ty::type_is_bot(return_ty);
511 if fails {
512 self.add_node(ast::DUMMY_NODE_ID, [])
513 } else {
514 ret
515 }
Niko Matsakis86b6e6e2013-05-10 17:10:35516 }
517
Felix S. Klock IIbe9c2d12014-05-20 16:49:19518 fn exprs<I:Iterator<Gc<ast::Expr>>>(&mut self,
519 mut exprs: I,
520 pred: CFGIndex) -> CFGIndex {
Niko Matsakis86b6e6e2013-05-10 17:10:35521 //! Constructs graph for `exprs` evaluated in order
Felix S. Klock IIbe9c2d12014-05-20 16:49:19522 exprs.fold(pred, |p, e| self.expr(e, p))
Niko Matsakis86b6e6e2013-05-10 17:10:35523 }
524
525 fn opt_expr(&mut self,
Alex Crichton54c2a1e2014-05-16 17:15:33526 opt_expr: Option<Gc<ast::Expr>>,
Niko Matsakis86b6e6e2013-05-10 17:10:35527 pred: CFGIndex) -> CFGIndex {
528 //! Constructs graph for `opt_expr` evaluated, if Some
529
530 opt_expr.iter().fold(pred, |p, &e| self.expr(e, p))
531 }
532
533 fn straightline(&mut self,
Alex Crichton54c2a1e2014-05-16 17:15:33534 expr: Gc<ast::Expr>,
Niko Matsakis86b6e6e2013-05-10 17:10:35535 pred: CFGIndex,
Alex Crichton54c2a1e2014-05-16 17:15:33536 subexprs: &[Gc<ast::Expr>]) -> CFGIndex {
Niko Matsakis86b6e6e2013-05-10 17:10:35537 //! Handles case of an expression that evaluates `subexprs` in order
538
Felix S. Klock IIbe9c2d12014-05-20 16:49:19539 let subexprs_exit = self.exprs(subexprs.iter().map(|&e|e), pred);
Niko Matsakis86b6e6e2013-05-10 17:10:35540 self.add_node(expr.id, [subexprs_exit])
541 }
542
543 fn add_dummy_node(&mut self, preds: &[CFGIndex]) -> CFGIndex {
Felix S. Klock II65b65fe2014-05-08 22:07:57544 self.add_node(ast::DUMMY_NODE_ID, preds)
Niko Matsakis86b6e6e2013-05-10 17:10:35545 }
546
Michael Woerister8a329772013-07-27 08:25:59547 fn add_node(&mut self, id: ast::NodeId, preds: &[CFGIndex]) -> CFGIndex {
Niko Matsakis86b6e6e2013-05-10 17:10:35548 assert!(!self.exit_map.contains_key(&id));
549 let node = self.graph.add_node(CFGNodeData {id: id});
Felix S. Klock II65b65fe2014-05-08 22:07:57550 if id != ast::DUMMY_NODE_ID {
551 assert!(!self.exit_map.contains_key(&id));
552 self.exit_map.insert(id, node);
553 }
Daniel Micay100894552013-08-03 16:45:23554 for &pred in preds.iter() {
Niko Matsakis86b6e6e2013-05-10 17:10:35555 self.add_contained_edge(pred, node);
556 }
557 node
558 }
559
560 fn add_contained_edge(&mut self,
561 source: CFGIndex,
562 target: CFGIndex) {
Huon Wilson7785fe12014-03-19 12:16:56563 let data = CFGEdgeData {exiting_scopes: vec!() };
Niko Matsakis86b6e6e2013-05-10 17:10:35564 self.graph.add_edge(source, target, data);
565 }
566
567 fn add_exiting_edge(&mut self,
Alex Crichton54c2a1e2014-05-16 17:15:33568 from_expr: Gc<ast::Expr>,
Niko Matsakis86b6e6e2013-05-10 17:10:35569 from_index: CFGIndex,
570 to_loop: LoopScope,
571 to_index: CFGIndex) {
Huon Wilson7785fe12014-03-19 12:16:56572 let mut data = CFGEdgeData {exiting_scopes: vec!() };
Niko Matsakis86b6e6e2013-05-10 17:10:35573 let mut scope_id = from_expr.id;
574 while scope_id != to_loop.loop_id {
Huon Wilson7785fe12014-03-19 12:16:56575
Niko Matsakis86b6e6e2013-05-10 17:10:35576 data.exiting_scopes.push(scope_id);
577 scope_id = self.tcx.region_maps.encl_scope(scope_id);
578 }
579 self.graph.add_edge(from_index, to_index, data);
580 }
581
Felix S. Klock II65b65fe2014-05-08 22:07:57582 fn add_returning_edge(&mut self,
Alex Crichton54c2a1e2014-05-16 17:15:33583 _from_expr: Gc<ast::Expr>,
Felix S. Klock II65b65fe2014-05-08 22:07:57584 from_index: CFGIndex) {
Patrick Walton36195eb2014-05-16 17:45:16585 let mut data = CFGEdgeData {
586 exiting_scopes: vec!(),
587 };
Felix S. Klock II65b65fe2014-05-08 22:07:57588 for &LoopScope { loop_id: id, .. } in self.loop_scopes.iter().rev() {
589 data.exiting_scopes.push(id);
590 }
591 self.graph.add_edge(from_index, self.fn_exit, data);
592 }
593
Niko Matsakis86b6e6e2013-05-10 17:10:35594 fn find_scope(&self,
Alex Crichton54c2a1e2014-05-16 17:15:33595 expr: Gc<ast::Expr>,
Edward Wang386db052014-02-15 08:54:32596 label: Option<ast::Ident>) -> LoopScope {
Niko Matsakis86b6e6e2013-05-10 17:10:35597 match label {
598 None => {
Simon Sapinaa66b912013-12-23 14:08:23599 return *self.loop_scopes.last().unwrap();
Niko Matsakis86b6e6e2013-05-10 17:10:35600 }
601
602 Some(_) => {
Alex Crichton0dbb9092014-03-21 02:49:20603 match self.tcx.def_map.borrow().find(&expr.id) {
Niko Matsakis0f03b562014-05-14 19:31:30604 Some(&def::DefLabel(loop_id)) => {
Daniel Micay100894552013-08-03 16:45:23605 for l in self.loop_scopes.iter() {
Niko Matsakis86b6e6e2013-05-10 17:10:35606 if l.loop_id == loop_id {
607 return *l;
608 }
609 }
610 self.tcx.sess.span_bug(
611 expr.span,
Patrick Walton36195eb2014-05-16 17:45:16612 format!("no loop scope for id {:?}",
613 loop_id).as_slice());
Niko Matsakis86b6e6e2013-05-10 17:10:35614 }
615
616 r => {
617 self.tcx.sess.span_bug(
618 expr.span,
Patrick Walton36195eb2014-05-16 17:45:16619 format!("bad entry `{:?}` in def_map for label",
620 r).as_slice());
Niko Matsakis86b6e6e2013-05-10 17:10:35621 }
622 }
623 }
624 }
625 }
626
Marvin Löbel74190852013-09-02 01:45:37627 fn is_method_call(&self, expr: &ast::Expr) -> bool {
Eduard Burtescu20b4e152014-03-06 17:24:11628 let method_call = typeck::MethodCall::expr(expr.id);
Felix S. Klock IIaaf398f2014-04-17 19:00:08629 self.tcx.method_map.borrow().contains_key(&method_call)
Niko Matsakis86b6e6e2013-05-10 17:10:35630 }
Patrick Walton99d44d22013-07-10 02:32:09631}