Skip to content

Commit

Permalink
visit: unify extern & assoc item visiting
Browse files Browse the repository at this point in the history
  • Loading branch information
Centril committed Feb 15, 2020
1 parent cf87edf commit 5abedd8
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 58 deletions.
39 changes: 14 additions & 25 deletions src/libsyntax/mut_visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -949,6 +949,19 @@ pub fn noop_flat_map_assoc_item<T: MutVisitor>(
) -> SmallVec<[P<AssocItem>; 1]> {
let AssocItem { id, ident, vis, defaultness: _, attrs, kind, span, tokens: _ } =
item.deref_mut();
walk_nested_item(visitor, id, span, ident, vis, attrs, kind);
smallvec![item]
}

pub fn walk_nested_item(
visitor: &mut impl MutVisitor,
id: &mut NodeId,
span: &mut Span,
ident: &mut Ident,
vis: &mut Visibility,
attrs: &mut Vec<Attribute>,
kind: &mut AssocItemKind,
) {
visitor.visit_id(id);
visitor.visit_ident(ident);
visitor.visit_vis(vis);
Expand All @@ -971,8 +984,6 @@ pub fn noop_flat_map_assoc_item<T: MutVisitor>(
AssocItemKind::Macro(mac) => visitor.visit_mac(mac),
}
visitor.visit_span(span);

smallvec![item]
}

pub fn noop_visit_fn_header<T: MutVisitor>(header: &mut FnHeader, vis: &mut T) {
Expand Down Expand Up @@ -1038,29 +1049,7 @@ pub fn noop_flat_map_foreign_item<T: MutVisitor>(
visitor: &mut T,
) -> SmallVec<[P<ForeignItem>; 1]> {
let ForeignItem { ident, attrs, id, kind, vis, span, tokens: _ } = item.deref_mut();
visitor.visit_ident(ident);
visit_attrs(attrs, visitor);
match kind {
ForeignItemKind::Fn(sig, generics, body) => {
visit_fn_sig(sig, visitor);
visitor.visit_generics(generics);
visit_opt(body, |body| visitor.visit_block(body));
}
ForeignItemKind::Const(ty, body) | ForeignItemKind::Static(ty, _, body) => {
visitor.visit_ty(ty);
visit_opt(body, |body| visitor.visit_expr(body));
}
ForeignItemKind::TyAlias(generics, bounds, ty) => {
visitor.visit_generics(generics);
visit_bounds(bounds, visitor);
visit_opt(ty, |ty| visitor.visit_ty(ty));
}
ForeignItemKind::Macro(mac) => visitor.visit_mac(mac),
}
visitor.visit_id(id);
visitor.visit_span(span);
visitor.visit_vis(vis);

walk_nested_item(visitor, id, span, ident, vis, attrs, kind);
smallvec![item]
}

Expand Down
59 changes: 26 additions & 33 deletions src/libsyntax/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -525,29 +525,8 @@ pub fn walk_pat<'a, V: Visitor<'a>>(visitor: &mut V, pattern: &'a Pat) {
}

pub fn walk_foreign_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a ForeignItem) {
visitor.visit_vis(&item.vis);
visitor.visit_ident(item.ident);

match item.kind {
ForeignItemKind::Fn(ref sig, ref generics, ref body) => {
visitor.visit_generics(generics);
let kind = FnKind::Fn(FnCtxt::Foreign, item.ident, sig, &item.vis, body.as_deref());
visitor.visit_fn(kind, item.span, item.id);
}
ForeignItemKind::Const(ref typ, ref body)
| ForeignItemKind::Static(ref typ, _, ref body) => {
visitor.visit_ty(typ);
walk_list!(visitor, visit_expr, body);
}
ForeignItemKind::TyAlias(ref generics, ref bounds, ref ty) => {
visitor.visit_generics(generics);
walk_list!(visitor, visit_param_bound, bounds);
walk_list!(visitor, visit_ty, ty);
}
ForeignItemKind::Macro(ref mac) => visitor.visit_mac(mac),
}

walk_list!(visitor, visit_attribute, &item.attrs);
let ForeignItem { id, span, ident, vis, attrs, kind, tokens: _ } = item;
walk_nested_item(visitor, *id, *span, *ident, vis, attrs, kind, FnCtxt::Foreign);
}

pub fn walk_global_asm<'a, V: Visitor<'a>>(_: &mut V, _: &'a GlobalAsm) {
Expand Down Expand Up @@ -630,25 +609,39 @@ pub fn walk_fn<'a, V: Visitor<'a>>(visitor: &mut V, kind: FnKind<'a>, _span: Spa
}

pub fn walk_assoc_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a AssocItem, ctxt: AssocCtxt) {
visitor.visit_vis(&item.vis);
visitor.visit_ident(item.ident);
walk_list!(visitor, visit_attribute, &item.attrs);
match item.kind {
AssocItemKind::Const(ref ty, ref expr) | AssocItemKind::Static(ref ty, _, ref expr) => {
let AssocItem { id, span, ident, vis, attrs, kind, tokens: _, defaultness: _ } = item;
walk_nested_item(visitor, *id, *span, *ident, vis, attrs, kind, FnCtxt::Assoc(ctxt));
}

fn walk_nested_item<'a, V: Visitor<'a>>(
visitor: &mut V,
id: NodeId,
span: Span,
ident: Ident,
vis: &'a Visibility,
attrs: &'a [Attribute],
kind: &'a AssocItemKind,
ctxt: FnCtxt,
) {
visitor.visit_vis(vis);
visitor.visit_ident(ident);
walk_list!(visitor, visit_attribute, attrs);
match kind {
AssocItemKind::Const(ty, expr) | AssocItemKind::Static(ty, _, expr) => {
visitor.visit_ty(ty);
walk_list!(visitor, visit_expr, expr);
}
AssocItemKind::Fn(ref sig, ref generics, ref body) => {
AssocItemKind::Fn(sig, generics, body) => {
visitor.visit_generics(generics);
let kind = FnKind::Fn(FnCtxt::Assoc(ctxt), item.ident, sig, &item.vis, body.as_deref());
visitor.visit_fn(kind, item.span, item.id);
let kind = FnKind::Fn(ctxt, ident, sig, vis, body.as_deref());
visitor.visit_fn(kind, span, id);
}
AssocItemKind::TyAlias(ref generics, ref bounds, ref ty) => {
AssocItemKind::TyAlias(generics, bounds, ty) => {
visitor.visit_generics(generics);
walk_list!(visitor, visit_param_bound, bounds);
walk_list!(visitor, visit_ty, ty);
}
AssocItemKind::Macro(ref mac) => {
AssocItemKind::Macro(mac) => {
visitor.visit_mac(mac);
}
}
Expand Down

0 comments on commit 5abedd8

Please sign in to comment.