-
Notifications
You must be signed in to change notification settings - Fork 339
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow function aliases #218
Changes from all commits
5c91efe
8f7891c
f70508b
2aceb7e
f81a2d7
fa9799b
6d63840
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ pub struct Parser<'a> { | |
pub doc: Option<&'a mut Doc>, | ||
pub derives: Option<&'a mut Vec<Derive>>, | ||
pub repr: Option<&'a mut Option<Atom>>, | ||
pub alias: Option<&'a mut Option<(Ident, bool)>>, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This needs a comment indicating what the bool means, or use an enum with two variants instead. |
||
} | ||
|
||
pub(super) fn parse_doc(cx: &mut Errors, attrs: &[Attribute]) -> Doc { | ||
|
@@ -57,11 +58,28 @@ pub(super) fn parse(cx: &mut Errors, attrs: &[Attribute], mut parser: Parser) { | |
} | ||
Err(err) => return cx.push(err), | ||
} | ||
} else if attr.path.is_ident("rust_name") || attr.path.is_ident("cxx_name") { | ||
match parse_function_alias_attribute.parse2(attr.tokens.clone()) { | ||
Ok(alias) => { | ||
if let Some(a) = &mut parser.alias { | ||
**a = Some((alias, attr.path.is_ident("rust_name"))); | ||
continue; | ||
} | ||
} | ||
Err(err) => return cx.push(err), | ||
} | ||
} else { | ||
return cx.error(attr, "unsupported attribute"); | ||
} | ||
return cx.error(attr, "unsupported attribute"); | ||
Comment on lines
+71
to
-61
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was correct before the PR. The change made in the PR makes well-formed attributes silently accepted anywhere where not expected, like putting a #[derive()] attribute on a function or a #[repr()] attribute on a function. |
||
} | ||
} | ||
|
||
fn parse_function_alias_attribute(input: ParseStream) -> Result<Ident> { | ||
input.parse::<Token![=]>()?; | ||
let lit: LitStr = input.parse()?; | ||
Ok(lit.parse::<Ident>()?) | ||
} | ||
|
||
fn parse_doc_attribute(input: ParseStream) -> Result<LitStr> { | ||
input.parse::<Token![=]>()?; | ||
let lit: LitStr = input.parse()?; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -360,6 +360,20 @@ fn parse_extern_fn(cx: &mut Errors, foreign_fn: &ForeignItemFn, lang: Lang) -> R | |
let unsafety = foreign_fn.sig.unsafety; | ||
let fn_token = foreign_fn.sig.fn_token; | ||
let ident = foreign_fn.sig.ident.clone(); | ||
let mut attr = None; | ||
attrs::parse(cx, &foreign_fn.attrs, attrs::Parser { alias: Some(&mut attr), ..Default::default() }); | ||
let mut alias = None; | ||
if let Some((ident, is_rust)) = attr { | ||
match lang { | ||
Lang::Cxx if !is_rust => { | ||
return Err(Error::new_spanned(foreign_fn, "C/C++ functions with 'cxx_name' are not allowed")); | ||
}, | ||
Lang::Rust if is_rust => { | ||
return Err(Error::new_spanned(foreign_fn, "Rust functions with 'rust_name' are not allowed")); | ||
}, | ||
Comment on lines
+368
to
+373
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is overly restrictive. This would make people need to remember which attribute is legal on which kind of function, and it's not needed -- any function should be able to have either attribute. We'll use the function's "real" name as the one that isn't specified by an attribute. |
||
_ => alias = Some(ident), | ||
} | ||
} | ||
let paren_token = foreign_fn.sig.paren_token; | ||
let semi_token = foreign_fn.semi_token; | ||
let api_function = match lang { | ||
|
@@ -371,6 +385,7 @@ fn parse_extern_fn(cx: &mut Errors, foreign_fn: &ForeignItemFn, lang: Lang) -> R | |
lang, | ||
doc, | ||
ident, | ||
alias, | ||
sig: Signature { | ||
unsafety, | ||
fn_token, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -116,7 +116,7 @@ impl<'a> Types<'a> { | |
rust.insert(ident); | ||
} | ||
Api::CxxFunction(efn) | Api::RustFunction(efn) => { | ||
let ident = &efn.ident; | ||
let ident = if let Some(alias) = &efn.alias { alias } else { &efn.ident }; | ||
if !function_names.insert((&efn.receiver, ident)) { | ||
duplicate_name(cx, efn, ident); | ||
} | ||
Comment on lines
+119
to
122
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As implemented in this PR, the
|
||
|
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -101,6 +101,15 @@ pub mod ffi { | |||
fn set2(&mut self, n: usize) -> usize; | ||||
fn set_succeed(&mut self, n: usize) -> Result<usize>; | ||||
fn get_fail(&mut self) -> Result<usize>; | ||||
|
||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove trailing whitespace. (Elsewhere as well)
Suggested change
|
||||
#[rust_name = "i32_overloaded_method"] | ||||
fn cOverloadedMethod(&self, x: i32) -> String; | ||||
#[rust_name = "str_overloaded_method"] | ||||
fn cOverloadedMethod(&self, x: &str) -> String; | ||||
#[rust_name = "i32_overloaded_function"] | ||||
fn cOverloadedFunction(x: i32) -> String; | ||||
#[rust_name = "str_overloaded_function"] | ||||
fn cOverloadedFunction(x: &str) -> String; | ||||
} | ||||
|
||||
extern "C" { | ||||
|
@@ -160,6 +169,9 @@ pub mod ffi { | |||
fn r_return_r2(n: usize) -> Box<R2>; | ||||
fn get(self: &R2) -> usize; | ||||
fn set(self: &mut R2, n: usize) -> usize; | ||||
|
||||
#[cxx_name = "rAliasedFunction"] | ||||
fn r_aliased_function(x: i32) -> String; | ||||
} | ||||
} | ||||
|
||||
|
@@ -358,3 +370,7 @@ fn r_fail_return_primitive() -> Result<usize, Error> { | |||
fn r_return_r2(n: usize) -> Box<R2> { | ||||
Box::new(R2(n)) | ||||
} | ||||
|
||||
fn r_aliased_function(x: i32) -> String { | ||||
x.to_string() | ||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't like this implementation of functions having an "ident" and an optional "alias", and especially counting on each use site to remember whether they should be checking for presence of the alias.
In my mind every function should have a non-optional rust name and non-optional c++ name. That's how we should store them post-parsing. Every place which uses of a function name will pick which of the two they want.