Skip to content

Commit

Permalink
enumeration: simplify standard types in variants.
Browse files Browse the repository at this point in the history
Fixes #745
  • Loading branch information
emilio committed Mar 31, 2022
1 parent aeb8193 commit a9563b5
Show file tree
Hide file tree
Showing 13 changed files with 328 additions and 9 deletions.
13 changes: 13 additions & 0 deletions src/bindgen/ir/enumeration.rs
Expand Up @@ -240,6 +240,12 @@ impl EnumVariant {
}
}

fn simplify_standard_types(&mut self, config: &Config) {
if let VariantBody::Body { ref mut body, .. } = self.body {
body.simplify_standard_types(config);
}
}

fn add_dependencies(&self, library: &Library, out: &mut Dependencies) {
if let VariantBody::Body { ref body, .. } = self.body {
body.add_dependencies(library, out);
Expand Down Expand Up @@ -1525,4 +1531,11 @@ impl Enum {
}
}
}

pub fn simplify_standard_types(&mut self, config: &Config) {
for variant in &mut self.variants {
variant.simplify_standard_types(config);
}
}

}
3 changes: 3 additions & 0 deletions src/bindgen/library.rs
Expand Up @@ -380,6 +380,9 @@ impl Library {
self.structs.for_all_items_mut(|x| {
x.simplify_standard_types(config);
});
self.enums.for_all_items_mut(|x| {
x.simplify_standard_types(config);
});
self.unions.for_all_items_mut(|x| {
x.simplify_standard_types(config);
});
Expand Down
34 changes: 33 additions & 1 deletion tests/expectations/enum.both.c
@@ -1,3 +1,17 @@
#if 0
''' '
#endif

#ifdef __cplusplus
template <typename T>
using Box = T*;
#endif

#if 0
' '''
#endif


#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
Expand Down Expand Up @@ -173,6 +187,23 @@ typedef struct P {
};
} P;

typedef enum Q_Tag {
Ok,
Err,
} Q_Tag;

typedef struct Q {
Q_Tag tag;
union {
struct {
uint32_t *ok;
};
struct {
uint32_t err;
};
};
} Q;

void root(struct Opaque *opaque,
A a,
B b,
Expand All @@ -189,7 +220,8 @@ void root(struct Opaque *opaque,
M m,
enum N n,
O o,
struct P p);
struct P p,
struct Q q);

#if 0
''' '
Expand Down
34 changes: 33 additions & 1 deletion tests/expectations/enum.both.compat.c
@@ -1,3 +1,17 @@
#if 0
''' '
#endif

#ifdef __cplusplus
template <typename T>
using Box = T*;
#endif

#if 0
' '''
#endif


#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
Expand Down Expand Up @@ -239,6 +253,23 @@ typedef struct P {
};
} P;

typedef enum Q_Tag {
Ok,
Err,
} Q_Tag;

typedef struct Q {
Q_Tag tag;
union {
struct {
uint32_t *ok;
};
struct {
uint32_t err;
};
};
} Q;

#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
Expand All @@ -259,7 +290,8 @@ void root(struct Opaque *opaque,
M m,
enum N n,
O o,
struct P p);
struct P p,
struct Q q);

#ifdef __cplusplus
} // extern "C"
Expand Down
34 changes: 33 additions & 1 deletion tests/expectations/enum.c
@@ -1,3 +1,17 @@
#if 0
''' '
#endif

#ifdef __cplusplus
template <typename T>
using Box = T*;
#endif

#if 0
' '''
#endif


#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
Expand Down Expand Up @@ -173,6 +187,23 @@ typedef struct {
};
} P;

typedef enum {
Ok,
Err,
} Q_Tag;

typedef struct {
Q_Tag tag;
union {
struct {
uint32_t *ok;
};
struct {
uint32_t err;
};
};
} Q;

void root(Opaque *opaque,
A a,
B b,
Expand All @@ -189,7 +220,8 @@ void root(Opaque *opaque,
M m,
N n,
O o,
P p);
P p,
Q q);

#if 0
''' '
Expand Down
34 changes: 33 additions & 1 deletion tests/expectations/enum.compat.c
@@ -1,3 +1,17 @@
#if 0
''' '
#endif

#ifdef __cplusplus
template <typename T>
using Box = T*;
#endif

#if 0
' '''
#endif


#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
Expand Down Expand Up @@ -239,6 +253,23 @@ typedef struct {
};
} P;

typedef enum {
Ok,
Err,
} Q_Tag;

typedef struct {
Q_Tag tag;
union {
struct {
uint32_t *ok;
};
struct {
uint32_t err;
};
};
} Q;

#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
Expand All @@ -259,7 +290,8 @@ void root(Opaque *opaque,
M m,
N n,
O o,
P p);
P p,
Q q);

#ifdef __cplusplus
} // extern "C"
Expand Down
38 changes: 37 additions & 1 deletion tests/expectations/enum.cpp
@@ -1,3 +1,17 @@
#if 0
''' '
#endif

#ifdef __cplusplus
template <typename T>
using Box = T*;
#endif

#if 0
' '''
#endif


#include <cstdarg>
#include <cstdint>
#include <cstdlib>
Expand Down Expand Up @@ -173,6 +187,27 @@ struct P {
};
};

struct Q {
enum class Tag {
Ok,
Err,
};

struct Ok_Body {
Box<uint32_t> _0;
};

struct Err_Body {
uint32_t _0;
};

Tag tag;
union {
Ok_Body ok;
Err_Body err;
};
};

extern "C" {

void root(Opaque *opaque,
Expand All @@ -191,7 +226,8 @@ void root(Opaque *opaque,
M m,
N n,
O o,
P p);
P p,
Q q);

} // extern "C"

Expand Down
26 changes: 25 additions & 1 deletion tests/expectations/enum.pyx
@@ -1,3 +1,17 @@
#if 0
''' '
#endif
#ifdef __cplusplus
template <typename T>
using Box = T*;
#endif
#if 0
' '''
#endif


from libc.stdint cimport int8_t, int16_t, int32_t, int64_t, intptr_t
from libc.stdint cimport uint8_t, uint16_t, uint32_t, uint64_t, uintptr_t
cdef extern from *:
Expand Down Expand Up @@ -142,6 +156,15 @@ cdef extern from *:
uint8_t p0;
P1_Body p1;

ctypedef enum Q_Tag:
Ok,
Err,

ctypedef struct Q:
Q_Tag tag;
uint32_t *ok;
uint32_t err;

void root(Opaque *opaque,
A a,
B b,
Expand All @@ -158,7 +181,8 @@ cdef extern from *:
M m,
N n,
O o,
P p);
P p,
Q q);

#if 0
''' '
Expand Down
34 changes: 33 additions & 1 deletion tests/expectations/enum.tag.c
@@ -1,3 +1,17 @@
#if 0
''' '
#endif

#ifdef __cplusplus
template <typename T>
using Box = T*;
#endif

#if 0
' '''
#endif


#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
Expand Down Expand Up @@ -173,6 +187,23 @@ struct P {
};
};

enum Q_Tag {
Ok,
Err,
};

struct Q {
enum Q_Tag tag;
union {
struct {
uint32_t *ok;
};
struct {
uint32_t err;
};
};
};

void root(struct Opaque *opaque,
A a,
B b,
Expand All @@ -189,7 +220,8 @@ void root(struct Opaque *opaque,
M m,
enum N n,
O o,
struct P p);
struct P p,
struct Q q);

#if 0
''' '
Expand Down

0 comments on commit a9563b5

Please sign in to comment.