]> git.proxmox.com Git - rustc.git/blob - src/librustc/hir/mod.rs
New upstream version 1.43.0+dfsg1
[rustc.git] / src / librustc / hir / mod.rs
1 //! HIR datatypes. See the [rustc guide] for more info.
2 //!
3 //! [rustc guide]: https://rust-lang.github.io/rustc-guide/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_hir::def_id::{DefId, LOCAL_CRATE};
11 use rustc_hir::print;
12 use rustc_hir::Crate;
13 use rustc_hir::HirId;
14 use std::ops::Deref;
15
16 /// A wrapper type which allows you to access HIR.
17 #[derive(Clone)]
18 pub struct Hir<'tcx> {
19 tcx: TyCtxt<'tcx>,
20 map: &'tcx map::Map<'tcx>,
21 }
22
23 impl<'tcx> Hir<'tcx> {
24 pub fn krate(&self) -> &'tcx Crate<'tcx> {
25 self.tcx.hir_crate(LOCAL_CRATE)
26 }
27 }
28
29 impl<'tcx> Deref for Hir<'tcx> {
30 type Target = &'tcx map::Map<'tcx>;
31
32 #[inline(always)]
33 fn deref(&self) -> &Self::Target {
34 &self.map
35 }
36 }
37
38 impl<'hir> print::PpAnn for Hir<'hir> {
39 fn nested(&self, state: &mut print::State<'_>, nested: print::Nested) {
40 self.map.nested(state, nested)
41 }
42 }
43
44 impl<'tcx> TyCtxt<'tcx> {
45 #[inline(always)]
46 pub fn hir(self) -> Hir<'tcx> {
47 Hir { tcx: self, map: &self.hir_map }
48 }
49
50 pub fn parent_module(self, id: HirId) -> DefId {
51 self.parent_module_from_def_id(DefId::local(id.owner))
52 }
53 }
54
55 pub fn provide(providers: &mut Providers<'_>) {
56 providers.parent_module_from_def_id = |tcx, id| {
57 let hir = tcx.hir();
58 hir.local_def_id(hir.get_module_parent_node(hir.as_local_hir_id(id).unwrap()))
59 };
60 providers.hir_crate = |tcx, _| tcx.hir_map.untracked_krate();
61 map::provide(providers);
62 }