1- // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
1+ // Copyright 2014-2015 The Rust Project Developers. See the COPYRIGHT
22// file at the top-level directory of this distribution and at
33// http://rust-lang.org/COPYRIGHT.
44//
@@ -108,8 +108,9 @@ pub fn run(args: Vec<String>) -> int {
108108pub fn run_compiler < ' a > ( args : & [ String ] ,
109109 callbacks : & mut CompilerCalls < ' a > ) {
110110 macro_rules! do_or_return { ( $expr: expr) => {
111- if $expr {
112- return ;
111+ match $expr {
112+ Compilation :: Stop => return ,
113+ Compilation :: Continue => { }
113114 }
114115 } }
115116
@@ -144,7 +145,7 @@ pub fn run_compiler<'a>(args: &[String],
144145 // It is somewhat unfortunate that this is hardwired in - this is forced by
145146 // the fact that pretty_print_input requires the session by value.
146147 let pretty = callbacks. parse_pretty ( & sess, & matches) ;
147- match pretty. into_iter ( ) . next ( ) {
148+ match pretty {
148149 Some ( ( ppm, opt_uii) ) => {
149150 pretty:: pretty_print_input ( sess, cfg, & input, ppm, opt_uii, ofile) ;
150151 return ;
@@ -180,26 +181,43 @@ fn make_input(free_matches: &[String]) -> Option<(Input, Option<Path>)> {
180181 }
181182}
182183
184+ // Whether to stop or continue compilation.
185+ #[ derive( Copy , Debug , Eq , PartialEq ) ]
186+ pub enum Compilation {
187+ Stop ,
188+ Continue ,
189+ }
190+
191+ impl Compilation {
192+ pub fn and_then < F : FnOnce ( ) -> Compilation > ( self , next : F ) -> Compilation {
193+ match self {
194+ Compilation :: Stop => Compilation :: Stop ,
195+ Compilation :: Continue => next ( )
196+ }
197+ }
198+ }
199+
183200// A trait for customising the compilation process. Offers a number of hooks for
184201// executing custom code or customising input.
185202pub trait CompilerCalls < ' a > {
186203 // Hook for a callback early in the process of handling arguments. This will
187204 // be called straight after options have been parsed but before anything
188- // else (e.g., selecting input and output). Return true to terminate compilation,
189- // false to continue.
190- fn early_callback ( & mut self , & getopts:: Matches , & diagnostics:: registry:: Registry ) -> bool ;
205+ // else (e.g., selecting input and output).
206+ fn early_callback ( & mut self ,
207+ & getopts:: Matches ,
208+ & diagnostics:: registry:: Registry )
209+ -> Compilation ;
191210
192211 // Hook for a callback late in the process of handling arguments. This will
193212 // be called just before actual compilation starts (and before build_controller
194- // is called), after all arguments etc. have been completely handled. Return
195- // true to terminate compilation, false to continue.
213+ // is called), after all arguments etc. have been completely handled.
196214 fn late_callback ( & mut self ,
197215 & getopts:: Matches ,
198216 & Session ,
199217 & Input ,
200218 & Option < Path > ,
201219 & Option < Path > )
202- -> bool ;
220+ -> Compilation ;
203221
204222 // Called after we extract the input from the arguments. Gives the implementer
205223 // an opportunity to change the inputs or to add some custom input handling.
@@ -253,7 +271,7 @@ impl<'a> CompilerCalls<'a> for RustcDefaultCalls {
253271 fn early_callback ( & mut self ,
254272 matches : & getopts:: Matches ,
255273 descriptions : & diagnostics:: registry:: Registry )
256- -> bool {
274+ -> Compilation {
257275 match matches. opt_str ( "explain" ) {
258276 Some ( ref code) => {
259277 match descriptions. find_description ( & code[ ] ) {
@@ -264,12 +282,12 @@ impl<'a> CompilerCalls<'a> for RustcDefaultCalls {
264282 early_error ( & format ! ( "no extended information for {}" , code) [ ] ) ;
265283 }
266284 }
267- return true ;
285+ return Compilation :: Stop ;
268286 } ,
269287 None => ( )
270288 }
271289
272- return false ;
290+ return Compilation :: Continue ;
273291 }
274292
275293 fn no_input ( & mut self ,
@@ -288,7 +306,8 @@ impl<'a> CompilerCalls<'a> for RustcDefaultCalls {
288306 return None ;
289307 }
290308 let sess = build_session ( sopts. clone ( ) , None , descriptions. clone ( ) ) ;
291- if RustcDefaultCalls :: print_crate_info ( & sess, None , odir, ofile) {
309+ let should_stop = RustcDefaultCalls :: print_crate_info ( & sess, None , odir, ofile) ;
310+ if should_stop == Compilation :: Stop {
292311 return None ;
293312 }
294313 early_error ( "no input filename given" ) ;
@@ -328,9 +347,9 @@ impl<'a> CompilerCalls<'a> for RustcDefaultCalls {
328347 input : & Input ,
329348 odir : & Option < Path > ,
330349 ofile : & Option < Path > )
331- -> bool {
332- RustcDefaultCalls :: print_crate_info ( sess, Some ( input) , odir, ofile) ||
333- RustcDefaultCalls :: list_metadata ( sess, matches, input)
350+ -> Compilation {
351+ RustcDefaultCalls :: print_crate_info ( sess, Some ( input) , odir, ofile) . and_then (
352+ || RustcDefaultCalls :: list_metadata ( sess, matches, input) )
334353 }
335354
336355 fn build_controller ( & mut self , sess : & Session ) -> CompileController < ' a > {
@@ -339,19 +358,19 @@ impl<'a> CompilerCalls<'a> for RustcDefaultCalls {
339358 if sess. opts . parse_only ||
340359 sess. opts . show_span . is_some ( ) ||
341360 sess. opts . debugging_opts . ast_json_noexpand {
342- control. after_parse . stop = true ;
361+ control. after_parse . stop = Compilation :: Stop ;
343362 }
344363
345364 if sess. opts . no_analysis || sess. opts . debugging_opts . ast_json {
346- control. after_write_deps . stop = true ;
365+ control. after_write_deps . stop = Compilation :: Stop ;
347366 }
348367
349368 if sess. opts . no_trans {
350- control. after_analysis . stop = true ;
369+ control. after_analysis . stop = Compilation :: Stop ;
351370 }
352371
353372 if !sess. opts . output_types . iter ( ) . any ( |& i| i == config:: OutputTypeExe ) {
354- control. after_llvm . stop = true ;
373+ control. after_llvm . stop = Compilation :: Stop ;
355374 }
356375
357376 if sess. opts . debugging_opts . save_analysis {
@@ -373,7 +392,7 @@ impl RustcDefaultCalls {
373392 pub fn list_metadata ( sess : & Session ,
374393 matches : & getopts:: Matches ,
375394 input : & Input )
376- -> bool {
395+ -> Compilation {
377396 let r = matches. opt_strs ( "Z" ) ;
378397 if r. contains ( & ( "ls" . to_string ( ) ) ) {
379398 match input {
@@ -388,20 +407,20 @@ impl RustcDefaultCalls {
388407 early_error ( "cannot list metadata for stdin" ) ;
389408 }
390409 }
391- return true ;
410+ return Compilation :: Stop ;
392411 }
393412
394- return false ;
413+ return Compilation :: Continue ;
395414 }
396415
397416
398417 fn print_crate_info ( sess : & Session ,
399418 input : Option < & Input > ,
400419 odir : & Option < Path > ,
401420 ofile : & Option < Path > )
402- -> bool {
421+ -> Compilation {
403422 if sess. opts . prints . len ( ) == 0 {
404- return false
423+ return Compilation :: Continue ;
405424 }
406425
407426 let attrs = input. map ( |input| parse_crate_attrs ( sess, input) ) ;
@@ -440,7 +459,7 @@ impl RustcDefaultCalls {
440459 }
441460 }
442461 }
443- return true ;
462+ return Compilation :: Stop ;
444463 }
445464}
446465
0 commit comments