2023-07-15 10:17:37 +00:00
|
|
|
use rustc_data_structures::fx::FxIndexMap;
|
2020-01-05 01:37:57 +00:00
|
|
|
use rustc_hir as hir;
|
2022-04-07 16:29:57 +00:00
|
|
|
use rustc_hir::def::DefKind;
|
2023-07-15 10:09:36 +00:00
|
|
|
use rustc_hir::def_id::DefId;
|
|
|
|
use rustc_middle::query::LocalCrate;
|
2020-03-29 15:19:48 +00:00
|
|
|
use rustc_middle::ty::TyCtxt;
|
2020-11-14 02:02:03 +00:00
|
|
|
use rustc_session::cstore::ForeignModule;
|
2018-02-10 22:28:17 +00:00
|
|
|
|
2023-07-15 10:17:37 +00:00
|
|
|
pub(crate) fn collect(tcx: TyCtxt<'_>, LocalCrate: LocalCrate) -> FxIndexMap<DefId, ForeignModule> {
|
|
|
|
let mut modules = FxIndexMap::default();
|
2023-07-15 10:09:36 +00:00
|
|
|
|
|
|
|
// We need to collect all the `ForeignMod`, even if they are empty.
|
2022-04-06 23:04:15 +00:00
|
|
|
for id in tcx.hir().items() {
|
2022-10-27 03:02:18 +00:00
|
|
|
if !matches!(tcx.def_kind(id.owner_id), DefKind::ForeignMod) {
|
2022-04-06 23:04:15 +00:00
|
|
|
continue;
|
2022-04-07 16:29:57 +00:00
|
|
|
}
|
2023-07-15 10:09:36 +00:00
|
|
|
|
|
|
|
let def_id = id.owner_id.to_def_id();
|
2022-04-07 16:29:57 +00:00
|
|
|
let item = tcx.hir().item(id);
|
2023-07-15 10:09:36 +00:00
|
|
|
|
2023-07-15 10:17:37 +00:00
|
|
|
if let hir::ItemKind::ForeignMod { abi, items } = item.kind {
|
2022-10-27 03:02:18 +00:00
|
|
|
let foreign_items = items.iter().map(|it| it.id.owner_id.to_def_id()).collect();
|
2023-07-15 10:17:37 +00:00
|
|
|
modules.insert(def_id, ForeignModule { def_id, abi, foreign_items });
|
2022-04-07 16:29:57 +00:00
|
|
|
}
|
2022-04-06 23:04:15 +00:00
|
|
|
}
|
2023-07-15 10:09:36 +00:00
|
|
|
|
2022-04-06 23:04:15 +00:00
|
|
|
modules
|
2018-02-10 22:28:17 +00:00
|
|
|
}
|