2016-04-14 12:14:03 +12:00
|
|
|
use self::collector::NodeCollector;
|
2019-02-28 22:43:53 +00:00
|
|
|
pub use self::definitions::{
|
2019-12-22 17:42:04 -05:00
|
|
|
DefKey, DefPath, DefPathData, DefPathHash, Definitions, DisambiguatedDefPathData,
|
2019-02-28 22:43:53 +00:00
|
|
|
};
|
2014-11-06 00:05:53 -08:00
|
|
|
|
2020-02-07 11:45:04 +01:00
|
|
|
use crate::arena::Arena;
|
|
|
|
use crate::hir::{HirOwner, HirOwnerItems};
|
2019-02-05 11:20:45 -06:00
|
|
|
use crate::middle::cstore::CrateStoreDyn;
|
2019-09-06 03:57:44 +01:00
|
|
|
use crate::ty::query::Providers;
|
2020-02-07 13:13:35 +01:00
|
|
|
use crate::ty::TyCtxt;
|
2020-02-29 20:37:32 +03:00
|
|
|
use rustc_ast::ast::{self, Name, NodeId};
|
2019-12-24 05:02:53 +01:00
|
|
|
use rustc_data_structures::fx::FxHashMap;
|
2018-08-03 12:22:22 -06:00
|
|
|
use rustc_data_structures::svh::Svh;
|
2020-01-05 02:37:57 +01:00
|
|
|
use rustc_hir::def::{DefKind, Res};
|
2020-02-08 04:14:29 +01:00
|
|
|
use rustc_hir::def_id::{DefId, DefIndex, LocalDefId, LOCAL_CRATE};
|
2020-01-07 18:12:06 +01:00
|
|
|
use rustc_hir::intravisit;
|
2020-01-05 02:37:57 +01:00
|
|
|
use rustc_hir::itemlikevisit::ItemLikeVisitor;
|
|
|
|
use rustc_hir::print::Nested;
|
|
|
|
use rustc_hir::*;
|
2019-12-31 20:15:40 +03:00
|
|
|
use rustc_span::hygiene::MacroKind;
|
2020-01-01 19:25:28 +01:00
|
|
|
use rustc_span::source_map::Spanned;
|
2020-01-02 03:39:11 +01:00
|
|
|
use rustc_span::symbol::kw;
|
2020-01-07 14:38:27 +01:00
|
|
|
use rustc_span::Span;
|
2019-12-22 17:42:04 -05:00
|
|
|
use rustc_target::spec::abi::Abi;
|
2015-07-31 00:04:06 -07:00
|
|
|
|
2014-07-02 17:50:18 +02:00
|
|
|
pub mod blocks;
|
2015-09-10 15:53:08 -04:00
|
|
|
mod collector;
|
2015-09-17 14:29:59 -04:00
|
|
|
pub mod definitions;
|
2017-03-14 15:50:40 +01:00
|
|
|
mod hir_id_validator;
|
2014-07-02 17:50:18 +02:00
|
|
|
|
2019-06-16 17:44:19 +02:00
|
|
|
/// Represents an entry and its parent `HirId`.
|
2018-08-25 15:48:42 +01:00
|
|
|
#[derive(Copy, Clone, Debug)]
|
|
|
|
pub struct Entry<'hir> {
|
2019-03-09 08:23:35 +01:00
|
|
|
parent: HirId,
|
2018-08-25 15:56:16 +01:00
|
|
|
node: Node<'hir>,
|
2014-02-14 07:07:09 +02:00
|
|
|
}
|
|
|
|
|
2018-08-25 15:48:42 +01:00
|
|
|
impl<'hir> Entry<'hir> {
|
2019-03-09 08:23:35 +01:00
|
|
|
fn parent_node(self) -> Option<HirId> {
|
2018-08-25 15:48:42 +01:00
|
|
|
match self.node {
|
2020-02-07 16:43:36 +01:00
|
|
|
Node::Crate(_) | Node::MacroDef(_) => None,
|
2018-08-25 15:48:42 +01:00
|
|
|
_ => Some(self.parent),
|
|
|
|
}
|
2014-02-14 07:07:09 +02:00
|
|
|
}
|
2020-02-07 15:34:39 +01:00
|
|
|
}
|
2014-02-14 07:07:09 +02:00
|
|
|
|
2020-02-07 15:34:39 +01:00
|
|
|
fn fn_decl<'hir>(node: Node<'hir>) -> Option<&'hir FnDecl<'hir>> {
|
|
|
|
match node {
|
|
|
|
Node::Item(ref item) => match item.kind {
|
|
|
|
ItemKind::Fn(ref sig, _, _) => Some(&sig.decl),
|
|
|
|
_ => None,
|
|
|
|
},
|
2018-06-07 17:03:58 +02:00
|
|
|
|
2020-02-07 15:34:39 +01:00
|
|
|
Node::TraitItem(ref item) => match item.kind {
|
|
|
|
TraitItemKind::Fn(ref sig, _) => Some(&sig.decl),
|
|
|
|
_ => None,
|
|
|
|
},
|
2018-06-07 17:03:58 +02:00
|
|
|
|
2020-02-07 15:34:39 +01:00
|
|
|
Node::ImplItem(ref item) => match item.kind {
|
|
|
|
ImplItemKind::Method(ref sig, _) => Some(&sig.decl),
|
|
|
|
_ => None,
|
|
|
|
},
|
2018-06-07 17:03:58 +02:00
|
|
|
|
2020-02-07 15:34:39 +01:00
|
|
|
Node::Expr(ref expr) => match expr.kind {
|
|
|
|
ExprKind::Closure(_, ref fn_decl, ..) => Some(fn_decl),
|
2018-08-25 15:48:42 +01:00
|
|
|
_ => None,
|
2020-02-07 15:34:39 +01:00
|
|
|
},
|
2018-06-07 17:03:58 +02:00
|
|
|
|
2020-02-07 15:34:39 +01:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
2019-11-06 11:43:33 -08:00
|
|
|
|
2020-02-07 15:34:39 +01:00
|
|
|
fn fn_sig<'hir>(node: Node<'hir>) -> Option<&'hir FnSig<'hir>> {
|
|
|
|
match &node {
|
|
|
|
Node::Item(item) => match &item.kind {
|
|
|
|
ItemKind::Fn(sig, _, _) => Some(sig),
|
|
|
|
_ => None,
|
|
|
|
},
|
2019-11-06 11:43:33 -08:00
|
|
|
|
2020-02-07 15:34:39 +01:00
|
|
|
Node::TraitItem(item) => match &item.kind {
|
|
|
|
TraitItemKind::Fn(sig, _) => Some(sig),
|
|
|
|
_ => None,
|
|
|
|
},
|
2019-11-06 11:43:33 -08:00
|
|
|
|
2020-02-07 15:34:39 +01:00
|
|
|
Node::ImplItem(item) => match &item.kind {
|
|
|
|
ImplItemKind::Method(sig, _) => Some(sig),
|
2019-11-06 11:43:33 -08:00
|
|
|
_ => None,
|
2020-02-07 15:34:39 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
_ => None,
|
2019-11-06 11:43:33 -08:00
|
|
|
}
|
2020-02-07 15:34:39 +01:00
|
|
|
}
|
2019-11-06 11:43:33 -08:00
|
|
|
|
2020-02-07 16:10:50 +01:00
|
|
|
fn associated_body<'hir>(node: Node<'hir>) -> Option<BodyId> {
|
|
|
|
match node {
|
|
|
|
Node::Item(item) => match item.kind {
|
|
|
|
ItemKind::Const(_, body) | ItemKind::Static(.., body) | ItemKind::Fn(.., body) => {
|
|
|
|
Some(body)
|
|
|
|
}
|
|
|
|
_ => None,
|
|
|
|
},
|
2016-12-20 23:05:21 +02:00
|
|
|
|
2020-02-07 16:10:50 +01:00
|
|
|
Node::TraitItem(item) => match item.kind {
|
|
|
|
TraitItemKind::Const(_, Some(body))
|
|
|
|
| TraitItemKind::Fn(_, TraitMethod::Provided(body)) => Some(body),
|
|
|
|
_ => None,
|
|
|
|
},
|
2016-12-20 23:05:21 +02:00
|
|
|
|
2020-02-07 16:10:50 +01:00
|
|
|
Node::ImplItem(item) => match item.kind {
|
|
|
|
ImplItemKind::Const(_, body) | ImplItemKind::Method(_, body) => Some(body),
|
|
|
|
_ => None,
|
|
|
|
},
|
2018-05-17 21:28:50 +03:00
|
|
|
|
2020-02-07 16:10:50 +01:00
|
|
|
Node::AnonConst(constant) => Some(constant.body),
|
2016-12-20 23:05:21 +02:00
|
|
|
|
2020-02-07 16:10:50 +01:00
|
|
|
Node::Expr(expr) => match expr.kind {
|
|
|
|
ExprKind::Closure(.., body, _, _) => Some(body),
|
2019-12-22 17:42:04 -05:00
|
|
|
_ => None,
|
2020-02-07 16:10:50 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
_ => None,
|
2017-02-20 21:18:16 -05:00
|
|
|
}
|
2020-02-07 16:10:50 +01:00
|
|
|
}
|
2017-02-20 21:18:16 -05:00
|
|
|
|
2020-02-07 16:10:50 +01:00
|
|
|
fn is_body_owner<'hir>(node: Node<'hir>, hir_id: HirId) -> bool {
|
|
|
|
match associated_body(node) {
|
|
|
|
Some(b) => b.hir_id == hir_id,
|
|
|
|
None => false,
|
2016-12-20 23:05:21 +02:00
|
|
|
}
|
2013-08-31 17:13:57 +10:00
|
|
|
}
|
|
|
|
|
2019-02-08 14:53:55 +01:00
|
|
|
/// Represents a mapping from `NodeId`s to AST elements and their parent `NodeId`s.
|
2020-02-07 13:13:35 +01:00
|
|
|
pub struct EarlyMap<'hir> {
|
|
|
|
pub krate: &'hir Crate<'hir>,
|
|
|
|
|
|
|
|
/// The SVH of the local crate.
|
|
|
|
pub crate_hash: Svh,
|
|
|
|
|
|
|
|
pub(super) owner_map: FxHashMap<DefIndex, &'hir HirOwner<'hir>>,
|
|
|
|
pub(super) owner_items_map: FxHashMap<DefIndex, &'hir HirOwnerItems<'hir>>,
|
|
|
|
|
|
|
|
pub(crate) definitions: &'hir Definitions,
|
|
|
|
|
|
|
|
/// The reverse mapping of `node_to_hir_id`.
|
|
|
|
pub(super) hir_to_node_id: FxHashMap<HirId, NodeId>,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Represents a mapping from `NodeId`s to AST elements and their parent `NodeId`s.
|
2017-01-26 03:21:50 +02:00
|
|
|
pub struct Map<'hir> {
|
2020-02-07 13:13:35 +01:00
|
|
|
pub(super) tcx: TyCtxt<'hir>,
|
|
|
|
|
|
|
|
pub(super) krate: &'hir Crate<'hir>,
|
2014-05-18 02:38:13 +03:00
|
|
|
|
2017-12-19 18:01:19 +01:00
|
|
|
/// The SVH of the local crate.
|
|
|
|
pub crate_hash: Svh,
|
|
|
|
|
2020-02-07 11:45:04 +01:00
|
|
|
pub(super) owner_map: FxHashMap<DefIndex, &'hir HirOwner<'hir>>,
|
|
|
|
pub(super) owner_items_map: FxHashMap<DefIndex, &'hir HirOwnerItems<'hir>>,
|
|
|
|
|
2020-02-07 13:13:35 +01:00
|
|
|
pub(super) definitions: &'hir Definitions,
|
2016-07-26 10:58:35 -04:00
|
|
|
|
2017-08-29 19:27:30 +03:00
|
|
|
/// The reverse mapping of `node_to_hir_id`.
|
2020-02-07 13:13:35 +01:00
|
|
|
pub(super) hir_to_node_id: FxHashMap<HirId, NodeId>,
|
2014-01-17 23:23:09 +11:00
|
|
|
}
|
|
|
|
|
2020-02-25 05:30:11 +01:00
|
|
|
/// An iterator that walks up the ancestor tree of a given `HirId`.
|
|
|
|
/// Constructed using `tcx.hir().parent_iter(hir_id)`.
|
|
|
|
pub struct ParentHirIterator<'map, 'hir> {
|
2019-09-25 17:05:30 -07:00
|
|
|
current_id: HirId,
|
2019-11-29 14:08:03 +01:00
|
|
|
map: &'map Map<'hir>,
|
2019-09-25 17:05:30 -07:00
|
|
|
}
|
|
|
|
|
2020-01-07 15:06:39 +01:00
|
|
|
impl<'hir> Iterator for ParentHirIterator<'_, 'hir> {
|
2019-11-29 14:08:03 +01:00
|
|
|
type Item = (HirId, Node<'hir>);
|
2019-09-25 17:05:30 -07:00
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
if self.current_id == CRATE_HIR_ID {
|
|
|
|
return None;
|
|
|
|
}
|
2019-12-22 17:42:04 -05:00
|
|
|
loop {
|
|
|
|
// There are nodes that do not have entries, so we need to skip them.
|
2019-09-25 17:05:30 -07:00
|
|
|
let parent_id = self.map.get_parent_node(self.current_id);
|
|
|
|
|
|
|
|
if parent_id == self.current_id {
|
|
|
|
self.current_id = CRATE_HIR_ID;
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.current_id = parent_id;
|
|
|
|
if let Some(entry) = self.map.find_entry(parent_id) {
|
|
|
|
return Some((parent_id, entry.node));
|
|
|
|
}
|
|
|
|
// If this `HirId` doesn't have an `Entry`, skip it and look for its `parent_id`.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-26 03:21:50 +02:00
|
|
|
impl<'hir> Map<'hir> {
|
2020-02-06 13:41:37 +01:00
|
|
|
/// This is used internally in the dependency tracking system.
|
|
|
|
/// Use the `krate` method to ensure your dependency on the
|
|
|
|
/// crate is tracked.
|
|
|
|
pub fn untracked_krate(&self) -> &Crate<'hir> {
|
|
|
|
&self.krate
|
|
|
|
}
|
|
|
|
|
2017-08-04 09:49:40 +02:00
|
|
|
#[inline]
|
2019-11-26 22:48:41 +01:00
|
|
|
pub fn definitions(&self) -> &Definitions {
|
|
|
|
&self.definitions
|
2016-12-16 12:51:36 -05:00
|
|
|
}
|
|
|
|
|
2015-09-17 14:29:59 -04:00
|
|
|
pub fn def_key(&self, def_id: DefId) -> DefKey {
|
|
|
|
assert!(def_id.is_local());
|
2016-12-16 17:23:29 -05:00
|
|
|
self.definitions.def_key(def_id.index)
|
2015-09-17 14:29:59 -04:00
|
|
|
}
|
|
|
|
|
2019-04-27 22:39:17 +02:00
|
|
|
pub fn def_path_from_hir_id(&self, id: HirId) -> Option<DefPath> {
|
2019-12-22 17:42:04 -05:00
|
|
|
self.opt_local_def_id(id).map(|def_id| self.def_path(def_id))
|
2015-09-17 14:29:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn def_path(&self, def_id: DefId) -> DefPath {
|
|
|
|
assert!(def_id.is_local());
|
2016-12-16 17:23:29 -05:00
|
|
|
self.definitions.def_path(def_id.index)
|
2015-09-17 14:29:59 -04:00
|
|
|
}
|
|
|
|
|
2017-08-04 09:49:40 +02:00
|
|
|
#[inline]
|
2019-06-27 11:16:59 +02:00
|
|
|
pub fn local_def_id_from_node_id(&self, node: NodeId) -> DefId {
|
2019-06-27 11:49:08 +02:00
|
|
|
self.opt_local_def_id_from_node_id(node).unwrap_or_else(|| {
|
2019-03-09 08:23:35 +01:00
|
|
|
let hir_id = self.node_to_hir_id(node);
|
2019-12-22 17:42:04 -05:00
|
|
|
bug!(
|
|
|
|
"local_def_id_from_node_id: no entry for `{}`, which has a map of `{:?}`",
|
|
|
|
node,
|
|
|
|
self.find_entry(hir_id)
|
|
|
|
)
|
2015-09-10 15:53:08 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-02-03 09:14:31 +01:00
|
|
|
#[inline]
|
2019-06-27 11:28:14 +02:00
|
|
|
pub fn local_def_id(&self, hir_id: HirId) -> DefId {
|
|
|
|
self.opt_local_def_id(hir_id).unwrap_or_else(|| {
|
2019-12-22 17:42:04 -05:00
|
|
|
bug!(
|
|
|
|
"local_def_id: no entry for `{:?}`, which has a map of `{:?}`",
|
|
|
|
hir_id,
|
|
|
|
self.find_entry(hir_id)
|
|
|
|
)
|
2019-02-03 09:14:31 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2019-06-27 11:49:08 +02:00
|
|
|
pub fn opt_local_def_id(&self, hir_id: HirId) -> Option<DefId> {
|
2019-02-03 22:27:52 +01:00
|
|
|
let node_id = self.hir_to_node_id(hir_id);
|
|
|
|
self.definitions.opt_local_def_id(node_id)
|
2019-02-03 09:14:31 +01:00
|
|
|
}
|
|
|
|
|
2017-08-04 09:49:40 +02:00
|
|
|
#[inline]
|
2019-06-27 11:49:08 +02:00
|
|
|
pub fn opt_local_def_id_from_node_id(&self, node: NodeId) -> Option<DefId> {
|
2016-12-16 17:23:29 -05:00
|
|
|
self.definitions.opt_local_def_id(node)
|
2015-09-02 16:11:32 -04:00
|
|
|
}
|
|
|
|
|
2017-08-04 09:49:40 +02:00
|
|
|
#[inline]
|
2015-09-02 16:11:32 -04:00
|
|
|
pub fn as_local_node_id(&self, def_id: DefId) -> Option<NodeId> {
|
2016-12-16 17:23:29 -05:00
|
|
|
self.definitions.as_local_node_id(def_id)
|
2015-09-02 16:11:32 -04:00
|
|
|
}
|
|
|
|
|
2019-02-03 09:14:31 +01:00
|
|
|
#[inline]
|
|
|
|
pub fn as_local_hir_id(&self, def_id: DefId) -> Option<HirId> {
|
|
|
|
self.definitions.as_local_hir_id(def_id)
|
|
|
|
}
|
|
|
|
|
2017-08-29 19:27:30 +03:00
|
|
|
#[inline]
|
|
|
|
pub fn hir_to_node_id(&self, hir_id: HirId) -> NodeId {
|
|
|
|
self.hir_to_node_id[&hir_id]
|
|
|
|
}
|
|
|
|
|
2017-08-04 09:49:40 +02:00
|
|
|
#[inline]
|
|
|
|
pub fn node_to_hir_id(&self, node_id: NodeId) -> HirId {
|
|
|
|
self.definitions.node_to_hir_id(node_id)
|
|
|
|
}
|
|
|
|
|
2017-08-08 14:33:51 +02:00
|
|
|
#[inline]
|
|
|
|
pub fn def_index_to_hir_id(&self, def_index: DefIndex) -> HirId {
|
|
|
|
self.definitions.def_index_to_hir_id(def_index)
|
|
|
|
}
|
|
|
|
|
2017-11-16 14:04:01 +01:00
|
|
|
#[inline]
|
|
|
|
pub fn local_def_id_to_hir_id(&self, def_id: LocalDefId) -> HirId {
|
|
|
|
self.definitions.def_index_to_hir_id(def_id.to_def_id().index)
|
|
|
|
}
|
|
|
|
|
2019-08-08 14:59:24 -07:00
|
|
|
pub fn def_kind(&self, hir_id: HirId) -> Option<DefKind> {
|
2020-03-05 21:50:44 +01:00
|
|
|
let node = self.find(hir_id)?;
|
2018-02-19 16:46:40 +01:00
|
|
|
|
2019-04-20 19:46:19 +03:00
|
|
|
Some(match node {
|
2019-12-22 17:42:04 -05:00
|
|
|
Node::Item(item) => match item.kind {
|
|
|
|
ItemKind::Static(..) => DefKind::Static,
|
|
|
|
ItemKind::Const(..) => DefKind::Const,
|
|
|
|
ItemKind::Fn(..) => DefKind::Fn,
|
|
|
|
ItemKind::Mod(..) => DefKind::Mod,
|
|
|
|
ItemKind::OpaqueTy(..) => DefKind::OpaqueTy,
|
|
|
|
ItemKind::TyAlias(..) => DefKind::TyAlias,
|
|
|
|
ItemKind::Enum(..) => DefKind::Enum,
|
|
|
|
ItemKind::Struct(..) => DefKind::Struct,
|
|
|
|
ItemKind::Union(..) => DefKind::Union,
|
|
|
|
ItemKind::Trait(..) => DefKind::Trait,
|
|
|
|
ItemKind::TraitAlias(..) => DefKind::TraitAlias,
|
|
|
|
ItemKind::ExternCrate(_)
|
|
|
|
| ItemKind::Use(..)
|
|
|
|
| ItemKind::ForeignMod(..)
|
|
|
|
| ItemKind::GlobalAsm(..)
|
2020-01-17 16:14:29 -08:00
|
|
|
| ItemKind::Impl { .. } => return None,
|
2019-12-22 17:42:04 -05:00
|
|
|
},
|
|
|
|
Node::ForeignItem(item) => match item.kind {
|
|
|
|
ForeignItemKind::Fn(..) => DefKind::Fn,
|
|
|
|
ForeignItemKind::Static(..) => DefKind::Static,
|
|
|
|
ForeignItemKind::Type => DefKind::ForeignTy,
|
|
|
|
},
|
|
|
|
Node::TraitItem(item) => match item.kind {
|
|
|
|
TraitItemKind::Const(..) => DefKind::AssocConst,
|
2020-03-03 12:46:22 -06:00
|
|
|
TraitItemKind::Fn(..) => DefKind::AssocFn,
|
2019-12-22 17:42:04 -05:00
|
|
|
TraitItemKind::Type(..) => DefKind::AssocTy,
|
|
|
|
},
|
|
|
|
Node::ImplItem(item) => match item.kind {
|
|
|
|
ImplItemKind::Const(..) => DefKind::AssocConst,
|
2020-03-03 12:29:07 -06:00
|
|
|
ImplItemKind::Method(..) => DefKind::AssocFn,
|
2019-12-22 17:42:04 -05:00
|
|
|
ImplItemKind::TyAlias(..) => DefKind::AssocTy,
|
|
|
|
ImplItemKind::OpaqueTy(..) => DefKind::AssocOpaqueTy,
|
|
|
|
},
|
2019-04-20 18:26:26 +03:00
|
|
|
Node::Variant(_) => DefKind::Variant,
|
2019-03-24 18:21:59 +01:00
|
|
|
Node::Ctor(variant_data) => {
|
2019-04-20 18:26:26 +03:00
|
|
|
// FIXME(eddyb) is this even possible, if we have a `Node::Ctor`?
|
2020-03-01 22:04:42 +01:00
|
|
|
variant_data.ctor_hir_id()?;
|
|
|
|
|
2019-06-24 09:58:49 +02:00
|
|
|
let ctor_of = match self.find(self.get_parent_node(hir_id)) {
|
2019-03-24 18:54:56 +01:00
|
|
|
Some(Node::Item(..)) => def::CtorOf::Struct,
|
|
|
|
Some(Node::Variant(..)) => def::CtorOf::Variant,
|
2019-03-24 18:21:59 +01:00
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
2019-04-20 18:26:26 +03:00
|
|
|
DefKind::Ctor(ctor_of, def::CtorKind::from_hir(variant_data))
|
2019-02-03 14:09:56 +01:00
|
|
|
}
|
2019-12-22 17:42:04 -05:00
|
|
|
Node::AnonConst(_)
|
|
|
|
| Node::Field(_)
|
|
|
|
| Node::Expr(_)
|
|
|
|
| Node::Stmt(_)
|
|
|
|
| Node::PathSegment(_)
|
|
|
|
| Node::Ty(_)
|
|
|
|
| Node::TraitRef(_)
|
|
|
|
| Node::Pat(_)
|
|
|
|
| Node::Binding(_)
|
|
|
|
| Node::Local(_)
|
|
|
|
| Node::Param(_)
|
|
|
|
| Node::Arm(_)
|
|
|
|
| Node::Lifetime(_)
|
|
|
|
| Node::Visibility(_)
|
|
|
|
| Node::Block(_)
|
2020-02-07 16:43:36 +01:00
|
|
|
| Node::Crate(_) => return None,
|
2019-04-20 18:26:26 +03:00
|
|
|
Node::MacroDef(_) => DefKind::Macro(MacroKind::Bang),
|
2019-12-22 17:42:04 -05:00
|
|
|
Node::GenericParam(param) => match param.kind {
|
|
|
|
GenericParamKind::Lifetime { .. } => return None,
|
|
|
|
GenericParamKind::Type { .. } => DefKind::TyParam,
|
|
|
|
GenericParamKind::Const { .. } => DefKind::ConstParam,
|
|
|
|
},
|
2019-04-20 19:46:19 +03:00
|
|
|
})
|
2019-02-04 11:09:32 +01:00
|
|
|
}
|
|
|
|
|
2019-03-09 08:23:35 +01:00
|
|
|
fn find_entry(&self, id: HirId) -> Option<Entry<'hir>> {
|
2020-02-07 19:17:48 +01:00
|
|
|
Some(self.get_entry(id))
|
2014-05-18 02:38:13 +03:00
|
|
|
}
|
|
|
|
|
2020-02-07 16:43:36 +01:00
|
|
|
fn get_entry(&self, id: HirId) -> Entry<'hir> {
|
|
|
|
if id.local_id == ItemLocalId::from_u32_const(0) {
|
|
|
|
let owner = self.tcx.hir_owner(id.owner_def_id());
|
2020-02-08 05:18:34 +01:00
|
|
|
Entry { parent: owner.parent, node: owner.node }
|
2020-02-07 16:43:36 +01:00
|
|
|
} else {
|
|
|
|
let owner = self.tcx.hir_owner_items(id.owner_def_id());
|
|
|
|
let item = owner.items[id.local_id].as_ref().unwrap();
|
2020-02-08 05:18:34 +01:00
|
|
|
Entry { parent: HirId { owner: id.owner, local_id: item.parent }, node: item.node }
|
2020-02-07 16:43:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-07 15:51:38 +01:00
|
|
|
pub fn item(&self, id: HirId) -> &'hir Item<'hir> {
|
2020-02-07 15:17:05 +01:00
|
|
|
match self.find(id).unwrap() {
|
|
|
|
Node::Item(item) => item,
|
|
|
|
_ => bug!(),
|
|
|
|
}
|
2020-01-07 15:51:38 +01:00
|
|
|
}
|
|
|
|
|
2019-11-28 21:47:10 +01:00
|
|
|
pub fn trait_item(&self, id: TraitItemId) -> &'hir TraitItem<'hir> {
|
2020-02-07 15:17:05 +01:00
|
|
|
match self.find(id.hir_id).unwrap() {
|
|
|
|
Node::TraitItem(item) => item,
|
|
|
|
_ => bug!(),
|
|
|
|
}
|
2016-12-04 04:21:06 +02:00
|
|
|
}
|
|
|
|
|
2019-11-28 22:16:44 +01:00
|
|
|
pub fn impl_item(&self, id: ImplItemId) -> &'hir ImplItem<'hir> {
|
2020-02-07 15:17:05 +01:00
|
|
|
match self.find(id.hir_id).unwrap() {
|
|
|
|
Node::ImplItem(item) => item,
|
|
|
|
_ => bug!(),
|
|
|
|
}
|
2016-11-02 18:25:31 -04:00
|
|
|
}
|
|
|
|
|
2019-11-29 11:09:23 +01:00
|
|
|
pub fn body(&self, id: BodyId) -> &'hir Body<'hir> {
|
2020-02-07 13:13:35 +01:00
|
|
|
self.tcx
|
|
|
|
.hir_owner_items(DefId::local(id.hir_id.owner))
|
|
|
|
.bodies
|
|
|
|
.get(&id.hir_id.local_id)
|
|
|
|
.unwrap()
|
2016-12-21 12:32:59 +02:00
|
|
|
}
|
|
|
|
|
2019-11-30 17:46:46 +01:00
|
|
|
pub fn fn_decl_by_hir_id(&self, hir_id: HirId) -> Option<&'hir FnDecl<'hir>> {
|
2020-02-07 15:34:39 +01:00
|
|
|
if let Some(node) = self.find(hir_id) {
|
|
|
|
fn_decl(node)
|
2019-03-09 08:57:35 +01:00
|
|
|
} else {
|
2020-02-07 15:34:39 +01:00
|
|
|
bug!("no node for hir_id `{}`", hir_id)
|
2019-03-09 08:57:35 +01:00
|
|
|
}
|
2019-02-04 11:09:32 +01:00
|
|
|
}
|
|
|
|
|
2019-11-30 15:20:35 +01:00
|
|
|
pub fn fn_sig_by_hir_id(&self, hir_id: HirId) -> Option<&'hir FnSig<'hir>> {
|
2020-02-07 15:34:39 +01:00
|
|
|
if let Some(node) = self.find(hir_id) {
|
|
|
|
fn_sig(node)
|
2019-11-06 11:43:33 -08:00
|
|
|
} else {
|
2020-02-07 15:34:39 +01:00
|
|
|
bug!("no node for hir_id `{}`", hir_id)
|
2019-11-06 11:43:33 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-16 17:30:53 +02:00
|
|
|
/// Returns the `HirId` that corresponds to the definition of
|
2018-11-27 02:59:49 +00:00
|
|
|
/// which this is the body of, i.e., a `fn`, `const` or `static`
|
2018-05-17 21:28:50 +03:00
|
|
|
/// item (possibly associated), a closure, or a `hir::AnonConst`.
|
2019-06-14 12:28:47 +02:00
|
|
|
pub fn body_owner(&self, BodyId { hir_id }: BodyId) -> HirId {
|
2019-06-24 09:46:38 +02:00
|
|
|
let parent = self.get_parent_node(hir_id);
|
2020-02-07 16:10:50 +01:00
|
|
|
assert!(self.find(parent).map_or(false, |n| is_body_owner(n, hir_id)));
|
2019-06-14 12:28:47 +02:00
|
|
|
parent
|
2016-12-20 23:05:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn body_owner_def_id(&self, id: BodyId) -> DefId {
|
2019-06-27 11:28:14 +02:00
|
|
|
self.local_def_id(self.body_owner(id))
|
2016-12-20 23:05:21 +02:00
|
|
|
}
|
|
|
|
|
2019-06-14 12:28:47 +02:00
|
|
|
/// Given a `HirId`, returns the `BodyId` associated with it,
|
2017-04-24 22:03:47 +03:00
|
|
|
/// if the node is a body owner, otherwise returns `None`.
|
2019-06-14 18:58:55 +02:00
|
|
|
pub fn maybe_body_owned_by(&self, hir_id: HirId) -> Option<BodyId> {
|
2020-02-07 16:10:50 +01:00
|
|
|
if let Some(node) = self.find(hir_id) {
|
|
|
|
associated_body(node)
|
2017-04-04 12:06:35 -04:00
|
|
|
} else {
|
2019-03-09 08:57:35 +01:00
|
|
|
bug!("no entry for id `{}`", hir_id)
|
2017-04-04 12:06:35 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-24 22:03:47 +03:00
|
|
|
/// Given a body owner's id, returns the `BodyId` associated with it.
|
2019-03-04 09:00:30 +01:00
|
|
|
pub fn body_owned_by(&self, id: HirId) -> BodyId {
|
2019-06-14 18:58:55 +02:00
|
|
|
self.maybe_body_owned_by(id).unwrap_or_else(|| {
|
2019-12-22 17:42:04 -05:00
|
|
|
span_bug!(
|
|
|
|
self.span(id),
|
|
|
|
"body_owned_by: {} has no associated body",
|
|
|
|
self.node_to_string(id)
|
|
|
|
);
|
2017-04-24 22:03:47 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-06-14 18:58:55 +02:00
|
|
|
pub fn body_owner_kind(&self, id: HirId) -> BodyOwnerKind {
|
2019-06-20 10:39:19 +02:00
|
|
|
match self.get(id) {
|
2019-12-22 17:42:04 -05:00
|
|
|
Node::Item(&Item { kind: ItemKind::Const(..), .. })
|
|
|
|
| Node::TraitItem(&TraitItem { kind: TraitItemKind::Const(..), .. })
|
|
|
|
| Node::ImplItem(&ImplItem { kind: ImplItemKind::Const(..), .. })
|
|
|
|
| Node::AnonConst(_) => BodyOwnerKind::Const,
|
|
|
|
Node::Ctor(..)
|
|
|
|
| Node::Item(&Item { kind: ItemKind::Fn(..), .. })
|
2020-03-03 12:46:22 -06:00
|
|
|
| Node::TraitItem(&TraitItem { kind: TraitItemKind::Fn(..), .. })
|
2019-12-22 17:42:04 -05:00
|
|
|
| Node::ImplItem(&ImplItem { kind: ImplItemKind::Method(..), .. }) => BodyOwnerKind::Fn,
|
|
|
|
Node::Item(&Item { kind: ItemKind::Static(_, m, _), .. }) => BodyOwnerKind::Static(m),
|
|
|
|
Node::Expr(&Expr { kind: ExprKind::Closure(..), .. }) => BodyOwnerKind::Closure,
|
2018-12-07 18:25:55 +01:00
|
|
|
node => bug!("{:#?} is not a body node", node),
|
2017-11-10 19:20:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-04 09:00:30 +01:00
|
|
|
pub fn ty_param_owner(&self, id: HirId) -> HirId {
|
2019-06-20 10:39:19 +02:00
|
|
|
match self.get(id) {
|
2019-12-22 17:42:04 -05:00
|
|
|
Node::Item(&Item { kind: ItemKind::Trait(..), .. })
|
|
|
|
| Node::Item(&Item { kind: ItemKind::TraitAlias(..), .. }) => id,
|
2019-06-24 09:46:38 +02:00
|
|
|
Node::GenericParam(_) => self.get_parent_node(id),
|
2019-12-22 17:42:04 -05:00
|
|
|
_ => bug!("ty_param_owner: {} not a type parameter", self.node_to_string(id)),
|
2017-02-11 19:26:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-04 09:00:30 +01:00
|
|
|
pub fn ty_param_name(&self, id: HirId) -> Name {
|
2019-06-20 10:39:19 +02:00
|
|
|
match self.get(id) {
|
2019-12-22 17:42:04 -05:00
|
|
|
Node::Item(&Item { kind: ItemKind::Trait(..), .. })
|
|
|
|
| Node::Item(&Item { kind: ItemKind::TraitAlias(..), .. }) => kw::SelfUpper,
|
2018-08-25 15:56:16 +01:00
|
|
|
Node::GenericParam(param) => param.name.ident().name,
|
2019-06-16 17:30:02 +02:00
|
|
|
_ => bug!("ty_param_name: {} not a type parameter", self.node_to_string(id)),
|
2017-02-11 19:26:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-10 13:07:16 +01:00
|
|
|
pub fn trait_impls(&self, trait_did: DefId) -> &'hir [HirId] {
|
2020-02-08 04:14:29 +01:00
|
|
|
self.tcx.all_local_trait_impls(LOCAL_CRATE).get(&trait_did).map_or(&[], |xs| &xs[..])
|
2017-02-19 14:46:29 +02:00
|
|
|
}
|
|
|
|
|
2019-02-08 14:53:55 +01:00
|
|
|
/// Gets the attributes on the crate. This is preferable to
|
2016-03-16 05:53:45 -04:00
|
|
|
/// invoking `krate.attrs` because it registers a tighter
|
|
|
|
/// dep-graph access.
|
2017-01-26 03:21:50 +02:00
|
|
|
pub fn krate_attrs(&self) -> &'hir [ast::Attribute] {
|
2020-02-07 16:43:36 +01:00
|
|
|
match self.get_entry(CRATE_HIR_ID).node {
|
|
|
|
Node::Crate(item) => item.attrs,
|
|
|
|
_ => bug!(),
|
|
|
|
}
|
2016-03-16 05:53:45 -04:00
|
|
|
}
|
|
|
|
|
2019-11-29 10:24:47 +01:00
|
|
|
pub fn get_module(&self, module: DefId) -> (&'hir Mod<'hir>, Span, HirId) {
|
2019-03-09 08:23:35 +01:00
|
|
|
let hir_id = self.as_local_hir_id(module).unwrap();
|
2020-02-07 16:43:36 +01:00
|
|
|
match self.get_entry(hir_id).node {
|
2019-12-22 17:42:04 -05:00
|
|
|
Node::Item(&Item { span, kind: ItemKind::Mod(ref m), .. }) => (m, span, hir_id),
|
2020-02-07 16:43:36 +01:00
|
|
|
Node::Crate(item) => (&item.module, item.span, hir_id),
|
2019-08-21 16:11:01 -07:00
|
|
|
node => panic!("not a module: {:?}", node),
|
2018-06-08 19:14:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-11 04:58:46 +01:00
|
|
|
pub fn visit_item_likes_in_module<V>(&self, module: DefId, visitor: &mut V)
|
2019-12-22 17:42:04 -05:00
|
|
|
where
|
|
|
|
V: ItemLikeVisitor<'hir>,
|
2018-06-06 22:13:52 +02:00
|
|
|
{
|
2020-02-07 18:25:36 +01:00
|
|
|
let module = self.tcx.hir_module_items(module);
|
2018-06-06 22:13:52 +02:00
|
|
|
|
|
|
|
for id in &module.items {
|
2019-06-14 18:58:55 +02:00
|
|
|
visitor.visit_item(self.expect_item(*id));
|
2018-06-06 22:13:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for id in &module.trait_items {
|
2019-03-07 15:46:41 +01:00
|
|
|
visitor.visit_trait_item(self.expect_trait_item(id.hir_id));
|
2018-06-06 22:13:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for id in &module.impl_items {
|
2019-03-07 15:46:41 +01:00
|
|
|
visitor.visit_impl_item(self.expect_impl_item(id.hir_id));
|
2018-06-06 22:13:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-28 22:43:53 +00:00
|
|
|
/// Retrieves the `Node` corresponding to `id`, panicking if it cannot be found.
|
2019-06-20 10:39:19 +02:00
|
|
|
pub fn get(&self, id: HirId) -> Node<'hir> {
|
2019-12-22 17:42:04 -05:00
|
|
|
self.find(id).unwrap_or_else(|| bug!("couldn't find hir id {} in the HIR map", id))
|
2019-02-03 09:14:31 +01:00
|
|
|
}
|
|
|
|
|
2018-08-25 15:56:16 +01:00
|
|
|
pub fn get_if_local(&self, id: DefId) -> Option<Node<'hir>> {
|
2020-02-07 18:46:21 +01:00
|
|
|
self.as_local_hir_id(id).map(|id| self.get(id))
|
2015-09-04 13:52:28 -04:00
|
|
|
}
|
|
|
|
|
2019-11-30 17:46:46 +01:00
|
|
|
pub fn get_generics(&self, id: DefId) -> Option<&'hir Generics<'hir>> {
|
2019-12-22 17:42:04 -05:00
|
|
|
self.get_if_local(id).and_then(|node| match node {
|
|
|
|
Node::ImplItem(ref impl_item) => Some(&impl_item.generics),
|
|
|
|
Node::TraitItem(ref trait_item) => Some(&trait_item.generics),
|
|
|
|
Node::Item(ref item) => match item.kind {
|
|
|
|
ItemKind::Fn(_, ref generics, _)
|
|
|
|
| ItemKind::TyAlias(_, ref generics)
|
|
|
|
| ItemKind::Enum(_, ref generics)
|
|
|
|
| ItemKind::Struct(_, ref generics)
|
|
|
|
| ItemKind::Union(_, ref generics)
|
|
|
|
| ItemKind::Trait(_, _, ref generics, ..)
|
|
|
|
| ItemKind::TraitAlias(ref generics, _)
|
2020-01-17 16:14:29 -08:00
|
|
|
| ItemKind::Impl { ref generics, .. } => Some(generics),
|
2018-06-27 15:25:18 -07:00
|
|
|
_ => None,
|
2019-12-22 17:42:04 -05:00
|
|
|
},
|
|
|
|
_ => None,
|
2018-06-27 15:25:18 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-02-08 14:53:55 +01:00
|
|
|
/// Retrieves the `Node` corresponding to `id`, returning `None` if cannot be found.
|
2019-06-24 09:58:49 +02:00
|
|
|
pub fn find(&self, hir_id: HirId) -> Option<Node<'hir>> {
|
2020-02-07 19:01:27 +01:00
|
|
|
let node = self.get_entry(hir_id).node;
|
|
|
|
if let Node::Crate(..) = node { None } else { Some(node) }
|
2014-02-14 07:07:09 +02:00
|
|
|
}
|
|
|
|
|
2019-06-24 09:41:29 +02:00
|
|
|
/// Similar to `get_parent`; returns the parent HIR Id, or just `hir_id` if there
|
|
|
|
/// is no parent. Note that the parent may be `CRATE_HIR_ID`, which is not itself
|
2019-02-28 22:43:53 +00:00
|
|
|
/// present in the map, so passing the return value of `get_parent_node` to
|
|
|
|
/// `get` may in fact panic.
|
2019-06-24 09:41:29 +02:00
|
|
|
/// This function returns the immediate parent in the HIR, whereas `get_parent`
|
2015-07-01 08:58:48 +12:00
|
|
|
/// returns the enclosing item. Note that this might not be the actual parent
|
2019-06-24 09:41:29 +02:00
|
|
|
/// node in the HIR -- some kinds of nodes are not in the map and these will
|
2019-02-28 22:43:53 +00:00
|
|
|
/// never appear as the parent node. Thus, you can always walk the parent nodes
|
2019-06-24 09:41:29 +02:00
|
|
|
/// from a node to the root of the HIR (unless you get back the same ID here,
|
2019-02-28 22:43:53 +00:00
|
|
|
/// which can happen if the ID is not in the map itself or is just weird).
|
2019-06-24 09:46:38 +02:00
|
|
|
pub fn get_parent_node(&self, hir_id: HirId) -> HirId {
|
2020-02-07 18:46:21 +01:00
|
|
|
self.get_entry(hir_id).parent_node().unwrap_or(hir_id)
|
2019-02-03 09:14:31 +01:00
|
|
|
}
|
|
|
|
|
2020-02-25 05:30:11 +01:00
|
|
|
/// Returns an iterator for the nodes in the ancestor tree of the `current_id`
|
|
|
|
/// until the crate root is reached. Prefer this over your own loop using `get_parent_node`.
|
|
|
|
pub fn parent_iter(&self, current_id: HirId) -> ParentHirIterator<'_, 'hir> {
|
|
|
|
ParentHirIterator { current_id, map: self }
|
|
|
|
}
|
|
|
|
|
2019-09-06 03:57:44 +01:00
|
|
|
/// Checks if the node is an argument. An argument is a local variable whose
|
2015-10-27 00:46:11 +09:00
|
|
|
/// immediate parent is an item or a closure.
|
2019-06-24 09:41:29 +02:00
|
|
|
pub fn is_argument(&self, id: HirId) -> bool {
|
2019-06-24 09:58:49 +02:00
|
|
|
match self.find(id) {
|
2018-08-25 15:56:16 +01:00
|
|
|
Some(Node::Binding(_)) => (),
|
2015-10-27 00:46:11 +09:00
|
|
|
_ => return false,
|
|
|
|
}
|
2019-06-24 09:58:49 +02:00
|
|
|
match self.find(self.get_parent_node(id)) {
|
2019-12-22 17:42:04 -05:00
|
|
|
Some(Node::Item(_)) | Some(Node::TraitItem(_)) | Some(Node::ImplItem(_)) => true,
|
|
|
|
Some(Node::Expr(e)) => match e.kind {
|
|
|
|
ExprKind::Closure(..) => true,
|
|
|
|
_ => false,
|
|
|
|
},
|
2015-10-27 00:46:11 +09:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-13 11:24:08 -07:00
|
|
|
/// Whether the expression pointed at by `hir_id` belongs to a `const` evaluation context.
|
|
|
|
/// Used exclusively for diagnostics, to avoid suggestion function calls.
|
|
|
|
pub fn is_const_context(&self, hir_id: HirId) -> bool {
|
2019-08-12 20:22:58 -07:00
|
|
|
let parent_id = self.get_parent_item(hir_id);
|
|
|
|
match self.get(parent_id) {
|
2019-12-22 17:42:04 -05:00
|
|
|
Node::Item(&Item { kind: ItemKind::Const(..), .. })
|
|
|
|
| Node::TraitItem(&TraitItem { kind: TraitItemKind::Const(..), .. })
|
|
|
|
| Node::ImplItem(&ImplItem { kind: ImplItemKind::Const(..), .. })
|
2019-08-12 20:22:58 -07:00
|
|
|
| Node::AnonConst(_)
|
2019-12-22 17:42:04 -05:00
|
|
|
| Node::Item(&Item { kind: ItemKind::Static(..), .. }) => true,
|
|
|
|
Node::Item(&Item { kind: ItemKind::Fn(ref sig, ..), .. }) => {
|
|
|
|
sig.header.constness == Constness::Const
|
|
|
|
}
|
2019-04-29 17:14:31 -07:00
|
|
|
_ => false,
|
2019-08-12 20:22:58 -07:00
|
|
|
}
|
2019-04-29 17:14:31 -07:00
|
|
|
}
|
|
|
|
|
2020-03-06 12:13:55 +01:00
|
|
|
/// Whether `hir_id` corresponds to a `mod` or a crate.
|
2019-08-21 16:11:01 -07:00
|
|
|
pub fn is_hir_id_module(&self, hir_id: HirId) -> bool {
|
2020-02-07 19:09:19 +01:00
|
|
|
match self.get_entry(hir_id) {
|
|
|
|
Entry { node: Node::Item(Item { kind: ItemKind::Mod(_), .. }), .. }
|
|
|
|
| Entry { node: Node::Crate(..), .. } => true,
|
2019-08-21 16:11:01 -07:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-16 17:44:19 +02:00
|
|
|
/// Retrieves the `HirId` for `id`'s enclosing method, unless there's a
|
2017-08-01 19:13:47 -07:00
|
|
|
/// `while` or `loop` before reaching it, as block tail returns are not
|
2017-06-27 13:34:56 -07:00
|
|
|
/// available in them.
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// fn foo(x: usize) -> bool {
|
|
|
|
/// if x == 1 {
|
2019-09-06 03:57:44 +01:00
|
|
|
/// true // If `get_return_block` gets passed the `id` corresponding
|
2019-06-16 17:44:19 +02:00
|
|
|
/// } else { // to this, it will return `foo`'s `HirId`.
|
2017-06-27 13:34:56 -07:00
|
|
|
/// false
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// fn foo(x: usize) -> bool {
|
|
|
|
/// loop {
|
2019-09-06 03:57:44 +01:00
|
|
|
/// true // If `get_return_block` gets passed the `id` corresponding
|
2017-06-27 13:34:56 -07:00
|
|
|
/// } // to this, it will return `None`.
|
|
|
|
/// false
|
|
|
|
/// }
|
|
|
|
/// ```
|
2019-02-24 09:33:17 +01:00
|
|
|
pub fn get_return_block(&self, id: HirId) -> Option<HirId> {
|
2020-02-25 05:30:11 +01:00
|
|
|
let mut iter = self.parent_iter(id).peekable();
|
2019-09-25 23:01:01 -07:00
|
|
|
let mut ignore_tail = false;
|
|
|
|
if let Some(entry) = self.find_entry(id) {
|
2019-09-27 09:47:37 -07:00
|
|
|
if let Node::Expr(Expr { kind: ExprKind::Ret(_), .. }) = entry.node {
|
2019-09-25 23:01:01 -07:00
|
|
|
// When dealing with `return` statements, we don't care about climbing only tail
|
|
|
|
// expressions.
|
|
|
|
ignore_tail = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while let Some((hir_id, node)) = iter.next() {
|
|
|
|
if let (Some((_, next_node)), false) = (iter.peek(), ignore_tail) {
|
|
|
|
match next_node {
|
|
|
|
Node::Block(Block { expr: None, .. }) => return None,
|
|
|
|
Node::Block(Block { expr: Some(expr), .. }) => {
|
|
|
|
if hir_id != expr.hir_id {
|
|
|
|
// The current node is not the tail expression of its parent.
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
2019-09-25 17:05:30 -07:00
|
|
|
match node {
|
2019-12-22 17:42:04 -05:00
|
|
|
Node::Item(_)
|
|
|
|
| Node::ForeignItem(_)
|
|
|
|
| Node::TraitItem(_)
|
|
|
|
| Node::Expr(Expr { kind: ExprKind::Closure(..), .. })
|
|
|
|
| Node::ImplItem(_) => return Some(hir_id),
|
2018-08-25 15:56:16 +01:00
|
|
|
Node::Expr(ref expr) => {
|
2019-09-26 14:39:48 +01:00
|
|
|
match expr.kind {
|
2019-09-25 23:01:01 -07:00
|
|
|
// Ignore `return`s on the first iteration
|
2019-09-25 17:05:30 -07:00
|
|
|
ExprKind::Loop(..) | ExprKind::Ret(..) => return None,
|
|
|
|
_ => {}
|
2017-06-24 00:57:39 -07:00
|
|
|
}
|
|
|
|
}
|
2019-09-25 23:01:01 -07:00
|
|
|
Node::Local(_) => return None,
|
2019-09-25 17:05:30 -07:00
|
|
|
_ => {}
|
2017-06-24 00:57:39 -07:00
|
|
|
}
|
2019-09-25 17:05:30 -07:00
|
|
|
}
|
|
|
|
None
|
2017-06-24 00:57:39 -07:00
|
|
|
}
|
|
|
|
|
2019-06-14 12:28:47 +02:00
|
|
|
/// Retrieves the `HirId` for `id`'s parent item, or `id` itself if no
|
2015-07-01 08:58:48 +12:00
|
|
|
/// parent item is in this map. The "parent item" is the closest parent node
|
2018-06-13 09:11:23 +02:00
|
|
|
/// in the HIR which is recorded by the map and is an item, either an item
|
2015-07-01 08:58:48 +12:00
|
|
|
/// in a module, trait, or impl.
|
2019-03-09 08:57:35 +01:00
|
|
|
pub fn get_parent_item(&self, hir_id: HirId) -> HirId {
|
2020-02-25 05:30:11 +01:00
|
|
|
for (hir_id, node) in self.parent_iter(hir_id) {
|
2019-09-25 17:05:30 -07:00
|
|
|
match node {
|
2020-02-07 16:43:36 +01:00
|
|
|
Node::Crate(_)
|
2019-12-22 17:42:04 -05:00
|
|
|
| Node::Item(_)
|
|
|
|
| Node::ForeignItem(_)
|
|
|
|
| Node::TraitItem(_)
|
|
|
|
| Node::ImplItem(_) => return hir_id,
|
2019-09-25 17:05:30 -07:00
|
|
|
_ => {}
|
|
|
|
}
|
2015-07-01 08:58:48 +12:00
|
|
|
}
|
2019-09-25 17:05:30 -07:00
|
|
|
hir_id
|
2015-06-16 21:56:33 +12:00
|
|
|
}
|
|
|
|
|
2019-03-09 08:57:35 +01:00
|
|
|
/// Returns the `HirId` of `id`'s nearest module parent, or `id` itself if no
|
2018-09-19 09:28:49 -05:00
|
|
|
/// module parent is in this map.
|
2020-02-22 19:53:10 +01:00
|
|
|
pub(super) fn get_module_parent_node(&self, hir_id: HirId) -> HirId {
|
2020-02-25 05:30:11 +01:00
|
|
|
for (hir_id, node) in self.parent_iter(hir_id) {
|
2019-09-25 17:05:30 -07:00
|
|
|
if let Node::Item(&Item { kind: ItemKind::Mod(_), .. }) = node {
|
|
|
|
return hir_id;
|
|
|
|
}
|
2018-09-19 09:28:49 -05:00
|
|
|
}
|
2019-09-25 17:05:30 -07:00
|
|
|
CRATE_HIR_ID
|
2016-02-25 01:55:54 +00:00
|
|
|
}
|
|
|
|
|
2019-09-27 18:35:34 -07:00
|
|
|
/// When on a match arm tail expression or on a match arm, give back the enclosing `match`
|
|
|
|
/// expression.
|
|
|
|
///
|
|
|
|
/// Used by error reporting when there's a type error in a match arm caused by the `match`
|
|
|
|
/// expression needing to be unit.
|
2019-11-29 14:08:03 +01:00
|
|
|
pub fn get_match_if_cause(&self, hir_id: HirId) -> Option<&'hir Expr<'hir>> {
|
2020-02-25 05:30:11 +01:00
|
|
|
for (_, node) in self.parent_iter(hir_id) {
|
2019-09-26 17:16:34 -07:00
|
|
|
match node {
|
2019-12-22 17:42:04 -05:00
|
|
|
Node::Item(_) | Node::ForeignItem(_) | Node::TraitItem(_) | Node::ImplItem(_) => {
|
|
|
|
break;
|
|
|
|
}
|
2019-09-27 18:42:30 -07:00
|
|
|
Node::Expr(expr) => match expr.kind {
|
2019-09-26 17:16:34 -07:00
|
|
|
ExprKind::Match(_, _, _) => return Some(expr),
|
|
|
|
_ => {}
|
|
|
|
},
|
2019-09-27 18:42:30 -07:00
|
|
|
Node::Stmt(stmt) => match stmt.kind {
|
2019-09-26 17:16:34 -07:00
|
|
|
StmtKind::Local(_) => break,
|
|
|
|
_ => {}
|
2019-12-22 17:42:04 -05:00
|
|
|
},
|
2019-09-26 17:16:34 -07:00
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2019-02-28 22:43:53 +00:00
|
|
|
/// Returns the nearest enclosing scope. A scope is roughly an item or block.
|
2019-05-04 16:09:28 +01:00
|
|
|
pub fn get_enclosing_scope(&self, hir_id: HirId) -> Option<HirId> {
|
2020-02-25 05:30:11 +01:00
|
|
|
for (hir_id, node) in self.parent_iter(hir_id) {
|
2019-09-25 17:05:30 -07:00
|
|
|
if match node {
|
2019-12-22 17:42:04 -05:00
|
|
|
Node::Item(i) => match i.kind {
|
|
|
|
ItemKind::Fn(..)
|
|
|
|
| ItemKind::Mod(..)
|
|
|
|
| ItemKind::Enum(..)
|
|
|
|
| ItemKind::Struct(..)
|
|
|
|
| ItemKind::Union(..)
|
|
|
|
| ItemKind::Trait(..)
|
2020-01-13 20:30:20 -08:00
|
|
|
| ItemKind::Impl { .. } => true,
|
2019-12-22 17:42:04 -05:00
|
|
|
_ => false,
|
2019-09-25 17:05:30 -07:00
|
|
|
},
|
2019-12-22 17:42:04 -05:00
|
|
|
Node::ForeignItem(fi) => match fi.kind {
|
|
|
|
ForeignItemKind::Fn(..) => true,
|
|
|
|
_ => false,
|
2019-09-25 17:05:30 -07:00
|
|
|
},
|
2019-12-22 17:42:04 -05:00
|
|
|
Node::TraitItem(ti) => match ti.kind {
|
2020-03-03 12:46:22 -06:00
|
|
|
TraitItemKind::Fn(..) => true,
|
2019-12-22 17:42:04 -05:00
|
|
|
_ => false,
|
2019-09-25 17:05:30 -07:00
|
|
|
},
|
2019-12-22 17:42:04 -05:00
|
|
|
Node::ImplItem(ii) => match ii.kind {
|
|
|
|
ImplItemKind::Method(..) => true,
|
|
|
|
_ => false,
|
2019-09-25 17:05:30 -07:00
|
|
|
},
|
|
|
|
Node::Block(_) => true,
|
|
|
|
_ => false,
|
|
|
|
} {
|
|
|
|
return Some(hir_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
2015-06-16 22:20:30 +12:00
|
|
|
}
|
|
|
|
|
2019-08-01 00:41:54 +01:00
|
|
|
/// Returns the defining scope for an opaque type definition.
|
2019-09-26 14:38:01 -07:00
|
|
|
pub fn get_defining_scope(&self, id: HirId) -> HirId {
|
2019-02-28 22:43:53 +00:00
|
|
|
let mut scope = id;
|
|
|
|
loop {
|
2019-09-25 17:05:30 -07:00
|
|
|
scope = self.get_enclosing_scope(scope).unwrap_or(CRATE_HIR_ID);
|
2019-05-04 16:09:28 +01:00
|
|
|
if scope == CRATE_HIR_ID {
|
2019-09-26 14:38:01 -07:00
|
|
|
return CRATE_HIR_ID;
|
2019-02-28 22:43:53 +00:00
|
|
|
}
|
2019-06-20 10:39:19 +02:00
|
|
|
match self.get(scope) {
|
2019-12-22 17:42:04 -05:00
|
|
|
Node::Item(i) => match i.kind {
|
|
|
|
ItemKind::OpaqueTy(OpaqueTy { impl_trait_fn: None, .. }) => {}
|
|
|
|
_ => break,
|
|
|
|
},
|
2019-02-28 22:43:53 +00:00
|
|
|
Node::Block(_) => {}
|
|
|
|
_ => break,
|
|
|
|
}
|
|
|
|
}
|
2019-09-26 14:38:01 -07:00
|
|
|
scope
|
2019-02-28 22:43:53 +00:00
|
|
|
}
|
|
|
|
|
2019-06-14 18:58:55 +02:00
|
|
|
pub fn get_parent_did(&self, id: HirId) -> DefId {
|
2019-06-27 11:28:14 +02:00
|
|
|
self.local_def_id(self.get_parent_item(id))
|
2019-02-04 11:09:32 +01:00
|
|
|
}
|
|
|
|
|
2019-06-14 18:58:55 +02:00
|
|
|
pub fn get_foreign_abi(&self, hir_id: HirId) -> Abi {
|
2019-03-09 08:23:35 +01:00
|
|
|
let parent = self.get_parent_item(hir_id);
|
2018-08-25 15:48:42 +01:00
|
|
|
if let Some(entry) = self.find_entry(parent) {
|
2018-09-12 12:31:11 +02:00
|
|
|
if let Entry {
|
2019-12-22 17:42:04 -05:00
|
|
|
node: Node::Item(Item { kind: ItemKind::ForeignMod(ref nm), .. }), ..
|
|
|
|
} = entry
|
2018-09-12 12:31:11 +02:00
|
|
|
{
|
|
|
|
return nm.abi;
|
2014-05-18 02:38:13 +03:00
|
|
|
}
|
2014-02-14 07:07:09 +02:00
|
|
|
}
|
2019-06-16 17:30:02 +02:00
|
|
|
bug!("expected foreign mod or inlined parent, found {}", self.node_to_string(parent))
|
2019-02-04 11:09:32 +01:00
|
|
|
}
|
|
|
|
|
2019-11-28 19:28:50 +01:00
|
|
|
pub fn expect_item(&self, id: HirId) -> &'hir Item<'hir> {
|
2019-12-22 17:42:04 -05:00
|
|
|
match self.find(id) {
|
2019-03-07 15:46:41 +01:00
|
|
|
Some(Node::Item(item)) => item,
|
2019-12-22 17:42:04 -05:00
|
|
|
_ => bug!("expected item, found {}", self.node_to_string(id)),
|
2019-03-07 15:46:41 +01:00
|
|
|
}
|
2019-02-03 09:14:31 +01:00
|
|
|
}
|
|
|
|
|
2019-11-28 22:16:44 +01:00
|
|
|
pub fn expect_impl_item(&self, id: HirId) -> &'hir ImplItem<'hir> {
|
2019-06-24 09:58:49 +02:00
|
|
|
match self.find(id) {
|
2018-08-25 15:56:16 +01:00
|
|
|
Some(Node::ImplItem(item)) => item,
|
2019-12-22 17:42:04 -05:00
|
|
|
_ => bug!("expected impl item, found {}", self.node_to_string(id)),
|
2016-08-10 01:43:12 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-28 21:47:10 +01:00
|
|
|
pub fn expect_trait_item(&self, id: HirId) -> &'hir TraitItem<'hir> {
|
2019-06-24 09:58:49 +02:00
|
|
|
match self.find(id) {
|
2018-08-25 15:56:16 +01:00
|
|
|
Some(Node::TraitItem(item)) => item,
|
2019-12-22 17:42:04 -05:00
|
|
|
_ => bug!("expected trait item, found {}", self.node_to_string(id)),
|
2015-07-14 22:03:14 +12:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-29 09:26:18 +01:00
|
|
|
pub fn expect_variant_data(&self, id: HirId) -> &'hir VariantData<'hir> {
|
2019-06-24 09:58:49 +02:00
|
|
|
match self.find(id) {
|
2019-12-22 17:42:04 -05:00
|
|
|
Some(Node::Item(i)) => match i.kind {
|
|
|
|
ItemKind::Struct(ref struct_def, _) | ItemKind::Union(ref struct_def, _) => {
|
|
|
|
struct_def
|
2014-04-03 13:38:45 +13:00
|
|
|
}
|
2019-12-22 17:42:04 -05:00
|
|
|
_ => bug!("struct ID bound to non-struct {}", self.node_to_string(id)),
|
|
|
|
},
|
2019-08-13 21:40:21 -03:00
|
|
|
Some(Node::Variant(variant)) => &variant.data,
|
2019-03-24 18:21:59 +01:00
|
|
|
Some(Node::Ctor(data)) => data,
|
2019-12-22 17:42:04 -05:00
|
|
|
_ => bug!("expected struct or variant, found {}", self.node_to_string(id)),
|
2014-04-03 13:38:45 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-29 09:26:18 +01:00
|
|
|
pub fn expect_variant(&self, id: HirId) -> &'hir Variant<'hir> {
|
2019-06-24 09:58:49 +02:00
|
|
|
match self.find(id) {
|
2018-08-25 15:56:16 +01:00
|
|
|
Some(Node::Variant(variant)) => variant,
|
2019-06-16 17:30:02 +02:00
|
|
|
_ => bug!("expected variant, found {}", self.node_to_string(id)),
|
2014-04-08 21:00:20 +12:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-28 20:18:29 +01:00
|
|
|
pub fn expect_foreign_item(&self, id: HirId) -> &'hir ForeignItem<'hir> {
|
2019-06-24 09:58:49 +02:00
|
|
|
match self.find(id) {
|
2018-08-25 15:56:16 +01:00
|
|
|
Some(Node::ForeignItem(item)) => item,
|
2019-12-22 17:42:04 -05:00
|
|
|
_ => bug!("expected foreign item, found {}", self.node_to_string(id)),
|
2014-02-14 07:07:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-29 13:43:03 +01:00
|
|
|
pub fn expect_expr(&self, id: HirId) -> &'hir Expr<'hir> {
|
2019-12-22 17:42:04 -05:00
|
|
|
match self.find(id) {
|
2019-03-09 08:57:35 +01:00
|
|
|
Some(Node::Expr(expr)) => expr,
|
2019-12-22 17:42:04 -05:00
|
|
|
_ => bug!("expected expr, found {}", self.node_to_string(id)),
|
2019-03-09 08:57:35 +01:00
|
|
|
}
|
2019-02-04 11:09:32 +01:00
|
|
|
}
|
|
|
|
|
2019-12-13 15:27:55 -08:00
|
|
|
pub fn opt_name(&self, id: HirId) -> Option<Name> {
|
|
|
|
Some(match self.get(id) {
|
2018-12-01 02:47:08 +00:00
|
|
|
Node::Item(i) => i.ident.name,
|
|
|
|
Node::ForeignItem(fi) => fi.ident.name,
|
2018-08-25 15:56:16 +01:00
|
|
|
Node::ImplItem(ii) => ii.ident.name,
|
|
|
|
Node::TraitItem(ti) => ti.ident.name,
|
2019-08-13 21:40:21 -03:00
|
|
|
Node::Variant(v) => v.ident.name,
|
2018-08-25 15:56:16 +01:00
|
|
|
Node::Field(f) => f.ident.name,
|
|
|
|
Node::Lifetime(lt) => lt.name.ident().name,
|
|
|
|
Node::GenericParam(param) => param.name.ident().name,
|
2019-09-26 16:18:31 +01:00
|
|
|
Node::Binding(&Pat { kind: PatKind::Binding(_, _, l, _), .. }) => l.name,
|
2019-06-19 15:44:51 +02:00
|
|
|
Node::Ctor(..) => self.name(self.get_parent_item(id)),
|
2019-12-13 15:27:55 -08:00
|
|
|
_ => return None,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn name(&self, id: HirId) -> Name {
|
|
|
|
match self.opt_name(id) {
|
|
|
|
Some(name) => name,
|
|
|
|
None => bug!("no name for {}", self.node_to_string(id)),
|
2014-02-14 07:07:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-28 22:43:53 +00:00
|
|
|
/// Given a node ID, gets a list of attributes associated with the AST
|
|
|
|
/// corresponding to the node-ID.
|
2019-06-14 18:58:55 +02:00
|
|
|
pub fn attrs(&self, id: HirId) -> &'hir [ast::Attribute] {
|
2019-03-03 18:47:54 +01:00
|
|
|
let attrs = match self.find_entry(id).map(|entry| entry.node) {
|
2019-08-27 13:24:32 +02:00
|
|
|
Some(Node::Param(a)) => Some(&a.attrs[..]),
|
2019-01-31 23:11:29 +01:00
|
|
|
Some(Node::Local(l)) => Some(&l.attrs[..]),
|
2018-08-25 15:56:16 +01:00
|
|
|
Some(Node::Item(i)) => Some(&i.attrs[..]),
|
|
|
|
Some(Node::ForeignItem(fi)) => Some(&fi.attrs[..]),
|
|
|
|
Some(Node::TraitItem(ref ti)) => Some(&ti.attrs[..]),
|
|
|
|
Some(Node::ImplItem(ref ii)) => Some(&ii.attrs[..]),
|
2019-08-13 21:40:21 -03:00
|
|
|
Some(Node::Variant(ref v)) => Some(&v.attrs[..]),
|
2018-08-25 15:56:16 +01:00
|
|
|
Some(Node::Field(ref f)) => Some(&f.attrs[..]),
|
|
|
|
Some(Node::Expr(ref e)) => Some(&*e.attrs),
|
2019-09-26 17:34:50 +01:00
|
|
|
Some(Node::Stmt(ref s)) => Some(s.kind.attrs()),
|
2019-03-30 22:54:29 +00:00
|
|
|
Some(Node::Arm(ref a)) => Some(&*a.attrs),
|
2018-08-25 15:56:16 +01:00
|
|
|
Some(Node::GenericParam(param)) => Some(¶m.attrs[..]),
|
2019-03-21 23:38:50 +01:00
|
|
|
// Unit/tuple structs/variants take the attributes straight from
|
|
|
|
// the struct/variant definition.
|
2019-06-14 18:58:55 +02:00
|
|
|
Some(Node::Ctor(..)) => return self.attrs(self.get_parent_item(id)),
|
2020-02-07 16:43:36 +01:00
|
|
|
Some(Node::Crate(item)) => Some(&item.attrs[..]),
|
2019-12-22 17:42:04 -05:00
|
|
|
_ => None,
|
2014-02-14 07:07:09 +02:00
|
|
|
};
|
2015-03-06 18:06:05 +01:00
|
|
|
attrs.unwrap_or(&[])
|
2014-02-14 07:07:09 +02:00
|
|
|
}
|
|
|
|
|
2019-06-14 18:58:55 +02:00
|
|
|
pub fn span(&self, hir_id: HirId) -> Span {
|
2019-03-09 08:23:35 +01:00
|
|
|
match self.find_entry(hir_id).map(|entry| entry.node) {
|
2019-08-27 13:24:32 +02:00
|
|
|
Some(Node::Param(param)) => param.span,
|
2018-08-25 15:56:16 +01:00
|
|
|
Some(Node::Item(item)) => item.span,
|
|
|
|
Some(Node::ForeignItem(foreign_item)) => foreign_item.span,
|
|
|
|
Some(Node::TraitItem(trait_method)) => trait_method.span,
|
|
|
|
Some(Node::ImplItem(impl_item)) => impl_item.span,
|
|
|
|
Some(Node::Variant(variant)) => variant.span,
|
|
|
|
Some(Node::Field(field)) => field.span,
|
|
|
|
Some(Node::AnonConst(constant)) => self.body(constant.body).value.span,
|
|
|
|
Some(Node::Expr(expr)) => expr.span,
|
|
|
|
Some(Node::Stmt(stmt)) => stmt.span,
|
2018-09-11 15:08:47 +12:00
|
|
|
Some(Node::PathSegment(seg)) => seg.ident.span,
|
2018-08-25 15:56:16 +01:00
|
|
|
Some(Node::Ty(ty)) => ty.span,
|
|
|
|
Some(Node::TraitRef(tr)) => tr.path.span,
|
|
|
|
Some(Node::Binding(pat)) => pat.span,
|
|
|
|
Some(Node::Pat(pat)) => pat.span,
|
2019-03-30 22:54:29 +00:00
|
|
|
Some(Node::Arm(arm)) => arm.span,
|
2018-08-25 15:56:16 +01:00
|
|
|
Some(Node::Block(block)) => block.span,
|
2019-12-22 17:42:04 -05:00
|
|
|
Some(Node::Ctor(..)) => match self.find(self.get_parent_node(hir_id)) {
|
2019-03-24 18:21:59 +01:00
|
|
|
Some(Node::Item(item)) => item.span,
|
|
|
|
Some(Node::Variant(variant)) => variant.span,
|
|
|
|
_ => unreachable!(),
|
2019-12-22 17:42:04 -05:00
|
|
|
},
|
2018-08-25 15:56:16 +01:00
|
|
|
Some(Node::Lifetime(lifetime)) => lifetime.span,
|
|
|
|
Some(Node::GenericParam(param)) => param.span,
|
|
|
|
Some(Node::Visibility(&Spanned {
|
2019-12-22 17:42:04 -05:00
|
|
|
node: VisibilityKind::Restricted { ref path, .. },
|
|
|
|
..
|
2018-06-30 20:34:18 -07:00
|
|
|
})) => path.span,
|
2018-08-25 15:56:16 +01:00
|
|
|
Some(Node::Visibility(v)) => bug!("unexpected Visibility {:?}", v),
|
|
|
|
Some(Node::Local(local)) => local.span,
|
|
|
|
Some(Node::MacroDef(macro_def)) => macro_def.span,
|
2020-02-07 16:43:36 +01:00
|
|
|
Some(Node::Crate(item)) => item.span,
|
2019-03-09 08:57:35 +01:00
|
|
|
None => bug!("hir::map::Map::span: id not in map: {:?}", hir_id),
|
2016-11-24 01:39:13 +02:00
|
|
|
}
|
2014-02-14 07:07:09 +02:00
|
|
|
}
|
|
|
|
|
2015-09-04 13:52:28 -04:00
|
|
|
pub fn span_if_local(&self, id: DefId) -> Option<Span> {
|
2019-06-14 18:58:55 +02:00
|
|
|
self.as_local_hir_id(id).map(|id| self.span(id))
|
2015-09-04 13:52:28 -04:00
|
|
|
}
|
|
|
|
|
2019-09-28 02:30:48 +02:00
|
|
|
pub fn res_span(&self, res: Res) -> Option<Span> {
|
|
|
|
match res {
|
|
|
|
Res::Err => None,
|
|
|
|
Res::Local(id) => Some(self.span(id)),
|
|
|
|
res => self.span_if_local(res.opt_def_id()?),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-16 17:30:02 +02:00
|
|
|
pub fn node_to_string(&self, id: HirId) -> String {
|
2019-02-03 09:14:31 +01:00
|
|
|
hir_id_to_string(self, id, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn hir_to_user_string(&self, id: HirId) -> String {
|
|
|
|
hir_id_to_string(self, id, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn hir_to_pretty_string(&self, id: HirId) -> String {
|
2019-06-20 10:39:19 +02:00
|
|
|
print::to_string(self, |s| s.print_node(self.get(id)))
|
2019-02-03 09:14:31 +01:00
|
|
|
}
|
2014-01-17 23:23:09 +11:00
|
|
|
}
|
2013-02-04 14:02:01 -08:00
|
|
|
|
2020-01-07 17:25:33 +01:00
|
|
|
impl<'hir> intravisit::Map<'hir> for Map<'hir> {
|
|
|
|
fn body(&self, id: BodyId) -> &'hir Body<'hir> {
|
|
|
|
self.body(id)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn item(&self, id: HirId) -> &'hir Item<'hir> {
|
|
|
|
self.item(id)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn trait_item(&self, id: TraitItemId) -> &'hir TraitItem<'hir> {
|
|
|
|
self.trait_item(id)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn impl_item(&self, id: ImplItemId) -> &'hir ImplItem<'hir> {
|
|
|
|
self.impl_item(id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-07 11:22:42 +02:00
|
|
|
trait Named {
|
|
|
|
fn name(&self) -> Name;
|
|
|
|
}
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
impl<T: Named> Named for Spanned<T> {
|
|
|
|
fn name(&self) -> Name {
|
|
|
|
self.node.name()
|
|
|
|
}
|
|
|
|
}
|
2014-08-07 11:22:42 +02:00
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
impl Named for Item<'_> {
|
|
|
|
fn name(&self) -> Name {
|
|
|
|
self.ident.name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl Named for ForeignItem<'_> {
|
|
|
|
fn name(&self) -> Name {
|
|
|
|
self.ident.name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl Named for Variant<'_> {
|
|
|
|
fn name(&self) -> Name {
|
|
|
|
self.ident.name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl Named for StructField<'_> {
|
|
|
|
fn name(&self) -> Name {
|
|
|
|
self.ident.name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl Named for TraitItem<'_> {
|
|
|
|
fn name(&self) -> Name {
|
|
|
|
self.ident.name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl Named for ImplItem<'_> {
|
|
|
|
fn name(&self) -> Name {
|
|
|
|
self.ident.name
|
|
|
|
}
|
|
|
|
}
|
2014-08-07 11:22:42 +02:00
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
pub fn map_crate<'hir>(
|
2019-12-24 23:43:44 +01:00
|
|
|
sess: &rustc_session::Session,
|
2020-02-07 11:45:04 +01:00
|
|
|
arena: &'hir Arena<'hir>,
|
2019-12-22 17:42:04 -05:00
|
|
|
cstore: &CrateStoreDyn,
|
2020-02-06 13:41:37 +01:00
|
|
|
krate: &'hir Crate<'hir>,
|
2019-12-22 17:42:04 -05:00
|
|
|
definitions: Definitions,
|
2020-02-07 13:13:35 +01:00
|
|
|
) -> EarlyMap<'hir> {
|
2019-10-08 14:05:41 +02:00
|
|
|
let _prof_timer = sess.prof.generic_activity("build_hir_map");
|
|
|
|
|
2019-02-15 10:30:44 +01:00
|
|
|
// Build the reverse mapping of `node_to_hir_id`.
|
2019-12-22 17:42:04 -05:00
|
|
|
let hir_to_node_id = definitions
|
|
|
|
.node_to_hir_id
|
|
|
|
.iter_enumerated()
|
|
|
|
.map(|(node_id, &hir_id)| (hir_id, node_id))
|
|
|
|
.collect();
|
2019-02-15 10:30:44 +01:00
|
|
|
|
2020-02-08 05:18:34 +01:00
|
|
|
let (owner_map, owner_items_map, crate_hash) = {
|
2020-02-06 13:41:37 +01:00
|
|
|
let hcx = crate::ich::StableHashingContext::new(sess, krate, &definitions, cstore);
|
|
|
|
|
|
|
|
let mut collector =
|
2020-02-08 05:18:34 +01:00
|
|
|
NodeCollector::root(sess, arena, krate, &definitions, &hir_to_node_id, hcx);
|
2020-02-06 13:41:37 +01:00
|
|
|
intravisit::walk_crate(&mut collector, krate);
|
2017-09-14 17:43:03 +02:00
|
|
|
|
2017-10-23 18:44:58 +02:00
|
|
|
let crate_disambiguator = sess.local_crate_disambiguator();
|
2017-12-01 15:56:37 +01:00
|
|
|
let cmdline_args = sess.opts.dep_tracking_hash();
|
2019-12-22 17:42:04 -05:00
|
|
|
collector.finalize_and_compute_crate_hash(crate_disambiguator, cstore, cmdline_args)
|
2019-02-15 10:30:44 +01:00
|
|
|
};
|
2014-01-17 23:23:09 +11:00
|
|
|
|
2020-02-07 13:13:35 +01:00
|
|
|
let map = EarlyMap {
|
2020-02-07 11:45:04 +01:00
|
|
|
krate,
|
|
|
|
crate_hash,
|
|
|
|
owner_map,
|
|
|
|
owner_items_map: owner_items_map.into_iter().map(|(k, v)| (k, &*v)).collect(),
|
|
|
|
hir_to_node_id,
|
2020-02-07 13:13:35 +01:00
|
|
|
definitions: arena.alloc(definitions),
|
2020-02-07 11:45:04 +01:00
|
|
|
};
|
2017-03-14 15:50:40 +01:00
|
|
|
|
2020-01-07 21:34:08 +01:00
|
|
|
sess.time("validate_HIR_map", || {
|
2020-02-14 17:49:16 -08:00
|
|
|
hir_id_validator::check_crate(&map, sess);
|
2018-06-06 22:13:52 +02:00
|
|
|
});
|
2017-03-14 15:50:40 +01:00
|
|
|
|
|
|
|
map
|
2011-06-19 22:41:21 +02:00
|
|
|
}
|
|
|
|
|
2016-12-27 10:00:18 +02:00
|
|
|
/// Identical to the `PpAnn` implementation for `hir::Crate`,
|
|
|
|
/// except it avoids creating a dependency on the whole crate.
|
2017-01-26 03:21:50 +02:00
|
|
|
impl<'hir> print::PpAnn for Map<'hir> {
|
2019-06-24 14:15:11 -04:00
|
|
|
fn nested(&self, state: &mut print::State<'_>, nested: print::Nested) {
|
2016-12-27 10:00:18 +02:00
|
|
|
match nested {
|
2019-06-14 18:58:55 +02:00
|
|
|
Nested::Item(id) => state.print_item(self.expect_item(id.id)),
|
2016-12-27 10:00:18 +02:00
|
|
|
Nested::TraitItem(id) => state.print_trait_item(self.trait_item(id)),
|
|
|
|
Nested::ImplItem(id) => state.print_impl_item(self.impl_item(id)),
|
|
|
|
Nested::Body(id) => state.print_expr(&self.body(id).value),
|
2019-12-22 17:42:04 -05:00
|
|
|
Nested::BodyParamPat(id, i) => state.print_pat(&self.body(id).params[i].pat),
|
2016-12-27 10:00:18 +02:00
|
|
|
}
|
|
|
|
}
|
2014-08-07 14:21:15 +02:00
|
|
|
}
|
|
|
|
|
2019-04-27 22:39:17 +02:00
|
|
|
fn hir_id_to_string(map: &Map<'_>, id: HirId, include_id: bool) -> String {
|
|
|
|
let id_str = format!(" (hir_id={})", id);
|
2015-02-18 14:48:57 -05:00
|
|
|
let id_str = if include_id { &id_str[..] } else { "" };
|
2014-09-19 15:38:53 +02:00
|
|
|
|
2016-04-06 13:51:55 +03:00
|
|
|
let path_str = || {
|
2019-09-06 03:57:44 +01:00
|
|
|
// This functionality is used for debugging, try to use `TyCtxt` to get
|
|
|
|
// the user-friendly path, otherwise fall back to stringifying `DefPath`.
|
2019-02-05 11:20:45 -06:00
|
|
|
crate::ty::tls::with_opt(|tcx| {
|
2016-04-06 13:51:55 +03:00
|
|
|
if let Some(tcx) = tcx {
|
2019-06-27 11:28:14 +02:00
|
|
|
let def_id = map.local_def_id(id);
|
2018-12-28 10:08:30 +02:00
|
|
|
tcx.def_path_str(def_id)
|
2019-04-27 22:39:17 +02:00
|
|
|
} else if let Some(path) = map.def_path_from_hir_id(id) {
|
2019-12-22 17:42:04 -05:00
|
|
|
path.data
|
|
|
|
.into_iter()
|
|
|
|
.map(|elem| elem.data.to_string())
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
.join("::")
|
2016-04-06 13:51:55 +03:00
|
|
|
} else {
|
|
|
|
String::from("<missing path>")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
2019-06-24 09:58:49 +02:00
|
|
|
match map.find(id) {
|
2018-08-25 15:56:16 +01:00
|
|
|
Some(Node::Item(item)) => {
|
2019-09-26 17:51:36 +01:00
|
|
|
let item_str = match item.kind {
|
2018-07-11 23:36:06 +08:00
|
|
|
ItemKind::ExternCrate(..) => "extern crate",
|
|
|
|
ItemKind::Use(..) => "use",
|
|
|
|
ItemKind::Static(..) => "static",
|
|
|
|
ItemKind::Const(..) => "const",
|
|
|
|
ItemKind::Fn(..) => "fn",
|
|
|
|
ItemKind::Mod(..) => "mod",
|
|
|
|
ItemKind::ForeignMod(..) => "foreign mod",
|
|
|
|
ItemKind::GlobalAsm(..) => "global asm",
|
2019-08-02 11:02:08 +01:00
|
|
|
ItemKind::TyAlias(..) => "ty",
|
2019-08-01 00:41:54 +01:00
|
|
|
ItemKind::OpaqueTy(..) => "opaque type",
|
2018-07-11 23:36:06 +08:00
|
|
|
ItemKind::Enum(..) => "enum",
|
|
|
|
ItemKind::Struct(..) => "struct",
|
|
|
|
ItemKind::Union(..) => "union",
|
|
|
|
ItemKind::Trait(..) => "trait",
|
|
|
|
ItemKind::TraitAlias(..) => "trait alias",
|
2020-01-17 16:14:29 -08:00
|
|
|
ItemKind::Impl { .. } => "impl",
|
2014-02-14 07:07:09 +02:00
|
|
|
};
|
2016-04-06 13:51:55 +03:00
|
|
|
format!("{} {}{}", item_str, path_str(), id_str)
|
2014-02-14 07:07:09 +02:00
|
|
|
}
|
2019-12-22 17:42:04 -05:00
|
|
|
Some(Node::ForeignItem(_)) => format!("foreign item {}{}", path_str(), id_str),
|
|
|
|
Some(Node::ImplItem(ii)) => match ii.kind {
|
|
|
|
ImplItemKind::Const(..) => {
|
|
|
|
format!("assoc const {} in {}{}", ii.ident, path_str(), id_str)
|
2014-08-04 13:56:56 -07:00
|
|
|
}
|
2019-12-22 17:42:04 -05:00
|
|
|
ImplItemKind::Method(..) => format!("method {} in {}{}", ii.ident, path_str(), id_str),
|
|
|
|
ImplItemKind::TyAlias(_) => {
|
|
|
|
format!("assoc type {} in {}{}", ii.ident, path_str(), id_str)
|
|
|
|
}
|
|
|
|
ImplItemKind::OpaqueTy(_) => {
|
|
|
|
format!("assoc opaque type {} in {}{}", ii.ident, path_str(), id_str)
|
|
|
|
}
|
|
|
|
},
|
2018-08-25 15:56:16 +01:00
|
|
|
Some(Node::TraitItem(ti)) => {
|
2019-09-26 17:07:54 +01:00
|
|
|
let kind = match ti.kind {
|
2016-12-04 04:21:06 +02:00
|
|
|
TraitItemKind::Const(..) => "assoc constant",
|
2020-03-03 12:46:22 -06:00
|
|
|
TraitItemKind::Fn(..) => "trait method",
|
2016-12-04 04:21:06 +02:00
|
|
|
TraitItemKind::Type(..) => "assoc type",
|
2015-03-10 12:28:44 +02:00
|
|
|
};
|
|
|
|
|
2018-06-10 22:24:24 +03:00
|
|
|
format!("{} {} in {}{}", kind, ti.ident, path_str(), id_str)
|
2014-02-14 07:07:09 +02:00
|
|
|
}
|
2018-08-25 15:56:16 +01:00
|
|
|
Some(Node::Variant(ref variant)) => {
|
2019-12-22 17:42:04 -05:00
|
|
|
format!("variant {} in {}{}", variant.ident, path_str(), id_str)
|
2014-02-14 07:07:09 +02:00
|
|
|
}
|
2018-08-25 15:56:16 +01:00
|
|
|
Some(Node::Field(ref field)) => {
|
2019-12-22 17:42:04 -05:00
|
|
|
format!("field {} in {}{}", field.ident, path_str(), id_str)
|
2014-02-14 07:07:09 +02:00
|
|
|
}
|
2019-12-22 17:42:04 -05:00
|
|
|
Some(Node::AnonConst(_)) => format!("const {}{}", map.hir_to_pretty_string(id), id_str),
|
|
|
|
Some(Node::Expr(_)) => format!("expr {}{}", map.hir_to_pretty_string(id), id_str),
|
|
|
|
Some(Node::Stmt(_)) => format!("stmt {}{}", map.hir_to_pretty_string(id), id_str),
|
2018-09-11 15:08:47 +12:00
|
|
|
Some(Node::PathSegment(_)) => {
|
2019-04-27 22:39:17 +02:00
|
|
|
format!("path segment {}{}", map.hir_to_pretty_string(id), id_str)
|
2018-09-11 15:08:47 +12:00
|
|
|
}
|
2019-12-22 17:42:04 -05:00
|
|
|
Some(Node::Ty(_)) => format!("type {}{}", map.hir_to_pretty_string(id), id_str),
|
|
|
|
Some(Node::TraitRef(_)) => format!("trait_ref {}{}", map.hir_to_pretty_string(id), id_str),
|
|
|
|
Some(Node::Binding(_)) => format!("local {}{}", map.hir_to_pretty_string(id), id_str),
|
|
|
|
Some(Node::Pat(_)) => format!("pat {}{}", map.hir_to_pretty_string(id), id_str),
|
|
|
|
Some(Node::Param(_)) => format!("param {}{}", map.hir_to_pretty_string(id), id_str),
|
|
|
|
Some(Node::Arm(_)) => format!("arm {}{}", map.hir_to_pretty_string(id), id_str),
|
|
|
|
Some(Node::Block(_)) => format!("block {}{}", map.hir_to_pretty_string(id), id_str),
|
|
|
|
Some(Node::Local(_)) => format!("local {}{}", map.hir_to_pretty_string(id), id_str),
|
|
|
|
Some(Node::Ctor(..)) => format!("ctor {}{}", path_str(), id_str),
|
|
|
|
Some(Node::Lifetime(_)) => format!("lifetime {}{}", map.hir_to_pretty_string(id), id_str),
|
|
|
|
Some(Node::GenericParam(ref param)) => format!("generic_param {:?}{}", param, id_str),
|
|
|
|
Some(Node::Visibility(ref vis)) => format!("visibility {:?}{}", vis, id_str),
|
|
|
|
Some(Node::MacroDef(_)) => format!("macro {}{}", path_str(), id_str),
|
2020-02-07 16:43:36 +01:00
|
|
|
Some(Node::Crate(..)) => String::from("root_crate"),
|
2018-08-25 15:48:42 +01:00
|
|
|
None => format!("unknown node{}", id_str),
|
2013-10-29 06:03:32 -04:00
|
|
|
}
|
|
|
|
}
|
2018-02-19 16:46:40 +01:00
|
|
|
|
2019-05-28 23:31:01 +03:00
|
|
|
pub fn provide(providers: &mut Providers<'_>) {
|
|
|
|
providers.def_kind = |tcx, def_id| {
|
2019-06-20 09:41:26 +02:00
|
|
|
if let Some(hir_id) = tcx.hir().as_local_hir_id(def_id) {
|
|
|
|
tcx.hir().def_kind(hir_id)
|
2019-05-28 23:31:01 +03:00
|
|
|
} else {
|
2019-12-22 17:42:04 -05:00
|
|
|
bug!("calling local def_kind query provider for upstream DefId: {:?}", def_id);
|
2019-05-28 23:31:01 +03:00
|
|
|
}
|
|
|
|
};
|
2018-02-19 16:46:40 +01:00
|
|
|
}
|