blob: af47617ea92fda64456e8551e0d74591e7103159 [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 Petrochenkov9b40e1e2016-02-14 12:25:12102 PatKind::Ident(_, _, None) |
Vadim Petrochenkov06755d92016-02-15 21:40:38103 PatKind::Path(..) |
Vadim Petrochenkov9b40e1e2016-02-14 12:25:12104 PatKind::QPath(..) |
105 PatKind::Lit(..) |
106 PatKind::Range(..) |
107 PatKind::Wild => {
James Miller97c17112015-02-19 14:27:25108 self.add_ast_node(pat.id, &[pred])
Niko Matsakis86b6e6e2013-05-10 17:10:35109 }
110
Vadim Petrochenkov9b40e1e2016-02-14 12:25:12111 PatKind::Box(ref subpat) |
112 PatKind::Ref(ref subpat, _) |
113 PatKind::Ident(_, _, Some(ref subpat)) => {
Jonas Schievink559fca02016-02-09 21:00:20114 let subpat_exit = self.pat(&subpat, pred);
James Miller97c17112015-02-19 14:27:25115 self.add_ast_node(pat.id, &[subpat_exit])
Niko Matsakis86b6e6e2013-05-10 17:10:35116 }
117
Vadim Petrochenkovd69aeaf2016-03-06 12:54:44118 PatKind::TupleStruct(_, ref subpats, _) |
119 PatKind::Tuple(ref subpats, _) => {
Eduard Burtescub0621282014-09-07 17:09:06120 let pats_exit = self.pats_all(subpats.iter(), pred);
James Miller97c17112015-02-19 14:27:25121 self.add_ast_node(pat.id, &[pats_exit])
Niko Matsakis86b6e6e2013-05-10 17:10:35122 }
123
Vadim Petrochenkov9b40e1e2016-02-14 12:25:12124 PatKind::Struct(_, ref subpats, _) => {
Niko Matsakis86b6e6e2013-05-10 17:10:35125 let pats_exit =
P1startead6c4b2014-10-06 00:36:53126 self.pats_all(subpats.iter().map(|f| &f.node.pat), pred);
James Miller97c17112015-02-19 14:27:25127 self.add_ast_node(pat.id, &[pats_exit])
Niko Matsakis86b6e6e2013-05-10 17:10:35128 }
129
Vadim Petrochenkov9b40e1e2016-02-14 12:25:12130 PatKind::Vec(ref pre, ref vec, ref post) => {
Eduard Burtescub0621282014-09-07 17:09:06131 let pre_exit = self.pats_all(pre.iter(), pred);
132 let vec_exit = self.pats_all(vec.iter(), pre_exit);
133 let post_exit = self.pats_all(post.iter(), vec_exit);
James Miller97c17112015-02-19 14:27:25134 self.add_ast_node(pat.id, &[post_exit])
Niko Matsakis86b6e6e2013-05-10 17:10:35135 }
136 }
137 }
138
Nick Cameronfacdf2e2015-07-31 07:04:06139 fn pats_all<'b, I: Iterator<Item=&'b P<hir::Pat>>>(&mut self,
Eduard Burtescub0621282014-09-07 17:09:06140 pats: I,
141 pred: CFGIndex) -> CFGIndex {
Niko Matsakis86b6e6e2013-05-10 17:10:35142 //! Handles case where all of the patterns must match.
Jonas Schievink559fca02016-02-09 21:00:20143 pats.fold(pred, |pred, pat| self.pat(&pat, pred))
Niko Matsakis86b6e6e2013-05-10 17:10:35144 }
145
Nick Cameronfacdf2e2015-07-31 07:04:06146 fn expr(&mut self, expr: &hir::Expr, pred: CFGIndex) -> CFGIndex {
Niko Matsakis86b6e6e2013-05-10 17:10:35147 match expr.node {
Nick Cameronfacdf2e2015-07-31 07:04:06148 hir::ExprBlock(ref blk) => {
Jonas Schievink559fca02016-02-09 21:00:20149 let blk_exit = self.block(&blk, pred);
James Miller97c17112015-02-19 14:27:25150 self.add_ast_node(expr.id, &[blk_exit])
Niko Matsakis86b6e6e2013-05-10 17:10:35151 }
152
Nick Cameronfacdf2e2015-07-31 07:04:06153 hir::ExprIf(ref cond, ref then, None) => {
Niko Matsakis86b6e6e2013-05-10 17:10:35154 //
155 // [pred]
156 // |
157 // v 1
158 // [cond]
159 // |
160 // / \
161 // / \
162 // v 2 *
163 // [then] |
164 // | |
165 // v 3 v 4
166 // [..expr..]
167 //
Jonas Schievink559fca02016-02-09 21:00:20168 let cond_exit = self.expr(&cond, pred); // 1
169 let then_exit = self.block(&then, cond_exit); // 2
James Miller97c17112015-02-19 14:27:25170 self.add_ast_node(expr.id, &[cond_exit, then_exit]) // 3,4
Niko Matsakis86b6e6e2013-05-10 17:10:35171 }
172
Nick Cameronfacdf2e2015-07-31 07:04:06173 hir::ExprIf(ref cond, ref then, Some(ref otherwise)) => {
Niko Matsakis86b6e6e2013-05-10 17:10:35174 //
175 // [pred]
176 // |
177 // v 1
178 // [cond]
179 // |
180 // / \
181 // / \
182 // v 2 v 3
183 // [then][otherwise]
184 // | |
185 // v 4 v 5
186 // [..expr..]
187 //
Jonas Schievink559fca02016-02-09 21:00:20188 let cond_exit = self.expr(&cond, pred); // 1
189 let then_exit = self.block(&then, cond_exit); // 2
190 let else_exit = self.expr(&otherwise, cond_exit); // 3
James Miller97c17112015-02-19 14:27:25191 self.add_ast_node(expr.id, &[then_exit, else_exit]) // 4, 5
Niko Matsakis86b6e6e2013-05-10 17:10:35192 }
193
Nick Cameronfacdf2e2015-07-31 07:04:06194 hir::ExprWhile(ref cond, ref body, _) => {
Niko Matsakis86b6e6e2013-05-10 17:10:35195 //
196 // [pred]
197 // |
198 // v 1
199 // [loopback] <--+ 5
200 // | |
201 // v 2 |
202 // +-----[cond] |
203 // | | |
204 // | v 4 |
205 // | [body] -----+
206 // v 3
207 // [expr]
208 //
Patrick Waltoncaa564b2014-07-22 03:54:28209 // Note that `break` and `continue` statements
Niko Matsakis86b6e6e2013-05-10 17:10:35210 // may cause additional edges.
211
Michael Sullivan7dbc5ae2013-07-30 23:47:22212 // Is the condition considered part of the loop?
Nick Cameronca085402014-11-17 08:39:01213 let loopback = self.add_dummy_node(&[pred]); // 1
Jonas Schievink559fca02016-02-09 21:00:20214 let cond_exit = self.expr(&cond, loopback); // 2
James Miller97c17112015-02-19 14:27:25215 let expr_exit = self.add_ast_node(expr.id, &[cond_exit]); // 3
Niko Matsakis86b6e6e2013-05-10 17:10:35216 self.loop_scopes.push(LoopScope {
217 loop_id: expr.id,
218 continue_index: loopback,
219 break_index: expr_exit
220 });
Jonas Schievink559fca02016-02-09 21:00:20221 let body_exit = self.block(&body, cond_exit); // 4
Alex Crichton54c2a1e2014-05-16 17:15:33222 self.add_contained_edge(body_exit, loopback); // 5
Felix S. Klock IIbe9c2d12014-05-20 16:49:19223 self.loop_scopes.pop();
Niko Matsakis86b6e6e2013-05-10 17:10:35224 expr_exit
225 }
226
Nick Cameronfacdf2e2015-07-31 07:04:06227 hir::ExprLoop(ref body, _) => {
Niko Matsakis86b6e6e2013-05-10 17:10:35228 //
229 // [pred]
230 // |
231 // v 1
232 // [loopback] <---+
233 // | 4 |
234 // v 3 |
235 // [body] ------+
236 //
237 // [expr] 2
238 //
239 // Note that `break` and `loop` statements
240 // may cause additional edges.
241
Nick Cameronca085402014-11-17 08:39:01242 let loopback = self.add_dummy_node(&[pred]); // 1
James Miller97c17112015-02-19 14:27:25243 let expr_exit = self.add_ast_node(expr.id, &[]); // 2
Niko Matsakis86b6e6e2013-05-10 17:10:35244 self.loop_scopes.push(LoopScope {
245 loop_id: expr.id,
246 continue_index: loopback,
247 break_index: expr_exit,
248 });
Jonas Schievink559fca02016-02-09 21:00:20249 let body_exit = self.block(&body, loopback); // 3
Alex Crichton54c2a1e2014-05-16 17:15:33250 self.add_contained_edge(body_exit, loopback); // 4
Niko Matsakis86b6e6e2013-05-10 17:10:35251 self.loop_scopes.pop();
252 expr_exit
253 }
254
Nick Cameronfacdf2e2015-07-31 07:04:06255 hir::ExprMatch(ref discr, ref arms, _) => {
James Miller4bae1332015-02-19 16:54:41256 self.match_(expr.id, &discr, &arms, pred)
Niko Matsakis86b6e6e2013-05-10 17:10:35257 }
258
Eduard Burtescuef4c7242016-03-29 06:32:58259 hir::ExprBinary(op, ref l, ref r) if op.node.is_lazy() => {
Niko Matsakis86b6e6e2013-05-10 17:10:35260 //
261 // [pred]
262 // |
263 // v 1
264 // [l]
265 // |
266 // / \
267 // / \
268 // v 2 *
269 // [r] |
270 // | |
271 // v 3 v 4
272 // [..exit..]
273 //
Jonas Schievink559fca02016-02-09 21:00:20274 let l_exit = self.expr(&l, pred); // 1
275 let r_exit = self.expr(&r, l_exit); // 2
James Miller97c17112015-02-19 14:27:25276 self.add_ast_node(expr.id, &[l_exit, r_exit]) // 3,4
Niko Matsakis86b6e6e2013-05-10 17:10:35277 }
278
Nick Cameronfacdf2e2015-07-31 07:04:06279 hir::ExprRet(ref v) => {
Eduard Burtescub0621282014-09-07 17:09:06280 let v_exit = self.opt_expr(v, pred);
James Miller97c17112015-02-19 14:27:25281 let b = self.add_ast_node(expr.id, &[v_exit]);
Felix S. Klock II65b65fe2014-05-08 22:07:57282 self.add_returning_edge(expr, b);
James Miller97c17112015-02-19 14:27:25283 self.add_unreachable_node()
Niko Matsakis86b6e6e2013-05-10 17:10:35284 }
285
Nick Cameronfacdf2e2015-07-31 07:04:06286 hir::ExprBreak(label) => {
Vadim Petrochenkovaad347c2016-03-06 12:54:44287 let loop_scope = self.find_scope(expr, label.map(|l| l.node));
James Miller97c17112015-02-19 14:27:25288 let b = self.add_ast_node(expr.id, &[pred]);
Felix S. Klock II65b65fe2014-05-08 22:07:57289 self.add_exiting_edge(expr, b,
Niko Matsakis86b6e6e2013-05-10 17:10:35290 loop_scope, loop_scope.break_index);
James Miller97c17112015-02-19 14:27:25291 self.add_unreachable_node()
Niko Matsakis86b6e6e2013-05-10 17:10:35292 }
293
Nick Cameronfacdf2e2015-07-31 07:04:06294 hir::ExprAgain(label) => {
Vadim Petrochenkovaad347c2016-03-06 12:54:44295 let loop_scope = self.find_scope(expr, label.map(|l| l.node));
James Miller97c17112015-02-19 14:27:25296 let a = self.add_ast_node(expr.id, &[pred]);
Felix S. Klock II65b65fe2014-05-08 22:07:57297 self.add_exiting_edge(expr, a,
Niko Matsakis86b6e6e2013-05-10 17:10:35298 loop_scope, loop_scope.continue_index);
James Miller97c17112015-02-19 14:27:25299 self.add_unreachable_node()
Niko Matsakis86b6e6e2013-05-10 17:10:35300 }
301
Nick Cameronfacdf2e2015-07-31 07:04:06302 hir::ExprVec(ref elems) => {
Eduard Burtescub0621282014-09-07 17:09:06303 self.straightline(expr, pred, elems.iter().map(|e| &**e))
Niko Matsakis86b6e6e2013-05-10 17:10:35304 }
305
Nick Cameronfacdf2e2015-07-31 07:04:06306 hir::ExprCall(ref func, ref args) => {
Jonas Schievink559fca02016-02-09 21:00:20307 self.call(expr, pred, &func, args.iter().map(|e| &**e))
Niko Matsakis86b6e6e2013-05-10 17:10:35308 }
309
Nick Cameronfacdf2e2015-07-31 07:04:06310 hir::ExprMethodCall(_, _, ref args) => {
Jonas Schievink559fca02016-02-09 21:00:20311 self.call(expr, pred, &args[0], args[1..].iter().map(|e| &**e))
Niko Matsakis86b6e6e2013-05-10 17:10:35312 }
313
Nick Cameronfacdf2e2015-07-31 07:04:06314 hir::ExprIndex(ref l, ref r) |
315 hir::ExprBinary(_, ref l, ref r) if self.tcx.is_method_call(expr.id) => {
Jonas Schievink559fca02016-02-09 21:00:20316 self.call(expr, pred, &l, Some(&**r).into_iter())
Niko Matsakis86b6e6e2013-05-10 17:10:35317 }
318
Nick Cameronfacdf2e2015-07-31 07:04:06319 hir::ExprUnary(_, ref e) if self.tcx.is_method_call(expr.id) => {
Jonas Schievink559fca02016-02-09 21:00:20320 self.call(expr, pred, &e, None::<hir::Expr>.iter())
Niko Matsakis86b6e6e2013-05-10 17:10:35321 }
322
Nick Cameronfacdf2e2015-07-31 07:04:06323 hir::ExprTup(ref exprs) => {
Eduard Burtescub0621282014-09-07 17:09:06324 self.straightline(expr, pred, exprs.iter().map(|e| &**e))
Niko Matsakis86b6e6e2013-05-10 17:10:35325 }
326
Nick Cameronfacdf2e2015-07-31 07:04:06327 hir::ExprStruct(_, ref fields, ref base) => {
Brandon Sandersond80a62d2014-11-05 23:05:01328 let field_cfg = self.straightline(expr, pred, fields.iter().map(|f| &*f.expr));
329 self.opt_expr(base, field_cfg)
Niko Matsakis86b6e6e2013-05-10 17:10:35330 }
331
Nick Cameronfacdf2e2015-07-31 07:04:06332 hir::ExprRepeat(ref elem, ref count) => {
Eduard Burtescub0621282014-09-07 17:09:06333 self.straightline(expr, pred, [elem, count].iter().map(|&e| &**e))
Niko Matsakis86b6e6e2013-05-10 17:10:35334 }
335
Nick Cameronfacdf2e2015-07-31 07:04:06336 hir::ExprAssign(ref l, ref r) |
337 hir::ExprAssignOp(_, ref l, ref r) => {
Eduard Burtescub0621282014-09-07 17:09:06338 self.straightline(expr, pred, [r, l].iter().map(|&e| &**e))
Niko Matsakis86b6e6e2013-05-10 17:10:35339 }
340
Nick Cameronfacdf2e2015-07-31 07:04:06341 hir::ExprIndex(ref l, ref r) |
342 hir::ExprBinary(_, ref l, ref r) => { // NB: && and || handled earlier
Eduard Burtescub0621282014-09-07 17:09:06343 self.straightline(expr, pred, [l, r].iter().map(|&e| &**e))
Niko Matsakis86b6e6e2013-05-10 17:10:35344 }
345
Eduard Burtescuf293ea22015-09-24 15:00:08346 hir::ExprBox(ref e) |
Nick Cameronfacdf2e2015-07-31 07:04:06347 hir::ExprAddrOf(_, ref e) |
348 hir::ExprCast(ref e, _) |
Eduard Burtescub8157cc2015-02-01 07:59:46349 hir::ExprType(ref e, _) |
Nick Cameronfacdf2e2015-07-31 07:04:06350 hir::ExprUnary(_, ref e) |
Nick Cameronfacdf2e2015-07-31 07:04:06351 hir::ExprField(ref e, _) |
352 hir::ExprTupField(ref e, _) => {
Aaron Turonfc525ee2014-09-15 03:27:36353 self.straightline(expr, pred, Some(&**e).into_iter())
Niko Matsakis86b6e6e2013-05-10 17:10:35354 }
355
Eduard Burtescu856185d2016-03-09 20:17:02356 hir::ExprInlineAsm(_, ref outputs, ref inputs) => {
357 let post_outputs = self.exprs(outputs.iter().map(|e| &**e), pred);
358 let post_inputs = self.exprs(inputs.iter().map(|e| &**e), post_outputs);
359 self.add_ast_node(expr.id, &[post_inputs])
Felix S. Klock IIbe9c2d12014-05-20 16:49:19360 }
361
Nick Cameronfacdf2e2015-07-31 07:04:06362 hir::ExprClosure(..) |
363 hir::ExprLit(..) |
364 hir::ExprPath(..) => {
365 self.straightline(expr, pred, None::<hir::Expr>.iter())
Niko Matsakis86b6e6e2013-05-10 17:10:35366 }
367 }
368 }
369
Nick Cameronfacdf2e2015-07-31 07:04:06370 fn call<'b, I: Iterator<Item=&'b hir::Expr>>(&mut self,
371 call_expr: &hir::Expr,
Niko Matsakis86b6e6e2013-05-10 17:10:35372 pred: CFGIndex,
Nick Cameronfacdf2e2015-07-31 07:04:06373 func_or_rcvr: &hir::Expr,
Eduard Burtescub0621282014-09-07 17:09:06374 args: I) -> CFGIndex {
Niko Matsakis7c445612014-11-25 19:21:20375 let method_call = ty::MethodCall::expr(call_expr.id);
Jared Roesch79d02892015-06-24 20:40:54376 let fn_ty = match self.tcx.tables.borrow().method_map.get(&method_call) {
Jakub Bukajcca84e92014-10-24 19:14:37377 Some(method) => method.ty,
Eduard Burtescuad66c212015-06-25 20:42:17378 None => self.tcx.expr_ty_adjusted(func_or_rcvr)
Eduard Burtescu59935f72015-06-24 05:24:13379 };
Jakub Bukajcca84e92014-10-24 19:14:37380
Niko Matsakis86b6e6e2013-05-10 17:10:35381 let func_or_rcvr_exit = self.expr(func_or_rcvr, pred);
Felix S. Klock IIbe9c2d12014-05-20 16:49:19382 let ret = self.straightline(call_expr, func_or_rcvr_exit, args);
Eduard Burtescu59935f72015-06-24 05:24:13383 if fn_ty.fn_ret().diverges() {
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
459 let this_has_bindings = pat_util::pat_contains_bindings_or_wild(
Jonas Schievink559fca02016-02-09 21:00:20460 &self.tcx.def_map.borrow(), &pat);
James Miller4bae1332015-02-19 16:54:41461
462 // If both this pattern and the previous pattern
463 // were free of bindings, they must consist only
464 // of "constant" patterns. Note we cannot match an
465 // all-constant pattern, fail the guard, and then
466 // match *another* all-constant pattern. This is
467 // because if the previous pattern matches, then
468 // we *cannot* match this one, unless all the
469 // constants are the same (which is rejected by
470 // `check_match`).
471 //
472 // We can use this to be smarter about the flow
473 // along guards. If the previous pattern matched,
474 // then we know we will not visit the guard in
475 // this one (whether or not the guard succeeded),
476 // if the previous pattern failed, then we know
477 // the guard for that pattern will not have been
478 // visited. Thus, it is not possible to visit both
479 // the previous guard and the current one when
480 // both patterns consist only of constant
481 // sub-patterns.
482 //
483 // However, if the above does not hold, then all
484 // previous guards need to be wired to visit the
485 // current guard pattern.
486 if prev_has_bindings || this_has_bindings {
487 while let Some(prev) = prev_guards.pop() {
488 self.add_contained_edge(prev, guard_start);
489 }
490 }
491
492 prev_has_bindings = this_has_bindings;
493
494 // Push the guard onto the list of previous guards
495 prev_guards.push(guard_exit);
496
497 // Update the exit node for the pattern
498 pat_exit = guard_exit;
499 }
500
501 // Add an edge from the exit of this pattern to the
502 // exit of the arm
503 self.add_contained_edge(pat_exit, arm_exit);
504 }
505
506 // Visit the body of this arm
507 let body_exit = self.expr(&arm.body, arm_exit);
508
509 // Link the body to the exit of the expression
510 self.add_contained_edge(body_exit, expr_exit);
511 }
512
513 expr_exit
514 }
515
Niko Matsakis86b6e6e2013-05-10 17:10:35516 fn add_dummy_node(&mut self, preds: &[CFGIndex]) -> CFGIndex {
James Miller97c17112015-02-19 14:27:25517 self.add_node(CFGNodeData::Dummy, preds)
Niko Matsakis86b6e6e2013-05-10 17:10:35518 }
519
James Miller97c17112015-02-19 14:27:25520 fn add_ast_node(&mut self, id: ast::NodeId, preds: &[CFGIndex]) -> CFGIndex {
James Miller97c17112015-02-19 14:27:25521 assert!(id != ast::DUMMY_NODE_ID);
522 self.add_node(CFGNodeData::AST(id), preds)
523 }
524
525 fn add_unreachable_node(&mut self) -> CFGIndex {
526 self.add_node(CFGNodeData::Unreachable, &[])
527 }
528
529 fn add_node(&mut self, data: CFGNodeData, preds: &[CFGIndex]) -> CFGIndex {
530 let node = self.graph.add_node(data);
Jorge Apariciod5d7e652015-01-31 17:20:46531 for &pred in preds {
Niko Matsakis86b6e6e2013-05-10 17:10:35532 self.add_contained_edge(pred, node);
533 }
534 node
535 }
536
537 fn add_contained_edge(&mut self,
538 source: CFGIndex,
539 target: CFGIndex) {
Huon Wilson7785fe12014-03-19 12:16:56540 let data = CFGEdgeData {exiting_scopes: vec!() };
Niko Matsakis86b6e6e2013-05-10 17:10:35541 self.graph.add_edge(source, target, data);
542 }
543
544 fn add_exiting_edge(&mut self,
Nick Cameronfacdf2e2015-07-31 07:04:06545 from_expr: &hir::Expr,
Niko Matsakis86b6e6e2013-05-10 17:10:35546 from_index: CFGIndex,
547 to_loop: LoopScope,
548 to_index: CFGIndex) {
Huon Wilson7785fe12014-03-19 12:16:56549 let mut data = CFGEdgeData {exiting_scopes: vec!() };
Ariel Ben-Yehudafc304382015-08-19 22:46:28550 let mut scope = self.tcx.region_maps.node_extent(from_expr.id);
551 let target_scope = self.tcx.region_maps.node_extent(to_loop.loop_id);
Felix S. Klock II5ff90872014-11-18 13:22:59552 while scope != target_scope {
Ariel Ben-Yehudafc304382015-08-19 22:46:28553 data.exiting_scopes.push(scope.node_id(&self.tcx.region_maps));
Felix S. Klock II5ff90872014-11-18 13:22:59554 scope = self.tcx.region_maps.encl_scope(scope);
Niko Matsakis86b6e6e2013-05-10 17:10:35555 }
556 self.graph.add_edge(from_index, to_index, data);
557 }
558
Felix S. Klock II65b65fe2014-05-08 22:07:57559 fn add_returning_edge(&mut self,
Nick Cameronfacdf2e2015-07-31 07:04:06560 _from_expr: &hir::Expr,
Felix S. Klock II65b65fe2014-05-08 22:07:57561 from_index: CFGIndex) {
Patrick Walton36195eb2014-05-16 17:45:16562 let mut data = CFGEdgeData {
563 exiting_scopes: vec!(),
564 };
Felix S. Klock II65b65fe2014-05-08 22:07:57565 for &LoopScope { loop_id: id, .. } in self.loop_scopes.iter().rev() {
566 data.exiting_scopes.push(id);
567 }
568 self.graph.add_edge(from_index, self.fn_exit, data);
569 }
570
Niko Matsakis86b6e6e2013-05-10 17:10:35571 fn find_scope(&self,
Nick Cameronfacdf2e2015-07-31 07:04:06572 expr: &hir::Expr,
Vadim Petrochenkov40ce8042015-09-23 17:04:49573 label: Option<ast::Name>) -> LoopScope {
Eduard Burtescu5a6a9ed2015-02-17 04:44:23574 if label.is_none() {
575 return *self.loop_scopes.last().unwrap();
576 }
Niko Matsakis86b6e6e2013-05-10 17:10:35577
Eduard Burtescu5a6a9ed2015-02-17 04:44:23578 match self.tcx.def_map.borrow().get(&expr.id).map(|d| d.full_def()) {
Vadim Petrochenkov2084c2c2016-01-20 19:31:10579 Some(Def::Label(loop_id)) => {
Eduard Burtescu5a6a9ed2015-02-17 04:44:23580 for l in &self.loop_scopes {
581 if l.loop_id == loop_id {
582 return *l;
Niko Matsakis86b6e6e2013-05-10 17:10:35583 }
584 }
Benjamin Herrbcdaccf2016-03-25 17:31:27585 span_bug!(expr.span, "no loop scope for id {}", loop_id);
Eduard Burtescu5a6a9ed2015-02-17 04:44:23586 }
587
588 r => {
Benjamin Herrbcdaccf2016-03-25 17:31:27589 span_bug!(expr.span, "bad entry `{:?}` in def_map for label", r);
Niko Matsakis86b6e6e2013-05-10 17:10:35590 }
591 }
592 }
Patrick Walton99d44d22013-07-10 02:32:09593}