Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
rustc: track hir::{TraitRef, Visibility} in the HIR map.
  • Loading branch information
eddyb committed Nov 28, 2016
1 parent 16b5c2c commit 6ebc6d8
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 11 deletions.
29 changes: 22 additions & 7 deletions src/librustc/hir/map/collector.rs
Expand Up @@ -124,13 +124,6 @@ impl<'ast> Visitor<'ast> for NodeCollector<'ast> {
this.insert(struct_def.id(), NodeStructCtor(struct_def));
}
}
ItemTrait(.., ref bounds, _) => {
for b in bounds.iter() {
if let TraitTyParamBound(ref t, TraitBoundModifier::None) = *b {
this.insert(t.trait_ref.ref_id, NodeItem(i));
}
}
}
ItemUse(ref view_path) => {
match view_path.node {
ViewPathList(_, ref paths) => {
Expand Down Expand Up @@ -217,6 +210,14 @@ impl<'ast> Visitor<'ast> for NodeCollector<'ast> {
});
}

fn visit_trait_ref(&mut self, tr: &'ast 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,
b: &'ast Expr, s: Span, id: NodeId) {
assert_eq!(self.parent_node, id);
Expand All @@ -234,6 +235,20 @@ impl<'ast> Visitor<'ast> for NodeCollector<'ast> {
self.insert(lifetime.id, NodeLifetime(lifetime));
}

fn visit_vis(&mut self, visibility: &'ast Visibility) {
match *visibility {
Visibility::Public |
Visibility::Crate |
Visibility::Inherited => {}
Visibility::Restricted { id, .. } => {
self.insert(id, NodeVisibility(visibility));
self.with_parent(id, |this| {
intravisit::walk_vis(this, visibility);
});
}
}
}

