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