From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1643) id 685203810AF8; Wed, 8 Jun 2022 12:52:11 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 685203810AF8 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] Add new mappings for items within a module X-Act-Checkin: gcc X-Git-Author: Philip Herron X-Git-Refname: refs/heads/devel/rust/master X-Git-Oldrev: 0866a4fbc6e7f70cd3708467419c60af8c6104f2 X-Git-Newrev: 8d1f5f99484f2b79439b86dca7123eb0260776d7 Message-Id: <20220608125211.685203810AF8@sourceware.org> Date: Wed, 8 Jun 2022 12:52:11 +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:52:11 -0000 https://gcc.gnu.org/g:8d1f5f99484f2b79439b86dca7123eb0260776d7 commit 8d1f5f99484f2b79439b86dca7123eb0260776d7 Author: Philip Herron Date: Thu Jun 2 15:46:57 2022 +0100 Add new mappings for items within a module This patch adds two new interfaces to the mappings class. - Lookup the child items within a module - Lookup the parent module id from the child item id Each of these API's are going to be required so that we can resolve two new types of path segment: super::foo::bar Where we need to be able to lookup the super module node id from the current scope node id. To then lookup the children within that scope. The other path is simpler such as: crate::foo::bar. Where we lookup the items within the current Crate NodeId and follow on as normal. Addresses #1227 Diff: --- gcc/rust/resolve/rust-ast-resolve-toplevel.h | 13 +++++++++ gcc/rust/util/rust-hir-map.cc | 41 ++++++++++++++++++++++++++++ gcc/rust/util/rust-hir-map.h | 10 +++++++ 3 files changed, 64 insertions(+) diff --git a/gcc/rust/resolve/rust-ast-resolve-toplevel.h b/gcc/rust/resolve/rust-ast-resolve-toplevel.h index 7cfaa72f5a5..8d4e12b0616 100644 --- a/gcc/rust/resolve/rust-ast-resolve-toplevel.h +++ b/gcc/rust/resolve/rust-ast-resolve-toplevel.h @@ -41,6 +41,8 @@ public: ResolveTopLevel resolver (prefix, canonical_prefix, current_module); item->accept_vis (resolver); + resolver.mappings->insert_child_item_to_parent_module_mapping ( + item->get_node_id (), current_module); }; void visit (AST::Module &module) override @@ -62,6 +64,7 @@ public: Definition{module.get_node_id (), module.get_node_id ()}); + mappings->insert_module_child_item (current_module, mod); mappings->insert_module_child (current_module, module.get_node_id ()); for (auto &item : module.get_items ()) @@ -86,6 +89,7 @@ public: rust_error_at (r, "redefined multiple times"); }); + mappings->insert_module_child_item (current_module, talias); mappings->insert_canonical_path (mappings->get_current_crate (), alias.get_node_id (), cpath); } @@ -105,6 +109,7 @@ public: rust_error_at (r, "redefined multiple times"); }); + mappings->insert_module_child_item (current_module, decl); mappings->insert_canonical_path (mappings->get_current_crate (), struct_decl.get_node_id (), cpath); } @@ -127,6 +132,7 @@ public: for (auto &variant : enum_decl.get_variants ()) ResolveTopLevel::go (variant.get (), path, cpath, current_module); + mappings->insert_module_child_item (current_module, decl); mappings->insert_canonical_path (mappings->get_current_crate (), enum_decl.get_node_id (), cpath); } @@ -146,6 +152,7 @@ public: rust_error_at (r, "redefined multiple times"); }); + mappings->insert_module_child_item (current_module, decl); mappings->insert_canonical_path (mappings->get_current_crate (), item.get_node_id (), cpath); } @@ -222,6 +229,7 @@ public: rust_error_at (r, "redefined multiple times"); }); + mappings->insert_module_child_item (current_module, decl); mappings->insert_canonical_path (mappings->get_current_crate (), struct_decl.get_node_id (), cpath); } @@ -241,6 +249,7 @@ public: rust_error_at (r, "redefined multiple times"); }); + mappings->insert_module_child_item (current_module, decl); mappings->insert_canonical_path (mappings->get_current_crate (), union_decl.get_node_id (), cpath); } @@ -264,6 +273,7 @@ public: var.get_node_id ()}); resolver->mark_decl_mutability (var.get_node_id (), var.is_mutable ()); + mappings->insert_module_child_item (current_module, decl); mappings->insert_canonical_path (mappings->get_current_crate (), var.get_node_id (), cpath); } @@ -285,6 +295,7 @@ public: Definition{constant.get_node_id (), constant.get_node_id ()}); + mappings->insert_module_child_item (current_module, decl); mappings->insert_canonical_path (mappings->get_current_crate (), constant.get_node_id (), cpath); } @@ -314,6 +325,7 @@ public: function.get_node_id ()); } + mappings->insert_module_child_item (current_module, decl); mappings->insert_canonical_path (mappings->get_current_crate (), function.get_node_id (), cpath); } @@ -391,6 +403,7 @@ public: for (auto &item : trait.get_trait_items ()) ResolveTopLevelTraitItems::go (item.get (), path, cpath); + mappings->insert_module_child_item (current_module, decl); mappings->insert_canonical_path (mappings->get_current_crate (), trait.get_node_id (), cpath); } diff --git a/gcc/rust/util/rust-hir-map.cc b/gcc/rust/util/rust-hir-map.cc index 8672c8acb12..f9932a4705e 100644 --- a/gcc/rust/util/rust-hir-map.cc +++ b/gcc/rust/util/rust-hir-map.cc @@ -884,5 +884,46 @@ Mappings::lookup_module_children (NodeId module) return Optional &>::some (it->second); } +void +Mappings::insert_module_child_item (NodeId module, + Resolver::CanonicalPath child) +{ + rust_assert (!child.is_empty ()); + rust_assert (child.get_node_id () != UNKNOWN_NODEID); + + auto it = module_child_items.find (module); + if (it == module_child_items.end ()) + module_child_items.insert ({module, {child}}); + else + it->second.emplace_back (child); +} + +Optional &> +Mappings::lookup_module_chidren_items (NodeId module) +{ + auto it = module_child_items.find (module); + if (it == module_child_items.end ()) + return Optional &>::none (); + + return Optional &>::some (it->second); +} + +void +Mappings::insert_child_item_to_parent_module_mapping (NodeId child_item, + NodeId parent_module) +{ + child_to_parent_module_map.insert ({child_item, parent_module}); +} + +Optional +Mappings::lookup_parent_module (NodeId child_item) +{ + auto it = child_to_parent_module_map.find (child_item); + if (it == child_to_parent_module_map.end ()) + return Optional::none (); + + return Optional::some (it->second); +} + } // namespace Analysis } // namespace Rust diff --git a/gcc/rust/util/rust-hir-map.h b/gcc/rust/util/rust-hir-map.h index 65f86616d56..a48d4beace7 100644 --- a/gcc/rust/util/rust-hir-map.h +++ b/gcc/rust/util/rust-hir-map.h @@ -326,6 +326,14 @@ public: void insert_module_child (NodeId module, NodeId child); Optional &> lookup_module_children (NodeId module); + void insert_module_child_item (NodeId module, Resolver::CanonicalPath item); + Optional &> + lookup_module_chidren_items (NodeId module); + + void insert_child_item_to_parent_module_mapping (NodeId child_item, + NodeId parent_module); + Optional lookup_parent_module (NodeId child_item); + private: Mappings (); @@ -396,6 +404,8 @@ private: // Maps each module's node id to a list of its children std::map> module_child_map; + std::map> module_child_items; + std::map child_to_parent_module_map; }; } // namespace Analysis