diff options
author | Arthur Cohen <arthur.cohen@embecosm.com> | 2023-09-14 17:38:06 +0200 |
---|---|---|
committer | Arthur Cohen <arthur.cohen@embecosm.com> | 2024-08-01 16:52:25 +0200 |
commit | 53c74beb3a15a477f4fef770fe6abccbdb124fb7 (patch) | |
tree | bfea8932ad69e212388afcce3d430df3c48bb6d8 /gcc/rust/resolve | |
parent | 786bc62f957e373d40a8c11fa4e607383ab2e2f1 (diff) | |
download | gcc-53c74beb3a15a477f4fef770fe6abccbdb124fb7.zip gcc-53c74beb3a15a477f4fef770fe6abccbdb124fb7.tar.gz gcc-53c74beb3a15a477f4fef770fe6abccbdb124fb7.tar.bz2 |
gccrs: nr2.0: Start using newtype pattern for Usage and Declaration
gcc/rust/ChangeLog:
* resolve/rust-name-resolution-context.cc (NameResolutionContext::map_usage):
Use newtype pattern.
(NameResolutionContext::lookup): Likewise.
* resolve/rust-name-resolution-context.h (class Usage): New class.
(class Definition): Likewise.
* resolve/rust-late-name-resolver-2.0.cc (Late::visit): Create instances
of Usage and Definition.
Diffstat (limited to 'gcc/rust/resolve')
-rw-r--r-- | gcc/rust/resolve/rust-late-name-resolver-2.0.cc | 4 | ||||
-rw-r--r-- | gcc/rust/resolve/rust-name-resolution-context.cc | 6 | ||||
-rw-r--r-- | gcc/rust/resolve/rust-name-resolution-context.h | 27 |
3 files changed, 30 insertions, 7 deletions
diff --git a/gcc/rust/resolve/rust-late-name-resolver-2.0.cc b/gcc/rust/resolve/rust-late-name-resolver-2.0.cc index e5a4f23..5003407 100644 --- a/gcc/rust/resolve/rust-late-name-resolver-2.0.cc +++ b/gcc/rust/resolve/rust-late-name-resolver-2.0.cc @@ -142,7 +142,7 @@ Late::visit (AST::IdentifierExpr &expr) return; } - ctx.map_usage (expr.get_node_id (), *resolved); + ctx.map_usage (Usage (expr.get_node_id ()), Definition (*resolved)); // in the old resolver, resolutions are kept in the resolver, not the mappings // :/ how do we deal with that? @@ -173,7 +173,7 @@ Late::visit (AST::TypePath &type) auto resolved = ctx.types.get (type.get_segments ().back ()->as_string ()); - ctx.map_usage (type.get_node_id (), *resolved); + ctx.map_usage (Usage (type.get_node_id ()), Definition (*resolved)); } } // namespace Resolver2_0 diff --git a/gcc/rust/resolve/rust-name-resolution-context.cc b/gcc/rust/resolve/rust-name-resolution-context.cc index 9fd8d52..0340d28 100644 --- a/gcc/rust/resolve/rust-name-resolution-context.cc +++ b/gcc/rust/resolve/rust-name-resolution-context.cc @@ -46,7 +46,7 @@ NameResolutionContext::insert (Identifier name, NodeId id, Namespace ns) } void -NameResolutionContext::map_usage (NodeId usage, NodeId definition) +NameResolutionContext::map_usage (Usage usage, Definition definition) { auto inserted = resolved_nodes.emplace (usage, definition).second; @@ -57,12 +57,12 @@ NameResolutionContext::map_usage (NodeId usage, NodeId definition) tl::optional<NodeId> NameResolutionContext::lookup (NodeId usage) { - auto it = resolved_nodes.find (usage); + auto it = resolved_nodes.find (Usage (usage)); if (it == resolved_nodes.end ()) return tl::nullopt; - return it->second; + return it->second.id; } void diff --git a/gcc/rust/resolve/rust-name-resolution-context.h b/gcc/rust/resolve/rust-name-resolution-context.h index e896ca0..8702900 100644 --- a/gcc/rust/resolve/rust-name-resolution-context.h +++ b/gcc/rust/resolve/rust-name-resolution-context.h @@ -133,6 +133,28 @@ change? correct */ +// FIXME: Documentation +class Usage +{ +public: + explicit Usage (NodeId id) : id (id) {} + + // TODO: move to name-resolution-ctx.cc + // storing it as a key in a map + bool operator< (const Usage other) const { return other.id < id; } + + NodeId id; +}; + +// FIXME: Documentation +class Definition +{ +public: + explicit Definition (NodeId id) : id (id) {} + + NodeId id; +}; + // Now our resolver, which keeps track of all the `ForeverStack`s we could want class NameResolutionContext { @@ -182,12 +204,13 @@ public: // TODO: Rename // TODO: Use newtype pattern for Usage and Definition - void map_usage (NodeId usage, NodeId definition); + void map_usage (Usage usage, Definition definition); + tl::optional<NodeId> lookup (NodeId usage); private: /* Map of "usage" nodes which have been resolved to a "definition" node */ - std::map<NodeId, NodeId> resolved_nodes; + std::map<Usage, Definition> resolved_nodes; }; } // namespace Resolver2_0 |