]> git.proxmox.com Git - rustc.git/blob - compiler/rustc_macros/src/query.rs
New upstream version 1.50.0+dfsg1
[rustc.git] / compiler / rustc_macros / src / query.rs
1 use proc_macro::TokenStream;
2 use proc_macro2::{Delimiter, TokenTree};
3 use quote::quote;
4 use syn::parse::{Parse, ParseStream, Result};
5 use syn::punctuated::Punctuated;
6 use syn::spanned::Spanned;
7 use syn::{
8 braced, parenthesized, parse_macro_input, AttrStyle, Attribute, Block, Error, Expr, Ident,
9 ReturnType, Token, Type,
10 };
11
12 mod kw {
13 syn::custom_keyword!(query);
14 }
15
16 /// Ident or a wildcard `_`.
17 struct IdentOrWild(Ident);
18
19 impl Parse for IdentOrWild {
20 fn parse(input: ParseStream<'_>) -> Result<Self> {
21 Ok(if input.peek(Token![_]) {
22 let underscore = input.parse::<Token![_]>()?;
23 IdentOrWild(Ident::new("_", underscore.span()))
24 } else {
25 IdentOrWild(input.parse()?)
26 })
27 }
28 }
29
30 /// A modifier for a query
31 enum QueryModifier {
32 /// The description of the query.
33 Desc(Option<Ident>, Punctuated<Expr, Token![,]>),
34
35 /// Use this type for the in-memory cache.
36 Storage(Type),
37
38 /// Cache the query to disk if the `Expr` returns true.
39 Cache(Option<(IdentOrWild, IdentOrWild)>, Block),
40
41 /// Custom code to load the query from disk.
42 LoadCached(Ident, Ident, Block),
43
44 /// A cycle error for this query aborting the compilation with a fatal error.
45 FatalCycle,
46
47 /// A cycle error results in a delay_bug call
48 CycleDelayBug,
49
50 /// Don't hash the result, instead just mark a query red if it runs
51 NoHash,
52
53 /// Generate a dep node based on the dependencies of the query
54 Anon,
55
56 /// Always evaluate the query, ignoring its dependencies
57 EvalAlways,
58 }
59
60 impl Parse for QueryModifier {
61 fn parse(input: ParseStream<'_>) -> Result<Self> {
62 let modifier: Ident = input.parse()?;
63 if modifier == "desc" {
64 // Parse a description modifier like:
65 // `desc { |tcx| "foo {}", tcx.item_path(key) }`
66 let attr_content;
67 braced!(attr_content in input);
68 let tcx = if attr_content.peek(Token![|]) {
69 attr_content.parse::<Token![|]>()?;
70 let tcx = attr_content.parse()?;
71 attr_content.parse::<Token![|]>()?;
72 Some(tcx)
73 } else {
74 None
75 };
76 let desc = attr_content.parse_terminated(Expr::parse)?;
77 Ok(QueryModifier::Desc(tcx, desc))
78 } else if modifier == "cache_on_disk_if" {
79 // Parse a cache modifier like:
80 // `cache(tcx, value) { |tcx| key.is_local() }`
81 let has_args = if let TokenTree::Group(group) = input.fork().parse()? {
82 group.delimiter() == Delimiter::Parenthesis
83 } else {
84 false
85 };
86 let args = if has_args {
87 let args;
88 parenthesized!(args in input);
89 let tcx = args.parse()?;
90 args.parse::<Token![,]>()?;
91 let value = args.parse()?;
92 Some((tcx, value))
93 } else {
94 None
95 };
96 let block = input.parse()?;
97 Ok(QueryModifier::Cache(args, block))
98 } else if modifier == "load_cached" {
99 // Parse a load_cached modifier like:
100 // `load_cached(tcx, id) { tcx.queries.on_disk_cache.try_load_query_result(tcx, id) }`
101 let args;
102 parenthesized!(args in input);
103 let tcx = args.parse()?;
104 args.parse::<Token![,]>()?;
105 let id = args.parse()?;
106 let block = input.parse()?;
107 Ok(QueryModifier::LoadCached(tcx, id, block))
108 } else if modifier == "storage" {
109 let args;
110 parenthesized!(args in input);
111 let ty = args.parse()?;
112 Ok(QueryModifier::Storage(ty))
113 } else if modifier == "fatal_cycle" {
114 Ok(QueryModifier::FatalCycle)
115 } else if modifier == "cycle_delay_bug" {
116 Ok(QueryModifier::CycleDelayBug)
117 } else if modifier == "no_hash" {
118 Ok(QueryModifier::NoHash)
119 } else if modifier == "anon" {
120 Ok(QueryModifier::Anon)
121 } else if modifier == "eval_always" {
122 Ok(QueryModifier::EvalAlways)
123 } else {
124 Err(Error::new(modifier.span(), "unknown query modifier"))
125 }
126 }
127 }
128
129 /// Ensures only doc comment attributes are used
130 fn check_attributes(attrs: Vec<Attribute>) -> Result<Vec<Attribute>> {
131 let inner = |attr: Attribute| {
132 if !attr.path.is_ident("doc") {
133 Err(Error::new(attr.span(), "attributes not supported on queries"))
134 } else if attr.style != AttrStyle::Outer {
135 Err(Error::new(
136 attr.span(),
137 "attributes must be outer attributes (`///`), not inner attributes",
138 ))
139 } else {
140 Ok(attr)
141 }
142 };
143 attrs.into_iter().map(inner).collect()
144 }
145
146 /// A compiler query. `query ... { ... }`
147 struct Query {
148 doc_comments: Vec<Attribute>,
149 modifiers: List<QueryModifier>,
150 name: Ident,
151 key: IdentOrWild,
152 arg: Type,
153 result: ReturnType,
154 }
155
156 impl Parse for Query {
157 fn parse(input: ParseStream<'_>) -> Result<Self> {
158 let doc_comments = check_attributes(input.call(Attribute::parse_outer)?)?;
159
160 // Parse the query declaration. Like `query type_of(key: DefId) -> Ty<'tcx>`
161 input.parse::<kw::query>()?;
162 let name: Ident = input.parse()?;
163 let arg_content;
164 parenthesized!(arg_content in input);
165 let key = arg_content.parse()?;
166 arg_content.parse::<Token![:]>()?;
167 let arg = arg_content.parse()?;
168 let result = input.parse()?;
169
170 // Parse the query modifiers
171 let content;
172 braced!(content in input);
173 let modifiers = content.parse()?;
174
175 Ok(Query { doc_comments, modifiers, name, key, arg, result })
176 }
177 }
178
179 /// A type used to greedily parse another type until the input is empty.
180 struct List<T>(Vec<T>);
181
182 impl<T: Parse> Parse for List<T> {
183 fn parse(input: ParseStream<'_>) -> Result<Self> {
184 let mut list = Vec::new();
185 while !input.is_empty() {
186 list.push(input.parse()?);
187 }
188 Ok(List(list))
189 }
190 }
191
192 /// A named group containing queries.
193 ///
194 /// For now, the name is not used any more, but the capability remains interesting for future
195 /// developments of the query system.
196 struct Group {
197 #[allow(unused)]
198 name: Ident,
199 queries: List<Query>,
200 }
201
202 impl Parse for Group {
203 fn parse(input: ParseStream<'_>) -> Result<Self> {
204 let name: Ident = input.parse()?;
205 let content;
206 braced!(content in input);
207 Ok(Group { name, queries: content.parse()? })
208 }
209 }
210
211 struct QueryModifiers {
212 /// The description of the query.
213 desc: (Option<Ident>, Punctuated<Expr, Token![,]>),
214
215 /// Use this type for the in-memory cache.
216 storage: Option<Type>,
217
218 /// Cache the query to disk if the `Block` returns true.
219 cache: Option<(Option<(IdentOrWild, IdentOrWild)>, Block)>,
220
221 /// Custom code to load the query from disk.
222 load_cached: Option<(Ident, Ident, Block)>,
223
224 /// A cycle error for this query aborting the compilation with a fatal error.
225 fatal_cycle: bool,
226
227 /// A cycle error results in a delay_bug call
228 cycle_delay_bug: bool,
229
230 /// Don't hash the result, instead just mark a query red if it runs
231 no_hash: bool,
232
233 /// Generate a dep node based on the dependencies of the query
234 anon: bool,
235
236 // Always evaluate the query, ignoring its dependencies
237 eval_always: bool,
238 }
239
240 /// Process query modifiers into a struct, erroring on duplicates
241 fn process_modifiers(query: &mut Query) -> QueryModifiers {
242 let mut load_cached = None;
243 let mut storage = None;
244 let mut cache = None;
245 let mut desc = None;
246 let mut fatal_cycle = false;
247 let mut cycle_delay_bug = false;
248 let mut no_hash = false;
249 let mut anon = false;
250 let mut eval_always = false;
251 for modifier in query.modifiers.0.drain(..) {
252 match modifier {
253 QueryModifier::LoadCached(tcx, id, block) => {
254 if load_cached.is_some() {
255 panic!("duplicate modifier `load_cached` for query `{}`", query.name);
256 }
257 load_cached = Some((tcx, id, block));
258 }
259 QueryModifier::Storage(ty) => {
260 if storage.is_some() {
261 panic!("duplicate modifier `storage` for query `{}`", query.name);
262 }
263 storage = Some(ty);
264 }
265 QueryModifier::Cache(args, expr) => {
266 if cache.is_some() {
267 panic!("duplicate modifier `cache` for query `{}`", query.name);
268 }
269 cache = Some((args, expr));
270 }
271 QueryModifier::Desc(tcx, list) => {
272 if desc.is_some() {
273 panic!("duplicate modifier `desc` for query `{}`", query.name);
274 }
275 desc = Some((tcx, list));
276 }
277 QueryModifier::FatalCycle => {
278 if fatal_cycle {
279 panic!("duplicate modifier `fatal_cycle` for query `{}`", query.name);
280 }
281 fatal_cycle = true;
282 }
283 QueryModifier::CycleDelayBug => {
284 if cycle_delay_bug {
285 panic!("duplicate modifier `cycle_delay_bug` for query `{}`", query.name);
286 }
287 cycle_delay_bug = true;
288 }
289 QueryModifier::NoHash => {
290 if no_hash {
291 panic!("duplicate modifier `no_hash` for query `{}`", query.name);
292 }
293 no_hash = true;
294 }
295 QueryModifier::Anon => {
296 if anon {
297 panic!("duplicate modifier `anon` for query `{}`", query.name);
298 }
299 anon = true;
300 }
301 QueryModifier::EvalAlways => {
302 if eval_always {
303 panic!("duplicate modifier `eval_always` for query `{}`", query.name);
304 }
305 eval_always = true;
306 }
307 }
308 }
309 let desc = desc.unwrap_or_else(|| {
310 panic!("no description provided for query `{}`", query.name);
311 });
312 QueryModifiers {
313 load_cached,
314 storage,
315 cache,
316 desc,
317 fatal_cycle,
318 cycle_delay_bug,
319 no_hash,
320 anon,
321 eval_always,
322 }
323 }
324
325 /// Add the impl of QueryDescription for the query to `impls` if one is requested
326 fn add_query_description_impl(
327 query: &Query,
328 modifiers: QueryModifiers,
329 impls: &mut proc_macro2::TokenStream,
330 ) {
331 let name = &query.name;
332 let arg = &query.arg;
333 let key = &query.key.0;
334
335 // Find out if we should cache the query on disk
336 let cache = if let Some((args, expr)) = modifiers.cache.as_ref() {
337 let try_load_from_disk = if let Some((tcx, id, block)) = modifiers.load_cached.as_ref() {
338 // Use custom code to load the query from disk
339 quote! {
340 #[inline]
341 fn try_load_from_disk(
342 #tcx: TyCtxt<'tcx>,
343 #id: SerializedDepNodeIndex
344 ) -> Option<Self::Value> {
345 #block
346 }
347 }
348 } else {
349 // Use the default code to load the query from disk
350 quote! {
351 #[inline]
352 fn try_load_from_disk(
353 tcx: TyCtxt<'tcx>,
354 id: SerializedDepNodeIndex
355 ) -> Option<Self::Value> {
356 tcx.queries.on_disk_cache.as_ref().and_then(|c| c.try_load_query_result(tcx, id))
357 }
358 }
359 };
360
361 let tcx = args
362 .as_ref()
363 .map(|t| {
364 let t = &(t.0).0;
365 quote! { #t }
366 })
367 .unwrap_or(quote! { _ });
368 let value = args
369 .as_ref()
370 .map(|t| {
371 let t = &(t.1).0;
372 quote! { #t }
373 })
374 .unwrap_or(quote! { _ });
375 // expr is a `Block`, meaning that `{ #expr }` gets expanded
376 // to `{ { stmts... } }`, which triggers the `unused_braces` lint.
377 quote! {
378 #[inline]
379 #[allow(unused_variables, unused_braces)]
380 fn cache_on_disk(
381 #tcx: TyCtxt<'tcx>,
382 #key: &Self::Key,
383 #value: Option<&Self::Value>
384 ) -> bool {
385 #expr
386 }
387
388 #try_load_from_disk
389 }
390 } else {
391 if modifiers.load_cached.is_some() {
392 panic!("load_cached modifier on query `{}` without a cache modifier", name);
393 }
394 quote! {}
395 };
396
397 let (tcx, desc) = modifiers.desc;
398 let tcx = tcx.as_ref().map(|t| quote! { #t }).unwrap_or(quote! { _ });
399
400 let desc = quote! {
401 #[allow(unused_variables)]
402 fn describe(
403 #tcx: TyCtxt<'tcx>,
404 #key: #arg,
405 ) -> Cow<'static, str> {
406 ::rustc_middle::ty::print::with_no_trimmed_paths(|| format!(#desc).into())
407 }
408 };
409
410 impls.extend(quote! {
411 impl<'tcx> QueryDescription<TyCtxt<'tcx>> for queries::#name<'tcx> {
412 #desc
413 #cache
414 }
415 });
416 }
417
418 pub fn rustc_queries(input: TokenStream) -> TokenStream {
419 let groups = parse_macro_input!(input as List<Group>);
420
421 let mut query_stream = quote! {};
422 let mut query_description_stream = quote! {};
423 let mut dep_node_def_stream = quote! {};
424 let mut cached_queries = quote! {};
425
426 for group in groups.0 {
427 for mut query in group.queries.0 {
428 let modifiers = process_modifiers(&mut query);
429 let name = &query.name;
430 let arg = &query.arg;
431 let result_full = &query.result;
432 let result = match query.result {
433 ReturnType::Default => quote! { -> () },
434 _ => quote! { #result_full },
435 };
436
437 if modifiers.cache.is_some() {
438 cached_queries.extend(quote! {
439 #name,
440 });
441 }
442
443 let mut attributes = Vec::new();
444
445 // Pass on the fatal_cycle modifier
446 if modifiers.fatal_cycle {
447 attributes.push(quote! { fatal_cycle });
448 };
449 // Pass on the storage modifier
450 if let Some(ref ty) = modifiers.storage {
451 attributes.push(quote! { storage(#ty) });
452 };
453 // Pass on the cycle_delay_bug modifier
454 if modifiers.cycle_delay_bug {
455 attributes.push(quote! { cycle_delay_bug });
456 };
457 // Pass on the no_hash modifier
458 if modifiers.no_hash {
459 attributes.push(quote! { no_hash });
460 };
461 // Pass on the anon modifier
462 if modifiers.anon {
463 attributes.push(quote! { anon });
464 };
465 // Pass on the eval_always modifier
466 if modifiers.eval_always {
467 attributes.push(quote! { eval_always });
468 };
469
470 let attribute_stream = quote! {#(#attributes),*};
471 let doc_comments = query.doc_comments.iter();
472 // Add the query to the group
473 query_stream.extend(quote! {
474 #(#doc_comments)*
475 [#attribute_stream] fn #name(#arg) #result,
476 });
477
478 // Create a dep node for the query
479 dep_node_def_stream.extend(quote! {
480 [#attribute_stream] #name(#arg),
481 });
482
483 add_query_description_impl(&query, modifiers, &mut query_description_stream);
484 }
485 }
486
487 TokenStream::from(quote! {
488 macro_rules! rustc_query_append {
489 ([$($macro:tt)*][$($other:tt)*]) => {
490 $($macro)* {
491 $($other)*
492
493 #query_stream
494
495 }
496 }
497 }
498 macro_rules! rustc_dep_node_append {
499 ([$($macro:tt)*][$($other:tt)*]) => {
500 $($macro)*(
501 $($other)*
502
503 #dep_node_def_stream
504 );
505 }
506 }
507 macro_rules! rustc_cached_queries {
508 ($($macro:tt)*) => {
509 $($macro)*(#cached_queries);
510 }
511 }
512
513 #query_description_stream
514 })
515 }