blob: c2ad38b09ae3722d922a978a8376412f5329d124 [file] [log] [blame]
David Tolnay7db73692019-10-20 14:51:12 -04001#![allow(
David Tolnay30d214c2020-03-15 23:54:34 -07002 clippy::inherent_to_string,
David Tolnay7db73692019-10-20 14:51:12 -04003 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
10extern crate proc_macro;
11
12mod expand;
David Tolnay7db73692019-10-20 14:51:12 -040013mod syntax;
David Tolnayd09e0122020-05-04 02:34:23 -070014mod type_id;
David Tolnay7db73692019-10-20 14:51:12 -040015
David Tolnay05ef6ff2020-08-29 11:27:05 -070016use crate::syntax::file::Module;
David Tolnay08419302020-04-19 20:38:20 -070017use crate::syntax::namespace::Namespace;
David Tolnay7db73692019-10-20 14:51:12 -040018use proc_macro::TokenStream;
David Tolnay05ef6ff2020-08-29 11:27:05 -070019use syn::{parse_macro_input, LitStr};
David Tolnay7db73692019-10-20 14:51:12 -040020
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]
39pub fn bridge(args: TokenStream, input: TokenStream) -> TokenStream {
40 let _ = syntax::error::ERRORS;
41
42 let namespace = parse_macro_input!(args as Namespace);
David Tolnay3c64a4e2020-08-29 14:07:38 -070043 let mut ffi = parse_macro_input!(input as Module);
44 ffi.namespace = namespace;
David Tolnay7db73692019-10-20 14:51:12 -040045
David Tolnay3c64a4e2020-08-29 14:07:38 -070046 expand::bridge(ffi)
David Tolnay7db73692019-10-20 14:51:12 -040047 .unwrap_or_else(|err| err.to_compile_error())
48 .into()
49}
David Tolnayd09e0122020-05-04 02:34:23 -070050
51#[proc_macro]
52pub fn type_id(input: TokenStream) -> TokenStream {
53 let arg = parse_macro_input!(input as LitStr);
54 type_id::expand(arg).into()
55}