]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/queries.rs
rename from `item_mir` to `optimized_mir`
[rust.git] / src / librustc_mir / queries.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 //! An experimental pass that scources for `#[rustc_mir]` attributes,
12 //! builds the resulting MIR, and dumps it out into a file for inspection.
13 //!
14 //! The attribute formats that are currently accepted are:
15 //!
16 //! - `#[rustc_mir(graphviz="file.gv")]`
17 //! - `#[rustc_mir(pretty="file.mir")]`
18
19 use build;
20 use rustc::hir::def_id::{CrateNum, DefId, LOCAL_CRATE};
21 use rustc::mir::Mir;
22 use rustc::mir::transform::{MirSource, MIR_CONST, MIR_VALIDATED, MIR_OPTIMIZED};
23 use rustc::ty::{self, TyCtxt};
24 use rustc::ty::maps::Providers;
25 use rustc::ty::steal::Steal;
26 use rustc::hir;
27 use rustc::hir::intravisit::{self, Visitor, NestedVisitorMap};
28 use rustc::util::nodemap::DefIdSet;
29 use syntax::ast;
30 use syntax_pos::{DUMMY_SP, Span};
31 use transform;
32
33 use std::rc::Rc;
34
35 pub fn provide(providers: &mut Providers) {
36     *providers = Providers {
37         mir_keys,
38         mir_const,
39         mir_validated,
40         optimized_mir,
41         is_mir_available,
42         ..*providers
43     };
44 }
45
46 fn is_mir_available<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> bool {
47     tcx.mir_keys(def_id.krate).contains(&def_id)
48 }
49
50 /// Finds the full set of def-ids within the current crate that have
51 /// MIR associated with them.
52 fn mir_keys<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, krate: CrateNum)
53                       -> Rc<DefIdSet> {
54     assert_eq!(krate, LOCAL_CRATE);
55
56     let mut set = DefIdSet();
57
58     // All body-owners have MIR associated with them.
59     set.extend(tcx.body_owners());
60
61     // Additionally, tuple struct/variant constructors have MIR, but
62     // they don't have a BodyId, so we need to build them separately.
63     struct GatherCtors<'a, 'tcx: 'a> {
64         tcx: TyCtxt<'a, 'tcx, 'tcx>,
65         set: &'a mut DefIdSet,
66     }
67     impl<'a, 'tcx> Visitor<'tcx> for GatherCtors<'a, 'tcx> {
68         fn visit_variant_data(&mut self,
69                               v: &'tcx hir::VariantData,
70                               _: ast::Name,
71                               _: &'tcx hir::Generics,
72                               _: ast::NodeId,
73                               _: Span) {
74             if let hir::VariantData::Tuple(_, node_id) = *v {
75                 self.set.insert(self.tcx.hir.local_def_id(node_id));
76             }
77             intravisit::walk_struct_def(self, v)
78         }
79         fn nested_visit_map<'b>(&'b mut self) -> NestedVisitorMap<'b, 'tcx> {
80             NestedVisitorMap::None
81         }
82     }
83     tcx.hir.krate().visit_all_item_likes(&mut GatherCtors {
84         tcx: tcx,
85         set: &mut set,
86     }.as_deep_visitor());
87
88     Rc::new(set)
89 }
90
91 fn mir_const<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> &'tcx Steal<Mir<'tcx>> {
92     let mut mir = build::mir_build(tcx, def_id);
93     let source = MirSource::from_local_def_id(tcx, def_id);
94     transform::run_suite(tcx, source, MIR_CONST, &mut mir);
95     tcx.alloc_steal_mir(mir)
96 }
97
98 fn mir_validated<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> &'tcx Steal<Mir<'tcx>> {
99     let source = MirSource::from_local_def_id(tcx, def_id);
100     if let MirSource::Const(_) = source {
101         // Ensure that we compute the `mir_const_qualif` for constants at
102         // this point, before we steal the mir-const result. We don't
103         // directly need the result or `mir_const_qualif`, so we can just force it.
104         ty::queries::mir_const_qualif::force(tcx, DUMMY_SP, def_id);
105     }
106
107     let mut mir = tcx.mir_const(def_id).steal();
108     transform::run_suite(tcx, source, MIR_VALIDATED, &mut mir);
109     tcx.alloc_steal_mir(mir)
110 }
111
112 fn optimized_mir<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> &'tcx Mir<'tcx> {
113     // Borrowck uses `mir_validated`, so we have to force it to
114     // execute before we can steal.
115     ty::queries::borrowck::force(tcx, DUMMY_SP, def_id);
116
117     let mut mir = tcx.mir_validated(def_id).steal();
118     let source = MirSource::from_local_def_id(tcx, def_id);
119     transform::run_suite(tcx, source, MIR_OPTIMIZED, &mut mir);
120     tcx.alloc_mir(mir)
121 }