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.
|
|
|
|
|
2014-05-14 14:31:30 -05:00
|
|
|
use middle::def::*;
|
2014-07-13 08:12:47 -05:00
|
|
|
use middle::ty;
|
2014-11-09 16:59:56 -06:00
|
|
|
use util::nodemap::FnvHashMap;
|
2012-12-07 21:34:57 -06:00
|
|
|
|
2014-09-13 12:10:34 -05:00
|
|
|
use syntax::ast;
|
2014-06-30 20:02:14 -05:00
|
|
|
use syntax::ast_util::{walk_pat};
|
2014-06-25 12:07:37 -05:00
|
|
|
use syntax::codemap::{Span, DUMMY_SP};
|
2012-01-14 18:05:07 -06:00
|
|
|
|
2014-09-13 12:10:34 -05:00
|
|
|
pub type PatIdMap = FnvHashMap<ast::Ident, ast::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.
|
2014-12-18 13:03:56 -06:00
|
|
|
pub fn pat_id_map(dm: &DefMap, pat: &ast::Pat) -> PatIdMap {
|
2015-01-16 16:27:43 -06:00
|
|
|
let mut map = FnvHashMap();
|
2014-06-30 20:02:14 -05:00
|
|
|
pat_bindings(dm, pat, |_bm, p_id, _s, path1| {
|
2014-10-11 07:05:16 -05:00
|
|
|
map.insert(path1.node, p_id);
|
2013-11-21 17:42:55 -06:00
|
|
|
});
|
2013-03-03 13:44:11 -06:00
|
|
|
map
|
2012-01-14 18:05:07 -06:00
|
|
|
}
|
|
|
|
|
2014-12-18 13:03:56 -06:00
|
|
|
pub fn pat_is_refutable(dm: &DefMap, pat: &ast::Pat) -> bool {
|
2014-10-11 07:05:16 -05:00
|
|
|
match pat.node {
|
2014-09-13 12:10:34 -05:00
|
|
|
ast::PatLit(_) | ast::PatRange(_, _) => true,
|
|
|
|
ast::PatEnum(_, _) |
|
|
|
|
ast::PatIdent(_, _, None) |
|
|
|
|
ast::PatStruct(..) => {
|
2015-02-16 22:44:23 -06:00
|
|
|
match dm.borrow().get(&pat.id).map(|d| d.full_def()) {
|
|
|
|
Some(DefVariant(..)) => true,
|
2014-10-11 07:05:16 -05:00
|
|
|
_ => false
|
|
|
|
}
|
|
|
|
}
|
2014-09-13 12:10:34 -05:00
|
|
|
ast::PatVec(_, _, _) => true,
|
2014-10-11 07:05:16 -05:00
|
|
|
_ => false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-18 13:03:56 -06:00
|
|
|
pub fn pat_is_variant_or_struct(dm: &DefMap, pat: &ast::Pat) -> bool {
|
2012-08-06 14:34:08 -05:00
|
|
|
match pat.node {
|
2014-09-13 12:10:34 -05:00
|
|
|
ast::PatEnum(_, _) |
|
|
|
|
ast::PatIdent(_, _, None) |
|
|
|
|
ast::PatStruct(..) => {
|
2015-02-16 22:44:23 -06:00
|
|
|
match dm.borrow().get(&pat.id).map(|d| d.full_def()) {
|
|
|
|
Some(DefVariant(..)) | Some(DefStruct(..)) => 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-18 13:03:56 -06:00
|
|
|
pub fn pat_is_const(dm: &DefMap, pat: &ast::Pat) -> bool {
|
2012-11-13 00:10:15 -06:00
|
|
|
match pat.node {
|
2014-09-13 12:10:34 -05:00
|
|
|
ast::PatIdent(_, _, None) | ast::PatEnum(..) => {
|
2015-02-16 22:44:23 -06:00
|
|
|
match dm.borrow().get(&pat.id).map(|d| d.full_def()) {
|
|
|
|
Some(DefConst(..)) => true,
|
2012-11-13 00:10:15 -06:00
|
|
|
_ => false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-18 13:03:56 -06:00
|
|
|
pub fn pat_is_binding(dm: &DefMap, pat: &ast::Pat) -> bool {
|
2012-11-13 00:10:15 -06:00
|
|
|
match pat.node {
|
2014-09-13 12:10:34 -05:00
|
|
|
ast::PatIdent(..) => {
|
2012-11-13 00:10:15 -06:00
|
|
|
!pat_is_variant_or_struct(dm, pat) &&
|
|
|
|
!pat_is_const(dm, pat)
|
|
|
|
}
|
|
|
|
_ => false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-18 13:03:56 -06:00
|
|
|
pub fn pat_is_binding_or_wild(dm: &DefMap, pat: &ast::Pat) -> bool {
|
2012-09-12 19:06:36 -05:00
|
|
|
match pat.node {
|
2014-09-13 12:10:34 -05:00
|
|
|
ast::PatIdent(..) => pat_is_binding(dm, pat),
|
|
|
|
ast::PatWild(_) => true,
|
2012-09-12 19:06:36 -05:00
|
|
|
_ => false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-24 17:38:41 -06:00
|
|
|
/// Call `it` on every "binding" in a pattern, e.g., on `a` in
|
|
|
|
/// `match foo() { Some(a) => (), None => () }`
|
2014-12-18 13:03:56 -06:00
|
|
|
pub fn pat_bindings<I>(dm: &DefMap, pat: &ast::Pat, mut it: I) where
|
2014-12-08 19:26:43 -06:00
|
|
|
I: FnMut(ast::BindingMode, ast::NodeId, Span, &ast::SpannedIdent),
|
|
|
|
{
|
2013-11-21 17:42:55 -06:00
|
|
|
walk_pat(pat, |p| {
|
2012-08-06 14:34:08 -05:00
|
|
|
match p.node {
|
2014-09-13 12:10:34 -05:00
|
|
|
ast::PatIdent(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
|
2013-11-21 17:42:55 -06:00
|
|
|
});
|
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
|
2013-11-28 14:22:53 -06:00
|
|
|
/// an ident, e.g. `foo`, or `Foo(foo)` or `foo @ Bar(..)`.
|
2014-12-18 13:03:56 -06:00
|
|
|
pub fn pat_contains_bindings(dm: &DefMap, pat: &ast::Pat) -> bool {
|
2013-08-14 04:04:41 -05:00
|
|
|
let mut contains_bindings = false;
|
2013-11-21 17:42:55 -06:00
|
|
|
walk_pat(pat, |p| {
|
2013-08-14 04:04:41 -05: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 17:42:55 -06:00
|
|
|
});
|
2013-08-14 04:04:41 -05:00
|
|
|
contains_bindings
|
|
|
|
}
|
2014-01-15 13:39:08 -06:00
|
|
|
|
2015-02-19 10:54:41 -06:00
|
|
|
/// Checks if the pattern contains any patterns that bind something to
|
|
|
|
/// an ident or wildcard, e.g. `foo`, or `Foo(_)`, `foo @ Bar(..)`,
|
|
|
|
pub fn pat_contains_bindings_or_wild(dm: &DefMap, pat: &ast::Pat) -> bool {
|
|
|
|
let mut contains_bindings = false;
|
|
|
|
walk_pat(pat, |p| {
|
|
|
|
if pat_is_binding_or_wild(dm, p) {
|
|
|
|
contains_bindings = true;
|
|
|
|
false // there's at least one binding/wildcard, can short circuit now.
|
|
|
|
} else {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
});
|
|
|
|
contains_bindings
|
|
|
|
}
|
|
|
|
|
2014-09-13 12:10:34 -05:00
|
|
|
pub fn simple_identifier<'a>(pat: &'a ast::Pat) -> Option<&'a ast::Ident> {
|
2014-01-15 13:39:08 -06:00
|
|
|
match pat.node {
|
2014-09-13 12:10:34 -05:00
|
|
|
ast::PatIdent(ast::BindByValue(_), ref path1, None) => {
|
2014-06-30 20:02:14 -05:00
|
|
|
Some(&path1.node)
|
2014-01-15 13:39:08 -06:00
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-06-25 12:07:37 -05:00
|
|
|
|
2014-09-13 12:10:34 -05:00
|
|
|
pub fn def_to_path(tcx: &ty::ctxt, id: ast::DefId) -> ast::Path {
|
2014-11-06 11:32:37 -06:00
|
|
|
ty::with_path(tcx, id, |path| ast::Path {
|
2014-07-13 08:12:47 -05:00
|
|
|
global: false,
|
2014-09-13 12:10:34 -05:00
|
|
|
segments: path.last().map(|elem| ast::PathSegment {
|
|
|
|
identifier: ast::Ident::new(elem.name()),
|
|
|
|
parameters: ast::PathParameters::none(),
|
2014-09-14 22:27:36 -05:00
|
|
|
}).into_iter().collect(),
|
2014-07-13 08:12:47 -05:00
|
|
|
span: DUMMY_SP,
|
|
|
|
})
|
|
|
|
}
|