2012-12-03 18:48:01 -06:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2012-09-04 13:37:29 -05:00
|
|
|
use ast::*;
|
2012-12-23 16:41:37 -06:00
|
|
|
use ast;
|
2013-10-28 21:22:42 -05:00
|
|
|
use codemap::{respan, Span, Spanned};
|
2013-06-06 16:17:00 -05:00
|
|
|
use parse::token;
|
2013-02-25 13:11:21 -06:00
|
|
|
use opt_vec::OptVec;
|
2013-11-25 01:08:53 -06:00
|
|
|
use util::small_vector::SmallVector;
|
2012-12-23 16:41:37 -06:00
|
|
|
|
2013-08-29 14:10:02 -05:00
|
|
|
// We may eventually want to be able to fold over type parameters, too.
|
2014-01-09 07:05:33 -06:00
|
|
|
pub trait Folder {
|
2013-12-27 21:34:51 -06:00
|
|
|
fn fold_crate(&mut self, c: Crate) -> Crate {
|
2013-08-29 14:10:02 -05:00
|
|
|
noop_fold_crate(c, self)
|
2013-09-06 21:11:55 -05:00
|
|
|
}
|
|
|
|
|
2013-12-27 21:34:51 -06:00
|
|
|
fn fold_meta_items(&mut self, meta_items: &[@MetaItem]) -> ~[@MetaItem] {
|
2013-08-29 14:10:02 -05:00
|
|
|
meta_items.map(|x| fold_meta_item_(*x, self))
|
2013-09-06 21:11:55 -05:00
|
|
|
}
|
|
|
|
|
2014-01-09 07:05:33 -06:00
|
|
|
fn fold_view_paths(&mut self, view_paths: &[@ViewPath]) -> ~[@ViewPath] {
|
2013-08-29 14:10:02 -05:00
|
|
|
view_paths.map(|view_path| {
|
|
|
|
let inner_view_path = match view_path.node {
|
2014-01-09 07:05:33 -06:00
|
|
|
ViewPathSimple(ref ident, ref path, node_id) => {
|
|
|
|
ViewPathSimple(ident.clone(),
|
|
|
|
self.fold_path(path),
|
|
|
|
self.new_id(node_id))
|
2013-08-29 14:10:02 -05:00
|
|
|
}
|
2014-01-09 07:05:33 -06:00
|
|
|
ViewPathGlob(ref path, node_id) => {
|
|
|
|
ViewPathGlob(self.fold_path(path), self.new_id(node_id))
|
2013-08-29 14:10:02 -05:00
|
|
|
}
|
2014-01-09 07:05:33 -06:00
|
|
|
ViewPathList(ref path, ref path_list_idents, node_id) => {
|
|
|
|
ViewPathList(self.fold_path(path),
|
|
|
|
path_list_idents.map(|path_list_ident| {
|
2013-08-29 14:10:02 -05:00
|
|
|
let id = self.new_id(path_list_ident.node
|
|
|
|
.id);
|
|
|
|
Spanned {
|
2014-01-09 07:05:33 -06:00
|
|
|
node: PathListIdent_ {
|
2013-08-29 14:10:02 -05:00
|
|
|
name: path_list_ident.node
|
|
|
|
.name
|
|
|
|
.clone(),
|
|
|
|
id: id,
|
|
|
|
},
|
|
|
|
span: self.new_span(
|
|
|
|
path_list_ident.span)
|
2014-01-09 07:05:33 -06:00
|
|
|
}
|
|
|
|
}),
|
|
|
|
self.new_id(node_id))
|
2013-08-29 14:10:02 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
@Spanned {
|
|
|
|
node: inner_view_path,
|
|
|
|
span: self.new_span(view_path.span),
|
|
|
|
}
|
|
|
|
})
|
2013-09-06 21:11:55 -05:00
|
|
|
}
|
|
|
|
|
2014-01-09 07:05:33 -06:00
|
|
|
fn fold_view_item(&mut self, vi: &ViewItem) -> ViewItem {
|
2013-08-29 14:10:02 -05:00
|
|
|
let inner_view_item = match vi.node {
|
2014-01-09 07:05:33 -06:00
|
|
|
ViewItemExternMod(ref ident, string, node_id) => {
|
|
|
|
ViewItemExternMod(ident.clone(), string, self.new_id(node_id))
|
2013-09-06 21:11:55 -05:00
|
|
|
}
|
2014-01-09 07:05:33 -06:00
|
|
|
ViewItemUse(ref view_paths) => {
|
|
|
|
ViewItemUse(self.fold_view_paths(*view_paths))
|
2013-09-06 21:11:55 -05:00
|
|
|
}
|
2013-08-29 14:10:02 -05:00
|
|
|
};
|
2014-01-09 07:05:33 -06:00
|
|
|
ViewItem {
|
2013-08-29 14:10:02 -05:00
|
|
|
node: inner_view_item,
|
|
|
|
attrs: vi.attrs.map(|a| fold_attribute_(*a, self)),
|
|
|
|
vis: vi.vis,
|
|
|
|
span: self.new_span(vi.span),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-09 07:05:33 -06:00
|
|
|
fn fold_foreign_item(&mut self, ni: @ForeignItem) -> @ForeignItem {
|
2014-01-06 06:00:46 -06:00
|
|
|
noop_fold_foreign_item(ni, self)
|
2013-09-06 21:11:55 -05:00
|
|
|
}
|
|
|
|
|
2014-01-09 07:05:33 -06:00
|
|
|
fn fold_item(&mut self, i: @Item) -> SmallVector<@Item> {
|
2013-08-29 14:10:02 -05:00
|
|
|
noop_fold_item(i, self)
|
2013-09-06 21:11:55 -05:00
|
|
|
}
|
|
|
|
|
2014-01-09 07:05:33 -06:00
|
|
|
fn fold_struct_field(&mut self, sf: &StructField) -> StructField {
|
2013-08-29 14:10:02 -05:00
|
|
|
let fold_attribute = |x| fold_attribute_(x, self);
|
2013-09-06 21:11:55 -05:00
|
|
|
|
2013-11-30 10:27:25 -06:00
|
|
|
Spanned {
|
2014-01-09 07:05:33 -06:00
|
|
|
node: ast::StructField_ {
|
2013-08-29 14:10:02 -05:00
|
|
|
kind: sf.node.kind,
|
|
|
|
id: self.new_id(sf.node.id),
|
2013-11-30 16:00:39 -06:00
|
|
|
ty: self.fold_ty(sf.node.ty),
|
2013-08-29 14:10:02 -05:00
|
|
|
attrs: sf.node.attrs.map(|e| fold_attribute(*e))
|
|
|
|
},
|
|
|
|
span: self.new_span(sf.span)
|
2013-09-06 21:11:55 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-09 07:05:33 -06:00
|
|
|
fn fold_item_underscore(&mut self, i: &Item_) -> Item_ {
|
2013-08-29 14:10:02 -05:00
|
|
|
noop_fold_item_underscore(i, self)
|
|
|
|
}
|
|
|
|
|
2014-01-09 07:05:33 -06:00
|
|
|
fn fold_fn_decl(&mut self, d: &FnDecl) -> P<FnDecl> {
|
2014-01-06 06:00:46 -06:00
|
|
|
noop_fold_fn_decl(d, self)
|
|
|
|
}
|
|
|
|
|
2013-12-27 21:34:51 -06:00
|
|
|
fn fold_type_method(&mut self, m: &TypeMethod) -> TypeMethod {
|
2013-08-29 14:10:02 -05:00
|
|
|
noop_fold_type_method(m, self)
|
|
|
|
}
|
|
|
|
|
2014-01-09 07:05:33 -06:00
|
|
|
fn fold_method(&mut self, m: @Method) -> @Method {
|
2014-01-06 06:00:46 -06:00
|
|
|
noop_fold_method(m, self)
|
2013-09-06 21:11:55 -05:00
|
|
|
}
|
|
|
|
|
2013-12-27 21:34:51 -06:00
|
|
|
fn fold_block(&mut self, b: P<Block>) -> P<Block> {
|
2013-08-29 14:10:02 -05:00
|
|
|
noop_fold_block(b, self)
|
|
|
|
}
|
|
|
|
|
2013-12-27 21:34:51 -06:00
|
|
|
fn fold_stmt(&mut self, s: &Stmt) -> SmallVector<@Stmt> {
|
2013-08-29 14:10:02 -05:00
|
|
|
noop_fold_stmt(s, self)
|
|
|
|
}
|
|
|
|
|
2013-12-27 21:34:51 -06:00
|
|
|
fn fold_arm(&mut self, a: &Arm) -> Arm {
|
2013-08-29 14:10:02 -05:00
|
|
|
Arm {
|
|
|
|
pats: a.pats.map(|x| self.fold_pat(*x)),
|
2013-09-20 01:08:47 -05:00
|
|
|
guard: a.guard.map(|x| self.fold_expr(x)),
|
2013-11-30 16:00:39 -06:00
|
|
|
body: self.fold_block(a.body),
|
2013-09-06 21:11:55 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-27 21:34:51 -06:00
|
|
|
fn fold_pat(&mut self, p: @Pat) -> @Pat {
|
2014-01-06 06:00:46 -06:00
|
|
|
noop_fold_pat(p, self)
|
2013-09-06 21:11:55 -05:00
|
|
|
}
|
|
|
|
|
2013-12-27 21:34:51 -06:00
|
|
|
fn fold_decl(&mut self, d: @Decl) -> SmallVector<@Decl> {
|
2013-08-29 14:10:02 -05:00
|
|
|
let node = match d.node {
|
2013-11-25 01:08:53 -06:00
|
|
|
DeclLocal(ref l) => SmallVector::one(DeclLocal(self.fold_local(*l))),
|
2013-08-29 14:10:02 -05:00
|
|
|
DeclItem(it) => {
|
2013-11-25 01:08:53 -06:00
|
|
|
self.fold_item(it).move_iter().map(|i| DeclItem(i)).collect()
|
2013-08-29 14:10:02 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-11-25 01:08:53 -06:00
|
|
|
node.move_iter().map(|node| {
|
2013-08-29 14:10:02 -05:00
|
|
|
@Spanned {
|
|
|
|
node: node,
|
|
|
|
span: d.span,
|
|
|
|
}
|
2013-11-25 01:08:53 -06:00
|
|
|
}).collect()
|
2013-08-29 14:10:02 -05:00
|
|
|
}
|
|
|
|
|
2013-12-27 21:34:51 -06:00
|
|
|
fn fold_expr(&mut self, e: @Expr) -> @Expr {
|
2013-08-29 14:10:02 -05:00
|
|
|
noop_fold_expr(e, self)
|
|
|
|
}
|
|
|
|
|
2013-12-27 21:34:51 -06:00
|
|
|
fn fold_ty(&mut self, t: P<Ty>) -> P<Ty> {
|
2013-08-29 14:10:02 -05:00
|
|
|
let node = match t.node {
|
2014-01-09 07:05:33 -06:00
|
|
|
TyNil | TyBot | TyInfer => t.node.clone(),
|
|
|
|
TyBox(ty) => TyBox(self.fold_ty(ty)),
|
|
|
|
TyUniq(ty) => TyUniq(self.fold_ty(ty)),
|
|
|
|
TyVec(ty) => TyVec(self.fold_ty(ty)),
|
|
|
|
TyPtr(ref mt) => TyPtr(fold_mt(mt, self)),
|
|
|
|
TyRptr(ref region, ref mt) => {
|
|
|
|
TyRptr(fold_opt_lifetime(region, self), fold_mt(mt, self))
|
2013-10-29 05:03:32 -05:00
|
|
|
}
|
2014-01-09 07:05:33 -06:00
|
|
|
TyClosure(ref f) => {
|
|
|
|
TyClosure(@ClosureTy {
|
2013-08-29 14:10:02 -05:00
|
|
|
sigil: f.sigil,
|
|
|
|
purity: f.purity,
|
2013-10-29 05:03:32 -05:00
|
|
|
region: fold_opt_lifetime(&f.region, self),
|
2013-08-29 14:10:02 -05:00
|
|
|
onceness: f.onceness,
|
|
|
|
bounds: fold_opt_bounds(&f.bounds, self),
|
2014-01-06 06:00:46 -06:00
|
|
|
decl: self.fold_fn_decl(f.decl),
|
2013-08-29 14:10:02 -05:00
|
|
|
lifetimes: f.lifetimes.map(|l| fold_lifetime(l, self)),
|
|
|
|
})
|
|
|
|
}
|
2014-01-09 07:05:33 -06:00
|
|
|
TyBareFn(ref f) => {
|
|
|
|
TyBareFn(@BareFnTy {
|
2013-08-29 14:10:02 -05:00
|
|
|
lifetimes: f.lifetimes.map(|l| fold_lifetime(l, self)),
|
|
|
|
purity: f.purity,
|
|
|
|
abis: f.abis,
|
2014-01-06 06:00:46 -06:00
|
|
|
decl: self.fold_fn_decl(f.decl)
|
2013-08-29 14:10:02 -05:00
|
|
|
})
|
|
|
|
}
|
2014-01-09 07:05:33 -06:00
|
|
|
TyTup(ref tys) => TyTup(tys.map(|&ty| self.fold_ty(ty))),
|
|
|
|
TyPath(ref path, ref bounds, id) => {
|
|
|
|
TyPath(self.fold_path(path),
|
|
|
|
fold_opt_bounds(bounds, self),
|
|
|
|
self.new_id(id))
|
2013-08-29 14:10:02 -05:00
|
|
|
}
|
2014-01-09 07:05:33 -06:00
|
|
|
TyFixedLengthVec(ty, e) => {
|
|
|
|
TyFixedLengthVec(self.fold_ty(ty), self.fold_expr(e))
|
2013-08-29 14:10:02 -05:00
|
|
|
}
|
2014-01-09 07:05:33 -06:00
|
|
|
TyTypeof(expr) => TyTypeof(self.fold_expr(expr)),
|
2013-08-29 14:10:02 -05:00
|
|
|
};
|
2013-11-30 16:00:39 -06:00
|
|
|
P(Ty {
|
2013-08-29 14:10:02 -05:00
|
|
|
id: self.new_id(t.id),
|
|
|
|
span: self.new_span(t.span),
|
|
|
|
node: node,
|
2013-11-30 16:00:39 -06:00
|
|
|
})
|
2013-09-06 21:11:55 -05:00
|
|
|
}
|
|
|
|
|
2014-01-09 07:05:33 -06:00
|
|
|
fn fold_mod(&mut self, m: &Mod) -> Mod {
|
2013-08-29 14:10:02 -05:00
|
|
|
noop_fold_mod(m, self)
|
2013-09-06 21:11:55 -05:00
|
|
|
}
|
|
|
|
|
2014-01-09 07:05:33 -06:00
|
|
|
fn fold_foreign_mod(&mut self, nm: &ForeignMod) -> ForeignMod {
|
|
|
|
ast::ForeignMod {
|
2013-08-29 14:10:02 -05:00
|
|
|
abis: nm.abis,
|
|
|
|
view_items: nm.view_items
|
|
|
|
.iter()
|
|
|
|
.map(|x| self.fold_view_item(x))
|
|
|
|
.collect(),
|
|
|
|
items: nm.items
|
|
|
|
.iter()
|
|
|
|
.map(|x| self.fold_foreign_item(*x))
|
|
|
|
.collect(),
|
2013-09-06 21:11:55 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-09 07:05:33 -06:00
|
|
|
fn fold_variant(&mut self, v: &Variant) -> P<Variant> {
|
2013-08-29 14:10:02 -05:00
|
|
|
let kind;
|
|
|
|
match v.node.kind {
|
2014-01-09 07:05:33 -06:00
|
|
|
TupleVariantKind(ref variant_args) => {
|
|
|
|
kind = TupleVariantKind(variant_args.map(|x|
|
2013-08-29 14:10:02 -05:00
|
|
|
fold_variant_arg_(x, self)))
|
|
|
|
}
|
2014-01-09 07:05:33 -06:00
|
|
|
StructVariantKind(ref struct_def) => {
|
|
|
|
kind = StructVariantKind(@ast::StructDef {
|
2013-08-29 14:10:02 -05:00
|
|
|
fields: struct_def.fields.iter()
|
2013-11-30 10:27:25 -06:00
|
|
|
.map(|f| self.fold_struct_field(f)).collect(),
|
2013-09-20 01:08:47 -05:00
|
|
|
ctor_id: struct_def.ctor_id.map(|c| self.new_id(c))
|
2013-08-29 14:10:02 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let fold_attribute = |x| fold_attribute_(x, self);
|
|
|
|
let attrs = v.node.attrs.map(|x| fold_attribute(*x));
|
|
|
|
|
|
|
|
let de = match v.node.disr_expr {
|
|
|
|
Some(e) => Some(self.fold_expr(e)),
|
|
|
|
None => None
|
|
|
|
};
|
2014-01-09 07:05:33 -06:00
|
|
|
let node = ast::Variant_ {
|
2013-08-29 14:10:02 -05:00
|
|
|
name: v.node.name,
|
|
|
|
attrs: attrs,
|
|
|
|
kind: kind,
|
|
|
|
id: self.new_id(v.node.id),
|
|
|
|
disr_expr: de,
|
|
|
|
vis: v.node.vis,
|
|
|
|
};
|
2013-11-30 16:00:39 -06:00
|
|
|
P(Spanned {
|
2013-08-29 14:10:02 -05:00
|
|
|
node: node,
|
|
|
|
span: self.new_span(v.span),
|
2013-11-30 16:00:39 -06:00
|
|
|
})
|
2013-09-06 21:11:55 -05:00
|
|
|
}
|
|
|
|
|
2013-12-27 21:34:51 -06:00
|
|
|
fn fold_ident(&mut self, i: Ident) -> Ident {
|
2013-08-29 14:10:02 -05:00
|
|
|
i
|
|
|
|
}
|
2013-09-06 21:11:55 -05:00
|
|
|
|
2013-12-27 21:34:51 -06:00
|
|
|
fn fold_path(&mut self, p: &Path) -> Path {
|
2013-08-29 14:10:02 -05:00
|
|
|
ast::Path {
|
|
|
|
span: self.new_span(p.span),
|
|
|
|
global: p.global,
|
|
|
|
segments: p.segments.map(|segment| ast::PathSegment {
|
|
|
|
identifier: self.fold_ident(segment.identifier),
|
2013-10-29 05:03:32 -05:00
|
|
|
lifetimes: segment.lifetimes.map(|l| fold_lifetime(l, self)),
|
2013-11-30 16:00:39 -06:00
|
|
|
types: segment.types.map(|&typ| self.fold_ty(typ)),
|
2013-09-06 21:11:55 -05:00
|
|
|
})
|
2013-08-29 14:10:02 -05:00
|
|
|
}
|
2013-09-06 21:11:55 -05:00
|
|
|
}
|
|
|
|
|
2013-12-27 21:34:51 -06:00
|
|
|
fn fold_local(&mut self, l: @Local) -> @Local {
|
2013-08-29 14:10:02 -05:00
|
|
|
@Local {
|
2013-11-30 16:00:39 -06:00
|
|
|
ty: self.fold_ty(l.ty),
|
2013-08-29 14:10:02 -05:00
|
|
|
pat: self.fold_pat(l.pat),
|
2013-09-20 01:08:47 -05:00
|
|
|
init: l.init.map(|e| self.fold_expr(e)),
|
2013-08-29 14:10:02 -05:00
|
|
|
id: self.new_id(l.id),
|
|
|
|
span: self.new_span(l.span),
|
|
|
|
}
|
2013-09-06 21:11:55 -05:00
|
|
|
}
|
|
|
|
|
2014-01-09 07:05:33 -06:00
|
|
|
fn fold_mac(&mut self, macro: &Mac) -> Mac {
|
2013-09-06 21:11:55 -05:00
|
|
|
Spanned {
|
2013-08-29 14:10:02 -05:00
|
|
|
node: match macro.node {
|
2014-01-09 07:05:33 -06:00
|
|
|
MacInvocTT(ref p, ref tts, ctxt) => {
|
|
|
|
MacInvocTT(self.fold_path(p),
|
|
|
|
fold_tts(*tts, self),
|
|
|
|
ctxt)
|
2013-08-29 14:10:02 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
span: self.new_span(macro.span)
|
2013-09-06 21:11:55 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-19 14:21:21 -06:00
|
|
|
fn map_exprs(&self, f: |@Expr| -> @Expr, es: &[@Expr]) -> ~[@Expr] {
|
2013-08-29 14:10:02 -05:00
|
|
|
es.map(|x| f(*x))
|
|
|
|
}
|
|
|
|
|
2013-12-27 21:34:51 -06:00
|
|
|
fn new_id(&mut self, i: NodeId) -> NodeId {
|
2013-08-29 14:10:02 -05:00
|
|
|
i
|
|
|
|
}
|
|
|
|
|
2013-12-27 21:34:51 -06:00
|
|
|
fn new_span(&mut self, sp: Span) -> Span {
|
2013-08-29 14:10:02 -05:00
|
|
|
sp
|
2013-09-06 21:11:55 -05:00
|
|
|
}
|
2013-10-29 05:03:32 -05:00
|
|
|
|
2014-01-09 07:05:33 -06:00
|
|
|
fn fold_explicit_self(&mut self, es: &ExplicitSelf) -> ExplicitSelf {
|
2013-10-29 05:03:32 -05:00
|
|
|
Spanned {
|
|
|
|
span: self.new_span(es.span),
|
|
|
|
node: self.fold_explicit_self_(&es.node)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-09 07:05:33 -06:00
|
|
|
fn fold_explicit_self_(&mut self, es: &ExplicitSelf_) -> ExplicitSelf_ {
|
2013-10-29 05:03:32 -05:00
|
|
|
match *es {
|
2014-01-09 07:05:33 -06:00
|
|
|
SelfStatic | SelfValue(_) | SelfUniq(_) | SelfBox(_) => {
|
2013-10-29 05:03:32 -05:00
|
|
|
*es
|
|
|
|
}
|
2014-01-09 07:05:33 -06:00
|
|
|
SelfRegion(ref lifetime, m) => {
|
|
|
|
SelfRegion(fold_opt_lifetime(lifetime, self), m)
|
2013-10-29 05:03:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-05-21 20:28:39 -05:00
|
|
|
}
|
2011-06-20 19:25:49 -05:00
|
|
|
|
2014-01-09 07:05:33 -06:00
|
|
|
/* some little folds that probably aren't useful to have in Folder itself*/
|
2013-08-29 14:10:02 -05:00
|
|
|
|
|
|
|
//used in noop_fold_item and noop_fold_crate and noop_fold_crate_directive
|
2014-01-09 07:05:33 -06:00
|
|
|
fn fold_meta_item_<T: Folder>(mi: @MetaItem, fld: &mut T) -> @MetaItem {
|
2013-08-29 14:10:02 -05:00
|
|
|
@Spanned {
|
|
|
|
node:
|
|
|
|
match mi.node {
|
|
|
|
MetaWord(id) => MetaWord(id),
|
|
|
|
MetaList(id, ref mis) => {
|
|
|
|
let fold_meta_item = |x| fold_meta_item_(x, fld);
|
|
|
|
MetaList(
|
|
|
|
id,
|
|
|
|
mis.map(|e| fold_meta_item(*e))
|
|
|
|
)
|
|
|
|
}
|
|
|
|
MetaNameValue(id, s) => MetaNameValue(id, s)
|
|
|
|
},
|
|
|
|
span: fld.new_span(mi.span) }
|
2013-01-08 16:00:45 -06:00
|
|
|
}
|
|
|
|
|
2013-08-29 14:10:02 -05:00
|
|
|
//used in noop_fold_item and noop_fold_crate
|
2014-01-09 07:05:33 -06:00
|
|
|
fn fold_attribute_<T: Folder>(at: Attribute, fld: &mut T) -> Attribute {
|
2013-08-29 14:10:02 -05:00
|
|
|
Spanned {
|
|
|
|
span: fld.new_span(at.span),
|
|
|
|
node: ast::Attribute_ {
|
|
|
|
style: at.node.style,
|
|
|
|
value: fold_meta_item_(at.node.value, fld),
|
|
|
|
is_sugared_doc: at.node.is_sugared_doc
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-06-20 19:25:49 -05:00
|
|
|
|
2013-08-29 14:10:02 -05:00
|
|
|
//used in noop_fold_foreign_item and noop_fold_fn_decl
|
2014-01-09 07:05:33 -06:00
|
|
|
fn fold_arg_<T: Folder>(a: &Arg, fld: &mut T) -> Arg {
|
|
|
|
Arg {
|
2013-11-30 16:00:39 -06:00
|
|
|
ty: fld.fold_ty(a.ty),
|
2013-08-29 14:10:02 -05:00
|
|
|
pat: fld.fold_pat(a.pat),
|
|
|
|
id: fld.new_id(a.id),
|
|
|
|
}
|
|
|
|
}
|
2011-06-20 19:25:49 -05:00
|
|
|
|
2014-01-09 07:05:33 -06:00
|
|
|
// build a new vector of tts by appling the Folder's fold_ident to
|
2013-08-29 14:10:02 -05:00
|
|
|
// all of the identifiers in the token trees.
|
2014-01-09 07:05:33 -06:00
|
|
|
pub fn fold_tts<T: Folder>(tts: &[TokenTree], fld: &mut T) -> ~[TokenTree] {
|
2013-11-20 18:23:04 -06:00
|
|
|
tts.map(|tt| {
|
2013-06-06 16:17:18 -05:00
|
|
|
match *tt {
|
2014-01-09 07:05:33 -06:00
|
|
|
TTTok(span, ref tok) =>
|
|
|
|
TTTok(span,maybe_fold_ident(tok,fld)),
|
|
|
|
TTDelim(tts) => TTDelim(@fold_tts(*tts, fld)),
|
|
|
|
TTSeq(span, pattern, ref sep, is_optional) =>
|
|
|
|
TTSeq(span,
|
|
|
|
@fold_tts(*pattern, fld),
|
|
|
|
sep.as_ref().map(|tok|maybe_fold_ident(tok,fld)),
|
|
|
|
is_optional),
|
|
|
|
TTNonterminal(sp,ref ident) =>
|
|
|
|
TTNonterminal(sp,fld.fold_ident(*ident))
|
2013-06-06 16:17:18 -05:00
|
|
|
}
|
2013-11-20 18:23:04 -06:00
|
|
|
})
|
2013-06-06 16:17:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// apply ident folder if it's an ident, otherwise leave it alone
|
2014-01-09 07:05:33 -06:00
|
|
|
fn maybe_fold_ident<T: Folder>(t: &token::Token, fld: &mut T) -> token::Token {
|
2013-06-06 16:17:18 -05:00
|
|
|
match *t {
|
2013-08-29 14:10:02 -05:00
|
|
|
token::IDENT(id, followed_by_colons) => {
|
|
|
|
token::IDENT(fld.fold_ident(id), followed_by_colons)
|
|
|
|
}
|
2013-07-02 14:47:32 -05:00
|
|
|
_ => (*t).clone()
|
2013-02-18 00:20:36 -06:00
|
|
|
}
|
2011-07-08 18:35:09 -05:00
|
|
|
}
|
|
|
|
|
2014-01-09 07:05:33 -06:00
|
|
|
pub fn noop_fold_fn_decl<T: Folder>(decl: &FnDecl, fld: &mut T) -> P<FnDecl> {
|
|
|
|
P(FnDecl {
|
2013-08-29 14:10:02 -05:00
|
|
|
inputs: decl.inputs.map(|x| fold_arg_(x, fld)), // bad copy
|
2013-11-30 16:00:39 -06:00
|
|
|
output: fld.fold_ty(decl.output),
|
2013-01-15 18:05:20 -06:00
|
|
|
cf: decl.cf,
|
2013-10-25 00:56:34 -05:00
|
|
|
variadic: decl.variadic
|
2013-11-30 16:00:39 -06:00
|
|
|
})
|
2011-12-20 13:03:21 -06:00
|
|
|
}
|
2011-06-20 19:25:49 -05:00
|
|
|
|
2014-01-09 07:05:33 -06:00
|
|
|
fn fold_ty_param_bound<T: Folder>(tpb: &TyParamBound, fld: &mut T)
|
2014-01-06 06:00:46 -06:00
|
|
|
-> TyParamBound {
|
2013-08-29 14:10:02 -05:00
|
|
|
match *tpb {
|
|
|
|
TraitTyParamBound(ref ty) => TraitTyParamBound(fold_trait_ref(ty, fld)),
|
|
|
|
RegionTyParamBound => RegionTyParamBound
|
|
|
|
}
|
2012-02-14 17:21:53 -06:00
|
|
|
}
|
|
|
|
|
2014-01-09 07:05:33 -06:00
|
|
|
pub fn fold_ty_param<T: Folder>(tp: &TyParam, fld: &mut T) -> TyParam {
|
2013-08-29 14:10:02 -05:00
|
|
|
TyParam {
|
|
|
|
ident: tp.ident,
|
|
|
|
id: fld.new_id(tp.id),
|
|
|
|
bounds: tp.bounds.map(|x| fold_ty_param_bound(x, fld)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-09 07:05:33 -06:00
|
|
|
pub fn fold_ty_params<T: Folder>(tps: &OptVec<TyParam>, fld: &mut T)
|
2014-01-06 06:00:46 -06:00
|
|
|
-> OptVec<TyParam> {
|
2013-08-29 14:10:02 -05:00
|
|
|
tps.map(|tp| fold_ty_param(tp, fld))
|
|
|
|
}
|
|
|
|
|
2014-01-09 07:05:33 -06:00
|
|
|
pub fn fold_lifetime<T: Folder>(l: &Lifetime, fld: &mut T) -> Lifetime {
|
2013-08-29 14:10:02 -05:00
|
|
|
Lifetime {
|
|
|
|
id: fld.new_id(l.id),
|
|
|
|
span: fld.new_span(l.span),
|
|
|
|
ident: l.ident
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-09 07:05:33 -06:00
|
|
|
pub fn fold_lifetimes<T: Folder>(lts: &OptVec<Lifetime>, fld: &mut T)
|
2014-01-06 06:00:46 -06:00
|
|
|
-> OptVec<Lifetime> {
|
2013-08-29 14:10:02 -05:00
|
|
|
lts.map(|l| fold_lifetime(l, fld))
|
|
|
|
}
|
|
|
|
|
2014-01-09 07:05:33 -06:00
|
|
|
pub fn fold_opt_lifetime<T: Folder>(o_lt: &Option<Lifetime>, fld: &mut T)
|
2014-01-06 06:00:46 -06:00
|
|
|
-> Option<Lifetime> {
|
2013-10-29 05:03:32 -05:00
|
|
|
o_lt.as_ref().map(|lt| fold_lifetime(lt, fld))
|
|
|
|
}
|
|
|
|
|
2014-01-09 07:05:33 -06:00
|
|
|
pub fn fold_generics<T: Folder>(generics: &Generics, fld: &mut T) -> Generics {
|
2013-08-29 14:10:02 -05:00
|
|
|
Generics {ty_params: fold_ty_params(&generics.ty_params, fld),
|
|
|
|
lifetimes: fold_lifetimes(&generics.lifetimes, fld)}
|
|
|
|
}
|
|
|
|
|
2014-01-09 07:05:33 -06:00
|
|
|
fn fold_struct_def<T: Folder>(struct_def: @StructDef, fld: &mut T) -> @StructDef {
|
|
|
|
@ast::StructDef {
|
2013-11-30 10:27:25 -06:00
|
|
|
fields: struct_def.fields.map(|f| fold_struct_field(f, fld)),
|
2013-09-20 01:08:47 -05:00
|
|
|
ctor_id: struct_def.ctor_id.map(|cid| fld.new_id(cid)),
|
2013-01-14 21:06:59 -06:00
|
|
|
}
|
2011-06-20 19:25:49 -05:00
|
|
|
}
|
|
|
|
|
2014-01-09 07:05:33 -06:00
|
|
|
fn fold_trait_ref<T: Folder>(p: &TraitRef, fld: &mut T) -> TraitRef {
|
|
|
|
ast::TraitRef {
|
2013-08-29 14:10:02 -05:00
|
|
|
path: fld.fold_path(&p.path),
|
|
|
|
ref_id: fld.new_id(p.ref_id),
|
2013-01-13 14:02:16 -06:00
|
|
|
}
|
2011-06-20 19:25:49 -05:00
|
|
|
}
|
|
|
|
|
2014-01-09 07:05:33 -06:00
|
|
|
fn fold_struct_field<T: Folder>(f: &StructField, fld: &mut T) -> StructField {
|
2013-11-30 10:27:25 -06:00
|
|
|
Spanned {
|
2014-01-09 07:05:33 -06:00
|
|
|
node: ast::StructField_ {
|
2013-08-29 14:10:02 -05:00
|
|
|
kind: f.node.kind,
|
|
|
|
id: fld.new_id(f.node.id),
|
2013-11-30 16:00:39 -06:00
|
|
|
ty: fld.fold_ty(f.node.ty),
|
2013-08-29 14:10:02 -05:00
|
|
|
attrs: f.node.attrs.map(|a| fold_attribute_(*a, fld)),
|
2013-06-27 19:41:35 -05:00
|
|
|
},
|
2013-08-29 14:10:02 -05:00
|
|
|
span: fld.new_span(f.span),
|
2013-06-27 19:41:35 -05:00
|
|
|
}
|
2012-01-31 21:30:40 -06:00
|
|
|
}
|
|
|
|
|
2014-01-09 07:05:33 -06:00
|
|
|
fn fold_field_<T: Folder>(field: Field, folder: &mut T) -> Field {
|
2013-08-29 14:10:02 -05:00
|
|
|
ast::Field {
|
2013-10-28 21:22:42 -05:00
|
|
|
ident: respan(field.ident.span, folder.fold_ident(field.ident.node)),
|
2013-08-29 14:10:02 -05:00
|
|
|
expr: folder.fold_expr(field.expr),
|
|
|
|
span: folder.new_span(field.span),
|
2013-09-06 21:11:55 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-09 07:05:33 -06:00
|
|
|
fn fold_mt<T: Folder>(mt: &MutTy, folder: &mut T) -> MutTy {
|
|
|
|
MutTy {
|
2013-11-30 16:00:39 -06:00
|
|
|
ty: folder.fold_ty(mt.ty),
|
2013-08-29 14:10:02 -05:00
|
|
|
mutbl: mt.mutbl,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-09 07:05:33 -06:00
|
|
|
fn fold_opt_bounds<T: Folder>(b: &Option<OptVec<TyParamBound>>, folder: &mut T)
|
|
|
|
-> Option<OptVec<TyParamBound>> {
|
2013-11-20 18:23:04 -06:00
|
|
|
b.as_ref().map(|bounds| {
|
|
|
|
bounds.map(|bound| {
|
2013-08-29 14:10:02 -05:00
|
|
|
fold_ty_param_bound(bound, folder)
|
2013-11-20 18:23:04 -06:00
|
|
|
})
|
|
|
|
})
|
2013-08-29 14:10:02 -05:00
|
|
|
}
|
|
|
|
|
2014-01-09 07:05:33 -06:00
|
|
|
fn fold_variant_arg_<T: Folder>(va: &VariantArg, folder: &mut T) -> VariantArg {
|
|
|
|
ast::VariantArg {
|
2013-11-30 16:00:39 -06:00
|
|
|
ty: folder.fold_ty(va.ty),
|
2013-08-29 14:10:02 -05:00
|
|
|
id: folder.new_id(va.id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-09 07:05:33 -06:00
|
|
|
pub fn noop_fold_block<T: Folder>(b: P<Block>, folder: &mut T) -> P<Block> {
|
2013-08-29 14:10:02 -05:00
|
|
|
let view_items = b.view_items.map(|x| folder.fold_view_item(x));
|
2013-11-25 01:08:53 -06:00
|
|
|
let stmts = b.stmts.iter().flat_map(|s| folder.fold_stmt(*s).move_iter()).collect();
|
2013-11-30 16:00:39 -06:00
|
|
|
P(Block {
|
2013-08-29 14:10:02 -05:00
|
|
|
view_items: view_items,
|
|
|
|
stmts: stmts,
|
2013-09-20 01:08:47 -05:00
|
|
|
expr: b.expr.map(|x| folder.fold_expr(x)),
|
2013-08-29 14:10:02 -05:00
|
|
|
id: folder.new_id(b.id),
|
|
|
|
rules: b.rules,
|
|
|
|
span: folder.new_span(b.span),
|
2013-11-30 16:00:39 -06:00
|
|
|
})
|
2013-08-29 14:10:02 -05:00
|
|
|
}
|
|
|
|
|
2014-01-09 07:05:33 -06:00
|
|
|
pub fn noop_fold_item_underscore<T: Folder>(i: &Item_, folder: &mut T) -> Item_ {
|
2013-02-18 00:20:36 -06:00
|
|
|
match *i {
|
2014-01-09 07:05:33 -06:00
|
|
|
ItemStatic(t, m, e) => {
|
|
|
|
ItemStatic(folder.fold_ty(t), m, folder.fold_expr(e))
|
2013-09-06 21:11:55 -05:00
|
|
|
}
|
2014-01-09 07:05:33 -06:00
|
|
|
ItemFn(decl, purity, abi, ref generics, body) => {
|
|
|
|
ItemFn(
|
2014-01-06 06:00:46 -06:00
|
|
|
folder.fold_fn_decl(decl),
|
2013-02-18 00:20:36 -06:00
|
|
|
purity,
|
2013-03-13 21:25:28 -05:00
|
|
|
abi,
|
2013-08-29 14:10:02 -05:00
|
|
|
fold_generics(generics, folder),
|
|
|
|
folder.fold_block(body)
|
2013-02-18 00:20:36 -06:00
|
|
|
)
|
|
|
|
}
|
2014-01-09 07:05:33 -06:00
|
|
|
ItemMod(ref m) => ItemMod(folder.fold_mod(m)),
|
|
|
|
ItemForeignMod(ref nm) => ItemForeignMod(folder.fold_foreign_mod(nm)),
|
|
|
|
ItemTy(t, ref generics) => {
|
|
|
|
ItemTy(folder.fold_ty(t), fold_generics(generics, folder))
|
2013-02-18 00:20:36 -06:00
|
|
|
}
|
2014-01-09 07:05:33 -06:00
|
|
|
ItemEnum(ref enum_definition, ref generics) => {
|
|
|
|
ItemEnum(
|
|
|
|
ast::EnumDef {
|
2013-11-30 16:00:39 -06:00
|
|
|
variants: enum_definition.variants.map(|&x| {
|
2013-08-29 14:10:02 -05:00
|
|
|
folder.fold_variant(x)
|
2013-11-20 18:23:04 -06:00
|
|
|
}),
|
2013-03-07 20:04:21 -06:00
|
|
|
},
|
2013-08-29 14:10:02 -05:00
|
|
|
fold_generics(generics, folder))
|
2013-02-18 00:20:36 -06:00
|
|
|
}
|
2014-01-09 07:05:33 -06:00
|
|
|
ItemStruct(ref struct_def, ref generics) => {
|
2013-08-29 14:10:02 -05:00
|
|
|
let struct_def = fold_struct_def(*struct_def, folder);
|
2014-01-09 07:05:33 -06:00
|
|
|
ItemStruct(struct_def, fold_generics(generics, folder))
|
2013-02-11 10:02:51 -06:00
|
|
|
}
|
2014-01-09 07:05:33 -06:00
|
|
|
ItemImpl(ref generics, ref ifce, ty, ref methods) => {
|
|
|
|
ItemImpl(fold_generics(generics, folder),
|
|
|
|
ifce.as_ref().map(|p| fold_trait_ref(p, folder)),
|
|
|
|
folder.fold_ty(ty),
|
|
|
|
methods.map(|x| folder.fold_method(*x))
|
2013-02-18 00:20:36 -06:00
|
|
|
)
|
2013-02-11 10:02:51 -06:00
|
|
|
}
|
2014-01-09 07:05:33 -06:00
|
|
|
ItemTrait(ref generics, ref traits, ref methods) => {
|
2013-11-20 18:23:04 -06:00
|
|
|
let methods = methods.map(|method| {
|
2013-02-11 10:02:51 -06:00
|
|
|
match *method {
|
2014-01-09 07:05:33 -06:00
|
|
|
Required(ref m) => Required(folder.fold_type_method(m)),
|
|
|
|
Provided(method) => Provided(folder.fold_method(method))
|
2013-02-11 10:02:51 -06:00
|
|
|
}
|
2013-11-20 18:23:04 -06:00
|
|
|
});
|
2014-01-09 07:05:33 -06:00
|
|
|
ItemTrait(fold_generics(generics, folder),
|
|
|
|
traits.map(|p| fold_trait_ref(p, folder)),
|
|
|
|
methods)
|
2013-02-11 10:02:51 -06:00
|
|
|
}
|
2014-01-09 07:05:33 -06:00
|
|
|
ItemMac(ref m) => ItemMac(folder.fold_mac(m)),
|
2013-02-11 10:02:51 -06:00
|
|
|
}
|
2011-06-20 19:25:49 -05:00
|
|
|
}
|
|
|
|
|
2014-01-09 07:05:33 -06:00
|
|
|
pub fn noop_fold_type_method<T: Folder>(m: &TypeMethod, fld: &mut T) -> TypeMethod {
|
2013-08-29 14:10:02 -05:00
|
|
|
TypeMethod {
|
2013-01-15 18:05:20 -06:00
|
|
|
ident: fld.fold_ident(m.ident),
|
2013-08-29 14:10:02 -05:00
|
|
|
attrs: m.attrs.map(|a| fold_attribute_(*a, fld)),
|
2013-01-15 18:05:20 -06:00
|
|
|
purity: m.purity,
|
2014-01-06 06:00:46 -06:00
|
|
|
decl: fld.fold_fn_decl(m.decl),
|
2013-08-29 14:10:02 -05:00
|
|
|
generics: fold_generics(&m.generics, fld),
|
2013-10-29 05:03:32 -05:00
|
|
|
explicit_self: fld.fold_explicit_self(&m.explicit_self),
|
2013-01-15 18:05:20 -06:00
|
|
|
id: fld.new_id(m.id),
|
|
|
|
span: fld.new_span(m.span),
|
2013-01-14 21:35:08 -06:00
|
|
|
}
|
2011-06-20 19:25:49 -05:00
|
|
|
}
|
|
|
|
|
2014-01-09 07:05:33 -06:00
|
|
|
pub fn noop_fold_mod<T: Folder>(m: &Mod, folder: &mut T) -> Mod {
|
|
|
|
ast::Mod {
|
2013-08-29 14:10:02 -05:00
|
|
|
view_items: m.view_items
|
|
|
|
.iter()
|
|
|
|
.map(|x| folder.fold_view_item(x)).collect(),
|
2013-11-25 01:08:53 -06:00
|
|
|
items: m.items.iter().flat_map(|x| folder.fold_item(*x).move_iter()).collect(),
|
2013-02-18 00:20:36 -06:00
|
|
|
}
|
2011-06-20 19:25:49 -05:00
|
|
|
}
|
|
|
|
|
2014-01-09 07:05:33 -06:00
|
|
|
pub fn noop_fold_crate<T: Folder>(c: Crate, folder: &mut T) -> Crate {
|
2013-08-29 14:10:02 -05:00
|
|
|
let fold_meta_item = |x| fold_meta_item_(x, folder);
|
|
|
|
let fold_attribute = |x| fold_attribute_(x, folder);
|
2013-02-18 00:20:36 -06:00
|
|
|
|
2013-08-29 14:10:02 -05:00
|
|
|
Crate {
|
|
|
|
module: folder.fold_mod(&c.module),
|
|
|
|
attrs: c.attrs.map(|x| fold_attribute(*x)),
|
|
|
|
config: c.config.map(|x| fold_meta_item(*x)),
|
|
|
|
span: folder.new_span(c.span),
|
2013-02-18 00:20:36 -06:00
|
|
|
}
|
2011-06-20 19:25:49 -05:00
|
|
|
}
|
|
|
|
|
2014-01-09 07:05:33 -06:00
|
|
|
pub fn noop_fold_item<T: Folder>(i: &Item, folder: &mut T) -> SmallVector<@Item> {
|
2013-08-29 14:10:02 -05:00
|
|
|
let fold_attribute = |x| fold_attribute_(x, folder);
|
2011-06-20 19:25:49 -05:00
|
|
|
|
2014-01-09 07:05:33 -06:00
|
|
|
SmallVector::one(@Item {
|
2013-08-29 14:10:02 -05:00
|
|
|
ident: folder.fold_ident(i.ident),
|
|
|
|
attrs: i.attrs.map(|e| fold_attribute(*e)),
|
|
|
|
id: folder.new_id(i.id),
|
|
|
|
node: folder.fold_item_underscore(&i.node),
|
|
|
|
vis: i.vis,
|
|
|
|
span: folder.new_span(i.span)
|
|
|
|
})
|
2012-01-22 18:30:07 -06:00
|
|
|
}
|
|
|
|
|
2014-01-09 07:05:33 -06:00
|
|
|
pub fn noop_fold_foreign_item<T: Folder>(ni: &ForeignItem, folder: &mut T) -> @ForeignItem {
|
|
|
|
@ForeignItem {
|
2014-01-06 06:00:46 -06:00
|
|
|
ident: folder.fold_ident(ni.ident),
|
|
|
|
attrs: ni.attrs.map(|x| fold_attribute_(*x, folder)),
|
|
|
|
node: match ni.node {
|
2014-01-09 07:05:33 -06:00
|
|
|
ForeignItemFn(ref fdec, ref generics) => {
|
|
|
|
ForeignItemFn(P(FnDecl {
|
2014-01-06 06:00:46 -06:00
|
|
|
inputs: fdec.inputs.map(|a| fold_arg_(a, folder)),
|
|
|
|
output: folder.fold_ty(fdec.output),
|
|
|
|
cf: fdec.cf,
|
|
|
|
variadic: fdec.variadic
|
|
|
|
}), fold_generics(generics, folder))
|
|
|
|
}
|
2014-01-09 07:05:33 -06:00
|
|
|
ForeignItemStatic(t, m) => {
|
|
|
|
ForeignItemStatic(folder.fold_ty(t), m)
|
2014-01-06 06:00:46 -06:00
|
|
|
}
|
|
|
|
},
|
|
|
|
id: folder.new_id(ni.id),
|
|
|
|
span: folder.new_span(ni.span),
|
|
|
|
vis: ni.vis,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-09 07:05:33 -06:00
|
|
|
pub fn noop_fold_method<T: Folder>(m: &Method, folder: &mut T) -> @Method {
|
|
|
|
@Method {
|
2014-01-06 06:00:46 -06:00
|
|
|
ident: folder.fold_ident(m.ident),
|
|
|
|
attrs: m.attrs.map(|a| fold_attribute_(*a, folder)),
|
|
|
|
generics: fold_generics(&m.generics, folder),
|
|
|
|
explicit_self: folder.fold_explicit_self(&m.explicit_self),
|
|
|
|
purity: m.purity,
|
|
|
|
decl: folder.fold_fn_decl(m.decl),
|
|
|
|
body: folder.fold_block(m.body),
|
|
|
|
id: folder.new_id(m.id),
|
|
|
|
span: folder.new_span(m.span),
|
|
|
|
self_id: folder.new_id(m.self_id),
|
|
|
|
vis: m.vis
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-09 07:05:33 -06:00
|
|
|
pub fn noop_fold_pat<T: Folder>(p: @Pat, folder: &mut T) -> @Pat {
|
2014-01-06 06:00:46 -06:00
|
|
|
let node = match p.node {
|
|
|
|
PatWild => PatWild,
|
|
|
|
PatWildMulti => PatWildMulti,
|
|
|
|
PatIdent(binding_mode, ref pth, ref sub) => {
|
|
|
|
PatIdent(binding_mode,
|
|
|
|
folder.fold_path(pth),
|
|
|
|
sub.map(|x| folder.fold_pat(x)))
|
|
|
|
}
|
|
|
|
PatLit(e) => PatLit(folder.fold_expr(e)),
|
|
|
|
PatEnum(ref pth, ref pats) => {
|
|
|
|
PatEnum(folder.fold_path(pth),
|
|
|
|
pats.as_ref().map(|pats| pats.map(|x| folder.fold_pat(*x))))
|
|
|
|
}
|
|
|
|
PatStruct(ref pth, ref fields, etc) => {
|
|
|
|
let pth_ = folder.fold_path(pth);
|
|
|
|
let fs = fields.map(|f| {
|
|
|
|
ast::FieldPat {
|
|
|
|
ident: f.ident,
|
|
|
|
pat: folder.fold_pat(f.pat)
|
|
|
|
}
|
|
|
|
});
|
|
|
|
PatStruct(pth_, fs, etc)
|
|
|
|
}
|
|
|
|
PatTup(ref elts) => PatTup(elts.map(|x| folder.fold_pat(*x))),
|
|
|
|
PatBox(inner) => PatBox(folder.fold_pat(inner)),
|
|
|
|
PatUniq(inner) => PatUniq(folder.fold_pat(inner)),
|
|
|
|
PatRegion(inner) => PatRegion(folder.fold_pat(inner)),
|
|
|
|
PatRange(e1, e2) => {
|
|
|
|
PatRange(folder.fold_expr(e1), folder.fold_expr(e2))
|
|
|
|
},
|
|
|
|
PatVec(ref before, ref slice, ref after) => {
|
|
|
|
PatVec(before.map(|x| folder.fold_pat(*x)),
|
|
|
|
slice.map(|x| folder.fold_pat(x)),
|
|
|
|
after.map(|x| folder.fold_pat(*x)))
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
@Pat {
|
|
|
|
id: folder.new_id(p.id),
|
|
|
|
span: folder.new_span(p.span),
|
|
|
|
node: node,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-09 07:05:33 -06:00
|
|
|
pub fn noop_fold_expr<T: Folder>(e: @Expr, folder: &mut T) -> @Expr {
|
2013-08-29 14:10:02 -05:00
|
|
|
let fold_field = |x| fold_field_(x, folder);
|
2011-07-13 17:44:09 -05:00
|
|
|
|
2013-08-29 14:10:02 -05:00
|
|
|
let node = match e.node {
|
2013-09-01 20:45:37 -05:00
|
|
|
ExprVstore(e, v) => {
|
2013-08-29 14:10:02 -05:00
|
|
|
ExprVstore(folder.fold_expr(e), v)
|
2013-02-18 00:20:36 -06:00
|
|
|
}
|
2013-12-17 18:46:18 -06:00
|
|
|
ExprBox(p, e) => {
|
|
|
|
ExprBox(folder.fold_expr(p), folder.fold_expr(e))
|
|
|
|
}
|
2013-09-01 20:45:37 -05:00
|
|
|
ExprVec(ref exprs, mutt) => {
|
2013-12-27 21:34:51 -06:00
|
|
|
ExprVec(exprs.map(|&x| folder.fold_expr(x)), mutt)
|
2013-02-18 00:20:36 -06:00
|
|
|
}
|
2013-09-01 20:45:37 -05:00
|
|
|
ExprRepeat(expr, count, mutt) => {
|
2013-08-29 14:10:02 -05:00
|
|
|
ExprRepeat(folder.fold_expr(expr), folder.fold_expr(count), mutt)
|
2013-02-18 00:20:36 -06:00
|
|
|
}
|
2013-08-29 14:10:02 -05:00
|
|
|
ExprTup(ref elts) => ExprTup(elts.map(|x| folder.fold_expr(*x))),
|
2013-09-01 20:45:37 -05:00
|
|
|
ExprCall(f, ref args, blk) => {
|
2013-08-29 14:10:02 -05:00
|
|
|
ExprCall(folder.fold_expr(f),
|
2013-12-27 21:34:51 -06:00
|
|
|
args.map(|&x| folder.fold_expr(x)),
|
2013-08-29 14:10:02 -05:00
|
|
|
blk)
|
2013-02-18 00:20:36 -06:00
|
|
|
}
|
2013-09-01 20:45:37 -05:00
|
|
|
ExprMethodCall(callee_id, f, i, ref tps, ref args, blk) => {
|
|
|
|
ExprMethodCall(
|
2013-08-29 14:10:02 -05:00
|
|
|
folder.new_id(callee_id),
|
|
|
|
folder.fold_expr(f),
|
|
|
|
folder.fold_ident(i),
|
2013-11-30 16:00:39 -06:00
|
|
|
tps.map(|&x| folder.fold_ty(x)),
|
2013-12-27 21:34:51 -06:00
|
|
|
args.map(|&x| folder.fold_expr(x)),
|
2013-02-18 00:20:36 -06:00
|
|
|
blk
|
|
|
|
)
|
|
|
|
}
|
2013-09-01 20:45:37 -05:00
|
|
|
ExprBinary(callee_id, binop, lhs, rhs) => {
|
2013-08-29 14:10:02 -05:00
|
|
|
ExprBinary(folder.new_id(callee_id),
|
|
|
|
binop,
|
|
|
|
folder.fold_expr(lhs),
|
|
|
|
folder.fold_expr(rhs))
|
2013-06-01 17:31:56 -05:00
|
|
|
}
|
2013-09-01 20:45:37 -05:00
|
|
|
ExprUnary(callee_id, binop, ohs) => {
|
2013-08-29 14:10:02 -05:00
|
|
|
ExprUnary(folder.new_id(callee_id), binop, folder.fold_expr(ohs))
|
2013-02-18 00:20:36 -06:00
|
|
|
}
|
2013-08-29 14:10:02 -05:00
|
|
|
ExprDoBody(f) => ExprDoBody(folder.fold_expr(f)),
|
|
|
|
ExprLit(_) => e.node.clone(),
|
2013-11-30 16:00:39 -06:00
|
|
|
ExprCast(expr, ty) => {
|
2013-08-29 14:10:02 -05:00
|
|
|
ExprCast(folder.fold_expr(expr), folder.fold_ty(ty))
|
2013-07-02 14:47:32 -05:00
|
|
|
}
|
2013-08-29 14:10:02 -05:00
|
|
|
ExprAddrOf(m, ohs) => ExprAddrOf(m, folder.fold_expr(ohs)),
|
2013-11-30 16:00:39 -06:00
|
|
|
ExprIf(cond, tr, fl) => {
|
2013-08-29 14:10:02 -05:00
|
|
|
ExprIf(folder.fold_expr(cond),
|
|
|
|
folder.fold_block(tr),
|
2013-09-20 01:08:47 -05:00
|
|
|
fl.map(|x| folder.fold_expr(x)))
|
2013-02-18 00:20:36 -06:00
|
|
|
}
|
2013-11-30 16:00:39 -06:00
|
|
|
ExprWhile(cond, body) => {
|
2013-08-29 14:10:02 -05:00
|
|
|
ExprWhile(folder.fold_expr(cond), folder.fold_block(body))
|
2013-02-18 00:20:36 -06:00
|
|
|
}
|
2013-11-30 16:00:39 -06:00
|
|
|
ExprForLoop(pat, iter, body, ref maybe_ident) => {
|
2013-08-29 14:10:02 -05:00
|
|
|
ExprForLoop(folder.fold_pat(pat),
|
|
|
|
folder.fold_expr(iter),
|
|
|
|
folder.fold_block(body),
|
2013-09-20 01:08:47 -05:00
|
|
|
maybe_ident.map(|i| folder.fold_ident(i)))
|
2013-07-29 19:25:00 -05:00
|
|
|
}
|
2013-11-30 16:00:39 -06:00
|
|
|
ExprLoop(body, opt_ident) => {
|
2013-08-29 14:10:02 -05:00
|
|
|
ExprLoop(folder.fold_block(body),
|
2013-09-20 01:08:47 -05:00
|
|
|
opt_ident.map(|x| folder.fold_ident(x)))
|
2013-02-18 00:20:36 -06:00
|
|
|
}
|
2013-09-01 20:45:37 -05:00
|
|
|
ExprMatch(expr, ref arms) => {
|
2013-08-29 14:10:02 -05:00
|
|
|
ExprMatch(folder.fold_expr(expr),
|
|
|
|
arms.map(|x| folder.fold_arm(x)))
|
2013-02-18 00:20:36 -06:00
|
|
|
}
|
2013-11-30 16:00:39 -06:00
|
|
|
ExprFnBlock(decl, body) => {
|
2014-01-06 06:00:46 -06:00
|
|
|
ExprFnBlock(folder.fold_fn_decl(decl), folder.fold_block(body))
|
2013-02-18 00:20:36 -06:00
|
|
|
}
|
2013-11-30 16:00:39 -06:00
|
|
|
ExprProc(decl, body) => {
|
2014-01-06 06:00:46 -06:00
|
|
|
ExprProc(folder.fold_fn_decl(decl), folder.fold_block(body))
|
2013-10-28 17:22:49 -05:00
|
|
|
}
|
2013-11-30 16:00:39 -06:00
|
|
|
ExprBlock(blk) => ExprBlock(folder.fold_block(blk)),
|
2013-09-01 20:45:37 -05:00
|
|
|
ExprAssign(el, er) => {
|
2013-08-29 14:10:02 -05:00
|
|
|
ExprAssign(folder.fold_expr(el), folder.fold_expr(er))
|
2013-02-18 00:20:36 -06:00
|
|
|
}
|
2013-09-01 20:45:37 -05:00
|
|
|
ExprAssignOp(callee_id, op, el, er) => {
|
2013-08-29 14:10:02 -05:00
|
|
|
ExprAssignOp(folder.new_id(callee_id),
|
|
|
|
op,
|
|
|
|
folder.fold_expr(el),
|
|
|
|
folder.fold_expr(er))
|
2013-02-18 00:20:36 -06:00
|
|
|
}
|
2013-09-01 20:45:37 -05:00
|
|
|
ExprField(el, id, ref tys) => {
|
2013-10-28 21:22:42 -05:00
|
|
|
ExprField(folder.fold_expr(el),
|
|
|
|
folder.fold_ident(id),
|
2013-11-30 16:00:39 -06:00
|
|
|
tys.map(|&x| folder.fold_ty(x)))
|
2013-02-18 00:20:36 -06:00
|
|
|
}
|
2013-09-01 20:45:37 -05:00
|
|
|
ExprIndex(callee_id, el, er) => {
|
2013-08-29 14:10:02 -05:00
|
|
|
ExprIndex(folder.new_id(callee_id),
|
|
|
|
folder.fold_expr(el),
|
|
|
|
folder.fold_expr(er))
|
2013-02-18 00:20:36 -06:00
|
|
|
}
|
2013-08-29 14:10:02 -05:00
|
|
|
ExprPath(ref pth) => ExprPath(folder.fold_path(pth)),
|
2013-09-01 20:45:37 -05:00
|
|
|
ExprSelf => ExprSelf,
|
2013-08-29 14:10:02 -05:00
|
|
|
ExprLogLevel => ExprLogLevel,
|
|
|
|
ExprBreak(opt_ident) => ExprBreak(opt_ident),
|
|
|
|
ExprAgain(opt_ident) => ExprAgain(opt_ident),
|
2013-09-01 20:45:37 -05:00
|
|
|
ExprRet(ref e) => {
|
2013-09-20 01:08:47 -05:00
|
|
|
ExprRet(e.map(|x| folder.fold_expr(x)))
|
2013-02-18 00:20:36 -06:00
|
|
|
}
|
2013-09-01 20:45:37 -05:00
|
|
|
ExprInlineAsm(ref a) => {
|
2014-01-09 07:05:33 -06:00
|
|
|
ExprInlineAsm(InlineAsm {
|
2013-08-29 14:10:02 -05:00
|
|
|
inputs: a.inputs.map(|&(c, input)| (c, folder.fold_expr(input))),
|
|
|
|
outputs: a.outputs.map(|&(c, out)| (c, folder.fold_expr(out))),
|
2013-07-02 14:47:32 -05:00
|
|
|
.. (*a).clone()
|
2013-03-27 15:42:21 -05:00
|
|
|
})
|
2013-03-12 19:53:25 -05:00
|
|
|
}
|
2013-08-29 14:10:02 -05:00
|
|
|
ExprMac(ref mac) => ExprMac(folder.fold_mac(mac)),
|
2013-09-01 20:45:37 -05:00
|
|
|
ExprStruct(ref path, ref fields, maybe_expr) => {
|
2013-08-29 14:10:02 -05:00
|
|
|
ExprStruct(folder.fold_path(path),
|
|
|
|
fields.map(|x| fold_field(*x)),
|
2013-09-20 01:08:47 -05:00
|
|
|
maybe_expr.map(|x| folder.fold_expr(x)))
|
2013-02-18 00:20:36 -06:00
|
|
|
},
|
2013-08-29 14:10:02 -05:00
|
|
|
ExprParen(ex) => ExprParen(folder.fold_expr(ex))
|
2013-09-06 21:11:55 -05:00
|
|
|
};
|
2012-01-25 18:23:43 -06:00
|
|
|
|
2013-08-29 14:10:02 -05:00
|
|
|
@Expr {
|
|
|
|
id: folder.new_id(e.id),
|
|
|
|
node: node,
|
|
|
|
span: folder.new_span(e.span),
|
2013-01-14 22:52:28 -06:00
|
|
|
}
|
2011-06-20 19:25:49 -05:00
|
|
|
}
|
|
|
|
|
2014-01-09 07:05:33 -06:00
|
|
|
pub fn noop_fold_stmt<T: Folder>(s: &Stmt, folder: &mut T) -> SmallVector<@Stmt> {
|
2013-11-25 01:08:53 -06:00
|
|
|
let nodes = match s.node {
|
2013-08-29 14:10:02 -05:00
|
|
|
StmtDecl(d, nid) => {
|
2013-11-25 01:08:53 -06:00
|
|
|
folder.fold_decl(d).move_iter()
|
|
|
|
.map(|d| StmtDecl(d, folder.new_id(nid)))
|
|
|
|
.collect()
|
2013-06-04 23:43:41 -05:00
|
|
|
}
|
2013-08-29 14:10:02 -05:00
|
|
|
StmtExpr(e, nid) => {
|
2013-11-25 01:08:53 -06:00
|
|
|
SmallVector::one(StmtExpr(folder.fold_expr(e), folder.new_id(nid)))
|
2013-01-15 15:51:43 -06:00
|
|
|
}
|
2013-08-29 14:10:02 -05:00
|
|
|
StmtSemi(e, nid) => {
|
2013-11-25 01:08:53 -06:00
|
|
|
SmallVector::one(StmtSemi(folder.fold_expr(e), folder.new_id(nid)))
|
2013-01-15 16:59:39 -06:00
|
|
|
}
|
2013-11-25 01:08:53 -06:00
|
|
|
StmtMac(ref mac, semi) => SmallVector::one(StmtMac(folder.fold_mac(mac), semi))
|
2013-08-29 14:10:02 -05:00
|
|
|
};
|
2012-05-21 20:28:39 -05:00
|
|
|
|
2013-11-25 01:08:53 -06:00
|
|
|
nodes.move_iter().map(|node| @Spanned {
|
2013-08-29 14:10:02 -05:00
|
|
|
node: node,
|
|
|
|
span: folder.new_span(s.span),
|
2013-11-25 01:08:53 -06:00
|
|
|
}).collect()
|
2011-06-20 19:25:49 -05:00
|
|
|
}
|
2013-06-06 16:17:00 -05:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use ast;
|
|
|
|
use util::parser_testing::{string_to_crate, matches_codepattern};
|
|
|
|
use parse::token;
|
|
|
|
use print::pprust;
|
|
|
|
use super::*;
|
2013-09-17 01:37:54 -05:00
|
|
|
|
2013-06-06 16:17:00 -05:00
|
|
|
// this version doesn't care about getting comments or docstrings in.
|
2014-01-09 07:05:33 -06:00
|
|
|
fn fake_print_crate(s: &mut pprust::State, crate: &ast::Crate) {
|
2013-07-19 00:38:55 -05:00
|
|
|
pprust::print_mod(s, &crate.module, crate.attrs);
|
2013-06-06 16:17:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// change every identifier to "zz"
|
2013-08-30 20:00:38 -05:00
|
|
|
struct ToZzIdentFolder;
|
|
|
|
|
2014-01-09 07:05:33 -06:00
|
|
|
impl Folder for ToZzIdentFolder {
|
2013-12-27 21:34:51 -06:00
|
|
|
fn fold_ident(&mut self, _: ast::Ident) -> ast::Ident {
|
2013-08-30 20:00:38 -05:00
|
|
|
token::str_to_ident("zz")
|
|
|
|
}
|
2013-06-06 16:17:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// maybe add to expand.rs...
|
|
|
|
macro_rules! assert_pred (
|
|
|
|
($pred:expr, $predname:expr, $a:expr , $b:expr) => (
|
|
|
|
{
|
|
|
|
let pred_val = $pred;
|
|
|
|
let a_val = $a;
|
|
|
|
let b_val = $b;
|
|
|
|
if !(pred_val(a_val,b_val)) {
|
2013-10-21 15:08:31 -05:00
|
|
|
fail!("expected args satisfying {}, got {:?} and {:?}",
|
2013-06-06 16:17:00 -05:00
|
|
|
$predname, a_val, b_val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
// make sure idents get transformed everywhere
|
|
|
|
#[test] fn ident_transformation () {
|
2013-12-27 21:34:51 -06:00
|
|
|
let mut zz_fold = ToZzIdentFolder;
|
2013-06-12 12:02:55 -05:00
|
|
|
let ast = string_to_crate(@"#[a] mod b {fn c (d : e, f : g) {h!(i,j,k);l;m}}");
|
2013-06-06 16:17:00 -05:00
|
|
|
assert_pred!(matches_codepattern,
|
|
|
|
"matches_codepattern",
|
2013-12-27 21:34:51 -06:00
|
|
|
pprust::to_str(&mut zz_fold.fold_crate(ast),fake_print_crate,
|
2013-06-06 16:17:00 -05:00
|
|
|
token::get_ident_interner()),
|
|
|
|
~"#[a]mod zz{fn zz(zz:zz,zz:zz){zz!(zz,zz,zz);zz;zz}}");
|
|
|
|
}
|
|
|
|
|
|
|
|
// even inside macro defs....
|
|
|
|
#[test] fn ident_transformation_in_defs () {
|
2013-12-27 21:34:51 -06:00
|
|
|
let mut zz_fold = ToZzIdentFolder;
|
2013-06-12 12:02:55 -05:00
|
|
|
let ast = string_to_crate(@"macro_rules! a {(b $c:expr $(d $e:token)f+
|
2013-06-06 16:17:00 -05:00
|
|
|
=> (g $(d $d $e)+))} ");
|
|
|
|
assert_pred!(matches_codepattern,
|
|
|
|
"matches_codepattern",
|
2013-12-27 21:34:51 -06:00
|
|
|
pprust::to_str(&mut zz_fold.fold_crate(ast),fake_print_crate,
|
2013-06-06 16:17:00 -05:00
|
|
|
token::get_ident_interner()),
|
|
|
|
~"zz!zz((zz$zz:zz$(zz $zz:zz)zz+=>(zz$(zz$zz$zz)+)))");
|
|
|
|
}
|
|
|
|
}
|
2013-08-29 14:10:02 -05:00
|
|
|
|