From 63af481671a78f3da748646c5939f3a82f58bb01 Mon Sep 17 00:00:00 2001 From: Pierre-Emmanuel Patry Date: Wed, 2 Aug 2023 16:57:09 +0200 Subject: Move extern crate resolving Move extern crate resolving under the extern crate declaration instead of doing it under the crate's root as extern crates are not resolved by the top level resolver. gcc/rust/ChangeLog: * metadata/rust-extern-crate.cc (ExternCrate::ExternCrate): Update definition to allow Extern crate with no content (pure proc macros). (ExternCrate::ok): Panic on no content. (ExternCrate::load): Likewise. * metadata/rust-extern-crate.h: Update prototypes. * resolve/rust-toplevel-name-resolver-2.0.cc (TopLevel::go): Remove macro resolution. (TopLevel::visit): Likewise. * resolve/rust-toplevel-name-resolver-2.0.h: Add visit prototype for extern crate. * rust-session-manager.cc (Session::load_extern_crate): Adapt content depending on the loaded crate's content. * util/rust-hir-map.cc (Mappings::lookup_derive_proc_macros): Change return type to optional because it is way more convenient. (Mappings::lookup_bang_proc_macros): Likewise. (Mappings::lookup_attribute_proc_macros): Likewise. * util/rust-hir-map.h: Update function prototypes. Signed-off-by: Pierre-Emmanuel Patry --- gcc/rust/metadata/rust-extern-crate.cc | 10 +- gcc/rust/metadata/rust-extern-crate.h | 6 +- .../resolve/rust-toplevel-name-resolver-2.0.cc | 107 ++++++++++++--------- gcc/rust/resolve/rust-toplevel-name-resolver-2.0.h | 1 + gcc/rust/rust-session-manager.cc | 17 ++-- gcc/rust/util/rust-hir-map.cc | 35 +++---- gcc/rust/util/rust-hir-map.h | 13 +-- 7 files changed, 106 insertions(+), 83 deletions(-) (limited to 'gcc') diff --git a/gcc/rust/metadata/rust-extern-crate.cc b/gcc/rust/metadata/rust-extern-crate.cc index bbb9945..7d6fc58 100644 --- a/gcc/rust/metadata/rust-extern-crate.cc +++ b/gcc/rust/metadata/rust-extern-crate.cc @@ -25,9 +25,11 @@ namespace Rust { namespace Imports { -ExternCrate::ExternCrate (Import::Stream &stream, +ExternCrate::ExternCrate (Import::Stream &stream) : import_stream (stream) {} + +ExternCrate::ExternCrate (const std::string &crate_name, std::vector macros) - : import_stream (stream), proc_macros (macros) + : proc_macros (macros), crate_name (crate_name) {} ExternCrate::~ExternCrate () {} @@ -35,12 +37,14 @@ ExternCrate::~ExternCrate () {} bool ExternCrate::ok () const { - return !import_stream.saw_error (); + return !import_stream->get ().saw_error (); } bool ExternCrate::load (location_t locus) { + rust_assert (this->import_stream.has_value ()); + auto &import_stream = this->import_stream->get (); // match header import_stream.require_bytes (locus, Metadata::kMagicHeader, sizeof (Metadata::kMagicHeader)); diff --git a/gcc/rust/metadata/rust-extern-crate.h b/gcc/rust/metadata/rust-extern-crate.h index 1bfd31a..19c1f87 100644 --- a/gcc/rust/metadata/rust-extern-crate.h +++ b/gcc/rust/metadata/rust-extern-crate.h @@ -21,6 +21,7 @@ #include "rust-system.h" #include "rust-imports.h" +#include "optional.h" namespace Rust { namespace Imports { @@ -28,7 +29,8 @@ namespace Imports { class ExternCrate { public: - ExternCrate (Import::Stream &stream, + ExternCrate (Import::Stream &stream); + ExternCrate (const std::string &crate_name, std::vector macros); ~ExternCrate (); @@ -46,7 +48,7 @@ public: bool is_neg_ok, int *ret); private: - Import::Stream &import_stream; + tl::optional> import_stream; std::vector proc_macros; std::string crate_name; 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 2246788..6125298 100644 --- a/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.cc +++ b/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.cc @@ -55,51 +55,6 @@ TopLevel::go (AST::Crate &crate) { for (auto &item : crate.items) item->accept_vis (*this); - - std::vector derive_macros; - std::vector attribute_macros; - std::vector bang_macros; - - Analysis::Mappings::get ()->lookup_attribute_proc_macros ( - crate.get_node_id (), attribute_macros); - Analysis::Mappings::get ()->lookup_bang_proc_macros (crate.get_node_id (), - bang_macros); - Analysis::Mappings::get ()->lookup_derive_proc_macros (crate.get_node_id (), - derive_macros); - - for (auto &derive : derive_macros) - { - auto res = ctx.macros.insert_at_root (derive.get_trait_name (), - derive.get_node_id ()); - if (!res) - { - rust_error_at (UNKNOWN_LOCATION, ErrorCode::E0428, - "macro %qs defined multiple times", - derive.get_trait_name ().c_str ()); - } - } - for (auto &attribute : attribute_macros) - { - auto res = ctx.macros.insert_at_root (attribute.get_name (), - attribute.get_node_id ()); - if (!res) - { - rust_error_at (UNKNOWN_LOCATION, ErrorCode::E0428, - "macro %qs defined multiple times", - attribute.get_name ().c_str ()); - } - } - for (auto &bang : bang_macros) - { - auto res - = ctx.macros.insert_at_root (bang.get_name (), bang.get_node_id ()); - if (!res) - { - rust_error_at (UNKNOWN_LOCATION, ErrorCode::E0428, - "macro %qs defined multiple times", - bang.get_name ().c_str ()); - } - } } void @@ -116,6 +71,68 @@ TopLevel::visit (AST::Module &module) module.get_name ()); } +void +TopLevel::visit (AST::ExternCrate &crate) +{ + CrateNum num; + if (!Analysis::Mappings::get ()->lookup_crate_name ( + crate.get_referenced_crate (), num)) + rust_unreachable (); + + auto attribute_macros + = Analysis::Mappings::get ()->lookup_attribute_proc_macros (num); + + auto bang_macros = Analysis::Mappings::get ()->lookup_bang_proc_macros (num); + + auto derive_macros + = Analysis::Mappings::get ()->lookup_derive_proc_macros (num); + + auto sub_visitor = [&] () { + if (derive_macros.has_value ()) + for (auto &derive : derive_macros.value ()) + { + auto res = ctx.macros.insert (derive.get_trait_name (), + derive.get_node_id ()); + if (!res) + { + rust_error_at (UNKNOWN_LOCATION, ErrorCode::E0428, + "macro %qs defined multiple times", + derive.get_trait_name ().c_str ()); + } + } + if (attribute_macros.has_value ()) + for (auto &attribute : attribute_macros.value ()) + { + auto res = ctx.macros.insert (attribute.get_name (), + attribute.get_node_id ()); + if (!res) + { + rust_error_at (UNKNOWN_LOCATION, ErrorCode::E0428, + "macro %qs defined multiple times", + attribute.get_name ().c_str ()); + } + } + if (bang_macros.has_value ()) + for (auto &bang : bang_macros.value ()) + { + auto res = ctx.macros.insert (bang.get_name (), bang.get_node_id ()); + if (!res) + { + rust_error_at (UNKNOWN_LOCATION, ErrorCode::E0428, + "macro %qs defined multiple times", + bang.get_name ().c_str ()); + } + } + }; + + if (crate.has_as_clause ()) + ctx.scoped (Rib::Kind::Module, crate.get_node_id (), sub_visitor, + crate.get_as_clause ()); + else + ctx.scoped (Rib::Kind::Module, crate.get_node_id (), sub_visitor, + crate.get_referenced_crate ()); +} + static bool is_macro_export (AST::MacroRulesDefinition &def) { 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 93d40b8..b8ed106 100644 --- a/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.h +++ b/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.h @@ -73,6 +73,7 @@ private: void visit (AST::Enum &enum_item) override; void visit (AST::Union &union_item) override; void visit (AST::ConstantItem &const_item) override; + void visit (AST::ExternCrate &crate) override; }; } // namespace Resolver2_0 diff --git a/gcc/rust/rust-session-manager.cc b/gcc/rust/rust-session-manager.cc index 567702c..453dae0 100644 --- a/gcc/rust/rust-session-manager.cc +++ b/gcc/rust/rust-session-manager.cc @@ -1018,19 +1018,24 @@ Session::load_extern_crate (const std::string &crate_name, location_t locus) { s = Import::open_package (import_name, locus, relative_import_path); } - if (s.first == NULL) + if (s.first == NULL && s.second.empty ()) { rust_error_at (locus, "failed to locate crate %<%s%>", import_name.c_str ()); return UNKNOWN_NODEID; } - Imports::ExternCrate extern_crate (*s.first, s.second); - bool ok = extern_crate.load (locus); - if (!ok) + auto extern_crate = s.first == nullptr + ? Imports::ExternCrate (crate_name, s.second) + : Imports::ExternCrate (*s.first); + if (s.first != nullptr) { - rust_error_at (locus, "failed to load crate metadata"); - return UNKNOWN_NODEID; + bool ok = extern_crate.load (locus); + if (!ok) + { + rust_error_at (locus, "failed to load crate metadata"); + return UNKNOWN_NODEID; + } } // ensure the current vs this crate name don't collide diff --git a/gcc/rust/util/rust-hir-map.cc b/gcc/rust/util/rust-hir-map.cc index 139999d..fb3c78f 100644 --- a/gcc/rust/util/rust-hir-map.cc +++ b/gcc/rust/util/rust-hir-map.cc @@ -972,38 +972,31 @@ Mappings::insert_attribute_proc_macros (CrateNum num, procmacrosAttributeMappings[num] = macros; } -bool -Mappings::lookup_derive_proc_macros (CrateNum num, - std::vector ¯os) +tl::optional &> +Mappings::lookup_derive_proc_macros (CrateNum num) { auto it = procmacrosDeriveMappings.find (num); if (it == procmacrosDeriveMappings.end ()) - return false; - - macros = it->second; - return true; + return tl::nullopt; + return it->second; } -bool -Mappings::lookup_bang_proc_macros (CrateNum num, - std::vector ¯os) + +tl::optional &> +Mappings::lookup_bang_proc_macros (CrateNum num) { auto it = procmacrosBangMappings.find (num); if (it == procmacrosBangMappings.end ()) - return false; - - macros = it->second; - return true; + return tl::nullopt; + return it->second; } -bool -Mappings::lookup_attribute_proc_macros (CrateNum num, - std::vector ¯os) + +tl::optional &> +Mappings::lookup_attribute_proc_macros (CrateNum num) { auto it = procmacrosAttributeMappings.find (num); if (it == procmacrosAttributeMappings.end ()) - return false; - - macros = it->second; - return true; + return tl::nullopt; + return it->second; } void diff --git a/gcc/rust/util/rust-hir-map.h b/gcc/rust/util/rust-hir-map.h index d1b0d02..a99143e 100644 --- a/gcc/rust/util/rust-hir-map.h +++ b/gcc/rust/util/rust-hir-map.h @@ -30,6 +30,7 @@ #include "rust-privacy-common.h" #include "libproc_macro_internal/proc_macro.h" #include "rust-proc-macro.h" +#include "optional.h" namespace Rust { namespace Analysis { @@ -294,12 +295,12 @@ public: void insert_attribute_proc_macros (CrateNum num, std::vector macros); - bool lookup_derive_proc_macros (CrateNum num, - std::vector ¯os); - bool lookup_bang_proc_macros (CrateNum num, - std::vector ¯os); - bool lookup_attribute_proc_macros (CrateNum num, - std::vector ¯os); + tl::optional &> + lookup_derive_proc_macros (CrateNum num); + tl::optional &> + lookup_bang_proc_macros (CrateNum num); + tl::optional &> + lookup_attribute_proc_macros (CrateNum num); void insert_derive_proc_macro_def (NodeId id, CustomDeriveProcMacro macro); void insert_bang_proc_macro_def (NodeId id, BangProcMacro macro); -- cgit v1.1