blob: 609d492a93aec5e9930547963e7407ea4f1a5c5e [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>,
Eduard Burtescuff0830d2016-10-25 23:27:1436 body: &hir::Expr) -> 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
Eduard Burtescuff0830d2016-10-25 23:27:1441 // outside input `body`. (Distinguishing `fn_exit` and `body_exit`
Felix S. Klock II65b65fe2014-05-08 22:07:5742 // also resolves chicken-and-egg problem that arises if you try to
Eduard Burtescuff0830d2016-10-25 23:27:1443 // have return exprs jump to `body_exit` during construction.)
James Miller97c17112015-02-19 14:27:2544 let fn_exit = graph.add_node(CFGNodeData::Exit);
Eduard Burtescuff0830d2016-10-25 23:27:1445 let body_exit;
Felix S. Klock II65b65fe2014-05-08 22:07:5746
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 };
Eduard Burtescuff0830d2016-10-25 23:27:1453 body_exit = cfg_builder.expr(body, entry);
54 cfg_builder.add_contained_edge(body_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) |
Eduard Burtescu16b5c2c2016-10-27 02:17:42103 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
Geoffry Song9d425492016-10-29 22:15: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
Geoffry Song9d425492016-10-29 22:15:06285 hir::ExprBreak(label, ref opt_expr) => {
286 let v = self.opt_expr(opt_expr, pred);
Vadim Petrochenkovaad347c2016-03-06 12:54:44287 let loop_scope = self.find_scope(expr, label.map(|l| l.node));
Geoffry Song9d425492016-10-29 22:15:06288 let b = self.add_ast_node(expr.id, &[v]);
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
Jonas Schievinkcf0b7bd2016-09-20 00:14:46302 hir::ExprArray(ref elems) => {
Nicholas Nethercote382d3b02016-10-28 10:16:44303 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) => {
Nicholas Nethercote382d3b02016-10-28 10:16:44307 self.call(expr, pred, &func, args.iter().map(|e| &*e))
Niko Matsakis86b6e6e2013-05-10 17:10:35308 }
309
Vadim Petrochenkove05e74a2016-08-26 16:23:42310 hir::ExprMethodCall(.., ref args) => {
Nicholas Nethercote382d3b02016-10-28 10:16:44311 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) |
Eduard Burtescu6a8d1312016-10-27 01:52:10315 hir::ExprBinary(_, ref l, ref r) if self.tcx.tables().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
Eduard Burtescu6a8d1312016-10-27 01:52:10319 hir::ExprUnary(_, ref e) if self.tcx.tables().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) => {
Nicholas Nethercote382d3b02016-10-28 10:16:44324 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) => {
Nicholas Nethercote382d3b02016-10-28 10:16:44357 let post_outputs = self.exprs(outputs.iter().map(|e| &*e), pred);
358 let post_inputs = self.exprs(inputs.iter().map(|e| &*e), post_outputs);
Eduard Burtescu856185d2016-03-09 20:17:02359 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(..) |
Eduard Burtescu16b5c2c2016-10-27 02:17:42364 hir::ExprPath(_) => {
Nick Cameronfacdf2e2015-07-31 07:04:06365 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);
Eduard Burtescu6a8d1312016-10-27 01:52:10376 let fn_ty = match self.tcx.tables().method_map.get(&method_call) {
Jakub Bukajcca84e92014-10-24 19:14:37377 Some(method) => method.ty,
Eduard Burtescu0d7201e2016-10-20 03:33:20378 None => self.tcx.tables().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);
Andrew Cannfadabe02016-08-02 07:56:20383 // FIXME(canndrew): This is_never should probably be an is_uninhabited.
384 if fn_ty.fn_ret().0.is_never() {
James Miller97c17112015-02-19 14:27:25385 self.add_unreachable_node()
Felix S. Klock IIbe9c2d12014-05-20 16:49:19386 } else {
387 ret
388 }
Niko Matsakis86b6e6e2013-05-10 17:10:35389 }
390
Nick Cameronfacdf2e2015-07-31 07:04:06391 fn exprs<'b, I: Iterator<Item=&'b hir::Expr>>(&mut self,
Aaron Turonb299c2b2014-11-06 17:32:37392 exprs: I,
Eduard Burtescub0621282014-09-07 17:09:06393 pred: CFGIndex) -> CFGIndex {
Niko Matsakis86b6e6e2013-05-10 17:10:35394 //! Constructs graph for `exprs` evaluated in order
Felix S. Klock IIbe9c2d12014-05-20 16:49:19395 exprs.fold(pred, |p, e| self.expr(e, p))
Niko Matsakis86b6e6e2013-05-10 17:10:35396 }
397
398 fn opt_expr(&mut self,
Nick Cameronfacdf2e2015-07-31 07:04:06399 opt_expr: &Option<P<hir::Expr>>,
Niko Matsakis86b6e6e2013-05-10 17:10:35400 pred: CFGIndex) -> CFGIndex {
401 //! Constructs graph for `opt_expr` evaluated, if Some
Jonas Schievink559fca02016-02-09 21:00:20402 opt_expr.iter().fold(pred, |p, e| self.expr(&e, p))
Niko Matsakis86b6e6e2013-05-10 17:10:35403 }
404
Nick Cameronfacdf2e2015-07-31 07:04:06405 fn straightline<'b, I: Iterator<Item=&'b hir::Expr>>(&mut self,
406 expr: &hir::Expr,
Niko Matsakis86b6e6e2013-05-10 17:10:35407 pred: CFGIndex,
Eduard Burtescub0621282014-09-07 17:09:06408 subexprs: I) -> CFGIndex {
Niko Matsakis86b6e6e2013-05-10 17:10:35409 //! Handles case of an expression that evaluates `subexprs` in order
410
Eduard Burtescub0621282014-09-07 17:09:06411 let subexprs_exit = self.exprs(subexprs, pred);
James Miller97c17112015-02-19 14:27:25412 self.add_ast_node(expr.id, &[subexprs_exit])
Niko Matsakis86b6e6e2013-05-10 17:10:35413 }
414
Nick Cameronfacdf2e2015-07-31 07:04:06415 fn match_(&mut self, id: ast::NodeId, discr: &hir::Expr,
416 arms: &[hir::Arm], pred: CFGIndex) -> CFGIndex {
James Miller4bae1332015-02-19 16:54:41417 // The CFG for match expression is quite complex, so no ASCII
418 // art for it (yet).
419 //
420 // The CFG generated below matches roughly what trans puts
421 // out. Each pattern and guard is visited in parallel, with
422 // arms containing multiple patterns generating multiple nodes
423 // for the same guard expression. The guard expressions chain
424 // into each other from top to bottom, with a specific
425 // exception to allow some additional valid programs
426 // (explained below). Trans differs slightly in that the
427 // pattern matching may continue after a guard but the visible
428 // behaviour should be the same.
429 //
430 // What is going on is explained in further comments.
431
432 // Visit the discriminant expression
433 let discr_exit = self.expr(discr, pred);
434
435 // Add a node for the exit of the match expression as a whole.
436 let expr_exit = self.add_ast_node(id, &[]);
437
438 // Keep track of the previous guard expressions
439 let mut prev_guards = Vec::new();
440 // Track if the previous pattern contained bindings or wildcards
441 let mut prev_has_bindings = false;
442
443 for arm in arms {
444 // Add an exit node for when we've visited all the
445 // patterns and the guard (if there is one) in the arm.
446 let arm_exit = self.add_dummy_node(&[]);
447
448 for pat in &arm.pats {
449 // Visit the pattern, coming from the discriminant exit
Jonas Schievink559fca02016-02-09 21:00:20450 let mut pat_exit = self.pat(&pat, discr_exit);
James Miller4bae1332015-02-19 16:54:41451
452 // If there is a guard expression, handle it here
453 if let Some(ref guard) = arm.guard {
454 // Add a dummy node for the previous guard
455 // expression to target
456 let guard_start = self.add_dummy_node(&[pat_exit]);
457 // Visit the guard expression
Jonas Schievink559fca02016-02-09 21:00:20458 let guard_exit = self.expr(&guard, guard_start);
James Miller4bae1332015-02-19 16:54:41459
Vadim Petrochenkovcf468202016-03-06 12:54:44460 let this_has_bindings = pat_util::pat_contains_bindings_or_wild(&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) {
iirelue593c3b2016-10-29 21:54:04540 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) {
iirelue593c3b2016-10-29 21:54:04549 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 {
iirelue593c3b2016-10-29 21:54:04563 exiting_scopes: vec![],
Patrick Walton36195eb2014-05-16 17:45:16564 };
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
Vadim Petrochenkovee4e5532016-06-03 20:15:00578 match self.tcx.expect_def(expr.id) {
579 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}