Skip to content

Commit

Permalink
Merge pull request #348 from dtolnay/alias
Browse files Browse the repository at this point in the history
Store independent rust name and c++ name for extern functions
  • Loading branch information
dtolnay authored Oct 10, 2020
2 parents f3a9afa + 1d673d8 commit e107c1b
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 29 deletions.
18 changes: 9 additions & 9 deletions gen/src/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ fn write_struct_with_methods(out: &mut OutFile, ety: &ExternType, methods: &[&Ex
for method in methods {
write!(out, " ");
let sig = &method.sig;
let local_name = method.ident.to_string();
let local_name = method.ident.cxx.to_string();
write_rust_function_shim_decl(out, &local_name, sig, false);
writeln!(out, ";");
}
Expand Down Expand Up @@ -517,8 +517,8 @@ fn write_cxx_function_shim(
write!(out, " ");
write_return_type(out, &efn.ret);
match &efn.receiver {
None => write!(out, "(*{}$)(", efn.ident),
Some(receiver) => write!(out, "({}::*{}$)(", receiver.ty, efn.ident),
None => write!(out, "(*{}$)(", efn.ident.rust),
Some(receiver) => write!(out, "({}::*{}$)(", receiver.ty, efn.ident.rust),
}
for (i, arg) in efn.args.iter().enumerate() {
if i > 0 {
Expand All @@ -534,8 +534,8 @@ fn write_cxx_function_shim(
}
write!(out, " = ");
match &efn.receiver {
None => write!(out, "{}", efn.ident),
Some(receiver) => write!(out, "&{}::{}", receiver.ty, efn.ident),
None => write!(out, "{}", efn.ident.cxx),
Some(receiver) => write!(out, "&{}::{}", receiver.ty, efn.ident.cxx),
}
writeln!(out, ";");
write!(out, " ");
Expand All @@ -562,8 +562,8 @@ fn write_cxx_function_shim(
_ => {}
}
match &efn.receiver {
None => write!(out, "{}$(", efn.ident),
Some(_) => write!(out, "(self.*{}$)(", efn.ident),
None => write!(out, "{}$(", efn.ident.rust),
Some(_) => write!(out, "(self.*{}$)(", efn.ident.rust),
}
for (i, arg) in efn.args.iter().enumerate() {
if i > 0 {
Expand Down Expand Up @@ -697,8 +697,8 @@ fn write_rust_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
writeln!(out, "//{}", line);
}
let local_name = match &efn.sig.receiver {
None => efn.ident.to_string(),
Some(receiver) => format!("{}::{}", receiver.ty, efn.ident),
None => efn.ident.cxx.to_string(),
Some(receiver) => format!("{}::{}", receiver.ty, efn.ident.cxx),
};
let invoke = mangle::extern_fn(&out.namespace, efn);
let indirect_call = false;
Expand Down
16 changes: 7 additions & 9 deletions macro/src/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,6 @@ fn expand_cxx_type(namespace: &Namespace, ety: &ExternType) -> TokenStream {
}

fn expand_cxx_function_decl(namespace: &Namespace, efn: &ExternFn, types: &Types) -> TokenStream {
let ident = &efn.ident;
let receiver = efn.receiver.iter().map(|receiver| {
let receiver_type = receiver.ty();
quote!(_: #receiver_type)
Expand Down Expand Up @@ -238,15 +237,14 @@ fn expand_cxx_function_decl(namespace: &Namespace, efn: &ExternFn, types: &Types
outparam = Some(quote!(__return: *mut #ret));
}
let link_name = mangle::extern_fn(namespace, efn);
let local_name = format_ident!("__{}", ident);
let local_name = format_ident!("__{}", efn.ident.rust);
quote! {
#[link_name = #link_name]
fn #local_name(#(#all_args,)* #outparam) #ret;
}
}

fn expand_cxx_function_shim(namespace: &Namespace, efn: &ExternFn, types: &Types) -> TokenStream {
let ident = &efn.ident;
let doc = &efn.doc;
let decl = expand_cxx_function_decl(namespace, efn, types);
let receiver = efn.receiver.iter().map(|receiver| {
Expand Down Expand Up @@ -329,7 +327,7 @@ fn expand_cxx_function_shim(namespace: &Namespace, efn: &ExternFn, types: &Types
}
})
.collect::<TokenStream>();
let local_name = format_ident!("__{}", ident);
let local_name = format_ident!("__{}", efn.ident.rust);
let call = if indirect_return {
let ret = expand_extern_type(efn.ret.as_ref().unwrap());
setup.extend(quote! {
Expand Down Expand Up @@ -426,6 +424,7 @@ fn expand_cxx_function_shim(namespace: &Namespace, efn: &ExternFn, types: &Types
if unsafety.is_none() {
dispatch = quote!(unsafe { #dispatch });
}
let ident = &efn.ident.rust;
let function_shim = quote! {
#doc
pub #unsafety fn #ident(#(#all_args,)*) #ret {
Expand Down Expand Up @@ -455,7 +454,7 @@ fn expand_function_pointer_trampoline(
let c_trampoline = mangle::c_trampoline(namespace, efn, var);
let r_trampoline = mangle::r_trampoline(namespace, efn, var);
let local_name = parse_quote!(__);
let catch_unwind_label = format!("::{}::{}", efn.ident, var);
let catch_unwind_label = format!("::{}::{}", efn.ident.rust, var);
let shim = expand_rust_function_shim_impl(
sig,
types,
Expand Down Expand Up @@ -510,11 +509,10 @@ fn expand_rust_type_assert_sized(ety: &ExternType) -> TokenStream {
}

fn expand_rust_function_shim(namespace: &Namespace, efn: &ExternFn, types: &Types) -> TokenStream {
let ident = &efn.ident;
let link_name = mangle::extern_fn(namespace, efn);
let local_name = format_ident!("__{}", ident);
let catch_unwind_label = format!("::{}", ident);
let invoke = Some(ident);
let local_name = format_ident!("__{}", efn.ident.rust);
let catch_unwind_label = format!("::{}", efn.ident.rust);
let invoke = Some(&efn.ident.rust);
expand_rust_function_shim_impl(
efn,
types,
Expand Down
4 changes: 2 additions & 2 deletions syntax/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,8 @@ fn check_api_type(cx: &mut Check, ety: &ExternType) {
if let Some(reason) = cx.types.required_trivial.get(&ety.ident) {
let what = match reason {
TrivialReason::StructField(strct) => format!("a field of `{}`", strct.ident),
TrivialReason::FunctionArgument(efn) => format!("an argument of `{}`", efn.ident),
TrivialReason::FunctionReturn(efn) => format!("a return value of `{}`", efn.ident),
TrivialReason::FunctionArgument(efn) => format!("an argument of `{}`", efn.ident.rust),
TrivialReason::FunctionReturn(efn) => format!("a return value of `{}`", efn.ident.rust),
};
let msg = format!(
"needs a cxx::ExternType impl in order to be used as {}",
Expand Down
2 changes: 1 addition & 1 deletion syntax/ident.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub(crate) fn check_all(cx: &mut Check, namespace: &Namespace, apis: &[Api]) {
check(cx, &ety.ident);
}
Api::CxxFunction(efn) | Api::RustFunction(efn) => {
check(cx, &efn.ident);
check(cx, &efn.ident.rust);
for arg in &efn.args {
check(cx, &arg.ident);
}
Expand Down
4 changes: 2 additions & 2 deletions syntax/mangle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ macro_rules! join {

pub fn extern_fn(namespace: &Namespace, efn: &ExternFn) -> Symbol {
match &efn.receiver {
Some(receiver) => join!(namespace, CXXBRIDGE, receiver.ty, efn.ident),
None => join!(namespace, CXXBRIDGE, efn.ident),
Some(receiver) => join!(namespace, CXXBRIDGE, receiver.ty, efn.ident.rust),
None => join!(namespace, CXXBRIDGE, efn.ident.rust),
}
}

Expand Down
7 changes: 6 additions & 1 deletion syntax/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,15 @@ pub struct Enum {
pub repr: Atom,
}

pub struct Pair {
pub cxx: Ident,
pub rust: Ident,
}

pub struct ExternFn {
pub lang: Lang,
pub doc: Doc,
pub ident: Ident,
pub ident: Pair,
pub sig: Signature,
pub semi_token: Token![;],
}
Expand Down
7 changes: 5 additions & 2 deletions syntax/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::syntax::file::{Item, ItemForeignMod};
use crate::syntax::report::Errors;
use crate::syntax::Atom::*;
use crate::syntax::{
attrs, error, Api, Doc, Enum, ExternFn, ExternType, Impl, Lang, Receiver, Ref, Signature,
attrs, error, Api, Doc, Enum, ExternFn, ExternType, Impl, Lang, Pair, Receiver, Ref, Signature,
Slice, Struct, Ty1, Type, TypeAlias, Var, Variant,
};
use proc_macro2::{Delimiter, Group, TokenStream, TokenTree};
Expand Down Expand Up @@ -370,7 +370,10 @@ fn parse_extern_fn(cx: &mut Errors, foreign_fn: &ForeignItemFn, lang: Lang) -> R
Ok(api_function(ExternFn {
lang,
doc,
ident,
ident: Pair {
cxx: ident.clone(),
rust: ident,
},
sig: Signature {
unsafety,
fn_token,
Expand Down
7 changes: 4 additions & 3 deletions syntax/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,10 @@ impl<'a> Types<'a> {
rust.insert(ident);
}
Api::CxxFunction(efn) | Api::RustFunction(efn) => {
let ident = &efn.ident;
if !function_names.insert((&efn.receiver, ident)) {
duplicate_name(cx, efn, ident);
// Note: duplication of the C++ name is fine because C++ has
// function overloading.
if !function_names.insert((&efn.receiver, &efn.ident.rust)) {
duplicate_name(cx, efn, &efn.ident.rust);
}
for arg in &efn.args {
visit(&mut all, &arg.ty);
Expand Down

0 comments on commit e107c1b

Please sign in to comment.