]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_middle/src/hir/mod.rs
Fix comment for async closure variant
[rust.git] / compiler / rustc_middle / src / hir / mod.rs
1 //! HIR datatypes. See the [rustc dev guide] for more info.
2 //!
3 //! [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/hir.html
4
5 pub mod map;
6 pub mod nested_filter;
7 pub mod place;
8
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};
14 use rustc_hir::*;
15 use rustc_query_system::ich::StableHashingContext;
16 use rustc_span::DUMMY_SP;
17
18 /// Top-level HIR node for current owner. This only contains the node for which
19 /// `HirId::local_id == 0`, and excludes bodies.
20 ///
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,
27 }
28
29 impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for Owner<'tcx> {
30     #[inline]
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)
34     }
35 }
36
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]>,
42     items: Box<[ItemId]>,
43     trait_items: Box<[TraitItemId]>,
44     impl_items: Box<[ImplItemId]>,
45     foreign_items: Box<[ForeignItemId]>,
46 }
47
48 impl ModuleItems {
49     pub fn items(&self) -> impl Iterator<Item = ItemId> + '_ {
50         self.items.iter().copied()
51     }
52
53     pub fn trait_items(&self) -> impl Iterator<Item = TraitItemId> + '_ {
54         self.trait_items.iter().copied()
55     }
56
57     pub fn impl_items(&self) -> impl Iterator<Item = ImplItemId> + '_ {
58         self.impl_items.iter().copied()
59     }
60
61     pub fn foreign_items(&self) -> impl Iterator<Item = ForeignItemId> + '_ {
62         self.foreign_items.iter().copied()
63     }
64 }
65
66 impl<'tcx> TyCtxt<'tcx> {
67     #[inline(always)]
68     pub fn hir(self) -> map::Map<'tcx> {
69         map::Map { tcx: self }
70     }
71
72     pub fn parent_module(self, id: HirId) -> LocalDefId {
73         self.parent_module_from_def_id(id.owner)
74     }
75
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)))
80     }
81 }
82
83 pub fn provide(providers: &mut Providers) {
84     providers.parent_module_from_def_id = |tcx, id| {
85         let hir = tcx.hir();
86         hir.get_module_parent_node(hir.local_def_id_to_hir_id(id))
87     };
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 })
96     };
97     providers.local_def_id_to_hir_id = |tcx, id| {
98         let owner = tcx.hir_crate(()).owners[id].map(|_| ());
99         match owner {
100             MaybeOwner::Owner(_) => HirId::make_owner(id),
101             MaybeOwner::Phantom => bug!("No HirId for {:?}", id),
102             MaybeOwner::NonOwner(hir_id) => hir_id,
103         }
104     };
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)
114             {
115                 parent_hir_id.local_id = *local_id;
116             }
117             parent_hir_id
118         });
119         parent
120     };
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| {
126         let hir = tcx.hir();
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)),
132             ..
133         }) = hir.get(hir_id)
134         {
135             tcx.arena.alloc_slice(idents)
136         } else {
137             span_bug!(hir.span(hir_id), "fn_arg_names: unexpected item {:?}", id);
138         }
139     };
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)
145     };
146     providers.in_scope_traits_map =
147         |tcx, id| tcx.hir_crate(()).owners[id].as_owner().map(|owner_info| &owner_info.trait_map);
148 }