From: Pierre-Emmanuel Patry <pierre-emmanuel.pa...@embecosm.com>

Wrap the return type with an optional and make the return type a pair
with the parent hid.

gcc/rust/ChangeLog:

        * backend/rust-compile-resolve-path.cc (HIRCompileBase::query_compile):
        Adapt code around new return type.
        * checks/errors/rust-const-checker.cc 
(ConstChecker::check_function_call):
        Likewise.
        * checks/errors/rust-unsafe-checker.cc 
(UnsafeChecker::check_use_of_static):
        Likewise.
        (UnsafeChecker::check_function_call): Likewise.
        * typecheck/rust-type-util.cc (query_type): Likewise.
        * util/rust-hir-map.cc (Mappings::insert_hir_extern_item): Likewise.
        (Mappings::lookup_hir_extern_item): Change return type.
        * util/rust-hir-map.h: Update the function's prototype.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.pa...@embecosm.com>
---
 gcc/rust/backend/rust-compile-resolve-path.cc |  8 +++-----
 gcc/rust/checks/errors/rust-const-checker.cc  |  9 ++++-----
 gcc/rust/checks/errors/rust-unsafe-checker.cc | 18 ++++++------------
 gcc/rust/typecheck/rust-type-util.cc          | 11 ++++-------
 gcc/rust/util/rust-hir-map.cc                 | 12 +++++-------
 gcc/rust/util/rust-hir-map.h                  |  5 ++++-
 6 files changed, 26 insertions(+), 37 deletions(-)

