added follow flag
This commit is contained in:
parent
6aeb955214
commit
a2f19f2a38
154
src/const.rs
154
src/const.rs
@ -3,60 +3,74 @@
|
|||||||
use syntax::ast::*;
|
use syntax::ast::*;
|
||||||
use syntax::ptr::P;
|
use syntax::ptr::P;
|
||||||
|
|
||||||
|
pub enum FloatWidth {
|
||||||
|
Fw32,
|
||||||
|
Fw64,
|
||||||
|
FwAny
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<FloatTy> for FloatWidth {
|
||||||
|
fn from(ty: FloatTy) -> FloatWidth {
|
||||||
|
match ty {
|
||||||
|
TyF32 => Fw32,
|
||||||
|
TyF64 => Fw64,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// a Lit_-like enum to fold constant `Expr`s into
|
/// a Lit_-like enum to fold constant `Expr`s into
|
||||||
#[derive(PartialEq, Eq, Debug, Clone)]
|
#[derive(PartialEq, Eq, Debug, Clone)]
|
||||||
pub enum Constant {
|
pub enum Constant {
|
||||||
|
/// a String "abc"
|
||||||
ConstantStr(&'static str, StrStyle),
|
ConstantStr(&'static str, StrStyle),
|
||||||
|
/// a Binary String b"abc"
|
||||||
ConstantBinary(Rc<Vec<u8>>),
|
ConstantBinary(Rc<Vec<u8>>),
|
||||||
|
/// a single byte b'a'
|
||||||
ConstantByte(u8),
|
ConstantByte(u8),
|
||||||
|
/// a single char 'a'
|
||||||
ConstantChar(char),
|
ConstantChar(char),
|
||||||
|
/// an integer
|
||||||
ConstantInt(u64, LitIntType),
|
ConstantInt(u64, LitIntType),
|
||||||
ConstantFloat(Cow<'static, str>, FloatTy),
|
/// a float with given type
|
||||||
ConstantFloatUnsuffixed(Cow<'static, str>),
|
ConstantFloat(Cow<'static, str>, FloatWidth),
|
||||||
|
/// true or false
|
||||||
ConstantBool(bool),
|
ConstantBool(bool),
|
||||||
|
/// an array of constants
|
||||||
ConstantVec(Vec<Constant>),
|
ConstantVec(Vec<Constant>),
|
||||||
|
/// also an array, but with only one constant, repeated N times
|
||||||
|
ConstantRepeat(Constant, usize),
|
||||||
|
/// a tuple of constants
|
||||||
ConstantTuple(Vec<Constant>),
|
ConstantTuple(Vec<Constant>),
|
||||||
}
|
}
|
||||||
|
|
||||||
/// simple constant folding
|
/// simple constant folding
|
||||||
pub fn constant(cx: &Context, e: &Expr) -> Option<Constant> {
|
pub fn constant(cx: &Context, e: &Expr, follow: bool) -> Option<Constant> {
|
||||||
match e {
|
match e {
|
||||||
&ExprParen(ref inner) => constant(cx, inner),
|
&ExprParen(ref inner) => constant(cx, inner, follow),
|
||||||
&ExprPath(_, _) => fetch_path(cx, e),
|
&ExprPath(_, _) => if follow { fetch_path(cx, e) } else { None },
|
||||||
&ExprBlock(ref block) => constant_block(cx, inner),
|
&ExprBlock(ref block) => constant_block(cx, inner, follow),
|
||||||
&ExprIf(ref cond, ref then, ref otherwise) =>
|
&ExprIf(ref cond, ref then, ref otherwise) =>
|
||||||
match constant(cx, cond) {
|
match constant(cx, cond) {
|
||||||
Some(ConstantBool(true)) => constant(cx, then),
|
Some(ConstantBool(true)) => constant(cx, then, follow),
|
||||||
Some(ConstantBool(false)) => constant(cx, otherwise),
|
Some(ConstantBool(false)) => constant(cx, otherwise, follow),
|
||||||
_ => None,
|
_ => None,
|
||||||
},
|
},
|
||||||
&ExprLit(ref lit) => Some(lit_to_constant(lit)),
|
&ExprLit(ref lit) => Some(lit_to_constant(lit)),
|
||||||
&ExprVec(ref vec) => constant_vec(cx, vec),
|
&ExprVec(ref vec) => constant_vec(cx, vec, follow),
|
||||||
&ExprTup(ref tup) => constant_tup(cx, tup),
|
&ExprTup(ref tup) => constant_tup(cx, tup, follow),
|
||||||
&ExprUnary(op, ref operand) => constant(cx, operand).and_then(
|
&ExprRepeat(ref value, ref number) =>
|
||||||
|
constant_binop_apply(cx, value, number,|v, n| ConstantRepeat(v, n)),
|
||||||
|
&ExprUnary(op, ref operand) => constant(cx, operand, follow).and_then(
|
||||||
|o| match op {
|
|o| match op {
|
||||||
UnNot =>
|
UnNot =>
|
||||||
if let ConstantBool(b) = o {
|
if let ConstantBool(b) = o {
|
||||||
Some(ConstantBool(!b))
|
Some(ConstantBool(!b))
|
||||||
} else { None },
|
} else { None },
|
||||||
UnNeg =>
|
UnNeg => constant_negate(o),
|
||||||
match o {
|
|
||||||
&ConstantInt(value, ty) =>
|
|
||||||
Some(ConstantInt(value, match ty {
|
|
||||||
SignedIntLit(ity, sign) =>
|
|
||||||
SignedIntLit(ity, neg_sign(sign)),
|
|
||||||
UnsuffixedIntLit(sign) =>
|
|
||||||
UnsuffixedIntLit(neg_sign(sign)),
|
|
||||||
_ => { return None; },
|
|
||||||
})),
|
|
||||||
&LitFloat(ref is, ref ty) =>
|
|
||||||
Some(ConstantFloat(neg_float_str(is), ty)),
|
|
||||||
&LitFloatUnsuffixed(ref is) =>
|
|
||||||
Some(ConstantFloatUnsuffixed(neg_float_str(is))),
|
|
||||||
_ => None,
|
|
||||||
},
|
|
||||||
UnUniq | UnDeref => o,
|
UnUniq | UnDeref => o,
|
||||||
}),
|
}),
|
||||||
|
&ExprBinary(op, ref left, ref right) =>
|
||||||
|
constant_binop(cx, op, left, right, follow),
|
||||||
//TODO: add other expressions
|
//TODO: add other expressions
|
||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
@ -69,19 +83,19 @@ fn lit_to_constant(lit: &Lit_) -> Constant {
|
|||||||
&LitByte(b) => ConstantByte(b),
|
&LitByte(b) => ConstantByte(b),
|
||||||
&LitChar(c) => ConstantChar(c),
|
&LitChar(c) => ConstantChar(c),
|
||||||
&LitInt(value, ty) => ConstantInt(value, ty),
|
&LitInt(value, ty) => ConstantInt(value, ty),
|
||||||
&LitFloat(ref is, ty) => ConstantFloat(Cow::Borrowed(&*is), ty),
|
&LitFloat(ref is, ty) => ConstantFloat(Cow::Borrowed(&*is), ty.into()),
|
||||||
&LitFloatUnsuffixed(InternedString) =>
|
&LitFloatUnsuffixed(InternedString) =>
|
||||||
ConstantFloatUnsuffixed(Cow::Borrowed(&*is)),
|
ConstantFloat(Cow::Borrowed(&*is), FwAny),
|
||||||
&LitBool(b) => ConstantBool(b),
|
&LitBool(b) => ConstantBool(b),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// create `Some(ConstantVec(..))` of all constants, unless there is any
|
/// create `Some(ConstantVec(..))` of all constants, unless there is any
|
||||||
/// non-constant part
|
/// non-constant part
|
||||||
fn constant_vec(cx: &Context, vec: &[&Expr]) -> Option<Constant> {
|
fn constant_vec(cx: &Context, vec: &[&Expr], follow: bool) -> Option<Constant> {
|
||||||
Vec<Constant> parts = Vec::new();
|
parts = Vec::new();
|
||||||
for opt_part in vec {
|
for opt_part in vec {
|
||||||
match constant(cx, opt_part) {
|
match constant(cx, opt_part, follow) {
|
||||||
Some(ref p) => parts.push(p),
|
Some(ref p) => parts.push(p),
|
||||||
None => { return None; },
|
None => { return None; },
|
||||||
}
|
}
|
||||||
@ -89,10 +103,10 @@ fn constant_vec(cx: &Context, vec: &[&Expr]) -> Option<Constant> {
|
|||||||
Some(ConstantVec(parts))
|
Some(ConstantVec(parts))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn constant_tup(cx, &Context, tup: &[&Expr]) -> Option<Constant> {
|
fn constant_tup(cx: &Context, tup: &[&Expr], follow: bool) -> Option<Constant> {
|
||||||
Vec<Constant> parts = Vec::new();
|
parts = Vec::new();
|
||||||
for opt_part in vec {
|
for opt_part in vec {
|
||||||
match constant(cx, opt_part) {
|
match constant(cx, opt_part, follow) {
|
||||||
Some(ref p) => parts.push(p),
|
Some(ref p) => parts.push(p),
|
||||||
None => { return None; },
|
None => { return None; },
|
||||||
}
|
}
|
||||||
@ -109,23 +123,81 @@ fn fetch_path(cx: &Context, e: &Expr) -> Option<Constant> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// A block can only yield a constant if it only has one constant expression
|
/// A block can only yield a constant if it only has one constant expression
|
||||||
fn constant_block(cx: &Context, block: &Block) -> Option<Constant> {
|
fn constant_block(cx: &Context, block: &Block, follow: bool) -> Option<Constant> {
|
||||||
if block.stmts.is_empty() {
|
if block.stmts.is_empty() {
|
||||||
block.expr.map(|b| constant(cx, b))
|
block.expr.map(|b| constant(cx, b, follow))
|
||||||
} else { None }
|
} else { None }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn constant_negate(o: Constant) -> Option<Constant> {
|
||||||
|
match o {
|
||||||
|
&ConstantInt(value, ty) =>
|
||||||
|
Some(ConstantInt(value, match ty {
|
||||||
|
SignedIntLit(ity, sign) => SignedIntLit(ity, neg_sign(sign)),
|
||||||
|
UnsuffixedIntLit(sign) => UnsuffixedIntLit(neg_sign(sign)),
|
||||||
|
_ => { return None; },
|
||||||
|
})),
|
||||||
|
&LitFloat(ref is, ref ty) => Some(ConstantFloat(neg_float_str(is), ty)),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn neg_sign(s: Sign) -> Sign {
|
fn neg_sign(s: Sign) -> Sign {
|
||||||
match s:
|
match s {
|
||||||
Sign::Plus => Sign::Minus,
|
Sign::Plus => Sign::Minus,
|
||||||
Sign::Minus => Sign::Plus,
|
Sign::Minus => Sign::Plus,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn neg_float_str(s: &InternedString) -> Cow<'static, str> {
|
fn neg_float_str(s: &InternedString) -> Cow<'static, str> {
|
||||||
if s.startsWith('-') {
|
if s.startsWith('-') {
|
||||||
Cow::Borrowed(s[1..])
|
Cow::Borrowed(s[1..])
|
||||||
} else {
|
} else {
|
||||||
Cow::Owned(format!("-{}", &*s))
|
Cow::Owned(format!("-{}", &*s))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn constant_binop(cx: &Context, op: BinOp, left: &Expr, right: &Expr,
|
||||||
|
follow: bool) -> Option<Constant> {
|
||||||
|
match op.node {
|
||||||
|
//BiAdd,
|
||||||
|
//BiSub,
|
||||||
|
//BiMul,
|
||||||
|
//BiDiv,
|
||||||
|
//BiRem,
|
||||||
|
BiAnd => constant_short_circuit(cx, left, right, false, follow),
|
||||||
|
BiOr => constant_short_circuit(cx, left, right, true, follow),
|
||||||
|
//BiBitXor,
|
||||||
|
//BiBitAnd,
|
||||||
|
//BiBitOr,
|
||||||
|
//BiShl,
|
||||||
|
//BiShr,
|
||||||
|
//BiEq,
|
||||||
|
//BiLt,
|
||||||
|
//BiLe,
|
||||||
|
//BiNe,
|
||||||
|
//BiGe,
|
||||||
|
//BiGt,
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn constant_binop_apply<F>(cx: &Context, left: &Expr, right: &Expr, op: F,
|
||||||
|
follow: bool) -> Option<Constant>
|
||||||
|
where F: FnMut(Constant, Constant) -> Option<Constant> {
|
||||||
|
constant(cx, left, follow).and_then(|l| constant(cx, right, follow)
|
||||||
|
.and_then(|r| op(l, r)))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn constant_short_circuit(cx: &Context, left: &Expr, right: &Expr, b: bool,
|
||||||
|
follow: bool) -> Option<Constant> {
|
||||||
|
if let ConstantBool(lbool) = constant(cx, left, follow) {
|
||||||
|
if l == b {
|
||||||
|
Some(ConstantBool(b))
|
||||||
|
} else {
|
||||||
|
if let ConstantBool(rbool) = constant(cx, right, follow) {
|
||||||
|
Some(ConstantBool(rbool))
|
||||||
|
} else { None }
|
||||||
|
}
|
||||||
|
} else { None }
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user