1
Fork 0
rust/compiler/rustc_middle/src/hir/mod.rs

176 lines
6.5 KiB
Rust
Raw Normal View History

2020-03-05 18:07:42 -03:00
//! HIR datatypes. See the [rustc dev guide] for more info.
//!
//! [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/hir.html
pub mod exports;
pub mod map;
pub mod place;
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;
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
2021-05-11 11:42:01 +02:00
use rustc_hir::def_id::LocalDefId;
use rustc_hir::*;
2021-05-28 21:14:11 +02:00
use rustc_index::vec::{Idx, IndexVec};
2020-11-14 16:48:54 +01:00
use rustc_query_system::ich::StableHashingContext;
2020-12-23 10:32:00 +01:00
use rustc_span::DUMMY_SP;
2021-09-07 20:07:33 +02:00
use std::collections::BTreeMap;
2020-02-06 11:59:29 +01:00
2021-02-28 20:23:10 +01:00
/// Result of HIR indexing for a given HIR owner.
#[derive(Debug, HashStable)]
2021-02-28 18:58:50 +01:00
pub struct IndexedHir<'hir> {
2021-02-28 20:23:10 +01:00
/// Contents of the HIR.
nodes: OwnerNodes<'hir>,
/// Map from each nested owner to its parent's local id.
parenting: FxHashMap<LocalDefId, ItemLocalId>,
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.
2021-03-27 13:21:26 +01:00
///
/// This struct exists to encapsulate all access to the hir_owner query in this module, and to
/// implement HashStable without hashing bodies.
2021-05-28 21:14:11 +02:00
#[derive(Copy, Clone, Debug)]
pub struct Owner<'tcx> {
2021-03-27 13:21:26 +01:00
node: OwnerNode<'tcx>,
node_hash: Fingerprint,
2020-02-07 11:14:47 +01:00
}
impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for Owner<'tcx> {
#[inline]
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
let Owner { node: _, node_hash } = self;
node_hash.hash_stable(hcx, hasher)
}
}
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.
#[derive(Clone, Debug)]
pub struct ParentedNode<'tcx> {
2020-02-10 14:29:21 +01:00
parent: ItemLocalId,
node: Node<'tcx>,
}
#[derive(Debug)]
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,
/// Pre-computed hash of the top node.
node_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.
nodes: IndexVec<ItemLocalId, Option<ParentedNode<'tcx>>>,
2021-05-28 21:14:11 +02:00
/// Content of local bodies.
2021-09-17 19:41:05 +02:00
bodies: &'tcx IndexVec<ItemLocalId, Option<&'tcx Body<'tcx>>>,
2020-02-07 11:14:47 +01:00
}
impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for OwnerNodes<'tcx> {
#[inline]
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
// 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.
let OwnerNodes { hash, node_hash: _, nodes: _, bodies: _ } = *self;
2020-02-10 14:29:21 +01:00
hash.hash_stable(hcx, hasher);
}
}
2021-07-16 14:42:26 +02:00
/// Attributes owner by a HIR owner.
#[derive(Copy, Clone, Debug, HashStable)]
2021-01-24 17:14:17 +01:00
pub struct AttributeMap<'tcx> {
2021-07-16 14:42:26 +02:00
map: &'tcx BTreeMap<ItemLocalId, &'tcx [Attribute]>,
2021-01-24 17:14:17 +01:00
}
impl<'tcx> AttributeMap<'tcx> {
2021-07-16 14:42:26 +02:00
fn new(owner_info: &'tcx Option<OwnerInfo<'tcx>>) -> AttributeMap<'tcx> {
const FALLBACK: &'static BTreeMap<ItemLocalId, &'static [Attribute]> = &BTreeMap::new();
let map = owner_info.as_ref().map_or(FALLBACK, |info| &info.attrs);
AttributeMap { map }
2021-01-24 17:14:17 +01:00
}
2021-07-16 14:42:26 +02:00
fn get(&self, id: ItemLocalId) -> &'tcx [Attribute] {
self.map.get(&id).copied().unwrap_or(&[])
2021-01-24 17:14:17 +01:00
}
}
2021-09-07 20:07:33 +02:00
/// Gather the LocalDefId for each item-like within a module, including items contained within
/// bodies. The Ids are in visitor order. This is used to partition a pass between modules.
#[derive(Debug, HashStable)]
2021-07-18 18:12:17 +02:00
pub struct ModuleItems {
2021-09-07 20:07:33 +02:00
submodules: Box<[LocalDefId]>,
items: Box<[ItemId]>,
trait_items: Box<[TraitItemId]>,
impl_items: Box<[ImplItemId]>,
foreign_items: Box<[ForeignItemId]>,
2021-07-18 18:12:17 +02:00
}
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
}
pub fn parent_module(self, id: HirId) -> LocalDefId {
self.parent_module_from_def_id(id.owner)
}
2020-02-06 11:59:29 +01:00
}
pub fn provide(providers: &mut Providers) {
providers.parent_module_from_def_id = |tcx, id| {
let hir = tcx.hir();
hir.local_def_id(hir.get_module_parent_node(hir.local_def_id_to_hir_id(id)))
};
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-07-18 18:12:17 +02:00
providers.hir_module_items = map::hir_module_items;
2021-05-28 21:14:11 +02:00
providers.hir_owner = |tcx, id| {
2021-02-28 20:23:10 +01:00
let owner = tcx.index_hir(id)?;
let node = owner.nodes.nodes[ItemLocalId::new(0)].as_ref().unwrap().node;
2021-03-27 13:21:26 +01:00
let node = node.as_owner().unwrap(); // Indexing must ensure it is an OwnerNode.
2021-02-28 20:23:10 +01:00
Some(Owner { node, node_hash: owner.nodes.node_hash })
2021-05-28 21:14:11 +02:00
};
2021-02-28 20:23:10 +01:00
providers.hir_owner_nodes = |tcx, id| tcx.index_hir(id).map(|i| &i.nodes);
2021-03-06 11:25:41 +01:00
providers.hir_owner_parent = |tcx, id| {
2021-02-28 20:23:10 +01:00
let parent = tcx.untracked_resolutions.definitions.def_key(id).parent;
let parent = parent.map_or(CRATE_HIR_ID, |local_def_index| {
let def_id = LocalDefId { local_def_index };
let mut parent_hir_id =
tcx.untracked_resolutions.definitions.local_def_id_to_hir_id(def_id);
if let Some(local_id) = tcx.index_hir(parent_hir_id.owner).unwrap().parenting.get(&id) {
parent_hir_id.local_id = *local_id;
}
parent_hir_id
});
parent
2021-03-06 11:25:41 +01:00
};
2021-07-16 14:42:26 +02:00
providers.hir_attrs = |tcx, id| AttributeMap::new(&tcx.hir_crate(()).owners[id]);
providers.source_span = |tcx, def_id| tcx.resolutions(()).definitions.def_span(def_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);
providers.fn_arg_names = |tcx, id| {
let hir = tcx.hir();
let hir_id = hir.local_def_id_to_hir_id(id.expect_local());
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());
providers.all_local_trait_impls = |tcx, ()| &tcx.resolutions(()).trait_impls;
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)
};
}