2019-03-26 13:07:13 -05:00
|
|
|
use crate::queries::Queries;
|
|
|
|
use crate::util;
|
|
|
|
pub use crate::passes::BoxedResolver;
|
|
|
|
|
2018-12-08 13:30:23 -06:00
|
|
|
use rustc::lint;
|
|
|
|
use rustc::session::config::{self, Input};
|
|
|
|
use rustc::session::{DiagnosticOutput, Session};
|
|
|
|
use rustc::util::common::ErrorReported;
|
|
|
|
use rustc_codegen_utils::codegen_backend::CodegenBackend;
|
|
|
|
use rustc_data_structures::OnDrop;
|
|
|
|
use rustc_data_structures::sync::Lrc;
|
|
|
|
use rustc_data_structures::fx::{FxHashSet, FxHashMap};
|
|
|
|
use rustc_metadata::cstore::CStore;
|
|
|
|
use std::path::PathBuf;
|
|
|
|
use std::result;
|
|
|
|
use std::sync::{Arc, Mutex};
|
|
|
|
use syntax;
|
|
|
|
use syntax::source_map::{FileLoader, SourceMap};
|
2019-04-05 17:15:49 -05:00
|
|
|
use syntax_pos::edition;
|
2018-12-08 13:30:23 -06:00
|
|
|
|
|
|
|
pub type Result<T> = result::Result<T, ErrorReported>;
|
|
|
|
|
|
|
|
/// Represents a compiler session.
|
|
|
|
/// Can be used run `rustc_interface` queries.
|
|
|
|
/// Created by passing `Config` to `run_compiler`.
|
|
|
|
pub struct Compiler {
|
|
|
|
pub(crate) sess: Lrc<Session>,
|
|
|
|
codegen_backend: Lrc<Box<dyn CodegenBackend>>,
|
|
|
|
source_map: Lrc<SourceMap>,
|
|
|
|
pub(crate) input: Input,
|
|
|
|
pub(crate) input_path: Option<PathBuf>,
|
|
|
|
pub(crate) output_dir: Option<PathBuf>,
|
|
|
|
pub(crate) output_file: Option<PathBuf>,
|
|
|
|
pub(crate) queries: Queries,
|
|
|
|
pub(crate) cstore: Lrc<CStore>,
|
|
|
|
pub(crate) crate_name: Option<String>,
|
2019-10-10 18:33:00 -05:00
|
|
|
pub(crate) register_lints: Option<Box<dyn Fn(&Session, &mut lint::LintStore) + Send + Sync>>,
|
2018-12-08 13:30:23 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Compiler {
|
|
|
|
pub fn session(&self) -> &Lrc<Session> {
|
|
|
|
&self.sess
|
|
|
|
}
|
|
|
|
pub fn codegen_backend(&self) -> &Lrc<Box<dyn CodegenBackend>> {
|
|
|
|
&self.codegen_backend
|
|
|
|
}
|
|
|
|
pub fn cstore(&self) -> &Lrc<CStore> {
|
|
|
|
&self.cstore
|
|
|
|
}
|
|
|
|
pub fn source_map(&self) -> &Lrc<SourceMap> {
|
|
|
|
&self.source_map
|
|
|
|
}
|
|
|
|
pub fn input(&self) -> &Input {
|
|
|
|
&self.input
|
|
|
|
}
|
|
|
|
pub fn output_dir(&self) -> &Option<PathBuf> {
|
|
|
|
&self.output_dir
|
|
|
|
}
|
|
|
|
pub fn output_file(&self) -> &Option<PathBuf> {
|
|
|
|
&self.output_file
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The compiler configuration
|
|
|
|
pub struct Config {
|
|
|
|
/// Command line options
|
|
|
|
pub opts: config::Options,
|
|
|
|
|
|
|
|
/// cfg! configuration in addition to the default ones
|
|
|
|
pub crate_cfg: FxHashSet<(String, Option<String>)>,
|
|
|
|
|
|
|
|
pub input: Input,
|
|
|
|
pub input_path: Option<PathBuf>,
|
|
|
|
pub output_dir: Option<PathBuf>,
|
|
|
|
pub output_file: Option<PathBuf>,
|
|
|
|
pub file_loader: Option<Box<dyn FileLoader + Send + Sync>>,
|
|
|
|
pub diagnostic_output: DiagnosticOutput,
|
|
|
|
|
|
|
|
/// Set to capture stderr output during compiler execution
|
|
|
|
pub stderr: Option<Arc<Mutex<Vec<u8>>>>,
|
|
|
|
|
|
|
|
pub crate_name: Option<String>,
|
|
|
|
pub lint_caps: FxHashMap<lint::LintId, lint::Level>,
|
2019-10-10 18:33:00 -05:00
|
|
|
|
|
|
|
pub register_lints: Option<Box<dyn Fn(&Session, &mut lint::LintStore) + Send + Sync>>,
|
2018-12-08 13:30:23 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn run_compiler_in_existing_thread_pool<F, R>(config: Config, f: F) -> R
|
|
|
|
where
|
|
|
|
F: FnOnce(&Compiler) -> R,
|
|
|
|
{
|
|
|
|
let (sess, codegen_backend, source_map) = util::create_session(
|
|
|
|
config.opts,
|
|
|
|
config.crate_cfg,
|
|
|
|
config.diagnostic_output,
|
|
|
|
config.file_loader,
|
|
|
|
config.input_path.clone(),
|
|
|
|
config.lint_caps,
|
|
|
|
);
|
|
|
|
|
|
|
|
let cstore = Lrc::new(CStore::new(codegen_backend.metadata_loader()));
|
|
|
|
|
|
|
|
let compiler = Compiler {
|
|
|
|
sess,
|
|
|
|
codegen_backend,
|
|
|
|
source_map,
|
|
|
|
cstore,
|
|
|
|
input: config.input,
|
|
|
|
input_path: config.input_path,
|
|
|
|
output_dir: config.output_dir,
|
|
|
|
output_file: config.output_file,
|
|
|
|
queries: Default::default(),
|
|
|
|
crate_name: config.crate_name,
|
2019-10-10 18:33:00 -05:00
|
|
|
register_lints: config.register_lints,
|
2018-12-08 13:30:23 -06:00
|
|
|
};
|
|
|
|
|
2019-04-17 12:26:38 -05:00
|
|
|
let _sess_abort_error = OnDrop(|| {
|
|
|
|
compiler.sess.diagnostic().print_error_count(&util::diagnostics_registry());
|
|
|
|
});
|
2018-12-08 13:30:23 -06:00
|
|
|
|
2019-10-03 20:29:25 -05:00
|
|
|
f(&compiler)
|
2018-12-08 13:30:23 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn run_compiler<F, R>(mut config: Config, f: F) -> R
|
|
|
|
where
|
|
|
|
F: FnOnce(&Compiler) -> R + Send,
|
|
|
|
R: Send,
|
|
|
|
{
|
2019-03-28 12:58:43 -05:00
|
|
|
let stderr = config.stderr.take();
|
|
|
|
util::spawn_thread_pool(
|
2019-04-05 17:15:49 -05:00
|
|
|
config.opts.edition,
|
2019-03-28 12:58:43 -05:00
|
|
|
config.opts.debugging_opts.threads,
|
|
|
|
&stderr,
|
|
|
|
|| run_compiler_in_existing_thread_pool(config, f),
|
|
|
|
)
|
2018-12-08 13:30:23 -06:00
|
|
|
}
|
|
|
|
|
2019-05-19 20:39:48 -05:00
|
|
|
pub fn default_thread_pool<F, R>(edition: edition::Edition, f: F) -> R
|
2018-12-08 13:30:23 -06:00
|
|
|
where
|
|
|
|
F: FnOnce() -> R + Send,
|
|
|
|
R: Send,
|
|
|
|
{
|
2019-09-30 15:27:28 -05:00
|
|
|
// the 1 here is duplicating code in config.opts.debugging_opts.threads
|
|
|
|
// which also defaults to 1; it ultimately doesn't matter as the default
|
|
|
|
// isn't threaded, and just ignores this parameter
|
|
|
|
util::spawn_thread_pool(edition, 1, &None, f)
|
2018-12-08 13:30:23 -06:00
|
|
|
}
|