2020-03-05 18:07:42 -03:00
|
|
|
//! HIR datatypes. See the [rustc dev guide] for more info.
|
2020-01-02 03:39:11 +01:00
|
|
|
//!
|
2020-03-09 18:33:04 -03:00
|
|
|
//! [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/hir.html
|
2020-01-02 03:39:11 +01:00
|
|
|
|
2020-01-02 04:53:12 +01:00
|
|
|
pub mod exports;
|
2020-01-02 03:39:11 +01:00
|
|
|
pub mod map;
|
2020-07-05 22:06:37 -04:00
|
|
|
pub mod place;
|
2020-01-02 03:39:11 +01:00
|
|
|
|
2020-02-09 12:13:08 +01:00
|
|
|
use crate::ich::StableHashingContext;
|
2020-01-02 03:39:11 +01:00
|
|
|
use crate::ty::query::Providers;
|
2020-02-06 11:59:29 +01:00
|
|
|
use crate::ty::TyCtxt;
|
2021-01-24 17:14:17 +01:00
|
|
|
use rustc_ast::Attribute;
|
2020-02-10 14:29:21 +01:00
|
|
|
use rustc_data_structures::fingerprint::Fingerprint;
|
2020-02-07 11:14:47 +01:00
|
|
|
use rustc_data_structures::fx::FxHashMap;
|
2020-02-09 12:13:08 +01:00
|
|
|
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
|
2021-05-11 11:42:01 +02:00
|
|
|
use rustc_hir::def_id::LocalDefId;
|
2020-06-11 13:42:22 -04:00
|
|
|
use rustc_hir::*;
|
2021-05-28 21:14:11 +02:00
|
|
|
use rustc_index::vec::{Idx, IndexVec};
|
2020-12-23 10:32:00 +01:00
|
|
|
use rustc_span::DUMMY_SP;
|
2021-01-24 17:14:17 +01:00
|
|
|
use std::collections::BTreeMap;
|
2020-02-06 11:59:29 +01:00
|
|
|
|
2021-05-28 21:14:11 +02:00
|
|
|
/// Result of HIR indexing.
|
2021-02-28 18:58:50 +01:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct IndexedHir<'hir> {
|
2021-05-28 21:14:11 +02:00
|
|
|
/// Contents of the HIR owned by each definition. None for definitions that are not HIR owners.
|
|
|
|
// The `mut` comes from construction time, and is harmless since we only ever hand out
|
|
|
|
// immutable refs to IndexedHir.
|
|
|
|
map: IndexVec<LocalDefId, Option<&'hir mut OwnerNodes<'hir>>>,
|
|
|
|
/// Map from each owner to its parent's HirId inside another owner.
|
|
|
|
// This map is separate from `map` to eventually allow for per-owner indexing.
|
2021-03-05 20:09:33 +01:00
|
|
|
parenting: FxHashMap<LocalDefId, HirId>,
|
2021-02-28 18:58:50 +01:00
|
|
|
}
|
|
|
|
|
2021-05-28 21:14:11 +02:00
|
|
|
/// Top-level HIR node for current owner. This only contains the node for which
|
|
|
|
/// `HirId::local_id == 0`, and excludes bodies.
|
|
|
|
#[derive(Copy, Clone, Debug)]
|
2020-03-18 03:48:17 +02:00
|
|
|
pub struct Owner<'tcx> {
|
2020-02-07 11:14:47 +01:00
|
|
|
node: Node<'tcx>,
|
|
|
|
}
|
|
|
|
|
2020-03-18 03:48:17 +02:00
|
|
|
impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for Owner<'tcx> {
|
2020-02-09 12:13:08 +01:00
|
|
|
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
|
2021-03-05 20:09:33 +01:00
|
|
|
let Owner { node } = self;
|
|
|
|
hcx.while_hashing_hir_bodies(false, |hcx| node.hash_stable(hcx, hasher));
|
2020-02-09 12:13:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-28 21:14:11 +02:00
|
|
|
/// HIR node coupled with its parent's id in the same HIR owner.
|
|
|
|
///
|
|
|
|
/// The parent is trash when the node is a HIR owner.
|
2021-01-03 09:19:16 -05:00
|
|
|
#[derive(Clone, Debug)]
|
2020-03-18 03:48:17 +02:00
|
|
|
pub struct ParentedNode<'tcx> {
|
2020-02-10 14:29:21 +01:00
|
|
|
parent: ItemLocalId,
|
|
|
|
node: Node<'tcx>,
|
|
|
|
}
|
|
|
|
|
2021-01-03 09:19:16 -05:00
|
|
|
#[derive(Debug)]
|
2020-03-18 03:48:17 +02:00
|
|
|
pub struct OwnerNodes<'tcx> {
|
2021-05-28 21:14:11 +02:00
|
|
|
/// Pre-computed hash of the full HIR.
|
2020-02-10 14:29:21 +01:00
|
|
|
hash: Fingerprint,
|
2021-05-28 21:14:11 +02:00
|
|
|
/// Full HIR for the current owner.
|
|
|
|
// The zeroth node's parent is trash, but is never accessed.
|
2020-03-18 03:48:17 +02:00
|
|
|
nodes: IndexVec<ItemLocalId, Option<ParentedNode<'tcx>>>,
|
2021-05-28 21:14:11 +02:00
|
|
|
/// Content of local bodies.
|
2020-02-07 11:14:47 +01:00
|
|
|
bodies: FxHashMap<ItemLocalId, &'tcx Body<'tcx>>,
|
|
|
|
}
|
|
|
|
|
2020-03-18 03:48:17 +02:00
|
|
|
impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for OwnerNodes<'tcx> {
|
2020-02-09 12:13:08 +01:00
|
|
|
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
|
2020-03-18 03:48:17 +02:00
|
|
|
// We ignore the `nodes` and `bodies` fields since these refer to information included in
|
2020-02-10 14:29:21 +01:00
|
|
|
// `hash` which is hashed in the collector and used for the crate hash.
|
2020-03-18 03:48:17 +02:00
|
|
|
let OwnerNodes { hash, nodes: _, bodies: _ } = *self;
|
2020-02-10 14:29:21 +01:00
|
|
|
hash.hash_stable(hcx, hasher);
|
2020-02-09 12:13:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-28 21:14:11 +02:00
|
|
|
/// Attributes owner by a HIR owner. It is build as a slice inside the attributes map, restricted
|
|
|
|
/// to the nodes whose `HirId::owner` is `prefix`.
|
2021-01-24 17:14:17 +01:00
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
pub struct AttributeMap<'tcx> {
|
|
|
|
map: &'tcx BTreeMap<HirId, &'tcx [Attribute]>,
|
|
|
|
prefix: LocalDefId,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for AttributeMap<'tcx> {
|
|
|
|
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
|
|
|
|
let range = self.range();
|
|
|
|
|
|
|
|
range.clone().count().hash_stable(hcx, hasher);
|
|
|
|
for (key, value) in range {
|
|
|
|
key.hash_stable(hcx, hasher);
|
|
|
|
value.hash_stable(hcx, hasher);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> std::fmt::Debug for AttributeMap<'tcx> {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
f.debug_struct("AttributeMap")
|
|
|
|
.field("prefix", &self.prefix)
|
|
|
|
.field("range", &&self.range().collect::<Vec<_>>()[..])
|
|
|
|
.finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> AttributeMap<'tcx> {
|
|
|
|
fn get(&self, id: ItemLocalId) -> &'tcx [Attribute] {
|
|
|
|
self.map.get(&HirId { owner: self.prefix, local_id: id }).copied().unwrap_or(&[])
|
|
|
|
}
|
|
|
|
|
|
|
|
fn range(&self) -> std::collections::btree_map::Range<'_, rustc_hir::HirId, &[Attribute]> {
|
|
|
|
let local_zero = ItemLocalId::from_u32(0);
|
|
|
|
let range = HirId { owner: self.prefix, local_id: local_zero }..HirId {
|
|
|
|
owner: LocalDefId { local_def_index: self.prefix.local_def_index + 1 },
|
|
|
|
local_id: local_zero,
|
|
|
|
};
|
|
|
|
self.map.range(range)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-06 11:59:29 +01:00
|
|
|
impl<'tcx> TyCtxt<'tcx> {
|
|
|
|
#[inline(always)]
|
2020-02-09 15:32:00 +01:00
|
|
|
pub fn hir(self) -> map::Map<'tcx> {
|
|
|
|
map::Map { tcx: self }
|
2020-02-06 11:59:29 +01:00
|
|
|
}
|
2020-02-11 14:38:16 +01:00
|
|
|
|
2020-03-18 20:27:59 +02:00
|
|
|
pub fn parent_module(self, id: HirId) -> LocalDefId {
|
|
|
|
self.parent_module_from_def_id(id.owner)
|
2020-02-11 14:38:16 +01:00
|
|
|
}
|
2020-02-06 11:59:29 +01:00
|
|
|
}
|
2020-01-02 03:39:11 +01:00
|
|
|
|
2020-07-05 23:00:14 +03:00
|
|
|
pub fn provide(providers: &mut Providers) {
|
2020-02-11 14:38:16 +01:00
|
|
|
providers.parent_module_from_def_id = |tcx, id| {
|
|
|
|
let hir = tcx.hir();
|
2020-08-12 12:22:56 +02:00
|
|
|
hir.local_def_id(hir.get_module_parent_node(hir.local_def_id_to_hir_id(id)))
|
2020-02-11 14:38:16 +01:00
|
|
|
};
|
2021-05-11 11:42:01 +02:00
|
|
|
providers.hir_crate = |tcx, ()| tcx.untracked_crate;
|
2020-02-09 15:32:00 +01:00
|
|
|
providers.index_hir = map::index_hir;
|
2021-02-28 18:58:50 +01:00
|
|
|
providers.crate_hash = map::crate_hash;
|
2021-01-31 17:58:57 +01:00
|
|
|
providers.hir_module_items = |tcx, id| &tcx.untracked_crate.modules[&id];
|
2021-05-28 21:14:11 +02:00
|
|
|
providers.hir_owner = |tcx, id| {
|
|
|
|
let owner = tcx.index_hir(()).map[id].as_ref()?;
|
|
|
|
let node = owner.nodes[ItemLocalId::new(0)].as_ref()?.node;
|
|
|
|
Some(Owner { node })
|
|
|
|
};
|
|
|
|
providers.hir_owner_nodes = |tcx, id| tcx.index_hir(()).map[id].as_deref();
|
2021-03-06 11:25:41 +01:00
|
|
|
providers.hir_owner_parent = |tcx, id| {
|
2021-05-11 11:42:01 +02:00
|
|
|
let index = tcx.index_hir(());
|
2021-03-06 11:25:41 +01:00
|
|
|
index.parenting.get(&id).copied().unwrap_or(CRATE_HIR_ID)
|
|
|
|
};
|
2021-01-24 17:14:17 +01:00
|
|
|
providers.hir_attrs = |tcx, id| AttributeMap { map: &tcx.untracked_crate.attrs, prefix: id };
|
2020-12-23 10:32:00 +01:00
|
|
|
providers.def_span = |tcx, def_id| tcx.hir().span_if_local(def_id).unwrap_or(DUMMY_SP);
|
2020-06-11 13:42:22 -04:00
|
|
|
providers.fn_arg_names = |tcx, id| {
|
|
|
|
let hir = tcx.hir();
|
2020-08-12 12:22:56 +02:00
|
|
|
let hir_id = hir.local_def_id_to_hir_id(id.expect_local());
|
2020-06-11 13:42:22 -04:00
|
|
|
if let Some(body_id) = hir.maybe_body_owned_by(hir_id) {
|
|
|
|
tcx.arena.alloc_from_iter(hir.body_param_names(body_id))
|
|
|
|
} else if let Node::TraitItem(&TraitItem {
|
|
|
|
kind: TraitItemKind::Fn(_, TraitFn::Required(idents)),
|
|
|
|
..
|
|
|
|
}) = hir.get(hir_id)
|
|
|
|
{
|
|
|
|
tcx.arena.alloc_slice(idents)
|
|
|
|
} else {
|
|
|
|
span_bug!(hir.span(hir_id), "fn_arg_names: unexpected item {:?}", id);
|
|
|
|
}
|
|
|
|
};
|
2021-01-16 14:35:16 +01:00
|
|
|
providers.opt_def_kind = |tcx, def_id| tcx.hir().opt_def_kind(def_id.expect_local());
|
2021-05-11 11:42:01 +02:00
|
|
|
providers.all_local_trait_impls = |tcx, ()| &tcx.hir_crate(()).trait_impls;
|
2021-06-04 21:37:05 +02:00
|
|
|
providers.expn_that_defined = |tcx, id| {
|
|
|
|
let id = id.expect_local();
|
2021-04-04 14:40:35 +02:00
|
|
|
tcx.resolutions(()).definitions.expansion_that_defined(id)
|
2021-06-04 21:37:05 +02:00
|
|
|
};
|
2020-01-02 03:39:11 +01:00
|
|
|
}
|