Skip to content

Commit

Permalink
rustc: don't call the HIR AST.
Browse files Browse the repository at this point in the history
  • Loading branch information
eddyb committed Jan 26, 2017
1 parent 45c8c56 commit 1ff3641
Show file tree
Hide file tree
Showing 22 changed files with 345 additions and 363 deletions.
14 changes: 7 additions & 7 deletions src/librustc/cfg/graphviz.rs
Expand Up @@ -17,14 +17,14 @@ use graphviz::IntoCow;

use syntax::ast;

use hir::map as ast_map;
use hir::map as hir_map;
use cfg;

pub type Node<'a> = (cfg::CFGIndex, &'a cfg::CFGNode);
pub type Edge<'a> = &'a cfg::CFGEdge;

pub struct LabelledCFG<'a, 'ast: 'a> {
pub ast_map: &'a ast_map::Map<'ast>,
pub struct LabelledCFG<'a, 'hir: 'a> {
pub hir_map: &'a hir_map::Map<'hir>,
pub cfg: &'a cfg::CFG,
pub name: String,
/// `labelled_edges` controls whether we emit labels on the edges
Expand Down Expand Up @@ -52,7 +52,7 @@ fn replace_newline_with_backslash_l(s: String) -> String {
}
}

impl<'a, 'ast> dot::Labeller<'a> for LabelledCFG<'a, 'ast> {
impl<'a, 'hir> dot::Labeller<'a> for LabelledCFG<'a, 'hir> {
type Node = Node<'a>;
type Edge = Edge<'a>;
fn graph_id(&'a self) -> dot::Id<'a> { dot::Id::new(&self.name[..]).unwrap() }
Expand All @@ -69,7 +69,7 @@ impl<'a, 'ast> dot::Labeller<'a> for LabelledCFG<'a, 'ast> {
} else if n.data.id() == ast::DUMMY_NODE_ID {
dot::LabelText::LabelStr("(dummy_node)".into_cow())
} else {
let s = self.ast_map.node_to_string(n.data.id());
let s = self.hir_map.node_to_string(n.data.id());
// left-aligns the lines
let s = replace_newline_with_backslash_l(s);
dot::LabelText::EscStr(s.into_cow())
Expand All @@ -88,7 +88,7 @@ impl<'a, 'ast> dot::Labeller<'a> for LabelledCFG<'a, 'ast> {
} else {
put_one = true;
}
let s = self.ast_map.node_to_string(node_id);
let s = self.hir_map.node_to_string(node_id);
// left-aligns the lines
let s = replace_newline_with_backslash_l(s);
label.push_str(&format!("exiting scope_{} {}",
Expand Down Expand Up @@ -120,7 +120,7 @@ impl<'a> dot::GraphWalk<'a> for &'a cfg::CFG {
}
}

impl<'a, 'ast> dot::GraphWalk<'a> for LabelledCFG<'a, 'ast>
impl<'a, 'hir> dot::GraphWalk<'a> for LabelledCFG<'a, 'hir>
{
type Node = Node<'a>;
type Edge = Edge<'a>;
Expand Down
12 changes: 6 additions & 6 deletions src/librustc/hir/intravisit.rs
Expand Up @@ -1085,13 +1085,13 @@ impl IdRange {
}


pub struct IdRangeComputingVisitor<'a, 'ast: 'a> {
pub struct IdRangeComputingVisitor<'a, 'hir: 'a> {
result: IdRange,
map: &'a map::Map<'ast>,
map: &'a map::Map<'hir>,
}

