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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

173 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 map;
pub mod nested_filter;
pub mod place;
use crate::ty::query::Providers;
use crate::ty::{DefIdTree, ImplSubject, TyCtxt};
2020-02-10 14:29:21 +01:00
use rustc_data_structures::fingerprint::Fingerprint;
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
2022-06-18 19:20:59 +02:00
use rustc_data_structures::sync::{par_for_each_in, Send, Sync};
2022-03-20 00:12:03 -03:00
use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_hir::*;
2020-11-14 16:48:54 +01:00
use rustc_query_system::ich::StableHashingContext;
use rustc_span::{ExpnId, DUMMY_SP};
2020-02-06 11:59:29 +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>,
2021-10-11 22:36:37 +02:00
hash_without_bodies: 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) {
2021-10-11 22:36:37 +02:00
let Owner { node: _, hash_without_bodies } = self;
hash_without_bodies.hash_stable(hcx, hasher)
}
}
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.
2022-03-28 19:53:01 +02:00
#[derive(Debug, HashStable, Encodable, Decodable)]
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
}
impl ModuleItems {
pub fn items(&self) -> impl Iterator<Item = ItemId> + '_ {
self.items.iter().copied()
}
pub fn trait_items(&self) -> impl Iterator<Item = TraitItemId> + '_ {
self.trait_items.iter().copied()
}
pub fn impl_items(&self) -> impl Iterator<Item = ImplItemId> + '_ {
self.impl_items.iter().copied()
}
pub fn foreign_items(&self) -> impl Iterator<Item = ForeignItemId> + '_ {
self.foreign_items.iter().copied()
}
2022-06-18 19:20:59 +02:00
pub fn par_items(&self, f: impl Fn(ItemId) + Send + Sync) {
par_for_each_in(&self.items[..], |&id| f(id))
}
pub fn par_trait_items(&self, f: impl Fn(TraitItemId) + Send + Sync) {
par_for_each_in(&self.trait_items[..], |&id| f(id))
}
pub fn par_impl_items(&self, f: impl Fn(ImplItemId) + Send + Sync) {
par_for_each_in(&self.impl_items[..], |&id| f(id))
}
pub fn par_foreign_items(&self, f: impl Fn(ForeignItemId) + Send + Sync) {
par_for_each_in(&self.foreign_items[..], |&id| f(id))
}
}
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)
}
2022-03-20 00:12:03 -03:00
2022-03-22 10:35:17 -03:00
pub fn impl_subject(self, def_id: DefId) -> ImplSubject<'tcx> {
2022-03-20 00:12:03 -03:00
self.impl_trait_ref(def_id)
.map(ImplSubject::Trait)
.unwrap_or_else(|| ImplSubject::Inherent(self.type_of(def_id)))
}
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.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;
providers.hir_crate_items = map::hir_crate_items;
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| {
let owner = tcx.hir_crate(()).owners.get(id)?.as_owner()?;
let node = owner.node();
Some(Owner { node, hash_without_bodies: owner.nodes.hash_without_bodies })
};
providers.local_def_id_to_hir_id = |tcx, id| {
let owner = tcx.hir_crate(()).owners[id].map(|_| ());
match owner {
MaybeOwner::Owner(_) => HirId::make_owner(id),
MaybeOwner::Phantom => bug!("No HirId for {:?}", id),
MaybeOwner::NonOwner(hir_id) => hir_id,
}
};
providers.hir_owner_nodes = |tcx, id| tcx.hir_crate(()).owners[id].map(|i| &i.nodes);
2021-03-06 11:25:41 +01:00
providers.hir_owner_parent = |tcx, id| {
// Accessing the local_parent is ok since its value is hashed as part of `id`'s DefPathHash.
tcx.opt_local_parent(id).map_or(CRATE_HIR_ID, |parent| {
let mut parent_hir_id = tcx.hir().local_def_id_to_hir_id(parent);
if let Some(local_id) =
tcx.hir_crate(()).owners[parent_hir_id.owner].unwrap().parenting.get(&id)
{
2021-02-28 20:23:10 +01:00
parent_hir_id.local_id = *local_id;
}
parent_hir_id
})
2021-03-06 11:25:41 +01:00
};
providers.hir_attrs =
|tcx, id| tcx.hir_crate(()).owners[id].as_owner().map_or(AttributeMap::EMPTY, |o| &o.attrs);
providers.source_span =
|tcx, def_id| tcx.resolutions(()).source_span.get(def_id).copied().unwrap_or(DUMMY_SP);
2022-03-30 16:51:25 +02:00
providers.def_span = |tcx, def_id| {
let def_id = def_id.expect_local();
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
tcx.hir().opt_span(hir_id).unwrap_or(DUMMY_SP)
};
providers.def_ident_span = |tcx, def_id| {
let def_id = def_id.expect_local();
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
tcx.hir().opt_ident_span(hir_id)
};
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();
tcx.resolutions(()).expn_that_defined.get(&id).copied().unwrap_or(ExpnId::root())
};
providers.in_scope_traits_map =
|tcx, id| tcx.hir_crate(()).owners[id].as_owner().map(|owner_info| &owner_info.trait_map);
}