Skip to content

Commit

Permalink
store the visit order in the Crate
Browse files Browse the repository at this point in the history
  • Loading branch information
nikomatsakis committed Feb 21, 2017
1 parent f69ce16 commit fed58f3
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 25 deletions.
28 changes: 3 additions & 25 deletions src/librustc/dep_graph/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use hir;
use hir::def_id::DefId;
use hir::itemlikevisit::ItemLikeVisitor;
use hir::intravisit::{self, NestedVisitorMap, Visitor};
use ty::TyCtxt;

use super::dep_node::DepNode;
Expand Down Expand Up @@ -79,30 +78,9 @@ pub fn visit_all_item_likes_in_krate<'a, 'tcx, V, F>(tcx: TyCtxt<'a, 'tcx, 'tcx>
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),
{
// NB: we use a visitor here rather than walking the keys of the
// hashmap so as to ensure we visit the bodies "in order".

let krate = tcx.hir.krate();
intravisit::walk_crate(&mut V { tcx, callback }, krate);

struct V<'a, 'tcx: 'a, C> {
tcx: TyCtxt<'a, 'tcx, 'tcx>,
callback: C
}

impl<'a, 'tcx, C> Visitor<'tcx> for V<'a, 'tcx, C>
where C: Fn(/* body_owner */ DefId, /* body id */ hir::BodyId),
{
fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
NestedVisitorMap::All(&self.tcx.hir)
}

fn visit_body(&mut self, body: &'tcx hir::Body) {
let body_id = body.id();
let body_owner_def_id = self.tcx.hir.body_owner_def_id(body_id);
(self.callback)(body_owner_def_id, body_id);

intravisit::walk_body(self, body);
}
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);
}
}
10 changes: 10 additions & 0 deletions src/librustc/hir/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ impl<'a> LoweringContext<'a> {
let module = self.lower_mod(&c.module);
let attrs = self.lower_attrs(&c.attrs);
let exported_macros = c.exported_macros.iter().map(|m| self.lower_macro_def(m)).collect();
let body_ids = body_ids(&self.bodies);

hir::Crate {
module: module,
Expand All @@ -195,6 +196,7 @@ impl<'a> LoweringContext<'a> {
trait_items: self.trait_items,
impl_items: self.impl_items,
bodies: self.bodies,
body_ids: body_ids,
}
}

Expand Down Expand Up @@ -2408,3 +2410,11 @@ impl<'a> LoweringContext<'a> {
}
}
}

fn body_ids(bodies: &BTreeMap<hir::BodyId, hir::Body>) -> Vec<hir::BodyId> {
// Sorting by span ensures that we get things in order within a
// file, and also puts the files in a sensible order.
let mut body_ids: Vec<_> = bodies.keys().cloned().collect();
body_ids.sort_by_key(|b| bodies[b].value.span);
body_ids
}
6 changes: 6 additions & 0 deletions src/librustc/hir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,12 @@ pub struct Crate {
pub trait_items: BTreeMap<TraitItemId, TraitItem>,
pub impl_items: BTreeMap<ImplItemId, ImplItem>,
pub bodies: BTreeMap<BodyId, Body>,

/// A list of the body ids written out in the order in which they
/// appear in the crate. If you're going to process all the bodies
/// in the crate, you should iterate over this list rather than the keys
/// of bodies.
pub body_ids: Vec<BodyId>,
}

impl Crate {
Expand Down
1 change: 1 addition & 0 deletions src/librustc_incremental/calculate_svh/svh_visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1166,6 +1166,7 @@ impl<'a, 'hash, 'tcx> StrictVersionHashVisitor<'a, 'hash, 'tcx> {
trait_items: _,
impl_items: _,
bodies: _,
body_ids: _,
} = *krate;

visit::Visitor::visit_mod(self, module, span, ast::CRATE_NODE_ID);
Expand Down

0 comments on commit fed58f3

Please sign in to comment.