@@ -22,6 +22,7 @@ use hir::def_id::{CRATE_DEF_INDEX, DefId, LocalDefId, DefIndexAddressSpace};
2222use syntax:: abi:: Abi ;
2323use syntax:: ast:: { self , Name , NodeId , CRATE_NODE_ID } ;
2424use syntax:: codemap:: Spanned ;
25+ use syntax:: ext:: base:: MacroKind ;
2526use syntax_pos:: Span ;
2627
2728use hir:: * ;
@@ -32,13 +33,15 @@ use util::nodemap::{DefIdMap, FxHashMap};
3233use arena:: TypedArena ;
3334use std:: cell:: RefCell ;
3435use std:: io;
36+ use ty:: TyCtxt ;
3537
3638pub mod blocks;
3739mod collector;
3840mod def_collector;
3941pub mod definitions;
4042mod hir_id_validator;
4143
44+
4245pub const ITEM_LIKE_SPACE : DefIndexAddressSpace = DefIndexAddressSpace :: Low ;
4346pub const REGULAR_SPACE : DefIndexAddressSpace = DefIndexAddressSpace :: High ;
4447
@@ -373,6 +376,92 @@ impl<'hir> Map<'hir> {
373376 self . definitions . as_local_node_id ( def_id. to_def_id ( ) ) . unwrap ( )
374377 }
375378
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+
376465 fn entry_count ( & self ) -> usize {
377466 self . map . len ( )
378467 }
@@ -1275,3 +1364,12 @@ fn node_id_to_string(map: &Map, id: NodeId, include_id: bool) -> String {
12751364 }
12761365 }
12771366}
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