Skip to content

Commit d0f70eb

Browse files
committed
Refactor various queries out of ty and into trans/common
1 parent 16e4fef commit d0f70eb

File tree

12 files changed

+75
-64
lines changed

12 files changed

+75
-64
lines changed

src/librustc/middle/ty.rs

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,6 @@ pub struct ctxt<'tcx> {
658658
pub tcache: RefCell<DefIdMap<Polytype<'tcx>>>,
659659
pub rcache: RefCell<FnvHashMap<creader_cache_key, Ty<'tcx>>>,
660660
pub short_names_cache: RefCell<FnvHashMap<Ty<'tcx>, String>>,
661-
pub needs_unwind_cleanup_cache: RefCell<FnvHashMap<Ty<'tcx>, bool>>,
662661
pub tc_cache: RefCell<FnvHashMap<Ty<'tcx>, TypeContents>>,
663662
pub ast_ty_to_ty_cache: RefCell<NodeMap<ast_ty_to_ty_cache_entry<'tcx>>>,
664663
pub enum_var_cache: RefCell<DefIdMap<Rc<Vec<Rc<VariantInfo<'tcx>>>>>>,
@@ -2006,7 +2005,6 @@ pub fn mk_ctxt<'tcx>(s: Session,
20062005
tcache: RefCell::new(DefIdMap::new()),
20072006
rcache: RefCell::new(FnvHashMap::new()),
20082007
short_names_cache: RefCell::new(FnvHashMap::new()),
2009-
needs_unwind_cleanup_cache: RefCell::new(FnvHashMap::new()),
20102008
tc_cache: RefCell::new(FnvHashMap::new()),
20112009
ast_ty_to_ty_cache: RefCell::new(NodeMap::new()),
20122010
enum_var_cache: RefCell::new(DefIdMap::new()),
@@ -2690,48 +2688,6 @@ pub fn type_is_floating_point(ty: Ty) -> bool {
26902688
}
26912689
}
26922690

2693-
pub fn type_needs_drop<'tcx>(cx: &ctxt<'tcx>, ty: Ty<'tcx>) -> bool {
2694-
type_contents(cx, ty).needs_drop(cx)
2695-
}
2696-
2697-
// Some things don't need cleanups during unwinding because the
2698-
// task can free them all at once later. Currently only things
2699-
// that only contain scalars and shared boxes can avoid unwind
2700-
// cleanups.
2701-
pub fn type_needs_unwind_cleanup<'tcx>(cx: &ctxt<'tcx>, ty: Ty<'tcx>) -> bool {
2702-
return memoized(&cx.needs_unwind_cleanup_cache, ty, |ty| {
2703-
type_needs_unwind_cleanup_(cx, ty, &mut FnvHashSet::new())
2704-
});
2705-
2706-
fn type_needs_unwind_cleanup_<'tcx>(cx: &ctxt<'tcx>, ty: Ty<'tcx>,
2707-
tycache: &mut FnvHashSet<Ty<'tcx>>) -> bool {
2708-
// Prevent infinite recursion
2709-
if !tycache.insert(ty) {
2710-
return false;
2711-
}
2712-
2713-
let mut needs_unwind_cleanup = false;
2714-
maybe_walk_ty(ty, |ty| {
2715-
needs_unwind_cleanup |= match ty.sty {
2716-
ty_bool | ty_int(_) | ty_uint(_) |
2717-
ty_float(_) | ty_tup(_) | ty_ptr(_) => false,
2718-
2719-
ty_enum(did, ref substs) =>
2720-
enum_variants(cx, did).iter().any(|v|
2721-
v.args.iter().any(|aty| {
2722-
let t = aty.subst(cx, substs);
2723-
type_needs_unwind_cleanup_(cx, t, tycache)
2724-
})
2725-
),
2726-
2727-
_ => true
2728-
};
2729-
!needs_unwind_cleanup
2730-
});
2731-
needs_unwind_cleanup
2732-
}
2733-
}
2734-
27352691
/// Type contents is how the type checker reasons about kinds.
27362692
/// They track what kinds of things are found within a type. You can
27372693
/// think of them as kind of an "anti-kind". They track the kinds of values

