blob: feefda741432c28cb8f41c5bd087455d1d5eaae9 [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::*;
Vadim Petrochenkov2084c2c2016-01-20 19:31:1013use middle::def::Def;
James Miller4bae1332015-02-19 16:54:4114use middle::pat_util;
Niko Matsakis86b6e6e2013-05-10 17:10:3515use middle::ty;
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
Vadim Petrochenkov9b40e1e2016-02-14 12:25:1219use rustc_front::hir::{self, PatKind};
Nick Cameronfacdf2e2015-07-31 07:04:0620
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,
Nick Cameronfacdf2e2015-07-31 07:04:0636 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) |
103 PatKind::Enum(_, None) |
104 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 Petrochenkov9b40e1e2016-02-14 12:25:12118 PatKind::Enum(_, Some(ref subpats)) |
119 PatKind::Tup(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 }
Niko Matsakis86b6e6e2013-05-10 17:10:35136 }
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
Nick Cameronfacdf2e2015-07-31 07:04:06259 hir::ExprBinary(op, ref l, ref r) if ::rustc_front::util::lazy_binop(op.node) => {
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 Petrochenkov40ce8042015-09-23 17:04:49287 let loop_scope = self.find_scope(expr, label.map(|l| l.node.name));
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 Petrochenkov40ce8042015-09-23 17:04:49295 let loop_scope = self.find_scope(expr, label.map(|l| l.node.name));
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::ExprRange(ref start, ref end) => {
Nick Cameroned8f5032014-12-18 04:55:04320 let fields = start.as_ref().map(|e| &**e).into_iter()
Joshua Landauca7418b2015-06-10 16:22:20321 .chain(end.as_ref().map(|e| &**e));
Nick Cameron17826e12014-12-15 00:17:11322 self.straightline(expr, pred, fields)
Nick Cameron8a357e12014-12-13 05:41:02323 }
324
Nick Cameronfacdf2e2015-07-31 07:04:06325 hir::ExprUnary(_, ref e) if self.tcx.is_method_call(expr.id) => {
Jonas Schievink559fca02016-02-09 21:00:20326 self.call(expr, pred, &e, None::<hir::Expr>.iter())
Niko Matsakis86b6e6e2013-05-10 17:10:35327 }
328
Nick Cameronfacdf2e2015-07-31 07:04:06329 hir::ExprTup(ref exprs) => {
Eduard Burtescub0621282014-09-07 17:09:06330 self.straightline(expr, pred, exprs.iter().map(|e| &**e))
Niko Matsakis86b6e6e2013-05-10 17:10:35331 }
332
Nick Cameronfacdf2e2015-07-31 07:04:06333 hir::ExprStruct(_, ref fields, ref base) => {
Brandon Sandersond80a62d2014-11-05 23:05:01334 let field_cfg = self.straightline(expr, pred, fields.iter().map(|f| &*f.expr));
335 self.opt_expr(base, field_cfg)
Niko Matsakis86b6e6e2013-05-10 17:10:35336 }
337
Nick Cameronfacdf2e2015-07-31 07:04:06338 hir::ExprRepeat(ref elem, ref count) => {
Eduard Burtescub0621282014-09-07 17:09:06339 self.straightline(expr, pred, [elem, count].iter().map(|&e| &**e))
Niko Matsakis86b6e6e2013-05-10 17:10:35340 }
341
Nick Cameronfacdf2e2015-07-31 07:04:06342 hir::ExprAssign(ref l, ref r) |
343 hir::ExprAssignOp(_, ref l, ref r) => {
Eduard Burtescub0621282014-09-07 17:09:06344 self.straightline(expr, pred, [r, l].iter().map(|&e| &**e))
Niko Matsakis86b6e6e2013-05-10 17:10:35345 }
346
Nick Cameronfacdf2e2015-07-31 07:04:06347 hir::ExprIndex(ref l, ref r) |
348 hir::ExprBinary(_, ref l, ref r) => { // NB: && and || handled earlier
Eduard Burtescub0621282014-09-07 17:09:06349 self.straightline(expr, pred, [l, r].iter().map(|&e| &**e))
Niko Matsakis86b6e6e2013-05-10 17:10:35350 }
351
Eduard Burtescuf293ea22015-09-24 15:00:08352 hir::ExprBox(ref e) |
Nick Cameronfacdf2e2015-07-31 07:04:06353 hir::ExprAddrOf(_, ref e) |
354 hir::ExprCast(ref e, _) |
Eduard Burtescub8157cc2015-02-01 07:59:46355 hir::ExprType(ref e, _) |
Nick Cameronfacdf2e2015-07-31 07:04:06356 hir::ExprUnary(_, ref e) |
Nick Cameronfacdf2e2015-07-31 07:04:06357 hir::ExprField(ref e, _) |
358 hir::ExprTupField(ref e, _) => {
Aaron Turonfc525ee2014-09-15 03:27:36359 self.straightline(expr, pred, Some(&**e).into_iter())
Niko Matsakis86b6e6e2013-05-10 17:10:35360 }
361
Nick Cameronfacdf2e2015-07-31 07:04:06362 hir::ExprInlineAsm(ref inline_asm) => {
Felix S. Klock IIbe9c2d12014-05-20 16:49:19363 let inputs = inline_asm.inputs.iter();
364 let outputs = inline_asm.outputs.iter();
Felix S. Klock IIbe9c2d12014-05-20 16:49:19365 let post_inputs = self.exprs(inputs.map(|a| {
Sean McArthur44440e52014-12-20 08:09:35366 debug!("cfg::construct InlineAsm id:{} input:{:?}", expr.id, a);
Eduard Burtescub0621282014-09-07 17:09:06367 let &(_, ref expr) = a;
368 &**expr
Felix S. Klock IIbe9c2d12014-05-20 16:49:19369 }), pred);
370 let post_outputs = self.exprs(outputs.map(|a| {
Sean McArthur44440e52014-12-20 08:09:35371 debug!("cfg::construct InlineAsm id:{} output:{:?}", expr.id, a);
Amanieu d'Antras65707df2015-12-05 08:18:24372 &*a.expr
Felix S. Klock IIbe9c2d12014-05-20 16:49:19373 }), post_inputs);
James Miller97c17112015-02-19 14:27:25374 self.add_ast_node(expr.id, &[post_outputs])
Felix S. Klock IIbe9c2d12014-05-20 16:49:19375 }
376
Nick Cameronfacdf2e2015-07-31 07:04:06377 hir::ExprClosure(..) |
378 hir::ExprLit(..) |
379 hir::ExprPath(..) => {
380 self.straightline(expr, pred, None::<hir::Expr>.iter())
Niko Matsakis86b6e6e2013-05-10 17:10:35381 }
382 }
383 }
384
Nick Cameronfacdf2e2015-07-31 07:04:06385 fn call<'b, I: Iterator<Item=&'b hir::Expr>>(&mut self,
386 call_expr: &hir::Expr,
Niko Matsakis86b6e6e2013-05-10 17:10:35387 pred: CFGIndex,
Nick Cameronfacdf2e2015-07-31 07:04:06388 func_or_rcvr: &hir::Expr,
Eduard Burtescub0621282014-09-07 17:09:06389 args: I) -> CFGIndex {
Niko Matsakis7c445612014-11-25 19:21:20390 let method_call = ty::MethodCall::expr(call_expr.id);
Jared Roesch79d02892015-06-24 20:40:54391 let fn_ty = match self.tcx.tables.borrow().method_map.get(&method_call) {
Jakub Bukajcca84e92014-10-24 19:14:37392 Some(method) => method.ty,
Eduard Burtescuad66c212015-06-25 20:42:17393 None => self.tcx.expr_ty_adjusted(func_or_rcvr)
Eduard Burtescu59935f72015-06-24 05:24:13394 };
Jakub Bukajcca84e92014-10-24 19:14:37395
Niko Matsakis86b6e6e2013-05-10 17:10:35396 let func_or_rcvr_exit = self.expr(func_or_rcvr, pred);
Felix S. Klock IIbe9c2d12014-05-20 16:49:19397 let ret = self.straightline(call_expr, func_or_rcvr_exit, args);
Eduard Burtescu59935f72015-06-24 05:24:13398 if fn_ty.fn_ret().diverges() {
James Miller97c17112015-02-19 14:27:25399 self.add_unreachable_node()
Felix S. Klock IIbe9c2d12014-05-20 16:49:19400 } else {
401 ret
402 }
Niko Matsakis86b6e6e2013-05-10 17:10:35403 }
404
Nick Cameronfacdf2e2015-07-31 07:04:06405 fn exprs<'b, I: Iterator<Item=&'b hir::Expr>>(&mut self,
Aaron Turonb299c2b2014-11-06 17:32:37406 exprs: I,
Eduard Burtescub0621282014-09-07 17:09:06407 pred: CFGIndex) -> CFGIndex {
Niko Matsakis86b6e6e2013-05-10 17:10:35408 //! Constructs graph for `exprs` evaluated in order
Felix S. Klock IIbe9c2d12014-05-20 16:49:19409 exprs.fold(pred, |p, e| self.expr(e, p))
Niko Matsakis86b6e6e2013-05-10 17:10:35410 }
411
412 fn opt_expr(&mut self,
Nick Cameronfacdf2e2015-07-31 07:04:06413 opt_expr: &Option<P<hir::Expr>>,
Niko Matsakis86b6e6e2013-05-10 17:10:35414 pred: CFGIndex) -> CFGIndex {
415 //! Constructs graph for `opt_expr` evaluated, if Some
Jonas Schievink559fca02016-02-09 21:00:20416 opt_expr.iter().fold(pred, |p, e| self.expr(&e, p))
Niko Matsakis86b6e6e2013-05-10 17:10:35417 }
418
Nick Cameronfacdf2e2015-07-31 07:04:06419 fn straightline<'b, I: Iterator<Item=&'b hir::Expr>>(&mut self,
420 expr: &hir::Expr,
Niko Matsakis86b6e6e2013-05-10 17:10:35421 pred: CFGIndex,
Eduard Burtescub0621282014-09-07 17:09:06422 subexprs: I) -> CFGIndex {
Niko Matsakis86b6e6e2013-05-10 17:10:35423 //! Handles case of an expression that evaluates `subexprs` in order
424
Eduard Burtescub0621282014-09-07 17:09:06425 let subexprs_exit = self.exprs(subexprs, pred);
James Miller97c17112015-02-19 14:27:25426 self.add_ast_node(expr.id, &[subexprs_exit])
Niko Matsakis86b6e6e2013-05-10 17:10:35427 }
428
Nick Cameronfacdf2e2015-07-31 07:04:06429 fn match_(&mut self, id: ast::NodeId, discr: &hir::Expr,
430 arms: &[hir::Arm], pred: CFGIndex) -> CFGIndex {
James Miller4bae1332015-02-19 16:54:41431 // The CFG for match expression is quite complex, so no ASCII
432 // art for it (yet).
433 //
434 // The CFG generated below matches roughly what trans puts
435 // out. Each pattern and guard is visited in parallel, with
436 // arms containing multiple patterns generating multiple nodes
437 // for the same guard expression. The guard expressions chain
438 // into each other from top to bottom, with a specific
439 // exception to allow some additional valid programs
440 // (explained below). Trans differs slightly in that the
441 // pattern matching may continue after a guard but the visible
442 // behaviour should be the same.
443 //
444 // What is going on is explained in further comments.
445
446 // Visit the discriminant expression
447 let discr_exit = self.expr(discr, pred);
448
449 // Add a node for the exit of the match expression as a whole.
450 let expr_exit = self.add_ast_node(id, &[]);
451
452 // Keep track of the previous guard expressions
453 let mut prev_guards = Vec::new();
454 // Track if the previous pattern contained bindings or wildcards
455 let mut prev_has_bindings = false;
456
457 for arm in arms {
458 // Add an exit node for when we've visited all the
459 // patterns and the guard (if there is one) in the arm.
460 let arm_exit = self.add_dummy_node(&[]);
461
462 for pat in &arm.pats {
463 // Visit the pattern, coming from the discriminant exit
Jonas Schievink559fca02016-02-09 21:00:20464 let mut pat_exit = self.pat(&pat, discr_exit);
James Miller4bae1332015-02-19 16:54:41465
466 // If there is a guard expression, handle it here
467 if let Some(ref guard) = arm.guard {
468 // Add a dummy node for the previous guard
469 // expression to target
470 let guard_start = self.add_dummy_node(&[pat_exit]);
471 // Visit the guard expression
Jonas Schievink559fca02016-02-09 21:00:20472 let guard_exit = self.expr(&guard, guard_start);
James Miller4bae1332015-02-19 16:54:41473
474 let this_has_bindings = pat_util::pat_contains_bindings_or_wild(
Jonas Schievink559fca02016-02-09 21:00:20475 &self.tcx.def_map.borrow(), &pat);
James Miller4bae1332015-02-19 16:54:41476
477 // If both this pattern and the previous pattern
478 // were free of bindings, they must consist only
479 // of "constant" patterns. Note we cannot match an
480 // all-constant pattern, fail the guard, and then
481 // match *another* all-constant pattern. This is
482 // because if the previous pattern matches, then
483 // we *cannot* match this one, unless all the
484 // constants are the same (which is rejected by
485 // `check_match`).
486 //
487 // We can use this to be smarter about the flow
488 // along guards. If the previous pattern matched,
489 // then we know we will not visit the guard in
490 // this one (whether or not the guard succeeded),
491 // if the previous pattern failed, then we know
492 // the guard for that pattern will not have been
493 // visited. Thus, it is not possible to visit both
494 // the previous guard and the current one when
495 // both patterns consist only of constant
496 // sub-patterns.
497 //
498 // However, if the above does not hold, then all
499 // previous guards need to be wired to visit the
500 // current guard pattern.
501 if prev_has_bindings || this_has_bindings {
502 while let Some(prev) = prev_guards.pop() {
503 self.add_contained_edge(prev, guard_start);
504 }
505 }
506
507 prev_has_bindings = this_has_bindings;
508
509 // Push the guard onto the list of previous guards
510 prev_guards.push(guard_exit);
511
512 // Update the exit node for the pattern
513 pat_exit = guard_exit;
514 }
515
516 // Add an edge from the exit of this pattern to the
517 // exit of the arm
518 self.add_contained_edge(pat_exit, arm_exit);
519 }
520
521 // Visit the body of this arm
522 let body_exit = self.expr(&arm.body, arm_exit);
523
524 // Link the body to the exit of the expression
525 self.add_contained_edge(body_exit, expr_exit);
526 }
527
528 expr_exit
529 }
530
Niko Matsakis86b6e6e2013-05-10 17:10:35531 fn add_dummy_node(&mut self, preds: &[CFGIndex]) -> CFGIndex {
James Miller97c17112015-02-19 14:27:25532 self.add_node(CFGNodeData::Dummy, preds)
Niko Matsakis86b6e6e2013-05-10 17:10:35533 }
534
James Miller97c17112015-02-19 14:27:25535 fn add_ast_node(&mut self, id: ast::NodeId, preds: &[CFGIndex]) -> CFGIndex {
James Miller97c17112015-02-19 14:27:25536 assert!(id != ast::DUMMY_NODE_ID);
537 self.add_node(CFGNodeData::AST(id), preds)
538 }
539
540 fn add_unreachable_node(&mut self) -> CFGIndex {
541 self.add_node(CFGNodeData::Unreachable, &[])
542 }
543
544 fn add_node(&mut self, data: CFGNodeData, preds: &[CFGIndex]) -> CFGIndex {
545 let node = self.graph.add_node(data);
Jorge Apariciod5d7e652015-01-31 17:20:46546 for &pred in preds {
Niko Matsakis86b6e6e2013-05-10 17:10:35547 self.add_contained_edge(pred, node);
548 }
549 node
550 }
551
552 fn add_contained_edge(&mut self,
553 source: CFGIndex,
554 target: CFGIndex) {
Huon Wilson7785fe12014-03-19 12:16:56555 let data = CFGEdgeData {exiting_scopes: vec!() };
Niko Matsakis86b6e6e2013-05-10 17:10:35556 self.graph.add_edge(source, target, data);
557 }
558
559 fn add_exiting_edge(&mut self,
Nick Cameronfacdf2e2015-07-31 07:04:06560 from_expr: &hir::Expr,
Niko Matsakis86b6e6e2013-05-10 17:10:35561 from_index: CFGIndex,
562 to_loop: LoopScope,
563 to_index: CFGIndex) {
Huon Wilson7785fe12014-03-19 12:16:56564 let mut data = CFGEdgeData {exiting_scopes: vec!() };
Ariel Ben-Yehudafc304382015-08-19 22:46:28565 let mut scope = self.tcx.region_maps.node_extent(from_expr.id);
566 let target_scope = self.tcx.region_maps.node_extent(to_loop.loop_id);
Felix S. Klock II5ff90872014-11-18 13:22:59567 while scope != target_scope {
Ariel Ben-Yehudafc304382015-08-19 22:46:28568 data.exiting_scopes.push(scope.node_id(&self.tcx.region_maps));
Felix S. Klock II5ff90872014-11-18 13:22:59569 scope = self.tcx.region_maps.encl_scope(scope);
Niko Matsakis86b6e6e2013-05-10 17:10:35570 }
571 self.graph.add_edge(from_index, to_index, data);
572 }
573
Felix S. Klock II65b65fe2014-05-08 22:07:57574 fn add_returning_edge(&mut self,
Nick Cameronfacdf2e2015-07-31 07:04:06575 _from_expr: &hir::Expr,
Felix S. Klock II65b65fe2014-05-08 22:07:57576 from_index: CFGIndex) {
Patrick Walton36195eb2014-05-16 17:45:16577 let mut data = CFGEdgeData {
578 exiting_scopes: vec!(),
579 };
Felix S. Klock II65b65fe2014-05-08 22:07:57580 for &LoopScope { loop_id: id, .. } in self.loop_scopes.iter().rev() {
581 data.exiting_scopes.push(id);
582 }
583 self.graph.add_edge(from_index, self.fn_exit, data);
584 }
585
Niko Matsakis86b6e6e2013-05-10 17:10:35586 fn find_scope(&self,
Nick Cameronfacdf2e2015-07-31 07:04:06587 expr: &hir::Expr,
Vadim Petrochenkov40ce8042015-09-23 17:04:49588 label: Option<ast::Name>) -> LoopScope {
Eduard Burtescu5a6a9ed2015-02-17 04:44:23589 if label.is_none() {
590 return *self.loop_scopes.last().unwrap();
591 }
Niko Matsakis86b6e6e2013-05-10 17:10:35592
Eduard Burtescu5a6a9ed2015-02-17 04:44:23593 match self.tcx.def_map.borrow().get(&expr.id).map(|d| d.full_def()) {
Vadim Petrochenkov2084c2c2016-01-20 19:31:10594 Some(Def::Label(loop_id)) => {
Eduard Burtescu5a6a9ed2015-02-17 04:44:23595 for l in &self.loop_scopes {
596 if l.loop_id == loop_id {
597 return *l;
Niko Matsakis86b6e6e2013-05-10 17:10:35598 }
599 }
Eduard Burtescu5a6a9ed2015-02-17 04:44:23600 self.tcx.sess.span_bug(expr.span,
601 &format!("no loop scope for id {}", loop_id));
602 }
603
604 r => {
605 self.tcx.sess.span_bug(expr.span,
606 &format!("bad entry `{:?}` in def_map for label", r));
Niko Matsakis86b6e6e2013-05-10 17:10:35607 }
608 }
609 }
Patrick Walton99d44d22013-07-10 02:32:09610}