提交 4f45b061 编写于 作者: B bjorn3

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

上级 90f34b5f
......@@ -454,7 +454,7 @@ fn GetModuleFileNameW(hModule: usize,
// 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 @@ pub fn and_then<F: FnOnce() -> Compilation>(self, next: F) -> 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 @@ fn early_callback(&mut self,
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 @@ fn late_callback(&mut self,
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 @@ fn some_input(&mut self,
(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 @@ fn no_input(&mut self,
// 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 @@ fn late_callback(&mut self,
.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)
});
......
......@@ -31,11 +31,11 @@
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 @@ fn early_callback(&mut self,
_: &errors::registry::Registry,
_: config::ErrorOutputType)
-> Compilation {
self.count *= 2;
*self.count *= 2;
Compilation::Continue
}
......@@ -56,13 +56,13 @@ fn late_callback(&mut self,
_: &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 @@ fn no_input(&mut self,
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 @@ fn build_controller(&mut self,
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);
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册