66
77use either:: Either ;
88use hir_expand:: { attrs:: collect_attrs, HirFileId } ;
9- use syntax:: ast;
9+ use syntax:: { ast, AstPtr } ;
1010
1111use crate :: {
1212 db:: DefDatabase ,
@@ -38,7 +38,7 @@ impl ChildBySource for TraitId {
3838
3939 data. attribute_calls ( ) . filter ( |( ast_id, _) | ast_id. file_id == file_id) . for_each (
4040 |( ast_id, call_id) | {
41- res[ keys:: ATTR_MACRO_CALL ] . insert ( ast_id. to_node ( db. upcast ( ) ) , call_id) ;
41+ res[ keys:: ATTR_MACRO_CALL ] . insert ( ast_id. to_ptr ( db. upcast ( ) ) , call_id) ;
4242 } ,
4343 ) ;
4444 data. items . iter ( ) . for_each ( |& ( _, item) | {
@@ -50,9 +50,10 @@ impl ChildBySource for TraitId {
5050impl ChildBySource for ImplId {
5151 fn child_by_source_to ( & self , db : & dyn DefDatabase , res : & mut DynMap , file_id : HirFileId ) {
5252 let data = db. impl_data ( * self ) ;
53+ // FIXME: Macro calls
5354 data. attribute_calls ( ) . filter ( |( ast_id, _) | ast_id. file_id == file_id) . for_each (
5455 |( ast_id, call_id) | {
55- res[ keys:: ATTR_MACRO_CALL ] . insert ( ast_id. to_node ( db. upcast ( ) ) , call_id) ;
56+ res[ keys:: ATTR_MACRO_CALL ] . insert ( ast_id. to_ptr ( db. upcast ( ) ) , call_id) ;
5657 } ,
5758 ) ;
5859 data. items . iter ( ) . for_each ( |& item| {
@@ -80,15 +81,15 @@ impl ChildBySource for ItemScope {
8081 . for_each ( |konst| insert_item_loc ( db, res, file_id, konst, keys:: CONST ) ) ;
8182 self . attr_macro_invocs ( ) . filter ( |( id, _) | id. file_id == file_id) . for_each (
8283 |( ast_id, call_id) | {
83- res[ keys:: ATTR_MACRO_CALL ] . insert ( ast_id. to_node ( db. upcast ( ) ) , call_id) ;
84+ res[ keys:: ATTR_MACRO_CALL ] . insert ( ast_id. to_ptr ( db. upcast ( ) ) , call_id) ;
8485 } ,
8586 ) ;
8687 self . legacy_macros ( ) . for_each ( |( _, ids) | {
8788 ids. iter ( ) . for_each ( |& id| {
8889 if let MacroId :: MacroRulesId ( id) = id {
8990 let loc = id. lookup ( db) ;
9091 if loc. id . file_id ( ) == file_id {
91- res[ keys:: MACRO_RULES ] . insert ( loc. source ( db) . value , id) ;
92+ res[ keys:: MACRO_RULES ] . insert ( loc. ast_ptr ( db) . value , id) ;
9293 }
9394 }
9495 } )
@@ -100,12 +101,18 @@ impl ChildBySource for ItemScope {
100101 if let Some ( ( _, Either :: Left ( attr) ) ) =
101102 collect_attrs ( & adt) . nth ( attr_id. ast_index ( ) )
102103 {
103- res[ keys:: DERIVE_MACRO_CALL ] . insert ( attr, ( attr_id, call_id, calls. into ( ) ) ) ;
104+ res[ keys:: DERIVE_MACRO_CALL ]
105+ . insert ( AstPtr :: new ( & attr) , ( attr_id, call_id, calls. into ( ) ) ) ;
104106 }
105107 } ) ;
106108 } ,
107109 ) ;
108-
110+ self . iter_macro_invoc ( ) . filter ( |( id, _) | id. file_id == file_id) . for_each (
111+ |( ast_id, & call) | {
112+ let ast = ast_id. to_ptr ( db. upcast ( ) ) ;
113+ res[ keys:: MACRO_CALL ] . insert ( ast, call) ;
114+ } ,
115+ ) ;
109116 fn add_module_def (
110117 db : & dyn DefDatabase ,
111118 map : & mut DynMap ,
@@ -155,8 +162,8 @@ impl ChildBySource for VariantId {
155162 for ( local_id, source) in arena_map. value . iter ( ) {
156163 let id = FieldId { parent, local_id } ;
157164 match source. clone ( ) {
158- Either :: Left ( source) => res[ keys:: TUPLE_FIELD ] . insert ( source, id) ,
159- Either :: Right ( source) => res[ keys:: RECORD_FIELD ] . insert ( source, id) ,
165+ Either :: Left ( source) => res[ keys:: TUPLE_FIELD ] . insert ( AstPtr :: new ( & source) , id) ,
166+ Either :: Right ( source) => res[ keys:: RECORD_FIELD ] . insert ( AstPtr :: new ( & source) , id) ,
160167 }
161168 }
162169 }
@@ -171,29 +178,30 @@ impl ChildBySource for EnumId {
171178
172179 let tree = loc. id . item_tree ( db) ;
173180 let ast_id_map = db. ast_id_map ( loc. id . file_id ( ) ) ;
174- let root = db. parse_or_expand ( loc. id . file_id ( ) ) ;
175181
176182 db. enum_data ( * self ) . variants . iter ( ) . for_each ( |& ( variant, _) | {
177- res[ keys:: ENUM_VARIANT ] . insert (
178- ast_id_map. get ( tree[ variant. lookup ( db) . id . value ] . ast_id ) . to_node ( & root) ,
179- variant,
180- ) ;
183+ res[ keys:: ENUM_VARIANT ]
184+ . insert ( ast_id_map. get ( tree[ variant. lookup ( db) . id . value ] . ast_id ) , variant) ;
181185 } ) ;
182186 }
183187}
184188
185189impl ChildBySource for DefWithBodyId {
186190 fn child_by_source_to ( & self , db : & dyn DefDatabase , res : & mut DynMap , file_id : HirFileId ) {
187- let body = db. body ( * self ) ;
191+ let ( body, sm ) = db. body_with_source_map ( * self ) ;
188192 if let & DefWithBodyId :: VariantId ( v) = self {
189193 VariantId :: EnumVariantId ( v) . child_by_source_to ( db, res, file_id)
190194 }
191195
196+ sm. expansions ( ) . filter ( |( ast, _) | ast. file_id == file_id) . for_each ( |( ast, & exp_id) | {
197+ res[ keys:: MACRO_CALL ] . insert ( ast. value , exp_id. macro_call_id ) ;
198+ } ) ;
199+
192200 for ( block, def_map) in body. blocks ( db) {
193201 // All block expressions are merged into the same map, because they logically all add
194202 // inner items to the containing `DefWithBodyId`.
195203 def_map[ DefMap :: ROOT ] . scope . child_by_source_to ( db, res, file_id) ;
196- res[ keys:: BLOCK ] . insert ( block. lookup ( db) . ast_id . to_node ( db. upcast ( ) ) , block) ;
204+ res[ keys:: BLOCK ] . insert ( block. lookup ( db) . ast_id . to_ptr ( db. upcast ( ) ) , block) ;
197205 }
198206 }
199207}
@@ -220,13 +228,17 @@ impl ChildBySource for GenericDefId {
220228 {
221229 let id = TypeOrConstParamId { parent : * self , local_id } ;
222230 match ast_param {
223- ast:: TypeOrConstParam :: Type ( a) => res[ keys:: TYPE_PARAM ] . insert ( a, id) ,
224- ast:: TypeOrConstParam :: Const ( a) => res[ keys:: CONST_PARAM ] . insert ( a, id) ,
231+ ast:: TypeOrConstParam :: Type ( a) => {
232+ res[ keys:: TYPE_PARAM ] . insert ( AstPtr :: new ( & a) , id)
233+ }
234+ ast:: TypeOrConstParam :: Const ( a) => {
235+ res[ keys:: CONST_PARAM ] . insert ( AstPtr :: new ( & a) , id)
236+ }
225237 }
226238 }
227239 for ( local_id, ast_param) in lts_idx_iter. zip ( generic_params_list. lifetime_params ( ) ) {
228240 let id = LifetimeParamId { parent : * self , local_id } ;
229- res[ keys:: LIFETIME_PARAM ] . insert ( ast_param, id) ;
241+ res[ keys:: LIFETIME_PARAM ] . insert ( AstPtr :: new ( & ast_param) , id) ;
230242 }
231243 }
232244 }
@@ -246,7 +258,7 @@ fn insert_item_loc<ID, N, Data>(
246258{
247259 let loc = id. lookup ( db) ;
248260 if loc. item_tree_id ( ) . file_id ( ) == file_id {
249- res[ key] . insert ( loc. source ( db) . value , id)
261+ res[ key] . insert ( loc. ast_ptr ( db) . value , id)
250262 }
251263}
252264
0 commit comments