Skip to content

Commit

Permalink
ir: simplify_standard_types should be recursive.
Browse files Browse the repository at this point in the history
Stuff like *mut Option<&mut Foo> should really be *mut *mut Foo for
example.
  • Loading branch information
emilio committed Feb 24, 2021
1 parent 8236c82 commit 7312b99
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 54 deletions.
22 changes: 13 additions & 9 deletions src/bindgen/ir/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -641,25 +641,29 @@ impl Type {
if let Some(ty) = self.simplified_type(config) {
*self = ty;
}

self.visit_types(|ty| ty.simplify_standard_types(config))
}

pub fn replace_self_with(&mut self, self_ty: &Path) {
if let Type::Path(ref mut generic_path) = *self {
generic_path.replace_self_with(self_ty);
}
self.visit_types(|ty| ty.replace_self_with(self_ty))
}

fn visit_types(&mut self, mut visitor: impl FnMut(&mut Type)) {
match *self {
Type::Array(ref mut ty, ..) | Type::Ptr { ref mut ty, .. } => {
ty.replace_self_with(self_ty)
}
Type::Path(ref mut generic_path) => {
generic_path.replace_self_with(self_ty);
}
Type::Primitive(..) => {}
Type::Array(ref mut ty, ..) | Type::Ptr { ref mut ty, .. } => visitor(ty),
Type::Path(..) | Type::Primitive(..) => {}
Type::FuncPtr {
ref mut ret,
ref mut args,
..
} => {
ret.replace_self_with(self_ty);
visitor(ret);
for arg in args {
arg.1.replace_self_with(self_ty);
visitor(&mut arg.1)
}
}
}
Expand Down
9 changes: 4 additions & 5 deletions tests/expectations/simplify_option_ptr.both.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,23 @@ typedef struct Opaque Opaque;

typedef struct Option_____Opaque Option_____Opaque;

typedef struct Option_______c_void Option_______c_void;

typedef struct Foo {
const struct Opaque *x;
struct Opaque *y;
void (*z)(void);
struct Option_______c_void *zz;
void (**zz)(void);
} Foo;

typedef union Bar {
const struct Opaque *x;
struct Opaque *y;
void (*z)(void);
struct Option_______c_void *zz;
void (**zz)(void);
} Bar;

void root(const struct Opaque *a,
struct Opaque *b,
struct Foo c,
union Bar d,
struct Option_____Opaque *e);
struct Option_____Opaque *e,
void (*f)(const struct Opaque*));
9 changes: 4 additions & 5 deletions tests/expectations/simplify_option_ptr.both.compat.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,18 @@ typedef struct Opaque Opaque;

typedef struct Option_____Opaque Option_____Opaque;

typedef struct Option_______c_void Option_______c_void;

typedef struct Foo {
const struct Opaque *x;
struct Opaque *y;
void (*z)(void);
struct Option_______c_void *zz;
void (**zz)(void);
} Foo;

typedef union Bar {
const struct Opaque *x;
struct Opaque *y;
void (*z)(void);
struct Option_______c_void *zz;
void (**zz)(void);
} Bar;

#ifdef __cplusplus
Expand All @@ -31,7 +29,8 @@ void root(const struct Opaque *a,
struct Opaque *b,
struct Foo c,
union Bar d,
struct Option_____Opaque *e);
struct Option_____Opaque *e,
void (*f)(const struct Opaque*));

#ifdef __cplusplus
} // extern "C"
Expand Down
8 changes: 3 additions & 5 deletions tests/expectations/simplify_option_ptr.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,18 @@ typedef struct Opaque Opaque;

typedef struct Option_____Opaque Option_____Opaque;

typedef struct Option_______c_void Option_______c_void;

typedef struct {
const Opaque *x;
Opaque *y;
void (*z)(void);
Option_______c_void *zz;
void (**zz)(void);
} Foo;

typedef union {
const Opaque *x;
Opaque *y;
void (*z)(void);
Option_______c_void *zz;
void (**zz)(void);
} Bar;

void root(const Opaque *a, Opaque *b, Foo c, Bar d, Option_____Opaque *e);
void root(const Opaque *a, Opaque *b, Foo c, Bar d, Option_____Opaque *e, void (*f)(const Opaque*));
8 changes: 3 additions & 5 deletions tests/expectations/simplify_option_ptr.compat.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,25 @@ typedef struct Opaque Opaque;

typedef struct Option_____Opaque Option_____Opaque;

typedef struct Option_______c_void Option_______c_void;

typedef struct {
const Opaque *x;
Opaque *y;
void (*z)(void);
Option_______c_void *zz;
void (**zz)(void);
} Foo;

