1use std::collections::hash_map::Entry;
2use std::mem;
3use std::sync::Arc;
4
5use rustc_data_structures::fx::{FxHashMap, FxIndexSet};
6use rustc_data_structures::memmap::Mmap;
7use rustc_data_structures::sync::{HashMapExt, Lock, RwLock};
8use rustc_data_structures::unhash::UnhashMap;
9use rustc_data_structures::unord::{UnordMap, UnordSet};
10use rustc_hir::def_id::{CrateNum, DefId, DefIndex, LOCAL_CRATE, LocalDefId, StableCrateId};
11use rustc_hir::definitions::DefPathHash;
12use rustc_index::{Idx, IndexVec};
13use rustc_macros::{Decodable, Encodable};
14use rustc_query_system::query::QuerySideEffects;
15use rustc_serialize::opaque::{FileEncodeResult, FileEncoder, IntEncodedWithFixedSize, MemDecoder};
16use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
17use rustc_session::Session;
18use rustc_span::hygiene::{
19 ExpnId, HygieneDecodeContext, HygieneEncodeContext, SyntaxContext, SyntaxContextData,
20};
21use rustc_span::source_map::Spanned;
22use rustc_span::{
23 BytePos, CachingSourceMapView, ExpnData, ExpnHash, Pos, RelativeBytePos, SourceFile, Span,
24 SpanDecoder, SpanEncoder, StableSourceFileId, Symbol,
25};
26
27use crate::dep_graph::{DepNodeIndex, SerializedDepNodeIndex};
28use crate::mir::interpret::{AllocDecodingSession, AllocDecodingState};
29use crate::mir::mono::MonoItem;
30use crate::mir::{self, interpret};
31use crate::ty::codec::{RefDecodable, TyDecoder, TyEncoder};
32use crate::ty::{self, Ty, TyCtxt};
33
34const TAG_FILE_FOOTER: u128 = 0xC0FFEE_C0FFEE_C0FFEE_C0FFEE_C0FFEE;
35
36const TAG_FULL_SPAN: u8 = 0;
38const TAG_PARTIAL_SPAN: u8 = 1;
40const TAG_RELATIVE_SPAN: u8 = 2;
41
42const TAG_SYNTAX_CONTEXT: u8 = 0;
43const TAG_EXPN_DATA: u8 = 1;
44
45const SYMBOL_STR: u8 = 0;
47const SYMBOL_OFFSET: u8 = 1;
48const SYMBOL_PREINTERNED: u8 = 2;
49
50pub struct OnDiskCache {
55 serialized_data: RwLock<Option<Mmap>>,
57
58 current_side_effects: Lock<FxHashMap<DepNodeIndex, QuerySideEffects>>,
61
62 file_index_to_stable_id: FxHashMap<SourceFileIndex, EncodedSourceFileId>,
63
64 file_index_to_file: Lock<FxHashMap<SourceFileIndex, Arc<SourceFile>>>,
66
67 query_result_index: FxHashMap<SerializedDepNodeIndex, AbsoluteBytePos>,
70
71 prev_side_effects_index: FxHashMap<SerializedDepNodeIndex, AbsoluteBytePos>,
74
75 alloc_decoding_state: AllocDecodingState,
76
77 syntax_contexts: FxHashMap<u32, AbsoluteBytePos>,
83 expn_data: UnhashMap<ExpnHash, AbsoluteBytePos>,
93 hygiene_context: HygieneDecodeContext,
95 foreign_expn_data: UnhashMap<ExpnHash, u32>,
100}
101
102#[derive(Encodable, Decodable)]
104struct Footer {
105 file_index_to_stable_id: FxHashMap<SourceFileIndex, EncodedSourceFileId>,
106 query_result_index: EncodedDepNodeIndex,
107 side_effects_index: EncodedDepNodeIndex,
108 interpret_alloc_index: Vec<u64>,
112 syntax_contexts: FxHashMap<u32, AbsoluteBytePos>,
114 expn_data: UnhashMap<ExpnHash, AbsoluteBytePos>,
116 foreign_expn_data: UnhashMap<ExpnHash, u32>,
117}
118
119pub type EncodedDepNodeIndex = Vec<(SerializedDepNodeIndex, AbsoluteBytePos)>;
120
121#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, Encodable, Decodable)]
122struct SourceFileIndex(u32);
123
124#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq, Encodable, Decodable)]
125pub struct AbsoluteBytePos(u64);
126
127impl AbsoluteBytePos {
128 #[inline]
129 pub fn new(pos: usize) -> AbsoluteBytePos {
130 AbsoluteBytePos(pos.try_into().expect("Incremental cache file size overflowed u64."))
131 }
132
133 #[inline]
134 fn to_usize(self) -> usize {
135 self.0 as usize
136 }
137}
138
139#[derive(Encodable, Decodable, Clone, Debug)]
140struct EncodedSourceFileId {
141 stable_source_file_id: StableSourceFileId,
142 stable_crate_id: StableCrateId,
143}
144
145impl EncodedSourceFileId {
146 #[inline]
147 fn new(tcx: TyCtxt<'_>, file: &SourceFile) -> EncodedSourceFileId {
148 EncodedSourceFileId {
149 stable_source_file_id: file.stable_id,
150 stable_crate_id: tcx.stable_crate_id(file.cnum),
151 }
152 }
153}
154
155impl OnDiskCache {
156 pub fn new(sess: &Session, data: Mmap, start_pos: usize) -> Result<Self, ()> {
161 assert!(sess.opts.incremental.is_some());
162
163 let mut decoder = MemDecoder::new(&data, start_pos)?;
164
165 let footer_pos = decoder
168 .with_position(decoder.len() - IntEncodedWithFixedSize::ENCODED_SIZE, |decoder| {
169 IntEncodedWithFixedSize::decode(decoder).0 as usize
170 });
171 let footer: Footer =
173 decoder.with_position(footer_pos, |decoder| decode_tagged(decoder, TAG_FILE_FOOTER));
174
175 Ok(Self {
176 serialized_data: RwLock::new(Some(data)),
177 file_index_to_stable_id: footer.file_index_to_stable_id,
178 file_index_to_file: Default::default(),
179 current_side_effects: Default::default(),
180 query_result_index: footer.query_result_index.into_iter().collect(),
181 prev_side_effects_index: footer.side_effects_index.into_iter().collect(),
182 alloc_decoding_state: AllocDecodingState::new(footer.interpret_alloc_index),
183 syntax_contexts: footer.syntax_contexts,
184 expn_data: footer.expn_data,
185 foreign_expn_data: footer.foreign_expn_data,
186 hygiene_context: Default::default(),
187 })
188 }
189
190 pub fn new_empty() -> Self {
191 Self {
192 serialized_data: RwLock::new(None),
193 file_index_to_stable_id: Default::default(),
194 file_index_to_file: Default::default(),
195 current_side_effects: Default::default(),
196 query_result_index: Default::default(),
197 prev_side_effects_index: Default::default(),
198 alloc_decoding_state: AllocDecodingState::new(Vec::new()),
199 syntax_contexts: FxHashMap::default(),
200 expn_data: UnhashMap::default(),
201 foreign_expn_data: UnhashMap::default(),
202 hygiene_context: Default::default(),
203 }
204 }
205
206 pub fn drop_serialized_data(&self, tcx: TyCtxt<'_>) {
212 tcx.dep_graph.exec_cache_promotions(tcx);
219
220 *self.serialized_data.write() = None;
221 }
222
223 pub fn serialize(&self, tcx: TyCtxt<'_>, encoder: FileEncoder) -> FileEncodeResult {
224 tcx.dep_graph.with_ignore(|| {
226 let (file_to_file_index, file_index_to_stable_id) = {
228 let files = tcx.sess.source_map().files();
229 let mut file_to_file_index =
230 FxHashMap::with_capacity_and_hasher(files.len(), Default::default());
231 let mut file_index_to_stable_id =
232 FxHashMap::with_capacity_and_hasher(files.len(), Default::default());
233
234 for (index, file) in files.iter().enumerate() {
235 let index = SourceFileIndex(index as u32);
236 let file_ptr: *const SourceFile = &raw const **file;
237 file_to_file_index.insert(file_ptr, index);
238 let source_file_id = EncodedSourceFileId::new(tcx, file);
239 file_index_to_stable_id.insert(index, source_file_id);
240 }
241
242 (file_to_file_index, file_index_to_stable_id)
243 };
244
245 let hygiene_encode_context = HygieneEncodeContext::default();
246
247 let mut encoder = CacheEncoder {
248 tcx,
249 encoder,
250 type_shorthands: Default::default(),
251 predicate_shorthands: Default::default(),
252 interpret_allocs: Default::default(),
253 source_map: CachingSourceMapView::new(tcx.sess.source_map()),
254 file_to_file_index,
255 hygiene_context: &hygiene_encode_context,
256 symbol_table: Default::default(),
257 };
258
259 let mut query_result_index = EncodedDepNodeIndex::new();
261
262 tcx.sess.time("encode_query_results", || {
263 let enc = &mut encoder;
264 let qri = &mut query_result_index;
265 (tcx.query_system.fns.encode_query_results)(tcx, enc, qri);
266 });
267
268 let side_effects_index: EncodedDepNodeIndex = self
270 .current_side_effects
271 .borrow()
272 .iter()
273 .map(|(dep_node_index, side_effects)| {
274 let pos = AbsoluteBytePos::new(encoder.position());
275 let dep_node_index = SerializedDepNodeIndex::new(dep_node_index.index());
276 encoder.encode_tagged(dep_node_index, side_effects);
277
278 (dep_node_index, pos)
279 })
280 .collect();
281
282 let interpret_alloc_index = {
283 let mut interpret_alloc_index = Vec::new();
284 let mut n = 0;
285 loop {
286 let new_n = encoder.interpret_allocs.len();
287 if n == new_n {
289 break;
291 }
292 interpret_alloc_index.reserve(new_n - n);
293 for idx in n..new_n {
294 let id = encoder.interpret_allocs[idx];
295 let pos: u64 = encoder.position().try_into().unwrap();
296 interpret_alloc_index.push(pos);
297 interpret::specialized_encode_alloc_id(&mut encoder, tcx, id);
298 }
299 n = new_n;
300 }
301 interpret_alloc_index
302 };
303
304 let mut syntax_contexts = FxHashMap::default();
305 let mut expn_data = UnhashMap::default();
306 let mut foreign_expn_data = UnhashMap::default();
307
308 hygiene_encode_context.encode(
312 &mut encoder,
313 |encoder, index, ctxt_data| {
314 let pos = AbsoluteBytePos::new(encoder.position());
315 encoder.encode_tagged(TAG_SYNTAX_CONTEXT, ctxt_data);
316 syntax_contexts.insert(index, pos);
317 },
318 |encoder, expn_id, data, hash| {
319 if expn_id.krate == LOCAL_CRATE {
320 let pos = AbsoluteBytePos::new(encoder.position());
321 encoder.encode_tagged(TAG_EXPN_DATA, data);
322 expn_data.insert(hash, pos);
323 } else {
324 foreign_expn_data.insert(hash, expn_id.local_id.as_u32());
325 }
326 },
327 );
328
329 let footer_pos = encoder.position() as u64;
331 encoder.encode_tagged(
332 TAG_FILE_FOOTER,
333 &Footer {
334 file_index_to_stable_id,
335 query_result_index,
336 side_effects_index,
337 interpret_alloc_index,
338 syntax_contexts,
339 expn_data,
340 foreign_expn_data,
341 },
342 );
343
344 IntEncodedWithFixedSize(footer_pos).encode(&mut encoder.encoder);
347
348 encoder.finish()
352 })
353 }
354
355 pub fn load_side_effects(
357 &self,
358 tcx: TyCtxt<'_>,
359 dep_node_index: SerializedDepNodeIndex,
360 ) -> QuerySideEffects {
361 let side_effects: Option<QuerySideEffects> =
362 self.load_indexed(tcx, dep_node_index, &self.prev_side_effects_index);
363
364 side_effects.unwrap_or_default()
365 }
366
367 pub fn store_side_effects(&self, dep_node_index: DepNodeIndex, side_effects: QuerySideEffects) {
371 let mut current_side_effects = self.current_side_effects.borrow_mut();
372 let prev = current_side_effects.insert(dep_node_index, side_effects);
373 debug_assert!(prev.is_none());
374 }
375
376 #[inline]
378 pub fn loadable_from_disk(&self, dep_node_index: SerializedDepNodeIndex) -> bool {
379 self.query_result_index.contains_key(&dep_node_index)
380 }
382
383 pub fn try_load_query_result<'tcx, T>(
386 &self,
387 tcx: TyCtxt<'tcx>,
388 dep_node_index: SerializedDepNodeIndex,
389 ) -> Option<T>
390 where
391 T: for<'a> Decodable<CacheDecoder<'a, 'tcx>>,
392 {
393 let opt_value = self.load_indexed(tcx, dep_node_index, &self.query_result_index);
394 debug_assert_eq!(opt_value.is_some(), self.loadable_from_disk(dep_node_index));
395 opt_value
396 }
397
398 pub fn store_side_effects_for_anon_node(
403 &self,
404 dep_node_index: DepNodeIndex,
405 side_effects: QuerySideEffects,
406 ) {
407 let mut current_side_effects = self.current_side_effects.borrow_mut();
408
409 let x = current_side_effects.entry(dep_node_index).or_default();
410 x.append(side_effects);
411 }
412
413 fn load_indexed<'tcx, T>(
414 &self,
415 tcx: TyCtxt<'tcx>,
416 dep_node_index: SerializedDepNodeIndex,
417 index: &FxHashMap<SerializedDepNodeIndex, AbsoluteBytePos>,
418 ) -> Option<T>
419 where
420 T: for<'a> Decodable<CacheDecoder<'a, 'tcx>>,
421 {
422 let pos = index.get(&dep_node_index).cloned()?;
423 let value = self.with_decoder(tcx, pos, |decoder| decode_tagged(decoder, dep_node_index));
424 Some(value)
425 }
426
427 fn with_decoder<'a, 'tcx, T, F: for<'s> FnOnce(&mut CacheDecoder<'s, 'tcx>) -> T>(
428 &self,
429 tcx: TyCtxt<'tcx>,
430 pos: AbsoluteBytePos,
431 f: F,
432 ) -> T
433 where
434 T: Decodable<CacheDecoder<'a, 'tcx>>,
435 {
436 let serialized_data = self.serialized_data.read();
437 let mut decoder = CacheDecoder {
438 tcx,
439 opaque: MemDecoder::new(serialized_data.as_deref().unwrap_or(&[]), pos.to_usize())
440 .unwrap(),
441 file_index_to_file: &self.file_index_to_file,
442 file_index_to_stable_id: &self.file_index_to_stable_id,
443 alloc_decoding_session: self.alloc_decoding_state.new_decoding_session(),
444 syntax_contexts: &self.syntax_contexts,
445 expn_data: &self.expn_data,
446 foreign_expn_data: &self.foreign_expn_data,
447 hygiene_context: &self.hygiene_context,
448 };
449 f(&mut decoder)
450 }
451}
452
453pub struct CacheDecoder<'a, 'tcx> {
459 tcx: TyCtxt<'tcx>,
460 opaque: MemDecoder<'a>,
461 file_index_to_file: &'a Lock<FxHashMap<SourceFileIndex, Arc<SourceFile>>>,
462 file_index_to_stable_id: &'a FxHashMap<SourceFileIndex, EncodedSourceFileId>,
463 alloc_decoding_session: AllocDecodingSession<'a>,
464 syntax_contexts: &'a FxHashMap<u32, AbsoluteBytePos>,
465 expn_data: &'a UnhashMap<ExpnHash, AbsoluteBytePos>,
466 foreign_expn_data: &'a UnhashMap<ExpnHash, u32>,
467 hygiene_context: &'a HygieneDecodeContext,
468}
469
470impl<'a, 'tcx> CacheDecoder<'a, 'tcx> {
471 #[inline]
472 fn file_index_to_file(&self, index: SourceFileIndex) -> Arc<SourceFile> {
473 let CacheDecoder { tcx, file_index_to_file, file_index_to_stable_id, .. } = *self;
474
475 Arc::clone(file_index_to_file.borrow_mut().entry(index).or_insert_with(|| {
476 let source_file_id = &file_index_to_stable_id[&index];
477 let source_file_cnum = tcx.stable_crate_id_to_crate_num(source_file_id.stable_crate_id);
478
479 if source_file_cnum != LOCAL_CRATE {
489 self.tcx.import_source_files(source_file_cnum);
490 }
491
492 tcx.sess
493 .source_map()
494 .source_file_by_stable_id(source_file_id.stable_source_file_id)
495 .expect("failed to lookup `SourceFile` in new context")
496 }))
497 }
498}
499
500fn decode_tagged<D, T, V>(decoder: &mut D, expected_tag: T) -> V
503where
504 T: Decodable<D> + Eq + std::fmt::Debug,
505 V: Decodable<D>,
506 D: Decoder,
507{
508 let start_pos = decoder.position();
509
510 let actual_tag = T::decode(decoder);
511 assert_eq!(actual_tag, expected_tag);
512 let value = V::decode(decoder);
513 let end_pos = decoder.position();
514
515 let expected_len: u64 = Decodable::decode(decoder);
516 assert_eq!((end_pos - start_pos) as u64, expected_len);
517
518 value
519}
520
521impl<'a, 'tcx> TyDecoder for CacheDecoder<'a, 'tcx> {
522 type I = TyCtxt<'tcx>;
523 const CLEAR_CROSS_CRATE: bool = false;
524
525 #[inline]
526 fn interner(&self) -> TyCtxt<'tcx> {
527 self.tcx
528 }
529
530 fn cached_ty_for_shorthand<F>(&mut self, shorthand: usize, or_insert_with: F) -> Ty<'tcx>
531 where
532 F: FnOnce(&mut Self) -> Ty<'tcx>,
533 {
534 let tcx = self.tcx;
535
536 let cache_key = ty::CReaderCacheKey { cnum: None, pos: shorthand };
537
538 if let Some(&ty) = tcx.ty_rcache.borrow().get(&cache_key) {
539 return ty;
540 }
541
542 let ty = or_insert_with(self);
543 tcx.ty_rcache.borrow_mut().insert_same(cache_key, ty);
545 ty
546 }
547
548 fn with_position<F, R>(&mut self, pos: usize, f: F) -> R
549 where
550 F: FnOnce(&mut Self) -> R,
551 {
552 debug_assert!(pos < self.opaque.len());
553
554 let new_opaque = self.opaque.split_at(pos);
555 let old_opaque = mem::replace(&mut self.opaque, new_opaque);
556 let r = f(self);
557 self.opaque = old_opaque;
558 r
559 }
560
561 fn decode_alloc_id(&mut self) -> interpret::AllocId {
562 let alloc_decoding_session = self.alloc_decoding_session;
563 alloc_decoding_session.decode_alloc_id(self)
564 }
565}
566
567crate::implement_ty_decoder!(CacheDecoder<'a, 'tcx>);
568
569impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for Vec<u8> {
573 fn decode(d: &mut CacheDecoder<'a, 'tcx>) -> Self {
574 Decodable::decode(&mut d.opaque)
575 }
576}
577
578impl<'a, 'tcx> SpanDecoder for CacheDecoder<'a, 'tcx> {
579 fn decode_syntax_context(&mut self) -> SyntaxContext {
580 let syntax_contexts = self.syntax_contexts;
581 rustc_span::hygiene::decode_syntax_context(self, self.hygiene_context, |this, id| {
582 let pos = syntax_contexts.get(&id).unwrap();
585 this.with_position(pos.to_usize(), |decoder| {
586 let data: SyntaxContextData = decode_tagged(decoder, TAG_SYNTAX_CONTEXT);
587 data
588 })
589 })
590 }
591
592 fn decode_expn_id(&mut self) -> ExpnId {
593 let hash = ExpnHash::decode(self);
594 if hash.is_root() {
595 return ExpnId::root();
596 }
597
598 if let Some(expn_id) = ExpnId::from_hash(hash) {
599 return expn_id;
600 }
601
602 let krate = self.tcx.stable_crate_id_to_crate_num(hash.stable_crate_id());
603
604 let expn_id = if krate == LOCAL_CRATE {
605 let pos = self
607 .expn_data
608 .get(&hash)
609 .unwrap_or_else(|| panic!("Bad hash {:?} (map {:?})", hash, self.expn_data));
610
611 let data: ExpnData =
612 self.with_position(pos.to_usize(), |decoder| decode_tagged(decoder, TAG_EXPN_DATA));
613 let expn_id = rustc_span::hygiene::register_local_expn_id(data, hash);
614
615 #[cfg(debug_assertions)]
616 {
617 use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
618 let local_hash = self.tcx.with_stable_hashing_context(|mut hcx| {
619 let mut hasher = StableHasher::new();
620 expn_id.expn_data().hash_stable(&mut hcx, &mut hasher);
621 hasher.finish()
622 });
623 debug_assert_eq!(hash.local_hash(), local_hash);
624 }
625
626 expn_id
627 } else {
628 let index_guess = self.foreign_expn_data[&hash];
629 self.tcx.expn_hash_to_expn_id(krate, index_guess, hash)
630 };
631
632 debug_assert_eq!(expn_id.krate, krate);
633 expn_id
634 }
635
636 fn decode_span(&mut self) -> Span {
637 let ctxt = SyntaxContext::decode(self);
638 let parent = Option::<LocalDefId>::decode(self);
639 let tag: u8 = Decodable::decode(self);
640
641 if tag == TAG_PARTIAL_SPAN {
642 return Span::new(BytePos(0), BytePos(0), ctxt, parent);
643 } else if tag == TAG_RELATIVE_SPAN {
644 let dlo = u32::decode(self);
645 let dto = u32::decode(self);
646
647 let enclosing = self.tcx.source_span_untracked(parent.unwrap()).data_untracked();
648 let span = Span::new(
649 enclosing.lo + BytePos::from_u32(dlo),
650 enclosing.lo + BytePos::from_u32(dto),
651 ctxt,
652 parent,
653 );
654
655 return span;
656 } else {
657 debug_assert_eq!(tag, TAG_FULL_SPAN);
658 }
659
660 let file_lo_index = SourceFileIndex::decode(self);
661 let line_lo = usize::decode(self);
662 let col_lo = RelativeBytePos::decode(self);
663 let len = BytePos::decode(self);
664
665 let file_lo = self.file_index_to_file(file_lo_index);
666 let lo = file_lo.lines()[line_lo - 1] + col_lo;
667 let lo = file_lo.absolute_position(lo);
668 let hi = lo + len;
669
670 Span::new(lo, hi, ctxt, parent)
671 }
672
673 #[inline]
675 fn decode_symbol(&mut self) -> Symbol {
676 let tag = self.read_u8();
677
678 match tag {
679 SYMBOL_STR => {
680 let s = self.read_str();
681 Symbol::intern(s)
682 }
683 SYMBOL_OFFSET => {
684 let pos = self.read_usize();
686
687 self.opaque.with_position(pos, |d| {
689 let s = d.read_str();
690 Symbol::intern(s)
691 })
692 }
693 SYMBOL_PREINTERNED => {
694 let symbol_index = self.read_u32();
695 Symbol::new_from_decoded(symbol_index)
696 }
697 _ => unreachable!(),
698 }
699 }
700
701 fn decode_crate_num(&mut self) -> CrateNum {
702 let stable_id = StableCrateId::decode(self);
703 let cnum = self.tcx.stable_crate_id_to_crate_num(stable_id);
704 cnum
705 }
706
707 fn decode_def_index(&mut self) -> DefIndex {
712 panic!("trying to decode `DefIndex` outside the context of a `DefId`")
713 }
714
715 fn decode_def_id(&mut self) -> DefId {
719 let def_path_hash = DefPathHash::decode(self);
721
722 match self.tcx.def_path_hash_to_def_id(def_path_hash) {
728 Some(r) => r,
729 None => panic!("Failed to convert DefPathHash {def_path_hash:?}"),
730 }
731 }
732
733 fn decode_attr_id(&mut self) -> rustc_span::AttrId {
734 panic!("cannot decode `AttrId` with `CacheDecoder`");
735 }
736}
737
738impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for &'tcx UnordSet<LocalDefId> {
739 #[inline]
740 fn decode(d: &mut CacheDecoder<'a, 'tcx>) -> Self {
741 RefDecodable::decode(d)
742 }
743}
744
745impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>>
746 for &'tcx UnordMap<DefId, ty::EarlyBinder<'tcx, Ty<'tcx>>>
747{
748 #[inline]
749 fn decode(d: &mut CacheDecoder<'a, 'tcx>) -> Self {
750 RefDecodable::decode(d)
751 }
752}
753
754impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>>
755 for &'tcx IndexVec<mir::Promoted, mir::Body<'tcx>>
756{
757 #[inline]
758 fn decode(d: &mut CacheDecoder<'a, 'tcx>) -> Self {
759 RefDecodable::decode(d)
760 }
761}
762
763impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for &'tcx [(ty::Clause<'tcx>, Span)] {
764 #[inline]
765 fn decode(d: &mut CacheDecoder<'a, 'tcx>) -> Self {
766 RefDecodable::decode(d)
767 }
768}
769
770impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for &'tcx [rustc_ast::InlineAsmTemplatePiece] {
771 #[inline]
772 fn decode(d: &mut CacheDecoder<'a, 'tcx>) -> Self {
773 RefDecodable::decode(d)
774 }
775}
776
777impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for &'tcx [Spanned<MonoItem<'tcx>>] {
778 #[inline]
779 fn decode(d: &mut CacheDecoder<'a, 'tcx>) -> Self {
780 RefDecodable::decode(d)
781 }
782}
783
784impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>>
785 for &'tcx crate::traits::specialization_graph::Graph
786{
787 #[inline]
788 fn decode(d: &mut CacheDecoder<'a, 'tcx>) -> Self {
789 RefDecodable::decode(d)
790 }
791}
792
793macro_rules! impl_ref_decoder {
794 (<$tcx:tt> $($ty:ty,)*) => {
795 $(impl<'a, $tcx> Decodable<CacheDecoder<'a, $tcx>> for &$tcx [$ty] {
796 #[inline]
797 fn decode(d: &mut CacheDecoder<'a, $tcx>) -> Self {
798 RefDecodable::decode(d)
799 }
800 })*
801 };
802}
803
804impl_ref_decoder! {<'tcx>
805 Span,
806 rustc_hir::Attribute,
807 rustc_span::Ident,
808 ty::Variance,
809 rustc_span::def_id::DefId,
810 rustc_span::def_id::LocalDefId,
811 (rustc_middle::middle::exported_symbols::ExportedSymbol<'tcx>, rustc_middle::middle::exported_symbols::SymbolExportInfo),
812 ty::DeducedParamAttrs,
813}
814
815pub struct CacheEncoder<'a, 'tcx> {
819 tcx: TyCtxt<'tcx>,
820 encoder: FileEncoder,
821 type_shorthands: FxHashMap<Ty<'tcx>, usize>,
822 predicate_shorthands: FxHashMap<ty::PredicateKind<'tcx>, usize>,
823 interpret_allocs: FxIndexSet<interpret::AllocId>,
824 source_map: CachingSourceMapView<'tcx>,
825 file_to_file_index: FxHashMap<*const SourceFile, SourceFileIndex>,
826 hygiene_context: &'a HygieneEncodeContext,
827 symbol_table: FxHashMap<Symbol, usize>,
828}
829
830impl<'a, 'tcx> CacheEncoder<'a, 'tcx> {
831 #[inline]
832 fn source_file_index(&mut self, source_file: Arc<SourceFile>) -> SourceFileIndex {
833 self.file_to_file_index[&(&raw const *source_file)]
834 }
835
836 pub fn encode_tagged<T: Encodable<Self>, V: Encodable<Self>>(&mut self, tag: T, value: &V) {
842 let start_pos = self.position();
843
844 tag.encode(self);
845 value.encode(self);
846
847 let end_pos = self.position();
848 ((end_pos - start_pos) as u64).encode(self);
849 }
850
851 #[inline]
852 fn finish(mut self) -> FileEncodeResult {
853 self.encoder.finish()
854 }
855}
856
857impl<'a, 'tcx> SpanEncoder for CacheEncoder<'a, 'tcx> {
858 fn encode_syntax_context(&mut self, syntax_context: SyntaxContext) {
859 rustc_span::hygiene::raw_encode_syntax_context(syntax_context, self.hygiene_context, self);
860 }
861
862 fn encode_expn_id(&mut self, expn_id: ExpnId) {
863 self.hygiene_context.schedule_expn_data_for_encoding(expn_id);
864 expn_id.expn_hash().encode(self);
865 }
866
867 fn encode_span(&mut self, span: Span) {
868 let span_data = span.data_untracked();
869 span_data.ctxt.encode(self);
870 span_data.parent.encode(self);
871
872 if span_data.is_dummy() {
873 return TAG_PARTIAL_SPAN.encode(self);
874 }
875
876 if let Some(parent) = span_data.parent {
877 let enclosing = self.tcx.source_span_untracked(parent).data_untracked();
878 if enclosing.contains(span_data) {
879 TAG_RELATIVE_SPAN.encode(self);
880 (span_data.lo - enclosing.lo).to_u32().encode(self);
881 (span_data.hi - enclosing.lo).to_u32().encode(self);
882 return;
883 }
884 }
885
886 let pos = self.source_map.byte_pos_to_line_and_col(span_data.lo);
887 let partial_span = match &pos {
888 Some((file_lo, _, _)) => !file_lo.contains(span_data.hi),
889 None => true,
890 };
891
892 if partial_span {
893 return TAG_PARTIAL_SPAN.encode(self);
894 }
895
896 let (file_lo, line_lo, col_lo) = pos.unwrap();
897
898 let len = span_data.hi - span_data.lo;
899
900 let source_file_index = self.source_file_index(file_lo);
901
902 TAG_FULL_SPAN.encode(self);
903 source_file_index.encode(self);
904 line_lo.encode(self);
905 col_lo.encode(self);
906 len.encode(self);
907 }
908
909 fn encode_symbol(&mut self, symbol: Symbol) {
911 if symbol.is_preinterned() {
913 self.encoder.emit_u8(SYMBOL_PREINTERNED);
914 self.encoder.emit_u32(symbol.as_u32());
915 } else {
916 match self.symbol_table.entry(symbol) {
918 Entry::Vacant(o) => {
919 self.encoder.emit_u8(SYMBOL_STR);
920 let pos = self.encoder.position();
921 o.insert(pos);
922 self.emit_str(symbol.as_str());
923 }
924 Entry::Occupied(o) => {
925 let x = *o.get();
926 self.emit_u8(SYMBOL_OFFSET);
927 self.emit_usize(x);
928 }
929 }
930 }
931 }
932
933 fn encode_crate_num(&mut self, crate_num: CrateNum) {
934 self.tcx.stable_crate_id(crate_num).encode(self);
935 }
936
937 fn encode_def_id(&mut self, def_id: DefId) {
938 self.tcx.def_path_hash(def_id).encode(self);
939 }
940
941 fn encode_def_index(&mut self, _def_index: DefIndex) {
942 bug!("encoding `DefIndex` without context");
943 }
944}
945
946impl<'a, 'tcx> TyEncoder for CacheEncoder<'a, 'tcx> {
947 type I = TyCtxt<'tcx>;
948 const CLEAR_CROSS_CRATE: bool = false;
949
950 #[inline]
951 fn position(&self) -> usize {
952 self.encoder.position()
953 }
954 #[inline]
955 fn type_shorthands(&mut self) -> &mut FxHashMap<Ty<'tcx>, usize> {
956 &mut self.type_shorthands
957 }
958 #[inline]
959 fn predicate_shorthands(&mut self) -> &mut FxHashMap<ty::PredicateKind<'tcx>, usize> {
960 &mut self.predicate_shorthands
961 }
962 #[inline]
963 fn encode_alloc_id(&mut self, alloc_id: &interpret::AllocId) {
964 let (index, _) = self.interpret_allocs.insert_full(*alloc_id);
965
966 index.encode(self);
967 }
968}
969
970macro_rules! encoder_methods {
971 ($($name:ident($ty:ty);)*) => {
972 #[inline]
973 $(fn $name(&mut self, value: $ty) {
974 self.encoder.$name(value)
975 })*
976 }
977}
978
979impl<'a, 'tcx> Encoder for CacheEncoder<'a, 'tcx> {
980 encoder_methods! {
981 emit_usize(usize);
982 emit_u128(u128);
983 emit_u64(u64);
984 emit_u32(u32);
985 emit_u16(u16);
986 emit_u8(u8);
987
988 emit_isize(isize);
989 emit_i128(i128);
990 emit_i64(i64);
991 emit_i32(i32);
992 emit_i16(i16);
993
994 emit_raw_bytes(&[u8]);
995 }
996}
997
998impl<'a, 'tcx> Encodable<CacheEncoder<'a, 'tcx>> for [u8] {
1003 fn encode(&self, e: &mut CacheEncoder<'a, 'tcx>) {
1004 self.encode(&mut e.encoder);
1005 }
1006}