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