Skip to content

Commit aa51f49

Browse files
committed
librustc: Change fold to use traits instead of @fn.
1 parent a6835dd commit aa51f49

File tree

10 files changed

+1068
-1048
lines changed

10 files changed

+1068
-1048
lines changed

src/librustc/front/config.rs

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111

1212
use std::option;
13+
use syntax::fold::ast_fold;
1314
use syntax::{ast, fold, attr};
1415

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

29-
pub fn strip_items(crate: &ast::Crate, in_cfg: in_cfg_pred)
30-
-> @ast::Crate {
30+
struct ItemRemover {
31+
ctxt: @Context,
32+
}
3133

32-
let ctxt = @Context { in_cfg: in_cfg };
34+
impl fold::ast_fold for ItemRemover {
35+
fn fold_mod(&self, module: &ast::_mod) -> ast::_mod {
36+
fold_mod(self.ctxt, module, self)
37+
}
38+
fn fold_block(&self, block: &ast::Block) -> ast::Block {
39+
fold_block(self.ctxt, block, self)
40+
}
41+
fn fold_foreign_mod(&self, foreign_module: &ast::foreign_mod)
42+
-> ast::foreign_mod {
43+
fold_foreign_mod(self.ctxt, foreign_module, self)
44+
}
45+
fn fold_item_underscore(&self, item: &ast::item_) -> ast::item_ {
46+
fold_item_underscore(self.ctxt, item, self)
47+
}
48+
}
3349

34-
let precursor = @fold::AstFoldFns {
35-
fold_mod: |a,b| fold_mod(ctxt, a, b),
36-
fold_block: |a,b| fold_block(ctxt, a, b),
37-
fold_foreign_mod: |a,b| fold_foreign_mod(ctxt, a, b),
38-
fold_item_underscore: |a,b| fold_item_underscore(ctxt, a, b),
39-
.. *fold::default_ast_fold()
50+
pub fn strip_items(crate: &ast::Crate, in_cfg: in_cfg_pred) -> @ast::Crate {
51+
let ctxt = @Context {
52+
in_cfg: in_cfg,
4053
};
41-
42-
let fold = fold::make_fold(precursor);
43-
@fold.fold_crate(crate)
54+
let precursor = ItemRemover {
55+
ctxt: ctxt,
56+
};
57+
@precursor.fold_crate(crate)
4458
}
4559

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

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

81-
fn fold_foreign_mod(
82-
cx: @Context,
83-
nm: &ast::foreign_mod,
84-
fld: @fold::ast_fold
85-
) -> ast::foreign_mod {
86-
let filtered_items = nm.items.iter().filter_map(|a| filter_foreign_item(cx, *a)).collect();
95+
fn fold_foreign_mod(cx: @Context, nm: &ast::foreign_mod, fld: &ItemRemover)
96+
-> ast::foreign_mod {
97+
let filtered_items = nm.items
98+
.iter()
99+
.filter_map(|a| filter_foreign_item(cx, *a))
100+
.collect();
87101
let filtered_view_items = do nm.view_items.iter().filter_map |a| {
88102
do filter_view_item(cx, a).map_move |x| {
89103
fld.fold_view_item(x)
@@ -97,8 +111,8 @@ fn fold_foreign_mod(
97111
}
98112
}
99113

100-
fn fold_item_underscore(cx: @Context, item: &ast::item_,
101-
fld: @fold::ast_fold) -> ast::item_ {
114+
fn fold_item_underscore(cx: @Context, item: &ast::item_, fld: &ItemRemover)
115+
-> ast::item_ {
102116
let item = match *item {
103117
ast::item_impl(ref a, ref b, ref c, ref methods) => {
104118
let methods = methods.iter().filter(|m| method_in_cfg(cx, **m))
@@ -133,11 +147,7 @@ fn filter_stmt(cx: @Context, stmt: @ast::stmt) ->
133147
}
134148
}
135149

136-
fn fold_block(
137-
cx: @Context,
138-
b: &ast::Block,
139-
fld: @fold::ast_fold
140-
) -> ast::Block {
150+
fn fold_block(cx: @Context, b: &ast::Block, fld: &ItemRemover) -> ast::Block {
141151
let resulting_stmts = do b.stmts.iter().filter_map |a| {
142152
filter_stmt(cx, *a).chain(|stmt| fld.fold_stmt(stmt))
143153
}.collect();

src/librustc/front/std_inject.rs

Lines changed: 95 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use syntax::ast;
1616
use syntax::attr;
1717
use syntax::codemap::dummy_sp;
1818
use syntax::codemap;
19+
use syntax::fold::ast_fold;
1920
use syntax::fold;
2021
use syntax::opt_vec;
2122

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

41-
fn inject_libstd_ref(sess: Session, crate: &ast::Crate) -> @ast::Crate {
42-
fn spanned<T>(x: T) -> codemap::spanned<T> {
43-
codemap::spanned { node: x, span: dummy_sp() }
42+
fn spanned<T>(x: T) -> codemap::spanned<T> {
43+
codemap::spanned { node: x, span: dummy_sp() }
44+
}
45+
46+
struct StandardLibraryInjector {
47+
sess: Session,
48+
}
49+
50+
impl fold::ast_fold for StandardLibraryInjector {
51+
fn fold_crate(&self, crate: &ast::Crate) -> ast::Crate {
52+
let n1 = self.sess.next_node_id();
53+
let version = STD_VERSION.to_managed();
54+
let vi1 = ast::view_item {
55+
node: ast::view_item_extern_mod(self.sess.ident_of("std"),
56+
None,
57+
~[],
58+
n1),
59+
attrs: ~[
60+
attr::mk_attr(attr::mk_name_value_item_str(@"vers", version))
61+
],
62+
vis: ast::private,
63+
span: dummy_sp()
64+
};
65+
66+
let vis = vec::append(~[vi1], crate.module.view_items);
67+
let mut new_module = ast::_mod {
68+
view_items: vis,
69+
..crate.module.clone()
70+
};
71+
72+
if !no_prelude(crate.attrs) {
73+
// only add `use std::prelude::*;` if there wasn't a
74+
// `#[no_implicit_prelude];` at the crate level.
75+
new_module = self.fold_mod(&new_module);
76+
}
77+
78+
// FIXME #2543: Bad copy.
79+
ast::Crate {
80+
module: new_module,
81+
..(*crate).clone()
82+
}
4483
}
4584

46-
let precursor = @fold::AstFoldFns {
47-
fold_crate: |crate, fld| {
48-
let n1 = sess.next_node_id();
49-
let vi1 = ast::view_item {
50-
node: ast::view_item_extern_mod(
51-
sess.ident_of("std"), None, ~[], n1),
52-
attrs: ~[
53-
attr::mk_attr(
54-
attr::mk_name_value_item_str(@"vers", STD_VERSION.to_managed()))
55-
],
56-
vis: ast::private,
57-
span: dummy_sp()
58-
};
59-
60-
let vis = vec::append(~[vi1], crate.module.view_items);
61-
let mut new_module = ast::_mod {
62-
view_items: vis,
63-
..crate.module.clone()
64-
};
65-
66-
if !no_prelude(crate.attrs) {
67-
// only add `use std::prelude::*;` if there wasn't a
68-
// `#[no_implicit_prelude];` at the crate level.
69-
new_module = fld.fold_mod(&new_module);
70-
}
71-
72-
// FIXME #2543: Bad copy.
73-
ast::Crate {
74-
module: new_module,
75-
..(*crate).clone()
76-
}
77-
},
78-
fold_item: |item, fld| {
79-
if !no_prelude(item.attrs) {
80-
// only recur if there wasn't `#[no_implicit_prelude];`
81-
// on this item, i.e. this means that the prelude is not
82-
// implicitly imported though the whole subtree
83-
fold::noop_fold_item(item, fld)
84-
} else {
85-
Some(item)
86-
}
87-
},
88-
fold_mod: |module, fld| {
89-
let n2 = sess.next_node_id();
90-
91-
let prelude_path = ast::Path {
92-
span: dummy_sp(),
93-
global: false,
94-
segments: ~[
95-
ast::PathSegment {
96-
identifier: sess.ident_of("std"),
97-
lifetime: None,
98-
types: opt_vec::Empty,
99-
},
100-
ast::PathSegment {
101-
identifier: sess.ident_of("prelude"),
102-
lifetime: None,
103-
types: opt_vec::Empty,
104-
},
105-
],
106-
};
107-
108-
let vp = @spanned(ast::view_path_glob(prelude_path, n2));
109-
let vi2 = ast::view_item { node: ast::view_item_use(~[vp]),
110-
attrs: ~[],
111-
vis: ast::private,
112-
span: dummy_sp() };
113-
114-
let vis = vec::append(~[vi2], module.view_items);
115-
116-
// FIXME #2543: Bad copy.
117-
let new_module = ast::_mod {
118-
view_items: vis,
119-
..(*module).clone()
120-
};
121-
fold::noop_fold_mod(&new_module, fld)
122-
},
123-
..*fold::default_ast_fold()
124-
};
85+
fn fold_item(&self, item: @ast::item) -> Option<@ast::item> {
86+
if !no_prelude(item.attrs) {
87+
// only recur if there wasn't `#[no_implicit_prelude];`
88+
// on this item, i.e. this means that the prelude is not
89+
// implicitly imported though the whole subtree
90+
fold::noop_fold_item(item, self)
91+
} else {
92+
Some(item)
93+
}
94+
}
95+
96+
fn fold_mod(&self, module: &ast::_mod) -> ast::_mod {
97+
let n2 = self.sess.next_node_id();
98+
99+
let prelude_path = ast::Path {
100+
span: dummy_sp(),
101+
global: false,
102+
segments: ~[
103+
ast::PathSegment {
104+
identifier: self.sess.ident_of("std"),
105+
lifetime: None,
106+
types: opt_vec::Empty,
107+
},
108+
ast::PathSegment {
109+
identifier: self.sess.ident_of("prelude"),
110+
lifetime: None,
111+
types: opt_vec::Empty,
112+
},
113+
],
114+
};
115+
116+
let vp = @spanned(ast::view_path_glob(prelude_path, n2));
117+
let vi2 = ast::view_item {
118+
node: ast::view_item_use(~[vp]),
119+
attrs: ~[],
120+
vis: ast::private,
121+
span: dummy_sp(),
122+
};
123+
124+
let vis = vec::append(~[vi2], module.view_items);
125+
126+
// FIXME #2543: Bad copy.
127+
let new_module = ast::_mod {
128+
view_items: vis,
129+
..(*module).clone()
130+
};
131+
fold::noop_fold_mod(&new_module, self)
132+
}
133+
}
125134

126-
let fold = fold::make_fold(precursor);
135+
fn inject_libstd_ref(sess: Session, crate: &ast::Crate) -> @ast::Crate {
136+
let fold = StandardLibraryInjector {
137+
sess: sess,
138+
};
127139
@fold.fold_crate(crate)
128140
}

0 commit comments

Comments
 (0)