]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: toplevel: Build list of imports for Early to resolve
authorArthur Cohen <arthur.cohen@embecosm.com>
Thu, 4 Apr 2024 13:42:29 +0000 (15:42 +0200)
committerArthur Cohen <arthur.cohen@embecosm.com>
Wed, 19 Mar 2025 14:32:12 +0000 (15:32 +0100)
gcc/rust/ChangeLog:

* resolve/rust-toplevel-name-resolver-2.0.cc: Comment out handle_use
call and error emission.
* resolve/rust-toplevel-name-resolver-2.0.h: Create ImportKind class.

gcc/rust/resolve/rust-toplevel-name-resolver-2.0.cc
gcc/rust/resolve/rust-toplevel-name-resolver-2.0.h

index b18b86ca8218a22fe46bf546eaff7cba8d59013a..3ce163075081fd55675ba552da7ac6582c775de3 100644 (file)
@@ -749,21 +749,28 @@ TopLevel::visit (AST::UseDeclaration &use)
   const auto &tree = use.get_tree ();
   flatten (tree.get (), paths, glob_path, rebind_path, this->ctx);
 
-  for (auto &path : paths)
-    if (!handle_use_dec (path))
-      rust_error_at (path.get_final_segment ().get_locus (), ErrorCode::E0433,
-                    "unresolved import %qs", path.as_string ().c_str ());
-
-  for (auto &glob : glob_path)
-    if (!handle_use_glob (glob))
-      rust_error_at (glob.get_final_segment ().get_locus (), ErrorCode::E0433,
-                    "unresolved import %qs", glob.as_string ().c_str ());
-
-  for (auto &rebind : rebind_path)
-    if (!handle_rebind (rebind))
-      rust_error_at (rebind.first.get_final_segment ().get_locus (),
-                    ErrorCode::E0433, "unresolved import %qs",
-                    rebind.first.as_string ().c_str ());
+  for (auto &&path : paths)
+    imports_to_resolve.emplace_back (ImportKind::Simple (std::move (path)));
+
+  // if (!handle_use_dec (path))
+  //   rust_error_at (path.get_final_segment ().get_locus (), ErrorCode::E0433,
+  //    "unresolved import %qs", path.as_string ().c_str ());
+
+  for (auto &&glob : glob_path)
+    imports_to_resolve.emplace_back (ImportKind::Glob (std::move (glob)));
+
+  // if (!handle_use_glob (glob))
+  //   rust_error_at (glob.get_final_segment ().get_locus (), ErrorCode::E0433,
+  //    "unresolved import %qs", glob.as_string ().c_str ());
+
+  for (auto &&rebind : rebind_path)
+    imports_to_resolve.emplace_back (
+      ImportKind::Rebind (std::move (rebind.first), std::move (rebind.second)));
+
+  // if (!handle_rebind (rebind))
+  //   rust_error_at (rebind.first.get_final_segment ().get_locus (),
+  //    ErrorCode::E0433, "unresolved import %qs",
+  //    rebind.first.as_string ().c_str ());
 }
 
 } // namespace Resolver2_0
index affddb97d50c1f3debba9dab50c993d40872e7eb..12c85c86a8153d9b38a378aa8cdab58447ddce33 100644 (file)
 #ifndef RUST_TOPLEVEL_NAME_RESOLVER_2_0_H
 #define RUST_TOPLEVEL_NAME_RESOLVER_2_0_H
 
+#include "optional.h"
 #include "rust-ast-visitor.h"
+#include "rust-ast.h"
+#include "rust-item.h"
 #include "rust-name-resolution-context.h"
 #include "rust-default-resolver.h"
 
@@ -87,6 +90,62 @@ private:
   // definition and its new local name.
   std::unordered_map<NodeId, NodeId> node_forwarding;
 
+  // Each import will be transformed into an instance of `ImportKind`, a class
+  // representing some of the data we need to resolve in the
+  // `EarlyNameResolver`. Basically, for each `UseTree` that we see in
+  // `TopLevel`, create one of these. `TopLevel` should build a list of these
+  // `ImportKind`s, which `Early` can then resolve to their proper definitions.
+  // Then, a final pass will insert the definitions into the `ForeverStack` -
+  // `FinalizeImports`.
+  //
+  // Using this struct should be very simple - each path within a `UseTree`
+  // becomes one `ImportKind`. The complex case is glob imports, in which case
+  // one glob import will become one `ImportKind` which will later become
+  // multiple definitions thanks to the `GlobbingVisitor`.
+  struct ImportKind
+  {
+    enum class Kind
+    {
+      Glob,
+      Simple,
+      Rebind,
+    } kind;
+
+    static ImportKind Glob (AST::SimplePath &&to_resolve)
+    {
+      return ImportKind (Kind::Glob, std::move (to_resolve));
+    }
+
+    static ImportKind Simple (AST::SimplePath &&to_resolve)
+    {
+      return ImportKind (Kind::Simple, std::move (to_resolve));
+    }
+
+    static ImportKind Rebind (AST::SimplePath &&to_resolve,
+                             AST::UseTreeRebind &&rebind)
+    {
+      return ImportKind (Kind::Rebind, std::move (to_resolve),
+                        std::move (rebind));
+    }
+
+    // The path for `Early` to resolve.
+    AST::SimplePath to_resolve;
+
+    // The path to rebind an import to - only present if kind is Kind::Rebind
+    tl::optional<AST::UseTreeRebind> rebind;
+
+  private:
+    ImportKind (Kind kind, AST::SimplePath &&to_resolve,
+               tl::optional<AST::UseTreeRebind> &&rebind = tl::nullopt)
+      : kind (kind), to_resolve (std::move (to_resolve)),
+       rebind (std::move (rebind))
+    {}
+  };
+
+  // One of the outputs of the `TopLevel` visitor - the list of imports that
+  // `Early` should take care of resolving
+  std::vector<ImportKind> imports_to_resolve;
+
   void visit (AST::Module &module) override;
   void visit (AST::Trait &trait) override;
   void visit (AST::MacroRulesDefinition &macro) override;