2012-12-03 18:48:01 -06:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2012-09-04 13:54:36 -05:00
|
|
|
use lib::llvm::ValueRef;
|
2012-12-13 15:05:22 -06:00
|
|
|
use middle::trans::base::*;
|
|
|
|
use middle::trans::build::*;
|
|
|
|
use middle::trans::common::*;
|
|
|
|
use middle::trans::datum::immediate_rvalue;
|
|
|
|
|
|
|
|
use syntax::ast;
|
2011-09-22 13:44:18 -05:00
|
|
|
|
2012-06-26 15:50:43 -05:00
|
|
|
export make_free_glue, autoderef, duplicate;
|
2011-09-22 13:44:18 -05:00
|
|
|
|
2012-08-28 17:54:45 -05:00
|
|
|
fn make_free_glue(bcx: block, vptrptr: ValueRef, box_ty: ty::t)
|
2012-02-17 06:17:40 -06:00
|
|
|
-> block {
|
2012-08-14 18:45:43 -05:00
|
|
|
let _icx = bcx.insn_ctxt("uniq::make_free_glue");
|
2012-08-28 17:54:45 -05:00
|
|
|
let box_datum = immediate_rvalue(Load(bcx, vptrptr), box_ty);
|
|
|
|
|
|
|
|
let not_null = IsNotNull(bcx, box_datum.val);
|
|
|
|
do with_cond(bcx, not_null) |bcx| {
|
|
|
|
let body_datum = box_datum.box_body(bcx);
|
|
|
|
let bcx = glue::drop_ty(bcx, body_datum.to_ref_llval(bcx),
|
|
|
|
body_datum.ty);
|
|
|
|
glue::trans_unique_free(bcx, box_datum.val)
|
2012-02-17 06:17:40 -06:00
|
|
|
}
|
2011-09-22 13:44:18 -05:00
|
|
|
}
|
|
|
|
|
2012-08-28 17:54:45 -05:00
|
|
|
fn duplicate(bcx: block, src_box: ValueRef, src_ty: ty::t) -> Result {
|
|
|
|
let _icx = bcx.insn_ctxt("uniq::duplicate");
|
2011-09-22 15:22:53 -05:00
|
|
|
|
2012-08-28 17:54:45 -05:00
|
|
|
// Load the body of the source (*src)
|
|
|
|
let src_datum = immediate_rvalue(src_box, src_ty);
|
|
|
|
let body_datum = src_datum.box_body(bcx);
|
2011-09-22 20:05:36 -05:00
|
|
|
|
2012-08-28 17:54:45 -05:00
|
|
|
// Malloc space in exchange heap and copy src into it
|
2012-07-17 12:48:19 -05:00
|
|
|
let {bcx: bcx, box: dst_box, body: dst_body} =
|
2012-08-28 17:54:45 -05:00
|
|
|
malloc_unique(bcx, body_datum.ty);
|
|
|
|
body_datum.copy_to(bcx, datum::INIT, dst_body);
|
2012-05-09 16:11:46 -05:00
|
|
|
|
2012-08-28 17:54:45 -05:00
|
|
|
// Copy the type descriptor
|
2012-05-09 16:11:46 -05:00
|
|
|
let src_tydesc_ptr = GEPi(bcx, src_box,
|
2012-08-27 14:16:37 -05:00
|
|
|
[0u, back::abi::box_field_tydesc]);
|
2012-05-09 16:11:46 -05:00
|
|
|
let dst_tydesc_ptr = GEPi(bcx, dst_box,
|
2012-08-27 14:16:37 -05:00
|
|
|
[0u, back::abi::box_field_tydesc]);
|
2012-05-09 16:11:46 -05:00
|
|
|
let td = Load(bcx, src_tydesc_ptr);
|
|
|
|
Store(bcx, td, dst_tydesc_ptr);
|
2011-09-22 20:05:36 -05:00
|
|
|
|
2012-08-01 19:30:05 -05:00
|
|
|
return rslt(bcx, dst_box);
|
2012-06-26 15:50:43 -05:00
|
|
|
}
|