1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
#![allow(cast_possible_truncation)]

use rustc::lint::LateContext;
use rustc::middle::const_eval::lookup_const_by_id;
use rustc::middle::def::PathResolution;
use rustc::middle::def::Def::*;
use rustc_front::hir::*;
use syntax::ptr::P;
use std::cmp::PartialOrd;
use std::cmp::Ordering::{self, Greater, Less, Equal};
use std::rc::Rc;
use std::ops::Deref;
use self::Constant::*;
use self::FloatWidth::*;

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::UintTy::*;
use syntax::ast::FloatTy::*;
use syntax::ast::Sign::{self, Plus, Minus};


#[derive(PartialEq, Eq, Debug, Copy, Clone)]
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
#[derive(Eq, Debug, Clone)]
pub enum Constant {
    /// a String "abc"
    ConstantStr(String, StrStyle),
    /// a Binary String b"abc"
    ConstantBinary(Rc<Vec<u8>>),
    /// a single byte b'a'
    ConstantByte(u8),
    /// a single char 'a'
    ConstantChar(char),
    /// an integer
    ConstantInt(u64, LitIntType),
    /// a float with given type
    ConstantFloat(String, FloatWidth),
    /// true or false
    ConstantBool(bool),
    /// an array of constants
    ConstantVec(Vec<Constant>),
    /// also an array, but with only one constant, repeated N times
    ConstantRepeat(Box<Constant>, usize),
    /// a tuple of constants
    ConstantTuple(Vec<Constant>),
}

impl Constant {
    /// 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");
        }
    }

    /// convert this constant to a f64, if possible
    #[allow(cast_precision_loss)]
    pub fn as_float(&self) -> Option<f64> {
        match *self {
            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
        }
    }
}

impl PartialEq for Constant {
    fn eq(&self, other: &Constant) -> bool {
        match (self, other) {
            (&ConstantStr(ref ls, ref lsty), &ConstantStr(ref rs, ref rsty)) =>
                ls == rs && lsty == rsty,
            (&ConstantBinary(ref l), &ConstantBinary(ref r)) => l == r,
            (&ConstantByte(l), &ConstantByte(r)) => l == r,
            (&ConstantChar(l), &ConstantChar(r)) => l == r,
            (&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)) =>
                if match (lw, rw) {
                    (FwAny, _) | (_, FwAny) | (Fw32, Fw32) | (Fw64, Fw64) => true,
                    _ => false,
                } {
                    match (ls.parse::<f64>(), rs.parse::<f64>()) {
                        (Ok(l), Ok(r)) => l.eq(&r),
                        _ => false,
                    }
                } else { false },
            (&ConstantBool(l), &ConstantBool(r)) => l == r,
            (&ConstantVec(ref l), &ConstantVec(ref r)) => l == r,
            (&ConstantRepeat(ref lv, ref ls), &ConstantRepeat(ref rv, ref rs)) =>
                ls == rs && lv == rv,
            (&ConstantTuple(ref l), &ConstantTuple(ref r)) => l == r,
            _ => false, //TODO: Are there inter-type equalities?
        }
    }
}

impl PartialOrd for Constant {
    fn partial_cmp(&self, other: &Constant) -> Option<Ordering> {
        match (self, other) {
            (&ConstantStr(ref ls, ref lsty), &ConstantStr(ref rs, ref rsty)) =>
                if lsty != rsty { None } else { Some(ls.cmp(rs)) },
            (&ConstantByte(ref l), &ConstantByte(ref r)) => Some(l.cmp(r)),
            (&ConstantChar(ref l), &ConstantChar(ref r)) => Some(l.cmp(r)),
            (&ConstantInt(ref lv, lty), &ConstantInt(ref rv, rty)) =>
                Some(match (is_negative(lty) && *lv != 0,
                            is_negative(rty) && *rv != 0) {
                    (true, true) => rv.cmp(lv),
                    (false, false) => lv.cmp(rv),
                    (true, false) => Less,
                    (false, true) => Greater,
                }),
            (&ConstantFloat(ref ls, lw), &ConstantFloat(ref rs, rw)) =>
                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,
                    }
                } else { None },
            (&ConstantBool(ref l), &ConstantBool(ref r)) => Some(l.cmp(r)),
            (&ConstantVec(ref l), &ConstantVec(ref r)) => l.partial_cmp(&r),
            (&ConstantRepeat(ref lv, ref ls), &ConstantRepeat(ref rv, ref rs)) =>
                match lv.partial_cmp(rv) {
                    Some(Equal) => Some(ls.cmp(rs)),
                    x => x,
                },
            (&ConstantTuple(ref l), &ConstantTuple(ref r)) => l.partial_cmp(r),
             _ => None, //TODO: Are there any useful inter-type orderings?
         }
    }
}



