Skip to content

Commit c157023

Browse files
committed
rustc_metadata: Move CrateMetadata into decoder.rs
It allows to make most of its fields private
1 parent 41ee980 commit c157023

File tree

5 files changed

+135
-142
lines changed

5 files changed

+135
-142
lines changed

src/librustc_metadata/creader.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
//! Validates all used crates and extern libraries and loads their metadata
22
3-
use crate::cstore::{self, CStore};
3+
use crate::cstore::CStore;
44
use crate::locator::{CrateLocator, CratePaths};
5-
use crate::rmeta::{CrateRoot, CrateDep, MetadataBlob};
5+
use crate::rmeta::{CrateMetadata, CrateNumMap, CrateRoot, CrateDep, MetadataBlob};
66

77
use rustc::hir::def_id::CrateNum;
88
use rustc_data_structures::svh::Svh;
@@ -46,9 +46,9 @@ pub struct CrateLoader<'a> {
4646

4747
fn dump_crates(cstore: &CStore) {
4848
info!("resolved crates:");
49-
cstore.iter_crate_data(|_, data| {
49+
cstore.iter_crate_data(|cnum, data| {
5050
info!(" name: {}", data.root.name);
51-
info!(" cnum: {}", data.cnum);
51+
info!(" cnum: {}", cnum);
5252
info!(" hash: {}", data.root.hash);
5353
info!(" reqd: {:?}", *data.dep_kind.lock());
5454
let CrateSource { dylib, rlib, rmeta } = data.source.clone();
@@ -224,7 +224,7 @@ impl<'a> CrateLoader<'a> {
224224
self.dlsym_proc_macros(&dlsym_dylib.0, dlsym_root.disambiguator, span)
225225
});
226226

227-
self.cstore.set_crate_data(cnum, cstore::CrateMetadata::new(
227+
self.cstore.set_crate_data(cnum, CrateMetadata::new(
228228
self.sess,
229229
metadata,
230230
crate_root,
@@ -439,10 +439,10 @@ impl<'a> CrateLoader<'a> {
439439
krate: CrateNum,
440440
span: Span,
441441
dep_kind: DepKind)
442-
-> cstore::CrateNumMap {
442+
-> CrateNumMap {
443443
debug!("resolving deps of external crate");
444444
if crate_root.proc_macro_data.is_some() {
445-
return cstore::CrateNumMap::new();
445+
return CrateNumMap::new();
446446
}
447447

448448
// The map from crate numbers in the crate we're resolving to local crate numbers.
@@ -792,7 +792,7 @@ impl<'a> CrateLoader<'a> {
792792
fn inject_dependency_if(&self,
793793
krate: CrateNum,
794794
what: &str,
795-
needs_dep: &dyn Fn(&cstore::CrateMetadata) -> bool) {
795+
needs_dep: &dyn Fn(&CrateMetadata) -> bool) {
796796
// don't perform this validation if the session has errors, as one of
797797
// those errors may indicate a circular dependency which could cause
798798
// this to stack overflow.

src/librustc_metadata/cstore.rs

Lines changed: 5 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,18 @@
11
// The crate store - a central repo for information collected about external
22
// crates and libraries
33

4-
use crate::rmeta::{CrateRoot, ImportedSourceFile, Lazy, MetadataBlob};
5-
use rustc::dep_graph::DepNodeIndex;
6-
use rustc::hir::def_id::{CrateNum, DefIndex};
7-
use rustc::hir::map::definitions::DefPathTable;
8-
use rustc::middle::cstore::{CrateSource, DepKind, ExternCrate};
9-
use rustc::mir::interpret::AllocDecodingState;
10-
use rustc::session::Session;
4+
use crate::rmeta::CrateMetadata;
5+
6+
use rustc_data_structures::sync::Lrc;
117
use rustc_index::vec::IndexVec;
12-
use rustc::util::common::record_time;
13-
use rustc::util::nodemap::FxHashMap;
14-
use rustc_data_structures::sync::{Lrc, Lock, Once, AtomicCell};
15-
use rustc_data_structures::svh::Svh;
8+
use rustc::hir::def_id::CrateNum;
169
use syntax::ast;
1710
use syntax::edition::Edition;
18-
use syntax_expand::base::SyntaxExtension;
1911
use syntax::expand::allocator::AllocatorKind;
20-
use proc_macro::bridge::client::ProcMacro;
12+
use syntax_expand::base::SyntaxExtension;
2113

2214
pub use crate::rmeta::{provide, provide_extern};
2315

24-
// A map from external crate numbers (as decoded from some crate file) to
25-
// local crate numbers (as generated during this session). Each external
26-
// crate may refer to types in other external crates, and each has their
27-
// own crate numbers.
28-
crate type CrateNumMap = IndexVec<CrateNum, CrateNum>;
29-
30-
crate struct CrateMetadata {
31-
/// The primary crate data - binary metadata blob.
32-
crate blob: MetadataBlob,
33-
34-
// --- Some data pre-decoded from the metadata blob, usually for performance ---
35-
36-
/// Properties of the whole crate.
37-
/// NOTE(eddyb) we pass `'static` to a `'tcx` parameter because this
38-
/// lifetime is only used behind `Lazy`, and therefore acts like an
39-
/// universal (`for<'tcx>`), that is paired up with whichever `TyCtxt`
40-
/// is being used to decode those values.
41-
crate root: CrateRoot<'static>,
42-
/// For each definition in this crate, we encode a key. When the
43-
/// crate is loaded, we read all the keys and put them in this
44-
/// hashmap, which gives the reverse mapping. This allows us to
45-
/// quickly retrace a `DefPath`, which is needed for incremental
46-
/// compilation support.
47-
crate def_path_table: DefPathTable,
48-
/// Trait impl data.
49-
/// FIXME: Used only from queries and can use query cache,
50-
/// so pre-decoding can probably be avoided.
51-
crate trait_impls: FxHashMap<(u32, DefIndex), Lazy<[DefIndex]>>,
52-
/// Proc macro descriptions for this crate, if it's a proc macro crate.
53-
crate raw_proc_macros: Option<&'static [ProcMacro]>,
54-
/// Source maps for code from the crate.
55-
crate source_map_import_info: Once<Vec<ImportedSourceFile>>,
56-
/// Used for decoding interpret::AllocIds in a cached & thread-safe manner.
57-
crate alloc_decoding_state: AllocDecodingState,
58-
/// The `DepNodeIndex` of the `DepNode` representing this upstream crate.
59-
/// It is initialized on the first access in `get_crate_dep_node_index()`.
60-
/// Do not access the value directly, as it might not have been initialized yet.
61-
/// The field must always be initialized to `DepNodeIndex::INVALID`.
62-
crate dep_node_index: AtomicCell<DepNodeIndex>,
63-
64-
// --- Other significant crate properties ---
65-
66-
/// ID of this crate, from the current compilation session's point of view.
67-
crate cnum: CrateNum,
68-
/// Maps crate IDs as they are were seen from this crate's compilation sessions into
69-
/// IDs as they are seen from the current compilation session.
70-
crate cnum_map: CrateNumMap,
71-
/// Same ID set as `cnum_map` plus maybe some injected crates like panic runtime.
72-
crate dependencies: Lock<Vec<CrateNum>>,
73-
/// How to link (or not link) this crate to the currently compiled crate.
74-
crate dep_kind: Lock<DepKind>,
75-
/// Filesystem location of this crate.
76-
crate source: CrateSource,
77-
/// Whether or not this crate should be consider a private dependency
78-
/// for purposes of the 'exported_private_dependencies' lint
79-
crate private_dep: bool,
80-
/// The hash for the host proc macro. Used to support `-Z dual-proc-macro`.
81-
crate host_hash: Option<Svh>,
82-
83-
// --- Data used only for improving diagnostics ---
84-
85-
/// Information about the `extern crate` item or path that caused this crate to be loaded.
86-
/// If this is `None`, then the crate was injected (e.g., by the allocator).
87-
crate extern_crate: Lock<Option<ExternCrate>>,
88-
}
89-
9016
#[derive(Clone)]
9117
pub struct CStore {
9218
metas: IndexVec<CrateNum, Option<Lrc<CrateMetadata>>>,
@@ -99,48 +25,6 @@ pub enum LoadedMacro {
9925
ProcMacro(SyntaxExtension),
10026
}
10127

102-
impl CrateMetadata {
103-
crate fn new(
104-
sess: &Session,
105-
blob: MetadataBlob,
106-
root: CrateRoot<'static>,
107-
raw_proc_macros: Option<&'static [ProcMacro]>,
108-
cnum: CrateNum,
109-
cnum_map: CrateNumMap,
110-
dep_kind: DepKind,
111-
source: CrateSource,
112-
private_dep: bool,
113-
host_hash: Option<Svh>,
114-
) -> CrateMetadata {
115-
let def_path_table = record_time(&sess.perf_stats.decode_def_path_tables_time, || {
116-
root.def_path_table.decode((&blob, sess))
117-
});
118-
let trait_impls = root.impls.decode((&blob, sess))
119-
.map(|trait_impls| (trait_impls.trait_id, trait_impls.impls)).collect();
120-
let alloc_decoding_state =
121-
AllocDecodingState::new(root.interpret_alloc_index.decode(&blob).collect());
122-
let dependencies = Lock::new(cnum_map.iter().cloned().collect());
123-
CrateMetadata {
124-
blob,
125-
root,
126-
def_path_table,
127-
trait_impls,
128-
raw_proc_macros,
129-
source_map_import_info: Once::new(),
130-
alloc_decoding_state,
131-
dep_node_index: AtomicCell::new(DepNodeIndex::INVALID),
132-
cnum,
133-
cnum_map,
134-
dependencies,
135-
dep_kind: Lock::new(dep_kind),
136-
source,
137-
private_dep,
138-
host_hash,
139-
extern_crate: Lock::new(None),
140-
}
141-
}
142-
}
143-
14428
impl Default for CStore {
14529
fn default() -> Self {
14630
CStore {

src/librustc_metadata/rmeta/decoder.rs

Lines changed: 115 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,30 @@
11
// Decoding metadata from a single crate's metadata
22

3-
use crate::cstore::CrateMetadata;
43
use crate::rmeta::*;
54
use crate::rmeta::table::{FixedSizeEncoding, PerDefTable};
65

76
use rustc_index::vec::IndexVec;
8-
use rustc_data_structures::sync::Lrc;
7+
use rustc_data_structures::sync::{Lrc, Lock, Once, AtomicCell};
98
use rustc::hir::map::{DefKey, DefPath, DefPathData, DefPathHash};
9+
use rustc::hir::map::definitions::DefPathTable;
1010
use rustc::hir;
11+
use rustc::middle::cstore::{CrateSource, ExternCrate};
1112
use rustc::middle::cstore::{LinkagePreference, NativeLibrary, ForeignModule};
1213
use rustc::middle::exported_symbols::{ExportedSymbol, SymbolExportLevel};
1314
use rustc::hir::def::{self, Res, DefKind, CtorOf, CtorKind};
1415
use rustc::hir::def_id::{CrateNum, DefId, DefIndex, LocalDefId, CRATE_DEF_INDEX, LOCAL_CRATE};
1516
use rustc_data_structures::fingerprint::Fingerprint;
1617
use rustc_data_structures::fx::FxHashMap;
17-
use rustc::dep_graph::{DepNodeIndex, DepKind};
18+
use rustc_data_structures::svh::Svh;
19+
use rustc::dep_graph::{self, DepNodeIndex};
1820
use rustc::middle::lang_items;
1921
use rustc::mir::{self, interpret};
20-
use rustc::mir::interpret::AllocDecodingSession;
22+
use rustc::mir::interpret::{AllocDecodingSession, AllocDecodingState};
2123
use rustc::session::Session;
2224
use rustc::ty::{self, Ty, TyCtxt};
2325
use rustc::ty::codec::TyDecoder;
2426
use rustc::mir::{Body, Promoted};
27+
use rustc::util::common::record_time;
2528
use rustc::util::captures::Captures;
2629

2730
use std::io;
@@ -46,9 +49,75 @@ mod cstore_impl;
4649

4750
crate struct MetadataBlob(MetadataRef);
4851

52+
// A map from external crate numbers (as decoded from some crate file) to
53+
// local crate numbers (as generated during this session). Each external
54+
// crate may refer to types in other external crates, and each has their
55+
// own crate numbers.
56+
crate type CrateNumMap = IndexVec<CrateNum, CrateNum>;
57+
58+
crate struct CrateMetadata {
59+
/// The primary crate data - binary metadata blob.
60+
blob: MetadataBlob,
61+
62+
// --- Some data pre-decoded from the metadata blob, usually for performance ---
63+
64+
/// Properties of the whole crate.
65+
/// NOTE(eddyb) we pass `'static` to a `'tcx` parameter because this
66+
/// lifetime is only used behind `Lazy`, and therefore acts like an
67+
/// universal (`for<'tcx>`), that is paired up with whichever `TyCtxt`
68+
/// is being used to decode those values.
69+
crate root: CrateRoot<'static>,
70+
/// For each definition in this crate, we encode a key. When the
71+
/// crate is loaded, we read all the keys and put them in this
72+
/// hashmap, which gives the reverse mapping. This allows us to
73+
/// quickly retrace a `DefPath`, which is needed for incremental
74+
/// compilation support.
75+
def_path_table: DefPathTable,
76+
/// Trait impl data.
77+
/// FIXME: Used only from queries and can use query cache,
78+
/// so pre-decoding can probably be avoided.
79+
trait_impls: FxHashMap<(u32, DefIndex), Lazy<[DefIndex]>>,
80+
/// Proc macro descriptions for this crate, if it's a proc macro crate.
81+
raw_proc_macros: Option<&'static [ProcMacro]>,
82+
/// Source maps for code from the crate.
83+
source_map_import_info: Once<Vec<ImportedSourceFile>>,
84+
/// Used for decoding interpret::AllocIds in a cached & thread-safe manner.
85+
alloc_decoding_state: AllocDecodingState,
86+
/// The `DepNodeIndex` of the `DepNode` representing this upstream crate.
87+
/// It is initialized on the first access in `get_crate_dep_node_index()`.
88+
/// Do not access the value directly, as it might not have been initialized yet.
89+
/// The field must always be initialized to `DepNodeIndex::INVALID`.
90+
dep_node_index: AtomicCell<DepNodeIndex>,
91+
92+
// --- Other significant crate properties ---
93+
94+
/// ID of this crate, from the current compilation session's point of view.
95+
cnum: CrateNum,
96+
/// Maps crate IDs as they are were seen from this crate's compilation sessions into
97+
/// IDs as they are seen from the current compilation session.
98+
cnum_map: CrateNumMap,
99+
/// Same ID set as `cnum_map` plus maybe some injected crates like panic runtime.
100+
crate dependencies: Lock<Vec<CrateNum>>,
101+
/// How to link (or not link) this crate to the currently compiled crate.
102+
crate dep_kind: Lock<DepKind>,
103+
/// Filesystem location of this crate.
104+
crate source: CrateSource,
105+
/// Whether or not this crate should be consider a private dependency
106+
/// for purposes of the 'exported_private_dependencies' lint
107+
private_dep: bool,
108+
/// The hash for the host proc macro. Used to support `-Z dual-proc-macro`.
109+
host_hash: Option<Svh>,
110+
111+
// --- Data used only for improving diagnostics ---
112+
113+
/// Information about the `extern crate` item or path that caused this crate to be loaded.
114+
/// If this is `None`, then the crate was injected (e.g., by the allocator).
115+
crate extern_crate: Lock<Option<ExternCrate>>,
116+
}
117+
49118
/// Holds information about a syntax_pos::SourceFile imported from another crate.
50119
/// See `imported_source_files()` for more information.
51-
crate struct ImportedSourceFile {
120+
struct ImportedSourceFile {
52121
/// This SourceFile's byte-offset within the source_map of its original crate
53122
original_start_pos: syntax_pos::BytePos,
54123
/// The end of this SourceFile within the source_map of its original crate
@@ -485,6 +554,46 @@ impl<'tcx> EntryKind<'tcx> {
485554
}
486555

487556
impl<'a, 'tcx> CrateMetadata {
557+
crate fn new(
558+
sess: &Session,
559+
blob: MetadataBlob,
560+
root: CrateRoot<'static>,
561+
raw_proc_macros: Option<&'static [ProcMacro]>,
562+
cnum: CrateNum,
563+
cnum_map: CrateNumMap,
564+
dep_kind: DepKind,
565+
source: CrateSource,
566+
private_dep: bool,
567+
host_hash: Option<Svh>,
568+
) -> CrateMetadata {
569+
let def_path_table = record_time(&sess.perf_stats.decode_def_path_tables_time, || {
570+
root.def_path_table.decode((&blob, sess))
571+
});
572+
let trait_impls = root.impls.decode((&blob, sess))
573+
.map(|trait_impls| (trait_impls.trait_id, trait_impls.impls)).collect();
574+
let alloc_decoding_state =
575+
AllocDecodingState::new(root.interpret_alloc_index.decode(&blob).collect());
576+
let dependencies = Lock::new(cnum_map.iter().cloned().collect());
577+
CrateMetadata {
578+
blob,
579+
root,
580+
def_path_table,
581+
trait_impls,
582+
raw_proc_macros,
583+
source_map_import_info: Once::new(),
584+
alloc_decoding_state,
585+
dep_node_index: AtomicCell::new(DepNodeIndex::INVALID),
586+
cnum,
587+
cnum_map,
588+
dependencies,
589+
dep_kind: Lock::new(dep_kind),
590+
source,
591+
private_dep,
592+
host_hash,
593+
extern_crate: Lock::new(None),
594+
}
595+
}
596+
488597
fn is_proc_macro_crate(&self) -> bool {
489598
self.root.proc_macro_decls_static.is_some()
490599
}
@@ -1391,7 +1500,7 @@ impl<'a, 'tcx> CrateMetadata {
13911500
// would always write the same value.
13921501

13931502
let def_path_hash = self.def_path_hash(CRATE_DEF_INDEX);
1394-
let dep_node = def_path_hash.to_dep_node(DepKind::CrateMetadata);
1503+
let dep_node = def_path_hash.to_dep_node(dep_graph::DepKind::CrateMetadata);
13951504

13961505
dep_node_index = tcx.dep_graph.dep_node_index_of(&dep_node);
13971506
assert!(dep_node_index != DepNodeIndex::INVALID);

src/librustc_metadata/rmeta/decoder/cstore_impl.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ macro_rules! provide {
5252
assert!(!$def_id.is_local());
5353

5454
let $cdata = $tcx.crate_data_as_any($def_id.krate);
55-
let $cdata = $cdata.downcast_ref::<cstore::CrateMetadata>()
55+
let $cdata = $cdata.downcast_ref::<rmeta::CrateMetadata>()
5656
.expect("CrateStore created data is not a CrateMetadata");
5757

5858
if $tcx.dep_graph.is_fully_enabled() {

0 commit comments

Comments
 (0)