Support constants with arbitrary types

This commit is contained in:
Vadim Petrochenkov
2020-10-03 20:49:38 +03:00
committed by Emilio Cobos Álvarez
parent 9669cadf60
commit 6439e6df56
9 changed files with 159 additions and 18 deletions
+1 -18
View File
@@ -195,12 +195,9 @@ impl Literal {
})
}
// Match literals like "one", 'a', 32 etc
// Match literals like true, 'a', 32 etc
syn::Expr::Lit(syn::ExprLit { ref lit, .. }) => {
match lit {
syn::Lit::Str(ref value) => {
Ok(Literal::Expr(format!("u8\"{}\"", value.value())))
}
syn::Lit::Byte(ref value) => Ok(Literal::Expr(format!("{}", value.value()))),
syn::Lit::Char(ref value) => Ok(Literal::Expr(match value.value() as u32 {
0..=255 => format!("'{}'", value.value().escape_default()),
@@ -363,16 +360,6 @@ pub struct Constant {
pub associated_to: Option<Path>,
}
fn can_handle(ty: &Type, expr: &syn::Expr) -> bool {
if ty.is_primitive_or_ptr_primitive() {
return true;
}
match *expr {
syn::Expr::Struct(_) => true,
_ => false,
}
}
impl Constant {
pub fn load(
path: Path,
@@ -390,10 +377,6 @@ impl Constant {
}
};
if !can_handle(&ty, expr) {
return Err("Unhandled const definition".to_owned());
}
let mut lit = Literal::load(&expr)?;
if let Some(ref associated_to) = associated_to {
@@ -0,0 +1,20 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
typedef enum E {
V,
} E;
typedef struct S {
uint8_t field;
} S;
typedef uint8_t A;
#define C1 (S){ .field = 0 }
#define C2 V
#define C3 0
@@ -0,0 +1,20 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
typedef enum E {
V,
} E;
typedef struct S {
uint8_t field;
} S;
typedef uint8_t A;
#define C1 (S){ .field = 0 }
#define C2 V
#define C3 0
@@ -0,0 +1,20 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
typedef enum {
V,
} E;
typedef struct {
uint8_t field;
} S;
typedef uint8_t A;
#define C1 (S){ .field = 0 }
#define C2 V
#define C3 0
@@ -0,0 +1,20 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
typedef enum {
V,
} E;
typedef struct {
uint8_t field;
} S;
typedef uint8_t A;
#define C1 (S){ .field = 0 }
#define C2 V
#define C3 0
@@ -0,0 +1,21 @@
#include <cstdarg>
#include <cstdint>
#include <cstdlib>
#include <ostream>
#include <new>
enum E {
V,
};
struct S {
uint8_t field;
};
using A = uint8_t;
static const S C1 = S{ /* .field = */ 0 };
static const E C2 = V;
static const A C3 = 0;
@@ -0,0 +1,20 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
enum E {
V,
};
struct S {
uint8_t field;
};
typedef uint8_t A;
#define C1 (S){ .field = 0 }
#define C2 V
#define C3 0
@@ -0,0 +1,20 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
enum E {
V,
};
struct S {
uint8_t field;
};
typedef uint8_t A;
#define C1 (S){ .field = 0 }
#define C2 V
#define C3 0
+17
View File
@@ -0,0 +1,17 @@
#[repr(C)]
pub struct S {
field: u8,
}
/// cbindgen:enum-class=false
#[repr(C)]
pub enum E {
V,
}
use E::*;
pub type A = u8;
pub const C1: S = S { field: 0 };
pub const C2: E = V;
pub const C3: A = 0;