2014-02-10 08:36:31 -06:00
|
|
|
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
|
2012-12-03 18:48:01 -06:00
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2014-02-10 08:36:31 -06:00
|
|
|
#[allow(non_camel_case_types)];
|
2013-05-17 17:28:44 -05:00
|
|
|
|
2013-03-21 02:27:26 -05:00
|
|
|
use metadata::csearch;
|
|
|
|
use middle::astencode;
|
2014-01-28 23:05:11 -06:00
|
|
|
|
2012-12-23 16:41:37 -06:00
|
|
|
use middle::ty;
|
2014-01-28 23:05:11 -06:00
|
|
|
use middle::typeck::astconv;
|
2012-12-23 16:41:37 -06:00
|
|
|
use middle;
|
|
|
|
|
2012-12-13 15:05:22 -06:00
|
|
|
use syntax::ast::*;
|
2014-01-10 16:02:36 -06:00
|
|
|
use syntax::parse::token::InternedString;
|
|
|
|
use syntax::visit::Visitor;
|
|
|
|
use syntax::visit;
|
|
|
|
use syntax::{ast, ast_map, ast_util};
|
2012-07-30 21:05:56 -05:00
|
|
|
|
2013-12-19 21:54:52 -06:00
|
|
|
use std::cell::RefCell;
|
2014-02-19 21:29:58 -06:00
|
|
|
use collections::HashMap;
|
2014-01-31 22:57:59 -06:00
|
|
|
use std::rc::Rc;
|
2013-03-21 02:27:26 -05:00
|
|
|
|
2012-07-30 21:05:56 -05:00
|
|
|
//
|
|
|
|
// This pass classifies expressions by their constant-ness.
|
|
|
|
//
|
|
|
|
// Constant-ness comes in 3 flavours:
|
|
|
|
//
|
|
|
|
// - Integer-constants: can be evaluated by the frontend all the way down
|
|
|
|
// to their actual value. They are used in a few places (enum
|
|
|
|
// discriminants, switch arms) and are a subset of
|
|
|
|
// general-constants. They cover all the integer and integer-ish
|
|
|
|
// literals (nil, bool, int, uint, char, iNN, uNN) and all integer
|
|
|
|
// operators and copies applied to them.
|
|
|
|
//
|
|
|
|
// - General-constants: can be evaluated by LLVM but not necessarily by
|
|
|
|
// the frontend; usually due to reliance on target-specific stuff such
|
|
|
|
// as "where in memory the value goes" or "what floating point mode the
|
|
|
|
// target uses". This _includes_ integer-constants, plus the following
|
|
|
|
// constructors:
|
|
|
|
//
|
2012-10-09 23:28:04 -05:00
|
|
|
// fixed-size vectors and strings: [] and ""/_
|
2012-07-30 21:05:56 -05:00
|
|
|
// vector and string slices: &[] and &""
|
|
|
|
// tuples: (,)
|
|
|
|
// records: {...}
|
|
|
|
// enums: foo(...)
|
|
|
|
// floating point literals and operators
|
|
|
|
// & and * pointers
|
|
|
|
// copies of general constants
|
|
|
|
//
|
2013-01-04 08:52:07 -06:00
|
|
|
// (in theory, probably not at first: if/match on integer-const
|
2012-07-30 21:05:56 -05:00
|
|
|
// conditions / descriminants)
|
|
|
|
//
|
|
|
|
// - Non-constants: everything else.
|
|
|
|
//
|
|
|
|
|
2013-01-30 15:44:24 -06:00
|
|
|
pub enum constness {
|
2012-07-30 21:05:56 -05:00
|
|
|
integral_const,
|
|
|
|
general_const,
|
|
|
|
non_const
|
|
|
|
}
|
|
|
|
|
2013-10-04 17:04:11 -05:00
|
|
|
type constness_cache = HashMap<ast::DefId, constness>;
|
|
|
|
|
2013-01-30 15:44:24 -06:00
|
|
|
pub fn join(a: constness, b: constness) -> constness {
|
2012-09-28 00:20:47 -05:00
|
|
|
match (a, b) {
|
2012-08-03 21:59:04 -05:00
|
|
|
(integral_const, integral_const) => integral_const,
|
2012-07-30 21:05:56 -05:00
|
|
|
(integral_const, general_const)
|
|
|
|
| (general_const, integral_const)
|
2012-08-03 21:59:04 -05:00
|
|
|
| (general_const, general_const) => general_const,
|
|
|
|
_ => non_const
|
2012-07-30 21:05:56 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-29 00:05:50 -05:00
|
|
|
pub fn join_all<It: Iterator<constness>>(mut cs: It) -> constness {
|
|
|
|
cs.fold(integral_const, |a, b| join(a, b))
|
2012-07-30 21:05:56 -05:00
|
|
|
}
|
|
|
|
|
2013-09-01 20:45:37 -05:00
|
|
|
pub fn lookup_const(tcx: ty::ctxt, e: &Expr) -> Option<@Expr> {
|
2013-12-23 13:15:16 -06:00
|
|
|
let opt_def = {
|
|
|
|
let def_map = tcx.def_map.borrow();
|
|
|
|
def_map.get().find_copy(&e.id)
|
|
|
|
};
|
|
|
|
match opt_def {
|
|
|
|
Some(ast::DefStatic(def_id, false)) => {
|
|
|
|
lookup_const_by_id(tcx, def_id)
|
|
|
|
}
|
|
|
|
Some(ast::DefVariant(enum_def, variant_def, _)) => {
|
|
|
|
lookup_variant_by_id(tcx, enum_def, variant_def)
|
|
|
|
}
|
2012-11-13 00:10:15 -06:00
|
|
|
_ => None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-16 03:27:54 -05:00
|
|
|
pub fn lookup_variant_by_id(tcx: ty::ctxt,
|
2013-09-01 20:45:37 -05:00
|
|
|
enum_def: ast::DefId,
|
|
|
|
variant_def: ast::DefId)
|
|
|
|
-> Option<@Expr> {
|
2014-01-09 07:05:33 -06:00
|
|
|
fn variant_expr(variants: &[ast::P<ast::Variant>], id: ast::NodeId) -> Option<@Expr> {
|
2013-08-03 11:45:23 -05:00
|
|
|
for variant in variants.iter() {
|
2013-07-16 03:27:54 -05:00
|
|
|
if variant.node.id == id {
|
|
|
|
return variant.node.disr_expr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
if ast_util::is_local(enum_def) {
|
2013-12-27 18:09:29 -06:00
|
|
|
{
|
2014-02-13 23:07:09 -06:00
|
|
|
match tcx.map.find(enum_def.node) {
|
2013-12-27 18:09:29 -06:00
|
|
|
None => None,
|
2014-02-13 23:07:09 -06:00
|
|
|
Some(ast_map::NodeItem(it)) => match it.node {
|
2014-01-09 07:05:33 -06:00
|
|
|
ItemEnum(ast::EnumDef { variants: ref variants }, _) => {
|
2013-12-27 18:09:29 -06:00
|
|
|
variant_expr(*variants, variant_def.node)
|
|
|
|
}
|
|
|
|
_ => None
|
|
|
|
},
|
|
|
|
Some(_) => None
|
|
|
|
}
|
2013-07-16 03:27:54 -05:00
|
|
|
}
|
|
|
|
} else {
|
2013-12-19 21:24:05 -06:00
|
|
|
{
|
|
|
|
let extern_const_variants = tcx.extern_const_variants.borrow();
|
|
|
|
match extern_const_variants.get().find(&variant_def) {
|
|
|
|
Some(&e) => return e,
|
|
|
|
None => {}
|
|
|
|
}
|
2013-10-04 17:04:11 -05:00
|
|
|
}
|
2013-07-16 03:27:54 -05:00
|
|
|
let maps = astencode::Maps {
|
2013-12-19 21:54:52 -06:00
|
|
|
root_map: @RefCell::new(HashMap::new()),
|
2013-12-21 19:04:42 -06:00
|
|
|
method_map: @RefCell::new(HashMap::new()),
|
2013-12-19 22:10:37 -06:00
|
|
|
vtable_map: @RefCell::new(HashMap::new()),
|
2013-12-19 22:03:08 -06:00
|
|
|
capture_map: @RefCell::new(HashMap::new())
|
2013-07-16 03:27:54 -05:00
|
|
|
};
|
2013-10-04 17:04:11 -05:00
|
|
|
let e = match csearch::maybe_get_item_ast(tcx, enum_def,
|
2014-02-13 23:07:09 -06:00
|
|
|
|a, b, c, d| astencode::decode_inlined_item(a, b,
|
2013-07-17 01:49:42 -05:00
|
|
|
maps,
|
2014-02-13 23:07:09 -06:00
|
|
|
c, d)) {
|
2014-01-09 07:05:33 -06:00
|
|
|
csearch::found(ast::IIItem(item)) => match item.node {
|
|
|
|
ItemEnum(ast::EnumDef { variants: ref variants }, _) => {
|
2013-07-16 03:27:54 -05:00
|
|
|
variant_expr(*variants, variant_def.node)
|
|
|
|
}
|
|
|
|
_ => None
|
|
|
|
},
|
|
|
|
_ => None
|
2013-10-04 17:04:11 -05:00
|
|
|
};
|
2013-12-19 21:24:05 -06:00
|
|
|
{
|
|
|
|
let mut extern_const_variants = tcx.extern_const_variants
|
|
|
|
.borrow_mut();
|
|
|
|
extern_const_variants.get().insert(variant_def, e);
|
|
|
|
return e;
|
|
|
|
}
|
2013-07-16 03:27:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-27 18:09:29 -06:00
|
|
|
pub fn lookup_const_by_id(tcx: ty::ctxt, def_id: ast::DefId)
|
|
|
|
-> Option<@Expr> {
|
2012-11-13 00:10:15 -06:00
|
|
|
if ast_util::is_local(def_id) {
|
2013-12-27 18:09:29 -06:00
|
|
|
{
|
2014-02-13 23:07:09 -06:00
|
|
|
match tcx.map.find(def_id.node) {
|
2013-12-27 18:09:29 -06:00
|
|
|
None => None,
|
2014-02-13 23:07:09 -06:00
|
|
|
Some(ast_map::NodeItem(it)) => match it.node {
|
2014-01-09 07:05:33 -06:00
|
|
|
ItemStatic(_, ast::MutImmutable, const_expr) => {
|
2013-12-27 18:09:29 -06:00
|
|
|
Some(const_expr)
|
|
|
|
}
|
|
|
|
_ => None
|
|
|
|
},
|
|
|
|
Some(_) => None
|
|
|
|
}
|
2012-10-15 14:27:09 -05:00
|
|
|
}
|
2012-11-13 00:10:15 -06:00
|
|
|
} else {
|
2013-12-19 21:21:42 -06:00
|
|
|
{
|
|
|
|
let extern_const_statics = tcx.extern_const_statics.borrow();
|
|
|
|
match extern_const_statics.get().find(&def_id) {
|
|
|
|
Some(&e) => return e,
|
|
|
|
None => {}
|
|
|
|
}
|
2013-10-04 17:04:11 -05:00
|
|
|
}
|
2013-03-21 02:27:26 -05:00
|
|
|
let maps = astencode::Maps {
|
2013-12-19 21:54:52 -06:00
|
|
|
root_map: @RefCell::new(HashMap::new()),
|
2013-12-21 19:04:42 -06:00
|
|
|
method_map: @RefCell::new(HashMap::new()),
|
2013-12-19 22:10:37 -06:00
|
|
|
vtable_map: @RefCell::new(HashMap::new()),
|
2013-12-19 22:03:08 -06:00
|
|
|
capture_map: @RefCell::new(HashMap::new())
|
2013-03-21 02:27:26 -05:00
|
|
|
};
|
2013-10-04 17:04:11 -05:00
|
|
|
let e = match csearch::maybe_get_item_ast(tcx, def_id,
|
2013-07-02 14:47:32 -05:00
|
|
|
|a, b, c, d| astencode::decode_inlined_item(a, b, maps, c, d)) {
|
2014-01-09 07:05:33 -06:00
|
|
|
csearch::found(ast::IIItem(item)) => match item.node {
|
|
|
|
ItemStatic(_, ast::MutImmutable, const_expr) => Some(const_expr),
|
2013-03-21 02:27:26 -05:00
|
|
|
_ => None
|
|
|
|
},
|
|
|
|
_ => None
|
2013-10-04 17:04:11 -05:00
|
|
|
};
|
2013-12-19 21:21:42 -06:00
|
|
|
{
|
|
|
|
let mut extern_const_statics = tcx.extern_const_statics
|
|
|
|
.borrow_mut();
|
|
|
|
extern_const_statics.get().insert(def_id, e);
|
|
|
|
return e;
|
|
|
|
}
|
2012-10-15 14:27:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-04 17:04:11 -05:00
|
|
|
struct ConstEvalVisitor {
|
|
|
|
tcx: ty::ctxt,
|
|
|
|
ccache: constness_cache,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ConstEvalVisitor {
|
|
|
|
fn classify(&mut self, e: &Expr) -> constness {
|
|
|
|
let did = ast_util::local_def(e.id);
|
|
|
|
match self.ccache.find(&did) {
|
|
|
|
Some(&x) => return x,
|
|
|
|
None => {}
|
|
|
|
}
|
|
|
|
let cn = match e.node {
|
|
|
|
ast::ExprLit(lit) => {
|
|
|
|
match lit.node {
|
2014-01-09 07:05:33 -06:00
|
|
|
ast::LitStr(..) | ast::LitFloat(..) => general_const,
|
2013-10-04 17:04:11 -05:00
|
|
|
_ => integral_const
|
|
|
|
}
|
2012-10-15 14:27:09 -05:00
|
|
|
}
|
2013-10-04 17:04:11 -05:00
|
|
|
|
|
|
|
ast::ExprUnary(_, _, inner) | ast::ExprParen(inner) =>
|
|
|
|
self.classify(inner),
|
|
|
|
|
|
|
|
ast::ExprBinary(_, _, a, b) =>
|
|
|
|
join(self.classify(a), self.classify(b)),
|
|
|
|
|
|
|
|
ast::ExprTup(ref es) |
|
|
|
|
ast::ExprVec(ref es, ast::MutImmutable) =>
|
|
|
|
join_all(es.iter().map(|e| self.classify(*e))),
|
|
|
|
|
|
|
|
ast::ExprVstore(e, vstore) => {
|
|
|
|
match vstore {
|
|
|
|
ast::ExprVstoreSlice => self.classify(e),
|
|
|
|
ast::ExprVstoreUniq |
|
|
|
|
ast::ExprVstoreMutSlice => non_const
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ast::ExprStruct(_, ref fs, None) => {
|
2013-11-21 17:42:55 -06:00
|
|
|
let cs = fs.iter().map(|f| self.classify(f.expr));
|
2013-10-04 17:04:11 -05:00
|
|
|
join_all(cs)
|
|
|
|
}
|
|
|
|
|
|
|
|
ast::ExprCast(base, _) => {
|
|
|
|
let ty = ty::expr_ty(self.tcx, e);
|
|
|
|
let base = self.classify(base);
|
|
|
|
if ty::type_is_integral(ty) {
|
|
|
|
join(integral_const, base)
|
|
|
|
} else if ty::type_is_fp(ty) {
|
|
|
|
join(general_const, base)
|
|
|
|
} else {
|
|
|
|
non_const
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ast::ExprField(base, _, _) => self.classify(base),
|
|
|
|
|
|
|
|
ast::ExprIndex(_, base, idx) =>
|
|
|
|
join(self.classify(base), self.classify(idx)),
|
|
|
|
|
|
|
|
ast::ExprAddrOf(ast::MutImmutable, base) => self.classify(base),
|
|
|
|
|
|
|
|
// FIXME: (#3728) we can probably do something CCI-ish
|
|
|
|
// surrounding nonlocal constants. But we don't yet.
|
|
|
|
ast::ExprPath(_) => self.lookup_constness(e),
|
|
|
|
|
2013-11-28 14:22:53 -06:00
|
|
|
ast::ExprRepeat(..) => general_const,
|
2013-10-04 17:04:11 -05:00
|
|
|
|
|
|
|
_ => non_const
|
|
|
|
};
|
|
|
|
self.ccache.insert(did, cn);
|
|
|
|
cn
|
|
|
|
}
|
|
|
|
|
|
|
|
fn lookup_constness(&self, e: &Expr) -> constness {
|
|
|
|
match lookup_const(self.tcx, e) {
|
|
|
|
Some(rhs) => {
|
|
|
|
let ty = ty::expr_ty(self.tcx, rhs);
|
|
|
|
if ty::type_is_integral(ty) {
|
|
|
|
integral_const
|
|
|
|
} else {
|
|
|
|
general_const
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None => non_const
|
2012-10-15 14:27:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-04 17:04:11 -05:00
|
|
|
}
|
2013-08-12 20:37:40 -05:00
|
|
|
|
|
|
|
impl Visitor<()> for ConstEvalVisitor {
|
2014-01-06 06:00:46 -06:00
|
|
|
fn visit_expr_post(&mut self, e: &Expr, _: ()) {
|
2013-10-04 17:04:11 -05:00
|
|
|
self.classify(e);
|
2013-08-12 20:37:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-05 15:15:24 -06:00
|
|
|
pub fn process_crate(krate: &ast::Crate,
|
2013-01-30 15:44:24 -06:00
|
|
|
tcx: ty::ctxt) {
|
2013-10-04 17:04:11 -05:00
|
|
|
let mut v = ConstEvalVisitor {
|
|
|
|
tcx: tcx,
|
|
|
|
ccache: HashMap::new(),
|
|
|
|
};
|
2014-02-05 15:15:24 -06:00
|
|
|
visit::walk_crate(&mut v, krate, ());
|
2012-07-30 21:05:56 -05:00
|
|
|
tcx.sess.abort_if_errors();
|
|
|
|
}
|
|
|
|
|
2012-03-22 16:56:56 -05:00
|
|
|
|
2012-06-21 18:44:10 -05:00
|
|
|
// FIXME (#33): this doesn't handle big integer/float literals correctly
|
|
|
|
// (nor does the rest of our literal handling).
|
2013-07-02 14:47:32 -05:00
|
|
|
#[deriving(Clone, Eq)]
|
2013-01-30 15:44:24 -06:00
|
|
|
pub enum const_val {
|
2012-06-04 19:26:17 -05:00
|
|
|
const_float(f64),
|
2012-03-22 16:56:56 -05:00
|
|
|
const_int(i64),
|
|
|
|
const_uint(u64),
|
2014-01-10 16:02:36 -06:00
|
|
|
const_str(InternedString),
|
2014-01-31 22:57:59 -06:00
|
|
|
const_binary(Rc<~[u8]>),
|
2012-08-23 17:51:23 -05:00
|
|
|
const_bool(bool)
|
2012-03-22 16:56:56 -05:00
|
|
|
}
|
|
|
|
|
2013-09-01 20:45:37 -05:00
|
|
|
pub fn eval_const_expr(tcx: middle::ty::ctxt, e: &Expr) -> const_val {
|
2013-07-16 03:27:54 -05:00
|
|
|
match eval_const_expr_partial(&tcx, e) {
|
2013-06-12 12:02:55 -05:00
|
|
|
Ok(r) => r,
|
|
|
|
Err(s) => tcx.sess.span_fatal(e.span, s)
|
2012-10-15 14:27:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-01 20:45:37 -05:00
|
|
|
pub fn eval_const_expr_partial<T: ty::ExprTyProvider>(tcx: &T, e: &Expr)
|
2013-01-30 15:44:24 -06:00
|
|
|
-> Result<const_val, ~str> {
|
2012-09-07 20:08:21 -05:00
|
|
|
use middle::ty;
|
2012-10-15 14:27:09 -05:00
|
|
|
fn fromb(b: bool) -> Result<const_val, ~str> { Ok(const_int(b as i64)) }
|
2012-08-23 17:51:23 -05:00
|
|
|
match e.node {
|
2013-09-01 20:45:37 -05:00
|
|
|
ExprUnary(_, UnNeg, inner) => {
|
2012-10-15 14:27:09 -05:00
|
|
|
match eval_const_expr_partial(tcx, inner) {
|
|
|
|
Ok(const_float(f)) => Ok(const_float(-f)),
|
|
|
|
Ok(const_int(i)) => Ok(const_int(-i)),
|
|
|
|
Ok(const_uint(i)) => Ok(const_uint(-i)),
|
2014-02-06 03:38:08 -06:00
|
|
|
Ok(const_str(_)) => Err(~"negate on string"),
|
|
|
|
Ok(const_bool(_)) => Err(~"negate on boolean"),
|
2013-07-02 14:47:32 -05:00
|
|
|
ref err => ((*err).clone())
|
2012-03-22 16:56:56 -05:00
|
|
|
}
|
|
|
|
}
|
2013-09-01 20:45:37 -05:00
|
|
|
ExprUnary(_, UnNot, inner) => {
|
2012-10-15 14:27:09 -05:00
|
|
|
match eval_const_expr_partial(tcx, inner) {
|
|
|
|
Ok(const_int(i)) => Ok(const_int(!i)),
|
|
|
|
Ok(const_uint(i)) => Ok(const_uint(!i)),
|
|
|
|
Ok(const_bool(b)) => Ok(const_bool(!b)),
|
2014-02-06 03:38:08 -06:00
|
|
|
_ => Err(~"not on float or string")
|
2012-03-22 16:56:56 -05:00
|
|
|
}
|
|
|
|
}
|
2013-09-01 20:45:37 -05:00
|
|
|
ExprBinary(_, op, a, b) => {
|
2012-10-15 14:27:09 -05:00
|
|
|
match (eval_const_expr_partial(tcx, a),
|
|
|
|
eval_const_expr_partial(tcx, b)) {
|
|
|
|
(Ok(const_float(a)), Ok(const_float(b))) => {
|
2012-08-23 17:51:23 -05:00
|
|
|
match op {
|
2013-09-01 20:45:37 -05:00
|
|
|
BiAdd => Ok(const_float(a + b)),
|
|
|
|
BiSub => Ok(const_float(a - b)),
|
|
|
|
BiMul => Ok(const_float(a * b)),
|
|
|
|
BiDiv => Ok(const_float(a / b)),
|
|
|
|
BiRem => Ok(const_float(a % b)),
|
|
|
|
BiEq => fromb(a == b),
|
|
|
|
BiLt => fromb(a < b),
|
|
|
|
BiLe => fromb(a <= b),
|
|
|
|
BiNe => fromb(a != b),
|
|
|
|
BiGe => fromb(a >= b),
|
|
|
|
BiGt => fromb(a > b),
|
2014-02-06 03:38:08 -06:00
|
|
|
_ => Err(~"can't do this op on floats")
|
2012-03-22 16:56:56 -05:00
|
|
|
}
|
|
|
|
}
|
2012-10-15 14:27:09 -05:00
|
|
|
(Ok(const_int(a)), Ok(const_int(b))) => {
|
2012-08-23 17:51:23 -05:00
|
|
|
match op {
|
2013-09-01 20:45:37 -05:00
|
|
|
BiAdd => Ok(const_int(a + b)),
|
|
|
|
BiSub => Ok(const_int(a - b)),
|
|
|
|
BiMul => Ok(const_int(a * b)),
|
|
|
|
BiDiv if b == 0 => Err(~"attempted to divide by zero"),
|
|
|
|
BiDiv => Ok(const_int(a / b)),
|
|
|
|
BiRem if b == 0 => Err(~"attempted remainder with a divisor of zero"),
|
|
|
|
BiRem => Ok(const_int(a % b)),
|
|
|
|
BiAnd | BiBitAnd => Ok(const_int(a & b)),
|
|
|
|
BiOr | BiBitOr => Ok(const_int(a | b)),
|
|
|
|
BiBitXor => Ok(const_int(a ^ b)),
|
|
|
|
BiShl => Ok(const_int(a << b)),
|
|
|
|
BiShr => Ok(const_int(a >> b)),
|
|
|
|
BiEq => fromb(a == b),
|
|
|
|
BiLt => fromb(a < b),
|
|
|
|
BiLe => fromb(a <= b),
|
|
|
|
BiNe => fromb(a != b),
|
|
|
|
BiGe => fromb(a >= b),
|
|
|
|
BiGt => fromb(a > b)
|
2012-03-22 16:56:56 -05:00
|
|
|
}
|
|
|
|
}
|
2012-10-15 14:27:09 -05:00
|
|
|
(Ok(const_uint(a)), Ok(const_uint(b))) => {
|
2012-08-23 17:51:23 -05:00
|
|
|
match op {
|
2013-09-01 20:45:37 -05:00
|
|
|
BiAdd => Ok(const_uint(a + b)),
|
|
|
|
BiSub => Ok(const_uint(a - b)),
|
|
|
|
BiMul => Ok(const_uint(a * b)),
|
|
|
|
BiDiv if b == 0 => Err(~"attempted to divide by zero"),
|
|
|
|
BiDiv => Ok(const_uint(a / b)),
|
|
|
|
BiRem if b == 0 => Err(~"attempted remainder with a divisor of zero"),
|
|
|
|
BiRem => Ok(const_uint(a % b)),
|
|
|
|
BiAnd | BiBitAnd => Ok(const_uint(a & b)),
|
|
|
|
BiOr | BiBitOr => Ok(const_uint(a | b)),
|
|
|
|
BiBitXor => Ok(const_uint(a ^ b)),
|
|
|
|
BiShl => Ok(const_uint(a << b)),
|
|
|
|
BiShr => Ok(const_uint(a >> b)),
|
|
|
|
BiEq => fromb(a == b),
|
|
|
|
BiLt => fromb(a < b),
|
|
|
|
BiLe => fromb(a <= b),
|
|
|
|
BiNe => fromb(a != b),
|
|
|
|
BiGe => fromb(a >= b),
|
|
|
|
BiGt => fromb(a > b),
|
2012-03-22 16:56:56 -05:00
|
|
|
}
|
|
|
|
}
|
2012-05-30 17:03:24 -05:00
|
|
|
// shifts can have any integral type as their rhs
|
2012-10-15 14:27:09 -05:00
|
|
|
(Ok(const_int(a)), Ok(const_uint(b))) => {
|
2012-08-23 17:51:23 -05:00
|
|
|
match op {
|
2013-09-01 20:45:37 -05:00
|
|
|
BiShl => Ok(const_int(a << b)),
|
|
|
|
BiShr => Ok(const_int(a >> b)),
|
2014-02-06 03:38:08 -06:00
|
|
|
_ => Err(~"can't do this op on an int and uint")
|
2012-05-30 17:03:24 -05:00
|
|
|
}
|
|
|
|
}
|
2012-10-15 14:27:09 -05:00
|
|
|
(Ok(const_uint(a)), Ok(const_int(b))) => {
|
2012-08-23 17:51:23 -05:00
|
|
|
match op {
|
2013-09-01 20:45:37 -05:00
|
|
|
BiShl => Ok(const_uint(a << b)),
|
|
|
|
BiShr => Ok(const_uint(a >> b)),
|
2014-02-06 03:38:08 -06:00
|
|
|
_ => Err(~"can't do this op on a uint and int")
|
2012-05-30 17:03:24 -05:00
|
|
|
}
|
|
|
|
}
|
2012-10-15 14:27:09 -05:00
|
|
|
(Ok(const_bool(a)), Ok(const_bool(b))) => {
|
|
|
|
Ok(const_bool(match op {
|
2013-09-01 20:45:37 -05:00
|
|
|
BiAnd => a && b,
|
|
|
|
BiOr => a || b,
|
|
|
|
BiBitXor => a ^ b,
|
|
|
|
BiBitAnd => a & b,
|
|
|
|
BiBitOr => a | b,
|
|
|
|
BiEq => a == b,
|
|
|
|
BiNe => a != b,
|
2014-02-06 03:38:08 -06:00
|
|
|
_ => return Err(~"can't do this op on bools")
|
2012-10-15 14:27:09 -05:00
|
|
|
}))
|
2012-08-23 17:51:23 -05:00
|
|
|
}
|
2014-02-06 03:38:08 -06:00
|
|
|
_ => Err(~"bad operands for binary")
|
2012-03-22 16:56:56 -05:00
|
|
|
}
|
|
|
|
}
|
2014-01-28 23:05:11 -06:00
|
|
|
ExprCast(base, target_ty) => {
|
|
|
|
// This tends to get called w/o the type actually having been
|
|
|
|
// populated in the ctxt, which was causing things to blow up
|
|
|
|
// (#5900). Fall back to doing a limited lookup to get past it.
|
|
|
|
let ety = ty::expr_ty_opt(tcx.ty_ctxt(), e)
|
|
|
|
.or_else(|| astconv::ast_ty_to_prim_ty(tcx.ty_ctxt(), target_ty))
|
|
|
|
.unwrap_or_else(|| tcx.ty_ctxt().sess.span_fatal(
|
|
|
|
target_ty.span,
|
2014-02-06 03:38:08 -06:00
|
|
|
format!("target type not found for const cast")
|
2014-01-28 23:05:11 -06:00
|
|
|
));
|
|
|
|
|
2012-10-15 14:27:09 -05:00
|
|
|
let base = eval_const_expr_partial(tcx, base);
|
2013-07-02 14:47:32 -05:00
|
|
|
match base {
|
2013-04-15 14:13:37 -05:00
|
|
|
Err(_) => base,
|
|
|
|
Ok(val) => {
|
|
|
|
match ty::get(ety).sty {
|
2013-07-02 14:47:32 -05:00
|
|
|
ty::ty_float(_) => {
|
|
|
|
match val {
|
|
|
|
const_uint(u) => Ok(const_float(u as f64)),
|
|
|
|
const_int(i) => Ok(const_float(i as f64)),
|
|
|
|
const_float(f) => Ok(const_float(f)),
|
2014-02-06 03:38:08 -06:00
|
|
|
_ => Err(~"can't cast float to str"),
|
2013-07-02 14:47:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ty::ty_uint(_) => {
|
|
|
|
match val {
|
|
|
|
const_uint(u) => Ok(const_uint(u)),
|
|
|
|
const_int(i) => Ok(const_uint(i as u64)),
|
|
|
|
const_float(f) => Ok(const_uint(f as u64)),
|
2014-02-06 03:38:08 -06:00
|
|
|
_ => Err(~"can't cast str to uint"),
|
2013-07-02 14:47:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ty::ty_int(_) | ty::ty_bool => {
|
|
|
|
match val {
|
|
|
|
const_uint(u) => Ok(const_int(u as i64)),
|
|
|
|
const_int(i) => Ok(const_int(i)),
|
|
|
|
const_float(f) => Ok(const_int(f as i64)),
|
2014-02-06 03:38:08 -06:00
|
|
|
_ => Err(~"can't cast str to int"),
|
2013-07-02 14:47:32 -05:00
|
|
|
}
|
|
|
|
}
|
2014-02-06 03:38:08 -06:00
|
|
|
_ => Err(~"can't cast this type")
|
2013-04-15 14:13:37 -05:00
|
|
|
}
|
2012-03-22 16:56:56 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-09-01 20:45:37 -05:00
|
|
|
ExprPath(_) => {
|
2013-07-16 03:27:54 -05:00
|
|
|
match lookup_const(tcx.ty_ctxt(), e) {
|
|
|
|
Some(actual_e) => eval_const_expr_partial(&tcx.ty_ctxt(), actual_e),
|
2014-02-06 03:38:08 -06:00
|
|
|
None => Err(~"non-constant path in constant expr")
|
2012-10-15 14:27:09 -05:00
|
|
|
}
|
|
|
|
}
|
2013-09-01 20:45:37 -05:00
|
|
|
ExprLit(lit) => Ok(lit_to_const(lit)),
|
2012-07-12 14:14:08 -05:00
|
|
|
// If we have a vstore, just keep going; it has to be a string
|
2013-09-01 20:45:37 -05:00
|
|
|
ExprVstore(e, _) => eval_const_expr_partial(tcx, e),
|
|
|
|
ExprParen(e) => eval_const_expr_partial(tcx, e),
|
2014-02-06 03:38:08 -06:00
|
|
|
_ => Err(~"unsupported constant expr")
|
2012-03-22 16:56:56 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-09 07:05:33 -06:00
|
|
|
pub fn lit_to_const(lit: &Lit) -> const_val {
|
2012-08-06 14:34:08 -05:00
|
|
|
match lit.node {
|
2014-01-10 16:02:36 -06:00
|
|
|
LitStr(ref s, _) => const_str((*s).clone()),
|
2014-01-31 22:57:59 -06:00
|
|
|
LitBinary(ref data) => const_binary(data.clone()),
|
2014-01-09 07:05:33 -06:00
|
|
|
LitChar(n) => const_uint(n as u64),
|
|
|
|
LitInt(n, _) => const_int(n),
|
|
|
|
LitUint(n, _) => const_uint(n),
|
|
|
|
LitIntUnsuffixed(n) => const_int(n),
|
2014-01-15 19:15:39 -06:00
|
|
|
LitFloat(ref n, _) | LitFloatUnsuffixed(ref n) => {
|
|
|
|
const_float(from_str::<f64>(n.get()).unwrap() as f64)
|
|
|
|
}
|
2014-01-09 07:05:33 -06:00
|
|
|
LitNil => const_int(0i64),
|
|
|
|
LitBool(b) => const_bool(b)
|
2012-03-22 16:56:56 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-22 20:58:41 -05:00
|
|
|
fn compare_vals<T : Eq + Ord>(a: T, b: T) -> Option<int> {
|
|
|
|
Some(if a == b { 0 } else if a < b { -1 } else { 1 })
|
|
|
|
}
|
2013-05-21 04:04:55 -05:00
|
|
|
pub fn compare_const_vals(a: &const_val, b: &const_val) -> Option<int> {
|
2013-06-22 20:58:41 -05:00
|
|
|
match (a, b) {
|
|
|
|
(&const_int(a), &const_int(b)) => compare_vals(a, b),
|
|
|
|
(&const_uint(a), &const_uint(b)) => compare_vals(a, b),
|
|
|
|
(&const_float(a), &const_float(b)) => compare_vals(a, b),
|
2014-01-10 16:02:36 -06:00
|
|
|
(&const_str(ref a), &const_str(ref b)) => compare_vals(a, b),
|
2013-06-22 20:58:41 -05:00
|
|
|
(&const_bool(a), &const_bool(b)) => compare_vals(a, b),
|
|
|
|
_ => None
|
2013-05-21 04:04:55 -05:00
|
|
|
}
|
2012-03-22 16:56:56 -05:00
|
|
|
}
|
|
|
|
|
2013-09-01 20:45:37 -05:00
|
|
|
pub fn compare_lit_exprs(tcx: middle::ty::ctxt, a: &Expr, b: &Expr) -> Option<int> {
|
2013-05-21 04:04:55 -05:00
|
|
|
compare_const_vals(&eval_const_expr(tcx, a), &eval_const_expr(tcx, b))
|
2012-03-22 16:56:56 -05:00
|
|
|
}
|
|
|
|
|
2013-09-01 20:45:37 -05:00
|
|
|
pub fn lit_expr_eq(tcx: middle::ty::ctxt, a: &Expr, b: &Expr) -> Option<bool> {
|
2013-09-20 01:08:47 -05:00
|
|
|
compare_lit_exprs(tcx, a, b).map(|val| val == 0)
|
2012-03-22 16:56:56 -05:00
|
|
|
}
|
|
|
|
|
2014-01-09 07:05:33 -06:00
|
|
|
pub fn lit_eq(a: &Lit, b: &Lit) -> Option<bool> {
|
2013-09-20 01:08:47 -05:00
|
|
|
compare_const_vals(&lit_to_const(a), &lit_to_const(b)).map(|val| val == 0)
|
2012-03-22 16:56:56 -05:00
|
|
|
}
|