]> git.proxmox.com Git - rustc.git/blame - src/librustc/middle/pat_util.rs
Imported Upstream version 1.8.0+dfsg1
[rustc.git] / src / librustc / middle / pat_util.rs
CommitLineData
223e47cc
LB
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
1a4d82fc 11use middle::def::*;
e9174d1e 12use middle::def_id::DefId;
1a4d82fc
JJ
13use middle::ty;
14use util::nodemap::FnvHashMap;
223e47cc 15
1a4d82fc 16use syntax::ast;
7453a54e 17use rustc_front::hir::{self, PatKind};
e9174d1e 18use rustc_front::util::walk_pat;
b039eaaf 19use syntax::codemap::{respan, Span, Spanned, DUMMY_SP};
223e47cc 20
92a42be0
SL
21use std::cell::RefCell;
22
b039eaaf 23pub type PatIdMap = FnvHashMap<ast::Name, ast::NodeId>;
223e47cc
LB
24
25// This is used because same-named variables in alternative patterns need to
1a4d82fc 26// use the NodeId of their namesake in the first pattern.
92a42be0 27pub fn pat_id_map(dm: &RefCell<DefMap>, pat: &hir::Pat) -> PatIdMap {
85aaf69f 28 let mut map = FnvHashMap();
92a42be0
SL
29 pat_bindings(dm, pat, |_bm, p_id, _s, path1| {
30 map.insert(path1.node, p_id);
1a4d82fc 31 });
223e47cc
LB
32 map
33}
34
e9174d1e 35pub fn pat_is_refutable(dm: &DefMap, pat: &hir::Pat) -> bool {
1a4d82fc 36 match pat.node {
7453a54e
SL
37 PatKind::Lit(_) | PatKind::Range(_, _) | PatKind::QPath(..) => true,
38 PatKind::TupleStruct(..) |
39 PatKind::Path(..) |
40 PatKind::Ident(_, _, None) |
41 PatKind::Struct(..) => {
92a42be0 42 match dm.get(&pat.id).map(|d| d.full_def()) {
7453a54e 43 Some(Def::Variant(..)) => true,
1a4d82fc
JJ
44 _ => false
45 }
46 }
7453a54e 47 PatKind::Vec(_, _, _) => true,
1a4d82fc
JJ
48 _ => false
49 }
50}
51
e9174d1e 52pub fn pat_is_variant_or_struct(dm: &DefMap, pat: &hir::Pat) -> bool {
223e47cc 53 match pat.node {
7453a54e
SL
54 PatKind::TupleStruct(..) |
55 PatKind::Path(..) |
56 PatKind::Ident(_, _, None) |
57 PatKind::Struct(..) => {
92a42be0 58 match dm.get(&pat.id).map(|d| d.full_def()) {
7453a54e 59 Some(Def::Variant(..)) | Some(Def::Struct(..)) | Some(Def::TyAlias(..)) => true,
223e47cc
LB
60 _ => false
61 }
62 }
63 _ => false
64 }
65}
66
e9174d1e 67pub fn pat_is_const(dm: &DefMap, pat: &hir::Pat) -> bool {
223e47cc 68 match pat.node {
7453a54e 69 PatKind::Ident(_, _, None) | PatKind::Path(..) | PatKind::QPath(..) => {
92a42be0 70 match dm.get(&pat.id).map(|d| d.full_def()) {
7453a54e 71 Some(Def::Const(..)) | Some(Def::AssociatedConst(..)) => true,
d9579d0f
AL
72 _ => false
73 }
74 }
75 _ => false
76 }
77}
78
79// Same as above, except that partially-resolved defs cause `false` to be
80// returned instead of a panic.
e9174d1e 81pub fn pat_is_resolved_const(dm: &DefMap, pat: &hir::Pat) -> bool {
d9579d0f 82 match pat.node {
7453a54e 83 PatKind::Ident(_, _, None) | PatKind::Path(..) | PatKind::QPath(..) => {
92a42be0 84 match dm.get(&pat.id)
d9579d0f
AL
85 .and_then(|d| if d.depth == 0 { Some(d.base_def) }
86 else { None } ) {
7453a54e 87 Some(Def::Const(..)) | Some(Def::AssociatedConst(..)) => true,
223e47cc
LB
88 _ => false
89 }
90 }
91 _ => false
92 }
93}
94
e9174d1e 95pub fn pat_is_binding(dm: &DefMap, pat: &hir::Pat) -> bool {
223e47cc 96 match pat.node {
7453a54e 97 PatKind::Ident(..) => {
223e47cc
LB
98 !pat_is_variant_or_struct(dm, pat) &&
99 !pat_is_const(dm, pat)
100 }
101 _ => false
102 }
103}
104
e9174d1e 105pub fn pat_is_binding_or_wild(dm: &DefMap, pat: &hir::Pat) -> bool {
223e47cc 106 match pat.node {
7453a54e
SL
107 PatKind::Ident(..) => pat_is_binding(dm, pat),
108 PatKind::Wild => true,
223e47cc
LB
109 _ => false
110 }
111}
112
1a4d82fc
JJ
113/// Call `it` on every "binding" in a pattern, e.g., on `a` in
114/// `match foo() { Some(a) => (), None => () }`
92a42be0 115pub fn pat_bindings<I>(dm: &RefCell<DefMap>, pat: &hir::Pat, mut it: I) where
b039eaaf 116 I: FnMut(hir::BindingMode, ast::NodeId, Span, &Spanned<ast::Name>),
1a4d82fc
JJ
117{
118 walk_pat(pat, |p| {
223e47cc 119 match p.node {
7453a54e 120 PatKind::Ident(binding_mode, ref pth, _) if pat_is_binding(&dm.borrow(), p) => {
b039eaaf
SL
121 it(binding_mode, p.id, p.span, &respan(pth.span, pth.node.name));
122 }
123 _ => {}
124 }
125 true
126 });
127}
92a42be0
SL
128pub fn pat_bindings_ident<I>(dm: &RefCell<DefMap>, pat: &hir::Pat, mut it: I) where
129 I: FnMut(hir::BindingMode, ast::NodeId, Span, &Spanned<hir::Ident>),
b039eaaf
SL
130{
131 walk_pat(pat, |p| {
132 match p.node {
7453a54e 133 PatKind::Ident(binding_mode, ref pth, _) if pat_is_binding(&dm.borrow(), p) => {
b039eaaf 134 it(binding_mode, p.id, p.span, &respan(pth.span, pth.node));
223e47cc
LB
135 }
136 _ => {}
137 }
1a4d82fc
JJ
138 true
139 });
140}
141
142/// Checks if the pattern contains any patterns that bind something to
143/// an ident, e.g. `foo`, or `Foo(foo)` or `foo @ Bar(..)`.
e9174d1e 144pub fn pat_contains_bindings(dm: &DefMap, pat: &hir::Pat) -> bool {
1a4d82fc
JJ
145 let mut contains_bindings = false;
146 walk_pat(pat, |p| {
147 if pat_is_binding(dm, p) {
148 contains_bindings = true;
149 false // there's at least one binding, can short circuit now.
150 } else {
151 true
152 }
153 });
154 contains_bindings
155}
156
62682a34 157/// Checks if the pattern contains any `ref` or `ref mut` bindings,
7453a54e 158/// and if yes whether its containing mutable ones or just immutables ones.
92a42be0 159pub fn pat_contains_ref_binding(dm: &RefCell<DefMap>, pat: &hir::Pat) -> Option<hir::Mutability> {
62682a34 160 let mut result = None;
c34b1796
AL
161 pat_bindings(dm, pat, |mode, _, _, _| {
162 match mode {
e9174d1e 163 hir::BindingMode::BindByRef(m) => {
62682a34
SL
164 // Pick Mutable as maximum
165 match result {
e9174d1e 166 None | Some(hir::MutImmutable) => result = Some(m),
62682a34
SL
167 _ => (),
168 }
169 }
e9174d1e 170 hir::BindingMode::BindByValue(_) => { }
c34b1796
AL
171 }
172 });
173 result
174}
175
176/// Checks if the patterns for this arm contain any `ref` or `ref mut`
7453a54e 177/// bindings, and if yes whether its containing mutable ones or just immutables ones.
92a42be0 178pub fn arm_contains_ref_binding(dm: &RefCell<DefMap>, arm: &hir::Arm) -> Option<hir::Mutability> {
62682a34
SL
179 arm.pats.iter()
180 .filter_map(|pat| pat_contains_ref_binding(dm, pat))
92a42be0 181 .max_by_key(|m| match *m {
e9174d1e
SL
182 hir::MutMutable => 1,
183 hir::MutImmutable => 0,
62682a34 184 })
c34b1796
AL
185}
186
187/// Checks if the pattern contains any patterns that bind something to
188/// an ident or wildcard, e.g. `foo`, or `Foo(_)`, `foo @ Bar(..)`,
e9174d1e 189pub fn pat_contains_bindings_or_wild(dm: &DefMap, pat: &hir::Pat) -> bool {
c34b1796
AL
190 let mut contains_bindings = false;
191 walk_pat(pat, |p| {
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.
195 } else {
196 true
197 }
198 });
199 contains_bindings
200}
201
b039eaaf 202pub fn simple_name<'a>(pat: &'a hir::Pat) -> Option<ast::Name> {
1a4d82fc 203 match pat.node {
7453a54e 204 PatKind::Ident(hir::BindByValue(_), ref path1, None) => {
b039eaaf 205 Some(path1.node.name)
1a4d82fc
JJ
206 }
207 _ => {
208 None
209 }
223e47cc
LB
210 }
211}
212
e9174d1e
SL
213pub fn def_to_path(tcx: &ty::ctxt, id: DefId) -> hir::Path {
214 tcx.with_path(id, |path| hir::Path {
1a4d82fc 215 global: false,
e9174d1e 216 segments: path.last().map(|elem| hir::PathSegment {
92a42be0 217 identifier: hir::Ident::from_name(elem.name()),
e9174d1e 218 parameters: hir::PathParameters::none(),
1a4d82fc
JJ
219 }).into_iter().collect(),
220 span: DUMMY_SP,
221 })
223e47cc 222}
62682a34
SL
223
224/// Return variants that are necessary to exist for the pattern to match.
b039eaaf 225pub fn necessary_variants(dm: &DefMap, pat: &hir::Pat) -> Vec<DefId> {
62682a34
SL
226 let mut variants = vec![];
227 walk_pat(pat, |p| {
228 match p.node {
7453a54e
SL
229 PatKind::TupleStruct(..) |
230 PatKind::Path(..) |
231 PatKind::Ident(_, _, None) |
232 PatKind::Struct(..) => {
92a42be0 233 match dm.get(&p.id) {
7453a54e 234 Some(&PathResolution { base_def: Def::Variant(_, id), .. }) => {
b039eaaf 235 variants.push(id);
62682a34
SL
236 }
237 _ => ()
238 }
239 }
240 _ => ()
241 }
242 true
243 });
244 variants.sort();
245 variants.dedup();
246 variants
247}