2015-08-12 06:49:28 -05:00
|
|
|
use rustc::lint::Context;
|
|
|
|
use rustc::middle::const_eval::lookup_const_by_id;
|
2015-08-14 10:14:54 -05:00
|
|
|
use rustc::middle::def::PathResolution;
|
|
|
|
use rustc::middle::def::Def::*;
|
2015-08-12 06:49:28 -05:00
|
|
|
use syntax::ast::*;
|
|
|
|
use syntax::ptr::P;
|
2015-08-14 10:14:54 -05:00
|
|
|
use std::rc::Rc;
|
|
|
|
use std::ops::Deref;
|
|
|
|
use self::ConstantVariant::*;
|
|
|
|
use self::FloatWidth::*;
|
|
|
|
|
|
|
|
#[derive(PartialEq, Eq, Debug, Copy, Clone)]
|
2015-08-13 02:25:44 -05:00
|
|
|
pub enum FloatWidth {
|
|
|
|
Fw32,
|
|
|
|
Fw64,
|
|
|
|
FwAny
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<FloatTy> for FloatWidth {
|
|
|
|
fn from(ty: FloatTy) -> FloatWidth {
|
|
|
|
match ty {
|
|
|
|
TyF32 => Fw32,
|
|
|
|
TyF64 => Fw64,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-13 03:45:30 -05:00
|
|
|
#[derive(PartialEq, Eq, Debug, Clone)]
|
|
|
|
pub struct Constant {
|
2015-08-16 08:56:09 -05:00
|
|
|
pub constant: ConstantVariant,
|
|
|
|
pub needed_resolution: bool
|
2015-08-13 03:45:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Constant {
|
2015-08-14 10:14:54 -05:00
|
|
|
pub fn new(variant: ConstantVariant) -> Constant {
|
2015-08-13 03:45:30 -05:00
|
|
|
Constant { constant: variant, needed_resolution: false }
|
|
|
|
}
|
|
|
|
|
2015-08-14 10:14:54 -05:00
|
|
|
pub fn new_resolved(variant: ConstantVariant) -> Constant {
|
2015-08-13 03:45:30 -05:00
|
|
|
Constant { constant: variant, needed_resolution: true }
|
|
|
|
}
|
2015-08-14 10:14:54 -05:00
|
|
|
|
|
|
|
// convert this constant to a f64, if possible
|
|
|
|
pub fn as_float(&self) -> Option<f64> {
|
|
|
|
match &self.constant {
|
|
|
|
&ConstantByte(b) => Some(b as f64),
|
|
|
|
&ConstantFloat(ref s, _) => s.parse().ok(),
|
|
|
|
&ConstantInt(i, ty) => Some(if is_negative(ty) {
|
|
|
|
-(i as f64) } else { i as f64 }),
|
|
|
|
_ => None
|
|
|
|
}
|
|
|
|
}
|
2015-08-13 03:45:30 -05:00
|
|
|
}
|
|
|
|
|
2015-08-12 06:49:28 -05:00
|
|
|
/// a Lit_-like enum to fold constant `Expr`s into
|
|
|
|
#[derive(PartialEq, Eq, Debug, Clone)]
|
2015-08-13 03:45:30 -05:00
|
|
|
pub enum ConstantVariant {
|
2015-08-13 02:25:44 -05:00
|
|
|
/// a String "abc"
|
2015-08-14 10:14:54 -05:00
|
|
|
ConstantStr(String, StrStyle),
|
2015-08-13 02:25:44 -05:00
|
|
|
/// a Binary String b"abc"
|
2015-08-12 06:49:28 -05:00
|
|
|
ConstantBinary(Rc<Vec<u8>>),
|
2015-08-13 02:25:44 -05:00
|
|
|
/// a single byte b'a'
|
2015-08-12 06:49:28 -05:00
|
|
|
ConstantByte(u8),
|
2015-08-13 02:25:44 -05:00
|
|
|
/// a single char 'a'
|
2015-08-12 06:49:28 -05:00
|
|
|
ConstantChar(char),
|
2015-08-13 02:25:44 -05:00
|
|
|
/// an integer
|
2015-08-12 06:49:28 -05:00
|
|
|
ConstantInt(u64, LitIntType),
|
2015-08-13 02:25:44 -05:00
|
|
|
/// a float with given type
|
2015-08-14 10:14:54 -05:00
|
|
|
ConstantFloat(String, FloatWidth),
|
2015-08-13 02:25:44 -05:00
|
|
|
/// true or false
|
2015-08-12 06:49:28 -05:00
|
|
|
ConstantBool(bool),
|
2015-08-13 02:25:44 -05:00
|
|
|
/// an array of constants
|
2015-08-16 09:05:51 -05:00
|
|
|
ConstantVec(Vec<Constant>),
|
2015-08-13 02:25:44 -05:00
|
|
|
/// also an array, but with only one constant, repeated N times
|
2015-08-14 10:14:54 -05:00
|
|
|
ConstantRepeat(Box<ConstantVariant>, usize),
|
2015-08-13 02:25:44 -05:00
|
|
|
/// a tuple of constants
|
2015-08-16 09:05:51 -05:00
|
|
|
ConstantTuple(Vec<Constant>),
|
2015-08-12 06:49:28 -05:00
|
|
|
}
|
|
|
|
|
2015-08-13 03:45:30 -05:00
|
|
|
impl ConstantVariant {
|
|
|
|
/// convert to u64 if possible
|
|
|
|
///
|
|
|
|
/// # panics
|
|
|
|
///
|
|
|
|
/// if the constant could not be converted to u64 losslessly
|
|
|
|
fn as_u64(&self) -> u64 {
|
|
|
|
if let &ConstantInt(val, _) = self {
|
|
|
|
val // TODO we may want to check the sign if any
|
|
|
|
} else {
|
|
|
|
panic!("Could not convert a {:?} to u64");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// simple constant folding: Insert an expression, get a constant or none.
|
|
|
|
pub fn constant(cx: &Context, e: &Expr) -> Option<Constant> {
|
2015-08-14 10:14:54 -05:00
|
|
|
match &e.node {
|
2015-08-13 03:45:30 -05:00
|
|
|
&ExprParen(ref inner) => constant(cx, inner),
|
|
|
|
&ExprPath(_, _) => fetch_path(cx, e),
|
2015-08-14 10:14:54 -05:00
|
|
|
&ExprBlock(ref block) => constant_block(cx, block),
|
2015-08-13 02:25:44 -05:00
|
|
|
&ExprIf(ref cond, ref then, ref otherwise) =>
|
2015-08-14 10:14:54 -05:00
|
|
|
constant_if(cx, &*cond, &*then, &*otherwise),
|
|
|
|
&ExprLit(ref lit) => Some(lit_to_constant(&lit.node)),
|
|
|
|
&ExprVec(ref vec) => constant_vec(cx, &vec[..]),
|
|
|
|
&ExprTup(ref tup) => constant_tup(cx, &tup[..]),
|
2015-08-13 02:25:44 -05:00
|
|
|
&ExprRepeat(ref value, ref number) =>
|
2015-08-14 10:14:54 -05:00
|
|
|
constant_binop_apply(cx, value, number,|v, n|
|
|
|
|
Some(ConstantRepeat(Box::new(v), n.as_u64() as usize))),
|
2015-08-13 03:45:30 -05:00
|
|
|
&ExprUnary(op, ref operand) => constant(cx, operand).and_then(
|
2015-08-12 06:49:28 -05:00
|
|
|
|o| match op {
|
|
|
|
UnNot =>
|
2015-08-14 10:14:54 -05:00
|
|
|
if let ConstantBool(b) = o.constant {
|
2015-08-13 03:45:30 -05:00
|
|
|
Some(Constant{
|
|
|
|
needed_resolution: o.needed_resolution,
|
|
|
|
constant: ConstantBool(!b),
|
|
|
|
})
|
2015-08-12 06:49:28 -05:00
|
|
|
} else { None },
|
2015-08-13 02:25:44 -05:00
|
|
|
UnNeg => constant_negate(o),
|
2015-08-14 10:14:54 -05:00
|
|
|
UnUniq | UnDeref => Some(o),
|
2015-08-12 06:49:28 -05:00
|
|
|
}),
|
2015-08-13 02:25:44 -05:00
|
|
|
&ExprBinary(op, ref left, ref right) =>
|
2015-08-14 10:14:54 -05:00
|
|
|
constant_binop(cx, op, left, right),
|
2015-08-12 06:49:28 -05:00
|
|
|
//TODO: add other expressions
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn lit_to_constant(lit: &Lit_) -> Constant {
|
|
|
|
match lit {
|
2015-08-14 10:14:54 -05:00
|
|
|
&LitStr(ref is, style) =>
|
|
|
|
Constant::new(ConstantStr(is.to_string(), style)),
|
2015-08-13 03:45:30 -05:00
|
|
|
&LitBinary(ref blob) => Constant::new(ConstantBinary(blob.clone())),
|
|
|
|
&LitByte(b) => Constant::new(ConstantByte(b)),
|
|
|
|
&LitChar(c) => Constant::new(ConstantChar(c)),
|
|
|
|
&LitInt(value, ty) => Constant::new(ConstantInt(value, ty)),
|
2015-08-14 10:14:54 -05:00
|
|
|
&LitFloat(ref is, ty) => {
|
|
|
|
Constant::new(ConstantFloat(is.to_string(), ty.into()))
|
|
|
|
},
|
|
|
|
&LitFloatUnsuffixed(ref is) => {
|
|
|
|
Constant::new(ConstantFloat(is.to_string(), FwAny))
|
|
|
|
},
|
2015-08-13 03:45:30 -05:00
|
|
|
&LitBool(b) => Constant::new(ConstantBool(b)),
|
2015-08-12 06:49:28 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// create `Some(ConstantVec(..))` of all constants, unless there is any
|
|
|
|
/// non-constant part
|
2015-08-14 10:14:54 -05:00
|
|
|
fn constant_vec<E: Deref<Target=Expr> + Sized>(cx: &Context, vec: &[E]) -> Option<Constant> {
|
2015-08-13 03:45:30 -05:00
|
|
|
let mut parts = Vec::new();
|
|
|
|
let mut resolved = false;
|
2015-08-16 09:09:00 -05:00
|
|
|
for opt_part in vec {
|
2015-08-13 03:45:30 -05:00
|
|
|
match constant(cx, opt_part) {
|
2015-08-14 10:14:54 -05:00
|
|
|
Some(p) => {
|
|
|
|
resolved |= (&p).needed_resolution;
|
2015-08-13 03:45:30 -05:00
|
|
|
parts.push(p)
|
|
|
|
},
|
2015-08-12 06:49:28 -05:00
|
|
|
None => { return None; },
|
|
|
|
}
|
|
|
|
}
|
2015-08-13 03:45:30 -05:00
|
|
|
Some(Constant {
|
2015-08-16 09:05:51 -05:00
|
|
|
constant: ConstantVec(parts),
|
2015-08-13 03:45:30 -05:00
|
|
|
needed_resolution: resolved
|
|
|
|
})
|
2015-08-12 06:49:28 -05:00
|
|
|
}
|
|
|
|
|
2015-08-14 10:14:54 -05:00
|
|
|
fn constant_tup<E: Deref<Target=Expr> + Sized>(cx: &Context, tup: &[E]) -> Option<Constant> {
|
2015-08-13 03:45:30 -05:00
|
|
|
let mut parts = Vec::new();
|
|
|
|
let mut resolved = false;
|
2015-08-16 09:09:00 -05:00
|
|
|
for opt_part in tup {
|
2015-08-13 03:45:30 -05:00
|
|
|
match constant(cx, opt_part) {
|
2015-08-14 10:14:54 -05:00
|
|
|
Some(p) => {
|
|
|
|
resolved |= (&p).needed_resolution;
|
2015-08-13 03:45:30 -05:00
|
|
|
parts.push(p)
|
|
|
|
},
|
2015-08-12 06:49:28 -05:00
|
|
|
None => { return None; },
|
|
|
|
}
|
|
|
|
}
|
2015-08-13 03:45:30 -05:00
|
|
|
Some(Constant {
|
2015-08-16 09:05:51 -05:00
|
|
|
constant: ConstantTuple(parts),
|
2015-08-13 03:45:30 -05:00
|
|
|
needed_resolution: resolved
|
|
|
|
})
|
2015-08-12 06:49:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// lookup a possibly constant expression from a ExprPath
|
|
|
|
fn fetch_path(cx: &Context, e: &Expr) -> Option<Constant> {
|
|
|
|
if let Some(&PathResolution { base_def: DefConst(id), ..}) =
|
|
|
|
cx.tcx.def_map.borrow().get(&e.id) {
|
2015-08-14 10:14:54 -05:00
|
|
|
lookup_const_by_id(cx.tcx, id, None).and_then(
|
|
|
|
|l| constant(cx, l).map(|c| Constant::new_resolved(c.constant)))
|
2015-08-12 06:49:28 -05:00
|
|
|
} else { None }
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A block can only yield a constant if it only has one constant expression
|
2015-08-13 03:45:30 -05:00
|
|
|
fn constant_block(cx: &Context, block: &Block) -> Option<Constant> {
|
2015-08-12 06:49:28 -05:00
|
|
|
if block.stmts.is_empty() {
|
2015-08-14 10:14:54 -05:00
|
|
|
block.expr.as_ref().and_then(|b| constant(cx, &*b))
|
2015-08-13 03:45:30 -05:00
|
|
|
} else { None }
|
|
|
|
}
|
|
|
|
|
2015-08-14 10:14:54 -05:00
|
|
|
fn constant_if(cx: &Context, cond: &Expr, then: &Block, otherwise:
|
|
|
|
&Option<P<Expr>>) -> Option<Constant> {
|
2015-08-13 03:45:30 -05:00
|
|
|
if let Some(Constant{ constant: ConstantBool(b), needed_resolution: res }) =
|
|
|
|
constant(cx, cond) {
|
2015-08-14 10:14:54 -05:00
|
|
|
if b {
|
|
|
|
constant_block(cx, then)
|
|
|
|
} else {
|
|
|
|
otherwise.as_ref().and_then(|expr| constant(cx, &*expr))
|
|
|
|
}.map(|part|
|
|
|
|
Constant {
|
|
|
|
constant: part.constant,
|
|
|
|
needed_resolution: res || part.needed_resolution,
|
|
|
|
})
|
2015-08-12 06:49:28 -05:00
|
|
|
} else { None }
|
|
|
|
}
|
|
|
|
|
2015-08-13 02:25:44 -05:00
|
|
|
fn constant_negate(o: Constant) -> Option<Constant> {
|
2015-08-13 03:45:30 -05:00
|
|
|
Some(Constant{
|
|
|
|
needed_resolution: o.needed_resolution,
|
|
|
|
constant: match o.constant {
|
2015-08-14 10:14:54 -05:00
|
|
|
ConstantInt(value, ty) =>
|
2015-08-13 03:45:30 -05:00
|
|
|
ConstantInt(value, match ty {
|
|
|
|
SignedIntLit(ity, sign) =>
|
|
|
|
SignedIntLit(ity, neg_sign(sign)),
|
|
|
|
UnsuffixedIntLit(sign) => UnsuffixedIntLit(neg_sign(sign)),
|
|
|
|
_ => { return None; },
|
|
|
|
}),
|
2015-08-16 09:09:00 -05:00
|
|
|
ConstantFloat(is, ty) =>
|
|
|
|
ConstantFloat(neg_float_str(is), ty),
|
2015-08-13 03:45:30 -05:00
|
|
|
_ => { return None; },
|
|
|
|
}
|
|
|
|
})
|
2015-08-13 02:25:44 -05:00
|
|
|
}
|
|
|
|
|
2015-08-12 06:49:28 -05:00
|
|
|
fn neg_sign(s: Sign) -> Sign {
|
2015-08-13 02:25:44 -05:00
|
|
|
match s {
|
2015-08-12 06:49:28 -05:00
|
|
|
Sign::Plus => Sign::Minus,
|
|
|
|
Sign::Minus => Sign::Plus,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-14 10:14:54 -05:00
|
|
|
fn neg_float_str(s: String) -> String {
|
|
|
|
if s.starts_with('-') {
|
|
|
|
s[1..].to_owned()
|
2015-08-12 06:49:28 -05:00
|
|
|
} else {
|
2015-08-14 10:14:54 -05:00
|
|
|
format!("-{}", &*s)
|
2015-08-12 06:49:28 -05:00
|
|
|
}
|
|
|
|
}
|
2015-08-13 02:25:44 -05:00
|
|
|
|
2015-08-13 07:22:05 -05:00
|
|
|
fn is_negative(ty: LitIntType) -> bool {
|
|
|
|
match ty {
|
|
|
|
SignedIntLit(_, sign) | UnsuffixedIntLit(sign) => sign == Minus,
|
|
|
|
UnsignedIntLit(_) => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-14 10:14:54 -05:00
|
|
|
fn unify_int_type(l: LitIntType, r: LitIntType, s: Sign) -> Option<LitIntType> {
|
2015-08-13 07:22:05 -05:00
|
|
|
match (l, r) {
|
|
|
|
(SignedIntLit(lty, _), SignedIntLit(rty, _)) => if lty == rty {
|
|
|
|
Some(SignedIntLit(lty, s)) } else { None },
|
|
|
|
(UnsignedIntLit(lty), UnsignedIntLit(rty)) =>
|
2015-08-14 10:14:54 -05:00
|
|
|
if s == Plus && lty == rty {
|
2015-08-13 07:22:05 -05:00
|
|
|
Some(UnsignedIntLit(lty))
|
|
|
|
} else { None },
|
2015-08-14 10:14:54 -05:00
|
|
|
(UnsuffixedIntLit(_), UnsuffixedIntLit(_)) => Some(UnsuffixedIntLit(s)),
|
|
|
|
(SignedIntLit(lty, _), UnsuffixedIntLit(_)) => Some(SignedIntLit(lty, s)),
|
2015-08-13 07:22:05 -05:00
|
|
|
(UnsignedIntLit(lty), UnsuffixedIntLit(rs)) => if rs == Plus {
|
|
|
|
Some(UnsignedIntLit(lty)) } else { None },
|
2015-08-14 10:14:54 -05:00
|
|
|
(UnsuffixedIntLit(_), SignedIntLit(rty, _)) => Some(SignedIntLit(rty, s)),
|
2015-08-13 07:22:05 -05:00
|
|
|
(UnsuffixedIntLit(ls), UnsignedIntLit(rty)) => if ls == Plus {
|
|
|
|
Some(UnsignedIntLit(rty)) } else { None },
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-13 03:45:30 -05:00
|
|
|
fn constant_binop(cx: &Context, op: BinOp, left: &Expr, right: &Expr)
|
|
|
|
-> Option<Constant> {
|
2015-08-13 02:25:44 -05:00
|
|
|
match op.node {
|
2015-08-13 07:22:05 -05:00
|
|
|
BiAdd => constant_binop_apply(cx, left, right, |l, r|
|
|
|
|
match (l, r) {
|
|
|
|
(ConstantByte(l8), ConstantByte(r8)) =>
|
2015-08-16 09:09:00 -05:00
|
|
|
l8.checked_add(r8).map(ConstantByte),
|
2015-08-13 07:22:05 -05:00
|
|
|
(ConstantInt(l64, lty), ConstantInt(r64, rty)) => {
|
|
|
|
let (ln, rn) = (is_negative(lty), is_negative(rty));
|
|
|
|
if ln == rn {
|
|
|
|
unify_int_type(lty, rty, if ln { Minus } else { Plus })
|
|
|
|
.and_then(|ty| l64.checked_add(r64).map(
|
|
|
|
|v| ConstantInt(v, ty)))
|
|
|
|
} else {
|
|
|
|
if ln {
|
|
|
|
add_neg_int(r64, rty, l64, lty)
|
|
|
|
} else {
|
|
|
|
add_neg_int(l64, lty, r64, rty)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
// TODO: float
|
|
|
|
_ => None
|
|
|
|
}),
|
|
|
|
BiSub => constant_binop_apply(cx, left, right, |l, r|
|
|
|
|
match (l, r) {
|
|
|
|
(ConstantByte(l8), ConstantByte(r8)) => if r8 > l8 {
|
|
|
|
None } else { Some(ConstantByte(l8 - r8)) },
|
|
|
|
(ConstantInt(l64, lty), ConstantInt(r64, rty)) => {
|
|
|
|
let (ln, rn) = (is_negative(lty), is_negative(rty));
|
|
|
|
match (ln, rn) {
|
|
|
|
(false, false) => sub_int(l64, lty, r64, rty, r64 > l64),
|
|
|
|
(true, true) => sub_int(l64, lty, r64, rty, l64 > r64),
|
|
|
|
(true, false) => unify_int_type(lty, rty, Minus)
|
|
|
|
.and_then(|ty| l64.checked_add(r64).map(
|
|
|
|
|v| ConstantInt(v, ty))),
|
|
|
|
(false, true) => unify_int_type(lty, rty, Plus)
|
|
|
|
.and_then(|ty| l64.checked_add(r64).map(
|
|
|
|
|v| ConstantInt(v, ty))),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => None,
|
|
|
|
}),
|
2015-08-13 02:25:44 -05:00
|
|
|
//BiMul,
|
|
|
|
//BiDiv,
|
|
|
|
//BiRem,
|
2015-08-13 03:45:30 -05:00
|
|
|
BiAnd => constant_short_circuit(cx, left, right, false),
|
|
|
|
BiOr => constant_short_circuit(cx, left, right, true),
|
2015-08-13 02:25:44 -05:00
|
|
|
//BiBitXor,
|
|
|
|
//BiBitAnd,
|
|
|
|
//BiBitOr,
|
|
|
|
//BiShl,
|
|
|
|
//BiShr,
|
|
|
|
//BiEq,
|
|
|
|
//BiLt,
|
|
|
|
//BiLe,
|
|
|
|
//BiNe,
|
|
|
|
//BiGe,
|
|
|
|
//BiGt,
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-13 07:22:05 -05:00
|
|
|
fn add_neg_int(pos: u64, pty: LitIntType, neg: u64, nty: LitIntType) ->
|
2015-08-14 10:14:54 -05:00
|
|
|
Option<ConstantVariant> {
|
2015-08-13 07:22:05 -05:00
|
|
|
if neg > pos {
|
|
|
|
unify_int_type(nty, pty, Minus).map(|ty| ConstantInt(neg - pos, ty))
|
|
|
|
} else {
|
|
|
|
unify_int_type(nty, pty, Plus).map(|ty| ConstantInt(pos - neg, ty))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-14 10:14:54 -05:00
|
|
|
fn sub_int(l: u64, lty: LitIntType, r: u64, rty: LitIntType, neg: bool) ->
|
|
|
|
Option<ConstantVariant> {
|
2015-08-13 07:22:05 -05:00
|
|
|
unify_int_type(lty, rty, if neg { Minus } else { Plus }).and_then(
|
2015-08-14 10:14:54 -05:00
|
|
|
|ty| l.checked_sub(r).map(|v| ConstantInt(v, ty)))
|
2015-08-13 07:22:05 -05:00
|
|
|
}
|
|
|
|
|
2015-08-13 03:45:30 -05:00
|
|
|
fn constant_binop_apply<F>(cx: &Context, left: &Expr, right: &Expr, op: F)
|
|
|
|
-> Option<Constant>
|
2015-08-14 10:14:54 -05:00
|
|
|
where F: Fn(ConstantVariant, ConstantVariant) -> Option<ConstantVariant> {
|
|
|
|
if let (Some(Constant { constant: lc, needed_resolution: ln }),
|
|
|
|
Some(Constant { constant: rc, needed_resolution: rn })) =
|
|
|
|
(constant(cx, left), constant(cx, right)) {
|
|
|
|
op(lc, rc).map(|c|
|
|
|
|
Constant {
|
|
|
|
needed_resolution: ln || rn,
|
|
|
|
constant: c,
|
|
|
|
})
|
|
|
|
} else { None }
|
2015-08-13 02:25:44 -05:00
|
|
|
}
|
|
|
|
|
2015-08-13 03:45:30 -05:00
|
|
|
fn constant_short_circuit(cx: &Context, left: &Expr, right: &Expr, b: bool) ->
|
|
|
|
Option<Constant> {
|
2015-08-14 10:14:54 -05:00
|
|
|
constant(cx, left).and_then(|left|
|
|
|
|
if let &ConstantBool(lbool) = &left.constant {
|
|
|
|
if lbool == b {
|
|
|
|
Some(left)
|
|
|
|
} else {
|
|
|
|
constant(cx, right).and_then(|right|
|
|
|
|
if let ConstantBool(_) = right.constant {
|
|
|
|
Some(Constant {
|
|
|
|
constant: right.constant,
|
|
|
|
needed_resolution: left.needed_resolution ||
|
|
|
|
right.needed_resolution,
|
|
|
|
})
|
|
|
|
} else { None }
|
|
|
|
)
|
|
|
|
}
|
|
|
|
} else { None }
|
|
|
|
)
|
2015-08-13 02:25:44 -05:00
|
|
|
}
|