diff --git a/gcc/rust/backend/rust-compile-resolve-path.cc 
b/gcc/rust/backend/rust-compile-resolve-path.cc
index bf294e4c879..c27074f89a4 100644
--- a/gcc/rust/backend/rust-compile-resolve-path.cc
+++ b/gcc/rust/backend/rust-compile-resolve-path.cc
@@ -201,10 +201,6 @@ HIRCompileBase::query_compile (HirId ref, TyTy::BaseType 
*lookup,
                               const Analysis::NodeMapping &mappings,
                               location_t expr_locus, bool is_qualified_path)
 {
-  HirId parent_block;
-  HIR::ExternalItem *resolved_extern_item
-    = ctx->get_mappings ().lookup_hir_extern_item (ref, &parent_block);
-  bool is_hir_extern_item = resolved_extern_item != nullptr;
   bool is_fn = lookup->get_kind () == TyTy::TypeKind::FNDEF;
   if (auto resolved_item = ctx->get_mappings ().lookup_hir_item (ref))
     {
@@ -215,8 +211,10 @@ HIRCompileBase::query_compile (HirId ref, TyTy::BaseType 
*lookup,
        return CompileItem::compile (*resolved_item, ctx, lookup, true,
                                     expr_locus);
     }
-  else if (is_hir_extern_item)
+  else if (auto hir_extern_item
+          = ctx->get_mappings ().lookup_hir_extern_item (ref))
     {
+      HIR::ExternalItem *resolved_extern_item = hir_extern_item->first;
       if (!lookup->has_substitutions_defined ())
        return CompileExternItem::compile (resolved_extern_item, ctx, nullptr,
                                           true, expr_locus);
diff --git a/gcc/rust/checks/errors/rust-const-checker.cc 
b/gcc/rust/checks/errors/rust-const-checker.cc
index 1bb2c37d7eb..8c12012b33f 100644
--- a/gcc/rust/checks/errors/rust-const-checker.cc
+++ b/gcc/rust/checks/errors/rust-const-checker.cc
@@ -315,11 +315,9 @@ ConstChecker::check_function_call (HirId fn_id, location_t 
locus)
   // There are const extern functions (intrinsics)
   // TODO: Should we check the ABI is only "rust intrinsics"? Is that handled
   // elsewhere?
-  HirId parent_block;
-  auto maybe_extern_item
-    = mappings.lookup_hir_extern_item (fn_id, &parent_block);
+  auto maybe_extern_item = mappings.lookup_hir_extern_item (fn_id);
   if (maybe_extern_item
-      && maybe_extern_item->get_extern_kind ()
+      && maybe_extern_item->first->get_extern_kind ()
           != ExternalItem::ExternKind::Function)
     return;
 
@@ -334,7 +332,8 @@ ConstChecker::check_function_call (HirId fn_id, location_t 
locus)
   if (maybe_extern_item)
     {
       {
-       auto fn = static_cast<ExternalFunctionItem *> (maybe_extern_item);
+       auto fn
+         = static_cast<ExternalFunctionItem *> (maybe_extern_item->first);
        if (!is_const_extern_fn (*fn))
          is_error = true;
       }
diff --git a/gcc/rust/checks/errors/rust-unsafe-checker.cc 
b/gcc/rust/checks/errors/rust-unsafe-checker.cc
index fc83283a795..19a0489297f 100644
--- a/gcc/rust/checks/errors/rust-unsafe-checker.cc
+++ b/gcc/rust/checks/errors/rust-unsafe-checker.cc
@@ -70,15 +70,12 @@ UnsafeChecker::check_use_of_static (HirId node_id, 
location_t locus)
   if (unsafe_context.is_in_context ())
     return;
 
-  HirId extern_block;
-  auto maybe_extern_static
-    = mappings.lookup_hir_extern_item (node_id, &extern_block);
-
   if (auto maybe_static_mut = mappings.lookup_hir_item (node_id))
     check_static_mut (*maybe_static_mut, locus);
 
-  if (maybe_extern_static)
-    check_extern_static (static_cast<ExternalItem *> (maybe_extern_static),
+  if (auto maybe_extern_static = mappings.lookup_hir_extern_item (node_id))
+    check_extern_static (static_cast<ExternalItem *> (
+                          maybe_extern_static->first),
                         locus);
 }
 
@@ -166,18 +163,15 @@ UnsafeChecker::check_function_call (HirId node_id, 
location_t locus)
   if (unsafe_context.is_in_context ())
     return;
 
-  HirId parent_extern_block;
   auto maybe_fn = mappings.lookup_hir_item (node_id);
-  auto maybe_extern
-    = mappings.lookup_hir_extern_item (node_id, &parent_extern_block);
 
   if (maybe_fn
       && maybe_fn.value ()->get_item_kind () == Item::ItemKind::Function)
     check_unsafe_call (static_cast<Function *> (*maybe_fn), locus, "function");
 
-  if (maybe_extern)
-    check_extern_call (static_cast<ExternalItem *> (maybe_extern),
-                      *mappings.lookup_hir_extern_block (parent_extern_block),
+  if (auto maybe_extern = mappings.lookup_hir_extern_item (node_id))
+    check_extern_call (static_cast<ExternalItem *> (maybe_extern->first),
+                      *mappings.lookup_hir_extern_block (maybe_extern->second),
                       locus);
 }
 
diff --git a/gcc/rust/typecheck/rust-type-util.cc 
b/gcc/rust/typecheck/rust-type-util.cc
index 6847d87ad96..05ac58bc18c 100644
--- a/gcc/rust/typecheck/rust-type-util.cc
+++ b/gcc/rust/typecheck/rust-type-util.cc
@@ -98,16 +98,13 @@ query_type (HirId reference, TyTy::BaseType **result)
     }
 
   // is it an extern item?
-  HirId parent_extern_block_id = UNKNOWN_HIRID;
-  HIR::ExternalItem *extern_item
-    = mappings.lookup_hir_extern_item (reference, &parent_extern_block_id);
-  if (extern_item != nullptr)
+  if (auto extern_item = mappings.lookup_hir_extern_item (reference))
     {
-      auto block = mappings.lookup_hir_extern_block (parent_extern_block_id);
+      auto block = mappings.lookup_hir_extern_block (extern_item->second);
       rust_assert (block.has_value ());
 
-      *result
-       = TypeCheckTopLevelExternItem::Resolve (extern_item, *block.value ());
+      *result = TypeCheckTopLevelExternItem::Resolve (extern_item->first,
+                                                     *block.value ());
       context->query_completed (reference);
       return true;
     }
diff --git a/gcc/rust/util/rust-hir-map.cc b/gcc/rust/util/rust-hir-map.cc
index 41e4b048eb5..7c4fd1ba0bd 100644
--- a/gcc/rust/util/rust-hir-map.cc
+++ b/gcc/rust/util/rust-hir-map.cc
@@ -437,22 +437,20 @@ 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) == nullptr);
+  rust_assert (!lookup_hir_extern_item (id));
 
   hirExternItemMappings[id] = {item, parent_block};
   insert_node_to_hir (item->get_mappings ().get_nodeid (), id);
 }
 
-HIR::ExternalItem *
-Mappings::lookup_hir_extern_item (HirId id, HirId *parent_block)
+tl::optional<std::pair<HIR::ExternalItem *, HirId>>
+Mappings::lookup_hir_extern_item (HirId id)
 {
   auto it = hirExternItemMappings.find (id);
   if (it == hirExternItemMappings.end ())
-    return nullptr;
-
-  *parent_block = it->second.second;
+    return tl::nullopt;
 
-  return it->second.first;
+  return it->second;
 }
 
 void
diff --git a/gcc/rust/util/rust-hir-map.h b/gcc/rust/util/rust-hir-map.h
index ff0da5556a7..da3aa9f5389 100644
--- a/gcc/rust/util/rust-hir-map.h
+++ b/gcc/rust/util/rust-hir-map.h
@@ -128,7 +128,10 @@ public:
   tl::optional<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);
+
+  // std::pair<hir_extern_item, parent hirid>
+  tl::optional<std::pair<HIR::ExternalItem *, HirId>>
+  lookup_hir_extern_item (HirId id);
 
   void insert_hir_impl_block (HIR::ImplBlock *item);
   tl::optional<HIR::ImplBlock *> lookup_hir_impl_block (HirId id);
-- 
2.45.2

Reply via email to