11//! Defines database & queries for name resolution.
22use base_db:: { salsa, CrateId , SourceDatabase , Upcast } ;
33use either:: Either ;
4- use hir_expand:: { db:: ExpandDatabase , HirFileId } ;
4+ use hir_expand:: { db:: ExpandDatabase , HirFileId , MacroDefId } ;
55use intern:: Interned ;
66use la_arena:: ArenaMap ;
77use syntax:: { ast, AstPtr } ;
@@ -24,9 +24,9 @@ use crate::{
2424 AttrDefId , BlockId , BlockLoc , ConstBlockId , ConstBlockLoc , ConstId , ConstLoc , DefWithBodyId ,
2525 EnumId , EnumLoc , ExternBlockId , ExternBlockLoc , ExternCrateId , ExternCrateLoc , FunctionId ,
2626 FunctionLoc , GenericDefId , ImplId , ImplLoc , InTypeConstId , InTypeConstLoc , LocalEnumVariantId ,
27- LocalFieldId , Macro2Id , Macro2Loc , MacroRulesId , MacroRulesLoc , ProcMacroId , ProcMacroLoc ,
28- StaticId , StaticLoc , StructId , StructLoc , TraitAliasId , TraitAliasLoc , TraitId , TraitLoc ,
29- TypeAliasId , TypeAliasLoc , UnionId , UnionLoc , UseId , UseLoc , VariantId ,
27+ LocalFieldId , Macro2Id , Macro2Loc , MacroId , MacroRulesId , MacroRulesLoc , ProcMacroId ,
28+ ProcMacroLoc , StaticId , StaticLoc , StructId , StructLoc , TraitAliasId , TraitAliasLoc , TraitId ,
29+ TraitLoc , TypeAliasId , TypeAliasLoc , UnionId , UnionLoc , UseId , UseLoc , VariantId ,
3030} ;
3131
3232#[ salsa:: query_group( InternDatabaseStorage ) ]
@@ -110,6 +110,8 @@ pub trait DefDatabase: InternDatabase + ExpandDatabase + Upcast<dyn ExpandDataba
110110 #[ salsa:: invoke( DefMap :: block_def_map_query) ]
111111 fn block_def_map ( & self , block : BlockId ) -> Arc < DefMap > ;
112112
113+ fn macro_def ( & self , m : MacroId ) -> MacroDefId ;
114+
113115 // region:data
114116
115117 #[ salsa:: invoke( StructData :: struct_data_query) ]
@@ -305,3 +307,72 @@ fn crate_supports_no_std(db: &dyn DefDatabase, crate_id: CrateId) -> bool {
305307
306308 false
307309}
310+
311+ fn macro_def ( db : & dyn DefDatabase , id : MacroId ) -> MacroDefId {
312+ use hir_expand:: InFile ;
313+
314+ use crate :: { Lookup , MacroDefKind , MacroExpander } ;
315+
316+ let kind = |expander, file_id, m| {
317+ let in_file = InFile :: new ( file_id, m) ;
318+ match expander {
319+ MacroExpander :: Declarative => MacroDefKind :: Declarative ( in_file) ,
320+ MacroExpander :: BuiltIn ( it) => MacroDefKind :: BuiltIn ( it, in_file) ,
321+ MacroExpander :: BuiltInAttr ( it) => MacroDefKind :: BuiltInAttr ( it, in_file) ,
322+ MacroExpander :: BuiltInDerive ( it) => MacroDefKind :: BuiltInDerive ( it, in_file) ,
323+ MacroExpander :: BuiltInEager ( it) => MacroDefKind :: BuiltInEager ( it, in_file) ,
324+ }
325+ } ;
326+
327+ match id {
328+ MacroId :: Macro2Id ( it) => {
329+ let loc: Macro2Loc = it. lookup ( db) ;
330+
331+ let item_tree = loc. id . item_tree ( db) ;
332+ let makro = & item_tree[ loc. id . value ] ;
333+ MacroDefId {
334+ krate : loc. container . krate ,
335+ kind : kind ( loc. expander , loc. id . file_id ( ) , makro. ast_id . upcast ( ) ) ,
336+ local_inner : false ,
337+ allow_internal_unsafe : loc. allow_internal_unsafe ,
338+ span : db
339+ . span_map ( loc. id . file_id ( ) )
340+ . span_for_range ( db. ast_id_map ( loc. id . file_id ( ) ) . get ( makro. ast_id ) . text_range ( ) ) ,
341+ }
342+ }
343+ MacroId :: MacroRulesId ( it) => {
344+ let loc: MacroRulesLoc = it. lookup ( db) ;
345+
346+ let item_tree = loc. id . item_tree ( db) ;
347+ let makro = & item_tree[ loc. id . value ] ;
348+ MacroDefId {
349+ krate : loc. container . krate ,
350+ kind : kind ( loc. expander , loc. id . file_id ( ) , makro. ast_id . upcast ( ) ) ,
351+ local_inner : loc. local_inner ,
352+ allow_internal_unsafe : loc. allow_internal_unsafe ,
353+ span : db
354+ . span_map ( loc. id . file_id ( ) )
355+ . span_for_range ( db. ast_id_map ( loc. id . file_id ( ) ) . get ( makro. ast_id ) . text_range ( ) ) ,
356+ }
357+ }
358+ MacroId :: ProcMacroId ( it) => {
359+ let loc = it. lookup ( db) ;
360+
361+ let item_tree = loc. id . item_tree ( db) ;
362+ let makro = & item_tree[ loc. id . value ] ;
363+ MacroDefId {
364+ krate : loc. container . krate ,
365+ kind : MacroDefKind :: ProcMacro (
366+ loc. expander ,
367+ loc. kind ,
368+ InFile :: new ( loc. id . file_id ( ) , makro. ast_id ) ,
369+ ) ,
370+ local_inner : false ,
371+ allow_internal_unsafe : false ,
372+ span : db
373+ . span_map ( loc. id . file_id ( ) )
374+ . span_for_range ( db. ast_id_map ( loc. id . file_id ( ) ) . get ( makro. ast_id ) . text_range ( ) ) ,
375+ }
376+ }
377+ }
378+ }
0 commit comments