]> git.lizzy.rs Git - rust.git/blob - src/librustc/middle/cfg/construct.rs
Auto merge of #28069 - alexcrichton:rt-atexit, r=brson
[rust.git] / src / librustc / middle / cfg / construct.rs
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.
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
11 use rustc_data_structures::graph;
12 use middle::cfg::*;
13 use middle::def;
14 use middle::pat_util;
15 use middle::ty;
16 use syntax::ast;
17 use syntax::ptr::P;
18
19 use rustc_front::hir;
20
21 struct CFGBuilder<'a, 'tcx: 'a> {
22     tcx: &'a ty::ctxt<'tcx>,
23     graph: CFGGraph,
24     fn_exit: CFGIndex,
25     loop_scopes: Vec<LoopScope>,
26 }
27
28 #[derive(Copy, Clone)]
29 struct LoopScope {
30     loop_id: ast::NodeId,     // id of loop/while node
31     continue_index: CFGIndex, // where to go on a `loop`
32     break_index: CFGIndex,    // where to go on a `break
33 }
34
35 pub fn construct(tcx: &ty::ctxt,
36                  blk: &hir::Block) -> CFG {
37     let mut graph = graph::Graph::new();
38     let entry = graph.add_node(CFGNodeData::Entry);
39
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.)
44     let fn_exit = graph.add_node(CFGNodeData::Exit);
45     let block_exit;
46
47     let mut cfg_builder = CFGBuilder {
48         graph: graph,
49         fn_exit: fn_exit,
50         tcx: tcx,
51         loop_scopes: Vec::new()
52     };
53     block_exit = cfg_builder.block(blk, entry);
54     cfg_builder.add_contained_edge(block_exit, fn_exit);
55     let CFGBuilder {graph, ..} = cfg_builder;
56     CFG {graph: graph,
57          entry: entry,
58          exit: fn_exit}
59 }
60
61 impl<'a, 'tcx> CFGBuilder<'a, 'tcx> {
62     fn block(&mut self, blk: &hir::Block, pred: CFGIndex) -> CFGIndex {
63         let mut stmts_exit = pred;
64         for stmt in &blk.stmts {
65             stmts_exit = self.stmt(&**stmt, stmts_exit);
66         }
67
68         let expr_exit = self.opt_expr(&blk.expr, stmts_exit);
69
70         self.add_ast_node(blk.id, &[expr_exit])
71     }
72
73     fn stmt(&mut self, stmt: &hir::Stmt, pred: CFGIndex) -> CFGIndex {
74         match stmt.node {
75             hir::StmtDecl(ref decl, id) => {
76                 let exit = self.decl(&**decl, pred);
77                 self.add_ast_node(id, &[exit])
78             }
79
80             hir::StmtExpr(ref expr, id) | hir::StmtSemi(ref expr, id) => {
81                 let exit = self.expr(&**expr, pred);
82                 self.add_ast_node(id, &[exit])
83             }
84         }
85     }
86
87     fn decl(&mut self, decl: &hir::Decl, pred: CFGIndex) -> CFGIndex {
88         match decl.node {
89             hir::DeclLocal(ref local) => {
90                 let init_exit = self.opt_expr(&local.init, pred);
91                 self.pat(&*local.pat, init_exit)
92             }
93
94             hir::DeclItem(_) => {
95                 pred
96             }
97         }
98     }
99
100     fn pat(&mut self, pat: &hir::Pat, pred: CFGIndex) -> CFGIndex {
101         match pat.node {
102             hir::PatIdent(_, _, None) |
103             hir::PatEnum(_, None) |
104             hir::PatQPath(..) |
105             hir::PatLit(..) |
106             hir::PatRange(..) |
107             hir::PatWild(_) => {
108                 self.add_ast_node(pat.id, &[pred])
109             }
110
111             hir::PatBox(ref subpat) |
112             hir::PatRegion(ref subpat, _) |
113             hir::PatIdent(_, _, Some(ref subpat)) => {
114                 let subpat_exit = self.pat(&**subpat, pred);
115                 self.add_ast_node(pat.id, &[subpat_exit])
116             }
117
118             hir::PatEnum(_, Some(ref subpats)) |
119             hir::PatTup(ref subpats) => {
120                 let pats_exit = self.pats_all(subpats.iter(), pred);
121                 self.add_ast_node(pat.id, &[pats_exit])
122             }
123
124             hir::PatStruct(_, ref subpats, _) => {
125                 let pats_exit =
126                     self.pats_all(subpats.iter().map(|f| &f.node.pat), pred);
127                 self.add_ast_node(pat.id, &[pats_exit])
128             }
129
130             hir::PatVec(ref pre, ref vec, ref post) => {
131                 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);
134                 self.add_ast_node(pat.id, &[post_exit])
135             }
136         }
137     }
138
139     fn pats_all<'b, I: Iterator<Item=&'b P<hir::Pat>>>(&mut self,
140                                           pats: I,
141                                           pred: CFGIndex) -> CFGIndex {
142         //! Handles case where all of the patterns must match.
143         pats.fold(pred, |pred, pat| self.pat(&**pat, pred))
144     }
145
146     fn expr(&mut self, expr: &hir::Expr, pred: CFGIndex) -> CFGIndex {
147         match expr.node {
148             hir::ExprBlock(ref blk) => {
149                 let blk_exit = self.block(&**blk, pred);
150                 self.add_ast_node(expr.id, &[blk_exit])
151             }
152
153             hir::ExprIf(ref cond, ref then, None) => {
154                 //
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                 //
168                 let cond_exit = self.expr(&**cond, pred);                // 1
169                 let then_exit = self.block(&**then, cond_exit);          // 2
170                 self.add_ast_node(expr.id, &[cond_exit, then_exit])      // 3,4
171             }
172
173             hir::ExprIf(ref cond, ref then, Some(ref otherwise)) => {
174                 //
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                 //
188                 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
191                 self.add_ast_node(expr.id, &[then_exit, else_exit])      // 4, 5
192             }
193
194             hir::ExprWhile(ref cond, ref body, _) => {
195                 //
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                 //
209                 // Note that `break` and `continue` statements
210                 // may cause additional edges.
211
212                 // Is the condition considered part of the loop?
213                 let loopback = self.add_dummy_node(&[pred]);              // 1
214                 let cond_exit = self.expr(&**cond, loopback);             // 2
215                 let expr_exit = self.add_ast_node(expr.id, &[cond_exit]); // 3
216                 self.loop_scopes.push(LoopScope {
217                     loop_id: expr.id,
218                     continue_index: loopback,
219                     break_index: expr_exit
220                 });
221                 let body_exit = self.block(&**body, cond_exit);          // 4
222                 self.add_contained_edge(body_exit, loopback);            // 5
223                 self.loop_scopes.pop();
224                 expr_exit
225             }
226
227             hir::ExprLoop(ref body, _) => {
228                 //
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
242                 let loopback = self.add_dummy_node(&[pred]);              // 1
243                 let expr_exit = self.add_ast_node(expr.id, &[]);          // 2
244                 self.loop_scopes.push(LoopScope {
245                     loop_id: expr.id,
246                     continue_index: loopback,
247                     break_index: expr_exit,
248                 });
249                 let body_exit = self.block(&**body, loopback);           // 3
250                 self.add_contained_edge(body_exit, loopback);            // 4
251                 self.loop_scopes.pop();
252                 expr_exit
253             }
254
255             hir::ExprMatch(ref discr, ref arms, _) => {
256                 self.match_(expr.id, &discr, &arms, pred)
257             }
258
259             hir::ExprBinary(op, ref l, ref r) if ::rustc_front::util::lazy_binop(op.node) => {
260                 //
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                 //
274                 let l_exit = self.expr(&**l, pred);                      // 1
275                 let r_exit = self.expr(&**r, l_exit);                    // 2
276                 self.add_ast_node(expr.id, &[l_exit, r_exit])            // 3,4
277             }
278
279             hir::ExprRet(ref v) => {
280                 let v_exit = self.opt_expr(v, pred);
281                 let b = self.add_ast_node(expr.id, &[v_exit]);
282                 self.add_returning_edge(expr, b);
283                 self.add_unreachable_node()
284             }
285
286             hir::ExprBreak(label) => {
287                 let loop_scope = self.find_scope(expr, label.map(|l| l.node));
288                 let b = self.add_ast_node(expr.id, &[pred]);
289                 self.add_exiting_edge(expr, b,
290                                       loop_scope, loop_scope.break_index);
291                 self.add_unreachable_node()
292             }
293
294             hir::ExprAgain(label) => {
295                 let loop_scope = self.find_scope(expr, label.map(|l| l.node));
296                 let a = self.add_ast_node(expr.id, &[pred]);
297                 self.add_exiting_edge(expr, a,
298                                       loop_scope, loop_scope.continue_index);
299                 self.add_unreachable_node()
300             }
301
302             hir::ExprVec(ref elems) => {
303                 self.straightline(expr, pred, elems.iter().map(|e| &**e))
304             }
305
306             hir::ExprCall(ref func, ref args) => {
307                 self.call(expr, pred, &**func, args.iter().map(|e| &**e))
308             }
309
310             hir::ExprMethodCall(_, _, ref args) => {
311                 self.call(expr, pred, &*args[0], args[1..].iter().map(|e| &**e))
312             }
313
314             hir::ExprIndex(ref l, ref r) |
315             hir::ExprBinary(_, ref l, ref r) if self.tcx.is_method_call(expr.id) => {
316                 self.call(expr, pred, &**l, Some(&**r).into_iter())
317             }
318
319             hir::ExprRange(ref start, ref end) => {
320                 let fields = start.as_ref().map(|e| &**e).into_iter()
321                     .chain(end.as_ref().map(|e| &**e));
322                 self.straightline(expr, pred, fields)
323             }
324
325             hir::ExprUnary(_, ref e) if self.tcx.is_method_call(expr.id) => {
326                 self.call(expr, pred, &**e, None::<hir::Expr>.iter())
327             }
328
329             hir::ExprTup(ref exprs) => {
330                 self.straightline(expr, pred, exprs.iter().map(|e| &**e))
331             }
332
333             hir::ExprStruct(_, ref fields, ref base) => {
334                 let field_cfg = self.straightline(expr, pred, fields.iter().map(|f| &*f.expr));
335                 self.opt_expr(base, field_cfg)
336             }
337
338             hir::ExprRepeat(ref elem, ref count) => {
339                 self.straightline(expr, pred, [elem, count].iter().map(|&e| &**e))
340             }
341
342             hir::ExprAssign(ref l, ref r) |
343             hir::ExprAssignOp(_, ref l, ref r) => {
344                 self.straightline(expr, pred, [r, l].iter().map(|&e| &**e))
345             }
346
347             hir::ExprBox(Some(ref l), ref r) |
348             hir::ExprIndex(ref l, ref r) |
349             hir::ExprBinary(_, ref l, ref r) => { // NB: && and || handled earlier
350                 self.straightline(expr, pred, [l, r].iter().map(|&e| &**e))
351             }
352
353             hir::ExprBox(None, ref e) |
354             hir::ExprAddrOf(_, ref e) |
355             hir::ExprCast(ref e, _) |
356             hir::ExprUnary(_, ref e) |
357             hir::ExprParen(ref e) |
358             hir::ExprField(ref e, _) |
359             hir::ExprTupField(ref e, _) => {
360                 self.straightline(expr, pred, Some(&**e).into_iter())
361             }
362
363             hir::ExprInlineAsm(ref inline_asm) => {
364                 let inputs = inline_asm.inputs.iter();
365                 let outputs = inline_asm.outputs.iter();
366                 let post_inputs = self.exprs(inputs.map(|a| {
367                     debug!("cfg::construct InlineAsm id:{} input:{:?}", expr.id, a);
368                     let &(_, ref expr) = a;
369                     &**expr
370                 }), pred);
371                 let post_outputs = self.exprs(outputs.map(|a| {
372                     debug!("cfg::construct InlineAsm id:{} output:{:?}", expr.id, a);
373                     let &(_, ref expr, _) = a;
374                     &**expr
375                 }), post_inputs);
376                 self.add_ast_node(expr.id, &[post_outputs])
377             }
378
379             hir::ExprClosure(..) |
380             hir::ExprLit(..) |
381             hir::ExprPath(..) => {
382                 self.straightline(expr, pred, None::<hir::Expr>.iter())
383             }
384         }
385     }
386
387     fn call<'b, I: Iterator<Item=&'b hir::Expr>>(&mut self,
388             call_expr: &hir::Expr,
389             pred: CFGIndex,
390             func_or_rcvr: &hir::Expr,
391             args: I) -> CFGIndex {
392         let method_call = ty::MethodCall::expr(call_expr.id);
393         let fn_ty = match self.tcx.tables.borrow().method_map.get(&method_call) {
394             Some(method) => method.ty,
395             None => self.tcx.expr_ty_adjusted(func_or_rcvr)
396         };
397
398         let func_or_rcvr_exit = self.expr(func_or_rcvr, pred);
399         let ret = self.straightline(call_expr, func_or_rcvr_exit, args);
400         if fn_ty.fn_ret().diverges() {
401             self.add_unreachable_node()
402         } else {
403             ret
404         }
405     }
406
407     fn exprs<'b, I: Iterator<Item=&'b hir::Expr>>(&mut self,
408                                              exprs: I,
409                                              pred: CFGIndex) -> CFGIndex {
410         //! Constructs graph for `exprs` evaluated in order
411         exprs.fold(pred, |p, e| self.expr(e, p))
412     }
413
414     fn opt_expr(&mut self,
415                 opt_expr: &Option<P<hir::Expr>>,
416                 pred: CFGIndex) -> CFGIndex {
417         //! Constructs graph for `opt_expr` evaluated, if Some
418         opt_expr.iter().fold(pred, |p, e| self.expr(&**e, p))
419     }
420
421     fn straightline<'b, I: Iterator<Item=&'b hir::Expr>>(&mut self,
422                     expr: &hir::Expr,
423                     pred: CFGIndex,
424                     subexprs: I) -> CFGIndex {
425         //! Handles case of an expression that evaluates `subexprs` in order
426
427         let subexprs_exit = self.exprs(subexprs, pred);
428         self.add_ast_node(expr.id, &[subexprs_exit])
429     }
430
431     fn match_(&mut self, id: ast::NodeId, discr: &hir::Expr,
432               arms: &[hir::Arm], pred: CFGIndex) -> CFGIndex {
433         // The CFG for match expression is quite complex, so no ASCII
434         // art for it (yet).
435         //
436         // The CFG generated below matches roughly what trans puts
437         // out. Each pattern and guard is visited in parallel, with
438         // arms containing multiple patterns generating multiple nodes
439         // for the same guard expression. The guard expressions chain
440         // into each other from top to bottom, with a specific
441         // exception to allow some additional valid programs
442         // (explained below). Trans differs slightly in that the
443         // pattern matching may continue after a guard but the visible
444         // behaviour should be the same.
445         //
446         // What is going on is explained in further comments.
447
448         // Visit the discriminant expression
449         let discr_exit = self.expr(discr, pred);
450
451         // Add a node for the exit of the match expression as a whole.
452         let expr_exit = self.add_ast_node(id, &[]);
453
454         // Keep track of the previous guard expressions
455         let mut prev_guards = Vec::new();
456         // Track if the previous pattern contained bindings or wildcards
457         let mut prev_has_bindings = false;
458
459         for arm in arms {
460             // Add an exit node for when we've visited all the
461             // patterns and the guard (if there is one) in the arm.
462             let arm_exit = self.add_dummy_node(&[]);
463
464             for pat in &arm.pats {
465                 // Visit the pattern, coming from the discriminant exit
466                 let mut pat_exit = self.pat(&**pat, discr_exit);
467
468                 // If there is a guard expression, handle it here
469                 if let Some(ref guard) = arm.guard {
470                     // Add a dummy node for the previous guard
471                     // expression to target
472                     let guard_start = self.add_dummy_node(&[pat_exit]);
473                     // Visit the guard expression
474                     let guard_exit = self.expr(&**guard, guard_start);
475
476                     let this_has_bindings = pat_util::pat_contains_bindings_or_wild(
477                         &self.tcx.def_map, &**pat);
478
479                     // If both this pattern and the previous pattern
480                     // were free of bindings, they must consist only
481                     // of "constant" patterns. Note we cannot match an
482                     // all-constant pattern, fail the guard, and then
483                     // match *another* all-constant pattern. This is
484                     // because if the previous pattern matches, then
485                     // we *cannot* match this one, unless all the
486                     // constants are the same (which is rejected by
487                     // `check_match`).
488                     //
489                     // We can use this to be smarter about the flow
490                     // along guards. If the previous pattern matched,
491                     // then we know we will not visit the guard in
492                     // this one (whether or not the guard succeeded),
493                     // if the previous pattern failed, then we know
494                     // the guard for that pattern will not have been
495                     // visited. Thus, it is not possible to visit both
496                     // the previous guard and the current one when
497                     // both patterns consist only of constant
498                     // sub-patterns.
499                     //
500                     // However, if the above does not hold, then all
501                     // previous guards need to be wired to visit the
502                     // current guard pattern.
503                     if prev_has_bindings || this_has_bindings {
504                         while let Some(prev) = prev_guards.pop() {
505                             self.add_contained_edge(prev, guard_start);
506                         }
507                     }
508
509                     prev_has_bindings = this_has_bindings;
510
511                     // Push the guard onto the list of previous guards
512                     prev_guards.push(guard_exit);
513
514                     // Update the exit node for the pattern
515                     pat_exit = guard_exit;
516                 }
517
518                 // Add an edge from the exit of this pattern to the
519                 // exit of the arm
520                 self.add_contained_edge(pat_exit, arm_exit);
521             }
522
523             // Visit the body of this arm
524             let body_exit = self.expr(&arm.body, arm_exit);
525
526             // Link the body to the exit of the expression
527             self.add_contained_edge(body_exit, expr_exit);
528         }
529
530         expr_exit
531     }
532
533     fn add_dummy_node(&mut self, preds: &[CFGIndex]) -> CFGIndex {
534         self.add_node(CFGNodeData::Dummy, preds)
535     }
536
537     fn add_ast_node(&mut self, id: ast::NodeId, preds: &[CFGIndex]) -> CFGIndex {
538         assert!(id != ast::DUMMY_NODE_ID);
539         self.add_node(CFGNodeData::AST(id), preds)
540     }
541
542     fn add_unreachable_node(&mut self) -> CFGIndex {
543         self.add_node(CFGNodeData::Unreachable, &[])
544     }
545
546     fn add_node(&mut self, data: CFGNodeData, preds: &[CFGIndex]) -> CFGIndex {
547         let node = self.graph.add_node(data);
548         for &pred in preds {
549             self.add_contained_edge(pred, node);
550         }
551         node
552     }
553
554     fn add_contained_edge(&mut self,
555                           source: CFGIndex,
556                           target: CFGIndex) {
557         let data = CFGEdgeData {exiting_scopes: vec!() };
558         self.graph.add_edge(source, target, data);
559     }
560
561     fn add_exiting_edge(&mut self,
562                         from_expr: &hir::Expr,
563                         from_index: CFGIndex,
564                         to_loop: LoopScope,
565                         to_index: CFGIndex) {
566         let mut data = CFGEdgeData {exiting_scopes: vec!() };
567         let mut scope = self.tcx.region_maps.node_extent(from_expr.id);
568         let target_scope = self.tcx.region_maps.node_extent(to_loop.loop_id);
569         while scope != target_scope {
570             data.exiting_scopes.push(scope.node_id(&self.tcx.region_maps));
571             scope = self.tcx.region_maps.encl_scope(scope);
572         }
573         self.graph.add_edge(from_index, to_index, data);
574     }
575
576     fn add_returning_edge(&mut self,
577                           _from_expr: &hir::Expr,
578                           from_index: CFGIndex) {
579         let mut data = CFGEdgeData {
580             exiting_scopes: vec!(),
581         };
582         for &LoopScope { loop_id: id, .. } in self.loop_scopes.iter().rev() {
583             data.exiting_scopes.push(id);
584         }
585         self.graph.add_edge(from_index, self.fn_exit, data);
586     }
587
588     fn find_scope(&self,
589                   expr: &hir::Expr,
590                   label: Option<ast::Ident>) -> LoopScope {
591         if label.is_none() {
592             return *self.loop_scopes.last().unwrap();
593         }
594
595         match self.tcx.def_map.borrow().get(&expr.id).map(|d| d.full_def()) {
596             Some(def::DefLabel(loop_id)) => {
597                 for l in &self.loop_scopes {
598                     if l.loop_id == loop_id {
599                         return *l;
600                     }
601                 }
602                 self.tcx.sess.span_bug(expr.span,
603                     &format!("no loop scope for id {}", loop_id));
604             }
605
606             r => {
607                 self.tcx.sess.span_bug(expr.span,
608                     &format!("bad entry `{:?}` in def_map for label", r));
609             }
610         }
611     }
612 }