cxxbridge_macro/
lib.rs

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