@@ -22,6 +22,7 @@ use hir::def_id::{CRATE_DEF_INDEX, DefId, LocalDefId, DefIndexAddressSpace};
22
22
use syntax:: abi:: Abi ;
23
23
use syntax:: ast:: { self , Name , NodeId , CRATE_NODE_ID } ;
24
24
use syntax:: codemap:: Spanned ;
25
+ use syntax:: ext:: base:: MacroKind ;
25
26
use syntax_pos:: Span ;
26
27
27
28
use hir:: * ;
@@ -32,13 +33,15 @@ use util::nodemap::{DefIdMap, FxHashMap};
32
33
use arena:: TypedArena ;
33
34
use std:: cell:: RefCell ;
34
35
use std:: io;
36
+ use ty:: TyCtxt ;
35
37
36
38
pub mod blocks;
37
39
mod collector;
38
40
mod def_collector;
39
41
pub mod definitions;
40
42
mod hir_id_validator;
41
43
44
+
42
45
pub const ITEM_LIKE_SPACE : DefIndexAddressSpace = DefIndexAddressSpace :: Low ;
43
46
pub const REGULAR_SPACE : DefIndexAddressSpace = DefIndexAddressSpace :: High ;
44
47
@@ -373,6 +376,92 @@ impl<'hir> Map<'hir> {
373
376
self . definitions . as_local_node_id ( def_id. to_def_id ( ) ) . unwrap ( )
374
377
}
375
378
379
+ pub fn describe_def ( & self , node_id : NodeId ) -> Option < Def > {
380
+ let node = if let Some ( node) = self . find ( node_id) {
381
+ node
382
+ } else {
383
+ return None
384
+ } ;
385
+
386
+ match node {
387
+ NodeItem ( item) => {
388
+ let def_id = || {
389
+ self . local_def_id ( item. id )
390
+ } ;
391
+
392
+ match item. node {
393
+ ItemStatic ( _, m, _) => Some ( Def :: Static ( def_id ( ) ,
394
+ m == MutMutable ) ) ,
395
+ ItemConst ( ..) => Some ( Def :: Const ( def_id ( ) ) ) ,
396
+ ItemFn ( ..) => Some ( Def :: Fn ( def_id ( ) ) ) ,
397
+ ItemMod ( ..) => Some ( Def :: Mod ( def_id ( ) ) ) ,
398
+ ItemGlobalAsm ( ..) => Some ( Def :: GlobalAsm ( def_id ( ) ) ) ,
399
+ ItemTy ( ..) => Some ( Def :: TyAlias ( def_id ( ) ) ) ,
400
+ ItemEnum ( ..) => Some ( Def :: Enum ( def_id ( ) ) ) ,
401
+ ItemStruct ( ..) => Some ( Def :: Struct ( def_id ( ) ) ) ,
402
+ ItemUnion ( ..) => Some ( Def :: Union ( def_id ( ) ) ) ,
403
+ ItemTrait ( ..) => Some ( Def :: Trait ( def_id ( ) ) ) ,
404
+ ItemTraitAlias ( ..) => {
405
+ bug ! ( "trait aliases are not yet implemented (see issue #41517)" )
406
+ } ,
407
+ ItemExternCrate ( _) |
408
+ ItemUse ( ..) |
409
+ ItemForeignMod ( ..) |
410
+ ItemImpl ( ..) => None ,
411
+ }
412
+ }
413
+ NodeForeignItem ( item) => {
414
+ let def_id = self . local_def_id ( item. id ) ;
415
+ match item. node {
416
+ ForeignItemFn ( ..) => Some ( Def :: Fn ( def_id) ) ,
417
+ ForeignItemStatic ( _, m) => Some ( Def :: Static ( def_id, m) ) ,
418
+ ForeignItemType => Some ( Def :: TyForeign ( def_id) ) ,
419
+ }
420
+ }
421
+ NodeTraitItem ( item) => {
422
+ let def_id = self . local_def_id ( item. id ) ;
423
+ match item. node {
424
+ TraitItemKind :: Const ( ..) => Some ( Def :: AssociatedConst ( def_id) ) ,
425
+ TraitItemKind :: Method ( ..) => Some ( Def :: Method ( def_id) ) ,
426
+ TraitItemKind :: Type ( ..) => Some ( Def :: AssociatedTy ( def_id) ) ,
427
+ }
428
+ }
429
+ NodeImplItem ( item) => {
430
+ let def_id = self . local_def_id ( item. id ) ;
431
+ match item. node {
432
+ ImplItemKind :: Const ( ..) => Some ( Def :: AssociatedConst ( def_id) ) ,
433
+ ImplItemKind :: Method ( ..) => Some ( Def :: Method ( def_id) ) ,
434
+ ImplItemKind :: Type ( ..) => Some ( Def :: AssociatedTy ( def_id) ) ,
435
+ }
436
+ }
437
+ NodeVariant ( variant) => {
438
+ let def_id = self . local_def_id ( variant. node . data . id ( ) ) ;
439
+ Some ( Def :: Variant ( def_id) )
440
+ }
441
+ NodeField ( _) |
442
+ NodeExpr ( _) |
443
+ NodeStmt ( _) |
444
+ NodeTy ( _) |
445
+ NodeTraitRef ( _) |
446
+ NodePat ( _) |
447
+ NodeBinding ( _) |
448
+ NodeStructCtor ( _) |
449
+ NodeLifetime ( _) |
450
+ NodeVisibility ( _) |
451
+ NodeBlock ( _) => None ,
452
+ NodeLocal ( local) => {
453
+ Some ( Def :: Local ( local. id ) )
454
+ }
455
+ NodeMacroDef ( macro_def) => {
456
+ Some ( Def :: Macro ( self . local_def_id ( macro_def. id ) ,
457
+ MacroKind :: Bang ) )
458
+ }
459
+ NodeTyParam ( param) => {
460
+ Some ( Def :: TyParam ( self . local_def_id ( param. id ) ) )
461
+ }
462
+ }
463
+ }
464
+
376
465
fn entry_count ( & self ) -> usize {
377
466
self . map . len ( )
378
467
}
@@ -1275,3 +1364,12 @@ fn node_id_to_string(map: &Map, id: NodeId, include_id: bool) -> String {
1275
1364
}
1276
1365
}
1277
1366
}
1367
+
1368
+ pub fn describe_def ( tcx : TyCtxt , def_id : DefId ) -> Option < Def > {
1369
+ if let Some ( node_id) = tcx. hir . as_local_node_id ( def_id) {
1370
+ tcx. hir . describe_def ( node_id)
1371
+ } else {
1372
+ bug ! ( "Calling local describe_def query provider for upstream DefId: {:?}" ,
1373
+ def_id)
1374
+ }
1375
+ }
0 commit comments