2016-04-14 12:14:03 +12:00
|
|
|
use self::collector::NodeCollector;
|
2016-09-23 21:13:59 +00:00
|
|
|
pub use self::def_collector::{DefCollector, MacroInvocationData};
|
2016-03-16 05:31:51 -04:00
|
|
|
pub use self::definitions::{Definitions, DefKey, DefPath, DefPathData,
|
2017-05-31 13:54:38 +02:00
|
|
|
DisambiguatedDefPathData, DefPathHash};
|
2014-11-06 00:05:53 -08:00
|
|
|
|
2019-02-05 11:20:45 -06:00
|
|
|
use crate::dep_graph::{DepGraph, DepNode, DepKind, DepNodeIndex};
|
2016-01-29 15:07:04 -05:00
|
|
|
|
2019-02-05 11:20:45 -06:00
|
|
|
use crate::hir::def_id::{CRATE_DEF_INDEX, DefId, LocalDefId, DefIndexAddressSpace};
|
2015-07-31 00:04:06 -07:00
|
|
|
|
2019-02-05 11:20:45 -06:00
|
|
|
use crate::middle::cstore::CrateStoreDyn;
|
2018-02-23 10:02:10 -08:00
|
|
|
|
2018-04-25 19:30:39 +03:00
|
|
|
use rustc_target::spec::abi::Abi;
|
2018-08-03 12:22:22 -06:00
|
|
|
use rustc_data_structures::svh::Svh;
|
2019-03-09 08:23:35 +01:00
|
|
|
use syntax::ast::{self, Name, NodeId};
|
2018-08-18 12:14:03 +02:00
|
|
|
use syntax::source_map::Spanned;
|
2018-02-19 16:46:40 +01:00
|
|
|
use syntax::ext::base::MacroKind;
|
2018-06-27 15:25:18 -07:00
|
|
|
use syntax_pos::{Span, DUMMY_SP};
|
2015-07-31 00:04:06 -07:00
|
|
|
|
2019-02-05 11:20:45 -06:00
|
|
|
use crate::hir::*;
|
|
|
|
use crate::hir::itemlikevisit::ItemLikeVisitor;
|
|
|
|
use crate::hir::print::Nested;
|
|
|
|
use crate::util::nodemap::FxHashMap;
|
|
|
|
use crate::util::common::time;
|
2012-12-23 17:41:37 -05:00
|
|
|
|
2015-02-26 21:00:43 -08:00
|
|
|
use std::io;
|
2018-07-11 22:41:03 +08:00
|
|
|
use std::result::Result::Err;
|
2019-02-05 11:20:45 -06:00
|
|
|
use crate::ty::TyCtxt;
|
2011-06-19 22:41:21 +02:00
|
|
|
|
2014-07-02 17:50:18 +02:00
|
|
|
pub mod blocks;
|
2015-09-10 15:53:08 -04:00
|
|
|
mod collector;
|
2016-04-14 12:14:03 +12:00
|
|
|
mod def_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
|
|
|
|
2017-03-16 18:17:18 +01:00
|
|
|
pub const ITEM_LIKE_SPACE: DefIndexAddressSpace = DefIndexAddressSpace::Low;
|
|
|
|
pub const REGULAR_SPACE: DefIndexAddressSpace = DefIndexAddressSpace::High;
|
|
|
|
|
2019-02-08 14:53:55 +01:00
|
|
|
/// Represents an entry and its parent `NodeId`.
|
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:48:42 +01:00
|
|
|
dep_node: DepNodeIndex,
|
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 {
|
2018-08-25 15:56:16 +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
|
|
|
}
|
|
|
|
|
2018-06-07 17:03:58 +02:00
|
|
|
fn fn_decl(&self) -> Option<&FnDecl> {
|
2018-08-25 15:48:42 +01:00
|
|
|
match self.node {
|
2018-08-25 15:56:16 +01:00
|
|
|
Node::Item(ref item) => {
|
2018-06-07 17:03:58 +02:00
|
|
|
match item.node {
|
2018-07-11 23:36:06 +08:00
|
|
|
ItemKind::Fn(ref fn_decl, _, _, _) => Some(&fn_decl),
|
2018-06-07 17:03:58 +02:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-25 15:56:16 +01:00
|
|
|
Node::TraitItem(ref item) => {
|
2018-06-07 17:03:58 +02:00
|
|
|
match item.node {
|
|
|
|
TraitItemKind::Method(ref method_sig, _) => Some(&method_sig.decl),
|
|
|
|
_ => None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-25 15:56:16 +01:00
|
|
|
Node::ImplItem(ref item) => {
|
2018-06-07 17:03:58 +02:00
|
|
|
match item.node {
|
|
|
|
ImplItemKind::Method(ref method_sig, _) => Some(&method_sig.decl),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-25 15:56:16 +01:00
|
|
|
Node::Expr(ref expr) => {
|
2018-06-07 17:03:58 +02:00
|
|
|
match expr.node {
|
2018-07-11 20:05:29 +08:00
|
|
|
ExprKind::Closure(_, ref fn_decl, ..) => Some(&fn_decl),
|
2018-06-07 17:03:58 +02:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-25 15:48:42 +01:00
|
|
|
_ => None,
|
2018-06-07 17:03:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-20 21:18:16 -05:00
|
|
|
fn associated_body(self) -> Option<BodyId> {
|
2018-08-25 15:48:42 +01:00
|
|
|
match self.node {
|
2018-08-25 15:56:16 +01:00
|
|
|
Node::Item(item) => {
|
2016-12-20 23:05:21 +02:00
|
|
|
match item.node {
|
2018-07-11 23:36:06 +08:00
|
|
|
ItemKind::Const(_, body) |
|
|
|
|
ItemKind::Static(.., body) |
|
|
|
|
ItemKind::Fn(_, _, _, body) => Some(body),
|
2017-02-20 21:18:16 -05:00
|
|
|
_ => None,
|
2016-12-20 23:05:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-25 15:56:16 +01:00
|
|
|
Node::TraitItem(item) => {
|
2016-12-20 23:05:21 +02:00
|
|
|
match item.node {
|
|
|
|
TraitItemKind::Const(_, Some(body)) |
|
2017-02-20 21:18:16 -05:00
|
|
|
TraitItemKind::Method(_, TraitMethod::Provided(body)) => Some(body),
|
|
|
|
_ => None
|
2016-12-20 23:05:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-25 15:56:16 +01:00
|
|
|
Node::ImplItem(item) => {
|
2016-12-20 23:05:21 +02:00
|
|
|
match item.node {
|
|
|
|
ImplItemKind::Const(_, body) |
|
2017-02-20 21:18:16 -05:00
|
|
|
ImplItemKind::Method(_, body) => Some(body),
|
|
|
|
_ => None,
|
2016-12-20 23:05:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-25 15:56:16 +01:00
|
|
|
Node::AnonConst(constant) => Some(constant.body),
|
2018-05-17 21:28:50 +03:00
|
|
|
|
2018-08-25 15:56:16 +01:00
|
|
|
Node::Expr(expr) => {
|
2016-12-20 23:05:21 +02:00
|
|
|
match expr.node {
|
2018-07-11 20:05:29 +08:00
|
|
|
ExprKind::Closure(.., body, _, _) => Some(body),
|
2017-02-20 21:18:16 -05:00
|
|
|
_ => None,
|
2016-12-20 23:05:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-20 21:18:16 -05:00
|
|
|
_ => None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-04 20:01:14 +01:00
|
|
|
fn is_body_owner(self, hir_id: HirId) -> bool {
|
2017-02-20 21:18:16 -05:00
|
|
|
match self.associated_body() {
|
2019-02-04 20:01:14 +01:00
|
|
|
Some(b) => b.hir_id == hir_id,
|
2017-02-20 21:18:16 -05:00
|
|
|
None => false,
|
2016-12-20 23:05:21 +02:00
|
|
|
}
|
|
|
|
}
|
2013-08-31 17:13:57 +10:00
|
|
|
}
|
|
|
|
|
2014-05-18 02:38:13 +03:00
|
|
|
/// Stores a crate and any number of inlined items from other crates.
|
|
|
|
pub struct Forest {
|
2016-01-29 15:07:04 -05:00
|
|
|
krate: Crate,
|
|
|
|
pub dep_graph: DepGraph,
|
2014-05-18 02:38:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Forest {
|
2016-03-29 13:19:37 -04:00
|
|
|
pub fn new(krate: Crate, dep_graph: &DepGraph) -> Forest {
|
2014-05-18 02:38:13 +03:00
|
|
|
Forest {
|
2017-07-03 11:19:51 -07:00
|
|
|
krate,
|
2016-03-29 13:19:37 -04:00
|
|
|
dep_graph: dep_graph.clone(),
|
2014-05-18 02:38:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-26 03:21:50 +02:00
|
|
|
pub fn krate<'hir>(&'hir self) -> &'hir Crate {
|
2017-06-02 17:36:30 +02:00
|
|
|
self.dep_graph.read(DepNode::new_no_params(DepKind::Krate));
|
2014-05-18 02:38:13 +03:00
|
|
|
&self.krate
|
|
|
|
}
|
2018-12-04 16:26:34 +01:00
|
|
|
|
2019-03-10 10:11:15 +01:00
|
|
|
/// This is used internally in the dependency tracking system.
|
2018-12-11 18:44:57 +01:00
|
|
|
/// Use the `krate` method to ensure your dependency on the
|
|
|
|
/// crate is tracked.
|
2018-12-04 16:26:34 +01:00
|
|
|
pub fn untracked_krate<'hir>(&'hir self) -> &'hir Crate {
|
|
|
|
&self.krate
|
|
|
|
}
|
2014-05-18 02:38:13 +03:00
|
|
|
}
|
|
|
|
|
2019-02-08 14:53:55 +01:00
|
|
|
/// Represents a mapping from `NodeId`s to AST elements and their parent `NodeId`s.
|
2015-07-31 00:04:06 -07:00
|
|
|
#[derive(Clone)]
|
2017-01-26 03:21:50 +02:00
|
|
|
pub struct Map<'hir> {
|
2014-05-18 02:38:13 +03:00
|
|
|
/// The backing storage for all the AST nodes.
|
2017-01-26 03:21:50 +02:00
|
|
|
pub forest: &'hir Forest,
|
2014-05-18 02:38:13 +03:00
|
|
|
|
2016-01-29 15:07:04 -05:00
|
|
|
/// Same as the dep_graph in forest, just available with one fewer
|
2017-08-11 20:34:14 +02:00
|
|
|
/// deref. This is a gratuitous micro-optimization.
|
2016-01-29 15:07:04 -05:00
|
|
|
pub dep_graph: DepGraph,
|
|
|
|
|
2017-12-19 18:01:19 +01:00
|
|
|
/// The SVH of the local crate.
|
|
|
|
pub crate_hash: Svh,
|
|
|
|
|
2018-08-25 15:48:42 +01:00
|
|
|
/// `NodeId`s are sequential integers from 0, so we can be
|
2014-01-17 23:23:09 +11:00
|
|
|
/// super-compact by storing them in a vector. Not everything with
|
2018-08-25 15:48:42 +01:00
|
|
|
/// a `NodeId` is in the map, but empirically the occupancy is about
|
2014-01-17 23:23:09 +11:00
|
|
|
/// 75-80%, so there's not too much overhead (certainly less than
|
|
|
|
/// a hashmap, since they (at the time of writing) have a maximum
|
2014-02-14 07:07:09 +02:00
|
|
|
/// of 75% occupancy).
|
2014-01-17 23:23:09 +11:00
|
|
|
///
|
|
|
|
/// Also, indexing is pretty quick when you've got a vector and
|
|
|
|
/// plain old integers.
|
2019-03-09 08:23:35 +01:00
|
|
|
map: FxHashMap<HirId, Entry<'hir>>,
|
2015-09-10 15:53:08 -04:00
|
|
|
|
2017-09-14 15:10:24 +02:00
|
|
|
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`.
|
|
|
|
hir_to_node_id: FxHashMap<HirId, NodeId>,
|
2014-01-17 23:23:09 +11:00
|
|
|
}
|
|
|
|
|
2017-01-26 03:21:50 +02:00
|
|
|
impl<'hir> Map<'hir> {
|
2016-01-29 15:07:04 -05:00
|
|
|
/// Registers a read in the dependency graph of the AST node with
|
|
|
|
/// the given `id`. This needs to be called each time a public
|
|
|
|
/// function returns the HIR for a node -- in other words, when it
|
|
|
|
/// "reveals" the content of a node to the caller (who might not
|
|
|
|
/// otherwise have had access to those contents, and hence needs a
|
|
|
|
/// read recorded). If the function just returns a DefId or
|
|
|
|
/// NodeId, no actual content was returned, so no read is needed.
|
2016-08-02 16:31:39 -04:00
|
|
|
pub fn read(&self, id: NodeId) {
|
2019-03-09 08:23:35 +01:00
|
|
|
let hir_id = self.node_to_hir_id(id);
|
|
|
|
if let Some(entry) = self.map.get(&hir_id) {
|
2018-08-25 15:48:42 +01:00
|
|
|
self.dep_graph.read_index(entry.dep_node);
|
|
|
|
} else {
|
2018-09-11 15:08:47 +12:00
|
|
|
bug!("called `HirMap::read()` with invalid `NodeId`: {:?}", id)
|
2016-01-29 15:07:04 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-03 09:14:31 +01:00
|
|
|
// FIXME(@ljedrz): replace the NodeId variant
|
|
|
|
pub fn read_by_hir_id(&self, hir_id: HirId) {
|
|
|
|
let node_id = self.hir_to_node_id(hir_id);
|
|
|
|
self.read(node_id);
|
|
|
|
}
|
|
|
|
|
2017-08-04 09:49:40 +02:00
|
|
|
#[inline]
|
2017-09-14 15:10:24 +02:00
|
|
|
pub fn definitions(&self) -> &'hir 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
|
|
|
}
|
|
|
|
|
2016-04-06 13:51:55 +03:00
|
|
|
pub fn def_path_from_id(&self, id: NodeId) -> Option<DefPath> {
|
|
|
|
self.opt_local_def_id(id).map(|def_id| {
|
|
|
|
self.def_path(def_id)
|
|
|
|
})
|
2015-09-17 14:29:59 -04:00
|
|
|
}
|
|
|
|
|
2019-02-03 09:14:31 +01:00
|
|
|
// FIXME(@ljedrz): replace the NodeId variant
|
|
|
|
pub fn def_path_from_hir_id(&self, id: HirId) -> DefPath {
|
|
|
|
self.def_path(self.local_def_id_from_hir_id(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]
|
2015-09-02 16:11:32 -04:00
|
|
|
pub fn local_def_id(&self, node: NodeId) -> DefId {
|
2015-09-10 15:53:08 -04:00
|
|
|
self.opt_local_def_id(node).unwrap_or_else(|| {
|
2019-03-09 08:23:35 +01:00
|
|
|
let hir_id = self.node_to_hir_id(node);
|
2016-03-26 19:59:04 +01:00
|
|
|
bug!("local_def_id: no entry for `{}`, which has a map of `{:?}`",
|
2019-03-09 08:23:35 +01:00
|
|
|
node, self.find_entry(hir_id))
|
2015-09-10 15:53:08 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-02-03 09:14:31 +01:00
|
|
|
// FIXME(@ljedrz): replace the NodeId variant
|
|
|
|
#[inline]
|
|
|
|
pub fn local_def_id_from_hir_id(&self, hir_id: HirId) -> DefId {
|
2019-03-09 08:23:35 +01:00
|
|
|
self.opt_local_def_id_from_hir_id(hir_id).unwrap_or_else(|| {
|
2019-02-03 09:14:31 +01:00
|
|
|
bug!("local_def_id_from_hir_id: no entry for `{:?}`, which has a map of `{:?}`",
|
2019-03-09 08:23:35 +01:00
|
|
|
hir_id, self.find_entry(hir_id))
|
2019-02-03 09:14:31 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME(@ljedrz): replace the NodeId variant
|
|
|
|
#[inline]
|
|
|
|
pub fn opt_local_def_id_from_hir_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]
|
2015-09-10 15:53:08 -04:00
|
|
|
pub fn opt_local_def_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
|
|
|
// FIXME(@ljedrz): replace the NodeId variant
|
|
|
|
#[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)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn def_index_to_node_id(&self, def_index: DefIndex) -> NodeId {
|
|
|
|
self.definitions.as_local_node_id(DefId::local(def_index)).unwrap()
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn local_def_id_to_node_id(&self, def_id: LocalDefId) -> NodeId {
|
|
|
|
self.definitions.as_local_node_id(def_id.to_def_id()).unwrap()
|
|
|
|
}
|
|
|
|
|
2018-02-19 16:46:40 +01:00
|
|
|
pub fn describe_def(&self, node_id: NodeId) -> Option<Def> {
|
|
|
|
let node = if let Some(node) = self.find(node_id) {
|
|
|
|
node
|
|
|
|
} else {
|
|
|
|
return None
|
|
|
|
};
|
|
|
|
|
|
|
|
match node {
|
2018-08-25 15:56:16 +01:00
|
|
|
Node::Item(item) => {
|
2019-02-27 17:35:24 +01:00
|
|
|
let def_id = || self.local_def_id_from_hir_id(item.hir_id);
|
2018-02-19 16:46:40 +01:00
|
|
|
|
|
|
|
match item.node {
|
2018-09-12 12:31:11 +02:00
|
|
|
ItemKind::Static(_, m, _) => Some(Def::Static(def_id(), m == MutMutable)),
|
2018-07-11 23:36:06 +08:00
|
|
|
ItemKind::Const(..) => Some(Def::Const(def_id())),
|
|
|
|
ItemKind::Fn(..) => Some(Def::Fn(def_id())),
|
|
|
|
ItemKind::Mod(..) => Some(Def::Mod(def_id())),
|
|
|
|
ItemKind::Existential(..) => Some(Def::Existential(def_id())),
|
|
|
|
ItemKind::Ty(..) => Some(Def::TyAlias(def_id())),
|
|
|
|
ItemKind::Enum(..) => Some(Def::Enum(def_id())),
|
|
|
|
ItemKind::Struct(..) => Some(Def::Struct(def_id())),
|
|
|
|
ItemKind::Union(..) => Some(Def::Union(def_id())),
|
|
|
|
ItemKind::Trait(..) => Some(Def::Trait(def_id())),
|
2018-10-12 01:50:03 +01:00
|
|
|
ItemKind::TraitAlias(..) => Some(Def::TraitAlias(def_id())),
|
2018-07-11 23:36:06 +08:00
|
|
|
ItemKind::ExternCrate(_) |
|
|
|
|
ItemKind::Use(..) |
|
|
|
|
ItemKind::ForeignMod(..) |
|
2018-07-31 01:59:41 +03:00
|
|
|
ItemKind::GlobalAsm(..) |
|
2018-07-11 23:36:06 +08:00
|
|
|
ItemKind::Impl(..) => None,
|
2018-02-19 16:46:40 +01:00
|
|
|
}
|
|
|
|
}
|
2018-08-25 15:56:16 +01:00
|
|
|
Node::ForeignItem(item) => {
|
2019-02-27 16:12:35 +01:00
|
|
|
let def_id = self.local_def_id_from_hir_id(item.hir_id);
|
2018-02-19 16:46:40 +01:00
|
|
|
match item.node {
|
2018-07-11 22:56:44 +08:00
|
|
|
ForeignItemKind::Fn(..) => Some(Def::Fn(def_id)),
|
|
|
|
ForeignItemKind::Static(_, m) => Some(Def::Static(def_id, m)),
|
2018-08-22 11:47:31 +01:00
|
|
|
ForeignItemKind::Type => Some(Def::ForeignTy(def_id)),
|
2018-02-19 16:46:40 +01:00
|
|
|
}
|
|
|
|
}
|
2018-08-25 15:56:16 +01:00
|
|
|
Node::TraitItem(item) => {
|
2019-02-26 11:04:58 +01:00
|
|
|
let def_id = self.local_def_id_from_hir_id(item.hir_id);
|
2018-02-19 16:46:40 +01:00
|
|
|
match item.node {
|
|
|
|
TraitItemKind::Const(..) => Some(Def::AssociatedConst(def_id)),
|
|
|
|
TraitItemKind::Method(..) => Some(Def::Method(def_id)),
|
|
|
|
TraitItemKind::Type(..) => Some(Def::AssociatedTy(def_id)),
|
|
|
|
}
|
|
|
|
}
|
2018-08-25 15:56:16 +01:00
|
|
|
Node::ImplItem(item) => {
|
2019-02-26 15:11:59 +01:00
|
|
|
let def_id = self.local_def_id_from_hir_id(item.hir_id);
|
2018-02-19 16:46:40 +01:00
|
|
|
match item.node {
|
|
|
|
ImplItemKind::Const(..) => Some(Def::AssociatedConst(def_id)),
|
|
|
|
ImplItemKind::Method(..) => Some(Def::Method(def_id)),
|
|
|
|
ImplItemKind::Type(..) => Some(Def::AssociatedTy(def_id)),
|
2018-07-03 19:38:14 +02:00
|
|
|
ImplItemKind::Existential(..) => Some(Def::AssociatedExistential(def_id)),
|
2018-02-19 16:46:40 +01:00
|
|
|
}
|
|
|
|
}
|
2018-08-25 15:56:16 +01:00
|
|
|
Node::Variant(variant) => {
|
2019-03-21 23:38:50 +01:00
|
|
|
let def_id = self.local_def_id_from_hir_id(variant.node.id);
|
2018-02-19 16:46:40 +01:00
|
|
|
Some(Def::Variant(def_id))
|
|
|
|
}
|
2019-03-24 18:21:59 +01:00
|
|
|
Node::Ctor(variant_data) => {
|
|
|
|
let ctor_of = match self.find(self.get_parent_node(node_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-03-21 23:38:50 +01:00
|
|
|
variant_data.ctor_hir_id()
|
|
|
|
.map(|hir_id| self.local_def_id_from_hir_id(hir_id))
|
2019-03-24 19:16:44 +01:00
|
|
|
.map(|def_id| Def::Ctor(def_id, ctor_of, def::CtorKind::from_hir(variant_data)))
|
2019-02-03 14:09:56 +01:00
|
|
|
}
|
2019-02-05 21:49:53 +01:00
|
|
|
Node::AnonConst(_) |
|
2018-08-25 15:56:16 +01:00
|
|
|
Node::Field(_) |
|
|
|
|
Node::Expr(_) |
|
|
|
|
Node::Stmt(_) |
|
2018-09-11 15:08:47 +12:00
|
|
|
Node::PathSegment(_) |
|
2018-08-25 15:56:16 +01:00
|
|
|
Node::Ty(_) |
|
|
|
|
Node::TraitRef(_) |
|
|
|
|
Node::Pat(_) |
|
|
|
|
Node::Binding(_) |
|
|
|
|
Node::Lifetime(_) |
|
|
|
|
Node::Visibility(_) |
|
|
|
|
Node::Block(_) |
|
|
|
|
Node::Crate => None,
|
|
|
|
Node::Local(local) => {
|
2019-02-26 10:04:27 +01:00
|
|
|
Some(Def::Local(self.hir_to_node_id(local.hir_id)))
|
2018-02-19 16:46:40 +01:00
|
|
|
}
|
2018-08-25 15:56:16 +01:00
|
|
|
Node::MacroDef(macro_def) => {
|
2019-02-18 15:55:00 +01:00
|
|
|
Some(Def::Macro(self.local_def_id_from_hir_id(macro_def.hir_id),
|
2018-02-19 16:46:40 +01:00
|
|
|
MacroKind::Bang))
|
|
|
|
}
|
2018-08-25 15:56:16 +01:00
|
|
|
Node::GenericParam(param) => {
|
2018-05-28 14:05:06 +01:00
|
|
|
Some(match param.kind {
|
2019-02-18 14:53:25 +01:00
|
|
|
GenericParamKind::Lifetime { .. } => {
|
|
|
|
let node_id = self.hir_to_node_id(param.hir_id);
|
|
|
|
Def::Local(node_id)
|
|
|
|
},
|
|
|
|
GenericParamKind::Type { .. } => Def::TyParam(
|
|
|
|
self.local_def_id_from_hir_id(param.hir_id)),
|
|
|
|
GenericParamKind::Const { .. } => Def::ConstParam(
|
|
|
|
self.local_def_id_from_hir_id(param.hir_id)),
|
2018-05-28 14:05:06 +01:00
|
|
|
})
|
2018-02-19 16:46:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-04 11:09:32 +01:00
|
|
|
// FIXME(@ljedrz): replace the NodeId variant
|
|
|
|
pub fn describe_def_by_hir_id(&self, hir_id: HirId) -> Option<Def> {
|
|
|
|
let node_id = self.hir_to_node_id(hir_id);
|
|
|
|
self.describe_def(node_id)
|
|
|
|
}
|
|
|
|
|
2015-01-17 23:33:05 +00:00
|
|
|
fn entry_count(&self) -> usize {
|
2017-01-04 04:01:58 +02:00
|
|
|
self.map.len()
|
2014-08-07 11:22:42 +02:00
|
|
|
}
|
|
|
|
|
2019-03-09 08:23:35 +01:00
|
|
|
fn find_entry(&self, id: HirId) -> Option<Entry<'hir>> {
|
|
|
|
self.map.get(&id).cloned()
|
2014-05-18 02:38:13 +03:00
|
|
|
}
|
|
|
|
|
2017-01-26 03:21:50 +02:00
|
|
|
pub fn krate(&self) -> &'hir Crate {
|
2016-01-29 15:07:04 -05:00
|
|
|
self.forest.krate()
|
2014-02-14 07:07:09 +02:00
|
|
|
}
|
|
|
|
|
2017-01-26 03:21:50 +02:00
|
|
|
pub fn trait_item(&self, id: TraitItemId) -> &'hir TraitItem {
|
2019-03-01 10:28:13 +01:00
|
|
|
self.read_by_hir_id(id.hir_id);
|
2016-12-04 04:21:06 +02:00
|
|
|
|
2018-11-27 02:59:49 +00:00
|
|
|
// N.B., intentionally bypass `self.forest.krate()` so that we
|
2016-12-04 04:21:06 +02:00
|
|
|
// do not trigger a read of the whole krate here
|
|
|
|
self.forest.krate.trait_item(id)
|
|
|
|
}
|
|
|
|
|
2017-01-26 03:21:50 +02:00
|
|
|
pub fn impl_item(&self, id: ImplItemId) -> &'hir ImplItem {
|
2019-03-01 10:28:13 +01:00
|
|
|
self.read_by_hir_id(id.hir_id);
|
2016-11-02 18:25:31 -04:00
|
|
|
|
2018-11-27 02:59:49 +00:00
|
|
|
// N.B., intentionally bypass `self.forest.krate()` so that we
|
2016-11-02 18:25:31 -04:00
|
|
|
// do not trigger a read of the whole krate here
|
|
|
|
self.forest.krate.impl_item(id)
|
|
|
|
}
|
|
|
|
|
2017-01-26 03:21:50 +02:00
|
|
|
pub fn body(&self, id: BodyId) -> &'hir Body {
|
2019-02-04 20:01:14 +01:00
|
|
|
self.read_by_hir_id(id.hir_id);
|
2016-12-21 12:32:59 +02:00
|
|
|
|
2018-11-27 02:59:49 +00:00
|
|
|
// N.B., intentionally bypass `self.forest.krate()` so that we
|
2016-12-21 12:32:59 +02:00
|
|
|
// do not trigger a read of the whole krate here
|
|
|
|
self.forest.krate.body(id)
|
|
|
|
}
|
|
|
|
|
2018-06-07 17:03:58 +02:00
|
|
|
pub fn fn_decl(&self, node_id: ast::NodeId) -> Option<FnDecl> {
|
2019-03-09 08:23:35 +01:00
|
|
|
let hir_id = self.node_to_hir_id(node_id);
|
|
|
|
if let Some(entry) = self.find_entry(hir_id) {
|
2018-09-12 12:31:11 +02:00
|
|
|
entry.fn_decl().cloned()
|
2018-06-07 17:03:58 +02:00
|
|
|
} else {
|
|
|
|
bug!("no entry for node_id `{}`", node_id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-04 11:09:32 +01:00
|
|
|
// FIXME(@ljedrz): replace the NodeId variant
|
|
|
|
pub fn fn_decl_by_hir_id(&self, hir_id: HirId) -> Option<FnDecl> {
|
|
|
|
let node_id = self.hir_to_node_id(hir_id);
|
|
|
|
self.fn_decl(node_id)
|
|
|
|
}
|
|
|
|
|
2016-12-20 23:05:21 +02:00
|
|
|
/// Returns the `NodeId` 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-02-04 20:01:14 +01:00
|
|
|
pub fn body_owner(&self, BodyId { hir_id }: BodyId) -> NodeId {
|
2019-03-09 08:23:35 +01:00
|
|
|
let parent = self.get_parent_node_by_hir_id(hir_id);
|
|
|
|
assert!(self.map.get(&parent).map_or(false, |e| e.is_body_owner(hir_id)));
|
|
|
|
self.hir_to_node_id(parent)
|
2016-12-20 23:05:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn body_owner_def_id(&self, id: BodyId) -> DefId {
|
|
|
|
self.local_def_id(self.body_owner(id))
|
|
|
|
}
|
|
|
|
|
2019-02-08 14:53:55 +01:00
|
|
|
/// Given a `NodeId`, returns the `BodyId` associated with it,
|
2017-04-24 22:03:47 +03:00
|
|
|
/// if the node is a body owner, otherwise returns `None`.
|
|
|
|
pub fn maybe_body_owned_by(&self, id: NodeId) -> Option<BodyId> {
|
2019-03-09 08:23:35 +01:00
|
|
|
let hir_id = self.node_to_hir_id(id);
|
|
|
|
if let Some(entry) = self.find_entry(hir_id) {
|
2017-11-08 11:31:15 +01:00
|
|
|
if self.dep_graph.is_fully_enabled() {
|
2019-03-09 08:23:35 +01:00
|
|
|
let hir_id_owner = hir_id.owner;
|
2017-11-08 11:31:15 +01:00
|
|
|
let def_path_hash = self.definitions.def_path_hash(hir_id_owner);
|
|
|
|
self.dep_graph.read(def_path_hash.to_dep_node(DepKind::HirBody));
|
|
|
|
}
|
|
|
|
|
2018-05-17 21:28:50 +03:00
|
|
|
entry.associated_body()
|
2017-04-04 12:06:35 -04:00
|
|
|
} else {
|
|
|
|
bug!("no entry for id `{}`", id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-04 20:01:14 +01:00
|
|
|
// FIXME(@ljedrz): replace the NodeId variant
|
|
|
|
pub fn maybe_body_owned_by_by_hir_id(&self, id: HirId) -> Option<BodyId> {
|
|
|
|
let node_id = self.hir_to_node_id(id);
|
|
|
|
self.maybe_body_owned_by(node_id)
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
self.maybe_body_owned_by_by_hir_id(id).unwrap_or_else(|| {
|
|
|
|
span_bug!(self.span_by_hir_id(id), "body_owned_by: {} has no associated body",
|
|
|
|
self.hir_to_string(id));
|
2017-04-24 22:03:47 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-11-10 19:20:35 +02:00
|
|
|
pub fn body_owner_kind(&self, id: NodeId) -> BodyOwnerKind {
|
|
|
|
match self.get(id) {
|
2018-08-25 15:56:16 +01:00
|
|
|
Node::Item(&Item { node: ItemKind::Const(..), .. }) |
|
|
|
|
Node::TraitItem(&TraitItem { node: TraitItemKind::Const(..), .. }) |
|
|
|
|
Node::ImplItem(&ImplItem { node: ImplItemKind::Const(..), .. }) |
|
|
|
|
Node::AnonConst(_) => {
|
2017-11-10 19:20:35 +02:00
|
|
|
BodyOwnerKind::Const
|
|
|
|
}
|
2019-03-21 23:38:50 +01:00
|
|
|
Node::Ctor(..) |
|
2018-12-07 18:25:55 +01:00
|
|
|
Node::Item(&Item { node: ItemKind::Fn(..), .. }) |
|
|
|
|
Node::TraitItem(&TraitItem { node: TraitItemKind::Method(..), .. }) |
|
|
|
|
Node::ImplItem(&ImplItem { node: ImplItemKind::Method(..), .. }) => {
|
|
|
|
BodyOwnerKind::Fn
|
|
|
|
}
|
2018-08-25 15:56:16 +01:00
|
|
|
Node::Item(&Item { node: ItemKind::Static(_, m, _), .. }) => {
|
2017-11-10 19:20:35 +02:00
|
|
|
BodyOwnerKind::Static(m)
|
|
|
|
}
|
2018-12-07 18:25:55 +01:00
|
|
|
Node::Expr(&Expr { node: ExprKind::Closure(..), .. }) => {
|
|
|
|
BodyOwnerKind::Closure
|
|
|
|
}
|
|
|
|
node => bug!("{:#?} is not a body node", node),
|
2017-11-10 19:20:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-04 20:01:14 +01:00
|
|
|
// FIXME(@ljedrz): replace the NodeId variant
|
|
|
|
pub fn body_owner_kind_by_hir_id(&self, id: HirId) -> BodyOwnerKind {
|
|
|
|
let node_id = self.hir_to_node_id(id);
|
|
|
|
self.body_owner_kind(node_id)
|
|
|
|
}
|
|
|
|
|
2019-03-04 09:00:30 +01:00
|
|
|
pub fn ty_param_owner(&self, id: HirId) -> HirId {
|
|
|
|
match self.get_by_hir_id(id) {
|
2019-03-11 16:39:40 -07:00
|
|
|
Node::Item(&Item { node: ItemKind::Trait(..), .. }) |
|
|
|
|
Node::Item(&Item { node: ItemKind::TraitAlias(..), .. }) => id,
|
2019-03-04 09:00:30 +01:00
|
|
|
Node::GenericParam(_) => self.get_parent_node_by_hir_id(id),
|
|
|
|
_ => bug!("ty_param_owner: {} not a type parameter", self.hir_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 {
|
|
|
|
match self.get_by_hir_id(id) {
|
2019-03-11 16:39:40 -07:00
|
|
|
Node::Item(&Item { node: ItemKind::Trait(..), .. }) |
|
|
|
|
Node::Item(&Item { node: ItemKind::TraitAlias(..), .. }) => keywords::SelfUpper.name(),
|
2018-08-25 15:56:16 +01:00
|
|
|
Node::GenericParam(param) => param.name.ident().name,
|
2019-03-04 09:00:30 +01:00
|
|
|
_ => bug!("ty_param_name: {} not a type parameter", self.hir_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] {
|
2017-06-02 17:36:30 +02:00
|
|
|
self.dep_graph.read(DepNode::new_no_params(DepKind::AllLocalTraitImpls));
|
2017-02-19 14:46:29 +02:00
|
|
|
|
2018-11-27 02:59:49 +00:00
|
|
|
// N.B., intentionally bypass `self.forest.krate()` so that we
|
2017-02-19 14:46:29 +02:00
|
|
|
// do not trigger a read of the whole krate here
|
|
|
|
self.forest.krate.trait_impls.get(&trait_did).map_or(&[], |xs| &xs[..])
|
|
|
|
}
|
|
|
|
|
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] {
|
2017-06-02 17:36:30 +02:00
|
|
|
let def_path_hash = self.definitions.def_path_hash(CRATE_DEF_INDEX);
|
|
|
|
|
|
|
|
self.dep_graph.read(def_path_hash.to_dep_node(DepKind::Hir));
|
2016-03-16 05:53:45 -04:00
|
|
|
&self.forest.krate.attrs
|
|
|
|
}
|
|
|
|
|
2019-03-09 08:23:35 +01:00
|
|
|
pub fn get_module(&self, module: DefId) -> (&'hir Mod, Span, HirId)
|
|
|
|
{
|
|
|
|
let hir_id = self.as_local_hir_id(module).unwrap();
|
|
|
|
self.read_by_hir_id(hir_id);
|
|
|
|
match self.find_entry(hir_id).unwrap().node {
|
2018-06-08 19:14:03 +02:00
|
|
|
Node::Item(&Item {
|
|
|
|
span,
|
|
|
|
node: ItemKind::Mod(ref m),
|
|
|
|
..
|
2018-06-11 08:48:15 +02:00
|
|
|
}) => (m, span, hir_id),
|
|
|
|
Node::Crate => (&self.forest.krate.module, self.forest.krate.span, hir_id),
|
2018-06-08 19:14:03 +02:00
|
|
|
_ => panic!("not a module")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-11 04:58:46 +01:00
|
|
|
pub fn visit_item_likes_in_module<V>(&self, module: DefId, visitor: &mut V)
|
2018-06-06 22:13:52 +02:00
|
|
|
where V: ItemLikeVisitor<'hir>
|
|
|
|
{
|
|
|
|
let node_id = self.as_local_node_id(module).unwrap();
|
|
|
|
|
|
|
|
// Read the module so we'll be re-executed if new items
|
|
|
|
// appear immediately under in the module. If some new item appears
|
2019-01-11 04:58:46 +01:00
|
|
|
// in some nested item in the module, we'll be re-executed due to reads
|
|
|
|
// in the expect_* calls the loops below
|
2018-06-06 22:13:52 +02:00
|
|
|
self.read(node_id);
|
|
|
|
|
|
|
|
let module = &self.forest.krate.modules[&node_id];
|
|
|
|
|
|
|
|
for id in &module.items {
|
2019-03-11 09:44:19 +01:00
|
|
|
visitor.visit_item(self.expect_item_by_hir_id(*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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-25 15:56:16 +01:00
|
|
|
/// Retrieve the Node corresponding to `id`, panicking if it cannot
|
2014-01-17 23:23:09 +11:00
|
|
|
/// be found.
|
2018-08-25 15:56:16 +01:00
|
|
|
pub fn get(&self, id: NodeId) -> Node<'hir> {
|
2018-09-12 12:31:11 +02:00
|
|
|
// read recorded by `find`
|
|
|
|
self.find(id).unwrap_or_else(|| bug!("couldn't find node id {} in the AST map", id))
|
2014-01-17 23:23:09 +11:00
|
|
|
}
|
2014-02-14 07:07:09 +02:00
|
|
|
|
2019-02-03 09:14:31 +01:00
|
|
|
// FIXME(@ljedrz): replace the NodeId variant
|
|
|
|
pub fn get_by_hir_id(&self, id: HirId) -> Node<'hir> {
|
|
|
|
let node_id = self.hir_to_node_id(id);
|
|
|
|
self.get(node_id)
|
|
|
|
}
|
|
|
|
|
2018-08-25 15:56:16 +01:00
|
|
|
pub fn get_if_local(&self, id: DefId) -> Option<Node<'hir>> {
|
2016-01-29 15:07:04 -05:00
|
|
|
self.as_local_node_id(id).map(|id| self.get(id)) // read recorded by `get`
|
2015-09-04 13:52:28 -04:00
|
|
|
}
|
|
|
|
|
2018-06-27 15:25:18 -07:00
|
|
|
pub fn get_generics(&self, id: DefId) -> Option<&'hir Generics> {
|
|
|
|
self.get_if_local(id).and_then(|node| {
|
|
|
|
match node {
|
2018-08-25 15:56:16 +01:00
|
|
|
Node::ImplItem(ref impl_item) => Some(&impl_item.generics),
|
|
|
|
Node::TraitItem(ref trait_item) => Some(&trait_item.generics),
|
|
|
|
Node::Item(ref item) => {
|
2018-06-27 15:25:18 -07:00
|
|
|
match item.node {
|
2018-07-11 23:36:06 +08:00
|
|
|
ItemKind::Fn(_, _, ref generics, _) |
|
|
|
|
ItemKind::Ty(_, ref generics) |
|
|
|
|
ItemKind::Enum(_, ref generics) |
|
|
|
|
ItemKind::Struct(_, ref generics) |
|
|
|
|
ItemKind::Union(_, ref generics) |
|
|
|
|
ItemKind::Trait(_, _, ref generics, ..) |
|
|
|
|
ItemKind::TraitAlias(ref generics, _) |
|
|
|
|
ItemKind::Impl(_, _, _, ref generics, ..) => Some(generics),
|
2018-06-27 15:25:18 -07:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_generics_span(&self, id: DefId) -> Option<Span> {
|
|
|
|
self.get_generics(id).map(|generics| generics.span).filter(|sp| *sp != DUMMY_SP)
|
|
|
|
}
|
|
|
|
|
2019-02-08 14:53:55 +01:00
|
|
|
/// Retrieves the `Node` corresponding to `id`, returning `None` if cannot be found.
|
2018-08-25 15:56:16 +01:00
|
|
|
pub fn find(&self, id: NodeId) -> Option<Node<'hir>> {
|
2019-03-09 08:23:35 +01:00
|
|
|
let hir_id = self.node_to_hir_id(id);
|
|
|
|
let result = self.find_entry(hir_id).and_then(|entry| {
|
2018-08-26 00:20:24 +01:00
|
|
|
if let Node::Crate = entry.node {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(entry.node)
|
|
|
|
}
|
|
|
|
});
|
2016-01-29 15:07:04 -05:00
|
|
|
if result.is_some() {
|
2019-03-09 08:23:35 +01:00
|
|
|
self.read_by_hir_id(hir_id);
|
2016-01-29 15:07:04 -05:00
|
|
|
}
|
|
|
|
result
|
2014-02-14 07:07:09 +02:00
|
|
|
}
|
|
|
|
|
2019-02-03 09:14:31 +01:00
|
|
|
// FIXME(@ljedrz): replace the NodeId variant
|
|
|
|
pub fn find_by_hir_id(&self, hir_id: HirId) -> Option<Node<'hir>> {
|
|
|
|
let node_id = self.hir_to_node_id(hir_id);
|
|
|
|
self.find(node_id)
|
|
|
|
}
|
|
|
|
|
2018-11-27 02:59:49 +00:00
|
|
|
/// Similar to `get_parent`; returns the parent node-id, or own `id` if there is
|
|
|
|
/// no parent. Note that the parent may be `CRATE_NODE_ID`, which is not itself
|
2017-08-01 19:13:47 -07:00
|
|
|
/// present in the map -- so passing the return value of get_parent_node to
|
|
|
|
/// get may actually panic.
|
2015-07-01 08:58:48 +12:00
|
|
|
/// This function returns the immediate parent in the AST, whereas get_parent
|
|
|
|
/// returns the enclosing item. Note that this might not be the actual parent
|
|
|
|
/// node in the AST - some kinds of nodes are not in the map and these will
|
2018-11-27 02:59:49 +00:00
|
|
|
/// never appear as the parent_node. So you can always walk the `parent_nodes`
|
2019-02-08 14:53:55 +01:00
|
|
|
/// from a node to the root of the ast (unless you get the same ID back here
|
|
|
|
/// that can happen if the ID is not in the map itself or is just weird).
|
2015-07-01 08:58:48 +12:00
|
|
|
pub fn get_parent_node(&self, id: NodeId) -> NodeId {
|
2019-03-09 08:23:35 +01:00
|
|
|
let hir_id = self.node_to_hir_id(id);
|
2017-11-08 11:31:15 +01:00
|
|
|
if self.dep_graph.is_fully_enabled() {
|
2019-03-09 08:23:35 +01:00
|
|
|
let hir_id_owner = hir_id.owner;
|
2017-11-08 11:31:15 +01:00
|
|
|
let def_path_hash = self.definitions.def_path_hash(hir_id_owner);
|
|
|
|
self.dep_graph.read(def_path_hash.to_dep_node(DepKind::HirBody));
|
|
|
|
}
|
|
|
|
|
2019-03-09 08:23:35 +01:00
|
|
|
self.find_entry(hir_id)
|
|
|
|
.and_then(|x| x.parent_node())
|
|
|
|
.map(|x| self.hir_to_node_id(x))
|
|
|
|
.unwrap_or(id)
|
2015-07-01 08:58:48 +12:00
|
|
|
}
|
|
|
|
|
2019-02-03 09:14:31 +01:00
|
|
|
// FIXME(@ljedrz): replace the NodeId variant
|
|
|
|
pub fn get_parent_node_by_hir_id(&self, id: HirId) -> HirId {
|
|
|
|
let node_id = self.hir_to_node_id(id);
|
|
|
|
let parent_node_id = self.get_parent_node(node_id);
|
|
|
|
self.node_to_hir_id(parent_node_id)
|
|
|
|
}
|
|
|
|
|
2015-10-27 00:46:11 +09:00
|
|
|
/// Check if the node is an argument. An argument is a local variable whose
|
|
|
|
/// immediate parent is an item or a closure.
|
|
|
|
pub fn is_argument(&self, id: NodeId) -> bool {
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
match self.find(self.get_parent_node(id)) {
|
2018-08-25 15:56:16 +01:00
|
|
|
Some(Node::Item(_)) |
|
|
|
|
Some(Node::TraitItem(_)) |
|
|
|
|
Some(Node::ImplItem(_)) => true,
|
|
|
|
Some(Node::Expr(e)) => {
|
2015-10-27 00:46:11 +09:00
|
|
|
match e.node {
|
2018-07-11 20:05:29 +08:00
|
|
|
ExprKind::Closure(..) => true,
|
2015-10-27 00:46:11 +09:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-01 08:58:48 +12:00
|
|
|
/// If there is some error when walking the parents (e.g., a node does not
|
|
|
|
/// have a parent in the map or a node can't be found), then we return the
|
2019-02-08 14:53:55 +01:00
|
|
|
/// last good `NodeId` we found. Note that reaching the crate root (`id == 0`),
|
2015-07-01 08:58:48 +12:00
|
|
|
/// is not an error, since items in the crate module have the crate root as
|
|
|
|
/// parent.
|
2017-06-24 00:57:39 -07:00
|
|
|
fn walk_parent_nodes<F, F2>(&self,
|
2019-03-09 08:23:35 +01:00
|
|
|
start_id: HirId,
|
2017-06-24 00:57:39 -07:00
|
|
|
found: F,
|
|
|
|
bail_early: F2)
|
2019-03-09 08:23:35 +01:00
|
|
|
-> Result<HirId, HirId>
|
2018-08-25 15:56:16 +01:00
|
|
|
where F: Fn(&Node<'hir>) -> bool, F2: Fn(&Node<'hir>) -> bool
|
2015-07-01 08:58:48 +12:00
|
|
|
{
|
|
|
|
let mut id = start_id;
|
2015-06-25 15:48:59 -07:00
|
|
|
loop {
|
2019-03-09 08:23:35 +01:00
|
|
|
let parent_node = self.get_parent_node_by_hir_id(id);
|
|
|
|
if parent_node == CRATE_HIR_ID {
|
|
|
|
return Ok(CRATE_HIR_ID);
|
2015-06-25 15:48:59 -07:00
|
|
|
}
|
|
|
|
if parent_node == id {
|
2015-07-01 08:58:48 +12:00
|
|
|
return Err(id);
|
2015-06-25 15:48:59 -07:00
|
|
|
}
|
|
|
|
|
2018-08-25 23:10:01 +01:00
|
|
|
if let Some(entry) = self.find_entry(parent_node) {
|
2018-08-26 00:20:24 +01:00
|
|
|
if let Node::Crate = entry.node {
|
|
|
|
return Err(id);
|
|
|
|
}
|
2018-08-25 23:10:01 +01:00
|
|
|
if found(&entry.node) {
|
|
|
|
return Ok(parent_node);
|
|
|
|
} else if bail_early(&entry.node) {
|
|
|
|
return Err(parent_node);
|
2015-07-01 08:58:48 +12:00
|
|
|
}
|
2018-08-25 15:48:42 +01:00
|
|
|
id = parent_node;
|
|
|
|
} else {
|
|
|
|
return Err(id);
|
2015-06-25 15:48:59 -07:00
|
|
|
}
|
|
|
|
}
|
2014-02-14 07:07:09 +02:00
|
|
|
}
|
|
|
|
|
2019-02-08 14:53:55 +01:00
|
|
|
/// Retrieves the `NodeId` 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 {
|
|
|
|
/// true // `get_return_block` gets passed the `id` corresponding
|
|
|
|
/// } else { // to this, it will return `foo`'s `NodeId`.
|
|
|
|
/// false
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// fn foo(x: usize) -> bool {
|
|
|
|
/// loop {
|
|
|
|
/// true // `get_return_block` gets passed the `id` corresponding
|
|
|
|
/// } // 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> {
|
2018-08-29 22:02:42 -07:00
|
|
|
let match_fn = |node: &Node<'_>| {
|
2017-06-24 00:57:39 -07:00
|
|
|
match *node {
|
2018-08-25 15:56:16 +01:00
|
|
|
Node::Item(_) |
|
|
|
|
Node::ForeignItem(_) |
|
|
|
|
Node::TraitItem(_) |
|
2018-12-30 13:55:00 -08:00
|
|
|
Node::Expr(Expr { node: ExprKind::Closure(..), ..}) |
|
2018-08-25 15:56:16 +01:00
|
|
|
Node::ImplItem(_) => true,
|
2017-06-24 00:57:39 -07:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
};
|
2018-08-29 22:02:42 -07:00
|
|
|
let match_non_returning_block = |node: &Node<'_>| {
|
2017-06-24 00:57:39 -07:00
|
|
|
match *node {
|
2018-08-25 15:56:16 +01:00
|
|
|
Node::Expr(ref expr) => {
|
2017-06-24 00:57:39 -07:00
|
|
|
match expr.node {
|
2018-12-30 13:55:00 -08:00
|
|
|
ExprKind::While(..) | ExprKind::Loop(..) | ExprKind::Ret(..) => true,
|
2017-06-24 00:57:39 -07:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-03-09 08:23:35 +01:00
|
|
|
self.walk_parent_nodes(id, match_fn, match_non_returning_block).ok()
|
2017-06-24 00:57:39 -07:00
|
|
|
}
|
|
|
|
|
2019-02-08 14:53:55 +01:00
|
|
|
/// Retrieves the `NodeId` 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.
|
|
|
|
pub fn get_parent(&self, id: NodeId) -> NodeId {
|
2019-03-09 08:23:35 +01:00
|
|
|
let hir_id = self.node_to_hir_id(id);
|
|
|
|
let parent_hid = match self.walk_parent_nodes(hir_id, |node| match *node {
|
2018-08-25 15:56:16 +01:00
|
|
|
Node::Item(_) |
|
|
|
|
Node::ForeignItem(_) |
|
|
|
|
Node::TraitItem(_) |
|
|
|
|
Node::ImplItem(_) => true,
|
2015-07-01 08:58:48 +12:00
|
|
|
_ => false,
|
2017-06-24 00:57:39 -07:00
|
|
|
}, |_| false) {
|
2015-07-01 08:58:48 +12:00
|
|
|
Ok(id) => id,
|
|
|
|
Err(id) => id,
|
2019-03-09 08:23:35 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
self.hir_to_node_id(parent_hid)
|
2015-06-16 21:56:33 +12:00
|
|
|
}
|
|
|
|
|
2019-02-03 09:14:31 +01:00
|
|
|
// FIXME(@ljedrz): replace the NodeId variant
|
|
|
|
pub fn get_parent_item(&self, id: HirId) -> HirId {
|
|
|
|
let node_id = self.hir_to_node_id(id);
|
|
|
|
let parent_node_id = self.get_parent(node_id);
|
|
|
|
self.node_to_hir_id(parent_node_id)
|
|
|
|
}
|
|
|
|
|
2018-11-27 02:59:49 +00:00
|
|
|
/// Returns the `DefId` of `id`'s nearest module parent, or `id` itself if no
|
2016-02-25 01:55:54 +00:00
|
|
|
/// module parent is in this map.
|
2017-03-24 23:03:15 +00:00
|
|
|
pub fn get_module_parent(&self, id: NodeId) -> DefId {
|
2018-09-19 09:28:49 -05:00
|
|
|
self.local_def_id(self.get_module_parent_node(id))
|
|
|
|
}
|
|
|
|
|
2019-02-04 20:01:14 +01:00
|
|
|
// FIXME(@ljedrz): replace the NodeId variant
|
|
|
|
pub fn get_module_parent_by_hir_id(&self, id: HirId) -> DefId {
|
|
|
|
let node_id = self.hir_to_node_id(id);
|
|
|
|
self.get_module_parent(node_id)
|
|
|
|
}
|
|
|
|
|
2018-11-27 02:59:49 +00:00
|
|
|
/// Returns the `NodeId` 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.
|
|
|
|
pub fn get_module_parent_node(&self, id: NodeId) -> NodeId {
|
2019-03-09 08:23:35 +01:00
|
|
|
let hir_id = self.node_to_hir_id(id);
|
|
|
|
let parent_hid = match self.walk_parent_nodes(hir_id, |node| match *node {
|
2018-08-25 15:56:16 +01:00
|
|
|
Node::Item(&Item { node: ItemKind::Mod(_), .. }) => true,
|
2016-02-25 01:55:54 +00:00
|
|
|
_ => false,
|
2017-06-24 00:57:39 -07:00
|
|
|
}, |_| false) {
|
2016-02-25 01:55:54 +00:00
|
|
|
Ok(id) => id,
|
|
|
|
Err(id) => id,
|
2019-03-09 08:23:35 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
self.hir_to_node_id(parent_hid)
|
2016-02-25 01:55:54 +00:00
|
|
|
}
|
|
|
|
|
2015-06-16 22:20:30 +12:00
|
|
|
/// Returns the nearest enclosing scope. A scope is an item or block.
|
2018-11-27 02:59:49 +00:00
|
|
|
/// FIXME: it is not clear to me that all items qualify as scopes -- statics
|
2017-08-15 21:45:21 +02:00
|
|
|
/// and associated types probably shouldn't, for example. Behavior in this
|
2015-06-16 22:20:30 +12:00
|
|
|
/// regard should be expected to be highly unstable.
|
|
|
|
pub fn get_enclosing_scope(&self, id: NodeId) -> Option<NodeId> {
|
2019-03-09 08:23:35 +01:00
|
|
|
let hir_id = self.node_to_hir_id(id);
|
|
|
|
let parent_hid = self.walk_parent_nodes(hir_id, |node| match *node {
|
2018-08-25 15:56:16 +01:00
|
|
|
Node::Item(_) |
|
|
|
|
Node::ForeignItem(_) |
|
|
|
|
Node::TraitItem(_) |
|
|
|
|
Node::ImplItem(_) |
|
|
|
|
Node::Block(_) => true,
|
2015-07-01 08:58:48 +12:00
|
|
|
_ => false,
|
2019-03-09 08:23:35 +01:00
|
|
|
}, |_| false).ok();
|
|
|
|
|
|
|
|
parent_hid.map(|hid| self.hir_to_node_id(hid))
|
2015-06-16 22:20:30 +12:00
|
|
|
}
|
|
|
|
|
2014-02-14 07:07:09 +02:00
|
|
|
pub fn get_parent_did(&self, id: NodeId) -> DefId {
|
2017-01-04 04:01:58 +02:00
|
|
|
self.local_def_id(self.get_parent(id))
|
2014-02-14 07:07:09 +02:00
|
|
|
}
|
|
|
|
|
2019-02-04 11:09:32 +01:00
|
|
|
// FIXME(@ljedrz): replace the NodeId variant
|
|
|
|
pub fn get_parent_did_by_hir_id(&self, id: HirId) -> DefId {
|
|
|
|
let node_id = self.hir_to_node_id(id);
|
|
|
|
self.get_parent_did(node_id)
|
|
|
|
}
|
|
|
|
|
2016-02-05 13:13:36 +01:00
|
|
|
pub fn get_foreign_abi(&self, id: NodeId) -> Abi {
|
2019-03-09 08:23:35 +01:00
|
|
|
let hir_id = self.node_to_hir_id(id);
|
|
|
|
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 {
|
|
|
|
node: Node::Item(Item { node: ItemKind::ForeignMod(ref nm), .. }), .. } = entry
|
|
|
|
{
|
2019-03-09 08:23:35 +01:00
|
|
|
self.read_by_hir_id(hir_id); // reveals some of the content of a node
|
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-03-09 08:23:35 +01:00
|
|
|
bug!("expected foreign mod or inlined parent, found {}", self.hir_to_string(parent))
|
2014-02-14 07:07:09 +02:00
|
|
|
}
|
|
|
|
|
2019-02-04 11:09:32 +01:00
|
|
|
// FIXME(@ljedrz): replace the NodeId variant
|
|
|
|
pub fn get_foreign_abi_by_hir_id(&self, id: HirId) -> Abi {
|
|
|
|
let node_id = self.hir_to_node_id(id);
|
|
|
|
self.get_foreign_abi(node_id)
|
|
|
|
}
|
|
|
|
|
2017-01-26 03:21:50 +02:00
|
|
|
pub fn expect_item(&self, id: NodeId) -> &'hir Item {
|
2016-02-05 13:19:21 -05:00
|
|
|
match self.find(id) { // read recorded by `find`
|
2018-08-25 15:56:16 +01:00
|
|
|
Some(Node::Item(item)) => item,
|
2016-03-26 19:59:04 +01:00
|
|
|
_ => bug!("expected item, found {}", self.node_to_string(id))
|
2014-02-14 07:07:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-03 09:14:31 +01:00
|
|
|
// FIXME(@ljedrz): replace the NodeId variant
|
|
|
|
pub fn expect_item_by_hir_id(&self, id: HirId) -> &'hir Item {
|
2019-03-07 15:46:41 +01:00
|
|
|
match self.find_by_hir_id(id) { // read recorded by `find`
|
|
|
|
Some(Node::Item(item)) => item,
|
|
|
|
_ => bug!("expected item, found {}", self.hir_to_string(id))
|
|
|
|
}
|
2019-02-03 09:14:31 +01:00
|
|
|
}
|
|
|
|
|
2019-03-07 15:46:41 +01:00
|
|
|
pub fn expect_impl_item(&self, id: HirId) -> &'hir ImplItem {
|
|
|
|
match self.find_by_hir_id(id) {
|
2018-08-25 15:56:16 +01:00
|
|
|
Some(Node::ImplItem(item)) => item,
|
2019-03-07 15:46:41 +01:00
|
|
|
_ => bug!("expected impl item, found {}", self.hir_to_string(id))
|
2016-08-10 01:43:12 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-07 15:46:41 +01:00
|
|
|
pub fn expect_trait_item(&self, id: HirId) -> &'hir TraitItem {
|
|
|
|
match self.find_by_hir_id(id) {
|
2018-08-25 15:56:16 +01:00
|
|
|
Some(Node::TraitItem(item)) => item,
|
2019-03-07 15:46:41 +01:00
|
|
|
_ => bug!("expected trait item, found {}", self.hir_to_string(id))
|
2015-07-14 22:03:14 +12:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-04 16:45:04 +01:00
|
|
|
pub fn expect_variant_data(&self, id: HirId) -> &'hir VariantData {
|
2019-03-07 15:46:41 +01:00
|
|
|
match self.find_by_hir_id(id) {
|
2018-08-25 15:56:16 +01:00
|
|
|
Some(Node::Item(i)) => {
|
2014-04-03 13:38:45 +13:00
|
|
|
match i.node {
|
2018-07-11 23:36:06 +08:00
|
|
|
ItemKind::Struct(ref struct_def, _) |
|
|
|
|
ItemKind::Union(ref struct_def, _) => struct_def,
|
2019-03-07 15:46:41 +01:00
|
|
|
_ => bug!("struct ID bound to non-struct {}", self.hir_to_string(id))
|
2014-04-03 13:38:45 +13:00
|
|
|
}
|
|
|
|
}
|
2018-08-25 15:56:16 +01:00
|
|
|
Some(Node::Variant(variant)) => &variant.node.data,
|
2019-03-24 18:21:59 +01:00
|
|
|
Some(Node::Ctor(data)) => data,
|
2019-03-07 15:46:41 +01:00
|
|
|
_ => bug!("expected struct or variant, found {}", self.hir_to_string(id))
|
2014-04-03 13:38:45 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-04 16:45:04 +01:00
|
|
|
pub fn expect_variant(&self, id: HirId) -> &'hir Variant {
|
2019-03-07 15:46:41 +01:00
|
|
|
match self.find_by_hir_id(id) {
|
2018-08-25 15:56:16 +01:00
|
|
|
Some(Node::Variant(variant)) => variant,
|
2019-03-07 15:46:41 +01:00
|
|
|
_ => bug!("expected variant, found {}", self.hir_to_string(id)),
|
2014-04-08 21:00:20 +12:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-07 15:46:41 +01:00
|
|
|
pub fn expect_foreign_item(&self, id: HirId) -> &'hir ForeignItem {
|
|
|
|
match self.find_by_hir_id(id) {
|
2018-08-25 15:56:16 +01:00
|
|
|
Some(Node::ForeignItem(item)) => item,
|
2019-03-07 15:46:41 +01:00
|
|
|
_ => bug!("expected foreign item, found {}", self.hir_to_string(id))
|
2014-02-14 07:07:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-26 03:21:50 +02:00
|
|
|
pub fn expect_expr(&self, id: NodeId) -> &'hir Expr {
|
2016-01-29 15:07:04 -05:00
|
|
|
match self.find(id) { // read recorded by find
|
2018-08-25 15:56:16 +01:00
|
|
|
Some(Node::Expr(expr)) => expr,
|
2016-03-26 19:59:04 +01:00
|
|
|
_ => bug!("expected expr, found {}", self.node_to_string(id))
|
2014-05-18 02:38:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-04 11:09:32 +01:00
|
|
|
// FIXME(@ljedrz): replace the NodeId variant
|
|
|
|
pub fn expect_expr_by_hir_id(&self, id: HirId) -> &'hir Expr {
|
|
|
|
let node_id = self.hir_to_node_id(id);
|
|
|
|
self.expect_expr(node_id)
|
|
|
|
}
|
|
|
|
|
2016-04-06 13:51:55 +03:00
|
|
|
/// Returns the name associated with the given NodeId's AST.
|
|
|
|
pub fn name(&self, id: NodeId) -> Name {
|
|
|
|
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,
|
2018-12-01 02:47:08 +00:00
|
|
|
Node::Variant(v) => v.node.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-03-07 12:18:59 +01:00
|
|
|
Node::Binding(&Pat { node: PatKind::Binding(_, _, l, _), .. }) => l.name,
|
2019-03-21 23:38:50 +01:00
|
|
|
Node::Ctor(..) => self.name(self.get_parent(id)),
|
2016-04-06 13:51:55 +03:00
|
|
|
_ => bug!("no name for {}", self.node_to_string(id))
|
2014-02-14 07:07:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-04 11:09:32 +01:00
|
|
|
// FIXME(@ljedrz): replace the NodeId variant
|
|
|
|
pub fn name_by_hir_id(&self, id: HirId) -> Name {
|
|
|
|
let node_id = self.hir_to_node_id(id);
|
|
|
|
self.name(node_id)
|
|
|
|
}
|
|
|
|
|
2015-10-07 23:11:25 +01:00
|
|
|
/// Given a node ID, get a list of attributes associated with the AST
|
2018-08-25 15:56:16 +01:00
|
|
|
/// corresponding to the Node ID
|
2017-01-26 03:21:50 +02:00
|
|
|
pub fn attrs(&self, id: NodeId) -> &'hir [ast::Attribute] {
|
2016-01-29 15:07:04 -05:00
|
|
|
self.read(id); // reveals attributes on the node
|
2019-03-03 18:47:54 +01:00
|
|
|
let attrs = match self.find_entry(id).map(|entry| entry.node) {
|
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[..]),
|
|
|
|
Some(Node::Variant(ref v)) => Some(&v.node.attrs[..]),
|
|
|
|
Some(Node::Field(ref f)) => Some(&f.attrs[..]),
|
|
|
|
Some(Node::Expr(ref e)) => Some(&*e.attrs),
|
|
|
|
Some(Node::Stmt(ref s)) => Some(s.node.attrs()),
|
|
|
|
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.
|
|
|
|
Some(Node::Ctor(..)) => return self.attrs(self.get_parent(id)),
|
2019-03-03 18:47:54 +01:00
|
|
|
Some(Node::Crate) => Some(&self.forest.krate.attrs[..]),
|
2014-02-14 07:07:09 +02:00
|
|
|
_ => None
|
|
|
|
};
|
2015-03-06 18:06:05 +01:00
|
|
|
attrs.unwrap_or(&[])
|
2014-02-14 07:07:09 +02:00
|
|
|
}
|
|
|
|
|
2019-02-04 11:09:32 +01:00
|
|
|
// FIXME(@ljedrz): replace the NodeId variant
|
|
|
|
pub fn attrs_by_hir_id(&self, id: HirId) -> &'hir [ast::Attribute] {
|
|
|
|
let node_id = self.hir_to_node_id(id);
|
|
|
|
self.attrs(node_id)
|
|
|
|
}
|
|
|
|
|
2014-08-07 11:22:42 +02:00
|
|
|
/// Returns an iterator that yields the node id's with paths that
|
|
|
|
/// match `parts`. (Requires `parts` is non-empty.)
|
|
|
|
///
|
|
|
|
/// For example, if given `parts` equal to `["bar", "quux"]`, then
|
|
|
|
/// the iterator will produce node id's for items with paths
|
|
|
|
/// such as `foo::bar::quux`, `bar::quux`, `other::bar::quux`, and
|
|
|
|
/// any other such items it can find in the map.
|
2014-12-10 19:46:38 -08:00
|
|
|
pub fn nodes_matching_suffix<'a>(&'a self, parts: &'a [String])
|
2017-01-26 03:21:50 +02:00
|
|
|
-> NodesMatchingSuffix<'a, 'hir> {
|
2014-08-11 09:32:26 -07:00
|
|
|
NodesMatchingSuffix {
|
|
|
|
map: self,
|
|
|
|
item_name: parts.last().unwrap(),
|
2015-01-19 11:07:13 -05:00
|
|
|
in_which: &parts[..parts.len() - 1],
|
2019-03-09 08:23:35 +01:00
|
|
|
idx: ast::CRATE_NODE_ID,
|
2014-08-11 09:32:26 -07:00
|
|
|
}
|
2014-08-07 11:22:42 +02:00
|
|
|
}
|
|
|
|
|
2014-05-21 22:21:11 +10:00
|
|
|
pub fn span(&self, id: NodeId) -> Span {
|
2019-03-09 08:23:35 +01:00
|
|
|
let hir_id = self.node_to_hir_id(id);
|
|
|
|
self.read_by_hir_id(hir_id); // reveals span from node
|
|
|
|
match self.find_entry(hir_id).map(|entry| entry.node) {
|
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,
|
|
|
|
Some(Node::Block(block)) => block.span,
|
2019-03-24 18:21:59 +01:00
|
|
|
Some(Node::Ctor(..)) => match self.find(self.get_parent_node(id)) {
|
|
|
|
Some(Node::Item(item)) => item.span,
|
|
|
|
Some(Node::Variant(variant)) => variant.span,
|
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
2018-08-25 15:56:16 +01:00
|
|
|
Some(Node::Lifetime(lifetime)) => lifetime.span,
|
|
|
|
Some(Node::GenericParam(param)) => param.span,
|
|
|
|
Some(Node::Visibility(&Spanned {
|
2018-07-01 11:05:10 -07: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,
|
|
|
|
Some(Node::Crate) => self.forest.krate.span,
|
2018-08-25 15:48:42 +01:00
|
|
|
None => bug!("hir::map::Map::span: id not in map: {:?}", id),
|
2016-11-24 01:39:13 +02:00
|
|
|
}
|
2014-02-14 07:07:09 +02:00
|
|
|
}
|
|
|
|
|
2019-02-04 11:09:32 +01:00
|
|
|
// FIXME(@ljedrz): replace the NodeId variant
|
|
|
|
pub fn span_by_hir_id(&self, id: HirId) -> Span {
|
|
|
|
let node_id = self.hir_to_node_id(id);
|
|
|
|
self.span(node_id)
|
|
|
|
}
|
|
|
|
|
2015-09-04 13:52:28 -04:00
|
|
|
pub fn span_if_local(&self, id: DefId) -> Option<Span> {
|
|
|
|
self.as_local_node_id(id).map(|id| self.span(id))
|
|
|
|
}
|
|
|
|
|
2014-06-21 03:39:03 -07:00
|
|
|
pub fn node_to_string(&self, id: NodeId) -> String {
|
2014-09-19 15:38:53 +02:00
|
|
|
node_id_to_string(self, id, true)
|
|
|
|
}
|
|
|
|
|
2019-02-03 09:14:31 +01:00
|
|
|
// FIXME(@ljedrz): replace the NodeId variant
|
|
|
|
pub fn hir_to_string(&self, id: HirId) -> String {
|
|
|
|
hir_id_to_string(self, id, true)
|
|
|
|
}
|
|
|
|
|
2014-09-19 15:38:53 +02:00
|
|
|
pub fn node_to_user_string(&self, id: NodeId) -> String {
|
|
|
|
node_id_to_string(self, id, false)
|
2014-01-17 23:23:09 +11:00
|
|
|
}
|
2016-12-27 10:00:18 +02:00
|
|
|
|
2019-02-03 09:14:31 +01:00
|
|
|
// FIXME(@ljedrz): replace the NodeId variant
|
|
|
|
pub fn hir_to_user_string(&self, id: HirId) -> String {
|
|
|
|
hir_id_to_string(self, id, false)
|
|
|
|
}
|
|
|
|
|
2016-12-27 10:00:18 +02:00
|
|
|
pub fn node_to_pretty_string(&self, id: NodeId) -> String {
|
|
|
|
print::to_string(self, |s| s.print_node(self.get(id)))
|
|
|
|
}
|
2019-02-03 09:14:31 +01:00
|
|
|
|
|
|
|
// FIXME(@ljedrz): replace the NodeId variant
|
|
|
|
pub fn hir_to_pretty_string(&self, id: HirId) -> String {
|
|
|
|
print::to_string(self, |s| s.print_node(self.get_by_hir_id(id)))
|
|
|
|
}
|
2014-01-17 23:23:09 +11:00
|
|
|
}
|
2013-02-04 14:02:01 -08:00
|
|
|
|
2017-01-26 03:21:50 +02:00
|
|
|
pub struct NodesMatchingSuffix<'a, 'hir:'a> {
|
|
|
|
map: &'a Map<'hir>,
|
2014-12-10 19:46:38 -08:00
|
|
|
item_name: &'a String,
|
|
|
|
in_which: &'a [String],
|
2014-08-27 21:46:52 -04:00
|
|
|
idx: NodeId,
|
|
|
|
}
|
|
|
|
|
2017-01-26 03:21:50 +02:00
|
|
|
impl<'a, 'hir> NodesMatchingSuffix<'a, 'hir> {
|
2019-02-08 14:53:55 +01:00
|
|
|
/// Returns `true` only if some suffix of the module path for parent
|
2014-08-11 09:32:26 -07:00
|
|
|
/// matches `self.in_which`.
|
2014-08-07 11:22:42 +02:00
|
|
|
///
|
2014-08-11 09:32:26 -07:00
|
|
|
/// In other words: let `[x_0,x_1,...,x_k]` be `self.in_which`;
|
2014-08-07 11:22:42 +02:00
|
|
|
/// returns true if parent's path ends with the suffix
|
|
|
|
/// `x_0::x_1::...::x_k`.
|
|
|
|
fn suffix_matches(&self, parent: NodeId) -> bool {
|
|
|
|
let mut cursor = parent;
|
2014-08-11 09:32:26 -07:00
|
|
|
for part in self.in_which.iter().rev() {
|
2014-08-07 11:22:42 +02:00
|
|
|
let (mod_id, mod_name) = match find_first_mod_parent(self.map, cursor) {
|
|
|
|
None => return false,
|
|
|
|
Some((node_id, name)) => (node_id, name),
|
|
|
|
};
|
2016-11-17 14:04:20 +00:00
|
|
|
if mod_name != &**part {
|
2014-08-07 11:22:42 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
cursor = self.map.get_parent(mod_id);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Finds the first mod in parent chain for `id`, along with
|
|
|
|
// that mod's name.
|
|
|
|
//
|
|
|
|
// If `id` itself is a mod named `m` with parent `p`, then
|
|
|
|
// returns `Some(id, m, p)`. If `id` has no mod in its parent
|
|
|
|
// chain, then returns `None`.
|
2018-08-29 22:02:42 -07:00
|
|
|
fn find_first_mod_parent<'a>(map: &'a Map<'_>, mut id: NodeId) -> Option<(NodeId, Name)> {
|
2014-08-07 11:22:42 +02:00
|
|
|
loop {
|
2018-09-12 12:31:11 +02:00
|
|
|
if let Node::Item(item) = map.find(id)? {
|
|
|
|
if item_is_mod(&item) {
|
2018-12-01 02:47:08 +00:00
|
|
|
return Some((id, item.ident.name))
|
2018-09-12 12:31:11 +02:00
|
|
|
}
|
2014-08-07 11:22:42 +02:00
|
|
|
}
|
|
|
|
let parent = map.get_parent(id);
|
|
|
|
if parent == id { return None }
|
|
|
|
id = parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn item_is_mod(item: &Item) -> bool {
|
|
|
|
match item.node {
|
2018-07-11 23:36:06 +08:00
|
|
|
ItemKind::Mod(_) => true,
|
2014-08-07 11:22:42 +02:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We are looking at some node `n` with a given name and parent
|
|
|
|
// id; do their names match what I am seeking?
|
|
|
|
fn matches_names(&self, parent_of_n: NodeId, name: Name) -> bool {
|
2016-11-17 14:04:20 +00:00
|
|
|
name == &**self.item_name && self.suffix_matches(parent_of_n)
|
2014-08-07 11:22:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-26 03:21:50 +02:00
|
|
|
impl<'a, 'hir> Iterator for NodesMatchingSuffix<'a, 'hir> {
|
2015-01-01 22:55:09 -05:00
|
|
|
type Item = NodeId;
|
|
|
|
|
2014-08-07 11:22:42 +02:00
|
|
|
fn next(&mut self) -> Option<NodeId> {
|
|
|
|
loop {
|
|
|
|
let idx = self.idx;
|
2016-08-31 14:00:29 +03:00
|
|
|
if idx.as_usize() >= self.map.entry_count() {
|
2014-08-07 11:22:42 +02:00
|
|
|
return None;
|
|
|
|
}
|
2016-08-31 14:00:29 +03:00
|
|
|
self.idx = NodeId::from_u32(self.idx.as_u32() + 1);
|
2019-03-09 08:23:35 +01:00
|
|
|
let hir_idx = self.map.node_to_hir_id(idx);
|
|
|
|
let name = match self.map.find_entry(hir_idx).map(|entry| entry.node) {
|
2018-08-25 15:56:16 +01:00
|
|
|
Some(Node::Item(n)) => n.name(),
|
|
|
|
Some(Node::ForeignItem(n)) => n.name(),
|
|
|
|
Some(Node::TraitItem(n)) => n.name(),
|
|
|
|
Some(Node::ImplItem(n)) => n.name(),
|
|
|
|
Some(Node::Variant(n)) => n.name(),
|
|
|
|
Some(Node::Field(n)) => n.name(),
|
2014-08-07 11:22:42 +02:00
|
|
|
_ => continue,
|
|
|
|
};
|
2015-06-25 15:48:59 -07:00
|
|
|
if self.matches_names(self.map.get_parent(idx), name) {
|
2014-08-07 11:22:42 +02:00
|
|
|
return Some(idx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
trait Named {
|
|
|
|
fn name(&self) -> Name;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T:Named> Named for Spanned<T> { fn name(&self) -> Name { self.node.name() } }
|
|
|
|
|
2018-12-01 02:47:08 +00: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 VariantKind { fn name(&self) -> Name { self.ident.name } }
|
2018-05-26 02:50:15 +03:00
|
|
|
impl Named for StructField { fn name(&self) -> Name { self.ident.name } }
|
2018-06-10 22:24:24 +03:00
|
|
|
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-02-05 11:20:45 -06:00
|
|
|
pub fn map_crate<'hir>(sess: &crate::session::Session,
|
2018-06-06 22:13:52 +02:00
|
|
|
cstore: &CrateStoreDyn,
|
|
|
|
forest: &'hir Forest,
|
2017-09-14 15:10:24 +02:00
|
|
|
definitions: &'hir Definitions)
|
2017-01-26 03:21:50 +02:00
|
|
|
-> Map<'hir> {
|
2019-02-15 10:30:44 +01:00
|
|
|
// Build the reverse mapping of `node_to_hir_id`.
|
|
|
|
let hir_to_node_id = definitions.node_to_hir_id.iter_enumerated()
|
|
|
|
.map(|(node_id, &hir_id)| (hir_id, node_id)).collect();
|
|
|
|
|
|
|
|
let (map, crate_hash) = {
|
2019-02-05 11:20:45 -06:00
|
|
|
let hcx = crate::ich::StableHashingContext::new(sess, &forest.krate, definitions, cstore);
|
2017-09-14 17:43:03 +02:00
|
|
|
|
2018-06-06 22:13:52 +02:00
|
|
|
let mut collector = NodeCollector::root(sess,
|
|
|
|
&forest.krate,
|
2017-08-18 20:24:19 +02:00
|
|
|
&forest.dep_graph,
|
2017-09-14 17:43:03 +02:00
|
|
|
&definitions,
|
2019-02-15 10:30:44 +01:00
|
|
|
&hir_to_node_id,
|
2018-06-06 22:13:52 +02:00
|
|
|
hcx);
|
2017-08-18 20:24:19 +02:00
|
|
|
intravisit::walk_crate(&mut collector, &forest.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();
|
2018-06-06 22:13:52 +02: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
|
|
|
|
2017-03-14 15:50:40 +01:00
|
|
|
let map = Map {
|
2017-07-03 11:19:51 -07:00
|
|
|
forest,
|
2016-01-29 15:07:04 -05:00
|
|
|
dep_graph: forest.dep_graph.clone(),
|
2017-12-19 18:01:19 +01:00
|
|
|
crate_hash,
|
2017-07-03 11:19:51 -07:00
|
|
|
map,
|
2017-08-29 19:27:30 +03:00
|
|
|
hir_to_node_id,
|
2017-07-03 11:19:51 -07:00
|
|
|
definitions,
|
2017-03-14 15:50:40 +01:00
|
|
|
};
|
|
|
|
|
2018-06-06 22:13:52 +02:00
|
|
|
time(sess, "validate hir map", || {
|
|
|
|
hir_id_validator::check_crate(&map);
|
|
|
|
});
|
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> {
|
2018-08-29 22:02:42 -07:00
|
|
|
fn nested(&self, state: &mut print::State<'_>, nested: print::Nested) -> io::Result<()> {
|
2016-12-27 10:00:18 +02:00
|
|
|
match nested {
|
2019-03-11 09:44:19 +01:00
|
|
|
Nested::Item(id) => state.print_item(self.expect_item_by_hir_id(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),
|
|
|
|
Nested::BodyArgPat(id, i) => state.print_pat(&self.body(id).arguments[i].pat)
|
|
|
|
}
|
|
|
|
}
|
2014-08-07 14:21:15 +02:00
|
|
|
}
|
|
|
|
|
2016-12-27 10:00:18 +02:00
|
|
|
impl<'a> print::State<'a> {
|
2018-08-29 22:02:42 -07:00
|
|
|
pub fn print_node(&mut self, node: Node<'_>) -> io::Result<()> {
|
2016-12-27 10:00:18 +02:00
|
|
|
match node {
|
2018-08-25 15:56:16 +01:00
|
|
|
Node::Item(a) => self.print_item(&a),
|
|
|
|
Node::ForeignItem(a) => self.print_foreign_item(&a),
|
|
|
|
Node::TraitItem(a) => self.print_trait_item(a),
|
|
|
|
Node::ImplItem(a) => self.print_impl_item(a),
|
|
|
|
Node::Variant(a) => self.print_variant(&a),
|
|
|
|
Node::AnonConst(a) => self.print_anon_const(&a),
|
|
|
|
Node::Expr(a) => self.print_expr(&a),
|
|
|
|
Node::Stmt(a) => self.print_stmt(&a),
|
2018-10-11 21:15:18 +13:00
|
|
|
Node::PathSegment(a) => self.print_path_segment(&a),
|
2018-08-25 15:56:16 +01:00
|
|
|
Node::Ty(a) => self.print_type(&a),
|
|
|
|
Node::TraitRef(a) => self.print_trait_ref(&a),
|
|
|
|
Node::Binding(a) |
|
|
|
|
Node::Pat(a) => self.print_pat(&a),
|
|
|
|
Node::Block(a) => {
|
2016-12-27 10:00:18 +02:00
|
|
|
use syntax::print::pprust::PrintState;
|
|
|
|
|
|
|
|
// containing cbox, will be closed by print-block at }
|
|
|
|
self.cbox(print::indent_unit)?;
|
|
|
|
// head-ibox, will be closed by print-block after {
|
|
|
|
self.ibox(0)?;
|
|
|
|
self.print_block(&a)
|
|
|
|
}
|
2018-08-25 15:56:16 +01:00
|
|
|
Node::Lifetime(a) => self.print_lifetime(&a),
|
|
|
|
Node::Visibility(a) => self.print_visibility(&a),
|
|
|
|
Node::GenericParam(_) => bug!("cannot print Node::GenericParam"),
|
|
|
|
Node::Field(_) => bug!("cannot print StructField"),
|
2014-08-07 14:21:15 +02:00
|
|
|
// these cases do not carry enough information in the
|
2017-01-26 03:21:50 +02:00
|
|
|
// hir_map to reconstruct their full structure for pretty
|
2014-08-07 14:21:15 +02:00
|
|
|
// printing.
|
2019-03-21 23:38:50 +01:00
|
|
|
Node::Ctor(..) => bug!("cannot print isolated Ctor"),
|
2018-08-25 15:56:16 +01:00
|
|
|
Node::Local(a) => self.print_local_decl(&a),
|
|
|
|
Node::MacroDef(_) => bug!("cannot print MacroDef"),
|
2018-09-12 12:31:11 +02:00
|
|
|
Node::Crate => bug!("cannot print Crate"),
|
2014-08-07 14:21:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-29 22:02:42 -07:00
|
|
|
fn node_id_to_string(map: &Map<'_>, id: NodeId, include_id: bool) -> String {
|
2014-09-19 15:38:53 +02:00
|
|
|
let id_str = format!(" (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 = || {
|
|
|
|
// 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 {
|
2018-12-28 10:08:30 +02:00
|
|
|
let def_id = map.local_def_id(id);
|
|
|
|
tcx.def_path_str(def_id)
|
2016-04-06 13:51:55 +03:00
|
|
|
} else if let Some(path) = map.def_path_from_id(id) {
|
|
|
|
path.data.into_iter().map(|elem| {
|
|
|
|
elem.data.to_string()
|
|
|
|
}).collect::<Vec<_>>().join("::")
|
|
|
|
} else {
|
|
|
|
String::from("<missing path>")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
2014-01-17 23:23:09 +11:00
|
|
|
match map.find(id) {
|
2018-08-25 15:56:16 +01:00
|
|
|
Some(Node::Item(item)) => {
|
2014-02-14 07:07:09 +02:00
|
|
|
let item_str = match item.node {
|
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",
|
|
|
|
ItemKind::Ty(..) => "ty",
|
2018-07-03 19:38:14 +02:00
|
|
|
ItemKind::Existential(..) => "existential 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",
|
|
|
|
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
|
|
|
}
|
2018-08-25 15:56:16 +01:00
|
|
|
Some(Node::ForeignItem(_)) => {
|
2016-04-06 13:51:55 +03:00
|
|
|
format!("foreign item {}{}", path_str(), id_str)
|
2014-02-14 07:07:09 +02:00
|
|
|
}
|
2018-08-25 15:56:16 +01:00
|
|
|
Some(Node::ImplItem(ii)) => {
|
2015-03-10 12:28:44 +02:00
|
|
|
match ii.node {
|
2015-11-12 15:57:51 +01:00
|
|
|
ImplItemKind::Const(..) => {
|
2018-06-10 22:24:24 +03:00
|
|
|
format!("assoc const {} in {}{}", ii.ident, path_str(), id_str)
|
2015-03-14 12:05:00 -06:00
|
|
|
}
|
2015-11-12 15:57:51 +01:00
|
|
|
ImplItemKind::Method(..) => {
|
2018-06-10 22:24:24 +03:00
|
|
|
format!("method {} in {}{}", ii.ident, path_str(), id_str)
|
2014-08-04 13:56:56 -07:00
|
|
|
}
|
2015-11-12 15:57:51 +01:00
|
|
|
ImplItemKind::Type(_) => {
|
2018-06-10 22:24:24 +03:00
|
|
|
format!("assoc type {} in {}{}", ii.ident, path_str(), id_str)
|
2014-08-05 19:44:21 -07:00
|
|
|
}
|
2018-07-03 19:38:14 +02:00
|
|
|
ImplItemKind::Existential(_) => {
|
|
|
|
format!("assoc existential type {} in {}{}", ii.ident, path_str(), id_str)
|
|
|
|
}
|
2014-08-04 13:56:56 -07:00
|
|
|
}
|
|
|
|
}
|
2018-08-25 15:56:16 +01:00
|
|
|
Some(Node::TraitItem(ti)) => {
|
2015-03-10 12:28:44 +02:00
|
|
|
let kind = match ti.node {
|
2016-12-04 04:21:06 +02:00
|
|
|
TraitItemKind::Const(..) => "assoc constant",
|
|
|
|
TraitItemKind::Method(..) => "trait method",
|
|
|
|
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)) => {
|
2014-09-19 15:38:53 +02:00
|
|
|
format!("variant {} in {}{}",
|
2018-12-01 02:47:08 +00:00
|
|
|
variant.node.ident,
|
2016-04-06 13:51:55 +03:00
|
|
|
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)) => {
|
2016-11-09 20:57:48 +02:00
|
|
|
format!("field {} in {}{}",
|
2018-05-26 02:50:15 +03:00
|
|
|
field.ident,
|
2016-11-09 20:57:48 +02:00
|
|
|
path_str(), id_str)
|
|
|
|
}
|
2018-08-25 15:56:16 +01:00
|
|
|
Some(Node::AnonConst(_)) => {
|
2018-05-17 21:28:50 +03:00
|
|
|
format!("const {}{}", map.node_to_pretty_string(id), id_str)
|
|
|
|
}
|
2018-08-25 15:56:16 +01:00
|
|
|
Some(Node::Expr(_)) => {
|
2016-12-27 10:00:18 +02:00
|
|
|
format!("expr {}{}", map.node_to_pretty_string(id), id_str)
|
2014-02-14 07:07:09 +02:00
|
|
|
}
|
2018-08-25 15:56:16 +01:00
|
|
|
Some(Node::Stmt(_)) => {
|
2016-12-27 10:00:18 +02:00
|
|
|
format!("stmt {}{}", map.node_to_pretty_string(id), id_str)
|
2014-02-14 07:07:09 +02:00
|
|
|
}
|
2018-09-11 15:08:47 +12:00
|
|
|
Some(Node::PathSegment(_)) => {
|
|
|
|
format!("path segment {}{}", map.node_to_pretty_string(id), id_str)
|
|
|
|
}
|
2018-08-25 15:56:16 +01:00
|
|
|
Some(Node::Ty(_)) => {
|
2016-12-27 10:00:18 +02:00
|
|
|
format!("type {}{}", map.node_to_pretty_string(id), id_str)
|
2016-08-06 03:12:20 +03:00
|
|
|
}
|
2018-08-25 15:56:16 +01:00
|
|
|
Some(Node::TraitRef(_)) => {
|
2016-12-27 10:00:18 +02:00
|
|
|
format!("trait_ref {}{}", map.node_to_pretty_string(id), id_str)
|
2016-10-30 08:04:52 +02:00
|
|
|
}
|
2018-08-25 15:56:16 +01:00
|
|
|
Some(Node::Binding(_)) => {
|
2016-12-27 10:00:18 +02:00
|
|
|
format!("local {}{}", map.node_to_pretty_string(id), id_str)
|
2014-02-14 07:07:09 +02:00
|
|
|
}
|
2018-08-25 15:56:16 +01:00
|
|
|
Some(Node::Pat(_)) => {
|
2016-12-27 10:00:18 +02:00
|
|
|
format!("pat {}{}", map.node_to_pretty_string(id), id_str)
|
2014-05-08 15:50:54 +02:00
|
|
|
}
|
2018-08-25 15:56:16 +01:00
|
|
|
Some(Node::Block(_)) => {
|
2016-12-27 10:00:18 +02:00
|
|
|
format!("block {}{}", map.node_to_pretty_string(id), id_str)
|
2014-02-14 07:07:09 +02:00
|
|
|
}
|
2018-08-25 15:56:16 +01:00
|
|
|
Some(Node::Local(_)) => {
|
2017-08-17 23:56:11 -07:00
|
|
|
format!("local {}{}", map.node_to_pretty_string(id), id_str)
|
|
|
|
}
|
2019-03-21 23:38:50 +01:00
|
|
|
Some(Node::Ctor(..)) => {
|
|
|
|
format!("ctor {}{}", path_str(), id_str)
|
2014-02-14 07:07:09 +02:00
|
|
|
}
|
2018-08-25 15:56:16 +01:00
|
|
|
Some(Node::Lifetime(_)) => {
|
2016-12-27 10:00:18 +02:00
|
|
|
format!("lifetime {}{}", map.node_to_pretty_string(id), id_str)
|
2014-04-11 11:28:43 +02:00
|
|
|
}
|
2018-08-25 15:56:16 +01:00
|
|
|
Some(Node::GenericParam(ref param)) => {
|
2018-05-28 14:05:06 +01:00
|
|
|
format!("generic_param {:?}{}", param, id_str)
|
2015-07-16 11:26:02 -07:00
|
|
|
}
|
2018-08-25 15:56:16 +01:00
|
|
|
Some(Node::Visibility(ref vis)) => {
|
2016-10-30 08:04:52 +02:00
|
|
|
format!("visibility {:?}{}", vis, id_str)
|
|
|
|
}
|
2018-08-25 15:56:16 +01:00
|
|
|
Some(Node::MacroDef(_)) => {
|
2017-09-14 17:43:03 +02:00
|
|
|
format!("macro {}{}", path_str(), id_str)
|
|
|
|
}
|
2018-10-31 00:10:10 +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-02-03 09:14:31 +01:00
|
|
|
// FIXME(@ljedrz): replace the NodeId variant
|
|
|
|
fn hir_id_to_string(map: &Map<'_>, id: HirId, include_id: bool) -> String {
|
|
|
|
let node_id = map.hir_to_node_id(id);
|
|
|
|
node_id_to_string(map, node_id, include_id)
|
|
|
|
}
|
|
|
|
|
2018-08-29 22:02:42 -07:00
|
|
|
pub fn describe_def(tcx: TyCtxt<'_, '_, '_>, def_id: DefId) -> Option<Def> {
|
2018-12-04 13:45:36 +01:00
|
|
|
if let Some(node_id) = tcx.hir().as_local_node_id(def_id) {
|
|
|
|
tcx.hir().describe_def(node_id)
|
2018-02-19 16:46:40 +01:00
|
|
|
} else {
|
|
|
|
bug!("Calling local describe_def query provider for upstream DefId: {:?}",
|
|
|
|
def_id)
|
|
|
|
}
|
|
|
|
}
|