]> git.proxmox.com Git - rustc.git/blob - src/librustc/dep_graph/visit.rs
Imported Upstream version 1.9.0+dfsg1
[rustc.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<'tcx,V,F>(tcx: &TyCtxt<'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: &'visit TyCtxt<'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);
43 debug!("Started task {:?}", task_id);
44 self.tcx.dep_graph.read(DepNode::Hir(item_def_id));
45 self.visitor.visit_item(i)
46 }
47 }
48
49 let krate = tcx.dep_graph.with_ignore(|| tcx.map.krate());
50 let mut tracking_visitor = TrackingVisitor {
51 tcx: tcx,
52 dep_node_fn: &mut dep_node_fn,
53 visitor: visitor
54 };
55 krate.visit_all_items(&mut tracking_visitor)
56 }