fn lit_to_constant(lit: &Lit_) -> Constant {
    match *lit {
        LitStr(ref is, style) => ConstantStr(is.to_string(), style),
        LitByte(b) => ConstantByte(b),
        LitByteStr(ref s) => ConstantBinary(s.clone()),
        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),
    }
}

fn constant_not(o: Constant) -> Option<Constant> {
    Some(match o {
        ConstantBool(b) => ConstantBool(!b),
        ConstantInt(value, ty) => {
            let (nvalue, nty) = match ty {
                SignedIntLit(ity, Plus) => {
                    if value == ::std::u64::MAX { return None; }
                    (value + 1, SignedIntLit(ity, Minus))
                },
                SignedIntLit(ity, Minus) => {
                    if value == 0 {
                        (1, SignedIntLit(ity, Minus))
                    } else {
                        (value - 1, SignedIntLit(ity, Plus))
                    }
                }
                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,
                        UintTy::TyUs => { return None; }  // refuse to guess
                    };
                    (!value & mask, UnsignedIntLit(ity))
                }
                UnsuffixedIntLit(_) => { return None; }  // refuse to guess
            };
            ConstantInt(nvalue, nty)
        },
        _ => { return None; }
    })
}

fn constant_negate(o: Constant) -> Option<Constant> {
    Some(match o {
        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; },
    })
}

fn neg_sign(s: Sign) -> Sign {
    match s {
        Sign::Plus => Sign::Minus,
        Sign::Minus => Sign::Plus,
    }
}

fn neg_float_str(s: String) -> String {
    if s.starts_with('-') {
        s[1..].to_owned()
    } else {
        format!("-{}", s)
    }
}

/// is the given LitIntType negative?
///
/// Examples
///
/// ```
/// assert!(is_negative(UnsuffixedIntLit(Minus)));
/// ```
pub fn is_negative(ty: LitIntType) -> bool {
    match ty {
        SignedIntLit(_, sign) | UnsuffixedIntLit(sign) => sign == Minus,
        UnsignedIntLit(_) => false,
    }
}

fn unify_int_type(l: LitIntType, r: LitIntType, s: Sign) -> Option<LitIntType> {
    match (l, r) {
        (SignedIntLit(lty, _), SignedIntLit(rty, _)) => if lty == rty {
            Some(SignedIntLit(lty, s)) } else { None },
        (UnsignedIntLit(lty), UnsignedIntLit(rty)) =>
            if s == Plus && lty == rty {
                Some(UnsignedIntLit(lty))
            } else { None },
        (UnsuffixedIntLit(_), UnsuffixedIntLit(_)) => Some(UnsuffixedIntLit(s)),
        (SignedIntLit(lty, _), UnsuffixedIntLit(_)) => Some(SignedIntLit(lty, s)),
        (UnsignedIntLit(lty), UnsuffixedIntLit(rs)) => if rs == Plus {
            Some(UnsignedIntLit(lty)) } else { None },
        (UnsuffixedIntLit(_), SignedIntLit(rty, _)) => Some(SignedIntLit(rty, s)),
        (UnsuffixedIntLit(ls), UnsignedIntLit(rty)) => if ls == Plus {
            Some(UnsignedIntLit(rty)) } else { None },
        _ => None,
    }
}

fn add_neg_int(pos: u64, pty: LitIntType, neg: u64, nty: LitIntType) ->
        Option<Constant> {
    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))
    }
}

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)))
}


pub fn constant(lcx: &LateContext, e: &Expr) -> Option<(Constant, bool)> {
    let mut cx = ConstEvalLateContext { lcx: Some(lcx), needed_resolution: false };
    cx.expr(e).map(|cst| (cst, cx.needed_resolution))
}

pub fn constant_simple(e: &Expr) -> Option<Constant> {
    let mut cx = ConstEvalLateContext { lcx: None, needed_resolution: false };
    cx.expr(e)
}

