blob: a2117610c5d3ed366cb5b635e87cc811ec95bc62 [file] [log] [blame]
David Tolnay7db73692019-10-20 14:51:12 -04001#![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
9extern crate proc_macro;
10
11mod expand;
12mod namespace;
13mod syntax;
14
15use crate::namespace::Namespace;
16use proc_macro::TokenStream;
17use 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]
37pub 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}