From: Arthur Cohen Date: Fri, 21 Jul 2023 16:22:43 +0000 (+0200) Subject: gccrs: rib2.0: Add shadowing X-Git-Tag: basepoints/gcc-15~2256 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5a0e099e892d575af167d6a0a7e9ae5f26f439d8;p=thirdparty%2Fgcc.git gccrs: rib2.0: Add shadowing gcc/rust/ChangeLog: * resolve/rust-rib.h: Add shadowing parameter. Make kind field public. * resolve/rust-rib.cc (Rib::insert): Likewise. --- diff --git a/gcc/rust/resolve/rust-rib.cc b/gcc/rust/resolve/rust-rib.cc index 2cc9f3e1862e..21fbe2ca530e 100644 --- a/gcc/rust/resolve/rust-rib.cc +++ b/gcc/rust/resolve/rust-rib.cc @@ -36,13 +36,15 @@ Rib::Rib (Kind kind, std::unordered_map values) {} tl::expected -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 diff --git a/gcc/rust/resolve/rust-rib.h b/gcc/rust/resolve/rust-rib.h index ea69cc7bd6e8..37bd90f1f75e 100644 --- a/gcc/rust/resolve/rust-rib.h +++ b/gcc/rust/resolve/rust-rib.h @@ -93,7 +93,7 @@ public: ForwardTypeParamBan, /* Const generic, as in the following example: fn foo() {} */ 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 insert (std::string name, NodeId id); + tl::expected insert (std::string name, NodeId id, + bool can_shadow = false); /** * Access an inserted NodeId. @@ -125,7 +127,6 @@ public: const std::unordered_map &get_values () const; private: - Kind kind; std::unordered_map values; };