]> git.lizzy.rs Git - rust.git/blob - src/librustc/ty/codec.rs
Auto merge of #59240 - euclio:struct-field-span, r=oli-obk
[rust.git] / src / librustc / ty / codec.rs
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.
5 //
6 // The functionality in here is shared between persisting to crate metadata and
7 // persisting to incr. comp. caches.
8
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};
13 use std::hash::Hash;
14 use std::intrinsics;
15 use crate::ty::{self, Ty, TyCtxt};
16 use crate::ty::subst::SubstsRef;
17 use crate::mir::interpret::Allocation;
18
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;
23
24 pub trait EncodableWithShorthand: Clone + Eq + Hash {
25     type Variant: Encodable;
26     fn variant(&self) -> &Self::Variant;
27 }
28
29 impl<'tcx> EncodableWithShorthand for Ty<'tcx> {
30     type Variant = ty::TyKind<'tcx>;
31     fn variant(&self) -> &Self::Variant {
32         &self.sty
33     }
34 }
35
36 impl<'tcx> EncodableWithShorthand for ty::Predicate<'tcx> {
37     type Variant = ty::Predicate<'tcx>;
38     fn variant(&self) -> &Self::Variant {
39         self
40     }
41 }
42
43 pub trait TyEncoder: Encoder {
44     fn position(&self) -> usize;
45 }
46
47 impl TyEncoder for opaque::Encoder {
48     #[inline]
49     fn position(&self) -> usize {
50         self.position()
51     }
52 }
53
54 /// Encode the given value or a previously cached shorthand.
55 pub fn encode_with_shorthand<E, T, M>(encoder: &mut E,
56                                       value: &T,
57                                       cache: M)
58                                       -> Result<(), E::Error>
59     where E: TyEncoder,
60           M: for<'b> Fn(&'b mut E) -> &'b mut FxHashMap<T, usize>,
61           T: EncodableWithShorthand,
62 {
63     let existing_shorthand = cache(encoder).get(value).cloned();
64     if let Some(shorthand) = existing_shorthand {
65         return encoder.emit_usize(shorthand);
66     }
67
68     let variant = value.variant();
69
70     let start = encoder.position();
71     variant.encode(encoder)?;
72     let len = encoder.position() - start;
73
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;
79
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;
83
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);
88     }
89
90     Ok(())
91 }
92
93 pub fn encode_predicates<'tcx, E, C>(encoder: &mut E,
94                                      predicates: &ty::GenericPredicates<'tcx>,
95                                      cache: C)
96                                      -> Result<(), E::Error>
97     where E: TyEncoder,
98           C: for<'b> Fn(&'b mut E) -> &'b mut FxHashMap<ty::Predicate<'tcx>, usize>,
99 {
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)?;
105     }
106     Ok(())
107 }
108
109 pub trait TyDecoder<'a, 'tcx: 'a>: Decoder {
110
111     fn tcx(&self) -> TyCtxt<'a, 'tcx, 'tcx>;
112
113     fn peek_byte(&self) -> u8;
114
115     fn position(&self) -> usize;
116
117     fn cached_ty_for_shorthand<F>(&mut self,
118                                   shorthand: usize,
119                                   or_insert_with: F)
120                                   -> Result<Ty<'tcx>, Self::Error>
121         where F: FnOnce(&mut Self) -> Result<Ty<'tcx>, Self::Error>;
122
123     fn with_position<F, R>(&mut self, pos: usize, f: F) -> R
124         where F: FnOnce(&mut Self) -> R;
125
126     fn map_encoded_cnum_to_current(&self, cnum: CrateNum) -> CrateNum;
127
128     fn positioned_at_shorthand(&self) -> bool {
129         (self.peek_byte() & (SHORTHAND_OFFSET as u8)) != 0
130     }
131 }
132
133 #[inline]
134 pub fn decode_cnum<'a, 'tcx, D>(decoder: &mut D) -> Result<CrateNum, D::Error>
135     where D: TyDecoder<'a, 'tcx>,
136           'tcx: 'a,
137 {
138     let cnum = CrateNum::from_u32(u32::decode(decoder)?);
139     Ok(decoder.map_encoded_cnum_to_current(cnum))
140 }
141
142 #[inline]
143 pub fn decode_ty<'a, 'tcx, D>(decoder: &mut D) -> Result<Ty<'tcx>, D::Error>
144     where D: TyDecoder<'a, 'tcx>,
145           'tcx: 'a,
146 {
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;
152
153         decoder.cached_ty_for_shorthand(shorthand, |decoder| {
154             decoder.with_position(shorthand, Ty::decode)
155         })
156     } else {
157         let tcx = decoder.tcx();
158         Ok(tcx.mk_ty(ty::TyKind::decode(decoder)?))
159     }
160 }
161
162 #[inline]
163 pub fn decode_predicates<'a, 'tcx, D>(decoder: &mut D)
164                                       -> Result<ty::GenericPredicates<'tcx>, D::Error>
165     where D: TyDecoder<'a, 'tcx>,
166           'tcx: 'a,
167 {
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;
176
177                 decoder.with_position(shorthand, ty::Predicate::decode)
178             } else {
179                 ty::Predicate::decode(decoder)
180             }?;
181             Ok((predicate, Decodable::decode(decoder)?))
182         })
183         .collect::<Result<Vec<_>, _>>()?,
184     })
185 }
186
187 #[inline]
188 pub fn decode_substs<'a, 'tcx, D>(decoder: &mut D) -> Result<SubstsRef<'tcx>, D::Error>
189     where D: TyDecoder<'a, 'tcx>,
190           'tcx: 'a,
191 {
192     let len = decoder.read_usize()?;
193     let tcx = decoder.tcx();
194     Ok(tcx.mk_substs((0..len).map(|_| Decodable::decode(decoder)))?)
195 }
196
197 #[inline]
198 pub fn decode_region<'a, 'tcx, D>(decoder: &mut D) -> Result<ty::Region<'tcx>, D::Error>
199     where D: TyDecoder<'a, 'tcx>,
200           'tcx: 'a,
201 {
202     Ok(decoder.tcx().mk_region(Decodable::decode(decoder)?))
203 }
204
205 #[inline]
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>,
209           'tcx: 'a,
210 {
211     let len = decoder.read_usize()?;
212     Ok(decoder.tcx().mk_type_list((0..len).map(|_| Decodable::decode(decoder)))?)
213 }
214
215 #[inline]
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>,
219           'tcx: 'a,
220 {
221     let def_id = DefId::decode(decoder)?;
222     Ok(decoder.tcx().adt_def(def_id))
223 }
224
225 #[inline]
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>,
229           'tcx: 'a,
230 {
231     let len = decoder.read_usize()?;
232     Ok(decoder.tcx()
233               .mk_existential_predicates((0..len).map(|_| Decodable::decode(decoder)))?)
234 }
235
236 #[inline]
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>,
240           'tcx: 'a,
241 {
242     let len = decoder.read_usize()?;
243     let interned: Result<Vec<CanonicalVarInfo>, _> = (0..len).map(|_| Decodable::decode(decoder))
244                                                              .collect();
245     Ok(decoder.tcx()
246               .intern_canonical_var_infos(interned?.as_slice()))
247 }
248
249 #[inline]
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>,
253           'tcx: 'a,
254 {
255     Ok(decoder.tcx().mk_const(Decodable::decode(decoder)?))
256 }
257
258 #[inline]
259 pub fn decode_allocation<'a, 'tcx, D>(decoder: &mut D)
260     -> Result<&'tcx Allocation, D::Error>
261     where D: TyDecoder<'a, 'tcx>,
262           'tcx: 'a,
263 {
264     Ok(decoder.tcx().intern_const_alloc(Decodable::decode(decoder)?))
265 }
266
267 #[macro_export]
268 macro_rules! __impl_decoder_methods {
269     ($($name:ident -> $ty:ty;)*) => {
270         $(fn $name(&mut self) -> Result<$ty, Self::Error> {
271             self.opaque.$name()
272         })*
273     }
274 }
275
276 #[macro_export]
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;
282             use $crate::ty;
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;
288
289             impl<$($typaram ),*> Decoder for $DecoderName<$($typaram),*> {
290                 type Error = String;
291
292                 __impl_decoder_methods! {
293                     read_nil -> ();
294
295                     read_u128 -> u128;
296                     read_u64 -> u64;
297                     read_u32 -> u32;
298                     read_u16 -> u16;
299                     read_u8 -> u8;
300                     read_usize -> usize;
301
302                     read_i128 -> i128;
303                     read_i64 -> i64;
304                     read_i32 -> i32;
305                     read_i16 -> i16;
306                     read_i8 -> i8;
307                     read_isize -> isize;
308
309                     read_bool -> bool;
310                     read_f64 -> f64;
311                     read_f32 -> f32;
312                     read_char -> char;
313                     read_str -> Cow<'_, str>;
314                 }
315
316                 fn error(&mut self, err: &str) -> Self::Error {
317                     self.opaque.error(err)
318                 }
319             }
320
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.
324
325             impl<$($typaram),*> SpecializedDecoder<CrateNum>
326             for $DecoderName<$($typaram),*> {
327                 fn specialized_decode(&mut self) -> Result<CrateNum, Self::Error> {
328                     decode_cnum(self)
329                 }
330             }
331
332             impl<$($typaram),*> SpecializedDecoder<ty::Ty<'tcx>>
333             for $DecoderName<$($typaram),*> {
334                 fn specialized_decode(&mut self) -> Result<ty::Ty<'tcx>, Self::Error> {
335                     decode_ty(self)
336                 }
337             }
338
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)
344                 }
345             }
346
347             impl<$($typaram),*> SpecializedDecoder<SubstsRef<'tcx>>
348             for $DecoderName<$($typaram),*> {
349                 fn specialized_decode(&mut self) -> Result<SubstsRef<'tcx>, Self::Error> {
350                     decode_substs(self)
351                 }
352             }
353
354             impl<$($typaram),*> SpecializedDecoder<ty::Region<'tcx>>
355             for $DecoderName<$($typaram),*> {
356                 fn specialized_decode(&mut self) -> Result<ty::Region<'tcx>, Self::Error> {
357                     decode_region(self)
358                 }
359             }
360
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)
366                 }
367             }
368
369             impl<$($typaram),*> SpecializedDecoder<&'tcx ty::AdtDef>
370             for $DecoderName<$($typaram),*> {
371                 fn specialized_decode(&mut self) -> Result<&'tcx ty::AdtDef, Self::Error> {
372                     decode_adt_def(self)
373                 }
374             }
375
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)
381                 }
382             }
383
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)
389                 }
390             }
391
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> {
395                     decode_const(self)
396                 }
397             }
398
399             impl<$($typaram),*> SpecializedDecoder<&'tcx $crate::mir::interpret::Allocation>
400             for $DecoderName<$($typaram),*> {
401                 fn specialized_decode(
402                     &mut self
403                 ) -> Result<&'tcx $crate::mir::interpret::Allocation, Self::Error> {
404                     decode_allocation(self)
405                 }
406             }
407         }
408     }
409 }