Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
rustc_metadata: Move some structs from cstore to decoder
This allows to privatize their fields.
  • Loading branch information
petrochenkov committed Nov 17, 2019
1 parent 4b6cef1 commit 68985c7
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 27 deletions.
4 changes: 2 additions & 2 deletions src/librustc_metadata/creader.rs
@@ -1,8 +1,8 @@
//! Validates all used crates and extern libraries and loads their metadata

use crate::cstore::{self, CStore, MetadataBlob};
use crate::cstore::{self, CStore};
use crate::locator::{self, CratePaths};
use crate::rmeta::{CrateRoot, CrateDep};
use crate::rmeta::{CrateRoot, CrateDep, MetadataBlob};
use rustc_data_structures::sync::{Lock, Once, AtomicCell};

use rustc::hir::def_id::CrateNum;
Expand Down
22 changes: 4 additions & 18 deletions src/librustc_metadata/cstore.rs
@@ -1,21 +1,20 @@
// The crate store - a central repo for information collected about external
// crates and libraries

use crate::rmeta;
use crate::rmeta::{CrateRoot, ImportedSourceFile, Lazy, MetadataBlob};
use rustc::dep_graph::DepNodeIndex;
use rustc::hir::def_id::{CrateNum, DefIndex};
use rustc::hir::map::definitions::DefPathTable;
use rustc::middle::cstore::{CrateSource, DepKind, ExternCrate};
use rustc::mir::interpret::AllocDecodingState;
use rustc_index::vec::IndexVec;
use rustc::util::nodemap::FxHashMap;
use rustc_data_structures::sync::{Lrc, Lock, MetadataRef, Once, AtomicCell};
use rustc_data_structures::sync::{Lrc, Lock, Once, AtomicCell};
use rustc_data_structures::svh::Svh;
use syntax::ast;
use syntax::edition::Edition;
use syntax_expand::base::SyntaxExtension;
use syntax::expand::allocator::AllocatorKind;
use syntax_pos;
use proc_macro::bridge::client::ProcMacro;

pub use crate::rmeta::{provide, provide_extern};
Expand All @@ -26,19 +25,6 @@ pub use crate::rmeta::{provide, provide_extern};
// own crate numbers.
crate type CrateNumMap = IndexVec<CrateNum, CrateNum>;

crate struct MetadataBlob(pub MetadataRef);

/// Holds information about a syntax_pos::SourceFile imported from another crate.
/// See `imported_source_files()` for more information.
crate struct ImportedSourceFile {
/// This SourceFile's byte-offset within the source_map of its original crate
pub original_start_pos: syntax_pos::BytePos,
/// The end of this SourceFile within the source_map of its original crate
pub original_end_pos: syntax_pos::BytePos,
/// The imported SourceFile's representation within the local source_map
pub translated_source_file: Lrc<syntax_pos::SourceFile>,
}

