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