(root)/
gcc-13.2.0/
gcc/
rust/
util/
rust-mapping-common.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_MAPPING_COMMON
      20  #define RUST_MAPPING_COMMON
      21  
      22  #include "rust-system.h"
      23  
      24  namespace Rust {
      25  
      26  // refers to a Crate
      27  typedef uint32_t CrateNum;
      28  // refers to any node in the AST in current Crate
      29  typedef uint32_t NodeId;
      30  // refers to any node in the HIR for the current crate
      31  typedef uint32_t HirId;
      32  // refers to any top-level decl in HIR
      33  typedef uint32_t LocalDefId;
      34  
      35  struct DefId
      36  {
      37    CrateNum crateNum;
      38    LocalDefId localDefId;
      39  
      40    bool operator== (const DefId &other) const
      41    {
      42      return this->crateNum == other.crateNum
      43  	   && this->localDefId == other.localDefId;
      44    }
      45  
      46    bool operator!= (const DefId &other) const { return !(*this == other); }
      47  
      48    bool operator< (const DefId &other) const
      49    {
      50      return ((uint64_t) this->crateNum << 32 | this->localDefId)
      51  	   < ((uint64_t) other.crateNum << 32 | other.localDefId);
      52    }
      53  
      54    std::string as_string () const
      55    {
      56      std::string buf;
      57      buf += std::to_string (crateNum);
      58      buf += " "; // or anything else
      59      buf += std::to_string (localDefId);
      60      return buf;
      61    }
      62  };
      63  
      64  #define UNKNOWN_CREATENUM ((uint32_t) (0))
      65  #define UNKNOWN_NODEID ((uint32_t) (0))
      66  #define UNKNOWN_HIRID ((uint32_t) (0))
      67  #define UNKNOWN_LOCAL_DEFID ((uint32_t) (0))
      68  #define UNKNOWN_DEFID (DefId{0, 0})
      69  
      70  } // namespace Rust
      71  
      72  namespace std {
      73  template <> struct hash<Rust::DefId>
      74  {
      75    size_t operator() (const Rust::DefId &id) const noexcept
      76    {
      77      // TODO: Check if we can improve performance by having a better hash
      78      // algorithm for `DefId`s
      79      return hash<uint32_t> () (hash<uint32_t> () (id.crateNum)
      80  			      + hash<uint32_t> () (id.localDefId));
      81    }
      82  };
      83  } // namespace std
      84  
      85  #endif // RUST_MAPPING_COMMON