Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enum: Support inlined definitions for tuple variants with a single field #631

Merged
merged 4 commits into from
Dec 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
149 changes: 111 additions & 38 deletions src/bindgen/ir/enumeration.rs

Large diffs are not rendered by default.

37 changes: 13 additions & 24 deletions src/bindgen/ir/structure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ pub struct Struct {
pub is_enum_variant_body: bool,
pub alignment: Option<ReprAlign>,
pub is_transparent: bool,
pub tuple_struct: bool,
pub cfg: Option<Cfg>,
pub annotations: AnnotationSet,
pub documentation: Documentation,
Expand Down Expand Up @@ -70,15 +69,12 @@ impl Struct {
layout_config.ensure_safe_to_represent(&align)?;
}

let (fields, tuple_struct) = match item.fields {
syn::Fields::Unit => (Vec::new(), false),
syn::Fields::Named(ref fields) => {
let out = fields
.named
.iter()
.try_skip_map(|field| Field::load(field, &path))?;
(out, false)
}
let fields = match item.fields {
syn::Fields::Unit => Vec::new(),
syn::Fields::Named(ref fields) => fields
.named
.iter()
.try_skip_map(|field| Field::load(field, &path))?,
syn::Fields::Unnamed(ref fields) => {
let mut out = Vec::new();
let mut current = 0;
Expand All @@ -95,7 +91,7 @@ impl Struct {
current += 1;
}
}
(out, true)
out
}
};

Expand All @@ -110,7 +106,6 @@ impl Struct {
is_enum_variant_body,
repr.align,
is_transparent,
tuple_struct,
Cfg::append(mod_cfg, Cfg::load(&item.attrs)),
AnnotationSet::load(&item.attrs)?,
Documentation::load(&item.attrs),
Expand All @@ -126,7 +121,6 @@ impl Struct {
is_enum_variant_body: bool,
alignment: Option<ReprAlign>,
is_transparent: bool,
tuple_struct: bool,
cfg: Option<Cfg>,
annotations: AnnotationSet,
documentation: Documentation,
Expand All @@ -141,7 +135,6 @@ impl Struct {
is_enum_variant_body,
alignment,
is_transparent,
tuple_struct,
cfg,
annotations,
documentation,
Expand Down Expand Up @@ -201,7 +194,6 @@ impl Struct {
self.is_enum_variant_body,
self.alignment,
self.is_transparent,
self.tuple_struct,
self.cfg.clone(),
self.annotations.clone(),
self.documentation.clone(),
Expand Down Expand Up @@ -313,7 +305,7 @@ impl Item for Struct {

// Scope for mutable borrow of fields
{
let mut names = self.fields.iter_mut().map(|field| &mut field.name);
let names = self.fields.iter_mut().map(|field| &mut field.name);

let field_rules = self
.annotations
Expand All @@ -328,16 +320,13 @@ impl Item for Struct {
for name in names {
*name = r.apply(name, IdentifierType::StructMember).into_owned();
}
} else if self.tuple_struct {
// If there is a tag field, skip it
if self.has_tag_field {
names.next();
}

} else {
// If we don't have any rules for a tuple struct, prefix them with
// an underscore so it still compiles
// an underscore so it still compiles.
for name in names {
name.insert(0, '_');
if name.starts_with(|c: char| c.is_ascii_digit()) {
name.insert(0, '_');
}
}
}
}
Expand Down
6 changes: 2 additions & 4 deletions src/bindgen/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,19 +176,17 @@ impl<'a, F: Write> SourceWriter<'a, F> {
}

pub fn close_brace(&mut self, semicolon: bool) {
self.pop_tab();
match self.bindings.config.language {
Language::Cxx | Language::C => {
self.pop_tab();
self.new_line();
if semicolon {
self.write("};");
} else {
self.write("}");
}
}
Language::Cython => {
self.pop_tab();
}
Language::Cython => {}
}
}

Expand Down
18 changes: 7 additions & 11 deletions tests/expectations/annotation.both.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,6 @@ enum F_Tag {
};
typedef uint8_t F_Tag;

typedef struct Foo_Body {
F_Tag tag;
int16_t _0;
} Foo_Body;

typedef struct Bar_Body {
F_Tag tag;
uint8_t x;
Expand All @@ -38,7 +33,10 @@ typedef struct Bar_Body {

typedef union F {
F_Tag tag;
Foo_Body foo;
struct {
F_Tag foo_tag;
int16_t foo;
};
Bar_Body bar;
} F;

Expand All @@ -49,10 +47,6 @@ enum H_Tag {
};
typedef uint8_t H_Tag;

typedef struct Hello_Body {
int16_t _0;
} Hello_Body;

typedef struct There_Body {
uint8_t x;
int16_t y;
Expand All @@ -61,7 +55,9 @@ typedef struct There_Body {
typedef struct H {
H_Tag tag;
union {
Hello_Body hello;
struct {
int16_t hello;
};
There_Body there;
};
} H;
Expand Down
18 changes: 7 additions & 11 deletions tests/expectations/annotation.both.compat.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,6 @@ enum F_Tag
typedef uint8_t F_Tag;
#endif // __cplusplus

typedef struct Foo_Body {
F_Tag tag;
int16_t _0;
} Foo_Body;

typedef struct Bar_Body {
F_Tag tag;
uint8_t x;
Expand All @@ -50,7 +45,10 @@ typedef struct Bar_Body {

typedef union F {
F_Tag tag;
Foo_Body foo;
struct {
F_Tag foo_tag;
int16_t foo;
};
Bar_Body bar;
} F;

Expand All @@ -67,10 +65,6 @@ enum H_Tag
typedef uint8_t H_Tag;
#endif // __cplusplus

typedef struct Hello_Body {
int16_t _0;
} Hello_Body;

typedef struct There_Body {
uint8_t x;
int16_t y;
Expand All @@ -79,7 +73,9 @@ typedef struct There_Body {
typedef struct H {
H_Tag tag;
union {
Hello_Body hello;
struct {
int16_t hello;
};
There_Body there;
};
} H;
Expand Down
18 changes: 7 additions & 11 deletions tests/expectations/annotation.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,6 @@ enum F_Tag {
};
typedef uint8_t F_Tag;

typedef struct {
F_Tag tag;
int16_t _0;
} Foo_Body;

typedef struct {
F_Tag tag;
uint8_t x;
Expand All @@ -38,7 +33,10 @@ typedef struct {

typedef union {
F_Tag tag;
Foo_Body foo;
struct {
F_Tag foo_tag;
int16_t foo;
};
Bar_Body bar;
} F;

Expand All @@ -49,10 +47,6 @@ enum H_Tag {
};
typedef uint8_t H_Tag;

typedef struct {
int16_t _0;
} Hello_Body;

typedef struct {
uint8_t x;
int16_t y;
Expand All @@ -61,7 +55,9 @@ typedef struct {
typedef struct {
H_Tag tag;
union {
Hello_Body hello;
struct {
int16_t hello;
};
There_Body there;
};
} H;
Expand Down
18 changes: 7 additions & 11 deletions tests/expectations/annotation.compat.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,6 @@ enum F_Tag
typedef uint8_t F_Tag;
#endif // __cplusplus

typedef struct {
F_Tag tag;
int16_t _0;
} Foo_Body;

typedef struct {
F_Tag tag;
uint8_t x;
Expand All @@ -50,7 +45,10 @@ typedef struct {

typedef union {
F_Tag tag;
Foo_Body foo;
struct {
F_Tag foo_tag;
int16_t foo;
};
Bar_Body bar;
} F;

Expand All @@ -67,10 +65,6 @@ enum H_Tag
typedef uint8_t H_Tag;
#endif // __cplusplus

typedef struct {
int16_t _0;
} Hello_Body;

typedef struct {
uint8_t x;
int16_t y;
Expand All @@ -79,7 +73,9 @@ typedef struct {
typedef struct {
H_Tag tag;
union {
Hello_Body hello;
struct {
int16_t hello;
};
There_Body there;
};
} H;
Expand Down
12 changes: 3 additions & 9 deletions tests/expectations/annotation.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,15 @@ cdef extern from *:
Baz,
ctypedef uint8_t F_Tag;

ctypedef struct Foo_Body:
F_Tag tag;
int16_t _0;

ctypedef struct Bar_Body:
F_Tag tag;
uint8_t x;
int16_t y;

ctypedef union F:
F_Tag tag;
Foo_Body foo;
F_Tag foo_tag;
int16_t foo;
Bar_Body bar;

cdef enum:
Expand All @@ -44,16 +41,13 @@ cdef extern from *:
Everyone,
ctypedef uint8_t H_Tag;

ctypedef struct Hello_Body:
int16_t _0;

ctypedef struct There_Body:
uint8_t x;
int16_t y;

ctypedef struct H:
H_Tag tag;
Hello_Body hello;
int16_t hello;
There_Body there;

void root(A x, B y, C z, F f, H h);
18 changes: 7 additions & 11 deletions tests/expectations/annotation.tag.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,6 @@ enum F_Tag {
};
typedef uint8_t F_Tag;

struct Foo_Body {
F_Tag tag;
int16_t _0;
};

struct Bar_Body {
F_Tag tag;
uint8_t x;
Expand All @@ -38,7 +33,10 @@ struct Bar_Body {

union F {
F_Tag tag;
struct Foo_Body foo;
struct {
F_Tag foo_tag;
int16_t foo;
};
struct Bar_Body bar;
};

Expand All @@ -49,10 +47,6 @@ enum H_Tag {
};
typedef uint8_t H_Tag;

struct Hello_Body {
int16_t _0;
};

struct There_Body {
uint8_t x;
int16_t y;
Expand All @@ -61,7 +55,9 @@ struct There_Body {
struct H {
H_Tag tag;
union {
struct Hello_Body hello;
struct {
int16_t hello;
};
struct There_Body there;
};
};
Expand Down
Loading