diff options
author | Arthur Cohen <arthur.cohen@embecosm.com> | 2022-04-22 16:28:26 +0200 |
---|---|---|
committer | Arthur Cohen <arthur.cohen@embecosm.com> | 2022-04-22 16:28:26 +0200 |
commit | 3001bb64cc25e9174cf69ad5e12df17ad45ef6b8 (patch) | |
tree | 07b79b4659b34f34dae6bd07e2fedbf794306444 /gcc/rust/resolve | |
parent | a936265bb0e6e042733c501db942615f2a64f705 (diff) | |
download | gcc-3001bb64cc25e9174cf69ad5e12df17ad45ef6b8.zip gcc-3001bb64cc25e9174cf69ad5e12df17ad45ef6b8.tar.gz gcc-3001bb64cc25e9174cf69ad5e12df17ad45ef6b8.tar.bz2 |
resolver: Refactor Rib class in a source file
Diffstat (limited to 'gcc/rust/resolve')
-rw-r--r-- | gcc/rust/resolve/rust-name-resolver.cc | 108 | ||||
-rw-r--r-- | gcc/rust/resolve/rust-name-resolver.h | 115 |
2 files changed, 117 insertions, 106 deletions
diff --git a/gcc/rust/resolve/rust-name-resolver.cc b/gcc/rust/resolve/rust-name-resolver.cc new file mode 100644 index 0000000..a80d64f --- /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 +// <http://www.gnu.org/licenses/>. + +#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<void (const CanonicalPath &, NodeId, Location)> 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<NodeId, CanonicalPath> (id, path)); + decls_within_rib.insert (std::pair<NodeId, Location> (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 2084480..19ca5a9 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<void (const CanonicalPath &, NodeId, Location)> 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<NodeId, CanonicalPath> (id, path)); - decls_within_rib.insert (std::pair<NodeId, Location> (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<void (const CanonicalPath &, NodeId, Location)> 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<bool (NodeId, Location)> cb) - { - for (auto it : decls_within_rib) - { - if (!cb (it.first, it.second)) - return; - } - } - - void iterate_references_for_def (NodeId def, std::function<bool (NodeId)> 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<NodeId, Location> &get_declarations () { return decls_within_rib; } private: CrateNum crate_num; |