Skip to content

Commit b09cf12

Browse files
committed
astencode: convert code to use TyDecoder directly
1 parent 38e6b57 commit b09cf12

File tree

2 files changed

+54
-81
lines changed

2 files changed

+54
-81
lines changed

src/librustc/metadata/tydecode.rs

+16-7
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use middle::subst;
2323
use middle::subst::VecPerParamSpace;
2424
use middle::ty::{self, ToPredicate, Ty, HasTypeFlags};
2525

26+
use rbml;
2627
use std::str;
2728
use syntax::abi;
2829
use syntax::ast;
@@ -166,6 +167,14 @@ pub struct TyDecoder<'a, 'tcx: 'a> {
166167
}
167168

168169
impl<'a,'tcx> TyDecoder<'a,'tcx> {
170+
pub fn with_doc(tcx: &'a ty::ctxt<'tcx>,
171+
crate_num: ast::CrateNum,
172+
doc: rbml::Doc<'a>,
173+
conv: DefIdConvert<'a>)
174+
-> TyDecoder<'a,'tcx> {
175+
TyDecoder::new(doc.data, crate_num, doc.start, tcx, conv)
176+
}
177+
169178
pub fn new(data: &'a [u8],
170179
crate_num: ast::CrateNum,
171180
pos: usize,
@@ -244,7 +253,7 @@ impl<'a,'tcx> TyDecoder<'a,'tcx> {
244253
r
245254
}
246255

247-
fn parse_substs(&mut self) -> subst::Substs<'tcx> {
256+
pub fn parse_substs(&mut self) -> subst::Substs<'tcx> {
248257
let regions = self.parse_region_substs();
249258
let types = self.parse_vec_per_param_space(|this| this.parse_ty());
250259
subst::Substs { types: types, regions: regions }
@@ -394,13 +403,13 @@ impl<'a,'tcx> TyDecoder<'a,'tcx> {
394403
result
395404
}
396405

397-
fn parse_trait_ref(&mut self) -> ty::TraitRef<'tcx> {
406+
pub fn parse_trait_ref(&mut self) -> ty::TraitRef<'tcx> {
398407
let def = self.parse_def(NominalType);
399408
let substs = self.tcx.mk_substs(self.parse_substs());
400409
ty::TraitRef {def_id: def, substs: substs}
401410
}
402411

403-
fn parse_ty(&mut self) -> Ty<'tcx> {
412+
pub fn parse_ty(&mut self) -> Ty<'tcx> {
404413
let tcx = self.tcx;
405414
match self.next() {
406415
'b' => return tcx.types.bool,
@@ -609,7 +618,7 @@ impl<'a,'tcx> TyDecoder<'a,'tcx> {
609618
abi::lookup(&abi_str[..]).expect(abi_str)
610619
}
611620

612-
fn parse_closure_ty(&mut self) -> ty::ClosureTy<'tcx> {
621+
pub fn parse_closure_ty(&mut self) -> ty::ClosureTy<'tcx> {
613622
let unsafety = parse_unsafety(self.next());
614623
let sig = self.parse_sig();
615624
let abi = self.parse_abi_set();
@@ -655,7 +664,7 @@ impl<'a,'tcx> TyDecoder<'a,'tcx> {
655664
variadic: variadic})
656665
}
657666

658-
fn parse_predicate(&mut self) -> ty::Predicate<'tcx> {
667+
pub fn parse_predicate(&mut self) -> ty::Predicate<'tcx> {
659668
match self.next() {
660669
't' => ty::Binder(self.parse_trait_ref()).to_predicate(),
661670
'e' => ty::Binder(ty::EquatePredicate(self.parse_ty(),
@@ -685,7 +694,7 @@ impl<'a,'tcx> TyDecoder<'a,'tcx> {
685694
}
686695
}
687696

688-
fn parse_type_param_def(&mut self) -> ty::TypeParameterDef<'tcx> {
697+
pub fn parse_type_param_def(&mut self) -> ty::TypeParameterDef<'tcx> {
689698
let name = self.parse_name(':');
690699
let def_id = self.parse_def(NominalType);
691700
let space = self.parse_param_space();
@@ -719,7 +728,7 @@ impl<'a,'tcx> TyDecoder<'a,'tcx> {
719728
}
720729
}
721730

722-
fn parse_existential_bounds(&mut self) -> ty::ExistentialBounds<'tcx> {
731+
pub fn parse_existential_bounds(&mut self) -> ty::ExistentialBounds<'tcx> {
723732
let builtin_bounds = self.parse_builtin_bounds();
724733
let region_bound = self.parse_region();
725734
let mut projection_bounds = Vec::new();

src/librustc/middle/astencode.rs

+38-74
Original file line numberDiff line numberDiff line change
@@ -1075,6 +1075,10 @@ impl<'a> doc_decoder_helpers for rbml::Doc<'a> {
10751075
}
10761076

10771077
trait rbml_decoder_decoder_helpers<'tcx> {
1078+
fn read_ty_encoded<'a, 'b, F, R>(&mut self, dcx: &DecodeContext<'a, 'b, 'tcx>,
1079+
f: F) -> R
1080+
where F: for<'x> FnOnce(&mut tydecode::TyDecoder<'x, 'tcx>) -> R;
1081+
10781082
fn read_ty<'a, 'b>(&mut self, dcx: &DecodeContext<'a, 'b, 'tcx>) -> Ty<'tcx>;
10791083
fn read_tys<'a, 'b>(&mut self, dcx: &DecodeContext<'a, 'b, 'tcx>) -> Vec<Ty<'tcx>>;
10801084
fn read_trait_ref<'a, 'b>(&mut self, dcx: &DecodeContext<'a, 'b, 'tcx>)
@@ -1123,14 +1127,14 @@ trait rbml_decoder_decoder_helpers<'tcx> {
11231127

11241128
impl<'a, 'tcx> rbml_decoder_decoder_helpers<'tcx> for reader::Decoder<'a> {
11251129
fn read_ty_nodcx(&mut self,
1126-
tcx: &ty::ctxt<'tcx>, cdata: &cstore::crate_metadata) -> Ty<'tcx> {
1130+
tcx: &ty::ctxt<'tcx>,
1131+
cdata: &cstore::crate_metadata)
1132+
-> Ty<'tcx> {
11271133
self.read_opaque(|_, doc| {
1128-
Ok(tydecode::parse_ty_data(
1129-
doc.data,
1130-
cdata.cnum,
1131-
doc.start,
1132-
tcx,
1133-
|_, id| decoder::translate_def_id(cdata, id)))
1134+
Ok(
1135+
tydecode::TyDecoder::with_doc(tcx, cdata.cnum, doc,
1136+
&mut |_, id| decoder::translate_def_id(cdata, id))
1137+
.parse_ty())
11341138
}).unwrap()
11351139
}
11361140

@@ -1149,32 +1153,22 @@ impl<'a, 'tcx> rbml_decoder_decoder_helpers<'tcx> for reader::Decoder<'a> {
11491153
-> subst::Substs<'tcx>
11501154
{
11511155
self.read_opaque(|_, doc| {
1152-
Ok(tydecode::parse_substs_data(
1153-
doc.data,
1154-
cdata.cnum,
1155-
doc.start,
1156-
tcx,
1157-
|_, id| decoder::translate_def_id(cdata, id)))
1156+
Ok(
1157+
tydecode::TyDecoder::with_doc(tcx, cdata.cnum, doc,
1158+
&mut |_, id| decoder::translate_def_id(cdata, id))
1159+
.parse_substs())
11581160
}).unwrap()
11591161
}
11601162

1161-
fn read_ty<'b, 'c>(&mut self, dcx: &DecodeContext<'b, 'c, 'tcx>) -> Ty<'tcx> {
1162-
// Note: regions types embed local node ids. In principle, we
1163-
// should translate these node ids into the new decode
1164-
// context. However, we do not bother, because region types
1165-
// are not used during trans.
1166-
1163+
fn read_ty_encoded<'b, 'c, F, R>(&mut self, dcx: &DecodeContext<'b, 'c, 'tcx>, op: F) -> R
1164+
where F: for<'x> FnOnce(&mut tydecode::TyDecoder<'x,'tcx>) -> R
1165+
{
11671166
return self.read_opaque(|this, doc| {
1168-
debug!("read_ty({})", type_string(doc));
1169-
1170-
let ty = tydecode::parse_ty_data(
1171-
doc.data,
1172-
dcx.cdata.cnum,
1173-
doc.start,
1174-
dcx.tcx,
1175-
|s, a| this.convert_def_id(dcx, s, a));
1176-
1177-
Ok(ty)
1167+
debug!("read_ty_encoded({})", type_string(doc));
1168+
Ok(op(
1169+
&mut tydecode::TyDecoder::with_doc(
1170+
dcx.tcx, dcx.cdata.cnum, doc,
1171+
&mut |s, a| this.convert_def_id(dcx, s, a))))
11781172
}).unwrap();
11791173

11801174
fn type_string(doc: rbml::Doc) -> String {
@@ -1186,56 +1180,39 @@ impl<'a, 'tcx> rbml_decoder_decoder_helpers<'tcx> for reader::Decoder<'a> {
11861180
}
11871181
}
11881182

1183+
fn read_ty<'b, 'c>(&mut self, dcx: &DecodeContext<'b, 'c, 'tcx>) -> Ty<'tcx> {
1184+
// Note: regions types embed local node ids. In principle, we
1185+
// should translate these node ids into the new decode
1186+
// context. However, we do not bother, because region types
1187+
// are not used during trans.
1188+
1189+
return self.read_ty_encoded(dcx, |decoder| decoder.parse_ty());
1190+
}
1191+
11891192
fn read_tys<'b, 'c>(&mut self, dcx: &DecodeContext<'b, 'c, 'tcx>)
11901193
-> Vec<Ty<'tcx>> {
11911194
self.read_to_vec(|this| Ok(this.read_ty(dcx))).unwrap().into_iter().collect()
11921195
}
11931196

11941197
fn read_trait_ref<'b, 'c>(&mut self, dcx: &DecodeContext<'b, 'c, 'tcx>)
11951198
-> ty::TraitRef<'tcx> {
1196-
self.read_opaque(|this, doc| {
1197-
let ty = tydecode::parse_trait_ref_data(
1198-
doc.data,
1199-
dcx.cdata.cnum,
1200-
doc.start,
1201-
dcx.tcx,
1202-
|s, a| this.convert_def_id(dcx, s, a));
1203-
Ok(ty)
1204-
}).unwrap()
1199+
self.read_ty_encoded(dcx, |decoder| decoder.parse_trait_ref())
12051200
}
12061201

12071202
fn read_poly_trait_ref<'b, 'c>(&mut self, dcx: &DecodeContext<'b, 'c, 'tcx>)
12081203
-> ty::PolyTraitRef<'tcx> {
1209-
ty::Binder(self.read_opaque(|this, doc| {
1210-
let ty = tydecode::parse_trait_ref_data(
1211-
doc.data,
1212-
dcx.cdata.cnum,
1213-
doc.start,
1214-
dcx.tcx,
1215-
|s, a| this.convert_def_id(dcx, s, a));
1216-
Ok(ty)
1217-
}).unwrap())
1204+
ty::Binder(self.read_ty_encoded(dcx, |decoder| decoder.parse_trait_ref()))
12181205
}
12191206

12201207
fn read_type_param_def<'b, 'c>(&mut self, dcx: &DecodeContext<'b, 'c, 'tcx>)
12211208
-> ty::TypeParameterDef<'tcx> {
1222-
self.read_opaque(|this, doc| {
1223-
Ok(tydecode::parse_type_param_def_data(
1224-
doc.data,
1225-
doc.start,
1226-
dcx.cdata.cnum,
1227-
dcx.tcx,
1228-
|s, a| this.convert_def_id(dcx, s, a)))
1229-
}).unwrap()
1209+
self.read_ty_encoded(dcx, |decoder| decoder.parse_type_param_def())
12301210
}
12311211

12321212
fn read_predicate<'b, 'c>(&mut self, dcx: &DecodeContext<'b, 'c, 'tcx>)
12331213
-> ty::Predicate<'tcx>
12341214
{
1235-
self.read_opaque(|this, doc| {
1236-
Ok(tydecode::parse_predicate_data(doc.data, doc.start, dcx.cdata.cnum, dcx.tcx,
1237-
|s, a| this.convert_def_id(dcx, s, a)))
1238-
}).unwrap()
1215+
self.read_ty_encoded(dcx, |decoder| decoder.parse_predicate())
12391216
}
12401217

12411218
fn read_type_scheme<'b, 'c>(&mut self, dcx: &DecodeContext<'b, 'c, 'tcx>)
@@ -1269,13 +1246,7 @@ impl<'a, 'tcx> rbml_decoder_decoder_helpers<'tcx> for reader::Decoder<'a> {
12691246
fn read_existential_bounds<'b, 'c>(&mut self, dcx: &DecodeContext<'b, 'c, 'tcx>)
12701247
-> ty::ExistentialBounds<'tcx>
12711248
{
1272-
self.read_opaque(|this, doc| {
1273-
Ok(tydecode::parse_existential_bounds_data(doc.data,
1274-
dcx.cdata.cnum,
1275-
doc.start,
1276-
dcx.tcx,
1277-
|s, a| this.convert_def_id(dcx, s, a)))
1278-
}).unwrap()
1249+
self.read_ty_encoded(dcx, |decoder| decoder.parse_existential_bounds())
12791250
}
12801251

12811252
fn read_substs<'b, 'c>(&mut self, dcx: &DecodeContext<'b, 'c, 'tcx>)
@@ -1380,14 +1351,7 @@ impl<'a, 'tcx> rbml_decoder_decoder_helpers<'tcx> for reader::Decoder<'a> {
13801351
fn read_closure_ty<'b, 'c>(&mut self, dcx: &DecodeContext<'b, 'c, 'tcx>)
13811352
-> ty::ClosureTy<'tcx>
13821353
{
1383-
self.read_opaque(|this, doc| {
1384-
Ok(tydecode::parse_ty_closure_data(
1385-
doc.data,
1386-
dcx.cdata.cnum,
1387-
doc.start,
1388-
dcx.tcx,
1389-
|s, a| this.convert_def_id(dcx, s, a)))
1390-
}).unwrap()
1354+
self.read_ty_encoded(dcx, |decoder| decoder.parse_closure_ty())
13911355
}
13921356

13931357
/// Converts a def-id that appears in a type. The correct

0 commit comments

Comments
 (0)