2011-11-10 10:41:42 -06:00
|
|
|
import std::{str, vec, option};
|
2011-09-12 18:13:28 -05:00
|
|
|
import option::{some, none};
|
2011-07-11 09:47:24 -05:00
|
|
|
|
|
|
|
import lib::llvm::llvm;
|
2011-11-10 10:41:42 -06:00
|
|
|
import lib::llvm::llvm::{ValueRef, BasicBlockRef};
|
2011-08-30 02:59:30 -05:00
|
|
|
import trans_build::*;
|
2011-09-12 18:13:28 -05:00
|
|
|
import trans::{new_sub_block_ctxt, new_scope_block_ctxt, load_if_immediate};
|
2011-07-11 09:47:24 -05:00
|
|
|
import syntax::ast;
|
2011-08-21 23:44:41 -05:00
|
|
|
import syntax::ast_util;
|
|
|
|
import syntax::ast_util::dummy_sp;
|
2011-07-11 09:47:24 -05:00
|
|
|
import syntax::ast::def_id;
|
|
|
|
import syntax::codemap::span;
|
|
|
|
import util::common::lit_eq;
|
|
|
|
|
2011-07-14 19:08:22 -05:00
|
|
|
import trans_common::*;
|
|
|
|
|
2011-09-28 14:07:33 -05:00
|
|
|
// An option identifying a branch (either a literal, a tag variant or a range)
|
2011-07-11 09:47:24 -05:00
|
|
|
tag opt {
|
|
|
|
lit(@ast::lit);
|
2011-07-27 07:19:39 -05:00
|
|
|
var(/* variant id */uint, /* variant dids */{tg: def_id, var: def_id});
|
2011-09-28 14:07:33 -05:00
|
|
|
range(@ast::lit, @ast::lit);
|
2011-07-11 09:47:24 -05:00
|
|
|
}
|
2011-09-12 04:27:30 -05:00
|
|
|
fn opt_eq(a: opt, b: opt) -> bool {
|
2011-07-27 07:19:39 -05:00
|
|
|
alt a {
|
|
|
|
lit(la) {
|
2011-09-28 14:07:33 -05:00
|
|
|
ret alt b { lit(lb) { lit_eq(la, lb) } _ { false } };
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
|
|
|
var(ida, _) {
|
2011-09-28 14:07:33 -05:00
|
|
|
ret alt b { var(idb, _) { ida == idb } _ { false } };
|
|
|
|
}
|
|
|
|
range(la1, la2) {
|
|
|
|
ret alt b {
|
|
|
|
range(lb1, lb2) { lit_eq(la1, lb1) && lit_eq(la2, lb2) }
|
|
|
|
_ { false }
|
|
|
|
};
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
2011-07-11 09:47:24 -05:00
|
|
|
}
|
|
|
|
}
|
2011-09-28 14:07:33 -05:00
|
|
|
|
|
|
|
tag opt_result {
|
|
|
|
single_result(result);
|
|
|
|
range_result(result, result);
|
|
|
|
}
|
|
|
|
fn trans_opt(bcx: @block_ctxt, o: opt) -> opt_result {
|
2011-11-03 04:57:54 -05:00
|
|
|
let ccx = bcx_ccx(bcx), bcx = bcx;
|
2011-07-27 07:19:39 -05:00
|
|
|
alt o {
|
2011-09-27 01:42:27 -05:00
|
|
|
lit(l) {
|
|
|
|
alt l.node {
|
|
|
|
ast::lit_str(s) {
|
2011-09-27 03:50:18 -05:00
|
|
|
let strty = ty::mk_str(bcx_tcx(bcx));
|
2011-10-10 06:32:50 -05:00
|
|
|
let cell = trans::empty_dest_cell();
|
|
|
|
bcx = trans_vec::trans_str(bcx, s, trans::by_val(cell));
|
|
|
|
add_clean_temp(bcx, *cell, strty);
|
|
|
|
ret single_result(rslt(bcx, *cell));
|
2011-09-27 01:42:27 -05:00
|
|
|
}
|
|
|
|
_ {
|
2011-09-28 14:07:33 -05:00
|
|
|
ret single_result(
|
2011-10-14 22:38:24 -05:00
|
|
|
rslt(bcx, trans::trans_crate_lit(ccx, *l)));
|
2011-09-27 01:42:27 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-10-14 22:38:24 -05:00
|
|
|
var(id, _) { ret single_result(rslt(bcx, C_int(ccx, id as int))); }
|
2011-09-28 14:07:33 -05:00
|
|
|
range(l1, l2) {
|
|
|
|
let cell1 = trans::empty_dest_cell();
|
|
|
|
let cell2 = trans::empty_dest_cell();
|
|
|
|
let bcx = trans::trans_lit(bcx, *l1, trans::by_val(cell1));
|
|
|
|
let bcx = trans::trans_lit(bcx, *l2, trans::by_val(cell2));
|
|
|
|
ret range_result(rslt(bcx, *cell1), rslt(bcx, *cell2));
|
|
|
|
}
|
2011-07-11 09:47:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn variant_opt(ccx: @crate_ctxt, pat_id: ast::node_id) -> opt {
|
2011-08-21 23:44:41 -05:00
|
|
|
let vdef = ast_util::variant_def_ids(ccx.tcx.def_map.get(pat_id));
|
2011-07-27 07:19:39 -05:00
|
|
|
let variants = ty::tag_variants(ccx.tcx, vdef.tg);
|
|
|
|
let i = 0u;
|
2011-08-15 23:54:52 -05:00
|
|
|
for v: ty::variant_info in variants {
|
2011-07-27 07:19:39 -05:00
|
|
|
if vdef.var == v.id { ret var(i, vdef); }
|
2011-07-11 09:47:24 -05:00
|
|
|
i += 1u;
|
|
|
|
}
|
|
|
|
fail;
|
|
|
|
}
|
|
|
|
|
2011-08-04 18:20:09 -05:00
|
|
|
type bind_map = [{ident: ast::ident, val: ValueRef}];
|
2011-09-12 04:27:30 -05:00
|
|
|
fn assoc(key: str, list: bind_map) -> option::t<ValueRef> {
|
2011-08-22 07:38:48 -05:00
|
|
|
for elt: {ident: ast::ident, val: ValueRef} in list {
|
2011-09-01 19:27:58 -05:00
|
|
|
if str::eq(elt.ident, key) { ret some(elt.val); }
|
2011-08-22 07:38:48 -05:00
|
|
|
}
|
|
|
|
ret none;
|
|
|
|
}
|
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
type match_branch =
|
2011-08-22 07:38:48 -05:00
|
|
|
@{pats: [@ast::pat],
|
|
|
|
bound: bind_map,
|
2011-09-02 17:34:58 -05:00
|
|
|
data:
|
|
|
|
@{body: BasicBlockRef,
|
|
|
|
guard: option::t<@ast::expr>,
|
|
|
|
id_map: ast_util::pat_id_map}};
|
2011-08-04 18:20:09 -05:00
|
|
|
type match = [match_branch];
|
2011-07-11 09:47:24 -05:00
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn matches_always(p: @ast::pat) -> bool {
|
2011-07-11 09:47:24 -05:00
|
|
|
ret alt p.node {
|
2011-07-27 07:19:39 -05:00
|
|
|
ast::pat_wild. { true }
|
|
|
|
ast::pat_bind(_) { true }
|
|
|
|
ast::pat_rec(_, _) { true }
|
2011-08-15 06:15:19 -05:00
|
|
|
ast::pat_tup(_) { true }
|
2011-07-27 07:19:39 -05:00
|
|
|
_ { false }
|
|
|
|
};
|
2011-07-11 09:47:24 -05:00
|
|
|
}
|
|
|
|
|
2011-10-18 17:07:40 -05:00
|
|
|
type enter_pat = fn@(@ast::pat) -> option::t<[@ast::pat]>;
|
2011-07-27 07:19:39 -05:00
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn enter_match(m: match, col: uint, val: ValueRef, e: enter_pat) -> match {
|
2011-08-19 17:16:48 -05:00
|
|
|
let result = [];
|
2011-08-15 23:54:52 -05:00
|
|
|
for br: match_branch in m {
|
2011-08-19 17:16:48 -05:00
|
|
|
alt e(br.pats[col]) {
|
2011-07-27 07:19:39 -05:00
|
|
|
some(sub) {
|
2011-09-02 17:34:58 -05:00
|
|
|
let pats =
|
|
|
|
vec::slice(br.pats, 0u, col) + sub +
|
2011-08-15 18:38:23 -05:00
|
|
|
vec::slice(br.pats, col + 1u, vec::len(br.pats));
|
2011-09-02 17:34:58 -05:00
|
|
|
let new_br =
|
|
|
|
@{pats: pats,
|
|
|
|
bound:
|
|
|
|
alt br.pats[col].node {
|
|
|
|
ast::pat_bind(name) {
|
|
|
|
br.bound + [{ident: name, val: val}]
|
|
|
|
}
|
|
|
|
_ { br.bound }
|
|
|
|
} with *br};
|
2011-08-19 17:16:48 -05:00
|
|
|
result += [new_br];
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
|
|
|
none. { }
|
2011-07-11 09:47:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ret result;
|
|
|
|
}
|
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn enter_default(m: match, col: uint, val: ValueRef) -> match {
|
|
|
|
fn e(p: @ast::pat) -> option::t<[@ast::pat]> {
|
2011-08-19 17:16:48 -05:00
|
|
|
ret if matches_always(p) { some([]) } else { none };
|
2011-07-13 04:33:46 -05:00
|
|
|
}
|
|
|
|
ret enter_match(m, col, val, e);
|
|
|
|
}
|
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn enter_opt(ccx: @crate_ctxt, m: match, opt: opt, col: uint, tag_size: uint,
|
|
|
|
val: ValueRef) -> match {
|
2011-08-12 10:57:21 -05:00
|
|
|
let dummy = @{id: 0, node: ast::pat_wild, span: dummy_sp()};
|
2011-09-12 04:27:30 -05:00
|
|
|
fn e(ccx: @crate_ctxt, dummy: @ast::pat, opt: opt, size: uint,
|
|
|
|
p: @ast::pat) -> option::t<[@ast::pat]> {
|
2011-07-27 07:19:39 -05:00
|
|
|
alt p.node {
|
|
|
|
ast::pat_tag(ctor, subpats) {
|
|
|
|
ret if opt_eq(variant_opt(ccx, p.id), opt) {
|
|
|
|
some(subpats)
|
|
|
|
} else { none };
|
|
|
|
}
|
|
|
|
ast::pat_lit(l) {
|
2011-08-19 17:16:48 -05:00
|
|
|
ret if opt_eq(lit(l), opt) { some([]) } else { none };
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
2011-09-28 14:07:33 -05:00
|
|
|
ast::pat_range(l1, l2) {
|
|
|
|
ret if opt_eq(range(l1, l2), opt) { some([]) } else { none };
|
|
|
|
}
|
2011-08-15 18:38:23 -05:00
|
|
|
_ { ret some(vec::init_elt(dummy, size)); }
|
2011-07-11 09:47:24 -05:00
|
|
|
}
|
|
|
|
}
|
2011-07-13 04:33:46 -05:00
|
|
|
ret enter_match(m, col, val, bind e(ccx, dummy, opt, tag_size, _));
|
2011-07-11 09:47:24 -05:00
|
|
|
}
|
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn enter_rec(m: match, col: uint, fields: [ast::ident], val: ValueRef) ->
|
2011-07-27 07:19:39 -05:00
|
|
|
match {
|
2011-08-15 15:33:12 -05:00
|
|
|
let dummy = @{id: 0, node: ast::pat_wild, span: dummy_sp()};
|
2011-09-12 04:27:30 -05:00
|
|
|
fn e(dummy: @ast::pat, fields: [ast::ident], p: @ast::pat) ->
|
2011-08-12 09:15:18 -05:00
|
|
|
option::t<[@ast::pat]> {
|
2011-07-27 07:19:39 -05:00
|
|
|
alt p.node {
|
|
|
|
ast::pat_rec(fpats, _) {
|
2011-08-19 17:16:48 -05:00
|
|
|
let pats = [];
|
2011-08-15 23:54:52 -05:00
|
|
|
for fname: ast::ident in fields {
|
2011-07-27 07:19:39 -05:00
|
|
|
let pat = dummy;
|
2011-08-15 23:54:52 -05:00
|
|
|
for fpat: ast::field_pat in fpats {
|
2011-09-01 19:27:58 -05:00
|
|
|
if str::eq(fpat.ident, fname) { pat = fpat.pat; break; }
|
2011-07-11 09:47:24 -05:00
|
|
|
}
|
2011-08-19 17:16:48 -05:00
|
|
|
pats += [pat];
|
2011-07-11 09:47:24 -05:00
|
|
|
}
|
2011-07-27 07:19:39 -05:00
|
|
|
ret some(pats);
|
|
|
|
}
|
2011-08-15 18:38:23 -05:00
|
|
|
_ { ret some(vec::init_elt(dummy, vec::len(fields))); }
|
2011-07-11 09:47:24 -05:00
|
|
|
}
|
|
|
|
}
|
2011-07-13 04:33:46 -05:00
|
|
|
ret enter_match(m, col, val, bind e(dummy, fields, _));
|
2011-07-11 09:47:24 -05:00
|
|
|
}
|
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn enter_tup(m: match, col: uint, val: ValueRef, n_elts: uint) -> match {
|
2011-08-15 15:33:12 -05:00
|
|
|
let dummy = @{id: 0, node: ast::pat_wild, span: dummy_sp()};
|
2011-09-12 04:27:30 -05:00
|
|
|
fn e(dummy: @ast::pat, n_elts: uint, p: @ast::pat) ->
|
2011-08-19 17:16:48 -05:00
|
|
|
option::t<[@ast::pat]> {
|
2011-08-15 06:15:19 -05:00
|
|
|
alt p.node {
|
|
|
|
ast::pat_tup(elts) { ret some(elts); }
|
2011-08-15 18:38:23 -05:00
|
|
|
_ { ret some(vec::init_elt(dummy, n_elts)); }
|
2011-08-15 06:15:19 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ret enter_match(m, col, val, bind e(dummy, n_elts, _));
|
|
|
|
}
|
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn enter_box(m: match, col: uint, val: ValueRef) -> match {
|
2011-08-12 10:57:21 -05:00
|
|
|
let dummy = @{id: 0, node: ast::pat_wild, span: dummy_sp()};
|
2011-09-12 04:27:30 -05:00
|
|
|
fn e(dummy: @ast::pat, p: @ast::pat) -> option::t<[@ast::pat]> {
|
2011-07-27 07:19:39 -05:00
|
|
|
alt p.node {
|
2011-08-19 17:16:48 -05:00
|
|
|
ast::pat_box(sub) { ret some([sub]); }
|
|
|
|
_ { ret some([dummy]); }
|
2011-07-13 04:11:43 -05:00
|
|
|
}
|
|
|
|
}
|
2011-07-13 04:33:46 -05:00
|
|
|
ret enter_match(m, col, val, bind e(dummy, _));
|
2011-07-13 04:11:43 -05:00
|
|
|
}
|
|
|
|
|
2011-09-23 13:15:17 -05:00
|
|
|
fn enter_uniq(m: match, col: uint, val: ValueRef) -> match {
|
|
|
|
let dummy = @{id: 0, node: ast::pat_wild, span: dummy_sp()};
|
|
|
|
fn e(dummy: @ast::pat, p: @ast::pat) -> option::t<[@ast::pat]> {
|
|
|
|
alt p.node {
|
|
|
|
ast::pat_uniq(sub) { ret some([sub]); }
|
|
|
|
_ { ret some([dummy]); }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ret enter_match(m, col, val, bind e(dummy, _));
|
|
|
|
}
|
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn get_options(ccx: @crate_ctxt, m: match, col: uint) -> [opt] {
|
2011-09-12 05:39:38 -05:00
|
|
|
fn add_to_set(&set: [opt], val: opt) {
|
2011-08-15 23:54:52 -05:00
|
|
|
for l: opt in set { if opt_eq(l, val) { ret; } }
|
2011-08-19 17:16:48 -05:00
|
|
|
set += [val];
|
2011-07-11 09:47:24 -05:00
|
|
|
}
|
|
|
|
|
2011-08-19 17:16:48 -05:00
|
|
|
let found = [];
|
2011-08-15 23:54:52 -05:00
|
|
|
for br: match_branch in m {
|
2011-08-19 17:16:48 -05:00
|
|
|
alt br.pats[col].node {
|
2011-07-27 07:19:39 -05:00
|
|
|
ast::pat_lit(l) { add_to_set(found, lit(l)); }
|
2011-09-28 14:07:33 -05:00
|
|
|
ast::pat_range(l1, l2) {
|
|
|
|
add_to_set(found, range(l1, l2));
|
|
|
|
}
|
2011-07-27 07:19:39 -05:00
|
|
|
ast::pat_tag(_, _) {
|
2011-08-19 17:16:48 -05:00
|
|
|
add_to_set(found, variant_opt(ccx, br.pats[col].id));
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
|
|
|
_ { }
|
2011-07-11 09:47:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ret found;
|
|
|
|
}
|
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
fn extract_variant_args(bcx: @block_ctxt, pat_id: ast::node_id,
|
2011-09-12 04:27:30 -05:00
|
|
|
vdefs: {tg: def_id, var: def_id}, val: ValueRef) ->
|
2011-08-19 17:16:48 -05:00
|
|
|
{vals: [ValueRef], bcx: @block_ctxt} {
|
2011-11-03 04:57:54 -05:00
|
|
|
let ccx = bcx.fcx.lcx.ccx, bcx = bcx;
|
2011-07-27 07:19:39 -05:00
|
|
|
let ty_param_substs = ty::node_id_to_type_params(ccx.tcx, pat_id);
|
|
|
|
let blobptr = val;
|
|
|
|
let variants = ty::tag_variants(ccx.tcx, vdefs.tg);
|
2011-08-19 17:16:48 -05:00
|
|
|
let args = [];
|
2011-07-27 07:19:39 -05:00
|
|
|
let size =
|
2011-08-15 18:38:23 -05:00
|
|
|
vec::len(ty::tag_variant_with_id(ccx.tcx, vdefs.tg, vdefs.var).args);
|
|
|
|
if size > 0u && vec::len(variants) != 1u {
|
2011-07-27 07:19:39 -05:00
|
|
|
let tagptr =
|
2011-10-14 22:38:24 -05:00
|
|
|
PointerCast(bcx, val, trans_common::T_opaque_tag_ptr(ccx));
|
2011-10-26 00:23:28 -05:00
|
|
|
blobptr = GEPi(bcx, tagptr, [0, 1]);
|
2011-07-11 09:47:24 -05:00
|
|
|
}
|
2011-07-27 07:19:39 -05:00
|
|
|
let i = 0u;
|
2011-09-01 19:25:06 -05:00
|
|
|
let vdefs_tg = vdefs.tg;
|
|
|
|
let vdefs_var = vdefs.var;
|
2011-07-27 07:19:39 -05:00
|
|
|
while i < size {
|
2011-09-02 17:34:58 -05:00
|
|
|
check (valid_variant_index(i, bcx, vdefs_tg, vdefs_var));
|
2011-07-27 07:19:39 -05:00
|
|
|
let r =
|
2011-09-01 19:25:06 -05:00
|
|
|
trans::GEP_tag(bcx, blobptr, vdefs_tg, vdefs_var, ty_param_substs,
|
2011-09-01 18:31:18 -05:00
|
|
|
i);
|
2011-07-11 09:47:24 -05:00
|
|
|
bcx = r.bcx;
|
2011-08-19 17:16:48 -05:00
|
|
|
args += [r.val];
|
2011-07-11 09:47:24 -05:00
|
|
|
i += 1u;
|
|
|
|
}
|
2011-07-27 07:19:39 -05:00
|
|
|
ret {vals: args, bcx: bcx};
|
2011-07-11 09:47:24 -05:00
|
|
|
}
|
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn collect_record_fields(m: match, col: uint) -> [ast::ident] {
|
2011-08-19 17:16:48 -05:00
|
|
|
let fields = [];
|
2011-08-15 23:54:52 -05:00
|
|
|
for br: match_branch in m {
|
2011-08-19 17:16:48 -05:00
|
|
|
alt br.pats[col].node {
|
2011-07-27 07:19:39 -05:00
|
|
|
ast::pat_rec(fs, _) {
|
2011-08-15 23:54:52 -05:00
|
|
|
for f: ast::field_pat in fs {
|
2011-09-01 19:27:58 -05:00
|
|
|
if !vec::any(bind str::eq(f.ident, _), fields) {
|
2011-08-19 17:16:48 -05:00
|
|
|
fields += [f.ident];
|
2011-07-13 04:11:43 -05:00
|
|
|
}
|
|
|
|
}
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
|
|
|
_ { }
|
2011-07-13 04:11:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ret fields;
|
|
|
|
}
|
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn any_box_pat(m: match, col: uint) -> bool {
|
2011-08-15 23:54:52 -05:00
|
|
|
for br: match_branch in m {
|
2011-08-19 17:16:48 -05:00
|
|
|
alt br.pats[col].node { ast::pat_box(_) { ret true; } _ { } }
|
2011-07-13 04:11:43 -05:00
|
|
|
}
|
|
|
|
ret false;
|
|
|
|
}
|
|
|
|
|
2011-09-23 13:15:17 -05:00
|
|
|
fn any_uniq_pat(m: match, col: uint) -> bool {
|
|
|
|
for br: match_branch in m {
|
|
|
|
alt br.pats[col].node { ast::pat_uniq(_) { ret true; } _ { } }
|
|
|
|
}
|
|
|
|
ret false;
|
|
|
|
}
|
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn any_tup_pat(m: match, col: uint) -> bool {
|
2011-08-15 06:15:19 -05:00
|
|
|
for br: match_branch in m {
|
2011-08-19 17:16:48 -05:00
|
|
|
alt br.pats[col].node { ast::pat_tup(_) { ret true; } _ { } }
|
2011-08-15 06:15:19 -05:00
|
|
|
}
|
|
|
|
ret false;
|
|
|
|
}
|
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
type exit_node = {bound: bind_map, from: BasicBlockRef, to: BasicBlockRef};
|
2011-10-18 17:07:40 -05:00
|
|
|
type mk_fail = fn@() -> BasicBlockRef;
|
2011-08-02 05:57:27 -05:00
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn pick_col(m: match) -> uint {
|
2011-08-19 17:16:48 -05:00
|
|
|
let scores = vec::init_elt_mut(0u, vec::len(m[0].pats));
|
2011-08-02 05:57:27 -05:00
|
|
|
for br: match_branch in m {
|
|
|
|
let i = 0u;
|
|
|
|
for p: @ast::pat in br.pats {
|
|
|
|
alt p.node {
|
2011-09-28 14:07:33 -05:00
|
|
|
ast::pat_lit(_) | ast::pat_tag(_, _) | ast::pat_range(_, _) {
|
|
|
|
scores[i] += 1u;
|
|
|
|
}
|
2011-08-19 17:16:48 -05:00
|
|
|
_ { }
|
2011-08-02 05:57:27 -05:00
|
|
|
}
|
|
|
|
i += 1u;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let max_score = 0u;
|
|
|
|
let best_col = 0u;
|
|
|
|
let i = 0u;
|
|
|
|
for score: uint in scores {
|
|
|
|
// Irrefutable columns always go first, they'd only be duplicated in
|
|
|
|
// the branches.
|
|
|
|
if score == 0u { ret i; }
|
|
|
|
// If no irrefutable ones are found, we pick the one with the biggest
|
|
|
|
// branching factor.
|
2011-08-19 17:16:48 -05:00
|
|
|
if score > max_score { max_score = score; best_col = i; }
|
2011-08-02 05:57:27 -05:00
|
|
|
i += 1u;
|
|
|
|
}
|
|
|
|
ret best_col;
|
|
|
|
}
|
2011-07-11 09:47:24 -05:00
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn compile_submatch(bcx: @block_ctxt, m: match, vals: [ValueRef], f: mk_fail,
|
2011-09-12 05:39:38 -05:00
|
|
|
&exits: [exit_node]) {
|
2011-11-03 04:57:54 -05:00
|
|
|
let bcx = bcx;
|
2011-08-30 02:59:30 -05:00
|
|
|
if vec::len(m) == 0u { Br(bcx, f()); ret; }
|
2011-08-19 17:16:48 -05:00
|
|
|
if vec::len(m[0].pats) == 0u {
|
2011-08-22 07:38:48 -05:00
|
|
|
let data = m[0].data;
|
|
|
|
alt data.guard {
|
|
|
|
some(e) {
|
2011-09-02 17:34:58 -05:00
|
|
|
let guard_cx = new_scope_block_ctxt(bcx, "submatch_guard");
|
2011-08-30 02:59:30 -05:00
|
|
|
Br(bcx, guard_cx.llbb);
|
2011-08-22 07:38:48 -05:00
|
|
|
// Temporarily set bindings. They'll be rewritten to PHI nodes for
|
|
|
|
// the actual arm block.
|
2011-10-21 05:21:27 -05:00
|
|
|
data.id_map.items {|key, val|
|
2011-10-07 04:20:51 -05:00
|
|
|
let local = local_mem(option::get(assoc(key, m[0].bound)));
|
|
|
|
bcx.fcx.lllocals.insert(val, local);
|
2011-10-21 05:21:27 -05:00
|
|
|
};
|
2011-08-24 07:01:56 -05:00
|
|
|
let {bcx: guard_bcx, val: guard_val} =
|
2011-10-05 04:26:27 -05:00
|
|
|
trans::trans_temp_expr(guard_cx, e);
|
2011-08-24 07:01:56 -05:00
|
|
|
guard_bcx = trans::trans_block_cleanups(guard_bcx, guard_cx);
|
2011-09-21 05:40:27 -05:00
|
|
|
let next_cx = new_sub_block_ctxt(guard_cx, "submatch_next");
|
|
|
|
let else_cx = new_sub_block_ctxt(guard_cx, "submatch_else");
|
2011-08-30 02:59:30 -05:00
|
|
|
CondBr(guard_bcx, guard_val, next_cx.llbb, else_cx.llbb);
|
2011-09-02 17:34:58 -05:00
|
|
|
compile_submatch(else_cx, vec::slice(m, 1u, vec::len(m)), vals, f,
|
|
|
|
exits);
|
2011-08-22 07:38:48 -05:00
|
|
|
bcx = next_cx;
|
|
|
|
}
|
2011-09-02 17:34:58 -05:00
|
|
|
_ { }
|
2011-08-22 07:38:48 -05:00
|
|
|
}
|
2011-09-21 05:40:27 -05:00
|
|
|
if !bcx.unreachable {
|
|
|
|
exits += [{bound: m[0].bound, from: bcx.llbb, to: data.body}];
|
|
|
|
}
|
2011-08-30 02:59:30 -05:00
|
|
|
Br(bcx, data.body);
|
2011-07-11 09:47:24 -05:00
|
|
|
ret;
|
|
|
|
}
|
|
|
|
|
2011-08-02 05:57:27 -05:00
|
|
|
let col = pick_col(m);
|
2011-08-19 17:16:48 -05:00
|
|
|
let val = vals[col];
|
|
|
|
let vals_left =
|
|
|
|
vec::slice(vals, 0u, col) +
|
|
|
|
vec::slice(vals, col + 1u, vec::len(vals));
|
2011-07-27 07:19:39 -05:00
|
|
|
let ccx = bcx.fcx.lcx.ccx;
|
|
|
|
let pat_id = 0;
|
2011-08-15 23:54:52 -05:00
|
|
|
for br: match_branch in m {
|
2011-08-19 17:16:48 -05:00
|
|
|
|
2011-07-11 09:47:24 -05:00
|
|
|
// Find a real id (we're adding placeholder wildcard patterns, but
|
|
|
|
// each column is guaranteed to have at least one real pattern)
|
2011-08-19 17:16:48 -05:00
|
|
|
if pat_id == 0 { pat_id = br.pats[col].id; }
|
2011-07-11 09:47:24 -05:00
|
|
|
}
|
2011-07-13 04:11:43 -05:00
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
let rec_fields = collect_record_fields(m, col);
|
2011-07-11 09:47:24 -05:00
|
|
|
// Separate path for extracting and binding record fields
|
2011-08-15 18:38:23 -05:00
|
|
|
if vec::len(rec_fields) > 0u {
|
2011-07-27 07:19:39 -05:00
|
|
|
let rec_ty = ty::node_id_to_monotype(ccx.tcx, pat_id);
|
|
|
|
let fields =
|
|
|
|
alt ty::struct(ccx.tcx, rec_ty) { ty::ty_rec(fields) { fields } };
|
2011-08-19 17:16:48 -05:00
|
|
|
let rec_vals = [];
|
2011-07-28 05:01:45 -05:00
|
|
|
for field_name: ast::ident in rec_fields {
|
2011-07-27 07:19:39 -05:00
|
|
|
let ix: uint =
|
2011-08-12 10:57:21 -05:00
|
|
|
ty::field_idx(ccx.sess, dummy_sp(), field_name, fields);
|
2011-09-17 12:18:30 -05:00
|
|
|
// not sure how to get rid of this check
|
|
|
|
check type_is_tup_like(bcx, rec_ty);
|
2011-08-19 17:16:48 -05:00
|
|
|
let r = trans::GEP_tup_like(bcx, rec_ty, val, [0, ix as int]);
|
|
|
|
rec_vals += [r.val];
|
2011-07-11 09:47:24 -05:00
|
|
|
bcx = r.bcx;
|
|
|
|
}
|
2011-07-13 04:33:46 -05:00
|
|
|
compile_submatch(bcx, enter_rec(m, col, rec_fields, val),
|
2011-07-11 09:47:24 -05:00
|
|
|
rec_vals + vals_left, f, exits);
|
|
|
|
ret;
|
|
|
|
}
|
|
|
|
|
2011-08-15 06:15:19 -05:00
|
|
|
if any_tup_pat(m, col) {
|
|
|
|
let tup_ty = ty::node_id_to_monotype(ccx.tcx, pat_id);
|
2011-08-19 17:16:48 -05:00
|
|
|
let n_tup_elts =
|
|
|
|
alt ty::struct(ccx.tcx, tup_ty) {
|
|
|
|
ty::ty_tup(elts) { vec::len(elts) }
|
|
|
|
};
|
|
|
|
let tup_vals = [], i = 0u;
|
2011-08-15 06:15:19 -05:00
|
|
|
while i < n_tup_elts {
|
2011-09-17 12:18:30 -05:00
|
|
|
// how to get rid of this check?
|
|
|
|
check type_is_tup_like(bcx, tup_ty);
|
2011-08-19 17:16:48 -05:00
|
|
|
let r = trans::GEP_tup_like(bcx, tup_ty, val, [0, i as int]);
|
|
|
|
tup_vals += [r.val];
|
2011-08-15 06:15:19 -05:00
|
|
|
bcx = r.bcx;
|
|
|
|
i += 1u;
|
|
|
|
}
|
|
|
|
compile_submatch(bcx, enter_tup(m, col, val, n_tup_elts),
|
|
|
|
tup_vals + vals_left, f, exits);
|
|
|
|
ret;
|
|
|
|
}
|
|
|
|
|
2011-07-13 04:11:43 -05:00
|
|
|
// Unbox in case of a box field
|
2011-07-27 07:19:39 -05:00
|
|
|
if any_box_pat(m, col) {
|
2011-08-30 02:59:30 -05:00
|
|
|
let box = Load(bcx, val);
|
2011-10-26 00:23:28 -05:00
|
|
|
let unboxed = GEPi(bcx, box, [0, back::abi::box_rc_field_body]);
|
2011-08-19 17:16:48 -05:00
|
|
|
compile_submatch(bcx, enter_box(m, col, val), [unboxed] + vals_left,
|
2011-07-27 07:19:39 -05:00
|
|
|
f, exits);
|
2011-07-13 04:11:43 -05:00
|
|
|
ret;
|
|
|
|
}
|
|
|
|
|
2011-09-23 13:15:17 -05:00
|
|
|
if any_uniq_pat(m, col) {
|
|
|
|
let unboxed = Load(bcx, val);
|
|
|
|
compile_submatch(bcx, enter_uniq(m, col, val),
|
|
|
|
[unboxed] + vals_left, f, exits);
|
|
|
|
ret;
|
|
|
|
}
|
|
|
|
|
2011-07-11 09:47:24 -05:00
|
|
|
// Decide what kind of branch we need
|
2011-07-27 07:19:39 -05:00
|
|
|
let opts = get_options(ccx, m, col);
|
2011-07-11 09:47:24 -05:00
|
|
|
tag branch_kind { no_branch; single; switch; compare; }
|
2011-07-27 07:19:39 -05:00
|
|
|
let kind = no_branch;
|
|
|
|
let test_val = val;
|
2011-08-15 18:38:23 -05:00
|
|
|
if vec::len(opts) > 0u {
|
2011-08-19 17:16:48 -05:00
|
|
|
alt opts[0] {
|
2011-07-27 07:19:39 -05:00
|
|
|
var(_, vdef) {
|
2011-08-15 18:38:23 -05:00
|
|
|
if vec::len(ty::tag_variants(ccx.tcx, vdef.tg)) == 1u {
|
2011-07-27 07:19:39 -05:00
|
|
|
kind = single;
|
|
|
|
} else {
|
2011-08-19 17:16:48 -05:00
|
|
|
let tagptr =
|
2011-08-30 02:59:30 -05:00
|
|
|
PointerCast(bcx, val,
|
2011-10-14 22:38:24 -05:00
|
|
|
trans_common::T_opaque_tag_ptr(ccx));
|
2011-10-26 00:23:28 -05:00
|
|
|
let discrimptr = GEPi(bcx, tagptr, [0, 0]);
|
2011-08-30 02:59:30 -05:00
|
|
|
test_val = Load(bcx, discrimptr);
|
2011-07-27 07:19:39 -05:00
|
|
|
kind = switch;
|
2011-07-11 09:47:24 -05:00
|
|
|
}
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
|
|
|
lit(l) {
|
2011-09-27 15:49:33 -05:00
|
|
|
kind = alt l.node {
|
2011-10-18 06:49:28 -05:00
|
|
|
ast::lit_str(_) | ast::lit_nil. | ast::lit_float(_) |
|
|
|
|
ast::lit_mach_float(_, _) {
|
2011-10-10 06:32:50 -05:00
|
|
|
test_val = Load(bcx, val); compare
|
|
|
|
}
|
2011-09-27 15:49:33 -05:00
|
|
|
_ { test_val = Load(bcx, val); switch }
|
|
|
|
};
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
2011-09-28 14:07:33 -05:00
|
|
|
range(_, _) {
|
|
|
|
test_val = Load(bcx, val);
|
|
|
|
kind = compare;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for o: opt in opts {
|
|
|
|
alt o {
|
|
|
|
range(_, _) { kind = compare; break; }
|
|
|
|
_ { }
|
2011-07-11 09:47:24 -05:00
|
|
|
}
|
|
|
|
}
|
2011-07-27 07:19:39 -05:00
|
|
|
let else_cx =
|
|
|
|
alt kind {
|
|
|
|
no_branch. | single. { bcx }
|
2011-09-02 17:34:58 -05:00
|
|
|
_ { new_sub_block_ctxt(bcx, "match_else") }
|
2011-07-27 07:19:39 -05:00
|
|
|
};
|
2011-09-21 05:40:27 -05:00
|
|
|
let sw;
|
|
|
|
if kind == switch {
|
|
|
|
sw = Switch(bcx, test_val, else_cx.llbb, vec::len(opts));
|
|
|
|
// FIXME This statement is purely here as a work-around for a bug that
|
|
|
|
// I expect to be the same as issue #951. If I remove it, sw ends up
|
|
|
|
// holding a corrupted value (when the compiler is optimized).
|
|
|
|
// This can be removed after our next LLVM upgrade.
|
|
|
|
val_ty(sw);
|
2011-10-14 22:38:24 -05:00
|
|
|
} else { sw = C_int(ccx, 0); } // Placeholder for when not using a switch
|
2011-07-27 07:19:39 -05:00
|
|
|
|
|
|
|
// Compile subtrees for each option
|
2011-08-15 23:54:52 -05:00
|
|
|
for opt: opt in opts {
|
2011-09-02 17:34:58 -05:00
|
|
|
let opt_cx = new_sub_block_ctxt(bcx, "match_case");
|
2011-07-27 07:19:39 -05:00
|
|
|
alt kind {
|
2011-08-30 02:59:30 -05:00
|
|
|
single. { Br(bcx, opt_cx.llbb); }
|
2011-07-27 07:19:39 -05:00
|
|
|
switch. {
|
2011-09-28 14:07:33 -05:00
|
|
|
let res = trans_opt(bcx, opt);
|
|
|
|
alt res {
|
|
|
|
single_result(r) {
|
|
|
|
llvm::LLVMAddCase(sw, r.val, opt_cx.llbb);
|
|
|
|
bcx = r.bcx;
|
|
|
|
}
|
|
|
|
}
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
|
|
|
compare. {
|
2011-09-02 17:34:58 -05:00
|
|
|
let compare_cx = new_scope_block_ctxt(bcx, "compare_scope");
|
2011-09-01 00:34:41 -05:00
|
|
|
Br(bcx, compare_cx.llbb);
|
|
|
|
bcx = compare_cx;
|
2011-07-27 07:19:39 -05:00
|
|
|
let t = ty::node_id_to_type(ccx.tcx, pat_id);
|
2011-09-28 14:07:33 -05:00
|
|
|
let res = trans_opt(bcx, opt);
|
|
|
|
alt res {
|
|
|
|
single_result(r) {
|
|
|
|
bcx = r.bcx;
|
|
|
|
let eq =
|
|
|
|
trans::trans_compare(bcx, ast::eq, test_val, t, r.val, t);
|
|
|
|
let cleanup_cx = trans::trans_block_cleanups(
|
|
|
|
eq.bcx, compare_cx);
|
|
|
|
bcx = new_sub_block_ctxt(bcx, "compare_next");
|
|
|
|
CondBr(cleanup_cx, eq.val, opt_cx.llbb, bcx.llbb);
|
|
|
|
}
|
|
|
|
range_result(rbegin, rend) {
|
|
|
|
bcx = rend.bcx;
|
|
|
|
let ge = trans::trans_compare(bcx, ast::ge, test_val, t,
|
|
|
|
rbegin.val, t);
|
|
|
|
let le = trans::trans_compare(ge.bcx, ast::le, test_val, t,
|
|
|
|
rend.val, t);
|
|
|
|
let in_range = rslt(le.bcx, And(le.bcx, ge.val, le.val));
|
2011-11-03 04:57:54 -05:00
|
|
|
bcx = in_range.bcx;
|
2011-09-28 14:07:33 -05:00
|
|
|
let cleanup_cx =
|
|
|
|
trans::trans_block_cleanups(bcx, compare_cx);
|
|
|
|
bcx = new_sub_block_ctxt(bcx, "compare_next");
|
|
|
|
CondBr(cleanup_cx, in_range.val, opt_cx.llbb, bcx.llbb);
|
|
|
|
}
|
|
|
|
}
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
|
|
|
_ { }
|
2011-07-11 09:47:24 -05:00
|
|
|
}
|
2011-07-27 07:19:39 -05:00
|
|
|
let size = 0u;
|
2011-08-19 17:16:48 -05:00
|
|
|
let unpacked = [];
|
2011-07-11 09:47:24 -05:00
|
|
|
alt opt {
|
2011-07-27 07:19:39 -05:00
|
|
|
var(_, vdef) {
|
|
|
|
let args = extract_variant_args(opt_cx, pat_id, vdef, val);
|
2011-08-15 18:38:23 -05:00
|
|
|
size = vec::len(args.vals);
|
2011-07-27 07:19:39 -05:00
|
|
|
unpacked = args.vals;
|
|
|
|
opt_cx = args.bcx;
|
|
|
|
}
|
2011-09-28 14:07:33 -05:00
|
|
|
lit(_) | range(_, _) { }
|
2011-07-11 09:47:24 -05:00
|
|
|
}
|
|
|
|
compile_submatch(opt_cx, enter_opt(ccx, m, opt, col, size, val),
|
|
|
|
unpacked + vals_left, f, exits);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compile the fall-through case
|
2011-08-30 02:59:30 -05:00
|
|
|
if kind == compare { Br(bcx, else_cx.llbb); }
|
2011-07-27 07:19:39 -05:00
|
|
|
if kind != single {
|
|
|
|
compile_submatch(else_cx, enter_default(m, col, val), vals_left, f,
|
|
|
|
exits);
|
2011-07-11 09:47:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-19 10:07:51 -05:00
|
|
|
// Returns false for unreachable blocks
|
2011-09-12 04:27:30 -05:00
|
|
|
fn make_phi_bindings(bcx: @block_ctxt, map: [exit_node],
|
|
|
|
ids: ast_util::pat_id_map) -> bool {
|
2011-07-27 07:19:39 -05:00
|
|
|
let our_block = bcx.llbb as uint;
|
2011-11-03 04:57:54 -05:00
|
|
|
let success = true, bcx = bcx;
|
2011-10-21 05:21:27 -05:00
|
|
|
ids.items {|name, node_id|
|
2011-08-19 17:16:48 -05:00
|
|
|
let llbbs = [];
|
|
|
|
let vals = [];
|
2011-08-15 23:54:52 -05:00
|
|
|
for ex: exit_node in map {
|
2011-07-27 07:19:39 -05:00
|
|
|
if ex.to as uint == our_block {
|
2011-09-13 05:14:30 -05:00
|
|
|
alt assoc(name, ex.bound) {
|
2011-08-19 17:16:48 -05:00
|
|
|
some(val) { llbbs += [ex.from]; vals += [val]; }
|
2011-07-27 07:19:39 -05:00
|
|
|
none. { }
|
2011-07-19 10:07:51 -05:00
|
|
|
}
|
2011-07-11 09:47:24 -05:00
|
|
|
}
|
|
|
|
}
|
2011-08-15 18:38:23 -05:00
|
|
|
if vec::len(vals) > 0u {
|
2011-09-13 05:14:30 -05:00
|
|
|
let local = Phi(bcx, val_ty(vals[0]), vals, llbbs);
|
2011-10-07 04:20:51 -05:00
|
|
|
bcx.fcx.lllocals.insert(node_id, local_mem(local));
|
2011-07-19 10:07:51 -05:00
|
|
|
} else { success = false; }
|
2011-10-21 05:21:27 -05:00
|
|
|
};
|
2011-09-13 05:14:30 -05:00
|
|
|
if success {
|
|
|
|
// Copy references that the alias analysis considered unsafe
|
2011-10-21 05:21:27 -05:00
|
|
|
ids.values {|node_id|
|
2011-09-13 05:14:30 -05:00
|
|
|
if bcx_ccx(bcx).copy_map.contains_key(node_id) {
|
2011-10-07 04:20:51 -05:00
|
|
|
let local = alt bcx.fcx.lllocals.get(node_id) {
|
|
|
|
local_mem(x) { x }
|
|
|
|
};
|
2011-09-13 05:14:30 -05:00
|
|
|
let e_ty = ty::node_id_to_type(bcx_tcx(bcx), node_id);
|
|
|
|
let {bcx: abcx, val: alloc} = trans::alloc_ty(bcx, e_ty);
|
|
|
|
bcx = trans::copy_val(abcx, trans::INIT, alloc,
|
|
|
|
load_if_immediate(abcx, local, e_ty),
|
|
|
|
e_ty);
|
|
|
|
add_clean(bcx, alloc, e_ty);
|
2011-10-07 04:20:51 -05:00
|
|
|
bcx.fcx.lllocals.insert(node_id, local_mem(alloc));
|
2011-09-13 05:14:30 -05:00
|
|
|
}
|
2011-10-21 05:21:27 -05:00
|
|
|
};
|
2011-09-23 16:37:15 -05:00
|
|
|
} else {
|
|
|
|
Unreachable(bcx);
|
2011-09-13 05:14:30 -05:00
|
|
|
}
|
2011-07-19 10:07:51 -05:00
|
|
|
ret success;
|
2011-07-11 09:47:24 -05:00
|
|
|
}
|
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn trans_alt(cx: @block_ctxt, expr: @ast::expr, arms: [ast::arm],
|
2011-09-23 14:13:50 -05:00
|
|
|
dest: trans::dest) -> @block_ctxt {
|
2011-08-19 17:16:48 -05:00
|
|
|
let bodies = [];
|
|
|
|
let match: match = [];
|
2011-11-02 08:30:51 -05:00
|
|
|
let alt_cx = new_scope_block_ctxt(cx, "alt");
|
|
|
|
Br(cx, alt_cx.llbb);
|
|
|
|
|
|
|
|
let er = trans::trans_temp_expr(alt_cx, expr);
|
2011-09-23 14:13:50 -05:00
|
|
|
if er.bcx.unreachable { ret er.bcx; }
|
2011-08-02 21:02:38 -05:00
|
|
|
|
2011-08-15 23:54:52 -05:00
|
|
|
for a: ast::arm in arms {
|
2011-09-21 05:40:27 -05:00
|
|
|
let body = new_scope_block_ctxt(er.bcx, "case_body");
|
2011-08-21 23:44:41 -05:00
|
|
|
let id_map = ast_util::pat_id_map(a.pats[0]);
|
2011-08-19 17:16:48 -05:00
|
|
|
bodies += [body];
|
2011-08-15 23:54:52 -05:00
|
|
|
for p: @ast::pat in a.pats {
|
2011-09-02 17:34:58 -05:00
|
|
|
match +=
|
|
|
|
[@{pats: [p],
|
|
|
|
bound: [],
|
|
|
|
data: @{body: body.llbb, guard: a.guard, id_map: id_map}}];
|
2011-07-11 09:47:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cached fail-on-fallthrough block
|
2011-07-27 07:19:39 -05:00
|
|
|
let fail_cx = @mutable none;
|
2011-09-12 04:27:30 -05:00
|
|
|
fn mk_fail(cx: @block_ctxt, sp: span,
|
2011-08-12 09:15:18 -05:00
|
|
|
done: @mutable option::t<BasicBlockRef>) -> BasicBlockRef {
|
2011-07-27 07:19:39 -05:00
|
|
|
alt *done { some(bb) { ret bb; } _ { } }
|
2011-09-02 17:34:58 -05:00
|
|
|
let fail_cx = new_sub_block_ctxt(cx, "case_fallthrough");
|
|
|
|
trans::trans_fail(fail_cx, some(sp), "non-exhaustive match failure");;
|
2011-07-11 09:47:24 -05:00
|
|
|
*done = some(fail_cx.llbb);
|
|
|
|
ret fail_cx.llbb;
|
|
|
|
}
|
|
|
|
|
2011-08-19 17:16:48 -05:00
|
|
|
let exit_map = [];
|
2011-07-27 07:19:39 -05:00
|
|
|
let t = trans::node_id_type(cx.fcx.lcx.ccx, expr.id);
|
2011-09-02 17:12:27 -05:00
|
|
|
let vr = trans::spill_if_immediate(er.bcx, er.val, t);
|
|
|
|
compile_submatch(vr.bcx, match, [vr.val],
|
2011-11-02 08:30:51 -05:00
|
|
|
bind mk_fail(alt_cx, expr.span, fail_cx), exit_map);
|
2011-07-11 09:47:24 -05:00
|
|
|
|
2011-09-23 14:13:50 -05:00
|
|
|
let arm_cxs = [], arm_dests = [], i = 0u;
|
2011-08-15 23:54:52 -05:00
|
|
|
for a: ast::arm in arms {
|
2011-08-19 17:16:48 -05:00
|
|
|
let body_cx = bodies[i];
|
2011-08-21 23:44:41 -05:00
|
|
|
if make_phi_bindings(body_cx, exit_map,
|
|
|
|
ast_util::pat_id_map(a.pats[0])) {
|
2011-09-23 14:13:50 -05:00
|
|
|
let arm_dest = trans::dup_for_join(dest);
|
|
|
|
arm_dests += [arm_dest];
|
|
|
|
arm_cxs += [trans::trans_block_dps(body_cx, a.body, arm_dest)];
|
2011-07-19 10:07:51 -05:00
|
|
|
}
|
2011-07-11 09:47:24 -05:00
|
|
|
i += 1u;
|
|
|
|
}
|
2011-11-02 08:30:51 -05:00
|
|
|
let after_cx = trans::join_returns(cx, arm_cxs, arm_dests, dest);
|
|
|
|
after_cx = trans::trans_block_cleanups(after_cx, alt_cx);
|
|
|
|
let next_cx = new_sub_block_ctxt(after_cx, "next");
|
|
|
|
Br(after_cx, next_cx.llbb);
|
|
|
|
ret next_cx;
|
2011-07-11 09:47:24 -05:00
|
|
|
}
|
|
|
|
|
2011-07-28 05:01:45 -05:00
|
|
|
// Not alt-related, but similar to the pattern-munging code above
|
2011-09-12 04:27:30 -05:00
|
|
|
fn bind_irrefutable_pat(bcx: @block_ctxt, pat: @ast::pat, val: ValueRef,
|
2011-08-19 17:16:48 -05:00
|
|
|
make_copy: bool) -> @block_ctxt {
|
2011-11-03 04:57:54 -05:00
|
|
|
let ccx = bcx.fcx.lcx.ccx, bcx = bcx;
|
2011-07-28 05:01:45 -05:00
|
|
|
alt pat.node {
|
|
|
|
ast::pat_bind(_) {
|
2011-09-13 05:31:16 -05:00
|
|
|
if make_copy || ccx.copy_map.contains_key(pat.id) {
|
2011-08-02 04:44:00 -05:00
|
|
|
let ty = ty::node_id_to_monotype(ccx.tcx, pat.id);
|
2011-09-02 20:59:22 -05:00
|
|
|
// FIXME: Could constrain pat_bind to make this
|
|
|
|
// check unnecessary.
|
2011-09-12 04:27:30 -05:00
|
|
|
check (type_has_static_size(ccx, ty));
|
2011-09-15 22:32:01 -05:00
|
|
|
check non_ty_var(ccx, ty);
|
2011-08-02 04:44:00 -05:00
|
|
|
let llty = trans::type_of(ccx, pat.span, ty);
|
|
|
|
let alloc = trans::alloca(bcx, llty);
|
2011-11-03 04:57:54 -05:00
|
|
|
bcx = trans::copy_val(bcx, trans::INIT, alloc,
|
|
|
|
trans::load_if_immediate(bcx, val, ty), ty);
|
2011-10-07 04:20:51 -05:00
|
|
|
bcx.fcx.lllocals.insert(pat.id, local_mem(alloc));
|
2011-08-22 10:42:15 -05:00
|
|
|
trans_common::add_clean(bcx, alloc, ty);
|
2011-10-07 04:20:51 -05:00
|
|
|
} else { bcx.fcx.lllocals.insert(pat.id, local_mem(val)); }
|
2011-07-28 05:01:45 -05:00
|
|
|
}
|
|
|
|
ast::pat_tag(_, sub) {
|
2011-08-15 18:38:23 -05:00
|
|
|
if vec::len(sub) == 0u { ret bcx; }
|
2011-08-21 23:44:41 -05:00
|
|
|
let vdefs = ast_util::variant_def_ids(ccx.tcx.def_map.get(pat.id));
|
2011-07-28 05:01:45 -05:00
|
|
|
let args = extract_variant_args(bcx, pat.id, vdefs, val);
|
|
|
|
let i = 0;
|
|
|
|
for argval: ValueRef in args.vals {
|
2011-10-07 04:20:51 -05:00
|
|
|
bcx = bind_irrefutable_pat(bcx, sub[i], argval, make_copy);
|
2011-07-28 05:01:45 -05:00
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ast::pat_rec(fields, _) {
|
|
|
|
let rec_ty = ty::node_id_to_monotype(ccx.tcx, pat.id);
|
|
|
|
let rec_fields =
|
|
|
|
alt ty::struct(ccx.tcx, rec_ty) { ty::ty_rec(fields) { fields } };
|
|
|
|
for f: ast::field_pat in fields {
|
|
|
|
let ix: uint =
|
|
|
|
ty::field_idx(ccx.sess, pat.span, f.ident, rec_fields);
|
2011-09-17 12:18:30 -05:00
|
|
|
// how to get rid of this check?
|
|
|
|
check type_is_tup_like(bcx, rec_ty);
|
2011-08-19 17:16:48 -05:00
|
|
|
let r = trans::GEP_tup_like(bcx, rec_ty, val, [0, ix as int]);
|
2011-10-07 04:20:51 -05:00
|
|
|
bcx = bind_irrefutable_pat(r.bcx, f.pat, r.val, make_copy);
|
2011-07-28 05:01:45 -05:00
|
|
|
}
|
|
|
|
}
|
2011-08-15 06:15:19 -05:00
|
|
|
ast::pat_tup(elems) {
|
|
|
|
let tup_ty = ty::node_id_to_monotype(ccx.tcx, pat.id);
|
|
|
|
let i = 0u;
|
|
|
|
for elem in elems {
|
2011-09-17 12:18:30 -05:00
|
|
|
// how to get rid of this check?
|
|
|
|
check type_is_tup_like(bcx, tup_ty);
|
2011-08-19 17:16:48 -05:00
|
|
|
let r = trans::GEP_tup_like(bcx, tup_ty, val, [0, i as int]);
|
2011-10-07 04:20:51 -05:00
|
|
|
bcx = bind_irrefutable_pat(r.bcx, elem, r.val, make_copy);
|
2011-08-15 06:15:19 -05:00
|
|
|
i += 1u;
|
|
|
|
}
|
|
|
|
}
|
2011-07-28 05:01:45 -05:00
|
|
|
ast::pat_box(inner) {
|
2011-08-30 02:59:30 -05:00
|
|
|
let box = Load(bcx, val);
|
2011-09-02 17:34:58 -05:00
|
|
|
let unboxed =
|
2011-10-26 00:23:28 -05:00
|
|
|
GEPi(bcx, box, [0, back::abi::box_rc_field_body]);
|
2011-10-07 04:20:51 -05:00
|
|
|
bcx = bind_irrefutable_pat(bcx, inner, unboxed, true);
|
2011-07-28 05:01:45 -05:00
|
|
|
}
|
2011-09-23 18:08:30 -05:00
|
|
|
ast::pat_uniq(inner) {
|
|
|
|
let val = Load(bcx, val);
|
2011-10-07 04:20:51 -05:00
|
|
|
bcx = bind_irrefutable_pat(bcx, inner, val, true);
|
2011-09-23 18:08:30 -05:00
|
|
|
}
|
2011-09-28 14:07:33 -05:00
|
|
|
ast::pat_wild. | ast::pat_lit(_) | ast::pat_range(_, _) { }
|
2011-07-28 05:01:45 -05:00
|
|
|
}
|
|
|
|
ret bcx;
|
|
|
|
}
|
|
|
|
|
2011-07-11 09:47:24 -05:00
|
|
|
// Local Variables:
|
2011-09-28 14:07:33 -05:00
|
|
|
// mode: rust
|
2011-07-11 09:47:24 -05:00
|
|
|
// fill-column: 78;
|
|
|
|
// indent-tabs-mode: nil
|
|
|
|
// c-basic-offset: 4
|
|
|
|
// buffer-file-coding-system: utf-8-unix
|
|
|
|
// End:
|