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.
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.
12 use middle::def_id::DefId;
14 use util::nodemap::FnvHashMap;
17 use syntax::ast_util::walk_pat;
18 use syntax::codemap::{Span, DUMMY_SP};
20 pub type PatIdMap = FnvHashMap<ast::Ident, ast::NodeId>;
22 // This is used because same-named variables in alternative patterns need to
23 // use the NodeId of their namesake in the first pattern.
24 pub fn pat_id_map(dm: &DefMap, pat: &ast::Pat) -> PatIdMap {
25 let mut map = FnvHashMap();
26 pat_bindings(dm, pat, |_bm, p_id, _s, path1| {
27 map.insert(path1.node, p_id);
32 pub fn pat_is_refutable(dm: &DefMap, pat: &ast::Pat) -> bool {
34 ast::PatLit(_) | ast::PatRange(_, _) | ast::PatQPath(..) => true,
36 ast::PatIdent(_, _, None) |
37 ast::PatStruct(..) => {
38 match dm.borrow().get(&pat.id).map(|d| d.full_def()) {
39 Some(DefVariant(..)) => true,
43 ast::PatVec(_, _, _) => true,
48 pub fn pat_is_variant_or_struct(dm: &DefMap, pat: &ast::Pat) -> bool {
51 ast::PatIdent(_, _, None) |
52 ast::PatStruct(..) => {
53 match dm.borrow().get(&pat.id).map(|d| d.full_def()) {
54 Some(DefVariant(..)) | Some(DefStruct(..)) => true,
62 pub fn pat_is_const(dm: &DefMap, pat: &ast::Pat) -> bool {
64 ast::PatIdent(_, _, None) | ast::PatEnum(..) | ast::PatQPath(..) => {
65 match dm.borrow().get(&pat.id).map(|d| d.full_def()) {
66 Some(DefConst(..)) | Some(DefAssociatedConst(..)) => true,
74 // Same as above, except that partially-resolved defs cause `false` to be
75 // returned instead of a panic.
76 pub fn pat_is_resolved_const(dm: &DefMap, pat: &ast::Pat) -> bool {
78 ast::PatIdent(_, _, None) | ast::PatEnum(..) | ast::PatQPath(..) => {
79 match dm.borrow().get(&pat.id)
80 .and_then(|d| if d.depth == 0 { Some(d.base_def) }
82 Some(DefConst(..)) | Some(DefAssociatedConst(..)) => true,
90 pub fn pat_is_binding(dm: &DefMap, pat: &ast::Pat) -> bool {
92 ast::PatIdent(..) => {
93 !pat_is_variant_or_struct(dm, pat) &&
94 !pat_is_const(dm, pat)
100 pub fn pat_is_binding_or_wild(dm: &DefMap, pat: &ast::Pat) -> bool {
102 ast::PatIdent(..) => pat_is_binding(dm, pat),
103 ast::PatWild(_) => true,
108 /// Call `it` on every "binding" in a pattern, e.g., on `a` in
109 /// `match foo() { Some(a) => (), None => () }`
110 pub fn pat_bindings<I>(dm: &DefMap, pat: &ast::Pat, mut it: I) where
111 I: FnMut(ast::BindingMode, ast::NodeId, Span, &ast::SpannedIdent),
115 ast::PatIdent(binding_mode, ref pth, _) if pat_is_binding(dm, p) => {
116 it(binding_mode, p.id, p.span, pth);
124 /// Checks if the pattern contains any patterns that bind something to
125 /// an ident, e.g. `foo`, or `Foo(foo)` or `foo @ Bar(..)`.
126 pub fn pat_contains_bindings(dm: &DefMap, pat: &ast::Pat) -> bool {
127 let mut contains_bindings = false;
129 if pat_is_binding(dm, p) {
130 contains_bindings = true;
131 false // there's at least one binding, can short circuit now.
139 /// Checks if the pattern contains any `ref` or `ref mut` bindings,
140 /// and if yes wether its containing mutable ones or just immutables ones.
141 pub fn pat_contains_ref_binding(dm: &DefMap, pat: &ast::Pat) -> Option<ast::Mutability> {
142 let mut result = None;
143 pat_bindings(dm, pat, |mode, _, _, _| {
145 ast::BindingMode::BindByRef(m) => {
146 // Pick Mutable as maximum
148 None | Some(ast::MutImmutable) => result = Some(m),
152 ast::BindingMode::BindByValue(_) => { }
158 /// Checks if the patterns for this arm contain any `ref` or `ref mut`
159 /// bindings, and if yes wether its containing mutable ones or just immutables ones.
160 pub fn arm_contains_ref_binding(dm: &DefMap, arm: &ast::Arm) -> Option<ast::Mutability> {
162 .filter_map(|pat| pat_contains_ref_binding(dm, pat))
163 .max_by(|m| match *m {
164 ast::MutMutable => 1,
165 ast::MutImmutable => 0,
169 /// Checks if the pattern contains any patterns that bind something to
170 /// an ident or wildcard, e.g. `foo`, or `Foo(_)`, `foo @ Bar(..)`,
171 pub fn pat_contains_bindings_or_wild(dm: &DefMap, pat: &ast::Pat) -> bool {
172 let mut contains_bindings = false;
174 if pat_is_binding_or_wild(dm, p) {
175 contains_bindings = true;
176 false // there's at least one binding/wildcard, can short circuit now.
184 pub fn simple_identifier<'a>(pat: &'a ast::Pat) -> Option<&'a ast::Ident> {
186 ast::PatIdent(ast::BindByValue(_), ref path1, None) => {
195 pub fn def_to_path(tcx: &ty::ctxt, id: DefId) -> ast::Path {
196 tcx.with_path(id, |path| ast::Path {
198 segments: path.last().map(|elem| ast::PathSegment {
199 identifier: ast::Ident::new(elem.name()),
200 parameters: ast::PathParameters::none(),
201 }).into_iter().collect(),
206 /// Return variants that are necessary to exist for the pattern to match.
207 pub fn necessary_variants(dm: &DefMap, pat: &ast::Pat) -> Vec<ast::NodeId> {
208 let mut variants = vec![];
212 ast::PatIdent(_, _, None) |
213 ast::PatStruct(..) => {
214 match dm.borrow().get(&p.id) {
215 Some(&PathResolution { base_def: DefVariant(_, id, _), .. }) => {
216 variants.push(id.node);