diff options
author | Philip Herron <philip.herron@embecosm.com> | 2021-01-08 17:18:00 +0000 |
---|---|---|
committer | Philip Herron <herron.philip@googlemail.com> | 2021-01-10 15:59:22 +0000 |
commit | 9a3ad294e0af5ed45b53c340382a7806fb150bdf (patch) | |
tree | 81458d62a2191b38be775026f2f8583d3ca7c2ee /gcc/rust/resolve/rust-name-resolver.h | |
parent | 0f42a240e53e932de0ae4799d54fe0bd15d06047 (diff) | |
download | gcc-9a3ad294e0af5ed45b53c340382a7806fb150bdf.zip gcc-9a3ad294e0af5ed45b53c340382a7806fb150bdf.tar.gz gcc-9a3ad294e0af5ed45b53c340382a7806fb150bdf.tar.bz2 |
Add in a check for unused decls within ribs.
This lead to cleanup of the name resolver as the usage of mappings means
that in a given rib if there are no references to a decl NodeId that
means it was not used.
To get on par with the offical rust compiler it should be allowed to have
a reference where the name was assigned but not used but this might be a seperate pass.
Diffstat (limited to 'gcc/rust/resolve/rust-name-resolver.h')
-rw-r--r-- | gcc/rust/resolve/rust-name-resolver.h | 48 |
1 files changed, 41 insertions, 7 deletions
diff --git a/gcc/rust/resolve/rust-name-resolver.h b/gcc/rust/resolve/rust-name-resolver.h index 38206f6..515ebfd 100644 --- a/gcc/rust/resolve/rust-name-resolver.h +++ b/gcc/rust/resolve/rust-name-resolver.h @@ -37,10 +37,10 @@ public: ~Rib () {} - void insert_name (std::string ident, NodeId id) + void insert_name (std::string ident, NodeId id, Location locus) { mappings[ident] = id; - decls_within_rib.insert (id); + decls_within_rib.insert (std::pair<NodeId, Location> (id, locus)); references[id] = {}; } @@ -57,11 +57,11 @@ public: CrateNum get_crate_num () const { return crate_num; } NodeId get_node_id () const { return node_id; } - void iterate_decls (std::function<bool (NodeId)> cb) + void iterate_decls (std::function<bool (NodeId, Location)> cb) { for (auto it : decls_within_rib) { - if (!cb (it)) + if (!cb (it.first, it.second)) return; } } @@ -84,11 +84,30 @@ public: 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; + } + private: CrateNum crate_num; NodeId node_id; std::map<std::string, NodeId> mappings; - std::set<NodeId> decls_within_rib; + std::set<std::pair<NodeId, Location> > decls_within_rib; std::map<NodeId, std::set<NodeId> > references; }; @@ -98,9 +117,9 @@ public: Scope (CrateNum crate_num) : crate_num (crate_num) {} ~Scope () {} - void insert (std::string ident, NodeId id) + void insert (std::string ident, NodeId id, Location locus) { - peek ()->insert_name (ident, id); + peek ()->insert_name (ident, id, locus); } bool lookup (std::string ident, NodeId *id) @@ -138,6 +157,21 @@ public: CrateNum get_crate_num () const { return crate_num; } + void append_reference_for_def (NodeId refId, NodeId defId) + { + bool ok = false; + iterate ([&] (Rib *r) mutable -> bool { + if (r->decl_was_declared_here (defId)) + { + ok = true; + r->append_reference_for_def (defId, refId); + return false; + } + return true; + }); + rust_assert (ok); + } + private: CrateNum crate_num; std::vector<Rib *> stack; |