Skip to content

Commit

Permalink
Auto merge of #33538 - Ms2ger:LocalCrateReader, r=arielb1
Browse files Browse the repository at this point in the history
Refactor code around LocalCrateReader.
  • Loading branch information
bors committed May 14, 2016
2 parents dee865a + c0e321a commit d3ec9d4
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 43 deletions.
6 changes: 3 additions & 3 deletions src/librustc/hir/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ pub struct Map<'ast> {
/// plain old integers.
map: RefCell<Vec<MapEntry<'ast>>>,

definitions: &'ast RefCell<Definitions>,
definitions: RefCell<Definitions>,
}

impl<'ast> Map<'ast> {
Expand Down Expand Up @@ -790,7 +790,7 @@ pub fn collect_definitions<'ast>(krate: &'ast ast::Crate) -> Definitions {
}

pub fn map_crate<'ast>(forest: &'ast mut Forest,
definitions: &'ast RefCell<Definitions>)
definitions: Definitions)
-> Map<'ast> {
let mut collector = NodeCollector::root(&forest.krate);
intravisit::walk_crate(&mut collector, &forest.krate);
Expand All @@ -816,7 +816,7 @@ pub fn map_crate<'ast>(forest: &'ast mut Forest,
forest: forest,
dep_graph: forest.dep_graph.clone(),
map: RefCell::new(map),
definitions: definitions,
definitions: RefCell::new(definitions),
}
}

Expand Down
15 changes: 7 additions & 8 deletions src/librustc_driver/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use rustc_borrowck as borrowck;
use rustc_incremental;
use rustc_resolve as resolve;
use rustc_metadata::macro_import;
use rustc_metadata::creader::LocalCrateReader;
use rustc_metadata::creader::read_local_crates;
use rustc_metadata::cstore::CStore;
use rustc_trans::back::link;
use rustc_trans::back::write;
Expand Down Expand Up @@ -155,22 +155,21 @@ pub fn compile_input(sess: &Session,
let dep_graph = DepGraph::new(sess.opts.build_dep_graph());

// Collect defintions for def ids.
let defs = &RefCell::new(time(sess.time_passes(),
"collecting defs",
|| hir_map::collect_definitions(&expanded_crate)));
let mut defs = time(sess.time_passes(),
"collecting defs",
|| hir_map::collect_definitions(&expanded_crate));

time(sess.time_passes(),
"external crate/lib resolution",
|| LocalCrateReader::new(sess, &cstore, defs, &expanded_crate, &id)
.read_crates(&dep_graph));
|| read_local_crates(sess, &cstore, &defs, &expanded_crate, &id, &dep_graph));

time(sess.time_passes(),
"early lint checks",
|| lint::check_ast_crate(sess, &expanded_crate));

let (analysis, resolutions, mut hir_forest) = {
let defs = &mut *defs.borrow_mut();
lower_and_resolve(sess, &id, defs, &expanded_crate, dep_graph, control.make_glob_map)
lower_and_resolve(sess, &id, &mut defs, &expanded_crate, dep_graph,
control.make_glob_map)
};

