]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_middle/src/ty/codec.rs
Rollup merge of #86374 - bossmc:enable-static-pie-for-gnu, r=nagisa
[rust.git] / compiler / rustc_middle / src / 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::arena::ArenaAllocatable;
10 use crate::infer::canonical::{CanonicalVarInfo, CanonicalVarInfos};
11 use crate::mir::{
12     self,
13     interpret::{AllocId, Allocation},
14 };
15 use crate::thir;
16 use crate::ty::subst::SubstsRef;
17 use crate::ty::{self, Ty, TyCtxt};
18 use rustc_data_structures::fx::FxHashMap;
19 use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
20 use rustc_span::Span;
21 use std::hash::Hash;
22 use std::intrinsics;
23 use std::marker::DiscriminantKind;
24
25 /// The shorthand encoding uses an enum's variant index `usize`
26 /// and is offset by this value so it never matches a real variant.
27 /// This offset is also chosen so that the first byte is never < 0x80.
28 pub const SHORTHAND_OFFSET: usize = 0x80;
29
30 pub trait EncodableWithShorthand<'tcx, E: TyEncoder<'tcx>>: Copy + Eq + Hash {
31     type Variant: Encodable<E>;
32     fn variant(&self) -> &Self::Variant;
33 }
34
35 #[allow(rustc::usage_of_ty_tykind)]
36 impl<'tcx, E: TyEncoder<'tcx>> EncodableWithShorthand<'tcx, E> for Ty<'tcx> {
37     type Variant = ty::TyKind<'tcx>;
38
39     #[inline]
40     fn variant(&self) -> &Self::Variant {
41         self.kind()
42     }
43 }
44
45 impl<'tcx, E: TyEncoder<'tcx>> EncodableWithShorthand<'tcx, E> for ty::PredicateKind<'tcx> {
46     type Variant = ty::PredicateKind<'tcx>;
47
48     #[inline]
49     fn variant(&self) -> &Self::Variant {
50         self
51     }
52 }
53
54 pub trait TyEncoder<'tcx>: Encoder {
55     const CLEAR_CROSS_CRATE: bool;
56
57     fn position(&self) -> usize;
58     fn type_shorthands(&mut self) -> &mut FxHashMap<Ty<'tcx>, usize>;
59     fn predicate_shorthands(&mut self) -> &mut FxHashMap<ty::PredicateKind<'tcx>, usize>;
60     fn encode_alloc_id(&mut self, alloc_id: &AllocId) -> Result<(), Self::Error>;
61 }
62
63 /// Trait for decoding to a reference.
64 ///
65 /// This is a separate trait from `Decodable` so that we can implement it for
66 /// upstream types, such as `FxHashSet`.
67 ///
68 /// The `TyDecodable` derive macro will use this trait for fields that are
69 /// references (and don't use a type alias to hide that).
70 ///
71 /// `Decodable` can still be implemented in cases where `Decodable` is required
72 /// by a trait bound.
73 pub trait RefDecodable<'tcx, D: TyDecoder<'tcx>> {
74     fn decode(d: &mut D) -> &'tcx Self;
75 }
76
77 /// Encode the given value or a previously cached shorthand.
78 pub fn encode_with_shorthand<'tcx, E, T, M>(
79     encoder: &mut E,
80     value: &T,
81     cache: M,
82 ) -> Result<(), E::Error>
83 where
84     E: TyEncoder<'tcx>,
85     M: for<'b> Fn(&'b mut E) -> &'b mut FxHashMap<T, usize>,
86     T: EncodableWithShorthand<'tcx, E>,
87     // The discriminant and shorthand must have the same size.
88     T::Variant: DiscriminantKind<Discriminant = isize>,
89 {
90     let existing_shorthand = cache(encoder).get(value).copied();
91     if let Some(shorthand) = existing_shorthand {
92         return encoder.emit_usize(shorthand);
93     }
94
95     let variant = value.variant();
96
97     let start = encoder.position();
98     variant.encode(encoder)?;
99     let len = encoder.position() - start;
100
101     // The shorthand encoding uses the same usize as the
102     // discriminant, with an offset so they can't conflict.
103     let discriminant = intrinsics::discriminant_value(variant);
104     assert!(SHORTHAND_OFFSET > discriminant as usize);
105
106     let shorthand = start + SHORTHAND_OFFSET;
107
108     // Get the number of bits that leb128 could fit
109     // in the same space as the fully encoded type.
110     let leb128_bits = len * 7;
111
112     // Check that the shorthand is a not longer than the
113     // full encoding itself, i.e., it's an obvious win.
114     if leb128_bits >= 64 || (shorthand as u64) < (1 << leb128_bits) {
115         cache(encoder).insert(*value, shorthand);
116     }
117
118     Ok(())
119 }
120
121 impl<'tcx, E: TyEncoder<'tcx>> Encodable<E> for Ty<'tcx> {
122     fn encode(&self, e: &mut E) -> Result<(), E::Error> {
123         encode_with_shorthand(e, self, TyEncoder::type_shorthands)
124     }
125 }
126
127 impl<'tcx, E: TyEncoder<'tcx>> Encodable<E> for ty::Binder<'tcx, ty::PredicateKind<'tcx>> {
128     fn encode(&self, e: &mut E) -> Result<(), E::Error> {
129         self.bound_vars().encode(e)?;
130         encode_with_shorthand(e, &self.skip_binder(), TyEncoder::predicate_shorthands)
131     }
132 }
133
134 impl<'tcx, E: TyEncoder<'tcx>> Encodable<E> for ty::Predicate<'tcx> {
135     fn encode(&self, e: &mut E) -> Result<(), E::Error> {
136         self.kind().encode(e)
137     }
138 }
139
140 impl<'tcx, E: TyEncoder<'tcx>> Encodable<E> for AllocId {
141     fn encode(&self, e: &mut E) -> Result<(), E::Error> {
142         e.encode_alloc_id(self)
143     }
144 }
145
146 macro_rules! encodable_via_deref {
147     ($($t:ty),+) => {
148         $(impl<'tcx, E: TyEncoder<'tcx>> Encodable<E> for $t {
149             fn encode(&self, e: &mut E) -> Result<(), E::Error> {
150                 (**self).encode(e)
151             }
152         })*
153     }
154 }
155
156 encodable_via_deref! {
157     &'tcx ty::TypeckResults<'tcx>,
158     ty::Region<'tcx>,
159     &'tcx mir::Body<'tcx>,
160     &'tcx mir::UnsafetyCheckResult,
161     &'tcx mir::BorrowCheckResult<'tcx>,
162     &'tcx mir::coverage::CodeRegion,
163     &'tcx ty::AdtDef
164 }
165
166 pub trait TyDecoder<'tcx>: Decoder {
167     const CLEAR_CROSS_CRATE: bool;
168
169     fn tcx(&self) -> TyCtxt<'tcx>;
170
171     fn peek_byte(&self) -> u8;
172
173     fn position(&self) -> usize;
174
175     fn cached_ty_for_shorthand<F>(&mut self, shorthand: usize, or_insert_with: F) -> Ty<'tcx>
176     where
177         F: FnOnce(&mut Self) -> Ty<'tcx>;
178
179     fn with_position<F, R>(&mut self, pos: usize, f: F) -> R
180     where
181         F: FnOnce(&mut Self) -> R;
182
183     fn positioned_at_shorthand(&self) -> bool {
184         (self.peek_byte() & (SHORTHAND_OFFSET as u8)) != 0
185     }
186
187     fn decode_alloc_id(&mut self) -> AllocId;
188 }
189
190 #[inline]
191 fn decode_arena_allocable<'tcx, D, T: ArenaAllocatable<'tcx> + Decodable<D>>(
192     decoder: &mut D,
193 ) -> &'tcx T
194 where
195     D: TyDecoder<'tcx>,
196 {
197     decoder.tcx().arena.alloc(Decodable::decode(decoder))
198 }
199
200 #[inline]
201 fn decode_arena_allocable_slice<'tcx, D, T: ArenaAllocatable<'tcx> + Decodable<D>>(
202     decoder: &mut D,
203 ) -> &'tcx [T]
204 where
205     D: TyDecoder<'tcx>,
206 {
207     decoder.tcx().arena.alloc_from_iter(<Vec<T> as Decodable<D>>::decode(decoder))
208 }
209
210 impl<'tcx, D: TyDecoder<'tcx>> Decodable<D> for Ty<'tcx> {
211     #[allow(rustc::usage_of_ty_tykind)]
212     fn decode(decoder: &mut D) -> Ty<'tcx> {
213         // Handle shorthands first, if we have a usize > 0x80.
214         if decoder.positioned_at_shorthand() {
215             let pos = decoder.read_usize();
216             assert!(pos >= SHORTHAND_OFFSET);
217             let shorthand = pos - SHORTHAND_OFFSET;
218
219             decoder.cached_ty_for_shorthand(shorthand, |decoder| {
220                 decoder.with_position(shorthand, Ty::decode)
221             })
222         } else {
223             let tcx = decoder.tcx();
224             tcx.mk_ty(ty::TyKind::decode(decoder))
225         }
226     }
227 }
228
229 impl<'tcx, D: TyDecoder<'tcx>> Decodable<D> for ty::Binder<'tcx, ty::PredicateKind<'tcx>> {
230     fn decode(decoder: &mut D) -> ty::Binder<'tcx, ty::PredicateKind<'tcx>> {
231         let bound_vars = Decodable::decode(decoder);
232         // Handle shorthands first, if we have a usize > 0x80.
233         ty::Binder::bind_with_vars(
234             if decoder.positioned_at_shorthand() {
235                 let pos = decoder.read_usize();
236                 assert!(pos >= SHORTHAND_OFFSET);
237                 let shorthand = pos - SHORTHAND_OFFSET;
238
239                 decoder.with_position(shorthand, ty::PredicateKind::decode)
240             } else {
241                 ty::PredicateKind::decode(decoder)
242             },
243             bound_vars,
244         )
245     }
246 }
247
248 impl<'tcx, D: TyDecoder<'tcx>> Decodable<D> for ty::Predicate<'tcx> {
249     fn decode(decoder: &mut D) -> ty::Predicate<'tcx> {
250         let predicate_kind = Decodable::decode(decoder);
251         decoder.tcx().mk_predicate(predicate_kind)
252     }
253 }
254
255 impl<'tcx, D: TyDecoder<'tcx>> Decodable<D> for SubstsRef<'tcx> {
256     fn decode(decoder: &mut D) -> Self {
257         let len = decoder.read_usize();
258         let tcx = decoder.tcx();
259         tcx.mk_substs(
260             (0..len).map::<ty::subst::GenericArg<'tcx>, _>(|_| Decodable::decode(decoder)),
261         )
262     }
263 }
264
265 impl<'tcx, D: TyDecoder<'tcx>> Decodable<D> for mir::Place<'tcx> {
266     fn decode(decoder: &mut D) -> Self {
267         let local: mir::Local = Decodable::decode(decoder);
268         let len = decoder.read_usize();
269         let projection = decoder.tcx().mk_place_elems(
270             (0..len).map::<mir::PlaceElem<'tcx>, _>(|_| Decodable::decode(decoder)),
271         );
272         mir::Place { local, projection }
273     }
274 }
275
276 impl<'tcx, D: TyDecoder<'tcx>> Decodable<D> for ty::Region<'tcx> {
277     fn decode(decoder: &mut D) -> Self {
278         decoder.tcx().mk_region(Decodable::decode(decoder))
279     }
280 }
281
282 impl<'tcx, D: TyDecoder<'tcx>> Decodable<D> for CanonicalVarInfos<'tcx> {
283     fn decode(decoder: &mut D) -> Self {
284         let len = decoder.read_usize();
285         let interned: Vec<CanonicalVarInfo<'tcx>> =
286             (0..len).map(|_| Decodable::decode(decoder)).collect();
287         decoder.tcx().intern_canonical_var_infos(interned.as_slice())
288     }
289 }
290
291 impl<'tcx, D: TyDecoder<'tcx>> Decodable<D> for AllocId {
292     fn decode(decoder: &mut D) -> Self {
293         decoder.decode_alloc_id()
294     }
295 }
296
297 impl<'tcx, D: TyDecoder<'tcx>> Decodable<D> for ty::SymbolName<'tcx> {
298     fn decode(decoder: &mut D) -> Self {
299         ty::SymbolName::new(decoder.tcx(), &decoder.read_str())
300     }
301 }
302
303 macro_rules! impl_decodable_via_ref {
304     ($($t:ty),+) => {
305         $(impl<'tcx, D: TyDecoder<'tcx>> Decodable<D> for $t {
306             fn decode(decoder: &mut D) -> Self {
307                 RefDecodable::decode(decoder)
308             }
309         })*
310     }
311 }
312
313 impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for ty::List<Ty<'tcx>> {
314     fn decode(decoder: &mut D) -> &'tcx Self {
315         let len = decoder.read_usize();
316         decoder.tcx().mk_type_list((0..len).map::<Ty<'tcx>, _>(|_| Decodable::decode(decoder)))
317     }
318 }
319
320 impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D>
321     for ty::List<ty::Binder<'tcx, ty::ExistentialPredicate<'tcx>>>
322 {
323     fn decode(decoder: &mut D) -> &'tcx Self {
324         let len = decoder.read_usize();
325         decoder.tcx().mk_poly_existential_predicates(
326             (0..len).map::<ty::Binder<'tcx, _>, _>(|_| Decodable::decode(decoder)),
327         )
328     }
329 }
330
331 impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for ty::Const<'tcx> {
332     fn decode(decoder: &mut D) -> &'tcx Self {
333         decoder.tcx().mk_const(Decodable::decode(decoder))
334     }
335 }
336
337 impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for [ty::ValTree<'tcx>] {
338     fn decode(decoder: &mut D) -> &'tcx Self {
339         decoder.tcx().arena.alloc_from_iter(
340             (0..decoder.read_usize()).map(|_| Decodable::decode(decoder)).collect::<Vec<_>>(),
341         )
342     }
343 }
344
345 impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for Allocation {
346     fn decode(decoder: &mut D) -> &'tcx Self {
347         decoder.tcx().intern_const_alloc(Decodable::decode(decoder))
348     }
349 }
350
351 impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for [(ty::Predicate<'tcx>, Span)] {
352     fn decode(decoder: &mut D) -> &'tcx Self {
353         decoder.tcx().arena.alloc_from_iter(
354             (0..decoder.read_usize()).map(|_| Decodable::decode(decoder)).collect::<Vec<_>>(),
355         )
356     }
357 }
358
359 impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for [thir::abstract_const::Node<'tcx>] {
360     fn decode(decoder: &mut D) -> &'tcx Self {
361         decoder.tcx().arena.alloc_from_iter(
362             (0..decoder.read_usize()).map(|_| Decodable::decode(decoder)).collect::<Vec<_>>(),
363         )
364     }
365 }
366
367 impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for [thir::abstract_const::NodeId] {
368     fn decode(decoder: &mut D) -> &'tcx Self {
369         decoder.tcx().arena.alloc_from_iter(
370             (0..decoder.read_usize()).map(|_| Decodable::decode(decoder)).collect::<Vec<_>>(),
371         )
372     }
373 }
374
375 impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for ty::List<ty::BoundVariableKind> {
376     fn decode(decoder: &mut D) -> &'tcx Self {
377         let len = decoder.read_usize();
378         decoder.tcx().mk_bound_variable_kinds(
379             (0..len).map::<ty::BoundVariableKind, _>(|_| Decodable::decode(decoder)),
380         )
381     }
382 }
383
384 impl_decodable_via_ref! {
385     &'tcx ty::TypeckResults<'tcx>,
386     &'tcx ty::List<Ty<'tcx>>,
387     &'tcx ty::List<ty::Binder<'tcx, ty::ExistentialPredicate<'tcx>>>,
388     &'tcx Allocation,
389     &'tcx mir::Body<'tcx>,
390     &'tcx mir::UnsafetyCheckResult,
391     &'tcx mir::BorrowCheckResult<'tcx>,
392     &'tcx mir::coverage::CodeRegion,
393     &'tcx ty::List<ty::BoundVariableKind>,
394     &'tcx ty::AdtDef
395 }
396
397 #[macro_export]
398 macro_rules! __impl_decoder_methods {
399     ($($name:ident -> $ty:ty;)*) => {
400         $(
401             #[inline]
402             fn $name(&mut self) -> $ty {
403                 self.opaque.$name()
404             }
405         )*
406     }
407 }
408
409 macro_rules! impl_arena_allocatable_decoder {
410     ([]$args:tt) => {};
411     ([decode $(, $attrs:ident)*]
412      [$name:ident: $ty:ty]) => {
413         impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for $ty {
414             #[inline]
415             fn decode(decoder: &mut D) -> &'tcx Self {
416                 decode_arena_allocable(decoder)
417             }
418         }
419
420         impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for [$ty] {
421             #[inline]
422             fn decode(decoder: &mut D) -> &'tcx Self {
423                 decode_arena_allocable_slice(decoder)
424             }
425         }
426     };
427     ([$ignore:ident $(, $attrs:ident)*]$args:tt) => {
428         impl_arena_allocatable_decoder!([$($attrs),*]$args);
429     };
430 }
431
432 macro_rules! impl_arena_allocatable_decoders {
433     ([$($a:tt $name:ident: $ty:ty,)*]) => {
434         $(
435             impl_arena_allocatable_decoder!($a [$name: $ty]);
436         )*
437     }
438 }
439
440 rustc_hir::arena_types!(impl_arena_allocatable_decoders);
441 arena_types!(impl_arena_allocatable_decoders);
442
443 #[macro_export]
444 macro_rules! implement_ty_decoder {
445     ($DecoderName:ident <$($typaram:tt),*>) => {
446         mod __ty_decoder_impl {
447             use std::borrow::Cow;
448             use rustc_serialize::Decoder;
449
450             use super::$DecoderName;
451
452             impl<$($typaram ),*> Decoder for $DecoderName<$($typaram),*> {
453                 $crate::__impl_decoder_methods! {
454                     read_unit -> ();
455
456                     read_u128 -> u128;
457                     read_u64 -> u64;
458                     read_u32 -> u32;
459                     read_u16 -> u16;
460                     read_u8 -> u8;
461                     read_usize -> usize;
462
463                     read_i128 -> i128;
464                     read_i64 -> i64;
465                     read_i32 -> i32;
466                     read_i16 -> i16;
467                     read_i8 -> i8;
468                     read_isize -> isize;
469
470                     read_bool -> bool;
471                     read_f64 -> f64;
472                     read_f32 -> f32;
473                     read_char -> char;
474                     read_str -> Cow<'_, str>;
475                 }
476
477                 #[inline]
478                 fn read_raw_bytes_into(&mut self, bytes: &mut [u8]) {
479                     self.opaque.read_raw_bytes_into(bytes)
480                 }
481             }
482         }
483     }
484 }
485
486 macro_rules! impl_binder_encode_decode {
487     ($($t:ty),+ $(,)?) => {
488         $(
489             impl<'tcx, E: TyEncoder<'tcx>> Encodable<E> for ty::Binder<'tcx, $t> {
490                 fn encode(&self, e: &mut E) -> Result<(), E::Error> {
491                     self.bound_vars().encode(e)?;
492                     self.as_ref().skip_binder().encode(e)
493                 }
494             }
495             impl<'tcx, D: TyDecoder<'tcx>> Decodable<D> for ty::Binder<'tcx, $t> {
496                 fn decode(decoder: &mut D) -> Self {
497                     let bound_vars = Decodable::decode(decoder);
498                     ty::Binder::bind_with_vars(Decodable::decode(decoder), bound_vars)
499                 }
500             }
501         )*
502     }
503 }
504
505 impl_binder_encode_decode! {
506     &'tcx ty::List<Ty<'tcx>>,
507     ty::FnSig<'tcx>,
508     ty::ExistentialPredicate<'tcx>,
509     ty::TraitRef<'tcx>,
510     Vec<ty::GeneratorInteriorTypeCause<'tcx>>,
511 }