2012-12-03 18:48:01 -06:00
|
|
|
// 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.
|
|
|
|
|
2013-05-17 17:28:44 -05:00
|
|
|
|
2012-12-23 16:41:37 -06:00
|
|
|
use middle::resolve;
|
2012-12-07 21:34:57 -06:00
|
|
|
|
2013-06-28 17:32:26 -05:00
|
|
|
use std::hashmap::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};
|
2013-08-31 11:13:04 -05:00
|
|
|
use syntax::codemap::Span;
|
2012-01-14 18:05:07 -06:00
|
|
|
|
2013-09-01 19:50:59 -05:00
|
|
|
pub type PatIdMap = HashMap<Ident, NodeId>;
|
2012-01-14 18:05:07 -06:00
|
|
|
|
|
|
|
// This is used because same-named variables in alternative patterns need to
|
2013-07-27 03:25:59 -05:00
|
|
|
// use the NodeId of their namesake in the first pattern.
|
2013-01-30 15:44:24 -06:00
|
|
|
pub fn pat_id_map(dm: resolve::DefMap, pat: @pat) -> PatIdMap {
|
2013-04-03 08:28:36 -05:00
|
|
|
let mut map = HashMap::new();
|
2012-08-06 09:20:23 -05:00
|
|
|
do pat_bindings(dm, pat) |_bm, p_id, _s, n| {
|
2012-01-30 23:00:57 -06:00
|
|
|
map.insert(path_to_ident(n), p_id);
|
2012-01-14 18:05:07 -06:00
|
|
|
};
|
2013-03-03 13:44:11 -06:00
|
|
|
map
|
2012-01-14 18:05:07 -06:00
|
|
|
}
|
|
|
|
|
2013-06-27 08:04:22 -05:00
|
|
|
pub fn pat_is_variant_or_struct(dm: resolve::DefMap, pat: &pat) -> bool {
|
2012-08-06 14:34:08 -05:00
|
|
|
match pat.node {
|
2012-10-30 17:53:06 -05:00
|
|
|
pat_enum(_, _) | pat_ident(_, _, None) | pat_struct(*) => {
|
2013-02-05 21:41:45 -06:00
|
|
|
match dm.find(&pat.id) {
|
2013-03-22 21:26:41 -05:00
|
|
|
Some(&def_variant(*)) | Some(&def_struct(*)) => true,
|
2012-10-30 17:53:06 -05:00
|
|
|
_ => false
|
|
|
|
}
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => false
|
2012-02-22 09:57:23 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-30 15:44:24 -06:00
|
|
|
pub fn pat_is_const(dm: resolve::DefMap, pat: &pat) -> bool {
|
2012-11-13 00:10:15 -06:00
|
|
|
match pat.node {
|
2013-03-21 02:27:26 -05:00
|
|
|
pat_ident(_, _, None) | pat_enum(*) => {
|
2013-02-05 21:41:45 -06:00
|
|
|
match dm.find(&pat.id) {
|
2013-06-21 20:46:34 -05:00
|
|
|
Some(&def_static(_, false)) => true,
|
2012-11-13 00:10:15 -06:00
|
|
|
_ => false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-30 15:44:24 -06:00
|
|
|
pub fn pat_is_binding(dm: resolve::DefMap, pat: @pat) -> bool {
|
2012-11-13 00:10:15 -06:00
|
|
|
match pat.node {
|
|
|
|
pat_ident(*) => {
|
|
|
|
!pat_is_variant_or_struct(dm, pat) &&
|
|
|
|
!pat_is_const(dm, pat)
|
|
|
|
}
|
|
|
|
_ => false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-30 15:44:24 -06:00
|
|
|
pub fn pat_is_binding_or_wild(dm: resolve::DefMap, pat: @pat) -> bool {
|
2012-09-12 19:06:36 -05:00
|
|
|
match pat.node {
|
2012-11-13 00:10:15 -06:00
|
|
|
pat_ident(*) => pat_is_binding(dm, pat),
|
2012-09-12 19:06:36 -05:00
|
|
|
pat_wild => true,
|
|
|
|
_ => false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-30 15:44:24 -06:00
|
|
|
pub fn pat_bindings(dm: resolve::DefMap, pat: @pat,
|
2013-08-31 11:13:04 -05:00
|
|
|
it: &fn(binding_mode, NodeId, Span, &Path)) {
|
2013-08-02 01:17:20 -05:00
|
|
|
do walk_pat(pat) |p| {
|
2012-08-06 14:34:08 -05:00
|
|
|
match p.node {
|
2013-07-05 05:15:21 -05:00
|
|
|
pat_ident(binding_mode, ref pth, _) if pat_is_binding(dm, p) => {
|
2012-08-06 09:20:23 -05:00
|
|
|
it(binding_mode, p.id, p.span, pth);
|
2012-02-22 09:57:23 -06:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => {}
|
2012-02-22 09:57:23 -06:00
|
|
|
}
|
2013-08-02 01:17:20 -05:00
|
|
|
true
|
|
|
|
};
|
2012-01-14 18:05:07 -06:00
|
|
|
}
|
|
|
|
|
2013-07-27 03:25:59 -05:00
|
|
|
pub fn pat_binding_ids(dm: resolve::DefMap, pat: @pat) -> ~[NodeId] {
|
2012-06-29 18:26:56 -05:00
|
|
|
let mut found = ~[];
|
2012-09-26 19:33:34 -05:00
|
|
|
pat_bindings(dm, pat, |_bm, b_id, _sp, _pt| found.push(b_id) );
|
2012-08-01 19:30:05 -05:00
|
|
|
return found;
|
2012-01-14 18:05:07 -06:00
|
|
|
}
|
2013-08-14 04:04:41 -05:00
|
|
|
|
|
|
|
/// Checks if the pattern contains any patterns that bind something to
|
|
|
|
/// an ident, e.g. `foo`, or `Foo(foo)` or `foo @ Bar(*)`.
|
|
|
|
pub fn pat_contains_bindings(dm: resolve::DefMap, pat: @pat) -> bool {
|
|
|
|
let mut contains_bindings = false;
|
|
|
|
do 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
|
|
|
|
}
|