@@ -20,15 +20,24 @@ struct ProcMacroDerive {
20
20
attrs : Vec < ast:: Name > ,
21
21
}
22
22
23
+ enum ProcMacroDefType {
24
+ Attr ,
25
+ Bang
26
+ }
27
+
23
28
struct ProcMacroDef {
24
29
function_name : Ident ,
25
30
span : Span ,
31
+ def_type : ProcMacroDefType
32
+ }
33
+
34
+ enum ProcMacro {
35
+ Derive ( ProcMacroDerive ) ,
36
+ Def ( ProcMacroDef )
26
37
}
27
38
28
39
struct CollectProcMacros < ' a > {
29
- derives : Vec < ProcMacroDerive > ,
30
- attr_macros : Vec < ProcMacroDef > ,
31
- bang_macros : Vec < ProcMacroDef > ,
40
+ macros : Vec < ProcMacro > ,
32
41
in_root : bool ,
33
42
handler : & ' a errors:: Handler ,
34
43
is_proc_macro_crate : bool ,
@@ -46,22 +55,22 @@ pub fn inject(sess: &ParseSess,
46
55
let ecfg = ExpansionConfig :: default ( "proc_macro" . to_string ( ) ) ;
47
56
let mut cx = ExtCtxt :: new ( sess, ecfg, resolver) ;
48
57
49
- let ( derives, attr_macros, bang_macros) = {
50
- let mut collect = CollectProcMacros {
51
- derives : Vec :: new ( ) ,
52
- attr_macros : Vec :: new ( ) ,
53
- bang_macros : Vec :: new ( ) ,
54
- in_root : true ,
55
- handler,
56
- is_proc_macro_crate,
57
- is_test_crate,
58
- } ;
59
- if has_proc_macro_decls || is_proc_macro_crate {
60
- visit:: walk_crate ( & mut collect, & krate) ;
61
- }
62
- ( collect. derives , collect. attr_macros , collect. bang_macros )
58
+ let mut collect = CollectProcMacros {
59
+ macros : Vec :: new ( ) ,
60
+ in_root : true ,
61
+ handler,
62
+ is_proc_macro_crate,
63
+ is_test_crate,
63
64
} ;
64
65
66
+ if has_proc_macro_decls || is_proc_macro_crate {
67
+ visit:: walk_crate ( & mut collect, & krate) ;
68
+ }
69
+ // NOTE: If you change the order of macros in this vec
70
+ // for any reason, you must also update 'raw_proc_macro'
71
+ // in src/librustc_metadata/decoder.rs
72
+ let macros = collect. macros ;
73
+
65
74
if !is_proc_macro_crate {
66
75
return krate
67
76
}
@@ -74,7 +83,7 @@ pub fn inject(sess: &ParseSess,
74
83
return krate;
75
84
}
76
85
77
- krate. module . items . push ( mk_decls ( & mut cx, & derives , & attr_macros , & bang_macros ) ) ;
86
+ krate. module . items . push ( mk_decls ( & mut cx, & macros ) ) ;
78
87
79
88
krate
80
89
}
@@ -161,12 +170,12 @@ impl<'a> CollectProcMacros<'a> {
161
170
} ;
162
171
163
172
if self . in_root && item. vis . node . is_pub ( ) {
164
- self . derives . push ( ProcMacroDerive {
173
+ self . macros . push ( ProcMacro :: Derive ( ProcMacroDerive {
165
174
span : item. span ,
166
175
trait_name : trait_ident. name ,
167
176
function_name : item. ident ,
168
177
attrs : proc_attrs,
169
- } ) ;
178
+ } ) ) ;
170
179
} else {
171
180
let msg = if !self . in_root {
172
181
"functions tagged with `#[proc_macro_derive]` must \
@@ -180,10 +189,11 @@ impl<'a> CollectProcMacros<'a> {
180
189
181
190
fn collect_attr_proc_macro ( & mut self , item : & ' a ast:: Item ) {
182
191
if self . in_root && item. vis . node . is_pub ( ) {
183
- self . attr_macros . push ( ProcMacroDef {
192
+ self . macros . push ( ProcMacro :: Def ( ProcMacroDef {
184
193
span : item. span ,
185
194
function_name : item. ident ,
186
- } ) ;
195
+ def_type : ProcMacroDefType :: Attr
196
+ } ) ) ;
187
197
} else {
188
198
let msg = if !self . in_root {
189
199
"functions tagged with `#[proc_macro_attribute]` must \
@@ -197,10 +207,11 @@ impl<'a> CollectProcMacros<'a> {
197
207
198
208
fn collect_bang_proc_macro ( & mut self , item : & ' a ast:: Item ) {
199
209
if self . in_root && item. vis . node . is_pub ( ) {
200
- self . bang_macros . push ( ProcMacroDef {
210
+ self . macros . push ( ProcMacro :: Def ( ProcMacroDef {
201
211
span : item. span ,
202
212
function_name : item. ident ,
203
- } ) ;
213
+ def_type : ProcMacroDefType :: Bang
214
+ } ) ) ;
204
215
} else {
205
216
let msg = if !self . in_root {
206
217
"functions tagged with `#[proc_macro]` must \
@@ -322,9 +333,7 @@ impl<'a> Visitor<'a> for CollectProcMacros<'a> {
322
333
// }
323
334
fn mk_decls (
324
335
cx : & mut ExtCtxt < ' _ > ,
325
- custom_derives : & [ ProcMacroDerive ] ,
326
- custom_attrs : & [ ProcMacroDef ] ,
327
- custom_macros : & [ ProcMacroDef ] ,
336
+ macros : & [ ProcMacro ] ,
328
337
) -> P < ast:: Item > {
329
338
let expn_id = cx. resolver . expansion_for_ast_pass (
330
339
DUMMY_SP ,
@@ -354,26 +363,32 @@ fn mk_decls(
354
363
let proc_macro_ty_method_path = |method| cx. expr_path ( cx. path ( span, vec ! [
355
364
proc_macro, bridge, client, proc_macro_ty, method,
356
365
] ) ) ;
357
- custom_derives. iter ( ) . map ( |cd| {
358
- cx. expr_call ( span, proc_macro_ty_method_path ( custom_derive) , vec ! [
359
- cx. expr_str( cd. span, cd. trait_name) ,
360
- cx. expr_vec_slice(
361
- span,
362
- cd. attrs. iter( ) . map( |& s| cx. expr_str( cd. span, s) ) . collect:: <Vec <_>>( )
363
- ) ,
364
- local_path( cd. span, cd. function_name) ,
365
- ] )
366
- } ) . chain ( custom_attrs. iter ( ) . map ( |ca| {
367
- cx. expr_call ( span, proc_macro_ty_method_path ( attr) , vec ! [
368
- cx. expr_str( ca. span, ca. function_name. name) ,
369
- local_path( ca. span, ca. function_name) ,
370
- ] )
371
- } ) ) . chain ( custom_macros. iter ( ) . map ( |cm| {
372
- cx. expr_call ( span, proc_macro_ty_method_path ( bang) , vec ! [
373
- cx. expr_str( cm. span, cm. function_name. name) ,
374
- local_path( cm. span, cm. function_name) ,
375
- ] )
376
- } ) ) . collect ( )
366
+ macros. iter ( ) . map ( |m| {
367
+ match m {
368
+ ProcMacro :: Derive ( cd) => {
369
+ cx. expr_call ( span, proc_macro_ty_method_path ( custom_derive) , vec ! [
370
+ cx. expr_str( cd. span, cd. trait_name) ,
371
+ cx. expr_vec_slice(
372
+ span,
373
+ cd. attrs. iter( ) . map( |& s| cx. expr_str( cd. span, s) ) . collect:: <Vec <_>>( )
374
+ ) ,
375
+ local_path( cd. span, cd. function_name) ,
376
+ ] )
377
+ } ,
378
+ ProcMacro :: Def ( ca) => {
379
+ let ident = match ca. def_type {
380
+ ProcMacroDefType :: Attr => attr,
381
+ ProcMacroDefType :: Bang => bang
382
+ } ;
383
+
384
+ cx. expr_call ( span, proc_macro_ty_method_path ( ident) , vec ! [
385
+ cx. expr_str( ca. span, ca. function_name. name) ,
386
+ local_path( ca. span, ca. function_name) ,
387
+ ] )
388
+
389
+ }
390
+ }
391
+ } ) . collect ( )
377
392
} ;
378
393
379
394
let decls_static = cx. item_static (
0 commit comments