-
Notifications
You must be signed in to change notification settings - Fork 758
Add an alternate path determining the arguments for a function. #1508
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
Changes from all commits
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 |
---|---|---|
|
@@ -309,6 +309,31 @@ pub fn cursor_mangling( | |
Some(mangling) | ||
} | ||
|
||
fn args_from_ty_and_cursor( | ||
ty: &clang::Type, | ||
cursor: &clang::Cursor, | ||
ctx: &mut BindgenContext, | ||
) -> Vec<(Option<String>, TypeId)> { | ||
match (cursor.args(), ty.args()) { | ||
(Some(cursor_args), Some(ty_args)) => { | ||
ty_args.iter().enumerate().map(|(i, ty)| { | ||
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. So, what about the following instead, which could be much shorter:
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 tried to rewrite this a few different ways but found everything unsatisfactory. Cursor::args() and Type::args() return different types and I haven't figured out a good way in Rust to work with iterators of different concrete types well. Definitely open to suggestions! 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. Meh, this doesn't look too bad as-is. Thanks again for looking into this! |
||
let name = cursor_args.get(i) | ||
.map(|c| c.spelling()) | ||
.and_then(|name| if name.is_empty() { None } else { Some(name) }); | ||
(name, Item::from_ty_or_ref(*ty, *cursor, None, ctx)) | ||
}).collect() | ||
} | ||
(Some(cursor_args), None) => { | ||
cursor_args.iter().map(|cursor| { | ||
let name = cursor.spelling(); | ||
let name = if name.is_empty() { None } else { Some(name) }; | ||
(name, Item::from_ty_or_ref(cursor.cur_type(), *cursor, None, ctx)) | ||
}).collect() | ||
} | ||
_ => panic!() | ||
} | ||
} | ||
|
||
impl FunctionSig { | ||
/// Construct a new function signature. | ||
pub fn new( | ||
|
@@ -363,28 +388,14 @@ impl FunctionSig { | |
ty.declaration() | ||
}; | ||
|
||
let mut args: Vec<_> = match kind { | ||
let mut args = match kind { | ||
CXCursor_FunctionDecl | | ||
CXCursor_Constructor | | ||
CXCursor_CXXMethod | | ||
CXCursor_ObjCInstanceMethodDecl | | ||
CXCursor_ObjCClassMethodDecl => { | ||
// For CXCursor_FunctionDecl, cursor.args() is the reliable way | ||
// to get parameter names and types. | ||
cursor | ||
.args() | ||
.unwrap() | ||
.iter() | ||
.map(|arg| { | ||
let arg_ty = arg.cur_type(); | ||
let name = arg.spelling(); | ||
let name = | ||
if name.is_empty() { None } else { Some(name) }; | ||
let ty = Item::from_ty_or_ref(arg_ty, *arg, None, ctx); | ||
(name, ty) | ||
}) | ||
.collect() | ||
} | ||
args_from_ty_and_cursor(&ty, &cursor, ctx) | ||
}, | ||
_ => { | ||
// For non-CXCursor_FunctionDecl, visiting the cursor's children | ||
// is the only reliable way to get parameter names. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/* automatically generated by rust-bindgen */ | ||
|
||
#![allow( | ||
dead_code, | ||
non_snake_case, | ||
non_camel_case_types, | ||
non_upper_case_globals | ||
)] | ||
#![cfg(target_os = "macos")] | ||
|
||
extern crate block; | ||
extern "C" { | ||
pub fn func() -> _bindgen_ty_id_4; | ||
} | ||
pub type _bindgen_ty_id_4 = | ||
*const ::block::Block<(::std::os::raw::c_int, ::std::os::raw::c_int), ::std::os::raw::c_int>; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/* automatically generated by rust-bindgen */ | ||
|
||
#![allow( | ||
dead_code, | ||
non_snake_case, | ||
non_camel_case_types, | ||
non_upper_case_globals | ||
)] | ||
|
||
extern "C" { | ||
pub fn func() -> ::std::option::Option< | ||
unsafe extern "C" fn( | ||
arg1: ::std::os::raw::c_int, | ||
arg2: ::std::os::raw::c_int, | ||
) -> ::std::os::raw::c_int, | ||
>; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// bindgen-flags: --generate-block --block-extern-crate -- -fblocks | ||
// bindgen-osx-only | ||
|
||
int (^func(void))(int, int); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
int (*func(void))(int, int); |
Uh oh!
There was an error while loading. Please reload this page.