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::ext::mtwt;
19 use rustc_front::util::walk_pat;
20 use syntax::codemap::{respan, Span, Spanned, DUMMY_SP};
22 use std::cell::RefCell;
24 pub type PatIdMap = FnvHashMap<ast::Name, ast::NodeId>;
26 // This is used because same-named variables in alternative patterns need to
27 // use the NodeId of their namesake in the first pattern.
28 pub fn pat_id_map(dm: &RefCell<DefMap>, pat: &hir::Pat) -> PatIdMap {
29 let mut map = FnvHashMap();
30 pat_bindings_hygienic(dm, pat, |_bm, p_id, _s, path1| {
31 map.insert(mtwt::resolve(path1.node), p_id);
36 pub fn pat_is_refutable(dm: &DefMap, pat: &hir::Pat) -> bool {
38 hir::PatLit(_) | hir::PatRange(_, _) | hir::PatQPath(..) => true,
40 hir::PatIdent(_, _, None) |
41 hir::PatStruct(..) => {
42 match dm.get(&pat.id).map(|d| d.full_def()) {
43 Some(DefVariant(..)) => true,
47 hir::PatVec(_, _, _) => true,
52 pub fn pat_is_variant_or_struct(dm: &DefMap, pat: &hir::Pat) -> bool {
55 hir::PatIdent(_, _, None) |
56 hir::PatStruct(..) => {
57 match dm.get(&pat.id).map(|d| d.full_def()) {
58 Some(DefVariant(..)) | Some(DefStruct(..)) => true,
66 pub fn pat_is_const(dm: &DefMap, pat: &hir::Pat) -> bool {
68 hir::PatIdent(_, _, None) | hir::PatEnum(..) | hir::PatQPath(..) => {
69 match dm.get(&pat.id).map(|d| d.full_def()) {
70 Some(DefConst(..)) | Some(DefAssociatedConst(..)) => true,
78 // Same as above, except that partially-resolved defs cause `false` to be
79 // returned instead of a panic.
80 pub fn pat_is_resolved_const(dm: &DefMap, pat: &hir::Pat) -> bool {
82 hir::PatIdent(_, _, None) | hir::PatEnum(..) | hir::PatQPath(..) => {
84 .and_then(|d| if d.depth == 0 { Some(d.base_def) }
86 Some(DefConst(..)) | Some(DefAssociatedConst(..)) => true,
94 pub fn pat_is_binding(dm: &DefMap, pat: &hir::Pat) -> bool {
96 hir::PatIdent(..) => {
97 !pat_is_variant_or_struct(dm, pat) &&
98 !pat_is_const(dm, pat)
104 pub fn pat_is_binding_or_wild(dm: &DefMap, pat: &hir::Pat) -> bool {
106 hir::PatIdent(..) => pat_is_binding(dm, pat),
107 hir::PatWild => true,
112 /// Call `it` on every "binding" in a pattern, e.g., on `a` in
113 /// `match foo() { Some(a) => (), None => () }`
114 pub fn pat_bindings<I>(dm: &RefCell<DefMap>, pat: &hir::Pat, mut it: I) where
115 I: FnMut(hir::BindingMode, ast::NodeId, Span, &Spanned<ast::Name>),
119 hir::PatIdent(binding_mode, ref pth, _) if pat_is_binding(&dm.borrow(), p) => {
120 it(binding_mode, p.id, p.span, &respan(pth.span, pth.node.name));
128 pub fn pat_bindings_hygienic<I>(dm: &RefCell<DefMap>, pat: &hir::Pat, mut it: I) where
129 I: FnMut(hir::BindingMode, ast::NodeId, Span, &Spanned<ast::Ident>),
133 hir::PatIdent(binding_mode, ref pth, _) if pat_is_binding(&dm.borrow(), p) => {
134 it(binding_mode, p.id, p.span, &respan(pth.span, pth.node));
142 /// Checks if the pattern contains any patterns that bind something to
143 /// an ident, e.g. `foo`, or `Foo(foo)` or `foo @ Bar(..)`.
144 pub fn pat_contains_bindings(dm: &DefMap, pat: &hir::Pat) -> bool {
145 let mut contains_bindings = false;
147 if pat_is_binding(dm, p) {
148 contains_bindings = true;
149 false // there's at least one binding, can short circuit now.
157 /// Checks if the pattern contains any `ref` or `ref mut` bindings,
158 /// and if yes wether its containing mutable ones or just immutables ones.
159 pub fn pat_contains_ref_binding(dm: &RefCell<DefMap>, pat: &hir::Pat) -> Option<hir::Mutability> {
160 let mut result = None;
161 pat_bindings(dm, pat, |mode, _, _, _| {
163 hir::BindingMode::BindByRef(m) => {
164 // Pick Mutable as maximum
166 None | Some(hir::MutImmutable) => result = Some(m),
170 hir::BindingMode::BindByValue(_) => { }
176 /// Checks if the patterns for this arm contain any `ref` or `ref mut`
177 /// bindings, and if yes wether its containing mutable ones or just immutables ones.
178 pub fn arm_contains_ref_binding(dm: &RefCell<DefMap>, arm: &hir::Arm) -> Option<hir::Mutability> {
180 .filter_map(|pat| pat_contains_ref_binding(dm, pat))
181 .max_by_key(|m| match *m {
182 hir::MutMutable => 1,
183 hir::MutImmutable => 0,
187 /// Checks if the pattern contains any patterns that bind something to
188 /// an ident or wildcard, e.g. `foo`, or `Foo(_)`, `foo @ Bar(..)`,
189 pub fn pat_contains_bindings_or_wild(dm: &DefMap, pat: &hir::Pat) -> bool {
190 let mut contains_bindings = false;
192 if pat_is_binding_or_wild(dm, p) {
193 contains_bindings = true;
194 false // there's at least one binding/wildcard, can short circuit now.
202 pub fn simple_name<'a>(pat: &'a hir::Pat) -> Option<ast::Name> {
204 hir::PatIdent(hir::BindByValue(_), ref path1, None) => {
205 Some(path1.node.name)
213 pub fn def_to_path(tcx: &ty::ctxt, id: DefId) -> hir::Path {
214 tcx.with_path(id, |path| hir::Path {
216 segments: path.last().map(|elem| hir::PathSegment {
217 identifier: ast::Ident::with_empty_ctxt(elem.name()),
218 parameters: hir::PathParameters::none(),
219 }).into_iter().collect(),
224 /// Return variants that are necessary to exist for the pattern to match.
225 pub fn necessary_variants(dm: &DefMap, pat: &hir::Pat) -> Vec<DefId> {
226 let mut variants = vec![];
230 hir::PatIdent(_, _, None) |
231 hir::PatStruct(..) => {
232 match dm.get(&p.id) {
233 Some(&PathResolution { base_def: DefVariant(_, id, _), .. }) => {