]> git.lizzy.rs Git - rust.git/blob - src/librustc_trans/back/symbol_export.rs
Refactor symbol export list generation.
[rust.git] / src / librustc_trans / back / symbol_export.rs
1 // Copyright 2016 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 context::SharedCrateContext;
12 use monomorphize::Instance;
13 use symbol_map::SymbolMap;
14 use util::nodemap::FxHashMap;
15 use rustc::hir::def_id::{DefId, CrateNum, LOCAL_CRATE};
16 use rustc::session::config;
17 use syntax::attr;
18 use trans_item::TransItem;
19
20 /// The SymbolExportLevel of a symbols specifies from which kinds of crates
21 /// the symbol will be exported. `C` symbols will be exported from any
22 /// kind of crate, including cdylibs which export very few things.
23 /// `Rust` will only be exported if the crate produced is a Rust
24 /// dylib.
25 #[derive(Eq, PartialEq, Debug, Copy, Clone)]
26 pub enum SymbolExportLevel {
27     C,
28     Rust,
29 }
30
31 /// The set of symbols exported from each crate in the crate graph.
32 pub struct ExportedSymbols {
33     exports: FxHashMap<CrateNum, Vec<(String, SymbolExportLevel)>>,
34 }
35
36 impl ExportedSymbols {
37
38     pub fn empty() -> ExportedSymbols {
39         ExportedSymbols {
40             exports: FxHashMap(),
41         }
42     }
43
44     pub fn compute_from<'a, 'tcx>(scx: &SharedCrateContext<'a, 'tcx>,
45                                   symbol_map: &SymbolMap<'tcx>)
46                                   -> ExportedSymbols {
47         let mut local_crate: Vec<_> = scx
48             .exported_symbols()
49             .iter()
50             .map(|&node_id| {
51                 scx.tcx().map.local_def_id(node_id)
52             })
53             .map(|def_id| {
54                 (symbol_for_def_id(scx, def_id, symbol_map),
55                  export_level(scx, def_id))
56             })
57             .collect();
58
59         if scx.sess().entry_fn.borrow().is_some() {
60             local_crate.push(("main".to_string(), SymbolExportLevel::C));
61         }
62
63         if scx.sess().crate_types.borrow().contains(&config::CrateTypeDylib) {
64             local_crate.push((scx.metadata_symbol_name(),
65                               SymbolExportLevel::Rust));
66         }
67
68         let mut exports = FxHashMap();
69         exports.insert(LOCAL_CRATE, local_crate);
70
71         for cnum in scx.sess().cstore.crates() {
72             debug_assert!(cnum != LOCAL_CRATE);
73
74             if scx.sess().cstore.plugin_registrar_fn(cnum).is_some() ||
75                scx.sess().cstore.derive_registrar_fn(cnum).is_some() {
76                 continue;
77             }
78
79             let crate_exports = scx
80                 .sess()
81                 .cstore
82                 .exported_symbols(cnum)
83                 .iter()
84                 .map(|&def_id| {
85                     debug!("EXTERN-SYMBOL: {:?}", def_id);
86                     let name = Instance::mono(scx, def_id).symbol_name(scx);
87                     (name, export_level(scx, def_id))
88                 })
89                 .collect();
90
91             exports.insert(cnum, crate_exports);
92         }
93
94         return ExportedSymbols {
95             exports: exports
96         };
97
98         fn export_level(scx: &SharedCrateContext,
99                         sym_def_id: DefId)
100                         -> SymbolExportLevel {
101             let attrs = scx.tcx().get_attrs(sym_def_id);
102             if attr::contains_extern_indicator(scx.sess().diagnostic(), &attrs) {
103                 SymbolExportLevel::C
104             } else {
105                 SymbolExportLevel::Rust
106             }
107         }
108     }
109
110     pub fn exported_symbols(&self,
111                             cnum: CrateNum)
112                             -> &[(String, SymbolExportLevel)] {
113         match self.exports.get(&cnum) {
114             Some(exports) => &exports[..],
115             None => &[]
116         }
117     }
118
119     pub fn for_each_exported_symbol<F>(&self,
120                                        cnum: CrateNum,
121                                        export_threshold: SymbolExportLevel,
122                                        mut f: F)
123         where F: FnMut(&str, SymbolExportLevel)
124     {
125         for &(ref name, export_level) in self.exported_symbols(cnum) {
126             if is_below_threshold(export_level, export_threshold) {
127                 f(&name[..], export_level)
128             }
129         }
130     }
131 }
132
133 pub fn crate_export_threshold(crate_type: config::CrateType)
134                                      -> SymbolExportLevel {
135     match crate_type {
136         config::CrateTypeExecutable |
137         config::CrateTypeStaticlib  |
138         config::CrateTypeCdylib     => SymbolExportLevel::C,
139         config::CrateTypeProcMacro  |
140         config::CrateTypeRlib       |
141         config::CrateTypeMetadata   |
142         config::CrateTypeDylib      => SymbolExportLevel::Rust,
143     }
144 }
145
146 pub fn crates_export_threshold(crate_types: &[config::CrateType])
147                                       -> SymbolExportLevel {
148     if crate_types.iter().any(|&crate_type| {
149         crate_export_threshold(crate_type) == SymbolExportLevel::Rust
150     }) {
151         SymbolExportLevel::Rust
152     } else {
153         SymbolExportLevel::C
154     }
155 }
156
157 pub fn is_below_threshold(level: SymbolExportLevel,
158                           threshold: SymbolExportLevel)
159                           -> bool {
160     if threshold == SymbolExportLevel::Rust {
161         // We export everything from Rust dylibs
162         true
163     } else {
164         level == SymbolExportLevel::C
165     }
166 }
167
168 fn symbol_for_def_id<'a, 'tcx>(scx: &SharedCrateContext<'a, 'tcx>,
169                                def_id: DefId,
170                                symbol_map: &SymbolMap<'tcx>)
171                                -> String {
172     // Just try to look things up in the symbol map. If nothing's there, we
173     // recompute.
174     if let Some(node_id) = scx.tcx().map.as_local_node_id(def_id) {
175         if let Some(sym) = symbol_map.get(TransItem::Static(node_id)) {
176             return sym.to_owned();
177         }
178     }
179
180     let instance = Instance::mono(scx, def_id);
181
182     symbol_map.get(TransItem::Fn(instance))
183               .map(str::to_owned)
184               .unwrap_or_else(|| instance.symbol_name(scx))
185 }