1 // Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 use rustc::hir::itemlikevisit::ItemLikeVisitor;
13 use rustc::middle::cstore::ForeignModule;
14 use rustc::ty::TyCtxt;
16 pub fn collect<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> Vec<ForeignModule> {
17 let mut collector = Collector {
21 tcx.hir().krate().visit_all_item_likes(&mut collector);
22 return collector.modules
25 struct Collector<'a, 'tcx: 'a> {
26 tcx: TyCtxt<'a, 'tcx, 'tcx>,
27 modules: Vec<ForeignModule>,
30 impl<'a, 'tcx> ItemLikeVisitor<'tcx> for Collector<'a, 'tcx> {
31 fn visit_item(&mut self, it: &'tcx hir::Item) {
32 let fm = match it.node {
33 hir::ItemKind::ForeignMod(ref fm) => fm,
37 let foreign_items = fm.items.iter()
38 .map(|it| self.tcx.hir().local_def_id(it.id))
40 self.modules.push(ForeignModule {
42 def_id: self.tcx.hir().local_def_id(it.id),
46 fn visit_trait_item(&mut self, _it: &'tcx hir::TraitItem) {}
47 fn visit_impl_item(&mut self, _it: &'tcx hir::ImplItem) {}