1
1
//! Defines database & queries for name resolution.
2
2
use base_db:: { salsa, CrateId , SourceDatabase , Upcast } ;
3
3
use either:: Either ;
4
- use hir_expand:: { db:: ExpandDatabase , HirFileId } ;
4
+ use hir_expand:: { db:: ExpandDatabase , HirFileId , MacroDefId } ;
5
5
use intern:: Interned ;
6
6
use la_arena:: ArenaMap ;
7
7
use syntax:: { ast, AstPtr } ;
@@ -24,9 +24,9 @@ use crate::{
24
24
AttrDefId , BlockId , BlockLoc , ConstBlockId , ConstBlockLoc , ConstId , ConstLoc , DefWithBodyId ,
25
25
EnumId , EnumLoc , ExternBlockId , ExternBlockLoc , ExternCrateId , ExternCrateLoc , FunctionId ,
26
26
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 ,
30
30
} ;
31
31
32
32
#[ salsa:: query_group( InternDatabaseStorage ) ]
@@ -110,6 +110,8 @@ pub trait DefDatabase: InternDatabase + ExpandDatabase + Upcast<dyn ExpandDataba
110
110
#[ salsa:: invoke( DefMap :: block_def_map_query) ]
111
111
fn block_def_map ( & self , block : BlockId ) -> Arc < DefMap > ;
112
112
113
+ fn macro_def ( & self , m : MacroId ) -> MacroDefId ;
114
+
113
115
// region:data
114
116
115
117
#[ salsa:: invoke( StructData :: struct_data_query) ]
@@ -305,3 +307,63 @@ fn crate_supports_no_std(db: &dyn DefDatabase, crate_id: CrateId) -> bool {
305
307
306
308
false
307
309
}
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 = 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
+ }
339
+ }
340
+ MacroId :: MacroRulesId ( it) => {
341
+ let loc = it. lookup ( db) ;
342
+
343
+ let item_tree = loc. id . item_tree ( db) ;
344
+ let makro = & item_tree[ loc. id . value ] ;
345
+ MacroDefId {
346
+ krate : loc. container . krate ,
347
+ kind : kind ( loc. expander , loc. id . file_id ( ) , makro. ast_id . upcast ( ) ) ,
348
+ local_inner : loc. local_inner ,
349
+ allow_internal_unsafe : loc. allow_internal_unsafe ,
350
+ }
351
+ }
352
+ MacroId :: ProcMacroId ( it) => {
353
+ let loc = it. lookup ( db) ;
354
+
355
+ let item_tree = loc. id . item_tree ( db) ;
356
+ let makro = & item_tree[ loc. id . value ] ;
357
+ MacroDefId {
358
+ krate : loc. container . krate ,
359
+ kind : MacroDefKind :: ProcMacro (
360
+ loc. expander ,
361
+ loc. kind ,
362
+ InFile :: new ( loc. id . file_id ( ) , makro. ast_id ) ,
363
+ ) ,
364
+ local_inner : false ,
365
+ allow_internal_unsafe : false ,
366
+ }
367
+ }
368
+ }
369
+ }
0 commit comments