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,72 @@ 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: 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