2020-01-02 03:39:11 +01:00
|
|
|
//! HIR datatypes. See the [rustc guide] for more info.
|
|
|
|
//!
|
2020-03-06 11:13:26 -03:00
|
|
|
//! [rustc guide]: https://rust-lang.github.io/rustc-dev-guide/hir.html
|
2020-01-02 03:39:11 +01:00
|
|
|
|
2020-01-02 04:53:12 +01:00
|
|
|
pub mod exports;
|
2020-01-02 03:39:11 +01:00
|
|
|
pub mod map;
|
|
|
|
|
|
|
|
use crate::ty::query::Providers;
|
2020-02-06 11:59:29 +01:00
|
|
|
use crate::ty::TyCtxt;
|
2020-02-11 14:38:16 +01:00
|
|
|
use rustc_hir::def_id::{DefId, LOCAL_CRATE};
|
2020-02-06 11:59:29 +01:00
|
|
|
use rustc_hir::print;
|
2020-02-06 12:46:26 +01:00
|
|
|
use rustc_hir::Crate;
|
2020-02-11 14:38:16 +01:00
|
|
|
use rustc_hir::HirId;
|
2020-02-06 11:59:29 +01:00
|
|
|
use std::ops::Deref;
|
|
|
|
|
|
|
|
/// A wrapper type which allows you to access HIR.
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct Hir<'tcx> {
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
map: &'tcx map::Map<'tcx>,
|
|
|
|
}
|
|
|
|
|
2020-02-06 12:46:26 +01:00
|
|
|
impl<'tcx> Hir<'tcx> {
|
|
|
|
pub fn krate(&self) -> &'tcx Crate<'tcx> {
|
|
|
|
self.tcx.hir_crate(LOCAL_CRATE)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-06 11:59:29 +01:00
|
|
|
impl<'tcx> Deref for Hir<'tcx> {
|
|
|
|
type Target = &'tcx map::Map<'tcx>;
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.map
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'hir> print::PpAnn for Hir<'hir> {
|
|
|
|
fn nested(&self, state: &mut print::State<'_>, nested: print::Nested) {
|
|
|
|
self.map.nested(state, nested)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> TyCtxt<'tcx> {
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn hir(self) -> Hir<'tcx> {
|
|
|
|
Hir { tcx: self, map: &self.hir_map }
|
|
|
|
}
|
2020-02-11 14:38:16 +01:00
|
|
|
|
|
|
|
pub fn parent_module(self, id: HirId) -> DefId {
|
|
|
|
self.parent_module_from_def_id(DefId::local(id.owner))
|
|
|
|
}
|
2020-02-06 11:59:29 +01:00
|
|
|
}
|
2020-01-02 03:39:11 +01:00
|
|
|
|
|
|
|
pub fn provide(providers: &mut Providers<'_>) {
|
2020-02-11 14:38:16 +01:00
|
|
|
providers.parent_module_from_def_id = |tcx, id| {
|
|
|
|
let hir = tcx.hir();
|
2020-02-22 19:53:10 +01:00
|
|
|
hir.local_def_id(hir.get_module_parent_node(hir.as_local_hir_id(id).unwrap()))
|
2020-02-11 14:38:16 +01:00
|
|
|
};
|
2020-02-06 13:41:37 +01:00
|
|
|
providers.hir_crate = |tcx, _| tcx.hir_map.untracked_krate();
|
2020-01-02 03:39:11 +01:00
|
|
|
map::provide(providers);
|
|
|
|
}
|