Skip to content

Commit ff12c0c

Browse files
author
bors-servo
authored
Auto merge of #196 - emilio:visitor, r=fitzgen
clang: Simplify visiting code. It has always been a mess. r? @fitzgen
2 parents 4e4bf45 + 3fa7228 commit ff12c0c

File tree

10 files changed

+44
-58
lines changed

10 files changed

+44
-58
lines changed

src/clang.rs

+19-33
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,21 +298,14 @@ impl Cursor {
306298

307299
/// Traverse this cursor's referent and its children.
308300
///
309-
/// Call the given function on each AST node traversed. See `CursorVisitor`
310-
/// for details on arguments passed to the function and how its return value
311-
/// is interpreted.
312-
pub fn visit<F>(&self, func: F)
313-
where F: for<'a, 'b> FnMut(&'a Cursor, &'b Cursor)
314-
-> Enum_CXChildVisitResult,
301+
/// Call the given function on each AST node traversed.
302+
pub fn visit<Visitor>(&self, mut visitor: Visitor)
303+
where Visitor: FnMut(Cursor) -> Enum_CXChildVisitResult,
315304
{
316-
let mut data: Box<CursorVisitor> = Box::new(func);
317-
let opt_visit =
318-
Some(visit_children as extern "C" fn(CXCursor,
319-
CXCursor,
320-
CXClientData)
321-
-> Enum_CXChildVisitResult);
322305
unsafe {
323-
clang_visitChildren(self.x, opt_visit, mem::transmute(&mut data));
306+
clang_visitChildren(self.x,
307+
Some(visit_children::<Visitor>),
308+
mem::transmute(&mut visitor));
324309
}
325310
}
326311

@@ -484,17 +469,18 @@ impl Cursor {
484469
}
485470
}
486471

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

500486
impl PartialEq for Cursor {
@@ -1184,7 +1170,7 @@ pub fn ast_dump(c: &Cursor, depth: isize) -> Enum_CXVisitorResult {
11841170
kind_to_str(c.kind()),
11851171
c.spelling(),
11861172
type_to_str(ct)));
1187-
c.visit(|s, _: &Cursor| ast_dump(s, depth + 1));
1173+
c.visit(|s| ast_dump(&s, depth + 1));
11881174
print_indent(depth, ")");
11891175
CXChildVisit_Continue
11901176
}

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
@@ -492,14 +492,14 @@ impl<'ctx> BindgenContext<'ctx> {
492492
use clangll::*;
493493
let mut args = vec![];
494494
let mut found_invalid_template_ref = false;
495-
location.visit(|c, _| {
495+
location.visit(|c| {
496496
if c.kind() == CXCursor_TemplateRef &&
497497
c.cur_type().kind() == CXType_Invalid {
498498
found_invalid_template_ref = true;
499499
}
500500
if c.kind() == CXCursor_TypeRef {
501501
let new_ty = Item::from_ty_or_ref(c.cur_type(),
502-
Some(*c),
502+
Some(c),
503503
Some(with_id),
504504
self);
505505
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
@@ -1016,11 +1016,11 @@ impl ClangItemParser for Item {
10161016
assert_eq!(popped_decl, declaration_to_look_for);
10171017
}
10181018

1019-
location.visit(|cur, _other| {
1019+
location.visit(|cur| {
10201020
use clangll::*;
10211021
result = Item::from_ty_with_id(id,
10221022
ty,
1023-
Some(*cur),
1023+
Some(cur),
10241024
parent_id,
10251025
ctx);
10261026
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
@@ -461,7 +461,7 @@ pub fn parse_one(ctx: &mut BindgenContext,
461461
Ok(id) => children.push(id),
462462
Err(ParseError::Continue) => {}
463463
Err(ParseError::Recurse) => {
464-
cursor.visit(|child, _| parse_one(ctx, *child, parent, children));
464+
cursor.visit(|child| parse_one(ctx, child, parent, children));
465465
}
466466
}
467467
CXChildVisit_Continue
@@ -480,12 +480,12 @@ fn parse(context: &mut BindgenContext) {
480480

481481
let cursor = context.translation_unit().cursor();
482482
if context.options().emit_ast {
483-
cursor.visit(|cur, _| clang::ast_dump(cur, 0));
483+
cursor.visit(|cur| clang::ast_dump(&cur, 0));
484484
}
485485

486486
let root = context.root_module();
487487
context.with_module(root, |context, children| {
488-
cursor.visit(|cursor, _| parse_one(context, *cursor, None, children))
488+
cursor.visit(|cursor| parse_one(context, cursor, None, children))
489489
});
490490

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

0 commit comments

Comments
 (0)