Skip to content

librustc: Change fold to use traits instead of @fn. #8864

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 36 additions & 26 deletions src/librustc/front/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@


use std::option;
use syntax::fold::ast_fold;
use syntax::{ast, fold, attr};

type in_cfg_pred = @fn(attrs: &[ast::Attribute]) -> bool;
Expand All @@ -26,21 +27,34 @@ pub fn strip_unconfigured_items(crate: @ast::Crate) -> @ast::Crate {
}
}

pub fn strip_items(crate: &ast::Crate, in_cfg: in_cfg_pred)
-> @ast::Crate {
struct ItemRemover {
ctxt: @Context,
}

let ctxt = @Context { in_cfg: in_cfg };
impl fold::ast_fold for ItemRemover {
fn fold_mod(&self, module: &ast::_mod) -> ast::_mod {
fold_mod(self.ctxt, module, self)
}
fn fold_block(&self, block: &ast::Block) -> ast::Block {
fold_block(self.ctxt, block, self)
}
fn fold_foreign_mod(&self, foreign_module: &ast::foreign_mod)
-> ast::foreign_mod {
fold_foreign_mod(self.ctxt, foreign_module, self)
}
fn fold_item_underscore(&self, item: &ast::item_) -> ast::item_ {
fold_item_underscore(self.ctxt, item, self)
}
}

let precursor = @fold::AstFoldFns {
fold_mod: |a,b| fold_mod(ctxt, a, b),
fold_block: |a,b| fold_block(ctxt, a, b),
fold_foreign_mod: |a,b| fold_foreign_mod(ctxt, a, b),
fold_item_underscore: |a,b| fold_item_underscore(ctxt, a, b),
.. *fold::default_ast_fold()
pub fn strip_items(crate: &ast::Crate, in_cfg: in_cfg_pred) -> @ast::Crate {
let ctxt = @Context {
in_cfg: in_cfg,
};

let fold = fold::make_fold(precursor);
@fold.fold_crate(crate)
let precursor = ItemRemover {
ctxt: ctxt,
};
@precursor.fold_crate(crate)
}

fn filter_item(cx: @Context, item: @ast::item) ->
Expand All @@ -56,7 +70,7 @@ fn filter_view_item<'r>(cx: @Context, view_item: &'r ast::view_item)-> Option<&'
}
}

fn fold_mod(cx: @Context, m: &ast::_mod, fld: @fold::ast_fold) -> ast::_mod {
fn fold_mod(cx: @Context, m: &ast::_mod, fld: &ItemRemover) -> ast::_mod {
let filtered_items = do m.items.iter().filter_map |a| {
filter_item(cx, *a).chain(|x| fld.fold_item(x))
}.collect();
Expand All @@ -78,12 +92,12 @@ fn filter_foreign_item(cx: @Context, item: @ast::foreign_item) ->
} else { option::None }
}

