2016-06-23 01:03:58 -06:00
|
|
|
use rustc::hir::def_id::DefId;
|
2016-11-03 10:38:08 +01:00
|
|
|
use rustc::mir;
|
2016-08-27 01:44:46 -06:00
|
|
|
use rustc::traits::{self, Reveal};
|
2016-06-23 01:03:58 -06:00
|
|
|
use rustc::ty::fold::TypeFoldable;
|
|
|
|
use rustc::ty::layout::Layout;
|
2016-08-27 01:44:46 -06:00
|
|
|
use rustc::ty::subst::Substs;
|
2016-06-23 01:03:58 -06:00
|
|
|
use rustc::ty::{self, Ty, TyCtxt, BareFnTy};
|
2016-09-19 02:19:31 -06:00
|
|
|
use std::rc::Rc;
|
2016-06-23 01:03:58 -06:00
|
|
|
use syntax::codemap::{DUMMY_SP, Span};
|
2016-09-19 02:19:31 -06:00
|
|
|
use syntax::{ast, attr};
|
2016-06-23 01:03:58 -06:00
|
|
|
|
|
|
|
use error::{EvalError, EvalResult};
|
2016-09-09 12:51:14 +02:00
|
|
|
use memory::Pointer;
|
2016-09-19 19:40:56 -06:00
|
|
|
use primval::PrimVal;
|
2016-10-14 03:31:45 -06:00
|
|
|
use super::{EvalContext, Lvalue, IntegerExt, StackPopCleanup};
|
2016-09-23 10:27:14 +02:00
|
|
|
use super::value::Value;
|
2016-06-23 01:03:58 -06:00
|
|
|
|
2016-09-20 16:05:30 +02:00
|
|
|
mod intrinsics;
|
|
|
|
|
2016-06-23 01:03:58 -06:00
|
|
|
impl<'a, 'tcx> EvalContext<'a, 'tcx> {
|
2016-07-06 17:55:05 +02:00
|
|
|
|
|
|
|
pub(super) fn goto_block(&mut self, target: mir::BasicBlock) {
|
|
|
|
self.frame_mut().block = target;
|
|
|
|
self.frame_mut().stmt = 0;
|
|
|
|
}
|
|
|
|
|
2016-06-23 01:03:58 -06:00
|
|
|
pub(super) fn eval_terminator(
|
|
|
|
&mut self,
|
|
|
|
terminator: &mir::Terminator<'tcx>,
|
|
|
|
) -> EvalResult<'tcx, ()> {
|
2016-11-03 10:38:08 +01:00
|
|
|
use rustc::mir::TerminatorKind::*;
|
2016-06-23 01:03:58 -06:00
|
|
|
match terminator.kind {
|
2016-09-09 17:44:04 +02:00
|
|
|
Return => self.pop_stack_frame()?,
|
2016-06-23 01:03:58 -06:00
|
|
|
|
2016-07-06 17:55:05 +02:00
|
|
|
Goto { target } => self.goto_block(target),
|
2016-06-23 01:03:58 -06:00
|
|
|
|
|
|
|
If { ref cond, targets: (then_target, else_target) } => {
|
2016-10-21 03:17:53 -06:00
|
|
|
let cond_val = self.eval_operand_to_primval(cond)?.try_as_bool()?;
|
2016-07-06 17:55:05 +02:00
|
|
|
self.goto_block(if cond_val { then_target } else { else_target });
|
2016-06-23 01:03:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
SwitchInt { ref discr, ref values, ref targets, .. } => {
|
2016-10-15 23:59:01 -06:00
|
|
|
let discr_val = self.eval_and_read_lvalue(discr)?;
|
2016-06-23 01:03:58 -06:00
|
|
|
let discr_ty = self.lvalue_ty(discr);
|
2016-09-27 11:10:25 +02:00
|
|
|
let discr_prim = self.value_to_primval(discr_val, discr_ty)?;
|
2016-06-23 01:03:58 -06:00
|
|
|
|
|
|
|
// Branch to the `otherwise` case by default, if no match is found.
|
|
|
|
let mut target_block = targets[targets.len() - 1];
|
|
|
|
|
2016-09-19 02:19:31 -06:00
|
|
|
for (index, const_val) in values.iter().enumerate() {
|
2016-09-27 11:10:25 +02:00
|
|
|
let val = self.const_to_value(const_val)?;
|
|
|
|
let prim = self.value_to_primval(val, discr_ty)?;
|
|
|
|
if discr_prim == prim {
|
2016-06-23 01:03:58 -06:00
|
|
|
target_block = targets[index];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-06 17:55:05 +02:00
|
|
|
self.goto_block(target_block);
|
2016-06-23 01:03:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
Switch { ref discr, ref targets, adt_def } => {
|
2016-10-15 19:48:30 -06:00
|
|
|
// FIXME(solson)
|
|
|
|
let lvalue = self.eval_lvalue(discr)?;
|
|
|
|
let lvalue = self.force_allocation(lvalue)?;
|
|
|
|
|
|
|
|
let adt_ptr = lvalue.to_ptr();
|
2016-06-23 01:03:58 -06:00
|
|
|
let adt_ty = self.lvalue_ty(discr);
|
|
|
|
let discr_val = self.read_discriminant_value(adt_ptr, adt_ty)?;
|
|
|
|
let matching = adt_def.variants.iter()
|
|
|
|
.position(|v| discr_val == v.disr_val.to_u64_unchecked());
|
|
|
|
|
|
|
|
match matching {
|
2016-07-06 17:55:05 +02:00
|
|
|
Some(i) => self.goto_block(targets[i]),
|
2016-06-23 01:03:58 -06:00
|
|
|
None => return Err(EvalError::InvalidDiscriminant),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Call { ref func, ref args, ref destination, .. } => {
|
2016-07-06 17:55:05 +02:00
|
|
|
let destination = match *destination {
|
2016-10-14 03:31:45 -06:00
|
|
|
Some((ref lv, target)) => Some((self.eval_lvalue(lv)?, target)),
|
2016-07-06 17:55:05 +02:00
|
|
|
None => None,
|
|
|
|
};
|
2016-06-23 01:03:58 -06:00
|
|
|
|
|
|
|
let func_ty = self.operand_ty(func);
|
|
|
|
match func_ty.sty {
|
|
|
|
ty::TyFnPtr(bare_fn_ty) => {
|
2016-09-19 19:01:28 -06:00
|
|
|
let fn_ptr = self.eval_operand_to_primval(func)?
|
2016-10-20 04:42:19 -06:00
|
|
|
.expect_fn_ptr("TyFnPtr callee did not evaluate to FnPtr");
|
2016-09-09 12:51:14 +02:00
|
|
|
let (def_id, substs, fn_ty) = self.memory.get_fn(fn_ptr.alloc_id)?;
|
2016-06-23 01:03:58 -06:00
|
|
|
if fn_ty != bare_fn_ty {
|
|
|
|
return Err(EvalError::FunctionPointerTyMismatch(fn_ty, bare_fn_ty));
|
|
|
|
}
|
2016-07-06 17:55:05 +02:00
|
|
|
self.eval_fn_call(def_id, substs, bare_fn_ty, destination, args,
|
2016-06-23 01:03:58 -06:00
|
|
|
terminator.source_info.span)?
|
|
|
|
},
|
|
|
|
ty::TyFnDef(def_id, substs, fn_ty) => {
|
2016-07-06 17:55:05 +02:00
|
|
|
self.eval_fn_call(def_id, substs, fn_ty, destination, args,
|
2016-06-23 01:03:58 -06:00
|
|
|
terminator.source_info.span)?
|
|
|
|
}
|
|
|
|
|
|
|
|
_ => return Err(EvalError::Unimplemented(format!("can't handle callee of type {:?}", func_ty))),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Drop { ref location, target, .. } => {
|
2016-11-03 13:27:35 +01:00
|
|
|
let val = self.eval_and_read_lvalue(location)?;
|
2016-10-15 19:48:30 -06:00
|
|
|
|
2016-06-23 01:03:58 -06:00
|
|
|
let ty = self.lvalue_ty(location);
|
2016-11-03 13:27:35 +01:00
|
|
|
self.drop(val, ty)?;
|
2016-07-06 17:55:05 +02:00
|
|
|
self.goto_block(target);
|
2016-06-23 01:03:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
Assert { ref cond, expected, ref msg, target, .. } => {
|
2016-10-21 03:17:53 -06:00
|
|
|
let cond_val = self.eval_operand_to_primval(cond)?.try_as_bool()?;
|
2016-09-19 02:19:31 -06:00
|
|
|
if expected == cond_val {
|
2016-07-06 17:55:05 +02:00
|
|
|
self.goto_block(target);
|
2016-06-23 01:03:58 -06:00
|
|
|
} else {
|
|
|
|
return match *msg {
|
|
|
|
mir::AssertMessage::BoundsCheck { ref len, ref index } => {
|
2016-09-19 19:01:28 -06:00
|
|
|
let span = terminator.source_info.span;
|
|
|
|
let len = self.eval_operand_to_primval(len).expect("can't eval len")
|
|
|
|
.expect_uint("BoundsCheck len wasn't a uint");
|
|
|
|
let index = self.eval_operand_to_primval(index)
|
|
|
|
.expect("can't eval index")
|
|
|
|
.expect_uint("BoundsCheck index wasn't a uint");
|
|
|
|
Err(EvalError::ArrayIndexOutOfBounds(span, len, index))
|
2016-06-23 01:03:58 -06:00
|
|
|
},
|
2016-09-19 19:01:28 -06:00
|
|
|
mir::AssertMessage::Math(ref err) =>
|
|
|
|
Err(EvalError::Math(terminator.source_info.span, err.clone())),
|
2016-06-23 01:03:58 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
DropAndReplace { .. } => unimplemented!(),
|
|
|
|
Resume => unimplemented!(),
|
|
|
|
Unreachable => unimplemented!(),
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn eval_fn_call(
|
|
|
|
&mut self,
|
|
|
|
def_id: DefId,
|
|
|
|
substs: &'tcx Substs<'tcx>,
|
|
|
|
fn_ty: &'tcx BareFnTy,
|
2016-10-21 10:29:56 +02:00
|
|
|
destination: Option<(Lvalue<'tcx>, mir::BasicBlock)>,
|
2016-09-19 19:40:56 -06:00
|
|
|
arg_operands: &[mir::Operand<'tcx>],
|
2016-06-23 01:03:58 -06:00
|
|
|
span: Span,
|
|
|
|
) -> EvalResult<'tcx, ()> {
|
|
|
|
use syntax::abi::Abi;
|
|
|
|
match fn_ty.abi {
|
|
|
|
Abi::RustIntrinsic => {
|
2016-08-27 01:44:46 -06:00
|
|
|
let ty = fn_ty.sig.0.output;
|
|
|
|
let layout = self.type_layout(ty);
|
2016-07-06 17:55:05 +02:00
|
|
|
let (ret, target) = destination.unwrap();
|
2016-09-26 17:49:30 +02:00
|
|
|
self.call_intrinsic(def_id, substs, arg_operands, ret, ty, layout)?;
|
2016-07-06 17:55:05 +02:00
|
|
|
self.goto_block(target);
|
|
|
|
Ok(())
|
2016-06-23 01:03:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
Abi::C => {
|
2016-08-27 01:44:46 -06:00
|
|
|
let ty = fn_ty.sig.0.output;
|
|
|
|
let size = self.type_size(ty);
|
2016-07-06 17:55:05 +02:00
|
|
|
let (ret, target) = destination.unwrap();
|
2016-09-19 19:40:56 -06:00
|
|
|
self.call_c_abi(def_id, arg_operands, ret, size)?;
|
2016-07-06 17:55:05 +02:00
|
|
|
self.goto_block(target);
|
|
|
|
Ok(())
|
2016-06-23 01:03:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
Abi::Rust | Abi::RustCall => {
|
2016-09-19 19:40:56 -06:00
|
|
|
let mut args = Vec::new();
|
|
|
|
for arg in arg_operands {
|
|
|
|
let arg_val = self.eval_operand(arg)?;
|
|
|
|
let arg_ty = self.operand_ty(arg);
|
|
|
|
args.push((arg_val, arg_ty));
|
2016-06-23 01:03:58 -06:00
|
|
|
}
|
|
|
|
|
2016-09-09 12:51:14 +02:00
|
|
|
// Only trait methods can have a Self parameter.
|
|
|
|
let (resolved_def_id, resolved_substs) =
|
|
|
|
if let Some(trait_id) = self.tcx.trait_of_item(def_id) {
|
2016-09-19 19:40:56 -06:00
|
|
|
self.trait_method(trait_id, def_id, substs, &mut args)?
|
2016-09-09 12:51:14 +02:00
|
|
|
} else {
|
|
|
|
(def_id, substs)
|
|
|
|
};
|
|
|
|
|
2016-09-27 17:01:06 +02:00
|
|
|
let mir = self.load_mir(resolved_def_id)?;
|
2016-10-14 03:31:45 -06:00
|
|
|
let (return_lvalue, return_to_block) = match destination {
|
|
|
|
Some((lvalue, block)) => (lvalue, StackPopCleanup::Goto(block)),
|
|
|
|
None => {
|
|
|
|
// FIXME(solson)
|
|
|
|
let lvalue = Lvalue::from_ptr(Pointer::never_ptr());
|
|
|
|
(lvalue, StackPopCleanup::None)
|
|
|
|
}
|
2016-07-06 17:55:05 +02:00
|
|
|
};
|
2016-10-14 03:31:45 -06:00
|
|
|
|
|
|
|
self.push_stack_frame(
|
|
|
|
resolved_def_id,
|
|
|
|
span,
|
|
|
|
mir,
|
|
|
|
resolved_substs,
|
|
|
|
return_lvalue,
|
2016-11-03 10:38:08 +01:00
|
|
|
return_to_block,
|
2016-10-14 03:31:45 -06:00
|
|
|
)?;
|
2016-06-23 01:03:58 -06:00
|
|
|
|
2016-10-15 19:48:30 -06:00
|
|
|
let arg_locals = self.frame().mir.args_iter();
|
|
|
|
for (arg_local, (arg_val, arg_ty)) in arg_locals.zip(args) {
|
2016-10-15 23:31:42 -06:00
|
|
|
let dest = self.eval_lvalue(&mir::Lvalue::Local(arg_local))?;
|
2016-09-19 19:40:56 -06:00
|
|
|
self.write_value(arg_val, dest, arg_ty)?;
|
2016-06-23 01:03:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
abi => Err(EvalError::Unimplemented(format!("can't handle function with {:?} ABI", abi))),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn read_discriminant_value(&self, adt_ptr: Pointer, adt_ty: Ty<'tcx>) -> EvalResult<'tcx, u64> {
|
|
|
|
use rustc::ty::layout::Layout::*;
|
|
|
|
let adt_layout = self.type_layout(adt_ty);
|
|
|
|
|
|
|
|
let discr_val = match *adt_layout {
|
|
|
|
General { discr, .. } | CEnum { discr, .. } => {
|
|
|
|
let discr_size = discr.size().bytes();
|
|
|
|
self.memory.read_uint(adt_ptr, discr_size as usize)?
|
|
|
|
}
|
|
|
|
|
|
|
|
RawNullablePointer { nndiscr, .. } => {
|
|
|
|
self.read_nonnull_discriminant_value(adt_ptr, nndiscr)?
|
|
|
|
}
|
|
|
|
|
|
|
|
StructWrappedNullablePointer { nndiscr, ref discrfield, .. } => {
|
|
|
|
let offset = self.nonnull_offset(adt_ty, nndiscr, discrfield)?;
|
|
|
|
let nonnull = adt_ptr.offset(offset.bytes() as isize);
|
|
|
|
self.read_nonnull_discriminant_value(nonnull, nndiscr)?
|
|
|
|
}
|
|
|
|
|
|
|
|
// The discriminant_value intrinsic returns 0 for non-sum types.
|
|
|
|
Array { .. } | FatPointer { .. } | Scalar { .. } | Univariant { .. } |
|
2016-09-07 10:12:15 +02:00
|
|
|
Vector { .. } | UntaggedUnion { .. } => 0,
|
2016-06-23 01:03:58 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
Ok(discr_val)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn read_nonnull_discriminant_value(&self, ptr: Pointer, nndiscr: u64) -> EvalResult<'tcx, u64> {
|
|
|
|
let not_null = match self.memory.read_usize(ptr) {
|
|
|
|
Ok(0) => false,
|
|
|
|
Ok(_) | Err(EvalError::ReadPointerAsBytes) => true,
|
|
|
|
Err(e) => return Err(e),
|
|
|
|
};
|
|
|
|
assert!(nndiscr == 0 || nndiscr == 1);
|
|
|
|
Ok(if not_null { nndiscr } else { 1 - nndiscr })
|
|
|
|
}
|
|
|
|
|
|
|
|
fn call_c_abi(
|
|
|
|
&mut self,
|
|
|
|
def_id: DefId,
|
|
|
|
args: &[mir::Operand<'tcx>],
|
2016-10-21 10:29:56 +02:00
|
|
|
dest: Lvalue<'tcx>,
|
2016-06-23 01:03:58 -06:00
|
|
|
dest_size: usize,
|
|
|
|
) -> EvalResult<'tcx, ()> {
|
|
|
|
let name = self.tcx.item_name(def_id);
|
|
|
|
let attrs = self.tcx.get_attrs(def_id);
|
|
|
|
let link_name = match attr::first_attr_value_str_by_name(&attrs, "link_name") {
|
|
|
|
Some(ln) => ln.clone(),
|
|
|
|
None => name.as_str(),
|
|
|
|
};
|
|
|
|
|
2016-09-23 10:27:14 +02:00
|
|
|
let args_res: EvalResult<Vec<Value>> = args.iter()
|
|
|
|
.map(|arg| self.eval_operand(arg))
|
2016-06-23 01:03:58 -06:00
|
|
|
.collect();
|
|
|
|
let args = args_res?;
|
|
|
|
|
|
|
|
if link_name.starts_with("pthread_") {
|
|
|
|
warn!("ignoring C ABI call: {}", link_name);
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
2016-09-23 10:27:14 +02:00
|
|
|
let usize = self.tcx.types.usize;
|
|
|
|
|
2016-06-23 01:03:58 -06:00
|
|
|
match &link_name[..] {
|
|
|
|
"__rust_allocate" => {
|
2016-10-14 03:31:45 -06:00
|
|
|
let size = self.value_to_primval(args[0], usize)?
|
|
|
|
.expect_uint("__rust_allocate first arg not usize");
|
|
|
|
let align = self.value_to_primval(args[1], usize)?
|
|
|
|
.expect_uint("__rust_allocate second arg not usize");
|
2016-07-05 14:27:27 +02:00
|
|
|
let ptr = self.memory.allocate(size as usize, align as usize)?;
|
2016-10-20 04:42:19 -06:00
|
|
|
self.write_primval(dest, PrimVal::from_ptr(ptr))?;
|
2016-06-23 01:03:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
"__rust_reallocate" => {
|
2016-09-23 10:27:14 +02:00
|
|
|
let ptr = args[0].read_ptr(&self.memory)?;
|
|
|
|
let size = self.value_to_primval(args[2], usize)?.expect_uint("__rust_reallocate third arg not usize");
|
|
|
|
let align = self.value_to_primval(args[3], usize)?.expect_uint("__rust_reallocate fourth arg not usize");
|
2016-07-05 14:27:27 +02:00
|
|
|
let new_ptr = self.memory.reallocate(ptr, size as usize, align as usize)?;
|
2016-10-20 04:42:19 -06:00
|
|
|
self.write_primval(dest, PrimVal::from_ptr(new_ptr))?;
|
2016-06-23 01:03:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
"memcmp" => {
|
2016-09-23 10:27:14 +02:00
|
|
|
let left = args[0].read_ptr(&self.memory)?;
|
|
|
|
let right = args[1].read_ptr(&self.memory)?;
|
|
|
|
let n = self.value_to_primval(args[2], usize)?.expect_uint("__rust_reallocate first arg not usize") as usize;
|
2016-06-23 01:03:58 -06:00
|
|
|
|
|
|
|
let result = {
|
|
|
|
let left_bytes = self.memory.read_bytes(left, n)?;
|
|
|
|
let right_bytes = self.memory.read_bytes(right, n)?;
|
|
|
|
|
|
|
|
use std::cmp::Ordering::*;
|
|
|
|
match left_bytes.cmp(right_bytes) {
|
|
|
|
Less => -1,
|
|
|
|
Equal => 0,
|
|
|
|
Greater => 1,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-10-20 04:42:19 -06:00
|
|
|
self.write_primval(dest, PrimVal::from_int_with_size(result, dest_size))?;
|
2016-06-23 01:03:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
_ => {
|
|
|
|
return Err(EvalError::Unimplemented(format!("can't call C ABI function: {}", link_name)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Since we pushed no stack frame, the main loop will act
|
|
|
|
// as if the call just completed and it's returning to the
|
|
|
|
// current frame.
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2016-09-10 15:14:49 +02:00
|
|
|
pub(super) fn fulfill_obligation(&self, trait_ref: ty::PolyTraitRef<'tcx>) -> traits::Vtable<'tcx, ()> {
|
2016-06-23 01:03:58 -06:00
|
|
|
// Do the initial selection for the obligation. This yields the shallow result we are
|
|
|
|
// looking for -- that is, what specific impl.
|
2016-09-06 16:04:51 +02:00
|
|
|
self.tcx.infer_ctxt(None, None, Reveal::All).enter(|infcx| {
|
2016-06-23 01:03:58 -06:00
|
|
|
let mut selcx = traits::SelectionContext::new(&infcx);
|
|
|
|
|
|
|
|
let obligation = traits::Obligation::new(
|
|
|
|
traits::ObligationCause::misc(DUMMY_SP, ast::DUMMY_NODE_ID),
|
|
|
|
trait_ref.to_poly_trait_predicate(),
|
|
|
|
);
|
|
|
|
let selection = selcx.select(&obligation).unwrap().unwrap();
|
|
|
|
|
|
|
|
// Currently, we use a fulfillment context to completely resolve all nested obligations.
|
|
|
|
// This is because they can inform the inference of the impl's type parameters.
|
|
|
|
let mut fulfill_cx = traits::FulfillmentContext::new();
|
|
|
|
let vtable = selection.map(|predicate| {
|
|
|
|
fulfill_cx.register_predicate_obligation(&infcx, predicate);
|
|
|
|
});
|
|
|
|
infcx.drain_fulfillment_cx_or_panic(DUMMY_SP, &mut fulfill_cx, &vtable)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-09-19 19:40:56 -06:00
|
|
|
fn unpack_fn_args(&self, args: &mut Vec<(Value, Ty<'tcx>)>) {
|
2016-09-21 15:57:13 +02:00
|
|
|
if let Some((last, last_ty)) = args.pop() {
|
|
|
|
let last_layout = self.type_layout(last_ty);
|
|
|
|
match (&last_ty.sty, last_layout) {
|
|
|
|
(&ty::TyTuple(fields),
|
|
|
|
&Layout::Univariant { ref variant, .. }) => {
|
2016-10-03 20:45:50 -06:00
|
|
|
let offsets = variant.offsets.iter().map(|s| s.bytes());
|
2016-09-19 19:40:56 -06:00
|
|
|
let last_ptr = match last {
|
|
|
|
Value::ByRef(ptr) => ptr,
|
|
|
|
_ => bug!("rust-call ABI tuple argument wasn't Value::ByRef"),
|
|
|
|
};
|
2016-09-21 15:57:13 +02:00
|
|
|
for (offset, ty) in offsets.zip(fields) {
|
2016-09-19 19:40:56 -06:00
|
|
|
let arg = Value::ByRef(last_ptr.offset(offset as isize));
|
|
|
|
args.push((arg, ty));
|
2016-09-21 15:57:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ty => bug!("expected tuple as last argument in function with 'rust-call' ABI, got {:?}", ty),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-23 01:03:58 -06:00
|
|
|
/// Trait method, which has to be resolved to an impl method.
|
|
|
|
fn trait_method(
|
2016-09-20 18:31:55 +02:00
|
|
|
&mut self,
|
2016-08-27 01:44:46 -06:00
|
|
|
trait_id: DefId,
|
2016-06-23 01:03:58 -06:00
|
|
|
def_id: DefId,
|
2016-09-09 12:51:14 +02:00
|
|
|
substs: &'tcx Substs<'tcx>,
|
2016-09-19 19:40:56 -06:00
|
|
|
args: &mut Vec<(Value, Ty<'tcx>)>,
|
2016-09-09 12:51:14 +02:00
|
|
|
) -> EvalResult<'tcx, (DefId, &'tcx Substs<'tcx>)> {
|
2016-08-27 01:44:46 -06:00
|
|
|
let trait_ref = ty::TraitRef::from_method(self.tcx, trait_id, substs);
|
|
|
|
let trait_ref = self.tcx.normalize_associated_type(&ty::Binder(trait_ref));
|
|
|
|
|
2016-06-23 01:03:58 -06:00
|
|
|
match self.fulfill_obligation(trait_ref) {
|
|
|
|
traits::VtableImpl(vtable_impl) => {
|
|
|
|
let impl_did = vtable_impl.impl_def_id;
|
|
|
|
let mname = self.tcx.item_name(def_id);
|
|
|
|
// Create a concatenated set of substitutions which includes those from the impl
|
|
|
|
// and those from the method:
|
2016-09-20 12:52:01 +02:00
|
|
|
let (did, substs) = find_method(self.tcx, substs, impl_did, vtable_impl.substs, mname);
|
2016-06-23 01:03:58 -06:00
|
|
|
|
2016-09-20 12:52:01 +02:00
|
|
|
Ok((did, substs))
|
2016-06-23 01:03:58 -06:00
|
|
|
}
|
|
|
|
|
2016-09-20 18:31:55 +02:00
|
|
|
traits::VtableClosure(vtable_closure) => {
|
|
|
|
let trait_closure_kind = self.tcx
|
|
|
|
.lang_items
|
|
|
|
.fn_trait_kind(trait_id)
|
|
|
|
.expect("The substitutions should have no type parameters remaining after passing through fulfill_obligation");
|
|
|
|
let closure_kind = self.tcx.closure_kind(vtable_closure.closure_def_id);
|
|
|
|
trace!("closures {:?}, {:?}", closure_kind, trait_closure_kind);
|
2016-09-21 15:57:13 +02:00
|
|
|
self.unpack_fn_args(args);
|
2016-09-20 18:31:55 +02:00
|
|
|
match (closure_kind, trait_closure_kind) {
|
|
|
|
(ty::ClosureKind::Fn, ty::ClosureKind::Fn) |
|
|
|
|
(ty::ClosureKind::FnMut, ty::ClosureKind::FnMut) |
|
|
|
|
(ty::ClosureKind::FnOnce, ty::ClosureKind::FnOnce) |
|
|
|
|
(ty::ClosureKind::Fn, ty::ClosureKind::FnMut) => {} // No adapter needed.
|
2016-09-19 19:40:56 -06:00
|
|
|
|
2016-09-20 18:31:55 +02:00
|
|
|
(ty::ClosureKind::Fn, ty::ClosureKind::FnOnce) |
|
|
|
|
(ty::ClosureKind::FnMut, ty::ClosureKind::FnOnce) => {
|
|
|
|
// The closure fn is a `fn(&self, ...)` or `fn(&mut self, ...)`.
|
|
|
|
// We want a `fn(self, ...)`.
|
|
|
|
// We can produce this by doing something like:
|
|
|
|
//
|
|
|
|
// fn call_once(self, ...) { call_mut(&self, ...) }
|
|
|
|
// fn call_once(mut self, ...) { call_mut(&mut self, ...) }
|
|
|
|
//
|
|
|
|
// These are both the same at trans time.
|
|
|
|
|
2016-09-19 19:40:56 -06:00
|
|
|
// Interpreter magic: insert an intermediate pointer, so we can skip the
|
|
|
|
// intermediate function call.
|
|
|
|
// FIXME: this is a memory leak, should probably add the pointer to the
|
|
|
|
// current stack.
|
2016-09-23 10:27:14 +02:00
|
|
|
let first = self.value_to_ptr_dont_use(args[0].0, args[0].1)?;
|
2016-10-20 04:42:19 -06:00
|
|
|
args[0].0 = Value::ByVal(PrimVal::from_ptr(first));
|
2016-09-19 19:40:56 -06:00
|
|
|
args[0].1 = self.tcx.mk_mut_ptr(args[0].1);
|
2016-09-20 18:31:55 +02:00
|
|
|
}
|
2016-09-19 19:40:56 -06:00
|
|
|
|
2016-09-20 18:31:55 +02:00
|
|
|
_ => bug!("cannot convert {:?} to {:?}", closure_kind, trait_closure_kind),
|
|
|
|
}
|
|
|
|
Ok((vtable_closure.closure_def_id, vtable_closure.substs.func_substs))
|
|
|
|
}
|
2016-06-23 01:03:58 -06:00
|
|
|
|
2016-09-16 10:23:04 +02:00
|
|
|
traits::VtableFnPointer(vtable_fn_ptr) => {
|
|
|
|
if let ty::TyFnDef(did, ref substs, _) = vtable_fn_ptr.fn_ty.sty {
|
2016-09-15 16:14:53 +02:00
|
|
|
args.remove(0);
|
2016-09-21 15:57:13 +02:00
|
|
|
self.unpack_fn_args(args);
|
2016-09-15 16:14:53 +02:00
|
|
|
Ok((did, substs))
|
|
|
|
} else {
|
2016-09-16 10:28:43 +02:00
|
|
|
bug!("VtableFnPointer did not contain a concrete function: {:?}", vtable_fn_ptr)
|
2016-09-15 16:14:53 +02:00
|
|
|
}
|
2016-06-23 01:03:58 -06:00
|
|
|
}
|
|
|
|
|
2016-09-09 12:51:14 +02:00
|
|
|
traits::VtableObject(ref data) => {
|
|
|
|
let idx = self.tcx.get_vtable_index_of_object_method(data, def_id);
|
2016-09-19 19:40:56 -06:00
|
|
|
if let Some(&mut(ref mut first_arg, ref mut first_ty)) = args.get_mut(0) {
|
2016-10-16 02:12:26 -06:00
|
|
|
let (self_ptr, vtable) = first_arg.expect_ptr_vtable_pair(&self.memory)?;
|
2016-10-20 04:42:19 -06:00
|
|
|
*first_arg = Value::ByVal(PrimVal::from_ptr(self_ptr));
|
2016-09-09 12:51:14 +02:00
|
|
|
let idx = idx + 3;
|
|
|
|
let offset = idx * self.memory.pointer_size();
|
|
|
|
let fn_ptr = self.memory.read_ptr(vtable.offset(offset as isize))?;
|
|
|
|
let (def_id, substs, ty) = self.memory.get_fn(fn_ptr.alloc_id)?;
|
|
|
|
// FIXME: skip_binder is wrong for HKL
|
|
|
|
*first_ty = ty.sig.skip_binder().inputs[0];
|
|
|
|
Ok((def_id, substs))
|
|
|
|
} else {
|
|
|
|
Err(EvalError::VtableForArgumentlessMethod)
|
|
|
|
}
|
|
|
|
},
|
2016-09-06 16:16:49 +02:00
|
|
|
vtable => bug!("resolved vtable bad vtable {:?} in trans", vtable),
|
2016-06-23 01:03:58 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(super) fn type_needs_drop(&self, ty: Ty<'tcx>) -> bool {
|
|
|
|
self.tcx.type_needs_drop_given_env(ty, &self.tcx.empty_parameter_environment())
|
|
|
|
}
|
|
|
|
|
2016-11-03 13:27:35 +01:00
|
|
|
fn drop(&mut self, val: Value, ty: Ty<'tcx>) -> EvalResult<'tcx, ()> {
|
2016-06-23 01:03:58 -06:00
|
|
|
if !self.type_needs_drop(ty) {
|
|
|
|
debug!("no need to drop {:?}", ty);
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
trace!("-need to drop {:?}", ty);
|
|
|
|
|
|
|
|
// TODO(solson): Call user-defined Drop::drop impls.
|
|
|
|
|
|
|
|
match ty.sty {
|
2016-11-03 13:30:54 +01:00
|
|
|
ty::TyBox(contents_ty) => {
|
2016-11-03 13:27:35 +01:00
|
|
|
let contents_ptr = val.read_ptr(&self.memory)?;
|
2016-11-03 13:30:54 +01:00
|
|
|
self.drop(Value::ByRef(contents_ptr), contents_ty)?;
|
2016-06-30 21:30:03 -06:00
|
|
|
trace!("-deallocating box");
|
|
|
|
self.memory.deallocate(contents_ptr)?;
|
2016-06-23 01:03:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(solson): Implement drop for other relevant types (e.g. aggregates).
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
2016-09-10 15:14:49 +02:00
|
|
|
pub(super) struct ImplMethod<'tcx> {
|
|
|
|
pub(super) method: Rc<ty::Method<'tcx>>,
|
|
|
|
pub(super) substs: &'tcx Substs<'tcx>,
|
|
|
|
pub(super) is_provided: bool,
|
2016-06-23 01:03:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Locates the applicable definition of a method, given its name.
|
2016-09-10 15:14:49 +02:00
|
|
|
pub(super) fn get_impl_method<'a, 'tcx>(
|
2016-06-23 01:03:58 -06:00
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
substs: &'tcx Substs<'tcx>,
|
2016-08-27 01:44:46 -06:00
|
|
|
impl_def_id: DefId,
|
|
|
|
impl_substs: &'tcx Substs<'tcx>,
|
2016-06-23 01:03:58 -06:00
|
|
|
name: ast::Name,
|
|
|
|
) -> ImplMethod<'tcx> {
|
2016-09-07 10:12:15 +02:00
|
|
|
assert!(!substs.needs_infer());
|
2016-06-23 01:03:58 -06:00
|
|
|
|
|
|
|
let trait_def_id = tcx.trait_id_of_impl(impl_def_id).unwrap();
|
|
|
|
let trait_def = tcx.lookup_trait_def(trait_def_id);
|
|
|
|
|
|
|
|
match trait_def.ancestors(impl_def_id).fn_defs(tcx, name).next() {
|
|
|
|
Some(node_item) => {
|
2016-09-06 16:04:51 +02:00
|
|
|
let substs = tcx.infer_ctxt(None, None, Reveal::All).enter(|infcx| {
|
2016-08-27 01:44:46 -06:00
|
|
|
let substs = substs.rebase_onto(tcx, trait_def_id, impl_substs);
|
2016-06-23 01:03:58 -06:00
|
|
|
let substs = traits::translate_substs(&infcx, impl_def_id,
|
|
|
|
substs, node_item.node);
|
|
|
|
tcx.lift(&substs).unwrap_or_else(|| {
|
|
|
|
bug!("trans::meth::get_impl_method: translate_substs \
|
|
|
|
returned {:?} which contains inference types/regions",
|
|
|
|
substs);
|
|
|
|
})
|
|
|
|
});
|
|
|
|
ImplMethod {
|
|
|
|
method: node_item.item,
|
|
|
|
substs: substs,
|
|
|
|
is_provided: node_item.node.is_from_trait(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
bug!("method {:?} not found in {:?}", name, impl_def_id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-09-20 12:52:01 +02:00
|
|
|
|
|
|
|
/// Locates the applicable definition of a method, given its name.
|
|
|
|
pub fn find_method<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
substs: &'tcx Substs<'tcx>,
|
|
|
|
impl_def_id: DefId,
|
|
|
|
impl_substs: &'tcx Substs<'tcx>,
|
|
|
|
name: ast::Name)
|
|
|
|
-> (DefId, &'tcx Substs<'tcx>)
|
|
|
|
{
|
|
|
|
assert!(!substs.needs_infer());
|
|
|
|
|
|
|
|
let trait_def_id = tcx.trait_id_of_impl(impl_def_id).unwrap();
|
|
|
|
let trait_def = tcx.lookup_trait_def(trait_def_id);
|
|
|
|
|
|
|
|
match trait_def.ancestors(impl_def_id).fn_defs(tcx, name).next() {
|
|
|
|
Some(node_item) => {
|
|
|
|
let substs = tcx.infer_ctxt(None, None, Reveal::All).enter(|infcx| {
|
|
|
|
let substs = substs.rebase_onto(tcx, trait_def_id, impl_substs);
|
|
|
|
let substs = traits::translate_substs(&infcx, impl_def_id, substs, node_item.node);
|
|
|
|
tcx.lift(&substs).unwrap_or_else(|| {
|
|
|
|
bug!("find_method: translate_substs \
|
|
|
|
returned {:?} which contains inference types/regions",
|
|
|
|
substs);
|
|
|
|
})
|
|
|
|
});
|
|
|
|
(node_item.item.def_id, substs)
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
bug!("method {:?} not found in {:?}", name, impl_def_id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|