fn visit_macro_def(&mut self, macro_def: &'ast MacroDef) {
self.insert_entry(macro_def.id, NotPresent);
}
Expand Down
30 changes: 27 additions & 3 deletions src/librustc/hir/map/mod.rs
Expand Up @@ -49,6 +49,7 @@ pub enum Node<'ast> {
NodeExpr(&'ast Expr),
NodeStmt(&'ast Stmt),
NodeTy(&'ast Ty),
NodeTraitRef(&'ast TraitRef),
NodeLocal(&'ast Pat),
NodePat(&'ast Pat),
NodeBlock(&'ast Block),
Expand All @@ -57,7 +58,8 @@ pub enum Node<'ast> {
NodeStructCtor(&'ast VariantData),

NodeLifetime(&'ast Lifetime),
NodeTyParam(&'ast TyParam)
NodeTyParam(&'ast TyParam),
NodeVisibility(&'ast Visibility),
}

/// Represents an entry and its parent NodeID.
Expand All @@ -76,12 +78,14 @@ pub enum MapEntry<'ast> {
EntryExpr(NodeId, &'ast Expr),
EntryStmt(NodeId, &'ast Stmt),
EntryTy(NodeId, &'ast Ty),
EntryTraitRef(NodeId, &'ast TraitRef),
EntryLocal(NodeId, &'ast Pat),
EntryPat(NodeId, &'ast Pat),
EntryBlock(NodeId, &'ast Block),
EntryStructCtor(NodeId, &'ast VariantData),
EntryLifetime(NodeId, &'ast Lifetime),
EntryTyParam(NodeId, &'ast TyParam),
EntryVisibility(NodeId, &'ast Visibility),

/// Roots for node trees.
RootCrate,
Expand All @@ -105,12 +109,14 @@ impl<'ast> MapEntry<'ast> {
NodeExpr(n) => EntryExpr(p, n),
NodeStmt(n) => EntryStmt(p, n),
NodeTy(n) => EntryTy(p, n),
NodeTraitRef(n) => EntryTraitRef(p, n),
NodeLocal(n) => EntryLocal(p, n),
NodePat(n) => EntryPat(p, n),
NodeBlock(n) => EntryBlock(p, n),
NodeStructCtor(n) => EntryStructCtor(p, n),
NodeLifetime(n) => EntryLifetime(p, n),
NodeTyParam(n) => EntryTyParam(p, n),
NodeVisibility(n) => EntryVisibility(p, n),
}
}

Expand All @@ -124,12 +130,14 @@ impl<'ast> MapEntry<'ast> {
EntryExpr(id, _) => id,
EntryStmt(id, _) => id,
EntryTy(id, _) => id,
EntryTraitRef(id, _) => id,
EntryLocal(id, _) => id,
EntryPat(id, _) => id,
EntryBlock(id, _) => id,
EntryStructCtor(id, _) => id,
EntryLifetime(id, _) => id,
EntryTyParam(id, _) => id,
EntryVisibility(id, _) => id,

NotPresent |
RootCrate |
Expand All @@ -147,12 +155,14 @@ impl<'ast> MapEntry<'ast> {
EntryExpr(_, n) => NodeExpr(n),
EntryStmt(_, n) => NodeStmt(n),
EntryTy(_, n) => NodeTy(n),
EntryTraitRef(_, n) => NodeTraitRef(n),
EntryLocal(_, n) => NodeLocal(n),
EntryPat(_, n) => NodePat(n),
EntryBlock(_, n) => NodeBlock(n),
EntryStructCtor(_, n) => NodeStructCtor(n),
EntryLifetime(_, n) => NodeLifetime(n),
EntryTyParam(_, n) => NodeTyParam(n),
EntryVisibility(_, n) => NodeVisibility(n),
_ => return None
})
}
Expand Down Expand Up @@ -266,12 +276,14 @@ impl<'ast> Map<'ast> {
EntryExpr(p, _) |
EntryStmt(p, _) |
EntryTy(p, _) |
EntryTraitRef(p, _) |
EntryLocal(p, _) |
EntryPat(p, _) |
EntryBlock(p, _) |
EntryStructCtor(p, _) |
EntryLifetime(p, _) |
EntryTyParam(p, _) =>
EntryTyParam(p, _) |
EntryVisibility(p, _) =>
id = p,

RootCrate =>
Expand Down Expand Up @@ -307,12 +319,14 @@ impl<'ast> Map<'ast> {
EntryExpr(p, _) |
EntryStmt(p, _) |
EntryTy(p, _) |
EntryTraitRef(p, _) |
EntryLocal(p, _) |
EntryPat(p, _) |
EntryBlock(p, _) |
EntryStructCtor(p, _) |
EntryLifetime(p, _) |
EntryTyParam(p, _) =>
EntryTyParam(p, _) |
EntryVisibility(p, _) =>
id = p,

RootInlinedParent(parent) => match *parent {
Expand Down Expand Up @@ -707,11 +721,13 @@ impl<'ast> Map<'ast> {
Some(NodeExpr(expr)) => expr.span,
Some(NodeStmt(stmt)) => stmt.span,
Some(NodeTy(ty)) => ty.span,
Some(NodeTraitRef(tr)) => tr.path.span,
Some(NodeLocal(pat)) => pat.span,
Some(NodePat(pat)) => pat.span,
Some(NodeBlock(block)) => block.span,
Some(NodeStructCtor(_)) => self.expect_item(self.get_parent(id)).span,
Some(NodeTyParam(ty_param)) => ty_param.span,
Some(NodeVisibility(&Visibility::Restricted { ref path, .. })) => path.span,
_ => return None,
};
Some(sp)
Expand Down Expand Up @@ -926,9 +942,11 @@ impl<'a> NodePrinter for pprust::State<'a> {
NodeExpr(a) => self.print_expr(&a),
NodeStmt(a) => self.print_stmt(&a),
NodeTy(a) => self.print_type(&a),
NodeTraitRef(a) => self.print_trait_ref(&a),
NodePat(a) => self.print_pat(&a),
NodeBlock(a) => self.print_block(&a),
NodeLifetime(a) => self.print_lifetime(&a),
NodeVisibility(a) => self.print_visibility(&a),
NodeTyParam(_) => bug!("cannot print TyParam"),
// these cases do not carry enough information in the
// ast_map to reconstruct their full structure for pretty
Expand Down Expand Up @@ -1018,6 +1036,9 @@ fn node_id_to_string(map: &Map, id: NodeId, include_id: bool) -> String {
Some(NodeTy(ref ty)) => {
format!("type {}{}", pprust::ty_to_string(&ty), id_str)
}
Some(NodeTraitRef(ref tr)) => {
format!("trait_ref {}{}", pprust::path_to_string(&tr.path), id_str)
}
Some(NodeLocal(ref pat)) => {
format!("local {}{}", pprust::pat_to_string(&pat), id_str)
}
Expand All @@ -1037,6 +1058,9 @@ fn node_id_to_string(map: &Map, id: NodeId, include_id: bool) -> String {
Some(NodeTyParam(ref ty_param)) => {
format!("typaram {:?}{}", ty_param, id_str)
}
Some(NodeVisibility(ref vis)) => {
format!("visibility {:?}{}", vis, id_str)
}
None => {
format!("unknown node{}", id_str)
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/hir/print.rs
Expand Up @@ -845,7 +845,7 @@ impl<'a> State<'a> {
self.ann.post(self, NodeItem(item))
}

fn print_trait_ref(&mut self, t: &hir::TraitRef) -> io::Result<()> {
pub fn print_trait_ref(&mut self, t: &hir::TraitRef) -> io::Result<()> {
self.print_path(&t.path, false)
}

Expand Down

0 comments on commit 6ebc6d8

Please sign in to comment.