Allow namespace override.
This change allows a
#[namespace (namespace = A::B)]
attribute for each item in a cxx::bridge.
We now have a fair number of different types of name floating
around:
* C++ identifiers
* C++ fully-qualified names
* Rust identifiers
* Rust fully-qualified names (future, when we support sub-modules)
* Items with both a Rust and C++ name (a 'Pair')
* Types with only a known Rust name, which can be resolved to a
C++ name.
This change attempts to put some sensible names for all these
things in syntax/mod.rs, and so that would be a good place to start
review.
At the moment, the Namespace (included in each CppName)
is ruthlessly cloned all over the place. As a given namespace is
likely to be applicable to many types and functions, it may
save significant memory in future to use Rc<> here. But let's not
optimise too early.
diff --git a/syntax/mangle.rs b/syntax/mangle.rs
index e461887..9255feb 100644
--- a/syntax/mangle.rs
+++ b/syntax/mangle.rs
@@ -1,6 +1,5 @@
-use crate::syntax::namespace::Namespace;
use crate::syntax::symbol::{self, Symbol};
-use crate::syntax::ExternFn;
+use crate::syntax::{ExternFn, Types};
use proc_macro2::Ident;
const CXXBRIDGE: &str = "cxxbridge05";
@@ -11,19 +10,27 @@
};
}
-pub fn extern_fn(namespace: &Namespace, efn: &ExternFn) -> Symbol {
+pub fn extern_fn(efn: &ExternFn, types: &Types) -> Symbol {
match &efn.receiver {
- Some(receiver) => join!(namespace, CXXBRIDGE, receiver.ty, efn.ident.rust),
- None => join!(namespace, CXXBRIDGE, efn.ident.rust),
+ Some(receiver) => {
+ let receiver_ident = types.resolve(&receiver.ty);
+ join!(
+ efn.ident.cxx.ns,
+ CXXBRIDGE,
+ receiver_ident.ident,
+ efn.ident.rust
+ )
+ }
+ None => join!(efn.ident.cxx.ns, CXXBRIDGE, efn.ident.rust),
}
}
// The C half of a function pointer trampoline.
-pub fn c_trampoline(namespace: &Namespace, efn: &ExternFn, var: &Ident) -> Symbol {
- join!(extern_fn(namespace, efn), var, 0)
+pub fn c_trampoline(efn: &ExternFn, var: &Ident, types: &Types) -> Symbol {
+ join!(extern_fn(efn, types), var, 0)
}
// The Rust half of a function pointer trampoline.
-pub fn r_trampoline(namespace: &Namespace, efn: &ExternFn, var: &Ident) -> Symbol {
- join!(extern_fn(namespace, efn), var, 1)
+pub fn r_trampoline(efn: &ExternFn, var: &Ident, types: &Types) -> Symbol {
+ join!(extern_fn(efn, types), var, 1)
}