impl<'a, 'ast> IdRangeComputingVisitor<'a, 'ast> {
pub fn new(map: &'a map::Map<'ast>) -> IdRangeComputingVisitor<'a, 'ast> {
impl<'a, 'hir> IdRangeComputingVisitor<'a, 'hir> {
pub fn new(map: &'a map::Map<'hir>) -> IdRangeComputingVisitor<'a, 'hir> {
IdRangeComputingVisitor { result: IdRange::max(), map: map }
}

Expand All @@ -1100,8 +1100,8 @@ impl<'a, 'ast> IdRangeComputingVisitor<'a, 'ast> {
}
}

impl<'a, 'ast> Visitor<'ast> for IdRangeComputingVisitor<'a, 'ast> {
fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'ast> {
impl<'a, 'hir> Visitor<'hir> for IdRangeComputingVisitor<'a, 'hir> {
fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'hir> {
NestedVisitorMap::OnlyBodies(&self.map)
}

Expand Down
54 changes: 27 additions & 27 deletions src/librustc/hir/map/collector.rs
Expand Up @@ -16,17 +16,17 @@ use syntax::ast::{NodeId, CRATE_NODE_ID};
use syntax_pos::Span;

/// A Visitor that walks over the HIR and collects Nodes into a HIR map
pub struct NodeCollector<'ast> {
pub struct NodeCollector<'hir> {
/// The crate
pub krate: &'ast Crate,
pub krate: &'hir Crate,
/// The node map
pub(super) map: Vec<MapEntry<'ast>>,
pub(super) map: Vec<MapEntry<'hir>>,
/// The parent of this node
pub parent_node: NodeId,
}

