]> git.lizzy.rs Git - rust.git/blob - src/librustc/ty/query/on_disk_cache.rs
1f0f6bee77795b6d7049a7d686b04a8b75faf3f0
[rust.git] / src / librustc / ty / query / on_disk_cache.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 use dep_graph::{DepNodeIndex, SerializedDepNodeIndex};
12 use errors::Diagnostic;
13 use hir;
14 use hir::def_id::{CrateNum, DefIndex, DefId, LocalDefId,
15                   RESERVED_FOR_INCR_COMP_CACHE, LOCAL_CRATE};
16 use hir::map::definitions::DefPathHash;
17 use ich::{CachingCodemapView, Fingerprint};
18 use mir::{self, interpret};
19 use mir::interpret::{AllocDecodingSession, AllocDecodingState};
20 use rustc_data_structures::fx::FxHashMap;
21 use rustc_data_structures::sync::{Lrc, Lock, HashMapExt, Once};
22 use rustc_data_structures::indexed_vec::{IndexVec, Idx};
23 use rustc_serialize::{Decodable, Decoder, Encodable, Encoder, opaque,
24                       SpecializedDecoder, SpecializedEncoder,
25                       UseSpecializedDecodable, UseSpecializedEncodable};
26 use session::{CrateDisambiguator, Session};
27 use std::mem;
28 use syntax::ast::NodeId;
29 use syntax::codemap::{SourceMap, StableFilemapId};
30 use syntax_pos::{BytePos, Span, DUMMY_SP, FileMap};
31 use syntax_pos::hygiene::{Mark, SyntaxContext, ExpnInfo};
32 use ty;
33 use ty::codec::{self as ty_codec, TyDecoder, TyEncoder};
34 use ty::context::TyCtxt;
35 use util::common::time;
36
37 const TAG_FILE_FOOTER: u128 = 0xC0FFEE_C0FFEE_C0FFEE_C0FFEE_C0FFEE;
38
39 const TAG_CLEAR_CROSS_CRATE_CLEAR: u8 = 0;
40 const TAG_CLEAR_CROSS_CRATE_SET: u8 = 1;
41
42 const TAG_NO_EXPANSION_INFO: u8 = 0;
43 const TAG_EXPANSION_INFO_SHORTHAND: u8 = 1;
44 const TAG_EXPANSION_INFO_INLINE: u8 = 2;
45
46 const TAG_VALID_SPAN: u8 = 0;
47 const TAG_INVALID_SPAN: u8 = 1;
48
49 /// `OnDiskCache` provides an interface to incr. comp. data cached from the
50 /// previous compilation session. This data will eventually include the results
51 /// of a few selected queries (like `typeck_tables_of` and `mir_optimized`) and
52 /// any diagnostics that have been emitted during a query.
53 pub struct OnDiskCache<'sess> {
54
55     // The complete cache data in serialized form.
56     serialized_data: Vec<u8>,
57
58     // This field collects all Diagnostics emitted during the current
59     // compilation session.
60     current_diagnostics: Lock<FxHashMap<DepNodeIndex, Vec<Diagnostic>>>,
61
62     prev_cnums: Vec<(u32, String, CrateDisambiguator)>,
63     cnum_map: Once<IndexVec<CrateNum, Option<CrateNum>>>,
64
65     codemap: &'sess SourceMap,
66     file_index_to_stable_id: FxHashMap<FileMapIndex, StableFilemapId>,
67
68     // These two fields caches that are populated lazily during decoding.
69     file_index_to_file: Lock<FxHashMap<FileMapIndex, Lrc<FileMap>>>,
70     synthetic_expansion_infos: Lock<FxHashMap<AbsoluteBytePos, SyntaxContext>>,
71
72     // A map from dep-node to the position of the cached query result in
73     // `serialized_data`.
74     query_result_index: FxHashMap<SerializedDepNodeIndex, AbsoluteBytePos>,
75
76     // A map from dep-node to the position of any associated diagnostics in
77     // `serialized_data`.
78     prev_diagnostics_index: FxHashMap<SerializedDepNodeIndex, AbsoluteBytePos>,
79
80     alloc_decoding_state: AllocDecodingState,
81 }
82
83 // This type is used only for (de-)serialization.
84 #[derive(RustcEncodable, RustcDecodable)]
85 struct Footer {
86     file_index_to_stable_id: FxHashMap<FileMapIndex, StableFilemapId>,
87     prev_cnums: Vec<(u32, String, CrateDisambiguator)>,
88     query_result_index: EncodedQueryResultIndex,
89     diagnostics_index: EncodedQueryResultIndex,
90     // the location of all allocations
91     interpret_alloc_index: Vec<u32>,
92 }
93
94 type EncodedQueryResultIndex = Vec<(SerializedDepNodeIndex, AbsoluteBytePos)>;
95 type EncodedDiagnosticsIndex = Vec<(SerializedDepNodeIndex, AbsoluteBytePos)>;
96 type EncodedDiagnostics = Vec<Diagnostic>;
97
98 #[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, RustcEncodable, RustcDecodable)]
99 struct FileMapIndex(u32);
100
101 #[derive(Copy, Clone, Debug, Hash, Eq, PartialEq, RustcEncodable, RustcDecodable)]
102 struct AbsoluteBytePos(u32);
103
104 impl AbsoluteBytePos {
105     fn new(pos: usize) -> AbsoluteBytePos {
106         debug_assert!(pos <= ::std::u32::MAX as usize);
107         AbsoluteBytePos(pos as u32)
108     }
109
110     fn to_usize(self) -> usize {
111         self.0 as usize
112     }
113 }
114
115 impl<'sess> OnDiskCache<'sess> {
116     /// Create a new OnDiskCache instance from the serialized data in `data`.
117     pub fn new(sess: &'sess Session, data: Vec<u8>, start_pos: usize) -> OnDiskCache<'sess> {
118         debug_assert!(sess.opts.incremental.is_some());
119
120         // Wrapping in a scope so we can borrow `data`
121         let footer: Footer = {
122             let mut decoder = opaque::Decoder::new(&data[..], start_pos);
123
124             // Decode the *position* of the footer which can be found in the
125             // last 8 bytes of the file.
126             decoder.set_position(data.len() - IntEncodedWithFixedSize::ENCODED_SIZE);
127             let query_result_index_pos = IntEncodedWithFixedSize::decode(&mut decoder)
128                 .expect("Error while trying to decode query result index position.")
129                 .0 as usize;
130
131             // Decoder the file footer which contains all the lookup tables, etc.
132             decoder.set_position(query_result_index_pos);
133             decode_tagged(&mut decoder, TAG_FILE_FOOTER)
134                 .expect("Error while trying to decode query result index position.")
135         };
136
137         OnDiskCache {
138             serialized_data: data,
139             file_index_to_stable_id: footer.file_index_to_stable_id,
140             file_index_to_file: Lock::new(FxHashMap()),
141             prev_cnums: footer.prev_cnums,
142             cnum_map: Once::new(),
143             codemap: sess.codemap(),
144             current_diagnostics: Lock::new(FxHashMap()),
145             query_result_index: footer.query_result_index.into_iter().collect(),
146             prev_diagnostics_index: footer.diagnostics_index.into_iter().collect(),
147             synthetic_expansion_infos: Lock::new(FxHashMap()),
148             alloc_decoding_state: AllocDecodingState::new(footer.interpret_alloc_index),
149         }
150     }
151
152     pub fn new_empty(codemap: &'sess SourceMap) -> OnDiskCache<'sess> {
153         OnDiskCache {
154             serialized_data: Vec::new(),
155             file_index_to_stable_id: FxHashMap(),
156             file_index_to_file: Lock::new(FxHashMap()),
157             prev_cnums: vec![],
158             cnum_map: Once::new(),
159             codemap,
160             current_diagnostics: Lock::new(FxHashMap()),
161             query_result_index: FxHashMap(),
162             prev_diagnostics_index: FxHashMap(),
163             synthetic_expansion_infos: Lock::new(FxHashMap()),
164             alloc_decoding_state: AllocDecodingState::new(Vec::new()),
165         }
166     }
167
168     pub fn serialize<'a, 'tcx, E>(&self,
169                                   tcx: TyCtxt<'a, 'tcx, 'tcx>,
170                                   encoder: &mut E)
171                                   -> Result<(), E::Error>
172         where E: ty_codec::TyEncoder
173      {
174         // Serializing the DepGraph should not modify it:
175         tcx.dep_graph.with_ignore(|| {
176             // Allocate FileMapIndices
177             let (file_to_file_index, file_index_to_stable_id) = {
178                 let mut file_to_file_index = FxHashMap();
179                 let mut file_index_to_stable_id = FxHashMap();
180
181                 for (index, file) in tcx.sess.codemap().files().iter().enumerate() {
182                     let index = FileMapIndex(index as u32);
183                     let file_ptr: *const FileMap = &**file as *const _;
184                     file_to_file_index.insert(file_ptr, index);
185                     file_index_to_stable_id.insert(index, StableFilemapId::new(&file));
186                 }
187
188                 (file_to_file_index, file_index_to_stable_id)
189             };
190
191             let mut encoder = CacheEncoder {
192                 tcx,
193                 encoder,
194                 type_shorthands: FxHashMap(),
195                 predicate_shorthands: FxHashMap(),
196                 expn_info_shorthands: FxHashMap(),
197                 interpret_allocs: FxHashMap(),
198                 interpret_allocs_inverse: Vec::new(),
199                 codemap: CachingCodemapView::new(tcx.sess.codemap()),
200                 file_to_file_index,
201             };
202
203             // Load everything into memory so we can write it out to the on-disk
204             // cache. The vast majority of cacheable query results should already
205             // be in memory, so this should be a cheap operation.
206             tcx.dep_graph.exec_cache_promotions(tcx);
207
208             // Encode query results
209             let mut query_result_index = EncodedQueryResultIndex::new();
210
211             time(tcx.sess, "encode query results", || {
212                 use ty::query::queries::*;
213                 let enc = &mut encoder;
214                 let qri = &mut query_result_index;
215
216                 encode_query_results::<type_of, _>(tcx, enc, qri)?;
217                 encode_query_results::<generics_of, _>(tcx, enc, qri)?;
218                 encode_query_results::<predicates_of, _>(tcx, enc, qri)?;
219                 encode_query_results::<used_trait_imports, _>(tcx, enc, qri)?;
220                 encode_query_results::<typeck_tables_of, _>(tcx, enc, qri)?;
221                 encode_query_results::<codegen_fulfill_obligation, _>(tcx, enc, qri)?;
222                 encode_query_results::<optimized_mir, _>(tcx, enc, qri)?;
223                 encode_query_results::<unsafety_check_result, _>(tcx, enc, qri)?;
224                 encode_query_results::<borrowck, _>(tcx, enc, qri)?;
225                 encode_query_results::<mir_borrowck, _>(tcx, enc, qri)?;
226                 encode_query_results::<mir_const_qualif, _>(tcx, enc, qri)?;
227                 encode_query_results::<def_symbol_name, _>(tcx, enc, qri)?;
228                 encode_query_results::<const_is_rvalue_promotable_to_static, _>(tcx, enc, qri)?;
229                 encode_query_results::<symbol_name, _>(tcx, enc, qri)?;
230                 encode_query_results::<check_match, _>(tcx, enc, qri)?;
231                 encode_query_results::<codegen_fn_attrs, _>(tcx, enc, qri)?;
232                 encode_query_results::<specialization_graph_of, _>(tcx, enc, qri)?;
233
234                 // const eval is special, it only encodes successfully evaluated constants
235                 use ty::query::QueryAccessors;
236                 let cache = const_eval::query_cache(tcx).borrow();
237                 assert!(cache.active.is_empty());
238                 for (key, entry) in cache.results.iter() {
239                     use ty::query::config::QueryDescription;
240                     if const_eval::cache_on_disk(key.clone()) {
241                         if let Ok(ref value) = entry.value {
242                             let dep_node = SerializedDepNodeIndex::new(entry.index.index());
243
244                             // Record position of the cache entry
245                             qri.push((dep_node, AbsoluteBytePos::new(enc.position())));
246
247                             // Encode the type check tables with the SerializedDepNodeIndex
248                             // as tag.
249                             enc.encode_tagged(dep_node, value)?;
250                         }
251                     }
252                 }
253
254                 Ok(())
255             })?;
256
257             // Encode diagnostics
258             let diagnostics_index = {
259                 let mut diagnostics_index = EncodedDiagnosticsIndex::new();
260
261                 for (dep_node_index, diagnostics) in self.current_diagnostics
262                                                         .borrow()
263                                                         .iter() {
264                     let pos = AbsoluteBytePos::new(encoder.position());
265                     // Let's make sure we get the expected type here:
266                     let diagnostics: &EncodedDiagnostics = diagnostics;
267                     let dep_node_index =
268                         SerializedDepNodeIndex::new(dep_node_index.index());
269                     encoder.encode_tagged(dep_node_index, diagnostics)?;
270                     diagnostics_index.push((dep_node_index, pos));
271                 }
272
273                 diagnostics_index
274             };
275
276             let interpret_alloc_index = {
277                 let mut interpret_alloc_index = Vec::new();
278                 let mut n = 0;
279                 loop {
280                     let new_n = encoder.interpret_allocs_inverse.len();
281                     // if we have found new ids, serialize those, too
282                     if n == new_n {
283                         // otherwise, abort
284                         break;
285                     }
286                     for idx in n..new_n {
287                         let id = encoder.interpret_allocs_inverse[idx];
288                         let pos = encoder.position() as u32;
289                         interpret_alloc_index.push(pos);
290                         interpret::specialized_encode_alloc_id(
291                             &mut encoder,
292                             tcx,
293                             id,
294                         )?;
295                     }
296                     n = new_n;
297                 }
298                 interpret_alloc_index
299             };
300
301             let sorted_cnums = sorted_cnums_including_local_crate(tcx);
302             let prev_cnums: Vec<_> = sorted_cnums.iter().map(|&cnum| {
303                 let crate_name = tcx.original_crate_name(cnum).as_str().to_string();
304                 let crate_disambiguator = tcx.crate_disambiguator(cnum);
305                 (cnum.as_u32(), crate_name, crate_disambiguator)
306             }).collect();
307
308             // Encode the file footer
309             let footer_pos = encoder.position() as u64;
310             encoder.encode_tagged(TAG_FILE_FOOTER, &Footer {
311                 file_index_to_stable_id,
312                 prev_cnums,
313                 query_result_index,
314                 diagnostics_index,
315                 interpret_alloc_index,
316             })?;
317
318             // Encode the position of the footer as the last 8 bytes of the
319             // file so we know where to look for it.
320             IntEncodedWithFixedSize(footer_pos).encode(encoder.encoder)?;
321
322             // DO NOT WRITE ANYTHING TO THE ENCODER AFTER THIS POINT! The address
323             // of the footer must be the last thing in the data stream.
324
325             return Ok(());
326
327             fn sorted_cnums_including_local_crate(tcx: TyCtxt) -> Vec<CrateNum> {
328                 let mut cnums = vec![LOCAL_CRATE];
329                 cnums.extend_from_slice(&tcx.crates()[..]);
330                 cnums.sort_unstable();
331                 // Just to be sure...
332                 cnums.dedup();
333                 cnums
334             }
335         })
336     }
337
338     /// Load a diagnostic emitted during the previous compilation session.
339     pub fn load_diagnostics<'a, 'tcx>(&self,
340                                       tcx: TyCtxt<'a, 'tcx, 'tcx>,
341                                       dep_node_index: SerializedDepNodeIndex)
342                                       -> Vec<Diagnostic> {
343         let diagnostics: Option<EncodedDiagnostics> = self.load_indexed(
344             tcx,
345             dep_node_index,
346             &self.prev_diagnostics_index,
347             "diagnostics");
348
349         diagnostics.unwrap_or(Vec::new())
350     }
351
352     /// Store a diagnostic emitted during the current compilation session.
353     /// Anything stored like this will be available via `load_diagnostics` in
354     /// the next compilation session.
355     pub fn store_diagnostics(&self,
356                              dep_node_index: DepNodeIndex,
357                              diagnostics: Vec<Diagnostic>) {
358         let mut current_diagnostics = self.current_diagnostics.borrow_mut();
359         let prev = current_diagnostics.insert(dep_node_index, diagnostics);
360         debug_assert!(prev.is_none());
361     }
362
363     /// Returns the cached query result if there is something in the cache for
364     /// the given SerializedDepNodeIndex. Otherwise returns None.
365     pub fn try_load_query_result<'tcx, T>(&self,
366                                           tcx: TyCtxt<'_, 'tcx, 'tcx>,
367                                           dep_node_index: SerializedDepNodeIndex)
368                                           -> Option<T>
369         where T: Decodable
370     {
371         self.load_indexed(tcx,
372                           dep_node_index,
373                           &self.query_result_index,
374                           "query result")
375     }
376
377     /// Store a diagnostic emitted during computation of an anonymous query.
378     /// Since many anonymous queries can share the same `DepNode`, we aggregate
379     /// them -- as opposed to regular queries where we assume that there is a
380     /// 1:1 relationship between query-key and `DepNode`.
381     pub fn store_diagnostics_for_anon_node(&self,
382                                            dep_node_index: DepNodeIndex,
383                                            mut diagnostics: Vec<Diagnostic>) {
384         let mut current_diagnostics = self.current_diagnostics.borrow_mut();
385
386         let x = current_diagnostics.entry(dep_node_index).or_insert_with(|| {
387             mem::replace(&mut diagnostics, Vec::new())
388         });
389
390         x.extend(diagnostics.into_iter());
391     }
392
393     fn load_indexed<'tcx, T>(&self,
394                              tcx: TyCtxt<'_, 'tcx, 'tcx>,
395                              dep_node_index: SerializedDepNodeIndex,
396                              index: &FxHashMap<SerializedDepNodeIndex,
397                                                AbsoluteBytePos>,
398                              debug_tag: &'static str)
399                              -> Option<T>
400         where T: Decodable
401     {
402         let pos = if let Some(&pos) = index.get(&dep_node_index) {
403             pos
404         } else {
405             return None
406         };
407
408         // Initialize the cnum_map using the value from the thread which finishes the closure first
409         self.cnum_map.init_nonlocking_same(|| {
410             Self::compute_cnum_map(tcx, &self.prev_cnums[..])
411         });
412
413         let mut decoder = CacheDecoder {
414             tcx,
415             opaque: opaque::Decoder::new(&self.serialized_data[..], pos.to_usize()),
416             codemap: self.codemap,
417             cnum_map: self.cnum_map.get(),
418             file_index_to_file: &self.file_index_to_file,
419             file_index_to_stable_id: &self.file_index_to_stable_id,
420             synthetic_expansion_infos: &self.synthetic_expansion_infos,
421             alloc_decoding_session: self.alloc_decoding_state.new_decoding_session(),
422         };
423
424         match decode_tagged(&mut decoder, dep_node_index) {
425             Ok(value) => {
426                 Some(value)
427             }
428             Err(e) => {
429                 bug!("Could not decode cached {}: {}", debug_tag, e)
430             }
431         }
432     }
433
434     // This function builds mapping from previous-session-CrateNum to
435     // current-session-CrateNum. There might be CrateNums from the previous
436     // Session that don't occur in the current one. For these, the mapping
437     // maps to None.
438     fn compute_cnum_map(tcx: TyCtxt,
439                         prev_cnums: &[(u32, String, CrateDisambiguator)])
440                         -> IndexVec<CrateNum, Option<CrateNum>>
441     {
442         tcx.dep_graph.with_ignore(|| {
443             let current_cnums = tcx.all_crate_nums(LOCAL_CRATE).iter().map(|&cnum| {
444                 let crate_name = tcx.original_crate_name(cnum)
445                                     .as_str()
446                                     .to_string();
447                 let crate_disambiguator = tcx.crate_disambiguator(cnum);
448                 ((crate_name, crate_disambiguator), cnum)
449             }).collect::<FxHashMap<_,_>>();
450
451             let map_size = prev_cnums.iter()
452                                     .map(|&(cnum, ..)| cnum)
453                                     .max()
454                                     .unwrap_or(0) + 1;
455             let mut map = IndexVec::new();
456             map.resize(map_size as usize, None);
457
458             for &(prev_cnum, ref crate_name, crate_disambiguator) in prev_cnums {
459                 let key = (crate_name.clone(), crate_disambiguator);
460                 map[CrateNum::from_u32(prev_cnum)] = current_cnums.get(&key).cloned();
461             }
462
463             map[LOCAL_CRATE] = Some(LOCAL_CRATE);
464             map
465         })
466     }
467 }
468
469
470 //- DECODING -------------------------------------------------------------------
471
472 /// A decoder that can read the incr. comp. cache. It is similar to the one
473 /// we use for crate metadata decoding in that it can rebase spans and
474 /// eventually will also handle things that contain `Ty` instances.
475 struct CacheDecoder<'a, 'tcx: 'a, 'x> {
476     tcx: TyCtxt<'a, 'tcx, 'tcx>,
477     opaque: opaque::Decoder<'x>,
478     codemap: &'x SourceMap,
479     cnum_map: &'x IndexVec<CrateNum, Option<CrateNum>>,
480     synthetic_expansion_infos: &'x Lock<FxHashMap<AbsoluteBytePos, SyntaxContext>>,
481     file_index_to_file: &'x Lock<FxHashMap<FileMapIndex, Lrc<FileMap>>>,
482     file_index_to_stable_id: &'x FxHashMap<FileMapIndex, StableFilemapId>,
483     alloc_decoding_session: AllocDecodingSession<'x>,
484 }
485
486 impl<'a, 'tcx, 'x> CacheDecoder<'a, 'tcx, 'x> {
487     fn file_index_to_file(&self, index: FileMapIndex) -> Lrc<FileMap> {
488         let CacheDecoder {
489             ref file_index_to_file,
490             ref file_index_to_stable_id,
491             ref codemap,
492             ..
493         } = *self;
494
495         file_index_to_file.borrow_mut().entry(index).or_insert_with(|| {
496             let stable_id = file_index_to_stable_id[&index];
497             codemap.filemap_by_stable_id(stable_id)
498                    .expect("Failed to lookup FileMap in new context.")
499         }).clone()
500     }
501 }
502
503 trait DecoderWithPosition: Decoder {
504     fn position(&self) -> usize;
505 }
506
507 impl<'enc> DecoderWithPosition for opaque::Decoder<'enc> {
508     fn position(&self) -> usize {
509         self.position()
510     }
511 }
512
513 impl<'a, 'tcx, 'x> DecoderWithPosition for CacheDecoder<'a, 'tcx, 'x> {
514     fn position(&self) -> usize {
515         self.opaque.position()
516     }
517 }
518
519 // Decode something that was encoded with encode_tagged() and verify that the
520 // tag matches and the correct amount of bytes was read.
521 fn decode_tagged<'a, 'tcx, D, T, V>(decoder: &mut D,
522                                     expected_tag: T)
523                                     -> Result<V, D::Error>
524     where T: Decodable + Eq + ::std::fmt::Debug,
525           V: Decodable,
526           D: DecoderWithPosition,
527           'tcx: 'a,
528 {
529     let start_pos = decoder.position();
530
531     let actual_tag = T::decode(decoder)?;
532     assert_eq!(actual_tag, expected_tag);
533     let value = V::decode(decoder)?;
534     let end_pos = decoder.position();
535
536     let expected_len: u64 = Decodable::decode(decoder)?;
537     assert_eq!((end_pos - start_pos) as u64, expected_len);
538
539     Ok(value)
540 }
541
542
543 impl<'a, 'tcx: 'a, 'x> ty_codec::TyDecoder<'a, 'tcx> for CacheDecoder<'a, 'tcx, 'x> {
544
545     #[inline]
546     fn tcx(&self) -> TyCtxt<'a, 'tcx, 'tcx> {
547         self.tcx
548     }
549
550     #[inline]
551     fn position(&self) -> usize {
552         self.opaque.position()
553     }
554
555     #[inline]
556     fn peek_byte(&self) -> u8 {
557         self.opaque.data[self.opaque.position()]
558     }
559
560     fn cached_ty_for_shorthand<F>(&mut self,
561                                   shorthand: usize,
562                                   or_insert_with: F)
563                                   -> Result<ty::Ty<'tcx>, Self::Error>
564         where F: FnOnce(&mut Self) -> Result<ty::Ty<'tcx>, Self::Error>
565     {
566         let tcx = self.tcx();
567
568         let cache_key = ty::CReaderCacheKey {
569             cnum: RESERVED_FOR_INCR_COMP_CACHE,
570             pos: shorthand,
571         };
572
573         if let Some(&ty) = tcx.rcache.borrow().get(&cache_key) {
574             return Ok(ty);
575         }
576
577         let ty = or_insert_with(self)?;
578         // This may overwrite the entry, but it should overwrite with the same value
579         tcx.rcache.borrow_mut().insert_same(cache_key, ty);
580         Ok(ty)
581     }
582
583     fn with_position<F, R>(&mut self, pos: usize, f: F) -> R
584         where F: FnOnce(&mut Self) -> R
585     {
586         debug_assert!(pos < self.opaque.data.len());
587
588         let new_opaque = opaque::Decoder::new(self.opaque.data, pos);
589         let old_opaque = mem::replace(&mut self.opaque, new_opaque);
590         let r = f(self);
591         self.opaque = old_opaque;
592         r
593     }
594
595     fn map_encoded_cnum_to_current(&self, cnum: CrateNum) -> CrateNum {
596         self.cnum_map[cnum].unwrap_or_else(|| {
597             bug!("Could not find new CrateNum for {:?}", cnum)
598         })
599     }
600 }
601
602 implement_ty_decoder!( CacheDecoder<'a, 'tcx, 'x> );
603
604 impl<'a, 'tcx, 'x> SpecializedDecoder<interpret::AllocId> for CacheDecoder<'a, 'tcx, 'x> {
605     fn specialized_decode(&mut self) -> Result<interpret::AllocId, Self::Error> {
606         let alloc_decoding_session = self.alloc_decoding_session;
607         alloc_decoding_session.decode_alloc_id(self)
608     }
609 }
610 impl<'a, 'tcx, 'x> SpecializedDecoder<Span> for CacheDecoder<'a, 'tcx, 'x> {
611     fn specialized_decode(&mut self) -> Result<Span, Self::Error> {
612         let tag: u8 = Decodable::decode(self)?;
613
614         if tag == TAG_INVALID_SPAN {
615             return Ok(DUMMY_SP);
616         } else {
617             debug_assert_eq!(tag, TAG_VALID_SPAN);
618         }
619
620         let file_lo_index = FileMapIndex::decode(self)?;
621         let line_lo = usize::decode(self)?;
622         let col_lo = BytePos::decode(self)?;
623         let len = BytePos::decode(self)?;
624
625         let file_lo = self.file_index_to_file(file_lo_index);
626         let lo = file_lo.lines[line_lo - 1] + col_lo;
627         let hi = lo + len;
628
629         let expn_info_tag = u8::decode(self)?;
630
631         let ctxt = match expn_info_tag {
632             TAG_NO_EXPANSION_INFO => {
633                 SyntaxContext::empty()
634             }
635             TAG_EXPANSION_INFO_INLINE => {
636                 let pos = AbsoluteBytePos::new(self.opaque.position());
637                 let expn_info: ExpnInfo = Decodable::decode(self)?;
638                 let ctxt = SyntaxContext::allocate_directly(expn_info);
639                 self.synthetic_expansion_infos.borrow_mut().insert(pos, ctxt);
640                 ctxt
641             }
642             TAG_EXPANSION_INFO_SHORTHAND => {
643                 let pos = AbsoluteBytePos::decode(self)?;
644                 let cached_ctxt = self.synthetic_expansion_infos
645                                       .borrow()
646                                       .get(&pos)
647                                       .cloned();
648
649                 if let Some(ctxt) = cached_ctxt {
650                     ctxt
651                 } else {
652                     let expn_info = self.with_position(pos.to_usize(), |this| {
653                          ExpnInfo::decode(this)
654                     })?;
655                     let ctxt = SyntaxContext::allocate_directly(expn_info);
656                     self.synthetic_expansion_infos.borrow_mut().insert(pos, ctxt);
657                     ctxt
658                 }
659             }
660             _ => {
661                 unreachable!()
662             }
663         };
664
665         Ok(Span::new(lo, hi, ctxt))
666     }
667 }
668
669 // This impl makes sure that we get a runtime error when we try decode a
670 // DefIndex that is not contained in a DefId. Such a case would be problematic
671 // because we would not know how to transform the DefIndex to the current
672 // context.
673 impl<'a, 'tcx, 'x> SpecializedDecoder<DefIndex> for CacheDecoder<'a, 'tcx, 'x> {
674     fn specialized_decode(&mut self) -> Result<DefIndex, Self::Error> {
675         bug!("Trying to decode DefIndex outside the context of a DefId")
676     }
677 }
678
679 // Both the CrateNum and the DefIndex of a DefId can change in between two
680 // compilation sessions. We use the DefPathHash, which is stable across
681 // sessions, to map the old DefId to the new one.
682 impl<'a, 'tcx, 'x> SpecializedDecoder<DefId> for CacheDecoder<'a, 'tcx, 'x> {
683     #[inline]
684     fn specialized_decode(&mut self) -> Result<DefId, Self::Error> {
685         // Load the DefPathHash which is was we encoded the DefId as.
686         let def_path_hash = DefPathHash::decode(self)?;
687
688         // Using the DefPathHash, we can lookup the new DefId
689         Ok(self.tcx().def_path_hash_to_def_id.as_ref().unwrap()[&def_path_hash])
690     }
691 }
692
693 impl<'a, 'tcx, 'x> SpecializedDecoder<LocalDefId> for CacheDecoder<'a, 'tcx, 'x> {
694     #[inline]
695     fn specialized_decode(&mut self) -> Result<LocalDefId, Self::Error> {
696         Ok(LocalDefId::from_def_id(DefId::decode(self)?))
697     }
698 }
699
700 impl<'a, 'tcx, 'x> SpecializedDecoder<hir::HirId> for CacheDecoder<'a, 'tcx, 'x> {
701     fn specialized_decode(&mut self) -> Result<hir::HirId, Self::Error> {
702         // Load the DefPathHash which is was we encoded the DefIndex as.
703         let def_path_hash = DefPathHash::decode(self)?;
704
705         // Use the DefPathHash to map to the current DefId.
706         let def_id = self.tcx()
707                          .def_path_hash_to_def_id
708                          .as_ref()
709                          .unwrap()[&def_path_hash];
710
711         debug_assert!(def_id.is_local());
712
713         // The ItemLocalId needs no remapping.
714         let local_id = hir::ItemLocalId::decode(self)?;
715
716         // Reconstruct the HirId and look up the corresponding NodeId in the
717         // context of the current session.
718         Ok(hir::HirId {
719             owner: def_id.index,
720             local_id
721         })
722     }
723 }
724
725 // NodeIds are not stable across compilation sessions, so we store them in their
726 // HirId representation. This allows use to map them to the current NodeId.
727 impl<'a, 'tcx, 'x> SpecializedDecoder<NodeId> for CacheDecoder<'a, 'tcx, 'x> {
728     #[inline]
729     fn specialized_decode(&mut self) -> Result<NodeId, Self::Error> {
730         let hir_id = hir::HirId::decode(self)?;
731         Ok(self.tcx().hir.hir_to_node_id(hir_id))
732     }
733 }
734
735 impl<'a, 'tcx, 'x> SpecializedDecoder<Fingerprint> for CacheDecoder<'a, 'tcx, 'x> {
736     fn specialized_decode(&mut self) -> Result<Fingerprint, Self::Error> {
737         Fingerprint::decode_opaque(&mut self.opaque)
738     }
739 }
740
741 impl<'a, 'tcx, 'x, T: Decodable> SpecializedDecoder<mir::ClearCrossCrate<T>>
742 for CacheDecoder<'a, 'tcx, 'x> {
743     #[inline]
744     fn specialized_decode(&mut self) -> Result<mir::ClearCrossCrate<T>, Self::Error> {
745         let discr = u8::decode(self)?;
746
747         match discr {
748             TAG_CLEAR_CROSS_CRATE_CLEAR => Ok(mir::ClearCrossCrate::Clear),
749             TAG_CLEAR_CROSS_CRATE_SET => {
750                 let val = T::decode(self)?;
751                 Ok(mir::ClearCrossCrate::Set(val))
752             }
753             _ => {
754                 unreachable!()
755             }
756         }
757     }
758 }
759
760 //- ENCODING -------------------------------------------------------------------
761
762 struct CacheEncoder<'enc, 'a, 'tcx, E>
763     where E: 'enc + ty_codec::TyEncoder,
764           'tcx: 'a,
765 {
766     tcx: TyCtxt<'a, 'tcx, 'tcx>,
767     encoder: &'enc mut E,
768     type_shorthands: FxHashMap<ty::Ty<'tcx>, usize>,
769     predicate_shorthands: FxHashMap<ty::Predicate<'tcx>, usize>,
770     expn_info_shorthands: FxHashMap<Mark, AbsoluteBytePos>,
771     interpret_allocs: FxHashMap<interpret::AllocId, usize>,
772     interpret_allocs_inverse: Vec<interpret::AllocId>,
773     codemap: CachingCodemapView<'tcx>,
774     file_to_file_index: FxHashMap<*const FileMap, FileMapIndex>,
775 }
776
777 impl<'enc, 'a, 'tcx, E> CacheEncoder<'enc, 'a, 'tcx, E>
778     where E: 'enc + ty_codec::TyEncoder
779 {
780     fn filemap_index(&mut self, filemap: Lrc<FileMap>) -> FileMapIndex {
781         self.file_to_file_index[&(&*filemap as *const FileMap)]
782     }
783
784     /// Encode something with additional information that allows to do some
785     /// sanity checks when decoding the data again. This method will first
786     /// encode the specified tag, then the given value, then the number of
787     /// bytes taken up by tag and value. On decoding, we can then verify that
788     /// we get the expected tag and read the expected number of bytes.
789     fn encode_tagged<T: Encodable, V: Encodable>(&mut self,
790                                                  tag: T,
791                                                  value: &V)
792                                                  -> Result<(), E::Error>
793     {
794         use ty::codec::TyEncoder;
795         let start_pos = self.position();
796
797         tag.encode(self)?;
798         value.encode(self)?;
799
800         let end_pos = self.position();
801         ((end_pos - start_pos) as u64).encode(self)
802     }
803 }
804
805 impl<'enc, 'a, 'tcx, E> SpecializedEncoder<interpret::AllocId> for CacheEncoder<'enc, 'a, 'tcx, E>
806     where E: 'enc + ty_codec::TyEncoder
807 {
808     fn specialized_encode(&mut self, alloc_id: &interpret::AllocId) -> Result<(), Self::Error> {
809         use std::collections::hash_map::Entry;
810         let index = match self.interpret_allocs.entry(*alloc_id) {
811             Entry::Occupied(e) => *e.get(),
812             Entry::Vacant(e) => {
813                 let idx = self.interpret_allocs_inverse.len();
814                 self.interpret_allocs_inverse.push(*alloc_id);
815                 e.insert(idx);
816                 idx
817             },
818         };
819
820         index.encode(self)
821     }
822 }
823
824 impl<'enc, 'a, 'tcx, E> SpecializedEncoder<Span> for CacheEncoder<'enc, 'a, 'tcx, E>
825     where E: 'enc + ty_codec::TyEncoder
826 {
827     fn specialized_encode(&mut self, span: &Span) -> Result<(), Self::Error> {
828
829         if *span == DUMMY_SP {
830             return TAG_INVALID_SPAN.encode(self);
831         }
832
833         let span_data = span.data();
834
835         if span_data.hi < span_data.lo {
836             return TAG_INVALID_SPAN.encode(self);
837         }
838
839         let (file_lo, line_lo, col_lo) = match self.codemap
840                                                    .byte_pos_to_line_and_col(span_data.lo) {
841             Some(pos) => pos,
842             None => {
843                 return TAG_INVALID_SPAN.encode(self);
844             }
845         };
846
847         if !file_lo.contains(span_data.hi) {
848             return TAG_INVALID_SPAN.encode(self);
849         }
850
851         let len = span_data.hi - span_data.lo;
852
853         let filemap_index = self.filemap_index(file_lo);
854
855         TAG_VALID_SPAN.encode(self)?;
856         filemap_index.encode(self)?;
857         line_lo.encode(self)?;
858         col_lo.encode(self)?;
859         len.encode(self)?;
860
861         if span_data.ctxt == SyntaxContext::empty() {
862             TAG_NO_EXPANSION_INFO.encode(self)
863         } else {
864             let mark = span_data.ctxt.outer();
865
866             if let Some(expn_info) = mark.expn_info() {
867                 if let Some(pos) = self.expn_info_shorthands.get(&mark).cloned() {
868                     TAG_EXPANSION_INFO_SHORTHAND.encode(self)?;
869                     pos.encode(self)
870                 } else {
871                     TAG_EXPANSION_INFO_INLINE.encode(self)?;
872                     let pos = AbsoluteBytePos::new(self.position());
873                     self.expn_info_shorthands.insert(mark, pos);
874                     expn_info.encode(self)
875                 }
876             } else {
877                 TAG_NO_EXPANSION_INFO.encode(self)
878             }
879         }
880     }
881 }
882
883 impl<'enc, 'a, 'tcx, E> ty_codec::TyEncoder for CacheEncoder<'enc, 'a, 'tcx, E>
884     where E: 'enc + ty_codec::TyEncoder
885 {
886     #[inline]
887     fn position(&self) -> usize {
888         self.encoder.position()
889     }
890 }
891
892 impl<'enc, 'a, 'tcx, E> SpecializedEncoder<CrateNum> for CacheEncoder<'enc, 'a, 'tcx, E>
893     where E: 'enc + ty_codec::TyEncoder
894 {
895     #[inline]
896     fn specialized_encode(&mut self, cnum: &CrateNum) -> Result<(), Self::Error> {
897         self.emit_u32(cnum.as_u32())
898     }
899 }
900
901 impl<'enc, 'a, 'tcx, E> SpecializedEncoder<ty::Ty<'tcx>> for CacheEncoder<'enc, 'a, 'tcx, E>
902     where E: 'enc + ty_codec::TyEncoder
903 {
904     #[inline]
905     fn specialized_encode(&mut self, ty: &ty::Ty<'tcx>) -> Result<(), Self::Error> {
906         ty_codec::encode_with_shorthand(self, ty,
907             |encoder| &mut encoder.type_shorthands)
908     }
909 }
910
911 impl<'enc, 'a, 'tcx, E> SpecializedEncoder<ty::GenericPredicates<'tcx>>
912     for CacheEncoder<'enc, 'a, 'tcx, E>
913     where E: 'enc + ty_codec::TyEncoder
914 {
915     #[inline]
916     fn specialized_encode(&mut self,
917                           predicates: &ty::GenericPredicates<'tcx>)
918                           -> Result<(), Self::Error> {
919         ty_codec::encode_predicates(self, predicates,
920             |encoder| &mut encoder.predicate_shorthands)
921     }
922 }
923
924 impl<'enc, 'a, 'tcx, E> SpecializedEncoder<hir::HirId> for CacheEncoder<'enc, 'a, 'tcx, E>
925     where E: 'enc + ty_codec::TyEncoder
926 {
927     #[inline]
928     fn specialized_encode(&mut self, id: &hir::HirId) -> Result<(), Self::Error> {
929         let hir::HirId {
930             owner,
931             local_id,
932         } = *id;
933
934         let def_path_hash = self.tcx.hir.definitions().def_path_hash(owner);
935
936         def_path_hash.encode(self)?;
937         local_id.encode(self)
938     }
939 }
940
941
942 impl<'enc, 'a, 'tcx, E> SpecializedEncoder<DefId> for CacheEncoder<'enc, 'a, 'tcx, E>
943     where E: 'enc + ty_codec::TyEncoder
944 {
945     #[inline]
946     fn specialized_encode(&mut self, id: &DefId) -> Result<(), Self::Error> {
947         let def_path_hash = self.tcx.def_path_hash(*id);
948         def_path_hash.encode(self)
949     }
950 }
951
952 impl<'enc, 'a, 'tcx, E> SpecializedEncoder<LocalDefId> for CacheEncoder<'enc, 'a, 'tcx, E>
953     where E: 'enc + ty_codec::TyEncoder
954 {
955     #[inline]
956     fn specialized_encode(&mut self, id: &LocalDefId) -> Result<(), Self::Error> {
957         id.to_def_id().encode(self)
958     }
959 }
960
961 impl<'enc, 'a, 'tcx, E> SpecializedEncoder<DefIndex> for CacheEncoder<'enc, 'a, 'tcx, E>
962     where E: 'enc + ty_codec::TyEncoder
963 {
964     fn specialized_encode(&mut self, _: &DefIndex) -> Result<(), Self::Error> {
965         bug!("Encoding DefIndex without context.")
966     }
967 }
968
969 // NodeIds are not stable across compilation sessions, so we store them in their
970 // HirId representation. This allows use to map them to the current NodeId.
971 impl<'enc, 'a, 'tcx, E> SpecializedEncoder<NodeId> for CacheEncoder<'enc, 'a, 'tcx, E>
972     where E: 'enc + ty_codec::TyEncoder
973 {
974     #[inline]
975     fn specialized_encode(&mut self, node_id: &NodeId) -> Result<(), Self::Error> {
976         let hir_id = self.tcx.hir.node_to_hir_id(*node_id);
977         hir_id.encode(self)
978     }
979 }
980
981 impl<'enc, 'a, 'tcx> SpecializedEncoder<Fingerprint>
982 for CacheEncoder<'enc, 'a, 'tcx, opaque::Encoder>
983 {
984     fn specialized_encode(&mut self, f: &Fingerprint) -> Result<(), Self::Error> {
985         f.encode_opaque(&mut self.encoder)
986     }
987 }
988
989 impl<'enc, 'a, 'tcx, E, T> SpecializedEncoder<mir::ClearCrossCrate<T>>
990 for CacheEncoder<'enc, 'a, 'tcx, E>
991     where E: 'enc + ty_codec::TyEncoder,
992           T: Encodable,
993 {
994     #[inline]
995     fn specialized_encode(&mut self,
996                           val: &mir::ClearCrossCrate<T>)
997                           -> Result<(), Self::Error> {
998         match *val {
999             mir::ClearCrossCrate::Clear => {
1000                 TAG_CLEAR_CROSS_CRATE_CLEAR.encode(self)
1001             }
1002             mir::ClearCrossCrate::Set(ref val) => {
1003                 TAG_CLEAR_CROSS_CRATE_SET.encode(self)?;
1004                 val.encode(self)
1005             }
1006         }
1007     }
1008 }
1009
1010 macro_rules! encoder_methods {
1011     ($($name:ident($ty:ty);)*) => {
1012         $(fn $name(&mut self, value: $ty) -> Result<(), Self::Error> {
1013             self.encoder.$name(value)
1014         })*
1015     }
1016 }
1017
1018 impl<'enc, 'a, 'tcx, E> Encoder for CacheEncoder<'enc, 'a, 'tcx, E>
1019     where E: 'enc + ty_codec::TyEncoder
1020 {
1021     type Error = E::Error;
1022
1023     fn emit_nil(&mut self) -> Result<(), Self::Error> {
1024         Ok(())
1025     }
1026
1027     encoder_methods! {
1028         emit_usize(usize);
1029         emit_u128(u128);
1030         emit_u64(u64);
1031         emit_u32(u32);
1032         emit_u16(u16);
1033         emit_u8(u8);
1034
1035         emit_isize(isize);
1036         emit_i128(i128);
1037         emit_i64(i64);
1038         emit_i32(i32);
1039         emit_i16(i16);
1040         emit_i8(i8);
1041
1042         emit_bool(bool);
1043         emit_f64(f64);
1044         emit_f32(f32);
1045         emit_char(char);
1046         emit_str(&str);
1047     }
1048 }
1049
1050 // An integer that will always encode to 8 bytes.
1051 struct IntEncodedWithFixedSize(u64);
1052
1053 impl IntEncodedWithFixedSize {
1054     pub const ENCODED_SIZE: usize = 8;
1055 }
1056
1057 impl UseSpecializedEncodable for IntEncodedWithFixedSize {}
1058 impl UseSpecializedDecodable for IntEncodedWithFixedSize {}
1059
1060 impl SpecializedEncoder<IntEncodedWithFixedSize> for opaque::Encoder {
1061     fn specialized_encode(&mut self, x: &IntEncodedWithFixedSize) -> Result<(), Self::Error> {
1062         let start_pos = self.position();
1063         for i in 0 .. IntEncodedWithFixedSize::ENCODED_SIZE {
1064             ((x.0 >> i * 8) as u8).encode(self)?;
1065         }
1066         let end_pos = self.position();
1067         assert_eq!((end_pos - start_pos), IntEncodedWithFixedSize::ENCODED_SIZE);
1068         Ok(())
1069     }
1070 }
1071
1072 impl<'enc> SpecializedDecoder<IntEncodedWithFixedSize> for opaque::Decoder<'enc> {
1073     fn specialized_decode(&mut self) -> Result<IntEncodedWithFixedSize, Self::Error> {
1074         let mut value: u64 = 0;
1075         let start_pos = self.position();
1076
1077         for i in 0 .. IntEncodedWithFixedSize::ENCODED_SIZE {
1078             let byte: u8 = Decodable::decode(self)?;
1079             value |= (byte as u64) << (i * 8);
1080         }
1081
1082         let end_pos = self.position();
1083         assert_eq!((end_pos - start_pos), IntEncodedWithFixedSize::ENCODED_SIZE);
1084
1085         Ok(IntEncodedWithFixedSize(value))
1086     }
1087 }
1088
1089 fn encode_query_results<'enc, 'a, 'tcx, Q, E>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
1090                                               encoder: &mut CacheEncoder<'enc, 'a, 'tcx, E>,
1091                                               query_result_index: &mut EncodedQueryResultIndex)
1092                                               -> Result<(), E::Error>
1093     where Q: super::config::QueryDescription<'tcx>,
1094           E: 'enc + TyEncoder,
1095           Q::Value: Encodable,
1096 {
1097     let desc = &format!("encode_query_results for {}",
1098         unsafe { ::std::intrinsics::type_name::<Q>() });
1099
1100     time(tcx.sess, desc, || {
1101
1102     let map = Q::query_cache(tcx).borrow();
1103     assert!(map.active.is_empty());
1104     for (key, entry) in map.results.iter() {
1105         if Q::cache_on_disk(key.clone()) {
1106             let dep_node = SerializedDepNodeIndex::new(entry.index.index());
1107
1108             // Record position of the cache entry
1109             query_result_index.push((dep_node, AbsoluteBytePos::new(encoder.position())));
1110
1111             // Encode the type check tables with the SerializedDepNodeIndex
1112             // as tag.
1113             encoder.encode_tagged(dep_node, &entry.value)?;
1114         }
1115     }
1116
1117     Ok(())
1118     })
1119 }