@@ -74,7 +74,7 @@ pub enum LegacyScope<'a> {
74
74
pub struct LegacyBinding < ' a > {
75
75
pub parent : Cell < LegacyScope < ' a > > ,
76
76
pub name : ast:: Name ,
77
- ext : Rc < SyntaxExtension > ,
77
+ def_id : DefId ,
78
78
pub span : Span ,
79
79
}
80
80
@@ -239,15 +239,34 @@ impl<'a> base::Resolver for Resolver<'a> {
239
239
240
240
fn resolve_invoc ( & mut self , invoc : & mut Invocation , scope : Mark , force : bool )
241
241
-> Result < Option < Rc < SyntaxExtension > > , Determinacy > {
242
- let ( attr , traits , item ) = match invoc. kind {
242
+ let def = match invoc. kind {
243
243
InvocationKind :: Attr { attr : None , .. } => return Ok ( None ) ,
244
+ _ => match self . resolve_invoc_to_def ( invoc, scope, force) {
245
+ Ok ( def) => def,
246
+ Err ( determinacy) => return Err ( determinacy) ,
247
+ } ,
248
+ } ;
249
+ self . macro_defs . insert ( invoc. expansion_data . mark , def. def_id ( ) ) ;
250
+ Ok ( Some ( self . get_macro ( def) ) )
251
+ }
252
+
253
+ fn resolve_macro ( & mut self , scope : Mark , path : & ast:: Path , kind : MacroKind , force : bool )
254
+ -> Result < Rc < SyntaxExtension > , Determinacy > {
255
+ self . resolve_macro_to_def ( scope, path, kind, force) . map ( |def| self . get_macro ( def) )
256
+ }
257
+ }
258
+
259
+ impl < ' a > Resolver < ' a > {
260
+ fn resolve_invoc_to_def ( & mut self , invoc : & mut Invocation , scope : Mark , force : bool )
261
+ -> Result < Def , Determinacy > {
262
+ let ( attr, traits, item) = match invoc. kind {
244
263
InvocationKind :: Attr { ref mut attr, ref traits, ref mut item } => ( attr, traits, item) ,
245
264
InvocationKind :: Bang { ref mac, .. } => {
246
- return self . resolve_macro ( scope, & mac. node . path , MacroKind :: Bang , force) . map ( Some ) ;
265
+ return self . resolve_macro_to_def ( scope, & mac. node . path , MacroKind :: Bang , force) ;
247
266
}
248
267
InvocationKind :: Derive { name, span, .. } => {
249
268
let path = ast:: Path :: from_ident ( span, Ident :: with_empty_ctxt ( name) ) ;
250
- return self . resolve_macro ( scope, & path, MacroKind :: Derive , force) . map ( Some ) ;
269
+ return self . resolve_macro_to_def ( scope, & path, MacroKind :: Derive , force) ;
251
270
}
252
271
} ;
253
272
@@ -257,8 +276,8 @@ impl<'a> base::Resolver for Resolver<'a> {
257
276
} ;
258
277
259
278
let mut determined = true ;
260
- match self . resolve_macro ( scope, & path, MacroKind :: Attr , force) {
261
- Ok ( ext ) => return Ok ( Some ( ext ) ) ,
279
+ match self . resolve_macro_to_def ( scope, & path, MacroKind :: Attr , force) {
280
+ Ok ( def ) => return Ok ( def ) ,
262
281
Err ( Determinacy :: Undetermined ) => determined = false ,
263
282
Err ( Determinacy :: Determined ) if force => return Err ( Determinacy :: Determined ) ,
264
283
Err ( Determinacy :: Determined ) => { }
@@ -293,8 +312,8 @@ impl<'a> base::Resolver for Resolver<'a> {
293
312
Err ( if determined { Determinacy :: Determined } else { Determinacy :: Undetermined } )
294
313
}
295
314
296
- fn resolve_macro ( & mut self , scope : Mark , path : & ast:: Path , kind : MacroKind , force : bool )
297
- -> Result < Rc < SyntaxExtension > , Determinacy > {
315
+ fn resolve_macro_to_def ( & mut self , scope : Mark , path : & ast:: Path , kind : MacroKind , force : bool )
316
+ -> Result < Def , Determinacy > {
298
317
let ast:: Path { ref segments, span } = * path;
299
318
if segments. iter ( ) . any ( |segment| segment. parameters . is_some ( ) ) {
300
319
let kind =
@@ -317,10 +336,10 @@ impl<'a> base::Resolver for Resolver<'a> {
317
336
return Err ( Determinacy :: Determined ) ;
318
337
}
319
338
320
- let ext = match self . resolve_path ( & path, Some ( MacroNS ) , None ) {
339
+ let def = match self . resolve_path ( & path, Some ( MacroNS ) , None ) {
321
340
PathResult :: NonModule ( path_res) => match path_res. base_def ( ) {
322
341
Def :: Err => Err ( Determinacy :: Determined ) ,
323
- def @ _ => Ok ( self . get_macro ( def) ) ,
342
+ def @ _ => Ok ( def) ,
324
343
} ,
325
344
PathResult :: Module ( ..) => unreachable ! ( ) ,
326
345
PathResult :: Indeterminate if !force => return Err ( Determinacy :: Undetermined ) ,
@@ -331,15 +350,15 @@ impl<'a> base::Resolver for Resolver<'a> {
331
350
} ;
332
351
self . current_module . macro_resolutions . borrow_mut ( )
333
352
. push ( ( path. into_boxed_slice ( ) , span) ) ;
334
- return ext ;
353
+ return def ;
335
354
}
336
355
337
356
let name = path[ 0 ] . name ;
338
357
let result = match self . resolve_legacy_scope ( & invocation. legacy_scope , name, false ) {
339
- Some ( MacroBinding :: Legacy ( binding) ) => Ok ( binding. ext . clone ( ) ) ,
340
- Some ( MacroBinding :: Modern ( binding) ) => Ok ( binding. get_macro ( self ) ) ,
358
+ Some ( MacroBinding :: Legacy ( binding) ) => Ok ( Def :: Macro ( binding. def_id , MacroKind :: Bang ) ) ,
359
+ Some ( MacroBinding :: Modern ( binding) ) => Ok ( binding. def_ignoring_ambiguity ( ) ) ,
341
360
None => match self . resolve_lexical_macro_path_segment ( path[ 0 ] , MacroNS , None ) {
342
- Ok ( binding) => Ok ( binding. get_macro ( self ) ) ,
361
+ Ok ( binding) => Ok ( binding. def_ignoring_ambiguity ( ) ) ,
343
362
Err ( Determinacy :: Undetermined ) if !force =>
344
363
return Err ( Determinacy :: Undetermined ) ,
345
364
Err ( _) => {
@@ -354,9 +373,7 @@ impl<'a> base::Resolver for Resolver<'a> {
354
373
355
374
result
356
375
}
357
- }
358
376
359
- impl < ' a > Resolver < ' a > {
360
377
// Resolve the initial segment of a non-global macro path (e.g. `foo` in `foo::bar!();`)
361
378
pub fn resolve_lexical_macro_path_segment ( & mut self ,
362
379
ident : Ident ,
@@ -597,33 +614,23 @@ impl<'a> Resolver<'a> {
597
614
}
598
615
599
616
pub fn define_macro ( & mut self , item : & ast:: Item , legacy_scope : & mut LegacyScope < ' a > ) {
600
- if item. ident . name == "macro_rules" {
617
+ self . local_macro_def_scopes . insert ( item. id , self . current_module ) ;
618
+ let ident = item. ident ;
619
+ if ident. name == "macro_rules" {
601
620
self . session . span_err ( item. span , "user-defined macros may not be named `macro_rules`" ) ;
602
621
}
603
622
604
- let invocation = self . arenas . alloc_invocation_data ( InvocationData {
605
- module : Cell :: new ( self . current_module ) ,
606
- // FIXME(jseyfried) the following are irrelevant
607
- def_index : CRATE_DEF_INDEX , const_integer : false ,
608
- legacy_scope : Cell :: new ( LegacyScope :: Empty ) , expansion : Cell :: new ( LegacyScope :: Empty ) ,
609
- } ) ;
610
- if let ast:: ItemKind :: MacroDef ( _, mark) = item. node {
611
- self . invocations . insert ( mark, invocation) ;
612
- }
613
-
623
+ let def_id = self . definitions . local_def_id ( item. id ) ;
624
+ let ext = Rc :: new ( macro_rules:: compile ( & self . session . parse_sess , item) ) ;
625
+ self . macro_map . insert ( def_id, ext) ;
614
626
* legacy_scope = LegacyScope :: Binding ( self . arenas . alloc_legacy_binding ( LegacyBinding {
615
- parent : Cell :: new ( * legacy_scope) ,
616
- name : item. ident . name ,
617
- ext : Rc :: new ( macro_rules:: compile ( & self . session . parse_sess , item) ) ,
618
- span : item. span ,
627
+ parent : Cell :: new ( * legacy_scope) , name : ident. name , def_id : def_id, span : item. span ,
619
628
} ) ) ;
620
- self . macro_names . insert ( item . ident . name ) ;
629
+ self . macro_names . insert ( ident. name ) ;
621
630
622
631
if attr:: contains_name ( & item. attrs , "macro_export" ) {
623
- self . macro_exports . push ( Export {
624
- name : item. ident . name ,
625
- def : Def :: Macro ( self . definitions . local_def_id ( item. id ) , MacroKind :: Bang ) ,
626
- } ) ;
632
+ let def = Def :: Macro ( def_id, MacroKind :: Bang ) ;
633
+ self . macro_exports . push ( Export { name : ident. name , def : def } ) ;
627
634
}
628
635
}
629
636
0 commit comments