Skip to content

Commit 7a42e46

Browse files
committed
Refactor the syntax::config::fold_* functions into methods
1 parent 29c1059 commit 7a42e46

File tree

1 file changed

+81
-147
lines changed

1 file changed

+81
-147
lines changed

src/libsyntax/config.rs

Lines changed: 81 additions & 147 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,71 @@ pub fn strip_unconfigured_items(diagnostic: &Handler, krate: ast::Crate,
5050

5151
impl<'a, F> fold::Folder for Context<'a, F> where F: FnMut(&[ast::Attribute]) -> bool {
5252
fn fold_foreign_mod(&mut self, foreign_mod: ast::ForeignMod) -> ast::ForeignMod {
53-
fold_foreign_mod(self, foreign_mod)
53+
ast::ForeignMod {
54+
abi: foreign_mod.abi,
55+
items: foreign_mod.items.into_iter().filter(|item| {
56+
(self.in_cfg)(&item.attrs)
57+
}).collect(),
58+
}
5459
}
60+
5561
fn fold_item_kind(&mut self, item: ast::ItemKind) -> ast::ItemKind {
56-
fold_item_kind(self, item)
62+
let fold_struct = |this: &mut Self, vdata| match vdata {
63+
ast::VariantData::Struct(fields, id) => {
64+
ast::VariantData::Struct(fields.into_iter().filter(|m| {
65+
(this.in_cfg)(&m.attrs)
66+
}).collect(), id)
67+
}
68+
ast::VariantData::Tuple(fields, id) => {
69+
ast::VariantData::Tuple(fields.into_iter().filter(|m| {
70+
(this.in_cfg)(&m.attrs)
71+
}).collect(), id)
72+
}
73+
ast::VariantData::Unit(id) => ast::VariantData::Unit(id)
74+
};
75+
76+
let item = match item {
77+
ast::ItemKind::Impl(u, o, a, b, c, impl_items) => {
78+
let impl_items = impl_items.into_iter()
79+
.filter(|ii| (self.in_cfg)(&ii.attrs))
80+
.collect();
81+
ast::ItemKind::Impl(u, o, a, b, c, impl_items)
82+
}
83+
ast::ItemKind::Trait(u, a, b, methods) => {
84+
let methods = methods.into_iter()
85+
.filter(|ti| (self.in_cfg)(&ti.attrs))
86+
.collect();
87+
ast::ItemKind::Trait(u, a, b, methods)
88+
}
89+
ast::ItemKind::Struct(def, generics) => {
90+
ast::ItemKind::Struct(fold_struct(self, def), generics)
91+
}
92+
ast::ItemKind::Enum(def, generics) => {
93+
let variants = def.variants.into_iter().filter_map(|v| {
94+
if !(self.in_cfg)(&v.node.attrs) {
95+
None
96+
} else {
97+
Some(Spanned {
98+
node: ast::Variant_ {
99+
name: v.node.name,
100+
attrs: v.node.attrs,
101+
data: fold_struct(self, v.node.data),
102+
disr_expr: v.node.disr_expr,
103+
},
104+
span: v.span
105+
})
106+
}
107+
});
108+
ast::ItemKind::Enum(ast::EnumDef {
109+
variants: variants.collect(),
110+
}, generics)
111+
}
112+
item => item,
113+
};
114+
115+
fold::noop_fold_item_kind(item, self)
57116
}
117+
58118
fn fold_expr(&mut self, expr: P<ast::Expr>) -> P<ast::Expr> {
59119
// If an expr is valid to cfg away it will have been removed by the
60120
// outer stmt or expression folder before descending in here.
@@ -69,17 +129,33 @@ impl<'a, F> fold::Folder for Context<'a, F> where F: FnMut(&[ast::Attribute]) ->
69129
}
70130
fold_expr(self, expr)
71131
}
132+
72133
fn fold_opt_expr(&mut self, expr: P<ast::Expr>) -> Option<P<ast::Expr>> {
73-
fold_opt_expr(self, expr)
134+
if (self.in_cfg)(expr.attrs()) {
135+
Some(fold_expr(self, expr))
136+
} else {
137+
None
138+
}
74139
}
140+
75141
fn fold_stmt(&mut self, stmt: ast::Stmt) -> SmallVector<ast::Stmt> {
76-
fold_stmt(self, stmt)
142+
if (self.in_cfg)(stmt.node.attrs()) {
143+
fold::noop_fold_stmt(stmt, self)
144+
} else {
145+
SmallVector::zero()
146+
}
77147
}
148+
78149
fn fold_mac(&mut self, mac: ast::Mac) -> ast::Mac {
79150
fold::noop_fold_mac(mac, self)
80151
}
152+
81153
fn fold_item(&mut self, item: P<ast::Item>) -> SmallVector<P<ast::Item>> {
82-
fold_item(self, item)
154+
if (self.in_cfg)(&item.attrs) {
155+
SmallVector::one(item.map(|i| self.fold_item_simple(i)))
156+
} else {
157+
SmallVector::zero()
158+
}
83159
}
84160
}
85161

