Skip to content

Commit a9cf198

Browse files
committed
auto merge of #17162 : sfackler/rust/decorator-traits, r=huonw
The other extension types already worked this way and it can be useful to track some state along with the extension. I also removed the `BasicMacroExpander` and `BasicIdentMacroExpander` since the span inside of them was never used. The expander function types now directly implement the relevant trait.
2 parents ccae356 + 200a08f commit a9cf198

File tree

4 files changed

+57
-48
lines changed

4 files changed

+57
-48
lines changed

src/librustc/plugin/registry.rs

+3-8
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use lint::{LintPassObject, LintId, Lint};
1414

1515
use syntax::ext::base::{SyntaxExtension, NamedSyntaxExtension, NormalTT};
16-
use syntax::ext::base::{IdentTT, LetSyntaxTT, ItemDecorator, ItemModifier, BasicMacroExpander};
16+
use syntax::ext::base::{IdentTT, LetSyntaxTT, ItemDecorator, ItemModifier};
1717
use syntax::ext::base::{MacroExpanderFn};
1818
use syntax::codemap::Span;
1919
use syntax::parse::token;
@@ -71,15 +71,10 @@ impl Registry {
7171
/// Register a macro of the usual kind.
7272
///
7373
/// This is a convenience wrapper for `register_syntax_extension`.
74-
/// It builds for you a `NormalTT` with a `BasicMacroExpander`,
74+
/// It builds for you a `NormalTT` that calls `expander`,
7575
/// and also takes care of interning the macro's name.
7676
pub fn register_macro(&mut self, name: &str, expander: MacroExpanderFn) {
77-
self.register_syntax_extension(
78-
token::intern(name),
79-
NormalTT(box BasicMacroExpander {
80-
expander: expander,
81-
span: None,
82-
}, None));
77+
self.register_syntax_extension(token::intern(name), NormalTT(box expander, None));
8378
}
8479

8580
/// Register a compiler lint pass.

src/libsyntax/ext/base.rs

+49-34
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,44 @@ pub struct MacroDef {
3939
pub ext: SyntaxExtension
4040
}
4141

42-
pub type ItemDecorator =
43-
fn(&mut ExtCtxt, Span, Gc<ast::MetaItem>, Gc<ast::Item>, |Gc<ast::Item>|);
42+
pub trait ItemDecorator {
43+
fn expand(&self,
44+
ecx: &mut ExtCtxt,
45+
sp: Span,
46+
meta_item: Gc<ast::MetaItem>,
47+
item: Gc<ast::Item>,
48+
push: |Gc<ast::Item>|);
49+
}
4450

45-
pub type ItemModifier =
46-
fn(&mut ExtCtxt, Span, Gc<ast::MetaItem>, Gc<ast::Item>) -> Gc<ast::Item>;
51+
impl ItemDecorator for fn(&mut ExtCtxt, Span, Gc<ast::MetaItem>, Gc<ast::Item>, |Gc<ast::Item>|) {
52+
fn expand(&self,
53+
ecx: &mut ExtCtxt,
54+
sp: Span,
55+
meta_item: Gc<ast::MetaItem>,
56+
item: Gc<ast::Item>,
57+
push: |Gc<ast::Item>|) {
58+
(*self)(ecx, sp, meta_item, item, push)
59+
}
60+
}
4761

48-
pub struct BasicMacroExpander {
49-
pub expander: MacroExpanderFn,
50-
pub span: Option<Span>
62+
pub trait ItemModifier {
63+
fn expand(&self,
64+
ecx: &mut ExtCtxt,
65+
span: Span,
66+
meta_item: Gc<ast::MetaItem>,
67+
item: Gc<ast::Item>)
68+
-> Gc<ast::Item>;
69+
}
70+
71+
impl ItemModifier for fn(&mut ExtCtxt, Span, Gc<ast::MetaItem>, Gc<ast::Item>) -> Gc<ast::Item> {
72+
fn expand(&self,
73+
ecx: &mut ExtCtxt,
74+
span: Span,
75+
meta_item: Gc<ast::MetaItem>,
76+
item: Gc<ast::Item>)
77+
-> Gc<ast::Item> {
78+
(*self)(ecx, span, meta_item, item)
79+
}
5180
}
5281

5382
/// Represents a thing that maps token trees to Macro Results
@@ -60,24 +89,18 @@ pub trait TTMacroExpander {
6089
}
6190

6291
pub type MacroExpanderFn =
63-
fn<'cx>(ecx: &'cx mut ExtCtxt, span: codemap::Span, token_tree: &[ast::TokenTree])
64-
-> Box<MacResult+'cx>;
92+
fn<'cx>(&'cx mut ExtCtxt, Span, &[ast::TokenTree]) -> Box<MacResult+'cx>;
6593

66-
impl TTMacroExpander for BasicMacroExpander {
94+
impl TTMacroExpander for MacroExpanderFn {
6795
fn expand<'cx>(&self,
6896
ecx: &'cx mut ExtCtxt,
6997
span: Span,
7098
token_tree: &[ast::TokenTree])
7199
-> Box<MacResult+'cx> {
72-
(self.expander)(ecx, span, token_tree)
100+
(*self)(ecx, span, token_tree)
73101
}
74102
}
75103

