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