]> git.lizzy.rs Git - rust.git/blob - src/librustc/middle/pat_util.rs
Add support for patterns referencing non-trivial statics
[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 std::gc::{Gc, GC};
17 use syntax::ast::*;
18 use syntax::ast_util::{walk_pat};
19 use syntax::codemap::{Span, DUMMY_SP};
20 use syntax::owned_slice::OwnedSlice;
21
22 pub type PatIdMap = HashMap<Ident, NodeId>;
23
24 // This is used because same-named variables in alternative patterns need to
25 // use the NodeId of their namesake in the first pattern.
26 pub fn pat_id_map(dm: &resolve::DefMap, pat: &Pat) -> PatIdMap {
27     let mut map = HashMap::new();
28     pat_bindings(dm, pat, |_bm, p_id, _s, path1| {
29       map.insert(path1.node, p_id);
30     });
31     map
32 }
33
34 pub fn pat_is_variant_or_struct(dm: &resolve::DefMap, pat: &Pat) -> bool {
35     match pat.node {
36         PatEnum(_, _) | PatIdent(_, _, None) | PatStruct(..) => {
37             match dm.borrow().find(&pat.id) {
38                 Some(&DefVariant(..)) | Some(&DefStruct(..)) => true,
39                 _ => false
40             }
41         }
42         _ => false
43     }
44 }
45
46 pub fn pat_is_const(dm: &resolve::DefMap, pat: &Pat) -> bool {
47     match pat.node {
48         PatIdent(_, _, None) | PatEnum(..) => {
49             match dm.borrow().find(&pat.id) {
50                 Some(&DefStatic(_, false)) => true,
51                 _ => false
52             }
53         }
54         _ => false
55     }
56 }
57
58 pub fn pat_is_binding(dm: &resolve::DefMap, pat: &Pat) -> bool {
59     match pat.node {
60         PatIdent(..) => {
61             !pat_is_variant_or_struct(dm, pat) &&
62             !pat_is_const(dm, pat)
63         }
64         _ => false
65     }
66 }
67
68 pub fn pat_is_binding_or_wild(dm: &resolve::DefMap, pat: &Pat) -> bool {
69     match pat.node {
70         PatIdent(..) => pat_is_binding(dm, pat),
71         PatWild | PatWildMulti => true,
72         _ => false
73     }
74 }
75
76 /// Call `it` on every "binding" in a pattern, e.g., on `a` in
77 /// `match foo() { Some(a) => (), None => () }`
78 pub fn pat_bindings(dm: &resolve::DefMap,
79                     pat: &Pat,
80                     it: |BindingMode, NodeId, Span, &SpannedIdent|) {
81     walk_pat(pat, |p| {
82         match p.node {
83           PatIdent(binding_mode, ref pth, _) if pat_is_binding(dm, p) => {
84             it(binding_mode, p.id, p.span, pth);
85           }
86           _ => {}
87         }
88         true
89     });
90 }
91
92 /// Checks if the pattern contains any patterns that bind something to
93 /// an ident, e.g. `foo`, or `Foo(foo)` or `foo @ Bar(..)`.
94 pub fn pat_contains_bindings(dm: &resolve::DefMap, pat: &Pat) -> bool {
95     let mut contains_bindings = false;
96     walk_pat(pat, |p| {
97         if pat_is_binding(dm, p) {
98             contains_bindings = true;
99             false // there's at least one binding, can short circuit now.
100         } else {
101             true
102         }
103     });
104     contains_bindings
105 }
106
107 pub fn simple_identifier<'a>(pat: &'a Pat) -> Option<&'a Ident> {
108     match pat.node {
109         PatIdent(BindByValue(_), ref path1, None) => {
110             Some(&path1.node)
111         }
112         _ => {
113             None
114         }
115     }
116 }
117
118 pub fn wild() -> Gc<Pat> {
119     box (GC) Pat { id: 0, node: PatWild, span: DUMMY_SP }
120 }
121
122 pub fn def_to_path(tcx: &ty::ctxt, id: DefId) -> Path {
123     ty::with_path(tcx, id, |mut path| Path {
124         global: false,
125         segments: path.last().map(|elem| PathSegment {
126             identifier: Ident::new(elem.name()),
127             lifetimes: vec!(),
128             types: OwnedSlice::empty()
129         }).move_iter().collect(),
130         span: DUMMY_SP,
131     })
132 }