Support casts in constants

This commit is contained in:
Hayes Neuman 2020-05-13 09:04:26 -04:00 committed by Emilio Cobos Álvarez
parent d747939e18
commit 967c378308
9 changed files with 61 additions and 2 deletions

View File

@ -37,6 +37,10 @@ pub enum Literal {
export_name: String, export_name: String,
fields: HashMap<String, Literal>, fields: HashMap<String, Literal>,
}, },
Cast {
ty: Type,
value: Box<Literal>,
},
} }
impl Literal { impl Literal {
@ -45,7 +49,8 @@ impl Literal {
Literal::PostfixUnaryOp { .. } Literal::PostfixUnaryOp { .. }
| Literal::BinOp { .. } | Literal::BinOp { .. }
| Literal::Expr(..) | Literal::Expr(..)
| Literal::Path(..) => {} | Literal::Path(..)
| Literal::Cast { .. } => {}
Literal::Struct { Literal::Struct {
ref mut path, ref mut path,
ref mut export_name, ref mut export_name,
@ -72,6 +77,7 @@ impl Literal {
.. ..
} => left.is_valid(bindings) && right.is_valid(bindings), } => left.is_valid(bindings) && right.is_valid(bindings),
Literal::Struct { ref path, .. } => bindings.struct_exists(path), Literal::Struct { ref path, .. } => bindings.struct_exists(path),
Literal::Cast { ref value, .. } => value.is_valid(bindings),
} }
} }
} }
@ -104,6 +110,9 @@ impl Literal {
right.rename_for_config(config); right.rename_for_config(config);
} }
Literal::Expr(_) => {} Literal::Expr(_) => {}
Literal::Cast { ref mut value, .. } => {
value.rename_for_config(config);
}
} }
} }
@ -229,6 +238,19 @@ impl Literal {
syn::Expr::Paren(syn::ExprParen { ref expr, .. }) => Self::load(expr), syn::Expr::Paren(syn::ExprParen { ref expr, .. }) => Self::load(expr),
syn::Expr::Cast(syn::ExprCast {
ref expr, ref ty, ..
}) => {
let val = Self::load(expr)?;
match Type::load(ty)? {
Some(ty) => Ok(Literal::Cast {
ty,
value: Box::new(val),
}),
None => Err("Cannot cast to zero sized type.".to_owned()),
}
}
_ => Err(format!("Unsupported expression. {:?}", *expr)), _ => Err(format!("Unsupported expression. {:?}", *expr)),
} }
} }
@ -252,6 +274,12 @@ impl Literal {
right.write(config, out); right.write(config, out);
write!(out, ")"); write!(out, ")");
} }
Literal::Cast { ref ty, ref value } => {
write!(out, "(");
ty.write(config, out);
write!(out, ")");
value.write(config, out);
}
Literal::Struct { Literal::Struct {
export_name, export_name,
fields, fields,

View File

@ -3,8 +3,12 @@
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#define CAST (uint8_t)'A'
#define DELIMITER ':' #define DELIMITER ':'
#define DOUBLE_CAST (uint32_t)(float)1
#define EQUID L'\U00010083' #define EQUID L'\U00010083'
#define FOO 10 #define FOO 10

View File

@ -3,8 +3,12 @@
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#define CAST (uint8_t)'A'
#define DELIMITER ':' #define DELIMITER ':'
#define DOUBLE_CAST (uint32_t)(float)1
#define EQUID L'\U00010083' #define EQUID L'\U00010083'
#define FOO 10 #define FOO 10

View File

@ -3,8 +3,12 @@
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#define CAST (uint8_t)'A'
#define DELIMITER ':' #define DELIMITER ':'
#define DOUBLE_CAST (uint32_t)(float)1
#define EQUID L'\U00010083' #define EQUID L'\U00010083'
#define FOO 10 #define FOO 10

View File

@ -3,8 +3,12 @@
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#define CAST (uint8_t)'A'
#define DELIMITER ':' #define DELIMITER ':'
#define DOUBLE_CAST (uint32_t)(float)1
#define EQUID L'\U00010083' #define EQUID L'\U00010083'
#define FOO 10 #define FOO 10

View File

@ -3,8 +3,12 @@
#include <cstdlib> #include <cstdlib>
#include <new> #include <new>
static const uint8_t CAST = (uint8_t)'A';
static const uint32_t DELIMITER = ':'; static const uint32_t DELIMITER = ':';
static const uint32_t DOUBLE_CAST = (uint32_t)(float)1;
static const uint32_t EQUID = L'\U00010083'; static const uint32_t EQUID = L'\U00010083';
static const int32_t FOO = 10; static const int32_t FOO = 10;

View File

@ -3,8 +3,12 @@
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#define CAST (uint8_t)'A'
#define DELIMITER ':' #define DELIMITER ':'
#define DOUBLE_CAST (uint32_t)(float)1
#define EQUID L'\U00010083' #define EQUID L'\U00010083'
#define FOO 10 #define FOO 10

View File

@ -3,8 +3,12 @@
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#define CAST (uint8_t)'A'
#define DELIMITER ':' #define DELIMITER ':'
#define DOUBLE_CAST (uint32_t)(float)1
#define EQUID L'\U00010083' #define EQUID L'\U00010083'
#define FOO 10 #define FOO 10

View File

@ -26,6 +26,9 @@ pub const XBOOL: i64 = 1;
pub const XFALSE: i64 = (0 << SHIFT) | XBOOL; pub const XFALSE: i64 = (0 << SHIFT) | XBOOL;
pub const XTRUE: i64 = 1 << (SHIFT | XBOOL); pub const XTRUE: i64 = 1 << (SHIFT | XBOOL);
pub const CAST: u8 = 'A' as u8;
pub const DOUBLE_CAST: u32 = 1 as f32 as u32;
#[repr(C)] #[repr(C)]
struct Foo { struct Foo {
x: [i32; FOO], x: [i32; FOO],