struct ConstEvalLateContext<'c, 'cc: 'c> {
    lcx: Option<&'c LateContext<'c, 'cc>>,
    needed_resolution: bool
}

impl<'c, 'cc> ConstEvalLateContext<'c, 'cc> {

    /// simple constant folding: Insert an expression, get a constant or none.
    fn expr(&mut self, e: &Expr) -> Option<Constant> {
        match e.node {
            ExprPath(_, _) => self.fetch_path(e),
            ExprBlock(ref block) => self.block(block),
            ExprIf(ref cond, ref then, ref otherwise) =>
                self.ifthenelse(cond, then, otherwise),
            ExprLit(ref lit) => Some(lit_to_constant(&lit.node)),
            ExprVec(ref vec) => self.multi(vec).map(ConstantVec),
            ExprTup(ref tup) => self.multi(tup).map(ConstantTuple),
            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
            _ => None,
        }
    }

    /// create `Some(Vec![..])` of all constants, unless there is any
    /// non-constant part
    fn multi<E: Deref<Target=Expr> + Sized>(&mut self, vec: &[E]) ->
            Option<Vec<Constant>> {
        vec.iter().map(|elem| self.expr(elem))
                  .collect::<Option<_>>()
    }

    /// lookup a possibly constant expression from a ExprPath
    fn fetch_path(&mut self, e: &Expr) -> Option<Constant> {
        if let Some(lcx) = self.lcx {
            let mut maybe_id = None;
            if let Some(&PathResolution { base_def: DefConst(id), ..}) =
                lcx.tcx.def_map.borrow().get(&e.id) {
                maybe_id = Some(id);
            }
            // separate if lets to avoid doubleborrowing the defmap
            if let Some(id) = maybe_id {
                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() {
            block.expr.as_ref().and_then(|ref b| self.expr(b))
        } else { None }
    }

    fn ifthenelse(&mut self, cond: &Expr, then: &Block, otherwise: &Option<P<Expr>>)
                  -> Option<Constant> {
        if let Some(ConstantBool(b)) = self.expr(cond) {
            if b {
                self.block(then)
            } else {
                otherwise.as_ref().and_then(|expr| self.expr(expr))
            }
        } else { None }
    }

    fn binop(&mut self, op: BinOp, left: &Expr, right: &Expr) -> Option<Constant> {
        match op.node {
            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)))
                        } else {
                            if ln {
                                add_neg_int(r64, rty, l64, lty)
                            } else {
                                add_neg_int(l64, lty, r64, rty)
                            }
                        }
                    },
                    // TODO: float (would need bignum library?)
                    _ => None
                }),
            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,
                }),
            BiMul => self.divmul(left, right, u64::checked_mul),
            BiDiv => self.divmul(left, right, u64::checked_div),
            //BiRem,
            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),
            BiEq => self.binop_apply(left, right,
                |l, r| Some(ConstantBool(l == r))),
            BiNe => self.binop_apply(left, right,
                |l, r| Some(ConstantBool(l != r))),
            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),
            _ => None
        }
    }

    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|
            match (l, r) {
                (ConstantInt(l64, lty), ConstantInt(r64, rty)) => {
                    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)))
                },
                _ => None,
            })
    }

    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
        })
    }

    fn cmp(&mut self, left: &Expr, right: &Expr, ordering: Ordering, b: bool) -> Option<Constant> {
        self.binop_apply(left, right, |l, r| l.partial_cmp(&r).map(|o|
            ConstantBool(b == (o == ordering))))
    }

    fn binop_apply<F>(&mut self, left: &Expr, right: &Expr, op: F) -> Option<Constant>
    where F: Fn(Constant, Constant) -> Option<Constant> {
        if let (Some(lc), Some(rc)) = (self.expr(left), self.expr(right)) {
            op(lc, rc)
        } else { None }
    }

    fn short_circuit(&mut self, left: &Expr, right: &Expr, b: bool) -> Option<Constant> {
        self.expr(left).and_then(|left|
            if let ConstantBool(lbool) = left {
                if lbool == b {
                    Some(left)
                } else {
                    self.expr(right).and_then(|right|
                        if let ConstantBool(_) = right {
                            Some(right)
                        } else { None }
                    )
                }
            } else { None }
        )
    }
}