1 //! HIR datatypes. See the [rustc dev guide] for more info.
3 //! [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/hir.html
9 use crate::ich
::StableHashingContext
;
10 use crate::ty
::query
::Providers
;
11 use crate::ty
::TyCtxt
;
12 use rustc_data_structures
::fingerprint
::Fingerprint
;
13 use rustc_data_structures
::fx
::FxHashMap
;
14 use rustc_data_structures
::stable_hasher
::{HashStable, StableHasher}
;
15 use rustc_hir
::def_id
::{LocalDefId, LOCAL_CRATE}
;
17 use rustc_index
::vec
::IndexVec
;
19 pub struct Owner
<'tcx
> {
24 impl<'a
, 'tcx
> HashStable
<StableHashingContext
<'a
>> for Owner
<'tcx
> {
25 fn hash_stable(&self, hcx
: &mut StableHashingContext
<'a
>, hasher
: &mut StableHasher
) {
26 let Owner { parent, node }
= self;
27 hcx
.while_hashing_hir_bodies(false, |hcx
| {
28 parent
.hash_stable(hcx
, hasher
);
29 node
.hash_stable(hcx
, hasher
);
35 pub struct ParentedNode
<'tcx
> {
40 pub struct OwnerNodes
<'tcx
> {
42 nodes
: IndexVec
<ItemLocalId
, Option
<ParentedNode
<'tcx
>>>,
43 bodies
: FxHashMap
<ItemLocalId
, &'tcx Body
<'tcx
>>,
46 impl<'a
, 'tcx
> HashStable
<StableHashingContext
<'a
>> for OwnerNodes
<'tcx
> {
47 fn hash_stable(&self, hcx
: &mut StableHashingContext
<'a
>, hasher
: &mut StableHasher
) {
48 // We ignore the `nodes` and `bodies` fields since these refer to information included in
49 // `hash` which is hashed in the collector and used for the crate hash.
50 let OwnerNodes { hash, nodes: _, bodies: _ }
= *self;
51 hash
.hash_stable(hcx
, hasher
);
55 impl<'tcx
> TyCtxt
<'tcx
> {
57 pub fn hir(self) -> map
::Map
<'tcx
> {
58 map
::Map { tcx: self }
61 pub fn parent_module(self, id
: HirId
) -> LocalDefId
{
62 self.parent_module_from_def_id(id
.owner
)
66 pub fn provide(providers
: &mut Providers
) {
67 providers
.parent_module_from_def_id
= |tcx
, id
| {
69 hir
.local_def_id(hir
.get_module_parent_node(hir
.local_def_id_to_hir_id(id
)))
71 providers
.hir_crate
= |tcx
, _
| tcx
.untracked_crate
;
72 providers
.index_hir
= map
::index_hir
;
73 providers
.hir_module_items
= |tcx
, id
| {
75 let module
= hir
.local_def_id_to_hir_id(id
);
76 &tcx
.untracked_crate
.modules
[&module
]
78 providers
.hir_owner
= |tcx
, id
| tcx
.index_hir(LOCAL_CRATE
).map
[id
].signature
;
79 providers
.hir_owner_nodes
= |tcx
, id
| tcx
.index_hir(LOCAL_CRATE
).map
[id
].with_bodies
.as_deref();
80 providers
.fn_arg_names
= |tcx
, id
| {
82 let hir_id
= hir
.local_def_id_to_hir_id(id
.expect_local());
83 if let Some(body_id
) = hir
.maybe_body_owned_by(hir_id
) {
84 tcx
.arena
.alloc_from_iter(hir
.body_param_names(body_id
))
85 } else if let Node
::TraitItem(&TraitItem
{
86 kind
: TraitItemKind
::Fn(_
, TraitFn
::Required(idents
)),
90 tcx
.arena
.alloc_slice(idents
)
92 span_bug
!(hir
.span(hir_id
), "fn_arg_names: unexpected item {:?}", id
);
95 map
::provide(providers
);