src/librustc_trans/trans/callee.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -721,7 +721,7 @@ pub fn trans_call_inner<'a, 'blk, 'tcx, F>(bcx: Block<'blk, 'tcx>,
721721
};
722722
if !is_rust_fn ||
723723
type_of::return_uses_outptr(ccx, ret_ty) ||
724-
ty::type_needs_drop(bcx.tcx(), ret_ty) {
724+
type_needs_drop(bcx.tcx(), ret_ty) {
725725
// Push the out-pointer if we use an out-pointer for this
726726
// return type, otherwise push "undef".
727727
if type_is_zero_size(ccx, ret_ty) {

src/librustc_trans/trans/cleanup.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -277,10 +277,10 @@ impl<'blk, 'tcx> CleanupMethods<'blk, 'tcx> for FunctionContext<'blk, 'tcx> {
277277
cleanup_scope: ScopeId,
278278
val: ValueRef,
279279
ty: Ty<'tcx>) {
280-
if !ty::type_needs_drop(self.ccx.tcx(), ty) { return; }
280+
if !common::type_needs_drop(self.ccx.tcx(), ty) { return; }
281281
let drop = box DropValue {
282282
is_immediate: false,
283-
must_unwind: ty::type_needs_unwind_cleanup(self.ccx.tcx(), ty),
283+
must_unwind: common::type_needs_unwind_cleanup(self.ccx, ty),
284284
val: val,
285285
ty: ty,
286286
zero: false
@@ -299,10 +299,10 @@ impl<'blk, 'tcx> CleanupMethods<'blk, 'tcx> for FunctionContext<'blk, 'tcx> {
299299
cleanup_scope: ScopeId,
300300
val: ValueRef,
301301
ty: Ty<'tcx>) {
302-
if !ty::type_needs_drop(self.ccx.tcx(), ty) { return; }
302+
if !common::type_needs_drop(self.ccx.tcx(), ty) { return; }
303303
let drop = box DropValue {
304304
is_immediate: false,
305-
must_unwind: ty::type_needs_unwind_cleanup(self.ccx.tcx(), ty),
305+
must_unwind: common::type_needs_unwind_cleanup(self.ccx, ty),
306306
val: val,
307307
ty: ty,
308308
zero: true
@@ -323,10 +323,10 @@ impl<'blk, 'tcx> CleanupMethods<'blk, 'tcx> for FunctionContext<'blk, 'tcx> {
323323
val: ValueRef,
324324
ty: Ty<'tcx>) {
325325

326-
if !ty::type_needs_drop(self.ccx.tcx(), ty) { return; }
326+
if !common::type_needs_drop(self.ccx.tcx(), ty) { return; }
327327
let drop = box DropValue {
328328
is_immediate: true,
329-
must_unwind: ty::type_needs_unwind_cleanup(self.ccx.tcx(), ty),
329+
must_unwind: common::type_needs_unwind_cleanup(self.ccx, ty),
330330
val: val,
331331
ty: ty,
332332
zero: false

src/librustc_trans/trans/common.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,58 @@ use syntax::ast_map::{PathElem, PathName};
5353
use syntax::codemap::Span;
5454
use syntax::parse::token::InternedString;
5555
use syntax::parse::token;
56+
use util::common::memoized;
57+
use util::nodemap::FnvHashSet;
5658

5759
pub use trans::context::CrateContext;
5860

61+
// Some things don't need cleanups during unwinding because the
62+
// task can free them all at once later. Currently only things
63+
// that only contain scalars and shared boxes can avoid unwind
64+
// cleanups.
65+
pub fn type_needs_unwind_cleanup<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, ty: Ty<'tcx>) -> bool {
66+
return memoized(ccx.needs_unwind_cleanup_cache(), ty, |ty| {
67+
type_needs_unwind_cleanup_(ccx.tcx(), ty, &mut FnvHashSet::new())
68+
});
69+
70+
fn type_needs_unwind_cleanup_<'tcx>(tcx: &ty::ctxt<'tcx>,
71+
ty: Ty<'tcx>,
72+
tycache: &mut FnvHashSet<Ty<'tcx>>)
73+
-> bool
74+
{
75+
// Prevent infinite recursion
76+
if !tycache.insert(ty) {
77+
return false;
78+
}
79+
80+
let mut needs_unwind_cleanup = false;
81+
ty::maybe_walk_ty(ty, |ty| {
82+
needs_unwind_cleanup |= match ty.sty {
83+
ty::ty_bool | ty::ty_int(_) | ty::ty_uint(_) |
84+
ty::ty_float(_) | ty::ty_tup(_) | ty::ty_ptr(_) => false,
85+
86+
ty::ty_enum(did, ref substs) =>
87+
ty::enum_variants(tcx, did).iter().any(|v|
88+
v.args.iter().any(|aty| {
89+
let t = aty.subst(tcx, substs);
90+
type_needs_unwind_cleanup_(tcx, t, tycache)
91+
})
92+
),
93+
94+
_ => true
95+
};
96+
!needs_unwind_cleanup
97+
});
98+
needs_unwind_cleanup
99+
}
100+
}
101+
102+
pub fn type_needs_drop<'tcx>(cx: &ty::ctxt<'tcx>,
103+
ty: Ty<'tcx>)
104+
-> bool {
105+
ty::type_contents(cx, ty).needs_drop(cx)
106+
}
107+
59108
fn type_is_newtype_immediate<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
60109
ty: Ty<'tcx>) -> bool {
61110
match ty.sty {

src/librustc_trans/trans/context.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ pub struct LocalCrateContext<'tcx> {
8484
tn: TypeNames,
8585
externs: RefCell<ExternMap>,
8686
item_vals: RefCell<NodeMap<ValueRef>>,
87+
needs_unwind_cleanup_cache: RefCell<FnvHashMap<Ty<'tcx>, bool>>,
8788
fn_pointer_shims: RefCell<FnvHashMap<Ty<'tcx>, ValueRef>>,
8889
drop_glues: RefCell<FnvHashMap<Ty<'tcx>, ValueRef>>,
8990
tydescs: RefCell<FnvHashMap<Ty<'tcx>, Rc<tydesc_info<'tcx>>>>,
@@ -389,6 +390,7 @@ impl<'tcx> LocalCrateContext<'tcx> {
389390
tn: TypeNames::new(),
390391
externs: RefCell::new(FnvHashMap::new()),
391392
item_vals: RefCell::new(NodeMap::new()),
393+
needs_unwind_cleanup_cache: RefCell::new(FnvHashMap::new()),
392394
fn_pointer_shims: RefCell::new(FnvHashMap::new()),
393395
drop_glues: RefCell::new(FnvHashMap::new()),
394396
tydescs: RefCell::new(FnvHashMap::new()),
@@ -569,6 +571,10 @@ impl<'b, 'tcx> CrateContext<'b, 'tcx> {
569571
&self.shared.link_meta
570572
}
571573

574+
pub fn needs_unwind_cleanup_cache(&self) -> &RefCell<FnvHashMap<Ty<'tcx>, bool>> {
575+
&self.local.needs_unwind_cleanup_cache
576+
}
577+
572578
pub fn fn_pointer_shims(&self) -> &RefCell<FnvHashMap<Ty<'tcx>, ValueRef>> {
573579
&self.local.fn_pointer_shims
574580
}

src/librustc_trans/trans/controlflow.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ pub fn trans_stmt_semi<'blk, 'tcx>(cx: Block<'blk, 'tcx>, e: &ast::Expr)
8787
-> Block<'blk, 'tcx> {
8888
let _icx = push_ctxt("trans_stmt_semi");
8989
let ty = expr_ty(cx, e);
90-
if ty::type_needs_drop(cx.tcx(), ty) {
90+
if type_needs_drop(cx.tcx(), ty) {
9191
expr::trans_to_lvalue(cx, e, "stmt").bcx
9292
} else {
9393
expr::trans_into(cx, e, expr::Ignore)

src/librustc_trans/trans/datum.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ impl KindOps for Lvalue {
218218
val: ValueRef,
219219
ty: Ty<'tcx>)
220220
-> Block<'blk, 'tcx> {
221-
if ty::type_needs_drop(bcx.tcx(), ty) {
221+
if type_needs_drop(bcx.tcx(), ty) {
222222
// cancel cleanup of affine values by zeroing out
223223
let () = zero_mem(bcx, val, ty);
224224
bcx
@@ -567,7 +567,7 @@ impl<'tcx, K: KindOps + fmt::Show> Datum<'tcx, K> {
567567
/// scalar-ish (like an int or a pointer) which (1) does not require drop glue and (2) is
568568
/// naturally passed around by value, and not by reference.
569569
pub fn to_llscalarish<'blk>(self, bcx: Block<'blk, 'tcx>) -> ValueRef {
570-
assert!(!ty::type_needs_drop(bcx.tcx(), self.ty));
570+
assert!(!type_needs_drop(bcx.tcx(), self.ty));
571571
assert!(self.appropriate_rvalue_mode(bcx.ccx()) == ByValue);
572572
if self.kind.is_by_ref() {
573573
load_ty(bcx, self.val, self.ty)

src/librustc_trans/trans/expr.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -972,7 +972,7 @@ fn trans_rvalue_stmt_unadjusted<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
972972
let src_datum = unpack_datum!(bcx, trans(bcx, &**src));
973973
let dst_datum = unpack_datum!(bcx, trans_to_lvalue(bcx, &**dst, "assign"));
974974

975-
if ty::type_needs_drop(bcx.tcx(), dst_datum.ty) {
975+
if type_needs_drop(bcx.tcx(), dst_datum.ty) {
976976
// If there are destructors involved, make sure we
977977
// are copying from an rvalue, since that cannot possible
978978
// alias an lvalue. We are concerned about code like:
@@ -1497,7 +1497,7 @@ pub fn trans_adt<'a, 'blk, 'tcx>(mut bcx: Block<'blk, 'tcx>,
14971497
assert_eq!(discr, 0);
14981498

14991499
match ty::expr_kind(bcx.tcx(), &*base.expr) {
1500-
ty::RvalueDpsExpr | ty::RvalueDatumExpr if !ty::type_needs_drop(bcx.tcx(), ty) => {
1500+
ty::RvalueDpsExpr | ty::RvalueDatumExpr if !type_needs_drop(bcx.tcx(), ty) => {
15011501
bcx = trans_into(bcx, &*base.expr, SaveIn(addr));
15021502
},
15031503
ty::RvalueStmtExpr => bcx.tcx().sess.bug("unexpected expr kind for struct base expr"),
@@ -2101,7 +2101,7 @@ fn trans_assign_op<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
21012101

21022102
// Evaluate LHS (destination), which should be an lvalue
21032103
let dst_datum = unpack_datum!(bcx, trans_to_lvalue(bcx, dst, "assign_op"));
2104-
assert!(!ty::type_needs_drop(bcx.tcx(), dst_datum.ty));
2104+
assert!(!type_needs_drop(bcx.tcx(), dst_datum.ty));
21052105
let dst_ty = dst_datum.ty;
21062106
let dst = load_ty(bcx, dst_datum.val, dst_datum.ty);
21072107

src/librustc_trans/trans/glue.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,11 @@ pub fn get_drop_glue_type<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
8484
if !ty::type_is_sized(tcx, t) {
8585
return t
8686
}
87-
if !ty::type_needs_drop(tcx, t) {
87+
if !type_needs_drop(tcx, t) {
8888
return ty::mk_i8();
8989
}
9090
match t.sty {
91-
ty::ty_uniq(typ) if !ty::type_needs_drop(tcx, typ)
91+
ty::ty_uniq(typ) if !type_needs_drop(tcx, typ)
9292
&& ty::type_is_sized(tcx, typ) => {
9393
let llty = sizing_type_of(ccx, typ);
9494
// `Box<ZeroSizeType>` does not allocate.
@@ -110,7 +110,7 @@ pub fn drop_ty<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
110110
// NB: v is an *alias* of type t here, not a direct value.
111111
debug!("drop_ty(t={})", t.repr(bcx.tcx()));
112112
let _icx = push_ctxt("drop_ty");
113-
if ty::type_needs_drop(bcx.tcx(), t) {
113+
if type_needs_drop(bcx.tcx(), t) {
114114
let ccx = bcx.ccx();
115115
let glue = get_drop_glue(ccx, t);
116116
let glue_type = get_drop_glue_type(ccx, t);
@@ -469,7 +469,7 @@ fn make_drop_glue<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, v0: ValueRef, t: Ty<'tcx>)
469469
ty::ty_vec(ty, None) => tvec::make_drop_glue_unboxed(bcx, v0, ty, false),
470470
_ => {
471471
assert!(ty::type_is_sized(bcx.tcx(), t));
472-
if ty::type_needs_drop(bcx.tcx(), t) &&
472+
if type_needs_drop(bcx.tcx(), t) &&
473473
ty::type_is_structural(t) {
474474
iter_structural_ty(bcx, v0, t, |bb, vv, tt| drop_ty(bb, vv, tt, None))
475475
} else {

src/librustc_trans/trans/intrinsic.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ pub fn trans_intrinsic_call<'a, 'blk, 'tcx>(mut bcx: Block<'blk, 'tcx>,
365365
}
366366
(_, "needs_drop") => {
367367
let tp_ty = *substs.types.get(FnSpace, 0);
368-
C_bool(ccx, ty::type_needs_drop(ccx.tcx(), tp_ty))
368+
C_bool(ccx, type_needs_drop(ccx.tcx(), tp_ty))
369369
}
370370
(_, "owns_managed") => {
371371
let tp_ty = *substs.types.get(FnSpace, 0);

src/librustc_trans/trans/meth.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ fn trans_trait_callee<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
436436
let self_datum = unpack_datum!(
437437
bcx, expr::trans(bcx, self_expr));
438438

439-
let llval = if ty::type_needs_drop(bcx.tcx(), self_datum.ty) {
439+
let llval = if type_needs_drop(bcx.tcx(), self_datum.ty) {
440440
let self_datum = unpack_datum!(
441441
bcx, self_datum.to_rvalue_datum(bcx, "trait_callee"));
442442

src/librustc_trans/trans/tvec.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ pub fn make_drop_glue_unboxed<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
6262
let _icx = push_ctxt("tvec::make_drop_glue_unboxed");
6363

6464
let dataptr = get_dataptr(bcx, vptr);
65-
let bcx = if ty::type_needs_drop(tcx, unit_ty) {
65+
let bcx = if type_needs_drop(tcx, unit_ty) {
6666
let len = get_len(bcx, vptr);
6767
iter_vec_raw(bcx, dataptr, unit_ty, len, |bb, vv, tt| glue::drop_ty(bb, vv, tt, None))
6868
} else {

0 commit comments

Comments
 (0)