typedef union {
const Opaque *x;
Opaque *y;
void (*z)(void);
Option_______c_void *zz;
void (**zz)(void);
} Bar;

#ifdef __cplusplus
extern "C" {
#endif // __cplusplus

void root(const Opaque *a, Opaque *b, Foo c, Bar d, Option_____Opaque *e);
void root(const Opaque *a, Opaque *b, Foo c, Bar d, Option_____Opaque *e, void (*f)(const Opaque*));

#ifdef __cplusplus
} // extern "C"
Expand Down
6 changes: 3 additions & 3 deletions tests/expectations/simplify_option_ptr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@ struct Foo {
const Opaque *x;
Opaque *y;
void (*z)();
Option<void(*)()> *zz;
void (**zz)();
};

union Bar {
const Opaque *x;
Opaque *y;
void (*z)();
Option<void(*)()> *zz;
void (**zz)();
};

extern "C" {

void root(const Opaque *a, Opaque *b, Foo c, Bar d, Option<Opaque*> *e);
void root(const Opaque *a, Opaque *b, Foo c, Bar d, Option<Opaque*> *e, void (*f)(const Opaque*));

} // extern "C"
14 changes: 8 additions & 6 deletions tests/expectations/simplify_option_ptr.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,21 @@ cdef extern from *:
ctypedef struct Option_____Opaque:
pass

ctypedef struct Option_______c_void:
pass

ctypedef struct Foo:
const Opaque *x;
Opaque *y;
void (*z)();
Option_______c_void *zz;
void (**zz)();

ctypedef union Bar:
const Opaque *x;
Opaque *y;
void (*z)();
Option_______c_void *zz;
void (**zz)();

void root(const Opaque *a, Opaque *b, Foo c, Bar d, Option_____Opaque *e);
void root(const Opaque *a,
Opaque *b,
Foo c,
Bar d,
Option_____Opaque *e,
void (*f)(const Opaque*));
9 changes: 4 additions & 5 deletions tests/expectations/simplify_option_ptr.tag.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,23 @@ struct Opaque;

struct Option_____Opaque;

struct Option_______c_void;

struct Foo {
const struct Opaque *x;
struct Opaque *y;
void (*z)(void);
struct Option_______c_void *zz;
void (**zz)(void);
};

union Bar {
const struct Opaque *x;
struct Opaque *y;
void (*z)(void);
struct Option_______c_void *zz;
void (**zz)(void);
};

void root(const struct Opaque *a,
struct Opaque *b,
struct Foo c,
union Bar d,
struct Option_____Opaque *e);
struct Option_____Opaque *e,
void (*f)(const struct Opaque*));
9 changes: 4 additions & 5 deletions tests/expectations/simplify_option_ptr.tag.compat.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,18 @@ struct Opaque;

struct Option_____Opaque;

struct Option_______c_void;

struct Foo {
const struct Opaque *x;
struct Opaque *y;
void (*z)(void);
struct Option_______c_void *zz;
void (**zz)(void);
};

union Bar {
const struct Opaque *x;
struct Opaque *y;
void (*z)(void);
struct Option_______c_void *zz;
void (**zz)(void);
};

#ifdef __cplusplus
Expand All @@ -31,7 +29,8 @@ void root(const struct Opaque *a,
struct Opaque *b,
struct Foo c,
union Bar d,
struct Option_____Opaque *e);
struct Option_____Opaque *e,
void (*f)(const struct Opaque*));

#ifdef __cplusplus
} // extern "C"
Expand Down
14 changes: 8 additions & 6 deletions tests/expectations/simplify_option_ptr.tag.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,21 @@ cdef extern from *:
cdef struct Option_____Opaque:
pass

cdef struct Option_______c_void:
pass

cdef struct Foo:
const Opaque *x;
Opaque *y;
void (*z)();
Option_______c_void *zz;
void (**zz)();

cdef union Bar:
const Opaque *x;
Opaque *y;
void (*z)();
Option_______c_void *zz;
void (**zz)();

void root(const Opaque *a, Opaque *b, Foo c, Bar d, Option_____Opaque *e);
void root(const Opaque *a,
Opaque *b,
Foo c,
Bar d,
Option_____Opaque *e,
void (*f)(const Opaque*));
1 change: 1 addition & 0 deletions tests/rust/simplify_option_ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ pub extern "C" fn root(
c: Foo,
d: Bar,
e: *mut Option<*mut Opaque>,
f: extern "C" fn(Option<&Opaque>),
) { }

0 comments on commit 7312b99

Please sign in to comment.