[committed,73/88] gccrs: Extract query_type from TypeCheckBase to be a simple extern

Message ID 20230405140411.3016563-74-arthur.cohen@embecosm.com
State Accepted
Headers
Series [committed,01/88] gccrs: fatal_error_flag: Fix typo in error message |

Checks

Context Check Description
snail/gcc-patch-check success Github commit url

Commit Message

Arthur Cohen April 5, 2023, 2:03 p.m. UTC
  From: Philip Herron <herron.philip@googlemail.com>

Signed-off-by: Philip Herron <herron.philip@googlemail.com>

gcc/rust/ChangeLog:

	* Make-lang.in: add new dependancy
	* typecheck/rust-hir-type-check-base.cc (TypeCheckBase::query_type): refactor
	* typecheck/rust-hir-type-check-base.h: refactor
	* typecheck/rust-hir-type-check.h (RUST_HIR_TYPE_CHECK): refactor
	* typecheck/rust-type-util.cc: New file.
	* typecheck/rust-type-util.h: New file.
---
 gcc/rust/Make-lang.in                         |   1 +
 .../typecheck/rust-hir-type-check-base.cc     |  73 ------------
 gcc/rust/typecheck/rust-hir-type-check-base.h |   2 -
 gcc/rust/typecheck/rust-hir-type-check.h      |   1 +
 gcc/rust/typecheck/rust-type-util.cc          | 108 ++++++++++++++++++
 gcc/rust/typecheck/rust-type-util.h           |  38 ++++++
 6 files changed, 148 insertions(+), 75 deletions(-)
 create mode 100644 gcc/rust/typecheck/rust-type-util.cc
 create mode 100644 gcc/rust/typecheck/rust-type-util.h
  

Patch

diff --git a/gcc/rust/Make-lang.in b/gcc/rust/Make-lang.in
index 09bbe450bd5..06f64a01dce 100644
--- a/gcc/rust/Make-lang.in
+++ b/gcc/rust/Make-lang.in
@@ -134,6 +134,7 @@  GRS_OBJS = \
     rust/rust-hir-type-check-implitem.o \
     rust/rust-hir-dot-operator.o \
     rust/rust-hir-path-probe.o \
+    rust/rust-type-util.o \
     rust/rust-coercion.o \
     rust/rust-casts.o \
     rust/rust-unify.o \
diff --git a/gcc/rust/typecheck/rust-hir-type-check-base.cc b/gcc/rust/typecheck/rust-hir-type-check-base.cc
index 6e42b7b7b23..e302e27f4ab 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-base.cc
+++ b/gcc/rust/typecheck/rust-hir-type-check-base.cc
@@ -483,78 +483,5 @@  TypeCheckBase::resolve_generic_params (
     }
 }
 
-bool
-TypeCheckBase::query_type (HirId reference, TyTy::BaseType **result)
-{
-  if (context->query_in_progress (reference))
-    return false;
-
-  if (context->lookup_type (reference, result))
-    return true;
-
-  context->insert_query (reference);
-
-  HIR::Item *item = mappings->lookup_hir_item (reference);
-  if (item != nullptr)
-    {
-      rust_debug_loc (item->get_locus (), "resolved item {%u} to", reference);
-      *result = TypeCheckItem::Resolve (*item);
-      context->query_completed (reference);
-      return true;
-    }
-
-  HirId parent_impl_id = UNKNOWN_HIRID;
-  HIR::ImplItem *impl_item
-    = mappings->lookup_hir_implitem (reference, &parent_impl_id);
-  if (impl_item != nullptr)
-    {
-      HIR::ImplBlock *impl_block
-	= mappings->lookup_hir_impl_block (parent_impl_id);
-      rust_assert (impl_block != nullptr);
-
-      // found an impl item
-      rust_debug_loc (impl_item->get_locus (), "resolved impl-item {%u} to",
-		      reference);
-
-      *result = TypeCheckItem::ResolveImplItem (*impl_block, *impl_item);
-      context->query_completed (reference);
-      return true;
-    }
-
-  // is it an impl_type?
-  HIR::ImplBlock *impl_block_by_type = nullptr;
-  bool found_impl_block_type
-    = mappings->lookup_impl_block_type (reference, &impl_block_by_type);
-  if (found_impl_block_type)
-    {
-      *result = TypeCheckItem::ResolveImplBlockSelf (*impl_block_by_type);
-      context->query_completed (reference);
-      return true;
-    }
-
-  // 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)
-    {
-      HIR::ExternBlock *block
-	= mappings->lookup_hir_extern_block (parent_extern_block_id);
-      rust_assert (block != nullptr);
-
-      *result = TypeCheckTopLevelExternItem::Resolve (extern_item, *block);
-      context->query_completed (reference);
-      return true;
-    }
-
-  // more?
-  Location possible_locus = mappings->lookup_location (reference);
-  rust_debug_loc (possible_locus, "query system failed to resolve: [%u]",
-		  reference);
-  context->query_completed (reference);
-
-  return false;
-}
-
 } // namespace Resolver
 } // namespace Rust
diff --git a/gcc/rust/typecheck/rust-hir-type-check-base.h b/gcc/rust/typecheck/rust-hir-type-check-base.h
index 335014339f3..b8ff2cf6dc9 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-base.h
+++ b/gcc/rust/typecheck/rust-hir-type-check-base.h
@@ -69,8 +69,6 @@  protected:
     const std::vector<std::unique_ptr<HIR::GenericParam>> &generic_params,
     std::vector<TyTy::SubstitutionParamMapping> &substitutions);
 
-  bool query_type (HirId reference, TyTy::BaseType **result);
-
   Analysis::Mappings *mappings;
   Resolver *resolver;
   TypeCheckContext *context;
