blob: 89f25499156bc38ade7c161ac2fa568f817bec43 [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,
David Tolnayef665fd2020-11-15 17:25:31 -08005 clippy::nonminimal_bool,
David Tolnay7db73692019-10-20 14:51:12 -04006 clippy::or_fun_call,
David Tolnay982158b2020-11-27 15:58:49 -08007 clippy::single_match,
David Tolnay7db73692019-10-20 14:51:12 -04008 clippy::toplevel_ref_arg,
9 clippy::useless_let_if_seq
10)]
11
12extern crate proc_macro;
13
David Tolnayd8ad9702020-11-27 12:43:59 -080014mod derive;
David Tolnay7db73692019-10-20 14:51:12 -040015mod expand;
David Tolnay7db73692019-10-20 14:51:12 -040016mod syntax;
David Tolnayd09e0122020-05-04 02:34:23 -070017mod type_id;
David Tolnay7db73692019-10-20 14:51:12 -040018
David Tolnay05ef6ff2020-08-29 11:27:05 -070019use crate::syntax::file::Module;
David Tolnay08419302020-04-19 20:38:20 -070020use crate::syntax::namespace::Namespace;
David Tolnaya8d94a12020-09-06 23:28:18 -070021use crate::syntax::qualified::QualifiedName;
David Tolnay7db73692019-10-20 14:51:12 -040022use proc_macro::TokenStream;
Adrian Taylor3e5cff42020-10-29 19:48:45 -070023use syn::parse::{Parse, ParseStream, Parser, Result};
David Tolnaya8d94a12020-09-06 23:28:18 -070024use syn::parse_macro_input;
David Tolnay7db73692019-10-20 14:51:12 -040025
26/// `#[cxx::bridge] mod ffi { ... }`
27///
28/// Refer to the crate-level documentation for the explanation of how this macro
29/// is intended to be used.
30///
31/// The only additional thing to note here is namespace support — if the
David Tolnayc72a9f62020-11-11 10:56:26 -080032/// types and functions on the `extern "C++"` side of our bridge are in a
David Tolnay7db73692019-10-20 14:51:12 -040033/// namespace, specify that namespace as an argument of the cxx::bridge
34/// attribute macro.
35///
36/// ```
David Tolnay79079662020-11-01 09:11:55 -080037/// #[cxx::bridge(namespace = "mycompany::rust")]
David Tolnay7db73692019-10-20 14:51:12 -040038/// # mod ffi {}
39/// ```
40///
41/// The types and functions from the `extern "Rust"` side of the bridge will be
42/// placed into that same namespace in the generated C++ code.
43#[proc_macro_attribute]
44pub fn bridge(args: TokenStream, input: TokenStream) -> TokenStream {
45 let _ = syntax::error::ERRORS;
46
Adrian Taylor3e5cff42020-10-29 19:48:45 -070047 let namespace = match Namespace::parse_bridge_attr_namespace.parse(args) {
David Tolnayd7a3a182020-11-01 20:45:14 -080048 Ok(namespace) => namespace,
Adrian Taylor3e5cff42020-10-29 19:48:45 -070049 Err(err) => return err.to_compile_error().into(),
50 };
David Tolnay3c64a4e2020-08-29 14:07:38 -070051 let mut ffi = parse_macro_input!(input as Module);
52 ffi.namespace = namespace;
David Tolnay7db73692019-10-20 14:51:12 -040053
David Tolnay3c64a4e2020-08-29 14:07:38 -070054 expand::bridge(ffi)
David Tolnay7db73692019-10-20 14:51:12 -040055 .unwrap_or_else(|err| err.to_compile_error())
56 .into()
57}
David Tolnayd09e0122020-05-04 02:34:23 -070058
59#[proc_macro]
60pub fn type_id(input: TokenStream) -> TokenStream {
David Tolnaya8d94a12020-09-06 23:28:18 -070061 struct TypeId(QualifiedName);
62
63 impl Parse for TypeId {
64 fn parse(input: ParseStream) -> Result<Self> {
65 QualifiedName::parse_quoted_or_unquoted(input).map(TypeId)
66 }
67 }
68
69 let arg = parse_macro_input!(input as TypeId);
70 type_id::expand(arg.0).into()
David Tolnayd09e0122020-05-04 02:34:23 -070071}