1
- // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
1
+ // Copyright 2014-2015 The Rust Project Developers. See the COPYRIGHT
2
2
// file at the top-level directory of this distribution and at
3
3
// http://rust-lang.org/COPYRIGHT.
4
4
//
@@ -108,8 +108,9 @@ pub fn run(args: Vec<String>) -> int {
108
108
pub fn run_compiler < ' a > ( args : & [ String ] ,
109
109
callbacks : & mut CompilerCalls < ' a > ) {
110
110
macro_rules! do_or_return { ( $expr: expr) => {
111
- if $expr {
112
- return ;
111
+ match $expr {
112
+ Compilation :: Stop => return ,
113
+ Compilation :: Continue => { }
113
114
}
114
115
} }
115
116
@@ -144,7 +145,7 @@ pub fn run_compiler<'a>(args: &[String],
144
145
// It is somewhat unfortunate that this is hardwired in - this is forced by
145
146
// the fact that pretty_print_input requires the session by value.
146
147
let pretty = callbacks. parse_pretty ( & sess, & matches) ;
147
- match pretty. into_iter ( ) . next ( ) {
148
+ match pretty {
148
149
Some ( ( ppm, opt_uii) ) => {
149
150
pretty:: pretty_print_input ( sess, cfg, & input, ppm, opt_uii, ofile) ;
150
151
return ;
@@ -180,26 +181,43 @@ fn make_input(free_matches: &[String]) -> Option<(Input, Option<Path>)> {
180
181
}
181
182
}
182
183
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
+
183
200
// A trait for customising the compilation process. Offers a number of hooks for
184
201
// executing custom code or customising input.
185
202
pub trait CompilerCalls < ' a > {
186
203
// Hook for a callback early in the process of handling arguments. This will
187
204
// 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 ;
191
210
192
211
// Hook for a callback late in the process of handling arguments. This will
193
212
// 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.
196
214
fn late_callback ( & mut self ,
197
215
& getopts:: Matches ,
198
216
& Session ,
199
217
& Input ,
200
218
& Option < Path > ,
201
219
& Option < Path > )
202
- -> bool ;
220
+ -> Compilation ;
203
221
204
222
// Called after we extract the input from the arguments. Gives the implementer
205
223
// an opportunity to change the inputs or to add some custom input handling.
@@ -253,7 +271,7 @@ impl<'a> CompilerCalls<'a> for RustcDefaultCalls {
253
271
fn early_callback ( & mut self ,
254
272
matches : & getopts:: Matches ,
255
273
descriptions : & diagnostics:: registry:: Registry )
256
- -> bool {
274
+ -> Compilation {
257
275
match matches. opt_str ( "explain" ) {
258
276
Some ( ref code) => {
259
277
match descriptions. find_description ( & code[ ] ) {
@@ -264,12 +282,12 @@ impl<'a> CompilerCalls<'a> for RustcDefaultCalls {
264
282
early_error ( & format ! ( "no extended information for {}" , code) [ ] ) ;
265
283
}
266
284
}
267
- return true ;
285
+ return Compilation :: Stop ;
268
286
} ,
269
287
None => ( )
270
288
}
271
289
272
- return false ;
290
+ return Compilation :: Continue ;
273
291
}
274
292
275
293
fn no_input ( & mut self ,
@@ -288,7 +306,8 @@ impl<'a> CompilerCalls<'a> for RustcDefaultCalls {
288
306
return None ;
289
307
}
290
308
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 {
292
311
return None ;
293
312
}
294
313
early_error ( "no input filename given" ) ;
@@ -328,9 +347,9 @@ impl<'a> CompilerCalls<'a> for RustcDefaultCalls {
328
347
input : & Input ,
329
348
odir : & Option < Path > ,
330
349
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) )
334
353
}
335
354
336
355
fn build_controller ( & mut self , sess : & Session ) -> CompileController < ' a > {
@@ -339,19 +358,19 @@ impl<'a> CompilerCalls<'a> for RustcDefaultCalls {
339
358
if sess. opts . parse_only ||
340
359
sess. opts . show_span . is_some ( ) ||
341
360
sess. opts . debugging_opts . ast_json_noexpand {
342
- control. after_parse . stop = true ;
361
+ control. after_parse . stop = Compilation :: Stop ;
343
362
}
344
363
345
364
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 ;
347
366
}
348
367
349
368
if sess. opts . no_trans {
350
- control. after_analysis . stop = true ;
369
+ control. after_analysis . stop = Compilation :: Stop ;
351
370
}
352
371
353
372
if !sess. opts . output_types . iter ( ) . any ( |& i| i == config:: OutputTypeExe ) {
354
- control. after_llvm . stop = true ;
373
+ control. after_llvm . stop = Compilation :: Stop ;
355
374
}
356
375
357
376
if sess. opts . debugging_opts . save_analysis {
@@ -373,7 +392,7 @@ impl RustcDefaultCalls {
373
392
pub fn list_metadata ( sess : & Session ,
374
393
matches : & getopts:: Matches ,
375
394
input : & Input )
376
- -> bool {
395
+ -> Compilation {
377
396
let r = matches. opt_strs ( "Z" ) ;
378
397
if r. contains ( & ( "ls" . to_string ( ) ) ) {
379
398
match input {
@@ -388,20 +407,20 @@ impl RustcDefaultCalls {
388
407
early_error ( "cannot list metadata for stdin" ) ;
389
408
}
390
409
}
391
- return true ;
410
+ return Compilation :: Stop ;
392
411
}
393
412
394
- return false ;
413
+ return Compilation :: Continue ;
395
414
}
396
415
397
416
398
417
fn print_crate_info ( sess : & Session ,
399
418
input : Option < & Input > ,
400
419
odir : & Option < Path > ,
401
420
ofile : & Option < Path > )
402
- -> bool {
421
+ -> Compilation {
403
422
if sess. opts . prints . len ( ) == 0 {
404
- return false
423
+ return Compilation :: Continue ;
405
424
}
406
425
407
426
let attrs = input. map ( |input| parse_crate_attrs ( sess, input) ) ;
@@ -440,7 +459,7 @@ impl RustcDefaultCalls {
440
459
}
441
460
}
442
461
}
443
- return true ;
462
+ return Compilation :: Stop ;
444
463
}
445
464
}
446
465
0 commit comments