public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Thomas Schwinge <tschwinge@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc/devel/rust/master] Changed DefId type from uint64_t to be struct
Date: Wed,  8 Jun 2022 11:49:48 +0000 (GMT)	[thread overview]
Message-ID: <20220608114948.080C3398641E@sourceware.org> (raw)

https://gcc.gnu.org/g:a7c31383def5c499864d61c5d00a1575d1cbff81

commit a7c31383def5c499864d61c5d00a1575d1cbff81
Author: Nirmal Patel <npate012@gmail.com>
Date:   Tue Nov 2 21:01:13 2021 -0400

    Changed DefId type from uint64_t to be struct
    
    DefId was uint64_t previously but it has been changed to be a struct.
    In order to reduce code breakage, ==, !=, and < operators have been implemented
    for DefId. Since DefId is now a proper struct, bit manipulation code has been
    removed with member accesses.
    
    Fixes #439
    
    Signed-off-by: Nirmal Patel <npate012@gmail.com>

Diff:
---
 gcc/rust/typecheck/rust-tyty.h      |  8 ++++----
 gcc/rust/util/rust-hir-map.cc       |  6 +++---
 gcc/rust/util/rust-mapping-common.h | 25 ++++++++++++++++++++-----
 3 files changed, 27 insertions(+), 12 deletions(-)

diff --git a/gcc/rust/typecheck/rust-tyty.h b/gcc/rust/typecheck/rust-tyty.h
index 5ffd95c29da..14c131a033d 100644
--- a/gcc/rust/typecheck/rust-tyty.h
+++ b/gcc/rust/typecheck/rust-tyty.h
@@ -1297,7 +1297,7 @@ public:
       params (std::move (params)), type (type), flags (flags),
       identifier (identifier), id (id), abi (abi)
   {
-    LocalDefId local_def_id = id & DEF_ID_LOCAL_DEF_MASK;
+    LocalDefId local_def_id = id.localDefId;
     rust_assert (local_def_id != UNKNOWN_LOCAL_DEFID);
   }
 
@@ -1312,7 +1312,7 @@ public:
       params (params), type (type), flags (flags), identifier (identifier),
       id (id), abi (abi)
   {
-    LocalDefId local_def_id = id & DEF_ID_LOCAL_DEF_MASK;
+    LocalDefId local_def_id = id.localDefId;
     rust_assert (local_def_id != UNKNOWN_LOCAL_DEFID);
   }
 
@@ -1471,7 +1471,7 @@ public:
       parameter_types (std::move (parameter_types)),
       result_type (std::move (result_type)), id (id)
   {
-    LocalDefId local_def_id = id & DEF_ID_LOCAL_DEF_MASK;
+    LocalDefId local_def_id = id.localDefId;
     rust_assert (local_def_id != UNKNOWN_LOCAL_DEFID);
   }
 
@@ -1485,7 +1485,7 @@ public:
       parameter_types (std::move (parameter_types)),
       result_type (std::move (result_type)), id (id)
   {
-    LocalDefId local_def_id = id & DEF_ID_LOCAL_DEF_MASK;
+    LocalDefId local_def_id = id.localDefId;
     rust_assert (local_def_id != UNKNOWN_LOCAL_DEFID);
   }
 
diff --git a/gcc/rust/util/rust-hir-map.cc b/gcc/rust/util/rust-hir-map.cc
index e965c234986..9e5b5095178 100644
--- a/gcc/rust/util/rust-hir-map.cc
+++ b/gcc/rust/util/rust-hir-map.cc
@@ -63,7 +63,7 @@ NodeMapping::get_defid () const
 DefId
 NodeMapping::get_defid (CrateNum crate_num, LocalDefId local_defid)
 {
-  return ((uint64_t) crate_num << 32) | local_defid;
+  return DefId{crate_num, local_defid};
 }
 
 std::string
@@ -209,8 +209,8 @@ Mappings::insert_hir_crate (HIR::Crate *crate)
 void
 Mappings::insert_defid_mapping (DefId id, HIR::Item *item)
 {
-  CrateNum crate_num = (id & DEF_ID_CRATE_MASK) >> 32;
-  LocalDefId local_def_id = id & DEF_ID_LOCAL_DEF_MASK;
+  CrateNum crate_num = id.crateNum;
+  LocalDefId local_def_id = id.localDefId;
 
   rust_assert (lookup_defid (id) == nullptr);
   rust_assert (lookup_local_defid (crate_num, local_def_id) == nullptr);
diff --git a/gcc/rust/util/rust-mapping-common.h b/gcc/rust/util/rust-mapping-common.h
index 1309a8d6e90..06657796181 100644
--- a/gcc/rust/util/rust-mapping-common.h
+++ b/gcc/rust/util/rust-mapping-common.h
@@ -31,17 +31,32 @@ typedef uint32_t NodeId;
 typedef uint32_t HirId;
 // refers to any top-level decl in HIR
 typedef uint32_t LocalDefId;
-// refers to <Crate><DefId>
-typedef uint64_t DefId;
 
-#define DEF_ID_CRATE_MASK 0xFFFFFFFF00000000
-#define DEF_ID_LOCAL_DEF_MASK 0x00000000FFFFFFFF
+struct DefId
+{
+  CrateNum crateNum;
+  LocalDefId localDefId;
+
+  bool operator== (const DefId &other) const
+  {
+    return this->crateNum == other.crateNum
+	   && this->localDefId == other.localDefId;
+  }
+
+  bool operator!= (const DefId &other) const { return !(*this == other); }
+
+  bool operator< (const DefId &other) const
+  {
+    return ((uint64_t) this->crateNum << 32 | this->localDefId)
+	   < ((uint64_t) other.crateNum << 32 | other.localDefId);
+  }
+};
 
 #define UNKNOWN_CREATENUM ((uint32_t) (0))
 #define UNKNOWN_NODEID ((uint32_t) (0))
 #define UNKNOWN_HIRID ((uint32_t) (0))
 #define UNKNOWN_LOCAL_DEFID ((uint32_t) (0))
-#define UNKNOWN_DEFID ((uint64_t) (0))
+#define UNKNOWN_DEFID (DefId{0, 0})
 
 } // namespace Rust


                 reply	other threads:[~2022-06-08 11:49 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220608114948.080C3398641E@sourceware.org \
    --to=tschwinge@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).