]> git.proxmox.com Git - rustc.git/blobdiff - src/librustc/mir/visit.rs
New upstream version 1.34.2+dfsg1
[rustc.git] / src / librustc / mir / visit.rs
index ac4f54b4b49627ff05ee7692e73689ea62e889c9..e5828039ac29cb33fb1510aa05359a75a9a7a7d4 100644 (file)
-// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
-// file at the top-level directory of this distribution and at
-// http://rust-lang.org/COPYRIGHT.
+use crate::hir::def_id::DefId;
+use crate::ty::subst::Substs;
+use crate::ty::{CanonicalUserTypeAnnotation, ClosureSubsts, GeneratorSubsts, Region, Ty};
+use crate::mir::*;
+use syntax_pos::Span;
+
+// # The MIR Visitor
+//
+// ## Overview
+//
+// There are two visitors, one for immutable and one for mutable references,
+// but both are generated by the following macro. The code is written according
+// to the following conventions:
+//
+// - introduce a `visit_foo` and a `super_foo` method for every MIR type
+// - `visit_foo`, by default, calls `super_foo`
+// - `super_foo`, by default, destructures the `foo` and calls `visit_foo`
+//
+// This allows you as a user to override `visit_foo` for types are
+// interested in, and invoke (within that method) call
+// `self.super_foo` to get the default behavior. Just as in an OO
+// language, you should never call `super` methods ordinarily except
+// in that circumstance.
+//
+// For the most part, we do not destructure things external to the
+// MIR, e.g., types, spans, etc, but simply visit them and stop. This
+// avoids duplication with other visitors like `TypeFoldable`.
+//
+// ## Updating
+//
+// The code is written in a very deliberate style intended to minimize
+// the chance of things being overlooked. You'll notice that we always
+// use pattern matching to reference fields and we ensure that all
+// matches are exhaustive.
+//
+// For example, the `super_basic_block_data` method begins like this:
+//
+// ```rust
+// fn super_basic_block_data(&mut self,
+//                           block: BasicBlock,
+//                           data: & $($mutability)? BasicBlockData<'tcx>) {
+//     let BasicBlockData {
+//         statements,
+//         terminator,
+//         is_cleanup: _
+//     } = *data;
+//
+//     for statement in statements {
+//         self.visit_statement(block, statement);
+//     }
+//
+//     ...
+// }
+// ```
+//
+// Here we used `let BasicBlockData { <fields> } = *data` deliberately,
+// rather than writing `data.statements` in the body. This is because if one
+// adds a new field to `BasicBlockData`, one will be forced to revise this code,
+// and hence one will (hopefully) invoke the correct visit methods (if any).
 //
-// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
-// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
-// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
-// option. This file may not be copied, modified, or distributed
-// except according to those terms.
+// For this to work, ALL MATCHES MUST BE EXHAUSTIVE IN FIELDS AND VARIANTS.
+// That means you never write `..` to skip over fields, nor do you write `_`
+// to skip over variants in a `match`.
+//
+// The only place that `_` is acceptable is to match a field (or
+// variant argument) that does not require visiting, as in
+// `is_cleanup` above.
 
