use std::collections::hash_map::Entry::*;
use std::sync::Arc;
-use rustc::ty::Instance;
-use rustc::hir;
-use rustc::hir::Node;
-use rustc::hir::CodegenFnAttrFlags;
-use rustc::hir::def_id::{CrateNum, DefId, LOCAL_CRATE, CRATE_DEF_INDEX};
-use rustc_data_structures::fingerprint::Fingerprint;
-use rustc::middle::exported_symbols::{SymbolExportLevel, ExportedSymbol, metadata_symbol_name};
+use rustc::middle::codegen_fn_attrs::CodegenFnAttrFlags;
+use rustc::middle::exported_symbols::{metadata_symbol_name, ExportedSymbol, SymbolExportLevel};
use rustc::session::config;
-use rustc::ty::{TyCtxt, SymbolName};
use rustc::ty::query::Providers;
use rustc::ty::subst::SubstsRef;
-use rustc::util::nodemap::{FxHashMap, DefIdMap};
+use rustc::ty::Instance;
+use rustc::ty::{SymbolName, TyCtxt};
+use rustc_data_structures::fingerprint::Fingerprint;
+use rustc_data_structures::fx::FxHashMap;
+use rustc_hir as hir;
+use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, CRATE_DEF_INDEX, LOCAL_CRATE};
+use rustc_hir::Node;
use rustc_index::vec::IndexVec;
use syntax::expand::allocator::ALLOCATOR_METHODS;
-pub type ExportedSymbols = FxHashMap<
- CrateNum,
- Arc<Vec<(String, SymbolExportLevel)>>,
->;
+pub type ExportedSymbols = FxHashMap<CrateNum, Arc<Vec<(String, SymbolExportLevel)>>>;
pub fn threshold(tcx: TyCtxt<'_>) -> SymbolExportLevel {
crates_export_threshold(&tcx.sess.crate_types.borrow())
fn crate_export_threshold(crate_type: config::CrateType) -> SymbolExportLevel {
match crate_type {
- config::CrateType::Executable |
- config::CrateType::Staticlib |
- config::CrateType::ProcMacro |
- config::CrateType::Cdylib => SymbolExportLevel::C,
- config::CrateType::Rlib |
- config::CrateType::Dylib => SymbolExportLevel::Rust,
+ config::CrateType::Executable
+ | config::CrateType::Staticlib
+ | config::CrateType::ProcMacro
+ | config::CrateType::Cdylib => SymbolExportLevel::C,
+ config::CrateType::Rlib | config::CrateType::Dylib => SymbolExportLevel::Rust,
}
}
pub fn crates_export_threshold(crate_types: &[config::CrateType]) -> SymbolExportLevel {
- if crate_types.iter().any(|&crate_type|
- crate_export_threshold(crate_type) == SymbolExportLevel::Rust)
+ if crate_types
+ .iter()
+ .any(|&crate_type| crate_export_threshold(crate_type) == SymbolExportLevel::Rust)
{
SymbolExportLevel::Rust
} else {
// export level, however, as they're just implementation details.
// Down below we'll hardwire all of the symbols to the `Rust` export
// level instead.
- let special_runtime_crate = tcx.is_panic_runtime(LOCAL_CRATE) ||
- tcx.is_compiler_builtins(LOCAL_CRATE);
+ let special_runtime_crate =
+ tcx.is_panic_runtime(LOCAL_CRATE) || tcx.is_compiler_builtins(LOCAL_CRATE);
- let mut reachable_non_generics: DefIdMap<_> = tcx.reachable_set(LOCAL_CRATE).0
+ let mut reachable_non_generics: DefIdMap<_> = tcx
+ .reachable_set(LOCAL_CRATE)
.iter()
.filter_map(|&hir_id| {
// We want to ignore some FFI functions that are not exposed from
}
// Only consider nodes that actually have exported symbols.
- Node::Item(&hir::Item {
- kind: hir::ItemKind::Static(..),
- ..
- }) |
- Node::Item(&hir::Item {
- kind: hir::ItemKind::Fn(..), ..
- }) |
- Node::ImplItem(&hir::ImplItem {
- kind: hir::ImplItemKind::Method(..),
- ..
- }) => {
+ Node::Item(&hir::Item { kind: hir::ItemKind::Static(..), .. })
+ | Node::Item(&hir::Item { kind: hir::ItemKind::Fn(..), .. })
+ | Node::ImplItem(&hir::ImplItem { kind: hir::ImplItemKind::Method(..), .. }) => {
let def_id = tcx.hir().local_def_id(hir_id);
let generics = tcx.generics_of(def_id);
if !generics.requires_monomorphization(tcx) &&
// Functions marked with #[inline] are only ever codegened
// with "internal" linkage and are never exported.
- !Instance::mono(tcx, def_id).def.requires_local(tcx) {
+ !Instance::mono(tcx, def_id).def.requires_local(tcx)
+ {
Some(def_id)
} else {
None
}
}
- _ => None
+ _ => None,
}
})
.map(|def_id| {
//
// In general though we won't link right if these
// symbols are stripped, and LTO currently strips them.
- if name == "rust_eh_personality" ||
- name == "rust_eh_register_frames" ||
- name == "rust_eh_unregister_frames" {
+ if name == "rust_eh_personality"
+ || name == "rust_eh_register_frames"
+ || name == "rust_eh_unregister_frames"
+ {
SymbolExportLevel::C
} else {
SymbolExportLevel::Rust
} else {
symbol_export_level(tcx, def_id)
};
- debug!("EXPORTED SYMBOL (local): {} ({:?})",
- tcx.symbol_name(Instance::mono(tcx, def_id)),
- export_level);
+ debug!(
+ "EXPORTED SYMBOL (local): {} ({:?})",
+ tcx.symbol_name(Instance::mono(tcx, def_id)),
+ export_level
+ );
(def_id, export_level)
})
.collect();
assert_eq!(cnum, LOCAL_CRATE);
if !tcx.sess.opts.output_types.should_codegen() {
- return Arc::new(vec![])
+ return Arc::new(vec![]);
}
- let mut symbols: Vec<_> = tcx.reachable_non_generics(LOCAL_CRATE)
- .iter()
- .map(|(&def_id, &level)| {
- (ExportedSymbol::NonGeneric(def_id), level)
- })
- .collect();
+ let mut symbols: Vec<_> = tcx
+ .reachable_non_generics(LOCAL_CRATE)
+ .iter()
+ .map(|(&def_id, &level)| (ExportedSymbol::NonGeneric(def_id), level))
+ .collect();
if tcx.entry_fn(LOCAL_CRATE).is_some() {
let exported_symbol = ExportedSymbol::NoDefId(SymbolName::new("main"));
// These are weak symbols that point to the profile version and the
// profile name, which need to be treated as exported so LTO doesn't nix
// them.
- const PROFILER_WEAK_SYMBOLS: [&str; 2] = [
- "__llvm_profile_raw_version",
- "__llvm_profile_filename",
- ];
+ const PROFILER_WEAK_SYMBOLS: [&str; 2] =
+ ["__llvm_profile_raw_version", "__llvm_profile_filename"];
symbols.extend(PROFILER_WEAK_SYMBOLS.iter().map(|sym| {
let exported_symbol = ExportedSymbol::NoDefId(SymbolName::new(sym));
}
if tcx.sess.opts.share_generics() && tcx.local_crate_exports_generics() {
- use rustc::mir::mono::{Linkage, Visibility, MonoItem};
+ use rustc::mir::mono::{Linkage, MonoItem, Visibility};
use rustc::ty::InstanceDef;
// Normally, we require that shared monomorphizations are not hidden,
// needs to be exported.
// However, on platforms that don't allow for Rust dylibs, having
// external linkage is enough for monomorphization to be linked to.
- let need_visibility = tcx.sess.target.target.options.dynamic_linking &&
- !tcx.sess.target.target.options.only_cdylib;
+ let need_visibility = tcx.sess.target.target.options.dynamic_linking
+ && !tcx.sess.target.target.options.only_cdylib;
let (_, cgus) = tcx.collect_and_partition_mono_items(LOCAL_CRATE);
- for (mono_item, &(linkage, visibility)) in cgus.iter()
- .flat_map(|cgu| cgu.items().iter()) {
+ for (mono_item, &(linkage, visibility)) in cgus.iter().flat_map(|cgu| cgu.items().iter()) {
if linkage != Linkage::External {
// We can only re-use things with external linkage, otherwise
// we'll get a linker error
- continue
+ continue;
}
if need_visibility && visibility == Visibility::Hidden {
// If we potentially share things from Rust dylibs, they must
// not be hidden
- continue
+ continue;
}
- if let &MonoItem::Fn(Instance {
- def: InstanceDef::Item(def_id),
- substs,
- }) = mono_item {
+ if let &MonoItem::Fn(Instance { def: InstanceDef::Item(def_id), substs }) = mono_item {
if substs.non_erasable_generics().next().is_some() {
- symbols.push((ExportedSymbol::Generic(def_id, substs),
- SymbolExportLevel::Rust));
+ symbols
+ .push((ExportedSymbol::Generic(def_id, substs), SymbolExportLevel::Rust));
}
}
}
let mut instances: DefIdMap<FxHashMap<_, _>> = Default::default();
let cnum_stable_ids: IndexVec<CrateNum, Fingerprint> = {
- let mut cnum_stable_ids = IndexVec::from_elem_n(Fingerprint::ZERO,
- cnums.len() + 1);
+ let mut cnum_stable_ids = IndexVec::from_elem_n(Fingerprint::ZERO, cnums.len() + 1);
for &cnum in cnums.iter() {
- cnum_stable_ids[cnum] = tcx.def_path_hash(DefId {
- krate: cnum,
- index: CRATE_DEF_INDEX,
- }).0;
+ cnum_stable_ids[cnum] =
+ tcx.def_path_hash(DefId { krate: cnum, index: CRATE_DEF_INDEX }).0;
}
cnum_stable_ids
fn is_unreachable_local_definition_provider(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
if let Some(hir_id) = tcx.hir().as_local_hir_id(def_id) {
- !tcx.reachable_set(LOCAL_CRATE).0.contains(&hir_id)
+ !tcx.reachable_set(LOCAL_CRATE).contains(&hir_id)
} else {
- bug!("is_unreachable_local_definition called with non-local DefId: {:?}",
- def_id)
+ bug!("is_unreachable_local_definition called with non-local DefId: {:?}", def_id)
}
}
if is_extern && !std_internal {
let target = &tcx.sess.target.target.llvm_target;
// WebAssembly cannot export data symbols, so reduce their export level
- if target.contains("wasm32") || target.contains("emscripten") {
- if let Some(Node::Item(&hir::Item {
- kind: hir::ItemKind::Static(..),
- ..
- })) = tcx.hir().get_if_local(sym_def_id) {
+ if target.contains("emscripten") {
+ if let Some(Node::Item(&hir::Item { kind: hir::ItemKind::Static(..), .. })) =
+ tcx.hir().get_if_local(sym_def_id)
+ {
return SymbolExportLevel::Rust;
}
}