]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: Strong type ScopeId
authorKushal Pal <kushalpal109@gmail.com>
Wed, 7 Aug 2024 10:16:24 +0000 (10:16 +0000)
committerArthur Cohen <arthur.cohen@embecosm.com>
Wed, 19 Mar 2025 14:32:09 +0000 (15:32 +0100)
gcc/rust/ChangeLog:

* checks/errors/borrowck/rust-bir-builder-expr-stmt.cc
(ExprStmtBuilder::setup_loop): Use value of ScopeId.
(ExprStmtBuilder::visit): Use continue scope id instead of
continue basic block id.
* checks/errors/borrowck/rust-bir-builder-internal.h: Use value
of ScopeId.
* checks/errors/borrowck/rust-bir-dump.cc (Dump::go): Use
ROOT_VALUE instead of hardcoded 0.
(Dump::visit_scope): Use value of ScopeId.
* checks/errors/borrowck/rust-bir-place.h (struct ScopeId):
ScopeId is now a struct.
(std::numeric_limits::max): Set invalid ScopeId.

Signed-off-by: Kushal Pal <kushalpal109@gmail.com>
gcc/rust/checks/errors/borrowck/rust-bir-builder-expr-stmt.cc
gcc/rust/checks/errors/borrowck/rust-bir-builder-internal.h
gcc/rust/checks/errors/borrowck/rust-bir-dump.cc
gcc/rust/checks/errors/borrowck/rust-bir-place.h

index 8d4d90aab80ce075c0eb9f7f71a4928efa1a92af..1713bf6fcf8d47e611c98128abeec06312374ae8 100644 (file)
@@ -45,7 +45,8 @@ ExprStmtBuilder::setup_loop (HIR::BaseLoopExpr &expr)
 
   BasicBlockId break_bb = new_bb ();
   // We are still outside the loop block;
-  ScopeId continue_scope = ctx.place_db.get_current_scope_id () + 1;
+  ScopeId continue_scope
+    = ctx.place_db.get_current_scope_id ().next_scope_id ();
   ctx.loop_and_label_stack.emplace_back (true, label, label_var, break_bb,
                                         continue_bb, continue_scope);
 
@@ -414,7 +415,7 @@ ExprStmtBuilder::visit (HIR::ContinueExpr &cont)
   LoopAndLabelCtx info = cont.has_label () ? get_label_ctx (cont.get_label ())
                                           : get_unnamed_loop_ctx ();
   start_new_consecutive_bb ();
-  unwind_until (info.continue_bb);
+  unwind_until (info.continue_scope);
   push_goto (info.continue_bb);
   // No code allowed after continue. Handled in BlockExpr.
 }
index b27f7717cd0c7c0bf982af6b8c1c3421f24029e5..e0d8bb3d2fcf24f6e20efd33b579c605b9644e71 100644 (file)
@@ -156,7 +156,7 @@ protected:
 
     auto place_id = ctx.place_db.add_variable (nodeid, ty);
 
-    if (ctx.place_db.get_current_scope_id () != 0)
+    if (ctx.place_db.get_current_scope_id () != INVALID_SCOPE)
       push_storage_live (place_id);
 
     if (user_type_annotation)
@@ -170,7 +170,7 @@ protected:
   void pop_scope ()
   {
     auto &scope = ctx.place_db.get_current_scope ();
-    if (ctx.place_db.get_current_scope_id () != 0)
+    if (ctx.place_db.get_current_scope_id () != INVALID_SCOPE)
       {
        std::for_each (scope.locals.rbegin (), scope.locals.rend (),
                       [&] (PlaceId place) { push_storage_dead (place); });
index 85ba3ee2ce4d35b8727de4ec121c43c3c07298b4..0c0e55567b5e8ac5c342a3474a40e4b209a4f712 100644 (file)
@@ -133,7 +133,7 @@ Dump::go (bool enable_simplify_cfg)
   stream << " {\n";
 
   // Print locals declaration.
-  visit_scope (0);
+  visit_scope (ROOT_SCOPE);
 
   // Print BBs.
   for (statement_bb = 0; statement_bb < func.basic_blocks.size ();
@@ -366,8 +366,8 @@ Dump::visit_scope (ScopeId id, size_t depth)
   if (scope.locals.empty () && scope.children.empty ())
     return;
 
-  if (id > 1)
-    indent (depth) << "scope " << id - 1 << " {\n";
+  if (id.value > 1)
+    indent (depth) << "scope " << id.value - 1 << " {\n";
 
   for (auto &local : scope.locals)
     {
@@ -385,9 +385,9 @@ Dump::visit_scope (ScopeId id, size_t depth)
       stream << "]\n";
     }
   for (auto &child : scope.children)
-    visit_scope (child, (id >= 1) ? depth + 1 : depth);
+    visit_scope (child, (id.value >= 1) ? depth + 1 : depth);
 
-  if (id > 1)
+  if (id.value > 1)
     indent (depth) << "}\n";
 }
 
index c78492a2394fe893220e0543b93b484777e8a31e..f7018d3af7f026fbadc6ac3cdd02bbb59a3afe4d 100644 (file)
@@ -165,13 +165,25 @@ public:
   }
 };
 
