]> git.lizzy.rs Git - rust.git/blob - src/librustc_trans/back/symbol_export.rs
Optimized error reporting for recursive requirements #47720
[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 std::rc::Rc;
12 use std::sync::Arc;
13
14 use base;
15 use monomorphize::Instance;
16 use rustc::hir::def_id::CrateNum;
17 use rustc::hir::def_id::{DefId, LOCAL_CRATE};
18 use rustc::middle::exported_symbols::SymbolExportLevel;
19 use rustc::session::config;
20 use rustc::ty::TyCtxt;
21 use rustc::ty::maps::Providers;
22 use rustc::util::nodemap::FxHashMap;
23 use rustc_allocator::ALLOCATOR_METHODS;
24 use rustc_back::LinkerFlavor;
25 use syntax::attr;
26
27 pub type ExportedSymbols = FxHashMap<
28     CrateNum,
29     Arc<Vec<(String, Option<DefId>, SymbolExportLevel)>>,
30 >;
31
32 pub fn threshold(tcx: TyCtxt) -> SymbolExportLevel {
33     crates_export_threshold(&tcx.sess.crate_types.borrow())
34 }
35
36 pub fn metadata_symbol_name(tcx: TyCtxt) -> String {
37     format!("rust_metadata_{}_{}",
38             tcx.crate_name(LOCAL_CRATE),
39             tcx.crate_disambiguator(LOCAL_CRATE).to_fingerprint().to_hex())
40 }
41
42 fn crate_export_threshold(crate_type: config::CrateType) -> SymbolExportLevel {
43     match crate_type {
44         config::CrateTypeExecutable |
45         config::CrateTypeStaticlib  |
46         config::CrateTypeProcMacro  |
47         config::CrateTypeCdylib     => SymbolExportLevel::C,
48         config::CrateTypeRlib       |
49         config::CrateTypeDylib      => SymbolExportLevel::Rust,
50     }
51 }
52
53 pub fn crates_export_threshold(crate_types: &[config::CrateType])
54                                       -> SymbolExportLevel {
55     if crate_types.iter().any(|&crate_type| {
56         crate_export_threshold(crate_type) == SymbolExportLevel::Rust
57     }) {
58         SymbolExportLevel::Rust
59     } else {
60         SymbolExportLevel::C
61     }
62 }
63
64 pub fn provide(providers: &mut Providers) {
65     providers.exported_symbol_ids = |tcx, cnum| {
66         let export_threshold = threshold(tcx);
67         Rc::new(tcx.exported_symbols(cnum)
68             .iter()
69             .filter_map(|&(_, id, level)| {
70                 id.and_then(|id| {
71                     if level.is_below_threshold(export_threshold) {
72                         Some(id)
73                     } else {
74                         None
75                     }
76                 })
77             })
78             .collect())
79     };
80
81     providers.is_exported_symbol = |tcx, id| {
82         tcx.exported_symbol_ids(id.krate).contains(&id)
83     };
84
85     providers.exported_symbols = |tcx, cnum| {
86         assert_eq!(cnum, LOCAL_CRATE);
87         let local_exported_symbols = base::find_exported_symbols(tcx);
88
89         let mut local_crate: Vec<_> = local_exported_symbols
90             .iter()
91             .map(|&node_id| {
92                 tcx.hir.local_def_id(node_id)
93             })
94             .map(|def_id| {
95                 let name = tcx.symbol_name(Instance::mono(tcx, def_id));
96                 let export_level = export_level(tcx, def_id);
97                 debug!("EXPORTED SYMBOL (local): {} ({:?})", name, export_level);
98                 (str::to_owned(&name), Some(def_id), export_level)
99             })
100             .collect();
101
102         if let Some(_) = *tcx.sess.entry_fn.borrow() {
103             local_crate.push(("main".to_string(),
104                               None,
105                               SymbolExportLevel::C));
106         }
107
108         if tcx.sess.allocator_kind.get().is_some() {
109             for method in ALLOCATOR_METHODS {
110                 local_crate.push((format!("__rust_{}", method.name),
111                                   None,
112                                   SymbolExportLevel::Rust));
113             }
114         }
115
116         if let Some(id) = tcx.sess.derive_registrar_fn.get() {
117             let def_id = tcx.hir.local_def_id(id);
118             let disambiguator = tcx.sess.local_crate_disambiguator();
119             let registrar = tcx.sess.generate_derive_registrar_symbol(disambiguator);
120             local_crate.push((registrar, Some(def_id), SymbolExportLevel::C));
121         }
122
123         if tcx.sess.crate_types.borrow().contains(&config::CrateTypeDylib) {
124             local_crate.push((metadata_symbol_name(tcx),
125                               None,
126                               SymbolExportLevel::Rust));
127         }
128
129         // Sort so we get a stable incr. comp. hash.
130         local_crate.sort_unstable_by(|&(ref name1, ..), &(ref name2, ..)| {
131             name1.cmp(name2)
132         });
133
134         Arc::new(local_crate)
135     };
136 }
137
138 pub fn provide_extern(providers: &mut Providers) {
139     providers.exported_symbols = |tcx, cnum| {
140         // If this crate is a plugin and/or a custom derive crate, then
141         // we're not even going to link those in so we skip those crates.
142         if tcx.plugin_registrar_fn(cnum).is_some() ||
143            tcx.derive_registrar_fn(cnum).is_some() {
144             return Arc::new(Vec::new())
145         }
146
147         // Check to see if this crate is a "special runtime crate". These
148         // crates, implementation details of the standard library, typically
149         // have a bunch of `pub extern` and `#[no_mangle]` functions as the
150         // ABI between them. We don't want their symbols to have a `C`
151         // export level, however, as they're just implementation details.
152         // Down below we'll hardwire all of the symbols to the `Rust` export
153         // level instead.
154         let special_runtime_crate =
155             tcx.is_panic_runtime(cnum) || tcx.is_compiler_builtins(cnum);
156
157         // Dealing with compiler-builtins and wasm right now is super janky.
158         // There's no linker! As a result we need all of the compiler-builtins
159         // exported symbols to make their way through all the way to the end of
160         // compilation. We want to make sure that LLVM doesn't remove them as
161         // well because we may or may not need them in the final output
162         // artifact. For now just force them to always get exported at the C
163         // layer, and we'll worry about gc'ing them later.
164         let compiler_builtins_and_binaryen =
165             tcx.is_compiler_builtins(cnum) &&
166             tcx.sess.linker_flavor() == LinkerFlavor::Binaryen;
167
168         let mut crate_exports: Vec<_> = tcx
169             .exported_symbol_ids(cnum)
170             .iter()
171             .map(|&def_id| {
172                 let name = tcx.symbol_name(Instance::mono(tcx, def_id));
173                 let export_level = if compiler_builtins_and_binaryen &&
174                                       tcx.contains_extern_indicator(def_id) {
175                     SymbolExportLevel::C
176                 } else if special_runtime_crate {
177                     // We can probably do better here by just ensuring that
178                     // it has hidden visibility rather than public
179                     // visibility, as this is primarily here to ensure it's
180                     // not stripped during LTO.
181                     //
182                     // In general though we won't link right if these
183                     // symbols are stripped, and LTO currently strips them.
184                     if &*name == "rust_eh_personality" ||
185                        &*name == "rust_eh_register_frames" ||
186                        &*name == "rust_eh_unregister_frames" {
187                         SymbolExportLevel::C
188                     } else {
189                         SymbolExportLevel::Rust
190                     }
191                 } else {
192                     export_level(tcx, def_id)
193                 };
194                 debug!("EXPORTED SYMBOL (re-export): {} ({:?})", name, export_level);
195                 (str::to_owned(&name), Some(def_id), export_level)
196             })
197             .collect();
198
199         // Sort so we get a stable incr. comp. hash.
200         crate_exports.sort_unstable_by(|&(ref name1, ..), &(ref name2, ..)| {
201             name1.cmp(name2)
202         });
203
204         Arc::new(crate_exports)
205     };
206 }
207
208 fn export_level(tcx: TyCtxt, sym_def_id: DefId) -> SymbolExportLevel {
209     // We export anything that's not mangled at the "C" layer as it probably has
210     // to do with ABI concerns. We do not, however, apply such treatment to
211     // special symbols in the standard library for various plumbing between
212     // core/std/allocators/etc. For example symbols used to hook up allocation
213     // are not considered for export
214     let is_extern = tcx.contains_extern_indicator(sym_def_id);
215     let std_internal = attr::contains_name(&tcx.get_attrs(sym_def_id),
216                                            "rustc_std_internal_symbol");
217     if is_extern && !std_internal {
218         SymbolExportLevel::C
219     } else {
220         SymbolExportLevel::Rust
221     }
222 }