1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
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.
11 use rustc_data_structures::graph;
13 use ty::{self, TyCtxt};
17 use hir::{self, PatKind};
18 use hir::def_id::DefId;
20 struct CFGBuilder<'a, 'tcx: 'a> {
21 tcx: TyCtxt<'a, 'tcx, 'tcx>,
23 tables: &'a ty::TypeckTables<'tcx>,
26 loop_scopes: Vec<LoopScope>,
27 breakable_block_scopes: Vec<BlockScope>,
30 #[derive(Copy, Clone)]
32 block_expr_id: ast::NodeId, // id of breakable block expr node
33 break_index: CFGIndex, // where to go on `break`
36 #[derive(Copy, Clone)]
38 loop_id: ast::NodeId, // id of loop/while node
39 continue_index: CFGIndex, // where to go on a `loop`
40 break_index: CFGIndex, // where to go on a `break`
43 pub fn construct<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
44 body: &hir::Body) -> CFG {
45 let mut graph = graph::Graph::new();
46 let entry = graph.add_node(CFGNodeData::Entry);
48 // `fn_exit` is target of return exprs, which lies somewhere
49 // outside input `body`. (Distinguishing `fn_exit` and `body_exit`
50 // also resolves chicken-and-egg problem that arises if you try to
51 // have return exprs jump to `body_exit` during construction.)
52 let fn_exit = graph.add_node(CFGNodeData::Exit);
55 // Find the tables for this body.
56 let owner_def_id = tcx.hir.local_def_id(tcx.hir.body_owner(body.id()));
57 let tables = tcx.typeck_tables_of(owner_def_id);
59 let mut cfg_builder = CFGBuilder {
65 loop_scopes: Vec::new(),
66 breakable_block_scopes: Vec::new(),
68 body_exit = cfg_builder.expr(&body.value, entry);
69 cfg_builder.add_contained_edge(body_exit, fn_exit);
70 let CFGBuilder { graph, .. } = cfg_builder;
78 impl<'a, 'tcx> CFGBuilder<'a, 'tcx> {
79 fn block(&mut self, blk: &hir::Block, pred: CFGIndex) -> CFGIndex {
80 if blk.targeted_by_break {
81 let expr_exit = self.add_ast_node(blk.id, &[]);
83 self.breakable_block_scopes.push(BlockScope {
84 block_expr_id: blk.id,
85 break_index: expr_exit,
88 let mut stmts_exit = pred;
89 for stmt in &blk.stmts {
90 stmts_exit = self.stmt(stmt, stmts_exit);
92 let blk_expr_exit = self.opt_expr(&blk.expr, stmts_exit);
93 self.add_contained_edge(blk_expr_exit, expr_exit);
95 self.breakable_block_scopes.pop();
99 let mut stmts_exit = pred;
100 for stmt in &blk.stmts {
101 stmts_exit = self.stmt(stmt, stmts_exit);
104 let expr_exit = self.opt_expr(&blk.expr, stmts_exit);
106 self.add_ast_node(blk.id, &[expr_exit])
110 fn stmt(&mut self, stmt: &hir::Stmt, pred: CFGIndex) -> CFGIndex {
112 hir::StmtDecl(ref decl, id) => {
113 let exit = self.decl(&decl, pred);
114 self.add_ast_node(id, &[exit])
117 hir::StmtExpr(ref expr, id) |
118 hir::StmtSemi(ref expr, id) => {
119 let exit = self.expr(&expr, pred);
120 self.add_ast_node(id, &[exit])
125 fn decl(&mut self, decl: &hir::Decl, pred: CFGIndex) -> CFGIndex {
127 hir::DeclLocal(ref local) => {
128 let init_exit = self.opt_expr(&local.init, pred);
129 self.pat(&local.pat, init_exit)
132 hir::DeclItem(_) => pred,
136 fn pat(&mut self, pat: &hir::Pat, pred: CFGIndex) -> CFGIndex {
138 PatKind::Binding(.., None) |
142 PatKind::Wild => self.add_ast_node(pat.id, &[pred]),
144 PatKind::Box(ref subpat) |
145 PatKind::Ref(ref subpat, _) |
146 PatKind::Binding(.., Some(ref subpat)) => {
147 let subpat_exit = self.pat(&subpat, pred);
148 self.add_ast_node(pat.id, &[subpat_exit])
151 PatKind::TupleStruct(_, ref subpats, _) |
152 PatKind::Tuple(ref subpats, _) => {
153 let pats_exit = self.pats_all(subpats.iter(), pred);
154 self.add_ast_node(pat.id, &[pats_exit])
157 PatKind::Struct(_, ref subpats, _) => {
158 let pats_exit = self.pats_all(subpats.iter().map(|f| &f.node.pat), pred);
159 self.add_ast_node(pat.id, &[pats_exit])
162 PatKind::Slice(ref pre, ref vec, ref post) => {
163 let pre_exit = self.pats_all(pre.iter(), pred);
164 let vec_exit = self.pats_all(vec.iter(), pre_exit);
165 let post_exit = self.pats_all(post.iter(), vec_exit);
166 self.add_ast_node(pat.id, &[post_exit])
171 fn pats_all<'b, I: Iterator<Item=&'b P<hir::Pat>>>(&mut self,
173 pred: CFGIndex) -> CFGIndex {
174 //! Handles case where all of the patterns must match.
175 pats.fold(pred, |pred, pat| self.pat(&pat, pred))
178 fn expr(&mut self, expr: &hir::Expr, pred: CFGIndex) -> CFGIndex {
180 hir::ExprBlock(ref blk) => {
181 let blk_exit = self.block(&blk, pred);
182 self.add_ast_node(expr.id, &[blk_exit])
185 hir::ExprIf(ref cond, ref then, None) => {
200 let cond_exit = self.expr(&cond, pred); // 1
201 let then_exit = self.expr(&then, cond_exit); // 2
202 self.add_ast_node(expr.id, &[cond_exit, then_exit]) // 3,4
205 hir::ExprIf(ref cond, ref then, Some(ref otherwise)) => {
220 let cond_exit = self.expr(&cond, pred); // 1
221 let then_exit = self.expr(&then, cond_exit); // 2
222 let else_exit = self.expr(&otherwise, cond_exit); // 3
223 self.add_ast_node(expr.id, &[then_exit, else_exit]) // 4, 5
226 hir::ExprWhile(ref cond, ref body, _) => {
241 // Note that `break` and `continue` statements
242 // may cause additional edges.
244 let loopback = self.add_dummy_node(&[pred]); // 1
246 // Create expr_exit without pred (cond_exit)
247 let expr_exit = self.add_ast_node(expr.id, &[]); // 3
249 // The LoopScope needs to be on the loop_scopes stack while evaluating the
250 // condition and the body of the loop (both can break out of the loop)
251 self.loop_scopes.push(LoopScope {
253 continue_index: loopback,
254 break_index: expr_exit
257 let cond_exit = self.expr(&cond, loopback); // 2
259 // Add pred (cond_exit) to expr_exit
260 self.add_contained_edge(cond_exit, expr_exit);
262 let body_exit = self.block(&body, cond_exit); // 4
263 self.add_contained_edge(body_exit, loopback); // 5
264 self.loop_scopes.pop();
268 hir::ExprLoop(ref body, _, _) => {
280 // Note that `break` and `loop` statements
281 // may cause additional edges.
283 let loopback = self.add_dummy_node(&[pred]); // 1
284 let expr_exit = self.add_ast_node(expr.id, &[]); // 2
285 self.loop_scopes.push(LoopScope {
287 continue_index: loopback,
288 break_index: expr_exit,
290 let body_exit = self.block(&body, loopback); // 3
291 self.add_contained_edge(body_exit, loopback); // 4
292 self.loop_scopes.pop();
296 hir::ExprMatch(ref discr, ref arms, _) => {
297 self.match_(expr.id, &discr, &arms, pred)
300 hir::ExprBinary(op, ref l, ref r) if op.node.is_lazy() => {
315 let l_exit = self.expr(&l, pred); // 1
316 let r_exit = self.expr(&r, l_exit); // 2
317 self.add_ast_node(expr.id, &[l_exit, r_exit]) // 3,4
320 hir::ExprRet(ref v) => {
321 let v_exit = self.opt_expr(v, pred);
322 let b = self.add_ast_node(expr.id, &[v_exit]);
323 self.add_returning_edge(expr, b);
324 self.add_unreachable_node()
327 hir::ExprBreak(destination, ref opt_expr) => {
328 let v = self.opt_expr(opt_expr, pred);
329 let (scope_id, break_dest) =
330 self.find_scope_edge(expr, destination, ScopeCfKind::Break);
331 let b = self.add_ast_node(expr.id, &[v]);
332 self.add_exiting_edge(expr, b, scope_id, break_dest);
333 self.add_unreachable_node()
336 hir::ExprAgain(destination) => {
337 let (scope_id, cont_dest) =
338 self.find_scope_edge(expr, destination, ScopeCfKind::Continue);
339 let a = self.add_ast_node(expr.id, &[pred]);
340 self.add_exiting_edge(expr, a, scope_id, cont_dest);
341 self.add_unreachable_node()
344 hir::ExprArray(ref elems) => {
345 self.straightline(expr, pred, elems.iter().map(|e| &*e))
348 hir::ExprCall(ref func, ref args) => {
349 self.call(expr, pred, &func, args.iter().map(|e| &*e))
352 hir::ExprMethodCall(.., ref args) => {
353 self.call(expr, pred, &args[0], args[1..].iter().map(|e| &*e))
356 hir::ExprIndex(ref l, ref r) |
357 hir::ExprBinary(_, ref l, ref r) if self.tables.is_method_call(expr.id) => {
358 self.call(expr, pred, &l, Some(&**r).into_iter())
361 hir::ExprUnary(_, ref e) if self.tables.is_method_call(expr.id) => {
362 self.call(expr, pred, &e, None::<hir::Expr>.iter())
365 hir::ExprTup(ref exprs) => {
366 self.straightline(expr, pred, exprs.iter().map(|e| &*e))
369 hir::ExprStruct(_, ref fields, ref base) => {
370 let field_cfg = self.straightline(expr, pred, fields.iter().map(|f| &*f.expr));
371 self.opt_expr(base, field_cfg)
374 hir::ExprAssign(ref l, ref r) |
375 hir::ExprAssignOp(_, ref l, ref r) => {
376 self.straightline(expr, pred, [r, l].iter().map(|&e| &**e))
379 hir::ExprIndex(ref l, ref r) |
380 hir::ExprBinary(_, ref l, ref r) => { // NB: && and || handled earlier
381 self.straightline(expr, pred, [l, r].iter().map(|&e| &**e))
384 hir::ExprBox(ref e) |
385 hir::ExprAddrOf(_, ref e) |
386 hir::ExprCast(ref e, _) |
387 hir::ExprType(ref e, _) |
388 hir::ExprUnary(_, ref e) |
389 hir::ExprField(ref e, _) |
390 hir::ExprTupField(ref e, _) |
391 hir::ExprRepeat(ref e, _) => {
392 self.straightline(expr, pred, Some(&**e).into_iter())
395 hir::ExprInlineAsm(_, ref outputs, ref inputs) => {
396 let post_outputs = self.exprs(outputs.iter().map(|e| &*e), pred);
397 let post_inputs = self.exprs(inputs.iter().map(|e| &*e), post_outputs);
398 self.add_ast_node(expr.id, &[post_inputs])
401 hir::ExprClosure(..) |
403 hir::ExprPath(_) => {
404 self.straightline(expr, pred, None::<hir::Expr>.iter())
409 fn call<'b, I: Iterator<Item=&'b hir::Expr>>(&mut self,
410 call_expr: &hir::Expr,
412 func_or_rcvr: &hir::Expr,
413 args: I) -> CFGIndex {
414 let method_call = ty::MethodCall::expr(call_expr.id);
415 let fn_ty = match self.tables.method_map.get(&method_call) {
416 Some(method) => method.ty,
417 None => self.tables.expr_ty_adjusted(func_or_rcvr),
420 let func_or_rcvr_exit = self.expr(func_or_rcvr, pred);
421 let ret = self.straightline(call_expr, func_or_rcvr_exit, args);
422 // FIXME(canndrew): This is_never should probably be an is_uninhabited.
423 if fn_ty.fn_ret().0.is_never() {
424 self.add_unreachable_node()
430 fn exprs<'b, I: Iterator<Item=&'b hir::Expr>>(&mut self,
432 pred: CFGIndex) -> CFGIndex {
433 //! Constructs graph for `exprs` evaluated in order
434 exprs.fold(pred, |p, e| self.expr(e, p))
437 fn opt_expr(&mut self,
438 opt_expr: &Option<P<hir::Expr>>,
439 pred: CFGIndex) -> CFGIndex {
440 //! Constructs graph for `opt_expr` evaluated, if Some
441 opt_expr.iter().fold(pred, |p, e| self.expr(&e, p))
444 fn straightline<'b, I: Iterator<Item=&'b hir::Expr>>(&mut self,
447 subexprs: I) -> CFGIndex {
448 //! Handles case of an expression that evaluates `subexprs` in order
450 let subexprs_exit = self.exprs(subexprs, pred);
451 self.add_ast_node(expr.id, &[subexprs_exit])
454 fn match_(&mut self, id: ast::NodeId, discr: &hir::Expr,
455 arms: &[hir::Arm], pred: CFGIndex) -> CFGIndex {
456 // The CFG for match expression is quite complex, so no ASCII
459 // The CFG generated below matches roughly what trans puts
460 // out. Each pattern and guard is visited in parallel, with
461 // arms containing multiple patterns generating multiple nodes
462 // for the same guard expression. The guard expressions chain
463 // into each other from top to bottom, with a specific
464 // exception to allow some additional valid programs
465 // (explained below). Trans differs slightly in that the
466 // pattern matching may continue after a guard but the visible
467 // behaviour should be the same.
469 // What is going on is explained in further comments.
471 // Visit the discriminant expression
472 let discr_exit = self.expr(discr, pred);
474 // Add a node for the exit of the match expression as a whole.
475 let expr_exit = self.add_ast_node(id, &[]);
477 // Keep track of the previous guard expressions
478 let mut prev_guards = Vec::new();
479 // Track if the previous pattern contained bindings or wildcards
480 let mut prev_has_bindings = false;
483 // Add an exit node for when we've visited all the
484 // patterns and the guard (if there is one) in the arm.
485 let arm_exit = self.add_dummy_node(&[]);
487 for pat in &arm.pats {
488 // Visit the pattern, coming from the discriminant exit
489 let mut pat_exit = self.pat(&pat, discr_exit);
491 // If there is a guard expression, handle it here
492 if let Some(ref guard) = arm.guard {
493 // Add a dummy node for the previous guard
494 // expression to target
495 let guard_start = self.add_dummy_node(&[pat_exit]);
496 // Visit the guard expression
497 let guard_exit = self.expr(&guard, guard_start);
499 let this_has_bindings = pat.contains_bindings_or_wild();
501 // If both this pattern and the previous pattern
502 // were free of bindings, they must consist only
503 // of "constant" patterns. Note we cannot match an
504 // all-constant pattern, fail the guard, and then
505 // match *another* all-constant pattern. This is
506 // because if the previous pattern matches, then
507 // we *cannot* match this one, unless all the
508 // constants are the same (which is rejected by
511 // We can use this to be smarter about the flow
512 // along guards. If the previous pattern matched,
513 // then we know we will not visit the guard in
514 // this one (whether or not the guard succeeded),
515 // if the previous pattern failed, then we know
516 // the guard for that pattern will not have been
517 // visited. Thus, it is not possible to visit both
518 // the previous guard and the current one when
519 // both patterns consist only of constant
522 // However, if the above does not hold, then all
523 // previous guards need to be wired to visit the
524 // current guard pattern.
525 if prev_has_bindings || this_has_bindings {
526 while let Some(prev) = prev_guards.pop() {
527 self.add_contained_edge(prev, guard_start);
531 prev_has_bindings = this_has_bindings;
533 // Push the guard onto the list of previous guards
534 prev_guards.push(guard_exit);
536 // Update the exit node for the pattern
537 pat_exit = guard_exit;
540 // Add an edge from the exit of this pattern to the
542 self.add_contained_edge(pat_exit, arm_exit);
545 // Visit the body of this arm
546 let body_exit = self.expr(&arm.body, arm_exit);
548 // Link the body to the exit of the expression
549 self.add_contained_edge(body_exit, expr_exit);
555 fn add_dummy_node(&mut self, preds: &[CFGIndex]) -> CFGIndex {
556 self.add_node(CFGNodeData::Dummy, preds)
559 fn add_ast_node(&mut self, id: ast::NodeId, preds: &[CFGIndex]) -> CFGIndex {
560 assert!(id != ast::DUMMY_NODE_ID);
561 self.add_node(CFGNodeData::AST(id), preds)
564 fn add_unreachable_node(&mut self) -> CFGIndex {
565 self.add_node(CFGNodeData::Unreachable, &[])
568 fn add_node(&mut self, data: CFGNodeData, preds: &[CFGIndex]) -> CFGIndex {
569 let node = self.graph.add_node(data);
571 self.add_contained_edge(pred, node);
576 fn add_contained_edge(&mut self,
579 let data = CFGEdgeData {exiting_scopes: vec![] };
580 self.graph.add_edge(source, target, data);
583 fn add_exiting_edge(&mut self,
584 from_expr: &hir::Expr,
585 from_index: CFGIndex,
586 scope_id: ast::NodeId,
587 to_index: CFGIndex) {
588 let mut data = CFGEdgeData { exiting_scopes: vec![] };
589 let mut scope = self.tcx.node_extent(from_expr.id);
590 let target_scope = self.tcx.node_extent(scope_id);
591 let region_maps = self.tcx.region_maps(self.owner_def_id);
592 while scope != target_scope {
593 data.exiting_scopes.push(scope.node_id());
594 scope = region_maps.encl_scope(scope);
596 self.graph.add_edge(from_index, to_index, data);
599 fn add_returning_edge(&mut self,
600 _from_expr: &hir::Expr,
601 from_index: CFGIndex) {
602 let mut data = CFGEdgeData {
603 exiting_scopes: vec![],
605 for &LoopScope { loop_id: id, .. } in self.loop_scopes.iter().rev() {
606 data.exiting_scopes.push(id);
608 self.graph.add_edge(from_index, self.fn_exit, data);
611 fn find_scope_edge(&self,
613 destination: hir::Destination,
614 scope_cf_kind: ScopeCfKind) -> (ast::NodeId, CFGIndex) {
616 match destination.target_id {
617 hir::ScopeTarget::Block(block_expr_id) => {
618 for b in &self.breakable_block_scopes {
619 if b.block_expr_id == block_expr_id {
620 return (block_expr_id, match scope_cf_kind {
621 ScopeCfKind::Break => b.break_index,
622 ScopeCfKind::Continue => bug!("can't continue to block"),
626 span_bug!(expr.span, "no block expr for id {}", block_expr_id);
628 hir::ScopeTarget::Loop(hir::LoopIdResult::Ok(loop_id)) => {
629 for l in &self.loop_scopes {
630 if l.loop_id == loop_id {
631 return (loop_id, match scope_cf_kind {
632 ScopeCfKind::Break => l.break_index,
633 ScopeCfKind::Continue => l.continue_index,
637 span_bug!(expr.span, "no loop scope for id {}", loop_id);
639 hir::ScopeTarget::Loop(hir::LoopIdResult::Err(err)) =>
640 span_bug!(expr.span, "loop scope error: {}", err),
645 #[derive(Copy, Clone, Eq, PartialEq)]