@@ -74,7 +74,7 @@ pub enum LegacyScope<'a> {
7474pub struct LegacyBinding < ' a > {
7575 pub parent : Cell < LegacyScope < ' a > > ,
7676 pub name : ast:: Name ,
77- ext : Rc < SyntaxExtension > ,
77+ def_id : DefId ,
7878 pub span : Span ,
7979}
8080
@@ -239,15 +239,34 @@ impl<'a> base::Resolver for Resolver<'a> {
239239
240240 fn resolve_invoc ( & mut self , invoc : & mut Invocation , scope : Mark , force : bool )
241241 -> Result < Option < Rc < SyntaxExtension > > , Determinacy > {
242- let ( attr , traits , item ) = match invoc. kind {
242+ let def = match invoc. kind {
243243 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 {
244263 InvocationKind :: Attr { ref mut attr, ref traits, ref mut item } => ( attr, traits, item) ,
245264 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) ;
247266 }
248267 InvocationKind :: Derive { name, span, .. } => {
249268 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) ;
251270 }
252271 } ;
253272
@@ -257,8 +276,8 @@ impl<'a> base::Resolver for Resolver<'a> {
257276 } ;
258277
259278 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 ) ,
262281 Err ( Determinacy :: Undetermined ) => determined = false ,
263282 Err ( Determinacy :: Determined ) if force => return Err ( Determinacy :: Determined ) ,
264283 Err ( Determinacy :: Determined ) => { }
@@ -293,8 +312,8 @@ impl<'a> base::Resolver for Resolver<'a> {
293312 Err ( if determined { Determinacy :: Determined } else { Determinacy :: Undetermined } )
294313 }
295314
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 > {
298317 let ast:: Path { ref segments, span } = * path;
299318 if segments. iter ( ) . any ( |segment| segment. parameters . is_some ( ) ) {
300319 let kind =
@@ -317,10 +336,10 @@ impl<'a> base::Resolver for Resolver<'a> {
317336 return Err ( Determinacy :: Determined ) ;
318337 }
319338
320- let ext = match self . resolve_path ( & path, Some ( MacroNS ) , None ) {
339+ let def = match self . resolve_path ( & path, Some ( MacroNS ) , None ) {
321340 PathResult :: NonModule ( path_res) => match path_res. base_def ( ) {
322341 Def :: Err => Err ( Determinacy :: Determined ) ,
323- def @ _ => Ok ( self . get_macro ( def) ) ,
342+ def @ _ => Ok ( def) ,
324343 } ,
325344 PathResult :: Module ( ..) => unreachable ! ( ) ,
326345 PathResult :: Indeterminate if !force => return Err ( Determinacy :: Undetermined ) ,
@@ -331,15 +350,15 @@ impl<'a> base::Resolver for Resolver<'a> {
331350 } ;
332351 self . current_module . macro_resolutions . borrow_mut ( )
333352 . push ( ( path. into_boxed_slice ( ) , span) ) ;
334- return ext ;
353+ return def ;
335354 }
336355
337356 let name = path[ 0 ] . name ;
338357 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 ( ) ) ,
341360 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 ( ) ) ,
343362 Err ( Determinacy :: Undetermined ) if !force =>
344363 return Err ( Determinacy :: Undetermined ) ,
345364 Err ( _) => {
@@ -354,9 +373,7 @@ impl<'a> base::Resolver for Resolver<'a> {
354373
355374 result
356375 }
357- }
358376
359- impl < ' a > Resolver < ' a > {
360377 // Resolve the initial segment of a non-global macro path (e.g. `foo` in `foo::bar!();`)
361378 pub fn resolve_lexical_macro_path_segment ( & mut self ,
362379 ident : Ident ,
@@ -597,33 +614,23 @@ impl<'a> Resolver<'a> {
597614 }
598615
599616 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" {
601620 self . session . span_err ( item. span , "user-defined macros may not be named `macro_rules`" ) ;
602621 }
603622
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) ;
614626 * 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 ,
619628 } ) ) ;
620- self . macro_names . insert ( item . ident . name ) ;
629+ self . macro_names . insert ( ident. name ) ;
621630
622631 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 } ) ;
627634 }
628635 }
629636
0 commit comments