rust-clippy/src/const.rs

204 lines
6.3 KiB
Rust
Raw Normal View History

2015-08-12 11:49:28 +00:00
use rustc::lint::Context;
use rustc::middle::const_eval::lookup_const_by_id;
use syntax::ast::*;
use syntax::ptr::P;
2015-08-13 07:25:44 +00: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-12 11:49:28 +00:00
/// a Lit_-like enum to fold constant `Expr`s into
#[derive(PartialEq, Eq, Debug, Clone)]
pub enum Constant {
2015-08-13 07:25:44 +00:00
/// a String "abc"
2015-08-12 11:49:28 +00:00
ConstantStr(&'static str, StrStyle),
2015-08-13 07:25:44 +00:00
/// a Binary String b"abc"
2015-08-12 11:49:28 +00:00
ConstantBinary(Rc<Vec<u8>>),
2015-08-13 07:25:44 +00:00
/// a single byte b'a'
2015-08-12 11:49:28 +00:00
ConstantByte(u8),
2015-08-13 07:25:44 +00:00
/// a single char 'a'
2015-08-12 11:49:28 +00:00
ConstantChar(char),
2015-08-13 07:25:44 +00:00
/// an integer
2015-08-12 11:49:28 +00:00
ConstantInt(u64, LitIntType),
2015-08-13 07:25:44 +00:00
/// a float with given type
ConstantFloat(Cow<'static, str>, FloatWidth),
/// true or false
2015-08-12 11:49:28 +00:00
ConstantBool(bool),
2015-08-13 07:25:44 +00:00
/// an array of constants
2015-08-12 11:49:28 +00:00
ConstantVec(Vec<Constant>),
2015-08-13 07:25:44 +00:00
/// also an array, but with only one constant, repeated N times
ConstantRepeat(Constant, usize),
/// a tuple of constants
2015-08-12 11:49:28 +00:00
ConstantTuple(Vec<Constant>),
}
/// simple constant folding
2015-08-13 07:25:44 +00:00
pub fn constant(cx: &Context, e: &Expr, follow: bool) -> Option<Constant> {
2015-08-12 11:49:28 +00:00
match e {
2015-08-13 07:25:44 +00:00
&ExprParen(ref inner) => constant(cx, inner, follow),
&ExprPath(_, _) => if follow { fetch_path(cx, e) } else { None },
&ExprBlock(ref block) => constant_block(cx, inner, follow),
&ExprIf(ref cond, ref then, ref otherwise) =>
2015-08-12 11:49:28 +00:00
match constant(cx, cond) {
2015-08-13 07:25:44 +00:00
Some(ConstantBool(true)) => constant(cx, then, follow),
Some(ConstantBool(false)) => constant(cx, otherwise, follow),
2015-08-12 11:49:28 +00:00
_ => None,
},
&ExprLit(ref lit) => Some(lit_to_constant(lit)),
2015-08-13 07:25:44 +00:00
&ExprVec(ref vec) => constant_vec(cx, vec, follow),
&ExprTup(ref tup) => constant_tup(cx, tup, follow),
&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(
2015-08-12 11:49:28 +00:00
|o| match op {
UnNot =>
if let ConstantBool(b) = o {
Some(ConstantBool(!b))
} else { None },
2015-08-13 07:25:44 +00:00
UnNeg => constant_negate(o),
2015-08-12 11:49:28 +00:00
UnUniq | UnDeref => o,
}),
2015-08-13 07:25:44 +00:00
&ExprBinary(op, ref left, ref right) =>
constant_binop(cx, op, left, right, follow),
2015-08-12 11:49:28 +00:00
//TODO: add other expressions
_ => None,
}
}
fn lit_to_constant(lit: &Lit_) -> Constant {
match lit {
&LitStr(ref is, style) => ConstantStr(&*is, style),
&LitBinary(ref blob) => ConstantBinary(blob.clone()),
&LitByte(b) => ConstantByte(b),
&LitChar(c) => ConstantChar(c),
&LitInt(value, ty) => ConstantInt(value, ty),
2015-08-13 07:25:44 +00:00
&LitFloat(ref is, ty) => ConstantFloat(Cow::Borrowed(&*is), ty.into()),
&LitFloatUnsuffixed(InternedString) =>
ConstantFloat(Cow::Borrowed(&*is), FwAny),
2015-08-12 11:49:28 +00:00
&LitBool(b) => ConstantBool(b),
}
}
/// create `Some(ConstantVec(..))` of all constants, unless there is any
/// non-constant part
2015-08-13 07:25:44 +00:00
fn constant_vec(cx: &Context, vec: &[&Expr], follow: bool) -> Option<Constant> {
parts = Vec::new();
2015-08-12 11:49:28 +00:00
for opt_part in vec {
2015-08-13 07:25:44 +00:00
match constant(cx, opt_part, follow) {
2015-08-12 11:49:28 +00:00
Some(ref p) => parts.push(p),
None => { return None; },
}
}
Some(ConstantVec(parts))
}
2015-08-13 07:25:44 +00:00
fn constant_tup(cx: &Context, tup: &[&Expr], follow: bool) -> Option<Constant> {
parts = Vec::new();
2015-08-12 11:49:28 +00:00
for opt_part in vec {
2015-08-13 07:25:44 +00:00
match constant(cx, opt_part, follow) {
2015-08-12 11:49:28 +00:00
Some(ref p) => parts.push(p),
None => { return None; },
}
}
Some(ConstantTuple(parts))
}
/// 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) {
lookup_const_by_id(cx.tcx, id, None).map(|l| constant(cx, l))
} else { None }
}
/// A block can only yield a constant if it only has one constant expression
2015-08-13 07:25:44 +00:00
fn constant_block(cx: &Context, block: &Block, follow: bool) -> Option<Constant> {
2015-08-12 11:49:28 +00:00
if block.stmts.is_empty() {
2015-08-13 07:25:44 +00:00
block.expr.map(|b| constant(cx, b, follow))
2015-08-12 11:49:28 +00:00
} else { None }
}
2015-08-13 07:25:44 +00:00
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,
}
}
2015-08-12 11:49:28 +00:00
fn neg_sign(s: Sign) -> Sign {
2015-08-13 07:25:44 +00:00
match s {
2015-08-12 11:49:28 +00:00
Sign::Plus => Sign::Minus,
Sign::Minus => Sign::Plus,
}
}
fn neg_float_str(s: &InternedString) -> Cow<'static, str> {
2015-08-13 07:25:44 +00:00
if s.startsWith('-') {
2015-08-12 11:49:28 +00:00
Cow::Borrowed(s[1..])
} else {
Cow::Owned(format!("-{}", &*s))
}
}
2015-08-13 07:25:44 +00:00
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 }
}