]> git.lizzy.rs Git - rust.git/commitdiff
astencode: convert code to use TyDecoder directly
authorNiko Matsakis <niko@alum.mit.edu>
Fri, 14 Aug 2015 09:58:10 +0000 (05:58 -0400)
committerNiko Matsakis <niko@alum.mit.edu>
Sat, 15 Aug 2015 09:01:47 +0000 (05:01 -0400)
src/librustc/metadata/tydecode.rs
src/librustc/middle/astencode.rs

index a24bf76ca7658571a4bb9f711af1e6e17e3b47c2..52370f652a2fb162a71772d133e70885677523a9 100644 (file)
@@ -23,6 +23,7 @@
 use middle::subst::VecPerParamSpace;
 use middle::ty::{self, ToPredicate, Ty, HasTypeFlags};
 
+use rbml;
 use std::str;
 use syntax::abi;
 use syntax::ast;
@@ -166,6 +167,14 @@ pub struct TyDecoder<'a, 'tcx: 'a> {
 }
 
 impl<'a,'tcx> TyDecoder<'a,'tcx> {
+    pub fn with_doc(tcx: &'a ty::ctxt<'tcx>,
+                    crate_num: ast::CrateNum,
+                    doc: rbml::Doc<'a>,
+                    conv: DefIdConvert<'a>)
+                    -> TyDecoder<'a,'tcx> {
+        TyDecoder::new(doc.data, crate_num, doc.start, tcx, conv)
+    }
+
     pub fn new(data: &'a [u8],
                crate_num: ast::CrateNum,
                pos: usize,
@@ -244,7 +253,7 @@ fn parse_vec_per_param_space<T, F>(&mut self, mut f: F) -> VecPerParamSpace<T> w
         r
     }
 
-    fn parse_substs(&mut self) -> subst::Substs<'tcx> {
+    pub fn parse_substs(&mut self) -> subst::Substs<'tcx> {
         let regions = self.parse_region_substs();
         let types = self.parse_vec_per_param_space(|this| this.parse_ty());
         subst::Substs { types: types, regions: regions }
@@ -394,13 +403,13 @@ fn parse_str(&mut self, term: char) -> String {
         result
     }
 
-    fn parse_trait_ref(&mut self) -> ty::TraitRef<'tcx> {
+    pub fn parse_trait_ref(&mut self) -> ty::TraitRef<'tcx> {
         let def = self.parse_def(NominalType);
         let substs = self.tcx.mk_substs(self.parse_substs());
         ty::TraitRef {def_id: def, substs: substs}
     }
 
-    fn parse_ty(&mut self) -> Ty<'tcx> {
+    pub fn parse_ty(&mut self) -> Ty<'tcx> {
         let tcx = self.tcx;
         match self.next() {
             'b' => return tcx.types.bool,
@@ -609,7 +618,7 @@ fn parse_abi_set(&mut self) -> abi::Abi {
         abi::lookup(&abi_str[..]).expect(abi_str)
     }
 
-    fn parse_closure_ty(&mut self) -> ty::ClosureTy<'tcx> {
+    pub fn parse_closure_ty(&mut self) -> ty::ClosureTy<'tcx> {
         let unsafety = parse_unsafety(self.next());
         let sig = self.parse_sig();
         let abi = self.parse_abi_set();
@@ -655,7 +664,7 @@ fn parse_sig(&mut self) -> ty::PolyFnSig<'tcx> {
                               variadic: variadic})
     }
 
-    fn parse_predicate(&mut self) -> ty::Predicate<'tcx> {
+    pub fn parse_predicate(&mut self) -> ty::Predicate<'tcx> {
         match self.next() {
             't' => ty::Binder(self.parse_trait_ref()).to_predicate(),
             'e' => ty::Binder(ty::EquatePredicate(self.parse_ty(),
@@ -685,7 +694,7 @@ fn parse_projection_predicate(&mut self) -> ty::ProjectionPredicate<'tcx> {
         }
     }
 
-    fn parse_type_param_def(&mut self) -> ty::TypeParameterDef<'tcx> {
+    pub fn parse_type_param_def(&mut self) -> ty::TypeParameterDef<'tcx> {
         let name = self.parse_name(':');
         let def_id = self.parse_def(NominalType);
         let space = self.parse_param_space();
@@ -719,7 +728,7 @@ fn parse_object_lifetime_default(&mut self) -> ty::ObjectLifetimeDefault {
         }
     }
 
-    fn parse_existential_bounds(&mut self) -> ty::ExistentialBounds<'tcx> {
+    pub fn parse_existential_bounds(&mut self) -> ty::ExistentialBounds<'tcx> {
         let builtin_bounds = self.parse_builtin_bounds();
         let region_bound = self.parse_region();
         let mut projection_bounds = Vec::new();
