Skip to content

Commit d243ea5

Browse files
committed
Move last remaining expression types out of trans_expr
Issue #667
1 parent f089f53 commit d243ea5

File tree

3 files changed

+58
-79
lines changed

3 files changed

+58
-79
lines changed

src/comp/middle/trans.rs

Lines changed: 32 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2243,10 +2243,7 @@ fn trans_expr_fn(bcx: @block_ctxt, f: ast::_fn, sp: span,
22432243
trans_closure(sub_cx, sp, f, llfn, none, [], id, {|_fcx|});
22442244
}
22452245
};
2246-
let addr = alt dest {
2247-
save_in(a) { a }
2248-
overwrite(a, ty) { bcx = drop_ty(bcx, a, ty); a }
2249-
};
2246+
let {bcx, val: addr} = get_dest_addr(bcx, dest);
22502247
fill_fn_pair(bcx, addr, llfn, env);
22512248
ret bcx;
22522249
}
@@ -2537,6 +2534,13 @@ fn store_in_dest(bcx: @block_ctxt, val: ValueRef, dest: dest) -> @block_ctxt {
25372534
ret bcx;
25382535
}
25392536

2537+
fn get_dest_addr(bcx: @block_ctxt, dest: dest) -> result {
2538+
alt dest {
2539+
save_in(a) { rslt(bcx, a) }
2540+
overwrite(a, t) { rslt(drop_ty(bcx, a, t), a) }
2541+
}
2542+
}
2543+
25402544
// Wrapper through which legacy non-DPS code can use DPS functions
25412545
fn dps_to_result(bcx: @block_ctxt,
25422546
work: block(@block_ctxt, dest) -> @block_ctxt,
@@ -3675,12 +3679,8 @@ fn trans_bind_1(cx: @block_ctxt, outgoing_fty: ty::t,
36753679
let lv = lval_maybe_callee_to_lval(f_res, pair_ty);
36763680
bcx = lv.bcx;
36773681
// FIXME[DPS] factor this out
3678-
let addr = alt dest {
3679-
save_in(a) { a }
3680-
overwrite(a, ty) { bcx = drop_ty(bcx, a, ty); a }
3681-
};
3682-
bcx = memmove_ty(bcx, addr, lv.val, pair_ty);
3683-
ret bcx;
3682+
let {bcx, val: addr} = get_dest_addr(bcx, dest);
3683+
ret memmove_ty(bcx, addr, lv.val, pair_ty);
36843684
}
36853685
let closure = alt f_res.env {
36863686
null_env. { none }
@@ -3717,10 +3717,7 @@ fn trans_bind_1(cx: @block_ctxt, outgoing_fty: ty::t,
37173717
closure.ptrty, ty_param_count, target_res);
37183718

37193719
// Fill the function pair
3720-
let addr = alt dest {
3721-
save_in(a) { a }
3722-
overwrite(a, ty) { bcx = drop_ty(bcx, a, ty); a }
3723-
};
3720+
let {bcx, val: addr} = get_dest_addr(bcx, dest);
37243721
fill_fn_pair(bcx, addr, llthunk.val, closure.ptr);
37253722
ret bcx;
37263723
}
@@ -4186,25 +4183,18 @@ fn trans_rec(bcx: @block_ctxt, fields: [ast::field],
41864183
ret bcx;
41874184
}
41884185

4186+
// FIXME[DPS] remove this entirely, rename trans_expr_dps to trans_expr
41894187
fn trans_expr(cx: @block_ctxt, e: @ast::expr) -> result {
4190-
// Fixme Fill in cx.sp
4191-
alt e.node {
4192-
ast::expr_anon_obj(anon_obj) {
4193-
ret trans_anon_obj(cx, e.span, anon_obj, e.id);
4194-
}
4195-
ast::expr_call(_, _) | ast::expr_field(_, _) | ast::expr_index(_, _) |
4196-
ast::expr_path(_) | ast::expr_unary(ast::deref., _) {
4188+
if expr_is_lval(bcx_tcx(cx), e) {
41974189
let t = ty::expr_ty(bcx_tcx(cx), e);
41984190
let sub = trans_lval(cx, e);
41994191
let v = sub.val;
42004192
if sub.is_mem { v = load_if_immediate(sub.bcx, v, t); }
42014193
ret rslt(sub.bcx, v);
4202-
}
4203-
// Fall through to DPS-style
4204-
_ {
4194+
} else {
4195+
// Fall through to DPS-style
42054196
ret dps_to_result(cx, {|bcx, dest| trans_expr_dps(bcx, e, dest)},
42064197
ty::expr_ty(bcx_tcx(cx), e));
4207-
}
42084198
}
42094199
}
42104200

@@ -4234,6 +4224,9 @@ fn trans_expr_by_ref(bcx: @block_ctxt, e: @ast::expr) -> result {
42344224
// - exprs returning non-immediates get save_in (or by_ref when lval)
42354225
fn trans_expr_dps(bcx: @block_ctxt, e: @ast::expr, dest: dest)
42364226
-> @block_ctxt {
4227+
let tcx = bcx_tcx(bcx);
4228+
if expr_is_lval(tcx, e) { ret lval_to_dps(bcx, e, dest); }
4229+
42374230
alt e.node {
42384231
ast::expr_if(cond, thn, els) | ast::expr_if_check(cond, thn, els) {
42394232
ret trans_if(bcx, cond, thn, els, dest);
@@ -4261,22 +4254,23 @@ fn trans_expr_dps(bcx: @block_ctxt, e: @ast::expr, dest: dest)
42614254
ast::expr_vec(args, _) { ret tvec::trans_vec(bcx, args, e.id, dest); }
42624255
ast::expr_binary(op, x, y) { ret trans_binary(bcx, op, x, y, dest); }
42634256
ast::expr_unary(op, x) {
4264-
if op == ast::deref {
4265-
ret trans_expr_backwards_compat(bcx, e, dest);
4266-
}
4257+
assert op != ast::deref; // lvals are handled above
42674258
ret trans_unary(bcx, op, x, e.id, dest);
42684259
}
42694260
ast::expr_fn(f) { ret trans_expr_fn(bcx, f, e.span, e.id, dest); }
42704261
ast::expr_bind(f, args) { ret trans_bind(bcx, f, args, e.id, dest); }
42714262
ast::expr_copy(a) {
4272-
if !expr_is_lval(bcx_tcx(bcx), a) {
4273-
ret trans_expr_dps(bcx, a, dest);
4274-
} else {
4275-
// FIXME[DPS] give this a name that makes more sense
4276-
ret trans_expr_backwards_compat(bcx, e, dest);
4277-
}
4263+
if !expr_is_lval(tcx, a) { ret trans_expr_dps(bcx, a, dest); }
4264+
else { ret lval_to_dps(bcx, a, dest); }
42784265
}
42794266
ast::expr_cast(val, _) { ret trans_cast(bcx, val, e.id, dest); }
4267+
ast::expr_anon_obj(anon_obj) {
4268+
ret trans_anon_obj(bcx, e.span, anon_obj, e.id, dest);
4269+
}
4270+
// FIXME[DPS] untangle non-lval calls and fields from trans_lval
4271+
ast::expr_call(_, _) | ast::expr_field(_, _) {
4272+
ret lval_to_dps(bcx, e, dest);
4273+
}
42804274

42814275
// These return nothing
42824276
ast::expr_break. {
@@ -4366,11 +4360,11 @@ fn trans_expr_dps(bcx: @block_ctxt, e: @ast::expr, dest: dest)
43664360
let {bcx, val: addr, is_mem} = trans_lval(bcx, dst);
43674361
assert is_mem;
43684362
// FIXME: calculate copy init-ness in typestate.
4369-
if expr_is_lval(bcx_tcx(bcx), src) {
4363+
if expr_is_lval(tcx, src) {
43704364
ret trans_expr_save_in(bcx, src, addr, DROP_EXISTING);
43714365
} else {
43724366
let srclv = trans_lval(bcx, src);
4373-
let t = ty::expr_ty(bcx_tcx(bcx), src);
4367+
let t = ty::expr_ty(tcx, src);
43744368
ret move_val(srclv.bcx, DROP_EXISTING, addr, srclv, t);
43754369
}
43764370
}
@@ -4379,7 +4373,7 @@ fn trans_expr_dps(bcx: @block_ctxt, e: @ast::expr, dest: dest)
43794373
let lhs_res = trans_lval(bcx, dst);
43804374
assert (lhs_res.is_mem);
43814375
let rhs_res = trans_lval(lhs_res.bcx, src);
4382-
let t = ty::expr_ty(bcx_tcx(bcx), src);
4376+
let t = ty::expr_ty(tcx, src);
43834377
let {bcx: bcx, val: tmp_alloc} = alloc_ty(rhs_res.bcx, t);
43844378
// Swap through a temporary.
43854379
bcx = move_val(bcx, INIT, tmp_alloc, lhs_res, t);
@@ -4390,15 +4384,10 @@ fn trans_expr_dps(bcx: @block_ctxt, e: @ast::expr, dest: dest)
43904384
assert dest == ignore;
43914385
ret trans_assign_op(bcx, op, dst, src);
43924386
}
4393-
4394-
ast::expr_mac(_) { ret bcx_ccx(bcx).sess.bug("unexpanded macro"); }
4395-
// Convert back from result to DPS
4396-
_ { ret trans_expr_backwards_compat(bcx, e, dest); }
43974387
}
43984388
}
43994389

4400-
fn trans_expr_backwards_compat(bcx: @block_ctxt, e: @ast::expr, dest: dest)
4401-
-> @block_ctxt {
4390+
fn lval_to_dps(bcx: @block_ctxt, e: @ast::expr, dest: dest) -> @block_ctxt {
44024391
let lv = trans_lval(bcx, e);
44034392
let {bcx, val, is_mem} = lv;
44044393
let ty = ty::expr_ty(bcx_tcx(bcx), e);

src/comp/middle/trans_objects.rs

Lines changed: 22 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,13 @@ fn trans_obj(cx: @local_ctxt, sp: span, ob: ast::_obj, ctor_id: ast::node_id,
213213
// instead "inlining" the construction of the object and returning the object
214214
// itself.
215215
fn trans_anon_obj(bcx: @block_ctxt, sp: span, anon_obj: ast::anon_obj,
216-
id: ast::node_id) -> result {
216+
id: ast::node_id, dest: trans::dest) -> @block_ctxt {
217+
if dest == trans::ignore {
218+
alt anon_obj.inner_obj {
219+
some(e) { ret trans::trans_expr_dps(bcx, e, trans::ignore); }
220+
none. { ret bcx; }
221+
}
222+
}
217223

218224
let ccx = bcx_ccx(bcx);
219225

@@ -283,43 +289,26 @@ fn trans_anon_obj(bcx: @block_ctxt, sp: span, anon_obj: ast::anon_obj,
283289
}
284290
}
285291

286-
// Allocate the object that we're going to return.
287-
let pair = alloca(bcx, ccx.rust_object_type);
288-
289-
// Take care of cleanups.
290-
let t = node_id_type(ccx, id);
291-
add_clean_temp(bcx, pair, t);
292-
293-
// Grab onto the first and second elements of the pair.
294-
let pair_vtbl = GEP(bcx, pair, [C_int(0), C_int(abi::obj_field_vtbl)]);
295-
let pair_box = GEP(bcx, pair, [C_int(0), C_int(abi::obj_field_box)]);
296-
297292
vtbl = PointerCast(bcx, vtbl, T_ptr(T_empty_struct()));
298-
Store(bcx, vtbl, pair_vtbl);
299293

300294
// Next we have to take care of the other half of the pair we're
301295
// returning: a boxed (reference-counted) tuple containing a tydesc,
302296
// typarams, fields, and a pointer to our inner_obj.
303297
let llbox_ty: TypeRef = T_ptr(T_empty_struct());
304298

305-
if vec::len(additional_fields) == 0u &&
306-
anon_obj.inner_obj == none {
307-
308-
// If the object we're translating has no fields and no inner_obj,
309-
// there's not much to do.
310-
Store(bcx, C_null(llbox_ty), pair_box);
311-
312-
} else {
313-
299+
let box = C_null(llbox_ty);
300+
if vec::len(additional_fields) > 0u || anon_obj.inner_obj != none {
314301
// Synthesize a type for the object body and hand it off to
315302
// trans_malloc_boxed, which allocates a box, including space for a
316303
// refcount.
317304
let body_ty: ty::t =
318305
create_object_body_type(ccx.tcx, additional_field_tys, [],
319306
some(inner_obj_ty));
320-
let box = trans_malloc_boxed(bcx, body_ty);
321-
bcx = box.bcx;
322-
let body = box.body;
307+
let box_r = trans_malloc_boxed(bcx, body_ty);
308+
box = box_r.box;
309+
bcx = box_r.bcx;
310+
add_clean_free(bcx, box, false);
311+
let body = box_r.body;
323312

324313
// Put together a tydesc for the body, so that the object can later be
325314
// freed by calling through its tydesc.
@@ -386,14 +375,15 @@ fn trans_anon_obj(bcx: @block_ctxt, sp: span, anon_obj: ast::anon_obj,
386375
inner_obj_ty);
387376
}
388377
}
389-
390-
// Store box ptr in outer pair.
391-
let p = PointerCast(bcx, box.box, llbox_ty);
392-
Store(bcx, p, pair_box);
378+
revoke_clean(bcx, box);
379+
box = PointerCast(bcx, box, llbox_ty);
393380
}
394-
395-
// return the object we built.
396-
ret rslt(bcx, pair);
381+
let {bcx, val: pair} = trans::get_dest_addr(bcx, dest);
382+
let pair_vtbl = GEP(bcx, pair, [C_int(0), C_int(abi::obj_field_vtbl)]);
383+
Store(bcx, vtbl, pair_vtbl);
384+
let pair_box = GEP(bcx, pair, [C_int(0), C_int(abi::obj_field_box)]);
385+
Store(bcx, box, pair_box);
386+
ret bcx;
397387
}
398388

399389
// Used only inside create_vtbl and create_backwarding_vtbl to distinguish

src/comp/middle/ty.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1299,14 +1299,14 @@ fn vars_in_type(cx: ctxt, ty: t) -> [int] {
12991299
}
13001300

13011301
fn type_autoderef(cx: ctxt, t: ty::t) -> ty::t {
1302-
let t1: ty::t = t;
1302+
let t1 = t;
13031303
while true {
13041304
alt struct(cx, t1) {
1305-
ty::ty_box(mt) { t1 = mt.ty; }
1306-
ty::ty_res(_, inner, tps) {
1305+
ty_box(mt) | ty_uniq(mt) { t1 = mt.ty; }
1306+
ty_res(_, inner, tps) {
13071307
t1 = substitute_type_params(cx, tps, inner);
13081308
}
1309-
ty::ty_tag(did, tps) {
1309+
ty_tag(did, tps) {
13101310
let variants = tag_variants(cx, did);
13111311
if vec::len(variants) != 1u || vec::len(variants[0].args) != 1u {
13121312
break;

0 commit comments

Comments
 (0)