summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAidan Hobson Sayers <aidanhs@cantab.net>2015-01-08 01:12:27 +0000
committerAidan Hobson Sayers <aidanhs@cantab.net>2015-01-08 01:14:27 +0000
commitf03bd2bcead68f6cafdbedf1ea15a8eb68f31c35 (patch)
treea7be0718ec1cd8adb1a34c2871ea344633211b12
parent295d153b3f6fb21dafb098a04c9cbbe3b3acf53d (diff)
Fix unboxed closures
-rw-r--r--src/bin/bindgen.rs3
-rw-r--r--src/clang.rs31
-rw-r--r--src/clangll.rs2
-rw-r--r--src/gen.rs5
-rw-r--r--src/lib.rs4
-rw-r--r--src/parser.rs20
-rw-r--r--tests/support.rs4
-rw-r--r--tests/test_builtins.rs2
-rw-r--r--tests/test_cmath.rs6
-rw-r--r--tests/test_struct.rs2
-rw-r--r--tests/tests.rs8
11 files changed, 45 insertions, 42 deletions
diff --git a/src/bin/bindgen.rs b/src/bin/bindgen.rs
index 233c10a3..ea7ebc36 100644
--- a/src/bin/bindgen.rs
+++ b/src/bin/bindgen.rs
@@ -1,9 +1,8 @@
#![crate_name = "bindgen"]
#![crate_type = "bin"]
-#![feature(phase)]
extern crate bindgen;
-#[phase(plugin, link)] extern crate log;
+#[macro_use] extern crate log;
extern crate syntax;
use bindgen::{Bindings, BindgenOptions, LinkType, Logger};
diff --git a/src/clang.rs b/src/clang.rs
index 4636389b..a54755af 100644
--- a/src/clang.rs
+++ b/src/clang.rs
@@ -3,9 +3,11 @@
use libc::{c_uint, c_char, c_int, c_ulong};
use std::{mem, io, ptr};
use std::fmt;
+use std::str;
+use std::ffi;
use std::hash::Hash;
use std::hash::sip::SipState;
-use std::c_str::{CString, ToCStr};
+use std::ffi::CString;
pub use clangll as ll;
use clangll::*;
@@ -16,7 +18,7 @@ pub struct Cursor {
x: CXCursor
}
-pub type CursorVisitor<'s> = |c: &Cursor, p: &Cursor|: 's -> Enum_CXChildVisitResult;
+pub type CursorVisitor<'s> = FnMut<(&'s Cursor, &'s Cursor), Enum_CXChildVisitResult> + 's;
impl Cursor {
// common
@@ -56,9 +58,12 @@ impl Cursor {
}
}
- pub fn visit(&self, func: CursorVisitor) {
+ pub fn visit<'a, F>(&self, func: F)
+ where F: FnMut(&'a Cursor, &'a Cursor) -> Enum_CXChildVisitResult + 'a
+ {
unsafe {
- let data = mem::transmute::<&CursorVisitor, CXClientData>(&func);
+ let mut mfunc: Box<CursorVisitor> = box func;
+ let data = mem::transmute::<&mut Box<CursorVisitor>, CXClientData>(&mut mfunc);
let opt_visit = Some(visit_children as extern "C" fn(CXCursor, CXCursor, CXClientData) -> Enum_CXChildVisitResult);
clang_visitChildren(self.x, opt_visit, data);
};
@@ -131,8 +136,9 @@ impl Cursor {
extern fn visit_children(cur: CXCursor, parent: ll::CXCursor,
data: CXClientData) -> ll::Enum_CXChildVisitResult {
unsafe {
- let func = mem::transmute::<CXClientData, &mut CursorVisitor>(data);
- return (*func)(&Cursor { x: cur }, &Cursor { x: parent });
+ let func = mem::transmute::<CXClientData, &mut Box<CursorVisitor>>(data);
+ let (cur_a, cur_b) = (Cursor { x: cur }, Cursor { x: parent });
+ return (*func)(&cur_a, &cur_b);
}
}
@@ -321,7 +327,8 @@ impl fmt::Show for String_ {
}
unsafe {
let c_str = clang_getCString(self.x) as *const c_char;
- String::from_raw_buf(c_str as *const u8).fmt(f)
+ let p = c_str as *const _;
+ str::from_utf8(ffi::c_str_to_bytes(&p)).unwrap().to_string().fmt(f)
}
}
}
@@ -357,9 +364,9 @@ pub struct TranslationUnit {
impl TranslationUnit {
pub fn parse(ix: &Index, file: &str, cmd_args: &[String],
unsaved: &[UnsavedFile], opts: uint) -> TranslationUnit {
- let _fname = file.to_c_str();
+ let _fname = CString::from_slice(file.as_bytes());
let fname = _fname.as_ptr();
- let _c_args: Vec<CString> = cmd_args.iter().map(|s| s.to_c_str()).collect();
+ let _c_args: Vec<CString> = cmd_args.iter().map(|s| CString::from_slice(s.as_bytes())).collect();
let c_args: Vec<*const c_char> = _c_args.iter().map(|s| s.as_ptr()).collect();
let mut c_unsaved: Vec<Struct_CXUnsavedFile> = unsaved.iter().map(|f| f.x).collect();
let tu = unsafe {
@@ -452,8 +459,8 @@ pub struct UnsavedFile {
impl UnsavedFile {
pub fn new(name: &str, contents: &str) -> UnsavedFile {
- let name = name.to_c_str();
- let contents = contents.to_c_str();
+ let name = CString::from_slice(name.as_bytes());
+ let contents = CString::from_slice(contents.as_bytes());
let x = Struct_CXUnsavedFile {
Filename: name.as_ptr(),
Contents: contents.as_ptr(),
@@ -704,7 +711,7 @@ pub fn ast_dump(c: &Cursor, depth: int)-> Enum_CXVisitorResult {
c.spelling().as_slice(),
type_to_str(ct)).as_slice()
);
- c.visit(|s, _| {
+ c.visit(|&: s, _: &Cursor| {
ast_dump(s, depth + 1)
});
print_indent(depth, ")");
diff --git a/src/clangll.rs b/src/clangll.rs
index 2e9cb839..d0bf087e 100644
--- a/src/clangll.rs
+++ b/src/clangll.rs
@@ -5,7 +5,7 @@
#![allow(unused_attributes)]
#![allow(non_snake_case)]
#![allow(non_upper_case_globals)]
-#![allow(raw_pointer_deriving)]
+#![allow(raw_pointer_derive)]
pub type ptrdiff_t = ::libc::c_long;
pub type size_t = ::libc::c_ulong;
diff --git a/src/gen.rs b/src/gen.rs
index 6dfe080a..c1b095f9 100644
--- a/src/gen.rs
+++ b/src/gen.rs
@@ -116,7 +116,6 @@ fn enum_name(name: &String) -> String {
pub fn gen_mod(links: &[(String, LinkType)], globs: Vec<Global>, span: Span) -> Vec<P<ast::Item>> {
// Create a dummy ExtCtxt. We only need this for string interning and that uses TLS.
let cfg = ExpansionConfig {
- deriving_hash_type_parameter: false,
crate_name: "xxx".to_string(),
enable_quotes: true,
recursion_limit: 64,
@@ -542,6 +541,7 @@ fn cstruct_to_rs(ctx: &mut GenCtx, name: String, members: Vec<CompMember>) -> Ve
if !methods.is_empty() {
let impl_ = ast::ItemImpl(
ast::Unsafety::Normal,
+ ast::ImplPolarity::Positive,
empty_generics(),
None,
P(mk_ty(ctx, false, vec!(id))),
@@ -617,6 +617,7 @@ fn cunion_to_rs(ctx: &mut GenCtx, name: String, layout: Layout, members: Vec<Com
let union_impl = ast::ItemImpl(
ast::Unsafety::Normal,
+ ast::ImplPolarity::Positive,
empty_generics(),
None,
P(cty_to_rs(ctx, &union)),
@@ -678,7 +679,7 @@ fn gen_comp_methods(ctx: &mut GenCtx, data_field: &str, data_offset: uint,
extra: &mut Vec<P<ast::Item>>) -> Vec<ast::ImplItem> {
let data_ident = ctx.ext_cx.ident_of(data_field);
- let mk_field_method = |ctx: &mut GenCtx, f: &FieldInfo, offset: uint| {
+ let mk_field_method = |&: ctx: &mut GenCtx, f: &FieldInfo, offset: uint| {
// TODO: Implement bitfield accessors
if f.bitfields.is_some() { return None; }
diff --git a/src/lib.rs b/src/lib.rs
index 32f2f57e..34d75071 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1,11 +1,11 @@
#![crate_name = "bindgen"]
#![crate_type = "dylib"]
-#![feature(globs, quote, phase, plugin_registrar)]
+#![feature(quote, plugin_registrar, unboxed_closures)]
extern crate syntax;
extern crate rustc;
extern crate libc;
-#[phase(plugin, link)] extern crate log;
+#[macro_use] extern crate log;
use std::collections::HashSet;
use std::default::Default;
diff --git a/src/parser.rs b/src/parser.rs
index a68a4318..cb36e3b7 100644
--- a/src/parser.rs
+++ b/src/parser.rs
@@ -135,7 +135,7 @@ fn opaque_decl(ctx: &mut ClangParserCtx, decl: &Cursor) {
ctx.globals.push(name);
}
-fn fwd_decl(ctx: &mut ClangParserCtx, cursor: &Cursor, f: |ctx: &mut ClangParserCtx|) {
+fn fwd_decl<F:FnOnce(&mut ClangParserCtx)->()>(ctx: &mut ClangParserCtx, cursor: &Cursor, f: F) {
let def = &cursor.definition();
if cursor == def {
f(ctx);
@@ -207,7 +207,7 @@ fn mk_fn_sig(ctx: &mut ClangParserCtx, ty: &cx::Type, cursor: &Cursor) -> il::Fu
// 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, _| {
+ cursor.visit(|&mut: c: &Cursor, _: &Cursor| {
if c.kind() == CXCursor_ParmDecl {
args_lst.push((c.spelling(), conv_ty(ctx, &c.cur_type(), c)));
}
@@ -415,13 +415,13 @@ fn visit_composite(cursor: &Cursor, parent: &Cursor,
}
}
CXCursor_StructDecl | CXCursor_UnionDecl => {
- fwd_decl(ctx, cursor, |ctx_| {
+ fwd_decl(ctx, cursor, |: ctx_| {
// If the struct is anonymous (i.e. declared here) then it
// cannot be used elsewhere and so does not need to be added
// to globals otherwise it will be declared later and a global.
let decl = decl_name(ctx_, cursor);
let ci = decl.compinfo();
- cursor.visit(|c, p| {
+ cursor.visit(|&mut: c, p| {
let mut ci_ = ci.borrow_mut();
visit_composite(c, p, ctx_, &mut ci_.members)
});
@@ -461,10 +461,10 @@ fn visit_top<'r>(cursor: &Cursor,
match cursor.kind() {
CXCursor_StructDecl | CXCursor_UnionDecl => {
- fwd_decl(ctx, cursor, |ctx_| {
+ fwd_decl(ctx, cursor, |: ctx_| {
let decl = decl_name(ctx_, cursor);
let ci = decl.compinfo();
- cursor.visit(|c, p| {
+ cursor.visit(|&mut: c, p| {
let mut ci_ = ci.borrow_mut();
visit_composite(c, p, ctx_, &mut ci_.members)
});
@@ -473,10 +473,10 @@ fn visit_top<'r>(cursor: &Cursor,
return CXChildVisit_Continue;
}
CXCursor_EnumDecl => {
- fwd_decl(ctx, cursor, |ctx_| {
+ fwd_decl(ctx, cursor, |: ctx_| {
let decl = decl_name(ctx_, cursor);
let ei = decl.enuminfo();
- cursor.visit(|c, _| {
+ cursor.visit(|&mut: c, _: &Cursor| {
let mut ei_ = ei.borrow_mut();
visit_enum(c, &mut ei_.items)
});
@@ -585,10 +585,10 @@ pub fn parse(options: ClangParserOptions, logger: &Logger) -> Result<Vec<Global>
let cursor = unit.cursor();
if ctx.options.emit_ast {
- cursor.visit(|cur, _| ast_dump(cur, 0));
+ cursor.visit(|&mut: cur, _: &Cursor| ast_dump(cur, 0));
}
- cursor.visit(|cur, _| visit_top(cur, &mut ctx));
+ cursor.visit(|&mut: cur, _: &Cursor| visit_top(cur, &mut ctx));
while !ctx.builtin_defs.is_empty() {
let c = ctx.builtin_defs.remove(0);
diff --git a/tests/support.rs b/tests/support.rs
index d837786a..314d23f8 100644
--- a/tests/support.rs
+++ b/tests/support.rs
@@ -31,7 +31,9 @@ pub fn generate_bindings(filename: &str) -> Result<Vec<P<ast::Item>>, ()> {
Ok(try!(bindgen::Bindings::generate(&options, Some(&logger as &Logger), None)).into_ast())
}
-pub fn test_bind_eq(filename: &str, f:|ext_cx: DummyExtCtxt| -> Vec<Option<P<ast::Item>>>) {
+pub fn test_bind_eq<F>(filename: &str, f:F)
+ where F: Fn(DummyExtCtxt) -> Vec<Option<P<ast::Item>>>
+{
let ext_cx = mk_dummy_ext_ctxt();
let items = generate_bindings(filename).unwrap();
let quoted =f(ext_cx).into_iter().map(|x| x.unwrap()).collect();
diff --git a/tests/test_builtins.rs b/tests/test_builtins.rs
index 1df562c8..dccb63f7 100644
--- a/tests/test_builtins.rs
+++ b/tests/test_builtins.rs
@@ -1,6 +1,6 @@
#[test]
fn test_builtin_va_list() {
- #[allow(dead_code, non_camel_case_types, raw_pointer_deriving)]
+ #[allow(dead_code, non_camel_case_types, raw_pointer_derive)]
mod ffi { bindgen!("headers/builtin_va_list.h", emit_builtins = true); }
// Should test for more than compilation.
}
diff --git a/tests/test_cmath.rs b/tests/test_cmath.rs
index 73e2cef1..baa89dbb 100644
--- a/tests/test_cmath.rs
+++ b/tests/test_cmath.rs
@@ -1,8 +1,4 @@
-#[allow(dead_code)]
-#[allow(non_snake_case)]
-#[allow(non_camel_case_types)]
-#[allow(non_upper_case_globals)]
-#[allow(raw_pointer_deriving)]
+#[allow(dead_code, non_camel_case_types, non_upper_case_globals, raw_pointer_derive)]
pub mod ffi { bindgen!("/usr/include/math.h", link = "m"); }
#[test]
diff --git a/tests/test_struct.rs b/tests/test_struct.rs
index 68078a33..634c09d1 100644
--- a/tests/test_struct.rs
+++ b/tests/test_struct.rs
@@ -59,7 +59,7 @@ fn with_anon_struct_array() {
#[test]
fn with_anon_struct_pointer() {
- #[allow(raw_pointer_deriving)]
+ #[allow(raw_pointer_derive)]
mod ffi { bindgen!("headers/struct_with_anon_struct_pointer.h"); }
let mut x: ffi::Struct_foo = Default::default();
let mut unnamed: ffi::Struct_Unnamed1 = Default::default();
diff --git a/tests/tests.rs b/tests/tests.rs
index 6515f6de..04e39c45 100644
--- a/tests/tests.rs
+++ b/tests/tests.rs
@@ -1,16 +1,14 @@
-#![feature(globs)]
-#![feature(macro_rules)]
-#![feature(phase)]
#![feature(quote)]
+#![feature(plugin)]
-#[phase(plugin)]
+#[plugin] #[no_link]
extern crate bindgen;
extern crate bindgen;
extern crate libc;
extern crate syntax;
-#[macro_escape]
+#[macro_use]
mod support;
mod test_cmath;