From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1643) id D73D33858435; Sat, 6 Aug 2022 12:08:54 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D73D33858435 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] mappings: Keep track of extern blocks X-Act-Checkin: gcc X-Git-Author: Arthur Cohen X-Git-Refname: refs/heads/devel/rust/master X-Git-Oldrev: e88a55ab6919346e0afe79bfd13f84ecf9634889 X-Git-Newrev: b38dee3d3808d43d5c25dba30259462eed93b2c0 Message-Id: <20220806120854.D73D33858435@sourceware.org> Date: Sat, 6 Aug 2022 12:08:54 +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: Sat, 06 Aug 2022 12:08:55 -0000 https://gcc.gnu.org/g:b38dee3d3808d43d5c25dba30259462eed93b2c0 commit b38dee3d3808d43d5c25dba30259462eed93b2c0 Author: Arthur Cohen Date: Thu Jul 28 11:08:27 2022 +0200 mappings: Keep track of extern blocks When inserting external items, it's interesting to also be able to lookup the extern block they were defined in, similarly to impl items. This is useful to know the ABI or other specifiers of the block the item has been declared in. Diff: --- gcc/rust/backend/rust-compile-resolve-path.cc | 3 ++- gcc/rust/checks/errors/rust-unsafe-checker.cc | 4 +++- gcc/rust/hir/rust-ast-lower-base.cc | 14 +++++++----- gcc/rust/hir/rust-ast-lower-extern.h | 6 +++-- gcc/rust/util/rust-hir-map.cc | 32 ++++++++++++++++++++++----- gcc/rust/util/rust-hir-map.h | 10 ++++++--- 6 files changed, 51 insertions(+), 18 deletions(-) diff --git a/gcc/rust/backend/rust-compile-resolve-path.cc b/gcc/rust/backend/rust-compile-resolve-path.cc index 95d8841b5ab..b5bfa3c21bf 100644 --- a/gcc/rust/backend/rust-compile-resolve-path.cc +++ b/gcc/rust/backend/rust-compile-resolve-path.cc @@ -159,8 +159,9 @@ HIRCompileBase::query_compile (HirId ref, TyTy::BaseType *lookup, Location expr_locus, bool is_qualified_path) { HIR::Item *resolved_item = ctx->get_mappings ()->lookup_hir_item (ref); + HirId parent_block; HIR::ExternalItem *resolved_extern_item - = ctx->get_mappings ()->lookup_hir_extern_item (ref); + = ctx->get_mappings ()->lookup_hir_extern_item (ref, &parent_block); bool is_hir_item = resolved_item != nullptr; bool is_hir_extern_item = resolved_extern_item != nullptr; if (is_hir_item) diff --git a/gcc/rust/checks/errors/rust-unsafe-checker.cc b/gcc/rust/checks/errors/rust-unsafe-checker.cc index 0d1e0e928e1..9642baadfed 100644 --- a/gcc/rust/checks/errors/rust-unsafe-checker.cc +++ b/gcc/rust/checks/errors/rust-unsafe-checker.cc @@ -69,7 +69,9 @@ UnsafeChecker::check_use_of_static (HirId node_id, Location locus) return; auto maybe_static_mut = mappings.lookup_hir_item (node_id); - auto maybe_extern_static = mappings.lookup_hir_extern_item (node_id); + HirId extern_block; + auto maybe_extern_static + = mappings.lookup_hir_extern_item (node_id, &extern_block); if (maybe_static_mut) check_static_mut (maybe_static_mut, locus); diff --git a/gcc/rust/hir/rust-ast-lower-base.cc b/gcc/rust/hir/rust-ast-lower-base.cc index cae4428dea6..a67461791d7 100644 --- a/gcc/rust/hir/rust-ast-lower-base.cc +++ b/gcc/rust/hir/rust-ast-lower-base.cc @@ -1038,6 +1038,11 @@ ASTLoweringBase::lower_extern_block (AST::ExternBlock &extern_block) { HIR::Visibility vis = translate_visibility (extern_block.get_visibility ()); + auto crate_num = mappings->get_current_crate (); + Analysis::NodeMapping mapping (crate_num, extern_block.get_node_id (), + mappings->get_next_hir_id (crate_num), + mappings->get_next_localdef_id (crate_num)); + std::vector> extern_items; for (auto &item : extern_block.get_extern_items ()) { @@ -1045,7 +1050,7 @@ ASTLoweringBase::lower_extern_block (AST::ExternBlock &extern_block) continue; HIR::ExternalItem *lowered - = ASTLoweringExternItem::translate (item.get ()); + = ASTLoweringExternItem::translate (item.get (), mapping.get_hirid ()); extern_items.push_back (std::unique_ptr (lowered)); } @@ -1058,17 +1063,14 @@ ASTLoweringBase::lower_extern_block (AST::ExternBlock &extern_block) rust_error_at (extern_block.get_locus (), "unknown ABI option"); } - auto crate_num = mappings->get_current_crate (); - Analysis::NodeMapping mapping (crate_num, extern_block.get_node_id (), - mappings->get_next_hir_id (crate_num), - mappings->get_next_localdef_id (crate_num)); - HIR::ExternBlock *hir_extern_block = new HIR::ExternBlock (mapping, abi, std::move (extern_items), std::move (vis), extern_block.get_inner_attrs (), extern_block.get_outer_attrs (), extern_block.get_locus ()); + mappings->insert_hir_extern_block (hir_extern_block); + return hir_extern_block; } diff --git a/gcc/rust/hir/rust-ast-lower-extern.h b/gcc/rust/hir/rust-ast-lower-extern.h index 28d160bb3e9..eeb59c9c5d6 100644 --- a/gcc/rust/hir/rust-ast-lower-extern.h +++ b/gcc/rust/hir/rust-ast-lower-extern.h @@ -31,13 +31,15 @@ class ASTLoweringExternItem : public ASTLoweringBase using Rust::HIR::ASTLoweringBase::visit; public: - static HIR::ExternalItem *translate (AST::ExternalItem *item) + static HIR::ExternalItem *translate (AST::ExternalItem *item, + HirId parent_hirid) { ASTLoweringExternItem resolver; item->accept_vis (resolver); rust_assert (resolver.translated != nullptr); - resolver.mappings->insert_hir_extern_item (resolver.translated); + resolver.mappings->insert_hir_extern_item (resolver.translated, + parent_hirid); resolver.mappings->insert_location ( resolver.translated->get_mappings ().get_hirid (), resolver.translated->get_locus ()); diff --git a/gcc/rust/util/rust-hir-map.cc b/gcc/rust/util/rust-hir-map.cc index c7bf1824a73..6a6deebf944 100644 --- a/gcc/rust/util/rust-hir-map.cc +++ b/gcc/rust/util/rust-hir-map.cc @@ -371,23 +371,45 @@ Mappings::lookup_hir_trait_item (HirId id) } void -Mappings::insert_hir_extern_item (HIR::ExternalItem *item) +Mappings::insert_hir_extern_block (HIR::ExternBlock *block) +{ + auto id = block->get_mappings ().get_hirid (); + rust_assert (lookup_hir_extern_block (id) == nullptr); + + hirExternBlockMappings[id] = block; + insert_node_to_hir (block->get_mappings ().get_nodeid (), id); +} + +HIR::ExternBlock * +Mappings::lookup_hir_extern_block (HirId id) +{ + auto it = hirExternBlockMappings.find (id); + if (it == hirExternBlockMappings.end ()) + return nullptr; + + return it->second; +} + +void +Mappings::insert_hir_extern_item (HIR::ExternalItem *item, HirId parent_block) { auto id = item->get_mappings ().get_hirid (); - rust_assert (lookup_hir_extern_item (id) == nullptr); + rust_assert (lookup_hir_extern_item (id, nullptr) == nullptr); - hirExternItemMappings[id] = item; + hirExternItemMappings[id] = {item, parent_block}; insert_node_to_hir (item->get_mappings ().get_nodeid (), id); } HIR::ExternalItem * -Mappings::lookup_hir_extern_item (HirId id) +Mappings::lookup_hir_extern_item (HirId id, HirId *parent_block) { auto it = hirExternItemMappings.find (id); if (it == hirExternItemMappings.end ()) return nullptr; - return it->second; + *parent_block = it->second.second; + + return it->second.first; } void diff --git a/gcc/rust/util/rust-hir-map.h b/gcc/rust/util/rust-hir-map.h index c8cebefcd04..98fcfe6a6a7 100644 --- a/gcc/rust/util/rust-hir-map.h +++ b/gcc/rust/util/rust-hir-map.h @@ -115,8 +115,11 @@ public: void insert_hir_trait_item (HIR::TraitItem *item); HIR::TraitItem *lookup_hir_trait_item (HirId id); - void insert_hir_extern_item (HIR::ExternalItem *item); - HIR::ExternalItem *lookup_hir_extern_item (HirId id); + void insert_hir_extern_block (HIR::ExternBlock *block); + HIR::ExternBlock *lookup_hir_extern_block (HirId id); + + void insert_hir_extern_item (HIR::ExternalItem *item, HirId parent_block); + HIR::ExternalItem *lookup_hir_extern_item (HirId id, HirId *parent_block); void insert_hir_impl_block (HIR::ImplBlock *item); HIR::ImplBlock *lookup_hir_impl_block (HirId id); @@ -312,7 +315,8 @@ private: std::map hirImplItemsToImplMappings; std::map hirImplBlockMappings; std::map hirTraitItemMappings; - std::map hirExternItemMappings; + std::map hirExternBlockMappings; + std::map> hirExternItemMappings; std::map hirPathSegMappings; std::map hirGenericParamMappings; std::map hirTraitItemsToTraitMappings;