blob: 87fe40561c65799dee5948404e26c5ade468fcca [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;
14
David Tolnay08419302020-04-19 20:38:20 -070015use crate::syntax::namespace::Namespace;
David Tolnay7db73692019-10-20 14:51:12 -040016use proc_macro::TokenStream;
Myron Ahneba35cf2020-02-05 19:41:51 +070017use syn::{parse_macro_input, Ident, ItemMod};
David Tolnay7db73692019-10-20 14:51:12 -040018
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}
Myron Ahneba35cf2020-02-05 19:41:51 +070047
48#[proc_macro]
49pub fn vector_builtin(input: TokenStream) -> TokenStream {
50 let ident = parse_macro_input!(input as Ident);
51 expand::expand_vector_builtin(ident).into()
52}