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
|
|
|
|
|
|
|
type pat_id_map = std::map::hashmap<str, node_id>;
|
|
|
|
|
|
|
|
// This is used because same-named variables in alternative patterns need to
|
|
|
|
// use the node_id of their namesake in the first pattern.
|
2012-02-22 09:57:23 -06:00
|
|
|
fn pat_id_map(dm: resolve::def_map, pat: @pat) -> pat_id_map {
|
2012-03-14 14:07:23 -05:00
|
|
|
let map = std::map::str_hash();
|
2012-02-22 09:57:23 -06:00
|
|
|
pat_bindings(dm, pat) {|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
|
|
|
};
|
|
|
|
ret map;
|
|
|
|
}
|
|
|
|
|
2012-02-22 09:57:23 -06:00
|
|
|
fn pat_is_variant(dm: resolve::def_map, pat: @pat) -> bool {
|
|
|
|
alt pat.node {
|
|
|
|
pat_enum(_, _) { true }
|
|
|
|
pat_ident(_, none) {
|
|
|
|
alt dm.find(pat.id) {
|
|
|
|
some(def_variant(_, _)) { true }
|
|
|
|
_ { false }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ { false }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-14 18:05:07 -06:00
|
|
|
// This does *not* normalize. The pattern should be already normalized
|
|
|
|
// if you want to get a normalized pattern out of it.
|
|
|
|
// Could return a constrained type in order to express that (future work)
|
2012-02-22 09:57:23 -06:00
|
|
|
fn pat_bindings(dm: resolve::def_map, pat: @pat,
|
|
|
|
it: fn(node_id, span, @path)) {
|
|
|
|
walk_pat(pat) {|p|
|
|
|
|
alt p.node {
|
|
|
|
pat_ident(pth, _) if !pat_is_variant(dm, p) {
|
|
|
|
it(p.id, p.span, pth);
|
|
|
|
}
|
|
|
|
_ {}
|
|
|
|
}
|
2012-01-14 18:05:07 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-22 09:57:23 -06:00
|
|
|
fn pat_binding_ids(dm: resolve::def_map, pat: @pat) -> [node_id] {
|
2012-03-15 08:47:03 -05:00
|
|
|
let mut found = [];
|
2012-02-22 09:57:23 -06:00
|
|
|
pat_bindings(dm, pat) {|b_id, _sp, _pt| found += [b_id]; };
|
2012-01-14 18:05:07 -06:00
|
|
|
ret found;
|
|
|
|
}
|