Skip to content

Commit dba3ddd

Browse files
committed
rustc: Calculate ExportedSymbols in a query
This commit moves the definition of the `ExportedSymbols` structure to the `rustc` crate and then creates a query that'll be used to construct the `ExportedSymbols` set. This in turn uses the reachablity query exposed in the previous commit.
1 parent baca9a6 commit dba3ddd

File tree

16 files changed

+266
-215
lines changed

16 files changed

+266
-215
lines changed

src/librustc/dep_graph/dep_node.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,7 @@ define_dep_nodes!( <'tcx>
535535
[] GetPanicStrategy(CrateNum),
536536
[] IsNoBuiltins(CrateNum),
537537
[] ImplDefaultness(DefId),
538-
[] ExportedSymbols(CrateNum),
538+
[] ExportedSymbolIds(CrateNum),
539539
[] NativeLibraries(CrateNum),
540540
[] PluginRegistrarFn(CrateNum),
541541
[] DeriveRegistrarFn(CrateNum),
@@ -575,6 +575,7 @@ define_dep_nodes!( <'tcx>
575575
[] MaybeUnusedExternCrates,
576576
[] StabilityIndex,
577577
[] AllCrateNums,
578+
[] ExportedSymbols,
578579
);
579580

580581
trait DepNodeParams<'a, 'gcx: 'tcx + 'a, 'tcx: 'a> : fmt::Debug {

src/librustc/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ pub mod middle {
9292
pub mod dependency_format;
9393
pub mod effect;
9494
pub mod entry;
95+
pub mod exported_symbols;
9596
pub mod free_region;
9697
pub mod intrinsicck;
9798
pub mod lang_items;
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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 util::nodemap::{FxHashMap, NodeSet};
12+
use hir::def_id::{DefId, CrateNum};
13+
14+
/// The SymbolExportLevel of a symbols specifies from which kinds of crates
15+
/// the symbol will be exported. `C` symbols will be exported from any
16+
/// kind of crate, including cdylibs which export very few things.
17+
/// `Rust` will only be exported if the crate produced is a Rust
18+
/// dylib.
19+
#[derive(Eq, PartialEq, Debug, Copy, Clone)]
20+
pub enum SymbolExportLevel {
21+
C,
22+
Rust,
23+
}
24+
25+
/// The set of symbols exported from each crate in the crate graph.
26+
#[derive(Debug)]
27+
pub struct ExportedSymbols {
28+
pub export_threshold: SymbolExportLevel,
29+
exports: FxHashMap<CrateNum, Vec<(String, DefId, SymbolExportLevel)>>,
30+
local_exports: NodeSet,
31+
}
32+
33+
impl ExportedSymbols {
34+
pub fn new(export_threshold: SymbolExportLevel,
35+
exports: FxHashMap<CrateNum, Vec<(String, DefId, SymbolExportLevel)>>,
36+
local_exports: NodeSet) -> ExportedSymbols {
37+
ExportedSymbols {
38+
export_threshold,
39+
exports,
40+
local_exports,
41+
}
42+
}
43+
44+
pub fn local_exports(&self) -> &NodeSet {
45+
&self.local_exports
46+
}
47+
48+
pub fn exported_symbols(&self, cnum: CrateNum)
49+
-> &[(String, DefId, SymbolExportLevel)]
50+
{
51+
match self.exports.get(&cnum) {
52+
Some(exports) => exports,
53+
None => &[]
54+
}
55+
}
56+
57+
pub fn for_each_exported_symbol<F>(&self, cnum: CrateNum, mut f: F)
58+
where F: FnMut(&str, DefId, SymbolExportLevel)
59+
{
60+
for &(ref name, def_id, export_level) in self.exported_symbols(cnum) {
61+
if is_below_threshold(export_level, self.export_threshold) {
62+
f(&name, def_id, export_level)
63+
}
64+
}
65+
}
66+
}
67+
68+
pub fn is_below_threshold(level: SymbolExportLevel,
69+
threshold: SymbolExportLevel)
70+
-> bool {
71+
if threshold == SymbolExportLevel::Rust {
72+
// We export everything from Rust dylibs
73+
true
74+
} else {
75+
level == SymbolExportLevel::C
76+
}
77+
}

src/librustc/ty/context.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ use middle::cstore::EncodedMetadata;
2727
use middle::free_region::FreeRegionMap;
2828
use middle::lang_items;
2929
use middle::resolve_lifetime::{self, ObjectLifetimeDefault};
30+
use middle::exported_symbols::ExportedSymbols;
3031
use middle::stability;
3132
use mir::Mir;
3233
use mir::transform::Passes;
@@ -64,6 +65,7 @@ use std::mem;
6465
use std::ops::Deref;
6566
use std::iter;
6667
use std::rc::Rc;
68+
use std::sync::Arc;
6769
use syntax::abi;
6870
use syntax::ast::{self, Name, NodeId};
6971
use syntax::attr;
@@ -1218,6 +1220,10 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
12181220
pub fn crate_data_as_rc_any(self, cnum: CrateNum) -> Rc<Any> {
12191221
self.cstore.crate_data_as_rc_any(cnum)
12201222
}
1223+
1224+
pub fn exported_symbols(self) -> Arc<ExportedSymbols> {
1225+
self.exported_symbol_set(LOCAL_CRATE)
1226+
}
12211227
}
12221228

12231229
impl<'a, 'tcx> TyCtxt<'a, 'tcx, 'tcx> {

src/librustc/ty/maps.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use middle::region;
2323
use middle::resolve_lifetime::{Region, ObjectLifetimeDefault};
2424
use middle::stability::{self, DeprecationEntry};
2525
use middle::lang_items::{LanguageItems, LangItem};
26+
use middle::exported_symbols::ExportedSymbols;
2627
use mir;
2728
use mir::transform::{MirSuite, MirPassIndex};
2829
use session::CompileResult;
@@ -48,6 +49,7 @@ use std::mem;
4849
use std::collections::BTreeMap;
4950
use std::ops::Deref;
5051
use std::rc::Rc;
52+
use std::sync::Arc;
5153
use syntax_pos::{Span, DUMMY_SP};
5254
use syntax::attr;
5355
use syntax::ast;
@@ -595,7 +597,7 @@ impl<'tcx> QueryDescription for queries::is_sanitizer_runtime<'tcx> {
595597
}
596598
}
597599

598-
impl<'tcx> QueryDescription for queries::exported_symbols<'tcx> {
600+
impl<'tcx> QueryDescription for queries::exported_symbol_ids<'tcx> {
599601
fn describe(_tcx: TyCtxt, _: CrateNum) -> String {
600602
format!("looking up the exported symbols of a crate")
601603
}
@@ -745,6 +747,12 @@ impl<'tcx> QueryDescription for queries::all_crate_nums<'tcx> {
745747
}
746748
}
747749

750+
impl<'tcx> QueryDescription for queries::exported_symbol_set<'tcx> {
751+
fn describe(_tcx: TyCtxt, _: CrateNum) -> String {
752+
format!("exported symbol set")
753+
}
754+
}
755+
748756
// If enabled, send a message to the profile-queries thread
749757
macro_rules! profq_msg {
750758
($tcx:expr, $msg:expr) => {
@@ -1322,7 +1330,7 @@ define_maps! { <'tcx>
13221330
[] fn lint_levels: lint_levels_node(CrateNum) -> Rc<lint::LintLevelMap>,
13231331

13241332
[] fn impl_defaultness: ImplDefaultness(DefId) -> hir::Defaultness,
1325-
[] fn exported_symbols: ExportedSymbols(CrateNum) -> Rc<Vec<DefId>>,
1333+
[] fn exported_symbol_ids: ExportedSymbolIds(CrateNum) -> Rc<Vec<DefId>>,
13261334
[] fn native_libraries: NativeLibraries(CrateNum) -> Rc<Vec<NativeLibrary>>,
13271335
[] fn plugin_registrar_fn: PluginRegistrarFn(CrateNum) -> Option<DefId>,
13281336
[] fn derive_registrar_fn: DeriveRegistrarFn(CrateNum) -> Option<DefId>,
@@ -1371,6 +1379,9 @@ define_maps! { <'tcx>
13711379

13721380
[] fn stability_index: stability_index_node(CrateNum) -> Rc<stability::Index<'tcx>>,
13731381
[] fn all_crate_nums: all_crate_nums_node(CrateNum) -> Rc<Vec<CrateNum>>,
1382+
1383+
[] fn exported_symbol_set: exported_symbol_set_node(CrateNum)
1384+
-> Arc<ExportedSymbols>,
13741385
}
13751386

13761387
fn type_param_predicates<'tcx>((item_id, param_id): (DefId, DefId)) -> DepConstructor<'tcx> {
@@ -1484,3 +1495,7 @@ fn stability_index_node<'tcx>(_: CrateNum) -> DepConstructor<'tcx> {
14841495
fn all_crate_nums_node<'tcx>(_: CrateNum) -> DepConstructor<'tcx> {
14851496
DepConstructor::AllCrateNums
14861497
}
1498+
1499+
fn exported_symbol_set_node<'tcx>(_: CrateNum) -> DepConstructor<'tcx> {
1500+
DepConstructor::ExportedSymbols
1501+
}

src/librustc_driver/driver.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -956,7 +956,7 @@ pub fn phase_3_run_analysis_passes<'tcx, F, R>(sess: &'tcx Session,
956956
mir::provide(&mut local_providers);
957957
reachable::provide(&mut local_providers);
958958
rustc_privacy::provide(&mut local_providers);
959-
trans::provide(&mut local_providers);
959+
trans::provide_local(&mut local_providers);
960960
typeck::provide(&mut local_providers);
961961
ty::provide(&mut local_providers);
962962
traits::provide(&mut local_providers);
@@ -968,7 +968,7 @@ pub fn phase_3_run_analysis_passes<'tcx, F, R>(sess: &'tcx Session,
968968

969969
let mut extern_providers = ty::maps::Providers::default();
970970
cstore::provide(&mut extern_providers);
971-
trans::provide(&mut extern_providers);
971+
trans::provide_extern(&mut extern_providers);
972972
ty::provide_extern(&mut extern_providers);
973973
traits::provide_extern(&mut extern_providers);
974974
// FIXME(eddyb) get rid of this once we replace const_eval with miri.

src/librustc_metadata/cstore_impl.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ provide! { <'tcx> tcx, def_id, other, cdata,
175175
extern_crate => { Rc::new(cdata.extern_crate.get()) }
176176
is_no_builtins => { cdata.is_no_builtins() }
177177
impl_defaultness => { cdata.get_impl_defaultness(def_id.index) }
178-
exported_symbols => { Rc::new(cdata.get_exported_symbols()) }
178+
exported_symbol_ids => { Rc::new(cdata.get_exported_symbols()) }
179179
native_libraries => { Rc::new(cdata.get_native_libraries()) }
180180
plugin_registrar_fn => {
181181
cdata.root.plugin_registrar_fn.map(|index| {

src/librustc_trans/back/linker.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ use context::SharedCrateContext;
1919

2020
use back::archive;
2121
use back::command::Command;
22-
use back::symbol_export::ExportedSymbols;
2322
use rustc::middle::dependency_format::Linkage;
23+
use rustc::middle::exported_symbols::ExportedSymbols;
2424
use rustc::hir::def_id::{LOCAL_CRATE, CrateNum};
2525
use rustc_back::LinkerFlavor;
2626
use rustc::session::Session;

src/librustc_trans/back/lto.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use errors::{FatalError, Handler};
1616
use llvm;
1717
use llvm::archive_ro::ArchiveRO;
1818
use llvm::{ModuleRef, TargetMachineRef, True, False};
19+
use rustc::middle::exported_symbols;
1920
use rustc::util::common::time;
2021
use rustc::util::common::path2cstr;
2122
use rustc::hir::def_id::LOCAL_CRATE;
@@ -68,7 +69,7 @@ pub fn run(cgcx: &CodegenContext,
6869
symbol_export::crates_export_threshold(&cgcx.crate_types);
6970

7071
let symbol_filter = &|&(ref name, _, level): &(String, _, _)| {
71-
if symbol_export::is_below_threshold(level, export_threshold) {
72+
if exported_symbols::is_below_threshold(level, export_threshold) {
7273
let mut bytes = Vec::with_capacity(name.len() + 1);
7374
bytes.extend(name.bytes());
7475
Some(CString::new(bytes).unwrap())

0 commit comments

Comments
 (0)