summaryrefslogtreecommitdiff
path: root/src/parser.rs
diff options
context:
space:
mode:
authorJyun-Yan You <jyyou.tw@gmail.com>2015-01-01 00:05:49 +0800
committerJyun-Yan You <jyyou.tw@gmail.com>2015-01-01 00:05:49 +0800
commit4482a25cb4657cf1ce280999db37b7beb843f6bc (patch)
tree0ce2ea417532f552503b764d420a57e224c96918 /src/parser.rs
parente81f8e2420b7aff83207ec18d866c3af096da016 (diff)
parent97945161d298bb89f7f1ea015c5cc54acc9e7c43 (diff)
Merge branch 'func_proto' of https://github.com/chris-chambers/rust-bindgen
Conflicts: src/parser.rs tests/forward_declared_struct.rs tests/test_struct.rs
Diffstat (limited to 'src/parser.rs')
-rw-r--r--src/parser.rs72
1 files changed, 46 insertions, 26 deletions
diff --git a/src/parser.rs b/src/parser.rs
index c4291d5e..2622a944 100644
--- a/src/parser.rs
+++ b/src/parser.rs
@@ -168,22 +168,14 @@ fn conv_ptr_ty(ctx: &mut ClangParserCtx, ty: &cx::Type, cursor: &Cursor, layout:
let ret_ty = ty.ret_type();
let decl = ty.declaration();
return if ret_ty.kind() != CXType_Invalid {
- let mut args_lst = vec!();
- cursor.visit(|c, _| {
- if c.kind() == CXCursor_ParmDecl {
- args_lst.push((c.spelling(), conv_ty(ctx, &c.cur_type(), c)));
- }
- CXChildVisit_Continue
- });
-
- let ret_ty = box conv_ty(ctx, &ret_ty, cursor);
- let abi = get_abi(ty.call_conv());
-
- TFunc(ret_ty, args_lst, ty.is_variadic(), abi)
+ TFuncPtr(mk_fn_sig(ctx, ty, cursor))
} else if decl.kind() != CXCursor_NoDeclFound {
- TPtr(box conv_decl_ty(ctx, &decl), is_const, layout)
+ TPtr(box conv_decl_ty(ctx, &decl), ty.is_const(), layout)
+ } else if cursor.kind() == CXCursor_VarDecl {
+ let can_ty = ty.canonical_type();
+ conv_ty(ctx, &can_ty, cursor)
} else {
- TPtr(box TVoid, is_const, layout)
+ TPtr(box TVoid, ty.is_const(), layout)
};
}
CXType_Typedef => {
@@ -200,6 +192,41 @@ fn conv_ptr_ty(ctx: &mut ClangParserCtx, ty: &cx::Type, cursor: &Cursor, layout:
}
}
+fn mk_fn_sig(ctx: &mut ClangParserCtx, ty: &cx::Type, cursor: &Cursor) -> il::FuncSig {
+ let args_lst: Vec<(String, il::Type)> = match cursor.kind() {
+ CXCursor_FunctionDecl => {
+ // For CXCursor_FunctionDecl, cursor.args() is the reliable way to
+ // get parameter names and types.
+ cursor.args().iter().map(|arg| {
+ let arg_name = arg.spelling();
+ (arg_name, conv_ty(ctx, &arg.cur_type(), arg))
+ }).collect()
+ }
+ _ => {
+ // For non-CXCursor_FunctionDecl, visiting the cursor's children is
+ // the only reliable way to get parameter names.
+ let mut args_lst = vec!();
+ cursor.visit(|c, _| {
+ if c.kind() == CXCursor_ParmDecl {
+ args_lst.push((c.spelling(), conv_ty(ctx, &c.cur_type(), c)));
+ }
+ CXChildVisit_Continue
+ });
+ args_lst
+ }
+ };
+
+ let ret_ty = box conv_ty(ctx, &ty.ret_type(), cursor);
+ let abi = get_abi(ty.call_conv());
+
+ il::FuncSig {
+ ret_ty: ret_ty,
+ args: args_lst,
+ is_variadic: ty.is_variadic(),
+ abi: abi,
+ }
+}
+
fn conv_decl_ty(ctx: &mut ClangParserCtx, cursor: &Cursor) -> il::Type {
return match cursor.kind() {
CXCursor_StructDecl => {
@@ -227,7 +254,7 @@ fn conv_decl_ty(ctx: &mut ClangParserCtx, cursor: &Cursor) -> il::Type {
}
fn conv_ty(ctx: &mut ClangParserCtx, ty: &cx::Type, cursor: &Cursor) -> il::Type {
- debug!("conv_ty: ty=`{}`", type_to_str(ty.kind()));
+ debug!("conv_ty: ty=`{}` sp=`{}` loc=`{}`", type_to_str(ty.kind()), cursor.spelling(), cursor.location());
let layout = Layout::new(ty.size(), ty.align());
return match ty.kind() {
CXType_Void | CXType_Invalid => TVoid,
@@ -251,6 +278,7 @@ fn conv_ty(ctx: &mut ClangParserCtx, ty: &cx::Type, cursor: &Cursor) -> il::Type
CXType_VariableArray | CXType_DependentSizedArray | CXType_IncompleteArray => {
conv_ptr_ty(ctx, &ty.elem_type(), cursor, layout)
}
+ CXType_FunctionProto => TFuncProto(mk_fn_sig(ctx, ty, cursor)),
CXType_Record |
CXType_Typedef |
CXType_Unexposed |
@@ -349,7 +377,7 @@ fn visit_composite(cursor: &Cursor, parent: &Cursor,
if let Some(CompMember::Comp(c)) = members.pop() {
members.push(CompMember::CompField(c, field));
} else {
- panic!(); // Checks in is_composite make this unreachable.
+ unreachable!(); // Checks in is_composite make this unreachable.
}
} else {
members.push(CompMember::Field(field));
@@ -431,19 +459,11 @@ fn visit_top<'r>(cursor: &Cursor,
return CXChildVisit_Continue;
}
- let args_lst: Vec<(String, il::Type)> = cursor.args().iter().map(|arg| {
- let arg_name = arg.spelling();
- (arg_name, conv_ty(ctx, &arg.cur_type(), cursor))
- }).collect();
-
- let ty = cursor.cur_type();
- let ret_ty = box conv_ty(ctx, &cursor.ret_type(), cursor);
- let abi = get_abi(ty.call_conv());
-
let func = decl_name(ctx, cursor);
let vi = func.varinfo();
let mut vi = vi.borrow_mut();
- vi.ty = TFunc(ret_ty.clone(), args_lst.clone(), ty.is_variadic(), abi);
+
+ vi.ty = TFuncPtr(mk_fn_sig(ctx, &cursor.cur_type(), cursor));
ctx.globals.push(func);
return CXChildVisit_Continue;