From: Arthur Cohen <arthur.co...@embecosm.com>

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.
---
 .../rust-toplevel-name-resolver-2.0.cc        | 37 +++++++-----
 .../resolve/rust-toplevel-name-resolver-2.0.h | 59 +++++++++++++++++++
 2 files changed, 81 insertions(+), 15 deletions(-)

diff --git a/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.cc 
b/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.cc
index b18b86ca821..3ce16307508 100644
--- a/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.cc
+++ b/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.cc
@@ -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
diff --git a/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.h 
b/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.h
index affddb97d50..12c85c86a81 100644
--- a/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.h
+++ b/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.h
@@ -19,7 +19,10 @@
 #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;
-- 
2.45.2

Reply via email to