(root)/
gcc-13.2.0/
gcc/
rust/
resolve/
rust-ast-resolve-pattern.h
       1  // Copyright (C) 2020-2023 Free Software Foundation, Inc.
       2  
       3  // This file is part of GCC.
       4  
       5  // GCC is free software; you can redistribute it and/or modify it under
       6  // the terms of the GNU General Public License as published by the Free
       7  // Software Foundation; either version 3, or (at your option) any later
       8  // version.
       9  
      10  // GCC is distributed in the hope that it will be useful, but WITHOUT ANY
      11  // WARRANTY; without even the implied warranty of MERCHANTABILITY or
      12  // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
      13  // for more details.
      14  
      15  // You should have received a copy of the GNU General Public License
      16  // along with GCC; see the file COPYING3.  If not see
      17  // <http://www.gnu.org/licenses/>.
      18  
      19  #ifndef RUST_AST_RESOLVE_PATTERN_H
      20  #define RUST_AST_RESOLVE_PATTERN_H
      21  
      22  #include "rust-ast-resolve-base.h"
      23  #include "rust-ast-full.h"
      24  
      25  namespace Rust {
      26  namespace Resolver {
      27  
      28  class ResolvePattern : public ResolverBase
      29  {
      30    using Rust::Resolver::ResolverBase::visit;
      31  
      32  public:
      33    static void go (AST::Pattern *pattern)
      34    {
      35      ResolvePattern resolver;
      36      pattern->accept_vis (resolver);
      37    }
      38  
      39    void visit (AST::IdentifierPattern &pattern) override
      40    {
      41      if (resolver->get_name_scope ().lookup (
      42  	  CanonicalPath::new_seg (pattern.get_node_id (), pattern.get_ident ()),
      43  	  &resolved_node))
      44        {
      45  	resolver->insert_resolved_name (pattern.get_node_id (), resolved_node);
      46        }
      47    }
      48  
      49  private:
      50    ResolvePattern () : ResolverBase () {}
      51  };
      52  
      53  class PatternDeclaration : public ResolverBase
      54  {
      55    using Rust::Resolver::ResolverBase::visit;
      56  
      57  public:
      58    static void go (AST::Pattern *pattern, Rib::ItemType type)
      59    {
      60      PatternDeclaration resolver (type);
      61      pattern->accept_vis (resolver);
      62    };
      63  
      64    void visit (AST::IdentifierPattern &pattern) override
      65    {
      66      // if we have a duplicate id this then allows for shadowing correctly
      67      // as new refs to this decl will match back here so it is ok to overwrite
      68      resolver->get_name_scope ().insert (
      69        CanonicalPath::new_seg (pattern.get_node_id (), pattern.get_ident ()),
      70        pattern.get_node_id (), pattern.get_locus (), type);
      71    }
      72  
      73    void visit (AST::GroupedPattern &pattern) override
      74    {
      75      pattern.get_pattern_in_parens ()->accept_vis (*this);
      76    }
      77  
      78    // cases in a match expression
      79    void visit (AST::PathInExpression &pattern) override;
      80  
      81    void visit (AST::StructPattern &pattern) override;
      82  
      83    void visit (AST::TupleStructPattern &pattern) override;
      84  
      85    void visit (AST::TuplePattern &pattern) override;
      86  
      87    void visit (AST::RangePattern &pattern) override;
      88  
      89  private:
      90    PatternDeclaration (Rib::ItemType type) : ResolverBase (), type (type) {}
      91  
      92    Rib::ItemType type;
      93  };
      94  
      95  } // namespace Resolver
      96  } // namespace Rust
      97  
      98  #endif // RUST_AST_RESOLVE_PATTERN_H