Skip to content

Commit 3c02749

Browse files
committed
Tweak ItemDecorator API
The old method of building up a list of items and threading it through all of the decorators was unwieldy and not really scalable as non-deriving ItemDecorators become possible. The API is now that the decorator gets an immutable reference to the item it's attached to, and a callback that it can pass new items to. If we want to add syntax extensions that can modify the item they're attached to, we can add that later, but I think it'll have to be separate from ItemDecorator to avoid strange ordering issues.
1 parent 68129d2 commit 3c02749

18 files changed

+76
-74
lines changed

src/libsyntax/ext/base.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ pub struct MacroDef {
3535
}
3636

3737
pub type ItemDecorator =
38-
fn(&mut ExtCtxt, Span, @ast::MetaItem, ~[@ast::Item]) -> ~[@ast::Item];
38+
fn(&mut ExtCtxt, Span, @ast::MetaItem, @ast::Item, |@ast::Item|);
3939

4040
pub struct BasicMacroExpander {
4141
expander: MacroExpanderFn,

src/libsyntax/ext/deriving/clone.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ use ext::deriving::generic::*;
1717
pub fn expand_deriving_clone(cx: &mut ExtCtxt,
1818
span: Span,
1919
mitem: @MetaItem,
20-
in_items: ~[@Item])
21-
-> ~[@Item] {
20+
item: @Item,
21+
push: |@Item|) {
2222
let trait_def = TraitDef {
2323
span: span,
2424
path: Path::new(~["std", "clone", "Clone"]),
@@ -38,14 +38,14 @@ pub fn expand_deriving_clone(cx: &mut ExtCtxt,
3838
]
3939
};
4040

41-
trait_def.expand(cx, mitem, in_items)
41+
trait_def.expand(cx, mitem, item, push)
4242
}
4343

4444
pub fn expand_deriving_deep_clone(cx: &mut ExtCtxt,
4545
span: Span,
4646
mitem: @MetaItem,
47-
in_items: ~[@Item])
48-
-> ~[@Item] {
47+
item: @Item,
48+
push: |@Item|) {
4949
let trait_def = TraitDef {
5050
span: span,
5151
path: Path::new(~["std", "clone", "DeepClone"]),
@@ -67,7 +67,7 @@ pub fn expand_deriving_deep_clone(cx: &mut ExtCtxt,
6767
]
6868
};
6969

70-
trait_def.expand(cx, mitem, in_items)
70+
trait_def.expand(cx, mitem, item, push)
7171
}
7272

7373
fn cs_clone(

src/libsyntax/ext/deriving/cmp/eq.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ use ext::deriving::generic::*;
1717
pub fn expand_deriving_eq(cx: &mut ExtCtxt,
1818
span: Span,
1919
mitem: @MetaItem,
20-
in_items: ~[@Item]) -> ~[@Item] {
20+
item: @Item,
21+
push: |@Item|) {
2122
// structures are equal if all fields are equal, and non equal, if
2223
// any fields are not equal or if the enum variants are different
2324
fn cs_eq(cx: &mut ExtCtxt, span: Span, substr: &Substructure) -> @Expr {
@@ -54,5 +55,5 @@ pub fn expand_deriving_eq(cx: &mut ExtCtxt,
5455
md!("ne", cs_ne)
5556
]
5657
};
57-
trait_def.expand(cx, mitem, in_items)
58+
trait_def.expand(cx, mitem, item, push)
5859
}

src/libsyntax/ext/deriving/cmp/ord.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ use ext::deriving::generic::*;
1818
pub fn expand_deriving_ord(cx: &mut ExtCtxt,
1919
span: Span,
2020
mitem: @MetaItem,
21-
in_items: ~[@Item]) -> ~[@Item] {
21+
item: @Item,
22+
push: |@Item|) {
2223
macro_rules! md (
2324
($name:expr, $op:expr, $equal:expr) => {
2425
MethodDef {
@@ -46,7 +47,7 @@ pub fn expand_deriving_ord(cx: &mut ExtCtxt,
4647
md!("ge", false, true)
4748
]
4849
};
49-
trait_def.expand(cx, mitem, in_items)
50+
trait_def.expand(cx, mitem, item, push)
5051
}
5152

5253
/// Strict inequality.

src/libsyntax/ext/deriving/cmp/totaleq.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ use ext::deriving::generic::*;
1717
pub fn expand_deriving_totaleq(cx: &mut ExtCtxt,
1818
span: Span,
1919
mitem: @MetaItem,
20-
in_items: ~[@Item]) -> ~[@Item] {
20+
item: @Item,
21+
push: |@Item|) {
2122
fn cs_equals(cx: &mut ExtCtxt, span: Span, substr: &Substructure) -> @Expr {
2223
cs_and(|cx, span, _, _| cx.expr_bool(span, false),
2324
cx, span, substr)
@@ -41,5 +42,5 @@ pub fn expand_deriving_totaleq(cx: &mut ExtCtxt,
4142
}
4243
]
4344
};
44-
trait_def.expand(cx, mitem, in_items)
45+
trait_def.expand(cx, mitem, item, push)
4546
}

src/libsyntax/ext/deriving/cmp/totalord.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ use std::cmp::{Ordering, Equal, Less, Greater};
1919
pub fn expand_deriving_totalord(cx: &mut ExtCtxt,
2020
span: Span,
2121
mitem: @MetaItem,
22-
in_items: ~[@Item]) -> ~[@Item] {
22+
item: @Item,
23+
push: |@Item|) {
2324
let trait_def = TraitDef {
2425
span: span,
2526
path: Path::new(~["std", "cmp", "TotalOrd"]),
@@ -39,7 +40,7 @@ pub fn expand_deriving_totalord(cx: &mut ExtCtxt,
3940
]
4041
};
4142

42-
trait_def.expand(cx, mitem, in_items)
43+
trait_def.expand(cx, mitem, item, push)
4344
}
4445

4546

src/libsyntax/ext/deriving/decodable.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ use parse::token;
2424
pub fn expand_deriving_decodable(cx: &mut ExtCtxt,
2525
span: Span,
2626
mitem: @MetaItem,
27-
in_items: ~[@Item]) -> ~[@Item] {
27+
item: @Item,
28+
push: |@Item|) {
2829
let trait_def = TraitDef {
2930
span: span,
3031
path: Path::new_(~["serialize", "Decodable"], None,
@@ -49,7 +50,7 @@ pub fn expand_deriving_decodable(cx: &mut ExtCtxt,
4950
]
5051
};
5152

52-
trait_def.expand(cx, mitem, in_items)
53+
trait_def.expand(cx, mitem, item, push)
5354
}
5455

5556
fn decodable_substructure(cx: &mut ExtCtxt, trait_span: Span,

src/libsyntax/ext/deriving/default.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ use ext::deriving::generic::*;
1717
pub fn expand_deriving_default(cx: &mut ExtCtxt,
1818
span: Span,
1919
mitem: @MetaItem,
20-
in_items: ~[@Item])
21-
-> ~[@Item] {
20+
item: @Item,
21+
push: |@Item|) {
2222
let trait_def = TraitDef {
2323
span: span,
2424
path: Path::new(~["std", "default", "Default"]),
@@ -37,7 +37,7 @@ pub fn expand_deriving_default(cx: &mut ExtCtxt,
3737
},
3838
]
3939
};
40-
trait_def.expand(cx, mitem, in_items)
40+
trait_def.expand(cx, mitem, item, push)
4141
}
4242

4343
fn default_substructure(cx: &mut ExtCtxt, trait_span: Span, substr: &Substructure) -> @Expr {

src/libsyntax/ext/deriving/encodable.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ use parse::token;
8585
pub fn expand_deriving_encodable(cx: &mut ExtCtxt,
8686
span: Span,
8787
mitem: @MetaItem,
88-
in_items: ~[@Item]) -> ~[@Item] {
88+
item: @Item,
89+
push: |@Item|) {
8990
let trait_def = TraitDef {
9091
span: span,
9192
path: Path::new_(~["serialize", "Encodable"], None,
@@ -110,7 +111,7 @@ pub fn expand_deriving_encodable(cx: &mut ExtCtxt,
110111
]
111112
};
112113

113-
trait_def.expand(cx, mitem, in_items)
114+
trait_def.expand(cx, mitem, item, push)
114115
}
115116

116117
fn encodable_substructure(cx: &mut ExtCtxt, trait_span: Span,

src/libsyntax/ext/deriving/generic.rs

+15-19
Original file line numberDiff line numberDiff line change
@@ -322,27 +322,23 @@ impl<'a> TraitDef<'a> {
322322
pub fn expand(&self,
323323
cx: &mut ExtCtxt,
324324
_mitem: @ast::MetaItem,
325-
in_items: ~[@ast::Item]) -> ~[@ast::Item] {
326-
let mut result = ~[];
327-
for item in in_items.iter() {
328-
result.push(*item);
329-
match item.node {
330-
ast::ItemStruct(struct_def, ref generics) => {
331-
result.push(self.expand_struct_def(cx,
332-
struct_def,
333-
item.ident,
334-
generics));
335-
}
336-
ast::ItemEnum(ref enum_def, ref generics) => {
337-
result.push(self.expand_enum_def(cx,
338-
enum_def,
339-
item.ident,
340-
generics));
341-
}
342-
_ => ()
325+
item: @ast::Item,
326+
push: |@ast::Item|) {
327+
match item.node {
328+
ast::ItemStruct(struct_def, ref generics) => {
329+
push(self.expand_struct_def(cx,
330+
struct_def,
331+
item.ident,
332+
generics));
333+
}
334+
ast::ItemEnum(ref enum_def, ref generics) => {
335+
push(self.expand_enum_def(cx,
336+
enum_def,
337+
item.ident,
338+
generics));
343339
}
340+
_ => ()
344341
}
345-
result
346342
}
347343

348344
/**

src/libsyntax/ext/deriving/iter_bytes.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ use ext::deriving::generic::*;
1818
pub fn expand_deriving_iter_bytes(cx: &mut ExtCtxt,
1919
span: Span,
2020
mitem: @MetaItem,
21-
in_items: ~[@Item]) -> ~[@Item] {
21+
item: @Item,
22+
push: |@Item|) {
2223
let trait_def = TraitDef {
2324
span: span,
2425
path: Path::new(~["std", "to_bytes", "IterBytes"]),
@@ -41,7 +42,7 @@ pub fn expand_deriving_iter_bytes(cx: &mut ExtCtxt,
4142
]
4243
};
4344

44-
trait_def.expand(cx, mitem, in_items)
45+
trait_def.expand(cx, mitem, item, push)
4546
}
4647

4748
fn iter_bytes_substructure(cx: &mut ExtCtxt, trait_span: Span, substr: &Substructure) -> @Expr {

src/libsyntax/ext/deriving/mod.rs

+7-9
Original file line numberDiff line numberDiff line change
@@ -48,25 +48,24 @@ pub mod generic;
4848
pub fn expand_meta_deriving(cx: &mut ExtCtxt,
4949
_span: Span,
5050
mitem: @MetaItem,
51-
in_items: ~[@Item])
52-
-> ~[@Item] {
51+
item: @Item,
52+
push: |@Item|) {
5353
match mitem.node {
5454
MetaNameValue(_, ref l) => {
5555
cx.span_err(l.span, "unexpected value in `deriving`");
56-
in_items
5756
}
5857
MetaWord(_) | MetaList(_, []) => {
5958
cx.span_warn(mitem.span, "empty trait list in `deriving`");
60-
in_items
6159
}
6260
MetaList(_, ref titems) => {
63-
titems.rev_iter().fold(in_items, |in_items, &titem| {
61+
for &titem in titems.rev_iter() {
6462
match titem.node {
6563
MetaNameValue(ref tname, _) |
6664
MetaList(ref tname, _) |
6765
MetaWord(ref tname) => {
6866
macro_rules! expand(($func:path) => ($func(cx, titem.span,
69-
titem, in_items)));
67+
titem, item,
68+
|i| push(i))));
7069
match tname.get() {
7170
"Clone" => expand!(clone::expand_deriving_clone),
7271
"DeepClone" => expand!(clone::expand_deriving_deep_clone),
@@ -94,12 +93,11 @@ pub fn expand_meta_deriving(cx: &mut ExtCtxt,
9493
ref tname => {
9594
cx.span_err(titem.span, format!("unknown \
9695
`deriving` trait: `{}`", *tname));
97-
in_items
9896
}
99-
}
97+
};
10098
}
10199
}
102-
})
100+
}
103101
}
104102
}
105103
}

src/libsyntax/ext/deriving/primitive.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ use parse::token::InternedString;
1919
pub fn expand_deriving_from_primitive(cx: &mut ExtCtxt,
2020
span: Span,
2121
mitem: @MetaItem,
22-
in_items: ~[@Item]) -> ~[@Item] {
22+
item: @Item,
23+
push: |@Item|) {
2324
let trait_def = TraitDef {
2425
span: span,
2526
path: Path::new(~["std", "num", "FromPrimitive"]),
@@ -61,7 +62,7 @@ pub fn expand_deriving_from_primitive(cx: &mut ExtCtxt,
6162
]
6263
};
6364

64-
trait_def.expand(cx, mitem, in_items)
65+
trait_def.expand(cx, mitem, item, push)
6566
}
6667

6768
fn cs_from(name: &str, cx: &mut ExtCtxt, trait_span: Span, substr: &Substructure) -> @Expr {

src/libsyntax/ext/deriving/rand.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ use opt_vec;
1919
pub fn expand_deriving_rand(cx: &mut ExtCtxt,
2020
span: Span,
2121
mitem: @MetaItem,
22-
in_items: ~[@Item])
23-
-> ~[@Item] {
22+
item: @Item,
23+
push: |@Item|) {
2424
let trait_def = TraitDef {
2525
span: span,
2626
path: Path::new(~["std", "rand", "Rand"]),
@@ -46,7 +46,7 @@ pub fn expand_deriving_rand(cx: &mut ExtCtxt,
4646
}
4747
]
4848
};
49-
trait_def.expand(cx, mitem, in_items)
49+
trait_def.expand(cx, mitem, item, push)
5050
}
5151

5252
fn rand_substructure(cx: &mut ExtCtxt, trait_span: Span, substr: &Substructure) -> @Expr {

src/libsyntax/ext/deriving/show.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ use std::hashmap::HashMap;
2323
pub fn expand_deriving_show(cx: &mut ExtCtxt,
2424
span: Span,
2525
mitem: @MetaItem,
26-
in_items: ~[@Item])
27-
-> ~[@Item] {
26+
item: @Item,
27+
push: |@Item|) {
2828
// &mut ::std::fmt::Formatter
2929
let fmtr = Ptr(~Literal(Path::new(~["std", "fmt", "Formatter"])),
3030
Borrowed(None, ast::MutMutable));
@@ -47,7 +47,7 @@ pub fn expand_deriving_show(cx: &mut ExtCtxt,
4747
}
4848
]
4949
};
50-
trait_def.expand(cx, mitem, in_items)
50+
trait_def.expand(cx, mitem, item, push)
5151
}
5252

5353
// we construct a format string and then defer to std::fmt, since that

src/libsyntax/ext/deriving/to_str.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ use parse::token;
2020
pub fn expand_deriving_to_str(cx: &mut ExtCtxt,
2121
span: Span,
2222
mitem: @MetaItem,
23-
in_items: ~[@Item])
24-
-> ~[@Item] {
23+
item: @Item,
24+
push: |@Item|) {
2525
let trait_def = TraitDef {
2626
span: span,
2727
path: Path::new(~["std", "to_str", "ToStr"]),
@@ -40,7 +40,7 @@ pub fn expand_deriving_to_str(cx: &mut ExtCtxt,
4040
}
4141
]
4242
};
43-
trait_def.expand(cx, mitem, in_items)
43+
trait_def.expand(cx, mitem, item, push)
4444
}
4545

4646
// It used to be the case that this deriving implementation invoked

src/libsyntax/ext/deriving/zero.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ use ext::deriving::generic::*;
1717
pub fn expand_deriving_zero(cx: &mut ExtCtxt,
1818
span: Span,
1919
mitem: @MetaItem,
20-
in_items: ~[@Item])
21-
-> ~[@Item] {
20+
item: @Item,
21+
push: |@Item|) {
2222
let trait_def = TraitDef {
2323
span: span,
2424
path: Path::new(~["std", "num", "Zero"]),
@@ -53,7 +53,7 @@ pub fn expand_deriving_zero(cx: &mut ExtCtxt,
5353
}
5454
]
5555
};
56-
trait_def.expand(cx, mitem, in_items)
56+
trait_def.expand(cx, mitem, item, push)
5757
}
5858

5959
fn zero_substructure(cx: &mut ExtCtxt, trait_span: Span, substr: &Substructure) -> @Expr {

0 commit comments

Comments
 (0)