]> git.lizzy.rs Git - rust.git/blob - src/librustc/middle/pat_util.rs
librustc: Automatically change uses of `~[T]` to `Vec<T>` in rustc.
[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
12 use middle::resolve;
13
14 use collections::HashMap;
15 use syntax::ast::*;
16 use syntax::ast_util::{path_to_ident, walk_pat};
17 use syntax::codemap::Span;
18
19 pub type PatIdMap = HashMap<Ident, NodeId>;
20
21 // This is used because same-named variables in alternative patterns need to
22 // use the NodeId of their namesake in the first pattern.
23 pub fn pat_id_map(dm: resolve::DefMap, pat: &Pat) -> PatIdMap {
24     let mut map = HashMap::new();
25     pat_bindings(dm, pat, |_bm, p_id, _s, n| {
26       map.insert(path_to_ident(n), p_id);
27     });
28     map
29 }
30
31 pub fn pat_is_variant_or_struct(dm: resolve::DefMap, pat: &Pat) -> bool {
32     match pat.node {
33         PatEnum(_, _) | PatIdent(_, _, None) | PatStruct(..) => {
34             let dm = dm.borrow();
35             match dm.get().find(&pat.id) {
36                 Some(&DefVariant(..)) | Some(&DefStruct(..)) => true,
37                 _ => false
38             }
39         }
40         _ => false
41     }
42 }
43
44 pub fn pat_is_const(dm: resolve::DefMap, pat: &Pat) -> bool {
45     match pat.node {
46         PatIdent(_, _, None) | PatEnum(..) => {
47             let dm = dm.borrow();
48             match dm.get().find(&pat.id) {
49                 Some(&DefStatic(_, false)) => true,
50                 _ => false
51             }
52         }
53         _ => false
54     }
55 }
56
57 pub fn pat_is_binding(dm: resolve::DefMap, pat: &Pat) -> bool {
58     match pat.node {
59         PatIdent(..) => {
60             !pat_is_variant_or_struct(dm, pat) &&
61             !pat_is_const(dm, pat)
62         }
63         _ => false
64     }
65 }
66
67 pub fn pat_is_binding_or_wild(dm: resolve::DefMap, pat: &Pat) -> bool {
68     match pat.node {
69         PatIdent(..) => pat_is_binding(dm, pat),
70         PatWild | PatWildMulti => true,
71         _ => false
72     }
73 }
74
75 /// Call `it` on every "binding" in a pattern, e.g., on `a` in
76 /// `match foo() { Some(a) => (), None => () }`
77 pub fn pat_bindings(dm: resolve::DefMap,
78                     pat: &Pat,
79                     it: |BindingMode, NodeId, Span, &Path|) {
80     walk_pat(pat, |p| {
81         match p.node {
82           PatIdent(binding_mode, ref pth, _) if pat_is_binding(dm, p) => {
83             it(binding_mode, p.id, p.span, pth);
84           }
85           _ => {}
86         }
87         true
88     });
89 }
90
91 pub fn pat_binding_ids(dm: resolve::DefMap, pat: &Pat) -> Vec<NodeId> {
92     let mut found = Vec::new();
93     pat_bindings(dm, pat, |_bm, b_id, _sp, _pt| found.push(b_id) );
94     return found;
95 }
96
97 /// Checks if the pattern contains any patterns that bind something to
98 /// an ident, e.g. `foo`, or `Foo(foo)` or `foo @ Bar(..)`.
99 pub fn pat_contains_bindings(dm: resolve::DefMap, pat: &Pat) -> bool {
100     let mut contains_bindings = false;
101     walk_pat(pat, |p| {
102         if pat_is_binding(dm, p) {
103             contains_bindings = true;
104             false // there's at least one binding, can short circuit now.
105         } else {
106             true
107         }
108     });
109     contains_bindings
110 }
111
112 pub fn simple_identifier<'a>(pat: &'a Pat) -> Option<&'a Path> {
113     match pat.node {
114         PatIdent(BindByValue(_), ref path, None) => {
115             Some(path)
116         }
117         _ => {
118             None
119         }
120     }
121 }