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::{ImplSubject, TyCtxt};
11 use rustc_data_structures::fingerprint::Fingerprint;
12 use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
13 use rustc_hir::def_id::{DefId, 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]>,
49 pub fn items(&self) -> impl Iterator<Item = ItemId> + '_ {
50 self.items.iter().copied()
53 pub fn trait_items(&self) -> impl Iterator<Item = TraitItemId> + '_ {
54 self.trait_items.iter().copied()
57 pub fn impl_items(&self) -> impl Iterator<Item = ImplItemId> + '_ {
58 self.impl_items.iter().copied()
61 pub fn foreign_items(&self) -> impl Iterator<Item = ForeignItemId> + '_ {
62 self.foreign_items.iter().copied()
66 impl<'tcx> TyCtxt<'tcx> {
68 pub fn hir(self) -> map::Map<'tcx> {
69 map::Map { tcx: self }
72 pub fn parent_module(self, id: HirId) -> LocalDefId {
73 self.parent_module_from_def_id(id.owner)
76 pub fn impl_subject(self, def_id: DefId) -> ImplSubject<'tcx> {
77 self.impl_trait_ref(def_id)
78 .map(ImplSubject::Trait)
79 .unwrap_or_else(|| ImplSubject::Inherent(self.type_of(def_id)))
83 pub fn provide(providers: &mut Providers) {
84 providers.parent_module_from_def_id = |tcx, id| {
86 hir.get_module_parent_node(hir.local_def_id_to_hir_id(id))
88 providers.hir_crate = |tcx, ()| tcx.untracked_crate;
89 providers.hir_crate_items = map::hir_crate_items;
90 providers.crate_hash = map::crate_hash;
91 providers.hir_module_items = map::hir_module_items;
92 providers.hir_owner = |tcx, id| {
93 let owner = tcx.hir_crate(()).owners.get(id)?.as_owner()?;
94 let node = owner.node();
95 Some(Owner { node, hash_without_bodies: owner.nodes.hash_without_bodies })
97 providers.local_def_id_to_hir_id = |tcx, id| {
98 let owner = tcx.hir_crate(()).owners[id].map(|_| ());
100 MaybeOwner::Owner(_) => HirId::make_owner(id),
101 MaybeOwner::Phantom => bug!("No HirId for {:?}", id),
102 MaybeOwner::NonOwner(hir_id) => hir_id,
105 providers.hir_owner_nodes = |tcx, id| tcx.hir_crate(()).owners[id].map(|i| &i.nodes);
106 providers.hir_owner_parent = |tcx, id| {
107 // Accessing the def_key is ok since its value is hashed as part of `id`'s DefPathHash.
108 let parent = tcx.untracked_resolutions.definitions.def_key(id).parent;
109 let parent = parent.map_or(CRATE_HIR_ID, |local_def_index| {
110 let def_id = LocalDefId { local_def_index };
111 let mut parent_hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
112 if let Some(local_id) =
113 tcx.hir_crate(()).owners[parent_hir_id.owner].unwrap().parenting.get(&id)
115 parent_hir_id.local_id = *local_id;
121 providers.hir_attrs =
122 |tcx, id| tcx.hir_crate(()).owners[id].as_owner().map_or(AttributeMap::EMPTY, |o| &o.attrs);
123 providers.source_span = |tcx, def_id| tcx.resolutions(()).definitions.def_span(def_id);
124 providers.def_span = |tcx, def_id| tcx.hir().span_if_local(def_id).unwrap_or(DUMMY_SP);
125 providers.fn_arg_names = |tcx, id| {
127 let hir_id = hir.local_def_id_to_hir_id(id.expect_local());
128 if let Some(body_id) = hir.maybe_body_owned_by(hir_id) {
129 tcx.arena.alloc_from_iter(hir.body_param_names(body_id))
130 } else if let Node::TraitItem(&TraitItem {
131 kind: TraitItemKind::Fn(_, TraitFn::Required(idents)),
135 tcx.arena.alloc_slice(idents)
137 span_bug!(hir.span(hir_id), "fn_arg_names: unexpected item {:?}", id);
140 providers.opt_def_kind = |tcx, def_id| tcx.hir().opt_def_kind(def_id.expect_local());
141 providers.all_local_trait_impls = |tcx, ()| &tcx.resolutions(()).trait_impls;
142 providers.expn_that_defined = |tcx, id| {
143 let id = id.expect_local();
144 tcx.resolutions(()).definitions.expansion_that_defined(id)
146 providers.in_scope_traits_map =
147 |tcx, id| tcx.hir_crate(()).owners[id].as_owner().map(|owner_info| &owner_info.trait_map);