Implement translation for ConstVal::{Array,Repeat}

This commit is contained in:
Simonas Kazlauskas 2015-12-05 20:35:00 +02:00
parent a5e7a61c49
commit b3720ea933
3 changed files with 12 additions and 11 deletions

View file

@ -108,12 +108,13 @@ pub fn const_lit(cx: &CrateContext, e: &hir::Expr, lit: &ast::Lit)
}
}
pub fn trans_constval<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
pub fn trans_constval<'blk, 'tcx>(bcx: common::Block<'blk, 'tcx>,
cv: &ConstVal,
ty: Ty<'tcx>,
param_substs: &'tcx Substs<'tcx>)
-> ValueRef
{
let ccx = bcx.ccx();
let llty = type_of::type_of(ccx, ty);
match *cv {
ConstVal::Float(v) => C_floating_f64(v, llty),
@ -123,21 +124,19 @@ pub fn trans_constval<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
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);
let expr = bcx.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::Array(id, _) | ConstVal::Repeat(id, _) => {
let expr = ccx.tcx().map.expect_expr(id);
expr::trans(bcx, expr).datum.val
},
ConstVal::Function(_) => {
unimplemented!()
},
ConstVal::Array(..) => {
unimplemented!()
},
ConstVal::Repeat(..) => {
unimplemented!()
},
}
}

View file

@ -26,11 +26,11 @@ impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> {
-> OperandRef<'tcx>
{
let ccx = bcx.ccx();
let val = consts::trans_constval(ccx, cv, ty, bcx.fcx.param_substs);
let val = consts::trans_constval(bcx, cv, ty, bcx.fcx.param_substs);
let val = if common::type_is_immediate(ccx, ty) {
Immediate(val)
OperandValue::Immediate(val)
} else {
Ref(val)
OperandValue::Ref(val)
};
assert!(!ty.has_erasable_regions());

View file

@ -50,6 +50,8 @@ impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> {
let did = inline::maybe_instantiate_inline(bcx.ccx(), did);
let expr = const_eval::lookup_const_by_id(bcx.tcx(), did, None)
.expect("def was const, but lookup_const_by_id failed");
// FIXME: this is falling back to translating from HIR. This is not easy to fix,
// because we would have somehow adapt const_eval to work on MIR rather than HIR.
let d = expr::trans(bcx, expr);
OperandRef::from_rvalue_datum(d.datum.to_rvalue_datum(d.bcx, "").datum)
}