2015-08-12 06:49:28 -05:00
|
|
|
use rustc::lint::Context;
|
|
|
|
use rustc::middle::const_eval::lookup_const_by_id;
|
|
|
|
use syntax::ast::*;
|
|
|
|
use syntax::ptr::P;
|
|
|
|
|
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 {
|
|
|
|
constant: ConstantVariant,
|
|
|
|
needed_resolution: bool
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Constant {
|
|
|
|
fn new(variant: ConstantVariant) -> Constant {
|
|
|
|
Constant { constant: variant, needed_resolution: false }
|
|
|
|
}
|
|
|
|
|
|
|
|
fn new_resolved(variant: ConstantVariant) -> Constant {
|
|
|
|
Constant { constant: variant, needed_resolution: true }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-12 06:49:28 -05:00
|
|
|
ConstantStr(&'static str, 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
|
|
|
|
ConstantFloat(Cow<'static, str>, FloatWidth),
|
|
|
|
/// 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-12 06:49:28 -05:00
|
|
|
ConstantVec(Vec<Constant>),
|
2015-08-13 02:25:44 -05:00
|
|
|
/// also an array, but with only one constant, repeated N times
|
|
|
|
ConstantRepeat(Constant, usize),
|
|
|
|
/// a tuple of constants
|
2015-08-12 06:49:28 -05:00
|
|
|
ConstantTuple(Vec<Constant>),
|
|
|
|
}
|
|
|
|
|
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-12 06:49:28 -05:00
|
|
|
match e {
|
2015-08-13 03:45:30 -05:00
|
|
|
&ExprParen(ref inner) => constant(cx, inner),
|
|
|
|
&ExprPath(_, _) => fetch_path(cx, e),
|
|
|
|
&ExprBlock(ref block) => constant_block(cx, inner),
|
2015-08-13 02:25:44 -05:00
|
|
|
&ExprIf(ref cond, ref then, ref otherwise) =>
|
2015-08-13 03:45:30 -05:00
|
|
|
constant_if(cx, cond, then, otherwise),
|
2015-08-12 06:49:28 -05:00
|
|
|
&ExprLit(ref lit) => Some(lit_to_constant(lit)),
|
2015-08-13 03:45:30 -05:00
|
|
|
&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-13 03:45:30 -05:00
|
|
|
constant_binop_apply(cx, value, number,|v, n| Constant {
|
|
|
|
constant: ConstantRepeat(v, n.constant.as_u64()),
|
|
|
|
needed_resolution: v.needed_resolution || n.needed_resolution
|
|
|
|
}),
|
|
|
|
&ExprUnary(op, ref operand) => constant(cx, operand).and_then(
|
2015-08-12 06:49:28 -05:00
|
|
|
|o| match op {
|
|
|
|
UnNot =>
|
2015-08-13 03:45:30 -05:00
|
|
|
if let ConstantBool(b) = o.variant {
|
|
|
|
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-12 06:49:28 -05:00
|
|
|
UnUniq | UnDeref => o,
|
|
|
|
}),
|
2015-08-13 02:25:44 -05:00
|
|
|
&ExprBinary(op, ref left, ref right) =>
|
2015-08-13 03:45:30 -05:00
|
|
|
constant_binop(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-13 03:45:30 -05:00
|
|
|
&LitStr(ref is, style) => Constant::new(ConstantStr(&*is, style)),
|
|
|
|
&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)),
|
|
|
|
&LitFloat(ref is, ty) =>
|
|
|
|
Constant::new(ConstantFloat(Cow::Borrowed(&*is), ty.into())),
|
2015-08-13 02:25:44 -05:00
|
|
|
&LitFloatUnsuffixed(InternedString) =>
|
2015-08-13 03:45:30 -05:00
|
|
|
Constant::new(ConstantFloat(Cow::Borrowed(&*is), FwAny)),
|
|
|
|
&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-13 03:45:30 -05:00
|
|
|
fn constant_vec(cx: &Context, vec: &[&Expr]) -> Option<Constant> {
|
|
|
|
let mut parts = Vec::new();
|
|
|
|
let mut resolved = false;
|
2015-08-12 06:49:28 -05:00
|
|
|
for opt_part in vec {
|
2015-08-13 03:45:30 -05:00
|
|
|
match constant(cx, opt_part) {
|
|
|
|
Some(ref p) => {
|
|
|
|
resolved |= p.needed_resolution;
|
|
|
|
parts.push(p)
|
|
|
|
},
|
2015-08-12 06:49:28 -05:00
|
|
|
None => { return None; },
|
|
|
|
}
|
|
|
|
}
|
2015-08-13 03:45:30 -05:00
|
|
|
Some(Constant {
|
|
|
|
constant: ConstantVec(parts),
|
|
|
|
needed_resolution: resolved
|
|
|
|
})
|
2015-08-12 06:49:28 -05:00
|
|
|
}
|
|
|
|
|
2015-08-13 03:45:30 -05:00
|
|
|
fn constant_tup(cx: &Context, tup: &[&Expr]) -> Option<Constant> {
|
|
|
|
let mut parts = Vec::new();
|
|
|
|
let mut resolved = false;
|
2015-08-12 06:49:28 -05:00
|
|
|
for opt_part in vec {
|
2015-08-13 03:45:30 -05:00
|
|
|
match constant(cx, opt_part) {
|
|
|
|
Some(ref p) => {
|
|
|
|
resolved |= p.needed_resolution;
|
|
|
|
parts.push(p)
|
|
|
|
},
|
2015-08-12 06:49:28 -05:00
|
|
|
None => { return None; },
|
|
|
|
}
|
|
|
|
}
|
2015-08-13 03:45:30 -05:00
|
|
|
Some(Constant {
|
|
|
|
constant: ConstantTuple(parts),
|
|
|
|
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-13 03:45:30 -05:00
|
|
|
lookup_const_by_id(cx.tcx, id, None).map(
|
|
|
|
|l| Constant::new_resolved(constant(cx, l).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-13 03:45:30 -05:00
|
|
|
block.expr.map(|b| constant(cx, b))
|
|
|
|
} else { None }
|
|
|
|
}
|
|
|
|
|
|
|
|
fn constant_if(cx: &Context, cond: &Expr, then: &Expr, otherwise: &Expr) ->
|
|
|
|
Option<Constant> {
|
|
|
|
if let Some(Constant{ constant: ConstantBool(b), needed_resolution: res }) =
|
|
|
|
constant(cx, cond) {
|
|
|
|
let part = constant(cx, if b { then } else { otherwise });
|
|
|
|
Some(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 {
|
|
|
|
&ConstantInt(value, ty) =>
|
|
|
|
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) => ConstantFloat(neg_float_str(is), ty),
|
|
|
|
_ => { 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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn neg_float_str(s: &InternedString) -> Cow<'static, str> {
|
2015-08-13 02:25:44 -05:00
|
|
|
if s.startsWith('-') {
|
2015-08-12 06:49:28 -05:00
|
|
|
Cow::Borrowed(s[1..])
|
|
|
|
} else {
|
|
|
|
Cow::Owned(format!("-{}", &*s))
|
|
|
|
}
|
|
|
|
}
|
2015-08-13 02:25:44 -05:00
|
|
|
|
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 {
|
|
|
|
//BiAdd,
|
|
|
|
//BiSub,
|
|
|
|
//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 03:45:30 -05:00
|
|
|
fn constant_binop_apply<F>(cx: &Context, left: &Expr, right: &Expr, op: F)
|
|
|
|
-> Option<Constant>
|
|
|
|
where F: FnMut(ConstantVariant, ConstantVariant) -> Option<ConstantVariant> {
|
|
|
|
constant(cx, left).and_then(|l| constant(cx, right).and_then(
|
|
|
|
|r| Constant {
|
|
|
|
needed_resolution: l.needed_resolution || r.needed_resolution,
|
|
|
|
constant: op(l.constant, r.constant)
|
|
|
|
}))
|
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> {
|
|
|
|
let leftconst = constant(cx, left);
|
|
|
|
if let ConstantBool(lbool) = leftconst.constant {
|
2015-08-13 02:25:44 -05:00
|
|
|
if l == b {
|
2015-08-13 03:45:30 -05:00
|
|
|
Some(leftconst)
|
2015-08-13 02:25:44 -05:00
|
|
|
} else {
|
2015-08-13 03:45:30 -05:00
|
|
|
let rightconst = constant(cx, right);
|
|
|
|
if let ConstantBool(rbool) = rightconst.constant {
|
|
|
|
Some(Constant {
|
|
|
|
constant: rightconst.constant,
|
|
|
|
needed_resolution: leftconst.needed_resolution ||
|
|
|
|
rightconst.needed_resolution,
|
|
|
|
})
|
2015-08-13 02:25:44 -05:00
|
|
|
} else { None }
|
|
|
|
}
|
|
|
|
} else { None }
|
|
|
|
}
|