2012-09-04 13:54:36 -05:00
|
|
|
use driver::session::session;
|
|
|
|
use lib::llvm::llvm;
|
|
|
|
use lib::llvm::{ValueRef, BasicBlockRef};
|
|
|
|
use pat_util::*;
|
|
|
|
use build::*;
|
|
|
|
use base::*;
|
|
|
|
use syntax::ast;
|
|
|
|
use syntax::ast_util;
|
|
|
|
use syntax::ast_util::{dummy_sp, path_to_ident};
|
|
|
|
use syntax::ast::def_id;
|
|
|
|
use syntax::codemap::span;
|
|
|
|
use syntax::print::pprust::pat_to_str;
|
|
|
|
use middle::resolve::DefMap;
|
|
|
|
use back::abi;
|
|
|
|
use std::map::hashmap;
|
|
|
|
use dvec::DVec;
|
2012-08-28 17:54:45 -05:00
|
|
|
use datum::*;
|
2012-09-04 13:54:36 -05:00
|
|
|
use common::*;
|
2012-08-28 17:54:45 -05:00
|
|
|
use expr::Dest;
|
|
|
|
|
|
|
|
fn macros() { include!("macros.rs"); } // FIXME(#3114): Macro import/export.
|
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 {
|
2012-08-06 14:34:08 -05:00
|
|
|
match (a, b) {
|
2012-08-03 21:59:04 -05:00
|
|
|
(lit(a), lit(b)) => const_eval::compare_lit_exprs(tcx, a, b) == 0,
|
|
|
|
(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
|
|
|
}
|
2012-08-03 21:59:04 -05: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-08-28 17:54:45 -05: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-08-14 18:45:43 -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;
|
2012-08-06 14:34:08 -05:00
|
|
|
match o {
|
2012-08-28 17:54:45 -05:00
|
|
|
lit(lit_expr) => {
|
|
|
|
let datumblock = expr::trans_to_datum(bcx, lit_expr);
|
|
|
|
return single_result(datumblock.to_result());
|
|
|
|
}
|
|
|
|
var(disr_val, _) => {
|
|
|
|
return single_result(rslt(bcx, C_int(ccx, disr_val)));
|
|
|
|
}
|
|
|
|
range(l1, l2) => {
|
|
|
|
return range_result(rslt(bcx, consts::const_expr(ccx, l1)),
|
|
|
|
rslt(bcx, consts::const_expr(ccx, l2)));
|
2011-09-27 01:42:27 -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-06-30 18:19:07 -05:00
|
|
|
for vec::each(*variants) |v| {
|
2012-08-01 19:30:05 -05:00
|
|
|
if vdef.var == v.id { return 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
|
|
|
}
|
|
|
|
|
2012-08-01 17:23:21 -05:00
|
|
|
struct binding {
|
2012-09-07 16:50:47 -05:00
|
|
|
val: ValueRef,
|
|
|
|
mode: ast::binding_mode,
|
|
|
|
ty: ty::t
|
2012-08-01 17:23:21 -05:00
|
|
|
}
|
|
|
|
|
2012-07-31 21:25:24 -05:00
|
|
|
type bind_map = ~[{
|
|
|
|
ident: ast::ident,
|
2012-08-01 17:23:21 -05:00
|
|
|
binding: binding
|
2012-07-31 21:25:24 -05:00
|
|
|
}];
|
|
|
|
|
2012-08-20 14:23:37 -05:00
|
|
|
fn assoc(key: ast::ident, list: bind_map) -> Option<binding> {
|
2012-06-30 18:19:07 -05:00
|
|
|
for vec::each(list) |elt| {
|
2012-07-18 18:18:02 -05:00
|
|
|
if elt.ident == key {
|
2012-08-20 14:23:37 -05:00
|
|
|
return Some(elt.binding);
|
2012-08-01 17:23:21 -05:00
|
|
|
}
|
2011-08-22 07:38:48 -05:00
|
|
|
}
|
2012-08-20 14:23:37 -05:00
|
|
|
return None;
|
2011-08-22 07:38:48 -05:00
|
|
|
}
|
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
type match_branch =
|
2012-06-29 18:26:56 -05:00
|
|
|
@{pats: ~[@ast::pat],
|
2011-08-22 07:38:48 -05:00
|
|
|
bound: bind_map,
|
2012-05-17 22:18:20 -05:00
|
|
|
data: @{bodycx: block,
|
2012-08-20 14:23:37 -05:00
|
|
|
guard: Option<@ast::expr>,
|
2012-01-14 18:05:07 -06:00
|
|
|
id_map: pat_id_map}};
|
2012-07-31 18:38:41 -05:00
|
|
|
type match_ = ~[match_branch];
|
2011-07-11 09:47:24 -05:00
|
|
|
|
2012-07-31 18:38:41 -05:00
|
|
|
fn has_nested_bindings(m: match_, col: uint) -> bool {
|
2012-06-30 18:19:07 -05:00
|
|
|
for vec::each(m) |br| {
|
2012-08-06 14:34:08 -05:00
|
|
|
match br.pats[col].node {
|
2012-08-20 14:23:37 -05:00
|
|
|
ast::pat_ident(_, _, Some(_)) => return true,
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => ()
|
2011-12-08 04:56:16 -06:00
|
|
|
}
|
|
|
|
}
|
2012-08-01 19:30:05 -05:00
|
|
|
return false;
|
2011-12-08 04:56:16 -06:00
|
|
|
}
|
|
|
|
|
2012-08-01 17:23:21 -05:00
|
|
|
fn expand_nested_bindings(bcx: block, m: match_, col: uint, val: ValueRef)
|
|
|
|
-> match_ {
|
|
|
|
|
2012-06-29 18:26:56 -05:00
|
|
|
let mut result = ~[];
|
2012-06-30 18:19:07 -05:00
|
|
|
for vec::each(m) |br| {
|
2012-08-06 14:34:08 -05:00
|
|
|
match br.pats[col].node {
|
2012-08-20 14:23:37 -05:00
|
|
|
ast::pat_ident(mode, name, Some(inner)) => {
|
2012-06-28 15:52:13 -05:00
|
|
|
let pats = vec::append(
|
|
|
|
vec::slice(br.pats, 0u, col),
|
2012-06-29 18:26:56 -05:00
|
|
|
vec::append(~[inner],
|
2012-07-26 16:00:31 -05:00
|
|
|
vec::view(br.pats, col + 1u, br.pats.len())));
|
2012-06-28 15:52:13 -05:00
|
|
|
vec::push(result,
|
|
|
|
@{pats: pats,
|
|
|
|
bound: vec::append(
|
2012-06-29 18:26:56 -05:00
|
|
|
br.bound, ~[{ident: path_to_ident(name),
|
2012-08-01 17:23:21 -05:00
|
|
|
binding: binding {
|
|
|
|
val: val,
|
|
|
|
mode: mode,
|
|
|
|
ty: node_id_type(bcx,
|
|
|
|
br.pats[col].id)
|
2012-09-04 15:29:32 -05:00
|
|
|
}}]),
|
2012-08-28 17:54:45 -05:00
|
|
|
..*br});
|
2011-12-08 04:56:16 -06:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => vec::push(result, br)
|
2011-12-08 04:56:16 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
result
|
2011-07-11 09:47:24 -05:00
|
|
|
}
|
|
|
|
|
2012-08-20 14:23:37 -05:00
|
|
|
type enter_pat = fn(@ast::pat) -> Option<~[@ast::pat]>;
|
2011-07-27 07:19:39 -05:00
|
|
|
|
2012-08-01 17:23:21 -05:00
|
|
|
fn enter_match(bcx: block, dm: DefMap, m: match_, col: uint, val: ValueRef,
|
2012-07-31 18:38:41 -05:00
|
|
|
e: enter_pat) -> match_ {
|
2012-06-29 18:26:56 -05:00
|
|
|
let mut result = ~[];
|
2012-06-30 18:19:07 -05:00
|
|
|
for vec::each(m) |br| {
|
2012-08-06 14:34:08 -05:00
|
|
|
match e(br.pats[col]) {
|
2012-08-20 14:23:37 -05:00
|
|
|
Some(sub) => {
|
2012-06-28 15:52:13 -05:00
|
|
|
let pats = vec::append(
|
2012-07-26 16:00:31 -05:00
|
|
|
vec::append(sub, vec::view(br.pats, 0u, col)),
|
|
|
|
vec::view(br.pats, col + 1u, br.pats.len()));
|
2012-02-22 09:57:23 -06:00
|
|
|
let self = br.pats[col];
|
2012-08-06 14:34:08 -05:00
|
|
|
let bound = match self.node {
|
2012-08-20 14:23:37 -05:00
|
|
|
ast::pat_ident(mode, name, None)
|
2012-08-03 21:59:04 -05:00
|
|
|
if !pat_is_variant(dm, self) => {
|
2012-06-28 15:52:13 -05:00
|
|
|
vec::append(br.bound,
|
2012-07-31 21:25:24 -05:00
|
|
|
~[{ident: path_to_ident(name),
|
2012-08-01 17:23:21 -05:00
|
|
|
binding: binding {
|
|
|
|
val: val,
|
|
|
|
mode: mode,
|
|
|
|
ty: node_id_type(bcx, br.pats[col].id)
|
|
|
|
}}])
|
2012-02-22 09:57:23 -06:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => br.bound
|
2012-02-22 09:57:23 -06:00
|
|
|
};
|
2012-08-28 17:54:45 -05:00
|
|
|
vec::push(result, @{pats: pats, bound: bound, ..*br});
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
2012-08-20 14:23:37 -05:00
|
|
|
None => ()
|
2011-07-11 09:47:24 -05:00
|
|
|
}
|
|
|
|
}
|
2012-08-01 19:30:05 -05:00
|
|
|
return result;
|
2011-07-11 09:47:24 -05:00
|
|
|
}
|
|
|
|
|
2012-08-01 17:23:21 -05:00
|
|
|
fn enter_default(bcx: block, dm: DefMap, m: match_, col: uint, val: ValueRef)
|
|
|
|
-> match_ {
|
|
|
|
|
|
|
|
do enter_match(bcx, dm, m, col, val) |p| {
|
2012-08-06 14:34:08 -05:00
|
|
|
match p.node {
|
2012-08-06 19:01:14 -05:00
|
|
|
ast::pat_wild | ast::pat_rec(_, _) | ast::pat_tup(_) |
|
2012-08-20 14:23:37 -05:00
|
|
|
ast::pat_struct(*) => 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-08-01 17:23:21 -05:00
|
|
|
fn enter_opt(bcx: block, m: match_, opt: opt, col: uint,
|
2012-07-31 18:38:41 -05:00
|
|
|
variant_size: uint, val: ValueRef) -> match_ {
|
2012-08-01 17:23:21 -05:00
|
|
|
let tcx = bcx.tcx();
|
2011-08-12 10:57:21 -05:00
|
|
|
let dummy = @{id: 0, node: ast::pat_wild, span: dummy_sp()};
|
2012-08-01 17:23:21 -05:00
|
|
|
do enter_match(bcx, tcx.def_map, m, col, val) |p| {
|
2012-08-06 14:34:08 -05:00
|
|
|
match p.node {
|
2012-08-03 21:59:04 -05:00
|
|
|
ast::pat_enum(_, subpats) => {
|
2012-04-20 02:54:42 -05:00
|
|
|
if opt_eq(tcx, variant_opt(tcx, p.id), opt) {
|
2012-08-20 14:23:37 -05:00
|
|
|
Some(option::get_default(subpats,
|
2012-04-23 17:50:51 -05:00
|
|
|
vec::from_elem(variant_size, dummy))) }
|
2012-08-20 14:23:37 -05:00
|
|
|
else { None }
|
2012-02-22 09:57:23 -06:00
|
|
|
}
|
2012-08-20 14:23:37 -05:00
|
|
|
ast::pat_ident(_, _, None) if pat_is_variant(tcx.def_map, p) => {
|
|
|
|
if opt_eq(tcx, variant_opt(tcx, p.id), opt) { Some(~[]) }
|
|
|
|
else { None }
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
ast::pat_lit(l) => {
|
2012-08-20 14:23:37 -05:00
|
|
|
if opt_eq(tcx, lit(l), opt) { Some(~[]) } else { None }
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
ast::pat_range(l1, l2) => {
|
2012-08-20 14:23:37 -05:00
|
|
|
if opt_eq(tcx, range(l1, l2), opt) { Some(~[]) } else { None }
|
2011-09-28 14:07:33 -05:00
|
|
|
}
|
2012-08-20 14:23:37 -05:00
|
|
|
_ => Some(vec::from_elem(variant_size, dummy))
|
2011-07-11 09:47:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-06 19:01:14 -05:00
|
|
|
fn enter_rec_or_struct(bcx: block, dm: DefMap, 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-08-01 17:23:21 -05:00
|
|
|
do enter_match(bcx, dm, m, col, val) |p| {
|
2012-08-06 14:34:08 -05:00
|
|
|
match p.node {
|
2012-08-06 19:01:14 -05:00
|
|
|
ast::pat_rec(fpats, _) | ast::pat_struct(_, fpats, _) => {
|
2012-06-29 18:26:56 -05:00
|
|
|
let mut pats = ~[];
|
2012-06-30 18:19:07 -05:00
|
|
|
for vec::each(fields) |fname| {
|
2012-08-28 17:54:45 -05:00
|
|
|
match fpats.find(|p| p.ident == fname) {
|
|
|
|
None => vec::push(pats, dummy),
|
|
|
|
Some(pat) => vec::push(pats, pat.pat)
|
2011-07-11 09:47:24 -05:00
|
|
|
}
|
|
|
|
}
|
2012-08-20 14:23:37 -05:00
|
|
|
Some(pats)
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
2012-08-20 14:23:37 -05:00
|
|
|
_ => Some(vec::from_elem(fields.len(), dummy))
|
2011-07-11 09:47:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-01 17:23:21 -05:00
|
|
|
fn enter_tup(bcx: block, dm: DefMap, m: match_, col: uint, val: ValueRef,
|
2012-07-31 18:38:41 -05:00
|
|
|
n_elts: uint) -> match_ {
|
2011-08-15 15:33:12 -05:00
|
|
|
let dummy = @{id: 0, node: ast::pat_wild, span: dummy_sp()};
|
2012-08-01 17:23:21 -05:00
|
|
|
do enter_match(bcx, dm, m, col, val) |p| {
|
2012-08-06 14:34:08 -05:00
|
|
|
match p.node {
|
2012-08-20 14:23:37 -05:00
|
|
|
ast::pat_tup(elts) => Some(elts),
|
|
|
|
_ => Some(vec::from_elem(n_elts, dummy))
|
2011-08-15 06:15:19 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-01 17:23:21 -05:00
|
|
|
fn enter_box(bcx: block, dm: DefMap, 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-08-01 17:23:21 -05:00
|
|
|
do enter_match(bcx, dm, m, col, val) |p| {
|
2012-08-06 14:34:08 -05:00
|
|
|
match p.node {
|
2012-08-20 14:23:37 -05:00
|
|
|
ast::pat_box(sub) => Some(~[sub]),
|
|
|
|
_ => Some(~[dummy])
|
2011-07-13 04:11:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-01 17:23:21 -05:00
|
|
|
fn enter_uniq(bcx: block, dm: DefMap, 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-08-01 17:23:21 -05:00
|
|
|
do enter_match(bcx, dm, m, col, val) |p| {
|
2012-08-06 14:34:08 -05:00
|
|
|
match p.node {
|
2012-08-20 14:23:37 -05:00
|
|
|
ast::pat_uniq(sub) => Some(~[sub]),
|
|
|
|
_ => Some(~[dummy])
|
2011-09-23 13:15:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-31 18:38:41 -05:00
|
|
|
fn get_options(ccx: @crate_ctxt, m: match_, col: uint) -> ~[opt] {
|
2012-08-14 18:54:13 -05:00
|
|
|
fn add_to_set(tcx: ty::ctxt, &&set: DVec<opt>, val: opt) {
|
2012-08-01 19:30:05 -05:00
|
|
|
if set.any(|l| opt_eq(tcx, l, val)) {return;}
|
2012-05-25 10:17:06 -05:00
|
|
|
set.push(val);
|
2011-07-11 09:47:24 -05:00
|
|
|
}
|
|
|
|
|
2012-08-27 16:22:25 -05:00
|
|
|
let found = DVec();
|
2012-06-30 18:19:07 -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 {
|
2012-08-06 14:34:08 -05:00
|
|
|
match cur.node {
|
2012-08-03 21:59:04 -05:00
|
|
|
ast::pat_lit(l) => add_to_set(ccx.tcx, found, lit(l)),
|
|
|
|
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
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => ()
|
2012-02-22 09:57:23 -06:00
|
|
|
}
|
2011-07-11 09:47:24 -05:00
|
|
|
}
|
|
|
|
}
|
2012-08-13 19:11:33 -05:00
|
|
|
return vec::from_mut(dvec::unwrap(move found));
|
2011-07-11 09:47:24 -05:00
|
|
|
}
|
|
|
|
|
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-06-29 18:26:56 -05:00
|
|
|
{vals: ~[ValueRef], bcx: block} {
|
2012-08-14 18:45:43 -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-08-23 19:39:07 -05:00
|
|
|
let enum_ty_substs = match ty::get(node_id_type(bcx, pat_id))
|
2012-08-06 14:34:08 -05:00
|
|
|
.struct {
|
2012-08-03 21:59:04 -05:00
|
|
|
ty::ty_enum(id, substs) => { assert id == vdefs.enm; substs.tps }
|
2012-08-23 19:39:07 -05:00
|
|
|
_ => bcx.sess().bug(~"extract_variant_args: pattern has non-enum type")
|
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-08-27 14:16:37 -05:00
|
|
|
blobptr = GEPi(bcx, enumptr, [0u, 1u]);
|
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-06-30 18:19:07 -05:00
|
|
|
let args = do vec::from_fn(size) |i| {
|
2012-04-18 23:26:25 -05:00
|
|
|
GEP_enum(bcx, blobptr, vdefs_tg, vdefs_var,
|
|
|
|
enum_ty_substs, i)
|
|
|
|
};
|
2012-08-01 19:30:05 -05:00
|
|
|
return {vals: args, bcx: bcx};
|
2011-07-11 09:47:24 -05:00
|
|
|
}
|
|
|
|
|
2012-08-28 17:54:45 -05:00
|
|
|
fn collect_record_or_struct_fields(m: match_, col: uint) -> ~[ast::ident] {
|
2012-06-29 18:26:56 -05:00
|
|
|
let mut fields: ~[ast::ident] = ~[];
|
2012-06-30 18:19:07 -05:00
|
|
|
for vec::each(m) |br| {
|
2012-08-06 14:34:08 -05:00
|
|
|
match br.pats[col].node {
|
2012-08-28 17:54:45 -05:00
|
|
|
ast::pat_rec(fs, _) => extend(&mut fields, fs),
|
|
|
|
ast::pat_struct(_, fs, _) => extend(&mut fields, fs),
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => ()
|
2011-07-13 04:11:43 -05:00
|
|
|
}
|
|
|
|
}
|
2012-08-01 19:30:05 -05:00
|
|
|
return fields;
|
2011-07-13 04:11:43 -05:00
|
|
|
|
2012-08-28 17:54:45 -05:00
|
|
|
fn extend(idents: &mut ~[ast::ident], field_pats: &[ast::field_pat]) {
|
|
|
|
for field_pats.each |field_pat| {
|
|
|
|
let field_ident = field_pat.ident;
|
|
|
|
if !vec::any(*idents, |x| x == field_ident) {
|
|
|
|
vec::push(*idents, field_ident);
|
2012-08-06 19:01:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-31 18:38:41 -05:00
|
|
|
fn root_pats_as_necessary(bcx: block, m: match_, col: uint, val: ValueRef) {
|
2012-06-30 18:19:07 -05:00
|
|
|
for vec::each(m) |br| {
|
2012-05-17 22:18:20 -05:00
|
|
|
let pat_id = br.pats[col].id;
|
|
|
|
|
2012-08-06 14:34:08 -05:00
|
|
|
match bcx.ccx().maps.root_map.find({id:pat_id, derefs:0u}) {
|
2012-08-28 17:54:45 -05:00
|
|
|
None => (),
|
|
|
|
Some(scope_id) => {
|
|
|
|
// Note: the scope_id will always be the id of the alt. See
|
|
|
|
// the extended comment in rustc::middle::borrowck::preserve()
|
|
|
|
// for details (look for the case covering cat_discr).
|
|
|
|
|
|
|
|
let datum = Datum {val: val, ty: node_id_type(bcx, pat_id),
|
|
|
|
mode: ByRef, source: FromLvalue};
|
|
|
|
datum.root(bcx, scope_id);
|
|
|
|
return; // if we kept going, we'd only re-root the same value
|
|
|
|
}
|
2012-05-17 22:18:20 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-31 18:38:41 -05:00
|
|
|
fn any_box_pat(m: match_, col: uint) -> bool {
|
2012-06-30 18:19:07 -05:00
|
|
|
for vec::each(m) |br| {
|
2012-08-06 14:34:08 -05:00
|
|
|
match br.pats[col].node {
|
2012-08-03 21:59:04 -05:00
|
|
|
ast::pat_box(_) => return true,
|
|
|
|
_ => ()
|
|
|
|
}
|
2011-07-13 04:11:43 -05:00
|
|
|
}
|
2012-08-01 19:30:05 -05:00
|
|
|
return false;
|
2011-07-13 04:11:43 -05:00
|
|
|
}
|
|
|
|
|
2012-07-31 18:38:41 -05:00
|
|
|
fn any_uniq_pat(m: match_, col: uint) -> bool {
|
2012-06-30 18:19:07 -05:00
|
|
|
for vec::each(m) |br| {
|
2012-08-06 14:34:08 -05:00
|
|
|
match br.pats[col].node {
|
2012-08-03 21:59:04 -05:00
|
|
|
ast::pat_uniq(_) => return true,
|
|
|
|
_ => ()
|
|
|
|
}
|
2011-09-23 13:15:17 -05:00
|
|
|
}
|
2012-08-01 19:30:05 -05:00
|
|
|
return false;
|
2011-09-23 13:15:17 -05:00
|
|
|
}
|
|
|
|
|
2012-07-31 18:38:41 -05:00
|
|
|
fn any_tup_pat(m: match_, col: uint) -> bool {
|
2012-06-30 18:19:07 -05:00
|
|
|
for vec::each(m) |br| {
|
2012-08-06 14:34:08 -05:00
|
|
|
match br.pats[col].node {
|
2012-08-03 21:59:04 -05:00
|
|
|
ast::pat_tup(_) => return true,
|
|
|
|
_ => ()
|
|
|
|
}
|
2011-08-15 06:15:19 -05:00
|
|
|
}
|
2012-08-01 19:30:05 -05:00
|
|
|
return false;
|
2011-08-15 06:15:19 -05:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2012-07-31 18:38:41 -05:00
|
|
|
fn pick_col(m: match_) -> uint {
|
2011-12-08 04:56:16 -06:00
|
|
|
fn score(p: @ast::pat) -> uint {
|
2012-08-06 14:34:08 -05:00
|
|
|
match p.node {
|
2012-08-03 21:59:04 -05:00
|
|
|
ast::pat_lit(_) | ast::pat_enum(_, _) | ast::pat_range(_, _) => 1u,
|
2012-08-20 14:23:37 -05:00
|
|
|
ast::pat_ident(_, _, Some(p)) => score(p),
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => 0u
|
2011-12-08 04:56:16 -06:00
|
|
|
}
|
|
|
|
}
|
2012-03-12 17:52:30 -05:00
|
|
|
let scores = vec::to_mut(vec::from_elem(m[0].pats.len(), 0u));
|
2012-06-30 18:19:07 -05:00
|
|
|
for vec::each(m) |br| {
|
2012-03-15 08:47:03 -05:00
|
|
|
let mut i = 0u;
|
2012-06-30 18:19:07 -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-06-30 18:19:07 -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.
|
2012-08-01 19:30:05 -05:00
|
|
|
if score == 0u { return i; }
|
2011-08-02 05:57:27 -05:00
|
|
|
// 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;
|
|
|
|
}
|
2012-08-01 19:30:05 -05:00
|
|
|
return best_col;
|
2011-08-02 05:57:27 -05:00
|
|
|
}
|
2011-07-11 09:47:24 -05:00
|
|
|
|
2012-08-27 18:26:35 -05:00
|
|
|
enum branch_kind { no_branch, single, switch, compare, }
|
|
|
|
|
|
|
|
impl branch_kind : cmp::Eq {
|
|
|
|
pure fn eq(&&other: branch_kind) -> bool {
|
|
|
|
(self as uint) == (other as uint)
|
|
|
|
}
|
2012-09-07 14:06:02 -05:00
|
|
|
pure fn ne(&&other: branch_kind) -> bool { !self.eq(other) }
|
2012-08-27 18:26:35 -05:00
|
|
|
}
|
|
|
|
|
2012-09-10 17:09:37 -05:00
|
|
|
// Compiles a comparison between two things.
|
2012-09-10 17:47:21 -05:00
|
|
|
fn compare_values(cx: block, lhs: ValueRef, rhs: ValueRef, rhs_t: ty::t) ->
|
|
|
|
Result {
|
|
|
|
let _icx = cx.insn_ctxt("compare_values");
|
2012-09-10 17:09:37 -05:00
|
|
|
if ty::type_is_scalar(rhs_t) {
|
2012-09-10 17:47:21 -05:00
|
|
|
let rs = compare_scalar_types(cx, lhs, rhs, rhs_t, ast::eq);
|
2012-09-10 17:09:37 -05:00
|
|
|
return rslt(rs.bcx, rs.val);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Determine the operation we need.
|
2012-09-10 17:47:21 -05:00
|
|
|
let llop = C_u8(abi::cmp_glue_op_eq);
|
2012-09-10 17:09:37 -05:00
|
|
|
let cmpval = glue::call_cmp_glue(cx, lhs, rhs, rhs_t, llop);
|
2012-09-10 17:47:21 -05:00
|
|
|
rslt(cx, cmpval)
|
2012-09-10 17:09:37 -05:00
|
|
|
}
|
|
|
|
|
2012-07-31 18:38:41 -05:00
|
|
|
fn compile_submatch(bcx: block, m: match_, vals: ~[ValueRef],
|
2012-08-20 14:23:37 -05:00
|
|
|
chk: Option<mk_fail>, &exits: ~[exit_node]) {
|
2012-07-27 15:13:03 -05:00
|
|
|
/*
|
|
|
|
For an empty match, a fall-through case must exist
|
|
|
|
*/
|
|
|
|
assert(m.len() > 0u || is_some(chk));
|
2012-08-14 18:45:43 -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-08-28 17:54:45 -05:00
|
|
|
if m.len() == 0u {
|
|
|
|
Br(bcx, option::get(chk)());
|
|
|
|
return;
|
|
|
|
}
|
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;
|
2012-08-06 14:34:08 -05:00
|
|
|
match data.guard {
|
2012-08-20 14:23:37 -05:00
|
|
|
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.
|
2012-08-01 17:23:21 -05:00
|
|
|
//
|
|
|
|
// Also, in the case of by-value, do the copy now.
|
|
|
|
|
2012-06-30 18:19:07 -05:00
|
|
|
for data.id_map.each |key, val| {
|
2012-08-01 17:23:21 -05:00
|
|
|
let binding = assoc(key, m[0].bound).get();
|
2012-08-28 17:54:45 -05:00
|
|
|
let datum = Datum {val: binding.val, ty: binding.ty,
|
|
|
|
mode: ByRef, source: FromLvalue};
|
2012-08-01 17:23:21 -05:00
|
|
|
|
2012-08-28 17:54:45 -05:00
|
|
|
if binding.mode == ast::bind_by_value {
|
|
|
|
let llty = type_of::type_of(bcx.fcx.ccx, binding.ty);
|
2012-08-01 17:23:21 -05:00
|
|
|
let alloc = alloca(bcx, llty);
|
2012-08-28 17:54:45 -05:00
|
|
|
bcx = datum.copy_to(bcx, INIT, alloc);
|
2012-08-01 17:23:21 -05:00
|
|
|
bcx.fcx.lllocals.insert(val, local_mem(alloc));
|
2012-08-28 17:54:45 -05:00
|
|
|
add_clean(bcx, alloc, binding.ty);
|
|
|
|
} else if binding.mode == ast::bind_by_move {
|
2012-08-22 18:00:28 -05:00
|
|
|
fail ~"can't translate bind_by_move into a pattern guard";
|
2012-08-01 17:23:21 -05:00
|
|
|
} else {
|
2012-08-28 17:54:45 -05:00
|
|
|
bcx.fcx.lllocals.insert(val, local_mem(datum.val));
|
2012-08-01 17:23:21 -05:00
|
|
|
}
|
2012-02-17 06:17:40 -06:00
|
|
|
};
|
2012-08-28 17:54:45 -05:00
|
|
|
|
|
|
|
let Result {bcx: guard_cx, val} = {
|
2012-07-14 00:57:48 -05:00
|
|
|
do with_scope_result(bcx, e.info(), ~"guard") |bcx| {
|
2012-08-28 17:54:45 -05:00
|
|
|
expr::trans_to_appropriate_llval(bcx, e)
|
2012-05-14 16:24:16 -05:00
|
|
|
}
|
2012-02-17 06:17:40 -06:00
|
|
|
};
|
2012-08-28 17:54:45 -05:00
|
|
|
|
2012-06-30 18:19:07 -05:00
|
|
|
bcx = do 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
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => ()
|
2011-08-22 07:38:48 -05:00
|
|
|
}
|
2011-09-21 05:40:27 -05:00
|
|
|
if !bcx.unreachable {
|
2012-06-26 02:39:18 -05:00
|
|
|
vec::push(exits, {bound: m[0].bound, from: bcx.llbb,
|
|
|
|
to: data.bodycx.llbb});
|
2011-09-21 05:40:27 -05:00
|
|
|
}
|
2012-05-17 22:18:20 -05:00
|
|
|
Br(bcx, data.bodycx.llbb);
|
2012-08-01 19:30:05 -05:00
|
|
|
return;
|
2011-07-11 09:47:24 -05:00
|
|
|
}
|
|
|
|
|
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) {
|
2012-08-01 17:23:21 -05:00
|
|
|
expand_nested_bindings(bcx, m, col, val)
|
2012-03-13 07:42:29 -05:00
|
|
|
} else { m };
|
2011-12-08 04:56:16 -06:00
|
|
|
|
2012-06-28 15:52:13 -05:00
|
|
|
let vals_left = vec::append(vec::slice(vals, 0u, col),
|
2012-07-26 16:00:31 -05:00
|
|
|
vec::view(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-06-30 18:19:07 -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
|
|
|
|
2012-05-17 22:18:20 -05:00
|
|
|
root_pats_as_necessary(bcx, m, col, val);
|
|
|
|
|
2012-08-28 17:54:45 -05:00
|
|
|
let rec_fields = collect_record_or_struct_fields(m, col);
|
2012-08-06 19:01:14 -05:00
|
|
|
if rec_fields.len() > 0 {
|
2012-08-28 17:54:45 -05:00
|
|
|
let pat_ty = node_id_type(bcx, pat_id);
|
|
|
|
do expr::with_field_tys(tcx, pat_ty) |_has_dtor, field_tys| {
|
|
|
|
let mut rec_vals = ~[];
|
|
|
|
for vec::each(rec_fields) |field_name| {
|
|
|
|
let ix = ty::field_idx_strict(tcx, field_name, field_tys);
|
|
|
|
vec::push(rec_vals, GEPi(bcx, val, struct_field(ix)));
|
2012-08-06 19:01:14 -05:00
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
compile_submatch(
|
|
|
|
bcx,
|
|
|
|
enter_rec_or_struct(bcx, dm, m, col, rec_fields, val),
|
|
|
|
vec::append(rec_vals, vals_left),
|
|
|
|
chk,
|
|
|
|
exits);
|
2012-08-06 19:01:14 -05:00
|
|
|
}
|
2012-08-01 19:30:05 -05:00
|
|
|
return;
|
2011-07-11 09:47:24 -05:00
|
|
|
}
|
|
|
|
|
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-08-06 14:34:08 -05:00
|
|
|
let n_tup_elts = match ty::get(tup_ty).struct {
|
2012-08-03 21:59:04 -05:00
|
|
|
ty::ty_tup(elts) => elts.len(),
|
|
|
|
_ => ccx.sess.bug(~"non-tuple type in tuple pattern")
|
2012-02-03 08:15:28 -06:00
|
|
|
};
|
2012-06-29 18:26:56 -05:00
|
|
|
let mut tup_vals = ~[], i = 0u;
|
2011-08-15 06:15:19 -05:00
|
|
|
while i < n_tup_elts {
|
2012-08-27 14:16:37 -05:00
|
|
|
vec::push(tup_vals, GEPi(bcx, val, [0u, i]));
|
2011-08-15 06:15:19 -05:00
|
|
|
i += 1u;
|
|
|
|
}
|
2012-08-01 17:23:21 -05:00
|
|
|
compile_submatch(bcx, enter_tup(bcx, dm, m, col, val, n_tup_elts),
|
2012-06-28 15:52:13 -05:00
|
|
|
vec::append(tup_vals, vals_left), chk, exits);
|
2012-08-01 19:30:05 -05:00
|
|
|
return;
|
2011-08-15 06:15:19 -05:00
|
|
|
}
|
|
|
|
|
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) {
|
2012-06-27 16:10:24 -05:00
|
|
|
let llbox = Load(bcx, val);
|
|
|
|
let box_no_addrspace = non_gc_box_cast(bcx, llbox);
|
2012-06-25 22:00:46 -05:00
|
|
|
let unboxed =
|
2012-08-27 14:16:37 -05:00
|
|
|
GEPi(bcx, box_no_addrspace, [0u, abi::box_field_body]);
|
2012-08-01 17:23:21 -05:00
|
|
|
compile_submatch(bcx, enter_box(bcx, dm, m, col, val),
|
2012-06-29 18:26:56 -05:00
|
|
|
vec::append(~[unboxed], vals_left), chk, exits);
|
2012-08-01 19:30:05 -05:00
|
|
|
return;
|
2011-07-13 04:11:43 -05:00
|
|
|
}
|
|
|
|
|
2011-09-23 13:15:17 -05:00
|
|
|
if any_uniq_pat(m, col) {
|
2012-06-27 16:10:24 -05:00
|
|
|
let llbox = Load(bcx, val);
|
|
|
|
let box_no_addrspace = non_gc_box_cast(bcx, llbox);
|
2012-06-25 22:00:46 -05:00
|
|
|
let unboxed =
|
2012-08-27 14:16:37 -05:00
|
|
|
GEPi(bcx, box_no_addrspace, [0u, abi::box_field_body]);
|
2012-08-01 17:23:21 -05:00
|
|
|
compile_submatch(bcx, enter_uniq(bcx, dm, m, col, val),
|
2012-06-29 18:26:56 -05:00
|
|
|
vec::append(~[unboxed], vals_left), chk, exits);
|
2012-08-01 19:30:05 -05:00
|
|
|
return;
|
2011-09-23 13:15:17 -05:00
|
|
|
}
|
|
|
|
|
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-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 {
|
2012-08-06 14:34:08 -05:00
|
|
|
match opts[0] {
|
2012-08-03 21:59:04 -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-08-27 14:16:37 -05:00
|
|
|
let discrimptr = GEPi(bcx, enumptr, [0u, 0u]);
|
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
|
|
|
}
|
2012-08-26 11:58:45 -05:00
|
|
|
lit(_) => {
|
2012-02-09 07:33:00 -06:00
|
|
|
let pty = node_id_type(bcx, pat_id);
|
2012-08-31 12:14:39 -05:00
|
|
|
test_val = load_if_immediate(bcx, val, pty);
|
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
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
range(_, _) => {
|
2012-08-28 17:54:45 -05:00
|
|
|
test_val = Load(bcx, val);
|
2011-09-28 14:07:33 -05:00
|
|
|
kind = compare;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-06-30 18:19:07 -05:00
|
|
|
for vec::each(opts) |o| {
|
2012-08-06 14:34:08 -05:00
|
|
|
match o {
|
2012-08-03 21:59:04 -05:00
|
|
|
range(_, _) => { kind = compare; break }
|
|
|
|
_ => ()
|
2011-07-11 09:47:24 -05:00
|
|
|
}
|
|
|
|
}
|
2012-08-06 14:34:08 -05:00
|
|
|
let else_cx = match kind {
|
2012-08-03 21:59:04 -05:00
|
|
|
no_branch | single => bcx,
|
|
|
|
_ => sub_block(bcx, ~"match_else")
|
2012-02-21 07:25:53 -06:00
|
|
|
};
|
|
|
|
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-08-01 17:23:21 -05:00
|
|
|
let defaults = enter_default(bcx, dm, m, col, val);
|
2012-03-13 07:42:29 -05:00
|
|
|
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-08-28 17:54:45 -05:00
|
|
|
|
2012-03-13 07:42:29 -05:00
|
|
|
// Compile subtrees for each option
|
2012-06-30 18:19:07 -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 {
|
2012-07-14 00:57:48 -05:00
|
|
|
opt_cx = sub_block(bcx, ~"match_case");
|
2012-08-06 14:34:08 -05:00
|
|
|
match kind {
|
2012-08-03 21:59:04 -05:00
|
|
|
single => Br(bcx, opt_cx.llbb),
|
|
|
|
switch => {
|
2012-08-23 19:39:07 -05:00
|
|
|
match trans_opt(bcx, opt) {
|
2012-08-03 21:59:04 -05:00
|
|
|
single_result(r) => {
|
2012-03-13 07:42:29 -05:00
|
|
|
llvm::LLVMAddCase(sw, r.val, opt_cx.llbb);
|
|
|
|
bcx = r.bcx;
|
2012-02-17 06:17:40 -06:00
|
|
|
}
|
2012-08-23 19:39:07 -05:00
|
|
|
_ => bcx.sess().bug(~"in compile_submatch, expected \
|
|
|
|
trans_opt to return a single_result")
|
2012-02-17 06:17:40 -06:00
|
|
|
}
|
2012-03-13 07:42:29 -05:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
compare => {
|
2012-03-13 07:42:29 -05:00
|
|
|
let t = node_id_type(bcx, pat_id);
|
2012-08-28 17:54:45 -05:00
|
|
|
let Result {bcx: after_cx, val: matches} = {
|
2012-08-20 14:23:37 -05:00
|
|
|
do with_scope_result(bcx, None, ~"compare_scope") |bcx| {
|
2012-08-06 14:34:08 -05:00
|
|
|
match trans_opt(bcx, opt) {
|
2012-08-28 17:54:45 -05:00
|
|
|
single_result(
|
2012-09-10 17:47:21 -05:00
|
|
|
Result {bcx, val}) => {
|
|
|
|
compare_values(bcx, test_val, val, t)
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
range_result(
|
|
|
|
Result {val: vbegin, _},
|
2012-09-10 17:47:21 -05:00
|
|
|
Result {bcx, val: vend}) => {
|
2012-09-10 17:26:15 -05:00
|
|
|
let Result {bcx, val: llge} =
|
|
|
|
compare_scalar_types(bcx, test_val,
|
|
|
|
vbegin, t, ast::ge);
|
|
|
|
let Result {bcx, val: llle} =
|
|
|
|
compare_scalar_types(bcx, test_val, vend,
|
|
|
|
t, ast::le);
|
2012-08-28 17:54:45 -05:00
|
|
|
rslt(bcx, And(bcx, llge, llle))
|
|
|
|
}
|
2012-05-14 16:24:16 -05:00
|
|
|
}
|
2012-03-13 07:42:29 -05:00
|
|
|
}
|
|
|
|
};
|
2012-07-14 00:57:48 -05:00
|
|
|
bcx = sub_block(after_cx, ~"compare_next");
|
2012-03-13 07:42:29 -05:00
|
|
|
CondBr(after_cx, matches, opt_cx.llbb, bcx.llbb);
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => ()
|
2012-03-13 07:42:29 -05:00
|
|
|
}
|
|
|
|
} else if kind == compare { Br(bcx, else_cx.llbb); }
|
2012-03-15 08:47:03 -05:00
|
|
|
let mut size = 0u;
|
2012-06-29 18:26:56 -05:00
|
|
|
let mut unpacked = ~[];
|
2012-08-06 14:34:08 -05:00
|
|
|
match opt {
|
2012-08-03 21:59:04 -05:00
|
|
|
var(_, vdef) => {
|
2011-07-27 07:19:39 -05:00
|
|
|
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;
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
lit(_) | range(_, _) => ()
|
2011-07-11 09:47:24 -05:00
|
|
|
}
|
2012-08-01 17:23:21 -05:00
|
|
|
compile_submatch(opt_cx, enter_opt(bcx, m, opt, col, size, val),
|
2012-06-28 15:52:13 -05:00
|
|
|
vec::append(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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-06 09:20:23 -05:00
|
|
|
struct phi_binding {
|
2012-09-07 16:50:47 -05:00
|
|
|
pat_id: ast::node_id,
|
|
|
|
phi_val: ValueRef,
|
|
|
|
mode: ast::binding_mode,
|
|
|
|
ty: ty::t
|
2012-08-06 09:20:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type phi_bindings_list = ~[phi_binding];
|
|
|
|
|
2011-07-19 10:07:51 -05:00
|
|
|
// Returns false for unreachable blocks
|
2012-08-06 09:20:23 -05:00
|
|
|
fn make_phi_bindings(bcx: block,
|
|
|
|
map: ~[exit_node],
|
|
|
|
ids: pat_util::pat_id_map)
|
2012-08-20 14:23:37 -05:00
|
|
|
-> Option<phi_bindings_list> {
|
2012-08-14 18:45:43 -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-08-06 09:20:23 -05:00
|
|
|
let mut phi_bindings = ~[];
|
2012-06-30 18:19:07 -05:00
|
|
|
for ids.each |name, node_id| {
|
2012-06-29 18:26:56 -05:00
|
|
|
let mut llbbs = ~[];
|
|
|
|
let mut vals = ~[];
|
2012-08-20 14:23:37 -05:00
|
|
|
let mut binding = None;
|
2012-06-30 18:19:07 -05:00
|
|
|
for vec::each(map) |ex| {
|
2011-07-27 07:19:39 -05:00
|
|
|
if ex.to as uint == our_block {
|
2012-08-06 14:34:08 -05:00
|
|
|
match assoc(name, ex.bound) {
|
2012-08-20 14:23:37 -05:00
|
|
|
Some(b) => {
|
2012-06-26 02:39:18 -05:00
|
|
|
vec::push(llbbs, ex.from);
|
2012-08-06 09:20:23 -05:00
|
|
|
vec::push(vals, b.val);
|
2012-08-20 14:23:37 -05:00
|
|
|
binding = Some(b);
|
2012-06-26 02:39:18 -05:00
|
|
|
}
|
2012-08-20 14:23:37 -05:00
|
|
|
None => ()
|
2011-07-19 10:07:51 -05:00
|
|
|
}
|
2011-07-11 09:47:24 -05:00
|
|
|
}
|
|
|
|
}
|
2012-08-06 09:20:23 -05:00
|
|
|
|
|
|
|
let binding = match binding {
|
2012-08-20 14:23:37 -05:00
|
|
|
Some(binding) => binding,
|
|
|
|
None => {
|
2012-08-06 09:20:23 -05:00
|
|
|
Unreachable(bcx);
|
2012-08-20 14:23:37 -05:00
|
|
|
return None;
|
2012-08-06 09:20:23 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let phi_val = Phi(bcx, val_ty(vals[0]), vals, llbbs);
|
|
|
|
vec::push(phi_bindings, phi_binding {
|
|
|
|
pat_id: node_id,
|
|
|
|
phi_val: phi_val,
|
|
|
|
mode: binding.mode,
|
|
|
|
ty: binding.ty
|
|
|
|
});
|
2011-09-13 05:14:30 -05:00
|
|
|
}
|
2012-08-20 14:23:37 -05:00
|
|
|
return Some(move phi_bindings);
|
2011-07-11 09:47:24 -05:00
|
|
|
}
|
|
|
|
|
2012-08-01 17:23:21 -05:00
|
|
|
// Copies by-value bindings into their homes.
|
2012-08-06 09:20:23 -05:00
|
|
|
fn make_pattern_bindings(bcx: block, phi_bindings: phi_bindings_list)
|
|
|
|
-> block {
|
2012-08-01 17:23:21 -05:00
|
|
|
let mut bcx = bcx;
|
|
|
|
|
2012-08-06 09:20:23 -05:00
|
|
|
for phi_bindings.each |binding| {
|
|
|
|
let phi_val = binding.phi_val;
|
2012-08-01 17:23:21 -05:00
|
|
|
match binding.mode {
|
2012-08-06 09:20:23 -05:00
|
|
|
ast::bind_by_implicit_ref => {
|
|
|
|
// use local: phi is a ptr to the value
|
|
|
|
bcx.fcx.lllocals.insert(binding.pat_id,
|
|
|
|
local_mem(phi_val));
|
|
|
|
}
|
|
|
|
ast::bind_by_ref(_) => {
|
|
|
|
// use local_imm: ptr is the value
|
|
|
|
bcx.fcx.lllocals.insert(binding.pat_id,
|
|
|
|
local_imm(phi_val));
|
|
|
|
}
|
2012-08-22 19:35:05 -05:00
|
|
|
ast::bind_by_value | ast::bind_by_move => {
|
2012-08-06 09:20:23 -05:00
|
|
|
// by value: make a new temporary and copy the value out
|
2012-08-28 17:54:45 -05:00
|
|
|
let phi_datum = Datum {val: phi_val, ty: binding.ty,
|
|
|
|
mode: ByRef, source: FromLvalue};
|
|
|
|
let scratch = scratch_datum(bcx, binding.ty, false);
|
|
|
|
if binding.mode == ast::bind_by_value {
|
|
|
|
phi_datum.copy_to_datum(bcx, INIT, scratch);
|
2012-08-22 19:35:05 -05:00
|
|
|
} else {
|
2012-08-28 17:54:45 -05:00
|
|
|
phi_datum.move_to_datum(bcx, INIT, scratch);
|
|
|
|
}
|
2012-08-06 09:20:23 -05:00
|
|
|
bcx.fcx.lllocals.insert(binding.pat_id,
|
2012-08-28 17:54:45 -05:00
|
|
|
local_mem(scratch.val));
|
|
|
|
add_clean(bcx, scratch.val, binding.ty);
|
2012-08-01 17:23:21 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return bcx;
|
|
|
|
}
|
|
|
|
|
2012-05-14 16:24:16 -05:00
|
|
|
fn trans_alt(bcx: block,
|
|
|
|
alt_expr: @ast::expr,
|
2012-08-28 17:54:45 -05:00
|
|
|
discr_expr: @ast::expr,
|
2012-06-29 18:26:56 -05:00
|
|
|
arms: ~[ast::arm],
|
2012-08-28 17:54:45 -05:00
|
|
|
dest: Dest) -> block {
|
2012-08-14 18:45:43 -05:00
|
|
|
let _icx = bcx.insn_ctxt("alt::trans_alt");
|
2012-07-14 00:57:48 -05:00
|
|
|
do with_scope(bcx, alt_expr.info(), ~"alt") |bcx| {
|
2012-08-28 17:54:45 -05:00
|
|
|
trans_alt_inner(bcx, discr_expr, arms, dest)
|
2012-03-13 07:42:29 -05:00
|
|
|
}
|
2012-02-17 06:17:40 -06:00
|
|
|
}
|
|
|
|
|
2012-08-28 17:54:45 -05:00
|
|
|
fn trans_alt_inner(scope_cx: block,
|
|
|
|
discr_expr: @ast::expr,
|
|
|
|
arms: ~[ast::arm],
|
|
|
|
dest: Dest) -> block {
|
2012-08-14 18:45:43 -05:00
|
|
|
let _icx = scope_cx.insn_ctxt("alt::trans_alt_inner");
|
2012-08-28 17:54:45 -05:00
|
|
|
let mut bcx = scope_cx;
|
|
|
|
let tcx = bcx.tcx();
|
2012-02-17 06:17:40 -06:00
|
|
|
|
2012-08-28 17:54:45 -05:00
|
|
|
let discr_datum = unpack_datum!(bcx, {
|
|
|
|
expr::trans_to_datum(bcx, discr_expr)
|
|
|
|
});
|
|
|
|
if bcx.unreachable {
|
|
|
|
return bcx;
|
|
|
|
}
|
2012-02-17 06:17:40 -06:00
|
|
|
|
2012-08-28 17:54:45 -05:00
|
|
|
let mut bodies = ~[], matches = ~[];
|
2012-06-30 18:19:07 -05:00
|
|
|
for vec::each(arms) |a| {
|
2012-07-14 00:57:48 -05:00
|
|
|
let body = scope_block(bcx, a.body.info(), ~"case_body");
|
2012-02-22 09:57:23 -06:00
|
|
|
let id_map = pat_util::pat_id_map(tcx.def_map, a.pats[0]);
|
2012-06-26 02:39:18 -05:00
|
|
|
vec::push(bodies, body);
|
2012-06-30 18:19:07 -05:00
|
|
|
for vec::each(a.pats) |p| {
|
2012-07-31 18:38:41 -05:00
|
|
|
vec::push(matches, @{pats: ~[p],
|
2012-06-29 18:26:56 -05:00
|
|
|
bound: ~[],
|
2012-05-17 22:18:20 -05:00
|
|
|
data: @{bodycx: body, guard: a.guard,
|
2012-06-26 02:39:18 -05:00
|
|
|
id_map: id_map}});
|
2011-07-11 09:47:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-28 17:54:45 -05:00
|
|
|
let t = node_id_type(bcx, discr_expr.id);
|
|
|
|
let chk = {
|
|
|
|
if ty::type_is_empty(tcx, t) {
|
|
|
|
// Special case for empty types
|
|
|
|
let fail_cx = @mut None;
|
|
|
|
Some(|| mk_fail(scope_cx, discr_expr.span,
|
2012-07-27 15:13:03 -05:00
|
|
|
~"scrutinizing value that can't exist", fail_cx))
|
2012-08-28 17:54:45 -05:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2012-03-13 07:42:29 -05:00
|
|
|
};
|
2012-06-29 18:26:56 -05:00
|
|
|
let mut exit_map = ~[];
|
2012-08-28 17:54:45 -05:00
|
|
|
let lldiscr = discr_datum.to_ref_llval(bcx);
|
|
|
|
compile_submatch(bcx, matches, ~[lldiscr], chk, exit_map);
|
2011-07-11 09:47:24 -05:00
|
|
|
|
2012-08-28 17:54:45 -05:00
|
|
|
let mut arm_cxs = ~[], i = 0u;
|
2012-06-30 18:19:07 -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]);
|
2012-08-06 09:20:23 -05:00
|
|
|
match make_phi_bindings(body_cx, exit_map, id_map) {
|
2012-08-20 14:23:37 -05:00
|
|
|
None => {}
|
|
|
|
Some(phi_bindings) => {
|
2012-08-06 09:20:23 -05:00
|
|
|
let body_cx = make_pattern_bindings(body_cx, phi_bindings);
|
2012-08-28 17:54:45 -05:00
|
|
|
let mut arm_cx =
|
|
|
|
controlflow::trans_block(body_cx, a.body, dest);
|
2012-08-23 19:39:07 -05:00
|
|
|
arm_cx = trans_block_cleanups(arm_cx,
|
|
|
|
block_cleanups(body_cx));
|
2012-08-06 09:20:23 -05:00
|
|
|
vec::push(arm_cxs, arm_cx);
|
|
|
|
}
|
2011-07-19 10:07:51 -05:00
|
|
|
}
|
2011-07-11 09:47:24 -05:00
|
|
|
i += 1u;
|
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
return controlflow::join_blocks(scope_cx, arm_cxs);
|
|
|
|
|
|
|
|
fn mk_fail(bcx: block, sp: span, msg: ~str,
|
|
|
|
done: @mut Option<BasicBlockRef>) -> BasicBlockRef {
|
|
|
|
match *done { Some(bb) => return bb, _ => () }
|
|
|
|
let fail_cx = sub_block(bcx, ~"case_fallthrough");
|
|
|
|
controlflow::trans_fail(fail_cx, Some(sp), msg);
|
|
|
|
*done = Some(fail_cx.llbb);
|
|
|
|
return fail_cx.llbb;
|
|
|
|
}
|
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-08-14 18:45:43 -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-08-06 14:34:08 -05:00
|
|
|
match pat.node {
|
2012-08-28 17:54:45 -05:00
|
|
|
ast::pat_ident(_, _,inner) => {
|
|
|
|
if pat_is_variant(bcx.tcx().def_map, pat) {
|
|
|
|
return bcx;
|
|
|
|
}
|
|
|
|
|
|
|
|
if make_copy {
|
|
|
|
let binding_ty = node_id_type(bcx, pat.id);
|
|
|
|
let datum = Datum {val: val, ty: binding_ty,
|
|
|
|
mode: ByRef, source: FromRvalue};
|
|
|
|
let scratch = scratch_datum(bcx, binding_ty, false);
|
|
|
|
datum.copy_to_datum(bcx, INIT, scratch);
|
|
|
|
bcx.fcx.lllocals.insert(pat.id, local_mem(scratch.val));
|
|
|
|
add_clean(bcx, scratch.val, binding_ty);
|
|
|
|
} else {
|
|
|
|
bcx.fcx.lllocals.insert(pat.id, local_mem(val));
|
|
|
|
}
|
|
|
|
|
|
|
|
for inner.each |inner_pat| {
|
|
|
|
bcx = bind_irrefutable_pat(bcx, inner_pat, val, true);
|
|
|
|
}
|
2011-07-28 05:01:45 -05:00
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
ast::pat_enum(_, sub_pats) => {
|
|
|
|
let pat_def = ccx.tcx.def_map.get(pat.id);
|
|
|
|
let vdefs = ast_util::variant_def_ids(pat_def);
|
|
|
|
let args = extract_variant_args(bcx, pat.id, vdefs, val);
|
|
|
|
for sub_pats.each |sub_pat| {
|
|
|
|
for vec::eachi(args.vals) |i, argval| {
|
|
|
|
bcx = bind_irrefutable_pat(bcx, sub_pat[i],
|
|
|
|
argval, make_copy);
|
|
|
|
}
|
2012-08-06 19:01:14 -05:00
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
ast::pat_rec(fields, _) | ast::pat_struct(_, fields, _) => {
|
|
|
|
let tcx = bcx.tcx();
|
|
|
|
let pat_ty = node_id_type(bcx, pat.id);
|
|
|
|
do expr::with_field_tys(tcx, pat_ty) |_has_dtor, field_tys| {
|
|
|
|
for vec::each(fields) |f| {
|
|
|
|
let ix = ty::field_idx_strict(tcx, f.ident, field_tys);
|
|
|
|
let fldptr = GEPi(bcx, val, struct_field(ix));
|
|
|
|
bcx = bind_irrefutable_pat(bcx, f.pat, fldptr, make_copy);
|
|
|
|
}
|
2012-08-06 19:01:14 -05:00
|
|
|
}
|
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
ast::pat_tup(elems) => {
|
|
|
|
for vec::eachi(elems) |i, elem| {
|
|
|
|
let fldptr = GEPi(bcx, val, [0u, i]);
|
|
|
|
bcx = bind_irrefutable_pat(bcx, elem, fldptr, make_copy);
|
|
|
|
}
|
2012-08-06 19:01:14 -05:00
|
|
|
}
|
2012-09-07 19:07:32 -05:00
|
|
|
ast::pat_box(inner) | ast::pat_uniq(inner) |
|
|
|
|
ast::pat_region(inner) => {
|
2012-08-28 17:54:45 -05:00
|
|
|
let llbox = Load(bcx, val);
|
|
|
|
let unboxed = GEPi(bcx, llbox, [0u, abi::box_field_body]);
|
|
|
|
bcx = bind_irrefutable_pat(bcx, inner, unboxed, true);
|
2011-08-15 06:15:19 -05:00
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
ast::pat_wild | ast::pat_lit(_) | ast::pat_range(_, _) => ()
|
2011-07-28 05:01:45 -05:00
|
|
|
}
|
2012-08-01 19:30:05 -05:00
|
|
|
return bcx;
|
2011-07-28 05:01:45 -05:00
|
|
|
}
|
|
|
|
|
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:
|