]>
Commit | Line | Data |
---|---|---|
54a0048b SL |
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; | |
476ff2be | 13 | use hir::itemlikevisit::ItemLikeVisitor; |
54a0048b SL |
14 | use ty::TyCtxt; |
15 | ||
16 | use super::dep_node::DepNode; | |
17 | ||
54a0048b SL |
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. | |
476ff2be SL |
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> | |
54a0048b SL |
28 | { |
29 | struct TrackingVisitor<'visit, 'tcx: 'visit, F: 'visit, V: 'visit> { | |
a7813a04 | 30 | tcx: TyCtxt<'visit, 'tcx, 'tcx>, |
54a0048b SL |
31 | dep_node_fn: &'visit mut F, |
32 | visitor: &'visit mut V | |
33 | } | |
34 | ||
476ff2be SL |
35 | impl<'visit, 'tcx, F, V> ItemLikeVisitor<'tcx> for TrackingVisitor<'visit, 'tcx, F, V> |
36 | where F: FnMut(DefId) -> DepNode<DefId>, V: ItemLikeVisitor<'tcx> | |
54a0048b SL |
37 | { |
38 | fn visit_item(&mut self, i: &'tcx hir::Item) { | |
32a655c1 | 39 | let item_def_id = self.tcx.hir.local_def_id(i.id); |
54a0048b | 40 | let task_id = (self.dep_node_fn)(item_def_id); |
3157f602 | 41 | let _task = self.tcx.dep_graph.in_task(task_id.clone()); |
54a0048b SL |
42 | debug!("Started task {:?}", task_id); |
43 | self.tcx.dep_graph.read(DepNode::Hir(item_def_id)); | |
a7813a04 XL |
44 | self.visitor.visit_item(i); |
45 | debug!("Ended task {:?}", task_id); | |
54a0048b | 46 | } |
476ff2be | 47 | |
32a655c1 SL |
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 | ||
476ff2be | 58 | fn visit_impl_item(&mut self, i: &'tcx hir::ImplItem) { |
32a655c1 | 59 | let impl_item_def_id = self.tcx.hir.local_def_id(i.id); |
476ff2be SL |
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); | |
476ff2be SL |
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 | } | |
54a0048b SL |
67 | } |
68 | ||
32a655c1 | 69 | let krate = tcx.dep_graph.with_ignore(|| tcx.hir.krate()); |
54a0048b SL |
70 | let mut tracking_visitor = TrackingVisitor { |
71 | tcx: tcx, | |
72 | dep_node_fn: &mut dep_node_fn, | |
73 | visitor: visitor | |
74 | }; | |
476ff2be | 75 | krate.visit_all_item_likes(&mut tracking_visitor) |
54a0048b | 76 | } |