Support renaming for constants with casts, and properly order them in the output.

This commit is contained in:
Hayes Neuman
2020-05-22 14:36:07 -04:00
committed by Emilio Cobos Álvarez
parent 967c378308
commit bb00d1c4a8
10 changed files with 63 additions and 8 deletions
+2 -2
View File
@@ -216,7 +216,7 @@ impl Bindings {
self.open_namespaces(&mut out);
for constant in &self.constants {
if constant.ty.is_primitive_or_ptr_primitive() {
if constant.uses_only_primitive_types() {
out.new_line_if_not_start();
constant.write(&self.config, &mut out, None);
out.new_line();
@@ -247,7 +247,7 @@ impl Bindings {
}
for constant in &self.constants {
if !constant.ty.is_primitive_or_ptr_primitive() {
if !constant.uses_only_primitive_types() {
out.new_line_if_not_start();
constant.write(&self.config, &mut out, None);
out.new_line();
+45 -6
View File
@@ -46,11 +46,17 @@ pub enum Literal {
impl Literal {
fn replace_self_with(&mut self, self_ty: &Path) {
match *self {
Literal::PostfixUnaryOp { .. }
| Literal::BinOp { .. }
| Literal::Expr(..)
| Literal::Path(..)
| Literal::Cast { .. } => {}
Literal::PostfixUnaryOp { ref mut value, .. } => {
value.replace_self_with(self_ty);
}
Literal::BinOp {
ref mut left,
ref mut right,
..
} => {
left.replace_self_with(self_ty);
right.replace_self_with(self_ty);
}
Literal::Struct {
ref mut path,
ref mut export_name,
@@ -63,6 +69,14 @@ impl Literal {
expr.replace_self_with(self_ty);
}
}
Literal::Cast {
ref mut ty,
ref mut value,
} => {
ty.replace_self_with(self_ty);
value.replace_self_with(self_ty);
}
Literal::Expr(..) | Literal::Path(..) => {}
}
}
@@ -80,6 +94,23 @@ impl Literal {
Literal::Cast { ref value, .. } => value.is_valid(bindings),
}
}
pub fn uses_only_primitive_types(&self) -> bool {
match self {
Literal::Expr(..) => true,
Literal::Path(..) => true,
Literal::PostfixUnaryOp { ref value, .. } => value.uses_only_primitive_types(),
Literal::BinOp {
ref left,
ref right,
..
} => left.uses_only_primitive_types() & right.uses_only_primitive_types(),
Literal::Struct { .. } => false,
Literal::Cast { ref value, ref ty } => {
value.uses_only_primitive_types() && ty.is_primitive_or_ptr_primitive()
}
}
}
}
impl Literal {
@@ -110,7 +141,11 @@ impl Literal {
right.rename_for_config(config);
}
Literal::Expr(_) => {}
Literal::Cast { ref mut value, .. } => {
Literal::Cast {
ref mut ty,
ref mut value,
} => {
ty.rename_for_config(config, &GenericParams::default());
value.rename_for_config(config);
}
}
@@ -398,6 +433,10 @@ impl Constant {
associated_to,
}
}
pub fn uses_only_primitive_types(&self) -> bool {
self.value.uses_only_primitive_types() && self.ty.is_primitive_or_ptr_primitive()
}
}
impl Item for Constant {
+2
View File
@@ -27,6 +27,8 @@ typedef union C_D {
typedef C_A C_F;
#define C_I (intptr_t)(C_F*)10
extern const int32_t G;
void root(const C_A *a, C_AwesomeB b, C_C c, C_D d, C_E e, C_F f);
+2
View File
@@ -33,6 +33,8 @@ typedef union C_D {
typedef C_A C_F;
#define C_I (intptr_t)(C_F*)10
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
+2
View File
@@ -27,6 +27,8 @@ typedef union {
typedef C_A C_F;
#define C_I (intptr_t)(C_F*)10
extern const int32_t G;
void root(const C_A *a, C_AwesomeB b, C_C c, C_D d, C_E e, C_F f);
+2
View File
@@ -33,6 +33,8 @@ typedef union {
typedef C_A C_F;
#define C_I (intptr_t)(C_F*)10
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
+2
View File
@@ -26,6 +26,8 @@ union C_D {
using C_F = C_A;
static const intptr_t C_I = (intptr_t)(C_F*)10;
extern "C" {
extern const int32_t G;
+2
View File
@@ -27,6 +27,8 @@ union C_D {
typedef struct C_A C_F;
#define C_I (intptr_t)(C_F*)10
extern const int32_t G;
void root(const struct C_A *a, struct C_AwesomeB b, struct C_C c, union C_D d, C_E e, C_F f);
+2
View File
@@ -33,6 +33,8 @@ union C_D {
typedef struct C_A C_F;
#define C_I (intptr_t)(C_F*)10
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
+2
View File
@@ -33,6 +33,8 @@ pub static G: i32 = 10;
pub const H: i32 = 10;
pub const I: isize = 10 as *mut F as isize;
#[no_mangle]
pub extern "C" fn root(
a: *const A,