Rustup part 2/2
This commit is contained in:
parent
79993e63a0
commit
c47ee6b521
4 changed files with 48 additions and 37 deletions
|
@ -38,36 +38,45 @@ impl<'a, 'tcx, M: Machine<'tcx>> EvalContext<'a, 'tcx, M> {
|
||||||
self.cast_from_int(val as u128, ty, val < 0)
|
self.cast_from_int(val as u128, ty, val < 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn int_to_int(&self, v: i128, ty: IntTy) -> u128 {
|
||||||
|
match ty {
|
||||||
|
IntTy::I8 => v as i8 as u128,
|
||||||
|
IntTy::I16 => v as i16 as u128,
|
||||||
|
IntTy::I32 => v as i32 as u128,
|
||||||
|
IntTy::I64 => v as i64 as u128,
|
||||||
|
IntTy::I128 => v as u128,
|
||||||
|
IntTy::Is => {
|
||||||
|
let ty = self.tcx.sess.target.isize_ty;
|
||||||
|
self.int_to_int(v, ty)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fn int_to_uint(&self, v: u128, ty: UintTy) -> u128 {
|
||||||
|
match ty {
|
||||||
|
UintTy::U8 => v as u8 as u128,
|
||||||
|
UintTy::U16 => v as u16 as u128,
|
||||||
|
UintTy::U32 => v as u32 as u128,
|
||||||
|
UintTy::U64 => v as u64 as u128,
|
||||||
|
UintTy::U128 => v,
|
||||||
|
UintTy::Us => {
|
||||||
|
let ty = self.tcx.sess.target.usize_ty;
|
||||||
|
self.int_to_uint(v, ty)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn cast_from_int(
|
fn cast_from_int(
|
||||||
&self,
|
&self,
|
||||||
v: u128,
|
v: u128,
|
||||||
ty: ty::Ty<'tcx>,
|
ty: ty::Ty<'tcx>,
|
||||||
negative: bool,
|
negative: bool,
|
||||||
) -> EvalResult<'tcx, PrimVal> {
|
) -> EvalResult<'tcx, PrimVal> {
|
||||||
|
trace!("cast_from_int: {}, {}, {}", v, ty, negative);
|
||||||
use rustc::ty::TypeVariants::*;
|
use rustc::ty::TypeVariants::*;
|
||||||
match ty.sty {
|
match ty.sty {
|
||||||
// Casts to bool are not permitted by rustc, no need to handle them here.
|
// Casts to bool are not permitted by rustc, no need to handle them here.
|
||||||
TyInt(IntTy::I8) => Ok(PrimVal::Bytes(v as i128 as i8 as u128)),
|
TyInt(ty) => Ok(PrimVal::Bytes(self.int_to_int(v as i128, ty))),
|
||||||
TyInt(IntTy::I16) => Ok(PrimVal::Bytes(v as i128 as i16 as u128)),
|
TyUint(ty) => Ok(PrimVal::Bytes(self.int_to_uint(v, ty))),
|
||||||
TyInt(IntTy::I32) => Ok(PrimVal::Bytes(v as i128 as i32 as u128)),
|
|
||||||
TyInt(IntTy::I64) => Ok(PrimVal::Bytes(v as i128 as i64 as u128)),
|
|
||||||
TyInt(IntTy::I128) => Ok(PrimVal::Bytes(v as u128)),
|
|
||||||
|
|
||||||
TyUint(UintTy::U8) => Ok(PrimVal::Bytes(v as u8 as u128)),
|
|
||||||
TyUint(UintTy::U16) => Ok(PrimVal::Bytes(v as u16 as u128)),
|
|
||||||
TyUint(UintTy::U32) => Ok(PrimVal::Bytes(v as u32 as u128)),
|
|
||||||
TyUint(UintTy::U64) => Ok(PrimVal::Bytes(v as u64 as u128)),
|
|
||||||
TyUint(UintTy::U128) => Ok(PrimVal::Bytes(v)),
|
|
||||||
|
|
||||||
TyInt(IntTy::Is) => {
|
|
||||||
let ty = self.tcx.types.isize;
|
|
||||||
self.cast_from_int(v, ty, negative)
|
|
||||||
}
|
|
||||||
|
|
||||||
TyUint(UintTy::Us) => {
|
|
||||||
let ty = self.tcx.types.usize;
|
|
||||||
self.cast_from_int(v, ty, negative)
|
|
||||||
}
|
|
||||||
|
|
||||||
TyFloat(FloatTy::F64) if negative => Ok(PrimVal::from_f64(v as i128 as f64)),
|
TyFloat(FloatTy::F64) if negative => Ok(PrimVal::from_f64(v as i128 as f64)),
|
||||||
TyFloat(FloatTy::F64) => Ok(PrimVal::from_f64(v as f64)),
|
TyFloat(FloatTy::F64) => Ok(PrimVal::from_f64(v as f64)),
|
||||||
|
|
|
@ -240,10 +240,20 @@ impl<'a, 'tcx, M: Machine<'tcx>> EvalContext<'a, 'tcx, M> {
|
||||||
Str(ref s) => return self.str_to_value(s),
|
Str(ref s) => return self.str_to_value(s),
|
||||||
|
|
||||||
ByteStr(ref bs) => {
|
ByteStr(ref bs) => {
|
||||||
let ptr = self.memory.allocate_cached(bs)?;
|
let ptr = self.memory.allocate_cached(bs.data)?;
|
||||||
PrimVal::Ptr(ptr)
|
PrimVal::Ptr(ptr)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Unevaluated(def_id, substs) => {
|
||||||
|
let instance = self.resolve_associated_const(def_id, substs);
|
||||||
|
let cid = GlobalId {
|
||||||
|
instance,
|
||||||
|
promoted: None,
|
||||||
|
};
|
||||||
|
return Ok(Value::ByRef(*self.globals.get(&cid).expect("static/const not cached")));
|
||||||
|
}
|
||||||
|
|
||||||
|
Aggregate(..) |
|
||||||
Variant(_) => unimplemented!(),
|
Variant(_) => unimplemented!(),
|
||||||
// function items are zero sized and thus have no readable value
|
// function items are zero sized and thus have no readable value
|
||||||
Function(..) => PrimVal::Undef,
|
Function(..) => PrimVal::Undef,
|
||||||
|
@ -1284,16 +1294,7 @@ impl<'a, 'tcx, M: Machine<'tcx>> EvalContext<'a, 'tcx, M> {
|
||||||
use rustc::mir::Literal;
|
use rustc::mir::Literal;
|
||||||
let mir::Constant { ref literal, .. } = **constant;
|
let mir::Constant { ref literal, .. } = **constant;
|
||||||
let value = match *literal {
|
let value = match *literal {
|
||||||
Literal::Value { ref value } => self.const_to_value(value)?,
|
Literal::Value { ref value } => self.const_to_value(&value.val)?,
|
||||||
|
|
||||||
Literal::Item { def_id, substs } => {
|
|
||||||
let instance = self.resolve_associated_const(def_id, substs);
|
|
||||||
let cid = GlobalId {
|
|
||||||
instance,
|
|
||||||
promoted: None,
|
|
||||||
};
|
|
||||||
Value::ByRef(*self.globals.get(&cid).expect("static/const not cached"))
|
|
||||||
}
|
|
||||||
|
|
||||||
Literal::Promoted { index } => {
|
Literal::Promoted { index } => {
|
||||||
let cid = GlobalId {
|
let cid = GlobalId {
|
||||||
|
@ -2501,7 +2502,7 @@ struct AssociatedTypeNormalizer<'a, 'tcx: 'a> {
|
||||||
|
|
||||||
impl<'a, 'tcx> AssociatedTypeNormalizer<'a, 'tcx> {
|
impl<'a, 'tcx> AssociatedTypeNormalizer<'a, 'tcx> {
|
||||||
fn fold<T: TypeFoldable<'tcx>>(&mut self, value: &T) -> T {
|
fn fold<T: TypeFoldable<'tcx>>(&mut self, value: &T) -> T {
|
||||||
if !value.has_projection_types() {
|
if !value.has_projections() {
|
||||||
value.clone()
|
value.clone()
|
||||||
} else {
|
} else {
|
||||||
value.fold_with(self)
|
value.fold_with(self)
|
||||||
|
@ -2515,7 +2516,7 @@ impl<'a, 'tcx> ::rustc::ty::fold::TypeFolder<'tcx, 'tcx> for AssociatedTypeNorma
|
||||||
}
|
}
|
||||||
|
|
||||||
fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
|
fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
|
||||||
if !ty.has_projection_types() {
|
if !ty.has_projections() {
|
||||||
ty
|
ty
|
||||||
} else {
|
} else {
|
||||||
self.tcx.normalize_associated_type(&ty)
|
self.tcx.normalize_associated_type(&ty)
|
||||||
|
|
|
@ -10,6 +10,7 @@ use rustc::traits::Reveal;
|
||||||
use rustc::ty;
|
use rustc::ty;
|
||||||
use rustc::ty::layout::Layout;
|
use rustc::ty::layout::Layout;
|
||||||
use rustc::ty::subst::Substs;
|
use rustc::ty::subst::Substs;
|
||||||
|
use rustc::middle::const_val::ConstVal;
|
||||||
|
|
||||||
use super::{EvalResult, EvalContext, StackPopCleanup, PtrAndAlign, GlobalId, Lvalue,
|
use super::{EvalResult, EvalContext, StackPopCleanup, PtrAndAlign, GlobalId, Lvalue,
|
||||||
MemoryKind, Machine, PrimVal};
|
MemoryKind, Machine, PrimVal};
|
||||||
|
@ -300,8 +301,7 @@ impl<'a, 'b, 'tcx, M: Machine<'tcx>> Visitor<'tcx> for ConstantExtractor<'a, 'b,
|
||||||
self.super_constant(constant, location);
|
self.super_constant(constant, location);
|
||||||
match constant.literal {
|
match constant.literal {
|
||||||
// already computed by rustc
|
// already computed by rustc
|
||||||
mir::Literal::Value { .. } => {}
|
mir::Literal::Value { value: &ty::Const { val: ConstVal::Unevaluated(def_id, substs), .. } } => {
|
||||||
mir::Literal::Item { def_id, substs } => {
|
|
||||||
self.try(|this| {
|
self.try(|this| {
|
||||||
this.ecx.global_item(
|
this.ecx.global_item(
|
||||||
def_id,
|
def_id,
|
||||||
|
@ -311,6 +311,7 @@ impl<'a, 'b, 'tcx, M: Machine<'tcx>> Visitor<'tcx> for ConstantExtractor<'a, 'b,
|
||||||
)
|
)
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
mir::Literal::Value { .. } => {}
|
||||||
mir::Literal::Promoted { index } => {
|
mir::Literal::Promoted { index } => {
|
||||||
let cid = GlobalId {
|
let cid = GlobalId {
|
||||||
instance: self.instance,
|
instance: self.instance,
|
||||||
|
|
|
@ -246,7 +246,7 @@ impl<'a, 'tcx, M: Machine<'tcx>> EvalContext<'a, 'tcx, M> {
|
||||||
{
|
{
|
||||||
let param_env = ty::ParamEnv::empty(Reveal::All);
|
let param_env = ty::ParamEnv::empty(Reveal::All);
|
||||||
|
|
||||||
if !value.has_projection_types() {
|
if !value.has_projections() {
|
||||||
return value.clone();
|
return value.clone();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue