|
| 1 | +use rustc::middle::lang_items::PanicLocationLangItem; |
| 2 | +use rustc::mir::interpret::{Pointer, PointerArithmetic, Scalar}; |
| 3 | +use rustc::ty::subst::Subst; |
| 4 | +use rustc_target::abi::{LayoutOf, Size}; |
| 5 | +use syntax_pos::Span; |
| 6 | + |
| 7 | +use crate::interpret::{ |
| 8 | + MemoryKind, |
| 9 | + intrinsics::{InterpCx, InterpResult, Machine, PlaceTy}, |
| 10 | +}; |
| 11 | + |
| 12 | +impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { |
| 13 | + pub fn write_caller_location( |
| 14 | + &mut self, |
| 15 | + span: Span, |
| 16 | + dest: PlaceTy<'tcx, M::PointerTag>, |
| 17 | + ) -> InterpResult<'tcx> { |
| 18 | + let caller = self.tcx.sess.source_map().lookup_char_pos(span.lo()); |
| 19 | + let filename = caller.file.name.to_string(); |
| 20 | + let line = Scalar::from_u32(caller.line as u32); |
| 21 | + let col = Scalar::from_u32(caller.col_display as u32 + 1); |
| 22 | + |
| 23 | + let ptr_size = self.pointer_size(); |
| 24 | + let u32_size = Size::from_bits(32); |
| 25 | + |
| 26 | + let loc_ty = self.tcx.type_of(self.tcx.require_lang_item(PanicLocationLangItem, None)) |
| 27 | + .subst(*self.tcx, self.tcx.mk_substs([self.tcx.lifetimes.re_static.into()].iter())); |
| 28 | + let loc_layout = self.layout_of(loc_ty)?; |
| 29 | + |
| 30 | + let file_alloc = self.tcx.allocate_bytes(filename.as_bytes()); |
| 31 | + let file_ptr = Pointer::new(file_alloc, Size::ZERO); |
| 32 | + let file = Scalar::Ptr(self.tag_static_base_pointer(file_ptr)); |
| 33 | + let file_len = Scalar::from_uint(filename.len() as u128, ptr_size); |
| 34 | + |
| 35 | + let location = self.allocate(loc_layout, MemoryKind::Stack); |
| 36 | + |
| 37 | + let file_out = self.mplace_field(location, 0)?; |
| 38 | + let file_ptr_out = self.force_ptr(self.mplace_field(file_out, 0)?.ptr)?; |
| 39 | + let file_len_out = self.force_ptr(self.mplace_field(file_out, 1)?.ptr)?; |
| 40 | + let line_out = self.force_ptr(self.mplace_field(location, 1)?.ptr)?; |
| 41 | + let col_out = self.force_ptr(self.mplace_field(location, 2)?.ptr)?; |
| 42 | + |
| 43 | + let layout = &self.tcx.data_layout; |
| 44 | + let alloc = self.memory.get_mut(file_ptr_out.alloc_id)?; |
| 45 | + |
| 46 | + alloc.write_scalar(layout, file_ptr_out, file.into(), ptr_size)?; |
| 47 | + alloc.write_scalar(layout, file_len_out, file_len.into(), ptr_size)?; |
| 48 | + alloc.write_scalar(layout, line_out, line.into(), u32_size)?; |
| 49 | + alloc.write_scalar(layout, col_out, col.into(), u32_size)?; |
| 50 | + |
| 51 | + self.write_scalar(location.ptr, dest)?; |
| 52 | + Ok(()) |
| 53 | + } |
| 54 | +} |
0 commit comments