rust/src/librustc/middle/pat_util.rs

122 lines
3.5 KiB
Rust
Raw Normal View History

// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use middle::resolve;
use collections::HashMap;
2012-09-04 13:54:36 -05:00
use syntax::ast::*;
2013-01-30 11:56:33 -06:00
use syntax::ast_util::{path_to_ident, walk_pat};
use syntax::codemap::Span;
2013-09-01 19:50:59 -05:00
pub type PatIdMap = HashMap<Ident, NodeId>;
// This is used because same-named variables in alternative patterns need to
// use the NodeId of their namesake in the first pattern.
2013-11-25 08:37:03 -06:00
pub fn pat_id_map(dm: resolve::DefMap, pat: &Pat) -> PatIdMap {
let mut map = HashMap::new();
pat_bindings(dm, pat, |_bm, p_id, _s, n| {
map.insert(path_to_ident(n), p_id);
});
2013-03-03 13:44:11 -06:00
map
}
pub fn pat_is_variant_or_struct(dm: resolve::DefMap, pat: &Pat) -> bool {
2012-08-06 14:34:08 -05:00
match pat.node {
2013-11-28 14:22:53 -06:00
PatEnum(_, _) | PatIdent(_, _, None) | PatStruct(..) => {
let dm = dm.borrow();
match dm.get().find(&pat.id) {
2013-11-28 14:22:53 -06:00
Some(&DefVariant(..)) | Some(&DefStruct(..)) => true,
_ => false
}
}
2012-08-03 21:59:04 -05:00
_ => false
}
}
pub fn pat_is_const(dm: resolve::DefMap, pat: &Pat) -> bool {
match pat.node {
2013-11-28 14:22:53 -06:00
PatIdent(_, _, None) | PatEnum(..) => {
let dm = dm.borrow();
match dm.get().find(&pat.id) {
Some(&DefStatic(_, false)) => true,
_ => false
}
}
_ => false
}
}
2013-11-25 08:37:03 -06:00
pub fn pat_is_binding(dm: resolve::DefMap, pat: &Pat) -> bool {
match pat.node {
2013-11-28 14:22:53 -06:00
PatIdent(..) => {
!pat_is_variant_or_struct(dm, pat) &&
!pat_is_const(dm, pat)
}
_ => false
}
}
2013-11-25 08:37:03 -06:00
pub fn pat_is_binding_or_wild(dm: resolve::DefMap, pat: &Pat) -> bool {
match pat.node {
2013-11-28 14:22:53 -06:00
PatIdent(..) => pat_is_binding(dm, pat),
2013-11-07 21:25:39 -06:00
PatWild | PatWildMulti => true,
_ => false
}
}
/// Call `it` on every "binding" in a pattern, e.g., on `a` in
/// `match foo() { Some(a) => (), None => () }`
pub fn pat_bindings(dm: resolve::DefMap,
2013-11-25 08:37:03 -06:00
pat: &Pat,
it: |BindingMode, NodeId, Span, &Path|) {
walk_pat(pat, |p| {
2012-08-06 14:34:08 -05:00
match p.node {
PatIdent(binding_mode, ref pth, _) if pat_is_binding(dm, p) => {
it(binding_mode, p.id, p.span, pth);
}
2012-08-03 21:59:04 -05:00
_ => {}
}
true
});
}
2013-11-25 08:37:03 -06:00
pub fn pat_binding_ids(dm: resolve::DefMap, pat: &Pat) -> ~[NodeId] {
let mut found = ~[];
pat_bindings(dm, pat, |_bm, b_id, _sp, _pt| found.push(b_id) );
2012-08-01 19:30:05 -05:00
return found;
}
/// Checks if the pattern contains any patterns that bind something to
2013-11-28 14:22:53 -06:00
/// an ident, e.g. `foo`, or `Foo(foo)` or `foo @ Bar(..)`.
2013-11-25 08:37:03 -06:00
pub fn pat_contains_bindings(dm: resolve::DefMap, pat: &Pat) -> bool {
let mut contains_bindings = false;
walk_pat(pat, |p| {
if pat_is_binding(dm, p) {
contains_bindings = true;
false // there's at least one binding, can short circuit now.
} else {
true
}
});
contains_bindings
}
pub fn simple_identifier<'a>(pat: &'a Pat) -> Option<&'a Path> {
match pat.node {
PatIdent(BindByValue(_), ref path, None) => {
Some(path)
}
_ => {
None
}
}
}