@@ -39,15 +39,44 @@ pub struct MacroDef {
39
39
pub ext : SyntaxExtension
40
40
}
41
41
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
+ }
44
50
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
+ }
47
61
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
+ }
51
80
}
52
81
53
82
/// Represents a thing that maps token trees to Macro Results
@@ -60,24 +89,18 @@ pub trait TTMacroExpander {
60
89
}
61
90
62
91
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 > ;
65
93
66
- impl TTMacroExpander for BasicMacroExpander {
94
+ impl TTMacroExpander for MacroExpanderFn {
67
95
fn expand < ' cx > ( & self ,
68
96
ecx : & ' cx mut ExtCtxt ,
69
97
span : Span ,
70
98
token_tree : & [ ast:: TokenTree ] )
71
99
-> Box < MacResult +' cx > {
72
- ( self . expander ) ( ecx , span , token_tree )
100
+ ( * self) ( ecx , span , token_tree )
73
101
}
74
102
}
75
103
76
- pub struct BasicIdentMacroExpander {
77
- pub expander : IdentMacroExpanderFn ,
78
- pub span : Option < Span >
79
- }
80
-
81
104
pub trait IdentMacroExpander {
82
105
fn expand < ' cx > ( & self ,
83
106
cx : & ' cx mut ExtCtxt ,
@@ -87,20 +110,20 @@ pub trait IdentMacroExpander {
87
110
-> Box < MacResult +' cx > ;
88
111
}
89
112
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 {
91
117
fn expand < ' cx > ( & self ,
92
118
cx : & ' cx mut ExtCtxt ,
93
119
sp : Span ,
94
120
ident : ast:: Ident ,
95
121
token_tree : Vec < ast:: TokenTree > )
96
122
-> Box < MacResult +' cx > {
97
- ( self . expander ) ( cx , sp , ident , token_tree )
123
+ ( * self) ( cx , sp , ident , token_tree )
98
124
}
99
125
}
100
126
101
- pub type IdentMacroExpanderFn =
102
- fn <' cx >( & ' cx mut ExtCtxt , Span , ast:: Ident , Vec < ast:: TokenTree > ) -> Box < MacResult +' cx > ;
103
-
104
127
/// The result of a macro expansion. The return values of the various
105
128
/// methods are spliced into the AST at the callsite of the macro ( or
106
129
/// just into the compiler's internal macro table, for `make_def`).
@@ -281,11 +304,11 @@ pub enum SyntaxExtension {
281
304
/// based upon it.
282
305
///
283
306
/// `#[deriving(...)]` is an `ItemDecorator`.
284
- ItemDecorator ( ItemDecorator ) ,
307
+ ItemDecorator ( Box < ItemDecorator + ' static > ) ,
285
308
286
309
/// A syntax extension that is attached to an item and modifies it
287
310
/// in-place.
288
- ItemModifier ( ItemModifier ) ,
311
+ ItemModifier ( Box < ItemModifier + ' static > ) ,
289
312
290
313
/// A normal, function-like syntax extension.
291
314
///
@@ -329,20 +352,12 @@ impl BlockInfo {
329
352
fn initial_syntax_expander_table ( ) -> SyntaxEnv {
330
353
// utility function to simplify creating NormalTT syntax extensions
331
354
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 )
337
356
}
338
357
339
358
let mut syntax_expanders = SyntaxEnv :: new ( ) ;
340
359
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 ) ) ;
346
361
syntax_expanders. insert ( intern( "fmt" ) ,
347
362
builtin_normal_expander (
348
363
ext:: fmt:: expand_syntax_ext ) ) ;
@@ -371,7 +386,7 @@ fn initial_syntax_expander_table() -> SyntaxEnv {
371
386
builtin_normal_expander (
372
387
ext:: log_syntax:: expand_syntax_ext ) ) ;
373
388
syntax_expanders. insert ( intern( "deriving" ) ,
374
- ItemDecorator ( ext:: deriving:: expand_meta_deriving) ) ;
389
+ ItemDecorator ( box ext:: deriving:: expand_meta_deriving ) ) ;
375
390
376
391
// Quasi-quoting expanders
377
392
syntax_expanders . insert( intern( "quote_tokens" ) ,
0 commit comments