76-
pub struct BasicIdentMacroExpander {
77-
pub expander: IdentMacroExpanderFn,
78-
pub span: Option<Span>
79-
}
80-
81104
pub trait IdentMacroExpander {
82105
fn expand<'cx>(&self,
83106
cx: &'cx mut ExtCtxt,
@@ -87,20 +110,20 @@ pub trait IdentMacroExpander {
87110
-> Box<MacResult+'cx>;
88111
}
89112

90-
impl IdentMacroExpander for BasicIdentMacroExpander {
113+
pub type IdentMacroExpanderFn =
114+
fn<'cx>(&'cx mut ExtCtxt, Span, ast::Ident, Vec<ast::TokenTree>) -> Box<MacResult+'cx>;
115+
116+
impl IdentMacroExpander for IdentMacroExpanderFn {
91117
fn expand<'cx>(&self,
92118
cx: &'cx mut ExtCtxt,
93119
sp: Span,
94120
ident: ast::Ident,
95121
token_tree: Vec<ast::TokenTree> )
96122
-> Box<MacResult+'cx> {
97-
(self.expander)(cx, sp, ident, token_tree)
123+
(*self)(cx, sp, ident, token_tree)
98124
}
99125
}
100126

101-
pub type IdentMacroExpanderFn =
102-
fn<'cx>(&'cx mut ExtCtxt, Span, ast::Ident, Vec<ast::TokenTree>) -> Box<MacResult+'cx>;
103-
104127
/// The result of a macro expansion. The return values of the various
105128
/// methods are spliced into the AST at the callsite of the macro (or
106129
/// just into the compiler's internal macro table, for `make_def`).
@@ -281,11 +304,11 @@ pub enum SyntaxExtension {
281304
/// based upon it.
282305
///
283306
/// `#[deriving(...)]` is an `ItemDecorator`.
284-
ItemDecorator(ItemDecorator),
307+
ItemDecorator(Box<ItemDecorator + 'static>),
285308

286309
/// A syntax extension that is attached to an item and modifies it
287310
/// in-place.
288-
ItemModifier(ItemModifier),
311+
ItemModifier(Box<ItemModifier + 'static>),
289312

290313
/// A normal, function-like syntax extension.
291314
///
@@ -329,20 +352,12 @@ impl BlockInfo {
329352
fn initial_syntax_expander_table() -> SyntaxEnv {
330353
// utility function to simplify creating NormalTT syntax extensions
331354
fn builtin_normal_expander(f: MacroExpanderFn) -> SyntaxExtension {
332-
NormalTT(box BasicMacroExpander {
333-
expander: f,
334-
span: None,
335-
},
336-
None)
355+
NormalTT(box f, None)
337356
}
338357

339358
let mut syntax_expanders = SyntaxEnv::new();
340359
syntax_expanders.insert(intern("macro_rules"),
341-
LetSyntaxTT(box BasicIdentMacroExpander {
342-
expander: ext::tt::macro_rules::add_new_extension,
343-
span: None,
344-
},
345-
None));
360+
LetSyntaxTT(box ext::tt::macro_rules::add_new_extension, None));
346361
syntax_expanders.insert(intern("fmt"),
347362
builtin_normal_expander(
348363
ext::fmt::expand_syntax_ext));
@@ -371,7 +386,7 @@ fn initial_syntax_expander_table() -> SyntaxEnv {
371386
builtin_normal_expander(
372387
ext::log_syntax::expand_syntax_ext));
373388
syntax_expanders.insert(intern("deriving"),
374-
ItemDecorator(ext::deriving::expand_meta_deriving));
389+
ItemDecorator(box ext::deriving::expand_meta_deriving));
375390

376391
// Quasi-quoting expanders
377392
syntax_expanders.insert(intern("quote_tokens"),

src/libsyntax/ext/expand.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ fn expand_item(it: Gc<ast::Item>, fld: &mut MacroExpander)
251251

252252
match fld.cx.syntax_env.find(&intern(mname.get())) {
253253
Some(rc) => match *rc {
254-
ItemDecorator(dec_fn) => {
254+
ItemDecorator(ref dec) => {
255255
attr::mark_used(attr);
256256

257257
fld.cx.bt_push(ExpnInfo {
@@ -266,8 +266,7 @@ fn expand_item(it: Gc<ast::Item>, fld: &mut MacroExpander)
266266
// we'd ideally decorator_items.push_all(expand_item(item, fld)),
267267
// but that double-mut-borrows fld
268268
let mut items: SmallVector<Gc<ast::Item>> = SmallVector::zero();
269-
dec_fn(fld.cx, attr.span, attr.node.value, it,
270-
|item| items.push(item));
269+
dec.expand(fld.cx, attr.span, attr.node.value, it, |item| items.push(item));
271270
decorator_items.extend(items.move_iter()
272271
.flat_map(|item| expand_item(item, fld).move_iter()));
273272

@@ -328,7 +327,7 @@ fn expand_item_modifiers(mut it: Gc<ast::Item>, fld: &mut MacroExpander)
328327

329328
match fld.cx.syntax_env.find(&intern(mname.get())) {
330329
Some(rc) => match *rc {
331-
ItemModifier(dec_fn) => {
330+
ItemModifier(ref mac) => {
332331
attr::mark_used(attr);
333332
fld.cx.bt_push(ExpnInfo {
334333
call_site: attr.span,
@@ -338,7 +337,7 @@ fn expand_item_modifiers(mut it: Gc<ast::Item>, fld: &mut MacroExpander)
338337
span: None,
339338
}
340339
});
341-
it = dec_fn(fld.cx, attr.span, attr.node.value, it);
340+
it = mac.expand(fld.cx, attr.span, attr.node.value, it);
342341
fld.cx.bt_pop();
343342
}
344343
_ => unreachable!()

src/test/auxiliary/macro_crate_test.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ pub fn plugin_registrar(reg: &mut Registry) {
3636
reg.register_macro("identity", expand_identity);
3737
reg.register_syntax_extension(
3838
token::intern("into_foo"),
39-
ItemModifier(expand_into_foo));
39+
ItemModifier(box expand_into_foo));
4040
}
4141

4242
fn expand_make_a_1(cx: &mut ExtCtxt, sp: Span, tts: &[TokenTree])

0 commit comments

Comments
 (0)