// Discard MTWT tables that aren't required past lowering to HIR.
Expand Down
11 changes: 5 additions & 6 deletions src/librustc_driver/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,9 @@ use rustc::traits::ProjectionMode;
use rustc::ty::{self, Ty, TyCtxt, TypeFoldable};
use rustc::infer::{self, InferOk, InferResult, TypeOrigin};
use rustc_metadata::cstore::CStore;
use rustc_metadata::creader::LocalCrateReader;
use rustc_metadata::creader::read_local_crates;
use rustc::hir::map as hir_map;
use rustc::session::{self, config};
use std::cell::RefCell;
use std::rc::Rc;
use syntax::ast;
use syntax::abi::Abi;
Expand Down Expand Up @@ -120,13 +119,13 @@ fn test_env<F>(source_string: &str,

let dep_graph = DepGraph::new(false);
let krate = driver::assign_node_ids(&sess, krate);
let defs = &RefCell::new(hir_map::collect_definitions(&krate));
LocalCrateReader::new(&sess, &cstore, defs, &krate, "test_crate").read_crates(&dep_graph);
let mut defs = hir_map::collect_definitions(&krate);
read_local_crates(&sess, &cstore, &defs, &krate, "test_crate", &dep_graph);
let _ignore = dep_graph.in_ignore();

let (_, resolutions, mut hir_forest) = {
let (defs, dep_graph) = (&mut *defs.borrow_mut(), dep_graph.clone());
driver::lower_and_resolve(&sess, "test-crate", defs, &krate, dep_graph, MakeGlobMap::No)
driver::lower_and_resolve(&sess, "test-crate", &mut defs, &krate, dep_graph.clone(),
MakeGlobMap::No)
};

let arenas = ty::CtxtArenas::new();
Expand Down
36 changes: 23 additions & 13 deletions src/librustc_metadata/creader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ use syntax::parse::token::InternedString;
use syntax::visit;
use log;

pub struct LocalCrateReader<'a> {
struct LocalCrateReader<'a> {
sess: &'a Session,
cstore: &'a CStore,
creader: CrateReader<'a>,
krate: &'a ast::Crate,
defintions: &'a RefCell<hir_map::Definitions>,
definitions: &'a hir_map::Definitions,
}

pub struct CrateReader<'a> {
Expand Down Expand Up @@ -841,25 +841,25 @@ impl<'a> CrateReader<'a> {
}

impl<'a> LocalCrateReader<'a> {
pub fn new(sess: &'a Session,
cstore: &'a CStore,
defs: &'a RefCell<hir_map::Definitions>,
krate: &'a ast::Crate,
local_crate_name: &str)
-> LocalCrateReader<'a> {
fn new(sess: &'a Session,
cstore: &'a CStore,
defs: &'a hir_map::Definitions,
krate: &'a ast::Crate,
local_crate_name: &str)
-> LocalCrateReader<'a> {
LocalCrateReader {
sess: sess,
cstore: cstore,
creader: CrateReader::new(sess, cstore, local_crate_name),
krate: krate,
defintions: defs,
definitions: defs,
}
}

