]> git.proxmox.com Git - rustc.git/blame - compiler/rustc_query_impl/src/on_disk_cache.rs
New upstream version 1.65.0+dfsg1
[rustc.git] / compiler / rustc_query_impl / src / on_disk_cache.rs
CommitLineData
136023e0 1use crate::QueryCtxt;
3dfed10e 2use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexSet};
c295e0f8
XL
3use rustc_data_structures::memmap::Mmap;
4use rustc_data_structures::sync::{HashMapExt, Lock, Lrc, RwLock};
5869c6ff 5use rustc_data_structures::unhash::UnhashMap;
17df50a5 6use rustc_hir::def_id::{CrateNum, DefId, DefIndex, LocalDefId, StableCrateId, LOCAL_CRATE};
ba9703b0 7use rustc_hir::definitions::DefPathHash;
dfeec247 8use rustc_index::vec::{Idx, IndexVec};
c295e0f8 9use rustc_middle::dep_graph::{DepNodeIndex, SerializedDepNodeIndex};
136023e0
XL
10use rustc_middle::mir::interpret::{AllocDecodingSession, AllocDecodingState};
11use rustc_middle::mir::{self, interpret};
12use rustc_middle::ty::codec::{RefDecodable, TyDecoder, TyEncoder};
13use rustc_middle::ty::{self, Ty, TyCtxt};
6a06907d 14use rustc_query_system::dep_graph::DepContext;
5e7ed085 15use rustc_query_system::query::{QueryCache, QueryContext, QuerySideEffects};
5869c6ff 16use rustc_serialize::{
923072b8 17 opaque::{FileEncodeResult, FileEncoder, IntEncodedWithFixedSize, MemDecoder},
5869c6ff
XL
18 Decodable, Decoder, Encodable, Encoder,
19};
17df50a5 20use rustc_session::Session;
3dfed10e 21use rustc_span::hygiene::{
136023e0 22 ExpnId, HygieneDecodeContext, HygieneEncodeContext, SyntaxContext, SyntaxContextData,
3dfed10e 23};
dfeec247 24use rustc_span::source_map::{SourceMap, StableSourceFileId};
c295e0f8 25use rustc_span::{BytePos, ExpnData, ExpnHash, Pos, SourceFile, Span};
f2b60f7d
FG
26use rustc_span::{CachingSourceMapView, Symbol};
27use std::collections::hash_map::Entry;
923072b8 28use std::io;
abe05a73 29use std::mem;
abe05a73 30
ff7c6d11
XL
31const TAG_FILE_FOOTER: u128 = 0xC0FFEE_C0FFEE_C0FFEE_C0FFEE_C0FFEE;
32
5869c6ff
XL
33// A normal span encoded with both location information and a `SyntaxContext`
34const TAG_FULL_SPAN: u8 = 0;
35// A partial span with no location information, encoded only with a `SyntaxContext`
36const TAG_PARTIAL_SPAN: u8 = 1;
c295e0f8 37const TAG_RELATIVE_SPAN: u8 = 2;
abe05a73 38
3dfed10e
XL
39const TAG_SYNTAX_CONTEXT: u8 = 0;
40const TAG_EXPN_DATA: u8 = 1;
41
f2b60f7d
FG
42// Tags for encoding Symbol's
43const SYMBOL_STR: u8 = 0;
44const SYMBOL_OFFSET: u8 = 1;
45const SYMBOL_PREINTERNED: u8 = 2;
46
e1599b0c 47/// Provides an interface to incremental compilation data cached from the
abe05a73 48/// previous compilation session. This data will eventually include the results
3dfed10e 49/// of a few selected queries (like `typeck` and `mir_optimized`) and
94222f64 50/// any side effects that have been emitted during a query.
abe05a73 51pub struct OnDiskCache<'sess> {
abe05a73 52 // The complete cache data in serialized form.
c295e0f8 53 serialized_data: RwLock<Option<Mmap>>,
abe05a73 54
94222f64 55 // Collects all `QuerySideEffects` created during the current compilation
e1599b0c 56 // session.
94222f64 57 current_side_effects: Lock<FxHashMap<DepNodeIndex, QuerySideEffects>>,
abe05a73 58
b7449926 59 source_map: &'sess SourceMap,
17df50a5 60 file_index_to_stable_id: FxHashMap<SourceFileIndex, EncodedSourceFileId>,
ff7c6d11 61
e1599b0c 62 // Caches that are populated lazily during decoding.
b7449926 63 file_index_to_file: Lock<FxHashMap<SourceFileIndex, Lrc<SourceFile>>>,
abe05a73
XL
64
65 // A map from dep-node to the position of the cached query result in
66 // `serialized_data`.
ff7c6d11
XL
67 query_result_index: FxHashMap<SerializedDepNodeIndex, AbsoluteBytePos>,
68
94222f64 69 // A map from dep-node to the position of any associated `QuerySideEffects` in
ff7c6d11 70 // `serialized_data`.
94222f64 71 prev_side_effects_index: FxHashMap<SerializedDepNodeIndex, AbsoluteBytePos>,
0531ce1d 72
94b46f34 73 alloc_decoding_state: AllocDecodingState,
3dfed10e
XL
74
75 // A map from syntax context ids to the position of their associated
76 // `SyntaxContextData`. We use a `u32` instead of a `SyntaxContext`
77 // to represent the fact that we are storing *encoded* ids. When we decode
78 // a `SyntaxContext`, a new id will be allocated from the global `HygieneData`,
79 // which will almost certainly be different than the serialized id.
80 syntax_contexts: FxHashMap<u32, AbsoluteBytePos>,
81 // A map from the `DefPathHash` of an `ExpnId` to the position
82 // of their associated `ExpnData`. Ideally, we would store a `DefId`,
83 // but we need to decode this before we've constructed a `TyCtxt` (which
84 // makes it difficult to decode a `DefId`).
85
86 // Note that these `DefPathHashes` correspond to both local and foreign
87 // `ExpnData` (e.g `ExpnData.krate` may not be `LOCAL_CRATE`). Alternatively,
88 // we could look up the `ExpnData` from the metadata of foreign crates,
89 // but it seemed easier to have `OnDiskCache` be independent of the `CStore`.
136023e0 90 expn_data: UnhashMap<ExpnHash, AbsoluteBytePos>,
3dfed10e
XL
91 // Additional information used when decoding hygiene data.
92 hygiene_context: HygieneDecodeContext,
c295e0f8 93 // Maps `ExpnHash`es to their raw value from the *previous*
fc512014 94 // compilation session. This is used as an initial 'guess' when
c295e0f8
XL
95 // we try to map an `ExpnHash` to its value in the current
96 // compilation session.
136023e0 97 foreign_expn_data: UnhashMap<ExpnHash, u32>,
abe05a73
XL
98}
99
3dfed10e
XL
100// This type is used only for serialization and deserialization.
101#[derive(Encodable, Decodable)]
ff7c6d11 102struct Footer {
17df50a5 103 file_index_to_stable_id: FxHashMap<SourceFileIndex, EncodedSourceFileId>,
94222f64
XL
104 query_result_index: EncodedDepNodeIndex,
105 side_effects_index: EncodedDepNodeIndex,
e1599b0c 106 // The location of all allocations.
94b46f34 107 interpret_alloc_index: Vec<u32>,
3dfed10e
XL
108 // See `OnDiskCache.syntax_contexts`
109 syntax_contexts: FxHashMap<u32, AbsoluteBytePos>,
110 // See `OnDiskCache.expn_data`
136023e0 111 expn_data: UnhashMap<ExpnHash, AbsoluteBytePos>,
136023e0 112 foreign_expn_data: UnhashMap<ExpnHash, u32>,
abe05a73
XL
113}
114
94222f64 115pub type EncodedDepNodeIndex = Vec<(SerializedDepNodeIndex, AbsoluteBytePos)>;
ff7c6d11 116
3dfed10e 117#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, Encodable, Decodable)]
b7449926 118struct SourceFileIndex(u32);
ff7c6d11 119
3dfed10e 120#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq, Encodable, Decodable)]
6a06907d 121pub struct AbsoluteBytePos(u32);
ff7c6d11
XL
122
123impl AbsoluteBytePos {
124 fn new(pos: usize) -> AbsoluteBytePos {
74b04a01 125 debug_assert!(pos <= u32::MAX as usize);
ff7c6d11
XL
126 AbsoluteBytePos(pos as u32)
127 }
128
129 fn to_usize(self) -> usize {
130 self.0 as usize
131 }
132}
abe05a73 133
17df50a5
XL
134/// An `EncodedSourceFileId` is the same as a `StableSourceFileId` except that
135/// the source crate is represented as a [StableCrateId] instead of as a
136/// `CrateNum`. This way `EncodedSourceFileId` can be encoded and decoded
137/// without any additional context, i.e. with a simple `opaque::Decoder` (which
138/// is the only thing available when decoding the cache's [Footer].
139#[derive(Encodable, Decodable, Clone, Debug)]
140struct EncodedSourceFileId {
141 file_name_hash: u64,
142 stable_crate_id: StableCrateId,
143}
144
145impl EncodedSourceFileId {
c295e0f8
XL
146 fn translate(&self, tcx: TyCtxt<'_>) -> StableSourceFileId {
147 let cnum = tcx.stable_crate_id_to_crate_num(self.stable_crate_id);
17df50a5
XL
148 StableSourceFileId { file_name_hash: self.file_name_hash, cnum }
149 }
150
151 fn new(tcx: TyCtxt<'_>, file: &SourceFile) -> EncodedSourceFileId {
152 let source_file_id = StableSourceFileId::new(file);
153 EncodedSourceFileId {
154 file_name_hash: source_file_id.file_name_hash,
155 stable_crate_id: tcx.stable_crate_id(source_file_id.cnum),
156 }
157 }
fc512014
XL
158}
159
136023e0 160impl<'sess> rustc_middle::ty::OnDiskCache<'sess> for OnDiskCache<'sess> {
c295e0f8
XL
161 /// Creates a new `OnDiskCache` instance from the serialized data in `data`.
162 fn new(sess: &'sess Session, data: Mmap, start_pos: usize) -> Self {
abe05a73
XL
163 debug_assert!(sess.opts.incremental.is_some());
164
e1599b0c 165 // Wrap in a scope so we can borrow `data`.
ff7c6d11 166 let footer: Footer = {
923072b8 167 let mut decoder = MemDecoder::new(&data, start_pos);
abe05a73 168
e1599b0c 169 // Decode the *position* of the footer, which can be found in the
ff7c6d11
XL
170 // last 8 bytes of the file.
171 decoder.set_position(data.len() - IntEncodedWithFixedSize::ENCODED_SIZE);
5099ac24 172 let footer_pos = IntEncodedWithFixedSize::decode(&mut decoder).0 as usize;
ff7c6d11 173
e1599b0c
XL
174 // Decode the file footer, which contains all the lookup tables, etc.
175 decoder.set_position(footer_pos);
3dfed10e 176
ff7c6d11 177 decode_tagged(&mut decoder, TAG_FILE_FOOTER)
abe05a73
XL
178 };
179
e1599b0c 180 Self {
c295e0f8 181 serialized_data: RwLock::new(Some(data)),
ff7c6d11 182 file_index_to_stable_id: footer.file_index_to_stable_id,
0bf4aa26 183 file_index_to_file: Default::default(),
b7449926 184 source_map: sess.source_map(),
94222f64 185 current_side_effects: Default::default(),
ff7c6d11 186 query_result_index: footer.query_result_index.into_iter().collect(),
94222f64 187 prev_side_effects_index: footer.side_effects_index.into_iter().collect(),
94b46f34 188 alloc_decoding_state: AllocDecodingState::new(footer.interpret_alloc_index),
3dfed10e
XL
189 syntax_contexts: footer.syntax_contexts,
190 expn_data: footer.expn_data,
136023e0 191 foreign_expn_data: footer.foreign_expn_data,
3dfed10e 192 hygiene_context: Default::default(),
abe05a73
XL
193 }
194 }
195
136023e0 196 fn new_empty(source_map: &'sess SourceMap) -> Self {
e1599b0c 197 Self {
c295e0f8 198 serialized_data: RwLock::new(None),
0bf4aa26
XL
199 file_index_to_stable_id: Default::default(),
200 file_index_to_file: Default::default(),
b7449926 201 source_map,
94222f64 202 current_side_effects: Default::default(),
0bf4aa26 203 query_result_index: Default::default(),
94222f64 204 prev_side_effects_index: Default::default(),
94b46f34 205 alloc_decoding_state: AllocDecodingState::new(Vec::new()),
3dfed10e 206 syntax_contexts: FxHashMap::default(),
136023e0
XL
207 expn_data: UnhashMap::default(),
208 foreign_expn_data: UnhashMap::default(),
3dfed10e 209 hygiene_context: Default::default(),
abe05a73
XL
210 }
211 }
212
c295e0f8
XL
213 /// Execute all cache promotions and release the serialized backing Mmap.
214 ///
215 /// Cache promotions require invoking queries, which needs to read the serialized data.
216 /// In order to serialize the new on-disk cache, the former on-disk cache file needs to be
217 /// deleted, hence we won't be able to refer to its memmapped data.
a2a8927a 218 fn drop_serialized_data(&self, tcx: TyCtxt<'_>) {
c295e0f8
XL
219 // Load everything into memory so we can write it out to the on-disk
220 // cache. The vast majority of cacheable query results should already
221 // be in memory, so this should be a cheap operation.
222 // Do this *before* we clone 'latest_foreign_def_path_hashes', since
223 // loading existing queries may cause us to create new DepNodes, which
224 // may in turn end up invoking `store_foreign_def_id_hash`
3c0e092e 225 tcx.dep_graph.exec_cache_promotions(tcx);
c295e0f8
XL
226
227 *self.serialized_data.write() = None;
228 }
229
923072b8 230 fn serialize<'tcx>(&self, tcx: TyCtxt<'tcx>, encoder: FileEncoder) -> FileEncodeResult {
e1599b0c 231 // Serializing the `DepGraph` should not modify it.
2c00a5a8 232 tcx.dep_graph.with_ignore(|| {
e1599b0c 233 // Allocate `SourceFileIndex`es.
2c00a5a8 234 let (file_to_file_index, file_index_to_stable_id) = {
a1dfa0c6 235 let files = tcx.sess.source_map().files();
dfeec247
XL
236 let mut file_to_file_index =
237 FxHashMap::with_capacity_and_hasher(files.len(), Default::default());
238 let mut file_index_to_stable_id =
239 FxHashMap::with_capacity_and_hasher(files.len(), Default::default());
2c00a5a8 240
a1dfa0c6 241 for (index, file) in files.iter().enumerate() {
b7449926
XL
242 let index = SourceFileIndex(index as u32);
243 let file_ptr: *const SourceFile = &**file as *const _;
2c00a5a8 244 file_to_file_index.insert(file_ptr, index);
17df50a5
XL
245 let source_file_id = EncodedSourceFileId::new(tcx, &file);
246 file_index_to_stable_id.insert(index, source_file_id);
2c00a5a8 247 }
abe05a73 248
2c00a5a8
XL
249 (file_to_file_index, file_index_to_stable_id)
250 };
251
3dfed10e
XL
252 let hygiene_encode_context = HygieneEncodeContext::default();
253
2c00a5a8
XL
254 let mut encoder = CacheEncoder {
255 tcx,
256 encoder,
0bf4aa26
XL
257 type_shorthands: Default::default(),
258 predicate_shorthands: Default::default(),
0bf4aa26 259 interpret_allocs: Default::default(),
b7449926 260 source_map: CachingSourceMapView::new(tcx.sess.source_map()),
2c00a5a8 261 file_to_file_index,
3dfed10e 262 hygiene_context: &hygiene_encode_context,
f2b60f7d 263 symbol_table: Default::default(),
2c00a5a8
XL
264 };
265
e1599b0c 266 // Encode query results.
94222f64 267 let mut query_result_index = EncodedDepNodeIndex::new();
2c00a5a8 268
923072b8 269 tcx.sess.time("encode_query_results", || {
2c00a5a8
XL
270 let enc = &mut encoder;
271 let qri = &mut query_result_index;
923072b8
FG
272 QueryCtxt::from_tcx(tcx).encode_query_results(enc, qri);
273 });
ff7c6d11 274
94222f64
XL
275 // Encode side effects.
276 let side_effects_index: EncodedDepNodeIndex = self
277 .current_side_effects
dfeec247 278 .borrow()
0bf4aa26 279 .iter()
923072b8
FG
280 .map(|(dep_node_index, side_effects)| {
281 let pos = AbsoluteBytePos::new(encoder.position());
282 let dep_node_index = SerializedDepNodeIndex::new(dep_node_index.index());
283 encoder.encode_tagged(dep_node_index, side_effects);
5869c6ff 284
923072b8
FG
285 (dep_node_index, pos)
286 })
287 .collect();
2c00a5a8 288
0531ce1d
XL
289 let interpret_alloc_index = {
290 let mut interpret_alloc_index = Vec::new();
291 let mut n = 0;
292 loop {
3dfed10e 293 let new_n = encoder.interpret_allocs.len();
e1599b0c 294 // If we have found new IDs, serialize those too.
0531ce1d 295 if n == new_n {
e1599b0c 296 // Otherwise, abort.
0531ce1d
XL
297 break;
298 }
a1dfa0c6 299 interpret_alloc_index.reserve(new_n - n);
0531ce1d 300 for idx in n..new_n {
3dfed10e 301 let id = encoder.interpret_allocs[idx];
94b46f34 302 let pos = encoder.position() as u32;
0531ce1d 303 interpret_alloc_index.push(pos);
923072b8 304 interpret::specialized_encode_alloc_id(&mut encoder, tcx, id);
0531ce1d
XL
305 }
306 n = new_n;
307 }
308 interpret_alloc_index
309 };
310
3dfed10e 311 let mut syntax_contexts = FxHashMap::default();
136023e0
XL
312 let mut expn_data = UnhashMap::default();
313 let mut foreign_expn_data = UnhashMap::default();
3dfed10e
XL
314
315 // Encode all hygiene data (`SyntaxContextData` and `ExpnData`) from the current
316 // session.
317
318 hygiene_encode_context.encode(
319 &mut encoder,
923072b8 320 |encoder, index, ctxt_data| {
3dfed10e 321 let pos = AbsoluteBytePos::new(encoder.position());
923072b8 322 encoder.encode_tagged(TAG_SYNTAX_CONTEXT, ctxt_data);
3dfed10e 323 syntax_contexts.insert(index, pos);
3dfed10e 324 },
923072b8 325 |encoder, expn_id, data, hash| {
136023e0
XL
326 if expn_id.krate == LOCAL_CRATE {
327 let pos = AbsoluteBytePos::new(encoder.position());
923072b8 328 encoder.encode_tagged(TAG_EXPN_DATA, data);
136023e0
XL
329 expn_data.insert(hash, pos);
330 } else {
331 foreign_expn_data.insert(hash, expn_id.local_id.as_u32());
332 }
3dfed10e 333 },
923072b8 334 );
3dfed10e
XL
335
336 // `Encode the file footer.
2c00a5a8 337 let footer_pos = encoder.position() as u64;
dfeec247
XL
338 encoder.encode_tagged(
339 TAG_FILE_FOOTER,
340 &Footer {
341 file_index_to_stable_id,
dfeec247 342 query_result_index,
94222f64 343 side_effects_index,
dfeec247 344 interpret_alloc_index,
3dfed10e 345 syntax_contexts,
136023e0
XL
346 expn_data,
347 foreign_expn_data,
dfeec247 348 },
923072b8 349 );
2c00a5a8
XL
350
351 // Encode the position of the footer as the last 8 bytes of the
352 // file so we know where to look for it.
923072b8 353 IntEncodedWithFixedSize(footer_pos).encode(&mut encoder.encoder);
2c00a5a8
XL
354
355 // DO NOT WRITE ANYTHING TO THE ENCODER AFTER THIS POINT! The address
356 // of the footer must be the last thing in the data stream.
357
923072b8 358 encoder.finish()
2c00a5a8 359 })
abe05a73 360 }
136023e0
XL
361}
362
363impl<'sess> OnDiskCache<'sess> {
364 pub fn as_dyn(&self) -> &dyn rustc_middle::ty::OnDiskCache<'sess> {
365 self as _
366 }
367
94222f64
XL
368 /// Loads a `QuerySideEffects` created during the previous compilation session.
369 pub fn load_side_effects(
dc9dc135 370 &self,
416331ca 371 tcx: TyCtxt<'_>,
dc9dc135 372 dep_node_index: SerializedDepNodeIndex,
94222f64
XL
373 ) -> QuerySideEffects {
374 let side_effects: Option<QuerySideEffects> =
5099ac24 375 self.load_indexed(tcx, dep_node_index, &self.prev_side_effects_index);
ff7c6d11 376
94222f64 377 side_effects.unwrap_or_default()
abe05a73
XL
378 }
379
94222f64
XL
380 /// Stores a `QuerySideEffects` emitted during the current compilation session.
381 /// Anything stored like this will be available via `load_side_effects` in
abe05a73 382 /// the next compilation session.
0731742a
XL
383 #[inline(never)]
384 #[cold]
94222f64
XL
385 pub fn store_side_effects(&self, dep_node_index: DepNodeIndex, side_effects: QuerySideEffects) {
386 let mut current_side_effects = self.current_side_effects.borrow_mut();
387 let prev = current_side_effects.insert(dep_node_index, side_effects);
abe05a73
XL
388 debug_assert!(prev.is_none());
389 }
390
ff7c6d11 391 /// Returns the cached query result if there is something in the cache for
9fa01778 392 /// the given `SerializedDepNodeIndex`; otherwise returns `None`.
6a06907d 393 pub fn try_load_query_result<'tcx, T>(
dc9dc135 394 &self,
3dfed10e 395 tcx: TyCtxt<'tcx>,
dc9dc135
XL
396 dep_node_index: SerializedDepNodeIndex,
397 ) -> Option<T>
398 where
3dfed10e 399 T: for<'a> Decodable<CacheDecoder<'a, 'tcx>>,
abe05a73 400 {
5099ac24 401 self.load_indexed(tcx, dep_node_index, &self.query_result_index)
ff7c6d11
XL
402 }
403
94222f64 404 /// Stores side effect emitted during computation of an anonymous query.
ff7c6d11
XL
405 /// Since many anonymous queries can share the same `DepNode`, we aggregate
406 /// them -- as opposed to regular queries where we assume that there is a
407 /// 1:1 relationship between query-key and `DepNode`.
0731742a
XL
408 #[inline(never)]
409 #[cold]
94222f64 410 pub fn store_side_effects_for_anon_node(
dfeec247
XL
411 &self,
412 dep_node_index: DepNodeIndex,
94222f64 413 side_effects: QuerySideEffects,
dfeec247 414 ) {
94222f64 415 let mut current_side_effects = self.current_side_effects.borrow_mut();
ff7c6d11 416
94222f64
XL
417 let x = current_side_effects.entry(dep_node_index).or_default();
418 x.append(side_effects);
ff7c6d11
XL
419 }
420
dc9dc135
XL
421 fn load_indexed<'tcx, T>(
422 &self,
423 tcx: TyCtxt<'tcx>,
424 dep_node_index: SerializedDepNodeIndex,
425 index: &FxHashMap<SerializedDepNodeIndex, AbsoluteBytePos>,
dc9dc135
XL
426 ) -> Option<T>
427 where
3dfed10e 428 T: for<'a> Decodable<CacheDecoder<'a, 'tcx>>,
ff7c6d11 429 {
0731742a 430 let pos = index.get(&dep_node_index).cloned()?;
abe05a73 431
5099ac24 432 self.with_decoder(tcx, pos, |decoder| Some(decode_tagged(decoder, dep_node_index)))
3dfed10e
XL
433 }
434
c295e0f8 435 fn with_decoder<'a, 'tcx, T, F: for<'s> FnOnce(&mut CacheDecoder<'s, 'tcx>) -> T>(
3dfed10e
XL
436 &'sess self,
437 tcx: TyCtxt<'tcx>,
438 pos: AbsoluteBytePos,
439 f: F,
440 ) -> T
441 where
442 T: Decodable<CacheDecoder<'a, 'tcx>>,
443 {
c295e0f8 444 let serialized_data = self.serialized_data.read();
abe05a73 445 let mut decoder = CacheDecoder {
ff7c6d11 446 tcx,
923072b8 447 opaque: MemDecoder::new(serialized_data.as_deref().unwrap_or(&[]), pos.to_usize()),
b7449926 448 source_map: self.source_map,
0531ce1d 449 file_index_to_file: &self.file_index_to_file,
ff7c6d11 450 file_index_to_stable_id: &self.file_index_to_stable_id,
94b46f34 451 alloc_decoding_session: self.alloc_decoding_state.new_decoding_session(),
3dfed10e
XL
452 syntax_contexts: &self.syntax_contexts,
453 expn_data: &self.expn_data,
136023e0 454 foreign_expn_data: &self.foreign_expn_data,
3dfed10e 455 hygiene_context: &self.hygiene_context,
abe05a73 456 };
3dfed10e 457 f(&mut decoder)
abe05a73 458 }
abe05a73
XL
459}
460
abe05a73
XL
461//- DECODING -------------------------------------------------------------------
462
fc512014 463/// A decoder that can read from the incremental compilation cache. It is similar to the one
e1599b0c
XL
464/// we use for crate metadata decoding in that it can rebase spans and eventually
465/// will also handle things that contain `Ty` instances.
6a06907d 466pub struct CacheDecoder<'a, 'tcx> {
dc9dc135 467 tcx: TyCtxt<'tcx>,
923072b8 468 opaque: MemDecoder<'a>,
dc9dc135 469 source_map: &'a SourceMap,
dc9dc135 470 file_index_to_file: &'a Lock<FxHashMap<SourceFileIndex, Lrc<SourceFile>>>,
17df50a5 471 file_index_to_stable_id: &'a FxHashMap<SourceFileIndex, EncodedSourceFileId>,
dc9dc135 472 alloc_decoding_session: AllocDecodingSession<'a>,
3dfed10e 473 syntax_contexts: &'a FxHashMap<u32, AbsoluteBytePos>,
136023e0
XL
474 expn_data: &'a UnhashMap<ExpnHash, AbsoluteBytePos>,
475 foreign_expn_data: &'a UnhashMap<ExpnHash, u32>,
3dfed10e 476 hygiene_context: &'a HygieneDecodeContext,
abe05a73
XL
477}
478
dc9dc135 479impl<'a, 'tcx> CacheDecoder<'a, 'tcx> {
b7449926 480 fn file_index_to_file(&self, index: SourceFileIndex) -> Lrc<SourceFile> {
ff7c6d11 481 let CacheDecoder {
c295e0f8 482 tcx,
0531ce1d 483 ref file_index_to_file,
ff7c6d11 484 ref file_index_to_stable_id,
b7449926 485 ref source_map,
ff7c6d11
XL
486 ..
487 } = *self;
488
dfeec247
XL
489 file_index_to_file
490 .borrow_mut()
491 .entry(index)
492 .or_insert_with(|| {
c295e0f8 493 let stable_id = file_index_to_stable_id[&index].translate(tcx);
a2a8927a
XL
494
495 // If this `SourceFile` is from a foreign crate, then make sure
496 // that we've imported all of the source files from that crate.
497 // This has usually already been done during macro invocation.
498 // However, when encoding query results like `TypeckResults`,
499 // we might encode an `AdtDef` for a foreign type (because it
500 // was referenced in the body of the function). There is no guarantee
501 // that we will load the source files from that crate during macro
502 // expansion, so we use `import_source_files` to ensure that the foreign
503 // source files are actually imported before we call `source_file_by_stable_id`.
504 if stable_id.cnum != LOCAL_CRATE {
505 self.tcx.cstore_untracked().import_source_files(self.tcx.sess, stable_id.cnum);
506 }
507
dfeec247
XL
508 source_map
509 .source_file_by_stable_id(stable_id)
510 .expect("failed to lookup `SourceFile` in new context")
511 })
512 .clone()
ff7c6d11
XL
513 }
514}
515
516trait DecoderWithPosition: Decoder {
517 fn position(&self) -> usize;
518}
519
923072b8 520impl<'a> DecoderWithPosition for MemDecoder<'a> {
ff7c6d11
XL
521 fn position(&self) -> usize {
522 self.position()
523 }
524}
abe05a73 525
dc9dc135 526impl<'a, 'tcx> DecoderWithPosition for CacheDecoder<'a, 'tcx> {
ff7c6d11
XL
527 fn position(&self) -> usize {
528 self.opaque.position()
abe05a73
XL
529 }
530}
531
e1599b0c 532// Decodes something that was encoded with `encode_tagged()` and verify that the
abe05a73 533// tag matches and the correct amount of bytes was read.
5099ac24 534fn decode_tagged<D, T, V>(decoder: &mut D, expected_tag: T) -> V
dc9dc135 535where
29967ef6 536 T: Decodable<D> + Eq + std::fmt::Debug,
3dfed10e 537 V: Decodable<D>,
dc9dc135 538 D: DecoderWithPosition,
abe05a73
XL
539{
540 let start_pos = decoder.position();
541
5099ac24 542 let actual_tag = T::decode(decoder);
abe05a73 543 assert_eq!(actual_tag, expected_tag);
5099ac24 544 let value = V::decode(decoder);
abe05a73
XL
545 let end_pos = decoder.position();
546
5099ac24 547 let expected_len: u64 = Decodable::decode(decoder);
abe05a73
XL
548 assert_eq!((end_pos - start_pos) as u64, expected_len);
549
5099ac24 550 value
abe05a73
XL
551}
552
923072b8
FG
553impl<'a, 'tcx> TyDecoder for CacheDecoder<'a, 'tcx> {
554 type I = TyCtxt<'tcx>;
3dfed10e
XL
555 const CLEAR_CROSS_CRATE: bool = false;
556
abe05a73 557 #[inline]
923072b8 558 fn interner(&self) -> TyCtxt<'tcx> {
ff7c6d11 559 self.tcx
abe05a73
XL
560 }
561
562 #[inline]
563 fn position(&self) -> usize {
564 self.opaque.position()
565 }
566
567 #[inline]
568 fn peek_byte(&self) -> u8 {
569 self.opaque.data[self.opaque.position()]
570 }
571
5099ac24 572 fn cached_ty_for_shorthand<F>(&mut self, shorthand: usize, or_insert_with: F) -> Ty<'tcx>
dfeec247 573 where
5099ac24 574 F: FnOnce(&mut Self) -> Ty<'tcx>,
abe05a73 575 {
923072b8 576 let tcx = self.tcx;
abe05a73 577
17df50a5 578 let cache_key = ty::CReaderCacheKey { cnum: None, pos: shorthand };
abe05a73 579
f035d41b 580 if let Some(&ty) = tcx.ty_rcache.borrow().get(&cache_key) {
5099ac24 581 return ty;
abe05a73
XL
582 }
583
5099ac24 584 let ty = or_insert_with(self);
e1599b0c 585 // This may overwrite the entry, but it should overwrite with the same value.
f035d41b 586 tcx.ty_rcache.borrow_mut().insert_same(cache_key, ty);
5099ac24 587 ty
abe05a73
XL
588 }
589
590 fn with_position<F, R>(&mut self, pos: usize, f: F) -> R
dfeec247
XL
591 where
592 F: FnOnce(&mut Self) -> R,
abe05a73
XL
593 {
594 debug_assert!(pos < self.opaque.data.len());
595
923072b8 596 let new_opaque = MemDecoder::new(self.opaque.data, pos);
abe05a73
XL
597 let old_opaque = mem::replace(&mut self.opaque, new_opaque);
598 let r = f(self);
599 self.opaque = old_opaque;
600 r
601 }
602
5099ac24 603 fn decode_alloc_id(&mut self) -> interpret::AllocId {
94b46f34
XL
604 let alloc_decoding_session = self.alloc_decoding_session;
605 alloc_decoding_session.decode_alloc_id(self)
0531ce1d
XL
606 }
607}
4462d4a0 608
136023e0 609rustc_middle::implement_ty_decoder!(CacheDecoder<'a, 'tcx>);
3dfed10e 610
5869c6ff
XL
611// This ensures that the `Decodable<opaque::Decoder>::decode` specialization for `Vec<u8>` is used
612// when a `CacheDecoder` is passed to `Decodable::decode`. Unfortunately, we have to manually opt
613// into specializations this way, given how `CacheDecoder` and the decoding traits currently work.
614impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for Vec<u8> {
5099ac24 615 fn decode(d: &mut CacheDecoder<'a, 'tcx>) -> Self {
5869c6ff
XL
616 Decodable::decode(&mut d.opaque)
617 }
618}
619
3dfed10e 620impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for SyntaxContext {
5099ac24 621 fn decode(decoder: &mut CacheDecoder<'a, 'tcx>) -> Self {
3dfed10e
XL
622 let syntax_contexts = decoder.syntax_contexts;
623 rustc_span::hygiene::decode_syntax_context(decoder, decoder.hygiene_context, |this, id| {
624 // This closure is invoked if we haven't already decoded the data for the `SyntaxContext` we are deserializing.
625 // We look up the position of the associated `SyntaxData` and decode it.
626 let pos = syntax_contexts.get(&id).unwrap();
627 this.with_position(pos.to_usize(), |decoder| {
5099ac24
FG
628 let data: SyntaxContextData = decode_tagged(decoder, TAG_SYNTAX_CONTEXT);
629 data
3dfed10e
XL
630 })
631 })
632 }
633}
634
635impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for ExpnId {
5099ac24
FG
636 fn decode(decoder: &mut CacheDecoder<'a, 'tcx>) -> Self {
637 let hash = ExpnHash::decode(decoder);
136023e0 638 if hash.is_root() {
5099ac24 639 return ExpnId::root();
136023e0
XL
640 }
641
642 if let Some(expn_id) = ExpnId::from_hash(hash) {
5099ac24 643 return expn_id;
136023e0
XL
644 }
645
c295e0f8 646 let krate = decoder.tcx.stable_crate_id_to_crate_num(hash.stable_crate_id());
136023e0
XL
647
648 let expn_id = if krate == LOCAL_CRATE {
649 // We look up the position of the associated `ExpnData` and decode it.
650 let pos = decoder
651 .expn_data
652 .get(&hash)
653 .unwrap_or_else(|| panic!("Bad hash {:?} (map {:?})", hash, decoder.expn_data));
654
655 let data: ExpnData = decoder
5099ac24 656 .with_position(pos.to_usize(), |decoder| decode_tagged(decoder, TAG_EXPN_DATA));
c295e0f8
XL
657 let expn_id = rustc_span::hygiene::register_local_expn_id(data, hash);
658
659 #[cfg(debug_assertions)]
660 {
661 use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
064997fb
FG
662 let local_hash: u64 = decoder.tcx.with_stable_hashing_context(|mut hcx| {
663 let mut hasher = StableHasher::new();
664 expn_id.expn_data().hash_stable(&mut hcx, &mut hasher);
665 hasher.finish()
c295e0f8 666 });
c295e0f8
XL
667 debug_assert_eq!(hash.local_hash(), local_hash);
668 }
669
670 expn_id
136023e0
XL
671 } else {
672 let index_guess = decoder.foreign_expn_data[&hash];
c295e0f8
XL
673 decoder.tcx.cstore_untracked().expn_hash_to_expn_id(
674 decoder.tcx.sess,
675 krate,
676 index_guess,
677 hash,
678 )
136023e0
XL
679 };
680
c295e0f8 681 debug_assert_eq!(expn_id.krate, krate);
5099ac24 682 expn_id
3dfed10e
XL
683 }
684}
685
686impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for Span {
5099ac24
FG
687 fn decode(decoder: &mut CacheDecoder<'a, 'tcx>) -> Self {
688 let ctxt = SyntaxContext::decode(decoder);
689 let parent = Option::<LocalDefId>::decode(decoder);
690 let tag: u8 = Decodable::decode(decoder);
ff7c6d11 691
5869c6ff 692 if tag == TAG_PARTIAL_SPAN {
5099ac24 693 return Span::new(BytePos(0), BytePos(0), ctxt, parent);
c295e0f8 694 } else if tag == TAG_RELATIVE_SPAN {
5099ac24
FG
695 let dlo = u32::decode(decoder);
696 let dto = u32::decode(decoder);
c295e0f8 697
923072b8 698 let enclosing = decoder.tcx.source_span_untracked(parent.unwrap()).data_untracked();
c295e0f8
XL
699 let span = Span::new(
700 enclosing.lo + BytePos::from_u32(dlo),
701 enclosing.lo + BytePos::from_u32(dto),
702 ctxt,
703 parent,
704 );
705
5099ac24 706 return span;
ff7c6d11 707 } else {
5869c6ff 708 debug_assert_eq!(tag, TAG_FULL_SPAN);
abe05a73
XL
709 }
710
5099ac24
FG
711 let file_lo_index = SourceFileIndex::decode(decoder);
712 let line_lo = usize::decode(decoder);
713 let col_lo = BytePos::decode(decoder);
714 let len = BytePos::decode(decoder);
ff7c6d11 715
3dfed10e 716 let file_lo = decoder.file_index_to_file(file_lo_index);
923072b8 717 let lo = file_lo.lines(|lines| lines[line_lo - 1] + col_lo);
ff7c6d11
XL
718 let hi = lo + len;
719
5099ac24 720 Span::new(lo, hi, ctxt, parent)
abe05a73
XL
721 }
722}
723
f2b60f7d
FG
724// copy&paste impl from rustc_metadata
725impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for Symbol {
726 fn decode(d: &mut CacheDecoder<'a, 'tcx>) -> Self {
727 let tag = d.read_u8();
728
729 match tag {
730 SYMBOL_STR => {
731 let s = d.read_str();
732 Symbol::intern(s)
733 }
734 SYMBOL_OFFSET => {
735 // read str offset
736 let pos = d.read_usize();
737 let old_pos = d.opaque.position();
738
739 // move to str ofset and read
740 d.opaque.set_position(pos);
741 let s = d.read_str();
742 let sym = Symbol::intern(s);
743
744 // restore position
745 d.opaque.set_position(old_pos);
746
747 sym
748 }
749 SYMBOL_PREINTERNED => {
750 let symbol_index = d.read_u32();
751 Symbol::new_from_decoded(symbol_index)
752 }
753 _ => unreachable!(),
754 }
755 }
756}
757
3dfed10e 758impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for CrateNum {
5099ac24
FG
759 fn decode(d: &mut CacheDecoder<'a, 'tcx>) -> Self {
760 let stable_id = StableCrateId::decode(d);
c295e0f8 761 let cnum = d.tcx.stable_crate_id_to_crate_num(stable_id);
5099ac24 762 cnum
e1599b0c
XL
763 }
764}
765
abe05a73 766// This impl makes sure that we get a runtime error when we try decode a
e1599b0c
XL
767// `DefIndex` that is not contained in a `DefId`. Such a case would be problematic
768// because we would not know how to transform the `DefIndex` to the current
abe05a73 769// context.
3dfed10e 770impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for DefIndex {
5099ac24
FG
771 fn decode(_d: &mut CacheDecoder<'a, 'tcx>) -> DefIndex {
772 panic!("trying to decode `DefIndex` outside the context of a `DefId`")
abe05a73
XL
773 }
774}
775
e1599b0c
XL
776// Both the `CrateNum` and the `DefIndex` of a `DefId` can change in between two
777// compilation sessions. We use the `DefPathHash`, which is stable across
778// sessions, to map the old `DefId` to the new one.
3dfed10e 779impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for DefId {
5099ac24 780 fn decode(d: &mut CacheDecoder<'a, 'tcx>) -> Self {
e1599b0c 781 // Load the `DefPathHash` which is was we encoded the `DefId` as.
5099ac24 782 let def_path_hash = DefPathHash::decode(d);
abe05a73 783
e1599b0c 784 // Using the `DefPathHash`, we can lookup the new `DefId`.
fc512014
XL
785 // Subtle: We only encode a `DefId` as part of a query result.
786 // If we get to this point, then all of the query inputs were green,
787 // which means that the definition with this hash is guaranteed to
788 // still exist in the current compilation session.
923072b8 789 d.tcx.def_path_hash_to_def_id(def_path_hash, &mut || {
5099ac24
FG
790 panic!("Failed to convert DefPathHash {:?}", def_path_hash)
791 })
abe05a73
XL
792 }
793}
794
3dfed10e 795impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for &'tcx FxHashSet<LocalDefId> {
5099ac24 796 fn decode(d: &mut CacheDecoder<'a, 'tcx>) -> Self {
3dfed10e
XL
797 RefDecodable::decode(d)
798 }
799}
800
f2b60f7d
FG
801impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for &'tcx FxHashMap<DefId, Ty<'tcx>> {
802 fn decode(d: &mut CacheDecoder<'a, 'tcx>) -> Self {
803 RefDecodable::decode(d)
804 }
805}
806
3dfed10e
XL
807impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>>
808 for &'tcx IndexVec<mir::Promoted, mir::Body<'tcx>>
809{
5099ac24 810 fn decode(d: &mut CacheDecoder<'a, 'tcx>) -> Self {
3dfed10e
XL
811 RefDecodable::decode(d)
812 }
813}
814
064997fb 815impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for &'tcx [ty::abstract_const::Node<'tcx>] {
5099ac24 816 fn decode(d: &mut CacheDecoder<'a, 'tcx>) -> Self {
1b1a35ee
XL
817 RefDecodable::decode(d)
818 }
819}
820
3dfed10e 821impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for &'tcx [(ty::Predicate<'tcx>, Span)] {
5099ac24 822 fn decode(d: &mut CacheDecoder<'a, 'tcx>) -> Self {
3dfed10e
XL
823 RefDecodable::decode(d)
824 }
825}
826
827impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for &'tcx [rustc_ast::InlineAsmTemplatePiece] {
5099ac24 828 fn decode(d: &mut CacheDecoder<'a, 'tcx>) -> Self {
3dfed10e
XL
829 RefDecodable::decode(d)
830 }
831}
832
923072b8
FG
833macro_rules! impl_ref_decoder {
834 (<$tcx:tt> $($ty:ty,)*) => {
835 $(impl<'a, $tcx> Decodable<CacheDecoder<'a, $tcx>> for &$tcx [$ty] {
836 fn decode(d: &mut CacheDecoder<'a, $tcx>) -> Self {
837 RefDecodable::decode(d)
838 }
839 })*
840 };
2c00a5a8
XL
841}
842
923072b8
FG
843impl_ref_decoder! {<'tcx>
844 Span,
845 rustc_ast::Attribute,
846 rustc_span::symbol::Ident,
847 ty::Variance,
848 rustc_span::def_id::DefId,
849 rustc_span::def_id::LocalDefId,
850 (rustc_middle::middle::exported_symbols::ExportedSymbol<'tcx>, rustc_middle::middle::exported_symbols::SymbolExportInfo),
5869c6ff
XL
851}
852
923072b8 853//- ENCODING -------------------------------------------------------------------
5869c6ff 854
fc512014 855/// An encoder that can write to the incremental compilation cache.
923072b8 856pub struct CacheEncoder<'a, 'tcx> {
dc9dc135 857 tcx: TyCtxt<'tcx>,
923072b8 858 encoder: FileEncoder,
48663c56 859 type_shorthands: FxHashMap<Ty<'tcx>, usize>,
5869c6ff 860 predicate_shorthands: FxHashMap<ty::PredicateKind<'tcx>, usize>,
3dfed10e 861 interpret_allocs: FxIndexSet<interpret::AllocId>,
b7449926
XL
862 source_map: CachingSourceMapView<'tcx>,
863 file_to_file_index: FxHashMap<*const SourceFile, SourceFileIndex>,
3dfed10e 864 hygiene_context: &'a HygieneEncodeContext,
f2b60f7d 865 symbol_table: FxHashMap<Symbol, usize>,
abe05a73
XL
866}
867
923072b8 868impl<'a, 'tcx> CacheEncoder<'a, 'tcx> {
b7449926
XL
869 fn source_file_index(&mut self, source_file: Lrc<SourceFile>) -> SourceFileIndex {
870 self.file_to_file_index[&(&*source_file as *const SourceFile)]
ff7c6d11
XL
871 }
872
abe05a73
XL
873 /// Encode something with additional information that allows to do some
874 /// sanity checks when decoding the data again. This method will first
875 /// encode the specified tag, then the given value, then the number of
876 /// bytes taken up by tag and value. On decoding, we can then verify that
877 /// we get the expected tag and read the expected number of bytes.
923072b8 878 fn encode_tagged<T: Encodable<Self>, V: Encodable<Self>>(&mut self, tag: T, value: &V) {
abe05a73
XL
879 let start_pos = self.position();
880
923072b8
FG
881 tag.encode(self);
882 value.encode(self);
abe05a73
XL
883
884 let end_pos = self.position();
923072b8
FG
885 ((end_pos - start_pos) as u64).encode(self);
886 }
887
888 fn finish(self) -> Result<usize, io::Error> {
889 self.encoder.finish()
abe05a73
XL
890 }
891}
892
923072b8
FG
893impl<'a, 'tcx> Encodable<CacheEncoder<'a, 'tcx>> for SyntaxContext {
894 fn encode(&self, s: &mut CacheEncoder<'a, 'tcx>) {
895 rustc_span::hygiene::raw_encode_syntax_context(*self, s.hygiene_context, s);
3dfed10e
XL
896 }
897}
0531ce1d 898
923072b8
FG
899impl<'a, 'tcx> Encodable<CacheEncoder<'a, 'tcx>> for ExpnId {
900 fn encode(&self, s: &mut CacheEncoder<'a, 'tcx>) {
136023e0 901 s.hygiene_context.schedule_expn_data_for_encoding(*self);
923072b8 902 self.expn_hash().encode(s);
0531ce1d
XL
903 }
904}
905
923072b8
FG
906impl<'a, 'tcx> Encodable<CacheEncoder<'a, 'tcx>> for Span {
907 fn encode(&self, s: &mut CacheEncoder<'a, 'tcx>) {
c295e0f8 908 let span_data = self.data_untracked();
923072b8
FG
909 span_data.ctxt.encode(s);
910 span_data.parent.encode(s);
c295e0f8
XL
911
912 if span_data.is_dummy() {
913 return TAG_PARTIAL_SPAN.encode(s);
914 }
915
916 if let Some(parent) = span_data.parent {
923072b8 917 let enclosing = s.tcx.source_span(parent).data_untracked();
c295e0f8 918 if enclosing.contains(span_data) {
923072b8
FG
919 TAG_RELATIVE_SPAN.encode(s);
920 (span_data.lo - enclosing.lo).to_u32().encode(s);
921 (span_data.hi - enclosing.lo).to_u32().encode(s);
922 return;
c295e0f8 923 }
ff7c6d11
XL
924 }
925
5869c6ff
XL
926 let pos = s.source_map.byte_pos_to_line_and_col(span_data.lo);
927 let partial_span = match &pos {
928 Some((file_lo, _, _)) => !file_lo.contains(span_data.hi),
929 None => true,
3dfed10e 930 };
ff7c6d11 931
5869c6ff 932 if partial_span {
c295e0f8 933 return TAG_PARTIAL_SPAN.encode(s);
ff7c6d11
XL
934 }
935
5869c6ff
XL
936 let (file_lo, line_lo, col_lo) = pos.unwrap();
937
ff7c6d11
XL
938 let len = span_data.hi - span_data.lo;
939
3dfed10e 940 let source_file_index = s.source_file_index(file_lo);
ff7c6d11 941
923072b8
FG
942 TAG_FULL_SPAN.encode(s);
943 source_file_index.encode(s);
944 line_lo.encode(s);
945 col_lo.encode(s);
946 len.encode(s);
ff7c6d11
XL
947 }
948}
949
f2b60f7d
FG
950// copy&paste impl from rustc_metadata
951impl<'a, 'tcx> Encodable<CacheEncoder<'a, 'tcx>> for Symbol {
952 fn encode(&self, s: &mut CacheEncoder<'a, 'tcx>) {
953 // if symbol preinterned, emit tag and symbol index
954 if self.is_preinterned() {
955 s.encoder.emit_u8(SYMBOL_PREINTERNED);
956 s.encoder.emit_u32(self.as_u32());
957 } else {
958 // otherwise write it as string or as offset to it
959 match s.symbol_table.entry(*self) {
960 Entry::Vacant(o) => {
961 s.encoder.emit_u8(SYMBOL_STR);
962 let pos = s.encoder.position();
963 o.insert(pos);
964 s.emit_str(self.as_str());
965 }
966 Entry::Occupied(o) => {
967 let x = o.get().clone();
968 s.emit_u8(SYMBOL_OFFSET);
969 s.emit_usize(x);
970 }
971 }
972 }
973 }
974}
975
923072b8
FG
976impl<'a, 'tcx> TyEncoder for CacheEncoder<'a, 'tcx> {
977 type I = TyCtxt<'tcx>;
3dfed10e 978 const CLEAR_CROSS_CRATE: bool = false;
e1599b0c 979
abe05a73 980 fn position(&self) -> usize {
5869c6ff 981 self.encoder.position()
abe05a73 982 }
3dfed10e
XL
983 fn type_shorthands(&mut self) -> &mut FxHashMap<Ty<'tcx>, usize> {
984 &mut self.type_shorthands
abe05a73 985 }
5869c6ff 986 fn predicate_shorthands(&mut self) -> &mut FxHashMap<ty::PredicateKind<'tcx>, usize> {
3dfed10e 987 &mut self.predicate_shorthands
abe05a73 988 }
923072b8 989 fn encode_alloc_id(&mut self, alloc_id: &interpret::AllocId) {
3dfed10e 990 let (index, _) = self.interpret_allocs.insert_full(*alloc_id);
abe05a73 991
923072b8 992 index.encode(self);
abe05a73
XL
993 }
994}
995
923072b8
FG
996impl<'a, 'tcx> Encodable<CacheEncoder<'a, 'tcx>> for CrateNum {
997 fn encode(&self, s: &mut CacheEncoder<'a, 'tcx>) {
998 s.tcx.stable_crate_id(*self).encode(s);
17df50a5
XL
999 }
1000}
1001
923072b8
FG
1002impl<'a, 'tcx> Encodable<CacheEncoder<'a, 'tcx>> for DefId {
1003 fn encode(&self, s: &mut CacheEncoder<'a, 'tcx>) {
1004 s.tcx.def_path_hash(*self).encode(s);
abe05a73
XL
1005 }
1006}
1007
923072b8
FG
1008impl<'a, 'tcx> Encodable<CacheEncoder<'a, 'tcx>> for DefIndex {
1009 fn encode(&self, _: &mut CacheEncoder<'a, 'tcx>) {
e1599b0c 1010 bug!("encoding `DefIndex` without context");
abe05a73
XL
1011 }
1012}
1013
abe05a73
XL
1014macro_rules! encoder_methods {
1015 ($($name:ident($ty:ty);)*) => {
74b04a01 1016 #[inline]
923072b8 1017 $(fn $name(&mut self, value: $ty) {
abe05a73
XL
1018 self.encoder.$name(value)
1019 })*
1020 }
1021}
1022
923072b8 1023impl<'a, 'tcx> Encoder for CacheEncoder<'a, 'tcx> {
abe05a73
XL
1024 encoder_methods! {
1025 emit_usize(usize);
1026 emit_u128(u128);
1027 emit_u64(u64);
1028 emit_u32(u32);
1029 emit_u16(u16);
1030 emit_u8(u8);
1031
1032 emit_isize(isize);
1033 emit_i128(i128);
1034 emit_i64(i64);
1035 emit_i32(i32);
1036 emit_i16(i16);
1037 emit_i8(i8);
1038
1039 emit_bool(bool);
1040 emit_f64(f64);
1041 emit_f32(f32);
1042 emit_char(char);
1043 emit_str(&str);
cdc7bbd5 1044 emit_raw_bytes(&[u8]);
abe05a73
XL
1045 }
1046}
1047
5869c6ff
XL
1048// This ensures that the `Encodable<opaque::FileEncoder>::encode` specialization for byte slices
1049// is used when a `CacheEncoder` having an `opaque::FileEncoder` is passed to `Encodable::encode`.
1050// Unfortunately, we have to manually opt into specializations this way, given how `CacheEncoder`
1051// and the encoding traits currently work.
923072b8
FG
1052impl<'a, 'tcx> Encodable<CacheEncoder<'a, 'tcx>> for [u8] {
1053 fn encode(&self, e: &mut CacheEncoder<'a, 'tcx>) {
1054 self.encode(&mut e.encoder);
5869c6ff
XL
1055 }
1056}
1057
6a06907d
XL
1058pub fn encode_query_results<'a, 'tcx, CTX, Q>(
1059 tcx: CTX,
923072b8 1060 encoder: &mut CacheEncoder<'a, 'tcx>,
94222f64 1061 query_result_index: &mut EncodedDepNodeIndex,
923072b8 1062) where
6a06907d 1063 CTX: QueryContext + 'tcx,
3c0e092e 1064 Q: super::QueryDescription<CTX>,
923072b8 1065 Q::Value: Encodable<CacheEncoder<'a, 'tcx>>,
abe05a73 1066{
74b04a01 1067 let _timer = tcx
6a06907d
XL
1068 .dep_context()
1069 .profiler()
29967ef6 1070 .extra_verbose_generic_activity("encode_query_results_for", std::any::type_name::<Q>());
dfeec247 1071
6a06907d
XL
1072 assert!(Q::query_state(tcx).all_inactive());
1073 let cache = Q::query_cache(tcx);
5e7ed085 1074 cache.iter(&mut |key, value, dep_node| {
3c0e092e 1075 if Q::cache_on_disk(*tcx.dep_context(), &key) {
cdc7bbd5
XL
1076 let dep_node = SerializedDepNodeIndex::new(dep_node.index());
1077
1078 // Record position of the cache entry.
1079 query_result_index.push((dep_node, AbsoluteBytePos::new(encoder.encoder.position())));
1080
1081 // Encode the type check tables with the `SerializedDepNodeIndex`
1082 // as tag.
923072b8 1083 encoder.encode_tagged(dep_node, value);
74b04a01 1084 }
cdc7bbd5 1085 });
abe05a73 1086}