Skip to content

Commit

Permalink
mir: distinguish between variable visibility scopes and SEME scopes.
Browse files Browse the repository at this point in the history
  • Loading branch information
eddyb committed Jun 7, 2016
1 parent f352550 commit 719a591
Show file tree
Hide file tree
Showing 19 changed files with 259 additions and 257 deletions.
37 changes: 20 additions & 17 deletions src/librustc/mir/repr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ pub struct Mir<'tcx> {
/// that indexes into this vector.
pub basic_blocks: Vec<BasicBlockData<'tcx>>,

/// List of lexical scopes; these are referenced by statements and
/// used (eventually) for debuginfo. Indexed by a `ScopeId`.
pub scopes: Vec<ScopeData>,
/// List of visibility (lexical) scopes; these are referenced by statements
/// and used (eventually) for debuginfo. Indexed by a `VisibilityScope`.
pub visibility_scopes: Vec<VisibilityScopeData>,

/// Rvalues promoted from this function, such as borrows of constants.
/// Each of them is the Mir of a constant with the fn's type parameters
Expand Down Expand Up @@ -173,7 +173,7 @@ pub struct VarDecl<'tcx> {
pub ty: Ty<'tcx>,

/// scope in which variable was declared
pub scope: ScopeId,
pub scope: VisibilityScope,

/// span where variable was declared
pub span: Span,
Expand Down Expand Up @@ -276,7 +276,7 @@ pub struct BasicBlockData<'tcx> {
#[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
pub struct Terminator<'tcx> {
pub span: Span,
pub scope: ScopeId,
pub scope: VisibilityScope,
pub kind: TerminatorKind<'tcx>
}

Expand Down Expand Up @@ -588,7 +588,7 @@ pub enum AssertMessage<'tcx> {
#[derive(Clone, RustcEncodable, RustcDecodable)]
pub struct Statement<'tcx> {
pub span: Span,
pub scope: ScopeId,
pub scope: VisibilityScope,
pub kind: StatementKind<'tcx>,
}

