]> git.proxmox.com Git - rustc.git/blob - src/librustc_mir/build/matches/util.rs
Imported Upstream version 1.8.0+dfsg1
[rustc.git] / src / librustc_mir / build / matches / util.rs
1 // Copyright 2015 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 use build::{BlockAnd, BlockAndExtension, Builder};
12 use build::matches::MatchPair;
13 use hair::*;
14 use rustc::mir::repr::*;
15 use std::u32;
16
17 impl<'a,'tcx> Builder<'a,'tcx> {
18 pub fn field_match_pairs<'pat>(&mut self,
19 lvalue: Lvalue<'tcx>,
20 subpatterns: &'pat [FieldPattern<'tcx>])
21 -> Vec<MatchPair<'pat, 'tcx>> {
22 subpatterns.iter()
23 .map(|fieldpat| {
24 let lvalue = lvalue.clone().field(fieldpat.field,
25 fieldpat.field_ty());
26 MatchPair::new(lvalue, &fieldpat.pattern)
27 })
28 .collect()
29 }
30
31 /// When processing an array/slice pattern like `lv @ [x, y, ..s, z]`,
32 /// this function converts the prefix (`x`, `y`) and suffix (`z`) into
33 /// distinct match pairs:
34 ///
35 /// lv[0 of 3] @ x // see ProjectionElem::ConstantIndex (and its Debug impl)
36 /// lv[1 of 3] @ y // to explain the `[x of y]` notation
37 /// lv[-1 of 3] @ z
38 ///
39 /// If a slice like `s` is present, then the function also creates
40 /// a temporary like:
41 ///
42 /// tmp0 = lv[2..-1] // using the special Rvalue::Slice
43 ///
44 /// and creates a match pair `tmp0 @ s`
45 pub fn prefix_suffix_slice<'pat>(&mut self,
46 match_pairs: &mut Vec<MatchPair<'pat, 'tcx>>,
47 block: BasicBlock,
48 lvalue: Lvalue<'tcx>,
49 prefix: &'pat [Pattern<'tcx>],
50 opt_slice: Option<&'pat Pattern<'tcx>>,
51 suffix: &'pat [Pattern<'tcx>])
52 -> BlockAnd<()> {
53 // If there is a `..P` pattern, create a temporary `t0` for
54 // the slice and then a match pair `t0 @ P`:
55 if let Some(slice) = opt_slice {
56 let prefix_len = prefix.len();
57 let suffix_len = suffix.len();
58 let rvalue = Rvalue::Slice {
59 input: lvalue.clone(),
60 from_start: prefix_len,
61 from_end: suffix_len,
62 };
63 let temp = self.temp(slice.ty.clone()); // no need to schedule drop, temp is always copy
64 self.cfg.push_assign(block, slice.span, &temp, rvalue);
65 match_pairs.push(MatchPair::new(temp, slice));
66 }
67
68 self.prefix_suffix(match_pairs, lvalue, prefix, suffix);
69
70 block.unit()
71 }
72
73 /// Helper for `prefix_suffix_slice` which just processes the prefix and suffix.
74 fn prefix_suffix<'pat>(&mut self,
75 match_pairs: &mut Vec<MatchPair<'pat, 'tcx>>,
76 lvalue: Lvalue<'tcx>,
77 prefix: &'pat [Pattern<'tcx>],
78 suffix: &'pat [Pattern<'tcx>]) {
79 let min_length = prefix.len() + suffix.len();
80 assert!(min_length < u32::MAX as usize);
81 let min_length = min_length as u32;
82
83 let prefix_pairs: Vec<_> =
84 prefix.iter()
85 .enumerate()
86 .map(|(idx, subpattern)| {
87 let elem = ProjectionElem::ConstantIndex {
88 offset: idx as u32,
89 min_length: min_length,
90 from_end: false,
91 };
92 let lvalue = lvalue.clone().elem(elem);
93 MatchPair::new(lvalue, subpattern)
94 })
95 .collect();
96
97 let suffix_pairs: Vec<_> =
98 suffix.iter()
99 .rev()
100 .enumerate()
101 .map(|(idx, subpattern)| {
102 let elem = ProjectionElem::ConstantIndex {
103 offset: (idx+1) as u32,
104 min_length: min_length,
105 from_end: true,
106 };
107 let lvalue = lvalue.clone().elem(elem);
108 MatchPair::new(lvalue, subpattern)
109 })
110 .collect();
111
112 match_pairs.extend(prefix_pairs.into_iter().chain(suffix_pairs));
113 }
114 }
115
116 impl<'pat, 'tcx> MatchPair<'pat, 'tcx> {
117 pub fn new(lvalue: Lvalue<'tcx>, pattern: &'pat Pattern<'tcx>) -> MatchPair<'pat, 'tcx> {
118 MatchPair {
119 lvalue: lvalue,
120 pattern: pattern,
121 }
122 }
123 }