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::items_after_statements,
10 clippy::large_enum_variant,
11 clippy::match_bool,
12 clippy::match_same_arms,
13 clippy::module_name_repetitions,
14 clippy::needless_pass_by_value,
15 clippy::new_without_default,
16 clippy::nonminimal_bool,
17 clippy::option_if_let_else,
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::too_many_arguments,
25 clippy::too_many_lines,
26 clippy::toplevel_ref_arg,
27 clippy::useless_let_if_seq,
28 // clippy bug: https://github.com/rust-lang/rust-clippy/issues/6983
29 clippy::wrong_self_convention
30 )]
31
32 extern crate proc_macro;
33
34 mod derive;
35 mod expand;
36 mod generics;
37 mod syntax;
38 mod tokens;
39 mod type_id;
40
41 #[cfg(feature = "experimental-enum-variants-from-header")]
42 mod clang;
43 #[cfg(feature = "experimental-enum-variants-from-header")]
44 mod load;
45
46 use crate::syntax::file::Module;
47 use crate::syntax::namespace::Namespace;
48 use crate::syntax::qualified::QualifiedName;
49 use crate::type_id::Crate;
50 use proc_macro::TokenStream;
51 use syn::parse::{Parse, ParseStream, Parser, Result};
52 use 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]
bridge(args: TokenStream, input: TokenStream) -> TokenStream72 pub 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]
type_id(input: TokenStream) -> TokenStream89 pub 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 }
106