1 use proc_macro::TokenStream;
2 use quote::{quote, quote_spanned};
3 use syn::parse::{Parse, ParseStream, Result};
4 use syn::punctuated::Punctuated;
5 use syn::spanned::Spanned;
7 braced, parenthesized, parse_macro_input, parse_quote, token, AttrStyle, Attribute, Block,
8 Error, Expr, Ident, Pat, ReturnType, Token, Type,
12 syn::custom_keyword!(query);
15 /// Ensures only doc comment attributes are used
16 fn check_attributes(attrs: Vec<Attribute>) -> Result<Vec<Attribute>> {
17 let inner = |attr: Attribute| {
18 if !attr.path.is_ident("doc") {
19 Err(Error::new(attr.span(), "attributes not supported on queries"))
20 } else if attr.style != AttrStyle::Outer {
23 "attributes must be outer attributes (`///`), not inner attributes",
29 attrs.into_iter().map(inner).collect()
32 /// A compiler query. `query ... { ... }`
34 doc_comments: Vec<Attribute>,
35 modifiers: QueryModifiers,
42 impl Parse for Query {
43 fn parse(input: ParseStream<'_>) -> Result<Self> {
44 let mut doc_comments = check_attributes(input.call(Attribute::parse_outer)?)?;
46 // Parse the query declaration. Like `query type_of(key: DefId) -> Ty<'tcx>`
47 input.parse::<kw::query>()?;
48 let name: Ident = input.parse()?;
50 parenthesized!(arg_content in input);
51 let key = arg_content.parse()?;
52 arg_content.parse::<Token![:]>()?;
53 let arg = arg_content.parse()?;
54 let result = input.parse()?;
56 // Parse the query modifiers
58 braced!(content in input);
59 let modifiers = parse_query_modifiers(&content)?;
61 // If there are no doc-comments, give at least some idea of what
62 // it does by showing the query description.
63 if doc_comments.is_empty() {
64 doc_comments.push(doc_comment_from_desc(&modifiers.desc.1)?);
67 Ok(Query { doc_comments, modifiers, name, key, arg, result })
71 /// A type used to greedily parse another type until the input is empty.
72 struct List<T>(Vec<T>);
74 impl<T: Parse> Parse for List<T> {
75 fn parse(input: ParseStream<'_>) -> Result<Self> {
76 let mut list = Vec::new();
77 while !input.is_empty() {
78 list.push(input.parse()?);
84 struct QueryModifiers {
85 /// The description of the query.
86 desc: (Option<Ident>, Punctuated<Expr, Token![,]>),
88 /// Use this type for the in-memory cache.
89 arena_cache: Option<Ident>,
91 /// Cache the query to disk if the `Block` returns true.
92 cache: Option<(Option<Pat>, Block)>,
94 /// A cycle error for this query aborting the compilation with a fatal error.
95 fatal_cycle: Option<Ident>,
97 /// A cycle error results in a delay_bug call
98 cycle_delay_bug: Option<Ident>,
100 /// Don't hash the result, instead just mark a query red if it runs
101 no_hash: Option<Ident>,
103 /// Generate a dep node based on the dependencies of the query
106 /// Always evaluate the query, ignoring its dependencies
107 eval_always: Option<Ident>,
109 /// Whether the query has a call depth limit
110 depth_limit: Option<Ident>,
112 /// Use a separate query provider for local and extern crates
113 separate_provide_extern: Option<Ident>,
115 /// Always remap the ParamEnv's constness before hashing.
116 remap_env_constness: Option<Ident>,
119 fn parse_query_modifiers(input: ParseStream<'_>) -> Result<QueryModifiers> {
120 let mut arena_cache = None;
121 let mut cache = None;
123 let mut fatal_cycle = None;
124 let mut cycle_delay_bug = None;
125 let mut no_hash = None;
127 let mut eval_always = None;
128 let mut depth_limit = None;
129 let mut separate_provide_extern = None;
130 let mut remap_env_constness = None;
132 while !input.is_empty() {
133 let modifier: Ident = input.parse()?;
135 macro_rules! try_insert {
136 ($name:ident = $expr:expr) => {
138 return Err(Error::new(modifier.span(), "duplicate modifier"));
144 if modifier == "desc" {
145 // Parse a description modifier like:
146 // `desc { |tcx| "foo {}", tcx.item_path(key) }`
148 braced!(attr_content in input);
149 let tcx = if attr_content.peek(Token![|]) {
150 attr_content.parse::<Token![|]>()?;
151 let tcx = attr_content.parse()?;
152 attr_content.parse::<Token![|]>()?;
157 let list = attr_content.parse_terminated(Expr::parse)?;
158 try_insert!(desc = (tcx, list));
159 } else if modifier == "cache_on_disk_if" {
160 // Parse a cache modifier like:
161 // `cache(tcx) { |tcx| key.is_local() }`
162 let args = if input.peek(token::Paren) {
164 parenthesized!(args in input);
165 let tcx = args.parse()?;
170 let block = input.parse()?;
171 try_insert!(cache = (args, block));
172 } else if modifier == "arena_cache" {
173 try_insert!(arena_cache = modifier);
174 } else if modifier == "fatal_cycle" {
175 try_insert!(fatal_cycle = modifier);
176 } else if modifier == "cycle_delay_bug" {
177 try_insert!(cycle_delay_bug = modifier);
178 } else if modifier == "no_hash" {
179 try_insert!(no_hash = modifier);
180 } else if modifier == "anon" {
181 try_insert!(anon = modifier);
182 } else if modifier == "eval_always" {
183 try_insert!(eval_always = modifier);
184 } else if modifier == "depth_limit" {
185 try_insert!(depth_limit = modifier);
186 } else if modifier == "separate_provide_extern" {
187 try_insert!(separate_provide_extern = modifier);
188 } else if modifier == "remap_env_constness" {
189 try_insert!(remap_env_constness = modifier);
191 return Err(Error::new(modifier.span(), "unknown query modifier"));
194 let Some(desc) = desc else {
195 return Err(input.error("no description provided"));
207 separate_provide_extern,
212 fn doc_comment_from_desc(list: &Punctuated<Expr, token::Comma>) -> Result<Attribute> {
214 let mut iter = list.iter();
215 let format_str: String = match iter.next() {
216 Some(&Expr::Lit(ExprLit { lit: Lit::Str(ref lit_str), .. })) => {
217 lit_str.value().replace("`{}`", "{}") // We add them later anyways for consistency
219 _ => return Err(Error::new(list.span(), "Expected a string literal")),
221 let mut fmt_fragments = format_str.split("{}");
222 let mut doc_string = fmt_fragments.next().unwrap().to_string();
223 iter.map(::quote::ToTokens::to_token_stream).zip(fmt_fragments).for_each(
224 |(tts, next_fmt_fragment)| {
225 use ::core::fmt::Write;
229 tts.to_string().replace(" . ", "."),
235 let doc_string = format!("[query description - consider adding a doc-comment!] {}", doc_string);
236 Ok(parse_quote! { #[doc = #doc_string] })
239 /// Add the impl of QueryDescription for the query to `impls` if one is requested
240 fn add_query_description_impl(query: &Query, impls: &mut proc_macro2::TokenStream) {
241 let name = &query.name;
242 let key = &query.key;
243 let modifiers = &query.modifiers;
245 // Find out if we should cache the query on disk
246 let cache = if let Some((args, expr)) = modifiers.cache.as_ref() {
247 let tcx = args.as_ref().map(|t| quote! { #t }).unwrap_or_else(|| quote! { _ });
248 // expr is a `Block`, meaning that `{ #expr }` gets expanded
249 // to `{ { stmts... } }`, which triggers the `unused_braces` lint.
251 #[allow(unused_variables, unused_braces)]
253 fn cache_on_disk(#tcx: TyCtxt<'tcx>, #key: &Self::Key) -> bool {
260 fn cache_on_disk(_: TyCtxt<'tcx>, _: &Self::Key) -> bool {
266 let (tcx, desc) = &modifiers.desc;
267 let tcx = tcx.as_ref().map_or_else(|| quote! { _ }, |t| quote! { #t });
270 #[allow(unused_variables)]
271 fn describe(tcx: QueryCtxt<'tcx>, key: Self::Key) -> String {
272 let (#tcx, #key) = (*tcx, key);
273 ::rustc_middle::ty::print::with_no_trimmed_paths!(
279 impls.extend(quote! {
287 pub fn rustc_queries(input: TokenStream) -> TokenStream {
288 let queries = parse_macro_input!(input as List<Query>);
290 let mut query_stream = quote! {};
291 let mut query_description_stream = quote! {};
292 let mut cached_queries = quote! {};
294 for query in queries.0 {
295 let Query { name, arg, modifiers, .. } = &query;
296 let result_full = &query.result;
297 let result = match query.result {
298 ReturnType::Default => quote! { -> () },
299 _ => quote! { #result_full },
302 if modifiers.cache.is_some() {
303 cached_queries.extend(quote! {
308 let mut attributes = Vec::new();
310 macro_rules! passthrough {
311 ( $( $modifier:ident ),+ $(,)? ) => {
312 $( if let Some($modifier) = &modifiers.$modifier {
313 attributes.push(quote! { (#$modifier) });
326 separate_provide_extern,
330 if modifiers.cache.is_some() {
331 attributes.push(quote! { (cache) });
333 // Pass on the cache modifier
334 if modifiers.cache.is_some() {
335 attributes.push(quote! { (cache) });
338 // This uses the span of the query definition for the commas,
339 // which can be important if we later encounter any ambiguity
340 // errors with any of the numerous macro_rules! macros that
341 // we use. Using the call-site span would result in a span pointing
342 // at the entire `rustc_queries!` invocation, which wouldn't
344 let span = name.span();
345 let attribute_stream = quote_spanned! {span=> #(#attributes),*};
346 let doc_comments = &query.doc_comments;
347 // Add the query to the group
348 query_stream.extend(quote! {
350 [#attribute_stream] fn #name(#arg) #result,
353 add_query_description_impl(&query, &mut query_description_stream);
356 TokenStream::from(quote! {
358 macro_rules! rustc_query_append {
359 ($macro:ident! $( [$($other:tt)*] )?) => {
368 macro_rules! rustc_query_description {
369 #query_description_stream