2018-02-10 16:28:17 -06:00
|
|
|
use rustc::middle::cstore::ForeignModule;
|
|
|
|
use rustc::ty::TyCtxt;
|
2020-01-04 19:37:57 -06:00
|
|
|
use rustc_hir as hir;
|
|
|
|
use rustc_hir::itemlikevisit::ItemLikeVisitor;
|
2018-02-10 16:28:17 -06:00
|
|
|
|
2019-10-06 06:30:46 -05:00
|
|
|
crate fn collect(tcx: TyCtxt<'_>) -> Vec<ForeignModule> {
|
2019-12-22 16:42:04 -06:00
|
|
|
let mut collector = Collector { tcx, modules: Vec::new() };
|
2018-12-04 06:45:36 -06:00
|
|
|
tcx.hir().krate().visit_all_item_likes(&mut collector);
|
2019-06-11 16:11:55 -05:00
|
|
|
return collector.modules;
|
2018-02-10 16:28:17 -06:00
|
|
|
}
|
|
|
|
|
2019-06-11 14:03:44 -05:00
|
|
|
struct Collector<'tcx> {
|
2019-06-13 16:48:52 -05:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2018-02-10 16:28:17 -06:00
|
|
|
modules: Vec<ForeignModule>,
|
|
|
|
}
|
|
|
|
|
2019-06-11 14:03:44 -05:00
|
|
|
impl ItemLikeVisitor<'tcx> for Collector<'tcx> {
|
2019-11-28 12:28:50 -06:00
|
|
|
fn visit_item(&mut self, it: &'tcx hir::Item<'tcx>) {
|
2019-09-26 11:51:36 -05:00
|
|
|
let fm = match it.kind {
|
2018-07-11 10:36:06 -05:00
|
|
|
hir::ItemKind::ForeignMod(ref fm) => fm,
|
2018-02-10 16:28:17 -06:00
|
|
|
_ => return,
|
|
|
|
};
|
|
|
|
|
2019-12-22 16:42:04 -06:00
|
|
|
let foreign_items =
|
|
|
|
fm.items.iter().map(|it| self.tcx.hir().local_def_id(it.hir_id)).collect();
|
|
|
|
self.modules
|
|
|
|
.push(ForeignModule { foreign_items, def_id: self.tcx.hir().local_def_id(it.hir_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>) {}
|
2018-02-10 16:28:17 -06:00
|
|
|
}
|