]>
Commit | Line | Data |
---|---|---|
6a06907d XL |
1 | //! The implementation of the query system itself. This defines the macros that |
2 | //! generate the actual methods on tcx which find and execute the provider, | |
3 | //! manage the caches, and so forth. | |
4 | ||
136023e0 | 5 | use crate::{on_disk_cache, queries, Queries}; |
17df50a5 | 6 | use rustc_middle::dep_graph::{DepKind, DepNode, DepNodeIndex, SerializedDepNodeIndex}; |
6a06907d XL |
7 | use rustc_middle::ty::tls::{self, ImplicitCtxt}; |
8 | use rustc_middle::ty::{self, TyCtxt}; | |
9 | use rustc_query_system::dep_graph::HasDepContext; | |
94222f64 XL |
10 | use rustc_query_system::query::{ |
11 | QueryContext, QueryDescription, QueryJobId, QueryMap, QuerySideEffects, | |
12 | }; | |
6a06907d XL |
13 | |
14 | use rustc_data_structures::sync::Lock; | |
15 | use rustc_data_structures::thin_vec::ThinVec; | |
136023e0 | 16 | use rustc_errors::{Diagnostic, Handler}; |
6a06907d | 17 | use rustc_serialize::opaque; |
17df50a5 | 18 | use rustc_span::def_id::LocalDefId; |
6a06907d | 19 | |
136023e0 XL |
20 | use std::any::Any; |
21 | ||
6a06907d XL |
22 | #[derive(Copy, Clone)] |
23 | pub struct QueryCtxt<'tcx> { | |
24 | pub tcx: TyCtxt<'tcx>, | |
136023e0 | 25 | pub queries: &'tcx Queries<'tcx>, |
6a06907d XL |
26 | } |
27 | ||
28 | impl<'tcx> std::ops::Deref for QueryCtxt<'tcx> { | |
29 | type Target = TyCtxt<'tcx>; | |
30 | ||
17df50a5 | 31 | #[inline] |
6a06907d XL |
32 | fn deref(&self) -> &Self::Target { |
33 | &self.tcx | |
34 | } | |
35 | } | |
36 | ||
37 | impl HasDepContext for QueryCtxt<'tcx> { | |
38 | type DepKind = rustc_middle::dep_graph::DepKind; | |
39 | type StableHashingContext = rustc_middle::ich::StableHashingContext<'tcx>; | |
40 | type DepContext = TyCtxt<'tcx>; | |
41 | ||
42 | #[inline] | |
43 | fn dep_context(&self) -> &Self::DepContext { | |
44 | &self.tcx | |
45 | } | |
46 | } | |
47 | ||
48 | impl QueryContext for QueryCtxt<'tcx> { | |
6a06907d XL |
49 | fn current_query_job(&self) -> Option<QueryJobId<Self::DepKind>> { |
50 | tls::with_related_context(**self, |icx| icx.query) | |
51 | } | |
52 | ||
53 | fn try_collect_active_jobs(&self) -> Option<QueryMap<Self::DepKind>> { | |
54 | self.queries.try_collect_active_jobs(**self) | |
55 | } | |
56 | ||
57 | fn try_load_from_on_disk_cache(&self, dep_node: &DepNode) { | |
58 | let cb = &super::QUERY_CALLBACKS[dep_node.kind as usize]; | |
59 | (cb.try_load_from_on_disk_cache)(*self, dep_node) | |
60 | } | |
61 | ||
62 | fn try_force_from_dep_node(&self, dep_node: &DepNode) -> bool { | |
6a06907d XL |
63 | debug!("try_force_from_dep_node({:?}) --- trying to force", dep_node); |
64 | ||
65 | // We must avoid ever having to call `force_from_dep_node()` for a | |
66 | // `DepNode::codegen_unit`: | |
67 | // Since we cannot reconstruct the query key of a `DepNode::codegen_unit`, we | |
68 | // would always end up having to evaluate the first caller of the | |
69 | // `codegen_unit` query that *is* reconstructible. This might very well be | |
70 | // the `compile_codegen_unit` query, thus re-codegenning the whole CGU just | |
71 | // to re-trigger calling the `codegen_unit` query with the right key. At | |
72 | // that point we would already have re-done all the work we are trying to | |
73 | // avoid doing in the first place. | |
74 | // The solution is simple: Just explicitly call the `codegen_unit` query for | |
75 | // each CGU, right after partitioning. This way `try_mark_green` will always | |
76 | // hit the cache instead of having to go through `force_from_dep_node`. | |
77 | // This assertion makes sure, we actually keep applying the solution above. | |
78 | debug_assert!( | |
79 | dep_node.kind != DepKind::codegen_unit, | |
80 | "calling force_from_dep_node() on DepKind::codegen_unit" | |
81 | ); | |
82 | ||
83 | let cb = &super::QUERY_CALLBACKS[dep_node.kind as usize]; | |
84 | (cb.force_from_dep_node)(*self, dep_node) | |
85 | } | |
86 | ||
87 | // Interactions with on_disk_cache | |
94222f64 | 88 | fn load_side_effects(&self, prev_dep_node_index: SerializedDepNodeIndex) -> QuerySideEffects { |
136023e0 XL |
89 | self.queries |
90 | .on_disk_cache | |
6a06907d | 91 | .as_ref() |
94222f64 | 92 | .map(|c| c.load_side_effects(**self, prev_dep_node_index)) |
6a06907d XL |
93 | .unwrap_or_default() |
94 | } | |
95 | ||
94222f64 | 96 | fn store_side_effects(&self, dep_node_index: DepNodeIndex, side_effects: QuerySideEffects) { |
136023e0 | 97 | if let Some(c) = self.queries.on_disk_cache.as_ref() { |
94222f64 | 98 | c.store_side_effects(dep_node_index, side_effects) |
6a06907d XL |
99 | } |
100 | } | |
101 | ||
94222f64 | 102 | fn store_side_effects_for_anon_node( |
6a06907d XL |
103 | &self, |
104 | dep_node_index: DepNodeIndex, | |
94222f64 | 105 | side_effects: QuerySideEffects, |
6a06907d | 106 | ) { |
136023e0 | 107 | if let Some(c) = self.queries.on_disk_cache.as_ref() { |
94222f64 | 108 | c.store_side_effects_for_anon_node(dep_node_index, side_effects) |
6a06907d XL |
109 | } |
110 | } | |
111 | ||
112 | /// Executes a job by changing the `ImplicitCtxt` to point to the | |
113 | /// new query job while it executes. It returns the diagnostics | |
114 | /// captured during execution and the actual result. | |
115 | #[inline(always)] | |
116 | fn start_query<R>( | |
117 | &self, | |
118 | token: QueryJobId<Self::DepKind>, | |
119 | diagnostics: Option<&Lock<ThinVec<Diagnostic>>>, | |
120 | compute: impl FnOnce() -> R, | |
121 | ) -> R { | |
122 | // The `TyCtxt` stored in TLS has the same global interner lifetime | |
123 | // as `self`, so we use `with_related_context` to relate the 'tcx lifetimes | |
124 | // when accessing the `ImplicitCtxt`. | |
125 | tls::with_related_context(**self, move |current_icx| { | |
126 | // Update the `ImplicitCtxt` to point to our new query job. | |
127 | let new_icx = ImplicitCtxt { | |
128 | tcx: **self, | |
129 | query: Some(token), | |
130 | diagnostics, | |
131 | layout_depth: current_icx.layout_depth, | |
132 | task_deps: current_icx.task_deps, | |
133 | }; | |
134 | ||
135 | // Use the `ImplicitCtxt` while we execute the query. | |
136 | tls::enter_context(&new_icx, |_| { | |
137 | rustc_data_structures::stack::ensure_sufficient_stack(compute) | |
138 | }) | |
139 | }) | |
140 | } | |
141 | } | |
142 | ||
143 | impl<'tcx> QueryCtxt<'tcx> { | |
136023e0 XL |
144 | #[inline] |
145 | pub fn from_tcx(tcx: TyCtxt<'tcx>) -> Self { | |
146 | let queries = tcx.queries.as_any(); | |
147 | let queries = unsafe { | |
148 | let queries = std::mem::transmute::<&dyn Any, &dyn Any>(queries); | |
149 | let queries = queries.downcast_ref().unwrap(); | |
150 | let queries = std::mem::transmute::<&Queries<'_>, &Queries<'_>>(queries); | |
151 | queries | |
152 | }; | |
153 | QueryCtxt { tcx, queries } | |
154 | } | |
155 | ||
156 | crate fn on_disk_cache(self) -> Option<&'tcx on_disk_cache::OnDiskCache<'tcx>> { | |
157 | self.queries.on_disk_cache.as_ref() | |
158 | } | |
159 | ||
160 | #[cfg(parallel_compiler)] | |
161 | pub unsafe fn deadlock(self, registry: &rustc_rayon_core::Registry) { | |
162 | rustc_query_system::query::deadlock(self, registry) | |
163 | } | |
164 | ||
6a06907d XL |
165 | pub(super) fn encode_query_results( |
166 | self, | |
167 | encoder: &mut on_disk_cache::CacheEncoder<'a, 'tcx, opaque::FileEncoder>, | |
94222f64 | 168 | query_result_index: &mut on_disk_cache::EncodedDepNodeIndex, |
6a06907d XL |
169 | ) -> opaque::FileEncodeResult { |
170 | macro_rules! encode_queries { | |
171 | ($($query:ident,)*) => { | |
172 | $( | |
173 | on_disk_cache::encode_query_results::<_, super::queries::$query<'_>>( | |
174 | self, | |
175 | encoder, | |
176 | query_result_index | |
177 | )?; | |
178 | )* | |
179 | } | |
180 | } | |
181 | ||
182 | rustc_cached_queries!(encode_queries!); | |
183 | ||
184 | Ok(()) | |
185 | } | |
136023e0 XL |
186 | |
187 | pub fn try_print_query_stack( | |
188 | self, | |
189 | query: Option<QueryJobId<DepKind>>, | |
190 | handler: &Handler, | |
191 | num_frames: Option<usize>, | |
192 | ) -> usize { | |
193 | rustc_query_system::query::print_query_stack(self, query, handler, num_frames) | |
194 | } | |
6a06907d XL |
195 | } |
196 | ||
197 | /// This struct stores metadata about each Query. | |
198 | /// | |
199 | /// Information is retrieved by indexing the `QUERIES` array using the integer value | |
200 | /// of the `DepKind`. Overall, this allows to implement `QueryContext` using this manual | |
201 | /// jump table instead of large matches. | |
202 | pub struct QueryStruct { | |
203 | /// The red/green evaluation system will try to mark a specific DepNode in the | |
204 | /// dependency graph as green by recursively trying to mark the dependencies of | |
205 | /// that `DepNode` as green. While doing so, it will sometimes encounter a `DepNode` | |
206 | /// where we don't know if it is red or green and we therefore actually have | |
207 | /// to recompute its value in order to find out. Since the only piece of | |
208 | /// information that we have at that point is the `DepNode` we are trying to | |
209 | /// re-evaluate, we need some way to re-run a query from just that. This is what | |
210 | /// `force_from_dep_node()` implements. | |
211 | /// | |
212 | /// In the general case, a `DepNode` consists of a `DepKind` and an opaque | |
213 | /// GUID/fingerprint that will uniquely identify the node. This GUID/fingerprint | |
214 | /// is usually constructed by computing a stable hash of the query-key that the | |
215 | /// `DepNode` corresponds to. Consequently, it is not in general possible to go | |
216 | /// back from hash to query-key (since hash functions are not reversible). For | |
217 | /// this reason `force_from_dep_node()` is expected to fail from time to time | |
218 | /// because we just cannot find out, from the `DepNode` alone, what the | |
219 | /// corresponding query-key is and therefore cannot re-run the query. | |
220 | /// | |
221 | /// The system deals with this case letting `try_mark_green` fail which forces | |
222 | /// the root query to be re-evaluated. | |
223 | /// | |
224 | /// Now, if `force_from_dep_node()` would always fail, it would be pretty useless. | |
225 | /// Fortunately, we can use some contextual information that will allow us to | |
226 | /// reconstruct query-keys for certain kinds of `DepNode`s. In particular, we | |
227 | /// enforce by construction that the GUID/fingerprint of certain `DepNode`s is a | |
228 | /// valid `DefPathHash`. Since we also always build a huge table that maps every | |
229 | /// `DefPathHash` in the current codebase to the corresponding `DefId`, we have | |
230 | /// everything we need to re-run the query. | |
231 | /// | |
232 | /// Take the `mir_promoted` query as an example. Like many other queries, it | |
233 | /// just has a single parameter: the `DefId` of the item it will compute the | |
234 | /// validated MIR for. Now, when we call `force_from_dep_node()` on a `DepNode` | |
235 | /// with kind `MirValidated`, we know that the GUID/fingerprint of the `DepNode` | |
236 | /// is actually a `DefPathHash`, and can therefore just look up the corresponding | |
237 | /// `DefId` in `tcx.def_path_hash_to_def_id`. | |
238 | /// | |
239 | /// When you implement a new query, it will likely have a corresponding new | |
240 | /// `DepKind`, and you'll have to support it here in `force_from_dep_node()`. As | |
241 | /// a rule of thumb, if your query takes a `DefId` or `LocalDefId` as sole parameter, | |
242 | /// then `force_from_dep_node()` should not fail for it. Otherwise, you can just | |
243 | /// add it to the "We don't have enough information to reconstruct..." group in | |
244 | /// the match below. | |
245 | pub(crate) force_from_dep_node: fn(tcx: QueryCtxt<'_>, dep_node: &DepNode) -> bool, | |
246 | ||
247 | /// Invoke a query to put the on-disk cached value in memory. | |
248 | pub(crate) try_load_from_on_disk_cache: fn(QueryCtxt<'_>, &DepNode), | |
249 | } | |
250 | ||
251 | macro_rules! handle_cycle_error { | |
252 | ([][$tcx: expr, $error:expr]) => {{ | |
253 | $error.emit(); | |
254 | Value::from_cycle_error($tcx) | |
255 | }}; | |
256 | ([fatal_cycle $($rest:tt)*][$tcx:expr, $error:expr]) => {{ | |
257 | $error.emit(); | |
258 | $tcx.sess.abort_if_errors(); | |
259 | unreachable!() | |
260 | }}; | |
261 | ([cycle_delay_bug $($rest:tt)*][$tcx:expr, $error:expr]) => {{ | |
262 | $error.delay_as_bug(); | |
263 | Value::from_cycle_error($tcx) | |
264 | }}; | |
265 | ([$other:ident $(($($other_args:tt)*))* $(, $($modifiers:tt)*)*][$($args:tt)*]) => { | |
266 | handle_cycle_error!([$($($modifiers)*)*][$($args)*]) | |
267 | }; | |
268 | } | |
269 | ||
270 | macro_rules! is_anon { | |
271 | ([]) => {{ | |
272 | false | |
273 | }}; | |
274 | ([anon $($rest:tt)*]) => {{ | |
275 | true | |
276 | }}; | |
277 | ([$other:ident $(($($other_args:tt)*))* $(, $($modifiers:tt)*)*]) => { | |
278 | is_anon!([$($($modifiers)*)*]) | |
279 | }; | |
280 | } | |
281 | ||
282 | macro_rules! is_eval_always { | |
283 | ([]) => {{ | |
284 | false | |
285 | }}; | |
286 | ([eval_always $($rest:tt)*]) => {{ | |
287 | true | |
288 | }}; | |
289 | ([$other:ident $(($($other_args:tt)*))* $(, $($modifiers:tt)*)*]) => { | |
290 | is_eval_always!([$($($modifiers)*)*]) | |
291 | }; | |
292 | } | |
293 | ||
294 | macro_rules! hash_result { | |
295 | ([][$hcx:expr, $result:expr]) => {{ | |
296 | dep_graph::hash_result($hcx, &$result) | |
297 | }}; | |
298 | ([no_hash $($rest:tt)*][$hcx:expr, $result:expr]) => {{ | |
299 | None | |
300 | }}; | |
301 | ([$other:ident $(($($other_args:tt)*))* $(, $($modifiers:tt)*)*][$($args:tt)*]) => { | |
302 | hash_result!([$($($modifiers)*)*][$($args)*]) | |
303 | }; | |
304 | } | |
305 | ||
306 | macro_rules! define_queries { | |
307 | (<$tcx:tt> | |
308 | $($(#[$attr:meta])* | |
309 | [$($modifiers:tt)*] fn $name:ident($($K:tt)*) -> $V:ty,)*) => { | |
310 | ||
311 | define_queries_struct! { | |
312 | tcx: $tcx, | |
313 | input: ($(([$($modifiers)*] [$($attr)*] [$name]))*) | |
314 | } | |
315 | ||
316 | mod make_query { | |
317 | use super::*; | |
318 | ||
319 | // Create an eponymous constructor for each query. | |
320 | $(#[allow(nonstandard_style)] $(#[$attr])* | |
321 | pub fn $name<$tcx>(tcx: QueryCtxt<$tcx>, key: query_keys::$name<$tcx>) -> QueryStackFrame { | |
322 | let kind = dep_graph::DepKind::$name; | |
323 | let name = stringify!($name); | |
324 | let description = ty::print::with_forced_impl_filename_line( | |
325 | // Force filename-line mode to avoid invoking `type_of` query. | |
326 | || queries::$name::describe(tcx, key) | |
327 | ); | |
328 | let description = if tcx.sess.verbose() { | |
329 | format!("{} [{}]", description, name) | |
330 | } else { | |
331 | description | |
332 | }; | |
333 | let span = if kind == dep_graph::DepKind::def_span { | |
334 | // The `def_span` query is used to calculate `default_span`, | |
335 | // so exit to avoid infinite recursion. | |
336 | None | |
337 | } else { | |
338 | Some(key.default_span(*tcx)) | |
339 | }; | |
94222f64 XL |
340 | let def_id = key.key_as_def_id(); |
341 | let def_kind = def_id | |
342 | .and_then(|def_id| def_id.as_local()) | |
343 | // Use `tcx.hir().opt_def_kind()` to reduce the chance of | |
344 | // accidentally triggering an infinite query loop. | |
345 | .and_then(|def_id| tcx.hir().opt_def_kind(def_id)) | |
346 | .map(|def_kind| $crate::util::def_kind_to_simple_def_kind(def_kind)); | |
6a06907d XL |
347 | let hash = || { |
348 | let mut hcx = tcx.create_stable_hashing_context(); | |
349 | let mut hasher = StableHasher::new(); | |
350 | std::mem::discriminant(&kind).hash_stable(&mut hcx, &mut hasher); | |
351 | key.hash_stable(&mut hcx, &mut hasher); | |
352 | hasher.finish::<u64>() | |
353 | }; | |
354 | ||
94222f64 | 355 | QueryStackFrame::new(name, description, span, def_kind, hash) |
6a06907d XL |
356 | })* |
357 | } | |
358 | ||
359 | #[allow(nonstandard_style)] | |
360 | pub mod queries { | |
361 | use std::marker::PhantomData; | |
362 | ||
363 | $(pub struct $name<$tcx> { | |
364 | data: PhantomData<&$tcx ()> | |
365 | })* | |
366 | } | |
367 | ||
368 | $(impl<$tcx> QueryConfig for queries::$name<$tcx> { | |
369 | type Key = query_keys::$name<$tcx>; | |
370 | type Value = query_values::$name<$tcx>; | |
371 | type Stored = query_stored::$name<$tcx>; | |
372 | const NAME: &'static str = stringify!($name); | |
373 | } | |
374 | ||
375 | impl<$tcx> QueryAccessors<QueryCtxt<$tcx>> for queries::$name<$tcx> { | |
376 | const ANON: bool = is_anon!([$($modifiers)*]); | |
377 | const EVAL_ALWAYS: bool = is_eval_always!([$($modifiers)*]); | |
378 | const DEP_KIND: dep_graph::DepKind = dep_graph::DepKind::$name; | |
379 | ||
380 | type Cache = query_storage::$name<$tcx>; | |
381 | ||
382 | #[inline(always)] | |
383 | fn query_state<'a>(tcx: QueryCtxt<$tcx>) -> &'a QueryState<crate::dep_graph::DepKind, Self::Key> | |
384 | where QueryCtxt<$tcx>: 'a | |
385 | { | |
386 | &tcx.queries.$name | |
387 | } | |
388 | ||
389 | #[inline(always)] | |
390 | fn query_cache<'a>(tcx: QueryCtxt<$tcx>) -> &'a QueryCacheStore<Self::Cache> | |
391 | where 'tcx:'a | |
392 | { | |
393 | &tcx.query_caches.$name | |
394 | } | |
395 | ||
396 | #[inline] | |
136023e0 XL |
397 | fn compute_fn(tcx: QueryCtxt<'tcx>, key: &Self::Key) -> |
398 | fn(TyCtxt<'tcx>, Self::Key) -> Self::Value | |
399 | { | |
400 | if key.query_crate_is_local() { | |
cdc7bbd5 XL |
401 | tcx.queries.local_providers.$name |
402 | } else { | |
403 | tcx.queries.extern_providers.$name | |
136023e0 | 404 | } |
6a06907d XL |
405 | } |
406 | ||
407 | fn hash_result( | |
408 | _hcx: &mut StableHashingContext<'_>, | |
409 | _result: &Self::Value | |
410 | ) -> Option<Fingerprint> { | |
411 | hash_result!([$($modifiers)*][_hcx, _result]) | |
412 | } | |
413 | ||
414 | fn handle_cycle_error( | |
415 | tcx: QueryCtxt<'tcx>, | |
416 | mut error: DiagnosticBuilder<'_>, | |
417 | ) -> Self::Value { | |
418 | handle_cycle_error!([$($modifiers)*][tcx, error]) | |
419 | } | |
420 | })* | |
421 | ||
422 | #[allow(non_upper_case_globals)] | |
423 | pub mod query_callbacks { | |
424 | use super::*; | |
425 | use rustc_middle::dep_graph::DepNode; | |
426 | use rustc_middle::ty::query::query_keys; | |
427 | use rustc_query_system::dep_graph::DepNodeParams; | |
428 | use rustc_query_system::query::{force_query, QueryDescription}; | |
429 | ||
430 | // We use this for most things when incr. comp. is turned off. | |
431 | pub const Null: QueryStruct = QueryStruct { | |
432 | force_from_dep_node: |_, dep_node| bug!("force_from_dep_node: encountered {:?}", dep_node), | |
433 | try_load_from_on_disk_cache: |_, _| {}, | |
434 | }; | |
435 | ||
436 | pub const TraitSelect: QueryStruct = QueryStruct { | |
437 | force_from_dep_node: |_, _| false, | |
438 | try_load_from_on_disk_cache: |_, _| {}, | |
439 | }; | |
440 | ||
441 | pub const CompileCodegenUnit: QueryStruct = QueryStruct { | |
442 | force_from_dep_node: |_, _| false, | |
443 | try_load_from_on_disk_cache: |_, _| {}, | |
444 | }; | |
445 | ||
cdc7bbd5 XL |
446 | pub const CompileMonoItem: QueryStruct = QueryStruct { |
447 | force_from_dep_node: |_, _| false, | |
448 | try_load_from_on_disk_cache: |_, _| {}, | |
449 | }; | |
450 | ||
6a06907d XL |
451 | $(pub const $name: QueryStruct = { |
452 | const is_anon: bool = is_anon!([$($modifiers)*]); | |
453 | ||
454 | #[inline(always)] | |
455 | fn can_reconstruct_query_key() -> bool { | |
456 | <query_keys::$name<'_> as DepNodeParams<TyCtxt<'_>>> | |
457 | ::can_reconstruct_query_key() | |
458 | } | |
459 | ||
460 | fn recover<'tcx>(tcx: TyCtxt<'tcx>, dep_node: &DepNode) -> Option<query_keys::$name<'tcx>> { | |
461 | <query_keys::$name<'_> as DepNodeParams<TyCtxt<'_>>>::recover(tcx, dep_node) | |
462 | } | |
463 | ||
464 | fn force_from_dep_node(tcx: QueryCtxt<'_>, dep_node: &DepNode) -> bool { | |
17df50a5 | 465 | force_query::<queries::$name<'_>, _>(tcx, dep_node) |
6a06907d XL |
466 | } |
467 | ||
468 | fn try_load_from_on_disk_cache(tcx: QueryCtxt<'_>, dep_node: &DepNode) { | |
469 | if is_anon { | |
470 | return | |
471 | } | |
472 | ||
473 | if !can_reconstruct_query_key() { | |
474 | return | |
475 | } | |
476 | ||
cdc7bbd5 | 477 | debug_assert!(tcx.dep_graph.is_green(dep_node)); |
6a06907d XL |
478 | |
479 | let key = recover(*tcx, dep_node).unwrap_or_else(|| panic!("Failed to recover key for {:?} with hash {}", dep_node, dep_node.hash)); | |
480 | if queries::$name::cache_on_disk(tcx, &key, None) { | |
481 | let _ = tcx.$name(key); | |
482 | } | |
483 | } | |
484 | ||
485 | QueryStruct { | |
486 | force_from_dep_node, | |
487 | try_load_from_on_disk_cache, | |
488 | } | |
489 | };)* | |
490 | } | |
491 | ||
492 | static QUERY_CALLBACKS: &[QueryStruct] = &make_dep_kind_array!(query_callbacks); | |
493 | } | |
494 | } | |
495 | ||
496 | // FIXME(eddyb) this macro (and others?) use `$tcx` and `'tcx` interchangeably. | |
497 | // We should either not take `$tcx` at all and use `'tcx` everywhere, or use | |
498 | // `$tcx` everywhere (even if that isn't necessary due to lack of hygiene). | |
499 | macro_rules! define_queries_struct { | |
500 | (tcx: $tcx:tt, | |
501 | input: ($(([$($modifiers:tt)*] [$($attr:tt)*] [$name:ident]))*)) => { | |
502 | pub struct Queries<$tcx> { | |
cdc7bbd5 XL |
503 | local_providers: Box<Providers>, |
504 | extern_providers: Box<Providers>, | |
6a06907d | 505 | |
136023e0 XL |
506 | pub on_disk_cache: Option<OnDiskCache<$tcx>>, |
507 | ||
6a06907d XL |
508 | $($(#[$attr])* $name: QueryState< |
509 | crate::dep_graph::DepKind, | |
510 | query_keys::$name<$tcx>, | |
511 | >,)* | |
512 | } | |
513 | ||
514 | impl<$tcx> Queries<$tcx> { | |
515 | pub fn new( | |
cdc7bbd5 XL |
516 | local_providers: Providers, |
517 | extern_providers: Providers, | |
136023e0 | 518 | on_disk_cache: Option<OnDiskCache<$tcx>>, |
6a06907d XL |
519 | ) -> Self { |
520 | Queries { | |
cdc7bbd5 XL |
521 | local_providers: Box::new(local_providers), |
522 | extern_providers: Box::new(extern_providers), | |
136023e0 | 523 | on_disk_cache, |
6a06907d XL |
524 | $($name: Default::default()),* |
525 | } | |
526 | } | |
527 | ||
528 | pub(crate) fn try_collect_active_jobs( | |
529 | &$tcx self, | |
530 | tcx: TyCtxt<$tcx>, | |
531 | ) -> Option<QueryMap<crate::dep_graph::DepKind>> { | |
532 | let tcx = QueryCtxt { tcx, queries: self }; | |
533 | let mut jobs = QueryMap::default(); | |
534 | ||
535 | $( | |
536 | self.$name.try_collect_active_jobs( | |
537 | tcx, | |
538 | dep_graph::DepKind::$name, | |
539 | make_query::$name, | |
540 | &mut jobs, | |
541 | )?; | |
542 | )* | |
543 | ||
544 | Some(jobs) | |
545 | } | |
546 | } | |
547 | ||
548 | impl QueryEngine<'tcx> for Queries<'tcx> { | |
136023e0 XL |
549 | fn as_any(&'tcx self) -> &'tcx dyn std::any::Any { |
550 | let this = unsafe { std::mem::transmute::<&Queries<'_>, &Queries<'_>>(self) }; | |
551 | this as _ | |
6a06907d XL |
552 | } |
553 | ||
554 | fn try_mark_green(&'tcx self, tcx: TyCtxt<'tcx>, dep_node: &dep_graph::DepNode) -> bool { | |
555 | let qcx = QueryCtxt { tcx, queries: self }; | |
556 | tcx.dep_graph.try_mark_green(qcx, dep_node).is_some() | |
557 | } | |
558 | ||
6a06907d XL |
559 | $($(#[$attr])* |
560 | #[inline(always)] | |
561 | fn $name( | |
562 | &'tcx self, | |
563 | tcx: TyCtxt<$tcx>, | |
564 | span: Span, | |
565 | key: query_keys::$name<$tcx>, | |
566 | lookup: QueryLookup, | |
567 | mode: QueryMode, | |
568 | ) -> Option<query_stored::$name<$tcx>> { | |
569 | let qcx = QueryCtxt { tcx, queries: self }; | |
570 | get_query::<queries::$name<$tcx>, _>(qcx, span, key, lookup, mode) | |
571 | })* | |
572 | } | |
573 | }; | |
574 | } | |
575 | ||
576 | fn describe_as_module(def_id: LocalDefId, tcx: TyCtxt<'_>) -> String { | |
577 | if def_id.is_top_level_module() { | |
578 | "top-level module".to_string() | |
579 | } else { | |
580 | format!("module `{}`", tcx.def_path_str(def_id.to_def_id())) | |
581 | } | |
582 | } | |
583 | ||
584 | rustc_query_description! {} |