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,
fields: HashMap<String, Literal>,
},
Cast {
ty: Type,
value: Box<Literal>,
},
}
impl Literal {
@ -45,7 +49,8 @@ impl Literal {
Literal::PostfixUnaryOp { .. }
| Literal::BinOp { .. }
| Literal::Expr(..)
| Literal::Path(..) => {}
| Literal::Path(..)
| Literal::Cast { .. } => {}
Literal::Struct {
ref mut path,
ref mut export_name,
@ -72,6 +77,7 @@ impl Literal {
..
} => left.is_valid(bindings) && right.is_valid(bindings),
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);
}
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::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)),
}
}
@ -252,6 +274,12 @@ impl Literal {
right.write(config, out);
write!(out, ")");
}
Literal::Cast { ref ty, ref value } => {
write!(out, "(");
ty.write(config, out);
write!(out, ")");
value.write(config, out);
}
Literal::Struct {
export_name,
fields,

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -26,10 +26,13 @@ pub const XBOOL: i64 = 1;
pub const XFALSE: i64 = (0 << 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)]
struct Foo {
x: [i32; FOO],
}
#[no_mangle]
pub extern "C" fn root(x: Foo) { }
pub extern "C" fn root(x: Foo) {}