1 use proc_macro::TokenStream;
3 Token, Ident, Type, Attribute, ReturnType, Expr, Block, Error,
4 braced, parenthesized, parse_macro_input,
6 use syn::spanned::Spanned;
7 use syn::parse::{Result, Parse, ParseStream};
8 use syn::punctuated::Punctuated;
11 use itertools::Itertools;
13 #[allow(non_camel_case_types)]
15 syn::custom_keyword!(query);
18 /// Ident or a wildcard `_`.
19 struct IdentOrWild(Ident);
21 impl Parse for IdentOrWild {
22 fn parse(input: ParseStream<'_>) -> Result<Self> {
23 Ok(if input.peek(Token![_]) {
24 let underscore = input.parse::<Token![_]>()?;
25 IdentOrWild(Ident::new("_", underscore.span()))
27 IdentOrWild(input.parse()?)
32 /// A modifier for a query
34 /// The description of the query.
35 Desc(Option<Ident>, Punctuated<Expr, Token![,]>),
37 /// Cache the query to disk if the `Expr` returns true.
38 Cache(Option<Ident>, Expr),
40 /// Custom code to load the query from disk.
41 LoadCached(Ident, Ident, Block),
43 /// A cycle error for this query aborting the compilation with a fatal error.
46 /// A cycle error results in a delay_bug call
49 /// Don't hash the result, instead just mark a query red if it runs
52 /// Don't force the query
55 /// Generate a dep node based on the dependencies of the query
58 /// Always evaluate the query, ignoring its depdendencies
62 impl Parse for QueryModifier {
63 fn parse(input: ParseStream<'_>) -> Result<Self> {
64 let modifier: Ident = input.parse()?;
65 if modifier == "desc" {
66 // Parse a description modifier like:
67 // `desc { |tcx| "foo {}", tcx.item_path(key) }`
69 braced!(attr_content in input);
70 let tcx = if attr_content.peek(Token![|]) {
71 attr_content.parse::<Token![|]>()?;
72 let tcx = attr_content.parse()?;
73 attr_content.parse::<Token![|]>()?;
78 let desc = attr_content.parse_terminated(Expr::parse)?;
79 Ok(QueryModifier::Desc(tcx, desc))
80 } else if modifier == "cache" {
81 // Parse a cache modifier like:
82 // `cache { |tcx| key.is_local() }`
84 braced!(attr_content in input);
85 let tcx = if attr_content.peek(Token![|]) {
86 attr_content.parse::<Token![|]>()?;
87 let tcx = attr_content.parse()?;
88 attr_content.parse::<Token![|]>()?;
93 let expr = attr_content.parse()?;
94 Ok(QueryModifier::Cache(tcx, expr))
95 } else if modifier == "load_cached" {
96 // Parse a load_cached modifier like:
97 // `load_cached(tcx, id) { tcx.queries.on_disk_cache.try_load_query_result(tcx, id) }`
99 parenthesized!(args in input);
100 let tcx = args.parse()?;
101 args.parse::<Token![,]>()?;
102 let id = args.parse()?;
103 let block = input.parse()?;
104 Ok(QueryModifier::LoadCached(tcx, id, block))
105 } else if modifier == "fatal_cycle" {
106 Ok(QueryModifier::FatalCycle)
107 } else if modifier == "cycle_delay_bug" {
108 Ok(QueryModifier::CycleDelayBug)
109 } else if modifier == "no_hash" {
110 Ok(QueryModifier::NoHash)
111 } else if modifier == "no_force" {
112 Ok(QueryModifier::NoForce)
113 } else if modifier == "anon" {
114 Ok(QueryModifier::Anon)
115 } else if modifier == "eval_always" {
116 Ok(QueryModifier::EvalAlways)
118 Err(Error::new(modifier.span(), "unknown query modifier"))
123 /// Ensures only doc comment attributes are used
124 fn check_attributes(attrs: Vec<Attribute>) -> Result<()> {
126 if !attr.path.is_ident("doc") {
127 return Err(Error::new(attr.span(), "attributes not supported on queries"));
133 /// A compiler query. `query ... { ... }`
135 modifiers: List<QueryModifier>,
142 impl Parse for Query {
143 fn parse(input: ParseStream<'_>) -> Result<Self> {
144 check_attributes(input.call(Attribute::parse_outer)?)?;
146 // Parse the query declaration. Like `query type_of(key: DefId) -> Ty<'tcx>`
147 input.parse::<kw::query>()?;
148 let name: Ident = input.parse()?;
150 parenthesized!(arg_content in input);
151 let key = arg_content.parse()?;
152 arg_content.parse::<Token![:]>()?;
153 let arg = arg_content.parse()?;
154 let result = input.parse()?;
156 // Parse the query modifiers
158 braced!(content in input);
159 let modifiers = content.parse()?;
171 /// A type used to greedily parse another type until the input is empty.
172 struct List<T>(Vec<T>);
174 impl<T: Parse> Parse for List<T> {
175 fn parse(input: ParseStream<'_>) -> Result<Self> {
176 let mut list = Vec::new();
177 while !input.is_empty() {
178 list.push(input.parse()?);
184 /// A named group containing queries.
187 queries: List<Query>,
190 impl Parse for Group {
191 fn parse(input: ParseStream<'_>) -> Result<Self> {
192 let name: Ident = input.parse()?;
194 braced!(content in input);
197 queries: content.parse()?,
202 struct QueryModifiers {
203 /// The description of the query.
204 desc: Option<(Option<Ident>, Punctuated<Expr, Token![,]>)>,
206 /// Cache the query to disk if the `Expr` returns true.
207 cache: Option<(Option<Ident>, Expr)>,
209 /// Custom code to load the query from disk.
210 load_cached: Option<(Ident, Ident, Block)>,
212 /// A cycle error for this query aborting the compilation with a fatal error.
215 /// A cycle error results in a delay_bug call
216 cycle_delay_bug: bool,
218 /// Don't hash the result, instead just mark a query red if it runs
221 /// Don't force the query
224 /// Generate a dep node based on the dependencies of the query
227 // Always evaluate the query, ignoring its depdendencies
231 /// Process query modifiers into a struct, erroring on duplicates
232 fn process_modifiers(query: &mut Query) -> QueryModifiers {
233 let mut load_cached = None;
234 let mut cache = None;
236 let mut fatal_cycle = false;
237 let mut cycle_delay_bug = false;
238 let mut no_hash = false;
239 let mut no_force = false;
240 let mut anon = false;
241 let mut eval_always = false;
242 for modifier in query.modifiers.0.drain(..) {
244 QueryModifier::LoadCached(tcx, id, block) => {
245 if load_cached.is_some() {
246 panic!("duplicate modifier `load_cached` for query `{}`", query.name);
248 load_cached = Some((tcx, id, block));
250 QueryModifier::Cache(tcx, expr) => {
252 panic!("duplicate modifier `cache` for query `{}`", query.name);
254 cache = Some((tcx, expr));
256 QueryModifier::Desc(tcx, list) => {
258 panic!("duplicate modifier `desc` for query `{}`", query.name);
260 desc = Some((tcx, list));
262 QueryModifier::FatalCycle => {
264 panic!("duplicate modifier `fatal_cycle` for query `{}`", query.name);
268 QueryModifier::CycleDelayBug => {
270 panic!("duplicate modifier `cycle_delay_bug` for query `{}`", query.name);
272 cycle_delay_bug = true;
274 QueryModifier::NoHash => {
276 panic!("duplicate modifier `no_hash` for query `{}`", query.name);
280 QueryModifier::NoForce => {
282 panic!("duplicate modifier `no_force` for query `{}`", query.name);
286 QueryModifier::Anon => {
288 panic!("duplicate modifier `anon` for query `{}`", query.name);
292 QueryModifier::EvalAlways => {
294 panic!("duplicate modifier `eval_always` for query `{}`", query.name);
313 /// Add the impl of QueryDescription for the query to `impls` if one is requested
314 fn add_query_description_impl(
316 modifiers: QueryModifiers,
317 impls: &mut proc_macro2::TokenStream,
319 let name = &query.name;
320 let arg = &query.arg;
321 let key = &query.key.0;
323 // Find out if we should cache the query on disk
324 let cache = modifiers.cache.as_ref().map(|(tcx, expr)| {
325 let try_load_from_disk = if let Some((tcx, id, block)) = modifiers.load_cached.as_ref() {
326 // Use custom code to load the query from disk
329 fn try_load_from_disk(
330 #tcx: TyCtxt<'tcx, 'tcx>,
331 #id: SerializedDepNodeIndex
332 ) -> Option<Self::Value> {
337 // Use the default code to load the query from disk
340 fn try_load_from_disk(
341 tcx: TyCtxt<'tcx, 'tcx>,
342 id: SerializedDepNodeIndex
343 ) -> Option<Self::Value> {
344 tcx.queries.on_disk_cache.try_load_query_result(tcx, id)
349 let tcx = tcx.as_ref().map(|t| quote! { #t }).unwrap_or(quote! { _ });
352 #[allow(unused_variables)]
353 fn cache_on_disk(#tcx: TyCtxt<'tcx, 'tcx>, #key: Self::Key) -> bool {
361 if cache.is_none() && modifiers.load_cached.is_some() {
362 panic!("load_cached modifier on query `{}` without a cache modifier", name);
365 let desc = modifiers.desc.as_ref().map(|(tcx, desc)| {
366 let tcx = tcx.as_ref().map(|t| quote! { #t }).unwrap_or(quote! { _ });
368 #[allow(unused_variables)]
370 #tcx: TyCtxt<'_, '_>,
372 ) -> Cow<'static, str> {
373 format!(#desc).into()
378 if desc.is_some() || cache.is_some() {
379 let cache = cache.unwrap_or(quote! {});
380 let desc = desc.unwrap_or(quote! {});
382 impls.extend(quote! {
383 impl<'tcx> QueryDescription<'tcx> for queries::#name<'tcx> {
391 pub fn rustc_queries(input: TokenStream) -> TokenStream {
392 let groups = parse_macro_input!(input as List<Group>);
394 let mut query_stream = quote! {};
395 let mut query_description_stream = quote! {};
396 let mut dep_node_def_stream = quote! {};
397 let mut dep_node_force_stream = quote! {};
398 let mut no_force_queries = Vec::new();
400 for group in groups.0 {
401 let mut group_stream = quote! {};
402 for mut query in group.queries.0 {
403 let modifiers = process_modifiers(&mut query);
404 let name = &query.name;
405 let arg = &query.arg;
406 let result_full = &query.result;
407 let result = match query.result {
408 ReturnType::Default => quote! { -> () },
409 _ => quote! { #result_full },
412 let mut attributes = Vec::new();
414 // Pass on the fatal_cycle modifier
415 if modifiers.fatal_cycle {
416 attributes.push(quote! { fatal_cycle });
418 // Pass on the cycle_delay_bug modifier
419 if modifiers.cycle_delay_bug {
420 attributes.push(quote! { cycle_delay_bug });
422 // Pass on the no_hash modifier
423 if modifiers.no_hash {
424 attributes.push(quote! { no_hash });
427 let mut attribute_stream = quote! {};
429 for e in attributes.into_iter().intersperse(quote! {,}) {
430 attribute_stream.extend(e);
433 // Add the query to the group
434 group_stream.extend(quote! {
435 [#attribute_stream] fn #name: #name(#arg) #result,
438 let mut attributes = Vec::new();
440 // Pass on the anon modifier
442 attributes.push(quote! { anon });
444 // Pass on the eval_always modifier
445 if modifiers.eval_always {
446 attributes.push(quote! { eval_always });
449 let mut attribute_stream = quote! {};
450 for e in attributes.into_iter().intersperse(quote! {,}) {
451 attribute_stream.extend(e);
453 // Create a dep node for the query
454 dep_node_def_stream.extend(quote! {
455 [#attribute_stream] #name(#arg),
458 if modifiers.no_force {
459 no_force_queries.push(name.clone());
461 // Add a match arm to force the query given the dep node
462 dep_node_force_stream.extend(quote! {
464 if let Some(key) = RecoverKey::recover($tcx, $dep_node) {
465 force_ex!($tcx, #name, key);
473 add_query_description_impl(&query, modifiers, &mut query_description_stream);
475 let name = &group.name;
476 query_stream.extend(quote! {
477 #name { #group_stream },
481 // Add an arm for the no force queries to panic when trying to force them
482 for query in no_force_queries {
483 dep_node_force_stream.extend(quote! {
487 dep_node_force_stream.extend(quote! {
489 bug!("Cannot force dep node: {:?}", $dep_node)
493 TokenStream::from(quote! {
494 macro_rules! rustc_query_append {
495 ([$($macro:tt)*][$($other:tt)*]) => {
504 macro_rules! rustc_dep_node_append {
505 ([$($macro:tt)*][$($other:tt)*]) => {
513 macro_rules! rustc_dep_node_force {
514 ([$dep_node:expr, $tcx:expr] $($other:tt)*) => {
515 match $dep_node.kind {
518 #dep_node_force_stream
522 #query_description_stream