2012-01-14 18:05:07 -06:00
|
|
|
import driver::session::session;
|
2011-07-11 09:47:24 -05:00
|
|
|
import lib::llvm::llvm;
|
2012-02-01 04:04:56 -06:00
|
|
|
import lib::llvm::{ValueRef, BasicBlockRef};
|
2012-01-14 18:05:07 -06:00
|
|
|
import pat_util::*;
|
2012-01-27 06:17:06 -06:00
|
|
|
import build::*;
|
2012-02-17 06:17:40 -06:00
|
|
|
import base::*;
|
2011-07-11 09:47:24 -05:00
|
|
|
import syntax::ast;
|
2011-08-21 23:44:41 -05:00
|
|
|
import syntax::ast_util;
|
2011-12-02 06:42:51 -06:00
|
|
|
import syntax::ast_util::{dummy_sp};
|
2011-07-11 09:47:24 -05:00
|
|
|
import syntax::ast::def_id;
|
|
|
|
import syntax::codemap::span;
|
2012-01-14 18:05:07 -06:00
|
|
|
import syntax::print::pprust::pat_to_str;
|
2012-02-01 20:52:08 -06:00
|
|
|
import back::abi;
|
2012-02-22 09:57:23 -06:00
|
|
|
import resolve::def_map;
|
2012-03-07 18:48:57 -06:00
|
|
|
import std::map::hashmap;
|
2011-07-11 09:47:24 -05:00
|
|
|
|
2012-01-27 06:17:06 -06:00
|
|
|
import common::*;
|
2011-07-14 19:08:22 -05:00
|
|
|
|
2012-01-19 16:34:23 -06:00
|
|
|
// An option identifying a branch (either a literal, a enum variant or a
|
|
|
|
// range)
|
2012-01-19 16:24:03 -06:00
|
|
|
enum opt {
|
2012-01-19 19:56:05 -06:00
|
|
|
lit(@ast::expr),
|
2012-01-30 23:00:57 -06:00
|
|
|
var(/* disr val */int, /* variant dids */{enm: def_id, var: def_id}),
|
2012-01-19 21:29:21 -06:00
|
|
|
range(@ast::expr, @ast::expr)
|
2011-07-11 09:47:24 -05:00
|
|
|
}
|
2012-03-14 12:04:03 -05:00
|
|
|
fn opt_eq(tcx: ty::ctxt, a: opt, b: opt) -> bool {
|
2011-12-02 06:42:51 -06:00
|
|
|
alt (a, b) {
|
2012-03-22 16:56:56 -05:00
|
|
|
(lit(a), lit(b)) { const_eval::compare_lit_exprs(tcx, a, b) == 0 }
|
2011-12-02 06:42:51 -06:00
|
|
|
(range(a1, a2), range(b1, b2)) {
|
2012-03-22 16:56:56 -05:00
|
|
|
const_eval::compare_lit_exprs(tcx, a1, b1) == 0 &&
|
|
|
|
const_eval::compare_lit_exprs(tcx, a2, b2) == 0
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
2011-12-02 06:42:51 -06:00
|
|
|
(var(a, _), var(b, _)) { a == b }
|
|
|
|
_ { false }
|
2011-07-11 09:47:24 -05:00
|
|
|
}
|
|
|
|
}
|
2011-09-28 14:07:33 -05:00
|
|
|
|
2012-01-19 16:24:03 -06:00
|
|
|
enum opt_result {
|
2012-01-19 19:56:05 -06:00
|
|
|
single_result(result),
|
|
|
|
range_result(result, result),
|
2011-09-28 14:07:33 -05:00
|
|
|
}
|
2012-02-17 06:17:40 -06:00
|
|
|
fn trans_opt(bcx: block, o: opt) -> opt_result {
|
2012-03-22 15:44:20 -05:00
|
|
|
let _icx = bcx.insn_ctxt("alt::trans_opt");
|
2012-03-15 08:47:03 -05:00
|
|
|
let ccx = bcx.ccx();
|
|
|
|
let mut 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 {
|
2011-12-02 06:42:51 -06:00
|
|
|
ast::expr_lit(@{node: ast::lit_str(s), _}) {
|
2012-02-21 07:20:18 -06:00
|
|
|
let strty = ty::mk_str(bcx.tcx());
|
2012-02-17 06:17:40 -06:00
|
|
|
let cell = empty_dest_cell();
|
2012-04-16 18:17:51 -05:00
|
|
|
bcx = tvec::trans_estr(bcx, s, ast::vstore_uniq, by_val(cell));
|
2011-10-10 06:32:50 -05:00
|
|
|
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(
|
2012-02-17 06:17:40 -06:00
|
|
|
rslt(bcx, trans_const_expr(ccx, l)));
|
2011-09-27 01:42:27 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-01-10 15:50:40 -06:00
|
|
|
var(disr_val, _) { ret single_result(rslt(bcx, C_int(ccx, disr_val))); }
|
2011-09-28 14:07:33 -05:00
|
|
|
range(l1, l2) {
|
2012-02-17 06:17:40 -06:00
|
|
|
ret range_result(rslt(bcx, trans_const_expr(ccx, l1)),
|
|
|
|
rslt(bcx, trans_const_expr(ccx, l2)));
|
2011-09-28 14:07:33 -05:00
|
|
|
}
|
2011-07-11 09:47:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-22 09:57:23 -06:00
|
|
|
fn variant_opt(tcx: ty::ctxt, pat_id: ast::node_id) -> opt {
|
|
|
|
let vdef = ast_util::variant_def_ids(tcx.def_map.get(pat_id));
|
|
|
|
let variants = ty::enum_variants(tcx, vdef.enm);
|
2012-03-27 08:14:12 -05:00
|
|
|
for vec::each(*variants) {|v|
|
2012-01-10 15:50:40 -06:00
|
|
|
if vdef.var == v.id { ret var(v.disr_val, vdef); }
|
2011-07-11 09:47:24 -05:00
|
|
|
}
|
2012-03-08 16:29:17 -06:00
|
|
|
core::unreachable();
|
2011-07-11 09:47:24 -05:00
|
|
|
}
|
|
|
|
|
2011-08-04 18:20:09 -05:00
|
|
|
type bind_map = [{ident: ast::ident, val: ValueRef}];
|
2012-01-31 19:05:20 -06:00
|
|
|
fn assoc(key: str, list: bind_map) -> option<ValueRef> {
|
2012-03-27 08:14:12 -05:00
|
|
|
for vec::each(list) {|elt|
|
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-11-22 05:44:59 -06:00
|
|
|
data: @{body: BasicBlockRef,
|
2012-01-31 19:05:20 -06:00
|
|
|
guard: option<@ast::expr>,
|
2012-01-14 18:05:07 -06:00
|
|
|
id_map: pat_id_map}};
|
2011-08-04 18:20:09 -05:00
|
|
|
type match = [match_branch];
|
2011-07-11 09:47:24 -05:00
|
|
|
|
2011-12-08 04:56:16 -06:00
|
|
|
fn has_nested_bindings(m: match, col: uint) -> bool {
|
2012-03-27 08:14:12 -05:00
|
|
|
for vec::each(m) {|br|
|
2011-12-08 04:56:16 -06:00
|
|
|
alt br.pats[col].node {
|
2012-01-14 18:05:07 -06:00
|
|
|
ast::pat_ident(_, some(_)) { ret true; }
|
2011-12-08 04:56:16 -06:00
|
|
|
_ {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ret false;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn expand_nested_bindings(m: match, col: uint, val: ValueRef) -> match {
|
2012-03-15 08:47:03 -05:00
|
|
|
let mut result = [];
|
2012-03-27 08:14:12 -05:00
|
|
|
for vec::each(m) {|br|
|
2012-01-14 18:05:07 -06:00
|
|
|
alt br.pats[col].node {
|
|
|
|
ast::pat_ident(name, some(inner)) {
|
2011-12-08 04:56:16 -06:00
|
|
|
let pats = vec::slice(br.pats, 0u, col) + [inner] +
|
2012-02-14 02:10:47 -06:00
|
|
|
vec::slice(br.pats, col + 1u, br.pats.len());
|
2011-12-08 04:56:16 -06:00
|
|
|
result += [@{pats: pats,
|
2012-01-14 18:05:07 -06:00
|
|
|
bound: br.bound + [{ident: path_to_ident(name),
|
|
|
|
val: val}]
|
2011-12-08 04:56:16 -06:00
|
|
|
with *br}];
|
|
|
|
}
|
|
|
|
_ { result += [br]; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result
|
2011-07-11 09:47:24 -05:00
|
|
|
}
|
|
|
|
|
2012-02-22 09:57:23 -06:00
|
|
|
type enter_pat = fn(@ast::pat) -> option<[@ast::pat]>;
|
2011-07-27 07:19:39 -05:00
|
|
|
|
2012-02-22 09:57:23 -06:00
|
|
|
fn enter_match(dm: def_map, m: match, col: uint, val: ValueRef,
|
|
|
|
e: enter_pat) -> match {
|
2012-03-15 08:47:03 -05:00
|
|
|
let mut result = [];
|
2012-03-27 08:14:12 -05:00
|
|
|
for vec::each(m) {|br|
|
2011-08-19 17:16:48 -05:00
|
|
|
alt e(br.pats[col]) {
|
2011-07-27 07:19:39 -05:00
|
|
|
some(sub) {
|
2011-11-22 05:44:59 -06:00
|
|
|
let pats = sub + vec::slice(br.pats, 0u, col) +
|
2012-02-14 02:10:47 -06:00
|
|
|
vec::slice(br.pats, col + 1u, br.pats.len());
|
2012-02-22 09:57:23 -06:00
|
|
|
let self = br.pats[col];
|
|
|
|
let bound = alt self.node {
|
|
|
|
ast::pat_ident(name, none) if !pat_is_variant(dm, self) {
|
|
|
|
br.bound + [{ident: path_to_ident(name), val: val}]
|
|
|
|
}
|
|
|
|
_ { br.bound }
|
|
|
|
};
|
|
|
|
result += [@{pats: pats, bound: bound with *br}];
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
2012-01-19 00:37:22 -06:00
|
|
|
none { }
|
2011-07-11 09:47:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ret result;
|
|
|
|
}
|
|
|
|
|
2012-02-22 09:57:23 -06:00
|
|
|
fn enter_default(dm: def_map, m: match, col: uint, val: ValueRef) -> match {
|
|
|
|
enter_match(dm, m, col, val) {|p|
|
2012-01-14 18:05:07 -06:00
|
|
|
alt p.node {
|
2012-02-22 09:57:23 -06:00
|
|
|
ast::pat_wild | ast::pat_rec(_, _) | ast::pat_tup(_) { some([]) }
|
|
|
|
ast::pat_ident(_, none) if !pat_is_variant(dm, p) {
|
|
|
|
some([])
|
|
|
|
}
|
|
|
|
_ { none }
|
2012-01-14 18:05:07 -06:00
|
|
|
}
|
2011-12-08 04:56:16 -06:00
|
|
|
}
|
2011-07-13 04:33:46 -05:00
|
|
|
}
|
|
|
|
|
2012-02-22 09:57:23 -06:00
|
|
|
fn enter_opt(tcx: ty::ctxt, m: match, opt: opt, col: uint,
|
|
|
|
variant_size: uint, val: ValueRef) -> match {
|
2011-08-12 10:57:21 -05:00
|
|
|
let dummy = @{id: 0, node: ast::pat_wild, span: dummy_sp()};
|
2012-02-22 09:57:23 -06:00
|
|
|
enter_match(tcx.def_map, m, col, val) {|p|
|
2011-07-27 07:19:39 -05:00
|
|
|
alt p.node {
|
2012-02-22 09:57:23 -06:00
|
|
|
ast::pat_enum(_, subpats) {
|
2012-03-14 12:04:03 -05:00
|
|
|
if opt_eq(tcx, variant_opt(tcx, p.id), opt) { some(subpats) }
|
2012-02-22 09:57:23 -06:00
|
|
|
else { none }
|
|
|
|
}
|
|
|
|
ast::pat_ident(_, none) if pat_is_variant(tcx.def_map, p) {
|
2012-03-14 12:04:03 -05:00
|
|
|
if opt_eq(tcx, variant_opt(tcx, p.id), opt) { some([]) }
|
2012-02-22 09:57:23 -06:00
|
|
|
else { none }
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
|
|
|
ast::pat_lit(l) {
|
2012-03-14 12:04:03 -05:00
|
|
|
if opt_eq(tcx, 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) {
|
2012-03-14 12:04:03 -05:00
|
|
|
if opt_eq(tcx, range(l1, l2), opt) { some([]) } else { none }
|
2011-09-28 14:07:33 -05:00
|
|
|
}
|
2012-03-12 17:52:30 -05:00
|
|
|
_ { some(vec::from_elem(variant_size, dummy)) }
|
2011-07-11 09:47:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-22 09:57:23 -06:00
|
|
|
fn enter_rec(dm: def_map, m: match, col: uint, fields: [ast::ident],
|
|
|
|
val: ValueRef) -> match {
|
2011-08-15 15:33:12 -05:00
|
|
|
let dummy = @{id: 0, node: ast::pat_wild, span: dummy_sp()};
|
2012-02-22 09:57:23 -06:00
|
|
|
enter_match(dm, m, col, val) {|p|
|
2011-07-27 07:19:39 -05:00
|
|
|
alt p.node {
|
|
|
|
ast::pat_rec(fpats, _) {
|
2012-03-15 08:47:03 -05:00
|
|
|
let mut pats = [];
|
2012-03-27 08:14:12 -05:00
|
|
|
for vec::each(fields) {|fname|
|
2012-03-15 08:47:03 -05:00
|
|
|
let mut pat = dummy;
|
2012-03-27 08:14:12 -05:00
|
|
|
for vec::each(fpats) {|fpat|
|
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
|
|
|
}
|
2012-02-22 09:57:23 -06:00
|
|
|
some(pats)
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
2012-03-12 17:52:30 -05:00
|
|
|
_ { some(vec::from_elem(fields.len(), dummy)) }
|
2011-07-11 09:47:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-22 09:57:23 -06:00
|
|
|
fn enter_tup(dm: def_map, 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()};
|
2012-02-22 09:57:23 -06:00
|
|
|
enter_match(dm, m, col, val) {|p|
|
2011-08-15 06:15:19 -05:00
|
|
|
alt p.node {
|
2012-02-22 09:57:23 -06:00
|
|
|
ast::pat_tup(elts) { some(elts) }
|
2012-03-12 17:52:30 -05:00
|
|
|
_ { some(vec::from_elem(n_elts, dummy)) }
|
2011-08-15 06:15:19 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-22 09:57:23 -06:00
|
|
|
fn enter_box(dm: def_map, 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()};
|
2012-02-22 09:57:23 -06:00
|
|
|
enter_match(dm, m, col, val) {|p|
|
2011-07-27 07:19:39 -05:00
|
|
|
alt p.node {
|
2012-02-22 09:57:23 -06:00
|
|
|
ast::pat_box(sub) { some([sub]) }
|
|
|
|
_ { some([dummy]) }
|
2011-07-13 04:11:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-22 09:57:23 -06:00
|
|
|
fn enter_uniq(dm: def_map, m: match, col: uint, val: ValueRef) -> match {
|
2011-09-23 13:15:17 -05:00
|
|
|
let dummy = @{id: 0, node: ast::pat_wild, span: dummy_sp()};
|
2012-02-22 09:57:23 -06:00
|
|
|
enter_match(dm, m, col, val) {|p|
|
2011-09-23 13:15:17 -05:00
|
|
|
alt p.node {
|
2012-02-22 09:57:23 -06:00
|
|
|
ast::pat_uniq(sub) { some([sub]) }
|
|
|
|
_ { some([dummy]) }
|
2011-09-23 13:15:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-14 19:31:16 -05:00
|
|
|
fn get_options(ccx: @crate_ctxt, m: match, col: uint) -> [opt] {
|
2012-03-14 12:04:03 -05:00
|
|
|
fn add_to_set(tcx: ty::ctxt, &set: [opt], val: opt) {
|
2012-03-27 08:14:12 -05:00
|
|
|
for vec::each(set) {|l| if opt_eq(tcx, l, val) { ret; } }
|
2011-08-19 17:16:48 -05:00
|
|
|
set += [val];
|
2011-07-11 09:47:24 -05:00
|
|
|
}
|
|
|
|
|
2012-03-15 08:47:03 -05:00
|
|
|
let mut found = [];
|
2012-03-27 08:14:12 -05:00
|
|
|
for vec::each(m) {|br|
|
2012-02-22 09:57:23 -06:00
|
|
|
let cur = br.pats[col];
|
|
|
|
if pat_is_variant(ccx.tcx.def_map, cur) {
|
2012-03-14 12:04:03 -05:00
|
|
|
add_to_set(ccx.tcx, found, variant_opt(ccx.tcx, br.pats[col].id));
|
2012-02-22 09:57:23 -06:00
|
|
|
} else {
|
|
|
|
alt cur.node {
|
2012-03-14 12:04:03 -05:00
|
|
|
ast::pat_lit(l) { add_to_set(ccx.tcx, found, lit(l)); }
|
2012-02-22 09:57:23 -06:00
|
|
|
ast::pat_range(l1, l2) {
|
2012-03-14 12:04:03 -05:00
|
|
|
add_to_set(ccx.tcx, found, range(l1, l2));
|
2012-02-22 09:57:23 -06:00
|
|
|
}
|
|
|
|
_ {}
|
|
|
|
}
|
2011-07-11 09:47:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ret found;
|
|
|
|
}
|
|
|
|
|
2012-02-17 06:17:40 -06:00
|
|
|
fn extract_variant_args(bcx: block, pat_id: ast::node_id,
|
2012-01-30 23:00:57 -06:00
|
|
|
vdefs: {enm: def_id, var: def_id}, val: ValueRef) ->
|
2012-02-17 06:17:40 -06:00
|
|
|
{vals: [ValueRef], bcx: block} {
|
2012-03-22 15:44:20 -05:00
|
|
|
let _icx = bcx.insn_ctxt("alt::extract_variant_args");
|
2012-03-15 08:47:03 -05:00
|
|
|
let ccx = bcx.fcx.ccx;
|
2012-03-07 02:19:29 -06:00
|
|
|
let enum_ty_substs = alt check ty::get(node_id_type(bcx, pat_id)).struct {
|
2012-04-18 23:26:25 -05:00
|
|
|
ty::ty_enum(id, substs) { assert id == vdefs.enm; substs.tps }
|
2012-03-07 02:19:29 -06:00
|
|
|
};
|
2012-03-15 08:47:03 -05:00
|
|
|
let mut blobptr = val;
|
2012-01-30 23:00:57 -06:00
|
|
|
let variants = ty::enum_variants(ccx.tcx, vdefs.enm);
|
2012-02-14 02:10:47 -06:00
|
|
|
let size = ty::enum_variant_with_id(ccx.tcx, vdefs.enm,
|
|
|
|
vdefs.var).args.len();
|
|
|
|
if size > 0u && (*variants).len() != 1u {
|
2012-01-25 07:34:31 -06:00
|
|
|
let enumptr =
|
2012-01-27 06:17:06 -06:00
|
|
|
PointerCast(bcx, val, T_opaque_enum_ptr(ccx));
|
2012-01-25 07:34:31 -06:00
|
|
|
blobptr = GEPi(bcx, enumptr, [0, 1]);
|
2011-07-11 09:47:24 -05:00
|
|
|
}
|
2012-01-30 23:00:57 -06:00
|
|
|
let vdefs_tg = vdefs.enm;
|
2011-09-01 19:25:06 -05:00
|
|
|
let vdefs_var = vdefs.var;
|
2012-04-18 23:26:25 -05:00
|
|
|
let args = vec::from_fn(size) { |i|
|
|
|
|
GEP_enum(bcx, blobptr, vdefs_tg, vdefs_var,
|
|
|
|
enum_ty_substs, i)
|
|
|
|
};
|
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] {
|
2012-03-15 08:47:03 -05:00
|
|
|
let mut fields = [];
|
2012-03-27 08:14:12 -05:00
|
|
|
for vec::each(m) {|br|
|
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, _) {
|
2012-03-27 08:14:12 -05:00
|
|
|
for vec::each(fs) {|f|
|
2011-12-16 08:27:50 -06:00
|
|
|
if !vec::any(fields, bind str::eq(f.ident, _)) {
|
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 {
|
2012-03-27 08:14:12 -05:00
|
|
|
for vec::each(m) {|br|
|
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 {
|
2012-03-27 08:14:12 -05:00
|
|
|
for vec::each(m) {|br|
|
2011-09-23 13:15:17 -05:00
|
|
|
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 {
|
2012-03-27 08:14:12 -05:00
|
|
|
for vec::each(m) {|br|
|
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-12-08 04:56:16 -06:00
|
|
|
fn score(p: @ast::pat) -> uint {
|
|
|
|
alt p.node {
|
2012-01-25 07:34:31 -06:00
|
|
|
ast::pat_lit(_) | ast::pat_enum(_, _) | ast::pat_range(_, _) { 1u }
|
2012-01-14 18:05:07 -06:00
|
|
|
ast::pat_ident(_, some(p)) { score(p) }
|
2011-12-08 04:56:16 -06:00
|
|
|
_ { 0u }
|
|
|
|
}
|
|
|
|
}
|
2012-03-12 17:52:30 -05:00
|
|
|
let scores = vec::to_mut(vec::from_elem(m[0].pats.len(), 0u));
|
2012-03-27 08:14:12 -05:00
|
|
|
for vec::each(m) {|br|
|
2012-03-15 08:47:03 -05:00
|
|
|
let mut i = 0u;
|
2012-03-27 08:14:12 -05:00
|
|
|
for vec::each(br.pats) {|p| scores[i] += score(p); i += 1u; }
|
2011-08-02 05:57:27 -05:00
|
|
|
}
|
2012-03-15 08:47:03 -05:00
|
|
|
let mut max_score = 0u;
|
|
|
|
let mut best_col = 0u;
|
|
|
|
let mut i = 0u;
|
2012-03-27 08:14:12 -05:00
|
|
|
for vec::each(scores) {|score|
|
2011-08-02 05:57:27 -05:00
|
|
|
// 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
|
|
|
|
2012-03-13 07:42:29 -05:00
|
|
|
fn compile_submatch(bcx: block, m: match, vals: [ValueRef],
|
|
|
|
chk: option<mk_fail>, &exits: [exit_node]) {
|
2012-03-22 15:44:20 -05:00
|
|
|
let _icx = bcx.insn_ctxt("alt::compile_submatch");
|
2012-03-15 08:47:03 -05:00
|
|
|
let mut bcx = bcx;
|
|
|
|
let tcx = bcx.tcx(), dm = tcx.def_map;
|
2012-03-13 07:42:29 -05:00
|
|
|
if m.len() == 0u { Br(bcx, option::get(chk)()); ret; }
|
2012-02-14 02:10:47 -06:00
|
|
|
if m[0].pats.len() == 0u {
|
2011-08-22 07:38:48 -05:00
|
|
|
let data = m[0].data;
|
|
|
|
alt data.guard {
|
|
|
|
some(e) {
|
2012-02-17 06:17:40 -06: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|
|
2012-02-17 06:17:40 -06:00
|
|
|
let loc = local_mem(option::get(assoc(key, m[0].bound)));
|
|
|
|
bcx.fcx.lllocals.insert(val, loc);
|
|
|
|
};
|
|
|
|
let {bcx: guard_cx, val} = with_scope_result(bcx, "guard") {|bcx|
|
|
|
|
trans_temp_expr(bcx, e)
|
|
|
|
};
|
|
|
|
bcx = with_cond(guard_cx, Not(guard_cx, val)) {|bcx|
|
2012-03-13 07:42:29 -05:00
|
|
|
compile_submatch(bcx, vec::tail(m), vals, chk, exits);
|
2012-02-17 06:17:40 -06:00
|
|
|
bcx
|
2011-10-21 05:21:27 -05:00
|
|
|
};
|
2011-08-22 07:38:48 -05:00
|
|
|
}
|
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];
|
2012-01-29 20:33:08 -06:00
|
|
|
let m = if has_nested_bindings(m, col) {
|
|
|
|
expand_nested_bindings(m, col, val)
|
2012-03-13 07:42:29 -05:00
|
|
|
} else { m };
|
2011-12-08 04:56:16 -06:00
|
|
|
|
2012-03-13 07:42:29 -05:00
|
|
|
let vals_left = vec::slice(vals, 0u, col) +
|
|
|
|
vec::slice(vals, col + 1u, vals.len());
|
2012-02-03 02:53:37 -06:00
|
|
|
let ccx = bcx.fcx.ccx;
|
2012-03-15 08:47:03 -05:00
|
|
|
let mut pat_id = 0;
|
2012-03-27 08:14:12 -05:00
|
|
|
for vec::each(m) {|br|
|
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
|
2012-02-14 02:10:47 -06:00
|
|
|
if rec_fields.len() > 0u {
|
2012-03-12 03:26:54 -05:00
|
|
|
let fields = ty::get_fields(node_id_type(bcx, pat_id));
|
2012-03-15 08:47:03 -05:00
|
|
|
let mut rec_vals = [];
|
2012-03-27 08:14:12 -05:00
|
|
|
for vec::each(rec_fields) {|field_name|
|
2011-12-18 12:30:40 -06:00
|
|
|
let ix = option::get(ty::field_idx(field_name, fields));
|
2012-03-12 03:26:54 -05:00
|
|
|
rec_vals += [GEPi(bcx, val, [0, ix as int])];
|
2011-07-11 09:47:24 -05:00
|
|
|
}
|
2012-02-22 09:57:23 -06:00
|
|
|
compile_submatch(bcx, enter_rec(dm, m, col, rec_fields, val),
|
2012-03-13 07:42:29 -05:00
|
|
|
rec_vals + vals_left, chk, exits);
|
2011-07-11 09:47:24 -05:00
|
|
|
ret;
|
|
|
|
}
|
|
|
|
|
2011-08-15 06:15:19 -05:00
|
|
|
if any_tup_pat(m, col) {
|
2012-02-09 07:33:00 -06:00
|
|
|
let tup_ty = node_id_type(bcx, pat_id);
|
2012-02-03 08:15:28 -06:00
|
|
|
let n_tup_elts = alt ty::get(tup_ty).struct {
|
2012-02-14 02:10:47 -06:00
|
|
|
ty::ty_tup(elts) { elts.len() }
|
2012-03-05 18:27:27 -06:00
|
|
|
_ { ccx.sess.bug("non-tuple type in tuple pattern"); }
|
2012-02-03 08:15:28 -06:00
|
|
|
};
|
2012-03-15 08:47:03 -05:00
|
|
|
let mut tup_vals = [], i = 0u;
|
2011-08-15 06:15:19 -05:00
|
|
|
while i < n_tup_elts {
|
2012-03-12 03:26:54 -05:00
|
|
|
tup_vals += [GEPi(bcx, val, [0, i as int])];
|
2011-08-15 06:15:19 -05:00
|
|
|
i += 1u;
|
|
|
|
}
|
2012-02-22 09:57:23 -06:00
|
|
|
compile_submatch(bcx, enter_tup(dm, m, col, val, n_tup_elts),
|
2012-03-13 07:42:29 -05:00
|
|
|
tup_vals + vals_left, chk, exits);
|
2011-08-15 06:15:19 -05:00
|
|
|
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);
|
2012-02-01 20:52:08 -06:00
|
|
|
let unboxed = GEPi(bcx, box, [0, abi::box_field_body]);
|
2012-02-22 09:57:23 -06:00
|
|
|
compile_submatch(bcx, enter_box(dm, m, col, val), [unboxed]
|
2012-03-13 07:42:29 -05:00
|
|
|
+ vals_left, chk, 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);
|
2012-02-22 09:57:23 -06:00
|
|
|
compile_submatch(bcx, enter_uniq(dm, m, col, val),
|
2012-03-13 07:42:29 -05:00
|
|
|
[unboxed] + vals_left, chk, exits);
|
2011-09-23 13:15:17 -05:00
|
|
|
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);
|
2012-01-19 19:56:05 -06:00
|
|
|
enum branch_kind { no_branch, single, switch, compare, }
|
2012-03-15 08:47:03 -05:00
|
|
|
let mut kind = no_branch;
|
|
|
|
let mut test_val = val;
|
2012-02-14 02:10:47 -06:00
|
|
|
if opts.len() > 0u {
|
2011-08-19 17:16:48 -05:00
|
|
|
alt opts[0] {
|
2011-07-27 07:19:39 -05:00
|
|
|
var(_, vdef) {
|
2012-02-22 09:57:23 -06:00
|
|
|
if (*ty::enum_variants(tcx, vdef.enm)).len() == 1u {
|
2011-07-27 07:19:39 -05:00
|
|
|
kind = single;
|
|
|
|
} else {
|
2012-01-25 07:34:31 -06:00
|
|
|
let enumptr =
|
2012-01-27 06:17:06 -06:00
|
|
|
PointerCast(bcx, val, T_opaque_enum_ptr(ccx));
|
2012-01-25 07:34:31 -06:00
|
|
|
let discrimptr = GEPi(bcx, enumptr, [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-12-02 06:42:51 -06:00
|
|
|
test_val = Load(bcx, val);
|
2012-02-09 07:33:00 -06:00
|
|
|
let pty = node_id_type(bcx, pat_id);
|
2012-02-03 08:15:28 -06:00
|
|
|
kind = if ty::type_is_integral(pty) { switch }
|
|
|
|
else { compare };
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
2011-09-28 14:07:33 -05:00
|
|
|
range(_, _) {
|
|
|
|
test_val = Load(bcx, val);
|
|
|
|
kind = compare;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-03-27 08:14:12 -05:00
|
|
|
for vec::each(opts) {|o|
|
2011-09-28 14:07:33 -05:00
|
|
|
alt o {
|
|
|
|
range(_, _) { kind = compare; break; }
|
|
|
|
_ { }
|
2011-07-11 09:47:24 -05:00
|
|
|
}
|
|
|
|
}
|
2012-02-21 07:25:53 -06:00
|
|
|
let else_cx = alt kind {
|
|
|
|
no_branch | single { bcx }
|
|
|
|
_ { sub_block(bcx, "match_else") }
|
|
|
|
};
|
|
|
|
let sw = if kind == switch {
|
|
|
|
Switch(bcx, test_val, else_cx.llbb, opts.len())
|
|
|
|
} else { C_int(ccx, 0) }; // Placeholder for when not using a switch
|
2011-07-27 07:19:39 -05:00
|
|
|
|
2012-03-13 07:42:29 -05:00
|
|
|
let defaults = enter_default(dm, m, col, val);
|
|
|
|
let exhaustive = option::is_none(chk) && defaults.len() == 0u;
|
2012-03-15 08:47:03 -05:00
|
|
|
let len = opts.len();
|
|
|
|
let mut i = 0u;
|
2012-03-13 07:42:29 -05:00
|
|
|
// Compile subtrees for each option
|
2012-03-27 08:14:12 -05:00
|
|
|
for vec::each(opts) {|opt|
|
2012-03-13 07:42:29 -05:00
|
|
|
i += 1u;
|
2012-03-15 08:47:03 -05:00
|
|
|
let mut opt_cx = else_cx;
|
2012-03-13 07:42:29 -05:00
|
|
|
if !exhaustive || i < len {
|
|
|
|
opt_cx = sub_block(bcx, "match_case");
|
|
|
|
alt kind {
|
|
|
|
single { Br(bcx, opt_cx.llbb); }
|
|
|
|
switch {
|
|
|
|
alt check trans_opt(bcx, opt) {
|
|
|
|
single_result(r) {
|
|
|
|
llvm::LLVMAddCase(sw, r.val, opt_cx.llbb);
|
|
|
|
bcx = r.bcx;
|
2012-02-17 06:17:40 -06:00
|
|
|
}
|
|
|
|
}
|
2012-03-13 07:42:29 -05:00
|
|
|
}
|
|
|
|
compare {
|
|
|
|
let t = node_id_type(bcx, pat_id);
|
|
|
|
let {bcx: after_cx, val: matches} =
|
|
|
|
with_scope_result(bcx, "compare_scope") {|bcx|
|
|
|
|
alt trans_opt(bcx, opt) {
|
|
|
|
single_result({bcx, val}) {
|
|
|
|
trans_compare(bcx, ast::eq, test_val, t, val, t)
|
|
|
|
}
|
|
|
|
range_result({val: vbegin, _}, {bcx, val: vend}) {
|
|
|
|
let {bcx, val: ge} = trans_compare(
|
|
|
|
bcx, ast::ge, test_val, t, vbegin, t);
|
|
|
|
let {bcx, val: le} = trans_compare(
|
|
|
|
bcx, ast::le, test_val, t, vend, t);
|
|
|
|
{bcx: bcx, val: And(bcx, ge, le)}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
bcx = sub_block(after_cx, "compare_next");
|
|
|
|
CondBr(after_cx, matches, opt_cx.llbb, bcx.llbb);
|
|
|
|
}
|
|
|
|
_ { }
|
|
|
|
}
|
|
|
|
} else if kind == compare { Br(bcx, else_cx.llbb); }
|
2012-03-15 08:47:03 -05:00
|
|
|
let mut size = 0u;
|
|
|
|
let mut 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);
|
2012-02-14 02:10:47 -06:00
|
|
|
size = args.vals.len();
|
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
|
|
|
}
|
2012-02-22 09:57:23 -06:00
|
|
|
compile_submatch(opt_cx, enter_opt(tcx, m, opt, col, size, val),
|
2012-03-13 07:42:29 -05:00
|
|
|
unpacked + vals_left, chk, exits);
|
2011-07-11 09:47:24 -05:00
|
|
|
}
|
|
|
|
|
2012-03-13 07:42:29 -05:00
|
|
|
// Compile the fall-through case, if any
|
|
|
|
if !exhaustive {
|
|
|
|
if kind == compare { Br(bcx, else_cx.llbb); }
|
|
|
|
if kind != single {
|
|
|
|
compile_submatch(else_cx, defaults, vals_left, chk, exits);
|
|
|
|
}
|
2011-07-11 09:47:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-19 10:07:51 -05:00
|
|
|
// Returns false for unreachable blocks
|
2012-02-17 06:17:40 -06:00
|
|
|
fn make_phi_bindings(bcx: block, map: [exit_node],
|
2012-01-14 18:05:07 -06:00
|
|
|
ids: pat_util::pat_id_map) -> bool {
|
2012-03-22 15:44:20 -05:00
|
|
|
let _icx = bcx.insn_ctxt("alt::make_phi_bindings");
|
2011-07-27 07:19:39 -05:00
|
|
|
let our_block = bcx.llbb as uint;
|
2012-03-15 08:47:03 -05:00
|
|
|
let mut success = true, bcx = bcx;
|
2011-10-21 05:21:27 -05:00
|
|
|
ids.items {|name, node_id|
|
2012-03-15 08:47:03 -05:00
|
|
|
let mut llbbs = [];
|
|
|
|
let mut vals = [];
|
2012-03-27 08:14:12 -05:00
|
|
|
for vec::each(map) {|ex|
|
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]; }
|
2012-01-19 00:37:22 -06:00
|
|
|
none { }
|
2011-07-19 10:07:51 -05:00
|
|
|
}
|
2011-07-11 09:47:24 -05:00
|
|
|
}
|
|
|
|
}
|
2012-02-14 02:10:47 -06:00
|
|
|
if vals.len() > 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|
|
2012-02-14 17:21:53 -06:00
|
|
|
if bcx.ccx().maps.copy_map.contains_key(node_id) {
|
2012-01-30 23:00:57 -06:00
|
|
|
let local = alt bcx.fcx.lllocals.find(node_id) {
|
|
|
|
some(local_mem(x)) { x }
|
2012-03-05 18:27:27 -06:00
|
|
|
_ { bcx.tcx().sess.bug("someone \
|
2012-01-30 23:00:57 -06:00
|
|
|
forgot to document an invariant in \
|
|
|
|
make_phi_bindings"); }
|
2011-10-07 04:20:51 -05:00
|
|
|
};
|
2012-02-09 07:33:00 -06:00
|
|
|
let e_ty = node_id_type(bcx, node_id);
|
2012-03-23 08:45:47 -05:00
|
|
|
let alloc = alloc_ty(bcx, e_ty);
|
|
|
|
bcx = copy_val(bcx, INIT, alloc,
|
|
|
|
load_if_immediate(bcx, local, e_ty), e_ty);
|
2011-09-13 05:14:30 -05:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2012-02-17 06:17:40 -06:00
|
|
|
fn trans_alt(bcx: block, expr: @ast::expr, arms: [ast::arm],
|
2012-03-13 07:42:29 -05:00
|
|
|
mode: ast::alt_mode, dest: dest) -> block {
|
2012-03-22 15:44:20 -05:00
|
|
|
let _icx = bcx.insn_ctxt("alt::trans_alt");
|
2012-03-13 07:42:29 -05:00
|
|
|
with_scope(bcx, "alt") {|bcx|
|
|
|
|
trans_alt_inner(bcx, expr, arms, mode, dest)
|
|
|
|
}
|
2012-02-17 06:17:40 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn trans_alt_inner(scope_cx: block, expr: @ast::expr, arms: [ast::arm],
|
2012-03-13 07:42:29 -05:00
|
|
|
mode: ast::alt_mode, dest: dest) -> block {
|
2012-03-22 15:44:20 -05:00
|
|
|
let _icx = scope_cx.insn_ctxt("alt::trans_alt_inner");
|
2012-02-21 07:20:18 -06:00
|
|
|
let bcx = scope_cx, tcx = bcx.tcx();
|
2012-03-15 08:47:03 -05:00
|
|
|
let mut bodies = [], match = [];
|
2012-02-17 06:17:40 -06:00
|
|
|
|
|
|
|
let {bcx, val, _} = trans_temp_expr(bcx, expr);
|
|
|
|
if bcx.unreachable { ret bcx; }
|
|
|
|
|
2012-03-27 08:14:12 -05:00
|
|
|
for vec::each(arms) {|a|
|
2012-02-17 06:17:40 -06:00
|
|
|
let body = scope_block(bcx, "case_body");
|
|
|
|
body.block_span = some(a.body.span);
|
2012-02-22 09:57:23 -06:00
|
|
|
let id_map = pat_util::pat_id_map(tcx.def_map, a.pats[0]);
|
2011-08-19 17:16:48 -05:00
|
|
|
bodies += [body];
|
2012-03-27 08:14:12 -05:00
|
|
|
for vec::each(a.pats) {|p|
|
2012-02-17 06:17:40 -06:00
|
|
|
match += [@{pats: [p],
|
|
|
|
bound: [],
|
|
|
|
data: @{body: body.llbb, guard: a.guard,
|
|
|
|
id_map: id_map}}];
|
2011-07-11 09:47:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-13 07:42:29 -05:00
|
|
|
let mk_fail = alt mode {
|
|
|
|
ast::alt_check {
|
|
|
|
// Cached fail-on-fallthrough block
|
2012-03-26 20:35:18 -05:00
|
|
|
let fail_cx = @mut none;
|
2012-03-13 07:42:29 -05:00
|
|
|
fn mk_fail(bcx: block, sp: span,
|
2012-03-26 20:35:18 -05:00
|
|
|
done: @mut option<BasicBlockRef>) -> BasicBlockRef {
|
2012-03-13 07:42:29 -05:00
|
|
|
alt *done { some(bb) { ret bb; } _ { } }
|
|
|
|
let fail_cx = sub_block(bcx, "case_fallthrough");
|
|
|
|
trans_fail(fail_cx, some(sp), "non-exhaustive match failure");;
|
|
|
|
*done = some(fail_cx.llbb);
|
|
|
|
ret fail_cx.llbb;
|
|
|
|
}
|
|
|
|
some(bind mk_fail(scope_cx, expr.span, fail_cx))
|
|
|
|
}
|
|
|
|
ast::alt_exhaustive { none }
|
|
|
|
};
|
2012-03-15 08:47:03 -05:00
|
|
|
let mut exit_map = [];
|
2012-02-17 06:17:40 -06:00
|
|
|
let t = node_id_type(bcx, expr.id);
|
2012-03-23 08:45:47 -05:00
|
|
|
let spilled = spill_if_immediate(bcx, val, t);
|
2012-03-13 07:42:29 -05:00
|
|
|
compile_submatch(bcx, match, [spilled], mk_fail, exit_map);
|
2011-07-11 09:47:24 -05:00
|
|
|
|
2012-03-15 08:47:03 -05:00
|
|
|
let mut arm_cxs = [], arm_dests = [], i = 0u;
|
2012-03-27 08:14:12 -05:00
|
|
|
for vec::each(arms) {|a|
|
2011-08-19 17:16:48 -05:00
|
|
|
let body_cx = bodies[i];
|
2012-02-22 09:57:23 -06:00
|
|
|
let id_map = pat_util::pat_id_map(tcx.def_map, a.pats[0]);
|
|
|
|
if make_phi_bindings(body_cx, exit_map, id_map) {
|
2012-02-17 06:17:40 -06:00
|
|
|
let arm_dest = dup_for_join(dest);
|
2011-09-23 14:13:50 -05:00
|
|
|
arm_dests += [arm_dest];
|
2012-03-15 08:47:03 -05:00
|
|
|
let mut arm_cx = trans_block(body_cx, a.body, arm_dest);
|
2012-02-17 06:17:40 -06:00
|
|
|
arm_cx = trans_block_cleanups(arm_cx, body_cx);
|
2012-02-15 06:57:29 -06:00
|
|
|
arm_cxs += [arm_cx];
|
2011-07-19 10:07:51 -05:00
|
|
|
}
|
2011-07-11 09:47:24 -05:00
|
|
|
i += 1u;
|
|
|
|
}
|
2012-02-17 06:17:40 -06:00
|
|
|
join_returns(scope_cx, arm_cxs, arm_dests, dest)
|
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
|
2012-02-17 06:17:40 -06:00
|
|
|
fn bind_irrefutable_pat(bcx: block, pat: @ast::pat, val: ValueRef,
|
|
|
|
make_copy: bool) -> block {
|
2012-03-22 15:44:20 -05:00
|
|
|
let _icx = bcx.insn_ctxt("alt::bind_irrefutable_pat");
|
2012-03-15 08:47:03 -05:00
|
|
|
let ccx = bcx.fcx.ccx;
|
|
|
|
let mut bcx = bcx;
|
2012-01-14 18:05:07 -06:00
|
|
|
|
|
|
|
// Necessary since bind_irrefutable_pat is called outside trans_alt
|
2012-02-22 09:57:23 -06:00
|
|
|
alt pat.node {
|
2012-01-14 18:05:07 -06:00
|
|
|
ast::pat_ident(_,inner) {
|
2012-02-22 09:57:23 -06:00
|
|
|
if pat_is_variant(bcx.tcx().def_map, pat) { ret bcx; }
|
2012-02-14 17:21:53 -06:00
|
|
|
if make_copy || ccx.maps.copy_map.contains_key(pat.id) {
|
2012-02-02 05:37:17 -06:00
|
|
|
let ty = node_id_type(bcx, pat.id);
|
2012-02-21 08:22:55 -06:00
|
|
|
let llty = type_of::type_of(ccx, ty);
|
2012-02-17 06:17:40 -06:00
|
|
|
let alloc = alloca(bcx, llty);
|
|
|
|
bcx = copy_val(bcx, INIT, alloc,
|
|
|
|
load_if_immediate(bcx, val, ty), ty);
|
2011-10-07 04:20:51 -05:00
|
|
|
bcx.fcx.lllocals.insert(pat.id, local_mem(alloc));
|
2012-01-27 06:17:06 -06:00
|
|
|
add_clean(bcx, alloc, ty);
|
2011-10-07 04:20:51 -05:00
|
|
|
} else { bcx.fcx.lllocals.insert(pat.id, local_mem(val)); }
|
2011-12-08 04:56:16 -06:00
|
|
|
alt inner {
|
|
|
|
some(pat) { bcx = bind_irrefutable_pat(bcx, pat, val, true); }
|
|
|
|
_ {}
|
|
|
|
}
|
2011-07-28 05:01:45 -05:00
|
|
|
}
|
2012-01-25 07:34:31 -06:00
|
|
|
ast::pat_enum(_, sub) {
|
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);
|
2012-03-15 08:47:03 -05:00
|
|
|
let mut i = 0;
|
2012-03-27 08:14:12 -05:00
|
|
|
for vec::each(args.vals) {|argval|
|
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, _) {
|
2012-03-12 03:26:54 -05:00
|
|
|
let rec_fields = ty::get_fields(node_id_type(bcx, pat.id));
|
2012-03-27 08:14:12 -05:00
|
|
|
for vec::each(fields) {|f|
|
2011-12-18 12:30:40 -06:00
|
|
|
let ix = option::get(ty::field_idx(f.ident, rec_fields));
|
2011-09-17 12:18:30 -05:00
|
|
|
// how to get rid of this check?
|
2012-03-12 03:26:54 -05:00
|
|
|
let fldptr = GEPi(bcx, val, [0, ix as int]);
|
|
|
|
bcx = bind_irrefutable_pat(bcx, f.pat, fldptr, make_copy);
|
2011-07-28 05:01:45 -05:00
|
|
|
}
|
|
|
|
}
|
2011-08-15 06:15:19 -05:00
|
|
|
ast::pat_tup(elems) {
|
2012-03-15 08:47:03 -05:00
|
|
|
let mut i = 0u;
|
2012-03-27 08:14:12 -05:00
|
|
|
for vec::each(elems) {|elem|
|
2012-03-12 03:26:54 -05:00
|
|
|
let fldptr = GEPi(bcx, val, [0, i as int]);
|
|
|
|
bcx = bind_irrefutable_pat(bcx, elem, fldptr, 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 =
|
2012-02-01 20:52:08 -06:00
|
|
|
GEPi(bcx, box, [0, abi::box_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
|
|
|
}
|
2012-01-19 00:37:22 -06: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:
|