]> git.lizzy.rs Git - rust.git/blob - src/librustc/middle/pat_util.rs
rollup merge of #17355 : gamazeps/issue17210
[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::resolve;
13 use middle::ty;
14
15 use std::collections::HashMap;
16 use syntax::ast::*;
17 use syntax::ast_util::{walk_pat};
18 use syntax::codemap::{Span, DUMMY_SP};
19 use syntax::owned_slice::OwnedSlice;
20
21 pub type PatIdMap = HashMap<Ident, NodeId>;
22
23 // This is used because same-named variables in alternative patterns need to
24 // use the NodeId of their namesake in the first pattern.
25 pub fn pat_id_map(dm: &resolve::DefMap, pat: &Pat) -> PatIdMap {
26     let mut map = HashMap::new();
27     pat_bindings(dm, pat, |_bm, p_id, _s, path1| {
28       map.insert(path1.node, p_id);
29     });
30     map
31 }
32
33 pub fn pat_is_variant_or_struct(dm: &resolve::DefMap, pat: &Pat) -> bool {
34     match pat.node {
35         PatEnum(_, _) | PatIdent(_, _, None) | PatStruct(..) => {
36             match dm.borrow().find(&pat.id) {
37                 Some(&DefVariant(..)) | Some(&DefStruct(..)) => true,
38                 _ => false
39             }
40         }
41         _ => false
42     }
43 }
44
45 pub fn pat_is_const(dm: &resolve::DefMap, pat: &Pat) -> bool {
46     match pat.node {
47         PatIdent(_, _, None) | PatEnum(..) => {
48             match dm.borrow().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(_) => 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, &SpannedIdent|) {
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 /// Checks if the pattern contains any patterns that bind something to
92 /// an ident, e.g. `foo`, or `Foo(foo)` or `foo @ Bar(..)`.
93 pub fn pat_contains_bindings(dm: &resolve::DefMap, pat: &Pat) -> bool {
94     let mut contains_bindings = false;
95     walk_pat(pat, |p| {
96         if pat_is_binding(dm, p) {
97             contains_bindings = true;
98             false // there's at least one binding, can short circuit now.
99         } else {
100             true
101         }
102     });
103     contains_bindings
104 }
105
106 pub fn simple_identifier<'a>(pat: &'a Pat) -> Option<&'a Ident> {
107     match pat.node {
108         PatIdent(BindByValue(_), ref path1, None) => {
109             Some(&path1.node)
110         }
111         _ => {
112             None
113         }
114     }
115 }
116
117 pub fn def_to_path(tcx: &ty::ctxt, id: DefId) -> Path {
118     ty::with_path(tcx, id, |mut path| Path {
119         global: false,
120         segments: path.last().map(|elem| PathSegment {
121             identifier: Ident::new(elem.name()),
122             lifetimes: vec!(),
123             types: OwnedSlice::empty()
124         }).into_iter().collect(),
125         span: DUMMY_SP,
126     })
127 }