@@ -89,11 +89,12 @@ impl fmt::Debug for RawVisibilityId {
89
89
/// The item tree of a source file.
90
90
#[ derive( Debug , Default , Eq , PartialEq ) ]
91
91
pub struct ItemTree {
92
- top_level : SmallVec < [ ModItem ; 1 ] > ,
92
+ top_level : SmallVec < [ ModItemId ; 1 ] > ,
93
93
// Consider splitting this into top level RawAttrs and the map?
94
94
attrs : FxHashMap < AttrOwner , RawAttrs > ,
95
95
96
- data : Option < Box < ItemTreeData > > ,
96
+ vis : ItemVisibilities ,
97
+ data : FxHashMap < FileAstId < ast:: Item > , ModItem > ,
97
98
}
98
99
99
100
impl ItemTree {
@@ -130,14 +131,15 @@ impl ItemTree {
130
131
if let Some ( attrs) = top_attrs {
131
132
item_tree. attrs . insert ( AttrOwner :: TopLevel , attrs) ;
132
133
}
133
- if item_tree. data . is_none ( ) && item_tree. top_level . is_empty ( ) && item_tree. attrs . is_empty ( )
134
+ if item_tree. data . is_empty ( ) && item_tree. top_level . is_empty ( ) && item_tree. attrs . is_empty ( )
134
135
{
135
136
EMPTY
136
137
. get_or_init ( || {
137
138
Arc :: new ( ItemTree {
138
139
top_level : SmallVec :: new_const ( ) ,
139
140
attrs : FxHashMap :: default ( ) ,
140
- data : None ,
141
+ data : FxHashMap :: default ( ) ,
142
+ vis : ItemVisibilities { arena : Box :: new ( [ ] ) } ,
141
143
} )
142
144
} )
143
145
. clone ( )
@@ -156,14 +158,15 @@ impl ItemTree {
156
158
157
159
let ctx = lower:: Ctx :: new ( db, loc. ast_id . file_id ) ;
158
160
let mut item_tree = ctx. lower_block ( & block) ;
159
- if item_tree. data . is_none ( ) && item_tree. top_level . is_empty ( ) && item_tree. attrs . is_empty ( )
161
+ if item_tree. data . is_empty ( ) && item_tree. top_level . is_empty ( ) && item_tree. attrs . is_empty ( )
160
162
{
161
163
EMPTY
162
164
. get_or_init ( || {
163
165
Arc :: new ( ItemTree {
164
166
top_level : SmallVec :: new_const ( ) ,
165
167
attrs : FxHashMap :: default ( ) ,
166
- data : None ,
168
+ data : FxHashMap :: default ( ) ,
169
+ vis : ItemVisibilities { arena : Box :: new ( [ ] ) } ,
167
170
} )
168
171
} )
169
172
. clone ( )
@@ -175,7 +178,7 @@ impl ItemTree {
175
178
176
179
/// Returns an iterator over all items located at the top level of the `HirFileId` this
177
180
/// `ItemTree` was created from.
178
- pub ( crate ) fn top_level_items ( & self ) -> & [ ModItem ] {
181
+ pub ( crate ) fn top_level_items ( & self ) -> & [ ModItemId ] {
179
182
& self . top_level
180
183
}
181
184
@@ -200,74 +203,33 @@ impl ItemTree {
200
203
///
201
204
/// For more detail, see [`ItemTreeDataStats`].
202
205
pub fn item_tree_stats ( & self ) -> ItemTreeDataStats {
203
- match self . data {
204
- Some ( ref data) => ItemTreeDataStats {
205
- traits : data. traits . len ( ) ,
206
- impls : data. impls . len ( ) ,
207
- mods : data. mods . len ( ) ,
208
- macro_calls : data. macro_calls . len ( ) ,
209
- macro_rules : data. macro_rules . len ( ) ,
210
- } ,
211
- None => ItemTreeDataStats :: default ( ) ,
206
+ let mut traits = 0 ;
207
+ let mut impls = 0 ;
208
+ let mut mods = 0 ;
209
+ let mut macro_calls = 0 ;
210
+ let mut macro_rules = 0 ;
211
+ for item in self . data . values ( ) {
212
+ match item {
213
+ ModItem :: Trait ( _) => traits += 1 ,
214
+ ModItem :: Impl ( _) => impls += 1 ,
215
+ ModItem :: Mod ( _) => mods += 1 ,
216
+ ModItem :: MacroCall ( _) => macro_calls += 1 ,
217
+ ModItem :: MacroRules ( _) => macro_rules += 1 ,
218
+ _ => { }
219
+ }
212
220
}
221
+ ItemTreeDataStats { traits, impls, mods, macro_calls, macro_rules }
213
222
}
214
223
215
224
pub fn pretty_print ( & self , db : & dyn DefDatabase , edition : Edition ) -> String {
216
225
pretty:: print_item_tree ( db, self , edition)
217
226
}
218
227
219
- fn data ( & self ) -> & ItemTreeData {
220
- self . data . as_ref ( ) . expect ( "attempted to access data of empty ItemTree" )
221
- }
222
-
223
- fn data_mut ( & mut self ) -> & mut ItemTreeData {
224
- self . data . get_or_insert_with ( Box :: default)
225
- }
226
-
227
228
fn shrink_to_fit ( & mut self ) {
228
- let ItemTree { top_level, attrs, data } = self ;
229
+ let ItemTree { top_level, attrs, data, vis : _ } = self ;
229
230
top_level. shrink_to_fit ( ) ;
230
231
attrs. shrink_to_fit ( ) ;
231
- if let Some ( data) = data {
232
- let ItemTreeData {
233
- uses,
234
- extern_crates,
235
- extern_blocks,
236
- functions,
237
- structs,
238
- unions,
239
- enums,
240
- consts,
241
- statics,
242
- traits,
243
- trait_aliases,
244
- impls,
245
- type_aliases,
246
- mods,
247
- macro_calls,
248
- macro_rules,
249
- macro_defs,
250
- vis : _,
251
- } = & mut * * data;
252
-
253
- uses. shrink_to_fit ( ) ;
254
- extern_crates. shrink_to_fit ( ) ;
255
- extern_blocks. shrink_to_fit ( ) ;
256
- functions. shrink_to_fit ( ) ;
257
- structs. shrink_to_fit ( ) ;
258
- unions. shrink_to_fit ( ) ;
259
- enums. shrink_to_fit ( ) ;
260
- consts. shrink_to_fit ( ) ;
261
- statics. shrink_to_fit ( ) ;
262
- traits. shrink_to_fit ( ) ;
263
- trait_aliases. shrink_to_fit ( ) ;
264
- impls. shrink_to_fit ( ) ;
265
- type_aliases. shrink_to_fit ( ) ;
266
- mods. shrink_to_fit ( ) ;
267
- macro_calls. shrink_to_fit ( ) ;
268
- macro_rules. shrink_to_fit ( ) ;
269
- macro_defs. shrink_to_fit ( ) ;
270
- }
232
+ data. shrink_to_fit ( ) ;
271
233
}
272
234
}
273
235
@@ -276,29 +238,26 @@ struct ItemVisibilities {
276
238
arena : Box < [ RawVisibility ] > ,
277
239
}
278
240
279
- #[ derive( Default , Debug , Eq , PartialEq ) ]
280
- struct ItemTreeData {
281
- uses : FxHashMap < ItemTreeAstId < Use > , Use > ,
282
- extern_crates : FxHashMap < ItemTreeAstId < ExternCrate > , ExternCrate > ,
283
- extern_blocks : FxHashMap < ItemTreeAstId < ExternBlock > , ExternBlock > ,
284
- functions : FxHashMap < ItemTreeAstId < Function > , Function > ,
285
- structs : FxHashMap < ItemTreeAstId < Struct > , Struct > ,
286
- unions : FxHashMap < ItemTreeAstId < Union > , Union > ,
287
- enums : FxHashMap < ItemTreeAstId < Enum > , Enum > ,
288
- consts : FxHashMap < ItemTreeAstId < Const > , Const > ,
289
- statics : FxHashMap < ItemTreeAstId < Static > , Static > ,
290
- traits : FxHashMap < ItemTreeAstId < Trait > , Trait > ,
291
- trait_aliases : FxHashMap < ItemTreeAstId < TraitAlias > , TraitAlias > ,
292
- impls : FxHashMap < ItemTreeAstId < Impl > , Impl > ,
293
- type_aliases : FxHashMap < ItemTreeAstId < TypeAlias > , TypeAlias > ,
294
- mods : FxHashMap < ItemTreeAstId < Mod > , Mod > ,
295
- macro_calls : FxHashMap < ItemTreeAstId < MacroCall > , MacroCall > ,
296
- macro_rules : FxHashMap < ItemTreeAstId < MacroRules > , MacroRules > ,
297
- macro_defs : FxHashMap < ItemTreeAstId < Macro2 > , Macro2 > ,
298
-
299
- vis : ItemVisibilities ,
241
+ #[ derive( Debug , Clone , Eq , PartialEq ) ]
242
+ enum ModItem {
243
+ Const ( Const ) ,
244
+ Enum ( Enum ) ,
245
+ ExternBlock ( ExternBlock ) ,
246
+ ExternCrate ( ExternCrate ) ,
247
+ Function ( Function ) ,
248
+ Impl ( Impl ) ,
249
+ Macro2 ( Macro2 ) ,
250
+ MacroCall ( MacroCall ) ,
251
+ MacroRules ( MacroRules ) ,
252
+ Mod ( Mod ) ,
253
+ Static ( Static ) ,
254
+ Struct ( Struct ) ,
255
+ Trait ( Trait ) ,
256
+ TraitAlias ( TraitAlias ) ,
257
+ TypeAlias ( TypeAlias ) ,
258
+ Union ( Union ) ,
259
+ Use ( Use ) ,
300
260
}
301
-
302
261
#[ derive( Default , Debug , Eq , PartialEq ) ]
303
262
pub struct ItemTreeDataStats {
304
263
pub traits : usize ,
@@ -316,9 +275,9 @@ pub enum AttrOwner {
316
275
TopLevel ,
317
276
}
318
277
319
- impl From < ModItem > for AttrOwner {
278
+ impl From < ModItemId > for AttrOwner {
320
279
#[ inline]
321
- fn from ( value : ModItem ) -> Self {
280
+ fn from ( value : ModItemId ) -> Self {
322
281
AttrOwner :: Item ( value. ast_id ( ) . erase ( ) )
323
282
}
324
283
}
@@ -385,7 +344,7 @@ macro_rules! mod_items {
385
344
$(
386
345
impl From <FileAstId <$ast>> for $mod_item {
387
346
fn from( id: FileAstId <$ast>) -> $mod_item {
388
- ModItem :: $typ( id)
347
+ ModItemId :: $typ( id)
389
348
}
390
349
}
391
350
) +
@@ -399,23 +358,29 @@ macro_rules! mod_items {
399
358
}
400
359
401
360
fn lookup( tree: & ItemTree , index: FileAstId <$ast>) -> & Self {
402
- & tree. data( ) . $fld[ & index]
361
+ match & tree. data[ & index. upcast( ) ] {
362
+ ModItem :: $typ( item) => item,
363
+ _ => panic!( "expected item of type `{}` at index `{:?}`" , stringify!( $typ) , index) ,
364
+ }
403
365
}
404
366
}
405
367
406
368
impl Index <FileAstId <$ast>> for ItemTree {
407
369
type Output = $typ;
408
370
409
371
fn index( & self , index: FileAstId <$ast>) -> & Self :: Output {
410
- & self . data( ) . $fld[ & index]
372
+ match & self . data[ & index. upcast( ) ] {
373
+ ModItem :: $typ( item) => item,
374
+ _ => panic!( "expected item of type `{}` at index `{:?}`" , stringify!( $typ) , index) ,
375
+ }
411
376
}
412
377
}
413
378
) +
414
379
} ;
415
380
}
416
381
417
382
mod_items ! {
418
- ModItem ->
383
+ ModItemId ->
419
384
Use in uses -> ast:: Use ,
420
385
ExternCrate in extern_crates -> ast:: ExternCrate ,
421
386
ExternBlock in extern_blocks -> ast:: ExternBlock ,
@@ -463,7 +428,7 @@ impl Index<RawVisibilityId> for ItemTree {
463
428
VisibilityExplicitness :: Explicit ,
464
429
)
465
430
} ) ,
466
- _ => & self . data ( ) . vis . arena [ index. 0 as usize ] ,
431
+ _ => & self . vis . arena [ index. 0 as usize ] ,
467
432
}
468
433
}
469
434
}
@@ -541,7 +506,7 @@ pub struct ExternCrate {
541
506
#[ derive( Debug , Clone , Eq , PartialEq ) ]
542
507
pub struct ExternBlock {
543
508
pub ast_id : FileAstId < ast:: ExternBlock > ,
544
- pub ( crate ) children : Box < [ ModItem ] > ,
509
+ pub ( crate ) children : Box < [ ModItemId ] > ,
545
510
}
546
511
547
512
#[ derive( Debug , Clone , Eq , PartialEq ) ]
@@ -656,7 +621,7 @@ pub struct Mod {
656
621
#[ derive( Debug , Clone , Eq , PartialEq ) ]
657
622
pub ( crate ) enum ModKind {
658
623
/// `mod m { ... }`
659
- Inline { items : Box < [ ModItem ] > } ,
624
+ Inline { items : Box < [ ModItemId ] > } ,
660
625
/// `mod m;`
661
626
Outline ,
662
627
}
0 commit comments