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.
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.
12 use hir::def_id::DefId;
13 use hir::itemlikevisit::ItemLikeVisitor;
16 use super::dep_node::DepNode;
18 /// Visit all the items in the krate in some order. When visiting a
19 /// particular item, first create a dep-node by calling `dep_node_fn`
20 /// and push that onto the dep-graph stack of tasks, and also create a
21 /// read edge from the corresponding AST node. This is used in
22 /// compiler passes to automatically record the item that they are
24 pub fn visit_all_item_likes_in_krate<'a, 'tcx, V, F>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
27 where F: FnMut(DefId) -> DepNode<DefId>, V: ItemLikeVisitor<'tcx>
29 struct TrackingVisitor<'visit, 'tcx: 'visit, F: 'visit, V: 'visit> {
30 tcx: TyCtxt<'visit, 'tcx, 'tcx>,
31 dep_node_fn: &'visit mut F,
32 visitor: &'visit mut V
35 impl<'visit, 'tcx, F, V> ItemLikeVisitor<'tcx> for TrackingVisitor<'visit, 'tcx, F, V>
36 where F: FnMut(DefId) -> DepNode<DefId>, V: ItemLikeVisitor<'tcx>
38 fn visit_item(&mut self, i: &'tcx hir::Item) {
39 let item_def_id = self.tcx.map.local_def_id(i.id);
40 let task_id = (self.dep_node_fn)(item_def_id);
41 let _task = self.tcx.dep_graph.in_task(task_id.clone());
42 debug!("Started task {:?}", task_id);
43 self.tcx.dep_graph.read(DepNode::Hir(item_def_id));
44 self.visitor.visit_item(i);
45 debug!("Ended task {:?}", task_id);
48 fn visit_impl_item(&mut self, i: &'tcx hir::ImplItem) {
49 let impl_item_def_id = self.tcx.map.local_def_id(i.id);
50 let task_id = (self.dep_node_fn)(impl_item_def_id);
51 let _task = self.tcx.dep_graph.in_task(task_id.clone());
52 debug!("Started task {:?}", task_id);
53 self.tcx.dep_graph.read(DepNode::Hir(impl_item_def_id));
54 self.visitor.visit_impl_item(i);
55 debug!("Ended task {:?}", task_id);
59 let krate = tcx.dep_graph.with_ignore(|| tcx.map.krate());
60 let mut tracking_visitor = TrackingVisitor {
62 dep_node_fn: &mut dep_node_fn,
65 krate.visit_all_item_likes(&mut tracking_visitor)