crate struct CrateMetadata {
/// The primary crate data - binary metadata blob.
crate blob: MetadataBlob,
Expand All @@ -50,7 +36,7 @@ crate struct CrateMetadata {
/// lifetime is only used behind `Lazy`, and therefore acts like an
/// universal (`for<'tcx>`), that is paired up with whichever `TyCtxt`
/// is being used to decode those values.
crate root: rmeta::CrateRoot<'static>,
crate root: CrateRoot<'static>,
/// For each definition in this crate, we encode a key. When the
/// crate is loaded, we read all the keys and put them in this
/// hashmap, which gives the reverse mapping. This allows us to
Expand All @@ -60,7 +46,7 @@ crate struct CrateMetadata {
/// Trait impl data.
/// FIXME: Used only from queries and can use query cache,
/// so pre-decoding can probably be avoided.
crate trait_impls: FxHashMap<(u32, DefIndex), rmeta::Lazy<[DefIndex]>>,
crate trait_impls: FxHashMap<(u32, DefIndex), Lazy<[DefIndex]>>,
/// Proc macro descriptions for this crate, if it's a proc macro crate.
crate raw_proc_macros: Option<&'static [ProcMacro]>,
/// Source maps for code from the crate.
Expand Down
5 changes: 2 additions & 3 deletions src/librustc_metadata/locator.rs
Expand Up @@ -212,9 +212,8 @@
//! no means all of the necessary details. Take a look at the rest of
//! metadata::locator or metadata::creader for all the juicy details!

use crate::cstore::MetadataBlob;
use crate::creader::Library;
use crate::rmeta::{METADATA_HEADER, rustc_version};
use crate::rmeta::{METADATA_HEADER, rustc_version, MetadataBlob};

use rustc_data_structures::fx::FxHashSet;
use rustc_data_structures::svh::Svh;
Expand Down Expand Up @@ -907,7 +906,7 @@ fn get_metadata_section_imp(target: &Target,
rustc_erase_owner!(OwningRef::new(StableDerefMmap(mmap)).map_owner_box())
}
};
let blob = MetadataBlob(raw_bytes);
let blob = MetadataBlob::new(raw_bytes);
if blob.is_compatible() {
Ok(blob)
} else {
Expand Down
25 changes: 21 additions & 4 deletions src/librustc_metadata/rmeta/decoder.rs
@@ -1,6 +1,6 @@
// Decoding metadata from a single crate's metadata

use crate::cstore::{self, CrateMetadata, MetadataBlob};
use crate::cstore::CrateMetadata;
use crate::rmeta::*;
use crate::rmeta::table::{FixedSizeEncoding, PerDefTable};

Expand Down Expand Up @@ -44,6 +44,19 @@ pub use cstore_impl::{provide, provide_extern};

mod cstore_impl;

crate struct MetadataBlob(MetadataRef);

/// Holds information about a syntax_pos::SourceFile imported from another crate.
/// See `imported_source_files()` for more information.
crate struct ImportedSourceFile {
/// This SourceFile's byte-offset within the source_map of its original crate
original_start_pos: syntax_pos::BytePos,
/// The end of this SourceFile within the source_map of its original crate
original_end_pos: syntax_pos::BytePos,
/// The imported SourceFile's representation within the local source_map
translated_source_file: Lrc<syntax_pos::SourceFile>,
}

crate struct DecodeContext<'a, 'tcx> {
opaque: opaque::Decoder<'a>,
cdata: Option<&'a CrateMetadata>,
Expand Down Expand Up @@ -393,7 +406,11 @@ for DecodeContext<'a, 'tcx> {

implement_ty_decoder!( DecodeContext<'a, 'tcx> );

impl<'tcx> MetadataBlob {
impl MetadataBlob {
crate fn new(metadata_ref: MetadataRef) -> MetadataBlob {
MetadataBlob(metadata_ref)
}

crate fn is_compatible(&self) -> bool {
self.raw_bytes().starts_with(METADATA_HEADER)
}
Expand Down Expand Up @@ -1296,7 +1313,7 @@ impl<'a, 'tcx> CrateMetadata {
fn imported_source_files(
&'a self,
local_source_map: &source_map::SourceMap,
) -> &[cstore::ImportedSourceFile] {
) -> &[ImportedSourceFile] {
self.source_map_import_info.init_locking(|| {
let external_source_map = self.root.source_map.decode(self);

Expand Down Expand Up @@ -1351,7 +1368,7 @@ impl<'a, 'tcx> CrateMetadata {
local_version.name, start_pos, end_pos,
local_version.start_pos, local_version.end_pos);

cstore::ImportedSourceFile {
ImportedSourceFile {
original_start_pos: start_pos,
original_end_pos: end_pos,
translated_source_file: local_version,
Expand Down
2 changes: 2 additions & 0 deletions src/librustc_metadata/rmeta/mod.rs
Expand Up @@ -14,6 +14,7 @@ use rustc::ty::{self, Ty, ReprOptions};
use rustc_target::spec::{PanicStrategy, TargetTriple};
use rustc_index::vec::IndexVec;
use rustc_data_structures::svh::Svh;
use rustc_data_structures::sync::MetadataRef;
use rustc_serialize::Encodable;
use syntax::{ast, attr};
use syntax::edition::Edition;
Expand All @@ -24,6 +25,7 @@ use std::marker::PhantomData;
use std::num::NonZeroUsize;

pub use decoder::{provide, provide_extern};
crate use decoder::{ImportedSourceFile, MetadataBlob};

mod decoder;
mod encoder;
Expand Down

0 comments on commit 68985c7

Please sign in to comment.