]> git.proxmox.com Git - rustc.git/blob - vendor/proc-macro-hack/src/lib.rs
New upstream version 1.46.0~beta.2+dfsg1
[rustc.git] / vendor / proc-macro-hack / src / lib.rs
1 //! [![github]](https://github.com/dtolnay/proc-macro-hack) [![crates-io]](https://crates.io/crates/proc-macro-hack) [![docs-rs]](https://docs.rs/proc-macro-hack)
2 //!
3 //! [github]: https://img.shields.io/badge/github-8da0cb?style=for-the-badge&labelColor=555555&logo=github
4 //! [crates-io]: https://img.shields.io/badge/crates.io-fc8d62?style=for-the-badge&labelColor=555555&logo=rust
5 //! [docs-rs]: https://img.shields.io/badge/docs.rs-66c2a5?style=for-the-badge&labelColor=555555&logoColor=white&logo=data:image/svg+xml;base64,PHN2ZyByb2xlPSJpbWciIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgdmlld0JveD0iMCAwIDUxMiA1MTIiPjxwYXRoIGZpbGw9IiNmNWY1ZjUiIGQ9Ik00ODguNiAyNTAuMkwzOTIgMjE0VjEwNS41YzAtMTUtOS4zLTI4LjQtMjMuNC0zMy43bC0xMDAtMzcuNWMtOC4xLTMuMS0xNy4xLTMuMS0yNS4zIDBsLTEwMCAzNy41Yy0xNC4xIDUuMy0yMy40IDE4LjctMjMuNCAzMy43VjIxNGwtOTYuNiAzNi4yQzkuMyAyNTUuNSAwIDI2OC45IDAgMjgzLjlWMzk0YzAgMTMuNiA3LjcgMjYuMSAxOS45IDMyLjJsMTAwIDUwYzEwLjEgNS4xIDIyLjEgNS4xIDMyLjIgMGwxMDMuOS01MiAxMDMuOSA1MmMxMC4xIDUuMSAyMi4xIDUuMSAzMi4yIDBsMTAwLTUwYzEyLjItNi4xIDE5LjktMTguNiAxOS45LTMyLjJWMjgzLjljMC0xNS05LjMtMjguNC0yMy40LTMzLjd6TTM1OCAyMTQuOGwtODUgMzEuOXYtNjguMmw4NS0zN3Y3My4zek0xNTQgMTA0LjFsMTAyLTM4LjIgMTAyIDM4LjJ2LjZsLTEwMiA0MS40LTEwMi00MS40di0uNnptODQgMjkxLjFsLTg1IDQyLjV2LTc5LjFsODUtMzguOHY3NS40em0wLTExMmwtMTAyIDQxLjQtMTAyLTQxLjR2LS42bDEwMi0zOC4yIDEwMiAzOC4ydi42em0yNDAgMTEybC04NSA0Mi41di03OS4xbDg1LTM4Ljh2NzUuNHptMC0xMTJsLTEwMiA0MS40LTEwMi00MS40di0uNmwxMDItMzguMiAxMDIgMzguMnYuNnoiPjwvcGF0aD48L3N2Zz4K
6 //!
7 //! <br>
8 //!
9 //! As of Rust 1.30, the language supports user-defined function-like procedural
10 //! macros. However these can only be invoked in item position, not in
11 //! statements or expressions.
12 //!
13 //! This crate implements an alternative type of procedural macro that can be
14 //! invoked in statement or expression position.
15 //!
16 //! # Defining procedural macros
17 //!
18 //! Two crates are required to define a procedural macro.
19 //!
20 //! ## The implementation crate
21 //!
22 //! This crate must contain nothing but procedural macros. Private helper
23 //! functions and private modules are fine but nothing can be public.
24 //!
25 //! [&raquo; example of an implementation crate][demo-hack-impl]
26 //!
27 //! Just like you would use a #\[proc_macro\] attribute to define a natively
28 //! supported procedural macro, use proc-macro-hack's #\[proc_macro_hack\]
29 //! attribute to define a procedural macro that works in expression position.
30 //! The function signature is the same as for ordinary function-like procedural
31 //! macros.
32 //!
33 //! ```
34 //! extern crate proc_macro;
35 //!
36 //! use proc_macro::TokenStream;
37 //! use proc_macro_hack::proc_macro_hack;
38 //! use quote::quote;
39 //! use syn::{parse_macro_input, Expr};
40 //!
41 //! # const IGNORE: &str = stringify! {
42 //! #[proc_macro_hack]
43 //! # };
44 //! pub fn add_one(input: TokenStream) -> TokenStream {
45 //! let expr = parse_macro_input!(input as Expr);
46 //! TokenStream::from(quote! {
47 //! 1 + (#expr)
48 //! })
49 //! }
50 //! #
51 //! # fn main() {}
52 //! ```
53 //!
54 //! ## The declaration crate
55 //!
56 //! This crate is allowed to contain other public things if you need, for
57 //! example traits or functions or ordinary macros.
58 //!
59 //! [&raquo; example of a declaration crate][demo-hack]
60 //!
61 //! Within the declaration crate there needs to be a re-export of your
62 //! procedural macro from the implementation crate. The re-export also carries a
63 //! \#\[proc_macro_hack\] attribute.
64 //!
65 //! ```
66 //! use proc_macro_hack::proc_macro_hack;
67 //!
68 //! /// Add one to an expression.
69 //! ///
70 //! /// (Documentation goes here on the re-export, not in the other crate.)
71 //! #[proc_macro_hack]
72 //! pub use demo_hack_impl::add_one;
73 //! #
74 //! # fn main() {}
75 //! ```
76 //!
77 //! Both crates depend on `proc-macro-hack`:
78 //!
79 //! ```toml
80 //! [dependencies]
81 //! proc-macro-hack = "0.5"
82 //! ```
83 //!
84 //! Additionally, your implementation crate (but not your declaration crate) is
85 //! a proc macro crate:
86 //!
87 //! ```toml
88 //! [lib]
89 //! proc-macro = true
90 //! ```
91 //!
92 //! # Using procedural macros
93 //!
94 //! Users of your crate depend on your declaration crate (not your
95 //! implementation crate), then use your procedural macros as usual.
96 //!
97 //! [&raquo; example of a downstream crate][example]
98 //!
99 //! ```
100 //! use demo_hack::add_one;
101 //!
102 //! fn main() {
103 //! let two = 2;
104 //! let nine = add_one!(two) + add_one!(2 + 3);
105 //! println!("nine = {}", nine);
106 //! }
107 //! ```
108 //!
109 //! [demo-hack-impl]: https://github.com/dtolnay/proc-macro-hack/tree/master/demo-hack-impl
110 //! [demo-hack]: https://github.com/dtolnay/proc-macro-hack/tree/master/demo-hack
111 //! [example]: https://github.com/dtolnay/proc-macro-hack/tree/master/example
112 //!
113 //! # Limitations
114 //!
115 //! - Only proc macros in expression position are supported. Proc macros in
116 //! pattern position ([#20]) are not supported.
117 //!
118 //! - By default, nested invocations are not supported i.e. the code emitted by
119 //! a proc-macro-hack macro invocation cannot contain recursive calls to the
120 //! same proc-macro-hack macro nor calls to any other proc-macro-hack macros.
121 //! Use [`proc-macro-nested`] if you require support for nested invocations.
122 //!
123 //! - By default, hygiene is structured such that the expanded code can't refer
124 //! to local variables other than those passed by name somewhere in the macro
125 //! input. If your macro must refer to *local* variables that don't get named
126 //! in the macro input, use `#[proc_macro_hack(fake_call_site)]` on the
127 //! re-export in your declaration crate. *Most macros won't need this.*
128 //!
129 //! [#10]: https://github.com/dtolnay/proc-macro-hack/issues/10
130 //! [#20]: https://github.com/dtolnay/proc-macro-hack/issues/20
131 //! [`proc-macro-nested`]: https://docs.rs/proc-macro-nested
132
133 #![recursion_limit = "512"]
134 #![allow(clippy::needless_doctest_main, clippy::toplevel_ref_arg)]
135
136 extern crate proc_macro;
137
138 #[macro_use]
139 mod quote;
140
141 mod error;
142 mod iter;
143 mod parse;
144
145 use crate::error::{compile_error, Error};
146 use crate::iter::Iter;
147 use crate::parse::{
148 parse_define_args, parse_enum_hack, parse_export_args, parse_fake_call_site, parse_input,
149 };
150 use proc_macro::{Ident, Punct, Spacing, Span, TokenStream, TokenTree};
151 use std::fmt::Write;
152
153 type Visibility = Option<Span>;
154
155 enum Input {
156 Export(Export),
157 Define(Define),
158 }
159
160 // pub use demo_hack_impl::{m1, m2 as qrst};
161 struct Export {
162 attrs: TokenStream,
163 vis: Visibility,
164 from: Ident,
165 macros: Vec<Macro>,
166 }
167
168 // pub fn m1(input: TokenStream) -> TokenStream { ... }
169 struct Define {
170 attrs: TokenStream,
171 name: Ident,
172 body: TokenStream,
173 }
174
175 struct Macro {
176 name: Ident,
177 export_as: Ident,
178 }
179
180 #[proc_macro_attribute]
181 pub fn proc_macro_hack(args: TokenStream, input: TokenStream) -> TokenStream {
182 let ref mut args = iter::new(args);
183 let ref mut input = iter::new(input);
184 expand_proc_macro_hack(args, input).unwrap_or_else(compile_error)
185 }
186
187 fn expand_proc_macro_hack(args: Iter, input: Iter) -> Result<TokenStream, Error> {
188 match parse_input(input)? {
189 Input::Export(export) => {
190 let args = parse_export_args(args)?;
191 Ok(expand_export(export, args))
192 }
193 Input::Define(define) => {
194 parse_define_args(args)?;
195 Ok(expand_define(define))
196 }
197 }
198 }
199
200 #[doc(hidden)]
201 #[proc_macro_derive(ProcMacroHack)]
202 pub fn enum_hack(input: TokenStream) -> TokenStream {
203 let ref mut input = iter::new(input);
204 parse_enum_hack(input).unwrap_or_else(compile_error)
205 }
206
207 struct FakeCallSite {
208 derive: Ident,
209 rest: TokenStream,
210 }
211
212 #[doc(hidden)]
213 #[proc_macro_attribute]
214 pub fn fake_call_site(args: TokenStream, input: TokenStream) -> TokenStream {
215 let ref mut args = iter::new(args);
216 let ref mut input = iter::new(input);
217 expand_fake_call_site(args, input).unwrap_or_else(compile_error)
218 }
219
220 fn expand_fake_call_site(args: Iter, input: Iter) -> Result<TokenStream, Error> {
221 let span = match args.next() {
222 Some(token) => token.span(),
223 None => return Ok(input.collect()),
224 };
225
226 let input = parse_fake_call_site(input)?;
227 let mut derive = input.derive;
228 derive.set_span(span);
229 let rest = input.rest;
230
231 Ok(quote! {
232 #[derive(#derive)]
233 #rest
234 })
235 }
236
237 struct ExportArgs {
238 support_nested: bool,
239 internal_macro_calls: u16,
240 fake_call_site: bool,
241 }
242
243 fn expand_export(export: Export, args: ExportArgs) -> TokenStream {
244 let dummy = dummy_name_for_export(&export);
245
246 let attrs = export.attrs;
247 let ref vis = export.vis.map(|span| Ident::new("pub", span));
248 let macro_export = match vis {
249 Some(_) => quote!(#[macro_export]),
250 None => quote!(),
251 };
252 let crate_prefix = vis.as_ref().map(|_| quote!($crate::));
253 let enum_variant = if args.support_nested {
254 if args.internal_macro_calls == 0 {
255 Ident::new("Nested", Span::call_site())
256 } else {
257 let name = format!("Nested{}", args.internal_macro_calls);
258 Ident::new(&name, Span::call_site())
259 }
260 } else {
261 Ident::new("Value", Span::call_site())
262 };
263
264 let from = export.from;
265 let rules = export
266 .macros
267 .into_iter()
268 .map(|Macro { name, export_as }| {
269 let actual_name = actual_proc_macro_name(&name);
270 let dispatch = dispatch_macro_name(&name);
271 let call_site = call_site_macro_name(&name);
272
273 let export_dispatch = if args.support_nested {
274 quote! {
275 #[doc(hidden)]
276 #vis use proc_macro_nested::dispatch as #dispatch;
277 }
278 } else {
279 quote!()
280 };
281
282 let proc_macro_call = if args.support_nested {
283 let extra_bangs = (0..args.internal_macro_calls)
284 .map(|_| TokenTree::Punct(Punct::new('!', Spacing::Alone)))
285 .collect::<TokenStream>();
286 quote! {
287 #crate_prefix #dispatch! { ($($proc_macro)*) #extra_bangs }
288 }
289 } else {
290 quote! {
291 proc_macro_call!()
292 }
293 };
294
295 let export_call_site = if args.fake_call_site {
296 quote! {
297 #[doc(hidden)]
298 #vis use proc_macro_hack::fake_call_site as #call_site;
299 }
300 } else {
301 quote!()
302 };
303
304 let do_derive = if !args.fake_call_site {
305 quote! {
306 #[derive(#crate_prefix #actual_name)]
307 }
308 } else if crate_prefix.is_some() {
309 quote! {
310 use #crate_prefix #actual_name;
311 #[#crate_prefix #call_site ($($proc_macro)*)]
312 #[derive(#actual_name)]
313 }
314 } else {
315 quote! {
316 #[#call_site ($($proc_macro)*)]
317 #[derive(#actual_name)]
318 }
319 };
320
321 quote! {
322 #[doc(hidden)]
323 #vis use #from::#actual_name;
324
325 #export_dispatch
326 #export_call_site
327
328 #attrs
329 #macro_export
330 macro_rules! #export_as {
331 ($($proc_macro:tt)*) => {{
332 #do_derive
333 #[allow(dead_code)]
334 enum ProcMacroHack {
335 #enum_variant = (stringify! { $($proc_macro)* }, 0).1,
336 }
337 #proc_macro_call
338 }};
339 }
340 }
341 })
342 .collect();
343
344 wrap_in_enum_hack(dummy, rules)
345 }
346
347 fn expand_define(define: Define) -> TokenStream {
348 let attrs = define.attrs;
349 let name = define.name;
350 let dummy = actual_proc_macro_name(&name);
351 let body = define.body;
352
353 quote! {
354 mod #dummy {
355 extern crate proc_macro;
356 pub use self::proc_macro::*;
357 }
358
359 #attrs
360 #[proc_macro_derive(#dummy)]
361 pub fn #dummy(input: #dummy::TokenStream) -> #dummy::TokenStream {
362 use std::iter::FromIterator;
363
364 let mut iter = input.into_iter();
365 iter.next().unwrap(); // `enum`
366 iter.next().unwrap(); // `ProcMacroHack`
367 iter.next().unwrap(); // `#`
368 iter.next().unwrap(); // `[allow(dead_code)]`
369
370 let mut braces = match iter.next().unwrap() {
371 #dummy::TokenTree::Group(group) => group.stream().into_iter(),
372 _ => unimplemented!(),
373 };
374 let variant = braces.next().unwrap(); // `Value` or `Nested`
375 let varname = variant.to_string();
376 let support_nested = varname.starts_with("Nested");
377 braces.next().unwrap(); // `=`
378
379 let mut parens = match braces.next().unwrap() {
380 #dummy::TokenTree::Group(group) => group.stream().into_iter(),
381 _ => unimplemented!(),
382 };
383 parens.next().unwrap(); // `stringify`
384 parens.next().unwrap(); // `!`
385
386 let inner = match parens.next().unwrap() {
387 #dummy::TokenTree::Group(group) => group.stream(),
388 _ => unimplemented!(),
389 };
390
391 let output: #dummy::TokenStream = #name(inner.clone());
392
393 fn count_bangs(input: #dummy::TokenStream) -> usize {
394 let mut count = 0;
395 for token in input {
396 match token {
397 #dummy::TokenTree::Punct(punct) => {
398 if punct.as_char() == '!' {
399 count += 1;
400 }
401 }
402 #dummy::TokenTree::Group(group) => {
403 count += count_bangs(group.stream());
404 }
405 _ => {}
406 }
407 }
408 count
409 }
410
411 // macro_rules! proc_macro_call {
412 // () => { #output }
413 // }
414 #dummy::TokenStream::from_iter(vec![
415 #dummy::TokenTree::Ident(
416 #dummy::Ident::new("macro_rules", #dummy::Span::call_site()),
417 ),
418 #dummy::TokenTree::Punct(
419 #dummy::Punct::new('!', #dummy::Spacing::Alone),
420 ),
421 #dummy::TokenTree::Ident(
422 #dummy::Ident::new(
423 &if support_nested {
424 let extra_bangs = if varname == "Nested" {
425 0
426 } else {
427 varname["Nested".len()..].parse().unwrap()
428 };
429 format!("proc_macro_call_{}", extra_bangs + count_bangs(inner))
430 } else {
431 String::from("proc_macro_call")
432 },
433 #dummy::Span::call_site(),
434 ),
435 ),
436 #dummy::TokenTree::Group(
437 #dummy::Group::new(#dummy::Delimiter::Brace, #dummy::TokenStream::from_iter(vec![
438 #dummy::TokenTree::Group(
439 #dummy::Group::new(#dummy::Delimiter::Parenthesis, #dummy::TokenStream::new()),
440 ),
441 #dummy::TokenTree::Punct(
442 #dummy::Punct::new('=', #dummy::Spacing::Joint),
443 ),
444 #dummy::TokenTree::Punct(
445 #dummy::Punct::new('>', #dummy::Spacing::Alone),
446 ),
447 #dummy::TokenTree::Group(
448 #dummy::Group::new(#dummy::Delimiter::Brace, output),
449 ),
450 ])),
451 ),
452 ])
453 }
454
455 fn #name #body
456 }
457 }
458
459 fn actual_proc_macro_name(conceptual: &Ident) -> Ident {
460 Ident::new(
461 &format!("proc_macro_hack_{}", conceptual),
462 conceptual.span(),
463 )
464 }
465
466 fn dispatch_macro_name(conceptual: &Ident) -> Ident {
467 Ident::new(
468 &format!("proc_macro_call_{}", conceptual),
469 conceptual.span(),
470 )
471 }
472
473 fn call_site_macro_name(conceptual: &Ident) -> Ident {
474 Ident::new(
475 &format!("proc_macro_fake_call_site_{}", conceptual),
476 conceptual.span(),
477 )
478 }
479
480 fn dummy_name_for_export(export: &Export) -> String {
481 let mut dummy = String::new();
482 let from = unraw(&export.from).to_string();
483 write!(dummy, "_{}{}", from.len(), from).unwrap();
484 for m in &export.macros {
485 let name = unraw(&m.name).to_string();
486 write!(dummy, "_{}{}", name.len(), name).unwrap();
487 }
488 dummy
489 }
490
491 fn unraw(ident: &Ident) -> Ident {
492 let string = ident.to_string();
493 if string.starts_with("r#") {
494 Ident::new(&string[2..], ident.span())
495 } else {
496 ident.clone()
497 }
498 }
499
500 fn wrap_in_enum_hack(dummy: String, inner: TokenStream) -> TokenStream {
501 let dummy = Ident::new(&dummy, Span::call_site());
502 quote! {
503 #[derive(proc_macro_hack::ProcMacroHack)]
504 enum #dummy {
505 Value = (stringify! { #inner }, 0).1,
506 }
507 }
508 }