@@ -7,6 +7,7 @@ use hir_expand::{
77 builtin_derive:: find_builtin_derive,
88 builtin_macro:: find_builtin_macro,
99 name:: { name, AsName , Name } ,
10+ proc_macro:: ProcMacroExpander ,
1011 HirFileId , MacroCallId , MacroDefId , MacroDefKind ,
1112} ;
1213use ra_cfg:: CfgOptions ;
@@ -64,6 +65,9 @@ pub(super) fn collect_defs(db: &dyn DefDatabase, mut def_map: CrateDefMap) -> Cr
6465 unexpanded_attribute_macros : Vec :: new ( ) ,
6566 mod_dirs : FxHashMap :: default ( ) ,
6667 cfg_options,
68+
69+ // FIXME: pass proc-macro from crate-graph
70+ proc_macros : Default :: default ( ) ,
6771 } ;
6872 collector. collect ( ) ;
6973 collector. finish ( )
@@ -122,6 +126,7 @@ struct DefCollector<'a> {
122126 unexpanded_attribute_macros : Vec < DeriveDirective > ,
123127 mod_dirs : FxHashMap < LocalModuleId , ModDir > ,
124128 cfg_options : & ' a CfgOptions ,
129+ proc_macros : Vec < ( Name , ProcMacroExpander ) > ,
125130}
126131
127132impl DefCollector < ' _ > {
@@ -177,6 +182,24 @@ impl DefCollector<'_> {
177182 for directive in unresolved_imports {
178183 self . record_resolved_import ( & directive)
179184 }
185+
186+ // Record proc-macros
187+ self . collect_proc_macro ( ) ;
188+ }
189+
190+ fn collect_proc_macro ( & mut self ) {
191+ let proc_macros = std:: mem:: take ( & mut self . proc_macros ) ;
192+ for ( name, expander) in proc_macros {
193+ let krate = self . def_map . krate ;
194+
195+ let macro_id = MacroDefId {
196+ ast_id : None ,
197+ krate : Some ( krate) ,
198+ kind : MacroDefKind :: CustomDerive ( expander) ,
199+ } ;
200+
201+ self . define_proc_macro ( name. clone ( ) , macro_id) ;
202+ }
180203 }
181204
182205 /// Define a macro with `macro_rules`.
@@ -238,6 +261,18 @@ impl DefCollector<'_> {
238261 self . def_map . modules [ module_id] . scope . define_legacy_macro ( name, mac) ;
239262 }
240263
264+ /// Define a proc macro
265+ ///
266+ /// A proc macro is similar to normal macro scope, but it would not visiable in legacy textual scoped.
267+ /// And unconditionally exported.
268+ fn define_proc_macro ( & mut self , name : Name , macro_ : MacroDefId ) {
269+ self . update (
270+ self . def_map . root ,
271+ & [ ( name, PerNs :: macros ( macro_, Visibility :: Public ) ) ] ,
272+ Visibility :: Public ,
273+ ) ;
274+ }
275+
241276 /// Import macros from `#[macro_use] extern crate`.
242277 fn import_macros_from_extern_crate (
243278 & mut self ,
@@ -537,8 +572,9 @@ impl DefCollector<'_> {
537572 true
538573 } ) ;
539574 attribute_macros. retain ( |directive| {
540- if let Some ( call_id) =
541- directive. ast_id . as_call_id ( self . db , |path| self . resolve_attribute_macro ( & path) )
575+ if let Some ( call_id) = directive
576+ . ast_id
577+ . as_call_id ( self . db , |path| self . resolve_attribute_macro ( & directive, & path) )
542578 {
543579 resolved. push ( ( directive. module_id , call_id, 0 ) ) ;
544580 res = ReachedFixedPoint :: No ;
@@ -562,9 +598,11 @@ impl DefCollector<'_> {
562598 res
563599 }
564600
565- fn resolve_attribute_macro ( & self , path : & ModPath ) -> Option < MacroDefId > {
566- // FIXME this is currently super hacky, just enough to support the
567- // built-in derives
601+ fn resolve_attribute_macro (
602+ & self ,
603+ directive : & DeriveDirective ,
604+ path : & ModPath ,
605+ ) -> Option < MacroDefId > {
568606 if let Some ( name) = path. as_ident ( ) {
569607 // FIXME this should actually be handled with the normal name
570608 // resolution; the std lib defines built-in stubs for the derives,
@@ -573,7 +611,15 @@ impl DefCollector<'_> {
573611 return Some ( def_id) ;
574612 }
575613 }
576- None
614+ let resolved_res = self . def_map . resolve_path_fp_with_macro (
615+ self . db ,
616+ ResolveMode :: Other ,
617+ directive. module_id ,
618+ & path,
619+ BuiltinShadowMode :: Module ,
620+ ) ;
621+
622+ resolved_res. resolved_def . take_macros ( )
577623 }
578624
579625 fn collect_macro_expansion (
@@ -776,7 +822,6 @@ impl ModCollector<'_, '_> {
776822 // FIXME: check attrs to see if this is an attribute macro invocation;
777823 // in which case we don't add the invocation, just a single attribute
778824 // macro invocation
779-
780825 self . collect_derives ( attrs, def) ;
781826
782827 let name = def. name . clone ( ) ;
@@ -955,6 +1000,7 @@ mod tests {
9551000 unexpanded_attribute_macros : Vec :: new ( ) ,
9561001 mod_dirs : FxHashMap :: default ( ) ,
9571002 cfg_options : & CfgOptions :: default ( ) ,
1003+ proc_macros : Default :: default ( ) ,
9581004 } ;
9591005 collector. collect ( ) ;
9601006 collector. def_map
0 commit comments