blob: 7d62b6ff900408e62de48bd258153df79f68736e [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
Niko Matsakis7ab0d1a2015-04-07 10:12:1311use rustc_data_structures::graph;
Niko Matsakis86b6e6e2013-05-10 17:10:3512use middle::cfg::*;
Niko Matsakis0f03b562014-05-14 19:31:3013use middle::def;
James Miller4bae1332015-02-19 16:54:4114use middle::pat_util;
Felix S. Klock II5ff90872014-11-18 13:22:5915use middle::region::CodeExtent;
Niko Matsakis86b6e6e2013-05-10 17:10:3516use middle::ty;
Niko Matsakis86b6e6e2013-05-10 17:10:3517use syntax::ast;
18use syntax::ast_util;
Eduard Burtescub0621282014-09-07 17:09:0619use syntax::ptr::P;
Niko Matsakis86b6e6e2013-05-10 17:10:3520
Eduard Burtescu28be6952014-04-22 12:56:3721struct CFGBuilder<'a, 'tcx: 'a> {
22 tcx: &'a ty::ctxt<'tcx>,
Niko Matsakis86b6e6e2013-05-10 17:10:3523 graph: CFGGraph,
Felix S. Klock II65b65fe2014-05-08 22:07:5724 fn_exit: CFGIndex,
25 loop_scopes: Vec<LoopScope>,
Niko Matsakis86b6e6e2013-05-10 17:10:3526}
27
Niko Matsakisd9530c02015-03-30 13:38:4428#[derive(Copy, Clone)]
Niko Matsakis86b6e6e2013-05-10 17:10:3529struct LoopScope {
Michael Woerister8a329772013-07-27 08:25:5930 loop_id: ast::NodeId, // id of loop/while node
Niko Matsakis86b6e6e2013-05-10 17:10:3531 continue_index: CFGIndex, // where to go on a `loop`
32 break_index: CFGIndex, // where to go on a `break
33}
34
Eduard Burtescu9b1fee82014-03-06 03:07:4735pub fn construct(tcx: &ty::ctxt,
Michael Woerister4bd14242013-07-19 05:38:5536 blk: &ast::Block) -> CFG {
Felix S. Klock II65b65fe2014-05-08 22:07:5737 let mut graph = graph::Graph::new();
James Miller97c17112015-02-19 14:27:2538 let entry = graph.add_node(CFGNodeData::Entry);
Felix S. Klock II65b65fe2014-05-08 22:07:5739
40 // `fn_exit` is target of return exprs, which lies somewhere
41 // outside input `blk`. (Distinguishing `fn_exit` and `block_exit`
42 // also resolves chicken-and-egg problem that arises if you try to
43 // have return exprs jump to `block_exit` during construction.)
James Miller97c17112015-02-19 14:27:2544 let fn_exit = graph.add_node(CFGNodeData::Exit);
Felix S. Klock II65b65fe2014-05-08 22:07:5745 let block_exit;
46
Niko Matsakis86b6e6e2013-05-10 17:10:3547 let mut cfg_builder = CFGBuilder {
Felix S. Klock II65b65fe2014-05-08 22:07:5748 graph: graph,
49 fn_exit: fn_exit,
Niko Matsakis86b6e6e2013-05-10 17:10:3550 tcx: tcx,
Patrick Walton3b6e9d42014-03-04 18:02:4951 loop_scopes: Vec::new()
Niko Matsakis86b6e6e2013-05-10 17:10:3552 };
Felix S. Klock II65b65fe2014-05-08 22:07:5753 block_exit = cfg_builder.block(blk, entry);
54 cfg_builder.add_contained_edge(block_exit, fn_exit);
James Millera0b7bad2015-02-19 14:33:5355 let CFGBuilder {graph, ..} = cfg_builder;
56 CFG {graph: graph,
Niko Matsakis86b6e6e2013-05-10 17:10:3557 entry: entry,
Felix S. Klock II65b65fe2014-05-08 22:07:5758 exit: fn_exit}
59}
60
Eduard Burtescu28be6952014-04-22 12:56:3761impl<'a, 'tcx> CFGBuilder<'a, 'tcx> {
Michael Woerister4bd14242013-07-19 05:38:5562 fn block(&mut self, blk: &ast::Block, pred: CFGIndex) -> CFGIndex {
Niko Matsakis86b6e6e2013-05-10 17:10:3563 let mut stmts_exit = pred;
Jorge Apariciod5d7e652015-01-31 17:20:4664 for stmt in &blk.stmts {
Eduard Burtescub0621282014-09-07 17:09:0665 stmts_exit = self.stmt(&**stmt, stmts_exit);
Niko Matsakis86b6e6e2013-05-10 17:10:3566 }
67
Eduard Burtescub0621282014-09-07 17:09:0668 let expr_exit = self.opt_expr(&blk.expr, stmts_exit);
Niko Matsakis86b6e6e2013-05-10 17:10:3569
James Miller97c17112015-02-19 14:27:2570 self.add_ast_node(blk.id, &[expr_exit])
Niko Matsakis86b6e6e2013-05-10 17:10:3571 }
72
Eduard Burtescub0621282014-09-07 17:09:0673 fn stmt(&mut self, stmt: &ast::Stmt, pred: CFGIndex) -> CFGIndex {
Niko Matsakis86b6e6e2013-05-10 17:10:3574 match stmt.node {
Niko Matsakis1b487a82014-08-28 01:46:5275 ast::StmtDecl(ref decl, id) => {
76 let exit = self.decl(&**decl, pred);
James Miller97c17112015-02-19 14:27:2577 self.add_ast_node(id, &[exit])
Niko Matsakis86b6e6e2013-05-10 17:10:3578 }
79
Niko Matsakis1b487a82014-08-28 01:46:5280 ast::StmtExpr(ref expr, id) | ast::StmtSemi(ref expr, id) => {
Eduard Burtescub0621282014-09-07 17:09:0681 let exit = self.expr(&**expr, pred);
James Miller97c17112015-02-19 14:27:2582 self.add_ast_node(id, &[exit])
Niko Matsakis86b6e6e2013-05-10 17:10:3583 }
84
Alex Crichtonab387a62013-11-28 20:22:5385 ast::StmtMac(..) => {
Niko Matsakis86b6e6e2013-05-10 17:10:3586 self.tcx.sess.span_bug(stmt.span, "unexpanded macro");
87 }
88 }
89 }
90
Alex Crichton54c2a1e2014-05-16 17:15:3391 fn decl(&mut self, decl: &ast::Decl, pred: CFGIndex) -> CFGIndex {
Niko Matsakis86b6e6e2013-05-10 17:10:3592 match decl.node {
Alex Crichton54c2a1e2014-05-16 17:15:3393 ast::DeclLocal(ref local) => {
Eduard Burtescub0621282014-09-07 17:09:0694 let init_exit = self.opt_expr(&local.init, pred);
Alex Crichton54c2a1e2014-05-16 17:15:3395 self.pat(&*local.pat, init_exit)
Niko Matsakis86b6e6e2013-05-10 17:10:3596 }
97
Marvin Löbel74190852013-09-02 01:45:3798 ast::DeclItem(_) => {
Niko Matsakis86b6e6e2013-05-10 17:10:3599 pred
100 }
101 }
102 }
103
Alex Crichton54c2a1e2014-05-16 17:15:33104 fn pat(&mut self, pat: &ast::Pat, pred: CFGIndex) -> CFGIndex {
Niko Matsakis86b6e6e2013-05-10 17:10:35105 match pat.node {
Marvin Löbel74190852013-09-02 01:45:37106 ast::PatIdent(_, _, None) |
107 ast::PatEnum(_, None) |
Sean Patrick Santos29eb5502015-03-25 16:53:28108 ast::PatQPath(..) |
Alex Crichtonab387a62013-11-28 20:22:53109 ast::PatLit(..) |
110 ast::PatRange(..) |
Felix S. Klock IId3202352014-08-06 15:04:44111 ast::PatWild(_) => {
James Miller97c17112015-02-19 14:27:25112 self.add_ast_node(pat.id, &[pred])
Niko Matsakis86b6e6e2013-05-10 17:10:35113 }
114
Alex Crichton54c2a1e2014-05-16 17:15:33115 ast::PatBox(ref subpat) |
Huon Wilsonbf6c0072014-12-05 23:56:25116 ast::PatRegion(ref subpat, _) |
Alex Crichton54c2a1e2014-05-16 17:15:33117 ast::PatIdent(_, _, Some(ref subpat)) => {
118 let subpat_exit = self.pat(&**subpat, pred);
James Miller97c17112015-02-19 14:27:25119 self.add_ast_node(pat.id, &[subpat_exit])
Niko Matsakis86b6e6e2013-05-10 17:10:35120 }
121
Marvin Löbel74190852013-09-02 01:45:37122 ast::PatEnum(_, Some(ref subpats)) |
123 ast::PatTup(ref subpats) => {
Eduard Burtescub0621282014-09-07 17:09:06124 let pats_exit = self.pats_all(subpats.iter(), pred);
James Miller97c17112015-02-19 14:27:25125 self.add_ast_node(pat.id, &[pats_exit])
Niko Matsakis86b6e6e2013-05-10 17:10:35126 }
127
Marvin Löbel74190852013-09-02 01:45:37128 ast::PatStruct(_, ref subpats, _) => {
Niko Matsakis86b6e6e2013-05-10 17:10:35129 let pats_exit =
P1startead6c4b2014-10-06 00:36:53130 self.pats_all(subpats.iter().map(|f| &f.node.pat), pred);
James Miller97c17112015-02-19 14:27:25131 self.add_ast_node(pat.id, &[pats_exit])
Niko Matsakis86b6e6e2013-05-10 17:10:35132 }
133
Marvin Löbel74190852013-09-02 01:45:37134 ast::PatVec(ref pre, ref vec, ref post) => {
Eduard Burtescub0621282014-09-07 17:09:06135 let pre_exit = self.pats_all(pre.iter(), pred);
136 let vec_exit = self.pats_all(vec.iter(), pre_exit);
137 let post_exit = self.pats_all(post.iter(), vec_exit);
James Miller97c17112015-02-19 14:27:25138 self.add_ast_node(pat.id, &[post_exit])
Niko Matsakis86b6e6e2013-05-10 17:10:35139 }
Keegan McAllister5fdd0e42014-05-19 20:29:41140
141 ast::PatMac(_) => {
142 self.tcx.sess.span_bug(pat.span, "unexpanded macro");
143 }
Niko Matsakis86b6e6e2013-05-10 17:10:35144 }
145 }
146
Jorge Aparicio62ee3f12015-01-02 04:26:38147 fn pats_all<'b, I: Iterator<Item=&'b P<ast::Pat>>>(&mut self,
Eduard Burtescub0621282014-09-07 17:09:06148 pats: I,
149 pred: CFGIndex) -> CFGIndex {
Niko Matsakis86b6e6e2013-05-10 17:10:35150 //! Handles case where all of the patterns must match.
Eduard Burtescub0621282014-09-07 17:09:06151 pats.fold(pred, |pred, pat| self.pat(&**pat, pred))
Niko Matsakis86b6e6e2013-05-10 17:10:35152 }
153
Eduard Burtescub0621282014-09-07 17:09:06154 fn expr(&mut self, expr: &ast::Expr, pred: CFGIndex) -> CFGIndex {
Niko Matsakis86b6e6e2013-05-10 17:10:35155 match expr.node {
Alex Crichton54c2a1e2014-05-16 17:15:33156 ast::ExprBlock(ref blk) => {
157 let blk_exit = self.block(&**blk, pred);
James Miller97c17112015-02-19 14:27:25158 self.add_ast_node(expr.id, &[blk_exit])
Niko Matsakis86b6e6e2013-05-10 17:10:35159 }
160
Alex Crichton54c2a1e2014-05-16 17:15:33161 ast::ExprIf(ref cond, ref then, None) => {
Niko Matsakis86b6e6e2013-05-10 17:10:35162 //
163 // [pred]
164 // |
165 // v 1
166 // [cond]
167 // |
168 // / \
169 // / \
170 // v 2 *
171 // [then] |
172 // | |
173 // v 3 v 4
174 // [..expr..]
175 //
Eduard Burtescub0621282014-09-07 17:09:06176 let cond_exit = self.expr(&**cond, pred); // 1
Alex Crichton54c2a1e2014-05-16 17:15:33177 let then_exit = self.block(&**then, cond_exit); // 2
James Miller97c17112015-02-19 14:27:25178 self.add_ast_node(expr.id, &[cond_exit, then_exit]) // 3,4
Niko Matsakis86b6e6e2013-05-10 17:10:35179 }
180
Alex Crichton54c2a1e2014-05-16 17:15:33181 ast::ExprIf(ref cond, ref then, Some(ref otherwise)) => {
Niko Matsakis86b6e6e2013-05-10 17:10:35182 //
183 // [pred]
184 // |
185 // v 1
186 // [cond]
187 // |
188 // / \
189 // / \
190 // v 2 v 3
191 // [then][otherwise]
192 // | |
193 // v 4 v 5
194 // [..expr..]
195 //
Eduard Burtescub0621282014-09-07 17:09:06196 let cond_exit = self.expr(&**cond, pred); // 1
Alex Crichton54c2a1e2014-05-16 17:15:33197 let then_exit = self.block(&**then, cond_exit); // 2
Eduard Burtescub0621282014-09-07 17:09:06198 let else_exit = self.expr(&**otherwise, cond_exit); // 3
James Miller97c17112015-02-19 14:27:25199 self.add_ast_node(expr.id, &[then_exit, else_exit]) // 4, 5
Niko Matsakis86b6e6e2013-05-10 17:10:35200 }
201
Kevin Ballard13e00e42014-08-28 04:34:03202 ast::ExprIfLet(..) => {
203 self.tcx.sess.span_bug(expr.span, "non-desugared ExprIfLet");
204 }
Kevin Ballard0e6ff432014-08-25 02:08:48205
Pythoner6373b9d62014-07-26 00:12:51206 ast::ExprWhile(ref cond, ref body, _) => {
Niko Matsakis86b6e6e2013-05-10 17:10:35207 //
208 // [pred]
209 // |
210 // v 1
211 // [loopback] <--+ 5
212 // | |
213 // v 2 |
214 // +-----[cond] |
215 // | | |
216 // | v 4 |
217 // | [body] -----+
218 // v 3
219 // [expr]
220 //
Patrick Waltoncaa564b2014-07-22 03:54:28221 // Note that `break` and `continue` statements
Niko Matsakis86b6e6e2013-05-10 17:10:35222 // may cause additional edges.
223
Michael Sullivan7dbc5ae2013-07-30 23:47:22224 // Is the condition considered part of the loop?
Nick Cameronca085402014-11-17 08:39:01225 let loopback = self.add_dummy_node(&[pred]); // 1
226 let cond_exit = self.expr(&**cond, loopback); // 2
James Miller97c17112015-02-19 14:27:25227 let expr_exit = self.add_ast_node(expr.id, &[cond_exit]); // 3
Niko Matsakis86b6e6e2013-05-10 17:10:35228 self.loop_scopes.push(LoopScope {
229 loop_id: expr.id,
230 continue_index: loopback,
231 break_index: expr_exit
232 });
Alex Crichton54c2a1e2014-05-16 17:15:33233 let body_exit = self.block(&**body, cond_exit); // 4
234 self.add_contained_edge(body_exit, loopback); // 5
Felix S. Klock IIbe9c2d12014-05-20 16:49:19235 self.loop_scopes.pop();
Niko Matsakis86b6e6e2013-05-10 17:10:35236 expr_exit
237 }
238
John Gallagher45fd6232014-10-03 04:41:24239 ast::ExprWhileLet(..) => {
240 self.tcx.sess.span_bug(expr.span, "non-desugared ExprWhileLet");
241 }
242
Jorge Aparicioa8733162015-01-22 21:59:23243 ast::ExprForLoop(..) => {
Jorge Aparicio9fdc0ef2015-01-08 23:04:26244 self.tcx.sess.span_bug(expr.span, "non-desugared ExprForLoop");
Patrick Waltoncaa564b2014-07-22 03:54:28245 }
Graydon Hoarec29e9fb2013-07-30 00:25:00246
Alex Crichton54c2a1e2014-05-16 17:15:33247 ast::ExprLoop(ref body, _) => {
Niko Matsakis86b6e6e2013-05-10 17:10:35248 //
249 // [pred]
250 // |
251 // v 1
252 // [loopback] <---+
253 // | 4 |
254 // v 3 |
255 // [body] ------+
256 //
257 // [expr] 2
258 //
259 // Note that `break` and `loop` statements
260 // may cause additional edges.
261
Nick Cameronca085402014-11-17 08:39:01262 let loopback = self.add_dummy_node(&[pred]); // 1
James Miller97c17112015-02-19 14:27:25263 let expr_exit = self.add_ast_node(expr.id, &[]); // 2
Niko Matsakis86b6e6e2013-05-10 17:10:35264 self.loop_scopes.push(LoopScope {
265 loop_id: expr.id,
266 continue_index: loopback,
267 break_index: expr_exit,
268 });
Alex Crichton54c2a1e2014-05-16 17:15:33269 let body_exit = self.block(&**body, loopback); // 3
270 self.add_contained_edge(body_exit, loopback); // 4
Niko Matsakis86b6e6e2013-05-10 17:10:35271 self.loop_scopes.pop();
272 expr_exit
273 }
274
Kevin Ballard976438f2014-08-25 21:55:00275 ast::ExprMatch(ref discr, ref arms, _) => {
James Miller4bae1332015-02-19 16:54:41276 self.match_(expr.id, &discr, &arms, pred)
Niko Matsakis86b6e6e2013-05-10 17:10:35277 }
278
Huon Wilson2e888d02015-01-13 03:24:37279 ast::ExprBinary(op, ref l, ref r) if ast_util::lazy_binop(op.node) => {
Niko Matsakis86b6e6e2013-05-10 17:10:35280 //
281 // [pred]
282 // |
283 // v 1
284 // [l]
285 // |
286 // / \
287 // / \
288 // v 2 *
289 // [r] |
290 // | |
291 // v 3 v 4
292 // [..exit..]
293 //
Eduard Burtescub0621282014-09-07 17:09:06294 let l_exit = self.expr(&**l, pred); // 1
295 let r_exit = self.expr(&**r, l_exit); // 2
James Miller97c17112015-02-19 14:27:25296 self.add_ast_node(expr.id, &[l_exit, r_exit]) // 3,4
Niko Matsakis86b6e6e2013-05-10 17:10:35297 }
298
Alex Crichton54c2a1e2014-05-16 17:15:33299 ast::ExprRet(ref v) => {
Eduard Burtescub0621282014-09-07 17:09:06300 let v_exit = self.opt_expr(v, pred);
James Miller97c17112015-02-19 14:27:25301 let b = self.add_ast_node(expr.id, &[v_exit]);
Felix S. Klock II65b65fe2014-05-08 22:07:57302 self.add_returning_edge(expr, b);
James Miller97c17112015-02-19 14:27:25303 self.add_unreachable_node()
Niko Matsakis86b6e6e2013-05-10 17:10:35304 }
305
Marvin Löbel74190852013-09-02 01:45:37306 ast::ExprBreak(label) => {
Niko Matsakis86b6e6e2013-05-10 17:10:35307 let loop_scope = self.find_scope(expr, label);
James Miller97c17112015-02-19 14:27:25308 let b = self.add_ast_node(expr.id, &[pred]);
Felix S. Klock II65b65fe2014-05-08 22:07:57309 self.add_exiting_edge(expr, b,
Niko Matsakis86b6e6e2013-05-10 17:10:35310 loop_scope, loop_scope.break_index);
James Miller97c17112015-02-19 14:27:25311 self.add_unreachable_node()
Niko Matsakis86b6e6e2013-05-10 17:10:35312 }
313
Marvin Löbel74190852013-09-02 01:45:37314 ast::ExprAgain(label) => {
Niko Matsakis86b6e6e2013-05-10 17:10:35315 let loop_scope = self.find_scope(expr, label);
James Miller97c17112015-02-19 14:27:25316 let a = self.add_ast_node(expr.id, &[pred]);
Felix S. Klock II65b65fe2014-05-08 22:07:57317 self.add_exiting_edge(expr, a,
Niko Matsakis86b6e6e2013-05-10 17:10:35318 loop_scope, loop_scope.continue_index);
James Miller97c17112015-02-19 14:27:25319 self.add_unreachable_node()
Niko Matsakis86b6e6e2013-05-10 17:10:35320 }
321
Eduard Burtescu7c48e532014-04-04 10:12:18322 ast::ExprVec(ref elems) => {
Eduard Burtescub0621282014-09-07 17:09:06323 self.straightline(expr, pred, elems.iter().map(|e| &**e))
Niko Matsakis86b6e6e2013-05-10 17:10:35324 }
325
Alex Crichton54c2a1e2014-05-16 17:15:33326 ast::ExprCall(ref func, ref args) => {
Eduard Burtescub0621282014-09-07 17:09:06327 self.call(expr, pred, &**func, args.iter().map(|e| &**e))
Niko Matsakis86b6e6e2013-05-10 17:10:35328 }
329
Eduard Burtescu05e4d942014-02-26 14:06:45330 ast::ExprMethodCall(_, _, ref args) => {
Aaron Turona506d4c2015-01-18 00:15:52331 self.call(expr, pred, &*args[0], args[1..].iter().map(|e| &**e))
Niko Matsakis86b6e6e2013-05-10 17:10:35332 }
333
Alex Crichton54c2a1e2014-05-16 17:15:33334 ast::ExprIndex(ref l, ref r) |
Eduard Burtescub0621282014-09-07 17:09:06335 ast::ExprBinary(_, ref l, ref r) if self.is_method_call(expr) => {
Aaron Turonfc525ee2014-09-15 03:27:36336 self.call(expr, pred, &**l, Some(&**r).into_iter())
Niko Matsakis86b6e6e2013-05-10 17:10:35337 }
338
Nick Cameron17826e12014-12-15 00:17:11339 ast::ExprRange(ref start, ref end) => {
Nick Cameroned8f5032014-12-18 04:55:04340 let fields = start.as_ref().map(|e| &**e).into_iter()
Joshua Landauca7418b2015-06-10 16:22:20341 .chain(end.as_ref().map(|e| &**e));
Nick Cameron17826e12014-12-15 00:17:11342 self.straightline(expr, pred, fields)
Nick Cameron8a357e12014-12-13 05:41:02343 }
344
Eduard Burtescub0621282014-09-07 17:09:06345 ast::ExprUnary(_, ref e) if self.is_method_call(expr) => {
346 self.call(expr, pred, &**e, None::<ast::Expr>.iter())
Niko Matsakis86b6e6e2013-05-10 17:10:35347 }
348
Marvin Löbel74190852013-09-02 01:45:37349 ast::ExprTup(ref exprs) => {
Eduard Burtescub0621282014-09-07 17:09:06350 self.straightline(expr, pred, exprs.iter().map(|e| &**e))
Niko Matsakis86b6e6e2013-05-10 17:10:35351 }
352
Eduard Burtescub0621282014-09-07 17:09:06353 ast::ExprStruct(_, ref fields, ref base) => {
Brandon Sandersond80a62d2014-11-05 23:05:01354 let field_cfg = self.straightline(expr, pred, fields.iter().map(|f| &*f.expr));
355 self.opt_expr(base, field_cfg)
Niko Matsakis86b6e6e2013-05-10 17:10:35356 }
357
Eduard Burtescub0621282014-09-07 17:09:06358 ast::ExprRepeat(ref elem, ref count) => {
359 self.straightline(expr, pred, [elem, count].iter().map(|&e| &**e))
Niko Matsakis86b6e6e2013-05-10 17:10:35360 }
361
Eduard Burtescub0621282014-09-07 17:09:06362 ast::ExprAssign(ref l, ref r) |
363 ast::ExprAssignOp(_, ref l, ref r) => {
364 self.straightline(expr, pred, [r, l].iter().map(|&e| &**e))
Niko Matsakis86b6e6e2013-05-10 17:10:35365 }
366
Felix S. Klock II7d4e7f02014-12-16 13:30:30367 ast::ExprBox(Some(ref l), ref r) |
Eduard Burtescub0621282014-09-07 17:09:06368 ast::ExprIndex(ref l, ref r) |
369 ast::ExprBinary(_, ref l, ref r) => { // NB: && and || handled earlier
370 self.straightline(expr, pred, [l, r].iter().map(|&e| &**e))
Niko Matsakis86b6e6e2013-05-10 17:10:35371 }
372
Felix S. Klock II7d4e7f02014-12-16 13:30:30373 ast::ExprBox(None, ref e) |
Eduard Burtescub0621282014-09-07 17:09:06374 ast::ExprAddrOf(_, ref e) |
375 ast::ExprCast(ref e, _) |
376 ast::ExprUnary(_, ref e) |
377 ast::ExprParen(ref e) |
Adolfo OchagavĂ­a35316972014-11-23 11:14:35378 ast::ExprField(ref e, _) |
379 ast::ExprTupField(ref e, _) => {
Aaron Turonfc525ee2014-09-15 03:27:36380 self.straightline(expr, pred, Some(&**e).into_iter())
Niko Matsakis86b6e6e2013-05-10 17:10:35381 }
382
Felix S. Klock IIbe9c2d12014-05-20 16:49:19383 ast::ExprInlineAsm(ref inline_asm) => {
384 let inputs = inline_asm.inputs.iter();
385 let outputs = inline_asm.outputs.iter();
Felix S. Klock IIbe9c2d12014-05-20 16:49:19386 let post_inputs = self.exprs(inputs.map(|a| {
Sean McArthur44440e52014-12-20 08:09:35387 debug!("cfg::construct InlineAsm id:{} input:{:?}", expr.id, a);
Eduard Burtescub0621282014-09-07 17:09:06388 let &(_, ref expr) = a;
389 &**expr
Felix S. Klock IIbe9c2d12014-05-20 16:49:19390 }), pred);
391 let post_outputs = self.exprs(outputs.map(|a| {
Sean McArthur44440e52014-12-20 08:09:35392 debug!("cfg::construct InlineAsm id:{} output:{:?}", expr.id, a);
Eduard Burtescub0621282014-09-07 17:09:06393 let &(_, ref expr, _) = a;
394 &**expr
Felix S. Klock IIbe9c2d12014-05-20 16:49:19395 }), post_inputs);
James Miller97c17112015-02-19 14:27:25396 self.add_ast_node(expr.id, &[post_outputs])
Felix S. Klock IIbe9c2d12014-05-20 16:49:19397 }
398
Alex Crichtonab387a62013-11-28 20:22:53399 ast::ExprMac(..) |
Niko Matsakis3e2929d2014-11-19 16:18:17400 ast::ExprClosure(..) |
Alex Crichtonab387a62013-11-28 20:22:53401 ast::ExprLit(..) |
Eduard Burtescud31b9eb2015-02-17 17:29:13402 ast::ExprPath(..) => {
Eduard Burtescub0621282014-09-07 17:09:06403 self.straightline(expr, pred, None::<ast::Expr>.iter())
Niko Matsakis86b6e6e2013-05-10 17:10:35404 }
405 }
406 }
407
Jorge Aparicio62ee3f12015-01-02 04:26:38408 fn call<'b, I: Iterator<Item=&'b ast::Expr>>(&mut self,
Eduard Burtescub0621282014-09-07 17:09:06409 call_expr: &ast::Expr,
Niko Matsakis86b6e6e2013-05-10 17:10:35410 pred: CFGIndex,
Eduard Burtescub0621282014-09-07 17:09:06411 func_or_rcvr: &ast::Expr,
412 args: I) -> CFGIndex {
Niko Matsakis7c445612014-11-25 19:21:20413 let method_call = ty::MethodCall::expr(call_expr.id);
Jared Roesch79d02892015-06-24 20:40:54414 let fn_ty = match self.tcx.tables.borrow().method_map.get(&method_call) {
Jakub Bukajcca84e92014-10-24 19:14:37415 Some(method) => method.ty,
Eduard Burtescuad66c212015-06-25 20:42:17416 None => self.tcx.expr_ty_adjusted(func_or_rcvr)
Eduard Burtescu59935f72015-06-24 05:24:13417 };
Jakub Bukajcca84e92014-10-24 19:14:37418
Niko Matsakis86b6e6e2013-05-10 17:10:35419 let func_or_rcvr_exit = self.expr(func_or_rcvr, pred);
Felix S. Klock IIbe9c2d12014-05-20 16:49:19420 let ret = self.straightline(call_expr, func_or_rcvr_exit, args);
Eduard Burtescu59935f72015-06-24 05:24:13421 if fn_ty.fn_ret().diverges() {
James Miller97c17112015-02-19 14:27:25422 self.add_unreachable_node()
Felix S. Klock IIbe9c2d12014-05-20 16:49:19423 } else {
424 ret
425 }
Niko Matsakis86b6e6e2013-05-10 17:10:35426 }
427
Jorge Aparicio62ee3f12015-01-02 04:26:38428 fn exprs<'b, I: Iterator<Item=&'b ast::Expr>>(&mut self,
Aaron Turonb299c2b2014-11-06 17:32:37429 exprs: I,
Eduard Burtescub0621282014-09-07 17:09:06430 pred: CFGIndex) -> CFGIndex {
Niko Matsakis86b6e6e2013-05-10 17:10:35431 //! Constructs graph for `exprs` evaluated in order
Felix S. Klock IIbe9c2d12014-05-20 16:49:19432 exprs.fold(pred, |p, e| self.expr(e, p))
Niko Matsakis86b6e6e2013-05-10 17:10:35433 }
434
435 fn opt_expr(&mut self,
Eduard Burtescub0621282014-09-07 17:09:06436 opt_expr: &Option<P<ast::Expr>>,
Niko Matsakis86b6e6e2013-05-10 17:10:35437 pred: CFGIndex) -> CFGIndex {
438 //! Constructs graph for `opt_expr` evaluated, if Some
Eduard Burtescub0621282014-09-07 17:09:06439 opt_expr.iter().fold(pred, |p, e| self.expr(&**e, p))
Niko Matsakis86b6e6e2013-05-10 17:10:35440 }
441
Jorge Aparicio62ee3f12015-01-02 04:26:38442 fn straightline<'b, I: Iterator<Item=&'b ast::Expr>>(&mut self,
Eduard Burtescub0621282014-09-07 17:09:06443 expr: &ast::Expr,
Niko Matsakis86b6e6e2013-05-10 17:10:35444 pred: CFGIndex,
Eduard Burtescub0621282014-09-07 17:09:06445 subexprs: I) -> CFGIndex {
Niko Matsakis86b6e6e2013-05-10 17:10:35446 //! Handles case of an expression that evaluates `subexprs` in order
447
Eduard Burtescub0621282014-09-07 17:09:06448 let subexprs_exit = self.exprs(subexprs, pred);
James Miller97c17112015-02-19 14:27:25449 self.add_ast_node(expr.id, &[subexprs_exit])
Niko Matsakis86b6e6e2013-05-10 17:10:35450 }
451
James Miller4bae1332015-02-19 16:54:41452 fn match_(&mut self, id: ast::NodeId, discr: &ast::Expr,
453 arms: &[ast::Arm], pred: CFGIndex) -> CFGIndex {
454 // The CFG for match expression is quite complex, so no ASCII
455 // art for it (yet).
456 //
457 // The CFG generated below matches roughly what trans puts
458 // out. Each pattern and guard is visited in parallel, with
459 // arms containing multiple patterns generating multiple nodes
460 // for the same guard expression. The guard expressions chain
461 // into each other from top to bottom, with a specific
462 // exception to allow some additional valid programs
463 // (explained below). Trans differs slightly in that the
464 // pattern matching may continue after a guard but the visible
465 // behaviour should be the same.
466 //
467 // What is going on is explained in further comments.
468
469 // Visit the discriminant expression
470 let discr_exit = self.expr(discr, pred);
471
472 // Add a node for the exit of the match expression as a whole.
473 let expr_exit = self.add_ast_node(id, &[]);
474
475 // Keep track of the previous guard expressions
476 let mut prev_guards = Vec::new();
477 // Track if the previous pattern contained bindings or wildcards
478 let mut prev_has_bindings = false;
479
480 for arm in arms {
481 // Add an exit node for when we've visited all the
482 // patterns and the guard (if there is one) in the arm.
483 let arm_exit = self.add_dummy_node(&[]);
484
485 for pat in &arm.pats {
486 // Visit the pattern, coming from the discriminant exit
487 let mut pat_exit = self.pat(&**pat, discr_exit);
488
489 // If there is a guard expression, handle it here
490 if let Some(ref guard) = arm.guard {
491 // Add a dummy node for the previous guard
492 // expression to target
493 let guard_start = self.add_dummy_node(&[pat_exit]);
494 // Visit the guard expression
495 let guard_exit = self.expr(&**guard, guard_start);
496
497 let this_has_bindings = pat_util::pat_contains_bindings_or_wild(
498 &self.tcx.def_map, &**pat);
499
500 // If both this pattern and the previous pattern
501 // were free of bindings, they must consist only
502 // of "constant" patterns. Note we cannot match an
503 // all-constant pattern, fail the guard, and then
504 // match *another* all-constant pattern. This is
505 // because if the previous pattern matches, then
506 // we *cannot* match this one, unless all the
507 // constants are the same (which is rejected by
508 // `check_match`).
509 //
510 // We can use this to be smarter about the flow
511 // along guards. If the previous pattern matched,
512 // then we know we will not visit the guard in
513 // this one (whether or not the guard succeeded),
514 // if the previous pattern failed, then we know
515 // the guard for that pattern will not have been
516 // visited. Thus, it is not possible to visit both
517 // the previous guard and the current one when
518 // both patterns consist only of constant
519 // sub-patterns.
520 //
521 // However, if the above does not hold, then all
522 // previous guards need to be wired to visit the
523 // current guard pattern.
524 if prev_has_bindings || this_has_bindings {
525 while let Some(prev) = prev_guards.pop() {
526 self.add_contained_edge(prev, guard_start);
527 }
528 }
529
530 prev_has_bindings = this_has_bindings;
531
532 // Push the guard onto the list of previous guards
533 prev_guards.push(guard_exit);
534
535 // Update the exit node for the pattern
536 pat_exit = guard_exit;
537 }
538
539 // Add an edge from the exit of this pattern to the
540 // exit of the arm
541 self.add_contained_edge(pat_exit, arm_exit);
542 }
543
544 // Visit the body of this arm
545 let body_exit = self.expr(&arm.body, arm_exit);
546
547 // Link the body to the exit of the expression
548 self.add_contained_edge(body_exit, expr_exit);
549 }
550
551 expr_exit
552 }
553
Niko Matsakis86b6e6e2013-05-10 17:10:35554 fn add_dummy_node(&mut self, preds: &[CFGIndex]) -> CFGIndex {
James Miller97c17112015-02-19 14:27:25555 self.add_node(CFGNodeData::Dummy, preds)
Niko Matsakis86b6e6e2013-05-10 17:10:35556 }
557
James Miller97c17112015-02-19 14:27:25558 fn add_ast_node(&mut self, id: ast::NodeId, preds: &[CFGIndex]) -> CFGIndex {
James Miller97c17112015-02-19 14:27:25559 assert!(id != ast::DUMMY_NODE_ID);
560 self.add_node(CFGNodeData::AST(id), preds)
561 }
562
563 fn add_unreachable_node(&mut self) -> CFGIndex {
564 self.add_node(CFGNodeData::Unreachable, &[])
565 }
566
567 fn add_node(&mut self, data: CFGNodeData, preds: &[CFGIndex]) -> CFGIndex {
568 let node = self.graph.add_node(data);
Jorge Apariciod5d7e652015-01-31 17:20:46569 for &pred in preds {
Niko Matsakis86b6e6e2013-05-10 17:10:35570 self.add_contained_edge(pred, node);
571 }
572 node
573 }
574
575 fn add_contained_edge(&mut self,
576 source: CFGIndex,
577 target: CFGIndex) {
Huon Wilson7785fe12014-03-19 12:16:56578 let data = CFGEdgeData {exiting_scopes: vec!() };
Niko Matsakis86b6e6e2013-05-10 17:10:35579 self.graph.add_edge(source, target, data);
580 }
581
582 fn add_exiting_edge(&mut self,
Eduard Burtescub0621282014-09-07 17:09:06583 from_expr: &ast::Expr,
Niko Matsakis86b6e6e2013-05-10 17:10:35584 from_index: CFGIndex,
585 to_loop: LoopScope,
586 to_index: CFGIndex) {
Huon Wilson7785fe12014-03-19 12:16:56587 let mut data = CFGEdgeData {exiting_scopes: vec!() };
Felix S. Klock II5ff90872014-11-18 13:22:59588 let mut scope = CodeExtent::from_node_id(from_expr.id);
589 let target_scope = CodeExtent::from_node_id(to_loop.loop_id);
590 while scope != target_scope {
Huon Wilson7785fe12014-03-19 12:16:56591
Felix S. Klock II5ff90872014-11-18 13:22:59592 data.exiting_scopes.push(scope.node_id());
593 scope = self.tcx.region_maps.encl_scope(scope);
Niko Matsakis86b6e6e2013-05-10 17:10:35594 }
595 self.graph.add_edge(from_index, to_index, data);
596 }
597
Felix S. Klock II65b65fe2014-05-08 22:07:57598 fn add_returning_edge(&mut self,
Eduard Burtescub0621282014-09-07 17:09:06599 _from_expr: &ast::Expr,
Felix S. Klock II65b65fe2014-05-08 22:07:57600 from_index: CFGIndex) {
Patrick Walton36195eb2014-05-16 17:45:16601 let mut data = CFGEdgeData {
602 exiting_scopes: vec!(),
603 };
Felix S. Klock II65b65fe2014-05-08 22:07:57604 for &LoopScope { loop_id: id, .. } in self.loop_scopes.iter().rev() {
605 data.exiting_scopes.push(id);
606 }
607 self.graph.add_edge(from_index, self.fn_exit, data);
608 }
609
Niko Matsakis86b6e6e2013-05-10 17:10:35610 fn find_scope(&self,
Eduard Burtescub0621282014-09-07 17:09:06611 expr: &ast::Expr,
Edward Wang386db052014-02-15 08:54:32612 label: Option<ast::Ident>) -> LoopScope {
Eduard Burtescu5a6a9ed2015-02-17 04:44:23613 if label.is_none() {
614 return *self.loop_scopes.last().unwrap();
615 }
Niko Matsakis86b6e6e2013-05-10 17:10:35616
Eduard Burtescu5a6a9ed2015-02-17 04:44:23617 match self.tcx.def_map.borrow().get(&expr.id).map(|d| d.full_def()) {
618 Some(def::DefLabel(loop_id)) => {
619 for l in &self.loop_scopes {
620 if l.loop_id == loop_id {
621 return *l;
Niko Matsakis86b6e6e2013-05-10 17:10:35622 }
623 }
Eduard Burtescu5a6a9ed2015-02-17 04:44:23624 self.tcx.sess.span_bug(expr.span,
625 &format!("no loop scope for id {}", loop_id));
626 }
627
628 r => {
629 self.tcx.sess.span_bug(expr.span,
630 &format!("bad entry `{:?}` in def_map for label", r));
Niko Matsakis86b6e6e2013-05-10 17:10:35631 }
632 }
633 }
634
Marvin Löbel74190852013-09-02 01:45:37635 fn is_method_call(&self, expr: &ast::Expr) -> bool {
Niko Matsakis7c445612014-11-25 19:21:20636 let method_call = ty::MethodCall::expr(expr.id);
Jared Roesch79d02892015-06-24 20:40:54637 self.tcx.tables.borrow().method_map.contains_key(&method_call)
Niko Matsakis86b6e6e2013-05-10 17:10:35638 }
Patrick Walton99d44d22013-07-10 02:32:09639}