2018-02-10 16:28:17 -06:00
|
|
|
use rustc::hir::itemlikevisit::ItemLikeVisitor;
|
|
|
|
use rustc::hir;
|
|
|
|
use rustc::middle::cstore::ForeignModule;
|
|
|
|
use rustc::ty::TyCtxt;
|
|
|
|
|
|
|
|
pub fn collect<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> Vec<ForeignModule> {
|
|
|
|
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);
|
2018-02-10 16:28:17 -06:00
|
|
|
return collector.modules
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Collector<'a, 'tcx: 'a> {
|
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
modules: Vec<ForeignModule>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx> ItemLikeVisitor<'tcx> for Collector<'a, 'tcx> {
|
|
|
|
fn visit_item(&mut self, it: &'tcx hir::Item) {
|
|
|
|
let fm = match it.node {
|
2018-07-11 10:36:06 -05:00
|
|
|
hir::ItemKind::ForeignMod(ref fm) => fm,
|
2018-02-10 16:28:17 -06:00
|
|
|
_ => return,
|
|
|
|
};
|
|
|
|
|
|
|
|
let foreign_items = fm.items.iter()
|
2019-02-27 09:12:35 -06:00
|
|
|
.map(|it| self.tcx.hir().local_def_id_from_hir_id(it.hir_id))
|
2018-02-10 16:28:17 -06:00
|
|
|
.collect();
|
|
|
|
self.modules.push(ForeignModule {
|
|
|
|
foreign_items,
|
2019-02-27 10:35:24 -06:00
|
|
|
def_id: self.tcx.hir().local_def_id_from_hir_id(it.hir_id),
|
2018-02-10 16:28:17 -06:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_trait_item(&mut self, _it: &'tcx hir::TraitItem) {}
|
|
|
|
fn visit_impl_item(&mut self, _it: &'tcx hir::ImplItem) {}
|
|
|
|
}
|