1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use hir;
use hir::def_id::DefId;
use hir::itemlikevisit::ItemLikeVisitor;
use ty::TyCtxt;

use super::dep_node::DepNode;

/// Visit all the items in the krate in some order. When visiting a
/// particular item, first create a dep-node by calling `dep_node_fn`
/// and push that onto the dep-graph stack of tasks, and also create a
/// read edge from the corresponding AST node. This is used in
/// compiler passes to automatically record the item that they are
/// working on.
pub fn visit_all_item_likes_in_krate<'a, 'tcx, V, F>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
                                                     mut dep_node_fn: F,
                                                     visitor: &mut V)
    where F: FnMut(DefId) -> DepNode<DefId>, V: ItemLikeVisitor<'tcx>
{
    struct TrackingVisitor<'visit, 'tcx: 'visit, F: 'visit, V: 'visit> {
        tcx: TyCtxt<'visit, 'tcx, 'tcx>,
        dep_node_fn: &'visit mut F,
        visitor: &'visit mut V
    }

    impl<'visit, 'tcx, F, V> ItemLikeVisitor<'tcx> for TrackingVisitor<'visit, 'tcx, F, V>
        where F: FnMut(DefId) -> DepNode<DefId>, V: ItemLikeVisitor<'tcx>
    {
        fn visit_item(&mut self, i: &'tcx hir::Item) {
            let item_def_id = self.tcx.hir.local_def_id(i.id);
            let task_id = (self.dep_node_fn)(item_def_id);
            let _task = self.tcx.dep_graph.in_task(task_id.clone());
            debug!("Started task {:?}", task_id);
            self.tcx.dep_graph.read(DepNode::Hir(item_def_id));
            self.visitor.visit_item(i);
            debug!("Ended task {:?}", task_id);
        }

        fn visit_trait_item(&mut self, i: &'tcx hir::TraitItem) {
            let trait_item_def_id = self.tcx.hir.local_def_id(i.id);
            let task_id = (self.dep_node_fn)(trait_item_def_id);
            let _task = self.tcx.dep_graph.in_task(task_id.clone());
            debug!("Started task {:?}", task_id);
            self.tcx.dep_graph.read(DepNode::Hir(trait_item_def_id));
            self.visitor.visit_trait_item(i);
            debug!("Ended task {:?}", task_id);
        }

        fn visit_impl_item(&mut self, i: &'tcx hir::ImplItem) {
            let impl_item_def_id = self.tcx.hir.local_def_id(i.id);
            let task_id = (self.dep_node_fn)(impl_item_def_id);
            let _task = self.tcx.dep_graph.in_task(task_id.clone());
            debug!("Started task {:?}", task_id);
            self.tcx.dep_graph.read(DepNode::Hir(impl_item_def_id));
            self.visitor.visit_impl_item(i);
            debug!("Ended task {:?}", task_id);
        }
    }

    let krate = tcx.dep_graph.with_ignore(|| tcx.hir.krate());
    let mut tracking_visitor = TrackingVisitor {
        tcx: tcx,
        dep_node_fn: &mut dep_node_fn,
        visitor: visitor
    };
    krate.visit_all_item_likes(&mut tracking_visitor)
}

pub fn visit_all_bodies_in_krate<'a, 'tcx, C>(tcx: TyCtxt<'a, 'tcx, 'tcx>, callback: C)
    where C: Fn(/* body_owner */ DefId, /* body id */ hir::BodyId),
{
    let krate = tcx.hir.krate();
    for &body_id in &krate.body_ids {
        let body_owner_def_id = tcx.hir.body_owner_def_id(body_id);
        callback(body_owner_def_id, body_id);
    }
}