]> git.proxmox.com Git - rustc.git/blobdiff - src/librustc_mir/util/def_use.rs
New upstream version 1.45.0+dfsg1
[rustc.git] / src / librustc_mir / util / def_use.rs
index cf98755eb6d90617a007746479bd134257bfcb42..b4448ead8eb8124f4d304f1653e76514fe1ecb7a 100644 (file)
@@ -1,11 +1,9 @@
 //! Def-use analysis.
 
-use rustc::mir::{
-    Body, BodyAndCache, Local, Location, PlaceElem, ReadOnlyBodyAndCache, VarDebugInfo,
-};
-use rustc::mir::visit::{PlaceContext, MutVisitor, Visitor};
-use rustc::ty::TyCtxt;
 use rustc_index::vec::IndexVec;
+use rustc_middle::mir::visit::{MutVisitor, PlaceContext, Visitor};
+use rustc_middle::mir::{Body, Local, Location, VarDebugInfo};
+use rustc_middle::ty::TyCtxt;
 use std::mem;
 
 pub struct DefUseAnalysis {
@@ -27,12 +25,10 @@ pub struct Use {
 
 impl DefUseAnalysis {
     pub fn new(body: &Body<'_>) -> DefUseAnalysis {
-        DefUseAnalysis {
-            info: IndexVec::from_elem_n(Info::new(), body.local_decls.len()),
-        }
+        DefUseAnalysis { info: IndexVec::from_elem_n(Info::new(), body.local_decls.len()) }
     }
 
-    pub fn analyze(&mut self, body: ReadOnlyBodyAndCache<'_, '_>) {
+    pub fn analyze(&mut self, body: &Body<'_>) {
         self.clear();
 
         let mut finder = DefUseFinder {
@@ -40,7 +36,7 @@ impl DefUseAnalysis {
             var_debug_info_index: 0,
             in_var_debug_info: false,
         };
-        finder.visit_body(body);
+        finder.visit_body(&body);
         self.info = finder.info
     }
 
@@ -57,7 +53,7 @@ impl DefUseAnalysis {
     fn mutate_defs_and_uses(
         &self,
         local: Local,
-        body: &mut BodyAndCache<'tcx>,
+        body: &mut Body<'tcx>,
         new_local: Local,
         tcx: TyCtxt<'tcx>,
     ) {
@@ -73,11 +69,13 @@ impl DefUseAnalysis {
     }
 
     // FIXME(pcwalton): this should update the def-use chains.
-    pub fn replace_all_defs_and_uses_with(&self,
-                                          local: Local,
-                                          body: &mut BodyAndCache<'tcx>,
-                                          new_local: Local,
-                                          tcx: TyCtxt<'tcx>) {
+    pub fn replace_all_defs_and_uses_with(
+        &self,
+        local: Local,
+        body: &mut Body<'tcx>,
+        new_local: Local,
+        tcx: TyCtxt<'tcx>,
+    ) {
         self.mutate_defs_and_uses(local, body, new_local, tcx)
     }
 }
@@ -89,18 +87,12 @@ struct DefUseFinder {
 }
 
 impl Visitor<'_> for DefUseFinder {
-    fn visit_local(&mut self,
-                   &local: &Local,
-                   context: PlaceContext,
-                   location: Location) {
+    fn visit_local(&mut self, &local: &Local, context: PlaceContext, location: Location) {
         let info = &mut self.info[local];
         if self.in_var_debug_info {
             info.var_debug_info_indices.push(self.var_debug_info_index);
         } else {
-            info.defs_and_uses.push(Use {
-                context,
-                location,
-            });
+            info.defs_and_uses.push(Use { context, location });
         }
     }
     fn visit_var_debug_info(&mut self, var_debug_info: &VarDebugInfo<'tcx>) {
@@ -114,10 +106,7 @@ impl Visitor<'_> for DefUseFinder {
 
 impl Info {
     fn new() -> Info {
-        Info {
-            defs_and_uses: vec![],
-            var_debug_info_indices: vec![],
-        }
+        Info { defs_and_uses: vec![], var_debug_info_indices: vec![] }
     }
 
     fn clear(&mut self) {
@@ -133,18 +122,14 @@ impl Info {
         self.defs_not_including_drop().count()
     }
 
-    pub fn defs_not_including_drop(
-        &self,
-    ) -> impl Iterator<Item=&Use> {
-        self.defs_and_uses.iter().filter(|place_use| {
-            place_use.context.is_mutating_use() && !place_use.context.is_drop()
-        })
+    pub fn defs_not_including_drop(&self) -> impl Iterator<Item = &Use> {
+        self.defs_and_uses
+            .iter()
+            .filter(|place_use| place_use.context.is_mutating_use() && !place_use.context.is_drop())
     }
 
     pub fn use_count(&self) -> usize {
-        self.defs_and_uses.iter().filter(|place_use| {
-            place_use.context.is_nonmutating_use()
-        }).count()
+        self.defs_and_uses.iter().filter(|place_use| place_use.context.is_nonmutating_use()).count()
     }
 }
 
@@ -155,11 +140,7 @@ struct MutateUseVisitor<'tcx> {
 }
 
 impl MutateUseVisitor<'tcx> {
-    fn new(
-        query: Local,
-        new_local: Local,
-        tcx: TyCtxt<'tcx>,
-    ) -> MutateUseVisitor<'tcx> {
+    fn new(query: Local, new_local: Local, tcx: TyCtxt<'tcx>) -> MutateUseVisitor<'tcx> {
         MutateUseVisitor { query, new_local, tcx }
     }
 }
@@ -169,24 +150,9 @@ impl MutVisitor<'tcx> for MutateUseVisitor<'tcx> {
         self.tcx
     }
 
-    fn visit_local(&mut self,
-                    local: &mut Local,
-                    _context: PlaceContext,
-                    _location: Location) {
+    fn visit_local(&mut self, local: &mut Local, _context: PlaceContext, _location: Location) {
         if *local == self.query {
             *local = self.new_local;
         }
     }
-
-    fn process_projection_elem(
-        &mut self,
-        elem: &PlaceElem<'tcx>,
-    ) -> Option<PlaceElem<'tcx>> {
-        match elem {
-            PlaceElem::Index(local) if *local == self.query => {
-                Some(PlaceElem::Index(self.new_local))
-            }
-            _ => None,
-        }
-    }
 }