index a07b849b400aa3892b621db0456298a810f2ff0d..e544d8d474979ac24bef90a8ce592736f9c1cdd3 100644 (file)
@@ -1075,6 +1075,10 @@ fn opt_child(&self, tag: c::astencode_tag) -> Option<rbml::Doc<'a>> {
 }
 
 trait rbml_decoder_decoder_helpers<'tcx> {
+    fn read_ty_encoded<'a, 'b, F, R>(&mut self, dcx: &DecodeContext<'a, 'b, 'tcx>,
+                                     f: F) -> R
+        where F: for<'x> FnOnce(&mut tydecode::TyDecoder<'x, 'tcx>) -> R;
+
     fn read_ty<'a, 'b>(&mut self, dcx: &DecodeContext<'a, 'b, 'tcx>) -> Ty<'tcx>;
     fn read_tys<'a, 'b>(&mut self, dcx: &DecodeContext<'a, 'b, 'tcx>) -> Vec<Ty<'tcx>>;
     fn read_trait_ref<'a, 'b>(&mut self, dcx: &DecodeContext<'a, 'b, 'tcx>)
@@ -1123,14 +1127,14 @@ fn read_substs_nodcx(&mut self, tcx: &ty::ctxt<'tcx>,
 
 impl<'a, 'tcx> rbml_decoder_decoder_helpers<'tcx> for reader::Decoder<'a> {
     fn read_ty_nodcx(&mut self,
-                     tcx: &ty::ctxt<'tcx>, cdata: &cstore::crate_metadata) -> Ty<'tcx> {
+                     tcx: &ty::ctxt<'tcx>,
+                     cdata: &cstore::crate_metadata)
+                     -> Ty<'tcx> {
         self.read_opaque(|_, doc| {
-            Ok(tydecode::parse_ty_data(
-                doc.data,
-                cdata.cnum,
-                doc.start,
-                tcx,
-                |_, id| decoder::translate_def_id(cdata, id)))
+            Ok(
+                tydecode::TyDecoder::with_doc(tcx, cdata.cnum, doc,
+                                              &mut |_, id| decoder::translate_def_id(cdata, id))
+                    .parse_ty())
         }).unwrap()
     }
 
@@ -1149,32 +1153,22 @@ fn read_substs_nodcx(&mut self,
                          -> subst::Substs<'tcx>
     {
         self.read_opaque(|_, doc| {
-            Ok(tydecode::parse_substs_data(
-                doc.data,
-                cdata.cnum,
-                doc.start,
-                tcx,
-                |_, id| decoder::translate_def_id(cdata, id)))
+            Ok(
+                tydecode::TyDecoder::with_doc(tcx, cdata.cnum, doc,
+                                              &mut |_, id| decoder::translate_def_id(cdata, id))
+                    .parse_substs())
         }).unwrap()
     }
 
-    fn read_ty<'b, 'c>(&mut self, dcx: &DecodeContext<'b, 'c, 'tcx>) -> Ty<'tcx> {
-        // Note: regions types embed local node ids.  In principle, we
-        // should translate these node ids into the new decode
-        // context.  However, we do not bother, because region types
-        // are not used during trans.
-
+    fn read_ty_encoded<'b, 'c, F, R>(&mut self, dcx: &DecodeContext<'b, 'c, 'tcx>, op: F) -> R
+        where F: for<'x> FnOnce(&mut tydecode::TyDecoder<'x,'tcx>) -> R
+    {
         return self.read_opaque(|this, doc| {
-            debug!("read_ty({})", type_string(doc));
-
-            let ty = tydecode::parse_ty_data(
-                doc.data,
-                dcx.cdata.cnum,
-                doc.start,
-                dcx.tcx,
-                |s, a| this.convert_def_id(dcx, s, a));
-
-            Ok(ty)
+            debug!("read_ty_encoded({})", type_string(doc));
+            Ok(op(
+                &mut tydecode::TyDecoder::with_doc(
+                    dcx.tcx, dcx.cdata.cnum, doc,
+                    &mut |s, a| this.convert_def_id(dcx, s, a))))
         }).unwrap();
 
         fn type_string(doc: rbml::Doc) -> String {
@@ -1186,6 +1180,15 @@ fn type_string(doc: rbml::Doc) -> String {
         }
     }
 
+    fn read_ty<'b, 'c>(&mut self, dcx: &DecodeContext<'b, 'c, 'tcx>) -> Ty<'tcx> {
+        // Note: regions types embed local node ids.  In principle, we
+        // should translate these node ids into the new decode
+        // context.  However, we do not bother, because region types
+        // are not used during trans.
+
+        return self.read_ty_encoded(dcx, |decoder| decoder.parse_ty());
+    }
+
     fn read_tys<'b, 'c>(&mut self, dcx: &DecodeContext<'b, 'c, 'tcx>)
                         -> Vec<Ty<'tcx>> {
         self.read_to_vec(|this| Ok(this.read_ty(dcx))).unwrap().into_iter().collect()
@@ -1193,49 +1196,23 @@ fn read_tys<'b, 'c>(&mut self, dcx: &DecodeContext<'b, 'c, 'tcx>)
 
     fn read_trait_ref<'b, 'c>(&mut self, dcx: &DecodeContext<'b, 'c, 'tcx>)
                               -> ty::TraitRef<'tcx> {
-        self.read_opaque(|this, doc| {
-            let ty = tydecode::parse_trait_ref_data(
-                doc.data,
-                dcx.cdata.cnum,
-                doc.start,
-                dcx.tcx,
-                |s, a| this.convert_def_id(dcx, s, a));
-            Ok(ty)
-        }).unwrap()
+        self.read_ty_encoded(dcx, |decoder| decoder.parse_trait_ref())
     }
 
     fn read_poly_trait_ref<'b, 'c>(&mut self, dcx: &DecodeContext<'b, 'c, 'tcx>)
                                    -> ty::PolyTraitRef<'tcx> {
-        ty::Binder(self.read_opaque(|this, doc| {
-            let ty = tydecode::parse_trait_ref_data(
-                doc.data,
-                dcx.cdata.cnum,
-                doc.start,
-                dcx.tcx,
-                |s, a| this.convert_def_id(dcx, s, a));
-            Ok(ty)
-        }).unwrap())
+        ty::Binder(self.read_ty_encoded(dcx, |decoder| decoder.parse_trait_ref()))
     }
 
     fn read_type_param_def<'b, 'c>(&mut self, dcx: &DecodeContext<'b, 'c, 'tcx>)
                                    -> ty::TypeParameterDef<'tcx> {
-        self.read_opaque(|this, doc| {
-            Ok(tydecode::parse_type_param_def_data(
-                doc.data,
-                doc.start,
-                dcx.cdata.cnum,
-                dcx.tcx,
-                |s, a| this.convert_def_id(dcx, s, a)))
-        }).unwrap()
+        self.read_ty_encoded(dcx, |decoder| decoder.parse_type_param_def())
     }
 
     fn read_predicate<'b, 'c>(&mut self, dcx: &DecodeContext<'b, 'c, 'tcx>)
                               -> ty::Predicate<'tcx>
     {
-        self.read_opaque(|this, doc| {
-            Ok(tydecode::parse_predicate_data(doc.data, doc.start, dcx.cdata.cnum, dcx.tcx,
-                                              |s, a| this.convert_def_id(dcx, s, a)))
-        }).unwrap()
+        self.read_ty_encoded(dcx, |decoder| decoder.parse_predicate())
     }
 
     fn read_type_scheme<'b, 'c>(&mut self, dcx: &DecodeContext<'b, 'c, 'tcx>)
@@ -1269,13 +1246,7 @@ fn read_type_scheme<'b, 'c>(&mut self, dcx: &DecodeContext<'b, 'c, 'tcx>)
     fn read_existential_bounds<'b, 'c>(&mut self, dcx: &DecodeContext<'b, 'c, 'tcx>)
                                        -> ty::ExistentialBounds<'tcx>
     {
-        self.read_opaque(|this, doc| {
-            Ok(tydecode::parse_existential_bounds_data(doc.data,
-                                                       dcx.cdata.cnum,
-                                                       doc.start,
-                                                       dcx.tcx,
-                                                       |s, a| this.convert_def_id(dcx, s, a)))
-        }).unwrap()
+        self.read_ty_encoded(dcx, |decoder| decoder.parse_existential_bounds())
     }
 
     fn read_substs<'b, 'c>(&mut self, dcx: &DecodeContext<'b, 'c, 'tcx>)
@@ -1380,14 +1351,7 @@ fn read_closure_kind<'b, 'c>(&mut self, _dcx: &DecodeContext<'b, 'c, 'tcx>)
     fn read_closure_ty<'b, 'c>(&mut self, dcx: &DecodeContext<'b, 'c, 'tcx>)
                                -> ty::ClosureTy<'tcx>
     {
-        self.read_opaque(|this, doc| {
-            Ok(tydecode::parse_ty_closure_data(
-                doc.data,
-                dcx.cdata.cnum,
-                doc.start,
-                dcx.tcx,
-                |s, a| this.convert_def_id(dcx, s, a)))
-        }).unwrap()
+        self.read_ty_encoded(dcx, |decoder| decoder.parse_closure_ty())
     }
 
     /// Converts a def-id that appears in a type.  The correct