@@ -94,114 +170,6 @@ pub fn strip_items<'a, F>(diagnostic: &'a Handler,
94170
ctxt.fold_crate(krate)
95171
}
96172

97-
fn filter_foreign_item<F>(cx: &mut Context<F>,
98-
item: ast::ForeignItem)
99-
-> Option<ast::ForeignItem> where
100-
F: FnMut(&[ast::Attribute]) -> bool
101-
{
102-
if foreign_item_in_cfg(cx, &item) {
103-
Some(item)
104-
} else {
105-
None
106-
}
107-
}
108-
109-
fn fold_foreign_mod<F>(cx: &mut Context<F>,
110-
ast::ForeignMod {abi, items}: ast::ForeignMod)
111-
-> ast::ForeignMod where
112-
F: FnMut(&[ast::Attribute]) -> bool
113-
{
114-
ast::ForeignMod {
115-
abi: abi,
116-
items: items.into_iter()
117-
.filter_map(|a| filter_foreign_item(cx, a))
118-
.collect()
119-
}
120-
}
121-
122-
fn fold_item<F>(cx: &mut Context<F>, item: P<ast::Item>) -> SmallVector<P<ast::Item>> where
123-
F: FnMut(&[ast::Attribute]) -> bool
124-
{
125-
if item_in_cfg(cx, &item) {
126-
SmallVector::one(item.map(|i| cx.fold_item_simple(i)))
127-
} else {
128-
SmallVector::zero()
129-
}
130-
}
131-
132-
fn fold_item_kind<F>(cx: &mut Context<F>, item: ast::ItemKind) -> ast::ItemKind where
133-
F: FnMut(&[ast::Attribute]) -> bool
134-
{
135-
let item = match item {
136-
ast::ItemKind::Impl(u, o, a, b, c, impl_items) => {
137-
let impl_items = impl_items.into_iter()
138-
.filter(|ii| (cx.in_cfg)(&ii.attrs))
139-
.collect();
140-
ast::ItemKind::Impl(u, o, a, b, c, impl_items)
141-
}
142-
ast::ItemKind::Trait(u, a, b, methods) => {
143-
let methods = methods.into_iter()
144-
.filter(|ti| (cx.in_cfg)(&ti.attrs))
145-
.collect();
146-
ast::ItemKind::Trait(u, a, b, methods)
147-
}
148-
ast::ItemKind::Struct(def, generics) => {
149-
ast::ItemKind::Struct(fold_struct(cx, def), generics)
150-
}
151-
ast::ItemKind::Enum(def, generics) => {
152-
let variants = def.variants.into_iter().filter_map(|v| {
153-
if !(cx.in_cfg)(&v.node.attrs) {
154-
None
155-
} else {
156-
Some(Spanned {
157-
node: ast::Variant_ {
158-
name: v.node.name,
159-
attrs: v.node.attrs,
160-
data: fold_struct(cx, v.node.data),
161-
disr_expr: v.node.disr_expr,
162-
},
163-
span: v.span
164-
})
165-
}
166-
});
167-
ast::ItemKind::Enum(ast::EnumDef {
168-
variants: variants.collect(),
169-
}, generics)
170-
}
171-
item => item,
172-
};
173-
174-
fold::noop_fold_item_kind(item, cx)
175-
}
176-
177-
fn fold_struct<F>(cx: &mut Context<F>, vdata: ast::VariantData) -> ast::VariantData where
178-
F: FnMut(&[ast::Attribute]) -> bool
179-
{
180-
match vdata {
181-
ast::VariantData::Struct(fields, id) => {
182-
ast::VariantData::Struct(fields.into_iter().filter(|m| {
183-
(cx.in_cfg)(&m.attrs)
184-
}).collect(), id)
185-
}
186-
ast::VariantData::Tuple(fields, id) => {
187-
ast::VariantData::Tuple(fields.into_iter().filter(|m| {
188-
(cx.in_cfg)(&m.attrs)
189-
}).collect(), id)
190-
}
191-
ast::VariantData::Unit(id) => ast::VariantData::Unit(id)
192-
}
193-
}
194-
195-
fn fold_opt_expr<F>(cx: &mut Context<F>, expr: P<ast::Expr>) -> Option<P<ast::Expr>>
196-
where F: FnMut(&[ast::Attribute]) -> bool
197-
{
198-
if expr_in_cfg(cx, &expr) {
199-
Some(fold_expr(cx, expr))
200-
} else {
201-
None
202-
}
203-
}
204-
205173
fn fold_expr<F>(cx: &mut Context<F>, expr: P<ast::Expr>) -> P<ast::Expr> where
206174
F: FnMut(&[ast::Attribute]) -> bool
207175
{
@@ -222,40 +190,6 @@ fn fold_expr<F>(cx: &mut Context<F>, expr: P<ast::Expr>) -> P<ast::Expr> where
222190
})
223191
}
224192

