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.
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.
11 use dep_graph::{DepNodeIndex, SerializedDepNodeIndex};
12 use errors::Diagnostic;
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};
28 use syntax::ast::NodeId;
29 use syntax::codemap::{CodeMap, StableFilemapId};
30 use syntax_pos::{BytePos, Span, DUMMY_SP, FileMap};
31 use syntax_pos::hygiene::{Mark, SyntaxContext, ExpnInfo};
33 use ty::codec::{self as ty_codec, TyDecoder, TyEncoder};
34 use ty::context::TyCtxt;
35 use util::common::time;
37 const TAG_FILE_FOOTER: u128 = 0xC0FFEE_C0FFEE_C0FFEE_C0FFEE_C0FFEE;
39 const TAG_CLEAR_CROSS_CRATE_CLEAR: u8 = 0;
40 const TAG_CLEAR_CROSS_CRATE_SET: u8 = 1;
42 const TAG_NO_EXPANSION_INFO: u8 = 0;
43 const TAG_EXPANSION_INFO_SHORTHAND: u8 = 1;
44 const TAG_EXPANSION_INFO_INLINE: u8 = 2;
46 const TAG_VALID_SPAN: u8 = 0;
47 const TAG_INVALID_SPAN: u8 = 1;
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> {
55 // The complete cache data in serialized form.
56 serialized_data: Vec<u8>,
58 // This field collects all Diagnostics emitted during the current
59 // compilation session.
60 current_diagnostics: Lock<FxHashMap<DepNodeIndex, Vec<Diagnostic>>>,
62 prev_cnums: Vec<(u32, String, CrateDisambiguator)>,
63 cnum_map: Once<IndexVec<CrateNum, Option<CrateNum>>>,
65 codemap: &'sess CodeMap,
66 file_index_to_stable_id: FxHashMap<FileMapIndex, StableFilemapId>,
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>>,
72 // A map from dep-node to the position of the cached query result in
74 query_result_index: FxHashMap<SerializedDepNodeIndex, AbsoluteBytePos>,
76 // A map from dep-node to the position of any associated diagnostics in
78 prev_diagnostics_index: FxHashMap<SerializedDepNodeIndex, AbsoluteBytePos>,
80 alloc_decoding_state: AllocDecodingState,
83 // This type is used only for (de-)serialization.
84 #[derive(RustcEncodable, RustcDecodable)]
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>,
94 type EncodedQueryResultIndex = Vec<(SerializedDepNodeIndex, AbsoluteBytePos)>;
95 type EncodedDiagnosticsIndex = Vec<(SerializedDepNodeIndex, AbsoluteBytePos)>;
96 type EncodedDiagnostics = Vec<Diagnostic>;
98 #[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, RustcEncodable, RustcDecodable)]
99 struct FileMapIndex(u32);
101 #[derive(Copy, Clone, Debug, Hash, Eq, PartialEq, RustcEncodable, RustcDecodable)]
102 struct AbsoluteBytePos(u32);
104 impl AbsoluteBytePos {
105 fn new(pos: usize) -> AbsoluteBytePos {
106 debug_assert!(pos <= ::std::u32::MAX as usize);
107 AbsoluteBytePos(pos as u32)
110 fn to_usize(self) -> usize {
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());
120 // Wrapping in a scope so we can borrow `data`
121 let footer: Footer = {
122 let mut decoder = opaque::Decoder::new(&data[..], start_pos);
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.")
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.")
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),
152 pub fn new_empty(codemap: &'sess CodeMap) -> OnDiskCache<'sess> {
154 serialized_data: Vec::new(),
155 file_index_to_stable_id: FxHashMap(),
156 file_index_to_file: Lock::new(FxHashMap()),
158 cnum_map: Once::new(),
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()),
168 pub fn serialize<'a, 'tcx, E>(&self,
169 tcx: TyCtxt<'a, 'tcx, 'tcx>,
171 -> Result<(), E::Error>
172 where E: ty_codec::TyEncoder
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();
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));
188 (file_to_file_index, file_index_to_stable_id)
191 let mut encoder = CacheEncoder {
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()),
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);
208 // Encode query results
209 let mut query_result_index = EncodedQueryResultIndex::new();
211 time(tcx.sess, "encode query results", || {
212 use ty::query::queries::*;
213 let enc = &mut encoder;
214 let qri = &mut query_result_index;
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)?;
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());
244 // Record position of the cache entry
245 qri.push((dep_node, AbsoluteBytePos::new(enc.position())));
247 // Encode the type check tables with the SerializedDepNodeIndex
249 enc.encode_tagged(dep_node, value)?;
257 // Encode diagnostics
258 let diagnostics_index = {
259 let mut diagnostics_index = EncodedDiagnosticsIndex::new();
261 for (dep_node_index, diagnostics) in self.current_diagnostics
264 let pos = AbsoluteBytePos::new(encoder.position());
265 // Let's make sure we get the expected type here:
266 let diagnostics: &EncodedDiagnostics = diagnostics;
268 SerializedDepNodeIndex::new(dep_node_index.index());
269 encoder.encode_tagged(dep_node_index, diagnostics)?;
270 diagnostics_index.push((dep_node_index, pos));
276 let interpret_alloc_index = {
277 let mut interpret_alloc_index = Vec::new();
280 let new_n = encoder.interpret_allocs_inverse.len();
281 // if we have found new ids, serialize those, too
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(
298 interpret_alloc_index
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)
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,
315 interpret_alloc_index,
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)?;
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.
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...
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)
343 let diagnostics: Option<EncodedDiagnostics> = self.load_indexed(
346 &self.prev_diagnostics_index,
349 diagnostics.unwrap_or(Vec::new())
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());
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)
371 self.load_indexed(tcx,
373 &self.query_result_index,
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();
386 let x = current_diagnostics.entry(dep_node_index).or_insert_with(|| {
387 mem::replace(&mut diagnostics, Vec::new())
390 x.extend(diagnostics.into_iter());
393 fn load_indexed<'tcx, T>(&self,
394 tcx: TyCtxt<'_, 'tcx, 'tcx>,
395 dep_node_index: SerializedDepNodeIndex,
396 index: &FxHashMap<SerializedDepNodeIndex,
398 debug_tag: &'static str)
402 let pos = if let Some(&pos) = index.get(&dep_node_index) {
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[..])
413 let mut decoder = CacheDecoder {
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(),
424 match decode_tagged(&mut decoder, dep_node_index) {
429 bug!("Could not decode cached {}: {}", debug_tag, e)
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
438 fn compute_cnum_map(tcx: TyCtxt,
439 prev_cnums: &[(u32, String, CrateDisambiguator)])
440 -> IndexVec<CrateNum, Option<CrateNum>>
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)
447 let crate_disambiguator = tcx.crate_disambiguator(cnum);
448 ((crate_name, crate_disambiguator), cnum)
449 }).collect::<FxHashMap<_,_>>();
451 let map_size = prev_cnums.iter()
452 .map(|&(cnum, ..)| cnum)
455 let mut map = IndexVec::new();
456 map.resize(map_size as usize, None);
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();
463 map[LOCAL_CRATE] = Some(LOCAL_CRATE);
470 //- DECODING -------------------------------------------------------------------
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 CodeMap,
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>,
486 impl<'a, 'tcx, 'x> CacheDecoder<'a, 'tcx, 'x> {
487 fn file_index_to_file(&self, index: FileMapIndex) -> Lrc<FileMap> {
489 ref file_index_to_file,
490 ref file_index_to_stable_id,
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.")
503 trait DecoderWithPosition: Decoder {
504 fn position(&self) -> usize;
507 impl<'enc> DecoderWithPosition for opaque::Decoder<'enc> {
508 fn position(&self) -> usize {
513 impl<'a, 'tcx, 'x> DecoderWithPosition for CacheDecoder<'a, 'tcx, 'x> {
514 fn position(&self) -> usize {
515 self.opaque.position()
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,
523 -> Result<V, D::Error>
524 where T: Decodable + Eq + ::std::fmt::Debug,
526 D: DecoderWithPosition,
529 let start_pos = decoder.position();
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();
536 let expected_len: u64 = Decodable::decode(decoder)?;
537 assert_eq!((end_pos - start_pos) as u64, expected_len);
543 impl<'a, 'tcx: 'a, 'x> ty_codec::TyDecoder<'a, 'tcx> for CacheDecoder<'a, 'tcx, 'x> {
546 fn tcx(&self) -> TyCtxt<'a, 'tcx, 'tcx> {
551 fn position(&self) -> usize {
552 self.opaque.position()
556 fn peek_byte(&self) -> u8 {
557 self.opaque.data[self.opaque.position()]
560 fn cached_ty_for_shorthand<F>(&mut self,
563 -> Result<ty::Ty<'tcx>, Self::Error>
564 where F: FnOnce(&mut Self) -> Result<ty::Ty<'tcx>, Self::Error>
566 let tcx = self.tcx();
568 let cache_key = ty::CReaderCacheKey {
569 cnum: RESERVED_FOR_INCR_COMP_CACHE,
573 if let Some(&ty) = tcx.rcache.borrow().get(&cache_key) {
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);
583 fn with_position<F, R>(&mut self, pos: usize, f: F) -> R
584 where F: FnOnce(&mut Self) -> R
586 debug_assert!(pos < self.opaque.data.len());
588 let new_opaque = opaque::Decoder::new(self.opaque.data, pos);
589 let old_opaque = mem::replace(&mut self.opaque, new_opaque);
591 self.opaque = old_opaque;
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)
602 implement_ty_decoder!( CacheDecoder<'a, 'tcx, 'x> );
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)
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)?;
614 if tag == TAG_INVALID_SPAN {
617 debug_assert_eq!(tag, TAG_VALID_SPAN);
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)?;
625 let file_lo = self.file_index_to_file(file_lo_index);
626 let lo = file_lo.lines.borrow()[line_lo - 1] + col_lo;
629 let expn_info_tag = u8::decode(self)?;
631 let ctxt = match expn_info_tag {
632 TAG_NO_EXPANSION_INFO => {
633 SyntaxContext::empty()
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);
642 TAG_EXPANSION_INFO_SHORTHAND => {
643 let pos = AbsoluteBytePos::decode(self)?;
644 let cached_ctxt = self.synthetic_expansion_infos
649 if let Some(ctxt) = cached_ctxt {
652 let expn_info = self.with_position(pos.to_usize(), |this| {
653 ExpnInfo::decode(this)
655 let ctxt = SyntaxContext::allocate_directly(expn_info);
656 self.synthetic_expansion_infos.borrow_mut().insert(pos, ctxt);
665 Ok(Span::new(lo, hi, ctxt))
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
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")
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> {
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)?;
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])
693 impl<'a, 'tcx, 'x> SpecializedDecoder<LocalDefId> for CacheDecoder<'a, 'tcx, 'x> {
695 fn specialized_decode(&mut self) -> Result<LocalDefId, Self::Error> {
696 Ok(LocalDefId::from_def_id(DefId::decode(self)?))
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)?;
705 // Use the DefPathHash to map to the current DefId.
706 let def_id = self.tcx()
707 .def_path_hash_to_def_id
709 .unwrap()[&def_path_hash];
711 debug_assert!(def_id.is_local());
713 // The ItemLocalId needs no remapping.
714 let local_id = hir::ItemLocalId::decode(self)?;
716 // Reconstruct the HirId and look up the corresponding NodeId in the
717 // context of the current session.
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> {
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))
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)
741 impl<'a, 'tcx, 'x, T: Decodable> SpecializedDecoder<mir::ClearCrossCrate<T>>
742 for CacheDecoder<'a, 'tcx, 'x> {
744 fn specialized_decode(&mut self) -> Result<mir::ClearCrossCrate<T>, Self::Error> {
745 let discr = u8::decode(self)?;
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))
760 //- ENCODING -------------------------------------------------------------------
762 struct CacheEncoder<'enc, 'a, 'tcx, E>
763 where E: 'enc + ty_codec::TyEncoder,
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>,
777 impl<'enc, 'a, 'tcx, E> CacheEncoder<'enc, 'a, 'tcx, E>
778 where E: 'enc + ty_codec::TyEncoder
780 fn filemap_index(&mut self, filemap: Lrc<FileMap>) -> FileMapIndex {
781 self.file_to_file_index[&(&*filemap as *const FileMap)]
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,
792 -> Result<(), E::Error>
794 use ty::codec::TyEncoder;
795 let start_pos = self.position();
800 let end_pos = self.position();
801 ((end_pos - start_pos) as u64).encode(self)
805 impl<'enc, 'a, 'tcx, E> SpecializedEncoder<interpret::AllocId> for CacheEncoder<'enc, 'a, 'tcx, E>
806 where E: 'enc + ty_codec::TyEncoder
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);
824 impl<'enc, 'a, 'tcx, E> SpecializedEncoder<Span> for CacheEncoder<'enc, 'a, 'tcx, E>
825 where E: 'enc + ty_codec::TyEncoder
827 fn specialized_encode(&mut self, span: &Span) -> Result<(), Self::Error> {
829 if *span == DUMMY_SP {
830 return TAG_INVALID_SPAN.encode(self);
833 let span_data = span.data();
835 if span_data.hi < span_data.lo {
836 return TAG_INVALID_SPAN.encode(self);
839 let (file_lo, line_lo, col_lo) = match self.codemap
840 .byte_pos_to_line_and_col(span_data.lo) {
843 return TAG_INVALID_SPAN.encode(self);
847 if !file_lo.contains(span_data.hi) {
848 return TAG_INVALID_SPAN.encode(self);
851 let len = span_data.hi - span_data.lo;
853 let filemap_index = self.filemap_index(file_lo);
855 TAG_VALID_SPAN.encode(self)?;
856 filemap_index.encode(self)?;
857 line_lo.encode(self)?;
858 col_lo.encode(self)?;
861 if span_data.ctxt == SyntaxContext::empty() {
862 TAG_NO_EXPANSION_INFO.encode(self)
864 let mark = span_data.ctxt.outer();
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)?;
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)
877 TAG_NO_EXPANSION_INFO.encode(self)
883 impl<'enc, 'a, 'tcx, E> ty_codec::TyEncoder for CacheEncoder<'enc, 'a, 'tcx, E>
884 where E: 'enc + ty_codec::TyEncoder
887 fn position(&self) -> usize {
888 self.encoder.position()
892 impl<'enc, 'a, 'tcx, E> SpecializedEncoder<CrateNum> for CacheEncoder<'enc, 'a, 'tcx, E>
893 where E: 'enc + ty_codec::TyEncoder
896 fn specialized_encode(&mut self, cnum: &CrateNum) -> Result<(), Self::Error> {
897 self.emit_u32(cnum.as_u32())
901 impl<'enc, 'a, 'tcx, E> SpecializedEncoder<ty::Ty<'tcx>> for CacheEncoder<'enc, 'a, 'tcx, E>
902 where E: 'enc + ty_codec::TyEncoder
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)
911 impl<'enc, 'a, 'tcx, E> SpecializedEncoder<ty::GenericPredicates<'tcx>>
912 for CacheEncoder<'enc, 'a, 'tcx, E>
913 where E: 'enc + ty_codec::TyEncoder
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)
924 impl<'enc, 'a, 'tcx, E> SpecializedEncoder<hir::HirId> for CacheEncoder<'enc, 'a, 'tcx, E>
925 where E: 'enc + ty_codec::TyEncoder
928 fn specialized_encode(&mut self, id: &hir::HirId) -> Result<(), Self::Error> {
934 let def_path_hash = self.tcx.hir.definitions().def_path_hash(owner);
936 def_path_hash.encode(self)?;
937 local_id.encode(self)
942 impl<'enc, 'a, 'tcx, E> SpecializedEncoder<DefId> for CacheEncoder<'enc, 'a, 'tcx, E>
943 where E: 'enc + ty_codec::TyEncoder
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)
952 impl<'enc, 'a, 'tcx, E> SpecializedEncoder<LocalDefId> for CacheEncoder<'enc, 'a, 'tcx, E>
953 where E: 'enc + ty_codec::TyEncoder
956 fn specialized_encode(&mut self, id: &LocalDefId) -> Result<(), Self::Error> {
957 id.to_def_id().encode(self)
961 impl<'enc, 'a, 'tcx, E> SpecializedEncoder<DefIndex> for CacheEncoder<'enc, 'a, 'tcx, E>
962 where E: 'enc + ty_codec::TyEncoder
964 fn specialized_encode(&mut self, _: &DefIndex) -> Result<(), Self::Error> {
965 bug!("Encoding DefIndex without context.")
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
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);
981 impl<'enc, 'a, 'tcx> SpecializedEncoder<Fingerprint>
982 for CacheEncoder<'enc, 'a, 'tcx, opaque::Encoder<'enc>>
984 fn specialized_encode(&mut self, f: &Fingerprint) -> Result<(), Self::Error> {
985 f.encode_opaque(&mut self.encoder)
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,
995 fn specialized_encode(&mut self,
996 val: &mir::ClearCrossCrate<T>)
997 -> Result<(), Self::Error> {
999 mir::ClearCrossCrate::Clear => {
1000 TAG_CLEAR_CROSS_CRATE_CLEAR.encode(self)
1002 mir::ClearCrossCrate::Set(ref val) => {
1003 TAG_CLEAR_CROSS_CRATE_SET.encode(self)?;
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)
1018 impl<'enc, 'a, 'tcx, E> Encoder for CacheEncoder<'enc, 'a, 'tcx, E>
1019 where E: 'enc + ty_codec::TyEncoder
1021 type Error = E::Error;
1023 fn emit_nil(&mut self) -> Result<(), Self::Error> {
1050 // An integer that will always encode to 8 bytes.
1051 struct IntEncodedWithFixedSize(u64);
1053 impl IntEncodedWithFixedSize {
1054 pub const ENCODED_SIZE: usize = 8;
1057 impl UseSpecializedEncodable for IntEncodedWithFixedSize {}
1058 impl UseSpecializedDecodable for IntEncodedWithFixedSize {}
1060 impl<'enc> SpecializedEncoder<IntEncodedWithFixedSize> for opaque::Encoder<'enc> {
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)?;
1066 let end_pos = self.position();
1067 assert_eq!((end_pos - start_pos), IntEncodedWithFixedSize::ENCODED_SIZE);
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();
1077 for i in 0 .. IntEncodedWithFixedSize::ENCODED_SIZE {
1078 let byte: u8 = Decodable::decode(self)?;
1079 value |= (byte as u64) << (i * 8);
1082 let end_pos = self.position();
1083 assert_eq!((end_pos - start_pos), IntEncodedWithFixedSize::ENCODED_SIZE);
1085 Ok(IntEncodedWithFixedSize(value))
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,
1097 let desc = &format!("encode_query_results for {}",
1098 unsafe { ::std::intrinsics::type_name::<Q>() });
1100 time(tcx.sess, desc, || {
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());
1108 // Record position of the cache entry
1109 query_result_index.push((dep_node, AbsoluteBytePos::new(encoder.position())));
1111 // Encode the type check tables with the SerializedDepNodeIndex
1113 encoder.encode_tagged(dep_node, &entry.value)?;