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.
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 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;
18 use trans_item::TransItem;
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
25 #[derive(Eq, PartialEq, Debug, Copy, Clone)]
26 pub enum SymbolExportLevel {
31 /// The set of symbols exported from each crate in the crate graph.
32 pub struct ExportedSymbols {
33 exports: FxHashMap<CrateNum, Vec<(String, SymbolExportLevel)>>,
36 impl ExportedSymbols {
38 pub fn empty() -> ExportedSymbols {
44 pub fn compute_from<'a, 'tcx>(scx: &SharedCrateContext<'a, 'tcx>,
45 symbol_map: &SymbolMap<'tcx>)
47 let mut local_crate: Vec<_> = scx
51 scx.tcx().map.local_def_id(node_id)
54 let name = symbol_for_def_id(scx, def_id, symbol_map);
55 let export_level = export_level(scx, def_id);
56 debug!("EXPORTED SYMBOL (local): {} ({:?})", name, export_level);
61 if scx.sess().entry_fn.borrow().is_some() {
62 local_crate.push(("main".to_string(), SymbolExportLevel::C));
65 if let Some(id) = scx.sess().derive_registrar_fn.get() {
66 let svh = &scx.link_meta().crate_hash;
67 let def_id = scx.tcx().map.local_def_id(id);
68 let idx = def_id.index;
69 let registrar = scx.sess().generate_derive_registrar_symbol(svh, idx);
70 local_crate.push((registrar, SymbolExportLevel::C));
73 if scx.sess().crate_types.borrow().contains(&config::CrateTypeDylib) {
74 local_crate.push((scx.metadata_symbol_name(),
75 SymbolExportLevel::Rust));
78 let mut exports = FxHashMap();
79 exports.insert(LOCAL_CRATE, local_crate);
81 for cnum in scx.sess().cstore.crates() {
82 debug_assert!(cnum != LOCAL_CRATE);
84 if scx.sess().cstore.plugin_registrar_fn(cnum).is_some() ||
85 scx.sess().cstore.derive_registrar_fn(cnum).is_some() {
89 let crate_exports = scx
92 .exported_symbols(cnum)
95 let name = Instance::mono(scx, def_id).symbol_name(scx);
96 let export_level = export_level(scx, def_id);
97 debug!("EXPORTED SYMBOL (re-export): {} ({:?})", name, export_level);
102 exports.insert(cnum, crate_exports);
105 return ExportedSymbols {
109 fn export_level(scx: &SharedCrateContext,
111 -> SymbolExportLevel {
112 let attrs = scx.tcx().get_attrs(sym_def_id);
113 if attr::contains_extern_indicator(scx.sess().diagnostic(), &attrs) {
116 SymbolExportLevel::Rust
121 pub fn exported_symbols(&self,
123 -> &[(String, SymbolExportLevel)] {
124 match self.exports.get(&cnum) {
125 Some(exports) => &exports[..],
130 pub fn for_each_exported_symbol<F>(&self,
132 export_threshold: SymbolExportLevel,
134 where F: FnMut(&str, SymbolExportLevel)
136 for &(ref name, export_level) in self.exported_symbols(cnum) {
137 if is_below_threshold(export_level, export_threshold) {
138 f(&name[..], export_level)
144 pub fn crate_export_threshold(crate_type: config::CrateType)
145 -> SymbolExportLevel {
147 config::CrateTypeExecutable |
148 config::CrateTypeStaticlib |
149 config::CrateTypeProcMacro |
150 config::CrateTypeCdylib => SymbolExportLevel::C,
151 config::CrateTypeRlib |
152 config::CrateTypeMetadata |
153 config::CrateTypeDylib => SymbolExportLevel::Rust,
157 pub fn crates_export_threshold(crate_types: &[config::CrateType])
158 -> SymbolExportLevel {
159 if crate_types.iter().any(|&crate_type| {
160 crate_export_threshold(crate_type) == SymbolExportLevel::Rust
162 SymbolExportLevel::Rust
168 pub fn is_below_threshold(level: SymbolExportLevel,
169 threshold: SymbolExportLevel)
171 if threshold == SymbolExportLevel::Rust {
172 // We export everything from Rust dylibs
175 level == SymbolExportLevel::C
179 fn symbol_for_def_id<'a, 'tcx>(scx: &SharedCrateContext<'a, 'tcx>,
181 symbol_map: &SymbolMap<'tcx>)
183 // Just try to look things up in the symbol map. If nothing's there, we
185 if let Some(node_id) = scx.tcx().map.as_local_node_id(def_id) {
186 if let Some(sym) = symbol_map.get(TransItem::Static(node_id)) {
187 return sym.to_owned();
191 let instance = Instance::mono(scx, def_id);
193 symbol_map.get(TransItem::Fn(instance))
195 .unwrap_or_else(|| instance.symbol_name(scx))