]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: rib2.0: Add shadowing
authorArthur Cohen <arthur.cohen@embecosm.com>
Fri, 21 Jul 2023 16:22:43 +0000 (18:22 +0200)
committerArthur Cohen <arthur.cohen@embecosm.com>
Tue, 16 Jan 2024 18:00:26 +0000 (19:00 +0100)
gcc/rust/ChangeLog:

* resolve/rust-rib.h: Add shadowing parameter. Make kind field public.
* resolve/rust-rib.cc (Rib::insert): Likewise.

gcc/rust/resolve/rust-rib.cc
gcc/rust/resolve/rust-rib.h

index 2cc9f3e1862effec5f13af977fcbbacc113d267d..21fbe2ca530e16b7afad84fde3d3749979a65a0d 100644 (file)
@@ -36,13 +36,15 @@ Rib::Rib (Kind kind, std::unordered_map<std::string, NodeId> values)
 {}
 
 tl::expected<NodeId, DuplicateNameError>
-Rib::insert (std::string name, NodeId id)
+Rib::insert (std::string name, NodeId id, bool can_shadow)
 {
   auto res = values.insert ({name, id});
   auto inserted_id = res.first->second;
+  auto existed = !res.second;
 
-  // if we couldn't insert, the element already exists - exit with an error
-  if (!res.second)
+  // if we couldn't insert, the element already exists - exit with an error,
+  // unless shadowing is allowed
+  if (existed && !can_shadow)
     return tl::make_unexpected (DuplicateNameError (name, inserted_id));
 
   // return the NodeId
index ea69cc7bd6e80625a10f7fe1e8a9d3fc80834728..37bd90f1f75eb43e48cbbf815da9f618ee067ee0 100644 (file)
@@ -93,7 +93,7 @@ public:
     ForwardTypeParamBan,
     /* Const generic, as in the following example: fn foo<T, const X: T>() {} */
     ConstParamType,
-  };
+  } kind;
 
   Rib (Kind kind);
   Rib (Kind kind, std::string identifier, NodeId id);
@@ -107,12 +107,14 @@ public:
    *
    * @param name The name associated with the AST node
    * @param id Its NodeId
+   * @param can_shadow If the newly inserted value can shadow an existing one
    *
    * @return `DuplicateNameError` if the node is already present in the rib. The
    *         `DuplicateNameError` class contains the NodeId of the existing
    * node. Returns the new NodeId on success.
    */
-  tl::expected<NodeId, DuplicateNameError> insert (std::string name, NodeId id);
+  tl::expected<NodeId, DuplicateNameError> insert (std::string name, NodeId id,
+                                                  bool can_shadow = false);
 
   /**
    * Access an inserted NodeId.
@@ -125,7 +127,6 @@ public:
   const std::unordered_map<std::string, NodeId> &get_values () const;
 
 private:
-  Kind kind;
   std::unordered_map<std::string, NodeId> values;
 };