|
| 1 | +// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT |
| 2 | +// file at the top-level directory of this distribution and at |
| 3 | +// http://rust-lang.org/COPYRIGHT. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 7 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 8 | +// option. This file may not be copied, modified, or distributed |
| 9 | +// except according to those terms. |
| 10 | + |
| 11 | +use llvm::ValueRef; |
| 12 | +use rustc::middle::ty::Ty; |
| 13 | +use rustc::mir::repr as mir; |
| 14 | +use trans::adt; |
| 15 | +use trans::base; |
| 16 | +use trans::build; |
| 17 | +use trans::common::{self, Block}; |
| 18 | +use trans::debuginfo::DebugLoc; |
| 19 | +use trans::glue; |
| 20 | +use trans::machine; |
| 21 | +use trans::type_of; |
| 22 | +use trans::type_::Type; |
| 23 | + |
| 24 | +use super::MirContext; |
| 25 | + |
| 26 | +impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> { |
| 27 | + pub fn trans_drop(&mut self, |
| 28 | + bcx: Block<'bcx, 'tcx>, |
| 29 | + value: &mir::Lvalue<'tcx>, |
| 30 | + target: mir::BasicBlock, |
| 31 | + unwind: Option<mir::BasicBlock>) { |
| 32 | + let lvalue = self.trans_lvalue(bcx, value); |
| 33 | + let ty = lvalue.ty.to_ty(bcx.tcx()); |
| 34 | + // Double check for necessity to drop |
| 35 | + if !glue::type_needs_drop(bcx.tcx(), ty) { |
| 36 | + build::Br(bcx, self.llblock(target), DebugLoc::None); |
| 37 | + return; |
| 38 | + } |
| 39 | + let drop_fn = glue::get_drop_glue(bcx.ccx(), ty); |
| 40 | + let drop_ty = glue::get_drop_glue_type(bcx.ccx(), ty); |
| 41 | + let llvalue = if drop_ty != ty { |
| 42 | + build::PointerCast(bcx, lvalue.llval, |
| 43 | + type_of::type_of(bcx.ccx(), drop_ty).ptr_to()) |
| 44 | + } else { |
| 45 | + lvalue.llval |
| 46 | + }; |
| 47 | + if let Some(unwind) = unwind { |
| 48 | + // block cannot be cleanup in this case, so a regular block is fine |
| 49 | + let intermediate_bcx = bcx.fcx.new_block("", None); |
| 50 | + let uwbcx = self.bcx(unwind); |
| 51 | + let unwind = self.make_landing_pad(uwbcx); |
| 52 | + // FIXME: it could be possible to do zeroing before invoking here if the drop glue |
| 53 | + // didn’t code in the checks inside itself. |
| 54 | + build::Invoke(bcx, |
| 55 | + drop_fn, |
| 56 | + &[llvalue], |
| 57 | + intermediate_bcx.llbb, |
| 58 | + unwind.llbb, |
| 59 | + None, |
| 60 | + DebugLoc::None); |
| 61 | + // FIXME: perhaps we also should fill inside failed branch? We do not want to re-drop a |
| 62 | + // failed drop again by mistake. (conflicts with MSVC SEH if we don’t want to introduce |
| 63 | + // a heap of hacks) |
| 64 | + self.drop_fill(intermediate_bcx, lvalue.llval, ty); |
| 65 | + build::Br(intermediate_bcx, self.llblock(target), DebugLoc::None); |
| 66 | + } else { |
| 67 | + build::Call(bcx, drop_fn, &[llvalue], None, DebugLoc::None); |
| 68 | + self.drop_fill(bcx, lvalue.llval, ty); |
| 69 | + build::Br(bcx, self.llblock(target), DebugLoc::None); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + pub fn drop_fill(&mut self, bcx: Block<'bcx, 'tcx>, value: ValueRef, ty: Ty<'tcx>) { |
| 74 | + let llty = type_of::type_of(bcx.ccx(), ty); |
| 75 | + let llptr = build::PointerCast(bcx, value, Type::i8(bcx.ccx()).ptr_to()); |
| 76 | + let filling = common::C_u8(bcx.ccx(), adt::DTOR_DONE); |
| 77 | + let size = machine::llsize_of(bcx.ccx(), llty); |
| 78 | + let align = common::C_u32(bcx.ccx(), machine::llalign_of_min(bcx.ccx(), llty)); |
| 79 | + base::call_memset(&build::B(bcx), llptr, filling, size, align, false); |
| 80 | + } |
| 81 | +} |
0 commit comments