1 //! HIR datatypes. See the [rustc dev guide] for more info.
3 //! [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/hir.html
8 use crate::ty::query::Providers;
10 use rustc_data_structures::cold_path;
11 use rustc_data_structures::fx::FxHashMap;
12 use rustc_hir::def_id::{DefId, LOCAL_CRATE};
17 use rustc_hir::ItemLocalId;
19 use rustc_index::vec::IndexVec;
23 pub struct HirOwner<'tcx> {
28 #[derive(HashStable, Clone)]
29 pub struct HirItem<'tcx> {
35 pub struct HirOwnerItems<'tcx> {
36 //owner: &'tcx HirOwner<'tcx>,
37 items: IndexVec<ItemLocalId, Option<HirItem<'tcx>>>,
38 bodies: FxHashMap<ItemLocalId, &'tcx Body<'tcx>>,
41 /// A wrapper type which allows you to access HIR.
43 pub struct Hir<'tcx> {
45 map: &'tcx map::Map<'tcx>,
48 impl<'tcx> Hir<'tcx> {
49 pub fn krate(&self) -> &'tcx Crate<'tcx> {
50 self.tcx.hir_crate(LOCAL_CRATE)
54 impl<'tcx> Deref for Hir<'tcx> {
55 type Target = &'tcx map::Map<'tcx>;
58 fn deref(&self) -> &Self::Target {
63 impl<'hir> print::PpAnn for Hir<'hir> {
64 fn nested(&self, state: &mut print::State<'_>, nested: print::Nested) {
65 self.map.nested(state, nested)
69 impl<'tcx> TyCtxt<'tcx> {
71 pub fn hir(self) -> Hir<'tcx> {
72 let map = self.late_hir_map.load();
73 let map = if unlikely!(map.is_none()) {
75 let map = self.hir_map(LOCAL_CRATE);
76 self.late_hir_map.store(Some(map));
82 Hir { tcx: self, map }
85 pub fn parent_module(self, id: HirId) -> DefId {
86 self.parent_module_from_def_id(DefId::local(id.owner))
90 pub fn provide(providers: &mut Providers<'_>) {
91 providers.parent_module_from_def_id = |tcx, id| {
93 hir.local_def_id(hir.get_module_parent_node(hir.as_local_hir_id(id).unwrap()))
95 providers.hir_crate = |tcx, _| tcx.hir_map(LOCAL_CRATE).untracked_krate();
96 providers.hir_map = |tcx, id| {
97 assert_eq!(id, LOCAL_CRATE);
98 let early = tcx.hir_map.steal();
99 tcx.arena.alloc(map::Map {
103 dep_graph: early.dep_graph,
105 crate_hash: early.crate_hash,
107 owner_map: early.owner_map,
108 owner_items_map: early.owner_items_map,
110 definitions: early.definitions,
112 hir_to_node_id: early.hir_to_node_id,
115 providers.hir_module_items = |tcx, id| {
116 assert_eq!(id.krate, LOCAL_CRATE);
118 let module = hir.as_local_hir_id(id).unwrap();
119 &hir.untracked_krate().modules[&module]
121 providers.hir_owner = |tcx, id| {
122 assert_eq!(id.krate, LOCAL_CRATE);
123 *tcx.hir().map.owner_map.get(&id.index).unwrap()
125 providers.hir_owner_items = |tcx, id| {
126 assert_eq!(id.krate, LOCAL_CRATE);
127 *tcx.hir().map.owner_items_map.get(&id.index).unwrap()
129 map::provide(providers);