-use middle::ty::Region;
-use mir::repr::*;
+macro_rules! make_mir_visitor {
+    ($visitor_trait_name:ident, $($mutability:ident)?) => {
+        pub trait $visitor_trait_name<'tcx> {
+            // Override these, and call `self.super_xxx` to revert back to the
+            // default behavior.
 
-pub trait Visitor<'tcx> {
-    // Override these, and call `self.super_xxx` to revert back to the
-    // default behavior.
+            fn visit_mir(&mut self, mir: & $($mutability)? Mir<'tcx>) {
+                self.super_mir(mir);
+            }
 
-    fn visit_mir(&mut self, mir: &Mir<'tcx>) {
-        self.super_mir(mir);
-    }
+            fn visit_basic_block_data(&mut self,
+                                      block: BasicBlock,
+                                      data: & $($mutability)? BasicBlockData<'tcx>) {
+                self.super_basic_block_data(block, data);
+            }
 
-    fn visit_basic_block_data(&mut self, block: BasicBlock, data: &BasicBlockData<'tcx>) {
-        self.super_basic_block_data(block, data);
-    }
+            fn visit_source_scope_data(&mut self,
+                                           scope_data: & $($mutability)? SourceScopeData) {
+                self.super_source_scope_data(scope_data);
+            }
 
-    fn visit_statement(&mut self, block: BasicBlock, statement: &Statement<'tcx>) {
-        self.super_statement(block, statement);
-    }
+            fn visit_statement(&mut self,
+                               block: BasicBlock,
+                               statement: & $($mutability)? Statement<'tcx>,
+                               location: Location) {
+                self.super_statement(block, statement, location);
+            }
 
-    fn visit_assign(&mut self, block: BasicBlock, lvalue: &Lvalue<'tcx>, rvalue: &Rvalue<'tcx>) {
-        self.super_assign(block, lvalue, rvalue);
-    }
+            fn visit_assign(&mut self,
+                            block: BasicBlock,
+                            place: & $($mutability)? Place<'tcx>,
+                            rvalue: & $($mutability)? Rvalue<'tcx>,
+                            location: Location) {
+                self.super_assign(block, place, rvalue, location);
+            }
 
-    fn visit_terminator(&mut self, block: BasicBlock, terminator: &Terminator<'tcx>) {
-        self.super_terminator(block, terminator);
-    }
+            fn visit_terminator(&mut self,
+                                block: BasicBlock,
+                                terminator: & $($mutability)? Terminator<'tcx>,
+                                location: Location) {
+                self.super_terminator(block, terminator, location);
+            }
 
-    fn visit_rvalue(&mut self, rvalue: &Rvalue<'tcx>) {
-        self.super_rvalue(rvalue);
-    }
+            fn visit_terminator_kind(&mut self,
+                                     block: BasicBlock,
+                                     kind: & $($mutability)? TerminatorKind<'tcx>,
+                                     location: Location) {
+                self.super_terminator_kind(block, kind, location);
+            }
 
-    fn visit_operand(&mut self, operand: &Operand<'tcx>) {
-        self.super_operand(operand);
-    }
+            fn visit_assert_message(&mut self,
+                                    msg: & $($mutability)? AssertMessage<'tcx>,
+                                    location: Location) {
+                self.super_assert_message(msg, location);
+            }
 
-    fn visit_lvalue(&mut self, lvalue: &Lvalue<'tcx>, context: LvalueContext) {
-        self.super_lvalue(lvalue, context);
-    }
+            fn visit_rvalue(&mut self,
+                            rvalue: & $($mutability)? Rvalue<'tcx>,
+                            location: Location) {
+                self.super_rvalue(rvalue, location);
+            }
 
-    fn visit_branch(&mut self, source: BasicBlock, target: BasicBlock) {
-        self.super_branch(source, target);
-    }
+            fn visit_operand(&mut self,
+                             operand: & $($mutability)? Operand<'tcx>,
+                             location: Location) {
+                self.super_operand(operand, location);
+            }
 
-    fn visit_constant(&mut self, constant: &Constant<'tcx>) {
-        self.super_constant(constant);
-    }
+            fn visit_ascribe_user_ty(&mut self,
+                                     place: & $($mutability)? Place<'tcx>,
+                                     variance: & $($mutability)? ty::Variance,
+                                     user_ty: & $($mutability)? UserTypeProjection<'tcx>,
+                                     location: Location) {
+                self.super_ascribe_user_ty(place, variance, user_ty, location);
+            }
 
-    // The `super_xxx` methods comprise the default behavior and are
-    // not meant to be overidden.
+            fn visit_retag(&mut self,
+                           kind: & $($mutability)? RetagKind,
+                           place: & $($mutability)? Place<'tcx>,
+                           location: Location) {
+                self.super_retag(kind, place, location);
+            }
 
-    fn super_mir(&mut self, mir: &Mir<'tcx>) {
-        for block in mir.all_basic_blocks() {
-            let data = mir.basic_block_data(block);
-            self.visit_basic_block_data(block, data);
-        }
-    }
+            fn visit_place(&mut self,
+                            place: & $($mutability)? Place<'tcx>,
+                            context: PlaceContext<'tcx>,
+                            location: Location) {
+                self.super_place(place, context, location);
+            }
 
-    fn super_basic_block_data(&mut self, block: BasicBlock, data: &BasicBlockData<'tcx>) {
-        for statement in &data.statements {
-            self.visit_statement(block, statement);
-        }
-        self.visit_terminator(block, &data.terminator);
-    }
+            fn visit_static(&mut self,
+                            static_: & $($mutability)? Static<'tcx>,
+                            context: PlaceContext<'tcx>,
+                            location: Location) {
+                self.super_static(static_, context, location);
+            }
 
-    fn super_statement(&mut self, block: BasicBlock, statement: &Statement<'tcx>) {
-        match statement.kind {
-            StatementKind::Assign(ref lvalue, ref rvalue) => {
-                self.visit_assign(block, lvalue, rvalue);
+            fn visit_projection(&mut self,
+                                place: & $($mutability)? PlaceProjection<'tcx>,
+                                context: PlaceContext<'tcx>,
+                                location: Location) {
+                self.super_projection(place, context, location);
             }
-            StatementKind::Drop(_, ref lvalue) => {
-                self.visit_lvalue(lvalue, LvalueContext::Drop);
+
+            fn visit_projection_elem(&mut self,
+                                     place: & $($mutability)? PlaceElem<'tcx>,
+                                     location: Location) {
+                self.super_projection_elem(place, location);
             }
-        }
-    }
 
-    fn super_assign(&mut self, _block: BasicBlock, lvalue: &Lvalue<'tcx>, rvalue: &Rvalue<'tcx>) {
-        self.visit_lvalue(lvalue, LvalueContext::Store);
-        self.visit_rvalue(rvalue);
-    }
+            fn visit_branch(&mut self,
+                            source: BasicBlock,
+                            target: BasicBlock) {
+                self.super_branch(source, target);
+            }
 
-    fn super_terminator(&mut self, block: BasicBlock, terminator: &Terminator<'tcx>) {
-        match *terminator {
-            Terminator::Goto { target } |
-            Terminator::Panic { target } => {
-                self.visit_branch(block, target);
+            fn visit_constant(&mut self,
+                              constant: & $($mutability)? Constant<'tcx>,
+                              location: Location) {
+                self.super_constant(constant, location);
             }
 
-            Terminator::If { ref cond, ref targets } => {
-                self.visit_operand(cond);
-                for &target in &targets[..] {
-                    self.visit_branch(block, target);
-                }
+            fn visit_def_id(&mut self,
+                            def_id: & $($mutability)? DefId,
+                            _: Location) {
+                self.super_def_id(def_id);
+            }
+
+            fn visit_span(&mut self,
+                          span: & $($mutability)? Span) {
+                self.super_span(span);
+            }
+
+            fn visit_source_info(&mut self,
+                                 source_info: & $($mutability)? SourceInfo) {
+                self.super_source_info(source_info);
             }
 
-            Terminator::Switch { ref discr, adt_def: _, ref targets } => {
-                self.visit_lvalue(discr, LvalueContext::Inspect);
-                for &target in targets {
-                    self.visit_branch(block, target);
+            fn visit_ty(&mut self,
+                        ty: & $($mutability)? Ty<'tcx>,
+                        _: TyContext) {
+                self.super_ty(ty);
+            }
+
+            fn visit_user_type_projection(
+                &mut self,
+                ty: & $($mutability)? UserTypeProjection<'tcx>,
+            ) {
+                self.super_user_type_projection(ty);
+            }
+
+            fn visit_user_type_annotation(
+                &mut self,
+                index: UserTypeAnnotationIndex,
+                ty: & $($mutability)? CanonicalUserTypeAnnotation<'tcx>,
+            ) {
+                self.super_user_type_annotation(index, ty);
+            }
+
+            fn visit_region(&mut self,
+                            region: & $($mutability)? ty::Region<'tcx>,
+                            _: Location) {
+                self.super_region(region);
+            }
+
+            fn visit_const(&mut self,
+                           constant: & $($mutability)? &'tcx ty::LazyConst<'tcx>,
+                           _: Location) {
+                self.super_const(constant);
+            }
+
+            fn visit_substs(&mut self,
+                            substs: & $($mutability)? &'tcx Substs<'tcx>,
+                            _: Location) {
+                self.super_substs(substs);
+            }
+
+            fn visit_closure_substs(&mut self,
+                                    substs: & $($mutability)? ClosureSubsts<'tcx>,
+                                    _: Location) {
+                self.super_closure_substs(substs);
+            }
+
+            fn visit_generator_substs(&mut self,
+                                      substs: & $($mutability)? GeneratorSubsts<'tcx>,
+                                    _: Location) {
+                self.super_generator_substs(substs);
+            }
+
+            fn visit_local_decl(&mut self,
+                                local: Local,
+                                local_decl: & $($mutability)? LocalDecl<'tcx>) {
+                self.super_local_decl(local, local_decl);
+            }
+
+            fn visit_local(&mut self,
+                            _local: & $($mutability)? Local,
+                            _context: PlaceContext<'tcx>,
+                            _location: Location) {
+            }
+
+            fn visit_source_scope(&mut self,
+                                      scope: & $($mutability)? SourceScope) {
+                self.super_source_scope(scope);
+            }
+
+            // The `super_xxx` methods comprise the default behavior and are
+            // not meant to be overridden.
+
+            fn super_mir(&mut self,
+                         mir: & $($mutability)? Mir<'tcx>) {
+                if let Some(yield_ty) = &$($mutability)? mir.yield_ty {
+                    self.visit_ty(yield_ty, TyContext::YieldTy(SourceInfo {
+                        span: mir.span,
+                        scope: OUTERMOST_SOURCE_SCOPE,
+                    }));
+                }
+
+                // for best performance, we want to use an iterator rather
+                // than a for-loop, to avoid calling Mir::invalidate for
+                // each basic block.
+                macro_rules! basic_blocks {
+                    (mut) => (mir.basic_blocks_mut().iter_enumerated_mut());
+                    () => (mir.basic_blocks().iter_enumerated());
+                };
+                for (bb, data) in basic_blocks!($($mutability)?) {
+                    self.visit_basic_block_data(bb, data);
+                }
+
+                for scope in &$($mutability)? mir.source_scopes {
+                    self.visit_source_scope_data(scope);
+                }
+
+                self.visit_ty(&$($mutability)? mir.return_ty(), TyContext::ReturnTy(SourceInfo {
+                    span: mir.span,
+                    scope: OUTERMOST_SOURCE_SCOPE,
+                }));
+
+                for local in mir.local_decls.indices() {
+                    self.visit_local_decl(local, & $($mutability)? mir.local_decls[local]);
+                }
+
+                macro_rules! type_annotations {
+                    (mut) => (mir.user_type_annotations.iter_enumerated_mut());
+                    () => (mir.user_type_annotations.iter_enumerated());
+                };
+
+                for (index, annotation) in type_annotations!($($mutability)?) {
+                    self.visit_user_type_annotation(
+                        index, annotation
+                    );
                 }
+
+                self.visit_span(&$($mutability)? mir.span);
             }
 
-            Terminator::SwitchInt { ref discr, switch_ty: _, values: _, ref targets } => {
-                self.visit_lvalue(discr, LvalueContext::Inspect);
-                for &target in targets {
-                    self.visit_branch(block, target);
+            fn super_basic_block_data(&mut self,
+                                      block: BasicBlock,
+                                      data: & $($mutability)? BasicBlockData<'tcx>) {
+                let BasicBlockData {
+                    statements,
+                    terminator,
+                    is_cleanup: _
+                } = data;
+
+                let mut index = 0;
+                for statement in statements {
+                    let location = Location { block: block, statement_index: index };
+                    self.visit_statement(block, statement, location);
+                    index += 1;
+                }
+
+                if let Some(terminator) = terminator {
+                    let location = Location { block: block, statement_index: index };
+                    self.visit_terminator(block, terminator, location);
                 }
             }
 
-            Terminator::Diverge |
-            Terminator::Return => {
+            fn super_source_scope_data(&mut self, scope_data: & $($mutability)? SourceScopeData) {
+                let SourceScopeData {
+                    span,
+                    parent_scope,
+                } = scope_data;
+
+                self.visit_span(span);
+                if let Some(parent_scope) = parent_scope {
+                    self.visit_source_scope(parent_scope);
+                }
             }
 
-            Terminator::Call { ref data, ref targets } => {
-                self.visit_lvalue(&data.destination, LvalueContext::Store);
-                self.visit_operand(&data.func);
-                for arg in &data.args {
-                    self.visit_operand(arg);
+            fn super_statement(&mut self,
+                               block: BasicBlock,
+                               statement: & $($mutability)? Statement<'tcx>,
+                               location: Location) {
+                let Statement {
+                    source_info,
+                    kind,
+                } = statement;
+
+                self.visit_source_info(source_info);
+                match kind {
+                    StatementKind::Assign(place, rvalue) => {
+                        self.visit_assign(block, place, rvalue, location);
+                    }
+                    StatementKind::FakeRead(_, place) => {
+                        self.visit_place(
+                            place,
+                            PlaceContext::NonMutatingUse(NonMutatingUseContext::Inspect),
+                            location
+                        );
+                    }
+                    StatementKind::SetDiscriminant { place, .. } => {
+                        self.visit_place(
+                            place,
+                            PlaceContext::MutatingUse(MutatingUseContext::Store),
+                            location
+                        );
+                    }
+                    StatementKind::StorageLive(local) => {
+                        self.visit_local(
+                            local,
+                            PlaceContext::NonUse(NonUseContext::StorageLive),
+                            location
+                        );
+                    }
+                    StatementKind::StorageDead(local) => {
+                        self.visit_local(
+                            local,
+                            PlaceContext::NonUse(NonUseContext::StorageDead),
+                            location
+                        );
+                    }
+                    StatementKind::InlineAsm { outputs, inputs, asm: _ } => {
+                        for output in & $($mutability)? outputs[..] {
+                            self.visit_place(
+                                output,
+                                PlaceContext::MutatingUse(MutatingUseContext::AsmOutput),
+                                location
+                            );
+                        }
+                        for (span, input) in & $($mutability)? inputs[..] {
+                            self.visit_span(span);
+                            self.visit_operand(input, location);
+                        }
+                    }
+                    StatementKind::Retag(kind, place) => {
+                        self.visit_retag(kind, place, location);
+                    }
+                    StatementKind::AscribeUserType(place, variance, user_ty) => {
+                        self.visit_ascribe_user_ty(place, variance, user_ty, location);
+                    }
+                    StatementKind::Nop => {}
                 }
-                for &target in &targets[..] {
-                    self.visit_branch(block, target);
+            }
+
+            fn super_assign(&mut self,
+                            _block: BasicBlock,
+                            place: &$($mutability)? Place<'tcx>,
+                            rvalue: &$($mutability)? Rvalue<'tcx>,
+                            location: Location) {
+                self.visit_place(
+                    place,
+                    PlaceContext::MutatingUse(MutatingUseContext::Store),
+                    location
+                );
+                self.visit_rvalue(rvalue, location);
+            }
+
+            fn super_terminator(&mut self,
+                                block: BasicBlock,
+                                terminator: &$($mutability)? Terminator<'tcx>,
+                                location: Location) {
+                let Terminator { source_info, kind } = terminator;
+
+                self.visit_source_info(source_info);
+                self.visit_terminator_kind(block, kind, location);
+            }
+
+            fn super_terminator_kind(&mut self,
+                                     block: BasicBlock,
+                                     kind: & $($mutability)? TerminatorKind<'tcx>,
+                                     source_location: Location) {
+                match kind {
+                    TerminatorKind::Goto { target } => {
+                        self.visit_branch(block, *target);
+                    }
+
+                    TerminatorKind::SwitchInt {
+                        discr,
+                        switch_ty,
+                        values: _,
+                        targets
+                    } => {
+                        self.visit_operand(discr, source_location);
+                        self.visit_ty(switch_ty, TyContext::Location(source_location));
+                        for target in targets {
+                            self.visit_branch(block, *target);
+                        }
+                    }
+
+                    TerminatorKind::Resume |
+                    TerminatorKind::Abort |
+                    TerminatorKind::Return |
+                    TerminatorKind::GeneratorDrop |
+                    TerminatorKind::Unreachable => {
+                    }
+
+                    TerminatorKind::Drop {
+                        location,
+                        target,
+                        unwind,
+                    } => {
+                        self.visit_place(
+                            location,
+                            PlaceContext::MutatingUse(MutatingUseContext::Drop),
+                            source_location
+                        );
+                        self.visit_branch(block, *target);
+                        unwind.map(|t| self.visit_branch(block, t));
+                    }
+
+                    TerminatorKind::DropAndReplace {
+                        location,
+                        value,
+                        target,
+                        unwind,
+                    } => {
+                        self.visit_place(
+                            location,
+                            PlaceContext::MutatingUse(MutatingUseContext::Drop),
+                            source_location
+                        );
+                        self.visit_operand(value, source_location);
+                        self.visit_branch(block, *target);
+                        unwind.map(|t| self.visit_branch(block, t));
+                    }
+
+                    TerminatorKind::Call {
+                        func,
+                        args,
+                        destination,
+                        cleanup,
+                        from_hir_call: _,
+                    } => {
+                        self.visit_operand(func, source_location);
+                        for arg in args {
+                            self.visit_operand(arg, source_location);
+                        }
+                        if let Some((destination, target)) = destination {
+                            self.visit_place(
+                                destination,
+                                PlaceContext::MutatingUse(MutatingUseContext::Call),
+                                source_location
+                            );
+                            self.visit_branch(block, *target);
+                        }
+                        cleanup.map(|t| self.visit_branch(block, t));
+                    }
+
+                    TerminatorKind::Assert {
+                        cond,
+                        expected: _,
+                        msg,
+                        target,
+                        cleanup,
+                    } => {
+                        self.visit_operand(cond, source_location);
+                        self.visit_assert_message(msg, source_location);
+                        self.visit_branch(block, *target);
+                        cleanup.map(|t| self.visit_branch(block, t));
+                    }
+
+                    TerminatorKind::Yield {
+                        value,
+                        resume,
+                        drop,
+                    } => {
+                        self.visit_operand(value, source_location);
+                        self.visit_branch(block, *resume);
+                        drop.map(|t| self.visit_branch(block, t));
+                    }
+
+                    TerminatorKind::FalseEdges { real_target, imaginary_targets } => {
+                        self.visit_branch(block, *real_target);
+                        for target in imaginary_targets {
+                            self.visit_branch(block, *target);
+                        }
+                    }
+
+                    TerminatorKind::FalseUnwind { real_target, unwind } => {
+                        self.visit_branch(block, *real_target);
+                        if let Some(unwind) = unwind {
+                            self.visit_branch(block, *unwind);
+                        }
+                    }
                 }
             }
-        }
-    }
 
-    fn super_rvalue(&mut self, rvalue: &Rvalue<'tcx>) {
-        match *rvalue {
-            Rvalue::Use(ref operand) => {
-                self.visit_operand(operand);
+            fn super_assert_message(&mut self,
+                                    msg: & $($mutability)? AssertMessage<'tcx>,
+                                    location: Location) {
+                use crate::mir::interpret::EvalErrorKind::*;
+                if let BoundsCheck { len, index } = msg {
+                    self.visit_operand(len, location);
+                    self.visit_operand(index, location);
+                }
             }
 
-            Rvalue::Repeat(ref value, ref len) => {
-                self.visit_operand(value);
-                self.visit_constant(len);
+            fn super_rvalue(&mut self,
+                            rvalue: & $($mutability)? Rvalue<'tcx>,
+                            location: Location) {
+                match rvalue {
+                    Rvalue::Use(operand) => {
+                        self.visit_operand(operand, location);
+                    }
+
+                    Rvalue::Repeat(value, _) => {
+                        self.visit_operand(value, location);
+                    }
+
+                    Rvalue::Ref(r, bk, path) => {
+                        self.visit_region(r, location);
+                        let ctx = match bk {
+                            BorrowKind::Shared => PlaceContext::NonMutatingUse(
+                                NonMutatingUseContext::SharedBorrow(*r)
+                            ),
+                            BorrowKind::Shallow => PlaceContext::NonMutatingUse(
+                                NonMutatingUseContext::ShallowBorrow(*r)
+                            ),
+                            BorrowKind::Unique => PlaceContext::NonMutatingUse(
+                                NonMutatingUseContext::UniqueBorrow(*r)
+                            ),
+                            BorrowKind::Mut { .. } =>
+                                PlaceContext::MutatingUse(MutatingUseContext::Borrow(*r)),
+                        };
+                        self.visit_place(path, ctx, location);
+                    }
+
+                    Rvalue::Len(path) => {
+                        self.visit_place(
+                            path,
+                            PlaceContext::NonMutatingUse(NonMutatingUseContext::Inspect),
+                            location
+                        );
+                    }
+
+                    Rvalue::Cast(_cast_kind, operand, ty) => {
+                        self.visit_operand(operand, location);
+                        self.visit_ty(ty, TyContext::Location(location));
+                    }
+
+                    Rvalue::BinaryOp(_bin_op, lhs, rhs)
+                    | Rvalue::CheckedBinaryOp(_bin_op, lhs, rhs) => {
+                        self.visit_operand(lhs, location);
+                        self.visit_operand(rhs, location);
+                    }
+
+                    Rvalue::UnaryOp(_un_op, op) => {
+                        self.visit_operand(op, location);
+                    }
+
+                    Rvalue::Discriminant(place) => {
+                        self.visit_place(
+                            place,
+                            PlaceContext::NonMutatingUse(NonMutatingUseContext::Inspect),
+                            location
+                        );
+                    }
+
+                    Rvalue::NullaryOp(_op, ty) => {
+                        self.visit_ty(ty, TyContext::Location(location));
+                    }
+
+                    Rvalue::Aggregate(kind, operands) => {
+                        let kind = &$($mutability)? **kind;
+                        match kind {
+                            AggregateKind::Array(ty) => {
+                                self.visit_ty(ty, TyContext::Location(location));
+                            }
+                            AggregateKind::Tuple => {
+                            }
+                            AggregateKind::Adt(
+                                _adt_def,
+                                _variant_index,
+                                substs,
+                                _user_substs,
+                                _active_field_index
+                            ) => {
+                                self.visit_substs(substs, location);
+                            }
+                            AggregateKind::Closure(
+                                def_id,
+                                closure_substs
+                            ) => {
+                                self.visit_def_id(def_id, location);
+                                self.visit_closure_substs(closure_substs, location);
+                            }
+                            AggregateKind::Generator(
+                                def_id,
+                                generator_substs,
+                                _movability,
+                            ) => {
+                                self.visit_def_id(def_id, location);
+                                self.visit_generator_substs(generator_substs, location);
+                            }
+                        }
+
+                        for operand in operands {
+                            self.visit_operand(operand, location);
+                        }
+                    }
+                }
             }
 
-            Rvalue::Ref(r, bk, ref path) => {
-                self.visit_lvalue(path, LvalueContext::Borrow {
-                    region: r,
-                    kind: bk
-                });
+            fn super_operand(&mut self,
+                             operand: & $($mutability)? Operand<'tcx>,
+                             location: Location) {
+                match operand {
+                    Operand::Copy(place) => {
+                        self.visit_place(
+                            place,
+                            PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy),
+                            location
+                        );
+                    }
+                    Operand::Move(place) => {
+                        self.visit_place(
+                            place,
+                            PlaceContext::NonMutatingUse(NonMutatingUseContext::Move),
+                            location
+                        );
+                    }
+                    Operand::Constant(constant) => {
+                        self.visit_constant(constant, location);
+                    }
+                }
             }
 
-            Rvalue::Len(ref path) => {
-                self.visit_lvalue(path, LvalueContext::Inspect);
+            fn super_ascribe_user_ty(&mut self,
+                                     place: & $($mutability)? Place<'tcx>,
+                                     _variance: & $($mutability)? ty::Variance,
+                                     user_ty: & $($mutability)? UserTypeProjection<'tcx>,
+                                     location: Location) {
+                self.visit_place(
+                    place,
+                    PlaceContext::NonUse(NonUseContext::AscribeUserTy),
+                    location
+                );
+                self.visit_user_type_projection(user_ty);
             }
 
-            Rvalue::Cast(_, ref operand, _) => {
-                self.visit_operand(operand);
+            fn super_retag(&mut self,
+                           _kind: & $($mutability)? RetagKind,
+                           place: & $($mutability)? Place<'tcx>,
+                           location: Location) {
+                self.visit_place(
+                    place,
+                    PlaceContext::MutatingUse(MutatingUseContext::Retag),
+                    location,
+                );
             }
 
-            Rvalue::BinaryOp(_, ref lhs, ref rhs) => {
-                self.visit_operand(lhs);
-                self.visit_operand(rhs);
+            fn super_place(&mut self,
+                            place: & $($mutability)? Place<'tcx>,
+                            context: PlaceContext<'tcx>,
+                            location: Location) {
+                match place {
+                    Place::Local(local) => {
+                        self.visit_local(local, context, location);
+                    }
+                    Place::Static(static_) => {
+                        self.visit_static(static_, context, location);
+                    }
+                    Place::Promoted(promoted) => {
+                        self.visit_ty(& $($mutability)? promoted.1, TyContext::Location(location));
+                    },
+                    Place::Projection(proj) => {
+                        self.visit_projection(proj, context, location);
+                    }
+                }
             }
 
-            Rvalue::UnaryOp(_, ref op) => {
-                self.visit_operand(op);
+            fn super_static(&mut self,
+                            static_: & $($mutability)? Static<'tcx>,
+                            _context: PlaceContext<'tcx>,
+                            location: Location) {
+                let Static { def_id, ty } = static_;
+                self.visit_def_id(def_id, location);
+                self.visit_ty(ty, TyContext::Location(location));
             }
 
-            Rvalue::Box(_) => {
+            fn super_projection(&mut self,
+                                proj: & $($mutability)? PlaceProjection<'tcx>,
+                                context: PlaceContext<'tcx>,
+                                location: Location) {
+                let Projection { base, elem } = proj;
+                let context = if context.is_mutating_use() {
+                    PlaceContext::MutatingUse(MutatingUseContext::Projection)
+                } else {
+                    PlaceContext::NonMutatingUse(NonMutatingUseContext::Projection)
+                };
+                self.visit_place(base, context, location);
+                self.visit_projection_elem(elem, location);
             }
 
-            Rvalue::Aggregate(_, ref operands) => {
-                for operand in operands {
-                    self.visit_operand(operand);
+            fn super_projection_elem(&mut self,
+                                     proj: & $($mutability)? PlaceElem<'tcx>,
+                                     location: Location) {
+                match proj {
+                    ProjectionElem::Deref => {
+                    }
+                    ProjectionElem::Subslice { from: _, to: _ } => {
+                    }
+                    ProjectionElem::Field(_field, ty) => {
+                        self.visit_ty(ty, TyContext::Location(location));
+                    }
+                    ProjectionElem::Index(local) => {
+                        self.visit_local(
+                            local,
+                            PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy),
+                            location
+                        );
+                    }
+                    ProjectionElem::ConstantIndex { offset: _,
+                                                    min_length: _,
+                                                    from_end: _ } => {
+                    }
+                    ProjectionElem::Downcast(_adt_def, _variant_index) => {
+                    }
                 }
             }
 
-            Rvalue::Slice { ref input, from_start, from_end } => {
-                self.visit_lvalue(input, LvalueContext::Slice {
-                    from_start: from_start,
-                    from_end: from_end,
+            fn super_local_decl(&mut self,
+                                local: Local,
+                                local_decl: & $($mutability)? LocalDecl<'tcx>) {
+                let LocalDecl {
+                    mutability: _,
+                    ty,
+                    user_ty,
+                    name: _,
+                    source_info,
+                    visibility_scope,
+                    internal: _,
+                    is_user_variable: _,
+                    is_block_tail: _,
+                } = local_decl;
+
+                self.visit_ty(ty, TyContext::LocalDecl {
+                    local,
+                    source_info: *source_info,
                 });
+                for (user_ty, _) in & $($mutability)? user_ty.contents {
+                    self.visit_user_type_projection(user_ty);
+                }
+                self.visit_source_info(source_info);
+                self.visit_source_scope(visibility_scope);
             }
 
-            Rvalue::InlineAsm(_) => {
+            fn super_source_scope(&mut self,
+                                      _scope: & $($mutability)? SourceScope) {
             }
-        }
-    }
 
-    fn super_operand(&mut self, operand: &Operand<'tcx>) {
-        match *operand {
-            Operand::Consume(ref lvalue) => {
-                self.visit_lvalue(lvalue, LvalueContext::Consume);
+            fn super_branch(&mut self,
+                            _source: BasicBlock,
+                            _target: BasicBlock) {
+            }
+
+            fn super_constant(&mut self,
+                              constant: & $($mutability)? Constant<'tcx>,
+                              location: Location) {
+                let Constant {
+                    span,
+                    ty,
+                    user_ty,
+                    literal,
+                } = constant;
+
+                self.visit_span(span);
+                self.visit_ty(ty, TyContext::Location(location));
+                drop(user_ty); // no visit method for this
+                self.visit_const(literal, location);
             }
-            Operand::Constant(ref constant) => {
-                self.visit_constant(constant);
+
+            fn super_def_id(&mut self, _def_id: & $($mutability)? DefId) {
+            }
+
+            fn super_span(&mut self, _span: & $($mutability)? Span) {
+            }
+
+            fn super_source_info(&mut self, source_info: & $($mutability)? SourceInfo) {
+                let SourceInfo {
+                    span,
+                    scope,
+                } = source_info;
+
+                self.visit_span(span);
+                self.visit_source_scope(scope);
+            }
+
+            fn super_user_type_projection(
+                &mut self,
+                _ty: & $($mutability)? UserTypeProjection<'tcx>,
+            ) {
+            }
+
+            fn super_user_type_annotation(
+                &mut self,
+                _index: UserTypeAnnotationIndex,
+                ty: & $($mutability)? CanonicalUserTypeAnnotation<'tcx>,
+            ) {
+                self.visit_span(& $($mutability)? ty.span);
+                self.visit_ty(& $($mutability)? ty.inferred_ty, TyContext::UserTy(ty.span));
+            }
+
+            fn super_ty(&mut self, _ty: & $($mutability)? Ty<'tcx>) {
             }
-        }
-    }
 
-    fn super_lvalue(&mut self, lvalue: &Lvalue<'tcx>, _context: LvalueContext) {
-        match *lvalue {
-            Lvalue::Var(_) |
-            Lvalue::Temp(_) |
-            Lvalue::Arg(_) |
-            Lvalue::Static(_) |
-            Lvalue::ReturnPointer => {
+            fn super_region(&mut self, _region: & $($mutability)? ty::Region<'tcx>) {
             }
-            Lvalue::Projection(ref proj) => {
-                self.visit_lvalue(&proj.base, LvalueContext::Projection);
+
+            fn super_const(&mut self, _const: & $($mutability)? &'tcx ty::LazyConst<'tcx>) {
+            }
+
+            fn super_substs(&mut self, _substs: & $($mutability)? &'tcx Substs<'tcx>) {
+            }
+
+            fn super_generator_substs(&mut self,
+                                      _substs: & $($mutability)? GeneratorSubsts<'tcx>) {
+            }
+
+            fn super_closure_substs(&mut self,
+                                    _substs: & $($mutability)? ClosureSubsts<'tcx>) {
+            }
+
+            // Convenience methods
+
+            fn visit_location(&mut self, mir: & $($mutability)? Mir<'tcx>, location: Location) {
+                let basic_block = & $($mutability)? mir[location.block];
+                if basic_block.statements.len() == location.statement_index {
+                    if let Some(ref $($mutability)? terminator) = basic_block.terminator {
+                        self.visit_terminator(location.block, terminator, location)
+                    }
+                } else {
+                    let statement = & $($mutability)?
+                        basic_block.statements[location.statement_index];
+                    self.visit_statement(location.block, statement, location)
+                }
             }
         }
     }
+}
 
-    fn super_branch(&mut self, _source: BasicBlock, _target: BasicBlock) {
+make_mir_visitor!(Visitor,);
+make_mir_visitor!(MutVisitor,mut);
+
+pub trait MirVisitable<'tcx> {
+    fn apply(&self, location: Location, visitor: &mut dyn Visitor<'tcx>);
+}
+
+impl<'tcx> MirVisitable<'tcx> for Statement<'tcx> {
+    fn apply(&self, location: Location, visitor: &mut dyn Visitor<'tcx>)
+    {
+        visitor.visit_statement(location.block, self, location)
     }
+}
 
-    fn super_constant(&mut self, _constant: &Constant<'tcx>) {
+impl<'tcx> MirVisitable<'tcx> for Terminator<'tcx> {
+    fn apply(&self, location: Location, visitor: &mut dyn Visitor<'tcx>)
+    {
+        visitor.visit_terminator(location.block, self, location)
     }
 }
 
-#[derive(Copy, Clone, Debug)]
-pub enum LvalueContext {
-    // Appears as LHS of an assignment or as dest of a call
-    Store,
+impl<'tcx> MirVisitable<'tcx> for Option<Terminator<'tcx>> {
+    fn apply(&self, location: Location, visitor: &mut dyn Visitor<'tcx>)
+    {
+        visitor.visit_terminator(location.block, self.as_ref().unwrap(), location)
+    }
+}
 
-    // Being dropped
-    Drop,
+/// Extra information passed to `visit_ty` and friends to give context
+/// about where the type etc appears.
+#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
+pub enum TyContext {
+    LocalDecl {
+        /// The index of the local variable we are visiting.
+        local: Local,
 
-    // Being inspected in some way, like loading a len
-    Inspect,
+        /// The source location where this local variable was declared.
+        source_info: SourceInfo,
+    },
+
+    /// The inferred type of a user type annotation.
+    UserTy(Span),
+
+    /// The return type of the function.
+    ReturnTy(SourceInfo),
 
-    // Being borrowed
-    Borrow { region: Region, kind: BorrowKind },
+    YieldTy(SourceInfo),
 
-    // Being sliced -- this should be same as being borrowed, probably
-    Slice { from_start: usize, from_end: usize },
+    /// A type found at some location.
+    Location(Location),
+}
 
-    // Used as base for another lvalue, e.g. `x` in `x.y`
+#[derive(Copy, Clone, Debug, PartialEq, Eq)]
+pub enum NonMutatingUseContext<'tcx> {
+    /// Being inspected in some way, like loading a len.
+    Inspect,
+    /// Consumed as part of an operand.
+    Copy,
+    /// Consumed as part of an operand.
+    Move,
+    /// Shared borrow.
+    SharedBorrow(Region<'tcx>),
+    /// Shallow borrow.
+    ShallowBorrow(Region<'tcx>),
+    /// Unique borrow.
+    UniqueBorrow(Region<'tcx>),
+    /// Used as base for another place, e.g., `x` in `x.y`. Will not mutate the place.
+    /// For example, the projection `x.y` is not marked as a mutation in these cases:
+    ///
+    ///     z = x.y;
+    ///     f(&x.y);
+    ///
     Projection,
+}
 
-    // Consumed as part of an operand
-    Consume,
+#[derive(Copy, Clone, Debug, PartialEq, Eq)]
+pub enum MutatingUseContext<'tcx> {
+    /// Appears as LHS of an assignment.
+    Store,
+    /// Can often be treated as a `Store`, but needs to be separate because
+    /// ASM is allowed to read outputs as well, so a `Store`-`AsmOutput` sequence
+    /// cannot be simplified the way a `Store`-`Store` can be.
+    AsmOutput,
+    /// Destination of a call.
+    Call,
+    /// Being dropped.
+    Drop,
+    /// Mutable borrow.
+    Borrow(Region<'tcx>),
+    /// Used as base for another place, e.g., `x` in `x.y`. Could potentially mutate the place.
+    /// For example, the projection `x.y` is marked as a mutation in these cases:
+    ///
+    ///     x.y = ...;
+    ///     f(&mut x.y);
+    ///
+    Projection,
+    /// Retagging, a "Stacked Borrows" shadow state operation
+    Retag,
+}
+
+#[derive(Copy, Clone, Debug, PartialEq, Eq)]
+pub enum NonUseContext {
+    /// Starting a storage live range.
+    StorageLive,
+    /// Ending a storage live range.
+    StorageDead,
+    /// User type annotation assertions for NLL.
+    AscribeUserTy,
+}
+
+#[derive(Copy, Clone, Debug, PartialEq, Eq)]
+pub enum PlaceContext<'tcx> {
+    NonMutatingUse(NonMutatingUseContext<'tcx>),
+    MutatingUse(MutatingUseContext<'tcx>),
+    NonUse(NonUseContext),
+}
+
+impl<'tcx> PlaceContext<'tcx> {
+    /// Returns `true` if this place context represents a drop.
+    pub fn is_drop(&self) -> bool {
+        match *self {
+            PlaceContext::MutatingUse(MutatingUseContext::Drop) => true,
+            _ => false,
+        }
+    }
+
+    /// Returns `true` if this place context represents a borrow.
+    pub fn is_borrow(&self) -> bool {
+        match *self {
+            PlaceContext::NonMutatingUse(NonMutatingUseContext::SharedBorrow(..)) |
+            PlaceContext::NonMutatingUse(NonMutatingUseContext::ShallowBorrow(..)) |
+            PlaceContext::NonMutatingUse(NonMutatingUseContext::UniqueBorrow(..)) |
+            PlaceContext::MutatingUse(MutatingUseContext::Borrow(..)) => true,
+            _ => false,
+        }
+    }
+
+    /// Returns `true` if this place context represents a storage live or storage dead marker.
+    pub fn is_storage_marker(&self) -> bool {
+        match *self {
+            PlaceContext::NonUse(NonUseContext::StorageLive) |
+            PlaceContext::NonUse(NonUseContext::StorageDead) => true,
+            _ => false,
+        }
+    }
+
+    /// Returns `true` if this place context represents a storage live marker.
+    pub fn is_storage_live_marker(&self) -> bool {
+        match *self {
+            PlaceContext::NonUse(NonUseContext::StorageLive) => true,
+            _ => false,
+        }
+    }
+
+    /// Returns `true` if this place context represents a storage dead marker.
+    pub fn is_storage_dead_marker(&self) -> bool {
+        match *self {
+            PlaceContext::NonUse(NonUseContext::StorageDead) => true,
+            _ => false,
+        }
+    }
+
+    /// Returns `true` if this place context represents a use that potentially changes the value.
+    pub fn is_mutating_use(&self) -> bool {
+        match *self {
+            PlaceContext::MutatingUse(..) => true,
+            _ => false,
+        }
+    }
+
+    /// Returns `true` if this place context represents a use that does not change the value.
+    pub fn is_nonmutating_use(&self) -> bool {
+        match *self {
+            PlaceContext::NonMutatingUse(..) => true,
+            _ => false,
+        }
+    }
+
+    /// Returns `true` if this place context represents a use.
+    pub fn is_use(&self) -> bool {
+        match *self {
+            PlaceContext::NonUse(..) => false,
+            _ => true,
+        }
+    }
+
+    /// Returns `true` if this place context represents an assignment statement.
+    pub fn is_place_assignment(&self) -> bool {
+        match *self {
+            PlaceContext::MutatingUse(MutatingUseContext::Store) |
+            PlaceContext::MutatingUse(MutatingUseContext::Call) |
+            PlaceContext::MutatingUse(MutatingUseContext::AsmOutput) => true,
+            _ => false,
+        }
+    }
 }