fn fold_foreign_mod(
cx: @Context,
nm: &ast::foreign_mod,
fld: @fold::ast_fold
) -> ast::foreign_mod {
let filtered_items = nm.items.iter().filter_map(|a| filter_foreign_item(cx, *a)).collect();
fn fold_foreign_mod(cx: @Context, nm: &ast::foreign_mod, fld: &ItemRemover)
-> ast::foreign_mod {
let filtered_items = nm.items
.iter()
.filter_map(|a| filter_foreign_item(cx, *a))
.collect();
let filtered_view_items = do nm.view_items.iter().filter_map |a| {
do filter_view_item(cx, a).map_move |x| {
fld.fold_view_item(x)
Expand All @@ -97,8 +111,8 @@ fn fold_foreign_mod(
}
}

fn fold_item_underscore(cx: @Context, item: &ast::item_,
fld: @fold::ast_fold) -> ast::item_ {
fn fold_item_underscore(cx: @Context, item: &ast::item_, fld: &ItemRemover)
-> ast::item_ {
let item = match *item {
ast::item_impl(ref a, ref b, ref c, ref methods) => {
let methods = methods.iter().filter(|m| method_in_cfg(cx, **m))
Expand Down Expand Up @@ -133,11 +147,7 @@ fn filter_stmt(cx: @Context, stmt: @ast::stmt) ->
}
}

fn fold_block(
cx: @Context,
b: &ast::Block,
fld: @fold::ast_fold
) -> ast::Block {
fn fold_block(cx: @Context, b: &ast::Block, fld: &ItemRemover) -> ast::Block {
let resulting_stmts = do b.stmts.iter().filter_map |a| {
filter_stmt(cx, *a).chain(|stmt| fld.fold_stmt(stmt))
}.collect();
Expand Down
178 changes: 95 additions & 83 deletions src/librustc/front/std_inject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use syntax::ast;
use syntax::attr;
use syntax::codemap::dummy_sp;
use syntax::codemap;
use syntax::fold::ast_fold;
use syntax::fold;
use syntax::opt_vec;

Expand All @@ -38,91 +39,102 @@ fn no_prelude(attrs: &[ast::Attribute]) -> bool {
attr::contains_name(attrs, "no_implicit_prelude")
}

fn inject_libstd_ref(sess: Session, crate: &ast::Crate) -> @ast::Crate {
fn spanned<T>(x: T) -> codemap::spanned<T> {
codemap::spanned { node: x, span: dummy_sp() }
fn spanned<T>(x: T) -> codemap::spanned<T> {
codemap::spanned { node: x, span: dummy_sp() }
}

struct StandardLibraryInjector {
sess: Session,
}

impl fold::ast_fold for StandardLibraryInjector {
fn fold_crate(&self, crate: &ast::Crate) -> ast::Crate {
let n1 = self.sess.next_node_id();
let version = STD_VERSION.to_managed();
let vi1 = ast::view_item {
node: ast::view_item_extern_mod(self.sess.ident_of("std"),
None,
~[],
n1),
attrs: ~[
attr::mk_attr(attr::mk_name_value_item_str(@"vers", version))
],
vis: ast::private,
span: dummy_sp()
};

let vis = vec::append(~[vi1], crate.module.view_items);
let mut new_module = ast::_mod {
view_items: vis,
..crate.module.clone()
};

if !no_prelude(crate.attrs) {
// only add `use std::prelude::*;` if there wasn't a
// `#[no_implicit_prelude];` at the crate level.
new_module = self.fold_mod(&new_module);
}

// FIXME #2543: Bad copy.
ast::Crate {
module: new_module,
..(*crate).clone()
}
}

let precursor = @fold::AstFoldFns {
fold_crate: |crate, fld| {
let n1 = sess.next_node_id();
let vi1 = ast::view_item {
node: ast::view_item_extern_mod(
sess.ident_of("std"), None, ~[], n1),
attrs: ~[
attr::mk_attr(
attr::mk_name_value_item_str(@"vers", STD_VERSION.to_managed()))
],
vis: ast::private,
span: dummy_sp()
};

let vis = vec::append(~[vi1], crate.module.view_items);
let mut new_module = ast::_mod {
view_items: vis,
..crate.module.clone()
};

if !no_prelude(crate.attrs) {
// only add `use std::prelude::*;` if there wasn't a
// `#[no_implicit_prelude];` at the crate level.
new_module = fld.fold_mod(&new_module);
}

// FIXME #2543: Bad copy.
ast::Crate {
module: new_module,
..(*crate).clone()
}
},
fold_item: |item, fld| {
if !no_prelude(item.attrs) {
// only recur if there wasn't `#[no_implicit_prelude];`
// on this item, i.e. this means that the prelude is not
// implicitly imported though the whole subtree
fold::noop_fold_item(item, fld)
} else {
Some(item)
}
},
fold_mod: |module, fld| {
let n2 = sess.next_node_id();

let prelude_path = ast::Path {
span: dummy_sp(),
global: false,
segments: ~[
ast::PathSegment {
identifier: sess.ident_of("std"),
lifetime: None,
types: opt_vec::Empty,
},
ast::PathSegment {
identifier: sess.ident_of("prelude"),
lifetime: None,
types: opt_vec::Empty,
},
],
};

let vp = @spanned(ast::view_path_glob(prelude_path, n2));
let vi2 = ast::view_item { node: ast::view_item_use(~[vp]),
attrs: ~[],
vis: ast::private,
span: dummy_sp() };

let vis = vec::append(~[vi2], module.view_items);

// FIXME #2543: Bad copy.
let new_module = ast::_mod {
view_items: vis,
..(*module).clone()
};
fold::noop_fold_mod(&new_module, fld)
},
..*fold::default_ast_fold()
};
fn fold_item(&self, item: @ast::item) -> Option<@ast::item> {
if !no_prelude(item.attrs) {
// only recur if there wasn't `#[no_implicit_prelude];`
// on this item, i.e. this means that the prelude is not
// implicitly imported though the whole subtree
fold::noop_fold_item(item, self)
} else {
Some(item)
}
}

fn fold_mod(&self, module: &ast::_mod) -> ast::_mod {
let n2 = self.sess.next_node_id();

let prelude_path = ast::Path {
span: dummy_sp(),
global: false,
segments: ~[
ast::PathSegment {
identifier: self.sess.ident_of("std"),
lifetime: None,
types: opt_vec::Empty,
},
ast::PathSegment {
identifier: self.sess.ident_of("prelude"),
lifetime: None,
types: opt_vec::Empty,
},
],
};

let vp = @spanned(ast::view_path_glob(prelude_path, n2));
let vi2 = ast::view_item {
node: ast::view_item_use(~[vp]),
attrs: ~[],
vis: ast::private,
span: dummy_sp(),
};

let vis = vec::append(~[vi2], module.view_items);

// FIXME #2543: Bad copy.
let new_module = ast::_mod {
view_items: vis,
..(*module).clone()
};
fold::noop_fold_mod(&new_module, self)
}
}

let fold = fold::make_fold(precursor);
fn inject_libstd_ref(sess: Session, crate: &ast::Crate) -> @ast::Crate {
let fold = StandardLibraryInjector {
sess: sess,
};
@fold.fold_crate(crate)
}
Loading