]> git.proxmox.com Git - rustc.git/blob - src/librustc/dep_graph/visit.rs
New upstream version 1.17.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::itemlikevisit::ItemLikeVisitor;
14 use ty::TyCtxt;
15
16 use super::dep_node::DepNode;
17
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
23 /// working on.
24 pub fn visit_all_item_likes_in_krate<'a, 'tcx, V, F>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
25 mut dep_node_fn: F,
26 visitor: &mut V)
27 where F: FnMut(DefId) -> DepNode<DefId>, V: ItemLikeVisitor<'tcx>
28 {
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
33 }
34
35 impl<'visit, 'tcx, F, V> ItemLikeVisitor<'tcx> for TrackingVisitor<'visit, 'tcx, F, V>
36 where F: FnMut(DefId) -> DepNode<DefId>, V: ItemLikeVisitor<'tcx>
37 {
38 fn visit_item(&mut self, i: &'tcx hir::Item) {
39 let item_def_id = self.tcx.hir.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);
46 }
47
48 fn visit_trait_item(&mut self, i: &'tcx hir::TraitItem) {
49 let trait_item_def_id = self.tcx.hir.local_def_id(i.id);
50 let task_id = (self.dep_node_fn)(trait_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(trait_item_def_id));
54 self.visitor.visit_trait_item(i);
55 debug!("Ended task {:?}", task_id);
56 }
57
58 fn visit_impl_item(&mut self, i: &'tcx hir::ImplItem) {
59 let impl_item_def_id = self.tcx.hir.local_def_id(i.id);
60 let task_id = (self.dep_node_fn)(impl_item_def_id);
61 let _task = self.tcx.dep_graph.in_task(task_id.clone());
62 debug!("Started task {:?}", task_id);
63 self.tcx.dep_graph.read(DepNode::Hir(impl_item_def_id));
64 self.visitor.visit_impl_item(i);
65 debug!("Ended task {:?}", task_id);
66 }
67 }
68
69 let krate = tcx.dep_graph.with_ignore(|| tcx.hir.krate());
70 let mut tracking_visitor = TrackingVisitor {
71 tcx: tcx,
72 dep_node_fn: &mut dep_node_fn,
73 visitor: visitor
74 };
75 krate.visit_all_item_likes(&mut tracking_visitor)
76 }
77
78 pub fn visit_all_bodies_in_krate<'a, 'tcx, C>(tcx: TyCtxt<'a, 'tcx, 'tcx>, callback: C)
79 where C: Fn(/* body_owner */ DefId, /* body id */ hir::BodyId),
80 {
81 let krate = tcx.hir.krate();
82 for &body_id in &krate.body_ids {
83 let body_owner_def_id = tcx.hir.body_owner_def_id(body_id);
84 callback(body_owner_def_id, body_id);
85 }
86 }