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