]> git.proxmox.com Git - rustc.git/blob - src/librustc_mir/transform/dump_mir.rs
New upstream version 1.14.0+dfsg1
[rustc.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::fmt;
14
15 use rustc::ty::TyCtxt;
16 use rustc::mir::*;
17 use rustc::mir::transform::{Pass, MirPass, MirPassHook, MirSource};
18 use pretty;
19
20 pub struct Marker<'a>(pub &'a str);
21
22 impl<'b, 'tcx> MirPass<'tcx> for Marker<'b> {
23 fn run_pass<'a>(&mut self, _tcx: TyCtxt<'a, 'tcx, 'tcx>,
24 _src: MirSource, _mir: &mut Mir<'tcx>)
25 {}
26 }
27
28 impl<'b> Pass for Marker<'b> {
29 fn name(&self) -> ::std::borrow::Cow<'static, str> { String::from(self.0).into() }
30 }
31
32 pub struct Disambiguator<'a> {
33 pass: &'a Pass,
34 is_after: bool
35 }
36
37 impl<'a> fmt::Display for Disambiguator<'a> {
38 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
39 let title = if self.is_after { "after" } else { "before" };
40 if let Some(fmt) = self.pass.disambiguator() {
41 write!(formatter, "{}-{}", fmt, title)
42 } else {
43 write!(formatter, "{}", title)
44 }
45 }
46 }
47
48 pub struct DumpMir;
49
50 impl<'tcx> MirPassHook<'tcx> for DumpMir {
51 fn on_mir_pass<'a>(
52 &mut self,
53 tcx: TyCtxt<'a, 'tcx, 'tcx>,
54 src: MirSource,
55 mir: &Mir<'tcx>,
56 pass: &Pass,
57 is_after: bool)
58 {
59 pretty::dump_mir(
60 tcx,
61 &*pass.name(),
62 &Disambiguator {
63 pass: pass,
64 is_after: is_after
65 },
66 src,
67 mir,
68 None
69 );
70 }
71 }
72
73 impl<'b> Pass for DumpMir {}