From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1643) id 7C54B3819E8A; Wed, 8 Jun 2022 12:36:21 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7C54B3819E8A Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Thomas Schwinge To: gcc-cvs@gcc.gnu.org Subject: [gcc/devel/rust/master] resolver: Refactor Rib class in a source file X-Act-Checkin: gcc X-Git-Author: Arthur Cohen X-Git-Refname: refs/heads/devel/rust/master X-Git-Oldrev: a936265bb0e6e042733c501db942615f2a64f705 X-Git-Newrev: 3001bb64cc25e9174cf69ad5e12df17ad45ef6b8 Message-Id: <20220608123621.7C54B3819E8A@sourceware.org> Date: Wed, 8 Jun 2022 12:36:21 +0000 (GMT) X-BeenThere: gcc-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 08 Jun 2022 12:36:21 -0000 https://gcc.gnu.org/g:3001bb64cc25e9174cf69ad5e12df17ad45ef6b8 commit 3001bb64cc25e9174cf69ad5e12df17ad45ef6b8 Author: Arthur Cohen Date: Fri Apr 22 16:28:26 2022 +0200 resolver: Refactor Rib class in a source file Diff: --- gcc/rust/Make-lang.in | 1 + gcc/rust/backend/rust-compile-base.cc | 52 +++++++-------- gcc/rust/resolve/rust-name-resolver.cc | 108 +++++++++++++++++++++++++++++++ gcc/rust/resolve/rust-name-resolver.h | 115 +++------------------------------ 4 files changed, 144 insertions(+), 132 deletions(-) diff --git a/gcc/rust/Make-lang.in b/gcc/rust/Make-lang.in index d25f403fbfb..436d9c3d6c4 100644 --- a/gcc/rust/Make-lang.in +++ b/gcc/rust/Make-lang.in @@ -85,6 +85,7 @@ GRS_OBJS = \ rust/rust-ast-lower.o \ rust/rust-ast-lower-base.o \ rust/rust-ast-lower-pattern.o \ + rust/rust-name-resolver.o \ rust/rust-ast-resolve.o \ rust/rust-ast-resolve-pattern.o \ rust/rust-ast-resolve-expr.o \ diff --git a/gcc/rust/backend/rust-compile-base.cc b/gcc/rust/backend/rust-compile-base.cc index 84afa1e17be..1ee7cd59beb 100644 --- a/gcc/rust/backend/rust-compile-base.cc +++ b/gcc/rust/backend/rust-compile-base.cc @@ -306,33 +306,33 @@ HIRCompileBase::compile_locals_for_block (Context *ctx, Resolver::Rib &rib, tree fndecl) { std::vector locals; - rib.iterate_decls ([&] (NodeId n, Location) mutable -> bool { - Resolver::Definition d; - bool ok = ctx->get_resolver ()->lookup_definition (n, &d); - rust_assert (ok); - - HIR::Stmt *decl = nullptr; - ok = ctx->get_mappings ()->resolve_nodeid_to_stmt (d.parent, &decl); - rust_assert (ok); - - // if its a function we extract this out side of this fn context - // and it is not a local to this function - bool is_item = ctx->get_mappings ()->lookup_hir_item ( - decl->get_mappings ().get_crate_num (), - decl->get_mappings ().get_hirid ()) - != nullptr; - if (is_item) - { - HIR::Item *item = static_cast (decl); - CompileItem::compile (item, ctx); - return true; - } - - Bvariable *compiled = CompileVarDecl::compile (fndecl, decl, ctx); - locals.push_back (compiled); + for (auto it : rib.get_declarations ()) + { + auto node_id = it.first; + + Resolver::Definition d; + bool ok = ctx->get_resolver ()->lookup_definition (node_id, &d); + rust_assert (ok); + + HIR::Stmt *decl = nullptr; + ok = ctx->get_mappings ()->resolve_nodeid_to_stmt (d.parent, &decl); + rust_assert (ok); + + // if its a function we extract this out side of this fn context + // and it is not a local to this function + bool is_item = ctx->get_mappings ()->lookup_hir_item ( + decl->get_mappings ().get_crate_num (), + decl->get_mappings ().get_hirid ()) + != nullptr; + if (is_item) + { + HIR::Item *item = static_cast (decl); + CompileItem::compile (item, ctx); + } - return true; - }); + Bvariable *compiled = CompileVarDecl::compile (fndecl, decl, ctx); + locals.push_back (compiled); + }; return locals; } diff --git a/gcc/rust/resolve/rust-name-resolver.cc b/gcc/rust/resolve/rust-name-resolver.cc new file mode 100644 index 00000000000..a80d64f1787 --- /dev/null +++ b/gcc/rust/resolve/rust-name-resolver.cc @@ -0,0 +1,108 @@ +// Copyright (C) 2020-2022 Free Software Foundation, Inc. + +// This file is part of GCC. + +// GCC is free software; you can redistribute it and/or modify it under +// the terms of the GNU General Public License as published by the Free +// Software Foundation; either version 3, or (at your option) any later +// version. + +// GCC is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +// for more details. + +// You should have received a copy of the GNU General Public License +// along with GCC; see the file COPYING3. If not see +// . + +#include "rust-name-resolver.h" + +namespace Rust { +namespace Resolver { + +Rib::Rib (CrateNum crateNum, NodeId node_id) + : crate_num (crateNum), node_id (node_id), + mappings (Analysis::Mappings::get ()) +{} + +void +Rib::insert_name ( + const CanonicalPath &path, NodeId id, Location locus, bool shadow, + std::function dup_cb) +{ + auto it = path_mappings.find (path); + bool path_already_exists = it != path_mappings.end (); + if (path_already_exists && !shadow) + { + const auto &decl = decls_within_rib.find (it->second); + if (decl != decls_within_rib.end ()) + dup_cb (path, it->second, decl->second); + else + dup_cb (path, it->second, locus); + + return; + } + + path_mappings[path] = id; + reverse_path_mappings.insert (std::pair (id, path)); + decls_within_rib.insert (std::pair (id, locus)); + references[id] = {}; +} + +bool +Rib::lookup_name (const CanonicalPath &ident, NodeId *id) +{ + auto it = path_mappings.find (ident); + if (it == path_mappings.end ()) + return false; + + *id = it->second; + return true; +} + +void +Rib::clear_name (const CanonicalPath &ident, NodeId id) +{ + auto ii = path_mappings.find (ident); + if (ii != path_mappings.end ()) + path_mappings.erase (ii); + + auto ij = reverse_path_mappings.find (id); + if (ij != reverse_path_mappings.end ()) + reverse_path_mappings.erase (ij); + + auto ik = decls_within_rib.find (id); + if (ik != decls_within_rib.end ()) + decls_within_rib.erase (ik); +} + +void +Rib::append_reference_for_def (NodeId def, NodeId ref) +{ + references[def].insert (ref); +} + +bool +Rib::have_references_for_node (NodeId def) const +{ + auto it = references.find (def); + if (it == references.end ()) + return false; + + return !it->second.empty (); +} + +bool +Rib::decl_was_declared_here (NodeId def) const +{ + for (auto &it : decls_within_rib) + { + if (it.first == def) + return true; + } + return false; +} + +} // namespace Resolver +} // namespace Rust diff --git a/gcc/rust/resolve/rust-name-resolver.h b/gcc/rust/resolve/rust-name-resolver.h index 208448036d9..19ca5a9fd15 100644 --- a/gcc/rust/resolve/rust-name-resolver.h +++ b/gcc/rust/resolve/rust-name-resolver.h @@ -32,120 +32,23 @@ class Rib public: // Rust uses local_def_ids assigned by def_collector on the AST // lets use NodeId instead - Rib (CrateNum crateNum, NodeId node_id) - : crate_num (crateNum), node_id (node_id), - mappings (Analysis::Mappings::get ()) - {} - - ~Rib () {} + Rib (CrateNum crateNum, NodeId node_id); // this takes the relative paths of items within a compilation unit for lookup void insert_name ( const CanonicalPath &path, NodeId id, Location locus, bool shadow, - std::function dup_cb) - { - auto it = path_mappings.find (path); - bool path_already_exists = it != path_mappings.end (); - if (path_already_exists && !shadow) - { - const auto &decl = decls_within_rib.find (it->second); - if (decl != decls_within_rib.end ()) - dup_cb (path, it->second, decl->second); - else - dup_cb (path, it->second, locus); - - return; - } - - path_mappings[path] = id; - reverse_path_mappings.insert (std::pair (id, path)); - decls_within_rib.insert (std::pair (id, locus)); - references[id] = {}; - } - - bool lookup_name (const CanonicalPath &ident, NodeId *id) - { - auto it = path_mappings.find (ident); - if (it == path_mappings.end ()) - return false; - - *id = it->second; - return true; - } + std::function dup_cb); - bool lookup_canonical_path (const NodeId &id, CanonicalPath *ident) - { - auto it = reverse_path_mappings.find (id); - if (it == reverse_path_mappings.end ()) - return false; - - *ident = it->second; - return true; - } - - void clear_name (const CanonicalPath &ident, NodeId id) - { - auto ii = path_mappings.find (ident); - if (ii != path_mappings.end ()) - path_mappings.erase (ii); - - auto ij = reverse_path_mappings.find (id); - if (ij != reverse_path_mappings.end ()) - reverse_path_mappings.erase (ij); - - auto ik = decls_within_rib.find (id); - if (ik != decls_within_rib.end ()) - decls_within_rib.erase (ik); - } + bool lookup_canonical_path (const NodeId &id, CanonicalPath *ident); + bool lookup_name (const CanonicalPath &ident, NodeId *id); + void clear_name (const CanonicalPath &ident, NodeId id); + void append_reference_for_def (NodeId def, NodeId ref); + bool have_references_for_node (NodeId def) const; + bool decl_was_declared_here (NodeId def) const; CrateNum get_crate_num () const { return crate_num; } NodeId get_node_id () const { return node_id; } - - void iterate_decls (std::function cb) - { - for (auto it : decls_within_rib) - { - if (!cb (it.first, it.second)) - return; - } - } - - void iterate_references_for_def (NodeId def, std::function cb) - { - auto it = references.find (def); - if (it == references.end ()) - return; - - for (auto ref : it->second) - { - if (!cb (ref)) - return; - } - } - - void append_reference_for_def (NodeId def, NodeId ref) - { - references[def].insert (ref); - } - - bool have_references_for_node (NodeId def) const - { - auto it = references.find (def); - if (it == references.end ()) - return false; - - return !it->second.empty (); - } - - bool decl_was_declared_here (NodeId def) const - { - for (auto &it : decls_within_rib) - { - if (it.first == def) - return true; - } - return false; - } + std::map &get_declarations () { return decls_within_rib; } private: CrateNum crate_num;