Skip to content

Commit

Permalink
rustc: split off BodyOwnerKind from MirSource.
Browse files Browse the repository at this point in the history
  • Loading branch information
eddyb committed Nov 14, 2017
1 parent c79e8f4 commit d6aa56f
Show file tree
Hide file tree
Showing 35 changed files with 238 additions and 275 deletions.
22 changes: 22 additions & 0 deletions src/librustc/hir/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,28 @@ impl<'hir> Map<'hir> {
})
}

pub fn body_owner_kind(&self, id: NodeId) -> BodyOwnerKind {
// Handle constants in enum discriminants, types, and repeat expressions.
let def_id = self.local_def_id(id);
let def_key = self.def_key(def_id);
if def_key.disambiguated_data.data == DefPathData::Initializer {
return BodyOwnerKind::Const;
}

match self.get(id) {
NodeItem(&Item { node: ItemConst(..), .. }) |
NodeTraitItem(&TraitItem { node: TraitItemKind::Const(..), .. }) |
NodeImplItem(&ImplItem { node: ImplItemKind::Const(..), .. }) => {
BodyOwnerKind::Const
}
NodeItem(&Item { node: ItemStatic(_, m, _), .. }) => {
BodyOwnerKind::Static(m)
}
// Default to function if it's not a constant or static.
_ => BodyOwnerKind::Fn
}
}

