2020-01-04 19:37:57 -06:00
|
|
|
use rustc_hir as hir;
|
|
|
|
use rustc_hir::itemlikevisit::ItemLikeVisitor;
|
2020-03-29 10:19:48 -05:00
|
|
|
use rustc_middle::ty::TyCtxt;
|
2020-11-13 20:02:03 -06:00
|
|
|
use rustc_session::cstore::ForeignModule;
|
2018-02-10 16:28:17 -06:00
|
|
|
|
2019-10-06 06:30:46 -05:00
|
|
|
crate fn collect(tcx: TyCtxt<'_>) -> Vec<ForeignModule> {
|
2022-04-06 18:04:15 -05:00
|
|
|
let mut modules = Vec::new();
|
|
|
|
for id in tcx.hir().items() {
|
|
|
|
let item = tcx.hir().item(id);
|
|
|
|
let hir::ItemKind::ForeignMod { items, .. } = item.kind else {
|
|
|
|
continue;
|
|
|
|
};
|
|
|
|
let foreign_items = items.iter().map(|it| it.id.def_id.to_def_id()).collect();
|
|
|
|
modules.push(ForeignModule { foreign_items, def_id: id.def_id.to_def_id() });
|
|
|
|
}
|
|
|
|
modules
|
2018-02-10 16:28:17 -06:00
|
|
|
}
|
|
|
|
|
2021-01-31 17:33:38 -06:00
|
|
|
struct Collector {
|
2018-02-10 16:28:17 -06:00
|
|
|
modules: Vec<ForeignModule>,
|
|
|
|
}
|
|
|
|
|
2021-12-14 11:35:27 -06:00
|
|
|
impl<'tcx> ItemLikeVisitor<'tcx> for Collector {
|
2019-11-28 12:28:50 -06:00
|
|
|
fn visit_item(&mut self, it: &'tcx hir::Item<'tcx>) {
|
2022-02-18 17:48:49 -06:00
|
|
|
let hir::ItemKind::ForeignMod { items, .. } = it.kind else {
|
|
|
|
return;
|
2018-02-10 16:28:17 -06:00
|
|
|
};
|
|
|
|
|
2021-01-31 17:33:38 -06:00
|
|
|
let foreign_items = items.iter().map(|it| it.id.def_id.to_def_id()).collect();
|
2021-01-30 10:47:51 -06:00
|
|
|
self.modules.push(ForeignModule { foreign_items, def_id: it.def_id.to_def_id() });
|
2018-02-10 16:28:17 -06:00
|
|
|
}
|
|
|
|
|
2019-11-28 14:47:10 -06:00
|
|
|
fn visit_trait_item(&mut self, _it: &'tcx hir::TraitItem<'tcx>) {}
|
2019-11-28 15:16:44 -06:00
|
|
|
fn visit_impl_item(&mut self, _it: &'tcx hir::ImplItem<'tcx>) {}
|
2020-11-11 14:57:54 -06:00
|
|
|
fn visit_foreign_item(&mut self, _it: &'tcx hir::ForeignItem<'tcx>) {}
|
2018-02-10 16:28:17 -06:00
|
|
|
}
|