| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1 | #![allow( |
| David Tolnay | 30d214c | 2020-03-15 23:54:34 -0700 | [diff] [blame] | 2 | clippy::inherent_to_string, |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 3 | clippy::large_enum_variant, |
| 4 | clippy::new_without_default, |
| 5 | clippy::or_fun_call, |
| 6 | clippy::toplevel_ref_arg, |
| 7 | clippy::useless_let_if_seq |
| 8 | )] |
| 9 | |
| 10 | extern crate proc_macro; |
| 11 | |
| 12 | mod expand; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 13 | mod syntax; |
| David Tolnay | d09e012 | 2020-05-04 02:34:23 -0700 | [diff] [blame] | 14 | mod type_id; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 15 | |
| David Tolnay | 05ef6ff | 2020-08-29 11:27:05 -0700 | [diff] [blame] | 16 | use crate::syntax::file::Module; |
| David Tolnay | 0841930 | 2020-04-19 20:38:20 -0700 | [diff] [blame] | 17 | use crate::syntax::namespace::Namespace; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 18 | use proc_macro::TokenStream; |
| David Tolnay | 05ef6ff | 2020-08-29 11:27:05 -0700 | [diff] [blame] | 19 | use syn::{parse_macro_input, LitStr}; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 20 | |
| 21 | /// `#[cxx::bridge] mod ffi { ... }` |
| 22 | /// |
| 23 | /// Refer to the crate-level documentation for the explanation of how this macro |
| 24 | /// is intended to be used. |
| 25 | /// |
| 26 | /// The only additional thing to note here is namespace support — if the |
| 27 | /// types and functions on the `extern "C"` side of our bridge are in a |
| 28 | /// namespace, specify that namespace as an argument of the cxx::bridge |
| 29 | /// attribute macro. |
| 30 | /// |
| 31 | /// ``` |
| 32 | /// #[cxx::bridge(namespace = mycompany::rust)] |
| 33 | /// # mod ffi {} |
| 34 | /// ``` |
| 35 | /// |
| 36 | /// The types and functions from the `extern "Rust"` side of the bridge will be |
| 37 | /// placed into that same namespace in the generated C++ code. |
| 38 | #[proc_macro_attribute] |
| 39 | pub fn bridge(args: TokenStream, input: TokenStream) -> TokenStream { |
| 40 | let _ = syntax::error::ERRORS; |
| 41 | |
| 42 | let namespace = parse_macro_input!(args as Namespace); |
| David Tolnay | 3c64a4e | 2020-08-29 14:07:38 -0700 | [diff] [blame^] | 43 | let mut ffi = parse_macro_input!(input as Module); |
| 44 | ffi.namespace = namespace; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 45 | |
| David Tolnay | 3c64a4e | 2020-08-29 14:07:38 -0700 | [diff] [blame^] | 46 | expand::bridge(ffi) |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 47 | .unwrap_or_else(|err| err.to_compile_error()) |
| 48 | .into() |
| 49 | } |
| David Tolnay | d09e012 | 2020-05-04 02:34:23 -0700 | [diff] [blame] | 50 | |
| 51 | #[proc_macro] |
| 52 | pub fn type_id(input: TokenStream) -> TokenStream { |
| 53 | let arg = parse_macro_input!(input as LitStr); |
| 54 | type_id::expand(arg).into() |
| 55 | } |