]> git.lizzy.rs Git - rust.git/blob - src/librustc_metadata/foreign_modules.rs
Rollup merge of #56641 - GuillaumeGomez:span-trait-method-invalid-nb-parameters,...
[rust.git] / src / librustc_metadata / foreign_modules.rs
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.
4 //
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.
10
11 use rustc::hir::itemlikevisit::ItemLikeVisitor;
12 use rustc::hir;
13 use rustc::middle::cstore::ForeignModule;
14 use rustc::ty::TyCtxt;
15
16 pub fn collect<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> Vec<ForeignModule> {
17     let mut collector = Collector {
18         tcx,
19         modules: Vec::new(),
20     };
21     tcx.hir().krate().visit_all_item_likes(&mut collector);
22     return collector.modules
23 }
24
25 struct Collector<'a, 'tcx: 'a> {
26     tcx: TyCtxt<'a, 'tcx, 'tcx>,
27     modules: Vec<ForeignModule>,
28 }
29
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,
34             _ => return,
35         };
36
37         let foreign_items = fm.items.iter()
38             .map(|it| self.tcx.hir().local_def_id(it.id))
39             .collect();
40         self.modules.push(ForeignModule {
41             foreign_items,
42             def_id: self.tcx.hir().local_def_id(it.id),
43         });
44     }
45
46     fn visit_trait_item(&mut self, _it: &'tcx hir::TraitItem) {}
47     fn visit_impl_item(&mut self, _it: &'tcx hir::ImplItem) {}
48 }