Add ScopeAuxiliaryVec, return MIR+aux via tuple

It's nice to be able to index with a scope-id,
but coherence rules prevent us from implementing
`Index<ScopeId>` for `Vec<ScopeAuxiliary>`, and I'd
prefer that `ScopeAuxiliary` remain in librustc_mir,
just for compilation time reasons.
This commit is contained in:
Niko Matsakis 2016-03-23 05:01:30 -04:00
parent 70d0123082
commit c36707a284
5 changed files with 38 additions and 23 deletions

View File

@ -633,7 +633,7 @@ impl<'a,'tcx> Builder<'a,'tcx> {
span: span,
});
let index = index as u32;
let extent = self.scope_auxiliary[var_scope_id.index()].extent;
let extent = self.scope_auxiliary[var_scope_id].extent;
self.schedule_drop(span, extent, &Lvalue::Var(index), var_ty);
self.var_indices.insert(var_id, index);

View File

@ -14,7 +14,7 @@ use rustc::middle::ty::{FnOutput, Ty};
use rustc::mir::repr::*;
use rustc_data_structures::fnv::FnvHashMap;
use rustc_front::hir;
use std::ops::{Index, IndexMut};
use syntax::ast;
use syntax::codemap::Span;
@ -33,7 +33,7 @@ pub struct Builder<'a, 'tcx: 'a> {
// but these are liable to get out of date once optimization
// begins. They are also hopefully temporary, and will be
// no longer needed when we adopt graph-based regions.
scope_auxiliary: Vec<ScopeAuxiliary>,
scope_auxiliary: ScopeAuxiliaryVec,
// the current set of loops; see the `scope` module for more
// details
@ -85,9 +85,24 @@ pub struct Location {
pub statement_index: usize,
}
pub struct MirAndScopeAuxiliary<'tcx> {
pub mir: Mir<'tcx>,
pub scope_auxiliary: Vec<ScopeAuxiliary>,
pub struct ScopeAuxiliaryVec {
pub vec: Vec<ScopeAuxiliary>
}
impl Index<ScopeId> for ScopeAuxiliaryVec {
type Output = ScopeAuxiliary;
#[inline]
fn index(&self, index: ScopeId) -> &ScopeAuxiliary {
&self.vec[index.index()]
}
}
impl IndexMut<ScopeId> for ScopeAuxiliaryVec {
#[inline]
fn index_mut(&mut self, index: ScopeId) -> &mut ScopeAuxiliary {
&mut self.vec[index.index()]
}
}
///////////////////////////////////////////////////////////////////////////
@ -143,7 +158,7 @@ pub fn construct<'a,'tcx>(hir: Cx<'a,'tcx>,
argument_extent: CodeExtent,
return_ty: FnOutput<'tcx>,
ast_block: &'tcx hir::Block)
-> MirAndScopeAuxiliary<'tcx> {
-> (Mir<'tcx>, ScopeAuxiliaryVec) {
let cfg = CFG { basic_blocks: vec![] };
let mut builder = Builder {
@ -152,7 +167,7 @@ pub fn construct<'a,'tcx>(hir: Cx<'a,'tcx>,
fn_span: span,
scopes: vec![],
scope_datas: vec![],
scope_auxiliary: vec![],
scope_auxiliary: ScopeAuxiliaryVec { vec: vec![] },
loop_scopes: vec![],
temp_decls: vec![],
var_decls: vec![],
@ -188,8 +203,8 @@ pub fn construct<'a,'tcx>(hir: Cx<'a,'tcx>,
true
}));
MirAndScopeAuxiliary {
mir: Mir {
(
Mir {
basic_blocks: builder.cfg.basic_blocks,
scopes: builder.scope_datas,
var_decls: builder.var_decls,
@ -198,8 +213,8 @@ pub fn construct<'a,'tcx>(hir: Cx<'a,'tcx>,
return_ty: return_ty,
span: span
},
scope_auxiliary: builder.scope_auxiliary,
}
builder.scope_auxiliary,
)
}
impl<'a,'tcx> Builder<'a,'tcx> {

View File

@ -257,7 +257,7 @@ impl<'a,'tcx> Builder<'a,'tcx> {
free: None,
cached_block: None,
});
self.scope_auxiliary.push(ScopeAuxiliary {
self.scope_auxiliary.vec.push(ScopeAuxiliary {
extent: extent,
dom: self.cfg.current_location(entry),
postdoms: vec![]
@ -279,7 +279,7 @@ impl<'a,'tcx> Builder<'a,'tcx> {
let scope = self.scopes.pop().unwrap();
assert_eq!(scope.extent, extent);
unpack!(block = build_scope_drops(&mut self.cfg, &scope, &self.scopes, block));
self.scope_auxiliary[scope.id.index()]
self.scope_auxiliary[scope.id]
.postdoms
.push(self.cfg.current_location(block));
block.unit()
@ -313,7 +313,7 @@ impl<'a,'tcx> Builder<'a,'tcx> {
self.cfg.terminate(block, scope.id, span, free);
block = next;
}
self.scope_auxiliary[scope.id.index()]
self.scope_auxiliary[scope.id]
.postdoms
.push(self.cfg.current_location(block));
}

View File

@ -19,7 +19,7 @@
extern crate syntax;
extern crate rustc_front;
use build::{self, MirAndScopeAuxiliary};
use build;
use rustc::dep_graph::DepNode;
use rustc::mir::repr::Mir;
use pretty;
@ -183,7 +183,7 @@ fn build_mir<'a,'tcx:'a>(cx: Cx<'a,'tcx>,
let parameter_scope =
cx.tcx().region_maps.lookup_code_extent(
CodeExtentData::ParameterScope { fn_id: fn_id, body_id: body.id });
let MirAndScopeAuxiliary { mut mir, scope_auxiliary } =
let (mut mir, scope_auxiliary) =
build::construct(cx,
span,
implicit_arg_tys,

View File

@ -8,7 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use build::{Location, ScopeAuxiliary};
use build::{Location, ScopeAuxiliaryVec};
use rustc::mir::repr::*;
use rustc::middle::ty::{self, TyCtxt};
use rustc_data_structures::fnv::FnvHashMap;
@ -39,7 +39,7 @@ pub fn dump_mir<'a, 'tcx>(tcx: &TyCtxt<'tcx>,
disambiguator: &Display,
node_id: NodeId,
mir: &Mir<'tcx>,
auxiliary: Option<&Vec<ScopeAuxiliary>>) {
auxiliary: Option<&ScopeAuxiliaryVec>) {
let filters = match tcx.sess.opts.debugging_opts.dump_mir {
None => return,
Some(ref filters) => filters,
@ -91,12 +91,12 @@ pub fn write_mir_fn<'tcx>(tcx: &TyCtxt<'tcx>,
node_id: NodeId,
mir: &Mir<'tcx>,
w: &mut Write,
auxiliary: Option<&Vec<ScopeAuxiliary>>)
auxiliary: Option<&ScopeAuxiliaryVec>)
-> io::Result<()> {
// compute scope/entry exit annotations
let mut annotations = FnvHashMap();
if let Some(auxiliary) = auxiliary {
for (index, auxiliary) in auxiliary.iter().enumerate() {
for (index, auxiliary) in auxiliary.vec.iter().enumerate() {
let scope_id = ScopeId::new(index);
annotations.entry(auxiliary.dom)
@ -183,7 +183,7 @@ fn comment(tcx: &TyCtxt,
fn write_scope_tree(tcx: &TyCtxt,
mir: &Mir,
auxiliary: Option<&Vec<ScopeAuxiliary>>,
auxiliary: Option<&ScopeAuxiliaryVec>,
scope_tree: &FnvHashMap<Option<ScopeId>, Vec<ScopeId>>,
w: &mut Write,
parent: Option<ScopeId>,
@ -201,7 +201,7 @@ fn write_scope_tree(tcx: &TyCtxt,
}
if let Some(auxiliary) = auxiliary {
let extent = auxiliary[child.index()].extent;
let extent = auxiliary[child].extent;
let data = tcx.region_maps.code_extent_data(extent);
writeln!(w, "{0:1$}Extent: {2:?}", "", indent, data)?;
}