Skip to content

Commit 8ff633c

Browse files
Implement describe_def query for LOCAL_CRATE
1 parent 15ff0ad commit 8ff633c

File tree

3 files changed

+105
-0
lines changed

3 files changed

+105
-0
lines changed

src/librustc/hir/map/mod.rs

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use hir::def_id::{CRATE_DEF_INDEX, DefId, LocalDefId, DefIndexAddressSpace};
2222
use syntax::abi::Abi;
2323
use syntax::ast::{self, Name, NodeId, CRATE_NODE_ID};
2424
use syntax::codemap::Spanned;
25+
use syntax::ext::base::MacroKind;
2526
use syntax_pos::Span;
2627

2728
use hir::*;
@@ -32,13 +33,15 @@ use util::nodemap::{DefIdMap, FxHashMap};
3233
use arena::TypedArena;
3334
use std::cell::RefCell;
3435
use std::io;
36+
use ty::TyCtxt;
3537

3638
pub mod blocks;
3739
mod collector;
3840
mod def_collector;
3941
pub mod definitions;
4042
mod hir_id_validator;
4143

44+
4245
pub const ITEM_LIKE_SPACE: DefIndexAddressSpace = DefIndexAddressSpace::Low;
4346
pub 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+
}

src/librustc/hir/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ use syntax::tokenstream::TokenStream;
4343
use syntax::util::ThinVec;
4444
use syntax::util::parser::ExprPrecedence;
4545
use ty::AdtKind;
46+
use ty::maps::Providers;
4647

4748
use rustc_data_structures::indexed_vec;
4849

@@ -2204,3 +2205,8 @@ pub type TraitMap = NodeMap<Vec<TraitCandidate>>;
22042205
// Map from the NodeId of a glob import to a list of items which are actually
22052206
// imported.
22062207
pub type GlobMap = NodeMap<FxHashSet<Name>>;
2208+
2209+
2210+
pub fn provide(providers: &mut Providers) {
2211+
providers.describe_def = map::describe_def;
2212+
}

src/librustc_driver/driver.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -929,6 +929,7 @@ pub fn phase_2_configure_and_expand_inner<'a, F>(sess: &'a Session,
929929
}
930930

931931
pub fn default_provide(providers: &mut ty::maps::Providers) {
932+
hir::provide(providers);
932933
borrowck::provide(providers);
933934
mir::provide(providers);
934935
reachable::provide(providers);

0 commit comments

Comments
 (0)