impl<'ast> NodeCollector<'ast> {
pub fn root(krate: &'ast Crate) -> NodeCollector<'ast> {
impl<'hir> NodeCollector<'hir> {
pub fn root(krate: &'hir Crate) -> NodeCollector<'hir> {
let mut collector = NodeCollector {
krate: krate,
map: vec![],
Expand All @@ -37,16 +37,16 @@ impl<'ast> NodeCollector<'ast> {
collector
}

fn insert_entry(&mut self, id: NodeId, entry: MapEntry<'ast>) {
debug!("ast_map: {:?} => {:?}", id, entry);
fn insert_entry(&mut self, id: NodeId, entry: MapEntry<'hir>) {
debug!("hir_map: {:?} => {:?}", id, entry);
let len = self.map.len();
if id.as_usize() >= len {
self.map.extend(repeat(NotPresent).take(id.as_usize() - len + 1));
}
self.map[id.as_usize()] = entry;
}

fn insert(&mut self, id: NodeId, node: Node<'ast>) {
fn insert(&mut self, id: NodeId, node: Node<'hir>) {
let entry = MapEntry::from_node(self.parent_node, node);
self.insert_entry(id, entry);
}
Expand All @@ -59,12 +59,12 @@ impl<'ast> NodeCollector<'ast> {
}
}

impl<'ast> Visitor<'ast> for NodeCollector<'ast> {
impl<'hir> Visitor<'hir> for NodeCollector<'hir> {
/// Because we want to track parent items and so forth, enable
/// deep walking so that we walk nested items in the context of
/// their outer items.

fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'ast> {
fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'hir> {
panic!("visit_nested_xxx must be manually implemented in this visitor")
}

Expand All @@ -85,7 +85,7 @@ impl<'ast> Visitor<'ast> for NodeCollector<'ast> {
self.visit_body(self.krate.body(id));
}

fn visit_item(&mut self, i: &'ast Item) {
fn visit_item(&mut self, i: &'hir Item) {
debug!("visit_item: {:?}", i);

self.insert(i.id, NodeItem(i));
Expand All @@ -104,39 +104,39 @@ impl<'ast> Visitor<'ast> for NodeCollector<'ast> {
});
}

fn visit_foreign_item(&mut self, foreign_item: &'ast ForeignItem) {
fn visit_foreign_item(&mut self, foreign_item: &'hir ForeignItem) {
self.insert(foreign_item.id, NodeForeignItem(foreign_item));

self.with_parent(foreign_item.id, |this| {
intravisit::walk_foreign_item(this, foreign_item);
});
}

fn visit_generics(&mut self, generics: &'ast Generics) {
fn visit_generics(&mut self, generics: &'hir Generics) {
for ty_param in generics.ty_params.iter() {
self.insert(ty_param.id, NodeTyParam(ty_param));
}

intravisit::walk_generics(self, generics);
}

fn visit_trait_item(&mut self, ti: &'ast TraitItem) {
fn visit_trait_item(&mut self, ti: &'hir TraitItem) {
self.insert(ti.id, NodeTraitItem(ti));

self.with_parent(ti.id, |this| {
intravisit::walk_trait_item(this, ti);
});
}

fn visit_impl_item(&mut self, ii: &'ast ImplItem) {
fn visit_impl_item(&mut self, ii: &'hir ImplItem) {
self.insert(ii.id, NodeImplItem(ii));

self.with_parent(ii.id, |this| {
intravisit::walk_impl_item(this, ii);
});
}

fn visit_pat(&mut self, pat: &'ast Pat) {
fn visit_pat(&mut self, pat: &'hir Pat) {
let node = if let PatKind::Binding(..) = pat.node {
NodeLocal(pat)
} else {
Expand All @@ -149,15 +149,15 @@ impl<'ast> Visitor<'ast> for NodeCollector<'ast> {
});
}

fn visit_expr(&mut self, expr: &'ast Expr) {
fn visit_expr(&mut self, expr: &'hir Expr) {
self.insert(expr.id, NodeExpr(expr));

self.with_parent(expr.id, |this| {
intravisit::walk_expr(this, expr);
});
}

fn visit_stmt(&mut self, stmt: &'ast Stmt) {
fn visit_stmt(&mut self, stmt: &'hir Stmt) {
let id = stmt.node.id();
self.insert(id, NodeStmt(stmt));

Expand All @@ -166,40 +166,40 @@ impl<'ast> Visitor<'ast> for NodeCollector<'ast> {
});
}

fn visit_ty(&mut self, ty: &'ast Ty) {
fn visit_ty(&mut self, ty: &'hir Ty) {
self.insert(ty.id, NodeTy(ty));

self.with_parent(ty.id, |this| {
intravisit::walk_ty(this, ty);
});
}

fn visit_trait_ref(&mut self, tr: &'ast TraitRef) {
fn visit_trait_ref(&mut self, tr: &'hir TraitRef) {
self.insert(tr.ref_id, NodeTraitRef(tr));

self.with_parent(tr.ref_id, |this| {
intravisit::walk_trait_ref(this, tr);
});
}

fn visit_fn(&mut self, fk: intravisit::FnKind<'ast>, fd: &'ast FnDecl,
fn visit_fn(&mut self, fk: intravisit::FnKind<'hir>, fd: &'hir FnDecl,
b: BodyId, s: Span, id: NodeId) {
assert_eq!(self.parent_node, id);
intravisit::walk_fn(self, fk, fd, b, s, id);
}

fn visit_block(&mut self, block: &'ast Block) {
fn visit_block(&mut self, block: &'hir Block) {
self.insert(block.id, NodeBlock(block));
self.with_parent(block.id, |this| {
intravisit::walk_block(this, block);
});
}

fn visit_lifetime(&mut self, lifetime: &'ast Lifetime) {
fn visit_lifetime(&mut self, lifetime: &'hir Lifetime) {
self.insert(lifetime.id, NodeLifetime(lifetime));
}

fn visit_vis(&mut self, visibility: &'ast Visibility) {
fn visit_vis(&mut self, visibility: &'hir Visibility) {
match *visibility {
Visibility::Public |
Visibility::Crate |
Expand All @@ -213,19 +213,19 @@ impl<'ast> Visitor<'ast> for NodeCollector<'ast> {
}
}

fn visit_macro_def(&mut self, macro_def: &'ast MacroDef) {
fn visit_macro_def(&mut self, macro_def: &'hir MacroDef) {
self.insert_entry(macro_def.id, NotPresent);
}

fn visit_variant(&mut self, v: &'ast Variant, g: &'ast Generics, item_id: NodeId) {
fn visit_variant(&mut self, v: &'hir Variant, g: &'hir Generics, item_id: NodeId) {
let id = v.node.data.id();
self.insert(id, NodeVariant(v));
self.with_parent(id, |this| {
intravisit::walk_variant(this, v, g, item_id);
});
}

fn visit_struct_field(&mut self, field: &'ast StructField) {
fn visit_struct_field(&mut self, field: &'hir StructField) {
self.insert(field.id, NodeField(field));
self.with_parent(field.id, |this| {
intravisit::walk_struct_field(this, field);
Expand Down

0 comments on commit 1ff3641

Please sign in to comment.