]> git.lizzy.rs Git - rust.git/blob - src/librustc/dep_graph/visit.rs
Rollup merge of #35558 - lukehinds:master, r=nikomatsakis
[rust.git] / src / librustc / dep_graph / visit.rs
1 // Copyright 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 hir;
12 use hir::def_id::DefId;
13 use hir::intravisit::Visitor;
14 use ty::TyCtxt;
15
16 use super::dep_node::DepNode;
17
18
19 /// Visit all the items in the krate in some order. When visiting a
20 /// particular item, first create a dep-node by calling `dep_node_fn`
21 /// and push that onto the dep-graph stack of tasks, and also create a
22 /// read edge from the corresponding AST node. This is used in
23 /// compiler passes to automatically record the item that they are
24 /// working on.
25 pub fn visit_all_items_in_krate<'a, 'tcx, V, F>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
26                                                 mut dep_node_fn: F,
27                                                 visitor: &mut V)
28     where F: FnMut(DefId) -> DepNode<DefId>, V: Visitor<'tcx>
29 {
30     struct TrackingVisitor<'visit, 'tcx: 'visit, F: 'visit, V: 'visit> {
31         tcx: TyCtxt<'visit, 'tcx, 'tcx>,
32         dep_node_fn: &'visit mut F,
33         visitor: &'visit mut V
34     }
35
36     impl<'visit, 'tcx, F, V> Visitor<'tcx> for TrackingVisitor<'visit, 'tcx, F, V>
37         where F: FnMut(DefId) -> DepNode<DefId>, V: Visitor<'tcx>
38     {
39         fn visit_item(&mut self, i: &'tcx hir::Item) {
40             let item_def_id = self.tcx.map.local_def_id(i.id);
41             let task_id = (self.dep_node_fn)(item_def_id);
42             let _task = self.tcx.dep_graph.in_task(task_id.clone());
43             debug!("Started task {:?}", task_id);
44             assert!(!self.tcx.map.is_inlined_def_id(item_def_id));
45             self.tcx.dep_graph.read(DepNode::Hir(item_def_id));
46             self.visitor.visit_item(i);
47             debug!("Ended task {:?}", task_id);
48         }
49     }
50
51     let krate = tcx.dep_graph.with_ignore(|| tcx.map.krate());
52     let mut tracking_visitor = TrackingVisitor {
53         tcx: tcx,
54         dep_node_fn: &mut dep_node_fn,
55         visitor: visitor
56     };
57     krate.visit_all_items(&mut tracking_visitor)
58 }