2011-09-22 13:44:18 -05:00
|
|
|
import syntax::ast;
|
2012-02-01 04:04:56 -06:00
|
|
|
import lib::llvm::ValueRef;
|
2012-01-27 06:17:06 -06:00
|
|
|
import common::*;
|
|
|
|
import build::*;
|
2012-02-17 06:17:40 -06:00
|
|
|
import base::*;
|
2012-03-12 04:05:15 -05:00
|
|
|
import shape::llsize_of;
|
2011-09-22 13:44:18 -05:00
|
|
|
|
2012-05-14 18:31:35 -05:00
|
|
|
export trans_uniq, make_free_glue, autoderef, duplicate;
|
2011-09-22 13:44:18 -05:00
|
|
|
|
2012-02-17 06:17:40 -06:00
|
|
|
fn trans_uniq(bcx: block, contents: @ast::expr,
|
|
|
|
node_id: ast::node_id, dest: dest) -> block {
|
2012-03-22 15:44:20 -05:00
|
|
|
let _icx = bcx.insn_ctxt("uniq::trans_uniq");
|
2012-02-02 05:37:17 -06:00
|
|
|
let uniq_ty = node_id_type(bcx, node_id);
|
2012-05-09 16:11:46 -05:00
|
|
|
let contents_ty = content_ty(uniq_ty);
|
|
|
|
let {box, body} = malloc_unique(bcx, contents_ty);
|
|
|
|
add_clean_free(bcx, box, true);
|
|
|
|
let bcx = trans_expr_save_in(bcx, contents, body);
|
|
|
|
revoke_clean(bcx, box);
|
|
|
|
ret store_in_dest(bcx, box, dest);
|
2011-09-22 13:44:18 -05:00
|
|
|
}
|
|
|
|
|
2012-02-17 06:17:40 -06:00
|
|
|
fn make_free_glue(bcx: block, vptr: ValueRef, t: ty::t)
|
|
|
|
-> block {
|
2012-03-22 15:44:20 -05:00
|
|
|
let _icx = bcx.insn_ctxt("uniq::make_free_glue");
|
2012-02-17 06:17:40 -06:00
|
|
|
with_cond(bcx, IsNotNull(bcx, vptr)) {|bcx|
|
2012-05-09 16:11:46 -05:00
|
|
|
let content_ty = content_ty(t);
|
|
|
|
let body_ptr = opaque_box_body(bcx, content_ty, vptr);
|
|
|
|
let bcx = drop_ty(bcx, body_ptr, content_ty);
|
|
|
|
trans_unique_free(bcx, vptr)
|
2012-02-17 06:17:40 -06:00
|
|
|
}
|
2011-09-22 13:44:18 -05:00
|
|
|
}
|
|
|
|
|
2012-02-03 08:15:28 -06:00
|
|
|
fn content_ty(t: ty::t) -> ty::t {
|
|
|
|
alt ty::get(t).struct {
|
2011-09-22 13:44:18 -05:00
|
|
|
ty::ty_uniq({ty: ct, _}) { ct }
|
2012-03-08 16:29:17 -06:00
|
|
|
_ { core::unreachable(); }
|
2011-09-22 13:44:18 -05:00
|
|
|
}
|
2011-09-22 15:22:53 -05:00
|
|
|
}
|
|
|
|
|
2012-05-09 16:11:46 -05:00
|
|
|
fn autoderef(bcx: block, v: ValueRef, t: ty::t) -> {v: ValueRef, t: ty::t} {
|
2012-02-03 08:15:28 -06:00
|
|
|
let content_ty = content_ty(t);
|
2012-05-09 16:11:46 -05:00
|
|
|
let v = opaque_box_body(bcx, content_ty, v);
|
2011-09-22 18:04:27 -05:00
|
|
|
ret {v: v, t: content_ty};
|
2011-09-22 20:05:36 -05:00
|
|
|
}
|
|
|
|
|
2012-02-17 06:17:40 -06:00
|
|
|
fn duplicate(bcx: block, v: ValueRef, t: ty::t) -> result {
|
2012-03-22 15:44:20 -05:00
|
|
|
let _icx = bcx.insn_ctxt("uniq::duplicate");
|
2012-02-03 08:15:28 -06:00
|
|
|
let content_ty = content_ty(t);
|
2012-05-09 16:11:46 -05:00
|
|
|
let {box: dst_box, body: dst_body} = malloc_unique(bcx, content_ty);
|
|
|
|
|
|
|
|
let src_box = v;
|
|
|
|
let src_body = opaque_box_body(bcx, content_ty, src_box);
|
|
|
|
let src_body = load_if_immediate(bcx, src_body, content_ty);
|
|
|
|
#debug("ST: %?", val_str(bcx.ccx().tn, src_body));
|
|
|
|
#debug("DT: %?", val_str(bcx.ccx().tn, dst_body));
|
|
|
|
let bcx = copy_val(bcx, INIT, dst_body, src_body, content_ty);
|
|
|
|
|
|
|
|
let src_tydesc_ptr = GEPi(bcx, src_box,
|
|
|
|
[0u, back::abi::box_field_tydesc]);
|
|
|
|
let dst_tydesc_ptr = GEPi(bcx, dst_box,
|
|
|
|
[0u, back::abi::box_field_tydesc]);
|
|
|
|
|
|
|
|
let td = Load(bcx, src_tydesc_ptr);
|
|
|
|
Store(bcx, td, dst_tydesc_ptr);
|
2011-09-22 20:05:36 -05:00
|
|
|
|
2012-05-09 16:11:46 -05:00
|
|
|
ret rslt(bcx, dst_box);
|
2011-09-22 13:44:18 -05:00
|
|
|
}
|