// Traverses an AST, reading all the information about use'd crates and
// extern libraries necessary for later resolving, typechecking, linking,
// etc.
pub fn read_crates(&mut self, dep_graph: &DepGraph) {
fn read_crates(&mut self, dep_graph: &DepGraph) {
let _task = dep_graph.in_task(DepNode::CrateReader);

self.process_crate(self.krate);
Expand Down Expand Up @@ -902,9 +902,8 @@ impl<'a> LocalCrateReader<'a> {
PathKind::Crate,
true);

let defs = self.defintions.borrow();
let def_id = defs.opt_local_def_id(i.id).unwrap();
let len = defs.def_path(def_id.index).data.len();
let def_id = self.definitions.opt_local_def_id(i.id).unwrap();
let len = self.definitions.def_path(def_id.index).data.len();

self.creader.update_extern_crate(cnum,
ExternCrate {
Expand Down Expand Up @@ -982,6 +981,17 @@ impl<'a> LocalCrateReader<'a> {
}
}

/// Traverses an AST, reading all the information about use'd crates and extern
/// libraries necessary for later resolving, typechecking, linking, etc.
pub fn read_local_crates(sess: & Session,
cstore: & CStore,
defs: & hir_map::Definitions,
krate: & ast::Crate,
local_crate_name: &str,
dep_graph: &DepGraph) {
LocalCrateReader::new(sess, cstore, defs, krate, local_crate_name).read_crates(dep_graph)
}

/// Imports the codemap from an external crate into the codemap of the crate
/// currently being compiled (the "local crate").
///
Expand Down
10 changes: 5 additions & 5 deletions src/librustdoc/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use rustc::lint;
use rustc_trans::back::link;
use rustc_resolve as resolve;
use rustc_metadata::cstore::CStore;
use rustc_metadata::creader::LocalCrateReader;
use rustc_metadata::creader::read_local_crates;

use syntax::{ast, codemap, errors};
use syntax::errors::emitter::ColorConfig;
Expand Down Expand Up @@ -153,13 +153,13 @@ pub fn run_core(search_paths: SearchPaths,
let krate = driver::assign_node_ids(&sess, krate);
let dep_graph = DepGraph::new(false);

let defs = &RefCell::new(hir_map::collect_definitions(&krate));
LocalCrateReader::new(&sess, &cstore, &defs, &krate, &name).read_crates(&dep_graph);
let mut defs = hir_map::collect_definitions(&krate);
read_local_crates(&sess, &cstore, &defs, &krate, &name, &dep_graph);

// Lower ast -> hir and resolve.
let (analysis, resolutions, mut hir_forest) = {
let defs = &mut *defs.borrow_mut();
driver::lower_and_resolve(&sess, &name, defs, &krate, dep_graph, resolve::MakeGlobMap::No)
driver::lower_and_resolve(&sess, &name, &mut defs, &krate, dep_graph,
resolve::MakeGlobMap::No)
};

let arenas = ty::CtxtArenas::new();
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ pub fn run(input: &str,
.expect("phase_2_configure_and_expand aborted in rustdoc!");
let krate = driver::assign_node_ids(&sess, krate);
let dep_graph = DepGraph::new(false);
let defs = &RefCell::new(hir_map::collect_definitions(&krate));
let defs = hir_map::collect_definitions(&krate);

let mut dummy_resolver = DummyResolver;
let krate = lower_crate(&krate, &sess, &mut dummy_resolver);
Expand Down
13 changes: 6 additions & 7 deletions src/test/run-make/execution-engine/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ extern crate rustc_metadata;
extern crate rustc_resolve;
#[macro_use] extern crate syntax;

use std::cell::RefCell;
use std::ffi::{CStr, CString};
use std::mem::transmute;
use std::path::PathBuf;
Expand All @@ -35,7 +34,7 @@ use rustc::session::config::{self, basic_options, build_configuration, Input, Op
use rustc::session::build_session;
use rustc_driver::{driver, abort_on_err};
use rustc_resolve::MakeGlobMap;
use rustc_metadata::creader::LocalCrateReader;
use rustc_metadata::creader::read_local_crates;
use rustc_metadata::cstore::CStore;
use libc::c_void;

Expand Down Expand Up @@ -240,14 +239,14 @@ fn compile_program(input: &str, sysroot: PathBuf)

let dep_graph = DepGraph::new(sess.opts.build_dep_graph());
let krate = driver::assign_node_ids(&sess, krate);
let defs = RefCell::new(ast_map::collect_definitions(&krate));
LocalCrateReader::new(&sess, &cstore, &defs, &krate, &id).read_crates(&dep_graph);
let mut defs = ast_map::collect_definitions(&krate);
read_local_crates(&sess, &cstore, &defs, &krate, &id, &dep_graph);
let (analysis, resolutions, mut hir_forest) = {
let defs = &mut *defs.borrow_mut();
driver::lower_and_resolve(&sess, &id, defs, &krate, dep_graph, MakeGlobMap::No)
driver::lower_and_resolve(&sess, &id, &mut defs, &krate, dep_graph, MakeGlobMap::No)
};

let arenas = ty::CtxtArenas::new();
let ast_map = ast_map::map_crate(&mut hir_forest, &defs);
let ast_map = ast_map::map_crate(&mut hir_forest, defs);

abort_on_err(driver::phase_3_run_analysis_passes(
&sess, ast_map, analysis, resolutions, &arenas, &id,
Expand Down

0 comments on commit d3ec9d4

Please sign in to comment.