pub fn ty_param_owner(&self, id: NodeId) -> NodeId {
match self.get(id) {
NodeItem(&Item { node: ItemTrait(..), .. }) => id,
Expand Down
12 changes: 12 additions & 0 deletions src/librustc/hir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1028,6 +1028,18 @@ impl Body {
}
}

#[derive(Copy, Clone, Debug)]
pub enum BodyOwnerKind {
/// Functions and methods.
Fn,

/// Constants and associated constants.
Const,

/// Initializer of a `static` item.
Static(Mutability),
}

/// An expression
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash)]
pub struct Expr {
Expand Down
3 changes: 1 addition & 2 deletions src/librustc/middle/region.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ use hir;
use hir::def_id::DefId;
use hir::intravisit::{self, Visitor, NestedVisitorMap};
use hir::{Block, Arm, Pat, PatKind, Stmt, Expr, Local};
use mir::transform::MirSource;
use rustc_data_structures::indexed_vec::Idx;
use rustc_data_structures::stable_hasher::{HashStable, StableHasher,
StableHasherResult};
Expand Down Expand Up @@ -1298,7 +1297,7 @@ impl<'a, 'tcx> Visitor<'tcx> for RegionResolutionVisitor<'a, 'tcx> {

// The body of the every fn is a root scope.
self.cx.parent = self.cx.var_parent;
if let MirSource::Fn(_) = MirSource::from_node(self.tcx, owner_id) {
if let hir::BodyOwnerKind::Fn = self.tcx.hir.body_owner_kind(owner_id) {
self.visit_expr(&body.value);
} else {
// Only functions have an outer terminating (drop) scope, while
Expand Down
1 change: 0 additions & 1 deletion src/librustc/mir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ use syntax_pos::Span;
mod cache;
pub mod tcx;
pub mod visit;
pub mod transform;
pub mod traversal;

/// Types for locals
Expand Down
74 changes: 0 additions & 74 deletions src/librustc/mir/transform.rs

This file was deleted.

15 changes: 6 additions & 9 deletions src/librustc_mir/borrow_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ use rustc::ty::maps::Providers;
use rustc::mir::{AssertMessage, BasicBlock, BorrowKind, Location, Lvalue, Local};
use rustc::mir::{Mir, Mutability, Operand, Projection, ProjectionElem, Rvalue};
use rustc::mir::{Statement, StatementKind, Terminator, TerminatorKind};
use rustc::mir::transform::MirSource;
use transform::nll;

use rustc_data_structures::indexed_set::{self, IdxSetBuf};
Expand Down Expand Up @@ -50,8 +49,7 @@ pub fn provide(providers: &mut Providers) {

fn mir_borrowck<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) {
let input_mir = tcx.mir_validated(def_id);
let src = MirSource::from_local_def_id(tcx, def_id);
debug!("run query mir_borrowck: {}", tcx.node_path_str(src.item_id()));
debug!("run query mir_borrowck: {}", tcx.item_path_str(def_id));

if {
!tcx.has_attr(def_id, "rustc_mir_borrowck") &&
Expand All @@ -63,21 +61,20 @@ fn mir_borrowck<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) {

tcx.infer_ctxt().enter(|infcx| {
let input_mir: &Mir = &input_mir.borrow();
do_mir_borrowck(&infcx, input_mir, def_id, src);
do_mir_borrowck(&infcx, input_mir, def_id);
});
debug!("mir_borrowck done");
}

fn do_mir_borrowck<'a, 'gcx, 'tcx>(infcx: &InferCtxt<'a, 'gcx, 'tcx>,
input_mir: &Mir<'gcx>,
def_id: DefId,
src: MirSource)
def_id: DefId)
{
let tcx = infcx.tcx;
let attributes = tcx.get_attrs(def_id);
let param_env = tcx.param_env(def_id);

let id = src.item_id();
let id = tcx.hir.as_local_node_id(def_id)
.expect("do_mir_borrowck: non-local DefId");

let move_data: MoveData<'tcx> = match MoveData::gather_moves(input_mir, tcx, param_env) {
Ok(move_data) => move_data,
Expand Down Expand Up @@ -117,7 +114,7 @@ fn do_mir_borrowck<'a, 'gcx, 'tcx>(infcx: &InferCtxt<'a, 'gcx, 'tcx>,
let opt_regioncx = if !tcx.sess.opts.debugging_opts.nll {
None
} else {
Some(nll::compute_regions(infcx, src, mir))
Some(nll::compute_regions(infcx, def_id, mir))
};

let mdpe = MoveDataParamEnv { move_data: move_data, param_env: param_env };
Expand Down
17 changes: 9 additions & 8 deletions src/librustc_mir/build/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use rustc::hir;
use rustc::hir::def_id::DefId;
use rustc::middle::region;
use rustc::mir::*;
use rustc::mir::transform::MirSource;
use rustc::mir::visit::{MutVisitor, TyContext};
use rustc::ty::{self, Ty, TyCtxt};
use rustc::ty::subst::Substs;
Expand All @@ -30,6 +29,7 @@ use syntax::abi::Abi;
use syntax::ast;
use syntax::symbol::keywords;
use syntax_pos::Span;
use transform::MirSource;
use util as mir_util;

/// Construct the MIR for a given def-id.
Expand Down Expand Up @@ -83,12 +83,11 @@ pub fn mir_build<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> Mir<'t
_ => unsupported(),
};

let src = MirSource::from_node(tcx, id);
tcx.infer_ctxt().enter(|infcx| {
let cx = Cx::new(&infcx, src);
let cx = Cx::new(&infcx, id);
let mut mir = if cx.tables().tainted_by_errors {
build::construct_error(cx, body_id)
} else if let MirSource::Fn(id) = src {
} else if let hir::BodyOwnerKind::Fn = cx.body_owner_kind {
// fetch the fully liberated fn signature (that is, all bound
// types/lifetimes replaced)
let fn_hir_id = tcx.hir.node_to_hir_id(id);
Expand Down Expand Up @@ -150,7 +149,8 @@ pub fn mir_build<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> Mir<'t
mem::transmute::<Mir, Mir<'tcx>>(mir)
};

mir_util::dump_mir(tcx, None, "mir_map", &0, src, &mir, |_, _| Ok(()) );
mir_util::dump_mir(tcx, None, "mir_map", &0,
MirSource::item(def_id), &mir, |_, _| Ok(()) );

mir
})
Expand Down Expand Up @@ -214,8 +214,7 @@ fn create_constructor_shim<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
let span = tcx.hir.span(ctor_id);
if let hir::VariantData::Tuple(ref fields, ctor_id) = *v {
tcx.infer_ctxt().enter(|infcx| {
let (mut mir, src) =
shim::build_adt_ctor(&infcx, ctor_id, fields, span);
let mut mir = shim::build_adt_ctor(&infcx, ctor_id, fields, span);

// Convert the Mir to global types.
let tcx = infcx.tcx.global_tcx();
Expand All @@ -228,7 +227,9 @@ fn create_constructor_shim<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
mem::transmute::<Mir, Mir<'tcx>>(mir)
};

mir_util::dump_mir(tcx, None, "mir_map", &0, src, &mir, |_, _| Ok(()) );
mir_util::dump_mir(tcx, None, "mir_map", &0,
MirSource::item(tcx.hir.local_def_id(ctor_id)),
&mir, |_, _| Ok(()) );

mir
})
Expand Down
12 changes: 5 additions & 7 deletions src/librustc_mir/build/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ use build::{BlockAnd, BlockAndExtension, Builder, CFG};
use hair::LintLevel;
use rustc::middle::region;
use rustc::ty::{Ty, TyCtxt};
use rustc::hir;
use rustc::hir::def_id::LOCAL_CRATE;
use rustc::mir::*;
use rustc::mir::transform::MirSource;
use syntax_pos::{Span};
use rustc_data_structures::indexed_vec::Idx;
use rustc_data_structures::fx::FxHashMap;
Expand Down Expand Up @@ -596,15 +596,13 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
/// When building statics/constants, returns `None` since
/// intermediate values do not have to be dropped in that case.
pub fn local_scope(&self) -> Option<region::Scope> {
match self.hir.src {
MirSource::Const(_) |
MirSource::Static(..) =>
match self.hir.body_owner_kind {
hir::BodyOwnerKind::Const |
hir::BodyOwnerKind::Static(_) =>
// No need to free storage in this context.
None,
MirSource::Fn(_) =>
hir::BodyOwnerKind::Fn =>
Some(self.topmost_scope()),
MirSource::Promoted(..) =>
bug!(),
}
}

Expand Down
28 changes: 13 additions & 15 deletions src/librustc_mir/hair/cx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
//!
use hair::*;
use rustc::mir::transform::MirSource;

use rustc::middle::const_val::{ConstEvalErr, ConstVal};
use rustc_const_eval::ConstContext;
Expand Down Expand Up @@ -51,30 +50,29 @@ pub struct Cx<'a, 'gcx: 'a + 'tcx, 'tcx: 'a> {
/// `const`, or the body of a `const fn`.
constness: hir::Constness,

/// What are we compiling?
pub src: MirSource,
/// What kind of body is being compiled.
pub body_owner_kind: hir::BodyOwnerKind,

/// True if this constant/function needs overflow checks.
check_overflow: bool,
}

impl<'a, 'gcx, 'tcx> Cx<'a, 'gcx, 'tcx> {
pub fn new(infcx: &'a InferCtxt<'a, 'gcx, 'tcx>,
src: MirSource) -> Cx<'a, 'gcx, 'tcx> {
let constness = match src {
MirSource::Const(_) |
MirSource::Static(..) => hir::Constness::Const,
MirSource::Fn(id) => {
let fn_like = FnLikeNode::from_node(infcx.tcx.hir.get(id));
src_id: ast::NodeId) -> Cx<'a, 'gcx, 'tcx> {
let tcx = infcx.tcx;
let src_def_id = tcx.hir.local_def_id(src_id);
let body_owner_kind = tcx.hir.body_owner_kind(src_id);

let constness = match body_owner_kind {
hir::BodyOwnerKind::Const |
hir::BodyOwnerKind::Static(_) => hir::Constness::Const,
hir::BodyOwnerKind::Fn => {
let fn_like = FnLikeNode::from_node(infcx.tcx.hir.get(src_id));
fn_like.map_or(hir::Constness::NotConst, |f| f.constness())
}
MirSource::Promoted(..) => bug!(),
};

let tcx = infcx.tcx;
let src_id = src.item_id();
let src_def_id = tcx.hir.local_def_id(src_id);

let attrs = tcx.hir.attrs(src_id);

// Some functions always have overflow checks enabled,
Expand All @@ -99,7 +97,7 @@ impl<'a, 'gcx, 'tcx> Cx<'a, 'gcx, 'tcx> {
region_scope_tree: tcx.region_scope_tree(src_def_id),
tables: tcx.typeck_tables_of(src_def_id),
constness,
src,
body_owner_kind,
check_overflow,
}
}
Expand Down
8 changes: 3 additions & 5 deletions src/librustc_mir/shim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use rustc::hir::def_id::DefId;
use rustc::infer;
use rustc::middle::const_val::ConstVal;
use rustc::mir::*;
use rustc::mir::transform::MirSource;
use rustc::ty::{self, Ty, TyCtxt};
use rustc::ty::subst::{Kind, Subst, Substs};
use rustc::ty::maps::Providers;
Expand Down Expand Up @@ -826,7 +825,7 @@ pub fn build_adt_ctor<'a, 'gcx, 'tcx>(infcx: &infer::InferCtxt<'a, 'gcx, 'tcx>,
ctor_id: ast::NodeId,
fields: &[hir::StructField],
span: Span)
-> (Mir<'tcx>, MirSource)
-> Mir<'tcx>
{
let tcx = infcx.tcx;
let def_id = tcx.hir.local_def_id(ctor_id);
Expand Down Expand Up @@ -875,7 +874,7 @@ pub fn build_adt_ctor<'a, 'gcx, 'tcx>(infcx: &infer::InferCtxt<'a, 'gcx, 'tcx>,
is_cleanup: false
};

let mir = Mir::new(
Mir::new(
IndexVec::from_elem_n(start_block, 1),
IndexVec::from_elem_n(
VisibilityScopeData { span: span, parent_scope: None }, 1
Expand All @@ -888,6 +887,5 @@ pub fn build_adt_ctor<'a, 'gcx, 'tcx>(infcx: &infer::InferCtxt<'a, 'gcx, 'tcx>,
sig.inputs().len(),
vec![],
span
);
(mir, MirSource::Fn(ctor_id))
)
}
3 changes: 1 addition & 2 deletions src/librustc_mir/transform/add_call_guards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@

use rustc::ty::TyCtxt;
use rustc::mir::*;
use rustc::mir::transform::MirSource;
use rustc_data_structures::indexed_vec::{Idx, IndexVec};
use transform::MirPass;
use transform::{MirPass, MirSource};

#[derive(PartialEq)]
pub enum AddCallGuards {
Expand Down
Loading

0 comments on commit d6aa56f

Please sign in to comment.