-using ScopeId = uint32_t;
+struct ScopeId
+{
+  uint32_t value;
+  ScopeId next_scope_id () const { return {value + 1}; }
+  // some overloads for comparision
+  bool operator== (const ScopeId &rhs) const { return value == rhs.value; }
+  bool operator!= (const ScopeId &rhs) const { return !(operator== (rhs)); }
+  bool operator< (const ScopeId &rhs) const { return value < rhs.value; }
+  bool operator> (const ScopeId &rhs) const { return value > rhs.value; }
+  bool operator<= (const ScopeId &rhs) const { return !(operator> (rhs)); }
+  bool operator>= (const ScopeId &rhs) const { return !(operator< (rhs)); }
+};
 
-static constexpr ScopeId INVALID_SCOPE = std::numeric_limits<ScopeId>::max ();
+static constexpr ScopeId INVALID_SCOPE
+  = {std::numeric_limits<uint32_t>::max ()};
 /** Arguments and return value are in the root scope. */
-static constexpr ScopeId ROOT_SCOPE = 0;
+static constexpr ScopeId ROOT_SCOPE = {0};
 /** Top-level local variables are in the top-level scope. */
-static constexpr ScopeId TOP_LEVEL_SCOPE = 1;
+static constexpr ScopeId TOP_LEVEL_SCOPE = {1};
 
 struct Scope
 {
@@ -195,7 +207,7 @@ private:
   std::vector<Place> places;
   std::unordered_map<TyTy::BaseType *, PlaceId> constants_lookup;
   std::vector<Scope> scopes;
-  ScopeId current_scope = 0;
+  ScopeId current_scope = ROOT_SCOPE;
 
   std::vector<Loan> loans;
 
@@ -228,9 +240,12 @@ public:
 
   const std::vector<Scope> &get_scopes () const { return scopes; }
 
-  const Scope &get_current_scope () const { return scopes[current_scope]; }
+  const Scope &get_current_scope () const
+  {
+    return scopes[current_scope.value];
+  }
 
-  const Scope &get_scope (ScopeId id) const { return scopes[id]; }
+  const Scope &get_scope (ScopeId id) const { return scopes[id.value]; }
 
   FreeRegion get_next_free_region ()
   {
@@ -244,17 +259,17 @@ public:
 
   ScopeId push_new_scope ()
   {
-    ScopeId new_scope = scopes.size ();
+    ScopeId new_scope = {scopes.size ()};
     scopes.emplace_back ();
-    scopes[new_scope].parent = current_scope;
-    scopes[current_scope].children.push_back (new_scope);
+    scopes[new_scope.value].parent = current_scope;
+    scopes[current_scope.value].children.push_back (new_scope);
     current_scope = new_scope;
     return new_scope;
   }
 
   ScopeId pop_scope ()
   {
-    current_scope = scopes[current_scope].parent;
+    current_scope = scopes[current_scope.value].parent;
     return current_scope;
   }
 
@@ -270,7 +285,7 @@ public:
 
     if (new_place_ref.kind == Place::VARIABLE
        || new_place_ref.kind == Place::TEMPORARY)
-      scopes[current_scope].locals.push_back (new_place);
+      scopes[current_scope.value].locals.push_back (new_place);
 
     auto variances = Resolver::TypeCheckContext::get ()
                       ->get_variance_analysis_ctx ()
@@ -460,9 +475,9 @@ private:
   WARN_UNUSED_RESULT bool is_in_scope (PlaceId place) const
   {
     for (ScopeId scope = current_scope; scope != INVALID_SCOPE;
-        scope = scopes[scope].parent)
+        scope = scopes[scope.value].parent)
       {
-       auto &scope_ref = scopes[scope];
+       auto &scope_ref = scopes[scope.value];
        if (std::find (scope_ref.locals.begin (), scope_ref.locals.end (),
                       place)
            != scope_ref.locals.end ())