blob: 1b2976b7435d8e38ed66dc9cf9d9d2921aaa6324 [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;
Eduard Burtescu5efdde02016-03-22 15:30:5712use cfg::*;
Eduard Burtescuffca6c32016-03-29 09:54:2613use hir::def::Def;
14use hir::pat_util;
Eduard Burtescu5efdde02016-03-22 15:30:5715use ty::{self, TyCtxt};
Niko Matsakis86b6e6e2013-05-10 17:10:3516use syntax::ast;
Eduard Burtescub0621282014-09-07 17:09:0617use syntax::ptr::P;
Niko Matsakis86b6e6e2013-05-10 17:10:3518
Eduard Burtescu8b093722016-03-29 05:50:4419use hir::{self, PatKind};
Nick Cameronfacdf2e2015-07-31 07:04:0620
Eduard Burtescu28be6952014-04-22 12:56:3721struct CFGBuilder<'a, 'tcx: 'a> {
Eduard Burtescu76affa52016-05-03 02:23:2222 tcx: TyCtxt<'a, 'tcx, '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 Burtescu76affa52016-05-03 02:23:2235pub fn construct<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
36 blk: &hir::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> {
Nick Cameronfacdf2e2015-07-31 07:04:0662 fn block(&mut self, blk: &hir::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 {
Vadim Petrochenkovca88e9c2015-12-07 14:17:4165 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
Nick Cameronfacdf2e2015-07-31 07:04:0673 fn stmt(&mut self, stmt: &hir::Stmt, pred: CFGIndex) -> CFGIndex {
Niko Matsakis86b6e6e2013-05-10 17:10:3574 match stmt.node {
Nick Cameronfacdf2e2015-07-31 07:04:0675 hir::StmtDecl(ref decl, id) => {
Jonas Schievink559fca02016-02-09 21:00:2076 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
Nick Cameronfacdf2e2015-07-31 07:04:0680 hir::StmtExpr(ref expr, id) | hir::StmtSemi(ref expr, id) => {
Jonas Schievink559fca02016-02-09 21:00:2081 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 }
Niko Matsakis86b6e6e2013-05-10 17:10:3584 }
85 }
86
Nick Cameronfacdf2e2015-07-31 07:04:0687 fn decl(&mut self, decl: &hir::Decl, pred: CFGIndex) -> CFGIndex {
Niko Matsakis86b6e6e2013-05-10 17:10:3588 match decl.node {
Nick Cameronfacdf2e2015-07-31 07:04:0689 hir::DeclLocal(ref local) => {
Eduard Burtescub0621282014-09-07 17:09:0690 let init_exit = self.opt_expr(&local.init, pred);
Jonas Schievink559fca02016-02-09 21:00:2091 self.pat(&local.pat, init_exit)
Niko Matsakis86b6e6e2013-05-10 17:10:3592 }
93
Nick Cameronfacdf2e2015-07-31 07:04:0694 hir::DeclItem(_) => {
Niko Matsakis86b6e6e2013-05-10 17:10:3595 pred
96 }
97 }
98 }
99
Nick Cameronfacdf2e2015-07-31 07:04:06100 fn pat(&mut self, pat: &hir::Pat, pred: CFGIndex) -> CFGIndex {
Niko Matsakis86b6e6e2013-05-10 17:10:35101 match pat.node {
Vadim Petrochenkove05e74a2016-08-26 16:23:42102 PatKind::Binding(.., None) |
Vadim Petrochenkov06755d92016-02-15 21:40:38103 PatKind::Path(..) |
Vadim Petrochenkov9b40e1e2016-02-14 12:25:12104 PatKind::Lit(..) |
105 PatKind::Range(..) |
106 PatKind::Wild => {
James Miller97c17112015-02-19 14:27:25107 self.add_ast_node(pat.id, &[pred])
Niko Matsakis86b6e6e2013-05-10 17:10:35108 }
109
Vadim Petrochenkov9b40e1e2016-02-14 12:25:12110 PatKind::Box(ref subpat) |
111 PatKind::Ref(ref subpat, _) |
Vadim Petrochenkove05e74a2016-08-26 16:23:42112 PatKind::Binding(.., Some(ref subpat)) => {
Jonas Schievink559fca02016-02-09 21:00:20113 let subpat_exit = self.pat(&subpat, pred);
James Miller97c17112015-02-19 14:27:25114 self.add_ast_node(pat.id, &[subpat_exit])
Niko Matsakis86b6e6e2013-05-10 17:10:35115 }
116
Vadim Petrochenkovd69aeaf2016-03-06 12:54:44117 PatKind::TupleStruct(_, ref subpats, _) |
118 PatKind::Tuple(ref subpats, _) => {
Eduard Burtescub0621282014-09-07 17:09:06119 let pats_exit = self.pats_all(subpats.iter(), pred);
James Miller97c17112015-02-19 14:27:25120 self.add_ast_node(pat.id, &[pats_exit])
Niko Matsakis86b6e6e2013-05-10 17:10:35121 }
122
Vadim Petrochenkov9b40e1e2016-02-14 12:25:12123 PatKind::Struct(_, ref subpats, _) => {
Niko Matsakis86b6e6e2013-05-10 17:10:35124 let pats_exit =
P1startead6c4b2014-10-06 00:36:53125 self.pats_all(subpats.iter().map(|f| &f.node.pat), pred);
James Miller97c17112015-02-19 14:27:25126 self.add_ast_node(pat.id, &[pats_exit])
Niko Matsakis86b6e6e2013-05-10 17:10:35127 }
128
Jonas Schievinkcf0b7bd2016-09-20 00:14:46129 PatKind::Slice(ref pre, ref vec, ref post) => {
Eduard Burtescub0621282014-09-07 17:09:06130 let pre_exit = self.pats_all(pre.iter(), pred);
131 let vec_exit = self.pats_all(vec.iter(), pre_exit);
132 let post_exit = self.pats_all(post.iter(), vec_exit);
James Miller97c17112015-02-19 14:27:25133 self.add_ast_node(pat.id, &[post_exit])
Niko Matsakis86b6e6e2013-05-10 17:10:35134 }
135 }
136 }
137
Nick Cameronfacdf2e2015-07-31 07:04:06138 fn pats_all<'b, I: Iterator<Item=&'b P<hir::Pat>>>(&mut self,
Eduard Burtescub0621282014-09-07 17:09:06139 pats: I,
140 pred: CFGIndex) -> CFGIndex {
Niko Matsakis86b6e6e2013-05-10 17:10:35141 //! Handles case where all of the patterns must match.
Jonas Schievink559fca02016-02-09 21:00:20142 pats.fold(pred, |pred, pat| self.pat(&pat, pred))
Niko Matsakis86b6e6e2013-05-10 17:10:35143 }
144
Nick Cameronfacdf2e2015-07-31 07:04:06145 fn expr(&mut self, expr: &hir::Expr, pred: CFGIndex) -> CFGIndex {
Niko Matsakis86b6e6e2013-05-10 17:10:35146 match expr.node {
Nick Cameronfacdf2e2015-07-31 07:04:06147 hir::ExprBlock(ref blk) => {
Jonas Schievink559fca02016-02-09 21:00:20148 let blk_exit = self.block(&blk, pred);
James Miller97c17112015-02-19 14:27:25149 self.add_ast_node(expr.id, &[blk_exit])
Niko Matsakis86b6e6e2013-05-10 17:10:35150 }
151
Nick Cameronfacdf2e2015-07-31 07:04:06152 hir::ExprIf(ref cond, ref then, None) => {
Niko Matsakis86b6e6e2013-05-10 17:10:35153 //
154 // [pred]
155 // |
156 // v 1
157 // [cond]
158 // |
159 // / \
160 // / \
161 // v 2 *
162 // [then] |
163 // | |
164 // v 3 v 4
165 // [..expr..]
166 //
Jonas Schievink559fca02016-02-09 21:00:20167 let cond_exit = self.expr(&cond, pred); // 1
168 let then_exit = self.block(&then, cond_exit); // 2
James Miller97c17112015-02-19 14:27:25169 self.add_ast_node(expr.id, &[cond_exit, then_exit]) // 3,4
Niko Matsakis86b6e6e2013-05-10 17:10:35170 }
171
Nick Cameronfacdf2e2015-07-31 07:04:06172 hir::ExprIf(ref cond, ref then, Some(ref otherwise)) => {
Niko Matsakis86b6e6e2013-05-10 17:10:35173 //
174 // [pred]
175 // |
176 // v 1
177 // [cond]
178 // |
179 // / \
180 // / \
181 // v 2 v 3
182 // [then][otherwise]
183 // | |
184 // v 4 v 5
185 // [..expr..]
186 //
Jonas Schievink559fca02016-02-09 21:00:20187 let cond_exit = self.expr(&cond, pred); // 1
188 let then_exit = self.block(&then, cond_exit); // 2
189 let else_exit = self.expr(&otherwise, cond_exit); // 3
James Miller97c17112015-02-19 14:27:25190 self.add_ast_node(expr.id, &[then_exit, else_exit]) // 4, 5
Niko Matsakis86b6e6e2013-05-10 17:10:35191 }
192
Nick Cameronfacdf2e2015-07-31 07:04:06193 hir::ExprWhile(ref cond, ref body, _) => {
Niko Matsakis86b6e6e2013-05-10 17:10:35194 //
195 // [pred]
196 // |
197 // v 1
198 // [loopback] <--+ 5
199 // | |
200 // v 2 |
201 // +-----[cond] |
202 // | | |
203 // | v 4 |
204 // | [body] -----+
205 // v 3
206 // [expr]
207 //
Patrick Waltoncaa564b2014-07-22 03:54:28208 // Note that `break` and `continue` statements
Niko Matsakis86b6e6e2013-05-10 17:10:35209 // may cause additional edges.
210
Michael Sullivan7dbc5ae2013-07-30 23:47:22211 // Is the condition considered part of the loop?
Nick Cameronca085402014-11-17 08:39:01212 let loopback = self.add_dummy_node(&[pred]); // 1
Jonas Schievink559fca02016-02-09 21:00:20213 let cond_exit = self.expr(&cond, loopback); // 2
James Miller97c17112015-02-19 14:27:25214 let expr_exit = self.add_ast_node(expr.id, &[cond_exit]); // 3
Niko Matsakis86b6e6e2013-05-10 17:10:35215 self.loop_scopes.push(LoopScope {
216 loop_id: expr.id,
217 continue_index: loopback,
218 break_index: expr_exit
219 });
Jonas Schievink559fca02016-02-09 21:00:20220 let body_exit = self.block(&body, cond_exit); // 4
Alex Crichton54c2a1e2014-05-16 17:15:33221 self.add_contained_edge(body_exit, loopback); // 5
Felix S. Klock IIbe9c2d12014-05-20 16:49:19222 self.loop_scopes.pop();
Niko Matsakis86b6e6e2013-05-10 17:10:35223 expr_exit
224 }
225
Nick Cameronfacdf2e2015-07-31 07:04:06226 hir::ExprLoop(ref body, _) => {
Niko Matsakis86b6e6e2013-05-10 17:10:35227 //
228 // [pred]
229 // |
230 // v 1
231 // [loopback] <---+
232 // | 4 |
233 // v 3 |
234 // [body] ------+
235 //
236 // [expr] 2
237 //
238 // Note that `break` and `loop` statements
239 // may cause additional edges.
240
Nick Cameronca085402014-11-17 08:39:01241 let loopback = self.add_dummy_node(&[pred]); // 1
James Miller97c17112015-02-19 14:27:25242 let expr_exit = self.add_ast_node(expr.id, &[]); // 2
Niko Matsakis86b6e6e2013-05-10 17:10:35243 self.loop_scopes.push(LoopScope {
244 loop_id: expr.id,
245 continue_index: loopback,
246 break_index: expr_exit,
247 });
Jonas Schievink559fca02016-02-09 21:00:20248 let body_exit = self.block(&body, loopback); // 3
Alex Crichton54c2a1e2014-05-16 17:15:33249 self.add_contained_edge(body_exit, loopback); // 4
Niko Matsakis86b6e6e2013-05-10 17:10:35250 self.loop_scopes.pop();
251 expr_exit
252 }
253
Nick Cameronfacdf2e2015-07-31 07:04:06254 hir::ExprMatch(ref discr, ref arms, _) => {
James Miller4bae1332015-02-19 16:54:41255 self.match_(expr.id, &discr, &arms, pred)
Niko Matsakis86b6e6e2013-05-10 17:10:35256 }
257
Eduard Burtescuef4c7242016-03-29 06:32:58258 hir::ExprBinary(op, ref l, ref r) if op.node.is_lazy() => {
Niko Matsakis86b6e6e2013-05-10 17:10:35259 //
260 // [pred]
261 // |
262 // v 1
263 // [l]
264 // |
265 // / \
266 // / \
267 // v 2 *
268 // [r] |
269 // | |
270 // v 3 v 4
271 // [..exit..]
272 //
Jonas Schievink559fca02016-02-09 21:00:20273 let l_exit = self.expr(&l, pred); // 1
274 let r_exit = self.expr(&r, l_exit); // 2
James Miller97c17112015-02-19 14:27:25275 self.add_ast_node(expr.id, &[l_exit, r_exit]) // 3,4
Niko Matsakis86b6e6e2013-05-10 17:10:35276 }
277
Nick Cameronfacdf2e2015-07-31 07:04:06278 hir::ExprRet(ref v) => {
Eduard Burtescub0621282014-09-07 17:09:06279 let v_exit = self.opt_expr(v, pred);
James Miller97c17112015-02-19 14:27:25280 let b = self.add_ast_node(expr.id, &[v_exit]);
Felix S. Klock II65b65fe2014-05-08 22:07:57281 self.add_returning_edge(expr, b);
James Miller97c17112015-02-19 14:27:25282 self.add_unreachable_node()
Niko Matsakis86b6e6e2013-05-10 17:10:35283 }
284
Nick Cameronfacdf2e2015-07-31 07:04:06285 hir::ExprBreak(label) => {
Vadim Petrochenkovaad347c2016-03-06 12:54:44286 let loop_scope = self.find_scope(expr, label.map(|l| l.node));
James Miller97c17112015-02-19 14:27:25287 let b = self.add_ast_node(expr.id, &[pred]);
Felix S. Klock II65b65fe2014-05-08 22:07:57288 self.add_exiting_edge(expr, b,
Niko Matsakis86b6e6e2013-05-10 17:10:35289 loop_scope, loop_scope.break_index);
James Miller97c17112015-02-19 14:27:25290 self.add_unreachable_node()
Niko Matsakis86b6e6e2013-05-10 17:10:35291 }
292
Nick Cameronfacdf2e2015-07-31 07:04:06293 hir::ExprAgain(label) => {
Vadim Petrochenkovaad347c2016-03-06 12:54:44294 let loop_scope = self.find_scope(expr, label.map(|l| l.node));
James Miller97c17112015-02-19 14:27:25295 let a = self.add_ast_node(expr.id, &[pred]);
Felix S. Klock II65b65fe2014-05-08 22:07:57296 self.add_exiting_edge(expr, a,
Niko Matsakis86b6e6e2013-05-10 17:10:35297 loop_scope, loop_scope.continue_index);
James Miller97c17112015-02-19 14:27:25298 self.add_unreachable_node()
Niko Matsakis86b6e6e2013-05-10 17:10:35299 }
300
Jonas Schievinkcf0b7bd2016-09-20 00:14:46301 hir::ExprArray(ref elems) => {
Eduard Burtescub0621282014-09-07 17:09:06302 self.straightline(expr, pred, elems.iter().map(|e| &**e))
Niko Matsakis86b6e6e2013-05-10 17:10:35303 }
304
Nick Cameronfacdf2e2015-07-31 07:04:06305 hir::ExprCall(ref func, ref args) => {
Jonas Schievink559fca02016-02-09 21:00:20306 self.call(expr, pred, &func, args.iter().map(|e| &**e))
Niko Matsakis86b6e6e2013-05-10 17:10:35307 }
308
Vadim Petrochenkove05e74a2016-08-26 16:23:42309 hir::ExprMethodCall(.., ref args) => {
Jonas Schievink559fca02016-02-09 21:00:20310 self.call(expr, pred, &args[0], args[1..].iter().map(|e| &**e))
Niko Matsakis86b6e6e2013-05-10 17:10:35311 }
312
Nick Cameronfacdf2e2015-07-31 07:04:06313 hir::ExprIndex(ref l, ref r) |
314 hir::ExprBinary(_, ref l, ref r) if self.tcx.is_method_call(expr.id) => {
Jonas Schievink559fca02016-02-09 21:00:20315 self.call(expr, pred, &l, Some(&**r).into_iter())
Niko Matsakis86b6e6e2013-05-10 17:10:35316 }
317
Nick Cameronfacdf2e2015-07-31 07:04:06318 hir::ExprUnary(_, ref e) if self.tcx.is_method_call(expr.id) => {
Jonas Schievink559fca02016-02-09 21:00:20319 self.call(expr, pred, &e, None::<hir::Expr>.iter())
Niko Matsakis86b6e6e2013-05-10 17:10:35320 }
321
Nick Cameronfacdf2e2015-07-31 07:04:06322 hir::ExprTup(ref exprs) => {
Eduard Burtescub0621282014-09-07 17:09:06323 self.straightline(expr, pred, exprs.iter().map(|e| &**e))
Niko Matsakis86b6e6e2013-05-10 17:10:35324 }
325
Nick Cameronfacdf2e2015-07-31 07:04:06326 hir::ExprStruct(_, ref fields, ref base) => {
Brandon Sandersond80a62d2014-11-05 23:05:01327 let field_cfg = self.straightline(expr, pred, fields.iter().map(|f| &*f.expr));
328 self.opt_expr(base, field_cfg)
Niko Matsakis86b6e6e2013-05-10 17:10:35329 }
330
Nick Cameronfacdf2e2015-07-31 07:04:06331 hir::ExprRepeat(ref elem, ref count) => {
Eduard Burtescub0621282014-09-07 17:09:06332 self.straightline(expr, pred, [elem, count].iter().map(|&e| &**e))
Niko Matsakis86b6e6e2013-05-10 17:10:35333 }
334
Nick Cameronfacdf2e2015-07-31 07:04:06335 hir::ExprAssign(ref l, ref r) |
336 hir::ExprAssignOp(_, ref l, ref r) => {
Eduard Burtescub0621282014-09-07 17:09:06337 self.straightline(expr, pred, [r, l].iter().map(|&e| &**e))
Niko Matsakis86b6e6e2013-05-10 17:10:35338 }
339
Nick Cameronfacdf2e2015-07-31 07:04:06340 hir::ExprIndex(ref l, ref r) |
341 hir::ExprBinary(_, ref l, ref r) => { // NB: && and || handled earlier
Eduard Burtescub0621282014-09-07 17:09:06342 self.straightline(expr, pred, [l, r].iter().map(|&e| &**e))
Niko Matsakis86b6e6e2013-05-10 17:10:35343 }
344
Eduard Burtescuf293ea22015-09-24 15:00:08345 hir::ExprBox(ref e) |
Nick Cameronfacdf2e2015-07-31 07:04:06346 hir::ExprAddrOf(_, ref e) |
347 hir::ExprCast(ref e, _) |
Eduard Burtescub8157cc2015-02-01 07:59:46348 hir::ExprType(ref e, _) |
Nick Cameronfacdf2e2015-07-31 07:04:06349 hir::ExprUnary(_, ref e) |
Nick Cameronfacdf2e2015-07-31 07:04:06350 hir::ExprField(ref e, _) |
351 hir::ExprTupField(ref e, _) => {
Aaron Turonfc525ee2014-09-15 03:27:36352 self.straightline(expr, pred, Some(&**e).into_iter())
Niko Matsakis86b6e6e2013-05-10 17:10:35353 }
354
Eduard Burtescu856185d2016-03-09 20:17:02355 hir::ExprInlineAsm(_, ref outputs, ref inputs) => {
356 let post_outputs = self.exprs(outputs.iter().map(|e| &**e), pred);
357 let post_inputs = self.exprs(inputs.iter().map(|e| &**e), post_outputs);
358 self.add_ast_node(expr.id, &[post_inputs])
Felix S. Klock IIbe9c2d12014-05-20 16:49:19359 }
360
Nick Cameronfacdf2e2015-07-31 07:04:06361 hir::ExprClosure(..) |
362 hir::ExprLit(..) |
363 hir::ExprPath(..) => {
364 self.straightline(expr, pred, None::<hir::Expr>.iter())
Niko Matsakis86b6e6e2013-05-10 17:10:35365 }
366 }
367 }
368
Nick Cameronfacdf2e2015-07-31 07:04:06369 fn call<'b, I: Iterator<Item=&'b hir::Expr>>(&mut self,
370 call_expr: &hir::Expr,
Niko Matsakis86b6e6e2013-05-10 17:10:35371 pred: CFGIndex,
Nick Cameronfacdf2e2015-07-31 07:04:06372 func_or_rcvr: &hir::Expr,
Eduard Burtescub0621282014-09-07 17:09:06373 args: I) -> CFGIndex {
Niko Matsakis7c445612014-11-25 19:21:20374 let method_call = ty::MethodCall::expr(call_expr.id);
Jared Roesch79d02892015-06-24 20:40:54375 let fn_ty = match self.tcx.tables.borrow().method_map.get(&method_call) {
Jakub Bukajcca84e92014-10-24 19:14:37376 Some(method) => method.ty,
Eduard Burtescuad66c212015-06-25 20:42:17377 None => self.tcx.expr_ty_adjusted(func_or_rcvr)
Eduard Burtescu59935f72015-06-24 05:24:13378 };
Jakub Bukajcca84e92014-10-24 19:14:37379
Niko Matsakis86b6e6e2013-05-10 17:10:35380 let func_or_rcvr_exit = self.expr(func_or_rcvr, pred);
Felix S. Klock IIbe9c2d12014-05-20 16:49:19381 let ret = self.straightline(call_expr, func_or_rcvr_exit, args);
Andrew Cannfadabe02016-08-02 07:56:20382 // FIXME(canndrew): This is_never should probably be an is_uninhabited.
383 if fn_ty.fn_ret().0.is_never() {
James Miller97c17112015-02-19 14:27:25384 self.add_unreachable_node()
Felix S. Klock IIbe9c2d12014-05-20 16:49:19385 } else {
386 ret
387 }
Niko Matsakis86b6e6e2013-05-10 17:10:35388 }
389
Nick Cameronfacdf2e2015-07-31 07:04:06390 fn exprs<'b, I: Iterator<Item=&'b hir::Expr>>(&mut self,
Aaron Turonb299c2b2014-11-06 17:32:37391 exprs: I,
Eduard Burtescub0621282014-09-07 17:09:06392 pred: CFGIndex) -> CFGIndex {
Niko Matsakis86b6e6e2013-05-10 17:10:35393 //! Constructs graph for `exprs` evaluated in order
Felix S. Klock IIbe9c2d12014-05-20 16:49:19394 exprs.fold(pred, |p, e| self.expr(e, p))
Niko Matsakis86b6e6e2013-05-10 17:10:35395 }
396
397 fn opt_expr(&mut self,
Nick Cameronfacdf2e2015-07-31 07:04:06398 opt_expr: &Option<P<hir::Expr>>,
Niko Matsakis86b6e6e2013-05-10 17:10:35399 pred: CFGIndex) -> CFGIndex {
400 //! Constructs graph for `opt_expr` evaluated, if Some
Jonas Schievink559fca02016-02-09 21:00:20401 opt_expr.iter().fold(pred, |p, e| self.expr(&e, p))
Niko Matsakis86b6e6e2013-05-10 17:10:35402 }
403
Nick Cameronfacdf2e2015-07-31 07:04:06404 fn straightline<'b, I: Iterator<Item=&'b hir::Expr>>(&mut self,
405 expr: &hir::Expr,
Niko Matsakis86b6e6e2013-05-10 17:10:35406 pred: CFGIndex,
Eduard Burtescub0621282014-09-07 17:09:06407 subexprs: I) -> CFGIndex {
Niko Matsakis86b6e6e2013-05-10 17:10:35408 //! Handles case of an expression that evaluates `subexprs` in order
409
Eduard Burtescub0621282014-09-07 17:09:06410 let subexprs_exit = self.exprs(subexprs, pred);
James Miller97c17112015-02-19 14:27:25411 self.add_ast_node(expr.id, &[subexprs_exit])
Niko Matsakis86b6e6e2013-05-10 17:10:35412 }
413
Nick Cameronfacdf2e2015-07-31 07:04:06414 fn match_(&mut self, id: ast::NodeId, discr: &hir::Expr,
415 arms: &[hir::Arm], pred: CFGIndex) -> CFGIndex {
James Miller4bae1332015-02-19 16:54:41416 // The CFG for match expression is quite complex, so no ASCII
417 // art for it (yet).
418 //
419 // The CFG generated below matches roughly what trans puts
420 // out. Each pattern and guard is visited in parallel, with
421 // arms containing multiple patterns generating multiple nodes
422 // for the same guard expression. The guard expressions chain
423 // into each other from top to bottom, with a specific
424 // exception to allow some additional valid programs
425 // (explained below). Trans differs slightly in that the
426 // pattern matching may continue after a guard but the visible
427 // behaviour should be the same.
428 //
429 // What is going on is explained in further comments.
430
431 // Visit the discriminant expression
432 let discr_exit = self.expr(discr, pred);
433
434 // Add a node for the exit of the match expression as a whole.
435 let expr_exit = self.add_ast_node(id, &[]);
436
437 // Keep track of the previous guard expressions
438 let mut prev_guards = Vec::new();
439 // Track if the previous pattern contained bindings or wildcards
440 let mut prev_has_bindings = false;
441
442 for arm in arms {
443 // Add an exit node for when we've visited all the
444 // patterns and the guard (if there is one) in the arm.
445 let arm_exit = self.add_dummy_node(&[]);
446
447 for pat in &arm.pats {
448 // Visit the pattern, coming from the discriminant exit
Jonas Schievink559fca02016-02-09 21:00:20449 let mut pat_exit = self.pat(&pat, discr_exit);
James Miller4bae1332015-02-19 16:54:41450
451 // If there is a guard expression, handle it here
452 if let Some(ref guard) = arm.guard {
453 // Add a dummy node for the previous guard
454 // expression to target
455 let guard_start = self.add_dummy_node(&[pat_exit]);
456 // Visit the guard expression
Jonas Schievink559fca02016-02-09 21:00:20457 let guard_exit = self.expr(&guard, guard_start);
James Miller4bae1332015-02-19 16:54:41458
Vadim Petrochenkovcf468202016-03-06 12:54:44459 let this_has_bindings = pat_util::pat_contains_bindings_or_wild(&pat);
James Miller4bae1332015-02-19 16:54:41460
461 // If both this pattern and the previous pattern
462 // were free of bindings, they must consist only
463 // of "constant" patterns. Note we cannot match an
464 // all-constant pattern, fail the guard, and then
465 // match *another* all-constant pattern. This is
466 // because if the previous pattern matches, then
467 // we *cannot* match this one, unless all the
468 // constants are the same (which is rejected by
469 // `check_match`).
470 //
471 // We can use this to be smarter about the flow
472 // along guards. If the previous pattern matched,
473 // then we know we will not visit the guard in
474 // this one (whether or not the guard succeeded),
475 // if the previous pattern failed, then we know
476 // the guard for that pattern will not have been
477 // visited. Thus, it is not possible to visit both
478 // the previous guard and the current one when
479 // both patterns consist only of constant
480 // sub-patterns.
481 //
482 // However, if the above does not hold, then all
483 // previous guards need to be wired to visit the
484 // current guard pattern.
485 if prev_has_bindings || this_has_bindings {
486 while let Some(prev) = prev_guards.pop() {
487 self.add_contained_edge(prev, guard_start);
488 }
489 }
490
491 prev_has_bindings = this_has_bindings;
492
493 // Push the guard onto the list of previous guards
494 prev_guards.push(guard_exit);
495
496 // Update the exit node for the pattern
497 pat_exit = guard_exit;
498 }
499
500 // Add an edge from the exit of this pattern to the
501 // exit of the arm
502 self.add_contained_edge(pat_exit, arm_exit);
503 }
504
505 // Visit the body of this arm
506 let body_exit = self.expr(&arm.body, arm_exit);
507
508 // Link the body to the exit of the expression
509 self.add_contained_edge(body_exit, expr_exit);
510 }
511
512 expr_exit
513 }
514
Niko Matsakis86b6e6e2013-05-10 17:10:35515 fn add_dummy_node(&mut self, preds: &[CFGIndex]) -> CFGIndex {
James Miller97c17112015-02-19 14:27:25516 self.add_node(CFGNodeData::Dummy, preds)
Niko Matsakis86b6e6e2013-05-10 17:10:35517 }
518
James Miller97c17112015-02-19 14:27:25519 fn add_ast_node(&mut self, id: ast::NodeId, preds: &[CFGIndex]) -> CFGIndex {
James Miller97c17112015-02-19 14:27:25520 assert!(id != ast::DUMMY_NODE_ID);
521 self.add_node(CFGNodeData::AST(id), preds)
522 }
523
524 fn add_unreachable_node(&mut self) -> CFGIndex {
525 self.add_node(CFGNodeData::Unreachable, &[])
526 }
527
528 fn add_node(&mut self, data: CFGNodeData, preds: &[CFGIndex]) -> CFGIndex {
529 let node = self.graph.add_node(data);
Jorge Apariciod5d7e652015-01-31 17:20:46530 for &pred in preds {
Niko Matsakis86b6e6e2013-05-10 17:10:35531 self.add_contained_edge(pred, node);
532 }
533 node
534 }
535
536 fn add_contained_edge(&mut self,
537 source: CFGIndex,
538 target: CFGIndex) {
iirelue593c3b2016-10-29 21:54:04539 let data = CFGEdgeData {exiting_scopes: vec![] };
Niko Matsakis86b6e6e2013-05-10 17:10:35540 self.graph.add_edge(source, target, data);
541 }
542
543 fn add_exiting_edge(&mut self,
Nick Cameronfacdf2e2015-07-31 07:04:06544 from_expr: &hir::Expr,
Niko Matsakis86b6e6e2013-05-10 17:10:35545 from_index: CFGIndex,
546 to_loop: LoopScope,
547 to_index: CFGIndex) {
iirelue593c3b2016-10-29 21:54:04548 let mut data = CFGEdgeData {exiting_scopes: vec![] };
Ariel Ben-Yehudafc304382015-08-19 22:46:28549 let mut scope = self.tcx.region_maps.node_extent(from_expr.id);
550 let target_scope = self.tcx.region_maps.node_extent(to_loop.loop_id);
Felix S. Klock II5ff90872014-11-18 13:22:59551 while scope != target_scope {
Ariel Ben-Yehudafc304382015-08-19 22:46:28552 data.exiting_scopes.push(scope.node_id(&self.tcx.region_maps));
Felix S. Klock II5ff90872014-11-18 13:22:59553 scope = self.tcx.region_maps.encl_scope(scope);
Niko Matsakis86b6e6e2013-05-10 17:10:35554 }
555 self.graph.add_edge(from_index, to_index, data);
556 }
557
Felix S. Klock II65b65fe2014-05-08 22:07:57558 fn add_returning_edge(&mut self,
Nick Cameronfacdf2e2015-07-31 07:04:06559 _from_expr: &hir::Expr,
Felix S. Klock II65b65fe2014-05-08 22:07:57560 from_index: CFGIndex) {
Patrick Walton36195eb2014-05-16 17:45:16561 let mut data = CFGEdgeData {
iirelue593c3b2016-10-29 21:54:04562 exiting_scopes: vec![],
Patrick Walton36195eb2014-05-16 17:45:16563 };
Felix S. Klock II65b65fe2014-05-08 22:07:57564 for &LoopScope { loop_id: id, .. } in self.loop_scopes.iter().rev() {
565 data.exiting_scopes.push(id);
566 }
567 self.graph.add_edge(from_index, self.fn_exit, data);
568 }
569
Niko Matsakis86b6e6e2013-05-10 17:10:35570 fn find_scope(&self,
Nick Cameronfacdf2e2015-07-31 07:04:06571 expr: &hir::Expr,
Vadim Petrochenkov40ce8042015-09-23 17:04:49572 label: Option<ast::Name>) -> LoopScope {
Eduard Burtescu5a6a9ed2015-02-17 04:44:23573 if label.is_none() {
574 return *self.loop_scopes.last().unwrap();
575 }
Niko Matsakis86b6e6e2013-05-10 17:10:35576
Vadim Petrochenkovee4e5532016-06-03 20:15:00577 match self.tcx.expect_def(expr.id) {
578 Def::Label(loop_id) => {
Eduard Burtescu5a6a9ed2015-02-17 04:44:23579 for l in &self.loop_scopes {
580 if l.loop_id == loop_id {
581 return *l;
Niko Matsakis86b6e6e2013-05-10 17:10:35582 }
583 }
Benjamin Herrbcdaccf2016-03-25 17:31:27584 span_bug!(expr.span, "no loop scope for id {}", loop_id);
Eduard Burtescu5a6a9ed2015-02-17 04:44:23585 }
586
587 r => {
Benjamin Herrbcdaccf2016-03-25 17:31:27588 span_bug!(expr.span, "bad entry `{:?}` in def_map for label", r);
Niko Matsakis86b6e6e2013-05-10 17:10:35589 }
590 }
591 }
Patrick Walton99d44d22013-07-10 02:32:09592}