diff options
author | Arthur Cohen <arthur.cohen@embecosm.com> | 2024-04-04 15:42:29 +0200 |
---|---|---|
committer | Arthur Cohen <arthur.cohen@embecosm.com> | 2025-03-19 15:32:12 +0100 |
commit | 8ed0cc70f73ab88218e587977cc0c181eb58453e (patch) | |
tree | 8f71888dc6fb1e661965286cd8a30dfdb9fb3562 /gcc/rust/resolve | |
parent | ca285a61c35df7da6ff652a2b2fbba8eef7d246b (diff) | |
download | gcc-8ed0cc70f73ab88218e587977cc0c181eb58453e.zip gcc-8ed0cc70f73ab88218e587977cc0c181eb58453e.tar.gz gcc-8ed0cc70f73ab88218e587977cc0c181eb58453e.tar.bz2 |
gccrs: toplevel: Build list of imports for Early to resolve
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.
Diffstat (limited to 'gcc/rust/resolve')
-rw-r--r-- | gcc/rust/resolve/rust-toplevel-name-resolver-2.0.cc | 37 | ||||
-rw-r--r-- | gcc/rust/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 b18b86c..3ce1630 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 affddb9..12c85c8 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 ¯o) override; |