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