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