]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: hir: Start adapting visitors to accept multiple kinds of Paths
authorArthur Cohen <arthur.cohen@embecosm.com>
Wed, 6 Nov 2024 15:32:35 +0000 (16:32 +0100)
committerArthur Cohen <arthur.cohen@embecosm.com>
Fri, 21 Mar 2025 11:33:13 +0000 (12:33 +0100)
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.

gcc/rust/ast/rust-item.h
gcc/rust/ast/rust-path.cc
gcc/rust/hir/rust-ast-lower-type.cc
gcc/rust/hir/rust-ast-lower-type.h
gcc/rust/resolve/rust-ast-resolve-item.cc
gcc/rust/resolve/rust-ast-resolve-type.h

index a6276a72d8df12d7b721ab8695ae8e80d0e9097e..dca4aab45c83088bf24c6112c3abcd7dfd6c980c 100644 (file)
@@ -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 */
index 06c98cdcc2de8f89291fbaf40a9cfd2fbc165f31..94cf2bb1514133dba5238a880b566b6db7ce6dca 100644 (file)
@@ -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
index 58c93b9e25dd09e15c06ba93ca137794fc8db4fc..c09d60fafc6fa19f09a9e894796bfee4e5a913d3 100644 (file)
 #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)
 {
index 5bb9a7e5f7c6f72635c24ad4ddaf8258e77535fe..72c6b29d7dd05c2c163c62f25b06aae16d45ef50 100644 (file)
@@ -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;
index 245523ae5aa8b3b31fd41afcef740467e6da6736..a330541b6828daf3506f35522f9897c11053e6c8 100644 (file)
@@ -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)
     {
index 561948e85b83f7c44960d81b8dd779806931893a..ed055a1f04ea66e6f8ae40f7417b269f3ebb1e36 100644 (file)
@@ -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;