Auto merge of #30265 - oli-obk:const_val_trans, r=pnkfelix

r? @nagisa

I'm going to need the `ConstVal` -> `ValueRef` translation to start removing trans/consts piece by piece. If you need anything implemented in the translation, feel free to assign an issue to me.
This commit is contained in:
bors 2015-12-11 08:12:41 +00:00
commit 81dd3824ff
2 changed files with 40 additions and 42 deletions

View File

@ -31,6 +31,7 @@ use trans::{adt, closure, debuginfo, expr, inline, machine};
use trans::base::{self, push_ctxt};
use trans::common::{self, type_is_sized, ExprOrMethodCall, node_id_substs, C_nil, const_get_elt};
use trans::common::{CrateContext, C_integral, C_floating, C_bool, C_str_slice, C_bytes, val_ty};
use trans::common::C_floating_f64;
use trans::common::{C_struct, C_undef, const_to_opt_int, const_to_opt_uint, VariantInfo, C_uint};
use trans::common::{type_is_fat_ptr, Field, C_vector, C_array, C_null, ExprId, MethodCallKey};
use trans::declare;
@ -107,6 +108,39 @@ pub fn const_lit(cx: &CrateContext, e: &hir::Expr, lit: &ast::Lit)
}
}
pub fn trans_constval<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
cv: &ConstVal,
ty: Ty<'tcx>,
param_substs: &'tcx Substs<'tcx>)
-> ValueRef
{
let llty = type_of::type_of(ccx, ty);
match *cv {
ConstVal::Float(v) => C_floating_f64(v, llty),
ConstVal::Bool(v) => C_bool(ccx, v),
ConstVal::Int(v) => C_integral(llty, v as u64, true),
ConstVal::Uint(v) => C_integral(llty, v, false),
ConstVal::Str(ref v) => C_str_slice(ccx, v.clone()),
ConstVal::ByteStr(ref v) => addr_of(ccx, C_bytes(ccx, v), 1, "byte_str"),
ConstVal::Struct(id) | ConstVal::Tuple(id) => {
let expr = ccx.tcx().map.expect_expr(id);
match const_expr(ccx, expr, param_substs, None, TrueConst::Yes) {
Ok((val, _)) => val,
Err(e) => panic!("const eval failure: {}", e.description()),
}
},
ConstVal::Function(_) => {
unimplemented!()
},
ConstVal::Array(..) => {
unimplemented!()
},
ConstVal::Repeat(..) => {
unimplemented!()
},
}
}
pub fn ptrcast(val: ValueRef, ty: Type) -> ValueRef {
unsafe {
llvm::LLVMConstPointerCast(val, ty.to_ref())

View File

@ -11,10 +11,8 @@
use middle::ty::{Ty, HasTypeFlags};
use rustc::middle::const_eval::ConstVal;
use rustc::mir::repr as mir;
use trans::consts::{self, TrueConst};
use trans::consts;
use trans::common::{self, Block};
use trans::common::{C_bool, C_bytes, C_floating_f64, C_integral, C_str_slice};
use trans::type_of;
use super::operand::OperandRef;
use super::MirContext;
@ -29,45 +27,11 @@ impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> {
use super::operand::OperandValue::{Ref, Immediate};
let ccx = bcx.ccx();
let llty = type_of::type_of(ccx, ty);
let val = match *cv {
ConstVal::Float(v) => Immediate(C_floating_f64(v, llty)),
ConstVal::Bool(v) => Immediate(C_bool(ccx, v)),
ConstVal::Int(v) => Immediate(C_integral(llty, v as u64, true)),
ConstVal::Uint(v) => Immediate(C_integral(llty, v, false)),
ConstVal::Str(ref v) => Immediate(C_str_slice(ccx, v.clone())),
ConstVal::ByteStr(ref v) => {
Immediate(consts::addr_of(ccx,
C_bytes(ccx, v),
1,
"byte_str"))
}
ConstVal::Struct(id) | ConstVal::Tuple(id) => {
let expr = bcx.tcx().map.expect_expr(id);
let (llval, _) = match consts::const_expr(ccx,
expr,
bcx.fcx.param_substs,
None,
TrueConst::Yes) {
Ok(v) => v,
Err(_) => panic!("constant eval failure"),
};
if common::type_is_immediate(bcx.ccx(), ty) {
Immediate(llval)
} else {
Ref(llval)
}
}
ConstVal::Function(_) => {
unimplemented!()
}
ConstVal::Array(..) => {
unimplemented!()
}
ConstVal::Repeat(..) => {
unimplemented!()
}
let val = consts::trans_constval(ccx, cv, ty, bcx.fcx.param_substs);
let val = if common::type_is_immediate(ccx, ty) {
Immediate(val)
} else {
Ref(val)
};
assert!(!ty.has_erasable_regions());