Skip to content

Commit 18477ac

Browse files
committed
auto merge of #12234 : sfackler/rust/restructure-item-decorator, r=huonw
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. @huonw
2 parents d40b537 + 3c02749 commit 18477ac

File tree

18 files changed

+76
-74
lines changed

18 files changed

+76
-74
lines changed

src/libsyntax/ext/base.rs

Lines changed: 1 addition & 1 deletion
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

Lines changed: 6 additions & 6 deletions
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

Lines changed: 3 additions & 2 deletions
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

Lines changed: 3 additions & 2 deletions
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

Lines changed: 3 additions & 2 deletions
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

Lines changed: 3 additions & 2 deletions
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

Lines changed: 3 additions & 2 deletions
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 3 additions & 2 deletions
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

Lines changed: 15 additions & 19 deletions
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
/**

0 commit comments

Comments
 (0)