Skip to content

Commit 490f34a

Browse files
committed
rustc: Remove CrateStore::used_crate*
This commit removes the `use_crates` and `used_crate_source` methods in favor of a mix of queries and helper methods being used now instead.
1 parent 0b7e0aa commit 490f34a

File tree

11 files changed

+120
-92
lines changed

11 files changed

+120
-92
lines changed

src/librustc/dep_graph/dep_node.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,8 @@ define_dep_nodes!( <'tcx>
567567
[] VisibleParentMap,
568568
[] IsDirectExternCrate(CrateNum),
569569
[] MissingExternCrateItem(CrateNum),
570+
[] UsedCrateSource(CrateNum),
571+
[] PostorderCnums,
570572
);
571573

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

src/librustc/middle/cstore.rs

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
//! probably get a better home if someone can find one.
2424
2525
use hir::def;
26-
use hir::def_id::{CrateNum, DefId, DefIndex};
26+
use hir::def_id::{CrateNum, DefId, DefIndex, LOCAL_CRATE};
2727
use hir::map as hir_map;
2828
use hir::map::definitions::{Definitions, DefKey, DefPathTable};
2929
use hir::svh::Svh;
@@ -251,14 +251,13 @@ pub trait CrateStore {
251251
fn extern_mod_stmt_cnum_untracked(&self, emod_id: ast::NodeId) -> Option<CrateNum>;
252252
fn item_generics_cloned_untracked(&self, def: DefId) -> ty::Generics;
253253
fn associated_item_cloned_untracked(&self, def: DefId) -> ty::AssociatedItem;
254+
fn postorder_cnums_untracked(&self) -> Vec<CrateNum>;
254255

255256
// This is basically a 1-based range of ints, which is a little
256257
// silly - I may fix that.
257258
fn crates(&self) -> Vec<CrateNum>;
258259

259260
// utility functions
260-
fn used_crates(&self, prefer: LinkagePreference) -> Vec<(CrateNum, LibSource)>;
261-
fn used_crate_source(&self, cnum: CrateNum) -> CrateSource;
262261
fn encode_metadata<'a, 'tcx>(&self,
263262
tcx: TyCtxt<'a, 'tcx, 'tcx>,
264263
link_meta: &LinkMeta,
@@ -340,9 +339,6 @@ impl CrateStore for DummyCrateStore {
340339
fn crates(&self) -> Vec<CrateNum> { vec![] }
341340

342341
// utility functions
343-
fn used_crates(&self, prefer: LinkagePreference) -> Vec<(CrateNum, LibSource)>
344-
{ vec![] }
345-
fn used_crate_source(&self, cnum: CrateNum) -> CrateSource { bug!("used_crate_source") }
346342
fn extern_mod_stmt_cnum_untracked(&self, emod_id: ast::NodeId) -> Option<CrateNum> { None }
347343
fn encode_metadata<'a, 'tcx>(&self,
348344
tcx: TyCtxt<'a, 'tcx, 'tcx>,
@@ -352,6 +348,7 @@ impl CrateStore for DummyCrateStore {
352348
bug!("encode_metadata")
353349
}
354350
fn metadata_encoding_version(&self) -> &[u8] { bug!("metadata_encoding_version") }
351+
fn postorder_cnums_untracked(&self) -> Vec<CrateNum> { bug!("postorder_cnums_untracked") }
355352

356353
// access to the metadata loader
357354
fn metadata_loader(&self) -> &MetadataLoader { bug!("metadata_loader") }
@@ -361,3 +358,46 @@ pub trait CrateLoader {
361358
fn process_item(&mut self, item: &ast::Item, defs: &Definitions);
362359
fn postprocess(&mut self, krate: &ast::Crate);
363360
}
361+
362+
// This method is used when generating the command line to pass through to
363+
// system linker. The linker expects undefined symbols on the left of the
364+
// command line to be defined in libraries on the right, not the other way
365+
// around. For more info, see some comments in the add_used_library function
366+
// below.
367+
//
368+
// In order to get this left-to-right dependency ordering, we perform a
369+
// topological sort of all crates putting the leaves at the right-most
370+
// positions.
371+
pub fn used_crates<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
372+
prefer: LinkagePreference) -> Vec<(CrateNum, LibSource)> {
373+
let mut libs = tcx.sess.cstore.crates()
374+
.into_iter()
375+
.filter_map(|cnum| {
376+
if tcx.dep_kind(cnum).macros_only() {
377+
return None
378+
}
379+
let source = tcx.used_crate_source(cnum);
380+
let path = match prefer {
381+
LinkagePreference::RequireDynamic => source.dylib.clone().map(|p| p.0),
382+
LinkagePreference::RequireStatic => source.rlib.clone().map(|p| p.0),
383+
};
384+
let path = match path {
385+
Some(p) => LibSource::Some(p),
386+
None => {
387+
if source.rmeta.is_some() {
388+
LibSource::MetadataOnly
389+
} else {
390+
LibSource::None
391+
}
392+
}
393+
};
394+
Some((cnum, path))
395+
})
396+
.collect::<Vec<_>>();
397+
let mut ordering = tcx.postorder_cnums(LOCAL_CRATE);
398+
Rc::make_mut(&mut ordering).reverse();
399+
libs.sort_by_key(|&(a, _)| {
400+
ordering.iter().position(|x| *x == a)
401+
});
402+
libs
403+
}

src/librustc/middle/dependency_format.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ use hir::def_id::CrateNum;
6666
use session;
6767
use session::config;
6868
use ty::TyCtxt;
69-
use middle::cstore::DepKind;
69+
use middle::cstore::{self, DepKind};
7070
use middle::cstore::LinkagePreference::{self, RequireStatic, RequireDynamic};
7171
use util::nodemap::FxHashMap;
7272
use rustc_back::PanicStrategy;
@@ -134,7 +134,7 @@ fn calculate_type<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
134134
}
135135
for cnum in sess.cstore.crates() {
136136
if tcx.dep_kind(cnum).macros_only() { continue }
137-
let src = sess.cstore.used_crate_source(cnum);
137+
let src = tcx.used_crate_source(cnum);
138138
if src.rlib.is_some() { continue }
139139
sess.err(&format!("dependency `{}` not found in rlib format",
140140
tcx.crate_name(cnum)));
@@ -168,7 +168,7 @@ fn calculate_type<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
168168
for cnum in sess.cstore.crates() {
169169
if tcx.dep_kind(cnum).macros_only() { continue }
170170
let name = tcx.crate_name(cnum);
171-
let src = sess.cstore.used_crate_source(cnum);
171+
let src = tcx.used_crate_source(cnum);
172172
if src.dylib.is_some() {
173173
info!("adding dylib: {}", name);
174174
add_library(tcx, cnum, RequireDynamic, &mut formats);
@@ -196,7 +196,7 @@ fn calculate_type<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
196196
// If the crate hasn't been included yet and it's not actually required
197197
// (e.g. it's an allocator) then we skip it here as well.
198198
for cnum in sess.cstore.crates() {
199-
let src = sess.cstore.used_crate_source(cnum);
199+
let src = tcx.used_crate_source(cnum);
200200
if src.dylib.is_none() &&
201201
!formats.contains_key(&cnum) &&
202202
tcx.dep_kind(cnum) == DepKind::Explicit {
@@ -225,7 +225,7 @@ fn calculate_type<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
225225
// making sure that everything is available in the requested format.
226226
for (cnum, kind) in ret.iter().enumerate() {
227227
let cnum = CrateNum::new(cnum + 1);
228-
let src = sess.cstore.used_crate_source(cnum);
228+
let src = tcx.used_crate_source(cnum);
229229
match *kind {
230230
Linkage::NotLinked |
231231
Linkage::IncludedFromDylib => {}
@@ -274,7 +274,7 @@ fn add_library(tcx: TyCtxt,
274274

275275
fn attempt_static<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> Option<DependencyList> {
276276
let sess = &tcx.sess;
277-
let crates = sess.cstore.used_crates(RequireStatic);
277+
let crates = cstore::used_crates(tcx, RequireStatic);
278278
if !crates.iter().by_ref().all(|&(_, ref p)| p.is_some()) {
279279
return None
280280
}

src/librustc/ty/maps.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use hir::svh::Svh;
1717
use lint;
1818
use middle::const_val;
1919
use middle::cstore::{ExternCrate, LinkagePreference, NativeLibrary};
20-
use middle::cstore::{NativeLibraryKind, DepKind};
20+
use middle::cstore::{NativeLibraryKind, DepKind, CrateSource};
2121
use middle::privacy::AccessLevels;
2222
use middle::region;
2323
use middle::region::RegionMaps;
@@ -718,6 +718,18 @@ impl<'tcx> QueryDescription for queries::missing_extern_crate_item<'tcx> {
718718
}
719719
}
720720

721+
impl<'tcx> QueryDescription for queries::used_crate_source<'tcx> {
722+
fn describe(_tcx: TyCtxt, _: CrateNum) -> String {
723+
format!("looking at the source for a crate")
724+
}
725+
}
726+
727+
impl<'tcx> QueryDescription for queries::postorder_cnums<'tcx> {
728+
fn describe(_tcx: TyCtxt, _: CrateNum) -> String {
729+
format!("generating a postorder list of CrateNums")
730+
}
731+
}
732+
721733
// If enabled, send a message to the profile-queries thread
722734
macro_rules! profq_msg {
723735
($tcx:expr, $msg:expr) => {
@@ -1331,6 +1343,8 @@ define_maps! { <'tcx>
13311343
[] visible_parent_map: visible_parent_map_node(CrateNum)
13321344
-> Rc<DefIdMap<DefId>>,
13331345
[] missing_extern_crate_item: MissingExternCrateItem(CrateNum) -> bool,
1346+
[] used_crate_source: UsedCrateSource(CrateNum) -> Rc<CrateSource>,
1347+
[] postorder_cnums: postorder_cnums_node(CrateNum) -> Rc<Vec<CrateNum>>,
13341348
}
13351349

13361350
fn type_param_predicates<'tcx>((item_id, param_id): (DefId, DefId)) -> DepConstructor<'tcx> {
@@ -1428,3 +1442,7 @@ fn get_lang_items_node<'tcx>(_: CrateNum) -> DepConstructor<'tcx> {
14281442
fn visible_parent_map_node<'tcx>(_: CrateNum) -> DepConstructor<'tcx> {
14291443
DepConstructor::VisibleParentMap
14301444
}
1445+
1446+
fn postorder_cnums_node<'tcx>(_: CrateNum) -> DepConstructor<'tcx> {
1447+
DepConstructor::PostorderCnums
1448+
}

src/librustc_metadata/creader.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use rustc::session::config::{Sanitizer, self};
2424
use rustc_back::PanicStrategy;
2525
use rustc::session::search_paths::PathKind;
2626
use rustc::middle;
27-
use rustc::middle::cstore::{CrateStore, validate_crate_name, ExternCrate};
27+
use rustc::middle::cstore::{validate_crate_name, ExternCrate};
2828
use rustc::util::common::record_time;
2929
use rustc::util::nodemap::FxHashSet;
3030
use rustc::hir::map::Definitions;
@@ -166,7 +166,7 @@ impl<'a> CrateLoader<'a> {
166166
// We're also sure to compare *paths*, not actual byte slices. The
167167
// `source` stores paths which are normalized which may be different
168168
// from the strings on the command line.
169-
let source = self.cstore.used_crate_source(cnum);
169+
let source = &self.cstore.get_crate_data(cnum).source;
170170
if let Some(locs) = self.sess.opts.externs.get(&*name.as_str()) {
171171
let found = locs.iter().any(|l| {
172172
let l = fs::canonicalize(l).ok();

src/librustc_metadata/cstore.rs

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -150,52 +150,12 @@ impl CStore {
150150
ordering.push(krate);
151151
}
152152

153-
// This method is used when generating the command line to pass through to
154-
// system linker. The linker expects undefined symbols on the left of the
155-
// command line to be defined in libraries on the right, not the other way
156-
// around. For more info, see some comments in the add_used_library function
157-
// below.
158-
//
159-
// In order to get this left-to-right dependency ordering, we perform a
160-
// topological sort of all crates putting the leaves at the right-most
161-
// positions.
162-
pub fn do_get_used_crates(&self,
163-
prefer: LinkagePreference)
164-
-> Vec<(CrateNum, LibSource)> {
153+
pub fn do_postorder_cnums_untracked(&self) -> Vec<CrateNum> {
165154
let mut ordering = Vec::new();
166155
for (&num, _) in self.metas.borrow().iter() {
167156
self.push_dependencies_in_postorder(&mut ordering, num);
168157
}
169-
info!("topological ordering: {:?}", ordering);
170-
ordering.reverse();
171-
let mut libs = self.metas
172-
.borrow()
173-
.iter()
174-
.filter_map(|(&cnum, data)| {
175-
if data.dep_kind.get().macros_only() { return None; }
176-
let path = match prefer {
177-
LinkagePreference::RequireDynamic => data.source.dylib.clone().map(|p| p.0),
178-
LinkagePreference::RequireStatic => data.source.rlib.clone().map(|p| p.0),
179-
};
180-
let path = match path {
181-
Some(p) => LibSource::Some(p),
182-
None => {
183-
if data.source.rmeta.is_some() {
184-
LibSource::MetadataOnly
185-
} else {
186-
LibSource::None
187-
}
188-
}
189-
};
190-
Some((cnum, path))
191-
})
192-
.collect::<Vec<_>>();
193-
libs.sort_by(|&(a, _), &(b, _)| {
194-
let a = ordering.iter().position(|x| *x == a);
195-
let b = ordering.iter().position(|x| *x == b);
196-
a.cmp(&b)
197-
});
198-
libs
158+
return ordering
199159
}
200160

201161
pub fn add_extern_mod_stmt_cnum(&self, emod_id: ast::NodeId, cnum: CrateNum) {

src/librustc_metadata/cstore_impl.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ use native_libs;
1515
use schema;
1616

1717
use rustc::ty::maps::QueryConfig;
18-
use rustc::middle::cstore::{CrateStore, CrateSource, LibSource, DepKind,
18+
use rustc::middle::cstore::{CrateStore, DepKind,
1919
MetadataLoader, LinkMeta,
20-
LinkagePreference, LoadedMacro, EncodedMetadata,
20+
LoadedMacro, EncodedMetadata,
2121
EncodedMetadataHashes, NativeLibraryKind};
2222
use rustc::hir::def;
2323
use rustc::session::Session;
@@ -229,6 +229,8 @@ provide! { <'tcx> tcx, def_id, other, cdata,
229229
_ => false,
230230
}
231231
}
232+
233+
used_crate_source => { Rc::new(cdata.source.clone()) }
232234
}
233235

234236
pub fn provide_local<'tcx>(providers: &mut Providers<'tcx>) {
@@ -332,6 +334,11 @@ pub fn provide_local<'tcx>(providers: &mut Providers<'tcx>) {
332334
Rc::new(visible_parent_map)
333335
},
334336

337+
postorder_cnums: |tcx, cnum| {
338+
assert_eq!(cnum, LOCAL_CRATE);
339+
Rc::new(tcx.sess.cstore.postorder_cnums_untracked())
340+
},
341+
335342
..*providers
336343
};
337344
}
@@ -477,21 +484,15 @@ impl CrateStore for cstore::CStore {
477484
result
478485
}
479486

480-
fn used_crates(&self, prefer: LinkagePreference) -> Vec<(CrateNum, LibSource)>
481-
{
482-
self.do_get_used_crates(prefer)
483-
}
484-
485-
fn used_crate_source(&self, cnum: CrateNum) -> CrateSource
486-
{
487-
self.get_crate_data(cnum).source.clone()
488-
}
489-
490487
fn extern_mod_stmt_cnum_untracked(&self, emod_id: ast::NodeId) -> Option<CrateNum>
491488
{
492489
self.do_extern_mod_stmt_cnum(emod_id)
493490
}
494491

492+
fn postorder_cnums_untracked(&self) -> Vec<CrateNum> {
493+
self.do_postorder_cnums_untracked()
494+
}
495+
495496
fn encode_metadata<'a, 'tcx>(&self,
496497
tcx: TyCtxt<'a, 'tcx, 'tcx>,
497498
link_meta: &LinkMeta,

0 commit comments

Comments
 (0)