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.
|
|
|
|
|
2013-01-08 21:37:25 -06:00
|
|
|
use core::prelude::*;
|
|
|
|
|
2012-09-04 13:37:29 -05:00
|
|
|
use ast::*;
|
2012-12-23 16:41:37 -06:00
|
|
|
use ast;
|
2013-02-25 13:11:21 -06:00
|
|
|
use codemap::{span, spanned};
|
|
|
|
use opt_vec::OptVec;
|
2012-12-23 16:41:37 -06:00
|
|
|
|
|
|
|
use core::option;
|
|
|
|
use core::vec;
|
2011-06-20 19:25:49 -05:00
|
|
|
|
2013-01-29 16:41:40 -06:00
|
|
|
pub trait ast_fold {
|
2013-03-01 09:01:48 -06:00
|
|
|
fn fold_crate(@self, &crate) -> crate;
|
|
|
|
fn fold_view_item(@self, @view_item) -> @view_item;
|
|
|
|
fn fold_foreign_item(@self, @foreign_item) -> @foreign_item;
|
|
|
|
fn fold_item(@self, @item) -> Option<@item>;
|
|
|
|
fn fold_struct_field(@self, @struct_field) -> @struct_field;
|
|
|
|
fn fold_item_underscore(@self, &item_) -> item_;
|
|
|
|
fn fold_method(@self, @method) -> @method;
|
|
|
|
fn fold_block(@self, &blk) -> blk;
|
|
|
|
fn fold_stmt(@self, &stmt) -> @stmt;
|
|
|
|
fn fold_arm(@self, &arm) -> arm;
|
|
|
|
fn fold_pat(@self, @pat) -> @pat;
|
|
|
|
fn fold_decl(@self, @decl) -> @decl;
|
|
|
|
fn fold_expr(@self, @expr) -> @expr;
|
|
|
|
fn fold_ty(@self, @Ty) -> @Ty;
|
|
|
|
fn fold_mod(@self, &_mod) -> _mod;
|
|
|
|
fn fold_foreign_mod(@self, &foreign_mod) -> foreign_mod;
|
|
|
|
fn fold_variant(@self, &variant) -> variant;
|
|
|
|
fn fold_ident(@self, ident) -> ident;
|
|
|
|
fn fold_path(@self, @path) -> @path;
|
|
|
|
fn fold_local(@self, @local) -> @local;
|
2013-03-01 15:30:06 -06:00
|
|
|
fn map_exprs(@self, @fn(@expr) -> @expr, &[@expr]) -> ~[@expr];
|
2013-02-26 20:42:00 -06:00
|
|
|
fn new_id(@self, node_id) -> node_id;
|
|
|
|
fn new_span(@self, span) -> span;
|
2012-05-21 20:28:39 -05:00
|
|
|
}
|
2011-06-20 19:25:49 -05:00
|
|
|
|
|
|
|
// We may eventually want to be able to fold over type parameters, too
|
|
|
|
|
2013-01-29 16:41:40 -06:00
|
|
|
pub struct AstFoldFns {
|
2011-07-27 07:19:39 -05:00
|
|
|
//unlike the others, item_ is non-trivial
|
2013-03-01 15:30:06 -06:00
|
|
|
fold_crate: @fn(&crate_, span, ast_fold) -> (crate_, span),
|
|
|
|
fold_view_item: @fn(view_item_, ast_fold) -> view_item_,
|
|
|
|
fold_foreign_item: @fn(@foreign_item, ast_fold) -> @foreign_item,
|
|
|
|
fold_item: @fn(@item, ast_fold) -> Option<@item>,
|
|
|
|
fold_struct_field: @fn(@struct_field, ast_fold) -> @struct_field,
|
|
|
|
fold_item_underscore: @fn(&item_, ast_fold) -> item_,
|
|
|
|
fold_method: @fn(@method, ast_fold) -> @method,
|
|
|
|
fold_block: @fn(&blk_, span, ast_fold) -> (blk_, span),
|
|
|
|
fold_stmt: @fn(&stmt_, span, ast_fold) -> (stmt_, span),
|
|
|
|
fold_arm: @fn(&arm, ast_fold) -> arm,
|
|
|
|
fold_pat: @fn(&pat_, span, ast_fold) -> (pat_, span),
|
|
|
|
fold_decl: @fn(&decl_, span, ast_fold) -> (decl_, span),
|
|
|
|
fold_expr: @fn(&expr_, span, ast_fold) -> (expr_, span),
|
|
|
|
fold_ty: @fn(&ty_, span, ast_fold) -> (ty_, span),
|
|
|
|
fold_mod: @fn(&_mod, ast_fold) -> _mod,
|
|
|
|
fold_foreign_mod: @fn(&foreign_mod, ast_fold) -> foreign_mod,
|
|
|
|
fold_variant: @fn(&variant_, span, ast_fold) -> (variant_, span),
|
|
|
|
fold_ident: @fn(ident, ast_fold) -> ident,
|
|
|
|
fold_path: @fn(@path, ast_fold) -> path,
|
|
|
|
fold_local: @fn(&local_, span, ast_fold) -> (local_, span),
|
|
|
|
map_exprs: @fn(@fn(@expr) -> @expr, &[@expr]) -> ~[@expr],
|
|
|
|
new_id: @fn(node_id) -> node_id,
|
|
|
|
new_span: @fn(span) -> span
|
2013-01-08 16:00:45 -06:00
|
|
|
}
|
|
|
|
|
2013-01-29 16:41:40 -06:00
|
|
|
pub type ast_fold_fns = @AstFoldFns;
|
2011-06-20 19:25:49 -05:00
|
|
|
|
|
|
|
/* some little folds that probably aren't useful to have in ast_fold itself*/
|
|
|
|
|
|
|
|
//used in noop_fold_item and noop_fold_crate and noop_fold_crate_directive
|
2013-03-01 09:01:48 -06:00
|
|
|
fn fold_meta_item_(mi: @meta_item, fld: @ast_fold) -> @meta_item {
|
2012-12-27 13:36:00 -06:00
|
|
|
@spanned {
|
|
|
|
node:
|
|
|
|
match mi.node {
|
2013-02-18 00:20:36 -06:00
|
|
|
meta_word(id) => meta_word(id),
|
|
|
|
meta_list(id, ref mis) => {
|
2013-02-17 12:59:09 -06:00
|
|
|
let fold_meta_item = |x| fold_meta_item_(x, fld);
|
2013-02-18 00:20:36 -06:00
|
|
|
meta_list(
|
|
|
|
id,
|
|
|
|
mis.map(|e| fold_meta_item(*e))
|
|
|
|
)
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
2013-02-18 00:20:36 -06:00
|
|
|
meta_name_value(id, s) => {
|
|
|
|
meta_name_value(id, /* FIXME (#2543) */ copy s)
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
2012-12-27 13:36:00 -06:00
|
|
|
},
|
|
|
|
span: fld.new_span(mi.span) }
|
2011-06-20 19:25:49 -05:00
|
|
|
}
|
|
|
|
//used in noop_fold_item and noop_fold_crate
|
2013-02-26 20:42:00 -06:00
|
|
|
fn fold_attribute_(at: attribute, fld: @ast_fold) -> attribute {
|
2013-01-13 18:51:48 -06:00
|
|
|
spanned {
|
|
|
|
node: ast::attribute_ {
|
|
|
|
style: at.node.style,
|
2013-02-25 08:19:44 -06:00
|
|
|
value: fold_meta_item_(at.node.value, fld),
|
2013-01-13 18:51:48 -06:00
|
|
|
is_sugared_doc: at.node.is_sugared_doc,
|
|
|
|
},
|
|
|
|
span: fld.new_span(at.span),
|
|
|
|
}
|
2011-06-20 19:25:49 -05:00
|
|
|
}
|
2012-06-26 18:18:37 -05:00
|
|
|
//used in noop_fold_foreign_item and noop_fold_fn_decl
|
2013-02-26 20:42:00 -06:00
|
|
|
fn fold_arg_(a: arg, fld: @ast_fold) -> arg {
|
2013-01-15 18:05:20 -06:00
|
|
|
ast::arg {
|
|
|
|
mode: a.mode,
|
2013-01-03 10:45:07 -06:00
|
|
|
is_mutbl: a.is_mutbl,
|
2013-01-15 18:05:20 -06:00
|
|
|
ty: fld.fold_ty(a.ty),
|
|
|
|
pat: fld.fold_pat(a.pat),
|
|
|
|
id: fld.new_id(a.id),
|
|
|
|
}
|
2011-06-20 19:25:49 -05:00
|
|
|
}
|
2011-07-08 18:35:09 -05:00
|
|
|
//used in noop_fold_expr, and possibly elsewhere in the future
|
2013-02-26 20:42:00 -06:00
|
|
|
fn fold_mac_(m: mac, fld: @ast_fold) -> mac {
|
2013-02-18 00:20:36 -06:00
|
|
|
spanned {
|
|
|
|
node: match m.node { mac_invoc_tt(*) => copy m.node },
|
|
|
|
span: fld.new_span(m.span),
|
|
|
|
}
|
2011-07-08 18:35:09 -05:00
|
|
|
}
|
|
|
|
|
2013-03-01 09:01:48 -06:00
|
|
|
pub fn fold_fn_decl(decl: &ast::fn_decl, fld: @ast_fold) -> ast::fn_decl {
|
2013-01-15 18:05:20 -06:00
|
|
|
ast::fn_decl {
|
|
|
|
inputs: decl.inputs.map(|x| fold_arg_(*x, fld)),
|
|
|
|
output: fld.fold_ty(decl.output),
|
|
|
|
cf: decl.cf,
|
|
|
|
}
|
2011-12-20 13:03:21 -06:00
|
|
|
}
|
2011-06-20 19:25:49 -05:00
|
|
|
|
2013-03-01 09:01:48 -06:00
|
|
|
fn fold_ty_param_bound(tpb: &TyParamBound, fld: @ast_fold) -> TyParamBound {
|
2013-02-18 00:20:36 -06:00
|
|
|
match *tpb {
|
2013-01-10 13:16:54 -06:00
|
|
|
TraitTyParamBound(ty) => TraitTyParamBound(fld.fold_ty(ty)),
|
|
|
|
RegionTyParamBound => RegionTyParamBound
|
|
|
|
}
|
2012-02-14 17:21:53 -06:00
|
|
|
}
|
|
|
|
|
2013-02-26 20:42:00 -06:00
|
|
|
pub fn fold_ty_param(tp: TyParam,
|
|
|
|
fld: @ast_fold) -> TyParam {
|
2013-02-14 23:50:03 -06:00
|
|
|
TyParam {ident: tp.ident,
|
|
|
|
id: fld.new_id(tp.id),
|
2013-02-28 09:25:31 -06:00
|
|
|
bounds: @tp.bounds.map(|x| fold_ty_param_bound(x, fld))}
|
2012-02-14 17:21:53 -06:00
|
|
|
}
|
|
|
|
|
2013-02-14 23:50:03 -06:00
|
|
|
pub fn fold_ty_params(tps: &OptVec<TyParam>,
|
2013-02-26 20:42:00 -06:00
|
|
|
fld: @ast_fold) -> OptVec<TyParam> {
|
2013-02-14 23:50:03 -06:00
|
|
|
tps.map(|tp| fold_ty_param(*tp, fld))
|
|
|
|
}
|
|
|
|
|
2013-02-26 20:42:00 -06:00
|
|
|
pub fn fold_lifetime(l: &Lifetime,
|
|
|
|
fld: @ast_fold) -> Lifetime {
|
2013-02-14 23:50:03 -06:00
|
|
|
Lifetime {id: fld.new_id(l.id),
|
|
|
|
span: fld.new_span(l.span),
|
|
|
|
ident: l.ident}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn fold_lifetimes(lts: &OptVec<Lifetime>,
|
2013-02-26 20:42:00 -06:00
|
|
|
fld: @ast_fold) -> OptVec<Lifetime> {
|
2013-02-14 23:50:03 -06:00
|
|
|
lts.map(|l| fold_lifetime(l, fld))
|
2012-02-14 17:21:53 -06:00
|
|
|
}
|
|
|
|
|
2013-02-26 20:42:00 -06:00
|
|
|
pub fn fold_generics(generics: &Generics, fld: @ast_fold) -> Generics {
|
2013-02-14 23:50:03 -06:00
|
|
|
Generics {ty_params: fold_ty_params(&generics.ty_params, fld),
|
|
|
|
lifetimes: fold_lifetimes(&generics.lifetimes, fld)}
|
2012-02-14 17:21:53 -06:00
|
|
|
}
|
|
|
|
|
2013-03-01 09:01:48 -06:00
|
|
|
pub fn noop_fold_crate(c: &crate_, fld: @ast_fold) -> crate_ {
|
2012-06-30 18:19:07 -05:00
|
|
|
let fold_meta_item = |x| fold_meta_item_(x, fld);
|
|
|
|
let fold_attribute = |x| fold_attribute_(x, fld);
|
2011-06-20 19:25:49 -05:00
|
|
|
|
2013-01-14 21:06:59 -06:00
|
|
|
crate_ {
|
2013-02-18 00:20:36 -06:00
|
|
|
module: fld.fold_mod(&c.module),
|
2013-01-14 21:06:59 -06:00
|
|
|
attrs: c.attrs.map(|x| fold_attribute(*x)),
|
|
|
|
config: c.config.map(|x| fold_meta_item(*x)),
|
|
|
|
}
|
2011-06-20 19:25:49 -05:00
|
|
|
}
|
|
|
|
|
2013-02-26 20:42:00 -06:00
|
|
|
fn noop_fold_view_item(vi: view_item_, _fld: @ast_fold) -> view_item_ {
|
2012-08-01 19:30:05 -05:00
|
|
|
return /* FIXME (#2543) */ copy vi;
|
2011-06-20 19:25:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-03-01 09:01:48 -06:00
|
|
|
fn noop_fold_foreign_item(ni: @foreign_item, fld: @ast_fold)
|
2012-06-26 18:18:37 -05:00
|
|
|
-> @foreign_item {
|
2012-06-30 18:19:07 -05:00
|
|
|
let fold_arg = |x| fold_arg_(x, fld);
|
|
|
|
let fold_attribute = |x| fold_attribute_(x, fld);
|
2011-07-27 07:19:39 -05:00
|
|
|
|
2013-01-13 14:02:16 -06:00
|
|
|
@ast::foreign_item {
|
|
|
|
ident: fld.fold_ident(ni.ident),
|
2013-02-18 00:20:36 -06:00
|
|
|
attrs: ni.attrs.map(|x| fold_attribute(*x)),
|
2013-01-13 14:02:16 -06:00
|
|
|
node:
|
|
|
|
match ni.node {
|
2013-02-28 09:25:31 -06:00
|
|
|
foreign_item_fn(ref fdec, purity, ref generics) => {
|
2013-01-13 14:02:16 -06:00
|
|
|
foreign_item_fn(
|
2013-01-15 18:05:20 -06:00
|
|
|
ast::fn_decl {
|
2013-01-13 14:02:16 -06:00
|
|
|
inputs: fdec.inputs.map(|a| fold_arg(*a)),
|
|
|
|
output: fld.fold_ty(fdec.output),
|
|
|
|
cf: fdec.cf,
|
|
|
|
},
|
|
|
|
purity,
|
2013-02-14 23:50:03 -06:00
|
|
|
fold_generics(generics, fld))
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
2012-08-25 17:09:33 -05:00
|
|
|
foreign_item_const(t) => {
|
2013-01-13 14:02:16 -06:00
|
|
|
foreign_item_const(fld.fold_ty(t))
|
2012-08-25 17:09:33 -05:00
|
|
|
}
|
2013-01-13 14:02:16 -06:00
|
|
|
},
|
|
|
|
id: fld.new_id(ni.id),
|
|
|
|
span: fld.new_span(ni.span),
|
|
|
|
vis: ni.vis,
|
|
|
|
}
|
2011-06-20 19:25:49 -05:00
|
|
|
}
|
|
|
|
|
2013-03-01 09:01:48 -06:00
|
|
|
pub fn noop_fold_item(i: @item, fld: @ast_fold) -> Option<@item> {
|
2012-06-30 18:19:07 -05:00
|
|
|
let fold_attribute = |x| fold_attribute_(x, fld);
|
2011-06-20 19:25:49 -05:00
|
|
|
|
2013-01-13 15:13:41 -06:00
|
|
|
Some(@ast::item { ident: fld.fold_ident(i.ident),
|
|
|
|
attrs: i.attrs.map(|e| fold_attribute(*e)),
|
|
|
|
id: fld.new_id(i.id),
|
2013-02-18 00:20:36 -06:00
|
|
|
node: fld.fold_item_underscore(&i.node),
|
2013-01-13 15:13:41 -06:00
|
|
|
vis: i.vis,
|
|
|
|
span: fld.new_span(i.span) })
|
2011-06-20 19:25:49 -05:00
|
|
|
}
|
|
|
|
|
2013-03-01 09:01:48 -06:00
|
|
|
fn noop_fold_struct_field(sf: @struct_field, fld: @ast_fold)
|
2012-08-15 17:53:58 -05:00
|
|
|
-> @struct_field {
|
2013-01-13 17:28:49 -06:00
|
|
|
@spanned { node: ast::struct_field_ { kind: copy sf.node.kind,
|
|
|
|
id: sf.node.id,
|
|
|
|
ty: fld.fold_ty(sf.node.ty) },
|
2012-12-27 13:36:00 -06:00
|
|
|
span: sf.span }
|
2012-01-31 21:30:40 -06:00
|
|
|
}
|
|
|
|
|
2013-03-01 09:01:48 -06:00
|
|
|
pub fn noop_fold_item_underscore(i: &item_, fld: @ast_fold) -> item_ {
|
2013-02-18 00:20:36 -06:00
|
|
|
match *i {
|
2013-02-11 10:02:51 -06:00
|
|
|
item_const(t, e) => item_const(fld.fold_ty(t), fld.fold_expr(e)),
|
2013-02-28 09:25:31 -06:00
|
|
|
item_fn(ref decl, purity, ref generics, ref body) => {
|
2013-02-18 00:20:36 -06:00
|
|
|
item_fn(
|
|
|
|
fold_fn_decl(decl, fld),
|
|
|
|
purity,
|
2013-02-28 09:25:31 -06:00
|
|
|
fold_generics(generics, fld),
|
2013-02-18 00:20:36 -06:00
|
|
|
fld.fold_block(body)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
item_mod(ref m) => item_mod(fld.fold_mod(m)),
|
|
|
|
item_foreign_mod(ref nm) => {
|
|
|
|
item_foreign_mod(fld.fold_foreign_mod(nm))
|
|
|
|
}
|
2013-02-28 09:25:31 -06:00
|
|
|
item_ty(t, ref generics) => {
|
|
|
|
item_ty(fld.fold_ty(t), fold_generics(generics, fld))
|
2013-02-18 00:20:36 -06:00
|
|
|
}
|
2013-02-28 09:25:31 -06:00
|
|
|
item_enum(ref enum_definition, ref generics) => {
|
2013-02-18 00:20:36 -06:00
|
|
|
item_enum(
|
2013-03-07 20:04:21 -06:00
|
|
|
ast::enum_def {
|
|
|
|
variants: do enum_definition.variants.map |x| {
|
|
|
|
fld.fold_variant(x)
|
|
|
|
},
|
|
|
|
common: do enum_definition.common.map |x| {
|
|
|
|
fold_struct_def(*x, fld)
|
2013-02-18 00:20:36 -06:00
|
|
|
}
|
2013-03-07 20:04:21 -06:00
|
|
|
},
|
2013-02-28 09:25:31 -06:00
|
|
|
fold_generics(generics, fld))
|
2013-02-18 00:20:36 -06:00
|
|
|
}
|
2013-02-28 09:25:31 -06:00
|
|
|
item_struct(ref struct_def, ref generics) => {
|
2013-02-18 00:20:36 -06:00
|
|
|
let struct_def = fold_struct_def(*struct_def, fld);
|
2013-02-28 09:25:31 -06:00
|
|
|
item_struct(struct_def, /* FIXME (#2543) */ copy *generics)
|
2013-02-11 10:02:51 -06:00
|
|
|
}
|
2013-02-28 09:25:31 -06:00
|
|
|
item_impl(ref generics, ifce, ty, ref methods) => {
|
2013-02-18 00:20:36 -06:00
|
|
|
item_impl(
|
2013-02-28 09:25:31 -06:00
|
|
|
fold_generics(generics, fld),
|
2013-02-18 00:20:36 -06:00
|
|
|
ifce.map(|p| fold_trait_ref(*p, fld)),
|
|
|
|
fld.fold_ty(ty),
|
|
|
|
methods.map(|x| fld.fold_method(*x))
|
|
|
|
)
|
2013-02-11 10:02:51 -06:00
|
|
|
}
|
2013-02-28 09:25:31 -06:00
|
|
|
item_trait(ref generics, ref traits, ref methods) => {
|
2013-02-11 10:02:51 -06:00
|
|
|
let methods = do methods.map |method| {
|
|
|
|
match *method {
|
|
|
|
required(*) => copy *method,
|
|
|
|
provided(method) => provided(fld.fold_method(method))
|
|
|
|
}
|
|
|
|
};
|
2013-02-18 00:20:36 -06:00
|
|
|
item_trait(
|
2013-02-28 09:25:31 -06:00
|
|
|
fold_generics(generics, fld),
|
2013-02-18 00:20:36 -06:00
|
|
|
traits.map(|p| fold_trait_ref(*p, fld)),
|
|
|
|
methods
|
|
|
|
)
|
2013-02-11 10:02:51 -06:00
|
|
|
}
|
|
|
|
item_mac(ref m) => {
|
|
|
|
// FIXME #2888: we might actually want to do something here.
|
2013-02-24 20:32:02 -06:00
|
|
|
item_mac(copy *m)
|
2013-02-11 10:02:51 -06:00
|
|
|
}
|
|
|
|
}
|
2011-06-20 19:25:49 -05:00
|
|
|
}
|
|
|
|
|
2013-02-26 20:42:00 -06:00
|
|
|
fn fold_struct_def(struct_def: @ast::struct_def, fld: @ast_fold)
|
2012-08-08 21:51:19 -05:00
|
|
|
-> @ast::struct_def {
|
2012-09-21 21:37:57 -05:00
|
|
|
let dtor = do option::map(&struct_def.dtor) |dtor| {
|
2013-02-18 00:20:36 -06:00
|
|
|
let dtor_body = fld.fold_block(&dtor.node.body);
|
2012-08-08 21:51:19 -05:00
|
|
|
let dtor_id = fld.new_id(dtor.node.id);
|
2013-02-18 00:20:36 -06:00
|
|
|
spanned {
|
|
|
|
node: ast::struct_dtor_ {
|
|
|
|
body: dtor_body,
|
|
|
|
id: dtor_id,
|
|
|
|
.. copy dtor.node
|
|
|
|
},
|
|
|
|
span: copy dtor.span
|
|
|
|
}
|
2012-12-27 13:36:00 -06:00
|
|
|
};
|
2013-01-15 18:05:20 -06:00
|
|
|
@ast::struct_def {
|
|
|
|
fields: struct_def.fields.map(|f| fold_struct_field(*f, fld)),
|
2012-10-23 21:18:18 -05:00
|
|
|
dtor: dtor,
|
2013-01-15 18:05:20 -06:00
|
|
|
ctor_id: struct_def.ctor_id.map(|cid| fld.new_id(*cid)),
|
|
|
|
}
|
2012-08-08 21:51:19 -05:00
|
|
|
}
|
|
|
|
|
2013-03-01 09:01:48 -06:00
|
|
|
fn fold_trait_ref(p: @trait_ref, fld: @ast_fold) -> @trait_ref {
|
2013-01-15 18:05:20 -06:00
|
|
|
@ast::trait_ref {
|
|
|
|
path: fld.fold_path(p.path),
|
|
|
|
ref_id: fld.new_id(p.ref_id),
|
|
|
|
}
|
2012-04-13 14:22:35 -05:00
|
|
|
}
|
|
|
|
|
2013-03-01 09:01:48 -06:00
|
|
|
fn fold_struct_field(f: @struct_field, fld: @ast_fold) -> @struct_field {
|
2013-02-18 00:20:36 -06:00
|
|
|
@spanned {
|
|
|
|
node: ast::struct_field_ {
|
|
|
|
kind: copy f.node.kind,
|
|
|
|
id: fld.new_id(f.node.id),
|
|
|
|
ty: fld.fold_ty(f.node.ty),
|
|
|
|
},
|
|
|
|
span: fld.new_span(f.span),
|
|
|
|
}
|
2012-08-15 17:53:58 -05:00
|
|
|
}
|
|
|
|
|
2013-03-01 09:01:48 -06:00
|
|
|
fn noop_fold_method(m: @method, fld: @ast_fold) -> @method {
|
2013-01-15 18:05:20 -06:00
|
|
|
@ast::method {
|
|
|
|
ident: fld.fold_ident(m.ident),
|
|
|
|
attrs: /* FIXME (#2543) */ copy m.attrs,
|
2013-02-14 23:50:03 -06:00
|
|
|
generics: fold_generics(&m.generics, fld),
|
2013-01-15 18:05:20 -06:00
|
|
|
self_ty: m.self_ty,
|
|
|
|
purity: m.purity,
|
2013-02-18 00:20:36 -06:00
|
|
|
decl: fold_fn_decl(&m.decl, fld),
|
|
|
|
body: fld.fold_block(&m.body),
|
2013-01-15 18:05:20 -06:00
|
|
|
id: fld.new_id(m.id),
|
|
|
|
span: fld.new_span(m.span),
|
|
|
|
self_id: fld.new_id(m.self_id),
|
|
|
|
vis: m.vis,
|
|
|
|
}
|
2011-06-20 19:25:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-03-01 09:01:48 -06:00
|
|
|
pub fn noop_fold_block(b: &blk_, fld: @ast_fold) -> blk_ {
|
2013-01-14 21:35:08 -06:00
|
|
|
ast::blk_ {
|
|
|
|
view_items: b.view_items.map(|x| fld.fold_view_item(*x)),
|
|
|
|
stmts: b.stmts.map(|x| fld.fold_stmt(*x)),
|
|
|
|
expr: b.expr.map(|x| fld.fold_expr(*x)),
|
|
|
|
id: fld.new_id(b.id),
|
|
|
|
rules: b.rules,
|
|
|
|
}
|
2011-06-20 19:25:49 -05:00
|
|
|
}
|
|
|
|
|
2013-03-01 09:01:48 -06:00
|
|
|
fn noop_fold_stmt(s: &stmt_, fld: @ast_fold) -> stmt_ {
|
2012-11-12 22:06:55 -06:00
|
|
|
let fold_mac = |x| fold_mac_(x, fld);
|
2013-02-18 00:20:36 -06:00
|
|
|
match *s {
|
|
|
|
stmt_decl(d, nid) => stmt_decl(fld.fold_decl(d), fld.new_id(nid)),
|
|
|
|
stmt_expr(e, nid) => stmt_expr(fld.fold_expr(e), fld.new_id(nid)),
|
|
|
|
stmt_semi(e, nid) => stmt_semi(fld.fold_expr(e), fld.new_id(nid)),
|
|
|
|
stmt_mac(ref mac, semi) => stmt_mac(fold_mac((*mac)), semi)
|
|
|
|
}
|
2011-06-20 19:25:49 -05:00
|
|
|
}
|
|
|
|
|
2013-03-01 09:01:48 -06:00
|
|
|
fn noop_fold_arm(a: &arm, fld: @ast_fold) -> arm {
|
2013-01-14 22:52:28 -06:00
|
|
|
arm {
|
2013-02-18 00:20:36 -06:00
|
|
|
pats: a.pats.map(|x| fld.fold_pat(*x)),
|
|
|
|
guard: a.guard.map(|x| fld.fold_expr(*x)),
|
|
|
|
body: fld.fold_block(&a.body),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-01 09:01:48 -06:00
|
|
|
pub fn noop_fold_pat(p: &pat_, fld: @ast_fold) -> pat_ {
|
2013-02-18 00:20:36 -06:00
|
|
|
match *p {
|
|
|
|
pat_wild => pat_wild,
|
2013-02-24 18:56:49 -06:00
|
|
|
pat_ident(binding_mode, pth, ref sub) => {
|
2013-02-18 00:20:36 -06:00
|
|
|
pat_ident(
|
|
|
|
binding_mode,
|
|
|
|
fld.fold_path(pth),
|
|
|
|
sub.map(|x| fld.fold_pat(*x))
|
|
|
|
)
|
|
|
|
}
|
|
|
|
pat_lit(e) => pat_lit(fld.fold_expr(e)),
|
2013-02-24 18:56:49 -06:00
|
|
|
pat_enum(pth, ref pats) => {
|
2013-02-18 00:20:36 -06:00
|
|
|
pat_enum(
|
|
|
|
fld.fold_path(pth),
|
|
|
|
pats.map(|pats| pats.map(|x| fld.fold_pat(*x)))
|
|
|
|
)
|
|
|
|
}
|
2013-02-24 18:56:49 -06:00
|
|
|
pat_struct(pth, ref fields, etc) => {
|
2012-08-06 19:01:14 -05:00
|
|
|
let pth_ = fld.fold_path(pth);
|
2013-01-14 22:52:28 -06:00
|
|
|
let fs = do fields.map |f| {
|
|
|
|
ast::field_pat {
|
|
|
|
ident: /* FIXME (#2543) */ copy f.ident,
|
|
|
|
pat: fld.fold_pat(f.pat)
|
|
|
|
}
|
|
|
|
};
|
2012-08-06 19:01:14 -05:00
|
|
|
pat_struct(pth_, fs, etc)
|
2013-02-18 00:20:36 -06:00
|
|
|
}
|
|
|
|
pat_tup(ref elts) => pat_tup(elts.map(|x| fld.fold_pat(*x))),
|
|
|
|
pat_box(inner) => pat_box(fld.fold_pat(inner)),
|
|
|
|
pat_uniq(inner) => pat_uniq(fld.fold_pat(inner)),
|
|
|
|
pat_region(inner) => pat_region(fld.fold_pat(inner)),
|
|
|
|
pat_range(e1, e2) => {
|
2012-03-20 20:35:59 -05:00
|
|
|
pat_range(fld.fold_expr(e1), fld.fold_expr(e2))
|
2013-02-18 00:20:36 -06:00
|
|
|
},
|
2013-02-26 12:58:46 -06:00
|
|
|
pat_vec(ref before, ref slice, ref after) => {
|
2013-02-18 00:20:36 -06:00
|
|
|
pat_vec(
|
2013-02-26 12:58:46 -06:00
|
|
|
before.map(|x| fld.fold_pat(*x)),
|
|
|
|
slice.map(|x| fld.fold_pat(*x)),
|
|
|
|
after.map(|x| fld.fold_pat(*x))
|
2013-02-18 00:20:36 -06:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2011-06-20 19:25:49 -05:00
|
|
|
}
|
|
|
|
|
2013-03-01 09:01:48 -06:00
|
|
|
fn noop_fold_decl(d: &decl_, fld: @ast_fold) -> decl_ {
|
2013-02-18 00:20:36 -06:00
|
|
|
match *d {
|
2013-02-24 18:56:49 -06:00
|
|
|
decl_local(ref ls) => decl_local(ls.map(|x| fld.fold_local(*x))),
|
2013-02-18 00:20:36 -06:00
|
|
|
decl_item(it) => {
|
|
|
|
match fld.fold_item(it) {
|
|
|
|
Some(it_folded) => decl_item(it_folded),
|
|
|
|
None => decl_local(~[]),
|
|
|
|
}
|
|
|
|
}
|
2012-02-10 07:33:36 -06:00
|
|
|
}
|
2011-06-20 19:25:49 -05:00
|
|
|
}
|
|
|
|
|
2013-03-01 15:30:06 -06:00
|
|
|
pub fn wrap<T>(f: @fn(&T, ast_fold) -> T)
|
|
|
|
-> @fn(&T, span, ast_fold) -> (T, span) {
|
|
|
|
let result: @fn(&T, span, @ast_fold) -> (T, span) = |x, s, fld| {
|
2012-01-22 18:30:07 -06:00
|
|
|
(f(x, fld), s)
|
2013-03-01 15:30:06 -06:00
|
|
|
};
|
|
|
|
result
|
2012-01-22 18:30:07 -06:00
|
|
|
}
|
|
|
|
|
2013-03-01 09:01:48 -06:00
|
|
|
pub fn noop_fold_expr(e: &expr_, fld: @ast_fold) -> expr_ {
|
2013-02-26 20:42:00 -06:00
|
|
|
fn fold_field_(field: field, fld: @ast_fold) -> field {
|
2013-01-14 23:36:27 -06:00
|
|
|
spanned {
|
|
|
|
node: ast::field_ {
|
|
|
|
mutbl: field.node.mutbl,
|
|
|
|
ident: fld.fold_ident(field.node.ident),
|
|
|
|
expr: fld.fold_expr(field.node.expr),
|
|
|
|
},
|
|
|
|
span: fld.new_span(field.span),
|
|
|
|
}
|
2011-06-20 19:25:49 -05:00
|
|
|
}
|
2012-06-30 18:19:07 -05:00
|
|
|
let fold_field = |x| fold_field_(x, fld);
|
2011-07-13 17:44:09 -05:00
|
|
|
|
2012-06-30 18:19:07 -05:00
|
|
|
let fold_mac = |x| fold_mac_(x, fld);
|
2011-06-20 19:25:49 -05:00
|
|
|
|
2013-02-18 00:20:36 -06:00
|
|
|
match *e {
|
|
|
|
expr_vstore(e, v) => {
|
2012-04-09 19:32:49 -05:00
|
|
|
expr_vstore(fld.fold_expr(e), v)
|
2013-02-18 00:20:36 -06:00
|
|
|
}
|
|
|
|
expr_vec(ref exprs, mutt) => {
|
|
|
|
expr_vec(fld.map_exprs(|x| fld.fold_expr(x), *exprs), mutt)
|
|
|
|
}
|
|
|
|
expr_repeat(expr, count, mutt) => {
|
|
|
|
expr_repeat(fld.fold_expr(expr), fld.fold_expr(count), mutt)
|
|
|
|
}
|
|
|
|
expr_tup(ref elts) => expr_tup(elts.map(|x| fld.fold_expr(*x))),
|
|
|
|
expr_call(f, ref args, blk) => {
|
|
|
|
expr_call(
|
|
|
|
fld.fold_expr(f),
|
|
|
|
fld.map_exprs(|x| fld.fold_expr(x), *args),
|
|
|
|
blk
|
|
|
|
)
|
|
|
|
}
|
|
|
|
expr_method_call(f, i, ref tps, ref args, blk) => {
|
|
|
|
expr_method_call(
|
|
|
|
fld.fold_expr(f),
|
|
|
|
fld.fold_ident(i),
|
|
|
|
tps.map(|x| fld.fold_ty(*x)),
|
|
|
|
fld.map_exprs(|x| fld.fold_expr(x), *args),
|
|
|
|
blk
|
|
|
|
)
|
|
|
|
}
|
|
|
|
expr_binary(binop, lhs, rhs) => {
|
2011-06-20 19:25:49 -05:00
|
|
|
expr_binary(binop, fld.fold_expr(lhs), fld.fold_expr(rhs))
|
2013-02-18 00:20:36 -06:00
|
|
|
}
|
|
|
|
expr_unary(binop, ohs) => expr_unary(binop, fld.fold_expr(ohs)),
|
|
|
|
expr_loop_body(f) => expr_loop_body(fld.fold_expr(f)),
|
|
|
|
expr_do_body(f) => expr_do_body(fld.fold_expr(f)),
|
|
|
|
expr_lit(_) => copy *e,
|
|
|
|
expr_cast(expr, ty) => expr_cast(fld.fold_expr(expr), ty),
|
|
|
|
expr_addr_of(m, ohs) => expr_addr_of(m, fld.fold_expr(ohs)),
|
|
|
|
expr_if(cond, ref tr, fl) => {
|
|
|
|
expr_if(
|
|
|
|
fld.fold_expr(cond),
|
|
|
|
fld.fold_block(tr),
|
|
|
|
fl.map(|x| fld.fold_expr(*x))
|
|
|
|
)
|
|
|
|
}
|
|
|
|
expr_while(cond, ref body) => {
|
|
|
|
expr_while(fld.fold_expr(cond), fld.fold_block(body))
|
|
|
|
}
|
|
|
|
expr_loop(ref body, opt_ident) => {
|
|
|
|
expr_loop(
|
|
|
|
fld.fold_block(body),
|
|
|
|
opt_ident.map(|x| fld.fold_ident(*x))
|
|
|
|
)
|
|
|
|
}
|
|
|
|
expr_match(expr, ref arms) => {
|
|
|
|
expr_match(
|
|
|
|
fld.fold_expr(expr),
|
|
|
|
arms.map(|x| fld.fold_arm(x))
|
|
|
|
)
|
|
|
|
}
|
|
|
|
expr_fn_block(ref decl, ref body) => {
|
|
|
|
expr_fn_block(
|
|
|
|
fold_fn_decl(decl, fld),
|
|
|
|
fld.fold_block(body)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
expr_block(ref blk) => expr_block(fld.fold_block(blk)),
|
|
|
|
expr_copy(e) => expr_copy(fld.fold_expr(e)),
|
|
|
|
expr_assign(el, er) => {
|
2011-06-20 19:25:49 -05:00
|
|
|
expr_assign(fld.fold_expr(el), fld.fold_expr(er))
|
2013-02-18 00:20:36 -06:00
|
|
|
}
|
|
|
|
expr_swap(el, er) => {
|
2011-06-30 02:35:05 -05:00
|
|
|
expr_swap(fld.fold_expr(el), fld.fold_expr(er))
|
2013-02-18 00:20:36 -06:00
|
|
|
}
|
|
|
|
expr_assign_op(op, el, er) => {
|
2011-06-20 19:25:49 -05:00
|
|
|
expr_assign_op(op, fld.fold_expr(el), fld.fold_expr(er))
|
2013-02-18 00:20:36 -06:00
|
|
|
}
|
|
|
|
expr_field(el, id, ref tys) => {
|
|
|
|
expr_field(
|
|
|
|
fld.fold_expr(el), fld.fold_ident(id),
|
|
|
|
tys.map(|x| fld.fold_ty(*x))
|
|
|
|
)
|
|
|
|
}
|
|
|
|
expr_index(el, er) => {
|
2011-06-20 19:25:49 -05:00
|
|
|
expr_index(fld.fold_expr(el), fld.fold_expr(er))
|
2013-02-18 00:20:36 -06:00
|
|
|
}
|
|
|
|
expr_path(pth) => expr_path(fld.fold_path(pth)),
|
|
|
|
expr_break(ref opt_ident) => {
|
|
|
|
expr_break(opt_ident.map(|x| fld.fold_ident(*x)))
|
|
|
|
}
|
|
|
|
expr_again(ref opt_ident) => {
|
|
|
|
expr_again(opt_ident.map(|x| fld.fold_ident(*x)))
|
|
|
|
}
|
|
|
|
expr_ret(ref e) => {
|
|
|
|
expr_ret(e.map(|x| fld.fold_expr(*x)))
|
|
|
|
}
|
|
|
|
expr_log(i, lv, e) => {
|
|
|
|
expr_log(
|
|
|
|
i,
|
|
|
|
fld.fold_expr(lv),
|
|
|
|
fld.fold_expr(e)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
expr_mac(ref mac) => expr_mac(fold_mac((*mac))),
|
|
|
|
expr_struct(path, ref fields, maybe_expr) => {
|
|
|
|
expr_struct(
|
|
|
|
fld.fold_path(path),
|
|
|
|
fields.map(|x| fold_field(*x)),
|
|
|
|
maybe_expr.map(|x| fld.fold_expr(*x))
|
|
|
|
)
|
|
|
|
},
|
|
|
|
expr_paren(ex) => expr_paren(fld.fold_expr(ex))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-01 09:01:48 -06:00
|
|
|
pub fn noop_fold_ty(t: &ty_, fld: @ast_fold) -> ty_ {
|
2012-06-30 18:19:07 -05:00
|
|
|
let fold_mac = |x| fold_mac_(x, fld);
|
2013-03-01 09:01:48 -06:00
|
|
|
fn fold_mt(mt: &mt, fld: @ast_fold) -> mt {
|
2013-02-18 00:20:36 -06:00
|
|
|
mt {
|
|
|
|
ty: fld.fold_ty(mt.ty),
|
|
|
|
mutbl: mt.mutbl,
|
|
|
|
}
|
2012-02-01 22:21:01 -06:00
|
|
|
}
|
2013-02-26 20:42:00 -06:00
|
|
|
fn fold_field(f: ty_field, fld: @ast_fold) -> ty_field {
|
2013-01-15 17:03:49 -06:00
|
|
|
spanned {
|
|
|
|
node: ast::ty_field_ {
|
|
|
|
ident: fld.fold_ident(f.node.ident),
|
2013-02-18 00:20:36 -06:00
|
|
|
mt: fold_mt(&f.node.mt, fld),
|
2013-01-15 17:03:49 -06:00
|
|
|
},
|
|
|
|
span: fld.new_span(f.span),
|
|
|
|
}
|
2012-02-01 22:21:01 -06:00
|
|
|
}
|
2013-02-18 00:20:36 -06:00
|
|
|
match *t {
|
|
|
|
ty_nil | ty_bot | ty_infer => copy *t,
|
|
|
|
ty_box(ref mt) => ty_box(fold_mt(mt, fld)),
|
|
|
|
ty_uniq(ref mt) => ty_uniq(fold_mt(mt, fld)),
|
|
|
|
ty_vec(ref mt) => ty_vec(fold_mt(mt, fld)),
|
|
|
|
ty_ptr(ref mt) => ty_ptr(fold_mt(mt, fld)),
|
|
|
|
ty_rptr(region, ref mt) => ty_rptr(region, fold_mt(mt, fld)),
|
|
|
|
ty_closure(ref f) => {
|
|
|
|
ty_closure(@TyClosure {
|
|
|
|
sigil: f.sigil,
|
|
|
|
purity: f.purity,
|
|
|
|
region: f.region,
|
|
|
|
onceness: f.onceness,
|
|
|
|
decl: fold_fn_decl(&f.decl, fld)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
ty_bare_fn(ref f) => {
|
|
|
|
ty_bare_fn(@TyBareFn {
|
|
|
|
purity: f.purity,
|
|
|
|
abi: f.abi,
|
|
|
|
decl: fold_fn_decl(&f.decl, fld)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
ty_tup(ref tys) => ty_tup(tys.map(|ty| fld.fold_ty(*ty))),
|
|
|
|
ty_path(path, id) => ty_path(fld.fold_path(path), fld.new_id(id)),
|
|
|
|
ty_fixed_length_vec(ref mt, vs) => {
|
|
|
|
ty_fixed_length_vec(
|
|
|
|
fold_mt(mt, fld),
|
|
|
|
vs
|
|
|
|
)
|
|
|
|
}
|
|
|
|
ty_mac(ref mac) => ty_mac(fold_mac(*mac))
|
2012-02-01 22:21:01 -06:00
|
|
|
}
|
2011-06-20 19:25:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// ...nor do modules
|
2013-03-01 09:01:48 -06:00
|
|
|
pub fn noop_fold_mod(m: &_mod, fld: @ast_fold) -> _mod {
|
2013-01-15 18:05:20 -06:00
|
|
|
ast::_mod {
|
|
|
|
view_items: vec::map(m.view_items, |x| fld.fold_view_item(*x)),
|
2013-01-31 19:12:29 -06:00
|
|
|
items: vec::filter_mapped(m.items, |x| fld.fold_item(*x)),
|
2013-01-15 18:05:20 -06:00
|
|
|
}
|
2011-06-20 19:25:49 -05:00
|
|
|
}
|
|
|
|
|
2013-03-01 09:01:48 -06:00
|
|
|
fn noop_fold_foreign_mod(nm: &foreign_mod, fld: @ast_fold) -> foreign_mod {
|
2013-01-15 18:05:20 -06:00
|
|
|
ast::foreign_mod {
|
|
|
|
sort: nm.sort,
|
|
|
|
abi: nm.abi,
|
|
|
|
view_items: vec::map(nm.view_items, |x| fld.fold_view_item(*x)),
|
|
|
|
items: vec::map(nm.items, |x| fld.fold_foreign_item(*x)),
|
|
|
|
}
|
2011-06-20 19:25:49 -05:00
|
|
|
}
|
|
|
|
|
2013-03-01 09:01:48 -06:00
|
|
|
fn noop_fold_variant(v: &variant_, fld: @ast_fold) -> variant_ {
|
2013-02-26 20:42:00 -06:00
|
|
|
fn fold_variant_arg_(va: variant_arg, fld: @ast_fold) -> variant_arg {
|
2013-01-15 18:05:20 -06:00
|
|
|
ast::variant_arg { ty: fld.fold_ty(va.ty), id: fld.new_id(va.id) }
|
2011-06-20 19:25:49 -05:00
|
|
|
}
|
2012-06-30 18:19:07 -05:00
|
|
|
let fold_variant_arg = |x| fold_variant_arg_(x, fld);
|
2012-08-07 16:24:04 -05:00
|
|
|
|
|
|
|
let kind;
|
|
|
|
match v.kind {
|
2013-02-18 00:20:36 -06:00
|
|
|
tuple_variant_kind(ref variant_args) => {
|
|
|
|
kind = tuple_variant_kind(do variant_args.map |x| {
|
|
|
|
fold_variant_arg(*x)
|
|
|
|
})
|
|
|
|
}
|
2012-08-07 20:54:44 -05:00
|
|
|
struct_variant_kind(struct_def) => {
|
2012-09-21 21:37:57 -05:00
|
|
|
let dtor = do option::map(&struct_def.dtor) |dtor| {
|
2013-02-18 00:20:36 -06:00
|
|
|
let dtor_body = fld.fold_block(&dtor.node.body);
|
2012-08-07 20:54:44 -05:00
|
|
|
let dtor_id = fld.new_id(dtor.node.id);
|
2013-02-18 00:20:36 -06:00
|
|
|
spanned {
|
|
|
|
node: ast::struct_dtor_ {
|
|
|
|
body: dtor_body,
|
|
|
|
id: dtor_id,
|
|
|
|
.. copy dtor.node
|
|
|
|
},
|
|
|
|
.. copy *dtor
|
|
|
|
}
|
2012-12-27 13:36:00 -06:00
|
|
|
};
|
2013-01-13 15:45:57 -06:00
|
|
|
kind = struct_variant_kind(@ast::struct_def {
|
2012-08-15 17:53:58 -05:00
|
|
|
fields: vec::map(struct_def.fields,
|
2012-09-21 20:43:30 -05:00
|
|
|
|f| fld.fold_struct_field(*f)),
|
2012-10-23 21:18:18 -05:00
|
|
|
dtor: dtor,
|
2012-10-24 16:36:00 -05:00
|
|
|
ctor_id: option::map(&struct_def.ctor_id, |c| fld.new_id(*c))
|
2012-08-07 20:54:44 -05:00
|
|
|
})
|
|
|
|
}
|
2012-12-04 12:50:00 -06:00
|
|
|
enum_variant_kind(ref enum_definition) => {
|
2013-02-18 00:20:36 -06:00
|
|
|
let variants = do (*enum_definition).variants.map |x| {
|
|
|
|
fld.fold_variant(x)
|
|
|
|
};
|
|
|
|
let common = do (*enum_definition).common.map |x| {
|
|
|
|
fold_struct_def(*x, fld)
|
|
|
|
};
|
2013-01-15 18:05:20 -06:00
|
|
|
kind = enum_variant_kind(
|
2013-03-07 20:04:21 -06:00
|
|
|
ast::enum_def { variants: variants, common: common }
|
2013-01-15 18:05:20 -06:00
|
|
|
);
|
2012-08-08 16:17:52 -05:00
|
|
|
}
|
2012-08-07 16:24:04 -05:00
|
|
|
}
|
2012-01-25 18:23:43 -06:00
|
|
|
|
2012-06-30 18:19:07 -05:00
|
|
|
let fold_attribute = |x| fold_attribute_(x, fld);
|
2013-02-18 00:20:36 -06:00
|
|
|
let attrs = v.attrs.map(|x| fold_attribute(*x));
|
2012-01-25 18:23:43 -06:00
|
|
|
|
2012-08-06 14:34:08 -05:00
|
|
|
let de = match v.disr_expr {
|
2012-08-20 14:23:37 -05:00
|
|
|
Some(e) => Some(fld.fold_expr(e)),
|
|
|
|
None => None
|
2012-01-10 17:59:22 -06:00
|
|
|
};
|
2013-01-15 18:05:20 -06:00
|
|
|
ast::variant_ {
|
|
|
|
name: /* FIXME (#2543) */ copy v.name,
|
|
|
|
attrs: attrs,
|
|
|
|
kind: kind,
|
|
|
|
id: fld.new_id(v.id),
|
|
|
|
disr_expr: de,
|
|
|
|
vis: v.vis,
|
|
|
|
}
|
2011-06-20 19:25:49 -05:00
|
|
|
}
|
|
|
|
|
2013-03-01 09:01:48 -06:00
|
|
|
fn noop_fold_ident(i: ident, _fld: @ast_fold) -> ident {
|
2013-02-18 00:20:36 -06:00
|
|
|
/* FIXME (#2543) */ copy i
|
2012-06-07 23:53:47 -05:00
|
|
|
}
|
2011-06-20 19:25:49 -05:00
|
|
|
|
2013-03-01 09:01:48 -06:00
|
|
|
fn noop_fold_path(p: @path, fld: @ast_fold) -> path {
|
|
|
|
ast::path {
|
|
|
|
span: fld.new_span(p.span),
|
|
|
|
global: p.global,
|
|
|
|
idents: p.idents.map(|x| fld.fold_ident(*x)),
|
|
|
|
rp: p.rp,
|
|
|
|
types: p.types.map(|x| fld.fold_ty(*x)),
|
|
|
|
}
|
2011-06-20 19:25:49 -05:00
|
|
|
}
|
|
|
|
|
2013-03-01 09:01:48 -06:00
|
|
|
fn noop_fold_local(l: &local_, fld: @ast_fold) -> local_ {
|
2013-01-14 22:52:28 -06:00
|
|
|
local_ {
|
|
|
|
is_mutbl: l.is_mutbl,
|
|
|
|
ty: fld.fold_ty(l.ty),
|
|
|
|
pat: fld.fold_pat(l.pat),
|
|
|
|
init: l.init.map(|e| fld.fold_expr(*e)),
|
|
|
|
id: fld.new_id(l.id),
|
|
|
|
}
|
2011-06-20 19:25:49 -05:00
|
|
|
}
|
|
|
|
|
2011-08-12 09:15:18 -05:00
|
|
|
/* temporarily eta-expand because of a compiler bug with using `fn<T>` as a
|
2011-07-21 18:47:47 -05:00
|
|
|
value */
|
2013-03-01 15:30:06 -06:00
|
|
|
fn noop_map_exprs(f: @fn(@expr) -> @expr, es: &[@expr]) -> ~[@expr] {
|
2013-02-18 00:20:36 -06:00
|
|
|
es.map(|x| f(*x))
|
2011-07-21 18:47:47 -05:00
|
|
|
}
|
|
|
|
|
2012-08-01 19:30:05 -05:00
|
|
|
fn noop_id(i: node_id) -> node_id { return i; }
|
2011-08-09 12:56:32 -05:00
|
|
|
|
2012-08-01 19:30:05 -05:00
|
|
|
fn noop_span(sp: span) -> span { return sp; }
|
2011-08-15 15:33:12 -05:00
|
|
|
|
2013-01-29 16:41:40 -06:00
|
|
|
pub fn default_ast_fold() -> ast_fold_fns {
|
2013-02-18 00:20:36 -06:00
|
|
|
@AstFoldFns {
|
|
|
|
fold_crate: wrap(noop_fold_crate),
|
|
|
|
fold_view_item: noop_fold_view_item,
|
|
|
|
fold_foreign_item: noop_fold_foreign_item,
|
|
|
|
fold_item: noop_fold_item,
|
|
|
|
fold_struct_field: noop_fold_struct_field,
|
|
|
|
fold_item_underscore: noop_fold_item_underscore,
|
|
|
|
fold_method: noop_fold_method,
|
|
|
|
fold_block: wrap(noop_fold_block),
|
|
|
|
fold_stmt: wrap(noop_fold_stmt),
|
|
|
|
fold_arm: noop_fold_arm,
|
|
|
|
fold_pat: wrap(noop_fold_pat),
|
|
|
|
fold_decl: wrap(noop_fold_decl),
|
|
|
|
fold_expr: wrap(noop_fold_expr),
|
|
|
|
fold_ty: wrap(noop_fold_ty),
|
|
|
|
fold_mod: noop_fold_mod,
|
|
|
|
fold_foreign_mod: noop_fold_foreign_mod,
|
|
|
|
fold_variant: wrap(noop_fold_variant),
|
|
|
|
fold_ident: noop_fold_ident,
|
|
|
|
fold_path: noop_fold_path,
|
|
|
|
fold_local: wrap(noop_fold_local),
|
|
|
|
map_exprs: noop_map_exprs,
|
|
|
|
new_id: noop_id,
|
2013-03-01 09:01:48 -06:00
|
|
|
new_span: noop_span,
|
2013-02-18 00:20:36 -06:00
|
|
|
}
|
2011-06-20 19:25:49 -05:00
|
|
|
}
|
|
|
|
|
2013-02-26 20:42:00 -06:00
|
|
|
impl ast_fold for AstFoldFns {
|
2011-06-20 19:25:49 -05:00
|
|
|
/* naturally, a macro to write these would be nice */
|
2013-03-01 09:01:48 -06:00
|
|
|
fn fold_crate(@self, c: &crate) -> crate {
|
|
|
|
let (n, s) = (self.fold_crate)(&c.node, c.span, self as @ast_fold);
|
2012-12-27 13:36:00 -06:00
|
|
|
spanned { node: n, span: (self.new_span)(s) }
|
2011-06-20 19:25:49 -05:00
|
|
|
}
|
2013-03-01 09:01:48 -06:00
|
|
|
fn fold_view_item(@self, x: @view_item) ->
|
2011-07-27 07:19:39 -05:00
|
|
|
@view_item {
|
2013-01-13 18:51:48 -06:00
|
|
|
@ast::view_item {
|
2013-02-26 20:42:00 -06:00
|
|
|
node: (self.fold_view_item)(x.node, self as @ast_fold),
|
2013-01-13 18:51:48 -06:00
|
|
|
attrs: vec::map(x.attrs, |a|
|
2013-02-26 20:42:00 -06:00
|
|
|
fold_attribute_(*a, self as @ast_fold)),
|
2013-01-13 18:51:48 -06:00
|
|
|
vis: x.vis,
|
|
|
|
span: (self.new_span)(x.span),
|
|
|
|
}
|
2011-06-20 19:25:49 -05:00
|
|
|
}
|
2013-03-01 09:01:48 -06:00
|
|
|
fn fold_foreign_item(@self, x: @foreign_item) -> @foreign_item {
|
|
|
|
(self.fold_foreign_item)(x, self as @ast_fold)
|
2011-06-20 19:25:49 -05:00
|
|
|
}
|
2013-03-01 09:01:48 -06:00
|
|
|
fn fold_item(@self, i: @item) -> Option<@item> {
|
|
|
|
(self.fold_item)(i, self as @ast_fold)
|
2011-06-20 19:25:49 -05:00
|
|
|
}
|
2013-03-01 09:01:48 -06:00
|
|
|
fn fold_struct_field(@self, sf: @struct_field) -> @struct_field {
|
2013-01-13 17:28:49 -06:00
|
|
|
@spanned {
|
|
|
|
node: ast::struct_field_ {
|
|
|
|
kind: copy sf.node.kind,
|
|
|
|
id: sf.node.id,
|
2013-02-26 20:42:00 -06:00
|
|
|
ty: (self as @ast_fold).fold_ty(sf.node.ty),
|
2013-01-13 17:28:49 -06:00
|
|
|
},
|
|
|
|
span: (self.new_span)(sf.span),
|
|
|
|
}
|
2012-01-31 21:30:40 -06:00
|
|
|
}
|
2013-03-01 09:01:48 -06:00
|
|
|
fn fold_item_underscore(@self, i: &item_) -> item_ {
|
|
|
|
(self.fold_item_underscore)(i, self as @ast_fold)
|
2011-06-20 19:25:49 -05:00
|
|
|
}
|
2013-03-01 09:01:48 -06:00
|
|
|
fn fold_method(@self, x: @method) -> @method {
|
|
|
|
(self.fold_method)(x, self as @ast_fold)
|
2011-06-20 19:25:49 -05:00
|
|
|
}
|
2013-03-01 09:01:48 -06:00
|
|
|
fn fold_block(@self, x: &blk) -> blk {
|
|
|
|
let (n, s) = (self.fold_block)(&x.node, x.span, self as @ast_fold);
|
2012-12-27 13:36:00 -06:00
|
|
|
spanned { node: n, span: (self.new_span)(s) }
|
2011-06-20 19:25:49 -05:00
|
|
|
}
|
2013-03-01 09:01:48 -06:00
|
|
|
fn fold_stmt(@self, x: &stmt) -> @stmt {
|
|
|
|
let (n, s) = (self.fold_stmt)(&x.node, x.span, self as @ast_fold);
|
2012-12-27 13:36:00 -06:00
|
|
|
@spanned { node: n, span: (self.new_span)(s) }
|
2011-06-20 19:25:49 -05:00
|
|
|
}
|
2013-03-01 09:01:48 -06:00
|
|
|
fn fold_arm(@self, x: &arm) -> arm {
|
|
|
|
(self.fold_arm)(x, self as @ast_fold)
|
2011-06-20 19:25:49 -05:00
|
|
|
}
|
2013-03-01 09:01:48 -06:00
|
|
|
fn fold_pat(@self, x: @pat) -> @pat {
|
|
|
|
let (n, s) = (self.fold_pat)(&x.node, x.span, self as @ast_fold);
|
2013-01-14 22:52:28 -06:00
|
|
|
@pat {
|
|
|
|
id: (self.new_id)(x.id),
|
|
|
|
node: n,
|
|
|
|
span: (self.new_span)(s),
|
|
|
|
}
|
2011-06-20 19:25:49 -05:00
|
|
|
}
|
2013-03-01 09:01:48 -06:00
|
|
|
fn fold_decl(@self, x: @decl) -> @decl {
|
|
|
|
let (n, s) = (self.fold_decl)(&x.node, x.span, self as @ast_fold);
|
2012-12-27 13:36:00 -06:00
|
|
|
@spanned { node: n, span: (self.new_span)(s) }
|
2011-06-20 19:25:49 -05:00
|
|
|
}
|
2013-03-01 09:01:48 -06:00
|
|
|
fn fold_expr(@self, x: @expr) -> @expr {
|
|
|
|
let (n, s) = (self.fold_expr)(&x.node, x.span, self as @ast_fold);
|
2013-01-15 15:51:43 -06:00
|
|
|
@expr {
|
|
|
|
id: (self.new_id)(x.id),
|
|
|
|
callee_id: (self.new_id)(x.callee_id),
|
|
|
|
node: n,
|
|
|
|
span: (self.new_span)(s),
|
|
|
|
}
|
2011-06-20 19:25:49 -05:00
|
|
|
}
|
2013-03-01 09:01:48 -06:00
|
|
|
fn fold_ty(@self, x: @Ty) -> @Ty {
|
|
|
|
let (n, s) = (self.fold_ty)(&x.node, x.span, self as @ast_fold);
|
2013-01-15 16:59:39 -06:00
|
|
|
@Ty {
|
|
|
|
id: (self.new_id)(x.id),
|
|
|
|
node: n,
|
|
|
|
span: (self.new_span)(s),
|
|
|
|
}
|
2011-06-20 19:25:49 -05:00
|
|
|
}
|
2013-03-01 09:01:48 -06:00
|
|
|
fn fold_mod(@self, x: &_mod) -> _mod {
|
|
|
|
(self.fold_mod)(x, self as @ast_fold)
|
2011-06-20 19:25:49 -05:00
|
|
|
}
|
2013-03-01 09:01:48 -06:00
|
|
|
fn fold_foreign_mod(@self, x: &foreign_mod) -> foreign_mod {
|
|
|
|
(self.fold_foreign_mod)(x, self as @ast_fold)
|
2011-07-13 17:44:09 -05:00
|
|
|
}
|
2013-03-01 09:01:48 -06:00
|
|
|
fn fold_variant(@self, x: &variant) -> variant {
|
|
|
|
let (n, s) = (self.fold_variant)(&x.node, x.span, self as @ast_fold);
|
2012-12-27 13:36:00 -06:00
|
|
|
spanned { node: n, span: (self.new_span)(s) }
|
2012-05-21 20:28:39 -05:00
|
|
|
}
|
2013-03-01 09:01:48 -06:00
|
|
|
fn fold_ident(@self, x: ident) -> ident {
|
|
|
|
(self.fold_ident)(x, self as @ast_fold)
|
2012-05-21 20:28:39 -05:00
|
|
|
}
|
2013-03-01 09:01:48 -06:00
|
|
|
fn fold_path(@self, x: @path) -> @path {
|
|
|
|
@(self.fold_path)(x, self as @ast_fold)
|
2012-05-21 20:28:39 -05:00
|
|
|
}
|
2013-03-01 09:01:48 -06:00
|
|
|
fn fold_local(@self, x: @local) -> @local {
|
|
|
|
let (n, s) = (self.fold_local)(&x.node, x.span, self as @ast_fold);
|
2012-12-27 13:36:00 -06:00
|
|
|
@spanned { node: n, span: (self.new_span)(s) }
|
2012-05-21 20:28:39 -05:00
|
|
|
}
|
2013-03-01 15:30:06 -06:00
|
|
|
fn map_exprs(@self,
|
|
|
|
f: @fn(@expr) -> @expr,
|
|
|
|
e: &[@expr])
|
|
|
|
-> ~[@expr] {
|
2012-11-29 19:51:16 -06:00
|
|
|
(self.map_exprs)(f, e)
|
2012-05-21 20:28:39 -05:00
|
|
|
}
|
2013-02-26 20:42:00 -06:00
|
|
|
fn new_id(@self, node_id: ast::node_id) -> node_id {
|
2012-11-29 19:51:16 -06:00
|
|
|
(self.new_id)(node_id)
|
2012-05-21 20:28:39 -05:00
|
|
|
}
|
2013-02-26 20:42:00 -06:00
|
|
|
fn new_span(@self, span: span) -> span {
|
2012-11-29 19:51:16 -06:00
|
|
|
(self.new_span)(span)
|
2012-05-21 20:28:39 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-29 16:41:40 -06:00
|
|
|
pub impl ast_fold {
|
2013-03-02 15:31:21 -06:00
|
|
|
fn fold_attributes(&self, attrs: ~[attribute]) -> ~[attribute] {
|
|
|
|
attrs.map(|x| fold_attribute_(*x, *self))
|
2012-07-16 21:16:19 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-29 16:41:40 -06:00
|
|
|
pub fn make_fold(afp: ast_fold_fns) -> ast_fold {
|
2013-02-26 20:42:00 -06:00
|
|
|
afp as @ast_fold
|
2011-06-20 19:25:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Local Variables:
|
|
|
|
// mode: rust
|
|
|
|
// fill-column: 78;
|
|
|
|
// indent-tabs-mode: nil
|
|
|
|
// c-basic-offset: 4
|
|
|
|
// buffer-file-coding-system: utf-8-unix
|
|
|
|
// End:
|
|
|
|
//
|