1 // This module contains some shared code for encoding and decoding various
2 // things from the `ty` module, and in particular implements support for
3 // "shorthands" which allow to have pointers back into the already encoded
4 // stream instead of re-encoding the same thing twice.
6 // The functionality in here is shared between persisting to crate metadata and
7 // persisting to incr. comp. caches.
9 use crate::hir::def_id::{DefId, CrateNum};
10 use crate::infer::canonical::{CanonicalVarInfo, CanonicalVarInfos};
11 use rustc_data_structures::fx::FxHashMap;
12 use crate::rustc_serialize::{Decodable, Decoder, Encoder, Encodable, opaque};
15 use crate::ty::{self, Ty, TyCtxt};
16 use crate::ty::subst::SubstsRef;
17 use crate::mir::interpret::Allocation;
19 /// The shorthand encoding uses an enum's variant index `usize`
20 /// and is offset by this value so it never matches a real variant.
21 /// This offset is also chosen so that the first byte is never < 0x80.
22 pub const SHORTHAND_OFFSET: usize = 0x80;
24 pub trait EncodableWithShorthand: Clone + Eq + Hash {
25 type Variant: Encodable;
26 fn variant(&self) -> &Self::Variant;
29 impl<'tcx> EncodableWithShorthand for Ty<'tcx> {
30 type Variant = ty::TyKind<'tcx>;
31 fn variant(&self) -> &Self::Variant {
36 impl<'tcx> EncodableWithShorthand for ty::Predicate<'tcx> {
37 type Variant = ty::Predicate<'tcx>;
38 fn variant(&self) -> &Self::Variant {
43 pub trait TyEncoder: Encoder {
44 fn position(&self) -> usize;
47 impl TyEncoder for opaque::Encoder {
49 fn position(&self) -> usize {
54 /// Encode the given value or a previously cached shorthand.
55 pub fn encode_with_shorthand<E, T, M>(encoder: &mut E,
58 -> Result<(), E::Error>
60 M: for<'b> Fn(&'b mut E) -> &'b mut FxHashMap<T, usize>,
61 T: EncodableWithShorthand,
63 let existing_shorthand = cache(encoder).get(value).cloned();
64 if let Some(shorthand) = existing_shorthand {
65 return encoder.emit_usize(shorthand);
68 let variant = value.variant();
70 let start = encoder.position();
71 variant.encode(encoder)?;
72 let len = encoder.position() - start;
74 // The shorthand encoding uses the same usize as the
75 // discriminant, with an offset so they can't conflict.
76 let discriminant = unsafe { intrinsics::discriminant_value(variant) };
77 assert!(discriminant < SHORTHAND_OFFSET as u64);
78 let shorthand = start + SHORTHAND_OFFSET;
80 // Get the number of bits that leb128 could fit
81 // in the same space as the fully encoded type.
82 let leb128_bits = len * 7;
84 // Check that the shorthand is a not longer than the
85 // full encoding itself, i.e., it's an obvious win.
86 if leb128_bits >= 64 || (shorthand as u64) < (1 << leb128_bits) {
87 cache(encoder).insert(value.clone(), shorthand);
93 pub fn encode_predicates<'tcx, E, C>(encoder: &mut E,
94 predicates: &ty::GenericPredicates<'tcx>,
96 -> Result<(), E::Error>
98 C: for<'b> Fn(&'b mut E) -> &'b mut FxHashMap<ty::Predicate<'tcx>, usize>,
100 predicates.parent.encode(encoder)?;
101 predicates.predicates.len().encode(encoder)?;
102 for (predicate, span) in &predicates.predicates {
103 encode_with_shorthand(encoder, predicate, &cache)?;
104 span.encode(encoder)?;
109 pub trait TyDecoder<'a, 'tcx: 'a>: Decoder {
111 fn tcx(&self) -> TyCtxt<'a, 'tcx, 'tcx>;
113 fn peek_byte(&self) -> u8;
115 fn position(&self) -> usize;
117 fn cached_ty_for_shorthand<F>(&mut self,
120 -> Result<Ty<'tcx>, Self::Error>
121 where F: FnOnce(&mut Self) -> Result<Ty<'tcx>, Self::Error>;
123 fn with_position<F, R>(&mut self, pos: usize, f: F) -> R
124 where F: FnOnce(&mut Self) -> R;
126 fn map_encoded_cnum_to_current(&self, cnum: CrateNum) -> CrateNum;
128 fn positioned_at_shorthand(&self) -> bool {
129 (self.peek_byte() & (SHORTHAND_OFFSET as u8)) != 0
134 pub fn decode_cnum<'a, 'tcx, D>(decoder: &mut D) -> Result<CrateNum, D::Error>
135 where D: TyDecoder<'a, 'tcx>,
138 let cnum = CrateNum::from_u32(u32::decode(decoder)?);
139 Ok(decoder.map_encoded_cnum_to_current(cnum))
143 pub fn decode_ty<'a, 'tcx, D>(decoder: &mut D) -> Result<Ty<'tcx>, D::Error>
144 where D: TyDecoder<'a, 'tcx>,
147 // Handle shorthands first, if we have an usize > 0x80.
148 if decoder.positioned_at_shorthand() {
149 let pos = decoder.read_usize()?;
150 assert!(pos >= SHORTHAND_OFFSET);
151 let shorthand = pos - SHORTHAND_OFFSET;
153 decoder.cached_ty_for_shorthand(shorthand, |decoder| {
154 decoder.with_position(shorthand, Ty::decode)
157 let tcx = decoder.tcx();
158 Ok(tcx.mk_ty(ty::TyKind::decode(decoder)?))
163 pub fn decode_predicates<'a, 'tcx, D>(decoder: &mut D)
164 -> Result<ty::GenericPredicates<'tcx>, D::Error>
165 where D: TyDecoder<'a, 'tcx>,
168 Ok(ty::GenericPredicates {
169 parent: Decodable::decode(decoder)?,
170 predicates: (0..decoder.read_usize()?).map(|_| {
171 // Handle shorthands first, if we have an usize > 0x80.
172 let predicate = if decoder.positioned_at_shorthand() {
173 let pos = decoder.read_usize()?;
174 assert!(pos >= SHORTHAND_OFFSET);
175 let shorthand = pos - SHORTHAND_OFFSET;
177 decoder.with_position(shorthand, ty::Predicate::decode)
179 ty::Predicate::decode(decoder)
181 Ok((predicate, Decodable::decode(decoder)?))
183 .collect::<Result<Vec<_>, _>>()?,
188 pub fn decode_substs<'a, 'tcx, D>(decoder: &mut D) -> Result<SubstsRef<'tcx>, D::Error>
189 where D: TyDecoder<'a, 'tcx>,
192 let len = decoder.read_usize()?;
193 let tcx = decoder.tcx();
194 Ok(tcx.mk_substs((0..len).map(|_| Decodable::decode(decoder)))?)
198 pub fn decode_region<'a, 'tcx, D>(decoder: &mut D) -> Result<ty::Region<'tcx>, D::Error>
199 where D: TyDecoder<'a, 'tcx>,
202 Ok(decoder.tcx().mk_region(Decodable::decode(decoder)?))
206 pub fn decode_ty_slice<'a, 'tcx, D>(decoder: &mut D)
207 -> Result<&'tcx ty::List<Ty<'tcx>>, D::Error>
208 where D: TyDecoder<'a, 'tcx>,
211 let len = decoder.read_usize()?;
212 Ok(decoder.tcx().mk_type_list((0..len).map(|_| Decodable::decode(decoder)))?)
216 pub fn decode_adt_def<'a, 'tcx, D>(decoder: &mut D)
217 -> Result<&'tcx ty::AdtDef, D::Error>
218 where D: TyDecoder<'a, 'tcx>,
221 let def_id = DefId::decode(decoder)?;
222 Ok(decoder.tcx().adt_def(def_id))
226 pub fn decode_existential_predicate_slice<'a, 'tcx, D>(decoder: &mut D)
227 -> Result<&'tcx ty::List<ty::ExistentialPredicate<'tcx>>, D::Error>
228 where D: TyDecoder<'a, 'tcx>,
231 let len = decoder.read_usize()?;
233 .mk_existential_predicates((0..len).map(|_| Decodable::decode(decoder)))?)
237 pub fn decode_canonical_var_infos<'a, 'tcx, D>(decoder: &mut D)
238 -> Result<CanonicalVarInfos<'tcx>, D::Error>
239 where D: TyDecoder<'a, 'tcx>,
242 let len = decoder.read_usize()?;
243 let interned: Result<Vec<CanonicalVarInfo>, _> = (0..len).map(|_| Decodable::decode(decoder))
246 .intern_canonical_var_infos(interned?.as_slice()))
250 pub fn decode_const<'a, 'tcx, D>(decoder: &mut D)
251 -> Result<&'tcx ty::Const<'tcx>, D::Error>
252 where D: TyDecoder<'a, 'tcx>,
255 Ok(decoder.tcx().mk_const(Decodable::decode(decoder)?))
259 pub fn decode_allocation<'a, 'tcx, D>(decoder: &mut D)
260 -> Result<&'tcx Allocation, D::Error>
261 where D: TyDecoder<'a, 'tcx>,
264 Ok(decoder.tcx().intern_const_alloc(Decodable::decode(decoder)?))
268 macro_rules! __impl_decoder_methods {
269 ($($name:ident -> $ty:ty;)*) => {
270 $(fn $name(&mut self) -> Result<$ty, Self::Error> {
277 macro_rules! implement_ty_decoder {
278 ($DecoderName:ident <$($typaram:tt),*>) => {
279 mod __ty_decoder_impl {
280 use super::$DecoderName;
281 use $crate::infer::canonical::CanonicalVarInfos;
283 use $crate::ty::codec::*;
284 use $crate::ty::subst::SubstsRef;
285 use $crate::hir::def_id::{CrateNum};
286 use crate::rustc_serialize::{Decoder, SpecializedDecoder};
287 use std::borrow::Cow;
289 impl<$($typaram ),*> Decoder for $DecoderName<$($typaram),*> {
292 __impl_decoder_methods! {
313 read_str -> Cow<'_, str>;
316 fn error(&mut self, err: &str) -> Self::Error {
317 self.opaque.error(err)
321 // FIXME(#36588) These impls are horribly unsound as they allow
322 // the caller to pick any lifetime for 'tcx, including 'static,
323 // by using the unspecialized proxies to them.
325 impl<$($typaram),*> SpecializedDecoder<CrateNum>
326 for $DecoderName<$($typaram),*> {
327 fn specialized_decode(&mut self) -> Result<CrateNum, Self::Error> {
332 impl<$($typaram),*> SpecializedDecoder<ty::Ty<'tcx>>
333 for $DecoderName<$($typaram),*> {
334 fn specialized_decode(&mut self) -> Result<ty::Ty<'tcx>, Self::Error> {
339 impl<$($typaram),*> SpecializedDecoder<ty::GenericPredicates<'tcx>>
340 for $DecoderName<$($typaram),*> {
341 fn specialized_decode(&mut self)
342 -> Result<ty::GenericPredicates<'tcx>, Self::Error> {
343 decode_predicates(self)
347 impl<$($typaram),*> SpecializedDecoder<SubstsRef<'tcx>>
348 for $DecoderName<$($typaram),*> {
349 fn specialized_decode(&mut self) -> Result<SubstsRef<'tcx>, Self::Error> {
354 impl<$($typaram),*> SpecializedDecoder<ty::Region<'tcx>>
355 for $DecoderName<$($typaram),*> {
356 fn specialized_decode(&mut self) -> Result<ty::Region<'tcx>, Self::Error> {
361 impl<$($typaram),*> SpecializedDecoder<&'tcx ty::List<ty::Ty<'tcx>>>
362 for $DecoderName<$($typaram),*> {
363 fn specialized_decode(&mut self)
364 -> Result<&'tcx ty::List<ty::Ty<'tcx>>, Self::Error> {
365 decode_ty_slice(self)
369 impl<$($typaram),*> SpecializedDecoder<&'tcx ty::AdtDef>
370 for $DecoderName<$($typaram),*> {
371 fn specialized_decode(&mut self) -> Result<&'tcx ty::AdtDef, Self::Error> {
376 impl<$($typaram),*> SpecializedDecoder<&'tcx ty::List<ty::ExistentialPredicate<'tcx>>>
377 for $DecoderName<$($typaram),*> {
378 fn specialized_decode(&mut self)
379 -> Result<&'tcx ty::List<ty::ExistentialPredicate<'tcx>>, Self::Error> {
380 decode_existential_predicate_slice(self)
384 impl<$($typaram),*> SpecializedDecoder<CanonicalVarInfos<'tcx>>
385 for $DecoderName<$($typaram),*> {
386 fn specialized_decode(&mut self)
387 -> Result<CanonicalVarInfos<'tcx>, Self::Error> {
388 decode_canonical_var_infos(self)
392 impl<$($typaram),*> SpecializedDecoder<&'tcx $crate::ty::Const<'tcx>>
393 for $DecoderName<$($typaram),*> {
394 fn specialized_decode(&mut self) -> Result<&'tcx ty::Const<'tcx>, Self::Error> {
399 impl<$($typaram),*> SpecializedDecoder<&'tcx $crate::mir::interpret::Allocation>
400 for $DecoderName<$($typaram),*> {
401 fn specialized_decode(
403 ) -> Result<&'tcx $crate::mir::interpret::Allocation, Self::Error> {
404 decode_allocation(self)