2015-08-26 23:10:01 +00:00
|
|
|
#![allow(cast_possible_truncation)]
|
|
|
|
|
2015-09-19 02:53:04 +00:00
|
|
|
use rustc::lint::LateContext;
|
2015-08-12 11:49:28 +00:00
|
|
|
use rustc::middle::const_eval::lookup_const_by_id;
|
2015-08-14 15:14:54 +00:00
|
|
|
use rustc::middle::def::PathResolution;
|
|
|
|
use rustc::middle::def::Def::*;
|
2015-09-03 14:42:17 +00:00
|
|
|
use rustc_front::hir::*;
|
2015-08-12 11:49:28 +00:00
|
|
|
use syntax::ptr::P;
|
2015-10-26 06:43:38 +00:00
|
|
|
use std::char;
|
2015-08-17 11:18:14 +00:00
|
|
|
use std::cmp::PartialOrd;
|
|
|
|
use std::cmp::Ordering::{self, Greater, Less, Equal};
|
2015-08-14 15:14:54 +00:00
|
|
|
use std::rc::Rc;
|
|
|
|
use std::ops::Deref;
|
2015-10-26 06:43:38 +00:00
|
|
|
use std::fmt;
|
2015-08-17 15:51:30 +00:00
|
|
|
use self::Constant::*;
|
2015-08-14 15:14:54 +00:00
|
|
|
use self::FloatWidth::*;
|
|
|
|
|
2015-09-17 00:01:41 +00:00
|
|
|
use syntax::ast::Lit_::*;
|
|
|
|
use syntax::ast::Lit_;
|
|
|
|
use syntax::ast::LitIntType::*;
|
|
|
|
use syntax::ast::LitIntType;
|
|
|
|
use syntax::ast::{UintTy, FloatTy, StrStyle};
|
|
|
|
use syntax::ast::FloatTy::*;
|
|
|
|
use syntax::ast::Sign::{self, Plus, Minus};
|
|
|
|
|
|
|
|
|
2015-08-14 15:14:54 +00:00
|
|
|
#[derive(PartialEq, Eq, Debug, Copy, Clone)]
|
2015-08-13 07:25:44 +00:00
|
|
|
pub enum FloatWidth {
|
|
|
|
Fw32,
|
|
|
|
Fw64,
|
2016-01-04 04:26:12 +00:00
|
|
|
FwAny,
|
2015-08-13 07:25:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2015-08-17 11:18:14 +00:00
|
|
|
#[derive(Eq, Debug, Clone)]
|
2015-08-17 15:51:30 +00:00
|
|
|
pub enum Constant {
|
2015-08-13 07:25:44 +00:00
|
|
|
/// a String "abc"
|
2015-08-14 15:14:54 +00:00
|
|
|
ConstantStr(String, 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
|
2015-08-14 15:14:54 +00:00
|
|
|
ConstantFloat(String, FloatWidth),
|
2015-08-13 07:25:44 +00:00
|
|
|
/// 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-16 14:05:51 +00:00
|
|
|
ConstantVec(Vec<Constant>),
|
2015-08-13 07:25:44 +00:00
|
|
|
/// also an array, but with only one constant, repeated N times
|
2015-08-17 15:51:30 +00:00
|
|
|
ConstantRepeat(Box<Constant>, usize),
|
2015-08-13 07:25:44 +00:00
|
|
|
/// a tuple of constants
|
2015-08-16 14:05:51 +00:00
|
|
|
ConstantTuple(Vec<Constant>),
|
2015-08-12 11:49:28 +00:00
|
|
|
}
|
|
|
|
|
2015-08-17 15:51:30 +00:00
|
|
|
impl Constant {
|
2015-08-13 08:45:30 +00:00
|
|
|
/// convert to u64 if possible
|
|
|
|
///
|
|
|
|
/// # panics
|
|
|
|
///
|
|
|
|
/// if the constant could not be converted to u64 losslessly
|
|
|
|
fn as_u64(&self) -> u64 {
|
2015-08-18 12:18:36 +00:00
|
|
|
if let ConstantInt(val, _) = *self {
|
2015-08-13 08:45:30 +00:00
|
|
|
val // TODO we may want to check the sign if any
|
|
|
|
} else {
|
2015-12-23 21:36:37 +00:00
|
|
|
panic!("Could not convert a {:?} to u64", self);
|
2015-08-13 08:45:30 +00:00
|
|
|
}
|
|
|
|
}
|
2015-08-17 15:51:30 +00:00
|
|
|
|
|
|
|
/// convert this constant to a f64, if possible
|
2015-08-20 12:25:08 +00:00
|
|
|
#[allow(cast_precision_loss)]
|
2015-08-19 22:04:01 +00:00
|
|
|
pub fn as_float(&self) -> Option<f64> {
|
|
|
|
match *self {
|
|
|
|
ConstantByte(b) => Some(b as f64),
|
|
|
|
ConstantFloat(ref s, _) => s.parse().ok(),
|
2016-01-04 04:26:12 +00:00
|
|
|
ConstantInt(i, ty) => {
|
|
|
|
Some(if is_negative(ty) {
|
|
|
|
-(i as f64)
|
|
|
|
} else {
|
|
|
|
i as f64
|
|
|
|
})
|
|
|
|
}
|
|
|
|
_ => None,
|
2015-08-19 22:04:01 +00:00
|
|
|
}
|
|
|
|
}
|
2015-08-13 08:45:30 +00:00
|
|
|
}
|
|
|
|
|
2015-08-17 15:51:30 +00:00
|
|
|
impl PartialEq for Constant {
|
|
|
|
fn eq(&self, other: &Constant) -> bool {
|
2015-08-17 11:18:14 +00:00
|
|
|
match (self, other) {
|
2016-01-04 04:26:12 +00:00
|
|
|
(&ConstantStr(ref ls, ref lsty), &ConstantStr(ref rs, ref rsty)) => ls == rs && lsty == rsty,
|
2015-08-17 14:45:50 +00:00
|
|
|
(&ConstantBinary(ref l), &ConstantBinary(ref r)) => l == r,
|
2015-08-17 11:18:14 +00:00
|
|
|
(&ConstantByte(l), &ConstantByte(r)) => l == r,
|
|
|
|
(&ConstantChar(l), &ConstantChar(r)) => l == r,
|
2016-01-04 04:26:12 +00:00
|
|
|
(&ConstantInt(lv, lty), &ConstantInt(rv, rty)) => {
|
|
|
|
lv == rv && (is_negative(lty) & (lv != 0)) == (is_negative(rty) & (rv != 0))
|
|
|
|
}
|
|
|
|
(&ConstantFloat(ref ls, lw), &ConstantFloat(ref rs, rw)) => {
|
2015-08-17 11:18:14 +00:00
|
|
|
if match (lw, rw) {
|
|
|
|
(FwAny, _) | (_, FwAny) | (Fw32, Fw32) | (Fw64, Fw64) => true,
|
|
|
|
_ => false,
|
|
|
|
} {
|
2015-08-17 11:23:17 +00:00
|
|
|
match (ls.parse::<f64>(), rs.parse::<f64>()) {
|
|
|
|
(Ok(l), Ok(r)) => l.eq(&r),
|
2015-08-17 11:18:14 +00:00
|
|
|
_ => false,
|
|
|
|
}
|
2016-01-04 04:26:12 +00:00
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
2015-08-17 11:18:14 +00:00
|
|
|
(&ConstantBool(l), &ConstantBool(r)) => l == r,
|
|
|
|
(&ConstantVec(ref l), &ConstantVec(ref r)) => l == r,
|
2016-01-04 04:26:12 +00:00
|
|
|
(&ConstantRepeat(ref lv, ref ls), &ConstantRepeat(ref rv, ref rs)) => ls == rs && lv == rv,
|
2015-08-17 11:18:14 +00:00
|
|
|
(&ConstantTuple(ref l), &ConstantTuple(ref r)) => l == r,
|
|
|
|
_ => false, //TODO: Are there inter-type equalities?
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-17 15:51:30 +00:00
|
|
|
impl PartialOrd for Constant {
|
|
|
|
fn partial_cmp(&self, other: &Constant) -> Option<Ordering> {
|
2015-08-17 11:18:14 +00:00
|
|
|
match (self, other) {
|
2016-01-04 04:26:12 +00:00
|
|
|
(&ConstantStr(ref ls, ref lsty), &ConstantStr(ref rs, ref rsty)) => {
|
|
|
|
if lsty != rsty {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(ls.cmp(rs))
|
|
|
|
}
|
|
|
|
}
|
2015-08-17 11:18:14 +00:00
|
|
|
(&ConstantByte(ref l), &ConstantByte(ref r)) => Some(l.cmp(r)),
|
|
|
|
(&ConstantChar(ref l), &ConstantChar(ref r)) => Some(l.cmp(r)),
|
2016-01-04 04:26:12 +00:00
|
|
|
(&ConstantInt(ref lv, lty), &ConstantInt(ref rv, rty)) => {
|
|
|
|
Some(match (is_negative(lty) && *lv != 0, is_negative(rty) && *rv != 0) {
|
2015-09-05 10:46:34 +00:00
|
|
|
(true, true) => rv.cmp(lv),
|
|
|
|
(false, false) => lv.cmp(rv),
|
|
|
|
(true, false) => Less,
|
|
|
|
(false, true) => Greater,
|
2016-01-04 04:26:12 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
(&ConstantFloat(ref ls, lw), &ConstantFloat(ref rs, rw)) => {
|
2015-08-17 11:18:14 +00:00
|
|
|
if match (lw, rw) {
|
|
|
|
(FwAny, _) | (_, FwAny) | (Fw32, Fw32) | (Fw64, Fw64) => true,
|
|
|
|
_ => false,
|
|
|
|
} {
|
|
|
|
match (ls.parse::<f64>(), rs.parse::<f64>()) {
|
|
|
|
(Ok(ref l), Ok(ref r)) => l.partial_cmp(r),
|
|
|
|
_ => None,
|
|
|
|
}
|
2016-01-04 04:26:12 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
2015-08-17 11:18:14 +00:00
|
|
|
(&ConstantBool(ref l), &ConstantBool(ref r)) => Some(l.cmp(r)),
|
|
|
|
(&ConstantVec(ref l), &ConstantVec(ref r)) => l.partial_cmp(&r),
|
2016-01-04 04:26:12 +00:00
|
|
|
(&ConstantRepeat(ref lv, ref ls), &ConstantRepeat(ref rv, ref rs)) => {
|
2015-08-17 11:18:14 +00:00
|
|
|
match lv.partial_cmp(rv) {
|
|
|
|
Some(Equal) => Some(ls.cmp(rs)),
|
|
|
|
x => x,
|
2016-01-04 04:26:12 +00:00
|
|
|
}
|
|
|
|
}
|
2015-08-17 11:18:14 +00:00
|
|
|
(&ConstantTuple(ref l), &ConstantTuple(ref r)) => l.partial_cmp(r),
|
2016-01-04 04:26:12 +00:00
|
|
|
_ => None, //TODO: Are there any useful inter-type orderings?
|
|
|
|
}
|
2015-08-17 11:18:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-26 06:43:38 +00:00
|
|
|
fn format_byte(fmt: &mut fmt::Formatter, b: u8) -> fmt::Result {
|
|
|
|
if b == b'\\' {
|
|
|
|
write!(fmt, "\\\\")
|
|
|
|
} else if 0x20 <= b && b <= 0x7e {
|
|
|
|
write!(fmt, "{}", char::from_u32(b as u32).expect("all u8 are valid char"))
|
|
|
|
} else if b == 0x0a {
|
|
|
|
write!(fmt, "\\n")
|
|
|
|
} else if b == 0x0d {
|
|
|
|
write!(fmt, "\\r")
|
|
|
|
} else {
|
|
|
|
write!(fmt, "\\x{:02x}", b)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for Constant {
|
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
match *self {
|
|
|
|
ConstantStr(ref s, _) => write!(fmt, "{:?}", s),
|
2016-01-04 04:26:12 +00:00
|
|
|
ConstantByte(ref b) => {
|
|
|
|
write!(fmt, "b'")
|
|
|
|
.and_then(|_| format_byte(fmt, *b))
|
|
|
|
.and_then(|_| write!(fmt, "'"))
|
|
|
|
}
|
2015-10-26 06:43:38 +00:00
|
|
|
ConstantBinary(ref bs) => {
|
|
|
|
try!(write!(fmt, "b\""));
|
|
|
|
for b in bs.iter() {
|
|
|
|
try!(format_byte(fmt, *b));
|
|
|
|
}
|
|
|
|
write!(fmt, "\"")
|
|
|
|
}
|
|
|
|
ConstantChar(ref c) => write!(fmt, "'{}'", c),
|
|
|
|
ConstantInt(ref i, ref ity) => {
|
|
|
|
let (sign, suffix) = match *ity {
|
2016-01-04 04:26:12 +00:00
|
|
|
LitIntType::SignedIntLit(ref sity, ref sign) => {
|
|
|
|
(if let Sign::Minus = *sign {
|
|
|
|
"-"
|
|
|
|
} else {
|
|
|
|
""
|
|
|
|
},
|
|
|
|
sity.ty_to_string())
|
|
|
|
}
|
|
|
|
LitIntType::UnsignedIntLit(ref uity) => ("", uity.ty_to_string()),
|
|
|
|
LitIntType::UnsuffixedIntLit(ref sign) => {
|
|
|
|
(if let Sign::Minus = *sign {
|
|
|
|
"-"
|
|
|
|
} else {
|
|
|
|
""
|
|
|
|
},
|
|
|
|
"".into())
|
|
|
|
}
|
2015-10-26 06:43:38 +00:00
|
|
|
};
|
|
|
|
write!(fmt, "{}{}{}", sign, i, suffix)
|
|
|
|
}
|
|
|
|
ConstantFloat(ref s, ref fw) => {
|
|
|
|
let suffix = match *fw {
|
|
|
|
FloatWidth::Fw32 => "f32",
|
|
|
|
FloatWidth::Fw64 => "f64",
|
|
|
|
FloatWidth::FwAny => "",
|
|
|
|
};
|
|
|
|
write!(fmt, "{}{}", s, suffix)
|
|
|
|
}
|
|
|
|
ConstantBool(ref b) => write!(fmt, "{}", b),
|
|
|
|
ConstantRepeat(ref c, ref n) => write!(fmt, "[{}; {}]", c, n),
|
2016-01-04 04:26:12 +00:00
|
|
|
ConstantVec(ref v) => {
|
|
|
|
write!(fmt,
|
|
|
|
"[{}]",
|
|
|
|
v.iter()
|
|
|
|
.map(|i| format!("{}", i))
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
.join(", "))
|
|
|
|
}
|
|
|
|
ConstantTuple(ref t) => {
|
|
|
|
write!(fmt,
|
|
|
|
"({})",
|
|
|
|
t.iter()
|
|
|
|
.map(|i| format!("{}", i))
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
.join(", "))
|
|
|
|
}
|
2015-10-26 06:43:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-08-17 15:51:30 +00:00
|
|
|
|
2015-08-12 11:49:28 +00:00
|
|
|
|
|
|
|
fn lit_to_constant(lit: &Lit_) -> Constant {
|
2015-08-18 12:18:36 +00:00
|
|
|
match *lit {
|
|
|
|
LitStr(ref is, style) => ConstantStr(is.to_string(), style),
|
|
|
|
LitByte(b) => ConstantByte(b),
|
2015-09-05 11:15:18 +00:00
|
|
|
LitByteStr(ref s) => ConstantBinary(s.clone()),
|
2015-08-18 12:18:36 +00:00
|
|
|
LitChar(c) => ConstantChar(c),
|
|
|
|
LitInt(value, ty) => ConstantInt(value, ty),
|
|
|
|
LitFloat(ref is, ty) => ConstantFloat(is.to_string(), ty.into()),
|
|
|
|
LitFloatUnsuffixed(ref is) => ConstantFloat(is.to_string(), FwAny),
|
|
|
|
LitBool(b) => ConstantBool(b),
|
2015-08-12 11:49:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-17 14:45:50 +00:00
|
|
|
fn constant_not(o: Constant) -> Option<Constant> {
|
2015-08-17 15:51:30 +00:00
|
|
|
Some(match o {
|
|
|
|
ConstantBool(b) => ConstantBool(!b),
|
|
|
|
ConstantInt(value, ty) => {
|
|
|
|
let (nvalue, nty) = match ty {
|
|
|
|
SignedIntLit(ity, Plus) => {
|
2016-01-04 04:26:12 +00:00
|
|
|
if value == ::std::u64::MAX {
|
|
|
|
return None;
|
|
|
|
}
|
2015-08-17 15:51:30 +00:00
|
|
|
(value + 1, SignedIntLit(ity, Minus))
|
2015-11-17 04:39:42 +00:00
|
|
|
}
|
2015-08-17 15:51:30 +00:00
|
|
|
SignedIntLit(ity, Minus) => {
|
|
|
|
if value == 0 {
|
|
|
|
(1, SignedIntLit(ity, Minus))
|
|
|
|
} else {
|
|
|
|
(value - 1, SignedIntLit(ity, Plus))
|
2015-08-17 14:45:50 +00:00
|
|
|
}
|
2015-08-17 15:51:30 +00:00
|
|
|
}
|
|
|
|
UnsignedIntLit(ity) => {
|
|
|
|
let mask = match ity {
|
|
|
|
UintTy::TyU8 => ::std::u8::MAX as u64,
|
|
|
|
UintTy::TyU16 => ::std::u16::MAX as u64,
|
|
|
|
UintTy::TyU32 => ::std::u32::MAX as u64,
|
|
|
|
UintTy::TyU64 => ::std::u64::MAX,
|
2016-01-04 04:26:12 +00:00
|
|
|
UintTy::TyUs => {
|
|
|
|
return None;
|
|
|
|
} // refuse to guess
|
2015-08-17 15:51:30 +00:00
|
|
|
};
|
|
|
|
(!value & mask, UnsignedIntLit(ity))
|
|
|
|
}
|
2016-01-04 04:26:12 +00:00
|
|
|
UnsuffixedIntLit(_) => {
|
|
|
|
return None;
|
|
|
|
} // refuse to guess
|
2015-08-17 15:51:30 +00:00
|
|
|
};
|
|
|
|
ConstantInt(nvalue, nty)
|
2015-11-17 04:39:42 +00:00
|
|
|
}
|
2016-01-04 04:26:12 +00:00
|
|
|
_ => {
|
|
|
|
return None;
|
|
|
|
}
|
2015-08-17 14:45:50 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-08-13 07:25:44 +00:00
|
|
|
fn constant_negate(o: Constant) -> Option<Constant> {
|
2015-08-17 15:51:30 +00:00
|
|
|
Some(match o {
|
2016-01-04 04:26:12 +00:00
|
|
|
ConstantInt(value, ty) => {
|
|
|
|
ConstantInt(value,
|
|
|
|
match ty {
|
|
|
|
SignedIntLit(ity, sign) => SignedIntLit(ity, neg_sign(sign)),
|
|
|
|
UnsuffixedIntLit(sign) => UnsuffixedIntLit(neg_sign(sign)),
|
|
|
|
_ => {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
ConstantFloat(is, ty) => ConstantFloat(neg_float_str(is), ty),
|
|
|
|
_ => {
|
|
|
|
return None;
|
|
|
|
}
|
2015-08-13 08:45:30 +00:00
|
|
|
})
|
2015-08-13 07:25:44 +00:00
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-14 15:14:54 +00:00
|
|
|
fn neg_float_str(s: String) -> String {
|
|
|
|
if s.starts_with('-') {
|
|
|
|
s[1..].to_owned()
|
2015-08-12 11:49:28 +00:00
|
|
|
} else {
|
2015-08-25 12:41:35 +00:00
|
|
|
format!("-{}", s)
|
2015-08-12 11:49:28 +00:00
|
|
|
}
|
|
|
|
}
|
2015-08-13 07:25:44 +00:00
|
|
|
|
2015-08-16 21:09:56 +00:00
|
|
|
/// is the given LitIntType negative?
|
|
|
|
///
|
|
|
|
/// Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// assert!(is_negative(UnsuffixedIntLit(Minus)));
|
|
|
|
/// ```
|
|
|
|
pub fn is_negative(ty: LitIntType) -> bool {
|
2015-08-13 12:22:05 +00:00
|
|
|
match ty {
|
|
|
|
SignedIntLit(_, sign) | UnsuffixedIntLit(sign) => sign == Minus,
|
|
|
|
UnsignedIntLit(_) => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-14 15:14:54 +00:00
|
|
|
fn unify_int_type(l: LitIntType, r: LitIntType, s: Sign) -> Option<LitIntType> {
|
2015-08-13 12:22:05 +00:00
|
|
|
match (l, r) {
|
2016-01-04 04:26:12 +00:00
|
|
|
(SignedIntLit(lty, _), SignedIntLit(rty, _)) => {
|
|
|
|
if lty == rty {
|
|
|
|
Some(SignedIntLit(lty, s))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
(UnsignedIntLit(lty), UnsignedIntLit(rty)) => {
|
2015-08-14 15:14:54 +00:00
|
|
|
if s == Plus && lty == rty {
|
2015-08-13 12:22:05 +00:00
|
|
|
Some(UnsignedIntLit(lty))
|
2016-01-04 04:26:12 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
2015-08-14 15:14:54 +00:00
|
|
|
(UnsuffixedIntLit(_), UnsuffixedIntLit(_)) => Some(UnsuffixedIntLit(s)),
|
|
|
|
(SignedIntLit(lty, _), UnsuffixedIntLit(_)) => Some(SignedIntLit(lty, s)),
|
2016-01-04 04:26:12 +00:00
|
|
|
(UnsignedIntLit(lty), UnsuffixedIntLit(rs)) => {
|
|
|
|
if rs == Plus {
|
|
|
|
Some(UnsignedIntLit(lty))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
2015-08-14 15:14:54 +00:00
|
|
|
(UnsuffixedIntLit(_), SignedIntLit(rty, _)) => Some(SignedIntLit(rty, s)),
|
2016-01-04 04:26:12 +00:00
|
|
|
(UnsuffixedIntLit(ls), UnsignedIntLit(rty)) => {
|
|
|
|
if ls == Plus {
|
|
|
|
Some(UnsignedIntLit(rty))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
2015-08-13 12:22:05 +00:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-04 04:26:12 +00:00
|
|
|
fn add_neg_int(pos: u64, pty: LitIntType, neg: u64, nty: LitIntType) -> Option<Constant> {
|
2015-08-13 12:22:05 +00: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))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-04 04:26:12 +00:00
|
|
|
fn sub_int(l: u64, lty: LitIntType, r: u64, rty: LitIntType, neg: bool) -> Option<Constant> {
|
|
|
|
unify_int_type(lty,
|
|
|
|
rty,
|
|
|
|
if neg {
|
|
|
|
Minus
|
|
|
|
} else {
|
|
|
|
Plus
|
|
|
|
})
|
|
|
|
.and_then(|ty| l.checked_sub(r).map(|v| ConstantInt(v, ty)))
|
2015-08-13 12:22:05 +00:00
|
|
|
}
|
|
|
|
|
2015-08-17 15:51:30 +00:00
|
|
|
|
2015-09-19 02:53:04 +00:00
|
|
|
pub fn constant(lcx: &LateContext, e: &Expr) -> Option<(Constant, bool)> {
|
2016-01-04 04:26:12 +00:00
|
|
|
let mut cx = ConstEvalLateContext {
|
|
|
|
lcx: Some(lcx),
|
|
|
|
needed_resolution: false,
|
|
|
|
};
|
2015-08-17 15:51:30 +00:00
|
|
|
cx.expr(e).map(|cst| (cst, cx.needed_resolution))
|
2015-08-13 07:25:44 +00:00
|
|
|
}
|
|
|
|
|
2015-08-17 15:51:30 +00:00
|
|
|
pub fn constant_simple(e: &Expr) -> Option<Constant> {
|
2016-01-04 04:26:12 +00:00
|
|
|
let mut cx = ConstEvalLateContext {
|
|
|
|
lcx: None,
|
|
|
|
needed_resolution: false,
|
|
|
|
};
|
2015-08-17 15:51:30 +00:00
|
|
|
cx.expr(e)
|
|
|
|
}
|
|
|
|
|
2015-09-19 02:53:04 +00:00
|
|
|
struct ConstEvalLateContext<'c, 'cc: 'c> {
|
|
|
|
lcx: Option<&'c LateContext<'c, 'cc>>,
|
2016-01-04 04:26:12 +00:00
|
|
|
needed_resolution: bool,
|
2015-08-17 15:51:30 +00:00
|
|
|
}
|
|
|
|
|
2015-09-19 02:53:04 +00:00
|
|
|
impl<'c, 'cc> ConstEvalLateContext<'c, 'cc> {
|
2015-08-17 15:51:30 +00:00
|
|
|
/// simple constant folding: Insert an expression, get a constant or none.
|
|
|
|
fn expr(&mut self, e: &Expr) -> Option<Constant> {
|
2015-08-17 17:55:59 +00:00
|
|
|
match e.node {
|
|
|
|
ExprPath(_, _) => self.fetch_path(e),
|
|
|
|
ExprBlock(ref block) => self.block(block),
|
2016-01-04 04:26:12 +00:00
|
|
|
ExprIf(ref cond, ref then, ref otherwise) => self.ifthenelse(cond, then, otherwise),
|
2015-08-17 17:55:59 +00:00
|
|
|
ExprLit(ref lit) => Some(lit_to_constant(&lit.node)),
|
2015-08-18 12:18:36 +00:00
|
|
|
ExprVec(ref vec) => self.multi(vec).map(ConstantVec),
|
|
|
|
ExprTup(ref tup) => self.multi(tup).map(ConstantTuple),
|
2016-01-04 04:26:12 +00:00
|
|
|
ExprRepeat(ref value, ref number) => {
|
|
|
|
self.binop_apply(value, number, |v, n| Some(ConstantRepeat(Box::new(v), n.as_u64() as usize)))
|
|
|
|
}
|
|
|
|
ExprUnary(op, ref operand) => {
|
|
|
|
self.expr(operand).and_then(|o| {
|
|
|
|
match op {
|
|
|
|
UnNot => constant_not(o),
|
|
|
|
UnNeg => constant_negate(o),
|
|
|
|
UnDeref => Some(o),
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
ExprBinary(op, ref left, ref right) => self.binop(op, left, right),
|
|
|
|
// TODO: add other expressions
|
2015-08-17 15:51:30 +00:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-18 10:26:01 +00:00
|
|
|
/// create `Some(Vec![..])` of all constants, unless there is any
|
2015-08-17 15:51:30 +00:00
|
|
|
/// non-constant part
|
2016-01-04 04:26:12 +00:00
|
|
|
fn multi<E: Deref<Target = Expr> + Sized>(&mut self, vec: &[E]) -> Option<Vec<Constant>> {
|
|
|
|
vec.iter()
|
|
|
|
.map(|elem| self.expr(elem))
|
|
|
|
.collect::<Option<_>>()
|
2015-08-17 15:51:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// lookup a possibly constant expression from a ExprPath
|
|
|
|
fn fetch_path(&mut self, e: &Expr) -> Option<Constant> {
|
|
|
|
if let Some(lcx) = self.lcx {
|
2015-08-22 07:31:54 +00:00
|
|
|
let mut maybe_id = None;
|
2016-01-04 04:26:12 +00:00
|
|
|
if let Some(&PathResolution { base_def: DefConst(id), ..}) = lcx.tcx.def_map.borrow().get(&e.id) {
|
2015-08-22 07:31:54 +00:00
|
|
|
maybe_id = Some(id);
|
|
|
|
}
|
2016-01-18 12:09:46 +00:00
|
|
|
// separate if lets to avoid double borrowing the def_map
|
2015-08-22 07:31:54 +00:00
|
|
|
if let Some(id) = maybe_id {
|
2015-08-17 15:51:30 +00:00
|
|
|
if let Some(const_expr) = lookup_const_by_id(lcx.tcx, id, None) {
|
|
|
|
let ret = self.expr(const_expr);
|
|
|
|
if ret.is_some() {
|
|
|
|
self.needed_resolution = true;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A block can only yield a constant if it only has one constant expression
|
|
|
|
fn block(&mut self, block: &Block) -> Option<Constant> {
|
|
|
|
if block.stmts.is_empty() {
|
2015-08-17 17:55:59 +00:00
|
|
|
block.expr.as_ref().and_then(|ref b| self.expr(b))
|
2016-01-04 04:26:12 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2015-08-17 15:51:30 +00:00
|
|
|
}
|
|
|
|
|
2016-01-04 04:26:12 +00:00
|
|
|
fn ifthenelse(&mut self, cond: &Expr, then: &Block, otherwise: &Option<P<Expr>>) -> Option<Constant> {
|
2015-08-17 15:51:30 +00:00
|
|
|
if let Some(ConstantBool(b)) = self.expr(cond) {
|
|
|
|
if b {
|
|
|
|
self.block(then)
|
2015-08-14 15:14:54 +00:00
|
|
|
} else {
|
2015-08-25 12:41:35 +00:00
|
|
|
otherwise.as_ref().and_then(|expr| self.expr(expr))
|
2015-08-14 15:14:54 +00:00
|
|
|
}
|
2016-01-04 04:26:12 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2015-08-17 15:51:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn binop(&mut self, op: BinOp, left: &Expr, right: &Expr) -> Option<Constant> {
|
|
|
|
match op.node {
|
2016-01-04 04:26:12 +00:00
|
|
|
BiAdd => {
|
|
|
|
self.binop_apply(left, right, |l, r| {
|
|
|
|
match (l, r) {
|
|
|
|
(ConstantByte(l8), ConstantByte(r8)) => l8.checked_add(r8).map(ConstantByte),
|
|
|
|
(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)))
|
2015-08-17 15:51:30 +00:00
|
|
|
} else {
|
2016-01-04 04:26:12 +00:00
|
|
|
if ln {
|
|
|
|
add_neg_int(r64, rty, l64, lty)
|
|
|
|
} else {
|
|
|
|
add_neg_int(l64, lty, r64, rty)
|
|
|
|
}
|
2015-08-17 15:51:30 +00:00
|
|
|
}
|
|
|
|
}
|
2016-01-04 04:26:12 +00:00
|
|
|
// TODO: float (would need bignum library?)
|
|
|
|
_ => None,
|
2015-11-17 04:39:42 +00:00
|
|
|
}
|
2016-01-04 04:26:12 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
BiSub => {
|
|
|
|
self.binop_apply(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)) => {
|
|
|
|
match (is_negative(lty), is_negative(rty)) {
|
|
|
|
(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-19 09:58:59 +00:00
|
|
|
BiMul => self.divmul(left, right, u64::checked_mul),
|
|
|
|
BiDiv => self.divmul(left, right, u64::checked_div),
|
2016-01-04 04:26:12 +00:00
|
|
|
// BiRem,
|
2015-08-17 15:51:30 +00:00
|
|
|
BiAnd => self.short_circuit(left, right, false),
|
|
|
|
BiOr => self.short_circuit(left, right, true),
|
|
|
|
BiBitXor => self.bitop(left, right, |x, y| x ^ y),
|
|
|
|
BiBitAnd => self.bitop(left, right, |x, y| x & y),
|
|
|
|
BiBitOr => self.bitop(left, right, |x, y| (x | y)),
|
|
|
|
BiShl => self.bitop(left, right, |x, y| x << y),
|
|
|
|
BiShr => self.bitop(left, right, |x, y| x >> y),
|
2016-01-04 04:26:12 +00:00
|
|
|
BiEq => self.binop_apply(left, right, |l, r| Some(ConstantBool(l == r))),
|
|
|
|
BiNe => self.binop_apply(left, right, |l, r| Some(ConstantBool(l != r))),
|
2015-08-17 15:51:30 +00:00
|
|
|
BiLt => self.cmp(left, right, Less, true),
|
|
|
|
BiLe => self.cmp(left, right, Greater, false),
|
|
|
|
BiGe => self.cmp(left, right, Less, false),
|
|
|
|
BiGt => self.cmp(left, right, Greater, true),
|
2016-01-04 04:26:12 +00:00
|
|
|
_ => None,
|
2015-08-17 15:51:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-04 04:26:12 +00:00
|
|
|
fn divmul<F>(&mut self, left: &Expr, right: &Expr, f: F) -> Option<Constant>
|
|
|
|
where F: Fn(u64, u64) -> Option<u64>
|
|
|
|
{
|
|
|
|
self.binop_apply(left, right, |l, r| {
|
2015-08-19 09:58:59 +00:00
|
|
|
match (l, r) {
|
|
|
|
(ConstantInt(l64, lty), ConstantInt(r64, rty)) => {
|
2016-01-04 04:26:12 +00:00
|
|
|
f(l64, r64).and_then(|value| {
|
|
|
|
unify_int_type(lty,
|
|
|
|
rty,
|
|
|
|
if is_negative(lty) == is_negative(rty) {
|
|
|
|
Plus
|
|
|
|
} else {
|
|
|
|
Minus
|
|
|
|
})
|
|
|
|
.map(|ty| ConstantInt(value, ty))
|
|
|
|
})
|
2015-11-17 04:39:42 +00:00
|
|
|
}
|
2015-08-19 09:58:59 +00:00
|
|
|
_ => None,
|
2016-01-04 04:26:12 +00:00
|
|
|
}
|
|
|
|
})
|
2015-08-19 09:58:59 +00:00
|
|
|
}
|
|
|
|
|
2016-01-04 04:26:12 +00:00
|
|
|
fn bitop<F>(&mut self, left: &Expr, right: &Expr, f: F) -> Option<Constant>
|
|
|
|
where F: Fn(u64, u64) -> u64
|
|
|
|
{
|
|
|
|
self.binop_apply(left, right, |l, r| {
|
|
|
|
match (l, r) {
|
|
|
|
(ConstantBool(l), ConstantBool(r)) => Some(ConstantBool(f(l as u64, r as u64) != 0)),
|
|
|
|
(ConstantByte(l8), ConstantByte(r8)) => Some(ConstantByte(f(l8 as u64, r8 as u64) as u8)),
|
|
|
|
(ConstantInt(l, lty), ConstantInt(r, rty)) => {
|
|
|
|
unify_int_type(lty, rty, Plus).map(|ty| ConstantInt(f(l, r), ty))
|
|
|
|
}
|
|
|
|
_ => None,
|
|
|
|
}
|
2015-08-17 15:51:30 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn cmp(&mut self, left: &Expr, right: &Expr, ordering: Ordering, b: bool) -> Option<Constant> {
|
2016-01-04 04:26:12 +00:00
|
|
|
self.binop_apply(left,
|
|
|
|
right,
|
|
|
|
|l, r| l.partial_cmp(&r).map(|o| ConstantBool(b == (o == ordering))))
|
2015-08-17 15:51:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn binop_apply<F>(&mut self, left: &Expr, right: &Expr, op: F) -> Option<Constant>
|
2016-01-04 04:26:12 +00:00
|
|
|
where F: Fn(Constant, Constant) -> Option<Constant>
|
|
|
|
{
|
2015-08-17 15:51:30 +00:00
|
|
|
if let (Some(lc), Some(rc)) = (self.expr(left), self.expr(right)) {
|
|
|
|
op(lc, rc)
|
2016-01-04 04:26:12 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2015-08-17 15:51:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn short_circuit(&mut self, left: &Expr, right: &Expr, b: bool) -> Option<Constant> {
|
2016-01-04 04:26:12 +00:00
|
|
|
self.expr(left).and_then(|left| {
|
2015-08-17 17:55:59 +00:00
|
|
|
if let ConstantBool(lbool) = left {
|
2015-08-17 15:51:30 +00:00
|
|
|
if lbool == b {
|
|
|
|
Some(left)
|
|
|
|
} else {
|
2016-01-04 04:26:12 +00:00
|
|
|
self.expr(right).and_then(|right| {
|
2015-08-17 15:51:30 +00:00
|
|
|
if let ConstantBool(_) = right {
|
|
|
|
Some(right)
|
2016-01-04 04:26:12 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
2015-08-17 15:51:30 +00:00
|
|
|
}
|
2016-01-04 04:26:12 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
2015-08-17 15:51:30 +00:00
|
|
|
}
|
2015-08-13 07:25:44 +00:00
|
|
|
}
|