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