Expand Down Expand Up @@ -754,29 +754,32 @@ impl<'tcx> Debug for Lvalue<'tcx> {
///////////////////////////////////////////////////////////////////////////
// Scopes

impl Index<ScopeId> for Vec<ScopeData> {
type Output = ScopeData;
impl Index<VisibilityScope> for Vec<VisibilityScopeData> {
type Output = VisibilityScopeData;

#[inline]
fn index(&self, index: ScopeId) -> &ScopeData {
fn index(&self, index: VisibilityScope) -> &VisibilityScopeData {
&self[index.index()]
}
}

impl IndexMut<ScopeId> for Vec<ScopeData> {
impl IndexMut<VisibilityScope> for Vec<VisibilityScopeData> {
#[inline]
fn index_mut(&mut self, index: ScopeId) -> &mut ScopeData {
fn index_mut(&mut self, index: VisibilityScope) -> &mut VisibilityScopeData {
&mut self[index.index()]
}
}

#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq, RustcEncodable, RustcDecodable)]
pub struct ScopeId(u32);
pub struct VisibilityScope(u32);

impl ScopeId {
pub fn new(index: usize) -> ScopeId {
/// The visibility scope all arguments go into.
pub const ARGUMENT_VISIBILITY_SCOPE: VisibilityScope = VisibilityScope(0);

impl VisibilityScope {
pub fn new(index: usize) -> VisibilityScope {
assert!(index < (u32::MAX as usize));
ScopeId(index as u32)
VisibilityScope(index as u32)
}

pub fn index(self) -> usize {
Expand All @@ -785,9 +788,9 @@ impl ScopeId {
}

#[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
pub struct ScopeData {
pub struct VisibilityScopeData {
pub span: Span,
pub parent_scope: Option<ScopeId>,
pub parent_scope: Option<VisibilityScope>,
}

///////////////////////////////////////////////////////////////////////////
Expand Down
36 changes: 18 additions & 18 deletions src/librustc/mir/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ macro_rules! make_mir_visitor {
self.super_basic_block_data(block, data);
}

fn visit_scope_data(&mut self,
scope_data: & $($mutability)* ScopeData) {
self.super_scope_data(scope_data);
fn visit_visibility_scope_data(&mut self,
scope_data: & $($mutability)* VisibilityScopeData) {
self.super_visibility_scope_data(scope_data);
}

fn visit_statement(&mut self,
Expand Down Expand Up @@ -236,9 +236,9 @@ macro_rules! make_mir_visitor {
self.super_arg_decl(arg_decl);
}

fn visit_scope_id(&mut self,
scope_id: & $($mutability)* ScopeId) {
self.super_scope_id(scope_id);
fn visit_visibility_scope(&mut self,
scope: & $($mutability)* VisibilityScope) {
self.super_visibility_scope(scope);
}

// The `super_xxx` methods comprise the default behavior and are
Expand All @@ -248,7 +248,7 @@ macro_rules! make_mir_visitor {
mir: & $($mutability)* Mir<'tcx>) {
let Mir {
ref $($mutability)* basic_blocks,
ref $($mutability)* scopes,
ref $($mutability)* visibility_scopes,
promoted: _, // Visited by passes separately.
ref $($mutability)* return_ty,
ref $($mutability)* var_decls,
Expand All @@ -263,8 +263,8 @@ macro_rules! make_mir_visitor {
self.visit_basic_block_data(block, data);
}

for scope in scopes {
self.visit_scope_data(scope);
for scope in visibility_scopes {
self.visit_visibility_scope_data(scope);
}

self.visit_fn_output(return_ty);
Expand Down Expand Up @@ -302,16 +302,16 @@ macro_rules! make_mir_visitor {
}
}

fn super_scope_data(&mut self,
scope_data: & $($mutability)* ScopeData) {
let ScopeData {
fn super_visibility_scope_data(&mut self,
scope_data: & $($mutability)* VisibilityScopeData) {
let VisibilityScopeData {
ref $($mutability)* span,
ref $($mutability)* parent_scope,
} = *scope_data;

self.visit_span(span);
if let Some(ref $($mutability)* parent_scope) = *parent_scope {
self.visit_scope_id(parent_scope);
self.visit_visibility_scope(parent_scope);
}
}

Expand All @@ -325,7 +325,7 @@ macro_rules! make_mir_visitor {
} = *statement;

self.visit_span(span);
self.visit_scope_id(scope);
self.visit_visibility_scope(scope);
match *kind {
StatementKind::Assign(ref $($mutability)* lvalue,
ref $($mutability)* rvalue) => {
Expand All @@ -352,7 +352,7 @@ macro_rules! make_mir_visitor {
} = *terminator;

self.visit_span(span);
self.visit_scope_id(scope);
self.visit_visibility_scope(scope);
self.visit_terminator_kind(block, kind);
}

Expand Down Expand Up @@ -627,7 +627,7 @@ macro_rules! make_mir_visitor {
} = *var_decl;

self.visit_ty(ty);
self.visit_scope_id(scope);
self.visit_visibility_scope(scope);
self.visit_span(span);
}

Expand All @@ -651,8 +651,8 @@ macro_rules! make_mir_visitor {
self.visit_ty(ty);
}

fn super_scope_id(&mut self,
_scope_id: & $($mutability)* ScopeId) {
fn super_visibility_scope(&mut self,
_scope: & $($mutability)* VisibilityScope) {
}

fn super_branch(&mut self,
Expand Down
39 changes: 27 additions & 12 deletions src/librustc_mir/build/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
ast_block: &'tcx hir::Block)
-> BlockAnd<()> {
let Block { extent, span, stmts, expr } = self.hir.mirror(ast_block);
self.in_scope(extent, block, move |this, _| {
self.in_scope(extent, block, move |this| {
// This convoluted structure is to avoid using recursion as we walk down a list
// of statements. Basically, the structure we get back is something like:
//
Expand All @@ -40,27 +40,40 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
//
// First we build all the statements in the block.
let mut let_extent_stack = Vec::with_capacity(8);
let outer_visibility_scope = this.visibility_scope;
for stmt in stmts {
let Stmt { span: _, kind } = this.hir.mirror(stmt);
match kind {
StmtKind::Expr { scope, expr } => {
unpack!(block = this.in_scope(scope, block, |this, _| {
unpack!(block = this.in_scope(scope, block, |this| {
let expr = this.hir.mirror(expr);
this.stmt_expr(block, expr)
}));
}
StmtKind::Let { remainder_scope, init_scope, pattern, initializer } => {
let remainder_scope_id = this.push_scope(remainder_scope, block);
let tcx = this.hir.tcx();

// Enter the remainder scope, i.e. the bindings' destruction scope.
this.push_scope(remainder_scope, block);
let_extent_stack.push(remainder_scope);
unpack!(block = this.in_scope(init_scope, block, move |this, _| {
// FIXME #30046 ^~~~
if let Some(init) = initializer {
this.expr_into_pattern(block, remainder_scope_id, pattern, init)
} else {
this.declare_bindings(remainder_scope_id, &pattern);
block.unit()
}
}));

// Declare the bindings, which may create a visibility scope.
let remainder_span = remainder_scope.span(&tcx.region_maps, &tcx.map);
let remainder_span = remainder_span.unwrap_or(span);
let scope = this.declare_bindings(None, remainder_span, &pattern);

// Evaluate the initializer, if present.
if let Some(init) = initializer {
unpack!(block = this.in_scope(init_scope, block, move |this| {
// FIXME #30046 ^~~~
this.expr_into_pattern(block, pattern, init)
}));
}

// Enter the visibility scope, after evaluating the initializer.
if let Some(visibility_scope) = scope {
this.visibility_scope = visibility_scope;
}
}
}
}
Expand All @@ -78,6 +91,8 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
for extent in let_extent_stack.into_iter().rev() {
unpack!(block = this.pop_scope(extent, block));
}
// Restore the original visibility scope.
this.visibility_scope = outer_visibility_scope;
block.unit()
})
}
Expand Down
8 changes: 4 additions & 4 deletions src/librustc_mir/build/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl<'tcx> CFG<'tcx> {

pub fn push_assign(&mut self,
block: BasicBlock,
scope: ScopeId,
scope: VisibilityScope,
span: Span,
lvalue: &Lvalue<'tcx>,
rvalue: Rvalue<'tcx>) {
Expand All @@ -63,7 +63,7 @@ impl<'tcx> CFG<'tcx> {

pub fn push_assign_constant(&mut self,
block: BasicBlock,
scope: ScopeId,
scope: VisibilityScope,
span: Span,
temp: &Lvalue<'tcx>,
constant: Constant<'tcx>) {
Expand All @@ -73,7 +73,7 @@ impl<'tcx> CFG<'tcx> {

pub fn push_assign_unit(&mut self,
block: BasicBlock,
scope: ScopeId,
scope: VisibilityScope,
span: Span,
lvalue: &Lvalue<'tcx>) {
self.push_assign(block, scope, span, lvalue, Rvalue::Aggregate(
Expand All @@ -83,7 +83,7 @@ impl<'tcx> CFG<'tcx> {

pub fn terminate(&mut self,
block: BasicBlock,
scope: ScopeId,
scope: VisibilityScope,
span: Span,
kind: TerminatorKind<'tcx>) {
debug_assert!(self.block_data(block).terminator.is_none(),
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir/build/expr/as_lvalue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
let expr_span = expr.span;
match expr.kind {
ExprKind::Scope { extent, value } => {
this.in_scope(extent, block, |this, _| this.as_lvalue(block, value))
this.in_scope(extent, block, |this| this.as_lvalue(block, value))
}
ExprKind::Field { lhs, name } => {
let lvalue = unpack!(block = this.as_lvalue(block, lhs));
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir/build/expr/as_operand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
let this = self;

if let ExprKind::Scope { extent, value } = expr.kind {
return this.in_scope(extent, block, |this, _| this.as_operand(block, value));
return this.in_scope(extent, block, |this| this.as_operand(block, value));
}

let category = Category::of(&expr.kind).unwrap();
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_mir/build/expr/as_rvalue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {

match expr.kind {
ExprKind::Scope { extent, value } => {
this.in_scope(extent, block, |this, _| this.as_rvalue(block, value))
this.in_scope(extent, block, |this| this.as_rvalue(block, value))
}
ExprKind::InlineAsm { asm, outputs, inputs } => {
let outputs = outputs.into_iter().map(|output| {
Expand Down Expand Up @@ -100,7 +100,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
let result = this.temp(expr.ty);
// to start, malloc some memory of suitable type (thus far, uninitialized):
this.cfg.push_assign(block, scope_id, expr_span, &result, Rvalue::Box(value.ty));
this.in_scope(value_extents, block, |this, _| {
this.in_scope(value_extents, block, |this| {
// schedule a shallow free of that memory, lest we unwind:
this.schedule_box_free(expr_span, value_extents, &result, value.ty);
// initialize the box contents:
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir/build/expr/as_temp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
let this = self;

if let ExprKind::Scope { extent, value } = expr.kind {
return this.in_scope(extent, block, |this, _| this.as_temp(block, value));
return this.in_scope(extent, block, |this| this.as_temp(block, value));
}

let expr_ty = expr.ty.clone();
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir/build/expr/into.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {

match expr.kind {
ExprKind::Scope { extent, value } => {
this.in_scope(extent, block, |this, _| this.into(destination, block, value))
this.in_scope(extent, block, |this| this.into(destination, block, value))
}
ExprKind::Block { body: ast_block } => {
this.ast_block(destination, expr.ty.is_nil(), block, ast_block)
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir/build/expr/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
match expr.kind {
ExprKind::Scope { extent, value } => {
let value = this.hir.mirror(value);
this.in_scope(extent, block, |this, _| this.stmt_expr(block, value))
this.in_scope(extent, block, |this| this.stmt_expr(block, value))
}
ExprKind::Assign { lhs, rhs } => {
let lhs = this.hir.mirror(lhs);
Expand Down
Loading

0 comments on commit 719a591

Please sign in to comment.