Skip to content

Commit

Permalink
Refactored syntax::fold.
Browse files Browse the repository at this point in the history
Prior to this, the code there had a few issues:

- Default implementations inconsistently either had the prefix `noop_` or
  not.
- Some default methods where implemented in terms of a public noop function
  for user code to call, others where implemented directly on the trait
  and did not allow users of the trait to reuse the code.
- Some of the default implementations where private, and thus not reusable
  for other implementors.
- There where some bugs where default implementations called other default
  implementations directly, rather than to the underlying Folder, with the
  result of some AST nodes never being visited even if the user implemented that
  method. (For example, the current Folder never folded struct fields)

This commit solves this situation somewhat radically by making _all_
`fold_...` functions in the module into Folder methods, and implementing
them all in terms of public `noop_...` functions for other implementors to
call out to.

Some public functions had to be renamed to fit the new system, so this is a
breaking change.

[breaking-change]
  • Loading branch information
Kimundi committed Jul 29, 2014
1 parent 7375f4d commit 26a39f2
Show file tree
Hide file tree
Showing 5 changed files with 420 additions and 295 deletions.
2 changes: 1 addition & 1 deletion src/librustc/front/config.rs
Expand Up @@ -44,7 +44,7 @@ impl<'a> fold::Folder for Context<'a> {
fold_expr(self, expr)
}
fn fold_mac(&mut self, mac: &ast::Mac) -> ast::Mac {
fold::fold_mac(mac, self)
fold::noop_fold_mac(mac, self)
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/libsyntax/ast.rs
Expand Up @@ -961,15 +961,15 @@ pub enum ExplicitSelf_ {

pub type ExplicitSelf = Spanned<ExplicitSelf_>;

#[deriving(PartialEq, Eq, Encodable, Decodable, Hash, Show)]
#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)]
pub struct Method {
pub attrs: Vec<Attribute>,
pub id: NodeId,
pub span: Span,
pub node: Method_,
}

#[deriving(PartialEq, Eq, Encodable, Decodable, Hash, Show)]
#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)]
pub enum Method_ {
/// Represents a method declaration
MethDecl(Ident,
Expand Down
2 changes: 1 addition & 1 deletion src/libsyntax/ast_map/mod.rs
Expand Up @@ -606,7 +606,7 @@ impl<'a, F: FoldOps> Folder for Ctx<'a, F> {
}

fn fold_mac(&mut self, mac: &Mac) -> Mac {
fold::fold_mac(mac, self)
fold::noop_fold_mac(mac, self)
}
}

Expand Down
12 changes: 6 additions & 6 deletions src/libsyntax/ext/expand.rs
Expand Up @@ -341,7 +341,7 @@ fn expand_item_underscore(item: &ast::Item_, fld: &mut MacroExpander) -> ast::It
ast::ItemFn(decl, fn_style, abi, ref generics, body) => {
let (rewritten_fn_decl, rewritten_body)
= expand_and_rename_fn_decl_and_block(&*decl, body, fld);
let expanded_generics = fold::fold_generics(generics,fld);
let expanded_generics = fold::noop_fold_generics(generics,fld);
ast::ItemFn(rewritten_fn_decl, fn_style, abi, expanded_generics, rewritten_body)
}
_ => noop_fold_item_underscore(&*item, fld)
Expand Down Expand Up @@ -792,7 +792,7 @@ impl<'a> Folder for IdentRenamer<'a> {
}
}
fn fold_mac(&mut self, macro: &ast::Mac) -> ast::Mac {
fold::fold_mac(macro, self)
fold::noop_fold_mac(macro, self)
}
}

Expand Down Expand Up @@ -824,7 +824,7 @@ impl<'a> Folder for PatIdentRenamer<'a> {
}
}
fn fold_mac(&mut self, macro: &ast::Mac) -> ast::Mac {
fold::fold_mac(macro, self)
fold::noop_fold_mac(macro, self)
}
}

Expand All @@ -847,7 +847,7 @@ fn expand_method(m: &ast::Method, fld: &mut MacroExpander) -> SmallVector<Gc<ast
id: id,
span: fld.new_span(m.span),
node: ast::MethDecl(fld.fold_ident(ident),
fold_generics(generics, fld),
noop_fold_generics(generics, fld),
abi,
fld.fold_explicit_self(explicit_self),
fn_style,
Expand Down Expand Up @@ -1014,7 +1014,7 @@ impl Folder for Marker {
let macro = match m.node {
MacInvocTT(ref path, ref tts, ctxt) => {
MacInvocTT(self.fold_path(path),
fold_tts(tts.as_slice(), self),
self.fold_tts(tts.as_slice()),
mtwt::apply_mark(self.mark, ctxt))
}
};
Expand All @@ -1027,7 +1027,7 @@ impl Folder for Marker {

// apply a given mark to the given token trees. Used prior to expansion of a macro.
fn mark_tts(tts: &[TokenTree], m: Mrk) -> Vec<TokenTree> {
fold_tts(tts, &mut Marker{mark:m})
noop_fold_tts(tts, &mut Marker{mark:m})
}

// apply a given mark to the given expr. Used following the expansion of a macro.
Expand Down

0 comments on commit 26a39f2

Please sign in to comment.