Commit 3b61c8ea authored by Ingvar Stepanyan's avatar Ingvar Stepanyan Committed by Ryan Hunt

Initial support for tagged enums

parent 2ccaa3e9
This diff is collapsed.
......@@ -22,6 +22,7 @@ pub struct Struct {
pub name: String,
pub generic_params: GenericParams,
pub fields: Vec<(String, Type, Documentation)>,
pub is_variant: bool,
pub tuple_struct: bool,
pub cfg: Option<Cfg>,
pub annotations: AnnotationSet,
......@@ -63,6 +64,7 @@ impl Struct {
name: name,
generic_params: GenericParams::new(generics),
fields: fields,
is_variant: false,
tuple_struct: tuple_struct,
cfg: Cfg::append(mod_cfg, Cfg::load(attrs)),
annotations: AnnotationSet::load(attrs)?,
......@@ -131,33 +133,25 @@ impl Item for Struct {
config.structure.rename_fields,
];
if let Some(o) = self.annotations.list("field-names") {
let mut overriden_fields = Vec::new();
let mut names = self.fields.iter_mut().map(|field| &mut field.0);
for (i, &(ref name, ref ty, ref doc)) in self.fields.iter().enumerate() {
if i >= o.len() {
overriden_fields.push((name.clone(), ty.clone(), doc.clone()));
} else {
overriden_fields.push((o[i].clone(), ty.clone(), doc.clone()));
}
if let Some(o) = self.annotations.list("field-names") {
for (dest, src) in names.zip(o) {
*dest = src;
}
self.fields = overriden_fields;
} else if let Some(r) = find_first_some(&field_rules) {
self.fields = self.fields
.iter()
.map(|x| {
(
r.apply_to_snake_case(&x.0, IdentifierType::StructMember),
x.1.clone(),
x.2.clone(),
)
})
.collect();
for name in names {
*name = r.apply_to_snake_case(name, IdentifierType::StructMember);
}
} else if self.tuple_struct {
// If there is a tag field, skip it
if self.is_variant {
names.next();
}
// If we don't have any rules for a tuple struct, prefix them with
// an underscore so it still compiles
for &mut (ref mut name, ..) in &mut self.fields {
for name in names {
name.insert(0, '_');
}
}
......@@ -189,6 +183,7 @@ impl Item for Struct {
.iter()
.map(|x| (x.0.clone(), x.1.specialize(&mappings), x.2.clone()))
.collect(),
is_variant: self.is_variant,
tuple_struct: self.tuple_struct,
cfg: self.cfg.clone(),
annotations: self.annotations.clone(),
......
......@@ -42,6 +42,30 @@ enum E {
};
typedef intptr_t E;
enum F_Tag {
Foo = 0,
Bar = 1,
Baz = 2,
};
typedef uint8_t F_Tag;
typedef struct {
F_Tag tag;
int16_t _0;
} Foo_Body;
typedef struct {
F_Tag tag;
uint8_t x;
int16_t y;
} Bar_Body;
typedef union {
F_Tag tag;
Foo_Body Foo;
Bar_Body Bar;
} F;
typedef struct Opaque Opaque;
void root(Opaque *o, A a, B b, C c, D d, E e);
void root(Opaque *o, A a, B b, C c, D d, E e, F f);
......@@ -36,10 +36,33 @@ enum class E : intptr_t {
e4 = 5,
};
union F {
enum class Tag : uint8_t {
Foo = 0,
Bar = 1,
Baz = 2,
};
struct Foo_Body {
Tag tag;
int16_t _0;
};
struct Bar_Body {
Tag tag;
uint8_t x;
int16_t y;
};
Tag tag;
Foo_Body Foo;
Bar_Body Bar;
};
struct Opaque;
extern "C" {
void root(Opaque *o, A a, B b, C c, D d, E e);
void root(Opaque *o, A a, B b, C c, D d, E e, F f);
} // extern "C"
......@@ -43,6 +43,13 @@ enum E {
e4 = 5,
}
#[repr(u8)]
enum F {
Foo(i16),
Bar { x: u8, y: i16 },
Baz
}
#[no_mangle]
pub extern "C" fn root(
o: *mut Opaque,
......@@ -50,5 +57,6 @@ pub extern "C" fn root(
b: B,
c: C,
d: D,
e: E
e: E,
f: F
) { }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment