2012-12-03 18:48:01 -06:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// 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.
|
|
|
|
|
2013-01-08 21:37:25 -06:00
|
|
|
use core::prelude::*;
|
2013-01-07 16:16:52 -06:00
|
|
|
|
2013-03-21 02:27:26 -05:00
|
|
|
use metadata::csearch;
|
|
|
|
use middle::astencode;
|
2012-12-23 16:41:37 -06:00
|
|
|
use middle::resolve;
|
|
|
|
use middle::ty;
|
|
|
|
use middle;
|
|
|
|
|
|
|
|
use core::float;
|
|
|
|
use core::vec;
|
2012-12-13 15:05:22 -06:00
|
|
|
use syntax::{ast, ast_map, ast_util, visit};
|
|
|
|
use syntax::ast::*;
|
2012-07-30 21:05:56 -05:00
|
|
|
|
2013-03-21 02:27:26 -05:00
|
|
|
use std::oldmap::HashMap;
|
|
|
|
|
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-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-01-30 15:44:24 -06:00
|
|
|
pub fn join_all(cs: &[constness]) -> constness {
|
2012-09-28 00:20:47 -05:00
|
|
|
vec::foldl(integral_const, cs, |a, b| join(a, *b))
|
2012-07-30 21:05:56 -05:00
|
|
|
}
|
|
|
|
|
2013-01-30 15:44:24 -06:00
|
|
|
pub fn classify(e: @expr,
|
|
|
|
def_map: resolve::DefMap,
|
|
|
|
tcx: ty::ctxt)
|
|
|
|
-> constness {
|
2012-07-30 21:05:56 -05:00
|
|
|
let did = ast_util::local_def(e.id);
|
2013-02-05 21:41:45 -06:00
|
|
|
match tcx.ccache.find(&did) {
|
2012-08-20 14:23:37 -05:00
|
|
|
Some(x) => x,
|
|
|
|
None => {
|
2012-07-30 21:05:56 -05:00
|
|
|
let cn =
|
2013-03-20 00:17:42 -05:00
|
|
|
match e.node {
|
2012-08-03 21:59:04 -05:00
|
|
|
ast::expr_lit(lit) => {
|
2012-08-06 14:34:08 -05:00
|
|
|
match lit.node {
|
2012-07-30 21:05:56 -05:00
|
|
|
ast::lit_str(*) |
|
2012-08-03 21:59:04 -05:00
|
|
|
ast::lit_float(*) => general_const,
|
|
|
|
_ => integral_const
|
2012-07-30 21:05:56 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ast::expr_copy(inner) |
|
2012-10-27 19:14:09 -05:00
|
|
|
ast::expr_unary(_, inner) |
|
|
|
|
ast::expr_paren(inner) => {
|
2012-07-30 21:05:56 -05:00
|
|
|
classify(inner, def_map, tcx)
|
|
|
|
}
|
|
|
|
|
2012-08-03 21:59:04 -05:00
|
|
|
ast::expr_binary(_, a, b) => {
|
2012-07-30 21:05:56 -05:00
|
|
|
join(classify(a, def_map, tcx),
|
|
|
|
classify(b, def_map, tcx))
|
|
|
|
}
|
|
|
|
|
2013-03-20 00:17:42 -05:00
|
|
|
ast::expr_tup(ref es) |
|
|
|
|
ast::expr_vec(ref es, ast::m_imm) => {
|
|
|
|
join_all(vec::map(*es, |e| classify(*e, def_map, tcx)))
|
2012-07-30 21:05:56 -05:00
|
|
|
}
|
|
|
|
|
2012-08-03 21:59:04 -05:00
|
|
|
ast::expr_vstore(e, vstore) => {
|
2012-09-11 23:25:01 -05:00
|
|
|
match vstore {
|
|
|
|
ast::expr_vstore_fixed(_) |
|
|
|
|
ast::expr_vstore_slice => classify(e, def_map, tcx),
|
|
|
|
ast::expr_vstore_uniq |
|
2012-11-21 13:39:06 -06:00
|
|
|
ast::expr_vstore_box |
|
2012-12-07 18:26:52 -06:00
|
|
|
ast::expr_vstore_mut_box |
|
|
|
|
ast::expr_vstore_mut_slice => non_const
|
2012-09-11 23:25:01 -05:00
|
|
|
}
|
2012-07-30 21:05:56 -05:00
|
|
|
}
|
|
|
|
|
2013-03-05 20:22:13 -06:00
|
|
|
ast::expr_struct(_, ref fs, None) => {
|
2012-12-04 12:50:00 -06:00
|
|
|
let cs = do vec::map((*fs)) |f| {
|
2012-07-30 21:05:56 -05:00
|
|
|
if f.node.mutbl == ast::m_imm {
|
|
|
|
classify(f.node.expr, def_map, tcx)
|
|
|
|
} else {
|
|
|
|
non_const
|
|
|
|
}
|
|
|
|
};
|
|
|
|
join_all(cs)
|
|
|
|
}
|
|
|
|
|
2012-08-03 21:59:04 -05:00
|
|
|
ast::expr_cast(base, _) => {
|
2012-07-30 21:05:56 -05:00
|
|
|
let ty = ty::expr_ty(tcx, e);
|
|
|
|
let base = classify(base, def_map, tcx);
|
|
|
|
if ty::type_is_integral(ty) {
|
|
|
|
join(integral_const, base)
|
|
|
|
} else if ty::type_is_fp(ty) {
|
|
|
|
join(general_const, base)
|
|
|
|
} else {
|
|
|
|
non_const
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-03 21:59:04 -05:00
|
|
|
ast::expr_field(base, _, _) => {
|
2012-07-30 21:05:56 -05:00
|
|
|
classify(base, def_map, tcx)
|
|
|
|
}
|
|
|
|
|
2012-08-03 21:59:04 -05:00
|
|
|
ast::expr_index(base, idx) => {
|
2012-07-30 21:05:56 -05:00
|
|
|
join(classify(base, def_map, tcx),
|
|
|
|
classify(idx, def_map, tcx))
|
|
|
|
}
|
|
|
|
|
2012-08-03 21:59:04 -05:00
|
|
|
ast::expr_addr_of(ast::m_imm, base) => {
|
2012-07-30 21:05:56 -05:00
|
|
|
classify(base, def_map, tcx)
|
|
|
|
}
|
|
|
|
|
2012-10-11 18:15:12 -05:00
|
|
|
// FIXME: (#3728) we can probably do something CCI-ish
|
2012-07-30 21:05:56 -05:00
|
|
|
// surrounding nonlocal constants. But we don't yet.
|
2012-08-03 21:59:04 -05:00
|
|
|
ast::expr_path(_) => {
|
2012-10-15 14:27:09 -05:00
|
|
|
lookup_constness(tcx, e)
|
2012-07-30 21:05:56 -05:00
|
|
|
}
|
|
|
|
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => non_const
|
2012-07-30 21:05:56 -05:00
|
|
|
};
|
|
|
|
tcx.ccache.insert(did, cn);
|
|
|
|
cn
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-30 15:44:24 -06:00
|
|
|
pub fn lookup_const(tcx: ty::ctxt, e: @expr) -> Option<@expr> {
|
2013-02-05 21:41:45 -06:00
|
|
|
match tcx.def_map.find(&e.id) {
|
2012-11-13 00:10:15 -06:00
|
|
|
Some(ast::def_const(def_id)) => lookup_const_by_id(tcx, def_id),
|
|
|
|
_ => None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-30 15:44:24 -06:00
|
|
|
pub fn lookup_const_by_id(tcx: ty::ctxt,
|
|
|
|
def_id: ast::def_id)
|
|
|
|
-> Option<@expr> {
|
2012-11-13 00:10:15 -06:00
|
|
|
if ast_util::is_local(def_id) {
|
2013-02-05 21:41:45 -06:00
|
|
|
match tcx.items.find(&def_id.node) {
|
2012-11-13 00:10:15 -06:00
|
|
|
None => None,
|
|
|
|
Some(ast_map::node_item(it, _)) => match it.node {
|
|
|
|
item_const(_, const_expr) => Some(const_expr),
|
|
|
|
_ => None
|
|
|
|
},
|
|
|
|
Some(_) => None
|
2012-10-15 14:27:09 -05:00
|
|
|
}
|
2012-11-13 00:10:15 -06:00
|
|
|
} else {
|
2013-03-21 02:27:26 -05:00
|
|
|
let maps = astencode::Maps {
|
|
|
|
mutbl_map: HashMap(),
|
|
|
|
root_map: HashMap(),
|
|
|
|
last_use_map: HashMap(),
|
|
|
|
method_map: HashMap(),
|
|
|
|
vtable_map: HashMap(),
|
|
|
|
write_guard_map: HashMap(),
|
|
|
|
moves_map: HashMap(),
|
|
|
|
capture_map: HashMap()
|
|
|
|
};
|
|
|
|
match csearch::maybe_get_item_ast(tcx, def_id,
|
|
|
|
|a, b, c, d| astencode::decode_inlined_item(a, b, maps, /*bar*/ copy c, d)) {
|
|
|
|
csearch::found(ast::ii_item(item)) => match item.node {
|
|
|
|
item_const(_, const_expr) => Some(const_expr),
|
|
|
|
_ => None
|
|
|
|
},
|
|
|
|
_ => None
|
|
|
|
}
|
2012-10-15 14:27:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-30 15:44:24 -06:00
|
|
|
pub fn lookup_constness(tcx: ty::ctxt, e: @expr) -> constness {
|
2012-10-15 14:27:09 -05:00
|
|
|
match lookup_const(tcx, e) {
|
|
|
|
Some(rhs) => {
|
|
|
|
let ty = ty::expr_ty(tcx, rhs);
|
|
|
|
if ty::type_is_integral(ty) {
|
|
|
|
integral_const
|
|
|
|
} else {
|
|
|
|
general_const
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None => non_const
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-30 15:44:24 -06:00
|
|
|
pub fn process_crate(crate: @ast::crate,
|
|
|
|
def_map: resolve::DefMap,
|
|
|
|
tcx: ty::ctxt) {
|
2013-01-08 16:00:45 -06:00
|
|
|
let v = visit::mk_simple_visitor(@visit::SimpleVisitor {
|
2012-09-04 15:29:32 -05:00
|
|
|
visit_expr_post: |e| { classify(e, def_map, tcx); },
|
|
|
|
.. *visit::default_simple_visitor()
|
2012-07-30 21:05:56 -05:00
|
|
|
});
|
|
|
|
visit::visit_crate(*crate, (), v);
|
|
|
|
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-01-30 15:44:24 -06:00
|
|
|
#[deriving_eq]
|
|
|
|
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),
|
2012-07-14 00:57:48 -05:00
|
|
|
const_str(~str),
|
2012-08-23 17:51:23 -05:00
|
|
|
const_bool(bool)
|
2012-03-22 16:56:56 -05:00
|
|
|
}
|
|
|
|
|
2013-01-30 15:44:24 -06:00
|
|
|
pub fn eval_const_expr(tcx: middle::ty::ctxt, e: @expr) -> const_val {
|
2012-10-15 14:27:09 -05:00
|
|
|
match eval_const_expr_partial(tcx, e) {
|
2013-01-07 16:16:52 -06:00
|
|
|
Ok(ref r) => (/*bad*/copy *r),
|
2013-02-11 21:26:38 -06:00
|
|
|
Err(ref s) => fail!(/*bad*/copy *s)
|
2012-10-15 14:27:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-30 15:44:24 -06:00
|
|
|
pub fn eval_const_expr_partial(tcx: middle::ty::ctxt, e: @expr)
|
|
|
|
-> 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 {
|
2012-08-03 21:59:04 -05:00
|
|
|
expr_unary(neg, 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)),
|
|
|
|
Ok(const_str(_)) => Err(~"Negate on string"),
|
|
|
|
Ok(const_bool(_)) => Err(~"Negate on boolean"),
|
2013-01-07 16:16:52 -06:00
|
|
|
ref err => (/*bad*/copy *err)
|
2012-03-22 16:56:56 -05:00
|
|
|
}
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
expr_unary(not, 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)),
|
|
|
|
_ => Err(~"Not on float or string")
|
2012-03-22 16:56:56 -05:00
|
|
|
}
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
expr_binary(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 {
|
2012-10-15 14:27:09 -05:00
|
|
|
add => Ok(const_float(a + b)),
|
|
|
|
subtract => Ok(const_float(a - b)),
|
|
|
|
mul => Ok(const_float(a * b)),
|
|
|
|
div => Ok(const_float(a / b)),
|
|
|
|
rem => Ok(const_float(a % b)),
|
2012-08-03 21:59:04 -05:00
|
|
|
eq => fromb(a == b),
|
|
|
|
lt => fromb(a < b),
|
|
|
|
le => fromb(a <= b),
|
|
|
|
ne => fromb(a != b),
|
|
|
|
ge => fromb(a >= b),
|
2012-08-23 17:51:23 -05:00
|
|
|
gt => fromb(a > b),
|
2012-10-15 14:27:09 -05: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 {
|
2012-10-15 14:27:09 -05:00
|
|
|
add => Ok(const_int(a + b)),
|
|
|
|
subtract => Ok(const_int(a - b)),
|
|
|
|
mul => Ok(const_int(a * b)),
|
2013-01-23 00:07:00 -06:00
|
|
|
div if b == 0 => Err(~"divide by zero"),
|
2012-10-15 14:27:09 -05:00
|
|
|
div => Ok(const_int(a / b)),
|
2013-01-23 00:07:00 -06:00
|
|
|
rem if b == 0 => Err(~"modulo zero"),
|
2012-10-15 14:27:09 -05:00
|
|
|
rem => Ok(const_int(a % b)),
|
|
|
|
and | bitand => Ok(const_int(a & b)),
|
|
|
|
or | bitor => Ok(const_int(a | b)),
|
|
|
|
bitxor => Ok(const_int(a ^ b)),
|
|
|
|
shl => Ok(const_int(a << b)),
|
|
|
|
shr => Ok(const_int(a >> b)),
|
2012-08-03 21:59:04 -05:00
|
|
|
eq => fromb(a == b),
|
|
|
|
lt => fromb(a < b),
|
|
|
|
le => fromb(a <= b),
|
|
|
|
ne => fromb(a != b),
|
|
|
|
ge => fromb(a >= b),
|
|
|
|
gt => 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 {
|
2012-10-15 14:27:09 -05:00
|
|
|
add => Ok(const_uint(a + b)),
|
|
|
|
subtract => Ok(const_uint(a - b)),
|
|
|
|
mul => Ok(const_uint(a * b)),
|
2013-01-23 00:07:00 -06:00
|
|
|
div if b == 0 => Err(~"divide by zero"),
|
2012-10-15 14:27:09 -05:00
|
|
|
div => Ok(const_uint(a / b)),
|
2013-01-23 00:07:00 -06:00
|
|
|
rem if b == 0 => Err(~"modulo zero"),
|
2012-10-15 14:27:09 -05:00
|
|
|
rem => Ok(const_uint(a % b)),
|
|
|
|
and | bitand => Ok(const_uint(a & b)),
|
|
|
|
or | bitor => Ok(const_uint(a | b)),
|
|
|
|
bitxor => Ok(const_uint(a ^ b)),
|
|
|
|
shl => Ok(const_uint(a << b)),
|
|
|
|
shr => Ok(const_uint(a >> b)),
|
2012-08-03 21:59:04 -05:00
|
|
|
eq => fromb(a == b),
|
|
|
|
lt => fromb(a < b),
|
|
|
|
le => fromb(a <= b),
|
|
|
|
ne => fromb(a != b),
|
|
|
|
ge => fromb(a >= b),
|
2012-08-23 17:51:23 -05:00
|
|
|
gt => 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 {
|
2012-10-15 14:27:09 -05:00
|
|
|
shl => Ok(const_int(a << b)),
|
|
|
|
shr => Ok(const_int(a >> b)),
|
|
|
|
_ => 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 {
|
2012-10-15 14:27:09 -05:00
|
|
|
shl => Ok(const_uint(a << b)),
|
|
|
|
shr => Ok(const_uint(a >> b)),
|
|
|
|
_ => 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 {
|
2012-08-23 17:51:23 -05:00
|
|
|
and => a && b,
|
|
|
|
or => a || b,
|
|
|
|
bitxor => a ^ b,
|
|
|
|
bitand => a & b,
|
|
|
|
bitor => a | b,
|
|
|
|
eq => a == b,
|
|
|
|
ne => a != b,
|
2012-10-15 14:27:09 -05:00
|
|
|
_ => return Err(~"Can't do this op on bools")
|
|
|
|
}))
|
2012-08-23 17:51:23 -05:00
|
|
|
}
|
2012-10-15 14:27:09 -05:00
|
|
|
_ => Err(~"Bad operands for binary")
|
2012-03-22 16:56:56 -05:00
|
|
|
}
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
expr_cast(base, _) => {
|
2012-03-22 16:56:56 -05:00
|
|
|
let ety = ty::expr_ty(tcx, e);
|
2012-10-15 14:27:09 -05:00
|
|
|
let base = eval_const_expr_partial(tcx, base);
|
2012-09-11 18:20:31 -05:00
|
|
|
match ty::get(ety).sty {
|
2012-08-03 21:59:04 -05:00
|
|
|
ty::ty_float(_) => {
|
2012-08-23 17:51:23 -05:00
|
|
|
match base {
|
2012-10-15 14:27:09 -05:00
|
|
|
Ok(const_uint(u)) => Ok(const_float(u as f64)),
|
|
|
|
Ok(const_int(i)) => Ok(const_float(i as f64)),
|
|
|
|
Ok(const_float(_)) => base,
|
|
|
|
_ => Err(~"Can't cast float to str")
|
2012-03-22 16:56:56 -05:00
|
|
|
}
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
ty::ty_uint(_) => {
|
2012-08-23 17:51:23 -05:00
|
|
|
match base {
|
2012-10-15 14:27:09 -05:00
|
|
|
Ok(const_uint(_)) => base,
|
|
|
|
Ok(const_int(i)) => Ok(const_uint(i as u64)),
|
|
|
|
Ok(const_float(f)) => Ok(const_uint(f as u64)),
|
|
|
|
_ => Err(~"Can't cast str to uint")
|
2012-03-22 16:56:56 -05:00
|
|
|
}
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
ty::ty_int(_) | ty::ty_bool => {
|
2012-08-23 17:51:23 -05:00
|
|
|
match base {
|
2012-10-15 14:27:09 -05:00
|
|
|
Ok(const_uint(u)) => Ok(const_int(u as i64)),
|
|
|
|
Ok(const_int(_)) => base,
|
|
|
|
Ok(const_float(f)) => Ok(const_int(f as i64)),
|
|
|
|
_ => Err(~"Can't cast str to int")
|
2012-03-22 16:56:56 -05:00
|
|
|
}
|
|
|
|
}
|
2012-10-15 14:27:09 -05:00
|
|
|
_ => Err(~"Can't cast this type")
|
2012-03-22 16:56:56 -05:00
|
|
|
}
|
|
|
|
}
|
2012-10-15 14:27:09 -05:00
|
|
|
expr_path(_) => {
|
|
|
|
match lookup_const(tcx, e) {
|
|
|
|
Some(actual_e) => eval_const_expr_partial(tcx, actual_e),
|
|
|
|
None => Err(~"Non-constant path in constant expr")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
expr_lit(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
|
2012-10-15 14:27:09 -05:00
|
|
|
expr_vstore(e, _) => eval_const_expr_partial(tcx, e),
|
2012-10-27 19:14:09 -05:00
|
|
|
expr_paren(e) => eval_const_expr_partial(tcx, e),
|
2012-10-15 14:27:09 -05:00
|
|
|
_ => Err(~"Unsupported constant expr")
|
2012-03-22 16:56:56 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-30 15:44:24 -06:00
|
|
|
pub fn lit_to_const(lit: @lit) -> const_val {
|
2012-08-06 14:34:08 -05:00
|
|
|
match lit.node {
|
2013-01-07 16:16:52 -06:00
|
|
|
lit_str(s) => const_str(/*bad*/copy *s),
|
2012-08-03 21:59:04 -05:00
|
|
|
lit_int(n, _) => const_int(n),
|
|
|
|
lit_uint(n, _) => const_uint(n),
|
|
|
|
lit_int_unsuffixed(n) => const_int(n),
|
2012-09-21 21:37:57 -05:00
|
|
|
lit_float(n, _) => const_float(float::from_str(*n).get() as f64),
|
2012-11-07 20:40:34 -06:00
|
|
|
lit_float_unsuffixed(n) =>
|
|
|
|
const_float(float::from_str(*n).get() as f64),
|
2012-08-03 21:59:04 -05:00
|
|
|
lit_nil => const_int(0i64),
|
2012-08-23 17:51:23 -05:00
|
|
|
lit_bool(b) => const_bool(b)
|
2012-03-22 16:56:56 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-30 15:44:24 -06:00
|
|
|
pub fn compare_const_vals(a: const_val, b: const_val) -> int {
|
2013-01-10 12:59:58 -06:00
|
|
|
match (&a, &b) {
|
|
|
|
(&const_int(a), &const_int(b)) => {
|
2012-03-22 16:56:56 -05:00
|
|
|
if a == b {
|
|
|
|
0
|
|
|
|
} else if a < b {
|
|
|
|
-1
|
|
|
|
} else {
|
|
|
|
1
|
|
|
|
}
|
|
|
|
}
|
2013-01-10 12:59:58 -06:00
|
|
|
(&const_uint(a), &const_uint(b)) => {
|
2012-03-22 16:56:56 -05:00
|
|
|
if a == b {
|
|
|
|
0
|
|
|
|
} else if a < b {
|
|
|
|
-1
|
|
|
|
} else {
|
|
|
|
1
|
|
|
|
}
|
|
|
|
}
|
2013-01-10 12:59:58 -06:00
|
|
|
(&const_float(a), &const_float(b)) => {
|
2012-03-22 16:56:56 -05:00
|
|
|
if a == b {
|
|
|
|
0
|
|
|
|
} else if a < b {
|
|
|
|
-1
|
|
|
|
} else {
|
|
|
|
1
|
|
|
|
}
|
|
|
|
}
|
2013-01-10 12:59:58 -06:00
|
|
|
(&const_str(ref a), &const_str(ref b)) => {
|
2012-12-04 12:50:00 -06:00
|
|
|
if (*a) == (*b) {
|
2012-03-22 16:56:56 -05:00
|
|
|
0
|
2012-12-04 12:50:00 -06:00
|
|
|
} else if (*a) < (*b) {
|
2012-03-22 16:56:56 -05:00
|
|
|
-1
|
|
|
|
} else {
|
|
|
|
1
|
|
|
|
}
|
|
|
|
}
|
2013-01-10 12:59:58 -06:00
|
|
|
(&const_bool(a), &const_bool(b)) => {
|
2012-08-23 17:51:23 -05:00
|
|
|
if a == b {
|
|
|
|
0
|
|
|
|
} else if a < b {
|
|
|
|
-1
|
|
|
|
} else {
|
|
|
|
1
|
|
|
|
}
|
|
|
|
}
|
2013-02-11 21:26:38 -06:00
|
|
|
_ => fail!(~"compare_const_vals: ill-typed comparison")
|
2012-03-22 16:56:56 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-30 15:44:24 -06:00
|
|
|
pub fn compare_lit_exprs(tcx: middle::ty::ctxt, a: @expr, b: @expr) -> int {
|
2012-03-22 16:56:56 -05:00
|
|
|
compare_const_vals(eval_const_expr(tcx, a), eval_const_expr(tcx, b))
|
|
|
|
}
|
|
|
|
|
2013-01-30 15:44:24 -06:00
|
|
|
pub fn lit_expr_eq(tcx: middle::ty::ctxt, a: @expr, b: @expr) -> bool {
|
2012-03-22 16:56:56 -05:00
|
|
|
compare_lit_exprs(tcx, a, b) == 0
|
|
|
|
}
|
|
|
|
|
2013-01-30 15:44:24 -06:00
|
|
|
pub fn lit_eq(a: @lit, b: @lit) -> bool {
|
2012-03-22 16:56:56 -05:00
|
|
|
compare_const_vals(lit_to_const(a), lit_to_const(b)) == 0
|
|
|
|
}
|
2012-07-30 21:05:56 -05:00
|
|
|
|
|
|
|
|
|
|
|
// Local Variables:
|
|
|
|
// mode: rust
|
|
|
|
// fill-column: 78;
|
|
|
|
// indent-tabs-mode: nil
|
|
|
|
// c-basic-offset: 4
|
|
|
|
// buffer-file-coding-system: utf-8-unix
|
|
|
|
// End:
|