Skip to content
Permalink
Browse files

rustc_metadata: Merge `cstore.rs` into `creader.rs`

  • Loading branch information
petrochenkov committed Nov 23, 2019
1 parent 3b1d60a commit 753ce4518e9178be819b898be83c6aef970d428c
@@ -25,7 +25,6 @@ use rustc_data_structures::{box_region_allow_access, declare_box_region_type, pa
use rustc_data_structures::sync::{Lrc, ParallelIterator, par_iter};
use rustc_errors::PResult;
use rustc_incremental;
use rustc_metadata::cstore;
use rustc_mir as mir;
use rustc_parse::{parse_crate_from_file, parse_crate_from_source_str};
use rustc_passes::{self, ast_validation, hir_stats, layout_test};
@@ -728,15 +727,15 @@ pub fn default_provide(providers: &mut ty::query::Providers<'_>) {
rustc_passes::provide(providers);
rustc_traits::provide(providers);
middle::region::provide(providers);
cstore::provide(providers);
rustc_metadata::provide(providers);
lint::provide(providers);
rustc_lint::provide(providers);
rustc_codegen_utils::provide(providers);
rustc_codegen_ssa::provide(providers);
}

pub fn default_provide_extern(providers: &mut ty::query::Providers<'_>) {
cstore::provide_extern(providers);
rustc_metadata::provide_extern(providers);
rustc_codegen_ssa::provide_extern(providers);
}

@@ -1,11 +1,12 @@
//! Validates all used crates and extern libraries and loads their metadata

use crate::cstore::CStore;
use crate::locator::{CrateLocator, CratePaths};
use crate::rmeta::{CrateMetadata, CrateNumMap, CrateRoot, CrateDep, MetadataBlob};

use rustc::hir::def_id::CrateNum;
use rustc_data_structures::svh::Svh;
use rustc_data_structures::sync::Lrc;
use rustc_index::vec::IndexVec;
use rustc::middle::cstore::DepKind;
use rustc::session::{Session, CrateDisambiguator};
use rustc::session::config::{Sanitizer, self};
@@ -21,18 +22,22 @@ use std::{cmp, fs};

use syntax::ast;
use syntax::attr;
use syntax::edition::Edition;
use syntax::expand::allocator::{global_allocator_spans, AllocatorKind};
use syntax::symbol::{Symbol, sym};
use syntax::span_fatal;
use syntax_expand::base::SyntaxExtension;
use syntax_pos::{Span, DUMMY_SP};
use log::{debug, info, log_enabled};
use proc_macro::bridge::client::ProcMacro;

use rustc_error_codes::*;

crate struct Library {
pub source: CrateSource,
pub metadata: MetadataBlob,
#[derive(Clone)]
pub struct CStore {
metas: IndexVec<CrateNum, Option<Lrc<CrateMetadata>>>,
crate injected_panic_runtime: Option<CrateNum>,
crate allocator_kind: Option<AllocatorKind>,
}

pub struct CrateLoader<'a> {
@@ -44,6 +49,33 @@ pub struct CrateLoader<'a> {
cstore: CStore,
}

pub enum LoadedMacro {
MacroDef(ast::Item, Edition),
ProcMacro(SyntaxExtension),
}

crate struct Library {
pub source: CrateSource,
pub metadata: MetadataBlob,
}

enum LoadResult {
Previous(CrateNum),
Loaded(Library),
}

enum LoadError<'a> {
LocatorError(CrateLocator<'a>),
}

impl<'a> LoadError<'a> {
fn report(self) -> ! {
match self {
LoadError::LocatorError(locator) => locator.report_errs(),
}
}
}

fn dump_crates(cstore: &CStore) {
info!("resolved crates:");
cstore.iter_crate_data(|cnum, data| {
@@ -58,21 +90,58 @@ fn dump_crates(cstore: &CStore) {
});
}

enum LoadResult {
Previous(CrateNum),
Loaded(Library),
}
impl CStore {
crate fn alloc_new_crate_num(&mut self) -> CrateNum {
self.metas.push(None);
CrateNum::new(self.metas.len() - 1)
}

enum LoadError<'a> {
LocatorError(CrateLocator<'a>),
}
crate fn get_crate_data(&self, cnum: CrateNum) -> &CrateMetadata {
self.metas[cnum].as_ref()
.unwrap_or_else(|| panic!("Failed to get crate data for {:?}", cnum))
}

impl<'a> LoadError<'a> {
fn report(self) -> ! {
match self {
LoadError::LocatorError(locator) => locator.report_errs(),
crate fn set_crate_data(&mut self, cnum: CrateNum, data: CrateMetadata) {
assert!(self.metas[cnum].is_none(), "Overwriting crate metadata entry");
self.metas[cnum] = Some(Lrc::new(data));
}

crate fn iter_crate_data(&self, mut f: impl FnMut(CrateNum, &CrateMetadata)) {
for (cnum, data) in self.metas.iter_enumerated() {
if let Some(data) = data {
f(cnum, data);
}
}
}

fn push_dependencies_in_postorder(&self, deps: &mut Vec<CrateNum>, cnum: CrateNum) {
if !deps.contains(&cnum) {
let data = self.get_crate_data(cnum);
for &dep in data.dependencies().iter() {
if dep != cnum {
self.push_dependencies_in_postorder(deps, dep);
}
}

deps.push(cnum);
}
}

crate fn crate_dependencies_in_postorder(&self, cnum: CrateNum) -> Vec<CrateNum> {
let mut deps = Vec::new();
if cnum == LOCAL_CRATE {
self.iter_crate_data(|cnum, _| self.push_dependencies_in_postorder(&mut deps, cnum));
} else {
self.push_dependencies_in_postorder(&mut deps, cnum);
}
deps
}

crate fn crate_dependencies_in_reverse_postorder(&self, cnum: CrateNum) -> Vec<CrateNum> {
let mut deps = self.crate_dependencies_in_postorder(cnum);
deps.reverse();
deps
}
}

impl<'a> CrateLoader<'a> {
@@ -85,7 +154,15 @@ impl<'a> CrateLoader<'a> {
sess,
metadata_loader,
local_crate_name: Symbol::intern(local_crate_name),
cstore: Default::default(),
cstore: CStore {
// We add an empty entry for LOCAL_CRATE (which maps to zero) in
// order to make array indices in `metas` match with the
// corresponding `CrateNum`. This first entry will always remain
// `None`.
metas: IndexVec::from_elem_n(None, 1),
injected_panic_runtime: None,
allocator_kind: None,
}
}
}

This file was deleted.

@@ -24,14 +24,15 @@ extern crate rustc;
#[macro_use]
extern crate rustc_data_structures;

pub use rmeta::{provide, provide_extern};

mod dependency_format;
mod foreign_modules;
mod link_args;
mod native_libs;
mod rmeta;

pub mod creader;
pub mod cstore;
pub mod dynamic_lib;
pub mod locator;

@@ -1,4 +1,4 @@
use crate::cstore::{self, LoadedMacro};
use crate::creader::{CStore, LoadedMacro};
use crate::link_args;
use crate::native_libs;
use crate::foreign_modules;
@@ -382,7 +382,7 @@ pub fn provide(providers: &mut Providers<'_>) {
};
}

impl cstore::CStore {
impl CStore {
pub fn export_macros_untracked(&self, cnum: CrateNum) {
let data = self.get_crate_data(cnum);
let mut dep_kind = data.dep_kind.lock();
@@ -458,7 +458,7 @@ impl cstore::CStore {
}
}

impl CrateStore for cstore::CStore {
impl CrateStore for CStore {
fn crate_data_as_any(&self, cnum: CrateNum) -> &dyn Any {
self.get_crate_data(cnum)
}
@@ -19,7 +19,7 @@ use rustc::hir::def::{self, *};
use rustc::hir::def_id::{CRATE_DEF_INDEX, LOCAL_CRATE, DefId};
use rustc::ty;
use rustc::middle::cstore::CrateStore;
use rustc_metadata::cstore::LoadedMacro;
use rustc_metadata::creader::LoadedMacro;

use std::cell::Cell;
use std::ptr;
@@ -32,8 +32,7 @@ use rustc::ty::{self, DefIdTree, ResolverOutputs};
use rustc::util::nodemap::{NodeMap, NodeSet, FxHashMap, FxHashSet, DefIdMap};
use rustc::span_bug;

use rustc_metadata::creader::CrateLoader;
use rustc_metadata::cstore::CStore;
use rustc_metadata::creader::{CrateLoader, CStore};

use syntax::{struct_span_err, unwrap_or};
use syntax::ast::{self, Name, NodeId, Ident, FloatTy, IntTy, UintTy};
@@ -10,7 +10,7 @@ use syntax_pos::Span;
use rustc::hir;
use rustc::hir::def::{Res, DefKind, CtorKind};
use rustc::hir::def_id::DefId;
use rustc_metadata::cstore::LoadedMacro;
use rustc_metadata::creader::LoadedMacro;
use rustc::ty;
use rustc::util::nodemap::FxHashSet;

0 comments on commit 753ce45

Please sign in to comment.
You can’t perform that action at this time.