]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/transform/dump_mir.rs
rustc: split off BodyOwnerKind from MirSource.
[rust.git] / src / librustc_mir / transform / dump_mir.rs
1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! This pass just dumps MIR at a specified point.
12
13 use std::borrow::Cow;
14 use std::fmt;
15 use std::fs::File;
16 use std::io;
17
18 use rustc::mir::Mir;
19 use rustc::session::config::{OutputFilenames, OutputType};
20 use rustc::ty::TyCtxt;
21 use transform::{MirPass, MirSource};
22 use util as mir_util;
23
24 pub struct Marker(pub &'static str);
25
26 impl MirPass for Marker {
27     fn name<'a>(&'a self) -> Cow<'a, str> {
28         Cow::Borrowed(self.0)
29     }
30
31     fn run_pass<'a, 'tcx>(&self,
32                           _tcx: TyCtxt<'a, 'tcx, 'tcx>,
33                           _source: MirSource,
34                           _mir: &mut Mir<'tcx>)
35     {
36     }
37 }
38
39 pub struct Disambiguator {
40     is_after: bool
41 }
42
43 impl fmt::Display for Disambiguator {
44     fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
45         let title = if self.is_after { "after" } else { "before" };
46         write!(formatter, "{}", title)
47     }
48 }
49
50
51 pub fn on_mir_pass<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
52                              pass_num: &fmt::Display,
53                              pass_name: &str,
54                              source: MirSource,
55                              mir: &Mir<'tcx>,
56                              is_after: bool) {
57     if mir_util::dump_enabled(tcx, pass_name, source) {
58         mir_util::dump_mir(tcx,
59                            Some(pass_num),
60                            pass_name,
61                            &Disambiguator { is_after },
62                            source,
63                            mir,
64                            |_, _| Ok(()) );
65     }
66 }
67
68 pub fn emit_mir<'a, 'tcx>(
69     tcx: TyCtxt<'a, 'tcx, 'tcx>,
70     outputs: &OutputFilenames)
71     -> io::Result<()>
72 {
73     let path = outputs.path(OutputType::Mir);
74     let mut f = File::create(&path)?;
75     mir_util::write_mir_pretty(tcx, None, &mut f)?;
76     Ok(())
77 }