]> git.lizzy.rs Git - rust.git/blob - src/librustc/ty/codec.rs
Auto merge of #49313 - sgrif:sg-revert-stuff, r=nikomatsakis
[rust.git] / src / librustc / ty / codec.rs
1 // Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 // This module contains some shared code for encoding and decoding various
12 // things from the `ty` module, and in particular implements support for
13 // "shorthands" which allow to have pointers back into the already encoded
14 // stream instead of re-encoding the same thing twice.
15 //
16 // The functionality in here is shared between persisting to crate metadata and
17 // persisting to incr. comp. caches.
18
19 use hir::def_id::{DefId, CrateNum};
20 use infer::canonical::{CanonicalVarInfo, CanonicalVarInfos};
21 use rustc_data_structures::fx::FxHashMap;
22 use rustc_serialize::{Decodable, Decoder, Encoder, Encodable, opaque};
23 use std::hash::Hash;
24 use std::intrinsics;
25 use ty::{self, Ty, TyCtxt};
26 use ty::subst::Substs;
27
28 /// The shorthand encoding uses an enum's variant index `usize`
29 /// and is offset by this value so it never matches a real variant.
30 /// This offset is also chosen so that the first byte is never < 0x80.
31 pub const SHORTHAND_OFFSET: usize = 0x80;
32
33 pub trait EncodableWithShorthand: Clone + Eq + Hash {
34     type Variant: Encodable;
35     fn variant(&self) -> &Self::Variant;
36 }
37
38 impl<'tcx> EncodableWithShorthand for Ty<'tcx> {
39     type Variant = ty::TypeVariants<'tcx>;
40     fn variant(&self) -> &Self::Variant {
41         &self.sty
42     }
43 }
44
45 impl<'tcx> EncodableWithShorthand for ty::Predicate<'tcx> {
46     type Variant = ty::Predicate<'tcx>;
47     fn variant(&self) -> &Self::Variant {
48         self
49     }
50 }
51
52 pub trait TyEncoder: Encoder {
53     fn position(&self) -> usize;
54 }
55
56 impl<'buf> TyEncoder for opaque::Encoder<'buf> {
57     #[inline]
58     fn position(&self) -> usize {
59         self.position()
60     }
61 }
62
63 /// Encode the given value or a previously cached shorthand.
64 pub fn encode_with_shorthand<E, T, M>(encoder: &mut E,
65                                       value: &T,
66                                       cache: M)
67                                       -> Result<(), E::Error>
68     where E: TyEncoder,
69           M: for<'b> Fn(&'b mut E) -> &'b mut FxHashMap<T, usize>,
70           T: EncodableWithShorthand,
71 {
72     let existing_shorthand = cache(encoder).get(value).cloned();
73     if let Some(shorthand) = existing_shorthand {
74         return encoder.emit_usize(shorthand);
75     }
76
77     let variant = value.variant();
78
79     let start = encoder.position();
80     variant.encode(encoder)?;
81     let len = encoder.position() - start;
82
83     // The shorthand encoding uses the same usize as the
84     // discriminant, with an offset so they can't conflict.
85     let discriminant = unsafe { intrinsics::discriminant_value(variant) };
86     assert!(discriminant < SHORTHAND_OFFSET as u64);
87     let shorthand = start + SHORTHAND_OFFSET;
88
89     // Get the number of bits that leb128 could fit
90     // in the same space as the fully encoded type.
91     let leb128_bits = len * 7;
92
93     // Check that the shorthand is a not longer than the
94     // full encoding itself, i.e. it's an obvious win.
95     if leb128_bits >= 64 || (shorthand as u64) < (1 << leb128_bits) {
96         cache(encoder).insert(value.clone(), shorthand);
97     }
98
99     Ok(())
100 }
101
102 pub fn encode_predicates<'tcx, E, C>(encoder: &mut E,
103                                      predicates: &ty::GenericPredicates<'tcx>,
104                                      cache: C)
105                                      -> Result<(), E::Error>
106     where E: TyEncoder,
107           C: for<'b> Fn(&'b mut E) -> &'b mut FxHashMap<ty::Predicate<'tcx>, usize>,
108 {
109     predicates.parent.encode(encoder)?;
110     predicates.predicates.len().encode(encoder)?;
111     for predicate in &predicates.predicates {
112         encode_with_shorthand(encoder, predicate, &cache)?
113     }
114     Ok(())
115 }
116
117 pub trait TyDecoder<'a, 'tcx: 'a>: Decoder {
118
119     fn tcx(&self) -> TyCtxt<'a, 'tcx, 'tcx>;
120
121     fn peek_byte(&self) -> u8;
122
123     fn position(&self) -> usize;
124
125     fn cached_ty_for_shorthand<F>(&mut self,
126                                   shorthand: usize,
127                                   or_insert_with: F)
128                                   -> Result<Ty<'tcx>, Self::Error>
129         where F: FnOnce(&mut Self) -> Result<Ty<'tcx>, Self::Error>;
130
131     fn with_position<F, R>(&mut self, pos: usize, f: F) -> R
132         where F: FnOnce(&mut Self) -> R;
133
134     fn map_encoded_cnum_to_current(&self, cnum: CrateNum) -> CrateNum;
135
136     fn positioned_at_shorthand(&self) -> bool {
137         (self.peek_byte() & (SHORTHAND_OFFSET as u8)) != 0
138     }
139 }
140
141 #[inline]
142 pub fn decode_cnum<'a, 'tcx, D>(decoder: &mut D) -> Result<CrateNum, D::Error>
143     where D: TyDecoder<'a, 'tcx>,
144           'tcx: 'a,
145 {
146     let cnum = CrateNum::from_u32(u32::decode(decoder)?);
147     Ok(decoder.map_encoded_cnum_to_current(cnum))
148 }
149
150 #[inline]
151 pub fn decode_ty<'a, 'tcx, D>(decoder: &mut D) -> Result<Ty<'tcx>, D::Error>
152     where D: TyDecoder<'a, 'tcx>,
153           'tcx: 'a,
154 {
155     // Handle shorthands first, if we have an usize > 0x80.
156     if decoder.positioned_at_shorthand() {
157         let pos = decoder.read_usize()?;
158         assert!(pos >= SHORTHAND_OFFSET);
159         let shorthand = pos - SHORTHAND_OFFSET;
160
161         decoder.cached_ty_for_shorthand(shorthand, |decoder| {
162             decoder.with_position(shorthand, Ty::decode)
163         })
164     } else {
165         let tcx = decoder.tcx();
166         Ok(tcx.mk_ty(ty::TypeVariants::decode(decoder)?))
167     }
168 }
169
170 #[inline]
171 pub fn decode_predicates<'a, 'tcx, D>(decoder: &mut D)
172                                       -> Result<ty::GenericPredicates<'tcx>, D::Error>
173     where D: TyDecoder<'a, 'tcx>,
174           'tcx: 'a,
175 {
176     Ok(ty::GenericPredicates {
177         parent: Decodable::decode(decoder)?,
178         predicates: (0..decoder.read_usize()?).map(|_| {
179                 // Handle shorthands first, if we have an usize > 0x80.
180                 if decoder.positioned_at_shorthand() {
181                     let pos = decoder.read_usize()?;
182                     assert!(pos >= SHORTHAND_OFFSET);
183                     let shorthand = pos - SHORTHAND_OFFSET;
184
185                     decoder.with_position(shorthand, ty::Predicate::decode)
186                 } else {
187                     ty::Predicate::decode(decoder)
188                 }
189             })
190             .collect::<Result<Vec<_>, _>>()?,
191     })
192 }
193
194 #[inline]
195 pub fn decode_substs<'a, 'tcx, D>(decoder: &mut D) -> Result<&'tcx Substs<'tcx>, D::Error>
196     where D: TyDecoder<'a, 'tcx>,
197           'tcx: 'a,
198 {
199     let len = decoder.read_usize()?;
200     let tcx = decoder.tcx();
201     Ok(tcx.mk_substs((0..len).map(|_| Decodable::decode(decoder)))?)
202 }
203
204 #[inline]
205 pub fn decode_region<'a, 'tcx, D>(decoder: &mut D) -> Result<ty::Region<'tcx>, D::Error>
206     where D: TyDecoder<'a, 'tcx>,
207           'tcx: 'a,
208 {
209     Ok(decoder.tcx().mk_region(Decodable::decode(decoder)?))
210 }
211
212 #[inline]
213 pub fn decode_ty_slice<'a, 'tcx, D>(decoder: &mut D)
214                                     -> Result<&'tcx ty::Slice<Ty<'tcx>>, D::Error>
215     where D: TyDecoder<'a, 'tcx>,
216           'tcx: 'a,
217 {
218     let len = decoder.read_usize()?;
219     Ok(decoder.tcx().mk_type_list((0..len).map(|_| Decodable::decode(decoder)))?)
220 }
221
222 #[inline]
223 pub fn decode_adt_def<'a, 'tcx, D>(decoder: &mut D)
224                                    -> Result<&'tcx ty::AdtDef, D::Error>
225     where D: TyDecoder<'a, 'tcx>,
226           'tcx: 'a,
227 {
228     let def_id = DefId::decode(decoder)?;
229     Ok(decoder.tcx().adt_def(def_id))
230 }
231
232 #[inline]
233 pub fn decode_existential_predicate_slice<'a, 'tcx, D>(decoder: &mut D)
234     -> Result<&'tcx ty::Slice<ty::ExistentialPredicate<'tcx>>, D::Error>
235     where D: TyDecoder<'a, 'tcx>,
236           'tcx: 'a,
237 {
238     let len = decoder.read_usize()?;
239     Ok(decoder.tcx()
240               .mk_existential_predicates((0..len).map(|_| Decodable::decode(decoder)))?)
241 }
242
243 #[inline]
244 pub fn decode_canonical_var_infos<'a, 'tcx, D>(decoder: &mut D)
245     -> Result<CanonicalVarInfos<'tcx>, D::Error>
246     where D: TyDecoder<'a, 'tcx>,
247           'tcx: 'a,
248 {
249     let len = decoder.read_usize()?;
250     let interned: Result<Vec<CanonicalVarInfo>, _> = (0..len).map(|_| Decodable::decode(decoder))
251                                                              .collect();
252     Ok(decoder.tcx()
253               .intern_canonical_var_infos(interned?.as_slice()))
254 }
255
256 #[inline]
257 pub fn decode_const<'a, 'tcx, D>(decoder: &mut D)
258                                  -> Result<&'tcx ty::Const<'tcx>, D::Error>
259     where D: TyDecoder<'a, 'tcx>,
260           'tcx: 'a,
261 {
262     Ok(decoder.tcx().mk_const(Decodable::decode(decoder)?))
263 }
264
265 #[macro_export]
266 macro_rules! __impl_decoder_methods {
267     ($($name:ident -> $ty:ty;)*) => {
268         $(fn $name(&mut self) -> Result<$ty, Self::Error> {
269             self.opaque.$name()
270         })*
271     }
272 }
273
274 #[macro_export]
275 macro_rules! implement_ty_decoder {
276     ($DecoderName:ident <$($typaram:tt),*>) => {
277         mod __ty_decoder_impl {
278             use super::$DecoderName;
279             use $crate::infer::canonical::CanonicalVarInfos;
280             use $crate::ty;
281             use $crate::ty::codec::*;
282             use $crate::ty::subst::Substs;
283             use $crate::hir::def_id::{CrateNum};
284             use rustc_serialize::{Decoder, SpecializedDecoder};
285             use std::borrow::Cow;
286
287             impl<$($typaram ),*> Decoder for $DecoderName<$($typaram),*> {
288                 type Error = String;
289
290                 __impl_decoder_methods! {
291                     read_nil -> ();
292
293                     read_u128 -> u128;
294                     read_u64 -> u64;
295                     read_u32 -> u32;
296                     read_u16 -> u16;
297                     read_u8 -> u8;
298                     read_usize -> usize;
299
300                     read_i128 -> i128;
301                     read_i64 -> i64;
302                     read_i32 -> i32;
303                     read_i16 -> i16;
304                     read_i8 -> i8;
305                     read_isize -> isize;
306
307                     read_bool -> bool;
308                     read_f64 -> f64;
309                     read_f32 -> f32;
310                     read_char -> char;
311                     read_str -> Cow<str>;
312                 }
313
314                 fn error(&mut self, err: &str) -> Self::Error {
315                     self.opaque.error(err)
316                 }
317             }
318
319             // FIXME(#36588) These impls are horribly unsound as they allow
320             // the caller to pick any lifetime for 'tcx, including 'static,
321             // by using the unspecialized proxies to them.
322
323             impl<$($typaram),*> SpecializedDecoder<CrateNum>
324             for $DecoderName<$($typaram),*> {
325                 fn specialized_decode(&mut self) -> Result<CrateNum, Self::Error> {
326                     decode_cnum(self)
327                 }
328             }
329
330             impl<$($typaram),*> SpecializedDecoder<ty::Ty<'tcx>>
331             for $DecoderName<$($typaram),*> {
332                 fn specialized_decode(&mut self) -> Result<ty::Ty<'tcx>, Self::Error> {
333                     decode_ty(self)
334                 }
335             }
336
337             impl<$($typaram),*> SpecializedDecoder<ty::GenericPredicates<'tcx>>
338             for $DecoderName<$($typaram),*> {
339                 fn specialized_decode(&mut self)
340                                       -> Result<ty::GenericPredicates<'tcx>, Self::Error> {
341                     decode_predicates(self)
342                 }
343             }
344
345             impl<$($typaram),*> SpecializedDecoder<&'tcx Substs<'tcx>>
346             for $DecoderName<$($typaram),*> {
347                 fn specialized_decode(&mut self) -> Result<&'tcx Substs<'tcx>, Self::Error> {
348                     decode_substs(self)
349                 }
350             }
351
352             impl<$($typaram),*> SpecializedDecoder<ty::Region<'tcx>>
353             for $DecoderName<$($typaram),*> {
354                 fn specialized_decode(&mut self) -> Result<ty::Region<'tcx>, Self::Error> {
355                     decode_region(self)
356                 }
357             }
358
359             impl<$($typaram),*> SpecializedDecoder<&'tcx ty::Slice<ty::Ty<'tcx>>>
360             for $DecoderName<$($typaram),*> {
361                 fn specialized_decode(&mut self)
362                                       -> Result<&'tcx ty::Slice<ty::Ty<'tcx>>, Self::Error> {
363                     decode_ty_slice(self)
364                 }
365             }
366
367             impl<$($typaram),*> SpecializedDecoder<&'tcx ty::AdtDef>
368             for $DecoderName<$($typaram),*> {
369                 fn specialized_decode(&mut self) -> Result<&'tcx ty::AdtDef, Self::Error> {
370                     decode_adt_def(self)
371                 }
372             }
373
374             impl<$($typaram),*> SpecializedDecoder<&'tcx ty::Slice<ty::ExistentialPredicate<'tcx>>>
375                 for $DecoderName<$($typaram),*> {
376                 fn specialized_decode(&mut self)
377                     -> Result<&'tcx ty::Slice<ty::ExistentialPredicate<'tcx>>, Self::Error> {
378                     decode_existential_predicate_slice(self)
379                 }
380             }
381
382             impl<$($typaram),*> SpecializedDecoder<CanonicalVarInfos<'tcx>>
383                 for $DecoderName<$($typaram),*> {
384                 fn specialized_decode(&mut self)
385                     -> Result<CanonicalVarInfos<'tcx>, Self::Error> {
386                     decode_canonical_var_infos(self)
387                 }
388             }
389
390             impl<$($typaram),*> SpecializedDecoder<&'tcx $crate::ty::Const<'tcx>>
391             for $DecoderName<$($typaram),*> {
392                 fn specialized_decode(&mut self) -> Result<&'tcx ty::Const<'tcx>, Self::Error> {
393                     decode_const(self)
394                 }
395             }
396         }
397     }
398 }
399