]> git.lizzy.rs Git - rust.git/blob - src/librustc/hir/mod.rs
Remove the `map` field from `Map`
[rust.git] / src / librustc / 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 exports;
6 pub mod map;
7
8 use crate::ty::query::Providers;
9 use crate::ty::TyCtxt;
10 use rustc_data_structures::cold_path;
11 use rustc_data_structures::fx::FxHashMap;
12 use rustc_hir::def_id::{DefId, LOCAL_CRATE};
13 use rustc_hir::print;
14 use rustc_hir::Body;
15 use rustc_hir::Crate;
16 use rustc_hir::HirId;
17 use rustc_hir::ItemLocalId;
18 use rustc_hir::Node;
19 use rustc_index::vec::IndexVec;
20 use std::ops::Deref;
21
22 #[derive(HashStable)]
23 pub struct HirOwner<'tcx> {
24     parent: HirId,
25     node: Node<'tcx>,
26 }
27
28 #[derive(HashStable, Clone)]
29 pub struct HirItem<'tcx> {
30     parent: ItemLocalId,
31     node: Node<'tcx>,
32 }
33
34 #[derive(HashStable)]
35 pub struct HirOwnerItems<'tcx> {
36     //owner: &'tcx HirOwner<'tcx>,
37     items: IndexVec<ItemLocalId, Option<HirItem<'tcx>>>,
38     bodies: FxHashMap<ItemLocalId, &'tcx Body<'tcx>>,
39 }
40
41 /// A wrapper type which allows you to access HIR.
42 #[derive(Clone)]
43 pub struct Hir<'tcx> {
44     tcx: TyCtxt<'tcx>,
45     map: &'tcx map::Map<'tcx>,
46 }
47
48 impl<'tcx> Hir<'tcx> {
49     pub fn krate(&self) -> &'tcx Crate<'tcx> {
50         self.tcx.hir_crate(LOCAL_CRATE)
51     }
52 }
53
54 impl<'tcx> Deref for Hir<'tcx> {
55     type Target = &'tcx map::Map<'tcx>;
56
57     #[inline(always)]
58     fn deref(&self) -> &Self::Target {
59         &self.map
60     }
61 }
62
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)
66     }
67 }
68
69 impl<'tcx> TyCtxt<'tcx> {
70     #[inline(always)]
71     pub fn hir(self) -> Hir<'tcx> {
72         let map = self.late_hir_map.load();
73         let map = if unlikely!(map.is_none()) {
74             cold_path(|| {
75                 let map = self.hir_map(LOCAL_CRATE);
76                 self.late_hir_map.store(Some(map));
77                 map
78             })
79         } else {
80             map.unwrap()
81         };
82         Hir { tcx: self, map }
83     }
84
85     pub fn parent_module(self, id: HirId) -> DefId {
86         self.parent_module_from_def_id(DefId::local(id.owner))
87     }
88 }
89
90 pub fn provide(providers: &mut Providers<'_>) {
91     providers.parent_module_from_def_id = |tcx, id| {
92         let hir = tcx.hir();
93         hir.local_def_id(hir.get_module_parent_node(hir.as_local_hir_id(id).unwrap()))
94     };
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 {
100             tcx,
101             krate: early.krate,
102
103             dep_graph: early.dep_graph,
104
105             crate_hash: early.crate_hash,
106
107             owner_map: early.owner_map,
108             owner_items_map: early.owner_items_map,
109
110             definitions: early.definitions,
111
112             hir_to_node_id: early.hir_to_node_id,
113         })
114     };
115     providers.hir_module_items = |tcx, id| {
116         assert_eq!(id.krate, LOCAL_CRATE);
117         let hir = tcx.hir();
118         let module = hir.as_local_hir_id(id).unwrap();
119         &hir.untracked_krate().modules[&module]
120     };
121     providers.hir_owner = |tcx, id| {
122         assert_eq!(id.krate, LOCAL_CRATE);
123         *tcx.hir().map.owner_map.get(&id.index).unwrap()
124     };
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()
128     };
129     map::provide(providers);
130 }