rustc: Factor out the align-elements logic in dynamic_size_of()

This commit is contained in:
Patrick Walton 2011-03-01 16:01:33 -08:00
parent 348c77c31b
commit bdbaf0c789

View File

@ -851,12 +851,7 @@ fn align_of(@block_ctxt cx, @ty.t t) -> result {
}
fn dynamic_size_of(@block_ctxt cx, @ty.t t) -> result {
alt (t.struct) {
case (ty.ty_param(?p)) {
auto szptr = field_of_tydesc(cx, t, abi.tydesc_field_size);
ret res(szptr.bcx, szptr.bcx.build.Load(szptr.val));
}
case (ty.ty_tup(?elts)) {
fn align_elements(@block_ctxt cx, vec[@ty.t] elts) -> result {
//
// C padding rules:
//
@ -880,21 +875,21 @@ fn dynamic_size_of(@block_ctxt cx, @ty.t t) -> result {
off = align_to(bcx, off, max_align);
ret res(bcx, off);
}
case (ty.ty_rec(?flds)) {
auto off = C_int(0);
auto max_align = C_int(1);
auto bcx = cx;
for (ty.field f in flds) {
auto elt_align = align_of(bcx, f.ty);
bcx = elt_align.bcx;
auto elt_size = size_of(bcx, f.ty);
bcx = elt_size.bcx;
auto aligned_off = align_to(bcx, off, elt_align.val);
off = cx.build.Add(aligned_off, elt_size.val);
max_align = umax(bcx, max_align, elt_align.val);
alt (t.struct) {
case (ty.ty_param(?p)) {
auto szptr = field_of_tydesc(cx, t, abi.tydesc_field_size);
ret res(szptr.bcx, szptr.bcx.build.Load(szptr.val));
}
off = align_to(bcx, off, max_align);
ret res(bcx, off);
case (ty.ty_tup(?elts)) {
ret align_elements(cx, elts);
}
case (ty.ty_rec(?flds)) {
let vec[@ty.t] tys = vec();
for (ty.field f in flds) {
tys += vec(f.ty);
}
ret align_elements(cx, tys);
}
}
}