2012-01-14 18:05:07 -06:00
|
|
|
import syntax::ast::*;
|
|
|
|
import syntax::ast_util;
|
2012-05-22 01:17:28 -05:00
|
|
|
import syntax::ast_util::{path_to_ident, respan, walk_pat};
|
2012-01-14 18:05:07 -06:00
|
|
|
import syntax::fold;
|
|
|
|
import syntax::fold::*;
|
2012-01-30 23:00:57 -06:00
|
|
|
import syntax::codemap::span;
|
2012-03-07 18:48:57 -06:00
|
|
|
import std::map::hashmap;
|
2012-01-14 18:05:07 -06:00
|
|
|
|
2012-02-22 09:57:23 -06:00
|
|
|
export pat_binding_ids, pat_bindings, pat_id_map;
|
|
|
|
export pat_is_variant;
|
2012-01-14 18:05:07 -06:00
|
|
|
|
2012-06-10 02:49:59 -05:00
|
|
|
type pat_id_map = std::map::hashmap<ident, node_id>;
|
2012-01-14 18:05:07 -06:00
|
|
|
|
|
|
|
// This is used because same-named variables in alternative patterns need to
|
|
|
|
// use the node_id of their namesake in the first pattern.
|
2012-08-29 15:26:26 -05:00
|
|
|
fn pat_id_map(dm: resolve::DefMap, pat: @pat) -> pat_id_map {
|
2012-07-18 18:18:02 -05:00
|
|
|
let map = std::map::uint_hash();
|
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
|
|
|
};
|
2012-08-01 19:30:05 -05:00
|
|
|
return map;
|
2012-01-14 18:05:07 -06:00
|
|
|
}
|
|
|
|
|
2012-08-29 15:26:26 -05:00
|
|
|
fn pat_is_variant(dm: resolve::DefMap, pat: @pat) -> bool {
|
2012-08-06 14:34:08 -05:00
|
|
|
match pat.node {
|
2012-08-03 21:59:04 -05:00
|
|
|
pat_enum(_, _) => true,
|
2012-08-20 14:23:37 -05:00
|
|
|
pat_ident(_, _, None) => match dm.find(pat.id) {
|
|
|
|
Some(def_variant(_, _)) => true,
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => false
|
2012-08-06 19:14:32 -05:00
|
|
|
},
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => false
|
2012-02-22 09:57:23 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-29 15:26:26 -05:00
|
|
|
fn pat_bindings(dm: resolve::DefMap, pat: @pat,
|
2012-08-06 09:20:23 -05:00
|
|
|
it: fn(binding_mode, node_id, span, @path)) {
|
2012-06-30 18:19:07 -05:00
|
|
|
do walk_pat(pat) |p| {
|
2012-08-06 14:34:08 -05:00
|
|
|
match p.node {
|
2012-08-06 09:20:23 -05:00
|
|
|
pat_ident(binding_mode, pth, _) if !pat_is_variant(dm, p) => {
|
|
|
|
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
|
|
|
}
|
2012-01-14 18:05:07 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-29 15:26:26 -05:00
|
|
|
fn pat_binding_ids(dm: resolve::DefMap, pat: @pat) -> ~[node_id] {
|
2012-06-29 18:26:56 -05:00
|
|
|
let mut found = ~[];
|
2012-08-06 09:20:23 -05:00
|
|
|
pat_bindings(dm, pat, |_bm, b_id, _sp, _pt| vec::push(found, b_id) );
|
2012-08-01 19:30:05 -05:00
|
|
|
return found;
|
2012-01-14 18:05:07 -06:00
|
|
|
}
|