]> git.lizzy.rs Git - rust.git/blob - src/librustc/middle/trans/_match.rs
3755cca8c35e97acf40a44b302acba8da1ef7957
[rust.git] / src / librustc / middle / trans / _match.rs
1 // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 /*!
12  *
13  * # Compilation of match statements
14  *
15  * I will endeavor to explain the code as best I can.  I have only a loose
16  * understanding of some parts of it.
17  *
18  * ## Matching
19  *
20  * The basic state of the code is maintained in an array `m` of `@Match`
21  * objects.  Each `@Match` describes some list of patterns, all of which must
22  * match against the current list of values.  If those patterns match, then
23  * the arm listed in the match is the correct arm.  A given arm may have
24  * multiple corresponding match entries, one for each alternative that
25  * remains.  As we proceed these sets of matches are adjusted by the various
26  * `enter_XXX()` functions, each of which adjusts the set of options given
27  * some information about the value which has been matched.
28  *
29  * So, initially, there is one value and N matches, each of which have one
30  * constituent pattern.  N here is usually the number of arms but may be
31  * greater, if some arms have multiple alternatives.  For example, here:
32  *
33  *     enum Foo { A, B(int), C(uint, uint) }
34  *     match foo {
35  *         A => ...,
36  *         B(x) => ...,
37  *         C(1u, 2) => ...,
38  *         C(_) => ...
39  *     }
40  *
41  * The value would be `foo`.  There would be four matches, each of which
42  * contains one pattern (and, in one case, a guard).  We could collect the
43  * various options and then compile the code for the case where `foo` is an
44  * `A`, a `B`, and a `C`.  When we generate the code for `C`, we would (1)
45  * drop the two matches that do not match a `C` and (2) expand the other two
46  * into two patterns each.  In the first case, the two patterns would be `1u`
47  * and `2`, and the in the second case the _ pattern would be expanded into
48  * `_` and `_`.  The two values are of course the arguments to `C`.
49  *
50  * Here is a quick guide to the various functions:
51  *
52  * - `compile_submatch()`: The main workhouse.  It takes a list of values and
53  *   a list of matches and finds the various possibilities that could occur.
54  *
55  * - `enter_XXX()`: modifies the list of matches based on some information
56  *   about the value that has been matched.  For example,
57  *   `enter_rec_or_struct()` adjusts the values given that a record or struct
58  *   has been matched.  This is an infallible pattern, so *all* of the matches
59  *   must be either wildcards or record/struct patterns.  `enter_opt()`
60  *   handles the fallible cases, and it is correspondingly more complex.
61  *
62  * ## Bindings
63  *
64  * We store information about the bound variables for each arm as part of the
65  * per-arm `ArmData` struct.  There is a mapping from identifiers to
66  * `BindingInfo` structs.  These structs contain the mode/id/type of the
67  * binding, but they also contain up to two LLVM values, called `llmatch` and
68  * `llbinding` respectively (the `llbinding`, as will be described shortly, is
69  * optional and only present for by-value bindings---therefore it is bundled
70  * up as part of the `TransBindingMode` type).  Both point at allocas.
71  *
72  * The `llmatch` binding always stores a pointer into the value being matched
73  * which points at the data for the binding.  If the value being matched has
74  * type `T`, then, `llmatch` will point at an alloca of type `T*` (and hence
75  * `llmatch` has type `T**`).  So, if you have a pattern like:
76  *
77  *    let a: A = ...;
78  *    let b: B = ...;
79  *    match (a, b) { (ref c, copy d) => { ... } }
80  *
81  * For `c` and `d`, we would generate allocas of type `C*` and `D*`
82  * respectively.  These are called the `llmatch`.  As we match, when we come
83  * up against an identifier, we store the current pointer into the
84  * corresponding alloca.
85  *
86  * In addition, for each by-value binding (copy or move), we will create a
87  * second alloca (`llbinding`) that will hold the final value.  In this
88  * example, that means that `d` would have this second alloca of type `D` (and
89  * hence `llbinding` has type `D*`).
90  *
91  * Once a pattern is completely matched, and assuming that there is no guard
92  * pattern, we will branch to a block that leads to the body itself.  For any
93  * by-value bindings, this block will first load the ptr from `llmatch` (the
94  * one of type `D*`) and copy/move the value into `llbinding` (the one of type
95  * `D`).  The second alloca then becomes the value of the local variable.  For
96  * by ref bindings, the value of the local variable is simply the first
97  * alloca.
98  *
99  * So, for the example above, we would generate a setup kind of like this:
100  *
101  *        +-------+
102  *        | Entry |
103  *        +-------+
104  *            |
105  *        +-------------------------------------------+
106  *        | llmatch_c = (addr of first half of tuple) |
107  *        | llmatch_d = (addr of first half of tuple) |
108  *        +-------------------------------------------+
109  *            |
110  *        +--------------------------------------+
111  *        | *llbinding_d = **llmatch_dlbinding_d |
112  *        +--------------------------------------+
113  *
114  * If there is a guard, the situation is slightly different, because we must
115  * execute the guard code.  Moreover, we need to do so once for each of the
116  * alternatives that lead to the arm, because if the guard fails, they may
117  * have different points from which to continue the search. Therefore, in that
118  * case, we generate code that looks more like:
119  *
120  *        +-------+
121  *        | Entry |
122  *        +-------+
123  *            |
124  *        +-------------------------------------------+
125  *        | llmatch_c = (addr of first half of tuple) |
126  *        | llmatch_d = (addr of first half of tuple) |
127  *        +-------------------------------------------+
128  *            |
129  *        +-------------------------------------------------+
130  *        | *llbinding_d = **llmatch_dlbinding_d            |
131  *        | check condition                                 |
132  *        | if false { free *llbinding_d, goto next case }  |
133  *        | if true { goto body }                           |
134  *        +-------------------------------------------------+
135  *
136  * The handling for the cleanups is a bit... sensitive.  Basically, the body
137  * is the one that invokes `add_clean()` for each binding.  During the guard
138  * evaluation, we add temporary cleanups and revoke them after the guard is
139  * evaluated (it could fail, after all).  Presuming the guard fails, we drop
140  * the various values we copied explicitly.  Note that guards and moves are
141  * just plain incompatible.
142  *
143  */
144
145 use back::abi;
146 use lib::llvm::{llvm, ValueRef, BasicBlockRef};
147 use middle::const_eval;
148 use middle::borrowck::root_map_key;
149 use middle::pat_util::*;
150 use middle::resolve::DefMap;
151 use middle::trans::adt;
152 use middle::trans::base::*;
153 use middle::trans::build::*;
154 use middle::trans::callee;
155 use middle::trans::common::*;
156 use middle::trans::consts;
157 use middle::trans::controlflow;
158 use middle::trans::datum;
159 use middle::trans::datum::*;
160 use middle::trans::expr::Dest;
161 use middle::trans::expr;
162 use middle::trans::glue;
163 use middle::trans::tvec;
164 use middle::trans::type_of;
165 use middle::ty;
166 use util::common::indenter;
167
168 use core::hashmap::HashMap;
169 use syntax::ast;
170 use syntax::ast::ident;
171 use syntax::ast_util::path_to_ident;
172 use syntax::ast_util;
173 use syntax::codemap::{span, dummy_sp};
174 use syntax::print::pprust::pat_to_str;
175
176 // An option identifying a literal: either a unit-like struct or an
177 // expression.
178 pub enum Lit {
179     UnitLikeStructLit(ast::node_id),    // the node ID of the pattern
180     ExprLit(@ast::expr),
181     ConstLit(ast::def_id),              // the def ID of the constant
182 }
183
184 // An option identifying a branch (either a literal, a enum variant or a
185 // range)
186 pub enum Opt {
187     lit(Lit),
188     var(/* disr val */int, @adt::Repr),
189     range(@ast::expr, @ast::expr),
190     vec_len_eq(uint),
191     vec_len_ge(uint, /* slice */uint)
192 }
193
194 pub fn opt_eq(tcx: ty::ctxt, a: &Opt, b: &Opt) -> bool {
195     match (a, b) {
196       (&lit(a), &lit(b)) => {
197         match (a, b) {
198             (UnitLikeStructLit(a), UnitLikeStructLit(b)) => a == b,
199             _ => {
200                 let a_expr;
201                 match a {
202                     ExprLit(existing_a_expr) => a_expr = existing_a_expr,
203                     ConstLit(a_const) => {
204                         let e = const_eval::lookup_const_by_id(tcx, a_const);
205                         a_expr = e.get();
206                     }
207                     UnitLikeStructLit(_) => {
208                         fail!(~"UnitLikeStructLit should have been handled \
209                                above")
210                     }
211                 }
212
213                 let b_expr;
214                 match b {
215                     ExprLit(existing_b_expr) => b_expr = existing_b_expr,
216                     ConstLit(b_const) => {
217                         let e = const_eval::lookup_const_by_id(tcx, b_const);
218                         b_expr = e.get();
219                     }
220                     UnitLikeStructLit(_) => {
221                         fail!(~"UnitLikeStructLit should have been handled \
222                                above")
223                     }
224                 }
225
226                 const_eval::compare_lit_exprs(tcx, a_expr, b_expr) == 0
227             }
228         }
229       }
230       (&range(a1, a2), &range(b1, b2)) => {
231         const_eval::compare_lit_exprs(tcx, a1, b1) == 0 &&
232         const_eval::compare_lit_exprs(tcx, a2, b2) == 0
233       }
234       (&var(a, _), &var(b, _)) => a == b,
235       (&vec_len_eq(a), &vec_len_eq(b)) => a == b,
236       (&vec_len_ge(a, _), &vec_len_ge(b, _)) => a == b,
237       _ => false
238     }
239 }
240
241 pub enum opt_result {
242     single_result(Result),
243     lower_bound(Result),
244     range_result(Result, Result),
245 }
246 pub fn trans_opt(bcx: block, o: &Opt) -> opt_result {
247     let _icx = bcx.insn_ctxt("match::trans_opt");
248     let ccx = bcx.ccx();
249     let bcx = bcx;
250     match *o {
251         lit(ExprLit(lit_expr)) => {
252             let datumblock = expr::trans_to_datum(bcx, lit_expr);
253             return single_result(datumblock.to_result());
254         }
255         lit(UnitLikeStructLit(pat_id)) => {
256             let struct_ty = ty::node_id_to_type(bcx.tcx(), pat_id);
257             let datumblock = datum::scratch_datum(bcx, struct_ty, true);
258             return single_result(datumblock.to_result(bcx));
259         }
260         lit(ConstLit(lit_id)) => {
261             let llval = consts::get_const_val(bcx.ccx(), lit_id);
262             return single_result(rslt(bcx, llval));
263         }
264         var(disr_val, repr) => {
265             return adt::trans_case(bcx, repr, disr_val);
266         }
267         range(l1, l2) => {
268             return range_result(rslt(bcx, consts::const_expr(ccx, l1)),
269                                 rslt(bcx, consts::const_expr(ccx, l2)));
270         }
271         vec_len_eq(n) => {
272             return single_result(rslt(bcx, C_int(ccx, n as int)));
273         }
274         vec_len_ge(n, _) => {
275             return lower_bound(rslt(bcx, C_int(ccx, n as int)));
276         }
277     }
278 }
279
280 pub fn variant_opt(bcx: block, pat_id: ast::node_id)
281     -> Opt {
282     let ccx = bcx.ccx();
283     match *ccx.tcx.def_map.get(&pat_id) {
284         ast::def_variant(enum_id, var_id) => {
285             let variants = ty::enum_variants(ccx.tcx, enum_id);
286             for vec::each(*variants) |v| {
287                 if var_id == v.id {
288                     return var(v.disr_val,
289                                adt::represent_node(bcx, pat_id))
290                 }
291             }
292             ::core::util::unreachable();
293         }
294         ast::def_fn(*) |
295         ast::def_struct(_) => {
296             return lit(UnitLikeStructLit(pat_id));
297         }
298         _ => {
299             ccx.sess.bug(~"non-variant or struct in variant_opt()");
300         }
301     }
302 }
303
304 pub enum TransBindingMode {
305     TrByValue(/*ismove:*/ bool, /*llbinding:*/ ValueRef),
306     TrByRef,
307     TrByImplicitRef
308 }
309
310 /**
311  * Information about a pattern binding:
312  * - `llmatch` is a pointer to a stack slot.  The stack slot contains a
313  *   pointer into the value being matched.  Hence, llmatch has type `T**`
314  *   where `T` is the value being matched.
315  * - `trmode` is the trans binding mode
316  * - `id` is the node id of the binding
317  * - `ty` is the Rust type of the binding */
318 pub struct BindingInfo {
319     llmatch: ValueRef,
320     trmode: TransBindingMode,
321     id: ast::node_id,
322     ty: ty::t,
323 }
324
325 pub type BindingsMap = HashMap<ident, BindingInfo>;
326
327 pub struct ArmData<'self> {
328     bodycx: block,
329     arm: &'self ast::arm,
330     bindings_map: BindingsMap
331 }
332
333 pub struct Match<'self> {
334     pats: ~[@ast::pat],
335     data: @ArmData<'self>
336 }
337
338 pub fn match_to_str(bcx: block, m: &Match) -> ~str {
339     if bcx.sess().verbose() {
340         // for many programs, this just take too long to serialize
341         fmt!("%?", m.pats.map(|p| pat_to_str(*p, bcx.sess().intr())))
342     } else {
343         fmt!("%u pats", m.pats.len())
344     }
345 }
346
347 pub fn matches_to_str(bcx: block, m: &[@Match]) -> ~str {
348     fmt!("%?", m.map(|n| match_to_str(bcx, *n)))
349 }
350
351 pub fn has_nested_bindings(m: &[@Match], col: uint) -> bool {
352     for vec::each(m) |br| {
353         match br.pats[col].node {
354           ast::pat_ident(_, _, Some(_)) => return true,
355           _ => ()
356         }
357     }
358     return false;
359 }
360
361 pub fn expand_nested_bindings<'r>(bcx: block,
362                                   m: &[@Match<'r>],
363                                   col: uint,
364                                   val: ValueRef)
365                               -> ~[@Match<'r>] {
366     debug!("expand_nested_bindings(bcx=%s, m=%s, col=%u, val=%?)",
367            bcx.to_str(),
368            matches_to_str(bcx, m),
369            col,
370            bcx.val_str(val));
371     let _indenter = indenter();
372
373     do m.map |br| {
374         match br.pats[col].node {
375             ast::pat_ident(_, path, Some(inner)) => {
376                 let pats = vec::append(
377                     vec::slice(br.pats, 0u, col).to_vec(),
378                     vec::append(~[inner],
379                                 vec::slice(br.pats, col + 1u,
380                                            br.pats.len())));
381
382                 let binding_info =
383                     br.data.bindings_map.get(&path_to_ident(path));
384
385                 Store(bcx, val, binding_info.llmatch);
386                 @Match {pats: pats, data: br.data}
387             }
388             _ => {
389                 *br
390             }
391         }
392     }
393 }
394
395 pub type enter_pat<'self> = &'self fn(@ast::pat) -> Option<~[@ast::pat]>;
396
397 pub fn assert_is_binding_or_wild(bcx: block, p: @ast::pat) {
398     if !pat_is_binding_or_wild(bcx.tcx().def_map, p) {
399         bcx.sess().span_bug(
400             p.span,
401             fmt!("Expected an identifier pattern but found p: %s",
402                  pat_to_str(p, bcx.sess().intr())));
403     }
404 }
405
406 pub fn enter_match<'r>(bcx: block,
407                        dm: DefMap,
408                        m: &[@Match<'r>],
409                        col: uint,
410                        val: ValueRef,
411                        e: enter_pat)
412                     -> ~[@Match<'r>] {
413     debug!("enter_match(bcx=%s, m=%s, col=%u, val=%?)",
414            bcx.to_str(),
415            matches_to_str(bcx, m),
416            col,
417            bcx.val_str(val));
418     let _indenter = indenter();
419
420     let mut result = ~[];
421     for vec::each(m) |br| {
422         match e(br.pats[col]) {
423             Some(sub) => {
424                 let pats =
425                     vec::append(
426                         vec::append(sub, vec::slice(br.pats, 0u, col)),
427                         vec::slice(br.pats, col + 1u, br.pats.len()));
428
429                 let self = br.pats[col];
430                 match self.node {
431                     ast::pat_ident(_, path, None) => {
432                         if pat_is_binding(dm, self) {
433                             let binding_info =
434                                 br.data.bindings_map.get(
435                                     &path_to_ident(path));
436                             Store(bcx, val, binding_info.llmatch);
437                         }
438                     }
439                     _ => {}
440                 }
441
442                 result.push(@Match {pats: pats, data: br.data});
443             }
444             None => ()
445         }
446     }
447
448     debug!("result=%s", matches_to_str(bcx, result));
449
450     return result;
451 }
452
453 pub fn enter_default<'r>(bcx: block,
454                          dm: DefMap,
455                          m: &[@Match<'r>],
456                          col: uint,
457                          val: ValueRef)
458                       -> ~[@Match<'r>] {
459     debug!("enter_default(bcx=%s, m=%s, col=%u, val=%?)",
460            bcx.to_str(),
461            matches_to_str(bcx, m),
462            col,
463            bcx.val_str(val));
464     let _indenter = indenter();
465
466     do enter_match(bcx, dm, m, col, val) |p| {
467         match p.node {
468           ast::pat_wild | ast::pat_tup(_) | ast::pat_struct(*) => Some(~[]),
469           ast::pat_ident(_, _, None) if pat_is_binding(dm, p) => Some(~[]),
470           _ => None
471         }
472     }
473 }
474
475 // <pcwalton> nmatsakis: what does enter_opt do?
476 // <pcwalton> in trans/match
477 // <pcwalton> trans/match.rs is like stumbling around in a dark cave
478 // <nmatsakis> pcwalton: the enter family of functions adjust the set of
479 //             patterns as needed
480 // <nmatsakis> yeah, at some point I kind of achieved some level of
481 //             understanding
482 // <nmatsakis> anyhow, they adjust the patterns given that something of that
483 //             kind has been found
484 // <nmatsakis> pcwalton: ok, right, so enter_XXX() adjusts the patterns, as I
485 //             said
486 // <nmatsakis> enter_match() kind of embodies the generic code
487 // <nmatsakis> it is provided with a function that tests each pattern to see
488 //             if it might possibly apply and so forth
489 // <nmatsakis> so, if you have a pattern like {a: _, b: _, _} and one like _
490 // <nmatsakis> then _ would be expanded to (_, _)
491 // <nmatsakis> one spot for each of the sub-patterns
492 // <nmatsakis> enter_opt() is one of the more complex; it covers the fallible
493 //             cases
494 // <nmatsakis> enter_rec_or_struct() or enter_tuple() are simpler, since they
495 //             are infallible patterns
496 // <nmatsakis> so all patterns must either be records (resp. tuples) or
497 //             wildcards
498
499 pub fn enter_opt<'r>(bcx: block,
500                      m: &[@Match<'r>],
501                      opt: &Opt,
502                      col: uint,
503                      variant_size: uint,
504                      val: ValueRef)
505                   -> ~[@Match<'r>] {
506     debug!("enter_opt(bcx=%s, m=%s, col=%u, val=%?)",
507            bcx.to_str(),
508            matches_to_str(bcx, m),
509            col,
510            bcx.val_str(val));
511     let _indenter = indenter();
512
513     let tcx = bcx.tcx();
514     let dummy = @ast::pat {id: 0, node: ast::pat_wild, span: dummy_sp()};
515     do enter_match(bcx, tcx.def_map, m, col, val) |p| {
516         match p.node {
517             ast::pat_enum(*) |
518             ast::pat_ident(_, _, None) if pat_is_const(tcx.def_map, p) => {
519                 let const_def = *tcx.def_map.get(&p.id);
520                 let const_def_id = ast_util::def_id_of_def(const_def);
521                 if opt_eq(tcx, &lit(ConstLit(const_def_id)), opt) {
522                     Some(~[])
523                 } else {
524                     None
525                 }
526             }
527             ast::pat_enum(_, ref subpats) => {
528                 if opt_eq(tcx, &variant_opt(bcx, p.id), opt) {
529                     match *subpats {
530                         None => Some(vec::from_elem(variant_size, dummy)),
531                         _ => copy *subpats
532                     }
533                 } else {
534                     None
535                 }
536             }
537             ast::pat_ident(_, _, None)
538                     if pat_is_variant_or_struct(tcx.def_map, p) => {
539                 if opt_eq(tcx, &variant_opt(bcx, p.id), opt) {
540                     Some(~[])
541                 } else {
542                     None
543                 }
544             }
545             ast::pat_lit(l) => {
546                 if opt_eq(tcx, &lit(ExprLit(l)), opt) {Some(~[])} else {None}
547             }
548             ast::pat_range(l1, l2) => {
549                 if opt_eq(tcx, &range(l1, l2), opt) {Some(~[])} else {None}
550             }
551             ast::pat_struct(_, ref field_pats, _) => {
552                 if opt_eq(tcx, &variant_opt(bcx, p.id), opt) {
553                     // Look up the struct variant ID.
554                     let struct_id;
555                     match *tcx.def_map.get(&p.id) {
556                         ast::def_variant(_, found_struct_id) => {
557                             struct_id = found_struct_id;
558                         }
559                         _ => {
560                             tcx.sess.span_bug(p.span, ~"expected enum \
561                                                         variant def");
562                         }
563                     }
564
565                     // Reorder the patterns into the same order they were
566                     // specified in the struct definition. Also fill in
567                     // unspecified fields with dummy.
568                     let mut reordered_patterns = ~[];
569                     for ty::lookup_struct_fields(tcx, struct_id).each
570                         |field| {
571                             match field_pats.find(|p|
572                                                   p.ident == field.ident) {
573                                 None => reordered_patterns.push(dummy),
574                                 Some(fp) => reordered_patterns.push(fp.pat)
575                             }
576                     }
577                     Some(reordered_patterns)
578                 } else {
579                     None
580                 }
581             }
582             ast::pat_vec(ref before, slice, ref after) => {
583                 match slice {
584                     Some(slice) => {
585                         let n = before.len() + after.len();
586                         let i = before.len();
587                         if opt_eq(tcx, &vec_len_ge(n, i), opt) {
588                             Some(vec::append_one(copy *before, slice) +
589                                     *after)
590                         } else {
591                             None
592                         }
593                     }
594                     None => {
595                         let n = before.len();
596                         if opt_eq(tcx, &vec_len_eq(n), opt) {
597                             Some(copy *before)
598                         } else {
599                             None
600                         }
601                     }
602                 }
603             }
604             _ => {
605                 assert_is_binding_or_wild(bcx, p);
606                 Some(vec::from_elem(variant_size, dummy))
607             }
608         }
609     }
610 }
611
612 pub fn enter_rec_or_struct<'r>(bcx: block,
613                                dm: DefMap,
614                                m: &[@Match<'r>],
615                                col: uint,
616                                fields: &[ast::ident],
617                                val: ValueRef)
618                             -> ~[@Match<'r>] {
619     debug!("enter_rec_or_struct(bcx=%s, m=%s, col=%u, val=%?)",
620            bcx.to_str(),
621            matches_to_str(bcx, m),
622            col,
623            bcx.val_str(val));
624     let _indenter = indenter();
625
626     let dummy = @ast::pat {id: 0, node: ast::pat_wild, span: dummy_sp()};
627     do enter_match(bcx, dm, m, col, val) |p| {
628         match p.node {
629             ast::pat_struct(_, ref fpats, _) => {
630                 let mut pats = ~[];
631                 for fields.each |fname| {
632                     match fpats.find(|p| p.ident == *fname) {
633                         None => pats.push(dummy),
634                         Some(pat) => pats.push(pat.pat)
635                     }
636                 }
637                 Some(pats)
638             }
639             _ => {
640                 assert_is_binding_or_wild(bcx, p);
641                 Some(vec::from_elem(fields.len(), dummy))
642             }
643         }
644     }
645 }
646
647 pub fn enter_tup<'r>(bcx: block,
648                      dm: DefMap,
649                      m: &[@Match<'r>],
650                      col: uint,
651                      val: ValueRef,
652                      n_elts: uint)
653                   -> ~[@Match<'r>] {
654     debug!("enter_tup(bcx=%s, m=%s, col=%u, val=%?)",
655            bcx.to_str(),
656            matches_to_str(bcx, m),
657            col,
658            bcx.val_str(val));
659     let _indenter = indenter();
660
661     let dummy = @ast::pat {id: 0, node: ast::pat_wild, span: dummy_sp()};
662     do enter_match(bcx, dm, m, col, val) |p| {
663         match p.node {
664             ast::pat_tup(/*bad*/copy elts) => {
665                 Some(elts)
666             }
667             _ => {
668                 assert_is_binding_or_wild(bcx, p);
669                 Some(vec::from_elem(n_elts, dummy))
670             }
671         }
672     }
673 }
674
675 pub fn enter_tuple_struct<'r>(bcx: block,
676                               dm: DefMap,
677                               m: &[@Match<'r>],
678                               col: uint,
679                               val: ValueRef,
680                               n_elts: uint)
681                           -> ~[@Match<'r>] {
682     debug!("enter_tuple_struct(bcx=%s, m=%s, col=%u, val=%?)",
683            bcx.to_str(),
684            matches_to_str(bcx, m),
685            col,
686            bcx.val_str(val));
687     let _indenter = indenter();
688
689     let dummy = @ast::pat {id: 0, node: ast::pat_wild, span: dummy_sp()};
690     do enter_match(bcx, dm, m, col, val) |p| {
691         match p.node {
692             ast::pat_enum(_, Some(/*bad*/copy elts)) => Some(elts),
693             _ => {
694                 assert_is_binding_or_wild(bcx, p);
695                 Some(vec::from_elem(n_elts, dummy))
696             }
697         }
698     }
699 }
700
701 pub fn enter_box<'r>(bcx: block,
702                      dm: DefMap,
703                      m: &[@Match<'r>],
704                      col: uint,
705                      val: ValueRef)
706                  -> ~[@Match<'r>] {
707     debug!("enter_box(bcx=%s, m=%s, col=%u, val=%?)",
708            bcx.to_str(),
709            matches_to_str(bcx, m),
710            col,
711            bcx.val_str(val));
712     let _indenter = indenter();
713
714     let dummy = @ast::pat {id: 0, node: ast::pat_wild, span: dummy_sp()};
715     do enter_match(bcx, dm, m, col, val) |p| {
716         match p.node {
717             ast::pat_box(sub) => {
718                 Some(~[sub])
719             }
720             _ => {
721                 assert_is_binding_or_wild(bcx, p);
722                 Some(~[dummy])
723             }
724         }
725     }
726 }
727
728 pub fn enter_uniq<'r>(bcx: block,
729                       dm: DefMap,
730                       m: &[@Match<'r>],
731                       col: uint,
732                       val: ValueRef)
733                   -> ~[@Match<'r>] {
734     debug!("enter_uniq(bcx=%s, m=%s, col=%u, val=%?)",
735            bcx.to_str(),
736            matches_to_str(bcx, m),
737            col,
738            bcx.val_str(val));
739     let _indenter = indenter();
740
741     let dummy = @ast::pat {id: 0, node: ast::pat_wild, span: dummy_sp()};
742     do enter_match(bcx, dm, m, col, val) |p| {
743         match p.node {
744             ast::pat_uniq(sub) => {
745                 Some(~[sub])
746             }
747             _ => {
748                 assert_is_binding_or_wild(bcx, p);
749                 Some(~[dummy])
750             }
751         }
752     }
753 }
754
755 pub fn enter_region<'r>(bcx: block,
756                         dm: DefMap,
757                         m: &[@Match<'r>],
758                         col: uint,
759                         val: ValueRef)
760                     -> ~[@Match<'r>] {
761     debug!("enter_region(bcx=%s, m=%s, col=%u, val=%?)",
762            bcx.to_str(),
763            matches_to_str(bcx, m),
764            col,
765            bcx.val_str(val));
766     let _indenter = indenter();
767
768     let dummy = @ast::pat { id: 0, node: ast::pat_wild, span: dummy_sp() };
769     do enter_match(bcx, dm, m, col, val) |p| {
770         match p.node {
771             ast::pat_region(sub) => {
772                 Some(~[sub])
773             }
774             _ => {
775                 assert_is_binding_or_wild(bcx, p);
776                 Some(~[dummy])
777             }
778         }
779     }
780 }
781
782 // Returns the options in one column of matches. An option is something that
783 // needs to be conditionally matched at runtime; for example, the discriminant
784 // on a set of enum variants or a literal.
785 pub fn get_options(bcx: block, m: &[@Match], col: uint) -> ~[Opt] {
786     let ccx = bcx.ccx();
787     fn add_to_set(tcx: ty::ctxt, set: &mut ~[Opt], val: Opt) {
788         if set.any(|l| opt_eq(tcx, l, &val)) {return;}
789         set.push(val);
790     }
791
792     let mut found = ~[];
793     for m.each |br| {
794         let cur = br.pats[col];
795         match cur.node {
796             ast::pat_lit(l) => {
797                 add_to_set(ccx.tcx, &mut found, lit(ExprLit(l)));
798             }
799             ast::pat_ident(*) => {
800                 // This is one of: an enum variant, a unit-like struct, or a
801                 // variable binding.
802                 match ccx.tcx.def_map.find(&cur.id) {
803                     Some(&ast::def_variant(*)) => {
804                         add_to_set(ccx.tcx, &mut found,
805                                    variant_opt(bcx, cur.id));
806                     }
807                     Some(&ast::def_struct(*)) => {
808                         add_to_set(ccx.tcx, &mut found,
809                                    lit(UnitLikeStructLit(cur.id)));
810                     }
811                     Some(&ast::def_const(const_did)) => {
812                         add_to_set(ccx.tcx, &mut found,
813                                    lit(ConstLit(const_did)));
814                     }
815                     _ => {}
816                 }
817             }
818             ast::pat_enum(*) | ast::pat_struct(*) => {
819                 // This could be one of: a tuple-like enum variant, a
820                 // struct-like enum variant, or a struct.
821                 match ccx.tcx.def_map.find(&cur.id) {
822                     Some(&ast::def_fn(*)) |
823                     Some(&ast::def_variant(*)) => {
824                         add_to_set(ccx.tcx, &mut found,
825                                    variant_opt(bcx, cur.id));
826                     }
827                     Some(&ast::def_const(const_did)) => {
828                         add_to_set(ccx.tcx, &mut found,
829                                    lit(ConstLit(const_did)));
830                     }
831                     _ => {}
832                 }
833             }
834             ast::pat_range(l1, l2) => {
835                 add_to_set(ccx.tcx, &mut found, range(l1, l2));
836             }
837             ast::pat_vec(ref before, slice, ref after) => {
838                 let opt = match slice {
839                     None => vec_len_eq(before.len()),
840                     Some(_) => vec_len_ge(before.len() + after.len(),
841                                           before.len())
842                 };
843                 add_to_set(ccx.tcx, &mut found, opt);
844             }
845             _ => {}
846         }
847     }
848     return found;
849 }
850
851 pub struct ExtractedBlock {
852     vals: ~[ValueRef],
853     bcx: block
854 }
855
856 pub fn extract_variant_args(bcx: block,
857                             repr: &adt::Repr,
858                             disr_val: int,
859                             val: ValueRef)
860     -> ExtractedBlock {
861     let _icx = bcx.insn_ctxt("match::extract_variant_args");
862     let args = do vec::from_fn(adt::num_args(repr, disr_val)) |i| {
863         adt::trans_field_ptr(bcx, repr, val, disr_val, i)
864     };
865
866     ExtractedBlock { vals: args, bcx: bcx }
867 }
868
869 pub fn extract_vec_elems(bcx: block,
870                          pat_id: ast::node_id,
871                          elem_count: uint,
872                          slice: Option<uint>,
873                          val: ValueRef,
874                          count: ValueRef)
875                       -> ExtractedBlock {
876     let _icx = bcx.insn_ctxt("match::extract_vec_elems");
877     let vt = tvec::vec_types(bcx, node_id_type(bcx, pat_id));
878     let unboxed = load_if_immediate(bcx, val, vt.vec_ty);
879     let (base, len) = tvec::get_base_and_len(bcx, unboxed, vt.vec_ty);
880
881     let mut elems = do vec::from_fn(elem_count) |i| {
882         match slice {
883             None => GEPi(bcx, base, ~[i]),
884             Some(n) if i < n => GEPi(bcx, base, ~[i]),
885             Some(n) if i > n => {
886                 InBoundsGEP(bcx, base, ~[
887                     Sub(bcx, count,
888                         C_int(bcx.ccx(), (elem_count - i) as int))])
889             }
890             _ => unsafe { llvm::LLVMGetUndef(vt.llunit_ty) }
891         }
892     };
893     if slice.is_some() {
894         let n = slice.get();
895         let slice_offset = Mul(bcx, vt.llunit_size,
896             C_int(bcx.ccx(), n as int)
897         );
898         let slice_begin = tvec::pointer_add(bcx, base, slice_offset);
899         let slice_len_offset = Mul(bcx, vt.llunit_size,
900             C_int(bcx.ccx(), (elem_count - 1u) as int)
901         );
902         let slice_len = Sub(bcx, len, slice_len_offset);
903         let slice_ty = ty::mk_evec(bcx.tcx(),
904             ty::mt {ty: vt.unit_ty, mutbl: ast::m_imm},
905             ty::vstore_slice(ty::re_static)
906         );
907         let scratch = scratch_datum(bcx, slice_ty, false);
908         Store(bcx, slice_begin,
909             GEPi(bcx, scratch.val, [0u, abi::slice_elt_base])
910         );
911         Store(bcx, slice_len,
912             GEPi(bcx, scratch.val, [0u, abi::slice_elt_len])
913         );
914         elems[n] = scratch.val;
915         scratch.add_clean(bcx);
916     }
917
918     ExtractedBlock { vals: elems, bcx: bcx }
919 }
920
921 // NB: This function does not collect fields from struct-like enum variants.
922 pub fn collect_record_or_struct_fields(bcx: block,
923                                        m: &[@Match],
924                                        col: uint)
925                                     -> ~[ast::ident] {
926     let mut fields: ~[ast::ident] = ~[];
927     for vec::each(m) |br| {
928         match br.pats[col].node {
929           ast::pat_struct(_, ref fs, _) => {
930             match ty::get(node_id_type(bcx, br.pats[col].id)).sty {
931               ty::ty_struct(*) => extend(&mut fields, *fs),
932               _ => ()
933             }
934           }
935           _ => ()
936         }
937     }
938     return fields;
939
940     fn extend(idents: &mut ~[ast::ident], field_pats: &[ast::field_pat]) {
941         for field_pats.each |field_pat| {
942             let field_ident = field_pat.ident;
943             if !vec::any(*idents, |x| *x == field_ident) {
944                 idents.push(field_ident);
945             }
946         }
947     }
948 }
949
950 pub fn root_pats_as_necessary(bcx: block,
951                               m: &[@Match],
952                               col: uint,
953                               val: ValueRef)
954                            -> block {
955     let mut bcx = bcx;
956     for vec::each(m) |br| {
957         let pat_id = br.pats[col].id;
958
959         let key = root_map_key {id: pat_id, derefs: 0u };
960         match bcx.ccx().maps.root_map.find(&key) {
961             None => (),
962             Some(&root_info) => {
963                 // Note: the scope_id will always be the id of the match.  See
964                 // the extended comment in rustc::middle::borrowck::preserve()
965                 // for details (look for the case covering cat_discr).
966
967                 let datum = Datum {val: val, ty: node_id_type(bcx, pat_id),
968                                    mode: ByRef, source: ZeroMem};
969                 bcx = datum.root(bcx, root_info);
970                 // If we kept going, we'd only re-root the same value, so
971                 // return now.
972                 return bcx;
973             }
974         }
975     }
976     return bcx;
977 }
978
979 // Macro for deciding whether any of the remaining matches fit a given kind of
980 // pattern.  Note that, because the macro is well-typed, either ALL of the
981 // matches should fit that sort of pattern or NONE (however, some of the
982 // matches may be wildcards like _ or identifiers).
983 macro_rules! any_pat (
984     ($m:expr, $pattern:pat) => (
985         vec::any($m, |br| {
986             match br.pats[col].node {
987                 $pattern => true,
988                 _ => false
989             }
990         })
991     )
992 )
993
994 pub fn any_box_pat(m: &[@Match], col: uint) -> bool {
995     any_pat!(m, ast::pat_box(_))
996 }
997
998 pub fn any_uniq_pat(m: &[@Match], col: uint) -> bool {
999     any_pat!(m, ast::pat_uniq(_))
1000 }
1001
1002 pub fn any_region_pat(m: &[@Match], col: uint) -> bool {
1003     any_pat!(m, ast::pat_region(_))
1004 }
1005
1006 pub fn any_tup_pat(m: &[@Match], col: uint) -> bool {
1007     any_pat!(m, ast::pat_tup(_))
1008 }
1009
1010 pub fn any_tuple_struct_pat(bcx: block, m: &[@Match], col: uint) -> bool {
1011     vec::any(m, |br| {
1012         let pat = br.pats[col];
1013         match pat.node {
1014             ast::pat_enum(_, Some(_)) => {
1015                 match bcx.tcx().def_map.find(&pat.id) {
1016                     Some(&ast::def_fn(*)) |
1017                     Some(&ast::def_struct(*)) => true,
1018                     _ => false
1019                 }
1020             }
1021             _ => false
1022         }
1023     })
1024 }
1025
1026 pub type mk_fail = @fn() -> BasicBlockRef;
1027
1028 pub fn pick_col(m: &[@Match]) -> uint {
1029     fn score(p: @ast::pat) -> uint {
1030         match p.node {
1031           ast::pat_lit(_) | ast::pat_enum(_, _) | ast::pat_range(_, _) => 1u,
1032           ast::pat_ident(_, _, Some(p)) => score(p),
1033           _ => 0u
1034         }
1035     }
1036     let mut scores = vec::from_elem(m[0].pats.len(), 0u);
1037     for vec::each(m) |br| {
1038         let mut i = 0u;
1039         for vec::each(br.pats) |p| { scores[i] += score(*p); i += 1u; }
1040     }
1041     let mut max_score = 0u;
1042     let mut best_col = 0u;
1043     let mut i = 0u;
1044     for vec::each(scores) |score| {
1045         let score = *score;
1046
1047         // Irrefutable columns always go first, they'd only be duplicated in
1048         // the branches.
1049         if score == 0u { return i; }
1050         // If no irrefutable ones are found, we pick the one with the biggest
1051         // branching factor.
1052         if score > max_score { max_score = score; best_col = i; }
1053         i += 1u;
1054     }
1055     return best_col;
1056 }
1057
1058 #[deriving(Eq)]
1059 pub enum branch_kind { no_branch, single, switch, compare, compare_vec_len, }
1060
1061 // Compiles a comparison between two things.
1062 //
1063 // NB: This must produce an i1, not a Rust bool (i8).
1064 pub fn compare_values(cx: block,
1065                       lhs: ValueRef,
1066                       rhs: ValueRef,
1067                       rhs_t: ty::t)
1068                    -> Result {
1069     let _icx = cx.insn_ctxt("compare_values");
1070     if ty::type_is_scalar(rhs_t) {
1071       let rs = compare_scalar_types(cx, lhs, rhs, rhs_t, ast::eq);
1072       return rslt(rs.bcx, rs.val);
1073     }
1074
1075     match ty::get(rhs_t).sty {
1076         ty::ty_estr(ty::vstore_uniq) => {
1077             let scratch_result = scratch_datum(cx, ty::mk_bool(), false);
1078             let scratch_lhs = alloca(cx, val_ty(lhs));
1079             Store(cx, lhs, scratch_lhs);
1080             let scratch_rhs = alloca(cx, val_ty(rhs));
1081             Store(cx, rhs, scratch_rhs);
1082             let did = cx.tcx().lang_items.uniq_str_eq_fn();
1083             let bcx = callee::trans_lang_call(cx, did,
1084                                                         ~[scratch_lhs,
1085                                                           scratch_rhs],
1086                                                         expr::SaveIn(
1087                                                          scratch_result.val));
1088             let result = scratch_result.to_result(bcx);
1089             Result {
1090                 bcx: result.bcx,
1091                 val: bool_to_i1(result.bcx, result.val)
1092             }
1093         }
1094         ty::ty_estr(_) => {
1095             let scratch_result = scratch_datum(cx, ty::mk_bool(), false);
1096             let did = cx.tcx().lang_items.str_eq_fn();
1097             let bcx = callee::trans_lang_call(cx, did,
1098                                                         ~[lhs, rhs],
1099                                                         expr::SaveIn(
1100                                                          scratch_result.val));
1101             let result = scratch_result.to_result(bcx);
1102             Result {
1103                 bcx: result.bcx,
1104                 val: bool_to_i1(result.bcx, result.val)
1105             }
1106         }
1107         _ => {
1108             cx.tcx().sess.bug(~"only scalars and strings supported in \
1109                                 compare_values");
1110         }
1111     }
1112 }
1113
1114 pub fn store_non_ref_bindings(bcx: block,
1115                               data: &ArmData,
1116                               opt_temp_cleanups: Option<&mut ~[ValueRef]>)
1117                            -> block {
1118     /*!
1119      *
1120      * For each copy/move binding, copy the value from the value
1121      * being matched into its final home.  This code executes once
1122      * one of the patterns for a given arm has completely matched.
1123      * It adds temporary cleanups to the `temp_cleanups` array,
1124      * if one is provided.
1125      */
1126
1127     let mut bcx = bcx;
1128     for data.bindings_map.each_value |&binding_info| {
1129         match binding_info.trmode {
1130             TrByValue(is_move, lldest) => {
1131                 let llval = Load(bcx, binding_info.llmatch); // get a T*
1132                 let datum = Datum {val: llval, ty: binding_info.ty,
1133                                    mode: ByRef, source: ZeroMem};
1134                 bcx = {
1135                     if is_move {
1136                         datum.move_to(bcx, INIT, lldest)
1137                     } else {
1138                         datum.copy_to(bcx, INIT, lldest)
1139                     }
1140                 };
1141
1142                 for opt_temp_cleanups.each |temp_cleanups| {
1143                     add_clean_temp_mem(bcx, lldest, binding_info.ty);
1144                     temp_cleanups.push(lldest);
1145                 }
1146             }
1147             TrByRef | TrByImplicitRef => {}
1148         }
1149     }
1150     return bcx;
1151 }
1152
1153 pub fn insert_lllocals(bcx: block,
1154                        data: &ArmData,
1155                        add_cleans: bool) -> block {
1156     /*!
1157      *
1158      * For each binding in `data.bindings_map`, adds an appropriate entry into
1159      * the `fcx.lllocals` map.  If add_cleans is true, then adds cleanups for
1160      * the bindings. */
1161
1162     for data.bindings_map.each_value |&binding_info| {
1163         let llval = match binding_info.trmode {
1164             // By value bindings: use the stack slot that we
1165             // copied/moved the value into
1166             TrByValue(_, lldest) => {
1167                 if add_cleans {
1168                     add_clean(bcx, lldest, binding_info.ty);
1169                 }
1170
1171                 lldest
1172             }
1173
1174             // By ref binding: use the ptr into the matched value
1175             TrByRef => {
1176                 binding_info.llmatch
1177             }
1178
1179             // Ugly: for implicit ref, we actually want a T*, but
1180             // we have a T**, so we had to load.  This will go away
1181             // once implicit refs go away.
1182             TrByImplicitRef => {
1183                 Load(bcx, binding_info.llmatch)
1184             }
1185         };
1186
1187         bcx.fcx.lllocals.insert(binding_info.id,
1188                                 local_mem(llval));
1189     }
1190     return bcx;
1191 }
1192
1193 pub fn compile_guard(bcx: block,
1194                      guard_expr: @ast::expr,
1195                      data: &ArmData,
1196                      m: &[@Match],
1197                      vals: &[ValueRef],
1198                      chk: Option<mk_fail>)
1199                   -> block {
1200     debug!("compile_guard(bcx=%s, guard_expr=%s, m=%s, vals=%?)",
1201            bcx.to_str(),
1202            bcx.expr_to_str(guard_expr),
1203            matches_to_str(bcx, m),
1204            vals.map(|v| bcx.val_str(*v)));
1205     let _indenter = indenter();
1206
1207     let mut bcx = bcx;
1208     let mut temp_cleanups = ~[];
1209     bcx = store_non_ref_bindings(bcx, data, Some(&mut temp_cleanups));
1210     bcx = insert_lllocals(bcx, data, false);
1211
1212     let val = unpack_result!(bcx, {
1213         do with_scope_result(bcx, guard_expr.info(),
1214                              ~"guard") |bcx| {
1215             expr::trans_to_datum(bcx, guard_expr).to_result()
1216         }
1217     });
1218     let val = bool_to_i1(bcx, val);
1219
1220     // Revoke the temp cleanups now that the guard successfully executed.
1221     for temp_cleanups.each |llval| {
1222         revoke_clean(bcx, *llval);
1223     }
1224
1225     return do with_cond(bcx, Not(bcx, val)) |bcx| {
1226         // Guard does not match: free the values we copied,
1227         // and remove all bindings from the lllocals table
1228         let bcx = drop_bindings(bcx, data);
1229         compile_submatch(bcx, m, vals, chk);
1230         bcx
1231     };
1232
1233     fn drop_bindings(bcx: block, data: &ArmData) -> block {
1234         let mut bcx = bcx;
1235         for data.bindings_map.each_value |&binding_info| {
1236             match binding_info.trmode {
1237                 TrByValue(_, llval) => {
1238                     bcx = glue::drop_ty(bcx, llval, binding_info.ty);
1239                 }
1240                 TrByRef | TrByImplicitRef => {}
1241             }
1242             bcx.fcx.lllocals.remove(&binding_info.id);
1243         }
1244         return bcx;
1245     }
1246 }
1247
1248 pub fn compile_submatch(bcx: block,
1249                         m: &[@Match],
1250                         vals: &[ValueRef],
1251                         chk: Option<mk_fail>) {
1252     debug!("compile_submatch(bcx=%s, m=%s, vals=%?)",
1253            bcx.to_str(),
1254            matches_to_str(bcx, m),
1255            vals.map(|v| bcx.val_str(*v)));
1256     let _indenter = indenter();
1257
1258     /*
1259       For an empty match, a fall-through case must exist
1260      */
1261     assert!((m.len() > 0u || chk.is_some()));
1262     let _icx = bcx.insn_ctxt("match::compile_submatch");
1263     let mut bcx = bcx;
1264     let tcx = bcx.tcx(), dm = tcx.def_map;
1265     if m.len() == 0u {
1266         Br(bcx, chk.get()());
1267         return;
1268     }
1269     if m[0].pats.len() == 0u {
1270         let data = m[0].data;
1271         match data.arm.guard {
1272             Some(guard_expr) => {
1273                 bcx = compile_guard(bcx, guard_expr, m[0].data,
1274                                     vec::slice(m, 1, m.len()),
1275                                     vals, chk);
1276             }
1277             _ => ()
1278         }
1279         Br(bcx, data.bodycx.llbb);
1280         return;
1281     }
1282
1283     let col = pick_col(m);
1284     let val = vals[col];
1285     let m = {
1286         if has_nested_bindings(m, col) {
1287             expand_nested_bindings(bcx, m, col, val)
1288         } else {
1289             m.to_vec()
1290         }
1291     };
1292
1293     let vals_left = vec::append(vec::slice(vals, 0u, col).to_vec(),
1294                                 vec::slice(vals, col + 1u, vals.len()));
1295     let ccx = *bcx.fcx.ccx;
1296     let mut pat_id = 0;
1297     for vec::each(m) |br| {
1298         // Find a real id (we're adding placeholder wildcard patterns, but
1299         // each column is guaranteed to have at least one real pattern)
1300         if pat_id == 0 { pat_id = br.pats[col].id; }
1301     }
1302
1303     bcx = root_pats_as_necessary(bcx, m, col, val);
1304     let rec_fields = collect_record_or_struct_fields(bcx, m, col);
1305     if rec_fields.len() > 0 {
1306         let pat_ty = node_id_type(bcx, pat_id);
1307         let pat_repr = adt::represent_type(bcx.ccx(), pat_ty);
1308         do expr::with_field_tys(tcx, pat_ty, None) |discr, field_tys| {
1309             let rec_vals = rec_fields.map(|field_name| {
1310                 let ix = ty::field_idx_strict(tcx, *field_name, field_tys);
1311                 adt::trans_field_ptr(bcx, pat_repr, val, discr, ix)
1312             });
1313             compile_submatch(
1314                 bcx,
1315                 enter_rec_or_struct(bcx, dm, m, col, rec_fields, val),
1316                 vec::append(rec_vals, vals_left),
1317                 chk);
1318         }
1319         return;
1320     }
1321
1322     if any_tup_pat(m, col) {
1323         let tup_ty = node_id_type(bcx, pat_id);
1324         let tup_repr = adt::represent_type(bcx.ccx(), tup_ty);
1325         let n_tup_elts = match ty::get(tup_ty).sty {
1326           ty::ty_tup(ref elts) => elts.len(),
1327           _ => ccx.sess.bug(~"non-tuple type in tuple pattern")
1328         };
1329         let tup_vals = do vec::from_fn(n_tup_elts) |i| {
1330             adt::trans_field_ptr(bcx, tup_repr, val, 0, i)
1331         };
1332         compile_submatch(bcx, enter_tup(bcx, dm, m, col, val, n_tup_elts),
1333                          vec::append(tup_vals, vals_left), chk);
1334         return;
1335     }
1336
1337     if any_tuple_struct_pat(bcx, m, col) {
1338         let struct_ty = node_id_type(bcx, pat_id);
1339         let struct_element_count;
1340         match ty::get(struct_ty).sty {
1341             ty::ty_struct(struct_id, _) => {
1342                 struct_element_count =
1343                     ty::lookup_struct_fields(tcx, struct_id).len();
1344             }
1345             _ => {
1346                 ccx.sess.bug(~"non-struct type in tuple struct pattern");
1347             }
1348         }
1349
1350         let struct_repr = adt::represent_type(bcx.ccx(), struct_ty);
1351         let llstructvals = do vec::from_fn(struct_element_count) |i| {
1352             adt::trans_field_ptr(bcx, struct_repr, val, 0, i)
1353         };
1354         compile_submatch(bcx,
1355                          enter_tuple_struct(bcx, dm, m, col, val,
1356                                             struct_element_count),
1357                          vec::append(llstructvals, vals_left),
1358                          chk);
1359         return;
1360     }
1361
1362     // Unbox in case of a box field
1363     if any_box_pat(m, col) {
1364         let llbox = Load(bcx, val);
1365         let box_no_addrspace = non_gc_box_cast(bcx, llbox);
1366         let unboxed =
1367             GEPi(bcx, box_no_addrspace, [0u, abi::box_field_body]);
1368         compile_submatch(bcx, enter_box(bcx, dm, m, col, val),
1369                          vec::append(~[unboxed], vals_left), chk);
1370         return;
1371     }
1372
1373     if any_uniq_pat(m, col) {
1374         let llbox = Load(bcx, val);
1375         let box_no_addrspace = non_gc_box_cast(bcx, llbox);
1376         let unboxed =
1377             GEPi(bcx, box_no_addrspace, [0u, abi::box_field_body]);
1378         compile_submatch(bcx, enter_uniq(bcx, dm, m, col, val),
1379                          vec::append(~[unboxed], vals_left), chk);
1380         return;
1381     }
1382
1383     if any_region_pat(m, col) {
1384         let loaded_val = Load(bcx, val);
1385         compile_submatch(bcx, enter_region(bcx, dm, m, col, val),
1386                          vec::append(~[loaded_val], vals_left), chk);
1387         return;
1388     }
1389
1390     // Decide what kind of branch we need
1391     let opts = get_options(bcx, m, col);
1392     let mut kind = no_branch;
1393     let mut test_val = val;
1394     if opts.len() > 0u {
1395         match opts[0] {
1396             var(_, repr) => {
1397                 let (the_kind, val_opt) = adt::trans_switch(bcx, repr, val);
1398                 kind = the_kind;
1399                 for val_opt.each |&tval| { test_val = tval; }
1400             }
1401             lit(_) => {
1402                 let pty = node_id_type(bcx, pat_id);
1403                 test_val = load_if_immediate(bcx, val, pty);
1404                 kind = if ty::type_is_integral(pty) { switch }
1405                 else { compare };
1406             }
1407             range(_, _) => {
1408                 test_val = Load(bcx, val);
1409                 kind = compare;
1410             },
1411             vec_len_eq(*) | vec_len_ge(*) => {
1412                 let vt = tvec::vec_types(bcx, node_id_type(bcx, pat_id));
1413                 let unboxed = load_if_immediate(bcx, val, vt.vec_ty);
1414                 let (_, len) = tvec::get_base_and_len(
1415                     bcx, unboxed, vt.vec_ty
1416                 );
1417                 test_val = SDiv(bcx, len, vt.llunit_size);
1418                 kind = compare_vec_len;
1419             }
1420         }
1421     }
1422     for vec::each(opts) |o| {
1423         match *o {
1424             range(_, _) => { kind = compare; break }
1425             _ => ()
1426         }
1427     }
1428     let else_cx = match kind {
1429         no_branch | single => bcx,
1430         _ => sub_block(bcx, ~"match_else")
1431     };
1432     let sw = if kind == switch {
1433         Switch(bcx, test_val, else_cx.llbb, opts.len())
1434     } else {
1435         C_int(ccx, 0) // Placeholder for when not using a switch
1436     };
1437
1438     let defaults = enter_default(else_cx, dm, m, col, val);
1439     let exhaustive = chk.is_none() && defaults.len() == 0u;
1440     let len = opts.len();
1441     let mut i = 0u;
1442
1443     // Compile subtrees for each option
1444     for vec::each(opts) |opt| {
1445         i += 1u;
1446         let mut opt_cx = else_cx;
1447         if !exhaustive || i < len {
1448             opt_cx = sub_block(bcx, ~"match_case");
1449             match kind {
1450               single => Br(bcx, opt_cx.llbb),
1451               switch => {
1452                   match trans_opt(bcx, opt) {
1453                       single_result(r) => {
1454                         unsafe {
1455                           llvm::LLVMAddCase(sw, r.val, opt_cx.llbb);
1456                           bcx = r.bcx;
1457                         }
1458                       }
1459                       _ => {
1460                           bcx.sess().bug(
1461                               ~"in compile_submatch, expected \
1462                                 trans_opt to return a single_result")
1463                       }
1464                   }
1465               }
1466               compare => {
1467                   let t = node_id_type(bcx, pat_id);
1468                   let Result {bcx: after_cx, val: matches} = {
1469                       do with_scope_result(bcx, None,
1470                                            ~"compare_scope") |bcx| {
1471                           match trans_opt(bcx, opt) {
1472                               single_result(
1473                                   Result {bcx, val}) => {
1474                                   compare_values(bcx, test_val, val, t)
1475                               }
1476                               lower_bound(
1477                                   Result {bcx, val}) => {
1478                                   compare_scalar_types(
1479                                           bcx, test_val, val,
1480                                           t, ast::ge)
1481                               }
1482                               range_result(
1483                                   Result {val: vbegin, _},
1484                                   Result {bcx, val: vend}) => {
1485                                   let Result {bcx, val: llge} =
1486                                       compare_scalar_types(
1487                                           bcx, test_val,
1488                                           vbegin, t, ast::ge);
1489                                   let Result {bcx, val: llle} =
1490                                       compare_scalar_types(
1491                                           bcx, test_val, vend,
1492                                           t, ast::le);
1493                                   rslt(bcx, And(bcx, llge, llle))
1494                               }
1495                           }
1496                       }
1497                   };
1498                   bcx = sub_block(after_cx, ~"compare_next");
1499                   CondBr(after_cx, matches, opt_cx.llbb, bcx.llbb);
1500               }
1501               compare_vec_len => {
1502                   let Result {bcx: after_cx, val: matches} = {
1503                       do with_scope_result(bcx, None,
1504                                            ~"compare_vec_len_scope") |bcx| {
1505                           match trans_opt(bcx, opt) {
1506                               single_result(
1507                                   Result {bcx, val}) => {
1508                                   let value = compare_scalar_values(
1509                                       bcx, test_val, val,
1510                                       signed_int, ast::eq);
1511                                   rslt(bcx, value)
1512                               }
1513                               lower_bound(
1514                                   Result {bcx, val: val}) => {
1515                                   let value = compare_scalar_values(
1516                                       bcx, test_val, val,
1517                                       signed_int, ast::ge);
1518                                   rslt(bcx, value)
1519                               }
1520                               range_result(
1521                                   Result {val: vbegin, _},
1522                                   Result {bcx, val: vend}) => {
1523                                   let llge =
1524                                       compare_scalar_values(
1525                                           bcx, test_val,
1526                                           vbegin, signed_int, ast::ge);
1527                                   let llle =
1528                                       compare_scalar_values(
1529                                           bcx, test_val, vend,
1530                                           signed_int, ast::le);
1531                                   rslt(bcx, And(bcx, llge, llle))
1532                               }
1533                           }
1534                       }
1535                   };
1536                   bcx = sub_block(after_cx, ~"compare_vec_len_next");
1537                   CondBr(after_cx, matches, opt_cx.llbb, bcx.llbb);
1538               }
1539               _ => ()
1540             }
1541         } else if kind == compare || kind == compare_vec_len {
1542             Br(bcx, else_cx.llbb);
1543         }
1544
1545         let mut size = 0u;
1546         let mut unpacked = ~[];
1547         match *opt {
1548             var(disr_val, repr) => {
1549                 let ExtractedBlock {vals: argvals, bcx: new_bcx} =
1550                     extract_variant_args(opt_cx, repr, disr_val, val);
1551                 size = argvals.len();
1552                 unpacked = argvals;
1553                 opt_cx = new_bcx;
1554             }
1555             vec_len_eq(n) | vec_len_ge(n, _) => {
1556                 let n = match *opt {
1557                     vec_len_ge(*) => n + 1u,
1558                     _ => n
1559                 };
1560                 let slice = match *opt {
1561                     vec_len_ge(_, i) => Some(i),
1562                     _ => None
1563                 };
1564                 let args = extract_vec_elems(opt_cx, pat_id, n, slice,
1565                     val, test_val);
1566                 size = args.vals.len();
1567                 unpacked = /*bad*/copy args.vals;
1568                 opt_cx = args.bcx;
1569             }
1570             lit(_) | range(_, _) => ()
1571         }
1572         let opt_ms = enter_opt(opt_cx, m, opt, col, size, val);
1573         let opt_vals = vec::append(unpacked, vals_left);
1574         compile_submatch(opt_cx, opt_ms, opt_vals, chk);
1575     }
1576
1577     // Compile the fall-through case, if any
1578     if !exhaustive {
1579         if kind == compare || kind == compare_vec_len {
1580             Br(bcx, else_cx.llbb);
1581         }
1582         if kind != single {
1583             compile_submatch(else_cx, defaults, vals_left, chk);
1584         }
1585     }
1586 }
1587
1588 pub fn trans_match(bcx: block,
1589                    match_expr: @ast::expr,
1590                    discr_expr: @ast::expr,
1591                    arms: ~[ast::arm],
1592                    dest: Dest) -> block {
1593     let _icx = bcx.insn_ctxt("match::trans_match");
1594     do with_scope(bcx, match_expr.info(), ~"match") |bcx| {
1595         trans_match_inner(bcx, discr_expr, arms, dest)
1596     }
1597 }
1598
1599 pub fn trans_match_inner(scope_cx: block,
1600                          discr_expr: @ast::expr,
1601                          arms: &[ast::arm],
1602                          dest: Dest) -> block {
1603     let _icx = scope_cx.insn_ctxt("match::trans_match_inner");
1604     let mut bcx = scope_cx;
1605     let tcx = bcx.tcx();
1606
1607     let discr_datum = unpack_datum!(bcx, {
1608         expr::trans_to_datum(bcx, discr_expr)
1609     });
1610     if bcx.unreachable {
1611         return bcx;
1612     }
1613
1614     let mut arm_datas = ~[], matches = ~[];
1615     for vec::each(arms) |arm| {
1616         let body = scope_block(bcx, arm.body.info(), ~"case_body");
1617
1618         // Create the bindings map, which is a mapping from each binding name
1619         // to an alloca() that will be the value for that local variable.
1620         // Note that we use the names because each binding will have many ids
1621         // from the various alternatives.
1622         let mut bindings_map = HashMap::new();
1623         do pat_bindings(tcx.def_map, arm.pats[0]) |bm, p_id, _s, path| {
1624             let ident = path_to_ident(path);
1625             let variable_ty = node_id_type(bcx, p_id);
1626             let llvariable_ty = type_of::type_of(bcx.ccx(), variable_ty);
1627
1628             let llmatch, trmode;
1629             match bm {
1630                 ast::bind_by_copy | ast::bind_infer => {
1631                     // in this case, the final type of the variable will be T,
1632                     // but during matching we need to store a *T as explained
1633                     // above
1634                     let is_move =
1635                         scope_cx.ccx().maps.moves_map.contains(&p_id);
1636                     llmatch = alloca(bcx, T_ptr(llvariable_ty));
1637                     trmode = TrByValue(is_move, alloca(bcx, llvariable_ty));
1638                 }
1639                 ast::bind_by_ref(_) => {
1640                     llmatch = alloca(bcx, llvariable_ty);
1641                     trmode = TrByRef;
1642                 }
1643             };
1644             bindings_map.insert(ident, BindingInfo {
1645                 llmatch: llmatch, trmode: trmode,
1646                 id: p_id, ty: variable_ty
1647             });
1648         }
1649
1650         let arm_data = @ArmData {bodycx: body,
1651                                  arm: arm,
1652                                  bindings_map: bindings_map};
1653         arm_datas.push(arm_data);
1654         for vec::each(arm.pats) |p| {
1655             matches.push(@Match {pats: ~[*p], data: arm_data});
1656         }
1657     }
1658
1659     let t = node_id_type(bcx, discr_expr.id);
1660     let chk = {
1661         if ty::type_is_empty(tcx, t) {
1662             // Special case for empty types
1663             let fail_cx = @mut None;
1664             let f: mk_fail = || mk_fail(scope_cx, discr_expr.span,
1665                             @~"scrutinizing value that can't exist", fail_cx);
1666             Some(f)
1667         } else {
1668             None
1669         }
1670     };
1671     let lldiscr = discr_datum.to_ref_llval(bcx);
1672     compile_submatch(bcx, matches, ~[lldiscr], chk);
1673
1674     let mut arm_cxs = ~[];
1675     for arm_datas.each |arm_data| {
1676         let mut bcx = arm_data.bodycx;
1677
1678         // If this arm has a guard, then the various by-value bindings have
1679         // already been copied into their homes.  If not, we do it here.  This
1680         // is just to reduce code space.  See extensive comment at the start
1681         // of the file for more details.
1682         if arm_data.arm.guard.is_none() {
1683             bcx = store_non_ref_bindings(bcx, *arm_data, None);
1684         }
1685
1686         // insert bindings into the lllocals map and add cleanups
1687         bcx = insert_lllocals(bcx, *arm_data, true);
1688
1689         bcx = controlflow::trans_block(bcx, &arm_data.arm.body, dest);
1690         bcx = trans_block_cleanups(bcx, block_cleanups(arm_data.bodycx));
1691         arm_cxs.push(bcx);
1692     }
1693
1694     bcx = controlflow::join_blocks(scope_cx, arm_cxs);
1695     return bcx;
1696
1697     fn mk_fail(bcx: block, sp: span, msg: @~str,
1698                finished: @mut Option<BasicBlockRef>) -> BasicBlockRef {
1699         match *finished { Some(bb) => return bb, _ => () }
1700         let fail_cx = sub_block(bcx, ~"case_fallthrough");
1701         controlflow::trans_fail(fail_cx, Some(sp), msg);
1702         *finished = Some(fail_cx.llbb);
1703         return fail_cx.llbb;
1704     }
1705 }
1706
1707 pub enum IrrefutablePatternBindingMode {
1708     // Stores the association between node ID and LLVM value in `lllocals`.
1709     BindLocal,
1710     // Stores the association between node ID and LLVM value in `llargs`.
1711     BindArgument
1712 }
1713
1714 // Not match-related, but similar to the pattern-munging code above
1715 pub fn bind_irrefutable_pat(bcx: block,
1716                             pat: @ast::pat,
1717                             val: ValueRef,
1718                             make_copy: bool,
1719                             binding_mode: IrrefutablePatternBindingMode)
1720                          -> block {
1721     let _icx = bcx.insn_ctxt("match::bind_irrefutable_pat");
1722     let ccx = *bcx.fcx.ccx;
1723     let mut bcx = bcx;
1724
1725     // Necessary since bind_irrefutable_pat is called outside trans_match
1726     match pat.node {
1727         ast::pat_ident(_, _, ref inner) => {
1728             if pat_is_variant_or_struct(bcx.tcx().def_map, pat) {
1729                 return bcx;
1730             }
1731
1732             if make_copy {
1733                 let binding_ty = node_id_type(bcx, pat.id);
1734                 let datum = Datum {val: val, ty: binding_ty,
1735                                    mode: ByRef, source: RevokeClean};
1736                 let scratch = scratch_datum(bcx, binding_ty, false);
1737                 datum.copy_to_datum(bcx, INIT, scratch);
1738                 match binding_mode {
1739                     BindLocal => {
1740                         bcx.fcx.lllocals.insert(pat.id,
1741                                                 local_mem(scratch.val));
1742                     }
1743                     BindArgument => {
1744                         bcx.fcx.llargs.insert(pat.id,
1745                                               local_mem(scratch.val));
1746                     }
1747                 }
1748                 add_clean(bcx, scratch.val, binding_ty);
1749             } else {
1750                 match binding_mode {
1751                     BindLocal => {
1752                         bcx.fcx.lllocals.insert(pat.id, local_mem(val));
1753                     }
1754                     BindArgument => {
1755                         bcx.fcx.llargs.insert(pat.id, local_mem(val));
1756                     }
1757                 }
1758             }
1759
1760             for inner.each |inner_pat| {
1761                 bcx = bind_irrefutable_pat(
1762                     bcx, *inner_pat, val, true, binding_mode);
1763             }
1764         }
1765         ast::pat_enum(_, ref sub_pats) => {
1766             match bcx.tcx().def_map.find(&pat.id) {
1767                 Some(&ast::def_variant(enum_id, var_id)) => {
1768                     let repr = adt::represent_node(bcx, pat.id);
1769                     let vinfo = ty::enum_variant_with_id(ccx.tcx,
1770                                                          enum_id,
1771                                                          var_id);
1772                     let args = extract_variant_args(bcx,
1773                                                     repr,
1774                                                     vinfo.disr_val,
1775                                                     val);
1776                     for sub_pats.each |sub_pat| {
1777                         for vec::eachi(args.vals) |i, argval| {
1778                             bcx = bind_irrefutable_pat(bcx,
1779                                                        sub_pat[i],
1780                                                        *argval,
1781                                                        make_copy,
1782                                                        binding_mode);
1783                         }
1784                     }
1785                 }
1786                 Some(&ast::def_fn(*)) |
1787                 Some(&ast::def_struct(*)) => {
1788                     match *sub_pats {
1789                         None => {
1790                             // This is a unit-like struct. Nothing to do here.
1791                         }
1792                         Some(ref elems) => {
1793                             // This is the tuple struct case.
1794                             let repr = adt::represent_node(bcx, pat.id);
1795                             for elems.eachi |i, elem| {
1796                                 let fldptr = adt::trans_field_ptr(bcx, repr,
1797                                                             val, 0, i);
1798                                 bcx = bind_irrefutable_pat(bcx,
1799                                                            *elem,
1800                                                            fldptr,
1801                                                            make_copy,
1802                                                            binding_mode);
1803                             }
1804                         }
1805                     }
1806                 }
1807                 Some(&ast::def_const(*)) => {
1808                     bcx = bind_irrefutable_pat(bcx, pat, val, make_copy, binding_mode);
1809                 }
1810                 _ => {
1811                     // Nothing to do here.
1812                 }
1813             }
1814         }
1815         ast::pat_struct(_, ref fields, _) => {
1816             let tcx = bcx.tcx();
1817             let pat_ty = node_id_type(bcx, pat.id);
1818             let pat_repr = adt::represent_type(bcx.ccx(), pat_ty);
1819             do expr::with_field_tys(tcx, pat_ty, None) |discr, field_tys| {
1820                 for fields.each |f| {
1821                     let ix = ty::field_idx_strict(tcx, f.ident, field_tys);
1822                     let fldptr = adt::trans_field_ptr(bcx, pat_repr, val,
1823                                                 discr, ix);
1824                     bcx = bind_irrefutable_pat(bcx,
1825                                                f.pat,
1826                                                fldptr,
1827                                                make_copy,
1828                                                binding_mode);
1829                 }
1830             }
1831         }
1832         ast::pat_tup(ref elems) => {
1833             let repr = adt::represent_node(bcx, pat.id);
1834             for elems.eachi |i, elem| {
1835                 let fldptr = adt::trans_field_ptr(bcx, repr, val, 0, i);
1836                 bcx = bind_irrefutable_pat(bcx,
1837                                            *elem,
1838                                            fldptr,
1839                                            make_copy,
1840                                            binding_mode);
1841             }
1842         }
1843         ast::pat_box(inner) | ast::pat_uniq(inner) => {
1844             let llbox = Load(bcx, val);
1845             let unboxed = GEPi(bcx, llbox, [0u, abi::box_field_body]);
1846             bcx = bind_irrefutable_pat(bcx,
1847                                        inner,
1848                                        unboxed,
1849                                        true,
1850                                        binding_mode);
1851         }
1852         ast::pat_region(inner) => {
1853             let loaded_val = Load(bcx, val);
1854             bcx = bind_irrefutable_pat(bcx,
1855                                        inner,
1856                                        loaded_val,
1857                                        true,
1858                                        binding_mode);
1859         }
1860         ast::pat_wild | ast::pat_lit(_) | ast::pat_range(_, _) |
1861         ast::pat_vec(*) => ()
1862     }
1863     return bcx;
1864 }
1865
1866 // Local Variables:
1867 // mode: rust
1868 // fill-column: 78;
1869 // indent-tabs-mode: nil
1870 // c-basic-offset: 4
1871 // buffer-file-coding-system: utf-8-unix
1872 // End: