(root)/
gcc-13.2.0/
gcc/
rust/
backend/
rust-compile-var-decl.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_COMPILE_VAR_DECL
      20  #define RUST_COMPILE_VAR_DECL
      21  
      22  #include "rust-compile-base.h"
      23  #include "rust-hir-visitor.h"
      24  
      25  namespace Rust {
      26  namespace Compile {
      27  
      28  class CompileVarDecl : public HIRCompileBase, public HIR::HIRPatternVisitor
      29  {
      30    using HIR::HIRPatternVisitor::visit;
      31  
      32  public:
      33    static void compile (tree fndecl, tree translated_type, HIR::Pattern *pattern,
      34  		       std::vector<Bvariable *> &locals, Context *ctx)
      35    {
      36      CompileVarDecl compiler (ctx, fndecl, translated_type, locals);
      37      pattern->accept_vis (compiler);
      38    }
      39  
      40    void visit (HIR::IdentifierPattern &pattern) override
      41    {
      42      if (!pattern.is_mut ())
      43        translated_type = ctx->get_backend ()->immutable_type (translated_type);
      44  
      45      Bvariable *var
      46        = ctx->get_backend ()->local_variable (fndecl, pattern.get_identifier (),
      47  					     translated_type, NULL /*decl_var*/,
      48  					     pattern.get_locus ());
      49  
      50      HirId stmt_id = pattern.get_pattern_mappings ().get_hirid ();
      51      ctx->insert_var_decl (stmt_id, var);
      52  
      53      locals.push_back (var);
      54    }
      55  
      56    // Empty visit for unused Pattern HIR nodes.
      57    void visit (HIR::LiteralPattern &) override {}
      58    void visit (HIR::PathInExpression &) override {}
      59    void visit (HIR::QualifiedPathInExpression &) override {}
      60    void visit (HIR::RangePattern &) override {}
      61    void visit (HIR::ReferencePattern &) override {}
      62    void visit (HIR::SlicePattern &) override {}
      63    void visit (HIR::StructPattern &) override {}
      64    void visit (HIR::TuplePattern &) override {}
      65    void visit (HIR::TupleStructPattern &) override {}
      66    void visit (HIR::WildcardPattern &) override {}
      67  
      68  private:
      69    CompileVarDecl (Context *ctx, tree fndecl, tree translated_type,
      70  		  std::vector<Bvariable *> &locals)
      71      : HIRCompileBase (ctx), fndecl (fndecl), translated_type (translated_type),
      72        locals (locals)
      73    {}
      74  
      75    tree fndecl;
      76    tree translated_type;
      77  
      78    std::vector<Bvariable *> &locals;
      79  };
      80  
      81  } // namespace Compile
      82  } // namespace Rust
      83  
      84  #endif // RUST_COMPILE_VAR_DECL