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;
18 use rustc_front::util::walk_pat;
19 use syntax::codemap::{respan, Span, Spanned, DUMMY_SP};
21 use std::cell::RefCell;
23 pub type PatIdMap = FnvHashMap<ast::Name, ast::NodeId>;
25 // This is used because same-named variables in alternative patterns need to
26 // use the NodeId of their namesake in the first pattern.
27 pub fn pat_id_map(dm: &RefCell<DefMap>, pat: &hir::Pat) -> PatIdMap {
28 let mut map = FnvHashMap();
29 pat_bindings(dm, pat, |_bm, p_id, _s, path1| {
30 map.insert(path1.node, p_id);
35 pub fn pat_is_refutable(dm: &DefMap, pat: &hir::Pat) -> bool {
37 hir::PatLit(_) | hir::PatRange(_, _) | hir::PatQPath(..) => true,
39 hir::PatIdent(_, _, None) |
40 hir::PatStruct(..) => {
41 match dm.get(&pat.id).map(|d| d.full_def()) {
42 Some(Def::Variant(..)) => true,
46 hir::PatVec(_, _, _) => true,
51 pub fn pat_is_variant_or_struct(dm: &DefMap, pat: &hir::Pat) -> bool {
54 hir::PatIdent(_, _, None) |
55 hir::PatStruct(..) => {
56 match dm.get(&pat.id).map(|d| d.full_def()) {
57 Some(Def::Variant(..)) | Some(Def::Struct(..)) => true,
65 pub fn pat_is_const(dm: &DefMap, pat: &hir::Pat) -> bool {
67 hir::PatIdent(_, _, None) | hir::PatEnum(..) | hir::PatQPath(..) => {
68 match dm.get(&pat.id).map(|d| d.full_def()) {
69 Some(Def::Const(..)) | Some(Def::AssociatedConst(..)) => true,
77 // Same as above, except that partially-resolved defs cause `false` to be
78 // returned instead of a panic.
79 pub fn pat_is_resolved_const(dm: &DefMap, pat: &hir::Pat) -> bool {
81 hir::PatIdent(_, _, None) | hir::PatEnum(..) | hir::PatQPath(..) => {
83 .and_then(|d| if d.depth == 0 { Some(d.base_def) }
85 Some(Def::Const(..)) | Some(Def::AssociatedConst(..)) => true,
93 pub fn pat_is_binding(dm: &DefMap, pat: &hir::Pat) -> bool {
95 hir::PatIdent(..) => {
96 !pat_is_variant_or_struct(dm, pat) &&
97 !pat_is_const(dm, pat)
103 pub fn pat_is_binding_or_wild(dm: &DefMap, pat: &hir::Pat) -> bool {
105 hir::PatIdent(..) => pat_is_binding(dm, pat),
106 hir::PatWild => true,
111 /// Call `it` on every "binding" in a pattern, e.g., on `a` in
112 /// `match foo() { Some(a) => (), None => () }`
113 pub fn pat_bindings<I>(dm: &RefCell<DefMap>, pat: &hir::Pat, mut it: I) where
114 I: FnMut(hir::BindingMode, ast::NodeId, Span, &Spanned<ast::Name>),
118 hir::PatIdent(binding_mode, ref pth, _) if pat_is_binding(&dm.borrow(), p) => {
119 it(binding_mode, p.id, p.span, &respan(pth.span, pth.node.name));
126 pub fn pat_bindings_ident<I>(dm: &RefCell<DefMap>, pat: &hir::Pat, mut it: I) where
127 I: FnMut(hir::BindingMode, ast::NodeId, Span, &Spanned<hir::Ident>),
131 hir::PatIdent(binding_mode, ref pth, _) if pat_is_binding(&dm.borrow(), p) => {
132 it(binding_mode, p.id, p.span, &respan(pth.span, pth.node));
140 /// Checks if the pattern contains any patterns that bind something to
141 /// an ident, e.g. `foo`, or `Foo(foo)` or `foo @ Bar(..)`.
142 pub fn pat_contains_bindings(dm: &DefMap, pat: &hir::Pat) -> bool {
143 let mut contains_bindings = false;
145 if pat_is_binding(dm, p) {
146 contains_bindings = true;
147 false // there's at least one binding, can short circuit now.
155 /// Checks if the pattern contains any `ref` or `ref mut` bindings,
156 /// and if yes whether its containing mutable ones or just immutables ones.
157 pub fn pat_contains_ref_binding(dm: &RefCell<DefMap>, pat: &hir::Pat) -> Option<hir::Mutability> {
158 let mut result = None;
159 pat_bindings(dm, pat, |mode, _, _, _| {
161 hir::BindingMode::BindByRef(m) => {
162 // Pick Mutable as maximum
164 None | Some(hir::MutImmutable) => result = Some(m),
168 hir::BindingMode::BindByValue(_) => { }
174 /// Checks if the patterns for this arm contain any `ref` or `ref mut`
175 /// bindings, and if yes whether its containing mutable ones or just immutables ones.
176 pub fn arm_contains_ref_binding(dm: &RefCell<DefMap>, arm: &hir::Arm) -> Option<hir::Mutability> {
178 .filter_map(|pat| pat_contains_ref_binding(dm, pat))
179 .max_by_key(|m| match *m {
180 hir::MutMutable => 1,
181 hir::MutImmutable => 0,
185 /// Checks if the pattern contains any patterns that bind something to
186 /// an ident or wildcard, e.g. `foo`, or `Foo(_)`, `foo @ Bar(..)`,
187 pub fn pat_contains_bindings_or_wild(dm: &DefMap, pat: &hir::Pat) -> bool {
188 let mut contains_bindings = false;
190 if pat_is_binding_or_wild(dm, p) {
191 contains_bindings = true;
192 false // there's at least one binding/wildcard, can short circuit now.
200 pub fn simple_name<'a>(pat: &'a hir::Pat) -> Option<ast::Name> {
202 hir::PatIdent(hir::BindByValue(_), ref path1, None) => {
203 Some(path1.node.name)
211 pub fn def_to_path(tcx: &ty::ctxt, id: DefId) -> hir::Path {
212 tcx.with_path(id, |path| hir::Path {
214 segments: path.last().map(|elem| hir::PathSegment {
215 identifier: hir::Ident::from_name(elem.name()),
216 parameters: hir::PathParameters::none(),
217 }).into_iter().collect(),
222 /// Return variants that are necessary to exist for the pattern to match.
223 pub fn necessary_variants(dm: &DefMap, pat: &hir::Pat) -> Vec<DefId> {
224 let mut variants = vec![];
228 hir::PatIdent(_, _, None) |
229 hir::PatStruct(..) => {
230 match dm.get(&p.id) {
231 Some(&PathResolution { base_def: Def::Variant(_, id), .. }) => {