cxxbridge_macro/
lib.rs

1#![allow(
2    clippy::cast_sign_loss,
3    clippy::default_trait_access,
4    clippy::derive_partial_eq_without_eq,
5    clippy::doc_markdown,
6    clippy::enum_glob_use,
7    clippy::if_same_then_else,
8    clippy::inherent_to_string,
9    clippy::into_iter_without_iter,
10    clippy::items_after_statements,
11    clippy::large_enum_variant,
12    clippy::match_bool,
13    clippy::match_same_arms,
14    clippy::module_name_repetitions,
15    clippy::needless_lifetimes,
16    clippy::needless_pass_by_value,
17    clippy::new_without_default,
18    clippy::nonminimal_bool,
19    clippy::or_fun_call,
20    clippy::redundant_else,
21    clippy::ref_option,
22    clippy::shadow_unrelated,
23    clippy::similar_names,
24    clippy::single_match,
25    clippy::single_match_else,
26    clippy::struct_field_names,
27    clippy::too_many_arguments,
28    clippy::too_many_lines,
29    clippy::toplevel_ref_arg,
30    clippy::uninlined_format_args,
31    clippy::useless_let_if_seq
32)]
33
34mod derive;
35mod expand;
36mod generics;
37mod syntax;
38mod tokens;
39mod type_id;
40
41#[cfg(feature = "experimental-enum-variants-from-header")]
42mod clang;
43#[cfg(feature = "experimental-enum-variants-from-header")]
44mod load;
45
46use crate::syntax::file::Module;
47use crate::syntax::namespace::Namespace;
48use crate::syntax::qualified::QualifiedName;
49use crate::type_id::Crate;
50use proc_macro::TokenStream;
51use syn::parse::{Parse, ParseStream, Parser, Result};
52use syn::parse_macro_input;
53
54/// `#[cxx::bridge] mod ffi { ... }`
55///
56/// Refer to the crate-level documentation for the explanation of how this macro
57/// is intended to be used.
58///
59/// The only additional thing to note here is namespace support — if the
60/// types and functions on the `extern "C++"` side of our bridge are in a
61/// namespace, specify that namespace as an argument of the cxx::bridge
62/// attribute macro.
63///
64/// ```
65/// #[cxx::bridge(namespace = "mycompany::rust")]
66/// # mod ffi {}
67/// ```
68///
69/// The types and functions from the `extern "Rust"` side of the bridge will be
70/// placed into that same namespace in the generated C++ code.
71#[proc_macro_attribute]
72pub fn bridge(args: TokenStream, input: TokenStream) -> TokenStream {
73    let _ = syntax::error::ERRORS;
74
75    let namespace = match Namespace::parse_bridge_attr_namespace.parse(args) {
76        Ok(namespace) => namespace,
77        Err(err) => return err.to_compile_error().into(),
78    };
79    let mut ffi = parse_macro_input!(input as Module);
80    ffi.namespace = namespace;
81
82    expand::bridge(ffi)
83        .unwrap_or_else(|err| err.to_compile_error())
84        .into()
85}
86
87#[doc(hidden)]
88#[proc_macro]
89pub fn type_id(input: TokenStream) -> TokenStream {
90    struct TypeId {
91        krate: Crate,
92        path: QualifiedName,
93    }
94
95    impl Parse for TypeId {
96        fn parse(input: ParseStream) -> Result<Self> {
97            let krate = input.parse().map(Crate::DollarCrate)?;
98            let path = QualifiedName::parse_quoted_or_unquoted(input)?;
99            Ok(TypeId { krate, path })
100        }
101    }
102
103    let arg = parse_macro_input!(input as TypeId);
104    type_id::expand(arg.krate, arg.path).into()
105}