Add AdHocCalls and pass self to build_controller as Box<Self>

This commit is contained in:
bjorn3 2018-05-17 20:32:09 +02:00
parent 90f34b5f83
commit 4f45b0611c
2 changed files with 65 additions and 34 deletions

View file

@ -454,7 +454,7 @@ fn get_codegen_sysroot(backend_name: &str) -> fn() -> Box<CodegenBackend> {
// See comments on CompilerCalls below for details about the callbacks argument.
// The FileLoader provides a way to load files from sources other than the file system.
pub fn run_compiler<'a>(args: &[String],
callbacks: &mut (CompilerCalls<'a> + sync::Send),
callbacks: Box<CompilerCalls<'a> + sync::Send + 'a>,
file_loader: Option<Box<FileLoader + Send + Sync + 'static>>,
emitter_dest: Option<Box<Write + Send>>)
-> (CompileResult, Option<Session>)
@ -478,7 +478,7 @@ fn run_compiler_with_pool<'a>(
matches: getopts::Matches,
sopts: config::Options,
cfg: ast::CrateConfig,
callbacks: &mut (CompilerCalls<'a> + sync::Send),
mut callbacks: Box<CompilerCalls<'a> + sync::Send + 'a>,
file_loader: Option<Box<FileLoader + Send + Sync + 'static>>,
emitter_dest: Option<Box<Write + Send>>
) -> (CompileResult, Option<Session>) {
@ -642,12 +642,12 @@ impl Compilation {
}
}
// A trait for customising the compilation process. Offers a number of hooks for
// executing custom code or customising input.
/// A trait for customising the compilation process. Offers a number of hooks for
/// executing custom code or customising input.
pub trait CompilerCalls<'a> {
// Hook for a callback early in the process of handling arguments. This will
// be called straight after options have been parsed but before anything
// else (e.g., selecting input and output).
/// Hook for a callback early in the process of handling arguments. This will
/// be called straight after options have been parsed but before anything
/// else (e.g., selecting input and output).
fn early_callback(&mut self,
_: &getopts::Matches,
_: &config::Options,
@ -658,9 +658,9 @@ pub trait CompilerCalls<'a> {
Compilation::Continue
}
// Hook for a callback late in the process of handling arguments. This will
// be called just before actual compilation starts (and before build_controller
// is called), after all arguments etc. have been completely handled.
/// Hook for a callback late in the process of handling arguments. This will
/// be called just before actual compilation starts (and before build_controller
/// is called), after all arguments etc. have been completely handled.
fn late_callback(&mut self,
_: &CodegenBackend,
_: &getopts::Matches,
@ -673,9 +673,9 @@ pub trait CompilerCalls<'a> {
Compilation::Continue
}
// Called after we extract the input from the arguments. Gives the implementer
// an opportunity to change the inputs or to add some custom input handling.
// The default behaviour is to simply pass through the inputs.
/// Called after we extract the input from the arguments. Gives the implementer
/// an opportunity to change the inputs or to add some custom input handling.
/// The default behaviour is to simply pass through the inputs.
fn some_input(&mut self,
input: Input,
input_path: Option<PathBuf>)
@ -683,11 +683,11 @@ pub trait CompilerCalls<'a> {
(input, input_path)
}
// Called after we extract the input from the arguments if there is no valid
// input. Gives the implementer an opportunity to supply alternate input (by
// returning a Some value) or to add custom behaviour for this error such as
// emitting error messages. Returning None will cause compilation to stop
// at this point.
/// Called after we extract the input from the arguments if there is no valid
/// input. Gives the implementer an opportunity to supply alternate input (by
/// returning a Some value) or to add custom behaviour for this error such as
/// emitting error messages. Returning None will cause compilation to stop
/// at this point.
fn no_input(&mut self,
_: &getopts::Matches,
_: &config::Options,
@ -701,13 +701,41 @@ pub trait CompilerCalls<'a> {
// Create a CompilController struct for controlling the behaviour of
// compilation.
fn build_controller(&mut self, _: &Session, _: &getopts::Matches) -> CompileController<'a>;
fn build_controller(
self: Box<Self>,
_: &Session,
_: &getopts::Matches
) -> CompileController<'a>;
}
// CompilerCalls instance for a regular rustc build.
/// CompilerCalls instance for a regular rustc build.
#[derive(Copy, Clone)]
pub struct RustcDefaultCalls;
/// CompilerCalls instance for quick access to the result of one compile phase.
pub enum AdHocCalls<'a> {
AfterAnalysis(Compilation, Box<Fn(&mut ::driver::CompileState) + 'a>)
}
impl<'a> CompilerCalls<'a> for AdHocCalls<'a> {
fn build_controller(
self: Box<Self>,
_: &Session,
_: &getopts::Matches
) -> CompileController<'a> {
let mut control = CompileController::basic();
match *self {
AdHocCalls::AfterAnalysis(c, f) => {
control.after_analysis.stop = c;
control.after_analysis.callback = f;
}
}
control
}
}
// FIXME remove these and use winapi 0.3 instead
// Duplicates: bootstrap/compile.rs, librustc_errors/emitter.rs
#[cfg(unix)]
@ -878,7 +906,7 @@ impl<'a> CompilerCalls<'a> for RustcDefaultCalls {
.and_then(|| RustcDefaultCalls::list_metadata(sess, cstore, matches, input))
}
fn build_controller(&mut self,
fn build_controller(self: Box<Self>,
sess: &Session,
matches: &getopts::Matches)
-> CompileController<'a> {
@ -1693,7 +1721,7 @@ pub fn main() {
}))
.collect::<Vec<_>>();
run_compiler(&args,
&mut RustcDefaultCalls,
Box::new(RustcDefaultCalls),
None,
None)
});

