1pub mod map;
6pub mod nested_filter;
7pub mod place;
8
9use rustc_data_structures::fingerprint::Fingerprint;
10use rustc_data_structures::sorted_map::SortedMap;
11use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
12use rustc_data_structures::sync::{DynSend, DynSync, try_par_for_each_in};
13use rustc_hir::def::DefKind;
14use rustc_hir::def_id::{DefId, LocalDefId, LocalModDefId};
15use rustc_hir::lints::DelayedLint;
16use rustc_hir::*;
17use rustc_macros::{Decodable, Encodable, HashStable};
18use rustc_span::{ErrorGuaranteed, ExpnId, Span};
19
20use crate::query::Providers;
21use crate::ty::{EarlyBinder, ImplSubject, TyCtxt};
22
23#[derive(Debug, HashStable, Encodable, Decodable)]
26pub struct ModuleItems {
27    add_root: bool,
30    submodules: Box<[OwnerId]>,
31    free_items: Box<[ItemId]>,
32    trait_items: Box<[TraitItemId]>,
33    impl_items: Box<[ImplItemId]>,
34    foreign_items: Box<[ForeignItemId]>,
35    opaques: Box<[LocalDefId]>,
36    body_owners: Box<[LocalDefId]>,
37    nested_bodies: Box<[LocalDefId]>,
38    delayed_lint_items: Box<[OwnerId]>,
40}
41
42impl ModuleItems {
43    pub fn free_items(&self) -> impl Iterator<Item = ItemId> {
50        self.free_items.iter().copied()
51    }
52
53    pub fn trait_items(&self) -> impl Iterator<Item = TraitItemId> {
54        self.trait_items.iter().copied()
55    }
56
57    pub fn delayed_lint_items(&self) -> impl Iterator<Item = OwnerId> {
58        self.delayed_lint_items.iter().copied()
59    }
60
61    pub fn impl_items(&self) -> impl Iterator<Item = ImplItemId> {
64        self.impl_items.iter().copied()
65    }
66
67    pub fn foreign_items(&self) -> impl Iterator<Item = ForeignItemId> {
68        self.foreign_items.iter().copied()
69    }
70
71    pub fn owners(&self) -> impl Iterator<Item = OwnerId> {
72        self.add_root
73            .then_some(CRATE_OWNER_ID)
74            .into_iter()
75            .chain(self.free_items.iter().map(|id| id.owner_id))
76            .chain(self.trait_items.iter().map(|id| id.owner_id))
77            .chain(self.impl_items.iter().map(|id| id.owner_id))
78            .chain(self.foreign_items.iter().map(|id| id.owner_id))
79    }
80
81    pub fn opaques(&self) -> impl Iterator<Item = LocalDefId> {
82        self.opaques.iter().copied()
83    }
84
85    pub fn nested_bodies(&self) -> impl Iterator<Item = LocalDefId> {
87        self.nested_bodies.iter().copied()
88    }
89
90    pub fn definitions(&self) -> impl Iterator<Item = LocalDefId> {
91        self.owners().map(|id| id.def_id)
92    }
93
94    pub fn par_nested_bodies(
96        &self,
97        f: impl Fn(LocalDefId) -> Result<(), ErrorGuaranteed> + DynSend + DynSync,
98    ) -> Result<(), ErrorGuaranteed> {
99        try_par_for_each_in(&self.nested_bodies[..], |&&id| f(id))
100    }
101
102    pub fn par_items(
103        &self,
104        f: impl Fn(ItemId) -> Result<(), ErrorGuaranteed> + DynSend + DynSync,
105    ) -> Result<(), ErrorGuaranteed> {
106        try_par_for_each_in(&self.free_items[..], |&&id| f(id))
107    }
108
109    pub fn par_trait_items(
110        &self,
111        f: impl Fn(TraitItemId) -> Result<(), ErrorGuaranteed> + DynSend + DynSync,
112    ) -> Result<(), ErrorGuaranteed> {
113        try_par_for_each_in(&self.trait_items[..], |&&id| f(id))
114    }
115
116    pub fn par_impl_items(
117        &self,
118        f: impl Fn(ImplItemId) -> Result<(), ErrorGuaranteed> + DynSend + DynSync,
119    ) -> Result<(), ErrorGuaranteed> {
120        try_par_for_each_in(&self.impl_items[..], |&&id| f(id))
121    }
122
123    pub fn par_foreign_items(
124        &self,
125        f: impl Fn(ForeignItemId) -> Result<(), ErrorGuaranteed> + DynSend + DynSync,
126    ) -> Result<(), ErrorGuaranteed> {
127        try_par_for_each_in(&self.foreign_items[..], |&&id| f(id))
128    }
129
130    pub fn par_opaques(
131        &self,
132        f: impl Fn(LocalDefId) -> Result<(), ErrorGuaranteed> + DynSend + DynSync,
133    ) -> Result<(), ErrorGuaranteed> {
134        try_par_for_each_in(&self.opaques[..], |&&id| f(id))
135    }
136}
137
138impl<'tcx> TyCtxt<'tcx> {
139    pub fn parent_module(self, id: HirId) -> LocalModDefId {
140        if !id.is_owner() && self.def_kind(id.owner) == DefKind::Mod {
141            LocalModDefId::new_unchecked(id.owner.def_id)
142        } else {
143            self.parent_module_from_def_id(id.owner.def_id)
144        }
145    }
146
147    pub fn parent_module_from_def_id(self, mut id: LocalDefId) -> LocalModDefId {
148        while let Some(parent) = self.opt_local_parent(id) {
149            id = parent;
150            if self.def_kind(id) == DefKind::Mod {
151                break;
152            }
153        }
154        LocalModDefId::new_unchecked(id)
155    }
156
157    pub fn impl_subject(self, def_id: DefId) -> EarlyBinder<'tcx, ImplSubject<'tcx>> {
158        match self.impl_trait_ref(def_id) {
159            Some(t) => t.map_bound(ImplSubject::Trait),
160            None => self.type_of(def_id).map_bound(ImplSubject::Inherent),
161        }
162    }
163
164    pub fn is_foreign_item(self, def_id: impl Into<DefId>) -> bool {
166        self.opt_parent(def_id.into())
167            .is_some_and(|parent| matches!(self.def_kind(parent), DefKind::ForeignMod))
168    }
169
170    pub fn hash_owner_nodes(
171        self,
172        node: OwnerNode<'_>,
173        bodies: &SortedMap<ItemLocalId, &Body<'_>>,
174        attrs: &SortedMap<ItemLocalId, &[Attribute]>,
175        delayed_lints: &[DelayedLint],
176        define_opaque: Option<&[(Span, LocalDefId)]>,
177    ) -> Hashes {
178        if !self.needs_crate_hash() {
179            return Hashes {
180                opt_hash_including_bodies: None,
181                attrs_hash: None,
182                delayed_lints_hash: None,
183            };
184        }
185
186        self.with_stable_hashing_context(|mut hcx| {
187            let mut stable_hasher = StableHasher::new();
188            node.hash_stable(&mut hcx, &mut stable_hasher);
189            bodies.hash_stable(&mut hcx, &mut stable_hasher);
191            let h1 = stable_hasher.finish();
192
193            let mut stable_hasher = StableHasher::new();
194            attrs.hash_stable(&mut hcx, &mut stable_hasher);
195
196            define_opaque.hash_stable(&mut hcx, &mut stable_hasher);
198
199            let h2 = stable_hasher.finish();
200
201            let mut stable_hasher = StableHasher::new();
203            delayed_lints.hash_stable(&mut hcx, &mut stable_hasher);
204            let h3 = stable_hasher.finish();
205
206            Hashes {
207                opt_hash_including_bodies: Some(h1),
208                attrs_hash: Some(h2),
209                delayed_lints_hash: Some(h3),
210            }
211        })
212    }
213}
214
215#[derive(Clone, Copy, Debug)]
217pub struct Hashes {
218    pub opt_hash_including_bodies: Option<Fingerprint>,
219    pub attrs_hash: Option<Fingerprint>,
220    pub delayed_lints_hash: Option<Fingerprint>,
221}
222
223pub fn provide(providers: &mut Providers) {
224    providers.hir_crate_items = map::hir_crate_items;
225    providers.crate_hash = map::crate_hash;
226    providers.hir_module_items = map::hir_module_items;
227    providers.local_def_id_to_hir_id = |tcx, def_id| match tcx.hir_crate(()).owners[def_id] {
228        MaybeOwner::Owner(_) => HirId::make_owner(def_id),
229        MaybeOwner::NonOwner(hir_id) => hir_id,
230        MaybeOwner::Phantom => bug!("No HirId for {:?}", def_id),
231    };
232    providers.opt_hir_owner_nodes =
233        |tcx, id| tcx.hir_crate(()).owners.get(id)?.as_owner().map(|i| &i.nodes);
234    providers.hir_owner_parent = |tcx, owner_id| {
235        tcx.opt_local_parent(owner_id.def_id).map_or(CRATE_HIR_ID, |parent_def_id| {
236            let parent_owner_id = tcx.local_def_id_to_hir_id(parent_def_id).owner;
237            HirId {
238                owner: parent_owner_id,
239                local_id: tcx.hir_crate(()).owners[parent_owner_id.def_id]
240                    .unwrap()
241                    .parenting
242                    .get(&owner_id.def_id)
243                    .copied()
244                    .unwrap_or(ItemLocalId::ZERO),
245            }
246        })
247    };
248    providers.hir_attr_map = |tcx, id| {
249        tcx.hir_crate(()).owners[id.def_id].as_owner().map_or(AttributeMap::EMPTY, |o| &o.attrs)
250    };
251    providers.opt_ast_lowering_delayed_lints =
252        |tcx, id| tcx.hir_crate(()).owners[id.def_id].as_owner().map(|o| &o.delayed_lints);
253    providers.def_span = |tcx, def_id| tcx.hir_span(tcx.local_def_id_to_hir_id(def_id));
254    providers.def_ident_span = |tcx, def_id| {
255        let hir_id = tcx.local_def_id_to_hir_id(def_id);
256        tcx.hir_opt_ident_span(hir_id)
257    };
258    providers.ty_span = |tcx, def_id| {
259        let node = tcx.hir_node_by_def_id(def_id);
260        match node.ty() {
261            Some(ty) => ty.span,
262            None => bug!("{def_id:?} doesn't have a type: {node:#?}"),
263        }
264    };
265    providers.fn_arg_idents = |tcx, def_id| {
266        let node = tcx.hir_node_by_def_id(def_id);
267        if let Some(body_id) = node.body_id() {
268            tcx.arena.alloc_from_iter(tcx.hir_body_param_idents(body_id))
269        } else if let Node::TraitItem(&TraitItem {
270            kind: TraitItemKind::Fn(_, TraitFn::Required(idents)),
271            ..
272        })
273        | Node::ForeignItem(&ForeignItem {
274            kind: ForeignItemKind::Fn(_, idents, _),
275            ..
276        }) = node
277        {
278            idents
279        } else {
280            span_bug!(
281                tcx.hir_span(tcx.local_def_id_to_hir_id(def_id)),
282                "fn_arg_idents: unexpected item {:?}",
283                def_id
284            );
285        }
286    };
287    providers.all_local_trait_impls = |tcx, ()| &tcx.resolutions(()).trait_impls;
288    providers.local_trait_impls =
289        |tcx, trait_id| tcx.resolutions(()).trait_impls.get(&trait_id).map_or(&[], |xs| &xs[..]);
290    providers.expn_that_defined =
291        |tcx, id| tcx.resolutions(()).expn_that_defined.get(&id).copied().unwrap_or(ExpnId::root());
292    providers.in_scope_traits_map = |tcx, id| {
293        tcx.hir_crate(()).owners[id.def_id].as_owner().map(|owner_info| &owner_info.trait_map)
294    };
295}