Factor out base::get_discrim_val from const translation

This commit is contained in:
Jed Davis 2012-12-30 20:29:17 -08:00
parent f38e9724e6
commit 452642422d
2 changed files with 27 additions and 24 deletions

View File

@ -779,6 +779,30 @@ fn trans_external_path(ccx: @crate_ctxt, did: ast::def_id, t: ty::t)
};
}
fn get_discrim_val(cx: @crate_ctxt, span: span, enum_did: ast::def_id,
variant_did: ast::def_id) -> ValueRef {
// Can't use `discrims` from the crate context here because
// those discriminants have an extra level of indirection,
// and there's no LLVM constant load instruction.
let mut lldiscrim_opt = None;
for ty::enum_variants(cx.tcx, enum_did).each |variant_info| {
if variant_info.id == variant_did {
lldiscrim_opt = Some(C_int(cx,
variant_info.disr_val));
break;
}
}
match lldiscrim_opt {
None => {
cx.tcx.sess.span_bug(span, ~"didn't find discriminant?!");
}
Some(found_lldiscrim) => {
found_lldiscrim
}
}
}
fn lookup_discriminant(ccx: @crate_ctxt, vid: ast::def_id) -> ValueRef {
unsafe {
let _icx = ccx.insn_ctxt("lookup_discriminant");

View File

@ -416,31 +416,10 @@ fn const_expr(cx: @crate_ctxt, e: @ast::expr) -> ValueRef {
// variants.
let ety = ty::expr_ty(cx.tcx, e);
let llty = type_of::type_of(cx, ety);
let lldiscrim = base::get_discrim_val(cx, e.span,
enum_did,
variant_did);
// Can't use `discrims` from the crate context here
// because those discriminants have an extra level of
// indirection, and there's no LLVM constant load
// instruction.
let mut lldiscrim_opt = None;
for ty::enum_variants(cx.tcx, enum_did).each
|variant_info| {
if variant_info.id == variant_did {
lldiscrim_opt = Some(C_int(cx,
variant_info.disr_val));
break;
}
}
let lldiscrim;
match lldiscrim_opt {
None => {
cx.tcx.sess.span_bug(e.span,
~"didn't find discriminant?!");
}
Some(found_lldiscrim) => {
lldiscrim = found_lldiscrim;
}
}
let fields = if ty::enum_is_univariant(cx.tcx, enum_did) {
~[lldiscrim]
} else {