]> git.lizzy.rs Git - rust.git/blob - src/librustc/middle/pat_util.rs
Remove alternate stack with sigaltstack before unmapping it.
[rust.git] / src / librustc / middle / pat_util.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 use middle::def::*;
12 use middle::def_id::DefId;
13 use middle::ty;
14 use util::nodemap::FnvHashMap;
15
16 use syntax::ast;
17 use rustc_front::hir;
18 use rustc_front::util::walk_pat;
19 use syntax::codemap::{respan, Span, Spanned, DUMMY_SP};
20
21 use std::cell::RefCell;
22
23 pub type PatIdMap = FnvHashMap<ast::Name, ast::NodeId>;
24
25 // This is used because same-named variables in alternative patterns need to
26 // use the NodeId of their namesake in the first pattern.
27 pub fn pat_id_map(dm: &RefCell<DefMap>, pat: &hir::Pat) -> PatIdMap {
28     let mut map = FnvHashMap();
29     pat_bindings(dm, pat, |_bm, p_id, _s, path1| {
30         map.insert(path1.node, p_id);
31     });
32     map
33 }
34
35 pub fn pat_is_refutable(dm: &DefMap, pat: &hir::Pat) -> bool {
36     match pat.node {
37         hir::PatLit(_) | hir::PatRange(_, _) | hir::PatQPath(..) => true,
38         hir::PatEnum(_, _) |
39         hir::PatIdent(_, _, None) |
40         hir::PatStruct(..) => {
41             match dm.get(&pat.id).map(|d| d.full_def()) {
42                 Some(Def::Variant(..)) => true,
43                 _ => false
44             }
45         }
46         hir::PatVec(_, _, _) => true,
47         _ => false
48     }
49 }
50
51 pub fn pat_is_variant_or_struct(dm: &DefMap, pat: &hir::Pat) -> bool {
52     match pat.node {
53         hir::PatEnum(_, _) |
54         hir::PatIdent(_, _, None) |
55         hir::PatStruct(..) => {
56             match dm.get(&pat.id).map(|d| d.full_def()) {
57                 Some(Def::Variant(..)) | Some(Def::Struct(..)) => true,
58                 _ => false
59             }
60         }
61         _ => false
62     }
63 }
64
65 pub fn pat_is_const(dm: &DefMap, pat: &hir::Pat) -> bool {
66     match pat.node {
67         hir::PatIdent(_, _, None) | hir::PatEnum(..) | hir::PatQPath(..) => {
68             match dm.get(&pat.id).map(|d| d.full_def()) {
69                 Some(Def::Const(..)) | Some(Def::AssociatedConst(..)) => true,
70                 _ => false
71             }
72         }
73         _ => false
74     }
75 }
76
77 // Same as above, except that partially-resolved defs cause `false` to be
78 // returned instead of a panic.
79 pub fn pat_is_resolved_const(dm: &DefMap, pat: &hir::Pat) -> bool {
80     match pat.node {
81         hir::PatIdent(_, _, None) | hir::PatEnum(..) | hir::PatQPath(..) => {
82             match dm.get(&pat.id)
83                     .and_then(|d| if d.depth == 0 { Some(d.base_def) }
84                                   else { None } ) {
85                 Some(Def::Const(..)) | Some(Def::AssociatedConst(..)) => true,
86                 _ => false
87             }
88         }
89         _ => false
90     }
91 }
92
93 pub fn pat_is_binding(dm: &DefMap, pat: &hir::Pat) -> bool {
94     match pat.node {
95         hir::PatIdent(..) => {
96             !pat_is_variant_or_struct(dm, pat) &&
97             !pat_is_const(dm, pat)
98         }
99         _ => false
100     }
101 }
102
103 pub fn pat_is_binding_or_wild(dm: &DefMap, pat: &hir::Pat) -> bool {
104     match pat.node {
105         hir::PatIdent(..) => pat_is_binding(dm, pat),
106         hir::PatWild => true,
107         _ => false
108     }
109 }
110
111 /// Call `it` on every "binding" in a pattern, e.g., on `a` in
112 /// `match foo() { Some(a) => (), None => () }`
113 pub fn pat_bindings<I>(dm: &RefCell<DefMap>, pat: &hir::Pat, mut it: I) where
114     I: FnMut(hir::BindingMode, ast::NodeId, Span, &Spanned<ast::Name>),
115 {
116     walk_pat(pat, |p| {
117         match p.node {
118           hir::PatIdent(binding_mode, ref pth, _) if pat_is_binding(&dm.borrow(), p) => {
119             it(binding_mode, p.id, p.span, &respan(pth.span, pth.node.name));
120           }
121           _ => {}
122         }
123         true
124     });
125 }
126 pub fn pat_bindings_ident<I>(dm: &RefCell<DefMap>, pat: &hir::Pat, mut it: I) where
127     I: FnMut(hir::BindingMode, ast::NodeId, Span, &Spanned<hir::Ident>),
128 {
129     walk_pat(pat, |p| {
130         match p.node {
131           hir::PatIdent(binding_mode, ref pth, _) if pat_is_binding(&dm.borrow(), p) => {
132             it(binding_mode, p.id, p.span, &respan(pth.span, pth.node));
133           }
134           _ => {}
135         }
136         true
137     });
138 }
139
140 /// Checks if the pattern contains any patterns that bind something to
141 /// an ident, e.g. `foo`, or `Foo(foo)` or `foo @ Bar(..)`.
142 pub fn pat_contains_bindings(dm: &DefMap, pat: &hir::Pat) -> bool {
143     let mut contains_bindings = false;
144     walk_pat(pat, |p| {
145         if pat_is_binding(dm, p) {
146             contains_bindings = true;
147             false // there's at least one binding, can short circuit now.
148         } else {
149             true
150         }
151     });
152     contains_bindings
153 }
154
155 /// Checks if the pattern contains any `ref` or `ref mut` bindings,
156 /// and if yes whether its containing mutable ones or just immutables ones.
157 pub fn pat_contains_ref_binding(dm: &RefCell<DefMap>, pat: &hir::Pat) -> Option<hir::Mutability> {
158     let mut result = None;
159     pat_bindings(dm, pat, |mode, _, _, _| {
160         match mode {
161             hir::BindingMode::BindByRef(m) => {
162                 // Pick Mutable as maximum
163                 match result {
164                     None | Some(hir::MutImmutable) => result = Some(m),
165                     _ => (),
166                 }
167             }
168             hir::BindingMode::BindByValue(_) => { }
169         }
170     });
171     result
172 }
173
174 /// Checks if the patterns for this arm contain any `ref` or `ref mut`
175 /// bindings, and if yes whether its containing mutable ones or just immutables ones.
176 pub fn arm_contains_ref_binding(dm: &RefCell<DefMap>, arm: &hir::Arm) -> Option<hir::Mutability> {
177     arm.pats.iter()
178             .filter_map(|pat| pat_contains_ref_binding(dm, pat))
179             .max_by_key(|m| match *m {
180                 hir::MutMutable => 1,
181                 hir::MutImmutable => 0,
182             })
183 }
184
185 /// Checks if the pattern contains any patterns that bind something to
186 /// an ident or wildcard, e.g. `foo`, or `Foo(_)`, `foo @ Bar(..)`,
187 pub fn pat_contains_bindings_or_wild(dm: &DefMap, pat: &hir::Pat) -> bool {
188     let mut contains_bindings = false;
189     walk_pat(pat, |p| {
190         if pat_is_binding_or_wild(dm, p) {
191             contains_bindings = true;
192             false // there's at least one binding/wildcard, can short circuit now.
193         } else {
194             true
195         }
196     });
197     contains_bindings
198 }
199
200 pub fn simple_name<'a>(pat: &'a hir::Pat) -> Option<ast::Name> {
201     match pat.node {
202         hir::PatIdent(hir::BindByValue(_), ref path1, None) => {
203             Some(path1.node.name)
204         }
205         _ => {
206             None
207         }
208     }
209 }
210
211 pub fn def_to_path(tcx: &ty::ctxt, id: DefId) -> hir::Path {
212     tcx.with_path(id, |path| hir::Path {
213         global: false,
214         segments: path.last().map(|elem| hir::PathSegment {
215             identifier: hir::Ident::from_name(elem.name()),
216             parameters: hir::PathParameters::none(),
217         }).into_iter().collect(),
218         span: DUMMY_SP,
219     })
220 }
221
222 /// Return variants that are necessary to exist for the pattern to match.
223 pub fn necessary_variants(dm: &DefMap, pat: &hir::Pat) -> Vec<DefId> {
224     let mut variants = vec![];
225     walk_pat(pat, |p| {
226         match p.node {
227             hir::PatEnum(_, _) |
228             hir::PatIdent(_, _, None) |
229             hir::PatStruct(..) => {
230                 match dm.get(&p.id) {
231                     Some(&PathResolution { base_def: Def::Variant(_, id), .. }) => {
232                         variants.push(id);
233                     }
234                     _ => ()
235                 }
236             }
237             _ => ()
238         }
239         true
240     });
241     variants.sort();
242     variants.dedup();
243     variants
244 }