blob: c9392dba1fdcbde4b1299269e6260ebe44b744ae [file] [log] [blame]
David Tolnay3caa50a2020-04-19 21:25:34 -07001use crate::syntax::namespace::Namespace;
David Tolnay891061b2020-04-19 22:42:33 -07002use crate::syntax::symbol::{self, Symbol};
3use crate::syntax::ExternFn;
4use proc_macro2::Ident;
David Tolnay5ea922a2020-04-19 21:58:06 -07005
David Tolnay69601622020-04-29 18:48:36 -07006const CXXBRIDGE: &str = "cxxbridge03";
David Tolnay5ea922a2020-04-19 21:58:06 -07007
8macro_rules! join {
9 ($($segment:expr),*) => {
10 symbol::join(&[$(&$segment),*])
11 };
12}
David Tolnay3caa50a2020-04-19 21:25:34 -070013
David Tolnay891061b2020-04-19 22:42:33 -070014pub fn extern_fn(namespace: &Namespace, efn: &ExternFn) -> Symbol {
David Tolnay5ea922a2020-04-19 21:58:06 -070015 match &efn.receiver {
David Tolnay05e11cc2020-04-20 02:13:56 -070016 Some(receiver) => join!(namespace, CXXBRIDGE, receiver.ty, efn.ident),
David Tolnay5ea922a2020-04-19 21:58:06 -070017 None => join!(namespace, CXXBRIDGE, efn.ident),
18 }
David Tolnay891061b2020-04-19 22:42:33 -070019}
20
21// The C half of a function pointer trampoline.
22pub fn c_trampoline(namespace: &Namespace, efn: &ExternFn, var: &Ident) -> Symbol {
23 join!(extern_fn(namespace, efn), var, 0)
24}
25
26// The Rust half of a function pointer trampoline.
27pub fn r_trampoline(namespace: &Namespace, efn: &ExternFn, var: &Ident) -> Symbol {
28 join!(extern_fn(namespace, efn), var, 1)
David Tolnay3caa50a2020-04-19 21:25:34 -070029}