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