diff options
author | bors-servo <lbergstrom+bors@mozilla.com> | 2016-11-03 04:55:01 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-11-03 04:55:01 -0500 |
commit | ff12c0cb988ad8bb6290e638423ad9cde6bdef8f (patch) | |
tree | 0a0d336233e3be87317f29a657bb6280ff962f51 | |
parent | 4e4bf459240125816fc684219e4843f2071bcf2a (diff) | |
parent | 3fa7228c192e468d0730888aaa37acc02bb28d82 (diff) |
Auto merge of #196 - emilio:visitor, r=fitzgen
clang: Simplify visiting code.
It has always been a mess.
r? @fitzgen
-rwxr-xr-x | src/clang.rs | 52 | ||||
-rw-r--r-- | src/ir/comp.rs | 18 | ||||
-rw-r--r-- | src/ir/context.rs | 4 | ||||
-rw-r--r-- | src/ir/enum_ty.rs | 2 | ||||
-rw-r--r-- | src/ir/function.rs | 4 | ||||
-rw-r--r-- | src/ir/item.rs | 4 | ||||
-rw-r--r-- | src/ir/module.rs | 4 | ||||
-rw-r--r-- | src/ir/ty.rs | 6 | ||||
-rw-r--r-- | src/ir/var.rs | 2 | ||||
-rwxr-xr-x | src/lib.rs | 6 |
10 files changed, 44 insertions, 58 deletions
diff --git a/src/clang.rs b/src/clang.rs index 7f81c379..57c42eaf 100755 --- a/src/clang.rs +++ b/src/clang.rs @@ -31,14 +31,6 @@ impl fmt::Debug for Cursor { } } -/// A cursor visitor function. -/// -/// The first argument is the AST node currently being visited. The second -/// argument is the parent of the AST node currently being visited. The return -/// value informs how traversal should proceed. -pub type CursorVisitor<'s> = for<'a, 'b> FnMut(&'a Cursor, &'b Cursor) - -> Enum_CXChildVisitResult + 's; - impl Cursor { /// Get the Unified Symbol Resolution for this cursor's referent, if /// available. @@ -306,21 +298,14 @@ impl Cursor { /// Traverse this cursor's referent and its children. /// - /// Call the given function on each AST node traversed. See `CursorVisitor` - /// for details on arguments passed to the function and how its return value - /// is interpreted. - pub fn visit<F>(&self, func: F) - where F: for<'a, 'b> FnMut(&'a Cursor, &'b Cursor) - -> Enum_CXChildVisitResult, + /// Call the given function on each AST node traversed. + pub fn visit<Visitor>(&self, mut visitor: Visitor) + where Visitor: FnMut(Cursor) -> Enum_CXChildVisitResult, { - let mut data: Box<CursorVisitor> = Box::new(func); - let opt_visit = - Some(visit_children as extern "C" fn(CXCursor, - CXCursor, - CXClientData) - -> Enum_CXChildVisitResult); unsafe { - clang_visitChildren(self.x, opt_visit, mem::transmute(&mut data)); + clang_visitChildren(self.x, + Some(visit_children::<Visitor>), + mem::transmute(&mut visitor)); } } @@ -484,17 +469,18 @@ impl Cursor { } } -extern "C" fn visit_children(cur: CXCursor, - parent: CXCursor, - data: CXClientData) - -> Enum_CXChildVisitResult { - let func: &mut Box<CursorVisitor> = unsafe { mem::transmute(data) }; - (*func)(&Cursor { - x: cur, - }, - &Cursor { - x: parent, - }) +extern "C" fn visit_children<Visitor>(cur: CXCursor, + _parent: CXCursor, + data: CXClientData) + -> Enum_CXChildVisitResult + where Visitor: FnMut(Cursor) -> Enum_CXChildVisitResult, +{ + let func: &mut Visitor = unsafe { mem::transmute(data) }; + let child = Cursor { + x: cur, + }; + + (*func)(child) } impl PartialEq for Cursor { @@ -1184,7 +1170,7 @@ pub fn ast_dump(c: &Cursor, depth: isize) -> Enum_CXVisitorResult { kind_to_str(c.kind()), c.spelling(), type_to_str(ct))); - c.visit(|s, _: &Cursor| ast_dump(s, depth + 1)); + c.visit(|s| ast_dump(&s, depth + 1)); print_indent(depth, ")"); CXChildVisit_Continue } diff --git a/src/ir/comp.rs b/src/ir/comp.rs index d55c24ca..22082772 100644 --- a/src/ir/comp.rs +++ b/src/ir/comp.rs @@ -515,7 +515,7 @@ impl CompInfo { ci.ref_template = Item::parse(cursor.specialized(), None, ctx).ok(); let mut maybe_anonymous_struct_field = None; - cursor.visit(|cur, _other| { + cursor.visit(|cur| { if cur.kind() != CXCursor_FieldDecl { if let Some((ty, _)) = maybe_anonymous_struct_field { let field = Field::new(None, ty, None, None, None, false); @@ -529,7 +529,7 @@ impl CompInfo { match maybe_anonymous_struct_field.take() { Some((ty, clang_ty)) => { let mut used = false; - cur.visit(|child, _| { + cur.visit(|child| { if child.cur_type() == clang_ty { used = true; } @@ -550,12 +550,12 @@ impl CompInfo { let bit_width = cur.bit_width(); let field_type = Item::from_ty_or_ref(cur.cur_type(), - Some(*cur), + Some(cur), Some(potential_id), ctx); let comment = cur.raw_comment(); - let annotations = Annotations::new(cur); + let annotations = Annotations::new(&cur); let name = cur.spelling(); let is_mutable = cursor.is_mutable_field(); @@ -575,7 +575,7 @@ impl CompInfo { ci.fields.push(field); // No we look for things like attributes and stuff. - cur.visit(|cur, _| { + cur.visit(|cur| { if cur.kind() == CXCursor_UnexposedAttr { ci.found_unknown_attr = true; } @@ -593,7 +593,7 @@ impl CompInfo { CXCursor_UnionDecl | CXCursor_ClassTemplate | CXCursor_ClassDecl => { - let inner = Item::parse(*cur, Some(potential_id), ctx) + let inner = Item::parse(cur, Some(potential_id), ctx) .expect("Inner ClassDecl"); if !ci.inner_types.contains(&inner) { ci.inner_types.push(inner); @@ -619,7 +619,7 @@ impl CompInfo { } let default_type = Item::from_ty(&cur.cur_type(), - Some(*cur), + Some(cur), Some(potential_id), ctx) .ok(); @@ -687,7 +687,7 @@ impl CompInfo { // NB: This gets us an owned `Function`, not a // `FunctionSig`. let method_signature = - Item::parse(*cur, Some(potential_id), ctx) + Item::parse(cur, Some(potential_id), ctx) .expect("CXXMethod"); let is_const = cur.method_is_const(); @@ -726,7 +726,7 @@ impl CompInfo { return CXChildVisit_Continue; } - let item = Item::parse(*cur, Some(potential_id), ctx) + let item = Item::parse(cur, Some(potential_id), ctx) .expect("VarDecl"); ci.inner_vars.push(item); } diff --git a/src/ir/context.rs b/src/ir/context.rs index beccc514..c2214340 100644 --- a/src/ir/context.rs +++ b/src/ir/context.rs @@ -492,14 +492,14 @@ impl<'ctx> BindgenContext<'ctx> { use clangll::*; let mut args = vec![]; let mut found_invalid_template_ref = false; - location.visit(|c, _| { + location.visit(|c| { if c.kind() == CXCursor_TemplateRef && c.cur_type().kind() == CXType_Invalid { found_invalid_template_ref = true; } if c.kind() == CXCursor_TypeRef { let new_ty = Item::from_ty_or_ref(c.cur_type(), - Some(*c), + Some(c), Some(with_id), self); args.push(new_ty); diff --git a/src/ir/enum_ty.rs b/src/ir/enum_ty.rs index fd7dbb45..e78184e7 100644 --- a/src/ir/enum_ty.rs +++ b/src/ir/enum_ty.rs @@ -70,7 +70,7 @@ impl Enum { None => true, }; - declaration.visit(|cursor, _| { + declaration.visit(|cursor| { if cursor.kind() == CXCursor_EnumConstantDecl { let name = cursor.spelling(); let comment = cursor.raw_comment(); diff --git a/src/ir/function.rs b/src/ir/function.rs index c2e6ffd0..163e3039 100644 --- a/src/ir/function.rs +++ b/src/ir/function.rs @@ -167,10 +167,10 @@ impl FunctionSig { // For non-CXCursor_FunctionDecl, visiting the cursor's children // is the only reliable way to get parameter names. let mut args = vec![]; - cursor.visit(|c, _| { + cursor.visit(|c| { if c.kind() == CXCursor_ParmDecl { let ty = - Item::from_ty(&c.cur_type(), Some(*c), None, ctx) + Item::from_ty(&c.cur_type(), Some(c), None, ctx) .expect("ParmDecl?"); let name = c.spelling(); let name = diff --git a/src/ir/item.rs b/src/ir/item.rs index 690f4222..0c12b9a9 100644 --- a/src/ir/item.rs +++ b/src/ir/item.rs @@ -1016,11 +1016,11 @@ impl ClangItemParser for Item { assert_eq!(popped_decl, declaration_to_look_for); } - location.visit(|cur, _other| { + location.visit(|cur| { use clangll::*; result = Item::from_ty_with_id(id, ty, - Some(*cur), + Some(cur), parent_id, ctx); match result { diff --git a/src/ir/module.rs b/src/ir/module.rs index c582d3eb..42175b92 100644 --- a/src/ir/module.rs +++ b/src/ir/module.rs @@ -49,8 +49,8 @@ impl ClangSubItemParser for Module { CXCursor_Namespace => { let module_id = ctx.module(cursor); ctx.with_module(module_id, |ctx, children| { - cursor.visit(|cursor, _| { - parse_one(ctx, *cursor, Some(module_id), children) + cursor.visit(|cursor| { + parse_one(ctx, cursor, Some(module_id), children) }) }); diff --git a/src/ir/ty.rs b/src/ir/ty.rs index 4d26cdff..81212029 100644 --- a/src/ir/ty.rs +++ b/src/ir/ty.rs @@ -547,14 +547,14 @@ impl Type { let mut inner = Err(ParseError::Continue); let mut args = vec![]; - location.visit(|cur, _| { + location.visit(|cur| { match cur.kind() { CXCursor_TypeAliasDecl => { debug_assert!(cur.cur_type().kind() == CXType_Typedef); inner = Item::from_ty(&cur.cur_type(), - Some(*cur), + Some(cur), Some(potential_id), ctx); } @@ -567,7 +567,7 @@ impl Type { let default_type = Item::from_ty(&cur.cur_type(), - Some(*cur), + Some(cur), Some(potential_id), ctx) .ok(); diff --git a/src/ir/var.rs b/src/ir/var.rs index 216d8185..33e56242 100644 --- a/src/ir/var.rs +++ b/src/ir/var.rs @@ -203,7 +203,7 @@ fn get_integer_literal_from_cursor(cursor: &clang::Cursor, -> Option<i64> { use clangll::*; let mut value = None; - cursor.visit(|c, _| { + cursor.visit(|c| { match c.kind() { CXCursor_IntegerLiteral | CXCursor_UnaryOperator => { @@ -461,7 +461,7 @@ pub fn parse_one(ctx: &mut BindgenContext, Ok(id) => children.push(id), Err(ParseError::Continue) => {} Err(ParseError::Recurse) => { - cursor.visit(|child, _| parse_one(ctx, *child, parent, children)); + cursor.visit(|child| parse_one(ctx, child, parent, children)); } } CXChildVisit_Continue @@ -480,12 +480,12 @@ fn parse(context: &mut BindgenContext) { let cursor = context.translation_unit().cursor(); if context.options().emit_ast { - cursor.visit(|cur, _| clang::ast_dump(cur, 0)); + cursor.visit(|cur| clang::ast_dump(&cur, 0)); } let root = context.root_module(); context.with_module(root, |context, children| { - cursor.visit(|cursor, _| parse_one(context, *cursor, None, children)) + cursor.visit(|cursor| parse_one(context, cursor, None, children)) }); assert!(context.current_module() == context.root_module(), |