rustc: track fields in the HIR map.

This commit is contained in:
Eduard Burtescu 2016-11-09 20:57:48 +02:00 committed by Eduard-Mihai Burtescu
parent 36d33d6b71
commit fcdb4def83
3 changed files with 26 additions and 0 deletions

View File

@ -242,4 +242,11 @@ impl<'ast> Visitor<'ast> for NodeCollector<'ast> {
fn visit_macro_def(&mut self, macro_def: &'ast MacroDef) {
self.insert_entry(macro_def.id, NotPresent);
}
fn visit_struct_field(&mut self, field: &'ast StructField) {
self.insert(field.id, NodeField(field));
self.with_parent(field.id, |this| {
intravisit::walk_struct_field(this, field);
});
}
}

View File

@ -46,6 +46,7 @@ pub enum Node<'ast> {
NodeTraitItem(&'ast TraitItem),
NodeImplItem(&'ast ImplItem),
NodeVariant(&'ast Variant),
NodeField(&'ast StructField),
NodeExpr(&'ast Expr),
NodeStmt(&'ast Stmt),
NodeTy(&'ast Ty),
@ -75,6 +76,7 @@ pub enum MapEntry<'ast> {
EntryTraitItem(NodeId, &'ast TraitItem),
EntryImplItem(NodeId, &'ast ImplItem),
EntryVariant(NodeId, &'ast Variant),
EntryField(NodeId, &'ast StructField),
EntryExpr(NodeId, &'ast Expr),
EntryStmt(NodeId, &'ast Stmt),
EntryTy(NodeId, &'ast Ty),
@ -106,6 +108,7 @@ impl<'ast> MapEntry<'ast> {
NodeTraitItem(n) => EntryTraitItem(p, n),
NodeImplItem(n) => EntryImplItem(p, n),
NodeVariant(n) => EntryVariant(p, n),
NodeField(n) => EntryField(p, n),
NodeExpr(n) => EntryExpr(p, n),
NodeStmt(n) => EntryStmt(p, n),
NodeTy(n) => EntryTy(p, n),
@ -127,6 +130,7 @@ impl<'ast> MapEntry<'ast> {
EntryTraitItem(id, _) => id,
EntryImplItem(id, _) => id,
EntryVariant(id, _) => id,
EntryField(id, _) => id,
EntryExpr(id, _) => id,
EntryStmt(id, _) => id,
EntryTy(id, _) => id,
@ -152,6 +156,7 @@ impl<'ast> MapEntry<'ast> {
EntryTraitItem(_, n) => NodeTraitItem(n),
EntryImplItem(_, n) => NodeImplItem(n),
EntryVariant(_, n) => NodeVariant(n),
EntryField(_, n) => NodeField(n),
EntryExpr(_, n) => NodeExpr(n),
EntryStmt(_, n) => NodeStmt(n),
EntryTy(_, n) => NodeTy(n),
@ -265,6 +270,7 @@ impl<'ast> Map<'ast> {
EntryForeignItem(p, _) |
EntryTraitItem(p, _) |
EntryVariant(p, _) |
EntryField(p, _) |
EntryExpr(p, _) |
EntryStmt(p, _) |
EntryTy(p, _) |
@ -308,6 +314,7 @@ impl<'ast> Map<'ast> {
EntryTraitItem(p, _) |
EntryImplItem(p, _) |
EntryVariant(p, _) |
EntryField(p, _) |
EntryExpr(p, _) |
EntryStmt(p, _) |
EntryTy(p, _) |
@ -656,6 +663,7 @@ impl<'ast> Map<'ast> {
NodeImplItem(ii) => ii.name,
NodeTraitItem(ti) => ti.name,
NodeVariant(v) => v.node.name,
NodeField(f) => f.name,
NodeLifetime(lt) => lt.name,
NodeTyParam(tp) => tp.name,
NodeLocal(&Pat { node: PatKind::Binding(_,_,l,_), .. }) => l.node,
@ -674,6 +682,7 @@ impl<'ast> Map<'ast> {
Some(NodeTraitItem(ref ti)) => Some(&ti.attrs[..]),
Some(NodeImplItem(ref ii)) => Some(&ii.attrs[..]),
Some(NodeVariant(ref v)) => Some(&v.node.attrs[..]),
Some(NodeField(ref f)) => Some(&f.attrs[..]),
Some(NodeExpr(ref e)) => Some(&*e.attrs),
Some(NodeStmt(ref s)) => Some(s.node.attrs()),
// unit/tuple structs take the attributes straight from
@ -710,6 +719,7 @@ impl<'ast> Map<'ast> {
Some(NodeTraitItem(trait_method)) => trait_method.span,
Some(NodeImplItem(ref impl_item)) => impl_item.span,
Some(NodeVariant(variant)) => variant.span,
Some(NodeField(field)) => field.span,
Some(NodeExpr(expr)) => expr.span,
Some(NodeStmt(stmt)) => stmt.span,
Some(NodeTy(ty)) => ty.span,
@ -831,6 +841,7 @@ impl<'a, 'ast> Iterator for NodesMatchingSuffix<'a, 'ast> {
Some(EntryTraitItem(_, n)) => n.name(),
Some(EntryImplItem(_, n)) => n.name(),
Some(EntryVariant(_, n)) => n.name(),
Some(EntryField(_, n)) => n.name(),
_ => continue,
};
if self.matches_names(self.map.get_parent(idx), name) {
@ -849,6 +860,7 @@ impl<T:Named> Named for Spanned<T> { fn name(&self) -> Name { self.node.name() }
impl Named for Item { fn name(&self) -> Name { self.name } }
impl Named for ForeignItem { fn name(&self) -> Name { self.name } }
impl Named for Variant_ { fn name(&self) -> Name { self.name } }
impl Named for StructField { fn name(&self) -> Name { self.name } }
impl Named for TraitItem { fn name(&self) -> Name { self.name } }
impl Named for ImplItem { fn name(&self) -> Name { self.name } }
@ -940,6 +952,7 @@ impl<'a> NodePrinter for pprust::State<'a> {
NodeLifetime(a) => self.print_lifetime(&a),
NodeVisibility(a) => self.print_visibility(&a),
NodeTyParam(_) => bug!("cannot print TyParam"),
NodeField(_) => bug!("cannot print StructField"),
// these cases do not carry enough information in the
// ast_map to reconstruct their full structure for pretty
// printing.
@ -1019,6 +1032,11 @@ fn node_id_to_string(map: &Map, id: NodeId, include_id: bool) -> String {
variant.node.name,
path_str(), id_str)
}
Some(NodeField(ref field)) => {
format!("field {} in {}{}",
field.name,
path_str(), id_str)
}
Some(NodeExpr(ref expr)) => {
format!("expr {}{}", pprust::expr_to_string(&expr), id_str)
}

View File

@ -299,6 +299,7 @@ impl<'a, 'tcx> ReachableContext<'a, 'tcx> {
ast_map::NodeForeignItem(_) |
ast_map::NodeVariant(_) |
ast_map::NodeStructCtor(_) |
ast_map::NodeField(_) |
ast_map::NodeTy(_) => {}
_ => {
bug!("found unexpected thingy in worklist: {}",