diff --git a/gcc/rust/typecheck/rust-hir-type-check.h b/gcc/rust/typecheck/rust-hir-type-check.h
index 512453947a6..6d4b9b8a62f 100644
--- a/gcc/rust/typecheck/rust-hir-type-check.h
+++ b/gcc/rust/typecheck/rust-hir-type-check.h
@@ -19,6 +19,7 @@ 
 #ifndef RUST_HIR_TYPE_CHECK
 #define RUST_HIR_TYPE_CHECK
 
+#include "rust-type-util.h"
 #include "rust-hir-full-decls.h"
 #include "rust-hir-map.h"
 #include "rust-tyty.h"
diff --git a/gcc/rust/typecheck/rust-type-util.cc b/gcc/rust/typecheck/rust-type-util.cc
new file mode 100644
index 00000000000..4e38826686b
--- /dev/null
+++ b/gcc/rust/typecheck/rust-type-util.cc
@@ -0,0 +1,108 @@ 
+// Copyright (C) 2020-2022 Free Software Foundation, Inc.
+
+// This file is part of GCC.
+
+// GCC is free software; you can redistribute it and/or modify it under
+// the terms of the GNU General Public License as published by the Free
+// Software Foundation; either version 3, or (at your option) any later
+// version.
+
+// GCC is distributed in the hope that it will be useful, but WITHOUT ANY
+// WARRANTY; without even the implied warranty of MERCHANTABILITY or
+// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+// for more details.
+
+// You should have received a copy of the GNU General Public License
+// along with GCC; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+#include "rust-type-util.h"
+#include "rust-diagnostics.h"
+#include "rust-hir-type-check.h"
+#include "rust-name-resolver.h"
+#include "rust-hir-visitor.h"
+#include "rust-hir-map.h"
+#include "rust-hir-type-check-item.h"
+#include "rust-hir-type-check-implitem.h"
+
+namespace Rust {
+namespace Resolver {
+
+bool
+query_type (HirId reference, TyTy::BaseType **result)
+{
+  Analysis::Mappings *mappings = Analysis::Mappings::get ();
+  TypeCheckContext *context = TypeCheckContext::get ();
+
+  if (context->query_in_progress (reference))
+    return false;
+
+  if (context->lookup_type (reference, result))
+    return true;
+
+  context->insert_query (reference);
+
+  HIR::Item *item = mappings->lookup_hir_item (reference);
+  if (item != nullptr)
+    {
+      rust_debug_loc (item->get_locus (), "resolved item {%u} to", reference);
+      *result = TypeCheckItem::Resolve (*item);
+      context->query_completed (reference);
+      return true;
+    }
+
+  HirId parent_impl_id = UNKNOWN_HIRID;
+  HIR::ImplItem *impl_item
+    = mappings->lookup_hir_implitem (reference, &parent_impl_id);
+  if (impl_item != nullptr)
+    {
+      HIR::ImplBlock *impl_block
+	= mappings->lookup_hir_impl_block (parent_impl_id);
+      rust_assert (impl_block != nullptr);
+
+      // found an impl item
+      rust_debug_loc (impl_item->get_locus (), "resolved impl-item {%u} to",
+		      reference);
+
+      *result = TypeCheckItem::ResolveImplItem (*impl_block, *impl_item);
+      context->query_completed (reference);
+      return true;
+    }
+
+  // is it an impl_type?
+  HIR::ImplBlock *impl_block_by_type = nullptr;
+  bool found_impl_block_type
+    = mappings->lookup_impl_block_type (reference, &impl_block_by_type);
+  if (found_impl_block_type)
+    {
+      *result = TypeCheckItem::ResolveImplBlockSelf (*impl_block_by_type);
+      context->query_completed (reference);
+      return true;
+    }
+
+  // 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)
+    {
+      HIR::ExternBlock *block
+	= mappings->lookup_hir_extern_block (parent_extern_block_id);
+      rust_assert (block != nullptr);
+
+      *result = TypeCheckTopLevelExternItem::Resolve (extern_item, *block);
+      context->query_completed (reference);
+      return true;
+    }
+
+  // more?
+  Location possible_locus = mappings->lookup_location (reference);
+  rust_debug_loc (possible_locus, "query system failed to resolve: [%u]",
+		  reference);
+  context->query_completed (reference);
+
+  return false;
+}
+
+} // namespace Resolver
+} // namespace Rust
diff --git a/gcc/rust/typecheck/rust-type-util.h b/gcc/rust/typecheck/rust-type-util.h
new file mode 100644
index 00000000000..a3f9efc003b
--- /dev/null
+++ b/gcc/rust/typecheck/rust-type-util.h
@@ -0,0 +1,38 @@ 
+// Copyright (C) 2020-2022 Free Software Foundation, Inc.
+
+// This file is part of GCC.
+
+// GCC is free software; you can redistribute it and/or modify it under
+// the terms of the GNU General Public License as published by the Free
+// Software Foundation; either version 3, or (at your option) any later
+// version.
+
+// GCC is distributed in the hope that it will be useful, but WITHOUT ANY
+// WARRANTY; without even the implied warranty of MERCHANTABILITY or
+// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+// for more details.
+
+// You should have received a copy of the GNU General Public License
+// along with GCC; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+#ifndef RUST_TYPE_UTIL
+#define RUST_TYPE_UTIL
+
+#include "rust-mapping-common.h"
+
+namespace Rust {
+
+namespace TyTy {
+class BaseType;
+}
+
+namespace Resolver {
+
+extern bool
+query_type (HirId reference, TyTy::BaseType **result);
+
+} // namespace Resolver
+} // namespace Rust
+
+#endif // RUST_TYPE_UTIL