https://gcc.gnu.org/g:3d2f58db04023adb74e5334cef31b67383def449

commit 3d2f58db04023adb74e5334cef31b67383def449
Author: Arthur Cohen <arthur.co...@embecosm.com>
Date:   Wed Nov 6 16:32:35 2024 +0100

    hir: Start adapting visitors to accept multiple kinds of Paths
    
    gcc/rust/ChangeLog:
    
            * ast/rust-item.h: Add new method to specifically get a type-path.
            * ast/rust-path.cc (LangItemPath::as_string): Implement properly.
            * hir/rust-ast-lower-type.cc (ASTLowerTypePath::translate): Adapt
            visitor to use the new LangItemPath.
            * hir/rust-ast-lower-type.h: Likewise.
            * resolve/rust-ast-resolve-item.cc (ResolveItem::visit): Likewise.
            * resolve/rust-ast-resolve-type.h: Likewise.

Diff:
---
 gcc/rust/ast/rust-item.h                  |  7 +++++++
 gcc/rust/ast/rust-path.cc                 |  3 +--
 gcc/rust/hir/rust-ast-lower-type.cc       |  9 +++++++++
 gcc/rust/hir/rust-ast-lower-type.h        |  1 +
 gcc/rust/resolve/rust-ast-resolve-item.cc |  2 +-
 gcc/rust/resolve/rust-ast-resolve-type.h  | 28 ++++++++++++++++++++++++++++
 6 files changed, 47 insertions(+), 3 deletions(-)

diff --git a/gcc/rust/ast/rust-item.h b/gcc/rust/ast/rust-item.h
index 0ee131390d14..02b305d816ee 100644
--- a/gcc/rust/ast/rust-item.h
+++ b/gcc/rust/ast/rust-item.h
@@ -3264,6 +3264,13 @@ public:
     return *trait_path;
   }
 
+  Type &get_trait_path_type ()
+  {
+    rust_assert (trait_path->get_path_kind () == Path::Kind::Type);
+
+    return (AST::Type &) static_cast<AST::TypePath &> (*trait_path);
+  }
+
 protected:
   /* Use covariance to implement clone function as returning this object
    * rather than base */
diff --git a/gcc/rust/ast/rust-path.cc b/gcc/rust/ast/rust-path.cc
index 58bfbb47f7c9..3a7e6e34d867 100644
--- a/gcc/rust/ast/rust-path.cc
+++ b/gcc/rust/ast/rust-path.cc
@@ -152,8 +152,7 @@ RegularPath::as_string () const
 std::string
 LangItemPath::as_string () const
 {
-  // FIXME: Handle #[lang] paths
-  rust_unreachable ();
+  return "#[lang = \"" + LangItem::ToString (kind) + "\"]";
 }
 
 SimplePath
diff --git a/gcc/rust/hir/rust-ast-lower-type.cc 
b/gcc/rust/hir/rust-ast-lower-type.cc
index 553c9c9612da..50576532ec46 100644
--- a/gcc/rust/hir/rust-ast-lower-type.cc
+++ b/gcc/rust/hir/rust-ast-lower-type.cc
@@ -19,10 +19,19 @@
 #include "rust-ast-lower-type.h"
 #include "optional.h"
 #include "rust-attribute-values.h"
+#include "rust-path.h"
 
 namespace Rust {
 namespace HIR {
 
+HIR::TypePath *
+ASTLowerTypePath::translate (AST::Path &type)
+{
+  rust_assert (type.get_path_kind () == AST::Path::Kind::Type);
+
+  return ASTLowerTypePath::translate (static_cast<AST::TypePath &> (type));
+}
+
 HIR::TypePath *
 ASTLowerTypePath::translate (AST::TypePath &type)
 {
diff --git a/gcc/rust/hir/rust-ast-lower-type.h 
b/gcc/rust/hir/rust-ast-lower-type.h
index 5207ce27b55a..ec73df27b8f3 100644
--- a/gcc/rust/hir/rust-ast-lower-type.h
+++ b/gcc/rust/hir/rust-ast-lower-type.h
@@ -31,6 +31,7 @@ protected:
   using Rust::HIR::ASTLoweringBase::visit;
 
 public:
+  static HIR::TypePath *translate (AST::Path &type);
   static HIR::TypePath *translate (AST::TypePath &type);
 
   void visit (AST::TypePathSegmentFunction &segment) override;
diff --git a/gcc/rust/resolve/rust-ast-resolve-item.cc 
b/gcc/rust/resolve/rust-ast-resolve-item.cc
index 185b82aa7f73..49c0e6f4e49e 100644
--- a/gcc/rust/resolve/rust-ast-resolve-item.cc
+++ b/gcc/rust/resolve/rust-ast-resolve-item.cc
@@ -680,7 +680,7 @@ ResolveItem::visit (AST::TraitImpl &impl_block)
 
   // setup paths
   CanonicalPath canonical_trait_type = CanonicalPath::create_empty ();
-  bool ok = ResolveTypeToCanonicalPath::go (impl_block.get_trait_path (),
+  bool ok = ResolveTypeToCanonicalPath::go (impl_block.get_trait_path_type (),
                                            canonical_trait_type);
   if (!ok)
     {
diff --git a/gcc/rust/resolve/rust-ast-resolve-type.h 
b/gcc/rust/resolve/rust-ast-resolve-type.h
index 50dd890c4e31..47c4e35a0a54 100644
--- a/gcc/rust/resolve/rust-ast-resolve-type.h
+++ b/gcc/rust/resolve/rust-ast-resolve-type.h
@@ -21,6 +21,8 @@
 
 #include "rust-ast-resolve-base.h"
 #include "rust-ast-resolve-expr.h"
+#include "rust-hir-map.h"
+#include "rust-path.h"
 
 namespace Rust {
 namespace Resolver {
@@ -56,6 +58,32 @@ class ResolveType : public ResolverBase
   using Rust::Resolver::ResolverBase::visit;
 
 public:
+  static NodeId go (AST::TypePath &type_path)
+  {
+    return ResolveType::go ((AST::Type &) type_path);
+  }
+
+  static NodeId go (AST::Path &type_path)
+  {
+    if (type_path.get_path_kind () == AST::Path::Kind::LangItem)
+      {
+       auto &type = static_cast<AST::LangItemPath &> (type_path);
+
+       Analysis::Mappings::get_lang_item (type);
+
+       type.get_node_id ();
+      }
+
+    rust_assert (type_path.get_path_kind () == AST::Path::Kind::Type);
+
+    // We have to do this dance to first downcast to a typepath, and then 
upcast
+    // to a Type. The altnernative is to split `go` into `go` and `go_inner` or
+    // something, but eventually this will need to change as we'll need
+    // `ResolveType::` to resolve other kinds of `Path`s as well.
+    return ResolveType::go (
+      (AST::Type &) static_cast<AST::TypePath &> (type_path));
+  }
+
   static NodeId go (AST::Type &type)
   {
     ResolveType resolver;

Reply via email to