2012-12-04 00:48:01 +00: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.
|
|
|
|
|
2014-05-14 19:31:30 +00:00
|
|
|
use middle::def::*;
|
2012-12-23 22:41:37 +00:00
|
|
|
use middle::resolve;
|
2012-12-08 03:34:57 +00:00
|
|
|
|
2014-05-30 02:03:06 +00:00
|
|
|
use std::collections::HashMap;
|
2014-06-25 17:07:37 +00:00
|
|
|
use std::gc::{Gc, GC};
|
2012-09-04 18:54:36 +00:00
|
|
|
use syntax::ast::*;
|
2013-01-30 17:56:33 +00:00
|
|
|
use syntax::ast_util::{path_to_ident, walk_pat};
|
2014-06-25 17:07:37 +00:00
|
|
|
use syntax::codemap::{Span, DUMMY_SP};
|
2012-01-15 00:05:07 +00:00
|
|
|
|
2013-09-02 00:50:59 +00:00
|
|
|
pub type PatIdMap = HashMap<Ident, NodeId>;
|
2012-01-15 00:05:07 +00:00
|
|
|
|
|
|
|
// This is used because same-named variables in alternative patterns need to
|
2013-07-27 08:25:59 +00:00
|
|
|
// use the NodeId of their namesake in the first pattern.
|
2014-04-22 16:06:43 +00:00
|
|
|
pub fn pat_id_map(dm: &resolve::DefMap, pat: &Pat) -> PatIdMap {
|
2013-04-03 13:28:36 +00:00
|
|
|
let mut map = HashMap::new();
|
2013-11-21 23:42:55 +00:00
|
|
|
pat_bindings(dm, pat, |_bm, p_id, _s, n| {
|
2012-01-31 05:00:57 +00:00
|
|
|
map.insert(path_to_ident(n), p_id);
|
2013-11-21 23:42:55 +00:00
|
|
|
});
|
2013-03-03 19:44:11 +00:00
|
|
|
map
|
2012-01-15 00:05:07 +00:00
|
|
|
}
|
|
|
|
|
2014-04-22 16:06:43 +00:00
|
|
|
pub fn pat_is_variant_or_struct(dm: &resolve::DefMap, pat: &Pat) -> bool {
|
2012-08-06 19:34:08 +00:00
|
|
|
match pat.node {
|
2013-11-28 20:22:53 +00:00
|
|
|
PatEnum(_, _) | PatIdent(_, _, None) | PatStruct(..) => {
|
2014-03-21 02:49:20 +00:00
|
|
|
match dm.borrow().find(&pat.id) {
|
2013-11-28 20:22:53 +00:00
|
|
|
Some(&DefVariant(..)) | Some(&DefStruct(..)) => true,
|
2012-10-30 22:53:06 +00:00
|
|
|
_ => false
|
|
|
|
}
|
|
|
|
}
|
2012-08-04 02:59:04 +00:00
|
|
|
_ => false
|
2012-02-22 15:57:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-22 16:06:43 +00:00
|
|
|
pub fn pat_is_const(dm: &resolve::DefMap, pat: &Pat) -> bool {
|
2012-11-13 06:10:15 +00:00
|
|
|
match pat.node {
|
2013-11-28 20:22:53 +00:00
|
|
|
PatIdent(_, _, None) | PatEnum(..) => {
|
2014-03-21 02:49:20 +00:00
|
|
|
match dm.borrow().find(&pat.id) {
|
2013-09-02 01:45:37 +00:00
|
|
|
Some(&DefStatic(_, false)) => true,
|
2012-11-13 06:10:15 +00:00
|
|
|
_ => false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-22 16:06:43 +00:00
|
|
|
pub fn pat_is_binding(dm: &resolve::DefMap, pat: &Pat) -> bool {
|
2012-11-13 06:10:15 +00:00
|
|
|
match pat.node {
|
2013-11-28 20:22:53 +00:00
|
|
|
PatIdent(..) => {
|
2012-11-13 06:10:15 +00:00
|
|
|
!pat_is_variant_or_struct(dm, pat) &&
|
|
|
|
!pat_is_const(dm, pat)
|
|
|
|
}
|
|
|
|
_ => false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-22 16:06:43 +00:00
|
|
|
pub fn pat_is_binding_or_wild(dm: &resolve::DefMap, pat: &Pat) -> bool {
|
2012-09-13 00:06:36 +00:00
|
|
|
match pat.node {
|
2013-11-28 20:22:53 +00:00
|
|
|
PatIdent(..) => pat_is_binding(dm, pat),
|
2013-11-08 03:25:39 +00:00
|
|
|
PatWild | PatWildMulti => true,
|
2012-09-13 00:06:36 +00:00
|
|
|
_ => false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-24 23:38:41 +00:00
|
|
|
/// Call `it` on every "binding" in a pattern, e.g., on `a` in
|
|
|
|
/// `match foo() { Some(a) => (), None => () }`
|
2014-04-22 16:06:43 +00:00
|
|
|
pub fn pat_bindings(dm: &resolve::DefMap,
|
2013-11-25 14:37:03 +00:00
|
|
|
pat: &Pat,
|
2013-11-19 21:22:03 +00:00
|
|
|
it: |BindingMode, NodeId, Span, &Path|) {
|
2013-11-21 23:42:55 +00:00
|
|
|
walk_pat(pat, |p| {
|
2012-08-06 19:34:08 +00:00
|
|
|
match p.node {
|
2013-09-02 01:45:37 +00:00
|
|
|
PatIdent(binding_mode, ref pth, _) if pat_is_binding(dm, p) => {
|
2012-08-06 14:20:23 +00:00
|
|
|
it(binding_mode, p.id, p.span, pth);
|
2012-02-22 15:57:23 +00:00
|
|
|
}
|
2012-08-04 02:59:04 +00:00
|
|
|
_ => {}
|
2012-02-22 15:57:23 +00:00
|
|
|
}
|
2013-08-02 06:17:20 +00:00
|
|
|
true
|
2013-11-21 23:42:55 +00:00
|
|
|
});
|
2012-01-15 00:05:07 +00:00
|
|
|
}
|
|
|
|
|
2013-08-14 09:04:41 +00:00
|
|
|
/// Checks if the pattern contains any patterns that bind something to
|
2013-11-28 20:22:53 +00:00
|
|
|
/// an ident, e.g. `foo`, or `Foo(foo)` or `foo @ Bar(..)`.
|
2014-04-22 16:06:43 +00:00
|
|
|
pub fn pat_contains_bindings(dm: &resolve::DefMap, pat: &Pat) -> bool {
|
2013-08-14 09:04:41 +00:00
|
|
|
let mut contains_bindings = false;
|
2013-11-21 23:42:55 +00:00
|
|
|
walk_pat(pat, |p| {
|
2013-08-14 09:04:41 +00:00
|
|
|
if pat_is_binding(dm, p) {
|
|
|
|
contains_bindings = true;
|
|
|
|
false // there's at least one binding, can short circuit now.
|
|
|
|
} else {
|
|
|
|
true
|
|
|
|
}
|
2013-11-21 23:42:55 +00:00
|
|
|
});
|
2013-08-14 09:04:41 +00:00
|
|
|
contains_bindings
|
|
|
|
}
|
2014-01-15 19:39:08 +00:00
|
|
|
|
|
|
|
pub fn simple_identifier<'a>(pat: &'a Pat) -> Option<&'a Path> {
|
|
|
|
match pat.node {
|
|
|
|
PatIdent(BindByValue(_), ref path, None) => {
|
|
|
|
Some(path)
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-06-25 17:07:37 +00:00
|
|
|
|
|
|
|
pub fn wild() -> Gc<Pat> {
|
|
|
|
box (GC) Pat { id: 0, node: PatWild, span: DUMMY_SP }
|
|
|
|
}
|