|
6 | 6 | // The functionality in here is shared between persisting to crate metadata and
|
7 | 7 | // persisting to incr. comp. caches.
|
8 | 8 |
|
| 9 | +use crate::arena::ArenaAllocatable; |
9 | 10 | use crate::hir::def_id::{DefId, CrateNum};
|
10 | 11 | use crate::infer::canonical::{CanonicalVarInfo, CanonicalVarInfos};
|
11 | 12 | use rustc_data_structures::fx::FxHashMap;
|
@@ -130,6 +131,26 @@ pub trait TyDecoder<'a, 'tcx: 'a>: Decoder {
|
130 | 131 | }
|
131 | 132 | }
|
132 | 133 |
|
| 134 | +#[inline] |
| 135 | +pub fn decode_arena_allocable<'a, 'tcx, D, T: ArenaAllocatable<'tcx> + Decodable>( |
| 136 | + decoder: &mut D |
| 137 | +) -> Result<&'tcx T, D::Error> |
| 138 | + where D: TyDecoder<'a, 'tcx>, |
| 139 | + 'tcx: 'a, |
| 140 | +{ |
| 141 | + Ok(decoder.tcx().arena.alloc(Decodable::decode(decoder)?)) |
| 142 | +} |
| 143 | + |
| 144 | +#[inline] |
| 145 | +pub fn decode_arena_allocable_slice<'a, 'tcx, D, T: ArenaAllocatable<'tcx> + Decodable>( |
| 146 | + decoder: &mut D |
| 147 | +) -> Result<&'tcx [T], D::Error> |
| 148 | + where D: TyDecoder<'a, 'tcx>, |
| 149 | + 'tcx: 'a, |
| 150 | +{ |
| 151 | + Ok(decoder.tcx().arena.alloc_from_iter(<Vec<T> as Decodable>::decode(decoder)?)) |
| 152 | +} |
| 153 | + |
133 | 154 | #[inline]
|
134 | 155 | pub fn decode_cnum<'a, 'tcx, D>(decoder: &mut D) -> Result<CrateNum, D::Error>
|
135 | 156 | where D: TyDecoder<'a, 'tcx>,
|
@@ -273,6 +294,35 @@ macro_rules! __impl_decoder_methods {
|
273 | 294 | }
|
274 | 295 | }
|
275 | 296 |
|
| 297 | +#[macro_export] |
| 298 | +macro_rules! impl_arena_allocatable_decoder { |
| 299 | + ([$DecoderName:ident [$($typaram:tt),*]], [[decode] $name:ident: $ty:ty], $tcx:lifetime) => { |
| 300 | + impl<$($typaram),*> SpecializedDecoder<&$tcx $ty> for $DecoderName<$($typaram),*> { |
| 301 | + #[inline] |
| 302 | + fn specialized_decode(&mut self) -> Result<&$tcx $ty, Self::Error> { |
| 303 | + decode_arena_allocable(self) |
| 304 | + } |
| 305 | + } |
| 306 | + |
| 307 | + impl<$($typaram),*> SpecializedDecoder<&$tcx [$ty]> for $DecoderName<$($typaram),*> { |
| 308 | + #[inline] |
| 309 | + fn specialized_decode(&mut self) -> Result<&$tcx [$ty], Self::Error> { |
| 310 | + decode_arena_allocable_slice(self) |
| 311 | + } |
| 312 | + } |
| 313 | + }; |
| 314 | + ([$DecoderName:ident [$($typaram:tt),*]], [[] $name:ident: $ty:ty], $tcx:lifetime) => {}; |
| 315 | +} |
| 316 | + |
| 317 | +#[macro_export] |
| 318 | +macro_rules! impl_arena_allocatable_decoders { |
| 319 | + ($args:tt, [$($a:tt $name:ident: $ty:ty,)*], $tcx:lifetime) => { |
| 320 | + $( |
| 321 | + impl_arena_allocatable_decoder!($args, [$a $name: $ty], $tcx); |
| 322 | + )* |
| 323 | + } |
| 324 | +} |
| 325 | + |
276 | 326 | #[macro_export]
|
277 | 327 | macro_rules! implement_ty_decoder {
|
278 | 328 | ($DecoderName:ident <$($typaram:tt),*>) => {
|
@@ -322,6 +372,8 @@ macro_rules! implement_ty_decoder {
|
322 | 372 | // the caller to pick any lifetime for 'tcx, including 'static,
|
323 | 373 | // by using the unspecialized proxies to them.
|
324 | 374 |
|
| 375 | + arena_types!(impl_arena_allocatable_decoders, [$DecoderName [$($typaram),*]], 'tcx); |
| 376 | + |
325 | 377 | impl<$($typaram),*> SpecializedDecoder<CrateNum>
|
326 | 378 | for $DecoderName<$($typaram),*> {
|
327 | 379 | fn specialized_decode(&mut self) -> Result<CrateNum, Self::Error> {
|
|
0 commit comments