Skip to content

Commit e48d4ed

Browse files
committed
clang: Simplify visiting code.
It has always been a mess.
1 parent 6b30385 commit e48d4ed

File tree

10 files changed

+43
-55
lines changed

10 files changed

+43
-55
lines changed

src/clang.rs

+18-30
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,6 @@ impl fmt::Debug for Cursor {
3131
}
3232
}
3333

34-
/// A cursor visitor function.
35-
///
36-
/// The first argument is the AST node currently being visited. The second
37-
/// argument is the parent of the AST node currently being visited. The return
38-
/// value informs how traversal should proceed.
39-
pub type CursorVisitor<'s> = for<'a, 'b> FnMut(&'a Cursor, &'b Cursor)
40-
-> Enum_CXChildVisitResult + 's;
41-
4234
impl Cursor {
4335
/// Get the Unified Symbol Resolution for this cursor's referent, if
4436
/// available.
@@ -306,18 +298,13 @@ impl Cursor {
306298
/// Call the given function on each AST node traversed. See `CursorVisitor`
307299
/// for details on arguments passed to the function and how its return value
308300
/// is interpreted.
309-
pub fn visit<F>(&self, func: F)
310-
where F: for<'a, 'b> FnMut(&'a Cursor, &'b Cursor)
311-
-> Enum_CXChildVisitResult,
301+
pub fn visit<Visitor>(&self, mut visitor: Visitor)
302+
where Visitor: FnMut(Cursor) -> Enum_CXChildVisitResult,
312303
{
313-
let mut data: Box<CursorVisitor> = Box::new(func);
314-
let opt_visit =
315-
Some(visit_children as extern "C" fn(CXCursor,
316-
CXCursor,
317-
CXClientData)
318-
-> Enum_CXChildVisitResult);
319304
unsafe {
320-
clang_visitChildren(self.x, opt_visit, mem::transmute(&mut data));
305+
clang_visitChildren(self.x,
306+
Some(visit_children::<Visitor>),
307+
mem::transmute(&mut visitor));
321308
}
322309
}
323310

@@ -481,17 +468,18 @@ impl Cursor {
481468
}
482469
}
483470

484-
extern "C" fn visit_children(cur: CXCursor,
485-
parent: CXCursor,
486-
data: CXClientData)
487-
-> Enum_CXChildVisitResult {
488-
let func: &mut Box<CursorVisitor> = unsafe { mem::transmute(data) };
489-
(*func)(&Cursor {
490-
x: cur,
491-
},
492-
&Cursor {
493-
x: parent,
494-
})
471+
extern "C" fn visit_children<Visitor>(cur: CXCursor,
472+
_parent: CXCursor,
473+
data: CXClientData)
474+
-> Enum_CXChildVisitResult
475+
where Visitor: FnMut(Cursor) -> Enum_CXChildVisitResult,
476+
{
477+
let func: &mut Visitor = unsafe { mem::transmute(data) };
478+
let child = Cursor {
479+
x: cur,
480+
};
481+
482+
(*func)(child)
495483
}
496484

497485
impl PartialEq for Cursor {
@@ -1407,7 +1395,7 @@ pub fn ast_dump(c: &Cursor, depth: isize) -> Enum_CXVisitorResult {
14071395
kind_to_str(c.kind()),
14081396
c.spelling(),
14091397
type_to_str(ct)));
1410-
c.visit(|s, _: &Cursor| ast_dump(s, depth + 1));
1398+
c.visit(|s| ast_dump(&s, depth + 1));
14111399
print_indent(depth, ")");
14121400
CXChildVisit_Continue
14131401
}

src/ir/comp.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,7 @@ impl CompInfo {
515515
ci.ref_template = Item::parse(cursor.specialized(), None, ctx).ok();
516516

517517
let mut maybe_anonymous_struct_field = None;
518-
cursor.visit(|cur, _other| {
518+
cursor.visit(|cur| {
519519
if cur.kind() != CXCursor_FieldDecl {
520520
if let Some((ty, _)) = maybe_anonymous_struct_field {
521521
let field = Field::new(None, ty, None, None, None, false);
@@ -529,7 +529,7 @@ impl CompInfo {
529529
match maybe_anonymous_struct_field.take() {
530530
Some((ty, clang_ty)) => {
531531
let mut used = false;
532-
cur.visit(|child, _| {
532+
cur.visit(|child| {
533533
if child.cur_type() == clang_ty {
534534
used = true;
535535
}
@@ -550,12 +550,12 @@ impl CompInfo {
550550

551551
let bit_width = cur.bit_width();
552552
let field_type = Item::from_ty_or_ref(cur.cur_type(),
553-
Some(*cur),
553+
Some(cur),
554554
Some(potential_id),
555555
ctx);
556556

557557
let comment = cur.raw_comment();
558-
let annotations = Annotations::new(cur);
558+
let annotations = Annotations::new(&cur);
559559
let name = cur.spelling();
560560
let is_mutable = cursor.is_mutable_field();
561561

@@ -575,7 +575,7 @@ impl CompInfo {
575575
ci.fields.push(field);
576576

577577
// No we look for things like attributes and stuff.
578-
cur.visit(|cur, _| {
578+
cur.visit(|cur| {
579579
if cur.kind() == CXCursor_UnexposedAttr {
580580
ci.found_unknown_attr = true;
581581
}
@@ -593,7 +593,7 @@ impl CompInfo {
593593
CXCursor_UnionDecl |
594594
CXCursor_ClassTemplate |
595595
CXCursor_ClassDecl => {
596-
let inner = Item::parse(*cur, Some(potential_id), ctx)
596+
let inner = Item::parse(cur, Some(potential_id), ctx)
597597
.expect("Inner ClassDecl");
598598
if !ci.inner_types.contains(&inner) {
599599
ci.inner_types.push(inner);
@@ -619,7 +619,7 @@ impl CompInfo {
619619
}
620620

621621
let default_type = Item::from_ty(&cur.cur_type(),
622-
Some(*cur),
622+
Some(cur),
623623
Some(potential_id),
624624
ctx)
625625
.ok();
@@ -687,7 +687,7 @@ impl CompInfo {
687687
// NB: This gets us an owned `Function`, not a
688688
// `FunctionSig`.
689689
let method_signature =
690-
Item::parse(*cur, Some(potential_id), ctx)
690+
Item::parse(cur, Some(potential_id), ctx)
691691
.expect("CXXMethod");
692692

693693
let is_const = cur.method_is_const();
@@ -726,7 +726,7 @@ impl CompInfo {
726726
return CXChildVisit_Continue;
727727
}
728728

729-
let item = Item::parse(*cur, Some(potential_id), ctx)
729+
let item = Item::parse(cur, Some(potential_id), ctx)
730730
.expect("VarDecl");
731731
ci.inner_vars.push(item);
732732
}

src/ir/context.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -490,14 +490,14 @@ impl<'ctx> BindgenContext<'ctx> {
490490
use clangll::*;
491491
let mut args = vec![];
492492
let mut found_invalid_template_ref = false;
493-
location.visit(|c, _| {
493+
location.visit(|c| {
494494
if c.kind() == CXCursor_TemplateRef &&
495495
c.cur_type().kind() == CXType_Invalid {
496496
found_invalid_template_ref = true;
497497
}
498498
if c.kind() == CXCursor_TypeRef {
499499
let new_ty = Item::from_ty_or_ref(c.cur_type(),
500-
Some(*c),
500+
Some(c),
501501
Some(with_id),
502502
self);
503503
args.push(new_ty);

src/ir/enum_ty.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ impl Enum {
7070
None => true,
7171
};
7272

73-
declaration.visit(|cursor, _| {
73+
declaration.visit(|cursor| {
7474
if cursor.kind() == CXCursor_EnumConstantDecl {
7575
let name = cursor.spelling();
7676
let comment = cursor.raw_comment();

src/ir/function.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,10 @@ impl FunctionSig {
167167
// For non-CXCursor_FunctionDecl, visiting the cursor's children
168168
// is the only reliable way to get parameter names.
169169
let mut args = vec![];
170-
cursor.visit(|c, _| {
170+
cursor.visit(|c| {
171171
if c.kind() == CXCursor_ParmDecl {
172172
let ty =
173-
Item::from_ty(&c.cur_type(), Some(*c), None, ctx)
173+
Item::from_ty(&c.cur_type(), Some(c), None, ctx)
174174
.expect("ParmDecl?");
175175
let name = c.spelling();
176176
let name =

src/ir/item.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -966,11 +966,11 @@ impl ClangItemParser for Item {
966966
assert_eq!(popped_decl, declaration_to_look_for);
967967
}
968968

969-
location.visit(|cur, _other| {
969+
location.visit(|cur| {
970970
use clangll::*;
971971
result = Item::from_ty_with_id(id,
972972
ty,
973-
Some(*cur),
973+
Some(cur),
974974
parent_id,
975975
ctx);
976976
match result {

src/ir/module.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ impl ClangSubItemParser for Module {
4949
CXCursor_Namespace => {
5050
let module_id = ctx.module(cursor);
5151
ctx.with_module(module_id, |ctx, children| {
52-
cursor.visit(|cursor, _| {
53-
parse_one(ctx, *cursor, Some(module_id), children)
52+
cursor.visit(|cursor| {
53+
parse_one(ctx, cursor, Some(module_id), children)
5454
})
5555
});
5656

src/ir/ty.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -547,14 +547,14 @@ impl Type {
547547
let mut inner = Err(ParseError::Continue);
548548
let mut args = vec![];
549549

550-
location.visit(|cur, _| {
550+
location.visit(|cur| {
551551
match cur.kind() {
552552
CXCursor_TypeAliasDecl => {
553553
debug_assert!(cur.cur_type().kind() ==
554554
CXType_Typedef);
555555
inner =
556556
Item::from_ty(&cur.cur_type(),
557-
Some(*cur),
557+
Some(cur),
558558
Some(potential_id),
559559
ctx);
560560
}
@@ -567,7 +567,7 @@ impl Type {
567567

568568
let default_type =
569569
Item::from_ty(&cur.cur_type(),
570-
Some(*cur),
570+
Some(cur),
571571
Some(potential_id),
572572
ctx)
573573
.ok();

src/ir/var.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ fn get_integer_literal_from_cursor(cursor: &clang::Cursor,
203203
-> Option<i64> {
204204
use clangll::*;
205205
let mut value = None;
206-
cursor.visit(|c, _| {
206+
cursor.visit(|c| {
207207
match c.kind() {
208208
CXCursor_IntegerLiteral |
209209
CXCursor_UnaryOperator => {

src/lib.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ pub fn parse_one(ctx: &mut BindgenContext,
418418
Ok(id) => children.push(id),
419419
Err(ParseError::Continue) => {}
420420
Err(ParseError::Recurse) => {
421-
cursor.visit(|child, _| parse_one(ctx, *child, parent, children));
421+
cursor.visit(|child| parse_one(ctx, child, parent, children));
422422
}
423423
}
424424
CXChildVisit_Continue
@@ -437,12 +437,12 @@ fn parse(context: &mut BindgenContext) {
437437

438438
let cursor = context.translation_unit().cursor();
439439
if context.options().emit_ast {
440-
cursor.visit(|cur, _| clang::ast_dump(cur, 0));
440+
cursor.visit(|cur| clang::ast_dump(&cur, 0));
441441
}
442442

443443
let root = context.root_module();
444444
context.with_module(root, |context, children| {
445-
cursor.visit(|cursor, _| parse_one(context, *cursor, None, children))
445+
cursor.visit(|cursor| parse_one(context, cursor, None, children))
446446
});
447447

448448
assert!(context.current_module() == context.root_module(),

0 commit comments

Comments
 (0)