public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc/devel/rust/master] mappings: Keep track of extern blocks
@ 2022-08-06 12:08 Thomas Schwinge
  0 siblings, 0 replies; only message in thread
From: Thomas Schwinge @ 2022-08-06 12:08 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:b38dee3d3808d43d5c25dba30259462eed93b2c0

commit b38dee3d3808d43d5c25dba30259462eed93b2c0
Author: Arthur Cohen <arthur.cohen@embecosm.com>
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<std::unique_ptr<HIR::ExternalItem>> 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<HIR::ExternalItem> (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<HirId, HIR::ImplBlock *> hirImplItemsToImplMappings;
   std::map<HirId, HIR::ImplBlock *> hirImplBlockMappings;
   std::map<HirId, HIR::TraitItem *> hirTraitItemMappings;
-  std::map<HirId, HIR::ExternalItem *> hirExternItemMappings;
+  std::map<HirId, HIR::ExternBlock *> hirExternBlockMappings;
+  std::map<HirId, std::pair<HIR::ExternalItem *, HirId>> hirExternItemMappings;
   std::map<HirId, HIR::PathExprSegment *> hirPathSegMappings;
   std::map<HirId, HIR::GenericParam *> hirGenericParamMappings;
   std::map<HirId, HIR::Trait *> hirTraitItemsToTraitMappings;


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-08-06 12:08 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-08-06 12:08 [gcc/devel/rust/master] mappings: Keep track of extern blocks Thomas Schwinge

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).