225-
fn fold_stmt<F>(cx: &mut Context<F>, stmt: ast::Stmt) -> SmallVector<ast::Stmt>
226-
where F: FnMut(&[ast::Attribute]) -> bool
227-
{
228-
if stmt_in_cfg(cx, &stmt) {
229-
fold::noop_fold_stmt(stmt, cx)
230-
} else {
231-
SmallVector::zero()
232-
}
233-
}
234-
235-
fn stmt_in_cfg<F>(cx: &mut Context<F>, stmt: &ast::Stmt) -> bool where
236-
F: FnMut(&[ast::Attribute]) -> bool
237-
{
238-
(cx.in_cfg)(stmt.node.attrs())
239-
}
240-
241-
fn expr_in_cfg<F>(cx: &mut Context<F>, expr: &ast::Expr) -> bool where
242-
F: FnMut(&[ast::Attribute]) -> bool
243-
{
244-
(cx.in_cfg)(expr.attrs())
245-
}
246-
247-
fn item_in_cfg<F>(cx: &mut Context<F>, item: &ast::Item) -> bool where
248-
F: FnMut(&[ast::Attribute]) -> bool
249-
{
250-
return (cx.in_cfg)(&item.attrs);
251-
}
252-
253-
fn foreign_item_in_cfg<F>(cx: &mut Context<F>, item: &ast::ForeignItem) -> bool where
254-
F: FnMut(&[ast::Attribute]) -> bool
255-
{
256-
return (cx.in_cfg)(&item.attrs);
257-
}
258-
259193
fn is_cfg(attr: &ast::Attribute) -> bool {
260194
attr.check_name("cfg")
261195
}

0 commit comments

Comments
 (0)