1 //! HIR datatypes. See the [rustc dev guide] for more info.
3 //! [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/hir.html
9 use crate::ty::query::Providers;
10 use crate::ty::TyCtxt;
11 use rustc_data_structures::fingerprint::Fingerprint;
12 use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
13 use rustc_hir::def_id::LocalDefId;
15 use rustc_query_system::ich::StableHashingContext;
16 use rustc_span::DUMMY_SP;
18 /// Top-level HIR node for current owner. This only contains the node for which
19 /// `HirId::local_id == 0`, and excludes bodies.
21 /// This struct exists to encapsulate all access to the hir_owner query in this module, and to
22 /// implement HashStable without hashing bodies.
23 #[derive(Copy, Clone, Debug)]
24 pub struct Owner<'tcx> {
25 node: OwnerNode<'tcx>,
26 hash_without_bodies: Fingerprint,
29 impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for Owner<'tcx> {
31 fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
32 let Owner { node: _, hash_without_bodies } = self;
33 hash_without_bodies.hash_stable(hcx, hasher)
37 /// Gather the LocalDefId for each item-like within a module, including items contained within
38 /// bodies. The Ids are in visitor order. This is used to partition a pass between modules.
39 #[derive(Debug, HashStable)]
40 pub struct ModuleItems {
41 submodules: Box<[LocalDefId]>,
43 trait_items: Box<[TraitItemId]>,
44 impl_items: Box<[ImplItemId]>,
45 foreign_items: Box<[ForeignItemId]>,
48 impl<'tcx> TyCtxt<'tcx> {
50 pub fn hir(self) -> map::Map<'tcx> {
51 map::Map { tcx: self }
54 pub fn parent_module(self, id: HirId) -> LocalDefId {
55 self.parent_module_from_def_id(id.owner)
59 pub fn provide(providers: &mut Providers) {
60 providers.parent_module_from_def_id = |tcx, id| {
62 hir.get_module_parent_node(hir.local_def_id_to_hir_id(id))
64 providers.hir_crate = |tcx, ()| tcx.untracked_crate;
65 providers.crate_hash = map::crate_hash;
66 providers.hir_module_items = map::hir_module_items;
67 providers.hir_owner = |tcx, id| {
68 let owner = tcx.hir_crate(()).owners.get(id)?.as_owner()?;
69 let node = owner.node();
70 Some(Owner { node, hash_without_bodies: owner.nodes.hash_without_bodies })
72 providers.local_def_id_to_hir_id = |tcx, id| {
73 let owner = tcx.hir_crate(()).owners[id].map(|_| ());
75 MaybeOwner::Owner(_) => HirId::make_owner(id),
76 MaybeOwner::Phantom => bug!("No HirId for {:?}", id),
77 MaybeOwner::NonOwner(hir_id) => hir_id,
80 providers.hir_owner_nodes = |tcx, id| tcx.hir_crate(()).owners[id].map(|i| &i.nodes);
81 providers.hir_owner_parent = |tcx, id| {
82 // Accessing the def_key is ok since its value is hashed as part of `id`'s DefPathHash.
83 let parent = tcx.untracked_resolutions.definitions.def_key(id).parent;
84 let parent = parent.map_or(CRATE_HIR_ID, |local_def_index| {
85 let def_id = LocalDefId { local_def_index };
86 let mut parent_hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
87 if let Some(local_id) =
88 tcx.hir_crate(()).owners[parent_hir_id.owner].unwrap().parenting.get(&id)
90 parent_hir_id.local_id = *local_id;
97 |tcx, id| tcx.hir_crate(()).owners[id].as_owner().map_or(AttributeMap::EMPTY, |o| &o.attrs);
98 providers.source_span = |tcx, def_id| tcx.resolutions(()).definitions.def_span(def_id);
99 providers.def_span = |tcx, def_id| tcx.hir().span_if_local(def_id).unwrap_or(DUMMY_SP);
100 providers.fn_arg_names = |tcx, id| {
102 let hir_id = hir.local_def_id_to_hir_id(id.expect_local());
103 if let Some(body_id) = hir.maybe_body_owned_by(hir_id) {
104 tcx.arena.alloc_from_iter(hir.body_param_names(body_id))
105 } else if let Node::TraitItem(&TraitItem {
106 kind: TraitItemKind::Fn(_, TraitFn::Required(idents)),
110 tcx.arena.alloc_slice(idents)
112 span_bug!(hir.span(hir_id), "fn_arg_names: unexpected item {:?}", id);
115 providers.opt_def_kind = |tcx, def_id| tcx.hir().opt_def_kind(def_id.expect_local());
116 providers.all_local_trait_impls = |tcx, ()| &tcx.resolutions(()).trait_impls;
117 providers.expn_that_defined = |tcx, id| {
118 let id = id.expect_local();
119 tcx.resolutions(()).definitions.expansion_that_defined(id)
121 providers.in_scope_traits_map =
122 |tcx, id| tcx.hir_crate(()).owners[id].as_owner().map(|owner_info| &owner_info.trait_map);