2012-12-03 16:48:01 -08: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.
|
|
|
|
|
2014-07-09 14:48:12 -07:00
|
|
|
//! A Folder represents an AST->AST fold; it accepts an AST piece,
|
|
|
|
//! and returns a piece of the same type. So, for instance, macro
|
|
|
|
//! expansion is a Folder that walks over an AST and produces another
|
|
|
|
//! AST.
|
|
|
|
//!
|
|
|
|
//! Note: using a Folder (other than the MacroExpander Folder) on
|
|
|
|
//! an AST before macro expansion is probably a bad idea. For instance,
|
|
|
|
//! a folder renaming item names in a module will miss all of those
|
|
|
|
//! that are created by the expansion of a macro.
|
|
|
|
|
2012-09-04 11:37:29 -07:00
|
|
|
use ast::*;
|
2012-12-23 17:41:37 -05:00
|
|
|
use ast;
|
2014-02-14 07:07:09 +02:00
|
|
|
use ast_util;
|
2013-10-28 19:22:42 -07:00
|
|
|
use codemap::{respan, Span, Spanned};
|
2013-06-06 14:17:00 -07:00
|
|
|
use parse::token;
|
2014-03-20 01:52:37 +11:00
|
|
|
use owned_slice::OwnedSlice;
|
2013-11-24 23:08:53 -08:00
|
|
|
use util::small_vector::SmallVector;
|
2012-12-23 17:41:37 -05:00
|
|
|
|
2014-03-27 16:40:35 +02:00
|
|
|
use std::rc::Rc;
|
2014-06-11 19:33:52 -07:00
|
|
|
use std::gc::{Gc, GC};
|
2014-03-27 16:40:35 +02:00
|
|
|
|
2014-01-09 15:05:33 +02:00
|
|
|
pub trait Folder {
|
2014-07-25 21:00:33 +02:00
|
|
|
// Any additions to this trait should happen in form
|
|
|
|
// of a call to a public `noop_*` function that only calls
|
|
|
|
// out to the folder again, not other `noop_*` functions.
|
|
|
|
//
|
|
|
|
// This is a necessary API workaround to the problem of not
|
|
|
|
// being able to call out to the super default method
|
|
|
|
// in an overridden default method.
|
|
|
|
|
2013-12-27 20:34:51 -07:00
|
|
|
fn fold_crate(&mut self, c: Crate) -> Crate {
|
2013-08-29 12:10:02 -07:00
|
|
|
noop_fold_crate(c, self)
|
2013-09-06 22:11:55 -04:00
|
|
|
}
|
|
|
|
|
2014-05-16 00:16:13 -07:00
|
|
|
fn fold_meta_items(&mut self, meta_items: &[Gc<MetaItem>]) -> Vec<Gc<MetaItem>> {
|
2014-07-25 21:00:33 +02:00
|
|
|
noop_fold_meta_items(meta_items, self)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fold_meta_item(&mut self, meta_item: &MetaItem) -> MetaItem {
|
|
|
|
noop_fold_meta_item(meta_item, self)
|
2013-09-06 22:11:55 -04:00
|
|
|
}
|
|
|
|
|
2014-05-16 00:16:13 -07:00
|
|
|
fn fold_view_path(&mut self, view_path: Gc<ViewPath>) -> Gc<ViewPath> {
|
2014-07-25 21:00:33 +02:00
|
|
|
noop_fold_view_path(view_path, self)
|
2013-09-06 22:11:55 -04:00
|
|
|
}
|
|
|
|
|
2014-01-09 15:05:33 +02:00
|
|
|
fn fold_view_item(&mut self, vi: &ViewItem) -> ViewItem {
|
2013-12-25 11:10:33 -07:00
|
|
|
noop_fold_view_item(vi, self)
|
2013-08-29 12:10:02 -07:00
|
|
|
}
|
|
|
|
|
2014-05-16 00:16:13 -07:00
|
|
|
fn fold_foreign_item(&mut self, ni: Gc<ForeignItem>) -> Gc<ForeignItem> {
|
|
|
|
noop_fold_foreign_item(&*ni, self)
|
2013-09-06 22:11:55 -04:00
|
|
|
}
|
|
|
|
|
2014-05-16 00:16:13 -07:00
|
|
|
fn fold_item(&mut self, i: Gc<Item>) -> SmallVector<Gc<Item>> {
|
|
|
|
noop_fold_item(&*i, self)
|
2013-09-06 22:11:55 -04:00
|
|
|
}
|
|
|
|
|
2014-07-25 21:00:33 +02:00
|
|
|
fn fold_item_simple(&mut self, i: &Item) -> Item {
|
|
|
|
noop_fold_item_simple(i, self)
|
|
|
|
}
|
|
|
|
|
2014-01-09 15:05:33 +02:00
|
|
|
fn fold_struct_field(&mut self, sf: &StructField) -> StructField {
|
2014-07-25 21:00:33 +02:00
|
|
|
noop_fold_struct_field(sf, self)
|
2013-09-06 22:11:55 -04:00
|
|
|
}
|
|
|
|
|
2014-01-09 15:05:33 +02:00
|
|
|
fn fold_item_underscore(&mut self, i: &Item_) -> Item_ {
|
2013-08-29 12:10:02 -07:00
|
|
|
noop_fold_item_underscore(i, self)
|
|
|
|
}
|
|
|
|
|
2014-01-09 15:05:33 +02:00
|
|
|
fn fold_fn_decl(&mut self, d: &FnDecl) -> P<FnDecl> {
|
2014-01-06 14:00:46 +02:00
|
|
|
noop_fold_fn_decl(d, self)
|
|
|
|
}
|
|
|
|
|
2013-12-27 20:34:51 -07:00
|
|
|
fn fold_type_method(&mut self, m: &TypeMethod) -> TypeMethod {
|
2013-08-29 12:10:02 -07:00
|
|
|
noop_fold_type_method(m, self)
|
|
|
|
}
|
|
|
|
|
2014-07-12 22:33:30 -07:00
|
|
|
fn fold_method(&mut self, m: Gc<Method>) -> SmallVector<Gc<Method>> {
|
2014-05-16 00:16:13 -07:00
|
|
|
noop_fold_method(&*m, self)
|
2013-09-06 22:11:55 -04:00
|
|
|
}
|
|
|
|
|
2013-12-27 20:34:51 -07:00
|
|
|
fn fold_block(&mut self, b: P<Block>) -> P<Block> {
|
2013-08-29 12:10:02 -07:00
|
|
|
noop_fold_block(b, self)
|
|
|
|
}
|
|
|
|
|
2014-05-16 00:16:13 -07:00
|
|
|
fn fold_stmt(&mut self, s: &Stmt) -> SmallVector<Gc<Stmt>> {
|
2013-08-29 12:10:02 -07:00
|
|
|
noop_fold_stmt(s, self)
|
|
|
|
}
|
|
|
|
|
2013-12-27 20:34:51 -07:00
|
|
|
fn fold_arm(&mut self, a: &Arm) -> Arm {
|
2014-07-25 21:00:33 +02:00
|
|
|
noop_fold_arm(a, self)
|
2013-09-06 22:11:55 -04:00
|
|
|
}
|
|
|
|
|
2014-05-16 00:16:13 -07:00
|
|
|
fn fold_pat(&mut self, p: Gc<Pat>) -> Gc<Pat> {
|
2014-01-06 14:00:46 +02:00
|
|
|
noop_fold_pat(p, self)
|
2013-09-06 22:11:55 -04:00
|
|
|
}
|
|
|
|
|
2014-05-16 00:16:13 -07:00
|
|
|
fn fold_decl(&mut self, d: Gc<Decl>) -> SmallVector<Gc<Decl>> {
|
2014-07-25 21:00:33 +02:00
|
|
|
noop_fold_decl(d, self)
|
2013-08-29 12:10:02 -07:00
|
|
|
}
|
|
|
|
|
2014-05-16 00:16:13 -07:00
|
|
|
fn fold_expr(&mut self, e: Gc<Expr>) -> Gc<Expr> {
|
2013-08-29 12:10:02 -07:00
|
|
|
noop_fold_expr(e, self)
|
|
|
|
}
|
|
|
|
|
2013-12-27 20:34:51 -07:00
|
|
|
fn fold_ty(&mut self, t: P<Ty>) -> P<Ty> {
|
2014-07-25 21:00:33 +02:00
|
|
|
noop_fold_ty(t, self)
|
2013-09-06 22:11:55 -04:00
|
|
|
}
|
|
|
|
|
2014-01-09 15:05:33 +02:00
|
|
|
fn fold_mod(&mut self, m: &Mod) -> Mod {
|
2013-08-29 12:10:02 -07:00
|
|
|
noop_fold_mod(m, self)
|
2013-09-06 22:11:55 -04:00
|
|
|
}
|
|
|
|
|
2014-01-09 15:05:33 +02:00
|
|
|
fn fold_foreign_mod(&mut self, nm: &ForeignMod) -> ForeignMod {
|
2014-07-25 21:00:33 +02:00
|
|
|
noop_fold_foreign_mod(nm, self)
|
2013-09-06 22:11:55 -04:00
|
|
|
}
|
|
|
|
|
2014-01-09 15:05:33 +02:00
|
|
|
fn fold_variant(&mut self, v: &Variant) -> P<Variant> {
|
2014-07-25 21:00:33 +02:00
|
|
|
noop_fold_variant(v, self)
|
2013-09-06 22:11:55 -04:00
|
|
|
}
|
|
|
|
|
2013-12-27 20:34:51 -07:00
|
|
|
fn fold_ident(&mut self, i: Ident) -> Ident {
|
2014-07-25 21:00:33 +02:00
|
|
|
noop_fold_ident(i, self)
|
2013-08-29 12:10:02 -07:00
|
|
|
}
|
2013-09-06 22:11:55 -04:00
|
|
|
|
2013-12-27 20:34:51 -07:00
|
|
|
fn fold_path(&mut self, p: &Path) -> Path {
|
2014-07-25 21:00:33 +02:00
|
|
|
noop_fold_path(p, self)
|
2013-09-06 22:11:55 -04:00
|
|
|
}
|
|
|
|
|
2014-05-16 00:16:13 -07:00
|
|
|
fn fold_local(&mut self, l: Gc<Local>) -> Gc<Local> {
|
2014-07-25 21:00:33 +02:00
|
|
|
noop_fold_local(l, self)
|
2013-09-06 22:11:55 -04:00
|
|
|
}
|
|
|
|
|
2014-07-09 14:48:12 -07:00
|
|
|
fn fold_mac(&mut self, _macro: &Mac) -> Mac {
|
|
|
|
fail!("fold_mac disabled by default");
|
|
|
|
// NB: see note about macros above.
|
|
|
|
// if you really want a folder that
|
|
|
|
// works on macros, use this
|
|
|
|
// definition in your trait impl:
|
2014-07-25 21:00:33 +02:00
|
|
|
// fold::noop_fold_mac(_macro, self)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fold_explicit_self(&mut self, es: &ExplicitSelf) -> ExplicitSelf {
|
|
|
|
noop_fold_explicit_self(es, self)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fold_explicit_self_underscore(&mut self, es: &ExplicitSelf_) -> ExplicitSelf_ {
|
|
|
|
noop_fold_explicit_self_underscore(es, self)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fold_lifetime(&mut self, l: &Lifetime) -> Lifetime {
|
|
|
|
noop_fold_lifetime(l, self)
|
|
|
|
}
|
|
|
|
|
2014-08-05 22:59:24 -04:00
|
|
|
fn fold_lifetime_def(&mut self, l: &LifetimeDef) -> LifetimeDef {
|
|
|
|
noop_fold_lifetime_def(l, self)
|
|
|
|
}
|
|
|
|
|
2014-07-25 21:00:33 +02:00
|
|
|
fn fold_attribute(&mut self, at: Attribute) -> Attribute {
|
|
|
|
noop_fold_attribute(at, self)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fold_arg(&mut self, a: &Arg) -> Arg {
|
|
|
|
noop_fold_arg(a, self)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fold_generics(&mut self, generics: &Generics) -> Generics {
|
|
|
|
noop_fold_generics(generics, self)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fold_trait_ref(&mut self, p: &TraitRef) -> TraitRef {
|
|
|
|
noop_fold_trait_ref(p, self)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fold_struct_def(&mut self, struct_def: Gc<StructDef>) -> Gc<StructDef> {
|
|
|
|
noop_fold_struct_def(struct_def, self)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fold_lifetimes(&mut self, lts: &[Lifetime]) -> Vec<Lifetime> {
|
|
|
|
noop_fold_lifetimes(lts, self)
|
|
|
|
}
|
|
|
|
|
2014-08-05 22:59:24 -04:00
|
|
|
fn fold_lifetime_defs(&mut self, lts: &[LifetimeDef]) -> Vec<LifetimeDef> {
|
|
|
|
noop_fold_lifetime_defs(lts, self)
|
|
|
|
}
|
|
|
|
|
2014-07-25 21:00:33 +02:00
|
|
|
fn fold_ty_param(&mut self, tp: &TyParam) -> TyParam {
|
|
|
|
noop_fold_ty_param(tp, self)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fold_ty_params(&mut self, tps: &[TyParam]) -> OwnedSlice<TyParam> {
|
|
|
|
noop_fold_ty_params(tps, self)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fold_tt(&mut self, tt: &TokenTree) -> TokenTree {
|
|
|
|
noop_fold_tt(tt, self)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fold_tts(&mut self, tts: &[TokenTree]) -> Vec<TokenTree> {
|
|
|
|
noop_fold_tts(tts, self)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fold_token(&mut self, t: &token::Token) -> token::Token {
|
|
|
|
noop_fold_token(t, self)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fold_interpolated(&mut self, nt : &token::Nonterminal) -> token::Nonterminal {
|
|
|
|
noop_fold_interpolated(nt, self)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fold_opt_lifetime(&mut self, o_lt: &Option<Lifetime>) -> Option<Lifetime> {
|
|
|
|
noop_fold_opt_lifetime(o_lt, self)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fold_variant_arg(&mut self, va: &VariantArg) -> VariantArg {
|
|
|
|
noop_fold_variant_arg(va, self)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fold_opt_bounds(&mut self, b: &Option<OwnedSlice<TyParamBound>>)
|
|
|
|
-> Option<OwnedSlice<TyParamBound>> {
|
|
|
|
noop_fold_opt_bounds(b, self)
|
|
|
|
}
|
|
|
|
|
2014-08-27 21:46:52 -04:00
|
|
|
fn fold_bounds(&mut self, b: &OwnedSlice<TyParamBound>)
|
|
|
|
-> OwnedSlice<TyParamBound> {
|
|
|
|
noop_fold_bounds(b, self)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fold_ty_param_bound(&mut self, tpb: &TyParamBound) -> TyParamBound {
|
|
|
|
noop_fold_ty_param_bound(tpb, self)
|
|
|
|
}
|
|
|
|
|
2014-07-25 21:00:33 +02:00
|
|
|
fn fold_mt(&mut self, mt: &MutTy) -> MutTy {
|
|
|
|
noop_fold_mt(mt, self)
|
2013-09-06 22:11:55 -04:00
|
|
|
}
|
|
|
|
|
2014-07-25 21:00:33 +02:00
|
|
|
fn fold_field(&mut self, field: Field) -> Field {
|
|
|
|
noop_fold_field(field, self)
|
|
|
|
}
|
|
|
|
|
2014-08-11 09:32:26 -07:00
|
|
|
fn fold_where_clause(&mut self, where_clause: &WhereClause)
|
|
|
|
-> WhereClause {
|
|
|
|
noop_fold_where_clause(where_clause, self)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fold_where_predicate(&mut self, where_predicate: &WherePredicate)
|
|
|
|
-> WherePredicate {
|
|
|
|
noop_fold_where_predicate(where_predicate, self)
|
|
|
|
}
|
|
|
|
|
2014-07-25 21:00:33 +02:00
|
|
|
// Helper methods:
|
|
|
|
|
2014-05-16 00:16:13 -07:00
|
|
|
fn map_exprs(&self, f: |Gc<Expr>| -> Gc<Expr>,
|
|
|
|
es: &[Gc<Expr>]) -> Vec<Gc<Expr>> {
|
2014-02-28 12:54:01 -08:00
|
|
|
es.iter().map(|x| f(*x)).collect()
|
2013-08-29 12:10:02 -07:00
|
|
|
}
|
|
|
|
|
2013-12-27 20:34:51 -07:00
|
|
|
fn new_id(&mut self, i: NodeId) -> NodeId {
|
2013-08-29 12:10:02 -07:00
|
|
|
i
|
|
|
|
}
|
|
|
|
|
2013-12-27 20:34:51 -07:00
|
|
|
fn new_span(&mut self, sp: Span) -> Span {
|
2013-08-29 12:10:02 -07:00
|
|
|
sp
|
2013-09-06 22:11:55 -04:00
|
|
|
}
|
2014-07-25 21:00:33 +02:00
|
|
|
}
|
2013-10-29 06:03:32 -04:00
|
|
|
|
2014-07-25 21:00:33 +02:00
|
|
|
pub fn noop_fold_meta_items<T: Folder>(meta_items: &[Gc<MetaItem>], fld: &mut T)
|
|
|
|
-> Vec<Gc<MetaItem>> {
|
|
|
|
meta_items.iter().map(|x| box (GC) fld.fold_meta_item(&**x)).collect()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn noop_fold_view_path<T: Folder>(view_path: Gc<ViewPath>, fld: &mut T) -> Gc<ViewPath> {
|
|
|
|
let inner_view_path = match view_path.node {
|
|
|
|
ViewPathSimple(ref ident, ref path, node_id) => {
|
|
|
|
let id = fld.new_id(node_id);
|
|
|
|
ViewPathSimple(ident.clone(),
|
|
|
|
fld.fold_path(path),
|
|
|
|
id)
|
|
|
|
}
|
|
|
|
ViewPathGlob(ref path, node_id) => {
|
|
|
|
let id = fld.new_id(node_id);
|
|
|
|
ViewPathGlob(fld.fold_path(path), id)
|
2013-10-29 06:03:32 -04:00
|
|
|
}
|
2014-07-25 21:00:33 +02:00
|
|
|
ViewPathList(ref path, ref path_list_idents, node_id) => {
|
|
|
|
let id = fld.new_id(node_id);
|
|
|
|
ViewPathList(fld.fold_path(path),
|
|
|
|
path_list_idents.iter().map(|path_list_ident| {
|
|
|
|
Spanned {
|
|
|
|
node: match path_list_ident.node {
|
|
|
|
PathListIdent { id, name } =>
|
|
|
|
PathListIdent {
|
|
|
|
id: fld.new_id(id),
|
|
|
|
name: name.clone()
|
|
|
|
},
|
|
|
|
PathListMod { id } =>
|
|
|
|
PathListMod { id: fld.new_id(id) }
|
|
|
|
},
|
|
|
|
span: fld.new_span(path_list_ident.span)
|
|
|
|
}
|
|
|
|
}).collect(),
|
|
|
|
id)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
box(GC) Spanned {
|
|
|
|
node: inner_view_path,
|
|
|
|
span: fld.new_span(view_path.span),
|
2013-10-29 06:03:32 -04:00
|
|
|
}
|
2014-07-25 21:00:33 +02:00
|
|
|
}
|
2013-10-29 06:03:32 -04:00
|
|
|
|
2014-07-25 21:00:33 +02:00
|
|
|
pub fn noop_fold_arm<T: Folder>(a: &Arm, fld: &mut T) -> Arm {
|
|
|
|
Arm {
|
|
|
|
attrs: a.attrs.iter().map(|x| fld.fold_attribute(*x)).collect(),
|
|
|
|
pats: a.pats.iter().map(|x| fld.fold_pat(*x)).collect(),
|
|
|
|
guard: a.guard.map(|x| fld.fold_expr(x)),
|
|
|
|
body: fld.fold_expr(a.body),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn noop_fold_decl<T: Folder>(d: Gc<Decl>, fld: &mut T) -> SmallVector<Gc<Decl>> {
|
|
|
|
let node = match d.node {
|
|
|
|
DeclLocal(ref l) => SmallVector::one(DeclLocal(fld.fold_local(*l))),
|
|
|
|
DeclItem(it) => {
|
|
|
|
fld.fold_item(it).move_iter().map(|i| DeclItem(i)).collect()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
node.move_iter().map(|node| {
|
|
|
|
box(GC) Spanned {
|
|
|
|
node: node,
|
|
|
|
span: fld.new_span(d.span),
|
|
|
|
}
|
|
|
|
}).collect()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn noop_fold_ty<T: Folder>(t: P<Ty>, fld: &mut T) -> P<Ty> {
|
|
|
|
let id = fld.new_id(t.id);
|
|
|
|
let node = match t.node {
|
|
|
|
TyNil | TyBot | TyInfer => t.node.clone(),
|
|
|
|
TyBox(ty) => TyBox(fld.fold_ty(ty)),
|
|
|
|
TyUniq(ty) => TyUniq(fld.fold_ty(ty)),
|
|
|
|
TyVec(ty) => TyVec(fld.fold_ty(ty)),
|
|
|
|
TyPtr(ref mt) => TyPtr(fld.fold_mt(mt)),
|
|
|
|
TyRptr(ref region, ref mt) => {
|
|
|
|
TyRptr(fld.fold_opt_lifetime(region), fld.fold_mt(mt))
|
|
|
|
}
|
2014-08-27 21:46:52 -04:00
|
|
|
TyClosure(ref f) => {
|
2014-07-25 21:00:33 +02:00
|
|
|
TyClosure(box(GC) ClosureTy {
|
|
|
|
fn_style: f.fn_style,
|
|
|
|
onceness: f.onceness,
|
2014-08-27 21:46:52 -04:00
|
|
|
bounds: fld.fold_bounds(&f.bounds),
|
2014-07-25 21:00:33 +02:00
|
|
|
decl: fld.fold_fn_decl(&*f.decl),
|
2014-08-05 22:59:24 -04:00
|
|
|
lifetimes: fld.fold_lifetime_defs(f.lifetimes.as_slice()),
|
2014-08-27 21:46:52 -04:00
|
|
|
})
|
2014-07-25 21:00:33 +02:00
|
|
|
}
|
|
|
|
TyProc(ref f) => {
|
|
|
|
TyProc(box(GC) ClosureTy {
|
|
|
|
fn_style: f.fn_style,
|
|
|
|
onceness: f.onceness,
|
2014-08-27 21:46:52 -04:00
|
|
|
bounds: fld.fold_bounds(&f.bounds),
|
2014-07-25 21:00:33 +02:00
|
|
|
decl: fld.fold_fn_decl(&*f.decl),
|
2014-08-05 22:59:24 -04:00
|
|
|
lifetimes: fld.fold_lifetime_defs(f.lifetimes.as_slice()),
|
2014-07-25 21:00:33 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
TyBareFn(ref f) => {
|
|
|
|
TyBareFn(box(GC) BareFnTy {
|
2014-08-05 22:59:24 -04:00
|
|
|
lifetimes: fld.fold_lifetime_defs(f.lifetimes.as_slice()),
|
2014-07-25 21:00:33 +02:00
|
|
|
fn_style: f.fn_style,
|
|
|
|
abi: f.abi,
|
|
|
|
decl: fld.fold_fn_decl(&*f.decl)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
TyUnboxedFn(ref f) => {
|
|
|
|
TyUnboxedFn(box(GC) UnboxedFnTy {
|
|
|
|
decl: fld.fold_fn_decl(&*f.decl),
|
2014-07-29 22:08:39 -07:00
|
|
|
kind: f.kind,
|
2014-07-25 21:00:33 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
TyTup(ref tys) => TyTup(tys.iter().map(|&ty| fld.fold_ty(ty)).collect()),
|
|
|
|
TyParen(ref ty) => TyParen(fld.fold_ty(*ty)),
|
|
|
|
TyPath(ref path, ref bounds, id) => {
|
|
|
|
let id = fld.new_id(id);
|
|
|
|
TyPath(fld.fold_path(path),
|
|
|
|
fld.fold_opt_bounds(bounds),
|
|
|
|
id)
|
|
|
|
}
|
|
|
|
TyFixedLengthVec(ty, e) => {
|
|
|
|
TyFixedLengthVec(fld.fold_ty(ty), fld.fold_expr(e))
|
|
|
|
}
|
|
|
|
TyTypeof(expr) => TyTypeof(fld.fold_expr(expr)),
|
|
|
|
};
|
|
|
|
P(Ty {
|
|
|
|
id: id,
|
|
|
|
span: fld.new_span(t.span),
|
|
|
|
node: node,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn noop_fold_foreign_mod<T: Folder>(nm: &ForeignMod, fld: &mut T) -> ForeignMod {
|
|
|
|
ast::ForeignMod {
|
|
|
|
abi: nm.abi,
|
|
|
|
view_items: nm.view_items
|
|
|
|
.iter()
|
|
|
|
.map(|x| fld.fold_view_item(x))
|
|
|
|
.collect(),
|
|
|
|
items: nm.items
|
|
|
|
.iter()
|
|
|
|
.map(|x| fld.fold_foreign_item(*x))
|
|
|
|
.collect(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn noop_fold_variant<T: Folder>(v: &Variant, fld: &mut T) -> P<Variant> {
|
|
|
|
let id = fld.new_id(v.node.id);
|
|
|
|
let kind;
|
|
|
|
match v.node.kind {
|
|
|
|
TupleVariantKind(ref variant_args) => {
|
|
|
|
kind = TupleVariantKind(variant_args.iter().map(|x|
|
|
|
|
fld.fold_variant_arg(x)).collect())
|
|
|
|
}
|
|
|
|
StructVariantKind(ref struct_def) => {
|
|
|
|
kind = StructVariantKind(box(GC) ast::StructDef {
|
|
|
|
fields: struct_def.fields.iter()
|
|
|
|
.map(|f| fld.fold_struct_field(f)).collect(),
|
|
|
|
ctor_id: struct_def.ctor_id.map(|c| fld.new_id(c)),
|
|
|
|
super_struct: match struct_def.super_struct {
|
|
|
|
Some(t) => Some(fld.fold_ty(t)),
|
|
|
|
None => None
|
|
|
|
},
|
|
|
|
is_virtual: struct_def.is_virtual,
|
|
|
|
})
|
2013-10-29 06:03:32 -04:00
|
|
|
}
|
|
|
|
}
|
2014-04-11 11:28:43 +02:00
|
|
|
|
2014-07-25 21:00:33 +02:00
|
|
|
let attrs = v.node.attrs.iter().map(|x| fld.fold_attribute(*x)).collect();
|
|
|
|
|
|
|
|
let de = match v.node.disr_expr {
|
|
|
|
Some(e) => Some(fld.fold_expr(e)),
|
|
|
|
None => None
|
|
|
|
};
|
|
|
|
let node = ast::Variant_ {
|
|
|
|
name: v.node.name,
|
|
|
|
attrs: attrs,
|
|
|
|
kind: kind,
|
|
|
|
id: id,
|
|
|
|
disr_expr: de,
|
|
|
|
vis: v.node.vis,
|
|
|
|
};
|
|
|
|
P(Spanned {
|
|
|
|
node: node,
|
|
|
|
span: fld.new_span(v.span),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn noop_fold_ident<T: Folder>(i: Ident, _: &mut T) -> Ident {
|
|
|
|
i
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn noop_fold_path<T: Folder>(p: &Path, fld: &mut T) -> Path {
|
|
|
|
ast::Path {
|
|
|
|
span: fld.new_span(p.span),
|
|
|
|
global: p.global,
|
|
|
|
segments: p.segments.iter().map(|segment| ast::PathSegment {
|
|
|
|
identifier: fld.fold_ident(segment.identifier),
|
|
|
|
lifetimes: segment.lifetimes.iter().map(|l| fld.fold_lifetime(l)).collect(),
|
|
|
|
types: segment.types.iter().map(|&typ| fld.fold_ty(typ)).collect(),
|
|
|
|
}).collect()
|
2014-04-11 11:28:43 +02:00
|
|
|
}
|
2014-07-25 21:00:33 +02:00
|
|
|
}
|
2014-06-25 18:15:07 -07:00
|
|
|
|
2014-07-25 21:00:33 +02:00
|
|
|
pub fn noop_fold_local<T: Folder>(l: Gc<Local>, fld: &mut T) -> Gc<Local> {
|
|
|
|
let id = fld.new_id(l.id); // Needs to be first, for ast_map.
|
|
|
|
box(GC) Local {
|
|
|
|
id: id,
|
|
|
|
ty: fld.fold_ty(l.ty),
|
|
|
|
pat: fld.fold_pat(l.pat),
|
|
|
|
init: l.init.map(|e| fld.fold_expr(e)),
|
|
|
|
span: fld.new_span(l.span),
|
|
|
|
source: l.source,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn noop_fold_attribute<T: Folder>(at: Attribute, fld: &mut T) -> Attribute {
|
|
|
|
Spanned {
|
|
|
|
span: fld.new_span(at.span),
|
|
|
|
node: ast::Attribute_ {
|
|
|
|
id: at.node.id,
|
|
|
|
style: at.node.style,
|
|
|
|
value: box (GC) fld.fold_meta_item(&*at.node.value),
|
|
|
|
is_sugared_doc: at.node.is_sugared_doc
|
2014-06-25 18:15:07 -07:00
|
|
|
}
|
|
|
|
}
|
2014-07-25 21:00:33 +02:00
|
|
|
}
|
2014-06-25 18:15:07 -07:00
|
|
|
|
2014-07-25 21:00:33 +02:00
|
|
|
pub fn noop_fold_explicit_self_underscore<T: Folder>(es: &ExplicitSelf_, fld: &mut T)
|
|
|
|
-> ExplicitSelf_ {
|
|
|
|
match *es {
|
|
|
|
SelfStatic | SelfValue(_) => *es,
|
|
|
|
SelfRegion(ref lifetime, m, id) => {
|
|
|
|
SelfRegion(fld.fold_opt_lifetime(lifetime), m, id)
|
|
|
|
}
|
|
|
|
SelfExplicit(ref typ, id) => SelfExplicit(fld.fold_ty(*typ), id),
|
|
|
|
}
|
|
|
|
}
|
2014-06-25 18:15:07 -07:00
|
|
|
|
2014-07-25 21:00:33 +02:00
|
|
|
pub fn noop_fold_explicit_self<T: Folder>(es: &ExplicitSelf, fld: &mut T) -> ExplicitSelf {
|
|
|
|
Spanned {
|
|
|
|
span: fld.new_span(es.span),
|
|
|
|
node: fld.fold_explicit_self_underscore(&es.node)
|
|
|
|
}
|
2012-05-21 18:28:39 -07:00
|
|
|
}
|
2011-06-20 17:25:49 -07:00
|
|
|
|
2014-07-09 14:48:12 -07:00
|
|
|
|
2014-07-25 21:00:33 +02:00
|
|
|
pub fn noop_fold_mac<T: Folder>(macro: &Mac, fld: &mut T) -> Mac {
|
2014-07-09 14:48:12 -07:00
|
|
|
Spanned {
|
|
|
|
node: match macro.node {
|
|
|
|
MacInvocTT(ref p, ref tts, ctxt) => {
|
|
|
|
MacInvocTT(fld.fold_path(p),
|
2014-07-25 21:00:33 +02:00
|
|
|
fld.fold_tts(tts.as_slice()),
|
2014-07-09 14:48:12 -07:00
|
|
|
ctxt)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
span: fld.new_span(macro.span)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-25 21:00:33 +02:00
|
|
|
pub fn noop_fold_meta_item<T: Folder>(mi: &MetaItem, fld: &mut T) -> MetaItem {
|
|
|
|
Spanned {
|
2013-08-29 12:10:02 -07:00
|
|
|
node:
|
|
|
|
match mi.node {
|
2014-01-08 10:35:15 -08:00
|
|
|
MetaWord(ref id) => MetaWord((*id).clone()),
|
|
|
|
MetaList(ref id, ref mis) => {
|
2014-07-25 21:00:33 +02:00
|
|
|
MetaList((*id).clone(),
|
|
|
|
mis.iter()
|
|
|
|
.map(|e| box (GC) fld.fold_meta_item(&**e)).collect())
|
2013-08-29 12:10:02 -07:00
|
|
|
}
|
2014-01-10 14:02:36 -08:00
|
|
|
MetaNameValue(ref id, ref s) => {
|
|
|
|
MetaNameValue((*id).clone(), (*s).clone())
|
|
|
|
}
|
2013-08-29 12:10:02 -07:00
|
|
|
},
|
|
|
|
span: fld.new_span(mi.span) }
|
2013-01-08 14:00:45 -08:00
|
|
|
}
|
|
|
|
|
2014-07-25 21:00:33 +02:00
|
|
|
pub fn noop_fold_arg<T: Folder>(a: &Arg, fld: &mut T) -> Arg {
|
2014-04-11 11:28:43 +02:00
|
|
|
let id = fld.new_id(a.id); // Needs to be first, for ast_map.
|
2014-01-09 15:05:33 +02:00
|
|
|
Arg {
|
2014-04-11 11:28:43 +02:00
|
|
|
id: id,
|
2013-12-01 00:00:39 +02:00
|
|
|
ty: fld.fold_ty(a.ty),
|
2013-08-29 12:10:02 -07:00
|
|
|
pat: fld.fold_pat(a.pat),
|
|
|
|
}
|
|
|
|
}
|
2011-06-20 17:25:49 -07:00
|
|
|
|
2014-07-25 21:00:33 +02:00
|
|
|
pub fn noop_fold_tt<T: Folder>(tt: &TokenTree, fld: &mut T) -> TokenTree {
|
2014-06-27 16:10:23 -07:00
|
|
|
match *tt {
|
|
|
|
TTTok(span, ref tok) =>
|
2014-07-25 21:00:33 +02:00
|
|
|
TTTok(span, fld.fold_token(tok)),
|
|
|
|
TTDelim(ref tts) => TTDelim(Rc::new(fld.fold_tts(tts.as_slice()))),
|
2014-06-27 16:10:23 -07:00
|
|
|
TTSeq(span, ref pattern, ref sep, is_optional) =>
|
2014-01-09 15:05:33 +02:00
|
|
|
TTSeq(span,
|
2014-07-25 21:00:33 +02:00
|
|
|
Rc::new(fld.fold_tts(pattern.as_slice())),
|
|
|
|
sep.as_ref().map(|tok| fld.fold_token(tok)),
|
2014-01-09 15:05:33 +02:00
|
|
|
is_optional),
|
2014-06-27 16:10:23 -07:00
|
|
|
TTNonterminal(sp,ref ident) =>
|
2014-01-09 15:05:33 +02:00
|
|
|
TTNonterminal(sp,fld.fold_ident(*ident))
|
2014-06-27 16:10:23 -07:00
|
|
|
}
|
2013-06-06 14:17:18 -07:00
|
|
|
}
|
|
|
|
|
2014-07-25 21:00:33 +02:00
|
|
|
pub fn noop_fold_tts<T: Folder>(tts: &[TokenTree], fld: &mut T) -> Vec<TokenTree> {
|
|
|
|
tts.iter().map(|tt| fld.fold_tt(tt)).collect()
|
2014-06-27 16:10:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// apply ident folder if it's an ident, apply other folds to interpolated nodes
|
2014-07-25 21:00:33 +02:00
|
|
|
pub fn noop_fold_token<T: Folder>(t: &token::Token, fld: &mut T) -> token::Token {
|
2013-06-06 14:17:18 -07:00
|
|
|
match *t {
|
2013-08-29 12:10:02 -07:00
|
|
|
token::IDENT(id, followed_by_colons) => {
|
|
|
|
token::IDENT(fld.fold_ident(id), followed_by_colons)
|
|
|
|
}
|
2014-02-15 16:54:32 +08:00
|
|
|
token::LIFETIME(id) => token::LIFETIME(fld.fold_ident(id)),
|
2014-07-25 21:00:33 +02:00
|
|
|
token::INTERPOLATED(ref nt) => token::INTERPOLATED(fld.fold_interpolated(nt)),
|
2013-07-02 12:47:32 -07:00
|
|
|
_ => (*t).clone()
|
2013-02-17 22:20:36 -08:00
|
|
|
}
|
2011-07-08 16:35:09 -07:00
|
|
|
}
|
|
|
|
|
2014-07-25 21:00:33 +02:00
|
|
|
/// apply folder to elements of interpolated nodes
|
2014-06-27 16:10:23 -07:00
|
|
|
//
|
|
|
|
// NB: this can occur only when applying a fold to partially expanded code, where
|
|
|
|
// parsed pieces have gotten implanted ito *other* macro invocations. This is relevant
|
|
|
|
// for macro hygiene, but possibly not elsewhere.
|
|
|
|
//
|
|
|
|
// One problem here occurs because the types for fold_item, fold_stmt, etc. allow the
|
|
|
|
// folder to return *multiple* items; this is a problem for the nodes here, because
|
|
|
|
// they insist on having exactly one piece. One solution would be to mangle the fold
|
|
|
|
// trait to include one-to-many and one-to-one versions of these entry points, but that
|
|
|
|
// would probably confuse a lot of people and help very few. Instead, I'm just going
|
|
|
|
// to put in dynamic checks. I think the performance impact of this will be pretty much
|
|
|
|
// nonexistent. The danger is that someone will apply a fold to a partially expanded
|
|
|
|
// node, and will be confused by the fact that their "fold_item" or "fold_stmt" isn't
|
|
|
|
// getting called on NtItem or NtStmt nodes. Hopefully they'll wind up reading this
|
|
|
|
// comment, and doing something appropriate.
|
|
|
|
//
|
|
|
|
// BTW, design choice: I considered just changing the type of, e.g., NtItem to contain
|
|
|
|
// multiple items, but decided against it when I looked at parse_item_or_view_item and
|
|
|
|
// tried to figure out what I would do with multiple items there....
|
2014-07-25 21:00:33 +02:00
|
|
|
pub fn noop_fold_interpolated<T: Folder>(nt : &token::Nonterminal, fld: &mut T)
|
|
|
|
-> token::Nonterminal {
|
2014-06-27 16:10:23 -07:00
|
|
|
match *nt {
|
|
|
|
token::NtItem(item) =>
|
|
|
|
token::NtItem(fld.fold_item(item)
|
2014-07-12 22:33:30 -07:00
|
|
|
// this is probably okay, because the only folds likely
|
|
|
|
// to peek inside interpolated nodes will be renamings/markings,
|
|
|
|
// which map single items to single items
|
2014-06-27 16:10:23 -07:00
|
|
|
.expect_one("expected fold to produce exactly one item")),
|
|
|
|
token::NtBlock(block) => token::NtBlock(fld.fold_block(block)),
|
|
|
|
token::NtStmt(stmt) =>
|
2014-07-07 16:35:15 -07:00
|
|
|
token::NtStmt(fld.fold_stmt(&*stmt)
|
2014-07-12 22:33:30 -07:00
|
|
|
// this is probably okay, because the only folds likely
|
|
|
|
// to peek inside interpolated nodes will be renamings/markings,
|
|
|
|
// which map single items to single items
|
2014-06-27 16:10:23 -07:00
|
|
|
.expect_one("expected fold to produce exactly one statement")),
|
|
|
|
token::NtPat(pat) => token::NtPat(fld.fold_pat(pat)),
|
|
|
|
token::NtExpr(expr) => token::NtExpr(fld.fold_expr(expr)),
|
|
|
|
token::NtTy(ty) => token::NtTy(fld.fold_ty(ty)),
|
|
|
|
token::NtIdent(ref id, is_mod_name) =>
|
|
|
|
token::NtIdent(box fld.fold_ident(**id),is_mod_name),
|
2014-07-25 21:00:33 +02:00
|
|
|
token::NtMeta(meta_item) => token::NtMeta(box (GC) fld.fold_meta_item(&*meta_item)),
|
2014-07-07 16:35:15 -07:00
|
|
|
token::NtPath(ref path) => token::NtPath(box fld.fold_path(&**path)),
|
2014-07-25 21:00:33 +02:00
|
|
|
token::NtTT(tt) => token::NtTT(box (GC) fld.fold_tt(&*tt)),
|
2014-06-27 16:10:23 -07:00
|
|
|
// it looks to me like we can leave out the matchers: token::NtMatchers(matchers)
|
|
|
|
_ => (*nt).clone()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-09 15:05:33 +02:00
|
|
|
pub fn noop_fold_fn_decl<T: Folder>(decl: &FnDecl, fld: &mut T) -> P<FnDecl> {
|
|
|
|
P(FnDecl {
|
2014-07-25 21:00:33 +02:00
|
|
|
inputs: decl.inputs.iter().map(|x| fld.fold_arg(x)).collect(), // bad copy
|
2013-12-01 00:00:39 +02:00
|
|
|
output: fld.fold_ty(decl.output),
|
2013-01-15 16:05:20 -08:00
|
|
|
cf: decl.cf,
|
2013-10-25 01:56:34 -04:00
|
|
|
variadic: decl.variadic
|
2013-12-01 00:00:39 +02:00
|
|
|
})
|
2011-12-20 11:03:21 -08:00
|
|
|
}
|
2011-06-20 17:25:49 -07:00
|
|
|
|
2014-07-25 21:00:33 +02:00
|
|
|
pub fn noop_fold_ty_param_bound<T: Folder>(tpb: &TyParamBound, fld: &mut T)
|
|
|
|
-> TyParamBound {
|
2013-08-29 12:10:02 -07:00
|
|
|
match *tpb {
|
2014-07-25 21:00:33 +02:00
|
|
|
TraitTyParamBound(ref ty) => TraitTyParamBound(fld.fold_trait_ref(ty)),
|
2014-08-27 21:46:52 -04:00
|
|
|
RegionTyParamBound(ref lifetime) => RegionTyParamBound(fld.fold_lifetime(lifetime)),
|
2014-06-01 18:41:46 -07:00
|
|
|
UnboxedFnTyParamBound(ref unboxed_function_type) => {
|
|
|
|
UnboxedFnTyParamBound(UnboxedFnTy {
|
2014-05-16 10:15:33 -07:00
|
|
|
decl: fld.fold_fn_decl(&*unboxed_function_type.decl),
|
2014-07-29 22:08:39 -07:00
|
|
|
kind: unboxed_function_type.kind,
|
2014-06-01 18:41:46 -07:00
|
|
|
})
|
|
|
|
}
|
2013-08-29 12:10:02 -07:00
|
|
|
}
|
2012-02-14 15:21:53 -08:00
|
|
|
}
|
|
|
|
|
2014-07-25 21:00:33 +02:00
|
|
|
pub fn noop_fold_ty_param<T: Folder>(tp: &TyParam, fld: &mut T) -> TyParam {
|
2014-04-11 11:28:43 +02:00
|
|
|
let id = fld.new_id(tp.id);
|
2013-08-29 12:10:02 -07:00
|
|
|
TyParam {
|
|
|
|
ident: tp.ident,
|
2014-04-11 11:28:43 +02:00
|
|
|
id: id,
|
2014-08-27 21:46:52 -04:00
|
|
|
bounds: fld.fold_bounds(&tp.bounds),
|
2014-07-25 21:00:33 +02:00
|
|
|
unbound: tp.unbound.as_ref().map(|x| fld.fold_ty_param_bound(x)),
|
2014-04-03 13:53:57 +13:00
|
|
|
default: tp.default.map(|x| fld.fold_ty(x)),
|
|
|
|
span: tp.span
|
2013-08-29 12:10:02 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-25 21:00:33 +02:00
|
|
|
pub fn noop_fold_ty_params<T: Folder>(tps: &[TyParam], fld: &mut T)
|
|
|
|
-> OwnedSlice<TyParam> {
|
|
|
|
tps.iter().map(|tp| fld.fold_ty_param(tp)).collect()
|
2013-08-29 12:10:02 -07:00
|
|
|
}
|
|
|
|
|
2014-04-11 11:28:43 +02:00
|
|
|
pub fn noop_fold_lifetime<T: Folder>(l: &Lifetime, fld: &mut T) -> Lifetime {
|
|
|
|
let id = fld.new_id(l.id);
|
2013-08-29 12:10:02 -07:00
|
|
|
Lifetime {
|
2014-04-11 11:28:43 +02:00
|
|
|
id: id,
|
2013-08-29 12:10:02 -07:00
|
|
|
span: fld.new_span(l.span),
|
2014-03-07 03:10:52 +01:00
|
|
|
name: l.name
|
2013-08-29 12:10:02 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-05 22:59:24 -04:00
|
|
|
pub fn noop_fold_lifetime_def<T: Folder>(l: &LifetimeDef, fld: &mut T)
|
|
|
|
-> LifetimeDef
|
|
|
|
{
|
|
|
|
LifetimeDef {
|
|
|
|
lifetime: fld.fold_lifetime(&l.lifetime),
|
|
|
|
bounds: fld.fold_lifetimes(l.bounds.as_slice()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-25 21:00:33 +02:00
|
|
|
pub fn noop_fold_lifetimes<T: Folder>(lts: &[Lifetime], fld: &mut T) -> Vec<Lifetime> {
|
2014-04-11 11:28:43 +02:00
|
|
|
lts.iter().map(|l| fld.fold_lifetime(l)).collect()
|
2013-08-29 12:10:02 -07:00
|
|
|
}
|
|
|
|
|
2014-08-05 22:59:24 -04:00
|
|
|
pub fn noop_fold_lifetime_defs<T: Folder>(lts: &[LifetimeDef], fld: &mut T) -> Vec<LifetimeDef> {
|
|
|
|
lts.iter().map(|l| fld.fold_lifetime_def(l)).collect()
|
|
|
|
}
|
|
|
|
|
2014-07-25 21:00:33 +02:00
|
|
|
pub fn noop_fold_opt_lifetime<T: Folder>(o_lt: &Option<Lifetime>, fld: &mut T)
|
2014-01-06 14:00:46 +02:00
|
|
|
-> Option<Lifetime> {
|
2014-04-11 11:28:43 +02:00
|
|
|
o_lt.as_ref().map(|lt| fld.fold_lifetime(lt))
|
2013-10-29 06:03:32 -04:00
|
|
|
}
|
|
|
|
|
2014-07-25 21:00:33 +02:00
|
|
|
pub fn noop_fold_generics<T: Folder>(generics: &Generics, fld: &mut T) -> Generics {
|
2014-08-11 09:32:26 -07:00
|
|
|
Generics {
|
|
|
|
ty_params: fld.fold_ty_params(generics.ty_params.as_slice()),
|
|
|
|
lifetimes: fld.fold_lifetime_defs(generics.lifetimes.as_slice()),
|
|
|
|
where_clause: fld.fold_where_clause(&generics.where_clause),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn noop_fold_where_clause<T: Folder>(
|
|
|
|
where_clause: &WhereClause,
|
|
|
|
fld: &mut T)
|
|
|
|
-> WhereClause {
|
|
|
|
WhereClause {
|
|
|
|
id: fld.new_id(where_clause.id),
|
|
|
|
predicates: where_clause.predicates.iter().map(|predicate| {
|
|
|
|
fld.fold_where_predicate(predicate)
|
|
|
|
}).collect(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn noop_fold_where_predicate<T: Folder>(
|
|
|
|
predicate: &WherePredicate,
|
|
|
|
fld: &mut T)
|
|
|
|
-> WherePredicate {
|
|
|
|
WherePredicate {
|
|
|
|
id: fld.new_id(predicate.id),
|
|
|
|
span: fld.new_span(predicate.span),
|
|
|
|
ident: fld.fold_ident(predicate.ident),
|
|
|
|
bounds: predicate.bounds.map(|x| {
|
|
|
|
fld.fold_ty_param_bound(x)
|
|
|
|
}),
|
|
|
|
}
|
2013-08-29 12:10:02 -07:00
|
|
|
}
|
|
|
|
|
2014-07-25 21:00:33 +02:00
|
|
|
pub fn noop_fold_struct_def<T: Folder>(struct_def: Gc<StructDef>,
|
2014-05-16 00:16:13 -07:00
|
|
|
fld: &mut T) -> Gc<StructDef> {
|
|
|
|
box(GC) ast::StructDef {
|
2014-07-25 21:00:33 +02:00
|
|
|
fields: struct_def.fields.iter().map(|f| fld.fold_struct_field(f)).collect(),
|
2013-09-20 02:08:47 -04:00
|
|
|
ctor_id: struct_def.ctor_id.map(|cid| fld.new_id(cid)),
|
2014-02-24 20:17:02 +13:00
|
|
|
super_struct: match struct_def.super_struct {
|
|
|
|
Some(t) => Some(fld.fold_ty(t)),
|
|
|
|
None => None
|
|
|
|
},
|
|
|
|
is_virtual: struct_def.is_virtual,
|
2013-01-14 19:06:59 -08:00
|
|
|
}
|
2011-06-20 17:25:49 -07:00
|
|
|
}
|
|
|
|
|
2014-07-25 21:00:33 +02:00
|
|
|
pub fn noop_fold_trait_ref<T: Folder>(p: &TraitRef, fld: &mut T) -> TraitRef {
|
2014-04-11 11:28:43 +02:00
|
|
|
let id = fld.new_id(p.ref_id);
|
2014-01-09 15:05:33 +02:00
|
|
|
ast::TraitRef {
|
2013-08-29 12:10:02 -07:00
|
|
|
path: fld.fold_path(&p.path),
|
2014-04-11 11:28:43 +02:00
|
|
|
ref_id: id,
|
2013-01-13 12:02:16 -08:00
|
|
|
}
|
2011-06-20 17:25:49 -07:00
|
|
|
}
|
|
|
|
|
2014-07-25 21:00:33 +02:00
|
|
|
pub fn noop_fold_struct_field<T: Folder>(f: &StructField, fld: &mut T) -> StructField {
|
2014-04-11 11:28:43 +02:00
|
|
|
let id = fld.new_id(f.node.id);
|
2013-11-30 17:27:25 +01:00
|
|
|
Spanned {
|
2014-01-09 15:05:33 +02:00
|
|
|
node: ast::StructField_ {
|
2013-08-29 12:10:02 -07:00
|
|
|
kind: f.node.kind,
|
2014-04-11 11:28:43 +02:00
|
|
|
id: id,
|
2013-12-01 00:00:39 +02:00
|
|
|
ty: fld.fold_ty(f.node.ty),
|
2014-06-25 18:15:07 -07:00
|
|
|
attrs: f.node.attrs.iter().map(|a| fld.fold_attribute(*a)).collect(),
|
2013-06-27 17:41:35 -07:00
|
|
|
},
|
2013-08-29 12:10:02 -07:00
|
|
|
span: fld.new_span(f.span),
|
2013-06-27 17:41:35 -07:00
|
|
|
}
|
2012-01-31 19:30:40 -08:00
|
|
|
}
|
|
|
|
|
2014-07-25 21:00:33 +02:00
|
|
|
pub fn noop_fold_field<T: Folder>(field: Field, folder: &mut T) -> Field {
|
2013-08-29 12:10:02 -07:00
|
|
|
ast::Field {
|
2013-10-28 19:22:42 -07:00
|
|
|
ident: respan(field.ident.span, folder.fold_ident(field.ident.node)),
|
2013-08-29 12:10:02 -07:00
|
|
|
expr: folder.fold_expr(field.expr),
|
|
|
|
span: folder.new_span(field.span),
|
2013-09-06 22:11:55 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-25 21:00:33 +02:00
|
|
|
pub fn noop_fold_mt<T: Folder>(mt: &MutTy, folder: &mut T) -> MutTy {
|
2014-01-09 15:05:33 +02:00
|
|
|
MutTy {
|
2013-12-01 00:00:39 +02:00
|
|
|
ty: folder.fold_ty(mt.ty),
|
2013-08-29 12:10:02 -07:00
|
|
|
mutbl: mt.mutbl,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-25 21:00:33 +02:00
|
|
|
pub fn noop_fold_opt_bounds<T: Folder>(b: &Option<OwnedSlice<TyParamBound>>, folder: &mut T)
|
2014-03-20 01:52:37 +11:00
|
|
|
-> Option<OwnedSlice<TyParamBound>> {
|
2014-08-27 21:46:52 -04:00
|
|
|
b.as_ref().map(|bounds| folder.fold_bounds(bounds))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn noop_fold_bounds<T: Folder>(bounds: &TyParamBounds, folder: &mut T)
|
|
|
|
-> TyParamBounds {
|
|
|
|
bounds.map(|bound| folder.fold_ty_param_bound(bound))
|
2013-08-29 12:10:02 -07:00
|
|
|
}
|
|
|
|
|
2014-07-25 21:00:33 +02:00
|
|
|
pub fn noop_fold_variant_arg<T: Folder>(va: &VariantArg, folder: &mut T) -> VariantArg {
|
2014-04-11 11:28:43 +02:00
|
|
|
let id = folder.new_id(va.id);
|
2014-01-09 15:05:33 +02:00
|
|
|
ast::VariantArg {
|
2013-12-01 00:00:39 +02:00
|
|
|
ty: folder.fold_ty(va.ty),
|
2014-04-11 11:28:43 +02:00
|
|
|
id: id,
|
2013-08-29 12:10:02 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-25 11:10:33 -07:00
|
|
|
pub fn noop_fold_view_item<T: Folder>(vi: &ViewItem, folder: &mut T)
|
|
|
|
-> ViewItem{
|
|
|
|
let inner_view_item = match vi.node {
|
2014-03-07 15:57:45 +08:00
|
|
|
ViewItemExternCrate(ref ident, ref string, node_id) => {
|
|
|
|
ViewItemExternCrate(ident.clone(),
|
2014-01-21 10:08:10 -08:00
|
|
|
(*string).clone(),
|
|
|
|
folder.new_id(node_id))
|
2013-12-25 11:10:33 -07:00
|
|
|
}
|
2014-04-26 22:33:45 +09:00
|
|
|
ViewItemUse(ref view_path) => {
|
|
|
|
ViewItemUse(folder.fold_view_path(*view_path))
|
2013-12-25 11:10:33 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
ViewItem {
|
|
|
|
node: inner_view_item,
|
2014-06-25 18:15:07 -07:00
|
|
|
attrs: vi.attrs.iter().map(|a| folder.fold_attribute(*a)).collect(),
|
2013-12-25 11:10:33 -07:00
|
|
|
vis: vi.vis,
|
|
|
|
span: folder.new_span(vi.span),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-09 15:05:33 +02:00
|
|
|
pub fn noop_fold_block<T: Folder>(b: P<Block>, folder: &mut T) -> P<Block> {
|
2014-04-11 11:28:43 +02:00
|
|
|
let id = folder.new_id(b.id); // Needs to be first, for ast_map.
|
2014-03-28 20:42:34 +01:00
|
|
|
let view_items = b.view_items.iter().map(|x| folder.fold_view_item(x)).collect();
|
2014-05-16 00:16:13 -07:00
|
|
|
let stmts = b.stmts.iter().flat_map(|s| folder.fold_stmt(&**s).move_iter()).collect();
|
2013-12-01 00:00:39 +02:00
|
|
|
P(Block {
|
2014-04-11 11:28:43 +02:00
|
|
|
id: id,
|
2013-08-29 12:10:02 -07:00
|
|
|
view_items: view_items,
|
|
|
|
stmts: stmts,
|
2013-09-20 02:08:47 -04:00
|
|
|
expr: b.expr.map(|x| folder.fold_expr(x)),
|
2013-08-29 12:10:02 -07:00
|
|
|
rules: b.rules,
|
|
|
|
span: folder.new_span(b.span),
|
2013-12-01 00:00:39 +02:00
|
|
|
})
|
2013-08-29 12:10:02 -07:00
|
|
|
}
|
|
|
|
|
2014-01-09 15:05:33 +02:00
|
|
|
pub fn noop_fold_item_underscore<T: Folder>(i: &Item_, folder: &mut T) -> Item_ {
|
2013-02-17 22:20:36 -08:00
|
|
|
match *i {
|
2014-01-09 15:05:33 +02:00
|
|
|
ItemStatic(t, m, e) => {
|
|
|
|
ItemStatic(folder.fold_ty(t), m, folder.fold_expr(e))
|
2013-09-06 22:11:55 -04:00
|
|
|
}
|
2014-04-06 18:04:40 -07:00
|
|
|
ItemFn(decl, fn_style, abi, ref generics, body) => {
|
2014-01-09 15:05:33 +02:00
|
|
|
ItemFn(
|
2014-05-16 00:16:13 -07:00
|
|
|
folder.fold_fn_decl(&*decl),
|
2014-04-06 18:04:40 -07:00
|
|
|
fn_style,
|
2013-03-13 22:25:28 -04:00
|
|
|
abi,
|
2014-07-25 21:00:33 +02:00
|
|
|
folder.fold_generics(generics),
|
2013-08-29 12:10:02 -07:00
|
|
|
folder.fold_block(body)
|
2013-02-17 22:20:36 -08:00
|
|
|
)
|
|
|
|
}
|
2014-01-09 15:05:33 +02:00
|
|
|
ItemMod(ref m) => ItemMod(folder.fold_mod(m)),
|
|
|
|
ItemForeignMod(ref nm) => ItemForeignMod(folder.fold_foreign_mod(nm)),
|
|
|
|
ItemTy(t, ref generics) => {
|
2014-07-25 21:00:33 +02:00
|
|
|
ItemTy(folder.fold_ty(t), folder.fold_generics(generics))
|
2013-02-17 22:20:36 -08:00
|
|
|
}
|
2014-01-09 15:05:33 +02:00
|
|
|
ItemEnum(ref enum_definition, ref generics) => {
|
|
|
|
ItemEnum(
|
|
|
|
ast::EnumDef {
|
2014-03-28 20:42:34 +01:00
|
|
|
variants: enum_definition.variants.iter().map(|&x| {
|
2014-05-16 00:16:13 -07:00
|
|
|
folder.fold_variant(&*x)
|
2014-03-28 20:42:34 +01:00
|
|
|
}).collect(),
|
2013-03-07 18:04:21 -08:00
|
|
|
},
|
2014-07-25 21:00:33 +02:00
|
|
|
folder.fold_generics(generics))
|
2013-02-17 22:20:36 -08:00
|
|
|
}
|
2014-01-09 15:05:33 +02:00
|
|
|
ItemStruct(ref struct_def, ref generics) => {
|
2014-07-25 21:00:33 +02:00
|
|
|
let struct_def = folder.fold_struct_def(*struct_def);
|
|
|
|
ItemStruct(struct_def, folder.fold_generics(generics))
|
2013-02-11 08:02:51 -08:00
|
|
|
}
|
2014-08-04 13:56:56 -07:00
|
|
|
ItemImpl(ref generics, ref ifce, ty, ref impl_items) => {
|
2014-07-25 21:00:33 +02:00
|
|
|
ItemImpl(folder.fold_generics(generics),
|
|
|
|
ifce.as_ref().map(|p| folder.fold_trait_ref(p)),
|
2014-01-09 15:05:33 +02:00
|
|
|
folder.fold_ty(ty),
|
2014-08-04 13:56:56 -07:00
|
|
|
impl_items.iter()
|
|
|
|
.flat_map(|impl_item| {
|
|
|
|
match *impl_item {
|
|
|
|
MethodImplItem(x) => {
|
|
|
|
folder.fold_method(x)
|
|
|
|
.move_iter()
|
|
|
|
.map(|x| MethodImplItem(x))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}).collect()
|
2013-02-17 22:20:36 -08:00
|
|
|
)
|
2013-02-11 08:02:51 -08:00
|
|
|
}
|
2014-08-27 21:46:52 -04:00
|
|
|
ItemTrait(ref generics, ref unbound, ref bounds, ref methods) => {
|
|
|
|
let bounds = folder.fold_bounds(bounds);
|
2014-07-12 22:33:30 -07:00
|
|
|
let methods = methods.iter().flat_map(|method| {
|
|
|
|
let r = match *method {
|
2014-08-04 13:56:56 -07:00
|
|
|
RequiredMethod(ref m) => {
|
|
|
|
SmallVector::one(RequiredMethod(
|
|
|
|
folder.fold_type_method(m))).move_iter()
|
|
|
|
}
|
|
|
|
ProvidedMethod(method) => {
|
2014-07-12 22:33:30 -07:00
|
|
|
// the awkward collect/iter idiom here is because
|
|
|
|
// even though an iter and a map satisfy the same trait bound,
|
|
|
|
// they're not actually the same type, so the method arms
|
|
|
|
// don't unify.
|
2014-08-04 13:56:56 -07:00
|
|
|
let methods : SmallVector<ast::TraitItem> =
|
2014-07-12 22:33:30 -07:00
|
|
|
folder.fold_method(method).move_iter()
|
2014-08-04 13:56:56 -07:00
|
|
|
.map(|m| ProvidedMethod(m)).collect();
|
2014-07-12 22:33:30 -07:00
|
|
|
methods.move_iter()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
r
|
2014-03-28 20:42:34 +01:00
|
|
|
}).collect();
|
2014-07-25 21:00:33 +02:00
|
|
|
ItemTrait(folder.fold_generics(generics),
|
2014-07-08 14:26:02 +12:00
|
|
|
unbound.clone(),
|
2014-08-27 21:46:52 -04:00
|
|
|
bounds,
|
2014-01-09 15:05:33 +02:00
|
|
|
methods)
|
2013-02-11 08:02:51 -08:00
|
|
|
}
|
2014-01-09 15:05:33 +02:00
|
|
|
ItemMac(ref m) => ItemMac(folder.fold_mac(m)),
|
2013-02-11 08:02:51 -08:00
|
|
|
}
|
2011-06-20 17:25:49 -07:00
|
|
|
}
|
|
|
|
|
2014-01-09 15:05:33 +02:00
|
|
|
pub fn noop_fold_type_method<T: Folder>(m: &TypeMethod, fld: &mut T) -> TypeMethod {
|
2014-04-11 11:28:43 +02:00
|
|
|
let id = fld.new_id(m.id); // Needs to be first, for ast_map.
|
2013-08-29 12:10:02 -07:00
|
|
|
TypeMethod {
|
2014-04-11 11:28:43 +02:00
|
|
|
id: id,
|
2013-01-15 16:05:20 -08:00
|
|
|
ident: fld.fold_ident(m.ident),
|
2014-06-25 18:15:07 -07:00
|
|
|
attrs: m.attrs.iter().map(|a| fld.fold_attribute(*a)).collect(),
|
2014-04-06 18:04:40 -07:00
|
|
|
fn_style: m.fn_style,
|
2014-05-28 22:26:56 -07:00
|
|
|
abi: m.abi,
|
2014-05-16 00:16:13 -07:00
|
|
|
decl: fld.fold_fn_decl(&*m.decl),
|
2014-07-25 21:00:33 +02:00
|
|
|
generics: fld.fold_generics(&m.generics),
|
2013-10-29 06:03:32 -04:00
|
|
|
explicit_self: fld.fold_explicit_self(&m.explicit_self),
|
2013-01-15 16:05:20 -08:00
|
|
|
span: fld.new_span(m.span),
|
2014-05-22 10:49:26 -07:00
|
|
|
vis: m.vis,
|
2013-01-14 19:35:08 -08:00
|
|
|
}
|
2011-06-20 17:25:49 -07:00
|
|
|
}
|
|
|
|
|
2014-01-09 15:05:33 +02:00
|
|
|
pub fn noop_fold_mod<T: Folder>(m: &Mod, folder: &mut T) -> Mod {
|
|
|
|
ast::Mod {
|
2014-04-27 05:05:45 +09:00
|
|
|
inner: folder.new_span(m.inner),
|
2013-08-29 12:10:02 -07:00
|
|
|
view_items: m.view_items
|
|
|
|
.iter()
|
|
|
|
.map(|x| folder.fold_view_item(x)).collect(),
|
2013-11-24 23:08:53 -08:00
|
|
|
items: m.items.iter().flat_map(|x| folder.fold_item(*x).move_iter()).collect(),
|
2013-02-17 22:20:36 -08:00
|
|
|
}
|
2011-06-20 17:25:49 -07:00
|
|
|
}
|
|
|
|
|
2014-01-09 15:05:33 +02:00
|
|
|
pub fn noop_fold_crate<T: Folder>(c: Crate, folder: &mut T) -> Crate {
|
2013-08-29 12:10:02 -07:00
|
|
|
Crate {
|
|
|
|
module: folder.fold_mod(&c.module),
|
2014-06-25 18:15:07 -07:00
|
|
|
attrs: c.attrs.iter().map(|x| folder.fold_attribute(*x)).collect(),
|
2014-07-25 21:00:33 +02:00
|
|
|
config: c.config.iter().map(|x| box (GC) folder.fold_meta_item(&**x)).collect(),
|
2013-08-29 12:10:02 -07:00
|
|
|
span: folder.new_span(c.span),
|
2014-07-17 09:45:31 -07:00
|
|
|
exported_macros: c.exported_macros
|
2013-02-17 22:20:36 -08:00
|
|
|
}
|
2011-06-20 17:25:49 -07:00
|
|
|
}
|
|
|
|
|
2014-06-27 16:10:23 -07:00
|
|
|
// fold one item into possibly many items
|
2014-05-16 00:16:13 -07:00
|
|
|
pub fn noop_fold_item<T: Folder>(i: &Item,
|
|
|
|
folder: &mut T) -> SmallVector<Gc<Item>> {
|
2014-07-25 21:00:33 +02:00
|
|
|
SmallVector::one(box(GC) folder.fold_item_simple(i))
|
2014-06-27 16:10:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// fold one item into exactly one item
|
2014-07-25 21:00:33 +02:00
|
|
|
pub fn noop_fold_item_simple<T: Folder>(i: &Item, folder: &mut T) -> Item {
|
2014-02-14 07:07:09 +02:00
|
|
|
let id = folder.new_id(i.id); // Needs to be first, for ast_map.
|
|
|
|
let node = folder.fold_item_underscore(&i.node);
|
|
|
|
let ident = match node {
|
|
|
|
// The node may have changed, recompute the "pretty" impl name.
|
|
|
|
ItemImpl(_, ref maybe_trait, ty, _) => {
|
2014-05-16 00:16:13 -07:00
|
|
|
ast_util::impl_pretty_name(maybe_trait, &*ty)
|
2014-02-14 07:07:09 +02:00
|
|
|
}
|
|
|
|
_ => i.ident
|
|
|
|
};
|
|
|
|
|
2014-06-27 16:10:23 -07:00
|
|
|
Item {
|
2014-02-14 07:07:09 +02:00
|
|
|
id: id,
|
|
|
|
ident: folder.fold_ident(ident),
|
2014-06-25 18:15:07 -07:00
|
|
|
attrs: i.attrs.iter().map(|e| folder.fold_attribute(*e)).collect(),
|
2014-02-14 07:07:09 +02:00
|
|
|
node: node,
|
2013-08-29 12:10:02 -07:00
|
|
|
vis: i.vis,
|
|
|
|
span: folder.new_span(i.span)
|
2014-06-27 16:10:23 -07:00
|
|
|
}
|
2012-01-22 17:30:07 -07:00
|
|
|
}
|
|
|
|
|
2014-05-16 00:16:13 -07:00
|
|
|
pub fn noop_fold_foreign_item<T: Folder>(ni: &ForeignItem,
|
|
|
|
folder: &mut T) -> Gc<ForeignItem> {
|
2014-04-11 11:28:43 +02:00
|
|
|
let id = folder.new_id(ni.id); // Needs to be first, for ast_map.
|
2014-05-16 00:16:13 -07:00
|
|
|
box(GC) ForeignItem {
|
2014-04-11 11:28:43 +02:00
|
|
|
id: id,
|
2014-01-06 14:00:46 +02:00
|
|
|
ident: folder.fold_ident(ni.ident),
|
2014-06-25 18:15:07 -07:00
|
|
|
attrs: ni.attrs.iter().map(|x| folder.fold_attribute(*x)).collect(),
|
2014-01-06 14:00:46 +02:00
|
|
|
node: match ni.node {
|
2014-01-09 15:05:33 +02:00
|
|
|
ForeignItemFn(ref fdec, ref generics) => {
|
|
|
|
ForeignItemFn(P(FnDecl {
|
2014-07-25 21:00:33 +02:00
|
|
|
inputs: fdec.inputs.iter().map(|a| folder.fold_arg(a)).collect(),
|
2014-01-06 14:00:46 +02:00
|
|
|
output: folder.fold_ty(fdec.output),
|
|
|
|
cf: fdec.cf,
|
|
|
|
variadic: fdec.variadic
|
2014-07-25 21:00:33 +02:00
|
|
|
}), folder.fold_generics(generics))
|
2014-01-06 14:00:46 +02:00
|
|
|
}
|
2014-01-09 15:05:33 +02:00
|
|
|
ForeignItemStatic(t, m) => {
|
|
|
|
ForeignItemStatic(folder.fold_ty(t), m)
|
2014-01-06 14:00:46 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
span: folder.new_span(ni.span),
|
|
|
|
vis: ni.vis,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-12 22:33:30 -07:00
|
|
|
// Default fold over a method.
|
|
|
|
// Invariant: produces exactly one method.
|
|
|
|
pub fn noop_fold_method<T: Folder>(m: &Method, folder: &mut T) -> SmallVector<Gc<Method>> {
|
2014-04-11 11:28:43 +02:00
|
|
|
let id = folder.new_id(m.id); // Needs to be first, for ast_map.
|
2014-07-12 22:33:30 -07:00
|
|
|
SmallVector::one(box(GC) Method {
|
2014-06-25 18:15:07 -07:00
|
|
|
attrs: m.attrs.iter().map(|a| folder.fold_attribute(*a)).collect(),
|
2014-07-11 21:22:11 -07:00
|
|
|
id: id,
|
2014-01-06 14:00:46 +02:00
|
|
|
span: folder.new_span(m.span),
|
2014-07-11 21:22:11 -07:00
|
|
|
node: match m.node {
|
2014-05-28 22:26:56 -07:00
|
|
|
MethDecl(ident,
|
|
|
|
ref generics,
|
|
|
|
abi,
|
|
|
|
ref explicit_self,
|
|
|
|
fn_style,
|
|
|
|
decl,
|
|
|
|
body,
|
|
|
|
vis) => {
|
2014-07-11 21:22:11 -07:00
|
|
|
MethDecl(folder.fold_ident(ident),
|
2014-07-25 21:00:33 +02:00
|
|
|
folder.fold_generics(generics),
|
2014-05-28 22:26:56 -07:00
|
|
|
abi,
|
2014-07-11 21:22:11 -07:00
|
|
|
folder.fold_explicit_self(explicit_self),
|
|
|
|
fn_style,
|
|
|
|
folder.fold_fn_decl(&*decl),
|
|
|
|
folder.fold_block(body),
|
|
|
|
vis)
|
|
|
|
},
|
|
|
|
MethMac(ref mac) => MethMac(folder.fold_mac(mac)),
|
|
|
|
}
|
2014-07-12 22:33:30 -07:00
|
|
|
})
|
2014-01-06 14:00:46 +02:00
|
|
|
}
|
|
|
|
|
2014-05-16 00:16:13 -07:00
|
|
|
pub fn noop_fold_pat<T: Folder>(p: Gc<Pat>, folder: &mut T) -> Gc<Pat> {
|
2014-04-11 11:28:43 +02:00
|
|
|
let id = folder.new_id(p.id);
|
2014-01-06 14:00:46 +02:00
|
|
|
let node = match p.node {
|
2014-08-06 17:04:44 +02:00
|
|
|
PatWild(k) => PatWild(k),
|
2014-06-30 18:02:14 -07:00
|
|
|
PatIdent(binding_mode, ref pth1, ref sub) => {
|
2014-01-06 14:00:46 +02:00
|
|
|
PatIdent(binding_mode,
|
2014-06-30 18:02:14 -07:00
|
|
|
Spanned{span: folder.new_span(pth1.span),
|
2014-07-02 23:17:10 -07:00
|
|
|
node: folder.fold_ident(pth1.node)},
|
2014-01-06 14:00:46 +02:00
|
|
|
sub.map(|x| folder.fold_pat(x)))
|
|
|
|
}
|
|
|
|
PatLit(e) => PatLit(folder.fold_expr(e)),
|
|
|
|
PatEnum(ref pth, ref pats) => {
|
|
|
|
PatEnum(folder.fold_path(pth),
|
2014-03-28 20:42:34 +01:00
|
|
|
pats.as_ref().map(|pats| pats.iter().map(|x| folder.fold_pat(*x)).collect()))
|
2014-01-06 14:00:46 +02:00
|
|
|
}
|
|
|
|
PatStruct(ref pth, ref fields, etc) => {
|
|
|
|
let pth_ = folder.fold_path(pth);
|
2014-03-28 20:42:34 +01:00
|
|
|
let fs = fields.iter().map(|f| {
|
2014-01-06 14:00:46 +02:00
|
|
|
ast::FieldPat {
|
|
|
|
ident: f.ident,
|
|
|
|
pat: folder.fold_pat(f.pat)
|
|
|
|
}
|
2014-03-28 20:42:34 +01:00
|
|
|
}).collect();
|
2014-01-06 14:00:46 +02:00
|
|
|
PatStruct(pth_, fs, etc)
|
|
|
|
}
|
2014-03-28 20:42:34 +01:00
|
|
|
PatTup(ref elts) => PatTup(elts.iter().map(|x| folder.fold_pat(*x)).collect()),
|
2014-05-26 17:55:54 -07:00
|
|
|
PatBox(inner) => PatBox(folder.fold_pat(inner)),
|
2014-01-06 14:00:46 +02:00
|
|
|
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) => {
|
2014-03-28 20:42:34 +01:00
|
|
|
PatVec(before.iter().map(|x| folder.fold_pat(*x)).collect(),
|
2014-01-06 14:00:46 +02:00
|
|
|
slice.map(|x| folder.fold_pat(x)),
|
2014-03-28 20:42:34 +01:00
|
|
|
after.iter().map(|x| folder.fold_pat(*x)).collect())
|
2014-01-06 14:00:46 +02:00
|
|
|
}
|
2014-05-19 13:29:41 -07:00
|
|
|
PatMac(ref mac) => PatMac(folder.fold_mac(mac)),
|
2014-01-06 14:00:46 +02:00
|
|
|
};
|
|
|
|
|
2014-05-16 00:16:13 -07:00
|
|
|
box(GC) Pat {
|
2014-04-11 11:28:43 +02:00
|
|
|
id: id,
|
2014-01-06 14:00:46 +02:00
|
|
|
span: folder.new_span(p.span),
|
|
|
|
node: node,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-16 00:16:13 -07:00
|
|
|
pub fn noop_fold_expr<T: Folder>(e: Gc<Expr>, folder: &mut T) -> Gc<Expr> {
|
2014-04-11 11:28:43 +02:00
|
|
|
let id = folder.new_id(e.id);
|
2013-08-29 12:10:02 -07:00
|
|
|
let node = match e.node {
|
2013-12-17 16:46:18 -08:00
|
|
|
ExprBox(p, e) => {
|
|
|
|
ExprBox(folder.fold_expr(p), folder.fold_expr(e))
|
|
|
|
}
|
2014-04-04 13:12:18 +03:00
|
|
|
ExprVec(ref exprs) => {
|
|
|
|
ExprVec(exprs.iter().map(|&x| folder.fold_expr(x)).collect())
|
2013-02-17 22:20:36 -08:00
|
|
|
}
|
2014-04-04 13:12:18 +03:00
|
|
|
ExprRepeat(expr, count) => {
|
|
|
|
ExprRepeat(folder.fold_expr(expr), folder.fold_expr(count))
|
2013-02-17 22:20:36 -08:00
|
|
|
}
|
2014-03-28 20:42:34 +01:00
|
|
|
ExprTup(ref elts) => ExprTup(elts.iter().map(|x| folder.fold_expr(*x)).collect()),
|
2014-02-14 10:28:32 +02:00
|
|
|
ExprCall(f, ref args) => {
|
2013-08-29 12:10:02 -07:00
|
|
|
ExprCall(folder.fold_expr(f),
|
2014-03-28 20:42:34 +01:00
|
|
|
args.iter().map(|&x| folder.fold_expr(x)).collect())
|
2013-02-17 22:20:36 -08:00
|
|
|
}
|
2014-02-26 16:06:45 +02:00
|
|
|
ExprMethodCall(i, ref tps, ref args) => {
|
2013-09-02 03:45:37 +02:00
|
|
|
ExprMethodCall(
|
2014-04-24 05:19:23 +08:00
|
|
|
respan(i.span, folder.fold_ident(i.node)),
|
2014-03-28 20:42:34 +01:00
|
|
|
tps.iter().map(|&x| folder.fold_ty(x)).collect(),
|
|
|
|
args.iter().map(|&x| folder.fold_expr(x)).collect())
|
2013-02-17 22:20:36 -08:00
|
|
|
}
|
2014-02-26 16:06:45 +02:00
|
|
|
ExprBinary(binop, lhs, rhs) => {
|
|
|
|
ExprBinary(binop,
|
2013-08-29 12:10:02 -07:00
|
|
|
folder.fold_expr(lhs),
|
|
|
|
folder.fold_expr(rhs))
|
2013-06-01 15:31:56 -07:00
|
|
|
}
|
2014-02-26 16:06:45 +02:00
|
|
|
ExprUnary(binop, ohs) => {
|
|
|
|
ExprUnary(binop, folder.fold_expr(ohs))
|
2013-02-17 22:20:36 -08:00
|
|
|
}
|
2013-08-29 12:10:02 -07:00
|
|
|
ExprLit(_) => e.node.clone(),
|
2013-12-01 00:00:39 +02:00
|
|
|
ExprCast(expr, ty) => {
|
2013-08-29 12:10:02 -07:00
|
|
|
ExprCast(folder.fold_expr(expr), folder.fold_ty(ty))
|
2013-07-02 12:47:32 -07:00
|
|
|
}
|
2013-08-29 12:10:02 -07:00
|
|
|
ExprAddrOf(m, ohs) => ExprAddrOf(m, folder.fold_expr(ohs)),
|
2013-12-01 00:00:39 +02:00
|
|
|
ExprIf(cond, tr, fl) => {
|
2013-08-29 12:10:02 -07:00
|
|
|
ExprIf(folder.fold_expr(cond),
|
|
|
|
folder.fold_block(tr),
|
2013-09-20 02:08:47 -04:00
|
|
|
fl.map(|x| folder.fold_expr(x)))
|
2013-02-17 22:20:36 -08:00
|
|
|
}
|
2014-07-25 20:12:51 -04:00
|
|
|
ExprWhile(cond, body, opt_ident) => {
|
|
|
|
ExprWhile(folder.fold_expr(cond),
|
|
|
|
folder.fold_block(body),
|
|
|
|
opt_ident.map(|i| folder.fold_ident(i)))
|
2013-02-17 22:20:36 -08:00
|
|
|
}
|
2014-07-25 20:12:51 -04:00
|
|
|
ExprForLoop(pat, iter, body, ref opt_ident) => {
|
2013-08-29 12:10:02 -07:00
|
|
|
ExprForLoop(folder.fold_pat(pat),
|
|
|
|
folder.fold_expr(iter),
|
|
|
|
folder.fold_block(body),
|
2014-07-25 20:12:51 -04:00
|
|
|
opt_ident.map(|i| folder.fold_ident(i)))
|
2013-07-29 17:25:00 -07:00
|
|
|
}
|
2013-12-01 00:00:39 +02:00
|
|
|
ExprLoop(body, opt_ident) => {
|
2013-08-29 12:10:02 -07:00
|
|
|
ExprLoop(folder.fold_block(body),
|
2014-07-25 20:12:51 -04:00
|
|
|
opt_ident.map(|i| folder.fold_ident(i)))
|
2013-02-17 22:20:36 -08:00
|
|
|
}
|
2013-09-02 03:45:37 +02:00
|
|
|
ExprMatch(expr, ref arms) => {
|
2013-08-29 12:10:02 -07:00
|
|
|
ExprMatch(folder.fold_expr(expr),
|
2014-03-28 20:42:34 +01:00
|
|
|
arms.iter().map(|x| folder.fold_arm(x)).collect())
|
2013-02-17 22:20:36 -08:00
|
|
|
}
|
2014-07-23 12:43:29 -07:00
|
|
|
ExprFnBlock(capture_clause, ref decl, ref body) => {
|
|
|
|
ExprFnBlock(capture_clause,
|
|
|
|
folder.fold_fn_decl(&**decl),
|
2014-05-16 00:16:13 -07:00
|
|
|
folder.fold_block(body.clone()))
|
2013-02-17 22:20:36 -08:00
|
|
|
}
|
2014-05-16 00:16:13 -07:00
|
|
|
ExprProc(ref decl, ref body) => {
|
|
|
|
ExprProc(folder.fold_fn_decl(&**decl),
|
|
|
|
folder.fold_block(body.clone()))
|
2013-10-28 15:22:49 -07:00
|
|
|
}
|
2014-07-29 22:08:39 -07:00
|
|
|
ExprUnboxedFn(capture_clause, kind, ref decl, ref body) => {
|
2014-07-23 12:43:29 -07:00
|
|
|
ExprUnboxedFn(capture_clause,
|
2014-07-29 22:08:39 -07:00
|
|
|
kind,
|
2014-07-23 12:43:29 -07:00
|
|
|
folder.fold_fn_decl(&**decl),
|
2014-05-28 22:26:56 -07:00
|
|
|
folder.fold_block(*body))
|
|
|
|
}
|
|
|
|
ExprBlock(ref blk) => ExprBlock(folder.fold_block(*blk)),
|
2013-09-02 03:45:37 +02:00
|
|
|
ExprAssign(el, er) => {
|
2013-08-29 12:10:02 -07:00
|
|
|
ExprAssign(folder.fold_expr(el), folder.fold_expr(er))
|
2013-02-17 22:20:36 -08:00
|
|
|
}
|
2014-02-26 16:06:45 +02:00
|
|
|
ExprAssignOp(op, el, er) => {
|
|
|
|
ExprAssignOp(op,
|
2013-08-29 12:10:02 -07:00
|
|
|
folder.fold_expr(el),
|
|
|
|
folder.fold_expr(er))
|
2013-02-17 22:20:36 -08:00
|
|
|
}
|
2013-09-02 03:45:37 +02:00
|
|
|
ExprField(el, id, ref tys) => {
|
2013-10-28 19:22:42 -07:00
|
|
|
ExprField(folder.fold_expr(el),
|
2014-06-13 22:56:42 +01:00
|
|
|
respan(id.span, folder.fold_ident(id.node)),
|
2014-03-28 20:42:34 +01:00
|
|
|
tys.iter().map(|&x| folder.fold_ty(x)).collect())
|
2013-02-17 22:20:36 -08:00
|
|
|
}
|
2014-02-26 16:06:45 +02:00
|
|
|
ExprIndex(el, er) => {
|
|
|
|
ExprIndex(folder.fold_expr(el), folder.fold_expr(er))
|
2013-02-17 22:20:36 -08:00
|
|
|
}
|
2013-08-29 12:10:02 -07:00
|
|
|
ExprPath(ref pth) => ExprPath(folder.fold_path(pth)),
|
2014-02-15 16:54:32 +08:00
|
|
|
ExprBreak(opt_ident) => ExprBreak(opt_ident.map(|x| folder.fold_ident(x))),
|
|
|
|
ExprAgain(opt_ident) => ExprAgain(opt_ident.map(|x| folder.fold_ident(x))),
|
2013-09-02 03:45:37 +02:00
|
|
|
ExprRet(ref e) => {
|
2013-09-20 02:08:47 -04:00
|
|
|
ExprRet(e.map(|x| folder.fold_expr(x)))
|
2013-02-17 22:20:36 -08:00
|
|
|
}
|
2013-09-02 03:45:37 +02:00
|
|
|
ExprInlineAsm(ref a) => {
|
2014-01-09 15:05:33 +02:00
|
|
|
ExprInlineAsm(InlineAsm {
|
2014-03-28 20:42:34 +01:00
|
|
|
inputs: a.inputs.iter().map(|&(ref c, input)| {
|
2014-01-15 18:30:40 -08:00
|
|
|
((*c).clone(), folder.fold_expr(input))
|
2014-03-28 20:42:34 +01:00
|
|
|
}).collect(),
|
2014-08-19 20:39:26 +01:00
|
|
|
outputs: a.outputs.iter().map(|&(ref c, out, is_rw)| {
|
|
|
|
((*c).clone(), folder.fold_expr(out), is_rw)
|
2014-03-28 20:42:34 +01:00
|
|
|
}).collect(),
|
2013-07-02 12:47:32 -07:00
|
|
|
.. (*a).clone()
|
2013-03-27 13:42:21 -07:00
|
|
|
})
|
2013-03-12 17:53:25 -07:00
|
|
|
}
|
2013-08-29 12:10:02 -07:00
|
|
|
ExprMac(ref mac) => ExprMac(folder.fold_mac(mac)),
|
2013-09-02 03:45:37 +02:00
|
|
|
ExprStruct(ref path, ref fields, maybe_expr) => {
|
2013-08-29 12:10:02 -07:00
|
|
|
ExprStruct(folder.fold_path(path),
|
2014-07-25 21:00:33 +02:00
|
|
|
fields.iter().map(|x| folder.fold_field(*x)).collect(),
|
2013-09-20 02:08:47 -04:00
|
|
|
maybe_expr.map(|x| folder.fold_expr(x)))
|
2013-02-17 22:20:36 -08:00
|
|
|
},
|
2013-08-29 12:10:02 -07:00
|
|
|
ExprParen(ex) => ExprParen(folder.fold_expr(ex))
|
2013-09-06 22:11:55 -04:00
|
|
|
};
|
2012-01-25 16:23:43 -08:00
|
|
|
|
2014-05-16 00:16:13 -07:00
|
|
|
box(GC) Expr {
|
2014-04-11 11:28:43 +02:00
|
|
|
id: id,
|
2013-08-29 12:10:02 -07:00
|
|
|
node: node,
|
|
|
|
span: folder.new_span(e.span),
|
2013-01-14 20:52:28 -08:00
|
|
|
}
|
2011-06-20 17:25:49 -07:00
|
|
|
}
|
|
|
|
|
2014-05-16 00:16:13 -07:00
|
|
|
pub fn noop_fold_stmt<T: Folder>(s: &Stmt,
|
|
|
|
folder: &mut T) -> SmallVector<Gc<Stmt>> {
|
2013-11-24 23:08:53 -08:00
|
|
|
let nodes = match s.node {
|
2014-04-11 11:28:43 +02:00
|
|
|
StmtDecl(d, id) => {
|
|
|
|
let id = folder.new_id(id);
|
2013-11-24 23:08:53 -08:00
|
|
|
folder.fold_decl(d).move_iter()
|
2014-04-11 11:28:43 +02:00
|
|
|
.map(|d| StmtDecl(d, id))
|
2013-11-24 23:08:53 -08:00
|
|
|
.collect()
|
2013-06-04 21:43:41 -07:00
|
|
|
}
|
2014-04-11 11:28:43 +02:00
|
|
|
StmtExpr(e, id) => {
|
|
|
|
let id = folder.new_id(id);
|
|
|
|
SmallVector::one(StmtExpr(folder.fold_expr(e), id))
|
2013-01-15 13:51:43 -08:00
|
|
|
}
|
2014-04-11 11:28:43 +02:00
|
|
|
StmtSemi(e, id) => {
|
|
|
|
let id = folder.new_id(id);
|
|
|
|
SmallVector::one(StmtSemi(folder.fold_expr(e), id))
|
2013-01-15 14:59:39 -08:00
|
|
|
}
|
2013-11-24 23:08:53 -08:00
|
|
|
StmtMac(ref mac, semi) => SmallVector::one(StmtMac(folder.fold_mac(mac), semi))
|
2013-08-29 12:10:02 -07:00
|
|
|
};
|
2012-05-21 18:28:39 -07:00
|
|
|
|
2014-05-16 00:16:13 -07:00
|
|
|
nodes.move_iter().map(|node| box(GC) Spanned {
|
2013-08-29 12:10:02 -07:00
|
|
|
node: node,
|
|
|
|
span: folder.new_span(s.span),
|
2013-11-24 23:08:53 -08:00
|
|
|
}).collect()
|
2011-06-20 17:25:49 -07:00
|
|
|
}
|
2013-06-06 14:17:00 -07:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
2014-01-30 14:28:44 -08:00
|
|
|
use std::io;
|
2013-06-06 14:17:00 -07:00
|
|
|
use ast;
|
|
|
|
use util::parser_testing::{string_to_crate, matches_codepattern};
|
|
|
|
use parse::token;
|
|
|
|
use print::pprust;
|
2014-07-09 14:48:12 -07:00
|
|
|
use fold;
|
2013-06-06 14:17:00 -07:00
|
|
|
use super::*;
|
2013-09-16 23:37:54 -07:00
|
|
|
|
2013-06-06 14:17:00 -07:00
|
|
|
// this version doesn't care about getting comments or docstrings in.
|
2014-03-17 22:27:37 -07:00
|
|
|
fn fake_print_crate(s: &mut pprust::State,
|
|
|
|
krate: &ast::Crate) -> io::IoResult<()> {
|
2014-03-17 09:55:41 +02:00
|
|
|
s.print_mod(&krate.module, krate.attrs.as_slice())
|
2013-06-06 14:17:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// change every identifier to "zz"
|
2013-08-30 18:00:38 -07:00
|
|
|
struct ToZzIdentFolder;
|
|
|
|
|
2014-01-09 15:05:33 +02:00
|
|
|
impl Folder for ToZzIdentFolder {
|
2013-12-27 20:34:51 -07:00
|
|
|
fn fold_ident(&mut self, _: ast::Ident) -> ast::Ident {
|
2013-08-30 18:00:38 -07:00
|
|
|
token::str_to_ident("zz")
|
|
|
|
}
|
2014-07-09 14:48:12 -07:00
|
|
|
fn fold_mac(&mut self, macro: &ast::Mac) -> ast::Mac {
|
2014-07-25 21:00:33 +02:00
|
|
|
fold::noop_fold_mac(macro, self)
|
2014-07-09 14:48:12 -07:00
|
|
|
}
|
2013-06-06 14:17:00 -07: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;
|
2014-05-07 16:33:43 -07:00
|
|
|
if !(pred_val(a_val.as_slice(),b_val.as_slice())) {
|
2013-10-21 13:08:31 -07:00
|
|
|
fail!("expected args satisfying {}, got {:?} and {:?}",
|
2013-06-06 14:17:00 -07:00
|
|
|
$predname, a_val, b_val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
// make sure idents get transformed everywhere
|
|
|
|
#[test] fn ident_transformation () {
|
2013-12-27 20:34:51 -07:00
|
|
|
let mut zz_fold = ToZzIdentFolder;
|
2014-01-30 18:46:19 -08:00
|
|
|
let ast = string_to_crate(
|
2014-05-25 03:17:19 -07:00
|
|
|
"#[a] mod b {fn c (d : e, f : g) {h!(i,j,k);l;m}}".to_string());
|
2014-03-17 09:55:41 +02:00
|
|
|
let folded_crate = zz_fold.fold_crate(ast);
|
2014-05-07 16:33:43 -07:00
|
|
|
assert_pred!(
|
|
|
|
matches_codepattern,
|
|
|
|
"matches_codepattern",
|
2014-06-21 03:39:03 -07:00
|
|
|
pprust::to_string(|s| fake_print_crate(s, &folded_crate)),
|
2014-05-25 03:17:19 -07:00
|
|
|
"#[a]mod zz{fn zz(zz:zz,zz:zz){zz!(zz,zz,zz);zz;zz}}".to_string());
|
2013-06-06 14:17:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// even inside macro defs....
|
|
|
|
#[test] fn ident_transformation_in_defs () {
|
2013-12-27 20:34:51 -07:00
|
|
|
let mut zz_fold = ToZzIdentFolder;
|
2014-01-30 18:46:19 -08:00
|
|
|
let ast = string_to_crate(
|
2014-04-15 18:17:48 -07:00
|
|
|
"macro_rules! a {(b $c:expr $(d $e:token)f+ => \
|
2014-05-25 03:17:19 -07:00
|
|
|
(g $(d $d $e)+))} ".to_string());
|
2014-03-17 09:55:41 +02:00
|
|
|
let folded_crate = zz_fold.fold_crate(ast);
|
2014-05-07 16:33:43 -07:00
|
|
|
assert_pred!(
|
|
|
|
matches_codepattern,
|
|
|
|
"matches_codepattern",
|
2014-06-21 03:39:03 -07:00
|
|
|
pprust::to_string(|s| fake_print_crate(s, &folded_crate)),
|
2014-05-25 03:17:19 -07:00
|
|
|
"zz!zz((zz$zz:zz$(zz $zz:zz)zz+=>(zz$(zz$zz$zz)+)))".to_string());
|
2013-06-06 14:17:00 -07:00
|
|
|
}
|
|
|
|
}
|