Skip to content

Commit

Permalink
ir: escape tagged union's field name
Browse files Browse the repository at this point in the history
  • Loading branch information
TheKK authored and emilio committed Mar 9, 2020
1 parent c344280 commit e2b2c81
Show file tree
Hide file tree
Showing 9 changed files with 188 additions and 8 deletions.
4 changes: 3 additions & 1 deletion src/bindgen/ir/enumeration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use crate::bindgen::library::Library;
use crate::bindgen::mangle;
use crate::bindgen::monomorph::Monomorphs;
use crate::bindgen::rename::{IdentifierType, RenameRule};
use crate::bindgen::reserved;
use crate::bindgen::utilities::find_first_some;
use crate::bindgen::writer::{ListType, Source, SourceWriter};

Expand Down Expand Up @@ -421,8 +422,9 @@ impl Item for Enum {
}

for variant in &mut self.variants {
if let Some((_, ref mut body)) = variant.body {
if let Some((ref mut field_name, ref mut body)) = variant.body {
body.rename_for_config(config);
reserved::escape(field_name);
}
}

Expand Down
24 changes: 23 additions & 1 deletion tests/expectations/both/reserved.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,26 @@ typedef struct C {
};
} C;

void root(A a, B b, C c, int32_t namespace_, float float_);
enum E_Tag {
Double,
Float,
};
typedef uint8_t E_Tag;

typedef struct Double_Body {
double _0;
} Double_Body;

typedef struct Float_Body {
float _0;
} Float_Body;

typedef struct E {
E_Tag tag;
union {
Double_Body double_;
Float_Body float_;
};
} E;

void root(A a, B b, C c, E e, int32_t namespace_, float float_);
30 changes: 29 additions & 1 deletion tests/expectations/both/reserved.compat.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,39 @@ typedef struct C {
};
} C;

enum E_Tag
#ifdef __cplusplus
: uint8_t
#endif // __cplusplus
{
Double,
Float,
};
#ifndef __cplusplus
typedef uint8_t E_Tag;
#endif // __cplusplus

typedef struct Double_Body {
double _0;
} Double_Body;

typedef struct Float_Body {
float _0;
} Float_Body;

typedef struct E {
E_Tag tag;
union {
Double_Body double_;
Float_Body float_;
};
} E;

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

void root(A a, B b, C c, int32_t namespace_, float float_);
void root(A a, B b, C c, E e, int32_t namespace_, float float_);

#ifdef __cplusplus
} // extern "C"
Expand Down
24 changes: 23 additions & 1 deletion tests/expectations/reserved.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,26 @@ typedef struct {
};
} C;

void root(A a, B b, C c, int32_t namespace_, float float_);
enum E_Tag {
Double,
Float,
};
typedef uint8_t E_Tag;

typedef struct {
double _0;
} Double_Body;

typedef struct {
float _0;
} Float_Body;

typedef struct {
E_Tag tag;
union {
Double_Body double_;
Float_Body float_;
};
} E;

void root(A a, B b, C c, E e, int32_t namespace_, float float_);
30 changes: 29 additions & 1 deletion tests/expectations/reserved.compat.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,39 @@ typedef struct {
};
} C;

enum E_Tag
#ifdef __cplusplus
: uint8_t
#endif // __cplusplus
{
Double,
Float,
};
#ifndef __cplusplus
typedef uint8_t E_Tag;
#endif // __cplusplus

typedef struct {
double _0;
} Double_Body;

typedef struct {
float _0;
} Float_Body;

typedef struct {
E_Tag tag;
union {
Double_Body double_;
Float_Body float_;
};
} E;

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

void root(A a, B b, C c, int32_t namespace_, float float_);
void root(A a, B b, C c, E e, int32_t namespace_, float float_);

#ifdef __cplusplus
} // extern "C"
Expand Down
23 changes: 22 additions & 1 deletion tests/expectations/reserved.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,29 @@ struct C {
};
};

struct E {
enum class Tag : uint8_t {
Double,
Float,
};

struct Double_Body {
double _0;
};

struct Float_Body {
float _0;
};

Tag tag;
union {
Double_Body double_;
Float_Body float_;
};
};

extern "C" {

void root(A a, B b, C c, int32_t namespace_, float float_);
void root(A a, B b, C c, E e, int32_t namespace_, float float_);

} // extern "C"
24 changes: 23 additions & 1 deletion tests/expectations/tag/reserved.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,26 @@ struct C {
};
};

void root(struct A a, struct B b, struct C c, int32_t namespace_, float float_);
enum E_Tag {
Double,
Float,
};
typedef uint8_t E_Tag;

struct Double_Body {
double _0;
};

struct Float_Body {
float _0;
};

struct E {
E_Tag tag;
union {
struct Double_Body double_;
struct Float_Body float_;
};
};

void root(struct A a, struct B b, struct C c, struct E e, int32_t namespace_, float float_);
30 changes: 29 additions & 1 deletion tests/expectations/tag/reserved.compat.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,39 @@ struct C {
};
};

enum E_Tag
#ifdef __cplusplus
: uint8_t
#endif // __cplusplus
{
Double,
Float,
};
#ifndef __cplusplus
typedef uint8_t E_Tag;
#endif // __cplusplus

struct Double_Body {
double _0;
};

struct Float_Body {
float _0;
};

struct E {
E_Tag tag;
union {
struct Double_Body double_;
struct Float_Body float_;
};
};

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

void root(struct A a, struct B b, struct C c, int32_t namespace_, float float_);
void root(struct A a, struct B b, struct C c, struct E e, int32_t namespace_, float float_);

#ifdef __cplusplus
} // extern "C"
Expand Down
7 changes: 7 additions & 0 deletions tests/rust/reserved.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,18 @@ enum C {
D { namespace: i32, float: f32 },
}

#[repr(C, u8)]
enum E {
Double(f64),
Float(f32),
}

#[no_mangle]
pub extern "C" fn root(
a: A,
b: B,
c: C,
e: E,
namespace: i32,
float: f32,
) { }

0 comments on commit e2b2c81

Please sign in to comment.