View file

@ -31,11 +31,11 @@ use syntax::ast;
use std::path::PathBuf;
struct TestCalls {
count: u32
struct TestCalls<'a> {
count: &'a mut u32
}
impl<'a> CompilerCalls<'a> for TestCalls {
impl<'a> CompilerCalls<'a> for TestCalls<'a> {
fn early_callback(&mut self,
_: &getopts::Matches,
_: &config::Options,
@ -43,7 +43,7 @@ impl<'a> CompilerCalls<'a> for TestCalls {
_: &errors::registry::Registry,
_: config::ErrorOutputType)
-> Compilation {
self.count *= 2;
*self.count *= 2;
Compilation::Continue
}
@ -56,13 +56,13 @@ impl<'a> CompilerCalls<'a> for TestCalls {
_: &Option<PathBuf>,
_: &Option<PathBuf>)
-> Compilation {
self.count *= 3;
*self.count *= 3;
Compilation::Stop
}
fn some_input(&mut self, input: Input, input_path: Option<PathBuf>)
-> (Input, Option<PathBuf>) {
self.count *= 5;
*self.count *= 5;
(input, input_path)
}
@ -77,7 +77,7 @@ impl<'a> CompilerCalls<'a> for TestCalls {
panic!("This shouldn't happen");
}
fn build_controller(&mut self,
fn build_controller(self: Box<Self>,
_: &Session,
_: &getopts::Matches)
-> driver::CompileController<'a> {
@ -87,9 +87,12 @@ impl<'a> CompilerCalls<'a> for TestCalls {
fn main() {
let mut tc = TestCalls { count: 1 };
// we should never get use this filename, but lets make sure they are valid args.
let args = vec!["compiler-calls".to_string(), "foo.rs".to_string()];
rustc_driver::run_compiler(&args, &mut tc, None, None);
assert_eq!(tc.count, 30);
let mut count = 1;
{
let tc = TestCalls { count: &mut count };
// we should never get use this filename, but lets make sure they are valid args.
let args = vec!["compiler-calls".to_string(), "foo.rs".to_string()];
rustc_driver::run_compiler(&args, Box::new(tc), None, None);
}
assert_eq!(count, 30);
}