]> git.lizzy.rs Git - rust.git/blobdiff - src/librustc/middle/pat_util.rs
Remove random Idents outside of libsyntax
[rust.git] / src / librustc / middle / pat_util.rs
index 7aac1a376e41a7f55e61483f87caaa0678185b16..f72a945741ff798d21030bbac9cbd56961a758c3 100644 (file)
 use util::nodemap::FnvHashMap;
 
 use syntax::ast;
-use syntax::ast_util::walk_pat;
-use syntax::codemap::{Span, DUMMY_SP};
+use rustc_front::hir;
+use rustc_front::util::walk_pat;
+use syntax::codemap::{respan, Span, Spanned, DUMMY_SP};
 
-pub type PatIdMap = FnvHashMap<ast::Ident, ast::NodeId>;
+pub type PatIdMap = FnvHashMap<ast::Name, ast::NodeId>;
 
 // This is used because same-named variables in alternative patterns need to
 // use the NodeId of their namesake in the first pattern.
-pub fn pat_id_map(dm: &DefMap, pat: &ast::Pat) -> PatIdMap {
+pub fn pat_id_map(dm: &DefMap, pat: &hir::Pat) -> PatIdMap {
     let mut map = FnvHashMap();
     pat_bindings(dm, pat, |_bm, p_id, _s, path1| {
         map.insert(path1.node, p_id);
@@ -29,27 +30,27 @@ pub fn pat_id_map(dm: &DefMap, pat: &ast::Pat) -> PatIdMap {
     map
 }
 
-pub fn pat_is_refutable(dm: &DefMap, pat: &ast::Pat) -> bool {
+pub fn pat_is_refutable(dm: &DefMap, pat: &hir::Pat) -> bool {
     match pat.node {
-        ast::PatLit(_) | ast::PatRange(_, _) | ast::PatQPath(..) => true,
-        ast::PatEnum(_, _) |
-        ast::PatIdent(_, _, None) |
-        ast::PatStruct(..) => {
+        hir::PatLit(_) | hir::PatRange(_, _) | hir::PatQPath(..) => true,
+        hir::PatEnum(_, _) |
+        hir::PatIdent(_, _, None) |
+        hir::PatStruct(..) => {
             match dm.borrow().get(&pat.id).map(|d| d.full_def()) {
                 Some(DefVariant(..)) => true,
                 _ => false
             }
         }
-        ast::PatVec(_, _, _) => true,
+        hir::PatVec(_, _, _) => true,
         _ => false
     }
 }
 
-pub fn pat_is_variant_or_struct(dm: &DefMap, pat: &ast::Pat) -> bool {
+pub fn pat_is_variant_or_struct(dm: &DefMap, pat: &hir::Pat) -> bool {
     match pat.node {
-        ast::PatEnum(_, _) |
-        ast::PatIdent(_, _, None) |
-        ast::PatStruct(..) => {
+        hir::PatEnum(_, _) |
+        hir::PatIdent(_, _, None) |
+        hir::PatStruct(..) => {
             match dm.borrow().get(&pat.id).map(|d| d.full_def()) {
                 Some(DefVariant(..)) | Some(DefStruct(..)) => true,
                 _ => false
@@ -59,9 +60,9 @@ pub fn pat_is_variant_or_struct(dm: &DefMap, pat: &ast::Pat) -> bool {
     }
 }
 
-pub fn pat_is_const(dm: &DefMap, pat: &ast::Pat) -> bool {
+pub fn pat_is_const(dm: &DefMap, pat: &hir::Pat) -> bool {
     match pat.node {
-        ast::PatIdent(_, _, None) | ast::PatEnum(..) | ast::PatQPath(..) => {
+        hir::PatIdent(_, _, None) | hir::PatEnum(..) | hir::PatQPath(..) => {
             match dm.borrow().get(&pat.id).map(|d| d.full_def()) {
                 Some(DefConst(..)) | Some(DefAssociatedConst(..)) => true,
                 _ => false
@@ -73,9 +74,9 @@ pub fn pat_is_const(dm: &DefMap, pat: &ast::Pat) -> bool {
 
 // Same as above, except that partially-resolved defs cause `false` to be
 // returned instead of a panic.
-pub fn pat_is_resolved_const(dm: &DefMap, pat: &ast::Pat) -> bool {
+pub fn pat_is_resolved_const(dm: &DefMap, pat: &hir::Pat) -> bool {
     match pat.node {
-        ast::PatIdent(_, _, None) | ast::PatEnum(..) | ast::PatQPath(..) => {
+        hir::PatIdent(_, _, None) | hir::PatEnum(..) | hir::PatQPath(..) => {
             match dm.borrow().get(&pat.id)
                     .and_then(|d| if d.depth == 0 { Some(d.base_def) }
                                   else { None } ) {
@@ -87,9 +88,9 @@ pub fn pat_is_resolved_const(dm: &DefMap, pat: &ast::Pat) -> bool {
     }
 }
 
-pub fn pat_is_binding(dm: &DefMap, pat: &ast::Pat) -> bool {
+pub fn pat_is_binding(dm: &DefMap, pat: &hir::Pat) -> bool {
     match pat.node {
-        ast::PatIdent(..) => {
+        hir::PatIdent(..) => {
             !pat_is_variant_or_struct(dm, pat) &&
             !pat_is_const(dm, pat)
         }
@@ -97,23 +98,37 @@ pub fn pat_is_binding(dm: &DefMap, pat: &ast::Pat) -> bool {
     }
 }
 
-pub fn pat_is_binding_or_wild(dm: &DefMap, pat: &ast::Pat) -> bool {
+pub fn pat_is_binding_or_wild(dm: &DefMap, pat: &hir::Pat) -> bool {
     match pat.node {
-        ast::PatIdent(..) => pat_is_binding(dm, pat),
-        ast::PatWild(_) => true,
+        hir::PatIdent(..) => pat_is_binding(dm, pat),
+        hir::PatWild(_) => true,
         _ => false
     }
 }
 
 /// Call `it` on every "binding" in a pattern, e.g., on `a` in
 /// `match foo() { Some(a) => (), None => () }`
-pub fn pat_bindings<I>(dm: &DefMap, pat: &ast::Pat, mut it: I) where
-    I: FnMut(ast::BindingMode, ast::NodeId, Span, &ast::SpannedIdent),
+pub fn pat_bindings<I>(dm: &DefMap, pat: &hir::Pat, mut it: I) where
+    I: FnMut(hir::BindingMode, ast::NodeId, Span, &Spanned<ast::Name>),
 {
     walk_pat(pat, |p| {
         match p.node {
-          ast::PatIdent(binding_mode, ref pth, _) if pat_is_binding(dm, p) => {
-            it(binding_mode, p.id, p.span, pth);
+          hir::PatIdent(binding_mode, ref pth, _) if pat_is_binding(dm, p) => {
+            it(binding_mode, p.id, p.span, &respan(pth.span, pth.node.name));
+          }
+          _ => {}
+        }
+        true
+    });
+}
+
+pub fn pat_bindings_hygienic<I>(dm: &DefMap, pat: &hir::Pat, mut it: I) where
+    I: FnMut(hir::BindingMode, ast::NodeId, Span, &Spanned<ast::Ident>),
+{
+    walk_pat(pat, |p| {
+        match p.node {
+          hir::PatIdent(binding_mode, ref pth, _) if pat_is_binding(dm, p) => {
+            it(binding_mode, p.id, p.span, &respan(pth.span, pth.node));
           }
           _ => {}
         }
@@ -123,7 +138,7 @@ pub fn pat_bindings<I>(dm: &DefMap, pat: &ast::Pat, mut it: I) where
 
 /// Checks if the pattern contains any patterns that bind something to
 /// an ident, e.g. `foo`, or `Foo(foo)` or `foo @ Bar(..)`.
-pub fn pat_contains_bindings(dm: &DefMap, pat: &ast::Pat) -> bool {
+pub fn pat_contains_bindings(dm: &DefMap, pat: &hir::Pat) -> bool {
     let mut contains_bindings = false;
     walk_pat(pat, |p| {
         if pat_is_binding(dm, p) {
@@ -138,18 +153,18 @@ pub fn pat_contains_bindings(dm: &DefMap, pat: &ast::Pat) -> bool {
 
 /// Checks if the pattern contains any `ref` or `ref mut` bindings,
 /// and if yes wether its containing mutable ones or just immutables ones.
-pub fn pat_contains_ref_binding(dm: &DefMap, pat: &ast::Pat) -> Option<ast::Mutability> {
+pub fn pat_contains_ref_binding(dm: &DefMap, pat: &hir::Pat) -> Option<hir::Mutability> {
     let mut result = None;
     pat_bindings(dm, pat, |mode, _, _, _| {
         match mode {
-            ast::BindingMode::BindByRef(m) => {
+            hir::BindingMode::BindByRef(m) => {
                 // Pick Mutable as maximum
                 match result {
-                    None | Some(ast::MutImmutable) => result = Some(m),
+                    None | Some(hir::MutImmutable) => result = Some(m),
                     _ => (),
                 }
             }
-            ast::BindingMode::BindByValue(_) => { }
+            hir::BindingMode::BindByValue(_) => { }
         }
     });
     result
@@ -157,18 +172,18 @@ pub fn pat_contains_ref_binding(dm: &DefMap, pat: &ast::Pat) -> Option<ast::Muta
 
 /// Checks if the patterns for this arm contain any `ref` or `ref mut`
 /// bindings, and if yes wether its containing mutable ones or just immutables ones.
-pub fn arm_contains_ref_binding(dm: &DefMap, arm: &ast::Arm) -> Option<ast::Mutability> {
+pub fn arm_contains_ref_binding(dm: &DefMap, arm: &hir::Arm) -> Option<hir::Mutability> {
     arm.pats.iter()
             .filter_map(|pat| pat_contains_ref_binding(dm, pat))
             .max_by(|m| match *m {
-                ast::MutMutable => 1,
-                ast::MutImmutable => 0,
+                hir::MutMutable => 1,
+                hir::MutImmutable => 0,
             })
 }
 
 /// 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 {
+pub fn pat_contains_bindings_or_wild(dm: &DefMap, pat: &hir::Pat) -> bool {
     let mut contains_bindings = false;
     walk_pat(pat, |p| {
         if pat_is_binding_or_wild(dm, p) {
@@ -181,10 +196,10 @@ pub fn pat_contains_bindings_or_wild(dm: &DefMap, pat: &ast::Pat) -> bool {
     contains_bindings
 }
 
-pub fn simple_identifier<'a>(pat: &'a ast::Pat) -> Option<&'a ast::Ident> {
+pub fn simple_name<'a>(pat: &'a hir::Pat) -> Option<ast::Name> {
     match pat.node {
-        ast::PatIdent(ast::BindByValue(_), ref path1, None) => {
-            Some(&path1.node)
+        hir::PatIdent(hir::BindByValue(_), ref path1, None) => {
+            Some(path1.node.name)
         }
         _ => {
             None
@@ -192,25 +207,25 @@ pub fn simple_identifier<'a>(pat: &'a ast::Pat) -> Option<&'a ast::Ident> {
     }
 }
 
-pub fn def_to_path(tcx: &ty::ctxt, id: DefId) -> ast::Path {
-    tcx.with_path(id, |path| ast::Path {
+pub fn def_to_path(tcx: &ty::ctxt, id: DefId) -> hir::Path {
+    tcx.with_path(id, |path| hir::Path {
         global: false,
-        segments: path.last().map(|elem| ast::PathSegment {
+        segments: path.last().map(|elem| hir::PathSegment {
             identifier: ast::Ident::new(elem.name()),
-            parameters: ast::PathParameters::none(),
+            parameters: hir::PathParameters::none(),
         }).into_iter().collect(),
         span: DUMMY_SP,
     })
 }
 
 /// Return variants that are necessary to exist for the pattern to match.
-pub fn necessary_variants(dm: &DefMap, pat: &ast::Pat) -> Vec<ast::NodeId> {
+pub fn necessary_variants(dm: &DefMap, pat: &hir::Pat) -> Vec<ast::NodeId> {
     let mut variants = vec![];
     walk_pat(pat, |p| {
         match p.node {
-            ast::PatEnum(_, _) |
-            ast::PatIdent(_, _, None) |
-            ast::PatStruct(..) => {
+            hir::PatEnum(_, _) |
+            hir::PatIdent(_, _, None) |
+            hir::PatStruct(..) => {
                 match dm.borrow().get(&p.id) {
                     Some(&PathResolution { base_def: DefVariant(_, id, _), .. }) => {
                         variants.push(id.node);