diff options
Diffstat (limited to 'gcc/rust/resolve/rust-name-resolution-context.h')
-rw-r--r-- | gcc/rust/resolve/rust-name-resolution-context.h | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/gcc/rust/resolve/rust-name-resolution-context.h b/gcc/rust/resolve/rust-name-resolution-context.h index 183ce7f..ea81bde 100644 --- a/gcc/rust/resolve/rust-name-resolution-context.h +++ b/gcc/rust/resolve/rust-name-resolution-context.h @@ -172,6 +172,9 @@ public: tl::expected<NodeId, DuplicateNameError> insert (Identifier name, NodeId id, Namespace ns); + tl::expected<NodeId, DuplicateNameError> insert_variant (Identifier name, + NodeId id); + tl::expected<NodeId, DuplicateNameError> insert_shadowable (Identifier name, NodeId id, Namespace ns); @@ -216,6 +219,46 @@ public: tl::optional<NodeId> lookup (NodeId usage) const; + template <typename S> + tl::optional<Rib::Definition> resolve_path (const std::vector<S> &segments, + Namespace ns) + { + std::function<void (const S &, NodeId)> insert_segment_resolution + = [this] (const S &seg, NodeId id) { + auto seg_id = unwrap_segment_node_id (seg); + if (resolved_nodes.find (Usage (seg_id)) == resolved_nodes.end ()) + map_usage (Usage (seg_id), Definition (id)); + }; + switch (ns) + { + case Namespace::Values: + return values.resolve_path (segments, insert_segment_resolution); + case Namespace::Types: + return types.resolve_path (segments, insert_segment_resolution); + case Namespace::Macros: + return macros.resolve_path (segments, insert_segment_resolution); + case Namespace::Labels: + return labels.resolve_path (segments, insert_segment_resolution); + default: + rust_unreachable (); + } + } + + template <typename S, typename... Args> + tl::optional<Rib::Definition> resolve_path (const std::vector<S> &segments, + Args... ns_args) + { + std::initializer_list<Namespace> namespaces = {ns_args...}; + + for (auto ns : namespaces) + { + if (auto ret = resolve_path (segments, ns)) + return ret; + } + + return tl::nullopt; + } + private: /* Map of "usage" nodes which have been resolved to a "definition" node */ std::map<Usage, Definition> resolved_nodes; |